Fossil SCM

Catch up w/ trunk & document --case-sensitive option in the add and addremove commands.

martin.weber 2012-04-19 14:34 msw-docco merge
Commit 626a317e5cea1ff0819994ad2c2a6fdd869bb0c1
+15 -8
--- src/add.c
+++ src/add.c
@@ -196,15 +196,20 @@
196196
** The --ignore option is a comma-separate list of glob patterns for files
197197
** to be excluded. Example: '*.o,*.obj,*.exe' If the --ignore option
198198
** does not appear on the command line then the "ignore-glob" setting is
199199
** used.
200200
**
201
+** The --case-sensitive option determines whether or not filenames should
202
+** be treated case sensitive or not. If the option is not given, the default
203
+** depends on the global setting, or the operating system default, if not set.
204
+**
201205
** Options:
202206
**
203
-** --dotfiles include files beginning with a dot (".")
204
-** --ignore <CSG> ignore files matching patterns from the
205
-** comma separated list of glob patterns.
207
+** --case-sensitive <BOOL> override case-sensitive setting
208
+** --dotfiles include files beginning with a dot (".")
209
+** --ignore <CSG> ignore files matching patterns from the
210
+** comma separated list of glob patterns.
206211
**
207212
** See also: addremove, rm
208213
*/
209214
void add_cmd(void){
210215
int i; /* Loop counter */
@@ -408,22 +413,24 @@
408413
** as a separate step.
409414
**
410415
** Files and directories whose names begin with "." are ignored unless
411416
** the --dotfiles option is used.
412417
**
413
-** The --ignore option overrides the "ignore-glob" setting. See
418
+** The --ignore option overrides the "ignore-glob" setting, as does the
419
+** --case-sensitive option with the "case-sensitive" setting. See the
414420
** documentation on the "settings" command for further information.
415421
**
416422
** The --test option shows what would happen without actually doing anything.
417423
**
418424
** This command can be used to track third party software.
419425
**
420426
** Options:
421
-** --dotfiles include files beginning with a dot (".")
422
-** --ignore <CSG> ignore files matching patterns from the
423
-** comma separated list of glob patterns.
424
-** --test If given, show what would be done without doing so.
427
+** --case-sensitive <BOOL> override case-sensitive setting
428
+** --dotfiles include files beginning with a dot (".")
429
+** --ignore <CSG> ignore files matching patterns from the
430
+** comma separated list of glob patterns.
431
+** --test If given, display instead of run actions
425432
**
426433
** See also: add, rm
427434
*/
428435
void addremove_cmd(void){
429436
Blob path;
430437
--- src/add.c
+++ src/add.c
@@ -196,15 +196,20 @@
196 ** The --ignore option is a comma-separate list of glob patterns for files
197 ** to be excluded. Example: '*.o,*.obj,*.exe' If the --ignore option
198 ** does not appear on the command line then the "ignore-glob" setting is
199 ** used.
200 **
 
 
 
 
201 ** Options:
202 **
203 ** --dotfiles include files beginning with a dot (".")
204 ** --ignore <CSG> ignore files matching patterns from the
205 ** comma separated list of glob patterns.
 
206 **
207 ** See also: addremove, rm
208 */
209 void add_cmd(void){
210 int i; /* Loop counter */
@@ -408,22 +413,24 @@
408 ** as a separate step.
409 **
410 ** Files and directories whose names begin with "." are ignored unless
411 ** the --dotfiles option is used.
412 **
413 ** The --ignore option overrides the "ignore-glob" setting. See
 
414 ** documentation on the "settings" command for further information.
415 **
416 ** The --test option shows what would happen without actually doing anything.
417 **
418 ** This command can be used to track third party software.
419 **
420 ** Options:
421 ** --dotfiles include files beginning with a dot (".")
422 ** --ignore <CSG> ignore files matching patterns from the
423 ** comma separated list of glob patterns.
424 ** --test If given, show what would be done without doing so.
 
425 **
426 ** See also: add, rm
427 */
428 void addremove_cmd(void){
429 Blob path;
430
--- src/add.c
+++ src/add.c
@@ -196,15 +196,20 @@
196 ** The --ignore option is a comma-separate list of glob patterns for files
197 ** to be excluded. Example: '*.o,*.obj,*.exe' If the --ignore option
198 ** does not appear on the command line then the "ignore-glob" setting is
199 ** used.
200 **
201 ** The --case-sensitive option determines whether or not filenames should
202 ** be treated case sensitive or not. If the option is not given, the default
203 ** depends on the global setting, or the operating system default, if not set.
204 **
205 ** Options:
206 **
207 ** --case-sensitive <BOOL> override case-sensitive setting
208 ** --dotfiles include files beginning with a dot (".")
209 ** --ignore <CSG> ignore files matching patterns from the
210 ** comma separated list of glob patterns.
211 **
212 ** See also: addremove, rm
213 */
214 void add_cmd(void){
215 int i; /* Loop counter */
@@ -408,22 +413,24 @@
413 ** as a separate step.
414 **
415 ** Files and directories whose names begin with "." are ignored unless
416 ** the --dotfiles option is used.
417 **
418 ** The --ignore option overrides the "ignore-glob" setting, as does the
419 ** --case-sensitive option with the "case-sensitive" setting. See the
420 ** documentation on the "settings" command for further information.
421 **
422 ** The --test option shows what would happen without actually doing anything.
423 **
424 ** This command can be used to track third party software.
425 **
426 ** Options:
427 ** --case-sensitive <BOOL> override case-sensitive setting
428 ** --dotfiles include files beginning with a dot (".")
429 ** --ignore <CSG> ignore files matching patterns from the
430 ** comma separated list of glob patterns.
431 ** --test If given, display instead of run actions
432 **
433 ** See also: add, rm
434 */
435 void addremove_cmd(void){
436 Blob path;
437
+15 -8
--- src/add.c
+++ src/add.c
@@ -196,15 +196,20 @@
196196
** The --ignore option is a comma-separate list of glob patterns for files
197197
** to be excluded. Example: '*.o,*.obj,*.exe' If the --ignore option
198198
** does not appear on the command line then the "ignore-glob" setting is
199199
** used.
200200
**
201
+** The --case-sensitive option determines whether or not filenames should
202
+** be treated case sensitive or not. If the option is not given, the default
203
+** depends on the global setting, or the operating system default, if not set.
204
+**
201205
** Options:
202206
**
203
-** --dotfiles include files beginning with a dot (".")
204
-** --ignore <CSG> ignore files matching patterns from the
205
-** comma separated list of glob patterns.
207
+** --case-sensitive <BOOL> override case-sensitive setting
208
+** --dotfiles include files beginning with a dot (".")
209
+** --ignore <CSG> ignore files matching patterns from the
210
+** comma separated list of glob patterns.
206211
**
207212
** See also: addremove, rm
208213
*/
209214
void add_cmd(void){
210215
int i; /* Loop counter */
@@ -408,22 +413,24 @@
408413
** as a separate step.
409414
**
410415
** Files and directories whose names begin with "." are ignored unless
411416
** the --dotfiles option is used.
412417
**
413
-** The --ignore option overrides the "ignore-glob" setting. See
418
+** The --ignore option overrides the "ignore-glob" setting, as does the
419
+** --case-sensitive option with the "case-sensitive" setting. See the
414420
** documentation on the "settings" command for further information.
415421
**
416422
** The --test option shows what would happen without actually doing anything.
417423
**
418424
** This command can be used to track third party software.
419425
**
420426
** Options:
421
-** --dotfiles include files beginning with a dot (".")
422
-** --ignore <CSG> ignore files matching patterns from the
423
-** comma separated list of glob patterns.
424
-** --test If given, show what would be done without doing so.
427
+** --case-sensitive <BOOL> override case-sensitive setting
428
+** --dotfiles include files beginning with a dot (".")
429
+** --ignore <CSG> ignore files matching patterns from the
430
+** comma separated list of glob patterns.
431
+** --test If given, display instead of run actions
425432
**
426433
** See also: add, rm
427434
*/
428435
void addremove_cmd(void){
429436
Blob path;
430437
--- src/add.c
+++ src/add.c
@@ -196,15 +196,20 @@
196 ** The --ignore option is a comma-separate list of glob patterns for files
197 ** to be excluded. Example: '*.o,*.obj,*.exe' If the --ignore option
198 ** does not appear on the command line then the "ignore-glob" setting is
199 ** used.
200 **
 
 
 
 
201 ** Options:
202 **
203 ** --dotfiles include files beginning with a dot (".")
204 ** --ignore <CSG> ignore files matching patterns from the
205 ** comma separated list of glob patterns.
 
206 **
207 ** See also: addremove, rm
208 */
209 void add_cmd(void){
210 int i; /* Loop counter */
@@ -408,22 +413,24 @@
408 ** as a separate step.
409 **
410 ** Files and directories whose names begin with "." are ignored unless
411 ** the --dotfiles option is used.
412 **
413 ** The --ignore option overrides the "ignore-glob" setting. See
 
414 ** documentation on the "settings" command for further information.
415 **
416 ** The --test option shows what would happen without actually doing anything.
417 **
418 ** This command can be used to track third party software.
419 **
420 ** Options:
421 ** --dotfiles include files beginning with a dot (".")
422 ** --ignore <CSG> ignore files matching patterns from the
423 ** comma separated list of glob patterns.
424 ** --test If given, show what would be done without doing so.
 
425 **
426 ** See also: add, rm
427 */
428 void addremove_cmd(void){
429 Blob path;
430
--- src/add.c
+++ src/add.c
@@ -196,15 +196,20 @@
196 ** The --ignore option is a comma-separate list of glob patterns for files
197 ** to be excluded. Example: '*.o,*.obj,*.exe' If the --ignore option
198 ** does not appear on the command line then the "ignore-glob" setting is
199 ** used.
200 **
201 ** The --case-sensitive option determines whether or not filenames should
202 ** be treated case sensitive or not. If the option is not given, the default
203 ** depends on the global setting, or the operating system default, if not set.
204 **
205 ** Options:
206 **
207 ** --case-sensitive <BOOL> override case-sensitive setting
208 ** --dotfiles include files beginning with a dot (".")
209 ** --ignore <CSG> ignore files matching patterns from the
210 ** comma separated list of glob patterns.
211 **
212 ** See also: addremove, rm
213 */
214 void add_cmd(void){
215 int i; /* Loop counter */
@@ -408,22 +413,24 @@
413 ** as a separate step.
414 **
415 ** Files and directories whose names begin with "." are ignored unless
416 ** the --dotfiles option is used.
417 **
418 ** The --ignore option overrides the "ignore-glob" setting, as does the
419 ** --case-sensitive option with the "case-sensitive" setting. See the
420 ** documentation on the "settings" command for further information.
421 **
422 ** The --test option shows what would happen without actually doing anything.
423 **
424 ** This command can be used to track third party software.
425 **
426 ** Options:
427 ** --case-sensitive <BOOL> override case-sensitive setting
428 ** --dotfiles include files beginning with a dot (".")
429 ** --ignore <CSG> ignore files matching patterns from the
430 ** comma separated list of glob patterns.
431 ** --test If given, display instead of run actions
432 **
433 ** See also: add, rm
434 */
435 void addremove_cmd(void){
436 Blob path;
437
+11 -1
--- src/attach.c
+++ src/attach.c
@@ -238,22 +238,32 @@
238238
char *zUUID;
239239
const char *zComment;
240240
char *zDate;
241241
int rid;
242242
int i, n;
243
+ int addCompress = 0;
244
+ Manifest *pManifest;
243245
244246
db_begin_transaction();
245247
blob_init(&content, aContent, szContent);
248
+ pManifest = manifest_parse(&content, 0);
249
+ manifest_destroy(pManifest);
250
+ blob_init(&content, aContent, szContent);
251
+ if( pManifest ){
252
+ blob_compress(&content, &content);
253
+ addCompress = 1;
254
+ }
246255
rid = content_put(&content);
247256
zUUID = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
248257
blob_zero(&manifest);
249258
for(i=n=0; zName[i]; i++){
250259
if( zName[i]=='/' || zName[i]=='\\' ) n = i;
251260
}
252261
zName += n;
253262
if( zName[0]==0 ) zName = "unknown";
254
- blob_appendf(&manifest, "A %F %F %s\n", zName, zTarget, zUUID);
263
+ blob_appendf(&manifest, "A %F%s %F %s\n",
264
+ zName, addCompress ? ".gz" : "", zTarget, zUUID);
255265
zComment = PD("comment", "");
256266
while( fossil_isspace(zComment[0]) ) zComment++;
257267
n = strlen(zComment);
258268
while( n>0 && fossil_isspace(zComment[n-1]) ){ n--; }
259269
if( n>0 ){
260270
--- src/attach.c
+++ src/attach.c
@@ -238,22 +238,32 @@
238 char *zUUID;
239 const char *zComment;
240 char *zDate;
241 int rid;
242 int i, n;
 
 
243
244 db_begin_transaction();
245 blob_init(&content, aContent, szContent);
 
 
 
 
 
 
 
246 rid = content_put(&content);
247 zUUID = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
248 blob_zero(&manifest);
249 for(i=n=0; zName[i]; i++){
250 if( zName[i]=='/' || zName[i]=='\\' ) n = i;
251 }
252 zName += n;
253 if( zName[0]==0 ) zName = "unknown";
254 blob_appendf(&manifest, "A %F %F %s\n", zName, zTarget, zUUID);
 
255 zComment = PD("comment", "");
256 while( fossil_isspace(zComment[0]) ) zComment++;
257 n = strlen(zComment);
258 while( n>0 && fossil_isspace(zComment[n-1]) ){ n--; }
259 if( n>0 ){
260
--- src/attach.c
+++ src/attach.c
@@ -238,22 +238,32 @@
238 char *zUUID;
239 const char *zComment;
240 char *zDate;
241 int rid;
242 int i, n;
243 int addCompress = 0;
244 Manifest *pManifest;
245
246 db_begin_transaction();
247 blob_init(&content, aContent, szContent);
248 pManifest = manifest_parse(&content, 0);
249 manifest_destroy(pManifest);
250 blob_init(&content, aContent, szContent);
251 if( pManifest ){
252 blob_compress(&content, &content);
253 addCompress = 1;
254 }
255 rid = content_put(&content);
256 zUUID = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
257 blob_zero(&manifest);
258 for(i=n=0; zName[i]; i++){
259 if( zName[i]=='/' || zName[i]=='\\' ) n = i;
260 }
261 zName += n;
262 if( zName[0]==0 ) zName = "unknown";
263 blob_appendf(&manifest, "A %F%s %F %s\n",
264 zName, addCompress ? ".gz" : "", zTarget, zUUID);
265 zComment = PD("comment", "");
266 while( fossil_isspace(zComment[0]) ) zComment++;
267 n = strlen(zComment);
268 while( n>0 && fossil_isspace(zComment[n-1]) ){ n--; }
269 if( n>0 ){
270
+1 -1
--- src/login.c
+++ src/login.c
@@ -385,11 +385,11 @@
385385
** is a manually operated browser or a bot. When in doubt, assume a bot.
386386
** Return true if we believe the agent is a real person.
387387
*/
388388
static int isHuman(const char *zAgent){
389389
int i;
390
- if( zAgent==0 ) return 0; /* If not UserAgent, the probably a bot */
390
+ if( zAgent==0 ) return 0; /* If not UserAgent, then probably a bot */
391391
for(i=0; zAgent[i]; i++){
392392
if( prefix_match("bot", zAgent+i) ) return 0;
393393
if( prefix_match("spider", zAgent+i) ) return 0;
394394
if( prefix_match("crawl", zAgent+i) ) return 0;
395395
/* If a URI appears in the User-Agent, it is probably a bot */
396396
--- src/login.c
+++ src/login.c
@@ -385,11 +385,11 @@
385 ** is a manually operated browser or a bot. When in doubt, assume a bot.
386 ** Return true if we believe the agent is a real person.
387 */
388 static int isHuman(const char *zAgent){
389 int i;
390 if( zAgent==0 ) return 0; /* If not UserAgent, the probably a bot */
391 for(i=0; zAgent[i]; i++){
392 if( prefix_match("bot", zAgent+i) ) return 0;
393 if( prefix_match("spider", zAgent+i) ) return 0;
394 if( prefix_match("crawl", zAgent+i) ) return 0;
395 /* If a URI appears in the User-Agent, it is probably a bot */
396
--- src/login.c
+++ src/login.c
@@ -385,11 +385,11 @@
385 ** is a manually operated browser or a bot. When in doubt, assume a bot.
386 ** Return true if we believe the agent is a real person.
387 */
388 static int isHuman(const char *zAgent){
389 int i;
390 if( zAgent==0 ) return 0; /* If not UserAgent, then probably a bot */
391 for(i=0; zAgent[i]; i++){
392 if( prefix_match("bot", zAgent+i) ) return 0;
393 if( prefix_match("spider", zAgent+i) ) return 0;
394 if( prefix_match("crawl", zAgent+i) ) return 0;
395 /* If a URI appears in the User-Agent, it is probably a bot */
396
+4 -2
--- src/manifest.c
+++ src/manifest.c
@@ -334,11 +334,11 @@
334334
** Each card is divided into tokens by a single space character.
335335
** The first token is a single upper-case letter which is the card type.
336336
** The card type determines the other parameters to the card.
337337
** Cards must occur in lexicographical order.
338338
*/
339
-static Manifest *manifest_parse(Blob *pContent, int rid){
339
+Manifest *manifest_parse(Blob *pContent, int rid){
340340
Manifest *p;
341341
int seenZ = 0;
342342
int i, lineNo=0;
343343
ManifestText x;
344344
char cPrevType = 0;
@@ -348,11 +348,13 @@
348348
char *zUuid;
349349
int sz = 0;
350350
int isRepeat;
351351
static Bag seen;
352352
353
- if( bag_find(&seen, rid) ){
353
+ if( rid==0 ){
354
+ isRepeat = 1;
355
+ }else if( bag_find(&seen, rid) ){
354356
isRepeat = 1;
355357
}else{
356358
isRepeat = 0;
357359
bag_insert(&seen, rid);
358360
}
359361
--- src/manifest.c
+++ src/manifest.c
@@ -334,11 +334,11 @@
334 ** Each card is divided into tokens by a single space character.
335 ** The first token is a single upper-case letter which is the card type.
336 ** The card type determines the other parameters to the card.
337 ** Cards must occur in lexicographical order.
338 */
339 static Manifest *manifest_parse(Blob *pContent, int rid){
340 Manifest *p;
341 int seenZ = 0;
342 int i, lineNo=0;
343 ManifestText x;
344 char cPrevType = 0;
@@ -348,11 +348,13 @@
348 char *zUuid;
349 int sz = 0;
350 int isRepeat;
351 static Bag seen;
352
353 if( bag_find(&seen, rid) ){
 
 
354 isRepeat = 1;
355 }else{
356 isRepeat = 0;
357 bag_insert(&seen, rid);
358 }
359
--- src/manifest.c
+++ src/manifest.c
@@ -334,11 +334,11 @@
334 ** Each card is divided into tokens by a single space character.
335 ** The first token is a single upper-case letter which is the card type.
336 ** The card type determines the other parameters to the card.
337 ** Cards must occur in lexicographical order.
338 */
339 Manifest *manifest_parse(Blob *pContent, int rid){
340 Manifest *p;
341 int seenZ = 0;
342 int i, lineNo=0;
343 ManifestText x;
344 char cPrevType = 0;
@@ -348,11 +348,13 @@
348 char *zUuid;
349 int sz = 0;
350 int isRepeat;
351 static Bag seen;
352
353 if( rid==0 ){
354 isRepeat = 1;
355 }else if( bag_find(&seen, rid) ){
356 isRepeat = 1;
357 }else{
358 isRepeat = 0;
359 bag_insert(&seen, rid);
360 }
361
+91 -49
--- src/shell.c
+++ src/shell.c
@@ -419,10 +419,11 @@
419419
sqlite3 *db; /* The database */
420420
int echoOn; /* True to echo input commands */
421421
int statsOn; /* True to display memory stats before each finalize */
422422
int cnt; /* Number of records displayed so far */
423423
FILE *out; /* Write results here */
424
+ FILE *traceOut; /* Output for sqlite3_trace() */
424425
int nErr; /* Number of errors seen */
425426
int mode; /* An output mode setting */
426427
int writableSchema; /* True if PRAGMA writable_schema=ON */
427428
int showHeader; /* True to show column names in List or Column mode */
428429
char *zDestTable; /* Name of destination table when MODE_Insert */
@@ -1307,10 +1308,11 @@
13071308
/* Always quote the table name, even if it appears to be pure ascii,
13081309
** in case it is a keyword. Ex: INSERT INTO "table" ... */
13091310
zTmp = appendText(zTmp, zTable, '"');
13101311
if( zTmp ){
13111312
zSelect = appendText(zSelect, zTmp, '\'');
1313
+ free(zTmp);
13121314
}
13131315
zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
13141316
rc = sqlite3_step(pTableInfo);
13151317
while( rc==SQLITE_ROW ){
13161318
const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
@@ -1335,11 +1337,11 @@
13351337
rc = run_table_dump_query(p, zSelect, zPrepStmt);
13361338
if( rc==SQLITE_CORRUPT ){
13371339
zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
13381340
run_table_dump_query(p, zSelect, 0);
13391341
}
1340
- if( zSelect ) free(zSelect);
1342
+ free(zSelect);
13411343
}
13421344
return 0;
13431345
}
13441346
13451347
/*
@@ -1365,11 +1367,11 @@
13651367
sqlite3_free(zErr);
13661368
zErr = 0;
13671369
}
13681370
zQ2 = malloc( len+100 );
13691371
if( zQ2==0 ) return rc;
1370
- sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery);
1372
+ sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
13711373
rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
13721374
if( rc ){
13731375
fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
13741376
}else{
13751377
rc = SQLITE_CORRUPT;
@@ -1431,10 +1433,11 @@
14311433
".stats ON|OFF Turn stats on or off\n"
14321434
".tables ?TABLE? List names of tables\n"
14331435
" If TABLE specified, only list tables matching\n"
14341436
" LIKE pattern TABLE.\n"
14351437
".timeout MS Try opening locked tables for MS milliseconds\n"
1438
+ ".trace FILE|off Output each SQL statement as it is run\n"
14361439
".vfsname ?AUX? Print the name of the VFS stack\n"
14371440
".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
14381441
;
14391442
14401443
static char zTimerHelp[] =
@@ -1519,10 +1522,56 @@
15191522
}else if( strcmp(zArg,"yes")==0 ){
15201523
val = 1;
15211524
}
15221525
return val;
15231526
}
1527
+
1528
+/*
1529
+** Close an output file, assuming it is not stderr or stdout
1530
+*/
1531
+static void output_file_close(FILE *f){
1532
+ if( f && f!=stdout && f!=stderr ) fclose(f);
1533
+}
1534
+
1535
+/*
1536
+** Try to open an output file. The names "stdout" and "stderr" are
1537
+** recognized and do the right thing. NULL is returned if the output
1538
+** filename is "off".
1539
+*/
1540
+static FILE *output_file_open(const char *zFile){
1541
+ FILE *f;
1542
+ if( strcmp(zFile,"stdout")==0 ){
1543
+ f = stdout;
1544
+ }else if( strcmp(zFile, "stderr")==0 ){
1545
+ f = stderr;
1546
+ }else if( strcmp(zFile, "off")==0 ){
1547
+ f = 0;
1548
+ }else{
1549
+ f = fopen(zFile, "wb");
1550
+ if( f==0 ){
1551
+ fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
1552
+ }
1553
+ }
1554
+ return f;
1555
+}
1556
+
1557
+/*
1558
+** A routine for handling output from sqlite3_trace().
1559
+*/
1560
+static void sql_trace_callback(void *pArg, const char *z){
1561
+ FILE *f = (FILE*)pArg;
1562
+ if( f ) fprintf(f, "%s\n", z);
1563
+}
1564
+
1565
+/*
1566
+** A no-op routine that runs with the ".breakpoint" doc-command. This is
1567
+** a useful spot to set a debugger breakpoint.
1568
+*/
1569
+static void test_breakpoint(void){
1570
+ static int nCall = 0;
1571
+ nCall++;
1572
+}
15241573
15251574
/*
15261575
** If an input line begins with "." then invoke this routine to
15271576
** process that line.
15281577
**
@@ -1598,10 +1647,17 @@
15981647
}else
15991648
16001649
if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 && nArg>1 && nArg<3 ){
16011650
bail_on_error = booleanValue(azArg[1]);
16021651
}else
1652
+
1653
+ /* The undocumented ".breakpoint" command causes a call to the no-op
1654
+ ** routine named test_breakpoint().
1655
+ */
1656
+ if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
1657
+ test_breakpoint();
1658
+ }else
16031659
16041660
if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 && nArg==1 ){
16051661
struct callback_data data;
16061662
char *zErrMsg = 0;
16071663
open_db(p);
@@ -1930,26 +1986,12 @@
19301986
}else
19311987
#endif
19321988
19331989
if( c=='l' && strncmp(azArg[0], "log", n)==0 && nArg>=2 ){
19341990
const char *zFile = azArg[1];
1935
- if( p->pLog && p->pLog!=stdout && p->pLog!=stderr ){
1936
- fclose(p->pLog);
1937
- p->pLog = 0;
1938
- }
1939
- if( strcmp(zFile,"stdout")==0 ){
1940
- p->pLog = stdout;
1941
- }else if( strcmp(zFile, "stderr")==0 ){
1942
- p->pLog = stderr;
1943
- }else if( strcmp(zFile, "off")==0 ){
1944
- p->pLog = 0;
1945
- }else{
1946
- p->pLog = fopen(zFile, "w");
1947
- if( p->pLog==0 ){
1948
- fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
1949
- }
1950
- }
1991
+ output_file_close(p->pLog);
1992
+ p->pLog = output_file_open(zFile);
19511993
}else
19521994
19531995
if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==2 ){
19541996
int n2 = strlen30(azArg[1]);
19551997
if( (n2==4 && strncmp(azArg[1],"line",n2)==0)
@@ -1998,37 +2040,35 @@
19982040
sqlite3_snprintf(sizeof(p->nullvalue), p->nullvalue,
19992041
"%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
20002042
}else
20012043
20022044
if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
2003
- if( p->out!=stdout ){
2004
- if( p->outfile[0]=='|' ){
2005
- pclose(p->out);
2006
- }else{
2007
- fclose(p->out);
2008
- }
2009
- }
2010
- if( strcmp(azArg[1],"stdout")==0 ){
2011
- p->out = stdout;
2012
- sqlite3_snprintf(sizeof(p->outfile), p->outfile, "stdout");
2013
- }else if( azArg[1][0]=='|' ){
2045
+ if( p->outfile[0]=='|' ){
2046
+ pclose(p->out);
2047
+ }else{
2048
+ output_file_close(p->out);
2049
+ }
2050
+ p->outfile[0] = 0;
2051
+ if( azArg[1][0]=='|' ){
20142052
p->out = popen(&azArg[1][1], "w");
20152053
if( p->out==0 ){
20162054
fprintf(stderr,"Error: cannot open pipe \"%s\"\n", &azArg[1][1]);
20172055
p->out = stdout;
20182056
rc = 1;
20192057
}else{
20202058
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
20212059
}
20222060
}else{
2023
- p->out = fopen(azArg[1], "wb");
2061
+ p->out = output_file_open(azArg[1]);
20242062
if( p->out==0 ){
2025
- fprintf(stderr,"Error: cannot write to \"%s\"\n", azArg[1]);
2063
+ if( strcmp(azArg[1],"off")!=0 ){
2064
+ fprintf(stderr,"Error: cannot write to \"%s\"\n", azArg[1]);
2065
+ }
20262066
p->out = stdout;
20272067
rc = 1;
20282068
} else {
2029
- sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
2069
+ sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
20302070
}
20312071
}
20322072
}else
20332073
20342074
if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
@@ -2394,10 +2434,22 @@
23942434
&& nArg==2
23952435
){
23962436
enableTimer = booleanValue(azArg[1]);
23972437
}else
23982438
2439
+ if( c=='t' && strncmp(azArg[0], "trace", n)==0 && nArg>1 ){
2440
+ output_file_close(p->traceOut);
2441
+ p->traceOut = output_file_open(azArg[1]);
2442
+#ifndef SQLITE_OMIT_TRACE
2443
+ if( p->traceOut==0 ){
2444
+ sqlite3_trace(p->db, 0, 0);
2445
+ }else{
2446
+ sqlite3_trace(p->db, sql_trace_callback, p->traceOut);
2447
+ }
2448
+#endif
2449
+ }else
2450
+
23992451
if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
24002452
printf("SQLite %s %s\n" /*extra-version-info*/,
24012453
sqlite3_libversion(), sqlite3_sourceid());
24022454
}else
24032455
@@ -2605,16 +2657,15 @@
26052657
return errCnt;
26062658
}
26072659
26082660
/*
26092661
** Return a pathname which is the user's home directory. A
2610
-** 0 return indicates an error of some kind. Space to hold the
2611
-** resulting string is obtained from malloc(). The calling
2612
-** function should free the result.
2662
+** 0 return indicates an error of some kind.
26132663
*/
26142664
static char *find_home_dir(void){
2615
- char *home_dir = NULL;
2665
+ static char *home_dir = NULL;
2666
+ if( home_dir ) return home_dir;
26162667
26172668
#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
26182669
struct passwd *pwent;
26192670
uid_t uid = getuid();
26202671
if( (pwent=getpwuid(uid)) != NULL) {
@@ -2623,11 +2674,11 @@
26232674
#endif
26242675
26252676
#if defined(_WIN32_WCE)
26262677
/* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
26272678
*/
2628
- home_dir = strdup("/");
2679
+ home_dir = "/";
26292680
#else
26302681
26312682
#if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
26322683
if (!home_dir) {
26332684
home_dir = getenv("USERPROFILE");
@@ -2679,11 +2730,10 @@
26792730
){
26802731
char *home_dir = NULL;
26812732
const char *sqliterc = sqliterc_override;
26822733
char *zBuf = 0;
26832734
FILE *in = NULL;
2684
- int nBuf;
26852735
int rc = 0;
26862736
26872737
if (sqliterc == NULL) {
26882738
home_dir = find_home_dir();
26892739
if( home_dir==0 ){
@@ -2690,29 +2740,22 @@
26902740
#if !defined(__RTP__) && !defined(_WRS_KERNEL)
26912741
fprintf(stderr,"%s: Error: cannot locate your home directory\n", Argv0);
26922742
#endif
26932743
return 1;
26942744
}
2695
- nBuf = strlen30(home_dir) + 16;
2696
- zBuf = malloc( nBuf );
2697
- if( zBuf==0 ){
2698
- fprintf(stderr,"%s: Error: out of memory\n",Argv0);
2699
- return 1;
2700
- }
2701
- sqlite3_snprintf(nBuf, zBuf,"%s/.sqliterc",home_dir);
2702
- free(home_dir);
2703
- sqliterc = (const char*)zBuf;
2745
+ zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
2746
+ sqliterc = zBuf;
27042747
}
27052748
in = fopen(sqliterc,"rb");
27062749
if( in ){
27072750
if( stdin_is_interactive ){
27082751
fprintf(stderr,"-- Loading resources from %s\n",sqliterc);
27092752
}
27102753
rc = process_input(p,in);
27112754
fclose(in);
27122755
}
2713
- free(zBuf);
2756
+ sqlite3_free(zBuf);
27142757
return rc;
27152758
}
27162759
27172760
/*
27182761
** Show available command line options
@@ -3055,11 +3098,10 @@
30553098
if( zHistory ){
30563099
stifle_history(100);
30573100
write_history(zHistory);
30583101
free(zHistory);
30593102
}
3060
- free(zHome);
30613103
}else{
30623104
rc = process_input(&data, stdin);
30633105
}
30643106
}
30653107
set_table_name(&data, 0);
30663108
--- src/shell.c
+++ src/shell.c
@@ -419,10 +419,11 @@
419 sqlite3 *db; /* The database */
420 int echoOn; /* True to echo input commands */
421 int statsOn; /* True to display memory stats before each finalize */
422 int cnt; /* Number of records displayed so far */
423 FILE *out; /* Write results here */
 
424 int nErr; /* Number of errors seen */
425 int mode; /* An output mode setting */
426 int writableSchema; /* True if PRAGMA writable_schema=ON */
427 int showHeader; /* True to show column names in List or Column mode */
428 char *zDestTable; /* Name of destination table when MODE_Insert */
@@ -1307,10 +1308,11 @@
1307 /* Always quote the table name, even if it appears to be pure ascii,
1308 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
1309 zTmp = appendText(zTmp, zTable, '"');
1310 if( zTmp ){
1311 zSelect = appendText(zSelect, zTmp, '\'');
 
1312 }
1313 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
1314 rc = sqlite3_step(pTableInfo);
1315 while( rc==SQLITE_ROW ){
1316 const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
@@ -1335,11 +1337,11 @@
1335 rc = run_table_dump_query(p, zSelect, zPrepStmt);
1336 if( rc==SQLITE_CORRUPT ){
1337 zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
1338 run_table_dump_query(p, zSelect, 0);
1339 }
1340 if( zSelect ) free(zSelect);
1341 }
1342 return 0;
1343 }
1344
1345 /*
@@ -1365,11 +1367,11 @@
1365 sqlite3_free(zErr);
1366 zErr = 0;
1367 }
1368 zQ2 = malloc( len+100 );
1369 if( zQ2==0 ) return rc;
1370 sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery);
1371 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
1372 if( rc ){
1373 fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
1374 }else{
1375 rc = SQLITE_CORRUPT;
@@ -1431,10 +1433,11 @@
1431 ".stats ON|OFF Turn stats on or off\n"
1432 ".tables ?TABLE? List names of tables\n"
1433 " If TABLE specified, only list tables matching\n"
1434 " LIKE pattern TABLE.\n"
1435 ".timeout MS Try opening locked tables for MS milliseconds\n"
 
1436 ".vfsname ?AUX? Print the name of the VFS stack\n"
1437 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
1438 ;
1439
1440 static char zTimerHelp[] =
@@ -1519,10 +1522,56 @@
1519 }else if( strcmp(zArg,"yes")==0 ){
1520 val = 1;
1521 }
1522 return val;
1523 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1524
1525 /*
1526 ** If an input line begins with "." then invoke this routine to
1527 ** process that line.
1528 **
@@ -1598,10 +1647,17 @@
1598 }else
1599
1600 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 && nArg>1 && nArg<3 ){
1601 bail_on_error = booleanValue(azArg[1]);
1602 }else
 
 
 
 
 
 
 
1603
1604 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 && nArg==1 ){
1605 struct callback_data data;
1606 char *zErrMsg = 0;
1607 open_db(p);
@@ -1930,26 +1986,12 @@
1930 }else
1931 #endif
1932
1933 if( c=='l' && strncmp(azArg[0], "log", n)==0 && nArg>=2 ){
1934 const char *zFile = azArg[1];
1935 if( p->pLog && p->pLog!=stdout && p->pLog!=stderr ){
1936 fclose(p->pLog);
1937 p->pLog = 0;
1938 }
1939 if( strcmp(zFile,"stdout")==0 ){
1940 p->pLog = stdout;
1941 }else if( strcmp(zFile, "stderr")==0 ){
1942 p->pLog = stderr;
1943 }else if( strcmp(zFile, "off")==0 ){
1944 p->pLog = 0;
1945 }else{
1946 p->pLog = fopen(zFile, "w");
1947 if( p->pLog==0 ){
1948 fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
1949 }
1950 }
1951 }else
1952
1953 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==2 ){
1954 int n2 = strlen30(azArg[1]);
1955 if( (n2==4 && strncmp(azArg[1],"line",n2)==0)
@@ -1998,37 +2040,35 @@
1998 sqlite3_snprintf(sizeof(p->nullvalue), p->nullvalue,
1999 "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
2000 }else
2001
2002 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
2003 if( p->out!=stdout ){
2004 if( p->outfile[0]=='|' ){
2005 pclose(p->out);
2006 }else{
2007 fclose(p->out);
2008 }
2009 }
2010 if( strcmp(azArg[1],"stdout")==0 ){
2011 p->out = stdout;
2012 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "stdout");
2013 }else if( azArg[1][0]=='|' ){
2014 p->out = popen(&azArg[1][1], "w");
2015 if( p->out==0 ){
2016 fprintf(stderr,"Error: cannot open pipe \"%s\"\n", &azArg[1][1]);
2017 p->out = stdout;
2018 rc = 1;
2019 }else{
2020 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
2021 }
2022 }else{
2023 p->out = fopen(azArg[1], "wb");
2024 if( p->out==0 ){
2025 fprintf(stderr,"Error: cannot write to \"%s\"\n", azArg[1]);
 
 
2026 p->out = stdout;
2027 rc = 1;
2028 } else {
2029 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
2030 }
2031 }
2032 }else
2033
2034 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
@@ -2394,10 +2434,22 @@
2394 && nArg==2
2395 ){
2396 enableTimer = booleanValue(azArg[1]);
2397 }else
2398
 
 
 
 
 
 
 
 
 
 
 
 
2399 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
2400 printf("SQLite %s %s\n" /*extra-version-info*/,
2401 sqlite3_libversion(), sqlite3_sourceid());
2402 }else
2403
@@ -2605,16 +2657,15 @@
2605 return errCnt;
2606 }
2607
2608 /*
2609 ** Return a pathname which is the user's home directory. A
2610 ** 0 return indicates an error of some kind. Space to hold the
2611 ** resulting string is obtained from malloc(). The calling
2612 ** function should free the result.
2613 */
2614 static char *find_home_dir(void){
2615 char *home_dir = NULL;
 
2616
2617 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
2618 struct passwd *pwent;
2619 uid_t uid = getuid();
2620 if( (pwent=getpwuid(uid)) != NULL) {
@@ -2623,11 +2674,11 @@
2623 #endif
2624
2625 #if defined(_WIN32_WCE)
2626 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
2627 */
2628 home_dir = strdup("/");
2629 #else
2630
2631 #if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
2632 if (!home_dir) {
2633 home_dir = getenv("USERPROFILE");
@@ -2679,11 +2730,10 @@
2679 ){
2680 char *home_dir = NULL;
2681 const char *sqliterc = sqliterc_override;
2682 char *zBuf = 0;
2683 FILE *in = NULL;
2684 int nBuf;
2685 int rc = 0;
2686
2687 if (sqliterc == NULL) {
2688 home_dir = find_home_dir();
2689 if( home_dir==0 ){
@@ -2690,29 +2740,22 @@
2690 #if !defined(__RTP__) && !defined(_WRS_KERNEL)
2691 fprintf(stderr,"%s: Error: cannot locate your home directory\n", Argv0);
2692 #endif
2693 return 1;
2694 }
2695 nBuf = strlen30(home_dir) + 16;
2696 zBuf = malloc( nBuf );
2697 if( zBuf==0 ){
2698 fprintf(stderr,"%s: Error: out of memory\n",Argv0);
2699 return 1;
2700 }
2701 sqlite3_snprintf(nBuf, zBuf,"%s/.sqliterc",home_dir);
2702 free(home_dir);
2703 sqliterc = (const char*)zBuf;
2704 }
2705 in = fopen(sqliterc,"rb");
2706 if( in ){
2707 if( stdin_is_interactive ){
2708 fprintf(stderr,"-- Loading resources from %s\n",sqliterc);
2709 }
2710 rc = process_input(p,in);
2711 fclose(in);
2712 }
2713 free(zBuf);
2714 return rc;
2715 }
2716
2717 /*
2718 ** Show available command line options
@@ -3055,11 +3098,10 @@
3055 if( zHistory ){
3056 stifle_history(100);
3057 write_history(zHistory);
3058 free(zHistory);
3059 }
3060 free(zHome);
3061 }else{
3062 rc = process_input(&data, stdin);
3063 }
3064 }
3065 set_table_name(&data, 0);
3066
--- src/shell.c
+++ src/shell.c
@@ -419,10 +419,11 @@
419 sqlite3 *db; /* The database */
420 int echoOn; /* True to echo input commands */
421 int statsOn; /* True to display memory stats before each finalize */
422 int cnt; /* Number of records displayed so far */
423 FILE *out; /* Write results here */
424 FILE *traceOut; /* Output for sqlite3_trace() */
425 int nErr; /* Number of errors seen */
426 int mode; /* An output mode setting */
427 int writableSchema; /* True if PRAGMA writable_schema=ON */
428 int showHeader; /* True to show column names in List or Column mode */
429 char *zDestTable; /* Name of destination table when MODE_Insert */
@@ -1307,10 +1308,11 @@
1308 /* Always quote the table name, even if it appears to be pure ascii,
1309 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
1310 zTmp = appendText(zTmp, zTable, '"');
1311 if( zTmp ){
1312 zSelect = appendText(zSelect, zTmp, '\'');
1313 free(zTmp);
1314 }
1315 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
1316 rc = sqlite3_step(pTableInfo);
1317 while( rc==SQLITE_ROW ){
1318 const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
@@ -1335,11 +1337,11 @@
1337 rc = run_table_dump_query(p, zSelect, zPrepStmt);
1338 if( rc==SQLITE_CORRUPT ){
1339 zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
1340 run_table_dump_query(p, zSelect, 0);
1341 }
1342 free(zSelect);
1343 }
1344 return 0;
1345 }
1346
1347 /*
@@ -1365,11 +1367,11 @@
1367 sqlite3_free(zErr);
1368 zErr = 0;
1369 }
1370 zQ2 = malloc( len+100 );
1371 if( zQ2==0 ) return rc;
1372 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
1373 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
1374 if( rc ){
1375 fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
1376 }else{
1377 rc = SQLITE_CORRUPT;
@@ -1431,10 +1433,11 @@
1433 ".stats ON|OFF Turn stats on or off\n"
1434 ".tables ?TABLE? List names of tables\n"
1435 " If TABLE specified, only list tables matching\n"
1436 " LIKE pattern TABLE.\n"
1437 ".timeout MS Try opening locked tables for MS milliseconds\n"
1438 ".trace FILE|off Output each SQL statement as it is run\n"
1439 ".vfsname ?AUX? Print the name of the VFS stack\n"
1440 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
1441 ;
1442
1443 static char zTimerHelp[] =
@@ -1519,10 +1522,56 @@
1522 }else if( strcmp(zArg,"yes")==0 ){
1523 val = 1;
1524 }
1525 return val;
1526 }
1527
1528 /*
1529 ** Close an output file, assuming it is not stderr or stdout
1530 */
1531 static void output_file_close(FILE *f){
1532 if( f && f!=stdout && f!=stderr ) fclose(f);
1533 }
1534
1535 /*
1536 ** Try to open an output file. The names "stdout" and "stderr" are
1537 ** recognized and do the right thing. NULL is returned if the output
1538 ** filename is "off".
1539 */
1540 static FILE *output_file_open(const char *zFile){
1541 FILE *f;
1542 if( strcmp(zFile,"stdout")==0 ){
1543 f = stdout;
1544 }else if( strcmp(zFile, "stderr")==0 ){
1545 f = stderr;
1546 }else if( strcmp(zFile, "off")==0 ){
1547 f = 0;
1548 }else{
1549 f = fopen(zFile, "wb");
1550 if( f==0 ){
1551 fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
1552 }
1553 }
1554 return f;
1555 }
1556
1557 /*
1558 ** A routine for handling output from sqlite3_trace().
1559 */
1560 static void sql_trace_callback(void *pArg, const char *z){
1561 FILE *f = (FILE*)pArg;
1562 if( f ) fprintf(f, "%s\n", z);
1563 }
1564
1565 /*
1566 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
1567 ** a useful spot to set a debugger breakpoint.
1568 */
1569 static void test_breakpoint(void){
1570 static int nCall = 0;
1571 nCall++;
1572 }
1573
1574 /*
1575 ** If an input line begins with "." then invoke this routine to
1576 ** process that line.
1577 **
@@ -1598,10 +1647,17 @@
1647 }else
1648
1649 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 && nArg>1 && nArg<3 ){
1650 bail_on_error = booleanValue(azArg[1]);
1651 }else
1652
1653 /* The undocumented ".breakpoint" command causes a call to the no-op
1654 ** routine named test_breakpoint().
1655 */
1656 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
1657 test_breakpoint();
1658 }else
1659
1660 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 && nArg==1 ){
1661 struct callback_data data;
1662 char *zErrMsg = 0;
1663 open_db(p);
@@ -1930,26 +1986,12 @@
1986 }else
1987 #endif
1988
1989 if( c=='l' && strncmp(azArg[0], "log", n)==0 && nArg>=2 ){
1990 const char *zFile = azArg[1];
1991 output_file_close(p->pLog);
1992 p->pLog = output_file_open(zFile);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1993 }else
1994
1995 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==2 ){
1996 int n2 = strlen30(azArg[1]);
1997 if( (n2==4 && strncmp(azArg[1],"line",n2)==0)
@@ -1998,37 +2040,35 @@
2040 sqlite3_snprintf(sizeof(p->nullvalue), p->nullvalue,
2041 "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
2042 }else
2043
2044 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
2045 if( p->outfile[0]=='|' ){
2046 pclose(p->out);
2047 }else{
2048 output_file_close(p->out);
2049 }
2050 p->outfile[0] = 0;
2051 if( azArg[1][0]=='|' ){
 
 
 
 
2052 p->out = popen(&azArg[1][1], "w");
2053 if( p->out==0 ){
2054 fprintf(stderr,"Error: cannot open pipe \"%s\"\n", &azArg[1][1]);
2055 p->out = stdout;
2056 rc = 1;
2057 }else{
2058 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
2059 }
2060 }else{
2061 p->out = output_file_open(azArg[1]);
2062 if( p->out==0 ){
2063 if( strcmp(azArg[1],"off")!=0 ){
2064 fprintf(stderr,"Error: cannot write to \"%s\"\n", azArg[1]);
2065 }
2066 p->out = stdout;
2067 rc = 1;
2068 } else {
2069 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
2070 }
2071 }
2072 }else
2073
2074 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
@@ -2394,10 +2434,22 @@
2434 && nArg==2
2435 ){
2436 enableTimer = booleanValue(azArg[1]);
2437 }else
2438
2439 if( c=='t' && strncmp(azArg[0], "trace", n)==0 && nArg>1 ){
2440 output_file_close(p->traceOut);
2441 p->traceOut = output_file_open(azArg[1]);
2442 #ifndef SQLITE_OMIT_TRACE
2443 if( p->traceOut==0 ){
2444 sqlite3_trace(p->db, 0, 0);
2445 }else{
2446 sqlite3_trace(p->db, sql_trace_callback, p->traceOut);
2447 }
2448 #endif
2449 }else
2450
2451 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
2452 printf("SQLite %s %s\n" /*extra-version-info*/,
2453 sqlite3_libversion(), sqlite3_sourceid());
2454 }else
2455
@@ -2605,16 +2657,15 @@
2657 return errCnt;
2658 }
2659
2660 /*
2661 ** Return a pathname which is the user's home directory. A
2662 ** 0 return indicates an error of some kind.
 
 
2663 */
2664 static char *find_home_dir(void){
2665 static char *home_dir = NULL;
2666 if( home_dir ) return home_dir;
2667
2668 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
2669 struct passwd *pwent;
2670 uid_t uid = getuid();
2671 if( (pwent=getpwuid(uid)) != NULL) {
@@ -2623,11 +2674,11 @@
2674 #endif
2675
2676 #if defined(_WIN32_WCE)
2677 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
2678 */
2679 home_dir = "/";
2680 #else
2681
2682 #if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
2683 if (!home_dir) {
2684 home_dir = getenv("USERPROFILE");
@@ -2679,11 +2730,10 @@
2730 ){
2731 char *home_dir = NULL;
2732 const char *sqliterc = sqliterc_override;
2733 char *zBuf = 0;
2734 FILE *in = NULL;
 
2735 int rc = 0;
2736
2737 if (sqliterc == NULL) {
2738 home_dir = find_home_dir();
2739 if( home_dir==0 ){
@@ -2690,29 +2740,22 @@
2740 #if !defined(__RTP__) && !defined(_WRS_KERNEL)
2741 fprintf(stderr,"%s: Error: cannot locate your home directory\n", Argv0);
2742 #endif
2743 return 1;
2744 }
2745 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
2746 sqliterc = zBuf;
 
 
 
 
 
 
 
2747 }
2748 in = fopen(sqliterc,"rb");
2749 if( in ){
2750 if( stdin_is_interactive ){
2751 fprintf(stderr,"-- Loading resources from %s\n",sqliterc);
2752 }
2753 rc = process_input(p,in);
2754 fclose(in);
2755 }
2756 sqlite3_free(zBuf);
2757 return rc;
2758 }
2759
2760 /*
2761 ** Show available command line options
@@ -3055,11 +3098,10 @@
3098 if( zHistory ){
3099 stifle_history(100);
3100 write_history(zHistory);
3101 free(zHistory);
3102 }
 
3103 }else{
3104 rc = process_input(&data, stdin);
3105 }
3106 }
3107 set_table_name(&data, 0);
3108
+454 -251
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -657,11 +657,11 @@
657657
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
658658
** [sqlite_version()] and [sqlite_source_id()].
659659
*/
660660
#define SQLITE_VERSION "3.7.12"
661661
#define SQLITE_VERSION_NUMBER 3007012
662
-#define SQLITE_SOURCE_ID "2012-03-31 19:12:23 af602d87736b52802a4e760ffeeaa28112b99d9a"
662
+#define SQLITE_SOURCE_ID "2012-04-17 09:09:33 8e2363ad76446e863d03ead91fd621e59d5cb495"
663663
664664
/*
665665
** CAPI3REF: Run-Time Library Version Numbers
666666
** KEYWORDS: sqlite3_version, sqlite3_sourceid
667667
**
@@ -2095,11 +2095,11 @@
20952095
** database connection is opened. By default, URI handling is globally
20962096
** disabled. The default value may be changed by compiling with the
20972097
** [SQLITE_USE_URI] symbol defined.
20982098
**
20992099
** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2100
-** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFNIG_GETPCACHE
2100
+** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
21012101
** <dd> These options are obsolete and should not be used by new code.
21022102
** They are retained for backwards compatibility but are now no-ops.
21032103
** </dl>
21042104
*/
21052105
#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
@@ -7533,11 +7533,15 @@
75337533
** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
75347534
*/
75357535
SQLITE_API int sqlite3_rtree_geometry_callback(
75367536
sqlite3 *db,
75377537
const char *zGeom,
7538
- int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes),
7538
+#ifdef SQLITE_RTREE_INT_ONLY
7539
+ int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
7540
+#else
7541
+ int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
7542
+#endif
75397543
void *pContext
75407544
);
75417545
75427546
75437547
/*
@@ -10933,11 +10937,10 @@
1093310937
int nRef; /* Number of names resolved by this context */
1093410938
int nErr; /* Number of errors encountered while resolving names */
1093510939
u8 allowAgg; /* Aggregate functions allowed here */
1093610940
u8 hasAgg; /* True if aggregates are seen */
1093710941
u8 isCheck; /* True if resolving names in a CHECK constraint */
10938
- int nDepth; /* Depth of subquery recursion. 1 for no recursion */
1093910942
AggInfo *pAggInfo; /* Information about aggregates at this level */
1094010943
NameContext *pNext; /* Next outer name context. NULL for outermost */
1094110944
};
1094210945
1094310946
/*
@@ -11399,10 +11402,11 @@
1139911402
int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
1140011403
Parse *pParse; /* Parser context. */
1140111404
union { /* Extra data for callback */
1140211405
NameContext *pNC; /* Naming context */
1140311406
int i; /* Integer value */
11407
+ SrcList *pSrcList; /* FROM clause */
1140411408
} u;
1140511409
};
1140611410
1140711411
/* Forward declarations */
1140811412
SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
@@ -11767,11 +11771,11 @@
1176711771
SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
1176811772
SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
1176911773
SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
1177011774
SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
1177111775
SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
11772
-SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
11776
+SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
1177311777
SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
1177411778
SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
1177511779
SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
1177611780
SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
1177711781
SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
@@ -24995,12 +24999,12 @@
2499524999
#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
2499625000
# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
2499725001
#endif
2499825002
2499925003
/*
25000
- ** Default permissions when creating auto proxy dir
25001
- */
25004
+** Default permissions when creating auto proxy dir
25005
+*/
2500225006
#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
2500325007
# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
2500425008
#endif
2500525009
2500625010
/*
@@ -25550,11 +25554,11 @@
2555025554
return 0;
2555125555
}
2555225556
2555325557
/*
2555425558
** Invoke open(). Do so multiple times, until it either succeeds or
25555
-** files for some reason other than EINTR.
25559
+** fails for some reason other than EINTR.
2555625560
**
2555725561
** If the file creation mode "m" is 0 then set it to the default for
2555825562
** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
2555925563
** 0644) as modified by the system umask. If m is not 0, then
2556025564
** make the file creation mode be exactly m ignoring the umask.
@@ -25566,24 +25570,33 @@
2556625570
** transaction crashes and leaves behind hot journals, then any
2556725571
** process that is able to write to the database will also be able to
2556825572
** recover the hot journals.
2556925573
*/
2557025574
static int robust_open(const char *z, int f, mode_t m){
25571
- int rc;
25575
+ int fd;
2557225576
mode_t m2;
2557325577
mode_t origM = 0;
2557425578
if( m==0 ){
2557525579
m2 = SQLITE_DEFAULT_FILE_PERMISSIONS;
2557625580
}else{
2557725581
m2 = m;
2557825582
origM = osUmask(0);
2557925583
}
25580
- do{ rc = osOpen(z,f,m2); }while( rc<0 && errno==EINTR );
25584
+ do{
25585
+#if defined(O_CLOEXEC)
25586
+ fd = osOpen(z,f|O_CLOEXEC,m2);
25587
+#else
25588
+ fd = osOpen(z,f,m2);
25589
+#endif
25590
+ }while( fd<0 && errno==EINTR );
2558125591
if( m ){
2558225592
osUmask(origM);
2558325593
}
25584
- return rc;
25594
+#if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
25595
+ if( fd>=0 ) osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
25596
+#endif
25597
+ return fd;
2558525598
}
2558625599
2558725600
/*
2558825601
** Helper functions to obtain and relinquish the global mutex. The
2558925602
** global mutex is used to protect the unixInodeInfo and
@@ -28374,13 +28387,10 @@
2837428387
for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
2837528388
if( ii>0 ){
2837628389
zDirname[ii] = '\0';
2837728390
fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
2837828391
if( fd>=0 ){
28379
-#ifdef FD_CLOEXEC
28380
- osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
28381
-#endif
2838228392
OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
2838328393
}
2838428394
}
2838528395
*pFd = fd;
2838628396
return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
@@ -28459,11 +28469,11 @@
2845928469
/* If the user has configured a chunk-size for this file, truncate the
2846028470
** file so that it consists of an integer number of chunks (i.e. the
2846128471
** actual file size after the operation may be larger than the requested
2846228472
** size).
2846328473
*/
28464
- if( pFile->szChunk ){
28474
+ if( pFile->szChunk>0 ){
2846528475
nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
2846628476
}
2846728477
2846828478
rc = robust_ftruncate(pFile->h, (off_t)nByte);
2846928479
if( rc ){
@@ -30221,14 +30231,10 @@
3022130231
else{
3022230232
p->openFlags = openFlags;
3022330233
}
3022430234
#endif
3022530235
30226
-#ifdef FD_CLOEXEC
30227
- osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
30228
-#endif
30229
-
3023030236
noLock = eType!=SQLITE_OPEN_MAIN_DB;
3023130237
3023230238
3023330239
#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
3023430240
if( fstatfs(fd, &fsInfo) == -1 ){
@@ -38001,10 +38007,15 @@
3800138007
#define ROWSET_ENTRY_PER_CHUNK \
3800238008
((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
3800338009
3800438010
/*
3800538011
** Each entry in a RowSet is an instance of the following object.
38012
+**
38013
+** This same object is reused to store a linked list of trees of RowSetEntry
38014
+** objects. In that alternative use, pRight points to the next entry
38015
+** in the list, pLeft points to the tree, and v is unused. The
38016
+** RowSet.pForest value points to the head of this forest list.
3800638017
*/
3800738018
struct RowSetEntry {
3800838019
i64 v; /* ROWID value for this entry */
3800938020
struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
3801038021
struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
@@ -38030,16 +38041,22 @@
3803038041
struct RowSetChunk *pChunk; /* List of all chunk allocations */
3803138042
sqlite3 *db; /* The database connection */
3803238043
struct RowSetEntry *pEntry; /* List of entries using pRight */
3803338044
struct RowSetEntry *pLast; /* Last entry on the pEntry list */
3803438045
struct RowSetEntry *pFresh; /* Source of new entry objects */
38035
- struct RowSetEntry *pTree; /* Binary tree of entries */
38046
+ struct RowSetEntry *pForest; /* List of binary trees of entries */
3803638047
u16 nFresh; /* Number of objects on pFresh */
38037
- u8 isSorted; /* True if pEntry is sorted */
38048
+ u8 rsFlags; /* Various flags */
3803838049
u8 iBatch; /* Current insert batch */
3803938050
};
3804038051
38052
+/*
38053
+** Allowed values for RowSet.rsFlags
38054
+*/
38055
+#define ROWSET_SORTED 0x01 /* True if RowSet.pEntry is sorted */
38056
+#define ROWSET_NEXT 0x02 /* True if sqlite3RowSetNext() has been called */
38057
+
3804138058
/*
3804238059
** Turn bulk memory into a RowSet object. N bytes of memory
3804338060
** are available at pSpace. The db pointer is used as a memory context
3804438061
** for any subsequent allocations that need to occur.
3804538062
** Return a pointer to the new RowSet object.
@@ -38056,14 +38073,14 @@
3805638073
p = pSpace;
3805738074
p->pChunk = 0;
3805838075
p->db = db;
3805938076
p->pEntry = 0;
3806038077
p->pLast = 0;
38061
- p->pTree = 0;
38078
+ p->pForest = 0;
3806238079
p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
3806338080
p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
38064
- p->isSorted = 1;
38081
+ p->rsFlags = ROWSET_SORTED;
3806538082
p->iBatch = 0;
3806638083
return p;
3806738084
}
3806838085
3806938086
/*
@@ -38079,12 +38096,37 @@
3807938096
}
3808038097
p->pChunk = 0;
3808138098
p->nFresh = 0;
3808238099
p->pEntry = 0;
3808338100
p->pLast = 0;
38084
- p->pTree = 0;
38085
- p->isSorted = 1;
38101
+ p->pForest = 0;
38102
+ p->rsFlags = ROWSET_SORTED;
38103
+}
38104
+
38105
+/*
38106
+** Allocate a new RowSetEntry object that is associated with the
38107
+** given RowSet. Return a pointer to the new and completely uninitialized
38108
+** objected.
38109
+**
38110
+** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
38111
+** routine returns NULL.
38112
+*/
38113
+static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
38114
+ assert( p!=0 );
38115
+ if( p->nFresh==0 ){
38116
+ struct RowSetChunk *pNew;
38117
+ pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
38118
+ if( pNew==0 ){
38119
+ return 0;
38120
+ }
38121
+ pNew->pNextChunk = p->pChunk;
38122
+ p->pChunk = pNew;
38123
+ p->pFresh = pNew->aEntry;
38124
+ p->nFresh = ROWSET_ENTRY_PER_CHUNK;
38125
+ }
38126
+ p->nFresh--;
38127
+ return p->pFresh++;
3808638128
}
3808738129
3808838130
/*
3808938131
** Insert a new value into a RowSet.
3809038132
**
@@ -38092,34 +38134,25 @@
3809238134
** memory allocation fails.
3809338135
*/
3809438136
SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
3809538137
struct RowSetEntry *pEntry; /* The new entry */
3809638138
struct RowSetEntry *pLast; /* The last prior entry */
38097
- assert( p!=0 );
38098
- if( p->nFresh==0 ){
38099
- struct RowSetChunk *pNew;
38100
- pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
38101
- if( pNew==0 ){
38102
- return;
38103
- }
38104
- pNew->pNextChunk = p->pChunk;
38105
- p->pChunk = pNew;
38106
- p->pFresh = pNew->aEntry;
38107
- p->nFresh = ROWSET_ENTRY_PER_CHUNK;
38108
- }
38109
- pEntry = p->pFresh++;
38110
- p->nFresh--;
38139
+
38140
+ /* This routine is never called after sqlite3RowSetNext() */
38141
+ assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
38142
+
38143
+ pEntry = rowSetEntryAlloc(p);
38144
+ if( pEntry==0 ) return;
3811138145
pEntry->v = rowid;
3811238146
pEntry->pRight = 0;
3811338147
pLast = p->pLast;
3811438148
if( pLast ){
38115
- if( p->isSorted && rowid<=pLast->v ){
38116
- p->isSorted = 0;
38149
+ if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){
38150
+ p->rsFlags &= ~ROWSET_SORTED;
3811738151
}
3811838152
pLast->pRight = pEntry;
3811938153
}else{
38120
- assert( p->pEntry==0 ); /* Fires if INSERT after SMALLEST */
3812138154
p->pEntry = pEntry;
3812238155
}
3812338156
p->pLast = pEntry;
3812438157
}
3812538158
@@ -38127,11 +38160,11 @@
3812738160
** Merge two lists of RowSetEntry objects. Remove duplicates.
3812838161
**
3812938162
** The input lists are connected via pRight pointers and are
3813038163
** assumed to each already be in sorted order.
3813138164
*/
38132
-static struct RowSetEntry *rowSetMerge(
38165
+static struct RowSetEntry *rowSetEntryMerge(
3813338166
struct RowSetEntry *pA, /* First sorted list to be merged */
3813438167
struct RowSetEntry *pB /* Second sorted list to be merged */
3813538168
){
3813638169
struct RowSetEntry head;
3813738170
struct RowSetEntry *pTail;
@@ -38161,36 +38194,33 @@
3816138194
}
3816238195
return head.pRight;
3816338196
}
3816438197
3816538198
/*
38166
-** Sort all elements on the pEntry list of the RowSet into ascending order.
38199
+** Sort all elements on the list of RowSetEntry objects into order of
38200
+** increasing v.
3816738201
*/
38168
-static void rowSetSort(RowSet *p){
38202
+static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
3816938203
unsigned int i;
38170
- struct RowSetEntry *pEntry;
38171
- struct RowSetEntry *aBucket[40];
38204
+ struct RowSetEntry *pNext, *aBucket[40];
3817238205
38173
- assert( p->isSorted==0 );
3817438206
memset(aBucket, 0, sizeof(aBucket));
38175
- while( p->pEntry ){
38176
- pEntry = p->pEntry;
38177
- p->pEntry = pEntry->pRight;
38178
- pEntry->pRight = 0;
38207
+ while( pIn ){
38208
+ pNext = pIn->pRight;
38209
+ pIn->pRight = 0;
3817938210
for(i=0; aBucket[i]; i++){
38180
- pEntry = rowSetMerge(aBucket[i], pEntry);
38211
+ pIn = rowSetEntryMerge(aBucket[i], pIn);
3818138212
aBucket[i] = 0;
3818238213
}
38183
- aBucket[i] = pEntry;
38214
+ aBucket[i] = pIn;
38215
+ pIn = pNext;
3818438216
}
38185
- pEntry = 0;
38217
+ pIn = 0;
3818638218
for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
38187
- pEntry = rowSetMerge(pEntry, aBucket[i]);
38219
+ pIn = rowSetEntryMerge(pIn, aBucket[i]);
3818838220
}
38189
- p->pEntry = pEntry;
38190
- p->pLast = 0;
38191
- p->isSorted = 1;
38221
+ return pIn;
3819238222
}
3819338223
3819438224
3819538225
/*
3819638226
** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
@@ -38280,24 +38310,41 @@
3828038310
}
3828138311
return p;
3828238312
}
3828338313
3828438314
/*
38285
-** Convert the list in p->pEntry into a sorted list if it is not
38286
-** sorted already. If there is a binary tree on p->pTree, then
38287
-** convert it into a list too and merge it into the p->pEntry list.
38315
+** Take all the entries on p->pEntry and on the trees in p->pForest and
38316
+** sort them all together into one big ordered list on p->pEntry.
38317
+**
38318
+** This routine should only be called once in the life of a RowSet.
3828838319
*/
3828938320
static void rowSetToList(RowSet *p){
38290
- if( !p->isSorted ){
38291
- rowSetSort(p);
38292
- }
38293
- if( p->pTree ){
38294
- struct RowSetEntry *pHead, *pTail;
38295
- rowSetTreeToList(p->pTree, &pHead, &pTail);
38296
- p->pTree = 0;
38297
- p->pEntry = rowSetMerge(p->pEntry, pHead);
38298
- }
38321
+
38322
+ /* This routine is called only once */
38323
+ assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
38324
+
38325
+ if( (p->rsFlags & ROWSET_SORTED)==0 ){
38326
+ p->pEntry = rowSetEntrySort(p->pEntry);
38327
+ }
38328
+
38329
+ /* While this module could theoretically support it, sqlite3RowSetNext()
38330
+ ** is never called after sqlite3RowSetText() for the same RowSet. So
38331
+ ** there is never a forest to deal with. Should this change, simply
38332
+ ** remove the assert() and the #if 0. */
38333
+ assert( p->pForest==0 );
38334
+#if 0
38335
+ while( p->pForest ){
38336
+ struct RowSetEntry *pTree = p->pForest->pLeft;
38337
+ if( pTree ){
38338
+ struct RowSetEntry *pHead, *pTail;
38339
+ rowSetTreeToList(pTree, &pHead, &pTail);
38340
+ p->pEntry = rowSetEntryMerge(p->pEntry, pHead);
38341
+ }
38342
+ p->pForest = p->pForest->pRight;
38343
+ }
38344
+#endif
38345
+ p->rsFlags |= ROWSET_NEXT; /* Verify this routine is never called again */
3829938346
}
3830038347
3830138348
/*
3830238349
** Extract the smallest element from the RowSet.
3830338350
** Write the element into *pRowid. Return 1 on success. Return
@@ -38305,11 +38352,16 @@
3830538352
**
3830638353
** After this routine has been called, the sqlite3RowSetInsert()
3830738354
** routine may not be called again.
3830838355
*/
3830938356
SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
38310
- rowSetToList(p);
38357
+ assert( p!=0 );
38358
+
38359
+ /* Merge the forest into a single sorted list on first call */
38360
+ if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p);
38361
+
38362
+ /* Return the next entry on the list */
3831138363
if( p->pEntry ){
3831238364
*pRowid = p->pEntry->v;
3831338365
p->pEntry = p->pEntry->pRight;
3831438366
if( p->pEntry==0 ){
3831538367
sqlite3RowSetClear(p);
@@ -38321,30 +38373,70 @@
3832138373
}
3832238374
3832338375
/*
3832438376
** Check to see if element iRowid was inserted into the the rowset as
3832538377
** part of any insert batch prior to iBatch. Return 1 or 0.
38378
+**
38379
+** If this is the first test of a new batch and if there exist entires
38380
+** on pRowSet->pEntry, then sort those entires into the forest at
38381
+** pRowSet->pForest so that they can be tested.
3832638382
*/
3832738383
SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
38328
- struct RowSetEntry *p;
38384
+ struct RowSetEntry *p, *pTree;
38385
+
38386
+ /* This routine is never called after sqlite3RowSetNext() */
38387
+ assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
38388
+
38389
+ /* Sort entries into the forest on the first test of a new batch
38390
+ */
3832938391
if( iBatch!=pRowSet->iBatch ){
38330
- if( pRowSet->pEntry ){
38331
- rowSetToList(pRowSet);
38332
- pRowSet->pTree = rowSetListToTree(pRowSet->pEntry);
38392
+ p = pRowSet->pEntry;
38393
+ if( p ){
38394
+ struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
38395
+ if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){
38396
+ p = rowSetEntrySort(p);
38397
+ }
38398
+ for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
38399
+ ppPrevTree = &pTree->pRight;
38400
+ if( pTree->pLeft==0 ){
38401
+ pTree->pLeft = rowSetListToTree(p);
38402
+ break;
38403
+ }else{
38404
+ struct RowSetEntry *pAux, *pTail;
38405
+ rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
38406
+ pTree->pLeft = 0;
38407
+ p = rowSetEntryMerge(pAux, p);
38408
+ }
38409
+ }
38410
+ if( pTree==0 ){
38411
+ *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
38412
+ if( pTree ){
38413
+ pTree->v = 0;
38414
+ pTree->pRight = 0;
38415
+ pTree->pLeft = rowSetListToTree(p);
38416
+ }
38417
+ }
3833338418
pRowSet->pEntry = 0;
3833438419
pRowSet->pLast = 0;
38420
+ pRowSet->rsFlags |= ROWSET_SORTED;
3833538421
}
3833638422
pRowSet->iBatch = iBatch;
3833738423
}
38338
- p = pRowSet->pTree;
38339
- while( p ){
38340
- if( p->v<iRowid ){
38341
- p = p->pRight;
38342
- }else if( p->v>iRowid ){
38343
- p = p->pLeft;
38344
- }else{
38345
- return 1;
38424
+
38425
+ /* Test to see if the iRowid value appears anywhere in the forest.
38426
+ ** Return 1 if it does and 0 if not.
38427
+ */
38428
+ for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
38429
+ p = pTree->pLeft;
38430
+ while( p ){
38431
+ if( p->v<iRowid ){
38432
+ p = p->pRight;
38433
+ }else if( p->v>iRowid ){
38434
+ p = p->pLeft;
38435
+ }else{
38436
+ return 1;
38437
+ }
3834638438
}
3834738439
}
3834838440
return 0;
3834938441
}
3835038442
@@ -49148,16 +49240,22 @@
4914849240
4914949241
4915049242
/*
4915149243
** This structure is passed around through all the sanity checking routines
4915249244
** in order to keep track of some global state information.
49245
+**
49246
+** The aRef[] array is allocated so that there is 1 bit for each page in
49247
+** the database. As the integrity-check proceeds, for each page used in
49248
+** the database the corresponding bit is set. This allows integrity-check to
49249
+** detect pages that are used twice and orphaned pages (both of which
49250
+** indicate corruption).
4915349251
*/
4915449252
typedef struct IntegrityCk IntegrityCk;
4915549253
struct IntegrityCk {
4915649254
BtShared *pBt; /* The tree being checked out */
4915749255
Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
49158
- int *anRef; /* Number of times each page is referenced */
49256
+ u8 *aPgRef; /* 1 bit per page in the db (see above) */
4915949257
Pgno nPage; /* Number of pages in the database */
4916049258
int mxErr; /* Stop accumulating errors when this reaches zero */
4916149259
int nErr; /* Number of messages written to zErrMsg so far */
4916249260
int mallocFailed; /* A memory allocation error has occurred */
4916349261
StrAccum errMsg; /* Accumulate the error message text here */
@@ -56998,10 +57096,29 @@
5699857096
}
5699957097
}
5700057098
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
5700157099
5700257100
#ifndef SQLITE_OMIT_INTEGRITY_CHECK
57101
+
57102
+/*
57103
+** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
57104
+** corresponds to page iPg is already set.
57105
+*/
57106
+static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
57107
+ assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
57108
+ return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
57109
+}
57110
+
57111
+/*
57112
+** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
57113
+*/
57114
+static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
57115
+ assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
57116
+ pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
57117
+}
57118
+
57119
+
5700357120
/*
5700457121
** Add 1 to the reference count for page iPage. If this is the second
5700557122
** reference to the page, add an error message to pCheck->zErrMsg.
5700657123
** Return 1 if there are 2 ore more references to the page and 0 if
5700757124
** if this is the first reference to the page.
@@ -57012,15 +57129,16 @@
5701257129
if( iPage==0 ) return 1;
5701357130
if( iPage>pCheck->nPage ){
5701457131
checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
5701557132
return 1;
5701657133
}
57017
- if( pCheck->anRef[iPage]==1 ){
57134
+ if( getPageReferenced(pCheck, iPage) ){
5701857135
checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
5701957136
return 1;
5702057137
}
57021
- return (pCheck->anRef[iPage]++)>1;
57138
+ setPageReferenced(pCheck, iPage);
57139
+ return 0;
5702257140
}
5702357141
5702457142
#ifndef SQLITE_OMIT_AUTOVACUUM
5702557143
/*
5702657144
** Check that the entry in the pointer-map for page iChild maps to
@@ -57392,21 +57510,19 @@
5739257510
*pnErr = 0;
5739357511
if( sCheck.nPage==0 ){
5739457512
sqlite3BtreeLeave(p);
5739557513
return 0;
5739657514
}
57397
- sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
57398
- if( !sCheck.anRef ){
57515
+
57516
+ sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
57517
+ if( !sCheck.aPgRef ){
5739957518
*pnErr = 1;
5740057519
sqlite3BtreeLeave(p);
5740157520
return 0;
5740257521
}
57403
- for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
5740457522
i = PENDING_BYTE_PAGE(pBt);
57405
- if( i<=sCheck.nPage ){
57406
- sCheck.anRef[i] = 1;
57407
- }
57523
+ if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
5740857524
sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
5740957525
sCheck.errMsg.useMalloc = 2;
5741057526
5741157527
/* Check the integrity of the freelist
5741257528
*/
@@ -57427,22 +57543,22 @@
5742757543
5742857544
/* Make sure every page in the file is referenced
5742957545
*/
5743057546
for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
5743157547
#ifdef SQLITE_OMIT_AUTOVACUUM
57432
- if( sCheck.anRef[i]==0 ){
57548
+ if( getPageReferenced(&sCheck, i)==0 ){
5743357549
checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
5743457550
}
5743557551
#else
5743657552
/* If the database supports auto-vacuum, make sure no tables contain
5743757553
** references to pointer-map pages.
5743857554
*/
57439
- if( sCheck.anRef[i]==0 &&
57555
+ if( getPageReferenced(&sCheck, i)==0 &&
5744057556
(PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
5744157557
checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
5744257558
}
57443
- if( sCheck.anRef[i]!=0 &&
57559
+ if( getPageReferenced(&sCheck, i)!=0 &&
5744457560
(PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
5744557561
checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
5744657562
}
5744757563
#endif
5744857564
}
@@ -57459,11 +57575,11 @@
5745957575
}
5746057576
5746157577
/* Clean up and report errors.
5746257578
*/
5746357579
sqlite3BtreeLeave(p);
57464
- sqlite3_free(sCheck.anRef);
57580
+ sqlite3_free(sCheck.aPgRef);
5746557581
if( sCheck.mallocFailed ){
5746657582
sqlite3StrAccumReset(&sCheck.errMsg);
5746757583
*pnErr = sCheck.nErr+1;
5746857584
return 0;
5746957585
}
@@ -73807,11 +73923,11 @@
7380773923
assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
7380873924
zId = pExpr->u.zToken;
7380973925
nId = sqlite3Strlen30(zId);
7381073926
pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
7381173927
if( pDef==0 ){
73812
- pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
73928
+ pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
7381373929
if( pDef==0 ){
7381473930
no_such_func = 1;
7381573931
}else{
7381673932
wrong_num_args = 1;
7381773933
}
@@ -78277,11 +78393,11 @@
7827778393
if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
7827878394
if( ExprHasProperty(pA, EP_IntValue) ){
7827978395
if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
7828078396
return 2;
7828178397
}
78282
- }else if( pA->op!=TK_COLUMN && pA->u.zToken ){
78398
+ }else if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
7828378399
if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
7828478400
if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
7828578401
return 2;
7828678402
}
7828778403
}
@@ -78313,10 +78429,45 @@
7831378429
if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
7831478430
if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
7831578431
}
7831678432
return 0;
7831778433
}
78434
+
78435
+/*
78436
+** This is the expression callback for sqlite3FunctionUsesOtherSrc().
78437
+**
78438
+** Determine if an expression references any table other than one of the
78439
+** tables in pWalker->u.pSrcList and abort if it does.
78440
+*/
78441
+static int exprUsesOtherSrc(Walker *pWalker, Expr *pExpr){
78442
+ if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){
78443
+ int i;
78444
+ SrcList *pSrc = pWalker->u.pSrcList;
78445
+ for(i=0; i<pSrc->nSrc; i++){
78446
+ if( pExpr->iTable==pSrc->a[i].iCursor ) return WRC_Continue;
78447
+ }
78448
+ return WRC_Abort;
78449
+ }else{
78450
+ return WRC_Continue;
78451
+ }
78452
+}
78453
+
78454
+/*
78455
+** Determine if any of the arguments to the pExpr Function references
78456
+** any SrcList other than pSrcList. Return true if they do. Return
78457
+** false if pExpr has no argument or has only constant arguments or
78458
+** only references tables named in pSrcList.
78459
+*/
78460
+static int sqlite3FunctionUsesOtherSrc(Expr *pExpr, SrcList *pSrcList){
78461
+ Walker w;
78462
+ assert( pExpr->op==TK_AGG_FUNCTION );
78463
+ memset(&w, 0, sizeof(w));
78464
+ w.xExprCallback = exprUsesOtherSrc;
78465
+ w.u.pSrcList = pSrcList;
78466
+ if( sqlite3WalkExprList(&w, pExpr->x.pList)!=WRC_Continue ) return 1;
78467
+ return 0;
78468
+}
7831878469
7831978470
/*
7832078471
** Add a new element to the pAggInfo->aCol[] array. Return the index of
7832178472
** the new element. Return a negative number if malloc fails.
7832278473
*/
@@ -78429,13 +78580,11 @@
7842978580
} /* end loop over pSrcList */
7843078581
}
7843178582
return WRC_Prune;
7843278583
}
7843378584
case TK_AGG_FUNCTION: {
78434
- /* The pNC->nDepth==0 test causes aggregate functions in subqueries
78435
- ** to be ignored */
78436
- if( pNC->nDepth==0 ){
78585
+ if( !sqlite3FunctionUsesOtherSrc(pExpr, pSrcList) ){
7843778586
/* Check to see if pExpr is a duplicate of another aggregate
7843878587
** function that is already in the pAggInfo structure
7843978588
*/
7844078589
struct AggInfo_func *pItem = pAggInfo->aFunc;
7844178590
for(i=0; i<pAggInfo->nFunc; i++, pItem++){
@@ -78475,19 +78624,11 @@
7847578624
}
7847678625
}
7847778626
return WRC_Continue;
7847878627
}
7847978628
static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
78480
- NameContext *pNC = pWalker->u.pNC;
78481
- if( pNC->nDepth==0 ){
78482
- pNC->nDepth++;
78483
- sqlite3WalkSelect(pWalker, pSelect);
78484
- pNC->nDepth--;
78485
- return WRC_Prune;
78486
- }else{
78487
- return WRC_Continue;
78488
- }
78629
+ return WRC_Continue;
7848978630
}
7849078631
7849178632
/*
7849278633
** Analyze the given expression looking for aggregate functions and
7849378634
** for variables that need to be added to the pParse->aAgg[] array.
@@ -78496,10 +78637,11 @@
7849678637
** This routine should only be called after the expression has been
7849778638
** analyzed by sqlite3ResolveExprNames().
7849878639
*/
7849978640
SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
7850078641
Walker w;
78642
+ memset(&w, 0, sizeof(w));
7850178643
w.xExprCallback = analyzeAggregate;
7850278644
w.xSelectCallback = analyzeAggregatesInSelect;
7850378645
w.u.pNC = pNC;
7850478646
assert( pNC->pSrcList!=0 );
7850578647
sqlite3WalkExpr(&w, pExpr);
@@ -85399,43 +85541,62 @@
8539985541
/* During the search for the best function definition, this procedure
8540085542
** is called to test how well the function passed as the first argument
8540185543
** matches the request for a function with nArg arguments in a system
8540285544
** that uses encoding enc. The value returned indicates how well the
8540385545
** request is matched. A higher value indicates a better match.
85546
+**
85547
+** If nArg is -1 that means to only return a match (non-zero) if p->nArg
85548
+** is also -1. In other words, we are searching for a function that
85549
+** takes a variable number of arguments.
85550
+**
85551
+** If nArg is -2 that means that we are searching for any function
85552
+** regardless of the number of arguments it uses, so return a positive
85553
+** match score for any
8540485554
**
8540585555
** The returned value is always between 0 and 6, as follows:
8540685556
**
85407
-** 0: Not a match, or if nArg<0 and the function is has no implementation.
85408
-** 1: A variable arguments function that prefers UTF-8 when a UTF-16
85409
-** encoding is requested, or vice versa.
85410
-** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
85411
-** requested, or vice versa.
85412
-** 3: A variable arguments function using the same text encoding.
85413
-** 4: A function with the exact number of arguments requested that
85414
-** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
85415
-** 5: A function with the exact number of arguments requested that
85416
-** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
85417
-** 6: An exact match.
85557
+** 0: Not a match.
85558
+** 1: UTF8/16 conversion required and function takes any number of arguments.
85559
+** 2: UTF16 byte order change required and function takes any number of args.
85560
+** 3: encoding matches and function takes any number of arguments
85561
+** 4: UTF8/16 conversion required - argument count matches exactly
85562
+** 5: UTF16 byte order conversion required - argument count matches exactly
85563
+** 6: Perfect match: encoding and argument count match exactly.
8541885564
**
85565
+** If nArg==(-2) then any function with a non-null xStep or xFunc is
85566
+** a perfect match and any function with both xStep and xFunc NULL is
85567
+** a non-match.
8541985568
*/
85420
-static int matchQuality(FuncDef *p, int nArg, u8 enc){
85421
- int match = 0;
85422
- if( p->nArg==-1 || p->nArg==nArg
85423
- || (nArg==-1 && (p->xFunc!=0 || p->xStep!=0))
85424
- ){
85569
+#define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */
85570
+static int matchQuality(
85571
+ FuncDef *p, /* The function we are evaluating for match quality */
85572
+ int nArg, /* Desired number of arguments. (-1)==any */
85573
+ u8 enc /* Desired text encoding */
85574
+){
85575
+ int match;
85576
+
85577
+ /* nArg of -2 is a special case */
85578
+ if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH;
85579
+
85580
+ /* Wrong number of arguments means "no match" */
85581
+ if( p->nArg!=nArg && p->nArg>=0 ) return 0;
85582
+
85583
+ /* Give a better score to a function with a specific number of arguments
85584
+ ** than to function that accepts any number of arguments. */
85585
+ if( p->nArg==nArg ){
85586
+ match = 4;
85587
+ }else{
8542585588
match = 1;
85426
- if( p->nArg==nArg || nArg==-1 ){
85427
- match = 4;
85428
- }
85429
- if( enc==p->iPrefEnc ){
85430
- match += 2;
85431
- }
85432
- else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
85433
- (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
85434
- match += 1;
85435
- }
85436
- }
85589
+ }
85590
+
85591
+ /* Bonus points if the text encoding matches */
85592
+ if( enc==p->iPrefEnc ){
85593
+ match += 2; /* Exact encoding match */
85594
+ }else if( (enc & p->iPrefEnc & 2)!=0 ){
85595
+ match += 1; /* Both are UTF16, but with different byte orders */
85596
+ }
85597
+
8543785598
return match;
8543885599
}
8543985600
8544085601
/*
8544185602
** Search a FuncDefHash for a function with the given name. Return
@@ -85487,17 +85648,16 @@
8548785648
** pointer to the FuncDef structure that defines that function, or return
8548885649
** NULL if the function does not exist.
8548985650
**
8549085651
** If the createFlag argument is true, then a new (blank) FuncDef
8549185652
** structure is created and liked into the "db" structure if a
85492
-** no matching function previously existed. When createFlag is true
85493
-** and the nArg parameter is -1, then only a function that accepts
85494
-** any number of arguments will be returned.
85653
+** no matching function previously existed.
8549585654
**
85496
-** If createFlag is false and nArg is -1, then the first valid
85497
-** function found is returned. A function is valid if either xFunc
85498
-** or xStep is non-zero.
85655
+** If nArg is -2, then the first valid function found is returned. A
85656
+** function is valid if either xFunc or xStep is non-zero. The nArg==(-2)
85657
+** case is used to see if zName is a valid function name for some number
85658
+** of arguments. If nArg is -2, then createFlag must be 0.
8549985659
**
8550085660
** If createFlag is false, then a function with the required name and
8550185661
** number of arguments may be returned even if the eTextRep flag does not
8550285662
** match that requested.
8550385663
*/
@@ -85505,18 +85665,19 @@
8550585665
sqlite3 *db, /* An open database */
8550685666
const char *zName, /* Name of the function. Not null-terminated */
8550785667
int nName, /* Number of characters in the name */
8550885668
int nArg, /* Number of arguments. -1 means any number */
8550985669
u8 enc, /* Preferred text encoding */
85510
- int createFlag /* Create new entry if true and does not otherwise exist */
85670
+ u8 createFlag /* Create new entry if true and does not otherwise exist */
8551185671
){
8551285672
FuncDef *p; /* Iterator variable */
8551385673
FuncDef *pBest = 0; /* Best match found so far */
8551485674
int bestScore = 0; /* Score of best match */
8551585675
int h; /* Hash value */
8551685676
85517
-
85677
+ assert( nArg>=(-2) );
85678
+ assert( nArg>=(-1) || createFlag==0 );
8551885679
assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
8551985680
h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
8552085681
8552185682
/* First search for a match amongst the application-defined functions.
8552285683
*/
@@ -85558,11 +85719,11 @@
8555885719
8555985720
/* If the createFlag parameter is true and the search did not reveal an
8556085721
** exact match for the name, number of arguments and encoding, then add a
8556185722
** new entry to the hash table and return it.
8556285723
*/
85563
- if( createFlag && (bestScore<6 || pBest->nArg!=nArg) &&
85724
+ if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
8556485725
(pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
8556585726
pBest->zName = (char *)&pBest[1];
8556685727
pBest->nArg = (u16)nArg;
8556785728
pBest->iPrefEnc = enc;
8556885729
memcpy(pBest->zName, zName, nName);
@@ -97526,11 +97687,12 @@
9752697687
9752797688
/***** If we reach this point, flattening is permitted. *****/
9752897689
9752997690
/* Authorize the subquery */
9753097691
pParse->zAuthContext = pSubitem->zName;
97531
- sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
97692
+ TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
97693
+ testcase( i==SQLITE_DENY );
9753297694
pParse->zAuthContext = zSavedAuthContext;
9753397695
9753497696
/* If the sub-query is a compound SELECT statement, then (by restrictions
9753597697
** 17 and 18 above) it must be a UNION ALL and the parent query must
9753697698
** be of the form:
@@ -129049,11 +129211,10 @@
129049129211
/* Advance to the next output block */
129050129212
pLeaf->iBlock++;
129051129213
pLeaf->key.n = 0;
129052129214
pLeaf->block.n = 0;
129053129215
129054
- nPrefix = 0;
129055129216
nSuffix = nTerm;
129056129217
nSpace = 1;
129057129218
nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
129058129219
nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
129059129220
}
@@ -132384,10 +132545,23 @@
132384132545
132385132546
/* Possible values for eCoordType: */
132386132547
#define RTREE_COORD_REAL32 0
132387132548
#define RTREE_COORD_INT32 1
132388132549
132550
+/*
132551
+** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
132552
+** only deal with integer coordinates. No floating point operations
132553
+** will be done.
132554
+*/
132555
+#ifdef SQLITE_RTREE_INT_ONLY
132556
+ typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
132557
+ typedef int RtreeValue; /* Low accuracy coordinate */
132558
+#else
132559
+ typedef double RtreeDValue; /* High accuracy coordinate */
132560
+ typedef float RtreeValue; /* Low accuracy coordinate */
132561
+#endif
132562
+
132389132563
/*
132390132564
** The minimum number of cells allowed for a node is a third of the
132391132565
** maximum. In Gutman's notation:
132392132566
**
132393132567
** m = M/3
@@ -132419,33 +132593,38 @@
132419132593
int nConstraint; /* Number of entries in aConstraint */
132420132594
RtreeConstraint *aConstraint; /* Search constraints. */
132421132595
};
132422132596
132423132597
union RtreeCoord {
132424
- float f;
132598
+ RtreeValue f;
132425132599
int i;
132426132600
};
132427132601
132428132602
/*
132429132603
** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
132430
-** formatted as a double. This macro assumes that local variable pRtree points
132431
-** to the Rtree structure associated with the RtreeCoord.
132604
+** formatted as a RtreeDValue (double or int64). This macro assumes that local
132605
+** variable pRtree points to the Rtree structure associated with the
132606
+** RtreeCoord.
132432132607
*/
132433
-#define DCOORD(coord) ( \
132434
- (pRtree->eCoordType==RTREE_COORD_REAL32) ? \
132435
- ((double)coord.f) : \
132436
- ((double)coord.i) \
132437
-)
132608
+#ifdef SQLITE_RTREE_INT_ONLY
132609
+# define DCOORD(coord) ((RtreeDValue)coord.i)
132610
+#else
132611
+# define DCOORD(coord) ( \
132612
+ (pRtree->eCoordType==RTREE_COORD_REAL32) ? \
132613
+ ((double)coord.f) : \
132614
+ ((double)coord.i) \
132615
+ )
132616
+#endif
132438132617
132439132618
/*
132440132619
** A search constraint.
132441132620
*/
132442132621
struct RtreeConstraint {
132443132622
int iCoord; /* Index of constrained coordinate */
132444132623
int op; /* Constraining operation */
132445
- double rValue; /* Constraint value. */
132446
- int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
132624
+ RtreeDValue rValue; /* Constraint value. */
132625
+ int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
132447132626
sqlite3_rtree_geometry *pGeom; /* Constraint callback argument for a MATCH */
132448132627
};
132449132628
132450132629
/* Possible values for RtreeConstraint.op */
132451132630
#define RTREE_EQ 0x41
@@ -132489,14 +132668,14 @@
132489132668
** the right-hand-side of an SQL MATCH operator used to constrain an
132490132669
** r-tree query.
132491132670
*/
132492132671
struct RtreeMatchArg {
132493132672
u32 magic; /* Always RTREE_GEOMETRY_MAGIC */
132494
- int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
132673
+ int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue*, int *);
132495132674
void *pContext;
132496132675
int nParam;
132497
- double aParam[1];
132676
+ RtreeDValue aParam[1];
132498132677
};
132499132678
132500132679
/*
132501132680
** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
132502132681
** a single instance of the following structure is allocated. It is used
@@ -132504,11 +132683,11 @@
132504132683
** is eventually deleted by the destructor mechanism provided by
132505132684
** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
132506132685
** the geometry callback function).
132507132686
*/
132508132687
struct RtreeGeomCallback {
132509
- int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
132688
+ int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
132510132689
void *pContext;
132511132690
};
132512132691
132513132692
#ifndef MAX
132514132693
# define MAX(x,y) ((x) < (y) ? (y) : (x))
@@ -133070,11 +133249,11 @@
133070133249
RtreeConstraint *pConstraint, /* MATCH constraint to test */
133071133250
RtreeCell *pCell, /* Cell to test */
133072133251
int *pbRes /* OUT: Test result */
133073133252
){
133074133253
int i;
133075
- double aCoord[RTREE_MAX_DIMENSIONS*2];
133254
+ RtreeDValue aCoord[RTREE_MAX_DIMENSIONS*2];
133076133255
int nCoord = pRtree->nDim*2;
133077133256
133078133257
assert( pConstraint->op==RTREE_MATCH );
133079133258
assert( pConstraint->pGeom );
133080133259
@@ -133100,12 +133279,12 @@
133100133279
int rc = SQLITE_OK;
133101133280
133102133281
nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
133103133282
for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
133104133283
RtreeConstraint *p = &pCursor->aConstraint[ii];
133105
- double cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
133106
- double cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
133284
+ RtreeDValue cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
133285
+ RtreeDValue cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
133107133286
133108133287
assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
133109133288
|| p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
133110133289
);
133111133290
@@ -133153,11 +133332,11 @@
133153133332
*pbEof = 0;
133154133333
133155133334
nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
133156133335
for(ii=0; ii<pCursor->nConstraint; ii++){
133157133336
RtreeConstraint *p = &pCursor->aConstraint[ii];
133158
- double coord = DCOORD(cell.aCoord[p->iCoord]);
133337
+ RtreeDValue coord = DCOORD(cell.aCoord[p->iCoord]);
133159133338
int res;
133160133339
assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
133161133340
|| p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
133162133341
);
133163133342
switch( p->op ){
@@ -133351,13 +133530,16 @@
133351133530
i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
133352133531
sqlite3_result_int64(ctx, iRowid);
133353133532
}else{
133354133533
RtreeCoord c;
133355133534
nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
133535
+#ifndef SQLITE_RTREE_INT_ONLY
133356133536
if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
133357133537
sqlite3_result_double(ctx, c.f);
133358
- }else{
133538
+ }else
133539
+#endif
133540
+ {
133359133541
assert( pRtree->eCoordType==RTREE_COORD_INT32 );
133360133542
sqlite3_result_int(ctx, c.i);
133361133543
}
133362133544
}
133363133545
@@ -133400,11 +133582,11 @@
133400133582
if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
133401133583
133402133584
/* Check that the blob is roughly the right size. */
133403133585
nBlob = sqlite3_value_bytes(pValue);
133404133586
if( nBlob<(int)sizeof(RtreeMatchArg)
133405
- || ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0
133587
+ || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
133406133588
){
133407133589
return SQLITE_ERROR;
133408133590
}
133409133591
133410133592
pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
@@ -133414,11 +133596,11 @@
133414133596
memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
133415133597
p = (RtreeMatchArg *)&pGeom[1];
133416133598
133417133599
memcpy(p, sqlite3_value_blob(pValue), nBlob);
133418133600
if( p->magic!=RTREE_GEOMETRY_MAGIC
133419
- || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(double))
133601
+ || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(RtreeDValue))
133420133602
){
133421133603
sqlite3_free(pGeom);
133422133604
return SQLITE_ERROR;
133423133605
}
133424133606
@@ -133486,11 +133668,15 @@
133486133668
rc = deserializeGeometry(argv[ii], p);
133487133669
if( rc!=SQLITE_OK ){
133488133670
break;
133489133671
}
133490133672
}else{
133673
+#ifdef SQLITE_RTREE_INT_ONLY
133674
+ p->rValue = sqlite3_value_int64(argv[ii]);
133675
+#else
133491133676
p->rValue = sqlite3_value_double(argv[ii]);
133677
+#endif
133492133678
}
133493133679
}
133494133680
}
133495133681
}
133496133682
@@ -133620,28 +133806,28 @@
133620133806
}
133621133807
133622133808
/*
133623133809
** Return the N-dimensional volumn of the cell stored in *p.
133624133810
*/
133625
-static float cellArea(Rtree *pRtree, RtreeCell *p){
133626
- float area = 1.0;
133811
+static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
133812
+ RtreeDValue area = (RtreeDValue)1;
133627133813
int ii;
133628133814
for(ii=0; ii<(pRtree->nDim*2); ii+=2){
133629
- area = (float)(area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
133815
+ area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
133630133816
}
133631133817
return area;
133632133818
}
133633133819
133634133820
/*
133635133821
** Return the margin length of cell p. The margin length is the sum
133636133822
** of the objects size in each dimension.
133637133823
*/
133638
-static float cellMargin(Rtree *pRtree, RtreeCell *p){
133639
- float margin = 0.0;
133824
+static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
133825
+ RtreeDValue margin = (RtreeDValue)0;
133640133826
int ii;
133641133827
for(ii=0; ii<(pRtree->nDim*2); ii+=2){
133642
- margin += (float)(DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
133828
+ margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
133643133829
}
133644133830
return margin;
133645133831
}
133646133832
133647133833
/*
@@ -133682,51 +133868,50 @@
133682133868
}
133683133869
133684133870
/*
133685133871
** Return the amount cell p would grow by if it were unioned with pCell.
133686133872
*/
133687
-static float cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
133688
- float area;
133873
+static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
133874
+ RtreeDValue area;
133689133875
RtreeCell cell;
133690133876
memcpy(&cell, p, sizeof(RtreeCell));
133691133877
area = cellArea(pRtree, &cell);
133692133878
cellUnion(pRtree, &cell, pCell);
133693133879
return (cellArea(pRtree, &cell)-area);
133694133880
}
133695133881
133696133882
#if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
133697
-static float cellOverlap(
133883
+static RtreeDValue cellOverlap(
133698133884
Rtree *pRtree,
133699133885
RtreeCell *p,
133700133886
RtreeCell *aCell,
133701133887
int nCell,
133702133888
int iExclude
133703133889
){
133704133890
int ii;
133705
- float overlap = 0.0;
133891
+ RtreeDValue overlap = 0.0;
133706133892
for(ii=0; ii<nCell; ii++){
133707133893
#if VARIANT_RSTARTREE_CHOOSESUBTREE
133708133894
if( ii!=iExclude )
133709133895
#else
133710133896
assert( iExclude==-1 );
133711133897
UNUSED_PARAMETER(iExclude);
133712133898
#endif
133713133899
{
133714133900
int jj;
133715
- float o = 1.0;
133901
+ RtreeDValue o = (RtreeDValue)1;
133716133902
for(jj=0; jj<(pRtree->nDim*2); jj+=2){
133717
- double x1;
133718
- double x2;
133903
+ RtreeDValue x1, x2;
133719133904
133720133905
x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
133721133906
x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
133722133907
133723133908
if( x2<x1 ){
133724133909
o = 0.0;
133725133910
break;
133726133911
}else{
133727
- o = o * (float)(x2-x1);
133912
+ o = o * (x2-x1);
133728133913
}
133729133914
}
133730133915
overlap += o;
133731133916
}
133732133917
}
@@ -133733,24 +133918,23 @@
133733133918
return overlap;
133734133919
}
133735133920
#endif
133736133921
133737133922
#if VARIANT_RSTARTREE_CHOOSESUBTREE
133738
-static float cellOverlapEnlargement(
133923
+static RtreeDValue cellOverlapEnlargement(
133739133924
Rtree *pRtree,
133740133925
RtreeCell *p,
133741133926
RtreeCell *pInsert,
133742133927
RtreeCell *aCell,
133743133928
int nCell,
133744133929
int iExclude
133745133930
){
133746
- double before;
133747
- double after;
133931
+ RtreeDValue before, after;
133748133932
before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
133749133933
cellUnion(pRtree, p, pInsert);
133750133934
after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
133751
- return (float)(after-before);
133935
+ return (after-before);
133752133936
}
133753133937
#endif
133754133938
133755133939
133756133940
/*
@@ -133770,15 +133954,15 @@
133770133954
133771133955
for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
133772133956
int iCell;
133773133957
sqlite3_int64 iBest = 0;
133774133958
133775
- float fMinGrowth = 0.0;
133776
- float fMinArea = 0.0;
133959
+ RtreeDValue fMinGrowth = 0.0;
133960
+ RtreeDValue fMinArea = 0.0;
133777133961
#if VARIANT_RSTARTREE_CHOOSESUBTREE
133778
- float fMinOverlap = 0.0;
133779
- float overlap;
133962
+ RtreeDValue fMinOverlap = 0.0;
133963
+ RtreeDValue overlap;
133780133964
#endif
133781133965
133782133966
int nCell = NCELL(pNode);
133783133967
RtreeCell cell;
133784133968
RtreeNode *pChild;
@@ -133805,12 +133989,12 @@
133805133989
** is inserted into it. Resolve ties by choosing the entry with
133806133990
** the smallest area.
133807133991
*/
133808133992
for(iCell=0; iCell<nCell; iCell++){
133809133993
int bBest = 0;
133810
- float growth;
133811
- float area;
133994
+ RtreeDValue growth;
133995
+ RtreeDValue area;
133812133996
nodeGetCell(pRtree, pNode, iCell, &cell);
133813133997
growth = cellGrowth(pRtree, &cell, pCell);
133814133998
area = cellArea(pRtree, &cell);
133815133999
133816134000
#if VARIANT_RSTARTREE_CHOOSESUBTREE
@@ -133933,30 +134117,30 @@
133933134117
int *piRightSeed
133934134118
){
133935134119
int i;
133936134120
int iLeftSeed = 0;
133937134121
int iRightSeed = 1;
133938
- float maxNormalInnerWidth = 0.0;
134122
+ RtreeDValue maxNormalInnerWidth = (RtreeDValue)0;
133939134123
133940134124
/* Pick two "seed" cells from the array of cells. The algorithm used
133941134125
** here is the LinearPickSeeds algorithm from Gutman[1984]. The
133942134126
** indices of the two seed cells in the array are stored in local
133943134127
** variables iLeftSeek and iRightSeed.
133944134128
*/
133945134129
for(i=0; i<pRtree->nDim; i++){
133946
- float x1 = DCOORD(aCell[0].aCoord[i*2]);
133947
- float x2 = DCOORD(aCell[0].aCoord[i*2+1]);
133948
- float x3 = x1;
133949
- float x4 = x2;
134130
+ RtreeDValue x1 = DCOORD(aCell[0].aCoord[i*2]);
134131
+ RtreeDValue x2 = DCOORD(aCell[0].aCoord[i*2+1]);
134132
+ RtreeDValue x3 = x1;
134133
+ RtreeDValue x4 = x2;
133950134134
int jj;
133951134135
133952134136
int iCellLeft = 0;
133953134137
int iCellRight = 0;
133954134138
133955134139
for(jj=1; jj<nCell; jj++){
133956
- float left = DCOORD(aCell[jj].aCoord[i*2]);
133957
- float right = DCOORD(aCell[jj].aCoord[i*2+1]);
134140
+ RtreeDValue left = DCOORD(aCell[jj].aCoord[i*2]);
134141
+ RtreeDValue right = DCOORD(aCell[jj].aCoord[i*2+1]);
133958134142
133959134143
if( left<x1 ) x1 = left;
133960134144
if( right>x4 ) x4 = right;
133961134145
if( left>x3 ){
133962134146
x3 = left;
@@ -133967,11 +134151,11 @@
133967134151
iCellLeft = jj;
133968134152
}
133969134153
}
133970134154
133971134155
if( x4!=x1 ){
133972
- float normalwidth = (x3 - x2) / (x4 - x1);
134156
+ RtreeDValue normalwidth = (x3 - x2) / (x4 - x1);
133973134157
if( normalwidth>maxNormalInnerWidth ){
133974134158
iLeftSeed = iCellLeft;
133975134159
iRightSeed = iCellRight;
133976134160
}
133977134161
}
@@ -133996,17 +134180,17 @@
133996134180
int *aiUsed
133997134181
){
133998134182
#define FABS(a) ((a)<0.0?-1.0*(a):(a))
133999134183
134000134184
int iSelect = -1;
134001
- float fDiff;
134185
+ RtreeDValue fDiff;
134002134186
int ii;
134003134187
for(ii=0; ii<nCell; ii++){
134004134188
if( aiUsed[ii]==0 ){
134005
- float left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
134006
- float right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
134007
- float diff = FABS(right-left);
134189
+ RtreeDValue left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
134190
+ RtreeDValue right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
134191
+ RtreeDValue diff = FABS(right-left);
134008134192
if( iSelect<0 || diff>fDiff ){
134009134193
fDiff = diff;
134010134194
iSelect = ii;
134011134195
}
134012134196
}
@@ -134029,17 +134213,17 @@
134029134213
int ii;
134030134214
int jj;
134031134215
134032134216
int iLeftSeed = 0;
134033134217
int iRightSeed = 1;
134034
- float fWaste = 0.0;
134218
+ RtreeDValue fWaste = 0.0;
134035134219
134036134220
for(ii=0; ii<nCell; ii++){
134037134221
for(jj=ii+1; jj<nCell; jj++){
134038
- float right = cellArea(pRtree, &aCell[jj]);
134039
- float growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
134040
- float waste = growth - right;
134222
+ RtreeDValue right = cellArea(pRtree, &aCell[jj]);
134223
+ RtreeDValue growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
134224
+ RtreeDValue waste = growth - right;
134041134225
134042134226
if( waste>fWaste ){
134043134227
iLeftSeed = ii;
134044134228
iRightSeed = jj;
134045134229
fWaste = waste;
@@ -134070,11 +134254,11 @@
134070134254
** sorting algorithm.
134071134255
*/
134072134256
static void SortByDistance(
134073134257
int *aIdx,
134074134258
int nIdx,
134075
- float *aDistance,
134259
+ RtreeDValue *aDistance,
134076134260
int *aSpare
134077134261
){
134078134262
if( nIdx>1 ){
134079134263
int iLeft = 0;
134080134264
int iRight = 0;
@@ -134096,12 +134280,12 @@
134096134280
iRight++;
134097134281
}else if( iRight==nRight ){
134098134282
aIdx[iLeft+iRight] = aLeft[iLeft];
134099134283
iLeft++;
134100134284
}else{
134101
- float fLeft = aDistance[aLeft[iLeft]];
134102
- float fRight = aDistance[aRight[iRight]];
134285
+ RtreeDValue fLeft = aDistance[aLeft[iLeft]];
134286
+ RtreeDValue fRight = aDistance[aRight[iRight]];
134103134287
if( fLeft<fRight ){
134104134288
aIdx[iLeft+iRight] = aLeft[iLeft];
134105134289
iLeft++;
134106134290
}else{
134107134291
aIdx[iLeft+iRight] = aRight[iRight];
@@ -134113,12 +134297,12 @@
134113134297
#if 0
134114134298
/* Check that the sort worked */
134115134299
{
134116134300
int jj;
134117134301
for(jj=1; jj<nIdx; jj++){
134118
- float left = aDistance[aIdx[jj-1]];
134119
- float right = aDistance[aIdx[jj]];
134302
+ RtreeDValue left = aDistance[aIdx[jj-1]];
134303
+ RtreeDValue right = aDistance[aIdx[jj]];
134120134304
assert( left<=right );
134121134305
}
134122134306
}
134123134307
#endif
134124134308
}
@@ -134157,14 +134341,14 @@
134157134341
SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
134158134342
134159134343
memcpy(aSpare, aLeft, sizeof(int)*nLeft);
134160134344
aLeft = aSpare;
134161134345
while( iLeft<nLeft || iRight<nRight ){
134162
- double xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
134163
- double xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
134164
- double xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
134165
- double xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
134346
+ RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
134347
+ RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
134348
+ RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
134349
+ RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
134166134350
if( (iLeft!=nLeft) && ((iRight==nRight)
134167134351
|| (xleft1<xright1)
134168134352
|| (xleft1==xright1 && xleft2<xright2)
134169134353
)){
134170134354
aIdx[iLeft+iRight] = aLeft[iLeft];
@@ -134178,14 +134362,14 @@
134178134362
#if 0
134179134363
/* Check that the sort worked */
134180134364
{
134181134365
int jj;
134182134366
for(jj=1; jj<nIdx; jj++){
134183
- float xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
134184
- float xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
134185
- float xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
134186
- float xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
134367
+ RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
134368
+ RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
134369
+ RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
134370
+ RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
134187134371
assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
134188134372
}
134189134373
}
134190134374
#endif
134191134375
}
@@ -134208,11 +134392,11 @@
134208134392
int *aSpare;
134209134393
int ii;
134210134394
134211134395
int iBestDim = 0;
134212134396
int iBestSplit = 0;
134213
- float fBestMargin = 0.0;
134397
+ RtreeDValue fBestMargin = 0.0;
134214134398
134215134399
int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
134216134400
134217134401
aaSorted = (int **)sqlite3_malloc(nByte);
134218134402
if( !aaSorted ){
@@ -134229,13 +134413,13 @@
134229134413
}
134230134414
SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
134231134415
}
134232134416
134233134417
for(ii=0; ii<pRtree->nDim; ii++){
134234
- float margin = 0.0;
134235
- float fBestOverlap = 0.0;
134236
- float fBestArea = 0.0;
134418
+ RtreeDValue margin = 0.0;
134419
+ RtreeDValue fBestOverlap = 0.0;
134420
+ RtreeDValue fBestArea = 0.0;
134237134421
int iBestLeft = 0;
134238134422
int nLeft;
134239134423
134240134424
for(
134241134425
nLeft=RTREE_MINCELLS(pRtree);
@@ -134243,12 +134427,12 @@
134243134427
nLeft++
134244134428
){
134245134429
RtreeCell left;
134246134430
RtreeCell right;
134247134431
int kk;
134248
- float overlap;
134249
- float area;
134432
+ RtreeDValue overlap;
134433
+ RtreeDValue area;
134250134434
134251134435
memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
134252134436
memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
134253134437
for(kk=1; kk<(nCell-1); kk++){
134254134438
if( kk<nLeft ){
@@ -134327,11 +134511,11 @@
134327134511
aiUsed[iRightSeed] = 1;
134328134512
134329134513
for(i=nCell-2; i>0; i--){
134330134514
RtreeCell *pNext;
134331134515
pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
134332
- float diff =
134516
+ RtreeDValue diff =
134333134517
cellGrowth(pRtree, pBboxLeft, pNext) -
134334134518
cellGrowth(pRtree, pBboxRight, pNext)
134335134519
;
134336134520
if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
134337134521
|| (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
@@ -134660,58 +134844,60 @@
134660134844
int iHeight
134661134845
){
134662134846
int *aOrder;
134663134847
int *aSpare;
134664134848
RtreeCell *aCell;
134665
- float *aDistance;
134849
+ RtreeDValue *aDistance;
134666134850
int nCell;
134667
- float aCenterCoord[RTREE_MAX_DIMENSIONS];
134851
+ RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
134668134852
int iDim;
134669134853
int ii;
134670134854
int rc = SQLITE_OK;
134855
+ int n;
134671134856
134672
- memset(aCenterCoord, 0, sizeof(float)*RTREE_MAX_DIMENSIONS);
134857
+ memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
134673134858
134674134859
nCell = NCELL(pNode)+1;
134860
+ n = (nCell+1)&(~1);
134675134861
134676134862
/* Allocate the buffers used by this operation. The allocation is
134677134863
** relinquished before this function returns.
134678134864
*/
134679
- aCell = (RtreeCell *)sqlite3_malloc(nCell * (
134680
- sizeof(RtreeCell) + /* aCell array */
134681
- sizeof(int) + /* aOrder array */
134682
- sizeof(int) + /* aSpare array */
134683
- sizeof(float) /* aDistance array */
134865
+ aCell = (RtreeCell *)sqlite3_malloc(n * (
134866
+ sizeof(RtreeCell) + /* aCell array */
134867
+ sizeof(int) + /* aOrder array */
134868
+ sizeof(int) + /* aSpare array */
134869
+ sizeof(RtreeDValue) /* aDistance array */
134684134870
));
134685134871
if( !aCell ){
134686134872
return SQLITE_NOMEM;
134687134873
}
134688
- aOrder = (int *)&aCell[nCell];
134689
- aSpare = (int *)&aOrder[nCell];
134690
- aDistance = (float *)&aSpare[nCell];
134874
+ aOrder = (int *)&aCell[n];
134875
+ aSpare = (int *)&aOrder[n];
134876
+ aDistance = (RtreeDValue *)&aSpare[n];
134691134877
134692134878
for(ii=0; ii<nCell; ii++){
134693134879
if( ii==(nCell-1) ){
134694134880
memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
134695134881
}else{
134696134882
nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
134697134883
}
134698134884
aOrder[ii] = ii;
134699134885
for(iDim=0; iDim<pRtree->nDim; iDim++){
134700
- aCenterCoord[iDim] += (float)DCOORD(aCell[ii].aCoord[iDim*2]);
134701
- aCenterCoord[iDim] += (float)DCOORD(aCell[ii].aCoord[iDim*2+1]);
134886
+ aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
134887
+ aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
134702134888
}
134703134889
}
134704134890
for(iDim=0; iDim<pRtree->nDim; iDim++){
134705
- aCenterCoord[iDim] = (float)(aCenterCoord[iDim]/((float)nCell*2.0));
134891
+ aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
134706134892
}
134707134893
134708134894
for(ii=0; ii<nCell; ii++){
134709134895
aDistance[ii] = 0.0;
134710134896
for(iDim=0; iDim<pRtree->nDim; iDim++){
134711
- float coord = (float)(DCOORD(aCell[ii].aCoord[iDim*2+1]) -
134712
- DCOORD(aCell[ii].aCoord[iDim*2]));
134897
+ RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
134898
+ DCOORD(aCell[ii].aCoord[iDim*2]));
134713134899
aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
134714134900
}
134715134901
}
134716134902
134717134903
SortByDistance(aOrder, nCell, aDistance, aSpare);
@@ -134949,20 +135135,23 @@
134949135135
if( nData>1 ){
134950135136
int ii;
134951135137
134952135138
/* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
134953135139
assert( nData==(pRtree->nDim*2 + 3) );
135140
+#ifndef SQLITE_RTREE_INT_ONLY
134954135141
if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
134955135142
for(ii=0; ii<(pRtree->nDim*2); ii+=2){
134956
- cell.aCoord[ii].f = (float)sqlite3_value_double(azData[ii+3]);
134957
- cell.aCoord[ii+1].f = (float)sqlite3_value_double(azData[ii+4]);
135143
+ cell.aCoord[ii].f = (RtreeValue)sqlite3_value_double(azData[ii+3]);
135144
+ cell.aCoord[ii+1].f = (RtreeValue)sqlite3_value_double(azData[ii+4]);
134958135145
if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
134959135146
rc = SQLITE_CONSTRAINT;
134960135147
goto constraint;
134961135148
}
134962135149
}
134963
- }else{
135150
+ }else
135151
+#endif
135152
+ {
134964135153
for(ii=0; ii<(pRtree->nDim*2); ii+=2){
134965135154
cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
134966135155
cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
134967135156
if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
134968135157
rc = SQLITE_CONSTRAINT;
@@ -135356,11 +135545,17 @@
135356135545
135357135546
nodeGetCell(&tree, &node, ii, &cell);
135358135547
sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
135359135548
nCell = (int)strlen(zCell);
135360135549
for(jj=0; jj<tree.nDim*2; jj++){
135361
- sqlite3_snprintf(512-nCell,&zCell[nCell]," %f",(double)cell.aCoord[jj].f);
135550
+#ifndef SQLITE_RTREE_INT_ONLY
135551
+ sqlite3_snprintf(512-nCell,&zCell[nCell], " %f",
135552
+ (double)cell.aCoord[jj].f);
135553
+#else
135554
+ sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
135555
+ cell.aCoord[jj].i);
135556
+#endif
135362135557
nCell = (int)strlen(zCell);
135363135558
}
135364135559
135365135560
if( zText ){
135366135561
char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
@@ -135398,11 +135593,15 @@
135398135593
rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
135399135594
if( rc==SQLITE_OK ){
135400135595
rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
135401135596
}
135402135597
if( rc==SQLITE_OK ){
135598
+#ifdef SQLITE_RTREE_INT_ONLY
135599
+ void *c = (void *)RTREE_COORD_INT32;
135600
+#else
135403135601
void *c = (void *)RTREE_COORD_REAL32;
135602
+#endif
135404135603
rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
135405135604
}
135406135605
if( rc==SQLITE_OK ){
135407135606
void *c = (void *)RTREE_COORD_INT32;
135408135607
rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
@@ -135432,11 +135631,11 @@
135432135631
static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
135433135632
RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
135434135633
RtreeMatchArg *pBlob;
135435135634
int nBlob;
135436135635
135437
- nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(double);
135636
+ nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue);
135438135637
pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
135439135638
if( !pBlob ){
135440135639
sqlite3_result_error_nomem(ctx);
135441135640
}else{
135442135641
int i;
@@ -135443,11 +135642,15 @@
135443135642
pBlob->magic = RTREE_GEOMETRY_MAGIC;
135444135643
pBlob->xGeom = pGeomCtx->xGeom;
135445135644
pBlob->pContext = pGeomCtx->pContext;
135446135645
pBlob->nParam = nArg;
135447135646
for(i=0; i<nArg; i++){
135647
+#ifdef SQLITE_RTREE_INT_ONLY
135648
+ pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
135649
+#else
135448135650
pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
135651
+#endif
135449135652
}
135450135653
sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
135451135654
}
135452135655
}
135453135656
@@ -135455,11 +135658,11 @@
135455135658
** Register a new geometry function for use with the r-tree MATCH operator.
135456135659
*/
135457135660
SQLITE_API int sqlite3_rtree_geometry_callback(
135458135661
sqlite3 *db,
135459135662
const char *zGeom,
135460
- int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *),
135663
+ int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue *, int *),
135461135664
void *pContext
135462135665
){
135463135666
RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
135464135667
135465135668
/* Allocate and populate the context object. */
135466135669
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -657,11 +657,11 @@
657 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
658 ** [sqlite_version()] and [sqlite_source_id()].
659 */
660 #define SQLITE_VERSION "3.7.12"
661 #define SQLITE_VERSION_NUMBER 3007012
662 #define SQLITE_SOURCE_ID "2012-03-31 19:12:23 af602d87736b52802a4e760ffeeaa28112b99d9a"
663
664 /*
665 ** CAPI3REF: Run-Time Library Version Numbers
666 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
667 **
@@ -2095,11 +2095,11 @@
2095 ** database connection is opened. By default, URI handling is globally
2096 ** disabled. The default value may be changed by compiling with the
2097 ** [SQLITE_USE_URI] symbol defined.
2098 **
2099 ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2100 ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFNIG_GETPCACHE
2101 ** <dd> These options are obsolete and should not be used by new code.
2102 ** They are retained for backwards compatibility but are now no-ops.
2103 ** </dl>
2104 */
2105 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
@@ -7533,11 +7533,15 @@
7533 ** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
7534 */
7535 SQLITE_API int sqlite3_rtree_geometry_callback(
7536 sqlite3 *db,
7537 const char *zGeom,
7538 int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes),
 
 
 
 
7539 void *pContext
7540 );
7541
7542
7543 /*
@@ -10933,11 +10937,10 @@
10933 int nRef; /* Number of names resolved by this context */
10934 int nErr; /* Number of errors encountered while resolving names */
10935 u8 allowAgg; /* Aggregate functions allowed here */
10936 u8 hasAgg; /* True if aggregates are seen */
10937 u8 isCheck; /* True if resolving names in a CHECK constraint */
10938 int nDepth; /* Depth of subquery recursion. 1 for no recursion */
10939 AggInfo *pAggInfo; /* Information about aggregates at this level */
10940 NameContext *pNext; /* Next outer name context. NULL for outermost */
10941 };
10942
10943 /*
@@ -11399,10 +11402,11 @@
11399 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
11400 Parse *pParse; /* Parser context. */
11401 union { /* Extra data for callback */
11402 NameContext *pNC; /* Naming context */
11403 int i; /* Integer value */
 
11404 } u;
11405 };
11406
11407 /* Forward declarations */
11408 SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
@@ -11767,11 +11771,11 @@
11767 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
11768 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
11769 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
11770 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
11771 SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
11772 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
11773 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
11774 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
11775 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
11776 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
11777 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
@@ -24995,12 +24999,12 @@
24995 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
24996 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
24997 #endif
24998
24999 /*
25000 ** Default permissions when creating auto proxy dir
25001 */
25002 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
25003 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
25004 #endif
25005
25006 /*
@@ -25550,11 +25554,11 @@
25550 return 0;
25551 }
25552
25553 /*
25554 ** Invoke open(). Do so multiple times, until it either succeeds or
25555 ** files for some reason other than EINTR.
25556 **
25557 ** If the file creation mode "m" is 0 then set it to the default for
25558 ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
25559 ** 0644) as modified by the system umask. If m is not 0, then
25560 ** make the file creation mode be exactly m ignoring the umask.
@@ -25566,24 +25570,33 @@
25566 ** transaction crashes and leaves behind hot journals, then any
25567 ** process that is able to write to the database will also be able to
25568 ** recover the hot journals.
25569 */
25570 static int robust_open(const char *z, int f, mode_t m){
25571 int rc;
25572 mode_t m2;
25573 mode_t origM = 0;
25574 if( m==0 ){
25575 m2 = SQLITE_DEFAULT_FILE_PERMISSIONS;
25576 }else{
25577 m2 = m;
25578 origM = osUmask(0);
25579 }
25580 do{ rc = osOpen(z,f,m2); }while( rc<0 && errno==EINTR );
 
 
 
 
 
 
25581 if( m ){
25582 osUmask(origM);
25583 }
25584 return rc;
 
 
 
25585 }
25586
25587 /*
25588 ** Helper functions to obtain and relinquish the global mutex. The
25589 ** global mutex is used to protect the unixInodeInfo and
@@ -28374,13 +28387,10 @@
28374 for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
28375 if( ii>0 ){
28376 zDirname[ii] = '\0';
28377 fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
28378 if( fd>=0 ){
28379 #ifdef FD_CLOEXEC
28380 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
28381 #endif
28382 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
28383 }
28384 }
28385 *pFd = fd;
28386 return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
@@ -28459,11 +28469,11 @@
28459 /* If the user has configured a chunk-size for this file, truncate the
28460 ** file so that it consists of an integer number of chunks (i.e. the
28461 ** actual file size after the operation may be larger than the requested
28462 ** size).
28463 */
28464 if( pFile->szChunk ){
28465 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
28466 }
28467
28468 rc = robust_ftruncate(pFile->h, (off_t)nByte);
28469 if( rc ){
@@ -30221,14 +30231,10 @@
30221 else{
30222 p->openFlags = openFlags;
30223 }
30224 #endif
30225
30226 #ifdef FD_CLOEXEC
30227 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
30228 #endif
30229
30230 noLock = eType!=SQLITE_OPEN_MAIN_DB;
30231
30232
30233 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
30234 if( fstatfs(fd, &fsInfo) == -1 ){
@@ -38001,10 +38007,15 @@
38001 #define ROWSET_ENTRY_PER_CHUNK \
38002 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
38003
38004 /*
38005 ** Each entry in a RowSet is an instance of the following object.
 
 
 
 
 
38006 */
38007 struct RowSetEntry {
38008 i64 v; /* ROWID value for this entry */
38009 struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
38010 struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
@@ -38030,16 +38041,22 @@
38030 struct RowSetChunk *pChunk; /* List of all chunk allocations */
38031 sqlite3 *db; /* The database connection */
38032 struct RowSetEntry *pEntry; /* List of entries using pRight */
38033 struct RowSetEntry *pLast; /* Last entry on the pEntry list */
38034 struct RowSetEntry *pFresh; /* Source of new entry objects */
38035 struct RowSetEntry *pTree; /* Binary tree of entries */
38036 u16 nFresh; /* Number of objects on pFresh */
38037 u8 isSorted; /* True if pEntry is sorted */
38038 u8 iBatch; /* Current insert batch */
38039 };
38040
 
 
 
 
 
 
38041 /*
38042 ** Turn bulk memory into a RowSet object. N bytes of memory
38043 ** are available at pSpace. The db pointer is used as a memory context
38044 ** for any subsequent allocations that need to occur.
38045 ** Return a pointer to the new RowSet object.
@@ -38056,14 +38073,14 @@
38056 p = pSpace;
38057 p->pChunk = 0;
38058 p->db = db;
38059 p->pEntry = 0;
38060 p->pLast = 0;
38061 p->pTree = 0;
38062 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
38063 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
38064 p->isSorted = 1;
38065 p->iBatch = 0;
38066 return p;
38067 }
38068
38069 /*
@@ -38079,12 +38096,37 @@
38079 }
38080 p->pChunk = 0;
38081 p->nFresh = 0;
38082 p->pEntry = 0;
38083 p->pLast = 0;
38084 p->pTree = 0;
38085 p->isSorted = 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38086 }
38087
38088 /*
38089 ** Insert a new value into a RowSet.
38090 **
@@ -38092,34 +38134,25 @@
38092 ** memory allocation fails.
38093 */
38094 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
38095 struct RowSetEntry *pEntry; /* The new entry */
38096 struct RowSetEntry *pLast; /* The last prior entry */
38097 assert( p!=0 );
38098 if( p->nFresh==0 ){
38099 struct RowSetChunk *pNew;
38100 pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
38101 if( pNew==0 ){
38102 return;
38103 }
38104 pNew->pNextChunk = p->pChunk;
38105 p->pChunk = pNew;
38106 p->pFresh = pNew->aEntry;
38107 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
38108 }
38109 pEntry = p->pFresh++;
38110 p->nFresh--;
38111 pEntry->v = rowid;
38112 pEntry->pRight = 0;
38113 pLast = p->pLast;
38114 if( pLast ){
38115 if( p->isSorted && rowid<=pLast->v ){
38116 p->isSorted = 0;
38117 }
38118 pLast->pRight = pEntry;
38119 }else{
38120 assert( p->pEntry==0 ); /* Fires if INSERT after SMALLEST */
38121 p->pEntry = pEntry;
38122 }
38123 p->pLast = pEntry;
38124 }
38125
@@ -38127,11 +38160,11 @@
38127 ** Merge two lists of RowSetEntry objects. Remove duplicates.
38128 **
38129 ** The input lists are connected via pRight pointers and are
38130 ** assumed to each already be in sorted order.
38131 */
38132 static struct RowSetEntry *rowSetMerge(
38133 struct RowSetEntry *pA, /* First sorted list to be merged */
38134 struct RowSetEntry *pB /* Second sorted list to be merged */
38135 ){
38136 struct RowSetEntry head;
38137 struct RowSetEntry *pTail;
@@ -38161,36 +38194,33 @@
38161 }
38162 return head.pRight;
38163 }
38164
38165 /*
38166 ** Sort all elements on the pEntry list of the RowSet into ascending order.
 
38167 */
38168 static void rowSetSort(RowSet *p){
38169 unsigned int i;
38170 struct RowSetEntry *pEntry;
38171 struct RowSetEntry *aBucket[40];
38172
38173 assert( p->isSorted==0 );
38174 memset(aBucket, 0, sizeof(aBucket));
38175 while( p->pEntry ){
38176 pEntry = p->pEntry;
38177 p->pEntry = pEntry->pRight;
38178 pEntry->pRight = 0;
38179 for(i=0; aBucket[i]; i++){
38180 pEntry = rowSetMerge(aBucket[i], pEntry);
38181 aBucket[i] = 0;
38182 }
38183 aBucket[i] = pEntry;
 
38184 }
38185 pEntry = 0;
38186 for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
38187 pEntry = rowSetMerge(pEntry, aBucket[i]);
38188 }
38189 p->pEntry = pEntry;
38190 p->pLast = 0;
38191 p->isSorted = 1;
38192 }
38193
38194
38195 /*
38196 ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
@@ -38280,24 +38310,41 @@
38280 }
38281 return p;
38282 }
38283
38284 /*
38285 ** Convert the list in p->pEntry into a sorted list if it is not
38286 ** sorted already. If there is a binary tree on p->pTree, then
38287 ** convert it into a list too and merge it into the p->pEntry list.
 
38288 */
38289 static void rowSetToList(RowSet *p){
38290 if( !p->isSorted ){
38291 rowSetSort(p);
38292 }
38293 if( p->pTree ){
38294 struct RowSetEntry *pHead, *pTail;
38295 rowSetTreeToList(p->pTree, &pHead, &pTail);
38296 p->pTree = 0;
38297 p->pEntry = rowSetMerge(p->pEntry, pHead);
38298 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38299 }
38300
38301 /*
38302 ** Extract the smallest element from the RowSet.
38303 ** Write the element into *pRowid. Return 1 on success. Return
@@ -38305,11 +38352,16 @@
38305 **
38306 ** After this routine has been called, the sqlite3RowSetInsert()
38307 ** routine may not be called again.
38308 */
38309 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
38310 rowSetToList(p);
 
 
 
 
 
38311 if( p->pEntry ){
38312 *pRowid = p->pEntry->v;
38313 p->pEntry = p->pEntry->pRight;
38314 if( p->pEntry==0 ){
38315 sqlite3RowSetClear(p);
@@ -38321,30 +38373,70 @@
38321 }
38322
38323 /*
38324 ** Check to see if element iRowid was inserted into the the rowset as
38325 ** part of any insert batch prior to iBatch. Return 1 or 0.
 
 
 
 
38326 */
38327 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
38328 struct RowSetEntry *p;
 
 
 
 
 
 
38329 if( iBatch!=pRowSet->iBatch ){
38330 if( pRowSet->pEntry ){
38331 rowSetToList(pRowSet);
38332 pRowSet->pTree = rowSetListToTree(pRowSet->pEntry);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38333 pRowSet->pEntry = 0;
38334 pRowSet->pLast = 0;
 
38335 }
38336 pRowSet->iBatch = iBatch;
38337 }
38338 p = pRowSet->pTree;
38339 while( p ){
38340 if( p->v<iRowid ){
38341 p = p->pRight;
38342 }else if( p->v>iRowid ){
38343 p = p->pLeft;
38344 }else{
38345 return 1;
 
 
 
 
 
 
38346 }
38347 }
38348 return 0;
38349 }
38350
@@ -49148,16 +49240,22 @@
49148
49149
49150 /*
49151 ** This structure is passed around through all the sanity checking routines
49152 ** in order to keep track of some global state information.
 
 
 
 
 
 
49153 */
49154 typedef struct IntegrityCk IntegrityCk;
49155 struct IntegrityCk {
49156 BtShared *pBt; /* The tree being checked out */
49157 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
49158 int *anRef; /* Number of times each page is referenced */
49159 Pgno nPage; /* Number of pages in the database */
49160 int mxErr; /* Stop accumulating errors when this reaches zero */
49161 int nErr; /* Number of messages written to zErrMsg so far */
49162 int mallocFailed; /* A memory allocation error has occurred */
49163 StrAccum errMsg; /* Accumulate the error message text here */
@@ -56998,10 +57096,29 @@
56998 }
56999 }
57000 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
57001
57002 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57003 /*
57004 ** Add 1 to the reference count for page iPage. If this is the second
57005 ** reference to the page, add an error message to pCheck->zErrMsg.
57006 ** Return 1 if there are 2 ore more references to the page and 0 if
57007 ** if this is the first reference to the page.
@@ -57012,15 +57129,16 @@
57012 if( iPage==0 ) return 1;
57013 if( iPage>pCheck->nPage ){
57014 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
57015 return 1;
57016 }
57017 if( pCheck->anRef[iPage]==1 ){
57018 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
57019 return 1;
57020 }
57021 return (pCheck->anRef[iPage]++)>1;
 
57022 }
57023
57024 #ifndef SQLITE_OMIT_AUTOVACUUM
57025 /*
57026 ** Check that the entry in the pointer-map for page iChild maps to
@@ -57392,21 +57510,19 @@
57392 *pnErr = 0;
57393 if( sCheck.nPage==0 ){
57394 sqlite3BtreeLeave(p);
57395 return 0;
57396 }
57397 sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
57398 if( !sCheck.anRef ){
 
57399 *pnErr = 1;
57400 sqlite3BtreeLeave(p);
57401 return 0;
57402 }
57403 for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
57404 i = PENDING_BYTE_PAGE(pBt);
57405 if( i<=sCheck.nPage ){
57406 sCheck.anRef[i] = 1;
57407 }
57408 sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
57409 sCheck.errMsg.useMalloc = 2;
57410
57411 /* Check the integrity of the freelist
57412 */
@@ -57427,22 +57543,22 @@
57427
57428 /* Make sure every page in the file is referenced
57429 */
57430 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
57431 #ifdef SQLITE_OMIT_AUTOVACUUM
57432 if( sCheck.anRef[i]==0 ){
57433 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
57434 }
57435 #else
57436 /* If the database supports auto-vacuum, make sure no tables contain
57437 ** references to pointer-map pages.
57438 */
57439 if( sCheck.anRef[i]==0 &&
57440 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
57441 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
57442 }
57443 if( sCheck.anRef[i]!=0 &&
57444 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
57445 checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
57446 }
57447 #endif
57448 }
@@ -57459,11 +57575,11 @@
57459 }
57460
57461 /* Clean up and report errors.
57462 */
57463 sqlite3BtreeLeave(p);
57464 sqlite3_free(sCheck.anRef);
57465 if( sCheck.mallocFailed ){
57466 sqlite3StrAccumReset(&sCheck.errMsg);
57467 *pnErr = sCheck.nErr+1;
57468 return 0;
57469 }
@@ -73807,11 +73923,11 @@
73807 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
73808 zId = pExpr->u.zToken;
73809 nId = sqlite3Strlen30(zId);
73810 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
73811 if( pDef==0 ){
73812 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
73813 if( pDef==0 ){
73814 no_such_func = 1;
73815 }else{
73816 wrong_num_args = 1;
73817 }
@@ -78277,11 +78393,11 @@
78277 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
78278 if( ExprHasProperty(pA, EP_IntValue) ){
78279 if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
78280 return 2;
78281 }
78282 }else if( pA->op!=TK_COLUMN && pA->u.zToken ){
78283 if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
78284 if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
78285 return 2;
78286 }
78287 }
@@ -78313,10 +78429,45 @@
78313 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
78314 if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
78315 }
78316 return 0;
78317 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78318
78319 /*
78320 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
78321 ** the new element. Return a negative number if malloc fails.
78322 */
@@ -78429,13 +78580,11 @@
78429 } /* end loop over pSrcList */
78430 }
78431 return WRC_Prune;
78432 }
78433 case TK_AGG_FUNCTION: {
78434 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
78435 ** to be ignored */
78436 if( pNC->nDepth==0 ){
78437 /* Check to see if pExpr is a duplicate of another aggregate
78438 ** function that is already in the pAggInfo structure
78439 */
78440 struct AggInfo_func *pItem = pAggInfo->aFunc;
78441 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
@@ -78475,19 +78624,11 @@
78475 }
78476 }
78477 return WRC_Continue;
78478 }
78479 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
78480 NameContext *pNC = pWalker->u.pNC;
78481 if( pNC->nDepth==0 ){
78482 pNC->nDepth++;
78483 sqlite3WalkSelect(pWalker, pSelect);
78484 pNC->nDepth--;
78485 return WRC_Prune;
78486 }else{
78487 return WRC_Continue;
78488 }
78489 }
78490
78491 /*
78492 ** Analyze the given expression looking for aggregate functions and
78493 ** for variables that need to be added to the pParse->aAgg[] array.
@@ -78496,10 +78637,11 @@
78496 ** This routine should only be called after the expression has been
78497 ** analyzed by sqlite3ResolveExprNames().
78498 */
78499 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
78500 Walker w;
 
78501 w.xExprCallback = analyzeAggregate;
78502 w.xSelectCallback = analyzeAggregatesInSelect;
78503 w.u.pNC = pNC;
78504 assert( pNC->pSrcList!=0 );
78505 sqlite3WalkExpr(&w, pExpr);
@@ -85399,43 +85541,62 @@
85399 /* During the search for the best function definition, this procedure
85400 ** is called to test how well the function passed as the first argument
85401 ** matches the request for a function with nArg arguments in a system
85402 ** that uses encoding enc. The value returned indicates how well the
85403 ** request is matched. A higher value indicates a better match.
 
 
 
 
 
 
 
 
85404 **
85405 ** The returned value is always between 0 and 6, as follows:
85406 **
85407 ** 0: Not a match, or if nArg<0 and the function is has no implementation.
85408 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
85409 ** encoding is requested, or vice versa.
85410 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
85411 ** requested, or vice versa.
85412 ** 3: A variable arguments function using the same text encoding.
85413 ** 4: A function with the exact number of arguments requested that
85414 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
85415 ** 5: A function with the exact number of arguments requested that
85416 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
85417 ** 6: An exact match.
85418 **
 
 
 
85419 */
85420 static int matchQuality(FuncDef *p, int nArg, u8 enc){
85421 int match = 0;
85422 if( p->nArg==-1 || p->nArg==nArg
85423 || (nArg==-1 && (p->xFunc!=0 || p->xStep!=0))
85424 ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85425 match = 1;
85426 if( p->nArg==nArg || nArg==-1 ){
85427 match = 4;
85428 }
85429 if( enc==p->iPrefEnc ){
85430 match += 2;
85431 }
85432 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
85433 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
85434 match += 1;
85435 }
85436 }
85437 return match;
85438 }
85439
85440 /*
85441 ** Search a FuncDefHash for a function with the given name. Return
@@ -85487,17 +85648,16 @@
85487 ** pointer to the FuncDef structure that defines that function, or return
85488 ** NULL if the function does not exist.
85489 **
85490 ** If the createFlag argument is true, then a new (blank) FuncDef
85491 ** structure is created and liked into the "db" structure if a
85492 ** no matching function previously existed. When createFlag is true
85493 ** and the nArg parameter is -1, then only a function that accepts
85494 ** any number of arguments will be returned.
85495 **
85496 ** If createFlag is false and nArg is -1, then the first valid
85497 ** function found is returned. A function is valid if either xFunc
85498 ** or xStep is non-zero.
 
85499 **
85500 ** If createFlag is false, then a function with the required name and
85501 ** number of arguments may be returned even if the eTextRep flag does not
85502 ** match that requested.
85503 */
@@ -85505,18 +85665,19 @@
85505 sqlite3 *db, /* An open database */
85506 const char *zName, /* Name of the function. Not null-terminated */
85507 int nName, /* Number of characters in the name */
85508 int nArg, /* Number of arguments. -1 means any number */
85509 u8 enc, /* Preferred text encoding */
85510 int createFlag /* Create new entry if true and does not otherwise exist */
85511 ){
85512 FuncDef *p; /* Iterator variable */
85513 FuncDef *pBest = 0; /* Best match found so far */
85514 int bestScore = 0; /* Score of best match */
85515 int h; /* Hash value */
85516
85517
 
85518 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
85519 h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
85520
85521 /* First search for a match amongst the application-defined functions.
85522 */
@@ -85558,11 +85719,11 @@
85558
85559 /* If the createFlag parameter is true and the search did not reveal an
85560 ** exact match for the name, number of arguments and encoding, then add a
85561 ** new entry to the hash table and return it.
85562 */
85563 if( createFlag && (bestScore<6 || pBest->nArg!=nArg) &&
85564 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
85565 pBest->zName = (char *)&pBest[1];
85566 pBest->nArg = (u16)nArg;
85567 pBest->iPrefEnc = enc;
85568 memcpy(pBest->zName, zName, nName);
@@ -97526,11 +97687,12 @@
97526
97527 /***** If we reach this point, flattening is permitted. *****/
97528
97529 /* Authorize the subquery */
97530 pParse->zAuthContext = pSubitem->zName;
97531 sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
 
97532 pParse->zAuthContext = zSavedAuthContext;
97533
97534 /* If the sub-query is a compound SELECT statement, then (by restrictions
97535 ** 17 and 18 above) it must be a UNION ALL and the parent query must
97536 ** be of the form:
@@ -129049,11 +129211,10 @@
129049 /* Advance to the next output block */
129050 pLeaf->iBlock++;
129051 pLeaf->key.n = 0;
129052 pLeaf->block.n = 0;
129053
129054 nPrefix = 0;
129055 nSuffix = nTerm;
129056 nSpace = 1;
129057 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
129058 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
129059 }
@@ -132384,10 +132545,23 @@
132384
132385 /* Possible values for eCoordType: */
132386 #define RTREE_COORD_REAL32 0
132387 #define RTREE_COORD_INT32 1
132388
 
 
 
 
 
 
 
 
 
 
 
 
 
132389 /*
132390 ** The minimum number of cells allowed for a node is a third of the
132391 ** maximum. In Gutman's notation:
132392 **
132393 ** m = M/3
@@ -132419,33 +132593,38 @@
132419 int nConstraint; /* Number of entries in aConstraint */
132420 RtreeConstraint *aConstraint; /* Search constraints. */
132421 };
132422
132423 union RtreeCoord {
132424 float f;
132425 int i;
132426 };
132427
132428 /*
132429 ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
132430 ** formatted as a double. This macro assumes that local variable pRtree points
132431 ** to the Rtree structure associated with the RtreeCoord.
 
132432 */
132433 #define DCOORD(coord) ( \
132434 (pRtree->eCoordType==RTREE_COORD_REAL32) ? \
132435 ((double)coord.f) : \
132436 ((double)coord.i) \
132437 )
 
 
 
 
132438
132439 /*
132440 ** A search constraint.
132441 */
132442 struct RtreeConstraint {
132443 int iCoord; /* Index of constrained coordinate */
132444 int op; /* Constraining operation */
132445 double rValue; /* Constraint value. */
132446 int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
132447 sqlite3_rtree_geometry *pGeom; /* Constraint callback argument for a MATCH */
132448 };
132449
132450 /* Possible values for RtreeConstraint.op */
132451 #define RTREE_EQ 0x41
@@ -132489,14 +132668,14 @@
132489 ** the right-hand-side of an SQL MATCH operator used to constrain an
132490 ** r-tree query.
132491 */
132492 struct RtreeMatchArg {
132493 u32 magic; /* Always RTREE_GEOMETRY_MAGIC */
132494 int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
132495 void *pContext;
132496 int nParam;
132497 double aParam[1];
132498 };
132499
132500 /*
132501 ** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
132502 ** a single instance of the following structure is allocated. It is used
@@ -132504,11 +132683,11 @@
132504 ** is eventually deleted by the destructor mechanism provided by
132505 ** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
132506 ** the geometry callback function).
132507 */
132508 struct RtreeGeomCallback {
132509 int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
132510 void *pContext;
132511 };
132512
132513 #ifndef MAX
132514 # define MAX(x,y) ((x) < (y) ? (y) : (x))
@@ -133070,11 +133249,11 @@
133070 RtreeConstraint *pConstraint, /* MATCH constraint to test */
133071 RtreeCell *pCell, /* Cell to test */
133072 int *pbRes /* OUT: Test result */
133073 ){
133074 int i;
133075 double aCoord[RTREE_MAX_DIMENSIONS*2];
133076 int nCoord = pRtree->nDim*2;
133077
133078 assert( pConstraint->op==RTREE_MATCH );
133079 assert( pConstraint->pGeom );
133080
@@ -133100,12 +133279,12 @@
133100 int rc = SQLITE_OK;
133101
133102 nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
133103 for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
133104 RtreeConstraint *p = &pCursor->aConstraint[ii];
133105 double cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
133106 double cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
133107
133108 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
133109 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
133110 );
133111
@@ -133153,11 +133332,11 @@
133153 *pbEof = 0;
133154
133155 nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
133156 for(ii=0; ii<pCursor->nConstraint; ii++){
133157 RtreeConstraint *p = &pCursor->aConstraint[ii];
133158 double coord = DCOORD(cell.aCoord[p->iCoord]);
133159 int res;
133160 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
133161 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
133162 );
133163 switch( p->op ){
@@ -133351,13 +133530,16 @@
133351 i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
133352 sqlite3_result_int64(ctx, iRowid);
133353 }else{
133354 RtreeCoord c;
133355 nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
 
133356 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
133357 sqlite3_result_double(ctx, c.f);
133358 }else{
 
 
133359 assert( pRtree->eCoordType==RTREE_COORD_INT32 );
133360 sqlite3_result_int(ctx, c.i);
133361 }
133362 }
133363
@@ -133400,11 +133582,11 @@
133400 if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
133401
133402 /* Check that the blob is roughly the right size. */
133403 nBlob = sqlite3_value_bytes(pValue);
133404 if( nBlob<(int)sizeof(RtreeMatchArg)
133405 || ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0
133406 ){
133407 return SQLITE_ERROR;
133408 }
133409
133410 pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
@@ -133414,11 +133596,11 @@
133414 memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
133415 p = (RtreeMatchArg *)&pGeom[1];
133416
133417 memcpy(p, sqlite3_value_blob(pValue), nBlob);
133418 if( p->magic!=RTREE_GEOMETRY_MAGIC
133419 || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(double))
133420 ){
133421 sqlite3_free(pGeom);
133422 return SQLITE_ERROR;
133423 }
133424
@@ -133486,11 +133668,15 @@
133486 rc = deserializeGeometry(argv[ii], p);
133487 if( rc!=SQLITE_OK ){
133488 break;
133489 }
133490 }else{
 
 
 
133491 p->rValue = sqlite3_value_double(argv[ii]);
 
133492 }
133493 }
133494 }
133495 }
133496
@@ -133620,28 +133806,28 @@
133620 }
133621
133622 /*
133623 ** Return the N-dimensional volumn of the cell stored in *p.
133624 */
133625 static float cellArea(Rtree *pRtree, RtreeCell *p){
133626 float area = 1.0;
133627 int ii;
133628 for(ii=0; ii<(pRtree->nDim*2); ii+=2){
133629 area = (float)(area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
133630 }
133631 return area;
133632 }
133633
133634 /*
133635 ** Return the margin length of cell p. The margin length is the sum
133636 ** of the objects size in each dimension.
133637 */
133638 static float cellMargin(Rtree *pRtree, RtreeCell *p){
133639 float margin = 0.0;
133640 int ii;
133641 for(ii=0; ii<(pRtree->nDim*2); ii+=2){
133642 margin += (float)(DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
133643 }
133644 return margin;
133645 }
133646
133647 /*
@@ -133682,51 +133868,50 @@
133682 }
133683
133684 /*
133685 ** Return the amount cell p would grow by if it were unioned with pCell.
133686 */
133687 static float cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
133688 float area;
133689 RtreeCell cell;
133690 memcpy(&cell, p, sizeof(RtreeCell));
133691 area = cellArea(pRtree, &cell);
133692 cellUnion(pRtree, &cell, pCell);
133693 return (cellArea(pRtree, &cell)-area);
133694 }
133695
133696 #if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
133697 static float cellOverlap(
133698 Rtree *pRtree,
133699 RtreeCell *p,
133700 RtreeCell *aCell,
133701 int nCell,
133702 int iExclude
133703 ){
133704 int ii;
133705 float overlap = 0.0;
133706 for(ii=0; ii<nCell; ii++){
133707 #if VARIANT_RSTARTREE_CHOOSESUBTREE
133708 if( ii!=iExclude )
133709 #else
133710 assert( iExclude==-1 );
133711 UNUSED_PARAMETER(iExclude);
133712 #endif
133713 {
133714 int jj;
133715 float o = 1.0;
133716 for(jj=0; jj<(pRtree->nDim*2); jj+=2){
133717 double x1;
133718 double x2;
133719
133720 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
133721 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
133722
133723 if( x2<x1 ){
133724 o = 0.0;
133725 break;
133726 }else{
133727 o = o * (float)(x2-x1);
133728 }
133729 }
133730 overlap += o;
133731 }
133732 }
@@ -133733,24 +133918,23 @@
133733 return overlap;
133734 }
133735 #endif
133736
133737 #if VARIANT_RSTARTREE_CHOOSESUBTREE
133738 static float cellOverlapEnlargement(
133739 Rtree *pRtree,
133740 RtreeCell *p,
133741 RtreeCell *pInsert,
133742 RtreeCell *aCell,
133743 int nCell,
133744 int iExclude
133745 ){
133746 double before;
133747 double after;
133748 before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
133749 cellUnion(pRtree, p, pInsert);
133750 after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
133751 return (float)(after-before);
133752 }
133753 #endif
133754
133755
133756 /*
@@ -133770,15 +133954,15 @@
133770
133771 for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
133772 int iCell;
133773 sqlite3_int64 iBest = 0;
133774
133775 float fMinGrowth = 0.0;
133776 float fMinArea = 0.0;
133777 #if VARIANT_RSTARTREE_CHOOSESUBTREE
133778 float fMinOverlap = 0.0;
133779 float overlap;
133780 #endif
133781
133782 int nCell = NCELL(pNode);
133783 RtreeCell cell;
133784 RtreeNode *pChild;
@@ -133805,12 +133989,12 @@
133805 ** is inserted into it. Resolve ties by choosing the entry with
133806 ** the smallest area.
133807 */
133808 for(iCell=0; iCell<nCell; iCell++){
133809 int bBest = 0;
133810 float growth;
133811 float area;
133812 nodeGetCell(pRtree, pNode, iCell, &cell);
133813 growth = cellGrowth(pRtree, &cell, pCell);
133814 area = cellArea(pRtree, &cell);
133815
133816 #if VARIANT_RSTARTREE_CHOOSESUBTREE
@@ -133933,30 +134117,30 @@
133933 int *piRightSeed
133934 ){
133935 int i;
133936 int iLeftSeed = 0;
133937 int iRightSeed = 1;
133938 float maxNormalInnerWidth = 0.0;
133939
133940 /* Pick two "seed" cells from the array of cells. The algorithm used
133941 ** here is the LinearPickSeeds algorithm from Gutman[1984]. The
133942 ** indices of the two seed cells in the array are stored in local
133943 ** variables iLeftSeek and iRightSeed.
133944 */
133945 for(i=0; i<pRtree->nDim; i++){
133946 float x1 = DCOORD(aCell[0].aCoord[i*2]);
133947 float x2 = DCOORD(aCell[0].aCoord[i*2+1]);
133948 float x3 = x1;
133949 float x4 = x2;
133950 int jj;
133951
133952 int iCellLeft = 0;
133953 int iCellRight = 0;
133954
133955 for(jj=1; jj<nCell; jj++){
133956 float left = DCOORD(aCell[jj].aCoord[i*2]);
133957 float right = DCOORD(aCell[jj].aCoord[i*2+1]);
133958
133959 if( left<x1 ) x1 = left;
133960 if( right>x4 ) x4 = right;
133961 if( left>x3 ){
133962 x3 = left;
@@ -133967,11 +134151,11 @@
133967 iCellLeft = jj;
133968 }
133969 }
133970
133971 if( x4!=x1 ){
133972 float normalwidth = (x3 - x2) / (x4 - x1);
133973 if( normalwidth>maxNormalInnerWidth ){
133974 iLeftSeed = iCellLeft;
133975 iRightSeed = iCellRight;
133976 }
133977 }
@@ -133996,17 +134180,17 @@
133996 int *aiUsed
133997 ){
133998 #define FABS(a) ((a)<0.0?-1.0*(a):(a))
133999
134000 int iSelect = -1;
134001 float fDiff;
134002 int ii;
134003 for(ii=0; ii<nCell; ii++){
134004 if( aiUsed[ii]==0 ){
134005 float left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
134006 float right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
134007 float diff = FABS(right-left);
134008 if( iSelect<0 || diff>fDiff ){
134009 fDiff = diff;
134010 iSelect = ii;
134011 }
134012 }
@@ -134029,17 +134213,17 @@
134029 int ii;
134030 int jj;
134031
134032 int iLeftSeed = 0;
134033 int iRightSeed = 1;
134034 float fWaste = 0.0;
134035
134036 for(ii=0; ii<nCell; ii++){
134037 for(jj=ii+1; jj<nCell; jj++){
134038 float right = cellArea(pRtree, &aCell[jj]);
134039 float growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
134040 float waste = growth - right;
134041
134042 if( waste>fWaste ){
134043 iLeftSeed = ii;
134044 iRightSeed = jj;
134045 fWaste = waste;
@@ -134070,11 +134254,11 @@
134070 ** sorting algorithm.
134071 */
134072 static void SortByDistance(
134073 int *aIdx,
134074 int nIdx,
134075 float *aDistance,
134076 int *aSpare
134077 ){
134078 if( nIdx>1 ){
134079 int iLeft = 0;
134080 int iRight = 0;
@@ -134096,12 +134280,12 @@
134096 iRight++;
134097 }else if( iRight==nRight ){
134098 aIdx[iLeft+iRight] = aLeft[iLeft];
134099 iLeft++;
134100 }else{
134101 float fLeft = aDistance[aLeft[iLeft]];
134102 float fRight = aDistance[aRight[iRight]];
134103 if( fLeft<fRight ){
134104 aIdx[iLeft+iRight] = aLeft[iLeft];
134105 iLeft++;
134106 }else{
134107 aIdx[iLeft+iRight] = aRight[iRight];
@@ -134113,12 +134297,12 @@
134113 #if 0
134114 /* Check that the sort worked */
134115 {
134116 int jj;
134117 for(jj=1; jj<nIdx; jj++){
134118 float left = aDistance[aIdx[jj-1]];
134119 float right = aDistance[aIdx[jj]];
134120 assert( left<=right );
134121 }
134122 }
134123 #endif
134124 }
@@ -134157,14 +134341,14 @@
134157 SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
134158
134159 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
134160 aLeft = aSpare;
134161 while( iLeft<nLeft || iRight<nRight ){
134162 double xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
134163 double xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
134164 double xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
134165 double xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
134166 if( (iLeft!=nLeft) && ((iRight==nRight)
134167 || (xleft1<xright1)
134168 || (xleft1==xright1 && xleft2<xright2)
134169 )){
134170 aIdx[iLeft+iRight] = aLeft[iLeft];
@@ -134178,14 +134362,14 @@
134178 #if 0
134179 /* Check that the sort worked */
134180 {
134181 int jj;
134182 for(jj=1; jj<nIdx; jj++){
134183 float xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
134184 float xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
134185 float xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
134186 float xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
134187 assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
134188 }
134189 }
134190 #endif
134191 }
@@ -134208,11 +134392,11 @@
134208 int *aSpare;
134209 int ii;
134210
134211 int iBestDim = 0;
134212 int iBestSplit = 0;
134213 float fBestMargin = 0.0;
134214
134215 int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
134216
134217 aaSorted = (int **)sqlite3_malloc(nByte);
134218 if( !aaSorted ){
@@ -134229,13 +134413,13 @@
134229 }
134230 SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
134231 }
134232
134233 for(ii=0; ii<pRtree->nDim; ii++){
134234 float margin = 0.0;
134235 float fBestOverlap = 0.0;
134236 float fBestArea = 0.0;
134237 int iBestLeft = 0;
134238 int nLeft;
134239
134240 for(
134241 nLeft=RTREE_MINCELLS(pRtree);
@@ -134243,12 +134427,12 @@
134243 nLeft++
134244 ){
134245 RtreeCell left;
134246 RtreeCell right;
134247 int kk;
134248 float overlap;
134249 float area;
134250
134251 memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
134252 memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
134253 for(kk=1; kk<(nCell-1); kk++){
134254 if( kk<nLeft ){
@@ -134327,11 +134511,11 @@
134327 aiUsed[iRightSeed] = 1;
134328
134329 for(i=nCell-2; i>0; i--){
134330 RtreeCell *pNext;
134331 pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
134332 float diff =
134333 cellGrowth(pRtree, pBboxLeft, pNext) -
134334 cellGrowth(pRtree, pBboxRight, pNext)
134335 ;
134336 if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
134337 || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
@@ -134660,58 +134844,60 @@
134660 int iHeight
134661 ){
134662 int *aOrder;
134663 int *aSpare;
134664 RtreeCell *aCell;
134665 float *aDistance;
134666 int nCell;
134667 float aCenterCoord[RTREE_MAX_DIMENSIONS];
134668 int iDim;
134669 int ii;
134670 int rc = SQLITE_OK;
 
134671
134672 memset(aCenterCoord, 0, sizeof(float)*RTREE_MAX_DIMENSIONS);
134673
134674 nCell = NCELL(pNode)+1;
 
134675
134676 /* Allocate the buffers used by this operation. The allocation is
134677 ** relinquished before this function returns.
134678 */
134679 aCell = (RtreeCell *)sqlite3_malloc(nCell * (
134680 sizeof(RtreeCell) + /* aCell array */
134681 sizeof(int) + /* aOrder array */
134682 sizeof(int) + /* aSpare array */
134683 sizeof(float) /* aDistance array */
134684 ));
134685 if( !aCell ){
134686 return SQLITE_NOMEM;
134687 }
134688 aOrder = (int *)&aCell[nCell];
134689 aSpare = (int *)&aOrder[nCell];
134690 aDistance = (float *)&aSpare[nCell];
134691
134692 for(ii=0; ii<nCell; ii++){
134693 if( ii==(nCell-1) ){
134694 memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
134695 }else{
134696 nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
134697 }
134698 aOrder[ii] = ii;
134699 for(iDim=0; iDim<pRtree->nDim; iDim++){
134700 aCenterCoord[iDim] += (float)DCOORD(aCell[ii].aCoord[iDim*2]);
134701 aCenterCoord[iDim] += (float)DCOORD(aCell[ii].aCoord[iDim*2+1]);
134702 }
134703 }
134704 for(iDim=0; iDim<pRtree->nDim; iDim++){
134705 aCenterCoord[iDim] = (float)(aCenterCoord[iDim]/((float)nCell*2.0));
134706 }
134707
134708 for(ii=0; ii<nCell; ii++){
134709 aDistance[ii] = 0.0;
134710 for(iDim=0; iDim<pRtree->nDim; iDim++){
134711 float coord = (float)(DCOORD(aCell[ii].aCoord[iDim*2+1]) -
134712 DCOORD(aCell[ii].aCoord[iDim*2]));
134713 aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
134714 }
134715 }
134716
134717 SortByDistance(aOrder, nCell, aDistance, aSpare);
@@ -134949,20 +135135,23 @@
134949 if( nData>1 ){
134950 int ii;
134951
134952 /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
134953 assert( nData==(pRtree->nDim*2 + 3) );
 
134954 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
134955 for(ii=0; ii<(pRtree->nDim*2); ii+=2){
134956 cell.aCoord[ii].f = (float)sqlite3_value_double(azData[ii+3]);
134957 cell.aCoord[ii+1].f = (float)sqlite3_value_double(azData[ii+4]);
134958 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
134959 rc = SQLITE_CONSTRAINT;
134960 goto constraint;
134961 }
134962 }
134963 }else{
 
 
134964 for(ii=0; ii<(pRtree->nDim*2); ii+=2){
134965 cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
134966 cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
134967 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
134968 rc = SQLITE_CONSTRAINT;
@@ -135356,11 +135545,17 @@
135356
135357 nodeGetCell(&tree, &node, ii, &cell);
135358 sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
135359 nCell = (int)strlen(zCell);
135360 for(jj=0; jj<tree.nDim*2; jj++){
135361 sqlite3_snprintf(512-nCell,&zCell[nCell]," %f",(double)cell.aCoord[jj].f);
 
 
 
 
 
 
135362 nCell = (int)strlen(zCell);
135363 }
135364
135365 if( zText ){
135366 char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
@@ -135398,11 +135593,15 @@
135398 rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
135399 if( rc==SQLITE_OK ){
135400 rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
135401 }
135402 if( rc==SQLITE_OK ){
 
 
 
135403 void *c = (void *)RTREE_COORD_REAL32;
 
135404 rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
135405 }
135406 if( rc==SQLITE_OK ){
135407 void *c = (void *)RTREE_COORD_INT32;
135408 rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
@@ -135432,11 +135631,11 @@
135432 static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
135433 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
135434 RtreeMatchArg *pBlob;
135435 int nBlob;
135436
135437 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(double);
135438 pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
135439 if( !pBlob ){
135440 sqlite3_result_error_nomem(ctx);
135441 }else{
135442 int i;
@@ -135443,11 +135642,15 @@
135443 pBlob->magic = RTREE_GEOMETRY_MAGIC;
135444 pBlob->xGeom = pGeomCtx->xGeom;
135445 pBlob->pContext = pGeomCtx->pContext;
135446 pBlob->nParam = nArg;
135447 for(i=0; i<nArg; i++){
 
 
 
135448 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
 
135449 }
135450 sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
135451 }
135452 }
135453
@@ -135455,11 +135658,11 @@
135455 ** Register a new geometry function for use with the r-tree MATCH operator.
135456 */
135457 SQLITE_API int sqlite3_rtree_geometry_callback(
135458 sqlite3 *db,
135459 const char *zGeom,
135460 int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *),
135461 void *pContext
135462 ){
135463 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
135464
135465 /* Allocate and populate the context object. */
135466
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -657,11 +657,11 @@
657 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
658 ** [sqlite_version()] and [sqlite_source_id()].
659 */
660 #define SQLITE_VERSION "3.7.12"
661 #define SQLITE_VERSION_NUMBER 3007012
662 #define SQLITE_SOURCE_ID "2012-04-17 09:09:33 8e2363ad76446e863d03ead91fd621e59d5cb495"
663
664 /*
665 ** CAPI3REF: Run-Time Library Version Numbers
666 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
667 **
@@ -2095,11 +2095,11 @@
2095 ** database connection is opened. By default, URI handling is globally
2096 ** disabled. The default value may be changed by compiling with the
2097 ** [SQLITE_USE_URI] symbol defined.
2098 **
2099 ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2100 ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
2101 ** <dd> These options are obsolete and should not be used by new code.
2102 ** They are retained for backwards compatibility but are now no-ops.
2103 ** </dl>
2104 */
2105 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
@@ -7533,11 +7533,15 @@
7533 ** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
7534 */
7535 SQLITE_API int sqlite3_rtree_geometry_callback(
7536 sqlite3 *db,
7537 const char *zGeom,
7538 #ifdef SQLITE_RTREE_INT_ONLY
7539 int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
7540 #else
7541 int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
7542 #endif
7543 void *pContext
7544 );
7545
7546
7547 /*
@@ -10933,11 +10937,10 @@
10937 int nRef; /* Number of names resolved by this context */
10938 int nErr; /* Number of errors encountered while resolving names */
10939 u8 allowAgg; /* Aggregate functions allowed here */
10940 u8 hasAgg; /* True if aggregates are seen */
10941 u8 isCheck; /* True if resolving names in a CHECK constraint */
 
10942 AggInfo *pAggInfo; /* Information about aggregates at this level */
10943 NameContext *pNext; /* Next outer name context. NULL for outermost */
10944 };
10945
10946 /*
@@ -11399,10 +11402,11 @@
11402 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
11403 Parse *pParse; /* Parser context. */
11404 union { /* Extra data for callback */
11405 NameContext *pNC; /* Naming context */
11406 int i; /* Integer value */
11407 SrcList *pSrcList; /* FROM clause */
11408 } u;
11409 };
11410
11411 /* Forward declarations */
11412 SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
@@ -11767,11 +11771,11 @@
11771 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
11772 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
11773 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
11774 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
11775 SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
11776 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
11777 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
11778 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
11779 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
11780 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
11781 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
@@ -24995,12 +24999,12 @@
24999 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
25000 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
25001 #endif
25002
25003 /*
25004 ** Default permissions when creating auto proxy dir
25005 */
25006 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
25007 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
25008 #endif
25009
25010 /*
@@ -25550,11 +25554,11 @@
25554 return 0;
25555 }
25556
25557 /*
25558 ** Invoke open(). Do so multiple times, until it either succeeds or
25559 ** fails for some reason other than EINTR.
25560 **
25561 ** If the file creation mode "m" is 0 then set it to the default for
25562 ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
25563 ** 0644) as modified by the system umask. If m is not 0, then
25564 ** make the file creation mode be exactly m ignoring the umask.
@@ -25566,24 +25570,33 @@
25570 ** transaction crashes and leaves behind hot journals, then any
25571 ** process that is able to write to the database will also be able to
25572 ** recover the hot journals.
25573 */
25574 static int robust_open(const char *z, int f, mode_t m){
25575 int fd;
25576 mode_t m2;
25577 mode_t origM = 0;
25578 if( m==0 ){
25579 m2 = SQLITE_DEFAULT_FILE_PERMISSIONS;
25580 }else{
25581 m2 = m;
25582 origM = osUmask(0);
25583 }
25584 do{
25585 #if defined(O_CLOEXEC)
25586 fd = osOpen(z,f|O_CLOEXEC,m2);
25587 #else
25588 fd = osOpen(z,f,m2);
25589 #endif
25590 }while( fd<0 && errno==EINTR );
25591 if( m ){
25592 osUmask(origM);
25593 }
25594 #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
25595 if( fd>=0 ) osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
25596 #endif
25597 return fd;
25598 }
25599
25600 /*
25601 ** Helper functions to obtain and relinquish the global mutex. The
25602 ** global mutex is used to protect the unixInodeInfo and
@@ -28374,13 +28387,10 @@
28387 for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
28388 if( ii>0 ){
28389 zDirname[ii] = '\0';
28390 fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
28391 if( fd>=0 ){
 
 
 
28392 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
28393 }
28394 }
28395 *pFd = fd;
28396 return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
@@ -28459,11 +28469,11 @@
28469 /* If the user has configured a chunk-size for this file, truncate the
28470 ** file so that it consists of an integer number of chunks (i.e. the
28471 ** actual file size after the operation may be larger than the requested
28472 ** size).
28473 */
28474 if( pFile->szChunk>0 ){
28475 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
28476 }
28477
28478 rc = robust_ftruncate(pFile->h, (off_t)nByte);
28479 if( rc ){
@@ -30221,14 +30231,10 @@
30231 else{
30232 p->openFlags = openFlags;
30233 }
30234 #endif
30235
 
 
 
 
30236 noLock = eType!=SQLITE_OPEN_MAIN_DB;
30237
30238
30239 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
30240 if( fstatfs(fd, &fsInfo) == -1 ){
@@ -38001,10 +38007,15 @@
38007 #define ROWSET_ENTRY_PER_CHUNK \
38008 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
38009
38010 /*
38011 ** Each entry in a RowSet is an instance of the following object.
38012 **
38013 ** This same object is reused to store a linked list of trees of RowSetEntry
38014 ** objects. In that alternative use, pRight points to the next entry
38015 ** in the list, pLeft points to the tree, and v is unused. The
38016 ** RowSet.pForest value points to the head of this forest list.
38017 */
38018 struct RowSetEntry {
38019 i64 v; /* ROWID value for this entry */
38020 struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
38021 struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
@@ -38030,16 +38041,22 @@
38041 struct RowSetChunk *pChunk; /* List of all chunk allocations */
38042 sqlite3 *db; /* The database connection */
38043 struct RowSetEntry *pEntry; /* List of entries using pRight */
38044 struct RowSetEntry *pLast; /* Last entry on the pEntry list */
38045 struct RowSetEntry *pFresh; /* Source of new entry objects */
38046 struct RowSetEntry *pForest; /* List of binary trees of entries */
38047 u16 nFresh; /* Number of objects on pFresh */
38048 u8 rsFlags; /* Various flags */
38049 u8 iBatch; /* Current insert batch */
38050 };
38051
38052 /*
38053 ** Allowed values for RowSet.rsFlags
38054 */
38055 #define ROWSET_SORTED 0x01 /* True if RowSet.pEntry is sorted */
38056 #define ROWSET_NEXT 0x02 /* True if sqlite3RowSetNext() has been called */
38057
38058 /*
38059 ** Turn bulk memory into a RowSet object. N bytes of memory
38060 ** are available at pSpace. The db pointer is used as a memory context
38061 ** for any subsequent allocations that need to occur.
38062 ** Return a pointer to the new RowSet object.
@@ -38056,14 +38073,14 @@
38073 p = pSpace;
38074 p->pChunk = 0;
38075 p->db = db;
38076 p->pEntry = 0;
38077 p->pLast = 0;
38078 p->pForest = 0;
38079 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
38080 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
38081 p->rsFlags = ROWSET_SORTED;
38082 p->iBatch = 0;
38083 return p;
38084 }
38085
38086 /*
@@ -38079,12 +38096,37 @@
38096 }
38097 p->pChunk = 0;
38098 p->nFresh = 0;
38099 p->pEntry = 0;
38100 p->pLast = 0;
38101 p->pForest = 0;
38102 p->rsFlags = ROWSET_SORTED;
38103 }
38104
38105 /*
38106 ** Allocate a new RowSetEntry object that is associated with the
38107 ** given RowSet. Return a pointer to the new and completely uninitialized
38108 ** objected.
38109 **
38110 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
38111 ** routine returns NULL.
38112 */
38113 static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
38114 assert( p!=0 );
38115 if( p->nFresh==0 ){
38116 struct RowSetChunk *pNew;
38117 pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
38118 if( pNew==0 ){
38119 return 0;
38120 }
38121 pNew->pNextChunk = p->pChunk;
38122 p->pChunk = pNew;
38123 p->pFresh = pNew->aEntry;
38124 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
38125 }
38126 p->nFresh--;
38127 return p->pFresh++;
38128 }
38129
38130 /*
38131 ** Insert a new value into a RowSet.
38132 **
@@ -38092,34 +38134,25 @@
38134 ** memory allocation fails.
38135 */
38136 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
38137 struct RowSetEntry *pEntry; /* The new entry */
38138 struct RowSetEntry *pLast; /* The last prior entry */
38139
38140 /* This routine is never called after sqlite3RowSetNext() */
38141 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
38142
38143 pEntry = rowSetEntryAlloc(p);
38144 if( pEntry==0 ) return;
 
 
 
 
 
 
 
 
38145 pEntry->v = rowid;
38146 pEntry->pRight = 0;
38147 pLast = p->pLast;
38148 if( pLast ){
38149 if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){
38150 p->rsFlags &= ~ROWSET_SORTED;
38151 }
38152 pLast->pRight = pEntry;
38153 }else{
 
38154 p->pEntry = pEntry;
38155 }
38156 p->pLast = pEntry;
38157 }
38158
@@ -38127,11 +38160,11 @@
38160 ** Merge two lists of RowSetEntry objects. Remove duplicates.
38161 **
38162 ** The input lists are connected via pRight pointers and are
38163 ** assumed to each already be in sorted order.
38164 */
38165 static struct RowSetEntry *rowSetEntryMerge(
38166 struct RowSetEntry *pA, /* First sorted list to be merged */
38167 struct RowSetEntry *pB /* Second sorted list to be merged */
38168 ){
38169 struct RowSetEntry head;
38170 struct RowSetEntry *pTail;
@@ -38161,36 +38194,33 @@
38194 }
38195 return head.pRight;
38196 }
38197
38198 /*
38199 ** Sort all elements on the list of RowSetEntry objects into order of
38200 ** increasing v.
38201 */
38202 static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
38203 unsigned int i;
38204 struct RowSetEntry *pNext, *aBucket[40];
 
38205
 
38206 memset(aBucket, 0, sizeof(aBucket));
38207 while( pIn ){
38208 pNext = pIn->pRight;
38209 pIn->pRight = 0;
 
38210 for(i=0; aBucket[i]; i++){
38211 pIn = rowSetEntryMerge(aBucket[i], pIn);
38212 aBucket[i] = 0;
38213 }
38214 aBucket[i] = pIn;
38215 pIn = pNext;
38216 }
38217 pIn = 0;
38218 for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
38219 pIn = rowSetEntryMerge(pIn, aBucket[i]);
38220 }
38221 return pIn;
 
 
38222 }
38223
38224
38225 /*
38226 ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
@@ -38280,24 +38310,41 @@
38310 }
38311 return p;
38312 }
38313
38314 /*
38315 ** Take all the entries on p->pEntry and on the trees in p->pForest and
38316 ** sort them all together into one big ordered list on p->pEntry.
38317 **
38318 ** This routine should only be called once in the life of a RowSet.
38319 */
38320 static void rowSetToList(RowSet *p){
38321
38322 /* This routine is called only once */
38323 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
38324
38325 if( (p->rsFlags & ROWSET_SORTED)==0 ){
38326 p->pEntry = rowSetEntrySort(p->pEntry);
38327 }
38328
38329 /* While this module could theoretically support it, sqlite3RowSetNext()
38330 ** is never called after sqlite3RowSetText() for the same RowSet. So
38331 ** there is never a forest to deal with. Should this change, simply
38332 ** remove the assert() and the #if 0. */
38333 assert( p->pForest==0 );
38334 #if 0
38335 while( p->pForest ){
38336 struct RowSetEntry *pTree = p->pForest->pLeft;
38337 if( pTree ){
38338 struct RowSetEntry *pHead, *pTail;
38339 rowSetTreeToList(pTree, &pHead, &pTail);
38340 p->pEntry = rowSetEntryMerge(p->pEntry, pHead);
38341 }
38342 p->pForest = p->pForest->pRight;
38343 }
38344 #endif
38345 p->rsFlags |= ROWSET_NEXT; /* Verify this routine is never called again */
38346 }
38347
38348 /*
38349 ** Extract the smallest element from the RowSet.
38350 ** Write the element into *pRowid. Return 1 on success. Return
@@ -38305,11 +38352,16 @@
38352 **
38353 ** After this routine has been called, the sqlite3RowSetInsert()
38354 ** routine may not be called again.
38355 */
38356 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
38357 assert( p!=0 );
38358
38359 /* Merge the forest into a single sorted list on first call */
38360 if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p);
38361
38362 /* Return the next entry on the list */
38363 if( p->pEntry ){
38364 *pRowid = p->pEntry->v;
38365 p->pEntry = p->pEntry->pRight;
38366 if( p->pEntry==0 ){
38367 sqlite3RowSetClear(p);
@@ -38321,30 +38373,70 @@
38373 }
38374
38375 /*
38376 ** Check to see if element iRowid was inserted into the the rowset as
38377 ** part of any insert batch prior to iBatch. Return 1 or 0.
38378 **
38379 ** If this is the first test of a new batch and if there exist entires
38380 ** on pRowSet->pEntry, then sort those entires into the forest at
38381 ** pRowSet->pForest so that they can be tested.
38382 */
38383 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
38384 struct RowSetEntry *p, *pTree;
38385
38386 /* This routine is never called after sqlite3RowSetNext() */
38387 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
38388
38389 /* Sort entries into the forest on the first test of a new batch
38390 */
38391 if( iBatch!=pRowSet->iBatch ){
38392 p = pRowSet->pEntry;
38393 if( p ){
38394 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
38395 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){
38396 p = rowSetEntrySort(p);
38397 }
38398 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
38399 ppPrevTree = &pTree->pRight;
38400 if( pTree->pLeft==0 ){
38401 pTree->pLeft = rowSetListToTree(p);
38402 break;
38403 }else{
38404 struct RowSetEntry *pAux, *pTail;
38405 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
38406 pTree->pLeft = 0;
38407 p = rowSetEntryMerge(pAux, p);
38408 }
38409 }
38410 if( pTree==0 ){
38411 *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
38412 if( pTree ){
38413 pTree->v = 0;
38414 pTree->pRight = 0;
38415 pTree->pLeft = rowSetListToTree(p);
38416 }
38417 }
38418 pRowSet->pEntry = 0;
38419 pRowSet->pLast = 0;
38420 pRowSet->rsFlags |= ROWSET_SORTED;
38421 }
38422 pRowSet->iBatch = iBatch;
38423 }
38424
38425 /* Test to see if the iRowid value appears anywhere in the forest.
38426 ** Return 1 if it does and 0 if not.
38427 */
38428 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
38429 p = pTree->pLeft;
38430 while( p ){
38431 if( p->v<iRowid ){
38432 p = p->pRight;
38433 }else if( p->v>iRowid ){
38434 p = p->pLeft;
38435 }else{
38436 return 1;
38437 }
38438 }
38439 }
38440 return 0;
38441 }
38442
@@ -49148,16 +49240,22 @@
49240
49241
49242 /*
49243 ** This structure is passed around through all the sanity checking routines
49244 ** in order to keep track of some global state information.
49245 **
49246 ** The aRef[] array is allocated so that there is 1 bit for each page in
49247 ** the database. As the integrity-check proceeds, for each page used in
49248 ** the database the corresponding bit is set. This allows integrity-check to
49249 ** detect pages that are used twice and orphaned pages (both of which
49250 ** indicate corruption).
49251 */
49252 typedef struct IntegrityCk IntegrityCk;
49253 struct IntegrityCk {
49254 BtShared *pBt; /* The tree being checked out */
49255 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
49256 u8 *aPgRef; /* 1 bit per page in the db (see above) */
49257 Pgno nPage; /* Number of pages in the database */
49258 int mxErr; /* Stop accumulating errors when this reaches zero */
49259 int nErr; /* Number of messages written to zErrMsg so far */
49260 int mallocFailed; /* A memory allocation error has occurred */
49261 StrAccum errMsg; /* Accumulate the error message text here */
@@ -56998,10 +57096,29 @@
57096 }
57097 }
57098 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
57099
57100 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
57101
57102 /*
57103 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
57104 ** corresponds to page iPg is already set.
57105 */
57106 static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
57107 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
57108 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
57109 }
57110
57111 /*
57112 ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
57113 */
57114 static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
57115 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
57116 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
57117 }
57118
57119
57120 /*
57121 ** Add 1 to the reference count for page iPage. If this is the second
57122 ** reference to the page, add an error message to pCheck->zErrMsg.
57123 ** Return 1 if there are 2 ore more references to the page and 0 if
57124 ** if this is the first reference to the page.
@@ -57012,15 +57129,16 @@
57129 if( iPage==0 ) return 1;
57130 if( iPage>pCheck->nPage ){
57131 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
57132 return 1;
57133 }
57134 if( getPageReferenced(pCheck, iPage) ){
57135 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
57136 return 1;
57137 }
57138 setPageReferenced(pCheck, iPage);
57139 return 0;
57140 }
57141
57142 #ifndef SQLITE_OMIT_AUTOVACUUM
57143 /*
57144 ** Check that the entry in the pointer-map for page iChild maps to
@@ -57392,21 +57510,19 @@
57510 *pnErr = 0;
57511 if( sCheck.nPage==0 ){
57512 sqlite3BtreeLeave(p);
57513 return 0;
57514 }
57515
57516 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
57517 if( !sCheck.aPgRef ){
57518 *pnErr = 1;
57519 sqlite3BtreeLeave(p);
57520 return 0;
57521 }
 
57522 i = PENDING_BYTE_PAGE(pBt);
57523 if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
 
 
57524 sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
57525 sCheck.errMsg.useMalloc = 2;
57526
57527 /* Check the integrity of the freelist
57528 */
@@ -57427,22 +57543,22 @@
57543
57544 /* Make sure every page in the file is referenced
57545 */
57546 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
57547 #ifdef SQLITE_OMIT_AUTOVACUUM
57548 if( getPageReferenced(&sCheck, i)==0 ){
57549 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
57550 }
57551 #else
57552 /* If the database supports auto-vacuum, make sure no tables contain
57553 ** references to pointer-map pages.
57554 */
57555 if( getPageReferenced(&sCheck, i)==0 &&
57556 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
57557 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
57558 }
57559 if( getPageReferenced(&sCheck, i)!=0 &&
57560 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
57561 checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
57562 }
57563 #endif
57564 }
@@ -57459,11 +57575,11 @@
57575 }
57576
57577 /* Clean up and report errors.
57578 */
57579 sqlite3BtreeLeave(p);
57580 sqlite3_free(sCheck.aPgRef);
57581 if( sCheck.mallocFailed ){
57582 sqlite3StrAccumReset(&sCheck.errMsg);
57583 *pnErr = sCheck.nErr+1;
57584 return 0;
57585 }
@@ -73807,11 +73923,11 @@
73923 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
73924 zId = pExpr->u.zToken;
73925 nId = sqlite3Strlen30(zId);
73926 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
73927 if( pDef==0 ){
73928 pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
73929 if( pDef==0 ){
73930 no_such_func = 1;
73931 }else{
73932 wrong_num_args = 1;
73933 }
@@ -78277,11 +78393,11 @@
78393 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
78394 if( ExprHasProperty(pA, EP_IntValue) ){
78395 if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
78396 return 2;
78397 }
78398 }else if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
78399 if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
78400 if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
78401 return 2;
78402 }
78403 }
@@ -78313,10 +78429,45 @@
78429 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
78430 if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
78431 }
78432 return 0;
78433 }
78434
78435 /*
78436 ** This is the expression callback for sqlite3FunctionUsesOtherSrc().
78437 **
78438 ** Determine if an expression references any table other than one of the
78439 ** tables in pWalker->u.pSrcList and abort if it does.
78440 */
78441 static int exprUsesOtherSrc(Walker *pWalker, Expr *pExpr){
78442 if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){
78443 int i;
78444 SrcList *pSrc = pWalker->u.pSrcList;
78445 for(i=0; i<pSrc->nSrc; i++){
78446 if( pExpr->iTable==pSrc->a[i].iCursor ) return WRC_Continue;
78447 }
78448 return WRC_Abort;
78449 }else{
78450 return WRC_Continue;
78451 }
78452 }
78453
78454 /*
78455 ** Determine if any of the arguments to the pExpr Function references
78456 ** any SrcList other than pSrcList. Return true if they do. Return
78457 ** false if pExpr has no argument or has only constant arguments or
78458 ** only references tables named in pSrcList.
78459 */
78460 static int sqlite3FunctionUsesOtherSrc(Expr *pExpr, SrcList *pSrcList){
78461 Walker w;
78462 assert( pExpr->op==TK_AGG_FUNCTION );
78463 memset(&w, 0, sizeof(w));
78464 w.xExprCallback = exprUsesOtherSrc;
78465 w.u.pSrcList = pSrcList;
78466 if( sqlite3WalkExprList(&w, pExpr->x.pList)!=WRC_Continue ) return 1;
78467 return 0;
78468 }
78469
78470 /*
78471 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
78472 ** the new element. Return a negative number if malloc fails.
78473 */
@@ -78429,13 +78580,11 @@
78580 } /* end loop over pSrcList */
78581 }
78582 return WRC_Prune;
78583 }
78584 case TK_AGG_FUNCTION: {
78585 if( !sqlite3FunctionUsesOtherSrc(pExpr, pSrcList) ){
 
 
78586 /* Check to see if pExpr is a duplicate of another aggregate
78587 ** function that is already in the pAggInfo structure
78588 */
78589 struct AggInfo_func *pItem = pAggInfo->aFunc;
78590 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
@@ -78475,19 +78624,11 @@
78624 }
78625 }
78626 return WRC_Continue;
78627 }
78628 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
78629 return WRC_Continue;
 
 
 
 
 
 
 
 
78630 }
78631
78632 /*
78633 ** Analyze the given expression looking for aggregate functions and
78634 ** for variables that need to be added to the pParse->aAgg[] array.
@@ -78496,10 +78637,11 @@
78637 ** This routine should only be called after the expression has been
78638 ** analyzed by sqlite3ResolveExprNames().
78639 */
78640 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
78641 Walker w;
78642 memset(&w, 0, sizeof(w));
78643 w.xExprCallback = analyzeAggregate;
78644 w.xSelectCallback = analyzeAggregatesInSelect;
78645 w.u.pNC = pNC;
78646 assert( pNC->pSrcList!=0 );
78647 sqlite3WalkExpr(&w, pExpr);
@@ -85399,43 +85541,62 @@
85541 /* During the search for the best function definition, this procedure
85542 ** is called to test how well the function passed as the first argument
85543 ** matches the request for a function with nArg arguments in a system
85544 ** that uses encoding enc. The value returned indicates how well the
85545 ** request is matched. A higher value indicates a better match.
85546 **
85547 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
85548 ** is also -1. In other words, we are searching for a function that
85549 ** takes a variable number of arguments.
85550 **
85551 ** If nArg is -2 that means that we are searching for any function
85552 ** regardless of the number of arguments it uses, so return a positive
85553 ** match score for any
85554 **
85555 ** The returned value is always between 0 and 6, as follows:
85556 **
85557 ** 0: Not a match.
85558 ** 1: UTF8/16 conversion required and function takes any number of arguments.
85559 ** 2: UTF16 byte order change required and function takes any number of args.
85560 ** 3: encoding matches and function takes any number of arguments
85561 ** 4: UTF8/16 conversion required - argument count matches exactly
85562 ** 5: UTF16 byte order conversion required - argument count matches exactly
85563 ** 6: Perfect match: encoding and argument count match exactly.
 
 
 
 
85564 **
85565 ** If nArg==(-2) then any function with a non-null xStep or xFunc is
85566 ** a perfect match and any function with both xStep and xFunc NULL is
85567 ** a non-match.
85568 */
85569 #define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */
85570 static int matchQuality(
85571 FuncDef *p, /* The function we are evaluating for match quality */
85572 int nArg, /* Desired number of arguments. (-1)==any */
85573 u8 enc /* Desired text encoding */
85574 ){
85575 int match;
85576
85577 /* nArg of -2 is a special case */
85578 if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH;
85579
85580 /* Wrong number of arguments means "no match" */
85581 if( p->nArg!=nArg && p->nArg>=0 ) return 0;
85582
85583 /* Give a better score to a function with a specific number of arguments
85584 ** than to function that accepts any number of arguments. */
85585 if( p->nArg==nArg ){
85586 match = 4;
85587 }else{
85588 match = 1;
85589 }
85590
85591 /* Bonus points if the text encoding matches */
85592 if( enc==p->iPrefEnc ){
85593 match += 2; /* Exact encoding match */
85594 }else if( (enc & p->iPrefEnc & 2)!=0 ){
85595 match += 1; /* Both are UTF16, but with different byte orders */
85596 }
85597
 
 
85598 return match;
85599 }
85600
85601 /*
85602 ** Search a FuncDefHash for a function with the given name. Return
@@ -85487,17 +85648,16 @@
85648 ** pointer to the FuncDef structure that defines that function, or return
85649 ** NULL if the function does not exist.
85650 **
85651 ** If the createFlag argument is true, then a new (blank) FuncDef
85652 ** structure is created and liked into the "db" structure if a
85653 ** no matching function previously existed.
 
 
85654 **
85655 ** If nArg is -2, then the first valid function found is returned. A
85656 ** function is valid if either xFunc or xStep is non-zero. The nArg==(-2)
85657 ** case is used to see if zName is a valid function name for some number
85658 ** of arguments. If nArg is -2, then createFlag must be 0.
85659 **
85660 ** If createFlag is false, then a function with the required name and
85661 ** number of arguments may be returned even if the eTextRep flag does not
85662 ** match that requested.
85663 */
@@ -85505,18 +85665,19 @@
85665 sqlite3 *db, /* An open database */
85666 const char *zName, /* Name of the function. Not null-terminated */
85667 int nName, /* Number of characters in the name */
85668 int nArg, /* Number of arguments. -1 means any number */
85669 u8 enc, /* Preferred text encoding */
85670 u8 createFlag /* Create new entry if true and does not otherwise exist */
85671 ){
85672 FuncDef *p; /* Iterator variable */
85673 FuncDef *pBest = 0; /* Best match found so far */
85674 int bestScore = 0; /* Score of best match */
85675 int h; /* Hash value */
85676
85677 assert( nArg>=(-2) );
85678 assert( nArg>=(-1) || createFlag==0 );
85679 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
85680 h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
85681
85682 /* First search for a match amongst the application-defined functions.
85683 */
@@ -85558,11 +85719,11 @@
85719
85720 /* If the createFlag parameter is true and the search did not reveal an
85721 ** exact match for the name, number of arguments and encoding, then add a
85722 ** new entry to the hash table and return it.
85723 */
85724 if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
85725 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
85726 pBest->zName = (char *)&pBest[1];
85727 pBest->nArg = (u16)nArg;
85728 pBest->iPrefEnc = enc;
85729 memcpy(pBest->zName, zName, nName);
@@ -97526,11 +97687,12 @@
97687
97688 /***** If we reach this point, flattening is permitted. *****/
97689
97690 /* Authorize the subquery */
97691 pParse->zAuthContext = pSubitem->zName;
97692 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
97693 testcase( i==SQLITE_DENY );
97694 pParse->zAuthContext = zSavedAuthContext;
97695
97696 /* If the sub-query is a compound SELECT statement, then (by restrictions
97697 ** 17 and 18 above) it must be a UNION ALL and the parent query must
97698 ** be of the form:
@@ -129049,11 +129211,10 @@
129211 /* Advance to the next output block */
129212 pLeaf->iBlock++;
129213 pLeaf->key.n = 0;
129214 pLeaf->block.n = 0;
129215
 
129216 nSuffix = nTerm;
129217 nSpace = 1;
129218 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
129219 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
129220 }
@@ -132384,10 +132545,23 @@
132545
132546 /* Possible values for eCoordType: */
132547 #define RTREE_COORD_REAL32 0
132548 #define RTREE_COORD_INT32 1
132549
132550 /*
132551 ** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
132552 ** only deal with integer coordinates. No floating point operations
132553 ** will be done.
132554 */
132555 #ifdef SQLITE_RTREE_INT_ONLY
132556 typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
132557 typedef int RtreeValue; /* Low accuracy coordinate */
132558 #else
132559 typedef double RtreeDValue; /* High accuracy coordinate */
132560 typedef float RtreeValue; /* Low accuracy coordinate */
132561 #endif
132562
132563 /*
132564 ** The minimum number of cells allowed for a node is a third of the
132565 ** maximum. In Gutman's notation:
132566 **
132567 ** m = M/3
@@ -132419,33 +132593,38 @@
132593 int nConstraint; /* Number of entries in aConstraint */
132594 RtreeConstraint *aConstraint; /* Search constraints. */
132595 };
132596
132597 union RtreeCoord {
132598 RtreeValue f;
132599 int i;
132600 };
132601
132602 /*
132603 ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
132604 ** formatted as a RtreeDValue (double or int64). This macro assumes that local
132605 ** variable pRtree points to the Rtree structure associated with the
132606 ** RtreeCoord.
132607 */
132608 #ifdef SQLITE_RTREE_INT_ONLY
132609 # define DCOORD(coord) ((RtreeDValue)coord.i)
132610 #else
132611 # define DCOORD(coord) ( \
132612 (pRtree->eCoordType==RTREE_COORD_REAL32) ? \
132613 ((double)coord.f) : \
132614 ((double)coord.i) \
132615 )
132616 #endif
132617
132618 /*
132619 ** A search constraint.
132620 */
132621 struct RtreeConstraint {
132622 int iCoord; /* Index of constrained coordinate */
132623 int op; /* Constraining operation */
132624 RtreeDValue rValue; /* Constraint value. */
132625 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
132626 sqlite3_rtree_geometry *pGeom; /* Constraint callback argument for a MATCH */
132627 };
132628
132629 /* Possible values for RtreeConstraint.op */
132630 #define RTREE_EQ 0x41
@@ -132489,14 +132668,14 @@
132668 ** the right-hand-side of an SQL MATCH operator used to constrain an
132669 ** r-tree query.
132670 */
132671 struct RtreeMatchArg {
132672 u32 magic; /* Always RTREE_GEOMETRY_MAGIC */
132673 int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue*, int *);
132674 void *pContext;
132675 int nParam;
132676 RtreeDValue aParam[1];
132677 };
132678
132679 /*
132680 ** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
132681 ** a single instance of the following structure is allocated. It is used
@@ -132504,11 +132683,11 @@
132683 ** is eventually deleted by the destructor mechanism provided by
132684 ** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
132685 ** the geometry callback function).
132686 */
132687 struct RtreeGeomCallback {
132688 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
132689 void *pContext;
132690 };
132691
132692 #ifndef MAX
132693 # define MAX(x,y) ((x) < (y) ? (y) : (x))
@@ -133070,11 +133249,11 @@
133249 RtreeConstraint *pConstraint, /* MATCH constraint to test */
133250 RtreeCell *pCell, /* Cell to test */
133251 int *pbRes /* OUT: Test result */
133252 ){
133253 int i;
133254 RtreeDValue aCoord[RTREE_MAX_DIMENSIONS*2];
133255 int nCoord = pRtree->nDim*2;
133256
133257 assert( pConstraint->op==RTREE_MATCH );
133258 assert( pConstraint->pGeom );
133259
@@ -133100,12 +133279,12 @@
133279 int rc = SQLITE_OK;
133280
133281 nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
133282 for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
133283 RtreeConstraint *p = &pCursor->aConstraint[ii];
133284 RtreeDValue cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
133285 RtreeDValue cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
133286
133287 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
133288 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
133289 );
133290
@@ -133153,11 +133332,11 @@
133332 *pbEof = 0;
133333
133334 nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
133335 for(ii=0; ii<pCursor->nConstraint; ii++){
133336 RtreeConstraint *p = &pCursor->aConstraint[ii];
133337 RtreeDValue coord = DCOORD(cell.aCoord[p->iCoord]);
133338 int res;
133339 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
133340 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
133341 );
133342 switch( p->op ){
@@ -133351,13 +133530,16 @@
133530 i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
133531 sqlite3_result_int64(ctx, iRowid);
133532 }else{
133533 RtreeCoord c;
133534 nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
133535 #ifndef SQLITE_RTREE_INT_ONLY
133536 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
133537 sqlite3_result_double(ctx, c.f);
133538 }else
133539 #endif
133540 {
133541 assert( pRtree->eCoordType==RTREE_COORD_INT32 );
133542 sqlite3_result_int(ctx, c.i);
133543 }
133544 }
133545
@@ -133400,11 +133582,11 @@
133582 if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
133583
133584 /* Check that the blob is roughly the right size. */
133585 nBlob = sqlite3_value_bytes(pValue);
133586 if( nBlob<(int)sizeof(RtreeMatchArg)
133587 || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
133588 ){
133589 return SQLITE_ERROR;
133590 }
133591
133592 pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
@@ -133414,11 +133596,11 @@
133596 memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
133597 p = (RtreeMatchArg *)&pGeom[1];
133598
133599 memcpy(p, sqlite3_value_blob(pValue), nBlob);
133600 if( p->magic!=RTREE_GEOMETRY_MAGIC
133601 || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(RtreeDValue))
133602 ){
133603 sqlite3_free(pGeom);
133604 return SQLITE_ERROR;
133605 }
133606
@@ -133486,11 +133668,15 @@
133668 rc = deserializeGeometry(argv[ii], p);
133669 if( rc!=SQLITE_OK ){
133670 break;
133671 }
133672 }else{
133673 #ifdef SQLITE_RTREE_INT_ONLY
133674 p->rValue = sqlite3_value_int64(argv[ii]);
133675 #else
133676 p->rValue = sqlite3_value_double(argv[ii]);
133677 #endif
133678 }
133679 }
133680 }
133681 }
133682
@@ -133620,28 +133806,28 @@
133806 }
133807
133808 /*
133809 ** Return the N-dimensional volumn of the cell stored in *p.
133810 */
133811 static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
133812 RtreeDValue area = (RtreeDValue)1;
133813 int ii;
133814 for(ii=0; ii<(pRtree->nDim*2); ii+=2){
133815 area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
133816 }
133817 return area;
133818 }
133819
133820 /*
133821 ** Return the margin length of cell p. The margin length is the sum
133822 ** of the objects size in each dimension.
133823 */
133824 static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
133825 RtreeDValue margin = (RtreeDValue)0;
133826 int ii;
133827 for(ii=0; ii<(pRtree->nDim*2); ii+=2){
133828 margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
133829 }
133830 return margin;
133831 }
133832
133833 /*
@@ -133682,51 +133868,50 @@
133868 }
133869
133870 /*
133871 ** Return the amount cell p would grow by if it were unioned with pCell.
133872 */
133873 static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
133874 RtreeDValue area;
133875 RtreeCell cell;
133876 memcpy(&cell, p, sizeof(RtreeCell));
133877 area = cellArea(pRtree, &cell);
133878 cellUnion(pRtree, &cell, pCell);
133879 return (cellArea(pRtree, &cell)-area);
133880 }
133881
133882 #if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
133883 static RtreeDValue cellOverlap(
133884 Rtree *pRtree,
133885 RtreeCell *p,
133886 RtreeCell *aCell,
133887 int nCell,
133888 int iExclude
133889 ){
133890 int ii;
133891 RtreeDValue overlap = 0.0;
133892 for(ii=0; ii<nCell; ii++){
133893 #if VARIANT_RSTARTREE_CHOOSESUBTREE
133894 if( ii!=iExclude )
133895 #else
133896 assert( iExclude==-1 );
133897 UNUSED_PARAMETER(iExclude);
133898 #endif
133899 {
133900 int jj;
133901 RtreeDValue o = (RtreeDValue)1;
133902 for(jj=0; jj<(pRtree->nDim*2); jj+=2){
133903 RtreeDValue x1, x2;
 
133904
133905 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
133906 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
133907
133908 if( x2<x1 ){
133909 o = 0.0;
133910 break;
133911 }else{
133912 o = o * (x2-x1);
133913 }
133914 }
133915 overlap += o;
133916 }
133917 }
@@ -133733,24 +133918,23 @@
133918 return overlap;
133919 }
133920 #endif
133921
133922 #if VARIANT_RSTARTREE_CHOOSESUBTREE
133923 static RtreeDValue cellOverlapEnlargement(
133924 Rtree *pRtree,
133925 RtreeCell *p,
133926 RtreeCell *pInsert,
133927 RtreeCell *aCell,
133928 int nCell,
133929 int iExclude
133930 ){
133931 RtreeDValue before, after;
 
133932 before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
133933 cellUnion(pRtree, p, pInsert);
133934 after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
133935 return (after-before);
133936 }
133937 #endif
133938
133939
133940 /*
@@ -133770,15 +133954,15 @@
133954
133955 for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
133956 int iCell;
133957 sqlite3_int64 iBest = 0;
133958
133959 RtreeDValue fMinGrowth = 0.0;
133960 RtreeDValue fMinArea = 0.0;
133961 #if VARIANT_RSTARTREE_CHOOSESUBTREE
133962 RtreeDValue fMinOverlap = 0.0;
133963 RtreeDValue overlap;
133964 #endif
133965
133966 int nCell = NCELL(pNode);
133967 RtreeCell cell;
133968 RtreeNode *pChild;
@@ -133805,12 +133989,12 @@
133989 ** is inserted into it. Resolve ties by choosing the entry with
133990 ** the smallest area.
133991 */
133992 for(iCell=0; iCell<nCell; iCell++){
133993 int bBest = 0;
133994 RtreeDValue growth;
133995 RtreeDValue area;
133996 nodeGetCell(pRtree, pNode, iCell, &cell);
133997 growth = cellGrowth(pRtree, &cell, pCell);
133998 area = cellArea(pRtree, &cell);
133999
134000 #if VARIANT_RSTARTREE_CHOOSESUBTREE
@@ -133933,30 +134117,30 @@
134117 int *piRightSeed
134118 ){
134119 int i;
134120 int iLeftSeed = 0;
134121 int iRightSeed = 1;
134122 RtreeDValue maxNormalInnerWidth = (RtreeDValue)0;
134123
134124 /* Pick two "seed" cells from the array of cells. The algorithm used
134125 ** here is the LinearPickSeeds algorithm from Gutman[1984]. The
134126 ** indices of the two seed cells in the array are stored in local
134127 ** variables iLeftSeek and iRightSeed.
134128 */
134129 for(i=0; i<pRtree->nDim; i++){
134130 RtreeDValue x1 = DCOORD(aCell[0].aCoord[i*2]);
134131 RtreeDValue x2 = DCOORD(aCell[0].aCoord[i*2+1]);
134132 RtreeDValue x3 = x1;
134133 RtreeDValue x4 = x2;
134134 int jj;
134135
134136 int iCellLeft = 0;
134137 int iCellRight = 0;
134138
134139 for(jj=1; jj<nCell; jj++){
134140 RtreeDValue left = DCOORD(aCell[jj].aCoord[i*2]);
134141 RtreeDValue right = DCOORD(aCell[jj].aCoord[i*2+1]);
134142
134143 if( left<x1 ) x1 = left;
134144 if( right>x4 ) x4 = right;
134145 if( left>x3 ){
134146 x3 = left;
@@ -133967,11 +134151,11 @@
134151 iCellLeft = jj;
134152 }
134153 }
134154
134155 if( x4!=x1 ){
134156 RtreeDValue normalwidth = (x3 - x2) / (x4 - x1);
134157 if( normalwidth>maxNormalInnerWidth ){
134158 iLeftSeed = iCellLeft;
134159 iRightSeed = iCellRight;
134160 }
134161 }
@@ -133996,17 +134180,17 @@
134180 int *aiUsed
134181 ){
134182 #define FABS(a) ((a)<0.0?-1.0*(a):(a))
134183
134184 int iSelect = -1;
134185 RtreeDValue fDiff;
134186 int ii;
134187 for(ii=0; ii<nCell; ii++){
134188 if( aiUsed[ii]==0 ){
134189 RtreeDValue left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
134190 RtreeDValue right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
134191 RtreeDValue diff = FABS(right-left);
134192 if( iSelect<0 || diff>fDiff ){
134193 fDiff = diff;
134194 iSelect = ii;
134195 }
134196 }
@@ -134029,17 +134213,17 @@
134213 int ii;
134214 int jj;
134215
134216 int iLeftSeed = 0;
134217 int iRightSeed = 1;
134218 RtreeDValue fWaste = 0.0;
134219
134220 for(ii=0; ii<nCell; ii++){
134221 for(jj=ii+1; jj<nCell; jj++){
134222 RtreeDValue right = cellArea(pRtree, &aCell[jj]);
134223 RtreeDValue growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
134224 RtreeDValue waste = growth - right;
134225
134226 if( waste>fWaste ){
134227 iLeftSeed = ii;
134228 iRightSeed = jj;
134229 fWaste = waste;
@@ -134070,11 +134254,11 @@
134254 ** sorting algorithm.
134255 */
134256 static void SortByDistance(
134257 int *aIdx,
134258 int nIdx,
134259 RtreeDValue *aDistance,
134260 int *aSpare
134261 ){
134262 if( nIdx>1 ){
134263 int iLeft = 0;
134264 int iRight = 0;
@@ -134096,12 +134280,12 @@
134280 iRight++;
134281 }else if( iRight==nRight ){
134282 aIdx[iLeft+iRight] = aLeft[iLeft];
134283 iLeft++;
134284 }else{
134285 RtreeDValue fLeft = aDistance[aLeft[iLeft]];
134286 RtreeDValue fRight = aDistance[aRight[iRight]];
134287 if( fLeft<fRight ){
134288 aIdx[iLeft+iRight] = aLeft[iLeft];
134289 iLeft++;
134290 }else{
134291 aIdx[iLeft+iRight] = aRight[iRight];
@@ -134113,12 +134297,12 @@
134297 #if 0
134298 /* Check that the sort worked */
134299 {
134300 int jj;
134301 for(jj=1; jj<nIdx; jj++){
134302 RtreeDValue left = aDistance[aIdx[jj-1]];
134303 RtreeDValue right = aDistance[aIdx[jj]];
134304 assert( left<=right );
134305 }
134306 }
134307 #endif
134308 }
@@ -134157,14 +134341,14 @@
134341 SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
134342
134343 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
134344 aLeft = aSpare;
134345 while( iLeft<nLeft || iRight<nRight ){
134346 RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
134347 RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
134348 RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
134349 RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
134350 if( (iLeft!=nLeft) && ((iRight==nRight)
134351 || (xleft1<xright1)
134352 || (xleft1==xright1 && xleft2<xright2)
134353 )){
134354 aIdx[iLeft+iRight] = aLeft[iLeft];
@@ -134178,14 +134362,14 @@
134362 #if 0
134363 /* Check that the sort worked */
134364 {
134365 int jj;
134366 for(jj=1; jj<nIdx; jj++){
134367 RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
134368 RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
134369 RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
134370 RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
134371 assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
134372 }
134373 }
134374 #endif
134375 }
@@ -134208,11 +134392,11 @@
134392 int *aSpare;
134393 int ii;
134394
134395 int iBestDim = 0;
134396 int iBestSplit = 0;
134397 RtreeDValue fBestMargin = 0.0;
134398
134399 int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
134400
134401 aaSorted = (int **)sqlite3_malloc(nByte);
134402 if( !aaSorted ){
@@ -134229,13 +134413,13 @@
134413 }
134414 SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
134415 }
134416
134417 for(ii=0; ii<pRtree->nDim; ii++){
134418 RtreeDValue margin = 0.0;
134419 RtreeDValue fBestOverlap = 0.0;
134420 RtreeDValue fBestArea = 0.0;
134421 int iBestLeft = 0;
134422 int nLeft;
134423
134424 for(
134425 nLeft=RTREE_MINCELLS(pRtree);
@@ -134243,12 +134427,12 @@
134427 nLeft++
134428 ){
134429 RtreeCell left;
134430 RtreeCell right;
134431 int kk;
134432 RtreeDValue overlap;
134433 RtreeDValue area;
134434
134435 memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
134436 memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
134437 for(kk=1; kk<(nCell-1); kk++){
134438 if( kk<nLeft ){
@@ -134327,11 +134511,11 @@
134511 aiUsed[iRightSeed] = 1;
134512
134513 for(i=nCell-2; i>0; i--){
134514 RtreeCell *pNext;
134515 pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
134516 RtreeDValue diff =
134517 cellGrowth(pRtree, pBboxLeft, pNext) -
134518 cellGrowth(pRtree, pBboxRight, pNext)
134519 ;
134520 if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
134521 || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
@@ -134660,58 +134844,60 @@
134844 int iHeight
134845 ){
134846 int *aOrder;
134847 int *aSpare;
134848 RtreeCell *aCell;
134849 RtreeDValue *aDistance;
134850 int nCell;
134851 RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
134852 int iDim;
134853 int ii;
134854 int rc = SQLITE_OK;
134855 int n;
134856
134857 memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
134858
134859 nCell = NCELL(pNode)+1;
134860 n = (nCell+1)&(~1);
134861
134862 /* Allocate the buffers used by this operation. The allocation is
134863 ** relinquished before this function returns.
134864 */
134865 aCell = (RtreeCell *)sqlite3_malloc(n * (
134866 sizeof(RtreeCell) + /* aCell array */
134867 sizeof(int) + /* aOrder array */
134868 sizeof(int) + /* aSpare array */
134869 sizeof(RtreeDValue) /* aDistance array */
134870 ));
134871 if( !aCell ){
134872 return SQLITE_NOMEM;
134873 }
134874 aOrder = (int *)&aCell[n];
134875 aSpare = (int *)&aOrder[n];
134876 aDistance = (RtreeDValue *)&aSpare[n];
134877
134878 for(ii=0; ii<nCell; ii++){
134879 if( ii==(nCell-1) ){
134880 memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
134881 }else{
134882 nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
134883 }
134884 aOrder[ii] = ii;
134885 for(iDim=0; iDim<pRtree->nDim; iDim++){
134886 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
134887 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
134888 }
134889 }
134890 for(iDim=0; iDim<pRtree->nDim; iDim++){
134891 aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
134892 }
134893
134894 for(ii=0; ii<nCell; ii++){
134895 aDistance[ii] = 0.0;
134896 for(iDim=0; iDim<pRtree->nDim; iDim++){
134897 RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
134898 DCOORD(aCell[ii].aCoord[iDim*2]));
134899 aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
134900 }
134901 }
134902
134903 SortByDistance(aOrder, nCell, aDistance, aSpare);
@@ -134949,20 +135135,23 @@
135135 if( nData>1 ){
135136 int ii;
135137
135138 /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
135139 assert( nData==(pRtree->nDim*2 + 3) );
135140 #ifndef SQLITE_RTREE_INT_ONLY
135141 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
135142 for(ii=0; ii<(pRtree->nDim*2); ii+=2){
135143 cell.aCoord[ii].f = (RtreeValue)sqlite3_value_double(azData[ii+3]);
135144 cell.aCoord[ii+1].f = (RtreeValue)sqlite3_value_double(azData[ii+4]);
135145 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
135146 rc = SQLITE_CONSTRAINT;
135147 goto constraint;
135148 }
135149 }
135150 }else
135151 #endif
135152 {
135153 for(ii=0; ii<(pRtree->nDim*2); ii+=2){
135154 cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
135155 cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
135156 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
135157 rc = SQLITE_CONSTRAINT;
@@ -135356,11 +135545,17 @@
135545
135546 nodeGetCell(&tree, &node, ii, &cell);
135547 sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
135548 nCell = (int)strlen(zCell);
135549 for(jj=0; jj<tree.nDim*2; jj++){
135550 #ifndef SQLITE_RTREE_INT_ONLY
135551 sqlite3_snprintf(512-nCell,&zCell[nCell], " %f",
135552 (double)cell.aCoord[jj].f);
135553 #else
135554 sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
135555 cell.aCoord[jj].i);
135556 #endif
135557 nCell = (int)strlen(zCell);
135558 }
135559
135560 if( zText ){
135561 char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
@@ -135398,11 +135593,15 @@
135593 rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
135594 if( rc==SQLITE_OK ){
135595 rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
135596 }
135597 if( rc==SQLITE_OK ){
135598 #ifdef SQLITE_RTREE_INT_ONLY
135599 void *c = (void *)RTREE_COORD_INT32;
135600 #else
135601 void *c = (void *)RTREE_COORD_REAL32;
135602 #endif
135603 rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
135604 }
135605 if( rc==SQLITE_OK ){
135606 void *c = (void *)RTREE_COORD_INT32;
135607 rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
@@ -135432,11 +135631,11 @@
135631 static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
135632 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
135633 RtreeMatchArg *pBlob;
135634 int nBlob;
135635
135636 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue);
135637 pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
135638 if( !pBlob ){
135639 sqlite3_result_error_nomem(ctx);
135640 }else{
135641 int i;
@@ -135443,11 +135642,15 @@
135642 pBlob->magic = RTREE_GEOMETRY_MAGIC;
135643 pBlob->xGeom = pGeomCtx->xGeom;
135644 pBlob->pContext = pGeomCtx->pContext;
135645 pBlob->nParam = nArg;
135646 for(i=0; i<nArg; i++){
135647 #ifdef SQLITE_RTREE_INT_ONLY
135648 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
135649 #else
135650 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
135651 #endif
135652 }
135653 sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
135654 }
135655 }
135656
@@ -135455,11 +135658,11 @@
135658 ** Register a new geometry function for use with the r-tree MATCH operator.
135659 */
135660 SQLITE_API int sqlite3_rtree_geometry_callback(
135661 sqlite3 *db,
135662 const char *zGeom,
135663 int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue *, int *),
135664 void *pContext
135665 ){
135666 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
135667
135668 /* Allocate and populate the context object. */
135669
+7 -3
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.7.12"
111111
#define SQLITE_VERSION_NUMBER 3007012
112
-#define SQLITE_SOURCE_ID "2012-03-31 19:12:23 af602d87736b52802a4e760ffeeaa28112b99d9a"
112
+#define SQLITE_SOURCE_ID "2012-04-17 09:09:33 8e2363ad76446e863d03ead91fd621e59d5cb495"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -1545,11 +1545,11 @@
15451545
** database connection is opened. By default, URI handling is globally
15461546
** disabled. The default value may be changed by compiling with the
15471547
** [SQLITE_USE_URI] symbol defined.
15481548
**
15491549
** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
1550
-** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFNIG_GETPCACHE
1550
+** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
15511551
** <dd> These options are obsolete and should not be used by new code.
15521552
** They are retained for backwards compatibility but are now no-ops.
15531553
** </dl>
15541554
*/
15551555
#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
@@ -6983,11 +6983,15 @@
69836983
** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
69846984
*/
69856985
SQLITE_API int sqlite3_rtree_geometry_callback(
69866986
sqlite3 *db,
69876987
const char *zGeom,
6988
- int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes),
6988
+#ifdef SQLITE_RTREE_INT_ONLY
6989
+ int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
6990
+#else
6991
+ int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
6992
+#endif
69896993
void *pContext
69906994
);
69916995
69926996
69936997
/*
69946998
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.12"
111 #define SQLITE_VERSION_NUMBER 3007012
112 #define SQLITE_SOURCE_ID "2012-03-31 19:12:23 af602d87736b52802a4e760ffeeaa28112b99d9a"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -1545,11 +1545,11 @@
1545 ** database connection is opened. By default, URI handling is globally
1546 ** disabled. The default value may be changed by compiling with the
1547 ** [SQLITE_USE_URI] symbol defined.
1548 **
1549 ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
1550 ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFNIG_GETPCACHE
1551 ** <dd> These options are obsolete and should not be used by new code.
1552 ** They are retained for backwards compatibility but are now no-ops.
1553 ** </dl>
1554 */
1555 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
@@ -6983,11 +6983,15 @@
6983 ** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
6984 */
6985 SQLITE_API int sqlite3_rtree_geometry_callback(
6986 sqlite3 *db,
6987 const char *zGeom,
6988 int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes),
 
 
 
 
6989 void *pContext
6990 );
6991
6992
6993 /*
6994
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.12"
111 #define SQLITE_VERSION_NUMBER 3007012
112 #define SQLITE_SOURCE_ID "2012-04-17 09:09:33 8e2363ad76446e863d03ead91fd621e59d5cb495"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -1545,11 +1545,11 @@
1545 ** database connection is opened. By default, URI handling is globally
1546 ** disabled. The default value may be changed by compiling with the
1547 ** [SQLITE_USE_URI] symbol defined.
1548 **
1549 ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
1550 ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
1551 ** <dd> These options are obsolete and should not be used by new code.
1552 ** They are retained for backwards compatibility but are now no-ops.
1553 ** </dl>
1554 */
1555 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
@@ -6983,11 +6983,15 @@
6983 ** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
6984 */
6985 SQLITE_API int sqlite3_rtree_geometry_callback(
6986 sqlite3 *db,
6987 const char *zGeom,
6988 #ifdef SQLITE_RTREE_INT_ONLY
6989 int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
6990 #else
6991 int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
6992 #endif
6993 void *pContext
6994 );
6995
6996
6997 /*
6998

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button