xref: /sqlite-3.40.0/src/test_autoext.c (revision 8a29dfde)
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 ** $Id: test_autoext.c,v 1.4 2008/03/19 23:52:35 mlcreech Exp $
15 */
16 #include "tcl.h"
17 #include "sqlite3ext.h"
18 
19 #ifndef SQLITE_OMIT_LOAD_EXTENSION
20 static SQLITE_EXTENSION_INIT1
21 
22 /*
23 ** The sqr() SQL function returns the square of its input value.
24 */
25 static void sqrFunc(
26   sqlite3_context *context,
27   int argc,
28   sqlite3_value **argv
29 ){
30   double r = sqlite3_value_double(argv[0]);
31   sqlite3_result_double(context, r*r);
32 }
33 
34 /*
35 ** This is the entry point to register the extension for the sqr() function.
36 */
37 static int sqr_init(
38   sqlite3 *db,
39   char **pzErrMsg,
40   const sqlite3_api_routines *pApi
41 ){
42   SQLITE_EXTENSION_INIT2(pApi);
43   sqlite3_create_function(db, "sqr", 1, SQLITE_ANY, 0, sqrFunc, 0, 0);
44   return 0;
45 }
46 
47 /*
48 ** The cube() SQL function returns the cube of its input value.
49 */
50 static void cubeFunc(
51   sqlite3_context *context,
52   int argc,
53   sqlite3_value **argv
54 ){
55   double r = sqlite3_value_double(argv[0]);
56   sqlite3_result_double(context, r*r*r);
57 }
58 
59 /*
60 ** This is the entry point to register the extension for the cube() function.
61 */
62 static int cube_init(
63   sqlite3 *db,
64   char **pzErrMsg,
65   const sqlite3_api_routines *pApi
66 ){
67   SQLITE_EXTENSION_INIT2(pApi);
68   sqlite3_create_function(db, "cube", 1, SQLITE_ANY, 0, cubeFunc, 0, 0);
69   return 0;
70 }
71 
72 /*
73 ** This is a broken extension entry point
74 */
75 static int broken_init(
76   sqlite3 *db,
77   char **pzErrMsg,
78   const sqlite3_api_routines *pApi
79 ){
80   char *zErr;
81   SQLITE_EXTENSION_INIT2(pApi);
82   zErr = sqlite3_mprintf("broken autoext!");
83   *pzErrMsg = zErr;
84   return 1;
85 }
86 
87 /*
88 ** tclcmd:   sqlite3_auto_extension_sqr
89 **
90 ** Register the "sqr" extension to be loaded automatically.
91 */
92 static int autoExtSqrObjCmd(
93   void * clientData,
94   Tcl_Interp *interp,
95   int objc,
96   Tcl_Obj *CONST objv[]
97 ){
98   sqlite3_auto_extension((void*)sqr_init);
99   return SQLITE_OK;
100 }
101 
102 /*
103 ** tclcmd:   sqlite3_auto_extension_cube
104 **
105 ** Register the "cube" extension to be loaded automatically.
106 */
107 static int autoExtCubeObjCmd(
108   void * clientData,
109   Tcl_Interp *interp,
110   int objc,
111   Tcl_Obj *CONST objv[]
112 ){
113   sqlite3_auto_extension((void*)cube_init);
114   return SQLITE_OK;
115 }
116 
117 /*
118 ** tclcmd:   sqlite3_auto_extension_broken
119 **
120 ** Register the broken extension to be loaded automatically.
121 */
122 static int autoExtBrokenObjCmd(
123   void * clientData,
124   Tcl_Interp *interp,
125   int objc,
126   Tcl_Obj *CONST objv[]
127 ){
128   sqlite3_auto_extension((void*)broken_init);
129   return SQLITE_OK;
130 }
131 
132 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
133 
134 
135 /*
136 ** tclcmd:   sqlite3_reset_auto_extension
137 **
138 ** Reset all auto-extensions
139 */
140 static int resetAutoExtObjCmd(
141   void * clientData,
142   Tcl_Interp *interp,
143   int objc,
144   Tcl_Obj *CONST objv[]
145 ){
146   sqlite3_reset_auto_extension();
147   return SQLITE_OK;
148 }
149 
150 
151 /*
152 ** This procedure registers the TCL procs defined in this file.
153 */
154 int Sqlitetest_autoext_Init(Tcl_Interp *interp){
155 #ifndef SQLITE_OMIT_LOAD_EXTENSION
156   Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_sqr",
157           autoExtSqrObjCmd, 0, 0);
158   Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_cube",
159           autoExtCubeObjCmd, 0, 0);
160   Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_broken",
161           autoExtBrokenObjCmd, 0, 0);
162 #endif
163   Tcl_CreateObjCommand(interp, "sqlite3_reset_auto_extension",
164           resetAutoExtObjCmd, 0, 0);
165   return TCL_OK;
166 }
167