###############################################################################
#
# tkt-b167206ad3.eagle --
#
# Written by Joe Mistachkin.
# Released to the public domain, use at your own risk!
#
###############################################################################

package require Eagle
package require Eagle.Library
package require Eagle.Test

runTestPrologue

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

package require System.Data.SQLite.Test
runSQLiteTestPrologue

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

runTest {test tkt-b167206ad3-1.1 {type affinity for DECIMAL} -setup {
  setupDb [set fileName tkt-b167206ad3-1.1.db]
} -body {
  sql execute $db {
    CREATE TABLE t1(x INTEGER, y DECIMAL(38,9));
    INSERT INTO t1 (x, y) VALUES(1, 123456789123456780);
    INSERT INTO t1 (x, y) VALUES(2, '123456789123456780');
  }

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

  set result [list]

  while {[$dataReader Read]} {
    set x [$dataReader -create -alias GetValue 0]
    set y1 [$dataReader -create -alias GetDecimal 1]
    set y2 [$dataReader -create -alias GetValue 1]

    lappend result [list [$x ToString] [$y1 ToString] [$y2 ToString]]
  }

  set result
} -cleanup {
  unset -nocomplain dataReader

  cleanupDb $fileName

  unset -nocomplain y2 y1 x result db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{1 123456789123456780 123456789123457000} {2\
123456789123456780 123456789123457000}}}

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

runTest {test tkt-b167206ad3-1.2 {type affinity for DECIMAL} -setup {
  setupDb [set fileName tkt-b167206ad3-1.2.db] "" "" "" GetDecimalAsText
} -body {
  sql execute $db {
    CREATE TABLE t1(x INTEGER, y DECIMAL(38,9));
    INSERT INTO t1 (x, y) VALUES(1, 123456789123456780);
    INSERT INTO t1 (x, y) VALUES(2, '123456789123456780');
  }

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

  set result [list]

  while {[$dataReader Read]} {
    set x [$dataReader -create -alias GetValue 0]
    set y1 [$dataReader -create -alias GetDecimal 1]
    set y2 [$dataReader -create -alias GetValue 1]

    lappend result [list [$x ToString] [$y1 ToString] [$y2 ToString]]
  }

  set result
} -cleanup {
  unset -nocomplain dataReader

  cleanupDb $fileName

  unset -nocomplain y2 y1 x result db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{1 123456789123456780 123456789123456780} {2\
123456789123456780 123456789123456780}}}

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

runTest {test tkt-b167206ad3-1.3 {type affinity for NUMERIC} -setup {
  setupDb [set fileName tkt-b167206ad3-1.3.db]
} -body {
  sql execute $db {
    CREATE TABLE t1(x INTEGER, y NUMERIC);
    INSERT INTO t1 (x, y) VALUES(1, 123456789123456780);
    INSERT INTO t1 (x, y) VALUES(2, '123456789123456780');
  }

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

  set result [list]

  while {[$dataReader Read]} {
    set x [$dataReader -create -alias GetValue 0]
    set y1 [$dataReader -create -alias GetDecimal 1]
    set y2 [$dataReader -create -alias GetValue 1]

    lappend result [list [$x ToString] [$y1 ToString] [$y2 ToString]]
  }

  set result
} -cleanup {
  unset -nocomplain dataReader

  cleanupDb $fileName

  unset -nocomplain y2 y1 x result db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{1 123456789123456780 123456789123457000} {2\
123456789123456780 123456789123457000}}}

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

runTest {test tkt-b167206ad3-1.4 {type affinity for NUMERIC} -setup {
  setupDb [set fileName tkt-b167206ad3-1.4.db] "" "" "" GetDecimalAsText
} -body {
  sql execute $db {
    CREATE TABLE t1(x INTEGER, y NUMERIC);
    INSERT INTO t1 (x, y) VALUES(1, 123456789123456780);
    INSERT INTO t1 (x, y) VALUES(2, '123456789123456780');
  }

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

  set result [list]

  while {[$dataReader Read]} {
    set x [$dataReader -create -alias GetValue 0]
    set y1 [$dataReader -create -alias GetDecimal 1]
    set y2 [$dataReader -create -alias GetValue 1]

    lappend result [list [$x ToString] [$y1 ToString] [$y2 ToString]]
  }

  set result
} -cleanup {
  unset -nocomplain dataReader

  cleanupDb $fileName

  unset -nocomplain y2 y1 x result db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{1 123456789123456780 123456789123456780} {2\
123456789123456780 123456789123456780}}}

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

