if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest 2
    namespace import -force ::tcltest::*
}

#tcltest::verbose 100
#tcltest::debug 100

set flist [glob -nocomplain -dir .. libpgtcl*[info sharedlibextension]]
set flist [concat $flist [glob -nocomplain libpgtcl*[info sharedlibextension]]]

if {[llength $flist] == 0} {
    puts "\nCan not find a shared lib file\n"
    exit
}


puts [list loading [lindex $flist end] ...]
load [lindex $flist end]

if [file exists $env(HOME)/.conninfo.tcl] {
    puts [list sourcing $env(HOME)/.conninfo.tcl ...]
    source $env(HOME)/.conninfo.tcl
} elseif [file exists conninfo.tcl] {
    puts [list sourcing conninfo.tcl ...]
    source conninfo.tcl
}

puts [list running tests ...]
#
#
#
test pgtcl-1.1 {connect to db using old connection string} -body {

    if [info exists ::conninfo(password)] {
        set conn [pg_connect $::conninfo(dbname) \
            -connhandle pgsql3 \
            -host $::conninfo(host) \
            -port $::conninfo(port) \
            -user $::conninfo(user) \
	    -password $::conninfo(password)]
    } else {
        set conn [pg_connect $::conninfo(dbname) \
            -connhandle pgsql3 \
            -host $::conninfo(host) \
            -port $::conninfo(port) \
            -user $::conninfo(user)]
    }

    pg_disconnect $conn

    set conn
} -result pgsql3


#
#
#
test pgtcl-1.2 {connect to db using conninfo string} -body {

    set l [list dbname=$::conninfo(dbname) host=$::conninfo(host) port=$::conninfo(port) user=$::conninfo(user)]
    if [info exists ::conninfo(password)] {
	lappend l password=$::conninfo(password)
    }
    set conn [pg_connect -connhandle pgsql3 -conninfo [join $l " "]]

    pg_disconnect $conn

    set conn
} -result pgsql3

#
#
#
test pgtcl-1.3 {connect to db using connlist option} -body {

    set conn [pg::connect -connhandle pgsql3 -connlist [array get ::conninfo]]

    pg_disconnect $conn

    set conn
} -result pgsql3

#
#
#
test pgtcl-1.4 {connect to db using -connhandle option} -body {

    set conn [pg::connect -connlist [array get ::conninfo] -connhandle myhan]

    pg_disconnect $conn

    set conn
} -result myhan

#
#
#
test pgtcl-2.1 {use connection as command} -body {

    set conn [pg::connect -connlist [array get ::conninfo] -connhandle myhan]

    set res [myhan exec "SELECT * FROM Pg_class LIMIT 5"]

    pg_result $res -clear

    pg_disconnect $conn

    set res
} -result myhan.0

#
#
#
test pgtcl-3.1 {test getting connection handles} -body {

    set cnt 3
    foreach C [list 1 2 3] {
        set myconn($C) [pg::connect -connlist [array get ::conninfo] -connhandle pgsql$cnt]
        incr cnt
    }

    #set conns [lsort [pg::dbinfo]]
    set conns [lsort [pg::dbinfo connections]]

    foreach C [list 1 2 3] {
        pg::disconnect $myconn($C)
    }

    set conns
} -result [lsort [list pgsql3 pgsql4 pgsql5]]

#
#
#
test pgtcl-3.2 {test getting connection handles, after disconnect} -body {

    set cnt 3
    foreach C [list 1 2 3] {
        set myconn($C) [pg::connect -connlist [array get ::conninfo] -connhandle pgsql$cnt]
        incr cnt
    }

    foreach C [list 1 2 3] {
        pg::disconnect $myconn($C)
    }

    #pg::conninfo
    pg::dbinfo connections
} -result [list]

