drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 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. |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 13 | */ |
mistachkin | 52b1dbb | 2016-07-28 14:37:04 +0000 | [diff] [blame] | 14 | #if defined(INCLUDE_SQLITE_TCL_H) |
| 15 | # include "sqlite_tcl.h" |
| 16 | #else |
| 17 | # include "tcl.h" |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 18 | # ifndef SQLITE_TCLAPI |
| 19 | # define SQLITE_TCLAPI |
| 20 | # endif |
mistachkin | 52b1dbb | 2016-07-28 14:37:04 +0000 | [diff] [blame] | 21 | #endif |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 22 | #include "sqlite3ext.h" |
mlcreech | 9323f76 | 2008-03-19 23:52:34 +0000 | [diff] [blame] | 23 | |
| 24 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 08ef8d7 | 2013-04-19 01:23:06 +0000 | [diff] [blame] | 25 | SQLITE_EXTENSION_INIT1 |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 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 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 97 | static int SQLITE_TCLAPI autoExtSqrObjCmd( |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 98 | void * clientData, |
| 99 | Tcl_Interp *interp, |
| 100 | int objc, |
| 101 | Tcl_Obj *CONST objv[] |
| 102 | ){ |
drh | 32c83c8 | 2016-08-01 14:35:48 +0000 | [diff] [blame] | 103 | int rc = sqlite3_auto_extension((void(*)(void))sqr_init); |
drh | c8d7567 | 2008-07-08 02:12:37 +0000 | [diff] [blame] | 104 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 105 | return SQLITE_OK; |
| 106 | } |
| 107 | |
| 108 | /* |
drh | 425e27d | 2013-07-15 17:02:28 +0000 | [diff] [blame] | 109 | ** tclcmd: sqlite3_cancel_auto_extension_sqr |
| 110 | ** |
| 111 | ** Unregister the "sqr" extension. |
| 112 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 113 | static int SQLITE_TCLAPI cancelAutoExtSqrObjCmd( |
drh | 425e27d | 2013-07-15 17:02:28 +0000 | [diff] [blame] | 114 | void * clientData, |
| 115 | Tcl_Interp *interp, |
| 116 | int objc, |
| 117 | Tcl_Obj *CONST objv[] |
| 118 | ){ |
drh | 32c83c8 | 2016-08-01 14:35:48 +0000 | [diff] [blame] | 119 | int rc = sqlite3_cancel_auto_extension((void(*)(void))sqr_init); |
drh | 425e27d | 2013-07-15 17:02:28 +0000 | [diff] [blame] | 120 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 121 | return SQLITE_OK; |
| 122 | } |
| 123 | |
| 124 | /* |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 125 | ** tclcmd: sqlite3_auto_extension_cube |
| 126 | ** |
| 127 | ** Register the "cube" extension to be loaded automatically. |
| 128 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 129 | static int SQLITE_TCLAPI autoExtCubeObjCmd( |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 130 | void * clientData, |
| 131 | Tcl_Interp *interp, |
| 132 | int objc, |
| 133 | Tcl_Obj *CONST objv[] |
| 134 | ){ |
drh | 32c83c8 | 2016-08-01 14:35:48 +0000 | [diff] [blame] | 135 | int rc = sqlite3_auto_extension((void(*)(void))cube_init); |
drh | c8d7567 | 2008-07-08 02:12:37 +0000 | [diff] [blame] | 136 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 137 | return SQLITE_OK; |
| 138 | } |
| 139 | |
| 140 | /* |
drh | 425e27d | 2013-07-15 17:02:28 +0000 | [diff] [blame] | 141 | ** tclcmd: sqlite3_cancel_auto_extension_cube |
| 142 | ** |
| 143 | ** Unregister the "cube" extension. |
| 144 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 145 | static int SQLITE_TCLAPI cancelAutoExtCubeObjCmd( |
drh | 425e27d | 2013-07-15 17:02:28 +0000 | [diff] [blame] | 146 | void * clientData, |
| 147 | Tcl_Interp *interp, |
| 148 | int objc, |
| 149 | Tcl_Obj *CONST objv[] |
| 150 | ){ |
drh | 32c83c8 | 2016-08-01 14:35:48 +0000 | [diff] [blame] | 151 | int rc = sqlite3_cancel_auto_extension((void(*)(void))cube_init); |
drh | 425e27d | 2013-07-15 17:02:28 +0000 | [diff] [blame] | 152 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 153 | return SQLITE_OK; |
| 154 | } |
| 155 | |
| 156 | /* |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 157 | ** tclcmd: sqlite3_auto_extension_broken |
| 158 | ** |
| 159 | ** Register the broken extension to be loaded automatically. |
| 160 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 161 | static int SQLITE_TCLAPI autoExtBrokenObjCmd( |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 162 | void * clientData, |
| 163 | Tcl_Interp *interp, |
| 164 | int objc, |
| 165 | Tcl_Obj *CONST objv[] |
| 166 | ){ |
drh | 32c83c8 | 2016-08-01 14:35:48 +0000 | [diff] [blame] | 167 | int rc = sqlite3_auto_extension((void(*)(void))broken_init); |
drh | c8d7567 | 2008-07-08 02:12:37 +0000 | [diff] [blame] | 168 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 169 | return SQLITE_OK; |
| 170 | } |
| 171 | |
drh | 425e27d | 2013-07-15 17:02:28 +0000 | [diff] [blame] | 172 | /* |
| 173 | ** tclcmd: sqlite3_cancel_auto_extension_broken |
| 174 | ** |
| 175 | ** Unregister the broken extension. |
| 176 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 177 | static int SQLITE_TCLAPI cancelAutoExtBrokenObjCmd( |
drh | 425e27d | 2013-07-15 17:02:28 +0000 | [diff] [blame] | 178 | void * clientData, |
| 179 | Tcl_Interp *interp, |
| 180 | int objc, |
| 181 | Tcl_Obj *CONST objv[] |
| 182 | ){ |
drh | 32c83c8 | 2016-08-01 14:35:48 +0000 | [diff] [blame] | 183 | int rc = sqlite3_cancel_auto_extension((void(*)(void))broken_init); |
drh | 425e27d | 2013-07-15 17:02:28 +0000 | [diff] [blame] | 184 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 185 | return SQLITE_OK; |
| 186 | } |
| 187 | |
mlcreech | 9323f76 | 2008-03-19 23:52:34 +0000 | [diff] [blame] | 188 | #endif /* SQLITE_OMIT_LOAD_EXTENSION */ |
| 189 | |
| 190 | |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 191 | /* |
| 192 | ** tclcmd: sqlite3_reset_auto_extension |
| 193 | ** |
| 194 | ** Reset all auto-extensions |
| 195 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 196 | static int SQLITE_TCLAPI resetAutoExtObjCmd( |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 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 | |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 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); |
drh | 425e27d | 2013-07-15 17:02:28 +0000 | [diff] [blame] | 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); |
drh | d3f4964 | 2013-08-06 18:35:31 +0000 | [diff] [blame] | 224 | #endif |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 225 | Tcl_CreateObjCommand(interp, "sqlite3_reset_auto_extension", |
| 226 | resetAutoExtObjCmd, 0, 0); |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 227 | return TCL_OK; |
| 228 | } |