System.Data.SQLite
Check-in [27785d64aa]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Cleanup and robustify tests.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | customDataTypes
Files: files | file ages | folders
SHA1: 27785d64aa1cc2a9e035cca60f9c81db61e296a5
User & Date: mistachkin 2016-06-20 20:22:43
Context
2016-06-20
20:30
Reorganize the new connection flags. check-in: d1e833bf59 user: mistachkin tags: customDataTypes
20:22
Cleanup and robustify tests. check-in: 27785d64aa user: mistachkin tags: customDataTypes
04:52
Add draft tests for parameter binding and data reader callbacks. Still needs cleanup. check-in: a07ba949c9 user: mistachkin tags: customDataTypes
Changes
Hide Diffs Unified Diffs Show Whitespace Changes Patch

Changes to Tests/types.eagle.

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

36
37
38
39
40
41
42
..
43
44
45
46
47
48
49


50
51
52
53
54
55
56
57
58
59
60
61
62

63
64
65
66
67
68
69
...
101
102
103
104
105
106
107
108
109
110
111
112
113



















114
115
116
117
118

119
120

121
122
123
124
125
126
127
128
129
130
131
132

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

155
156
157

158
159
160
161
162
163
164
165
166
167
168
169
170


171
172
173


174
175
176
177
178
179

180


181
182

183

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

202
203
204
205
206
207
208

209


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

232
233
234
235


236

237
238
239
240
241

242
243
244
245
246
247
248
249
250
251
252
253
254

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269

270
271
272
273
274
275
276
277
278
279
280

281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303


304
305
306
307
308
309
310

311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338

339
340
341
342
343
344
345
346
347
348
349
350


351
352


353
354


355


356
357
358
359
360
361
362
363
364
365
366
367
368
...
372
373
374
375
376
377
378

379
380
381
382
383
384
385
386
387
388
389
390


391
392


393
394


395


396
397
398
399
400
401
402
403
404
405
406
407
408
...
412
413
414
415
416
417
418

419
420
421
422
423
424
425
426
427
428
429
430


431
432


433
434


435


436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452

453
454
455
456
457
458

459
460
461
462
463
464
465
466
467



468









469
470


471


472
473
474
475
476
477
478
479
480
481
482
483
484
...
488
489
490
491
492
493
494

495
496
497
498
499
500
501
502
503
504
505
506
507
508
509


510
511


512
513


514


515
516
517
518
519
520
521
522
523
524




525
526
527
528
529
530
531
532
533
534
runSQLiteTestPrologue

###############################################################################

proc bindValueCallback1 {
        convert command flags parameter typeName index userData
        completeVarName } {

  lappend ::log(bind) [list convert [isObjectHandle $convert]]
  lappend ::log(bind) [list command [isObjectHandle $command]]
  lappend ::log(bind) [list flags [getStringFromObjectHandle $flags]]
  lappend ::log(bind) [list parameter [isObjectHandle $parameter]]
  lappend ::log(bind) [list typeName [getStringFromObjectHandle $typeName]]
  lappend ::log(bind) [list index [getStringFromObjectHandle $index]]
  lappend ::log(bind) [list userData [getStringFromObjectHandle $userData]]

  if {[getStringFromObjectHandle $userData] == 3} then {

    error "parameter binding canceled"
  }

  if {[getStringFromObjectHandle $userData] == 2} then {
    $parameter DbType String
    $parameter Value custom
  }
................................................................................

  if {[getStringFromObjectHandle $userData] == 1} then {
    upvar 1 $completeVarName complete
    set complete [object invoke -create System.Boolean Parse True]
  }
}



proc readValueCallback1 {
        convert dataReader flags eventArgs typeName index userData
        completeVarName } {

  lappend ::log(read) [list convert [isObjectHandle $convert]]
  lappend ::log(read) [list dataReader [isObjectHandle $dataReader]]
  lappend ::log(read) [list flags [getStringFromObjectHandle $flags]]
  lappend ::log(read) [list eventArgs [isObjectHandle $eventArgs]]
  lappend ::log(read) [list typeName [getStringFromObjectHandle $typeName]]
  lappend ::log(read) [list index [getStringFromObjectHandle $index]]
  lappend ::log(read) [list userData [getStringFromObjectHandle $userData]]

  if {[getStringFromObjectHandle $userData] == 3} then {

    error "reading of value canceled"
  }

  if {[getStringFromObjectHandle $userData] == 1} then {
    upvar 1 $completeVarName complete
    set complete [object invoke -create System.Boolean Parse True]
  }
................................................................................
  unset -nocomplain typeCallbacks
  unset -nocomplain result connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {0 True True 1 True True}}

###############################################################################

set readArguments [list \
    convert dataReader flags eventArgs typeName index userData \
    completeVarName]

