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