-
-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
553 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module gorm.io/gen/rawsql_driver | ||
|
||
go 1.18 | ||
|
||
require ( | ||
github.com/pingcap/tidb/parser v0.0.0-20230327100244-b67c0321c05a | ||
gorm.io/gorm v1.24.6 | ||
) | ||
|
||
require ( | ||
github.com/benbjohnson/clock v1.1.0 // indirect | ||
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/jinzhu/now v1.1.5 // indirect | ||
github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63 // indirect | ||
github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7 // indirect | ||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect | ||
go.uber.org/atomic v1.7.0 // indirect | ||
go.uber.org/multierr v1.6.0 // indirect | ||
go.uber.org/zap v1.24.0 // indirect | ||
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 // indirect | ||
golang.org/x/text v0.8.0 // indirect | ||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= | ||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= | ||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= | ||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= | ||
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 h1:iwZdTE0PVqJCos1vaoKsclOGD3ADKpshg3SRtYBbwso= | ||
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548/go.mod h1:e6NPNENfs9mPDVNRekM7lKScauxd5kXTr1Mfyig6TDM= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= | ||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= | ||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= | ||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= | ||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | ||
github.com/pingcap/errors v0.11.0/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= | ||
github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63 h1:+FZIDR/D97YOPik4N4lPDaUcLDF/EQPogxtlHB2ZZRM= | ||
github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63/go.mod h1:X2r9ueLEUZgtx2cIogM0v4Zj5uvvzhuuiu7Pn8HzMPg= | ||
github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7 h1:k2BbABz9+TNpYRwsCCFS8pEEnFVOdbgEjL/kTlLuzZQ= | ||
github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7/go.mod h1:8AanEdAHATuRurdGxZXBz0At+9avep+ub7U1AGYLIMM= | ||
github.com/pingcap/tidb/parser v0.0.0-20230327100244-b67c0321c05a h1:GsiwtTVFsN+1bfeUaStwOoBzTC4tKpETiWmhxT55jAc= | ||
github.com/pingcap/tidb/parser v0.0.0-20230327100244-b67c0321c05a/go.mod h1:IxXRBZ14Of1KkR3NXEwsoKrM8JbkOIHJHpwS/Ad8vPY= | ||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= | ||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk= | ||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= | ||
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= | ||
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= | ||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= | ||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= | ||
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= | ||
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= | ||
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= | ||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= | ||
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= | ||
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= | ||
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 h1:TfdoLivD44QwvssI9Sv1xwa5DcL5XQr4au4sZ2F2NV4= | ||
golang.org/x/exp v0.0.0-20220428152302-39d4317da171/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= | ||
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= | ||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= | ||
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= | ||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= | ||
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8= | ||
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= | ||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gorm.io/gorm v1.24.6 h1:wy98aq9oFEetsc4CAbKD2SoBCdMzsbSIvSUUFJuHi5s= | ||
gorm.io/gorm v1.24.6/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package rawsql_driver | ||
|
||
import ( | ||
"strings" | ||
|
||
"gorm.io/gorm" | ||
"gorm.io/gorm/migrator" | ||
) | ||
|
||
type Migrator struct { | ||
migrator.Migrator | ||
Dialector | ||
} | ||
|
||
func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) { | ||
columnTypes := make([]gorm.ColumnType, 0) | ||
err := m.RunWithValue(value, func(stmt *gorm.Statement) error { | ||
var ( | ||
_, tableName = m.CurrentSchema(stmt, stmt.Table) | ||
) | ||
table, ok := m.tables[tableName] | ||
if ok && table != nil { | ||
columnTypes = table.ColumnTypes | ||
} | ||
return nil | ||
}) | ||
return columnTypes, err | ||
} | ||
|
||
func (m Migrator) GetIndexes(value interface{}) ([]gorm.Index, error) { | ||
indexes := make([]gorm.Index, 0) | ||
err := m.RunWithValue(value, func(stmt *gorm.Statement) error { | ||
var ( | ||
_, tableName = m.CurrentSchema(stmt, stmt.Table) | ||
) | ||
table, ok := m.tables[tableName] | ||
if ok && table != nil { | ||
indexes = table.Indexes | ||
} | ||
return nil | ||
}) | ||
return indexes, err | ||
} | ||
|
||
func (m Migrator) GetTables() (tableList []string, err error) { | ||
tableList = make([]string, 0, len(m.tables)) | ||
for tb, _ := range m.tables { | ||
tableList = append(tableList, tb) | ||
} | ||
return tableList, nil | ||
} | ||
func (m Migrator) CurrentSchema(stmt *gorm.Statement, table string) (string, string) { | ||
if tables := strings.Split(table, `.`); len(tables) == 2 { | ||
return tables[0], tables[1] | ||
} | ||
m.DB = m.DB.Table(table) | ||
return "", table | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package rawsql_driver | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
_ "github.com/pingcap/tidb/parser/test_driver" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/clause" | ||
"gorm.io/gorm/migrator" | ||
"gorm.io/gorm/schema" | ||
) | ||
|
||
type Config struct { | ||
DriverName string //mysql | ||
FilePath []string //create table sql file or file path | ||
SQL []string //create table sql content | ||
Parser | ||
} | ||
|
||
type Dialector struct { | ||
*Config | ||
tables map[string]*Table | ||
} | ||
|
||
func New(config Config) gorm.Dialector { | ||
return &Dialector{Config: &config, tables: map[string]*Table{}} | ||
} | ||
|
||
func (dialector Dialector) Name() string { | ||
return dialector.DriverName | ||
} | ||
|
||
func (dialector Dialector) Initialize(db *gorm.DB) error { | ||
if dialector.DriverName == "" { | ||
dialector.DriverName = "mysql" | ||
} | ||
if dialector.SQL == nil { | ||
dialector.SQL = make([]string, 0) | ||
} | ||
if dialector.tables == nil { | ||
dialector.tables = make(map[string]*Table) | ||
} | ||
if dialector.Parser == nil { | ||
dialector.Parser = &defaultParser{} | ||
} | ||
if err := dialector.fileTOSQL(); err != nil { | ||
return err | ||
} | ||
|
||
if err := dialector.sqlTOTable(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (dialector Dialector) sqlTOTable() error { | ||
for _, sql := range dialector.SQL { | ||
tables, err := dialector.Parser.Tables(sql) | ||
if err != nil { | ||
return err | ||
} | ||
for _, table := range tables { | ||
dialector.tables[table.Name] = table | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (dialector Dialector) fileTOSQL() error { | ||
for _, f := range dialector.FilePath { | ||
if f == "" { | ||
continue | ||
} | ||
v, err := os.Stat(f) | ||
if err != nil { | ||
return err | ||
} | ||
if v.IsDir() { | ||
err = dialector.readFiles(f) | ||
} else { | ||
err = dialector.readFile(f) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (dialector Dialector) readFiles(folder string) (err error) { | ||
files, _ := ioutil.ReadDir(folder) | ||
for _, file := range files { | ||
fn := filepath.Join(folder, file.Name()) | ||
if file.IsDir() { | ||
err = dialector.readFiles(fn) | ||
} else { | ||
err = dialector.readFile(fn) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (dialector Dialector) readFile(fileName string) error { | ||
content, err := ioutil.ReadFile(fileName) | ||
if err != nil { | ||
return err | ||
} | ||
dialector.SQL = append(dialector.SQL, string(content)) | ||
return nil | ||
} | ||
|
||
func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator { | ||
return Migrator{ | ||
Migrator: migrator.Migrator{ | ||
Config: migrator.Config{ | ||
DB: db, | ||
Dialector: dialector, | ||
}, | ||
}, | ||
Dialector: dialector, | ||
} | ||
} | ||
|
||
func (dialector Dialector) DataTypeOf(field *schema.Field) string { | ||
return "" | ||
} | ||
func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression { | ||
return clause.Expr{SQL: "DEFAULT"} | ||
} | ||
func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {} | ||
func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {} | ||
func (dialector Dialector) Explain(sql string, vars ...interface{}) string { | ||
return "" | ||
} |
Oops, something went wrong.