diff --git a/README.md b/README.md index 344bede..7ac08ad 100755 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ 特点: -- 支持mysql/mariadb和postgresql数据库 +- 支持mysql/mariadb、postgresql和sqlserver数据库 - 支持Go,JAVA,Kotlin,Thrift,Protobuf,Python,Ts/Js,C#,Html等多种语言 - 支持代码模板, 提供模板函数,可自定义生成格式;安装包内置多种语言模板 - 支持根据模型文件逆向生成数据库及代码 diff --git a/bin/tto.go b/bin/tto.go index 708fafb..6fc67c2 100755 --- a/bin/tto.go +++ b/bin/tto.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" "github.com/ixre/gof/db" + "github.com/ixre/gof/db/dialect" "github.com/ixre/gof/db/orm" "github.com/ixre/gof/shell" "github.com/ixre/tto" @@ -144,7 +145,7 @@ func generate() { } // 初始化生成器 driver := re.GetString("database.driver") - dialect, dbDriver := tto.GetDialect(driver) + dbDriver, dialect := dialect.GetDialect(driver) dg := tto.DBCodeGenerator(dbDriver, opt) dg.Package(pkgName) @@ -168,9 +169,9 @@ func generate() { } userMeta := re.GetBoolean("code.meta_settings") tables, err = dg.Parses(list, userMeta) - - }else{ - tables,err = tto.ReadModels(modelPath) + + } else { + tables, err = tto.ReadModels(modelPath) } if err != nil { println("[ tto][ error]:", err.Error()) diff --git a/core.go b/core.go index 7ba1ff5..2ac5ea9 100755 --- a/core.go +++ b/core.go @@ -1,13 +1,14 @@ package tto import ( - "github.com/ixre/gof/db/orm" - "github.com/ixre/tto/config" "strings" + + "github.com/ixre/gof/db/db" + "github.com/ixre/tto/config" ) // BuildVersion 版本号 -const BuildVersion = "0.5.0" +const BuildVersion = "0.5.1" // ReleaseCodeHome 代码页 const ReleaseCodeHome = "https://github.com/ixre/tto" @@ -36,7 +37,7 @@ type Table struct { // 数据库编码 Charset string // 表 - Raw *orm.Table + Raw *db.Table // 主键 Pk string // 主键属性 diff --git a/ddl/clickhouse.go b/ddl/clickhouse.go index 5113a45..2c5251a 100644 --- a/ddl/clickhouse.go +++ b/ddl/clickhouse.go @@ -3,9 +3,10 @@ package ddl import ( "bytes" "fmt" - "github.com/ixre/gof/db/orm" - "github.com/ixre/tto" "strings" + + "github.com/ixre/gof/db/db" + "github.com/ixre/tto" ) // GenerateClickHouseMergeTreeTableDDL 使用MergeTree引擎生成Clickhouse表的DDL语法 @@ -59,25 +60,25 @@ func GenerateClickHouseMergeTreeTableDDL(table *tto.Table) string { func getClickhouseColumnType(typeId int) string { switch typeId { - case orm.TypeBoolean: + case db.TypeBoolean: return "Int8" - case orm.TypeInt64: + case db.TypeInt64: return "Int64" - case orm.TypeFloat32: + case db.TypeFloat32: return "Float32" - case orm.TypeFloat64: + case db.TypeFloat64: return "Float64" - case orm.TypeInt16: + case db.TypeInt16: return "Int16" - case orm.TypeInt32: + case db.TypeInt32: return "Int32" - case orm.TypeString: + case db.TypeString: return "String" - case orm.TypeDecimal: + case db.TypeDecimal: return "Decimal64" - case orm.TypeDateTime: + case db.TypeDateTime: return "Date" - case orm.TypeBytes: + case db.TypeBytes: return "String" } return fmt.Sprintf("Unknown type id:%d", typeId) diff --git a/example_run.sh b/example_run.sh index c75d073..2ea58ff 100755 --- a/example_run.sh +++ b/example_run.sh @@ -12,7 +12,7 @@ if [[ $(whereis tto) = 'tto:' ]]; then echo '未安装tto客户端,请运行安装命令: curl -L https://raw.githubusercontent.com/ixre/tto/master/install | sh' fi -CONF_DIR=$(dirname "$0")/generator +CONF_DIR=$(dirname "$0") tto -conf "$CONF_DIR"/tto.conf -t "$CONF_DIR"/templates -o output -excludes tmp_ -clean # Replace generator description part of code file diff --git a/generate_test.go b/generate_test.go index 446d98f..11ab11a 100755 --- a/generate_test.go +++ b/generate_test.go @@ -14,7 +14,7 @@ import ( var ( driver = "mysql" dbName = "" - dbPrefix = "mm_" + dbPrefix = "t_B" connString = "root:@tcp(127.0.0.1:3306)/baozhang?charset=utf8" genDir = "./generated_code/" tplDir = "./templates/java/spring/kotlin" @@ -25,6 +25,9 @@ func TestGenAll(t *testing.T) { driver = "postgresql" connString = "postgres://postgres:123456@127.0.0.1:5432/go2o?sslmode=disable" + // driver = "sqlserver" + // connString = "sqlserver://sfDBUser:Jbmeon@001@192.168.1.1:1433?database=DCF19_ERP_TEST_B&encrypt=disable" + // 初始化生成器 conn, _ := db.NewConnector(driver, connString, nil, false) ds, _ := orm.NewDialectSession(driver, conn.Raw()) @@ -57,7 +60,7 @@ func TestGenAll(t *testing.T) { //dg.GenerateGoRepoCodes(tables, genDir) dg.WalkGenerateCodes(tables) //格式化代码 - shell.Run("gofmt -w " + genDir, false) + shell.Run("gofmt -w "+genDir, false) t.Log("生成成功, 输出目录", genDir) } @@ -71,7 +74,6 @@ func TestGenAll(t *testing.T) { // return nil // } - func TestReadTables(t *testing.T) { txt, _ := ioutil.ReadFile("./templates/table.tb") tables, _ := ReadTables(string(txt)) @@ -81,7 +83,7 @@ func TestReadTables(t *testing.T) { func TestGenerateByReadedTables(t *testing.T) { //txt, _ := ioutil.ReadFile("./templates/table.tb") //tables, _ := ReadTables(string(txt), "user") - tables,_ := ReadModels("./templates") + tables, _ := ReadModels("./templates") // 生成自定义代码 opt := &Options{ TplDir: tplDir, diff --git a/go.mod b/go.mod index cf50917..a6a11bc 100755 --- a/go.mod +++ b/go.mod @@ -1,26 +1,20 @@ module github.com/ixre/tto -go 1.17 +go 1.18 require ( github.com/go-sql-driver/mysql v1.6.0 - github.com/ixre/gof v1.12.4 - github.com/labstack/echo/v4 v4.7.2 + github.com/ixre/gof v1.13.0 github.com/pelletier/go-toml v1.9.5 ) require ( - github.com/gomodule/redigo v1.8.8 // indirect - github.com/labstack/gommon v0.3.1 // indirect - github.com/lib/pq v1.10.5 // indirect - github.com/mattn/go-colorable v0.1.11 // indirect - github.com/mattn/go-isatty v0.0.14 // indirect - github.com/valyala/bytebufferpool v1.0.0 // indirect - github.com/valyala/fasttemplate v1.2.1 // indirect - golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect - golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect - golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect - golang.org/x/text v0.3.7 // indirect + github.com/denisenkom/go-mssqldb v0.12.2 // indirect + github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect + github.com/golang-sql/sqlexp v0.1.0 // indirect + github.com/gomodule/redigo v1.8.9 // indirect + github.com/lib/pq v1.10.6 // indirect + golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect ) //replace github.com/ixre/gof => ../gof diff --git a/lang/csharp.go b/lang/csharp.go index d9b41d0..7b5aa56 100644 --- a/lang/csharp.go +++ b/lang/csharp.go @@ -12,7 +12,8 @@ package lang import ( "fmt" - "github.com/ixre/gof/db/orm" + + "github.com/ixre/gof/db/db" ) type CSharpLang struct { @@ -31,23 +32,23 @@ func (j CSharpLang) PkgPath(pkg string) string { func (j CSharpLang) ParseType(typeId int) string { switch typeId { - case orm.TypeBoolean: + case db.TypeBoolean: return "bool" - case orm.TypeInt64: + case db.TypeInt64: return "long" - case orm.TypeFloat32: + case db.TypeFloat32: return "float" - case orm.TypeFloat64: + case db.TypeFloat64: return "double" - case orm.TypeInt16, orm.TypeInt32: + case db.TypeInt16, db.TypeInt32: return "int" - case orm.TypeString: + case db.TypeString: return "string" - case orm.TypeDecimal: + case db.TypeDecimal: return "Decimal" - case orm.TypeDateTime: + case db.TypeDateTime: return "DateTime" - case orm.TypeBytes: + case db.TypeBytes: return "byte[]" } return fmt.Sprintf("Unknown type id:%d", typeId) @@ -55,21 +56,21 @@ func (j CSharpLang) ParseType(typeId int) string { func (j CSharpLang) DefaultValue(typeId int) string { switch typeId { - case orm.TypeBoolean: + case db.TypeBoolean: return "false" - case orm.TypeInt64: + case db.TypeInt64: return "0L" - case orm.TypeFloat32, orm.TypeFloat64: + case db.TypeFloat32, db.TypeFloat64: return "0F" - case orm.TypeInt16, orm.TypeInt32: + case db.TypeInt16, db.TypeInt32: return "0" - case orm.TypeDecimal: + case db.TypeDecimal: return "new Decimal(0.0)" - case orm.TypeString: + case db.TypeString: return "\"\"" - case orm.TypeDateTime: + case db.TypeDateTime: return "DateTime.Now" - case orm.TypeBytes: + case db.TypeBytes: return "new Byte[0]" } return fmt.Sprintf("Unknown type id:%d", typeId) @@ -77,23 +78,23 @@ func (j CSharpLang) DefaultValue(typeId int) string { func (j CSharpLang) ParsePkType(typeId int) string { switch typeId { - case orm.TypeBoolean: + case db.TypeBoolean: return "bool" - case orm.TypeInt64: + case db.TypeInt64: return "long" - case orm.TypeFloat32: + case db.TypeFloat32: return "float" - case orm.TypeFloat64: + case db.TypeFloat64: return "double" - case orm.TypeInt16, orm.TypeInt32: + case db.TypeInt16, db.TypeInt32: return "int" - case orm.TypeString: + case db.TypeString: return "string" - case orm.TypeDecimal: + case db.TypeDecimal: return "Decimal" - case orm.TypeDateTime: + case db.TypeDateTime: return "DateTime" - case orm.TypeBytes: + case db.TypeBytes: return "byte[]" } return fmt.Sprintf("Unknown type id:%d", typeId) diff --git a/lang/dart.go b/lang/dart.go index 5b10d51..4618606 100644 --- a/lang/dart.go +++ b/lang/dart.go @@ -1,6 +1,8 @@ package lang -import "github.com/ixre/gof/db/orm" +import ( + "github.com/ixre/gof/db/db" +) /** * Copyright (C) 2007-2020 56X.NET,All rights reserved. @@ -19,15 +21,15 @@ type dart struct { func (d dart) ParseType(typeId int) string { switch typeId { - case orm.TypeString: + case db.TypeString: return "String" - case orm.TypeBoolean: + case db.TypeBoolean: return "bool" - case orm.TypeInt16,orm.TypeInt32,orm.TypeInt64: + case db.TypeInt16, db.TypeInt32, db.TypeInt64: return "int" - case orm.TypeFloat32,orm.TypeFloat64, orm.TypeDecimal: + case db.TypeFloat32, db.TypeFloat64, db.TypeDecimal: return "num" - case orm.TypeDateTime: + case db.TypeDateTime: return "DateTime" } return "dynamic" @@ -39,15 +41,15 @@ func (d dart) SqlMapType(typeId int, len int) string { func (d dart) DefaultValue(typeId int) string { switch typeId { - case orm.TypeString: + case db.TypeString: return "\"\"" - case orm.TypeBoolean: + case db.TypeBoolean: return "false" - case orm.TypeInt16, orm.TypeInt32,orm.TypeInt64: + case db.TypeInt16, db.TypeInt32, db.TypeInt64: return "0" - case orm.TypeFloat32, orm.TypeFloat64, orm.TypeDecimal: + case db.TypeFloat32, db.TypeFloat64, db.TypeDecimal: return "0.0" - case orm.TypeDateTime: + case db.TypeDateTime: return "DateTime.now()" } return "{}" diff --git a/lang/go.go b/lang/go.go index 8aabe6d..af0be05 100644 --- a/lang/go.go +++ b/lang/go.go @@ -1,8 +1,9 @@ package lang import ( - "github.com/ixre/gof/db/orm" "regexp" + + "github.com/ixre/gof/db/db" ) /** @@ -44,23 +45,23 @@ func (g GoLang) DefaultValue(typeId int) string { func GoTypes(typeId int) string { switch typeId { - case orm.TypeString: + case db.TypeString: return "string" - case orm.TypeBoolean: + case db.TypeBoolean: return "bool" - case orm.TypeInt16: + case db.TypeInt16: return "int16" - case orm.TypeInt32: + case db.TypeInt32: return "int" - case orm.TypeInt64: + case db.TypeInt64: return "int64" - case orm.TypeFloat32: + case db.TypeFloat32: return "float32" - case orm.TypeFloat64, orm.TypeDecimal: + case db.TypeFloat64, db.TypeDecimal: return "float64" - case orm.TypeDateTime: + case db.TypeDateTime: return "time.Time" - case orm.TypeBytes: + case db.TypeBytes: return "[]byte" } return "interface{}" @@ -68,16 +69,14 @@ func GoTypes(typeId int) string { func GoValues(typeId int) string { switch typeId { - case orm.TypeString: + case db.TypeString: return "\"\"" - case orm.TypeBoolean: + case db.TypeBoolean: return "false" - case orm.TypeInt16, orm.TypeInt32, orm.TypeInt64: + case db.TypeInt16, db.TypeInt32, db.TypeInt64: return "0" - case orm.TypeFloat32, orm.TypeFloat64: + case db.TypeFloat32, db.TypeFloat64: return "0.0" } return "" } - - diff --git a/lang/java.go b/lang/java.go index c995d62..b295f59 100644 --- a/lang/java.go +++ b/lang/java.go @@ -18,7 +18,8 @@ find output/java -name "*.java" | xargs sed -i 's/ float / Float /g' import ( "fmt" - "github.com/ixre/gof/db/orm" + + "github.com/ixre/gof/db/db" ) type JavaLang struct { @@ -51,23 +52,23 @@ var _ Lang = new(JavaLang) func javaTypes(typeId int) string { switch typeId { - case orm.TypeBoolean: + case db.TypeBoolean: return "boolean" - case orm.TypeInt64: + case db.TypeInt64: return "long" - case orm.TypeFloat32: + case db.TypeFloat32: return "float" - case orm.TypeFloat64: + case db.TypeFloat64: return "double" - case orm.TypeInt16, orm.TypeInt32: + case db.TypeInt16, db.TypeInt32: return "int" - case orm.TypeString: + case db.TypeString: return "String" - case orm.TypeDecimal: + case db.TypeDecimal: return "BigDecimal" - case orm.TypeDateTime: + case db.TypeDateTime: return "Date" - case orm.TypeBytes: + case db.TypeBytes: return "Byte[]" } return fmt.Sprintf("Unknown type id:%d", typeId) @@ -75,23 +76,23 @@ func javaTypes(typeId int) string { func javaPkTypes(typeId int) string { switch typeId { - case orm.TypeBoolean: + case db.TypeBoolean: return "Boolean" - case orm.TypeInt64: + case db.TypeInt64: return "Long" - case orm.TypeFloat32: + case db.TypeFloat32: return "Float" - case orm.TypeFloat64: + case db.TypeFloat64: return "Double" - case orm.TypeInt16, orm.TypeInt32: + case db.TypeInt16, db.TypeInt32: return "Integer" - case orm.TypeString: + case db.TypeString: return "String" - case orm.TypeDecimal: + case db.TypeDecimal: return "BigDecimal" - case orm.TypeDateTime: + case db.TypeDateTime: return "Date" - case orm.TypeBytes: + case db.TypeBytes: return "Byte[]" } return fmt.Sprintf("Unknown type id:%d", typeId) @@ -99,21 +100,21 @@ func javaPkTypes(typeId int) string { func javaValues(typeId int) string { switch typeId { - case orm.TypeBoolean: + case db.TypeBoolean: return "false" - case orm.TypeInt64: + case db.TypeInt64: return "0L" - case orm.TypeFloat32, orm.TypeFloat64: + case db.TypeFloat32, db.TypeFloat64: return "0F" - case orm.TypeInt16, orm.TypeInt32: + case db.TypeInt16, db.TypeInt32: return "0" - case orm.TypeDecimal: + case db.TypeDecimal: return "new BigDecimal(0.0)" - case orm.TypeString: + case db.TypeString: return "\"\"" - case orm.TypeDateTime: + case db.TypeDateTime: return "new Date()" - case orm.TypeBytes: + case db.TypeBytes: return "new Byte[0]" } return fmt.Sprintf("Unknown type id:%d", typeId) diff --git a/lang/kotlin.go b/lang/kotlin.go index d9e5270..1130e7b 100644 --- a/lang/kotlin.go +++ b/lang/kotlin.go @@ -2,7 +2,8 @@ package lang import ( "fmt" - "github.com/ixre/gof/db/orm" + + "github.com/ixre/gof/db/db" ) /** @@ -42,23 +43,23 @@ var _ Lang = new(KotlinLang) func KotlinTypes(typeId int) string { switch typeId { - case orm.TypeBoolean: + case db.TypeBoolean: return "Boolean" - case orm.TypeInt64: + case db.TypeInt64: return "Long" - case orm.TypeFloat32: + case db.TypeFloat32: return "Float" - case orm.TypeFloat64: + case db.TypeFloat64: return "Double" - case orm.TypeInt16, orm.TypeInt32: + case db.TypeInt16, db.TypeInt32: return "Int" - case orm.TypeString: + case db.TypeString: return "String" - case orm.TypeDecimal: + case db.TypeDecimal: return "BigDecimal" - case orm.TypeDateTime: + case db.TypeDateTime: return "Date" - case orm.TypeBytes: + case db.TypeBytes: return "ByteArray" } return fmt.Sprintf("Unknown type id:%d", typeId) @@ -66,19 +67,19 @@ func KotlinTypes(typeId int) string { func KotlinValues(typeId int) string { switch typeId { - case orm.TypeBoolean: + case db.TypeBoolean: return "false" - case orm.TypeInt64: + case db.TypeInt64: return "0L" - case orm.TypeFloat32, orm.TypeFloat64: + case db.TypeFloat32, db.TypeFloat64: return "0F" - case orm.TypeInt16, orm.TypeInt32: + case db.TypeInt16, db.TypeInt32: return "0" - case orm.TypeDecimal: + case db.TypeDecimal: return "BigDecimal(0.0)" - case orm.TypeString: + case db.TypeString: return "\"\"" - case orm.TypeDateTime: + case db.TypeDateTime: return "Date()" } return fmt.Sprintf("Unknown type id:%d", typeId) diff --git a/lang/lang.go b/lang/lang.go index 570e59a..72d835e 100644 --- a/lang/lang.go +++ b/lang/lang.go @@ -1,9 +1,10 @@ package lang import ( - "github.com/ixre/gof/db/orm" "strconv" "strings" + + "github.com/ixre/gof/db/db" ) /** @@ -87,13 +88,13 @@ func (c CommonLang) DefaultValue(typeId int) string { func CommonValues(typeId int) string { switch typeId { - case orm.TypeBoolean: + case db.TypeBoolean: return "false" - case orm.TypeInt64, orm.TypeInt16, orm.TypeInt32: + case db.TypeInt64, db.TypeInt16, db.TypeInt32: return "0" - case orm.TypeFloat32, orm.TypeFloat64, orm.TypeDecimal: + case db.TypeFloat32, db.TypeFloat64, db.TypeDecimal: return "0.0" - case orm.TypeString: + case db.TypeString: return "\"\"" } return "null" diff --git a/lang/protobuf.go b/lang/protobuf.go index 00eeb27..bba1e16 100644 --- a/lang/protobuf.go +++ b/lang/protobuf.go @@ -2,7 +2,8 @@ package lang import ( "fmt" - "github.com/ixre/gof/db/orm" + + "github.com/ixre/gof/db/db" ) /** @@ -41,23 +42,23 @@ var _ Lang = new(Protobuf) func ProtobufTypes(typeId int) string { switch typeId { - case orm.TypeBoolean: + case db.TypeBoolean: return "bool" - case orm.TypeFloat32: + case db.TypeFloat32: return "double" - case orm.TypeFloat64: + case db.TypeFloat64: return "double" - case orm.TypeInt16: + case db.TypeInt16: return "int32" - case orm.TypeInt32: + case db.TypeInt32: return "int32" - case orm.TypeInt64: + case db.TypeInt64: return "int64" - case orm.TypeString: + case db.TypeString: return "string" - case orm.TypeDecimal: + case db.TypeDecimal: return "decimal" - case orm.TypeDateTime: + case db.TypeDateTime: return "string" } return fmt.Sprintf("Unknown type id:%d", typeId) diff --git a/lang/python.go b/lang/python.go index a310426..dafbc08 100644 --- a/lang/python.go +++ b/lang/python.go @@ -2,7 +2,8 @@ package lang import ( "fmt" - "github.com/ixre/gof/db/orm" + + "github.com/ixre/gof/db/db" ) /** @@ -21,7 +22,7 @@ type PythonLang struct { // python SQL ORM 类型与python类型不一样,需单独处理 func (p PythonLang) SqlMapType(typeId int, len int) string { switch typeId { - case orm.TypeString: + case db.TypeString: if len > 0 { if len > 2048 { return "Text" @@ -29,19 +30,19 @@ func (p PythonLang) SqlMapType(typeId int, len int) string { return fmt.Sprintf("String(%d)", len) } return "String" - case orm.TypeBoolean: + case db.TypeBoolean: return "Boolean" - case orm.TypeInt16: + case db.TypeInt16: return "SmallInteger" - case orm.TypeInt32: + case db.TypeInt32: return "Integer" - case orm.TypeInt64: + case db.TypeInt64: return "BigInteger" - case orm.TypeFloat32: + case db.TypeFloat32: return "Float" - case orm.TypeFloat64: + case db.TypeFloat64: return "Float" - case orm.TypeDecimal: + case db.TypeDecimal: return "Decimal" } return "String" @@ -67,17 +68,17 @@ var _ Lang = new(PythonLang) func PyTypes(typeId int) string { switch typeId { - case orm.TypeBoolean: + case db.TypeBoolean: return "bool" - case orm.TypeFloat32, orm.TypeFloat64, orm.TypeDecimal: + case db.TypeFloat32, db.TypeFloat64, db.TypeDecimal: return "float" - case orm.TypeInt16, orm.TypeInt32, orm.TypeInt64: + case db.TypeInt16, db.TypeInt32, db.TypeInt64: return "int" - case orm.TypeString: + case db.TypeString: return "str" - case orm.TypeDateTime: + case db.TypeDateTime: return "time" - case orm.TypeBytes: + case db.TypeBytes: return "byte[]" } return "any" @@ -85,13 +86,13 @@ func PyTypes(typeId int) string { func PythonValues(typeId int) string { switch typeId { - case orm.TypeBoolean: + case db.TypeBoolean: return "False" - case orm.TypeInt64, orm.TypeInt16, orm.TypeInt32: + case db.TypeInt64, db.TypeInt16, db.TypeInt32: return "0" - case orm.TypeFloat32, orm.TypeFloat64, orm.TypeDecimal: + case db.TypeFloat32, db.TypeFloat64, db.TypeDecimal: return "0.0" - case orm.TypeString: + case db.TypeString: return "\"\"" } return "None" diff --git a/lang/thrift.go b/lang/thrift.go index 93b4cac..b9efb8e 100644 --- a/lang/thrift.go +++ b/lang/thrift.go @@ -1,8 +1,9 @@ package lang import ( - "github.com/ixre/gof/db/orm" "strconv" + + "github.com/ixre/gof/db/db" ) type Thrift struct { @@ -39,23 +40,23 @@ var _ Lang = new(Thrift) func ThriftTypes(typeId int) string { switch typeId { - case orm.TypeString: + case db.TypeString: return "string" - case orm.TypeBoolean: + case db.TypeBoolean: return "bool" - case orm.TypeInt16: + case db.TypeInt16: return "i16" - case orm.TypeInt32: + case db.TypeInt32: return "i32" - case orm.TypeInt64: + case db.TypeInt64: return "i64" - case orm.TypeFloat32: + case db.TypeFloat32: return "f32" - case orm.TypeFloat64: + case db.TypeFloat64: return "f64" - case orm.TypeDecimal: + case db.TypeDecimal: return "f64" - case orm.TypeDateTime: + case db.TypeDateTime: return "string" } return strconv.Itoa(typeId) diff --git a/lang/typescript.go b/lang/typescript.go index 788344a..2254fdd 100644 --- a/lang/typescript.go +++ b/lang/typescript.go @@ -1,8 +1,6 @@ package lang -import ( - "github.com/ixre/gof/db/orm" -) +import "github.com/ixre/gof/db/db" /** * Copyright (C) 2007-2020 56X.NET,All rights reserved. @@ -37,13 +35,13 @@ func (t Typescript) DefaultValue(typeId int) string { func tsValues(typeId int) string { switch typeId { - case orm.TypeString: + case db.TypeString: return "''" - case orm.TypeBoolean: + case db.TypeBoolean: return "false" - case orm.TypeInt16, orm.TypeInt32, orm.TypeInt64: + case db.TypeInt16, db.TypeInt32, db.TypeInt64: return "0" - case orm.TypeFloat32, orm.TypeFloat64: + case db.TypeFloat32, db.TypeFloat64: return "0.0" } return "" @@ -53,21 +51,21 @@ var _ Lang = new(Typescript) func TsTypes(typeId int) string { switch typeId { - case orm.TypeBoolean: + case db.TypeBoolean: return "boolean" - case orm.TypeInt64: + case db.TypeInt64: return "number" - case orm.TypeFloat32: + case db.TypeFloat32: return "number" - case orm.TypeFloat64: + case db.TypeFloat64: return "number" - case orm.TypeInt16, orm.TypeInt32, orm.TypeDecimal: + case db.TypeInt16, db.TypeInt32, db.TypeDecimal: return "number" - case orm.TypeString: + case db.TypeString: return "string" - case orm.TypeDateTime: + case db.TypeDateTime: return "Date" - case orm.TypeBytes: + case db.TypeBytes: return "any" } return "any" diff --git a/parser.go b/parser.go index a7aee6a..67b36d3 100755 --- a/parser.go +++ b/parser.go @@ -3,17 +3,18 @@ package tto import ( "encoding/json" "fmt" - "github.com/ixre/gof/db/orm" - "github.com/ixre/gof/log" - "github.com/ixre/gof/util" - "github.com/ixre/tto/config" "io/ioutil" "os" "strings" + + "github.com/ixre/gof/db/db" + "github.com/ixre/gof/log" + "github.com/ixre/gof/util" + "github.com/ixre/tto/config" ) // 获取表结构,userMeta 是否使用用户的元数据 -func parseTable(ordinal int, tb *orm.Table, shortUpper bool, userMeta bool) *Table { +func parseTable(ordinal int, tb *db.Table, shortUpper bool, userMeta bool) *Table { n := &Table{ Ordinal: ordinal, Name: tb.Name, @@ -27,7 +28,7 @@ func parseTable(ordinal int, tb *orm.Table, shortUpper bool, userMeta bool) *Tab Raw: tb, Pk: "id", PkProp: "Id", - PkType: orm.TypeInt32, + PkType: db.TypeInt32, Columns: make([]*Column, len(tb.Columns)), } if len(n.Comment) == 0 { @@ -56,7 +57,7 @@ func parseTable(ordinal int, tb *orm.Table, shortUpper bool, userMeta bool) *Tab } // 兼容JAVA项目int主键 if CompactMode && c.DbType == "int(11)" { - c.Type = orm.TypeInt32 + c.Type = db.TypeInt32 } n.Columns[i] = c } diff --git a/reverse.go b/reverse.go index 43515dc..c2c60f5 100644 --- a/reverse.go +++ b/reverse.go @@ -8,7 +8,7 @@ import ( "strings" "unicode" - "github.com/ixre/gof/db/orm" + "github.com/ixre/gof/db/db" "github.com/ixre/tto/config" ) @@ -18,7 +18,7 @@ func ReadModels(path string) ([]*Table, error) { if err == nil { for _, v := range list { if strings.HasSuffix(v.Name(), ".t") { - bytes, err := ioutil.ReadFile(filepath.Join(path,v.Name())) + bytes, err := ioutil.ReadFile(filepath.Join(path, v.Name())) if err == nil { tb, _ := ReadTables(string(bytes)) if len(tb) > 0 { @@ -30,6 +30,7 @@ func ReadModels(path string) ([]*Table, error) { } return tables, err } + var tableRegex = regexp.MustCompile(`///\s*([\S]+)\s*type\s+([^\s]+)\sstruct{([^}]+)}`) var txtColRegex = regexp.MustCompile(`///\s*([\S]+)\s*([\S]+)\s+([\S]+)\s+` + "`([^`]+)`") var txtPropRegex = regexp.MustCompile(`([\S]+?)\s*:\s*"(.+?)"`) @@ -86,10 +87,10 @@ func readColumns(table *Table, txt string) { table.PkProp = col.Prop table.PkType = col.Type if prefix := props["prefix"]; len(prefix) > 0 { - if !strings.HasSuffix(prefix,"_"){ + if !strings.HasSuffix(prefix, "_") { prefix += "_" } - table.Name = KeyFormat(prefix+table.Name) + table.Name = KeyFormat(prefix + table.Name) table.Prefix = prefix[:len(prefix)-1] } } @@ -127,23 +128,23 @@ func KeyFormat(s string) string { func GetOrmTypeFromGoType(typeName string) int { switch typeName { case "string": - return orm.TypeString + return db.TypeString case "bool": - return orm.TypeBoolean + return db.TypeBoolean case "int16": - return orm.TypeInt16 + return db.TypeInt16 case "int": - return orm.TypeInt32 + return db.TypeInt32 case "int64": - return orm.TypeInt64 + return db.TypeInt64 case "float32": - return orm.TypeFloat32 + return db.TypeFloat32 case "float64": - return orm.TypeDecimal + return db.TypeDecimal case "time.Time": - return orm.TypeDateTime + return db.TypeDateTime case "[]byte": - return orm.TypeBytes + return db.TypeBytes } return 0 } diff --git a/session.go b/session.go index a8af2c5..7982227 100755 --- a/session.go +++ b/session.go @@ -12,7 +12,6 @@ import ( "bytes" "errors" "fmt" - "github.com/ixre/gof/db/orm" "io/ioutil" "log" "os" @@ -24,6 +23,8 @@ import ( "sync" "text/template" "time" + + "github.com/ixre/gof/db/db" ) const ( @@ -62,7 +63,7 @@ type ( // 遍历模板文件夹, 并生成代码, 如果为源代码目标,文件存在,则自动生成添加 .gen后缀 WalkGenerateCodes(tables []*Table) error // 转换表格,如果meta为true,则读取元数据,如果没有则自动生成元数据 - Parses(tables []*orm.Table, meta bool) (arr []*Table, err error) + Parses(tables []*db.Table, meta bool) (arr []*Table, err error) } ) @@ -153,7 +154,7 @@ func (s *sessionImpl) init() Session { } // 转换表格,如果meta为true,则读取元数据,如果没有则自动生成元数据 -func (s *sessionImpl) Parses(tables []*orm.Table, meta bool) (arr []*Table, err error) { +func (s *sessionImpl) Parses(tables []*db.Table, meta bool) (arr []*Table, err error) { n := make([]*Table, len(tables)) for i, tb := range tables { n[i] = parseTable(i, tb, s.useUpperId, meta) @@ -465,7 +466,7 @@ func (s *sessionImpl) testFilePath(path string, excludePatterns []string) bool { if strings.HasSuffix(strings.ToUpper(path), "README.MD") { return false } - if !strings.HasSuffix(path,".tpl"){ + if !strings.HasSuffix(path, ".tpl") { return false } if excludePatterns == nil { diff --git a/template.go b/template.go index 94d0fa6..af2c09a 100755 --- a/template.go +++ b/template.go @@ -7,7 +7,8 @@ import ( ) var predefineRegexp = regexp.MustCompilePOSIX(`#\!([^\!-]+):([^#]+?)`) -var lineJoinRegexp = regexp.MustCompile(`\s*\\(\s+)` ) +var lineJoinRegexp = regexp.MustCompile(`\s*\\(\s+)`) + // var lineJoinRegexp = regexp.MustCompile(`\\s*\\\\(\\s+)`) var tplCommentRegexp = regexp.MustCompile(`/\*+(\s*)#!(.+?)*/`) diff --git a/tests/generate_go_test.go b/tests/generate_go_test.go index e8f85b2..334addb 100755 --- a/tests/generate_go_test.go +++ b/tests/generate_go_test.go @@ -9,11 +9,11 @@ package tests import ( + "github.com/ixre/tto/utils" "regexp" "sync" "testing" "time" - "github.com/ixre/tto/utils" ) type testStruct struct { diff --git a/util.go b/util.go index 7e66f58..4037d8a 100755 --- a/util.go +++ b/util.go @@ -1,23 +1,12 @@ package tto import ( - "github.com/ixre/gof/db/orm" "github.com/ixre/gof/util" "os" "strings" "unicode" ) -func GetDialect(driver string) (dialect orm.Dialect, driverName string) { - switch driver { - case "mysql", "mariadb": - return &orm.MySqlDialect{}, "mysql" - case "postgres", "postgresql", "pgsql": - return &orm.PostgresqlDialect{}, "pgsql" - } - return nil, "-" -} - func prefix(str string) string { if i := strings.Index(str, "_"); i != -1 { return str[:i] @@ -57,7 +46,7 @@ func shortTitle(str string) string { // 将首字母小写 func lowerTitle(s string) string { - if len(s) == 0{ + if len(s) == 0 { return s } if rune0 := rune(s[0]); unicode.IsUpper(rune0) {