os/persistentdata/persistentstorage/sqlite3api/TEST/SRC/test_autoext.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
** 2006 August 23
sl@0
     3
**
sl@0
     4
** The author disclaims copyright to this source code.  In place of
sl@0
     5
** a legal notice, here is a blessing:
sl@0
     6
**
sl@0
     7
**    May you do good and not evil.
sl@0
     8
**    May you find forgiveness for yourself and forgive others.
sl@0
     9
**    May you share freely, never taking more than you give.
sl@0
    10
**
sl@0
    11
*************************************************************************
sl@0
    12
** Test extension for testing the sqlite3_auto_extension() function.
sl@0
    13
**
sl@0
    14
** $Id: test_autoext.c,v 1.5 2008/07/08 02:12:37 drh Exp $
sl@0
    15
*/
sl@0
    16
#include "tcl.h"
sl@0
    17
#include "sqlite3ext.h"
sl@0
    18
sl@0
    19
#ifndef SQLITE_OMIT_LOAD_EXTENSION
sl@0
    20
static SQLITE_EXTENSION_INIT1
sl@0
    21
sl@0
    22
/*
sl@0
    23
** The sqr() SQL function returns the square of its input value.
sl@0
    24
*/
sl@0
    25
static void sqrFunc(
sl@0
    26
  sqlite3_context *context,
sl@0
    27
  int argc,
sl@0
    28
  sqlite3_value **argv
sl@0
    29
){
sl@0
    30
  double r = sqlite3_value_double(argv[0]);
sl@0
    31
  sqlite3_result_double(context, r*r);
sl@0
    32
}
sl@0
    33
sl@0
    34
/*
sl@0
    35
** This is the entry point to register the extension for the sqr() function.
sl@0
    36
*/
sl@0
    37
static int sqr_init(
sl@0
    38
  sqlite3 *db, 
sl@0
    39
  char **pzErrMsg, 
sl@0
    40
  const sqlite3_api_routines *pApi
sl@0
    41
){
sl@0
    42
  SQLITE_EXTENSION_INIT2(pApi);
sl@0
    43
  sqlite3_create_function(db, "sqr", 1, SQLITE_ANY, 0, sqrFunc, 0, 0);
sl@0
    44
  return 0;
sl@0
    45
}
sl@0
    46
sl@0
    47
/*
sl@0
    48
** The cube() SQL function returns the cube of its input value.
sl@0
    49
*/
sl@0
    50
static void cubeFunc(
sl@0
    51
  sqlite3_context *context,
sl@0
    52
  int argc,
sl@0
    53
  sqlite3_value **argv
sl@0
    54
){
sl@0
    55
  double r = sqlite3_value_double(argv[0]);
sl@0
    56
  sqlite3_result_double(context, r*r*r);
sl@0
    57
}
sl@0
    58
sl@0
    59
/*
sl@0
    60
** This is the entry point to register the extension for the cube() function.
sl@0
    61
*/
sl@0
    62
static int cube_init(
sl@0
    63
  sqlite3 *db, 
sl@0
    64
  char **pzErrMsg, 
sl@0
    65
  const sqlite3_api_routines *pApi
sl@0
    66
){
sl@0
    67
  SQLITE_EXTENSION_INIT2(pApi);
sl@0
    68
  sqlite3_create_function(db, "cube", 1, SQLITE_ANY, 0, cubeFunc, 0, 0);
sl@0
    69
  return 0;
sl@0
    70
}
sl@0
    71
sl@0
    72
/*
sl@0
    73
** This is a broken extension entry point
sl@0
    74
*/
sl@0
    75
static int broken_init(
sl@0
    76
  sqlite3 *db, 
sl@0
    77
  char **pzErrMsg, 
sl@0
    78
  const sqlite3_api_routines *pApi
sl@0
    79
){
sl@0
    80
  char *zErr;
sl@0
    81
  SQLITE_EXTENSION_INIT2(pApi);
sl@0
    82
  zErr = sqlite3_mprintf("broken autoext!");
sl@0
    83
  *pzErrMsg = zErr;
sl@0
    84
  return 1;
sl@0
    85
}
sl@0
    86
