]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add a prototype intarray($PTR,$N) table valued function.
authordrh <drh@noemail.net>
Wed, 29 Jun 2016 05:00:30 +0000 (05:00 +0000)
committerdrh <drh@noemail.net>
Wed, 29 Jun 2016 05:00:30 +0000 (05:00 +0000)
FossilOrigin-Name: 233b33382dc70de45f90b6dfdb5785f20b21489e

Makefile.in
ext/misc/array.c [new file with mode: 0644]
manifest
manifest.uuid
src/test1.c
test/tabfunc01.test

index b9f684e6171ff0f9fedc505b245274f8deb12a01..ff863ec906d71e614fcbd826c3baebdaec1048fb 100644 (file)
@@ -416,6 +416,7 @@ TESTSRC = \
 #
 TESTSRC += \
   $(TOP)/ext/misc/amatch.c \
+  $(TOP)/ext/misc/array.c \
   $(TOP)/ext/misc/closure.c \
   $(TOP)/ext/misc/csv.c \
   $(TOP)/ext/misc/eval.c \
diff --git a/ext/misc/array.c b/ext/misc/array.c
new file mode 100644 (file)
index 0000000..54b249f
--- /dev/null
@@ -0,0 +1,296 @@
+/*
+** 2016-06-29
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+**
+** This file demonstrates how to create a table-valued-function that
+** returns the values in a C-language array.
+** Examples:
+**
+**      SELECT * FROM intarray($ptr,5)
+**
+** The query above returns 5 integers contained in a C-language array
+** at the address $ptr.  $ptr is a pointer to the array of integers that
+** has been cast to an integer.
+**
+** HOW IT WORKS
+**
+** The intarray "function" is really a virtual table with the
+** following schema:
+**
+**     CREATE FUNCTION intarray(
+**       value,
+**       pointer HIDDEN,
+**       count HIDDEN
+**     );
+*/
+#include "sqlite3ext.h"
+SQLITE_EXTENSION_INIT1
+#include <assert.h>
+#include <string.h>
+
+#ifndef SQLITE_OMIT_VIRTUALTABLE
+
+
+/* intarray_cursor is a subclass of sqlite3_vtab_cursor which will
+** serve as the underlying representation of a cursor that scans
+** over rows of the result
+*/
+typedef struct intarray_cursor intarray_cursor;
+struct intarray_cursor {
+  sqlite3_vtab_cursor base;  /* Base class - must be first */
+  int isDesc;                /* True to count down rather than up */
+  sqlite3_int64 iRowid;      /* The rowid */
+  sqlite3_int64 iPtr;        /* Pointer to array of integers */
+  sqlite3_int64 iCnt;        /* Number of integers in the array */
+};
+
+/*
+** The intarrayConnect() method is invoked to create a new
+** intarray_vtab that describes the intarray virtual table.
+**
+** Think of this routine as the constructor for intarray_vtab objects.
+**
+** All this routine needs to do is:
+**
+**    (1) Allocate the intarray_vtab object and initialize all fields.
+**
+**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
+**        result set of queries against intarray will look like.
+*/
+static int intarrayConnect(
+  sqlite3 *db,
+  void *pAux,
+  int argc, const char *const*argv,
+  sqlite3_vtab **ppVtab,
+  char **pzErr
+){
+  sqlite3_vtab *pNew;
+  int rc;
+
+/* Column numbers */
+#define INTARRAY_COLUMN_VALUE   0
+#define INTARRAY_COLUMN_POINTER 1
+#define INTARRAY_COLUMN_COUNT   2
+
+  rc = sqlite3_declare_vtab(db,
+     "CREATE TABLE x(value,pointer hidden,count hidden)");
+  if( rc==SQLITE_OK ){
+    pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
+    if( pNew==0 ) return SQLITE_NOMEM;
+    memset(pNew, 0, sizeof(*pNew));
+  }
+  return rc;
+}
+
+/*
+** This method is the destructor for intarray_cursor objects.
+*/
+static int intarrayDisconnect(sqlite3_vtab *pVtab){
+  sqlite3_free(pVtab);
+  return SQLITE_OK;
+}
+
+/*
+** Constructor for a new intarray_cursor object.
+*/
+static int intarrayOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
+  intarray_cursor *pCur;
+  pCur = sqlite3_malloc( sizeof(*pCur) );
+  if( pCur==0 ) return SQLITE_NOMEM;
+  memset(pCur, 0, sizeof(*pCur));
+  *ppCursor = &pCur->base;
+  return SQLITE_OK;
+}
+
+/*
+** Destructor for a intarray_cursor.
+*/
+static int intarrayClose(sqlite3_vtab_cursor *cur){
+  sqlite3_free(cur);
+  return SQLITE_OK;
+}
+
+
+/*
+** Advance a intarray_cursor to its next row of output.
+*/
+static int intarrayNext(sqlite3_vtab_cursor *cur){
+  intarray_cursor *pCur = (intarray_cursor*)cur;
+  pCur->iRowid++;
+  return SQLITE_OK;
+}
+
+/*
+** Return values of columns for the row at which the intarray_cursor
+** is currently pointing.
+*/
+static int intarrayColumn(
+  sqlite3_vtab_cursor *cur,   /* The cursor */
+  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
+  int i                       /* Which column to return */
+){
+  intarray_cursor *pCur = (intarray_cursor*)cur;
+  sqlite3_int64 x = 0;
+  switch( i ){
+    case INTARRAY_COLUMN_POINTER:   x = pCur->iPtr;   break;
+    case INTARRAY_COLUMN_COUNT:     x = pCur->iCnt;   break;
+    default: {
+      int *p = (int*)pCur->iPtr;
+      x = (int)p[pCur->iRowid-1];
+      break;
+    }
+  }
+  sqlite3_result_int64(ctx, x);
+  return SQLITE_OK;
+}
+
+/*
+** Return the rowid for the current row.  In this implementation, the
+** rowid is the same as the output value.
+*/
+static int intarrayRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
+  intarray_cursor *pCur = (intarray_cursor*)cur;
+  *pRowid = pCur->iRowid;
+  return SQLITE_OK;
+}
+
+/*
+** Return TRUE if the cursor has been moved off of the last
+** row of output.
+*/
+static int intarrayEof(sqlite3_vtab_cursor *cur){
+  intarray_cursor *pCur = (intarray_cursor*)cur;
+  return pCur->iRowid>=pCur->iCnt;
+}
+
+/*
+** This method is called to "rewind" the intarray_cursor object back
+** to the first row of output.
+*/
+static int intarrayFilter(
+  sqlite3_vtab_cursor *pVtabCursor, 
+  int idxNum, const char *idxStr,
+  int argc, sqlite3_value **argv
+){
+  intarray_cursor *pCur = (intarray_cursor *)pVtabCursor;
+  int i = 0;
+  if( idxNum ){
+    pCur->iPtr = sqlite3_value_int64(argv[0]);
+    pCur->iCnt = sqlite3_value_int64(argv[1]);
+  }else{
+    pCur->iPtr = 0;
+    pCur->iCnt = 0;
+  }
+  pCur->iRowid = 1;
+  return SQLITE_OK;
+}
+
+/*
+** SQLite will invoke this method one or more times while planning a query
+** that uses the intarray virtual table.  This routine needs to create
+** a query plan for each invocation and compute an estimated cost for that
+** plan.
+**
+** In this implementation idxNum is used to represent the
+** query plan.  idxStr is unused.
+**
+** idxNum is 1 if the pointer= and count= constraints exist and is 0 otherwise.
+** If idxNum is 0, then intarray becomes an empty table.
+*/
+static int intarrayBestIndex(
+  sqlite3_vtab *tab,
+  sqlite3_index_info *pIdxInfo
+){
+  int i;                 /* Loop over constraints */
+  int idxNum = 0;        /* The query plan bitmask */
+  int ptrIdx = -1;       /* Index of the pointer= constraint, or -1 if none */
+  int cntIdx = -1;       /* Index of the count= constraint, or -1 if none */
+  int nArg = 0;          /* Number of arguments that intarrayFilter() expects */
+
+  const struct sqlite3_index_constraint *pConstraint;
+  pConstraint = pIdxInfo->aConstraint;
+  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
+    if( pConstraint->usable==0 ) continue;
+    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
+    switch( pConstraint->iColumn ){
+      case INTARRAY_COLUMN_POINTER:
+        ptrIdx = i;
+        break;
+      case INTARRAY_COLUMN_COUNT:
+        cntIdx = i;
+        break;
+    }
+  }
+  if( ptrIdx>=0 && cntIdx>=0 ){
+    pIdxInfo->aConstraintUsage[ptrIdx].argvIndex = 1;
+    pIdxInfo->aConstraintUsage[ptrIdx].omit = 1;
+    pIdxInfo->aConstraintUsage[cntIdx].argvIndex = 2;
+    pIdxInfo->aConstraintUsage[cntIdx].omit = 1;
+    pIdxInfo->estimatedCost = (double)1;
+    pIdxInfo->estimatedRows = (double)100;
+    pIdxInfo->idxNum = 1;
+  }else{
+    pIdxInfo->estimatedCost = (double)2147483647;
+    pIdxInfo->estimatedRows = (double)2147483647;
+    pIdxInfo->idxNum = 0;
+  }
+  return SQLITE_OK;
+}
+
+/*
+** This following structure defines all the methods for the 
+** intarray virtual table.
+*/
+static sqlite3_module intarrayModule = {
+  0,                         /* iVersion */
+  0,                         /* xCreate */
+  intarrayConnect,           /* xConnect */
+  intarrayBestIndex,         /* xBestIndex */
+  intarrayDisconnect,        /* xDisconnect */
+  0,                         /* xDestroy */
+  intarrayOpen,              /* xOpen - open a cursor */
+  intarrayClose,             /* xClose - close a cursor */
+  intarrayFilter,            /* xFilter - configure scan constraints */
+  intarrayNext,              /* xNext - advance a cursor */
+  intarrayEof,               /* xEof - check for end of scan */
+  intarrayColumn,            /* xColumn - read data */
+  intarrayRowid,             /* xRowid - read data */
+  0,                         /* xUpdate */
+  0,                         /* xBegin */
+  0,                         /* xSync */
+  0,                         /* xCommit */
+  0,                         /* xRollback */
+  0,                         /* xFindMethod */
+  0,                         /* xRename */
+};
+
+#endif /* SQLITE_OMIT_VIRTUALTABLE */
+
+#ifdef _WIN32
+__declspec(dllexport)
+#endif
+int sqlite3_array_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 = sqlite3_mprintf(
+        "intarray() requires SQLite 3.8.12 or later");
+    return SQLITE_ERROR;
+  }
+  rc = sqlite3_create_module(db, "intarray", &intarrayModule, 0);
+#endif
+  return rc;
+}
index 2dd253cf309ad9e7dac3e84c4e38a4385a81da3a..f6356d3145533e70215acada9d4d2804a1df61b3 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,6 +1,6 @@
-C Prevent\sthe\sWhereLoop.rSetup\scost\sestimate\sfrom\sgoing\snegative\son\scomplex\nqueries.
-D 2016-06-26T04:06:28.081
-F Makefile.in bc2b4864a23a4a21c3e26d7b4350f51bab324d45
+C Add\sa\sprototype\sintarray($PTR,$N)\stable\svalued\sfunction.
+D 2016-06-29T05:00:30.819
+F Makefile.in 541d493154ec3b0b20b2f1d495ec66f55905191e
 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
 F Makefile.msc 50149765ef72f4e652b9a0f1f6462c4784bb9423
 F README.md 8ecc12493ff9f820cdea6520a9016001cb2e59b7
