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.

drh 2013-01-18 00:47 trunk
Commit f97e1cf6662be46c53fd16416d4030b310bedc69
1 file changed +37 -37
+37 -37
--- src/db.c
+++ src/db.c
@@ -484,20 +484,30 @@
484484
/*
485485
** Execute multiple SQL statements.
486486
*/
487487
int db_multi_exec(const char *zSql, ...){
488488
Blob sql;
489
- int rc;
489
+ int rc = SQLITE_OK;
490490
va_list ap;
491
- char *zErr = 0;
491
+ const char *z, *zEnd;
492
+ sqlite3_stmt *pStmt;
492493
blob_init(&sql, 0, 0);
493494
va_start(ap, zSql);
494495
blob_vappendf(&sql, zSql, ap);
495496
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;
499509
}
500510
blob_reset(&sql);
501511
return rc;
502512
}
503513
@@ -642,11 +652,11 @@
642652
sqlite3 *db;
643653
int rc;
644654
const char *zSql;
645655
va_list ap;
646656
647
- db = openDatabase(zFileName);
657
+ db = db_open(zFileName);
648658
sqlite3_exec(db, "BEGIN EXCLUSIVE", 0, 0, 0);
649659
rc = sqlite3_exec(db, zSchema, 0, 0, 0);
650660
if( rc!=SQLITE_OK ){
651661
db_err(sqlite3_errmsg(db));
652662
}
@@ -693,11 +703,11 @@
693703
694704
/*
695705
** Open a database file. Return a pointer to the new database
696706
** connection. An error results in process abort.
697707
*/
698
-LOCAL sqlite3 *openDatabase(const char *zDbName){
708
+LOCAL sqlite3 *db_open(const char *zDbName){
699709
int rc;
700710
const char *zVfs;
701711
sqlite3 *db;
702712
703713
if( g.fSqlTrace ) fossil_trace("-- sqlite3_open: [%s]\n", zDbName);
@@ -713,10 +723,23 @@
713723
sqlite3_busy_timeout(db, 5000);
714724
sqlite3_wal_autocheckpoint(db, 1); /* Set to checkpoint frequently */
715725
sqlite3_create_function(db, "now", 0, SQLITE_ANY, 0, db_now_function, 0, 0);
716726
sqlite3_create_function(db, "checkin_mtime", 2, SQLITE_ANY, 0,
717727
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);
718741
return db;
719742
}
720743
721744
722745
/*
@@ -744,13 +767,12 @@
744767
const char *zLabel,
745768
int *pWasAttached
746769
){
747770
if( !g.db ){
748771
assert( g.zMainDbType==0 );
749
- g.db = openDatabase(zDbName);
772
+ g.db = db_open(zDbName);
750773
g.zMainDbType = zLabel;
751
- db_connection_init();
752774
if ( pWasAttached ) *pWasAttached = 0;
753775
}else{
754776
assert( g.zMainDbType!=0 );
755777
db_attach(zDbName, zLabel);
756778
if ( pWasAttached ) *pWasAttached = 1;
@@ -817,11 +839,11 @@
817839
db_open_or_attach(zDbName, "configdb", &g.useAttach);
818840
g.dbConfig = 0;
819841
g.zConfigDbType = 0;
820842
}else{
821843
g.useAttach = 0;
822
- g.dbConfig = openDatabase(zDbName);
844
+ g.dbConfig = db_open(zDbName);
823845
g.zConfigDbType = "configdb";
824846
}
825847
g.configOpen = 1;
826848
free(zDbName);
827849
}
@@ -1441,11 +1463,11 @@
14411463
** SQL functions for debugging.
14421464
**
14431465
** The print() function writes its arguments on stdout, but only
14441466
** if the -sqlprint command-line option is turned on.
14451467
*/
1446
-static void db_sql_print(
1468
+LOCAL void db_sql_print(
14471469
sqlite3_context *context,
14481470
int argc,
14491471
sqlite3_value **argv
14501472
){
14511473
int i;
@@ -1454,20 +1476,20 @@
14541476
char c = i==argc-1 ? '\n' : ' ';
14551477
fossil_print("%s%c", sqlite3_value_text(argv[i]), c);
14561478
}
14571479
}
14581480
}
1459
-static void db_sql_trace(void *notUsed, const char *zSql){
1481
+LOCAL void db_sql_trace(void *notUsed, const char *zSql){
14601482
int n = strlen(zSql);
14611483
fossil_trace("%s%s\n", zSql, (n>0 && zSql[n-1]==';') ? "" : ";");
14621484
}
14631485
14641486
/*
14651487
** Implement the user() SQL function. user() takes no arguments and
14661488
** returns the user ID of the current user.
14671489
*/
1468
-static void db_sql_user(
1490
+LOCAL void db_sql_user(
14691491
sqlite3_context *context,
14701492
int argc,
14711493
sqlite3_value **argv
14721494
){
14731495
if( g.zLogin!=0 ){
@@ -1479,11 +1501,11 @@
14791501
** Implement the cgi() SQL function. cgi() takes an argument which is
14801502
** a name of CGI query parameter. The value of that parameter is returned,
14811503
** if available. Optional second argument will be returned if the first
14821504
** doesn't exist as a CGI parameter.
14831505
*/
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){
14851507
const char* zP;
14861508
if( argc!=1 && argc!=2 ) return;
14871509
zP = P((const char*)sqlite3_value_text(argv[0]));
14881510
if( zP ){
14891511
sqlite3_result_text(context, zP, -1, SQLITE_STATIC);
@@ -1510,11 +1532,11 @@
15101532
** (meaning that id was named on the command-line).
15111533
**
15121534
** In the second form (3 arguments) return argument X if true and Y
15131535
** if false. Except if Y is NULL then always return X.
15141536
*/
1515
-static void file_is_selected(
1537
+LOCAL void file_is_selected(
15161538
sqlite3_context *context,
15171539
int argc,
15181540
sqlite3_value **argv
15191541
){
15201542
int rc = 0;
@@ -1598,32 +1620,10 @@
15981620
zOut = mprintf("%s", zKey);
15991621
}
16001622
return zOut;
16011623
}
16021624
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
-
16251625
/*
16261626
** Return true if the string zVal represents "true" (or "false").
16271627
*/
16281628
int is_truth(const char *zVal){
16291629
static const char *const azOn[] = { "on", "yes", "true", "1" };
16301630
--- 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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button