blob: e23e41a08a897988e9284cdf501578ed4b8da263 [file] [log] [blame]
drh1409be62006-08-23 20:07:20 +00001/*
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.
drh1409be62006-08-23 20:07:20 +000013*/
mistachkin52b1dbb2016-07-28 14:37:04 +000014#if defined(INCLUDE_SQLITE_TCL_H)
15# include "sqlite_tcl.h"
16#else
17# include "tcl.h"
mistachkin7617e4a2016-07-28 17:11:20 +000018# ifndef SQLITE_TCLAPI
19# define SQLITE_TCLAPI
20# endif
mistachkin52b1dbb2016-07-28 14:37:04 +000021#endif
drh1409be62006-08-23 20:07:20 +000022#include "sqlite3ext.h"
mlcreech9323f762008-03-19 23:52:34 +000023
24#ifndef SQLITE_OMIT_LOAD_EXTENSION
drh08ef8d72013-04-19 01:23:06 +000025SQLITE_EXTENSION_INIT1
drh1409be62006-08-23 20:07:20 +000026
27/*
28** The sqr() SQL function returns the square of its input value.
29*/
30static 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*/
42static 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*/
55static 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*/
67static 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*/
80static 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*/
mistachkin7617e4a2016-07-28 17:11:20 +000097static int SQLITE_TCLAPI autoExtSqrObjCmd(
drh1409be62006-08-23 20:07:20 +000098 void * clientData,
99 Tcl_Interp *interp,
100 int objc,
101 Tcl_Obj *CONST objv[]
102){
drh32c83c82016-08-01 14:35:48 +0000103 int rc = sqlite3_auto_extension((void(*)(void))sqr_init);
drhc8d75672008-07-08 02:12:37 +0000104 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
drh1409be62006-08-23 20:07:20 +0000105 return SQLITE_OK;
106}
107
108/*
drh425e27d2013-07-15 17:02:28 +0000109** tclcmd: sqlite3_cancel_auto_extension_sqr
110**
111** Unregister the "sqr" extension.
112*/
mistachkin7617e4a2016-07-28 17:11:20 +0000113static int SQLITE_TCLAPI cancelAutoExtSqrObjCmd(
drh425e27d2013-07-15 17:02:28 +0000114 void * clientData,
115 Tcl_Interp *interp,
116 int objc,
117 Tcl_Obj *CONST objv[]
118){
drh32c83c82016-08-01 14:35:48 +0000119 int rc = sqlite3_cancel_auto_extension((void(*)(void))sqr_init);
drh425e27d2013-07-15 17:02:28 +0000120 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
121 return SQLITE_OK;
122}
123
124/*
drh1409be62006-08-23 20:07:20 +0000125** tclcmd: sqlite3_auto_extension_cube
126**
127** Register the "cube" extension to be loaded automatically.
128*/
mistachkin7617e4a2016-07-28 17:11:20 +0000129static int SQLITE_TCLAPI autoExtCubeObjCmd(
drh1409be62006-08-23 20:07:20 +0000130 void * clientData,
131 Tcl_Interp *interp,
132 int objc,
133 Tcl_Obj *CONST objv[]
134){
drh32c83c82016-08-01 14:35:48 +0000135 int rc = sqlite3_auto_extension((void(*)(void))cube_init);
drhc8d75672008-07-08 02:12:37 +0000136 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
drh1409be62006-08-23 20:07:20 +0000137 return SQLITE_OK;
138}
139
140/*
drh425e27d2013-07-15 17:02:28 +0000141** tclcmd: sqlite3_cancel_auto_extension_cube
142**
143** Unregister the "cube" extension.
144*/
mistachkin7617e4a2016-07-28 17:11:20 +0000145static int SQLITE_TCLAPI cancelAutoExtCubeObjCmd(
drh425e27d2013-07-15 17:02:28 +0000146 void * clientData,
147 Tcl_Interp *interp,
148 int objc,
149 Tcl_Obj *CONST objv[]
150){
drh32c83c82016-08-01 14:35:48 +0000151 int rc = sqlite3_cancel_auto_extension((void(*)(void))cube_init);
drh425e27d2013-07-15 17:02:28 +0000152 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
153 return SQLITE_OK;
154}
155
156/*
drh1409be62006-08-23 20:07:20 +0000157** tclcmd: sqlite3_auto_extension_broken
158**
159** Register the broken extension to be loaded automatically.
160*/
mistachkin7617e4a2016-07-28 17:11:20 +0000161static int SQLITE_TCLAPI autoExtBrokenObjCmd(
drh1409be62006-08-23 20:07:20 +0000162 void * clientData,
163 Tcl_Interp *interp,
164 int objc,
165 Tcl_Obj *CONST objv[]
166){
drh32c83c82016-08-01 14:35:48 +0000167 int rc = sqlite3_auto_extension((void(*)(void))broken_init);
drhc8d75672008-07-08 02:12:37 +0000168 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
drh1409be62006-08-23 20:07:20 +0000169 return SQLITE_OK;
170}
171
drh425e27d2013-07-15 17:02:28 +0000172/*
173** tclcmd: sqlite3_cancel_auto_extension_broken
174**
175** Unregister the broken extension.
176*/
mistachkin7617e4a2016-07-28 17:11:20 +0000177static int SQLITE_TCLAPI cancelAutoExtBrokenObjCmd(
drh425e27d2013-07-15 17:02:28 +0000178 void * clientData,
179 Tcl_Interp *interp,
180 int objc,
181 Tcl_Obj *CONST objv[]
182){
drh32c83c82016-08-01 14:35:48 +0000183 int rc = sqlite3_cancel_auto_extension((void(*)(void))broken_init);
drh425e27d2013-07-15 17:02:28 +0000184 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
185 return SQLITE_OK;
186}
187
mlcreech9323f762008-03-19 23:52:34 +0000188#endif /* SQLITE_OMIT_LOAD_EXTENSION */
189
190
drh1409be62006-08-23 20:07:20 +0000191/*
192** tclcmd: sqlite3_reset_auto_extension
193**
194** Reset all auto-extensions
195*/
mistachkin7617e4a2016-07-28 17:11:20 +0000196static int SQLITE_TCLAPI resetAutoExtObjCmd(
drh1409be62006-08-23 20:07:20 +0000197 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
drh1409be62006-08-23 20:07:20 +0000207/*
208** This procedure registers the TCL procs defined in this file.
209*/
210int 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);
drh425e27d2013-07-15 17:02:28 +0000218 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);
drhd3f49642013-08-06 18:35:31 +0000224#endif
drh1409be62006-08-23 20:07:20 +0000225 Tcl_CreateObjCommand(interp, "sqlite3_reset_auto_extension",
226 resetAutoExtObjCmd, 0, 0);
drh1409be62006-08-23 20:07:20 +0000227 return TCL_OK;
228}