From 6693ca59618240c7ce48848c415c195fac413bdb Mon Sep 17 00:00:00 2001 From: Sol Cates Date: Fri, 17 Mar 2017 13:44:05 -0700 Subject: [PATCH 1/8] Added support for DateTime in relation to using go-swagger generated models --- dialect_mysql.go | 5 ++++- dialect_mysql_test.go | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/dialect_mysql.go b/dialect_mysql.go index 3d7d3402..10dc6c12 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -15,6 +15,7 @@ import ( "fmt" "reflect" "strings" + "log" ) // Implementation of Dialect for MySQL databases. @@ -58,7 +59,7 @@ func (d MySQLDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) return "mediumblob" } } - + log.Println(val.Name()) switch val.Name() { case "NullInt64": return "bigint" @@ -68,6 +69,8 @@ func (d MySQLDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) return "tinyint" case "Time": return "datetime" + case "DateTime": + return "datetime" } if maxsize < 1 { diff --git a/dialect_mysql_test.go b/dialect_mysql_test.go index d1018cf6..8ed05b1b 100644 --- a/dialect_mysql_test.go +++ b/dialect_mysql_test.go @@ -22,6 +22,7 @@ import ( . "github.com/onsi/gomega" "github.com/go-gorp/gorp" + "github.com/go-openapi/strfmt" ) var _ = Describe("MySQLDialect", func() { @@ -61,6 +62,7 @@ var _ = Describe("MySQLDialect", func() { Entry("NullFloat64", sql.NullFloat64{}, 0, false, "double"), Entry("NullBool", sql.NullBool{}, 0, false, "tinyint"), Entry("Time", time.Time{}, 0, false, "datetime"), + Entry("DateTime", strfmt.DateTime{}, 0, false, "datetime"), Entry("default-size string", "", 0, false, "varchar(255)"), Entry("sized string", "", 50, false, "varchar(50)"), Entry("large string", "", 1024, false, "text"), From 7d6864d362a604b61ebdaeed8d07a65dd2a0a3f1 Mon Sep 17 00:00:00 2001 From: Sol Cates Date: Fri, 17 Mar 2017 13:45:10 -0700 Subject: [PATCH 2/8] Removed debug logging --- dialect_mysql.go | 1 - 1 file changed, 1 deletion(-) diff --git a/dialect_mysql.go b/dialect_mysql.go index 10dc6c12..8ded9a0e 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -59,7 +59,6 @@ func (d MySQLDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) return "mediumblob" } } - log.Println(val.Name()) switch val.Name() { case "NullInt64": return "bigint" From e5735580371d3e43d8d7904f2173eac959032a4d Mon Sep 17 00:00:00 2001 From: Sol Cates Date: Fri, 17 Mar 2017 13:48:20 -0700 Subject: [PATCH 3/8] oops... forgot to clear the imports --- dialect_mysql.go | 1 - 1 file changed, 1 deletion(-) diff --git a/dialect_mysql.go b/dialect_mysql.go index 8ded9a0e..1a3fda86 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -15,7 +15,6 @@ import ( "fmt" "reflect" "strings" - "log" ) // Implementation of Dialect for MySQL databases. From 9c91cbd4664cfd4fbbefc2a2b569763e355c0bcd Mon Sep 17 00:00:00 2001 From: Sol Cates Date: Fri, 17 Mar 2017 13:53:35 -0700 Subject: [PATCH 4/8] Added support for all DB backends for DateTime to 'datetime' --- dialect_mysql.go | 6 ++---- dialect_oracle.go | 2 +- dialect_postgres.go | 2 +- dialect_sqlite.go | 2 ++ dialect_sqlserver.go | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/dialect_mysql.go b/dialect_mysql.go index 1a3fda86..f9bbadc5 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -19,7 +19,6 @@ import ( // Implementation of Dialect for MySQL databases. type MySQLDialect struct { - // Engine is the storage engine to use "InnoDB" vs "MyISAM" for example Engine string @@ -65,10 +64,9 @@ func (d MySQLDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) return "double" case "NullBool": return "tinyint" - case "Time": - return "datetime" - case "DateTime": + case "Time", "DateTime": return "datetime" + } if maxsize < 1 { diff --git a/dialect_oracle.go b/dialect_oracle.go index c381380f..3b1fc524 100644 --- a/dialect_oracle.go +++ b/dialect_oracle.go @@ -59,7 +59,7 @@ func (d OracleDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) return "double precision" case "NullBool": return "boolean" - case "NullTime", "Time": + case "NullTime", "Time", "DateTime": return "timestamp with time zone" } diff --git a/dialect_postgres.go b/dialect_postgres.go index 6602b2f9..600d9a55 100644 --- a/dialect_postgres.go +++ b/dialect_postgres.go @@ -56,7 +56,7 @@ func (d PostgresDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr boo return "double precision" case "NullBool": return "boolean" - case "Time", "NullTime": + case "Time", "NullTime","DateTime": return "timestamp with time zone" } diff --git a/dialect_sqlite.go b/dialect_sqlite.go index 7d9b2975..ad6960c6 100644 --- a/dialect_sqlite.go +++ b/dialect_sqlite.go @@ -47,6 +47,8 @@ func (d SqliteDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) return "integer" case "Time": return "datetime" + case "DateTime": + return "datetime" } if maxsize < 1 { diff --git a/dialect_sqlserver.go b/dialect_sqlserver.go index 8808af59..86782d04 100644 --- a/dialect_sqlserver.go +++ b/dialect_sqlserver.go @@ -66,7 +66,7 @@ func (d SqlServerDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bo return "float(53)" case "NullBool": return "bit" - case "NullTime", "Time": + case "NullTime", "Time","DateTime": if d.Version == "2005" { return "datetime" } From 2b6e371ac66f1302bfca933d32b5e65fea4cd1e5 Mon Sep 17 00:00:00 2001 From: Sol Cates Date: Fri, 17 Mar 2017 13:54:36 -0700 Subject: [PATCH 5/8] Minor Cleanup --- dialect_sqlite.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dialect_sqlite.go b/dialect_sqlite.go index ad6960c6..29243b79 100644 --- a/dialect_sqlite.go +++ b/dialect_sqlite.go @@ -45,10 +45,9 @@ func (d SqliteDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) return "real" case "NullBool": return "integer" - case "Time": - return "datetime" - case "DateTime": + case "Time", "DateTime": return "datetime" + } if maxsize < 1 { From 3bc11c6df9e4f691b158bd63cc5f8319a09a04a9 Mon Sep 17 00:00:00 2001 From: Sol Cates Date: Fri, 17 Mar 2017 14:42:39 -0700 Subject: [PATCH 6/8] Added Date type support for mysql as well --- .gitignore | 1 + dialect_mysql.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index d18c8ca7..b16c22c8 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ _obj 6.out gorptest.bin tmp +.idea diff --git a/dialect_mysql.go b/dialect_mysql.go index f9bbadc5..e0898948 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -66,6 +66,8 @@ func (d MySQLDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) return "tinyint" case "Time", "DateTime": return "datetime" + case "Date": + return "date" } From 6b5986c9ee05fc56289c1e769288a075f9e07c61 Mon Sep 17 00:00:00 2001 From: Sol Cates Date: Fri, 17 Mar 2017 15:11:25 -0700 Subject: [PATCH 7/8] Also added unique tag to support usage to create unique fields --- db.go | 4 ++++ dialect_mysql.go | 1 + 2 files changed, 5 insertions(+) diff --git a/db.go b/db.go index a573bb8f..782a48ae 100644 --- a/db.go +++ b/db.go @@ -259,6 +259,7 @@ func (m *DbMap) readStructColumns(t reflect.Type) (cols []*ColumnMap, primaryKey var isAuto bool var isPK bool var isNotNull bool + var unique bool for _, argString := range cArguments[1:] { argString = strings.TrimSpace(argString) arg := strings.SplitN(argString, ":", 2) @@ -288,6 +289,8 @@ func (m *DbMap) readStructColumns(t reflect.Type) (cols []*ColumnMap, primaryKey isAuto = true case "notnull": isNotNull = true + case "unique": + unique = true default: panic(fmt.Sprintf("Unrecognized tag option for field %v: %v", f.Name, arg)) } @@ -333,6 +336,7 @@ func (m *DbMap) readStructColumns(t reflect.Type) (cols []*ColumnMap, primaryKey isAutoIncr: isAuto, isNotNull: isNotNull, MaxSize: maxSize, + Unique: unique, } if isPK { primaryKey = append(primaryKey, cm) diff --git a/dialect_mysql.go b/dialect_mysql.go index e0898948..1d5f297b 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -57,6 +57,7 @@ func (d MySQLDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) return "mediumblob" } } + fmt.Println(val.Name()) switch val.Name() { case "NullInt64": return "bigint" From 33578e9f5b0ac6c44ae946e79914542c6f76c686 Mon Sep 17 00:00:00 2001 From: Sol Cates Date: Fri, 17 Mar 2017 15:14:09 -0700 Subject: [PATCH 8/8] clean up --- dialect_mysql.go | 1 - 1 file changed, 1 deletion(-) diff --git a/dialect_mysql.go b/dialect_mysql.go index 1d5f297b..e0898948 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -57,7 +57,6 @@ func (d MySQLDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) return "mediumblob" } } - fmt.Println(val.Name()) switch val.Name() { case "NullInt64": return "bigint"