forked from xerial/sqlite-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.java
More file actions
366 lines (330 loc) · 11.7 KB
/
Copy pathFunction.java
File metadata and controls
366 lines (330 loc) · 11.7 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* Copyright (c) 2007 David Crawshaw <david@zentus.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package org.sqlite;
import java.sql.Connection;
import java.sql.SQLException;
import org.sqlite.core.Codes;
import org.sqlite.core.DB;
/**
* Provides an interface for creating SQLite user-defined functions.
*
* <p>A subclass of <tt>org.sqlite.Function</tt> can be registered with <tt>Function.create()</tt>
* and called by the name it was given. All functions must implement <tt>xFunc()</tt>, which is
* called when SQLite runs the custom function. Eg.
*
* <pre>
* Class.forName("org.sqlite.JDBC");
* Connection conn = DriverManager.getConnection("jdbc:sqlite:");
*
* Function.create(conn, "myFunc", new Function() {
* protected void xFunc() {
* System.out.println("myFunc called!");
* }
* });
*
* conn.createStatement().execute("select myFunc();");
* </pre>
*
* <p>Arguments passed to a custom function can be accessed using the <tt>protected</tt> functions
* provided. <tt>args()</tt> returns the number of arguments passed, while
* <tt>value_<type>(int)</tt> returns the value of the specific argument. Similarly a function
* can return a value using the <tt>result(<type>)</tt> function.
*
* <p>Aggregate functions are not yet supported, but coming soon.
*/
public abstract class Function {
/**
* Flag to provide to {@link #create(Connection, String, Function, int)} that marks this
* Function as deterministic, making is usable in Indexes on Expressions.
*/
public static final int FLAG_DETERMINISTIC = 0x800;
private SQLiteConnection conn;
private DB db;
long context = 0; // pointer sqlite3_context*
long value = 0; // pointer sqlite3_value**
int args = 0;
/**
* Registers a given function with the connection.
*
* @param conn The connection.
* @param name The name of the function.
* @param f The function to register.
*/
public static final void create(Connection conn, String name, Function f) throws SQLException {
create(conn, name, f, 0);
}
/**
* Registers a given function with the connection.
*
* @param conn The connection.
* @param name The name of the function.
* @param f The function to register.
* @param flags Extra flags to pass, such as {@link #FLAG_DETERMINISTIC}
*/
public static final void create(Connection conn, String name, Function f, int flags)
throws SQLException {
create(conn, name, f, -1, flags);
}
/**
* Registers a given function with the connection.
*
* @param conn The connection.
* @param name The name of the function.
* @param f The function to register.
* @param nArgs The number of arguments that the function takes.
* @param flags Extra flags to pass, such as {@link #FLAG_DETERMINISTIC}
*/
public static final void create(Connection conn, String name, Function f, int nArgs, int flags)
throws SQLException {
if (conn == null || !(conn instanceof SQLiteConnection)) {
throw new SQLException("connection must be to an SQLite db");
}
if (conn.isClosed()) {
throw new SQLException("connection closed");
}
f.conn = (SQLiteConnection) conn;
f.db = f.conn.getDatabase();
if (nArgs < -1 || nArgs > 127) {
throw new SQLException("invalid args provided: " + nArgs);
}
if (name == null || name.length() > 255) {
throw new SQLException("invalid function name: '" + name + "'");
}
if (f.db.create_function(name, f, nArgs, flags) != Codes.SQLITE_OK) {
throw new SQLException("error creating function");
}
}
/**
* Removes a named function from the given connection.
*
* @param conn The connection to remove the function from.
* @param name The name of the function.
* @param nArgs The number of args for the function.
* @throws SQLException
*/
public static final void destroy(Connection conn, String name, int nArgs) throws SQLException {
if (conn == null || !(conn instanceof SQLiteConnection)) {
throw new SQLException("connection must be to an SQLite db");
}
((SQLiteConnection) conn).getDatabase().destroy_function(name, nArgs);
}
/**
* Removes a named function from the given connection.
*
* @param conn The connection to remove the function from.
* @param name The name of the function.
* @throws SQLException
*/
public static final void destroy(Connection conn, String name) throws SQLException {
destroy(conn, name, -1);
}
/**
* Called by SQLite as a custom function. Should access arguments through <tt>value_*(int)</tt>,
* return results with <tt>result(*)</tt> and throw errors with <tt>error(String)</tt>.
*/
protected abstract void xFunc() throws SQLException;
/**
* Returns the number of arguments passed to the function. Can only be called from
* <tt>xFunc()</tt>.
*/
protected final synchronized int args() throws SQLException {
checkContext();
return args;
}
/**
* Called by <tt>xFunc</tt> to return a value.
*
* @param value
*/
protected final synchronized void result(byte[] value) throws SQLException {
checkContext();
db.result_blob(context, value);
}
/**
* Called by <tt>xFunc</tt> to return a value.
*
* @param value
*/
protected final synchronized void result(double value) throws SQLException {
checkContext();
db.result_double(context, value);
}
/**
* Called by <tt>xFunc</tt> to return a value.
*
* @param value
*/
protected final synchronized void result(int value) throws SQLException {
checkContext();
db.result_int(context, value);
}
/**
* Called by <tt>xFunc</tt> to return a value.
*
* @param value
*/
protected final synchronized void result(long value) throws SQLException {
checkContext();
db.result_long(context, value);
}
/** Called by <tt>xFunc</tt> to return a value. */
protected final synchronized void result() throws SQLException {
checkContext();
db.result_null(context);
}
/**
* Called by <tt>xFunc</tt> to return a value.
*
* @param value
*/
protected final synchronized void result(String value) throws SQLException {
checkContext();
db.result_text(context, value);
}
/**
* Called by <tt>xFunc</tt> to throw an error.
*
* @param err
*/
protected final synchronized void error(String err) throws SQLException {
checkContext();
db.result_error(context, err);
}
/**
* Called by <tt>xFunc</tt> to access the value of an argument.
*
* @param arg
*/
protected final synchronized String value_text(int arg) throws SQLException {
checkValue(arg);
return db.value_text(this, arg);
}
/**
* Called by <tt>xFunc</tt> to access the value of an argument.
*
* @param arg
*/
protected final synchronized byte[] value_blob(int arg) throws SQLException {
checkValue(arg);
return db.value_blob(this, arg);
}
/**
* Called by <tt>xFunc</tt> to access the value of an argument.
*
* @param arg
*/
protected final synchronized double value_double(int arg) throws SQLException {
checkValue(arg);
return db.value_double(this, arg);
}
/**
* Called by <tt>xFunc</tt> to access the value of an argument.
*
* @param arg
*/
protected final synchronized int value_int(int arg) throws SQLException {
checkValue(arg);
return db.value_int(this, arg);
}
/**
* Called by <tt>xFunc</tt> to access the value of an argument.
*
* @param arg
*/
protected final synchronized long value_long(int arg) throws SQLException {
checkValue(arg);
return db.value_long(this, arg);
}
/**
* Called by <tt>xFunc</tt> to access the value of an argument.
*
* @param arg
*/
protected final synchronized int value_type(int arg) throws SQLException {
checkValue(arg);
return db.value_type(this, arg);
}
/** @throws SQLException */
private void checkContext() throws SQLException {
if (conn == null || conn.getDatabase() == null || context == 0) {
throw new SQLException("no context, not allowed to read value");
}
}
/**
* @param arg
* @throws SQLException
*/
private void checkValue(int arg) throws SQLException {
if (conn == null || conn.getDatabase() == null || value == 0) {
throw new SQLException("not in value access state");
}
if (arg >= args) {
throw new SQLException("arg " + arg + " out bounds [0," + args + ")");
}
}
/**
* Provides an interface for creating SQLite user-defined aggregate functions.
*
* @see Function
*/
public abstract static class Aggregate extends Function implements Cloneable {
/** @see org.sqlite.Function#xFunc() */
protected final void xFunc() {}
/**
* Defines the abstract aggregate callback function
*
* @throws SQLException
* @see <a
* href="http://www.sqlite.org/c3ref/aggregate_context.html">http://www.sqlite.org/c3ref/aggregate_context.html</a>
*/
protected abstract void xStep() throws SQLException;
/**
* Defines the abstract aggregate callback function
*
* @throws SQLException
* @see <a
* href="http://www.sqlite.org/c3ref/aggregate_context.html">http://www.sqlite.org/c3ref/aggregate_context.html</a>
*/
protected abstract void xFinal() throws SQLException;
/** @see java.lang.Object#clone() */
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
/**
* Provides an interface for creating SQLite user-defined window functions.
*
* @see Aggregate
*/
public abstract static class Window extends Aggregate {
/**
* Defines the abstract window callback function
*
* @throws SQLException
* @see <a
* href="https://sqlite.org/windowfunctions.html#user_defined_aggregate_window_functions">https://sqlite.org/windowfunctions.html#user_defined_aggregate_window_functions</a>
*/
protected abstract void xInverse() throws SQLException;
/**
* Defines the abstract window callback function
*
* @throws SQLException
* @see <a
* href="https://sqlite.org/windowfunctions.html#user_defined_aggregate_window_functions">https://sqlite.org/windowfunctions.html#user_defined_aggregate_window_functions</a>
*/
protected abstract void xValue() throws SQLException;
}
}