#
#
#
test pgtcl-3.3 {test renaming of connection handle command} -body {

    set conn [pg::connect -connlist [array get ::conninfo] -connhandle pgsql3]

    rename $conn MYCONN
    set res [MYCONN exec "SELECT * FROM Pg_class LIMIT 5"]

    pg_result $res -clear

    pg_disconnect $conn

    set res

} -result pgsql3.0


#
#
#
test pgtcl-3.4 {test deleting of a command handle} -body {
    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT * FROM Pg_class  WHERE relname='tables'"]

    pg::result $res -clear
    rename $conn {}

    #pg::conninfo
    pg::dbinfo connections
} -result [list]


#
#
test pgtcl-4.1 {get result handles} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo] -connhandle pgsql9]

    foreach R [list 1 2 3 4] {
        set res($R) [$conn exec "SELECT * FROM Pg_class LIMIT $R"]
    }

    #set results [lsort [pg::results $conn]]
    set results [lsort [pg::dbinfo results $conn]]

    foreach R [list 1 2 3 4] {

        pg_result $res($R) -clear
    }

    pg_disconnect $conn

    set results

} -result [lsort [list pgsql9.0 pgsql9.1 pgsql9.2 pgsql9.3]]

#
##
#
test pgtcl-4.2 {test using result handle command} -body {

    catch {unset res}

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT * FROM Pg_class LIMIT 1"]

    set num [$res -numTuples]
    $res -clear

    pg_disconnect $conn

    set num
} -result 1

#
#
#
test pgtcl-4.3 {test deleting of a result handle} -body {
    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT * FROM Pg_class  WHERE relname='tables'"]

    rename $res {}

    #pg::result $res -clear
    pg::dbinfo results $conn

} -result [list]

## only check if have dicts installed
if {[llength [info commands dict]] == 1} {
#
#
#
test pgtcl-4.4 {get results as a DICT} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT * FROM Pg_class  WHERE relname='pg_tables'"]

    set results [pg::result $res -dict]

    pg_result $res -clear

    pg_disconnect $conn

    dict get $results 0 relname

} -result pg_tables
}

test pgtcl-4.5 {get results as a list} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT relname
                           FROM Pg_class
                          WHERE relname LIKE 'pg_%'
                       ORDER BY relname
                          LIMIT 5"]

    set results [pg::result $res -list]

    pg_result $res -clear

    pg_disconnect $conn

    lsort $results

} -result [list pg_aggregate pg_aggregate_fnoid_index pg_am pg_am_name_index pg_am_oid_index]


#
#
#
test pgtcl-4.6 {get results as a list of lists} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT relname, relkind
                           FROM Pg_class
                          WHERE relname LIKE 'pg_%'
                       ORDER BY relname
                          LIMIT 3"]

    set results [pg::result $res -llist]

    pg_result $res -clear

    pg_disconnect $conn

    lsort $results

} -result [list [list pg_aggregate r] [list pg_aggregate_fnoid_index i] [list pg_am r]]

#
#
#
test pgtcl-4.7 {get results an array with -assign} -body {

    unset -nocomplain res ARR

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT relname, relkind
                           FROM Pg_class
                          WHERE relname LIKE 'pg_%'
                       ORDER BY relname
                          LIMIT 1"]

    pg::result $res -assign ARR

    pg_result $res -clear

    pg_disconnect $conn

    lsort [array get ARR]

} -result [lsort [list 0,relname pg_aggregate 0,relkind r]]

#
#
#
test pgtcl-4.8 {get results an array with -assignbyidx} -body {

    unset -nocomplain ARR res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT oid, relname, relkind
                           FROM Pg_class
                          WHERE relname LIKE 'pg_%'
                       ORDER BY relname
                          LIMIT 1"]

    pg::result $res -assignbyidx ARR

    pg_result $res -clear

    pg_disconnect $conn

    lsort [array get ARR]

} -result [lsort [list 2600,relname pg_aggregate 2600,relkind r]]

