SQLite

Check-in [790402c150]
Login

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

Overview
Comment:In shell, reworked .header and .echo handling. Updated shell_exec() to (really) handle multiple statements. Tickets [72adc99de9], [7b61b6c6ce], and [eb620916be].
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 790402c150e2026cd0c147a4cadbe9b9ab97b688
User & Date: shane 2009-10-22 21:23:35.000
References
2009-10-22
21:27 Ticket [7b61b6c6ce] insert mode is incorrectly converting text to numbers status still Fixed with 1 other change (artifact: b8c779e7ff user: shane)
21:27 Ticket [72adc99de9] shell mode insert doesn't output blobs as blobs status still Fixed with 1 other change (artifact: b4739ea677 user: shane)
21:26 Fixed ticket [eb620916be]: Shell doesn't print header if multiple statements on one line plus 3 other changes (artifact: 8b2e3b3dce user: shane)
Context
2009-10-23
00:37
In shell, modified "import" handling to ensure error code returned correctly on exit. Ticket [bd770b2c52]. (check-in: 009efad0f4 user: shane tags: trunk)
2009-10-22
21:23
In shell, reworked .header and .echo handling. Updated shell_exec() to (really) handle multiple statements. Tickets [72adc99de9], [7b61b6c6ce], and [eb620916be]. (check-in: 790402c150 user: shane tags: trunk)
20:52
Merge the MD5 checksum logic into the TCL interface. This facilitates building a tclsh that contains both SQLite and MD5. The plan is to use this augmented tclsh to help build the documentation. (check-in: a024c0a85b user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/shell.c.
1497
1498
1499
1500
1501
1502
1503





1504
1505
1506
1507
1508
1509
1510
/*
** This is the callback routine that the shell
** invokes for each row of a query result.
*/
static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
  int i;
  struct callback_data *p = (struct callback_data*)pArg;





  switch( p->mode ){
    case MODE_Line: {
      int w = 5;
      if( azArg==0 ) break;
      for(i=0; i<nArg; i++){
        int len = strlen30(azCol[i] ? azCol[i] : "");
        if( len>w ) w = len;







>
>
>
>
>







1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
/*
** This is the callback routine that the shell
** invokes for each row of a query result.
*/
static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
  int i;
  struct callback_data *p = (struct callback_data*)pArg;

  if( p->echoOn && p->cnt==0  && p->pStmt){
    printf("%s\n", sqlite3_sql(p->pStmt));
  }

  switch( p->mode ){
    case MODE_Line: {
      int w = 5;
      if( azArg==0 ) break;
      for(i=0; i<nArg; i++){
        int len = strlen30(azCol[i] ? azCol[i] : "");
        if( len>w ) w = len;
1639
1640
1641
1642
1643
1644
1645

1646
1647
1648
1649
1650
1651
1652
      for(i=0; i<nArg; i++){
        output_csv(p, azArg[i], i<nArg-1);
      }
      fprintf(p->out,"\n");
      break;
    }
    case MODE_Insert: {

      if( azArg==0 ) break;
      fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
      for(i=0; i<nArg; i++){
        char *zSep = i>0 ? ",": "";
        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
          fprintf(p->out,"%sNULL",zSep);
        }else if( aiType && aiType[i]==SQLITE_TEXT ){







>







1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
      for(i=0; i<nArg; i++){
        output_csv(p, azArg[i], i<nArg-1);
      }
      fprintf(p->out,"\n");
      break;
    }
    case MODE_Insert: {
      p->cnt++;
      if( azArg==0 ) break;
      fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
      for(i=0; i<nArg; i++){
        char *zSep = i>0 ? ",": "";
        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
          fprintf(p->out,"%sNULL",zSep);
        }else if( aiType && aiType[i]==SQLITE_TEXT ){
1825
1826
1827
1828
1829
1830
1831

1832

1833
1834
1835
1836
1837

1838
1839
1840
1841
1842
1843







1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869

1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922






1923
1924
1925
1926
1927
1928
1929
  const char *zSql,                           /* SQL to be evaluated */
  int (*xCallback)(void*,int,char**,char**,int*),   /* Callback function */
                                              /* (not the same as sqlite3_exec) */
  struct callback_data *pArg,                 /* Pointer to struct callback_data */
  char **pzErrMsg                             /* Error msg written here */
){
  sqlite3_stmt *pStmt = NULL;

  int rc, rc2;


  if( pzErrMsg ){
    *pzErrMsg = NULL;
  }


  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  if( (SQLITE_OK != rc) || !pStmt ){
    if( pzErrMsg ){
      *pzErrMsg = save_err_msg(db);
    }
  }else{







    /* perform the first step.  this will tell us if we
    ** have a result set or not and how wide it is.
    */
    rc = sqlite3_step(pStmt);
    /* if we have a result set... */
    if( SQLITE_ROW == rc ){
      /* if callback... */
      if( xCallback ){
        /* allocate space for col name ptr, value ptr, and type */
        int nCol = sqlite3_column_count(pStmt);
        void *pData = sqlite3_malloc(3*nCol*sizeof(const char*) + 1);
        if( !pData ){
          rc = SQLITE_NOMEM;
        }else{
          char **azCols = (char **)pData;      /* Names of result columns */
          char **azVals = &azCols[nCol];       /* Results */
          int *aiTypes = (int *)&azVals[nCol]; /* Result types */
          int i;
          assert(sizeof(int) <= sizeof(char *)); 
          /* save off ptrs to column names */
          for(i=0; i<nCol; i++){
            azCols[i] = (char *)sqlite3_column_name(pStmt, i);
          }
          /* save off the prepared statment handle */
          if( pArg ){
            pArg->pStmt = pStmt;

          }
          do{
            /* extract the data and data types */
            for(i=0; i<nCol; i++){
              azVals[i] = (char *)sqlite3_column_text(pStmt, i);
              aiTypes[i] = sqlite3_column_type(pStmt, i);
              if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
                rc = SQLITE_NOMEM;
                break; /* from for */
              }
            } /* end for */

            /* if data and types extracted successfully... */
            if( SQLITE_ROW == rc ){ 
              /* call the supplied callback with the result row data */
              if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
                rc = SQLITE_ABORT;
              }else{
                rc = sqlite3_step(pStmt);
              }
            }
          } while( SQLITE_ROW == rc );
          sqlite3_free(pData);
          if( pArg ){
            pArg->pStmt = NULL;
          }
        }
      }else{
        do{
          rc = sqlite3_step(pStmt);
        } while( rc == SQLITE_ROW );
      }
    }

    /* if the last sqlite3_step() didn't complete successfully... */
    if( (SQLITE_OK != rc) && (SQLITE_DONE != rc) ){ 
      if( pzErrMsg ){
        *pzErrMsg = save_err_msg(db);
      }
    }else{
      rc = SQLITE_OK;
    }

    rc2 = sqlite3_finalize(pStmt);
    /* if the last sqlite3_finalize() didn't complete successfully 
    ** AND we don't have a save error from sqlite3_step ... */
    if( (SQLITE_OK != rc2) && (SQLITE_OK == rc) ){
      rc = rc2;
      if( pzErrMsg ){
        *pzErrMsg = save_err_msg(db);
      }
    }
  }







  return rc;
}


/*
** This is a different callback routine used for dumping the database.







>
|
>





>
|
|
|
|
|
|
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>







1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
  const char *zSql,                           /* SQL to be evaluated */
  int (*xCallback)(void*,int,char**,char**,int*),   /* Callback function */
                                              /* (not the same as sqlite3_exec) */
  struct callback_data *pArg,                 /* Pointer to struct callback_data */
  char **pzErrMsg                             /* Error msg written here */
){
  sqlite3_stmt *pStmt = NULL;
  int rc = SQLITE_OK;
  int rc2;
  const char *zLeftover;      /* Tail of unprocessed SQL */

  if( pzErrMsg ){
    *pzErrMsg = NULL;
  }

  while( zSql[0] && (SQLITE_OK == rc) ){
    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
    if( SQLITE_OK != rc ){
      if( pzErrMsg ){
        *pzErrMsg = save_err_msg(db);
      }
    }else{
      if( !pStmt ){
        /* this happens for a comment or white-space */
        zSql = zLeftover;
        while( isspace(zSql[0]) ) zSql++;
        continue;
      }

      /* perform the first step.  this will tell us if we
      ** have a result set or not and how wide it is.
      */
      rc = sqlite3_step(pStmt);
      /* if we have a result set... */
      if( SQLITE_ROW == rc ){
        /* if we have a callback... */
        if( xCallback ){
          /* allocate space for col name ptr, value ptr, and type */
          int nCol = sqlite3_column_count(pStmt);
          void *pData = sqlite3_malloc(3*nCol*sizeof(const char*) + 1);
          if( !pData ){
            rc = SQLITE_NOMEM;
          }else{
            char **azCols = (char **)pData;      /* Names of result columns */
            char **azVals = &azCols[nCol];       /* Results */
            int *aiTypes = (int *)&azVals[nCol]; /* Result types */
            int i;
            assert(sizeof(int) <= sizeof(char *)); 
            /* save off ptrs to column names */
            for(i=0; i<nCol; i++){
              azCols[i] = (char *)sqlite3_column_name(pStmt, i);
            }
            /* save off the prepared statment handle and reset row count */
            if( pArg ){
              pArg->pStmt = pStmt;
              pArg->cnt = 0;
            }
            do{
              /* extract the data and data types */
              for(i=0; i<nCol; i++){
                azVals[i] = (char *)sqlite3_column_text(pStmt, i);
                aiTypes[i] = sqlite3_column_type(pStmt, i);
                if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
                  rc = SQLITE_NOMEM;
                  break; /* from for */
                }
              } /* end for */

              /* if data and types extracted successfully... */
              if( SQLITE_ROW == rc ){ 
                /* call the supplied callback with the result row data */
                if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
                  rc = SQLITE_ABORT;
                }else{
                  rc = sqlite3_step(pStmt);
                }
              }
            } while( SQLITE_ROW == rc );
            sqlite3_free(pData);
            if( pArg ){
              pArg->pStmt = NULL;
            }
          }
        }else{
          do{
            rc = sqlite3_step(pStmt);
          } while( rc == SQLITE_ROW );
        }
      }

      /* if the last sqlite3_step() didn't complete successfully... */
      if( (SQLITE_OK != rc) && (SQLITE_DONE != rc) ){ 
        if( pzErrMsg ){
          *pzErrMsg = save_err_msg(db);
        }
      }else{
        rc = SQLITE_OK;
      }

      rc2 = sqlite3_finalize(pStmt);
      /* if the last sqlite3_finalize() didn't complete successfully 
      ** AND we don't have a save error from sqlite3_step ... */
      if( (SQLITE_OK != rc2) && (SQLITE_OK == rc) ){
        rc = rc2;
        if( pzErrMsg ){
          *pzErrMsg = save_err_msg(db);
        }
      }

      if( SQLITE_OK == rc ){ 
        zSql = zLeftover;
        while( isspace(zSql[0]) ) zSql++;
      }
    }
  } /* end while */

  return rc;
}


/*
** This is a different callback routine used for dumping the database.
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029

3030
3031
3032
3033
3034
3035
3036
      break;  /* We have reached EOF */
    }
    if( seenInterrupt ){
      if( in!=0 ) break;
      seenInterrupt = 0;
    }
    lineno++;
    if( p->echoOn ) printf("%s\n", zLine);
    if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
    if( zLine && zLine[0]=='.' && nSql==0 ){

      rc = do_meta_command(zLine, p);
      if( rc==2 ){
        break;
      }else if( rc ){
        errCnt++;
      }
      continue;







<


>







3043
3044
3045
3046
3047
3048
3049

3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
      break;  /* We have reached EOF */
    }
    if( seenInterrupt ){
      if( in!=0 ) break;
      seenInterrupt = 0;
    }
    lineno++;

    if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
    if( zLine && zLine[0]=='.' && nSql==0 ){
      if( p->echoOn ) printf("%s\n", zLine);
      rc = do_meta_command(zLine, p);
      if( rc==2 ){
        break;
      }else if( rc ){
        errCnt++;
      }
      continue;