nish 0.45.1 → 0.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -88,6 +88,7 @@ typedef struct nishsqlite_shape {
88
88
  char *columns;
89
89
  u32 cookie;
90
90
  int32_t is_index, ncol, nkey, alias;
91
+ int32_t int_first; /* an index whose first key column has INTEGER or NUMERIC affinity */
91
92
  Pgno root;
92
93
  KeyInfo *ki; /* one reference, the entry's */
93
94
  } nishsqlite_shape;
@@ -186,6 +187,15 @@ static void slot_give(nishsqlite_table *t, int32_t h) {
186
187
 
187
188
  static void cursors_invalidate(nishsqlite_conn *c);
188
189
 
190
+ /* The connection's rollback hook. SQLite calls it at the end of every
191
+ * rollback of the connection's transaction, the ones it makes on its own
192
+ * among them: a statement that fails with SQLITE_NOMEM, SQLITE_IOERR or
193
+ * SQLITE_FULL rolls the whole transaction back, a plain SELECT included. Every
194
+ * other way a read can end (`nishsqlite_exec`, a statement that is not plain
195
+ * stepping, `nishsqlite_end_read`, `nishsqlite_close`) invalidates the cursors
196
+ * before it runs, so a cursor call checks nothing but its handle. */
197
+ static void nishsqlite_rolled_back(void *user) { cursors_invalidate(user); }
198
+
189
199
  /* Empties the connection's checked shapes. Before the close: a KeyInfo is
190
200
  * freed through its connection. */
191
201
  static void shapes_clear(nishsqlite_conn *c) {
@@ -250,6 +260,7 @@ int32_t nishsqlite_open(const char *path, int32_t flags) {
250
260
  c->db = db;
251
261
  c->self = h;
252
262
  sqlite3_set_authorizer(db, nishsqlite_authorize, c);
263
+ sqlite3_rollback_hook(db, nishsqlite_rolled_back, c);
253
264
  return h;
254
265
  }
255
266
  free(c);
@@ -512,11 +523,11 @@ int32_t nishsqlite_column_into(int32_t h, int32_t c, uint8_t *buf, uint64_t n, i
512
523
  * A cursor belongs to its connection (the slot's owner) and lives until the
513
524
  * program closes it, `nishsqlite_end_read`, or anything that could change what
514
525
  * it reads: a statement that is not plain stepping, `nishsqlite_exec`, the
515
- * authorizer seeing an action off its allowlist, or the transaction ending
516
- * under it (`cursor_of`). Invalidating frees the cursor and its slot, so its
517
- * handle answers SQLITE_MISUSE from then on. The moves and reads allocate
518
- * nothing once the first row has been read: the decoded row and the copy
519
- * buffer are the cursor's and are reused. */
526
+ * authorizer seeing an action off its allowlist, or SQLite rolling the
527
+ * transaction back on its own (`nishsqlite_rolled_back`). Invalidating frees
528
+ * the cursor and its slot, so its handle answers SQLITE_MISUSE from then on.
529
+ * The moves and reads allocate nothing once the first row has been read: the
530
+ * decoded row and the copy buffer are the cursor's and are reused. */
520
531
 
521
532
  #define NISH_SQLITE_SEEK_GE 1
522
533
  #define NISH_SQLITE_SEEK_GT 2
@@ -530,18 +541,26 @@ int32_t nishsqlite_column_into(int32_t h, int32_t c, uint8_t *buf, uint64_t n, i
530
541
 
531
542
  typedef struct nishsqlite_cursor {
532
543
  nishsqlite_conn *conn;
544
+ sqlite3_mutex *mutex; /* the connection's `db->mutex`, held around every call */
533
545
  Btree *btree; /* `main`, from sqlite3DbNameToBtree */
534
546
  BtCursor *bt; /* in the same block, after this struct */
535
547
  KeyInfo *ki; /* the record's fields, for the decoder and a seek */
536
- UnpackedRecord *row; /* the current row, decoded on the first read of it */
548
+ const u8 *rec; /* the current row's record, on its page or in `buf` */
549
+ u32 *ftype; /* its fields' serial types, read from its header on the first read */
550
+ u32 *foff; /* and where each field's bytes start in `rec` */
537
551
  Mem *key; /* the seek key's `nkeyfields` values, in the block */
552
+ UnpackedRecord seek; /* the key a seek or count hands SQLite: `key`, under `ki` */
538
553
  int32_t nkeyparts; /* how many of them are set */
539
554
  int32_t nkeyfields; /* an index's key columns; a table's columns */
540
555
  u8 *buf; /* a record that is not all on its page, copied */
541
556
  u32 cap;
557
+ int64_t *counts; /* the last `count_range`'s answer, per key */
558
+ uint64_t ncounts; /* how many of them it answered */
559
+ uint64_t counts_cap;
542
560
  int32_t ncol; /* columns the program reads: a table's, or an index's key and rowid */
543
561
  int32_t alias; /* a table's INTEGER PRIMARY KEY column, -1 when none */
544
562
  int32_t is_index;
563
+ int32_t int_first; /* an index whose first key column stores integers as integers */
545
564
  int32_t has_row; /* the last move answered a row */
546
565
  int32_t unpacked; /* `row` holds the current row */
547
566
  int32_t nfields; /* the fields the current row's record holds */
@@ -551,9 +570,9 @@ static void cursor_free(nishsqlite_cursor *cur) {
551
570
  sqlite3 *db = cur->conn->db;
552
571
  sqlite3BtreeCloseCursor(cur->bt);
553
572
  for (int32_t i = 0; i < cur->nkeyfields; i++) sqlite3VdbeMemRelease(&cur->key[i]);
554
- if (cur->row) sqlite3DbFree(db, cur->row);
555
573
  sqlite3KeyInfoUnref(cur->ki);
556
574
  sqlite3_free(cur->buf);
575
+ sqlite3_free(cur->counts);
557
576
  sqlite3_free(cur);
558
577
  }
559
578
 
@@ -572,18 +591,13 @@ static void cursors_invalidate(nishsqlite_conn *c) {
572
591
  sqlite3_mutex_leave(mx);
573
592
  }
574
593
 
575
- /* The cursor `h` names, or NULL. A cursor whose transaction has ended, by
576
- * whatever route, is invalidated here with every other cursor of its
577
- * connection before anything reads it. */
594
+ /* The cursor `h` names, or NULL: the one check a cursor call makes. A cursor
595
+ * whose read has ended, by whatever route, is already gone from its slot,
596
+ * because every route invalidates the connection's cursors as it ends the read
597
+ * (`nishsqlite_rolled_back`), so a live slot is a cursor inside its read. */
578
598
  static inline nishsqlite_cursor *cursor_of(int32_t h) {
579
599
  nishsqlite_slot *s = slot_of(&nishsqlite_cursors, h);
580
- if (s == NULL) return NULL;
581
- nishsqlite_cursor *cur = s->p;
582
- if (sqlite3_get_autocommit(cur->conn->db) || sqlite3BtreeTxnState(cur->btree) == SQLITE_TXN_NONE) {
583
- cursors_invalidate(cur->conn);
584
- return NULL;
585
- }
586
- return cur;
600
+ return s ? s->p : NULL;
587
601
  }
588
602
 
589
603
  /* Runs the adapter's own SQL, which the authorizer lets through unwatched. */
@@ -821,7 +835,7 @@ static int main_is_utf8(nishsqlite_conn *c) {
821
835
  * has other columns, and SQLITE_NOMEM, all negated. */
822
836
  static int32_t cursor_build(nishsqlite_conn *c, const char *name, const char *columns) {
823
837
  sqlite3 *db = c->db;
824
- int32_t is_index = 0, ncol = 0, alias = -1, nkey = 0;
838
+ int32_t is_index = 0, ncol = 0, alias = -1, nkey = 0, int_first = 0;
825
839
  Pgno root = 0;
826
840
  char *table = NULL;
827
841
  KeyInfo *ki = NULL;
@@ -836,6 +850,7 @@ static int32_t cursor_build(nishsqlite_conn *c, const char *name, const char *co
836
850
  ncol = s->ncol;
837
851
  nkey = s->nkey;
838
852
  alias = s->alias;
853
+ int_first = s->int_first;
839
854
  root = s->root;
840
855
  ki = sqlite3KeyInfoRef(s->ki);
841
856
  goto build;
@@ -856,6 +871,14 @@ static int32_t cursor_build(nishsqlite_conn *c, const char *name, const char *co
856
871
  rc = ki ? index_shape(c, name, want, &nkey, ki) : SQLITE_NOMEM;
857
872
  ncol = nkey + 1;
858
873
  }
874
+ /* A first key column with INTEGER or NUMERIC affinity holds an integer
875
+ * value as an integer, so `count_range`'s integer compare agrees with SQL's
876
+ * `col = k`; TEXT affinity would compare k as text. */
877
+ Index *ix = rc == SQLITE_OK ? sqlite3FindIndex(db, name, "main") : NULL;
878
+ if (ix) {
879
+ char aff = sqlite3TableColumnAffinity(ix->pTable, ix->aiColumn[0]);
880
+ int_first = aff == SQLITE_AFF_INTEGER || aff == SQLITE_AFF_NUMERIC;
881
+ }
859
882
  }
860
883
  sqlite3_free(table);
861
884
  if (rc == SQLITE_OK && sqlite3_str_errcode(want) != SQLITE_OK) rc = SQLITE_NOMEM;
@@ -874,7 +897,7 @@ static int32_t cursor_build(nishsqlite_conn *c, const char *name, const char *co
874
897
  sqlite3_free(s->name);
875
898
  sqlite3_free(s->columns);
876
899
  sqlite3KeyInfoUnref(s->ki);
877
- *s = (nishsqlite_shape){n, cols, cookie, is_index, ncol, nkey, alias, root, sqlite3KeyInfoRef(ki)};
900
+ *s = (nishsqlite_shape){n, cols, cookie, is_index, ncol, nkey, alias, int_first, root, sqlite3KeyInfoRef(ki)};
878
901
  c->shape_next = (c->shape_next + 1) % NISH_SQLITE_SHAPES;
879
902
  } else {
880
903
  sqlite3_free(n);
@@ -886,26 +909,33 @@ build:;
886
909
  int32_t nkeyfields = is_index ? nkey : ncol;
887
910
  size_t head = ROUND8(sizeof(nishsqlite_cursor));
888
911
  size_t btsz = ROUND8((size_t)sqlite3BtreeCursorSize());
889
- nishsqlite_cursor *cur = sqlite3_malloc64(head + btsz + (size_t)nkeyfields * sizeof(Mem));
912
+ size_t keysz = ROUND8((size_t)nkeyfields * sizeof(Mem));
913
+ /* Room for a record's fields: the key's, and an index entry's rowid after them. */
914
+ size_t fieldsz = (size_t)(nkeyfields + 1) * sizeof(u32);
915
+ nishsqlite_cursor *cur = sqlite3_malloc64(head + btsz + keysz + 2 * fieldsz);
890
916
  if (cur == NULL) {
891
917
  sqlite3KeyInfoUnref(ki);
892
918
  return -SQLITE_NOMEM;
893
919
  }
894
920
  memset(cur, 0, head);
895
921
  cur->conn = c;
922
+ cur->mutex = sqlite3_db_mutex(db);
896
923
  cur->btree = sqlite3DbNameToBtree(db, "main");
897
924
  cur->bt = (BtCursor *)((u8 *)cur + head);
898
925
  sqlite3BtreeCursorZero(cur->bt);
899
926
  cur->ki = ki;
900
927
  cur->key = (Mem *)((u8 *)cur + head + btsz);
928
+ cur->ftype = (u32 *)((u8 *)cur + head + btsz + keysz);
929
+ cur->foff = cur->ftype + nkeyfields + 1;
901
930
  cur->nkeyfields = nkeyfields;
902
931
  for (int32_t i = 0; i < nkeyfields; i++) sqlite3VdbeMemInit(&cur->key[i], db, MEM_Null);
903
932
  cur->ncol = ncol;
904
933
  cur->alias = alias;
905
934
  cur->is_index = is_index;
906
- cur->row = sqlite3VdbeAllocUnpackedRecord(ki);
907
- rc = cur->row ? SQLITE_OK : SQLITE_NOMEM;
908
- if (rc == SQLITE_OK) rc = sqlite3BtreeCursor(cur->btree, root, 0, is_index ? ki : NULL, cur->bt);
935
+ cur->int_first = int_first;
936
+ cur->seek.pKeyInfo = ki;
937
+ cur->seek.aMem = cur->key;
938
+ rc = sqlite3BtreeCursor(cur->btree, root, 0, is_index ? ki : NULL, cur->bt);
909
939
  int32_t hc = rc == SQLITE_OK ? slot_take(&nishsqlite_cursors, cur, c->self) : 0;
910
940
  if (hc == 0) {
911
941
  cursor_free(cur);
@@ -921,8 +951,7 @@ static int32_t unlocked_cursor_open(int32_t h, const char *name, const char *col
921
951
  return cursor_build(c, name, columns);
922
952
  }
923
953
 
924
- static int32_t unlocked_cursor_close(int32_t h) {
925
- nishsqlite_cursor *cur = cursor_of(h);
954
+ static int32_t unlocked_cursor_close(nishsqlite_cursor *cur, int32_t h) {
926
955
  if (cur == NULL) return SQLITE_MISUSE;
927
956
  cur->conn->ncursors--;
928
957
  cursor_free(cur);
@@ -940,16 +969,14 @@ static int32_t moved(nishsqlite_cursor *cur, int rc, int row) {
940
969
  return rc == SQLITE_OK ? cur->has_row : -rc;
941
970
  }
942
971
 
943
- static int32_t unlocked_cursor_first(int32_t h) {
944
- nishsqlite_cursor *cur = cursor_of(h);
972
+ static int32_t unlocked_cursor_first(nishsqlite_cursor *cur) {
945
973
  if (cur == NULL) return -SQLITE_MISUSE;
946
974
  int empty = 1;
947
975
  int rc = sqlite3BtreeFirst(cur->bt, &empty);
948
976
  return moved(cur, rc, !empty);
949
977
  }
950
978
 
951
- static int32_t unlocked_cursor_last(int32_t h) {
952
- nishsqlite_cursor *cur = cursor_of(h);
979
+ static int32_t unlocked_cursor_last(nishsqlite_cursor *cur) {
953
980
  if (cur == NULL) return -SQLITE_MISUSE;
954
981
  int empty = 1;
955
982
  int rc = sqlite3BtreeLast(cur->bt, &empty);
@@ -963,22 +990,19 @@ static int32_t stepped(nishsqlite_cursor *cur, int rc) {
963
990
 
964
991
  /* Next and previous need a row to move from: with none they are
965
992
  * SQLITE_MISUSE, so a loop cannot run past its end and restart. */
966
- static int32_t unlocked_cursor_next(int32_t h) {
967
- nishsqlite_cursor *cur = cursor_of(h);
993
+ static int32_t unlocked_cursor_next(nishsqlite_cursor *cur) {
968
994
  if (cur == NULL || !cur->has_row) return -SQLITE_MISUSE;
969
995
  return stepped(cur, sqlite3BtreeNext(cur->bt, 0));
970
996
  }
971
997
 
972
- static int32_t unlocked_cursor_previous(int32_t h) {
973
- nishsqlite_cursor *cur = cursor_of(h);
998
+ static int32_t unlocked_cursor_previous(nishsqlite_cursor *cur) {
974
999
  if (cur == NULL || !cur->has_row) return -SQLITE_MISUSE;
975
1000
  return stepped(cur, sqlite3BtreePrevious(cur->bt, 0));
976
1001
  }
977
1002
 
978
1003
  /* Moves a table cursor to the row `rowid`: 1 when it is there, 0 when not.
979
1004
  * An index cursor is SQLITE_MISUSE. */
980
- static int32_t unlocked_cursor_seek_rowid(int32_t h, int64_t rowid) {
981
- nishsqlite_cursor *cur = cursor_of(h);
1005
+ static int32_t unlocked_cursor_seek_rowid(nishsqlite_cursor *cur, int64_t rowid) {
982
1006
  if (cur == NULL || cur->is_index) return -SQLITE_MISUSE;
983
1007
  int res = -1;
984
1008
  int rc = sqlite3BtreeTableMoveto(cur->bt, rowid, 0, &res);
@@ -987,36 +1011,34 @@ static int32_t unlocked_cursor_seek_rowid(int32_t h, int64_t rowid) {
987
1011
 
988
1012
  /* ---- Seek keys ---------------------------------------------------------- */
989
1013
 
990
- /* The index cursor `h` with room for one more key part, or NULL with `*rc`
991
- * saying why. The caller sets `cur->key[cur->nkeyparts]` and counts it only
992
- * when the set succeeds, so a failed part leaves the key as it was. */
993
- static nishsqlite_cursor *key_next(int32_t h, int32_t *rc) {
994
- nishsqlite_cursor *cur = cursor_of(h);
1014
+ /* `cur` when it is an index cursor with room for one more key part, or NULL
1015
+ * with `*rc` saying why. The caller sets `cur->key[cur->nkeyparts]` and counts
1016
+ * it only when the set succeeds, so a failed part leaves the key as it was. */
1017
+ static nishsqlite_cursor *key_next(nishsqlite_cursor *cur, int32_t *rc) {
995
1018
  *rc = cur == NULL || !cur->is_index ? SQLITE_MISUSE : SQLITE_RANGE;
996
1019
  if (cur == NULL || !cur->is_index || cur->nkeyparts >= cur->nkeyfields) return NULL;
997
1020
  *rc = SQLITE_OK;
998
1021
  return cur;
999
1022
  }
1000
1023
 
1001
- static int32_t unlocked_key_reset(int32_t h) {
1002
- nishsqlite_cursor *cur = cursor_of(h);
1024
+ static int32_t unlocked_key_reset(nishsqlite_cursor *cur) {
1003
1025
  if (cur == NULL || !cur->is_index) return SQLITE_MISUSE;
1004
1026
  for (int32_t i = 0; i < cur->nkeyparts; i++) sqlite3VdbeMemSetNull(&cur->key[i]);
1005
1027
  cur->nkeyparts = 0;
1006
1028
  return SQLITE_OK;
1007
1029
  }
1008
1030
 
1009
- static int32_t unlocked_key_i64(int32_t h, int64_t v) {
1031
+ static int32_t unlocked_key_i64(nishsqlite_cursor *cur, int64_t v) {
1010
1032
  int32_t rc;
1011
- nishsqlite_cursor *cur = key_next(h, &rc);
1033
+ cur = key_next(cur, &rc);
1012
1034
  if (cur) sqlite3VdbeMemSetInt64(&cur->key[cur->nkeyparts++], v);
1013
1035
  return rc;
1014
1036
  }
1015
1037
 
1016
1038
  /* Text values are copied into the key (SQLITE_TRANSIENT), as a bind is. */
1017
- static int32_t unlocked_key_text(int32_t h, const char *text) {
1039
+ static int32_t unlocked_key_text(nishsqlite_cursor *cur, const char *text) {
1018
1040
  int32_t rc;
1019
- nishsqlite_cursor *cur = key_next(h, &rc);
1041
+ cur = key_next(cur, &rc);
1020
1042
  if (cur) rc = sqlite3VdbeMemSetStr(&cur->key[cur->nkeyparts], text, -1, SQLITE_UTF8, SQLITE_TRANSIENT);
1021
1043
  if (cur && rc == SQLITE_OK) cur->nkeyparts++;
1022
1044
  return rc;
@@ -1024,34 +1046,38 @@ static int32_t unlocked_key_text(int32_t h, const char *text) {
1024
1046
 
1025
1047
  /* `buf[off, off + len)` as text, `n` the buffer's length; a window outside
1026
1048
  * the buffer is SQLITE_MISUSE, as `nishsqlite_bind_bytes` answers. */
1027
- static int32_t unlocked_key_bytes(int32_t h, const uint8_t *buf, uint64_t n, int32_t off, int32_t len) {
1049
+ static int32_t unlocked_key_bytes(nishsqlite_cursor *cur, const uint8_t *buf, uint64_t n, int32_t off, int32_t len) {
1028
1050
  if (off < 0 || len < 0 || (uint64_t)off + (uint64_t)len > n) return SQLITE_MISUSE;
1029
1051
  int32_t rc;
1030
- nishsqlite_cursor *cur = key_next(h, &rc);
1052
+ cur = key_next(cur, &rc);
1031
1053
  const char *p = len == 0 ? "" : (const char *)(buf + off);
1032
1054
  if (cur) rc = sqlite3VdbeMemSetStr(&cur->key[cur->nkeyparts], p, len, SQLITE_UTF8, SQLITE_TRANSIENT);
1033
1055
  if (cur && rc == SQLITE_OK) cur->nkeyparts++;
1034
1056
  return rc;
1035
1057
  }
1036
1058
 
1059
+ /* The kept key, made at open, readied for a compare of its first `nfield`
1060
+ * values: an integer seek sets its Mems in place and builds no record. */
1061
+ static UnpackedRecord *seek_key(nishsqlite_cursor *cur, int32_t nfield, int default_rc) {
1062
+ UnpackedRecord *k = &cur->seek;
1063
+ k->nField = (u16)nfield;
1064
+ k->default_rc = (i8)default_rc;
1065
+ k->errCode = 0;
1066
+ k->eqSeen = 0;
1067
+ return k;
1068
+ }
1069
+
1037
1070
  /* Moves an index cursor to the first entry at or past the key (`op` GE or
1038
1071
  * GT), or the last at or before it (LE or LT), comparing only the key's
1039
1072
  * values, so a key shorter than the index is a prefix. This is the VDBE's
1040
1073
  * OP_SeekGE/GT/LE/LT: the record's default answer for an equal prefix decides
1041
1074
  * which side of the equal run the B-tree lands on, and one step corrects it. */
1042
- static int32_t unlocked_cursor_seek(int32_t h, int32_t op) {
1043
- nishsqlite_cursor *cur = cursor_of(h);
1075
+ static int32_t unlocked_cursor_seek(nishsqlite_cursor *cur, int32_t op) {
1044
1076
  if (cur == NULL || !cur->is_index || cur->nkeyparts < 1 || op < NISH_SQLITE_SEEK_GE ||
1045
1077
  op > NISH_SQLITE_SEEK_LT)
1046
1078
  return -SQLITE_MISUSE;
1047
- UnpackedRecord key;
1048
- memset(&key, 0, sizeof key);
1049
- key.pKeyInfo = cur->ki;
1050
- key.aMem = cur->key;
1051
- key.nField = (u16)cur->nkeyparts;
1052
- key.default_rc = op == NISH_SQLITE_SEEK_GT || op == NISH_SQLITE_SEEK_LE ? -1 : 1;
1053
1079
  int res = 0;
1054
- int rc = sqlite3BtreeIndexMoveto(cur->bt, &key, &res);
1080
+ int rc = sqlite3BtreeIndexMoveto(cur->bt, seek_key(cur, cur->nkeyparts, op == NISH_SQLITE_SEEK_GT || op == NISH_SQLITE_SEEK_LE ? -1 : 1), &res);
1055
1081
  if (rc != SQLITE_OK) return moved(cur, rc, 0);
1056
1082
  if (op == NISH_SQLITE_SEEK_GE || op == NISH_SQLITE_SEEK_GT) {
1057
1083
  if (res < 0 || (res == 0 && op == NISH_SQLITE_SEEK_GT)) return stepped(cur, sqlite3BtreeNext(cur->bt, 0));
@@ -1063,123 +1089,304 @@ static int32_t unlocked_cursor_seek(int32_t h, int32_t op) {
1063
1089
 
1064
1090
  /* ---- Cursor columns ----------------------------------------------------- */
1065
1091
 
1066
- /* The current row's record decoded into `row`, once per row. A record that
1067
- * is all on its page is decoded where it lies; one that spills onto overflow
1068
- * pages is copied whole into the cursor's buffer first, with zeroed slack
1069
- * past it. SQLITE_MISUSE with no row, SQLITE_CORRUPT for a record longer than
1070
- * SQLite allows. */
1092
+ /* The current entry's record: where it lies when it is all on its page,
1093
+ * else copied whole into the cursor's buffer with zeroed slack past it.
1094
+ * SQLITE_CORRUPT for a record longer than SQLite allows. */
1095
+ static int record_of(nishsqlite_cursor *cur, u32 *sz, const void **p) {
1096
+ *sz = sqlite3BtreePayloadSize(cur->bt);
1097
+ if (*sz > SQLITE_MAX_LENGTH) return SQLITE_CORRUPT;
1098
+ u32 amt = 0;
1099
+ *p = sqlite3BtreePayloadFetch(cur->bt, &amt);
1100
+ if (amt >= *sz) return SQLITE_OK;
1101
+ if (cur->cap < *sz + NISH_SQLITE_SLACK) {
1102
+ u64 cap = (u64)cur->cap * 2;
1103
+ if (cap < (u64)*sz + NISH_SQLITE_SLACK) cap = (u64)*sz + NISH_SQLITE_SLACK;
1104
+ u8 *buf = sqlite3_realloc64(cur->buf, cap);
1105
+ if (buf == NULL) return SQLITE_NOMEM;
1106
+ cur->buf = buf;
1107
+ cur->cap = (u32)cap;
1108
+ }
1109
+ int rc = sqlite3BtreePayload(cur->bt, 0, *sz, cur->buf);
1110
+ if (rc != SQLITE_OK) return rc;
1111
+ memset(cur->buf + *sz, 0, NISH_SQLITE_SLACK);
1112
+ *p = cur->buf;
1113
+ return SQLITE_OK;
1114
+ }
1115
+
1116
+ /* The current row's record header read into `ftype` and `foff`, once per
1117
+ * row: each field's serial type and where its bytes start. The walk is
1118
+ * `sqlite3VdbeRecordUnpack`'s, bound for bound, so it finds the fields that
1119
+ * function would and no others, and like it a record whose last field runs
1120
+ * past its end reads that field as NULL. What it skips is building a Mem
1121
+ * per field: a column is decoded from its serial type when it is read
1122
+ * (`field_value`). SQLITE_MISUSE with no row, SQLITE_CORRUPT for a record
1123
+ * longer than SQLite allows. */
1071
1124
  static int row_of(nishsqlite_cursor *cur) {
1072
1125
  if (!cur->has_row) return SQLITE_MISUSE;
1073
1126
  if (cur->unpacked) return SQLITE_OK;
1074
1127
  if (sqlite3BtreeCursorHasMoved(cur->bt)) return SQLITE_ABORT;
1075
- u32 sz = sqlite3BtreePayloadSize(cur->bt);
1076
- if (sz > SQLITE_MAX_LENGTH) return SQLITE_CORRUPT;
1128
+ u32 sz = 0;
1129
+ const void *p = NULL;
1130
+ int rc = record_of(cur, &sz, &p);
1131
+ if (rc != SQLITE_OK) return rc;
1077
1132
  cur->nfields = 0;
1133
+ cur->rec = p;
1078
1134
  if (sz > 0) {
1079
- u32 amt = 0;
1080
- const void *p = sqlite3BtreePayloadFetch(cur->bt, &amt);
1081
- if (amt < sz) {
1082
- if (cur->cap < sz + NISH_SQLITE_SLACK) {
1083
- u64 cap = (u64)cur->cap * 2;
1084
- if (cap < (u64)sz + NISH_SQLITE_SLACK) cap = (u64)sz + NISH_SQLITE_SLACK;
1085
- u8 *buf = sqlite3_realloc64(cur->buf, cap);
1086
- if (buf == NULL) return SQLITE_NOMEM;
1087
- cur->buf = buf;
1088
- cur->cap = (u32)cap;
1089
- }
1090
- int rc = sqlite3BtreePayload(cur->bt, 0, sz, cur->buf);
1091
- if (rc != SQLITE_OK) return rc;
1092
- memset(cur->buf + sz, 0, NISH_SQLITE_SLACK);
1093
- p = cur->buf;
1135
+ const u8 *a = p;
1136
+ u32 hdr, idx, d, u = 0, nfield = (u32)cur->nkeyfields + 1;
1137
+ idx = getVarint32(a, hdr);
1138
+ d = hdr;
1139
+ while (idx < hdr && d <= sz) {
1140
+ u32 t;
1141
+ idx += getVarint32(&a[idx], t);
1142
+ cur->ftype[u] = t;
1143
+ cur->foff[u] = d;
1144
+ d += sqlite3VdbeSerialTypeLen(t);
1145
+ if (++u >= nfield) break;
1094
1146
  }
1095
- cur->row->nField = (u16)(cur->nkeyfields + 1);
1096
- sqlite3VdbeRecordUnpack((int)sz, p, cur->row);
1097
- cur->nfields = cur->row->nField;
1147
+ if (d > sz && u) cur->ftype[u - 1] = 0;
1148
+ cur->nfields = (int32_t)u;
1098
1149
  }
1099
1150
  cur->unpacked = 1;
1100
1151
  return SQLITE_OK;
1101
1152
  }
1102
1153
 
1103
- /* Column `n` of the current row: NULL with `*rc` set when it cannot be read.
1104
- * The INTEGER PRIMARY KEY column is the row's key, not a field of its record,
1105
- * so it answers NULL with `*alias` set. A row whose record holds fewer fields
1106
- * than `n` (one written before ALTER TABLE added the column) is SQLITE_SCHEMA. */
1107
- static Mem *column_mem(int32_t h, int32_t n, nishsqlite_cursor **out, int *alias, int *rc) {
1108
- nishsqlite_cursor *cur = cursor_of(h);
1109
- *out = cur;
1154
+ /* Field `n` of the current row's record, which `row_of` has read: its type
1155
+ * as `sqlite3_value_type` answers it, with `*v` as `column_value` describes.
1156
+ * Integers, NULL and text or blob bytes are read in place, as
1157
+ * `sqlite3VdbeSerialGet` reads them; anything else (a REAL, or a text or
1158
+ * blob read as an integer) goes through `sqlite3VdbeSerialGet` and SQLite's
1159
+ * own conversion, so every answer is the one a Mem would give. */
1160
+ /* The integer of serial type `t` at `b`: 1 to 6 big-endian and signed, 8
1161
+ * and 9 the constants 0 and 1, as `sqlite3VdbeSerialGet` reads them. Any
1162
+ * other type is 0; the callers check the type first. */
1163
+ static int64_t serial_int(u32 t, const u8 *b) {
1164
+ switch (t) {
1165
+ case 1: return ONE_BYTE_INT(b);
1166
+ case 2: return TWO_BYTE_INT(b);
1167
+ case 3: return THREE_BYTE_INT(b);
1168
+ case 4: return FOUR_BYTE_INT(b);
1169
+ case 5: return SIX_BYTE_INT(b);
1170
+ case 6: return (int64_t)sqlite3Get8byte(b);
1171
+ case 9: return 1;
1172
+ default: return 0;
1173
+ }
1174
+ }
1175
+
1176
+ static inline int32_t field_value(nishsqlite_cursor *cur, int32_t n, uint64_t *v, int bytes, uint8_t *buf, uint64_t room) {
1177
+ u32 t = cur->ftype[n];
1178
+ const u8 *b = cur->rec + cur->foff[n];
1179
+ if (t >= 12 && bytes) {
1180
+ *v = (t - 12) / 2;
1181
+ if (*v > 0 && *v <= room) memcpy(buf, b, (size_t)*v);
1182
+ return (t & 1) ? SQLITE_TEXT : SQLITE_BLOB;
1183
+ }
1184
+ if (t == 0 || t == 10 || t == 11) {
1185
+ *v = 0;
1186
+ return SQLITE_NULL;
1187
+ }
1188
+ if (t <= 9 && t != 7) {
1189
+ *v = (uint64_t)serial_int(t, b);
1190
+ return SQLITE_INTEGER;
1191
+ }
1192
+ Mem m;
1193
+ memset(&m, 0, sizeof m);
1194
+ m.enc = cur->ki->enc;
1195
+ m.db = cur->ki->db;
1196
+ sqlite3VdbeSerialGet(b, t, &m);
1197
+ *v = (uint64_t)sqlite3_value_int64((sqlite3_value *)&m);
1198
+ return sqlite3_value_type((sqlite3_value *)&m);
1199
+ }
1200
+
1201
+ /* Whether column `n` of the current row can be read from its record: 0
1202
+ * with `*rc` set when it cannot. The INTEGER PRIMARY KEY column is the row's
1203
+ * key, not a field of its record, so it answers 0 with `*alias` set. A row
1204
+ * whose record holds fewer fields than `n` (one written before ALTER TABLE
1205
+ * added the column) is SQLITE_SCHEMA. */
1206
+ static int column_field(nishsqlite_cursor *cur, int32_t n, int *alias, int *rc) {
1110
1207
  *alias = 0;
1111
1208
  if (cur == NULL || !cur->has_row) {
1112
1209
  *rc = SQLITE_MISUSE;
1113
- return NULL;
1210
+ return 0;
1114
1211
  }
1115
1212
  if (n < 0 || n >= cur->ncol) {
1116
1213
  *rc = SQLITE_RANGE;
1117
- return NULL;
1214
+ return 0;
1118
1215
  }
1119
1216
  if (n == cur->alias) {
1120
1217
  *alias = 1;
1121
1218
  *rc = SQLITE_OK;
1122
- return NULL;
1219
+ return 0;
1123
1220
  }
1124
1221
  *rc = row_of(cur);
1125
- if (*rc != SQLITE_OK) return NULL;
1222
+ if (*rc != SQLITE_OK) return 0;
1126
1223
  if (n >= cur->nfields) {
1127
1224
  *rc = SQLITE_SCHEMA;
1128
- return NULL;
1225
+ return 0;
1129
1226
  }
1130
- return &cur->row->aMem[n];
1227
+ return 1;
1228
+ }
1229
+
1230
+ /* Column `n`'s type, SQLITE_INTEGER to SQLITE_NULL, or the negated code, with
1231
+ * `*v` its 8 bytes: with `bytes` set, a TEXT or BLOB column's length, else
1232
+ * the integer `sqlite3_column_int64` would convert the column to, the
1233
+ * INTEGER PRIMARY KEY column's being the row's key. With `bytes` set, a TEXT
1234
+ * or BLOB column's bytes are copied to `buf` when all of them fit in its
1235
+ * `room`. The type is the stored value's: a REAL column holding a whole
1236
+ * number stores it as an integer, and answers SQLITE_INTEGER, where a query
1237
+ * would convert it. */
1238
+ static int32_t column_value(nishsqlite_cursor *cur, int32_t n, uint64_t *v, int bytes, uint8_t *buf, uint64_t room) {
1239
+ int alias, rc;
1240
+ int ok = column_field(cur, n, &alias, &rc);
1241
+ if (alias) {
1242
+ if (sqlite3BtreeCursorHasMoved(cur->bt)) return -SQLITE_ABORT;
1243
+ *v = (uint64_t)sqlite3BtreeIntegerKey(cur->bt);
1244
+ return SQLITE_INTEGER;
1245
+ }
1246
+ if (!ok) return -rc;
1247
+ return field_value(cur, n, v, bytes, buf, room);
1131
1248
  }
1132
1249
 
1133
- /* The column's type, SQLITE_INTEGER to SQLITE_NULL, or the negated code. The
1134
- * type is the stored value's: a REAL column holding a whole number stores it
1135
- * as an integer, and answers SQLITE_INTEGER, where a query would convert it. */
1136
- static int32_t unlocked_cursor_column_type(int32_t h, int32_t n) {
1137
- nishsqlite_cursor *cur;
1138
- int alias, rc;
1139
- Mem *m = column_mem(h, n, &cur, &alias, &rc);
1140
- if (alias) return sqlite3BtreeCursorHasMoved(cur->bt) ? -SQLITE_ABORT : SQLITE_INTEGER;
1141
- return m ? sqlite3_value_type((sqlite3_value *)m) : -rc;
1250
+ static int32_t unlocked_cursor_column_type(nishsqlite_cursor *cur, int32_t n) {
1251
+ uint64_t v;
1252
+ return column_value(cur, n, &v, 1, NULL, 0);
1142
1253
  }
1143
1254
 
1144
1255
  /* The column as an integer, converted as `sqlite3_column_int64` converts; 0
1145
1256
  * when it cannot be read. */
1146
- static int64_t unlocked_cursor_column_i64(int32_t h, int32_t n) {
1147
- nishsqlite_cursor *cur;
1148
- int alias, rc;
1149
- Mem *m = column_mem(h, n, &cur, &alias, &rc);
1150
- if (alias) return sqlite3BtreeCursorHasMoved(cur->bt) ? 0 : sqlite3BtreeIntegerKey(cur->bt);
1151
- return m ? sqlite3_value_int64((sqlite3_value *)m) : 0;
1257
+ static int64_t unlocked_cursor_column_i64(nishsqlite_cursor *cur, int32_t n) {
1258
+ uint64_t v;
1259
+ return column_value(cur, n, &v, 0, NULL, 0) < 0 ? 0 : (int64_t)v;
1152
1260
  }
1153
1261
 
1154
1262
  /* Copies a TEXT or BLOB column's bytes into `buf` at `at`, `n` the buffer's
1155
1263
  * length, and answers their length; as `nishsqlite_column_into`, it copies
1156
- * only when all of them fit. NULL answers 0. An INTEGER or FLOAT column,
1157
- * the INTEGER PRIMARY KEY column among them, is SQLITE_MISMATCH: the cursor
1158
- * answers stored bytes and converts no number to text. Codes are negated. */
1159
- static int32_t unlocked_cursor_column_into(int32_t h, int32_t c, uint8_t *buf, uint64_t n, int32_t at) {
1264
+ * only when all of them fit. NULL answers 0. An INTEGER or FLOAT column is
1265
+ * SQLITE_MISMATCH, the INTEGER PRIMARY KEY column among them unless the row
1266
+ * has moved, which is SQLITE_ABORT as columnType answers: the cursor answers
1267
+ * stored bytes and converts no number to text. Codes are negated. */
1268
+ static int32_t unlocked_cursor_column_into(nishsqlite_cursor *cur, int32_t c, uint8_t *buf, uint64_t n, int32_t at) {
1160
1269
  if (at < 0 || (uint64_t)at > n) return -SQLITE_MISUSE;
1161
- nishsqlite_cursor *cur;
1162
- int alias, rc;
1163
- Mem *m = column_mem(h, c, &cur, &alias, &rc);
1164
- if (alias) return -SQLITE_MISMATCH;
1165
- if (m == NULL) return -rc;
1166
- int type = sqlite3_value_type((sqlite3_value *)m);
1270
+ uint64_t len;
1271
+ int32_t type = column_value(cur, c, &len, 1, buf + at, n - (uint64_t)at);
1272
+ if (type < 0) return type;
1167
1273
  if (type == SQLITE_NULL) return 0;
1168
1274
  if (type != SQLITE_TEXT && type != SQLITE_BLOB) return -SQLITE_MISMATCH;
1169
- const void *p = sqlite3_value_blob((sqlite3_value *)m);
1170
- int32_t len = sqlite3_value_bytes((sqlite3_value *)m);
1171
- if (p != NULL && (uint64_t)len <= n - (uint64_t)at) memcpy(buf + at, p, (size_t)len);
1172
- return len;
1275
+ return (int32_t)len;
1173
1276
  }
1174
1277
 
1175
1278
  /* The current row's rowid: a table row's key, or an index entry's last
1176
1279
  * field. 0 when there is no row or it cannot be read. */
1177
- static int64_t unlocked_cursor_rowid(int32_t h) {
1178
- nishsqlite_cursor *cur = cursor_of(h);
1280
+ static int64_t unlocked_cursor_rowid(nishsqlite_cursor *cur) {
1179
1281
  if (cur == NULL || !cur->has_row) return 0;
1180
1282
  if (!cur->is_index) return sqlite3BtreeCursorHasMoved(cur->bt) ? 0 : sqlite3BtreeIntegerKey(cur->bt);
1283
+ /* An index entry's rowid is its record's last field, so it is read off
1284
+ * the record's end, as `sqlite3VdbeIdxRowid` reads it, without walking
1285
+ * the header: the header's last byte is the rowid's serial type, an
1286
+ * integer's being one byte. Any other record takes the walk below. */
1287
+ if (!cur->unpacked) {
1288
+ if (sqlite3BtreeCursorHasMoved(cur->bt)) return 0;
1289
+ u32 sz = 0;
1290
+ const void *p = NULL;
1291
+ if (record_of(cur, &sz, &p) != SQLITE_OK) return 0;
1292
+ const u8 *a = p;
1293
+ u32 hdr = 0;
1294
+ if (sz >= 3) getVarint32NR(a, hdr);
1295
+ if (hdr >= 3 && hdr <= sz) {
1296
+ u32 t = a[hdr - 1];
1297
+ if (t >= 1 && t <= 9 && t != 7 && sz >= hdr + sqlite3SmallTypeSizes[t])
1298
+ return serial_int(t, a + sz - sqlite3SmallTypeSizes[t]);
1299
+ }
1300
+ }
1181
1301
  if (row_of(cur) != SQLITE_OK || cur->nfields < cur->ncol) return 0;
1182
- return sqlite3_value_int64((sqlite3_value *)&cur->row->aMem[cur->ncol - 1]);
1302
+ uint64_t v;
1303
+ return field_value(cur, cur->ncol - 1, &v, 0, NULL, 0) < 0 ? 0 : (int64_t)v;
1304
+ }
1305
+
1306
+ /* ---- Range counts ------------------------------------------------------- */
1307
+
1308
+ /* Counts an index's entries whose first key column is k, for each k in
1309
+ * [lo, hi], into the cursor's `counts[k - lo]` (read back with
1310
+ * `count_at`), in one seek and one forward walk: each entry's record is
1311
+ * compared with k by SQLite's record compare, never unpacked. It uses the
1312
+ * kept key's first value, so it leaves no seek key set, and leaves the cursor
1313
+ * on no row. `n` is the caller's room, which bounds the buffer: it grows to
1314
+ * the largest span asked and is kept. It answers the entries counted, or
1315
+ * negated: SQLITE_MISUSE for a table cursor, SQLITE_MISMATCH for an index
1316
+ * whose first key column does not store integers as integers, SQLITE_RANGE
1317
+ * for lo > hi or a span longer than `n`, SQLITE_NOMEM, SQLITE_TOOBIG past
1318
+ * INT32_MAX entries, and SQLite's code when a page cannot be read. */
1319
+ static int32_t unlocked_cursor_count_range(nishsqlite_cursor *cur, int64_t lo, int64_t hi, uint64_t n) {
1320
+ if (cur == NULL || !cur->is_index) return -SQLITE_MISUSE;
1321
+ if (!cur->int_first) return -SQLITE_MISMATCH;
1322
+ if (lo > hi || (uint64_t)hi - (uint64_t)lo >= n) return -SQLITE_RANGE;
1323
+ uint64_t span = (uint64_t)hi - (uint64_t)lo + 1;
1324
+ cur->ncounts = 0;
1325
+ if (span > cur->counts_cap) {
1326
+ int64_t *counts = sqlite3_realloc64(cur->counts, span * sizeof *counts);
1327
+ if (counts == NULL) return -SQLITE_NOMEM;
1328
+ cur->counts = counts;
1329
+ cur->counts_cap = span;
1330
+ }
1331
+ int64_t *out = cur->counts;
1332
+ memset(out, 0, (size_t)span * sizeof *out);
1333
+ (void)unlocked_key_reset(cur);
1334
+ /* In index order: lo up to hi, or hi down to lo under DESC. */
1335
+ int desc = (cur->ki->aSortFlags[0] & KEYINFO_ORDER_DESC) != 0;
1336
+ uint64_t at = desc ? span - 1 : 0; /* k - lo for the k the walk is on */
1337
+ Mem *k = &cur->key[0];
1338
+ sqlite3VdbeMemSetInt64(k, desc ? hi : lo);
1339
+ UnpackedRecord *key = seek_key(cur, 1, 1);
1340
+ int res = 0;
1341
+ int rc = sqlite3BtreeIndexMoveto(cur->bt, key, &res);
1342
+ if (rc == SQLITE_OK && res < 0) rc = sqlite3BtreeNext(cur->bt, 0);
1343
+ key->default_rc = 0;
1344
+ int32_t total = 0;
1345
+ while (rc == SQLITE_OK) {
1346
+ u32 sz = 0;
1347
+ const void *p = NULL;
1348
+ if ((rc = record_of(cur, &sz, &p)) != SQLITE_OK) break;
1349
+ /* row_of reads an empty record as zero fields, but a compare reads the
1350
+ * record's header size from its first byte, which an empty one does not
1351
+ * have; and an index entry always holds at least its rowid. */
1352
+ if (sz == 0) {
1353
+ rc = SQLITE_CORRUPT;
1354
+ break;
1355
+ }
1356
+ int c = sqlite3VdbeRecordCompare((int)sz, p, key);
1357
+ if (key->errCode) {
1358
+ rc = key->errCode;
1359
+ break;
1360
+ }
1361
+ /* The entry sorts after k: move k on in index order until it does not. */
1362
+ while (c > 0) {
1363
+ if (desc ? at == 0 : at == span - 1) goto done;
1364
+ at = desc ? at - 1 : at + 1;
1365
+ sqlite3VdbeMemSetInt64(k, (int64_t)((uint64_t)lo + at));
1366
+ c = sqlite3VdbeRecordCompare((int)sz, p, key);
1367
+ }
1368
+ if (c == 0) {
1369
+ if (total == INT32_MAX) {
1370
+ rc = SQLITE_TOOBIG;
1371
+ break;
1372
+ }
1373
+ out[at]++;
1374
+ total++;
1375
+ }
1376
+ rc = sqlite3BtreeNext(cur->bt, 0);
1377
+ }
1378
+ done:
1379
+ sqlite3VdbeMemSetNull(k);
1380
+ moved(cur, SQLITE_OK, 0);
1381
+ if (rc != SQLITE_OK && rc != SQLITE_DONE) return -rc;
1382
+ cur->ncounts = span;
1383
+ return total;
1384
+ }
1385
+
1386
+ /* The last successful count's entry `i`: -1 past its span, and for every
1387
+ * `i` after a count that failed past its refusals. */
1388
+ static int64_t unlocked_cursor_count_at(nishsqlite_cursor *cur, uint64_t i) {
1389
+ return cur != NULL && i < cur->ncounts ? cur->counts[i] : -1;
1183
1390
  }
1184
1391
 
1185
1392
  /* ---- Locking ------------------------------------------------------------ */
@@ -1187,17 +1394,16 @@ static int64_t unlocked_cursor_rowid(int32_t h) {
1187
1394
  /* Every cursor entry point holds its connection's `db->mutex` around the
1188
1395
  * work, as SQLite's own API functions do: the internal functions the cursors
1189
1396
  * call (the allocator, the decoder, the B-tree) expect it held. The mutex is
1190
- * recursive, and NULL (a no-op) when the build or the open asks for none. */
1397
+ * recursive, and NULL (a no-op) when the build or the open asks for none. A
1398
+ * cursor entry point looks its handle up once (`cursor_of`), outside the
1399
+ * mutex: the tables are the thread's own, and the lookup reads nothing of
1400
+ * SQLite's. The work then gets the cursor, NULL for a handle that names none,
1401
+ * which every one of them answers as SQLITE_MISUSE. */
1191
1402
  static sqlite3_mutex *conn_mutex(int32_t h) {
1192
1403
  nishsqlite_conn *c = conn_of(h);
1193
1404
  return c ? sqlite3_db_mutex(c->db) : NULL;
1194
1405
  }
1195
1406
 
1196
- static sqlite3_mutex *cur_mutex(int32_t h) {
1197
- nishsqlite_slot *s = slot_of(&nishsqlite_cursors, h);
1198
- return s ? sqlite3_db_mutex(((nishsqlite_cursor *)s->p)->conn->db) : NULL;
1199
- }
1200
-
1201
1407
  #define NISH_SQLITE_LOCKED(type, mutex, call) \
1202
1408
  sqlite3_mutex *mx_ = (mutex); \
1203
1409
  sqlite3_mutex_enter(mx_); \
@@ -1205,66 +1411,160 @@ static sqlite3_mutex *cur_mutex(int32_t h) {
1205
1411
  sqlite3_mutex_leave(mx_); \
1206
1412
  return r_
1207
1413
 
1414
+ #define NISH_SQLITE_CURSOR(type, call) \
1415
+ nishsqlite_cursor *cur = cursor_of(h); \
1416
+ NISH_SQLITE_LOCKED(type, cur ? cur->mutex : NULL, call)
1417
+
1208
1418
  int32_t nishsqlite_cursor_open(int32_t h, const char *name, const char *columns) {
1209
1419
  NISH_SQLITE_LOCKED(int32_t, conn_mutex(h), unlocked_cursor_open(h, name, columns));
1210
1420
  }
1211
1421
 
1212
1422
  int32_t nishsqlite_cursor_close(int32_t h) {
1213
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_close(h));
1423
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_close(cur, h));
1214
1424
  }
1215
1425
 
1216
1426
  int32_t nishsqlite_cursor_first(int32_t h) {
1217
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_first(h));
1427
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_first(cur));
1218
1428
  }
1219
1429
 
1220
1430
  int32_t nishsqlite_cursor_last(int32_t h) {
1221
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_last(h));
1431
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_last(cur));
1222
1432
  }
1223
1433
 
1224
1434
  int32_t nishsqlite_cursor_next(int32_t h) {
1225
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_next(h));
1435
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_next(cur));
1226
1436
  }
1227
1437
 
1228
1438
  int32_t nishsqlite_cursor_previous(int32_t h) {
1229
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_previous(h));
1439
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_previous(cur));
1230
1440
  }
1231
1441
 
1232
1442
  int32_t nishsqlite_cursor_seek_rowid(int32_t h, int64_t rowid) {
1233
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_seek_rowid(h, rowid));
1443
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_seek_rowid(cur, rowid));
1234
1444
  }
1235
1445
 
1236
1446
  int32_t nishsqlite_key_reset(int32_t h) {
1237
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_key_reset(h));
1447
+ NISH_SQLITE_CURSOR(int32_t, unlocked_key_reset(cur));
1238
1448
  }
1239
1449
 
1240
1450
  int32_t nishsqlite_key_i64(int32_t h, int64_t v) {
1241
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_key_i64(h, v));
1451
+ NISH_SQLITE_CURSOR(int32_t, unlocked_key_i64(cur, v));
1242
1452
  }
1243
1453
 
1244
1454
  int32_t nishsqlite_key_text(int32_t h, const char *text) {
1245
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_key_text(h, text));
1455
+ NISH_SQLITE_CURSOR(int32_t, unlocked_key_text(cur, text));
1246
1456
  }
1247
1457
 
1248
1458
  int32_t nishsqlite_key_bytes(int32_t h, const uint8_t *buf, uint64_t n, int32_t off, int32_t len) {
1249
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_key_bytes(h, buf, n, off, len));
1459
+ NISH_SQLITE_CURSOR(int32_t, unlocked_key_bytes(cur, buf, n, off, len));
1250
1460
  }
1251
1461
 
1462
+ int32_t nishsqlite_cursor_count_range(int32_t h, int64_t lo, int64_t hi, uint64_t n) {
1463
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_count_range(cur, lo, hi, n));
1464
+ }
1465
+ int64_t nishsqlite_cursor_count_at(int32_t h, uint64_t i) {
1466
+ NISH_SQLITE_CURSOR(int64_t, unlocked_cursor_count_at(cur, i));
1467
+ }
1252
1468
  int32_t nishsqlite_cursor_seek(int32_t h, int32_t op) {
1253
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_seek(h, op));
1469
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_seek(cur, op));
1254
1470
  }
1255
1471
 
1256
1472
  int32_t nishsqlite_cursor_column_type(int32_t h, int32_t n) {
1257
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_column_type(h, n));
1473
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_column_type(cur, n));
1258
1474
  }
1259
1475
 
1260
1476
  int64_t nishsqlite_cursor_column_i64(int32_t h, int32_t n) {
1261
- NISH_SQLITE_LOCKED(int64_t, cur_mutex(h), unlocked_cursor_column_i64(h, n));
1477
+ NISH_SQLITE_CURSOR(int64_t, unlocked_cursor_column_i64(cur, n));
1262
1478
  }
1263
1479
 
1264
1480
  int32_t nishsqlite_cursor_column_into(int32_t h, int32_t c, uint8_t *buf, uint64_t n, int32_t at) {
1265
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_column_into(h, c, buf, n, at));
1481
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_column_into(cur, c, buf, n, at));
1266
1482
  }
1267
1483
 
1268
1484
  int64_t nishsqlite_cursor_rowid(int32_t h) {
1269
- NISH_SQLITE_LOCKED(int64_t, cur_mutex(h), unlocked_cursor_rowid(h));
1485
+ NISH_SQLITE_CURSOR(int64_t, unlocked_cursor_rowid(cur));
1486
+ }
1487
+
1488
+ /* readRow: reads a list of columns of the current row in one call. `io` holds
1489
+ * `n / 11` columns: first their indices, a little-endian u16 each, which it
1490
+ * leaves as they are, then a slot of 9 bytes per column, in order: the type
1491
+ * unlocked_cursor_column_type answers, then 8 bytes, a TEXT or BLOB
1492
+ * column's length, or the value unlocked_cursor_column_i64 answers for the
1493
+ * rest. No TEXT or BLOB is converted to a number. Each column's Mem is
1494
+ * found once, under the one lock the caller holds. The bytes of TEXT and
1495
+ * BLOB columns go to `text` in column order while they fit. Answers the
1496
+ * length they need, SQLITE_TOOBIG past INT32_MAX, or the code column_type
1497
+ * refuses the first bad column with, MISUSE with no row even for an empty
1498
+ * list; after a refusal, or a length over `ntext`, the caller reads nothing
1499
+ * from `io` or `text`. The slots are stored in the machine's order, which
1500
+ * is little-endian on every target the runtime builds for. Codes are
1501
+ * negated. */
1502
+ #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
1503
+ #error "readRow's slots are little-endian"
1504
+ #endif
1505
+ /* `column_value` for each listed column, with the checks it repeats per
1506
+ * column made once a row: the cursor's row, and its record's header, which
1507
+ * is read only when a listed column is a field of the record. Each column
1508
+ * answers what `column_value` would, in the same order, so the first refusal
1509
+ * is the one a column-by-column read would meet. */
1510
+ static int32_t unlocked_cursor_read_row(nishsqlite_cursor *cur, uint8_t *io, uint64_t n, uint8_t *text, uint64_t ntext) {
1511
+ if (cur == NULL || !cur->has_row) return -SQLITE_MISUSE;
1512
+ uint64_t ncols = n / 11, need = 0;
1513
+ uint8_t *out = io + 2 * ncols;
1514
+ int read = 0;
1515
+ for (uint64_t k = 0; k < ncols; k++, out += 9) {
1516
+ int32_t c = io[2 * k] | (io[2 * k + 1] << 8);
1517
+ uint64_t v;
1518
+ int32_t type;
1519
+ if (c >= cur->ncol) return -SQLITE_RANGE;
1520
+ if (c == cur->alias) {
1521
+ if (sqlite3BtreeCursorHasMoved(cur->bt)) return -SQLITE_ABORT;
1522
+ v = (uint64_t)sqlite3BtreeIntegerKey(cur->bt);
1523
+ type = SQLITE_INTEGER;
1524
+ } else {
1525
+ if (!read) {
1526
+ int rc = row_of(cur);
1527
+ if (rc != SQLITE_OK) return -rc;
1528
+ read = 1;
1529
+ }
1530
+ if (c >= cur->nfields) return -SQLITE_SCHEMA;
1531
+ type = field_value(cur, c, &v, 1, text + need, need <= ntext ? ntext - need : 0);
1532
+ if (type == SQLITE_TEXT || type == SQLITE_BLOB) need += v;
1533
+ }
1534
+ out[0] = (uint8_t)type;
1535
+ memcpy(out + 1, &v, 8);
1536
+ }
1537
+ return need > INT32_MAX ? -SQLITE_TOOBIG : (int32_t)need;
1538
+ }
1539
+
1540
+ int32_t nishsqlite_cursor_read_row(int32_t h, uint8_t *io, uint64_t n, uint8_t *text, uint64_t ntext) {
1541
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_read_row(cur, io, n, text, ntext));
1542
+ }
1543
+
1544
+ /* Copies the current row's record, SQLite's record format whole (its header
1545
+ * of varints, then its fields' bytes), into `buf`, `n` its length, and
1546
+ * answers the record's length. A record longer than `n` copies nothing and
1547
+ * answers its length all the same, so the caller grows `buf` and asks again.
1548
+ * An index entry's record ends with its rowid; a table row's holds no
1549
+ * INTEGER PRIMARY KEY, which is the row's key (`rowid`). SQLITE_MISUSE with
1550
+ * no row, SQLITE_ABORT when the row has moved, SQLITE_CORRUPT for a record
1551
+ * longer than SQLite allows, negated. */
1552
+ static int32_t unlocked_cursor_record(nishsqlite_cursor *cur, uint8_t *buf, uint64_t n) {
1553
+ if (cur == NULL || !cur->has_row) return -SQLITE_MISUSE;
1554
+ if (sqlite3BtreeCursorHasMoved(cur->bt)) return -SQLITE_ABORT;
1555
+ u32 sz = sqlite3BtreePayloadSize(cur->bt);
1556
+ if (sz > SQLITE_MAX_LENGTH) return -SQLITE_CORRUPT;
1557
+ if (sz > n) return (int32_t)sz;
1558
+ u32 amt = 0;
1559
+ const void *p = sqlite3BtreePayloadFetch(cur->bt, &amt);
1560
+ if (amt >= sz) {
1561
+ if (sz > 0) memcpy(buf, p, sz);
1562
+ return (int32_t)sz;
1563
+ }
1564
+ int rc = sqlite3BtreePayload(cur->bt, 0, sz, buf);
1565
+ return rc == SQLITE_OK ? (int32_t)sz : -rc;
1566
+ }
1567
+
1568
+ int32_t nishsqlite_cursor_record(int32_t h, uint8_t *buf, uint64_t n) {
1569
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_record(cur, buf, n));
1270
1570
  }