xref: /sqlite-3.40.0/src/test_autoext.c (revision 52b1dbb5)
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 #endif
19 #include "sqlite3ext.h"
20 
21 #ifndef SQLITE_OMIT_LOAD_EXTENSION
22 SQLITE_EXTENSION_INIT1
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_cancel_auto_extension_sqr
107 **
108 ** Unregister the "sqr" extension.
109 */
110 static int cancelAutoExtSqrObjCmd(
111   void * clientData,
112   Tcl_Interp *interp,
113   int objc,
114   Tcl_Obj *CONST objv[]
115 ){
116   int rc = sqlite3_cancel_auto_extension((void*)sqr_init);
117   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
118   return SQLITE_OK;
119 }
120 
121 /*
122 ** tclcmd:   sqlite3_auto_extension_cube
123 **
124 ** Register the "cube" extension to be loaded automatically.
125 */
126 static int autoExtCubeObjCmd(
127   void * clientData,
128   Tcl_Interp *interp,
129   int objc,
130   Tcl_Obj *CONST objv[]
131 ){
132   int rc = sqlite3_auto_extension((void*)cube_init);
133   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
134   return SQLITE_OK;
135 }
136 
137 /*
138 ** tclcmd:   sqlite3_cancel_auto_extension_cube
139 **
140 ** Unregister the "cube" extension.
141 */
142 static int cancelAutoExtCubeObjCmd(
143   void * clientData,
144   Tcl_Interp *interp,
145   int objc,
146   Tcl_Obj *CONST objv[]
147 ){
148   int rc = sqlite3_cancel_auto_extension((void*)cube_init);
149   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
150   return SQLITE_OK;
151 }
152 
153 /*
154 ** tclcmd:   sqlite3_auto_extension_broken
155 **
156 ** Register the broken extension to be loaded automatically.
157 */
158 static int autoExtBrokenObjCmd(
159   void * clientData,
160   Tcl_Interp *interp,
161   int objc,
162   Tcl_Obj *CONST objv[]
163 ){
164   int rc = sqlite3_auto_extension((void*)broken_init);
165   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
166   return SQLITE_OK;
167 }
168 
169 /*
170 ** tclcmd:   sqlite3_cancel_auto_extension_broken
171 **
172 ** Unregister the broken extension.
173 */
174 static int cancelAutoExtBrokenObjCmd(
175   void * clientData,
176   Tcl_Interp *interp,
177   int objc,
178   Tcl_Obj *CONST objv[]
179 ){
180   int rc = sqlite3_cancel_auto_extension((void*)broken_init);
181   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
182   return SQLITE_OK;
183 }
184 
185 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
186 
187 
188 /*
189 ** tclcmd:   sqlite3_reset_auto_extension
190 **
191 ** Reset all auto-extensions
192 */
193 static int resetAutoExtObjCmd(
194   void * clientData,
195   Tcl_Interp *interp,
196   int objc,
197   Tcl_Obj *CONST objv[]
198 ){
199   sqlite3_reset_auto_extension();
200   return SQLITE_OK;
201 }
202 
203 
204 /*
205 ** This procedure registers the TCL procs defined in this file.
206 */
207 int Sqlitetest_autoext_Init(Tcl_Interp *interp){
208 #ifndef SQLITE_OMIT_LOAD_EXTENSION
209   Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_sqr",
210           autoExtSqrObjCmd, 0, 0);
211   Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_cube",
212           autoExtCubeObjCmd, 0, 0);
213   Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_broken",
214           autoExtBrokenObjCmd, 0, 0);
215   Tcl_CreateObjCommand(interp, "sqlite3_cancel_auto_extension_sqr",
216           cancelAutoExtSqrObjCmd, 0, 0);
217   Tcl_CreateObjCommand(interp, "sqlite3_cancel_auto_extension_cube",
218           cancelAutoExtCubeObjCmd, 0, 0);
219   Tcl_CreateObjCommand(interp, "sqlite3_cancel_auto_extension_broken",
220           cancelAutoExtBrokenObjCmd, 0, 0);
221 #endif
222   Tcl_CreateObjCommand(interp, "sqlite3_reset_auto_extension",
223           resetAutoExtObjCmd, 0, 0);
224   return TCL_OK;
225 }
226