1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/SRC/test_loadext.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,124 @@
1.4 +/*
1.5 +** 2006 June 14
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_load_extension() function.
1.16 +**
1.17 +** $Id: test_loadext.c,v 1.3 2008/08/02 03:50:39 drh Exp $
1.18 +*/
1.19 +#include <string.h>
1.20 +#include "sqlite3ext.h"
1.21 +SQLITE_EXTENSION_INIT1
1.22 +
1.23 +/*
1.24 +** The half() SQL function returns half of its input value.
1.25 +*/
1.26 +static void halfFunc(
1.27 + sqlite3_context *context,
1.28 + int argc,
1.29 + sqlite3_value **argv
1.30 +){
1.31 + sqlite3_result_double(context, 0.5*sqlite3_value_double(argv[0]));
1.32 +}
1.33 +
1.34 +/*
1.35 +** SQL functions to call the sqlite3_status function and return results.
1.36 +*/
1.37 +static void statusFunc(
1.38 + sqlite3_context *context,
1.39 + int argc,
1.40 + sqlite3_value **argv
1.41 +){
1.42 + int op, mx, cur, resetFlag, rc;
1.43 + if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){
1.44 + op = sqlite3_value_int(argv[0]);
1.45 + }else if( sqlite3_value_type(argv[0])==SQLITE_TEXT ){
1.46 + int i;
1.47 + const char *zName;
1.48 + static const struct {
1.49 + const char *zName;
1.50 + int op;
1.51 + } aOp[] = {
1.52 + { "MEMORY_USED", SQLITE_STATUS_MEMORY_USED },
1.53 + { "PAGECACHE_USED", SQLITE_STATUS_PAGECACHE_USED },
1.54 + { "PAGECACHE_OVERFLOW", SQLITE_STATUS_PAGECACHE_OVERFLOW },
1.55 + { "SCRATCH_USED", SQLITE_STATUS_SCRATCH_USED },
1.56 + { "SCRATCH_OVERFLOW", SQLITE_STATUS_SCRATCH_OVERFLOW },
1.57 + { "MALLOC_SIZE", SQLITE_STATUS_MALLOC_SIZE },
1.58 + };
1.59 + int nOp = sizeof(aOp)/sizeof(aOp[0]);
1.60 + zName = (const char*)sqlite3_value_text(argv[0]);
1.61 + for(i=0; i<nOp; i++){
1.62 + if( strcmp(aOp[i].zName, zName)==0 ){
1.63 + op = aOp[i].op;
1.64 + break;
1.65 + }
1.66 + }
1.67 + if( i>=nOp ){
1.68 + char *zMsg = sqlite3_mprintf("unknown status property: %s", zName);
1.69 + sqlite3_result_error(context, zMsg, -1);
1.70 + sqlite3_free(zMsg);
1.71 + return;
1.72 + }
1.73 + }else{
1.74 + sqlite3_result_error(context, "unknown status type", -1);
1.75 + return;
1.76 + }
1.77 + if( argc==2 ){
1.78 + resetFlag = sqlite3_value_int(argv[1]);
1.79 + }else{
1.80 + resetFlag = 0;
1.81 + }
1.82 + rc = sqlite3_status(op, &cur, &mx, resetFlag);
1.83 + if( rc!=SQLITE_OK ){
1.84 + char *zMsg = sqlite3_mprintf("sqlite3_status(%d,...) returns %d", op, rc);
1.85 + sqlite3_result_error(context, zMsg, -1);
1.86 + sqlite3_free(zMsg);
1.87 + return;
1.88 + }
1.89 + if( argc==2 ){
1.90 + sqlite3_result_int(context, mx);
1.91 + }else{
1.92 + sqlite3_result_int(context, cur);
1.93 + }
1.94 +}
1.95 +
1.96 +/*
1.97 +** Extension load function.
1.98 +*/
1.99 +int testloadext_init(
1.100 + sqlite3 *db,
1.101 + char **pzErrMsg,
1.102 + const sqlite3_api_routines *pApi
1.103 +){
1.104 + int nErr = 0;
1.105 + SQLITE_EXTENSION_INIT2(pApi);
1.106 + nErr |= sqlite3_create_function(db, "half", 1, SQLITE_ANY, 0, halfFunc, 0, 0);
1.107 + nErr |= sqlite3_create_function(db, "sqlite3_status", 1, SQLITE_ANY, 0,
1.108 + statusFunc, 0, 0);
1.109 + nErr |= sqlite3_create_function(db, "sqlite3_status", 2, SQLITE_ANY, 0,
1.110 + statusFunc, 0, 0);
1.111 + return nErr ? SQLITE_ERROR : SQLITE_OK;
1.112 +}
1.113 +
1.114 +/*
1.115 +** Another extension entry point. This one always fails.
1.116 +*/
1.117 +int testbrokenext_init(
1.118 + sqlite3 *db,
1.119 + char **pzErrMsg,
1.120 + const sqlite3_api_routines *pApi
1.121 +){
1.122 + char *zErr;
1.123 + SQLITE_EXTENSION_INIT2(pApi);
1.124 + zErr = sqlite3_mprintf("broken!");
1.125 + *pzErrMsg = zErr;
1.126 + return 1;
1.127 +}