File: sqlite_basic.c

package info (click to toggle)
libopendbx 1.4.6-18
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,024 kB
  • sloc: sh: 10,899; ansic: 7,284; xml: 1,837; cpp: 1,696; makefile: 316; sed: 16
file content (502 lines) | stat: -rw-r--r-- 10,475 bytes parent folder | download | duplicates (7)
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
 *  OpenDBX - A simple but extensible database abstraction layer
 *  Copyright (C) 2004-2008 Norbert Sendetzky and others
 *
 *  Distributed under the terms of the GNU Library General Public Licence
 * version 2 or (at your option) any later version.
 */



#include "sqlite_basic.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>



/*
 *  Declaration of SQLite capabilities
 */

struct odbx_basic_ops sqlite_odbx_basic_ops = {
	.init = sqlite_odbx_init,
	.bind = sqlite_odbx_bind,
	.unbind = sqlite_odbx_unbind,
	.finish = sqlite_odbx_finish,
	.get_option = sqlite_odbx_get_option,
	.set_option = sqlite_odbx_set_option,
	.error = sqlite_odbx_error,
	.error_type = sqlite_odbx_error_type,
	.escape = NULL,
	.query = sqlite_odbx_query,
	.result = sqlite_odbx_result,
	.result_finish = sqlite_odbx_result_finish,
	.rows_affected = sqlite_odbx_rows_affected,
	.row_fetch = sqlite_odbx_row_fetch,
	.column_count = sqlite_odbx_column_count,
	.column_name = sqlite_odbx_column_name,
	.column_type = sqlite_odbx_column_type,
	.field_length = sqlite_odbx_field_length,
	.field_value = sqlite_odbx_field_value,
};



/*
 *  Private sqlite error messages
 */

static const char* sqlite_odbx_errmsg[] = {
	gettext_noop("Opening database failed")
};



/*
 *  ODBX basic operations
 *  SQLite style
 */


/*
 *  SQLite doesn't connect to a database server. Instead it opens the database
 *  (a file on the harddisk) directly. Thus, host doesn't contain the name of a
 *  computer but of a directory in the filesystem instead. Port is unused.
 */

static int sqlite_odbx_init( odbx_t* handle, const char* host, const char* port )
{
	if( ( handle->aux = malloc( sizeof( struct sconn ) ) ) == NULL )
	{
		return -ODBX_ERR_NOMEM;
	}

	struct sconn* aux = handle->aux;

	aux->pathlen = 0;
	aux->path = NULL;
	aux->errmsg = NULL;
	handle->generic = NULL;

	if( host != NULL )
	{
		aux->pathlen = strlen( host );   /* host == directory */

		if( ( aux->path = malloc( aux->pathlen + 1 ) ) == NULL )
		{
			free( handle->aux );
			handle->aux = NULL;

			return -ODBX_ERR_NOMEM;
		}

		snprintf( aux->path, aux->pathlen + 1, "%s", host );
	}

	return ODBX_ERR_SUCCESS;
}



/*
 *  username and password are not used by SQLite. It relies on the filesystem
 *  access control mechanisms for allowing or preventing database access.
 */

static int sqlite_odbx_bind( odbx_t* handle, const char* database, const char* who, const char* cred, int method )
{
	struct sconn* aux = handle->aux;

	if( aux == NULL ) { return -ODBX_ERR_PARAM; }
	if( method != ODBX_BIND_SIMPLE ) { return -ODBX_ERR_NOTSUP; }

	aux->errmsg = NULL;
	size_t flen = strlen( database ) + 1;

	if( ( aux->path = realloc( aux->path, aux->pathlen + flen + 1 ) ) == NULL )
	{
		return -ODBX_ERR_NOMEM;
	}

	snprintf( aux->path + aux->pathlen, flen + 1, "%s", database );

	/*  The second parameter is currently unused. */
	if( ( handle->generic = (void*) sqlite_open( aux->path, 0, NULL ) ) == NULL )
	{
		aux->errno = SQLITE_CANTOPEN;
		aux->errmsg = (char*) dgettext( "opendbx", sqlite_odbx_errmsg[0] );
		return -ODBX_ERR_BACKEND;
	}

	int err;

	if( ( err = sqlite_exec( (sqlite*) handle->generic, "PRAGMA empty_result_callbacks = ON;", NULL, NULL, NULL ) ) != SQLITE_OK )
	{
		aux->errno = err;
		aux->errmsg = (char*) sqlite_error_string( err );
		return -ODBX_ERR_BACKEND;
	}

	return ODBX_ERR_SUCCESS;
}



