os/persistentdata/persistentstorage/sqlite3api/TEST/SRC/test_loadext.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 June 14
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_load_extension() function.
sl@0
    13
**
sl@0
    14
** $Id: test_loadext.c,v 1.3 2008/08/02 03:50:39 drh Exp $
sl@0
    15
*/
sl@0
    16
#include <string.h>
sl@0
    17
#include "sqlite3ext.h"
sl@0
    18
SQLITE_EXTENSION_INIT1
sl@0
    19
sl@0
    20
/*
sl@0
    21
** The half() SQL function returns half of its input value.
sl@0
    22
*/
sl@0
    23
static void halfFunc(
sl@0
    24
  sqlite3_context *context,
sl@0
    25
  int argc,
sl@0
    26
  sqlite3_value **argv
sl@0
    27
){
sl@0
    28
  sqlite3_result_double(context, 0.5*sqlite3_value_double(argv[0]));
sl@0
    29
}
sl@0
    30
sl@0
    31
/*
sl@0
    32
** SQL functions to call the sqlite3_status function and return results.
sl@0
    33
*/
sl@0
    34
static void statusFunc(
sl@0
    35
  sqlite3_context *context,
sl@0
    36
  int argc,
sl@0
    37
  sqlite3_value **argv
sl@0
    38
){
sl@0
    39
  int op, mx, cur, resetFlag, rc;
sl@0
    40
  if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){
sl@0
    41
    op = sqlite3_value_int(argv[0]);
sl@0
    42
  }else if( sqlite3_value_type(argv[0])==SQLITE_TEXT ){
sl@0
    43
    int i;
sl@0
    44
    const char *zName;
sl@0
    45
    static const struct {
sl@0
    46
      const char *zName;
sl@0
    47
      int op;
sl@0
    48
    } aOp[] = {
sl@0
    49
      { "MEMORY_USED",         SQLITE_STATUS_MEMORY_USED         },
sl@0
    50
      { "PAGECACHE_USED",      SQLITE_STATUS_PAGECACHE_USED      },
sl@0
    51
      { "PAGECACHE_OVERFLOW",  SQLITE_STATUS_PAGECACHE_OVERFLOW  },
sl@0
    52
      { "SCRATCH_USED",        SQLITE_STATUS_SCRATCH_USED        },
sl@0
    53
      { "SCRATCH_OVERFLOW",    SQLITE_STATUS_SCRATCH_OVERFLOW    },
sl@0
    54
      { "MALLOC_SIZE",         SQLITE_STATUS_MALLOC_SIZE         },
sl@0
    55
    };
sl@0
    56
    int nOp = sizeof(aOp)/sizeof(aOp[0]);
sl@0
    57
    zName = (const char*)sqlite3_value_text(argv[0]);
sl@0
    58
    for(i=0; i<nOp; i++){
sl@0
    59
      if( strcmp(aOp[i].zName, zName)==0 ){
sl@0
    60
        op = aOp[i].op;
sl@0
    61
        break;
sl@0
    62
      }
sl@0
    63
    }
sl@0
    64
    if( i>=nOp ){
sl@0
    65
      char *zMsg = sqlite3_mprintf("unknown status property: %s", zName);
sl@0
    66
      sqlite3_result_error(context, zMsg, -1);
sl@0
    67
      sqlite3_free(zMsg);
sl@0
    68
      return;
sl@0
    69
    }
sl@0
    70
  }else{
sl@0
    71
    sqlite3_result_error(context, "unknown status type", -1);
sl@0
    72
    return;
sl@0
    73
  }
sl@0
    74
  if( argc==2 ){
sl@0
    75
    resetFlag = sqlite3_value_int(argv[1]);
sl@0
    76
  }else{
sl@0
    77
    resetFlag = 0;
sl@0
    78
  }
sl@0
    79
  rc = sqlite3_status(op, &cur, &mx, resetFlag);
sl@0
    80
  if( rc!=SQLITE_OK ){
sl@0
    81
    char *zMsg = sqlite3_mprintf("sqlite3_status(%d,...) returns %d", op, rc);
sl@0
    82
    sqlite3_result_error(context, zMsg, -1);
sl@0
    83
    sqlite3_free(zMsg);
sl@0
    84
    return;
sl@0
    85
  } 
sl@0
    86
  if( argc==2 ){
sl@0
    87
    sqlite3_result_int(context, mx);
sl@0
    88
  }else{
sl@0
    89
    sqlite3_result_int(context, cur);
sl@0
    90
  }
sl@0
    91
}
sl@0
    92
sl@0
    93
/*
sl@0
    94
** Extension load function.
sl@0
    95
*/
sl@0
    96
int testloadext_init(
sl@0
    97
  sqlite3 *db, 
sl@0
    98
  char **pzErrMsg, 
sl@0
    99
  const sqlite3_api_routines *pApi
sl@0
   100
){
sl@0
   101
  int nErr = 0;
sl@0
   102
  SQLITE_EXTENSION_INIT2(pApi);
sl@0
   103
  nErr |= sqlite3_create_function(db, "half", 1, SQLITE_ANY, 0, halfFunc, 0, 0);
sl@0
   104
  nErr |= sqlite3_create_function(db, "sqlite3_status", 1, SQLITE_ANY, 0,
sl@0
   105
                          statusFunc, 0, 0);
sl@0
   106
  nErr |= sqlite3_create_function(db, "sqlite3_status", 2, SQLITE_ANY, 0,
sl@0
   107
                          statusFunc, 0, 0);
sl@0
   108
  return nErr ? SQLITE_ERROR : SQLITE_OK;
sl@0
   109
}
sl@0
   110
sl@0
   111
/*
sl@0
   112
** Another extension entry point. This one always fails.
sl@0
   113
*/
sl@0
   114
int testbrokenext_init(
sl@0
   115
  sqlite3 *db, 
sl@0
   116
  char **pzErrMsg, 
sl@0
   117
  const sqlite3_api_routines *pApi
sl@0
   118
){
sl@0
   119
  char *zErr;
sl@0
   120
  SQLITE_EXTENSION_INIT2(pApi);
sl@0
   121
  zErr = sqlite3_mprintf("broken!");
sl@0
   122
  *pzErrMsg = zErr;
sl@0
   123
  return 1;
sl@0
   124
}