@@ -51,6 +51,7 @@ import (
51
51
"testing"
52
52
"time"
53
53
54
+ "github.com/godror/godror"
54
55
. "github.com/oracle-samples/gorm-oracle/tests/utils"
55
56
56
57
"gorm.io/gorm"
@@ -59,7 +60,6 @@ import (
59
60
)
60
61
61
62
func TestScannerValuer (t * testing.T ) {
62
- t .Skip ()
63
63
DB .Migrator ().DropTable (& ScannerValuerStruct {})
64
64
if err := DB .Migrator ().AutoMigrate (& ScannerValuerStruct {}); err != nil {
65
65
t .Fatalf ("no error should happen when migrate scanner, valuer struct, got error %v" , err )
@@ -92,7 +92,7 @@ func TestScannerValuer(t *testing.T) {
92
92
93
93
var result ScannerValuerStruct
94
94
95
- if err := DB .Find (& result , "id = ?" , data .ID ).Error ; err != nil {
95
+ if err := DB .Find (& result , "\" id \" = ?" , data .ID ).Error ; err != nil {
96
96
t .Fatalf ("no error should happen when query scanner, valuer struct, but got %v" , err )
97
97
}
98
98
@@ -107,22 +107,24 @@ func TestScannerValuer(t *testing.T) {
107
107
}
108
108
109
109
func TestScannerValuerWithFirstOrCreate (t * testing.T ) {
110
- t .Skip ()
111
110
DB .Migrator ().DropTable (& ScannerValuerStruct {})
112
111
if err := DB .Migrator ().AutoMigrate (& ScannerValuerStruct {}); err != nil {
113
112
t .Errorf ("no error should happen when migrate scanner, valuer struct" )
114
113
}
115
114
116
- data := ScannerValuerStruct {
117
- Name : sql.NullString {String : "name" , Valid : true },
118
- Gender : & sql.NullString {String : "M" , Valid : true },
119
- Age : sql.NullInt64 {Int64 : 18 , Valid : true },
115
+ cond := ScannerValuerStruct {
116
+ Name : sql.NullString {String : "name" , Valid : true },
117
+ Gender : & sql.NullString {String : "M" , Valid : true },
118
+ Age : sql.NullInt64 {Int64 : 18 , Valid : true },
119
+ }
120
+
121
+ attrs := ScannerValuerStruct {
120
122
ExampleStruct : ExampleStruct {"name" , "value1" },
121
123
ExampleStructPtr : & ExampleStruct {"name" , "value2" },
122
124
}
123
125
124
126
var result ScannerValuerStruct
125
- tx := DB .Where (data ).FirstOrCreate (& result )
127
+ tx := DB .Where (cond ). Attrs ( attrs ).FirstOrCreate (& result )
126
128
127
129
if tx .RowsAffected != 1 {
128
130
t .Errorf ("RowsAffected should be 1 after create some record" )
@@ -132,9 +134,9 @@ func TestScannerValuerWithFirstOrCreate(t *testing.T) {
132
134
t .Errorf ("Should not raise any error, but got %v" , tx .Error )
133
135
}
134
136
135
- tests .AssertObjEqual (t , result , data , "Name" , "Gender" , "Age" )
137
+ tests .AssertObjEqual (t , result , cond , "Name" , "Gender" , "Age" )
136
138
137
- if err := DB .Where (data ).Assign (ScannerValuerStruct {Age : sql.NullInt64 {Int64 : 18 , Valid : true }}).FirstOrCreate (& result ).Error ; err != nil {
139
+ if err := DB .Where (cond ).Assign (ScannerValuerStruct {Age : sql.NullInt64 {Int64 : 18 , Valid : true }}).FirstOrCreate (& result ).Error ; err != nil {
138
140
t .Errorf ("Should not raise any error, but got %v" , err )
139
141
}
140
142
@@ -151,7 +153,6 @@ func TestScannerValuerWithFirstOrCreate(t *testing.T) {
151
153
}
152
154
153
155
func TestInvalidValuer (t * testing.T ) {
154
- t .Skip ()
155
156
DB .Migrator ().DropTable (& ScannerValuerStruct {})
156
157
if err := DB .Migrator ().AutoMigrate (& ScannerValuerStruct {}); err != nil {
157
158
t .Errorf ("no error should happen when migrate scanner, valuer struct" )
@@ -205,6 +206,24 @@ type ScannerValuerStruct struct {
205
206
ExampleStructPtr * ExampleStruct
206
207
}
207
208
209
+ func (StringsSlice ) GormDataType () string { return "CLOB" }
210
+ func (l StringsSlice ) GormValue (ctx context.Context , db * gorm.DB ) clause.Expr {
211
+ v , err := l .Value ()
212
+ if err != nil {
213
+ return gorm .Expr ("?" , err )
214
+ }
215
+ return gorm .Expr ("?" , v )
216
+ }
217
+
218
+ func (StructsSlice ) GormDataType () string { return "CLOB" }
219
+ func (l StructsSlice ) GormValue (ctx context.Context , db * gorm.DB ) clause.Expr {
220
+ v , err := l .Value ()
221
+ if err != nil {
222
+ return gorm .Expr ("?" , err )
223
+ }
224
+ return gorm .Expr ("?" , v )
225
+ }
226
+
208
227
type EncryptedData []byte
209
228
210
229
func (data * EncryptedData ) Scan (value interface {}) error {
@@ -235,17 +254,44 @@ func (data EncryptedData) Value() (driver.Value, error) {
235
254
236
255
type Num int64
237
256
238
- func (i * Num ) Scan (src interface {}) error {
239
- switch s := src .(type ) {
240
- case []byte :
241
- n , _ := strconv .Atoi (string (s ))
242
- * i = Num (n )
257
+ func (n * Num ) Scan (val interface {}) error {
258
+ if val == nil {
259
+ * n = 0
260
+ return nil
261
+ }
262
+
263
+ switch x := val .(type ) {
243
264
case int64 :
244
- * i = Num (s )
265
+ * n = Num (x )
266
+ return nil
267
+
268
+ case godror.Number :
269
+ i , err := strconv .ParseInt (string (x ), 10 , 64 )
270
+ if err != nil {
271
+ return fmt .Errorf ("Num.Scan: cannot parse godror.Number %q: %w" , string (x ), err )
272
+ }
273
+ * n = Num (i )
274
+ return nil
275
+
276
+ case string :
277
+ i , err := strconv .ParseInt (x , 10 , 64 )
278
+ if err != nil {
279
+ return fmt .Errorf ("Num.Scan: cannot parse string %q: %w" , x , err )
280
+ }
281
+ * n = Num (i )
282
+ return nil
283
+
284
+ case []byte :
285
+ i , err := strconv .ParseInt (string (x ), 10 , 64 )
286
+ if err != nil {
287
+ return fmt .Errorf ("Num.Scan: cannot parse []byte %q: %w" , string (x ), err )
288
+ }
289
+ * n = Num (i )
290
+ return nil
291
+
245
292
default :
246
- return errors . New ( "Cannot scan NamedInt from " + reflect . ValueOf ( src ). String () )
293
+ return fmt . Errorf ( "Num.Scan: unsupported type %T" , val )
247
294
}
248
- return nil
249
295
}
250
296
251
297
type StringsSlice []string
0 commit comments