Fossil SCM

fossil-scm / test / th1-query-api-1.th1
Blame History Raw 158 lines
1
This is not a formal test suite, but a tinkering ground.
2
Run it through "fossil test-th-render THIS_FILE".
3
<th1>
4
set stmt [query prepare {SELECT login, cap FROM user}]
5
set colCount [query $stmt col count]
6
puts "query column count: ${colCount}\n"
7
puts "stmt id=${stmt}\n"
8
9
proc noop {} {}
10
proc incr {name {step 1}} {
11
upvar $name x
12
set x [expr $x+$step]
13
}
14
15
16
set sep " "
17
set i 0
18
set colNames(0) 0
19
for {set i 0} {$i < $colCount} {incr i} {
20
set colNames($i) [query $stmt col name $i]
21
puts "colNames($i)=" $colNames($i) "\n"
22
}
23
24
for {set row 0} {[query $stmt step]} {incr row} {
25
for {set i 0} {$i < $colCount} {incr i} {
26
if {$i > 0} {
27
puts $sep
28
} else {
29
puts "#$row: $sep"
30
}
31
puts $colNames($i) = [query $stmt col string $i]
32
}
33
puts "\n"
34
}
35
unset row
36
37
query $stmt finalize
38
#query finalize $stmt
39
40
41
proc query_step_each {{stmt} {callback}} {
42
set colNames(0) 0
43
set colCount [query $stmt col count]
44
for {set i 0} {$i < $colCount} {incr i} {
45
set colNames($i) [query $stmt col name $i]
46
}
47
upvar cb $callback
48
for {set row 0} {[query $stmt step]} {incr row} {
49
#puts "Calling callback: $stmt $colCount colNames\n"
50
$callback $stmt $colCount
51
}
52
}
53
54
set sql {SELECT uid, login FROM user WHERE uid!=?}
55
#set sql {SELECT uid, login FROM user WHERE login=?}
56
#set sql {SELECT tagid, value, null FROM tagxref WHERE value IS ? LIMIT 3}
57
set stmt [query prepare $sql]
58
puts "stmt ID=" $stmt "\n"
59
#query bind int $stmt 1 3
60
#query $stmt bind 1 int 3
61
query $stmt bind 1 string 3
62
#query $stmt bind string 1 3
63
#set stmt [query prepare $sql]
64
#query $stmt bind 1 string 1
65
#set stmt [query prepare $sql]
66
#query $stmt bind null 1
67
puts "USER LIST:\n"
68
catch {
69
proc my_each {stmt colCount} {
70
upvar 2 sep sep
71
puts [query $stmt col int 0] " (type=" [query $stmt col type 0] ")" $sep
72
puts [query $stmt col double 0] $sep
73
puts [query $stmt col string 1] " (type=" [query $stmt col type 1] ")" $sep
74
puts "isnull 0 ?= " [query $stmt col is_null 0] $sep
75
puts "isnull 2 ?= " [query col is_null $stmt 2]
76
# for {set i 0} {$i < $colCount} {incr i} {
77
# if {$i > 0} { puts $sep }
78
# }
79
puts "\n"
80
# error "hi!"
81
}
82
query_step_each $stmt my_each
83
# query reset $stmt
84
# query $stmt reset
85
# query_step_each $stmt {
86
# proc each {stmt cc} { puts hi "\n" }
87
# }
88
return 0
89
} rc
90
query finalize $stmt
91
if { 0 != $rc } {
92
puts "ERROR: $rc\n"
93
}
94
95
set consts [list SQLITE_BLOB SQLITE_FLOAT SQLITE_INTEGER SQLITE_NULL SQLITE_TEXT]
96
#set consts $SQLITE_CONSTANTS
97
puts consts = $consts "\n"
98
for {set i 0} {$i < [llength $consts]} {incr i} {
99
set x [lindex $consts $i]
100
puts \$$x = [expr \$$x] "\n"
101
}
102
103
set ARGC [argv len]
104
puts ARGC = $ARGC "\n"
105
for {set i 0} {$i < $ARGC} {incr i} {
106
puts "argv at $i = " [argv at $i] \n
107
}
108
109
set magicDefault hi
110
set optA [argv string AA a $magicDefault]
111
puts "argv string AA = " $optA \n
112
113
set optA [argv bool BB b 0]
114
puts "argv bool BB = " $optA \n
115
116
set exception 0
117
catch {
118
argv int noSuchOptionAndNoDefault
119
} exception
120
puts exception = $exception "\n"
121
122
proc multiStmt {} {
123
set max 5
124
set i 0
125
set s(0) 0
126
for {set i 0} {$i < $max} {incr i} {
127
set s($i) [query prepare "SELECT $i"]
128
puts "s($i) = $s($i)\n"
129
}
130
for {set i 0} {$i < $max} {incr i} {
131
query $s($i) step
132
}
133
for {set i 0} {$i < $max} {incr i} {
134
puts "closing stmt $s($i)\n"
135
query $s($i) finalize
136
}
137
138
puts "Preparing again\n"
139
140
for {set i 0} {$i < $max} {incr i} {
141
set s($i) [query prepare "SELECT $i"]
142
puts "s($i) = $s($i)\n"
143
}
144
for {set i 0} {$i < $max} {incr i} {
145
query $s($i) step
146
}
147
puts "Closing again\n"
148
149
for {set i 0} {$i < $max} {incr i} {
150
puts "closing stmt $s($i)\n"
151
query $s($i) finalize
152
}
153
}
154
multiStmt
155
156
puts "If you got this far, you win!\n"
157
</th1>
158

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button