blob: 11f541360d356aeb8e6c5feaba2ee7723f10f6d2 [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.
13**
drhc8d75672008-07-08 02:12:37 +000014** $Id: test_autoext.c,v 1.5 2008/07/08 02:12:37 drh Exp $
drh1409be62006-08-23 20:07:20 +000015*/
drh1409be62006-08-23 20:07:20 +000016#include "tcl.h"
drh1409be62006-08-23 20:07:20 +000017#include "sqlite3ext.h"
mlcreech9323f762008-03-19 23:52:34 +000018
19#ifndef SQLITE_OMIT_LOAD_EXTENSION
drh1409be62006-08-23 20:07:20 +000020static SQLITE_EXTENSION_INIT1
21
22/*
23** The sqr() SQL function returns the square of its input value.
24*/
25static 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*/
37static 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*/
50static 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*/
62static 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*/
75static 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*/
92static int autoExtSqrObjCmd(
93 void * clientData,
94 Tcl_Interp *interp,
95 int objc,
96 Tcl_Obj *CONST objv[]
97){
drhc8d75672008-07-08 02:12:37 +000098 int rc = sqlite3_auto_extension((void*)sqr_init);
99 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
drh1409be62006-08-23 20:07:20 +0000100 return SQLITE_OK;
101}
102
103/*
104** tclcmd: sqlite3_auto_extension_cube
105**
106** Register the "cube" extension to be loaded automatically.
107*/
108static int autoExtCubeObjCmd(
109 void * clientData,
110 Tcl_Interp *interp,
111 int objc,
112 Tcl_Obj *CONST objv[]
113){
drhc8d75672008-07-08 02:12:37 +0000114 int rc = sqlite3_auto_extension((void*)cube_init);
115 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
drh1409be62006-08-23 20:07:20 +0000116 return SQLITE_OK;
117}
118
119/*
120** tclcmd: sqlite3_auto_extension_broken
121**
122** Register the broken extension to be loaded automatically.
123*/
124static int autoExtBrokenObjCmd(
125 void * clientData,
126 Tcl_Interp *interp,
127 int objc,
128 Tcl_Obj *CONST objv[]
129){
drhc8d75672008-07-08 02:12:37 +0000130 int rc = sqlite3_auto_extension((void*)broken_init);
131 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
drh1409be62006-08-23 20:07:20 +0000132 return SQLITE_OK;
133}
134
mlcreech9323f762008-03-19 23:52:34 +0000135#endif /* SQLITE_OMIT_LOAD_EXTENSION */
136
137
drh1409be62006-08-23 20:07:20 +0000138/*
139** tclcmd: sqlite3_reset_auto_extension
140**
141** Reset all auto-extensions
142*/
143static int resetAutoExtObjCmd(
144 void * clientData,
145 Tcl_Interp *interp,
146 int objc,
147 Tcl_Obj *CONST objv[]
148){
149 sqlite3_reset_auto_extension();
150 return SQLITE_OK;
151}
152
153
drh1409be62006-08-23 20:07:20 +0000154/*
155** This procedure registers the TCL procs defined in this file.
156*/
157int Sqlitetest_autoext_Init(Tcl_Interp *interp){
158#ifndef SQLITE_OMIT_LOAD_EXTENSION
159 Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_sqr",
160 autoExtSqrObjCmd, 0, 0);
161 Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_cube",
162 autoExtCubeObjCmd, 0, 0);
163 Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_broken",
164 autoExtBrokenObjCmd, 0, 0);
drh984bfaa2008-03-19 16:08:53 +0000165#endif
drh1409be62006-08-23 20:07:20 +0000166 Tcl_CreateObjCommand(interp, "sqlite3_reset_auto_extension",
167 resetAutoExtObjCmd, 0, 0);
drh1409be62006-08-23 20:07:20 +0000168 return TCL_OK;
169}