Fossil

Check-in [ead1432af9]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Extracted json_serialize_array() function common to both test-json-carray and settings commands, reducing redundant code.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | fossil-spawn
Files: files | file ages | folders
SHA3-256: ead1432af9b5288d1f294ac4b02feae0b371c26c03b32fde189f3de8d82560ff
User & Date: wyoung 2021-06-22 03:45:22.026
Original Comment: Extracted json_serialize_array() function common to both test-json-carray and settings commands, reducing redundant code. No functional change.
References
2021-06-22
08:46
[ead1432af9 | An earlier refactoring on this branch] broke the new array-based setting method: got a little too clever with my use of DB handles. check-in: 3bcf3abd56 user: wyoung tags: fossil-spawn
Context
2021-06-22
03:52
Renamed the test-* wrapper for the new JSON array serializer to better match its internal implementation function. NFC. check-in: 06d27250d5 user: wyoung tags: fossil-spawn
03:45
Extracted json_serialize_array() function common to both test-json-carray and settings commands, reducing redundant code. check-in: ead1432af9 user: wyoung tags: fossil-spawn
02:37
Taught "fossil settings" how to accept multiple ?VALUES? parameters, storing the result as a JSON-encoded array in the config table. Nothing uses this yet, but the resulting SQL DB manipulation appears to work correctly. check-in: ca069402f8 user: wyoung tags: fossil-spawn
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/db.c.
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
  if( globalFlag && g.repositoryOpen ){
    db_multi_exec("DELETE FROM config WHERE name=%Q", zName);
  }
  db_end_transaction(0);
  db_protect_pop();
}
void db_set_array(const char *zName, char* azValues[], size_t nValues, int globalFlag){
  Stmt q;
  db_assert_protection_off_or_not_sensitive(zName);
  db_unprotect(PROTECT_CONFIG);
  db_begin_transaction();
  if( globalFlag ){
    db_swap_connections();
    sqlite3_carray_init(g.db, 0, 0);      /* not registered globally on purpose */
    db_prepare(&q, "REPLACE INTO global_config(name,value) VALUES(%Q,"
                     "(SELECT json_group_array(value) FROM carray(?1)))",
                    zName);
    sqlite3_carray_bind(q.pStmt, 1, azValues, nValues, CARRAY_TEXT, SQLITE_STATIC);
    db_exec(&q);
    db_swap_connections();
  }else{
    sqlite3_carray_init(g.db, 0, 0);      /* ditto */
    db_prepare(&q, "REPLACE INTO config(name,value,mtime) VALUES(%Q,"
                     "(SELECT json_group_array(value) FROM carray(?1)),now())",
                    zName);
    sqlite3_carray_bind(q.pStmt, 1, azValues, nValues, CARRAY_TEXT, SQLITE_STATIC);
    db_exec(&q);
  }
  if( globalFlag && g.repositoryOpen ){
    db_multi_exec("DELETE FROM config WHERE name=%Q", zName);
  }
  db_end_transaction(0);
  db_protect_pop();
}
void db_unset(const char *zName, int globalFlag){
  db_begin_transaction();
  db_unprotect(PROTECT_CONFIG);
  if( globalFlag ){
    db_swap_connections();
    db_multi_exec("DELETE FROM global_config WHERE name=%Q", zName);







<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







3218
3219
3220
3221
3222
3223
3224




3225





















3226
3227
3228
3229
3230
3231
3232
  if( globalFlag && g.repositoryOpen ){
    db_multi_exec("DELETE FROM config WHERE name=%Q", zName);
  }
  db_end_transaction(0);
  db_protect_pop();
}
void db_set_array(const char *zName, char* azValues[], size_t nValues, int globalFlag){




  db_set(zName, json_serialize_array(azValues, nValues), globalFlag);





















}
void db_unset(const char *zName, int globalFlag){
  db_begin_transaction();
  db_unprotect(PROTECT_CONFIG);
  if( globalFlag ){
    db_swap_connections();
    db_multi_exec("DELETE FROM global_config WHERE name=%Q", zName);
4522
4523
4524
4525
4526
4527
4528







4529

4530
4531
4532
4533
4534
4535

4536
4537
4538



4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
/*
** COMMAND: test-json-carray
**
** Serializes the passed arguments as a JSON array of strings, proving that
** the JSON1 and Carray SQLite extensions are cooperating.
*/
void test_json_carray_cmd(void){







  Stmt q;

  sqlite3_open(":memory:", &g.db);
  sqlite3_carray_init(g.db, 0, 0);
  db_prepare(&q, "SELECT json_group_array(value) FROM carray(?1)");
  if( sqlite3_carray_bind(q.pStmt, 1, g.argv+2, g.argc-2, CARRAY_TEXT,
        SQLITE_STATIC)!= SQLITE_OK){
    fossil_fatal("Could not bind argv array: %s\n", sqlite3_errmsg(g.db));

  }
  if( db_step(&q)==SQLITE_ROW ){
    fossil_print("%s\n", db_column_text(&q, 0));



  }else{ 
    fossil_fatal("SQLite error: %s", sqlite3_errmsg(g.db));
  }
  db_finalize(&q);
  sqlite3_close(g.db);
  g.db = 0;
  g.repositoryOpen = 0;
  g.localOpen = 0;
}

/*
** COMMAND: test-without-rowid
**
** Usage: %fossil test-without-rowid FILENAME...
**







>
>
>
>
>
>
>

>
|
|

|

|
>


|
>
>
>



<
<
<
<
<







4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528





4529
4530
4531
4532
4533
4534
4535
/*
** COMMAND: test-json-carray
**
** Serializes the passed arguments as a JSON array of strings, proving that
** the JSON1 and Carray SQLite extensions are cooperating.
*/
void test_json_carray_cmd(void){
  fossil_print("%s\n", json_serialize_array(g.argv+2, g.argc-2));
}

/*
** Serializes the passed array as a JSON array of strings.
*/
const char* json_serialize_array(char* const azValues[], size_t nValues){
  Stmt q;
  sqlite3* db;
  sqlite3_open(":memory:", &db);
  sqlite3_carray_init(db, 0, 0);
  db_prepare(&q, "SELECT json_group_array(value) FROM carray(?1)");
  if( sqlite3_carray_bind(q.pStmt, 1, (void*)azValues, nValues, CARRAY_TEXT,
        SQLITE_STATIC)!= SQLITE_OK){
    fossil_fatal("Could not bind argv array for JSON: %s\n",
        sqlite3_errmsg(db));
  }
  if( db_step(&q)==SQLITE_ROW ){
    const char* ret = fossil_strdup(db_column_text(&q, 0));
    db_finalize(&q);
    sqlite3_close(db);
    return ret;
  }else{ 
    fossil_fatal("SQLite error: %s", sqlite3_errmsg(g.db));
  }





}

/*
** COMMAND: test-without-rowid
**
** Usage: %fossil test-without-rowid FILENAME...
**