| OLD | NEW | 
 | (Empty) | 
|    1 /* |  | 
|    2 ** 2006 August 23 |  | 
|    3 ** |  | 
|    4 ** The author disclaims copyright to this source code.  In place of |  | 
|    5 ** a legal notice, here is a blessing: |  | 
|    6 ** |  | 
|    7 **    May you do good and not evil. |  | 
|    8 **    May you find forgiveness for yourself and forgive others. |  | 
|    9 **    May you share freely, never taking more than you give. |  | 
|   10 ** |  | 
|   11 ************************************************************************* |  | 
|   12 ** Test extension for testing the sqlite3_auto_extension() function. |  | 
|   13 ** |  | 
|   14 ** $Id: test_autoext.c,v 1.5 2008/07/08 02:12:37 drh Exp $ |  | 
|   15 */ |  | 
|   16 #include "tcl.h" |  | 
|   17 #include "sqlite3ext.h" |  | 
|   18  |  | 
|   19 #ifndef SQLITE_OMIT_LOAD_EXTENSION |  | 
|   20 #ifndef SQLITE_CORE |  | 
|   21 static SQLITE_EXTENSION_INIT1 |  | 
|   22 #endif |  | 
|   23  |  | 
|   24 /* |  | 
|   25 ** The sqr() SQL function returns the square of its input value. |  | 
|   26 */ |  | 
|   27 static void sqrFunc( |  | 
|   28   sqlite3_context *context, |  | 
|   29   int argc, |  | 
|   30   sqlite3_value **argv |  | 
|   31 ){ |  | 
|   32   double r = sqlite3_value_double(argv[0]); |  | 
|   33   sqlite3_result_double(context, r*r); |  | 
|   34 } |  | 
|   35  |  | 
|   36 /* |  | 
|   37 ** This is the entry point to register the extension for the sqr() function. |  | 
|   38 */ |  | 
|   39 static int sqr_init( |  | 
|   40   sqlite3 *db,  |  | 
|   41   char **pzErrMsg,  |  | 
|   42   const sqlite3_api_routines *pApi |  | 
|   43 ){ |  | 
|   44   SQLITE_EXTENSION_INIT2(pApi); |  | 
|   45   sqlite3_create_function(db, "sqr", 1, SQLITE_ANY, 0, sqrFunc, 0, 0); |  | 
|   46   return 0; |  | 
|   47 } |  | 
|   48  |  | 
|   49 /* |  | 
|   50 ** The cube() SQL function returns the cube of its input value. |  | 
|   51 */ |  | 
|   52 static void cubeFunc( |  | 
|   53   sqlite3_context *context, |  | 
|   54   int argc, |  | 
|   55   sqlite3_value **argv |  | 
|   56 ){ |  | 
|   57   double r = sqlite3_value_double(argv[0]); |  | 
|   58   sqlite3_result_double(context, r*r*r); |  | 
|   59 } |  | 
|   60  |  | 
|   61 /* |  | 
|   62 ** This is the entry point to register the extension for the cube() function. |  | 
|   63 */ |  | 
|   64 static int cube_init( |  | 
|   65   sqlite3 *db,  |  | 
|   66   char **pzErrMsg,  |  | 
|   67   const sqlite3_api_routines *pApi |  | 
|   68 ){ |  | 
|   69   SQLITE_EXTENSION_INIT2(pApi); |  | 
|   70   sqlite3_create_function(db, "cube", 1, SQLITE_ANY, 0, cubeFunc, 0, 0); |  | 
|   71   return 0; |  | 
|   72 } |  | 
|   73  |  | 
|   74 /* |  | 
|   75 ** This is a broken extension entry point |  | 
|   76 */ |  | 
|   77 static int broken_init( |  | 
|   78   sqlite3 *db,  |  | 
|   79   char **pzErrMsg,  |  | 
|   80   const sqlite3_api_routines *pApi |  | 
|   81 ){ |  | 
|   82   char *zErr; |  | 
|   83   SQLITE_EXTENSION_INIT2(pApi); |  | 
|   84   zErr = sqlite3_mprintf("broken autoext!"); |  | 
|   85   *pzErrMsg = zErr; |  | 
|   86   return 1; |  | 
|   87 } |  | 
|   88  |  | 
|   89 /* |  | 
|   90 ** tclcmd:   sqlite3_auto_extension_sqr |  | 
|   91 ** |  | 
|   92 ** Register the "sqr" extension to be loaded automatically. |  | 
|   93 */ |  | 
|   94 static int autoExtSqrObjCmd( |  | 
|   95   void * clientData, |  | 
|   96   Tcl_Interp *interp, |  | 
|   97   int objc, |  | 
|   98   Tcl_Obj *CONST objv[] |  | 
|   99 ){ |  | 
|  100   int rc = sqlite3_auto_extension((void*)sqr_init); |  | 
|  101   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |  | 
|  102   return SQLITE_OK; |  | 
|  103 } |  | 
|  104  |  | 
|  105 /* |  | 
|  106 ** tclcmd:   sqlite3_auto_extension_cube |  | 
|  107 ** |  | 
|  108 ** Register the "cube" extension to be loaded automatically. |  | 
|  109 */ |  | 
|  110 static int autoExtCubeObjCmd( |  | 
|  111   void * clientData, |  | 
|  112   Tcl_Interp *interp, |  | 
|  113   int objc, |  | 
|  114   Tcl_Obj *CONST objv[] |  | 
|  115 ){ |  | 
|  116   int rc = sqlite3_auto_extension((void*)cube_init); |  | 
|  117   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |  | 
|  118   return SQLITE_OK; |  | 
|  119 } |  | 
|  120  |  | 
|  121 /* |  | 
|  122 ** tclcmd:   sqlite3_auto_extension_broken |  | 
|  123 ** |  | 
|  124 ** Register the broken extension to be loaded automatically. |  | 
|  125 */ |  | 
|  126 static int autoExtBrokenObjCmd( |  | 
|  127   void * clientData, |  | 
|  128   Tcl_Interp *interp, |  | 
|  129   int objc, |  | 
|  130   Tcl_Obj *CONST objv[] |  | 
|  131 ){ |  | 
|  132   int rc = sqlite3_auto_extension((void*)broken_init); |  | 
|  133   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |  | 
|  134   return SQLITE_OK; |  | 
|  135 } |  | 
|  136  |  | 
|  137 #endif /* SQLITE_OMIT_LOAD_EXTENSION */ |  | 
|  138  |  | 
|  139  |  | 
|  140 /* |  | 
|  141 ** tclcmd:   sqlite3_reset_auto_extension |  | 
|  142 ** |  | 
|  143 ** Reset all auto-extensions |  | 
|  144 */ |  | 
|  145 static int resetAutoExtObjCmd( |  | 
|  146   void * clientData, |  | 
|  147   Tcl_Interp *interp, |  | 
|  148   int objc, |  | 
|  149   Tcl_Obj *CONST objv[] |  | 
|  150 ){ |  | 
|  151   sqlite3_reset_auto_extension(); |  | 
|  152   return SQLITE_OK; |  | 
|  153 } |  | 
|  154  |  | 
|  155  |  | 
|  156 /* |  | 
|  157 ** This procedure registers the TCL procs defined in this file. |  | 
|  158 */ |  | 
|  159 int Sqlitetest_autoext_Init(Tcl_Interp *interp){ |  | 
|  160 #ifndef SQLITE_OMIT_LOAD_EXTENSION |  | 
|  161   Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_sqr", |  | 
|  162           autoExtSqrObjCmd, 0, 0); |  | 
|  163   Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_cube", |  | 
|  164           autoExtCubeObjCmd, 0, 0); |  | 
|  165   Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_broken", |  | 
|  166           autoExtBrokenObjCmd, 0, 0); |  | 
|  167 #endif |  | 
|  168   Tcl_CreateObjCommand(interp, "sqlite3_reset_auto_extension", |  | 
|  169           resetAutoExtObjCmd, 0, 0); |  | 
|  170   return TCL_OK; |  | 
|  171 } |  | 
| OLD | NEW |