@@ -204,6 +204,7 @@ F ext/icu/README.txt d9fbbad0c2f647c3fdf715fc9fd64af53aedfc43
 F ext/icu/icu.c 43df9d8ef2fae7a325100ebd713ab089dc829dd7
 F ext/icu/sqliteicu.h 728867a802baa5a96de7495e9689a8e01715ef37
 F ext/misc/amatch.c 211108e201105e4bb0c076527b8cfd34330fc234
+F ext/misc/array.c 20af0591e6611755dd8a9d1124e9c9a8cf42761f
 F ext/misc/closure.c 0d2a038df8fbae7f19de42e7c7d71f2e4dc88704
 F ext/misc/compress.c 122faa92d25033d6c3f07c39231de074ab3d2e83
 F ext/misc/csv.c f01126ba170fd4ef7c752b156568a80c912d4441
@@ -392,7 +393,7 @@ F src/sqliteLimit.h c0373387c287c8d0932510b5547ecde31b5da247
 F src/status.c 70912d7be68e9e2dbc4010c93d344af61d4c59ba
 F src/table.c 5226df15ab9179b9ed558d89575ea0ce37b03fc9
 F src/tclsqlite.c 25fbbbb97f76dbfd113153fb63f52d7ecfac5dd0
-F src/test1.c 43b37ab2b7338fd3313e74902f0d6c821eae843b
+F src/test1.c 081e4ed40525590406a51f7e7e4cee31cdb5d029
 F src/test2.c 5586f43fcd9a1be0830793cf9d354082c261b25b
 F src/test3.c c75c8af0eadb335236c9e61b51044c58a8f7dd59
 F src/test4.c d168f83cc78d02e8d35567bb5630e40dcd85ac1e
