Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 6404a69714e44061bb698a40a27269e29d773b79 [file] [log] [blame]
danielk197769e777f2006-06-14 10:38:021/*
2** 2006 June 14
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_load_extension() function.
danielk197769e777f2006-06-14 10:38:0213*/
drhd72a8412008-06-19 15:44:0014#include <string.h>
danielk197769e777f2006-06-14 10:38:0215#include "sqlite3ext.h"
16SQLITE_EXTENSION_INIT1
17
18/*
19** The half() SQL function returns half of its input value.
20*/
21static void halfFunc(
22 sqlite3_context *context,
23 int argc,
24 sqlite3_value **argv
25){
26 sqlite3_result_double(context, 0.5*sqlite3_value_double(argv[0]));
27}
28
29/*
drhd72a8412008-06-19 15:44:0030** SQL functions to call the sqlite3_status function and return results.
31*/
32static void statusFunc(
33 sqlite3_context *context,
34 int argc,
35 sqlite3_value **argv
36){
mistachkin8ccdef62015-12-16 22:06:5237 int op = 0, mx, cur, resetFlag, rc;
drhd72a8412008-06-19 15:44:0038 if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){
39 op = sqlite3_value_int(argv[0]);
40 }else if( sqlite3_value_type(argv[0])==SQLITE_TEXT ){
41 int i;
42 const char *zName;
43 static const struct {
44 const char *zName;
45 int op;
46 } aOp[] = {
47 { "MEMORY_USED", SQLITE_STATUS_MEMORY_USED },
48 { "PAGECACHE_USED", SQLITE_STATUS_PAGECACHE_USED },
49 { "PAGECACHE_OVERFLOW", SQLITE_STATUS_PAGECACHE_OVERFLOW },
50 { "SCRATCH_USED", SQLITE_STATUS_SCRATCH_USED },
51 { "SCRATCH_OVERFLOW", SQLITE_STATUS_SCRATCH_OVERFLOW },
52 { "MALLOC_SIZE", SQLITE_STATUS_MALLOC_SIZE },
53 };
54 int nOp = sizeof(aOp)/sizeof(aOp[0]);
55 zName = (const char*)sqlite3_value_text(argv[0]);
56 for(i=0; i<nOp; i++){
57 if( strcmp(aOp[i].zName, zName)==0 ){
58 op = aOp[i].op;
59 break;
60 }
61 }
62 if( i>=nOp ){
63 char *zMsg = sqlite3_mprintf("unknown status property: %s", zName);
64 sqlite3_result_error(context, zMsg, -1);
65 sqlite3_free(zMsg);
66 return;
67 }
68 }else{
69 sqlite3_result_error(context, "unknown status type", -1);
70 return;
71 }
72 if( argc==2 ){
73 resetFlag = sqlite3_value_int(argv[1]);
74 }else{
75 resetFlag = 0;
76 }
77 rc = sqlite3_status(op, &cur, &mx, resetFlag);
78 if( rc!=SQLITE_OK ){
79 char *zMsg = sqlite3_mprintf("sqlite3_status(%d,...) returns %d", op, rc);
80 sqlite3_result_error(context, zMsg, -1);
81 sqlite3_free(zMsg);
82 return;
83 }
84 if( argc==2 ){
85 sqlite3_result_int(context, mx);
86 }else{
87 sqlite3_result_int(context, cur);
88 }
89}
90
91/*
danielk197769e777f2006-06-14 10:38:0292** Extension load function.
93*/
mistachkin1925a2e2014-02-24 21:20:2594#ifdef _WIN32
95__declspec(dllexport)
96#endif
danielk197769e777f2006-06-14 10:38:0297int testloadext_init(
98 sqlite3 *db,
99 char **pzErrMsg,
100 const sqlite3_api_routines *pApi
101){
drh701bb3b2008-08-02 03:50:39102 int nErr = 0;
danielk197769e777f2006-06-14 10:38:02103 SQLITE_EXTENSION_INIT2(pApi);
drh701bb3b2008-08-02 03:50:39104 nErr |= sqlite3_create_function(db, "half", 1, SQLITE_ANY, 0, halfFunc, 0, 0);
105 nErr |= sqlite3_create_function(db, "sqlite3_status", 1, SQLITE_ANY, 0,
drhd72a8412008-06-19 15:44:00106 statusFunc, 0, 0);
drh701bb3b2008-08-02 03:50:39107 nErr |= sqlite3_create_function(db, "sqlite3_status", 2, SQLITE_ANY, 0,
drhd72a8412008-06-19 15:44:00108 statusFunc, 0, 0);
drh701bb3b2008-08-02 03:50:39109 return nErr ? SQLITE_ERROR : SQLITE_OK;
danielk197769e777f2006-06-14 10:38:02110}
111
112/*
113** Another extension entry point. This one always fails.
114*/
mistachkin1925a2e2014-02-24 21:20:25115#ifdef _WIN32
116__declspec(dllexport)
117#endif
danielk197769e777f2006-06-14 10:38:02118int testbrokenext_init(
119 sqlite3 *db,
120 char **pzErrMsg,
121 const sqlite3_api_routines *pApi
122){
123 char *zErr;
124 SQLITE_EXTENSION_INIT2(pApi);
125 zErr = sqlite3_mprintf("broken!");
126 *pzErrMsg = zErr;
127 return 1;
128}