Add --tinyint-as-bool flag (v2.1.4 release)
This commit is contained in:
parent
5449ce7c6c
commit
dea748d409
4 changed files with 42 additions and 10 deletions
|
@ -23,6 +23,12 @@ type Column struct {
|
|||
// https://www.postgresql.org/docs/9.1/static/infoschema-element-types.html
|
||||
ArrType *string
|
||||
UDTName string
|
||||
|
||||
// MySQL only bits
|
||||
// Used to get full type, ex:
|
||||
// tinyint(1) instead of tinyint
|
||||
// Used for "tinyint-as-bool" flag
|
||||
FullDBType string
|
||||
}
|
||||
|
||||
// ColumnNames of the columns.
|
||||
|
|
|
@ -11,6 +11,12 @@ import (
|
|||
"github.com/vattle/sqlboiler/bdb"
|
||||
)
|
||||
|
||||
// TinyintAsBool is a global that is set from main.go if a user specifies
|
||||
// this flag when generating. This flag only applies to MySQL so we're using
|
||||
// a global instead, to avoid breaking the interface. If TinyintAsBool is true
|
||||
// then tinyint(1) will be mapped in your generated structs to bool opposed to int8.
|
||||
var TinyintAsBool bool
|
||||
|
||||
// MySQLDriver holds the database connection string and a handle
|
||||
// to the database connection.
|
||||
type MySQLDriver struct {
|
||||
|
@ -123,6 +129,7 @@ func (m *MySQLDriver) Columns(schema, tableName string) ([]bdb.Column, error) {
|
|||
rows, err := m.dbConn.Query(`
|
||||
select
|
||||
c.column_name,
|
||||
c.column_type,
|
||||
if(c.data_type = 'enum', c.column_type, c.data_type),
|
||||
if(extra = 'auto_increment','auto_increment', c.column_default),
|
||||
c.is_nullable = 'YES',
|
||||
|
@ -144,19 +151,21 @@ func (m *MySQLDriver) Columns(schema, tableName string) ([]bdb.Column, error) {
|
|||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var colName, colType string
|
||||
var colName, colType, colFullType string
|
||||
var nullable, unique bool
|
||||
var defaultValue *string
|
||||
if err := rows.Scan(&colName, &colType, &defaultValue, &nullable, &unique); err != nil {
|
||||
if err := rows.Scan(&colName, &colFullType, &colType, &defaultValue, &nullable, &unique); err != nil {
|
||||
return nil, errors.Wrapf(err, "unable to scan for table %s", tableName)
|
||||
}
|
||||
|
||||
column := bdb.Column{
|
||||
Name: colName,
|
||||
DBType: colType,
|
||||
Nullable: nullable,
|
||||
Unique: unique,
|
||||
Name: colName,
|
||||
FullDBType: colFullType, // example: tinyint(1) instead of tinyint
|
||||
DBType: colType,
|
||||
Nullable: nullable,
|
||||
Unique: unique,
|
||||
}
|
||||
|
||||
if defaultValue != nil && *defaultValue != "NULL" {
|
||||
column.Default = *defaultValue
|
||||
}
|
||||
|
@ -260,7 +269,12 @@ func (m *MySQLDriver) TranslateColumnType(c bdb.Column) bdb.Column {
|
|||
if c.Nullable {
|
||||
switch c.DBType {
|
||||
case "tinyint":
|
||||
c.Type = "null.Int8"
|
||||
// map tinyint(1) to bool if TinyintAsBool is true
|
||||
if TinyintAsBool && c.FullDBType == "tinyint(1)" {
|
||||
c.Type = "null.Bool"
|
||||
} else {
|
||||
c.Type = "null.Int8"
|
||||
}
|
||||
case "smallint":
|
||||
c.Type = "null.Int16"
|
||||
case "mediumint":
|
||||
|
@ -287,7 +301,12 @@ func (m *MySQLDriver) TranslateColumnType(c bdb.Column) bdb.Column {
|
|||
} else {
|
||||
switch c.DBType {
|
||||
case "tinyint":
|
||||
c.Type = "int8"
|
||||
// map tinyint(1) to bool if TinyintAsBool is true
|
||||
if TinyintAsBool && c.FullDBType == "tinyint(1)" {
|
||||
c.Type = "bool"
|
||||
} else {
|
||||
c.Type = "int8"
|
||||
}
|
||||
case "smallint":
|
||||
c.Type = "int16"
|
||||
case "mediumint":
|
||||
|
|
7
main.go
7
main.go
|
@ -10,9 +10,10 @@ import (
|
|||
"github.com/kat-co/vala"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/vattle/sqlboiler/bdb/drivers"
|
||||
)
|
||||
|
||||
const sqlBoilerVersion = "2.1.3"
|
||||
const sqlBoilerVersion = "2.1.4"
|
||||
|
||||
var (
|
||||
cmdState *State
|
||||
|
@ -82,6 +83,7 @@ func main() {
|
|||
rootCmd.PersistentFlags().BoolP("no-hooks", "", false, "Disable hooks feature for your models")
|
||||
rootCmd.PersistentFlags().BoolP("no-auto-timestamps", "", false, "Disable automatic timestamps for created_at/updated_at")
|
||||
rootCmd.PersistentFlags().BoolP("version", "", false, "Print the version")
|
||||
rootCmd.PersistentFlags().BoolP("tinyint-as-bool", "", false, "Map MySQL tinyint(1) in Go to bool instead of int8")
|
||||
|
||||
viper.SetDefault("postgres.sslmode", "require")
|
||||
viper.SetDefault("postgres.port", "5432")
|
||||
|
@ -206,6 +208,9 @@ func preRun(cmd *cobra.Command, args []string) error {
|
|||
SSLMode: viper.GetString("mysql.sslmode"),
|
||||
}
|
||||
|
||||
// Set MySQL TinyintAsBool global var. This flag only applies to MySQL.
|
||||
drivers.TinyintAsBool = viper.GetBool("tinyint-as-bool")
|
||||
|
||||
// MySQL doesn't have schemas, just databases
|
||||
cmdConfig.Schema = cmdConfig.MySQL.DBName
|
||||
|
||||
|
|
4
testdata/mysql_test_schema.sql
vendored
4
testdata/mysql_test_schema.sql
vendored
|
@ -135,7 +135,9 @@ CREATE TABLE magicest (
|
|||
aaaa char NULL,
|
||||
bbbb char NOT NULL,
|
||||
cccc text NULL,
|
||||
dddd text NOT NULL
|
||||
dddd text NOT NULL,
|
||||
eeee tinyint(2) NULL,
|
||||
ffff tinyint(2) NOT NULL
|
||||
);
|
||||
|
||||
create table owner (
|
||||
|
|
Loading…
Add table
Reference in a new issue