SQLite

Check-in [b5d5f0ad71]
Login

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

Overview
Comment:Alter the Tcl eval sub-command so that it supports blobs. (CVS 1473)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: b5d5f0ad717ef43a9714dd1cc40e20d1b94a3e58
User & Date: danielk1977 2004-05-27 12:11:32.000
Context
2004-05-27
13:35
A couple of test cases and fixes for blob literals. (CVS 1474) (check-in: 6d552af67c user: danielk1977 tags: trunk)
12:11
Alter the Tcl eval sub-command so that it supports blobs. (CVS 1473) (check-in: b5d5f0ad71 user: danielk1977 tags: trunk)
10:30
A few more bugfixes. Test cases pass now. (CVS 1472) (check-in: c9e3015faf user: danielk1977 tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/tclsqlite.c.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
** 2001 September 15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** A TCL Interface to SQLite
**
** $Id: tclsqlite.c,v 1.73 2004/05/26 23:25:31 drh Exp $
*/
#ifndef NO_TCL     /* Omit this whole file if TCL is unavailable */

#include "sqliteInt.h"
#include "tcl.h"
#include <stdlib.h>
#include <string.h>













|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
** 2001 September 15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** A TCL Interface to SQLite
**
** $Id: tclsqlite.c,v 1.74 2004/05/27 12:11:32 danielk1977 Exp $
*/
#ifndef NO_TCL     /* Omit this whole file if TCL is unavailable */

#include "sqliteInt.h"
#include "tcl.h"
#include <stdlib.h>
#include <string.h>
53
54
55
56
57
58
59

60
61
62
63
64
65
66
  char *zBusy;          /* The busy callback routine */
  char *zCommit;        /* The commit hook callback routine */
  char *zTrace;         /* The trace callback routine */
  char *zProgress;      /* The progress callback routine */
  char *zAuth;          /* The authorization callback routine */
  SqlFunc *pFunc;       /* List of SQL functions */
  int rc;               /* Return code of most recent sqlite3_exec() */

};

/*
** An instance of this structure passes information thru the sqlite
** logic from the original TCL command into the callback routine.
*/
typedef struct CallbackData CallbackData;







>







53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  char *zBusy;          /* The busy callback routine */
  char *zCommit;        /* The commit hook callback routine */
  char *zTrace;         /* The trace callback routine */
  char *zProgress;      /* The progress callback routine */
  char *zAuth;          /* The authorization callback routine */
  SqlFunc *pFunc;       /* List of SQL functions */
  int rc;               /* Return code of most recent sqlite3_exec() */
  int nChange;         /* Database changes for the most recent eval */
};

/*
** An instance of this structure passes information thru the sqlite
** logic from the original TCL command into the callback routine.
*/
typedef struct CallbackData CallbackData;
656
657
658
659
660
661
662
663

