Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[r9343]: incubator / ooSQLite / examples / user.extensions / examplePackage.cpp Maximize Restore History

Download this file

examplePackage.cpp    370 lines (339 with data), 14.1 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
367
368
369
/*----------------------------------------------------------------------------*/
/* */
/* Copyright (c) 2013-2013 Rexx Language Association. All rights reserved. */
/* */
/* This program and the accompanying materials are made available under */
/* the terms of the Common Public License v1.0 which accompanies this */
/* distribution. A copy is also available at the following address: */
/* http://www.oorexx.org/license.html */
/* */
/* Redistribution and use in source and binary forms, with or */
/* without modification, are permitted provided that the following */
/* conditions are met: */
/* */
/* Redistributions of source code must retain the above copyright */
/* notice, this list of conditions and the following disclaimer. */
/* Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in */
/* the documentation and/or other materials provided with the distribution. */
/* */
/* Neither the name of Rexx Language Association nor the names */
/* of its contributors may be used to endorse or promote products */
/* derived from this software without specific prior written permission. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
/* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
/* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/*----------------------------------------------------------------------------*/
/**
* examplePackage.cpp
*
* Example of how to write a loadable package file for ooSQLite. This file is
* built as a shared library, which can be loaded through the ??() class
* method of the ooSQLite class.
*
* Note these points:
*
*
*
*
*
*
*/
#include "oosqlPackage.hpp"
#ifdef _WIN32
#include <windows.h>
#endif
#include <string.h>
#include <stdio.h> // printf() if needed
/**
* ASCII to EBCDIC tranlation table. Not sure what version of EBCDIC, values
* taken directly from an ooRexx contributor.
*/
static unsigned char asciiToEbcdic[ 256 ] =
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D, 0x4D,
0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8,
0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
0xE8, 0xE9, 0xAD, 0xE0, 0xBD, 0x5F, 0x6D,
0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B
};
/**
* EBCDIC to ASCII translation table. This is an exact inverse of the above.
* The table is not used here in this code. It is present simply to be
* complete, the 2 tables could be used in some other application that translate
* from and to ASCII.
*/
static unsigned char ebcdicToAscii[256] =
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
0x2E, 0x2E, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x39, 0x3A, 0x3B, 0x3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E,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x39, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E
};
/**
* The half() SQL function returns half of its input value. This is an example
* from the SQLite website.
*/
void half(sqlite3_context *context, int argc, sqlite3_value **argv)
{
sqlite3_result_double(context, 0.5*sqlite3_value_double(argv[0]));
}
/**
* An example collation, essentially a reverse sort.
*
* @param data
* @param len1
* @param str1
* @param len2
* @param str2
*
* @return int
*/
int reverse(void* data, int len1, const void* str1, int len2, const void* str2)
{
return - sqlite3_strnicmp((char *)str1, (char *)str2, len1 < len2 ? len1 : len2);
}
/**
* An example collation, it allows ascii data to be ordered as if it were
* ebcdic.
*
* @param data
* @param len1
* @param str1
* @param len2
* @param str2
*
* @return int
*/
static int ebcdic(void* data, int len1, const void* str1, int len2, const void* str2)
{
register unsigned char *a = (unsigned char *)str1;
register unsigned char *b = (unsigned char *)str2;
int min = len1 < len2 ? len1 : len2;
int i;
if ( min == 0 )
{
// I don't think this is possible, but just to make sure
return len1 - len2;
}
for ( i = 0; i < min, asciiToEbcdic[*a] == asciiToEbcdic[*b]; i++ )
{
a++;
b++;
}
return (i == min) ? len1 - len2 : asciiToEbcdic[*a] - asciiToEbcdic[*b];
}
/* struct for use in the strAggregate aggregate function below. */
typedef struct _StrAggCntx
{
size_t count;
char *result;
} StrAggCntx;
typedef StrAggCntx *pStrAggCntx;
/**
* An example of a user defined aggregate. This example is a simple rewrite of
* an example in "Definitive Guide to SQLite", Second Edition, by Grant Allen
* and Mike Owens, published by Apress.
*
* The aggregate expects 1 text argument and concatenates each string to produce
* its result.
*
* An example usage:
*
* SELECT season, strAggregate(name) from episodes group by season;
*
* This will concatenate the name of each episode into a single string listing
* each episode name, grouped by season.
*
* Each aggregate can, and probably needs to, allocate its individual block to
* keep track of what it is aggragating. This then is specific to the unique
* instance of the aggregate. Allocationg the block is done through
* sqlite3_aggregate_context().
*
* At the first invocation, this SQLite function will allocated a zero filled
* block of the size requested. On each subsequent invocation, this function
* simple returns the already allocated block.
*
* After that, is is simply a matter of concatenating the new string to the
* accumulated result string.
*
* @param sqlCntx
* @param countArgs
* @param values
*/
void strAggStep(sqlite3_context* sqlCntx, int countArgs, sqlite3_value** values)
{
pStrAggCntx psac = (pStrAggCntx)sqlite3_aggregate_context(sqlCntx, sizeof(StrAggCntx));
if ( psac == NULL )
{
sqlite3_result_error_nomem(sqlCntx);
return;
}
static const char comma[] = ", ";
const char *text = (const char *)sqlite3_value_text(values[0]);
size_t len = strlen(text);
if ( psac->result == NULL )
{
psac->result = (char *)sqlite3_malloc((int)len + 1);
if ( psac->result == NULL )
{
sqlite3_result_error_nomem(sqlCntx);
return;
}
memcpy(psac->result, text, len + 1);
psac->count = len;
}
else
{
const size_t commaLen = strlen(comma);
psac->result = (char *)sqlite3_realloc(psac->result, (int)(psac->count + len + commaLen + 1));
memcpy(psac->result + psac->count, comma, commaLen);
psac->count += commaLen;
memcpy(psac->result + psac->count, text, len + 1);
psac->count += len;
}
}
/**
* Implementation of the xFinal function for the strAggregate user define
* aggregate.
*
* Here we simple set the result of the aggregate to the text value in our
* accumulator.
*
* @param sqlCntx
*/
void strAggFinalize(sqlite3_context* sqlCntx)
{
pStrAggCntx psac = (pStrAggCntx) sqlite3_aggregate_context(sqlCntx, sizeof(StrAggCntx));
if ( psac == NULL || psac->result == NULL )
{
sqlite3_result_error_nomem(sqlCntx);
return;
}
sqlite3_result_text(sqlCntx, psac->result, (int)psac->count, sqlite3_free);
}
/**
* examplePackage_collations is an array of SQLiteCollationEntry structs. See
* the SQLiteCollationEntry struct in oosqlPackage.hpp for the fields in the
* struct.
*
* The OOSQL_COLLATION() is intended to make it somewhat easier to fill in the
* struct fields. But, it might be just as easy to skip using it and fill in
* the fields individually.
*
* Note that, with the OOSQL_COLLATION macro, the string name of the collation
* is derived from the function name. If you do not use the macro, the second
* entry must be the string name for the collation. That is the name used by
* SQLite.
*/
SQLiteCollationEntry examplePackage_collations[] = {
// The REVERSE collation. No destroy callback, no user data pointer, no
// collation needed callback.
OOSQL_COLLATION(reverse, reverse, NULL, NULL, NULL),
// The EBCDIC collation. We fill out the struct fields completely instead of
// using the OOSQL_COLLATION macro.
{NULL, "ebcdic", ebcdic, NULL, NULL, NULL, 0},
// The last entry in the struc, must always be present. Here the macro is
// simplier to use. To fill it in manually, use all NULLs.
OOSQL_LAST_COLLATION()
};
/**
* examplePackage_functions is an array of SQLiteFunctionEntry structs. See
* oosqlPackage.hpp for its definition.
*
* Again, the OOSQL_FUNCTION macro() is meant to make things a little easier.
* You can always assing the field values manually as shown above for the
* collations.
*/
SQLiteFunctionEntry examplePackage_functions[] = {
// Only 1 function. Only the function callback is used, the other callbacks
// are NULL. The function callback takes 1 arg.
OOSQL_FUNCTION(half, half, NULL, NULL, NULL, NULL, NULL, 1),
{NULL, "strAggregate", NULL, strAggStep, strAggFinalize, NULL, NULL, NULL, 1, 0},
OOSQL_LAST_FUNCTION()
};
/**
* Each package must fill out the package entry struct
*
*/
ooSQLitePackageEntry examplePackage_package_entry =
{
OOSQL_STANDARD_PACKAGE_HEADER
OOSQLITE_1_0_0, // needs at least the 1.0.0 ooSQLite
SQLITE_VERSION_NUMBER,
"examplePackage", // name of the package
"0.0.1", // package information
NULL, // no exported collation needed function
examplePackage_collations, // the exported collations
examplePackage_functions, // the exported functions
NULL // no exported modules
};
// This macro expands to the package entry function used to load the package.
// The name "examplePackage" must match the prefix of _package_entry in the
// ooSQLitePackage entry table above.
OOSQLITE_GET_PACKAGE(examplePackage);