static int sqlite_odbx_unbind( odbx_t* handle )
{
	struct sconn* aux = (struct sconn*) handle->aux;

	if( handle->generic != NULL && aux != NULL )
	{
		sqlite_close( (sqlite*) handle->generic );

		handle->generic = NULL;
		aux->errmsg = NULL;

		return ODBX_ERR_SUCCESS;
	}

	return -ODBX_ERR_PARAM;
}



static int sqlite_odbx_finish( odbx_t* handle )
{
	if( handle->aux != NULL )
	{
		free( ((struct sconn*) handle->aux)->path );
		free(  handle->aux );
		handle->aux = NULL;

		return ODBX_ERR_SUCCESS;
	}

	return -ODBX_ERR_PARAM;
}



static int sqlite_odbx_get_option( odbx_t* handle, unsigned int option, void* value )
{
	if( handle->aux == NULL )
	{
		return -ODBX_ERR_PARAM;
	}

	((struct sconn*) handle->aux)->errmsg = NULL;

	switch( option )
	{
		case ODBX_OPT_API_VERSION:
			*(int*) value = APINUMBER;
			break;
		case ODBX_OPT_THREAD_SAFE:   /* FIXME: How to find out if THREADSAFE was set while sqlite compilation */
		case ODBX_OPT_TLS:
		case ODBX_OPT_MULTI_STATEMENTS:
		case ODBX_OPT_PAGED_RESULTS:
		case ODBX_OPT_COMPRESS:
		case ODBX_OPT_CONNECT_TIMEOUT:
			*(int*) value = ODBX_DISABLE;
			break;
		default:
			return -ODBX_ERR_OPTION;
	}

	return ODBX_ERR_SUCCESS;
}



static int sqlite_odbx_set_option( odbx_t* handle, unsigned int option, void* value )
{
	if( handle->aux == NULL )
	{
		return -ODBX_ERR_PARAM;
	}

	((struct sconn*) handle->aux)->errmsg = NULL;

	switch( option )
	{
		case ODBX_OPT_API_VERSION:
		case ODBX_OPT_THREAD_SAFE:
			return -ODBX_ERR_OPTRO;
		case ODBX_OPT_TLS:
		case ODBX_OPT_MULTI_STATEMENTS:
		case ODBX_OPT_PAGED_RESULTS:
		case ODBX_OPT_COMPRESS:
		case ODBX_OPT_CONNECT_TIMEOUT:
			break;
		default:
			return -ODBX_ERR_OPTION;
	}

	return -ODBX_ERR_OPTWR;
}



static const char* sqlite_odbx_error( odbx_t* handle )
{
	if( handle->aux != NULL )
	{
		return ((struct sconn*) handle->aux)->errmsg;
	}

	return NULL;
}



static int sqlite_odbx_error_type( odbx_t* handle )
{
	if( handle->aux != NULL )
	{
		switch( ((struct sconn*) handle->aux)->errno )
		{
			case SQLITE_OK:
				return 0;
			case SQLITE_PERM:
			case SQLITE_NOMEM:
			case SQLITE_READONLY:
			case SQLITE_IOERR:
			case SQLITE_CORRUPT:
			case SQLITE_FULL:
			case SQLITE_CANTOPEN:
			case SQLITE_NOLFS:
			case SQLITE_AUTH:
				return -1;
		}
	}

	return 1;
}



static int sqlite_odbx_query( odbx_t* handle, const char* query, unsigned long length )
{
	struct sconn* aux = (struct sconn*) handle->aux;

	if( aux == NULL || query == NULL )
	{
		return -ODBX_ERR_PARAM;
	}

	if( ( aux->stmt = malloc( length + 1 ) ) == NULL )
	{
		return -ODBX_ERR_NOMEM;
	}

	memcpy( aux->stmt, query, length );
	aux->stmt[length] = 0;

	return ODBX_ERR_SUCCESS;
}