set readMappings [list \
    [list Boolean   GetBoolean   false  BooleanValue   true   0 \



















        {False False True False False False False False}] \
    [list Byte      GetByte      false  ByteValue      true   0 \
        {0 0 1 0 0 0 0 0}] \
    [list Byte      GetBytes     true   BytesValue     false  0 \
        {0 1 48 1 49 3 {49 46 50} 5 {116 104 114 101 101} 1 4 27\

        {50 48 49 54 45 48 54 45 49 57 32 49 57 58 53 48 58 48 52\
        46 49 50 51 52 53 54 55} 36 {55 52 102 49 102 52 48 50 45\

        100 100 101 102 45 52 48 50 55 45 97 55 56 102 45 51 56 52\
        55 102 97 57 55 98 56 51 48}}] \
    [list Char      GetChar      false  CharValue      true   0 \
        "<NUL> <NUL> \x01 <NUL> <NUL> <NUL> <NUL> <NUL>"] \
    [list Char      GetChars     true   CharsValue     false  0 \
        "0 1 0 1 1 3 {1 . 2} 1 t 1 \x04 1 2 1 7"] \
    [list DateTime  GetDateTime  false  DateTimeValue  true   0 \
        {{0001-01-01 00:00:00Z} {0001-01-01 00:00:00Z}\
        {0001-01-01 00:00:00Z} {0001-01-01 00:00:00Z}\
        {0001-01-01 00:00:00Z} {0001-01-01 00:00:00Z}\
        {2016-06-19 19:50:04.1234567Z} {0001-01-01 00:00:00Z}}] \
    [list Decimal   GetDecimal   false  DecimalValue   true   0 \

        {0 0 1 1.2 0 0 0 0}] \
    [list Double    GetDouble    false  DoubleValue    true   0 \
        {0 0 1 1.2 0 0 0 0}] \
    [list Single    GetFloat     false  FloatValue     true   0 \
        {0 0 1 1.2 0 0 0 0}] \
    [list Guid      GetGuid      false  GuidValue      true   [guid null] \
        {00000000-0000-0000-0000-000000000000\
        00000000-0000-0000-0000-000000000000\
        00000000-0000-0000-0000-000000000000\
        00000000-0000-0000-0000-000000000000\
        00000000-0000-0000-0000-000000000000\
        00060504-0000-0000-0000-000000000000\
        00000000-0000-0000-0000-000000000000\
        74f1f402-ddef-4027-a78f-3847fa97b830}] \
    [list Int16     GetInt16     false  Int16Value     true   0 \
        {0 0 1 0 0 0 0 0}] \
    [list Int32     GetInt32     false  Int32Value     true   0 \
        {0 0 1 0 0 0 0 0}] \
    [list Int64     GetInt64     false  Int64Value     true   0 \
        {0 0 1 0 0 0 0 0}] \
    [list String    GetString    false  StringValue    false  null \
        "<NUL> <NUL> <NUL> <NUL> three \x04\x05\x06 {2016-06-19 19:50:04.1234567}\

        74f1f402-ddef-4027-a78f-3847fa97b830"] \
    [list Object    GetValue     false  Value          false  null \
        "{} 0 1 1.2 three {4 5 6} {2016-06-19 19:50:04.1234567}\

        74f1f402-ddef-4027-a78f-3847fa97b830"]]

###############################################################################

set savedDateTimeFormat [object invoke Interpreter.GetActive DateTimeFormat]
if {![isObjectHandle $savedDateTimeFormat]} then {set savedDateTimeFormat null}
object invoke Interpreter.GetActive DateTimeFormat [getDateTimeFormat]

###############################################################################

for {set i 0} {$i < [llength $readMappings]} {incr i} {
  foreach {
    typeName methodName isArray propertyName


    isRequired value expectedResult
  } [lindex $readMappings $i] break



  proc readValueCallback2 $readArguments [subst {
    if {$isArray} then {
      if {\[catch {
        set keyValue \[\$dataReader GetInt64 0\]

set dataOffset \[\$eventArgs ArrayEventArgs.DataOffset\]

set buffer \[\$eventArgs -create [appendArgs ArrayEventArgs. $typeName Buffer]\]


set bufferOffset \[\$eventArgs ArrayEventArgs.BufferOffset\]
set length \[\$eventArgs ArrayEventArgs.Length\]

set readValue \[\$dataReader \[\$eventArgs MethodName\] \$index \$dataOffset \$buffer \$bufferOffset \$length\]


        \$eventArgs \[appendArgs Value. $propertyName\] \$readValue
      } error\]} then {



          set readValue \[\$dataReader -tostring GetValue \$index\]



        if {"$typeName" eq "Char"} then {
          set string \[object create -alias String \$readValue\]


          set buffer \[\$string -create -alias ToCharArray]


        } else {

          set buffer \[object invoke -create -alias System.Text.Encoding.UTF8 GetBytes \$readValue\]
        }

        \$eventArgs ArrayEventArgs.Length \[\$buffer Length\]

        \$eventArgs \[appendArgs Value. $propertyName\] \$buffer


      } else {



        set buffer \[\$eventArgs -create [appendArgs ArrayEventArgs. $typeName Buffer]\]
        \$eventArgs \[appendArgs Value. $propertyName\] \$buffer



      }
    } else {
      if {\[catch {
        set keyValue \[\$dataReader GetInt64 0\]
        set readValue \[\$dataReader \[\$eventArgs MethodName\] \$index\]

        if {"$typeName" eq "Char"} then {
          set readValue \[object invoke -create Char Parse \$readValue\]
        }

        \$eventArgs \[appendArgs Value. $propertyName\] \$readValue
      } error\]} then {

        \$eventArgs \[appendArgs Value. $propertyName\] {$value}
      }
    }

    upvar 1 \$completeVarName complete
    set complete \[object invoke -create System.Boolean Parse True\]
  }]



  runTest {test [appendArgs types-2. $i] [appendArgs $methodName " callback"] -setup [subst -nocommands {

    set typeName {$typeName}
    set methodName {$methodName}
    set isArray {$isArray}

    setupDb [set fileName [appendArgs types-2. $i .db]] "" "" "" UseConnectionReadValueCallbacks

  }] -body {
    sql execute $db {
      CREATE TABLE t1(x INTEGER, y SPECIAL);
      INSERT INTO t1 (x, y) VALUES(1, NULL);
      INSERT INTO t1 (x, y) VALUES(2, 0);
      INSERT INTO t1 (x, y) VALUES(3, 1);
      INSERT INTO t1 (x, y) VALUES(4, 1.2);
      INSERT INTO t1 (x, y) VALUES(5, 'three');
      INSERT INTO t1 (x, y) VALUES(6, X'040506');
      INSERT INTO t1 (x, y) VALUES(7, '2016-06-19 19:50:04.1234567');
      INSERT INTO t1 (x, y) VALUES(8, '74f1f402-ddef-4027-a78f-3847fa97b830');
    }


    set connection [getDbConnection]

    set result [list]

    set typeCallbacks [object invoke -marshalflags +DynamicCallback \
        System.Data.SQLite.SQLiteTypeCallbacks Create null \
        {-callbackflags +Default readValueCallback2} null null]

    $connection SetTypeCallbacks SPECIAL $typeCallbacks

    set dataReader [sql execute -execute reader -format datareader \
        -alias $db "SELECT x, y FROM t1 ORDER BY x;"]

    while {[$dataReader Read]} {
      if {$isArray} then {

        set buffer [object invoke -create Array CreateInstance $typeName 100]

        if {[catch {
          $dataReader $methodName 1 0 $buffer 0 1
        } value] == 0} then {
          lappend result $value

          if {$value > 0} then {
            set list [object create -alias StringList $buffer]

            lappend result [object invoke StringList GetRange $list 0 [expr {$value - 1}] false]

          }
        } else {
          lappend result [list array_error $::errorCode]
        }
      } else {
        if {[catch {
          $dataReader $methodName 1
        } value] == 0} then {

          if {$value eq "\x00"} then {
            lappend result <NUL>
          } else {
            lappend result [getStringFromObjectHandle $value]
          }

        } else {
          lappend result [list value_error $::errorCode]
        }
      }
    }

    set result
  } -cleanup {


    unset -nocomplain dataReader
    freeDbConnection

    cleanupDb $fileName

    unset -nocomplain buffer typeCallbacks isArray methodName typeName
    unset -nocomplain value result connection db fileName

  } -constraints {eagle command.object monoBug28 command.sql compile.DATA\
SQLite System.Data.SQLite} -result $expectedResult}

  rename readValueCallback2 ""
}

###############################################################################

object invoke Interpreter.GetActive DateTimeFormat $savedDateTimeFormat
unset -nocomplain savedDateTimeFormat

###############################################################################

unset -nocomplain i readArguments readMappings typeName methodName isArray \
    propertyName isRequired value expectedResult

###############################################################################

runTest {test types-3.1 {bind callback (incomplete)} -setup {
  unset -nocomplain log

  setupDb [set fileName types-3.1.db] "" "" "" \
      "UseConnectionBindValueCallbacks UseParameterNameForTypeName"
} -body {
  sql execute $db {
    CREATE TABLE t1(x SPECIAL);
  }


  set connection [getDbConnection]

  set typeCallbacks [object invoke -marshalflags +DynamicCallback \
      System.Data.SQLite.SQLiteTypeCallbacks Create \
      {-callbackflags +Default bindValueCallback1} null 0 null]

  $connection SetTypeCallbacks SPECIAL $typeCallbacks

  sql execute $db {
    INSERT INTO t1 (x) VALUES(?);
  } [list Special Int64 1234]



  list [expr {
    [info exists log(bind)] ? $log(bind) : "<MISSING>"


  }] [sql execute -execute reader -format list $db \
      "SELECT * FROM t1 ORDER BY x;"]


} -cleanup {


  freeDbConnection

  cleanupDb $fileName

  unset -nocomplain typeCallbacks
  unset -nocomplain log connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{{convert true} {command true} {flags\
{UseConnectionBindValueCallbacks, UseParameterNameForTypeName}} {parameter\
true} {typeName Special} {index 1} {userData 0}} 1234}}

###############################################################################

................................................................................
  setupDb [set fileName types-3.2.db] "" "" "" \
      "UseConnectionBindValueCallbacks UseParameterNameForTypeName"
} -body {
  sql execute $db {
    CREATE TABLE t1(x SPECIAL);
  }


  set connection [getDbConnection]

  set typeCallbacks [object invoke -marshalflags +DynamicCallback \
      System.Data.SQLite.SQLiteTypeCallbacks Create \
      {-callbackflags +Default bindValueCallback1} null 1 null]

  $connection SetTypeCallbacks SPECIAL $typeCallbacks

  sql execute $db {
    INSERT INTO t1 (x) VALUES(?);
  } [list Special Int64 5678]



  list [expr {
    [info exists log(bind)] ? $log(bind) : "<MISSING>"


  }] [sql execute -execute reader -format list $db \
      "SELECT * FROM t1 ORDER BY x;"]


} -cleanup {


  freeDbConnection

  cleanupDb $fileName

  unset -nocomplain typeCallbacks
  unset -nocomplain log connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{{convert true} {command true} {flags\
{UseConnectionBindValueCallbacks, UseParameterNameForTypeName}} {parameter\
true} {typeName Special} {index 1} {userData 1}} {}}}

