From b0c6124926e3adfbb43395918bcea4a9e3e0d4b1 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 26 Feb 2018 22:29:21 +0800 Subject: [PATCH] Update structure --- .gitignore | 2 -- api.go | 35 ++++++++++++++----- dialect.go | 9 +++++ dialects/dialect.go | 13 ------- gorm.go | 5 ++- .../utils/statement.go => model/model.go | 20 +++++++++-- {dialects/common/utils => model}/utils.go | 2 +- 7 files changed, 55 insertions(+), 31 deletions(-) delete mode 100644 .gitignore create mode 100644 dialect.go delete mode 100644 dialects/dialect.go rename dialects/common/utils/statement.go => model/model.go (73%) rename {dialects/common/utils => model}/utils.go (96%) diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 01dc5ce0..00000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -documents -_book diff --git a/api.go b/api.go index 6bd9cb5d..07910a62 100644 --- a/api.go +++ b/api.go @@ -2,7 +2,6 @@ package gorm import ( "github.com/jinzhu/gorm/builder" - "github.com/jinzhu/gorm/dialects" ) // Where add condition @@ -150,12 +149,17 @@ func (s *DB) Find(out interface{}, where ...interface{}) *DB { stmt := tx.Statement stmt.Dest = out + // has inline condition if len(where) > 0 { + clone := tx.clone() stmt = s.Statement.Clone() stmt.Conditions = append(stmt.Conditions, s.Statement.BuildCondition(where[0], where[1:]...)) + tx.AddError(clone.Dialect().Query(clone)) + tx.AddError(clone.Error) + } else { + tx.AddError(tx.Dialect().Query(tx)) } - tx.AddError(tx.Dialect().Query(stmt)) return tx } @@ -168,7 +172,7 @@ func (s *DB) Scan(dest interface{}) *DB { stmt.Table = stmt.Dest stmt.Dest = dest - tx.AddError(tx.Dialect().Query(stmt)) + tx.AddError(tx.Dialect().Query(tx)) return tx } @@ -176,7 +180,7 @@ func (s *DB) Scan(dest interface{}) *DB { func (s *DB) Create(value interface{}) *DB { tx := s.init() tx.Statement.Dest = value - tx.AddError(tx.Dialect().Insert(tx.Statement)) + tx.AddError(tx.Dialect().Insert(tx)) return tx } @@ -185,7 +189,7 @@ func (s *DB) Save(value interface{}) *DB { tx := s.init() tx.Statement.Dest = value // FIXME check primary key has value or not - tx.AddError(tx.Dialect().Update(tx.Statement)) + tx.AddError(tx.Dialect().Update(tx)) return tx } @@ -193,7 +197,7 @@ func (s *DB) Save(value interface{}) *DB { func (s *DB) Update(column string, value interface{}) *DB { tx := s.init() tx.Statement.Assignments = append(tx.Statement.Assignments, builder.Assignment{Column: column, Value: value}) - tx.AddError(tx.Dialect().Update(tx.Statement)) + tx.AddError(tx.Dialect().Update(tx)) return tx } @@ -201,7 +205,7 @@ func (s *DB) Update(column string, value interface{}) *DB { func (s *DB) Updates(values interface{}) *DB { tx := s.init() tx.Statement.Assignments = append(tx.Statement.Assignments, builder.Assignment{Value: values}) - tx.AddError(tx.Dialect().Update(tx.Statement)) + tx.AddError(tx.Dialect().Update(tx)) return tx } @@ -211,12 +215,17 @@ func (s *DB) Delete(value interface{}, where ...interface{}) *DB { stmt := tx.Statement stmt.Dest = value + // has inline condition if len(where) > 0 { + clone := tx.clone() stmt = s.Statement.Clone() stmt.Conditions = append(stmt.Conditions, s.Statement.BuildCondition(where[0], where[1:]...)) + tx.AddError(clone.Dialect().Update(clone)) + tx.AddError(clone.Error) + } else { + tx.AddError(tx.Dialect().Update(tx)) } - tx.AddError(tx.Dialect().Update(stmt)) return tx } @@ -264,7 +273,7 @@ func (s *DB) GetErrors() []error { } // Dialect return DB dialect -func (s *DB) Dialect() dialects.Dialect { +func (s *DB) Dialect() Dialect { if s.TxDialect != nil { return s.TxDialect } @@ -302,3 +311,11 @@ func (s *DB) init() *DB { } return s } + +func (s *DB) clone() *DB { + return &DB{ + TxDialect: s.TxDialect, + Statement: s.Statement, + Config: s.Config, + } +} diff --git a/dialect.go b/dialect.go new file mode 100644 index 00000000..19b3fcea --- /dev/null +++ b/dialect.go @@ -0,0 +1,9 @@ +package gorm + +// Dialect GORM dialect interface +type Dialect interface { + Insert(*DB) error + Query(*DB) error + Update(*DB) error + Delete(*DB) error +} diff --git a/dialects/dialect.go b/dialects/dialect.go deleted file mode 100644 index fe1e1d28..00000000 --- a/dialects/dialect.go +++ /dev/null @@ -1,13 +0,0 @@ -package dialects - -import ( - "github.com/jinzhu/gorm/builder" -) - -// Dialect GORM dialect interface -type Dialect interface { - Insert(*builder.Statement) error - Query(*builder.Statement) error - Update(*builder.Statement) error - Delete(*builder.Statement) error -} diff --git a/gorm.go b/gorm.go index e1b3fa3b..5085e26f 100644 --- a/gorm.go +++ b/gorm.go @@ -4,7 +4,6 @@ import ( "time" "github.com/jinzhu/gorm/builder" - "github.com/jinzhu/gorm/dialects" "github.com/jinzhu/gorm/logger" ) @@ -24,12 +23,12 @@ type Config struct { LogMode logger.LogLevel // Dialect DB Dialect - Dialect dialects.Dialect + Dialect Dialect } // DB GORM DB definition type DB struct { - TxDialect dialects.Dialect + TxDialect Dialect Statement *builder.Statement // Global config diff --git a/dialects/common/utils/statement.go b/model/model.go similarity index 73% rename from dialects/common/utils/statement.go rename to model/model.go index 319c5b31..833e916a 100644 --- a/dialects/common/utils/statement.go +++ b/model/model.go @@ -1,4 +1,4 @@ -package utils +package model import ( "github.com/jinzhu/gorm" @@ -18,7 +18,22 @@ func GetCreatingAssignments(stmt *builder.Statement, errs *gorm.Errors) chan []s // GetTable get table name func GetTable(stmt *builder.Statement, errs *gorm.Errors) chan string { - return nil + tableChan := make(chan string) + + go func() { + if stmt.Table != nil { + if table, ok := stmt.Table.(string); ok { + tableChan <- DefaultTableNameHandler(stmt, table) + } else if tableSchema := schema.Parse(stmt.Table); tableSchema != nil { + if tableSchema.TableName != "" { + tableChan <- DefaultTableNameHandler(stmt, tableSchema.TableName) + } + tableSchema.ModelType.Name + } + } + }() + + return tableChan } // if scope.Value == nil { @@ -38,6 +53,5 @@ func GetTable(stmt *builder.Statement, errs *gorm.Errors) chan string { // s.defaultTableName = tableName // } // } - // return DefaultTableNameHandler(db, s.defaultTableName) // } diff --git a/dialects/common/utils/utils.go b/model/utils.go similarity index 96% rename from dialects/common/utils/utils.go rename to model/utils.go index c38d23c1..edd67a0a 100644 --- a/dialects/common/utils/utils.go +++ b/model/utils.go @@ -1,4 +1,4 @@ -package utils +package model // ToSearchableMap convert attrs to searchable map func ToSearchableMap(attrs ...interface{}) (result interface{}) {