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