runTest {test tkt-b167206ad3-2.1 {bind type for DECIMAL} -setup {
  setupDb [set fileName tkt-b167206ad3-2.1.db]
} -body {
  sql execute $db {
    CREATE TABLE t1(x INTEGER, y DECIMAL(38,9));
  }

  set d(1) [object invoke -create Decimal Parse 123456789123456780]
  set d(2) [object invoke -create Decimal Parse 12345678912345.1556346]

  sql execute $db "INSERT INTO t1 (x, y) VALUES(1, ?);" \
      [list param1 Decimal $d(1)]

  sql execute $db "INSERT INTO t1 (x, y) VALUES(2, ?);" \
      [list param1 Decimal $d(2)]

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

  set result [list]

  while {[$dataReader Read]} {
    set x [$dataReader -create -alias GetValue 0]
    set y1 [$dataReader -create -alias GetDecimal 1]
    set y2 [$dataReader -create -alias GetValue 1]

    lappend result [list [$x ToString] [$y1 ToString] [$y2 ToString]]
  }

  set result
} -cleanup {
  unset -nocomplain dataReader

  cleanupDb $fileName

  unset -nocomplain y2 y1 x d result db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{1 123456789123456780 123456789123457000} {2\
12345678912345.2 12345678912345.2}}}

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

runTest {test tkt-b167206ad3-2.2 {bind type for DECIMAL} -setup {
  setupDb [set fileName tkt-b167206ad3-2.2.db] "" "" "" \
      "BindDecimalAsText GetAllAsText"
} -body {
  sql execute $db {
    CREATE TABLE t1(x INTEGER, y DECIMAL(38,9));
  }

  set d(1) [object invoke -create Decimal Parse 123456789123456780]
  set d(2) [object invoke -create Decimal Parse 12345678912345.1556346]

  sql execute $db "INSERT INTO t1 (x, y) VALUES(1, ?);" \
      [list param1 Decimal $d(1)]

  sql execute $db "INSERT INTO t1 (x, y) VALUES(2, ?);" \
      [list param1 Decimal $d(2)]

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

  set result [list]

  while {[$dataReader Read]} {
    set x [$dataReader -create -alias GetValue 0]
    set y1 [$dataReader -create -alias GetDecimal 1]
    set y2 [$dataReader -create -alias GetValue 1]

    lappend result [list [$x ToString] [$y1 ToString] [$y2 ToString]]
  }

  set result
} -cleanup {
  unset -nocomplain dataReader

  cleanupDb $fileName

  unset -nocomplain y2 y1 x d result db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{1 123456789123456780 123456789123456780} {2\
12345678912345.1556346 12345678912345.1556346}}}

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

runTest {test tkt-b167206ad3-2.3 {bind type for NUMERIC} -setup {
  setupDb [set fileName tkt-b167206ad3-2.3.db]
} -body {
  sql execute $db {
    CREATE TABLE t1(x INTEGER, y NUMERIC);
  }

  set d(1) [object invoke -create Decimal Parse 123456789123456780]
  set d(2) [object invoke -create Decimal Parse 12345678912345.1556346]

  sql execute $db "INSERT INTO t1 (x, y) VALUES(1, ?);" \
      [list param1 Decimal $d(1)]

  sql execute $db "INSERT INTO t1 (x, y) VALUES(2, ?);" \
      [list param1 Decimal $d(2)]

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

  set result [list]

  while {[$dataReader Read]} {
    set x [$dataReader -create -alias GetValue 0]
    set y1 [$dataReader -create -alias GetDecimal 1]
    set y2 [$dataReader -create -alias GetValue 1]

    lappend result [list [$x ToString] [$y1 ToString] [$y2 ToString]]
  }

  set result
} -cleanup {
  unset -nocomplain dataReader

  cleanupDb $fileName

  unset -nocomplain y2 y1 x d result db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{1 123456789123456780 123456789123457000} {2\
12345678912345.2 12345678912345.2}}}

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

runTest {test tkt-b167206ad3-2.4 {bind type for NUMERIC} -setup {
  setupDb [set fileName tkt-b167206ad3-2.4.db] "" "" "" \
      "BindDecimalAsText GetAllAsText"
} -body {
  sql execute $db {
    CREATE TABLE t1(x INTEGER, y NUMERIC);
  }

  set d(1) [object invoke -create Decimal Parse 123456789123456780]
  set d(2) [object invoke -create Decimal Parse 12345678912345.1556346]

  sql execute $db "INSERT INTO t1 (x, y) VALUES(1, ?);" \
      [list param1 Decimal $d(1)]

  sql execute $db "INSERT INTO t1 (x, y) VALUES(2, ?);" \
      [list param1 Decimal $d(2)]

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

  set result [list]

  while {[$dataReader Read]} {
    set x [$dataReader -create -alias GetValue 0]
    set y1 [$dataReader -create -alias GetDecimal 1]
    set y2 [$dataReader -create -alias GetValue 1]

    lappend result [list [$x ToString] [$y1 ToString] [$y2 ToString]]
  }

  set result
} -cleanup {
  unset -nocomplain dataReader

  cleanupDb $fileName

  unset -nocomplain y2 y1 x d result db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {{1 123456789123456780 123456789123456780} {2\
12345678912345.1556346 12345678912345.1556346}}}

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

runSQLiteTestEpilogue
runTestEpilogue
