mirror of
https://github.com/sqlite/sqlite.git
synced 2025-07-30 19:03:16 +03:00
Add the "onecolumn" method to the TCL language bindings. (CVS 1076)
FossilOrigin-Name: c7b4c28fbc0bca5645af5750fd353f4eca400b40
This commit is contained in:
@ -11,7 +11,7 @@
|
||||
*************************************************************************
|
||||
** A TCL Interface to SQLite
|
||||
**
|
||||
** $Id: tclsqlite.c,v 1.49 2003/06/06 19:00:42 drh Exp $
|
||||
** $Id: tclsqlite.c,v 1.50 2003/08/19 14:31:02 drh Exp $
|
||||
*/
|
||||
#ifndef NO_TCL /* Omit this whole file if TCL is unavailable */
|
||||
|
||||
@ -248,6 +248,35 @@ static int DbEvalCallback2(
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** This is a second alternative callback for database queries. A the
|
||||
** first column of the first row of the result is made the TCL result.
|
||||
*/
|
||||
static int DbEvalCallback3(
|
||||
void *clientData, /* An instance of CallbackData */
|
||||
int nCol, /* Number of columns in the result */
|
||||
char ** azCol, /* Data for each column */
|
||||
char ** azN /* Name for each column */
|
||||
){
|
||||
Tcl_Interp *interp = (Tcl_Interp*)clientData;
|
||||
Tcl_Obj *pElem;
|
||||
if( azCol==0 ) return 1;
|
||||
if( nCol==0 ) return 1;
|
||||
#ifdef UTF_TRANSLATION_NEEDED
|
||||
{
|
||||
Tcl_DString dCol;
|
||||
Tcl_DStringInit(&dCol);
|
||||
Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol);
|
||||
pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
|
||||
Tcl_DStringFree(&dCol);
|
||||
}
|
||||
#else
|
||||
pElem = Tcl_NewStringObj(azCol[0], -1);
|
||||
#endif
|
||||
Tcl_SetObjResult(interp, pElem);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
** Called when the command is deleted.
|
||||
*/
|
||||
@ -427,13 +456,14 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
|
||||
"authorizer", "busy", "changes",
|
||||
"close", "complete", "errorcode",
|
||||
"eval", "function", "last_insert_rowid",
|
||||
"timeout", "trace", 0
|
||||
"onecolumn", "timeout", "trace",
|
||||
0
|
||||
};
|
||||
enum DB_enum {
|
||||
DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
|
||||
DB_CLOSE, DB_COMPLETE, DB_ERRORCODE,
|
||||
DB_EVAL, DB_FUNCTION, DB_LAST_INSERT_ROWID,
|
||||
DB_TIMEOUT, DB_TRACE,
|
||||
DB_ONECOLUMN, DB_TIMEOUT, DB_TRACE,
|
||||
};
|
||||
|
||||
if( objc<2 ){
|
||||
@ -715,6 +745,34 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
** $db onecolumn SQL
|
||||
**
|
||||
** Return a single column from a single row of the given SQL query.
|
||||
*/
|
||||
case DB_ONECOLUMN: {
|
||||
int rc;
|
||||
char *zSql;
|
||||
char *zErrMsg = 0;
|
||||
if( objc!=3 ){
|
||||
Tcl_WrongNumArgs(interp, 2, objv, "SQL");
|
||||
return TCL_ERROR;
|
||||
}
|
||||
zSql = Tcl_GetStringFromObj(objv[2], 0);
|
||||
rc = sqlite_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
|
||||
if( rc==SQLITE_ABORT ){
|
||||
/* Do nothing. This is normal. */
|
||||
}else if( zErrMsg ){
|
||||
Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
|
||||
free(zErrMsg);
|
||||
rc = TCL_ERROR;
|
||||
}else if( rc!=SQLITE_OK ){
|
||||
Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
|
||||
rc = TCL_ERROR;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
** $db timeout MILLESECONDS
|
||||
**
|
||||
|
Reference in New Issue
Block a user