From dee1240a0481559c8356a83261b7459bbd959670 Mon Sep 17 00:00:00 2001 From: Nick Kotenberg Date: Sun, 18 Feb 2018 16:27:17 -0700 Subject: [PATCH 1/8] Finished --- .gitignore | 3 +++ main.go | 52 +++++++++++++++++++++++++++++++++++++++++++--------- test.json | 6 +++--- 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 0026861..bca4f66 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,6 @@ _cgo_export.* _testmain.go *.exe + +# Ignore the file that was created on test +db_structs.go diff --git a/main.go b/main.go index 69e9980..6e4aafb 100644 --- a/main.go +++ b/main.go @@ -13,11 +13,25 @@ import ( ) var defaults = Configuration{ - DbUser: "db_user", - DbPassword: "db_pw", - DbName: "bd_name", - PkgName: "DbStructs", - TagLabel: "db", + DbUser: "db_user", + DbPassword: "db_pw", + DbName: "bd_name", + PkgName: "DbStructs", + TagLabel: "db", + Xorm: false, + OnlyBaseTables: false, +} + +func init() { + + flag.StringVar(&defaults.DbUser, "user", "root", "Set the user for the db connection") + flag.StringVar(&defaults.DbPassword, "pass", "pass", "Set the pass for the db connection") + flag.StringVar(&defaults.DbName, "db", "database", "Set the pass for the db connection") + + flag.BoolVar(&defaults.Xorm, "xorm", false, "Xorm support.") + flag.BoolVar(&defaults.OnlyBaseTables, "base", false, "Sets whether to only use base tables.") + + flag.Parse() } var config Configuration @@ -30,6 +44,9 @@ type Configuration struct { PkgName string `json:"pkg_name"` // TagLabel produces tags commonly used to match database field names with Go struct members TagLabel string `json:"tag_label"` + // Adds the tablename return for the xorm ORM + Xorm bool `json:"xorm"` + OnlyBaseTables bool `json:"only_base_tables"` } type ColumnSchema struct { @@ -62,6 +79,11 @@ func writeStructs(schemas []ColumnSchema) (int, error) { if cs.TableName != currentTable { if currentTable != "" { out = out + "}\n\n" + if config.Xorm { + out = out + "func (t *" + formatName(currentTable) + ") TableName() string {\n" + + "\t return \"" + currentTable + "\"\n" + + "}\n\n" + } } out = out + "type " + formatName(cs.TableName) + " struct{\n" } @@ -107,9 +129,21 @@ func getSchema() []ColumnSchema { log.Fatal(err) } defer conn.Close() - q := "SELECT TABLE_NAME, COLUMN_NAME, IS_NULLABLE, DATA_TYPE, " + - "CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, COLUMN_TYPE, " + - "COLUMN_KEY FROM COLUMNS WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME, ORDINAL_POSITION" + q := "SELECT COLUMNS.TABLE_NAME, COLUMNS.COLUMN_NAME, COLUMNS.IS_NULLABLE, COLUMNS.DATA_TYPE, " + + "COLUMNS.CHARACTER_MAXIMUM_LENGTH, COLUMNS.NUMERIC_PRECISION, COLUMNS.NUMERIC_SCALE, COLUMNS.COLUMN_TYPE, " + + "COLUMNS.COLUMN_KEY FROM COLUMNS " + + if config.OnlyBaseTables { + q = q + "LEFT JOIN TABLES ON TABLES.TABLE_NAME = COLUMNS.TABLE_NAME AND TABLES.TABLE_SCHEMA = COLUMNS.TABLE_SCHEMA " + } + + q = q + "WHERE COLUMNS.TABLE_SCHEMA = ? " + + if config.OnlyBaseTables { + q = q + "AND TABLES.TABLE_TYPE = \"BASE TABLE\" " + } + + q = q + "ORDER BY COLUMNS.TABLE_NAME, COLUMNS.ORDINAL_POSITION" rows, err := conn.Query(q, config.DbName) if err != nil { log.Fatal(err) @@ -184,7 +218,7 @@ var configFile = flag.String("json", "", "Config file") func main() { flag.Parse() - + if len(*configFile) > 0 { f, err := os.Open(*configFile) if err != nil { diff --git a/test.json b/test.json index a952580..1e07bdf 100644 --- a/test.json +++ b/test.json @@ -1,7 +1,7 @@ { - "db_user": "db_user", - "db_password": "db_pass", - "db_name": "db_name", + "db_user": "test", + "db_password": "test", + "db_name": "test", "pkg_name": "JsonTest", "tag_label": "db" } \ No newline at end of file From 7f834bfffa5e4a3166a86e7c373b4e38b81d4721 Mon Sep 17 00:00:00 2001 From: Nick Kotenberg Date: Sun, 18 Feb 2018 16:37:37 -0700 Subject: [PATCH 2/8] Finished --- main.go | 39 +++++++++++++++------------------------ test.json | 6 ++++-- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/main.go b/main.go index 6e4aafb..6a7c492 100644 --- a/main.go +++ b/main.go @@ -13,25 +13,14 @@ import ( ) var defaults = Configuration{ - DbUser: "db_user", - DbPassword: "db_pw", - DbName: "bd_name", - PkgName: "DbStructs", - TagLabel: "db", - Xorm: false, - OnlyBaseTables: false, -} - -func init() { - - flag.StringVar(&defaults.DbUser, "user", "root", "Set the user for the db connection") - flag.StringVar(&defaults.DbPassword, "pass", "pass", "Set the pass for the db connection") - flag.StringVar(&defaults.DbName, "db", "database", "Set the pass for the db connection") - - flag.BoolVar(&defaults.Xorm, "xorm", false, "Xorm support.") - flag.BoolVar(&defaults.OnlyBaseTables, "base", false, "Sets whether to only use base tables.") - - flag.Parse() + DbUser: "db_user", + DbPassword: "db_pw", + DbName: "bd_name", + PkgName: "DbStructs", + TagLabel: "db", + Xorm: false, + OnlyBaseTables: false, + IgnoreNullables: false, } var config Configuration @@ -45,8 +34,9 @@ type Configuration struct { // TagLabel produces tags commonly used to match database field names with Go struct members TagLabel string `json:"tag_label"` // Adds the tablename return for the xorm ORM - Xorm bool `json:"xorm"` - OnlyBaseTables bool `json:"only_base_tables"` + Xorm bool `json:"xorm"` + OnlyBaseTables bool `json:"only_base_tables"` + IgnoreNullables bool `json:"ignore_nullables"` } type ColumnSchema struct { @@ -62,6 +52,7 @@ type ColumnSchema struct { } func writeStructs(schemas []ColumnSchema) (int, error) { + file, err := os.Create("db_structs.go") if err != nil { log.Fatal(err) @@ -185,7 +176,7 @@ func goType(col *ColumnSchema) (string, string, error) { var gt string = "" switch col.DataType { case "char", "varchar", "enum", "set", "text", "longtext", "mediumtext", "tinytext": - if col.IsNullable == "YES" { + if col.IsNullable == "YES" && !config.IgnoreNullables { gt = "sql.NullString" } else { gt = "string" @@ -195,13 +186,13 @@ func goType(col *ColumnSchema) (string, string, error) { case "date", "time", "datetime", "timestamp": gt, requiredImport = "time.Time", "time" case "bit", "tinyint", "smallint", "int", "mediumint", "bigint": - if col.IsNullable == "YES" { + if col.IsNullable == "YES" && !config.IgnoreNullables { gt = "sql.NullInt64" } else { gt = "int64" } case "float", "decimal", "double": - if col.IsNullable == "YES" { + if col.IsNullable == "YES" && !config.IgnoreNullables { gt = "sql.NullFloat64" } else { gt = "float64" diff --git a/test.json b/test.json index 1e07bdf..c741eb8 100644 --- a/test.json +++ b/test.json @@ -1,7 +1,9 @@ { "db_user": "test", "db_password": "test", - "db_name": "test", + "db_name": "fusion", "pkg_name": "JsonTest", - "tag_label": "db" + "tag_label": "db", + "xorm":true, + "only_base_tables":true } \ No newline at end of file From 25ede2ffa1fe17cdf8be1423ca5cd47509c01784 Mon Sep 17 00:00:00 2001 From: Nick Kotenberg Date: Sun, 18 Feb 2018 16:38:00 -0700 Subject: [PATCH 3/8] Finished --- test.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.json b/test.json index c741eb8..f8b8296 100644 --- a/test.json +++ b/test.json @@ -1,7 +1,7 @@ { "db_user": "test", "db_password": "test", - "db_name": "fusion", + "db_name": "test", "pkg_name": "JsonTest", "tag_label": "db", "xorm":true, From ab959137262f0e156711e8c426f3abf9a1afb0fd Mon Sep 17 00:00:00 2001 From: Nick Kotenberg Date: Sun, 18 Feb 2018 16:39:23 -0700 Subject: [PATCH 4/8] Finished --- test.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test.json b/test.json index f8b8296..61fb1c4 100644 --- a/test.json +++ b/test.json @@ -1,9 +1,10 @@ { "db_user": "test", "db_password": "test", - "db_name": "test", + "db_name": "fusion", "pkg_name": "JsonTest", "tag_label": "db", "xorm":true, - "only_base_tables":true + "only_base_tables":true, + "ignore_nullables":true } \ No newline at end of file From 477ffee698835968a0621eaae755edbd7ad0b1ec Mon Sep 17 00:00:00 2001 From: Nick Kotenberg Date: Sun, 18 Feb 2018 16:40:45 -0700 Subject: [PATCH 5/8] Finished --- test.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.json b/test.json index 61fb1c4..66fcf33 100644 --- a/test.json +++ b/test.json @@ -1,7 +1,7 @@ { "db_user": "test", "db_password": "test", - "db_name": "fusion", + "db_name": "test", "pkg_name": "JsonTest", "tag_label": "db", "xorm":true, From 0f25fa2192c5b1ffdb339890aa40e163338ff1c0 Mon Sep 17 00:00:00 2001 From: Nick Kotenberg Date: Sun, 18 Feb 2018 16:44:32 -0700 Subject: [PATCH 6/8] Finished --- main.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 6a7c492..86de0cc 100644 --- a/main.go +++ b/main.go @@ -89,7 +89,17 @@ func writeStructs(schemas []ColumnSchema) (int, error) { } out = out + "\t" + formatName(cs.ColumnName) + " " + goType if len(config.TagLabel) > 0 { - out = out + "\t`" + config.TagLabel + ":\"" + cs.ColumnName + "\"`" + if config.Xorm { + out = out + "\t`" + config.TagLabel + ":" + if cs.ColumnName == "id" { + out = out + "\"'" + cs.ColumnName + "' pk autoincr" + } else { + out = out + "\"" + cs.ColumnName + } + out = out + "\" json:\"" + cs.ColumnName + "\"`" + } else { + out = out + "\t`" + config.TagLabel + ":\"" + cs.ColumnName + "\"`" + } } out = out + "\n" currentTable = cs.TableName From cd856dc98c7d2d80fa1c3d746c9891acac7bd3c8 Mon Sep 17 00:00:00 2001 From: Nick Kotenberg Date: Sun, 18 Feb 2018 16:44:54 -0700 Subject: [PATCH 7/8] Finished --- test.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.json b/test.json index 66fcf33..61fb1c4 100644 --- a/test.json +++ b/test.json @@ -1,7 +1,7 @@ { "db_user": "test", "db_password": "test", - "db_name": "test", + "db_name": "fusion", "pkg_name": "JsonTest", "tag_label": "db", "xorm":true, From 20af87295d5eea4bbd130c4c793701481b11a098 Mon Sep 17 00:00:00 2001 From: Nick Kotenberg Date: Tue, 27 Feb 2018 08:49:50 -0700 Subject: [PATCH 8/8] Finished --- main.go | 36 +++++++++++++++++++++++++++++------- test.json | 6 +++--- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 86de0cc..78bbfee 100644 --- a/main.go +++ b/main.go @@ -71,9 +71,17 @@ func writeStructs(schemas []ColumnSchema) (int, error) { if currentTable != "" { out = out + "}\n\n" if config.Xorm { - out = out + "func (t *" + formatName(currentTable) + ") TableName() string {\n" + + out = out + "func (t " + formatName(currentTable) + ") TableName() string {\n" + "\t return \"" + currentTable + "\"\n" + "}\n\n" + + out = out + "func (t " + formatName(currentTable) + ") SetId(id int64) {\n" + + "\tt.Id = id\n" + + "}\n\n" + + out = out + "func (t " + formatName(currentTable) + ") GetId() int64 {\n" + + "\treturn t.Id\n" + + "}\n\n" } } out = out + "type " + formatName(cs.TableName) + " struct{\n" @@ -96,10 +104,20 @@ func writeStructs(schemas []ColumnSchema) (int, error) { } else { out = out + "\"" + cs.ColumnName } - out = out + "\" json:\"" + cs.ColumnName + "\"`" + out = out + "\" json:\"" + cs.ColumnName + "\"" } else { - out = out + "\t`" + config.TagLabel + ":\"" + cs.ColumnName + "\"`" + out = out + "\t`" + config.TagLabel + ":\"" + cs.ColumnName + "\"" } + + // Need to make this an option at some point + if true { + out = out + " schema:\"" + cs.ColumnName + "\"" + if goType == "bool" { + out = out + " sql:\"default: false\"" + } + } + + out = out + "`" } out = out + "\n" currentTable = cs.TableName @@ -180,7 +198,7 @@ func formatName(name string) string { func goType(col *ColumnSchema) (string, string, error) { requiredImport := "" - if col.IsNullable == "YES" { + if col.IsNullable == "YES" && !config.IgnoreNullables { requiredImport = "database/sql" } var gt string = "" @@ -196,10 +214,14 @@ func goType(col *ColumnSchema) (string, string, error) { case "date", "time", "datetime", "timestamp": gt, requiredImport = "time.Time", "time" case "bit", "tinyint", "smallint", "int", "mediumint", "bigint": - if col.IsNullable == "YES" && !config.IgnoreNullables { - gt = "sql.NullInt64" + if col.ColumnType == "tinyint(1) unsigned" { + gt = "bool" } else { - gt = "int64" + if col.IsNullable == "YES" && !config.IgnoreNullables { + gt = "sql.NullInt64" + } else { + gt = "int64" + } } case "float", "decimal", "double": if col.IsNullable == "YES" && !config.IgnoreNullables { diff --git a/test.json b/test.json index 61fb1c4..abdeb7d 100644 --- a/test.json +++ b/test.json @@ -1,9 +1,9 @@ { "db_user": "test", "db_password": "test", - "db_name": "fusion", - "pkg_name": "JsonTest", - "tag_label": "db", + "db_name": "go_fusion", + "pkg_name": "db", + "tag_label": "xorm", "xorm":true, "only_base_tables":true, "ignore_nullables":true