/ Check-in [b49b8fdd]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Fix a C++-ism in the previous change to tclsqlite.c. (CVS 2168)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: b49b8fdd11a5a4aac15ceda58a28bbc852f6f239
User & Date: drh 2004-12-17 20:48:06
Context
2004-12-18
18:40
Improvements to the query optimizer. This is a work in progress. (CVS 2169) check-in: 9b86993f user: drh tags: trunk
2004-12-17
20:48
Fix a C++-ism in the previous change to tclsqlite.c. (CVS 2168) check-in: b49b8fdd user: drh tags: trunk
15:41
Add 'copy' method for tcl interface. Behaves similar to shell .import or COPY statment in 2.x. (CVS 2167) check-in: a9311d9d user: tpoindex tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/tclsqlite.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** A TCL Interface to SQLite
    13     13   **
    14         -** $Id: tclsqlite.c,v 1.110 2004/12/17 15:41:12 tpoindex Exp $
           14  +** $Id: tclsqlite.c,v 1.111 2004/12/17 20:48:06 drh Exp $
    15     15   */
    16     16   #ifndef NO_TCL     /* Omit this whole file if TCL is unavailable */
    17     17   
    18     18   #include "sqliteInt.h"
    19     19   #include "hash.h"
    20     20   #include "tcl.h"
    21     21   #include <stdlib.h>
................................................................................
  1107   1107     ** the sqlite3 shell.c ".import" command.
  1108   1108     **
  1109   1109     ** This command usage is equivalent to the sqlite2.x COPY statement,
  1110   1110     ** which imports file data into a table using the PostgreSQL COPY file format:
  1111   1111     **   $db copy $conflit_algo $table_name $filename \t \\N
  1112   1112     */
  1113   1113     case DB_COPY: {
  1114         -    char *zSep;
  1115         -    char *zNull;
  1116         -    if( objc<5 || objc>7 ){
  1117         -      Tcl_WrongNumArgs(interp, 2, objv, "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?");
  1118         -      return TCL_ERROR;
  1119         -    }
  1120         -    if( objc>=6 ){
  1121         -      zSep = Tcl_GetStringFromObj(objv[5], 0);
  1122         -    }else{
  1123         -      zSep = "\t";
  1124         -    }
  1125         -    if( objc>=7 ){
  1126         -      zNull = Tcl_GetStringFromObj(objv[6], 0);
  1127         -    }else{
  1128         -      zNull = "";
  1129         -    }
  1130   1114       char *zTable;               /* Insert data into this table */
  1131   1115       char *zFile;                /* The file from which to extract data */
  1132   1116       char *zConflict;            /* The conflict algorithm to use */
  1133   1117       sqlite3_stmt *pStmt;        /* A statement */
  1134   1118       int rc;                     /* Result code */
  1135   1119       int nCol;                   /* Number of columns in the table */
  1136   1120       int nByte;                  /* Number of bytes in an SQL string */
................................................................................
  1142   1126       char **azCol;               /* zLine[] broken up into columns */
  1143   1127       char *zCommit;              /* How to commit changes */
  1144   1128       FILE *in;                   /* The input file */
  1145   1129       int lineno = 0;             /* Line number of input file */
  1146   1130       char zLineNum[80];          /* Line number print buffer */
  1147   1131       Tcl_Obj *pResult;           /* interp result */
  1148   1132   
         1133  +    char *zSep;
         1134  +    char *zNull;
         1135  +    if( objc<5 || objc>7 ){
         1136  +      Tcl_WrongNumArgs(interp, 2, objv, 
         1137  +         "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?");
         1138  +      return TCL_ERROR;
         1139  +    }
         1140  +    if( objc>=6 ){
         1141  +      zSep = Tcl_GetStringFromObj(objv[5], 0);
         1142  +    }else{
         1143  +      zSep = "\t";
         1144  +    }
         1145  +    if( objc>=7 ){
         1146  +      zNull = Tcl_GetStringFromObj(objv[6], 0);
         1147  +    }else{
         1148  +      zNull = "";
         1149  +    }
  1149   1150       zConflict = Tcl_GetStringFromObj(objv[2], 0);
  1150   1151       zTable = Tcl_GetStringFromObj(objv[3], 0);
  1151   1152       zFile = Tcl_GetStringFromObj(objv[4], 0);
  1152   1153       nSep = strlen(zSep);
  1153   1154       nNull = strlen(zNull);
  1154   1155       if( nSep==0 ){
  1155   1156         Tcl_AppendResult(interp, "Error: non-null separator required for copy", 0);
................................................................................
  1156   1157         return TCL_ERROR;
  1157   1158       }
  1158   1159       if(sqlite3StrICmp(zConflict, "rollback") != 0 &&
  1159   1160          sqlite3StrICmp(zConflict, "abort"   ) != 0 &&
  1160   1161          sqlite3StrICmp(zConflict, "fail"    ) != 0 &&
  1161   1162          sqlite3StrICmp(zConflict, "ignore"  ) != 0 &&
  1162   1163          sqlite3StrICmp(zConflict, "replace" ) != 0 ) {
  1163         -      Tcl_AppendResult(interp, "Error: \"", zConflict, "\", conflict-algorithm must be one of: rollback, abort, fail, ignore, or replace", 0);
         1164  +      Tcl_AppendResult(interp, "Error: \"", zConflict, 
         1165  +            "\", conflict-algorithm must be one of: rollback, "
         1166  +            "abort, fail, ignore, or replace", 0);
  1164   1167         return TCL_ERROR;
  1165   1168       }
  1166   1169       zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
  1167   1170       if( zSql==0 ){
  1168   1171         Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0);
  1169   1172         return TCL_ERROR;
  1170   1173       }
................................................................................
  1182   1185         return TCL_ERROR;
  1183   1186       }
  1184   1187       zSql = malloc( nByte + 50 + nCol*2 );
  1185   1188       if( zSql==0 ) {
  1186   1189         Tcl_AppendResult(interp, "Error: can't malloc()", 0);
  1187   1190         return TCL_ERROR;
  1188   1191       }
  1189         -    sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?", zConflict, zTable);
         1192  +    sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?",
         1193  +         zConflict, zTable);
  1190   1194       j = strlen(zSql);
  1191   1195       for(i=1; i<nCol; i++){
  1192   1196         zSql[j++] = ',';
  1193   1197         zSql[j++] = '?';
  1194   1198       }
  1195   1199       zSql[j++] = ')';
  1196   1200       zSql[j] = 0;