Compare commits

...

3 Commits

Author SHA1 Message Date
nettnikl 7d353f293a
Merge 9a3ea037a9 into 6bfccf8afa 2024-11-22 09:40:24 +08:00
Jinzhu 6bfccf8afa Refactor all tests script 2024-11-21 17:03:31 +08:00
Niklas J. Nett 9a3ea037a9 Add parse methods with central log instance 2024-08-06 20:04:24 +02:00
14 changed files with 58 additions and 42 deletions

View File

@ -22,7 +22,7 @@ func (db *DB) Association(column string) *Association {
association := &Association{DB: db} association := &Association{DB: db}
table := db.Statement.Table table := db.Statement.Table
if err := db.Statement.Parse(db.Statement.Model); err == nil { if err := db.Statement.ParseWithLogger(db.Statement.Model, db.Logger); err == nil {
db.Statement.Table = table db.Statement.Table = table
association.Relationship = db.Statement.Schema.Relationships.Relations[column] association.Relationship = db.Statement.Schema.Relationships.Relations[column]

View File

@ -102,7 +102,7 @@ func (p *processor) Execute(db *DB) *DB {
// parse model values // parse model values
if stmt.Model != nil { if stmt.Model != nil {
if err := stmt.Parse(stmt.Model); err != nil && (!errors.Is(err, schema.ErrUnsupportedDataType) || (stmt.Table == "" && stmt.TableExpr == nil && stmt.SQL.Len() == 0)) { if err := stmt.ParseWithLogger(stmt.Model, p.db.Logger); err != nil && (!errors.Is(err, schema.ErrUnsupportedDataType) || (stmt.Table == "" && stmt.TableExpr == nil && stmt.SQL.Len() == 0)) {
if errors.Is(err, schema.ErrUnsupportedDataType) && stmt.Table == "" && stmt.TableExpr == nil { if errors.Is(err, schema.ErrUnsupportedDataType) && stmt.Table == "" && stmt.TableExpr == nil {
db.AddError(fmt.Errorf("%w: Table not set, please set it like: db.Model(&user) or db.Table(\"users\")", err)) db.AddError(fmt.Errorf("%w: Table not set, please set it like: db.Model(&user) or db.Table(\"users\")", err))
} else { } else {

View File

@ -173,7 +173,7 @@ func preloadDB(db *gorm.DB, reflectValue reflect.Value, dest interface{}) *gorm.
return true return true
}) })
if err := tx.Statement.Parse(dest); err != nil { if err := tx.Statement.ParseWithLogger(dest, db.Logger); err != nil {
tx.AddError(err) tx.AddError(err)
return tx return tx
} }

View File

@ -86,7 +86,7 @@ func BuildQuerySQL(db *gorm.DB) {
if queryFields { if queryFields {
stmt := gorm.Statement{DB: db} stmt := gorm.Statement{DB: db}
// smaller struct // smaller struct
if err := stmt.Parse(db.Statement.Dest); err == nil && (db.QueryFields || stmt.Schema.ModelType != db.Statement.Schema.ModelType) { if err := stmt.ParseWithLogger(db.Statement.Dest, db.Logger); err == nil && (db.QueryFields || stmt.Schema.ModelType != db.Statement.Schema.ModelType) {
clauseSelect.Columns = make([]clause.Column, len(stmt.Schema.DBNames)) clauseSelect.Columns = make([]clause.Column, len(stmt.Schema.DBNames))
for idx, dbName := range stmt.Schema.DBNames { for idx, dbName := range stmt.Schema.DBNames {

View File

@ -250,7 +250,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
if !updatingValue.CanAddr() || stmt.Dest != stmt.Model { if !updatingValue.CanAddr() || stmt.Dest != stmt.Model {
// different schema // different schema
updatingStmt := &gorm.Statement{DB: stmt.DB} updatingStmt := &gorm.Statement{DB: stmt.DB}
if err := updatingStmt.Parse(stmt.Dest); err == nil { if err := updatingStmt.ParseWithLogger(stmt.Dest, stmt.Logger); err == nil {
updatingSchema = updatingStmt.Schema updatingSchema = updatingStmt.Schema
isDiffSchema = true isDiffSchema = true
} }

View File

@ -87,7 +87,7 @@ func (db *DB) Save(value interface{}) (tx *DB) {
} }
tx = tx.callbacks.Create().Execute(tx.Set("gorm:update_track_time", true)) tx = tx.callbacks.Create().Execute(tx.Set("gorm:update_track_time", true))
case reflect.Struct: case reflect.Struct:
if err := tx.Statement.Parse(value); err == nil && tx.Statement.Schema != nil { if err := tx.Statement.ParseWithLogger(value, db.Logger); err == nil && tx.Statement.Schema != nil {
for _, pf := range tx.Statement.Schema.PrimaryFields { for _, pf := range tx.Statement.Schema.PrimaryFields {
if _, isZero := pf.ValueOf(tx.Statement.Context, reflectValue); isZero { if _, isZero := pf.ValueOf(tx.Statement.Context, reflectValue); isZero {
return tx.callbacks.Create().Execute(tx) return tx.callbacks.Create().Execute(tx)
@ -466,7 +466,7 @@ func (db *DB) Count(count *int64) (tx *DB) {
dbName := tx.Statement.Selects[0] dbName := tx.Statement.Selects[0]
fields := strings.FieldsFunc(dbName, utils.IsValidDBNameChar) fields := strings.FieldsFunc(dbName, utils.IsValidDBNameChar)
if len(fields) == 1 || (len(fields) == 3 && (strings.ToUpper(fields[1]) == "AS" || fields[1] == ".")) { if len(fields) == 1 || (len(fields) == 3 && (strings.ToUpper(fields[1]) == "AS" || fields[1] == ".")) {
if tx.Statement.Parse(tx.Statement.Model) == nil { if tx.Statement.ParseWithLogger(tx.Statement.Model, db.Logger) == nil {
if f := tx.Statement.Schema.LookUpField(dbName); f != nil { if f := tx.Statement.Schema.LookUpField(dbName); f != nil {
dbName = f.DBName dbName = f.DBName
} }
@ -555,7 +555,7 @@ func (db *DB) Scan(dest interface{}) (tx *DB) {
func (db *DB) Pluck(column string, dest interface{}) (tx *DB) { func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
if tx.Statement.Model != nil { if tx.Statement.Model != nil {
if tx.Statement.Parse(tx.Statement.Model) == nil { if tx.Statement.ParseWithLogger(tx.Statement.Model, db.Logger) == nil {
if f := tx.Statement.Schema.LookUpField(column); f != nil { if f := tx.Statement.Schema.LookUpField(column); f != nil {
column = f.DBName column = f.DBName
} }
@ -575,7 +575,7 @@ func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
func (db *DB) ScanRows(rows *sql.Rows, dest interface{}) error { func (db *DB) ScanRows(rows *sql.Rows, dest interface{}) error {
tx := db.getInstance() tx := db.getInstance()
if err := tx.Statement.Parse(dest); !errors.Is(err, schema.ErrUnsupportedDataType) { if err := tx.Statement.ParseWithLogger(dest, db.Logger); !errors.Is(err, schema.ErrUnsupportedDataType) {
tx.AddError(err) tx.AddError(err)
} }
tx.Statement.Dest = dest tx.Statement.Dest = dest

2
go.mod
View File

@ -5,5 +5,5 @@ go 1.18
require ( require (
github.com/jinzhu/inflection v1.0.0 github.com/jinzhu/inflection v1.0.0
github.com/jinzhu/now v1.1.5 github.com/jinzhu/now v1.1.5
golang.org/x/text v0.14.0 golang.org/x/text v0.20.0
) )

4
go.sum
View File

@ -2,5 +2,5 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= 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 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=

View File

@ -183,7 +183,6 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
if config.Dialector != nil { if config.Dialector != nil {
err = config.Dialector.Initialize(db) err = config.Dialector.Initialize(db)
if err != nil { if err != nil {
if db, _ := db.DB(); db != nil { if db, _ := db.DB(); db != nil {
_ = db.Close() _ = db.Close()
@ -450,13 +449,13 @@ func (db *DB) SetupJoinTable(model interface{}, field string, joinTable interfac
modelSchema, joinSchema *schema.Schema modelSchema, joinSchema *schema.Schema
) )
err := stmt.Parse(model) err := stmt.ParseWithLogger(model, db.Logger)
if err != nil { if err != nil {
return err return err
} }
modelSchema = stmt.Schema modelSchema = stmt.Schema
err = stmt.Parse(joinTable) err = stmt.ParseWithLogger(joinTable, db.Logger)
if err != nil { if err != nil {
return err return err
} }

View File

@ -67,7 +67,7 @@ func (m Migrator) RunWithValue(value interface{}, fc func(*gorm.Statement) error
if table, ok := value.(string); ok { if table, ok := value.(string); ok {
stmt.Table = table stmt.Table = table
} else if err := stmt.ParseWithSpecialTableName(value, stmt.Table); err != nil { } else if err := stmt.ParseWithSpecialTableNameWithLogger(value, stmt.Table, m.DB.Logger); err != nil {
return err return err
} }
@ -348,7 +348,7 @@ func (m Migrator) RenameTable(oldName, newName interface{}) error {
oldTable = clause.Table{Name: v} oldTable = clause.Table{Name: v}
} else { } else {
stmt := &gorm.Statement{DB: m.DB} stmt := &gorm.Statement{DB: m.DB}
if err := stmt.Parse(oldName); err == nil { if err := stmt.ParseWithLogger(oldName, m.DB.Logger); err == nil {
oldTable = m.CurrentTable(stmt) oldTable = m.CurrentTable(stmt)
} else { } else {
return err return err
@ -359,7 +359,7 @@ func (m Migrator) RenameTable(oldName, newName interface{}) error {
newTable = clause.Table{Name: v} newTable = clause.Table{Name: v}
} else { } else {
stmt := &gorm.Statement{DB: m.DB} stmt := &gorm.Statement{DB: m.DB}
if err := stmt.Parse(newName); err == nil { if err := stmt.ParseWithLogger(newName, m.DB.Logger); err == nil {
newTable = m.CurrentTable(stmt) newTable = m.CurrentTable(stmt)
} else { } else {
return err return err
@ -918,7 +918,7 @@ func (m Migrator) ReorderModels(values []interface{}, autoAdd bool) (results []i
} }
beDependedOn := map[*schema.Schema]bool{} beDependedOn := map[*schema.Schema]bool{}
// support for special table name // support for special table name
if err := dep.ParseWithSpecialTableName(value, m.DB.Statement.Table); err != nil { if err := dep.ParseWithSpecialTableNameWithLogger(value, m.DB.Statement.Table, m.Config.DB.Logger); err != nil {
m.DB.Logger.Error(context.Background(), "failed to parse value %#v, got error %v", value, err) m.DB.Logger.Error(context.Background(), "failed to parse value %#v, got error %v", value, err)
} }
if _, ok := parsedSchemas[dep.Statement.Schema]; ok { if _, ok := parsedSchemas[dep.Statement.Schema]; ok {

View File

@ -115,11 +115,19 @@ type TablerWithNamer interface {
// Parse get data type from dialector // Parse get data type from dialector
func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error) { func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error) {
return ParseWithSpecialTableName(dest, cacheStore, namer, "") return ParseWithSpecialTableNameWithLogger(dest, cacheStore, namer, "", logger.Default)
}
func ParseWithLogger(dest interface{}, cacheStore *sync.Map, namer Namer, log logger.Interface) (*Schema, error) {
return ParseWithSpecialTableNameWithLogger(dest, cacheStore, namer, "", log)
} }
// ParseWithSpecialTableName get data type from dialector with extra schema table // ParseWithSpecialTableName get data type from dialector with extra schema table
func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Namer, specialTableName string) (*Schema, error) { func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Namer, specialTableName string) (*Schema, error) {
return ParseWithSpecialTableNameWithLogger(dest, cacheStore, namer, specialTableName, logger.Default)
}
func ParseWithSpecialTableNameWithLogger(dest interface{}, cacheStore *sync.Map, namer Namer, specialTableName string, log logger.Interface) (*Schema, error) {
if dest == nil { if dest == nil {
return nil, fmt.Errorf("%w: %+v", ErrUnsupportedDataType, dest) return nil, fmt.Errorf("%w: %+v", ErrUnsupportedDataType, dest)
} }
@ -316,7 +324,7 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
case "func(*gorm.DB) error": // TODO hack case "func(*gorm.DB) error": // TODO hack
reflect.Indirect(reflect.ValueOf(schema)).FieldByName(string(cbName)).SetBool(true) reflect.Indirect(reflect.ValueOf(schema)).FieldByName(string(cbName)).SetBool(true)
default: default:
logger.Default.Warn(context.Background(), "Model %v don't match %vInterface, should be `%v(*gorm.DB) error`. Please see https://gorm.io/docs/hooks.html", schema, cbName, cbName) log.Warn(context.Background(), "Model %v don't match %vInterface, should be `%v(*gorm.DB) error`. Please see https://gorm.io/docs/hooks.html", schema, cbName, cbName)
} }
} }
} }
@ -331,7 +339,7 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
defer func() { defer func() {
if schema.err != nil { if schema.err != nil {
logger.Default.Error(context.Background(), schema.err.Error()) log.Error(context.Background(), schema.err.Error())
cacheStore.Delete(modelType) cacheStore.Delete(modelType)
} }
}() }()

View File

@ -486,11 +486,19 @@ func (stmt *Statement) Build(clauses ...string) {
} }
func (stmt *Statement) Parse(value interface{}) (err error) { func (stmt *Statement) Parse(value interface{}) (err error) {
return stmt.ParseWithSpecialTableName(value, "") return stmt.ParseWithSpecialTableNameWithLogger(value, "", logger.Default)
}
func (stmt *Statement) ParseWithLogger(value interface{}, log logger.Interface) (err error) {
return stmt.ParseWithSpecialTableNameWithLogger(value, "", log)
} }
func (stmt *Statement) ParseWithSpecialTableName(value interface{}, specialTableName string) (err error) { func (stmt *Statement) ParseWithSpecialTableName(value interface{}, specialTableName string) (err error) {
if stmt.Schema, err = schema.ParseWithSpecialTableName(value, stmt.DB.cacheStore, stmt.DB.NamingStrategy, specialTableName); err == nil && stmt.Table == "" { return stmt.ParseWithSpecialTableNameWithLogger(value, specialTableName, logger.Default)
}
func (stmt *Statement) ParseWithSpecialTableNameWithLogger(value interface{}, specialTableName string, log logger.Interface) (err error) {
if stmt.Schema, err = schema.ParseWithSpecialTableNameWithLogger(value, stmt.DB.cacheStore, stmt.DB.NamingStrategy, specialTableName, log); err == nil && stmt.Table == "" {
if tables := strings.Split(stmt.Schema.Table, "."); len(tables) == 2 { if tables := strings.Split(stmt.Schema.Table, "."); len(tables) == 2 {
stmt.TableExpr = &clause.Expr{SQL: stmt.Quote(stmt.Schema.Table)} stmt.TableExpr = &clause.Expr{SQL: stmt.Quote(stmt.Schema.Table)}
stmt.Table = tables[1] stmt.Table = tables[1]

View File

@ -8,7 +8,7 @@ require (
github.com/lib/pq v1.10.9 github.com/lib/pq v1.10.9
github.com/stretchr/testify v1.9.0 github.com/stretchr/testify v1.9.0
gorm.io/driver/mysql v1.5.7 gorm.io/driver/mysql v1.5.7
gorm.io/driver/postgres v1.5.9 gorm.io/driver/postgres v1.5.10
gorm.io/driver/sqlite v1.5.6 gorm.io/driver/sqlite v1.5.6
gorm.io/driver/sqlserver v1.5.4 gorm.io/driver/sqlserver v1.5.4
gorm.io/gorm v1.25.12 gorm.io/gorm v1.25.12
@ -25,12 +25,12 @@ require (
github.com/jackc/pgx/v5 v5.7.1 // indirect github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/kr/text v0.2.0 // indirect github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v1.14.23 // indirect github.com/mattn/go-sqlite3 v1.14.24 // indirect
github.com/microsoft/go-mssqldb v1.7.2 // indirect github.com/microsoft/go-mssqldb v1.7.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect
golang.org/x/crypto v0.27.0 // indirect golang.org/x/crypto v0.29.0 // indirect
golang.org/x/text v0.18.0 // indirect golang.org/x/text v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

View File

@ -16,21 +16,22 @@ then
fi fi
# SqlServer for Mac M1 # SqlServer for Mac M1
if [[ -z $GITHUB_ACTION ]]; then if [[ -z $GITHUB_ACTION && -d tests ]]; then
if [ -d tests ]
then
cd tests cd tests
if [[ $(uname -a) == *" arm64" ]]; then if [[ $(uname -a) == *" arm64" ]]; then
MSSQL_IMAGE=mcr.microsoft.com/azure-sql-edge docker compose up -d || true MSSQL_IMAGE=mcr.microsoft.com/azure-sql-edge docker compose up -d --wait
go install github.com/microsoft/go-sqlcmd/cmd/sqlcmd@latest || true go install github.com/microsoft/go-sqlcmd/cmd/sqlcmd@latest || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF DB_ID('gorm') IS NULL CREATE DATABASE gorm" > /dev/null || true for query in \
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF SUSER_ID (N'gorm') IS NULL CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';" > /dev/null || true "IF DB_ID('gorm') IS NULL CREATE DATABASE gorm" \
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF USER_ID (N'gorm') IS NULL CREATE USER gorm FROM LOGIN gorm; ALTER SERVER ROLE sysadmin ADD MEMBER [gorm];" > /dev/null || true "IF SUSER_ID (N'gorm') IS NULL CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';" \
"IF USER_ID (N'gorm') IS NULL CREATE USER gorm FROM LOGIN gorm; ALTER SERVER ROLE sysadmin ADD MEMBER [gorm];"
do
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "$query" > /dev/null || true
done
else else
MSSQL_IMAGE=mcr.microsoft.com/mssql/server docker compose up -d MSSQL_IMAGE=mcr.microsoft.com/mssql/server docker compose up -d --wait
fi fi
cd .. cd ..
fi
fi fi