###############################################################################

................................................................................
  setupDb [set fileName types-3.3.db] "" "" "" \
      "UseConnectionBindValueCallbacks UseParameterNameForTypeName"
} -body {
  sql execute $db {
    CREATE TABLE t1(x SPECIAL);
  }


  set connection [getDbConnection]

  set typeCallbacks [object invoke -marshalflags +DynamicCallback \
      System.Data.SQLite.SQLiteTypeCallbacks Create \
      {-callbackflags +Default bindValueCallback1} null 2 null]

  $connection SetTypeCallbacks SPECIAL $typeCallbacks

  sql execute $db {
    INSERT INTO t1 (x) VALUES(?);
  } [list Special Int64 9999]



  list [expr {
    [info exists log(bind)] ? $log(bind) : "<MISSING>"


  }] [sql execute -execute reader -format list $db \
      "SELECT * FROM t1 ORDER BY x;"]


} -cleanup {


  freeDbConnection

  cleanupDb $fileName

  unset -nocomplain typeCallbacks
  unset -nocomplain log connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{{convert true} {command true} {flags\
{UseConnectionBindValueCallbacks, UseParameterNameForTypeName}} {parameter\
true} {typeName Special} {index 1} {userData 2}} custom}}

###############################################################################

runTest {test types-4.1 {read callback (exception)} -setup {
  unset -nocomplain log

  setupDb [set fileName types-4.1.db] "" "" "" UseConnectionReadValueCallbacks

} -body {
  sql execute $db {
    CREATE TABLE t1(x SPECIAL);
    INSERT INTO t1 (x) VALUES(8888);
  }


  set connection [getDbConnection]

  set typeCallbacks [object invoke -marshalflags +DynamicCallback \
      System.Data.SQLite.SQLiteTypeCallbacks Create null \
      {-callbackflags "+Default ThrowOnError" readValueCallback1} \
      null 3]

  $connection SetTypeCallbacks SPECIAL $typeCallbacks




  list [catch {sql execute -execute reader -format list $db {SELECT * FROM t1 ORDER BY x;}} error] [extractSystemDataSQLiteExceptionMessage $error] [catch {sql execute -execute scalar $db {SELECT COUNT(*) FROM t1;}} error] [extractSystemDataSQLiteExceptionMessage $error] [expr {









    [info exists log(read)] ? $log(read) : "<MISSING>"
  }]


} -cleanup {


  freeDbConnection

  cleanupDb $fileName

  unset -nocomplain typeCallbacks
  unset -nocomplain log connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {1 {reading of value canceled} 0 1 {{convert true}\
{dataReader true} {flags UseConnectionReadValueCallbacks} {eventArgs true}\
{typeName SPECIAL} {index 0} {userData 3}}}}

###############################################################################

................................................................................
  setupDb [set fileName types-5.1.db] "" "" "" \
      "UseConnectionBindValueCallbacks UseParameterNameForTypeName"
} -body {
  sql execute $db {
    CREATE TABLE t1(x SPECIAL);
  }


  set connection [getDbConnection]

  set typeCallbacks [object invoke -marshalflags +DynamicCallback \
      System.Data.SQLite.SQLiteTypeCallbacks Create \
      {-callbackflags "+Default ThrowOnError" bindValueCallback1} \
      null 3 null]

  $connection SetTypeCallbacks SPECIAL $typeCallbacks

  catch {
    sql execute $db {
      INSERT INTO t1 (x) VALUES(?);
    } [list Special Int64 4321]
  }



  list [expr {
    [info exists log(bind)] ? $log(bind) : "<MISSING>"


  }] [sql execute -execute reader -format list $db \
      "SELECT * FROM t1 ORDER BY x;"]


} -cleanup {


  freeDbConnection

  cleanupDb $fileName

  unset -nocomplain typeCallbacks
  unset -nocomplain log connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{{convert true} {command true} {flags\
{UseConnectionBindValueCallbacks, UseParameterNameForTypeName}} {parameter\
true} {typeName Special} {index 1} {userData 3}} {}}}