#
#
#
test pgtcl-4.9 {get results an array with -assignbyidx with appendstr} -body {

    unset -nocomplain ARR res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT '0', relname, relkind
                           FROM Pg_class
                          WHERE relname LIKE 'pg_%'
                       ORDER BY relname
                          LIMIT 1"]

    pg::result $res -assignbyidx ARR X

    pg_result $res -clear

    pg_disconnect $conn

    lsort [array get ARR]

} -result [lsort [list 0,relnameX pg_aggregate 0,relkindX r]]

#
#
#
test pgtcl-4.10 {test pg_result -numAttrs} -body {

    unset -nocomplain res num

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT oid, relname, relkind
                           FROM Pg_class
                          WHERE relname LIKE 'pg_%'
                       ORDER BY relname
                          LIMIT 1"]

    set num [pg::result $res -numAttrs]

    pg_result $res -clear

    pg_disconnect $conn

    set num
} -result 3

#
#
#
test pgtcl-4.11 {test pg_result -numTuples} -body {

    unset -nocomplain res num

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT oid, relname, relkind
                           FROM Pg_class
                          WHERE relname LIKE 'pg_%'
                       ORDER BY relname
                          LIMIT 10"]

    set num [pg::result $res -numTuples]

    pg_result $res -clear

    pg_disconnect $conn

    set num
} -result 10


#
#
#
test pgtcl-4.12 {test pg_result -status} -body {

    unset -nocomplain res stat

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT 1
                           FROM Pg_class
                          WHERE relname LIKE 'pg_%'
                       ORDER BY relname
                          LIMIT 1"]

    set stat [pg::result $res -status]

    pg_result $res -clear

    pg_disconnect $conn

    set stat
} -result "PGRES_TUPLES_OK"

#
#
#
test pgtcl-4.13 {test pg_result -error} -body {

    unset -nocomplain res stat

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT 1
                           FROM Pg_junk"]

    set stat [string match "*does not exist*" [pg::result $res -error]]

    pg_result $res -clear

    pg_disconnect $conn

    set stat
} -result 1

