forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite3.c
2363 lines (1953 loc) · 64.8 KB
/
sqlite3.c
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Scott MacVicar <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_sqlite3.h"
#include "php_sqlite3_structs.h"
#include "main/SAPI.h"
#include <sqlite3.h>
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "SAPI.h"
ZEND_DECLARE_MODULE_GLOBALS(sqlite3)
static PHP_GINIT_FUNCTION(sqlite3);
static int php_sqlite3_authorizer(void *autharg, int access_type, const char *arg3, const char *arg4, const char *arg5, const char *arg6);
static void sqlite3_param_dtor(zval *data);
static int php_sqlite3_compare_stmt_zval_free(php_sqlite3_free_list **free_list, zval *statement);
/* {{{ Error Handler
*/
static void php_sqlite3_error(php_sqlite3_db_object *db_obj, char *format, ...)
{
va_list arg;
char *message;
va_start(arg, format);
vspprintf(&message, 0, format, arg);
va_end(arg);
if (db_obj && db_obj->exception) {
zend_throw_exception(zend_ce_exception, message, 0);
} else {
php_error_docref(NULL, E_WARNING, "%s", message);
}
if (message) {
efree(message);
}
}
/* }}} */
#define SQLITE3_CHECK_INITIALIZED(db_obj, member, class_name) \
if (!(db_obj) || !(member)) { \
php_sqlite3_error(db_obj, "The " #class_name " object has not been correctly initialised"); \
RETURN_FALSE; \
}
#define SQLITE3_CHECK_INITIALIZED_STMT(member, class_name) \
if (!(member)) { \
php_error_docref(NULL, E_WARNING, "The " #class_name " object has not been correctly initialised"); \
RETURN_FALSE; \
}
/* {{{ PHP_INI
*/
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("sqlite3.extension_dir", NULL, PHP_INI_SYSTEM, OnUpdateString, extension_dir, zend_sqlite3_globals, sqlite3_globals)
PHP_INI_END()
/* }}} */
/* Handlers */
static zend_object_handlers sqlite3_object_handlers;
static zend_object_handlers sqlite3_stmt_object_handlers;
static zend_object_handlers sqlite3_result_object_handlers;
/* Class entries */
zend_class_entry *php_sqlite3_sc_entry;
zend_class_entry *php_sqlite3_stmt_entry;
zend_class_entry *php_sqlite3_result_entry;
/* {{{ proto void SQLite3::open(String filename [, int Flags [, string Encryption Key]])
Opens a SQLite 3 Database, if the build includes encryption then it will attempt to use the key. */
PHP_METHOD(sqlite3, open)
{
php_sqlite3_db_object *db_obj;
zval *object = getThis();
char *filename, *encryption_key, *fullpath;
size_t filename_len, encryption_key_len = 0;
zend_long flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
db_obj = Z_SQLITE3_DB_P(object);
if (FAILURE == zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p|ls", &filename, &filename_len, &flags, &encryption_key, &encryption_key_len)) {
return;
}
if (db_obj->initialised) {
zend_throw_exception(zend_ce_exception, "Already initialised DB Object", 0);
return;
}
if (filename_len != 0 && (filename_len != sizeof(":memory:")-1 ||
memcmp(filename, ":memory:", sizeof(":memory:")-1) != 0)) {
if (!(fullpath = expand_filepath(filename, NULL))) {
zend_throw_exception(zend_ce_exception, "Unable to expand filepath", 0);
return;
}
if (php_check_open_basedir(fullpath)) {
zend_throw_exception_ex(zend_ce_exception, 0, "open_basedir prohibits opening %s", fullpath);
efree(fullpath);
return;
}
} else {
/* filename equals "" or ":memory:" */
fullpath = filename;
}
#if SQLITE_VERSION_NUMBER >= 3005000
if (sqlite3_open_v2(fullpath, &(db_obj->db), flags, NULL) != SQLITE_OK) {
#else
if (sqlite3_open(fullpath, &(db_obj->db)) != SQLITE_OK) {
#endif
zend_throw_exception_ex(zend_ce_exception, 0, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
if (fullpath != filename) {
efree(fullpath);
}
return;
}
#if SQLITE_HAS_CODEC
if (encryption_key_len > 0) {
if (sqlite3_key(db_obj->db, encryption_key, encryption_key_len) != SQLITE_OK) {
zend_throw_exception_ex(zend_ce_exception, 0, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
return;
}
}
#endif
db_obj->initialised = 1;
if (PG(open_basedir) && *PG(open_basedir)) {
sqlite3_set_authorizer(db_obj->db, php_sqlite3_authorizer, NULL);
}
if (fullpath != filename) {
efree(fullpath);
}
}
/* }}} */
/* {{{ proto bool SQLite3::close()
Close a SQLite 3 Database. */
PHP_METHOD(sqlite3, close)
{
php_sqlite3_db_object *db_obj;
zval *object = getThis();
int errcode;
db_obj = Z_SQLITE3_DB_P(object);
if (zend_parse_parameters_none() == FAILURE) {
return;
}
if (db_obj->initialised) {
zend_llist_clean(&(db_obj->free_list));
if(db_obj->db) {
errcode = sqlite3_close(db_obj->db);
if (errcode != SQLITE_OK) {
php_sqlite3_error(db_obj, "Unable to close database: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
RETURN_FALSE;
}
}
db_obj->initialised = 0;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool SQLite3::exec(String Query)
Executes a result-less query against a given database. */
PHP_METHOD(sqlite3, exec)
{
php_sqlite3_db_object *db_obj;
zval *object = getThis();
zend_string *sql;
char *errtext = NULL;
db_obj = Z_SQLITE3_DB_P(object);
SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S", &sql)) {
return;
}
if (sqlite3_exec(db_obj->db, ZSTR_VAL(sql), NULL, NULL, &errtext) != SQLITE_OK) {
php_sqlite3_error(db_obj, "%s", errtext);
sqlite3_free(errtext);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto Array SQLite3::version()
Returns the SQLite3 Library version as a string constant and as a number. */
PHP_METHOD(sqlite3, version)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
array_init(return_value);
add_assoc_string(return_value, "versionString", (char*)sqlite3_libversion());
add_assoc_long(return_value, "versionNumber", sqlite3_libversion_number());
return;
}
/* }}} */
/* {{{ proto int SQLite3::lastInsertRowID()
Returns the rowid of the most recent INSERT into the database from the database connection. */
PHP_METHOD(sqlite3, lastInsertRowID)
{
php_sqlite3_db_object *db_obj;
zval *object = getThis();
db_obj = Z_SQLITE3_DB_P(object);
SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
RETURN_LONG((zend_long) sqlite3_last_insert_rowid(db_obj->db));
}
/* }}} */
/* {{{ proto int SQLite3::lastErrorCode()
Returns the numeric result code of the most recent failed sqlite API call for the database connection. */
PHP_METHOD(sqlite3, lastErrorCode)
{
php_sqlite3_db_object *db_obj;
zval *object = getThis();
db_obj = Z_SQLITE3_DB_P(object);
SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
if (db_obj->initialised) {
RETURN_LONG(sqlite3_errcode(db_obj->db));
} else {
RETURN_LONG(0);
}
}
/* }}} */
/* {{{ proto string SQLite3::lastErrorMsg()
Returns english text describing the most recent failed sqlite API call for the database connection. */
PHP_METHOD(sqlite3, lastErrorMsg)
{
php_sqlite3_db_object *db_obj;
zval *object = getThis();
db_obj = Z_SQLITE3_DB_P(object);
SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
if (db_obj->initialised) {
RETURN_STRING((char *)sqlite3_errmsg(db_obj->db));
} else {
RETURN_EMPTY_STRING();
}
}
/* }}} */
/* {{{ proto bool SQLite3::busyTimeout(int msecs)
Sets a busy handler that will sleep until database is not locked or timeout is reached. Passing a value less than or equal to zero turns off all busy handlers. */
PHP_METHOD(sqlite3, busyTimeout)
{
php_sqlite3_db_object *db_obj;
zval *object = getThis();
zend_long ms;
#ifdef SQLITE_ENABLE_API_ARMOR
int return_code;
#endif
db_obj = Z_SQLITE3_DB_P(object);
SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ms)) {
return;
}
#ifdef SQLITE_ENABLE_API_ARMOR
return_code = sqlite3_busy_timeout(db_obj->db, ms);
if (return_code != SQLITE_OK) {
php_sqlite3_error(db_obj, "Unable to set busy timeout: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
RETURN_FALSE;
}
#else
php_ignore_value(sqlite3_busy_timeout(db_obj->db, ms));
#endif
RETURN_TRUE;
}
/* }}} */
#ifndef SQLITE_OMIT_LOAD_EXTENSION
/* {{{ proto bool SQLite3::loadExtension(String Shared Library)
Attempts to load an SQLite extension library. */
PHP_METHOD(sqlite3, loadExtension)
{
php_sqlite3_db_object *db_obj;
zval *object = getThis();
char *extension, *lib_path, *extension_dir, *errtext = NULL;
char fullpath[MAXPATHLEN];
size_t extension_len, extension_dir_len;
db_obj = Z_SQLITE3_DB_P(object);
SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &extension, &extension_len)) {
return;
}
#ifdef ZTS
if ((strncmp(sapi_module.name, "cgi", 3) != 0) &&
(strcmp(sapi_module.name, "cli") != 0) &&
(strncmp(sapi_module.name, "embed", 5) != 0)
) { php_sqlite3_error(db_obj, "Not supported in multithreaded Web servers");
RETURN_FALSE;
}
#endif
if (!SQLITE3G(extension_dir)) {
php_sqlite3_error(db_obj, "SQLite Extension are disabled");
RETURN_FALSE;
}
if (extension_len == 0) {
php_sqlite3_error(db_obj, "Empty string as an extension");
RETURN_FALSE;
}
extension_dir = SQLITE3G(extension_dir);
extension_dir_len = strlen(SQLITE3G(extension_dir));
if (IS_SLASH(extension_dir[extension_dir_len-1])) {
spprintf(&lib_path, 0, "%s%s", extension_dir, extension);
} else {
spprintf(&lib_path, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, extension);
}
if (!VCWD_REALPATH(lib_path, fullpath)) {
php_sqlite3_error(db_obj, "Unable to load extension at '%s'", lib_path);
efree(lib_path);
RETURN_FALSE;
}
efree(lib_path);
if (strncmp(fullpath, extension_dir, extension_dir_len) != 0) {
php_sqlite3_error(db_obj, "Unable to open extensions outside the defined directory");
RETURN_FALSE;
}
/* Extension loading should only be enabled for when we attempt to load */
sqlite3_enable_load_extension(db_obj->db, 1);
if (sqlite3_load_extension(db_obj->db, fullpath, 0, &errtext) != SQLITE_OK) {
php_sqlite3_error(db_obj, "%s", errtext);
sqlite3_free(errtext);
sqlite3_enable_load_extension(db_obj->db, 0);
RETURN_FALSE;
}
sqlite3_enable_load_extension(db_obj->db, 0);
RETURN_TRUE;
}
/* }}} */
#endif
/* {{{ proto int SQLite3::changes()
Returns the number of database rows that were changed (or inserted or deleted) by the most recent SQL statement. */
PHP_METHOD(sqlite3, changes)
{
php_sqlite3_db_object *db_obj;
zval *object = getThis();
db_obj = Z_SQLITE3_DB_P(object);
SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
RETURN_LONG(sqlite3_changes(db_obj->db));
}
/* }}} */
/* {{{ proto String SQLite3::escapeString(String value)
Returns a string that has been properly escaped. */
PHP_METHOD(sqlite3, escapeString)
{
zend_string *sql;
char *ret;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S", &sql)) {
return;
}
if (ZSTR_LEN(sql)) {
ret = sqlite3_mprintf("%q", ZSTR_VAL(sql));
if (ret) {
RETVAL_STRING(ret);
sqlite3_free(ret);
}
} else {
RETURN_EMPTY_STRING();
}
}
/* }}} */
/* {{{ proto SQLite3Stmt SQLite3::prepare(String Query)
Returns a prepared SQL statement for execution. */
PHP_METHOD(sqlite3, prepare)
{
php_sqlite3_db_object *db_obj;
php_sqlite3_stmt *stmt_obj;
zval *object = getThis();
zend_string *sql;
int errcode;
php_sqlite3_free_list *free_item;
db_obj = Z_SQLITE3_DB_P(object);
SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S", &sql)) {
return;
}
if (!ZSTR_LEN(sql)) {
RETURN_FALSE;
}
object_init_ex(return_value, php_sqlite3_stmt_entry);
stmt_obj = Z_SQLITE3_STMT_P(return_value);
stmt_obj->db_obj = db_obj;
ZVAL_COPY(&stmt_obj->db_obj_zval, object);
errcode = sqlite3_prepare_v2(db_obj->db, ZSTR_VAL(sql), ZSTR_LEN(sql), &(stmt_obj->stmt), NULL);
if (errcode != SQLITE_OK) {
php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
zval_dtor(return_value);
RETURN_FALSE;
}
stmt_obj->initialised = 1;
free_item = emalloc(sizeof(php_sqlite3_free_list));
free_item->stmt_obj = stmt_obj;
ZVAL_COPY_VALUE(&free_item->stmt_obj_zval, return_value);
zend_llist_add_element(&(db_obj->free_list), &free_item);
}
/* }}} */
/* {{{ proto SQLite3Result SQLite3::query(String Query)
Returns true or false, for queries that return data it will return a SQLite3Result object. */
PHP_METHOD(sqlite3, query)
{
php_sqlite3_db_object *db_obj;
php_sqlite3_result *result;
php_sqlite3_stmt *stmt_obj;
zval *object = getThis();
zval stmt;
zend_string *sql;
char *errtext = NULL;
int return_code;
db_obj = Z_SQLITE3_DB_P(object);
SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S", &sql)) {
return;
}
if (!ZSTR_LEN(sql)) {
RETURN_FALSE;
}
/* If there was no return value then just execute the query */
if (!USED_RET()) {
if (sqlite3_exec(db_obj->db, ZSTR_VAL(sql), NULL, NULL, &errtext) != SQLITE_OK) {
php_sqlite3_error(db_obj, "%s", errtext);
sqlite3_free(errtext);
}
return;
}
object_init_ex(&stmt, php_sqlite3_stmt_entry);
stmt_obj = Z_SQLITE3_STMT_P(&stmt);
stmt_obj->db_obj = db_obj;
ZVAL_COPY(&stmt_obj->db_obj_zval, object);
return_code = sqlite3_prepare_v2(db_obj->db, ZSTR_VAL(sql), ZSTR_LEN(sql), &(stmt_obj->stmt), NULL);
if (return_code != SQLITE_OK) {
php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
zval_ptr_dtor(&stmt);
RETURN_FALSE;
}
stmt_obj->initialised = 1;
object_init_ex(return_value, php_sqlite3_result_entry);
result = Z_SQLITE3_RESULT_P(return_value);
result->db_obj = db_obj;
result->stmt_obj = stmt_obj;
ZVAL_COPY_VALUE(&result->stmt_obj_zval, &stmt);
return_code = sqlite3_step(result->stmt_obj->stmt);
switch (return_code) {
case SQLITE_ROW: /* Valid Row */
case SQLITE_DONE: /* Valid but no results */
{
php_sqlite3_free_list *free_item;
free_item = emalloc(sizeof(php_sqlite3_free_list));
free_item->stmt_obj = stmt_obj;
free_item->stmt_obj_zval = stmt;
zend_llist_add_element(&(db_obj->free_list), &free_item);
sqlite3_reset(result->stmt_obj->stmt);
break;
}
default:
if (!EG(exception)) {
php_sqlite3_error(db_obj, "Unable to execute statement: %s", sqlite3_errmsg(db_obj->db));
}
sqlite3_finalize(stmt_obj->stmt);
stmt_obj->initialised = 0;
zval_dtor(return_value);
RETURN_FALSE;
}
}
/* }}} */
static void sqlite_value_to_zval(sqlite3_stmt *stmt, int column, zval *data) /* {{{ */
{
sqlite3_int64 val;
switch (sqlite3_column_type(stmt, column)) {
case SQLITE_INTEGER:
val = sqlite3_column_int64(stmt, column);
#if LONG_MAX <= 2147483647
if (val > ZEND_LONG_MAX || val < ZEND_LONG_MIN) {
ZVAL_STRINGL(data, (char *)sqlite3_column_text(stmt, column), sqlite3_column_bytes(stmt, column));
} else {
#endif
ZVAL_LONG(data, (zend_long) val);
#if LONG_MAX <= 2147483647
}
#endif
break;
case SQLITE_FLOAT:
ZVAL_DOUBLE(data, sqlite3_column_double(stmt, column));
break;
case SQLITE_NULL:
ZVAL_NULL(data);
break;
case SQLITE3_TEXT:
ZVAL_STRING(data, (char*)sqlite3_column_text(stmt, column));
break;
case SQLITE_BLOB:
default:
ZVAL_STRINGL(data, (char*)sqlite3_column_blob(stmt, column), sqlite3_column_bytes(stmt, column));
}
}
/* }}} */
/* {{{ proto SQLite3Result SQLite3::querySingle(String Query [, bool entire_row = false])
Returns a string of the first column, or an array of the entire row. */
PHP_METHOD(sqlite3, querySingle)
{
php_sqlite3_db_object *db_obj;
zval *object = getThis();
zend_string *sql;
char *errtext = NULL;
int return_code;
zend_bool entire_row = 0;
sqlite3_stmt *stmt;
db_obj = Z_SQLITE3_DB_P(object);
SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S|b", &sql, &entire_row)) {
return;
}
if (!ZSTR_LEN(sql)) {
RETURN_FALSE;
}
/* If there was no return value then just execute the query */
if (!USED_RET()) {
if (sqlite3_exec(db_obj->db, ZSTR_VAL(sql), NULL, NULL, &errtext) != SQLITE_OK) {
php_sqlite3_error(db_obj, "%s", errtext);
sqlite3_free(errtext);
}
return;
}
return_code = sqlite3_prepare_v2(db_obj->db, ZSTR_VAL(sql), ZSTR_LEN(sql), &stmt, NULL);
if (return_code != SQLITE_OK) {
php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
RETURN_FALSE;
}
return_code = sqlite3_step(stmt);
switch (return_code) {
case SQLITE_ROW: /* Valid Row */
{
if (!entire_row) {
sqlite_value_to_zval(stmt, 0, return_value);
} else {
int i = 0;
array_init(return_value);
for (i = 0; i < sqlite3_data_count(stmt); i++) {
zval data;
sqlite_value_to_zval(stmt, i, &data);
add_assoc_zval(return_value, (char*)sqlite3_column_name(stmt, i), &data);
}
}
break;
}
case SQLITE_DONE: /* Valid but no results */
{
if (!entire_row) {
RETVAL_NULL();
} else {
array_init(return_value);
}
break;
}
default:
if (!EG(exception)) {
php_sqlite3_error(db_obj, "Unable to execute statement: %s", sqlite3_errmsg(db_obj->db));
}
RETVAL_FALSE;
}
sqlite3_finalize(stmt);
}
/* }}} */
static int sqlite3_do_callback(struct php_sqlite3_fci *fc, zval *cb, int argc, sqlite3_value **argv, sqlite3_context *context, int is_agg) /* {{{ */
{
zval *zargs = NULL;
zval retval;
int i;
int ret;
int fake_argc;
php_sqlite3_agg_context *agg_context = NULL;
if (is_agg) {
is_agg = 2;
}
fake_argc = argc + is_agg;
fc->fci.size = sizeof(fc->fci);
ZVAL_COPY_VALUE(&fc->fci.function_name, cb);
fc->fci.object = NULL;
fc->fci.retval = &retval;
fc->fci.param_count = fake_argc;
/* build up the params */
if (fake_argc) {
zargs = (zval *)safe_emalloc(fake_argc, sizeof(zval), 0);
}
if (is_agg) {
/* summon the aggregation context */
agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
if (Z_ISUNDEF(agg_context->zval_context)) {
ZVAL_NULL(&agg_context->zval_context);
}
ZVAL_DUP(&zargs[0], &agg_context->zval_context);
ZVAL_LONG(&zargs[1], agg_context->row_count);
}
for (i = 0; i < argc; i++) {
switch (sqlite3_value_type(argv[i])) {
case SQLITE_INTEGER:
#if ZEND_LONG_MAX > 2147483647
ZVAL_LONG(&zargs[i + is_agg], sqlite3_value_int64(argv[i]));
#else
ZVAL_LONG(&zargs[i + is_agg], sqlite3_value_int(argv[i]));
#endif
break;
case SQLITE_FLOAT:
ZVAL_DOUBLE(&zargs[i + is_agg], sqlite3_value_double(argv[i]));
break;
case SQLITE_NULL:
ZVAL_NULL(&zargs[i + is_agg]);
break;
case SQLITE_BLOB:
case SQLITE3_TEXT:
default:
ZVAL_STRINGL(&zargs[i + is_agg], (char*)sqlite3_value_text(argv[i]), sqlite3_value_bytes(argv[i]));
break;
}
}
fc->fci.params = zargs;
if ((ret = zend_call_function(&fc->fci, &fc->fcc)) == FAILURE) {
php_error_docref(NULL, E_WARNING, "An error occurred while invoking the callback");
}
if (is_agg) {
zval_ptr_dtor(&zargs[0]);
}
/* clean up the params */
if (fake_argc) {
for (i = is_agg; i < argc + is_agg; i++) {
zval_ptr_dtor(&zargs[i]);
}
if (is_agg) {
zval_ptr_dtor(&zargs[1]);
}
efree(zargs);
}
if (!is_agg || !argv) {
/* only set the sqlite return value if we are a scalar function,
* or if we are finalizing an aggregate */
if (!Z_ISUNDEF(retval)) {
switch (Z_TYPE(retval)) {
case IS_LONG:
#if ZEND_LONG_MAX > 2147483647
sqlite3_result_int64(context, Z_LVAL(retval));
#else
sqlite3_result_int(context, Z_LVAL(retval));
#endif
break;
case IS_NULL:
sqlite3_result_null(context);
break;
case IS_DOUBLE:
sqlite3_result_double(context, Z_DVAL(retval));
break;
default:
convert_to_string_ex(&retval);
sqlite3_result_text(context, Z_STRVAL(retval), Z_STRLEN(retval), SQLITE_TRANSIENT);
break;
}
} else {
sqlite3_result_error(context, "failed to invoke callback", 0);
}
if (agg_context && !Z_ISUNDEF(agg_context->zval_context)) {
zval_ptr_dtor(&agg_context->zval_context);
}
} else {
/* we're stepping in an aggregate; the return value goes into
* the context */
if (agg_context && !Z_ISUNDEF(agg_context->zval_context)) {
zval_ptr_dtor(&agg_context->zval_context);
}
ZVAL_COPY_VALUE(&agg_context->zval_context, &retval);
ZVAL_UNDEF(&retval);
}
if (!Z_ISUNDEF(retval)) {
zval_ptr_dtor(&retval);
}
return ret;
}
/* }}}*/
static void php_sqlite3_callback_func(sqlite3_context *context, int argc, sqlite3_value **argv) /* {{{ */
{
php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
sqlite3_do_callback(&func->afunc, &func->func, argc, argv, context, 0);
}
/* }}}*/
static void php_sqlite3_callback_step(sqlite3_context *context, int argc, sqlite3_value **argv) /* {{{ */
{
php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
php_sqlite3_agg_context *agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
agg_context->row_count++;
sqlite3_do_callback(&func->astep, &func->step, argc, argv, context, 1);
}
/* }}} */
static void php_sqlite3_callback_final(sqlite3_context *context) /* {{{ */
{
php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
php_sqlite3_agg_context *agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
agg_context->row_count = 0;
sqlite3_do_callback(&func->afini, &func->fini, 0, NULL, context, 1);
}
/* }}} */
static int php_sqlite3_callback_compare(void *coll, int a_len, const void *a, int b_len, const void* b) /* {{{ */
{
php_sqlite3_collation *collation = (php_sqlite3_collation*)coll;
zval *zargs = NULL;
zval retval;
int ret;
collation->fci.fci.size = (sizeof(collation->fci.fci));
ZVAL_COPY_VALUE(&collation->fci.fci.function_name, &collation->cmp_func);
collation->fci.fci.object = NULL;
collation->fci.fci.retval = &retval;
collation->fci.fci.param_count = 2;
zargs = safe_emalloc(2, sizeof(zval), 0);
ZVAL_STRINGL(&zargs[0], a, a_len);
ZVAL_STRINGL(&zargs[1], b, b_len);
collation->fci.fci.params = zargs;
if (!EG(exception)) {
//Exception occurred on previous callback. Don't attempt to call function
if ((ret = zend_call_function(&collation->fci.fci, &collation->fci.fcc)) == FAILURE) {
php_error_docref(NULL, E_WARNING, "An error occurred while invoking the compare callback");
}
} else {
ZVAL_UNDEF(&retval);
}
zval_ptr_dtor(&zargs[0]);
zval_ptr_dtor(&zargs[1]);
efree(zargs);
if (EG(exception)) {
ret = 0;
} else if (Z_TYPE(retval) != IS_LONG){
//retval ought to contain a ZVAL_LONG by now
// (the result of a comparison, i.e. most likely -1, 0, or 1)
//I suppose we could accept any scalar return type, though.
php_error_docref(NULL, E_WARNING, "An error occurred while invoking the compare callback (invalid return type). Collation behaviour is undefined.");
} else {
ret = Z_LVAL(retval);
}
zval_ptr_dtor(&retval);
return ret;
}
/* }}} */
/* {{{ proto bool SQLite3::createFunction(string name, mixed callback [, int argcount])
Allows registration of a PHP function as a SQLite UDF that can be called within SQL statements. */
PHP_METHOD(sqlite3, createFunction)
{
php_sqlite3_db_object *db_obj;
zval *object = getThis();
php_sqlite3_func *func;
char *sql_func;
size_t sql_func_len;
zval *callback_func;
zend_string *callback_name;
zend_long sql_func_num_args = -1;
db_obj = Z_SQLITE3_DB_P(object);
SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz|l", &sql_func, &sql_func_len, &callback_func, &sql_func_num_args) == FAILURE) {
return;
}
if (!sql_func_len) {
RETURN_FALSE;
}
if (!zend_is_callable(callback_func, 0, &callback_name)) {
php_sqlite3_error(db_obj, "Not a valid callback function %s", ZSTR_VAL(callback_name));
zend_string_release(callback_name);
RETURN_FALSE;
}
zend_string_release(callback_name);
func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, SQLITE_UTF8, func, php_sqlite3_callback_func, NULL, NULL) == SQLITE_OK) {
func->func_name = estrdup(sql_func);
ZVAL_COPY(&func->func, callback_func);
func->argc = sql_func_num_args;
func->next = db_obj->funcs;
db_obj->funcs = func;
RETURN_TRUE;
}
efree(func);
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool SQLite3::createAggregate(string name, mixed step, mixed final [, int argcount])
Allows registration of a PHP function for use as an aggregate. */
PHP_METHOD(sqlite3, createAggregate)
{
php_sqlite3_db_object *db_obj;
zval *object = getThis();
php_sqlite3_func *func;
char *sql_func;
zend_string *callback_name;
size_t sql_func_len;
zval *step_callback, *fini_callback;
zend_long sql_func_num_args = -1;
db_obj = Z_SQLITE3_DB_P(object);
SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
if (zend_parse_parameters(ZEND_NUM_ARGS(), "szz|l", &sql_func, &sql_func_len, &step_callback, &fini_callback, &sql_func_num_args) == FAILURE) {
return;
}
if (!sql_func_len) {
RETURN_FALSE;
}
if (!zend_is_callable(step_callback, 0, &callback_name)) {
php_sqlite3_error(db_obj, "Not a valid callback function %s", ZSTR_VAL(callback_name));
zend_string_release(callback_name);
RETURN_FALSE;
}
zend_string_release(callback_name);
if (!zend_is_callable(fini_callback, 0, &callback_name)) {
php_sqlite3_error(db_obj, "Not a valid callback function %s", ZSTR_VAL(callback_name));
zend_string_release(callback_name);
RETURN_FALSE;
}
zend_string_release(callback_name);
func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, SQLITE_UTF8, func, NULL, php_sqlite3_callback_step, php_sqlite3_callback_final) == SQLITE_OK) {
func->func_name = estrdup(sql_func);
ZVAL_COPY(&func->step, step_callback);
ZVAL_COPY(&func->fini, fini_callback);
func->argc = sql_func_num_args;
func->next = db_obj->funcs;
db_obj->funcs = func;
RETURN_TRUE;