Skip to content

Commit

Permalink
支持sqlserver数据库
Browse files Browse the repository at this point in the history
  • Loading branch information
ixre committed Aug 10, 2022
1 parent e20b2b2 commit 6e111c5
Show file tree
Hide file tree
Showing 23 changed files with 233 additions and 235 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

特点:

- 支持mysql/mariadb和postgresql数据库
- 支持mysql/mariadb、postgresql和sqlserver数据库
- 支持Go,JAVA,Kotlin,Thrift,Protobuf,Python,Ts/Js,C#,Html等多种语言
- 支持代码模板, 提供模板函数,可自定义生成格式;安装包内置多种语言模板
- 支持根据模型文件逆向生成数据库及代码
Expand Down
9 changes: 5 additions & 4 deletions bin/tto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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())
Expand Down
9 changes: 5 additions & 4 deletions core.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -36,7 +37,7 @@ type Table struct {
// 数据库编码
Charset string
// 表
Raw *orm.Table
Raw *db.Table
// 主键
Pk string
// 主键属性
Expand Down
25 changes: 13 additions & 12 deletions ddl/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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语法
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion example_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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())
Expand Down Expand Up @@ -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)
}

Expand All @@ -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))
Expand All @@ -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,
Expand Down
22 changes: 8 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
55 changes: 28 additions & 27 deletions lang/csharp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ package lang

import (
"fmt"
"github.com/ixre/gof/db/orm"

"github.com/ixre/gof/db/db"
)

type CSharpLang struct {
Expand All @@ -31,69 +32,69 @@ 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)
}

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)
}

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)
Expand Down
24 changes: 13 additions & 11 deletions lang/dart.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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"
Expand All @@ -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 "{}"
Expand Down
Loading

0 comments on commit 6e111c5

Please sign in to comment.