Fossil SCM

Cleanups in the th1 query statement finalization.

stephan 2012-07-14 11:15 th1-query-api
Commit 4fdf0ac279a979f3313945c63d950749cb40d9ab
+10 -2
--- src/th.c
+++ src/th.c
@@ -1658,12 +1658,19 @@
16581658
Th_HashDelete(interp, interp->paCmd);
16591659
16601660
#ifdef TH_USE_SQLITE
16611661
{
16621662
int i;
1663
+ sqlite3_stmt * st;
16631664
for( i = 0; i < interp->stmt.nStmt; ++i ){
1664
- Th_FinalizeStmt( interp, i );
1665
+ st = interp->stmt.aStmt[i];
1666
+ if(NULL != st){
1667
+ fossil_warning("Auto-finalizing unfinalized query_prepare "
1668
+ "statement id #%d: %s",
1669
+ i+1, sqlite3_sql(st));
1670
+ Th_FinalizeStmt( interp, i+1 );
1671
+ }
16651672
}
16661673
Th_Free(interp, interp->stmt.aStmt);
16671674
}
16681675
#endif
16691676
@@ -2659,12 +2666,13 @@
26592666
st = interp->stmt.aStmt[stmtId-1];
26602667
if(NULL != st){
26612668
interp->stmt.aStmt[stmtId-1] = NULL;
26622669
sqlite3_finalize(st);
26632670
return 0;
2671
+ }else{
2672
+ return 1;
26642673
}
2665
- return 1;
26662674
}
26672675
26682676
sqlite3_stmt * Th_GetStmt(Th_Interp *interp, int stmtId){
26692677
return ((stmtId<1) || (stmtId > interp->stmt.nStmt))
26702678
? NULL
26712679
--- src/th.c
+++ src/th.c
@@ -1658,12 +1658,19 @@
1658 Th_HashDelete(interp, interp->paCmd);
1659
1660 #ifdef TH_USE_SQLITE
1661 {
1662 int i;
 
1663 for( i = 0; i < interp->stmt.nStmt; ++i ){
1664 Th_FinalizeStmt( interp, i );
 
 
 
 
 
 
1665 }
1666 Th_Free(interp, interp->stmt.aStmt);
1667 }
1668 #endif
1669
@@ -2659,12 +2666,13 @@
2659 st = interp->stmt.aStmt[stmtId-1];
2660 if(NULL != st){
2661 interp->stmt.aStmt[stmtId-1] = NULL;
2662 sqlite3_finalize(st);
2663 return 0;
 
 
2664 }
2665 return 1;
2666 }
2667
2668 sqlite3_stmt * Th_GetStmt(Th_Interp *interp, int stmtId){
2669 return ((stmtId<1) || (stmtId > interp->stmt.nStmt))
2670 ? NULL
2671
--- src/th.c
+++ src/th.c
@@ -1658,12 +1658,19 @@
1658 Th_HashDelete(interp, interp->paCmd);
1659
1660 #ifdef TH_USE_SQLITE
1661 {
1662 int i;
1663 sqlite3_stmt * st;
1664 for( i = 0; i < interp->stmt.nStmt; ++i ){
1665 st = interp->stmt.aStmt[i];
1666 if(NULL != st){
1667 fossil_warning("Auto-finalizing unfinalized query_prepare "
1668 "statement id #%d: %s",
1669 i+1, sqlite3_sql(st));
1670 Th_FinalizeStmt( interp, i+1 );
1671 }
1672 }
1673 Th_Free(interp, interp->stmt.aStmt);
1674 }
1675 #endif
1676
@@ -2659,12 +2666,13 @@
2666 st = interp->stmt.aStmt[stmtId-1];
2667 if(NULL != st){
2668 interp->stmt.aStmt[stmtId-1] = NULL;
2669 sqlite3_finalize(st);
2670 return 0;
2671 }else{
2672 return 1;
2673 }
 
2674 }
2675
2676 sqlite3_stmt * Th_GetStmt(Th_Interp *interp, int stmtId){
2677 return ((stmtId<1) || (stmtId > interp->stmt.nStmt))
2678 ? NULL
2679
+25
--- src/th.h
+++ src/th.h
@@ -193,10 +193,35 @@
193193
int Th_CallSubCommand(Th_Interp*,void*,int,const char**,int*,Th_SubCommand*);
194194
195195
#ifdef TH_USE_SQLITE
196196
#include "stddef.h" /* size_t */
197197
extern void *fossil_realloc(void *p, size_t n);
198
+
199
+/*
200
+** Adds the given prepared statement to the interpreter. Returns the
201
+** statements opaque identifier (a positive value). Ownerships of
202
+** pStmt is transfered to interp and it must be cleaned up by the
203
+** client by calling Th_FinalizeStmt(), passing it the value returned
204
+** by this function.
205
+**
206
+** If interp is destroyed before all statements are finalized,
207
+** it will finalize them but may emit a warning message.
208
+*/
198209
int Th_AddStmt(Th_Interp *interp, sqlite3_stmt * pStmt);
210
+
211
+/*
212
+** Expects stmtId to be a statement identifier returned by
213
+** Th_AddStmt(). On success, finalizes the statement and returns 0.
214
+** On error (statement not found) non-0 is returned. After this
215
+** call, some subsequent call to Th_AddStmt() may return the
216
+** same statement ID.
217
+*/
199218
int Th_FinalizeStmt(Th_Interp *interp, int stmtId);
219
+
220
+/*
221
+** Fetches the statement with the given ID, as returned by
222
+** Th_AddStmt(). Returns NULL if stmtId does not refer (or no longer
223
+** refers) to a statement added via Th_AddStmt().
224
+*/
200225
sqlite3_stmt * Th_GetStmt(Th_Interp *interp, int stmtId);
201226
#endif
202227
203228
--- src/th.h
+++ src/th.h
@@ -193,10 +193,35 @@
193 int Th_CallSubCommand(Th_Interp*,void*,int,const char**,int*,Th_SubCommand*);
194
195 #ifdef TH_USE_SQLITE
196 #include "stddef.h" /* size_t */
197 extern void *fossil_realloc(void *p, size_t n);
 
 
 
 
 
 
 
 
 
 
 
198 int Th_AddStmt(Th_Interp *interp, sqlite3_stmt * pStmt);
 
 
 
 
 
 
 
 
199 int Th_FinalizeStmt(Th_Interp *interp, int stmtId);
 
 
 
 
 
 
200 sqlite3_stmt * Th_GetStmt(Th_Interp *interp, int stmtId);
201 #endif
202
203
--- src/th.h
+++ src/th.h
@@ -193,10 +193,35 @@
193 int Th_CallSubCommand(Th_Interp*,void*,int,const char**,int*,Th_SubCommand*);
194
195 #ifdef TH_USE_SQLITE
196 #include "stddef.h" /* size_t */
197 extern void *fossil_realloc(void *p, size_t n);
198
199 /*
200 ** Adds the given prepared statement to the interpreter. Returns the
201 ** statements opaque identifier (a positive value). Ownerships of
202 ** pStmt is transfered to interp and it must be cleaned up by the
203 ** client by calling Th_FinalizeStmt(), passing it the value returned
204 ** by this function.
205 **
206 ** If interp is destroyed before all statements are finalized,
207 ** it will finalize them but may emit a warning message.
208 */
209 int Th_AddStmt(Th_Interp *interp, sqlite3_stmt * pStmt);
210
211 /*
212 ** Expects stmtId to be a statement identifier returned by
213 ** Th_AddStmt(). On success, finalizes the statement and returns 0.
214 ** On error (statement not found) non-0 is returned. After this
215 ** call, some subsequent call to Th_AddStmt() may return the
216 ** same statement ID.
217 */
218 int Th_FinalizeStmt(Th_Interp *interp, int stmtId);
219
220 /*
221 ** Fetches the statement with the given ID, as returned by
222 ** Th_AddStmt(). Returns NULL if stmtId does not refer (or no longer
223 ** refers) to a statement added via Th_AddStmt().
224 */
225 sqlite3_stmt * Th_GetStmt(Th_Interp *interp, int stmtId);
226 #endif
227
228
--- test/th1-query-api-1.th1
+++ test/th1-query-api-1.th1
@@ -132,7 +132,42 @@
132132
catch {
133133
argv_getint noSuchOptionAndNoDefault
134134
} exception
135135
puts exception = $exception "\n"
136136
137
+
138
+proc multiStmt {} {
139
+ set max 5
140
+ set i 0
141
+ set s(0) 0
142
+ for {set i 0} {$i < $max} {incr i} {
143
+ set s($i) [query_prepare "SELECT $i"]
144
+ puts "s($i) = $s($i)\n"
145
+ }
146
+ for {set i 0} {$i < $max} {incr i} {
147
+ query_step $s($i)
148
+ }
149
+ for {set i 0} {$i < $max} {incr i} {
150
+ puts "closing stmt $s($i)\n"
151
+ query_finalize $s($i)
152
+ }
153
+
154
+ puts "Preparing again\n"
155
+
156
+ for {set i 0} {$i < $max} {incr i} {
157
+ set s($i) [query_prepare "SELECT $i"]
158
+ puts "s($i) = $s($i)\n"
159
+ }
160
+ for {set i 0} {$i < $max} {incr i} {
161
+ query_step $s($i)
162
+ }
163
+ puts "Closing again\n"
164
+
165
+ for {set i 0} {$i < $max} {incr i} {
166
+ puts "closing stmt $s($i)\n"
167
+ query_finalize $s($i)
168
+ }
169
+}
170
+multiStmt
171
+
137172
puts "If you got this far, you win!\n"
138173
</th1>
139174
--- test/th1-query-api-1.th1
+++ test/th1-query-api-1.th1
@@ -132,7 +132,42 @@
132 catch {
133 argv_getint noSuchOptionAndNoDefault
134 } exception
135 puts exception = $exception "\n"
136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137 puts "If you got this far, you win!\n"
138 </th1>
139
--- test/th1-query-api-1.th1
+++ test/th1-query-api-1.th1
@@ -132,7 +132,42 @@
132 catch {
133 argv_getint noSuchOptionAndNoDefault
134 } exception
135 puts exception = $exception "\n"
136
137
138 proc multiStmt {} {
139 set max 5
140 set i 0
141 set s(0) 0
142 for {set i 0} {$i < $max} {incr i} {
143 set s($i) [query_prepare "SELECT $i"]
144 puts "s($i) = $s($i)\n"
145 }
146 for {set i 0} {$i < $max} {incr i} {
147 query_step $s($i)
148 }
149 for {set i 0} {$i < $max} {incr i} {
150 puts "closing stmt $s($i)\n"
151 query_finalize $s($i)
152 }
153
154 puts "Preparing again\n"
155
156 for {set i 0} {$i < $max} {incr i} {
157 set s($i) [query_prepare "SELECT $i"]
158 puts "s($i) = $s($i)\n"
159 }
160 for {set i 0} {$i < $max} {incr i} {
161 query_step $s($i)
162 }
163 puts "Closing again\n"
164
165 for {set i 0} {$i < $max} {incr i} {
166 puts "closing stmt $s($i)\n"
167 query_finalize $s($i)
168 }
169 }
170 multiStmt
171
172 puts "If you got this far, you win!\n"
173 </th1>
174

Keyboard Shortcuts

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