664
665
666
667
668
669
670
  case DB_CHANGES: {
    Tcl_Obj *pResult;
    int nChange;
    if( objc!=2 ){
      Tcl_WrongNumArgs(interp, 2, objv, "");
      return TCL_ERROR;
    }
    nChange = sqlite3_changes(pDb->db);

    pResult = Tcl_GetObjResult(interp);
    Tcl_SetIntObj(pResult, nChange);
    break;
  }

  /*
  **     $db last_statement_changes







|
>







657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
  case DB_CHANGES: {
    Tcl_Obj *pResult;
    int nChange;
    if( objc!=2 ){
      Tcl_WrongNumArgs(interp, 2, objv, "");
      return TCL_ERROR;
    }
    /* nChange = sqlite3_changes(pDb->db); */
    nChange = pDb->nChange;
    pResult = Tcl_GetObjResult(interp);
    Tcl_SetIntObj(pResult, nChange);
    break;
  }

  /*
  **     $db last_statement_changes
767
768
769
770
771
772
773






























































































774
775
776
777
778
779
780
  **
  ** 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.
  */






























































































  case DB_EVAL: {
    CallbackData cbData;
    char *zErrMsg;
    char *zSql;
#ifdef UTF_TRANSLATION_NEEDED
    Tcl_DString dSql;
    int i;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
  **
  ** 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.
  */
  case DB_EVAL: {
    char const *zSql;
    char const *zLeft;
    sqlite3_stmt *pStmt;
    Tcl_Obj *pRet = 0;

    if( objc!=5 && objc!=3 ){
      Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
      return TCL_ERROR;
    }

    pDb->nChange = 0;
    zSql = Tcl_GetStringFromObj(objv[2], 0);
    while( zSql[0] ){
      int i;
  
      if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
        Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_STATIC);
        rc = TCL_ERROR;
        break;
      }
  
      if( pStmt && objc==5 ){
        Tcl_Obj *pColList = Tcl_NewObj();
        Tcl_IncrRefCount(pColList);

        for(i=0; i<sqlite3_column_count(pStmt); i++){
          Tcl_ListObjAppendElement(interp, pColList,
              Tcl_NewStringObj(sqlite3_column_name(pStmt, i), -1)
          );
        }
        Tcl_ObjSetVar2(interp,objv[3],Tcl_NewStringObj("*",-1),pColList,0);
      }

      while( pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
        for(i=0; i<sqlite3_column_count(pStmt); i++){
          Tcl_Obj *pVal;
          
          /* Set pVal to contain the i'th column of this row. */
          if( SQLITE3_BLOB!=sqlite3_column_type(pStmt, i) ){
            pVal = Tcl_NewStringObj(sqlite3_column_text(pStmt, i), -1);
          }else{
            pVal = Tcl_NewByteArrayObj(
              sqlite3_column_blob(pStmt, i),
              sqlite3_column_bytes(pStmt, i)
            );
          }
  
          if( objc==5 ){
            Tcl_Obj *pName = Tcl_NewStringObj(sqlite3_column_name(pStmt, i), -1);
            Tcl_IncrRefCount(pName);
            if( !strcmp("", Tcl_GetString(objv[3])) ){
              Tcl_ObjSetVar2(interp, pName, 0, pVal, 0);
            }else{
              Tcl_ObjSetVar2(interp, objv[3], pName, pVal, 0);
            }
            Tcl_DecrRefCount(pName);
          }else{
            if( !pRet ){
              pRet = Tcl_NewObj();
              Tcl_IncrRefCount(pRet);
            }
            Tcl_ListObjAppendElement(interp, pRet, pVal);
          }
        }
  
        if( objc==5 ){
          rc = Tcl_EvalObjEx(interp, objv[4], 0);
          if( rc!=TCL_ERROR ) rc = TCL_OK;
        }
      }
       
      if( pStmt && SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){
        continue;
      }
  
      if( pStmt && SQLITE_OK!=sqlite3_errcode(pDb->db) ){
        Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_STATIC);
        rc = TCL_ERROR;
        break;
      }

      pDb->nChange += sqlite3_changes(pDb->db);
      zSql = zLeft;
    }

    if( rc==TCL_OK && pRet ){
      Tcl_SetObjResult(interp, pRet);
      Tcl_DecrRefCount(pRet);
    }

    break;
  }
#if 0
  case DB_EVAL: {
    CallbackData cbData;
    char *zErrMsg;
    char *zSql;
#ifdef UTF_TRANSLATION_NEEDED
    Tcl_DString dSql;
    int i;
835
836
837
838
839
840
841

842
843
844
845
846
847
848
      }
      free(cbData.azColName);
      cbData.azColName = 0;
    }
#endif
    return rc;
  }


  /*
  **     $db function NAME SCRIPT
  **
  ** Create a new SQL function called NAME.  Whenever that function is
  ** called, invoke SCRIPT to evaluate the function.
  */







>







931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
      }
      free(cbData.azColName);
      cbData.azColName = 0;
    }
#endif
    return rc;
  }
#endif

  /*
  **     $db function NAME SCRIPT
  **
  ** Create a new SQL function called NAME.  Whenever that function is
  ** called, invoke SCRIPT to evaluate the function.
  */