Fossil SCM
Extracted json_serialize_array() function common to both test-json-carray and settings commands, reducing redundant code. No functional change.
Commit
ead1432af9b5288d1f294ac4b02feae0b371c26c03b32fde189f3de8d82560ff
Parent
ca069402f8fb2d5…
1 file changed
+18
-36
M
src/db.c
+18
-36
| --- src/db.c | ||
| +++ src/db.c | ||
| @@ -3220,36 +3220,11 @@ | ||
| 3220 | 3220 | } |
| 3221 | 3221 | db_end_transaction(0); |
| 3222 | 3222 | db_protect_pop(); |
| 3223 | 3223 | } |
| 3224 | 3224 | void db_set_array(const char *zName, char* azValues[], size_t nValues, int globalFlag){ |
| 3225 | - Stmt q; | |
| 3226 | - db_assert_protection_off_or_not_sensitive(zName); | |
| 3227 | - db_unprotect(PROTECT_CONFIG); | |
| 3228 | - db_begin_transaction(); | |
| 3229 | - if( globalFlag ){ | |
| 3230 | - db_swap_connections(); | |
| 3231 | - sqlite3_carray_init(g.db, 0, 0); /* not registered globally on purpose */ | |
| 3232 | - db_prepare(&q, "REPLACE INTO global_config(name,value) VALUES(%Q," | |
| 3233 | - "(SELECT json_group_array(value) FROM carray(?1)))", | |
| 3234 | - zName); | |
| 3235 | - sqlite3_carray_bind(q.pStmt, 1, azValues, nValues, CARRAY_TEXT, SQLITE_STATIC); | |
| 3236 | - db_exec(&q); | |
| 3237 | - db_swap_connections(); | |
| 3238 | - }else{ | |
| 3239 | - sqlite3_carray_init(g.db, 0, 0); /* ditto */ | |
| 3240 | - db_prepare(&q, "REPLACE INTO config(name,value,mtime) VALUES(%Q," | |
| 3241 | - "(SELECT json_group_array(value) FROM carray(?1)),now())", | |
| 3242 | - zName); | |
| 3243 | - sqlite3_carray_bind(q.pStmt, 1, azValues, nValues, CARRAY_TEXT, SQLITE_STATIC); | |
| 3244 | - db_exec(&q); | |
| 3245 | - } | |
| 3246 | - if( globalFlag && g.repositoryOpen ){ | |
| 3247 | - db_multi_exec("DELETE FROM config WHERE name=%Q", zName); | |
| 3248 | - } | |
| 3249 | - db_end_transaction(0); | |
| 3250 | - db_protect_pop(); | |
| 3225 | + db_set(zName, json_serialize_array(azValues, nValues), globalFlag); | |
| 3251 | 3226 | } |
| 3252 | 3227 | void db_unset(const char *zName, int globalFlag){ |
| 3253 | 3228 | db_begin_transaction(); |
| 3254 | 3229 | db_unprotect(PROTECT_CONFIG); |
| 3255 | 3230 | if( globalFlag ){ |
| @@ -4524,28 +4499,35 @@ | ||
| 4524 | 4499 | ** |
| 4525 | 4500 | ** Serializes the passed arguments as a JSON array of strings, proving that |
| 4526 | 4501 | ** the JSON1 and Carray SQLite extensions are cooperating. |
| 4527 | 4502 | */ |
| 4528 | 4503 | void test_json_carray_cmd(void){ |
| 4504 | + fossil_print("%s\n", json_serialize_array(g.argv+2, g.argc-2)); | |
| 4505 | +} | |
| 4506 | + | |
| 4507 | +/* | |
| 4508 | +** Serializes the passed array as a JSON array of strings. | |
| 4509 | +*/ | |
| 4510 | +const char* json_serialize_array(char* const azValues[], size_t nValues){ | |
| 4529 | 4511 | Stmt q; |
| 4530 | - sqlite3_open(":memory:", &g.db); | |
| 4531 | - sqlite3_carray_init(g.db, 0, 0); | |
| 4512 | + sqlite3* db; | |
| 4513 | + sqlite3_open(":memory:", &db); | |
| 4514 | + sqlite3_carray_init(db, 0, 0); | |
| 4532 | 4515 | db_prepare(&q, "SELECT json_group_array(value) FROM carray(?1)"); |
| 4533 | - if( sqlite3_carray_bind(q.pStmt, 1, g.argv+2, g.argc-2, CARRAY_TEXT, | |
| 4516 | + if( sqlite3_carray_bind(q.pStmt, 1, (void*)azValues, nValues, CARRAY_TEXT, | |
| 4534 | 4517 | SQLITE_STATIC)!= SQLITE_OK){ |
| 4535 | - fossil_fatal("Could not bind argv array: %s\n", sqlite3_errmsg(g.db)); | |
| 4518 | + fossil_fatal("Could not bind argv array for JSON: %s\n", | |
| 4519 | + sqlite3_errmsg(db)); | |
| 4536 | 4520 | } |
| 4537 | 4521 | if( db_step(&q)==SQLITE_ROW ){ |
| 4538 | - fossil_print("%s\n", db_column_text(&q, 0)); | |
| 4522 | + const char* ret = fossil_strdup(db_column_text(&q, 0)); | |
| 4523 | + db_finalize(&q); | |
| 4524 | + sqlite3_close(db); | |
| 4525 | + return ret; | |
| 4539 | 4526 | }else{ |
| 4540 | 4527 | fossil_fatal("SQLite error: %s", sqlite3_errmsg(g.db)); |
| 4541 | 4528 | } |
| 4542 | - db_finalize(&q); | |
| 4543 | - sqlite3_close(g.db); | |
| 4544 | - g.db = 0; | |
| 4545 | - g.repositoryOpen = 0; | |
| 4546 | - g.localOpen = 0; | |
| 4547 | 4529 | } |
| 4548 | 4530 | |
| 4549 | 4531 | /* |
| 4550 | 4532 | ** COMMAND: test-without-rowid |
| 4551 | 4533 | ** |
| 4552 | 4534 |
| --- src/db.c | |
| +++ src/db.c | |
| @@ -3220,36 +3220,11 @@ | |
| 3220 | } |
| 3221 | db_end_transaction(0); |
| 3222 | db_protect_pop(); |
| 3223 | } |
| 3224 | void db_set_array(const char *zName, char* azValues[], size_t nValues, int globalFlag){ |
| 3225 | Stmt q; |
| 3226 | db_assert_protection_off_or_not_sensitive(zName); |
| 3227 | db_unprotect(PROTECT_CONFIG); |
| 3228 | db_begin_transaction(); |
| 3229 | if( globalFlag ){ |
| 3230 | db_swap_connections(); |
| 3231 | sqlite3_carray_init(g.db, 0, 0); /* not registered globally on purpose */ |
| 3232 | db_prepare(&q, "REPLACE INTO global_config(name,value) VALUES(%Q," |
| 3233 | "(SELECT json_group_array(value) FROM carray(?1)))", |
| 3234 | zName); |
| 3235 | sqlite3_carray_bind(q.pStmt, 1, azValues, nValues, CARRAY_TEXT, SQLITE_STATIC); |
| 3236 | db_exec(&q); |
| 3237 | db_swap_connections(); |
| 3238 | }else{ |
| 3239 | sqlite3_carray_init(g.db, 0, 0); /* ditto */ |
| 3240 | db_prepare(&q, "REPLACE INTO config(name,value,mtime) VALUES(%Q," |
| 3241 | "(SELECT json_group_array(value) FROM carray(?1)),now())", |
| 3242 | zName); |
| 3243 | sqlite3_carray_bind(q.pStmt, 1, azValues, nValues, CARRAY_TEXT, SQLITE_STATIC); |
| 3244 | db_exec(&q); |
| 3245 | } |
| 3246 | if( globalFlag && g.repositoryOpen ){ |
| 3247 | db_multi_exec("DELETE FROM config WHERE name=%Q", zName); |
| 3248 | } |
| 3249 | db_end_transaction(0); |
| 3250 | db_protect_pop(); |
| 3251 | } |
| 3252 | void db_unset(const char *zName, int globalFlag){ |
| 3253 | db_begin_transaction(); |
| 3254 | db_unprotect(PROTECT_CONFIG); |
| 3255 | if( globalFlag ){ |
| @@ -4524,28 +4499,35 @@ | |
| 4524 | ** |
| 4525 | ** Serializes the passed arguments as a JSON array of strings, proving that |
| 4526 | ** the JSON1 and Carray SQLite extensions are cooperating. |
| 4527 | */ |
| 4528 | void test_json_carray_cmd(void){ |
| 4529 | Stmt q; |
| 4530 | sqlite3_open(":memory:", &g.db); |
| 4531 | sqlite3_carray_init(g.db, 0, 0); |
| 4532 | db_prepare(&q, "SELECT json_group_array(value) FROM carray(?1)"); |
| 4533 | if( sqlite3_carray_bind(q.pStmt, 1, g.argv+2, g.argc-2, CARRAY_TEXT, |
| 4534 | SQLITE_STATIC)!= SQLITE_OK){ |
| 4535 | fossil_fatal("Could not bind argv array: %s\n", sqlite3_errmsg(g.db)); |
| 4536 | } |
| 4537 | if( db_step(&q)==SQLITE_ROW ){ |
| 4538 | fossil_print("%s\n", db_column_text(&q, 0)); |
| 4539 | }else{ |
| 4540 | fossil_fatal("SQLite error: %s", sqlite3_errmsg(g.db)); |
| 4541 | } |
| 4542 | db_finalize(&q); |
| 4543 | sqlite3_close(g.db); |
| 4544 | g.db = 0; |
| 4545 | g.repositoryOpen = 0; |
| 4546 | g.localOpen = 0; |
| 4547 | } |
| 4548 | |
| 4549 | /* |
| 4550 | ** COMMAND: test-without-rowid |
| 4551 | ** |
| 4552 |
| --- src/db.c | |
| +++ src/db.c | |
| @@ -3220,36 +3220,11 @@ | |
| 3220 | } |
| 3221 | db_end_transaction(0); |
| 3222 | db_protect_pop(); |
| 3223 | } |
| 3224 | void db_set_array(const char *zName, char* azValues[], size_t nValues, int globalFlag){ |
| 3225 | db_set(zName, json_serialize_array(azValues, nValues), globalFlag); |
| 3226 | } |
| 3227 | void db_unset(const char *zName, int globalFlag){ |
| 3228 | db_begin_transaction(); |
| 3229 | db_unprotect(PROTECT_CONFIG); |
| 3230 | if( globalFlag ){ |
| @@ -4524,28 +4499,35 @@ | |
| 4499 | ** |
| 4500 | ** Serializes the passed arguments as a JSON array of strings, proving that |
| 4501 | ** the JSON1 and Carray SQLite extensions are cooperating. |
| 4502 | */ |
| 4503 | void test_json_carray_cmd(void){ |
| 4504 | fossil_print("%s\n", json_serialize_array(g.argv+2, g.argc-2)); |
| 4505 | } |
| 4506 | |
| 4507 | /* |
| 4508 | ** Serializes the passed array as a JSON array of strings. |
| 4509 | */ |
| 4510 | const char* json_serialize_array(char* const azValues[], size_t nValues){ |
| 4511 | Stmt q; |
| 4512 | sqlite3* db; |
| 4513 | sqlite3_open(":memory:", &db); |
| 4514 | sqlite3_carray_init(db, 0, 0); |
| 4515 | db_prepare(&q, "SELECT json_group_array(value) FROM carray(?1)"); |
| 4516 | if( sqlite3_carray_bind(q.pStmt, 1, (void*)azValues, nValues, CARRAY_TEXT, |
| 4517 | SQLITE_STATIC)!= SQLITE_OK){ |
| 4518 | fossil_fatal("Could not bind argv array for JSON: %s\n", |
| 4519 | sqlite3_errmsg(db)); |
| 4520 | } |
| 4521 | if( db_step(&q)==SQLITE_ROW ){ |
| 4522 | const char* ret = fossil_strdup(db_column_text(&q, 0)); |
| 4523 | db_finalize(&q); |
| 4524 | sqlite3_close(db); |
| 4525 | return ret; |
| 4526 | }else{ |
| 4527 | fossil_fatal("SQLite error: %s", sqlite3_errmsg(g.db)); |
| 4528 | } |
| 4529 | } |
| 4530 | |
| 4531 | /* |
| 4532 | ** COMMAND: test-without-rowid |
| 4533 | ** |
| 4534 |