###############################################################################
#
# tkt-3c00ec5b52.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

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

proc saveSQLiteConvertEnvironment {} {
  foreach name [list \
      Use_SQLiteConvert_DefaultDbType Use_SQLiteConvert_DefaultTypeName] {
    #
    # NOTE: Does the live environment variable exist?  If so, save the value
    #       for later; otherwise, make sure the saved value does not exist
    #       either.  The live environment variables ARE NOT changed by this
    #       procedure.
    #
    if {[uplevel 1 [list info exists env($name)]]} then {
      uplevel 1 [list set savedEnv($name) [uplevel 1 [list set env($name)]]]
    } else {
      uplevel 1 [list unset -nocomplain savedEnv($name)]
    }
  }
}

proc restoreSQLiteConvertEnvironment {} {
  foreach name [list \
      Use_SQLiteConvert_DefaultDbType Use_SQLiteConvert_DefaultTypeName] {
    #
    # NOTE: Does the saved environment variable exist?  If so, restore the
    #       saved value and then unset it; otherwise, make sure the live
    #       environment variable does not exist either (i.e. it was not set
    #       to begin with).  Both the saved and live environment variables
    #       ARE changed by this procedure.
    #
    if {[uplevel 1 [list info exists savedEnv($name)]]} then {
      uplevel 1 [list set env($name) [uplevel 1 [list set savedEnv($name)]]]
      uplevel 1 [list unset -nocomplain savedEnv($name)]
    } else {
      uplevel 1 [list unset -nocomplain env($name)]
    }
  }
}

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

runTest {test tkt-3c00ec5b52-1.1 {default types, via connection} -setup {
  setupDb [set fileName tkt-3c00ec5b52-1.1.db] "" "" "" UseConnectionTypes \
      "DefaultDbType=String;DefaultTypeName=TEXT;"
} -body {
  set connection [getDbConnection]
  set result [list]

  lappend result [object invoke -flags +NonPublic \
      System.Data.SQLite.SQLiteConvert TypeNameToDbType $connection "" None]

  lappend result [object invoke -flags +NonPublic \
      System.Data.SQLite.SQLiteConvert DbTypeToTypeName $connection Object None]

  set result
} -cleanup {
  freeDbConnection

  unset -nocomplain result connection

  cleanupDb $fileName

  unset -nocomplain db fileName
} -constraints \
{eagle monoBug28 command.sql compile.DATA SQLite System.Data.SQLite} -result \
{String TEXT}}

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

runTest {test tkt-3c00ec5b52-1.2 {fallback default types w/flag} -setup {
  setupDb [set fileName tkt-3c00ec5b52-1.2.db] "" "" "" UseConnectionTypes
} -body {
  set connection [getDbConnection]
  set result [list]

  lappend result [object invoke -flags +NonPublic \
      System.Data.SQLite.SQLiteConvert TypeNameToDbType $connection "" None]

  lappend result [object invoke -flags +NonPublic \
      System.Data.SQLite.SQLiteConvert DbTypeToTypeName $connection Object None]

  set result
} -cleanup {
  freeDbConnection

  unset -nocomplain result connection

  cleanupDb $fileName

  unset -nocomplain db fileName
} -constraints \
{eagle monoBug28 command.sql compile.DATA SQLite System.Data.SQLite} -result \
{Object {}}}

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

runTest {test tkt-3c00ec5b52-1.3 {fallback default types w/o flag} -setup {
  setupDb [set fileName tkt-3c00ec5b52-1.3.db]
} -body {
  set connection [getDbConnection]
  set result [list]

  lappend result [object invoke -flags +NonPublic \
      System.Data.SQLite.SQLiteConvert TypeNameToDbType $connection "" None]

  lappend result [object invoke -flags +NonPublic \
      System.Data.SQLite.SQLiteConvert DbTypeToTypeName $connection Object None]

  set result
} -cleanup {
  freeDbConnection

  unset -nocomplain result connection

  cleanupDb $fileName

  unset -nocomplain db fileName
} -constraints \
{eagle monoBug28 command.sql compile.DATA SQLite System.Data.SQLite} -result \
{Object {}}}

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

runTest {test tkt-3c00ec5b52-1.4 {default types, via environment} -setup {
  saveSQLiteConvertEnvironment

  set env(Use_SQLiteConvert_DefaultDbType) String
  set env(Use_SQLiteConvert_DefaultTypeName) TEXT

  setupDb [set fileName tkt-3c00ec5b52-1.4.db]
} -body {
  set connection [getDbConnection]
  set result [list]

  lappend result [object invoke -flags +NonPublic \
      System.Data.SQLite.SQLiteConvert TypeNameToDbType $connection "" None]

  lappend result [object invoke -flags +NonPublic \
      System.Data.SQLite.SQLiteConvert DbTypeToTypeName $connection Object None]

  set result
} -cleanup {
  freeDbConnection

  unset -nocomplain result connection

  cleanupDb $fileName
  restoreSQLiteConvertEnvironment

  unset -nocomplain db fileName savedEnv
} -constraints \
{eagle monoBug28 command.sql compile.DATA SQLite System.Data.SQLite} -result \
{String TEXT}}

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

runTest {test tkt-3c00ec5b52-1.5 {default types, with unique index} -setup {
  saveSQLiteConvertEnvironment

  set env(Use_SQLiteConvert_DefaultDbType) String
  set env(Use_SQLiteConvert_DefaultTypeName) TEXT

  setupDb [set fileName tkt-3c00ec5b52-1.5.db]
} -body {
  sql execute $db "CREATE TABLE t1(x UNIQUE);"

  set connection [getDbConnection]
  set dataTable [$connection -alias GetSchema INDEXES]
  set results [list]

  object foreach -alias dataRow [set dataRows [$dataTable -alias Rows]] {
    lappend results [list [$dataRow Item TABLE_NAME] \
        [$dataRow Item INDEX_NAME] [$dataRow Item UNIQUE]]
  }

  set results
} -cleanup {
  freeDbConnection

  unset -nocomplain results dataRow dataRows dataTable connection

  cleanupDb $fileName
  restoreSQLiteConvertEnvironment

  unset -nocomplain db fileName savedEnv
} -constraints \
{eagle monoBug28 command.sql compile.DATA SQLite System.Data.SQLite} -result \
{{t1 sqlite_autoindex_t1_1 True}}}

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

rename restoreSQLiteConvertEnvironment ""
rename saveSQLiteConvertEnvironment ""

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

runSQLiteTestEpilogue
runTestEpilogue
