Fossil SCM
Consolidate all sqlite3_open() calls into the db_open() routine and initialize every database connection the same way. Avoid using sqlite3_exec() in order to obtain a more accurate count of perpared statements when --sqltrace is used.
Commit
f97e1cf6662be46c53fd16416d4030b310bedc69
Parent
7536c6aea562700…
1 file changed
+37
-37
M
src/db.c
+37
-37
| --- src/db.c | ||
| +++ src/db.c | ||
| @@ -484,20 +484,30 @@ | ||
| 484 | 484 | /* |
| 485 | 485 | ** Execute multiple SQL statements. |
| 486 | 486 | */ |
| 487 | 487 | int db_multi_exec(const char *zSql, ...){ |
| 488 | 488 | Blob sql; |
| 489 | - int rc; | |
| 489 | + int rc = SQLITE_OK; | |
| 490 | 490 | va_list ap; |
| 491 | - char *zErr = 0; | |
| 491 | + const char *z, *zEnd; | |
| 492 | + sqlite3_stmt *pStmt; | |
| 492 | 493 | blob_init(&sql, 0, 0); |
| 493 | 494 | va_start(ap, zSql); |
| 494 | 495 | blob_vappendf(&sql, zSql, ap); |
| 495 | 496 | va_end(ap); |
| 496 | - rc = sqlite3_exec(g.db, blob_buffer(&sql), 0, 0, &zErr); | |
| 497 | - if( rc!=SQLITE_OK ){ | |
| 498 | - db_err("%s\n%s", zErr, blob_buffer(&sql)); | |
| 497 | + z = blob_str(&sql); | |
| 498 | + while( rc==SQLITE_OK && z[0] ){ | |
| 499 | + pStmt = 0; | |
| 500 | + rc = sqlite3_prepare_v2(g.db, z, -1, &pStmt, &zEnd); | |
| 501 | + if( rc!=SQLITE_OK ) break; | |
| 502 | + if( pStmt ){ | |
| 503 | + db.nPrepare++; | |
| 504 | + while( sqlite3_step(pStmt)==SQLITE_ROW ){} | |
| 505 | + rc = sqlite3_finalize(pStmt); | |
| 506 | + if( rc ) db_err("%s: {%.*s}", sqlite3_errmsg(g.db), (int)(zEnd-z), z); | |
| 507 | + } | |
| 508 | + z = zEnd; | |
| 499 | 509 | } |
| 500 | 510 | blob_reset(&sql); |
| 501 | 511 | return rc; |
| 502 | 512 | } |
| 503 | 513 | |
| @@ -642,11 +652,11 @@ | ||
| 642 | 652 | sqlite3 *db; |
| 643 | 653 | int rc; |
| 644 | 654 | const char *zSql; |
| 645 | 655 | va_list ap; |
| 646 | 656 | |
| 647 | - db = openDatabase(zFileName); | |
| 657 | + db = db_open(zFileName); | |
| 648 | 658 | sqlite3_exec(db, "BEGIN EXCLUSIVE", 0, 0, 0); |
| 649 | 659 | rc = sqlite3_exec(db, zSchema, 0, 0, 0); |
| 650 | 660 | if( rc!=SQLITE_OK ){ |
| 651 | 661 | db_err(sqlite3_errmsg(db)); |
| 652 | 662 | } |
| @@ -693,11 +703,11 @@ | ||
| 693 | 703 | |
| 694 | 704 | /* |
| 695 | 705 | ** Open a database file. Return a pointer to the new database |
| 696 | 706 | ** connection. An error results in process abort. |
| 697 | 707 | */ |
| 698 | -LOCAL sqlite3 *openDatabase(const char *zDbName){ | |
| 708 | +LOCAL sqlite3 *db_open(const char *zDbName){ | |
| 699 | 709 | int rc; |
| 700 | 710 | const char *zVfs; |
| 701 | 711 | sqlite3 *db; |
| 702 | 712 | |
| 703 | 713 | if( g.fSqlTrace ) fossil_trace("-- sqlite3_open: [%s]\n", zDbName); |
| @@ -713,10 +723,23 @@ | ||
| 713 | 723 | sqlite3_busy_timeout(db, 5000); |
| 714 | 724 | sqlite3_wal_autocheckpoint(db, 1); /* Set to checkpoint frequently */ |
| 715 | 725 | sqlite3_create_function(db, "now", 0, SQLITE_ANY, 0, db_now_function, 0, 0); |
| 716 | 726 | sqlite3_create_function(db, "checkin_mtime", 2, SQLITE_ANY, 0, |
| 717 | 727 | db_checkin_mtime_function, 0, 0); |
| 728 | + sqlite3_create_function(db, "user", 0, SQLITE_ANY, 0, db_sql_user, 0, 0); | |
| 729 | + sqlite3_create_function(db, "cgi", 1, SQLITE_ANY, 0, db_sql_cgi, 0, 0); | |
| 730 | + sqlite3_create_function(db, "cgi", 2, SQLITE_ANY, 0, db_sql_cgi, 0, 0); | |
| 731 | + sqlite3_create_function(db, "print", -1, SQLITE_UTF8, 0,db_sql_print,0,0); | |
| 732 | + sqlite3_create_function( | |
| 733 | + db, "is_selected", 1, SQLITE_UTF8, 0, file_is_selected,0,0 | |
| 734 | + ); | |
| 735 | + sqlite3_create_function( | |
| 736 | + db, "if_selected", 3, SQLITE_UTF8, 0, file_is_selected,0,0 | |
| 737 | + ); | |
| 738 | + if( g.fSqlTrace ) sqlite3_trace(db, db_sql_trace, 0); | |
| 739 | + re_add_sql_func(db); | |
| 740 | + sqlite3_exec(db, "PRAGMA foreign_keys=OFF;", 0, 0, 0); | |
| 718 | 741 | return db; |
| 719 | 742 | } |
| 720 | 743 | |
| 721 | 744 | |
| 722 | 745 | /* |
| @@ -744,13 +767,12 @@ | ||
| 744 | 767 | const char *zLabel, |
| 745 | 768 | int *pWasAttached |
| 746 | 769 | ){ |
| 747 | 770 | if( !g.db ){ |
| 748 | 771 | assert( g.zMainDbType==0 ); |
| 749 | - g.db = openDatabase(zDbName); | |
| 772 | + g.db = db_open(zDbName); | |
| 750 | 773 | g.zMainDbType = zLabel; |
| 751 | - db_connection_init(); | |
| 752 | 774 | if ( pWasAttached ) *pWasAttached = 0; |
| 753 | 775 | }else{ |
| 754 | 776 | assert( g.zMainDbType!=0 ); |
| 755 | 777 | db_attach(zDbName, zLabel); |
| 756 | 778 | if ( pWasAttached ) *pWasAttached = 1; |
| @@ -817,11 +839,11 @@ | ||
| 817 | 839 | db_open_or_attach(zDbName, "configdb", &g.useAttach); |
| 818 | 840 | g.dbConfig = 0; |
| 819 | 841 | g.zConfigDbType = 0; |
| 820 | 842 | }else{ |
| 821 | 843 | g.useAttach = 0; |
| 822 | - g.dbConfig = openDatabase(zDbName); | |
| 844 | + g.dbConfig = db_open(zDbName); | |
| 823 | 845 | g.zConfigDbType = "configdb"; |
| 824 | 846 | } |
| 825 | 847 | g.configOpen = 1; |
| 826 | 848 | free(zDbName); |
| 827 | 849 | } |
| @@ -1441,11 +1463,11 @@ | ||
| 1441 | 1463 | ** SQL functions for debugging. |
| 1442 | 1464 | ** |
| 1443 | 1465 | ** The print() function writes its arguments on stdout, but only |
| 1444 | 1466 | ** if the -sqlprint command-line option is turned on. |
| 1445 | 1467 | */ |
| 1446 | -static void db_sql_print( | |
| 1468 | +LOCAL void db_sql_print( | |
| 1447 | 1469 | sqlite3_context *context, |
| 1448 | 1470 | int argc, |
| 1449 | 1471 | sqlite3_value **argv |
| 1450 | 1472 | ){ |
| 1451 | 1473 | int i; |
| @@ -1454,20 +1476,20 @@ | ||
| 1454 | 1476 | char c = i==argc-1 ? '\n' : ' '; |
| 1455 | 1477 | fossil_print("%s%c", sqlite3_value_text(argv[i]), c); |
| 1456 | 1478 | } |
| 1457 | 1479 | } |
| 1458 | 1480 | } |
| 1459 | -static void db_sql_trace(void *notUsed, const char *zSql){ | |
| 1481 | +LOCAL void db_sql_trace(void *notUsed, const char *zSql){ | |
| 1460 | 1482 | int n = strlen(zSql); |
| 1461 | 1483 | fossil_trace("%s%s\n", zSql, (n>0 && zSql[n-1]==';') ? "" : ";"); |
| 1462 | 1484 | } |
| 1463 | 1485 | |
| 1464 | 1486 | /* |
| 1465 | 1487 | ** Implement the user() SQL function. user() takes no arguments and |
| 1466 | 1488 | ** returns the user ID of the current user. |
| 1467 | 1489 | */ |
| 1468 | -static void db_sql_user( | |
| 1490 | +LOCAL void db_sql_user( | |
| 1469 | 1491 | sqlite3_context *context, |
| 1470 | 1492 | int argc, |
| 1471 | 1493 | sqlite3_value **argv |
| 1472 | 1494 | ){ |
| 1473 | 1495 | if( g.zLogin!=0 ){ |
| @@ -1479,11 +1501,11 @@ | ||
| 1479 | 1501 | ** Implement the cgi() SQL function. cgi() takes an argument which is |
| 1480 | 1502 | ** a name of CGI query parameter. The value of that parameter is returned, |
| 1481 | 1503 | ** if available. Optional second argument will be returned if the first |
| 1482 | 1504 | ** doesn't exist as a CGI parameter. |
| 1483 | 1505 | */ |
| 1484 | -static void db_sql_cgi(sqlite3_context *context, int argc, sqlite3_value **argv){ | |
| 1506 | +LOCAL void db_sql_cgi(sqlite3_context *context, int argc, sqlite3_value **argv){ | |
| 1485 | 1507 | const char* zP; |
| 1486 | 1508 | if( argc!=1 && argc!=2 ) return; |
| 1487 | 1509 | zP = P((const char*)sqlite3_value_text(argv[0])); |
| 1488 | 1510 | if( zP ){ |
| 1489 | 1511 | sqlite3_result_text(context, zP, -1, SQLITE_STATIC); |
| @@ -1510,11 +1532,11 @@ | ||
| 1510 | 1532 | ** (meaning that id was named on the command-line). |
| 1511 | 1533 | ** |
| 1512 | 1534 | ** In the second form (3 arguments) return argument X if true and Y |
| 1513 | 1535 | ** if false. Except if Y is NULL then always return X. |
| 1514 | 1536 | */ |
| 1515 | -static void file_is_selected( | |
| 1537 | +LOCAL void file_is_selected( | |
| 1516 | 1538 | sqlite3_context *context, |
| 1517 | 1539 | int argc, |
| 1518 | 1540 | sqlite3_value **argv |
| 1519 | 1541 | ){ |
| 1520 | 1542 | int rc = 0; |
| @@ -1598,32 +1620,10 @@ | ||
| 1598 | 1620 | zOut = mprintf("%s", zKey); |
| 1599 | 1621 | } |
| 1600 | 1622 | return zOut; |
| 1601 | 1623 | } |
| 1602 | 1624 | |
| 1603 | -/* | |
| 1604 | -** This function registers auxiliary functions when the SQLite | |
| 1605 | -** database connection is first established. | |
| 1606 | -*/ | |
| 1607 | -void db_connection_init(void){ | |
| 1608 | - sqlite3_exec(g.db, "PRAGMA foreign_keys=OFF;", 0, 0, 0); | |
| 1609 | - sqlite3_create_function(g.db, "user", 0, SQLITE_ANY, 0, db_sql_user, 0, 0); | |
| 1610 | - sqlite3_create_function(g.db, "cgi", 1, SQLITE_ANY, 0, db_sql_cgi, 0, 0); | |
| 1611 | - sqlite3_create_function(g.db, "cgi", 2, SQLITE_ANY, 0, db_sql_cgi, 0, 0); | |
| 1612 | - sqlite3_create_function(g.db, "print", -1, SQLITE_UTF8, 0,db_sql_print,0,0); | |
| 1613 | - sqlite3_create_function( | |
| 1614 | - g.db, "is_selected", 1, SQLITE_UTF8, 0, file_is_selected,0,0 | |
| 1615 | - ); | |
| 1616 | - sqlite3_create_function( | |
| 1617 | - g.db, "if_selected", 3, SQLITE_UTF8, 0, file_is_selected,0,0 | |
| 1618 | - ); | |
| 1619 | - if( g.fSqlTrace ){ | |
| 1620 | - sqlite3_trace(g.db, db_sql_trace, 0); | |
| 1621 | - } | |
| 1622 | - re_add_sql_func(g.db); | |
| 1623 | -} | |
| 1624 | - | |
| 1625 | 1625 | /* |
| 1626 | 1626 | ** Return true if the string zVal represents "true" (or "false"). |
| 1627 | 1627 | */ |
| 1628 | 1628 | int is_truth(const char *zVal){ |
| 1629 | 1629 | static const char *const azOn[] = { "on", "yes", "true", "1" }; |
| 1630 | 1630 |
| --- src/db.c | |
| +++ src/db.c | |
| @@ -484,20 +484,30 @@ | |
| 484 | /* |
| 485 | ** Execute multiple SQL statements. |
| 486 | */ |
| 487 | int db_multi_exec(const char *zSql, ...){ |
| 488 | Blob sql; |
| 489 | int rc; |
| 490 | va_list ap; |
| 491 | char *zErr = 0; |
| 492 | blob_init(&sql, 0, 0); |
| 493 | va_start(ap, zSql); |
| 494 | blob_vappendf(&sql, zSql, ap); |
| 495 | va_end(ap); |
| 496 | rc = sqlite3_exec(g.db, blob_buffer(&sql), 0, 0, &zErr); |
| 497 | if( rc!=SQLITE_OK ){ |
| 498 | db_err("%s\n%s", zErr, blob_buffer(&sql)); |
| 499 | } |
| 500 | blob_reset(&sql); |
| 501 | return rc; |
| 502 | } |
| 503 | |
| @@ -642,11 +652,11 @@ | |
| 642 | sqlite3 *db; |
| 643 | int rc; |
| 644 | const char *zSql; |
| 645 | va_list ap; |
| 646 | |
| 647 | db = openDatabase(zFileName); |
| 648 | sqlite3_exec(db, "BEGIN EXCLUSIVE", 0, 0, 0); |
| 649 | rc = sqlite3_exec(db, zSchema, 0, 0, 0); |
| 650 | if( rc!=SQLITE_OK ){ |
| 651 | db_err(sqlite3_errmsg(db)); |
| 652 | } |
| @@ -693,11 +703,11 @@ | |
| 693 | |
| 694 | /* |
| 695 | ** Open a database file. Return a pointer to the new database |
| 696 | ** connection. An error results in process abort. |
| 697 | */ |
| 698 | LOCAL sqlite3 *openDatabase(const char *zDbName){ |
| 699 | int rc; |
| 700 | const char *zVfs; |
| 701 | sqlite3 *db; |
| 702 | |
| 703 | if( g.fSqlTrace ) fossil_trace("-- sqlite3_open: [%s]\n", zDbName); |
| @@ -713,10 +723,23 @@ | |
| 713 | sqlite3_busy_timeout(db, 5000); |
| 714 | sqlite3_wal_autocheckpoint(db, 1); /* Set to checkpoint frequently */ |
| 715 | sqlite3_create_function(db, "now", 0, SQLITE_ANY, 0, db_now_function, 0, 0); |
| 716 | sqlite3_create_function(db, "checkin_mtime", 2, SQLITE_ANY, 0, |
| 717 | db_checkin_mtime_function, 0, 0); |
| 718 | return db; |
| 719 | } |
| 720 | |
| 721 | |
| 722 | /* |
| @@ -744,13 +767,12 @@ | |
| 744 | const char *zLabel, |
| 745 | int *pWasAttached |
| 746 | ){ |
| 747 | if( !g.db ){ |
| 748 | assert( g.zMainDbType==0 ); |
| 749 | g.db = openDatabase(zDbName); |
| 750 | g.zMainDbType = zLabel; |
| 751 | db_connection_init(); |
| 752 | if ( pWasAttached ) *pWasAttached = 0; |
| 753 | }else{ |
| 754 | assert( g.zMainDbType!=0 ); |
| 755 | db_attach(zDbName, zLabel); |
| 756 | if ( pWasAttached ) *pWasAttached = 1; |
| @@ -817,11 +839,11 @@ | |
| 817 | db_open_or_attach(zDbName, "configdb", &g.useAttach); |
| 818 | g.dbConfig = 0; |
| 819 | g.zConfigDbType = 0; |
| 820 | }else{ |
| 821 | g.useAttach = 0; |
| 822 | g.dbConfig = openDatabase(zDbName); |
| 823 | g.zConfigDbType = "configdb"; |
| 824 | } |
| 825 | g.configOpen = 1; |
| 826 | free(zDbName); |
| 827 | } |
| @@ -1441,11 +1463,11 @@ | |
| 1441 | ** SQL functions for debugging. |
| 1442 | ** |
| 1443 | ** The print() function writes its arguments on stdout, but only |
| 1444 | ** if the -sqlprint command-line option is turned on. |
| 1445 | */ |
| 1446 | static void db_sql_print( |
| 1447 | sqlite3_context *context, |
| 1448 | int argc, |
| 1449 | sqlite3_value **argv |
| 1450 | ){ |
| 1451 | int i; |
| @@ -1454,20 +1476,20 @@ | |
| 1454 | char c = i==argc-1 ? '\n' : ' '; |
| 1455 | fossil_print("%s%c", sqlite3_value_text(argv[i]), c); |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | static void db_sql_trace(void *notUsed, const char *zSql){ |
| 1460 | int n = strlen(zSql); |
| 1461 | fossil_trace("%s%s\n", zSql, (n>0 && zSql[n-1]==';') ? "" : ";"); |
| 1462 | } |
| 1463 | |
| 1464 | /* |
| 1465 | ** Implement the user() SQL function. user() takes no arguments and |
| 1466 | ** returns the user ID of the current user. |
| 1467 | */ |
| 1468 | static void db_sql_user( |
| 1469 | sqlite3_context *context, |
| 1470 | int argc, |
| 1471 | sqlite3_value **argv |
| 1472 | ){ |
| 1473 | if( g.zLogin!=0 ){ |
| @@ -1479,11 +1501,11 @@ | |
| 1479 | ** Implement the cgi() SQL function. cgi() takes an argument which is |
| 1480 | ** a name of CGI query parameter. The value of that parameter is returned, |
| 1481 | ** if available. Optional second argument will be returned if the first |
| 1482 | ** doesn't exist as a CGI parameter. |
| 1483 | */ |
| 1484 | static void db_sql_cgi(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 1485 | const char* zP; |
| 1486 | if( argc!=1 && argc!=2 ) return; |
| 1487 | zP = P((const char*)sqlite3_value_text(argv[0])); |
| 1488 | if( zP ){ |
| 1489 | sqlite3_result_text(context, zP, -1, SQLITE_STATIC); |
| @@ -1510,11 +1532,11 @@ | |
| 1510 | ** (meaning that id was named on the command-line). |
| 1511 | ** |
| 1512 | ** In the second form (3 arguments) return argument X if true and Y |
| 1513 | ** if false. Except if Y is NULL then always return X. |
| 1514 | */ |
| 1515 | static void file_is_selected( |
| 1516 | sqlite3_context *context, |
| 1517 | int argc, |
| 1518 | sqlite3_value **argv |
| 1519 | ){ |
| 1520 | int rc = 0; |
| @@ -1598,32 +1620,10 @@ | |
| 1598 | zOut = mprintf("%s", zKey); |
| 1599 | } |
| 1600 | return zOut; |
| 1601 | } |
| 1602 | |
| 1603 | /* |
| 1604 | ** This function registers auxiliary functions when the SQLite |
| 1605 | ** database connection is first established. |
| 1606 | */ |
| 1607 | void db_connection_init(void){ |
| 1608 | sqlite3_exec(g.db, "PRAGMA foreign_keys=OFF;", 0, 0, 0); |
| 1609 | sqlite3_create_function(g.db, "user", 0, SQLITE_ANY, 0, db_sql_user, 0, 0); |
| 1610 | sqlite3_create_function(g.db, "cgi", 1, SQLITE_ANY, 0, db_sql_cgi, 0, 0); |
| 1611 | sqlite3_create_function(g.db, "cgi", 2, SQLITE_ANY, 0, db_sql_cgi, 0, 0); |
| 1612 | sqlite3_create_function(g.db, "print", -1, SQLITE_UTF8, 0,db_sql_print,0,0); |
| 1613 | sqlite3_create_function( |
| 1614 | g.db, "is_selected", 1, SQLITE_UTF8, 0, file_is_selected,0,0 |
| 1615 | ); |
| 1616 | sqlite3_create_function( |
| 1617 | g.db, "if_selected", 3, SQLITE_UTF8, 0, file_is_selected,0,0 |
| 1618 | ); |
| 1619 | if( g.fSqlTrace ){ |
| 1620 | sqlite3_trace(g.db, db_sql_trace, 0); |
| 1621 | } |
| 1622 | re_add_sql_func(g.db); |
| 1623 | } |
| 1624 | |
| 1625 | /* |
| 1626 | ** Return true if the string zVal represents "true" (or "false"). |
| 1627 | */ |
| 1628 | int is_truth(const char *zVal){ |
| 1629 | static const char *const azOn[] = { "on", "yes", "true", "1" }; |
| 1630 |
| --- src/db.c | |
| +++ src/db.c | |
| @@ -484,20 +484,30 @@ | |
| 484 | /* |
| 485 | ** Execute multiple SQL statements. |
| 486 | */ |
| 487 | int db_multi_exec(const char *zSql, ...){ |
| 488 | Blob sql; |
| 489 | int rc = SQLITE_OK; |
| 490 | va_list ap; |
| 491 | const char *z, *zEnd; |
| 492 | sqlite3_stmt *pStmt; |
| 493 | blob_init(&sql, 0, 0); |
| 494 | va_start(ap, zSql); |
| 495 | blob_vappendf(&sql, zSql, ap); |
| 496 | va_end(ap); |
| 497 | z = blob_str(&sql); |
| 498 | while( rc==SQLITE_OK && z[0] ){ |
| 499 | pStmt = 0; |
| 500 | rc = sqlite3_prepare_v2(g.db, z, -1, &pStmt, &zEnd); |
| 501 | if( rc!=SQLITE_OK ) break; |
| 502 | if( pStmt ){ |
| 503 | db.nPrepare++; |
| 504 | while( sqlite3_step(pStmt)==SQLITE_ROW ){} |
| 505 | rc = sqlite3_finalize(pStmt); |
| 506 | if( rc ) db_err("%s: {%.*s}", sqlite3_errmsg(g.db), (int)(zEnd-z), z); |
| 507 | } |
| 508 | z = zEnd; |
| 509 | } |
| 510 | blob_reset(&sql); |
| 511 | return rc; |
| 512 | } |
| 513 | |
| @@ -642,11 +652,11 @@ | |
| 652 | sqlite3 *db; |
| 653 | int rc; |
| 654 | const char *zSql; |
| 655 | va_list ap; |
| 656 | |
| 657 | db = db_open(zFileName); |
| 658 | sqlite3_exec(db, "BEGIN EXCLUSIVE", 0, 0, 0); |
| 659 | rc = sqlite3_exec(db, zSchema, 0, 0, 0); |
| 660 | if( rc!=SQLITE_OK ){ |
| 661 | db_err(sqlite3_errmsg(db)); |
| 662 | } |
| @@ -693,11 +703,11 @@ | |
| 703 | |
| 704 | /* |
| 705 | ** Open a database file. Return a pointer to the new database |
| 706 | ** connection. An error results in process abort. |
| 707 | */ |
| 708 | LOCAL sqlite3 *db_open(const char *zDbName){ |
| 709 | int rc; |
| 710 | const char *zVfs; |
| 711 | sqlite3 *db; |
| 712 | |
| 713 | if( g.fSqlTrace ) fossil_trace("-- sqlite3_open: [%s]\n", zDbName); |
| @@ -713,10 +723,23 @@ | |
| 723 | sqlite3_busy_timeout(db, 5000); |
| 724 | sqlite3_wal_autocheckpoint(db, 1); /* Set to checkpoint frequently */ |
| 725 | sqlite3_create_function(db, "now", 0, SQLITE_ANY, 0, db_now_function, 0, 0); |
| 726 | sqlite3_create_function(db, "checkin_mtime", 2, SQLITE_ANY, 0, |
| 727 | db_checkin_mtime_function, 0, 0); |
| 728 | sqlite3_create_function(db, "user", 0, SQLITE_ANY, 0, db_sql_user, 0, 0); |
| 729 | sqlite3_create_function(db, "cgi", 1, SQLITE_ANY, 0, db_sql_cgi, 0, 0); |
| 730 | sqlite3_create_function(db, "cgi", 2, SQLITE_ANY, 0, db_sql_cgi, 0, 0); |
| 731 | sqlite3_create_function(db, "print", -1, SQLITE_UTF8, 0,db_sql_print,0,0); |
| 732 | sqlite3_create_function( |
| 733 | db, "is_selected", 1, SQLITE_UTF8, 0, file_is_selected,0,0 |
| 734 | ); |
| 735 | sqlite3_create_function( |
| 736 | db, "if_selected", 3, SQLITE_UTF8, 0, file_is_selected,0,0 |
| 737 | ); |
| 738 | if( g.fSqlTrace ) sqlite3_trace(db, db_sql_trace, 0); |
| 739 | re_add_sql_func(db); |
| 740 | sqlite3_exec(db, "PRAGMA foreign_keys=OFF;", 0, 0, 0); |
| 741 | return db; |
| 742 | } |
| 743 | |
| 744 | |
| 745 | /* |
| @@ -744,13 +767,12 @@ | |
| 767 | const char *zLabel, |
| 768 | int *pWasAttached |
| 769 | ){ |
| 770 | if( !g.db ){ |
| 771 | assert( g.zMainDbType==0 ); |
| 772 | g.db = db_open(zDbName); |
| 773 | g.zMainDbType = zLabel; |
| 774 | if ( pWasAttached ) *pWasAttached = 0; |
| 775 | }else{ |
| 776 | assert( g.zMainDbType!=0 ); |
| 777 | db_attach(zDbName, zLabel); |
| 778 | if ( pWasAttached ) *pWasAttached = 1; |
| @@ -817,11 +839,11 @@ | |
| 839 | db_open_or_attach(zDbName, "configdb", &g.useAttach); |
| 840 | g.dbConfig = 0; |
| 841 | g.zConfigDbType = 0; |
| 842 | }else{ |
| 843 | g.useAttach = 0; |
| 844 | g.dbConfig = db_open(zDbName); |
| 845 | g.zConfigDbType = "configdb"; |
| 846 | } |
| 847 | g.configOpen = 1; |
| 848 | free(zDbName); |
| 849 | } |
| @@ -1441,11 +1463,11 @@ | |
| 1463 | ** SQL functions for debugging. |
| 1464 | ** |
| 1465 | ** The print() function writes its arguments on stdout, but only |
| 1466 | ** if the -sqlprint command-line option is turned on. |
| 1467 | */ |
| 1468 | LOCAL void db_sql_print( |
| 1469 | sqlite3_context *context, |
| 1470 | int argc, |
| 1471 | sqlite3_value **argv |
| 1472 | ){ |
| 1473 | int i; |
| @@ -1454,20 +1476,20 @@ | |
| 1476 | char c = i==argc-1 ? '\n' : ' '; |
| 1477 | fossil_print("%s%c", sqlite3_value_text(argv[i]), c); |
| 1478 | } |
| 1479 | } |
| 1480 | } |
| 1481 | LOCAL void db_sql_trace(void *notUsed, const char *zSql){ |
| 1482 | int n = strlen(zSql); |
| 1483 | fossil_trace("%s%s\n", zSql, (n>0 && zSql[n-1]==';') ? "" : ";"); |
| 1484 | } |
| 1485 | |
| 1486 | /* |
| 1487 | ** Implement the user() SQL function. user() takes no arguments and |
| 1488 | ** returns the user ID of the current user. |
| 1489 | */ |
| 1490 | LOCAL void db_sql_user( |
| 1491 | sqlite3_context *context, |
| 1492 | int argc, |
| 1493 | sqlite3_value **argv |
| 1494 | ){ |
| 1495 | if( g.zLogin!=0 ){ |
| @@ -1479,11 +1501,11 @@ | |
| 1501 | ** Implement the cgi() SQL function. cgi() takes an argument which is |
| 1502 | ** a name of CGI query parameter. The value of that parameter is returned, |
| 1503 | ** if available. Optional second argument will be returned if the first |
| 1504 | ** doesn't exist as a CGI parameter. |
| 1505 | */ |
| 1506 | LOCAL void db_sql_cgi(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 1507 | const char* zP; |
| 1508 | if( argc!=1 && argc!=2 ) return; |
| 1509 | zP = P((const char*)sqlite3_value_text(argv[0])); |
| 1510 | if( zP ){ |
| 1511 | sqlite3_result_text(context, zP, -1, SQLITE_STATIC); |
| @@ -1510,11 +1532,11 @@ | |
| 1532 | ** (meaning that id was named on the command-line). |
| 1533 | ** |
| 1534 | ** In the second form (3 arguments) return argument X if true and Y |
| 1535 | ** if false. Except if Y is NULL then always return X. |
| 1536 | */ |
| 1537 | LOCAL void file_is_selected( |
| 1538 | sqlite3_context *context, |
| 1539 | int argc, |
| 1540 | sqlite3_value **argv |
| 1541 | ){ |
| 1542 | int rc = 0; |
| @@ -1598,32 +1620,10 @@ | |
| 1620 | zOut = mprintf("%s", zKey); |
| 1621 | } |
| 1622 | return zOut; |
| 1623 | } |
| 1624 | |
| 1625 | /* |
| 1626 | ** Return true if the string zVal represents "true" (or "false"). |
| 1627 | */ |
| 1628 | int is_truth(const char *zVal){ |
| 1629 | static const char *const azOn[] = { "on", "yes", "true", "1" }; |
| 1630 |