|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "sqlite3ext.h" |
|
|
SQLITE_EXTENSION_INIT1 |
|
|
#include <assert.h> |
|
|
#include <string.h> |
|
|
#include <limits.h> |
|
|
#include <math.h> |
|
|
|
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef unsigned char u8; |
|
|
typedef struct series_cursor series_cursor; |
|
|
struct series_cursor { |
|
|
sqlite3_vtab_cursor base; |
|
|
sqlite3_int64 iOBase; |
|
|
sqlite3_int64 iOTerm; |
|
|
sqlite3_int64 iOStep; |
|
|
sqlite3_int64 iBase; |
|
|
sqlite3_int64 iTerm; |
|
|
sqlite3_uint64 iStep; |
|
|
sqlite3_int64 iValue; |
|
|
u8 bDesc; |
|
|
u8 bDone; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static sqlite3_uint64 span64(sqlite3_int64 a, sqlite3_int64 b){ |
|
|
assert( a>=b ); |
|
|
return (*(sqlite3_uint64*)&a) - (*(sqlite3_uint64*)&b); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static sqlite3_int64 add64(sqlite3_int64 a, sqlite3_uint64 b){ |
|
|
sqlite3_uint64 x = *(sqlite3_uint64*)&a; |
|
|
x += b; |
|
|
return *(sqlite3_int64*)&x; |
|
|
} |
|
|
static sqlite3_int64 sub64(sqlite3_int64 a, sqlite3_uint64 b){ |
|
|
sqlite3_uint64 x = *(sqlite3_uint64*)&a; |
|
|
x -= b; |
|
|
return *(sqlite3_int64*)&x; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int seriesConnect( |
|
|
sqlite3 *db, |
|
|
void *pUnused, |
|
|
int argcUnused, const char *const*argvUnused, |
|
|
sqlite3_vtab **ppVtab, |
|
|
char **pzErrUnused |
|
|
){ |
|
|
sqlite3_vtab *pNew; |
|
|
int rc; |
|
|
|
|
|
|
|
|
#define SERIES_COLUMN_ROWID (-1) |
|
|
#define SERIES_COLUMN_VALUE 0 |
|
|
#define SERIES_COLUMN_START 1 |
|
|
#define SERIES_COLUMN_STOP 2 |
|
|
#define SERIES_COLUMN_STEP 3 |
|
|
|
|
|
(void)pUnused; |
|
|
(void)argcUnused; |
|
|
(void)argvUnused; |
|
|
(void)pzErrUnused; |
|
|
rc = sqlite3_declare_vtab(db, |
|
|
"CREATE TABLE x(value,start hidden,stop hidden,step hidden)"); |
|
|
if( rc==SQLITE_OK ){ |
|
|
pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) ); |
|
|
if( pNew==0 ) return SQLITE_NOMEM; |
|
|
memset(pNew, 0, sizeof(*pNew)); |
|
|
sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); |
|
|
} |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int seriesDisconnect(sqlite3_vtab *pVtab){ |
|
|
sqlite3_free(pVtab); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){ |
|
|
series_cursor *pCur; |
|
|
(void)pUnused; |
|
|
pCur = sqlite3_malloc( sizeof(*pCur) ); |
|
|
if( pCur==0 ) return SQLITE_NOMEM; |
|
|
memset(pCur, 0, sizeof(*pCur)); |
|
|
*ppCursor = &pCur->base; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int seriesClose(sqlite3_vtab_cursor *cur){ |
|
|
sqlite3_free(cur); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int seriesNext(sqlite3_vtab_cursor *cur){ |
|
|
series_cursor *pCur = (series_cursor*)cur; |
|
|
if( pCur->iValue==pCur->iTerm ){ |
|
|
pCur->bDone = 1; |
|
|
}else if( pCur->bDesc ){ |
|
|
pCur->iValue = sub64(pCur->iValue, pCur->iStep); |
|
|
assert( pCur->iValue>=pCur->iTerm ); |
|
|
}else{ |
|
|
pCur->iValue = add64(pCur->iValue, pCur->iStep); |
|
|
assert( pCur->iValue<=pCur->iTerm ); |
|
|
} |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int seriesColumn( |
|
|
sqlite3_vtab_cursor *cur, |
|
|
sqlite3_context *ctx, |
|
|
int i |
|
|
){ |
|
|
series_cursor *pCur = (series_cursor*)cur; |
|
|
sqlite3_int64 x = 0; |
|
|
switch( i ){ |
|
|
case SERIES_COLUMN_START: x = pCur->iOBase; break; |
|
|
case SERIES_COLUMN_STOP: x = pCur->iOTerm; break; |
|
|
case SERIES_COLUMN_STEP: x = pCur->iOStep; break; |
|
|
default: x = pCur->iValue; break; |
|
|
} |
|
|
sqlite3_result_int64(ctx, x); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
#ifndef LARGEST_UINT64 |
|
|
#define LARGEST_INT64 ((sqlite3_int64)0x7fffffffffffffffLL) |
|
|
#define LARGEST_UINT64 ((sqlite3_uint64)0xffffffffffffffffULL) |
|
|
#define SMALLEST_INT64 ((sqlite3_int64)0x8000000000000000LL) |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
|
|
series_cursor *pCur = (series_cursor*)cur; |
|
|
*pRowid = pCur->iValue; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int seriesEof(sqlite3_vtab_cursor *cur){ |
|
|
series_cursor *pCur = (series_cursor*)cur; |
|
|
return pCur->bDone; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef SQLITE_SERIES_CONSTRAINT_VERIFY |
|
|
# define SQLITE_SERIES_CONSTRAINT_VERIFY 0 |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static sqlite3_uint64 seriesSteps(series_cursor *pCur){ |
|
|
if( pCur->bDesc ){ |
|
|
assert( pCur->iBase >= pCur->iTerm ); |
|
|
return span64(pCur->iBase, pCur->iTerm)/pCur->iStep; |
|
|
}else{ |
|
|
assert( pCur->iBase <= pCur->iTerm ); |
|
|
return span64(pCur->iTerm, pCur->iBase)/pCur->iStep; |
|
|
} |
|
|
} |
|
|
|
|
|
#if defined(SQLITE_ENABLE_MATH_FUNCTIONS) || defined(_WIN32) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static double seriesCeil(double r){ return ceil(r); } |
|
|
static double seriesFloor(double r){ return floor(r); } |
|
|
#elif defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC) |
|
|
|
|
|
|
|
|
|
|
|
static double seriesCeil(double r){ return __builtin_ceil(r); } |
|
|
static double seriesFloor(double r){ return __builtin_floor(r); } |
|
|
#else |
|
|
|
|
|
|
|
|
|
|
|
static double seriesCeil(double r){ |
|
|
sqlite3_int64 x; |
|
|
if( r!=r ) return r; |
|
|
if( r<=(-4503599627370496.0) ) return r; |
|
|
if( r>=(+4503599627370496.0) ) return r; |
|
|
x = (sqlite3_int64)r; |
|
|
if( r==(double)x ) return r; |
|
|
if( r>(double)x ) x++; |
|
|
return (double)x; |
|
|
} |
|
|
static double seriesFloor(double r){ |
|
|
sqlite3_int64 x; |
|
|
if( r!=r ) return r; |
|
|
if( r<=(-4503599627370496.0) ) return r; |
|
|
if( r>=(+4503599627370496.0) ) return r; |
|
|
x = (sqlite3_int64)r; |
|
|
if( r==(double)x ) return r; |
|
|
if( r<(double)x ) x--; |
|
|
return (double)x; |
|
|
} |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int seriesFilter( |
|
|
sqlite3_vtab_cursor *pVtabCursor, |
|
|
int idxNum, const char *idxStrUnused, |
|
|
int argc, sqlite3_value **argv |
|
|
){ |
|
|
series_cursor *pCur = (series_cursor *)pVtabCursor; |
|
|
int iArg = 0; |
|
|
int i; |
|
|
sqlite3_int64 iMin = SMALLEST_INT64; |
|
|
sqlite3_int64 iMax = LARGEST_INT64; |
|
|
sqlite3_int64 iLimit = 0; |
|
|
sqlite3_int64 iOffset = 0; |
|
|
|
|
|
(void)idxStrUnused; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(i=0; i<argc; i++){ |
|
|
if( sqlite3_value_type(argv[i])==SQLITE_NULL ){ |
|
|
goto series_no_rows; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( idxNum & 0x01 ){ |
|
|
pCur->iOBase = sqlite3_value_int64(argv[iArg++]); |
|
|
}else{ |
|
|
pCur->iOBase = 0; |
|
|
} |
|
|
if( idxNum & 0x02 ){ |
|
|
pCur->iOTerm = sqlite3_value_int64(argv[iArg++]); |
|
|
}else{ |
|
|
pCur->iOTerm = 0xffffffff; |
|
|
} |
|
|
if( idxNum & 0x04 ){ |
|
|
pCur->iOStep = sqlite3_value_int64(argv[iArg++]); |
|
|
if( pCur->iOStep==0 ) pCur->iOStep = 1; |
|
|
}else{ |
|
|
pCur->iOStep = 1; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( (idxNum & 0x05)==0 && (idxNum & 0x0380)!=0 ){ |
|
|
pCur->iOBase = SMALLEST_INT64; |
|
|
} |
|
|
if( (idxNum & 0x06)==0 && (idxNum & 0x3080)!=0 ){ |
|
|
pCur->iOTerm = LARGEST_INT64; |
|
|
} |
|
|
pCur->iBase = pCur->iOBase; |
|
|
pCur->iTerm = pCur->iOTerm; |
|
|
if( pCur->iOStep>0 ){ |
|
|
pCur->iStep = pCur->iOStep; |
|
|
}else if( pCur->iOStep>SMALLEST_INT64 ){ |
|
|
pCur->iStep = -pCur->iOStep; |
|
|
}else{ |
|
|
pCur->iStep = LARGEST_INT64; |
|
|
pCur->iStep++; |
|
|
} |
|
|
pCur->bDesc = pCur->iOStep<0; |
|
|
if( pCur->bDesc==0 && pCur->iBase>pCur->iTerm ){ |
|
|
goto series_no_rows; |
|
|
} |
|
|
if( pCur->bDesc!=0 && pCur->iBase<pCur->iTerm ){ |
|
|
goto series_no_rows; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( idxNum & 0x20 ){ |
|
|
iLimit = sqlite3_value_int64(argv[iArg++]); |
|
|
if( idxNum & 0x40 ){ |
|
|
iOffset = sqlite3_value_int64(argv[iArg++]); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( idxNum & 0x3380 ){ |
|
|
if( idxNum & 0x0080 ){ |
|
|
if( sqlite3_value_numeric_type(argv[iArg])==SQLITE_FLOAT ){ |
|
|
double r = sqlite3_value_double(argv[iArg++]); |
|
|
if( r==seriesCeil(r) |
|
|
&& r>=(double)SMALLEST_INT64 |
|
|
&& r<=(double)LARGEST_INT64 |
|
|
){ |
|
|
iMin = iMax = (sqlite3_int64)r; |
|
|
}else{ |
|
|
goto series_no_rows; |
|
|
} |
|
|
}else{ |
|
|
iMin = iMax = sqlite3_value_int64(argv[iArg++]); |
|
|
} |
|
|
}else{ |
|
|
if( idxNum & 0x0300 ){ |
|
|
if( sqlite3_value_numeric_type(argv[iArg])==SQLITE_FLOAT ){ |
|
|
double r = sqlite3_value_double(argv[iArg++]); |
|
|
if( r<(double)SMALLEST_INT64 ){ |
|
|
iMin = SMALLEST_INT64; |
|
|
}else if( (idxNum & 0x0200)!=0 && r==seriesCeil(r) ){ |
|
|
iMin = (sqlite3_int64)seriesCeil(r+1.0); |
|
|
}else{ |
|
|
iMin = (sqlite3_int64)seriesCeil(r); |
|
|
} |
|
|
}else{ |
|
|
iMin = sqlite3_value_int64(argv[iArg++]); |
|
|
if( (idxNum & 0x0200)!=0 ){ |
|
|
if( iMin==LARGEST_INT64 ){ |
|
|
goto series_no_rows; |
|
|
}else{ |
|
|
iMin++; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
if( idxNum & 0x3000 ){ |
|
|
if( sqlite3_value_numeric_type(argv[iArg])==SQLITE_FLOAT ){ |
|
|
double r = sqlite3_value_double(argv[iArg++]); |
|
|
if( r>(double)LARGEST_INT64 ){ |
|
|
iMax = LARGEST_INT64; |
|
|
}else if( (idxNum & 0x2000)!=0 && r==seriesFloor(r) ){ |
|
|
iMax = (sqlite3_int64)(r-1.0); |
|
|
}else{ |
|
|
iMax = (sqlite3_int64)seriesFloor(r); |
|
|
} |
|
|
}else{ |
|
|
iMax = sqlite3_value_int64(argv[iArg++]); |
|
|
if( idxNum & 0x2000 ){ |
|
|
if( iMax==SMALLEST_INT64 ){ |
|
|
goto series_no_rows; |
|
|
}else{ |
|
|
iMax--; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
if( iMin>iMax ){ |
|
|
goto series_no_rows; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( pCur->bDesc==0 ){ |
|
|
if( pCur->iBase<iMin ){ |
|
|
sqlite3_uint64 span = span64(iMin,pCur->iBase); |
|
|
pCur->iBase = add64(pCur->iBase, (span/pCur->iStep)*pCur->iStep); |
|
|
if( pCur->iBase<iMin ){ |
|
|
if( pCur->iBase > sub64(LARGEST_INT64, pCur->iStep) ){ |
|
|
goto series_no_rows; |
|
|
} |
|
|
pCur->iBase = add64(pCur->iBase, pCur->iStep); |
|
|
} |
|
|
} |
|
|
if( pCur->iTerm>iMax ){ |
|
|
pCur->iTerm = iMax; |
|
|
} |
|
|
}else{ |
|
|
if( pCur->iBase>iMax ){ |
|
|
sqlite3_uint64 span = span64(pCur->iBase,iMax); |
|
|
pCur->iBase = sub64(pCur->iBase, (span/pCur->iStep)*pCur->iStep); |
|
|
if( pCur->iBase>iMax ){ |
|
|
if( pCur->iBase < add64(SMALLEST_INT64, pCur->iStep) ){ |
|
|
goto series_no_rows; |
|
|
} |
|
|
pCur->iBase = sub64(pCur->iBase, pCur->iStep); |
|
|
} |
|
|
} |
|
|
if( pCur->iTerm<iMin ){ |
|
|
pCur->iTerm = iMin; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if( pCur->bDesc==0 ){ |
|
|
if( pCur->iBase>pCur->iTerm ){ |
|
|
goto series_no_rows; |
|
|
} |
|
|
pCur->iTerm = sub64(pCur->iTerm, |
|
|
span64(pCur->iTerm,pCur->iBase) % pCur->iStep); |
|
|
}else{ |
|
|
if( pCur->iBase<pCur->iTerm ){ |
|
|
goto series_no_rows; |
|
|
} |
|
|
pCur->iTerm = add64(pCur->iTerm, |
|
|
span64(pCur->iBase,pCur->iTerm) % pCur->iStep); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( ((idxNum & 0x0008)!=0 && pCur->bDesc==0) |
|
|
|| ((idxNum & 0x0010)!=0 && pCur->bDesc!=0) |
|
|
){ |
|
|
sqlite3_int64 tmp = pCur->iBase; |
|
|
pCur->iBase = pCur->iTerm; |
|
|
pCur->iTerm = tmp; |
|
|
pCur->bDesc = !pCur->bDesc; |
|
|
} |
|
|
|
|
|
|
|
|
assert( pCur->iStep!=0 ); |
|
|
if( idxNum & 0x20 ){ |
|
|
if( iOffset>0 ){ |
|
|
if( seriesSteps(pCur) < (sqlite3_uint64)iOffset ){ |
|
|
goto series_no_rows; |
|
|
}else if( pCur->bDesc ){ |
|
|
pCur->iBase = sub64(pCur->iBase, pCur->iStep*iOffset); |
|
|
}else{ |
|
|
pCur->iBase = add64(pCur->iBase, pCur->iStep*iOffset); |
|
|
} |
|
|
} |
|
|
if( iLimit>=0 && seriesSteps(pCur) > (sqlite3_uint64)iLimit ){ |
|
|
pCur->iTerm = add64(pCur->iBase, (iLimit - 1)*pCur->iStep); |
|
|
} |
|
|
} |
|
|
pCur->iValue = pCur->iBase; |
|
|
pCur->bDone = 0; |
|
|
return SQLITE_OK; |
|
|
|
|
|
series_no_rows: |
|
|
pCur->iBase = 0; |
|
|
pCur->iTerm = 0; |
|
|
pCur->iStep = 1; |
|
|
pCur->bDesc = 0; |
|
|
pCur->bDone = 1; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int seriesBestIndex( |
|
|
sqlite3_vtab *pVTab, |
|
|
sqlite3_index_info *pIdxInfo |
|
|
){ |
|
|
int i, j; |
|
|
int idxNum = 0; |
|
|
#ifndef ZERO_ARGUMENT_GENERATE_SERIES |
|
|
int bStartSeen = 0; |
|
|
#endif |
|
|
int unusableMask = 0; |
|
|
int nArg = 0; |
|
|
int aIdx[7]; |
|
|
|
|
|
|
|
|
const struct sqlite3_index_constraint *pConstraint; |
|
|
|
|
|
|
|
|
|
|
|
assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 ); |
|
|
assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 ); |
|
|
|
|
|
aIdx[0] = aIdx[1] = aIdx[2] = aIdx[3] = aIdx[4] = aIdx[5] = aIdx[6] = -1; |
|
|
pConstraint = pIdxInfo->aConstraint; |
|
|
for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
|
|
int iCol; |
|
|
int iMask; |
|
|
int op = pConstraint->op; |
|
|
if( op>=SQLITE_INDEX_CONSTRAINT_LIMIT |
|
|
&& op<=SQLITE_INDEX_CONSTRAINT_OFFSET |
|
|
){ |
|
|
if( pConstraint->usable==0 ){ |
|
|
|
|
|
}else if( op==SQLITE_INDEX_CONSTRAINT_LIMIT ){ |
|
|
aIdx[3] = i; |
|
|
idxNum |= 0x20; |
|
|
}else{ |
|
|
assert( op==SQLITE_INDEX_CONSTRAINT_OFFSET ); |
|
|
aIdx[4] = i; |
|
|
idxNum |= 0x40; |
|
|
} |
|
|
continue; |
|
|
} |
|
|
if( pConstraint->iColumn<SERIES_COLUMN_START ){ |
|
|
if( (pConstraint->iColumn==SERIES_COLUMN_VALUE || |
|
|
pConstraint->iColumn==SERIES_COLUMN_ROWID) |
|
|
&& pConstraint->usable |
|
|
){ |
|
|
switch( op ){ |
|
|
case SQLITE_INDEX_CONSTRAINT_EQ: |
|
|
case SQLITE_INDEX_CONSTRAINT_IS: { |
|
|
idxNum |= 0x0080; |
|
|
idxNum &= ~0x3300; |
|
|
aIdx[5] = i; |
|
|
aIdx[6] = -1; |
|
|
#ifndef ZERO_ARGUMENT_GENERATE_SERIES |
|
|
bStartSeen = 1; |
|
|
#endif |
|
|
break; |
|
|
} |
|
|
case SQLITE_INDEX_CONSTRAINT_GE: { |
|
|
if( idxNum & 0x0080 ) break; |
|
|
idxNum |= 0x0100; |
|
|
idxNum &= ~0x0200; |
|
|
aIdx[5] = i; |
|
|
#ifndef ZERO_ARGUMENT_GENERATE_SERIES |
|
|
bStartSeen = 1; |
|
|
#endif |
|
|
break; |
|
|
} |
|
|
case SQLITE_INDEX_CONSTRAINT_GT: { |
|
|
if( idxNum & 0x0080 ) break; |
|
|
idxNum |= 0x0200; |
|
|
idxNum &= ~0x0100; |
|
|
aIdx[5] = i; |
|
|
#ifndef ZERO_ARGUMENT_GENERATE_SERIES |
|
|
bStartSeen = 1; |
|
|
#endif |
|
|
break; |
|
|
} |
|
|
case SQLITE_INDEX_CONSTRAINT_LE: { |
|
|
if( idxNum & 0x0080 ) break; |
|
|
idxNum |= 0x1000; |
|
|
idxNum &= ~0x2000; |
|
|
aIdx[6] = i; |
|
|
break; |
|
|
} |
|
|
case SQLITE_INDEX_CONSTRAINT_LT: { |
|
|
if( idxNum & 0x0080 ) break; |
|
|
idxNum |= 0x2000; |
|
|
idxNum &= ~0x1000; |
|
|
aIdx[6] = i; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
continue; |
|
|
} |
|
|
iCol = pConstraint->iColumn - SERIES_COLUMN_START; |
|
|
assert( iCol>=0 && iCol<=2 ); |
|
|
iMask = 1 << iCol; |
|
|
#ifndef ZERO_ARGUMENT_GENERATE_SERIES |
|
|
if( iCol==0 && op==SQLITE_INDEX_CONSTRAINT_EQ ){ |
|
|
bStartSeen = 1; |
|
|
} |
|
|
#endif |
|
|
if( pConstraint->usable==0 ){ |
|
|
unusableMask |= iMask; |
|
|
continue; |
|
|
}else if( op==SQLITE_INDEX_CONSTRAINT_EQ ){ |
|
|
idxNum |= iMask; |
|
|
aIdx[iCol] = i; |
|
|
} |
|
|
} |
|
|
if( aIdx[3]==0 ){ |
|
|
|
|
|
idxNum &= ~0x60; |
|
|
aIdx[4] = 0; |
|
|
} |
|
|
for(i=0; i<7; i++){ |
|
|
if( (j = aIdx[i])>=0 ){ |
|
|
pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg; |
|
|
pIdxInfo->aConstraintUsage[j].omit = |
|
|
!SQLITE_SERIES_CONSTRAINT_VERIFY || i>=3; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef ZERO_ARGUMENT_GENERATE_SERIES |
|
|
if( !bStartSeen ){ |
|
|
sqlite3_free(pVTab->zErrMsg); |
|
|
pVTab->zErrMsg = sqlite3_mprintf( |
|
|
"first argument to \"generate_series()\" missing or unusable"); |
|
|
return SQLITE_ERROR; |
|
|
} |
|
|
#endif |
|
|
if( (unusableMask & ~idxNum)!=0 ){ |
|
|
|
|
|
|
|
|
|
|
|
return SQLITE_CONSTRAINT; |
|
|
} |
|
|
if( (idxNum & 0x03)==0x03 ){ |
|
|
|
|
|
|
|
|
pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0)); |
|
|
pIdxInfo->estimatedRows = 1000; |
|
|
if( pIdxInfo->nOrderBy>=1 && pIdxInfo->aOrderBy[0].iColumn==0 ){ |
|
|
if( pIdxInfo->aOrderBy[0].desc ){ |
|
|
idxNum |= 0x08; |
|
|
}else{ |
|
|
idxNum |= 0x10; |
|
|
} |
|
|
pIdxInfo->orderByConsumed = 1; |
|
|
} |
|
|
}else if( (idxNum & 0x21)==0x21 ){ |
|
|
|
|
|
pIdxInfo->estimatedRows = 2500; |
|
|
}else{ |
|
|
|
|
|
|
|
|
|
|
|
pIdxInfo->estimatedRows = 2147483647; |
|
|
} |
|
|
pIdxInfo->idxNum = idxNum; |
|
|
#ifdef SQLITE_INDEX_SCAN_HEX |
|
|
pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_HEX; |
|
|
#endif |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static sqlite3_module seriesModule = { |
|
|
0, |
|
|
0, |
|
|
seriesConnect, |
|
|
seriesBestIndex, |
|
|
seriesDisconnect, |
|
|
0, |
|
|
seriesOpen, |
|
|
seriesClose, |
|
|
seriesFilter, |
|
|
seriesNext, |
|
|
seriesEof, |
|
|
seriesColumn, |
|
|
seriesRowid, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0 |
|
|
}; |
|
|
|
|
|
#endif |
|
|
|
|
|
#ifdef _WIN32 |
|
|
__declspec(dllexport) |
|
|
#endif |
|
|
int sqlite3_series_init( |
|
|
sqlite3 *db, |
|
|
char **pzErrMsg, |
|
|
const sqlite3_api_routines *pApi |
|
|
){ |
|
|
int rc = SQLITE_OK; |
|
|
SQLITE_EXTENSION_INIT2(pApi); |
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
if( sqlite3_libversion_number()<3008012 && pzErrMsg!=0 ){ |
|
|
*pzErrMsg = sqlite3_mprintf( |
|
|
"generate_series() requires SQLite 3.8.12 or later"); |
|
|
return SQLITE_ERROR; |
|
|
} |
|
|
rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0); |
|
|
#endif |
|
|
return rc; |
|
|
} |
|
|
|