@@ -1114,7 +1115,7 @@ F test/symlink.test c9ebe7330d228249e447038276bfc8a7b22f4849
 F test/sync.test 2f84bdbc2b2df1fcb0220575b4b9f8cea94b7529
 F test/syscall.test f59ba4e25f7ba4a4c031026cc2ef8b6e4b4c639c
 F test/sysfault.test c9f2b0d8d677558f74de750c75e12a5454719d04
-F test/tabfunc01.test f977868fa8bb7beb4b2072883190411653473906
+F test/tabfunc01.test a1976cbc37cbcdd4b4bd1e52d19a173dd62ab9e0
 F test/table.test b708f3e5fa2542fa51dfab21fc07b36ea445cb2f
 F test/tableapi.test 2674633fa95d80da917571ebdd759a14d9819126
 F test/tableopts.test dba698ba97251017b7c80d738c198d39ab747930
@@ -1502,7 +1503,10 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 507014e4c7a70cd09410c89c8ed466c8edab39d2
-R ae55d782914cbf2db58c75f82f862316
+P f81050859170c8708a1b296da8dd3ef0dd314a11
+R 609a6d062299b01b515e433d814c02e8
+T *branch * prototype-int-array
+T *sym-prototype-int-array *
+T -sym-trunk *
 U drh
