1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/SRC/test_autoext.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,169 @@
1.4 +/*
1.5 +** 2006 August 23
1.6 +**
1.7 +** The author disclaims copyright to this source code. In place of
1.8 +** a legal notice, here is a blessing:
1.9 +**
1.10 +** May you do good and not evil.
1.11 +** May you find forgiveness for yourself and forgive others.
1.12 +** May you share freely, never taking more than you give.
1.13 +**
1.14 +*************************************************************************
1.15 +** Test extension for testing the sqlite3_auto_extension() function.
1.16 +**
1.17 +** $Id: test_autoext.c,v 1.5 2008/07/08 02:12:37 drh Exp $
1.18 +*/
1.19 +#include "tcl.h"
1.20 +#include "sqlite3ext.h"
1.21 +
1.22 +#ifndef SQLITE_OMIT_LOAD_EXTENSION
1.23 +static SQLITE_EXTENSION_INIT1
1.24 +
1.25 +/*
1.26 +** The sqr() SQL function returns the square of its input value.
1.27 +*/
1.28 +static void sqrFunc(
1.29 + sqlite3_context *context,
1.30 + int argc,
1.31 + sqlite3_value **argv
1.32 +){
1.33 + double r = sqlite3_value_double(argv[0]);
1.34 + sqlite3_result_double(context, r*r);
1.35 +}
1.36 +
1.37 +/*
1.38 +** This is the entry point to register the extension for the sqr() function.
1.39 +*/
1.40 +static int sqr_init(
1.41 + sqlite3 *db,
1.42 + char **pzErrMsg,
1.43 + const sqlite3_api_routines *pApi
1.44 +){
1.45 + SQLITE_EXTENSION_INIT2(pApi);
1.46 + sqlite3_create_function(db, "sqr", 1, SQLITE_ANY, 0, sqrFunc, 0, 0);
1.47 + return 0;
1.48 +}
1.49 +
1.50 +/*
1.51 +** The cube() SQL function returns the cube of its input value.
1.52 +*/
1.53 +static void cubeFunc(
1.54 + sqlite3_context *context,
1.55 + int argc,
1.56 + sqlite3_value **argv
1.57 +){
1.58 + double r = sqlite3_value_double(argv[0]);
1.59 + sqlite3_result_double(context, r*r*r);
1.60 +}
1.61 +
1.62 +/*
1.63 +** This is the entry point to register the extension for the cube() function.
1.64 +*/
1.65 +static int cube_init(
1.66 + sqlite3 *db,
1.67 + char **pzErrMsg,
1.68 + const sqlite3_api_routines *pApi
1.69 +){
1.70 + SQLITE_EXTENSION_INIT2(pApi);
1.71 + sqlite3_create_function(db, "cube", 1, SQLITE_ANY, 0, cubeFunc, 0, 0);
1.72 + return 0;
1.73 +}
1.74 +
1.75 +/*
1.76 +** This is a broken extension entry point
1.77 +*/
1.78 +static int broken_init(
1.79 + sqlite3 *db,
1.80 + char **pzErrMsg,
1.81 + const sqlite3_api_routines *pApi
1.82 +){
1.83 + char *zErr;
1.84 + SQLITE_EXTENSION_INIT2(pApi);
1.85 + zErr = sqlite3_mprintf("broken autoext!");
1.86 + *pzErrMsg = zErr;
1.87 + return 1;
1.88 +}
1.89 +
1.90 +/*
1.91 +** tclcmd: sqlite3_auto_extension_sqr
1.92 +**
1.93 +** Register the "sqr" extension to be loaded automatically.
1.94 +*/
1.95 +static int autoExtSqrObjCmd(
1.96 + void * clientData,
1.97 + Tcl_Interp *interp,
1.98 + int objc,
1.99 + Tcl_Obj *CONST objv[]
1.100 +){
1.101 + int rc = sqlite3_auto_extension((void*)sqr_init);
1.102 + Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1.103 + return SQLITE_OK;
1.104 +}
1.105 +
1.106 +/*
1.107 +** tclcmd: sqlite3_auto_extension_cube
1.108 +**
1.109 +** Register the "cube" extension to be loaded automatically.
1.110 +*/
1.111 +static int autoExtCubeObjCmd(
1.112 + void * clientData,
1.113 + Tcl_Interp *interp,
1.114 + int objc,
1.115 + Tcl_Obj *CONST objv[]
1.116 +){
1.117 + int rc = sqlite3_auto_extension((void*)cube_init);
1.118 + Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1.119 + return SQLITE_OK;
1.120 +}
1.121 +
1.122 +/*
1.123 +** tclcmd: sqlite3_auto_extension_broken
1.124 +**
1.125 +** Register the broken extension to be loaded automatically.
1.126 +*/
1.127 +static int autoExtBrokenObjCmd(
1.128 + void * clientData,
1.129 + Tcl_Interp *interp,
1.130 + int objc,
1.131 + Tcl_Obj *CONST objv[]
1.132 +){
1.133 + int rc = sqlite3_auto_extension((void*)broken_init);
1.134 + Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1.135 + return SQLITE_OK;
1.136 +}
1.137 +
1.138 +#endif /* SQLITE_OMIT_LOAD_EXTENSION */
1.139 +
1.140 +
1.141 +/*
1.142 +** tclcmd: sqlite3_reset_auto_extension
1.143 +**
1.144 +** Reset all auto-extensions
1.145 +*/
1.146 +static int resetAutoExtObjCmd(
1.147 + void * clientData,
1.148 + Tcl_Interp *interp,
1.149 + int objc,
1.150 + Tcl_Obj *CONST objv[]
1.151 +){
1.152 + sqlite3_reset_auto_extension();
1.153 + return SQLITE_OK;
1.154 +}
1.155 +
1.156 +
1.157 +/*
1.158 +** This procedure registers the TCL procs defined in this file.
1.159 +*/
1.160 +int Sqlitetest_autoext_Init(Tcl_Interp *interp){
1.161 +#ifndef SQLITE_OMIT_LOAD_EXTENSION
1.162 + Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_sqr",
1.163 + autoExtSqrObjCmd, 0, 0);
1.164 + Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_cube",
1.165 + autoExtCubeObjCmd, 0, 0);
1.166 + Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_broken",
1.167 + autoExtBrokenObjCmd, 0, 0);
1.168 +#endif
1.169 + Tcl_CreateObjCommand(interp, "sqlite3_reset_auto_extension",
1.170 + resetAutoExtObjCmd, 0, 0);
1.171 + return TCL_OK;
1.172 +}