summaryrefslogtreecommitdiff
path: root/chromium/third_party/sqlite/src/ext/misc/urifuncs.c
blob: 9697b724cb7efcaf41101cdd0234172a413e28c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
** 2020-01-11
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
******************************************************************************
**
** This SQLite extension implements various SQL functions used to access
** the following SQLite C-language APIs:
**
**         sqlite3_uri_parameter()
**         sqlite3_uri_boolean()
**         sqlite3_uri_int64()
**         sqlite3_uri_key()
**         sqlite3_filename_database()
**         sqlite3_filename_journal()
**         sqlite3_filename_wal()
**         sqlite3_db_filename()
**
** These SQL functions are for testing and demonstration purposes only.
**
**
*/
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <assert.h>
#include <string.h>

/*
** SQL function:    sqlite3_db_filename(SCHEMA) 
**
** Return the filename corresponding to SCHEMA.
*/
static void func_db_filename(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  const char *zSchema = (const char*)sqlite3_value_text(argv[0]);
  sqlite3 *db = sqlite3_context_db_handle(context);
  const char *zFile = sqlite3_db_filename(db, zSchema);
  sqlite3_result_text(context, zFile, -1, SQLITE_TRANSIENT);
}

/*
** SQL function:    sqlite3_uri_parameter(SCHEMA,NAME) 
**
** Return the value of the NAME query parameter to the database for SCHEMA
*/
static void func_uri_parameter(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  const char *zSchema = (const char*)sqlite3_value_text(argv[0]);
  sqlite3 *db = sqlite3_context_db_handle(context);
  const char *zName = (const char*)sqlite3_value_text(argv[1]);
  const char *zFile = sqlite3_db_filename(db, zSchema);
  const char *zRes = sqlite3_uri_parameter(zFile, zName);
  sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
}

/*
** SQL function:    sqlite3_uri_boolean(SCHEMA,NAME,DEFAULT) 
**
** Return the boolean value of the NAME query parameter to
** the database for SCHEMA
*/
static void func_uri_boolean(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  const char *zSchema = (const char*)sqlite3_value_text(argv[0]);
  sqlite3 *db = sqlite3_context_db_handle(context);
  const char *zName = (const char*)sqlite3_value_text(argv[1]);
  const char *zFile = sqlite3_db_filename(db, zSchema);
  int iDflt = sqlite3_value_int(argv[2]);
  int iRes = sqlite3_uri_boolean(zFile, zName, iDflt);
  sqlite3_result_int(context, iRes);
}

/*
** SQL function:    sqlite3_uri_key(SCHEMA,N)
**
** Return the name of the Nth query parameter
*/
static void func_uri_key(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  const char *zSchema = (const char*)sqlite3_value_text(argv[0]);
  sqlite3 *db = sqlite3_context_db_handle(context);
  int N = sqlite3_value_int(argv[1]);
  const char *zFile = sqlite3_db_filename(db, zSchema);
  const char *zRes = sqlite3_uri_key(zFile, N);
  sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
}

/*
** SQL function:    sqlite3_uri_int64(SCHEMA,NAME,DEFAULT) 
**
** Return the int64 value of the NAME query parameter to
** the database for SCHEMA
*/
static void func_uri_int64(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  const char *zSchema = (const char*)sqlite3_value_text(argv[0]);
  sqlite3 *db = sqlite3_context_db_handle(context);
  const char *zName = (const char*)sqlite3_value_text(argv[1]);
  const char *zFile = sqlite3_db_filename(db, zSchema);
  sqlite3_int64 iDflt = sqlite3_value_int64(argv[2]);
  sqlite3_int64 iRes = sqlite3_uri_int64(zFile, zName, iDflt);
  sqlite3_result_int64(context, iRes);
}

/*
** SQL function:    sqlite3_filename_database(SCHEMA)
**
** Return the database filename for SCHEMA
*/
static void func_filename_database(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  const char *zSchema = (const char*)sqlite3_value_text(argv[0]);
  sqlite3 *db = sqlite3_context_db_handle(context);
  const char *zFile = sqlite3_db_filename(db, zSchema);
  const char *zRes = zFile ? sqlite3_filename_database(zFile) : 0;
  sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
}

/*
** SQL function:    sqlite3_filename_journal(SCHEMA)
**
** Return the rollback journal filename for SCHEMA
*/
static void func_filename_journal(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  const char *zSchema = (const char*)sqlite3_value_text(argv[0]);
  sqlite3 *db = sqlite3_context_db_handle(context);
  const char *zFile = sqlite3_db_filename(db, zSchema);
  const char *zRes = zFile ? sqlite3_filename_journal(zFile) : 0;
  sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
}

/*
** SQL function:    sqlite3_filename_wal(SCHEMA)
**
** Return the WAL filename for SCHEMA
*/
static void func_filename_wal(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  const char *zSchema = (const char*)sqlite3_value_text(argv[0]);
  sqlite3 *db = sqlite3_context_db_handle(context);
  const char *zFile = sqlite3_db_filename(db, zSchema);
  const char *zRes = zFile ? sqlite3_filename_wal(zFile) : 0;
  sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
}

#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_urifuncs_init(
  sqlite3 *db, 
  char **pzErrMsg, 
  const sqlite3_api_routines *pApi
){
  static const struct {
    const char *zFuncName;
    int nArg;
    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
  } aFunc[] = {
    { "sqlite3_db_filename",       1, func_db_filename       },
    { "sqlite3_uri_parameter",     2, func_uri_parameter     },
    { "sqlite3_uri_boolean",       3, func_uri_boolean       },
    { "sqlite3_uri_int64",         3, func_uri_int64         },
    { "sqlite3_uri_key",           2, func_uri_key           },
    { "sqlite3_filename_database", 1, func_filename_database },
    { "sqlite3_filename_journal",  1, func_filename_journal  },
    { "sqlite3_filename_wal",      1, func_filename_wal      },
  };
  int rc = SQLITE_OK;
  int i;
  SQLITE_EXTENSION_INIT2(pApi);
  (void)pzErrMsg;  /* Unused parameter */
  for(i=0; rc==SQLITE_OK && i<sizeof(aFunc)/sizeof(aFunc[0]); i++){
    rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
                     SQLITE_UTF8, 0,
                     aFunc[i].xFunc, 0, 0);
  }
  return rc;
}