static int sqlite_odbx_result( odbx_t* handle, odbx_result_t** result, struct timeval* timeout, unsigned long chunk )
{
	char** res;
	long ms = 0;
	int err, nrow, ncolumn;
	struct sres* sres;
	struct sconn* aux = (struct sconn*) handle->aux;


	if( handle->generic == NULL || aux == NULL )
	{
		return -ODBX_ERR_PARAM;
	}

	aux->errmsg = NULL;
	if( aux->stmt == NULL )
	{
		return ODBX_RES_DONE;   /* no more results */
	}

	if( timeout != NULL )
	{
		ms = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
	}

	while( ( err = sqlite_get_table( (sqlite*) handle->generic, aux->stmt, &res, &nrow, &ncolumn, NULL ) ) == SQLITE_BUSY )
	{
		if( ms <= 0 ) { return ODBX_RES_TIMEOUT; }   /* Timeout */

		sqlite_busy_timeout( (sqlite*) handle->generic, 100 );
		ms -= 100;
	}

	free( aux->stmt );
	aux->stmt = NULL;

	if( err != SQLITE_OK )
	{
		aux->errno = err;
		aux->errmsg = (char*) sqlite_error_string( err );
		return -ODBX_ERR_BACKEND;
	}

	if( ( *result = (odbx_result_t*) malloc( sizeof( struct odbx_result_t ) ) ) == NULL )
	{
		return -ODBX_ERR_NOMEM;
	}

	if( ( sres = (struct sres*) malloc( sizeof( struct sres ) ) ) == NULL )
	{
		free( *result );
		*result = NULL;

		return -ODBX_ERR_NOMEM;
	}

	(*result)->generic = (void*) res;
	(*result)->aux = (void*) sres;
	sres->ncolumn = ncolumn;
	sres->nrow = nrow;
	sres->cur = -1;

	if( !ncolumn ) { return ODBX_RES_NOROWS; }   /* empty or not SELECT like query */

	return ODBX_RES_ROWS;   /* result is available */
}



static int sqlite_odbx_result_finish( odbx_result_t* result )
{
	if( result->handle != NULL && result->handle->aux != NULL )
	{
		((struct sconn*) result->handle->aux)->errmsg = NULL;
	}

	sqlite_free_table( (char**) result->generic );

	if( result->aux != NULL )
	{
		free( result->aux );
		result->aux = NULL;
	}

	free( result );

	return ODBX_ERR_SUCCESS;
}



static int sqlite_odbx_row_fetch( odbx_result_t* result )
{
	struct sres* res = (struct sres*) result->aux;

	if( result->aux != NULL && result->handle != NULL && result->handle->aux != NULL )
	{
		((struct sconn*) result->handle->aux)->errmsg = NULL;

		res->cur++;
		if( res->cur < res->nrow ) { return ODBX_ROW_NEXT; }

		return ODBX_ROW_DONE;
	}

	return -ODBX_ERR_PARAM;
}



static uint64_t sqlite_odbx_rows_affected( odbx_result_t* result )
{
	if( result->handle != NULL && result->handle->aux != NULL )
	{
		((struct sconn*) result->handle->aux)->errmsg = NULL;
		return (uint64_t) sqlite_changes( (sqlite*) result->handle->generic );
	}

	return 0;
}



static unsigned long sqlite_odbx_column_count( odbx_result_t* result )
{
	if( result->handle != NULL && result->handle->aux != NULL && result->aux != NULL )
	{
		((struct sconn*) result->handle->aux)->errmsg = NULL;
		return ((struct sres*) result->aux)->ncolumn;
	}

	return 0;
}



static const char* sqlite_odbx_column_name( odbx_result_t* result, unsigned long pos )
{
	if( result->handle != NULL && result->handle->aux != NULL && result->aux != NULL )
	{
		((struct sconn*) result->handle->aux)->errmsg = NULL;
		if( result->generic != NULL && pos < ((struct sres*) result->aux)->ncolumn )
		{
			return ((const char**) result->generic)[pos];
		}
	}

	return NULL;
}



static int sqlite_odbx_column_type( odbx_result_t* result, unsigned long pos )
{
	if( result->handle != NULL && result->handle->aux != NULL )
	{
		((struct sconn*) result->handle->aux)->errmsg = NULL;
		return ODBX_TYPE_CLOB;
	}

	return -ODBX_ERR_PARAM;
}



static unsigned long sqlite_odbx_field_length( odbx_result_t* result, unsigned long pos )
{
	if( result->handle != NULL && result->handle->aux != NULL && result->aux != NULL )
	{
		struct sres* aux = (struct sres*) result->aux;
		((struct sconn*) result->handle->aux)->errmsg = NULL;

		if( result->generic && pos < aux->ncolumn )
		{
			int num = aux->cur * aux->ncolumn + aux->ncolumn + pos;
			return (unsigned long) strlen( ((char**) result->generic)[num] );
		}
	}

	return 0;
}



static const char* sqlite_odbx_field_value( odbx_result_t* result, unsigned long pos )
{
	if( result->handle != NULL && result->handle->aux != NULL && result->aux != NULL )
	{
		struct sres* aux = (struct sres*) result->aux;
		((struct sconn*) result->handle->aux)->errmsg = NULL;

		if( result->generic && pos < aux->ncolumn )
		{
			int num = aux->ncolumn + aux->cur * aux->ncolumn + pos;
			return  ((const char**) result->generic)[num];
		}
	}

	return NULL;
}