1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-30 19:03:16 +03:00

Add an optional 5th parameter to the next_char() function that is the

collating sequence to use for comparison.

FossilOrigin-Name: 9415db6ef255d27ca8473c17e65749a197c30455
This commit is contained in:
drh
2013-07-08 01:27:43 +00:00
parent 3492f4f9af
commit d4b473b2cf
4 changed files with 79 additions and 30 deletions

View File

@ -10,12 +10,22 @@
**
******************************************************************************
**
** This file contains code to implement the next_char(A,T,F,W) SQL function.
** This file contains code to implement the next_char(A,T,F,W,C) SQL function.
**
** The next_char(A,T,F,W) function finds all valid "next" characters for
** string A given the vocabulary in T.F. The T.F field should be indexed.
** If the W value exists and is a non-empty string, then it is an SQL
** expression that limits the entries in T.F that will be considered.
** The next_char(A,T,F,W,C) function finds all valid "next" characters for
** string A given the vocabulary in T.F. If the W value exists and is a
** non-empty string, then it is an SQL expression that limits the entries
** in T.F that will be considered. If C exists and is a non-empty string,
** then it is the name of the collating sequence to use for comparison. If
**
** Only the first three arguments are required. If the C parameter is
** omitted or is NULL or is an empty string, then the default collating
** sequence of T.F is used for comparision. If the W parameter is omitted
** or is NULL or is an empty string, then no filtering of the output is
** done.
**
** The T.F column should be indexed using collation C or else this routine
** will be quite slow.
**
** For example, suppose an application has a dictionary like this:
**
@ -184,6 +194,9 @@ static void nextCharFunc(
const unsigned char *zTable = sqlite3_value_text(argv[1]);
const unsigned char *zField = sqlite3_value_text(argv[2]);
const unsigned char *zWhere;
const unsigned char *zCollName;
char *zWhereClause = 0;
char *zColl = 0;
char *zSql;
int rc;
@ -192,25 +205,41 @@ static void nextCharFunc(
c.zPrefix = sqlite3_value_text(argv[0]);
c.nPrefix = sqlite3_value_bytes(argv[0]);
if( zTable==0 || zField==0 || c.zPrefix==0 ) return;
if( argc<4
|| (zWhere = sqlite3_value_text(argv[3]))==0
|| zWhere[0]==0
if( argc>=4
&& (zWhere = sqlite3_value_text(argv[3]))!=0
&& zWhere[0]!=0
){
zSql = sqlite3_mprintf(
"SELECT \"%w\" FROM \"%w\""
" WHERE \"%w\">=(?1 || ?2)"
" AND \"%w\"<=(?1 || char(1114111))" /* 1114111 == 0x10ffff */
" ORDER BY 1 ASC LIMIT 1",
zField, zTable, zField, zField);
zWhereClause = sqlite3_mprintf("AND (%s)", zWhere);
if( zWhereClause==0 ){
sqlite3_result_error_nomem(context);
return;
}
}else{
zSql = sqlite3_mprintf(
"SELECT \"%w\" FROM \"%w\""
" WHERE \"%w\">=(?1 || ?2)"
" AND \"%w\"<=(?1 || char(1114111))" /* 1114111 == 0x10ffff */
" AND (%s)"
" ORDER BY 1 ASC LIMIT 1",
zField, zTable, zField, zField, zWhere);
zWhereClause = "";
}
if( argc>=5
&& (zCollName = sqlite3_value_text(argv[4]))!=0
&& zCollName[0]!=0
){
zColl = sqlite3_mprintf("collate \"%w\"", zCollName);
if( zColl==0 ){
sqlite3_result_error_nomem(context);
if( zWhereClause[0] ) sqlite3_free(zWhereClause);
return;
}
}else{
zColl = "";
}
zSql = sqlite3_mprintf(
"SELECT \"%w\" FROM \"%w\""
" WHERE \"%w\">=(?1 || ?2) %s"
" AND \"%w\"<=(?1 || char(1114111)) %s" /* 1114111 == 0x10ffff */
" %s"
" ORDER BY 1 %s ASC LIMIT 1",
zField, zTable, zField, zColl, zField, zColl, zWhereClause, zColl
);
if( zWhereClause[0] ) sqlite3_free(zWhereClause);
if( zColl[0] ) sqlite3_free(zColl);
if( zSql==0 ){
sqlite3_result_error_nomem(context);
return;
@ -261,5 +290,9 @@ int sqlite3_nextchar_init(
rc = sqlite3_create_function(db, "next_char", 4, SQLITE_UTF8, 0,
nextCharFunc, 0, 0);
}
if( rc==SQLITE_OK ){
rc = sqlite3_create_function(db, "next_char", 5, SQLITE_UTF8, 0,
nextCharFunc, 0, 0);
}
return rc;
}

View File

@ -1,5 +1,5 @@
C Fixes\sfor\stest\scases\srunning\sin\sthe\s"mmap"\spermutation.
D 2013-07-06T18:07:57.900
C Add\san\soptional\s5th\sparameter\sto\sthe\snext_char()\sfunction\sthat\sis\sthe\ncollating\ssequence\sto\suse\sfor\scomparison.
D 2013-07-08T01:27:43.173
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 5e41da95d92656a5004b03d3576e8b226858a28e
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -110,7 +110,7 @@ F ext/misc/amatch.c eae8454cd9dcb287b2a3ec2e65a865a4ac5f0d06
F ext/misc/closure.c 997c20ddf35f85ab399f4a02a557a9baa822ec32
F ext/misc/fuzzer.c 136533c53cfce0957f0b48fa11dba27e21c5c01d
F ext/misc/ieee754.c b0362167289170627659e84173f5d2e8fee8566e
F ext/misc/nextchar.c 51c0abfa91009f736eb55d00452770df0ea09c3b
F ext/misc/nextchar.c 80ba262d23238efcfcb3d72d71aa4513098e26a6
F ext/misc/percentile.c bcbee3c061b884eccb80e21651daaae8e1e43c63
F ext/misc/regexp.c c25c65fe775f5d9801fb8573e36ebe73f2c0c2e0
F ext/misc/rot13.c 1ac6f95f99b575907b9b09c81a349114cf9be45a
@ -793,7 +793,7 @@ F test/speed3.test d32043614c08c53eafdc80f33191d5bd9b920523
F test/speed4.test abc0ad3399dcf9703abed2fff8705e4f8e416715
F test/speed4p.explain 6b5f104ebeb34a038b2f714150f51d01143e59aa
F test/speed4p.test 0e51908951677de5a969b723e03a27a1c45db38b
F test/spellfix.test bea537caf587df30d430c2c6a8fe9f64b8712834
F test/spellfix.test 38246facf7d9d7eeb8a57d7497cf7ce73ce5785d
F test/sqllimits1.test b1aae27cc98eceb845e7f7adf918561256e31298
F test/stat.test be8d477306006ec696bc86757cfb34bec79447ce
F test/stmt.test 25d64e3dbf9a3ce89558667d7f39d966fe2a71b9
@ -1101,7 +1101,7 @@ F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
F tool/wherecosttest.c f407dc4c79786982a475261866a161cd007947ae
F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
P 0ae7e75b215b0d75920769da9146c54ce2ad3ce0
R ca6d3b61d7033e8bd8885f323788dfc0
U dan
Z 8857197c0df65b9e0d74b83df35e71c7
P cdb97d41abf4a3b8e22fa8ca9f3aab4a3f968d27
R cddb399d89cded656ee0c42122eb620d
U drh
Z 856ce6a2b43d394812cb00c43d8e6345

View File

@ -1 +1 @@
cdb97d41abf4a3b8e22fa8ca9f3aab4a3f968d27
9415db6ef255d27ca8473c17e65749a197c30455

View File

@ -105,6 +105,22 @@ do_test 1.14 {
catchsql {SELECT next_char('','xyzzy','a')}
} {1 {no such table: xyzzy}}
do_execsql_test 1.20 {
CREATE TABLE vocab2(w TEXT);
CREATE INDEX vocab2w ON vocab2(w COLLATE nocase);
INSERT INTO vocab2 VALUES('abc'), ('ABD'), ('aBe'), ('AbF');
SELECT next_char('ab', 'vocab2', 'w', null, 'nocase');
} {cDeF}
do_execsql_test 1.21 {
SELECT next_char('ab','vocab2','w',null,null);
} {c}
do_execsql_test 1.22 {
SELECT next_char('AB','vocab2','w',null,'NOCASE');
} {cDeF}
do_execsql_test 1.23 {
SELECT next_char('ab','vocab2','w',null,'binary');
} {c}
do_execsql_test 2.1 {
CREATE VIRTUAL TABLE t2 USING spellfix1;
INSERT INTO t2 (word, soundslike) VALUES('school', 'skuul');