Fossil SCM
Collect the various file-scope variables used by the "db.c" module into a single structure. This is code cleanup. There are no functional changes.
Commit
a537c995313243b3fd46b32dd61988fd57ae09df
Parent
0a72346109624fe…
1 file changed
+61
-56
M
src/db.c
+61
-56
| --- src/db.c | ||
| +++ src/db.c | ||
| @@ -99,29 +99,34 @@ | ||
| 99 | 99 | free(z); |
| 100 | 100 | db_force_rollback(); |
| 101 | 101 | fossil_exit(rc); |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | -static int nBegin = 0; /* Nesting depth of BEGIN */ | |
| 105 | -static int doRollback = 0; /* True to force a rollback */ | |
| 106 | -static int nCommitHook = 0; /* Number of commit hooks */ | |
| 107 | -static struct sCommitHook { | |
| 108 | - int (*xHook)(void); /* Functions to call at db_end_transaction() */ | |
| 109 | - int sequence; /* Call functions in sequence order */ | |
| 110 | -} aHook[5]; | |
| 111 | -static Stmt *pAllStmt = 0; /* List of all unfinalized statements */ | |
| 112 | -static int nPrepare = 0; /* Number of calls to sqlite3_prepare() */ | |
| 113 | -static int nDeleteOnFail = 0; /* Number of entries in azDeleteOnFail[] */ | |
| 114 | -static char *azDeleteOnFail[3]; /* Files to delete on a failure */ | |
| 115 | - | |
| 104 | +/* | |
| 105 | +** All static variable that a used by only this file are gathered into | |
| 106 | +** the following structure. | |
| 107 | +*/ | |
| 108 | +static struct DbLocalData { | |
| 109 | + int nBegin; /* Nesting depth of BEGIN */ | |
| 110 | + int doRollback; /* True to force a rollback */ | |
| 111 | + int nCommitHook; /* Number of commit hooks */ | |
| 112 | + Stmt *pAllStmt; /* List of all unfinalized statements */ | |
| 113 | + int nPrepare; /* Number of calls to sqlite3_prepare() */ | |
| 114 | + int nDeleteOnFail; /* Number of entries in azDeleteOnFail[] */ | |
| 115 | + struct sCommitHook { | |
| 116 | + int (*xHook)(void); /* Functions to call at db_end_transaction() */ | |
| 117 | + int sequence; /* Call functions in sequence order */ | |
| 118 | + } aHook[5]; | |
| 119 | + char *azDeleteOnFail[3]; /* Files to delete on a failure */ | |
| 120 | +} db = {0, 0, 0, 0, 0, 0, }; | |
| 116 | 121 | |
| 117 | 122 | /* |
| 118 | 123 | ** Arrange for the given file to be deleted on a failure. |
| 119 | 124 | */ |
| 120 | 125 | void db_delete_on_failure(const char *zFilename){ |
| 121 | - assert( nDeleteOnFail<count(azDeleteOnFail) ); | |
| 122 | - azDeleteOnFail[nDeleteOnFail++] = fossil_strdup(zFilename); | |
| 126 | + assert( db.nDeleteOnFail<count(db.azDeleteOnFail) ); | |
| 127 | + db.azDeleteOnFail[db.nDeleteOnFail++] = fossil_strdup(zFilename); | |
| 123 | 128 | } |
| 124 | 129 | |
| 125 | 130 | /* |
| 126 | 131 | ** This routine is called by the SQLite commit-hook mechanism |
| 127 | 132 | ** just prior to each commit. All this routine does is verify |
| @@ -130,11 +135,11 @@ | ||
| 130 | 135 | ** below. |
| 131 | 136 | ** |
| 132 | 137 | ** This is just a safety and sanity check. |
| 133 | 138 | */ |
| 134 | 139 | static int db_verify_at_commit(void *notUsed){ |
| 135 | - if( nBegin ){ | |
| 140 | + if( db.nBegin ){ | |
| 136 | 141 | fossil_panic("illegal commit attempt"); |
| 137 | 142 | return 1; |
| 138 | 143 | } |
| 139 | 144 | return 0; |
| 140 | 145 | } |
| @@ -141,32 +146,32 @@ | ||
| 141 | 146 | |
| 142 | 147 | /* |
| 143 | 148 | ** Begin and end a nested transaction |
| 144 | 149 | */ |
| 145 | 150 | void db_begin_transaction(void){ |
| 146 | - if( nBegin==0 ){ | |
| 151 | + if( db.nBegin==0 ){ | |
| 147 | 152 | db_multi_exec("BEGIN"); |
| 148 | 153 | sqlite3_commit_hook(g.db, db_verify_at_commit, 0); |
| 149 | 154 | } |
| 150 | - nBegin++; | |
| 155 | + db.nBegin++; | |
| 151 | 156 | } |
| 152 | 157 | void db_end_transaction(int rollbackFlag){ |
| 153 | 158 | if( g.db==0 ) return; |
| 154 | - if( nBegin<=0 ) return; | |
| 155 | - if( rollbackFlag ) doRollback = 1; | |
| 156 | - nBegin--; | |
| 157 | - if( nBegin==0 ){ | |
| 159 | + if( db.nBegin<=0 ) return; | |
| 160 | + if( rollbackFlag ) db.doRollback = 1; | |
| 161 | + db.nBegin--; | |
| 162 | + if( db.nBegin==0 ){ | |
| 158 | 163 | int i; |
| 159 | - if( doRollback==0 ) leaf_do_pending_checks(); | |
| 160 | - for(i=0; doRollback==0 && i<nCommitHook; i++){ | |
| 161 | - doRollback |= aHook[i].xHook(); | |
| 162 | - } | |
| 163 | - while( pAllStmt ){ | |
| 164 | - db_finalize(pAllStmt); | |
| 165 | - } | |
| 166 | - db_multi_exec(doRollback ? "ROLLBACK" : "COMMIT"); | |
| 167 | - doRollback = 0; | |
| 164 | + if( db.doRollback==0 ) leaf_do_pending_checks(); | |
| 165 | + for(i=0; db.doRollback==0 && i<db.nCommitHook; i++){ | |
| 166 | + db.doRollback |= db.aHook[i].xHook(); | |
| 167 | + } | |
| 168 | + while( db.pAllStmt ){ | |
| 169 | + db_finalize(db.pAllStmt); | |
| 170 | + } | |
| 171 | + db_multi_exec(db.doRollback ? "ROLLBACK" : "COMMIT"); | |
| 172 | + db.doRollback = 0; | |
| 168 | 173 | } |
| 169 | 174 | } |
| 170 | 175 | |
| 171 | 176 | /* |
| 172 | 177 | ** Force a rollback and shutdown the database |
| @@ -179,21 +184,21 @@ | ||
| 179 | 184 | busy = 1; |
| 180 | 185 | undo_rollback(); |
| 181 | 186 | while( (pStmt = sqlite3_next_stmt(g.db,pStmt))!=0 ){ |
| 182 | 187 | sqlite3_reset(pStmt); |
| 183 | 188 | } |
| 184 | - while( pAllStmt ){ | |
| 185 | - db_finalize(pAllStmt); | |
| 189 | + while( db.pAllStmt ){ | |
| 190 | + db_finalize(db.pAllStmt); | |
| 186 | 191 | } |
| 187 | - if( nBegin ){ | |
| 192 | + if( db.nBegin ){ | |
| 188 | 193 | sqlite3_exec(g.db, "ROLLBACK", 0, 0, 0); |
| 189 | - nBegin = 0; | |
| 194 | + db.nBegin = 0; | |
| 190 | 195 | } |
| 191 | 196 | busy = 0; |
| 192 | 197 | db_close(0); |
| 193 | - for(i=0; i<nDeleteOnFail; i++){ | |
| 194 | - file_delete(azDeleteOnFail[i]); | |
| 198 | + for(i=0; i<db.nDeleteOnFail; i++){ | |
| 199 | + file_delete(db.azDeleteOnFail[i]); | |
| 195 | 200 | } |
| 196 | 201 | } |
| 197 | 202 | |
| 198 | 203 | /* |
| 199 | 204 | ** Install a commit hook. Hooks are installed in sequence order. |
| @@ -205,25 +210,25 @@ | ||
| 205 | 210 | ** rolls back rather than commit. It is the responsibility of the |
| 206 | 211 | ** hooks themselves to issue any error messages. |
| 207 | 212 | */ |
| 208 | 213 | void db_commit_hook(int (*x)(void), int sequence){ |
| 209 | 214 | int i; |
| 210 | - assert( nCommitHook < sizeof(aHook)/sizeof(aHook[1]) ); | |
| 211 | - for(i=0; i<nCommitHook; i++){ | |
| 212 | - assert( x!=aHook[i].xHook ); | |
| 213 | - if( aHook[i].sequence>sequence ){ | |
| 215 | + assert( db.nCommitHook < sizeof(db.aHook)/sizeof(db.aHook[1]) ); | |
| 216 | + for(i=0; i<db.nCommitHook; i++){ | |
| 217 | + assert( x!=db.aHook[i].xHook ); | |
| 218 | + if( db.aHook[i].sequence>sequence ){ | |
| 214 | 219 | int s = sequence; |
| 215 | 220 | int (*xS)(void) = x; |
| 216 | - sequence = aHook[i].sequence; | |
| 217 | - x = aHook[i].xHook; | |
| 218 | - aHook[i].sequence = s; | |
| 219 | - aHook[i].xHook = xS; | |
| 221 | + sequence = db.aHook[i].sequence; | |
| 222 | + x = db.aHook[i].xHook; | |
| 223 | + db.aHook[i].sequence = s; | |
| 224 | + db.aHook[i].xHook = xS; | |
| 220 | 225 | } |
| 221 | 226 | } |
| 222 | - aHook[nCommitHook].sequence = sequence; | |
| 223 | - aHook[nCommitHook].xHook = x; | |
| 224 | - nCommitHook++; | |
| 227 | + db.aHook[db.nCommitHook].sequence = sequence; | |
| 228 | + db.aHook[db.nCommitHook].xHook = x; | |
| 229 | + db.nCommitHook++; | |
| 225 | 230 | } |
| 226 | 231 | |
| 227 | 232 | /* |
| 228 | 233 | ** Prepare a Stmt. Assume that the Stmt is previously uninitialized. |
| 229 | 234 | ** If the input string contains multiple SQL statements, only the first |
| @@ -234,11 +239,11 @@ | ||
| 234 | 239 | char *zSql; |
| 235 | 240 | blob_zero(&pStmt->sql); |
| 236 | 241 | blob_vappendf(&pStmt->sql, zFormat, ap); |
| 237 | 242 | va_end(ap); |
| 238 | 243 | zSql = blob_str(&pStmt->sql); |
| 239 | - nPrepare++; | |
| 244 | + db.nPrepare++; | |
| 240 | 245 | rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt->pStmt, 0); |
| 241 | 246 | if( rc!=0 && !errOk ){ |
| 242 | 247 | db_err("%s\n%s", sqlite3_errmsg(g.db), zSql); |
| 243 | 248 | } |
| 244 | 249 | pStmt->pNext = pStmt->pPrev = 0; |
| @@ -265,14 +270,14 @@ | ||
| 265 | 270 | int rc = SQLITE_OK; |
| 266 | 271 | if( blob_size(&pStmt->sql)==0 ){ |
| 267 | 272 | va_list ap; |
| 268 | 273 | va_start(ap, zFormat); |
| 269 | 274 | rc = db_vprepare(pStmt, 0, zFormat, ap); |
| 270 | - pStmt->pNext = pAllStmt; | |
| 275 | + pStmt->pNext = db.pAllStmt; | |
| 271 | 276 | pStmt->pPrev = 0; |
| 272 | - if( pAllStmt ) pAllStmt->pPrev = pStmt; | |
| 273 | - pAllStmt = pStmt; | |
| 277 | + if( db.pAllStmt ) db.pAllStmt->pPrev = pStmt; | |
| 278 | + db.pAllStmt = pStmt; | |
| 274 | 279 | va_end(ap); |
| 275 | 280 | } |
| 276 | 281 | return rc; |
| 277 | 282 | } |
| 278 | 283 | |
| @@ -372,12 +377,12 @@ | ||
| 372 | 377 | if( pStmt->pNext ){ |
| 373 | 378 | pStmt->pNext->pPrev = pStmt->pPrev; |
| 374 | 379 | } |
| 375 | 380 | if( pStmt->pPrev ){ |
| 376 | 381 | pStmt->pPrev->pNext = pStmt->pNext; |
| 377 | - }else if( pAllStmt==pStmt ){ | |
| 378 | - pAllStmt = pStmt->pNext; | |
| 382 | + }else if( db.pAllStmt==pStmt ){ | |
| 383 | + db.pAllStmt = pStmt->pNext; | |
| 379 | 384 | } |
| 380 | 385 | pStmt->pNext = 0; |
| 381 | 386 | pStmt->pPrev = 0; |
| 382 | 387 | return rc; |
| 383 | 388 | } |
| @@ -1086,14 +1091,14 @@ | ||
| 1086 | 1091 | fprintf(stderr, "-- MALLOC_SIZE %10d\n", hiwtr); |
| 1087 | 1092 | sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &cur, &hiwtr, 0); |
| 1088 | 1093 | fprintf(stderr, "-- MALLOC_COUNT %10d %10d\n", cur, hiwtr); |
| 1089 | 1094 | sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &cur, &hiwtr, 0); |
| 1090 | 1095 | fprintf(stderr, "-- PCACHE_OVFLOW %10d %10d\n", cur, hiwtr); |
| 1091 | - fprintf(stderr, "-- prepared statements %10d\n", nPrepare); | |
| 1096 | + fprintf(stderr, "-- prepared statements %10d\n", db.nPrepare); | |
| 1092 | 1097 | } |
| 1093 | - while( pAllStmt ){ | |
| 1094 | - db_finalize(pAllStmt); | |
| 1098 | + while( db.pAllStmt ){ | |
| 1099 | + db_finalize(db.pAllStmt); | |
| 1095 | 1100 | } |
| 1096 | 1101 | db_end_transaction(1); |
| 1097 | 1102 | pStmt = 0; |
| 1098 | 1103 | if( reportErrors ){ |
| 1099 | 1104 | while( (pStmt = sqlite3_next_stmt(g.db, pStmt))!=0 ){ |
| 1100 | 1105 |
| --- src/db.c | |
| +++ src/db.c | |
| @@ -99,29 +99,34 @@ | |
| 99 | free(z); |
| 100 | db_force_rollback(); |
| 101 | fossil_exit(rc); |
| 102 | } |
| 103 | |
| 104 | static int nBegin = 0; /* Nesting depth of BEGIN */ |
| 105 | static int doRollback = 0; /* True to force a rollback */ |
| 106 | static int nCommitHook = 0; /* Number of commit hooks */ |
| 107 | static struct sCommitHook { |
| 108 | int (*xHook)(void); /* Functions to call at db_end_transaction() */ |
| 109 | int sequence; /* Call functions in sequence order */ |
| 110 | } aHook[5]; |
| 111 | static Stmt *pAllStmt = 0; /* List of all unfinalized statements */ |
| 112 | static int nPrepare = 0; /* Number of calls to sqlite3_prepare() */ |
| 113 | static int nDeleteOnFail = 0; /* Number of entries in azDeleteOnFail[] */ |
| 114 | static char *azDeleteOnFail[3]; /* Files to delete on a failure */ |
| 115 | |
| 116 | |
| 117 | /* |
| 118 | ** Arrange for the given file to be deleted on a failure. |
| 119 | */ |
| 120 | void db_delete_on_failure(const char *zFilename){ |
| 121 | assert( nDeleteOnFail<count(azDeleteOnFail) ); |
| 122 | azDeleteOnFail[nDeleteOnFail++] = fossil_strdup(zFilename); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | ** This routine is called by the SQLite commit-hook mechanism |
| 127 | ** just prior to each commit. All this routine does is verify |
| @@ -130,11 +135,11 @@ | |
| 130 | ** below. |
| 131 | ** |
| 132 | ** This is just a safety and sanity check. |
| 133 | */ |
| 134 | static int db_verify_at_commit(void *notUsed){ |
| 135 | if( nBegin ){ |
| 136 | fossil_panic("illegal commit attempt"); |
| 137 | return 1; |
| 138 | } |
| 139 | return 0; |
| 140 | } |
| @@ -141,32 +146,32 @@ | |
| 141 | |
| 142 | /* |
| 143 | ** Begin and end a nested transaction |
| 144 | */ |
| 145 | void db_begin_transaction(void){ |
| 146 | if( nBegin==0 ){ |
| 147 | db_multi_exec("BEGIN"); |
| 148 | sqlite3_commit_hook(g.db, db_verify_at_commit, 0); |
| 149 | } |
| 150 | nBegin++; |
| 151 | } |
| 152 | void db_end_transaction(int rollbackFlag){ |
| 153 | if( g.db==0 ) return; |
| 154 | if( nBegin<=0 ) return; |
| 155 | if( rollbackFlag ) doRollback = 1; |
| 156 | nBegin--; |
| 157 | if( nBegin==0 ){ |
| 158 | int i; |
| 159 | if( doRollback==0 ) leaf_do_pending_checks(); |
| 160 | for(i=0; doRollback==0 && i<nCommitHook; i++){ |
| 161 | doRollback |= aHook[i].xHook(); |
| 162 | } |
| 163 | while( pAllStmt ){ |
| 164 | db_finalize(pAllStmt); |
| 165 | } |
| 166 | db_multi_exec(doRollback ? "ROLLBACK" : "COMMIT"); |
| 167 | doRollback = 0; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | ** Force a rollback and shutdown the database |
| @@ -179,21 +184,21 @@ | |
| 179 | busy = 1; |
| 180 | undo_rollback(); |
| 181 | while( (pStmt = sqlite3_next_stmt(g.db,pStmt))!=0 ){ |
| 182 | sqlite3_reset(pStmt); |
| 183 | } |
| 184 | while( pAllStmt ){ |
| 185 | db_finalize(pAllStmt); |
| 186 | } |
| 187 | if( nBegin ){ |
| 188 | sqlite3_exec(g.db, "ROLLBACK", 0, 0, 0); |
| 189 | nBegin = 0; |
| 190 | } |
| 191 | busy = 0; |
| 192 | db_close(0); |
| 193 | for(i=0; i<nDeleteOnFail; i++){ |
| 194 | file_delete(azDeleteOnFail[i]); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | ** Install a commit hook. Hooks are installed in sequence order. |
| @@ -205,25 +210,25 @@ | |
| 205 | ** rolls back rather than commit. It is the responsibility of the |
| 206 | ** hooks themselves to issue any error messages. |
| 207 | */ |
| 208 | void db_commit_hook(int (*x)(void), int sequence){ |
| 209 | int i; |
| 210 | assert( nCommitHook < sizeof(aHook)/sizeof(aHook[1]) ); |
| 211 | for(i=0; i<nCommitHook; i++){ |
| 212 | assert( x!=aHook[i].xHook ); |
| 213 | if( aHook[i].sequence>sequence ){ |
| 214 | int s = sequence; |
| 215 | int (*xS)(void) = x; |
| 216 | sequence = aHook[i].sequence; |
| 217 | x = aHook[i].xHook; |
| 218 | aHook[i].sequence = s; |
| 219 | aHook[i].xHook = xS; |
| 220 | } |
| 221 | } |
| 222 | aHook[nCommitHook].sequence = sequence; |
| 223 | aHook[nCommitHook].xHook = x; |
| 224 | nCommitHook++; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | ** Prepare a Stmt. Assume that the Stmt is previously uninitialized. |
| 229 | ** If the input string contains multiple SQL statements, only the first |
| @@ -234,11 +239,11 @@ | |
| 234 | char *zSql; |
| 235 | blob_zero(&pStmt->sql); |
| 236 | blob_vappendf(&pStmt->sql, zFormat, ap); |
| 237 | va_end(ap); |
| 238 | zSql = blob_str(&pStmt->sql); |
| 239 | nPrepare++; |
| 240 | rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt->pStmt, 0); |
| 241 | if( rc!=0 && !errOk ){ |
| 242 | db_err("%s\n%s", sqlite3_errmsg(g.db), zSql); |
| 243 | } |
| 244 | pStmt->pNext = pStmt->pPrev = 0; |
| @@ -265,14 +270,14 @@ | |
| 265 | int rc = SQLITE_OK; |
| 266 | if( blob_size(&pStmt->sql)==0 ){ |
| 267 | va_list ap; |
| 268 | va_start(ap, zFormat); |
| 269 | rc = db_vprepare(pStmt, 0, zFormat, ap); |
| 270 | pStmt->pNext = pAllStmt; |
| 271 | pStmt->pPrev = 0; |
| 272 | if( pAllStmt ) pAllStmt->pPrev = pStmt; |
| 273 | pAllStmt = pStmt; |
| 274 | va_end(ap); |
| 275 | } |
| 276 | return rc; |
| 277 | } |
| 278 | |
| @@ -372,12 +377,12 @@ | |
| 372 | if( pStmt->pNext ){ |
| 373 | pStmt->pNext->pPrev = pStmt->pPrev; |
| 374 | } |
| 375 | if( pStmt->pPrev ){ |
| 376 | pStmt->pPrev->pNext = pStmt->pNext; |
| 377 | }else if( pAllStmt==pStmt ){ |
| 378 | pAllStmt = pStmt->pNext; |
| 379 | } |
| 380 | pStmt->pNext = 0; |
| 381 | pStmt->pPrev = 0; |
| 382 | return rc; |
| 383 | } |
| @@ -1086,14 +1091,14 @@ | |
| 1086 | fprintf(stderr, "-- MALLOC_SIZE %10d\n", hiwtr); |
| 1087 | sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &cur, &hiwtr, 0); |
| 1088 | fprintf(stderr, "-- MALLOC_COUNT %10d %10d\n", cur, hiwtr); |
| 1089 | sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &cur, &hiwtr, 0); |
| 1090 | fprintf(stderr, "-- PCACHE_OVFLOW %10d %10d\n", cur, hiwtr); |
| 1091 | fprintf(stderr, "-- prepared statements %10d\n", nPrepare); |
| 1092 | } |
| 1093 | while( pAllStmt ){ |
| 1094 | db_finalize(pAllStmt); |
| 1095 | } |
| 1096 | db_end_transaction(1); |
| 1097 | pStmt = 0; |
| 1098 | if( reportErrors ){ |
| 1099 | while( (pStmt = sqlite3_next_stmt(g.db, pStmt))!=0 ){ |
| 1100 |
| --- src/db.c | |
| +++ src/db.c | |
| @@ -99,29 +99,34 @@ | |
| 99 | free(z); |
| 100 | db_force_rollback(); |
| 101 | fossil_exit(rc); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | ** All static variable that a used by only this file are gathered into |
| 106 | ** the following structure. |
| 107 | */ |
| 108 | static struct DbLocalData { |
| 109 | int nBegin; /* Nesting depth of BEGIN */ |
| 110 | int doRollback; /* True to force a rollback */ |
| 111 | int nCommitHook; /* Number of commit hooks */ |
| 112 | Stmt *pAllStmt; /* List of all unfinalized statements */ |
| 113 | int nPrepare; /* Number of calls to sqlite3_prepare() */ |
| 114 | int nDeleteOnFail; /* Number of entries in azDeleteOnFail[] */ |
| 115 | struct sCommitHook { |
| 116 | int (*xHook)(void); /* Functions to call at db_end_transaction() */ |
| 117 | int sequence; /* Call functions in sequence order */ |
| 118 | } aHook[5]; |
| 119 | char *azDeleteOnFail[3]; /* Files to delete on a failure */ |
| 120 | } db = {0, 0, 0, 0, 0, 0, }; |
| 121 | |
| 122 | /* |
| 123 | ** Arrange for the given file to be deleted on a failure. |
| 124 | */ |
| 125 | void db_delete_on_failure(const char *zFilename){ |
| 126 | assert( db.nDeleteOnFail<count(db.azDeleteOnFail) ); |
| 127 | db.azDeleteOnFail[db.nDeleteOnFail++] = fossil_strdup(zFilename); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | ** This routine is called by the SQLite commit-hook mechanism |
| 132 | ** just prior to each commit. All this routine does is verify |
| @@ -130,11 +135,11 @@ | |
| 135 | ** below. |
| 136 | ** |
| 137 | ** This is just a safety and sanity check. |
| 138 | */ |
| 139 | static int db_verify_at_commit(void *notUsed){ |
| 140 | if( db.nBegin ){ |
| 141 | fossil_panic("illegal commit attempt"); |
| 142 | return 1; |
| 143 | } |
| 144 | return 0; |
| 145 | } |
| @@ -141,32 +146,32 @@ | |
| 146 | |
| 147 | /* |
| 148 | ** Begin and end a nested transaction |
| 149 | */ |
| 150 | void db_begin_transaction(void){ |
| 151 | if( db.nBegin==0 ){ |
| 152 | db_multi_exec("BEGIN"); |
| 153 | sqlite3_commit_hook(g.db, db_verify_at_commit, 0); |
| 154 | } |
| 155 | db.nBegin++; |
| 156 | } |
| 157 | void db_end_transaction(int rollbackFlag){ |
| 158 | if( g.db==0 ) return; |
| 159 | if( db.nBegin<=0 ) return; |
| 160 | if( rollbackFlag ) db.doRollback = 1; |
| 161 | db.nBegin--; |
| 162 | if( db.nBegin==0 ){ |
| 163 | int i; |
| 164 | if( db.doRollback==0 ) leaf_do_pending_checks(); |
| 165 | for(i=0; db.doRollback==0 && i<db.nCommitHook; i++){ |
| 166 | db.doRollback |= db.aHook[i].xHook(); |
| 167 | } |
| 168 | while( db.pAllStmt ){ |
| 169 | db_finalize(db.pAllStmt); |
| 170 | } |
| 171 | db_multi_exec(db.doRollback ? "ROLLBACK" : "COMMIT"); |
| 172 | db.doRollback = 0; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | ** Force a rollback and shutdown the database |
| @@ -179,21 +184,21 @@ | |
| 184 | busy = 1; |
| 185 | undo_rollback(); |
| 186 | while( (pStmt = sqlite3_next_stmt(g.db,pStmt))!=0 ){ |
| 187 | sqlite3_reset(pStmt); |
| 188 | } |
| 189 | while( db.pAllStmt ){ |
| 190 | db_finalize(db.pAllStmt); |
| 191 | } |
| 192 | if( db.nBegin ){ |
| 193 | sqlite3_exec(g.db, "ROLLBACK", 0, 0, 0); |
| 194 | db.nBegin = 0; |
| 195 | } |
| 196 | busy = 0; |
| 197 | db_close(0); |
| 198 | for(i=0; i<db.nDeleteOnFail; i++){ |
| 199 | file_delete(db.azDeleteOnFail[i]); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | ** Install a commit hook. Hooks are installed in sequence order. |
| @@ -205,25 +210,25 @@ | |
| 210 | ** rolls back rather than commit. It is the responsibility of the |
| 211 | ** hooks themselves to issue any error messages. |
| 212 | */ |
| 213 | void db_commit_hook(int (*x)(void), int sequence){ |
| 214 | int i; |
| 215 | assert( db.nCommitHook < sizeof(db.aHook)/sizeof(db.aHook[1]) ); |
| 216 | for(i=0; i<db.nCommitHook; i++){ |
| 217 | assert( x!=db.aHook[i].xHook ); |
| 218 | if( db.aHook[i].sequence>sequence ){ |
| 219 | int s = sequence; |
| 220 | int (*xS)(void) = x; |
| 221 | sequence = db.aHook[i].sequence; |
| 222 | x = db.aHook[i].xHook; |
| 223 | db.aHook[i].sequence = s; |
| 224 | db.aHook[i].xHook = xS; |
| 225 | } |
| 226 | } |
| 227 | db.aHook[db.nCommitHook].sequence = sequence; |
| 228 | db.aHook[db.nCommitHook].xHook = x; |
| 229 | db.nCommitHook++; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | ** Prepare a Stmt. Assume that the Stmt is previously uninitialized. |
| 234 | ** If the input string contains multiple SQL statements, only the first |
| @@ -234,11 +239,11 @@ | |
| 239 | char *zSql; |
| 240 | blob_zero(&pStmt->sql); |
| 241 | blob_vappendf(&pStmt->sql, zFormat, ap); |
| 242 | va_end(ap); |
| 243 | zSql = blob_str(&pStmt->sql); |
| 244 | db.nPrepare++; |
| 245 | rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt->pStmt, 0); |
| 246 | if( rc!=0 && !errOk ){ |
| 247 | db_err("%s\n%s", sqlite3_errmsg(g.db), zSql); |
| 248 | } |
| 249 | pStmt->pNext = pStmt->pPrev = 0; |
| @@ -265,14 +270,14 @@ | |
| 270 | int rc = SQLITE_OK; |
| 271 | if( blob_size(&pStmt->sql)==0 ){ |
| 272 | va_list ap; |
| 273 | va_start(ap, zFormat); |
| 274 | rc = db_vprepare(pStmt, 0, zFormat, ap); |
| 275 | pStmt->pNext = db.pAllStmt; |
| 276 | pStmt->pPrev = 0; |
| 277 | if( db.pAllStmt ) db.pAllStmt->pPrev = pStmt; |
| 278 | db.pAllStmt = pStmt; |
| 279 | va_end(ap); |
| 280 | } |
| 281 | return rc; |
| 282 | } |
| 283 | |
| @@ -372,12 +377,12 @@ | |
| 377 | if( pStmt->pNext ){ |
| 378 | pStmt->pNext->pPrev = pStmt->pPrev; |
| 379 | } |
| 380 | if( pStmt->pPrev ){ |
| 381 | pStmt->pPrev->pNext = pStmt->pNext; |
| 382 | }else if( db.pAllStmt==pStmt ){ |
| 383 | db.pAllStmt = pStmt->pNext; |
| 384 | } |
| 385 | pStmt->pNext = 0; |
| 386 | pStmt->pPrev = 0; |
| 387 | return rc; |
| 388 | } |
| @@ -1086,14 +1091,14 @@ | |
| 1091 | fprintf(stderr, "-- MALLOC_SIZE %10d\n", hiwtr); |
| 1092 | sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &cur, &hiwtr, 0); |
| 1093 | fprintf(stderr, "-- MALLOC_COUNT %10d %10d\n", cur, hiwtr); |
| 1094 | sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &cur, &hiwtr, 0); |
| 1095 | fprintf(stderr, "-- PCACHE_OVFLOW %10d %10d\n", cur, hiwtr); |
| 1096 | fprintf(stderr, "-- prepared statements %10d\n", db.nPrepare); |
| 1097 | } |
| 1098 | while( db.pAllStmt ){ |
| 1099 | db_finalize(db.pAllStmt); |
| 1100 | } |
| 1101 | db_end_transaction(1); |
| 1102 | pStmt = 0; |
| 1103 | if( reportErrors ){ |
| 1104 | while( (pStmt = sqlite3_next_stmt(g.db, pStmt))!=0 ){ |
| 1105 |