Import Upstream version 1.9.6
Shengjing Zhu
1 year, 3 months ago
0 | --- | |
1 | engines: | |
2 | gofmt: | |
3 | enabled: true | |
4 | govet: | |
5 | enabled: true | |
6 | golint: | |
7 | enabled: true | |
8 | ratings: | |
9 | paths: | |
10 | - "**.go" |
0 | 0 | # GORM |
1 | 1 | |
2 | The fantastic ORM library for Golang, aims to be developer friendly. | |
2 | GORM V2 moved to https://github.com/go-gorm/gorm | |
3 | 3 | |
4 | [![go report card](https://goreportcard.com/badge/github.com/jinzhu/gorm "go report card")](https://goreportcard.com/report/github.com/jinzhu/gorm) | |
5 | [![wercker status](https://app.wercker.com/status/8596cace912c9947dd9c8542ecc8cb8b/s/master "wercker status")](https://app.wercker.com/project/byKey/8596cace912c9947dd9c8542ecc8cb8b) | |
6 | [![Join the chat at https://gitter.im/jinzhu/gorm](https://img.shields.io/gitter/room/jinzhu/gorm.svg)](https://gitter.im/jinzhu/gorm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | |
7 | [![Open Collective Backer](https://opencollective.com/gorm/tiers/backer/badge.svg?label=backer&color=brightgreen "Open Collective Backer")](https://opencollective.com/gorm) | |
8 | [![Open Collective Sponsor](https://opencollective.com/gorm/tiers/sponsor/badge.svg?label=sponsor&color=brightgreen "Open Collective Sponsor")](https://opencollective.com/gorm) | |
9 | [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT) | |
10 | [![GoDoc](https://godoc.org/github.com/jinzhu/gorm?status.svg)](https://godoc.org/github.com/jinzhu/gorm) | |
11 | ||
12 | ## Overview | |
13 | ||
14 | * Full-Featured ORM (almost) | |
15 | * Associations (Has One, Has Many, Belongs To, Many To Many, Polymorphism) | |
16 | * Hooks (Before/After Create/Save/Update/Delete/Find) | |
17 | * Preloading (eager loading) | |
18 | * Transactions | |
19 | * Composite Primary Key | |
20 | * SQL Builder | |
21 | * Auto Migrations | |
22 | * Logger | |
23 | * Extendable, write Plugins based on GORM callbacks | |
24 | * Every feature comes with tests | |
25 | * Developer Friendly | |
26 | ||
27 | ## Getting Started | |
28 | ||
29 | * GORM Guides [http://gorm.io](http://gorm.io) | |
30 | ||
31 | ## Contributing | |
32 | ||
33 | [You can help to deliver a better GORM, check out things you can do](http://gorm.io/contribute.html) | |
34 | ||
35 | ## License | |
36 | ||
37 | © Jinzhu, 2013~time.Now | |
38 | ||
39 | Released under the [MIT License](https://github.com/jinzhu/gorm/blob/master/License) | |
4 | GORM V1 Doc https://v1.gorm.io/ |
266 | 266 | query = scope.DB() |
267 | 267 | ) |
268 | 268 | |
269 | if relationship.Kind == "many_to_many" { | |
269 | switch relationship.Kind { | |
270 | case "many_to_many": | |
270 | 271 | query = relationship.JoinTableHandler.JoinWith(relationship.JoinTableHandler, query, scope.Value) |
271 | } else if relationship.Kind == "has_many" || relationship.Kind == "has_one" { | |
272 | case "has_many", "has_one": | |
272 | 273 | primaryKeys := scope.getColumnAsArray(relationship.AssociationForeignFieldNames, scope.Value) |
273 | 274 | query = query.Where( |
274 | 275 | fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.ForeignDBNames), toQueryMarks(primaryKeys)), |
275 | 276 | toQueryValues(primaryKeys)..., |
276 | 277 | ) |
277 | } else if relationship.Kind == "belongs_to" { | |
278 | case "belongs_to": | |
278 | 279 | primaryKeys := scope.getColumnAsArray(relationship.ForeignFieldNames, scope.Value) |
279 | 280 | query = query.Where( |
280 | 281 | fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.AssociationForeignDBNames), toQueryMarks(primaryKeys)), |
366 | 367 | return association |
367 | 368 | } |
368 | 369 | |
370 | // setErr set error when the error is not nil. And return Association. | |
369 | 371 | func (association *Association) setErr(err error) *Association { |
370 | 372 | if err != nil { |
371 | 373 | association.Error = err |
0 | 0 | package gorm |
1 | 1 | |
2 | import "log" | |
2 | import "fmt" | |
3 | 3 | |
4 | 4 | // DefaultCallback default callbacks defined by gorm |
5 | var DefaultCallback = &Callback{} | |
5 | var DefaultCallback = &Callback{logger: nopLogger{}} | |
6 | 6 | |
7 | 7 | // Callback is a struct that contains all CRUD callbacks |
8 | 8 | // Field `creates` contains callbacks will be call when creating object |
12 | 12 | // Field `rowQueries` contains callbacks will be call when querying object with Row, Rows... |
13 | 13 | // Field `processors` contains all callback processors, will be used to generate above callbacks in order |
14 | 14 | type Callback struct { |
15 | logger logger | |
15 | 16 | creates []*func(scope *Scope) |
16 | 17 | updates []*func(scope *Scope) |
17 | 18 | deletes []*func(scope *Scope) |
22 | 23 | |
23 | 24 | // CallbackProcessor contains callback informations |
24 | 25 | type CallbackProcessor struct { |
26 | logger logger | |
25 | 27 | name string // current callback's name |
26 | 28 | before string // register current callback before a callback |
27 | 29 | after string // register current callback after a callback |
32 | 34 | parent *Callback |
33 | 35 | } |
34 | 36 | |
35 | func (c *Callback) clone() *Callback { | |
37 | func (c *Callback) clone(logger logger) *Callback { | |
36 | 38 | return &Callback{ |
39 | logger: logger, | |
37 | 40 | creates: c.creates, |
38 | 41 | updates: c.updates, |
39 | 42 | deletes: c.deletes, |
52 | 55 | // scope.Err(errors.New("error")) |
53 | 56 | // }) |
54 | 57 | func (c *Callback) Create() *CallbackProcessor { |
55 | return &CallbackProcessor{kind: "create", parent: c} | |
58 | return &CallbackProcessor{logger: c.logger, kind: "create", parent: c} | |
56 | 59 | } |
57 | 60 | |
58 | 61 | // Update could be used to register callbacks for updating object, refer `Create` for usage |
59 | 62 | func (c *Callback) Update() *CallbackProcessor { |
60 | return &CallbackProcessor{kind: "update", parent: c} | |
63 | return &CallbackProcessor{logger: c.logger, kind: "update", parent: c} | |
61 | 64 | } |
62 | 65 | |
63 | 66 | // Delete could be used to register callbacks for deleting object, refer `Create` for usage |
64 | 67 | func (c *Callback) Delete() *CallbackProcessor { |
65 | return &CallbackProcessor{kind: "delete", parent: c} | |
68 | return &CallbackProcessor{logger: c.logger, kind: "delete", parent: c} | |
66 | 69 | } |
67 | 70 | |
68 | 71 | // Query could be used to register callbacks for querying objects with query methods like `Find`, `First`, `Related`, `Association`... |
69 | 72 | // Refer `Create` for usage |
70 | 73 | func (c *Callback) Query() *CallbackProcessor { |
71 | return &CallbackProcessor{kind: "query", parent: c} | |
74 | return &CallbackProcessor{logger: c.logger, kind: "query", parent: c} | |
72 | 75 | } |
73 | 76 | |
74 | 77 | // RowQuery could be used to register callbacks for querying objects with `Row`, `Rows`, refer `Create` for usage |
75 | 78 | func (c *Callback) RowQuery() *CallbackProcessor { |
76 | return &CallbackProcessor{kind: "row_query", parent: c} | |
79 | return &CallbackProcessor{logger: c.logger, kind: "row_query", parent: c} | |
77 | 80 | } |
78 | 81 | |
79 | 82 | // After insert a new callback after callback `callbackName`, refer `Callbacks.Create` |
92 | 95 | func (cp *CallbackProcessor) Register(callbackName string, callback func(scope *Scope)) { |
93 | 96 | if cp.kind == "row_query" { |
94 | 97 | if cp.before == "" && cp.after == "" && callbackName != "gorm:row_query" { |
95 | log.Printf("Registing RowQuery callback %v without specify order with Before(), After(), applying Before('gorm:row_query') by default for compatibility...\n", callbackName) | |
98 | cp.logger.Print("info", fmt.Sprintf("Registering RowQuery callback %v without specify order with Before(), After(), applying Before('gorm:row_query') by default for compatibility...", callbackName)) | |
96 | 99 | cp.before = "gorm:row_query" |
97 | 100 | } |
98 | 101 | } |
99 | 102 | |
103 | cp.logger.Print("info", fmt.Sprintf("[info] registering callback `%v` from %v", callbackName, fileWithLineNum())) | |
100 | 104 | cp.name = callbackName |
101 | 105 | cp.processor = &callback |
102 | 106 | cp.parent.processors = append(cp.parent.processors, cp) |
106 | 110 | // Remove a registered callback |
107 | 111 | // db.Callback().Create().Remove("gorm:update_time_stamp_when_create") |
108 | 112 | func (cp *CallbackProcessor) Remove(callbackName string) { |
109 | log.Printf("[info] removing callback `%v` from %v\n", callbackName, fileWithLineNum()) | |
113 | cp.logger.Print("info", fmt.Sprintf("[info] removing callback `%v` from %v", callbackName, fileWithLineNum())) | |
110 | 114 | cp.name = callbackName |
111 | 115 | cp.remove = true |
112 | 116 | cp.parent.processors = append(cp.parent.processors, cp) |
115 | 119 | |
116 | 120 | // Replace a registered callback with new callback |
117 | 121 | // db.Callback().Create().Replace("gorm:update_time_stamp_when_create", func(*Scope) { |
118 | // scope.SetColumn("Created", now) | |
119 | // scope.SetColumn("Updated", now) | |
122 | // scope.SetColumn("CreatedAt", now) | |
123 | // scope.SetColumn("UpdatedAt", now) | |
120 | 124 | // }) |
121 | 125 | func (cp *CallbackProcessor) Replace(callbackName string, callback func(scope *Scope)) { |
122 | log.Printf("[info] replacing callback `%v` from %v\n", callbackName, fileWithLineNum()) | |
126 | cp.logger.Print("info", fmt.Sprintf("[info] replacing callback `%v` from %v", callbackName, fileWithLineNum())) | |
123 | 127 | cp.name = callbackName |
124 | 128 | cp.processor = &callback |
125 | 129 | cp.replace = true |
131 | 135 | // db.Callback().Create().Get("gorm:create") |
132 | 136 | func (cp *CallbackProcessor) Get(callbackName string) (callback func(scope *Scope)) { |
133 | 137 | for _, p := range cp.parent.processors { |
134 | if p.name == callbackName && p.kind == cp.kind && !cp.remove { | |
135 | return *p.processor | |
136 | } | |
137 | } | |
138 | return nil | |
138 | if p.name == callbackName && p.kind == cp.kind { | |
139 | if p.remove { | |
140 | callback = nil | |
141 | } else { | |
142 | callback = *p.processor | |
143 | } | |
144 | } | |
145 | } | |
146 | return | |
139 | 147 | } |
140 | 148 | |
141 | 149 | // getRIndex get right index from string slice |
158 | 166 | for _, cp := range cps { |
159 | 167 | // show warning message the callback name already exists |
160 | 168 | if index := getRIndex(allNames, cp.name); index > -1 && !cp.replace && !cp.remove { |
161 | log.Printf("[warning] duplicated callback `%v` from %v\n", cp.name, fileWithLineNum()) | |
169 | cp.logger.Print("warning", fmt.Sprintf("[warning] duplicated callback `%v` from %v", cp.name, fileWithLineNum())) | |
162 | 170 | } |
163 | 171 | allNames = append(allNames, cp.name) |
164 | 172 | } |
30 | 30 | // updateTimeStampForCreateCallback will set `CreatedAt`, `UpdatedAt` when creating |
31 | 31 | func updateTimeStampForCreateCallback(scope *Scope) { |
32 | 32 | if !scope.HasError() { |
33 | now := NowFunc() | |
33 | now := scope.db.nowFunc() | |
34 | 34 | |
35 | 35 | if createdAtField, ok := scope.FieldByName("CreatedAt"); ok { |
36 | 36 | if createdAtField.IsBlank { |
58 | 58 | |
59 | 59 | for _, field := range scope.Fields() { |
60 | 60 | if scope.changeableField(field) { |
61 | if field.IsNormal { | |
61 | if field.IsNormal && !field.IsIgnored { | |
62 | 62 | if field.IsBlank && field.HasDefaultValue { |
63 | 63 | blankColumnsWithDefaultValue = append(blankColumnsWithDefaultValue, scope.Quote(field.DBName)) |
64 | 64 | scope.InstanceSet("gorm:blank_columns_with_default_value", blankColumnsWithDefaultValue) |
82 | 82 | quotedTableName = scope.QuotedTableName() |
83 | 83 | primaryField = scope.PrimaryField() |
84 | 84 | extraOption string |
85 | insertModifier string | |
85 | 86 | ) |
86 | 87 | |
87 | 88 | if str, ok := scope.Get("gorm:insert_option"); ok { |
88 | 89 | extraOption = fmt.Sprint(str) |
90 | } | |
91 | if str, ok := scope.Get("gorm:insert_modifier"); ok { | |
92 | insertModifier = strings.ToUpper(fmt.Sprint(str)) | |
93 | if insertModifier == "INTO" { | |
94 | insertModifier = "" | |
95 | } | |
89 | 96 | } |
90 | 97 | |
91 | 98 | if primaryField != nil { |
92 | 99 | returningColumn = scope.Quote(primaryField.DBName) |
93 | 100 | } |
94 | 101 | |
95 | lastInsertIDReturningSuffix := scope.Dialect().LastInsertIDReturningSuffix(quotedTableName, returningColumn) | |
102 | lastInsertIDOutputInterstitial := scope.Dialect().LastInsertIDOutputInterstitial(quotedTableName, returningColumn, columns) | |
103 | var lastInsertIDReturningSuffix string | |
104 | if lastInsertIDOutputInterstitial == "" { | |
105 | lastInsertIDReturningSuffix = scope.Dialect().LastInsertIDReturningSuffix(quotedTableName, returningColumn) | |
106 | } | |
96 | 107 | |
97 | 108 | if len(columns) == 0 { |
98 | 109 | scope.Raw(fmt.Sprintf( |
99 | "INSERT INTO %v %v%v%v", | |
110 | "INSERT%v INTO %v %v%v%v", | |
111 | addExtraSpaceIfExist(insertModifier), | |
100 | 112 | quotedTableName, |
101 | 113 | scope.Dialect().DefaultValueStr(), |
102 | 114 | addExtraSpaceIfExist(extraOption), |
104 | 116 | )) |
105 | 117 | } else { |
106 | 118 | scope.Raw(fmt.Sprintf( |
107 | "INSERT INTO %v (%v) VALUES (%v)%v%v", | |
119 | "INSERT%v INTO %v (%v)%v VALUES (%v)%v%v", | |
120 | addExtraSpaceIfExist(insertModifier), | |
108 | 121 | scope.QuotedTableName(), |
109 | 122 | strings.Join(columns, ","), |
123 | addExtraSpaceIfExist(lastInsertIDOutputInterstitial), | |
110 | 124 | strings.Join(placeholders, ","), |
111 | 125 | addExtraSpaceIfExist(extraOption), |
112 | 126 | addExtraSpaceIfExist(lastInsertIDReturningSuffix), |
113 | 127 | )) |
114 | 128 | } |
115 | 129 | |
116 | // execute create sql | |
117 | if lastInsertIDReturningSuffix == "" || primaryField == nil { | |
130 | // execute create sql: no primaryField | |
131 | if primaryField == nil { | |
118 | 132 | if result, err := scope.SQLDB().Exec(scope.SQL, scope.SQLVars...); scope.Err(err) == nil { |
119 | 133 | // set rows affected count |
120 | 134 | scope.db.RowsAffected, _ = result.RowsAffected() |
126 | 140 | } |
127 | 141 | } |
128 | 142 | } |
143 | return | |
144 | } | |
145 | ||
146 | // execute create sql: lastInsertID implemention for majority of dialects | |
147 | if lastInsertIDReturningSuffix == "" && lastInsertIDOutputInterstitial == "" { | |
148 | if result, err := scope.SQLDB().Exec(scope.SQL, scope.SQLVars...); scope.Err(err) == nil { | |
149 | // set rows affected count | |
150 | scope.db.RowsAffected, _ = result.RowsAffected() | |
151 | ||
152 | // set primary value to primary field | |
153 | if primaryField != nil && primaryField.IsBlank { | |
154 | if primaryValue, err := result.LastInsertId(); scope.Err(err) == nil { | |
155 | scope.Err(primaryField.Set(primaryValue)) | |
156 | } | |
157 | } | |
158 | } | |
159 | return | |
160 | } | |
161 | ||
162 | // execute create sql: dialects with additional lastInsertID requirements (currently postgres & mssql) | |
163 | if primaryField.Field.CanAddr() { | |
164 | if err := scope.SQLDB().QueryRow(scope.SQL, scope.SQLVars...).Scan(primaryField.Field.Addr().Interface()); scope.Err(err) == nil { | |
165 | primaryField.IsBlank = false | |
166 | scope.db.RowsAffected = 1 | |
167 | } | |
129 | 168 | } else { |
130 | if primaryField.Field.CanAddr() { | |
131 | if err := scope.SQLDB().QueryRow(scope.SQL, scope.SQLVars...).Scan(primaryField.Field.Addr().Interface()); scope.Err(err) == nil { | |
132 | primaryField.IsBlank = false | |
133 | scope.db.RowsAffected = 1 | |
134 | } | |
135 | } else { | |
136 | scope.Err(ErrUnaddressable) | |
137 | } | |
169 | scope.Err(ErrUnaddressable) | |
138 | 170 | } |
171 | return | |
139 | 172 | } |
140 | 173 | } |
141 | 174 |
16 | 16 | // beforeDeleteCallback will invoke `BeforeDelete` method before deleting |
17 | 17 | func beforeDeleteCallback(scope *Scope) { |
18 | 18 | if scope.DB().HasBlockGlobalUpdate() && !scope.hasConditions() { |
19 | scope.Err(errors.New("Missing WHERE clause while deleting")) | |
19 | scope.Err(errors.New("missing WHERE clause while deleting")) | |
20 | 20 | return |
21 | 21 | } |
22 | 22 | if !scope.HasError() { |
39 | 39 | "UPDATE %v SET %v=%v%v%v", |
40 | 40 | scope.QuotedTableName(), |
41 | 41 | scope.Quote(deletedAtField.DBName), |
42 | scope.AddToVars(NowFunc()), | |
42 | scope.AddToVars(scope.db.nowFunc()), | |
43 | 43 | addExtraSpaceIfExist(scope.CombinedConditionSql()), |
44 | 44 | addExtraSpaceIfExist(extraOption), |
45 | 45 | )).Exec() |
15 | 15 | // queryCallback used to query data from database |
16 | 16 | func queryCallback(scope *Scope) { |
17 | 17 | if _, skip := scope.InstanceGet("gorm:skip_query_callback"); skip { |
18 | return | |
19 | } | |
20 | ||
21 | //we are only preloading relations, dont touch base model | |
22 | if _, skip := scope.InstanceGet("gorm:only_preload"); skip { | |
18 | 23 | return |
19 | 24 | } |
20 | 25 | |
54 | 59 | |
55 | 60 | if !scope.HasError() { |
56 | 61 | scope.db.RowsAffected = 0 |
62 | ||
63 | if str, ok := scope.Get("gorm:query_hint"); ok { | |
64 | scope.SQL = fmt.Sprint(str) + scope.SQL | |
65 | } | |
66 | ||
57 | 67 | if str, ok := scope.Get("gorm:query_option"); ok { |
58 | 68 | scope.SQL += addExtraSpaceIfExist(fmt.Sprint(str)) |
59 | 69 | } |
13 | 13 | return |
14 | 14 | } |
15 | 15 | |
16 | if _, ok := scope.Get("gorm:auto_preload"); ok { | |
17 | autoPreload(scope) | |
16 | if ap, ok := scope.Get("gorm:auto_preload"); ok { | |
17 | // If gorm:auto_preload IS NOT a bool then auto preload. | |
18 | // Else if it IS a bool, use the value | |
19 | if apb, ok := ap.(bool); !ok { | |
20 | autoPreload(scope) | |
21 | } else if apb { | |
22 | autoPreload(scope) | |
23 | } | |
18 | 24 | } |
19 | 25 | |
20 | 26 | if scope.Search.preload == nil || scope.HasError() { |
93 | 99 | continue |
94 | 100 | } |
95 | 101 | |
96 | if val, ok := field.TagSettings["PRELOAD"]; ok { | |
102 | if val, ok := field.TagSettingsGet("PRELOAD"); ok { | |
97 | 103 | if preload, err := strconv.ParseBool(val); err != nil { |
98 | 104 | scope.Err(errors.New("invalid preload option")) |
99 | 105 | return |
154 | 160 | ) |
155 | 161 | |
156 | 162 | if indirectScopeValue.Kind() == reflect.Slice { |
163 | foreignValuesToResults := make(map[string]reflect.Value) | |
164 | for i := 0; i < resultsValue.Len(); i++ { | |
165 | result := resultsValue.Index(i) | |
166 | foreignValues := toString(getValueFromFields(result, relation.ForeignFieldNames)) | |
167 | foreignValuesToResults[foreignValues] = result | |
168 | } | |
157 | 169 | for j := 0; j < indirectScopeValue.Len(); j++ { |
158 | for i := 0; i < resultsValue.Len(); i++ { | |
159 | result := resultsValue.Index(i) | |
160 | foreignValues := getValueFromFields(result, relation.ForeignFieldNames) | |
161 | if indirectValue := indirect(indirectScopeValue.Index(j)); equalAsString(getValueFromFields(indirectValue, relation.AssociationForeignFieldNames), foreignValues) { | |
162 | indirectValue.FieldByName(field.Name).Set(result) | |
163 | break | |
164 | } | |
170 | indirectValue := indirect(indirectScopeValue.Index(j)) | |
171 | valueString := toString(getValueFromFields(indirectValue, relation.AssociationForeignFieldNames)) | |
172 | if result, found := foreignValuesToResults[valueString]; found { | |
173 | indirectValue.FieldByName(field.Name).Set(result) | |
165 | 174 | } |
166 | 175 | } |
167 | 176 | } else { |
248 | 257 | indirectScopeValue = scope.IndirectValue() |
249 | 258 | ) |
250 | 259 | |
260 | foreignFieldToObjects := make(map[string][]*reflect.Value) | |
261 | if indirectScopeValue.Kind() == reflect.Slice { | |
262 | for j := 0; j < indirectScopeValue.Len(); j++ { | |
263 | object := indirect(indirectScopeValue.Index(j)) | |
264 | valueString := toString(getValueFromFields(object, relation.ForeignFieldNames)) | |
265 | foreignFieldToObjects[valueString] = append(foreignFieldToObjects[valueString], &object) | |
266 | } | |
267 | } | |
268 | ||
251 | 269 | for i := 0; i < resultsValue.Len(); i++ { |
252 | 270 | result := resultsValue.Index(i) |
253 | 271 | if indirectScopeValue.Kind() == reflect.Slice { |
254 | value := getValueFromFields(result, relation.AssociationForeignFieldNames) | |
255 | for j := 0; j < indirectScopeValue.Len(); j++ { | |
256 | object := indirect(indirectScopeValue.Index(j)) | |
257 | if equalAsString(getValueFromFields(object, relation.ForeignFieldNames), value) { | |
272 | valueString := toString(getValueFromFields(result, relation.AssociationForeignFieldNames)) | |
273 | if objects, found := foreignFieldToObjects[valueString]; found { | |
274 | for _, object := range objects { | |
258 | 275 | object.FieldByName(field.Name).Set(result) |
259 | 276 | } |
260 | 277 | } |
373 | 390 | key := toString(getValueFromFields(indirectScopeValue, foreignFieldNames)) |
374 | 391 | fieldsSourceMap[key] = append(fieldsSourceMap[key], indirectScopeValue.FieldByName(field.Name)) |
375 | 392 | } |
376 | for source, link := range linkHash { | |
377 | for i, field := range fieldsSourceMap[source] { | |
393 | ||
394 | for source, fields := range fieldsSourceMap { | |
395 | for _, f := range fields { | |
378 | 396 | //If not 0 this means Value is a pointer and we already added preloaded models to it |
379 | if fieldsSourceMap[source][i].Len() != 0 { | |
397 | if f.Len() != 0 { | |
380 | 398 | continue |
381 | 399 | } |
382 | field.Set(reflect.Append(fieldsSourceMap[source][i], link...)) | |
383 | } | |
384 | ||
385 | } | |
386 | } | |
400 | ||
401 | v := reflect.MakeSlice(f.Type(), 0, 0) | |
402 | if len(linkHash[source]) > 0 { | |
403 | v = reflect.Append(f, linkHash[source]...) | |
404 | } | |
405 | ||
406 | f.Set(v) | |
407 | } | |
408 | } | |
409 | } |
0 | 0 | package gorm |
1 | 1 | |
2 | import "database/sql" | |
2 | import ( | |
3 | "database/sql" | |
4 | "fmt" | |
5 | ) | |
3 | 6 | |
4 | 7 | // Define callbacks for row query |
5 | 8 | func init() { |
20 | 23 | if result, ok := scope.InstanceGet("row_query_result"); ok { |
21 | 24 | scope.prepareQuerySQL() |
22 | 25 | |
26 | if str, ok := scope.Get("gorm:query_hint"); ok { | |
27 | scope.SQL = fmt.Sprint(str) + scope.SQL | |
28 | } | |
29 | ||
30 | if str, ok := scope.Get("gorm:query_option"); ok { | |
31 | scope.SQL += addExtraSpaceIfExist(fmt.Sprint(str)) | |
32 | } | |
33 | ||
23 | 34 | if rowResult, ok := result.(*RowQueryResult); ok { |
24 | 35 | rowResult.Row = scope.SQLDB().QueryRow(scope.SQL, scope.SQLVars...) |
25 | 36 | } else if rowsResult, ok := result.(*RowsQueryResult); ok { |
20 | 20 | |
21 | 21 | if v, ok := value.(string); ok { |
22 | 22 | v = strings.ToLower(v) |
23 | if v == "false" || v != "skip" { | |
24 | return false | |
25 | } | |
23 | return v == "true" | |
26 | 24 | } |
27 | 25 | |
28 | 26 | return true |
35 | 33 | if value, ok := scope.Get("gorm:save_associations"); ok { |
36 | 34 | autoUpdate = checkTruth(value) |
37 | 35 | autoCreate = autoUpdate |
38 | } else if value, ok := field.TagSettings["SAVE_ASSOCIATIONS"]; ok { | |
36 | saveReference = autoUpdate | |
37 | } else if value, ok := field.TagSettingsGet("SAVE_ASSOCIATIONS"); ok { | |
39 | 38 | autoUpdate = checkTruth(value) |
40 | 39 | autoCreate = autoUpdate |
40 | saveReference = autoUpdate | |
41 | 41 | } |
42 | 42 | |
43 | 43 | if value, ok := scope.Get("gorm:association_autoupdate"); ok { |
44 | 44 | autoUpdate = checkTruth(value) |
45 | } else if value, ok := field.TagSettings["ASSOCIATION_AUTOUPDATE"]; ok { | |
45 | } else if value, ok := field.TagSettingsGet("ASSOCIATION_AUTOUPDATE"); ok { | |
46 | 46 | autoUpdate = checkTruth(value) |
47 | 47 | } |
48 | 48 | |
49 | 49 | if value, ok := scope.Get("gorm:association_autocreate"); ok { |
50 | 50 | autoCreate = checkTruth(value) |
51 | } else if value, ok := field.TagSettings["ASSOCIATION_AUTOCREATE"]; ok { | |
51 | } else if value, ok := field.TagSettingsGet("ASSOCIATION_AUTOCREATE"); ok { | |
52 | 52 | autoCreate = checkTruth(value) |
53 | 53 | } |
54 | 54 | |
55 | 55 | if value, ok := scope.Get("gorm:association_save_reference"); ok { |
56 | 56 | saveReference = checkTruth(value) |
57 | } else if value, ok := field.TagSettings["ASSOCIATION_SAVE_REFERENCE"]; ok { | |
57 | } else if value, ok := field.TagSettingsGet("ASSOCIATION_SAVE_REFERENCE"); ok { | |
58 | 58 | saveReference = checkTruth(value) |
59 | 59 | } |
60 | 60 | } |
22 | 22 | func afterCreate2(s *Scope) {} |
23 | 23 | |
24 | 24 | func TestRegisterCallback(t *testing.T) { |
25 | var callback = &Callback{} | |
25 | var callback = &Callback{logger: defaultLogger} | |
26 | 26 | |
27 | 27 | callback.Create().Register("before_create1", beforeCreate1) |
28 | 28 | callback.Create().Register("before_create2", beforeCreate2) |
36 | 36 | } |
37 | 37 | |
38 | 38 | func TestRegisterCallbackWithOrder(t *testing.T) { |
39 | var callback1 = &Callback{} | |
39 | var callback1 = &Callback{logger: defaultLogger} | |
40 | 40 | callback1.Create().Register("before_create1", beforeCreate1) |
41 | 41 | callback1.Create().Register("create", create) |
42 | 42 | callback1.Create().Register("after_create1", afterCreate1) |
45 | 45 | t.Errorf("register callback with order") |
46 | 46 | } |
47 | 47 | |
48 | var callback2 = &Callback{} | |
48 | var callback2 = &Callback{logger: defaultLogger} | |
49 | 49 | |
50 | 50 | callback2.Update().Register("create", create) |
51 | 51 | callback2.Update().Before("create").Register("before_create1", beforeCreate1) |
59 | 59 | } |
60 | 60 | |
61 | 61 | func TestRegisterCallbackWithComplexOrder(t *testing.T) { |
62 | var callback1 = &Callback{} | |
62 | var callback1 = &Callback{logger: defaultLogger} | |
63 | 63 | |
64 | 64 | callback1.Query().Before("after_create1").After("before_create1").Register("create", create) |
65 | 65 | callback1.Query().Register("before_create1", beforeCreate1) |
69 | 69 | t.Errorf("register callback with order") |
70 | 70 | } |
71 | 71 | |
72 | var callback2 = &Callback{} | |
72 | var callback2 = &Callback{logger: defaultLogger} | |
73 | 73 | |
74 | 74 | callback2.Delete().Before("after_create1").After("before_create1").Register("create", create) |
75 | 75 | callback2.Delete().Before("create").Register("before_create1", beforeCreate1) |
85 | 85 | func replaceCreate(s *Scope) {} |
86 | 86 | |
87 | 87 | func TestReplaceCallback(t *testing.T) { |
88 | var callback = &Callback{} | |
88 | var callback = &Callback{logger: defaultLogger} | |
89 | 89 | |
90 | 90 | callback.Create().Before("after_create1").After("before_create1").Register("create", create) |
91 | 91 | callback.Create().Register("before_create1", beforeCreate1) |
98 | 98 | } |
99 | 99 | |
100 | 100 | func TestRemoveCallback(t *testing.T) { |
101 | var callback = &Callback{} | |
101 | var callback = &Callback{logger: defaultLogger} | |
102 | 102 | |
103 | 103 | callback.Create().Before("after_create1").After("before_create1").Register("create", create) |
104 | 104 | callback.Create().Register("before_create1", beforeCreate1) |
33 | 33 | // beforeUpdateCallback will invoke `BeforeSave`, `BeforeUpdate` method before updating |
34 | 34 | func beforeUpdateCallback(scope *Scope) { |
35 | 35 | if scope.DB().HasBlockGlobalUpdate() && !scope.hasConditions() { |
36 | scope.Err(errors.New("Missing WHERE clause while updating")) | |
36 | scope.Err(errors.New("missing WHERE clause while updating")) | |
37 | 37 | return |
38 | 38 | } |
39 | 39 | if _, ok := scope.Get("gorm:update_column"); !ok { |
49 | 49 | // updateTimeStampForUpdateCallback will set `UpdatedAt` when updating |
50 | 50 | func updateTimeStampForUpdateCallback(scope *Scope) { |
51 | 51 | if _, ok := scope.Get("gorm:update_column"); !ok { |
52 | scope.SetColumn("UpdatedAt", NowFunc()) | |
52 | scope.SetColumn("UpdatedAt", scope.db.nowFunc()) | |
53 | 53 | } |
54 | 54 | } |
55 | 55 | |
74 | 74 | } else { |
75 | 75 | for _, field := range scope.Fields() { |
76 | 76 | if scope.changeableField(field) { |
77 | if !field.IsPrimaryKey && field.IsNormal { | |
78 | sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) | |
77 | if !field.IsPrimaryKey && field.IsNormal && (field.Name != "CreatedAt" || !field.IsBlank) { | |
78 | if !field.IsForeignKey || !field.IsBlank || !field.HasDefaultValue { | |
79 | sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) | |
80 | } | |
79 | 81 | } else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" { |
80 | 82 | for _, foreignKey := range relationship.ForeignDBNames { |
81 | 83 | if foreignField, ok := scope.FieldByName(foreignKey); ok && !scope.changeableField(foreignField) { |
1 | 1 | |
2 | 2 | import ( |
3 | 3 | "errors" |
4 | ||
5 | "github.com/jinzhu/gorm" | |
6 | ||
7 | 4 | "reflect" |
8 | 5 | "testing" |
6 | ||
7 | "github.com/jinzhu/gorm" | |
9 | 8 | ) |
10 | 9 | |
11 | 10 | func (s *Product) BeforeCreate() (err error) { |
174 | 173 | t.Errorf("Record shouldn't be deleted because of an error happened in after delete callback") |
175 | 174 | } |
176 | 175 | } |
176 | ||
177 | func TestGetCallback(t *testing.T) { | |
178 | scope := DB.NewScope(nil) | |
179 | ||
180 | if DB.Callback().Create().Get("gorm:test_callback") != nil { | |
181 | t.Errorf("`gorm:test_callback` should be nil") | |
182 | } | |
183 | ||
184 | DB.Callback().Create().Register("gorm:test_callback", func(scope *gorm.Scope) { scope.Set("gorm:test_callback_value", 1) }) | |
185 | callback := DB.Callback().Create().Get("gorm:test_callback") | |
186 | if callback == nil { | |
187 | t.Errorf("`gorm:test_callback` should be non-nil") | |
188 | } | |
189 | callback(scope) | |
190 | if v, ok := scope.Get("gorm:test_callback_value"); !ok || v != 1 { | |
191 | t.Errorf("`gorm:test_callback_value` should be `1, true` but `%v, %v`", v, ok) | |
192 | } | |
193 | ||
194 | DB.Callback().Create().Replace("gorm:test_callback", func(scope *gorm.Scope) { scope.Set("gorm:test_callback_value", 2) }) | |
195 | callback = DB.Callback().Create().Get("gorm:test_callback") | |
196 | if callback == nil { | |
197 | t.Errorf("`gorm:test_callback` should be non-nil") | |
198 | } | |
199 | callback(scope) | |
200 | if v, ok := scope.Get("gorm:test_callback_value"); !ok || v != 2 { | |
201 | t.Errorf("`gorm:test_callback_value` should be `2, true` but `%v, %v`", v, ok) | |
202 | } | |
203 | ||
204 | DB.Callback().Create().Remove("gorm:test_callback") | |
205 | if DB.Callback().Create().Get("gorm:test_callback") != nil { | |
206 | t.Errorf("`gorm:test_callback` should be nil") | |
207 | } | |
208 | ||
209 | DB.Callback().Create().Register("gorm:test_callback", func(scope *gorm.Scope) { scope.Set("gorm:test_callback_value", 3) }) | |
210 | callback = DB.Callback().Create().Get("gorm:test_callback") | |
211 | if callback == nil { | |
212 | t.Errorf("`gorm:test_callback` should be non-nil") | |
213 | } | |
214 | callback(scope) | |
215 | if v, ok := scope.Get("gorm:test_callback_value"); !ok || v != 3 { | |
216 | t.Errorf("`gorm:test_callback_value` should be `3, true` but `%v, %v`", v, ok) | |
217 | } | |
218 | } | |
219 | ||
220 | func TestUseDefaultCallback(t *testing.T) { | |
221 | createCallbackName := "gorm:test_use_default_callback_for_create" | |
222 | gorm.DefaultCallback.Create().Register(createCallbackName, func(*gorm.Scope) { | |
223 | // nop | |
224 | }) | |
225 | if gorm.DefaultCallback.Create().Get(createCallbackName) == nil { | |
226 | t.Errorf("`%s` expected non-nil, but got nil", createCallbackName) | |
227 | } | |
228 | gorm.DefaultCallback.Create().Remove(createCallbackName) | |
229 | if gorm.DefaultCallback.Create().Get(createCallbackName) != nil { | |
230 | t.Errorf("`%s` expected nil, but got non-nil", createCallbackName) | |
231 | } | |
232 | ||
233 | updateCallbackName := "gorm:test_use_default_callback_for_update" | |
234 | scopeValueName := "gorm:test_use_default_callback_for_update_value" | |
235 | gorm.DefaultCallback.Update().Register(updateCallbackName, func(scope *gorm.Scope) { | |
236 | scope.Set(scopeValueName, 1) | |
237 | }) | |
238 | gorm.DefaultCallback.Update().Replace(updateCallbackName, func(scope *gorm.Scope) { | |
239 | scope.Set(scopeValueName, 2) | |
240 | }) | |
241 | ||
242 | scope := DB.NewScope(nil) | |
243 | callback := gorm.DefaultCallback.Update().Get(updateCallbackName) | |
244 | callback(scope) | |
245 | if v, ok := scope.Get(scopeValueName); !ok || v != 2 { | |
246 | t.Errorf("`%s` should be `2, true` but `%v, %v`", scopeValueName, v, ok) | |
247 | } | |
248 | } |
100 | 100 | } |
101 | 101 | } |
102 | 102 | |
103 | func TestCreateWithNowFuncOverride(t *testing.T) { | |
104 | user1 := User{Name: "CreateUserTimestampOverride"} | |
105 | ||
106 | timeA := now.MustParse("2016-01-01") | |
107 | ||
108 | // do DB.New() because we don't want this test to affect other tests | |
109 | db1 := DB.New() | |
110 | // set the override to use static timeA | |
111 | db1.SetNowFuncOverride(func() time.Time { | |
112 | return timeA | |
113 | }) | |
114 | // call .New again to check the override is carried over as well during clone | |
115 | db1 = db1.New() | |
116 | ||
117 | db1.Save(&user1) | |
118 | ||
119 | if user1.CreatedAt.UTC().Format(time.RFC3339) != timeA.UTC().Format(time.RFC3339) { | |
120 | t.Errorf("CreatedAt be using the nowFuncOverride") | |
121 | } | |
122 | if user1.UpdatedAt.UTC().Format(time.RFC3339) != timeA.UTC().Format(time.RFC3339) { | |
123 | t.Errorf("UpdatedAt be using the nowFuncOverride") | |
124 | } | |
125 | ||
126 | // now create another user with a fresh DB.Now() that doesn't have the nowFuncOverride set | |
127 | // to make sure that setting it only affected the above instance | |
128 | ||
129 | user2 := User{Name: "CreateUserTimestampOverrideNoMore"} | |
130 | ||
131 | db2 := DB.New() | |
132 | ||
133 | db2.Save(&user2) | |
134 | ||
135 | if user2.CreatedAt.UTC().Format(time.RFC3339) == timeA.UTC().Format(time.RFC3339) { | |
136 | t.Errorf("CreatedAt no longer be using the nowFuncOverride") | |
137 | } | |
138 | if user2.UpdatedAt.UTC().Format(time.RFC3339) == timeA.UTC().Format(time.RFC3339) { | |
139 | t.Errorf("UpdatedAt no longer be using the nowFuncOverride") | |
140 | } | |
141 | } | |
142 | ||
103 | 143 | type AutoIncrementUser struct { |
104 | 144 | User |
105 | 145 | Sequence uint `gorm:"AUTO_INCREMENT"` |
228 | 268 | t.Errorf("Should not create omitted relationships") |
229 | 269 | } |
230 | 270 | } |
271 | ||
272 | func TestCreateIgnore(t *testing.T) { | |
273 | float := 35.03554004971999 | |
274 | now := time.Now() | |
275 | user := User{Name: "CreateUser", Age: 18, Birthday: &now, UserNum: Num(111), PasswordHash: []byte{'f', 'a', 'k', '4'}, Latitude: float} | |
276 | ||
277 | if !DB.NewRecord(user) || !DB.NewRecord(&user) { | |
278 | t.Error("User should be new record before create") | |
279 | } | |
280 | ||
281 | if count := DB.Create(&user).RowsAffected; count != 1 { | |
282 | t.Error("There should be one record be affected when create record") | |
283 | } | |
284 | if DB.Dialect().GetName() == "mysql" && DB.Set("gorm:insert_modifier", "IGNORE").Create(&user).Error != nil { | |
285 | t.Error("Should ignore duplicate user insert by insert modifier:IGNORE ") | |
286 | } | |
287 | } |
288 | 288 | func TestSelfReferencingMany2ManyColumn(t *testing.T) { |
289 | 289 | DB.DropTable(&SelfReferencingUser{}, "UserFriends") |
290 | 290 | DB.AutoMigrate(&SelfReferencingUser{}) |
291 | if !DB.HasTable("UserFriends") { | |
292 | t.Errorf("auto migrate error, table UserFriends should be created") | |
293 | } | |
291 | 294 | |
292 | 295 | friend1 := SelfReferencingUser{Name: "friend1_m2m"} |
293 | 296 | if err := DB.Create(&friend1).Error; err != nil { |
310 | 313 | |
311 | 314 | if DB.Model(&user).Association("Friends").Count() != 2 { |
312 | 315 | t.Errorf("Should find created friends correctly") |
316 | } | |
317 | ||
318 | var count int | |
319 | if err := DB.Table("UserFriends").Count(&count).Error; err != nil { | |
320 | t.Errorf("no error should happen, but got %v", err) | |
321 | } | |
322 | if count == 0 { | |
323 | t.Errorf("table UserFriends should have records") | |
313 | 324 | } |
314 | 325 | |
315 | 326 | var newUser = SelfReferencingUser{} |
36 | 36 | ModifyColumn(tableName string, columnName string, typ string) error |
37 | 37 | |
38 | 38 | // LimitAndOffsetSQL return generated SQL with Limit and Offset, as mssql has special case |
39 | LimitAndOffsetSQL(limit, offset interface{}) string | |
39 | LimitAndOffsetSQL(limit, offset interface{}) (string, error) | |
40 | 40 | // SelectFromDummyTable return select values, for most dbs, `SELECT values` just works, mysql needs `SELECT value FROM DUAL` |
41 | 41 | SelectFromDummyTable() string |
42 | // LastInsertIDOutputInterstitial most dbs support LastInsertId, but mssql needs to use `OUTPUT` | |
43 | LastInsertIDOutputInterstitial(tableName, columnName string, columns []string) string | |
42 | 44 | // LastInsertIdReturningSuffix most dbs support LastInsertId, but postgres needs to use `RETURNING` |
43 | 45 | LastInsertIDReturningSuffix(tableName, columnName string) string |
44 | 46 | // DefaultValueStr |
46 | 48 | |
47 | 49 | // BuildKeyName returns a valid key name (foreign key, index key) for the given table, field and reference |
48 | 50 | BuildKeyName(kind, tableName string, fields ...string) string |
51 | ||
52 | // NormalizeIndexAndColumn returns valid index name and column name depending on each dialect | |
53 | NormalizeIndexAndColumn(indexName, columnName string) (string, string) | |
49 | 54 | |
50 | 55 | // CurrentDatabase return current database name |
51 | 56 | CurrentDatabase() string |
71 | 76 | dialectsMap[name] = dialect |
72 | 77 | } |
73 | 78 | |
79 | // GetDialect gets the dialect for the specified dialect name | |
80 | func GetDialect(name string) (dialect Dialect, ok bool) { | |
81 | dialect, ok = dialectsMap[name] | |
82 | return | |
83 | } | |
84 | ||
74 | 85 | // ParseFieldStructForDialect get field's sql data type |
75 | 86 | var ParseFieldStructForDialect = func(field *StructField, dialect Dialect) (fieldValue reflect.Value, sqlType string, size int, additionalType string) { |
76 | 87 | // Get redirected field type |
77 | 88 | var ( |
78 | 89 | reflectType = field.Struct.Type |
79 | dataType = field.TagSettings["TYPE"] | |
90 | dataType, _ = field.TagSettingsGet("TYPE") | |
80 | 91 | ) |
81 | 92 | |
82 | 93 | for reflectType.Kind() == reflect.Ptr { |
105 | 116 | } |
106 | 117 | |
107 | 118 | // Default Size |
108 | if num, ok := field.TagSettings["SIZE"]; ok { | |
119 | if num, ok := field.TagSettingsGet("SIZE"); ok { | |
109 | 120 | size, _ = strconv.Atoi(num) |
110 | 121 | } else { |
111 | 122 | size = 255 |
112 | 123 | } |
113 | 124 | |
114 | 125 | // Default type from tag setting |
115 | additionalType = field.TagSettings["NOT NULL"] + " " + field.TagSettings["UNIQUE"] | |
116 | if value, ok := field.TagSettings["DEFAULT"]; ok { | |
126 | notNull, _ := field.TagSettingsGet("NOT NULL") | |
127 | unique, _ := field.TagSettingsGet("UNIQUE") | |
128 | additionalType = notNull + " " + unique | |
129 | if value, ok := field.TagSettingsGet("DEFAULT"); ok { | |
117 | 130 | additionalType = additionalType + " DEFAULT " + value |
131 | } | |
132 | ||
133 | if value, ok := field.TagSettingsGet("COMMENT"); ok { | |
134 | additionalType = additionalType + " COMMENT " + value | |
118 | 135 | } |
119 | 136 | |
120 | 137 | return fieldValue, dataType, size, strings.TrimSpace(additionalType) |
7 | 7 | "strings" |
8 | 8 | "time" |
9 | 9 | ) |
10 | ||
11 | var keyNameRegex = regexp.MustCompile("[^a-zA-Z0-9]+") | |
10 | 12 | |
11 | 13 | // DefaultForeignKeyNamer contains the default foreign key name generator method |
12 | 14 | type DefaultForeignKeyNamer struct { |
38 | 40 | } |
39 | 41 | |
40 | 42 | func (s *commonDialect) fieldCanAutoIncrement(field *StructField) bool { |
41 | if value, ok := field.TagSettings["AUTO_INCREMENT"]; ok { | |
43 | if value, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok { | |
42 | 44 | return strings.ToLower(value) != "false" |
43 | 45 | } |
44 | 46 | return field.IsPrimaryKey |
136 | 138 | return |
137 | 139 | } |
138 | 140 | |
139 | func (commonDialect) LimitAndOffsetSQL(limit, offset interface{}) (sql string) { | |
141 | // LimitAndOffsetSQL return generated SQL with Limit and Offset | |
142 | func (s commonDialect) LimitAndOffsetSQL(limit, offset interface{}) (sql string, err error) { | |
140 | 143 | if limit != nil { |
141 | if parsedLimit, err := strconv.ParseInt(fmt.Sprint(limit), 0, 0); err == nil && parsedLimit >= 0 { | |
144 | if parsedLimit, err := s.parseInt(limit); err != nil { | |
145 | return "", err | |
146 | } else if parsedLimit >= 0 { | |
142 | 147 | sql += fmt.Sprintf(" LIMIT %d", parsedLimit) |
143 | 148 | } |
144 | 149 | } |
145 | 150 | if offset != nil { |
146 | if parsedOffset, err := strconv.ParseInt(fmt.Sprint(offset), 0, 0); err == nil && parsedOffset >= 0 { | |
151 | if parsedOffset, err := s.parseInt(offset); err != nil { | |
152 | return "", err | |
153 | } else if parsedOffset >= 0 { | |
147 | 154 | sql += fmt.Sprintf(" OFFSET %d", parsedOffset) |
148 | 155 | } |
149 | 156 | } |
151 | 158 | } |
152 | 159 | |
153 | 160 | func (commonDialect) SelectFromDummyTable() string { |
161 | return "" | |
162 | } | |
163 | ||
164 | func (commonDialect) LastInsertIDOutputInterstitial(tableName, columnName string, columns []string) string { | |
154 | 165 | return "" |
155 | 166 | } |
156 | 167 | |
165 | 176 | // BuildKeyName returns a valid key name (foreign key, index key) for the given table, field and reference |
166 | 177 | func (DefaultForeignKeyNamer) BuildKeyName(kind, tableName string, fields ...string) string { |
167 | 178 | keyName := fmt.Sprintf("%s_%s_%s", kind, tableName, strings.Join(fields, "_")) |
168 | keyName = regexp.MustCompile("[^a-zA-Z0-9]+").ReplaceAllString(keyName, "_") | |
179 | keyName = keyNameRegex.ReplaceAllString(keyName, "_") | |
169 | 180 | return keyName |
181 | } | |
182 | ||
183 | // NormalizeIndexAndColumn returns argument's index name and column name without doing anything | |
184 | func (commonDialect) NormalizeIndexAndColumn(indexName, columnName string) (string, string) { | |
185 | return indexName, columnName | |
186 | } | |
187 | ||
188 | func (commonDialect) parseInt(value interface{}) (int64, error) { | |
189 | return strconv.ParseInt(fmt.Sprint(value), 0, 0) | |
170 | 190 | } |
171 | 191 | |
172 | 192 | // IsByteArrayOrSlice returns true of the reflected value is an array or slice |
1 | 1 | |
2 | 2 | import ( |
3 | 3 | "crypto/sha1" |
4 | "database/sql" | |
4 | 5 | "fmt" |
5 | 6 | "reflect" |
6 | 7 | "regexp" |
7 | "strconv" | |
8 | 8 | "strings" |
9 | 9 | "time" |
10 | 10 | "unicode/utf8" |
11 | 11 | ) |
12 | 12 | |
13 | var mysqlIndexRegex = regexp.MustCompile(`^(.+)\((\d+)\)$`) | |
14 | ||
13 | 15 | type mysql struct { |
14 | 16 | commonDialect |
15 | 17 | } |
32 | 34 | |
33 | 35 | // MySQL allows only one auto increment column per table, and it must |
34 | 36 | // be a KEY column. |
35 | if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok { | |
36 | if _, ok = field.TagSettings["INDEX"]; !ok && !field.IsPrimaryKey { | |
37 | delete(field.TagSettings, "AUTO_INCREMENT") | |
37 | if _, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok { | |
38 | if _, ok = field.TagSettingsGet("INDEX"); !ok && !field.IsPrimaryKey { | |
39 | field.TagSettingsDelete("AUTO_INCREMENT") | |
38 | 40 | } |
39 | 41 | } |
40 | 42 | |
44 | 46 | sqlType = "boolean" |
45 | 47 | case reflect.Int8: |
46 | 48 | if s.fieldCanAutoIncrement(field) { |
47 | field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" | |
49 | field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT") | |
48 | 50 | sqlType = "tinyint AUTO_INCREMENT" |
49 | 51 | } else { |
50 | 52 | sqlType = "tinyint" |
51 | 53 | } |
52 | 54 | case reflect.Int, reflect.Int16, reflect.Int32: |
53 | 55 | if s.fieldCanAutoIncrement(field) { |
54 | field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" | |
56 | field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT") | |
55 | 57 | sqlType = "int AUTO_INCREMENT" |
56 | 58 | } else { |
57 | 59 | sqlType = "int" |
58 | 60 | } |
59 | 61 | case reflect.Uint8: |
60 | 62 | if s.fieldCanAutoIncrement(field) { |
61 | field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" | |
63 | field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT") | |
62 | 64 | sqlType = "tinyint unsigned AUTO_INCREMENT" |
63 | 65 | } else { |
64 | 66 | sqlType = "tinyint unsigned" |
65 | 67 | } |
66 | 68 | case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uintptr: |
67 | 69 | if s.fieldCanAutoIncrement(field) { |
68 | field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" | |
70 | field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT") | |
69 | 71 | sqlType = "int unsigned AUTO_INCREMENT" |
70 | 72 | } else { |
71 | 73 | sqlType = "int unsigned" |
72 | 74 | } |
73 | 75 | case reflect.Int64: |
74 | 76 | if s.fieldCanAutoIncrement(field) { |
75 | field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" | |
77 | field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT") | |
76 | 78 | sqlType = "bigint AUTO_INCREMENT" |
77 | 79 | } else { |
78 | 80 | sqlType = "bigint" |
79 | 81 | } |
80 | 82 | case reflect.Uint64: |
81 | 83 | if s.fieldCanAutoIncrement(field) { |
82 | field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" | |
84 | field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT") | |
83 | 85 | sqlType = "bigint unsigned AUTO_INCREMENT" |
84 | 86 | } else { |
85 | 87 | sqlType = "bigint unsigned" |
95 | 97 | case reflect.Struct: |
96 | 98 | if _, ok := dataValue.Interface().(time.Time); ok { |
97 | 99 | precision := "" |
98 | if p, ok := field.TagSettings["PRECISION"]; ok { | |
100 | if p, ok := field.TagSettingsGet("PRECISION"); ok { | |
99 | 101 | precision = fmt.Sprintf("(%s)", p) |
100 | 102 | } |
101 | 103 | |
102 | if _, ok := field.TagSettings["NOT NULL"]; ok { | |
103 | sqlType = fmt.Sprintf("timestamp%v", precision) | |
104 | if _, ok := field.TagSettings["NOT NULL"]; ok || field.IsPrimaryKey { | |
105 | sqlType = fmt.Sprintf("DATETIME%v", precision) | |
104 | 106 | } else { |
105 | sqlType = fmt.Sprintf("timestamp%v NULL", precision) | |
107 | sqlType = fmt.Sprintf("DATETIME%v NULL", precision) | |
106 | 108 | } |
107 | 109 | } |
108 | 110 | default: |
117 | 119 | } |
118 | 120 | |
119 | 121 | if sqlType == "" { |
120 | panic(fmt.Sprintf("invalid sql type %s (%s) for mysql", dataValue.Type().Name(), dataValue.Kind().String())) | |
122 | panic(fmt.Sprintf("invalid sql type %s (%s) in field %s for mysql", dataValue.Type().Name(), dataValue.Kind().String(), field.Name)) | |
121 | 123 | } |
122 | 124 | |
123 | 125 | if strings.TrimSpace(additionalType) == "" { |
136 | 138 | return err |
137 | 139 | } |
138 | 140 | |
139 | func (s mysql) LimitAndOffsetSQL(limit, offset interface{}) (sql string) { | |
141 | func (s mysql) LimitAndOffsetSQL(limit, offset interface{}) (sql string, err error) { | |
140 | 142 | if limit != nil { |
141 | if parsedLimit, err := strconv.ParseInt(fmt.Sprint(limit), 0, 0); err == nil && parsedLimit >= 0 { | |
143 | parsedLimit, err := s.parseInt(limit) | |
144 | if err != nil { | |
145 | return "", err | |
146 | } | |
147 | if parsedLimit >= 0 { | |
142 | 148 | sql += fmt.Sprintf(" LIMIT %d", parsedLimit) |
143 | 149 | |
144 | 150 | if offset != nil { |
145 | if parsedOffset, err := strconv.ParseInt(fmt.Sprint(offset), 0, 0); err == nil && parsedOffset >= 0 { | |
151 | parsedOffset, err := s.parseInt(offset) | |
152 | if err != nil { | |
153 | return "", err | |
154 | } | |
155 | if parsedOffset >= 0 { | |
146 | 156 | sql += fmt.Sprintf(" OFFSET %d", parsedOffset) |
147 | 157 | } |
148 | 158 | } |
156 | 166 | currentDatabase, tableName := currentDatabaseAndTable(&s, tableName) |
157 | 167 | s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA=? AND TABLE_NAME=? AND CONSTRAINT_NAME=? AND CONSTRAINT_TYPE='FOREIGN KEY'", currentDatabase, tableName, foreignKeyName).Scan(&count) |
158 | 168 | return count > 0 |
169 | } | |
170 | ||
171 | func (s mysql) HasTable(tableName string) bool { | |
172 | currentDatabase, tableName := currentDatabaseAndTable(&s, tableName) | |
173 | var name string | |
174 | // allow mysql database name with '-' character | |
175 | if err := s.db.QueryRow(fmt.Sprintf("SHOW TABLES FROM `%s` WHERE `Tables_in_%s` = ?", currentDatabase, currentDatabase), tableName).Scan(&name); err != nil { | |
176 | if err == sql.ErrNoRows { | |
177 | return false | |
178 | } | |
179 | panic(err) | |
180 | } else { | |
181 | return true | |
182 | } | |
183 | } | |
184 | ||
185 | func (s mysql) HasIndex(tableName string, indexName string) bool { | |
186 | currentDatabase, tableName := currentDatabaseAndTable(&s, tableName) | |
187 | if rows, err := s.db.Query(fmt.Sprintf("SHOW INDEXES FROM `%s` FROM `%s` WHERE Key_name = ?", tableName, currentDatabase), indexName); err != nil { | |
188 | panic(err) | |
189 | } else { | |
190 | defer rows.Close() | |
191 | return rows.Next() | |
192 | } | |
193 | } | |
194 | ||
195 | func (s mysql) HasColumn(tableName string, columnName string) bool { | |
196 | currentDatabase, tableName := currentDatabaseAndTable(&s, tableName) | |
197 | if rows, err := s.db.Query(fmt.Sprintf("SHOW COLUMNS FROM `%s` FROM `%s` WHERE Field = ?", tableName, currentDatabase), columnName); err != nil { | |
198 | panic(err) | |
199 | } else { | |
200 | defer rows.Close() | |
201 | return rows.Next() | |
202 | } | |
159 | 203 | } |
160 | 204 | |
161 | 205 | func (s mysql) CurrentDatabase() (name string) { |
177 | 221 | bs := h.Sum(nil) |
178 | 222 | |
179 | 223 | // sha1 is 40 characters, keep first 24 characters of destination |
180 | destRunes := []rune(regexp.MustCompile("[^a-zA-Z0-9]+").ReplaceAllString(fields[0], "_")) | |
224 | destRunes := []rune(keyNameRegex.ReplaceAllString(fields[0], "_")) | |
181 | 225 | if len(destRunes) > 24 { |
182 | 226 | destRunes = destRunes[:24] |
183 | 227 | } |
185 | 229 | return fmt.Sprintf("%s%x", string(destRunes), bs) |
186 | 230 | } |
187 | 231 | |
232 | // NormalizeIndexAndColumn returns index name and column name for specify an index prefix length if needed | |
233 | func (mysql) NormalizeIndexAndColumn(indexName, columnName string) (string, string) { | |
234 | submatch := mysqlIndexRegex.FindStringSubmatch(indexName) | |
235 | if len(submatch) != 3 { | |
236 | return indexName, columnName | |
237 | } | |
238 | indexName = submatch[1] | |
239 | columnName = fmt.Sprintf("%s(%s)", columnName, submatch[2]) | |
240 | return indexName, columnName | |
241 | } | |
242 | ||
188 | 243 | func (mysql) DefaultValueStr() string { |
189 | 244 | return "VALUES()" |
190 | 245 | } |
33 | 33 | sqlType = "boolean" |
34 | 34 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uintptr: |
35 | 35 | if s.fieldCanAutoIncrement(field) { |
36 | field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" | |
36 | field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT") | |
37 | 37 | sqlType = "serial" |
38 | 38 | } else { |
39 | 39 | sqlType = "integer" |
40 | 40 | } |
41 | 41 | case reflect.Int64, reflect.Uint32, reflect.Uint64: |
42 | 42 | if s.fieldCanAutoIncrement(field) { |
43 | field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" | |
43 | field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT") | |
44 | 44 | sqlType = "bigserial" |
45 | 45 | } else { |
46 | 46 | sqlType = "bigint" |
48 | 48 | case reflect.Float32, reflect.Float64: |
49 | 49 | sqlType = "numeric" |
50 | 50 | case reflect.String: |
51 | if _, ok := field.TagSettings["SIZE"]; !ok { | |
51 | if _, ok := field.TagSettingsGet("SIZE"); !ok { | |
52 | 52 | size = 0 // if SIZE haven't been set, use `text` as the default type, as there are no performance different |
53 | 53 | } |
54 | 54 | |
119 | 119 | return |
120 | 120 | } |
121 | 121 | |
122 | func (s postgres) LastInsertIDOutputInterstitial(tableName, key string, columns []string) string { | |
123 | return "" | |
124 | } | |
125 | ||
122 | 126 | func (s postgres) LastInsertIDReturningSuffix(tableName, key string) string { |
123 | 127 | return fmt.Sprintf("RETURNING %v.%v", tableName, key) |
124 | 128 | } |
28 | 28 | sqlType = "bool" |
29 | 29 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr: |
30 | 30 | if s.fieldCanAutoIncrement(field) { |
31 | field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" | |
31 | field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT") | |
32 | 32 | sqlType = "integer primary key autoincrement" |
33 | 33 | } else { |
34 | 34 | sqlType = "integer" |
35 | 35 | } |
36 | 36 | case reflect.Int64, reflect.Uint64: |
37 | 37 | if s.fieldCanAutoIncrement(field) { |
38 | field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" | |
38 | field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT") | |
39 | 39 | sqlType = "integer primary key autoincrement" |
40 | 40 | } else { |
41 | 41 | sqlType = "bigint" |
0 | 0 | package mssql |
1 | 1 | |
2 | 2 | import ( |
3 | "database/sql/driver" | |
4 | "encoding/json" | |
5 | "errors" | |
3 | 6 | "fmt" |
4 | 7 | "reflect" |
5 | 8 | "strconv" |
6 | 9 | "strings" |
7 | 10 | "time" |
8 | 11 | |
12 | // Importing mssql driver package only in dialect file, otherwide not needed | |
9 | 13 | _ "github.com/denisenkom/go-mssqldb" |
10 | 14 | "github.com/jinzhu/gorm" |
11 | 15 | ) |
13 | 17 | func setIdentityInsert(scope *gorm.Scope) { |
14 | 18 | if scope.Dialect().GetName() == "mssql" { |
15 | 19 | for _, field := range scope.PrimaryFields() { |
16 | if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok && !field.IsBlank { | |
20 | if _, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok && !field.IsBlank { | |
17 | 21 | scope.NewDB().Exec(fmt.Sprintf("SET IDENTITY_INSERT %v ON", scope.TableName())) |
18 | 22 | scope.InstanceSet("mssql:identity_insert_on", true) |
19 | 23 | } |
65 | 69 | sqlType = "bit" |
66 | 70 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr: |
67 | 71 | if s.fieldCanAutoIncrement(field) { |
68 | field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" | |
72 | field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT") | |
69 | 73 | sqlType = "int IDENTITY(1,1)" |
70 | 74 | } else { |
71 | 75 | sqlType = "int" |
72 | 76 | } |
73 | 77 | case reflect.Int64, reflect.Uint64: |
74 | 78 | if s.fieldCanAutoIncrement(field) { |
75 | field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" | |
79 | field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT") | |
76 | 80 | sqlType = "bigint IDENTITY(1,1)" |
77 | 81 | } else { |
78 | 82 | sqlType = "bigint" |
111 | 115 | } |
112 | 116 | |
113 | 117 | func (s mssql) fieldCanAutoIncrement(field *gorm.StructField) bool { |
114 | if value, ok := field.TagSettings["AUTO_INCREMENT"]; ok { | |
118 | if value, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok { | |
115 | 119 | return value != "FALSE" |
116 | 120 | } |
117 | 121 | return field.IsPrimaryKey |
129 | 133 | } |
130 | 134 | |
131 | 135 | func (s mssql) HasForeignKey(tableName string, foreignKeyName string) bool { |
132 | return false | |
136 | var count int | |
137 | currentDatabase, tableName := currentDatabaseAndTable(&s, tableName) | |
138 | s.db.QueryRow(`SELECT count(*) | |
139 | FROM sys.foreign_keys as F inner join sys.tables as T on F.parent_object_id=T.object_id | |
140 | inner join information_schema.tables as I on I.TABLE_NAME = T.name | |
141 | WHERE F.name = ? | |
142 | AND T.Name = ? AND I.TABLE_CATALOG = ?;`, foreignKeyName, tableName, currentDatabase).Scan(&count) | |
143 | return count > 0 | |
133 | 144 | } |
134 | 145 | |
135 | 146 | func (s mssql) HasTable(tableName string) bool { |
156 | 167 | return |
157 | 168 | } |
158 | 169 | |
159 | func (mssql) LimitAndOffsetSQL(limit, offset interface{}) (sql string) { | |
170 | func parseInt(value interface{}) (int64, error) { | |
171 | return strconv.ParseInt(fmt.Sprint(value), 0, 0) | |
172 | } | |
173 | ||
174 | func (mssql) LimitAndOffsetSQL(limit, offset interface{}) (sql string, err error) { | |
160 | 175 | if offset != nil { |
161 | if parsedOffset, err := strconv.ParseInt(fmt.Sprint(offset), 0, 0); err == nil && parsedOffset >= 0 { | |
176 | if parsedOffset, err := parseInt(offset); err != nil { | |
177 | return "", err | |
178 | } else if parsedOffset >= 0 { | |
162 | 179 | sql += fmt.Sprintf(" OFFSET %d ROWS", parsedOffset) |
163 | 180 | } |
164 | 181 | } |
165 | 182 | if limit != nil { |
166 | if parsedLimit, err := strconv.ParseInt(fmt.Sprint(limit), 0, 0); err == nil && parsedLimit >= 0 { | |
183 | if parsedLimit, err := parseInt(limit); err != nil { | |
184 | return "", err | |
185 | } else if parsedLimit >= 0 { | |
167 | 186 | if sql == "" { |
168 | 187 | // add default zero offset |
169 | 188 | sql += " OFFSET 0 ROWS" |
178 | 197 | return "" |
179 | 198 | } |
180 | 199 | |
200 | func (mssql) LastInsertIDOutputInterstitial(tableName, columnName string, columns []string) string { | |
201 | if len(columns) == 0 { | |
202 | // No OUTPUT to query | |
203 | return "" | |
204 | } | |
205 | return fmt.Sprintf("OUTPUT Inserted.%v", columnName) | |
206 | } | |
207 | ||
181 | 208 | func (mssql) LastInsertIDReturningSuffix(tableName, columnName string) string { |
182 | return "" | |
209 | // https://stackoverflow.com/questions/5228780/how-to-get-last-inserted-id | |
210 | return "; SELECT SCOPE_IDENTITY()" | |
183 | 211 | } |
184 | 212 | |
185 | 213 | func (mssql) DefaultValueStr() string { |
186 | 214 | return "DEFAULT VALUES" |
215 | } | |
216 | ||
217 | // NormalizeIndexAndColumn returns argument's index name and column name without doing anything | |
218 | func (mssql) NormalizeIndexAndColumn(indexName, columnName string) (string, string) { | |
219 | return indexName, columnName | |
187 | 220 | } |
188 | 221 | |
189 | 222 | func currentDatabaseAndTable(dialect gorm.Dialect, tableName string) (string, string) { |
193 | 226 | } |
194 | 227 | return dialect.CurrentDatabase(), tableName |
195 | 228 | } |
229 | ||
230 | // JSON type to support easy handling of JSON data in character table fields | |
231 | // using golang json.RawMessage for deferred decoding/encoding | |
232 | type JSON struct { | |
233 | json.RawMessage | |
234 | } | |
235 | ||
236 | // Value get value of JSON | |
237 | func (j JSON) Value() (driver.Value, error) { | |
238 | if len(j.RawMessage) == 0 { | |
239 | return nil, nil | |
240 | } | |
241 | return j.MarshalJSON() | |
242 | } | |
243 | ||
244 | // Scan scan value into JSON | |
245 | func (j *JSON) Scan(value interface{}) error { | |
246 | str, ok := value.(string) | |
247 | if !ok { | |
248 | return errors.New(fmt.Sprint("Failed to unmarshal JSONB value (strcast):", value)) | |
249 | } | |
250 | bytes := []byte(str) | |
251 | return json.Unmarshal(bytes, j) | |
252 | } |
3 | 3 | "database/sql" |
4 | 4 | "database/sql/driver" |
5 | 5 | |
6 | _ "github.com/lib/pq" | |
7 | "github.com/lib/pq/hstore" | |
8 | 6 | "encoding/json" |
9 | 7 | "errors" |
10 | 8 | "fmt" |
9 | ||
10 | _ "github.com/lib/pq" | |
11 | "github.com/lib/pq/hstore" | |
11 | 12 | ) |
12 | 13 | |
13 | 14 | type Hstore map[string]*string |
5 | 5 | ) |
6 | 6 | |
7 | 7 | var ( |
8 | // ErrRecordNotFound record not found error, happens when haven't find any matched data when looking up with a struct | |
8 | // ErrRecordNotFound returns a "record not found error". Occurs only when attempting to query the database with a struct; querying with a slice won't return this error | |
9 | 9 | ErrRecordNotFound = errors.New("record not found") |
10 | // ErrInvalidSQL invalid SQL error, happens when you passed invalid SQL | |
10 | // ErrInvalidSQL occurs when you attempt a query with invalid SQL | |
11 | 11 | ErrInvalidSQL = errors.New("invalid SQL") |
12 | // ErrInvalidTransaction invalid transaction when you are trying to `Commit` or `Rollback` | |
12 | // ErrInvalidTransaction occurs when you are trying to `Commit` or `Rollback` | |
13 | 13 | ErrInvalidTransaction = errors.New("no valid transaction") |
14 | 14 | // ErrCantStartTransaction can't start transaction when you are trying to start one with `Begin` |
15 | 15 | ErrCantStartTransaction = errors.New("can't start transaction") |
20 | 20 | // Errors contains all happened errors |
21 | 21 | type Errors []error |
22 | 22 | |
23 | // IsRecordNotFoundError returns current error has record not found error or not | |
23 | // IsRecordNotFoundError returns true if error contains a RecordNotFound error | |
24 | 24 | func IsRecordNotFoundError(err error) bool { |
25 | 25 | if errs, ok := err.(Errors); ok { |
26 | 26 | for _, err := range errs { |
32 | 32 | return err == ErrRecordNotFound |
33 | 33 | } |
34 | 34 | |
35 | // GetErrors gets all happened errors | |
35 | // GetErrors gets all errors that have occurred and returns a slice of errors (Error type) | |
36 | 36 | func (errs Errors) GetErrors() []error { |
37 | 37 | return errs |
38 | 38 | } |
39 | 39 | |
40 | // Add adds an error | |
40 | // Add adds an error to a given slice of errors | |
41 | 41 | func (errs Errors) Add(newErrors ...error) Errors { |
42 | 42 | for _, err := range newErrors { |
43 | 43 | if err == nil { |
61 | 61 | return errs |
62 | 62 | } |
63 | 63 | |
64 | // Error format happened errors | |
64 | // Error takes a slice of all errors that have occurred and returns it as a formatted string | |
65 | 65 | func (errs Errors) Error() string { |
66 | 66 | var errors = []string{} |
67 | 67 | for _, e := range errs { |
1 | 1 | |
2 | 2 | import ( |
3 | 3 | "database/sql" |
4 | "database/sql/driver" | |
4 | 5 | "errors" |
5 | 6 | "fmt" |
6 | 7 | "reflect" |
43 | 44 | if reflectValue.Type().ConvertibleTo(fieldValue.Type()) { |
44 | 45 | fieldValue.Set(reflectValue.Convert(fieldValue.Type())) |
45 | 46 | } else if scanner, ok := fieldValue.Addr().Interface().(sql.Scanner); ok { |
46 | err = scanner.Scan(reflectValue.Interface()) | |
47 | v := reflectValue.Interface() | |
48 | if valuer, ok := v.(driver.Valuer); ok { | |
49 | if v, err = valuer.Value(); err == nil { | |
50 | err = scanner.Scan(v) | |
51 | } | |
52 | } else { | |
53 | err = scanner.Scan(v) | |
54 | } | |
47 | 55 | } else { |
48 | 56 | err = fmt.Errorf("could not convert argument of field %s from %s to %s", field.Name, reflectValue.Type(), fieldValue.Type()) |
49 | 57 | } |
0 | 0 | package gorm_test |
1 | 1 | |
2 | 2 | import ( |
3 | "database/sql/driver" | |
4 | "encoding/hex" | |
5 | "fmt" | |
3 | 6 | "testing" |
4 | 7 | |
5 | 8 | "github.com/jinzhu/gorm" |
42 | 45 | |
43 | 46 | if field, ok := scope.FieldByName("embedded_name"); !ok { |
44 | 47 | t.Errorf("should find embedded field") |
45 | } else if _, ok := field.TagSettings["NOT NULL"]; !ok { | |
48 | } else if _, ok := field.TagSettingsGet("NOT NULL"); !ok { | |
46 | 49 | t.Errorf("should find embedded field's tag settings") |
47 | 50 | } |
48 | 51 | } |
52 | ||
53 | type UUID [16]byte | |
54 | ||
55 | type NullUUID struct { | |
56 | UUID | |
57 | Valid bool | |
58 | } | |
59 | ||
60 | func FromString(input string) (u UUID) { | |
61 | src := []byte(input) | |
62 | return FromBytes(src) | |
63 | } | |
64 | ||
65 | func FromBytes(src []byte) (u UUID) { | |
66 | dst := u[:] | |
67 | hex.Decode(dst[0:4], src[0:8]) | |
68 | hex.Decode(dst[4:6], src[9:13]) | |
69 | hex.Decode(dst[6:8], src[14:18]) | |
70 | hex.Decode(dst[8:10], src[19:23]) | |
71 | hex.Decode(dst[10:], src[24:]) | |
72 | return | |
73 | } | |
74 | ||
75 | func (u UUID) String() string { | |
76 | buf := make([]byte, 36) | |
77 | src := u[:] | |
78 | hex.Encode(buf[0:8], src[0:4]) | |
79 | buf[8] = '-' | |
80 | hex.Encode(buf[9:13], src[4:6]) | |
81 | buf[13] = '-' | |
82 | hex.Encode(buf[14:18], src[6:8]) | |
83 | buf[18] = '-' | |
84 | hex.Encode(buf[19:23], src[8:10]) | |
85 | buf[23] = '-' | |
86 | hex.Encode(buf[24:], src[10:]) | |
87 | return string(buf) | |
88 | } | |
89 | ||
90 | func (u UUID) Value() (driver.Value, error) { | |
91 | return u.String(), nil | |
92 | } | |
93 | ||
94 | func (u *UUID) Scan(src interface{}) error { | |
95 | switch src := src.(type) { | |
96 | case UUID: // support gorm convert from UUID to NullUUID | |
97 | *u = src | |
98 | return nil | |
99 | case []byte: | |
100 | *u = FromBytes(src) | |
101 | return nil | |
102 | case string: | |
103 | *u = FromString(src) | |
104 | return nil | |
105 | } | |
106 | return fmt.Errorf("uuid: cannot convert %T to UUID", src) | |
107 | } | |
108 | ||
109 | func (u *NullUUID) Scan(src interface{}) error { | |
110 | u.Valid = true | |
111 | return u.UUID.Scan(src) | |
112 | } | |
113 | ||
114 | func TestFieldSet(t *testing.T) { | |
115 | type TestFieldSetNullUUID struct { | |
116 | NullUUID NullUUID | |
117 | } | |
118 | scope := DB.NewScope(&TestFieldSetNullUUID{}) | |
119 | field := scope.Fields()[0] | |
120 | err := field.Set(FromString("3034d44a-da03-11e8-b366-4a00070b9f00")) | |
121 | if err != nil { | |
122 | t.Fatal(err) | |
123 | } | |
124 | if id, ok := field.Field.Addr().Interface().(*NullUUID); !ok { | |
125 | t.Fatal() | |
126 | } else if !id.Valid || id.UUID.String() != "3034d44a-da03-11e8-b366-4a00070b9f00" { | |
127 | t.Fatal(id) | |
128 | } | |
129 | } |
0 | module github.com/jinzhu/gorm | |
1 | ||
2 | go 1.12 | |
3 | ||
4 | require ( | |
5 | github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd | |
6 | github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 | |
7 | github.com/go-sql-driver/mysql v1.5.0 | |
8 | github.com/jinzhu/inflection v1.0.0 | |
9 | github.com/jinzhu/now v1.0.1 | |
10 | github.com/lib/pq v1.1.1 | |
11 | github.com/mattn/go-sqlite3 v1.14.0 | |
12 | golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd // indirect | |
13 | ) |
0 | github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= | |
1 | github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= | |
2 | github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM= | |
3 | github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= | |
4 | github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= | |
5 | github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= | |
6 | github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= | |
7 | github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= | |
8 | github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= | |
9 | github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= | |
10 | github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= | |
11 | github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= | |
12 | github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M= | |
13 | github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | |
14 | github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4= | |
15 | github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= | |
16 | github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA= | |
17 | github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= | |
18 | github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw= | |
19 | github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= | |
20 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | |
21 | golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c h1:Vj5n4GlwjmQteupaxJ9+0FNOmBrHfq7vN4btdGoDZgI= | |
22 | golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | |
23 | golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd h1:GGJVjV8waZKRHrgwvtH66z9ZGVurTD1MT0n1Bb+q4aM= | |
24 | golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= | |
25 | golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= | |
26 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | |
27 | golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | |
28 | golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= | |
29 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | |
30 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | |
31 | golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | |
32 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= |
0 | 0 | package gorm |
1 | 1 | |
2 | import "database/sql" | |
2 | import ( | |
3 | "context" | |
4 | "database/sql" | |
5 | ) | |
3 | 6 | |
4 | 7 | // SQLCommon is the minimal database connection functionality gorm requires. Implemented by *sql.DB. |
5 | 8 | type SQLCommon interface { |
11 | 14 | |
12 | 15 | type sqlDb interface { |
13 | 16 | Begin() (*sql.Tx, error) |
17 | BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) | |
14 | 18 | } |
15 | 19 | |
16 | 20 | type sqlTx interface { |
38 | 38 | |
39 | 39 | messages = []interface{}{source, currentTime} |
40 | 40 | |
41 | if len(values) == 2 { | |
42 | //remove the line break | |
43 | currentTime = currentTime[1:] | |
44 | //remove the brackets | |
45 | source = fmt.Sprintf("\033[35m%v\033[0m", values[1]) | |
46 | ||
47 | messages = []interface{}{currentTime, source} | |
48 | } | |
49 | ||
41 | 50 | if level == "sql" { |
42 | 51 | // duration |
43 | 52 | messages = append(messages, fmt.Sprintf(" \033[36;1m[%.2fms]\033[0m ", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0)) |
48 | 57 | if indirectValue.IsValid() { |
49 | 58 | value = indirectValue.Interface() |
50 | 59 | if t, ok := value.(time.Time); ok { |
51 | formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05"))) | |
60 | if t.IsZero() { | |
61 | formattedValues = append(formattedValues, fmt.Sprintf("'%v'", "0000-00-00 00:00:00")) | |
62 | } else { | |
63 | formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05"))) | |
64 | } | |
52 | 65 | } else if b, ok := value.([]byte); ok { |
53 | 66 | if str := string(b); isPrintable(str) { |
54 | 67 | formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str)) |
62 | 75 | formattedValues = append(formattedValues, "NULL") |
63 | 76 | } |
64 | 77 | } else { |
65 | formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) | |
78 | switch value.(type) { | |
79 | case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool: | |
80 | formattedValues = append(formattedValues, fmt.Sprintf("%v", value)) | |
81 | default: | |
82 | formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) | |
83 | } | |
66 | 84 | } |
67 | 85 | } else { |
68 | 86 | formattedValues = append(formattedValues, "NULL") |
116 | 134 | func (logger Logger) Print(values ...interface{}) { |
117 | 135 | logger.Println(LogFormatter(values...)...) |
118 | 136 | } |
137 | ||
138 | type nopLogger struct{} | |
139 | ||
140 | func (nopLogger) Print(values ...interface{}) {} |
0 | 0 | package gorm |
1 | 1 | |
2 | 2 | import ( |
3 | "context" | |
3 | 4 | "database/sql" |
4 | 5 | "errors" |
5 | 6 | "fmt" |
6 | 7 | "reflect" |
7 | 8 | "strings" |
9 | "sync" | |
8 | 10 | "time" |
9 | 11 | ) |
10 | 12 | |
11 | 13 | // DB contains information for current db connection |
12 | 14 | type DB struct { |
15 | sync.RWMutex | |
13 | 16 | Value interface{} |
14 | 17 | Error error |
15 | 18 | RowsAffected int64 |
17 | 20 | // single db |
18 | 21 | db SQLCommon |
19 | 22 | blockGlobalUpdate bool |
20 | logMode int | |
23 | logMode logModeValue | |
21 | 24 | logger logger |
22 | 25 | search *search |
23 | values map[string]interface{} | |
26 | values sync.Map | |
24 | 27 | |
25 | 28 | // global db |
26 | 29 | parent *DB |
27 | 30 | callbacks *Callback |
28 | 31 | dialect Dialect |
29 | 32 | singularTable bool |
30 | } | |
33 | ||
34 | // function to be used to override the creating of a new timestamp | |
35 | nowFuncOverride func() time.Time | |
36 | } | |
37 | ||
38 | type logModeValue int | |
39 | ||
40 | const ( | |
41 | defaultLogMode logModeValue = iota | |
42 | noLogMode | |
43 | detailedLogMode | |
44 | ) | |
31 | 45 | |
32 | 46 | // Open initialize a new db connection, need to import driver first, e.g: |
33 | 47 | // |
47 | 61 | } |
48 | 62 | var source string |
49 | 63 | var dbSQL SQLCommon |
64 | var ownDbSQL bool | |
50 | 65 | |
51 | 66 | switch value := args[0].(type) { |
52 | 67 | case string: |
58 | 73 | source = args[1].(string) |
59 | 74 | } |
60 | 75 | dbSQL, err = sql.Open(driver, source) |
76 | ownDbSQL = true | |
61 | 77 | case SQLCommon: |
62 | 78 | dbSQL = value |
79 | ownDbSQL = false | |
80 | default: | |
81 | return nil, fmt.Errorf("invalid database source: %v is not a valid type", value) | |
63 | 82 | } |
64 | 83 | |
65 | 84 | db = &DB{ |
66 | 85 | db: dbSQL, |
67 | 86 | logger: defaultLogger, |
68 | values: map[string]interface{}{}, | |
69 | 87 | callbacks: DefaultCallback, |
70 | 88 | dialect: newDialect(dialect, dbSQL), |
71 | 89 | } |
75 | 93 | } |
76 | 94 | // Send a ping to make sure the database connection is alive. |
77 | 95 | if d, ok := dbSQL.(*sql.DB); ok { |
78 | if err = d.Ping(); err != nil { | |
96 | if err = d.Ping(); err != nil && ownDbSQL { | |
79 | 97 | d.Close() |
80 | 98 | } |
81 | 99 | } |
105 | 123 | // DB get `*sql.DB` from current connection |
106 | 124 | // If the underlying database connection is not a *sql.DB, returns nil |
107 | 125 | func (s *DB) DB() *sql.DB { |
108 | db, _ := s.db.(*sql.DB) | |
126 | db, ok := s.db.(*sql.DB) | |
127 | if !ok { | |
128 | panic("can't support full GORM on currently status, maybe this is a TX instance.") | |
129 | } | |
109 | 130 | return db |
110 | 131 | } |
111 | 132 | |
116 | 137 | |
117 | 138 | // Dialect get dialect |
118 | 139 | func (s *DB) Dialect() Dialect { |
119 | return s.parent.dialect | |
140 | return s.dialect | |
120 | 141 | } |
121 | 142 | |
122 | 143 | // Callback return `Callbacks` container, you could add/change/delete callbacks with it |
123 | 144 | // db.Callback().Create().Register("update_created_at", updateCreated) |
124 | 145 | // Refer https://jinzhu.github.io/gorm/development.html#callbacks |
125 | 146 | func (s *DB) Callback() *Callback { |
126 | s.parent.callbacks = s.parent.callbacks.clone() | |
147 | s.parent.callbacks = s.parent.callbacks.clone(s.logger) | |
127 | 148 | return s.parent.callbacks |
128 | 149 | } |
129 | 150 | |
135 | 156 | // LogMode set log mode, `true` for detailed logs, `false` for no log, default, will only print error logs |
136 | 157 | func (s *DB) LogMode(enable bool) *DB { |
137 | 158 | if enable { |
138 | s.logMode = 2 | |
139 | } else { | |
140 | s.logMode = 1 | |
159 | s.logMode = detailedLogMode | |
160 | } else { | |
161 | s.logMode = noLogMode | |
141 | 162 | } |
142 | 163 | return s |
164 | } | |
165 | ||
166 | // SetNowFuncOverride set the function to be used when creating a new timestamp | |
167 | func (s *DB) SetNowFuncOverride(nowFuncOverride func() time.Time) *DB { | |
168 | s.nowFuncOverride = nowFuncOverride | |
169 | return s | |
170 | } | |
171 | ||
172 | // Get a new timestamp, using the provided nowFuncOverride on the DB instance if set, | |
173 | // otherwise defaults to the global NowFunc() | |
174 | func (s *DB) nowFunc() time.Time { | |
175 | if s.nowFuncOverride != nil { | |
176 | return s.nowFuncOverride() | |
177 | } | |
178 | ||
179 | return NowFunc() | |
143 | 180 | } |
144 | 181 | |
145 | 182 | // BlockGlobalUpdate if true, generates an error on update/delete without where clause. |
156 | 193 | |
157 | 194 | // SingularTable use singular table by default |
158 | 195 | func (s *DB) SingularTable(enable bool) { |
159 | modelStructsMap = newModelStructsMap() | |
196 | s.parent.Lock() | |
197 | defer s.parent.Unlock() | |
160 | 198 | s.parent.singularTable = enable |
161 | 199 | } |
162 | 200 | |
164 | 202 | func (s *DB) NewScope(value interface{}) *Scope { |
165 | 203 | dbClone := s.clone() |
166 | 204 | dbClone.Value = value |
167 | return &Scope{db: dbClone, Search: dbClone.search.clone(), Value: value} | |
168 | } | |
169 | ||
170 | // QueryExpr returns the query as expr object | |
171 | func (s *DB) QueryExpr() *expr { | |
205 | scope := &Scope{db: dbClone, Value: value} | |
206 | if s.search != nil { | |
207 | scope.Search = s.search.clone() | |
208 | } else { | |
209 | scope.Search = &search{} | |
210 | } | |
211 | return scope | |
212 | } | |
213 | ||
214 | // QueryExpr returns the query as SqlExpr object | |
215 | func (s *DB) QueryExpr() *SqlExpr { | |
172 | 216 | scope := s.NewScope(s.Value) |
173 | 217 | scope.InstanceSet("skip_bindvar", true) |
174 | 218 | scope.prepareQuerySQL() |
177 | 221 | } |
178 | 222 | |
179 | 223 | // SubQuery returns the query as sub query |
180 | func (s *DB) SubQuery() *expr { | |
224 | func (s *DB) SubQuery() *SqlExpr { | |
181 | 225 | scope := s.NewScope(s.Value) |
182 | 226 | scope.InstanceSet("skip_bindvar", true) |
183 | 227 | scope.prepareQuerySQL() |
284 | 328 | func (s *DB) First(out interface{}, where ...interface{}) *DB { |
285 | 329 | newScope := s.NewScope(out) |
286 | 330 | newScope.Search.Limit(1) |
331 | ||
287 | 332 | return newScope.Set("gorm:order_by_primary_key", "ASC"). |
288 | 333 | inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db |
289 | 334 | } |
306 | 351 | // Find find records that match given conditions |
307 | 352 | func (s *DB) Find(out interface{}, where ...interface{}) *DB { |
308 | 353 | return s.NewScope(out).inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db |
354 | } | |
355 | ||
356 | //Preloads preloads relations, don`t touch out | |
357 | func (s *DB) Preloads(out interface{}) *DB { | |
358 | return s.NewScope(out).InstanceSet("gorm:only_preload", 1).callCallbacks(s.parent.callbacks.queries).db | |
309 | 359 | } |
310 | 360 | |
311 | 361 | // Scan scan value to a struct |
386 | 436 | } |
387 | 437 | |
388 | 438 | // Update update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update |
439 | // WARNING when update with struct, GORM will not update fields that with zero value | |
389 | 440 | func (s *DB) Update(attrs ...interface{}) *DB { |
390 | 441 | return s.Updates(toSearchableMap(attrs...), true) |
391 | 442 | } |
418 | 469 | if !scope.PrimaryKeyZero() { |
419 | 470 | newDB := scope.callCallbacks(s.parent.callbacks.updates).db |
420 | 471 | if newDB.Error == nil && newDB.RowsAffected == 0 { |
421 | return s.New().FirstOrCreate(value) | |
472 | return s.New().Table(scope.TableName()).FirstOrCreate(value) | |
422 | 473 | } |
423 | 474 | return newDB |
424 | 475 | } |
432 | 483 | } |
433 | 484 | |
434 | 485 | // Delete delete value match given conditions, if the value has primary key, then will including the primary key as condition |
486 | // WARNING If model has DeletedAt field, GORM will only set field DeletedAt's value to current time | |
435 | 487 | func (s *DB) Delete(value interface{}, where ...interface{}) *DB { |
436 | 488 | return s.NewScope(value).inlineCondition(where...).callCallbacks(s.parent.callbacks.deletes).db |
437 | 489 | } |
475 | 527 | return s.clone().LogMode(true) |
476 | 528 | } |
477 | 529 | |
478 | // Begin begin a transaction | |
530 | // Transaction start a transaction as a block, | |
531 | // return error will rollback, otherwise to commit. | |
532 | func (s *DB) Transaction(fc func(tx *DB) error) (err error) { | |
533 | ||
534 | if _, ok := s.db.(*sql.Tx); ok { | |
535 | return fc(s) | |
536 | } | |
537 | ||
538 | panicked := true | |
539 | tx := s.Begin() | |
540 | defer func() { | |
541 | // Make sure to rollback when panic, Block error or Commit error | |
542 | if panicked || err != nil { | |
543 | tx.Rollback() | |
544 | } | |
545 | }() | |
546 | ||
547 | err = fc(tx) | |
548 | ||
549 | if err == nil { | |
550 | err = tx.Commit().Error | |
551 | } | |
552 | ||
553 | panicked = false | |
554 | return | |
555 | } | |
556 | ||
557 | // Begin begins a transaction | |
479 | 558 | func (s *DB) Begin() *DB { |
559 | return s.BeginTx(context.Background(), &sql.TxOptions{}) | |
560 | } | |
561 | ||
562 | // BeginTx begins a transaction with options | |
563 | func (s *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) *DB { | |
480 | 564 | c := s.clone() |
481 | 565 | if db, ok := c.db.(sqlDb); ok && db != nil { |
482 | tx, err := db.Begin() | |
566 | tx, err := db.BeginTx(ctx, opts) | |
483 | 567 | c.db = interface{}(tx).(SQLCommon) |
568 | ||
569 | c.dialect.SetDB(c.db) | |
484 | 570 | c.AddError(err) |
485 | 571 | } else { |
486 | 572 | c.AddError(ErrCantStartTransaction) |
490 | 576 | |
491 | 577 | // Commit commit a transaction |
492 | 578 | func (s *DB) Commit() *DB { |
493 | if db, ok := s.db.(sqlTx); ok && db != nil { | |
579 | var emptySQLTx *sql.Tx | |
580 | if db, ok := s.db.(sqlTx); ok && db != nil && db != emptySQLTx { | |
494 | 581 | s.AddError(db.Commit()) |
495 | 582 | } else { |
496 | 583 | s.AddError(ErrInvalidTransaction) |
500 | 587 | |
501 | 588 | // Rollback rollback a transaction |
502 | 589 | func (s *DB) Rollback() *DB { |
503 | if db, ok := s.db.(sqlTx); ok && db != nil { | |
504 | s.AddError(db.Rollback()) | |
590 | var emptySQLTx *sql.Tx | |
591 | if db, ok := s.db.(sqlTx); ok && db != nil && db != emptySQLTx { | |
592 | if err := db.Rollback(); err != nil && err != sql.ErrTxDone { | |
593 | s.AddError(err) | |
594 | } | |
595 | } else { | |
596 | s.AddError(ErrInvalidTransaction) | |
597 | } | |
598 | return s | |
599 | } | |
600 | ||
601 | // RollbackUnlessCommitted rollback a transaction if it has not yet been | |
602 | // committed. | |
603 | func (s *DB) RollbackUnlessCommitted() *DB { | |
604 | var emptySQLTx *sql.Tx | |
605 | if db, ok := s.db.(sqlTx); ok && db != nil && db != emptySQLTx { | |
606 | err := db.Rollback() | |
607 | // Ignore the error indicating that the transaction has already | |
608 | // been committed. | |
609 | if err != sql.ErrTxDone { | |
610 | s.AddError(err) | |
611 | } | |
505 | 612 | } else { |
506 | 613 | s.AddError(ErrInvalidTransaction) |
507 | 614 | } |
669 | 776 | |
670 | 777 | // InstantSet instant set setting, will affect current db |
671 | 778 | func (s *DB) InstantSet(name string, value interface{}) *DB { |
672 | s.values[name] = value | |
779 | s.values.Store(name, value) | |
673 | 780 | return s |
674 | 781 | } |
675 | 782 | |
676 | 783 | // Get get setting by name |
677 | 784 | func (s *DB) Get(name string) (value interface{}, ok bool) { |
678 | value, ok = s.values[name] | |
785 | value, ok = s.values.Load(name) | |
679 | 786 | return |
680 | 787 | } |
681 | 788 | |
684 | 791 | scope := s.NewScope(source) |
685 | 792 | for _, field := range scope.GetModelStruct().StructFields { |
686 | 793 | if field.Name == column || field.DBName == column { |
687 | if many2many := field.TagSettings["MANY2MANY"]; many2many != "" { | |
794 | if many2many, _ := field.TagSettingsGet("MANY2MANY"); many2many != "" { | |
688 | 795 | source := (&Scope{Value: source}).GetModelStruct().ModelType |
689 | 796 | destination := (&Scope{Value: reflect.New(field.Struct.Type).Interface()}).GetModelStruct().ModelType |
690 | 797 | handler.Setup(field.Relationship, many2many, source, destination) |
701 | 808 | func (s *DB) AddError(err error) error { |
702 | 809 | if err != nil { |
703 | 810 | if err != ErrRecordNotFound { |
704 | if s.logMode == 0 { | |
705 | go s.print(fileWithLineNum(), err) | |
811 | if s.logMode == defaultLogMode { | |
812 | go s.print("error", fileWithLineNum(), err) | |
706 | 813 | } else { |
707 | 814 | s.log(err) |
708 | 815 | } |
739 | 846 | parent: s.parent, |
740 | 847 | logger: s.logger, |
741 | 848 | logMode: s.logMode, |
742 | values: map[string]interface{}{}, | |
743 | 849 | Value: s.Value, |
744 | 850 | Error: s.Error, |
745 | 851 | blockGlobalUpdate: s.blockGlobalUpdate, |
746 | } | |
747 | ||
748 | for key, value := range s.values { | |
749 | db.values[key] = value | |
750 | } | |
852 | dialect: newDialect(s.dialect.GetName(), s.db), | |
853 | nowFuncOverride: s.nowFuncOverride, | |
854 | } | |
855 | ||
856 | s.values.Range(func(k, v interface{}) bool { | |
857 | db.values.Store(k, v) | |
858 | return true | |
859 | }) | |
751 | 860 | |
752 | 861 | if s.search == nil { |
753 | 862 | db.search = &search{limit: -1, offset: -1} |
764 | 873 | } |
765 | 874 | |
766 | 875 | func (s *DB) log(v ...interface{}) { |
767 | if s != nil && s.logMode == 2 { | |
876 | if s != nil && s.logMode == detailedLogMode { | |
768 | 877 | s.print(append([]interface{}{"log", fileWithLineNum()}, v...)...) |
769 | 878 | } |
770 | 879 | } |
771 | 880 | |
772 | 881 | func (s *DB) slog(sql string, t time.Time, vars ...interface{}) { |
773 | if s.logMode == 2 { | |
882 | if s.logMode == detailedLogMode { | |
774 | 883 | s.print("sql", fileWithLineNum(), NowFunc().Sub(t), sql, vars, s.RowsAffected) |
775 | 884 | } |
776 | 885 | } |
0 | 0 | package gorm_test |
1 | 1 | |
2 | // Run tests | |
3 | // $ docker-compose up | |
4 | // $ ./test_all.sh | |
5 | ||
2 | 6 | import ( |
7 | "context" | |
3 | 8 | "database/sql" |
4 | 9 | "database/sql/driver" |
10 | "errors" | |
5 | 11 | "fmt" |
6 | 12 | "os" |
7 | 13 | "path/filepath" |
8 | 14 | "reflect" |
15 | "sort" | |
9 | 16 | "strconv" |
17 | "strings" | |
18 | "sync" | |
10 | 19 | "testing" |
11 | 20 | "time" |
12 | 21 | |
46 | 55 | case "postgres": |
47 | 56 | fmt.Println("testing postgres...") |
48 | 57 | if dbDSN == "" { |
49 | dbDSN = "user=gorm password=gorm DB.name=gorm port=9920 sslmode=disable" | |
58 | dbDSN = "user=gorm password=gorm dbname=gorm port=9920 sslmode=disable" | |
50 | 59 | } |
51 | 60 | db, err = gorm.Open("postgres", dbDSN) |
52 | 61 | case "mssql": |
78 | 87 | return |
79 | 88 | } |
80 | 89 | |
90 | func TestOpen_ReturnsError_WithBadArgs(t *testing.T) { | |
91 | stringRef := "foo" | |
92 | testCases := []interface{}{42, time.Now(), &stringRef} | |
93 | for _, tc := range testCases { | |
94 | t.Run(fmt.Sprintf("%v", tc), func(t *testing.T) { | |
95 | _, err := gorm.Open("postgresql", tc) | |
96 | if err == nil { | |
97 | t.Error("Should got error with invalid database source") | |
98 | } | |
99 | if !strings.HasPrefix(err.Error(), "invalid database source:") { | |
100 | t.Errorf("Should got error starting with \"invalid database source:\", but got %q", err.Error()) | |
101 | } | |
102 | }) | |
103 | } | |
104 | } | |
105 | ||
81 | 106 | func TestStringPrimaryKey(t *testing.T) { |
82 | 107 | type UUIDStruct struct { |
83 | 108 | ID string `gorm:"primary_key"` |
156 | 181 | DB.Table("deleted_users").Find(&deletedUsers) |
157 | 182 | if len(deletedUsers) != 1 { |
158 | 183 | t.Errorf("Query from specified table") |
184 | } | |
185 | ||
186 | var user User | |
187 | DB.Table("deleted_users").First(&user, "name = ?", "DeletedUser") | |
188 | ||
189 | user.Age = 20 | |
190 | DB.Table("deleted_users").Save(&user) | |
191 | if DB.Table("deleted_users").First(&user, "name = ? AND age = ?", "DeletedUser", 20).RecordNotFound() { | |
192 | t.Errorf("Failed to found updated user") | |
159 | 193 | } |
160 | 194 | |
161 | 195 | DB.Save(getPreparedUser("normal_user", "reset_table")) |
256 | 290 | if DB.NewScope([]Cart{}).TableName() != "shopping_cart" { |
257 | 291 | t.Errorf("[]Cart's singular table name should be shopping_cart") |
258 | 292 | } |
293 | DB.SingularTable(false) | |
294 | } | |
295 | ||
296 | func TestTableNameConcurrently(t *testing.T) { | |
297 | DB := DB.Model("") | |
298 | if DB.NewScope(Order{}).TableName() != "orders" { | |
299 | t.Errorf("Order's table name should be orders") | |
300 | } | |
301 | ||
302 | var wg sync.WaitGroup | |
303 | wg.Add(10) | |
304 | ||
305 | for i := 1; i <= 10; i++ { | |
306 | go func(db *gorm.DB) { | |
307 | DB.SingularTable(true) | |
308 | wg.Done() | |
309 | }(DB) | |
310 | } | |
311 | wg.Wait() | |
312 | ||
313 | if DB.NewScope(Order{}).TableName() != "order" { | |
314 | t.Errorf("Order's singular table name should be order") | |
315 | } | |
316 | ||
259 | 317 | DB.SingularTable(false) |
260 | 318 | } |
261 | 319 | |
376 | 434 | if err := DB.First(&User{}, "name = ?", "transcation-2").Error; err != nil { |
377 | 435 | t.Errorf("Should be able to find committed record") |
378 | 436 | } |
437 | ||
438 | tx3 := DB.Begin() | |
439 | u3 := User{Name: "transcation-3"} | |
440 | if err := tx3.Save(&u3).Error; err != nil { | |
441 | t.Errorf("No error should raise") | |
442 | } | |
443 | ||
444 | if err := tx3.First(&User{}, "name = ?", "transcation-3").Error; err != nil { | |
445 | t.Errorf("Should find saved record") | |
446 | } | |
447 | ||
448 | tx3.RollbackUnlessCommitted() | |
449 | ||
450 | if err := tx.First(&User{}, "name = ?", "transcation").Error; err == nil { | |
451 | t.Errorf("Should not find record after rollback") | |
452 | } | |
453 | ||
454 | tx4 := DB.Begin() | |
455 | u4 := User{Name: "transcation-4"} | |
456 | if err := tx4.Save(&u4).Error; err != nil { | |
457 | t.Errorf("No error should raise") | |
458 | } | |
459 | ||
460 | if err := tx4.First(&User{}, "name = ?", "transcation-4").Error; err != nil { | |
461 | t.Errorf("Should find saved record") | |
462 | } | |
463 | ||
464 | tx4.Commit() | |
465 | ||
466 | tx4.RollbackUnlessCommitted() | |
467 | ||
468 | if err := DB.First(&User{}, "name = ?", "transcation-4").Error; err != nil { | |
469 | t.Errorf("Should be able to find committed record") | |
470 | } | |
471 | } | |
472 | ||
473 | func assertPanic(t *testing.T, f func()) { | |
474 | defer func() { | |
475 | if r := recover(); r == nil { | |
476 | t.Errorf("The code did not panic") | |
477 | } | |
478 | }() | |
479 | f() | |
480 | } | |
481 | ||
482 | func TestTransactionWithBlock(t *testing.T) { | |
483 | // rollback | |
484 | err := DB.Transaction(func(tx *gorm.DB) error { | |
485 | u := User{Name: "transcation"} | |
486 | if err := tx.Save(&u).Error; err != nil { | |
487 | t.Errorf("No error should raise") | |
488 | } | |
489 | ||
490 | if err := tx.First(&User{}, "name = ?", "transcation").Error; err != nil { | |
491 | t.Errorf("Should find saved record") | |
492 | } | |
493 | ||
494 | return errors.New("the error message") | |
495 | }) | |
496 | ||
497 | if err.Error() != "the error message" { | |
498 | t.Errorf("Transaction return error will equal the block returns error") | |
499 | } | |
500 | ||
501 | if err := DB.First(&User{}, "name = ?", "transcation").Error; err == nil { | |
502 | t.Errorf("Should not find record after rollback") | |
503 | } | |
504 | ||
505 | // commit | |
506 | DB.Transaction(func(tx *gorm.DB) error { | |
507 | u2 := User{Name: "transcation-2"} | |
508 | if err := tx.Save(&u2).Error; err != nil { | |
509 | t.Errorf("No error should raise") | |
510 | } | |
511 | ||
512 | if err := tx.First(&User{}, "name = ?", "transcation-2").Error; err != nil { | |
513 | t.Errorf("Should find saved record") | |
514 | } | |
515 | return nil | |
516 | }) | |
517 | ||
518 | if err := DB.First(&User{}, "name = ?", "transcation-2").Error; err != nil { | |
519 | t.Errorf("Should be able to find committed record") | |
520 | } | |
521 | ||
522 | // panic will rollback | |
523 | assertPanic(t, func() { | |
524 | DB.Transaction(func(tx *gorm.DB) error { | |
525 | u3 := User{Name: "transcation-3"} | |
526 | if err := tx.Save(&u3).Error; err != nil { | |
527 | t.Errorf("No error should raise") | |
528 | } | |
529 | ||
530 | if err := tx.First(&User{}, "name = ?", "transcation-3").Error; err != nil { | |
531 | t.Errorf("Should find saved record") | |
532 | } | |
533 | ||
534 | panic("force panic") | |
535 | }) | |
536 | }) | |
537 | ||
538 | if err := DB.First(&User{}, "name = ?", "transcation-3").Error; err == nil { | |
539 | t.Errorf("Should not find record after panic rollback") | |
540 | } | |
541 | } | |
542 | ||
543 | func TestTransaction_NoErrorOnRollbackAfterCommit(t *testing.T) { | |
544 | tx := DB.Begin() | |
545 | u := User{Name: "transcation"} | |
546 | if err := tx.Save(&u).Error; err != nil { | |
547 | t.Errorf("No error should raise") | |
548 | } | |
549 | ||
550 | if err := tx.Commit().Error; err != nil { | |
551 | t.Errorf("Commit should not raise error") | |
552 | } | |
553 | ||
554 | if err := tx.Rollback().Error; err != nil { | |
555 | t.Errorf("Rollback should not raise error") | |
556 | } | |
557 | } | |
558 | ||
559 | func TestTransactionReadonly(t *testing.T) { | |
560 | dialect := os.Getenv("GORM_DIALECT") | |
561 | if dialect == "" { | |
562 | dialect = "sqlite" | |
563 | } | |
564 | switch dialect { | |
565 | case "mssql", "sqlite": | |
566 | t.Skipf("%s does not support readonly transactions\n", dialect) | |
567 | } | |
568 | ||
569 | tx := DB.Begin() | |
570 | u := User{Name: "transcation"} | |
571 | if err := tx.Save(&u).Error; err != nil { | |
572 | t.Errorf("No error should raise") | |
573 | } | |
574 | tx.Commit() | |
575 | ||
576 | tx = DB.BeginTx(context.Background(), &sql.TxOptions{ReadOnly: true}) | |
577 | if err := tx.First(&User{}, "name = ?", "transcation").Error; err != nil { | |
578 | t.Errorf("Should find saved record") | |
579 | } | |
580 | ||
581 | if sqlTx, ok := tx.CommonDB().(*sql.Tx); !ok || sqlTx == nil { | |
582 | t.Errorf("Should return the underlying sql.Tx") | |
583 | } | |
584 | ||
585 | u = User{Name: "transcation-2"} | |
586 | if err := tx.Save(&u).Error; err == nil { | |
587 | t.Errorf("Error should have been raised in a readonly transaction") | |
588 | } | |
589 | ||
590 | tx.Rollback() | |
379 | 591 | } |
380 | 592 | |
381 | 593 | func TestRow(t *testing.T) { |
563 | 775 | } |
564 | 776 | } |
565 | 777 | |
778 | type JoinedIds struct { | |
779 | UserID int64 `gorm:"column:id"` | |
780 | BillingAddressID int64 `gorm:"column:id"` | |
781 | EmailID int64 `gorm:"column:id"` | |
782 | } | |
783 | ||
784 | func TestScanIdenticalColumnNames(t *testing.T) { | |
785 | var user = User{ | |
786 | Name: "joinsIds", | |
787 | Email: "joinIds@example.com", | |
788 | BillingAddress: Address{ | |
789 | Address1: "One Park Place", | |
790 | }, | |
791 | Emails: []Email{{Email: "join1@example.com"}, {Email: "join2@example.com"}}, | |
792 | } | |
793 | DB.Save(&user) | |
794 | ||
795 | var users []JoinedIds | |
796 | DB.Select("users.id, addresses.id, emails.id").Table("users"). | |
797 | Joins("left join addresses on users.billing_address_id = addresses.id"). | |
798 | Joins("left join emails on emails.user_id = users.id"). | |
799 | Where("name = ?", "joinsIds").Scan(&users) | |
800 | ||
801 | if len(users) != 2 { | |
802 | t.Fatal("should find two rows using left join") | |
803 | } | |
804 | ||
805 | if user.Id != users[0].UserID { | |
806 | t.Errorf("Expected result row to contain UserID %d, but got %d", user.Id, users[0].UserID) | |
807 | } | |
808 | if user.Id != users[1].UserID { | |
809 | t.Errorf("Expected result row to contain UserID %d, but got %d", user.Id, users[1].UserID) | |
810 | } | |
811 | ||
812 | if user.BillingAddressID.Int64 != users[0].BillingAddressID { | |
813 | t.Errorf("Expected result row to contain BillingAddressID %d, but got %d", user.BillingAddressID.Int64, users[0].BillingAddressID) | |
814 | } | |
815 | if user.BillingAddressID.Int64 != users[1].BillingAddressID { | |
816 | t.Errorf("Expected result row to contain BillingAddressID %d, but got %d", user.BillingAddressID.Int64, users[0].BillingAddressID) | |
817 | } | |
818 | ||
819 | if users[0].EmailID == users[1].EmailID { | |
820 | t.Errorf("Email ids should be unique. Got %d and %d", users[0].EmailID, users[1].EmailID) | |
821 | } | |
822 | ||
823 | if int64(user.Emails[0].Id) != users[0].EmailID && int64(user.Emails[1].Id) != users[0].EmailID { | |
824 | t.Errorf("Expected result row ID to be either %d or %d, but was %d", user.Emails[0].Id, user.Emails[1].Id, users[0].EmailID) | |
825 | } | |
826 | ||
827 | if int64(user.Emails[0].Id) != users[1].EmailID && int64(user.Emails[1].Id) != users[1].EmailID { | |
828 | t.Errorf("Expected result row ID to be either %d or %d, but was %d", user.Emails[0].Id, user.Emails[1].Id, users[1].EmailID) | |
829 | } | |
830 | } | |
831 | ||
566 | 832 | func TestJoinsWithSelect(t *testing.T) { |
567 | 833 | type result struct { |
568 | 834 | Name string |
577 | 843 | |
578 | 844 | var results []result |
579 | 845 | DB.Table("users").Select("name, emails.email").Joins("left join emails on emails.user_id = users.id").Where("name = ?", "joins_with_select").Scan(&results) |
846 | ||
847 | sort.Slice(results, func(i, j int) bool { | |
848 | return strings.Compare(results[i].Email, results[j].Email) < 0 | |
849 | }) | |
850 | ||
580 | 851 | if len(results) != 2 || results[0].Email != "join1@example.com" || results[1].Email != "join2@example.com" { |
581 | 852 | t.Errorf("Should find all two emails with Join select") |
582 | 853 | } |
861 | 1132 | } |
862 | 1133 | } |
863 | 1134 | |
1135 | func TestSaveAssociations(t *testing.T) { | |
1136 | db := DB.New() | |
1137 | deltaAddressCount := 0 | |
1138 | if err := db.Model(&Address{}).Count(&deltaAddressCount).Error; err != nil { | |
1139 | t.Errorf("failed to fetch address count") | |
1140 | t.FailNow() | |
1141 | } | |
1142 | ||
1143 | placeAddress := &Address{ | |
1144 | Address1: "somewhere on earth", | |
1145 | } | |
1146 | ownerAddress1 := &Address{ | |
1147 | Address1: "near place address", | |
1148 | } | |
1149 | ownerAddress2 := &Address{ | |
1150 | Address1: "address2", | |
1151 | } | |
1152 | db.Create(placeAddress) | |
1153 | ||
1154 | addressCountShouldBe := func(t *testing.T, expectedCount int) { | |
1155 | countFromDB := 0 | |
1156 | t.Helper() | |
1157 | err := db.Model(&Address{}).Count(&countFromDB).Error | |
1158 | if err != nil { | |
1159 | t.Error("failed to fetch address count") | |
1160 | } | |
1161 | if countFromDB != expectedCount { | |
1162 | t.Errorf("address count mismatch: %d", countFromDB) | |
1163 | } | |
1164 | } | |
1165 | addressCountShouldBe(t, deltaAddressCount+1) | |
1166 | ||
1167 | // owner address should be created, place address should be reused | |
1168 | place1 := &Place{ | |
1169 | PlaceAddressID: placeAddress.ID, | |
1170 | PlaceAddress: placeAddress, | |
1171 | OwnerAddress: ownerAddress1, | |
1172 | } | |
1173 | err := db.Create(place1).Error | |
1174 | if err != nil { | |
1175 | t.Errorf("failed to store place: %s", err.Error()) | |
1176 | } | |
1177 | addressCountShouldBe(t, deltaAddressCount+2) | |
1178 | ||
1179 | // owner address should be created again, place address should be reused | |
1180 | place2 := &Place{ | |
1181 | PlaceAddressID: placeAddress.ID, | |
1182 | PlaceAddress: &Address{ | |
1183 | ID: 777, | |
1184 | Address1: "address1", | |
1185 | }, | |
1186 | OwnerAddress: ownerAddress2, | |
1187 | OwnerAddressID: 778, | |
1188 | } | |
1189 | err = db.Create(place2).Error | |
1190 | if err != nil { | |
1191 | t.Errorf("failed to store place: %s", err.Error()) | |
1192 | } | |
1193 | addressCountShouldBe(t, deltaAddressCount+3) | |
1194 | ||
1195 | count := 0 | |
1196 | db.Model(&Place{}).Where(&Place{ | |
1197 | PlaceAddressID: placeAddress.ID, | |
1198 | OwnerAddressID: ownerAddress1.ID, | |
1199 | }).Count(&count) | |
1200 | if count != 1 { | |
1201 | t.Errorf("only one instance of (%d, %d) should be available, found: %d", | |
1202 | placeAddress.ID, ownerAddress1.ID, count) | |
1203 | } | |
1204 | ||
1205 | db.Model(&Place{}).Where(&Place{ | |
1206 | PlaceAddressID: placeAddress.ID, | |
1207 | OwnerAddressID: ownerAddress2.ID, | |
1208 | }).Count(&count) | |
1209 | if count != 1 { | |
1210 | t.Errorf("only one instance of (%d, %d) should be available, found: %d", | |
1211 | placeAddress.ID, ownerAddress2.ID, count) | |
1212 | } | |
1213 | ||
1214 | db.Model(&Place{}).Where(&Place{ | |
1215 | PlaceAddressID: placeAddress.ID, | |
1216 | }).Count(&count) | |
1217 | if count != 2 { | |
1218 | t.Errorf("two instances of (%d) should be available, found: %d", | |
1219 | placeAddress.ID, count) | |
1220 | } | |
1221 | } | |
1222 | ||
864 | 1223 | func TestBlockGlobalUpdate(t *testing.T) { |
865 | 1224 | db := DB.New() |
866 | 1225 | db.Create(&Toy{Name: "Stuffed Animal", OwnerType: "Nobody"}) |
897 | 1256 | if err != nil { |
898 | 1257 | t.Error("Unexpected error on conditional delete") |
899 | 1258 | } |
1259 | } | |
1260 | ||
1261 | func TestCountWithHaving(t *testing.T) { | |
1262 | db := DB.New() | |
1263 | db.Delete(User{}) | |
1264 | defer db.Delete(User{}) | |
1265 | ||
1266 | DB.Create(getPreparedUser("user1", "pluck_user")) | |
1267 | DB.Create(getPreparedUser("user2", "pluck_user")) | |
1268 | user3 := getPreparedUser("user3", "pluck_user") | |
1269 | user3.Languages = []Language{} | |
1270 | DB.Create(user3) | |
1271 | ||
1272 | var count int | |
1273 | err := db.Model(User{}).Select("users.id"). | |
1274 | Joins("LEFT JOIN user_languages ON user_languages.user_id = users.id"). | |
1275 | Joins("LEFT JOIN languages ON user_languages.language_id = languages.id"). | |
1276 | Group("users.id").Having("COUNT(languages.id) > 1").Count(&count).Error | |
1277 | ||
1278 | if err != nil { | |
1279 | t.Error("Unexpected error on query count with having") | |
1280 | } | |
1281 | ||
1282 | if count != 2 { | |
1283 | t.Error("Unexpected result on query count with having") | |
1284 | } | |
1285 | } | |
1286 | ||
1287 | func TestPluck(t *testing.T) { | |
1288 | db := DB.New() | |
1289 | db.Delete(User{}) | |
1290 | defer db.Delete(User{}) | |
1291 | ||
1292 | DB.Create(&User{Id: 1, Name: "user1"}) | |
1293 | DB.Create(&User{Id: 2, Name: "user2"}) | |
1294 | DB.Create(&User{Id: 3, Name: "user3"}) | |
1295 | ||
1296 | var ids []int64 | |
1297 | err := db.Model(User{}).Order("id").Pluck("id", &ids).Error | |
1298 | ||
1299 | if err != nil { | |
1300 | t.Error("Unexpected error on pluck") | |
1301 | } | |
1302 | ||
1303 | if len(ids) != 3 || ids[0] != 1 || ids[1] != 2 || ids[2] != 3 { | |
1304 | t.Error("Unexpected result on pluck") | |
1305 | } | |
1306 | ||
1307 | err = db.Model(User{}).Order("id").Pluck("id", &ids).Error | |
1308 | ||
1309 | if err != nil { | |
1310 | t.Error("Unexpected error on pluck again") | |
1311 | } | |
1312 | ||
1313 | if len(ids) != 3 || ids[0] != 1 || ids[1] != 2 || ids[2] != 3 { | |
1314 | t.Error("Unexpected result on pluck again") | |
1315 | } | |
1316 | } | |
1317 | ||
1318 | func TestCountWithQueryOption(t *testing.T) { | |
1319 | db := DB.New() | |
1320 | db.Delete(User{}) | |
1321 | defer db.Delete(User{}) | |
1322 | ||
1323 | DB.Create(&User{Name: "user1"}) | |
1324 | DB.Create(&User{Name: "user2"}) | |
1325 | DB.Create(&User{Name: "user3"}) | |
1326 | ||
1327 | var count int | |
1328 | err := db.Model(User{}).Select("users.id"). | |
1329 | Set("gorm:query_option", "WHERE users.name='user2'"). | |
1330 | Count(&count).Error | |
1331 | ||
1332 | if err != nil { | |
1333 | t.Error("Unexpected error on query count with query_option") | |
1334 | } | |
1335 | ||
1336 | if count != 1 { | |
1337 | t.Error("Unexpected result on query count with query_option") | |
1338 | } | |
1339 | } | |
1340 | ||
1341 | func TestQueryHint1(t *testing.T) { | |
1342 | db := DB.New() | |
1343 | ||
1344 | _, err := db.Model(User{}).Raw("select 1").Rows() | |
1345 | ||
1346 | if err != nil { | |
1347 | t.Error("Unexpected error on query count with query_option") | |
1348 | } | |
1349 | } | |
1350 | ||
1351 | func TestQueryHint2(t *testing.T) { | |
1352 | type TestStruct struct { | |
1353 | ID string `gorm:"primary_key"` | |
1354 | Name string | |
1355 | } | |
1356 | DB.DropTable(&TestStruct{}) | |
1357 | DB.AutoMigrate(&TestStruct{}) | |
1358 | ||
1359 | data := TestStruct{ID: "uuid", Name: "hello"} | |
1360 | if err := DB.Set("gorm:query_hint", "/*master*/").Save(&data).Error; err != nil { | |
1361 | t.Error("Unexpected error on query count with query_option") | |
1362 | } | |
1363 | } | |
1364 | ||
1365 | func TestFloatColumnPrecision(t *testing.T) { | |
1366 | if dialect := os.Getenv("GORM_DIALECT"); dialect != "mysql" && dialect != "sqlite" { | |
1367 | t.Skip() | |
1368 | } | |
1369 | ||
1370 | type FloatTest struct { | |
1371 | ID string `gorm:"primary_key"` | |
1372 | FloatValue float64 `gorm:"column:float_value" sql:"type:float(255,5);"` | |
1373 | } | |
1374 | DB.DropTable(&FloatTest{}) | |
1375 | DB.AutoMigrate(&FloatTest{}) | |
1376 | ||
1377 | data := FloatTest{ID: "uuid", FloatValue: 112.57315} | |
1378 | if err := DB.Save(&data).Error; err != nil || data.ID != "uuid" || data.FloatValue != 112.57315 { | |
1379 | t.Errorf("Float value should not lose precision") | |
1380 | } | |
1381 | } | |
1382 | ||
1383 | func TestWhereUpdates(t *testing.T) { | |
1384 | type OwnerEntity struct { | |
1385 | gorm.Model | |
1386 | OwnerID uint | |
1387 | OwnerType string | |
1388 | } | |
1389 | ||
1390 | type SomeEntity struct { | |
1391 | gorm.Model | |
1392 | Name string | |
1393 | OwnerEntity OwnerEntity `gorm:"polymorphic:Owner"` | |
1394 | } | |
1395 | ||
1396 | DB.DropTable(&SomeEntity{}) | |
1397 | DB.AutoMigrate(&SomeEntity{}) | |
1398 | ||
1399 | a := SomeEntity{Name: "test"} | |
1400 | DB.Model(&a).Where(a).Updates(SomeEntity{Name: "test2"}) | |
900 | 1401 | } |
901 | 1402 | |
902 | 1403 | func BenchmarkGorm(b *testing.B) { |
117 | 117 | Owner *User `sql:"-"` |
118 | 118 | } |
119 | 119 | |
120 | type Place struct { | |
121 | Id int64 | |
122 | PlaceAddressID int | |
123 | PlaceAddress *Address `gorm:"save_associations:false"` | |
124 | OwnerAddressID int | |
125 | OwnerAddress *Address `gorm:"save_associations:true"` | |
126 | } | |
127 | ||
120 | 128 | type EncryptedData []byte |
121 | 129 | |
122 | 130 | func (data *EncryptedData) Scan(value interface{}) error { |
283 | 291 | DB.Exec(fmt.Sprintf("drop table %v;", table)) |
284 | 292 | } |
285 | 293 | |
286 | values := []interface{}{&Short{}, &ReallyLongThingThatReferencesShort{}, &ReallyLongTableNameToTestMySQLNameLengthLimit{}, &NotSoLongTableName{}, &Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Hamster{}, &Toy{}, &ElementWithIgnoredField{}} | |
294 | values := []interface{}{&Short{}, &ReallyLongThingThatReferencesShort{}, &ReallyLongTableNameToTestMySQLNameLengthLimit{}, &NotSoLongTableName{}, &Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Hamster{}, &Toy{}, &ElementWithIgnoredField{}, &Place{}} | |
287 | 295 | for _, value := range values { |
288 | 296 | DB.DropTable(value) |
289 | 297 | } |
397 | 405 | } |
398 | 406 | } |
399 | 407 | |
408 | func TestCreateAndAutomigrateTransaction(t *testing.T) { | |
409 | tx := DB.Begin() | |
410 | ||
411 | func() { | |
412 | type Bar struct { | |
413 | ID uint | |
414 | } | |
415 | DB.DropTableIfExists(&Bar{}) | |
416 | ||
417 | if ok := DB.HasTable("bars"); ok { | |
418 | t.Errorf("Table should not exist, but does") | |
419 | } | |
420 | ||
421 | if ok := tx.HasTable("bars"); ok { | |
422 | t.Errorf("Table should not exist, but does") | |
423 | } | |
424 | }() | |
425 | ||
426 | func() { | |
427 | type Bar struct { | |
428 | Name string | |
429 | } | |
430 | err := tx.CreateTable(&Bar{}).Error | |
431 | ||
432 | if err != nil { | |
433 | t.Errorf("Should have been able to create the table, but couldn't: %s", err) | |
434 | } | |
435 | ||
436 | if ok := tx.HasTable(&Bar{}); !ok { | |
437 | t.Errorf("The transaction should be able to see the table") | |
438 | } | |
439 | }() | |
440 | ||
441 | func() { | |
442 | type Bar struct { | |
443 | Stuff string | |
444 | } | |
445 | ||
446 | err := tx.AutoMigrate(&Bar{}).Error | |
447 | if err != nil { | |
448 | t.Errorf("Should have been able to alter the table, but couldn't") | |
449 | } | |
450 | }() | |
451 | ||
452 | tx.Rollback() | |
453 | } | |
454 | ||
400 | 455 | type MultipleIndexes struct { |
401 | 456 | ID int64 |
402 | 457 | UserID int64 `sql:"unique_index:uix_multipleindexes_user_name,uix_multipleindexes_user_email;index:idx_multipleindexes_user_other"` |
482 | 537 | t.Errorf("No error should happen when ModifyColumn, but got %v", err) |
483 | 538 | } |
484 | 539 | } |
540 | ||
541 | func TestIndexWithPrefixLength(t *testing.T) { | |
542 | if dialect := os.Getenv("GORM_DIALECT"); dialect != "mysql" { | |
543 | t.Skip("Skipping this because only mysql support setting an index prefix length") | |
544 | } | |
545 | ||
546 | type IndexWithPrefix struct { | |
547 | gorm.Model | |
548 | Name string | |
549 | Description string `gorm:"type:text;index:idx_index_with_prefixes_length(100)"` | |
550 | } | |
551 | type IndexesWithPrefix struct { | |
552 | gorm.Model | |
553 | Name string | |
554 | Description1 string `gorm:"type:text;index:idx_index_with_prefixes_length(100)"` | |
555 | Description2 string `gorm:"type:text;index:idx_index_with_prefixes_length(100)"` | |
556 | } | |
557 | type IndexesWithPrefixAndWithoutPrefix struct { | |
558 | gorm.Model | |
559 | Name string `gorm:"index:idx_index_with_prefixes_length"` | |
560 | Description string `gorm:"type:text;index:idx_index_with_prefixes_length(100)"` | |
561 | } | |
562 | tables := []interface{}{&IndexWithPrefix{}, &IndexesWithPrefix{}, &IndexesWithPrefixAndWithoutPrefix{}} | |
563 | for _, table := range tables { | |
564 | scope := DB.NewScope(table) | |
565 | tableName := scope.TableName() | |
566 | t.Run(fmt.Sprintf("Create index with prefix length: %s", tableName), func(t *testing.T) { | |
567 | if err := DB.DropTableIfExists(table).Error; err != nil { | |
568 | t.Errorf("Failed to drop %s table: %v", tableName, err) | |
569 | } | |
570 | if err := DB.CreateTable(table).Error; err != nil { | |
571 | t.Errorf("Failed to create %s table: %v", tableName, err) | |
572 | } | |
573 | if !scope.Dialect().HasIndex(tableName, "idx_index_with_prefixes_length") { | |
574 | t.Errorf("Failed to create %s table index:", tableName) | |
575 | } | |
576 | }) | |
577 | } | |
578 | } |
16 | 16 | return defaultTableName |
17 | 17 | } |
18 | 18 | |
19 | type safeModelStructsMap struct { | |
20 | m map[reflect.Type]*ModelStruct | |
21 | l *sync.RWMutex | |
22 | } | |
23 | ||
24 | func (s *safeModelStructsMap) Set(key reflect.Type, value *ModelStruct) { | |
19 | // lock for mutating global cached model metadata | |
20 | var structsLock sync.Mutex | |
21 | ||
22 | // global cache of model metadata | |
23 | var modelStructsMap sync.Map | |
24 | ||
25 | // ModelStruct model definition | |
26 | type ModelStruct struct { | |
27 | PrimaryFields []*StructField | |
28 | StructFields []*StructField | |
29 | ModelType reflect.Type | |
30 | ||
31 | defaultTableName string | |
32 | l sync.Mutex | |
33 | } | |
34 | ||
35 | // TableName returns model's table name | |
36 | func (s *ModelStruct) TableName(db *DB) string { | |
25 | 37 | s.l.Lock() |
26 | 38 | defer s.l.Unlock() |
27 | s.m[key] = value | |
28 | } | |
29 | ||
30 | func (s *safeModelStructsMap) Get(key reflect.Type) *ModelStruct { | |
31 | s.l.RLock() | |
32 | defer s.l.RUnlock() | |
33 | return s.m[key] | |
34 | } | |
35 | ||
36 | func newModelStructsMap() *safeModelStructsMap { | |
37 | return &safeModelStructsMap{l: new(sync.RWMutex), m: make(map[reflect.Type]*ModelStruct)} | |
38 | } | |
39 | ||
40 | var modelStructsMap = newModelStructsMap() | |
41 | ||
42 | // ModelStruct model definition | |
43 | type ModelStruct struct { | |
44 | PrimaryFields []*StructField | |
45 | StructFields []*StructField | |
46 | ModelType reflect.Type | |
47 | defaultTableName string | |
48 | } | |
49 | ||
50 | // TableName get model's table name | |
51 | func (s *ModelStruct) TableName(db *DB) string { | |
39 | ||
52 | 40 | if s.defaultTableName == "" && db != nil && s.ModelType != nil { |
53 | 41 | // Set default table name |
54 | 42 | if tabler, ok := reflect.New(s.ModelType).Interface().(tabler); ok { |
55 | 43 | s.defaultTableName = tabler.TableName() |
56 | 44 | } else { |
57 | tableName := ToDBName(s.ModelType.Name()) | |
58 | if db == nil || !db.parent.singularTable { | |
45 | tableName := ToTableName(s.ModelType.Name()) | |
46 | db.parent.RLock() | |
47 | if db == nil || (db.parent != nil && !db.parent.singularTable) { | |
59 | 48 | tableName = inflection.Plural(tableName) |
60 | 49 | } |
50 | db.parent.RUnlock() | |
61 | 51 | s.defaultTableName = tableName |
62 | 52 | } |
63 | 53 | } |
80 | 70 | Struct reflect.StructField |
81 | 71 | IsForeignKey bool |
82 | 72 | Relationship *Relationship |
83 | } | |
84 | ||
85 | func (structField *StructField) clone() *StructField { | |
73 | ||
74 | tagSettingsLock sync.RWMutex | |
75 | } | |
76 | ||
77 | // TagSettingsSet Sets a tag in the tag settings map | |
78 | func (sf *StructField) TagSettingsSet(key, val string) { | |
79 | sf.tagSettingsLock.Lock() | |
80 | defer sf.tagSettingsLock.Unlock() | |
81 | sf.TagSettings[key] = val | |
82 | } | |
83 | ||
84 | // TagSettingsGet returns a tag from the tag settings | |
85 | func (sf *StructField) TagSettingsGet(key string) (string, bool) { | |
86 | sf.tagSettingsLock.RLock() | |
87 | defer sf.tagSettingsLock.RUnlock() | |
88 | val, ok := sf.TagSettings[key] | |
89 | return val, ok | |
90 | } | |
91 | ||
92 | // TagSettingsDelete deletes a tag | |
93 | func (sf *StructField) TagSettingsDelete(key string) { | |
94 | sf.tagSettingsLock.Lock() | |
95 | defer sf.tagSettingsLock.Unlock() | |
96 | delete(sf.TagSettings, key) | |
97 | } | |
98 | ||
99 | func (sf *StructField) clone() *StructField { | |
86 | 100 | clone := &StructField{ |
87 | DBName: structField.DBName, | |
88 | Name: structField.Name, | |
89 | Names: structField.Names, | |
90 | IsPrimaryKey: structField.IsPrimaryKey, | |
91 | IsNormal: structField.IsNormal, | |
92 | IsIgnored: structField.IsIgnored, | |
93 | IsScanner: structField.IsScanner, | |
94 | HasDefaultValue: structField.HasDefaultValue, | |
95 | Tag: structField.Tag, | |
101 | DBName: sf.DBName, | |
102 | Name: sf.Name, | |
103 | Names: sf.Names, | |
104 | IsPrimaryKey: sf.IsPrimaryKey, | |
105 | IsNormal: sf.IsNormal, | |
106 | IsIgnored: sf.IsIgnored, | |
107 | IsScanner: sf.IsScanner, | |
108 | HasDefaultValue: sf.HasDefaultValue, | |
109 | Tag: sf.Tag, | |
96 | 110 | TagSettings: map[string]string{}, |
97 | Struct: structField.Struct, | |
98 | IsForeignKey: structField.IsForeignKey, | |
99 | } | |
100 | ||
101 | if structField.Relationship != nil { | |
102 | relationship := *structField.Relationship | |
111 | Struct: sf.Struct, | |
112 | IsForeignKey: sf.IsForeignKey, | |
113 | } | |
114 | ||
115 | if sf.Relationship != nil { | |
116 | relationship := *sf.Relationship | |
103 | 117 | clone.Relationship = &relationship |
104 | 118 | } |
105 | 119 | |
106 | for key, value := range structField.TagSettings { | |
120 | // copy the struct field tagSettings, they should be read-locked while they are copied | |
121 | sf.tagSettingsLock.Lock() | |
122 | defer sf.tagSettingsLock.Unlock() | |
123 | for key, value := range sf.TagSettings { | |
107 | 124 | clone.TagSettings[key] = value |
108 | 125 | } |
109 | 126 | |
125 | 142 | |
126 | 143 | func getForeignField(column string, fields []*StructField) *StructField { |
127 | 144 | for _, field := range fields { |
128 | if field.Name == column || field.DBName == column || field.DBName == ToDBName(column) { | |
145 | if field.Name == column || field.DBName == column || field.DBName == ToColumnName(column) { | |
129 | 146 | return field |
130 | 147 | } |
131 | 148 | } |
134 | 151 | |
135 | 152 | // GetModelStruct get value's model struct, relationships based on struct and tag definition |
136 | 153 | func (scope *Scope) GetModelStruct() *ModelStruct { |
154 | return scope.getModelStruct(scope, make([]*StructField, 0)) | |
155 | } | |
156 | ||
157 | func (scope *Scope) getModelStruct(rootScope *Scope, allFields []*StructField) *ModelStruct { | |
137 | 158 | var modelStruct ModelStruct |
138 | 159 | // Scope value can't be nil |
139 | 160 | if scope.Value == nil { |
151 | 172 | } |
152 | 173 | |
153 | 174 | // Get Cached model struct |
154 | if value := modelStructsMap.Get(reflectType); value != nil { | |
155 | return value | |
175 | isSingularTable := false | |
176 | if scope.db != nil && scope.db.parent != nil { | |
177 | scope.db.parent.RLock() | |
178 | isSingularTable = scope.db.parent.singularTable | |
179 | scope.db.parent.RUnlock() | |
180 | } | |
181 | ||
182 | hashKey := struct { | |
183 | singularTable bool | |
184 | reflectType reflect.Type | |
185 | }{isSingularTable, reflectType} | |
186 | if value, ok := modelStructsMap.Load(hashKey); ok && value != nil { | |
187 | return value.(*ModelStruct) | |
156 | 188 | } |
157 | 189 | |
158 | 190 | modelStruct.ModelType = reflectType |
169 | 201 | } |
170 | 202 | |
171 | 203 | // is ignored field |
172 | if _, ok := field.TagSettings["-"]; ok { | |
204 | if _, ok := field.TagSettingsGet("-"); ok { | |
173 | 205 | field.IsIgnored = true |
174 | 206 | } else { |
175 | if _, ok := field.TagSettings["PRIMARY_KEY"]; ok { | |
207 | if _, ok := field.TagSettingsGet("PRIMARY_KEY"); ok { | |
176 | 208 | field.IsPrimaryKey = true |
177 | 209 | modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field) |
178 | 210 | } |
179 | 211 | |
180 | if _, ok := field.TagSettings["DEFAULT"]; ok { | |
212 | if _, ok := field.TagSettingsGet("DEFAULT"); ok && !field.IsPrimaryKey { | |
181 | 213 | field.HasDefaultValue = true |
182 | 214 | } |
183 | 215 | |
184 | if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok && !field.IsPrimaryKey { | |
216 | if _, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok && !field.IsPrimaryKey { | |
185 | 217 | field.HasDefaultValue = true |
186 | 218 | } |
187 | 219 | |
197 | 229 | if indirectType.Kind() == reflect.Struct { |
198 | 230 | for i := 0; i < indirectType.NumField(); i++ { |
199 | 231 | for key, value := range parseTagSetting(indirectType.Field(i).Tag) { |
200 | if _, ok := field.TagSettings[key]; !ok { | |
201 | field.TagSettings[key] = value | |
232 | if _, ok := field.TagSettingsGet(key); !ok { | |
233 | field.TagSettingsSet(key, value) | |
202 | 234 | } |
203 | 235 | } |
204 | 236 | } |
206 | 238 | } else if _, isTime := fieldValue.(*time.Time); isTime { |
207 | 239 | // is time |
208 | 240 | field.IsNormal = true |
209 | } else if _, ok := field.TagSettings["EMBEDDED"]; ok || fieldStruct.Anonymous { | |
241 | } else if _, ok := field.TagSettingsGet("EMBEDDED"); ok || fieldStruct.Anonymous { | |
210 | 242 | // is embedded struct |
211 | for _, subField := range scope.New(fieldValue).GetModelStruct().StructFields { | |
243 | for _, subField := range scope.New(fieldValue).getModelStruct(rootScope, allFields).StructFields { | |
212 | 244 | subField = subField.clone() |
213 | 245 | subField.Names = append([]string{fieldStruct.Name}, subField.Names...) |
214 | if prefix, ok := field.TagSettings["EMBEDDED_PREFIX"]; ok { | |
246 | if prefix, ok := field.TagSettingsGet("EMBEDDED_PREFIX"); ok { | |
215 | 247 | subField.DBName = prefix + subField.DBName |
216 | 248 | } |
217 | 249 | |
218 | 250 | if subField.IsPrimaryKey { |
219 | if _, ok := subField.TagSettings["PRIMARY_KEY"]; ok { | |
251 | if _, ok := subField.TagSettingsGet("PRIMARY_KEY"); ok { | |
220 | 252 | modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, subField) |
221 | 253 | } else { |
222 | 254 | subField.IsPrimaryKey = false |
232 | 264 | } |
233 | 265 | |
234 | 266 | modelStruct.StructFields = append(modelStruct.StructFields, subField) |
267 | allFields = append(allFields, subField) | |
235 | 268 | } |
236 | 269 | continue |
237 | 270 | } else { |
247 | 280 | elemType = field.Struct.Type |
248 | 281 | ) |
249 | 282 | |
250 | if foreignKey := field.TagSettings["FOREIGNKEY"]; foreignKey != "" { | |
283 | if foreignKey, _ := field.TagSettingsGet("FOREIGNKEY"); foreignKey != "" { | |
251 | 284 | foreignKeys = strings.Split(foreignKey, ",") |
252 | 285 | } |
253 | 286 | |
254 | if foreignKey := field.TagSettings["ASSOCIATION_FOREIGNKEY"]; foreignKey != "" { | |
287 | if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_FOREIGNKEY"); foreignKey != "" { | |
255 | 288 | associationForeignKeys = strings.Split(foreignKey, ",") |
256 | } else if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" { | |
289 | } else if foreignKey, _ := field.TagSettingsGet("ASSOCIATIONFOREIGNKEY"); foreignKey != "" { | |
257 | 290 | associationForeignKeys = strings.Split(foreignKey, ",") |
258 | 291 | } |
259 | 292 | |
262 | 295 | } |
263 | 296 | |
264 | 297 | if elemType.Kind() == reflect.Struct { |
265 | if many2many := field.TagSettings["MANY2MANY"]; many2many != "" { | |
298 | if many2many, _ := field.TagSettingsGet("MANY2MANY"); many2many != "" { | |
266 | 299 | relationship.Kind = "many_to_many" |
267 | 300 | |
268 | 301 | { // Foreign Keys for Source |
269 | 302 | joinTableDBNames := []string{} |
270 | 303 | |
271 | if foreignKey := field.TagSettings["JOINTABLE_FOREIGNKEY"]; foreignKey != "" { | |
304 | if foreignKey, _ := field.TagSettingsGet("JOINTABLE_FOREIGNKEY"); foreignKey != "" { | |
272 | 305 | joinTableDBNames = strings.Split(foreignKey, ",") |
273 | 306 | } |
274 | 307 | |
289 | 322 | // if defined join table's foreign key |
290 | 323 | relationship.ForeignDBNames = append(relationship.ForeignDBNames, joinTableDBNames[idx]) |
291 | 324 | } else { |
292 | defaultJointableForeignKey := ToDBName(reflectType.Name()) + "_" + foreignField.DBName | |
325 | defaultJointableForeignKey := ToColumnName(reflectType.Name()) + "_" + foreignField.DBName | |
293 | 326 | relationship.ForeignDBNames = append(relationship.ForeignDBNames, defaultJointableForeignKey) |
294 | 327 | } |
295 | 328 | } |
299 | 332 | { // Foreign Keys for Association (Destination) |
300 | 333 | associationJoinTableDBNames := []string{} |
301 | 334 | |
302 | if foreignKey := field.TagSettings["ASSOCIATION_JOINTABLE_FOREIGNKEY"]; foreignKey != "" { | |
335 | if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_JOINTABLE_FOREIGNKEY"); foreignKey != "" { | |
303 | 336 | associationJoinTableDBNames = strings.Split(foreignKey, ",") |
304 | 337 | } |
305 | 338 | |
320 | 353 | relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationJoinTableDBNames[idx]) |
321 | 354 | } else { |
322 | 355 | // join table foreign keys for association |
323 | joinTableDBName := ToDBName(elemType.Name()) + "_" + field.DBName | |
356 | joinTableDBName := ToColumnName(elemType.Name()) + "_" + field.DBName | |
324 | 357 | relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, joinTableDBName) |
325 | 358 | } |
326 | 359 | } |
337 | 370 | var toFields = toScope.GetStructFields() |
338 | 371 | relationship.Kind = "has_many" |
339 | 372 | |
340 | if polymorphic := field.TagSettings["POLYMORPHIC"]; polymorphic != "" { | |
373 | if polymorphic, _ := field.TagSettingsGet("POLYMORPHIC"); polymorphic != "" { | |
341 | 374 | // Dog has many toys, tag polymorphic is Owner, then associationType is Owner |
342 | 375 | // Toy use OwnerID, OwnerType ('dogs') as foreign key |
343 | 376 | if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil { |
345 | 378 | relationship.PolymorphicType = polymorphicType.Name |
346 | 379 | relationship.PolymorphicDBName = polymorphicType.DBName |
347 | 380 | // if Dog has multiple set of toys set name of the set (instead of default 'dogs') |
348 | if value, ok := field.TagSettings["POLYMORPHIC_VALUE"]; ok { | |
381 | if value, ok := field.TagSettingsGet("POLYMORPHIC_VALUE"); ok { | |
349 | 382 | relationship.PolymorphicValue = value |
350 | 383 | } else { |
351 | 384 | relationship.PolymorphicValue = scope.TableName() |
365 | 398 | } else { |
366 | 399 | // generate foreign keys from defined association foreign keys |
367 | 400 | for _, scopeFieldName := range associationForeignKeys { |
368 | if foreignField := getForeignField(scopeFieldName, modelStruct.StructFields); foreignField != nil { | |
401 | if foreignField := getForeignField(scopeFieldName, allFields); foreignField != nil { | |
369 | 402 | foreignKeys = append(foreignKeys, associationType+foreignField.Name) |
370 | 403 | associationForeignKeys = append(associationForeignKeys, foreignField.Name) |
371 | 404 | } |
377 | 410 | for _, foreignKey := range foreignKeys { |
378 | 411 | if strings.HasPrefix(foreignKey, associationType) { |
379 | 412 | associationForeignKey := strings.TrimPrefix(foreignKey, associationType) |
380 | if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil { | |
413 | if foreignField := getForeignField(associationForeignKey, allFields); foreignField != nil { | |
381 | 414 | associationForeignKeys = append(associationForeignKeys, associationForeignKey) |
382 | 415 | } |
383 | 416 | } |
384 | 417 | } |
385 | 418 | if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 { |
386 | associationForeignKeys = []string{scope.PrimaryKey()} | |
419 | associationForeignKeys = []string{rootScope.PrimaryKey()} | |
387 | 420 | } |
388 | 421 | } else if len(foreignKeys) != len(associationForeignKeys) { |
389 | 422 | scope.Err(errors.New("invalid foreign keys, should have same length")) |
393 | 426 | |
394 | 427 | for idx, foreignKey := range foreignKeys { |
395 | 428 | if foreignField := getForeignField(foreignKey, toFields); foreignField != nil { |
396 | if associationField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); associationField != nil { | |
397 | // source foreign keys | |
429 | if associationField := getForeignField(associationForeignKeys[idx], allFields); associationField != nil { | |
430 | // mark field as foreignkey, use global lock to avoid race | |
431 | structsLock.Lock() | |
398 | 432 | foreignField.IsForeignKey = true |
433 | structsLock.Unlock() | |
434 | ||
435 | // association foreign keys | |
399 | 436 | relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name) |
400 | 437 | relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationField.DBName) |
401 | 438 | |
427 | 464 | tagAssociationForeignKeys []string |
428 | 465 | ) |
429 | 466 | |
430 | if foreignKey := field.TagSettings["FOREIGNKEY"]; foreignKey != "" { | |
467 | if foreignKey, _ := field.TagSettingsGet("FOREIGNKEY"); foreignKey != "" { | |
431 | 468 | tagForeignKeys = strings.Split(foreignKey, ",") |
432 | 469 | } |
433 | 470 | |
434 | if foreignKey := field.TagSettings["ASSOCIATION_FOREIGNKEY"]; foreignKey != "" { | |
471 | if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_FOREIGNKEY"); foreignKey != "" { | |
435 | 472 | tagAssociationForeignKeys = strings.Split(foreignKey, ",") |
436 | } else if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" { | |
473 | } else if foreignKey, _ := field.TagSettingsGet("ASSOCIATIONFOREIGNKEY"); foreignKey != "" { | |
437 | 474 | tagAssociationForeignKeys = strings.Split(foreignKey, ",") |
438 | 475 | } |
439 | 476 | |
440 | if polymorphic := field.TagSettings["POLYMORPHIC"]; polymorphic != "" { | |
477 | if polymorphic, _ := field.TagSettingsGet("POLYMORPHIC"); polymorphic != "" { | |
441 | 478 | // Cat has one toy, tag polymorphic is Owner, then associationType is Owner |
442 | 479 | // Toy use OwnerID, OwnerType ('cats') as foreign key |
443 | 480 | if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil { |
445 | 482 | relationship.PolymorphicType = polymorphicType.Name |
446 | 483 | relationship.PolymorphicDBName = polymorphicType.DBName |
447 | 484 | // if Cat has several different types of toys set name for each (instead of default 'cats') |
448 | if value, ok := field.TagSettings["POLYMORPHIC_VALUE"]; ok { | |
485 | if value, ok := field.TagSettingsGet("POLYMORPHIC_VALUE"); ok { | |
449 | 486 | relationship.PolymorphicValue = value |
450 | 487 | } else { |
451 | 488 | relationship.PolymorphicValue = scope.TableName() |
469 | 506 | } else { |
470 | 507 | // generate foreign keys form association foreign keys |
471 | 508 | for _, associationForeignKey := range tagAssociationForeignKeys { |
472 | if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil { | |
509 | if foreignField := getForeignField(associationForeignKey, allFields); foreignField != nil { | |
473 | 510 | foreignKeys = append(foreignKeys, associationType+foreignField.Name) |
474 | 511 | associationForeignKeys = append(associationForeignKeys, foreignField.Name) |
475 | 512 | } |
481 | 518 | for _, foreignKey := range foreignKeys { |
482 | 519 | if strings.HasPrefix(foreignKey, associationType) { |
483 | 520 | associationForeignKey := strings.TrimPrefix(foreignKey, associationType) |
484 | if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil { | |
521 | if foreignField := getForeignField(associationForeignKey, allFields); foreignField != nil { | |
485 | 522 | associationForeignKeys = append(associationForeignKeys, associationForeignKey) |
486 | 523 | } |
487 | 524 | } |
488 | 525 | } |
489 | 526 | if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 { |
490 | associationForeignKeys = []string{scope.PrimaryKey()} | |
527 | associationForeignKeys = []string{rootScope.PrimaryKey()} | |
491 | 528 | } |
492 | 529 | } else if len(foreignKeys) != len(associationForeignKeys) { |
493 | 530 | scope.Err(errors.New("invalid foreign keys, should have same length")) |
497 | 534 | |
498 | 535 | for idx, foreignKey := range foreignKeys { |
499 | 536 | if foreignField := getForeignField(foreignKey, toFields); foreignField != nil { |
500 | if scopeField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); scopeField != nil { | |
537 | if scopeField := getForeignField(associationForeignKeys[idx], allFields); scopeField != nil { | |
538 | // mark field as foreignkey, use global lock to avoid race | |
539 | structsLock.Lock() | |
501 | 540 | foreignField.IsForeignKey = true |
502 | // source foreign keys | |
541 | structsLock.Unlock() | |
542 | ||
543 | // association foreign keys | |
503 | 544 | relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scopeField.Name) |
504 | 545 | relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scopeField.DBName) |
505 | 546 | |
557 | 598 | for idx, foreignKey := range foreignKeys { |
558 | 599 | if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil { |
559 | 600 | if associationField := getForeignField(associationForeignKeys[idx], toFields); associationField != nil { |
601 | // mark field as foreignkey, use global lock to avoid race | |
602 | structsLock.Lock() | |
560 | 603 | foreignField.IsForeignKey = true |
604 | structsLock.Unlock() | |
561 | 605 | |
562 | 606 | // association foreign keys |
563 | 607 | relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name) |
583 | 627 | } |
584 | 628 | |
585 | 629 | // Even it is ignored, also possible to decode db value into the field |
586 | if value, ok := field.TagSettings["COLUMN"]; ok { | |
630 | if value, ok := field.TagSettingsGet("COLUMN"); ok { | |
587 | 631 | field.DBName = value |
588 | 632 | } else { |
589 | field.DBName = ToDBName(fieldStruct.Name) | |
633 | field.DBName = ToColumnName(fieldStruct.Name) | |
590 | 634 | } |
591 | 635 | |
592 | 636 | modelStruct.StructFields = append(modelStruct.StructFields, field) |
637 | allFields = append(allFields, field) | |
593 | 638 | } |
594 | 639 | } |
595 | 640 | |
600 | 645 | } |
601 | 646 | } |
602 | 647 | |
603 | modelStructsMap.Set(reflectType, &modelStruct) | |
648 | modelStructsMap.Store(hashKey, &modelStruct) | |
604 | 649 | |
605 | 650 | return &modelStruct |
606 | 651 | } |
613 | 658 | func parseTagSetting(tags reflect.StructTag) map[string]string { |
614 | 659 | setting := map[string]string{} |
615 | 660 | for _, str := range []string{tags.Get("sql"), tags.Get("gorm")} { |
661 | if str == "" { | |
662 | continue | |
663 | } | |
616 | 664 | tags := strings.Split(str, ";") |
617 | 665 | for _, value := range tags { |
618 | 666 | v := strings.Split(value, ":") |
0 | package gorm_test | |
1 | ||
2 | import ( | |
3 | "sync" | |
4 | "testing" | |
5 | ||
6 | "github.com/jinzhu/gorm" | |
7 | ) | |
8 | ||
9 | type ModelA struct { | |
10 | gorm.Model | |
11 | Name string | |
12 | ||
13 | ModelCs []ModelC `gorm:"foreignkey:OtherAID"` | |
14 | } | |
15 | ||
16 | type ModelB struct { | |
17 | gorm.Model | |
18 | Name string | |
19 | ||
20 | ModelCs []ModelC `gorm:"foreignkey:OtherBID"` | |
21 | } | |
22 | ||
23 | type ModelC struct { | |
24 | gorm.Model | |
25 | Name string | |
26 | ||
27 | OtherAID uint64 | |
28 | OtherA *ModelA `gorm:"foreignkey:OtherAID"` | |
29 | OtherBID uint64 | |
30 | OtherB *ModelB `gorm:"foreignkey:OtherBID"` | |
31 | } | |
32 | ||
33 | type RequestModel struct { | |
34 | Name string | |
35 | Children []ChildModel `gorm:"foreignkey:ParentID"` | |
36 | } | |
37 | ||
38 | type ChildModel struct { | |
39 | ID string | |
40 | ParentID string | |
41 | Name string | |
42 | } | |
43 | ||
44 | type ResponseModel struct { | |
45 | gorm.Model | |
46 | RequestModel | |
47 | } | |
48 | ||
49 | // This test will try to cause a race condition on the model's foreignkey metadata | |
50 | func TestModelStructRaceSameModel(t *testing.T) { | |
51 | // use a WaitGroup to execute as much in-sync as possible | |
52 | // it's more likely to hit a race condition than without | |
53 | n := 32 | |
54 | start := sync.WaitGroup{} | |
55 | start.Add(n) | |
56 | ||
57 | // use another WaitGroup to know when the test is done | |
58 | done := sync.WaitGroup{} | |
59 | done.Add(n) | |
60 | ||
61 | for i := 0; i < n; i++ { | |
62 | go func() { | |
63 | start.Wait() | |
64 | ||
65 | // call GetStructFields, this had a race condition before we fixed it | |
66 | DB.NewScope(&ModelA{}).GetStructFields() | |
67 | ||
68 | done.Done() | |
69 | }() | |
70 | ||
71 | start.Done() | |
72 | } | |
73 | ||
74 | done.Wait() | |
75 | } | |
76 | ||
77 | // This test will try to cause a race condition on the model's foreignkey metadata | |
78 | func TestModelStructRaceDifferentModel(t *testing.T) { | |
79 | // use a WaitGroup to execute as much in-sync as possible | |
80 | // it's more likely to hit a race condition than without | |
81 | n := 32 | |
82 | start := sync.WaitGroup{} | |
83 | start.Add(n) | |
84 | ||
85 | // use another WaitGroup to know when the test is done | |
86 | done := sync.WaitGroup{} | |
87 | done.Add(n) | |
88 | ||
89 | for i := 0; i < n; i++ { | |
90 | i := i | |
91 | go func() { | |
92 | start.Wait() | |
93 | ||
94 | // call GetStructFields, this had a race condition before we fixed it | |
95 | if i%2 == 0 { | |
96 | DB.NewScope(&ModelA{}).GetStructFields() | |
97 | } else { | |
98 | DB.NewScope(&ModelB{}).GetStructFields() | |
99 | } | |
100 | ||
101 | done.Done() | |
102 | }() | |
103 | ||
104 | start.Done() | |
105 | } | |
106 | ||
107 | done.Wait() | |
108 | } | |
109 | ||
110 | func TestModelStructEmbeddedHasMany(t *testing.T) { | |
111 | fields := DB.NewScope(&ResponseModel{}).GetStructFields() | |
112 | ||
113 | var childrenField *gorm.StructField | |
114 | ||
115 | for i := 0; i < len(fields); i++ { | |
116 | field := fields[i] | |
117 | ||
118 | if field != nil && field.Name == "Children" { | |
119 | childrenField = field | |
120 | } | |
121 | } | |
122 | ||
123 | if childrenField == nil { | |
124 | t.Error("childrenField should not be nil") | |
125 | return | |
126 | } | |
127 | ||
128 | if childrenField.Relationship == nil { | |
129 | t.Error("childrenField.Relation should not be nil") | |
130 | return | |
131 | } | |
132 | ||
133 | expected := "has_many" | |
134 | actual := childrenField.Relationship.Kind | |
135 | ||
136 | if actual != expected { | |
137 | t.Errorf("childrenField.Relationship.Kind should be %v, but was %v", expected, actual) | |
138 | } | |
139 | } |
0 | package gorm | |
1 | ||
2 | import ( | |
3 | "bytes" | |
4 | "strings" | |
5 | ) | |
6 | ||
7 | // Namer is a function type which is given a string and return a string | |
8 | type Namer func(string) string | |
9 | ||
10 | // NamingStrategy represents naming strategies | |
11 | type NamingStrategy struct { | |
12 | DB Namer | |
13 | Table Namer | |
14 | Column Namer | |
15 | } | |
16 | ||
17 | // TheNamingStrategy is being initialized with defaultNamingStrategy | |
18 | var TheNamingStrategy = &NamingStrategy{ | |
19 | DB: defaultNamer, | |
20 | Table: defaultNamer, | |
21 | Column: defaultNamer, | |
22 | } | |
23 | ||
24 | // AddNamingStrategy sets the naming strategy | |
25 | func AddNamingStrategy(ns *NamingStrategy) { | |
26 | if ns.DB == nil { | |
27 | ns.DB = defaultNamer | |
28 | } | |
29 | if ns.Table == nil { | |
30 | ns.Table = defaultNamer | |
31 | } | |
32 | if ns.Column == nil { | |
33 | ns.Column = defaultNamer | |
34 | } | |
35 | TheNamingStrategy = ns | |
36 | } | |
37 | ||
38 | // DBName alters the given name by DB | |
39 | func (ns *NamingStrategy) DBName(name string) string { | |
40 | return ns.DB(name) | |
41 | } | |
42 | ||
43 | // TableName alters the given name by Table | |
44 | func (ns *NamingStrategy) TableName(name string) string { | |
45 | return ns.Table(name) | |
46 | } | |
47 | ||
48 | // ColumnName alters the given name by Column | |
49 | func (ns *NamingStrategy) ColumnName(name string) string { | |
50 | return ns.Column(name) | |
51 | } | |
52 | ||
53 | // ToDBName convert string to db name | |
54 | func ToDBName(name string) string { | |
55 | return TheNamingStrategy.DBName(name) | |
56 | } | |
57 | ||
58 | // ToTableName convert string to table name | |
59 | func ToTableName(name string) string { | |
60 | return TheNamingStrategy.TableName(name) | |
61 | } | |
62 | ||
63 | // ToColumnName convert string to db name | |
64 | func ToColumnName(name string) string { | |
65 | return TheNamingStrategy.ColumnName(name) | |
66 | } | |
67 | ||
68 | var smap = newSafeMap() | |
69 | ||
70 | func defaultNamer(name string) string { | |
71 | const ( | |
72 | lower = false | |
73 | upper = true | |
74 | ) | |
75 | ||
76 | if v := smap.Get(name); v != "" { | |
77 | return v | |
78 | } | |
79 | ||
80 | if name == "" { | |
81 | return "" | |
82 | } | |
83 | ||
84 | var ( | |
85 | value = commonInitialismsReplacer.Replace(name) | |
86 | buf = bytes.NewBufferString("") | |
87 | lastCase, currCase, nextCase, nextNumber bool | |
88 | ) | |
89 | ||
90 | for i, v := range value[:len(value)-1] { | |
91 | nextCase = bool(value[i+1] >= 'A' && value[i+1] <= 'Z') | |
92 | nextNumber = bool(value[i+1] >= '0' && value[i+1] <= '9') | |
93 | ||
94 | if i > 0 { | |
95 | if currCase == upper { | |
96 | if lastCase == upper && (nextCase == upper || nextNumber == upper) { | |
97 | buf.WriteRune(v) | |
98 | } else { | |
99 | if value[i-1] != '_' && value[i+1] != '_' { | |
100 | buf.WriteRune('_') | |
101 | } | |
102 | buf.WriteRune(v) | |
103 | } | |
104 | } else { | |
105 | buf.WriteRune(v) | |
106 | if i == len(value)-2 && (nextCase == upper && nextNumber == lower) { | |
107 | buf.WriteRune('_') | |
108 | } | |
109 | } | |
110 | } else { | |
111 | currCase = upper | |
112 | buf.WriteRune(v) | |
113 | } | |
114 | lastCase = currCase | |
115 | currCase = nextCase | |
116 | } | |
117 | ||
118 | buf.WriteByte(value[len(value)-1]) | |
119 | ||
120 | s := strings.ToLower(buf.String()) | |
121 | smap.Set(name, s) | |
122 | return s | |
123 | } |
0 | package gorm_test | |
1 | ||
2 | import ( | |
3 | "testing" | |
4 | ||
5 | "github.com/jinzhu/gorm" | |
6 | ) | |
7 | ||
8 | func TestTheNamingStrategy(t *testing.T) { | |
9 | ||
10 | cases := []struct { | |
11 | name string | |
12 | namer gorm.Namer | |
13 | expected string | |
14 | }{ | |
15 | {name: "auth", expected: "auth", namer: gorm.TheNamingStrategy.DB}, | |
16 | {name: "userRestrictions", expected: "user_restrictions", namer: gorm.TheNamingStrategy.Table}, | |
17 | {name: "clientID", expected: "client_id", namer: gorm.TheNamingStrategy.Column}, | |
18 | } | |
19 | ||
20 | for _, c := range cases { | |
21 | t.Run(c.name, func(t *testing.T) { | |
22 | result := c.namer(c.name) | |
23 | if result != c.expected { | |
24 | t.Errorf("error in naming strategy. expected: %v got :%v\n", c.expected, result) | |
25 | } | |
26 | }) | |
27 | } | |
28 | ||
29 | } | |
30 | ||
31 | func TestNamingStrategy(t *testing.T) { | |
32 | ||
33 | dbNameNS := func(name string) string { | |
34 | return "db_" + name | |
35 | } | |
36 | tableNameNS := func(name string) string { | |
37 | return "tbl_" + name | |
38 | } | |
39 | columnNameNS := func(name string) string { | |
40 | return "col_" + name | |
41 | } | |
42 | ||
43 | ns := &gorm.NamingStrategy{ | |
44 | DB: dbNameNS, | |
45 | Table: tableNameNS, | |
46 | Column: columnNameNS, | |
47 | } | |
48 | ||
49 | cases := []struct { | |
50 | name string | |
51 | namer gorm.Namer | |
52 | expected string | |
53 | }{ | |
54 | {name: "auth", expected: "db_auth", namer: ns.DB}, | |
55 | {name: "user", expected: "tbl_user", namer: ns.Table}, | |
56 | {name: "password", expected: "col_password", namer: ns.Column}, | |
57 | } | |
58 | ||
59 | for _, c := range cases { | |
60 | t.Run(c.name, func(t *testing.T) { | |
61 | result := c.namer(c.name) | |
62 | if result != c.expected { | |
63 | t.Errorf("error in naming strategy. expected: %v got :%v\n", c.expected, result) | |
64 | } | |
65 | }) | |
66 | } | |
67 | ||
68 | } |
122 | 122 | } |
123 | 123 | } |
124 | 124 | |
125 | func TestAutoPreloadFalseDoesntPreload(t *testing.T) { | |
126 | user1 := getPreloadUser("auto_user1") | |
127 | DB.Save(user1) | |
128 | ||
129 | preloadDB := DB.Set("gorm:auto_preload", false).Where("role = ?", "Preload") | |
130 | var user User | |
131 | preloadDB.Find(&user) | |
132 | ||
133 | if user.BillingAddress.Address1 != "" { | |
134 | t.Error("AutoPreload was set to fasle, but still fetched data") | |
135 | } | |
136 | ||
137 | user2 := getPreloadUser("auto_user2") | |
138 | DB.Save(user2) | |
139 | ||
140 | var users []User | |
141 | preloadDB.Find(&users) | |
142 | ||
143 | for _, user := range users { | |
144 | if user.BillingAddress.Address1 != "" { | |
145 | t.Error("AutoPreload was set to fasle, but still fetched data") | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
125 | 150 | func TestNestedPreload1(t *testing.T) { |
126 | 151 | type ( |
127 | 152 | Level1 struct { |
745 | 770 | levelB3 := &LevelB3{ |
746 | 771 | Value: "bar", |
747 | 772 | LevelB1ID: sql.NullInt64{Valid: true, Int64: int64(levelB1.ID)}, |
773 | LevelB2s: []*LevelB2{}, | |
748 | 774 | } |
749 | 775 | if err := DB.Create(levelB3).Error; err != nil { |
750 | 776 | t.Error(err) |
1650 | 1676 | lvl := Level1{ |
1651 | 1677 | Name: "l1", |
1652 | 1678 | Level2s: []Level2{ |
1653 | Level2{Name: "l2-1"}, Level2{Name: "l2-2"}, | |
1679 | {Name: "l2-1"}, {Name: "l2-2"}, | |
1654 | 1680 | }, |
1655 | 1681 | } |
1656 | 1682 | DB.Save(&lvl) |
37 | 37 | |
38 | 38 | if user.Email != "" { |
39 | 39 | t.Errorf("User's Email should be blank as no one set it") |
40 | } | |
41 | } | |
42 | ||
43 | func TestQueryWithAssociation(t *testing.T) { | |
44 | user := &User{Name: "user1", Emails: []Email{{Email: "user1@example.com"}}, Company: Company{Name: "company"}} | |
45 | ||
46 | if err := DB.Create(&user).Error; err != nil { | |
47 | t.Fatalf("errors happened when create user: %v", err) | |
48 | } | |
49 | ||
50 | user.CreatedAt = time.Time{} | |
51 | user.UpdatedAt = time.Time{} | |
52 | if err := DB.Where(&user).First(&User{}).Error; err != nil { | |
53 | t.Errorf("search with struct with association should returns no error, but got %v", err) | |
54 | } | |
55 | ||
56 | if err := DB.Where(user).First(&User{}).Error; err != nil { | |
57 | t.Errorf("search with struct with association should returns no error, but got %v", err) | |
40 | 58 | } |
41 | 59 | } |
42 | 60 | |
180 | 198 | |
181 | 199 | scopedb.Where("birthday > ?", parseTime("2000-1-1")).Find(&users) |
182 | 200 | if len(users) != 2 { |
183 | t.Errorf("Should found 2 users's birthday > 2000-1-1, but got %v", len(users)) | |
201 | t.Errorf("Should found 2 users' birthday > 2000-1-1, but got %v", len(users)) | |
184 | 202 | } |
185 | 203 | |
186 | 204 | scopedb.Where("birthday > ?", "2002-10-10").Find(&users) |
187 | 205 | if len(users) != 2 { |
188 | t.Errorf("Should found 2 users's birthday >= 2002-10-10, but got %v", len(users)) | |
206 | t.Errorf("Should found 2 users' birthday >= 2002-10-10, but got %v", len(users)) | |
189 | 207 | } |
190 | 208 | |
191 | 209 | scopedb.Where("birthday >= ?", "2010-1-1").Where("birthday < ?", "2020-1-1").Find(&users) |
192 | 210 | if len(users) != 1 { |
193 | t.Errorf("Should found 1 users's birthday < 2020-1-1 and >= 2010-1-1, but got %v", len(users)) | |
211 | t.Errorf("Should found 1 users' birthday < 2020-1-1 and >= 2010-1-1, but got %v", len(users)) | |
194 | 212 | } |
195 | 213 | |
196 | 214 | DB.Where("name in (?)", []string{user1.Name, user2.Name}).Find(&users) |
456 | 474 | } |
457 | 475 | } |
458 | 476 | |
477 | func TestLimitAndOffsetSQL(t *testing.T) { | |
478 | user1 := User{Name: "TestLimitAndOffsetSQL1", Age: 10} | |
479 | user2 := User{Name: "TestLimitAndOffsetSQL2", Age: 20} | |
480 | user3 := User{Name: "TestLimitAndOffsetSQL3", Age: 30} | |
481 | user4 := User{Name: "TestLimitAndOffsetSQL4", Age: 40} | |
482 | user5 := User{Name: "TestLimitAndOffsetSQL5", Age: 50} | |
483 | if err := DB.Save(&user1).Save(&user2).Save(&user3).Save(&user4).Save(&user5).Error; err != nil { | |
484 | t.Fatal(err) | |
485 | } | |
486 | ||
487 | tests := []struct { | |
488 | name string | |
489 | limit, offset interface{} | |
490 | users []*User | |
491 | ok bool | |
492 | }{ | |
493 | { | |
494 | name: "OK", | |
495 | limit: float64(2), | |
496 | offset: float64(2), | |
497 | users: []*User{ | |
498 | &User{Name: "TestLimitAndOffsetSQL3", Age: 30}, | |
499 | &User{Name: "TestLimitAndOffsetSQL2", Age: 20}, | |
500 | }, | |
501 | ok: true, | |
502 | }, | |
503 | { | |
504 | name: "Limit parse error", | |
505 | limit: float64(1000000), // 1e+06 | |
506 | offset: float64(2), | |
507 | ok: false, | |
508 | }, | |
509 | { | |
510 | name: "Offset parse error", | |
511 | limit: float64(2), | |
512 | offset: float64(1000000), // 1e+06 | |
513 | ok: false, | |
514 | }, | |
515 | } | |
516 | ||
517 | for _, tt := range tests { | |
518 | t.Run(tt.name, func(t *testing.T) { | |
519 | var users []*User | |
520 | err := DB.Where("name LIKE ?", "TestLimitAndOffsetSQL%").Order("age desc").Limit(tt.limit).Offset(tt.offset).Find(&users).Error | |
521 | if tt.ok { | |
522 | if err != nil { | |
523 | t.Errorf("error expected nil, but got %v", err) | |
524 | } | |
525 | if len(users) != len(tt.users) { | |
526 | t.Errorf("users length expected %d, but got %d", len(tt.users), len(users)) | |
527 | } | |
528 | for i := range tt.users { | |
529 | if users[i].Name != tt.users[i].Name { | |
530 | t.Errorf("users[%d] name expected %s, but got %s", i, tt.users[i].Name, users[i].Name) | |
531 | } | |
532 | if users[i].Age != tt.users[i].Age { | |
533 | t.Errorf("users[%d] age expected %d, but got %d", i, tt.users[i].Age, users[i].Age) | |
534 | } | |
535 | } | |
536 | } else { | |
537 | if err == nil { | |
538 | t.Error("error expected not nil, but got nil") | |
539 | } | |
540 | } | |
541 | }) | |
542 | } | |
543 | } | |
544 | ||
459 | 545 | func TestOr(t *testing.T) { |
460 | 546 | user1 := User{Name: "OrUser1", Age: 1} |
461 | 547 | user2 := User{Name: "OrUser2", Age: 10} |
531 | 617 | DB.Table("users").Where("name = ?", "user3").Count(&name3Count) |
532 | 618 | DB.Not("name", "user3").Find(&users4) |
533 | 619 | if len(users1)-len(users4) != int(name3Count) { |
534 | t.Errorf("Should find all users's name not equal 3") | |
620 | t.Errorf("Should find all users' name not equal 3") | |
535 | 621 | } |
536 | 622 | |
537 | 623 | DB.Not("name = ?", "user3").Find(&users4) |
538 | 624 | if len(users1)-len(users4) != int(name3Count) { |
539 | t.Errorf("Should find all users's name not equal 3") | |
625 | t.Errorf("Should find all users' name not equal 3") | |
540 | 626 | } |
541 | 627 | |
542 | 628 | DB.Not("name <> ?", "user3").Find(&users4) |
543 | 629 | if len(users4) != int(name3Count) { |
544 | t.Errorf("Should find all users's name not equal 3") | |
630 | t.Errorf("Should find all users' name not equal 3") | |
545 | 631 | } |
546 | 632 | |
547 | 633 | DB.Not(User{Name: "user3"}).Find(&users5) |
548 | 634 | |
549 | 635 | if len(users1)-len(users5) != int(name3Count) { |
550 | t.Errorf("Should find all users's name not equal 3") | |
636 | t.Errorf("Should find all users' name not equal 3") | |
551 | 637 | } |
552 | 638 | |
553 | 639 | DB.Not(map[string]interface{}{"name": "user3"}).Find(&users6) |
554 | 640 | if len(users1)-len(users6) != int(name3Count) { |
555 | t.Errorf("Should find all users's name not equal 3") | |
641 | t.Errorf("Should find all users' name not equal 3") | |
556 | 642 | } |
557 | 643 | |
558 | 644 | DB.Not(map[string]interface{}{"name": "user3", "company_id": nil}).Find(&users7) |
562 | 648 | |
563 | 649 | DB.Not("name", []string{"user3"}).Find(&users8) |
564 | 650 | if len(users1)-len(users8) != int(name3Count) { |
565 | t.Errorf("Should find all users's name not equal 3") | |
651 | t.Errorf("Should find all users' name not equal 3") | |
566 | 652 | } |
567 | 653 | |
568 | 654 | var name2Count int64 |
569 | 655 | DB.Table("users").Where("name = ?", "user2").Count(&name2Count) |
570 | 656 | DB.Not("name", []string{"user3", "user2"}).Find(&users9) |
571 | 657 | if len(users1)-len(users9) != (int(name3Count) + int(name2Count)) { |
572 | t.Errorf("Should find all users's name not equal 3") | |
658 | t.Errorf("Should find all users' name not equal 3") | |
573 | 659 | } |
574 | 660 | } |
575 | 661 |
62 | 62 | |
63 | 63 | // Dialect get dialect |
64 | 64 | func (scope *Scope) Dialect() Dialect { |
65 | return scope.db.parent.dialect | |
65 | return scope.db.dialect | |
66 | 66 | } |
67 | 67 | |
68 | 68 | // Quote used to quote string to escape them for database |
69 | 69 | func (scope *Scope) Quote(str string) string { |
70 | if strings.Index(str, ".") != -1 { | |
70 | if strings.Contains(str, ".") { | |
71 | 71 | newStrs := []string{} |
72 | 72 | for _, str := range strings.Split(str, ".") { |
73 | 73 | newStrs = append(newStrs, scope.Dialect().Quote(str)) |
133 | 133 | // FieldByName find `gorm.Field` with field name or db name |
134 | 134 | func (scope *Scope) FieldByName(name string) (field *Field, ok bool) { |
135 | 135 | var ( |
136 | dbName = ToDBName(name) | |
136 | dbName = ToColumnName(name) | |
137 | 137 | mostMatchedField *Field |
138 | 138 | ) |
139 | 139 | |
224 | 224 | updateAttrs[field.DBName] = value |
225 | 225 | return field.Set(value) |
226 | 226 | } |
227 | if (field.DBName == dbName) || (field.Name == name && mostMatchedField == nil) { | |
227 | if !field.IsIgnored && ((field.DBName == dbName) || (field.Name == name && mostMatchedField == nil)) { | |
228 | 228 | mostMatchedField = field |
229 | 229 | } |
230 | 230 | } |
256 | 256 | func (scope *Scope) AddToVars(value interface{}) string { |
257 | 257 | _, skipBindVar := scope.InstanceGet("skip_bindvar") |
258 | 258 | |
259 | if expr, ok := value.(*expr); ok { | |
259 | if expr, ok := value.(*SqlExpr); ok { | |
260 | 260 | exp := expr.expr |
261 | 261 | for _, arg := range expr.args { |
262 | 262 | if skipBindVar { |
329 | 329 | // QuotedTableName return quoted table name |
330 | 330 | func (scope *Scope) QuotedTableName() (name string) { |
331 | 331 | if scope.Search != nil && len(scope.Search.tableName) > 0 { |
332 | if strings.Index(scope.Search.tableName, " ") != -1 { | |
332 | if strings.Contains(scope.Search.tableName, " ") { | |
333 | 333 | return scope.Search.tableName |
334 | 334 | } |
335 | 335 | return scope.Quote(scope.Search.tableName) |
401 | 401 | // Begin start a transaction |
402 | 402 | func (scope *Scope) Begin() *Scope { |
403 | 403 | if db, ok := scope.SQLDB().(sqlDb); ok { |
404 | if tx, err := db.Begin(); err == nil { | |
404 | if tx, err := db.Begin(); scope.Err(err) == nil { | |
405 | 405 | scope.db.db = interface{}(tx).(SQLCommon) |
406 | 406 | scope.InstanceSet("gorm:started_transaction", true) |
407 | 407 | } |
485 | 485 | values[index] = &ignored |
486 | 486 | |
487 | 487 | selectFields = fields |
488 | offset := 0 | |
488 | 489 | if idx, ok := selectedColumnsMap[column]; ok { |
489 | selectFields = selectFields[idx+1:] | |
490 | offset = idx + 1 | |
491 | selectFields = selectFields[offset:] | |
490 | 492 | } |
491 | 493 | |
492 | 494 | for fieldIndex, field := range selectFields { |
500 | 502 | resetFields[index] = field |
501 | 503 | } |
502 | 504 | |
503 | selectedColumnsMap[column] = fieldIndex | |
505 | selectedColumnsMap[column] = offset + fieldIndex | |
504 | 506 | |
505 | 507 | if field.IsNormal { |
506 | 508 | break |
585 | 587 | scope.Err(fmt.Errorf("invalid query condition: %v", value)) |
586 | 588 | return |
587 | 589 | } |
588 | ||
590 | scopeQuotedTableName := newScope.QuotedTableName() | |
589 | 591 | for _, field := range newScope.Fields() { |
590 | if !field.IsIgnored && !field.IsBlank { | |
591 | sqls = append(sqls, fmt.Sprintf("(%v.%v %s %v)", quotedTableName, scope.Quote(field.DBName), equalSQL, scope.AddToVars(field.Field.Interface()))) | |
592 | if !field.IsIgnored && !field.IsBlank && field.Relationship == nil { | |
593 | sqls = append(sqls, fmt.Sprintf("(%v.%v %s %v)", scopeQuotedTableName, scope.Quote(field.DBName), equalSQL, scope.AddToVars(field.Field.Interface()))) | |
592 | 594 | } |
593 | 595 | } |
594 | 596 | return strings.Join(sqls, " AND ") |
691 | 693 | |
692 | 694 | buff := bytes.NewBuffer([]byte{}) |
693 | 695 | i := 0 |
694 | for pos := range str { | |
696 | for pos, char := range str { | |
695 | 697 | if str[pos] == '?' { |
696 | 698 | buff.WriteString(replacements[i]) |
697 | 699 | i++ |
698 | 700 | } else { |
699 | buff.WriteByte(str[pos]) | |
701 | buff.WriteRune(char) | |
700 | 702 | } |
701 | 703 | } |
702 | 704 | |
782 | 784 | for _, order := range scope.Search.orders { |
783 | 785 | if str, ok := order.(string); ok { |
784 | 786 | orders = append(orders, scope.quoteIfPossible(str)) |
785 | } else if expr, ok := order.(*expr); ok { | |
787 | } else if expr, ok := order.(*SqlExpr); ok { | |
786 | 788 | exp := expr.expr |
787 | 789 | for _, arg := range expr.args { |
788 | 790 | exp = strings.Replace(exp, "?", scope.AddToVars(arg), 1) |
794 | 796 | } |
795 | 797 | |
796 | 798 | func (scope *Scope) limitAndOffsetSQL() string { |
797 | return scope.Dialect().LimitAndOffsetSQL(scope.Search.limit, scope.Search.offset) | |
799 | sql, err := scope.Dialect().LimitAndOffsetSQL(scope.Search.limit, scope.Search.offset) | |
800 | scope.Err(err) | |
801 | return sql | |
798 | 802 | } |
799 | 803 | |
800 | 804 | func (scope *Scope) groupSQL() string { |
852 | 856 | } |
853 | 857 | |
854 | 858 | func (scope *Scope) callCallbacks(funcs []*func(s *Scope)) *Scope { |
859 | defer func() { | |
860 | if err := recover(); err != nil { | |
861 | if db, ok := scope.db.db.(sqlTx); ok { | |
862 | db.Rollback() | |
863 | } | |
864 | panic(err) | |
865 | } | |
866 | }() | |
855 | 867 | for _, f := range funcs { |
856 | 868 | (*f)(scope) |
857 | 869 | if scope.skipLeft { |
861 | 873 | return scope |
862 | 874 | } |
863 | 875 | |
864 | func convertInterfaceToMap(values interface{}, withIgnoredField bool) map[string]interface{} { | |
876 | func convertInterfaceToMap(values interface{}, withIgnoredField bool, db *DB) map[string]interface{} { | |
865 | 877 | var attrs = map[string]interface{}{} |
866 | 878 | |
867 | 879 | switch value := values.(type) { |
869 | 881 | return value |
870 | 882 | case []interface{}: |
871 | 883 | for _, v := range value { |
872 | for key, value := range convertInterfaceToMap(v, withIgnoredField) { | |
884 | for key, value := range convertInterfaceToMap(v, withIgnoredField, db) { | |
873 | 885 | attrs[key] = value |
874 | 886 | } |
875 | 887 | } |
879 | 891 | switch reflectValue.Kind() { |
880 | 892 | case reflect.Map: |
881 | 893 | for _, key := range reflectValue.MapKeys() { |
882 | attrs[ToDBName(key.Interface().(string))] = reflectValue.MapIndex(key).Interface() | |
894 | attrs[ToColumnName(key.Interface().(string))] = reflectValue.MapIndex(key).Interface() | |
883 | 895 | } |
884 | 896 | default: |
885 | for _, field := range (&Scope{Value: values}).Fields() { | |
897 | for _, field := range (&Scope{Value: values, db: db}).Fields() { | |
886 | 898 | if !field.IsBlank && (withIgnoredField || !field.IsIgnored) { |
887 | 899 | attrs[field.DBName] = field.Field.Interface() |
888 | 900 | } |
894 | 906 | |
895 | 907 | func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[string]interface{}, hasUpdate bool) { |
896 | 908 | if scope.IndirectValue().Kind() != reflect.Struct { |
897 | return convertInterfaceToMap(value, false), true | |
909 | return convertInterfaceToMap(value, false, scope.db), true | |
898 | 910 | } |
899 | 911 | |
900 | 912 | results = map[string]interface{}{} |
901 | 913 | |
902 | for key, value := range convertInterfaceToMap(value, true) { | |
903 | if field, ok := scope.FieldByName(key); ok && scope.changeableField(field) { | |
904 | if _, ok := value.(*expr); ok { | |
905 | hasUpdate = true | |
906 | results[field.DBName] = value | |
907 | } else { | |
908 | err := field.Set(value) | |
909 | if field.IsNormal { | |
914 | for key, value := range convertInterfaceToMap(value, true, scope.db) { | |
915 | if field, ok := scope.FieldByName(key); ok { | |
916 | if scope.changeableField(field) { | |
917 | if _, ok := value.(*SqlExpr); ok { | |
910 | 918 | hasUpdate = true |
911 | if err == ErrUnaddressable { | |
912 | results[field.DBName] = value | |
913 | } else { | |
914 | results[field.DBName] = field.Field.Interface() | |
919 | results[field.DBName] = value | |
920 | } else { | |
921 | err := field.Set(value) | |
922 | if field.IsNormal && !field.IsIgnored { | |
923 | hasUpdate = true | |
924 | if err == ErrUnaddressable { | |
925 | results[field.DBName] = value | |
926 | } else { | |
927 | results[field.DBName] = field.Field.Interface() | |
928 | } | |
915 | 929 | } |
916 | 930 | } |
917 | 931 | } |
932 | } else { | |
933 | results[key] = value | |
918 | 934 | } |
919 | 935 | } |
920 | 936 | return |
971 | 987 | if dest.Kind() != reflect.Slice { |
972 | 988 | scope.Err(fmt.Errorf("results should be a slice, not %s", dest.Kind())) |
973 | 989 | return scope |
990 | } | |
991 | ||
992 | if dest.Len() > 0 { | |
993 | dest.Set(reflect.Zero(dest.Type())) | |
974 | 994 | } |
975 | 995 | |
976 | 996 | if query, ok := scope.Search.selects["query"]; !ok || !scope.isQueryForColumn(query, column) { |
996 | 1016 | func (scope *Scope) count(value interface{}) *Scope { |
997 | 1017 | if query, ok := scope.Search.selects["query"]; !ok || !countingQueryRegexp.MatchString(fmt.Sprint(query)) { |
998 | 1018 | if len(scope.Search.group) != 0 { |
999 | scope.Search.Select("count(*) FROM ( SELECT count(*) as name ") | |
1000 | scope.Search.group += " ) AS count_table" | |
1019 | if len(scope.Search.havingConditions) != 0 { | |
1020 | scope.prepareQuerySQL() | |
1021 | scope.Search = &search{} | |
1022 | scope.Search.Select("count(*)") | |
1023 | scope.Search.Table(fmt.Sprintf("( %s ) AS count_table", scope.SQL)) | |
1024 | } else { | |
1025 | scope.Search.Select("count(*) FROM ( SELECT count(*) as name ") | |
1026 | scope.Search.group += " ) AS count_table" | |
1027 | } | |
1001 | 1028 | } else { |
1002 | 1029 | scope.Search.Select("count(*)") |
1003 | 1030 | } |
1112 | 1139 | if field, ok := scope.FieldByName(fieldName); ok { |
1113 | 1140 | foreignKeyStruct := field.clone() |
1114 | 1141 | foreignKeyStruct.IsPrimaryKey = false |
1115 | foreignKeyStruct.TagSettings["IS_JOINTABLE_FOREIGNKEY"] = "true" | |
1116 | delete(foreignKeyStruct.TagSettings, "AUTO_INCREMENT") | |
1142 | foreignKeyStruct.TagSettingsSet("IS_JOINTABLE_FOREIGNKEY", "true") | |
1143 | foreignKeyStruct.TagSettingsDelete("AUTO_INCREMENT") | |
1117 | 1144 | sqlTypes = append(sqlTypes, scope.Quote(relationship.ForeignDBNames[idx])+" "+scope.Dialect().DataTypeOf(foreignKeyStruct)) |
1118 | 1145 | primaryKeys = append(primaryKeys, scope.Quote(relationship.ForeignDBNames[idx])) |
1119 | 1146 | } |
1123 | 1150 | if field, ok := toScope.FieldByName(fieldName); ok { |
1124 | 1151 | foreignKeyStruct := field.clone() |
1125 | 1152 | foreignKeyStruct.IsPrimaryKey = false |
1126 | foreignKeyStruct.TagSettings["IS_JOINTABLE_FOREIGNKEY"] = "true" | |
1127 | delete(foreignKeyStruct.TagSettings, "AUTO_INCREMENT") | |
1153 | foreignKeyStruct.TagSettingsSet("IS_JOINTABLE_FOREIGNKEY", "true") | |
1154 | foreignKeyStruct.TagSettingsDelete("AUTO_INCREMENT") | |
1128 | 1155 | sqlTypes = append(sqlTypes, scope.Quote(relationship.AssociationForeignDBNames[idx])+" "+scope.Dialect().DataTypeOf(foreignKeyStruct)) |
1129 | 1156 | primaryKeys = append(primaryKeys, scope.Quote(relationship.AssociationForeignDBNames[idx])) |
1130 | 1157 | } |
1172 | 1199 | } |
1173 | 1200 | |
1174 | 1201 | func (scope *Scope) dropTable() *Scope { |
1175 | scope.Raw(fmt.Sprintf("DROP TABLE %v%s", scope.QuotedTableName(), scope.getTableOptions())).Exec() | |
1202 | scope.Raw(fmt.Sprintf("DROP TABLE %v", scope.QuotedTableName())).Exec() | |
1176 | 1203 | return scope |
1177 | 1204 | } |
1178 | 1205 | |
1214 | 1241 | } |
1215 | 1242 | |
1216 | 1243 | func (scope *Scope) removeForeignKey(field string, dest string) { |
1217 | keyName := scope.Dialect().BuildKeyName(scope.TableName(), field, dest) | |
1218 | ||
1244 | keyName := scope.Dialect().BuildKeyName(scope.TableName(), field, dest, "foreign") | |
1219 | 1245 | if !scope.Dialect().HasForeignKey(scope.TableName(), keyName) { |
1220 | 1246 | return |
1221 | 1247 | } |
1222 | var query = `ALTER TABLE %s DROP CONSTRAINT %s;` | |
1248 | var mysql mysql | |
1249 | var query string | |
1250 | if scope.Dialect().GetName() == mysql.GetName() { | |
1251 | query = `ALTER TABLE %s DROP FOREIGN KEY %s;` | |
1252 | } else { | |
1253 | query = `ALTER TABLE %s DROP CONSTRAINT %s;` | |
1254 | } | |
1255 | ||
1223 | 1256 | scope.Raw(fmt.Sprintf(query, scope.QuotedTableName(), scope.quoteIfPossible(keyName))).Exec() |
1224 | 1257 | } |
1225 | 1258 | |
1253 | 1286 | var uniqueIndexes = map[string][]string{} |
1254 | 1287 | |
1255 | 1288 | for _, field := range scope.GetStructFields() { |
1256 | if name, ok := field.TagSettings["INDEX"]; ok { | |
1289 | if name, ok := field.TagSettingsGet("INDEX"); ok { | |
1257 | 1290 | names := strings.Split(name, ",") |
1258 | 1291 | |
1259 | 1292 | for _, name := range names { |
1260 | 1293 | if name == "INDEX" || name == "" { |
1261 | 1294 | name = scope.Dialect().BuildKeyName("idx", scope.TableName(), field.DBName) |
1262 | 1295 | } |
1263 | indexes[name] = append(indexes[name], field.DBName) | |
1264 | } | |
1265 | } | |
1266 | ||
1267 | if name, ok := field.TagSettings["UNIQUE_INDEX"]; ok { | |
1296 | name, column := scope.Dialect().NormalizeIndexAndColumn(name, field.DBName) | |
1297 | indexes[name] = append(indexes[name], column) | |
1298 | } | |
1299 | } | |
1300 | ||
1301 | if name, ok := field.TagSettingsGet("UNIQUE_INDEX"); ok { | |
1268 | 1302 | names := strings.Split(name, ",") |
1269 | 1303 | |
1270 | 1304 | for _, name := range names { |
1271 | 1305 | if name == "UNIQUE_INDEX" || name == "" { |
1272 | 1306 | name = scope.Dialect().BuildKeyName("uix", scope.TableName(), field.DBName) |
1273 | 1307 | } |
1274 | uniqueIndexes[name] = append(uniqueIndexes[name], field.DBName) | |
1308 | name, column := scope.Dialect().NormalizeIndexAndColumn(name, field.DBName) | |
1309 | uniqueIndexes[name] = append(uniqueIndexes[name], column) | |
1275 | 1310 | } |
1276 | 1311 | } |
1277 | 1312 | } |
1292 | 1327 | } |
1293 | 1328 | |
1294 | 1329 | func (scope *Scope) getColumnAsArray(columns []string, values ...interface{}) (results [][]interface{}) { |
1330 | resultMap := make(map[string][]interface{}) | |
1295 | 1331 | for _, value := range values { |
1296 | 1332 | indirectValue := indirect(reflect.ValueOf(value)) |
1297 | 1333 | |
1310 | 1346 | } |
1311 | 1347 | |
1312 | 1348 | if hasValue { |
1313 | results = append(results, result) | |
1349 | h := fmt.Sprint(result...) | |
1350 | if _, exist := resultMap[h]; !exist { | |
1351 | resultMap[h] = result | |
1352 | } | |
1314 | 1353 | } |
1315 | 1354 | } |
1316 | 1355 | case reflect.Struct: |
1325 | 1364 | } |
1326 | 1365 | |
1327 | 1366 | if hasValue { |
1328 | results = append(results, result) | |
1329 | } | |
1330 | } | |
1331 | } | |
1332 | ||
1367 | h := fmt.Sprint(result...) | |
1368 | if _, exist := resultMap[h]; !exist { | |
1369 | resultMap[h] = result | |
1370 | } | |
1371 | } | |
1372 | } | |
1373 | } | |
1374 | for _, v := range resultMap { | |
1375 | results = append(results, v) | |
1376 | } | |
1333 | 1377 | return |
1334 | 1378 | } |
1335 | 1379 |
77 | 77 | t.Errorf("The error should be returned from Valuer, but get %v", err) |
78 | 78 | } |
79 | 79 | } |
80 | ||
81 | func TestDropTableWithTableOptions(t *testing.T) { | |
82 | type UserWithOptions struct { | |
83 | gorm.Model | |
84 | } | |
85 | DB.AutoMigrate(&UserWithOptions{}) | |
86 | ||
87 | DB = DB.Set("gorm:table_options", "CHARSET=utf8") | |
88 | err := DB.DropTable(&UserWithOptions{}).Error | |
89 | if err != nil { | |
90 | t.Errorf("Table must be dropped, got error %s", err) | |
91 | } | |
92 | } |
31 | 31 | } |
32 | 32 | |
33 | 33 | func (s *search) clone() *search { |
34 | clone := *s | |
34 | clone := search{ | |
35 | db: s.db, | |
36 | whereConditions: make([]map[string]interface{}, len(s.whereConditions)), | |
37 | orConditions: make([]map[string]interface{}, len(s.orConditions)), | |
38 | notConditions: make([]map[string]interface{}, len(s.notConditions)), | |
39 | havingConditions: make([]map[string]interface{}, len(s.havingConditions)), | |
40 | joinConditions: make([]map[string]interface{}, len(s.joinConditions)), | |
41 | initAttrs: make([]interface{}, len(s.initAttrs)), | |
42 | assignAttrs: make([]interface{}, len(s.assignAttrs)), | |
43 | selects: s.selects, | |
44 | omits: make([]string, len(s.omits)), | |
45 | orders: make([]interface{}, len(s.orders)), | |
46 | preload: make([]searchPreload, len(s.preload)), | |
47 | offset: s.offset, | |
48 | limit: s.limit, | |
49 | group: s.group, | |
50 | tableName: s.tableName, | |
51 | raw: s.raw, | |
52 | Unscoped: s.Unscoped, | |
53 | ignoreOrderQuery: s.ignoreOrderQuery, | |
54 | } | |
55 | for i, value := range s.whereConditions { | |
56 | clone.whereConditions[i] = value | |
57 | } | |
58 | for i, value := range s.orConditions { | |
59 | clone.orConditions[i] = value | |
60 | } | |
61 | for i, value := range s.notConditions { | |
62 | clone.notConditions[i] = value | |
63 | } | |
64 | for i, value := range s.havingConditions { | |
65 | clone.havingConditions[i] = value | |
66 | } | |
67 | for i, value := range s.joinConditions { | |
68 | clone.joinConditions[i] = value | |
69 | } | |
70 | for i, value := range s.initAttrs { | |
71 | clone.initAttrs[i] = value | |
72 | } | |
73 | for i, value := range s.assignAttrs { | |
74 | clone.assignAttrs[i] = value | |
75 | } | |
76 | for i, value := range s.omits { | |
77 | clone.omits[i] = value | |
78 | } | |
79 | for i, value := range s.orders { | |
80 | clone.orders[i] = value | |
81 | } | |
82 | for i, value := range s.preload { | |
83 | clone.preload[i] = value | |
84 | } | |
35 | 85 | return &clone |
36 | 86 | } |
37 | 87 | |
97 | 147 | } |
98 | 148 | |
99 | 149 | func (s *search) Having(query interface{}, values ...interface{}) *search { |
100 | if val, ok := query.(*expr); ok { | |
150 | if val, ok := query.(*SqlExpr); ok { | |
101 | 151 | s.havingConditions = append(s.havingConditions, map[string]interface{}{"query": val.expr, "args": val.args}) |
102 | 152 | } else { |
103 | 153 | s.havingConditions = append(s.havingConditions, map[string]interface{}{"query": query, "args": values}) |
0 | 0 | package gorm |
1 | 1 | |
2 | 2 | import ( |
3 | "fmt" | |
3 | 4 | "reflect" |
4 | 5 | "testing" |
5 | 6 | ) |
27 | 28 | t.Errorf("selectStr should be copied") |
28 | 29 | } |
29 | 30 | } |
31 | ||
32 | func TestWhereCloneCorruption(t *testing.T) { | |
33 | for whereCount := 1; whereCount <= 8; whereCount++ { | |
34 | t.Run(fmt.Sprintf("w=%d", whereCount), func(t *testing.T) { | |
35 | s := new(search) | |
36 | for w := 0; w < whereCount; w++ { | |
37 | s = s.clone().Where(fmt.Sprintf("w%d = ?", w), fmt.Sprintf("value%d", w)) | |
38 | } | |
39 | if len(s.whereConditions) != whereCount { | |
40 | t.Errorf("s: where count should be %d", whereCount) | |
41 | } | |
42 | ||
43 | q1 := s.clone().Where("finalThing = ?", "THING1") | |
44 | q2 := s.clone().Where("finalThing = ?", "THING2") | |
45 | ||
46 | if reflect.DeepEqual(q1.whereConditions, q2.whereConditions) { | |
47 | t.Errorf("Where conditions should be different") | |
48 | } | |
49 | }) | |
50 | } | |
51 | } |
0 | 0 | package gorm |
1 | 1 | |
2 | 2 | import ( |
3 | "bytes" | |
4 | 3 | "database/sql/driver" |
5 | 4 | "fmt" |
6 | 5 | "reflect" |
25 | 24 | var commonInitialisms = []string{"API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "LHS", "QPS", "RAM", "RHS", "RPC", "SLA", "SMTP", "SSH", "TLS", "TTL", "UID", "UI", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XSRF", "XSS"} |
26 | 25 | var commonInitialismsReplacer *strings.Replacer |
27 | 26 | |
28 | var goSrcRegexp = regexp.MustCompile(`jinzhu/gorm/.*.go`) | |
29 | var goTestRegexp = regexp.MustCompile(`jinzhu/gorm/.*test.go`) | |
27 | var goSrcRegexp = regexp.MustCompile(`jinzhu/gorm(@.*)?/.*.go`) | |
28 | var goTestRegexp = regexp.MustCompile(`jinzhu/gorm(@.*)?/.*test.go`) | |
30 | 29 | |
31 | 30 | func init() { |
32 | 31 | var commonInitialismsForReplacer []string |
57 | 56 | return &safeMap{l: new(sync.RWMutex), m: make(map[string]string)} |
58 | 57 | } |
59 | 58 | |
60 | var smap = newSafeMap() | |
61 | ||
62 | type strCase bool | |
63 | ||
64 | const ( | |
65 | lower strCase = false | |
66 | upper strCase = true | |
67 | ) | |
68 | ||
69 | // ToDBName convert string to db name | |
70 | func ToDBName(name string) string { | |
71 | if v := smap.Get(name); v != "" { | |
72 | return v | |
73 | } | |
74 | ||
75 | if name == "" { | |
76 | return "" | |
77 | } | |
78 | ||
79 | var ( | |
80 | value = commonInitialismsReplacer.Replace(name) | |
81 | buf = bytes.NewBufferString("") | |
82 | lastCase, currCase, nextCase strCase | |
83 | ) | |
84 | ||
85 | for i, v := range value[:len(value)-1] { | |
86 | nextCase = strCase(value[i+1] >= 'A' && value[i+1] <= 'Z') | |
87 | if i > 0 { | |
88 | if currCase == upper { | |
89 | if lastCase == upper && nextCase == upper { | |
90 | buf.WriteRune(v) | |
91 | } else { | |
92 | if value[i-1] != '_' && value[i+1] != '_' { | |
93 | buf.WriteRune('_') | |
94 | } | |
95 | buf.WriteRune(v) | |
96 | } | |
97 | } else { | |
98 | buf.WriteRune(v) | |
99 | if i == len(value)-2 && nextCase == upper { | |
100 | buf.WriteRune('_') | |
101 | } | |
102 | } | |
103 | } else { | |
104 | currCase = upper | |
105 | buf.WriteRune(v) | |
106 | } | |
107 | lastCase = currCase | |
108 | currCase = nextCase | |
109 | } | |
110 | ||
111 | buf.WriteByte(value[len(value)-1]) | |
112 | ||
113 | s := strings.ToLower(buf.String()) | |
114 | smap.Set(name, s) | |
115 | return s | |
116 | } | |
117 | ||
118 | 59 | // SQL expression |
119 | type expr struct { | |
60 | type SqlExpr struct { | |
120 | 61 | expr string |
121 | 62 | args []interface{} |
122 | 63 | } |
123 | 64 | |
124 | 65 | // Expr generate raw SQL expression, for example: |
125 | 66 | // DB.Model(&product).Update("price", gorm.Expr("price * ? + ?", 2, 100)) |
126 | func Expr(expression string, args ...interface{}) *expr { | |
127 | return &expr{expr: expression, args: args} | |
67 | func Expr(expression string, args ...interface{}) *SqlExpr { | |
68 | return &SqlExpr{expr: expression, args: args} | |
128 | 69 | } |
129 | 70 | |
130 | 71 | func indirect(reflectValue reflect.Value) reflect.Value { |
264 | 205 | // as FieldByName could panic |
265 | 206 | if indirectValue := reflect.Indirect(value); indirectValue.IsValid() { |
266 | 207 | for _, fieldName := range fieldNames { |
267 | if fieldValue := indirectValue.FieldByName(fieldName); fieldValue.IsValid() { | |
208 | if fieldValue := reflect.Indirect(indirectValue.FieldByName(fieldName)); fieldValue.IsValid() { | |
268 | 209 | result := fieldValue.Interface() |
269 | 210 | if r, ok := result.(driver.Valuer); ok { |
270 | 211 | result, _ = r.Value() |
0 | package gorm_test | |
1 | ||
2 | import ( | |
3 | "testing" | |
4 | ||
5 | "github.com/jinzhu/gorm" | |
6 | ) | |
7 | ||
8 | func TestToDBNameGenerateFriendlyName(t *testing.T) { | |
9 | var maps = map[string]string{ | |
10 | "": "", | |
11 | "X": "x", | |
12 | "ThisIsATest": "this_is_a_test", | |
13 | "PFAndESI": "pf_and_esi", | |
14 | "AbcAndJkl": "abc_and_jkl", | |
15 | "EmployeeID": "employee_id", | |
16 | "SKU_ID": "sku_id", | |
17 | "FieldX": "field_x", | |
18 | "HTTPAndSMTP": "http_and_smtp", | |
19 | "HTTPServerHandlerForURLID": "http_server_handler_for_url_id", | |
20 | "UUID": "uuid", | |
21 | "HTTPURL": "http_url", | |
22 | "HTTP_URL": "http_url", | |
23 | "ThisIsActuallyATestSoWeMayBeAbleToUseThisCodeInGormPackageAlsoIdCanBeUsedAtTheEndAsID": "this_is_actually_a_test_so_we_may_be_able_to_use_this_code_in_gorm_package_also_id_can_be_used_at_the_end_as_id", | |
24 | } | |
25 | ||
26 | for key, value := range maps { | |
27 | if gorm.ToDBName(key) != value { | |
28 | t.Errorf("%v ToDBName should equal %v, but got %v", key, value, gorm.ToDBName(key)) | |
29 | } | |
30 | } | |
31 | } |
3 | 3 | services: |
4 | 4 | - name: mariadb |
5 | 5 | id: mariadb:latest |
6 | env: | |
7 | MYSQL_DATABASE: gorm | |
8 | MYSQL_USER: gorm | |
9 | MYSQL_PASSWORD: gorm | |
10 | MYSQL_RANDOM_ROOT_PASSWORD: "yes" | |
11 | - name: mysql | |
12 | id: mysql:latest | |
6 | 13 | env: |
7 | 14 | MYSQL_DATABASE: gorm |
8 | 15 | MYSQL_USER: gorm |
17 | 24 | MYSQL_RANDOM_ROOT_PASSWORD: "yes" |
18 | 25 | - name: mysql56 |
19 | 26 | id: mysql:5.6 |
20 | env: | |
21 | MYSQL_DATABASE: gorm | |
22 | MYSQL_USER: gorm | |
23 | MYSQL_PASSWORD: gorm | |
24 | MYSQL_RANDOM_ROOT_PASSWORD: "yes" | |
25 | - name: mysql55 | |
26 | id: mysql:5.5 | |
27 | 27 | env: |
28 | 28 | MYSQL_DATABASE: gorm |
29 | 29 | MYSQL_USER: gorm |
82 | 82 | code: | |
83 | 83 | cd $WERCKER_SOURCE_DIR |
84 | 84 | go version |
85 | go get -t ./... | |
85 | go get -t -v ./... | |
86 | 86 | |
87 | 87 | # Build the project |
88 | 88 | - script: |
94 | 94 | - script: |
95 | 95 | name: test sqlite |
96 | 96 | code: | |
97 | go test ./... | |
97 | go test -race -v ./... | |
98 | 98 | |
99 | 99 | - script: |
100 | 100 | name: test mariadb |
101 | 101 | code: | |
102 | GORM_DIALECT=mysql GORM_DSN="gorm:gorm@tcp(mariadb:3306)/gorm?charset=utf8&parseTime=True" go test ./... | |
102 | GORM_DIALECT=mysql GORM_DSN="gorm:gorm@tcp(mariadb:3306)/gorm?charset=utf8&parseTime=True" go test -race ./... | |
103 | ||
104 | - script: | |
105 | name: test mysql | |
106 | code: | | |
107 | GORM_DIALECT=mysql GORM_DSN="gorm:gorm@tcp(mysql:3306)/gorm?charset=utf8&parseTime=True" go test -race ./... | |
103 | 108 | |
104 | 109 | - script: |
105 | 110 | name: test mysql5.7 |
106 | 111 | code: | |
107 | GORM_DIALECT=mysql GORM_DSN="gorm:gorm@tcp(mysql57:3306)/gorm?charset=utf8&parseTime=True" go test ./... | |
112 | GORM_DIALECT=mysql GORM_DSN="gorm:gorm@tcp(mysql57:3306)/gorm?charset=utf8&parseTime=True" go test -race ./... | |
108 | 113 | |
109 | 114 | - script: |
110 | 115 | name: test mysql5.6 |
111 | 116 | code: | |
112 | GORM_DIALECT=mysql GORM_DSN="gorm:gorm@tcp(mysql56:3306)/gorm?charset=utf8&parseTime=True" go test ./... | |
113 | ||
114 | - script: | |
115 | name: test mysql5.5 | |
116 | code: | | |
117 | GORM_DIALECT=mysql GORM_DSN="gorm:gorm@tcp(mysql55:3306)/gorm?charset=utf8&parseTime=True" go test ./... | |
117 | GORM_DIALECT=mysql GORM_DSN="gorm:gorm@tcp(mysql56:3306)/gorm?charset=utf8&parseTime=True" go test -race ./... | |
118 | 118 | |
119 | 119 | - script: |
120 | 120 | name: test postgres |
121 | 121 | code: | |
122 | GORM_DIALECT=postgres GORM_DSN="host=postgres user=gorm password=gorm DB.name=gorm port=5432 sslmode=disable" go test ./... | |
122 | GORM_DIALECT=postgres GORM_DSN="host=postgres user=gorm password=gorm DB.name=gorm port=5432 sslmode=disable" go test -race ./... | |
123 | 123 | |
124 | 124 | - script: |
125 | 125 | name: test postgres96 |
126 | 126 | code: | |
127 | GORM_DIALECT=postgres GORM_DSN="host=postgres96 user=gorm password=gorm DB.name=gorm port=5432 sslmode=disable" go test ./... | |
127 | GORM_DIALECT=postgres GORM_DSN="host=postgres96 user=gorm password=gorm DB.name=gorm port=5432 sslmode=disable" go test -race ./... | |
128 | 128 | |
129 | 129 | - script: |
130 | 130 | name: test postgres95 |
131 | 131 | code: | |
132 | GORM_DIALECT=postgres GORM_DSN="host=postgres95 user=gorm password=gorm DB.name=gorm port=5432 sslmode=disable" go test ./... | |
132 | GORM_DIALECT=postgres GORM_DSN="host=postgres95 user=gorm password=gorm DB.name=gorm port=5432 sslmode=disable" go test -race ./... | |
133 | 133 | |
134 | 134 | - script: |
135 | 135 | name: test postgres94 |
136 | 136 | code: | |
137 | GORM_DIALECT=postgres GORM_DSN="host=postgres94 user=gorm password=gorm DB.name=gorm port=5432 sslmode=disable" go test ./... | |
137 | GORM_DIALECT=postgres GORM_DSN="host=postgres94 user=gorm password=gorm DB.name=gorm port=5432 sslmode=disable" go test -race ./... | |
138 | 138 | |
139 | 139 | - script: |
140 | 140 | name: test postgres93 |
141 | 141 | code: | |
142 | GORM_DIALECT=postgres GORM_DSN="host=postgres93 user=gorm password=gorm DB.name=gorm port=5432 sslmode=disable" go test ./... | |
142 | GORM_DIALECT=postgres GORM_DSN="host=postgres93 user=gorm password=gorm DB.name=gorm port=5432 sslmode=disable" go test -race ./... | |
143 | 143 | |
144 | 144 | - script: |
145 | name: test mssql | |
145 | name: codecov | |
146 | 146 | code: | |
147 | GORM_DIALECT=mssql GORM_DSN="sqlserver://gorm:LoremIpsum86@mssql:1433?database=gorm" go test ./... | |
147 | go test -race -coverprofile=coverage.txt -covermode=atomic ./... | |
148 | bash <(curl -s https://codecov.io/bash) |