php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | |
Patch php_sqlite3_create_collation.patch for SQLite related Bug #60871Patch version 2012-01-24 19:10 UTC Return to Bug #60871 | Download this patchThis patch is obsolete Obsoleted by patches: Patch Revisions: 2012-01-29 02:52 UTC | 2012-01-24 19:10 UTCDeveloper: macdewar@gmail.comdiff -purN ext/sqlite3/CREDITS ext/sqlite3/CREDITS --- ext/sqlite3/CREDITS 2011-05-16 10:02:07.000000000 -0300 +++ ext/sqlite3/CREDITS 2012-01-28 22:20:30.644447481 -0400 --- ext/sqlite3/CREDITS 2010-12-11 10:57:34.000000000 -0400 +++ ext/sqlite3/CREDITS 2012-01-24 10:40:21.616705725 -0400 @@ -1,2 +1,2 @@ SQLite3 -Scott MacVicar, Ilia Alshanetsky +Scott MacVicar, Ilia Alshanetsky, Brad Dewar diff -purN ext/sqlite3/php_sqlite3_structs.h ext/sqlite3/php_sqlite3_structs.h --- ext/sqlite3/php_sqlite3_structs.h 2012-01-04 10:32:43.000000000 -0400 +++ ext/sqlite3/php_sqlite3_structs.h 2012-01-28 22:22:19.675084351 -0400 --- ext/sqlite3/php_sqlite3_structs.h 2012-01-24 10:33:41.842377103 -0400 +++ ext/sqlite3/php_sqlite3_structs.h 2012-01-24 14:15:15.815330445 -0400 @@ -62,12 +62,23 @@ typedef struct _php_sqlite3_func { struct php_sqlite3_fci afunc, astep, afini; } php_sqlite3_func; +/* Structure for SQLite collation function */ +typedef struct _php_sqlite3_collation { + struct _php_sqlite3_collation *next; + + const char *collation_name; + + const char *coll_name; + zval *cmp_func; + struct php_sqlite3_fci fci; +} php_sqlite3_collation; + zend_bool exception; zend_llist free_list; diff -purN ext/sqlite3/sqlite3.c ext/sqlite3/sqlite3.c --- ext/sqlite3/sqlite3.c 2012-01-04 10:32:43.000000000 -0400 +++ ext/sqlite3/sqlite3.c 2012-01-28 22:43:16.268596032 -0400 @@ -848,6 +848,60 @@ static void php_sqlite3_callback_final(s --- ext/sqlite3/sqlite3.c 2012-01-24 10:34:09.946023564 -0400 +++ ext/sqlite3/sqlite3.c 2012-01-24 14:34:06.951228044 -0400 @@ -851,6 +851,65 @@ static void php_sqlite3_callback_final(s } /* }}} */ +static int php_sqlite3_callback_compare(void *coll, int a_len, const void *a, int b_len, const void* b) /* {{{ */ + zval *retval = NULL; + int ret; + + TSRMLS_FETCH(); + + //perhaps the rest should separated into its own function, but it's never called again, so it's inline here. + + collation->fci.fci.size = (sizeof(collation->fci.fci)); + collation->fci.fci.function_table = EG(function_table); + collation->fci.fci.function_name = collation->cmp_func; + ZVAL_STRINGL(*zargs[1], b, b_len, 1); + + collation->fci.fci.params = zargs; + + if ((ret = zend_call_function(&collation->fci.fci, &collation->fci.fcc TSRMLS_CC)) == FAILURE) { + if ((ret = zend_call_function(&collation->fci.fci, &coll->fci.fcc TSRMLS_CC)) == FAILURE) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the compare callback"); + } + + zval_ptr_dtor(zargs[0]); + + 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) @@ -958,6 +1012,53 @@ PHP_METHOD(sqlite3, createAggregate) @@ -961,6 +1020,54 @@ PHP_METHOD(sqlite3, createAggregate) } /* }}} */ +/* {{{ proto bool SQLite3::createCollation(string name, mixed callback) + zval *callback_func; + db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC); + + SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3) + + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &collation_name, &collation_name_len, &callback_func) == FAILURE) { + RETURN_FALSE; + } + + } + efree(callback_name); + + collation = (php_sqlite3_collation *)ecalloc(1, sizeof(*collation)); + if (sqlite3_create_collation(db_obj->db, collation_name, SQLITE_UTF8, collation, php_sqlite3_callback_compare) == SQLITE_OK) { + if (sqlite3_create_collation(db_obj->db, collation_name, SQLITE_UTF8, ud_cmp_func, php_sqlite3_callback_compare) == SQLITE_OK) { + collation->collation_name = estrdup(collation_name); + + MAKE_STD_ZVAL(collation->cmp_func); + MAKE_COPY_ZVAL(&callback_func, collation->cmp_func); + + RETURN_FALSE; +} +/* }}} */ + + typedef struct { sqlite3_blob *blob; size_t position; @@ -1746,6 +1847,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_c @@ -1749,6 +1856,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_c ZEND_ARG_INFO(0, argument_count) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createcollation, 0, 0, 2) + ZEND_BEGIN_ARG_INFO_EX(argingo_sqlite3_openblob, 0, 0, 3) ZEND_ARG_INFO(0, table) ZEND_ARG_INFO(0, column) @@ -1809,6 +1915,7 @@ static zend_function_entry php_sqlite3_c @@ -1812,6 +1924,7 @@ static zend_function_entry php_sqlite3_c PHP_ME(sqlite3, querySingle, arginfo_sqlite3_querysingle, ZEND_ACC_PUBLIC) PHP_ME(sqlite3, createFunction, arginfo_sqlite3_createfunction, ZEND_ACC_PUBLIC) PHP_ME(sqlite3, createAggregate, arginfo_sqlite3_createaggregate, ZEND_ACC_PUBLIC) + PHP_ME(sqlite3, createCollation, arginfo_sqlite3_createcollation, ZEND_ACC_PUBLIC) PHP_ME(sqlite3, openBlob, argingo_sqlite3_openblob, ZEND_ACC_PUBLIC) PHP_ME(sqlite3, enableExceptions, argingo_sqlite3_enableexceptions, ZEND_ACC_PUBLIC) /* Aliases */ @@ -1905,6 +2012,7 @@ static void php_sqlite3_object_free_stor @@ -1908,6 +2021,7 @@ static void php_sqlite3_object_free_stor { php_sqlite3_db_object *intern = (php_sqlite3_db_object *)object; php_sqlite3_func *func; + php_sqlite3_collation *collation; if (!intern) { return; @@ -1931,6 +2039,19 @@ static void php_sqlite3_object_free_stor @@ -1934,6 +2048,19 @@ static void php_sqlite3_object_free_stor efree(func); } + while (intern->collations){ + if (intern->initialised && intern->db){ + sqlite3_create_collation(intern->db, collation->collation_name, SQLITE_UTF8, NULL, NULL); + } + efree((char*)collation->collation_name); + if (collation->cmp_func){ + if (cmp_func->func){ + zval_ptr_dtor(&collation->cmp_func); + } + efree(collation); + } sqlite3_close(intern->db); intern->initialised = 0; diff -purN ext/sqlite3/tests/sqlite3_36_create_collation.phpt ext/sqlite3/tests/sqlite3_36_create_collation.phpt --- ext/sqlite3/tests/sqlite3_36_create_collation.phpt 1969-12-31 20:00:00.000000000 -0400 +++ ext/sqlite3/tests/sqlite3_36_create_collation.phpt 2012-01-28 22:26:19.260068002 -0400 +++ ext/sqlite3/tests/sqlite3_36_create_collation.phpt 2012-01-24 11:26:35.183521376 -0400 @@ -0,0 +1,44 @@ +--TEST-- +Test SQLite3::createCollation() by adding strnatcmp() as an SQL COLLATE sequence +--SKIPIF-- |
Copyright © 2001-2024 The PHP Group All rights reserved. |
Last updated: Thu Mar 28 14:01:29 2024 UTC |