###############################################################################

rename readValueCallback1 ""
rename bindValueCallback1 ""

###############################################################################

runSQLiteTestEpilogue
runTestEpilogue







<









>







 







>
>



<









>







 







|



|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
|
<
|
>
|
|
>
|
|
<
|
<
|
<
|


|
<
>
|
<
|
<
|
<







|
<
|
<
|
<
|
<
|
>
|
<
|
>
|









|

|
>
>
|
<

>
>
|


<
<
|
>
|
>
>
|
|
>
|
>



<
<
<
|
<
<



<
<

<
<

>
|


<
<

<
>

>
>

<

<
<
<



<








<



>




>
>
|
>




|
>













>





|
|




|



>
|


|






|
>


|



|

<





<

|






>
>





|
|
>













|
|













>



|
|







>
>
|

>
>
|

>
>

>
>




<
|







 







>



|
|







>
>
|

>
>
|

>
>

>
>




<
|







 







>



|
|







>
>
|

>
>
|

>
>

>
>




<
|










|
>






>



|
<




>
>
>
|
>
>
>
>
>
>
>
>
>
|
<
>
>

>
>




|
|







 







>



|
<
|









>
>
|

>
>
|

>
>

>
>




<
|




>
>
>
>










19
20
21
22
23
24
25

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
..
43
44
45
46
47
48
49
50
51
52
53
54

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

