Add --tinyint-as-bool flag (v2.1.4 release)

This commit is contained in:
Patrick O'brien 2017-01-02 15:16:08 +10:00
parent 5449ce7c6c
commit dea748d409
4 changed files with 42 additions and 10 deletions

View file

@ -23,6 +23,12 @@ type Column struct {
// https://www.postgresql.org/docs/9.1/static/infoschema-element-types.html // https://www.postgresql.org/docs/9.1/static/infoschema-element-types.html
ArrType *string ArrType *string
UDTName 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. // ColumnNames of the columns.

View file

@ -11,6 +11,12 @@ import (
"github.com/vattle/sqlboiler/bdb" "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 // MySQLDriver holds the database connection string and a handle
// to the database connection. // to the database connection.
type MySQLDriver struct { type MySQLDriver struct {
@ -123,6 +129,7 @@ func (m *MySQLDriver) Columns(schema, tableName string) ([]bdb.Column, error) {
rows, err := m.dbConn.Query(` rows, err := m.dbConn.Query(`
select select
c.column_name, c.column_name,
c.column_type,
if(c.data_type = 'enum', c.column_type, c.data_type), if(c.data_type = 'enum', c.column_type, c.data_type),
if(extra = 'auto_increment','auto_increment', c.column_default), if(extra = 'auto_increment','auto_increment', c.column_default),
c.is_nullable = 'YES', c.is_nullable = 'YES',
@ -144,19 +151,21 @@ func (m *MySQLDriver) Columns(schema, tableName string) ([]bdb.Column, error) {
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var colName, colType string var colName, colType, colFullType string
var nullable, unique bool var nullable, unique bool
var defaultValue *string 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) return nil, errors.Wrapf(err, "unable to scan for table %s", tableName)
} }
column := bdb.Column{ column := bdb.Column{
Name: colName, Name: colName,
DBType: colType, FullDBType: colFullType, // example: tinyint(1) instead of tinyint
Nullable: nullable, DBType: colType,
Unique: unique, Nullable: nullable,
Unique: unique,
} }
if defaultValue != nil && *defaultValue != "NULL" { if defaultValue != nil && *defaultValue != "NULL" {
column.Default = *defaultValue column.Default = *defaultValue
} }
@ -260,7 +269,12 @@ func (m *MySQLDriver) TranslateColumnType(c bdb.Column) bdb.Column {
if c.Nullable { if c.Nullable {
switch c.DBType { switch c.DBType {
case "tinyint": 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": case "smallint":
c.Type = "null.Int16" c.Type = "null.Int16"
case "mediumint": case "mediumint":
@ -287,7 +301,12 @@ func (m *MySQLDriver) TranslateColumnType(c bdb.Column) bdb.Column {
} else { } else {
switch c.DBType { switch c.DBType {
case "tinyint": 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": case "smallint":
c.Type = "int16" c.Type = "int16"
case "mediumint": case "mediumint":

View file

@ -10,9 +10,10 @@ import (
"github.com/kat-co/vala" "github.com/kat-co/vala"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/vattle/sqlboiler/bdb/drivers"
) )
const sqlBoilerVersion = "2.1.3" const sqlBoilerVersion = "2.1.4"
var ( var (
cmdState *State cmdState *State
@ -82,6 +83,7 @@ func main() {
rootCmd.PersistentFlags().BoolP("no-hooks", "", false, "Disable hooks feature for your models") 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("no-auto-timestamps", "", false, "Disable automatic timestamps for created_at/updated_at")
rootCmd.PersistentFlags().BoolP("version", "", false, "Print the version") 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.sslmode", "require")
viper.SetDefault("postgres.port", "5432") viper.SetDefault("postgres.port", "5432")
@ -206,6 +208,9 @@ func preRun(cmd *cobra.Command, args []string) error {
SSLMode: viper.GetString("mysql.sslmode"), 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 // MySQL doesn't have schemas, just databases
cmdConfig.Schema = cmdConfig.MySQL.DBName cmdConfig.Schema = cmdConfig.MySQL.DBName

View file

@ -135,7 +135,9 @@ CREATE TABLE magicest (
aaaa char NULL, aaaa char NULL,
bbbb char NOT NULL, bbbb char NOT NULL,
cccc text NULL, cccc text NULL,
dddd text NOT NULL dddd text NOT NULL,
eeee tinyint(2) NULL,
ffff tinyint(2) NOT NULL
); );
create table owner ( create table owner (