Fixes default log output to console for macOS
[sqlcipher.git] / src / test_autoext.c
blobe23e41a08a897988e9284cdf501578ed4b8da263
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.
11 *************************************************************************
12 ** Test extension for testing the sqlite3_auto_extension() function.
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"
24 #ifndef SQLITE_OMIT_LOAD_EXTENSION
25 SQLITE_EXTENSION_INIT1
28 ** The sqr() SQL function returns the square of its input value.
30 static void sqrFunc(
31 sqlite3_context *context,
32 int argc,
33 sqlite3_value **argv
35 double r = sqlite3_value_double(argv[0]);
36 sqlite3_result_double(context, r*r);
40 ** This is the entry point to register the extension for the sqr() function.
42 static int sqr_init(
43 sqlite3 *db,
44 char **pzErrMsg,
45 const sqlite3_api_routines *pApi
47 SQLITE_EXTENSION_INIT2(pApi);
48 sqlite3_create_function(db, "sqr", 1, SQLITE_ANY, 0, sqrFunc, 0, 0);
49 return 0;
53 ** The cube() SQL function returns the cube of its input value.
55 static void cubeFunc(
56 sqlite3_context *context,
57 int argc,
58 sqlite3_value **argv
60 double r = sqlite3_value_double(argv[0]);
61 sqlite3_result_double(context, r*r*r);
65 ** This is the entry point to register the extension for the cube() function.
67 static int cube_init(
68 sqlite3 *db,
69 char **pzErrMsg,
70 const sqlite3_api_routines *pApi
72 SQLITE_EXTENSION_INIT2(pApi);
73 sqlite3_create_function(db, "cube", 1, SQLITE_ANY, 0, cubeFunc, 0, 0);
74 return 0;
78 ** This is a broken extension entry point
80 static int broken_init(
81 sqlite3 *db,
82 char **pzErrMsg,
83 const sqlite3_api_routines *pApi
85 char *zErr;
86 SQLITE_EXTENSION_INIT2(pApi);
87 zErr = sqlite3_mprintf("broken autoext!");
88 *pzErrMsg = zErr;
89 return 1;
93 ** tclcmd: sqlite3_auto_extension_sqr
95 ** Register the "sqr" extension to be loaded automatically.
97 static int SQLITE_TCLAPI autoExtSqrObjCmd(
98 void * clientData,
99 Tcl_Interp *interp,
100 int objc,
101 Tcl_Obj *CONST objv[]
103 int rc = sqlite3_auto_extension((void(*)(void))sqr_init);
104 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
105 return SQLITE_OK;
109 ** tclcmd: sqlite3_cancel_auto_extension_sqr
111 ** Unregister the "sqr" extension.
113 static int SQLITE_TCLAPI cancelAutoExtSqrObjCmd(
114 void * clientData,
115 Tcl_Interp *interp,
116 int objc,
117 Tcl_Obj *CONST objv[]
119 int rc = sqlite3_cancel_auto_extension((void(*)(void))sqr_init);
120 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
121 return SQLITE_OK;
125 ** tclcmd: sqlite3_auto_extension_cube
127 ** Register the "cube" extension to be loaded automatically.
129 static int SQLITE_TCLAPI autoExtCubeObjCmd(
130 void * clientData,
131 Tcl_Interp *interp,
132 int objc,
133 Tcl_Obj *CONST objv[]
135 int rc = sqlite3_auto_extension((void(*)(void))cube_init);
136 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
137 return SQLITE_OK;
141 ** tclcmd: sqlite3_cancel_auto_extension_cube
143 ** Unregister the "cube" extension.
145 static int SQLITE_TCLAPI cancelAutoExtCubeObjCmd(
146 void * clientData,
147 Tcl_Interp *interp,
148 int objc,
149 Tcl_Obj *CONST objv[]
151 int rc = sqlite3_cancel_auto_extension((void(*)(void))cube_init);
152 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
153 return SQLITE_OK;
157 ** tclcmd: sqlite3_auto_extension_broken
159 ** Register the broken extension to be loaded automatically.
161 static int SQLITE_TCLAPI autoExtBrokenObjCmd(
162 void * clientData,
163 Tcl_Interp *interp,
164 int objc,
165 Tcl_Obj *CONST objv[]
167 int rc = sqlite3_auto_extension((void(*)(void))broken_init);
168 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
169 return SQLITE_OK;
173 ** tclcmd: sqlite3_cancel_auto_extension_broken
175 ** Unregister the broken extension.
177 static int SQLITE_TCLAPI cancelAutoExtBrokenObjCmd(
178 void * clientData,
179 Tcl_Interp *interp,
180 int objc,
181 Tcl_Obj *CONST objv[]
183 int rc = sqlite3_cancel_auto_extension((void(*)(void))broken_init);
184 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
185 return SQLITE_OK;
188 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
192 ** tclcmd: sqlite3_reset_auto_extension
194 ** Reset all auto-extensions
196 static int SQLITE_TCLAPI resetAutoExtObjCmd(
197 void * clientData,
198 Tcl_Interp *interp,
199 int objc,
200 Tcl_Obj *CONST objv[]
202 sqlite3_reset_auto_extension();
203 return SQLITE_OK;
208 ** This procedure registers the TCL procs defined in this file.
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;