136

137
138
139
140
141
142
143

144

145

146
147
148
149

150
151

152

153

154
155
156
157
158
159
160
161

162

163

164

165
166
167

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

186
187
188
189
190
191


192
193
194
195
196
197
198
199
200
201
202
203
204



205


206
207
208


209


210
211
212
213
214


215

216
217
218
219
220

221



222
223
224

225
226
227
228
229
230
231
232

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300

301
302
303
304
305

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381

382
383
384
385
386
387
388
389
...
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429

430
431
432
433
434
435
436
437
...
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477

478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501

502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519

520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
...
541
542
543
544
545
546
547
548
549
550
551
552

553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579

580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
runSQLiteTestPrologue

###############################################################################

proc bindValueCallback1 {
        convert command flags parameter typeName index userData
        completeVarName } {

  lappend ::log(bind) [list convert [isObjectHandle $convert]]
  lappend ::log(bind) [list command [isObjectHandle $command]]
  lappend ::log(bind) [list flags [getStringFromObjectHandle $flags]]
  lappend ::log(bind) [list parameter [isObjectHandle $parameter]]
  lappend ::log(bind) [list typeName [getStringFromObjectHandle $typeName]]
  lappend ::log(bind) [list index [getStringFromObjectHandle $index]]
  lappend ::log(bind) [list userData [getStringFromObjectHandle $userData]]

  if {[getStringFromObjectHandle $userData] == 3} then {
    upvar 1 $completeVarName complete; unset complete
    error "parameter binding canceled"
  }

  if {[getStringFromObjectHandle $userData] == 2} then {
    $parameter DbType String
    $parameter Value custom
  }
................................................................................

  if {[getStringFromObjectHandle $userData] == 1} then {
    upvar 1 $completeVarName complete
    set complete [object invoke -create System.Boolean Parse True]
  }
}

###############################################################################

proc readValueCallback1 {
        convert dataReader flags eventArgs typeName index userData
        completeVarName } {

  lappend ::log(read) [list convert [isObjectHandle $convert]]
  lappend ::log(read) [list dataReader [isObjectHandle $dataReader]]
  lappend ::log(read) [list flags [getStringFromObjectHandle $flags]]
  lappend ::log(read) [list eventArgs [isObjectHandle $eventArgs]]
  lappend ::log(read) [list typeName [getStringFromObjectHandle $typeName]]
  lappend ::log(read) [list index [getStringFromObjectHandle $index]]
  lappend ::log(read) [list userData [getStringFromObjectHandle $userData]]

  if {[getStringFromObjectHandle $userData] == 3} then {
    upvar 1 $completeVarName complete; unset complete
    error "reading of value canceled"
  }

  if {[getStringFromObjectHandle $userData] == 1} then {
    upvar 1 $completeVarName complete
    set complete [object invoke -create System.Boolean Parse True]
  }
................................................................................
  unset -nocomplain typeCallbacks
  unset -nocomplain result connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {0 True True 1 True True}}

###############################################################################

set readArgs [list \
    convert dataReader flags eventArgs typeName index userData \
    completeVarName]

set params [list \
    [list Boolean    GetBoolean    false   BooleanValue    true    0] \
    [list Byte       GetByte       false   ByteValue       true    0] \
    [list Byte       GetBytes      true    BytesValue      false   0] \
    [list Char       GetChar       false   CharValue       true    0] \
    [list Char       GetChars      true    CharsValue      false   0] \
    [list DateTime   GetDateTime   false   DateTimeValue   true    0] \
    [list Decimal    GetDecimal    false   DecimalValue    true    0] \
    [list Double     GetDouble     false   DoubleValue     true    0] \
    [list Single     GetFloat      false   FloatValue      true    0] \
    [list Guid       GetGuid       false   GuidValue       true    \
        00000000-0000-0000-0000-000000000000] \
    [list Int16      GetInt16      false   Int16Value      true    0] \
    [list Int32      GetInt32      false   Int32Value      true    0] \
    [list Int64      GetInt64      false   Int64Value      true    0] \
    [list String     GetString     false   StringValue     false   null] \
    [list Object     GetValue      false   Value           false   null]]

