12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406 |
- package gorm
- import (
- "bytes"
- "database/sql"
- "database/sql/driver"
- "errors"
- "fmt"
- "reflect"
- "regexp"
- "strings"
- "time"
- )
- type Scope struct {
- Search *search
- Value interface{}
- SQL string
- SQLVars []interface{}
- db *DB
- instanceID string
- primaryKeyField *Field
- skipLeft bool
- fields *[]*Field
- selectAttrs *[]string
- }
- func (scope *Scope) IndirectValue() reflect.Value {
- return indirect(reflect.ValueOf(scope.Value))
- }
- func (scope *Scope) New(value interface{}) *Scope {
- return &Scope{db: scope.NewDB(), Search: &search{}, Value: value}
- }
- func (scope *Scope) DB() *DB {
- return scope.db
- }
- func (scope *Scope) NewDB() *DB {
- if scope.db != nil {
- db := scope.db.clone()
- db.search = nil
- db.Value = nil
- return db
- }
- return nil
- }
- func (scope *Scope) SQLDB() SQLCommon {
- return scope.db.db
- }
- func (scope *Scope) Dialect() Dialect {
- return scope.db.dialect
- }
- func (scope *Scope) Quote(str string) string {
- if strings.Contains(str, ".") {
- newStrs := []string{}
- for _, str := range strings.Split(str, ".") {
- newStrs = append(newStrs, scope.Dialect().Quote(str))
- }
- return strings.Join(newStrs, ".")
- }
- return scope.Dialect().Quote(str)
- }
- func (scope *Scope) Err(err error) error {
- if err != nil {
- scope.db.AddError(err)
- }
- return err
- }
- func (scope *Scope) HasError() bool {
- return scope.db.Error != nil
- }
- func (scope *Scope) Log(v ...interface{}) {
- scope.db.log(v...)
- }
- func (scope *Scope) SkipLeft() {
- scope.skipLeft = true
- }
- func (scope *Scope) Fields() []*Field {
- if scope.fields == nil {
- var (
- fields []*Field
- indirectScopeValue = scope.IndirectValue()
- isStruct = indirectScopeValue.Kind() == reflect.Struct
- )
- for _, structField := range scope.GetModelStruct().StructFields {
- if isStruct {
- fieldValue := indirectScopeValue
- for _, name := range structField.Names {
- if fieldValue.Kind() == reflect.Ptr && fieldValue.IsNil() {
- fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
- }
- fieldValue = reflect.Indirect(fieldValue).FieldByName(name)
- }
- fields = append(fields, &Field{StructField: structField, Field: fieldValue, IsBlank: isBlank(fieldValue)})
- } else {
- fields = append(fields, &Field{StructField: structField, IsBlank: true})
- }
- }
- scope.fields = &fields
- }
- return *scope.fields
- }
- func (scope *Scope) FieldByName(name string) (field *Field, ok bool) {
- var (
- dbName = ToColumnName(name)
- mostMatchedField *Field
- )
- for _, field := range scope.Fields() {
- if field.Name == name || field.DBName == name {
- return field, true
- }
- if field.DBName == dbName {
- mostMatchedField = field
- }
- }
- return mostMatchedField, mostMatchedField != nil
- }
- func (scope *Scope) PrimaryFields() (fields []*Field) {
- for _, field := range scope.Fields() {
- if field.IsPrimaryKey {
- fields = append(fields, field)
- }
- }
- return fields
- }
- func (scope *Scope) PrimaryField() *Field {
- if primaryFields := scope.GetModelStruct().PrimaryFields; len(primaryFields) > 0 {
- if len(primaryFields) > 1 {
- if field, ok := scope.FieldByName("id"); ok {
- return field
- }
- }
- return scope.PrimaryFields()[0]
- }
- return nil
- }
- func (scope *Scope) PrimaryKey() string {
- if field := scope.PrimaryField(); field != nil {
- return field.DBName
- }
- return ""
- }
- func (scope *Scope) PrimaryKeyZero() bool {
- field := scope.PrimaryField()
- return field == nil || field.IsBlank
- }
- func (scope *Scope) PrimaryKeyValue() interface{} {
- if field := scope.PrimaryField(); field != nil && field.Field.IsValid() {
- return field.Field.Interface()
- }
- return 0
- }
- func (scope *Scope) HasColumn(column string) bool {
- for _, field := range scope.GetStructFields() {
- if field.IsNormal && (field.Name == column || field.DBName == column) {
- return true
- }
- }
- return false
- }
- func (scope *Scope) SetColumn(column interface{}, value interface{}) error {
- var updateAttrs = map[string]interface{}{}
- if attrs, ok := scope.InstanceGet("gorm:update_attrs"); ok {
- updateAttrs = attrs.(map[string]interface{})
- defer scope.InstanceSet("gorm:update_attrs", updateAttrs)
- }
- if field, ok := column.(*Field); ok {
- updateAttrs[field.DBName] = value
- return field.Set(value)
- } else if name, ok := column.(string); ok {
- var (
- dbName = ToDBName(name)
- mostMatchedField *Field
- )
- for _, field := range scope.Fields() {
- if field.DBName == value {
- updateAttrs[field.DBName] = value
- return field.Set(value)
- }
- if (field.DBName == dbName) || (field.Name == name && mostMatchedField == nil) {
- mostMatchedField = field
- }
- }
- if mostMatchedField != nil {
- updateAttrs[mostMatchedField.DBName] = value
- return mostMatchedField.Set(value)
- }
- }
- return errors.New("could not convert column to field")
- }
- func (scope *Scope) CallMethod(methodName string) {
- if scope.Value == nil {
- return
- }
- if indirectScopeValue := scope.IndirectValue(); indirectScopeValue.Kind() == reflect.Slice {
- for i := 0; i < indirectScopeValue.Len(); i++ {
- scope.callMethod(methodName, indirectScopeValue.Index(i))
- }
- } else {
- scope.callMethod(methodName, indirectScopeValue)
- }
- }
- func (scope *Scope) AddToVars(value interface{}) string {
- _, skipBindVar := scope.InstanceGet("skip_bindvar")
- if expr, ok := value.(*expr); ok {
- exp := expr.expr
- for _, arg := range expr.args {
- if skipBindVar {
- scope.AddToVars(arg)
- } else {
- exp = strings.Replace(exp, "?", scope.AddToVars(arg), 1)
- }
- }
- return exp
- }
- scope.SQLVars = append(scope.SQLVars, value)
- if skipBindVar {
- return "?"
- }
- return scope.Dialect().BindVar(len(scope.SQLVars))
- }
- func (scope *Scope) SelectAttrs() []string {
- if scope.selectAttrs == nil {
- attrs := []string{}
- for _, value := range scope.Search.selects {
- if str, ok := value.(string); ok {
- attrs = append(attrs, str)
- } else if strs, ok := value.([]string); ok {
- attrs = append(attrs, strs...)
- } else if strs, ok := value.([]interface{}); ok {
- for _, str := range strs {
- attrs = append(attrs, fmt.Sprintf("%v", str))
- }
- }
- }
- scope.selectAttrs = &attrs
- }
- return *scope.selectAttrs
- }
- func (scope *Scope) OmitAttrs() []string {
- return scope.Search.omits
- }
- type tabler interface {
- TableName() string
- }
- type dbTabler interface {
- TableName(*DB) string
- }
- func (scope *Scope) TableName() string {
- if scope.Search != nil && len(scope.Search.tableName) > 0 {
- return scope.Search.tableName
- }
- if tabler, ok := scope.Value.(tabler); ok {
- return tabler.TableName()
- }
- if tabler, ok := scope.Value.(dbTabler); ok {
- return tabler.TableName(scope.db)
- }
- return scope.GetModelStruct().TableName(scope.db.Model(scope.Value))
- }
- func (scope *Scope) QuotedTableName() (name string) {
- if scope.Search != nil && len(scope.Search.tableName) > 0 {
- if strings.Contains(scope.Search.tableName, " ") {
- return scope.Search.tableName
- }
- return scope.Quote(scope.Search.tableName)
- }
- return scope.Quote(scope.TableName())
- }
- func (scope *Scope) CombinedConditionSql() string {
- joinSQL := scope.joinsSQL()
- whereSQL := scope.whereSQL()
- if scope.Search.raw {
- whereSQL = strings.TrimSuffix(strings.TrimPrefix(whereSQL, "WHERE ("), ")")
- }
- return joinSQL + whereSQL + scope.groupSQL() +
- scope.havingSQL() + scope.orderSQL() + scope.limitAndOffsetSQL()
- }
- func (scope *Scope) Raw(sql string) *Scope {
- scope.SQL = strings.Replace(sql, "$$$", "?", -1)
- return scope
- }
- func (scope *Scope) Exec() *Scope {
- defer scope.trace(NowFunc())
- if !scope.HasError() {
- if result, err := scope.SQLDB().Exec(scope.SQL, scope.SQLVars...); scope.Err(err) == nil {
- if count, err := result.RowsAffected(); scope.Err(err) == nil {
- scope.db.RowsAffected = count
- }
- }
- }
- return scope
- }
- func (scope *Scope) Set(name string, value interface{}) *Scope {
- scope.db.InstantSet(name, value)
- return scope
- }
- func (scope *Scope) Get(name string) (interface{}, bool) {
- return scope.db.Get(name)
- }
- func (scope *Scope) InstanceID() string {
- if scope.instanceID == "" {
- scope.instanceID = fmt.Sprintf("%v%v", &scope, &scope.db)
- }
- return scope.instanceID
- }
- func (scope *Scope) InstanceSet(name string, value interface{}) *Scope {
- return scope.Set(name+scope.InstanceID(), value)
- }
- func (scope *Scope) InstanceGet(name string) (interface{}, bool) {
- return scope.Get(name + scope.InstanceID())
- }
- func (scope *Scope) Begin() *Scope {
- if db, ok := scope.SQLDB().(sqlDb); ok {
- if tx, err := db.Begin(); err == nil {
- scope.db.db = interface{}(tx).(SQLCommon)
- scope.InstanceSet("gorm:started_transaction", true)
- }
- }
- return scope
- }
- func (scope *Scope) CommitOrRollback() *Scope {
- if _, ok := scope.InstanceGet("gorm:started_transaction"); ok {
- if db, ok := scope.db.db.(sqlTx); ok {
- if scope.HasError() {
- db.Rollback()
- } else {
- scope.Err(db.Commit())
- }
- scope.db.db = scope.db.parent.db
- }
- }
- return scope
- }
- func (scope *Scope) callMethod(methodName string, reflectValue reflect.Value) {
-
- if reflectValue.CanAddr() && reflectValue.Kind() != reflect.Ptr {
- reflectValue = reflectValue.Addr()
- }
- if methodValue := reflectValue.MethodByName(methodName); methodValue.IsValid() {
- switch method := methodValue.Interface().(type) {
- case func():
- method()
- case func(*Scope):
- method(scope)
- case func(*DB):
- newDB := scope.NewDB()
- method(newDB)
- scope.Err(newDB.Error)
- case func() error:
- scope.Err(method())
- case func(*Scope) error:
- scope.Err(method(scope))
- case func(*DB) error:
- newDB := scope.NewDB()
- scope.Err(method(newDB))
- scope.Err(newDB.Error)
- default:
- scope.Err(fmt.Errorf("unsupported function %v", methodName))
- }
- }
- }
- var (
- columnRegexp = regexp.MustCompile("^[a-zA-Z\\d]+(\\.[a-zA-Z\\d]+)*$")
- isNumberRegexp = regexp.MustCompile("^\\s*\\d+\\s*$")
- comparisonRegexp = regexp.MustCompile("(?i) (=|<>|(>|<)(=?)|LIKE|IS|IN) ")
- countingQueryRegexp = regexp.MustCompile("(?i)^count(.+)$")
- )
- func (scope *Scope) quoteIfPossible(str string) string {
- if columnRegexp.MatchString(str) {
- return scope.Quote(str)
- }
- return str
- }
- func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) {
- var (
- ignored interface{}
- values = make([]interface{}, len(columns))
- selectFields []*Field
- selectedColumnsMap = map[string]int{}
- resetFields = map[int]*Field{}
- )
- for index, column := range columns {
- values[index] = &ignored
- selectFields = fields
- offset := 0
- if idx, ok := selectedColumnsMap[column]; ok {
- offset = idx + 1
- selectFields = selectFields[offset:]
- }
- for fieldIndex, field := range selectFields {
- if field.DBName == column {
- if field.Field.Kind() == reflect.Ptr {
- values[index] = field.Field.Addr().Interface()
- } else {
- reflectValue := reflect.New(reflect.PtrTo(field.Struct.Type))
- reflectValue.Elem().Set(field.Field.Addr())
- values[index] = reflectValue.Interface()
- resetFields[index] = field
- }
- selectedColumnsMap[column] = offset + fieldIndex
- if field.IsNormal {
- break
- }
- }
- }
- }
- scope.Err(rows.Scan(values...))
- for index, field := range resetFields {
- if v := reflect.ValueOf(values[index]).Elem().Elem(); v.IsValid() {
- field.Field.Set(v)
- }
- }
- }
- func (scope *Scope) primaryCondition(value interface{}) string {
- return fmt.Sprintf("(%v.%v = %v)", scope.QuotedTableName(), scope.Quote(scope.PrimaryKey()), value)
- }
- func (scope *Scope) buildCondition(clause map[string]interface{}, include bool) (str string) {
- var (
- quotedTableName = scope.QuotedTableName()
- quotedPrimaryKey = scope.Quote(scope.PrimaryKey())
- equalSQL = "="
- inSQL = "IN"
- )
-
- if !include {
- equalSQL = "<>"
- inSQL = "NOT IN"
- }
- switch value := clause["query"].(type) {
- case sql.NullInt64:
- return fmt.Sprintf("(%v.%v %s %v)", quotedTableName, quotedPrimaryKey, equalSQL, value.Int64)
- case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
- return fmt.Sprintf("(%v.%v %s %v)", quotedTableName, quotedPrimaryKey, equalSQL, value)
- case []int, []int8, []int16, []int32, []int64, []uint, []uint8, []uint16, []uint32, []uint64, []string, []interface{}:
- if !include && reflect.ValueOf(value).Len() == 0 {
- return
- }
- str = fmt.Sprintf("(%v.%v %s (?))", quotedTableName, quotedPrimaryKey, inSQL)
- clause["args"] = []interface{}{value}
- case string:
- if isNumberRegexp.MatchString(value) {
- return fmt.Sprintf("(%v.%v %s %v)", quotedTableName, quotedPrimaryKey, equalSQL, scope.AddToVars(value))
- }
- if value != "" {
- if !include {
- if comparisonRegexp.MatchString(value) {
- str = fmt.Sprintf("NOT (%v)", value)
- } else {
- str = fmt.Sprintf("(%v.%v NOT IN (?))", quotedTableName, scope.Quote(value))
- }
- } else {
- str = fmt.Sprintf("(%v)", value)
- }
- }
- case map[string]interface{}:
- var sqls []string
- for key, value := range value {
- if value != nil {
- sqls = append(sqls, fmt.Sprintf("(%v.%v %s %v)", quotedTableName, scope.Quote(key), equalSQL, scope.AddToVars(value)))
- } else {
- if !include {
- sqls = append(sqls, fmt.Sprintf("(%v.%v IS NOT NULL)", quotedTableName, scope.Quote(key)))
- } else {
- sqls = append(sqls, fmt.Sprintf("(%v.%v IS NULL)", quotedTableName, scope.Quote(key)))
- }
- }
- }
- return strings.Join(sqls, " AND ")
- case interface{}:
- var sqls []string
- newScope := scope.New(value)
- if len(newScope.Fields()) == 0 {
- scope.Err(fmt.Errorf("invalid query condition: %v", value))
- return
- }
- scopeQuotedTableName := newScope.QuotedTableName()
- for _, field := range newScope.Fields() {
- if !field.IsIgnored && !field.IsBlank {
- sqls = append(sqls, fmt.Sprintf("(%v.%v %s %v)", scopeQuotedTableName, scope.Quote(field.DBName), equalSQL, scope.AddToVars(field.Field.Interface())))
- }
- }
- return strings.Join(sqls, " AND ")
- default:
- scope.Err(fmt.Errorf("invalid query condition: %v", value))
- return
- }
- replacements := []string{}
- args := clause["args"].([]interface{})
- for _, arg := range args {
- var err error
- switch reflect.ValueOf(arg).Kind() {
- case reflect.Slice:
- if scanner, ok := interface{}(arg).(driver.Valuer); ok {
- arg, err = scanner.Value()
- replacements = append(replacements, scope.AddToVars(arg))
- } else if b, ok := arg.([]byte); ok {
- replacements = append(replacements, scope.AddToVars(b))
- } else if as, ok := arg.([][]interface{}); ok {
- var tempMarks []string
- for _, a := range as {
- var arrayMarks []string
- for _, v := range a {
- arrayMarks = append(arrayMarks, scope.AddToVars(v))
- }
- if len(arrayMarks) > 0 {
- tempMarks = append(tempMarks, fmt.Sprintf("(%v)", strings.Join(arrayMarks, ",")))
- }
- }
- if len(tempMarks) > 0 {
- replacements = append(replacements, strings.Join(tempMarks, ","))
- }
- } else if values := reflect.ValueOf(arg); values.Len() > 0 {
- var tempMarks []string
- for i := 0; i < values.Len(); i++ {
- tempMarks = append(tempMarks, scope.AddToVars(values.Index(i).Interface()))
- }
- replacements = append(replacements, strings.Join(tempMarks, ","))
- } else {
- replacements = append(replacements, scope.AddToVars(Expr("NULL")))
- }
- default:
- if valuer, ok := interface{}(arg).(driver.Valuer); ok {
- arg, err = valuer.Value()
- }
- replacements = append(replacements, scope.AddToVars(arg))
- }
- if err != nil {
- scope.Err(err)
- }
- }
- buff := bytes.NewBuffer([]byte{})
- i := 0
- for _, s := range str {
- if s == '?' && len(replacements) > i {
- buff.WriteString(replacements[i])
- i++
- } else {
- buff.WriteRune(s)
- }
- }
- str = buff.String()
- return
- }
- func (scope *Scope) buildSelectQuery(clause map[string]interface{}) (str string) {
- switch value := clause["query"].(type) {
- case string:
- str = value
- case []string:
- str = strings.Join(value, ", ")
- }
- args := clause["args"].([]interface{})
- replacements := []string{}
- for _, arg := range args {
- switch reflect.ValueOf(arg).Kind() {
- case reflect.Slice:
- values := reflect.ValueOf(arg)
- var tempMarks []string
- for i := 0; i < values.Len(); i++ {
- tempMarks = append(tempMarks, scope.AddToVars(values.Index(i).Interface()))
- }
- replacements = append(replacements, strings.Join(tempMarks, ","))
- default:
- if valuer, ok := interface{}(arg).(driver.Valuer); ok {
- arg, _ = valuer.Value()
- }
- replacements = append(replacements, scope.AddToVars(arg))
- }
- }
- buff := bytes.NewBuffer([]byte{})
- i := 0
- for pos, char := range str {
- if str[pos] == '?' {
- buff.WriteString(replacements[i])
- i++
- } else {
- buff.WriteRune(char)
- }
- }
- str = buff.String()
- return
- }
- func (scope *Scope) whereSQL() (sql string) {
- var (
- quotedTableName = scope.QuotedTableName()
- deletedAtField, hasDeletedAtField = scope.FieldByName("DeletedAt")
- primaryConditions, andConditions, orConditions []string
- )
- if !scope.Search.Unscoped && hasDeletedAtField {
- sql := fmt.Sprintf("%v.%v IS NULL", quotedTableName, scope.Quote(deletedAtField.DBName))
- primaryConditions = append(primaryConditions, sql)
- }
- if !scope.PrimaryKeyZero() {
- for _, field := range scope.PrimaryFields() {
- sql := fmt.Sprintf("%v.%v = %v", quotedTableName, scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))
- primaryConditions = append(primaryConditions, sql)
- }
- }
- for _, clause := range scope.Search.whereConditions {
- if sql := scope.buildCondition(clause, true); sql != "" {
- andConditions = append(andConditions, sql)
- }
- }
- for _, clause := range scope.Search.orConditions {
- if sql := scope.buildCondition(clause, true); sql != "" {
- orConditions = append(orConditions, sql)
- }
- }
- for _, clause := range scope.Search.notConditions {
- if sql := scope.buildCondition(clause, false); sql != "" {
- andConditions = append(andConditions, sql)
- }
- }
- orSQL := strings.Join(orConditions, " OR ")
- combinedSQL := strings.Join(andConditions, " AND ")
- if len(combinedSQL) > 0 {
- if len(orSQL) > 0 {
- combinedSQL = combinedSQL + " OR " + orSQL
- }
- } else {
- combinedSQL = orSQL
- }
- if len(primaryConditions) > 0 {
- sql = "WHERE " + strings.Join(primaryConditions, " AND ")
- if len(combinedSQL) > 0 {
- sql = sql + " AND (" + combinedSQL + ")"
- }
- } else if len(combinedSQL) > 0 {
- sql = "WHERE " + combinedSQL
- }
- return
- }
- func (scope *Scope) selectSQL() string {
- if len(scope.Search.selects) == 0 {
- if len(scope.Search.joinConditions) > 0 {
- return fmt.Sprintf("%v.*", scope.QuotedTableName())
- }
- return "*"
- }
- return scope.buildSelectQuery(scope.Search.selects)
- }
- func (scope *Scope) orderSQL() string {
- if len(scope.Search.orders) == 0 || scope.Search.ignoreOrderQuery {
- return ""
- }
- var orders []string
- for _, order := range scope.Search.orders {
- if str, ok := order.(string); ok {
- orders = append(orders, scope.quoteIfPossible(str))
- } else if expr, ok := order.(*expr); ok {
- exp := expr.expr
- for _, arg := range expr.args {
- exp = strings.Replace(exp, "?", scope.AddToVars(arg), 1)
- }
- orders = append(orders, exp)
- }
- }
- return " ORDER BY " + strings.Join(orders, ",")
- }
- func (scope *Scope) limitAndOffsetSQL() string {
- return scope.Dialect().LimitAndOffsetSQL(scope.Search.limit, scope.Search.offset)
- }
- func (scope *Scope) groupSQL() string {
- if len(scope.Search.group) == 0 {
- return ""
- }
- return " GROUP BY " + scope.Search.group
- }
- func (scope *Scope) havingSQL() string {
- if len(scope.Search.havingConditions) == 0 {
- return ""
- }
- var andConditions []string
- for _, clause := range scope.Search.havingConditions {
- if sql := scope.buildCondition(clause, true); sql != "" {
- andConditions = append(andConditions, sql)
- }
- }
- combinedSQL := strings.Join(andConditions, " AND ")
- if len(combinedSQL) == 0 {
- return ""
- }
- return " HAVING " + combinedSQL
- }
- func (scope *Scope) joinsSQL() string {
- var joinConditions []string
- for _, clause := range scope.Search.joinConditions {
- if sql := scope.buildCondition(clause, true); sql != "" {
- joinConditions = append(joinConditions, strings.TrimSuffix(strings.TrimPrefix(sql, "("), ")"))
- }
- }
- return strings.Join(joinConditions, " ") + " "
- }
- func (scope *Scope) prepareQuerySQL() {
- if scope.Search.raw {
- scope.Raw(scope.CombinedConditionSql())
- } else {
- scope.Raw(fmt.Sprintf("SELECT %v FROM %v %v", scope.selectSQL(), scope.QuotedTableName(), scope.CombinedConditionSql()))
- }
- return
- }
- func (scope *Scope) inlineCondition(values ...interface{}) *Scope {
- if len(values) > 0 {
- scope.Search.Where(values[0], values[1:]...)
- }
- return scope
- }
- func (scope *Scope) callCallbacks(funcs []*func(s *Scope)) *Scope {
- defer func() {
- if err := recover(); err != nil {
- if db, ok := scope.db.db.(sqlTx); ok {
- db.Rollback()
- }
- panic(err)
- }
- }()
- for _, f := range funcs {
- (*f)(scope)
- if scope.skipLeft {
- break
- }
- }
- return scope
- }
- func convertInterfaceToMap(values interface{}, withIgnoredField bool) map[string]interface{} {
- var attrs = map[string]interface{}{}
- switch value := values.(type) {
- case map[string]interface{}:
- return value
- case []interface{}:
- for _, v := range value {
- for key, value := range convertInterfaceToMap(v, withIgnoredField) {
- attrs[key] = value
- }
- }
- case interface{}:
- reflectValue := reflect.ValueOf(values)
- switch reflectValue.Kind() {
- case reflect.Map:
- for _, key := range reflectValue.MapKeys() {
- attrs[ToColumnName(key.Interface().(string))] = reflectValue.MapIndex(key).Interface()
- }
- default:
- for _, field := range (&Scope{Value: values}).Fields() {
- if !field.IsBlank && (withIgnoredField || !field.IsIgnored) {
- attrs[field.DBName] = field.Field.Interface()
- }
- }
- }
- }
- return attrs
- }
- func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[string]interface{}, hasUpdate bool) {
- if scope.IndirectValue().Kind() != reflect.Struct {
- return convertInterfaceToMap(value, false), true
- }
- results = map[string]interface{}{}
- for key, value := range convertInterfaceToMap(value, true) {
- if field, ok := scope.FieldByName(key); ok && scope.changeableField(field) {
- if _, ok := value.(*expr); ok {
- hasUpdate = true
- results[field.DBName] = value
- } else {
- err := field.Set(value)
- if field.IsNormal && !field.IsIgnored {
- hasUpdate = true
- if err == ErrUnaddressable {
- results[field.DBName] = value
- } else {
- results[field.DBName] = field.Field.Interface()
- }
- }
- }
- }
- }
- return
- }
- func (scope *Scope) row() *sql.Row {
- defer scope.trace(NowFunc())
- result := &RowQueryResult{}
- scope.InstanceSet("row_query_result", result)
- scope.callCallbacks(scope.db.parent.callbacks.rowQueries)
- return result.Row
- }
- func (scope *Scope) rows() (*sql.Rows, error) {
- defer scope.trace(NowFunc())
- result := &RowsQueryResult{}
- scope.InstanceSet("row_query_result", result)
- scope.callCallbacks(scope.db.parent.callbacks.rowQueries)
- return result.Rows, result.Error
- }
- func (scope *Scope) initialize() *Scope {
- for _, clause := range scope.Search.whereConditions {
- scope.updatedAttrsWithValues(clause["query"])
- }
- scope.updatedAttrsWithValues(scope.Search.initAttrs)
- scope.updatedAttrsWithValues(scope.Search.assignAttrs)
- return scope
- }
- func (scope *Scope) isQueryForColumn(query interface{}, column string) bool {
- queryStr := strings.ToLower(fmt.Sprint(query))
- if queryStr == column {
- return true
- }
- if strings.HasSuffix(queryStr, "as "+column) {
- return true
- }
- if strings.HasSuffix(queryStr, "as "+scope.Quote(column)) {
- return true
- }
- return false
- }
- func (scope *Scope) pluck(column string, value interface{}) *Scope {
- dest := reflect.Indirect(reflect.ValueOf(value))
- if dest.Kind() != reflect.Slice {
- scope.Err(fmt.Errorf("results should be a slice, not %s", dest.Kind()))
- return scope
- }
- if query, ok := scope.Search.selects["query"]; !ok || !scope.isQueryForColumn(query, column) {
- scope.Search.Select(column)
- }
- rows, err := scope.rows()
- if scope.Err(err) == nil {
- defer rows.Close()
- for rows.Next() {
- elem := reflect.New(dest.Type().Elem()).Interface()
- scope.Err(rows.Scan(elem))
- dest.Set(reflect.Append(dest, reflect.ValueOf(elem).Elem()))
- }
- if err := rows.Err(); err != nil {
- scope.Err(err)
- }
- }
- return scope
- }
- func (scope *Scope) count(value interface{}) *Scope {
- if query, ok := scope.Search.selects["query"]; !ok || !countingQueryRegexp.MatchString(fmt.Sprint(query)) {
- if len(scope.Search.group) != 0 {
- scope.Search.Select("count(*) FROM ( SELECT count(*) as name ")
- scope.Search.group += " ) AS count_table"
- } else {
- scope.Search.Select("count(*)")
- }
- }
- scope.Search.ignoreOrderQuery = true
- scope.Err(scope.row().Scan(value))
- return scope
- }
- func (scope *Scope) typeName() string {
- typ := scope.IndirectValue().Type()
- for typ.Kind() == reflect.Slice || typ.Kind() == reflect.Ptr {
- typ = typ.Elem()
- }
- return typ.Name()
- }
- func (scope *Scope) trace(t time.Time) {
- if len(scope.SQL) > 0 {
- scope.db.slog(scope.SQL, t, scope.SQLVars...)
- }
- }
- func (scope *Scope) changeableField(field *Field) bool {
- if selectAttrs := scope.SelectAttrs(); len(selectAttrs) > 0 {
- for _, attr := range selectAttrs {
- if field.Name == attr || field.DBName == attr {
- return true
- }
- }
- return false
- }
- for _, attr := range scope.OmitAttrs() {
- if field.Name == attr || field.DBName == attr {
- return false
- }
- }
- return true
- }
- func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
- toScope := scope.db.NewScope(value)
- tx := scope.db.Set("gorm:association:source", scope.Value)
- for _, foreignKey := range append(foreignKeys, toScope.typeName()+"Id", scope.typeName()+"Id") {
- fromField, _ := scope.FieldByName(foreignKey)
- toField, _ := toScope.FieldByName(foreignKey)
- if fromField != nil {
- if relationship := fromField.Relationship; relationship != nil {
- if relationship.Kind == "many_to_many" {
- joinTableHandler := relationship.JoinTableHandler
- scope.Err(joinTableHandler.JoinWith(joinTableHandler, tx, scope.Value).Find(value).Error)
- } else if relationship.Kind == "belongs_to" {
- for idx, foreignKey := range relationship.ForeignDBNames {
- if field, ok := scope.FieldByName(foreignKey); ok {
- tx = tx.Where(fmt.Sprintf("%v = ?", scope.Quote(relationship.AssociationForeignDBNames[idx])), field.Field.Interface())
- }
- }
- scope.Err(tx.Find(value).Error)
- } else if relationship.Kind == "has_many" || relationship.Kind == "has_one" {
- for idx, foreignKey := range relationship.ForeignDBNames {
- if field, ok := scope.FieldByName(relationship.AssociationForeignDBNames[idx]); ok {
- tx = tx.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface())
- }
- }
- if relationship.PolymorphicType != "" {
- tx = tx.Where(fmt.Sprintf("%v = ?", scope.Quote(relationship.PolymorphicDBName)), relationship.PolymorphicValue)
- }
- scope.Err(tx.Find(value).Error)
- }
- } else {
- sql := fmt.Sprintf("%v = ?", scope.Quote(toScope.PrimaryKey()))
- scope.Err(tx.Where(sql, fromField.Field.Interface()).Find(value).Error)
- }
- return scope
- } else if toField != nil {
- sql := fmt.Sprintf("%v = ?", scope.Quote(toField.DBName))
- scope.Err(tx.Where(sql, scope.PrimaryKeyValue()).Find(value).Error)
- return scope
- }
- }
- scope.Err(fmt.Errorf("invalid association %v", foreignKeys))
- return scope
- }
- func (scope *Scope) getTableOptions() string {
- tableOptions, ok := scope.Get("gorm:table_options")
- if !ok {
- return ""
- }
- return " " + tableOptions.(string)
- }
- func (scope *Scope) createJoinTable(field *StructField) {
- if relationship := field.Relationship; relationship != nil && relationship.JoinTableHandler != nil {
- joinTableHandler := relationship.JoinTableHandler
- joinTable := joinTableHandler.Table(scope.db)
- if !scope.Dialect().HasTable(joinTable) {
- toScope := &Scope{Value: reflect.New(field.Struct.Type).Interface()}
- var sqlTypes, primaryKeys []string
- for idx, fieldName := range relationship.ForeignFieldNames {
- if field, ok := scope.FieldByName(fieldName); ok {
- foreignKeyStruct := field.clone()
- foreignKeyStruct.IsPrimaryKey = false
- foreignKeyStruct.TagSettingsSet("IS_JOINTABLE_FOREIGNKEY", "true")
- foreignKeyStruct.TagSettingsDelete("AUTO_INCREMENT")
- sqlTypes = append(sqlTypes, scope.Quote(relationship.ForeignDBNames[idx])+" "+scope.Dialect().DataTypeOf(foreignKeyStruct))
- primaryKeys = append(primaryKeys, scope.Quote(relationship.ForeignDBNames[idx]))
- }
- }
- for idx, fieldName := range relationship.AssociationForeignFieldNames {
- if field, ok := toScope.FieldByName(fieldName); ok {
- foreignKeyStruct := field.clone()
- foreignKeyStruct.IsPrimaryKey = false
- foreignKeyStruct.TagSettingsSet("IS_JOINTABLE_FOREIGNKEY", "true")
- foreignKeyStruct.TagSettingsDelete("AUTO_INCREMENT")
- sqlTypes = append(sqlTypes, scope.Quote(relationship.AssociationForeignDBNames[idx])+" "+scope.Dialect().DataTypeOf(foreignKeyStruct))
- primaryKeys = append(primaryKeys, scope.Quote(relationship.AssociationForeignDBNames[idx]))
- }
- }
- scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v, PRIMARY KEY (%v))%s", scope.Quote(joinTable), strings.Join(sqlTypes, ","), strings.Join(primaryKeys, ","), scope.getTableOptions())).Error)
- }
- scope.NewDB().Table(joinTable).AutoMigrate(joinTableHandler)
- }
- }
- func (scope *Scope) createTable() *Scope {
- var tags []string
- var primaryKeys []string
- var primaryKeyInColumnType = false
- for _, field := range scope.GetModelStruct().StructFields {
- if field.IsNormal {
- sqlTag := scope.Dialect().DataTypeOf(field)
-
-
-
- if strings.Contains(strings.ToLower(sqlTag), "primary key") {
- primaryKeyInColumnType = true
- }
- tags = append(tags, scope.Quote(field.DBName)+" "+sqlTag)
- }
- if field.IsPrimaryKey {
- primaryKeys = append(primaryKeys, scope.Quote(field.DBName))
- }
- scope.createJoinTable(field)
- }
- var primaryKeyStr string
- if len(primaryKeys) > 0 && !primaryKeyInColumnType {
- primaryKeyStr = fmt.Sprintf(", PRIMARY KEY (%v)", strings.Join(primaryKeys, ","))
- }
- scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v %v)%s", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr, scope.getTableOptions())).Exec()
- scope.autoIndex()
- return scope
- }
- func (scope *Scope) dropTable() *Scope {
- scope.Raw(fmt.Sprintf("DROP TABLE %v%s", scope.QuotedTableName(), scope.getTableOptions())).Exec()
- return scope
- }
- func (scope *Scope) modifyColumn(column string, typ string) {
- scope.db.AddError(scope.Dialect().ModifyColumn(scope.QuotedTableName(), scope.Quote(column), typ))
- }
- func (scope *Scope) dropColumn(column string) {
- scope.Raw(fmt.Sprintf("ALTER TABLE %v DROP COLUMN %v", scope.QuotedTableName(), scope.Quote(column))).Exec()
- }
- func (scope *Scope) addIndex(unique bool, indexName string, column ...string) {
- if scope.Dialect().HasIndex(scope.TableName(), indexName) {
- return
- }
- var columns []string
- for _, name := range column {
- columns = append(columns, scope.quoteIfPossible(name))
- }
- sqlCreate := "CREATE INDEX"
- if unique {
- sqlCreate = "CREATE UNIQUE INDEX"
- }
- scope.Raw(fmt.Sprintf("%s %v ON %v(%v) %v", sqlCreate, indexName, scope.QuotedTableName(), strings.Join(columns, ", "), scope.whereSQL())).Exec()
- }
- func (scope *Scope) addForeignKey(field string, dest string, onDelete string, onUpdate string) {
-
- keyName := scope.Dialect().BuildKeyName(scope.TableName(), field, dest, "foreign")
- if scope.Dialect().HasForeignKey(scope.TableName(), keyName) {
- return
- }
- var query = `ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s ON DELETE %s ON UPDATE %s;`
- scope.Raw(fmt.Sprintf(query, scope.QuotedTableName(), scope.quoteIfPossible(keyName), scope.quoteIfPossible(field), dest, onDelete, onUpdate)).Exec()
- }
- func (scope *Scope) removeForeignKey(field string, dest string) {
- keyName := scope.Dialect().BuildKeyName(scope.TableName(), field, dest, "foreign")
- if !scope.Dialect().HasForeignKey(scope.TableName(), keyName) {
- return
- }
- var mysql mysql
- var query string
- if scope.Dialect().GetName() == mysql.GetName() {
- query = `ALTER TABLE %s DROP FOREIGN KEY %s;`
- } else {
- query = `ALTER TABLE %s DROP CONSTRAINT %s;`
- }
- scope.Raw(fmt.Sprintf(query, scope.QuotedTableName(), scope.quoteIfPossible(keyName))).Exec()
- }
- func (scope *Scope) removeIndex(indexName string) {
- scope.Dialect().RemoveIndex(scope.TableName(), indexName)
- }
- func (scope *Scope) autoMigrate() *Scope {
- tableName := scope.TableName()
- quotedTableName := scope.QuotedTableName()
- if !scope.Dialect().HasTable(tableName) {
- scope.createTable()
- } else {
- for _, field := range scope.GetModelStruct().StructFields {
- if !scope.Dialect().HasColumn(tableName, field.DBName) {
- if field.IsNormal {
- sqlTag := scope.Dialect().DataTypeOf(field)
- scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", quotedTableName, scope.Quote(field.DBName), sqlTag)).Exec()
- }
- }
- scope.createJoinTable(field)
- }
- scope.autoIndex()
- }
- return scope
- }
- func (scope *Scope) autoIndex() *Scope {
- var indexes = map[string][]string{}
- var uniqueIndexes = map[string][]string{}
- for _, field := range scope.GetStructFields() {
- if name, ok := field.TagSettingsGet("INDEX"); ok {
- names := strings.Split(name, ",")
- for _, name := range names {
- if name == "INDEX" || name == "" {
- name = scope.Dialect().BuildKeyName("idx", scope.TableName(), field.DBName)
- }
- indexes[name] = append(indexes[name], field.DBName)
- }
- }
- if name, ok := field.TagSettingsGet("UNIQUE_INDEX"); ok {
- names := strings.Split(name, ",")
- for _, name := range names {
- if name == "UNIQUE_INDEX" || name == "" {
- name = scope.Dialect().BuildKeyName("uix", scope.TableName(), field.DBName)
- }
- uniqueIndexes[name] = append(uniqueIndexes[name], field.DBName)
- }
- }
- }
- for name, columns := range indexes {
- if db := scope.NewDB().Table(scope.TableName()).Model(scope.Value).AddIndex(name, columns...); db.Error != nil {
- scope.db.AddError(db.Error)
- }
- }
- for name, columns := range uniqueIndexes {
- if db := scope.NewDB().Table(scope.TableName()).Model(scope.Value).AddUniqueIndex(name, columns...); db.Error != nil {
- scope.db.AddError(db.Error)
- }
- }
- return scope
- }
- func (scope *Scope) getColumnAsArray(columns []string, values ...interface{}) (results [][]interface{}) {
- resultMap := make(map[string][]interface{})
- for _, value := range values {
- indirectValue := indirect(reflect.ValueOf(value))
- switch indirectValue.Kind() {
- case reflect.Slice:
- for i := 0; i < indirectValue.Len(); i++ {
- var result []interface{}
- var object = indirect(indirectValue.Index(i))
- var hasValue = false
- for _, column := range columns {
- field := object.FieldByName(column)
- if hasValue || !isBlank(field) {
- hasValue = true
- }
- result = append(result, field.Interface())
- }
- if hasValue {
- h := fmt.Sprint(result...)
- if _, exist := resultMap[h]; !exist {
- resultMap[h] = result
- }
- }
- }
- case reflect.Struct:
- var result []interface{}
- var hasValue = false
- for _, column := range columns {
- field := indirectValue.FieldByName(column)
- if hasValue || !isBlank(field) {
- hasValue = true
- }
- result = append(result, field.Interface())
- }
- if hasValue {
- h := fmt.Sprint(result...)
- if _, exist := resultMap[h]; !exist {
- resultMap[h] = result
- }
- }
- }
- }
- for _, v := range resultMap {
- results = append(results, v)
- }
- return
- }
- func (scope *Scope) getColumnAsScope(column string) *Scope {
- indirectScopeValue := scope.IndirectValue()
- switch indirectScopeValue.Kind() {
- case reflect.Slice:
- if fieldStruct, ok := scope.GetModelStruct().ModelType.FieldByName(column); ok {
- fieldType := fieldStruct.Type
- if fieldType.Kind() == reflect.Slice || fieldType.Kind() == reflect.Ptr {
- fieldType = fieldType.Elem()
- }
- resultsMap := map[interface{}]bool{}
- results := reflect.New(reflect.SliceOf(reflect.PtrTo(fieldType))).Elem()
- for i := 0; i < indirectScopeValue.Len(); i++ {
- result := indirect(indirect(indirectScopeValue.Index(i)).FieldByName(column))
- if result.Kind() == reflect.Slice {
- for j := 0; j < result.Len(); j++ {
- if elem := result.Index(j); elem.CanAddr() && resultsMap[elem.Addr()] != true {
- resultsMap[elem.Addr()] = true
- results = reflect.Append(results, elem.Addr())
- }
- }
- } else if result.CanAddr() && resultsMap[result.Addr()] != true {
- resultsMap[result.Addr()] = true
- results = reflect.Append(results, result.Addr())
- }
- }
- return scope.New(results.Interface())
- }
- case reflect.Struct:
- if field := indirectScopeValue.FieldByName(column); field.CanAddr() {
- return scope.New(field.Addr().Interface())
- }
- }
- return nil
- }
- func (scope *Scope) hasConditions() bool {
- return !scope.PrimaryKeyZero() ||
- len(scope.Search.whereConditions) > 0 ||
- len(scope.Search.orConditions) > 0 ||
- len(scope.Search.notConditions) > 0
- }
|