Fossil SCM
Cleanups in the th1 query statement finalization.
Commit
4fdf0ac279a979f3313945c63d950749cb40d9ab
Parent
008a16c0e598062…
3 files changed
+10
-2
+25
+35
M
src/th.c
+10
-2
| --- src/th.c | ||
| +++ src/th.c | ||
| @@ -1658,12 +1658,19 @@ | ||
| 1658 | 1658 | Th_HashDelete(interp, interp->paCmd); |
| 1659 | 1659 | |
| 1660 | 1660 | #ifdef TH_USE_SQLITE |
| 1661 | 1661 | { |
| 1662 | 1662 | int i; |
| 1663 | + sqlite3_stmt * st; | |
| 1663 | 1664 | 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 | + } | |
| 1665 | 1672 | } |
| 1666 | 1673 | Th_Free(interp, interp->stmt.aStmt); |
| 1667 | 1674 | } |
| 1668 | 1675 | #endif |
| 1669 | 1676 | |
| @@ -2659,12 +2666,13 @@ | ||
| 2659 | 2666 | st = interp->stmt.aStmt[stmtId-1]; |
| 2660 | 2667 | if(NULL != st){ |
| 2661 | 2668 | interp->stmt.aStmt[stmtId-1] = NULL; |
| 2662 | 2669 | sqlite3_finalize(st); |
| 2663 | 2670 | return 0; |
| 2671 | + }else{ | |
| 2672 | + return 1; | |
| 2664 | 2673 | } |
| 2665 | - return 1; | |
| 2666 | 2674 | } |
| 2667 | 2675 | |
| 2668 | 2676 | sqlite3_stmt * Th_GetStmt(Th_Interp *interp, int stmtId){ |
| 2669 | 2677 | return ((stmtId<1) || (stmtId > interp->stmt.nStmt)) |
| 2670 | 2678 | ? NULL |
| 2671 | 2679 |
| --- 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 |
M
src/th.h
+25
| --- src/th.h | ||
| +++ src/th.h | ||
| @@ -193,10 +193,35 @@ | ||
| 193 | 193 | int Th_CallSubCommand(Th_Interp*,void*,int,const char**,int*,Th_SubCommand*); |
| 194 | 194 | |
| 195 | 195 | #ifdef TH_USE_SQLITE |
| 196 | 196 | #include "stddef.h" /* size_t */ |
| 197 | 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 | +*/ | |
| 198 | 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 | +*/ | |
| 199 | 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 | +*/ | |
| 200 | 225 | sqlite3_stmt * Th_GetStmt(Th_Interp *interp, int stmtId); |
| 201 | 226 | #endif |
| 202 | 227 | |
| 203 | 228 |
| --- 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 |
+35
| --- test/th1-query-api-1.th1 | ||
| +++ test/th1-query-api-1.th1 | ||
| @@ -132,7 +132,42 @@ | ||
| 132 | 132 | catch { |
| 133 | 133 | argv_getint noSuchOptionAndNoDefault |
| 134 | 134 | } exception |
| 135 | 135 | puts exception = $exception "\n" |
| 136 | 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 | + | |
| 137 | 172 | puts "If you got this far, you win!\n" |
| 138 | 173 | </th1> |
| 139 | 174 |
| --- 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 |