###############################################################################

set expectedResults [list \
    {False False True False False False False False} \

    {0 0 1 0 0 0 0 0} \

    {0 1 48 1 49 3 {49 46 50} 5 {116 104 114\
        101 101} 1 4 27 {50 48 49 54 45 48 54\
        45 49 57 32 49 57 58 53 48 58 48 52 46\
        49 50 51 52 53 54 55} 36 {55 52 102 49\
        102 52 48 50 45 100 100 101 102 45 52\
        48 50 55 45 97 55 56 102 45 51 56 52 55\
        102 97 57 55 98 56 51 48}} \

    "<NUL> <NUL> \x01 <NUL> <NUL> <NUL> <NUL> <NUL>" \

    "0 1 0 1 1 3 {1 . 2} 1 t 1 \x04 1 2 1 7" \

    {{0001-01-01 00:00:00Z} {0001-01-01 00:00:00Z}\
        {0001-01-01 00:00:00Z} {0001-01-01 00:00:00Z}\
        {0001-01-01 00:00:00Z} {0001-01-01 00:00:00Z}\
        {2016-06-19 19:50:04.1234567Z} {0001-01-01\

        00:00:00Z}} \
    {0 0 1 1.2 0 0 0 0} \

    {0 0 1 1.2 0 0 0 0} \

    {0 0 1 1.2 0 0 0 0} \

    {00000000-0000-0000-0000-000000000000\
        00000000-0000-0000-0000-000000000000\
        00000000-0000-0000-0000-000000000000\
        00000000-0000-0000-0000-000000000000\
        00000000-0000-0000-0000-000000000000\
        00060504-0000-0000-0000-000000000000\
        00000000-0000-0000-0000-000000000000\
        74f1f402-ddef-4027-a78f-3847fa97b830} \

    {0 0 1 0 0 0 0 0} \

    {0 0 1 0 0 0 0 0} \

    {0 0 1 0 0 0 0 0} \

    "<NUL> <NUL> <NUL> <NUL> three \x04\x05\x06\
        {2016-06-19 19:50:04.1234567}\
        74f1f402-ddef-4027-a78f-3847fa97b830" \

    {{} 0 1 1.2 three {4 5 6} {2016-06-19\
        19:50:04.1234567}\
        74f1f402-ddef-4027-a78f-3847fa97b830}]

###############################################################################

set savedDateTimeFormat [object invoke Interpreter.GetActive DateTimeFormat]
if {![isObjectHandle $savedDateTimeFormat]} then {set savedDateTimeFormat null}
object invoke Interpreter.GetActive DateTimeFormat [getDateTimeFormat]

###############################################################################