#
#
#
test pgtcl-4.14 {test pg_result -conn} -body {

    unset -nocomplain res stat

    set conn [pg::connect -connlist [array get ::conninfo] -connhandle PGTEST]

    set res [$conn exec "SELECT 1
                           FROM Pg_class"]

    set stat [pg_result $res -conn]

    pg_result $res -clear

    pg_disconnect $conn

    set stat
} -result PGTEST

#
#
#
test pgtcl-4.15 {test pg_result -getTuple} -body {

    unset -nocomplain res stat

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT 'one', 'two'
                           FROM Pg_class
                          WHERE relname LIKE 'pg_%'
                       ORDER BY relname
                          LIMIT 1"]

    set stat [pg_result $res -getTuple 0]

    pg_result $res -clear

    pg_disconnect $conn

    set stat
} -result [list one two]

#
#
#
test pgtcl-4.16 {test pg_result -typleArray} -body {

    unset -nocomplain res ARR

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT 'one' AS col1, 'two' AS col2
                           FROM Pg_class
                          WHERE relname LIKE 'pg_%'
                       ORDER BY relname
                          LIMIT 1"]

    pg_result $res -tupleArray 0 ARR

    pg_result $res -clear

    pg_disconnect $conn

    lsort [array get ARR]

} -result [lsort [list col1 one col2 two]]

#
#
#
test pgtcl-4.17 {test pg_result -attributes} -body {

    unset -nocomplain res stat

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT 'one' AS col1, 'two' AS col2
                           FROM Pg_class
                          WHERE relname LIKE 'pg_%'
                       ORDER BY relname
                          LIMIT 1"]

    set stat [pg_result $res -attributes]

    pg_result $res -clear

    pg_disconnect $conn

    lsort $stat

} -result [lsort [list col1 col2]]

#
#
#
test pgtcl-4.18 {test pg_result -lAttributes} -body {

    unset -nocomplain res stat

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec "SELECT 'one' AS col1, 'two' AS col2
                           FROM Pg_class
                          WHERE relname LIKE 'pg_%'
                       ORDER BY relname
                          LIMIT 1"]

    set stat [pg_result $res -lAttributes]

    pg_result $res -clear

    pg_disconnect $conn

# drop actual type details (oid, size) because they're not stable

    set names {}
    foreach tuple $stat {
	lappend names [lindex $tuple 0]
    }

    set names
} -result [list col1 col2]

#
#
#
test pgtcl-5.1 {quote a string} -body {

    set qstr [pg::quote "o'reilly"]

    set qstr
} -result 'o''reilly'

#
#
#
test pgtcl-5.2 {escape a binary string} -body {

    set bn [binary format a* \000\\HELLO's]
    set qstr [pg::escape_bytea $bn]

    set qstr
} -result \\000\\\\HELLO''s

#
#
#
test pgtcl-5.3 {unescape a binary string} -body {

    set qstr [pg::unescape_bytea "\\\\000\\\\\\\\HELLO\\'s"]

    set qstr
} -result [binary format a* \\000\\\\HELLO\'s]



#
#
#
test pgtcl-6.1 {using pg_exec with params} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec {SELECT relname
                           FROM Pg_class
                          WHERE relname LIKE $1
                       ORDER BY relname
                          LIMIT $2} "pg_%" 5]

    set results [pg::result $res -list]

    pg_result $res -clear

    pg_disconnect $conn

    lsort $results

} -result [list pg_aggregate pg_aggregate_fnoid_index pg_am pg_am_name_index pg_am_oid_index]


#
#
#
test pgtcl-6.2 {using pg_exec_prepared with params} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec {PREPARE test_prepare (integer)
                          AS SELECT relname
                           FROM Pg_class
                          WHERE relname LIKE 'pg_%'
                       ORDER BY relname
                          LIMIT $1}]

    set res [pg::exec_prepared $conn test_prepare 5]

    set results [pg::result $res -list]

    if {[$res -status] != "PGRES_TUPLES_OK"} {
            puts "[$res -status] [pg::result $res -error] during select"
    }

    pg_result $res -clear

    pg_disconnect $conn

    lsort $results

} -result [list pg_aggregate pg_aggregate_fnoid_index pg_am pg_am_name_index pg_am_oid_index]

#
#
#
test pgtcl-6.3 {using pg_exec with named params} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    array set params {pattern "pg_%" limit 5}

    set res [$conn exec -paramarray params {SELECT relname
                           FROM Pg_class
                          WHERE relname LIKE `pattern`
                       ORDER BY relname
                          LIMIT `limit`}]

    set results [pg::result $res -list]

    pg_result $res -clear

    pg_disconnect $conn

    lsort $results

} -result [list pg_aggregate pg_aggregate_fnoid_index pg_am pg_am_name_index pg_am_oid_index]

#
#
#
test pgtcl-6.4 {using pg_exec with embedded variables} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set pattern "pg_%"
    set limit 5

    set res [$conn exec -variables {SELECT relname
                           FROM Pg_class
                          WHERE relname LIKE :pattern
                       ORDER BY relname
                          LIMIT :limit}]

    set results [pg::result $res -list]

    pg_result $res -clear

    pg_disconnect $conn

    lsort $results

} -result [list pg_aggregate pg_aggregate_fnoid_index pg_am pg_am_name_index pg_am_oid_index]

#
#
#
test pgtcl-6.5 {pg_select error code} -body {
    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    catch {$conn select "SELECT * FROM UNKNOWN_TABLE_GLARGH;" _ {break}}

    set res $::errorCode

    pg_disconnect $conn

    set res
} -result {POSTGRESQL PGRES_FATAL_ERROR {ERROR:  relation "unknown_table_glargh" does not exist}}

