persistentstorage/sqlite3api/TEST/SRC/test_autoext.c
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     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.5 2008/07/08 02:12:37 drh 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   int rc = sqlite3_auto_extension((void*)sqr_init);
       
    99   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
       
   100   return SQLITE_OK;
       
   101 }
       
   102 
       
   103 /*
       
   104 ** tclcmd:   sqlite3_auto_extension_cube
       
   105 **
       
   106 ** Register the "cube" extension to be loaded automatically.
       
   107 */
       
   108 static int autoExtCubeObjCmd(
       
   109   void * clientData,
       
   110   Tcl_Interp *interp,
       
   111   int objc,
       
   112   Tcl_Obj *CONST objv[]
       
   113 ){
       
   114   int rc = sqlite3_auto_extension((void*)cube_init);
       
   115   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
       
   116   return SQLITE_OK;
       
   117 }
       
   118 
       
   119 /*
       
   120 ** tclcmd:   sqlite3_auto_extension_broken
       
   121 **
       
   122 ** Register the broken extension to be loaded automatically.
       
   123 */
       
   124 static int autoExtBrokenObjCmd(
       
   125   void * clientData,
       
   126   Tcl_Interp *interp,
       
   127   int objc,
       
   128   Tcl_Obj *CONST objv[]
       
   129 ){
       
   130   int rc = sqlite3_auto_extension((void*)broken_init);
       
   131   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
       
   132   return SQLITE_OK;
       
   133 }
       
   134 
       
   135 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
       
   136 
       
   137 
       
   138 /*
       
   139 ** tclcmd:   sqlite3_reset_auto_extension
       
   140 **
       
   141 ** Reset all auto-extensions
       
   142 */
       
   143 static 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 
       
   154 /*
       
   155 ** This procedure registers the TCL procs defined in this file.
       
   156 */
       
   157 int 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);
       
   165 #endif
       
   166   Tcl_CreateObjCommand(interp, "sqlite3_reset_auto_extension",
       
   167           resetAutoExtObjCmd, 0, 0);
       
   168   return TCL_OK;
       
   169 }