sl@0
    87
/*
sl@0
    88
** tclcmd:   sqlite3_auto_extension_sqr
sl@0
    89
**
sl@0
    90
** Register the "sqr" extension to be loaded automatically.
sl@0
    91
*/
sl@0
    92
static int autoExtSqrObjCmd(
sl@0
    93
  void * clientData,
sl@0
    94
  Tcl_Interp *interp,
sl@0
    95
  int objc,
sl@0
    96
  Tcl_Obj *CONST objv[]
sl@0
    97
){
sl@0
    98
  int rc = sqlite3_auto_extension((void*)sqr_init);
sl@0
    99
  Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
sl@0
   100
  return SQLITE_OK;
sl@0
   101
}
sl@0
   102
sl@0
   103
/*
sl@0
   104
** tclcmd:   sqlite3_auto_extension_cube
sl@0
   105
**
sl@0
   106
** Register the "cube" extension to be loaded automatically.
sl@0
   107
*/
sl@0
   108
static int autoExtCubeObjCmd(
sl@0
   109
  void * clientData,
sl@0
   110
  Tcl_Interp *interp,
sl@0
   111
  int objc,
sl@0
   112
  Tcl_Obj *CONST objv[]
sl@0
   113
){
sl@0
   114
  int rc = sqlite3_auto_extension((void*)cube_init);
sl@0
   115
  Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
sl@0
   116
  return SQLITE_OK;
sl@0
   117
}
sl@0
   118
sl@0
   119
/*
sl@0
   120
** tclcmd:   sqlite3_auto_extension_broken
sl@0
   121
**
sl@0
   122
** Register the broken extension to be loaded automatically.
sl@0
   123
*/
sl@0
   124
static int autoExtBrokenObjCmd(
sl@0
   125
  void * clientData,
sl@0
   126
  Tcl_Interp *interp,
sl@0
   127
  int objc,
sl@0
   128
  Tcl_Obj *CONST objv[]
sl@0
   129
){
sl@0
   130
  int rc = sqlite3_auto_extension((void*)broken_init);
sl@0
   131
  Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
sl@0
   132
  return SQLITE_OK;
sl@0
   133
}
sl@0
   134
sl@0
   135
#endif /* SQLITE_OMIT_LOAD_EXTENSION */
sl@0
   136
sl@0
   137
sl@0
   138
/*
sl@0
   139
** tclcmd:   sqlite3_reset_auto_extension
sl@0
   140
**
sl@0
   141
** Reset all auto-extensions
sl@0
   142
*/
sl@0
   143
static int resetAutoExtObjCmd(
sl@0
   144
  void * clientData,
sl@0
   145
  Tcl_Interp *interp,
sl@0
   146
  int objc,
sl@0
   147
  Tcl_Obj *CONST objv[]
sl@0
   148
){
sl@0
   149
  sqlite3_reset_auto_extension();
sl@0
   150
  return SQLITE_OK;
sl@0
   151
}
sl@0
   152
sl@0
   153
sl@0
   154
/*
sl@0
   155
** This procedure registers the TCL procs defined in this file.
sl@0
   156
*/
sl@0
   157
int Sqlitetest_autoext_Init(Tcl_Interp *interp){
sl@0
   158
#ifndef SQLITE_OMIT_LOAD_EXTENSION
sl@0
   159
  Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_sqr",
sl@0
   160
          autoExtSqrObjCmd, 0, 0);
sl@0
   161
  Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_cube",
sl@0
   162
          autoExtCubeObjCmd, 0, 0);
sl@0
   163
  Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_broken",
sl@0
   164
          autoExtBrokenObjCmd, 0, 0);
sl@0
   165
#endif
sl@0
   166
  Tcl_CreateObjCommand(interp, "sqlite3_reset_auto_extension",
sl@0
   167
          resetAutoExtObjCmd, 0, 0);
sl@0
   168
  return TCL_OK;
sl@0
   169
}