#
#
#
test pgtcl-6.6 {using pg_exec with statement starting with a comment} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set res [$conn exec {-- something about this statement here

			 SELECT relname
                           FROM Pg_class
                          WHERE relname LIKE 'pg_%'
                       ORDER BY relname
                          LIMIT 5}]

    set results [pg::result $res -list]

    pg_result $res -clear

    pg_disconnect $conn

    lsort $results

} -result [list pg_aggregate pg_aggregate_fnoid_index pg_am pg_am_name_index pg_am_oid_index]

#
#
#
test pgtcl-7.1 {using pg_select} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set names {}

    pg::select $conn "SELECT relname FROM Pg_class LIMIT 5" resultArr {
        set headers $resultArr(.headers)
        set numcols $resultArr(.numcols)
        set tupno $resultArr(.tupno)

        lappend names $resultArr(relname)

    }

    rename $conn {}

    llength $names

} -result 5



#
#
#
test pgtcl-7.2 {using pg_select via handle} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set names {}

    $conn select "SELECT relname FROM Pg_class LIMIT 5" resultArr {
        set headers $resultArr(.headers)
        set numcols $resultArr(.numcols)
        set tupno $resultArr(.tupno)

        lappend names $resultArr(relname)

    }

    rename $conn {}

    llength $names

} -result 5


#
#
#
test pgtcl-7.3 {using pg_select with parameters} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set names {}

    $conn select -params {pg_% 5} {SELECT relname FROM Pg_class WHERE relname like $1 LIMIT $2} resultArr {
        set headers $resultArr(.headers)
        set numcols $resultArr(.numcols)
        set tupno $resultArr(.tupno)

        lappend names $resultArr(relname)
    }

    rename $conn {}

    llength $names

} -result 5


#
#
#
test pgtcl-7.4 {using pg_select with named parameters} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set names {}

    array set params {pattern pg_% limit 5}

    $conn select -paramarray params "SELECT relname FROM Pg_class WHERE relname like `pattern` LIMIT `limit`" resultArr {
        set headers $resultArr(.headers)
        set numcols $resultArr(.numcols)
        set tupno $resultArr(.tupno)

        lappend names $resultArr(relname)
    }

    rename $conn {}

    llength $names

} -result 5

#
#
#
test pgtcl-7.5 {using pg_select with embedded variables} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set names {}

    set pattern "pg_%"
    set limit 5

    $conn select -variables "SELECT relname FROM Pg_class WHERE relname like :pattern LIMIT :limit" resultArr {
        set headers $resultArr(.headers)
        set numcols $resultArr(.numcols)
        set tupno $resultArr(.tupno)

        lappend names $resultArr(relname)
    }

    rename $conn {}

    llength $names

} -result 5

#
#
#
test pgtcl-7.6 {pg_select -count} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set names {}

    pg::select $conn -count ntuples "SELECT relname FROM Pg_class LIMIT 5" resultArr {
        set headers $resultArr(.headers)
        set numcols $resultArr(.numcols)
        set tupno $resultArr(.tupno)

        lappend names $resultArr(relname)

	set count $ntuples
    }

    rename $conn {}

    list [llength $names] $count

} -result {5 5}



#
#
#
test pgtcl-8.1 {using pg_execute} -body {

    unset -nocomplain res names

    set conn [pg::connect -connlist [array get ::conninfo]]

    set names [list]
    pg::execute -array resultArr $conn "SELECT relname FROM Pg_class LIMIT 5" {

        lappend names $resultArr(relname)

    }

    rename $conn {}
    llength $names

} -result 5

#
#
#
test pgtcl-8.2 {using connection command handle for pg_execute, no options} -body {

    unset -nocomplain res names

    set conn [pg::connect -connlist [array get ::conninfo]]

    $conn execute -array resultArr "SELECT relname FROM Pg_class LIMIT 5" {
        lappend ::names $resultArr(relname)

    }

    rename $conn {}

    llength $::names

} -result 5

