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