for {set i 0} {$i < [llength $params]} {incr i} {
  foreach {
    typeName methodName isArray propertyName isRequired value
  } [lindex $params $i] break

  set expectedResult [lindex $expectedResults $i]


  #############################################################################

  proc readValueCallback2 $readArgs [subst {
    if {$isArray} then {
      if {\[catch {


        set dataOffset \[\$eventArgs ArrayEventArgs.DataOffset\]

        set buffer \[\$eventArgs -create [appendArgs ArrayEventArgs. \
            $typeName Buffer]\]

        set bufferOffset \[\$eventArgs ArrayEventArgs.BufferOffset\]
        set length \[\$eventArgs ArrayEventArgs.Length\]

        set readValue \[\$dataReader \[\$eventArgs MethodName\] \
            \$index \$dataOffset \$buffer \$bufferOffset \$length\]

        \$eventArgs \[appendArgs Value. $propertyName\] \$readValue
      } error\]} then {



        set readValue \[\$dataReader -tostring GetValue \$index\]



        if {"$typeName" eq "Char"} then {
          set string \[object create -alias String \$readValue\]


          set buffer \[\$string -create -alias ToCharArray]


        } else {
          set buffer \[object invoke -create -alias \
              System.Text.Encoding.UTF8 GetBytes \$readValue\]
        }



        \$eventArgs \[appendArgs Value. $propertyName\] \$buffer

        \$eventArgs ArrayEventArgs.Length \[\$buffer Length\]
      } else {
        set buffer \[\$eventArgs -create [appendArgs ArrayEventArgs. \
            $typeName Buffer]\]


        \$eventArgs \[appendArgs Value. $propertyName\] \$buffer



      }
    } else {
      if {\[catch {

        set readValue \[\$dataReader \[\$eventArgs MethodName\] \$index\]

        if {"$typeName" eq "Char"} then {
          set readValue \[object invoke -create Char Parse \$readValue\]
        }

        \$eventArgs \[appendArgs Value. $propertyName\] \$readValue
      } error\]} then {

        \$eventArgs \[appendArgs Value. $propertyName\] {$value}
      }
    }

    upvar 1 \$completeVarName complete
    set complete \[object invoke -create System.Boolean Parse True\]
  }]

  #############################################################################

  runTest {test [appendArgs types-2. $i] [appendArgs $methodName " callback"] \
      -setup [subst -nocommands {
    set typeName {$typeName}
    set methodName {$methodName}
    set isArray {$isArray}

    setupDb [set fileName [appendArgs types-2. $i .db]] "" "" "" \
        UseConnectionReadValueCallbacks
  }] -body {
    sql execute $db {
      CREATE TABLE t1(x INTEGER, y SPECIAL);
      INSERT INTO t1 (x, y) VALUES(1, NULL);
      INSERT INTO t1 (x, y) VALUES(2, 0);
      INSERT INTO t1 (x, y) VALUES(3, 1);
      INSERT INTO t1 (x, y) VALUES(4, 1.2);
      INSERT INTO t1 (x, y) VALUES(5, 'three');
      INSERT INTO t1 (x, y) VALUES(6, X'040506');
      INSERT INTO t1 (x, y) VALUES(7, '2016-06-19 19:50:04.1234567');
      INSERT INTO t1 (x, y) VALUES(8, '74f1f402-ddef-4027-a78f-3847fa97b830');
    }

    set callback {-callbackflags +Default readValueCallback2}
    set connection [getDbConnection]

    set result [list]

    set typeCallbacks [object invoke -marshalflags +DynamicCallback \
        System.Data.SQLite.SQLiteTypeCallbacks Create null $callback \
        null null]

    $connection SetTypeCallbacks SPECIAL $typeCallbacks

    set dataReader [sql execute -execute reader -format datareader \
        -alias $db "SELECT y FROM t1 ORDER BY x;"]

    while {[$dataReader Read]} {
      if {$isArray} then {
        set buffer [object invoke \
            -create Array CreateInstance $typeName 100]

        if {[catch {
          $dataReader $methodName 0 0 $buffer 0 1
        } value] == 0} then {
          lappend result $value

          if {$value > 0} then {
            set list [object create -alias StringList $buffer]

            lappend result [object invoke StringList GetRange \
                $list 0 [expr {$value - 1}] false]
          }
        } else {
          lappend result [list error(array) $::errorCode]
        }
      } else {
        if {[catch {
          $dataReader $methodName 0
        } value] == 0} then {

          if {$value eq "\x00"} then {
            lappend result <NUL>
          } else {
            lappend result [getStringFromObjectHandle $value]
          }

        } else {
          lappend result [list error(value) $::errorCode]
        }
      }
    }

    set result
  } -cleanup {
    catch {object removecallback $callback}

    unset -nocomplain dataReader
    freeDbConnection

    cleanupDb $fileName

    unset -nocomplain buffer typeCallbacks callback value list
    unset -nocomplain result connection db fileName
    unset -nocomplain typeName methodName isArray
  } -constraints {eagle command.object monoBug28 command.sql compile.DATA\
SQLite System.Data.SQLite} -result $expectedResult}

  rename readValueCallback2 ""
}

###############################################################################

object invoke Interpreter.GetActive DateTimeFormat $savedDateTimeFormat
unset -nocomplain savedDateTimeFormat

###############################################################################

unset -nocomplain i readArgs params typeName methodName isArray propertyName \
    isRequired expectedResults expectedResult

###############################################################################

runTest {test types-3.1 {bind callback (incomplete)} -setup {
  unset -nocomplain log

  setupDb [set fileName types-3.1.db] "" "" "" \
      "UseConnectionBindValueCallbacks UseParameterNameForTypeName"
} -body {
  sql execute $db {
    CREATE TABLE t1(x SPECIAL);
  }

  set callback {-callbackflags +Default bindValueCallback1}
  set connection [getDbConnection]

  set typeCallbacks [object invoke -marshalflags +DynamicCallback \
      System.Data.SQLite.SQLiteTypeCallbacks Create $callback null \
      0 null]

  $connection SetTypeCallbacks SPECIAL $typeCallbacks

  sql execute $db {
    INSERT INTO t1 (x) VALUES(?);
  } [list Special Int64 1234]

  set result [list]

  lappend result [expr {
    [info exists log(bind)] ? $log(bind) : "<MISSING>"
  }]

  lappend result [sql execute -execute reader -format list $db \
      "SELECT * FROM t1 ORDER BY x;"]

  set result
} -cleanup {
  catch {object removecallback $callback}

  freeDbConnection

  cleanupDb $fileName


  unset -nocomplain result typeCallbacks callback log connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{{convert true} {command true} {flags\
{UseConnectionBindValueCallbacks, UseParameterNameForTypeName}} {parameter\
true} {typeName Special} {index 1} {userData 0}} 1234}}

###############################################################################

................................................................................
  setupDb [set fileName types-3.2.db] "" "" "" \
      "UseConnectionBindValueCallbacks UseParameterNameForTypeName"
} -body {
  sql execute $db {
    CREATE TABLE t1(x SPECIAL);
  }

  set callback {-callbackflags +Default bindValueCallback1}
  set connection [getDbConnection]

  set typeCallbacks [object invoke -marshalflags +DynamicCallback \
      System.Data.SQLite.SQLiteTypeCallbacks Create $callback null \
      1 null]

  $connection SetTypeCallbacks SPECIAL $typeCallbacks

  sql execute $db {
    INSERT INTO t1 (x) VALUES(?);
  } [list Special Int64 5678]

  set result [list]

  lappend result [expr {
    [info exists log(bind)] ? $log(bind) : "<MISSING>"
  }]

  lappend result [sql execute -execute reader -format list $db \
      "SELECT * FROM t1 ORDER BY x;"]

  set result
} -cleanup {
  catch {object removecallback $callback}

  freeDbConnection

  cleanupDb $fileName


  unset -nocomplain result typeCallbacks callback log connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{{convert true} {command true} {flags\
{UseConnectionBindValueCallbacks, UseParameterNameForTypeName}} {parameter\
true} {typeName Special} {index 1} {userData 1}} {}}}