#
#
#
test pgtcl-8.3 {using connection command handle for pg_execute, -array option} -body {

    unset -nocomplain res names

    set conn [pg::connect -connlist [array get ::conninfo]]

    $conn execute -array resultArr -oid OID "SELECT relname FROM Pg_class LIMIT 5" {
        lappend ::names $resultArr(relname)

    }

    rename $conn {}

    llength $::names

} -result 5

#
#
#
test pgtcl-9.1 {dbinfo version} -body {
    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set val [pg::dbinfo version $conn]
    set val2 [$conn version]

    lappend res [string equal $val $val2]
    ::pg::disconnect $conn

    lappend res [regexp {(7|8|9|10|11|12)0([0-9])[012]([0-9])} $val]

} -result [list 1 1]

#
#
#
test pgtcl-9.2 {dbinfo protocol} -body {
    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set val [pg::dbinfo protocol $conn]
    set val2 [$conn protocol]
    lappend res [string equal $val $val2]
    ::pg::disconnect $conn

    lappend res [regexp {2|3} $val]

} -result [list 1 1]

#
#
#
test pgtcl-9.3 {dbinfo param} -body {
    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set val [pg::dbinfo param $conn server_version]
    set val2 [$conn param server_version]
    lappend res [string equal $val $val2]
    ::pg::disconnect $conn

    set match [regexp {(7|8|9|10|11|12)\.([0-9])\.([0-9])} $val]
    if {!$match} {
	set match [regexp {^(10|11|12)\.[1-9][0-9]* \(.*} $val]
    }
    lappend res $match

} -result [list 1 1]


#
#
#
test pgtcl-9.4 {dbinfo backend pid} -body {
    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set val [pg::dbinfo backendpid $conn]
    set val2 [$conn backendpid]
    lappend res [string equal $val $val2]
    ::pg::disconnect $conn

    lappend res [regexp {^[0-9]{2,7}$} $val]

} -result [list 1 1]

#
#
#
test pgtcl-9.5 {dbinfo socket} -body {
    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set val [pg::dbinfo socket $conn]
    set val2 [$conn socket]
    lappend res [string equal $val $val2]
    ::pg::disconnect $conn

    lappend res [regexp {^[0-9]{1,6}$} $val]

} -result [list 1 1]

#
# COPY in/out tests
#
test pgtcl-10.1 {copy in} -body {
    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set r [pg_exec $conn {
    CREATE TEMPORARY TABLE test1011 (
	key text primary key,
	val text
    );
    }]
    set ok 1
    if {[pg_result $r -status] ne "PGRES_COMMAND_OK"} {
	set ok 0
	set result "ERROR [pg_result $r -status] [pg_result $r -error]"
    }
    pg_result $r -clear

    if {$ok} {
	set r [pg_exec $conn "COPY test1011 FROM stdin with DELIMITER as '\t';"]
	if {"[pg_result $r -status]" ne "PGRES_COPY_IN"} {
	    set ok 0
	    set result "ERROR [pg_result $r -status] [pg_result $r -error]"
	} else {
	    puts $conn "name	batfink"
	    puts $conn "wings	steel"
	    puts $conn "type	superhero"
	    puts $conn "studio	Hal Seeger Studios"
	    puts $conn "\\."
	}
    }

    if {$ok} {
	set result {}
	pg_select $conn -nodotfields "select key,val from test1011 ORDER BY key;" row {
	    lappend result [array get row]
	}
    }

    ::pg::disconnect $conn

    set result
} -result {{key name val batfink} {key studio val {Hal Seeger Studios}} {key type val superhero} {key wings val steel}}

#
#
#
test pgtcl-10.2 {copy in new API} -body {
    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set r [pg_exec $conn {
    CREATE TEMPORARY TABLE test1011 (
	key text primary key,
	val text
    );
    }]
    set ok 1
    if {[pg_result $r -status] ne "PGRES_COMMAND_OK"} {
	set ok 0
	set result "ERROR [pg_result $r -status] [pg_result $r -error]"
    }
    pg_result $r -clear

    if {$ok} {
	set r [pg_exec $conn "COPY test1011 FROM stdin with DELIMITER as '\t';"]
	if {"[pg_result $r -status]" ne "PGRES_COPY_IN"} {
	    set ok 0
	    set result "ERROR [pg_result $r -status] [pg_result $r -error]"
	} else {
	    puts $conn "name	batfink"
	    puts $conn "wings	steel"
	    puts $conn "type	superhero"
	    puts $conn "studio	Hal Seeger Studios"
	    $conn copy_complete
	}
    }

    if {$ok} {
	set result {}
	pg_select $conn -nodotfields "select key,val from test1011 ORDER BY key;" row {
	    lappend result [array get row]
	}
    }

    ::pg::disconnect $conn

    set result
} -result {{key name val batfink} {key studio val {Hal Seeger Studios}} {key type val superhero} {key wings val steel}}

#
#
#
test pgtcl-10.3 {copy out} -body {
    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set r [pg_exec $conn {
    CREATE TEMPORARY TABLE test1011 (
	key text primary key,
	val text
    );
    INSERT INTO test1011 (key, val) VALUES ('name', 'batfink');
    INSERT INTO test1011 (key, val) VALUES ('wings', 'steel');
    INSERT INTO test1011 (key, val) VALUES ('type', 'superhero');
    INSERT INTO test1011 (key, val) VALUES ('studio', 'Hal Seeger Studios');
    }]
    set ok 1
    if {[pg_result $r -status] ne "PGRES_COMMAND_OK"} {
	set ok 0
	set result "ERROR [pg_result $r -status] [pg_result $r -error]"
    }
    pg_result $r -clear

    if {$ok} {
	set r [pg_exec $conn "COPY test1011 TO stdout with DELIMITER as '\t';"]
	if {"[pg_result $r -status]" eq "PGRES_COPY_OUT"} {
	    set result {}
	    while {[gets $conn line]>= 0} {
		lappend result [split $line "\t"]
	    }
	} else {
	    set result "ERROR [pg_result $r -status] [pg_result $r -error]"
	}
	pg_result $r -clear
	set result [lsort -index 0 $result]
    }
    ::pg::disconnect $conn

    set result
} -result {{name batfink} {studio {Hal Seeger Studios}} {type superhero} {wings steel}}

#
#
#
test pgtcl-11.0 {using pg_exec with 4-byte unicode} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set glyph "𝔄"

    set res [$conn exec {SELECT relname
                           FROM Pg_class
                          WHERE relname LIKE '$glyph'}]

    set results [pg::result $res -list]

    pg_result $res -clear

    pg_disconnect $conn

    lsort $results

} -result [list]