-Z e21bca843071316b56d3c29d5bf9cc08
+Z 45472ddb0e35914c318f364b2c8020fb
index ee49eb40ac05237c587fdfc17b4dbc2242b7eab8..2ff393eb5873f07ef2b60d0619d094a0ed38d344 100644 (file)
@@ -1 +1 @@
-f81050859170c8708a1b296da8dd3ef0dd314a11
\ No newline at end of file
+233b33382dc70de45f90b6dfdb5785f20b21489e
\ No newline at end of file
index 0f16d62d8504e72931e4bebf5a3ccee42208d611..9a73e0e0b59934bde3531de02b35643f3e6f9a7b 100644 (file)
@@ -3241,6 +3241,46 @@ static int test_bind_int(
 }
 
 
+/*
+** Usage:   sqlite3_bind_intarray  STMT N INT  ...
+**
+** Create a C-language array of integers from the arguments.  Bind a pointer
+** to this array to the NAME parameter of STMT.
+*/
+static int test_bind_intarray(
+  void * clientData,
+  Tcl_Interp *interp,
+  int objc,
+  Tcl_Obj *CONST objv[]
+){
+  sqlite3_stmt *pStmt;
+  int idx;
+  int i;
+  static int *p = 0;
+
+  sqlite3_free(p);
+  p = 0;
+  if( objc<4 ){
+    Tcl_AppendResult(interp, "wrong # args: should be \"",
+        Tcl_GetStringFromObj(objv[0], 0), " STMT NAME INT...", 0);
+    return TCL_ERROR;
+  }
+
+  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
+  if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
+  p = sqlite3_malloc( sizeof(int)*(objc-3) );
+  if( p==0 ) return TCL_ERROR;
+  for(i=0; i<objc-3; i++){
+    if( Tcl_GetIntFromObj(interp, objv[3+i], &p[i]) ){
+      sqlite3_free(p);
+      return TCL_ERROR;
+    }
+  }  
+  sqlite3_bind_int64(pStmt, idx, (sqlite3_int64)p);
+  return TCL_OK;
+}
+
+
 /*
 ** Usage:   sqlite3_bind_int64  STMT N VALUE
 **
@@ -6583,6 +6623,7 @@ static int tclLoadStaticExtensionCmd(
   Tcl_Obj *CONST objv[]
 ){
   extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*);
+  extern int sqlite3_array_init(sqlite3*,char**,const sqlite3_api_routines*);
   extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*);
   extern int sqlite3_csv_init(sqlite3*,char**,const sqlite3_api_routines*);
   extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*);
@@ -6601,6 +6642,7 @@ static int tclLoadStaticExtensionCmd(
     int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*);
   } aExtension[] = {
     { "amatch",                sqlite3_amatch_init               },
+    { "array",                 sqlite3_array_init                },
     { "closure",               sqlite3_closure_init              },
     { "csv",                   sqlite3_csv_init                  },
     { "eval",                  sqlite3_eval_init                 },
@@ -7096,6 +7138,7 @@ int Sqlitetest1_Init(Tcl_Interp *interp){
      { "register_dbstat_vtab",          test_register_dbstat_vtab  },
      { "sqlite3_connection_pointer",    get_sqlite_pointer, 0 },
      { "sqlite3_bind_int",              test_bind_int,      0 },
+     { "sqlite3_bind_intarray",         test_bind_intarray, 0 },
      { "sqlite3_bind_zeroblob",         test_bind_zeroblob, 0 },
      { "sqlite3_bind_zeroblob64",       test_bind_zeroblob64, 0 },
      { "sqlite3_bind_int64",            test_bind_int64,    0 },
index 879a045b8e9f2c5a238a6647cf810f67ae91bca9..473f3f385ce9db9d6f758f960e1e5c058c2816eb 100644 (file)
@@ -22,6 +22,7 @@ ifcapable !vtab {
   return
 }
 load_static_extension db series
+load_static_extension db array
 
 do_execsql_test tabfunc01-1.1 {
   SELECT *, '|' FROM generate_series WHERE start=1 AND stop=9 AND step=2;
@@ -135,4 +136,25 @@ do_execsql_test tabfunc01-500 {
   ORDER BY +1;
 } {1 7 11 17}
 
+
+do_test tabfunc01-600 {
+  set TAIL {}
+  set VM [sqlite3_prepare db {SELECT * FROM intarray(?2,?3)} -1 TAIL]
+  set TAIL
+} {}
+do_test tabfunc01-610 {
+  sqlite3_bind_intarray $VM 2 11 22 33 44 55
+  sqlite3_bind_int $VM 3 4
+  sqlite3_step $VM
+} SQLITE_ROW
+do_test tabfunc01-620 {
+  sqlite3_column_int $VM 0
+} 11
+do_test tabfunc01-621 {
+  sqlite3_step $VM
+  sqlite3_column_int $VM 0
+} 22
+sqlite3_finalize $VM
+catch {sqlite3_bind_intarray}
+
 finish_test