*************************************************************************
** A TCL Interface to SQLite
**
-** $Id: tclsqlite.c,v 1.131 2005/08/16 11:11:35 drh Exp $
+** $Id: tclsqlite.c,v 1.132 2005/08/29 23:00:04 drh Exp $
*/
#ifndef NO_TCL /* Omit this whole file if TCL is unavailable */
char *zBusy; /* The busy callback routine */
char *zCommit; /* The commit hook callback routine */
char *zTrace; /* The trace callback routine */
+ char *zProfile; /* The profile callback routine */
char *zProgress; /* The progress callback routine */
char *zAuth; /* The authorization callback routine */
char *zNull; /* Text to substitute for an SQL NULL value */
if( pDb->zTrace ){
Tcl_Free(pDb->zTrace);
}
+ if( pDb->zProfile ){
+ Tcl_Free(pDb->zProfile);
+ }
if( pDb->zAuth ){
Tcl_Free(pDb->zAuth);
}
Tcl_ResetResult(pDb->interp);
}
+/*
+** This routine is called by the SQLite profile handler after a statement
+** SQL has executed. The TCL script in pDb->zProfile is evaluated.
+*/
+static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){
+ SqliteDb *pDb = (SqliteDb*)cd;
+ Tcl_DString str;
+ char zTm[100];
+
+ sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm);
+ Tcl_DStringInit(&str);
+ Tcl_DStringAppend(&str, pDb->zProfile, -1);
+ Tcl_DStringAppendElement(&str, zSql);
+ Tcl_DStringAppendElement(&str, zTm);
+ Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
+ Tcl_DStringFree(&str);
+ Tcl_ResetResult(pDb->interp);
+}
+
/*
** This routine is called when a transaction is committed. The
** TCL script in pDb->zCommit is executed. If it returns non-zero or
"collation_needed", "commit_hook", "complete",
"copy", "errorcode", "eval",
"function", "last_insert_rowid", "nullvalue",
- "onecolumn", "progress", "rekey",
- "timeout", "total_changes", "trace",
- "transaction", "version", 0
+ "onecolumn", "profile", "progress",
+ "rekey", "timeout", "total_changes",
+ "trace", "transaction", "version",
+ 0
};
enum DB_enum {
DB_AUTHORIZER, DB_BUSY, DB_CACHE,
DB_COLLATION_NEEDED, DB_COMMIT_HOOK, DB_COMPLETE,
DB_COPY, DB_ERRORCODE, DB_EVAL,
DB_FUNCTION, DB_LAST_INSERT_ROWID,DB_NULLVALUE,
- DB_ONECOLUMN, DB_PROGRESS, DB_REKEY,
- DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE,
- DB_TRANSACTION, DB_VERSION,
+ DB_ONECOLUMN, DB_PROFILE, DB_PROGRESS,
+ DB_REKEY, DB_TIMEOUT, DB_TOTAL_CHANGES,
+ DB_TRACE, DB_TRANSACTION, DB_VERSION
};
/* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */
break;
}
- /* $db commit_hook ?CALLBACK?
- **
- ** Invoke the given callback just before committing every SQL transaction.
- ** If the callback throws an exception or returns non-zero, then the
- ** transaction is aborted. If CALLBACK is an empty string, the callback
- ** is disabled.
- */
- case DB_COMMIT_HOOK: {
- if( objc>3 ){
- Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
- return TCL_ERROR;
- }else if( objc==2 ){
- if( pDb->zCommit ){
- Tcl_AppendResult(interp, pDb->zCommit, 0);
- }
- }else{
- char *zCommit;
- int len;
- if( pDb->zCommit ){
- Tcl_Free(pDb->zCommit);
- }
- zCommit = Tcl_GetStringFromObj(objv[2], &len);
- if( zCommit && len>0 ){
- pDb->zCommit = Tcl_Alloc( len + 1 );
- strcpy(pDb->zCommit, zCommit);
- }else{
- pDb->zCommit = 0;
- }
- if( pDb->zCommit ){
- pDb->interp = interp;
- sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
- }else{
- sqlite3_commit_hook(pDb->db, 0, 0);
- }
- }
- break;
- }
-
/*
** $db collate NAME SCRIPT
**
break;
}
+ /* $db commit_hook ?CALLBACK?
+ **
+ ** Invoke the given callback just before committing every SQL transaction.
+ ** If the callback throws an exception or returns non-zero, then the
+ ** transaction is aborted. If CALLBACK is an empty string, the callback
+ ** is disabled.
+ */
+ case DB_COMMIT_HOOK: {
+ if( objc>3 ){
+ Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
+ return TCL_ERROR;
+ }else if( objc==2 ){
+ if( pDb->zCommit ){
+ Tcl_AppendResult(interp, pDb->zCommit, 0);
+ }
+ }else{
+ char *zCommit;
+ int len;
+ if( pDb->zCommit ){
+ Tcl_Free(pDb->zCommit);
+ }
+ zCommit = Tcl_GetStringFromObj(objv[2], &len);
+ if( zCommit && len>0 ){
+ pDb->zCommit = Tcl_Alloc( len + 1 );
+ strcpy(pDb->zCommit, zCommit);
+ }else{
+ pDb->zCommit = 0;
+ }
+ if( pDb->zCommit ){
+ pDb->interp = interp;
+ sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
+ }else{
+ sqlite3_commit_hook(pDb->db, 0, 0);
+ }
+ }
+ break;
+ }
+
/* $db complete SQL
**
** Return TRUE if SQL is a complete SQL statement. Return FALSE if
break;
}
- /*
- ** $db errorcode
+ /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR?
**
- ** Return the numeric error code that was returned by the most recent
- ** call to sqlite3_exec().
- */
- case DB_ERRORCODE: {
- Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
- break;
- }
-
- /*
- ** $db eval $sql ?array? ?{ ...code... }?
- ** $db onecolumn $sql
+ ** Copy data into table from filename, optionally using SEPARATOR
+ ** as column separators. If a column contains a null string, or the
+ ** value of NULLINDICATOR, a NULL is inserted for the column.
+ ** conflict-algorithm is one of the sqlite conflict algorithms:
+ ** rollback, abort, fail, ignore, replace
+ ** On success, return the number of lines processed, not necessarily same
+ ** as 'db changes' due to conflict-algorithm selected.
**
- ** The SQL statement in $sql is evaluated. For each row, the values are
- ** placed in elements of the array named "array" and ...code... is executed.
- ** If "array" and "code" are omitted, then no callback is every invoked.
- ** If "array" is an empty string, then the values are placed in variables
- ** that have the same name as the fields extracted by the query.
+ ** This code is basically an implementation/enhancement of
+ ** the sqlite3 shell.c ".import" command.
**
- ** The onecolumn method is the equivalent of:
- ** lindex [$db eval $sql] 0
+ ** This command usage is equivalent to the sqlite2.x COPY statement,
+ ** which imports file data into a table using the PostgreSQL COPY file format:
+ ** $db copy $conflit_algo $table_name $filename \t \\N
*/
- case DB_ONECOLUMN:
- case DB_EVAL: {
- char const *zSql; /* Next SQL statement to execute */
- char const *zLeft; /* What is left after first stmt in zSql */
- sqlite3_stmt *pStmt; /* Compiled SQL statment */
- Tcl_Obj *pArray; /* Name of array into which results are written */
- Tcl_Obj *pScript; /* Script to run for each result set */
- Tcl_Obj **apParm; /* Parameters that need a Tcl_DecrRefCount() */
- int nParm; /* Number of entries used in apParm[] */
- Tcl_Obj *aParm[10]; /* Static space for apParm[] in the common case */
- Tcl_Obj *pRet; /* Value to be returned */
- SqlPreparedStmt *pPreStmt; /* Pointer to a prepared statement */
- int rc2;
+ case DB_COPY: {
+ char *zTable; /* Insert data into this table */
+ char *zFile; /* The file from which to extract data */
+ char *zConflict; /* The conflict algorithm to use */
+ sqlite3_stmt *pStmt; /* A statement */
+ int rc; /* Result code */
+ int nCol; /* Number of columns in the table */
+ int nByte; /* Number of bytes in an SQL string */
+ int i, j; /* Loop counters */
+ int nSep; /* Number of bytes in zSep[] */
+ int nNull; /* Number of bytes in zNull[] */
+ char *zSql; /* An SQL statement */
+ char *zLine; /* A single line of input from the file */
+ char **azCol; /* zLine[] broken up into columns */
+ char *zCommit; /* How to commit changes */
+ FILE *in; /* The input file */
+ int lineno = 0; /* Line number of input file */
+ char zLineNum[80]; /* Line number print buffer */
+ Tcl_Obj *pResult; /* interp result */
- if( choice==DB_ONECOLUMN ){
- if( objc!=3 ){
- Tcl_WrongNumArgs(interp, 2, objv, "SQL");
- return TCL_ERROR;
- }
- pRet = 0;
+ char *zSep;
+ char *zNull;
+ if( objc<5 || objc>7 ){
+ Tcl_WrongNumArgs(interp, 2, objv,
+ "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?");
+ return TCL_ERROR;
+ }
+ if( objc>=6 ){
+ zSep = Tcl_GetStringFromObj(objv[5], 0);
}else{
- if( objc<3 || objc>5 ){
- Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?");
- return TCL_ERROR;
- }
- pRet = Tcl_NewObj();
- Tcl_IncrRefCount(pRet);
+ zSep = "\t";
}
- if( objc==3 ){
- pArray = pScript = 0;
- }else if( objc==4 ){
- pArray = 0;
- pScript = objv[3];
+ if( objc>=7 ){
+ zNull = Tcl_GetStringFromObj(objv[6], 0);
}else{
- pArray = objv[3];
- if( Tcl_GetString(pArray)[0]==0 ) pArray = 0;
- pScript = objv[4];
+ zNull = "";
}
-
- Tcl_IncrRefCount(objv[2]);
- zSql = Tcl_GetStringFromObj(objv[2], 0);
- while( rc==TCL_OK && zSql[0] ){
- int i; /* Loop counter */
- int nVar; /* Number of bind parameters in the pStmt */
- int nCol; /* Number of columns in the result set */
- Tcl_Obj **apColName = 0; /* Array of column names */
- int len; /* String length of zSql */
-
- /* Try to find a SQL statement that has already been compiled and
- ** which matches the next sequence of SQL.
- */
- pStmt = 0;
- pPreStmt = pDb->stmtList;
- len = strlen(zSql);
- if( pPreStmt && sqlite3_expired(pPreStmt->pStmt) ){
- flushStmtCache(pDb);
- pPreStmt = 0;
- }
- for(; pPreStmt; pPreStmt=pPreStmt->pNext){
- int n = pPreStmt->nSql;
- if( len>=n
- && memcmp(pPreStmt->zSql, zSql, n)==0
- && (zSql[n]==0 || zSql[n-1]==';')
- ){
- pStmt = pPreStmt->pStmt;
- zLeft = &zSql[pPreStmt->nSql];
-
- /* When a prepared statement is found, unlink it from the
- ** cache list. It will later be added back to the beginning
- ** of the cache list in order to implement LRU replacement.
- */
- if( pPreStmt->pPrev ){
- pPreStmt->pPrev->pNext = pPreStmt->pNext;
- }else{
- pDb->stmtList = pPreStmt->pNext;
- }
- if( pPreStmt->pNext ){
- pPreStmt->pNext->pPrev = pPreStmt->pPrev;
- }else{
- pDb->stmtLast = pPreStmt->pPrev;
- }
- pDb->nStmt--;
- break;
- }
- }
-
- /* If no prepared statement was found. Compile the SQL text
- */
- if( pStmt==0 ){
- if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
- Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
- rc = TCL_ERROR;
- break;
- }
- if( pStmt==0 ){
- if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
- /* A compile-time error in the statement
- */
- Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
- rc = TCL_ERROR;
- break;
- }else{
- /* The statement was a no-op. Continue to the next statement
- ** in the SQL string.
- */
- zSql = zLeft;
- continue;
+ zConflict = Tcl_GetStringFromObj(objv[2], 0);
+ zTable = Tcl_GetStringFromObj(objv[3], 0);
+ zFile = Tcl_GetStringFromObj(objv[4], 0);
+ nSep = strlen(zSep);
+ nNull = strlen(zNull);
+ if( nSep==0 ){
+ Tcl_AppendResult(interp, "Error: non-null separator required for copy", 0);
+ return TCL_ERROR;
+ }
+ if(sqlite3StrICmp(zConflict, "rollback") != 0 &&
+ sqlite3StrICmp(zConflict, "abort" ) != 0 &&
+ sqlite3StrICmp(zConflict, "fail" ) != 0 &&
+ sqlite3StrICmp(zConflict, "ignore" ) != 0 &&
+ sqlite3StrICmp(zConflict, "replace" ) != 0 ) {
+ Tcl_AppendResult(interp, "Error: \"", zConflict,
+ "\", conflict-algorithm must be one of: rollback, "
+ "abort, fail, ignore, or replace", 0);
+ return TCL_ERROR;
+ }
+ zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
+ if( zSql==0 ){
+ Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0);
+ return TCL_ERROR;
+ }
+ nByte = strlen(zSql);
+ rc = sqlite3_prepare(pDb->db, zSql, 0, &pStmt, 0);
+ sqlite3_free(zSql);
+ if( rc ){
+ Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0);
+ nCol = 0;
+ }else{
+ nCol = sqlite3_column_count(pStmt);
+ }
+ sqlite3_finalize(pStmt);
+ if( nCol==0 ) {
+ return TCL_ERROR;
+ }
+ zSql = malloc( nByte + 50 + nCol*2 );
+ if( zSql==0 ) {
+ Tcl_AppendResult(interp, "Error: can't malloc()", 0);
+ return TCL_ERROR;
+ }
+ sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?",
+ zConflict, zTable);
+ j = strlen(zSql);
+ for(i=1; i<nCol; i++){
+ zSql[j++] = ',';
+ zSql[j++] = '?';
+ }
+ zSql[j++] = ')';
+ zSql[j] = 0;
+ rc = sqlite3_prepare(pDb->db, zSql, 0, &pStmt, 0);
+ free(zSql);
+ if( rc ){
+ Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0);
+ sqlite3_finalize(pStmt);
+ return TCL_ERROR;
+ }
+ in = fopen(zFile, "rb");
+ if( in==0 ){
+ Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL);
+ sqlite3_finalize(pStmt);
+ return TCL_ERROR;
+ }
+ azCol = malloc( sizeof(azCol[0])*(nCol+1) );
+ if( azCol==0 ) {
+ Tcl_AppendResult(interp, "Error: can't malloc()", 0);
+ return TCL_ERROR;
+ }
+ sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0);
+ zCommit = "COMMIT";
+ while( (zLine = local_getline(0, in))!=0 ){
+ char *z;
+ i = 0;
+ lineno++;
+ azCol[0] = zLine;
+ for(i=0, z=zLine; *z; z++){
+ if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){
+ *z = 0;
+ i++;
+ if( i<nCol ){
+ azCol[i] = &z[nSep];
+ z += nSep-1;
+ }
+ }
+ }
+ if( i+1!=nCol ){
+ char *zErr;
+ zErr = malloc(200 + strlen(zFile));
+ sprintf(zErr,"Error: %s line %d: expected %d columns of data but found %d",
+ zFile, lineno, nCol, i+1);
+ Tcl_AppendResult(interp, zErr, 0);
+ free(zErr);
+ zCommit = "ROLLBACK";
+ break;
+ }
+ for(i=0; i<nCol; i++){
+ /* check for null data, if so, bind as null */
+ if ((nNull>0 && strcmp(azCol[i], zNull)==0) || strlen(azCol[i])==0) {
+ sqlite3_bind_null(pStmt, i+1);
+ }else{
+ sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
+ }
+ }
+ sqlite3_step(pStmt);
+ rc = sqlite3_reset(pStmt);
+ free(zLine);
+ if( rc!=SQLITE_OK ){
+ Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), 0);
+ zCommit = "ROLLBACK";
+ break;
+ }
+ }
+ free(azCol);
+ fclose(in);
+ sqlite3_finalize(pStmt);
+ sqlite3_exec(pDb->db, zCommit, 0, 0, 0);
+
+ if( zCommit[0] == 'C' ){
+ /* success, set result as number of lines processed */
+ pResult = Tcl_GetObjResult(interp);
+ Tcl_SetIntObj(pResult, lineno);
+ rc = TCL_OK;
+ }else{
+ /* failure, append lineno where failed */
+ sprintf(zLineNum,"%d",lineno);
+ Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,0);
+ rc = TCL_ERROR;
+ }
+ break;
+ }
+
+ /*
+ ** $db errorcode
+ **
+ ** Return the numeric error code that was returned by the most recent
+ ** call to sqlite3_exec().
+ */
+ case DB_ERRORCODE: {
+ Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
+ break;
+ }
+
+ /*
+ ** $db eval $sql ?array? ?{ ...code... }?
+ ** $db onecolumn $sql
+ **
+ ** The SQL statement in $sql is evaluated. For each row, the values are
+ ** placed in elements of the array named "array" and ...code... is executed.
+ ** If "array" and "code" are omitted, then no callback is every invoked.
+ ** If "array" is an empty string, then the values are placed in variables
+ ** that have the same name as the fields extracted by the query.
+ **
+ ** The onecolumn method is the equivalent of:
+ ** lindex [$db eval $sql] 0
+ */
+ case DB_ONECOLUMN:
+ case DB_EVAL: {
+ char const *zSql; /* Next SQL statement to execute */
+ char const *zLeft; /* What is left after first stmt in zSql */
+ sqlite3_stmt *pStmt; /* Compiled SQL statment */
+ Tcl_Obj *pArray; /* Name of array into which results are written */
+ Tcl_Obj *pScript; /* Script to run for each result set */
+ Tcl_Obj **apParm; /* Parameters that need a Tcl_DecrRefCount() */
+ int nParm; /* Number of entries used in apParm[] */
+ Tcl_Obj *aParm[10]; /* Static space for apParm[] in the common case */
+ Tcl_Obj *pRet; /* Value to be returned */
+ SqlPreparedStmt *pPreStmt; /* Pointer to a prepared statement */
+ int rc2;
+
+ if( choice==DB_ONECOLUMN ){
+ if( objc!=3 ){
+ Tcl_WrongNumArgs(interp, 2, objv, "SQL");
+ return TCL_ERROR;
+ }
+ pRet = 0;
+ }else{
+ if( objc<3 || objc>5 ){
+ Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?");
+ return TCL_ERROR;
+ }
+ pRet = Tcl_NewObj();
+ Tcl_IncrRefCount(pRet);
+ }
+ if( objc==3 ){
+ pArray = pScript = 0;
+ }else if( objc==4 ){
+ pArray = 0;
+ pScript = objv[3];
+ }else{
+ pArray = objv[3];
+ if( Tcl_GetString(pArray)[0]==0 ) pArray = 0;
+ pScript = objv[4];
+ }
+
+ Tcl_IncrRefCount(objv[2]);
+ zSql = Tcl_GetStringFromObj(objv[2], 0);
+ while( rc==TCL_OK && zSql[0] ){
+ int i; /* Loop counter */
+ int nVar; /* Number of bind parameters in the pStmt */
+ int nCol; /* Number of columns in the result set */
+ Tcl_Obj **apColName = 0; /* Array of column names */
+ int len; /* String length of zSql */
+
+ /* Try to find a SQL statement that has already been compiled and
+ ** which matches the next sequence of SQL.
+ */
+ pStmt = 0;
+ pPreStmt = pDb->stmtList;
+ len = strlen(zSql);
+ if( pPreStmt && sqlite3_expired(pPreStmt->pStmt) ){
+ flushStmtCache(pDb);
+ pPreStmt = 0;
+ }
+ for(; pPreStmt; pPreStmt=pPreStmt->pNext){
+ int n = pPreStmt->nSql;
+ if( len>=n
+ && memcmp(pPreStmt->zSql, zSql, n)==0
+ && (zSql[n]==0 || zSql[n-1]==';')
+ ){
+ pStmt = pPreStmt->pStmt;
+ zLeft = &zSql[pPreStmt->nSql];
+
+ /* When a prepared statement is found, unlink it from the
+ ** cache list. It will later be added back to the beginning
+ ** of the cache list in order to implement LRU replacement.
+ */
+ if( pPreStmt->pPrev ){
+ pPreStmt->pPrev->pNext = pPreStmt->pNext;
+ }else{
+ pDb->stmtList = pPreStmt->pNext;
+ }
+ if( pPreStmt->pNext ){
+ pPreStmt->pNext->pPrev = pPreStmt->pPrev;
+ }else{
+ pDb->stmtLast = pPreStmt->pPrev;
+ }
+ pDb->nStmt--;
+ break;
+ }
+ }
+
+ /* If no prepared statement was found. Compile the SQL text
+ */
+ if( pStmt==0 ){
+ if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
+ Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
+ rc = TCL_ERROR;
+ break;
+ }
+ if( pStmt==0 ){
+ if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
+ /* A compile-time error in the statement
+ */
+ Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
+ rc = TCL_ERROR;
+ break;
+ }else{
+ /* The statement was a no-op. Continue to the next statement
+ ** in the SQL string.
+ */
+ zSql = zLeft;
+ continue;
}
}
assert( pPreStmt==0 );
break;
}
+ /*
+ ** $db nullvalue ?STRING?
+ **
+ ** Change text used when a NULL comes back from the database. If ?STRING?
+ ** is not present, then the current string used for NULL is returned.
+ ** If STRING is present, then STRING is returned.
+ **
+ */
+ case DB_NULLVALUE: {
+ if( objc!=2 && objc!=3 ){
+ Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE");
+ return TCL_ERROR;
+ }
+ if( objc==3 ){
+ int len;
+ char *zNull = Tcl_GetStringFromObj(objv[2], &len);
+ if( pDb->zNull ){
+ Tcl_Free(pDb->zNull);
+ }
+ if( zNull && len>0 ){
+ pDb->zNull = Tcl_Alloc( len + 1 );
+ strncpy(pDb->zNull, zNull, len);
+ pDb->zNull[len] = '\0';
+ }else{
+ pDb->zNull = 0;
+ }
+ }
+ Tcl_SetObjResult(interp, dbTextToObj(pDb->zNull));
+ break;
+ }
+
/*
** $db last_insert_rowid
**
break;
}
+ /* $db profile ?CALLBACK?
+ **
+ ** Make arrangements to invoke the CALLBACK routine after each SQL statement
+ ** that has run. The text of the SQL and the amount of elapse time are
+ ** appended to CALLBACK before the script is run.
+ */
+ case DB_PROFILE: {
+ if( objc>3 ){
+ Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
+ return TCL_ERROR;
+ }else if( objc==2 ){
+ if( pDb->zProfile ){
+ Tcl_AppendResult(interp, pDb->zProfile, 0);
+ }
+ }else{
+ char *zProfile;
+ int len;
+ if( pDb->zProfile ){
+ Tcl_Free(pDb->zProfile);
+ }
+ zProfile = Tcl_GetStringFromObj(objv[2], &len);
+ if( zProfile && len>0 ){
+ pDb->zProfile = Tcl_Alloc( len + 1 );
+ strcpy(pDb->zProfile, zProfile);
+ }else{
+ pDb->zProfile = 0;
+ }
+#ifndef SQLITE_OMIT_TRACE
+ if( pDb->zProfile ){
+ pDb->interp = interp;
+ sqlite3_profile(pDb->db, DbProfileHandler, pDb);
+ }else{
+ sqlite3_profile(pDb->db, 0, 0);
+ }
+#endif
+ }
+ break;
+ }
+
/*
** $db rekey KEY
**
sqlite3_busy_timeout(pDb->db, ms);
break;
}
-
- /*
- ** $db nullvalue ?STRING?
- **
- ** Change text used when a NULL comes back from the database. If ?STRING?
- ** is not present, then the current string used for NULL is returned.
- ** If STRING is present, then STRING is returned.
- **
- */
- case DB_NULLVALUE: {
- if( objc!=2 && objc!=3 ){
- Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE");
- return TCL_ERROR;
- }
- if( objc==3 ){
- int len;
- char *zNull = Tcl_GetStringFromObj(objv[2], &len);
- if( pDb->zNull ){
- Tcl_Free(pDb->zNull);
- }
- if( zNull && len>0 ){
- pDb->zNull = Tcl_Alloc( len + 1 );
- strncpy(pDb->zNull, zNull, len);
- pDb->zNull[len] = '\0';
- }else{
- pDb->zNull = 0;
- }
- }
- Tcl_SetObjResult(interp, dbTextToObj(pDb->zNull));
- break;
- }
/*
** $db total_changes
}else{
pDb->zTrace = 0;
}
+#ifndef SQLITE_OMIT_TRACE
if( pDb->zTrace ){
pDb->interp = interp;
sqlite3_trace(pDb->db, DbTraceHandler, pDb);
}else{
sqlite3_trace(pDb->db, 0, 0);
}
+#endif
}
break;
}
break;
}
- /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR?
- **
- ** Copy data into table from filename, optionally using SEPARATOR
- ** as column separators. If a column contains a null string, or the
- ** value of NULLINDICATOR, a NULL is inserted for the column.
- ** conflict-algorithm is one of the sqlite conflict algorithms:
- ** rollback, abort, fail, ignore, replace
- ** On success, return the number of lines processed, not necessarily same
- ** as 'db changes' due to conflict-algorithm selected.
- **
- ** This code is basically an implementation/enhancement of
- ** the sqlite3 shell.c ".import" command.
- **
- ** This command usage is equivalent to the sqlite2.x COPY statement,
- ** which imports file data into a table using the PostgreSQL COPY file format:
- ** $db copy $conflit_algo $table_name $filename \t \\N
- */
- case DB_COPY: {
- char *zTable; /* Insert data into this table */
- char *zFile; /* The file from which to extract data */
- char *zConflict; /* The conflict algorithm to use */
- sqlite3_stmt *pStmt; /* A statement */
- int rc; /* Result code */
- int nCol; /* Number of columns in the table */
- int nByte; /* Number of bytes in an SQL string */
- int i, j; /* Loop counters */
- int nSep; /* Number of bytes in zSep[] */
- int nNull; /* Number of bytes in zNull[] */
- char *zSql; /* An SQL statement */
- char *zLine; /* A single line of input from the file */
- char **azCol; /* zLine[] broken up into columns */
- char *zCommit; /* How to commit changes */
- FILE *in; /* The input file */
- int lineno = 0; /* Line number of input file */
- char zLineNum[80]; /* Line number print buffer */
- Tcl_Obj *pResult; /* interp result */
-
- char *zSep;
- char *zNull;
- if( objc<5 || objc>7 ){
- Tcl_WrongNumArgs(interp, 2, objv,
- "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?");
- return TCL_ERROR;
- }
- if( objc>=6 ){
- zSep = Tcl_GetStringFromObj(objv[5], 0);
- }else{
- zSep = "\t";
- }
- if( objc>=7 ){
- zNull = Tcl_GetStringFromObj(objv[6], 0);
- }else{
- zNull = "";
- }
- zConflict = Tcl_GetStringFromObj(objv[2], 0);
- zTable = Tcl_GetStringFromObj(objv[3], 0);
- zFile = Tcl_GetStringFromObj(objv[4], 0);
- nSep = strlen(zSep);
- nNull = strlen(zNull);
- if( nSep==0 ){
- Tcl_AppendResult(interp, "Error: non-null separator required for copy", 0);
- return TCL_ERROR;
- }
- if(sqlite3StrICmp(zConflict, "rollback") != 0 &&
- sqlite3StrICmp(zConflict, "abort" ) != 0 &&
- sqlite3StrICmp(zConflict, "fail" ) != 0 &&
- sqlite3StrICmp(zConflict, "ignore" ) != 0 &&
- sqlite3StrICmp(zConflict, "replace" ) != 0 ) {
- Tcl_AppendResult(interp, "Error: \"", zConflict,
- "\", conflict-algorithm must be one of: rollback, "
- "abort, fail, ignore, or replace", 0);
- return TCL_ERROR;
- }
- zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
- if( zSql==0 ){
- Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0);
- return TCL_ERROR;
- }
- nByte = strlen(zSql);
- rc = sqlite3_prepare(pDb->db, zSql, 0, &pStmt, 0);
- sqlite3_free(zSql);
- if( rc ){
- Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0);
- nCol = 0;
- }else{
- nCol = sqlite3_column_count(pStmt);
- }
- sqlite3_finalize(pStmt);
- if( nCol==0 ) {
- return TCL_ERROR;
- }
- zSql = malloc( nByte + 50 + nCol*2 );
- if( zSql==0 ) {
- Tcl_AppendResult(interp, "Error: can't malloc()", 0);
- return TCL_ERROR;
- }
- sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?",
- zConflict, zTable);
- j = strlen(zSql);
- for(i=1; i<nCol; i++){
- zSql[j++] = ',';
- zSql[j++] = '?';
- }
- zSql[j++] = ')';
- zSql[j] = 0;
- rc = sqlite3_prepare(pDb->db, zSql, 0, &pStmt, 0);
- free(zSql);
- if( rc ){
- Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0);
- sqlite3_finalize(pStmt);
- return TCL_ERROR;
- }
- in = fopen(zFile, "rb");
- if( in==0 ){
- Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL);
- sqlite3_finalize(pStmt);
- return TCL_ERROR;
- }
- azCol = malloc( sizeof(azCol[0])*(nCol+1) );
- if( azCol==0 ) {
- Tcl_AppendResult(interp, "Error: can't malloc()", 0);
- return TCL_ERROR;
- }
- sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0);
- zCommit = "COMMIT";
- while( (zLine = local_getline(0, in))!=0 ){
- char *z;
- i = 0;
- lineno++;
- azCol[0] = zLine;
- for(i=0, z=zLine; *z; z++){
- if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){
- *z = 0;
- i++;
- if( i<nCol ){
- azCol[i] = &z[nSep];
- z += nSep-1;
- }
- }
- }
- if( i+1!=nCol ){
- char *zErr;
- zErr = malloc(200 + strlen(zFile));
- sprintf(zErr,"Error: %s line %d: expected %d columns of data but found %d",
- zFile, lineno, nCol, i+1);
- Tcl_AppendResult(interp, zErr, 0);
- free(zErr);
- zCommit = "ROLLBACK";
- break;
- }
- for(i=0; i<nCol; i++){
- /* check for null data, if so, bind as null */
- if ((nNull>0 && strcmp(azCol[i], zNull)==0) || strlen(azCol[i])==0) {
- sqlite3_bind_null(pStmt, i+1);
- }else{
- sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
- }
- }
- sqlite3_step(pStmt);
- rc = sqlite3_reset(pStmt);
- free(zLine);
- if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), 0);
- zCommit = "ROLLBACK";
- break;
- }
- }
- free(azCol);
- fclose(in);
- sqlite3_finalize(pStmt);
- sqlite3_exec(pDb->db, zCommit, 0, 0, 0);
-
- if( zCommit[0] == 'C' ){
- /* success, set result as number of lines processed */
- pResult = Tcl_GetObjResult(interp);
- Tcl_SetIntObj(pResult, lineno);
- rc = TCL_OK;
- }else{
- /* failure, append lineno where failed */
- sprintf(zLineNum,"%d",lineno);
- Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,0);
- rc = TCL_ERROR;
- }
- break;
- }
-
/* $db version
**
** Return the version string for this database.