#
#
#
test pgtcl-11.1 {using pg_exec with 4-byte unicode variables} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set glyph "𝔄"

    set res [$conn exec -variables {SELECT relname
                           FROM Pg_class
                          WHERE relname LIKE :glyph}]

    set results [pg::result $res -list]

    pg_result $res -clear

    pg_disconnect $conn

    lsort $results

} -result [list]

#
#
#
test pgtcl-11.2 {using pg_exec with 4-byte unicode array replacement} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set paramarray(glyph) "𝔄"

    set res [$conn exec -paramarray paramarray {SELECT relname
                           FROM Pg_class
                          WHERE relname LIKE `glyph`}]

    set results [pg::result $res -list]

    pg_result $res -clear

    pg_disconnect $conn

    lsort $results

} -result [list]


#
#
#
test pgtcl-11.3 {using pg_exec with 4-byte unicode positional args} -body {

    unset -nocomplain res

    set conn [pg::connect -connlist [array get ::conninfo]]

    set glyph "𝔄"

    set res [$conn exec {SELECT relname
                           FROM Pg_class
                          WHERE relname LIKE $1} $glyph]

    set results [pg::result $res -list]

    pg_result $res -clear

    pg_disconnect $conn

    lsort $results

} -result [list]



puts "tests complete"
