Fossil SCM

Improve error handling in "fossil sqlite" command ".tables". Cherry-pick from SQLite trunk.

jan.nijtmans 2015-10-02 10:37 trunk
Commit 5c9aecae2149c164727d2b904d91facd2419b940
1 file changed +45 -10
+45 -10
--- src/shell.c
+++ src/shell.c
@@ -2610,10 +2610,26 @@
26102610
}
26112611
sqlite3_free(zSchemaTab);
26122612
return 0;
26132613
}
26142614
2615
+/*
2616
+** Print the current sqlite3_errmsg() value to stderr and return 1.
2617
+*/
2618
+static int shellDatabaseError(sqlite3 *db){
2619
+ const char *zErr = sqlite3_errmsg(db);
2620
+ fprintf(stderr, "Error: %s\n", zErr);
2621
+ return 1;
2622
+}
2623
+
2624
+/*
2625
+** Print an out-of-memory message to stderr and return 1.
2626
+*/
2627
+static int shellNomemError(void){
2628
+ fprintf(stderr, "Error: out of memory\n");
2629
+ return 1;
2630
+}
26152631
26162632
/*
26172633
** If an input line begins with "." then invoke this routine to
26182634
** process that line.
26192635
**
@@ -3711,17 +3727,21 @@
37113727
int nRow, nAlloc;
37123728
char *zSql = 0;
37133729
int ii;
37143730
open_db(p, 0);
37153731
rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
3716
- if( rc ) return rc;
3732
+ if( rc ) return shellDatabaseError(p->db);
3733
+
3734
+ /* Create an SQL statement to query for the list of tables in the
3735
+ ** main and all attached databases where the table name matches the
3736
+ ** LIKE pattern bound to variable "?1". */
37173737
zSql = sqlite3_mprintf(
37183738
"SELECT name FROM sqlite_master"
37193739
" WHERE type IN ('table','view')"
37203740
" AND name NOT LIKE 'sqlite_%%'"
37213741
" AND name LIKE ?1");
3722
- while( sqlite3_step(pStmt)==SQLITE_ROW ){
3742
+ while( zSql && sqlite3_step(pStmt)==SQLITE_ROW ){
37233743
const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
37243744
if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue;
37253745
if( strcmp(zDbName,"temp")==0 ){
37263746
zSql = sqlite3_mprintf(
37273747
"%z UNION ALL "
@@ -3736,15 +3756,21 @@
37363756
" WHERE type IN ('table','view')"
37373757
" AND name NOT LIKE 'sqlite_%%'"
37383758
" AND name LIKE ?1", zSql, zDbName, zDbName);
37393759
}
37403760
}
3741
- sqlite3_finalize(pStmt);
3742
- zSql = sqlite3_mprintf("%z ORDER BY 1", zSql);
3743
- rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3761
+ rc = sqlite3_finalize(pStmt);
3762
+ if( zSql && rc==SQLITE_OK ){
3763
+ zSql = sqlite3_mprintf("%z ORDER BY 1", zSql);
3764
+ if( zSql ) rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3765
+ }
37443766
sqlite3_free(zSql);
3745
- if( rc ) return rc;
3767
+ if( !zSql ) return shellNomemError();
3768
+ if( rc ) return shellDatabaseError(p->db);
3769
+
3770
+ /* Run the SQL statement prepared by the above block. Store the results
3771
+ ** as an array of nul-terminated strings in azResult[]. */
37463772
nRow = nAlloc = 0;
37473773
azResult = 0;
37483774
if( nArg>1 ){
37493775
sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
37503776
}else{
@@ -3754,21 +3780,29 @@
37543780
if( nRow>=nAlloc ){
37553781
char **azNew;
37563782
int n2 = nAlloc*2 + 10;
37573783
azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
37583784
if( azNew==0 ){
3759
- fprintf(stderr, "Error: out of memory\n");
3785
+ rc = shellNomemError();
37603786
break;
37613787
}
37623788
nAlloc = n2;
37633789
azResult = azNew;
37643790
}
37653791
azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
3766
- if( azResult[nRow] ) nRow++;
3792
+ if( 0==azResult[nRow] ){
3793
+ rc = shellNomemError();
3794
+ break;
3795
+ }
3796
+ nRow++;
3797
+ }
3798
+ if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
3799
+ rc = shellDatabaseError(p->db);
37673800
}
3768
- sqlite3_finalize(pStmt);
3769
- if( nRow>0 ){
3801
+
3802
+ /* Pretty-print the contents of array azResult[] to the output */
3803
+ if( rc==0 && nRow>0 ){
37703804
int len, maxlen = 0;
37713805
int i, j;
37723806
int nPrintCol, nPrintRow;
37733807
for(i=0; i<nRow; i++){
37743808
len = strlen30(azResult[i]);
@@ -3783,10 +3817,11 @@
37833817
fprintf(p->out, "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
37843818
}
37853819
fprintf(p->out, "\n");
37863820
}
37873821
}
3822
+
37883823
for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
37893824
sqlite3_free(azResult);
37903825
}else
37913826
37923827
if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
37933828
--- src/shell.c
+++ src/shell.c
@@ -2610,10 +2610,26 @@
2610 }
2611 sqlite3_free(zSchemaTab);
2612 return 0;
2613 }
2614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2615
2616 /*
2617 ** If an input line begins with "." then invoke this routine to
2618 ** process that line.
2619 **
@@ -3711,17 +3727,21 @@
3711 int nRow, nAlloc;
3712 char *zSql = 0;
3713 int ii;
3714 open_db(p, 0);
3715 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
3716 if( rc ) return rc;
 
 
 
 
3717 zSql = sqlite3_mprintf(
3718 "SELECT name FROM sqlite_master"
3719 " WHERE type IN ('table','view')"
3720 " AND name NOT LIKE 'sqlite_%%'"
3721 " AND name LIKE ?1");
3722 while( sqlite3_step(pStmt)==SQLITE_ROW ){
3723 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
3724 if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue;
3725 if( strcmp(zDbName,"temp")==0 ){
3726 zSql = sqlite3_mprintf(
3727 "%z UNION ALL "
@@ -3736,15 +3756,21 @@
3736 " WHERE type IN ('table','view')"
3737 " AND name NOT LIKE 'sqlite_%%'"
3738 " AND name LIKE ?1", zSql, zDbName, zDbName);
3739 }
3740 }
3741 sqlite3_finalize(pStmt);
3742 zSql = sqlite3_mprintf("%z ORDER BY 1", zSql);
3743 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
 
 
3744 sqlite3_free(zSql);
3745 if( rc ) return rc;
 
 
 
 
3746 nRow = nAlloc = 0;
3747 azResult = 0;
3748 if( nArg>1 ){
3749 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
3750 }else{
@@ -3754,21 +3780,29 @@
3754 if( nRow>=nAlloc ){
3755 char **azNew;
3756 int n2 = nAlloc*2 + 10;
3757 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
3758 if( azNew==0 ){
3759 fprintf(stderr, "Error: out of memory\n");
3760 break;
3761 }
3762 nAlloc = n2;
3763 azResult = azNew;
3764 }
3765 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
3766 if( azResult[nRow] ) nRow++;
 
 
 
 
 
 
 
3767 }
3768 sqlite3_finalize(pStmt);
3769 if( nRow>0 ){
 
3770 int len, maxlen = 0;
3771 int i, j;
3772 int nPrintCol, nPrintRow;
3773 for(i=0; i<nRow; i++){
3774 len = strlen30(azResult[i]);
@@ -3783,10 +3817,11 @@
3783 fprintf(p->out, "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
3784 }
3785 fprintf(p->out, "\n");
3786 }
3787 }
 
3788 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
3789 sqlite3_free(azResult);
3790 }else
3791
3792 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
3793
--- src/shell.c
+++ src/shell.c
@@ -2610,10 +2610,26 @@
2610 }
2611 sqlite3_free(zSchemaTab);
2612 return 0;
2613 }
2614
2615 /*
2616 ** Print the current sqlite3_errmsg() value to stderr and return 1.
2617 */
2618 static int shellDatabaseError(sqlite3 *db){
2619 const char *zErr = sqlite3_errmsg(db);
2620 fprintf(stderr, "Error: %s\n", zErr);
2621 return 1;
2622 }
2623
2624 /*
2625 ** Print an out-of-memory message to stderr and return 1.
2626 */
2627 static int shellNomemError(void){
2628 fprintf(stderr, "Error: out of memory\n");
2629 return 1;
2630 }
2631
2632 /*
2633 ** If an input line begins with "." then invoke this routine to
2634 ** process that line.
2635 **
@@ -3711,17 +3727,21 @@
3727 int nRow, nAlloc;
3728 char *zSql = 0;
3729 int ii;
3730 open_db(p, 0);
3731 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
3732 if( rc ) return shellDatabaseError(p->db);
3733
3734 /* Create an SQL statement to query for the list of tables in the
3735 ** main and all attached databases where the table name matches the
3736 ** LIKE pattern bound to variable "?1". */
3737 zSql = sqlite3_mprintf(
3738 "SELECT name FROM sqlite_master"
3739 " WHERE type IN ('table','view')"
3740 " AND name NOT LIKE 'sqlite_%%'"
3741 " AND name LIKE ?1");
3742 while( zSql && sqlite3_step(pStmt)==SQLITE_ROW ){
3743 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
3744 if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue;
3745 if( strcmp(zDbName,"temp")==0 ){
3746 zSql = sqlite3_mprintf(
3747 "%z UNION ALL "
@@ -3736,15 +3756,21 @@
3756 " WHERE type IN ('table','view')"
3757 " AND name NOT LIKE 'sqlite_%%'"
3758 " AND name LIKE ?1", zSql, zDbName, zDbName);
3759 }
3760 }
3761 rc = sqlite3_finalize(pStmt);
3762 if( zSql && rc==SQLITE_OK ){
3763 zSql = sqlite3_mprintf("%z ORDER BY 1", zSql);
3764 if( zSql ) rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3765 }
3766 sqlite3_free(zSql);
3767 if( !zSql ) return shellNomemError();
3768 if( rc ) return shellDatabaseError(p->db);
3769
3770 /* Run the SQL statement prepared by the above block. Store the results
3771 ** as an array of nul-terminated strings in azResult[]. */
3772 nRow = nAlloc = 0;
3773 azResult = 0;
3774 if( nArg>1 ){
3775 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
3776 }else{
@@ -3754,21 +3780,29 @@
3780 if( nRow>=nAlloc ){
3781 char **azNew;
3782 int n2 = nAlloc*2 + 10;
3783 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
3784 if( azNew==0 ){
3785 rc = shellNomemError();
3786 break;
3787 }
3788 nAlloc = n2;
3789 azResult = azNew;
3790 }
3791 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
3792 if( 0==azResult[nRow] ){
3793 rc = shellNomemError();
3794 break;
3795 }
3796 nRow++;
3797 }
3798 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
3799 rc = shellDatabaseError(p->db);
3800 }
3801
3802 /* Pretty-print the contents of array azResult[] to the output */
3803 if( rc==0 && nRow>0 ){
3804 int len, maxlen = 0;
3805 int i, j;
3806 int nPrintCol, nPrintRow;
3807 for(i=0; i<nRow; i++){
3808 len = strlen30(azResult[i]);
@@ -3783,10 +3817,11 @@
3817 fprintf(p->out, "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
3818 }
3819 fprintf(p->out, "\n");
3820 }
3821 }
3822
3823 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
3824 sqlite3_free(azResult);
3825 }else
3826
3827 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
3828

Keyboard Shortcuts

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