2011-03-21 19:17:42 +03:00
|
|
|
|
2015-06-02 12:19:22 +03:00
|
|
|
proc do_changeset_test {tn session res} {
|
|
|
|
set r [list]
|
|
|
|
foreach x $res {lappend r $x}
|
|
|
|
uplevel do_test $tn [list [subst -nocommands {
|
|
|
|
set x [list]
|
|
|
|
sqlite3session_foreach c [$session changeset] { lappend x [set c] }
|
|
|
|
set x
|
|
|
|
}]] [list $r]
|
|
|
|
}
|
|
|
|
|
2015-10-01 19:35:57 +03:00
|
|
|
proc do_patchset_test {tn session res} {
|
|
|
|
set r [list]
|
|
|
|
foreach x $res {lappend r $x}
|
|
|
|
uplevel do_test $tn [list [subst -nocommands {
|
|
|
|
set x [list]
|
|
|
|
sqlite3session_foreach c [$session patchset] { lappend x [set c] }
|
|
|
|
set x
|
|
|
|
}]] [list $r]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
proc do_changeset_invert_test {tn session res} {
|
|
|
|
set r [list]
|
|
|
|
foreach x $res {lappend r $x}
|
|
|
|
uplevel do_test $tn [list [subst -nocommands {
|
|
|
|
set x [list]
|
|
|
|
set changeset [sqlite3changeset_invert [$session changeset]]
|
|
|
|
sqlite3session_foreach c [set changeset] { lappend x [set c] }
|
|
|
|
set x
|
|
|
|
}]] [list $r]
|
|
|
|
}
|
|
|
|
|
2011-03-21 19:17:42 +03:00
|
|
|
|
|
|
|
proc do_conflict_test {tn args} {
|
|
|
|
|
|
|
|
set O(-tables) [list]
|
|
|
|
set O(-sql) [list]
|
|
|
|
set O(-conflicts) [list]
|
2017-02-04 20:33:30 +03:00
|
|
|
set O(-policy) "OMIT"
|
2011-03-21 19:17:42 +03:00
|
|
|
|
|
|
|
array set V $args
|
|
|
|
foreach key [array names V] {
|
|
|
|
if {![info exists O($key)]} {error "no such option: $key"}
|
|
|
|
}
|
|
|
|
array set O $args
|
|
|
|
|
2017-02-04 20:33:30 +03:00
|
|
|
proc xConflict {args} [subst -nocommands {
|
|
|
|
lappend ::xConflict [set args]
|
|
|
|
return $O(-policy)
|
|
|
|
}]
|
|
|
|
proc bgerror {args} { set ::background_error $args }
|
|
|
|
|
2011-03-21 19:17:42 +03:00
|
|
|
sqlite3session S db main
|
|
|
|
foreach t $O(-tables) { S attach $t }
|
|
|
|
execsql $O(-sql)
|
|
|
|
|
|
|
|
set ::xConflict [list]
|
|
|
|
sqlite3changeset_apply db2 [S changeset] xConflict
|
|
|
|
|
|
|
|
set conflicts [list]
|
|
|
|
foreach c $O(-conflicts) {
|
|
|
|
lappend conflicts $c
|
|
|
|
}
|
|
|
|
|
|
|
|
after 1 {set go 1}
|
|
|
|
vwait go
|
|
|
|
|
|
|
|
uplevel do_test $tn [list { set ::xConflict }] [list $conflicts]
|
|
|
|
S delete
|
|
|
|
}
|
|
|
|
|
|
|
|
proc do_common_sql {sql} {
|
|
|
|
execsql $sql db
|
|
|
|
execsql $sql db2
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:08:00 +03:00
|
|
|
proc changeset_from_sql {sql {dbname main}} {
|
2016-08-29 17:18:18 +03:00
|
|
|
if {$dbname == "main"} {
|
|
|
|
return [sql_exec_changeset db $sql]
|
|
|
|
}
|
2011-03-22 15:08:00 +03:00
|
|
|
set rc [catch {
|
|
|
|
sqlite3session S db $dbname
|
|
|
|
db eval "SELECT name FROM $dbname.sqlite_master WHERE type = 'table'" {
|
|
|
|
S attach $name
|
|
|
|
}
|
|
|
|
db eval $sql
|
|
|
|
S changeset
|
|
|
|
} changeset]
|
|
|
|
catch { S delete }
|
|
|
|
|
|
|
|
if {$rc} {
|
|
|
|
error $changeset
|
|
|
|
}
|
|
|
|
return $changeset
|
|
|
|
}
|
|
|
|
|
2011-03-21 19:17:42 +03:00
|
|
|
proc do_then_apply_sql {sql {dbname main}} {
|
|
|
|
proc xConflict args { return "OMIT" }
|
|
|
|
set rc [catch {
|
|
|
|
sqlite3session S db $dbname
|
|
|
|
db eval "SELECT name FROM $dbname.sqlite_master WHERE type = 'table'" {
|
|
|
|
S attach $name
|
|
|
|
}
|
|
|
|
db eval $sql
|
|
|
|
sqlite3changeset_apply db2 [S changeset] xConflict
|
|
|
|
} msg]
|
|
|
|
|
|
|
|
catch { S delete }
|
|
|
|
|
|
|
|
if {$rc} {error $msg}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc do_iterator_test {tn tbl_list sql res} {
|
|
|
|
sqlite3session S db main
|
2011-03-22 18:21:03 +03:00
|
|
|
if {[llength $tbl_list]==0} { S attach * }
|
2011-03-21 19:17:42 +03:00
|
|
|
foreach t $tbl_list {S attach $t}
|
2011-03-22 18:21:03 +03:00
|
|
|
|
2011-03-21 19:17:42 +03:00
|
|
|
execsql $sql
|
|
|
|
|
|
|
|
set r [list]
|
|
|
|
foreach v $res { lappend r $v }
|
|
|
|
|
|
|
|
set x [list]
|
|
|
|
sqlite3session_foreach c [S changeset] { lappend x $c }
|
|
|
|
uplevel do_test $tn [list [list set {} $x]] [list $r]
|
|
|
|
|
|
|
|
S delete
|
|
|
|
}
|
|
|
|
|
|
|
|
# Compare the contents of all tables in [db1] and [db2]. Throw an error if
|
|
|
|
# they are not identical, or return an empty string if they are.
|
|
|
|
#
|
|
|
|
proc compare_db {db1 db2} {
|
|
|
|
|
|
|
|
set sql {SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name}
|
|
|
|
set lot1 [$db1 eval $sql]
|
|
|
|
set lot2 [$db2 eval $sql]
|
|
|
|
|
2011-03-21 22:41:29 +03:00
|
|
|
if {$lot1 != $lot2} {
|
|
|
|
puts $lot1
|
|
|
|
puts $lot2
|
|
|
|
error "databases contain different tables"
|
|
|
|
}
|
2011-03-21 19:17:42 +03:00
|
|
|
|
|
|
|
foreach tbl $lot1 {
|
|
|
|
set col1 [list]
|
|
|
|
set col2 [list]
|
|
|
|
|
|
|
|
$db1 eval "PRAGMA table_info = $tbl" { lappend col1 $name }
|
|
|
|
$db2 eval "PRAGMA table_info = $tbl" { lappend col2 $name }
|
|
|
|
if {$col1 != $col2} { error "table $tbl schema mismatch" }
|
|
|
|
|
|
|
|
set sql "SELECT * FROM $tbl ORDER BY [join $col1 ,]"
|
|
|
|
set data1 [$db1 eval $sql]
|
|
|
|
set data2 [$db2 eval $sql]
|
2011-03-22 15:08:00 +03:00
|
|
|
if {$data1 != $data2} {
|
|
|
|
puts "$data1"
|
|
|
|
puts "$data2"
|
|
|
|
error "table $tbl data mismatch"
|
|
|
|
}
|
2011-03-21 19:17:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2011-04-15 20:03:32 +04:00
|
|
|
proc changeset_to_list {c} {
|
|
|
|
set list [list]
|
|
|
|
sqlite3session_foreach elem $c { lappend list $elem }
|
|
|
|
lsort $list
|
|
|
|
}
|
2018-03-20 23:27:03 +03:00
|
|
|
|
2018-04-06 19:22:25 +03:00
|
|
|
set ones {zero one two three four five six seven eight nine
|
|
|
|
ten eleven twelve thirteen fourteen fifteen sixteen seventeen
|
|
|
|
eighteen nineteen}
|
|
|
|
set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
|
|
|
|
proc number_name {n} {
|
|
|
|
if {$n>=1000} {
|
|
|
|
set txt "[number_name [expr {$n/1000}]] thousand"
|
|
|
|
set n [expr {$n%1000}]
|
|
|
|
} else {
|
|
|
|
set txt {}
|
|
|
|
}
|
|
|
|
if {$n>=100} {
|
|
|
|
append txt " [lindex $::ones [expr {$n/100}]] hundred"
|
|
|
|
set n [expr {$n%100}]
|
|
|
|
}
|
|
|
|
if {$n>=20} {
|
|
|
|
append txt " [lindex $::tens [expr {$n/10}]]"
|
|
|
|
set n [expr {$n%10}]
|
|
|
|
}
|
|
|
|
if {$n>0} {
|
|
|
|
append txt " [lindex $::ones $n]"
|
|
|
|
}
|
|
|
|
set txt [string trim $txt]
|
|
|
|
if {$txt==""} {set txt zero}
|
|
|
|
return $txt
|
|
|
|
}
|