// MyParseType parse a mysql type into a Go type based on the column // definition. func MyParseType(args *internal.ArgType, dt string, nullable bool) (int, string, string) { precision := 0 nilVal := "nil" unsigned := false // extract unsigned if strings.HasSuffix(dt, " unsigned") { unsigned = true dt = dt[:len(dt)-len(" unsigned")] } // extract precision dt, precision, _ = args.ParsePrecision(dt) var typ string switchDT: switch dt { case "bit": nilVal = "0" if precision == 1 { nilVal = "false" typ = "bool" if nullable { nilVal = "sql.NullBool{}" typ = "sql.NullBool" } break switchDT } else if precision <= 8 { typ = "uint8" } else if precision <= 16 { typ = "uint16" } else if precision <= 32 { typ = "uint32" } else { typ = "uint64" } if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "bool", "boolean": nilVal = "false" typ = "bool" if nullable { nilVal = "sql.NullBool{}" typ = "sql.NullBool" } case "char", "varchar", "tinytext", "text", "mediumtext", "longtext": nilVal = `""` typ = "string" if nullable { nilVal = "sql.NullString{}" typ = "sql.NullString" } case "tinyint", "smallint": nilVal = "0" typ = "int16" if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "mediumint", "int", "integer": nilVal = "0" typ = args.Int32Type if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "bigint": nilVal = "0" typ = "int64" if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "float": nilVal = "0.0" typ = "float32" if nullable { nilVal = "sql.NullFloat64{}" typ = "sql.NullFloat64" } case "decimal", "double": nilVal = "0.0" typ = "float64" if nullable { nilVal = "sql.NullFloat64{}" typ = "sql.NullFloat64" } case "binary", "varbinary", "tinyblob", "blob", "mediumblob", "longblob": typ = "[]byte" case "timestamp", "datetime", "date": typ = "*time.Time" if nullable { nilVal = "mysql.NullTime{}" typ = "mysql.NullTime" } default: if strings.HasPrefix(dt, args.Schema+".") { // in the same schema, so chop off typ = internal.SnakeToIdentifier(dt[len(args.Schema)+1:]) nilVal = typ + "(0)" } else { typ = internal.SnakeToIdentifier(dt) nilVal = typ + "{}" } } // add 'u' as prefix to type if its unsigned // FIXME: this needs to be tested properly... if unsigned && internal.IntRE.MatchString(typ) { typ = "u" + typ } return precision, nilVal, typ }
// PgParseType parse a postgres type into a Go type based on the column // definition. func PgParseType(args *internal.ArgType, dt string, nullable bool) (int, string, string) { precision := 0 nilVal := "nil" asSlice := false // handle SETOF if strings.HasPrefix(dt, "SETOF ") { _, _, t := PgParseType(args, dt[len("SETOF "):], false) return 0, "nil", "[]" + t } // determine if it's a slice if strings.HasSuffix(dt, "[]") { dt = dt[:len(dt)-2] asSlice = true } // extract precision dt, precision, _ = args.ParsePrecision(dt) var typ string switch dt { case "boolean": nilVal = "false" typ = "bool" if nullable { nilVal = "sql.NullBool{}" typ = "sql.NullBool" } case "character", "character varying", "text", "money": nilVal = `""` typ = "string" if nullable { nilVal = "sql.NullString{}" typ = "sql.NullString" } case "smallint": nilVal = "0" typ = "int16" if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "integer": nilVal = "0" typ = args.Int32Type if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "bigint": nilVal = "0" typ = "int64" if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "smallserial": nilVal = "0" typ = "uint16" if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "serial": nilVal = "0" typ = args.Uint32Type if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "bigserial": nilVal = "0" typ = "uint64" if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "real": nilVal = "0.0" typ = "float32" if nullable { nilVal = "sql.NullFloat64{}" typ = "sql.NullFloat64" } case "numeric", "double precision": nilVal = "0.0" typ = "float64" if nullable { nilVal = "sql.NullFloat64{}" typ = "sql.NullFloat64" } case "bytea": asSlice = true typ = "byte" case "date", "timestamp with time zone", "time with time zone", "time without time zone", "timestamp without time zone": typ = "*time.Time" if nullable { nilVal = "pq.NullTime{}" typ = "pq.NullTime" } case "interval": typ = "*time.Duration" case `"char"`, "bit": // FIXME: this needs to actually be tested ... // i think this should be 'rune' but I don't think database/sql // supports 'rune' as a type? // // this is mainly here because postgres's pg_catalog.* meta tables have // this as a type. //typ = "rune" nilVal = `uint8(0)` typ = "uint8" case `"any"`, "bit varying": asSlice = true typ = "byte" default: if strings.HasPrefix(dt, args.Schema+".") { // in the same schema, so chop off typ = internal.SnakeToIdentifier(dt[len(args.Schema)+1:]) nilVal = typ + "(0)" } else { typ = internal.SnakeToIdentifier(dt) nilVal = typ + "{}" } } // special case for []slice if typ == "string" && asSlice { return precision, "StringSlice{}", "StringSlice" } // correct type if slice if asSlice { typ = "[]" + typ nilVal = "nil" } return precision, nilVal, typ }
// MsParseType parse a postgres type into a Go type based on the column // definition. func MsParseType(args *internal.ArgType, dt string, nullable bool) (int, string, string) { precision := 0 nilVal := "nil" // extract precision dt, precision, _ = args.ParsePrecision(dt) var typ string switch dt { case "tinyint", "bit": nilVal = "false" typ = "bool" if nullable { nilVal = "sql.NullBool{}" typ = "sql.NullBool" } case "char", "varchar", "text", "nchar", "nvarchar", "ntext", "smallmoney", "money": nilVal = `""` typ = "string" if nullable { nilVal = "sql.NullString{}" typ = "sql.NullString" } case "smallint": nilVal = "0" typ = "int16" if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "int": nilVal = "0" typ = args.Int32Type if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "bigint": nilVal = "0" typ = "int64" if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "smallserial": nilVal = "0" typ = "uint16" if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "serial": nilVal = "0" typ = args.Uint32Type if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "bigserial": nilVal = "0" typ = "uint64" if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "real": nilVal = "0.0" typ = "float32" if nullable { nilVal = "sql.NullFloat64{}" typ = "sql.NullFloat64" } case "numeric", "decimal": nilVal = "0.0" typ = "float64" if nullable { nilVal = "sql.NullFloat64{}" typ = "sql.NullFloat64" } case "binary", "varbinary": typ = "[]byte" case "datetime", "datetime2", "timestamp": nilVal = "time.Time{}" typ = "time.Time" case "time with time zone", "time without time zone", "timestamp without time zone": nilVal = "0" typ = "int64" if nullable { nilVal = "sql.NullInt64{}" typ = "sql.NullInt64" } case "interval": typ = "*time.Duration" default: if strings.HasPrefix(dt, args.Schema+".") { // in the same schema, so chop off typ = internal.SnakeToIdentifier(dt[len(args.Schema)+1:]) nilVal = typ + "(0)" } else { typ = internal.SnakeToIdentifier(dt) nilVal = typ + "{}" } } return precision, nilVal, typ }