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 #if defined(INCLUDE_SQLITE_TCL_H) 15 # include "sqlite_tcl.h" 16 #else 17 # include "tcl.h" 18 # ifndef SQLITE_TCLAPI 19 # define SQLITE_TCLAPI 20 # endif 21 #endif 22 #include "sqlite3ext.h" 23 24 #ifndef SQLITE_OMIT_LOAD_EXTENSION 25 SQLITE_EXTENSION_INIT1 26 27 /* 28 ** The sqr() SQL function returns the square of its input value. 29 */ 30 static void sqrFunc( 31 sqlite3_context *context, 32 int argc, 33 sqlite3_value **argv 34 ){ 35 double r = sqlite3_value_double(argv[0]); 36 sqlite3_result_double(context, r*r); 37 } 38 39 /* 40 ** This is the entry point to register the extension for the sqr() function. 41 */ 42 static int sqr_init( 43 sqlite3 *db, 44 char **pzErrMsg, 45 const sqlite3_api_routines *pApi 46 ){ 47 SQLITE_EXTENSION_INIT2(pApi); 48 sqlite3_create_function(db, "sqr", 1, SQLITE_ANY, 0, sqrFunc, 0, 0); 49 return 0; 50 } 51 52 /* 53 ** The cube() SQL function returns the cube of its input value. 54 */ 55 static void cubeFunc( 56 sqlite3_context *context, 57 int argc, 58 sqlite3_value **argv 59 ){ 60 double r = sqlite3_value_double(argv[0]); 61 sqlite3_result_double(context, r*r*r); 62 } 63 64 /* 65 ** This is the entry point to register the extension for the cube() function. 66 */ 67 static int cube_init( 68 sqlite3 *db, 69 char **pzErrMsg, 70 const sqlite3_api_routines *pApi 71 ){ 72 SQLITE_EXTENSION_INIT2(pApi); 73 sqlite3_create_function(db, "cube", 1, SQLITE_ANY, 0, cubeFunc, 0, 0); 74 return 0; 75 } 76 77 /* 78 ** This is a broken extension entry point 79 */ 80 static int broken_init( 81 sqlite3 *db, 82 char **pzErrMsg, 83 const sqlite3_api_routines *pApi 84 ){ 85 char *zErr; 86 SQLITE_EXTENSION_INIT2(pApi); 87 zErr = sqlite3_mprintf("broken autoext!"); 88 *pzErrMsg = zErr; 89 return 1; 90 } 91 92 /* 93 ** tclcmd: sqlite3_auto_extension_sqr 94 ** 95 ** Register the "sqr" extension to be loaded automatically. 96 */ 97 static int SQLITE_TCLAPI autoExtSqrObjCmd( 98 void * clientData, 99 Tcl_Interp *interp, 100 int objc, 101 Tcl_Obj *CONST objv[] 102 ){ 103 int rc = sqlite3_auto_extension((void(*)(void))sqr_init); 104 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 105 return SQLITE_OK; 106 } 107 108 /* 109 ** tclcmd: sqlite3_cancel_auto_extension_sqr 110 ** 111 ** Unregister the "sqr" extension. 112 */ 113 static int SQLITE_TCLAPI cancelAutoExtSqrObjCmd( 114 void * clientData, 115 Tcl_Interp *interp, 116 int objc, 117 Tcl_Obj *CONST objv[] 118 ){ 119 int rc = sqlite3_cancel_auto_extension((void(*)(void))sqr_init); 120 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 121 return SQLITE_OK; 122 } 123 124 /* 125 ** tclcmd: sqlite3_auto_extension_cube 126 ** 127 ** Register the "cube" extension to be loaded automatically. 128 */ 129 static int SQLITE_TCLAPI autoExtCubeObjCmd( 130 void * clientData, 131 Tcl_Interp *interp, 132 int objc, 133 Tcl_Obj *CONST objv[] 134 ){ 135 int rc = sqlite3_auto_extension((void(*)(void))cube_init); 136 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 137 return SQLITE_OK; 138 } 139 140 /* 141 ** tclcmd: sqlite3_cancel_auto_extension_cube 142 ** 143 ** Unregister the "cube" extension. 144 */ 145 static int SQLITE_TCLAPI cancelAutoExtCubeObjCmd( 146 void * clientData, 147 Tcl_Interp *interp, 148 int objc, 149 Tcl_Obj *CONST objv[] 150 ){ 151 int rc = sqlite3_cancel_auto_extension((void(*)(void))cube_init); 152 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 153 return SQLITE_OK; 154 } 155 156 /* 157 ** tclcmd: sqlite3_auto_extension_broken 158 ** 159 ** Register the broken extension to be loaded automatically. 160 */ 161 static int SQLITE_TCLAPI autoExtBrokenObjCmd( 162 void * clientData, 163 Tcl_Interp *interp, 164 int objc, 165 Tcl_Obj *CONST objv[] 166 ){ 167 int rc = sqlite3_auto_extension((void(*)(void))broken_init); 168 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 169 return SQLITE_OK; 170 } 171 172 /* 173 ** tclcmd: sqlite3_cancel_auto_extension_broken 174 ** 175 ** Unregister the broken extension. 176 */ 177 static int SQLITE_TCLAPI cancelAutoExtBrokenObjCmd( 178 void * clientData, 179 Tcl_Interp *interp, 180 int objc, 181 Tcl_Obj *CONST objv[] 182 ){ 183 int rc = sqlite3_cancel_auto_extension((void(*)(void))broken_init); 184 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 185 return SQLITE_OK; 186 } 187 188 #endif /* SQLITE_OMIT_LOAD_EXTENSION */ 189 190 191 /* 192 ** tclcmd: sqlite3_reset_auto_extension 193 ** 194 ** Reset all auto-extensions 195 */ 196 static int SQLITE_TCLAPI resetAutoExtObjCmd( 197 void * clientData, 198 Tcl_Interp *interp, 199 int objc, 200 Tcl_Obj *CONST objv[] 201 ){ 202 sqlite3_reset_auto_extension(); 203 return SQLITE_OK; 204 } 205 206 207 /* 208 ** This procedure registers the TCL procs defined in this file. 209 */ 210 int Sqlitetest_autoext_Init(Tcl_Interp *interp){ 211 #ifndef SQLITE_OMIT_LOAD_EXTENSION 212 Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_sqr", 213 autoExtSqrObjCmd, 0, 0); 214 Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_cube", 215 autoExtCubeObjCmd, 0, 0); 216 Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_broken", 217 autoExtBrokenObjCmd, 0, 0); 218 Tcl_CreateObjCommand(interp, "sqlite3_cancel_auto_extension_sqr", 219 cancelAutoExtSqrObjCmd, 0, 0); 220 Tcl_CreateObjCommand(interp, "sqlite3_cancel_auto_extension_cube", 221 cancelAutoExtCubeObjCmd, 0, 0); 222 Tcl_CreateObjCommand(interp, "sqlite3_cancel_auto_extension_broken", 223 cancelAutoExtBrokenObjCmd, 0, 0); 224 #endif 225 Tcl_CreateObjCommand(interp, "sqlite3_reset_auto_extension", 226 resetAutoExtObjCmd, 0, 0); 227 return TCL_OK; 228 } 229