###############################################################################

................................................................................
  setupDb [set fileName types-3.3.db] "" "" "" \
      "UseConnectionBindValueCallbacks UseParameterNameForTypeName"
} -body {
  sql execute $db {
    CREATE TABLE t1(x SPECIAL);
  }

  set callback {-callbackflags +Default bindValueCallback1}
  set connection [getDbConnection]

  set typeCallbacks [object invoke -marshalflags +DynamicCallback \
      System.Data.SQLite.SQLiteTypeCallbacks Create $callback null \
      2 null]

  $connection SetTypeCallbacks SPECIAL $typeCallbacks

  sql execute $db {
    INSERT INTO t1 (x) VALUES(?);
  } [list Special Int64 9999]

  set result [list]

  lappend result [expr {
    [info exists log(bind)] ? $log(bind) : "<MISSING>"
  }]

  lappend result [sql execute -execute reader -format list $db \
      "SELECT * FROM t1 ORDER BY x;"]

  set result
} -cleanup {
  catch {object removecallback $callback}

  freeDbConnection

  cleanupDb $fileName


  unset -nocomplain result typeCallbacks callback log connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{{convert true} {command true} {flags\
{UseConnectionBindValueCallbacks, UseParameterNameForTypeName}} {parameter\
true} {typeName Special} {index 1} {userData 2}} custom}}

###############################################################################

runTest {test types-4.1 {read callback (exception)} -setup {
  unset -nocomplain log

  setupDb [set fileName types-4.1.db] "" "" "" \
      UseConnectionReadValueCallbacks
} -body {
  sql execute $db {
    CREATE TABLE t1(x SPECIAL);
    INSERT INTO t1 (x) VALUES(8888);
  }

  set callback {-callbackflags {+Default ThrowOnError} readValueCallback1}
  set connection [getDbConnection]

  set typeCallbacks [object invoke -marshalflags +DynamicCallback \
      System.Data.SQLite.SQLiteTypeCallbacks Create null $callback \

      null 3]

  $connection SetTypeCallbacks SPECIAL $typeCallbacks

  set result [list]

  lappend result [catch {
    sql execute -execute reader -format list $db {SELECT * FROM t1 ORDER BY x;}
  } error]

  lappend result [extractSystemDataSQLiteExceptionMessage $error]

  lappend result [catch {
    sql execute -execute scalar $db {SELECT COUNT(*) FROM t1;}
  } error]

  lappend result [extractSystemDataSQLiteExceptionMessage $error]
  lappend result [expr {[info exists log(read)] ? $log(read) : "<MISSING>"}]


  set result
} -cleanup {
  catch {object removecallback $callback}

  freeDbConnection

  cleanupDb $fileName

  unset -nocomplain error result typeCallbacks callback log connection db \
      fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {1 {reading of value canceled} 0 1 {{convert true}\
{dataReader true} {flags UseConnectionReadValueCallbacks} {eventArgs true}\
{typeName SPECIAL} {index 0} {userData 3}}}}

###############################################################################

................................................................................
  setupDb [set fileName types-5.1.db] "" "" "" \
      "UseConnectionBindValueCallbacks UseParameterNameForTypeName"
} -body {
  sql execute $db {
    CREATE TABLE t1(x SPECIAL);
  }

  set callback {-callbackflags {+Default ThrowOnError} bindValueCallback1}
  set connection [getDbConnection]

  set typeCallbacks [object invoke -marshalflags +DynamicCallback \
      System.Data.SQLite.SQLiteTypeCallbacks Create $callback null \

      3 null]

  $connection SetTypeCallbacks SPECIAL $typeCallbacks

  catch {
    sql execute $db {
      INSERT INTO t1 (x) VALUES(?);
    } [list Special Int64 4321]
  }

  set result [list]

  lappend result [expr {
    [info exists log(bind)] ? $log(bind) : "<MISSING>"
  }]

  lappend result [sql execute -execute reader -format list $db \
      "SELECT * FROM t1 ORDER BY x;"]

  set result
} -cleanup {
  catch {object removecallback $callback}

  freeDbConnection

  cleanupDb $fileName


  unset -nocomplain result typeCallbacks callback log connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{{convert true} {command true} {flags\
{UseConnectionBindValueCallbacks, UseParameterNameForTypeName}} {parameter\
true} {typeName Special} {index 1} {userData 3}} {}}}

###############################################################################

catch {eval object dispose [info objects System#Boolean#*]}

###############################################################################

rename readValueCallback1 ""
rename bindValueCallback1 ""

###############################################################################

runSQLiteTestEpilogue
runTestEpilogue