// Code generated for linux/arm by 'generator --package-name libsqlite3 --prefix-enumerator=_ --prefix-external=x_ --prefix-field=F --prefix-static-internal=_ --prefix-static-none=_ --prefix-tagged-enum=_ --prefix-tagged-struct=T --prefix-tagged-union=T --prefix-typename=T --prefix-undefined=_ -ignore-unsupported-alignment -DHAVE_USLEEP -DLONGDOUBLE_TYPE=double -DNDEBUG -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_GEOPOLY -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MATH_FUNCTIONS -DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_OFFSET_SQL_FUNC -DSQLITE_ENABLE_PREUPDATE_HOOK -DSQLITE_ENABLE_RBU -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_SESSION -DSQLITE_ENABLE_SNAPSHOT -DSQLITE_ENABLE_STAT4 -DSQLITE_ENABLE_UNLOCK_NOTIFY -DSQLITE_HAVE_ZLIB=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_SOUNDEX -DSQLITE_THREADSAFE=1 -DSQLITE_WITHOUT_ZONEMALLOC -Dpread64=pread -Dpwrite64=pwrite -extended-errors -o sqlite3.go sqlite3.c -I/tmp/libsqlite3/sqlite-amalgamation-3450300/ccgo -DSQLITE_MUTEX_NOOP -DSQLITE_OS_UNIX=1 -eval-all-macros', DO NOT EDIT. //go:build linux && arm // +build linux,arm package sqlite3 import ( "reflect" "unsafe" "modernc.org/libc" ) var ( _ reflect.Type _ unsafe.Pointer ) const ACCESSPERMS = 511 const ADJ_ESTERROR = 8 const ADJ_FREQUENCY = 2 const ADJ_MAXERROR = 4 const ADJ_MICRO = 4096 const ADJ_NANO = 8192 const ADJ_OFFSET = 1 const ADJ_OFFSET_SINGLESHOT = 32769 const ADJ_OFFSET_SS_READ = 40961 const ADJ_SETOFFSET = 256 const ADJ_STATUS = 16 const ADJ_TAI = 128 const ADJ_TICK = 16384 const ADJ_TIMECONST = 32 const ALLBITS = -1 const ALLPERMS = 4095 const AT_EACCESS = 512 const AT_EMPTY_PATH = 4096 const AT_FDCWD = -100 const AT_NO_AUTOMOUNT = 2048 const AT_RECURSIVE = 32768 const AT_REMOVEDIR = 512 const AT_STATX_DONT_SYNC = 16384 const AT_STATX_FORCE_SYNC = 8192 const AT_STATX_SYNC_AS_STAT = 0 const AT_STATX_SYNC_TYPE = 24576 const AT_SYMLINK_FOLLOW = 1024 const AT_SYMLINK_NOFOLLOW = 256 const BIG_ENDIAN = 4321 const BITVEC_MXHASH = 0 const BITVEC_NBIT = 0 const BITVEC_NELEM = 0 const BITVEC_NINT = 0 const BITVEC_NPTR = 0 const BITVEC_SZ = 512 const BITVEC_SZELEM = 8 const BITVEC_TELEM = 0 const BITVEC_USIZE = 0 const BTALLOC_ANY = 0 const BTALLOC_EXACT = 1 const BTALLOC_LE = 2 const BTCF_AtLast = 8 const BTCF_Incrblob = 16 const BTCF_Multiple = 32 const BTCF_Pinned = 64 const BTCF_ValidNKey = 2 const BTCF_ValidOvfl = 4 const BTCF_WriteFlag = 1 const BTCURSOR_FIRST_UNINIT = 0 const BTCURSOR_MAX_DEPTH = 20 const BTREE_APPEND = 8 const BTREE_APPLICATION_ID = 8 const BTREE_AUTOVACUUM_FULL = 1 const BTREE_AUTOVACUUM_INCR = 2 const BTREE_AUTOVACUUM_NONE = 0 const BTREE_AUXDELETE = 4 const BTREE_BLOBKEY = 2 const BTREE_BULKLOAD = 1 const BTREE_DATA_VERSION = 15 const BTREE_DEFAULT_CACHE_SIZE = 3 const BTREE_FILE_FORMAT = 2 const BTREE_FORDELETE = 8 const BTREE_FREE_PAGE_COUNT = 0 const BTREE_HINT_RANGE = 0 const BTREE_INCR_VACUUM = 7 const BTREE_INTKEY = 1 const BTREE_LARGEST_ROOT_PAGE = 4 const BTREE_MEMORY = 2 const BTREE_OMIT_JOURNAL = 1 const BTREE_PREFORMAT = 128 const BTREE_SAVEPOSITION = 2 const BTREE_SCHEMA_VERSION = 1 const BTREE_SEEK_EQ = 2 const BTREE_SINGLE = 4 const BTREE_TEXT_ENCODING = 5 const BTREE_UNORDERED = 8 const BTREE_USER_VERSION = 6 const BTREE_WRCSR = 4 const BTS_EXCLUSIVE = 64 const BTS_FAST_SECURE = 12 const BTS_INITIALLY_EMPTY = 16 const BTS_NO_WAL = 32 const BTS_OVERWRITE = 8 const BTS_PAGESIZE_FIXED = 2 const BTS_PENDING = 128 const BTS_READ_ONLY = 1 const BTS_SECURE_DELETE = 4 const BUFSIZ = 8192 const BYTE_ORDER = 1234 const CACHE_STALE = 0 const CBRK = 0 const CC_AND = 24 const CC_BANG = 15 const CC_BOM = 30 const CC_COMMA = 23 const CC_DIGIT = 3 const CC_DOLLAR = 4 const CC_DOT = 26 const CC_EQ = 14 const CC_GT = 13 const CC_ID = 27 const CC_ILLEGAL = 28 const CC_KYWD = 2 const CC_KYWD0 = 1 const CC_LP = 17 const CC_LT = 12 const CC_MINUS = 11 const CC_NUL = 29 const CC_PERCENT = 22 const CC_PIPE = 10 const CC_PLUS = 20 const CC_QUOTE = 8 const CC_QUOTE2 = 9 const CC_RP = 18 const CC_SEMI = 19 const CC_SLASH = 16 const CC_SPACE = 7 const CC_STAR = 21 const CC_TILDA = 25 const CC_VARALPHA = 5 const CC_VARNUM = 6 const CC_X = 0 const CDISCARD = 15 const CDSUSP = 25 const CEOF = 4 const CEOL = 0 const CEOT = 4 const CERASE = 127 const CFLUSH = 15 const CINTR = 3 const CKCNSTRNT_COLUMN = 1 const CKCNSTRNT_ROWID = 2 const CKILL = 21 const CLNEXT = 22 const CLOCK_BOOTTIME = 7 const CLOCK_BOOTTIME_ALARM = 9 const CLOCK_MONOTONIC = 1 const CLOCK_MONOTONIC_COARSE = 6 const CLOCK_MONOTONIC_RAW = 4 const CLOCK_PROCESS_CPUTIME_ID = 2 const CLOCK_REALTIME = 0 const CLOCK_REALTIME_ALARM = 8 const CLOCK_REALTIME_COARSE = 5 const CLOCK_TAI = 11 const CLOCK_THREAD_CPUTIME_ID = 3 const CLOSE_RANGE_CLOEXEC = 4 const CLOSE_RANGE_UNSHARE = 2 const CMIN = 1 const COLFLAG_BUSY = 256 const COLFLAG_GENERATED = 96 const COLFLAG_HASCOLL = 512 const COLFLAG_HASTYPE = 4 const COLFLAG_HIDDEN = 2 const COLFLAG_NOEXPAND = 1024 const COLFLAG_NOINSERT = 98 const COLFLAG_NOTAVAIL = 128 const COLFLAG_PRIMKEY = 1 const COLFLAG_SORTERREF = 16 const COLFLAG_STORED = 64 const COLFLAG_UNIQUE = 8 const COLFLAG_VIRTUAL = 32 const COLNAME_COLUMN = 4 const COLNAME_DATABASE = 2 const COLNAME_DECLTYPE = 1 const COLNAME_N = 5 const COLNAME_NAME = 0 const COLNAME_TABLE = 3 const COLTYPE_ANY = 1 const COLTYPE_BLOB = 2 const COLTYPE_CUSTOM = 0 const COLTYPE_INT = 3 const COLTYPE_INTEGER = 4 const COLTYPE_REAL = 5 const COLTYPE_TEXT = 6 const CQUIT = 28 const CREPRINT = 18 const CRPRNT = 18 const CSTART = 17 const CSTATUS = 0 const CSTOP = 19 const CSUSP = 26 const CTIME = 0 const CURSOR_FAULT = 4 const CURSOR_INVALID = 1 const CURSOR_REQUIRESEEK = 3 const CURSOR_SKIPNEXT = 2 const CURSOR_VALID = 0 const CURTYPE_BTREE = 0 const CURTYPE_PSEUDO = 3 const CURTYPE_SORTER = 1 const CURTYPE_VTAB = 2 const CWERASE = 23 const DBFLAG_EncodingFixed = 64 const DBFLAG_InternalFunc = 32 const DBFLAG_PreferBuiltin = 2 const DBFLAG_SchemaChange = 1 const DBFLAG_SchemaKnownOk = 16 const DBFLAG_Vacuum = 4 const DBFLAG_VacuumInto = 8 const DBSTAT_PAGE_PADDING_BYTES = 256 const DB_ResetWanted = 8 const DB_SchemaLoaded = 1 const DB_UnresetViews = 2 const DEFFILEMODE = 438 const DIRECT_MODE = 0 const DLFO_EH_SEGMENT_TYPE = 0 const DLFO_STRUCT_HAS_EH_COUNT = 1 const DLFO_STRUCT_HAS_EH_DBASE = 0 const DN_ACCESS = 1 const DN_ATTRIB = 32 const DN_CREATE = 4 const DN_DELETE = 8 const DN_MODIFY = 2 const DN_MULTISHOT = 2147483648 const DN_RENAME = 16 const DOTLOCK_SUFFIX = ".lock" const E2BIG = 7 const EACCES = 13 const EADDRINUSE = 98 const EADDRNOTAVAIL = 99 const EADV = 68 const EAFNOSUPPORT = 97 const EAGAIN = 11 const EALREADY = 114 const EBADE = 52 const EBADF = 9 const EBADFD = 77 const EBADMSG = 74 const EBADR = 53 const EBADRQC = 56 const EBADSLT = 57 const EBFONT = 59 const EBUSY = 16 const ECANCELED = 125 const ECHILD = 10 const ECHRNG = 44 const ECOMM = 70 const ECONNABORTED = 103 const ECONNREFUSED = 111 const ECONNRESET = 104 const EDEADLK = 35 const EDEADLOCK = 35 const EDESTADDRREQ = 89 const EDOM = 33 const EDOTDOT = 73 const EDQUOT = 122 const EEXIST = 17 const EFAULT = 14 const EFBIG = 27 const EHOSTDOWN = 112 const EHOSTUNREACH = 113 const EHWPOISON = 133 const EIDRM = 43 const EILSEQ = 84 const EINPROGRESS = 115 const EINTR = 4 const EINVAL = 22 const EIO = 5 const EISCONN = 106 const EISDIR = 21 const EISNAM = 120 const EKEYEXPIRED = 127 const EKEYREJECTED = 129 const EKEYREVOKED = 128 const EL2HLT = 51 const EL2NSYNC = 45 const EL3HLT = 46 const EL3RST = 47 const ELIBACC = 79 const ELIBBAD = 80 const ELIBEXEC = 83 const ELIBMAX = 82 const ELIBSCN = 81 const ELNRNG = 48 const ELOOP = 40 const EMEDIUMTYPE = 124 const EMFILE = 24 const EMLINK = 31 const EMSGSIZE = 90 const EMULTIHOP = 72 const ENAMETOOLONG = 36 const ENAME_NAME = 0 const ENAME_ROWID = 3 const ENAME_SPAN = 1 const ENAME_TAB = 2 const ENAVAIL = 119 const ENETDOWN = 100 const ENETRESET = 102 const ENETUNREACH = 101 const ENFILE = 23 const ENOANO = 55 const ENOBUFS = 105 const ENOCSI = 50 const ENODATA = 61 const ENODEV = 19 const ENOENT = 2 const ENOEXEC = 8 const ENOKEY = 126 const ENOLCK = 37 const ENOLINK = 67 const ENOMEDIUM = 123 const ENOMEM = 12 const ENOMSG = 42 const ENONET = 64 const ENOPKG = 65 const ENOPROTOOPT = 92 const ENOSPC = 28 const ENOSR = 63 const ENOSTR = 60 const ENOSYS = 38 const ENOTBLK = 15 const ENOTCONN = 107 const ENOTDIR = 20 const ENOTEMPTY = 39 const ENOTNAM = 118 const ENOTRECOVERABLE = 131 const ENOTSOCK = 88 const ENOTSUP = 95 const ENOTTY = 25 const ENOTUNIQ = 76 const ENXIO = 6 const EOF = -1 const EOPNOTSUPP = 95 const EOVERFLOW = 75 const EOWNERDEAD = 130 const EPERM = 1 const EPFNOSUPPORT = 96 const EPIPE = 32 const EPROTO = 71 const EPROTONOSUPPORT = 93 const EPROTOTYPE = 91 const EP_Agg = 16 const EP_CanBeNull = 2097152 const EP_Collate = 512 const EP_Commuted = 1024 const EP_ConstFunc = 1048576 const EP_DblQuoted = 128 const EP_Distinct = 4 const EP_FixedCol = 32 const EP_FromDDL = 1073741824 const EP_FullSize = 131072 const EP_HasFunc = 8 const EP_IfNullRow = 262144 const EP_Immutable = 2 const EP_InfixFunc = 256 const EP_InnerON = 2 const EP_IntValue = 2048 const EP_IsFalse = 536870912 const EP_IsTrue = 268435456 const EP_Leaf = 8388608 const EP_NoReduce = 1 const EP_OuterON = 1 const EP_Propagate = 4194824 const EP_Quoted = 67108864 const EP_Reduced = 16384 const EP_Skip = 8192 const EP_Static = 134217728 const EP_Subquery = 4194304 const EP_Subrtn = 33554432 const EP_TokenOnly = 65536 const EP_Unlikely = 524288 const EP_VarSelect = 64 const EP_Win = 32768 const EP_WinFunc = 16777216 const EP_xIsSelect = 4096 const ERANGE = 34 const EREMCHG = 78 const EREMOTE = 66 const EREMOTEIO = 121 const ERESTART = 85 const ERFKILL = 132 const EROFS = 30 const ESHUTDOWN = 108 const ESOCKTNOSUPPORT = 94 const ESPIPE = 29 const ESRCH = 3 const ESRMNT = 69 const ESTALE = 116 const ESTRPIPE = 86 const ETIME = 62 const ETIMEDOUT = 110 const ETOOMANYREFS = 109 const ETXTBSY = 26 const EU4_EXPR = 2 const EU4_IDX = 1 const EU4_NONE = 0 const EUCLEAN = 117 const EUNATCH = 49 const EUSERS = 87 const EWOULDBLOCK = 11 const EXCLUDED_TABLE_NUMBER = 2 const EXCLUSIVE_LOCK = 4 const EXDEV = 18 const EXFULL = 54 const EXIT_FAILURE = 1 const EXIT_SUCCESS = 0 const EXPRDUP_REDUCE = 1 const EXPR_FULLSIZE = 0 const F2FS_FEATURE_ATOMIC_WRITE = 4 const F2FS_IOCTL_MAGIC = 245 const F2FS_IOC_ABORT_VOLATILE_WRITE = 62725 const F2FS_IOC_COMMIT_ATOMIC_WRITE = 62722 const F2FS_IOC_GET_FEATURES = 2147546380 const F2FS_IOC_START_ATOMIC_WRITE = 62721 const F2FS_IOC_START_VOLATILE_WRITE = 62723 const FALLOC_FL_COLLAPSE_RANGE = 8 const FALLOC_FL_INSERT_RANGE = 32 const FALLOC_FL_KEEP_SIZE = 1 const FALLOC_FL_NO_HIDE_STALE = 4 const FALLOC_FL_PUNCH_HOLE = 2 const FALLOC_FL_UNSHARE_RANGE = 64 const FALLOC_FL_ZERO_RANGE = 16 const FAPPEND = 1024 const FASYNC = 8192 const FD_CLOEXEC = 1 const FD_SETSIZE = 1024 const FFSYNC = 1052672 const FILENAME_MAX = 4096 const FIOASYNC = 21586 const FIOCLEX = 21585 const FIONBIO = 21537 const FIONCLEX = 21584 const FIONREAD = 21531 const FIOQSIZE = 21598 const FLAG_SIGNED = 1 const FLAG_STRING = 4 const FNDELAY = 2048 const FNONBLOCK = 2048 const FP_ILOGB0 = -2147483647 const FP_ILOGBNAN = 2147483647 const FP_INFINITE = 1 const FP_INT_DOWNWARD = 1 const FP_INT_TONEAREST = 4 const FP_INT_TONEARESTFROMZERO = 3 const FP_INT_TOWARDZERO = 2 const FP_INT_UPWARD = 0 const FP_LLOGB0 = -2147483647 const FP_LLOGBNAN = 2147483647 const FP_NAN = 0 const FP_NORMAL = 4 const FP_SUBNORMAL = 3 const FP_ZERO = 2 const FTS5CSR_EOF = 1 const FTS5CSR_FREE_ZRANK = 16 const FTS5CSR_REQUIRE_CONTENT = 2 const FTS5CSR_REQUIRE_DOCSIZE = 4 const FTS5CSR_REQUIRE_INST = 8 const FTS5CSR_REQUIRE_POSLIST = 64 const FTS5CSR_REQUIRE_RESEEK = 32 const FTS5INDEX_QUERY_DESC = 2 const FTS5INDEX_QUERY_NOOUTPUT = 32 const FTS5INDEX_QUERY_NOTOKENDATA = 128 const FTS5INDEX_QUERY_PREFIX = 1 const FTS5INDEX_QUERY_SCAN = 8 const FTS5INDEX_QUERY_SCANONETERM = 256 const FTS5INDEX_QUERY_SKIPEMPTY = 16 const FTS5INDEX_QUERY_SKIPHASH = 64 const FTS5INDEX_QUERY_TEST_NOIDX = 4 const FTS5TOKEN = 0 const FTS5_AND = 2 const FTS5_AVERAGES_ROWID = 1 const FTS5_BI_MATCH = 1 const FTS5_BI_ORDER_DESC = 128 const FTS5_BI_ORDER_RANK = 32 const FTS5_BI_ORDER_ROWID = 64 const FTS5_BI_RANK = 2 const FTS5_BI_ROWID_EQ = 4 const FTS5_BI_ROWID_GE = 16 const FTS5_BI_ROWID_LE = 8 const FTS5_CARET = 12 const FTS5_COLON = 5 const FTS5_COMMA = 13 const FTS5_CONTENT_EXTERNAL = 2 const FTS5_CONTENT_NONE = 1 const FTS5_CONTENT_NORMAL = 0 const FTS5_CORRUPT = 267 const FTS5_CURRENT_VERSION = 4 const FTS5_CURRENT_VERSION_SECUREDELETE = 5 const FTS5_DATA_DLI_B = 1 const FTS5_DATA_HEIGHT_B = 5 const FTS5_DATA_ID_B = 16 const FTS5_DATA_PADDING = 20 const FTS5_DATA_PAGE_B = 31 const FTS5_DATA_ZERO_PADDING = 8 const FTS5_DEFAULT_AUTOMERGE = 4 const FTS5_DEFAULT_CRISISMERGE = 16 const FTS5_DEFAULT_DELETE_AUTOMERGE = 10 const FTS5_DEFAULT_HASHSIZE = 1048576 const FTS5_DEFAULT_NEARDIST = 10 const FTS5_DEFAULT_PAGE_SIZE = 4050 const FTS5_DEFAULT_RANK = "bm25" const FTS5_DEFAULT_USERMERGE = 4 const FTS5_DETAIL_COLUMNS = 2 const FTS5_DETAIL_FULL = 0 const FTS5_DETAIL_NONE = 1 const FTS5_EOF = 0 const FTS5_LCP = 7 const FTS5_LP = 10 const FTS5_MAIN_PREFIX = 48 const FTS5_MAX_LEVEL = 64 const FTS5_MAX_PAGE_SIZE = 65536 const FTS5_MAX_PREFIX_INDEXES = 31 const FTS5_MAX_SEGMENT = 2000 const FTS5_MAX_TOKEN_SIZE = 32768 const FTS5_MERGE_NLIST = 16 const FTS5_MINUS = 6 const FTS5_MIN_DLIDX_SIZE = 4 const FTS5_NOINLINE = "SQLITE_NOINLINE" const FTS5_NOT = 3 const FTS5_OPT_WORK_UNIT = 1000 const FTS5_OR = 1 const FTS5_PATTERN_GLOB = 66 const FTS5_PATTERN_LIKE = 65 const FTS5_PATTERN_NONE = 0 const FTS5_PLAN_MATCH = 1 const FTS5_PLAN_ROWID = 6 const FTS5_PLAN_SCAN = 5 const FTS5_PLAN_SORTED_MATCH = 4 const FTS5_PLAN_SOURCE = 2 const FTS5_PLAN_SPECIAL = 3 const FTS5_PLUS = 14 const FTS5_PORTER_MAX_TOKEN = 64 const FTS5_RANK_NAME = "rank" const FTS5_RCP = 8 const FTS5_REMOVE_DIACRITICS_COMPLEX = 2 const FTS5_REMOVE_DIACRITICS_NONE = 0 const FTS5_REMOVE_DIACRITICS_SIMPLE = 1 const FTS5_ROWID_NAME = "rowid" const FTS5_RP = 11 const FTS5_SEGITER_ONETERM = 1 const FTS5_SEGITER_REVERSE = 2 const FTS5_STAR = 15 const FTS5_STMT_DELETE_CONTENT = 5 const FTS5_STMT_DELETE_DOCSIZE = 7 const FTS5_STMT_INSERT_CONTENT = 3 const FTS5_STMT_LOOKUP = 2 const FTS5_STMT_LOOKUP_DOCSIZE = 8 const FTS5_STMT_REPLACE_CONFIG = 9 const FTS5_STMT_REPLACE_CONTENT = 4 const FTS5_STMT_REPLACE_DOCSIZE = 6 const FTS5_STMT_SCAN = 10 const FTS5_STMT_SCAN_ASC = 0 const FTS5_STMT_SCAN_DESC = 1 const FTS5_STRING = 9 const FTS5_STRUCTURE_ROWID = 10 const FTS5_STRUCTURE_V2 = "\xff\x00\x00\x01" const FTS5_TERM = 4 const FTS5_TOKENIZE_AUX = 8 const FTS5_TOKENIZE_DOCUMENT = 4 const FTS5_TOKENIZE_PREFIX = 2 const FTS5_TOKENIZE_QUERY = 1 const FTS5_TOKEN_COLOCATED = 1 const FTS5_VOCAB_COL = 0 const FTS5_VOCAB_COL_SCHEMA = "term, col, doc, cnt" const FTS5_VOCAB_INSTANCE = 2 const FTS5_VOCAB_INST_SCHEMA = "term, doc, col, offset" const FTS5_VOCAB_ROW = 1 const FTS5_VOCAB_ROW_SCHEMA = "term, doc, cnt" const FTS5_VOCAB_TERM_EQ = 1 const FTS5_VOCAB_TERM_GE = 2 const FTS5_VOCAB_TERM_LE = 4 const FTS5_WORK_UNIT = 64 const FULLY_WITHIN = 2 const FUNC_PERFECT_MATCH = 6 const F_ADD_SEALS = 1033 const F_DUPFD = 0 const F_DUPFD_CLOEXEC = 1030 const F_EXLCK = 4 const F_GETFD = 1 const F_GETFL = 3 const F_GETLEASE = 1025 const F_GETLK = 12 const F_GETLK64 = 12 const F_GETOWN = 9 const F_GETOWN_EX = 16 const F_GETPIPE_SZ = 1032 const F_GETSIG = 11 const F_GET_FILE_RW_HINT = 1037 const F_GET_RW_HINT = 1035 const F_GET_SEALS = 1034 const F_LOCK = 1 const F_NOTIFY = 1026 const F_OFD_GETLK = 36 const F_OFD_SETLK = 37 const F_OFD_SETLKW = 38 const F_OK = 0 const F_RDLCK = 0 const F_SEAL_FUTURE_WRITE = 16 const F_SEAL_GROW = 4 const F_SEAL_SEAL = 1 const F_SEAL_SHRINK = 2 const F_SEAL_WRITE = 8 const F_SETFD = 2 const F_SETFL = 4 const F_SETLEASE = 1024 const F_SETLK = 13 const F_SETLK64 = 13 const F_SETLKW = 14 const F_SETLKW64 = 14 const F_SETOWN = 8 const F_SETOWN_EX = 15 const F_SETPIPE_SZ = 1031 const F_SETSIG = 10 const F_SET_FILE_RW_HINT = 1038 const F_SET_RW_HINT = 1036 const F_SHLCK = 8 const F_TEST = 3 const F_TLOCK = 2 const F_ULOCK = 0 const F_UNLCK = 2 const F_WRLCK = 1 const GCC_VERSION = 12002000 const GEOPOLY_PI = 3.141592653589793 const HASHSIZE = 97 const HASHTABLE_HASH_1 = 383 const HASHTABLE_NPAGE = 4096 const HASHTABLE_NPAGE_ONE = 4096 const HASHTABLE_NSLOT = 8192 const HAVE_FCHOWN = 1 const HAVE_FULLFSYNC = 0 const HAVE_GETHOSTUUID = 0 const HAVE_LSTAT = 1 const HAVE_MREMAP = 1 const HAVE_PREAD = 1 const HAVE_PWRITE = 1 const HAVE_READLINK = 1 const HAVE_USLEEP = 1 const HUGE_VAL = 0 const HUGE_VALF = 0 const HUGE_VALL = 0 const HUGE_VAL_F32 = 0 const HUGE_VAL_F32X = 0 const HUGE_VAL_F64 = 0 const INCRINIT_NORMAL = 0 const INCRINIT_ROOT = 2 const INCRINIT_TASK = 1 const INFINITY = 0 const INITFLAG_AlterAdd = 3 const INITFLAG_AlterDrop = 2 const INITFLAG_AlterMask = 3 const INITFLAG_AlterRename = 1 const INLINEFUNC_affinity = 4 const INLINEFUNC_coalesce = 0 const INLINEFUNC_expr_compare = 3 const INLINEFUNC_expr_implies_expr = 2 const INLINEFUNC_iif = 5 const INLINEFUNC_implies_nonnull_row = 1 const INLINEFUNC_sqlite_offset = 6 const INLINEFUNC_unlikely = 99 const INTERFACE = 1 const IN_INDEX_EPH = 2 const IN_INDEX_INDEX_ASC = 3 const IN_INDEX_INDEX_DESC = 4 const IN_INDEX_LOOP = 4 const IN_INDEX_MEMBERSHIP = 2 const IN_INDEX_NOOP = 5 const IN_INDEX_NOOP_OK = 1 const IN_INDEX_ROWID = 1 const IOCSIZE_MASK = 1073676288 const IOCSIZE_SHIFT = 16 const IOC_IN = 1073741824 const IOC_INOUT = 3221225472 const IOC_OUT = 2147483648 const IsStat4 = 1 const JEACH_ATOM = 3 const JEACH_FULLKEY = 6 const JEACH_ID = 4 const JEACH_JSON = 8 const JEACH_KEY = 0 const JEACH_PARENT = 5 const JEACH_PATH = 7 const JEACH_ROOT = 9 const JEACH_TYPE = 2 const JEACH_VALUE = 1 const JEDIT_DEL = 1 const JEDIT_INS = 3 const JEDIT_REPL = 2 const JEDIT_SET = 4 const JSONB_ARRAY = 11 const JSONB_FALSE = 2 const JSONB_FLOAT = 5 const JSONB_FLOAT5 = 6 const JSONB_INT = 3 const JSONB_INT5 = 4 const JSONB_NULL = 0 const JSONB_OBJECT = 12 const JSONB_TEXT = 7 const JSONB_TEXT5 = 9 const JSONB_TEXTJ = 8 const JSONB_TEXTRAW = 10 const JSONB_TRUE = 1 const JSON_ABPATH = 3 const JSON_BLOB = 8 const JSON_CACHE_ID = -429938 const JSON_CACHE_SIZE = 4 const JSON_EDITABLE = 1 const JSON_INVALID_CHAR = 629145 const JSON_ISSET = 4 const JSON_JSON = 1 const JSON_KEEPERROR = 2 const JSON_LOOKUP_ERROR = 4294967295 const JSON_LOOKUP_NOTFOUND = 4294967294 const JSON_LOOKUP_PATHERROR = 4294967293 const JSON_MAX_DEPTH = 1000 const JSON_MERGE_BADPATCH = 2 const JSON_MERGE_BADTARGET = 1 const JSON_MERGE_OK = 0 const JSON_MERGE_OOM = 3 const JSON_SQL = 2 const JSON_SUBTYPE = 74 const JSTRING_ERR = 4 const JSTRING_MALFORMED = 2 const JSTRING_OOM = 1 const JT_CROSS = 2 const JT_ERROR = 128 const JT_INNER = 1 const JT_LEFT = 8 const JT_LTORJ = 64 const JT_NATURAL = 4 const JT_OUTER = 32 const JT_RIGHT = 16 const KEYINFO_ORDER_BIGNULL = 2 const KEYINFO_ORDER_DESC = 1 const LEGACY_SCHEMA_TABLE = "sqlite_master" const LEGACY_TEMP_SCHEMA_TABLE = "sqlite_temp_master" const LITTLE_ENDIAN = 1234 const LM_ID_BASE = 0 const LM_ID_NEWLM = -1 const LOCATE_NOERR = 2 const LOCATE_VIEW = 1 const LOCK_EX = 2 const LOCK_MAND = 32 const LOCK_NB = 4 const LOCK_READ = 64 const LOCK_RW = 192 const LOCK_SH = 1 const LOCK_UN = 8 const LOCK_WRITE = 128 const LONGDOUBLE_TYPE = 0 const LOOKASIDE_SMALL = 128 const L_INCR = 1 const L_SET = 0 const L_XTND = 2 const L_ctermid = 9 const L_cuserid = 9 const L_tmpnam = 20 const M10d_Any = 1 const M10d_No = 2 const M10d_Yes = 0 const MADV_COLD = 20 const MADV_DODUMP = 17 const MADV_DOFORK = 11 const MADV_DONTDUMP = 16 const MADV_DONTFORK = 10 const MADV_DONTNEED = 4 const MADV_DONTNEED_LOCKED = 24 const MADV_FREE = 8 const MADV_HUGEPAGE = 14 const MADV_HWPOISON = 100 const MADV_KEEPONFORK = 19 const MADV_MERGEABLE = 12 const MADV_NOHUGEPAGE = 15 const MADV_NORMAL = 0 const MADV_PAGEOUT = 21 const MADV_POPULATE_READ = 22 const MADV_POPULATE_WRITE = 23 const MADV_RANDOM = 1 const MADV_REMOVE = 9 const MADV_SEQUENTIAL = 2 const MADV_UNMERGEABLE = 13 const MADV_WILLNEED = 3 const MADV_WIPEONFORK = 18 const MAP_ANON = 32 const MAP_ANONYMOUS = 32 const MAP_DENYWRITE = 2048 const MAP_EXECUTABLE = 4096 const MAP_FAILED = -1 const MAP_FILE = 0 const MAP_FIXED = 16 const MAP_FIXED_NOREPLACE = 1048576 const MAP_GROWSDOWN = 256 const MAP_HUGETLB = 262144 const MAP_HUGE_MASK = 63 const MAP_HUGE_SHIFT = 26 const MAP_LOCKED = 8192 const MAP_NONBLOCK = 65536 const MAP_NORESERVE = 16384 const MAP_POPULATE = 32768 const MAP_PRIVATE = 2 const MAP_SHARED = 1 const MAP_SHARED_VALIDATE = 3 const MAP_STACK = 131072 const MAP_SYNC = 524288 const MAP_TYPE = 15 const MATH_ERREXCEPT = 2 const MATH_ERRNO = 1 const MAXFLOAT = 0 const MAX_HANDLE_SZ = 128 const MAX_PATHNAME = 512 const MAX_SECTOR_SIZE = 65536 const MB_CUR_MAX = 0 const MCL_CURRENT = 1 const MCL_FUTURE = 2 const MCL_ONFAULT = 4 const MEMJOURNAL_DFLT_FILECHUNKSIZE = 1024 const MEMTYPE_HEAP = 1 const MEMTYPE_LOOKASIDE = 2 const MEMTYPE_PCACHE = 4 const MEM_AffMask = 63 const MEM_Agg = 32768 const MEM_Blob = 16 const MEM_Cleared = 256 const MEM_Dyn = 4096 const MEM_Ephem = 16384 const MEM_FromBind = 64 const MEM_Int = 4 const MEM_IntReal = 32 const MEM_Null = 1 const MEM_Real = 8 const MEM_Static = 8192 const MEM_Str = 2 const MEM_Subtype = 2048 const MEM_Term = 512 const MEM_TypeMask = 3519 const MEM_Undefined = 0 const MEM_Zero = 1024 const MFD_ALLOW_SEALING = 2 const MFD_CLOEXEC = 1 const MFD_HUGETLB = 4 const MLOCK_ONFAULT = 1 const MOD_CLKA = 32769 const MOD_CLKB = 16384 const MOD_ESTERROR = 8 const MOD_FREQUENCY = 2 const MOD_MAXERROR = 4 const MOD_MICRO = 4096 const MOD_NANO = 8192 const MOD_OFFSET = 1 const MOD_STATUS = 16 const MOD_TAI = 128 const MOD_TIMECONST = 32 const MREMAP_DONTUNMAP = 4 const MREMAP_FIXED = 2 const MREMAP_MAYMOVE = 1 const MSVC_VERSION = 0 const MS_ASYNC = 1 const MS_INVALIDATE = 2 const MS_SYNC = 4 const M_1_PI = 0 const M_1_PIf = 0 const M_1_PIf32 = 0 const M_1_PIf32x = 0 const M_1_PIf64 = 0 const M_1_PIl = 0 const M_2_PI = 0 const M_2_PIf = 0 const M_2_PIf32 = 0 const M_2_PIf32x = 0 const M_2_PIf64 = 0 const M_2_PIl = 0 const M_2_SQRTPI = 0 const M_2_SQRTPIf = 0 const M_2_SQRTPIf32 = 0 const M_2_SQRTPIf32x = 0 const M_2_SQRTPIf64 = 0 const M_2_SQRTPIl = 0 const M_E = 0 const M_Ef = 0 const M_Ef32 = 0 const M_Ef32x = 0 const M_Ef64 = 0 const M_El = 0 const M_LN10 = 0 const M_LN10f = 0 const M_LN10f32 = 0 const M_LN10f32x = 0 const M_LN10f64 = 0 const M_LN10l = 0 const M_LN2 = 0 const M_LN2f = 0 const M_LN2f32 = 0 const M_LN2f32x = 0 const M_LN2f64 = 0 const M_LN2l = 0 const M_LOG10E = 0 const M_LOG10Ef = 0 const M_LOG10Ef32 = 0 const M_LOG10Ef32x = 0 const M_LOG10Ef64 = 0 const M_LOG10El = 0 const M_LOG2E = 0 const M_LOG2Ef = 0 const M_LOG2Ef32 = 0 const M_LOG2Ef32x = 0 const M_LOG2Ef64 = 0 const M_LOG2El = 0 const M_PI = 3.141592653589793 const M_PI_2 = 0 const M_PI_2f = 0 const M_PI_2f32 = 0 const M_PI_2f32x = 0 const M_PI_2f64 = 0 const M_PI_2l = 0 const M_PI_4 = 0 const M_PI_4f = 0 const M_PI_4f32 = 0 const M_PI_4f32x = 0 const M_PI_4f64 = 0 const M_PI_4l = 0 const M_PIf = 0 const M_PIf32 = 0 const M_PIf32x = 0 const M_PIf64 = 0 const M_PIl = 0 const M_SQRT1_2 = 0 const M_SQRT1_2f = 0 const M_SQRT1_2f32 = 0 const M_SQRT1_2f32x = 0 const M_SQRT1_2f64 = 0 const M_SQRT1_2l = 0 const M_SQRT2 = 0 const M_SQRT2f = 0 const M_SQRT2f32 = 0 const M_SQRT2f32x = 0 const M_SQRT2f64 = 0 const M_SQRT2l = 0 const NAN = 0 const NB = 3 const NCC = 8 const NC_AllowAgg = 1 const NC_AllowWin = 16384 const NC_Complex = 8192 const NC_FromDDL = 262144 const NC_GenCol = 8 const NC_HasAgg = 16 const NC_HasWin = 32768 const NC_IdxExpr = 32 const NC_InAggFunc = 131072 const NC_IsCheck = 4 const NC_IsDDL = 65536 const NC_MinMaxAgg = 4096 const NC_NoSelect = 524288 const NC_OrderAgg = 134217728 const NC_PartIdx = 2 const NC_SelfRef = 46 const NC_Subquery = 64 const NC_UAggInfo = 256 const NC_UBaseReg = 1024 const NC_UEList = 128 const NC_UUpsert = 512 const NC_Where = 1048576 const NDEBUG = 1 const NFDBITS = "__NFDBITS" const NN = 1 const NOT_WITHIN = 0 const NO_LOCK = 0 const N_6PACK = 7 const N_AX25 = 5 const N_HCI = 15 const N_HDLC = 13 const N_IRDA = 11 const N_MASC = 8 const N_MOUSE = 2 const N_OR_COST = 3 const N_PPP = 3 const N_PROFIBUS_FDL = 10 const N_R3964 = 9 const N_SLIP = 1 const N_SMSBLOCK = 12 const N_SORT_BUCKET = 32 const N_STATEMENT = 8 const N_STRIP = 4 const N_SYNC_PPP = 14 const N_TTY = 0 const N_X25 = 6 const OE_Abort = 2 const OE_Cascade = 10 const OE_Default = 11 const OE_Fail = 3 const OE_Ignore = 4 const OE_None = 0 const OE_Replace = 5 const OE_Restrict = 7 const OE_Rollback = 1 const OE_SetDflt = 9 const OE_SetNull = 8 const OE_Update = 6 const OMIT_TEMPDB = 0 const ONEPASS_MULTI = 2 const ONEPASS_OFF = 0 const ONEPASS_SINGLE = 1 const OPFLAG_APPEND = 8 const OPFLAG_AUXDELETE = 4 const OPFLAG_BULKCSR = 1 const OPFLAG_BYTELENARG = 192 const OPFLAG_EPHEM = 1 const OPFLAG_FORDELETE = 8 const OPFLAG_ISNOOP = 64 const OPFLAG_ISUPDATE = 4 const OPFLAG_LASTROWID = 32 const OPFLAG_LENGTHARG = 64 const OPFLAG_NCHANGE = 1 const OPFLAG_NOCHNG = 1 const OPFLAG_NOCHNG_MAGIC = 109 const OPFLAG_P2ISREG = 16 const OPFLAG_PERMUTE = 1 const OPFLAG_PREFORMAT = 128 const OPFLAG_SAVEPOSITION = 2 const OPFLAG_SEEKEQ = 2 const OPFLAG_TYPEOFARG = 128 const OPFLAG_USESEEKRESULT = 16 const OPFLG_IN1 = 2 const OPFLG_IN2 = 4 const OPFLG_IN3 = 8 const OPFLG_JUMP = 1 const OPFLG_NCYCLE = 64 const OPFLG_OUT2 = 16 const OPFLG_OUT3 = 32 const OP_Abortable = 189 const OP_Add = 106 const OP_AddImm = 86 const OP_Affinity = 96 const OP_AggFinal = 165 const OP_AggInverse = 161 const OP_AggStep = 162 const OP_AggStep1 = 163 const OP_AggValue = 164 const OP_And = 44 const OP_AutoCommit = 1 const OP_BeginSubrtn = 74 const OP_BitAnd = 102 const OP_BitNot = 114 const OP_BitOr = 103 const OP_Blob = 77 const OP_Cast = 88 const OP_Checkpoint = 3 const OP_Clear = 145 const OP_Close = 122 const OP_ClrSubtype = 180 const OP_CollSeq = 85 const OP_Column = 94 const OP_ColumnsUsed = 123 const OP_Compare = 90 const OP_Concat = 111 const OP_Copy = 80 const OP_Count = 98 const OP_CreateBtree = 147 const OP_CursorHint = 185 const OP_CursorLock = 167 const OP_CursorUnlock = 168 const OP_DecrJumpZero = 61 const OP_DeferredSeek = 141 const OP_Delete = 130 const OP_Destroy = 144 const OP_Divide = 109 const OP_DropIndex = 152 const OP_DropTable = 151 const OP_DropTrigger = 154 const OP_ElseEq = 58 const OP_EndCoroutine = 68 const OP_Eq = 53 const OP_Expire = 166 const OP_Explain = 188 const OP_Filter = 64 const OP_FilterAdd = 183 const OP_FinishSeek = 143 const OP_FkCheck = 83 const OP_FkCounter = 158 const OP_FkIfZero = 49 const OP_Found = 29 const OP_Function = 66 const OP_Ge = 57 const OP_GetSubtype = 181 const OP_Gosub = 10 const OP_Goto = 9 const OP_Gt = 54 const OP_Halt = 70 const OP_HaltIfNull = 69 const OP_IdxDelete = 140 const OP_IdxGE = 45 const OP_IdxGT = 41 const OP_IdxInsert = 138 const OP_IdxLE = 40 const OP_IdxLT = 42 const OP_IdxRowid = 142 const OP_If = 16 const OP_IfNoHope = 26 const OP_IfNot = 17 const OP_IfNotOpen = 25 const OP_IfNotZero = 60 const OP_IfNullRow = 20 const OP_IfPos = 59 const OP_IfSmaller = 33 const OP_IncrVacuum = 62 const OP_Init = 8 const OP_InitCoroutine = 11 const OP_Insert = 128 const OP_Int64 = 72 const OP_IntCopy = 82 const OP_Integer = 71 const OP_IntegrityCk = 155 const OP_IsNull = 50 const OP_IsTrue = 91 const OP_IsType = 18 const OP_JournalMode = 4 const OP_Jump = 14 const OP_Last = 32 const OP_Le = 55 const OP_LoadAnalysis = 150 const OP_Lt = 56 const OP_MakeRecord = 97 const OP_MaxPgcnt = 179 const OP_MemMax = 159 const OP_Move = 79 const OP_Multiply = 108 const OP_MustBeInt = 13 const OP_Ne = 52 const OP_NewRowid = 127 const OP_Next = 39 const OP_NoConflict = 27 const OP_Noop = 187 const OP_Not = 19 const OP_NotExists = 31 const OP_NotFound = 28 const OP_NotNull = 51 const OP_Null = 75 const OP_NullRow = 136 const OP_Offset = 93 const OP_OffsetLimit = 160 const OP_Once = 15 const OP_OpenAutoindex = 116 const OP_OpenDup = 115 const OP_OpenEphemeral = 118 const OP_OpenPseudo = 121 const OP_OpenRead = 112 const OP_OpenWrite = 113 const OP_Or = 43 const OP_Pagecount = 178 const OP_Param = 157 const OP_ParseSchema = 149 const OP_Permutation = 89 const OP_Prev = 38 const OP_Program = 48 const OP_PureFunc = 65 const OP_ReadCookie = 99 const OP_Real = 153 const OP_RealAffinity = 87 const OP_ReleaseReg = 186 const OP_Remainder = 110 const OP_ReopenIdx = 101 const OP_ResetCount = 131 const OP_ResetSorter = 146 const OP_ResultRow = 84 const OP_Return = 67 const OP_Rewind = 36 const OP_RowCell = 129 const OP_RowData = 134 const OP_RowSetAdd = 156 const OP_RowSetRead = 46 const OP_RowSetTest = 47 const OP_Rowid = 135 const OP_SCopy = 81 const OP_Savepoint = 0 const OP_SeekEnd = 137 const OP_SeekGE = 23 const OP_SeekGT = 24 const OP_SeekHit = 125 const OP_SeekLE = 22 const OP_SeekLT = 21 const OP_SeekRowid = 30 const OP_SeekScan = 124 const OP_Sequence = 126 const OP_SequenceTest = 120 const OP_SetCookie = 100 const OP_SetSubtype = 182 const OP_ShiftLeft = 104 const OP_ShiftRight = 105 const OP_SoftNull = 76 const OP_Sort = 35 const OP_SorterCompare = 132 const OP_SorterData = 133 const OP_SorterInsert = 139 const OP_SorterNext = 37 const OP_SorterOpen = 119 const OP_SorterSort = 34 const OP_SqlExec = 148 const OP_String = 73 const OP_String8 = 117 const OP_Subtract = 107 const OP_TableLock = 169 const OP_Trace = 184 const OP_Transaction = 2 const OP_TypeCheck = 95 const OP_VBegin = 170 const OP_VCheck = 174 const OP_VColumn = 176 const OP_VCreate = 171 const OP_VDestroy = 172 const OP_VFilter = 6 const OP_VInitIn = 175 const OP_VNext = 63 const OP_VOpen = 173 const OP_VRename = 177 const OP_VUpdate = 7 const OP_Vacuum = 5 const OP_Variable = 78 const OP_Yield = 12 const OP_ZeroOrNull = 92 const OS_VXWORKS = 0 const O_ACCMODE = 3 const O_APPEND = 1024 const O_ASYNC = 8192 const O_BINARY = 0 const O_CLOEXEC = 524288 const O_CREAT = 64 const O_DIRECT = 65536 const O_DIRECTORY = 16384 const O_DSYNC = 4096 const O_EXCL = 128 const O_FSYNC = 1052672 const O_LARGEFILE = 131072 const O_NDELAY = 2048 const O_NOATIME = 262144 const O_NOCTTY = 256 const O_NOFOLLOW = 32768 const O_NONBLOCK = 2048 const O_PATH = 2097152 const O_RDONLY = 0 const O_RDWR = 2 const O_RSYNC = 1052672 const O_SYNC = 1052672 const O_TMPFILE = 4210688 const O_TRUNC = 512 const O_WRONLY = 1 const P4_COLLSEQ = -2 const P4_DYNAMIC = -6 const P4_EXPR = -9 const P4_FREE_IF_LE = -6 const P4_FUNCCTX = -15 const P4_FUNCDEF = -7 const P4_INT32 = -3 const P4_INT64 = -13 const P4_INTARRAY = -14 const P4_KEYINFO = -8 const P4_MEM = -10 const P4_NOTUSED = 0 const P4_REAL = -12 const P4_STATIC = -1 const P4_SUBPROGRAM = -4 const P4_TABLE = -5 const P4_TABLEREF = -16 const P4_TRANSIENT = 0 const P4_VTAB = -11 const P5_ConstraintCheck = 3 const P5_ConstraintFK = 4 const P5_ConstraintNotNull = 1 const P5_ConstraintUnique = 2 const PAGER_CACHESPILL = 32 const PAGER_CKPT_FULLFSYNC = 16 const PAGER_ERROR = 6 const PAGER_FLAGS_MASK = 56 const PAGER_FULLFSYNC = 8 const PAGER_GET_NOCONTENT = 1 const PAGER_GET_READONLY = 2 const PAGER_JOURNALMODE_DELETE = 0 const PAGER_JOURNALMODE_MEMORY = 4 const PAGER_JOURNALMODE_OFF = 2 const PAGER_JOURNALMODE_PERSIST = 1 const PAGER_JOURNALMODE_QUERY = -1 const PAGER_JOURNALMODE_TRUNCATE = 3 const PAGER_JOURNALMODE_WAL = 5 const PAGER_LOCKINGMODE_EXCLUSIVE = 1 const PAGER_LOCKINGMODE_NORMAL = 0 const PAGER_LOCKINGMODE_QUERY = -1 const PAGER_MEMORY = 2 const PAGER_OMIT_JOURNAL = 1 const PAGER_OPEN = 0 const PAGER_READER = 1 const PAGER_STAT_HIT = 0 const PAGER_STAT_MISS = 1 const PAGER_STAT_SPILL = 3 const PAGER_STAT_WRITE = 2 const PAGER_SYNCHRONOUS_EXTRA = 4 const PAGER_SYNCHRONOUS_FULL = 3 const PAGER_SYNCHRONOUS_MASK = 7 const PAGER_SYNCHRONOUS_NORMAL = 2 const PAGER_SYNCHRONOUS_OFF = 1 const PAGER_WRITER_CACHEMOD = 3 const PAGER_WRITER_DBMOD = 4 const PAGER_WRITER_FINISHED = 5 const PAGER_WRITER_LOCKED = 2 const PARSE_MODE_DECLARE_VTAB = 1 const PARSE_MODE_NORMAL = 0 const PARSE_MODE_RENAME = 2 const PARSE_MODE_UNMAP = 3 const PARTLY_WITHIN = 1 const PCACHE1_MIGHT_USE_GROUP_MUTEX = 1 const PCACHE_DIRTYLIST_ADD = 2 const PCACHE_DIRTYLIST_FRONT = 3 const PCACHE_DIRTYLIST_REMOVE = 1 const PDP_ENDIAN = 3412 const PENDING_BYTE = 0 const PENDING_LOCK = 3 const PGHDR_CLEAN = 1 const PGHDR_DIRTY = 2 const PGHDR_DONT_WRITE = 16 const PGHDR_MMAP = 32 const PGHDR_NEED_SYNC = 8 const PGHDR_WAL_APPEND = 64 const PGHDR_WRITEABLE = 4 const PKEY_DISABLE_ACCESS = 1 const PKEY_DISABLE_WRITE = 2 const POSIX_FADV_DONTNEED = 4 const POSIX_FADV_NOREUSE = 5 const POSIX_FADV_NORMAL = 0 const POSIX_FADV_RANDOM = 1 const POSIX_FADV_SEQUENTIAL = 2 const POSIX_FADV_WILLNEED = 3 const POSIX_MADV_DONTNEED = 4 const POSIX_MADV_NORMAL = 0 const POSIX_MADV_RANDOM = 1 const POSIX_MADV_SEQUENTIAL = 2 const POSIX_MADV_WILLNEED = 3 const PREFERRED_SCHEMA_TABLE = "sqlite_schema" const PREFERRED_TEMP_SCHEMA_TABLE = "sqlite_temp_schema" const PROT_EXEC = 4 const PROT_GROWSDOWN = 16777216 const PROT_GROWSUP = 33554432 const PROT_NONE = 0 const PROT_READ = 1 const PROT_WRITE = 2 const PTF_INTKEY = 1 const PTF_LEAF = 8 const PTF_LEAFDATA = 4 const PTF_ZERODATA = 2 const PTRMAP_BTREE = 5 const PTRMAP_FREEPAGE = 2 const PTRMAP_OVERFLOW1 = 3 const PTRMAP_OVERFLOW2 = 4 const PTRMAP_ROOTPAGE = 1 const P_tmpdir = "/tmp" const PragFlg_NeedSchema = 1 const PragFlg_NoColumns = 2 const PragFlg_NoColumns1 = 4 const PragFlg_ReadOnly = 8 const PragFlg_Result0 = 16 const PragFlg_Result1 = 32 const PragFlg_SchemaOpt = 64 const PragFlg_SchemaReq = 128 const PragTyp_ACTIVATE_EXTENSIONS = 0 const PragTyp_ANALYSIS_LIMIT = 1 const PragTyp_AUTO_VACUUM = 3 const PragTyp_BUSY_TIMEOUT = 5 const PragTyp_CACHE_SIZE = 6 const PragTyp_CACHE_SPILL = 7 const PragTyp_CASE_SENSITIVE_LIKE = 8 const PragTyp_COLLATION_LIST = 9 const PragTyp_COMPILE_OPTIONS = 10 const PragTyp_DATABASE_LIST = 12 const PragTyp_DATA_STORE_DIRECTORY = 11 const PragTyp_DEFAULT_CACHE_SIZE = 13 const PragTyp_ENCODING = 14 const PragTyp_FLAG = 4 const PragTyp_FOREIGN_KEY_CHECK = 15 const PragTyp_FOREIGN_KEY_LIST = 16 const PragTyp_FUNCTION_LIST = 17 const PragTyp_HARD_HEAP_LIMIT = 18 const PragTyp_HEADER_VALUE = 2 const PragTyp_INCREMENTAL_VACUUM = 19 const PragTyp_INDEX_INFO = 20 const PragTyp_INDEX_LIST = 21 const PragTyp_INTEGRITY_CHECK = 22 const PragTyp_JOURNAL_MODE = 23 const PragTyp_JOURNAL_SIZE_LIMIT = 24 const PragTyp_LOCKING_MODE = 26 const PragTyp_LOCK_PROXY_FILE = 25 const PragTyp_LOCK_STATUS = 44 const PragTyp_MMAP_SIZE = 28 const PragTyp_MODULE_LIST = 29 const PragTyp_OPTIMIZE = 30 const PragTyp_PAGE_COUNT = 27 const PragTyp_PAGE_SIZE = 31 const PragTyp_PRAGMA_LIST = 32 const PragTyp_SECURE_DELETE = 33 const PragTyp_SHRINK_MEMORY = 34 const PragTyp_SOFT_HEAP_LIMIT = 35 const PragTyp_STATS = 45 const PragTyp_SYNCHRONOUS = 36 const PragTyp_TABLE_INFO = 37 const PragTyp_TABLE_LIST = 38 const PragTyp_TEMP_STORE = 39 const PragTyp_TEMP_STORE_DIRECTORY = 40 const PragTyp_THREADS = 41 const PragTyp_WAL_AUTOCHECKPOINT = 42 const PragTyp_WAL_CHECKPOINT = 43 const RAND_MAX = 2147483647 const RBU_CREATE_STATE = "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)" const RBU_DELETE = 2 const RBU_ENABLE_DELTA_CKSUM = 0 const RBU_EXCLUSIVE_CHECKPOINT = "rbu_exclusive_checkpoint" const RBU_IDX_DELETE = 4 const RBU_IDX_INSERT = 5 const RBU_INSERT = 1 const RBU_PK_EXTERNAL = 3 const RBU_PK_IPK = 2 const RBU_PK_NONE = 1 const RBU_PK_NOTABLE = 0 const RBU_PK_VTAB = 5 const RBU_PK_WITHOUT_ROWID = 4 const RBU_REPLACE = 3 const RBU_STAGE_CAPTURE = 3 const RBU_STAGE_CKPT = 4 const RBU_STAGE_DONE = 5 const RBU_STAGE_MOVE = 2 const RBU_STAGE_OAL = 1 const RBU_STATE_CKPT = 6 const RBU_STATE_COOKIE = 7 const RBU_STATE_DATATBL = 10 const RBU_STATE_IDX = 3 const RBU_STATE_OALSZ = 8 const RBU_STATE_PHASEONESTEP = 9 const RBU_STATE_PROGRESS = 5 const RBU_STATE_ROW = 4 const RBU_STATE_STAGE = 1 const RBU_STATE_TBL = 2 const RBU_UPDATE = 6 const RBU_ZIPVFS_CTRL_FILE_POINTER = 230439 const READMARK_NOT_USED = 4294967295 const READ_LOCK = 1 const RENAME_EXCHANGE = 2 const RENAME_NOREPLACE = 1 const RENAME_WHITEOUT = 4 const RESERVED_BYTE = 1 const RESERVED_LOCK = 2 const RNDAWAY = 0 const RNDTOWARDS = 0 const ROWSET_ALLOCATION_SIZE = 1024 const ROWSET_ENTRY_PER_CHUNK = 1016 const ROWSET_NEXT = 2 const ROWSET_SORTED = 1 const RTLD_BINDING_MASK = 3 const RTLD_DEEPBIND = 8 const RTLD_GLOBAL = 256 const RTLD_LAZY = 1 const RTLD_LOCAL = 0 const RTLD_NEXT = -1 const RTLD_NODELETE = 4096 const RTLD_NOLOAD = 4 const RTLD_NOW = 2 const RTREE_CACHE_SZ = 5 const RTREE_CHECK_MAX_ERROR = 100 const RTREE_COORD_INT32 = 1 const RTREE_COORD_REAL32 = 0 const RTREE_DEFAULT_ROWEST = 1048576 const RTREE_EQ = 65 const RTREE_FALSE = 64 const RTREE_GE = 68 const RTREE_GT = 69 const RTREE_LE = 66 const RTREE_LT = 67 const RTREE_MATCH = 70 const RTREE_MAXCELLS = 51 const RTREE_MAX_AUX_COLUMN = 100 const RTREE_MAX_DEPTH = 40 const RTREE_MAX_DIMENSIONS = 5 const RTREE_MIN_ROWEST = 100 const RTREE_QUERY = 71 const RTREE_TRUE = 63 const RTREE_ZERO = 0 const RWF_WRITE_LIFE_NOT_SET = 0 const RWH_WRITE_LIFE_EXTREME = 5 const RWH_WRITE_LIFE_LONG = 4 const RWH_WRITE_LIFE_MEDIUM = 3 const RWH_WRITE_LIFE_NONE = 1 const RWH_WRITE_LIFE_NOT_SET = 0 const RWH_WRITE_LIFE_SHORT = 2 const R_OK = 4 const SAVEPOINT_BEGIN = 0 const SAVEPOINT_RELEASE = 1 const SAVEPOINT_ROLLBACK = 2 const SCHEMA_ROOT = 1 const SEEK_CUR = 1 const SEEK_DATA = 3 const SEEK_END = 2 const SEEK_HOLE = 4 const SEEK_SET = 0 const SESSIONS_ROWID = "_rowid_" const SESSIONS_STRM_CHUNK_SIZE = 1024 const SESSION_MAX_BUFFER_SZ = 2147483391 const SESSION_UPDATE_CACHE_SZ = 12 const SF_Aggregate = 8 const SF_All = 2 const SF_ComplexResult = 262144 const SF_Compound = 256 const SF_Converted = 65536 const SF_CopyCte = 67108864 const SF_Distinct = 1 const SF_Expanded = 64 const SF_FixedLimit = 16384 const SF_HasAgg = 16 const SF_HasTypeInfo = 128 const SF_IncludeHidden = 131072 const SF_MaybeConvert = 32768 const SF_MinMaxAgg = 4096 const SF_MultiPart = 33554432 const SF_MultiValue = 1024 const SF_NestedFrom = 2048 const SF_NoopOrderBy = 4194304 const SF_OrderByReqd = 134217728 const SF_PushDown = 16777216 const SF_Recursive = 8192 const SF_Resolved = 4 const SF_UFSrcCheck = 8388608 const SF_UpdateFrom = 268435456 const SF_UsesEphemeral = 32 const SF_Values = 512 const SF_View = 2097152 const SF_WhereBegin = 524288 const SF_WinRewrite = 1048576 const SHARED_FIRST = 2 const SHARED_LOCK = 1 const SHARED_SIZE = 510 const SIOCADDDLCI = 35200 const SIOCADDMULTI = 35121 const SIOCADDRT = 35083 const SIOCDARP = 35155 const SIOCDELDLCI = 35201 const SIOCDELMULTI = 35122 const SIOCDELRT = 35084 const SIOCDEVPRIVATE = 35312 const SIOCDIFADDR = 35126 const SIOCDRARP = 35168 const SIOCGARP = 35156 const SIOCGIFADDR = 35093 const SIOCGIFBR = 35136 const SIOCGIFBRDADDR = 35097 const SIOCGIFCONF = 35090 const SIOCGIFCOUNT = 35128 const SIOCGIFDSTADDR = 35095 const SIOCGIFENCAP = 35109 const SIOCGIFFLAGS = 35091 const SIOCGIFHWADDR = 35111 const SIOCGIFINDEX = 35123 const SIOCGIFMAP = 35184 const SIOCGIFMEM = 35103 const SIOCGIFMETRIC = 35101 const SIOCGIFMTU = 35105 const SIOCGIFNAME = 35088 const SIOCGIFNETMASK = 35099 const SIOCGIFPFLAGS = 35125 const SIOCGIFSLAVE = 35113 const SIOCGIFTXQLEN = 35138 const SIOCGRARP = 35169 const SIOCPROTOPRIVATE = 35296 const SIOCRTMSG = 35085 const SIOCSARP = 35157 const SIOCSIFADDR = 35094 const SIOCSIFBR = 35137 const SIOCSIFBRDADDR = 35098 const SIOCSIFDSTADDR = 35096 const SIOCSIFENCAP = 35110 const SIOCSIFFLAGS = 35092 const SIOCSIFHWADDR = 35108 const SIOCSIFHWBROADCAST = 35127 const SIOCSIFLINK = 35089 const SIOCSIFMAP = 35185 const SIOCSIFMEM = 35104 const SIOCSIFMETRIC = 35102 const SIOCSIFMTU = 35106 const SIOCSIFNAME = 35107 const SIOCSIFNETMASK = 35100 const SIOCSIFPFLAGS = 35124 const SIOCSIFSLAVE = 35120 const SIOCSIFTXQLEN = 35139 const SIOCSRARP = 35170 const SIOGIFINDEX = 35123 const SLOT_2_0 = 2080895 const SLOT_4_2_0 = 4028612735 const SNAN = 0 const SNANF = 0 const SNANF32 = 0 const SNANF32X = 0 const SNANF64 = 0 const SNANL = 0 const SORTER_MAX_MERGE_COUNT = 16 const SORTER_TYPE_INTEGER = 1 const SORTER_TYPE_TEXT = 2 const SORTFLAG_UseSorter = 1 const SPILLFLAG_NOSYNC = 4 const SPILLFLAG_OFF = 1 const SPILLFLAG_ROLLBACK = 2 const SPLICE_F_GIFT = 8 const SPLICE_F_MORE = 4 const SPLICE_F_MOVE = 1 const SPLICE_F_NONBLOCK = 2 const SQLITE3_TEXT = 3 const SQLITE_ABORT = 4 const SQLITE_ABORT_ROLLBACK = 516 const SQLITE_ACCESS_EXISTS = 0 const SQLITE_ACCESS_READ = 2 const SQLITE_ACCESS_READWRITE = 1 const SQLITE_AFF_BLOB = 65 const SQLITE_AFF_FLEXNUM = 70 const SQLITE_AFF_INTEGER = 68 const SQLITE_AFF_MASK = 71 const SQLITE_AFF_NONE = 64 const SQLITE_AFF_NUMERIC = 67 const SQLITE_AFF_REAL = 69 const SQLITE_AFF_TEXT = 66 const SQLITE_ALLOW_COVERING_INDEX_SCAN = 1 const SQLITE_ALTER_TABLE = 26 const SQLITE_AMALGAMATION = 1 const SQLITE_ANALYZE = 28 const SQLITE_ANY = 5 const SQLITE_ASCII = 1 const SQLITE_ATOMIC_INTRINSICS = 1 const SQLITE_ATTACH = 24 const SQLITE_AUTH = 23 const SQLITE_AUTH_USER = 279 const SQLITE_AllOpts = 4294967295 const SQLITE_AutoIndex = 32768 const SQLITE_BIGENDIAN = 0 const SQLITE_BIG_DBL = 1e+99 const SQLITE_BLDF1_INDEXED = 1 const SQLITE_BLDF1_UNIQUE = 2 const SQLITE_BLDF2_2NDPASS = 4 const SQLITE_BLOB = 4 const SQLITE_BUSY = 5 const SQLITE_BUSY_RECOVERY = 261 const SQLITE_BUSY_SNAPSHOT = 517 const SQLITE_BUSY_TIMEOUT = 773 const SQLITE_BYTEORDER = 1234 const SQLITE_BalancedMerge = 2097152 const SQLITE_BloomFilter = 524288 const SQLITE_BloomPulldown = 1048576 const SQLITE_CANTOPEN = 14 const SQLITE_CANTOPEN_BKPT = 0 const SQLITE_CANTOPEN_CONVPATH = 1038 const SQLITE_CANTOPEN_DIRTYWAL = 1294 const SQLITE_CANTOPEN_FULLPATH = 782 const SQLITE_CANTOPEN_ISDIR = 526 const SQLITE_CANTOPEN_NOTEMPDIR = 270 const SQLITE_CANTOPEN_SYMLINK = 1550 const SQLITE_CHANGESETAPPLY_FKNOACTION = 8 const SQLITE_CHANGESETAPPLY_IGNORENOOP = 4 const SQLITE_CHANGESETAPPLY_INVERT = 2 const SQLITE_CHANGESETAPPLY_NOSAVEPOINT = 1 const SQLITE_CHANGESETSTART_INVERT = 2 const SQLITE_CHANGESET_ABORT = 2 const SQLITE_CHANGESET_CONFLICT = 3 const SQLITE_CHANGESET_CONSTRAINT = 4 const SQLITE_CHANGESET_DATA = 1 const SQLITE_CHANGESET_FOREIGN_KEY = 5 const SQLITE_CHANGESET_NOTFOUND = 2 const SQLITE_CHANGESET_OMIT = 0 const SQLITE_CHANGESET_REPLACE = 1 const SQLITE_CHECKPOINT_FULL = 1 const SQLITE_CHECKPOINT_PASSIVE = 0 const SQLITE_CHECKPOINT_RESTART = 2 const SQLITE_CHECKPOINT_TRUNCATE = 3 const SQLITE_CONFIG_COVERING_INDEX_SCAN = 20 const SQLITE_CONFIG_GETMALLOC = 5 const SQLITE_CONFIG_GETMUTEX = 11 const SQLITE_CONFIG_GETPCACHE = 15 const SQLITE_CONFIG_GETPCACHE2 = 19 const SQLITE_CONFIG_HEAP = 8 const SQLITE_CONFIG_LOG = 16 const SQLITE_CONFIG_LOOKASIDE = 13 const SQLITE_CONFIG_MALLOC = 4 const SQLITE_CONFIG_MEMDB_MAXSIZE = 29 const SQLITE_CONFIG_MEMSTATUS = 9 const SQLITE_CONFIG_MMAP_SIZE = 22 const SQLITE_CONFIG_MULTITHREAD = 2 const SQLITE_CONFIG_MUTEX = 10 const SQLITE_CONFIG_PAGECACHE = 7 const SQLITE_CONFIG_PCACHE = 14 const SQLITE_CONFIG_PCACHE2 = 18 const SQLITE_CONFIG_PCACHE_HDRSZ = 24 const SQLITE_CONFIG_PMASZ = 25 const SQLITE_CONFIG_ROWID_IN_VIEW = 30 const SQLITE_CONFIG_SCRATCH = 6 const SQLITE_CONFIG_SERIALIZED = 3 const SQLITE_CONFIG_SINGLETHREAD = 1 const SQLITE_CONFIG_SMALL_MALLOC = 27 const SQLITE_CONFIG_SORTERREF_SIZE = 28 const SQLITE_CONFIG_SQLLOG = 21 const SQLITE_CONFIG_STMTJRNL_SPILL = 26 const SQLITE_CONFIG_URI = 17 const SQLITE_CONFIG_WIN32_HEAPSIZE = 23 const SQLITE_CONSTRAINT = 19 const SQLITE_CONSTRAINT_CHECK = 275 const SQLITE_CONSTRAINT_COMMITHOOK = 531 const SQLITE_CONSTRAINT_DATATYPE = 3091 const SQLITE_CONSTRAINT_FOREIGNKEY = 787 const SQLITE_CONSTRAINT_FUNCTION = 1043 const SQLITE_CONSTRAINT_NOTNULL = 1299 const SQLITE_CONSTRAINT_PINNED = 2835 const SQLITE_CONSTRAINT_PRIMARYKEY = 1555 const SQLITE_CONSTRAINT_ROWID = 2579 const SQLITE_CONSTRAINT_TRIGGER = 1811 const SQLITE_CONSTRAINT_UNIQUE = 2067 const SQLITE_CONSTRAINT_VTAB = 2323 const SQLITE_COPY = 0 const SQLITE_CORE = 1 const SQLITE_CORRUPT = 11 const SQLITE_CORRUPT_BKPT = 0 const SQLITE_CORRUPT_INDEX = 779 const SQLITE_CORRUPT_SEQUENCE = 523 const SQLITE_CORRUPT_VTAB = 267 const SQLITE_CREATE_INDEX = 1 const SQLITE_CREATE_TABLE = 2 const SQLITE_CREATE_TEMP_INDEX = 3 const SQLITE_CREATE_TEMP_TABLE = 4 const SQLITE_CREATE_TEMP_TRIGGER = 5 const SQLITE_CREATE_TEMP_VIEW = 6 const SQLITE_CREATE_TRIGGER = 7 const SQLITE_CREATE_VIEW = 8 const SQLITE_CREATE_VTABLE = 29 const SQLITE_CacheSpill = 32 const SQLITE_CellSizeCk = 2097152 const SQLITE_CkptFullFSync = 16 const SQLITE_Coroutines = 33554432 const SQLITE_CountOfView = 512 const SQLITE_CoverIdxScan = 32 const SQLITE_CursorHints = 1024 const SQLITE_DBCONFIG_DEFENSIVE = 1010 const SQLITE_DBCONFIG_DQS_DDL = 1014 const SQLITE_DBCONFIG_DQS_DML = 1013 const SQLITE_DBCONFIG_ENABLE_FKEY = 1002 const SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER = 1004 const SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION = 1005 const SQLITE_DBCONFIG_ENABLE_QPSG = 1007 const SQLITE_DBCONFIG_ENABLE_TRIGGER = 1003 const SQLITE_DBCONFIG_ENABLE_VIEW = 1015 const SQLITE_DBCONFIG_LEGACY_ALTER_TABLE = 1012 const SQLITE_DBCONFIG_LEGACY_FILE_FORMAT = 1016 const SQLITE_DBCONFIG_LOOKASIDE = 1001 const SQLITE_DBCONFIG_MAINDBNAME = 1000 const SQLITE_DBCONFIG_MAX = 1019 const SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE = 1006 const SQLITE_DBCONFIG_RESET_DATABASE = 1009 const SQLITE_DBCONFIG_REVERSE_SCANORDER = 1019 const SQLITE_DBCONFIG_STMT_SCANSTATUS = 1018 const SQLITE_DBCONFIG_TRIGGER_EQP = 1008 const SQLITE_DBCONFIG_TRUSTED_SCHEMA = 1017 const SQLITE_DBCONFIG_WRITABLE_SCHEMA = 1011 const SQLITE_DBSTATUS_CACHE_HIT = 7 const SQLITE_DBSTATUS_CACHE_MISS = 8 const SQLITE_DBSTATUS_CACHE_SPILL = 12 const SQLITE_DBSTATUS_CACHE_USED = 1 const SQLITE_DBSTATUS_CACHE_USED_SHARED = 11 const SQLITE_DBSTATUS_CACHE_WRITE = 9 const SQLITE_DBSTATUS_DEFERRED_FKS = 10 const SQLITE_DBSTATUS_LOOKASIDE_HIT = 4 const SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL = 6 const SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE = 5 const SQLITE_DBSTATUS_LOOKASIDE_USED = 0 const SQLITE_DBSTATUS_MAX = 12 const SQLITE_DBSTATUS_SCHEMA_USED = 2 const SQLITE_DBSTATUS_STMT_USED = 3 const SQLITE_DEFAULT_AUTOVACUUM = 0 const SQLITE_DEFAULT_CACHE_SIZE = -2000 const SQLITE_DEFAULT_FILE_FORMAT = 4 const SQLITE_DEFAULT_FILE_PERMISSIONS = 420 const SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT = -1 const SQLITE_DEFAULT_LOOKASIDE = 40 const SQLITE_DEFAULT_MEMSTATUS = 0 const SQLITE_DEFAULT_MMAP_SIZE = 0 const SQLITE_DEFAULT_PAGE_SIZE = 4096 const SQLITE_DEFAULT_PCACHE_INITSZ = 20 const SQLITE_DEFAULT_PROXYDIR_PERMISSIONS = 493 const SQLITE_DEFAULT_RECURSIVE_TRIGGERS = 0 const SQLITE_DEFAULT_SECTOR_SIZE = 4096 const SQLITE_DEFAULT_SORTERREF_SIZE = 2147483647 const SQLITE_DEFAULT_SYNCHRONOUS = 2 const SQLITE_DEFAULT_WAL_AUTOCHECKPOINT = 1000 const SQLITE_DEFAULT_WAL_SYNCHRONOUS = 2 const SQLITE_DEFAULT_WORKER_THREADS = 0 const SQLITE_DELETE = 9 const SQLITE_DENY = 1 const SQLITE_DESERIALIZE_FREEONCLOSE = 1 const SQLITE_DESERIALIZE_READONLY = 4 const SQLITE_DESERIALIZE_RESIZEABLE = 2 const SQLITE_DETACH = 25 const SQLITE_DETERMINISTIC = 2048 const SQLITE_DIRECTONLY = 524288 const SQLITE_DIRECT_OVERFLOW_READ = 1 const SQLITE_DONE = 101 const SQLITE_DQS = 3 const SQLITE_DROP_INDEX = 10 const SQLITE_DROP_TABLE = 11 const SQLITE_DROP_TEMP_INDEX = 12 const SQLITE_DROP_TEMP_TABLE = 13 const SQLITE_DROP_TEMP_TRIGGER = 14 const SQLITE_DROP_TEMP_VIEW = 15 const SQLITE_DROP_TRIGGER = 16 const SQLITE_DROP_VIEW = 17 const SQLITE_DROP_VTABLE = 30 const SQLITE_Defensive = 268435456 const SQLITE_DeferFKs = 524288 const SQLITE_DistinctOpt = 16 const SQLITE_DqsDDL = 536870912 const SQLITE_DqsDML = 1073741824 const SQLITE_ECEL_DUP = 1 const SQLITE_ECEL_FACTOR = 2 const SQLITE_ECEL_OMITREF = 8 const SQLITE_ECEL_REF = 4 const SQLITE_EMPTY = 16 const SQLITE_ENABLE_COLUMN_METADATA = 1 const SQLITE_ENABLE_DBSTAT_VTAB = 1 const SQLITE_ENABLE_FTS5 = 1 const SQLITE_ENABLE_GEOPOLY = 1 const SQLITE_ENABLE_JSON1 = 1 const SQLITE_ENABLE_LOCKING_STYLE = 0 const SQLITE_ENABLE_MATH_FUNCTIONS = 1 const SQLITE_ENABLE_MEMORY_MANAGEMENT = 1 const SQLITE_ENABLE_OFFSET_SQL_FUNC = 1 const SQLITE_ENABLE_PREUPDATE_HOOK = 1 const SQLITE_ENABLE_RBU = 1 const SQLITE_ENABLE_RTREE = 1 const SQLITE_ENABLE_SESSION = 1 const SQLITE_ENABLE_SNAPSHOT = 1 const SQLITE_ENABLE_STAT4 = 1 const SQLITE_ENABLE_UNLOCK_NOTIFY = 1 const SQLITE_ERROR = 1 const SQLITE_ERROR_MISSING_COLLSEQ = 257 const SQLITE_ERROR_RETRY = 513 const SQLITE_ERROR_SNAPSHOT = 769 const SQLITE_EXTERN = 0 const SQLITE_EnableQPSG = 8388608 const SQLITE_EnableTrigger = 262144 const SQLITE_EnableView = 2147483648 const SQLITE_FAIL = 3 const SQLITE_FAULTINJECTOR_COUNT = 1 const SQLITE_FAULTINJECTOR_MALLOC = 0 const SQLITE_FCNTL_BEGIN_ATOMIC_WRITE = 31 const SQLITE_FCNTL_BUSYHANDLER = 15 const SQLITE_FCNTL_CHUNK_SIZE = 6 const SQLITE_FCNTL_CKPT_DONE = 37 const SQLITE_FCNTL_CKPT_START = 39 const SQLITE_FCNTL_CKSM_FILE = 41 const SQLITE_FCNTL_COMMIT_ATOMIC_WRITE = 32 const SQLITE_FCNTL_COMMIT_PHASETWO = 22 const SQLITE_FCNTL_DATA_VERSION = 35 const SQLITE_FCNTL_DB_UNCHANGED = 3389603744 const SQLITE_FCNTL_EXTERNAL_READER = 40 const SQLITE_FCNTL_FILE_POINTER = 7 const SQLITE_FCNTL_GET_LOCKPROXYFILE = 2 const SQLITE_FCNTL_HAS_MOVED = 20 const SQLITE_FCNTL_JOURNAL_POINTER = 28 const SQLITE_FCNTL_LAST_ERRNO = 4 const SQLITE_FCNTL_LOCKSTATE = 1 const SQLITE_FCNTL_LOCK_TIMEOUT = 34 const SQLITE_FCNTL_MMAP_SIZE = 18 const SQLITE_FCNTL_OVERWRITE = 11 const SQLITE_FCNTL_PDB = 30 const SQLITE_FCNTL_PERSIST_WAL = 10 const SQLITE_FCNTL_POWERSAFE_OVERWRITE = 13 const SQLITE_FCNTL_PRAGMA = 14 const SQLITE_FCNTL_RBU = 26 const SQLITE_FCNTL_RBUCNT = 5149216 const SQLITE_FCNTL_RESERVE_BYTES = 38 const SQLITE_FCNTL_RESET_CACHE = 42 const SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE = 33 const SQLITE_FCNTL_SET_LOCKPROXYFILE = 3 const SQLITE_FCNTL_SIZE_HINT = 5 const SQLITE_FCNTL_SIZE_LIMIT = 36 const SQLITE_FCNTL_SYNC = 21 const SQLITE_FCNTL_SYNC_OMITTED = 8 const SQLITE_FCNTL_TEMPFILENAME = 16 const SQLITE_FCNTL_TRACE = 19 const SQLITE_FCNTL_VFSNAME = 12 const SQLITE_FCNTL_VFS_POINTER = 27 const SQLITE_FCNTL_WAL_BLOCK = 24 const SQLITE_FCNTL_WIN32_AV_RETRY = 9 const SQLITE_FCNTL_WIN32_GET_HANDLE = 29 const SQLITE_FCNTL_WIN32_SET_HANDLE = 23 const SQLITE_FCNTL_ZIPVFS = 25 const SQLITE_FILE_HEADER = "SQLite format 3" const SQLITE_FLOAT = 2 const SQLITE_FORMAT = 24 const SQLITE_FP_PRECISION_LIMIT = 100000000 const SQLITE_FRAME_MAGIC = 2275391262 const SQLITE_FSFLAGS_IS_MSDOS = 1 const SQLITE_FTS5_MAX_EXPR_DEPTH = 256 const SQLITE_FULL = 13 const SQLITE_FUNCTION = 31 const SQLITE_FUNC_ANYORDER = 134217728 const SQLITE_FUNC_BUILTIN = 8388608 const SQLITE_FUNC_BYTELEN = 192 const SQLITE_FUNC_CASE = 8 const SQLITE_FUNC_CONSTANT = 2048 const SQLITE_FUNC_COUNT = 256 const SQLITE_FUNC_DIRECT = 524288 const SQLITE_FUNC_ENCMASK = 3 const SQLITE_FUNC_EPHEM = 16 const SQLITE_FUNC_HASH_SZ = 23 const SQLITE_FUNC_INLINE = 4194304 const SQLITE_FUNC_INTERNAL = 262144 const SQLITE_FUNC_LENGTH = 64 const SQLITE_FUNC_LIKE = 4 const SQLITE_FUNC_MINMAX = 4096 const SQLITE_FUNC_NEEDCOLL = 32 const SQLITE_FUNC_RUNONLY = 32768 const SQLITE_FUNC_SLOCHNG = 8192 const SQLITE_FUNC_TEST = 16384 const SQLITE_FUNC_TYPEOF = 128 const SQLITE_FUNC_UNLIKELY = 1024 const SQLITE_FUNC_UNSAFE = 2097152 const SQLITE_FUNC_WINDOW = 65536 const SQLITE_FactorOutConst = 8 const SQLITE_FlttnUnionAll = 8388608 const SQLITE_ForeignKeys = 16384 const SQLITE_Fts3Tokenizer = 4194304 const SQLITE_FullColNames = 4 const SQLITE_FullFSync = 8 const SQLITE_GET_LOCKPROXYFILE = 2 const SQLITE_GroupByOrder = 4 const SQLITE_HAVE_C99_MATH_FUNCS = 1 const SQLITE_HAVE_ZLIB = 1 const SQLITE_IDXTYPE_APPDEF = 0 const SQLITE_IDXTYPE_IPK = 3 const SQLITE_IDXTYPE_PRIMARYKEY = 2 const SQLITE_IDXTYPE_UNIQUE = 1 const SQLITE_IGNORE = 2 const SQLITE_INDEX_CONSTRAINT_EQ = 2 const SQLITE_INDEX_CONSTRAINT_FUNCTION = 150 const SQLITE_INDEX_CONSTRAINT_GE = 32 const SQLITE_INDEX_CONSTRAINT_GLOB = 66 const SQLITE_INDEX_CONSTRAINT_GT = 4 const SQLITE_INDEX_CONSTRAINT_IS = 72 const SQLITE_INDEX_CONSTRAINT_ISNOT = 69 const SQLITE_INDEX_CONSTRAINT_ISNOTNULL = 70 const SQLITE_INDEX_CONSTRAINT_ISNULL = 71 const SQLITE_INDEX_CONSTRAINT_LE = 8 const SQLITE_INDEX_CONSTRAINT_LIKE = 65 const SQLITE_INDEX_CONSTRAINT_LIMIT = 73 const SQLITE_INDEX_CONSTRAINT_LT = 16 const SQLITE_INDEX_CONSTRAINT_MATCH = 64 const SQLITE_INDEX_CONSTRAINT_NE = 68 const SQLITE_INDEX_CONSTRAINT_OFFSET = 74 const SQLITE_INDEX_CONSTRAINT_REGEXP = 67 const SQLITE_INDEX_SCAN_UNIQUE = 1 const SQLITE_INNOCUOUS = 2097152 const SQLITE_INSERT = 18 const SQLITE_INTEGER = 1 const SQLITE_INTEGRITY_CHECK_ERROR_MAX = 100 const SQLITE_INTERNAL = 2 const SQLITE_INTERRUPT = 9 const SQLITE_IOCAP_ATOMIC = 1 const SQLITE_IOCAP_ATOMIC16K = 64 const SQLITE_IOCAP_ATOMIC1K = 4 const SQLITE_IOCAP_ATOMIC2K = 8 const SQLITE_IOCAP_ATOMIC32K = 128 const SQLITE_IOCAP_ATOMIC4K = 16 const SQLITE_IOCAP_ATOMIC512 = 2 const SQLITE_IOCAP_ATOMIC64K = 256 const SQLITE_IOCAP_ATOMIC8K = 32 const SQLITE_IOCAP_BATCH_ATOMIC = 16384 const SQLITE_IOCAP_IMMUTABLE = 8192 const SQLITE_IOCAP_POWERSAFE_OVERWRITE = 4096 const SQLITE_IOCAP_SAFE_APPEND = 512 const SQLITE_IOCAP_SEQUENTIAL = 1024 const SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN = 2048 const SQLITE_IOERR = 10 const SQLITE_IOERR_ACCESS = 3338 const SQLITE_IOERR_AUTH = 7178 const SQLITE_IOERR_BEGIN_ATOMIC = 7434 const SQLITE_IOERR_BLOCKED = 2826 const SQLITE_IOERR_CHECKRESERVEDLOCK = 3594 const SQLITE_IOERR_CLOSE = 4106 const SQLITE_IOERR_COMMIT_ATOMIC = 7690 const SQLITE_IOERR_CONVPATH = 6666 const SQLITE_IOERR_CORRUPTFS = 8458 const SQLITE_IOERR_DATA = 8202 const SQLITE_IOERR_DELETE = 2570 const SQLITE_IOERR_DELETE_NOENT = 5898 const SQLITE_IOERR_DIR_CLOSE = 4362 const SQLITE_IOERR_DIR_FSYNC = 1290 const SQLITE_IOERR_FSTAT = 1802 const SQLITE_IOERR_FSYNC = 1034 const SQLITE_IOERR_GETTEMPPATH = 6410 const SQLITE_IOERR_IN_PAGE = 8714 const SQLITE_IOERR_LOCK = 3850 const SQLITE_IOERR_MMAP = 6154 const SQLITE_IOERR_NOMEM = 3082 const SQLITE_IOERR_NOMEM_BKPT = 3082 const SQLITE_IOERR_RDLOCK = 2314 const SQLITE_IOERR_READ = 266 const SQLITE_IOERR_ROLLBACK_ATOMIC = 7946 const SQLITE_IOERR_SEEK = 5642 const SQLITE_IOERR_SHMLOCK = 5130 const SQLITE_IOERR_SHMMAP = 5386 const SQLITE_IOERR_SHMOPEN = 4618 const SQLITE_IOERR_SHMSIZE = 4874 const SQLITE_IOERR_SHORT_READ = 522 const SQLITE_IOERR_TRUNCATE = 1546 const SQLITE_IOERR_UNLOCK = 2058 const SQLITE_IOERR_VNODE = 6922 const SQLITE_IOERR_WRITE = 778 const SQLITE_IgnoreChecks = 512 const SQLITE_IndexedExpr = 16777216 const SQLITE_JUMPIFNULL = 16 const SQLITE_LAST_ERRNO = 4 const SQLITE_LIKE_DOESNT_MATCH_BLOBS = 1 const SQLITE_LIMIT_ATTACHED = 7 const SQLITE_LIMIT_COLUMN = 2 const SQLITE_LIMIT_COMPOUND_SELECT = 4 const SQLITE_LIMIT_EXPR_DEPTH = 3 const SQLITE_LIMIT_FUNCTION_ARG = 6 const SQLITE_LIMIT_LENGTH = 0 const SQLITE_LIMIT_LIKE_PATTERN_LENGTH = 8 const SQLITE_LIMIT_SQL_LENGTH = 1 const SQLITE_LIMIT_TRIGGER_DEPTH = 10 const SQLITE_LIMIT_VARIABLE_NUMBER = 9 const SQLITE_LIMIT_VDBE_OP = 5 const SQLITE_LIMIT_WORKER_THREADS = 11 const SQLITE_LITTLEENDIAN = 1 const SQLITE_LOCKED = 6 const SQLITE_LOCKED_SHAREDCACHE = 262 const SQLITE_LOCKED_VTAB = 518 const SQLITE_LOCK_EXCLUSIVE = 4 const SQLITE_LOCK_NONE = 0 const SQLITE_LOCK_PENDING = 3 const SQLITE_LOCK_RESERVED = 2 const SQLITE_LOCK_SHARED = 1 const SQLITE_LegacyAlter = 67108864 const SQLITE_LegacyFileFmt = 2 const SQLITE_LoadExtFunc = 131072 const SQLITE_LoadExtension = 65536 const SQLITE_MALLOC_SOFT_LIMIT = 1024 const SQLITE_MATCH = 0 const SQLITE_MAX_ALLOCATION_SIZE = 2147483391 const SQLITE_MAX_ATTACHED = 10 const SQLITE_MAX_COLUMN = 2000 const SQLITE_MAX_COMPOUND_SELECT = 500 const SQLITE_MAX_DB = 12 const SQLITE_MAX_DEFAULT_PAGE_SIZE = 8192 const SQLITE_MAX_EXPR_DEPTH = 1000 const SQLITE_MAX_FILE_FORMAT = 4 const SQLITE_MAX_FUNCTION_ARG = 127 const SQLITE_MAX_LENGTH = 1000000000 const SQLITE_MAX_LIKE_PATTERN_LENGTH = 50000 const SQLITE_MAX_MEMORY = 0 const SQLITE_MAX_MMAP_SIZE = 2147418112 const SQLITE_MAX_PAGE_COUNT = 4294967294 const SQLITE_MAX_PAGE_SIZE = 65536 const SQLITE_MAX_PATHLEN = 4096 const SQLITE_MAX_PMASZ = 536870912 const SQLITE_MAX_PREPARE_RETRY = 25 const SQLITE_MAX_SCHEMA_RETRY = 50 const SQLITE_MAX_SQL_LENGTH = 1000000000 const SQLITE_MAX_SRCLIST = 200 const SQLITE_MAX_SYMLINK = 200 const SQLITE_MAX_SYMLINKS = 100 const SQLITE_MAX_TRIGGER_DEPTH = 1000 const SQLITE_MAX_VARIABLE_NUMBER = 32766 const SQLITE_MAX_VDBE_OP = 250000000 const SQLITE_MAX_WORKER_THREADS = 8 const SQLITE_MEMDB_DEFAULT_MAXSIZE = 1073741824 const SQLITE_MINIMUM_FILE_DESCRIPTOR = 3 const SQLITE_MISMATCH = 20 const SQLITE_MISUSE = 21 const SQLITE_MISUSE_BKPT = 0 const SQLITE_MUTEX_FAST = 0 const SQLITE_MUTEX_NOOP = 1 const SQLITE_MUTEX_RECURSIVE = 1 const SQLITE_MUTEX_STATIC_APP1 = 8 const SQLITE_MUTEX_STATIC_APP2 = 9 const SQLITE_MUTEX_STATIC_APP3 = 10 const SQLITE_MUTEX_STATIC_LRU = 6 const SQLITE_MUTEX_STATIC_LRU2 = 7 const SQLITE_MUTEX_STATIC_MAIN = 2 const SQLITE_MUTEX_STATIC_MASTER = 2 const SQLITE_MUTEX_STATIC_MEM = 3 const SQLITE_MUTEX_STATIC_MEM2 = 4 const SQLITE_MUTEX_STATIC_OPEN = 4 const SQLITE_MUTEX_STATIC_PMEM = 7 const SQLITE_MUTEX_STATIC_PRNG = 5 const SQLITE_MUTEX_STATIC_TEMPDIR = 11 const SQLITE_MUTEX_STATIC_VFS1 = 11 const SQLITE_MUTEX_STATIC_VFS2 = 12 const SQLITE_MUTEX_STATIC_VFS3 = 13 const SQLITE_MX_JUMP_OPCODE = 64 const SQLITE_MinMaxOpt = 65536 const SQLITE_NOLFS = 22 const SQLITE_NOMATCH = 1 const SQLITE_NOMEM = 7 const SQLITE_NOMEM_BKPT = 7 const SQLITE_NOTADB = 26 const SQLITE_NOTFOUND = 12 const SQLITE_NOTICE = 27 const SQLITE_NOTICE_RBU = 795 const SQLITE_NOTICE_RECOVER_ROLLBACK = 539 const SQLITE_NOTICE_RECOVER_WAL = 283 const SQLITE_NOTNULL = 144 const SQLITE_NOWILDCARDMATCH = 2 const SQLITE_NTUNE = 6 const SQLITE_NULL = 5 const SQLITE_NULLEQ = 128 const SQLITE_N_BTREE_META = 16 const SQLITE_N_KEYWORD = 147 const SQLITE_N_LIMIT = 12 const SQLITE_N_STDTYPE = 6 const SQLITE_NoCkptOnClose = 2048 const SQLITE_NoSchemaError = 134217728 const SQLITE_NullCallback = 256 const SQLITE_NullUnusedCols = 67108864 const SQLITE_OK = 0 const SQLITE_OK_LOAD_PERMANENTLY = 256 const SQLITE_OK_SYMLINK = 512 const SQLITE_OPEN_AUTOPROXY = 32 const SQLITE_OPEN_CREATE = 4 const SQLITE_OPEN_DELETEONCLOSE = 8 const SQLITE_OPEN_EXCLUSIVE = 16 const SQLITE_OPEN_EXRESCODE = 33554432 const SQLITE_OPEN_FULLMUTEX = 65536 const SQLITE_OPEN_MAIN_DB = 256 const SQLITE_OPEN_MAIN_JOURNAL = 2048 const SQLITE_OPEN_MASTER_JOURNAL = 16384 const SQLITE_OPEN_MEMORY = 128 const SQLITE_OPEN_NOFOLLOW = 16777216 const SQLITE_OPEN_NOMUTEX = 32768 const SQLITE_OPEN_PRIVATECACHE = 262144 const SQLITE_OPEN_READONLY = 1 const SQLITE_OPEN_READWRITE = 2 const SQLITE_OPEN_SHAREDCACHE = 131072 const SQLITE_OPEN_SUBJOURNAL = 8192 const SQLITE_OPEN_SUPER_JOURNAL = 16384 const SQLITE_OPEN_TEMP_DB = 512 const SQLITE_OPEN_TEMP_JOURNAL = 4096 const SQLITE_OPEN_TRANSIENT_DB = 1024 const SQLITE_OPEN_URI = 64 const SQLITE_OPEN_WAL = 524288 const SQLITE_OS_UNIX = 1 const SQLITE_OmitNoopJoin = 256 const SQLITE_OmitOrderBy = 262144 const SQLITE_OnePass = 134217728 const SQLITE_OrderByIdxJoin = 64 const SQLITE_PERM = 3 const SQLITE_POWERSAFE_OVERWRITE = 1 const SQLITE_PRAGMA = 19 const SQLITE_PREPARE_MASK = 15 const SQLITE_PREPARE_NORMALIZE = 2 const SQLITE_PREPARE_NO_VTAB = 4 const SQLITE_PREPARE_PERSISTENT = 1 const SQLITE_PREPARE_SAVESQL = 128 const SQLITE_PRINTF_INTERNAL = 1 const SQLITE_PRINTF_MALLOCED = 4 const SQLITE_PRINTF_SQLFUNC = 2 const SQLITE_PRINT_BUF_SIZE = 70 const SQLITE_PRIVATE = 0 const SQLITE_PROTOCOL = 15 const SQLITE_PTRSIZE = 4 const SQLITE_PropagateConst = 32768 const SQLITE_PushDown = 4096 const SQLITE_QUERY_PLANNER_LIMIT = 20000 const SQLITE_QUERY_PLANNER_LIMIT_INCR = 1000 const SQLITE_QueryFlattener = 1 const SQLITE_QueryOnly = 1048576 const SQLITE_RANGE = 25 const SQLITE_RBU_STATE_CHECKPOINT = 3 const SQLITE_RBU_STATE_DONE = 4 const SQLITE_RBU_STATE_ERROR = 5 const SQLITE_RBU_STATE_MOVE = 2 const SQLITE_RBU_STATE_OAL = 1 const SQLITE_RBU_UPDATE_CACHESIZE = 16 const SQLITE_READ = 20 const SQLITE_READONLY = 8 const SQLITE_READONLY_CANTINIT = 1288 const SQLITE_READONLY_CANTLOCK = 520 const SQLITE_READONLY_DBMOVED = 1032 const SQLITE_READONLY_DIRECTORY = 1544 const SQLITE_READONLY_RECOVERY = 264 const SQLITE_READONLY_ROLLBACK = 776 const SQLITE_RECURSIVE = 33 const SQLITE_REINDEX = 27 const SQLITE_REPLACE = 5 const SQLITE_RESULT_SUBTYPE = 16777216 const SQLITE_ROLLBACK = 1 const SQLITE_ROW = 100 const SQLITE_RecTriggers = 8192 const SQLITE_ReleaseReg = 4194304 const SQLITE_ResetDatabase = 33554432 const SQLITE_ReverseOrder = 4096 const SQLITE_SAVEPOINT = 32 const SQLITE_SCANSTAT_COMPLEX = 1 const SQLITE_SCANSTAT_EST = 2 const SQLITE_SCANSTAT_EXPLAIN = 4 const SQLITE_SCANSTAT_NAME = 3 const SQLITE_SCANSTAT_NCYCLE = 7 const SQLITE_SCANSTAT_NLOOP = 0 const SQLITE_SCANSTAT_NVISIT = 1 const SQLITE_SCANSTAT_PARENTID = 6 const SQLITE_SCANSTAT_SELECTID = 5 const SQLITE_SCHEMA = 17 const SQLITE_SELECT = 21 const SQLITE_SERIALIZE_NOCOPY = 1 const SQLITE_SESSION_CONFIG_STRMSIZE = 1 const SQLITE_SESSION_OBJCONFIG_ROWID = 2 const SQLITE_SESSION_OBJCONFIG_SIZE = 1 const SQLITE_SET_LOCKPROXYFILE = 3 const SQLITE_SHM_EXCLUSIVE = 8 const SQLITE_SHM_LOCK = 2 const SQLITE_SHM_NLOCK = 8 const SQLITE_SHM_SHARED = 4 const SQLITE_SHM_UNLOCK = 1 const SQLITE_SORTER_PMASZ = 250 const SQLITE_SOUNDEX = 1 const SQLITE_SOURCE_ID = "2024-04-15 13:34:05 8653b758870e6ef0c98d46b3ace27849054af85da891eb121e9aaa537f1e8355" const SQLITE_SO_ASC = 0 const SQLITE_SO_DESC = 1 const SQLITE_SO_UNDEFINED = -1 const SQLITE_STAT4_SAMPLES = 24 const SQLITE_STATE_BUSY = 109 const SQLITE_STATE_CLOSED = 206 const SQLITE_STATE_ERROR = 213 const SQLITE_STATE_OPEN = 118 const SQLITE_STATE_SICK = 186 const SQLITE_STATE_ZOMBIE = 167 const SQLITE_STATUS_MALLOC_COUNT = 9 const SQLITE_STATUS_MALLOC_SIZE = 5 const SQLITE_STATUS_MEMORY_USED = 0 const SQLITE_STATUS_PAGECACHE_OVERFLOW = 2 const SQLITE_STATUS_PAGECACHE_SIZE = 7 const SQLITE_STATUS_PAGECACHE_USED = 1 const SQLITE_STATUS_PARSER_STACK = 6 const SQLITE_STATUS_SCRATCH_OVERFLOW = 4 const SQLITE_STATUS_SCRATCH_SIZE = 8 const SQLITE_STATUS_SCRATCH_USED = 3 const SQLITE_STDCALL = 0 const SQLITE_STMTJRNL_SPILL = 65536 const SQLITE_STMTSTATUS_AUTOINDEX = 3 const SQLITE_STMTSTATUS_FILTER_HIT = 8 const SQLITE_STMTSTATUS_FILTER_MISS = 7 const SQLITE_STMTSTATUS_FULLSCAN_STEP = 1 const SQLITE_STMTSTATUS_MEMUSED = 99 const SQLITE_STMTSTATUS_REPREPARE = 5 const SQLITE_STMTSTATUS_RUN = 6 const SQLITE_STMTSTATUS_SORT = 2 const SQLITE_STMTSTATUS_VM_STEP = 4 const SQLITE_SUBTYPE = 1048576 const SQLITE_SYNC_DATAONLY = 16 const SQLITE_SYNC_FULL = 3 const SQLITE_SYNC_NORMAL = 2 const SQLITE_SYSTEM_MALLOC = 1 const SQLITE_SeekScan = 131072 const SQLITE_ShortColNames = 64 const SQLITE_SimplifyJoin = 8192 const SQLITE_SkipScan = 16384 const SQLITE_Stat4 = 2048 const SQLITE_StmtScanStatus = 1024 const SQLITE_TEMP_FILE_PREFIX = "etilqs_" const SQLITE_TEMP_STORE = 1 const SQLITE_TESTCTRL_ALWAYS = 13 const SQLITE_TESTCTRL_ASSERT = 12 const SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS = 10 const SQLITE_TESTCTRL_BITVEC_TEST = 8 const SQLITE_TESTCTRL_BYTEORDER = 22 const SQLITE_TESTCTRL_EXPLAIN_STMT = 19 const SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS = 29 const SQLITE_TESTCTRL_FAULT_INSTALL = 9 const SQLITE_TESTCTRL_FIRST = 5 const SQLITE_TESTCTRL_FK_NO_ACTION = 7 const SQLITE_TESTCTRL_IMPOSTER = 25 const SQLITE_TESTCTRL_INTERNAL_FUNCTIONS = 17 const SQLITE_TESTCTRL_ISINIT = 23 const SQLITE_TESTCTRL_ISKEYWORD = 16 const SQLITE_TESTCTRL_JSON_SELFCHECK = 14 const SQLITE_TESTCTRL_LAST = 34 const SQLITE_TESTCTRL_LOCALTIME_FAULT = 18 const SQLITE_TESTCTRL_LOGEST = 33 const SQLITE_TESTCTRL_NEVER_CORRUPT = 20 const SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD = 19 const SQLITE_TESTCTRL_OPTIMIZATIONS = 15 const SQLITE_TESTCTRL_PARSER_COVERAGE = 26 const SQLITE_TESTCTRL_PENDING_BYTE = 11 const SQLITE_TESTCTRL_PRNG_RESET = 7 const SQLITE_TESTCTRL_PRNG_RESTORE = 6 const SQLITE_TESTCTRL_PRNG_SAVE = 5 const SQLITE_TESTCTRL_PRNG_SEED = 28 const SQLITE_TESTCTRL_RESERVE = 14 const SQLITE_TESTCTRL_RESULT_INTREAL = 27 const SQLITE_TESTCTRL_SCRATCHMALLOC = 17 const SQLITE_TESTCTRL_SEEK_COUNT = 30 const SQLITE_TESTCTRL_SORTER_MMAP = 24 const SQLITE_TESTCTRL_TRACEFLAGS = 31 const SQLITE_TESTCTRL_TUNE = 32 const SQLITE_TESTCTRL_USELONGDOUBLE = 34 const SQLITE_TESTCTRL_VDBE_COVERAGE = 21 const SQLITE_TEXT = 3 const SQLITE_THREADSAFE = 1 const SQLITE_TOKEN_KEYWORD = 2 const SQLITE_TOKEN_QUOTED = 1 const SQLITE_TOOBIG = 18 const SQLITE_TRACE_CLOSE = 8 const SQLITE_TRACE_LEGACY = 64 const SQLITE_TRACE_NONLEGACY_MASK = 15 const SQLITE_TRACE_PROFILE = 2 const SQLITE_TRACE_ROW = 4 const SQLITE_TRACE_STMT = 1 const SQLITE_TRACE_XPROFILE = 128 const SQLITE_TRANSACTION = 22 const SQLITE_TXN_NONE = 0 const SQLITE_TXN_READ = 1 const SQLITE_TXN_WRITE = 2 const SQLITE_Transitive = 128 const SQLITE_TriggerEQP = 16777216 const SQLITE_TrustedSchema = 128 const SQLITE_UPDATE = 23 const SQLITE_USE_URI = 0 const SQLITE_UTF16 = 4 const SQLITE_UTF16BE = 3 const SQLITE_UTF16LE = 2 const SQLITE_UTF16NATIVE = 2 const SQLITE_UTF16_ALIGNED = 8 const SQLITE_UTF8 = 1 const SQLITE_VERSION = "3.45.3" const SQLITE_VERSION_NUMBER = 3045003 const SQLITE_VTABRISK_High = 2 const SQLITE_VTABRISK_Low = 0 const SQLITE_VTABRISK_Normal = 1 const SQLITE_VTAB_CONSTRAINT_SUPPORT = 1 const SQLITE_VTAB_DIRECTONLY = 3 const SQLITE_VTAB_INNOCUOUS = 2 const SQLITE_VTAB_USES_ALL_SCHEMAS = 4 const SQLITE_WARNING = 28 const SQLITE_WARNING_AUTOINDEX = 284 const SQLITE_WIN32_DATA_DIRECTORY_TYPE = 1 const SQLITE_WIN32_TEMP_DIRECTORY_TYPE = 2 const SQLITE_WITHOUT_ZONEMALLOC = 1 const SQLITE_WindowFunc = 2 const SQLITE_WriteSchema = 1 const SRT_Coroutine = 13 const SRT_Discard = 4 const SRT_DistFifo = 5 const SRT_DistQueue = 6 const SRT_EphemTab = 12 const SRT_Except = 2 const SRT_Exists = 3 const SRT_Fifo = 8 const SRT_Mem = 10 const SRT_Output = 9 const SRT_Queue = 7 const SRT_Set = 11 const SRT_Table = 14 const SRT_Union = 1 const SRT_Upfrom = 15 const STATX_ALL = 4095 const STATX_ATIME = 32 const STATX_ATTR_APPEND = 32 const STATX_ATTR_AUTOMOUNT = 4096 const STATX_ATTR_COMPRESSED = 4 const STATX_ATTR_DAX = 2097152 const STATX_ATTR_ENCRYPTED = 2048 const STATX_ATTR_IMMUTABLE = 16 const STATX_ATTR_MOUNT_ROOT = 8192 const STATX_ATTR_NODUMP = 64 const STATX_ATTR_VERITY = 1048576 const STATX_BASIC_STATS = 2047 const STATX_BLOCKS = 1024 const STATX_BTIME = 2048 const STATX_CTIME = 128 const STATX_GID = 16 const STATX_INO = 256 const STATX_MNT_ID = 4096 const STATX_MODE = 2 const STATX_MTIME = 64 const STATX_NLINK = 4 const STATX_SIZE = 512 const STATX_TYPE = 1 const STATX_UID = 8 const STATX__RESERVED = 2147483648 const STAT_GET_NDLT = 4 const STAT_GET_NEQ = 2 const STAT_GET_NLT = 3 const STAT_GET_ROWID = 1 const STAT_GET_STAT1 = 0 const STA_CLK = 32768 const STA_CLOCKERR = 4096 const STA_DEL = 32 const STA_FLL = 8 const STA_FREQHOLD = 128 const STA_INS = 16 const STA_MODE = 16384 const STA_NANO = 8192 const STA_PLL = 1 const STA_PPSERROR = 2048 const STA_PPSFREQ = 2 const STA_PPSJITTER = 512 const STA_PPSSIGNAL = 256 const STA_PPSTIME = 4 const STA_PPSWANDER = 1024 const STA_RONLY = 65280 const STA_UNSYNC = 64 const STDERR_FILENO = 2 const STDIN_FILENO = 0 const STDOUT_FILENO = 1 const SYNC_FILE_RANGE_WAIT_AFTER = 4 const SYNC_FILE_RANGE_WAIT_BEFORE = 1 const SYNC_FILE_RANGE_WRITE = 2 const SYNC_FILE_RANGE_WRITE_AND_WAIT = 7 const S_BLKSIZE = 512 const S_IEXEC = 64 const S_IFBLK = 24576 const S_IFCHR = 8192 const S_IFDIR = 16384 const S_IFIFO = 4096 const S_IFLNK = 40960 const S_IFMT = 61440 const S_IFREG = 32768 const S_IFSOCK = 49152 const S_IREAD = 256 const S_IRGRP = 32 const S_IROTH = 4 const S_IRUSR = 256 const S_IRWXG = 56 const S_IRWXO = 7 const S_IRWXU = 448 const S_ISGID = 1024 const S_ISUID = 2048 const S_ISVTX = 512 const S_IWGRP = 16 const S_IWOTH = 2 const S_IWRITE = 128 const S_IWUSR = 128 const S_IXGRP = 8 const S_IXOTH = 1 const S_IXUSR = 64 const TABTYP_NORM = 0 const TABTYP_VIEW = 2 const TABTYP_VTAB = 1 const TCFLSH = 21515 const TCGETA = 21509 const TCGETS = 21505 const TCGETS2 = 2147505194 const TCGETX = 21554 const TCSBRK = 21513 const TCSBRKP = 21541 const TCSETA = 21510 const TCSETAF = 21512 const TCSETAW = 21511 const TCSETS = 21506 const TCSETS2 = 1073763371 const TCSETSF = 21508 const TCSETSF2 = 1073763373 const TCSETSW = 21507 const TCSETSW2 = 1073763372 const TCSETX = 21555 const TCSETXF = 21556 const TCSETXW = 21557 const TCXONC = 21514 const TERM_ANDINFO = 32 const TERM_CODED = 4 const TERM_COPIED = 8 const TERM_DYNAMIC = 1 const TERM_HEURTRUTH = 8192 const TERM_HIGHTRUTH = 16384 const TERM_IS = 2048 const TERM_LIKE = 1024 const TERM_LIKECOND = 512 const TERM_LIKEOPT = 256 const TERM_OK = 64 const TERM_ORINFO = 16 const TERM_SLICE = 32768 const TERM_VARSELECT = 4096 const TERM_VIRTUAL = 2 const TERM_VNULL = 128 const TF_Autoincrement = 8 const TF_Ephemeral = 16384 const TF_Eponymous = 32768 const TF_HasGenerated = 96 const TF_HasHidden = 2 const TF_HasNotNull = 2048 const TF_HasPrimaryKey = 4 const TF_HasStat1 = 16 const TF_HasStat4 = 8192 const TF_HasStored = 64 const TF_HasVirtual = 32 const TF_NoVisibleRowid = 512 const TF_OOOHidden = 1024 const TF_Readonly = 1 const TF_Shadow = 4096 const TF_StatsUsed = 256 const TF_Strict = 65536 const TF_WithoutRowid = 128 const TIMER_ABSTIME = 1 const TIME_UTC = 1 const TIOCCBRK = 21544 const TIOCCONS = 21533 const TIOCEXCL = 21516 const TIOCGDEV = 2147505202 const TIOCGETD = 21540 const TIOCGEXCL = 2147505216 const TIOCGICOUNT = 21597 const TIOCGISO7816 = 2147505218 const TIOCGLCKTRMIOS = 21590 const TIOCGPGRP = 21519 const TIOCGPKT = 2147505208 const TIOCGPTLCK = 2147505209 const TIOCGPTN = 2147505200 const TIOCGPTPEER = 21569 const TIOCGRS485 = 21550 const TIOCGSERIAL = 21534 const TIOCGSID = 21545 const TIOCGSOFTCAR = 21529 const TIOCGWINSZ = 21523 const TIOCINQ = 21531 const TIOCLINUX = 21532 const TIOCMBIC = 21527 const TIOCMBIS = 21526 const TIOCMGET = 21525 const TIOCMIWAIT = 21596 const TIOCMSET = 21528 const TIOCM_CAR = 64 const TIOCM_CD = 64 const TIOCM_CTS = 32 const TIOCM_DSR = 256 const TIOCM_DTR = 2 const TIOCM_LE = 1 const TIOCM_RI = 128 const TIOCM_RNG = 128 const TIOCM_RTS = 4 const TIOCM_SR = 16 const TIOCM_ST = 8 const TIOCNOTTY = 21538 const TIOCNXCL = 21517 const TIOCOUTQ = 21521 const TIOCPKT = 21536 const TIOCPKT_DATA = 0 const TIOCPKT_DOSTOP = 32 const TIOCPKT_FLUSHREAD = 1 const TIOCPKT_FLUSHWRITE = 2 const TIOCPKT_IOCTL = 64 const TIOCPKT_NOSTOP = 16 const TIOCPKT_START = 8 const TIOCPKT_STOP = 4 const TIOCSBRK = 21543 const TIOCSCTTY = 21518 const TIOCSERCONFIG = 21587 const TIOCSERGETLSR = 21593 const TIOCSERGETMULTI = 21594 const TIOCSERGSTRUCT = 21592 const TIOCSERGWILD = 21588 const TIOCSERSETMULTI = 21595 const TIOCSERSWILD = 21589 const TIOCSER_TEMT = 1 const TIOCSETD = 21539 const TIOCSIG = 1073763382 const TIOCSISO7816 = 3221247043 const TIOCSLCKTRMIOS = 21591 const TIOCSPGRP = 21520 const TIOCSPTLCK = 1073763377 const TIOCSRS485 = 21551 const TIOCSSERIAL = 21535 const TIOCSSOFTCAR = 21530 const TIOCSTI = 21522 const TIOCSWINSZ = 21524 const TIOCVHANGUP = 21559 const TK_ABORT = 27 const TK_ACTION = 28 const TK_ADD = 163 const TK_AFTER = 29 const TK_AGG_COLUMN = 169 const TK_AGG_FUNCTION = 168 const TK_ALL = 135 const TK_ALTER = 162 const TK_ALWAYS = 96 const TK_ANALYZE = 30 const TK_AND = 44 const TK_ANY = 101 const TK_AS = 24 const TK_ASC = 31 const TK_ASTERISK = 180 const TK_ATTACH = 32 const TK_AUTOINCR = 126 const TK_BEFORE = 33 const TK_BEGIN = 5 const TK_BETWEEN = 48 const TK_BITAND = 102 const TK_BITNOT = 114 const TK_BITOR = 103 const TK_BLOB = 154 const TK_BY = 34 const TK_CASCADE = 35 const TK_CASE = 157 const TK_CAST = 36 const TK_CHECK = 124 const TK_COLLATE = 113 const TK_COLUMN = 167 const TK_COLUMNKW = 60 const TK_COMMA = 25 const TK_COMMIT = 10 const TK_CONCAT = 111 const TK_CONFLICT = 37 const TK_CONSTRAINT = 119 const TK_CREATE = 17 const TK_CTIME_KW = 100 const TK_CURRENT = 85 const TK_DATABASE = 38 const TK_DEFAULT = 120 const TK_DEFERRABLE = 131 const TK_DEFERRED = 7 const TK_DELETE = 128 const TK_DESC = 39 const TK_DETACH = 40 const TK_DISTINCT = 140 const TK_DO = 61 const TK_DOT = 141 const TK_DROP = 133 const TK_EACH = 41 const TK_ELSE = 160 const TK_END = 11 const TK_EQ = 53 const TK_ERROR = 182 const TK_ESCAPE = 58 const TK_EXCEPT = 136 const TK_EXCLUDE = 91 const TK_EXCLUSIVE = 9 const TK_EXISTS = 20 const TK_EXPLAIN = 2 const TK_FAIL = 42 const TK_FILTER = 166 const TK_FIRST = 83 const TK_FLOAT = 153 const TK_FOLLOWING = 86 const TK_FOR = 62 const TK_FOREIGN = 132 const TK_FROM = 142 const TK_FUNCTION = 172 const TK_GE = 57 const TK_GENERATED = 95 const TK_GROUP = 146 const TK_GROUPS = 92 const TK_GT = 54 const TK_HAVING = 147 const TK_ID = 59 const TK_IF = 18 const TK_IF_NULL_ROW = 179 const TK_IGNORE = 63 const TK_ILLEGAL = 184 const TK_IMMEDIATE = 8 const TK_IN = 49 const TK_INDEX = 161 const TK_INDEXED = 116 const TK_INITIALLY = 64 const TK_INSERT = 127 const TK_INSTEAD = 65 const TK_INTEGER = 155 const TK_INTERSECT = 137 const TK_INTO = 151 const TK_IS = 45 const TK_ISNOT = 171 const TK_ISNULL = 50 const TK_JOIN = 143 const TK_JOIN_KW = 118 const TK_KEY = 67 const TK_LAST = 84 const TK_LE = 55 const TK_LIKE_KW = 47 const TK_LIMIT = 148 const TK_LP = 22 const TK_LSHIFT = 104 const TK_LT = 56 const TK_MATCH = 46 const TK_MATERIALIZED = 97 const TK_MINUS = 107 const TK_NE = 52 const TK_NO = 66 const TK_NOT = 19 const TK_NOTHING = 152 const TK_NOTNULL = 51 const TK_NULL = 121 const TK_NULLS = 82 const TK_OF = 68 const TK_OFFSET = 69 const TK_ON = 115 const TK_OR = 43 const TK_ORDER = 145 const TK_OTHERS = 93 const TK_OVER = 165 const TK_PARTITION = 87 const TK_PLAN = 4 const TK_PLUS = 106 const TK_PRAGMA = 70 const TK_PRECEDING = 88 const TK_PRIMARY = 122 const TK_PTR = 112 const TK_QUERY = 3 const TK_RAISE = 71 const TK_RANGE = 89 const TK_RECURSIVE = 72 const TK_REFERENCES = 125 const TK_REGISTER = 176 const TK_REINDEX = 98 const TK_RELEASE = 14 const TK_REM = 110 const TK_RENAME = 99 const TK_REPLACE = 73 const TK_RESTRICT = 74 const TK_RETURNING = 150 const TK_ROLLBACK = 12 const TK_ROW = 75 const TK_ROWS = 76 const TK_RP = 23 const TK_RSHIFT = 105 const TK_SAVEPOINT = 13 const TK_SELECT = 138 const TK_SELECT_COLUMN = 178 const TK_SEMI = 1 const TK_SET = 130 const TK_SLASH = 109 const TK_SPACE = 183 const TK_SPAN = 181 const TK_STAR = 108 const TK_STRING = 117 const TK_TABLE = 16 const TK_TEMP = 21 const TK_THEN = 159 const TK_TIES = 94 const TK_TO = 15 const TK_TRANSACTION = 6 const TK_TRIGGER = 77 const TK_TRUEFALSE = 170 const TK_TRUTH = 175 const TK_UMINUS = 173 const TK_UNBOUNDED = 90 const TK_UNION = 134 const TK_UNIQUE = 123 const TK_UPDATE = 129 const TK_UPLUS = 174 const TK_USING = 144 const TK_VACUUM = 78 const TK_VALUES = 139 const TK_VARIABLE = 156 const TK_VECTOR = 177 const TK_VIEW = 79 const TK_VIRTUAL = 80 const TK_WHEN = 158 const TK_WHERE = 149 const TK_WINDOW = 164 const TK_WITH = 81 const TK_WITHOUT = 26 const TMP_MAX = 238328 const TOKEN = 0 const TRANS_NONE = 0 const TRANS_READ = 1 const TRANS_WRITE = 2 const TREETRACE_ENABLED = 0 const TRIGGER_AFTER = 2 const TRIGGER_BEFORE = 1 const TTYDEF_CFLAG = 0 const TTYDEF_IFLAG = 0 const TTYDEF_LFLAG = 0 const TTYDEF_OFLAG = 0 const TTYDEF_SPEED = 0 const UNIXFILE_DELETE = 32 const UNIXFILE_DIRSYNC = 8 const UNIXFILE_EXCL = 1 const UNIXFILE_NOLOCK = 128 const UNIXFILE_PERSIST_WAL = 4 const UNIXFILE_PSOW = 16 const UNIXFILE_RDONLY = 2 const UNIXFILE_URI = 64 const UNIX_SHM_BASE = 120 const UNIX_SHM_DMS = 128 const UNKNOWN_LOCK = 5 const USE_PREAD = 1 const UTIME_NOW = 1073741823 const UTIME_OMIT = 1073741822 const UpperToLower = 0 const VDBE_DISPLAY_P4 = 1 const VDBE_HALT_STATE = 3 const VDBE_INIT_STATE = 0 const VDBE_READY_STATE = 1 const VDBE_RUN_STATE = 2 const ViewCanHaveRowid = 0 const WALINDEX_HDR_SIZE = 0 const WALINDEX_MAX_VERSION = 3007000 const WALINDEX_PGSZ = 0 const WAL_ALL_BUT_WRITE = 1 const WAL_CKPT_LOCK = 1 const WAL_EXCLUSIVE_MODE = 1 const WAL_FRAME_HDRSIZE = 24 const WAL_HDRSIZE = 32 const WAL_HEAPMEMORY_MODE = 2 const WAL_LOCK_CKPT = 1 const WAL_LOCK_READ0 = 3 const WAL_LOCK_WRITE = 0 const WAL_MAGIC = 931071618 const WAL_MAX_VERSION = 3007000 const WAL_NORMAL_MODE = 0 const WAL_NREADER = 5 const WAL_RDONLY = 1 const WAL_RDWR = 0 const WAL_RECOVER_LOCK = 2 const WAL_RETRY = -1 const WAL_RETRY_BLOCKED_MASK = 0 const WAL_RETRY_PROTOCOL_LIMIT = 100 const WAL_SAVEPOINT_NDATA = 4 const WAL_SHM_RDONLY = 2 const WAL_WRITE_LOCK = 0 const WCONTINUED = 8 const WEXITED = 4 const WHERE_AGG_DISTINCT = 1024 const WHERE_AUTO_INDEX = 16384 const WHERE_BIGNULL_SORT = 524288 const WHERE_BLOOMFILTER = 4194304 const WHERE_BOTH_LIMIT = 48 const WHERE_BTM_LIMIT = 32 const WHERE_COLUMN_EQ = 1 const WHERE_COLUMN_IN = 4 const WHERE_COLUMN_NULL = 8 const WHERE_COLUMN_RANGE = 2 const WHERE_CONSTRAINT = 15 const WHERE_DISTINCTBY = 128 const WHERE_DISTINCT_NOOP = 0 const WHERE_DISTINCT_ORDERED = 2 const WHERE_DISTINCT_UNIQUE = 1 const WHERE_DISTINCT_UNORDERED = 3 const WHERE_DUPLICATES_OK = 16 const WHERE_EXPRIDX = 67108864 const WHERE_GROUPBY = 64 const WHERE_IDX_ONLY = 64 const WHERE_INDEXED = 512 const WHERE_IN_ABLE = 2048 const WHERE_IN_EARLYOUT = 262144 const WHERE_IN_SEEKSCAN = 1048576 const WHERE_IPK = 256 const WHERE_MULTI_OR = 8192 const WHERE_OMIT_OFFSET = 16777216 const WHERE_ONEPASS_DESIRED = 4 const WHERE_ONEPASS_MULTIROW = 8 const WHERE_ONEROW = 4096 const WHERE_ORDERBY_LIMIT = 2048 const WHERE_ORDERBY_MAX = 2 const WHERE_ORDERBY_MIN = 1 const WHERE_ORDERBY_NORMAL = 0 const WHERE_OR_SUBCLAUSE = 32 const WHERE_PARTIALIDX = 131072 const WHERE_RIGHT_JOIN = 4096 const WHERE_SELFCULL = 8388608 const WHERE_SKIPSCAN = 32768 const WHERE_SORTBYGROUP = 512 const WHERE_TOP_LIMIT = 16 const WHERE_TRANSCONS = 2097152 const WHERE_UNQ_WANTED = 65536 const WHERE_USE_LIMIT = 16384 const WHERE_VIRTUALTABLE = 1024 const WHERE_WANT_DISTINCT = 256 const WINDOW_AGGINVERSE = 2 const WINDOW_AGGSTEP = 3 const WINDOW_ENDING_INT = 1 const WINDOW_ENDING_NUM = 4 const WINDOW_NTH_VALUE_INT = 2 const WINDOW_RETURN_ROW = 1 const WINDOW_STARTING_INT = 0 const WINDOW_STARTING_NUM = 3 const WNOHANG = 1 const WNOWAIT = 16777216 const WO_ALL = 16383 const WO_AND = 1024 const WO_AUX = 64 const WO_EQ = 2 const WO_EQUIV = 2048 const WO_GE = 32 const WO_GT = 4 const WO_IN = 1 const WO_IS = 128 const WO_ISNULL = 256 const WO_LE = 8 const WO_LT = 16 const WO_NOOP = 4096 const WO_OR = 512 const WO_ROWVAL = 8192 const WO_SINGLE = 511 const WRC_Abort = 2 const WRC_Continue = 0 const WRC_Prune = 1 const WRITE_LOCK = 2 const WSTOPPED = 2 const WUNTRACED = 2 const W_OK = 2 const XN_EXPR = -2 const XN_ROWID = -1 const X_OK = 1 const YYFALLBACK = 1 const YYMALLOCARGTYPE = 0 const YYNOCODE = 319 const YYNOERRORRECOVERY = 1 const YYNRULE = 405 const YYNRULE_WITH_ACTION = 340 const YYNSTATE = 579 const YYNTOKEN = 185 const YYPARSEFREENEVERNULL = 1 const YYSTACKDEPTH = 100 const YYWILDCARD = 101 const YY_ACCEPT_ACTION = 1244 const YY_ACTTAB_COUNT = 2100 const YY_ERROR_ACTION = 1243 const YY_MAX_REDUCE = 1650 const YY_MAX_SHIFT = 578 const YY_MAX_SHIFTREDUCE = 1242 const YY_MIN_REDUCE = 1246 const YY_MIN_SHIFTREDUCE = 838 const YY_NO_ACTION = 1245 const YY_REDUCE_COUNT = 410 const YY_REDUCE_MAX = 1753 const YY_REDUCE_MIN = -271 const YY_SHIFT_COUNT = 578 const YY_SHIFT_MAX = 2088 const YY_SHIFT_MIN = 0 const _ALLOCA_H = 1 const _ASSERT_H = 1 const _BITS_BYTESWAP_H = 1 const _BITS_ENDIANNESS_H = 1 const _BITS_ENDIAN_H = 1 const _BITS_ERRNO_H = 1 const _BITS_LIBM_SIMD_DECL_STUBS_H = 1 const _BITS_POSIX_OPT_H = 1 const _BITS_PTHREADTYPES_ARCH_H = 1 const _BITS_PTHREADTYPES_COMMON_H = 1 const _BITS_STAT_H = 1 const _BITS_STDINT_INTN_H = 1 const _BITS_STDIO_LIM_H = 1 const _BITS_STRUCT_STAT_H = 1 const _BITS_TIME64_H = 1 const _BITS_TIMEX_H = 1 const _BITS_TIME_H = 1 const _BITS_TYPESIZES_H = 1 const _BITS_TYPES_H = 1 const _BITS_TYPES_LOCALE_T_H = 1 const _BITS_TYPES___LOCALE_T_H = 1 const _BITS_UINTN_IDENTITY_H = 1 const _CS_POSIX_V5_WIDTH_RESTRICTED_ENVS = 0 const _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS = 0 const _CS_POSIX_V7_WIDTH_RESTRICTED_ENVS = 0 const _DLFCN_H = 1 const _ENDIAN_H = 1 const _ERRNO_H = 1 const _FCNTL_H = 1 const _FEATURES_H = 1 const _FILE_OFFSET_BITS = 64 const _GETOPT_CORE_H = 1 const _GETOPT_POSIX_H = 1 const _IOC_DIRBITS = 2 const _IOC_DIRMASK = 3 const _IOC_DIRSHIFT = 30 const _IOC_NONE = 0 const _IOC_NRBITS = 8 const _IOC_NRMASK = 255 const _IOC_NRSHIFT = 0 const _IOC_READ = 2 const _IOC_SIZEBITS = 14 const _IOC_SIZEMASK = 16383 const _IOC_SIZESHIFT = 16 const _IOC_TYPEBITS = 8 const _IOC_TYPEMASK = 255 const _IOC_TYPESHIFT = 8 const _IOC_WRITE = 1 const _IOFBF = 0 const _IOLBF = 1 const _IONBF = 2 const _IO_EOF_SEEN = 16 const _IO_ERR_SEEN = 32 const _IO_USER_LOCK = 32768 const _LARGE_FILE = 1 const _LFS64_ASYNCHRONOUS_IO = 1 const _LFS64_LARGEFILE = 1 const _LFS64_STDIO = 1 const _LFS_ASYNCHRONOUS_IO = 1 const _LFS_LARGEFILE = 1 const _MATH_H = 1 const _POSIX2_CHAR_TERM = 200809 const _POSIX2_C_BIND = 200809 const _POSIX2_C_DEV = 200809 const _POSIX2_C_VERSION = 200809 const _POSIX2_LOCALEDEF = 200809 const _POSIX2_SW_DEV = 200809 const _POSIX2_VERSION = 200809 const _POSIX_ADVISORY_INFO = 200809 const _POSIX_ASYNCHRONOUS_IO = 200809 const _POSIX_ASYNC_IO = 1 const _POSIX_BARRIERS = 200809 const _POSIX_CHOWN_RESTRICTED = 0 const _POSIX_CLOCK_SELECTION = 200809 const _POSIX_CPUTIME = 0 const _POSIX_FSYNC = 200809 const _POSIX_IPV6 = 200809 const _POSIX_JOB_CONTROL = 1 const _POSIX_MAPPED_FILES = 200809 const _POSIX_MEMLOCK = 200809 const _POSIX_MEMLOCK_RANGE = 200809 const _POSIX_MEMORY_PROTECTION = 200809 const _POSIX_MESSAGE_PASSING = 200809 const _POSIX_MONOTONIC_CLOCK = 0 const _POSIX_NO_TRUNC = 1 const _POSIX_PRIORITIZED_IO = 200809 const _POSIX_PRIORITY_SCHEDULING = 200809 const _POSIX_RAW_SOCKETS = 200809 const _POSIX_READER_WRITER_LOCKS = 200809 const _POSIX_REALTIME_SIGNALS = 200809 const _POSIX_REENTRANT_FUNCTIONS = 1 const _POSIX_REGEXP = 1 const _POSIX_SAVED_IDS = 1 const _POSIX_SEMAPHORES = 200809 const _POSIX_SHARED_MEMORY_OBJECTS = 200809 const _POSIX_SHELL = 1 const _POSIX_SPAWN = 200809 const _POSIX_SPIN_LOCKS = 200809 const _POSIX_SPORADIC_SERVER = -1 const _POSIX_SYNCHRONIZED_IO = 200809 const _POSIX_THREADS = 200809 const _POSIX_THREAD_ATTR_STACKADDR = 200809 const _POSIX_THREAD_ATTR_STACKSIZE = 200809 const _POSIX_THREAD_CPUTIME = 0 const _POSIX_THREAD_PRIORITY_SCHEDULING = 200809 const _POSIX_THREAD_PRIO_INHERIT = 200809 const _POSIX_THREAD_PRIO_PROTECT = 200809 const _POSIX_THREAD_PROCESS_SHARED = 200809 const _POSIX_THREAD_ROBUST_PRIO_INHERIT = 200809 const _POSIX_THREAD_ROBUST_PRIO_PROTECT = -1 const _POSIX_THREAD_SAFE_FUNCTIONS = 200809 const _POSIX_THREAD_SPORADIC_SERVER = -1 const _POSIX_TIMEOUTS = 200809 const _POSIX_TIMERS = 200809 const _POSIX_TRACE = -1 const _POSIX_TRACE_EVENT_FILTER = -1 const _POSIX_TRACE_INHERIT = -1 const _POSIX_TRACE_LOG = -1 const _POSIX_TYPED_MEMORY_OBJECTS = -1 const _POSIX_V6_ILP32_OFF32 = 1 const _POSIX_V6_ILP32_OFFBIG = 1 const _POSIX_V6_LP64_OFF64 = -1 const _POSIX_V6_LPBIG_OFFBIG = -1 const _POSIX_V7_ILP32_OFF32 = 1 const _POSIX_V7_ILP32_OFFBIG = 1 const _POSIX_V7_LP64_OFF64 = -1 const _POSIX_V7_LPBIG_OFFBIG = -1 const _POSIX_VDISABLE = 0 const _POSIX_VERSION = 200809 const _PRINTF_NAN_LEN_MAX = 4 const _SC_PAGE_SIZE = 0 const _SIGSET_NWORDS = 1024 const _STDC_PREDEF_H = 1 const _STDIO_H = 1 const _STDLIB_H = 1 const _STRINGS_H = 1 const _STRING_H = 1 const _STRUCT_TIMESPEC = 1 const _SYS_CDEFS_H = 1 const _SYS_IOCTL_H = 1 const _SYS_MMAN_H = 1 const _SYS_SELECT_H = 1 const _SYS_STAT_H = 1 const _SYS_TIME_H = 1 const _SYS_TYPES_H = 1 const _THREAD_MUTEX_INTERNAL_H = 1 const _THREAD_SHARED_TYPES_H = 1 const _TIME_H = 1 const _UNISTD_H = 1 const _XBS5_ILP32_OFF32 = 1 const _XBS5_ILP32_OFFBIG = 1 const _XBS5_LP64_OFF64 = -1 const _XBS5_LPBIG_OFFBIG = -1 const _XOPEN_ENH_I18N = 1 const _XOPEN_LEGACY = 1 const _XOPEN_REALTIME = 1 const _XOPEN_REALTIME_THREADS = 1 const _XOPEN_SHM = 1 const _XOPEN_UNIX = 1 const _XOPEN_VERSION = 700 const _XOPEN_XCU_VERSION = 4 const _XOPEN_XPG2 = 1 const _XOPEN_XPG3 = 1 const _XOPEN_XPG4 = 1 const __ACCUM_EPSILON__ = 0 const __ACCUM_FBIT__ = 15 const __ACCUM_IBIT__ = 16 const __ACCUM_MAX__ = 0 const __ACCUM_MIN__ = 0 const __APCS_32__ = 1 const __ARMEL__ = 1 const __ARM_32BIT_STATE = 1 const __ARM_ARCH = 6 const __ARM_ARCH_6__ = 1 const __ARM_ARCH_ISA_ARM = 1 const __ARM_ARCH_ISA_THUMB = 1 const __ARM_EABI__ = 1 const __ARM_FEATURE_CLZ = 1 const __ARM_FEATURE_COPROC = 15 const __ARM_FEATURE_DSP = 1 const __ARM_FEATURE_LDREX = 4 const __ARM_FEATURE_QBIT = 1 const __ARM_FEATURE_SAT = 1 const __ARM_FEATURE_SIMD32 = 1 const __ARM_FEATURE_UNALIGNED = 1 const __ARM_FP = 12 const __ARM_PCS_VFP = 1 const __ARM_SIZEOF_MINIMAL_ENUM = 4 const __ARM_SIZEOF_WCHAR_T = 4 const __ASSERT_VOID_CAST = 0 const __ATOMIC_ACQUIRE = 2 const __ATOMIC_ACQ_REL = 4 const __ATOMIC_CONSUME = 1 const __ATOMIC_RELAXED = 0 const __ATOMIC_RELEASE = 3 const __ATOMIC_SEQ_CST = 5 const __BIGGEST_ALIGNMENT__ = 8 const __BIG_ENDIAN = 4321 const __BIT_TYPES_DEFINED__ = 1 const __BLKCNT64_T_TYPE = 0 const __BLKCNT_T_TYPE = "__SLONGWORD_TYPE" const __BLKSIZE_T_TYPE = "__SLONGWORD_TYPE" const __BYTE_ORDER = 1234 const __BYTE_ORDER__ = 1234 const __CCGO__ = 1 const __CHAR_BIT__ = 8 const __CHAR_UNSIGNED__ = 1 const __CLOCKID_T_TYPE = 0 const __CLOCK_T_TYPE = "__SLONGWORD_TYPE" const __CPU_MASK_TYPE = "__ULONGWORD_TYPE" const __DADDR_T_TYPE = 0 const __DA_FBIT__ = 31 const __DA_IBIT__ = 32 const __DBL_DECIMAL_DIG__ = 17 const __DBL_DIG__ = 15 const __DBL_HAS_DENORM__ = 1 const __DBL_HAS_INFINITY__ = 1 const __DBL_HAS_QUIET_NAN__ = 1 const __DBL_IS_IEC_60559__ = 2 const __DBL_MANT_DIG__ = 53 const __DBL_MAX_10_EXP__ = 308 const __DBL_MAX_EXP__ = 1024 const __DBL_MIN_10_EXP__ = -307 const __DBL_MIN_EXP__ = -1021 const __DECIMAL_DIG__ = 17 const __DEC_EVAL_METHOD__ = 2 const __DEV_T_TYPE = 0 const __DQ_FBIT__ = 63 const __DQ_IBIT__ = 0 const __ELF__ = 1 const __FD_SETSIZE = 1024 const __FILE_defined = 1 const __FINITE_MATH_ONLY__ = 0 const __FLOAT_WORD_ORDER = 1234 const __FLOAT_WORD_ORDER__ = 1234 const __FLT32X_DECIMAL_DIG__ = 17 const __FLT32X_DENORM_MIN__ = 0 const __FLT32X_DIG__ = 15 const __FLT32X_EPSILON__ = 0 const __FLT32X_HAS_DENORM__ = 1 const __FLT32X_HAS_INFINITY__ = 1 const __FLT32X_HAS_QUIET_NAN__ = 1 const __FLT32X_IS_IEC_60559__ = 2 const __FLT32X_MANT_DIG__ = 53 const __FLT32X_MAX_10_EXP__ = 308 const __FLT32X_MAX_EXP__ = 1024 const __FLT32X_MAX__ = 0 const __FLT32X_MIN_10_EXP__ = -307 const __FLT32X_MIN_EXP__ = -1021 const __FLT32X_MIN__ = 0 const __FLT32X_NORM_MAX__ = 0 const __FLT32_DECIMAL_DIG__ = 9 const __FLT32_DENORM_MIN__ = 0 const __FLT32_DIG__ = 6 const __FLT32_EPSILON__ = 0 const __FLT32_HAS_DENORM__ = 1 const __FLT32_HAS_INFINITY__ = 1 const __FLT32_HAS_QUIET_NAN__ = 1 const __FLT32_IS_IEC_60559__ = 2 const __FLT32_MANT_DIG__ = 24 const __FLT32_MAX_10_EXP__ = 38 const __FLT32_MAX_EXP__ = 128 const __FLT32_MAX__ = 0 const __FLT32_MIN_10_EXP__ = -37 const __FLT32_MIN_EXP__ = -125 const __FLT32_MIN__ = 0 const __FLT32_NORM_MAX__ = 0 const __FLT64_DECIMAL_DIG__ = 17 const __FLT64_DENORM_MIN__ = 0 const __FLT64_DIG__ = 15 const __FLT64_EPSILON__ = 0 const __FLT64_HAS_DENORM__ = 1 const __FLT64_HAS_INFINITY__ = 1 const __FLT64_HAS_QUIET_NAN__ = 1 const __FLT64_IS_IEC_60559__ = 2 const __FLT64_MANT_DIG__ = 53 const __FLT64_MAX_10_EXP__ = 308 const __FLT64_MAX_EXP__ = 1024 const __FLT64_MAX__ = 0 const __FLT64_MIN_10_EXP__ = -307 const __FLT64_MIN_EXP__ = -1021 const __FLT64_MIN__ = 0 const __FLT64_NORM_MAX__ = 0 const __FLT_DECIMAL_DIG__ = 9 const __FLT_DENORM_MIN__ = 0 const __FLT_DIG__ = 6 const __FLT_EPSILON__ = 0 const __FLT_EVAL_METHOD_TS_18661_3__ = 0 const __FLT_EVAL_METHOD__ = 0 const __FLT_HAS_DENORM__ = 1 const __FLT_HAS_INFINITY__ = 1 const __FLT_HAS_QUIET_NAN__ = 1 const __FLT_IS_IEC_60559__ = 2 const __FLT_MANT_DIG__ = 24 const __FLT_MAX_10_EXP__ = 38 const __FLT_MAX_EXP__ = 128 const __FLT_MAX__ = 0 const __FLT_MIN_10_EXP__ = -37 const __FLT_MIN_EXP__ = -125 const __FLT_MIN__ = 0 const __FLT_NORM_MAX__ = 0 const __FLT_RADIX__ = 2 const __FP_LOGB0_IS_MIN = 0 const __FP_LOGBNAN_IS_MIN = 0 const __FP_LONG_MAX = 2147483647 const __FRACT_EPSILON__ = 0 const __FRACT_FBIT__ = 15 const __FRACT_IBIT__ = 0 const __FRACT_MAX__ = 0 const __FRACT_MIN__ = 0 const __FSBLKCNT64_T_TYPE = 0 const __FSBLKCNT_T_TYPE = "__ULONGWORD_TYPE" const __FSFILCNT64_T_TYPE = 0 const __FSFILCNT_T_TYPE = "__ULONGWORD_TYPE" const __FSWORD_T_TYPE = 0 const __FUNCTION__ = 0 const __F_GETOWN = 9 const __F_GETOWN_EX = 16 const __F_GETSIG = 11 const __F_SETOWN = 8 const __F_SETOWN_EX = 15 const __F_SETSIG = 10 const __GCC_ASM_FLAG_OUTPUTS__ = 1 const __GCC_ATOMIC_BOOL_LOCK_FREE = 1 const __GCC_ATOMIC_CHAR16_T_LOCK_FREE = 1 const __GCC_ATOMIC_CHAR32_T_LOCK_FREE = 2 const __GCC_ATOMIC_CHAR_LOCK_FREE = 1 const __GCC_ATOMIC_INT_LOCK_FREE = 2 const __GCC_ATOMIC_LLONG_LOCK_FREE = 1 const __GCC_ATOMIC_LONG_LOCK_FREE = 2 const __GCC_ATOMIC_POINTER_LOCK_FREE = 2 const __GCC_ATOMIC_SHORT_LOCK_FREE = 1 const __GCC_ATOMIC_TEST_AND_SET_TRUEVAL = 1 const __GCC_ATOMIC_WCHAR_T_LOCK_FREE = 2 const __GCC_CONSTRUCTIVE_SIZE = 64 const __GCC_DESTRUCTIVE_SIZE = 64 const __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 = 1 const __GCC_IEC_559 = 2 const __GCC_IEC_559_COMPLEX = 2 const __GID_T_TYPE = "__U32_TYPE" const __GLIBC_FLT_EVAL_METHOD = 0 const __GLIBC_MINOR__ = 36 const __GLIBC__ = 2 const __GNUC_EXECUTION_CHARSET_NAME = "UTF-8" const __GNUC_MINOR__ = 2 const __GNUC_PATCHLEVEL__ = 0 const __GNUC_STDC_INLINE__ = 1 const __GNUC_WIDE_EXECUTION_CHARSET_NAME = "UTF-32LE" const __GNUC__ = 12 const __GXX_ABI_VERSION = 1017 const __GXX_TYPEINFO_EQUALITY_INLINE = 0 const __HAVE_DISTINCT_FLOAT128 = 0 const __HAVE_DISTINCT_FLOAT128X = 0 const __HAVE_DISTINCT_FLOAT16 = 0 const __HAVE_DISTINCT_FLOAT32 = 0 const __HAVE_DISTINCT_FLOAT32X = 0 const __HAVE_DISTINCT_FLOAT64 = 0 const __HAVE_DISTINCT_FLOAT64X = 0 const __HAVE_FLOAT128 = 0 const __HAVE_FLOAT128X = 0 const __HAVE_FLOAT128_UNLIKE_LDBL = 0 const __HAVE_FLOAT16 = 0 const __HAVE_FLOAT32 = 1 const __HAVE_FLOAT32X = 1 const __HAVE_FLOAT64 = 1 const __HAVE_FLOAT64X = 0 const __HAVE_FLOAT64X_LONG_DOUBLE = 0 const __HAVE_FLOATN_NOT_TYPEDEF = 1 const __HAVE_GENERIC_SELECTION = 1 const __HAVE_SPECULATION_SAFE_VALUE = 1 const __HA_FBIT__ = 7 const __HA_IBIT__ = 8 const __HQ_FBIT__ = 15 const __HQ_IBIT__ = 0 const __ID_T_TYPE = "__U32_TYPE" const __ILP32_OFFBIG_CFLAGS = "-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64" const __INO64_T_TYPE = 0 const __INO_T_TYPE = "__ULONGWORD_TYPE" const __INT16_MAX__ = 32767 const __INT32_MAX__ = 2147483647 const __INT32_TYPE__ = 0 const __INT64_MAX__ = 9223372036854775807 const __INT8_MAX__ = 127 const __INTMAX_MAX__ = 9223372036854775807 const __INTMAX_WIDTH__ = 64 const __INTPTR_MAX__ = 2147483647 const __INTPTR_TYPE__ = 0 const __INTPTR_WIDTH__ = 32 const __INT_FAST16_MAX__ = 2147483647 const __INT_FAST16_TYPE__ = 0 const __INT_FAST16_WIDTH__ = 32 const __INT_FAST32_MAX__ = 2147483647 const __INT_FAST32_TYPE__ = 0 const __INT_FAST32_WIDTH__ = 32 const __INT_FAST64_MAX__ = 9223372036854775807 const __INT_FAST64_WIDTH__ = 64 const __INT_FAST8_MAX__ = 127 const __INT_FAST8_WIDTH__ = 8 const __INT_LEAST16_MAX__ = 32767 const __INT_LEAST16_WIDTH__ = 16 const __INT_LEAST32_MAX__ = 2147483647 const __INT_LEAST32_TYPE__ = 0 const __INT_LEAST32_WIDTH__ = 32 const __INT_LEAST64_MAX__ = 9223372036854775807 const __INT_LEAST64_WIDTH__ = 64 const __INT_LEAST8_MAX__ = 127 const __INT_LEAST8_WIDTH__ = 8 const __INT_MAX__ = 2147483647 const __INT_WIDTH__ = 32 const __KERNEL_OLD_TIMEVAL_MATCHES_TIMEVAL64 = 0 const __KEY_T_TYPE = 0 const __LACCUM_EPSILON__ = 0 const __LACCUM_FBIT__ = 31 const __LACCUM_IBIT__ = 32 const __LACCUM_MAX__ = 0 const __LACCUM_MIN__ = 0 const __LDBL_DECIMAL_DIG__ = 17 const __LDBL_DENORM_MIN__ = 0 const __LDBL_DIG__ = 15 const __LDBL_EPSILON__ = 0 const __LDBL_HAS_DENORM__ = 1 const __LDBL_HAS_INFINITY__ = 1 const __LDBL_HAS_QUIET_NAN__ = 1 const __LDBL_IS_IEC_60559__ = 2 const __LDBL_MANT_DIG__ = 53 const __LDBL_MAX_10_EXP__ = 308 const __LDBL_MAX_EXP__ = 1024 const __LDBL_MAX__ = 0 const __LDBL_MIN_10_EXP__ = -307 const __LDBL_MIN_EXP__ = -1021 const __LDBL_MIN__ = 0 const __LDBL_NORM_MAX__ = 0 const __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI = 0 const __LEAF = 0 const __LFRACT_EPSILON__ = 0 const __LFRACT_FBIT__ = 31 const __LFRACT_IBIT__ = 0 const __LFRACT_MAX__ = 0 const __LFRACT_MIN__ = 0 const __LITTLE_ENDIAN = 1234 const __LLACCUM_EPSILON__ = 0 const __LLACCUM_FBIT__ = 31 const __LLACCUM_IBIT__ = 32 const __LLACCUM_MAX__ = 0 const __LLACCUM_MIN__ = 0 const __LLFRACT_EPSILON__ = 0 const __LLFRACT_FBIT__ = 63 const __LLFRACT_IBIT__ = 0 const __LLFRACT_MAX__ = 0 const __LLFRACT_MIN__ = 0 const __LONG_LONG_MAX__ = 9223372036854775807 const __LONG_LONG_WIDTH__ = 64 const __LONG_MAX__ = 2147483647 const __LONG_WIDTH__ = 32 const __MATH_DECLARE_LDOUBLE = 1 const __MODE_T_TYPE = "__U32_TYPE" const __NLINK_T_TYPE = "__UWORD_TYPE" const __NO_INLINE__ = 1 const __NO_LONG_DOUBLE_MATH = 1 const __OFF64_T_TYPE = 0 const __OFF_T_TYPE = "__SLONGWORD_TYPE" const __ORDER_BIG_ENDIAN__ = 4321 const __ORDER_LITTLE_ENDIAN__ = 1234 const __ORDER_PDP_ENDIAN__ = 3412 const __O_CLOEXEC = 524288 const __O_DIRECT = 65536 const __O_DIRECTORY = 16384 const __O_DSYNC = 4096 const __O_LARGEFILE = 131072 const __O_NOATIME = 262144 const __O_NOFOLLOW = 32768 const __O_PATH = 2097152 const __O_TMPFILE = 4210688 const __PDP_ENDIAN = 3412 const __PID_T_TYPE = 0 const __POSIX2_THIS_VERSION = 200809 const __POSIX_FADV_DONTNEED = 4 const __POSIX_FADV_NOREUSE = 5 const __PRAGMA_REDEFINE_EXTNAME = 1 const __PRETTY_FUNCTION__ = 0 const __PTHREAD_MUTEX_HAVE_PREV = 0 const __PTRDIFF_MAX__ = 2147483647 const __PTRDIFF_TYPE__ = 0 const __PTRDIFF_WIDTH__ = 32 const __QQ_FBIT__ = 7 const __QQ_IBIT__ = 0 const __RLIM64_T_TYPE = 0 const __RLIM_T_MATCHES_RLIM64_T = 0 const __RLIM_T_TYPE = "__ULONGWORD_TYPE" const __S32_TYPE = 0 const __S64_TYPE = 0 const __SACCUM_EPSILON__ = 0 const __SACCUM_FBIT__ = 7 const __SACCUM_IBIT__ = 8 const __SACCUM_MAX__ = 0 const __SACCUM_MIN__ = 0 const __SA_FBIT__ = 15 const __SA_IBIT__ = 16 const __SCHAR_MAX__ = 127 const __SCHAR_WIDTH__ = 8 const __SFRACT_EPSILON__ = 0 const __SFRACT_FBIT__ = 7 const __SFRACT_IBIT__ = 0 const __SFRACT_MAX__ = 0 const __SFRACT_MIN__ = 0 const __SHRT_MAX__ = 32767 const __SHRT_WIDTH__ = 16 const __SIG_ATOMIC_MAX__ = 2147483647 const __SIG_ATOMIC_MIN__ = -2147483648 const __SIG_ATOMIC_TYPE__ = 0 const __SIG_ATOMIC_WIDTH__ = 32 const __SIZEOF_DOUBLE__ = 8 const __SIZEOF_FLOAT__ = 4 const __SIZEOF_INT__ = 4 const __SIZEOF_LONG_DOUBLE__ = 8 const __SIZEOF_LONG_LONG__ = 8 const __SIZEOF_LONG__ = 4 const __SIZEOF_POINTER__ = 4 const __SIZEOF_PTHREAD_ATTR_T = 36 const __SIZEOF_PTHREAD_BARRIERATTR_T = 4 const __SIZEOF_PTHREAD_BARRIER_T = 20 const __SIZEOF_PTHREAD_CONDATTR_T = 4 const __SIZEOF_PTHREAD_COND_T = 48 const __SIZEOF_PTHREAD_MUTEXATTR_T = 4 const __SIZEOF_PTHREAD_MUTEX_T = 24 const __SIZEOF_PTHREAD_RWLOCKATTR_T = 8 const __SIZEOF_PTHREAD_RWLOCK_T = 32 const __SIZEOF_PTRDIFF_T__ = 4 const __SIZEOF_SHORT__ = 2 const __SIZEOF_SIZE_T__ = 4 const __SIZEOF_WCHAR_T__ = 4 const __SIZEOF_WINT_T__ = 4 const __SIZE_MAX__ = 4294967295 const __SIZE_WIDTH__ = 32 const __SQLITESESSION_H_ = 1 const __SQUAD_TYPE = 0 const __SQ_FBIT__ = 31 const __SQ_IBIT__ = 0 const __SSIZE_T_TYPE = 0 const __STATFS_MATCHES_STATFS64 = 0 const __STDC_HOSTED__ = 1 const __STDC_IEC_559_COMPLEX__ = 1 const __STDC_IEC_559__ = 1 const __STDC_IEC_60559_BFP__ = 201404 const __STDC_IEC_60559_COMPLEX__ = 201404 const __STDC_ISO_10646__ = 201706 const __STDC_UTF_16__ = 1 const __STDC_UTF_32__ = 1 const __STDC_VERSION__ = 201710 const __STDC__ = 1 const __SUSECONDS64_T_TYPE = 0 const __SUSECONDS_T_TYPE = "__SLONGWORD_TYPE" const __SWORD_TYPE = 0 const __SYSCALL_SLONG_TYPE = "__SLONGWORD_TYPE" const __SYSCALL_ULONG_TYPE = "__ULONGWORD_TYPE" const __S_IEXEC = 64 const __S_IFBLK = 24576 const __S_IFCHR = 8192 const __S_IFDIR = 16384 const __S_IFIFO = 4096 const __S_IFLNK = 40960 const __S_IFMT = 61440 const __S_IFREG = 32768 const __S_IFSOCK = 49152 const __S_IREAD = 256 const __S_ISGID = 1024 const __S_ISUID = 2048 const __S_ISVTX = 512 const __S_IWRITE = 128 const __TA_FBIT__ = 63 const __TA_IBIT__ = 64 const __THUMB_INTERWORK__ = 1 const __TIME64_T_TYPE = 0 const __TIMER_T_TYPE = 0 const __TIMESIZE = 32 const __TIME_T_TYPE = "__SLONGWORD_TYPE" const __TQ_FBIT__ = 127 const __TQ_IBIT__ = 0 const __U64_TYPE = 0 const __UACCUM_EPSILON__ = 0 const __UACCUM_FBIT__ = 16 const __UACCUM_IBIT__ = 16 const __UACCUM_MAX__ = 0 const __UACCUM_MIN__ = 0 const __UDA_FBIT__ = 32 const __UDA_IBIT__ = 32 const __UDQ_FBIT__ = 64 const __UDQ_IBIT__ = 0 const __UFRACT_EPSILON__ = 0 const __UFRACT_FBIT__ = 16 const __UFRACT_IBIT__ = 0 const __UFRACT_MAX__ = 0 const __UFRACT_MIN__ = 0 const __UHA_FBIT__ = 8 const __UHA_IBIT__ = 8 const __UHQ_FBIT__ = 16 const __UHQ_IBIT__ = 0 const __UID_T_TYPE = "__U32_TYPE" const __UINT16_MAX__ = 65535 const __UINT32_MAX__ = 4294967295 const __UINT64_MAX__ = 18446744073709551615 const __UINT8_MAX__ = 255 const __UINTMAX_MAX__ = 18446744073709551615 const __UINTPTR_MAX__ = 4294967295 const __UINT_FAST16_MAX__ = 4294967295 const __UINT_FAST32_MAX__ = 4294967295 const __UINT_FAST64_MAX__ = 18446744073709551615 const __UINT_FAST8_MAX__ = 255 const __UINT_LEAST16_MAX__ = 65535 const __UINT_LEAST32_MAX__ = 4294967295 const __UINT_LEAST64_MAX__ = 18446744073709551615 const __UINT_LEAST8_MAX__ = 255 const __ULACCUM_EPSILON__ = 0 const __ULACCUM_FBIT__ = 32 const __ULACCUM_IBIT__ = 32 const __ULACCUM_MAX__ = 0 const __ULACCUM_MIN__ = 0 const __ULFRACT_EPSILON__ = 0 const __ULFRACT_FBIT__ = 32 const __ULFRACT_IBIT__ = 0 const __ULFRACT_MAX__ = 0 const __ULFRACT_MIN__ = 0 const __ULLACCUM_EPSILON__ = 0 const __ULLACCUM_FBIT__ = 32 const __ULLACCUM_IBIT__ = 32 const __ULLACCUM_MAX__ = 0 const __ULLACCUM_MIN__ = 0 const __ULLFRACT_EPSILON__ = 0 const __ULLFRACT_FBIT__ = 64 const __ULLFRACT_IBIT__ = 0 const __ULLFRACT_MAX__ = 0 const __ULLFRACT_MIN__ = 0 const __UQQ_FBIT__ = 8 const __UQQ_IBIT__ = 0 const __UQUAD_TYPE = 0 const __USACCUM_EPSILON__ = 0 const __USACCUM_FBIT__ = 8 const __USACCUM_IBIT__ = 8 const __USACCUM_MAX__ = 0 const __USACCUM_MIN__ = 0 const __USA_FBIT__ = 16 const __USA_IBIT__ = 16 const __USECONDS_T_TYPE = "__U32_TYPE" const __USFRACT_EPSILON__ = 0 const __USFRACT_FBIT__ = 8 const __USFRACT_IBIT__ = 0 const __USFRACT_MAX__ = 0 const __USFRACT_MIN__ = 0 const __USQ_FBIT__ = 32 const __USQ_IBIT__ = 0 const __UTA_FBIT__ = 64 const __UTA_IBIT__ = 64 const __UTQ_FBIT__ = 128 const __UTQ_IBIT__ = 0 const __VERSION__ = "12.2.0" const __VFP_FP__ = 1 const __WALL = 1073741824 const __WCHAR_MAX__ = 4294967295 const __WCHAR_MIN__ = 0 const __WCHAR_WIDTH__ = 32 const __WCLONE = 2147483648 const __WCOREFLAG = 128 const __WINT_MAX__ = 4294967295 const __WINT_MIN__ = 0 const __WINT_WIDTH__ = 32 const __WNOTHREAD = 536870912 const __WORDSIZE = 32 const __WORDSIZE32_PTRDIFF_LONG = 0 const __WORDSIZE32_SIZE_ULONG = 0 const __WORDSIZE_TIME64_COMPAT32 = 0 const __W_CONTINUED = 65535 const ____FILE_defined = 1 const _____fpos64_t_defined = 1 const _____fpos_t_defined = 1 const ____mbstate_t_defined = 1 const __arm__ = 1 const __clock_t_defined = 1 const __clockid_t_defined = 1 const __cookie_io_functions_t_defined = 1 const __error_t_defined = 1 const __glibc_c99_flexarr_available = 1 const __gnu_linux__ = 1 const __have_pthread_attr_t = 1 const __iovec_defined = 1 const __itimerspec_defined = 1 const __ldiv_t_defined = 1 const __linux = 1 const __linux__ = 1 const __lldiv_t_defined = 1 const __ptr_t = 0 const __sigset_t_defined = 1 const __statx_defined = 1 const __statx_timestamp_defined = 1 const __struct_FILE_defined = 1 const __struct_tm_defined = 1 const __time_t_defined = 1 const __timer_t_defined = 1 const __timeval_defined = 1 const __unix = 1 const __unix__ = 1 const bBatch = 0 const cume_distFinalizeFunc = 0 const errno = 0 const etBUFSIZE = 70 const etCHARX = 8 const etDECIMAL = 16 const etDYNSTRING = 6 const etEXP = 2 const etFLOAT = 1 const etGENERIC = 3 const etINVALID = 17 const etORDINAL = 15 const etPERCENT = 7 const etPOINTER = 13 const etRADIX = 0 const etSIZE = 4 const etSQLESCAPE = 9 const etSQLESCAPE2 = 10 const etSQLESCAPE3 = 14 const etSRCITEM = 12 const etSTRING = 5 const etTOKEN = 11 const fdatasync = 0 const first_valueInvFunc = 0 const first_valueValueFunc = 0 const fts5GetVarint = 0 const fts5YYMALLOCARGTYPE = 0 const fts5YYNFTS5TOKEN = 16 const fts5YYNOCODE = 27 const fts5YYNOERRORRECOVERY = 1 const fts5YYNRULE = 28 const fts5YYNRULE_WITH_ACTION = 28 const fts5YYNSTATE = 35 const fts5YYPARSEFREENOTNULL = 1 const fts5YYSTACKDEPTH = 100 const fts5YY_ACCEPT_ACTION = 81 const fts5YY_ACTTAB_COUNT = 105 const fts5YY_ERROR_ACTION = 80 const fts5YY_MAX_REDUCE = 110 const fts5YY_MAX_SHIFT = 34 const fts5YY_MAX_SHIFTREDUCE = 79 const fts5YY_MIN_REDUCE = 83 const fts5YY_MIN_SHIFTREDUCE = 52 const fts5YY_NO_ACTION = 82 const fts5YY_REDUCE_COUNT = 17 const fts5YY_REDUCE_MAX = 67 const fts5YY_REDUCE_MIN = -17 const fts5YY_SHIFT_COUNT = 34 const fts5YY_SHIFT_MAX = 93 const fts5YY_SHIFT_MIN = 0 const get4byte = 0 const getVarint = 0 const linux = 1 const math_errhandling = 3 const mem0 = 0 const noopFunc = 0 const nth_valueInvFunc = 0 const nth_valueValueFunc = 0 const ntileFinalizeFunc = 0 const pcache1 = 0 const percent_rankFinalizeFunc = 0 const pread64 = 0 const put4byte = 0 const putVarint = 0 const pwrite64 = 0 const sqlite3Fts5ParserARG_PARAM = 0 const sqlite3Fts5ParserARG_PDECL = 0 const sqlite3Fts5ParserFTS5TOKENTYPE = 0 const sqlite3GlobalConfig = 0 const sqlite3ParserCTX_PARAM = 0 const sqlite3ParserCTX_PDECL = 0 const sqlite3ParserTOKENTYPE = 0 const sqlite3Parser_ENGINEALWAYSONSTACK = 1 const sqlite3StrNICmp = 0 const threadid = 0 const tkCREATE = 4 const tkEND = 7 const tkEXPLAIN = 3 const tkOTHER = 2 const tkSEMI = 0 const tkTEMP = 5 const tkTRIGGER = 6 const tkWS = 1 const unix = 1 const vfsList = 0 const wsdAutoext = 0 const wsdHooks = 0 const wsdPrng = 0 const wsdStat = 0 type t__builtin_va_list = uintptr type t__predefined_size_t = uint32 type t__predefined_wchar_t = uint32 type t__predefined_ptrdiff_t = int32 type t__gnuc_va_list = uintptr type Tva_list = uintptr type va_list = Tva_list // C documentation // // /* // ** CAPI3REF: Database Connection Handle // ** KEYWORDS: {database connection} {database connections} // ** // ** Each open SQLite database is represented by a pointer to an instance of // ** the opaque structure named "sqlite3". It is useful to think of an sqlite3 // ** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and // ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()] // ** and [sqlite3_close_v2()] are its destructors. There are many other // ** interfaces (such as // ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and // ** [sqlite3_busy_timeout()] to name but three) that are methods on an // ** sqlite3 object. // */ type Tsqlite3 = struct { F__ccgo_align [0]uint32 FpVfs uintptr FpVdbe uintptr FpDfltColl uintptr Fmutex uintptr FaDb uintptr FnDb int32 FmDbFlags Tu32 F__ccgo_align7 [4]byte Fflags Tu64 FlastRowid Ti64 FszMmap Ti64 FnSchemaLock Tu32 FopenFlags uint32 FerrCode int32 FerrByteOffset int32 FerrMask int32 FiSysErrno int32 FdbOptFlags Tu32 Fenc Tu8 FautoCommit Tu8 Ftemp_store Tu8 FmallocFailed Tu8 FbBenignMalloc Tu8 FdfltLockMode Tu8 FnextAutovac int8 FsuppressErr Tu8 FvtabOnConflict Tu8 FisTransactionSavepoint Tu8 FmTrace Tu8 FnoSharedCache Tu8 FnSqlExec Tu8 FeOpenState Tu8 FnextPagesize int32 FnChange Ti64 FnTotalChange Ti64 FaLimit [12]int32 FnMaxSorterMmap int32 Finit1 Tsqlite3InitInfo FnVdbeActive int32 FnVdbeRead int32 FnVdbeWrite int32 FnVdbeExec int32 FnVDestroy int32 FnExtension int32 FaExtension uintptr Ftrace struct { FxV2 [0]uintptr FxLegacy uintptr } FpTraceArg uintptr FxProfile uintptr FpProfileArg uintptr FpCommitArg uintptr FxCommitCallback uintptr FpRollbackArg uintptr FxRollbackCallback uintptr FpUpdateArg uintptr FxUpdateCallback uintptr FpAutovacPagesArg uintptr FxAutovacDestr uintptr FxAutovacPages uintptr FpParse uintptr FpPreUpdateArg uintptr FxPreUpdateCallback uintptr FpPreUpdate uintptr FxWalCallback uintptr FpWalArg uintptr FxCollNeeded uintptr FxCollNeeded16 uintptr FpCollNeededArg uintptr FpErr uintptr F__ccgo_align67 [4]byte Fu1 struct { F__ccgo_align [0]uint32 FnotUsed1 [0]float64 FisInterrupted int32 F__ccgo_pad2 [4]byte } Flookaside TLookaside FxAuth Tsqlite3_xauth FpAuthArg uintptr FxProgress uintptr FpProgressArg uintptr FnProgressOps uint32 FnVTrans int32 FaModule THash FpVtabCtx uintptr FaVTrans uintptr FpDisconnect uintptr FaFunc THash FaCollSeq THash FbusyHandler TBusyHandler FaDbStatic [2]TDb FpSavepoint uintptr FnAnalysisLimit int32 FbusyTimeout int32 FnSavepoint int32 FnStatement int32 FnDeferredCons Ti64 FnDeferredImmCons Ti64 FpnBytesFreed uintptr FpDbData uintptr FpBlockingConnection uintptr FpUnlockConnection uintptr FpUnlockArg uintptr FxUnlockNotify uintptr FpNextBlocked uintptr F__ccgo_pad97 [4]byte } type sqlite3 = Tsqlite3 // C documentation // // /* // ** CAPI3REF: 64-Bit Integer Types // ** KEYWORDS: sqlite_int64 sqlite_uint64 // ** // ** Because there is no cross-platform way to specify 64-bit integer types // ** SQLite includes typedefs for 64-bit signed and unsigned integers. // ** // ** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions. // ** The sqlite_int64 and sqlite_uint64 types are supported for backwards // ** compatibility only. // ** // ** ^The sqlite3_int64 and sqlite_int64 types can store integer values // ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The // ** sqlite3_uint64 and sqlite_uint64 types can store integer values // ** between 0 and +18446744073709551615 inclusive. // */ type Tsqlite_int64 = int64 type sqlite_int64 = Tsqlite_int64 type Tsqlite_uint64 = uint64 type sqlite_uint64 = Tsqlite_uint64 type Tsqlite3_int64 = int64 type sqlite3_int64 = Tsqlite3_int64 type Tsqlite3_uint64 = uint64 type sqlite3_uint64 = Tsqlite3_uint64 // C documentation // // /* // ** The type for a callback function. // ** This is legacy and deprecated. It is included for historical // ** compatibility and is not documented. // */ type Tsqlite3_callback = uintptr type sqlite3_callback = Tsqlite3_callback /* ** CAPI3REF: Result Codes ** KEYWORDS: {result code definitions} ** ** Many SQLite functions return an integer result code from the set shown ** here in order to indicate success or failure. ** ** New error codes may be added in future versions of SQLite. ** ** See also: [extended result code definitions] */ /* beginning-of-error-codes */ /* end-of-error-codes */ /* ** CAPI3REF: Extended Result Codes ** KEYWORDS: {extended result code definitions} ** ** In its default configuration, SQLite API routines return one of 30 integer ** [result codes]. However, experience has shown that many of ** these result codes are too coarse-grained. They do not provide as ** much information about problems as programmers might like. In an effort to ** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8] ** and later) include ** support for additional result codes that provide more detailed information ** about errors. These [extended result codes] are enabled or disabled ** on a per database connection basis using the ** [sqlite3_extended_result_codes()] API. Or, the extended code for ** the most recent error can be obtained using ** [sqlite3_extended_errcode()]. */ /* ** CAPI3REF: Flags For File Open Operations ** ** These bit values are intended for use in the ** 3rd parameter to the [sqlite3_open_v2()] interface and ** in the 4th parameter to the [sqlite3_vfs.xOpen] method. ** ** Only those flags marked as "Ok for sqlite3_open_v2()" may be ** used as the third argument to the [sqlite3_open_v2()] interface. ** The other flags have historically been ignored by sqlite3_open_v2(), ** though future versions of SQLite might change so that an error is ** raised if any of the disallowed bits are passed into sqlite3_open_v2(). ** Applications should not depend on the historical behavior. ** ** Note in particular that passing the SQLITE_OPEN_EXCLUSIVE flag into ** [sqlite3_open_v2()] does *not* cause the underlying database file ** to be opened using O_EXCL. Passing SQLITE_OPEN_EXCLUSIVE into ** [sqlite3_open_v2()] has historically be a no-op and might become an ** error in future versions of SQLite. */ /* Reserved: 0x00F00000 */ /* Legacy compatibility: */ /* ** CAPI3REF: Device Characteristics ** ** The xDeviceCharacteristics method of the [sqlite3_io_methods] ** object returns an integer which is a vector of these ** bit values expressing I/O characteristics of the mass storage ** device that holds the file that the [sqlite3_io_methods] ** refers to. ** ** The SQLITE_IOCAP_ATOMIC property means that all writes of ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values ** mean that writes of blocks that are nnn bytes in size and ** are aligned to an address which is an integer multiple of ** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means ** that when data is appended to a file, the data is appended ** first then the size of the file is extended, never the other ** way around. The SQLITE_IOCAP_SEQUENTIAL property means that ** information is written to disk in the same order as calls ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that ** after reboot following a crash or power loss, the only bytes in a ** file that were written at the application level might have changed ** and that adjacent bytes, even bytes within the same sector are ** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN ** flag indicates that a file cannot be deleted when open. The ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on ** read-only media and cannot be changed even by processes with ** elevated privileges. ** ** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying ** filesystem supports doing multiple write operations atomically when those ** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and ** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. */ /* ** CAPI3REF: File Locking Levels ** ** SQLite uses one of these integer values as the second ** argument to calls it makes to the xLock() and xUnlock() methods ** of an [sqlite3_io_methods] object. These values are ordered from ** lest restrictive to most restrictive. ** ** The argument to xLock() is always SHARED or higher. The argument to ** xUnlock is either SHARED or NONE. */ /* ** CAPI3REF: Synchronization Type Flags ** ** When SQLite invokes the xSync() method of an ** [sqlite3_io_methods] object it uses a combination of ** these integer values as the second argument. ** ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the ** sync operation only needs to flush data to mass storage. Inode ** information need not be flushed. If the lower four bits of the flag ** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics. ** If the lower four bits equal SQLITE_SYNC_FULL, that means ** to use Mac OS X style fullsync instead of fsync(). ** ** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags ** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL ** settings. The [synchronous pragma] determines when calls to the ** xSync VFS method occur and applies uniformly across all platforms. ** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how ** energetic or rigorous or forceful the sync operations are and ** only make a difference on Mac OSX for the default SQLite code. ** (Third-party VFS implementations might also make the distinction ** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the ** operating systems natively supported by SQLite, only Mac OSX ** cares about the difference.) */ // C documentation // // /* // ** CAPI3REF: OS Interface Open File Handle // ** // ** An [sqlite3_file] object represents an open file in the // ** [sqlite3_vfs | OS interface layer]. Individual OS interface // ** implementations will // ** want to subclass this object by appending additional fields // ** for their own use. The pMethods entry is a pointer to an // ** [sqlite3_io_methods] object that defines methods for performing // ** I/O operations on the open file. // */ type Tsqlite3_file = struct { FpMethods uintptr } type sqlite3_file = Tsqlite3_file type Tsqlite3_file1 = struct { FpMethods uintptr } type sqlite3_file1 = Tsqlite3_file1 // C documentation // // /* // ** CAPI3REF: OS Interface File Virtual Methods Object // ** // ** Every file opened by the [sqlite3_vfs.xOpen] method populates an // ** [sqlite3_file] object (or, more commonly, a subclass of the // ** [sqlite3_file] object) with a pointer to an instance of this object. // ** This object defines the methods used to perform various operations // ** against the open file represented by the [sqlite3_file] object. // ** // ** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element // ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method // ** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The // ** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen] // ** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element // ** to NULL. // ** // ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or // ** [SQLITE_SYNC_FULL]. The first choice is the normal fsync(). // ** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY] // ** flag may be ORed in to indicate that only the data of the file // ** and not its inode needs to be synced. // ** // ** The integer values to xLock() and xUnlock() are one of // ** // ** xLock() upgrades the database file lock. In other words, xLock() moves the // ** database file lock in the direction NONE toward EXCLUSIVE. The argument to // ** xLock() is always on of SHARED, RESERVED, PENDING, or EXCLUSIVE, never // ** SQLITE_LOCK_NONE. If the database file lock is already at or above the // ** requested lock, then the call to xLock() is a no-op. // ** xUnlock() downgrades the database file lock to either SHARED or NONE. // * If the lock is already at or below the requested lock state, then the call // ** to xUnlock() is a no-op. // ** The xCheckReservedLock() method checks whether any database connection, // ** either in this process or in some other process, is holding a RESERVED, // ** PENDING, or EXCLUSIVE lock on the file. It returns true // ** if such a lock exists and false otherwise. // ** // ** The xFileControl() method is a generic interface that allows custom // ** VFS implementations to directly control an open file using the // ** [sqlite3_file_control()] interface. The second "op" argument is an // ** integer opcode. The third argument is a generic pointer intended to // ** point to a structure that may contain arguments or space in which to // ** write return values. Potential uses for xFileControl() might be // ** functions to enable blocking locks with timeouts, to change the // ** locking strategy (for example to use dot-file locks), to inquire // ** about the status of a lock, or to break stale locks. The SQLite // ** core reserves all opcodes less than 100 for its own use. // ** A [file control opcodes | list of opcodes] less than 100 is available. // ** Applications that define a custom xFileControl method should use opcodes // ** greater than 100 to avoid conflicts. VFS implementations should // ** return [SQLITE_NOTFOUND] for file control opcodes that they do not // ** recognize. // ** // ** The xSectorSize() method returns the sector size of the // ** device that underlies the file. The sector size is the // ** minimum write that can be performed without disturbing // ** other bytes in the file. The xDeviceCharacteristics() // ** method returns a bit vector describing behaviors of the // ** underlying device: // ** // ** // ** // ** The SQLITE_IOCAP_ATOMIC property means that all writes of // ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values // ** mean that writes of blocks that are nnn bytes in size and // ** are aligned to an address which is an integer multiple of // ** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means // ** that when data is appended to a file, the data is appended // ** first then the size of the file is extended, never the other // ** way around. The SQLITE_IOCAP_SEQUENTIAL property means that // ** information is written to disk in the same order as calls // ** to xWrite(). // ** // ** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill // ** in the unread portions of the buffer with zeros. A VFS that // ** fails to zero-fill short reads might seem to work. However, // ** failure to zero-fill short reads will eventually lead to // ** database corruption. // */ type Tsqlite3_io_methods = struct { FiVersion int32 FxClose uintptr FxRead uintptr FxWrite uintptr FxTruncate uintptr FxSync uintptr FxFileSize uintptr FxLock uintptr FxUnlock uintptr FxCheckReservedLock uintptr FxFileControl uintptr FxSectorSize uintptr FxDeviceCharacteristics uintptr FxShmMap uintptr FxShmLock uintptr FxShmBarrier uintptr FxShmUnmap uintptr FxFetch uintptr FxUnfetch uintptr } type sqlite3_io_methods = Tsqlite3_io_methods type Tsqlite3_io_methods1 = struct { FiVersion int32 FxClose uintptr FxRead uintptr FxWrite uintptr FxTruncate uintptr FxSync uintptr FxFileSize uintptr FxLock uintptr FxUnlock uintptr FxCheckReservedLock uintptr FxFileControl uintptr FxSectorSize uintptr FxDeviceCharacteristics uintptr FxShmMap uintptr FxShmLock uintptr FxShmBarrier uintptr FxShmUnmap uintptr FxFetch uintptr FxUnfetch uintptr } type sqlite3_io_methods1 = Tsqlite3_io_methods1 // C documentation // // /* // ** CAPI3REF: Loadable Extension Thunk // ** // ** A pointer to the opaque sqlite3_api_routines structure is passed as // ** the third parameter to entry points of [loadable extensions]. This // ** structure must be typedefed in order to work around compiler warnings // ** on some platforms. // */ type Tsqlite3_api_routines = struct { Faggregate_context uintptr Faggregate_count uintptr Fbind_blob uintptr Fbind_double uintptr Fbind_int uintptr Fbind_int64 uintptr Fbind_null uintptr Fbind_parameter_count uintptr Fbind_parameter_index uintptr Fbind_parameter_name uintptr Fbind_text uintptr Fbind_text16 uintptr Fbind_value uintptr Fbusy_handler uintptr Fbusy_timeout uintptr Fchanges uintptr Fclose1 uintptr Fcollation_needed uintptr Fcollation_needed16 uintptr Fcolumn_blob uintptr Fcolumn_bytes uintptr Fcolumn_bytes16 uintptr Fcolumn_count uintptr Fcolumn_database_name uintptr Fcolumn_database_name16 uintptr Fcolumn_decltype uintptr Fcolumn_decltype16 uintptr Fcolumn_double uintptr Fcolumn_int uintptr Fcolumn_int64 uintptr Fcolumn_name uintptr Fcolumn_name16 uintptr Fcolumn_origin_name uintptr Fcolumn_origin_name16 uintptr Fcolumn_table_name uintptr Fcolumn_table_name16 uintptr Fcolumn_text uintptr Fcolumn_text16 uintptr Fcolumn_type uintptr Fcolumn_value uintptr Fcommit_hook uintptr Fcomplete uintptr Fcomplete16 uintptr Fcreate_collation uintptr Fcreate_collation16 uintptr Fcreate_function uintptr Fcreate_function16 uintptr Fcreate_module uintptr Fdata_count uintptr Fdb_handle uintptr Fdeclare_vtab uintptr Fenable_shared_cache uintptr Ferrcode uintptr Ferrmsg uintptr Ferrmsg16 uintptr Fexec uintptr Fexpired uintptr Ffinalize uintptr Ffree uintptr Ffree_table uintptr Fget_autocommit uintptr Fget_auxdata uintptr Fget_table uintptr Fglobal_recover uintptr Finterruptx uintptr Flast_insert_rowid uintptr Flibversion uintptr Flibversion_number uintptr Fmalloc uintptr Fmprintf uintptr Fopen uintptr Fopen16 uintptr Fprepare uintptr Fprepare16 uintptr Fprofile uintptr Fprogress_handler uintptr Frealloc uintptr Freset uintptr Fresult_blob uintptr Fresult_double uintptr Fresult_error uintptr Fresult_error16 uintptr Fresult_int uintptr Fresult_int64 uintptr Fresult_null uintptr Fresult_text uintptr Fresult_text16 uintptr Fresult_text16be uintptr Fresult_text16le uintptr Fresult_value uintptr Frollback_hook uintptr Fset_authorizer uintptr Fset_auxdata uintptr Fxsnprintf uintptr Fstep uintptr Ftable_column_metadata uintptr Fthread_cleanup uintptr Ftotal_changes uintptr Ftrace uintptr Ftransfer_bindings uintptr Fupdate_hook uintptr Fuser_data uintptr Fvalue_blob uintptr Fvalue_bytes uintptr Fvalue_bytes16 uintptr Fvalue_double uintptr Fvalue_int uintptr Fvalue_int64 uintptr Fvalue_numeric_type uintptr Fvalue_text uintptr Fvalue_text16 uintptr Fvalue_text16be uintptr Fvalue_text16le uintptr Fvalue_type uintptr Fvmprintf uintptr Foverload_function uintptr Fprepare_v2 uintptr Fprepare16_v2 uintptr Fclear_bindings uintptr Fcreate_module_v2 uintptr Fbind_zeroblob uintptr Fblob_bytes uintptr Fblob_close uintptr Fblob_open uintptr Fblob_read uintptr Fblob_write uintptr Fcreate_collation_v2 uintptr Ffile_control uintptr Fmemory_highwater uintptr Fmemory_used uintptr Fmutex_alloc uintptr Fmutex_enter uintptr Fmutex_free uintptr Fmutex_leave uintptr Fmutex_try uintptr Fopen_v2 uintptr Frelease_memory uintptr Fresult_error_nomem uintptr Fresult_error_toobig uintptr Fsleep uintptr Fsoft_heap_limit uintptr Fvfs_find uintptr Fvfs_register uintptr Fvfs_unregister uintptr Fxthreadsafe uintptr Fresult_zeroblob uintptr Fresult_error_code uintptr Ftest_control uintptr Frandomness uintptr Fcontext_db_handle uintptr Fextended_result_codes uintptr Flimit uintptr Fnext_stmt uintptr Fsql uintptr Fstatus uintptr Fbackup_finish uintptr Fbackup_init uintptr Fbackup_pagecount uintptr Fbackup_remaining uintptr Fbackup_step uintptr Fcompileoption_get uintptr Fcompileoption_used uintptr Fcreate_function_v2 uintptr Fdb_config uintptr Fdb_mutex uintptr Fdb_status uintptr Fextended_errcode uintptr Flog uintptr Fsoft_heap_limit64 uintptr Fsourceid uintptr Fstmt_status uintptr Fstrnicmp uintptr Funlock_notify uintptr Fwal_autocheckpoint uintptr Fwal_checkpoint uintptr Fwal_hook uintptr Fblob_reopen uintptr Fvtab_config uintptr Fvtab_on_conflict uintptr Fclose_v2 uintptr Fdb_filename uintptr Fdb_readonly uintptr Fdb_release_memory uintptr Ferrstr uintptr Fstmt_busy uintptr Fstmt_readonly uintptr Fstricmp uintptr Furi_boolean uintptr Furi_int64 uintptr Furi_parameter uintptr Fxvsnprintf uintptr Fwal_checkpoint_v2 uintptr Fauto_extension uintptr Fbind_blob64 uintptr Fbind_text64 uintptr Fcancel_auto_extension uintptr Fload_extension uintptr Fmalloc64 uintptr Fmsize uintptr Frealloc64 uintptr Freset_auto_extension uintptr Fresult_blob64 uintptr Fresult_text64 uintptr Fstrglob uintptr Fvalue_dup uintptr Fvalue_free uintptr Fresult_zeroblob64 uintptr Fbind_zeroblob64 uintptr Fvalue_subtype uintptr Fresult_subtype uintptr Fstatus64 uintptr Fstrlike uintptr Fdb_cacheflush uintptr Fsystem_errno uintptr Ftrace_v2 uintptr Fexpanded_sql uintptr Fset_last_insert_rowid uintptr Fprepare_v3 uintptr Fprepare16_v3 uintptr Fbind_pointer uintptr Fresult_pointer uintptr Fvalue_pointer uintptr Fvtab_nochange uintptr Fvalue_nochange uintptr Fvtab_collation uintptr Fkeyword_count uintptr Fkeyword_name uintptr Fkeyword_check uintptr Fstr_new uintptr Fstr_finish uintptr Fstr_appendf uintptr Fstr_vappendf uintptr Fstr_append uintptr Fstr_appendall uintptr Fstr_appendchar uintptr Fstr_reset uintptr Fstr_errcode uintptr Fstr_length uintptr Fstr_value uintptr Fcreate_window_function uintptr Fnormalized_sql uintptr Fstmt_isexplain uintptr Fvalue_frombind uintptr Fdrop_modules uintptr Fhard_heap_limit64 uintptr Furi_key uintptr Ffilename_database uintptr Ffilename_journal uintptr Ffilename_wal uintptr Fcreate_filename uintptr Ffree_filename uintptr Fdatabase_file_object uintptr Ftxn_state uintptr Fchanges64 uintptr Ftotal_changes64 uintptr Fautovacuum_pages uintptr Ferror_offset uintptr Fvtab_rhs_value uintptr Fvtab_distinct uintptr Fvtab_in uintptr Fvtab_in_first uintptr Fvtab_in_next uintptr Fdeserialize uintptr Fserialize uintptr Fdb_name uintptr Fvalue_encoding uintptr Fis_interrupted uintptr Fstmt_explain uintptr Fget_clientdata uintptr Fset_clientdata uintptr } type sqlite3_api_routines = Tsqlite3_api_routines // C documentation // // /* // ** CAPI3REF: File Name // ** // ** Type [sqlite3_filename] is used by SQLite to pass filenames to the // ** xOpen method of a [VFS]. It may be cast to (const char*) and treated // ** as a normal, nul-terminated, UTF-8 buffer containing the filename, but // ** may also be passed to special APIs such as: // ** // ** // */ type Tsqlite3_filename = uintptr type sqlite3_filename = Tsqlite3_filename // C documentation // // /* // ** CAPI3REF: OS Interface Object // ** // ** An instance of the sqlite3_vfs object defines the interface between // ** the SQLite core and the underlying operating system. The "vfs" // ** in the name of the object stands for "virtual file system". See // ** the [VFS | VFS documentation] for further information. // ** // ** The VFS interface is sometimes extended by adding new methods onto // ** the end. Each time such an extension occurs, the iVersion field // ** is incremented. The iVersion value started out as 1 in // ** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2 // ** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased // ** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6]. Additional fields // ** may be appended to the sqlite3_vfs object and the iVersion value // ** may increase again in future versions of SQLite. // ** Note that due to an oversight, the structure // ** of the sqlite3_vfs object changed in the transition from // ** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0] // ** and yet the iVersion field was not increased. // ** // ** The szOsFile field is the size of the subclassed [sqlite3_file] // ** structure used by this VFS. mxPathname is the maximum length of // ** a pathname in this VFS. // ** // ** Registered sqlite3_vfs objects are kept on a linked list formed by // ** the pNext pointer. The [sqlite3_vfs_register()] // ** and [sqlite3_vfs_unregister()] interfaces manage this list // ** in a thread-safe way. The [sqlite3_vfs_find()] interface // ** searches the list. Neither the application code nor the VFS // ** implementation should use the pNext pointer. // ** // ** The pNext field is the only field in the sqlite3_vfs // ** structure that SQLite will ever modify. SQLite will only access // ** or modify this field while holding a particular static mutex. // ** The application should never modify anything within the sqlite3_vfs // ** object once the object has been registered. // ** // ** The zName field holds the name of the VFS module. The name must // ** be unique across all VFS modules. // ** // ** [[sqlite3_vfs.xOpen]] // ** ^SQLite guarantees that the zFilename parameter to xOpen // ** is either a NULL pointer or string obtained // ** from xFullPathname() with an optional suffix added. // ** ^If a suffix is added to the zFilename parameter, it will // ** consist of a single "-" character followed by no more than // ** 11 alphanumeric and/or "-" characters. // ** ^SQLite further guarantees that // ** the string will be valid and unchanged until xClose() is // ** called. Because of the previous sentence, // ** the [sqlite3_file] can safely store a pointer to the // ** filename if it needs to remember the filename for some reason. // ** If the zFilename parameter to xOpen is a NULL pointer then xOpen // ** must invent its own temporary name for the file. ^Whenever the // ** xFilename parameter is NULL it will also be the case that the // ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE]. // ** // ** The flags argument to xOpen() includes all bits set in // ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] // ** or [sqlite3_open16()] is used, then flags includes at least // ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. // ** If xOpen() opens a file read-only then it sets *pOutFlags to // ** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set. // ** // ** ^(SQLite will also add one of the following flags to the xOpen() // ** call, depending on the object being opened: // ** // ** )^ // ** // ** The file I/O implementation can use the object type flags to // ** change the way it deals with files. For example, an application // ** that does not care about crash recovery or rollback might make // ** the open of a journal file a no-op. Writes to this journal would // ** also be no-ops, and any attempt to read the journal would return // ** SQLITE_IOERR. Or the implementation might recognize that a database // ** file will be doing page-aligned sector reads and writes in a random // ** order and set up its I/O subsystem accordingly. // ** // ** SQLite might also add one of the following flags to the xOpen method: // ** // ** // ** // ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be // ** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE] // ** will be set for TEMP databases and their journals, transient // ** databases, and subjournals. // ** // ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction // ** with the [SQLITE_OPEN_CREATE] flag, which are both directly // ** analogous to the O_EXCL and O_CREAT flags of the POSIX open() // ** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the // ** SQLITE_OPEN_CREATE, is used to indicate that file should always // ** be created, and that it is an error if it already exists. // ** It is not used to indicate the file should be opened // ** for exclusive access. // ** // ** ^At least szOsFile bytes of memory are allocated by SQLite // ** to hold the [sqlite3_file] structure passed as the third // ** argument to xOpen. The xOpen method does not have to // ** allocate the structure; it should just fill it in. Note that // ** the xOpen method must set the sqlite3_file.pMethods to either // ** a valid [sqlite3_io_methods] object or to NULL. xOpen must do // ** this even if the open fails. SQLite expects that the sqlite3_file.pMethods // ** element will be valid after xOpen returns regardless of the success // ** or failure of the xOpen call. // ** // ** [[sqlite3_vfs.xAccess]] // ** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] // ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to // ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ] // ** to test whether a file is at least readable. The SQLITE_ACCESS_READ // ** flag is never actually used and is not implemented in the built-in // ** VFSes of SQLite. The file is named by the second argument and can be a // ** directory. The xAccess method returns [SQLITE_OK] on success or some // ** non-zero error code if there is an I/O error or if the name of // ** the file given in the second argument is illegal. If SQLITE_OK // ** is returned, then non-zero or zero is written into *pResOut to indicate // ** whether or not the file is accessible. // ** // ** ^SQLite will always allocate at least mxPathname+1 bytes for the // ** output buffer xFullPathname. The exact size of the output buffer // ** is also passed as a parameter to both methods. If the output buffer // ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is // ** handled as a fatal error by SQLite, vfs implementations should endeavor // ** to prevent this by setting mxPathname to a sufficiently large value. // ** // ** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64() // ** interfaces are not strictly a part of the filesystem, but they are // ** included in the VFS structure for completeness. // ** The xRandomness() function attempts to return nBytes bytes // ** of good-quality randomness into zOut. The return value is // ** the actual number of bytes of randomness obtained. // ** The xSleep() method causes the calling thread to sleep for at // ** least the number of microseconds given. ^The xCurrentTime() // ** method returns a Julian Day Number for the current date and time as // ** a floating point value. // ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian // ** Day Number multiplied by 86400000 (the number of milliseconds in // ** a 24-hour day). // ** ^SQLite will use the xCurrentTimeInt64() method to get the current // ** date and time if that method is available (if iVersion is 2 or // ** greater and the function pointer is not NULL) and will fall back // ** to xCurrentTime() if xCurrentTimeInt64() is unavailable. // ** // ** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces // ** are not used by the SQLite core. These optional interfaces are provided // ** by some VFSes to facilitate testing of the VFS code. By overriding // ** system calls with functions under its control, a test program can // ** simulate faults and error conditions that would otherwise be difficult // ** or impossible to induce. The set of system calls that can be overridden // ** varies from one VFS to another, and from one version of the same VFS to the // ** next. Applications that use these interfaces must be prepared for any // ** or all of these interfaces to be NULL or for their behavior to change // ** from one release to the next. Applications must not attempt to access // ** any of these methods if the iVersion of the VFS is less than 3. // */ type Tsqlite3_vfs = struct { FiVersion int32 FszOsFile int32 FmxPathname int32 FpNext uintptr FzName uintptr FpAppData uintptr FxOpen uintptr FxDelete uintptr FxAccess uintptr FxFullPathname uintptr FxDlOpen uintptr FxDlError uintptr FxDlSym uintptr FxDlClose uintptr FxRandomness uintptr FxSleep uintptr FxCurrentTime uintptr FxGetLastError uintptr FxCurrentTimeInt64 uintptr FxSetSystemCall uintptr FxGetSystemCall uintptr FxNextSystemCall uintptr } type sqlite3_vfs = Tsqlite3_vfs type Tsqlite3_syscall_ptr = uintptr type sqlite3_syscall_ptr = Tsqlite3_syscall_ptr type Tsqlite3_vfs1 = struct { FiVersion int32 FszOsFile int32 FmxPathname int32 FpNext uintptr FzName uintptr FpAppData uintptr FxOpen uintptr FxDelete uintptr FxAccess uintptr FxFullPathname uintptr FxDlOpen uintptr FxDlError uintptr FxDlSym uintptr FxDlClose uintptr FxRandomness uintptr FxSleep uintptr FxCurrentTime uintptr FxGetLastError uintptr FxCurrentTimeInt64 uintptr FxSetSystemCall uintptr FxGetSystemCall uintptr FxNextSystemCall uintptr } type sqlite3_vfs1 = Tsqlite3_vfs1 // C documentation // // /* // ** CAPI3REF: Memory Allocation Routines // ** // ** An instance of this object defines the interface between SQLite // ** and low-level memory allocation routines. // ** // ** This object is used in only one place in the SQLite interface. // ** A pointer to an instance of this object is the argument to // ** [sqlite3_config()] when the configuration option is // ** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC]. // ** By creating an instance of this object // ** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC]) // ** during configuration, an application can specify an alternative // ** memory allocation subsystem for SQLite to use for all of its // ** dynamic memory needs. // ** // ** Note that SQLite comes with several [built-in memory allocators] // ** that are perfectly adequate for the overwhelming majority of applications // ** and that this object is only useful to a tiny minority of applications // ** with specialized memory allocation requirements. This object is // ** also used during testing of SQLite in order to specify an alternative // ** memory allocator that simulates memory out-of-memory conditions in // ** order to verify that SQLite recovers gracefully from such // ** conditions. // ** // ** The xMalloc, xRealloc, and xFree methods must work like the // ** malloc(), realloc() and free() functions from the standard C library. // ** ^SQLite guarantees that the second argument to // ** xRealloc is always a value returned by a prior call to xRoundup. // ** // ** xSize should return the allocated size of a memory allocation // ** previously obtained from xMalloc or xRealloc. The allocated size // ** is always at least as big as the requested size but may be larger. // ** // ** The xRoundup method returns what would be the allocated size of // ** a memory allocation given a particular requested size. Most memory // ** allocators round up memory allocations at least to the next multiple // ** of 8. Some allocators round up to a larger multiple or to a power of 2. // ** Every memory allocation request coming in through [sqlite3_malloc()] // ** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0, // ** that causes the corresponding memory allocation to fail. // ** // ** The xInit method initializes the memory allocator. For example, // ** it might allocate any required mutexes or initialize internal data // ** structures. The xShutdown method is invoked (indirectly) by // ** [sqlite3_shutdown()] and should deallocate any resources acquired // ** by xInit. The pAppData pointer is used as the only parameter to // ** xInit and xShutdown. // ** // ** SQLite holds the [SQLITE_MUTEX_STATIC_MAIN] mutex when it invokes // ** the xInit method, so the xInit method need not be threadsafe. The // ** xShutdown method is only called from [sqlite3_shutdown()] so it does // ** not need to be threadsafe either. For all other methods, SQLite // ** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the // ** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which // ** it is by default) and so the methods are automatically serialized. // ** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other // ** methods must be threadsafe or else make their own arrangements for // ** serialization. // ** // ** SQLite will never invoke xInit() more than once without an intervening // ** call to xShutdown(). // */ type Tsqlite3_mem_methods = struct { FxMalloc uintptr FxFree uintptr FxRealloc uintptr FxSize uintptr FxRoundup uintptr FxInit uintptr FxShutdown uintptr FpAppData uintptr } type sqlite3_mem_methods = Tsqlite3_mem_methods type Tsqlite3_mem_methods1 = struct { FxMalloc uintptr FxFree uintptr FxRealloc uintptr FxSize uintptr FxRoundup uintptr FxInit uintptr FxShutdown uintptr FpAppData uintptr } type sqlite3_mem_methods1 = Tsqlite3_mem_methods1 // C documentation // // /* // ** CAPI3REF: Dynamically Typed Value Object // ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value} // ** // ** SQLite uses the sqlite3_value object to represent all values // ** that can be stored in a database table. SQLite uses dynamic typing // ** for the values it stores. ^Values stored in sqlite3_value objects // ** can be integers, floating point values, strings, BLOBs, or NULL. // ** // ** An sqlite3_value object may be either "protected" or "unprotected". // ** Some interfaces require a protected sqlite3_value. Other interfaces // ** will accept either a protected or an unprotected sqlite3_value. // ** Every interface that accepts sqlite3_value arguments specifies // ** whether or not it requires a protected sqlite3_value. The // ** [sqlite3_value_dup()] interface can be used to construct a new // ** protected sqlite3_value from an unprotected sqlite3_value. // ** // ** The terms "protected" and "unprotected" refer to whether or not // ** a mutex is held. An internal mutex is held for a protected // ** sqlite3_value object but no mutex is held for an unprotected // ** sqlite3_value object. If SQLite is compiled to be single-threaded // ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0) // ** or if SQLite is run in one of reduced mutex modes // ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD] // ** then there is no distinction between protected and unprotected // ** sqlite3_value objects and they can be used interchangeably. However, // ** for maximum code portability it is recommended that applications // ** still make the distinction between protected and unprotected // ** sqlite3_value objects even when not strictly required. // ** // ** ^The sqlite3_value objects that are passed as parameters into the // ** implementation of [application-defined SQL functions] are protected. // ** ^The sqlite3_value objects returned by [sqlite3_vtab_rhs_value()] // ** are protected. // ** ^The sqlite3_value object returned by // ** [sqlite3_column_value()] is unprotected. // ** Unprotected sqlite3_value objects may only be used as arguments // ** to [sqlite3_result_value()], [sqlite3_bind_value()], and // ** [sqlite3_value_dup()]. // ** The [sqlite3_value_blob | sqlite3_value_type()] family of // ** interfaces require protected sqlite3_value objects. // */ type Tsqlite3_value = struct { F__ccgo_align [0]uint32 Fu TMemValue Fz uintptr Fn int32 Fflags Tu16 Fenc Tu8 FeSubtype Tu8 Fdb uintptr FszMalloc int32 FuTemp Tu32 FzMalloc uintptr FxDel uintptr } type sqlite3_value = Tsqlite3_value // C documentation // // /* // ** CAPI3REF: SQL Function Context Object // ** // ** The context in which an SQL function executes is stored in an // ** sqlite3_context object. ^A pointer to an sqlite3_context object // ** is always first parameter to [application-defined SQL functions]. // ** The application-defined SQL function implementation will pass this // ** pointer through into calls to [sqlite3_result_int | sqlite3_result()], // ** [sqlite3_aggregate_context()], [sqlite3_user_data()], // ** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()], // ** and/or [sqlite3_set_auxdata()]. // */ type Tsqlite3_context = struct { FpOut uintptr FpFunc uintptr FpMem uintptr FpVdbe uintptr FiOp int32 FisError int32 Fenc Tu8 FskipFlag Tu8 Fargc Tu8 Fargv [1]uintptr } type sqlite3_context = Tsqlite3_context // C documentation // // /* // ** CAPI3REF: Constants Defining Special Destructor Behavior // ** // ** These are special values for the destructor that is passed in as the // ** final argument to routines like [sqlite3_result_blob()]. ^If the destructor // ** argument is SQLITE_STATIC, it means that the content pointer is constant // ** and will never change. It does not need to be destroyed. ^The // ** SQLITE_TRANSIENT value means that the content will likely change in // ** the near future and that SQLite should make its own private copy of // ** the content before returning. // ** // ** The typedef is necessary to work around problems in certain // ** C++ compilers. // */ type Tsqlite3_destructor_type = uintptr type sqlite3_destructor_type = Tsqlite3_destructor_type // C documentation // // /* // ** Structures used by the virtual table interface // */ type Tsqlite3_vtab = struct { FpModule uintptr FnRef int32 FzErrMsg uintptr } type sqlite3_vtab = Tsqlite3_vtab type Tsqlite3_index_info = struct { F__ccgo_align [0]uint32 FnConstraint int32 FaConstraint uintptr FnOrderBy int32 FaOrderBy uintptr FaConstraintUsage uintptr FidxNum int32 FidxStr uintptr FneedToFreeIdxStr int32 ForderByConsumed int32 F__ccgo_align9 [4]byte FestimatedCost float64 FestimatedRows Tsqlite3_int64 FidxFlags int32 F__ccgo_align12 [4]byte FcolUsed Tsqlite3_uint64 } type sqlite3_index_info = Tsqlite3_index_info type Tsqlite3_vtab_cursor = struct { FpVtab uintptr } type sqlite3_vtab_cursor = Tsqlite3_vtab_cursor type Tsqlite3_module = struct { FiVersion int32 FxCreate uintptr FxConnect uintptr FxBestIndex uintptr FxDisconnect uintptr FxDestroy uintptr FxOpen uintptr FxClose uintptr FxFilter uintptr FxNext uintptr FxEof uintptr FxColumn uintptr FxRowid uintptr FxUpdate uintptr FxBegin uintptr FxSync uintptr FxCommit uintptr FxRollback uintptr FxFindFunction uintptr FxRename uintptr FxSavepoint uintptr FxRelease uintptr FxRollbackTo uintptr FxShadowName uintptr FxIntegrity uintptr } type sqlite3_module = Tsqlite3_module /* ** CAPI3REF: Virtual Table Object ** KEYWORDS: sqlite3_module {virtual table module} ** ** This structure, sometimes called a "virtual table module", ** defines the implementation of a [virtual table]. ** This structure consists mostly of methods for the module. ** ** ^A virtual table module is created by filling in a persistent ** instance of this structure and passing a pointer to that instance ** to [sqlite3_create_module()] or [sqlite3_create_module_v2()]. ** ^The registration remains valid until it is replaced by a different ** module or until the [database connection] closes. The content ** of this structure must not change while it is registered with ** any database connection. */ type Tsqlite3_module1 = struct { FiVersion int32 FxCreate uintptr FxConnect uintptr FxBestIndex uintptr FxDisconnect uintptr FxDestroy uintptr FxOpen uintptr FxClose uintptr FxFilter uintptr FxNext uintptr FxEof uintptr FxColumn uintptr FxRowid uintptr FxUpdate uintptr FxBegin uintptr FxSync uintptr FxCommit uintptr FxRollback uintptr FxFindFunction uintptr FxRename uintptr FxSavepoint uintptr FxRelease uintptr FxRollbackTo uintptr FxShadowName uintptr FxIntegrity uintptr } type sqlite3_module1 = Tsqlite3_module1 /* ** CAPI3REF: Virtual Table Indexing Information ** KEYWORDS: sqlite3_index_info ** ** The sqlite3_index_info structure and its substructures is used as part ** of the [virtual table] interface to ** pass information into and receive the reply from the [xBestIndex] ** method of a [virtual table module]. The fields under **Inputs** are the ** inputs to xBestIndex and are read-only. xBestIndex inserts its ** results into the **Outputs** fields. ** ** ^(The aConstraint[] array records WHERE clause constraints of the form: ** **
column OP expr
** ** where OP is =, <, <=, >, or >=.)^ ^(The particular operator is ** stored in aConstraint[].op using one of the ** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^ ** ^(The index of the column is stored in ** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the ** expr on the right-hand side can be evaluated (and thus the constraint ** is usable) and false if it cannot.)^ ** ** ^The optimizer automatically inverts terms of the form "expr OP column" ** and makes other simplifications to the WHERE clause in an attempt to ** get as many WHERE clause terms into the form shown above as possible. ** ^The aConstraint[] array only reports WHERE clause terms that are ** relevant to the particular virtual table being queried. ** ** ^Information about the ORDER BY clause is stored in aOrderBy[]. ** ^Each term of aOrderBy records a column of the ORDER BY clause. ** ** The colUsed field indicates which columns of the virtual table may be ** required by the current scan. Virtual table columns are numbered from ** zero in the order in which they appear within the CREATE TABLE statement ** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62), ** the corresponding bit is set within the colUsed mask if the column may be ** required by SQLite. If the table has at least 64 columns and any column ** to the right of the first 63 is required, then bit 63 of colUsed is also ** set. In other words, column iCol may be required if the expression ** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to ** non-zero. ** ** The [xBestIndex] method must fill aConstraintUsage[] with information ** about what parameters to pass to xFilter. ^If argvIndex>0 then ** the right-hand side of the corresponding aConstraint[] is evaluated ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit ** is true, then the constraint is assumed to be fully handled by the ** virtual table and might not be checked again by the byte code.)^ ^(The ** aConstraintUsage[].omit flag is an optimization hint. When the omit flag ** is left in its default setting of false, the constraint will always be ** checked separately in byte code. If the omit flag is change to true, then ** the constraint may or may not be checked in byte code. In other words, ** when the omit flag is true there is no guarantee that the constraint will ** not be checked again using byte code.)^ ** ** ^The idxNum and idxStr values are recorded and passed into the ** [xFilter] method. ** ^[sqlite3_free()] is used to free idxStr if and only if ** needToFreeIdxStr is true. ** ** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in ** the correct order to satisfy the ORDER BY clause so that no separate ** sorting step is required. ** ** ^The estimatedCost value is an estimate of the cost of a particular ** strategy. A cost of N indicates that the cost of the strategy is similar ** to a linear scan of an SQLite table with N rows. A cost of log(N) ** indicates that the expense of the operation is similar to that of a ** binary search on a unique indexed field of an SQLite table with N rows. ** ** ^The estimatedRows value is an estimate of the number of rows that ** will be returned by the strategy. ** ** The xBestIndex method may optionally populate the idxFlags field with a ** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag - ** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite ** assumes that the strategy may visit at most one row. ** ** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then ** SQLite also assumes that if a call to the xUpdate() method is made as ** part of the same statement to delete or update a virtual table row and the ** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback ** any database changes. In other words, if the xUpdate() returns ** SQLITE_CONSTRAINT, the database contents must be exactly as they were ** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not ** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by ** the xUpdate method are automatically rolled back by SQLite. ** ** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info ** structure for SQLite [version 3.8.2] ([dateof:3.8.2]). ** If a virtual table extension is ** used with an SQLite version earlier than 3.8.2, the results of attempting ** to read or write the estimatedRows field are undefined (but are likely ** to include crashing the application). The estimatedRows field should ** therefore only be used if [sqlite3_libversion_number()] returns a ** value greater than or equal to 3008002. Similarly, the idxFlags field ** was added for [version 3.9.0] ([dateof:3.9.0]). ** It may therefore only be used if ** sqlite3_libversion_number() returns a value greater than or equal to ** 3009000. */ type Tsqlite3_index_info1 = struct { F__ccgo_align [0]uint32 FnConstraint int32 FaConstraint uintptr FnOrderBy int32 FaOrderBy uintptr FaConstraintUsage uintptr FidxNum int32 FidxStr uintptr FneedToFreeIdxStr int32 ForderByConsumed int32 F__ccgo_align9 [4]byte FestimatedCost float64 FestimatedRows Tsqlite3_int64 FidxFlags int32 F__ccgo_align12 [4]byte FcolUsed Tsqlite3_uint64 } type sqlite3_index_info1 = Tsqlite3_index_info1 /* ** CAPI3REF: Virtual Table Instance Object ** KEYWORDS: sqlite3_vtab ** ** Every [virtual table module] implementation uses a subclass ** of this object to describe a particular instance ** of the [virtual table]. Each subclass will ** be tailored to the specific needs of the module implementation. ** The purpose of this superclass is to define certain fields that are ** common to all module implementations. ** ** ^Virtual tables methods can set an error message by assigning a ** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should ** take care that any prior string is freed by a call to [sqlite3_free()] ** prior to assigning a new string to zErrMsg. ^After the error message ** is delivered up to the client application, the string will be automatically ** freed by sqlite3_free() and the zErrMsg field will be zeroed. */ type Tsqlite3_vtab1 = struct { FpModule uintptr FnRef int32 FzErrMsg uintptr } type sqlite3_vtab1 = Tsqlite3_vtab1 /* ** CAPI3REF: Virtual Table Cursor Object ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor} ** ** Every [virtual table module] implementation uses a subclass of the ** following structure to describe cursors that point into the ** [virtual table] and are used ** to loop through the virtual table. Cursors are created using the ** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed ** by the [sqlite3_module.xClose | xClose] method. Cursors are used ** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods ** of the module. Each module implementation will define ** the content of a cursor structure to suit its own needs. ** ** This superclass exists in order to define fields of the cursor that ** are common to all implementations. */ type Tsqlite3_vtab_cursor1 = struct { FpVtab uintptr } type sqlite3_vtab_cursor1 = Tsqlite3_vtab_cursor1 // C documentation // // /* // ** CAPI3REF: Mutex Methods Object // ** // ** An instance of this structure defines the low-level routines // ** used to allocate and use mutexes. // ** // ** Usually, the default mutex implementations provided by SQLite are // ** sufficient, however the application has the option of substituting a custom // ** implementation for specialized deployments or systems for which SQLite // ** does not provide a suitable implementation. In this case, the application // ** creates and populates an instance of this structure to pass // ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option. // ** Additionally, an instance of this structure can be used as an // ** output variable when querying the system for the current mutex // ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. // ** // ** ^The xMutexInit method defined by this structure is invoked as // ** part of system initialization by the sqlite3_initialize() function. // ** ^The xMutexInit routine is called by SQLite exactly once for each // ** effective call to [sqlite3_initialize()]. // ** // ** ^The xMutexEnd method defined by this structure is invoked as // ** part of system shutdown by the sqlite3_shutdown() function. The // ** implementation of this method is expected to release all outstanding // ** resources obtained by the mutex methods implementation, especially // ** those obtained by the xMutexInit method. ^The xMutexEnd() // ** interface is invoked exactly once for each call to [sqlite3_shutdown()]. // ** // ** ^(The remaining seven methods defined by this structure (xMutexAlloc, // ** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and // ** xMutexNotheld) implement the following interfaces (respectively): // ** // ** )^ // ** // ** The only difference is that the public sqlite3_XXX functions enumerated // ** above silently ignore any invocations that pass a NULL pointer instead // ** of a valid mutex handle. The implementations of the methods defined // ** by this structure are not required to handle this case. The results // ** of passing a NULL pointer instead of a valid mutex handle are undefined // ** (i.e. it is acceptable to provide an implementation that segfaults if // ** it is passed a NULL pointer). // ** // ** The xMutexInit() method must be threadsafe. It must be harmless to // ** invoke xMutexInit() multiple times within the same process and without // ** intervening calls to xMutexEnd(). Second and subsequent calls to // ** xMutexInit() must be no-ops. // ** // ** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] // ** and its associates). Similarly, xMutexAlloc() must not use SQLite memory // ** allocation for a static mutex. ^However xMutexAlloc() may use SQLite // ** memory allocation for a fast or recursive mutex. // ** // ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is // ** called, but only if the prior call to xMutexInit returned SQLITE_OK. // ** If xMutexInit fails in any way, it is expected to clean up after itself // ** prior to returning. // */ type Tsqlite3_mutex_methods = struct { FxMutexInit uintptr FxMutexEnd uintptr FxMutexAlloc uintptr FxMutexFree uintptr FxMutexEnter uintptr FxMutexTry uintptr FxMutexLeave uintptr FxMutexHeld uintptr FxMutexNotheld uintptr } type sqlite3_mutex_methods = Tsqlite3_mutex_methods type Tsqlite3_mutex_methods1 = struct { FxMutexInit uintptr FxMutexEnd uintptr FxMutexAlloc uintptr FxMutexFree uintptr FxMutexEnter uintptr FxMutexTry uintptr FxMutexLeave uintptr FxMutexHeld uintptr FxMutexNotheld uintptr } type sqlite3_mutex_methods1 = Tsqlite3_mutex_methods1 // C documentation // // /* // ** CAPI3REF: Dynamic String Object // ** KEYWORDS: {dynamic string} // ** // ** An instance of the sqlite3_str object contains a dynamically-sized // ** string under construction. // ** // ** The lifecycle of an sqlite3_str object is as follows: // **
    // **
  1. ^The sqlite3_str object is created using [sqlite3_str_new()]. // **
  2. ^Text is appended to the sqlite3_str object using various // ** methods, such as [sqlite3_str_appendf()]. // **
  3. ^The sqlite3_str object is destroyed and the string it created // ** is returned using the [sqlite3_str_finish()] interface. // **
// */ type Tsqlite3_str = struct { Fdb uintptr FzText uintptr FnAlloc Tu32 FmxAlloc Tu32 FnChar Tu32 FaccError Tu8 FprintfFlags Tu8 } type sqlite3_str = Tsqlite3_str // C documentation // // /* // ** CAPI3REF: Custom Page Cache Object // ** // ** The sqlite3_pcache_page object represents a single page in the // ** page cache. The page cache will allocate instances of this // ** object. Various methods of the page cache use pointers to instances // ** of this object as parameters or as their return value. // ** // ** See [sqlite3_pcache_methods2] for additional information. // */ type Tsqlite3_pcache_page = struct { FpBuf uintptr FpExtra uintptr } type sqlite3_pcache_page = Tsqlite3_pcache_page type Tsqlite3_pcache_page1 = struct { FpBuf uintptr FpExtra uintptr } type sqlite3_pcache_page1 = Tsqlite3_pcache_page1 // C documentation // // /* // ** CAPI3REF: Application Defined Page Cache. // ** KEYWORDS: {page cache} // ** // ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can // ** register an alternative page cache implementation by passing in an // ** instance of the sqlite3_pcache_methods2 structure.)^ // ** In many applications, most of the heap memory allocated by // ** SQLite is used for the page cache. // ** By implementing a // ** custom page cache using this API, an application can better control // ** the amount of memory consumed by SQLite, the way in which // ** that memory is allocated and released, and the policies used to // ** determine exactly which parts of a database file are cached and for // ** how long. // ** // ** The alternative page cache mechanism is an // ** extreme measure that is only needed by the most demanding applications. // ** The built-in page cache is recommended for most uses. // ** // ** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an // ** internal buffer by SQLite within the call to [sqlite3_config]. Hence // ** the application may discard the parameter after the call to // ** [sqlite3_config()] returns.)^ // ** // ** [[the xInit() page cache method]] // ** ^(The xInit() method is called once for each effective // ** call to [sqlite3_initialize()])^ // ** (usually only once during the lifetime of the process). ^(The xInit() // ** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^ // ** The intent of the xInit() method is to set up global data structures // ** required by the custom page cache implementation. // ** ^(If the xInit() method is NULL, then the // ** built-in default page cache is used instead of the application defined // ** page cache.)^ // ** // ** [[the xShutdown() page cache method]] // ** ^The xShutdown() method is called by [sqlite3_shutdown()]. // ** It can be used to clean up // ** any outstanding resources before process shutdown, if required. // ** ^The xShutdown() method may be NULL. // ** // ** ^SQLite automatically serializes calls to the xInit method, // ** so the xInit method need not be threadsafe. ^The // ** xShutdown method is only called from [sqlite3_shutdown()] so it does // ** not need to be threadsafe either. All other methods must be threadsafe // ** in multithreaded applications. // ** // ** ^SQLite will never invoke xInit() more than once without an intervening // ** call to xShutdown(). // ** // ** [[the xCreate() page cache methods]] // ** ^SQLite invokes the xCreate() method to construct a new cache instance. // ** SQLite will typically create one cache instance for each open database file, // ** though this is not guaranteed. ^The // ** first parameter, szPage, is the size in bytes of the pages that must // ** be allocated by the cache. ^szPage will always a power of two. ^The // ** second parameter szExtra is a number of bytes of extra storage // ** associated with each page cache entry. ^The szExtra parameter will // ** a number less than 250. SQLite will use the // ** extra szExtra bytes on each page to store metadata about the underlying // ** database page on disk. The value passed into szExtra depends // ** on the SQLite version, the target platform, and how SQLite was compiled. // ** ^The third argument to xCreate(), bPurgeable, is true if the cache being // ** created will be used to cache database pages of a file stored on disk, or // ** false if it is used for an in-memory database. The cache implementation // ** does not have to do anything special based with the value of bPurgeable; // ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will // ** never invoke xUnpin() except to deliberately delete a page. // ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to // ** false will always have the "discard" flag set to true. // ** ^Hence, a cache created with bPurgeable false will // ** never contain any unpinned pages. // ** // ** [[the xCachesize() page cache method]] // ** ^(The xCachesize() method may be called at any time by SQLite to set the // ** suggested maximum cache-size (number of pages stored by) the cache // ** instance passed as the first argument. This is the value configured using // ** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable // ** parameter, the implementation is not required to do anything with this // ** value; it is advisory only. // ** // ** [[the xPagecount() page cache methods]] // ** The xPagecount() method must return the number of pages currently // ** stored in the cache, both pinned and unpinned. // ** // ** [[the xFetch() page cache methods]] // ** The xFetch() method locates a page in the cache and returns a pointer to // ** an sqlite3_pcache_page object associated with that page, or a NULL pointer. // ** The pBuf element of the returned sqlite3_pcache_page object will be a // ** pointer to a buffer of szPage bytes used to store the content of a // ** single database page. The pExtra element of sqlite3_pcache_page will be // ** a pointer to the szExtra bytes of extra storage that SQLite has requested // ** for each entry in the page cache. // ** // ** The page to be fetched is determined by the key. ^The minimum key value // ** is 1. After it has been retrieved using xFetch, the page is considered // ** to be "pinned". // ** // ** If the requested page is already in the page cache, then the page cache // ** implementation must return a pointer to the page buffer with its content // ** intact. If the requested page is not already in the cache, then the // ** cache implementation should use the value of the createFlag // ** parameter to help it determined what action to take: // ** // ** // **
createFlag Behavior when page is not already in cache // **
0 Do not allocate a new page. Return NULL. // **
1 Allocate a new page if it easy and convenient to do so. // ** Otherwise return NULL. // **
2 Make every effort to allocate a new page. Only return // ** NULL if allocating a new page is effectively impossible. // **
// ** // ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite // ** will only use a createFlag of 2 after a prior call with a createFlag of 1 // ** failed.)^ In between the xFetch() calls, SQLite may // ** attempt to unpin one or more cache pages by spilling the content of // ** pinned pages to disk and synching the operating system disk cache. // ** // ** [[the xUnpin() page cache method]] // ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page // ** as its second argument. If the third parameter, discard, is non-zero, // ** then the page must be evicted from the cache. // ** ^If the discard parameter is // ** zero, then the page may be discarded or retained at the discretion of // ** page cache implementation. ^The page cache implementation // ** may choose to evict unpinned pages at any time. // ** // ** The cache must not perform any reference counting. A single // ** call to xUnpin() unpins the page regardless of the number of prior calls // ** to xFetch(). // ** // ** [[the xRekey() page cache methods]] // ** The xRekey() method is used to change the key value associated with the // ** page passed as the second argument. If the cache // ** previously contains an entry associated with newKey, it must be // ** discarded. ^Any prior cache entry associated with newKey is guaranteed not // ** to be pinned. // ** // ** When SQLite calls the xTruncate() method, the cache must discard all // ** existing cache entries with page numbers (keys) greater than or equal // ** to the value of the iLimit parameter passed to xTruncate(). If any // ** of these pages are pinned, they are implicitly unpinned, meaning that // ** they can be safely discarded. // ** // ** [[the xDestroy() page cache method]] // ** ^The xDestroy() method is used to delete a cache allocated by xCreate(). // ** All resources associated with the specified cache should be freed. ^After // ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*] // ** handle invalid, and will not use it with any other sqlite3_pcache_methods2 // ** functions. // ** // ** [[the xShrink() page cache method]] // ** ^SQLite invokes the xShrink() method when it wants the page cache to // ** free up as much of heap memory as possible. The page cache implementation // ** is not obligated to free any memory, but well-behaved implementations should // ** do their best. // */ type Tsqlite3_pcache_methods2 = struct { FiVersion int32 FpArg uintptr FxInit uintptr FxShutdown uintptr FxCreate uintptr FxCachesize uintptr FxPagecount uintptr FxFetch uintptr FxUnpin uintptr FxRekey uintptr FxTruncate uintptr FxDestroy uintptr FxShrink uintptr } type sqlite3_pcache_methods2 = Tsqlite3_pcache_methods2 type Tsqlite3_pcache_methods21 = struct { FiVersion int32 FpArg uintptr FxInit uintptr FxShutdown uintptr FxCreate uintptr FxCachesize uintptr FxPagecount uintptr FxFetch uintptr FxUnpin uintptr FxRekey uintptr FxTruncate uintptr FxDestroy uintptr FxShrink uintptr } type sqlite3_pcache_methods21 = Tsqlite3_pcache_methods21 // C documentation // // /* // ** This is the obsolete pcache_methods object that has now been replaced // ** by sqlite3_pcache_methods2. This object is not used by SQLite. It is // ** retained in the header file for backwards compatibility only. // */ type Tsqlite3_pcache_methods = struct { FpArg uintptr FxInit uintptr FxShutdown uintptr FxCreate uintptr FxCachesize uintptr FxPagecount uintptr FxFetch uintptr FxUnpin uintptr FxRekey uintptr FxTruncate uintptr FxDestroy uintptr } type sqlite3_pcache_methods = Tsqlite3_pcache_methods type Tsqlite3_pcache_methods1 = struct { FpArg uintptr FxInit uintptr FxShutdown uintptr FxCreate uintptr FxCachesize uintptr FxPagecount uintptr FxFetch uintptr FxUnpin uintptr FxRekey uintptr FxTruncate uintptr FxDestroy uintptr } type sqlite3_pcache_methods1 = Tsqlite3_pcache_methods1 // C documentation // // /* // ** CAPI3REF: Online Backup Object // ** // ** The sqlite3_backup object records state information about an ongoing // ** online backup operation. ^The sqlite3_backup object is created by // ** a call to [sqlite3_backup_init()] and is destroyed by a call to // ** [sqlite3_backup_finish()]. // ** // ** See Also: [Using the SQLite Online Backup API] // */ type Tsqlite3_backup = struct { FpDestDb uintptr FpDest uintptr FiDestSchema Tu32 FbDestLocked int32 FiNext TPgno FpSrcDb uintptr FpSrc uintptr Frc int32 FnRemaining TPgno FnPagecount TPgno FisAttached int32 FpNext uintptr } type sqlite3_backup = Tsqlite3_backup // C documentation // // /* // ** CAPI3REF: Database Snapshot // ** KEYWORDS: {snapshot} {sqlite3_snapshot} // ** // ** An instance of the snapshot object records the state of a [WAL mode] // ** database for some specific point in history. // ** // ** In [WAL mode], multiple [database connections] that are open on the // ** same database file can each be reading a different historical version // ** of the database file. When a [database connection] begins a read // ** transaction, that connection sees an unchanging copy of the database // ** as it existed for the point in time when the transaction first started. // ** Subsequent changes to the database from other connections are not seen // ** by the reader until a new read transaction is started. // ** // ** The sqlite3_snapshot object records state information about an historical // ** version of the database file so that it is possible to later open a new read // ** transaction that sees that historical version of the database rather than // ** the most recent version. // */ type Tsqlite3_snapshot = struct { Fhidden [48]uint8 } type sqlite3_snapshot = Tsqlite3_snapshot /* ** CAPI3REF: Flags for sqlite3_deserialize() ** ** The following are allowed values for 6th argument (the F argument) to ** the [sqlite3_deserialize(D,S,P,N,M,F)] interface. ** ** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization ** in the P argument is held in memory obtained from [sqlite3_malloc64()] ** and that SQLite should take ownership of this memory and automatically ** free it when it has finished using it. Without this flag, the caller ** is responsible for freeing any dynamically allocated memory. ** ** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to ** grow the size of the database using calls to [sqlite3_realloc64()]. This ** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used. ** Without this flag, the deserialized database cannot increase in size beyond ** the number of bytes specified by the M parameter. ** ** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database ** should be treated as read-only. */ /* ** Undo the hack that converts floating point types to integer for ** builds on processors without floating point support. */ /******** Begin file sqlite3rtree.h *********/ /* ** 2010 August 30 ** ** 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. ** ************************************************************************* */ type Tsqlite3_rtree_geometry = struct { FpContext uintptr FnParam int32 FaParam uintptr FpUser uintptr FxDelUser uintptr } type sqlite3_rtree_geometry = Tsqlite3_rtree_geometry type Tsqlite3_rtree_query_info = struct { F__ccgo_align [0]uint32 FpContext uintptr FnParam int32 FaParam uintptr FpUser uintptr FxDelUser uintptr FaCoord uintptr FanQueue uintptr FnCoord int32 FiLevel int32 FmxLevel int32 FiRowid Tsqlite3_int64 FrParentScore Tsqlite3_rtree_dbl FeParentWithin int32 FeWithin int32 FrScore Tsqlite3_rtree_dbl FapSqlParam uintptr F__ccgo_pad16 [4]byte } type sqlite3_rtree_query_info = Tsqlite3_rtree_query_info // C documentation // // /* The double-precision datatype used by RTree depends on the // ** SQLITE_RTREE_INT_ONLY compile-time option. // */ type Tsqlite3_rtree_dbl = float64 type sqlite3_rtree_dbl = Tsqlite3_rtree_dbl /* ** A pointer to a structure of the following type is passed as the first ** argument to callbacks registered using rtree_geometry_callback(). */ type Tsqlite3_rtree_geometry1 = struct { FpContext uintptr FnParam int32 FaParam uintptr FpUser uintptr FxDelUser uintptr } type sqlite3_rtree_geometry1 = Tsqlite3_rtree_geometry1 /* ** A pointer to a structure of the following type is passed as the ** argument to scored geometry callback registered using ** sqlite3_rtree_query_callback(). ** ** Note that the first 5 fields of this structure are identical to ** sqlite3_rtree_geometry. This structure is a subclass of ** sqlite3_rtree_geometry. */ type Tsqlite3_rtree_query_info1 = struct { F__ccgo_align [0]uint32 FpContext uintptr FnParam int32 FaParam uintptr FpUser uintptr FxDelUser uintptr FaCoord uintptr FanQueue uintptr FnCoord int32 FiLevel int32 FmxLevel int32 FiRowid Tsqlite3_int64 FrParentScore Tsqlite3_rtree_dbl FeParentWithin int32 FeWithin int32 FrScore Tsqlite3_rtree_dbl FapSqlParam uintptr F__ccgo_pad16 [4]byte } type sqlite3_rtree_query_info1 = Tsqlite3_rtree_query_info1 /* ** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin. */ /******** End of sqlite3rtree.h *********/ /******** Begin file sqlite3session.h *********/ /* ** Make sure we can call this stuff from C++. */ // C documentation // // /* // ** CAPI3REF: Session Object Handle // ** // ** An instance of this object is a [session] that can be used to // ** record changes to a database. // */ type Tsqlite3_session = struct { F__ccgo_align [0]uint32 Fdb uintptr FzDb uintptr FbEnableSize int32 FbEnable int32 FbIndirect int32 FbAutoAttach int32 FbImplicitPK int32 Frc int32 FpFilterCtx uintptr FxTableFilter uintptr FnMalloc Ti64 FnMaxChangesetSize Ti64 FpZeroBlob uintptr FpNext uintptr FpTable uintptr Fhook TSessionHook } type sqlite3_session = Tsqlite3_session // C documentation // // /* // ** CAPI3REF: Changeset Iterator Handle // ** // ** An instance of this object acts as a cursor for iterating // ** over the elements of a [changeset] or [patchset]. // */ type Tsqlite3_changeset_iter = struct { Fin TSessionInput Ftblhdr TSessionBuffer FbPatchset int32 FbInvert int32 FbSkipEmpty int32 Frc int32 FpConflict uintptr FzTab uintptr FnCol int32 Fop int32 FbIndirect int32 FabPK uintptr FapValue uintptr } type sqlite3_changeset_iter = Tsqlite3_changeset_iter // C documentation // // /* // ** CAPI3REF: Changegroup Handle // ** // ** A changegroup is an object used to combine two or more // ** [changesets] or [patchsets] // */ type Tsqlite3_changegroup = struct { Frc int32 FbPatch int32 FpList uintptr Fdb uintptr FzDb uintptr } type sqlite3_changegroup = Tsqlite3_changegroup /* ** CAPI3REF: Flags for sqlite3changeset_apply_v2 ** ** The following flags may passed via the 9th parameter to ** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]: ** **
**
SQLITE_CHANGESETAPPLY_NOSAVEPOINT
** Usually, the sessions module encloses all operations performed by ** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The ** SAVEPOINT is committed if the changeset or patchset is successfully ** applied, or rolled back if an error occurs. Specifying this flag ** causes the sessions module to omit this savepoint. In this case, if the ** caller has an open transaction or savepoint when apply_v2() is called, ** it may revert the partially applied changeset by rolling it back. ** **
SQLITE_CHANGESETAPPLY_INVERT
** Invert the changeset before applying it. This is equivalent to inverting ** a changeset using sqlite3changeset_invert() before applying it. It is ** an error to specify this flag with a patchset. ** **
SQLITE_CHANGESETAPPLY_IGNORENOOP
** Do not invoke the conflict handler callback for any changes that ** would not actually modify the database even if they were applied. ** Specifically, this means that the conflict handler is not invoked ** for: ** ** **
SQLITE_CHANGESETAPPLY_FKNOACTION
** If this flag it set, then all foreign key constraints in the target ** database behave as if they were declared with "ON UPDATE NO ACTION ON ** DELETE NO ACTION", even if they are actually CASCADE, RESTRICT, SET NULL ** or SET DEFAULT. */ /* ** CAPI3REF: Constants Passed To The Conflict Handler ** ** Values that may be passed as the second argument to a conflict-handler. ** **
**
SQLITE_CHANGESET_DATA
** The conflict handler is invoked with CHANGESET_DATA as the second argument ** when processing a DELETE or UPDATE change if a row with the required ** PRIMARY KEY fields is present in the database, but one or more other ** (non primary-key) fields modified by the update do not contain the ** expected "before" values. ** ** The conflicting row, in this case, is the database row with the matching ** primary key. ** **
SQLITE_CHANGESET_NOTFOUND
** The conflict handler is invoked with CHANGESET_NOTFOUND as the second ** argument when processing a DELETE or UPDATE change if a row with the ** required PRIMARY KEY fields is not present in the database. ** ** There is no conflicting row in this case. The results of invoking the ** sqlite3changeset_conflict() API are undefined. ** **
SQLITE_CHANGESET_CONFLICT
** CHANGESET_CONFLICT is passed as the second argument to the conflict ** handler while processing an INSERT change if the operation would result ** in duplicate primary key values. ** ** The conflicting row in this case is the database row with the matching ** primary key. ** **
SQLITE_CHANGESET_FOREIGN_KEY
** If foreign key handling is enabled, and applying a changeset leaves the ** database in a state containing foreign key violations, the conflict ** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument ** exactly once before the changeset is committed. If the conflict handler ** returns CHANGESET_OMIT, the changes, including those that caused the ** foreign key constraint violation, are committed. Or, if it returns ** CHANGESET_ABORT, the changeset is rolled back. ** ** No current or conflicting row information is provided. The only function ** it is possible to call on the supplied sqlite3_changeset_iter handle ** is sqlite3changeset_fk_conflicts(). ** **
SQLITE_CHANGESET_CONSTRAINT
** If any other constraint violation occurs while applying a change (i.e. ** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is ** invoked with CHANGESET_CONSTRAINT as the second argument. ** ** There is no conflicting row in this case. The results of invoking the ** sqlite3changeset_conflict() API are undefined. ** **
*/ /* ** CAPI3REF: Constants Returned By The Conflict Handler ** ** A conflict handler callback must return one of the following three values. ** **
**
SQLITE_CHANGESET_OMIT
** If a conflict handler returns this value no special action is taken. The ** change that caused the conflict is not applied. The session module ** continues to the next change in the changeset. ** **
SQLITE_CHANGESET_REPLACE
** This value may only be returned if the second argument to the conflict ** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this ** is not the case, any changes applied so far are rolled back and the ** call to sqlite3changeset_apply() returns SQLITE_MISUSE. ** ** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict ** handler, then the conflicting row is either updated or deleted, depending ** on the type of change. ** ** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict ** handler, then the conflicting row is removed from the database and a ** second attempt to apply the change is made. If this second attempt fails, ** the original row is restored to the database before continuing. ** **
SQLITE_CHANGESET_ABORT
** If this value is returned, any changes applied so far are rolled back ** and the call to sqlite3changeset_apply() returns SQLITE_ABORT. **
*/ // C documentation // // /* // ** CAPI3REF: Rebasing changesets // ** EXPERIMENTAL // ** // ** Suppose there is a site hosting a database in state S0. And that // ** modifications are made that move that database to state S1 and a // ** changeset recorded (the "local" changeset). Then, a changeset based // ** on S0 is received from another site (the "remote" changeset) and // ** applied to the database. The database is then in state // ** (S1+"remote"), where the exact state depends on any conflict // ** resolution decisions (OMIT or REPLACE) made while applying "remote". // ** Rebasing a changeset is to update it to take those conflict // ** resolution decisions into account, so that the same conflicts // ** do not have to be resolved elsewhere in the network. // ** // ** For example, if both the local and remote changesets contain an // ** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)": // ** // ** local: INSERT INTO t1 VALUES(1, 'v1'); // ** remote: INSERT INTO t1 VALUES(1, 'v2'); // ** // ** and the conflict resolution is REPLACE, then the INSERT change is // ** removed from the local changeset (it was overridden). Or, if the // ** conflict resolution was "OMIT", then the local changeset is modified // ** to instead contain: // ** // ** UPDATE t1 SET b = 'v2' WHERE a=1; // ** // ** Changes within the local changeset are rebased as follows: // ** // **
// **
Local INSERT
// ** This may only conflict with a remote INSERT. If the conflict // ** resolution was OMIT, then add an UPDATE change to the rebased // ** changeset. Or, if the conflict resolution was REPLACE, add // ** nothing to the rebased changeset. // ** // **
Local DELETE
// ** This may conflict with a remote UPDATE or DELETE. In both cases the // ** only possible resolution is OMIT. If the remote operation was a // ** DELETE, then add no change to the rebased changeset. If the remote // ** operation was an UPDATE, then the old.* fields of change are updated // ** to reflect the new.* values in the UPDATE. // ** // **
Local UPDATE
// ** This may conflict with a remote UPDATE or DELETE. If it conflicts // ** with a DELETE, and the conflict resolution was OMIT, then the update // ** is changed into an INSERT. Any undefined values in the new.* record // ** from the update change are filled in using the old.* values from // ** the conflicting DELETE. Or, if the conflict resolution was REPLACE, // ** the UPDATE change is simply omitted from the rebased changeset. // ** // ** If conflict is with a remote UPDATE and the resolution is OMIT, then // ** the old.* values are rebased using the new.* values in the remote // ** change. Or, if the resolution is REPLACE, then the change is copied // ** into the rebased changeset with updates to columns also updated by // ** the conflicting remote UPDATE removed. If this means no columns would // ** be updated, the change is omitted. // **
// ** // ** A local change may be rebased against multiple remote changes // ** simultaneously. If a single key is modified by multiple remote // ** changesets, they are combined as follows before the local changeset // ** is rebased: // ** // ** // ** // ** Note that conflict resolutions from multiple remote changesets are // ** combined on a per-field basis, not per-row. This means that in the // ** case of multiple remote UPDATE operations, some fields of a single // ** local change may be rebased for REPLACE while others are rebased for // ** OMIT. // ** // ** In order to rebase a local changeset, the remote changeset must first // ** be applied to the local database using sqlite3changeset_apply_v2() and // ** the buffer of rebase information captured. Then: // ** // **
    // **
  1. An sqlite3_rebaser object is created by calling // ** sqlite3rebaser_create(). // **
  2. The new object is configured with the rebase buffer obtained from // ** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure(). // ** If the local changeset is to be rebased against multiple remote // ** changesets, then sqlite3rebaser_configure() should be called // ** multiple times, in the same order that the multiple // ** sqlite3changeset_apply_v2() calls were made. // **
  3. Each local changeset is rebased by calling sqlite3rebaser_rebase(). // **
  4. The sqlite3_rebaser object is deleted by calling // ** sqlite3rebaser_delete(). // **
// */ type Tsqlite3_rebaser = struct { Fgrp Tsqlite3_changegroup } type sqlite3_rebaser = Tsqlite3_rebaser /* ** CAPI3REF: Values for sqlite3session_config(). */ /* ** Make sure we can call this stuff from C++. */ /******** End of sqlite3session.h *********/ /******** Begin file fts5.h *********/ /* ** 2014 May 31 ** ** 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. ** ****************************************************************************** ** ** Interfaces to extend FTS5. Using the interfaces defined in this file, ** FTS5 may be extended with: ** ** * custom tokenizers, and ** * custom auxiliary functions. */ /************************************************************************* ** CUSTOM AUXILIARY FUNCTIONS ** ** Virtual table implementations may overload SQL functions by implementing ** the sqlite3_module.xFindFunction() method. */ type TFts5ExtensionApi = struct { FiVersion int32 FxUserData uintptr FxColumnCount uintptr FxRowCount uintptr FxColumnTotalSize uintptr FxTokenize uintptr FxPhraseCount uintptr FxPhraseSize uintptr FxInstCount uintptr FxInst uintptr FxRowid uintptr FxColumnText uintptr FxColumnSize uintptr FxQueryPhrase uintptr FxSetAuxdata uintptr FxGetAuxdata uintptr FxPhraseFirst uintptr FxPhraseNext uintptr FxPhraseFirstColumn uintptr FxPhraseNextColumn uintptr FxQueryToken uintptr FxInstToken uintptr } type Fts5ExtensionApi = TFts5ExtensionApi type TFts5PhraseIter = struct { Fa uintptr Fb uintptr } type Fts5PhraseIter = TFts5PhraseIter type Tfts5_extension_function = uintptr type fts5_extension_function = Tfts5_extension_function type TFts5PhraseIter1 = struct { Fa uintptr Fb uintptr } type Fts5PhraseIter1 = TFts5PhraseIter1 /* ** EXTENSION API FUNCTIONS ** ** xUserData(pFts): ** Return a copy of the context pointer the extension function was ** registered with. ** ** xColumnTotalSize(pFts, iCol, pnToken): ** If parameter iCol is less than zero, set output variable *pnToken ** to the total number of tokens in the FTS5 table. Or, if iCol is ** non-negative but less than the number of columns in the table, return ** the total number of tokens in column iCol, considering all rows in ** the FTS5 table. ** ** If parameter iCol is greater than or equal to the number of columns ** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g. ** an OOM condition or IO error), an appropriate SQLite error code is ** returned. ** ** xColumnCount(pFts): ** Return the number of columns in the table. ** ** xColumnSize(pFts, iCol, pnToken): ** If parameter iCol is less than zero, set output variable *pnToken ** to the total number of tokens in the current row. Or, if iCol is ** non-negative but less than the number of columns in the table, set ** *pnToken to the number of tokens in column iCol of the current row. ** ** If parameter iCol is greater than or equal to the number of columns ** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g. ** an OOM condition or IO error), an appropriate SQLite error code is ** returned. ** ** This function may be quite inefficient if used with an FTS5 table ** created with the "columnsize=0" option. ** ** xColumnText: ** If parameter iCol is less than zero, or greater than or equal to the ** number of columns in the table, SQLITE_RANGE is returned. ** ** Otherwise, this function attempts to retrieve the text of column iCol of ** the current document. If successful, (*pz) is set to point to a buffer ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes ** (not characters) of the buffer and SQLITE_OK is returned. Otherwise, ** if an error occurs, an SQLite error code is returned and the final values ** of (*pz) and (*pn) are undefined. ** ** xPhraseCount: ** Returns the number of phrases in the current query expression. ** ** xPhraseSize: ** If parameter iCol is less than zero, or greater than or equal to the ** number of phrases in the current query, as returned by xPhraseCount, ** 0 is returned. Otherwise, this function returns the number of tokens in ** phrase iPhrase of the query. Phrases are numbered starting from zero. ** ** xInstCount: ** Set *pnInst to the total number of occurrences of all phrases within ** the query within the current row. Return SQLITE_OK if successful, or ** an error code (i.e. SQLITE_NOMEM) if an error occurs. ** ** This API can be quite slow if used with an FTS5 table created with the ** "detail=none" or "detail=column" option. If the FTS5 table is created ** with either "detail=none" or "detail=column" and "content=" option ** (i.e. if it is a contentless table), then this API always returns 0. ** ** xInst: ** Query for the details of phrase match iIdx within the current row. ** Phrase matches are numbered starting from zero, so the iIdx argument ** should be greater than or equal to zero and smaller than the value ** output by xInstCount(). If iIdx is less than zero or greater than ** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned. ** ** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol ** to the column in which it occurs and *piOff the token offset of the ** first token of the phrase. SQLITE_OK is returned if successful, or an ** error code (i.e. SQLITE_NOMEM) if an error occurs. ** ** This API can be quite slow if used with an FTS5 table created with the ** "detail=none" or "detail=column" option. ** ** xRowid: ** Returns the rowid of the current row. ** ** xTokenize: ** Tokenize text using the tokenizer belonging to the FTS5 table. ** ** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback): ** This API function is used to query the FTS table for phrase iPhrase ** of the current query. Specifically, a query equivalent to: ** ** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid ** ** with $p set to a phrase equivalent to the phrase iPhrase of the ** current query is executed. Any column filter that applies to ** phrase iPhrase of the current query is included in $p. For each ** row visited, the callback function passed as the fourth argument ** is invoked. The context and API objects passed to the callback ** function may be used to access the properties of each matched row. ** Invoking Api.xUserData() returns a copy of the pointer passed as ** the third argument to pUserData. ** ** If parameter iPhrase is less than zero, or greater than or equal to ** the number of phrases in the query, as returned by xPhraseCount(), ** this function returns SQLITE_RANGE. ** ** If the callback function returns any value other than SQLITE_OK, the ** query is abandoned and the xQueryPhrase function returns immediately. ** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK. ** Otherwise, the error code is propagated upwards. ** ** If the query runs to completion without incident, SQLITE_OK is returned. ** Or, if some error occurs before the query completes or is aborted by ** the callback, an SQLite error code is returned. ** ** ** xSetAuxdata(pFts5, pAux, xDelete) ** ** Save the pointer passed as the second argument as the extension function's ** "auxiliary data". The pointer may then be retrieved by the current or any ** future invocation of the same fts5 extension function made as part of ** the same MATCH query using the xGetAuxdata() API. ** ** Each extension function is allocated a single auxiliary data slot for ** each FTS query (MATCH expression). If the extension function is invoked ** more than once for a single FTS query, then all invocations share a ** single auxiliary data context. ** ** If there is already an auxiliary data pointer when this function is ** invoked, then it is replaced by the new pointer. If an xDelete callback ** was specified along with the original pointer, it is invoked at this ** point. ** ** The xDelete callback, if one is specified, is also invoked on the ** auxiliary data pointer after the FTS5 query has finished. ** ** If an error (e.g. an OOM condition) occurs within this function, ** the auxiliary data is set to NULL and an error code returned. If the ** xDelete parameter was not NULL, it is invoked on the auxiliary data ** pointer before returning. ** ** ** xGetAuxdata(pFts5, bClear) ** ** Returns the current auxiliary data pointer for the fts5 extension ** function. See the xSetAuxdata() method for details. ** ** If the bClear argument is non-zero, then the auxiliary data is cleared ** (set to NULL) before this function returns. In this case the xDelete, ** if any, is not invoked. ** ** ** xRowCount(pFts5, pnRow) ** ** This function is used to retrieve the total number of rows in the table. ** In other words, the same value that would be returned by: ** ** SELECT count(*) FROM ftstable; ** ** xPhraseFirst() ** This function is used, along with type Fts5PhraseIter and the xPhraseNext ** method, to iterate through all instances of a single query phrase within ** the current row. This is the same information as is accessible via the ** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient ** to use, this API may be faster under some circumstances. To iterate ** through instances of phrase iPhrase, use the following code: ** ** Fts5PhraseIter iter; ** int iCol, iOff; ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff); ** iCol>=0; ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff) ** ){ ** // An instance of phrase iPhrase at offset iOff of column iCol ** } ** ** The Fts5PhraseIter structure is defined above. Applications should not ** modify this structure directly - it should only be used as shown above ** with the xPhraseFirst() and xPhraseNext() API methods (and by ** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below). ** ** This API can be quite slow if used with an FTS5 table created with the ** "detail=none" or "detail=column" option. If the FTS5 table is created ** with either "detail=none" or "detail=column" and "content=" option ** (i.e. if it is a contentless table), then this API always iterates ** through an empty set (all calls to xPhraseFirst() set iCol to -1). ** ** xPhraseNext() ** See xPhraseFirst above. ** ** xPhraseFirstColumn() ** This function and xPhraseNextColumn() are similar to the xPhraseFirst() ** and xPhraseNext() APIs described above. The difference is that instead ** of iterating through all instances of a phrase in the current row, these ** APIs are used to iterate through the set of columns in the current row ** that contain one or more instances of a specified phrase. For example: ** ** Fts5PhraseIter iter; ** int iCol; ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol); ** iCol>=0; ** pApi->xPhraseNextColumn(pFts, &iter, &iCol) ** ){ ** // Column iCol contains at least one instance of phrase iPhrase ** } ** ** This API can be quite slow if used with an FTS5 table created with the ** "detail=none" option. If the FTS5 table is created with either ** "detail=none" "content=" option (i.e. if it is a contentless table), ** then this API always iterates through an empty set (all calls to ** xPhraseFirstColumn() set iCol to -1). ** ** The information accessed using this API and its companion ** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext ** (or xInst/xInstCount). The chief advantage of this API is that it is ** significantly more efficient than those alternatives when used with ** "detail=column" tables. ** ** xPhraseNextColumn() ** See xPhraseFirstColumn above. ** ** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken) ** This is used to access token iToken of phrase iPhrase of the current ** query. Before returning, output parameter *ppToken is set to point ** to a buffer containing the requested token, and *pnToken to the ** size of this buffer in bytes. ** ** If iPhrase or iToken are less than zero, or if iPhrase is greater than ** or equal to the number of phrases in the query as reported by ** xPhraseCount(), or if iToken is equal to or greater than the number of ** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken are both zeroed. ** ** The output text is not a copy of the query text that specified the ** token. It is the output of the tokenizer module. For tokendata=1 ** tables, this includes any embedded 0x00 and trailing data. ** ** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken) ** This is used to access token iToken of phrase hit iIdx within the ** current row. If iIdx is less than zero or greater than or equal to the ** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise, ** output variable (*ppToken) is set to point to a buffer containing the ** matching document token, and (*pnToken) to the size of that buffer in ** bytes. This API is not available if the specified token matches a ** prefix query term. In that case both output variables are always set ** to 0. ** ** The output text is not a copy of the document text that was tokenized. ** It is the output of the tokenizer module. For tokendata=1 tables, this ** includes any embedded 0x00 and trailing data. ** ** This API can be quite slow if used with an FTS5 table created with the ** "detail=none" or "detail=column" option. */ type TFts5ExtensionApi1 = struct { FiVersion int32 FxUserData uintptr FxColumnCount uintptr FxRowCount uintptr FxColumnTotalSize uintptr FxTokenize uintptr FxPhraseCount uintptr FxPhraseSize uintptr FxInstCount uintptr FxInst uintptr FxRowid uintptr FxColumnText uintptr FxColumnSize uintptr FxQueryPhrase uintptr FxSetAuxdata uintptr FxGetAuxdata uintptr FxPhraseFirst uintptr FxPhraseNext uintptr FxPhraseFirstColumn uintptr FxPhraseNextColumn uintptr FxQueryToken uintptr FxInstToken uintptr } type Fts5ExtensionApi1 = TFts5ExtensionApi1 type Tfts5_tokenizer = struct { FxCreate uintptr FxDelete uintptr FxTokenize uintptr } type fts5_tokenizer = Tfts5_tokenizer type Tfts5_tokenizer1 = struct { FxCreate uintptr FxDelete uintptr FxTokenize uintptr } type fts5_tokenizer1 = Tfts5_tokenizer1 /* Flags that may be passed as the third argument to xTokenize() */ /* Flags that may be passed by the tokenizer implementation back to FTS5 ** as the third argument to the supplied xToken callback. */ /* ** END OF CUSTOM TOKENIZERS *************************************************************************/ // C documentation // // /************************************************************************* // ** FTS5 EXTENSION REGISTRATION API // */ type Tfts5_api = struct { FiVersion int32 FxCreateTokenizer uintptr FxFindTokenizer uintptr FxCreateFunction uintptr } type fts5_api = Tfts5_api type Tfts5_api1 = struct { FiVersion int32 FxCreateTokenizer uintptr FxFindTokenizer uintptr FxCreateFunction uintptr } type fts5_api1 = Tfts5_api1 /* ** END OF REGISTRATION API *************************************************************************/ /******** End of fts5.h *********/ /************** End of sqlite3.h *********************************************/ /************** Continuing where we left off in sqliteInt.h ******************/ /* ** Reuse the STATIC_LRU for mutex access to sqlite3_temp_directory. */ /* ** Include the configuration header output by 'configure' if we're using the ** autoconf-based build */ /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/ /************** Begin file sqliteLimit.h *************************************/ /* ** 2007 May 7 ** ** 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 defines various limits of what SQLite can process. */ /* ** The maximum length of a TEXT or BLOB in bytes. This also ** limits the size of a row in a table or index. ** ** The hard limit is the ability of a 32-bit signed integer ** to count the size: 2^31-1 or 2147483647. */ /* ** This is the maximum number of ** ** * Columns in a table ** * Columns in an index ** * Columns in a view ** * Terms in the SET clause of an UPDATE statement ** * Terms in the result set of a SELECT statement ** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement. ** * Terms in the VALUES clause of an INSERT statement ** ** The hard upper limit here is 32676. Most database people will ** tell you that in a well-normalized database, you usually should ** not have more than a dozen or so columns in any table. And if ** that is the case, there is no point in having more than a few ** dozen values in any of the other situations described above. */ /* ** The maximum length of a single SQL statement in bytes. ** ** It used to be the case that setting this value to zero would ** turn the limit off. That is no longer true. It is not possible ** to turn this limit off. */ /* ** The maximum depth of an expression tree. This is limited to ** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might ** want to place more severe limits on the complexity of an ** expression. A value of 0 means that there is no limit. */ /* ** The maximum number of terms in a compound SELECT statement. ** The code generator for compound SELECT statements does one ** level of recursion for each term. A stack overflow can result ** if the number of terms is too large. In practice, most SQL ** never has more than 3 or 4 terms. Use a value of 0 to disable ** any limit on the number of terms in a compound SELECT. */ /* ** The maximum number of opcodes in a VDBE program. ** Not currently enforced. */ /* ** The maximum number of arguments to an SQL function. */ /* ** The suggested maximum number of in-memory pages to use for ** the main database table and for temporary tables. ** ** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000, ** which means the cache size is limited to 2048000 bytes of memory. ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options. */ /* ** The default number of frames to accumulate in the log file before ** checkpointing the database in WAL mode. */ /* ** The maximum number of attached databases. This must be between 0 ** and 125. The upper bound of 125 is because the attached databases are ** counted using a signed 8-bit integer which has a maximum value of 127 ** and we have to allow 2 extra counts for the "main" and "temp" databases. */ /* ** The maximum value of a ?nnn wildcard that the parser will accept. ** If the value exceeds 32767 then extra space is required for the Expr ** structure. But otherwise, we believe that the number can be as large ** as a signed 32-bit integer can hold. */ /* Maximum page size. The upper bound on this value is 65536. This a limit ** imposed by the use of 16-bit offsets within each page. ** ** Earlier versions of SQLite allowed the user to change this value at ** compile time. This is no longer permitted, on the grounds that it creates ** a library that is technically incompatible with an SQLite library ** compiled with a different limit. If a process operating on a database ** with a page-size of 65536 bytes crashes, then an instance of SQLite ** compiled with the default page-size limit will not be able to rollback ** the aborted transaction. This could lead to database corruption. */ /* ** The default size of a database page. */ /* ** Ordinarily, if no value is explicitly provided, SQLite creates databases ** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain ** device characteristics (sector-size and atomic write() support), ** SQLite may choose a larger value. This constant is the maximum value ** SQLite will choose on its own. */ /* ** Maximum number of pages in one database file. ** ** This is really just the default value for the max_page_count pragma. ** This value can be lowered (or raised) at run-time using that the ** max_page_count macro. */ /* ** Maximum length (in bytes) of the pattern in a LIKE or GLOB ** operator. */ /* ** Maximum depth of recursion for triggers. ** ** A value of 1 means that a trigger program will not be able to itself ** fire any triggers. A value of 0 means that no trigger programs at all ** may be executed. */ /************** End of sqliteLimit.h *****************************************/ /************** Continuing where we left off in sqliteInt.h ******************/ /* Disable nuisance warnings on Borland compilers */ /* ** A few places in the code require atomic load/store of aligned ** integer values. */ /* ** Include standard header files as necessary */ /* ** The following macros are used to cast pointers to integers and ** integers to pointers. The way you do this varies from one compiler ** to the next, so we have developed the following set of #if statements ** to generate appropriate macros for a wide range of compilers. ** ** The correct "ANSI" way to do this is to use the intptr_t type. ** Unfortunately, that typedef is not available on all compilers, or ** if it is available, it requires an #include of specific headers ** that vary from one machine to the next. ** ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on ** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)). ** So we have to define the macros in different ways depending on the ** compiler. */ /* ** Macros to hint to the compiler that a function should or should not be ** inlined. */ /* ** Make sure that the compiler intrinsics we desire are enabled when ** compiling with an appropriate version of MSVC unless prevented by ** the SQLITE_DISABLE_INTRINSIC define. */ /* ** Enable SQLITE_USE_SEH by default on MSVC builds. Only omit ** SEH support if the -DSQLITE_OMIT_SEH option is given. */ /* ** Enable SQLITE_DIRECT_OVERFLOW_READ, unless the build explicitly ** disables it using -DSQLITE_DIRECT_OVERFLOW_READ=0 */ /* In all other cases, enable */ /* ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2. ** 0 means mutexes are permanently disable and the library is never ** threadsafe. 1 means the library is serialized which is the highest ** level of threadsafety. 2 means the library is multithreaded - multiple ** threads can use SQLite as long as no two threads try to use the same ** database connection at the same time. ** ** Older versions of SQLite used an optional THREADSAFE macro. ** We support that for legacy. ** ** To ensure that the correct value of "THREADSAFE" is reported when querying ** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this ** logic is partially replicated in ctime.c. If it is updated here, it should ** also be updated there. */ /* ** Powersafe overwrite is on by default. But can be turned off using ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option. */ /* ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by ** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in ** which case memory allocation statistics are disabled by default. */ /* ** Exactly one of the following macros must be defined in order to ** specify which memory allocation subsystem to use. ** ** SQLITE_SYSTEM_MALLOC // Use normal system malloc() ** SQLITE_WIN32_MALLOC // Use Win32 native heap API ** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails ** SQLITE_MEMDEBUG // Debugging version of system malloc() ** ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the ** assert() macro is enabled, each call into the Win32 native heap subsystem ** will cause HeapValidate to be called. If heap validation should fail, an ** assertion will be triggered. ** ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as ** the default. */ /* ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the ** sizes of memory allocations below this value where possible. */ /* ** We need to define _XOPEN_SOURCE as follows in order to enable ** recursive mutexes on most Unix systems and fchmod() on OpenBSD. ** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit ** it. */ /* ** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that ** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true, ** make it true by defining or undefining NDEBUG. ** ** Setting NDEBUG makes the code smaller and faster by disabling the ** assert() statements in the code. So we want the default action ** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out ** feature. */ /* ** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on. */ /* ** The testcase() macro is used to aid in coverage testing. When ** doing coverage testing, the condition inside the argument to ** testcase() must be evaluated both true and false in order to ** get full branch coverage. The testcase() macro is inserted ** to help ensure adequate test coverage in places where simple ** condition/decision coverage is inadequate. For example, testcase() ** can be used to make sure boundary values are tested. For ** bitmask tests, testcase() can be used to make sure each bit ** is significant and used at least once. On switch statements ** where multiple cases go to the same block of code, testcase() ** can insure that all cases are evaluated. */ /* ** The TESTONLY macro is used to enclose variable declarations or ** other bits of code that are needed to support the arguments ** within testcase() and assert() macros. */ /* ** Sometimes we need a small amount of code such as a variable initialization ** to setup for a later assert() statement. We do not want this code to ** appear when assert() is disabled. The following macro is therefore ** used to contain that setup code. The "VVA" acronym stands for ** "Verification, Validation, and Accreditation". In other words, the ** code within VVA_ONLY() will only run during verification processes. */ /* ** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage ** and mutation testing */ /* ** The ALWAYS and NEVER macros surround boolean expressions which ** are intended to always be true or false, respectively. Such ** expressions could be omitted from the code completely. But they ** are included in a few cases in order to enhance the resilience ** of SQLite to unexpected behavior - to make the code "self-healing" ** or "ductile" rather than being "brittle" and crashing at the first ** hint of unplanned behavior. ** ** In other words, ALWAYS and NEVER are added for defensive code. ** ** When doing coverage testing ALWAYS and NEVER are hard-coded to ** be true and false so that the unreachable code they specify will ** not be counted as untested code. */ /* ** Some conditionals are optimizations only. In other words, if the ** conditionals are replaced with a constant 1 (true) or 0 (false) then ** the correct answer is still obtained, though perhaps not as quickly. ** ** The following macros mark these optimizations conditionals. */ /* ** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is ** defined. We need to defend against those failures when testing with ** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches ** during a normal build. The following macro can be used to disable tests ** that are always false except when SQLITE_TEST_REALLOC_STRESS is set. */ /* ** Declarations used for tracing the operating system interfaces. */ /* ** Is the sqlite3ErrName() function needed in the build? Currently, ** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when ** OSTRACE is enabled), and by several "test*.c" files (which are ** compiled using SQLITE_TEST). */ /* ** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN */ /* ** SQLITE_OMIT_VIRTUALTABLE implies SQLITE_OMIT_ALTERTABLE */ /* ** Return true (non-zero) if the input is an integer that is too large ** to fit in 32-bits. This macro is used inside of various testcase() ** macros to verify that we have tested SQLite for large-file support. */ /* ** The macro unlikely() is a hint that surrounds a boolean ** expression that is usually false. Macro likely() surrounds ** a boolean expression that is usually true. These hints could, ** in theory, be used by the compiler to generate better code, but ** currently they are just comments for human readers. */ /************** Include hash.h in the middle of sqliteInt.h ******************/ /************** Begin file hash.h ********************************************/ /* ** 2001 September 22 ** ** 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 is the header file for the generic hash-table implementation ** used in SQLite. */ // C documentation // // /* Forward declarations of structures. */ type THash = struct { Fhtsize uint32 Fcount uint32 Ffirst uintptr Fht uintptr } type Hash = THash type THashElem = struct { Fnext uintptr Fprev uintptr Fdata uintptr FpKey uintptr } type HashElem = THashElem /* A complete hash table is an instance of the following structure. ** The internals of this structure are intended to be opaque -- client ** code should not attempt to access or modify the fields of this structure ** directly. Change this structure only by using the routines below. ** However, some of the "procedures" and "functions" for modifying and ** accessing this structure are really macros, so we can't really make ** this structure opaque. ** ** All elements of the hash table are on a single doubly-linked list. ** Hash.first points to the head of this list. ** ** There are Hash.htsize buckets. Each bucket points to a spot in ** the global doubly-linked list. The contents of the bucket are the ** element pointed to plus the next _ht.count-1 elements in the list. ** ** Hash.htsize and Hash.ht may be zero. In that case lookup is done ** by a linear search of the global list. For small tables, the ** Hash.ht table is never allocated because if there are few elements ** in the table, it is faster to do a linear search than to manage ** the hash table. */ type THash1 = struct { Fhtsize uint32 Fcount uint32 Ffirst uintptr Fht uintptr } type Hash1 = THash1 /* Each element in the hash table is an instance of the following ** structure. All elements are stored on a single doubly-linked list. ** ** Again, this structure is intended to be opaque, but it can't really ** be opaque because it is used by macros. */ type THashElem1 = struct { Fnext uintptr Fprev uintptr Fdata uintptr FpKey uintptr } type HashElem1 = THashElem1 type Tsize_t = uint32 type size_t = Tsize_t type t__u_char = uint8 type t__u_short = uint16 type t__u_int = uint32 type t__u_long = uint32 type t__int8_t = int8 type t__uint8_t = uint8 type t__int16_t = int16 type t__uint16_t = uint16 type t__int32_t = int32 type t__uint32_t = uint32 type t__int64_t = int64 type t__uint64_t = uint64 type t__int_least8_t = int8 type t__uint_least8_t = uint8 type t__int_least16_t = int16 type t__uint_least16_t = uint16 type t__int_least32_t = int32 type t__uint_least32_t = uint32 type t__int_least64_t = int64 type t__uint_least64_t = uint64 type t__quad_t = int64 type t__u_quad_t = uint64 type t__intmax_t = int64 type t__uintmax_t = uint64 type t__dev_t = uint64 type t__uid_t = uint32 type t__gid_t = uint32 type t__ino_t = uint32 type t__ino64_t = uint64 type t__mode_t = uint32 type t__nlink_t = uint32 type t__off_t = int32 type t__off64_t = int64 type t__pid_t = int32 type t__fsid_t = struct { F__val [2]int32 } type t__clock_t = int32 type t__rlim_t = uint32 type t__rlim64_t = uint64 type t__id_t = uint32 type t__time_t = int32 type t__useconds_t = uint32 type t__suseconds_t = int32 type t__suseconds64_t = int64 type t__daddr_t = int32 type t__key_t = int32 type t__clockid_t = int32 type t__timer_t = uintptr type t__blksize_t = int32 type t__blkcnt_t = int32 type t__blkcnt64_t = int64 type t__fsblkcnt_t = uint32 type t__fsblkcnt64_t = uint64 type t__fsfilcnt_t = uint32 type t__fsfilcnt64_t = uint64 type t__fsword_t = int32 type t__ssize_t = int32 type t__syscall_slong_t = int32 type t__syscall_ulong_t = uint32 type t__loff_t = int64 type t__caddr_t = uintptr type t__intptr_t = int32 type t__socklen_t = uint32 type t__sig_atomic_t = int32 type t__time64_t = int64 type t__mbstate_t = struct { F__count int32 F__value struct { F__wchb [0][4]uint8 F__wch uint32 } } type t__fpos_t = struct { F__pos t__off_t F__state t__mbstate_t } type T_G_fpos_t = t__fpos_t type _G_fpos_t = T_G_fpos_t type t__fpos64_t = struct { F__ccgo_align [0]uint32 F__pos t__off64_t F__state t__mbstate_t } type T_G_fpos64_t = t__fpos64_t type _G_fpos64_t = T_G_fpos64_t type T_IO_FILE = struct { F__ccgo_align [0]uint32 F_flags int32 F_IO_read_ptr uintptr F_IO_read_end uintptr F_IO_read_base uintptr F_IO_write_base uintptr F_IO_write_ptr uintptr F_IO_write_end uintptr F_IO_buf_base uintptr F_IO_buf_end uintptr F_IO_save_base uintptr F_IO_backup_base uintptr F_IO_save_end uintptr F_markers uintptr F_chain uintptr F_fileno int32 F_flags2 int32 F_old_offset t__off_t F_cur_column uint16 F_vtable_offset int8 F_shortbuf [1]uint8 F_lock uintptr F__ccgo_align21 [4]byte F_offset t__off64_t F_codecvt uintptr F_wide_data uintptr F_freeres_list uintptr F_freeres_buf uintptr F__pad5 Tsize_t F_mode int32 F_unused2 [40]uint8 } type _IO_FILE = T_IO_FILE type t__FILE = struct { F__ccgo_align [0]uint32 F_flags int32 F_IO_read_ptr uintptr F_IO_read_end uintptr F_IO_read_base uintptr F_IO_write_base uintptr F_IO_write_ptr uintptr F_IO_write_end uintptr F_IO_buf_base uintptr F_IO_buf_end uintptr F_IO_save_base uintptr F_IO_backup_base uintptr F_IO_save_end uintptr F_markers uintptr F_chain uintptr F_fileno int32 F_flags2 int32 F_old_offset t__off_t F_cur_column uint16 F_vtable_offset int8 F_shortbuf [1]uint8 F_lock uintptr F__ccgo_align21 [4]byte F_offset t__off64_t F_codecvt uintptr F_wide_data uintptr F_freeres_list uintptr F_freeres_buf uintptr F__pad5 Tsize_t F_mode int32 F_unused2 [40]uint8 } type TFILE = struct { F__ccgo_align [0]uint32 F_flags int32 F_IO_read_ptr uintptr F_IO_read_end uintptr F_IO_read_base uintptr F_IO_write_base uintptr F_IO_write_ptr uintptr F_IO_write_end uintptr F_IO_buf_base uintptr F_IO_buf_end uintptr F_IO_save_base uintptr F_IO_backup_base uintptr F_IO_save_end uintptr F_markers uintptr F_chain uintptr F_fileno int32 F_flags2 int32 F_old_offset t__off_t F_cur_column uint16 F_vtable_offset int8 F_shortbuf [1]uint8 F_lock uintptr F__ccgo_align21 [4]byte F_offset t__off64_t F_codecvt uintptr F_wide_data uintptr F_freeres_list uintptr F_freeres_buf uintptr F__pad5 Tsize_t F_mode int32 F_unused2 [40]uint8 } type FILE = TFILE type T_IO_lock_t = struct{} type _IO_lock_t = T_IO_lock_t type Tcookie_io_functions_t = struct { Fread uintptr Fwrite uintptr Fseek uintptr Fclose1 uintptr } type cookie_io_functions_t = Tcookie_io_functions_t type T_IO_cookie_io_functions_t = Tcookie_io_functions_t type _IO_cookie_io_functions_t = T_IO_cookie_io_functions_t type Toff_t = int64 type off_t = Toff_t type Toff64_t = int64 type off64_t = Toff64_t type Tssize_t = int32 type ssize_t = Tssize_t type Tfpos_t = struct { F__ccgo_align [0]uint32 F__pos t__off64_t F__state t__mbstate_t } type fpos_t = Tfpos_t type Tfpos64_t = struct { F__ccgo_align [0]uint32 F__pos t__off64_t F__state t__mbstate_t } type fpos64_t = Tfpos64_t type Twchar_t = uint32 type wchar_t = Twchar_t type Tdiv_t = struct { Fquot int32 Frem int32 } type div_t = Tdiv_t type Tldiv_t = struct { Fquot int32 Frem int32 } type ldiv_t = Tldiv_t type Tlldiv_t = struct { F__ccgo_align [0]uint32 Fquot int64 Frem int64 } type lldiv_t = Tlldiv_t type t__locale_struct = struct { F__locales [13]uintptr F__ctype_b uintptr F__ctype_tolower uintptr F__ctype_toupper uintptr F__names [13]uintptr } type t__locale_t = uintptr type Tlocale_t = uintptr type locale_t = Tlocale_t type Tu_char = uint8 type u_char = Tu_char type Tu_short = uint16 type u_short = Tu_short type Tu_int = uint32 type u_int = Tu_int type Tu_long = uint32 type u_long = Tu_long type Tquad_t = int64 type quad_t = Tquad_t type Tu_quad_t = uint64 type u_quad_t = Tu_quad_t type Tfsid_t = struct { F__val [2]int32 } type fsid_t = Tfsid_t type Tloff_t = int64 type loff_t = Tloff_t type Tino_t = uint64 type ino_t = Tino_t type Tino64_t = uint64 type ino64_t = Tino64_t type Tdev_t = uint64 type dev_t = Tdev_t type Tgid_t = uint32 type gid_t = Tgid_t type Tmode_t = uint32 type mode_t = Tmode_t type Tnlink_t = uint32 type nlink_t = Tnlink_t type Tuid_t = uint32 type uid_t = Tuid_t type Tpid_t = int32 type pid_t = Tpid_t type Tid_t = uint32 type id_t = Tid_t type Tdaddr_t = int32 type daddr_t = Tdaddr_t type Tcaddr_t = uintptr type caddr_t = Tcaddr_t type Tkey_t = int32 type key_t = Tkey_t type Tclock_t = int32 type clock_t = Tclock_t type Tclockid_t = int32 type clockid_t = Tclockid_t type Ttime_t = int32 type time_t = Ttime_t type Ttimer_t = uintptr type timer_t = Ttimer_t type Tuseconds_t = uint32 type useconds_t = Tuseconds_t type Tsuseconds_t = int32 type suseconds_t = Tsuseconds_t type Tulong = uint32 type ulong = Tulong type Tushort = uint16 type ushort = Tushort type Tuint = uint32 type uint = Tuint type Tint8_t = int8 type int8_t = Tint8_t type Tint16_t = int16 type int16_t = Tint16_t type Tint32_t = int32 type int32_t = Tint32_t type Tint64_t = int64 type int64_t = Tint64_t type Tu_int8_t = uint8 type u_int8_t = Tu_int8_t type Tu_int16_t = uint16 type u_int16_t = Tu_int16_t type Tu_int32_t = uint32 type u_int32_t = Tu_int32_t type Tu_int64_t = uint64 type u_int64_t = Tu_int64_t type Tregister_t = int32 type register_t = Tregister_t type t__sigset_t = struct { F__val [32]uint32 } type Tsigset_t = struct { F__val [32]uint32 } type sigset_t = Tsigset_t type Ttimeval = struct { Ftv_sec t__time_t Ftv_usec t__suseconds_t } type timeval = Ttimeval type Ttimespec = struct { Ftv_sec t__time_t Ftv_nsec t__syscall_slong_t } type timespec = Ttimespec type t__fd_mask = int32 type Tfd_set = struct { Ffds_bits [32]t__fd_mask } type fd_set = Tfd_set type Tfd_mask = int32 type fd_mask = Tfd_mask type Tblksize_t = int32 type blksize_t = Tblksize_t type Tblkcnt_t = int64 type blkcnt_t = Tblkcnt_t type Tfsblkcnt_t = uint64 type fsblkcnt_t = Tfsblkcnt_t type Tfsfilcnt_t = uint64 type fsfilcnt_t = Tfsfilcnt_t type Tblkcnt64_t = int64 type blkcnt64_t = Tblkcnt64_t type Tfsblkcnt64_t = uint64 type fsblkcnt64_t = Tfsblkcnt64_t type Tfsfilcnt64_t = uint64 type fsfilcnt64_t = Tfsfilcnt64_t type t__atomic_wide_counter = struct { F__ccgo_align [0]uint32 F__value32 [0]struct { F__low uint32 F__high uint32 } F__value64 uint64 } type t__pthread_list_t = struct { F__prev uintptr F__next uintptr } type t__pthread_internal_list = t__pthread_list_t type t__pthread_slist_t = struct { F__next uintptr } type t__pthread_internal_slist = t__pthread_slist_t type t__pthread_mutex_s = struct { F__lock int32 F__count uint32 F__owner int32 F__kind int32 F__nusers uint32 F__ccgo5_20 struct { F__list [0]t__pthread_slist_t F__spins int32 } } type t__pthread_rwlock_arch_t = struct { F__readers uint32 F__writers uint32 F__wrphase_futex uint32 F__writers_futex uint32 F__pad3 uint32 F__pad4 uint32 F__flags uint8 F__shared uint8 F__pad1 uint8 F__pad2 uint8 F__cur_writer int32 } type t__pthread_cond_s = struct { F__ccgo_align [0]uint32 F__wseq t__atomic_wide_counter F__g1_start t__atomic_wide_counter F__g_refs [2]uint32 F__g_size [2]uint32 F__g1_orig_size uint32 F__wrefs uint32 F__g_signals [2]uint32 } type t__tss_t = uint32 type t__thrd_t = uint32 type t__once_flag = struct { F__data int32 } type Tpthread_t = uint32 type pthread_t = Tpthread_t type Tpthread_mutexattr_t = struct { F__align [0]int32 F__size [4]uint8 } type pthread_mutexattr_t = Tpthread_mutexattr_t type Tpthread_condattr_t = struct { F__align [0]int32 F__size [4]uint8 } type pthread_condattr_t = Tpthread_condattr_t type Tpthread_key_t = uint32 type pthread_key_t = Tpthread_key_t type Tpthread_once_t = int32 type pthread_once_t = Tpthread_once_t type Tpthread_attr_t1 = struct { F__align [0]int32 F__size [36]uint8 } type pthread_attr_t1 = Tpthread_attr_t1 type Tpthread_attr_t = struct { F__align [0]int32 F__size [36]uint8 } type pthread_attr_t = Tpthread_attr_t type Tpthread_mutex_t = struct { F__size [0][24]uint8 F__align [0]int32 F__data t__pthread_mutex_s } type pthread_mutex_t = Tpthread_mutex_t type Tpthread_cond_t = struct { F__ccgo_align [0]uint32 F__size [0][48]uint8 F__align [0]int64 F__data t__pthread_cond_s } type pthread_cond_t = Tpthread_cond_t type Tpthread_rwlock_t = struct { F__size [0][32]uint8 F__align [0]int32 F__data t__pthread_rwlock_arch_t } type pthread_rwlock_t = Tpthread_rwlock_t type Tpthread_rwlockattr_t = struct { F__align [0]int32 F__size [8]uint8 } type pthread_rwlockattr_t = Tpthread_rwlockattr_t type Tpthread_spinlock_t = int32 type pthread_spinlock_t = Tpthread_spinlock_t type Tpthread_barrier_t = struct { F__align [0]int32 F__size [20]uint8 } type pthread_barrier_t = Tpthread_barrier_t type Tpthread_barrierattr_t = struct { F__align [0]int32 F__size [4]uint8 } type pthread_barrierattr_t = Tpthread_barrierattr_t type Trandom_data = struct { Ffptr uintptr Frptr uintptr Fstate uintptr Frand_type int32 Frand_deg int32 Frand_sep int32 Fend_ptr uintptr } type random_data = Trandom_data type Tdrand48_data = struct { F__ccgo_align [0]uint32 F__x [3]uint16 F__old_x [3]uint16 F__c uint16 F__init uint16 F__a uint64 } type drand48_data = Tdrand48_data type t__compar_fn_t = uintptr type Tcomparison_fn_t = uintptr type comparison_fn_t = Tcomparison_fn_t type t__compar_d_fn_t = uintptr type Tptrdiff_t = int32 type ptrdiff_t = Tptrdiff_t type Tmax_align_t = struct { F__ccgo_align [0]uint32 F__max_align_ll int64 F__max_align_ld float64 } type max_align_t = Tmax_align_t /* ** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY. ** This allows better measurements of where memcpy() is used when running ** cachegrind. But this macro version of memcpy() is very slow so it ** should not be used in production. This is a performance measurement ** hack only. */ /* ** If compiling for a processor that lacks floating point support, ** substitute integer for floating-point */ /* ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0 ** afterward. Having this macro allows us to cause the C compiler ** to omit code used by TEMP tables without messy #ifndef statements. */ /* ** The "file format" number is an integer that is incremented whenever ** the VDBE-level file format changes. The following macros define the ** the default file format for new databases and the maximum file format ** that the library can read. */ /* ** Determine whether triggers are recursive by default. This can be ** changed at run-time using a pragma. */ /* ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified ** on the command-line */ /* ** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if ** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it ** to zero. */ /* ** The default initial allocation for the pagecache when using separate ** pagecaches for each database connection. A positive number is the ** number of pages. A negative number N translations means that a buffer ** of -1024*N bytes is allocated and used for as many pages as it will hold. ** ** The default value of "20" was chosen to minimize the run-time of the ** speedtest1 test program with options: --shrink-memory --reprepare */ /* ** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option. */ /* ** The compile-time options SQLITE_MMAP_READWRITE and ** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another. ** You must choose one or the other (or neither) but not both. */ /* ** GCC does not define the offsetof() macro so we'll have to do it ** ourselves. */ /* ** Macros to compute minimum and maximum of two numbers. */ /* ** Swap two objects of type TYPE. */ /* ** Check to see if this machine uses EBCDIC. (Yes, believe it or ** not, there are still machines out there that use EBCDIC.) */ // C documentation // // /* // ** Integers of known sizes. These typedefs might change for architectures // ** where the sizes very. Preprocessor macros are available so that the // ** types can be conveniently redefined at compile-type. Like this: // ** // ** cc '-DUINTPTR_TYPE=long long int' ... // */ type Ti64 = int64 type i64 = Ti64 /* 8-byte signed integer */ type Tu64 = uint64 type u64 = Tu64 /* 8-byte unsigned integer */ type Tu32 = uint32 type u32 = Tu32 /* 4-byte unsigned integer */ type Tu16 = uint16 type u16 = Tu16 /* 2-byte unsigned integer */ type Ti16 = int16 type i16 = Ti16 /* 2-byte signed integer */ type Tu8 = uint8 type u8 = Tu8 /* 1-byte unsigned integer */ type Ti8 = int8 type i8 = Ti8 /* 1-byte signed integer */ /* ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value ** that can be stored in a u32 without loss of data. The value ** is 0x00000000ffffffff. But because of quirks of some compilers, we ** have to specify the value in the less intuitive manner shown: */ // C documentation // // /* // ** The datatype used to store estimates of the number of rows in a // ** table or index. // */ type TtRowcnt = uint64 type tRowcnt = TtRowcnt // C documentation // // /* // ** Estimated quantities used for query planning are stored as 16-bit // ** logarithms. For quantity X, the value stored is 10*log2(X). This // ** gives a possible range of values of approximately 1.0e986 to 1e-986. // ** But the allowed values are "grainy". Not every value is representable. // ** For example, quantities 16 and 17 are both represented by a LogEst // ** of 40. However, since LogEst quantities are suppose to be estimates, // ** not exact values, this imprecision is not a problem. // ** // ** "LogEst" is short for "Logarithmic Estimate". // ** // ** Examples: // ** 1 -> 0 20 -> 43 10000 -> 132 // ** 2 -> 10 25 -> 46 25000 -> 146 // ** 3 -> 16 100 -> 66 1000000 -> 199 // ** 4 -> 20 1000 -> 99 1048576 -> 200 // ** 10 -> 33 1024 -> 100 4294967296 -> 320 // ** // ** The LogEst can be negative to indicate fractional values. // ** Examples: // ** // ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40 // */ type TLogEst = int16 type LogEst = TLogEst /* ** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer */ // C documentation // // /* The uptr type is an unsigned integer large enough to hold a pointer // */ type Tuptr = uint32 type uptr = Tuptr /* ** Bits for the sqlite3WhereTrace mask: ** ** (---any--) Top-level block structure ** 0x-------F High-level debug messages ** 0x----FFF- More detail ** 0xFFFF---- Low-level debug messages ** ** 0x00000001 Code generation ** 0x00000002 Solver ** 0x00000004 Solver costs ** 0x00000008 WhereLoop inserts ** ** 0x00000010 Display sqlite3_index_info xBestIndex calls ** 0x00000020 Range an equality scan metrics ** 0x00000040 IN operator decisions ** 0x00000080 WhereLoop cost adjustements ** 0x00000100 ** 0x00000200 Covering index decisions ** 0x00000400 OR optimization ** 0x00000800 Index scanner ** 0x00001000 More details associated with code generation ** 0x00002000 ** 0x00004000 Show all WHERE terms at key points ** 0x00008000 Show the full SELECT statement at key places ** ** 0x00010000 Show more detail when printing WHERE terms ** 0x00020000 Show WHERE terms returned from whereScanNext() */ // C documentation // // /* // ** An instance of the following structure is used to store the busy-handler // ** callback for a given sqlite handle. // ** // ** The sqlite.busyHandler member of the sqlite struct contains the busy // ** callback for the database handle. Each pager opened via the sqlite // ** handle is passed a pointer to sqlite.busyHandler. The busy-handler // ** callback is currently invoked only from within pager.c. // */ type TBusyHandler = struct { FxBusyHandler uintptr FpBusyArg uintptr FnBusy int32 } type BusyHandler = TBusyHandler type TBusyHandler1 = struct { FxBusyHandler uintptr FpBusyArg uintptr FnBusy int32 } type BusyHandler1 = TBusyHandler1 /* ** Name of table that holds the database schema. ** ** The PREFERRED names are used wherever possible. But LEGACY is also ** used for backwards compatibility. ** ** 1. Queries can use either the PREFERRED or the LEGACY names ** 2. The sqlite3_set_authorizer() callback uses the LEGACY name ** 3. The PRAGMA table_list statement uses the PREFERRED name ** ** The LEGACY names are stored in the internal symbol hash table ** in support of (2). Names are translated using sqlite3PreferredTableName() ** for (3). The sqlite3FindTable() function takes care of translating ** names for (1). ** ** Note that "sqlite_temp_schema" can also be called "temp.sqlite_schema". */ /* ** The root-page of the schema table. */ /* ** The name of the schema table. The name is different for TEMP. */ /* ** A convenience macro that returns the number of elements in ** an array. */ /* ** Determine if the argument is a power of two */ /* ** The following value as a destructor means to use sqlite3DbFree(). ** The sqlite3DbFree() routine requires two parameters instead of the ** one parameter that destructors normally want. So we have to introduce ** this magic value that the code knows to handle differently. Any ** pointer will work here as long as it is distinct from SQLITE_STATIC ** and SQLITE_TRANSIENT. */ /* ** When SQLITE_OMIT_WSD is defined, it means that the target platform does ** not support Writable Static Data (WSD) such as global and static variables. ** All variables must either be on the stack or dynamically allocated from ** the heap. When WSD is unsupported, the variable declarations scattered ** throughout the SQLite code must become constants instead. The SQLITE_WSD ** macro is used for this purpose. And instead of referencing the variable ** directly, we use its constant as a key to lookup the run-time allocated ** buffer that holds real variable. The constant is also the initializer ** for the run-time allocated buffer. ** ** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL ** macros become no-ops and have zero performance impact. */ /* ** The following macros are used to suppress compiler warnings and to ** make it clear to human readers when a function parameter is deliberately ** left unused within the body of a function. This usually happens when ** a function is called via a function pointer. For example the ** implementation of an SQL aggregate step callback may not use the ** parameter indicating the number of arguments passed to the aggregate, ** if it knows that this is enforced elsewhere. ** ** When a function parameter is not used at all within the body of a function, ** it is generally named "NotUsed" or "NotUsed2" to make things even clearer. ** However, these macros may also be used to suppress warnings related to ** parameters that may or may not be used depending on compilation options. ** For example those parameters only used in assert() statements. In these ** cases the parameters are named as per the usual conventions. */ // C documentation // // /* // ** Forward references to structures // */ type TAggInfo = struct { FdirectMode Tu8 FuseSortingIdx Tu8 FnSortingColumn Tu16 FsortingIdx int32 FsortingIdxPTab int32 FiFirstReg int32 FpGroupBy uintptr FaCol uintptr FnColumn int32 FnAccumulator int32 FaFunc uintptr FnFunc int32 FselId Tu32 } type AggInfo = TAggInfo type TAuthContext = struct { FzAuthContext uintptr FpParse uintptr } type AuthContext = TAuthContext type TAutoincInfo = struct { FpNext uintptr FpTab uintptr FiDb int32 FregCtr int32 } type AutoincInfo = TAutoincInfo type TBitvec = struct { FiSize Tu32 FnSet Tu32 FiDivisor Tu32 Fu struct { FaHash [0][125]Tu32 FapSub [0][125]uintptr FaBitmap [500]Tu8 } } type Bitvec = TBitvec type TCollSeq = struct { FzName uintptr Fenc Tu8 FpUser uintptr FxCmp uintptr FxDel uintptr } type CollSeq = TCollSeq type TColumn = struct { FzCnName uintptr F__ccgo4 uint8 Faffinity uint8 FszEst Tu8 FhName Tu8 FiDflt Tu16 FcolFlags Tu16 } type Column = TColumn type TCte = struct { FzName uintptr FpCols uintptr FpSelect uintptr FzCteErr uintptr FpUse uintptr FeM10d Tu8 } type Cte = TCte type TCteUse = struct { FnUse int32 FaddrM9e int32 FregRtn int32 FiCur int32 FnRowEst TLogEst FeM10d Tu8 } type CteUse = TCteUse type TDb = struct { FzDbSName uintptr FpBt uintptr Fsafety_level Tu8 FbSyncSet Tu8 FpSchema uintptr } type Db = TDb type TDbClientData = struct { FpNext uintptr FpData uintptr FxDestructor uintptr FzName [1]uint8 } type DbClientData = TDbClientData type TDbFixer = struct { FpParse uintptr Fw TWalker FpSchema uintptr FbTemp Tu8 FzDb uintptr FzType uintptr FpName uintptr } type DbFixer = TDbFixer type TSchema = struct { Fschema_cookie int32 FiGeneration int32 FtblHash THash FidxHash THash FtrigHash THash FfkeyHash THash FpSeqTab uintptr Ffile_format Tu8 Fenc Tu8 FschemaFlags Tu16 Fcache_size int32 } type Schema = TSchema type TExpr = struct { Fop Tu8 FaffExpr uint8 Fop2 Tu8 Fflags Tu32 Fu struct { FiValue [0]int32 FzToken uintptr } FpLeft uintptr FpRight uintptr Fx struct { FpSelect [0]uintptr FpList uintptr } FnHeight int32 FiTable int32 FiColumn TynVar FiAgg Ti16 Fw struct { FiOfst [0]int32 FiJoin int32 } FpAggInfo uintptr Fy struct { FpWin [0]uintptr Fsub [0]struct { FiAddr int32 FregReturn int32 } FpTab uintptr F__ccgo_pad3 [4]byte } } type Expr = TExpr type TExprList = struct { FnExpr int32 FnAlloc int32 Fa [1]TExprList_item } type ExprList = TExprList type TFKey = struct { FpFrom uintptr FpNextFrom uintptr FzTo uintptr FpNextTo uintptr FpPrevTo uintptr FnCol int32 FisDeferred Tu8 FaAction [2]Tu8 FapTrigger [2]uintptr FaCol [1]TsColMap } type FKey = TFKey type TFpDecode = struct { Fsign uint8 FisSpecial uint8 Fn int32 FiDP int32 Fz uintptr FzBuf [24]uint8 } type FpDecode = TFpDecode type TFuncDestructor = struct { FnRef int32 FxDestroy uintptr FpUserData uintptr } type FuncDestructor = TFuncDestructor type TFuncDef = struct { FnArg Ti8 FfuncFlags Tu32 FpUserData uintptr FpNext uintptr FxSFunc uintptr FxFinalize uintptr FxValue uintptr FxInverse uintptr FzName uintptr Fu struct { FpDestructor [0]uintptr FpHash uintptr } } type FuncDef = TFuncDef type TFuncDefHash = struct { Fa [23]uintptr } type FuncDefHash = TFuncDefHash type TIdList = struct { FnId int32 FeU4 Tu8 Fa [1]TIdList_item } type IdList = TIdList type TIndex = struct { F__ccgo_align [0]uint32 FzName uintptr FaiColumn uintptr FaiRowLogEst uintptr FpTable uintptr FzColAff uintptr FpNext uintptr FpSchema uintptr FaSortOrder uintptr FazColl uintptr FpPartIdxWhere uintptr FaColExpr uintptr Ftnum TPgno FszIdxRow TLogEst FnKeyCol Tu16 FnColumn Tu16 FonError Tu8 F__ccgo56 uint16 FnSample int32 FmxSample int32 FnSampleCol int32 FaAvgEq uintptr FaSample uintptr FaiRowEst uintptr F__ccgo_align34 [4]byte FnRowEst0 TtRowcnt FcolNotIdxed TBitmask } type Index = TIndex type TIndexedExpr = struct { FpExpr uintptr FiDataCur int32 FiIdxCur int32 FiIdxCol int32 FbMaybeNullRow Tu8 Faff Tu8 FpIENext uintptr } type IndexedExpr = TIndexedExpr type TIndexSample = struct { Fp uintptr Fn int32 FanEq uintptr FanLt uintptr FanDLt uintptr } type IndexSample = TIndexSample type TKeyInfo = struct { FnRef Tu32 Fenc Tu8 FnKeyField Tu16 FnAllField Tu16 Fdb uintptr FaSortFlags uintptr FaColl [1]uintptr } type KeyInfo = TKeyInfo type TLookaside = struct { FbDisable Tu32 Fsz Tu16 FszTrue Tu16 FbMalloced Tu8 FnSlot Tu32 FanStat [3]Tu32 FpInit uintptr FpFree uintptr FpSmallInit uintptr FpSmallFree uintptr FpMiddle uintptr FpStart uintptr FpEnd uintptr FpTrueEnd uintptr } type Lookaside = TLookaside type TLookasideSlot = struct { FpNext uintptr } type LookasideSlot = TLookasideSlot type TModule = struct { FpModule uintptr FzName uintptr FnRefModule int32 FpAux uintptr FxDestroy uintptr FpEpoTab uintptr } type Module = TModule type TNameContext = struct { FpParse uintptr FpSrcList uintptr FuNC struct { FpAggInfo [0]uintptr FpUpsert [0]uintptr FiBaseReg [0]int32 FpEList uintptr } FpNext uintptr FnRef int32 FnNcErr int32 FncFlags int32 FnNestedSelect Tu32 FpWinSelect uintptr } type NameContext = TNameContext type TOnOrUsing = struct { FpOn uintptr FpUsing uintptr } type OnOrUsing = TOnOrUsing type TParse = struct { Fdb uintptr FzErrMsg uintptr FpVdbe uintptr Frc int32 FcolNamesSet Tu8 FcheckSchema Tu8 Fnested Tu8 FnTempReg Tu8 FisMultiWrite Tu8 FmayAbort Tu8 FhasCompound Tu8 FokConstFactor Tu8 FdisableLookaside Tu8 FprepFlags Tu8 FwithinRJSubrtn Tu8 FnRangeReg int32 FiRangeReg int32 FnErr int32 FnTab int32 FnMem int32 FszOpAlloc int32 FiSelfTab int32 FnLabel int32 FnLabelAlloc int32 FaLabel uintptr FpConstExpr uintptr FpIdxEpr uintptr FpIdxPartExpr uintptr FconstraintName TToken FwriteMask TyDbMask FcookieMask TyDbMask FregRowid int32 FregRoot int32 FnMaxArg int32 FnSelect int32 FnProgressSteps Tu32 FnTableLock int32 FaTableLock uintptr FpAinc uintptr FpToplevel uintptr FpTriggerTab uintptr FpTriggerPrg uintptr FpCleanup uintptr Fu1 struct { FpReturning [0]uintptr FaddrCrTab int32 } Foldmask Tu32 Fnewmask Tu32 FnQueryLoop TLogEst FeTriggerOp Tu8 FbReturning Tu8 FeOrconf Tu8 FdisableTriggers Tu8 FaTempReg [8]int32 FpOuterParse uintptr FsNameToken TToken FsLastToken TToken FnVar TynVar FiPkSortOrder Tu8 Fexplain Tu8 FeParseMode Tu8 FnVtabLock int32 FnHeight int32 FaddrExplain int32 FpVList uintptr FpReprepare uintptr FzTail uintptr FpNewTable uintptr FpNewIndex uintptr FpNewTrigger uintptr FzAuthContext uintptr FsArg TToken FapVtabLock uintptr FpWith uintptr FpRename uintptr } type Parse = TParse type TParseCleanup = struct { FpNext uintptr FpPtr uintptr FxCleanup uintptr } type ParseCleanup = TParseCleanup type TPreUpdate = struct { F__ccgo_align [0]uint32 Fv uintptr FpCsr uintptr Fop int32 FaRecord uintptr Fkeyinfo TKeyInfo FpUnpacked uintptr FpNewUnpacked uintptr FiNewReg int32 FiBlobWrite int32 FiKey1 Ti64 FiKey2 Ti64 FaNew uintptr FpTab uintptr FpPk uintptr F__ccgo_pad14 [4]byte } type PreUpdate = TPreUpdate type TPrintfArguments = struct { FnArg int32 FnUsed int32 FapArg uintptr } type PrintfArguments = TPrintfArguments type TRCStr = struct { F__ccgo_align [0]uint32 FnRCRef Tu64 } type RCStr = TRCStr type TRenameToken = struct { Fp uintptr Ft TToken FpNext uintptr } type RenameToken = TRenameToken type TReturning = struct { FpParse uintptr FpReturnEL uintptr FretTrig TTrigger FretTStep TTriggerStep FiRetCur int32 FnRetCol int32 FiRetReg int32 FzName [40]uint8 } type Returning = TReturning type TRowSet = struct { FpChunk uintptr Fdb uintptr FpEntry uintptr FpLast uintptr FpFresh uintptr FpForest uintptr FnFresh Tu16 FrsFlags Tu16 FiBatch int32 } type RowSet = TRowSet type TSavepoint = struct { F__ccgo_align [0]uint32 FzName uintptr F__ccgo_align1 [4]byte FnDeferredCons Ti64 FnDeferredImmCons Ti64 FpNext uintptr F__ccgo_pad4 [4]byte } type Savepoint = TSavepoint type TSelect = struct { Fop Tu8 FnSelectRow TLogEst FselFlags Tu32 FiLimit int32 FiOffset int32 FselId Tu32 FaddrOpenEphm [2]int32 FpEList uintptr FpSrc uintptr FpWhere uintptr FpGroupBy uintptr FpHaving uintptr FpOrderBy uintptr FpPrior uintptr FpNext uintptr FpLimit uintptr FpWith uintptr FpWin uintptr FpWinDefn uintptr } type Select = TSelect type TSQLiteThread = struct { FxTask uintptr FpIn uintptr FpResult uintptr } type SQLiteThread = TSQLiteThread type TSelectDest = struct { FeDest Tu8 FiSDParm int32 FiSDParm2 int32 FiSdst int32 FnSdst int32 FzAffSdst uintptr FpOrderBy uintptr } type SelectDest = TSelectDest type TSrcItem = struct { F__ccgo_align [0]uint32 FpSchema uintptr FzDatabase uintptr FzName uintptr FzAlias uintptr FpTab uintptr FpSelect uintptr FaddrFillSub int32 FregReturn int32 FregResult int32 Ffg struct { F__ccgo_align [0]uint32 Fjointype Tu8 F__ccgo_align1 [2]byte F__ccgo4 uint16 } FiCursor int32 Fu3 struct { FpUsing [0]uintptr FpOn uintptr } F__ccgo_align12 [4]byte FcolUsed TBitmask Fu1 struct { FpFuncArg [0]uintptr FzIndexedBy uintptr } Fu2 struct { FpCteUse [0]uintptr FpIBIndex uintptr } } type SrcItem = TSrcItem type TSrcList = struct { F__ccgo_align [0]uint32 FnSrc int32 FnAlloc Tu32 Fa [1]TSrcItem } type SrcList = TSrcList type TStrAccum = struct { Fdb uintptr FzText uintptr FnAlloc Tu32 FmxAlloc Tu32 FnChar Tu32 FaccError Tu8 FprintfFlags Tu8 } type StrAccum = TStrAccum type Tsqlite3_str1 = TStrAccum type sqlite3_str1 = Tsqlite3_str1 /* Internal alias for sqlite3_str */ type TTable = struct { FzName uintptr FaCol uintptr FpIndex uintptr FzColAff uintptr FpCheck uintptr Ftnum TPgno FnTabRef Tu32 FtabFlags Tu32 FiPKey Ti16 FnCol Ti16 FnNVCol Ti16 FnRowLogEst TLogEst FszTabRow TLogEst FkeyConf Tu8 FeTabType Tu8 Fu struct { Fview [0]struct { FpSelect uintptr } Fvtab [0]struct { FnArg int32 FazArg uintptr Fp uintptr } Ftab struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr } } FpTrigger uintptr FpSchema uintptr } type Table = TTable type TTableLock = struct { FiDb int32 FiTab TPgno FisWriteLock Tu8 FzLockName uintptr } type TableLock = TTableLock type TToken = struct { Fz uintptr Fn uint32 } type Token = TToken type TTrigger = struct { FzName uintptr Ftable uintptr Fop Tu8 Ftr_tm Tu8 FbReturning Tu8 FpWhen uintptr FpColumns uintptr FpSchema uintptr FpTabSchema uintptr Fstep_list uintptr FpNext uintptr } type Trigger = TTrigger type TTriggerPrg = struct { FpTrigger uintptr FpNext uintptr FpProgram uintptr Forconf int32 FaColmask [2]Tu32 } type TriggerPrg = TTriggerPrg type TTriggerStep = struct { Fop Tu8 Forconf Tu8 FpTrig uintptr FpSelect uintptr FzTarget uintptr FpFrom uintptr FpWhere uintptr FpExprList uintptr FpIdList uintptr FpUpsert uintptr FzSpan uintptr FpNext uintptr FpLast uintptr } type TriggerStep = TTriggerStep type TUnpackedRecord = struct { F__ccgo_align [0]uint32 FpKeyInfo uintptr FaMem uintptr Fu struct { F__ccgo_align [0]uint32 Fi [0]Ti64 Fz uintptr F__ccgo_pad2 [4]byte } Fn int32 FnField Tu16 Fdefault_rc Ti8 FerrCode Tu8 Fr1 Ti8 Fr2 Ti8 FeqSeen Tu8 F__ccgo_pad10 [5]byte } type UnpackedRecord = TUnpackedRecord type TUpsert = struct { FpUpsertTarget uintptr FpUpsertTargetWhere uintptr FpUpsertSet uintptr FpUpsertWhere uintptr FpNextUpsert uintptr FisDoUpdate Tu8 FisDup Tu8 FpToFree uintptr FpUpsertIdx uintptr FpUpsertSrc uintptr FregData int32 FiDataCur int32 FiIdxCur int32 } type Upsert = TUpsert type TVTable = struct { Fdb uintptr FpMod uintptr FpVtab uintptr FnRef int32 FbConstraint Tu8 FbAllSchemas Tu8 FeVtabRisk Tu8 FiSavepoint int32 FpNext uintptr } type VTable = TVTable type TVtabCtx = struct { FpVTable uintptr FpTab uintptr FpPrior uintptr FbDeclared int32 } type VtabCtx = TVtabCtx type TWalker = struct { FpParse uintptr FxExprCallback uintptr FxSelectCallback uintptr FxSelectCallback2 uintptr FwalkerDepth int32 FeCode Tu16 FmWFlags Tu16 Fu struct { Fn [0]int32 FiCur [0]int32 FpSrcList [0]uintptr FpCCurHint [0]uintptr FpRefSrcList [0]uintptr FaiCol [0]uintptr FpIdxCover [0]uintptr FpGroupBy [0]uintptr FpSelect [0]uintptr FpRewrite [0]uintptr FpConst [0]uintptr FpRename [0]uintptr FpTab [0]uintptr FpCovIdxCk [0]uintptr FpSrcItem [0]uintptr FpFix [0]uintptr FaMem [0]uintptr FpNC uintptr } } type Walker = TWalker type TWhereInfo = struct { F__ccgo_align [0]uint32 FpParse uintptr FpTabList uintptr FpOrderBy uintptr FpResultSet uintptr FpSelect uintptr FaiCurOnePass [2]int32 FiContinue int32 FiBreak int32 FsavedNQueryLoop int32 FwctrlFlags Tu16 FiLimit TLogEst FnLevel Tu8 FnOBSat Ti8 FeOnePass Tu8 FeDistinct Tu8 F__ccgo48 uint8 FnRowOut TLogEst FiTop int32 FiEndWhere int32 FpLoops uintptr FpMemToFree uintptr F__ccgo_align24 [4]byte FrevMask TBitmask FsWC TWhereClause FsMaskSet TWhereMaskSet Fa [1]TWhereLevel } type WhereInfo = TWhereInfo type TWindow = struct { FzName uintptr FzBase uintptr FpPartition uintptr FpOrderBy uintptr FeFrmType Tu8 FeStart Tu8 FeEnd Tu8 FbImplicitFrame Tu8 FeExclude Tu8 FpStart uintptr FpEnd uintptr FppThis uintptr FpNextWin uintptr FpFilter uintptr FpWFunc uintptr FiEphCsr int32 FregAccum int32 FregResult int32 FcsrApp int32 FregApp int32 FregPart int32 FpOwner uintptr FnBufferCol int32 FiArgCol int32 FregOne int32 FregStartRowid int32 FregEndRowid int32 FbExprArgs Tu8 } type Window = TWindow type TWith = struct { FnCte int32 FbView int32 FpOuter uintptr Fa [1]TCte } type With = TWith // C documentation // // /* // ** The bitmask datatype defined below is used for various optimizations. // ** // ** Changing this from a 64-bit to a 32-bit type limits the number of // ** tables in a join to 32 instead of 64. But it also reduces the size // ** of the library by 738 bytes on ix86. // */ type TBitmask = uint64 type Bitmask = TBitmask /* ** The number of bits in a Bitmask. "BMS" means "BitMask Size". */ /* ** A bit in a Bitmask */ // C documentation // // /* A VList object records a mapping between parameters/variables/wildcards // ** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer // ** variable number associated with that parameter. See the format description // ** on the sqlite3VListAdd() routine for more information. A VList is really // ** just an array of integers. // */ type TVList = int32 type VList = TVList /************** End of os.h **************************************************/ /************** Continuing where we left off in sqliteInt.h ******************/ /************** Include pager.h in the middle of sqliteInt.h *****************/ /************** Begin file pager.h *******************************************/ /* ** 2001 September 15 ** ** 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 header file defines the interface that the sqlite page cache ** subsystem. The page cache subsystem reads and writes a file a page ** at a time and provides a journal for rollback. */ /* ** Default maximum size for persistent journal files. A negative ** value means no limit. This value may be overridden using the ** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit". */ // C documentation // // /* // ** The type used to represent a page number. The first page in a file // ** is called page 1. 0 is used to represent "not a page". // */ type TPgno = uint32 type Pgno = TPgno // C documentation // // /* // ** Each open file is managed by a separate instance of the "Pager" structure. // */ type TPager = struct { F__ccgo_align [0]uint32 FpVfs uintptr FexclusiveMode Tu8 FjournalMode Tu8 FuseJournal Tu8 FnoSync Tu8 FfullSync Tu8 FextraSync Tu8 FsyncFlags Tu8 FwalSyncFlags Tu8 FtempFile Tu8 FnoLock Tu8 FreadOnly Tu8 FmemDb Tu8 FmemVfs Tu8 FeState Tu8 FeLock Tu8 FchangeCountDone Tu8 FsetSuper Tu8 FdoNotSpill Tu8 FsubjInMemory Tu8 FbUseFetch Tu8 FhasHeldSharedLock Tu8 FdbSize TPgno FdbOrigSize TPgno FdbFileSize TPgno FdbHintSize TPgno FerrCode int32 FnRec int32 FcksumInit Tu32 FnSubRec Tu32 FpInJournal uintptr Ffd uintptr Fjfd uintptr Fsjfd uintptr F__ccgo_align34 [4]byte FjournalOff Ti64 FjournalHdr Ti64 FpBackup uintptr FaSavepoint uintptr FnSavepoint int32 FiDataVersion Tu32 FdbFileVers [16]uint8 FnMmapOut int32 F__ccgo_align42 [4]byte FszMmap Tsqlite3_int64 FpMmapFreelist uintptr FnExtra Tu16 FnReserve Ti16 FvfsFlags Tu32 FsectorSize Tu32 FmxPgno TPgno FlckPgno TPgno FpageSize Ti64 FjournalSizeLimit Ti64 FzFilename uintptr FzJournal uintptr FxBusyHandler uintptr FpBusyHandlerArg uintptr FaStat [4]Tu32 FxReiniter uintptr FxGet uintptr FpTmpSpace uintptr FpPCache uintptr FpWal uintptr FzWal uintptr } type Pager = TPager // C documentation // // /* // ** Handle type for pages. // */ type TDbPage = struct { F__ccgo_align [0]uint32 FpPage uintptr FpData uintptr FpExtra uintptr FpCache uintptr FpDirty uintptr FpPager uintptr Fpgno TPgno Fflags Tu16 F__ccgo_align8 [2]byte FnRef Ti64 FpDirtyNext uintptr FpDirtyPrev uintptr } type DbPage = TDbPage // C documentation // // /* // ** Handle type for pages. // */ type TPgHdr2 = TDbPage type PgHdr2 = TPgHdr2 /* Functions to support testing and debugging. */ /************** End of pager.h ***********************************************/ /************** Continuing where we left off in sqliteInt.h ******************/ /************** Include btree.h in the middle of sqliteInt.h *****************/ /************** Begin file btree.h *******************************************/ /* ** 2001 September 15 ** ** 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 header file defines the interface that the sqlite B-Tree file ** subsystem. See comments in the source code for a detailed description ** of what each interface routine does. */ /* TODO: This definition is just included so other modules compile. It ** needs to be revisited. */ /* ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise ** it must be turned on for each database using "PRAGMA auto_vacuum = 1". */ // C documentation // // /* // ** Forward declarations of structure // */ type TBtree = struct { Fdb uintptr FpBt uintptr FinTrans Tu8 Fsharable Tu8 Flocked Tu8 FhasIncrblobCur Tu8 FwantToLock int32 FnBackup int32 FiBDataVersion Tu32 FpNext uintptr FpPrev uintptr Flock TBtLock } type Btree = TBtree type TBtCursor = struct { F__ccgo_align [0]uint32 FeState Tu8 FcurFlags Tu8 FcurPagerFlags Tu8 Fhints Tu8 FskipNext int32 FpBtree uintptr FaOverflow uintptr FpKey uintptr FpBt uintptr FpNext uintptr F__ccgo_align10 [4]byte Finfo TCellInfo FnKey Ti64 FpgnoRoot TPgno FiPage Ti8 FcurIntKey Tu8 Fix Tu16 FaiIdx [19]Tu16 FpKeyInfo uintptr FpPage uintptr FapPage [19]uintptr F__ccgo_pad20 [4]byte } type BtCursor = TBtCursor type TBtShared = struct { FpPager uintptr Fdb uintptr FpCursor uintptr FpPage1 uintptr FopenFlags Tu8 FautoVacuum Tu8 FincrVacuum Tu8 FbDoTruncate Tu8 FinTransaction Tu8 Fmax1bytePayload Tu8 FnReserveWanted Tu8 FbtsFlags Tu16 FmaxLocal Tu16 FminLocal Tu16 FmaxLeaf Tu16 FminLeaf Tu16 FpageSize Tu32 FusableSize Tu32 FnTransaction int32 FnPage Tu32 FpSchema uintptr FxFreeSchema uintptr Fmutex uintptr FpHasContent uintptr FnRef int32 FpNext uintptr FpLock uintptr FpWriter uintptr FpTmpSpace uintptr FnPreformatSize int32 } type BtShared = TBtShared type TBtreePayload = struct { F__ccgo_align [0]uint32 FpKey uintptr F__ccgo_align1 [4]byte FnKey Tsqlite3_int64 FpData uintptr FaMem uintptr FnMem Tu16 FnData int32 FnZero int32 F__ccgo_pad7 [4]byte } type BtreePayload = TBtreePayload /* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */ /* An instance of the BtreePayload object describes the content of a single ** entry in either an index or table btree. ** ** Index btrees (used for indexes and also WITHOUT ROWID tables) contain ** an arbitrary key and no data. These btrees have pKey,nKey set to the ** key and the pData,nData,nZero fields are uninitialized. The aMem,nMem ** fields give an array of Mem objects that are a decomposition of the key. ** The nMem field might be zero, indicating that no decomposition is available. ** ** Table btrees (used for rowid tables) contain an integer rowid used as ** the key and passed in the nKey field. The pKey field is zero. ** pData,nData hold the content of the new entry. nZero extra zero bytes ** are appended to the end of the content when constructing the entry. ** The aMem,nMem fields are uninitialized for table btrees. ** ** Field usage summary: ** ** Table BTrees Index Btrees ** ** pKey always NULL encoded key ** nKey the ROWID length of pKey ** pData data not used ** aMem not used decomposed key value ** nMem not used entries in aMem ** nData length of pData not used ** nZero extra zeros after pData not used ** ** This object is used to pass information into sqlite3BtreeInsert(). The ** same information used to be passed as five separate parameters. But placing ** the information into this object helps to keep the interface more ** organized and understandable, and it also helps the resulting code to ** run a little faster by using fewer registers for parameter passing. */ type TBtreePayload1 = struct { F__ccgo_align [0]uint32 FpKey uintptr F__ccgo_align1 [4]byte FnKey Tsqlite3_int64 FpData uintptr FaMem uintptr FnMem Tu16 FnData int32 FnZero int32 F__ccgo_pad7 [4]byte } type BtreePayload1 = TBtreePayload1 /************** End of btree.h ***********************************************/ /************** Continuing where we left off in sqliteInt.h ******************/ /************** Include vdbe.h in the middle of sqliteInt.h ******************/ /************** Begin file vdbe.h ********************************************/ /* ** 2001 September 15 ** ** 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. ** ************************************************************************* ** Header file for the Virtual DataBase Engine (VDBE) ** ** This header defines the interface to the virtual database engine ** or VDBE. The VDBE implements an abstract machine that runs a ** simple program to access and modify the underlying database. */ /* #include */ // C documentation // // /* // ** A single VDBE is an opaque structure named "Vdbe". Only routines // ** in the source file sqliteVdbe.c are allowed to see the insides // ** of this structure. // */ type TVdbe = struct { F__ccgo_align [0]uint32 Fdb uintptr FppVPrev uintptr FpVNext uintptr FpParse uintptr FnVar TynVar FnMem int32 FnCursor int32 FcacheCtr Tu32 Fpc int32 Frc int32 FnChange Ti64 FiStatement int32 F__ccgo_align12 [4]byte FiCurrentTime Ti64 FnFkConstraint Ti64 FnStmtDefCons Ti64 FnStmtDefImmCons Ti64 FaMem uintptr FapArg uintptr FapCsr uintptr FaVar uintptr FaOp uintptr FnOp int32 FnOpAlloc int32 FaColName uintptr FpResultRow uintptr FzErrMsg uintptr FpVList uintptr F__ccgo_align27 [4]byte FstartTime Ti64 FnResColumn Tu16 FnResAlloc Tu16 FerrorAction Tu8 FminWriteFileFormat Tu8 FprepFlags Tu8 FeVdbeState Tu8 F__ccgo152 uint16 FbtreeMask TyDbMask FlockMask TyDbMask FaCounter [9]Tu32 FzSql uintptr FpFree uintptr FpFrame uintptr FpDelFrame uintptr FnFrame int32 Fexpmask Tu32 FpProgram uintptr FpAuxData uintptr } type Vdbe = TVdbe // C documentation // // /* // ** The names of the following types declared in vdbeInt.h are required // ** for the VdbeOp definition. // */ type TMem = struct { F__ccgo_align [0]uint32 Fu TMemValue Fz uintptr Fn int32 Fflags Tu16 Fenc Tu8 FeSubtype Tu8 Fdb uintptr FszMalloc int32 FuTemp Tu32 FzMalloc uintptr FxDel uintptr } type Mem = TMem // C documentation // // /* // ** The names of the following types declared in vdbeInt.h are required // ** for the VdbeOp definition. // */ type Tsqlite3_value1 = TMem type sqlite3_value1 = Tsqlite3_value1 type TSubProgram = struct { FaOp uintptr FnOp int32 FnMem int32 FnCsr int32 FaOnce uintptr Ftoken uintptr FpNext uintptr } type SubProgram = TSubProgram /* ** A single instruction of the virtual machine has an opcode ** and as many as three operands. The instruction is recorded ** as an instance of the following structure: */ type TVdbeOp1 = struct { Fopcode Tu8 Fp4type int8 Fp5 Tu16 Fp1 int32 Fp2 int32 Fp3 int32 Fp4 Tp4union } type VdbeOp1 = TVdbeOp1 type TVdbeOp = struct { Fopcode Tu8 Fp4type int8 Fp5 Tu16 Fp1 int32 Fp2 int32 Fp3 int32 Fp4 Tp4union } type VdbeOp = TVdbeOp /* ** A sub-routine used to implement a trigger program. */ type TSubProgram1 = struct { FaOp uintptr FnOp int32 FnMem int32 FnCsr int32 FaOnce uintptr Ftoken uintptr FpNext uintptr } type SubProgram1 = TSubProgram1 /* ** A smaller version of VdbeOp used for the VdbeAddOpList() function because ** it takes up less space. */ type TVdbeOpList1 = struct { Fopcode Tu8 Fp1 int8 Fp2 int8 Fp3 int8 } type VdbeOpList1 = TVdbeOpList1 type TVdbeOpList = struct { Fopcode Tu8 Fp1 int8 Fp2 int8 Fp3 int8 } type VdbeOpList = TVdbeOpList type TRecordCompare = uintptr type RecordCompare = TRecordCompare /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on ** each VDBE opcode. ** ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op ** comments in VDBE programs that show key decision points in the code ** generator. */ /* ** The VdbeCoverage macros are used to set a coverage testing point ** for VDBE branch instructions. The coverage testing points are line ** numbers in the sqlite3.c source file. VDBE branch coverage testing ** only works with an amalgamation build. That's ok since a VDBE branch ** coverage build designed for testing the test suite only. No application ** should ever ship with VDBE branch coverage measuring turned on. ** ** VdbeCoverage(v) // Mark the previously coded instruction ** // as a branch ** ** VdbeCoverageIf(v, conditional) // Mark previous if conditional true ** ** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken ** ** VdbeCoverageNeverTaken(v) // Previous branch is never taken ** ** VdbeCoverageNeverNull(v) // Previous three-way branch is only ** // taken on the first two ways. The ** // NULL option is not possible ** ** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested ** // in distinguishing equal and not-equal. ** ** Every VDBE branch operation must be tagged with one of the macros above. ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch() ** routine in vdbe.c, alerting the developer to the missed tag. ** ** During testing, the test application will invoke ** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback ** routine that is invoked as each bytecode branch is taken. The callback ** contains the sqlite3.c source line number of the VdbeCoverage macro and ** flags to indicate whether or not the branch was taken. The test application ** is responsible for keeping track of this and reporting byte-code branches ** that are never taken. ** ** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the ** vdbe.c source file for additional information. */ /************** End of vdbe.h ************************************************/ /************** Continuing where we left off in sqliteInt.h ******************/ /************** Include pcache.h in the middle of sqliteInt.h ****************/ /************** Begin file pcache.h ******************************************/ /* ** 2008 August 05 ** ** 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 header file defines the interface that the sqlite page cache ** subsystem. */ type TPgHdr = struct { F__ccgo_align [0]uint32 FpPage uintptr FpData uintptr FpExtra uintptr FpCache uintptr FpDirty uintptr FpPager uintptr Fpgno TPgno Fflags Tu16 F__ccgo_align8 [2]byte FnRef Ti64 FpDirtyNext uintptr FpDirtyPrev uintptr } type PgHdr = TPgHdr type TPCache = struct { F__ccgo_align [0]uint32 FpDirty uintptr FpDirtyTail uintptr FpSynced uintptr F__ccgo_align3 [4]byte FnRefSum Ti64 FszCache int32 FszSpill int32 FszPage int32 FszExtra int32 FbPurgeable Tu8 FeCreate Tu8 FxStress uintptr FpStress uintptr FpCache uintptr } type PCache = TPCache /************** End of mutex.h ***********************************************/ /************** Continuing where we left off in sqliteInt.h ******************/ /* The SQLITE_EXTRA_DURABLE compile-time option used to set the default ** synchronous setting to EXTRA. It is no longer supported. */ /* ** Default synchronous levels. ** ** Note that (for historical reasons) the PAGER_SYNCHRONOUS_* macros differ ** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1. ** ** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS ** OFF 1 0 ** NORMAL 2 1 ** FULL 3 2 ** EXTRA 4 3 ** ** The "PRAGMA synchronous" statement also uses the zero-based numbers. ** In other words, the zero-based numbers are used for all external interfaces ** and the one-based values are used internally. */ /* ** Each database file to be accessed by the system is an instance ** of the following structure. There are normally two of these structures ** in the sqlite.aDb[] array. aDb[0] is the main database file and ** aDb[1] is the database file used to hold temporary tables. Additional ** databases may be attached. */ type TDb1 = struct { FzDbSName uintptr FpBt uintptr Fsafety_level Tu8 FbSyncSet Tu8 FpSchema uintptr } type Db1 = TDb1 /* ** An instance of the following structure stores a database schema. ** ** Most Schema objects are associated with a Btree. The exception is ** the Schema for the TEMP database (sqlite3.aDb[1]) which is free-standing. ** In shared cache mode, a single Schema object can be shared by multiple ** Btrees that refer to the same underlying BtShared object. ** ** Schema objects are automatically deallocated when the last Btree that ** references them is destroyed. The TEMP Schema is manually freed by ** sqlite3_close(). * ** A thread must be holding a mutex on the corresponding Btree in order ** to access Schema content. This implies that the thread must also be ** holding a mutex on the sqlite3 connection pointer that owns the Btree. ** For a TEMP Schema, only the connection mutex is required. */ type TSchema1 = struct { Fschema_cookie int32 FiGeneration int32 FtblHash THash FidxHash THash FtrigHash THash FfkeyHash THash FpSeqTab uintptr Ffile_format Tu8 Fenc Tu8 FschemaFlags Tu16 Fcache_size int32 } type Schema1 = TSchema1 /* ** These macros can be used to test, set, or clear bits in the ** Db.pSchema->flags field. */ /* ** Allowed values for the DB.pSchema->flags field. ** ** The DB_SchemaLoaded flag is set after the database schema has been ** read into internal hash tables. ** ** DB_UnresetViews means that one or more views have column names that ** have been filled out. If the schema changes, these column names might ** changes and so the view will need to be reset. */ /* ** The number of different kinds of things that can be limited ** using the sqlite3_limit() interface. */ /* ** Lookaside malloc is a set of fixed-size buffers that can be used ** to satisfy small transient memory allocation requests for objects ** associated with a particular database connection. The use of ** lookaside malloc provides a significant performance enhancement ** (approx 10%) by avoiding numerous malloc/free requests while parsing ** SQL statements. ** ** The Lookaside structure holds configuration information about the ** lookaside malloc subsystem. Each available memory allocation in ** the lookaside subsystem is stored on a linked list of LookasideSlot ** objects. ** ** Lookaside allocations are only allowed for objects that are associated ** with a particular database connection. Hence, schema information cannot ** be stored in lookaside because in shared cache mode the schema information ** is shared by multiple database connections. Therefore, while parsing ** schema information, the Lookaside.bEnabled flag is cleared so that ** lookaside allocations are not used to construct the schema objects. ** ** New lookaside allocations are only allowed if bDisable==0. When ** bDisable is greater than zero, sz is set to zero which effectively ** disables lookaside without adding a new test for the bDisable flag ** in a performance-critical path. sz should be set by to szTrue whenever ** bDisable changes back to zero. ** ** Lookaside buffers are initially held on the pInit list. As they are ** used and freed, they are added back to the pFree list. New allocations ** come off of pFree first, then pInit as a fallback. This dual-list ** allows use to compute a high-water mark - the maximum number of allocations ** outstanding at any point in the past - by subtracting the number of ** allocations on the pInit list from the total number of allocations. ** ** Enhancement on 2019-12-12: Two-size-lookaside ** The default lookaside configuration is 100 slots of 1200 bytes each. ** The larger slot sizes are important for performance, but they waste ** a lot of space, as most lookaside allocations are less than 128 bytes. ** The two-size-lookaside enhancement breaks up the lookaside allocation ** into two pools: One of 128-byte slots and the other of the default size ** (1200-byte) slots. Allocations are filled from the small-pool first, ** failing over to the full-size pool if that does not work. Thus more ** lookaside slots are available while also using less memory. ** This enhancement can be omitted by compiling with ** SQLITE_OMIT_TWOSIZE_LOOKASIDE. */ type TLookaside1 = struct { FbDisable Tu32 Fsz Tu16 FszTrue Tu16 FbMalloced Tu8 FnSlot Tu32 FanStat [3]Tu32 FpInit uintptr FpFree uintptr FpSmallInit uintptr FpSmallFree uintptr FpMiddle uintptr FpStart uintptr FpEnd uintptr FpTrueEnd uintptr } type Lookaside1 = TLookaside1 type TLookasideSlot1 = struct { FpNext uintptr } type LookasideSlot1 = TLookasideSlot1 /* Size of the smaller allocations in two-size lookaside */ /* ** A hash table for built-in function definitions. (Application-defined ** functions use a regular table table from hash.h.) ** ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots. ** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH() ** macro to compute a hash on the function name. */ type TFuncDefHash1 = struct { Fa [23]uintptr } type FuncDefHash1 = TFuncDefHash1 // C documentation // // /* // ** typedef for the authorization callback function. // */ type Tsqlite3_xauth = uintptr type sqlite3_xauth = Tsqlite3_xauth /* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing ** in the style of sqlite3_trace() */ /* ** Maximum number of sqlite3.aDb[] entries. This is the number of attached ** databases plus 2 for "main" and "temp". */ /* ** Each database connection is an instance of the following structure. */ type Tsqlite31 = struct { F__ccgo_align [0]uint32 FpVfs uintptr FpVdbe uintptr FpDfltColl uintptr Fmutex uintptr FaDb uintptr FnDb int32 FmDbFlags Tu32 F__ccgo_align7 [4]byte Fflags Tu64 FlastRowid Ti64 FszMmap Ti64 FnSchemaLock Tu32 FopenFlags uint32 FerrCode int32 FerrByteOffset int32 FerrMask int32 FiSysErrno int32 FdbOptFlags Tu32 Fenc Tu8 FautoCommit Tu8 Ftemp_store Tu8 FmallocFailed Tu8 FbBenignMalloc Tu8 FdfltLockMode Tu8 FnextAutovac int8 FsuppressErr Tu8 FvtabOnConflict Tu8 FisTransactionSavepoint Tu8 FmTrace Tu8 FnoSharedCache Tu8 FnSqlExec Tu8 FeOpenState Tu8 FnextPagesize int32 FnChange Ti64 FnTotalChange Ti64 FaLimit [12]int32 FnMaxSorterMmap int32 Finit1 Tsqlite3InitInfo FnVdbeActive int32 FnVdbeRead int32 FnVdbeWrite int32 FnVdbeExec int32 FnVDestroy int32 FnExtension int32 FaExtension uintptr Ftrace struct { FxV2 [0]uintptr FxLegacy uintptr } FpTraceArg uintptr FxProfile uintptr FpProfileArg uintptr FpCommitArg uintptr FxCommitCallback uintptr FpRollbackArg uintptr FxRollbackCallback uintptr FpUpdateArg uintptr FxUpdateCallback uintptr FpAutovacPagesArg uintptr FxAutovacDestr uintptr FxAutovacPages uintptr FpParse uintptr FpPreUpdateArg uintptr FxPreUpdateCallback uintptr FpPreUpdate uintptr FxWalCallback uintptr FpWalArg uintptr FxCollNeeded uintptr FxCollNeeded16 uintptr FpCollNeededArg uintptr FpErr uintptr F__ccgo_align67 [4]byte Fu1 struct { F__ccgo_align [0]uint32 FnotUsed1 [0]float64 FisInterrupted int32 F__ccgo_pad2 [4]byte } Flookaside TLookaside FxAuth Tsqlite3_xauth FpAuthArg uintptr FxProgress uintptr FpProgressArg uintptr FnProgressOps uint32 FnVTrans int32 FaModule THash FpVtabCtx uintptr FaVTrans uintptr FpDisconnect uintptr FaFunc THash FaCollSeq THash FbusyHandler TBusyHandler FaDbStatic [2]TDb FpSavepoint uintptr FnAnalysisLimit int32 FbusyTimeout int32 FnSavepoint int32 FnStatement int32 FnDeferredCons Ti64 FnDeferredImmCons Ti64 FpnBytesFreed uintptr FpDbData uintptr FpBlockingConnection uintptr FpUnlockConnection uintptr FpUnlockArg uintptr FxUnlockNotify uintptr FpNextBlocked uintptr F__ccgo_pad97 [4]byte } type sqlite31 = Tsqlite31 /* ** A macro to discover the encoding of a database. */ /* ** A u64 constant where the lower 32 bits are all zeros. Only the ** upper 32 bits are included in the argument. Necessary because some ** C-compilers still do not accept LL integer literals. */ /* ** Possible values for the sqlite3.flags. ** ** Value constraints (enforced via assert()): ** SQLITE_FullFSync == PAGER_FULLFSYNC ** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC ** SQLITE_CacheSpill == PAGER_CACHE_SPILL */ /* result set is empty */ /* DELETE, or UPDATE and return */ /* the count using a callback. */ /* Flags used only if debugging */ /* ** Allowed values for sqlite3.mDbFlags */ /* ** Bits of the sqlite3.dbOptFlags field that are used by the ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to ** selectively disable various optimizations. */ /* TH3 expects this value ^^^^^^^^^^ to be 0x0000800. Don't change it */ /* TH3 expects this value ^^^^^^^^^^ to be 0x40000. Coordinate any change */ /* TH3 expects this value ^^^^^^^^^^ See flatten04.test */ /* ** Macros for testing whether or not optimizations are enabled or disabled. */ /* ** Return true if it OK to factor constant expressions into the initialization ** code. The argument is a Parse object for the code generator. */ /* Possible values for the sqlite3.eOpenState field. ** The numbers are randomly selected such that a minimum of three bits must ** change to convert any number to another or to zero */ /* ** Each SQL function is defined by an instance of the following ** structure. For global built-in functions (ex: substr(), max(), count()) ** a pointer to this structure is held in the sqlite3BuiltinFunctions object. ** For per-connection application-defined functions, a pointer to this ** structure is held in the db->aHash hash table. ** ** The u.pHash field is used by the global built-ins. The u.pDestructor ** field is used by per-connection app-def functions. */ type TFuncDef1 = struct { FnArg Ti8 FfuncFlags Tu32 FpUserData uintptr FpNext uintptr FxSFunc uintptr FxFinalize uintptr FxValue uintptr FxInverse uintptr FzName uintptr Fu struct { FpDestructor [0]uintptr FpHash uintptr } } type FuncDef1 = TFuncDef1 /* ** This structure encapsulates a user-function destructor callback (as ** configured using create_function_v2()) and a reference counter. When ** create_function_v2() is called to create a function with a destructor, ** a single object of this type is allocated. FuncDestructor.nRef is set to ** the number of FuncDef objects created (either 1 or 3, depending on whether ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor ** member of each of the new FuncDef objects is set to point to the allocated ** FuncDestructor. ** ** Thereafter, when one of the FuncDef objects is deleted, the reference ** count on this object is decremented. When it reaches 0, the destructor ** is invoked and the FuncDestructor structure freed. */ type TFuncDestructor1 = struct { FnRef int32 FxDestroy uintptr FpUserData uintptr } type FuncDestructor1 = TFuncDestructor1 /* ** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF ** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And ** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There ** are assert() statements in the code to verify this. ** ** Value constraints (enforced via assert()): ** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg ** SQLITE_FUNC_ANYORDER == NC_OrderAgg == SF_OrderByReqd ** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG ** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG ** SQLITE_FUNC_BYTELEN == OPFLAG_BYTELENARG ** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API ** SQLITE_FUNC_DIRECT == SQLITE_DIRECTONLY from the API ** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS -- opposite meanings!!! ** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API ** ** Note that even though SQLITE_FUNC_UNSAFE and SQLITE_INNOCUOUS have the ** same bit value, their meanings are inverted. SQLITE_FUNC_UNSAFE is ** used internally and if set means that the function has side effects. ** SQLITE_INNOCUOUS is used by application code and means "not unsafe". ** See multiple instances of tag-20230109-1. */ /* 0x0200 -- available for reuse */ /* SQLITE_SUBTYPE 0x00100000 // Consumer of subtypes */ /* SQLITE_RESULT_SUBTYPE 0x01000000 // Generator of subtypes */ /* Identifier numbers for each in-line function */ /* ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are ** used to create the initializers for the FuncDef structures. ** ** FUNCTION(zName, nArg, iArg, bNC, xFunc) ** Used to create a scalar function definition of a function zName ** implemented by C function xFunc that accepts nArg arguments. The ** value passed as iArg is cast to a (void*) and made available ** as the user-data (sqlite3_user_data()) for the function. If ** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set. ** ** VFUNCTION(zName, nArg, iArg, bNC, xFunc) ** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag. ** ** SFUNCTION(zName, nArg, iArg, bNC, xFunc) ** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and ** adds the SQLITE_DIRECTONLY flag. ** ** INLINE_FUNC(zName, nArg, iFuncId, mFlags) ** zName is the name of a function that is implemented by in-line ** byte code rather than by the usual callbacks. The iFuncId ** parameter determines the function id. The mFlags parameter is ** optional SQLITE_FUNC_ flags for this function. ** ** TEST_FUNC(zName, nArg, iFuncId, mFlags) ** zName is the name of a test-only function implemented by in-line ** byte code rather than by the usual callbacks. The iFuncId ** parameter determines the function id. The mFlags parameter is ** optional SQLITE_FUNC_ flags for this function. ** ** DFUNCTION(zName, nArg, iArg, bNC, xFunc) ** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and ** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions ** and functions like sqlite_version() that can change, but not during ** a single query. The iArg is ignored. The user-data is always set ** to a NULL pointer. The bNC parameter is not used. ** ** MFUNCTION(zName, nArg, xPtr, xFunc) ** For math-library functions. xPtr is an arbitrary pointer. ** ** PURE_DATE(zName, nArg, iArg, bNC, xFunc) ** Used for "pure" date/time functions, this macro is like DFUNCTION ** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is ** ignored and the user-data for these functions is set to an ** arbitrary non-NULL pointer. The bNC parameter is not used. ** ** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal) ** Used to create an aggregate function definition implemented by ** the C functions xStep and xFinal. The first four parameters ** are interpreted in the same way as the first 4 parameters to ** FUNCTION(). ** ** WAGGREGATE(zName, nArg, iArg, xStep, xFinal, xValue, xInverse) ** Used to create an aggregate function definition implemented by ** the C functions xStep and xFinal. The first four parameters ** are interpreted in the same way as the first 4 parameters to ** FUNCTION(). ** ** LIKEFUNC(zName, nArg, pArg, flags) ** Used to create a scalar function definition of a function zName ** that accepts nArg arguments and is implemented by a call to C ** function likeFunc. Argument pArg is cast to a (void *) and made ** available as the function user-data (sqlite3_user_data()). The ** FuncDef.flags variable is set to the value passed as the flags ** parameter. */ /* ** All current savepoints are stored in a linked list starting at ** sqlite3.pSavepoint. The first element in the list is the most recently ** opened savepoint. Savepoints are added to the list by the vdbe ** OP_Savepoint instruction. */ type TSavepoint1 = struct { F__ccgo_align [0]uint32 FzName uintptr F__ccgo_align1 [4]byte FnDeferredCons Ti64 FnDeferredImmCons Ti64 FpNext uintptr F__ccgo_pad4 [4]byte } type Savepoint1 = TSavepoint1 /* ** The following are used as the second parameter to sqlite3Savepoint(), ** and as the P1 argument to the OP_Savepoint instruction. */ /* ** Each SQLite module (virtual table definition) is defined by an ** instance of the following structure, stored in the sqlite3.aModule ** hash table. */ type TModule1 = struct { FpModule uintptr FzName uintptr FnRefModule int32 FpAux uintptr FxDestroy uintptr FpEpoTab uintptr } type Module1 = TModule1 /* ** Information about each column of an SQL table is held in an instance ** of the Column structure, in the Table.aCol[] array. ** ** Definitions: ** ** "table column index" This is the index of the column in the ** Table.aCol[] array, and also the index of ** the column in the original CREATE TABLE stmt. ** ** "storage column index" This is the index of the column in the ** record BLOB generated by the OP_MakeRecord ** opcode. The storage column index is less than ** or equal to the table column index. It is ** equal if and only if there are no VIRTUAL ** columns to the left. ** ** Notes on zCnName: ** The zCnName field stores the name of the column, the datatype of the ** column, and the collating sequence for the column, in that order, all in ** a single allocation. Each string is 0x00 terminated. The datatype ** is only included if the COLFLAG_HASTYPE bit of colFlags is set and the ** collating sequence name is only included if the COLFLAG_HASCOLL bit is ** set. */ type TColumn1 = struct { FzCnName uintptr F__ccgo4 uint8 Faffinity uint8 FszEst Tu8 FhName Tu8 FiDflt Tu16 FcolFlags Tu16 } type Column1 = TColumn1 /* Allowed values for Column.eCType. ** ** Values must match entries in the global constant arrays ** sqlite3StdTypeLen[] and sqlite3StdType[]. Each value is one more ** than the offset into these arrays for the corresponding name. ** Adjust the SQLITE_N_STDTYPE value if adding or removing entries. */ /* Allowed values for Column.colFlags. ** ** Constraints: ** TF_HasVirtual == COLFLAG_VIRTUAL ** TF_HasStored == COLFLAG_STORED ** TF_HasHidden == COLFLAG_HIDDEN */ /* ** A "Collating Sequence" is defined by an instance of the following ** structure. Conceptually, a collating sequence consists of a name and ** a comparison routine that defines the order of that sequence. ** ** If CollSeq.xCmp is NULL, it means that the ** collating sequence is undefined. Indices built on an undefined ** collating sequence may not be read or written. */ type TCollSeq1 = struct { FzName uintptr Fenc Tu8 FpUser uintptr FxCmp uintptr FxDel uintptr } type CollSeq1 = TCollSeq1 /* ** A sort order can be either ASC or DESC. */ /* ** Column affinity types. ** ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and ** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve ** the speed a little by numbering the values consecutively. ** ** But rather than start with 0 or 1, we begin with 'A'. That way, ** when multiple affinity types are concatenated into a string and ** used as the P4 operand, they will be more readable. ** ** Note also that the numeric types are grouped together so that testing ** for a numeric type is a single comparison. And the BLOB type is first. */ /* ** The SQLITE_AFF_MASK values masks off the significant bits of an ** affinity value. */ /* ** Additional bit values that can be ORed with an affinity without ** changing the affinity. ** ** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL. ** It causes an assert() to fire if either operand to a comparison ** operator is NULL. It is added to certain comparison operators to ** prove that the operands are always NOT NULL. */ /* ** An object of this type is created for each virtual table present in ** the database schema. ** ** If the database schema is shared, then there is one instance of this ** structure for each database connection (sqlite3*) that uses the shared ** schema. This is because each database connection requires its own unique ** instance of the sqlite3_vtab* handle used to access the virtual table ** implementation. sqlite3_vtab* handles can not be shared between ** database connections, even when the rest of the in-memory database ** schema is shared, as the implementation often stores the database ** connection handle passed to it via the xConnect() or xCreate() method ** during initialization internally. This database connection handle may ** then be used by the virtual table implementation to access real tables ** within the database. So that they appear as part of the callers ** transaction, these accesses need to be made via the same database ** connection as that used to execute SQL operations on the virtual table. ** ** All VTable objects that correspond to a single table in a shared ** database schema are initially stored in a linked-list pointed to by ** the Table.pVTable member variable of the corresponding Table object. ** When an sqlite3_prepare() operation is required to access the virtual ** table, it searches the list for the VTable that corresponds to the ** database connection doing the preparing so as to use the correct ** sqlite3_vtab* handle in the compiled query. ** ** When an in-memory Table object is deleted (for example when the ** schema is being reloaded for some reason), the VTable objects are not ** deleted and the sqlite3_vtab* handles are not xDisconnect()ed ** immediately. Instead, they are moved from the Table.pVTable list to ** another linked list headed by the sqlite3.pDisconnect member of the ** corresponding sqlite3 structure. They are then deleted/xDisconnected ** next time a statement is prepared using said sqlite3*. This is done ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes. ** Refer to comments above function sqlite3VtabUnlockList() for an ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect ** list without holding the corresponding sqlite3.mutex mutex. ** ** The memory for objects of this type is always allocated by ** sqlite3DbMalloc(), using the connection handle stored in VTable.db as ** the first argument. */ type TVTable1 = struct { Fdb uintptr FpMod uintptr FpVtab uintptr FnRef int32 FbConstraint Tu8 FbAllSchemas Tu8 FeVtabRisk Tu8 FiSavepoint int32 FpNext uintptr } type VTable1 = TVTable1 /* Allowed values for VTable.eVtabRisk */ /* ** The schema for each SQL table, virtual table, and view is represented ** in memory by an instance of the following structure. */ type TTable1 = struct { FzName uintptr FaCol uintptr FpIndex uintptr FzColAff uintptr FpCheck uintptr Ftnum TPgno FnTabRef Tu32 FtabFlags Tu32 FiPKey Ti16 FnCol Ti16 FnNVCol Ti16 FnRowLogEst TLogEst FszTabRow TLogEst FkeyConf Tu8 FeTabType Tu8 Fu struct { Fview [0]struct { FpSelect uintptr } Fvtab [0]struct { FnArg int32 FazArg uintptr Fp uintptr } Ftab struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr } } FpTrigger uintptr FpSchema uintptr } type Table1 = TTable1 /* ** Allowed values for Table.tabFlags. ** ** TF_OOOHidden applies to tables or view that have hidden columns that are ** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING ** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden, ** the TF_OOOHidden attribute would apply in this case. Such tables require ** special handling during INSERT processing. The "OOO" means "Out Of Order". ** ** Constraints: ** ** TF_HasVirtual == COLFLAG_VIRTUAL ** TF_HasStored == COLFLAG_STORED ** TF_HasHidden == COLFLAG_HIDDEN */ /* ** Allowed values for Table.eTabType */ /* ** Test to see whether or not a table is a virtual table. This is ** done as a macro so that it will be optimized out when virtual ** table support is omitted from the build. */ /* ** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn() ** only works for non-virtual tables (ordinary tables and views) and is ** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The ** IsHiddenColumn() macro is general purpose. */ /* Does the table have a rowid */ /* Macro is true if the SQLITE_ALLOW_ROWID_IN_VIEW (mis-)feature is ** available. By default, this macro is false */ /* ** Each foreign key constraint is an instance of the following structure. ** ** A foreign key is associated with two tables. The "from" table is ** the table that contains the REFERENCES clause that creates the foreign ** key. The "to" table is the table that is named in the REFERENCES clause. ** Consider this example: ** ** CREATE TABLE ex1( ** a INTEGER PRIMARY KEY, ** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x) ** ); ** ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2". ** Equivalent names: ** ** from-table == child-table ** to-table == parent-table ** ** Each REFERENCES clause generates an instance of the following structure ** which is attached to the from-table. The to-table need not exist when ** the from-table is created. The existence of the to-table is not checked. ** ** The list of all parents for child Table X is held at X.pFKey. ** ** A list of all children for a table named Z (which might not even exist) ** is held in Schema.fkeyHash with a hash key of Z. */ type TFKey1 = struct { FpFrom uintptr FpNextFrom uintptr FzTo uintptr FpNextTo uintptr FpPrevTo uintptr FnCol int32 FisDeferred Tu8 FaAction [2]Tu8 FapTrigger [2]uintptr FaCol [1]TsColMap } type FKey1 = TFKey1 /* ** SQLite supports many different ways to resolve a constraint ** error. ROLLBACK processing means that a constraint violation ** causes the operation in process to fail and for the current transaction ** to be rolled back. ABORT processing means the operation in process ** fails and any prior changes from that one operation are backed out, ** but the transaction is not rolled back. FAIL processing means that ** the operation in progress stops and returns an error code. But prior ** changes due to the same operation are not backed out and no rollback ** occurs. IGNORE means that the particular row that caused the constraint ** error is not inserted or updated. Processing continues and no error ** is returned. REPLACE means that preexisting database rows that caused ** a UNIQUE constraint violation are removed so that the new insert or ** update can proceed. Processing continues and no error is reported. ** UPDATE applies to insert operations only and means that the insert ** is omitted and the DO UPDATE clause of an upsert is run instead. ** ** RESTRICT, SETNULL, SETDFLT, and CASCADE actions apply only to foreign keys. ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the ** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign ** key is set to NULL. SETDFLT means that the foreign key is set ** to its default value. CASCADE means that a DELETE or UPDATE of the ** referenced table row is propagated into the row that holds the ** foreign key. ** ** The OE_Default value is a place holder that means to use whatever ** conflict resolution algorithm is required from context. ** ** The following symbolic values are used to record which type ** of conflict resolution action to take. */ /* ** An instance of the following structure is passed as the first ** argument to sqlite3VdbeKeyCompare and is used to control the ** comparison of the two index keys. ** ** Note that aSortOrder[] and aColl[] have nField+1 slots. There ** are nField slots for the columns of an index then one extra slot ** for the rowid at the end. */ type TKeyInfo1 = struct { FnRef Tu32 Fenc Tu8 FnKeyField Tu16 FnAllField Tu16 Fdb uintptr FaSortFlags uintptr FaColl [1]uintptr } type KeyInfo1 = TKeyInfo1 /* ** Allowed bit values for entries in the KeyInfo.aSortFlags[] array. */ /* ** This object holds a record which has been parsed out into individual ** fields, for the purposes of doing a comparison. ** ** A record is an object that contains one or more fields of data. ** Records are used to store the content of a table row and to store ** the key of an index. A blob encoding of a record is created by ** the OP_MakeRecord opcode of the VDBE and is disassembled by the ** OP_Column opcode. ** ** An instance of this object serves as a "key" for doing a search on ** an index b+tree. The goal of the search is to find the entry that ** is closed to the key described by this object. This object might hold ** just a prefix of the key. The number of fields is given by ** pKeyInfo->nField. ** ** The r1 and r2 fields are the values to return if this key is less than ** or greater than a key in the btree, respectively. These are normally ** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree ** is in DESC order. ** ** The key comparison functions actually return default_rc when they find ** an equals comparison. default_rc can be -1, 0, or +1. If there are ** multiple entries in the b-tree with the same key (when only looking ** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to ** cause the search to find the last match, or +1 to cause the search to ** find the first match. ** ** The key comparison functions will set eqSeen to true if they ever ** get and equal results when comparing this structure to a b-tree record. ** When default_rc!=0, the search might end up on the record immediately ** before the first match or immediately after the last match. The ** eqSeen field will indicate whether or not an exact match exists in the ** b-tree. */ type TUnpackedRecord1 = struct { F__ccgo_align [0]uint32 FpKeyInfo uintptr FaMem uintptr Fu struct { F__ccgo_align [0]uint32 Fi [0]Ti64 Fz uintptr F__ccgo_pad2 [4]byte } Fn int32 FnField Tu16 Fdefault_rc Ti8 FerrCode Tu8 Fr1 Ti8 Fr2 Ti8 FeqSeen Tu8 F__ccgo_pad10 [5]byte } type UnpackedRecord1 = TUnpackedRecord1 /* ** Each SQL index is represented in memory by an ** instance of the following structure. ** ** The columns of the table that are to be indexed are described ** by the aiColumn[] field of this structure. For example, suppose ** we have the following table and index: ** ** CREATE TABLE Ex1(c1 int, c2 int, c3 text); ** CREATE INDEX Ex2 ON Ex1(c3,c1); ** ** In the Table structure describing Ex1, nCol==3 because there are ** three columns in the table. In the Index structure describing ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed. ** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[]. ** The second column to be indexed (c1) has an index of 0 in ** Ex1.aCol[], hence Ex2.aiColumn[1]==0. ** ** The Index.onError field determines whether or not the indexed columns ** must be unique and what to do if they are not. When Index.onError=OE_None, ** it means this is not a unique index. Otherwise it is a unique index ** and the value of Index.onError indicates which conflict resolution ** algorithm to employ when an attempt is made to insert a non-unique ** element. ** ** The colNotIdxed bitmask is used in combination with SrcItem.colUsed ** for a fast test to see if an index can serve as a covering index. ** colNotIdxed has a 1 bit for every column of the original table that ** is *not* available in the index. Thus the expression ** "colUsed & colNotIdxed" will be non-zero if the index is not a ** covering index. The most significant bit of of colNotIdxed will always ** be true (note-20221022-a). If a column beyond the 63rd column of the ** table is used, the "colUsed & colNotIdxed" test will always be non-zero ** and we have to assume either that the index is not covering, or use ** an alternative (slower) algorithm to determine whether or not ** the index is covering. ** ** While parsing a CREATE TABLE or CREATE INDEX statement in order to ** generate VDBE code (as opposed to parsing one read from an sqlite_schema ** table as part of parsing an existing database schema), transient instances ** of this structure may be created. In this case the Index.tnum variable is ** used to store the address of a VDBE instruction, not a database page ** number (it cannot - the database page is not allocated until the VDBE ** program is executed). See convertToWithoutRowidTable() for details. */ type TIndex1 = struct { F__ccgo_align [0]uint32 FzName uintptr FaiColumn uintptr FaiRowLogEst uintptr FpTable uintptr FzColAff uintptr FpNext uintptr FpSchema uintptr FaSortOrder uintptr FazColl uintptr FpPartIdxWhere uintptr FaColExpr uintptr Ftnum TPgno FszIdxRow TLogEst FnKeyCol Tu16 FnColumn Tu16 FonError Tu8 F__ccgo56 uint16 FnSample int32 FmxSample int32 FnSampleCol int32 FaAvgEq uintptr FaSample uintptr FaiRowEst uintptr F__ccgo_align34 [4]byte FnRowEst0 TtRowcnt FcolNotIdxed TBitmask } type Index1 = TIndex1 /* ** Allowed values for Index.idxType */ /* Return true if index X is a PRIMARY KEY index */ /* Return true if index X is a UNIQUE index */ /* The Index.aiColumn[] values are normally positive integer. But ** there are some negative values that have special meaning: */ /* ** Each sample stored in the sqlite_stat4 table is represented in memory ** using a structure of this type. See documentation at the top of the ** analyze.c source file for additional information. */ type TIndexSample1 = struct { Fp uintptr Fn int32 FanEq uintptr FanLt uintptr FanDLt uintptr } type IndexSample1 = TIndexSample1 /* ** Possible values to use within the flags argument to sqlite3GetToken(). */ /* ** Each token coming out of the lexer is an instance of ** this structure. Tokens are also used as part of an expression. ** ** The memory that "z" points to is owned by other objects. Take care ** that the owner of the "z" string does not deallocate the string before ** the Token goes out of scope! Very often, the "z" points to some place ** in the middle of the Parse.zSql text. But it might also point to a ** static string. */ type TToken1 = struct { Fz uintptr Fn uint32 } type Token1 = TToken1 /* ** An instance of this structure contains information needed to generate ** code for a SELECT that contains aggregate functions. ** ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a ** pointer to this structure. The Expr.iAgg field is the index in ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate ** code for that node. ** ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the ** original Select structure that describes the SELECT statement. These ** fields do not need to be freed when deallocating the AggInfo structure. */ type TAggInfo1 = struct { FdirectMode Tu8 FuseSortingIdx Tu8 FnSortingColumn Tu16 FsortingIdx int32 FsortingIdxPTab int32 FiFirstReg int32 FpGroupBy uintptr FaCol uintptr FnColumn int32 FnAccumulator int32 FaFunc uintptr FnFunc int32 FselId Tu32 } type AggInfo1 = TAggInfo1 /* ** Macros to compute aCol[] and aFunc[] register numbers. ** ** These macros should not be used prior to the call to ** assignAggregateRegisters() that computes the value of pAggInfo->iFirstReg. ** The assert()s that are part of this macro verify that constraint. */ // C documentation // // /* // ** The datatype ynVar is a signed integer, either 16-bit or 32-bit. // ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater // ** than 32767 we have to make it 32-bit. 16-bit is preferred because // ** it uses less memory in the Expr object, which is a big memory user // ** in systems with lots of prepared statements. And few applications // ** need more than about 10 or 20 variables. But some extreme users want // ** to have prepared statements with over 32766 variables, and for them // ** the option is available (at compile-time). // */ type TynVar = int16 type ynVar = TynVar /* ** Each node of an expression in the parse tree is an instance ** of this structure. ** ** Expr.op is the opcode. The integer parser token codes are reused ** as opcodes here. For example, the parser defines TK_GE to be an integer ** code representing the ">=" operator. This same integer code is reused ** to represent the greater-than-or-equal-to operator in the expression ** tree. ** ** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, ** or TK_STRING), then Expr.u.zToken contains the text of the SQL literal. If ** the expression is a variable (TK_VARIABLE), then Expr.u.zToken contains the ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION), ** then Expr.u.zToken contains the name of the function. ** ** Expr.pRight and Expr.pLeft are the left and right subexpressions of a ** binary operator. Either or both may be NULL. ** ** Expr.x.pList is a list of arguments if the expression is an SQL function, ** a CASE expression or an IN expression of the form " IN (, ...)". ** Expr.x.pSelect is used if the expression is a sub-select or an expression of ** the form " IN (SELECT ...)". If the EP_xIsSelect bit is set in the ** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is ** valid. ** ** An expression of the form ID or ID.ID refers to a column in a table. ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is ** the integer cursor number of a VDBE cursor pointing to that table and ** Expr.iColumn is the column number for the specific column. If the ** expression is used as a result in an aggregate SELECT, then the ** value is also stored in the Expr.iAgg column in the aggregate so that ** it can be accessed after all aggregates are computed. ** ** If the expression is an unbound variable marker (a question mark ** character '?' in the original SQL) then the Expr.iTable holds the index ** number for that variable. ** ** If the expression is a subquery then Expr.iColumn holds an integer ** register number containing the result of the subquery. If the ** subquery gives a constant result, then iTable is -1. If the subquery ** gives a different answer at different times during statement processing ** then iTable is the address of a subroutine that computes the subquery. ** ** If the Expr is of type OP_Column, and the table it is selecting from ** is a disk table or the "old.*" pseudo-table, then pTab points to the ** corresponding table definition. ** ** ALLOCATION NOTES: ** ** Expr objects can use a lot of memory space in database schema. To ** help reduce memory requirements, sometimes an Expr object will be ** truncated. And to reduce the number of memory allocations, sometimes ** two or more Expr objects will be stored in a single memory allocation, ** together with Expr.u.zToken strings. ** ** If the EP_Reduced and EP_TokenOnly flags are set when ** an Expr object is truncated. When EP_Reduced is set, then all ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees ** are contained within the same memory allocation. Note, however, that ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately ** allocated, regardless of whether or not EP_Reduced is set. */ type TExpr1 = struct { Fop Tu8 FaffExpr uint8 Fop2 Tu8 Fflags Tu32 Fu struct { FiValue [0]int32 FzToken uintptr } FpLeft uintptr FpRight uintptr Fx struct { FpSelect [0]uintptr FpList uintptr } FnHeight int32 FiTable int32 FiColumn TynVar FiAgg Ti16 Fw struct { FiOfst [0]int32 FiJoin int32 } FpAggInfo uintptr Fy struct { FpWin [0]uintptr Fsub [0]struct { FiAddr int32 FregReturn int32 } FpTab uintptr F__ccgo_pad3 [4]byte } } type Expr1 = TExpr1 /* The following are the meanings of bits in the Expr.flags field. ** Value restrictions: ** ** EP_Agg == NC_HasAgg == SF_HasAgg ** EP_Win == NC_HasWin */ /* 0x80000000 // Available */ /* The EP_Propagate mask is a set of properties that automatically propagate ** upwards into parent nodes. */ /* Macros can be used to test, set, or clear bits in the ** Expr.flags field. */ /* Macros used to ensure that the correct members of unions are accessed ** in Expr. */ /* Flags for use with Expr.vvaFlags */ /* The ExprSetVVAProperty() macro is used for Verification, Validation, ** and Accreditation only. It works like ExprSetProperty() during VVA ** processes but is a no-op for delivery. */ /* ** Macros to determine the number of bytes required by a normal Expr ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags ** and an Expr struct with the EP_TokenOnly flag set. */ /* ** Flags passed to the sqlite3ExprDup() function. See the header comment ** above sqlite3ExprDup() for details. */ /* ** True if the expression passed as an argument was a function with ** an OVER() clause (a window function). */ /* ** A list of expressions. Each expression may optionally have a ** name. An expr/name combination can be used in several ways, such ** as the list of "expr AS ID" fields following a "SELECT" or in the ** list of "ID = expr" items in an UPDATE. A list of expressions can ** also be used as the argument to a function, in which case the a.zName ** field is not used. ** ** In order to try to keep memory usage down, the Expr.a.zEName field ** is used for multiple purposes: ** ** eEName Usage ** ---------- ------------------------- ** ENAME_NAME (1) the AS of result set column ** (2) COLUMN= of an UPDATE ** ** ENAME_TAB DB.TABLE.NAME used to resolve names ** of subqueries ** ** ENAME_SPAN Text of the original result set ** expression. */ type TExprList1 = struct { FnExpr int32 FnAlloc int32 Fa [1]TExprList_item } type ExprList1 = TExprList1 /* ** Allowed values for Expr.a.eEName */ /* ** An instance of this structure can hold a simple list of identifiers, ** such as the list "a,b,c" in the following statements: ** ** INSERT INTO t(a,b,c) VALUES ...; ** CREATE INDEX idx ON t(a,b,c); ** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...; ** ** The IdList.a.idx field is used when the IdList represents the list of ** column names after a table name in an INSERT statement. In the statement ** ** INSERT INTO t(a,b,c) ... ** ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k. */ type TIdList1 = struct { FnId int32 FeU4 Tu8 Fa [1]TIdList_item } type IdList1 = TIdList1 /* ** Allowed values for IdList.eType, which determines which value of the a.u4 ** is valid. */ /* ** The SrcItem object represents a single term in the FROM clause of a query. ** The SrcList object is mostly an array of SrcItems. ** ** The jointype starts out showing the join type between the current table ** and the next table on the list. The parser builds the list this way. ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each ** jointype expresses the join between the table and the previous table. ** ** In the colUsed field, the high-order bit (bit 63) is set if the table ** contains more than 63 columns and the 64-th or later column is used. ** ** Union member validity: ** ** u1.zIndexedBy fg.isIndexedBy && !fg.isTabFunc ** u1.pFuncArg fg.isTabFunc && !fg.isIndexedBy ** u2.pIBIndex fg.isIndexedBy && !fg.isCte ** u2.pCteUse fg.isCte && !fg.isIndexedBy */ type TSrcItem1 = struct { F__ccgo_align [0]uint32 FpSchema uintptr FzDatabase uintptr FzName uintptr FzAlias uintptr FpTab uintptr FpSelect uintptr FaddrFillSub int32 FregReturn int32 FregResult int32 Ffg struct { F__ccgo_align [0]uint32 Fjointype Tu8 F__ccgo_align1 [2]byte F__ccgo4 uint16 } FiCursor int32 Fu3 struct { FpUsing [0]uintptr FpOn uintptr } F__ccgo_align12 [4]byte FcolUsed TBitmask Fu1 struct { FpFuncArg [0]uintptr FzIndexedBy uintptr } Fu2 struct { FpCteUse [0]uintptr FpIBIndex uintptr } } type SrcItem1 = TSrcItem1 /* ** The OnOrUsing object represents either an ON clause or a USING clause. ** It can never be both at the same time, but it can be neither. */ type TOnOrUsing1 = struct { FpOn uintptr FpUsing uintptr } type OnOrUsing1 = TOnOrUsing1 /* ** This object represents one or more tables that are the source of ** content for an SQL statement. For example, a single SrcList object ** is used to hold the FROM clause of a SELECT statement. SrcList also ** represents the target tables for DELETE, INSERT, and UPDATE statements. ** */ type TSrcList1 = struct { F__ccgo_align [0]uint32 FnSrc int32 FnAlloc Tu32 Fa [1]TSrcItem } type SrcList1 = TSrcList1 /* ** Permitted values of the SrcList.a.jointype field */ /* ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin() ** and the WhereInfo.wctrlFlags member. ** ** Value constraints (enforced via assert()): ** WHERE_USE_LIMIT == SF_FixedLimit */ /* 0x2000 not currently used */ /* 0x8000 not currently used */ /* Allowed return values from sqlite3WhereIsDistinct() */ /* ** A NameContext defines a context in which to resolve table and column ** names. The context consists of a list of tables (the pSrcList) field and ** a list of named expression (pEList). The named expression list may ** be NULL. The pSrc corresponds to the FROM clause of a SELECT or ** to the table being operated on by INSERT, UPDATE, or DELETE. The ** pEList corresponds to the result set of a SELECT and is NULL for ** other statements. ** ** NameContexts can be nested. When resolving names, the inner-most ** context is searched first. If no match is found, the next outer ** context is checked. If there is still no match, the next context ** is checked. This process continues until either a match is found ** or all contexts are check. When a match is found, the nRef member of ** the context containing the match is incremented. ** ** Each subquery gets a new NameContext. The pNext field points to the ** NameContext in the parent query. Thus the process of scanning the ** NameContext list corresponds to searching through successively outer ** subqueries looking for a match. */ type TNameContext1 = struct { FpParse uintptr FpSrcList uintptr FuNC struct { FpAggInfo [0]uintptr FpUpsert [0]uintptr FiBaseReg [0]int32 FpEList uintptr } FpNext uintptr FnRef int32 FnNcErr int32 FncFlags int32 FnNestedSelect Tu32 FpWinSelect uintptr } type NameContext1 = TNameContext1 /* ** Allowed values for the NameContext, ncFlags field. ** ** Value constraints (all checked via assert()): ** NC_HasAgg == SF_HasAgg == EP_Agg ** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX ** NC_OrderAgg == SF_OrderByReqd == SQLITE_FUNC_ANYORDER ** NC_HasWin == EP_Win ** */ /* ** An instance of the following object describes a single ON CONFLICT ** clause in an upsert. ** ** The pUpsertTarget field is only set if the ON CONFLICT clause includes ** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the ** conflict-target clause.) The pUpsertTargetWhere is the optional ** WHERE clause used to identify partial unique indexes. ** ** pUpsertSet is the list of column=expr terms of the UPDATE statement. ** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The ** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the ** WHERE clause is omitted. */ type TUpsert1 = struct { FpUpsertTarget uintptr FpUpsertTargetWhere uintptr FpUpsertSet uintptr FpUpsertWhere uintptr FpNextUpsert uintptr FisDoUpdate Tu8 FisDup Tu8 FpToFree uintptr FpUpsertIdx uintptr FpUpsertSrc uintptr FregData int32 FiDataCur int32 FiIdxCur int32 } type Upsert1 = TUpsert1 /* ** An instance of the following structure contains all information ** needed to generate code for a single SELECT statement. ** ** See the header comment on the computeLimitRegisters() routine for a ** detailed description of the meaning of the iLimit and iOffset fields. ** ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes. ** These addresses must be stored so that we can go back and fill in ** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor ** the number of columns in P2 can be computed at the same time ** as the OP_OpenEphm instruction is coded because not ** enough information about the compound query is known at that point. ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences ** for the result set. The KeyInfo for addrOpenEphm[2] contains collating ** sequences for the ORDER BY clause. */ type TSelect1 = struct { Fop Tu8 FnSelectRow TLogEst FselFlags Tu32 FiLimit int32 FiOffset int32 FselId Tu32 FaddrOpenEphm [2]int32 FpEList uintptr FpSrc uintptr FpWhere uintptr FpGroupBy uintptr FpHaving uintptr FpOrderBy uintptr FpPrior uintptr FpNext uintptr FpLimit uintptr FpWith uintptr FpWin uintptr FpWinDefn uintptr } type Select1 = TSelect1 /* ** Allowed values for Select.selFlags. The "SF" prefix stands for ** "Select Flag". ** ** Value constraints (all checked via assert()) ** SF_HasAgg == NC_HasAgg ** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX ** SF_OrderByReqd == NC_OrderAgg == SQLITE_FUNC_ANYORDER ** SF_FixedLimit == WHERE_USE_LIMIT */ /* True if S exists and has SF_NestedFrom */ /* ** The results of a SELECT can be distributed in several ways, as defined ** by one of the following macros. The "SRT" prefix means "SELECT Result ** Type". ** ** SRT_Union Store results as a key in a temporary index ** identified by pDest->iSDParm. ** ** SRT_Except Remove results from the temporary index pDest->iSDParm. ** ** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result ** set is not empty. ** ** SRT_Discard Throw the results away. This is used by SELECT ** statements within triggers whose only purpose is ** the side-effects of functions. ** ** SRT_Output Generate a row of output (using the OP_ResultRow ** opcode) for each row in the result set. ** ** SRT_Mem Only valid if the result is a single column. ** Store the first column of the first result row ** in register pDest->iSDParm then abandon the rest ** of the query. This destination implies "LIMIT 1". ** ** SRT_Set The result must be a single column. Store each ** row of result as the key in table pDest->iSDParm. ** Apply the affinity pDest->affSdst before storing ** results. Used to implement "IN (SELECT ...)". ** ** SRT_EphemTab Create an temporary table pDest->iSDParm and store ** the result there. The cursor is left open after ** returning. This is like SRT_Table except that ** this destination uses OP_OpenEphemeral to create ** the table first. ** ** SRT_Coroutine Generate a co-routine that returns a new row of ** results each time it is invoked. The entry point ** of the co-routine is stored in register pDest->iSDParm ** and the result row is stored in pDest->nDest registers ** starting with pDest->iSdst. ** ** SRT_Table Store results in temporary table pDest->iSDParm. ** SRT_Fifo This is like SRT_EphemTab except that the table ** is assumed to already be open. SRT_Fifo has ** the additional property of being able to ignore ** the ORDER BY clause. ** ** SRT_DistFifo Store results in a temporary table pDest->iSDParm. ** But also use temporary table pDest->iSDParm+1 as ** a record of all prior results and ignore any duplicate ** rows. Name means: "Distinct Fifo". ** ** SRT_Queue Store results in priority queue pDest->iSDParm (really ** an index). Append a sequence number so that all entries ** are distinct. ** ** SRT_DistQueue Store results in priority queue pDest->iSDParm only if ** the same record has never been stored before. The ** index at pDest->iSDParm+1 hold all prior stores. ** ** SRT_Upfrom Store results in the temporary table already opened by ** pDest->iSDParm. If (pDest->iSDParm<0), then the temp ** table is an intkey table - in this case the first ** column returned by the SELECT is used as the integer ** key. If (pDest->iSDParm>0), then the table is an index ** table. (pDest->iSDParm) is the number of key columns in ** each index record in this case. */ /* The DISTINCT clause is ignored for all of the above. Not that ** IgnorableDistinct() implies IgnorableOrderby() */ /* The ORDER BY clause is ignored for all of the above */ /* ** An instance of this object describes where to put of the results of ** a SELECT statement. */ type TSelectDest1 = struct { FeDest Tu8 FiSDParm int32 FiSDParm2 int32 FiSdst int32 FnSdst int32 FzAffSdst uintptr FpOrderBy uintptr } type SelectDest1 = TSelectDest1 /* ** During code generation of statements that do inserts into AUTOINCREMENT ** tables, the following information is attached to the Table.u.autoInc.p ** pointer of each autoincrement table to record some side information that ** the code generator needs. We have to keep per-table autoincrement ** information in case inserts are done within triggers. Triggers do not ** normally coordinate their activities, but we do need to coordinate the ** loading and saving of autoincrement information. */ type TAutoincInfo1 = struct { FpNext uintptr FpTab uintptr FiDb int32 FregCtr int32 } type AutoincInfo1 = TAutoincInfo1 /* ** At least one instance of the following structure is created for each ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE ** statement. All such objects are stored in the linked list headed at ** Parse.pTriggerPrg and deleted once statement compilation has been ** completed. ** ** A Vdbe sub-program that implements the body and WHEN clause of trigger ** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of ** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable. ** The Parse.pTriggerPrg list never contains two entries with the same ** values for both pTrigger and orconf. ** ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns ** accessed (or set to 0 for triggers fired as a result of INSERT ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to ** a mask of new.* columns used by the program. */ type TTriggerPrg1 = struct { FpTrigger uintptr FpNext uintptr FpProgram uintptr Forconf int32 FaColmask [2]Tu32 } type TriggerPrg1 = TTriggerPrg1 // C documentation // // /* // ** The yDbMask datatype for the bitmask of all attached databases. // */ type TyDbMask = uint32 type yDbMask = TyDbMask /* ** For each index X that has as one of its arguments either an expression ** or the name of a virtual generated column, and if X is in scope such that ** the value of the expression can simply be read from the index, then ** there is an instance of this object on the Parse.pIdxExpr list. ** ** During code generation, while generating code to evaluate expressions, ** this list is consulted and if a matching expression is found, the value ** is read from the index rather than being recomputed. */ type TIndexedExpr1 = struct { FpExpr uintptr FiDataCur int32 FiIdxCur int32 FiIdxCol int32 FbMaybeNullRow Tu8 Faff Tu8 FpIENext uintptr } type IndexedExpr1 = TIndexedExpr1 /* ** An instance of the ParseCleanup object specifies an operation that ** should be performed after parsing to deallocation resources obtained ** during the parse and which are no longer needed. */ type TParseCleanup1 = struct { FpNext uintptr FpPtr uintptr FxCleanup uintptr } type ParseCleanup1 = TParseCleanup1 /* ** An SQL parser context. A copy of this structure is passed through ** the parser and down into all the parser action routine in order to ** carry around information that is global to the entire parse. ** ** The structure is divided into two parts. When the parser and code ** generate call themselves recursively, the first part of the structure ** is constant but the second part is reset at the beginning and end of ** each recursion. ** ** The nTableLock and aTableLock variables are only used if the shared-cache ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are ** used to store the set of table-locks required by the statement being ** compiled. Function sqlite3TableLock() is used to add entries to the ** list. */ type TParse1 = struct { Fdb uintptr FzErrMsg uintptr FpVdbe uintptr Frc int32 FcolNamesSet Tu8 FcheckSchema Tu8 Fnested Tu8 FnTempReg Tu8 FisMultiWrite Tu8 FmayAbort Tu8 FhasCompound Tu8 FokConstFactor Tu8 FdisableLookaside Tu8 FprepFlags Tu8 FwithinRJSubrtn Tu8 FnRangeReg int32 FiRangeReg int32 FnErr int32 FnTab int32 FnMem int32 FszOpAlloc int32 FiSelfTab int32 FnLabel int32 FnLabelAlloc int32 FaLabel uintptr FpConstExpr uintptr FpIdxEpr uintptr FpIdxPartExpr uintptr FconstraintName TToken FwriteMask TyDbMask FcookieMask TyDbMask FregRowid int32 FregRoot int32 FnMaxArg int32 FnSelect int32 FnProgressSteps Tu32 FnTableLock int32 FaTableLock uintptr FpAinc uintptr FpToplevel uintptr FpTriggerTab uintptr FpTriggerPrg uintptr FpCleanup uintptr Fu1 struct { FpReturning [0]uintptr FaddrCrTab int32 } Foldmask Tu32 Fnewmask Tu32 FnQueryLoop TLogEst FeTriggerOp Tu8 FbReturning Tu8 FeOrconf Tu8 FdisableTriggers Tu8 FaTempReg [8]int32 FpOuterParse uintptr FsNameToken TToken FsLastToken TToken FnVar TynVar FiPkSortOrder Tu8 Fexplain Tu8 FeParseMode Tu8 FnVtabLock int32 FnHeight int32 FaddrExplain int32 FpVList uintptr FpReprepare uintptr FzTail uintptr FpNewTable uintptr FpNewIndex uintptr FpNewTrigger uintptr FzAuthContext uintptr FsArg TToken FapVtabLock uintptr FpWith uintptr FpRename uintptr } type Parse1 = TParse1 /* Allowed values for Parse.eParseMode */ /* ** Sizes and pointers of various parts of the Parse object. */ /* ** Return true if currently inside an sqlite3_declare_vtab() call. */ /* ** An instance of the following structure can be declared on a stack and used ** to save the Parse.zAuthContext value so that it can be restored later. */ type TAuthContext1 = struct { FzAuthContext uintptr FpParse uintptr } type AuthContext1 = TAuthContext1 /* ** Bitfield flags for P5 value in various opcodes. ** ** Value constraints (enforced via assert()): ** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH ** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF ** OPFLAG_BULKCSR == BTREE_BULKLOAD ** OPFLAG_SEEKEQ == BTREE_SEEK_EQ ** OPFLAG_FORDELETE == BTREE_FORDELETE ** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION ** OPFLAG_AUXDELETE == BTREE_AUXDELETE */ /* Also used in P2 (not P5) of OP_Delete */ /* ** Each trigger present in the database schema is stored as an instance of ** struct Trigger. ** ** Pointers to instances of struct Trigger are stored in two ways. ** 1. In the "trigHash" hash table (part of the sqlite3* that represents the ** database). This allows Trigger structures to be retrieved by name. ** 2. All triggers associated with a single table form a linked list, using the ** pNext member of struct Trigger. A pointer to the first element of the ** linked list is stored as the "pTrigger" member of the associated ** struct Table. ** ** The "step_list" member points to the first element of a linked list ** containing the SQL statements specified as the trigger program. */ type TTrigger1 = struct { FzName uintptr Ftable uintptr Fop Tu8 Ftr_tm Tu8 FbReturning Tu8 FpWhen uintptr FpColumns uintptr FpSchema uintptr FpTabSchema uintptr Fstep_list uintptr FpNext uintptr } type Trigger1 = TTrigger1 /* ** A trigger is either a BEFORE or an AFTER trigger. The following constants ** determine which. ** ** If there are multiple triggers, you might of some BEFORE and some AFTER. ** In that cases, the constants below can be ORed together. */ /* ** An instance of struct TriggerStep is used to store a single SQL statement ** that is a part of a trigger-program. ** ** Instances of struct TriggerStep are stored in a singly linked list (linked ** using the "pNext" member) referenced by the "step_list" member of the ** associated struct Trigger instance. The first element of the linked list is ** the first step of the trigger-program. ** ** The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or ** "SELECT" statement. The meanings of the other members is determined by the ** value of "op" as follows: ** ** (op == TK_INSERT) ** orconf -> stores the ON CONFLICT algorithm ** pSelect -> The content to be inserted - either a SELECT statement or ** a VALUES clause. ** zTarget -> Dequoted name of the table to insert into. ** pIdList -> If this is an INSERT INTO ... () VALUES ... ** statement, then this stores the column-names to be ** inserted into. ** pUpsert -> The ON CONFLICT clauses for an Upsert ** ** (op == TK_DELETE) ** zTarget -> Dequoted name of the table to delete from. ** pWhere -> The WHERE clause of the DELETE statement if one is specified. ** Otherwise NULL. ** ** (op == TK_UPDATE) ** zTarget -> Dequoted name of the table to update. ** pWhere -> The WHERE clause of the UPDATE statement if one is specified. ** Otherwise NULL. ** pExprList -> A list of the columns to update and the expressions to update ** them to. See sqlite3Update() documentation of "pChanges" ** argument. ** ** (op == TK_SELECT) ** pSelect -> The SELECT statement ** ** (op == TK_RETURNING) ** pExprList -> The list of expressions that follow the RETURNING keyword. ** */ type TTriggerStep1 = struct { Fop Tu8 Forconf Tu8 FpTrig uintptr FpSelect uintptr FzTarget uintptr FpFrom uintptr FpWhere uintptr FpExprList uintptr FpIdList uintptr FpUpsert uintptr FzSpan uintptr FpNext uintptr FpLast uintptr } type TriggerStep1 = TTriggerStep1 /* ** Information about a RETURNING clause */ type TReturning1 = struct { FpParse uintptr FpReturnEL uintptr FretTrig TTrigger FretTStep TTriggerStep FiRetCur int32 FnRetCol int32 FiRetReg int32 FzName [40]uint8 } type Returning1 = TReturning1 /* ** The following object is the header for an "RCStr" or "reference-counted ** string". An RCStr is passed around and used like any other char* ** that has been dynamically allocated. The important interface ** differences: ** ** 1. RCStr strings are reference counted. They are deallocated ** when the reference count reaches zero. ** ** 2. Use sqlite3RCStrUnref() to free an RCStr string rather than ** sqlite3_free() ** ** 3. Make a (read-only) copy of a read-only RCStr string using ** sqlite3RCStrRef(). ** ** "String" is in the name, but an RCStr object can also be used to hold ** binary data. */ type TRCStr1 = struct { F__ccgo_align [0]uint32 FnRCRef Tu64 } type RCStr1 = TRCStr1 // C documentation // // /* // ** A pointer to this structure is used to communicate information // ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback. // */ type TInitData = struct { Fdb uintptr FpzErrMsg uintptr FiDb int32 Frc int32 FmInitFlags Tu32 FnInitRow Tu32 FmxPage TPgno } type InitData = TInitData /* ** Allowed values for mInitFlags */ /* Tuning parameters are set using SQLITE_TESTCTRL_TUNE and are controlled ** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning ** parameters are for temporary use during development, to help find ** optimal values for parameters in the query planner. The should not ** be used on trunk check-ins. They are a temporary mechanism available ** for transient development builds only. ** ** Tuning parameters are numbered starting with 1. */ /* ** Structure containing global configuration data for the SQLite library. ** ** This structure also contains some state information. */ type TSqlite3Config = struct { F__ccgo_align [0]uint32 FbMemstat int32 FbCoreMutex Tu8 FbFullMutex Tu8 FbOpenUri Tu8 FbUseCis Tu8 FbSmallMalloc Tu8 FbExtraSchemaChecks Tu8 FbUseLongDouble Tu8 FmxStrlen int32 FneverCorrupt int32 FszLookaside int32 FnLookaside int32 FnStmtSpill int32 Fm Tsqlite3_mem_methods Fmutex Tsqlite3_mutex_methods Fpcache2 Tsqlite3_pcache_methods2 FpHeap uintptr FnHeap int32 FmnReq int32 FmxReq int32 FszMmap Tsqlite3_int64 FmxMmap Tsqlite3_int64 FpPage uintptr FszPage int32 FnPage int32 FmxParserStack int32 FsharedCacheEnabled int32 FszPma Tu32 FisInit int32 FinProgress int32 FisMutexInit int32 FisMallocInit int32 FisPCacheInit int32 FnRefInitMutex int32 FpInitMutex uintptr FxLog uintptr FpLogArg uintptr F__ccgo_align37 [4]byte FmxMemdbSize Tsqlite3_int64 FxTestCallback uintptr FbLocaltimeFault int32 FxAltLocaltime uintptr FiOnceResetThreshold int32 FszSorterRef Tu32 FiPrngSeed uint32 } type Sqlite3Config = TSqlite3Config /* ** This macro is used inside of assert() statements to indicate that ** the assert is only valid on a well-formed database. Instead of: ** ** assert( X ); ** ** One writes: ** ** assert( X || CORRUPT_DB ); ** ** CORRUPT_DB is true during normal operation. CORRUPT_DB does not indicate ** that the database is definitely corrupt, only that it might be corrupt. ** For most test cases, CORRUPT_DB is set to false using a special ** sqlite3_test_control(). This enables assert() statements to prove ** things that are always true for well-formed databases. */ /* ** Context pointer passed down through the tree-walk. */ type TWalker1 = struct { FpParse uintptr FxExprCallback uintptr FxSelectCallback uintptr FxSelectCallback2 uintptr FwalkerDepth int32 FeCode Tu16 FmWFlags Tu16 Fu struct { Fn [0]int32 FiCur [0]int32 FpSrcList [0]uintptr FpCCurHint [0]uintptr FpRefSrcList [0]uintptr FaiCol [0]uintptr FpIdxCover [0]uintptr FpGroupBy [0]uintptr FpSelect [0]uintptr FpRewrite [0]uintptr FpConst [0]uintptr FpRename [0]uintptr FpTab [0]uintptr FpCovIdxCk [0]uintptr FpSrcItem [0]uintptr FpFix [0]uintptr FaMem [0]uintptr FpNC uintptr } } type Walker1 = TWalker1 /* ** The following structure contains information used by the sqliteFix... ** routines as they walk the parse tree to make database references ** explicit. */ type TDbFixer1 = struct { FpParse uintptr Fw TWalker FpSchema uintptr FbTemp Tu8 FzDb uintptr FzType uintptr FpName uintptr } type DbFixer1 = TDbFixer1 /* ** Return code from the parse-tree walking primitives and their ** callbacks. */ /* ** A single common table expression */ type TCte1 = struct { FzName uintptr FpCols uintptr FpSelect uintptr FzCteErr uintptr FpUse uintptr FeM10d Tu8 } type Cte1 = TCte1 /* ** Allowed values for the materialized flag (eM10d): */ /* ** An instance of the With object represents a WITH clause containing ** one or more CTEs (common table expressions). */ type TWith1 = struct { FnCte int32 FbView int32 FpOuter uintptr Fa [1]TCte } type With1 = TWith1 /* ** The Cte object is not guaranteed to persist for the entire duration ** of code generation. (The query flattener or other parser tree ** edits might delete it.) The following object records information ** about each Common Table Expression that must be preserved for the ** duration of the parse. ** ** The CteUse objects are freed using sqlite3ParserAddCleanup() rather ** than sqlite3SelectDelete(), which is what enables them to persist ** until the end of code generation. */ type TCteUse1 = struct { FnUse int32 FaddrM9e int32 FregRtn int32 FiCur int32 FnRowEst TLogEst FeM10d Tu8 } type CteUse1 = TCteUse1 /* Client data associated with sqlite3_set_clientdata() and ** sqlite3_get_clientdata(). */ type TDbClientData1 = struct { FpNext uintptr FpData uintptr FxDestructor uintptr FzName [1]uint8 } type DbClientData1 = TDbClientData1 /* ** This object is used in various ways, most (but not all) related to window ** functions. ** ** (1) A single instance of this structure is attached to the ** the Expr.y.pWin field for each window function in an expression tree. ** This object holds the information contained in the OVER clause, ** plus additional fields used during code generation. ** ** (2) All window functions in a single SELECT form a linked-list ** attached to Select.pWin. The Window.pFunc and Window.pExpr ** fields point back to the expression that is the window function. ** ** (3) The terms of the WINDOW clause of a SELECT are instances of this ** object on a linked list attached to Select.pWinDefn. ** ** (4) For an aggregate function with a FILTER clause, an instance ** of this object is stored in Expr.y.pWin with eFrmType set to ** TK_FILTER. In this case the only field used is Window.pFilter. ** ** The uses (1) and (2) are really the same Window object that just happens ** to be accessible in two different ways. Use case (3) are separate objects. */ type TWindow1 = struct { FzName uintptr FzBase uintptr FpPartition uintptr FpOrderBy uintptr FeFrmType Tu8 FeStart Tu8 FeEnd Tu8 FbImplicitFrame Tu8 FeExclude Tu8 FpStart uintptr FpEnd uintptr FppThis uintptr FpNextWin uintptr FpFilter uintptr FpWFunc uintptr FiEphCsr int32 FregAccum int32 FregResult int32 FcsrApp int32 FregApp int32 FregPart int32 FpOwner uintptr FnBufferCol int32 FiArgCol int32 FregOne int32 FregStartRowid int32 FregEndRowid int32 FbExprArgs Tu8 } type Window1 = TWindow1 /* ** An instance of the following structure holds information about SQL ** functions arguments that are the parameters to the printf() function. */ type TPrintfArguments1 = struct { FnArg int32 FnUsed int32 FapArg uintptr } type PrintfArguments1 = TPrintfArguments1 /* ** An instance of this object receives the decoding of a floating point ** value into an approximate decimal representation. */ type TFpDecode1 = struct { Fsign uint8 FisSpecial uint8 Fn int32 FiDP int32 Fz uintptr FzBuf [24]uint8 } type FpDecode1 = TFpDecode1 /************** End of sqliteInt.h *******************************************/ /************** Begin file os_common.h ***************************************/ /* ** 2004 May 22 ** ** 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 contains macros and a little bit of code that is common to ** all of the platform-specific files (os_*.c) and is #included into those ** files. ** ** This file should be #included by the os_*.c files only. It is not a ** general purpose header file. */ /* ** At least two bugs have slipped in because we changed the MEMORY_DEBUG ** macro to SQLITE_DEBUG and some older makefiles have not yet made the ** switch. The following code should catch this problem at compile-time. */ /* ** Macros for performance tracing. Normally turned off. Only works ** on i486 hardware. */ /* ** If we compile with the SQLITE_TEST macro set, then the following block ** of code will give us the ability to simulate a disk I/O error. This ** is used for testing the I/O recovery logic. */ /* ** When testing, keep a count of the number of open files. */ /************** End of os_common.h *******************************************/ /************** Begin file ctime.c *******************************************/ /* DO NOT EDIT! ** This file is automatically generated by the script in the canonical ** SQLite source tree at tool/mkctimec.tcl. ** ** To modify this header, edit any of the various lists in that script ** which specify categories of generated conditionals in this file. */ /* ** 2010 February 23 ** ** 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 implements routines used to report what compile-time options ** SQLite was built with. */ /* ** Include the configuration header output by 'configure' if we're using the ** autoconf-based build */ /* These macros are provided to "stringify" the value of the define ** for those options in which the value is meaningful. */ /* Like CTIMEOPT_VAL, but especially for SQLITE_DEFAULT_LOOKASIDE. This ** option requires a separate macro because legal values contain a single ** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** An array of names of all compile-time options. This array should // ** be sorted A-Z. // ** // ** This array looks large, but in a typical installation actually uses // ** only a handful of compile-time options, so most times this array is usually // ** rather short and uses little memory space. // */ var _sqlite3azCompileOpt = [54]uintptr{ 0: __ccgo_ts, 1: __ccgo_ts + 20, 2: __ccgo_ts + 40, 3: __ccgo_ts + 59, 4: __ccgo_ts + 84, 5: __ccgo_ts + 106, 6: __ccgo_ts + 136, 7: __ccgo_ts + 156, 8: __ccgo_ts + 176, 9: __ccgo_ts + 199, 10: __ccgo_ts + 224, 11: __ccgo_ts + 251, 12: __ccgo_ts + 276, 13: __ccgo_ts + 298, 14: __ccgo_ts + 330, 15: __ccgo_ts + 356, 16: __ccgo_ts + 381, 17: __ccgo_ts + 402, 18: __ccgo_ts + 425, 19: __ccgo_ts + 444, 20: __ccgo_ts + 456, 21: __ccgo_ts + 471, 22: __ccgo_ts + 493, 23: __ccgo_ts + 518, 24: __ccgo_ts + 541, 25: __ccgo_ts + 563, 26: __ccgo_ts + 574, 27: __ccgo_ts + 587, 28: __ccgo_ts + 602, 29: __ccgo_ts + 618, 30: __ccgo_ts + 631, 31: __ccgo_ts + 652, 32: __ccgo_ts + 676, 33: __ccgo_ts + 699, 34: __ccgo_ts + 715, 35: __ccgo_ts + 731, 36: __ccgo_ts + 755, 37: __ccgo_ts + 782, 38: __ccgo_ts + 802, 39: __ccgo_ts + 823, 40: __ccgo_ts + 845, 41: __ccgo_ts + 875, 42: __ccgo_ts + 900, 43: __ccgo_ts + 926, 44: __ccgo_ts + 946, 45: __ccgo_ts + 972, 46: __ccgo_ts + 995, 47: __ccgo_ts + 1021, 48: __ccgo_ts + 1043, 49: __ccgo_ts + 1064, 50: __ccgo_ts + 1075, 51: __ccgo_ts + 1083, 52: __ccgo_ts + 1097, 53: __ccgo_ts + 1110, } func _sqlite3CompileOptions(tls *libc.TLS, pnOpt uintptr) (r uintptr) { *(*int32)(unsafe.Pointer(pnOpt)) = int32(libc.Uint32FromInt64(216) / libc.Uint32FromInt64(4)) return uintptr(unsafe.Pointer(&_sqlite3azCompileOpt)) } /************** End of ctime.c ***********************************************/ /************** Begin file global.c ******************************************/ /* ** 2008 June 13 ** ** 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 contains definitions of global variables and constants. */ /* #include "sqliteInt.h" */ // C documentation // // /* An array to map all upper-case characters into their corresponding // ** lower-case character. // ** // ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not // ** handle case conversions for the UTF character set since the tables // ** involved are nearly as big or bigger than SQLite itself. // */ var _sqlite3UpperToLower = [274]uint8{ 1: uint8(1), 2: uint8(2), 3: uint8(3), 4: uint8(4), 5: uint8(5), 6: uint8(6), 7: uint8(7), 8: uint8(8), 9: uint8(9), 10: uint8(10), 11: uint8(11), 12: uint8(12), 13: uint8(13), 14: uint8(14), 15: uint8(15), 16: uint8(16), 17: uint8(17), 18: uint8(18), 19: uint8(19), 20: uint8(20), 21: uint8(21), 22: uint8(22), 23: uint8(23), 24: uint8(24), 25: uint8(25), 26: uint8(26), 27: uint8(27), 28: uint8(28), 29: uint8(29), 30: uint8(30), 31: uint8(31), 32: uint8(32), 33: uint8(33), 34: uint8(34), 35: uint8(35), 36: uint8(36), 37: uint8(37), 38: uint8(38), 39: uint8(39), 40: uint8(40), 41: uint8(41), 42: uint8(42), 43: uint8(43), 44: uint8(44), 45: uint8(45), 46: uint8(46), 47: uint8(47), 48: uint8(48), 49: uint8(49), 50: uint8(50), 51: uint8(51), 52: uint8(52), 53: uint8(53), 54: uint8(54), 55: uint8(55), 56: uint8(56), 57: uint8(57), 58: uint8(58), 59: uint8(59), 60: uint8(60), 61: uint8(61), 62: uint8(62), 63: uint8(63), 64: uint8(64), 65: uint8(97), 66: uint8(98), 67: uint8(99), 68: uint8(100), 69: uint8(101), 70: uint8(102), 71: uint8(103), 72: uint8(104), 73: uint8(105), 74: uint8(106), 75: uint8(107), 76: uint8(108), 77: uint8(109), 78: uint8(110), 79: uint8(111), 80: uint8(112), 81: uint8(113), 82: uint8(114), 83: uint8(115), 84: uint8(116), 85: uint8(117), 86: uint8(118), 87: uint8(119), 88: uint8(120), 89: uint8(121), 90: uint8(122), 91: uint8(91), 92: uint8(92), 93: uint8(93), 94: uint8(94), 95: uint8(95), 96: uint8(96), 97: uint8(97), 98: uint8(98), 99: uint8(99), 100: uint8(100), 101: uint8(101), 102: uint8(102), 103: uint8(103), 104: uint8(104), 105: uint8(105), 106: uint8(106), 107: uint8(107), 108: uint8(108), 109: uint8(109), 110: uint8(110), 111: uint8(111), 112: uint8(112), 113: uint8(113), 114: uint8(114), 115: uint8(115), 116: uint8(116), 117: uint8(117), 118: uint8(118), 119: uint8(119), 120: uint8(120), 121: uint8(121), 122: uint8(122), 123: uint8(123), 124: uint8(124), 125: uint8(125), 126: uint8(126), 127: uint8(127), 128: uint8(128), 129: uint8(129), 130: uint8(130), 131: uint8(131), 132: uint8(132), 133: uint8(133), 134: uint8(134), 135: uint8(135), 136: uint8(136), 137: uint8(137), 138: uint8(138), 139: uint8(139), 140: uint8(140), 141: uint8(141), 142: uint8(142), 143: uint8(143), 144: uint8(144), 145: uint8(145), 146: uint8(146), 147: uint8(147), 148: uint8(148), 149: uint8(149), 150: uint8(150), 151: uint8(151), 152: uint8(152), 153: uint8(153), 154: uint8(154), 155: uint8(155), 156: uint8(156), 157: uint8(157), 158: uint8(158), 159: uint8(159), 160: uint8(160), 161: uint8(161), 162: uint8(162), 163: uint8(163), 164: uint8(164), 165: uint8(165), 166: uint8(166), 167: uint8(167), 168: uint8(168), 169: uint8(169), 170: uint8(170), 171: uint8(171), 172: uint8(172), 173: uint8(173), 174: uint8(174), 175: uint8(175), 176: uint8(176), 177: uint8(177), 178: uint8(178), 179: uint8(179), 180: uint8(180), 181: uint8(181), 182: uint8(182), 183: uint8(183), 184: uint8(184), 185: uint8(185), 186: uint8(186), 187: uint8(187), 188: uint8(188), 189: uint8(189), 190: uint8(190), 191: uint8(191), 192: uint8(192), 193: uint8(193), 194: uint8(194), 195: uint8(195), 196: uint8(196), 197: uint8(197), 198: uint8(198), 199: uint8(199), 200: uint8(200), 201: uint8(201), 202: uint8(202), 203: uint8(203), 204: uint8(204), 205: uint8(205), 206: uint8(206), 207: uint8(207), 208: uint8(208), 209: uint8(209), 210: uint8(210), 211: uint8(211), 212: uint8(212), 213: uint8(213), 214: uint8(214), 215: uint8(215), 216: uint8(216), 217: uint8(217), 218: uint8(218), 219: uint8(219), 220: uint8(220), 221: uint8(221), 222: uint8(222), 223: uint8(223), 224: uint8(224), 225: uint8(225), 226: uint8(226), 227: uint8(227), 228: uint8(228), 229: uint8(229), 230: uint8(230), 231: uint8(231), 232: uint8(232), 233: uint8(233), 234: uint8(234), 235: uint8(235), 236: uint8(236), 237: uint8(237), 238: uint8(238), 239: uint8(239), 240: uint8(240), 241: uint8(241), 242: uint8(242), 243: uint8(243), 244: uint8(244), 245: uint8(245), 246: uint8(246), 247: uint8(247), 248: uint8(248), 249: uint8(249), 250: uint8(250), 251: uint8(251), 252: uint8(252), 253: uint8(253), 254: uint8(254), 255: uint8(255), 256: uint8(1), 259: uint8(1), 260: uint8(1), 263: uint8(1), 265: uint8(1), 267: uint8(1), 268: uint8(1), 270: uint8(1), 273: uint8(1), } var _sqlite3aLTb = uintptr(unsafe.Pointer(&_sqlite3UpperToLower)) + uintptr(libc.Int32FromInt32(256)-libc.Int32FromInt32(OP_Ne)) var _sqlite3aEQb = uintptr(unsafe.Pointer(&_sqlite3UpperToLower)) + uintptr(libc.Int32FromInt32(256)+libc.Int32FromInt32(6)-libc.Int32FromInt32(OP_Ne)) var _sqlite3aGTb = uintptr(unsafe.Pointer(&_sqlite3UpperToLower)) + uintptr(libc.Int32FromInt32(256)+libc.Int32FromInt32(12)-libc.Int32FromInt32(OP_Ne)) // C documentation // // /* // ** The following 256 byte lookup table is used to support SQLites built-in // ** equivalents to the following standard library functions: // ** // ** isspace() 0x01 // ** isalpha() 0x02 // ** isdigit() 0x04 // ** isalnum() 0x06 // ** isxdigit() 0x08 // ** toupper() 0x20 // ** SQLite identifier character 0x40 $, _, or non-ascii // ** Quote character 0x80 // ** // ** Bit 0x20 is set if the mapped character requires translation to upper // ** case. i.e. if the character is a lower-case ASCII character. // ** If x is a lower-case ASCII character, then its upper-case equivalent // ** is (x - 0x20). Therefore toupper() can be implemented as: // ** // ** (x & ~(map[x]&0x20)) // ** // ** The equivalent of tolower() is implemented using the sqlite3UpperToLower[] // ** array. tolower() is used more often than toupper() by SQLite. // ** // ** Bit 0x40 is set if the character is non-alphanumeric and can be used in an // ** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any // ** non-ASCII UTF character. Hence the test for whether or not a character is // ** part of an identifier is 0x46. // */ var _sqlite3CtypeMap = [256]uint8{ 9: uint8(0x01), 10: uint8(0x01), 11: uint8(0x01), 12: uint8(0x01), 13: uint8(0x01), 32: uint8(0x01), 34: uint8(0x80), 36: uint8(0x40), 39: uint8(0x80), 48: uint8(0x0c), 49: uint8(0x0c), 50: uint8(0x0c), 51: uint8(0x0c), 52: uint8(0x0c), 53: uint8(0x0c), 54: uint8(0x0c), 55: uint8(0x0c), 56: uint8(0x0c), 57: uint8(0x0c), 65: uint8(0x0a), 66: uint8(0x0a), 67: uint8(0x0a), 68: uint8(0x0a), 69: uint8(0x0a), 70: uint8(0x0a), 71: uint8(0x02), 72: uint8(0x02), 73: uint8(0x02), 74: uint8(0x02), 75: uint8(0x02), 76: uint8(0x02), 77: uint8(0x02), 78: uint8(0x02), 79: uint8(0x02), 80: uint8(0x02), 81: uint8(0x02), 82: uint8(0x02), 83: uint8(0x02), 84: uint8(0x02), 85: uint8(0x02), 86: uint8(0x02), 87: uint8(0x02), 88: uint8(0x02), 89: uint8(0x02), 90: uint8(0x02), 91: uint8(0x80), 95: uint8(0x40), 96: uint8(0x80), 97: uint8(0x2a), 98: uint8(0x2a), 99: uint8(0x2a), 100: uint8(0x2a), 101: uint8(0x2a), 102: uint8(0x2a), 103: uint8(0x22), 104: uint8(0x22), 105: uint8(0x22), 106: uint8(0x22), 107: uint8(0x22), 108: uint8(0x22), 109: uint8(0x22), 110: uint8(0x22), 111: uint8(0x22), 112: uint8(0x22), 113: uint8(0x22), 114: uint8(0x22), 115: uint8(0x22), 116: uint8(0x22), 117: uint8(0x22), 118: uint8(0x22), 119: uint8(0x22), 120: uint8(0x22), 121: uint8(0x22), 122: uint8(0x22), 128: uint8(0x40), 129: uint8(0x40), 130: uint8(0x40), 131: uint8(0x40), 132: uint8(0x40), 133: uint8(0x40), 134: uint8(0x40), 135: uint8(0x40), 136: uint8(0x40), 137: uint8(0x40), 138: uint8(0x40), 139: uint8(0x40), 140: uint8(0x40), 141: uint8(0x40), 142: uint8(0x40), 143: uint8(0x40), 144: uint8(0x40), 145: uint8(0x40), 146: uint8(0x40), 147: uint8(0x40), 148: uint8(0x40), 149: uint8(0x40), 150: uint8(0x40), 151: uint8(0x40), 152: uint8(0x40), 153: uint8(0x40), 154: uint8(0x40), 155: uint8(0x40), 156: uint8(0x40), 157: uint8(0x40), 158: uint8(0x40), 159: uint8(0x40), 160: uint8(0x40), 161: uint8(0x40), 162: uint8(0x40), 163: uint8(0x40), 164: uint8(0x40), 165: uint8(0x40), 166: uint8(0x40), 167: uint8(0x40), 168: uint8(0x40), 169: uint8(0x40), 170: uint8(0x40), 171: uint8(0x40), 172: uint8(0x40), 173: uint8(0x40), 174: uint8(0x40), 175: uint8(0x40), 176: uint8(0x40), 177: uint8(0x40), 178: uint8(0x40), 179: uint8(0x40), 180: uint8(0x40), 181: uint8(0x40), 182: uint8(0x40), 183: uint8(0x40), 184: uint8(0x40), 185: uint8(0x40), 186: uint8(0x40), 187: uint8(0x40), 188: uint8(0x40), 189: uint8(0x40), 190: uint8(0x40), 191: uint8(0x40), 192: uint8(0x40), 193: uint8(0x40), 194: uint8(0x40), 195: uint8(0x40), 196: uint8(0x40), 197: uint8(0x40), 198: uint8(0x40), 199: uint8(0x40), 200: uint8(0x40), 201: uint8(0x40), 202: uint8(0x40), 203: uint8(0x40), 204: uint8(0x40), 205: uint8(0x40), 206: uint8(0x40), 207: uint8(0x40), 208: uint8(0x40), 209: uint8(0x40), 210: uint8(0x40), 211: uint8(0x40), 212: uint8(0x40), 213: uint8(0x40), 214: uint8(0x40), 215: uint8(0x40), 216: uint8(0x40), 217: uint8(0x40), 218: uint8(0x40), 219: uint8(0x40), 220: uint8(0x40), 221: uint8(0x40), 222: uint8(0x40), 223: uint8(0x40), 224: uint8(0x40), 225: uint8(0x40), 226: uint8(0x40), 227: uint8(0x40), 228: uint8(0x40), 229: uint8(0x40), 230: uint8(0x40), 231: uint8(0x40), 232: uint8(0x40), 233: uint8(0x40), 234: uint8(0x40), 235: uint8(0x40), 236: uint8(0x40), 237: uint8(0x40), 238: uint8(0x40), 239: uint8(0x40), 240: uint8(0x40), 241: uint8(0x40), 242: uint8(0x40), 243: uint8(0x40), 244: uint8(0x40), 245: uint8(0x40), 246: uint8(0x40), 247: uint8(0x40), 248: uint8(0x40), 249: uint8(0x40), 250: uint8(0x40), 251: uint8(0x40), 252: uint8(0x40), 253: uint8(0x40), 254: uint8(0x40), 255: uint8(0x40), } /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards ** compatibility for legacy applications, the URI filename capability is ** disabled by default. ** ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options. ** ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally ** disabled. The default value may be changed by compiling with the ** SQLITE_USE_URI symbol defined. */ /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if ** that compile-time option is omitted. */ /* The minimum PMA size is set to this value multiplied by the database ** page size in bytes. */ /* Statement journals spill to disk when their size exceeds the following ** threshold (in bytes). 0 means that statement journals are created and ** written to disk immediately (the default behavior for SQLite versions ** before 3.12.0). -1 means always keep the entire statement journal in ** memory. (The statement journal is also always held entirely in memory ** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this ** setting.) */ /* ** The default lookaside-configuration, the format "SZ,N". SZ is the ** number of bytes in each lookaside slot (should be a multiple of 8) ** and N is the number of slots. The lookaside-configuration can be ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE) ** or at run-time for an individual database connection using ** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE); ** ** With the two-size-lookaside enhancement, less lookaside is required. ** The default configuration of 1200,40 actually provides 30 1200-byte slots ** and 93 128-byte slots, which is more lookaside than is available ** using the older 1200,100 configuration without two-size-lookaside. */ /* The default maximum size of an in-memory database created using ** sqlite3_deserialize() */ // C documentation // // /* // ** The following singleton contains the global configuration for // ** the SQLite library. // */ var _sqlite3Config = TSqlite3Config{ FbCoreMutex: uint8(1), FbFullMutex: libc.BoolUint8(true), FbUseCis: uint8(SQLITE_ALLOW_COVERING_INDEX_SCAN), FbExtraSchemaChecks: uint8(1), FmxStrlen: int32(0x7ffffffe), FszLookaside: int32(1200), FnLookaside: int32(40), FnStmtSpill: libc.Int32FromInt32(64) * libc.Int32FromInt32(1024), FmxMmap: int64(SQLITE_MAX_MMAP_SIZE), FnPage: int32(SQLITE_DEFAULT_PCACHE_INITSZ), FszPma: uint32(SQLITE_SORTER_PMASZ), FmxMemdbSize: int64(SQLITE_MEMDB_DEFAULT_MAXSIZE), FiOnceResetThreshold: int32(0x7ffffffe), FszSorterRef: uint32(SQLITE_DEFAULT_SORTERREF_SIZE), } // C documentation // // /* // ** Hash table for global functions - functions common to all // ** database connections. After initialization, this table is // ** read-only. // */ var _sqlite3BuiltinFunctions TFuncDefHash // C documentation // // /* // ** The value of the "pending" byte must be 0x40000000 (1 byte past the // ** 1-gibabyte boundary) in a compatible database. SQLite never uses // ** the database page that contains the pending byte. It never attempts // ** to read or write that page. The pending byte page is set aside // ** for use by the VFS layers as space for managing file locks. // ** // ** During testing, it is often desirable to move the pending byte to // ** a different position in the file. This allows code that has to // ** deal with the pending byte to run on files that are much smaller // ** than 1 GiB. The sqlite3_test_control() interface can be used to // ** move the pending byte. // ** // ** IMPORTANT: Changing the pending byte to any value other than // ** 0x40000000 results in an incompatible database file format! // ** Changing the pending byte during operation will result in undefined // ** and incorrect behavior. // */ var _sqlite3PendingByte = int32(0x40000000) // C documentation // // /* // ** Tracing flags set by SQLITE_TESTCTRL_TRACEFLAGS. // */ var _sqlite3TreeTrace = uint32(0) var _sqlite3WhereTrace = uint32(0) // C documentation // // /* #include "opcodes.h" */ // /* // ** Properties of opcodes. The OPFLG_INITIALIZER macro is // ** created by mkopcodeh.awk during compilation. Data is obtained // ** from the comments following the "case OP_xxxx:" statements in // ** the vdbe.c file. // */ var _sqlite3OpcodeProperty = [190]uint8{ 4: uint8(0x10), 6: uint8(0x41), 8: uint8(0x01), 9: uint8(0x01), 10: uint8(0x01), 11: uint8(0x01), 12: uint8(0x03), 13: uint8(0x03), 14: uint8(0x01), 15: uint8(0x01), 16: uint8(0x03), 17: uint8(0x03), 18: uint8(0x01), 19: uint8(0x12), 20: uint8(0x01), 21: uint8(0x49), 22: uint8(0x49), 23: uint8(0x49), 24: uint8(0x49), 25: uint8(0x01), 26: uint8(0x49), 27: uint8(0x49), 28: uint8(0x49), 29: uint8(0x49), 30: uint8(0x49), 31: uint8(0x49), 32: uint8(0x41), 33: uint8(0x01), 34: uint8(0x41), 35: uint8(0x41), 36: uint8(0x41), 37: uint8(0x01), 38: uint8(0x41), 39: uint8(0x41), 40: uint8(0x41), 41: uint8(0x41), 42: uint8(0x41), 43: uint8(0x26), 44: uint8(0x26), 45: uint8(0x41), 46: uint8(0x23), 47: uint8(0x0b), 48: uint8(0x01), 49: uint8(0x01), 50: uint8(0x03), 51: uint8(0x03), 52: uint8(0x0b), 53: uint8(0x0b), 54: uint8(0x0b), 55: uint8(0x0b), 56: uint8(0x0b), 57: uint8(0x0b), 58: uint8(0x01), 59: uint8(0x03), 60: uint8(0x03), 61: uint8(0x03), 62: uint8(0x01), 63: uint8(0x41), 64: uint8(0x01), 67: uint8(0x02), 68: uint8(0x02), 69: uint8(0x08), 71: uint8(0x10), 72: uint8(0x10), 73: uint8(0x10), 75: uint8(0x10), 77: uint8(0x10), 78: uint8(0x10), 81: uint8(0x10), 82: uint8(0x10), 86: uint8(0x02), 87: uint8(0x02), 88: uint8(0x02), 91: uint8(0x12), 92: uint8(0x1e), 93: uint8(0x20), 94: uint8(0x40), 98: uint8(0x10), 99: uint8(0x10), 101: uint8(0x40), 102: uint8(0x26), 103: uint8(0x26), 104: uint8(0x26), 105: uint8(0x26), 106: uint8(0x26), 107: uint8(0x26), 108: uint8(0x26), 109: uint8(0x26), 110: uint8(0x26), 111: uint8(0x26), 112: uint8(0x40), 114: uint8(0x12), 115: uint8(0x40), 116: uint8(0x40), 117: uint8(0x10), 118: uint8(0x40), 122: uint8(0x40), 124: uint8(0x40), 125: uint8(0x40), 126: uint8(0x10), 127: uint8(0x10), 133: uint8(0x40), 135: uint8(0x50), 137: uint8(0x40), 138: uint8(0x04), 139: uint8(0x04), 141: uint8(0x40), 142: uint8(0x50), 143: uint8(0x40), 144: uint8(0x10), 147: uint8(0x10), 153: uint8(0x10), 156: uint8(0x06), 157: uint8(0x10), 159: uint8(0x04), 160: uint8(0x1a), 173: uint8(0x40), 174: uint8(0x10), 175: uint8(0x50), 176: uint8(0x40), 178: uint8(0x10), 179: uint8(0x10), 180: uint8(0x02), 181: uint8(0x12), 182: uint8(0x12), } // C documentation // // /* // ** Name of the default collating sequence // */ var _sqlite3StrBINARY = [7]uint8{'B', 'I', 'N', 'A', 'R', 'Y'} // C documentation // // /* // ** Standard typenames. These names must match the COLTYPE_* definitions. // ** Adjust the SQLITE_N_STDTYPE value if adding or removing entries. // ** // ** sqlite3StdType[] The actual names of the datatypes. // ** // ** sqlite3StdTypeLen[] The length (in bytes) of each entry // ** in sqlite3StdType[]. // ** // ** sqlite3StdTypeAffinity[] The affinity associated with each entry // ** in sqlite3StdType[]. // */ var _sqlite3StdTypeLen = [6]uint8{ 0: uint8(3), 1: uint8(4), 2: uint8(3), 3: uint8(7), 4: uint8(4), 5: uint8(4), } var _sqlite3StdTypeAffinity = [6]uint8{ 0: uint8(SQLITE_AFF_NUMERIC), 1: uint8(SQLITE_AFF_BLOB), 2: uint8(SQLITE_AFF_INTEGER), 3: uint8(SQLITE_AFF_INTEGER), 4: uint8(SQLITE_AFF_REAL), 5: uint8(SQLITE_AFF_TEXT), } var _sqlite3StdType = [6]uintptr{ 0: __ccgo_ts + 1123, 1: __ccgo_ts + 1127, 2: __ccgo_ts + 1132, 3: __ccgo_ts + 1136, 4: __ccgo_ts + 1144, 5: __ccgo_ts + 1149, } /************** End of global.c **********************************************/ /************** Begin file status.c ******************************************/ /* ** 2008 June 18 ** ** 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 module implements the sqlite3_status() interface and related ** functionality. */ /* #include "sqliteInt.h" */ /************** Include vdbeInt.h in the middle of status.c ******************/ /************** Begin file vdbeInt.h *****************************************/ /* ** 2003 September 6 ** ** 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 is the header file for information that is private to the ** VDBE. This information used to all be at the top of the single ** source code file "vdbe.c". When that file became too big (over ** 6000 lines long) it was split up into several smaller files and ** this header information was factored out. */ /* ** The maximum number of times that a statement will try to reparse ** itself before giving up and returning SQLITE_SCHEMA. */ /* ** VDBE_DISPLAY_P4 is true or false depending on whether or not the ** "explain" P4 display logic is enabled. */ // C documentation // // /* // ** SQL is translated into a sequence of instructions to be // ** executed by a virtual machine. Each instruction is an instance // ** of the following structure. // */ type TOp = struct { Fopcode Tu8 Fp4type int8 Fp5 Tu16 Fp1 int32 Fp2 int32 Fp3 int32 Fp4 Tp4union } type Op = TOp // C documentation // // /* // ** Boolean values // */ type TBool = uint32 type Bool = TBool // C documentation // // /* Opaque type used by code in vdbesort.c */ type TVdbeSorter = struct { F__ccgo_align [0]uint32 FmnPmaSize int32 FmxPmaSize int32 FmxKeysize int32 Fpgsz int32 FpReader uintptr FpMerger uintptr Fdb uintptr FpKeyInfo uintptr FpUnpacked uintptr F__ccgo_align9 [4]byte Flist TSorterList FiMemory int32 FnMemory int32 FbUsePMA Tu8 FbUseThreads Tu8 FiPrev Tu8 FnTask Tu8 FtypeMask Tu8 F__ccgo_align17 [3]byte FaTask [1]TSortSubtask } type VdbeSorter = TVdbeSorter // C documentation // // /* Elements of the linked list at Vdbe.pAuxData */ type TAuxData = struct { FiAuxOp int32 FiAuxArg int32 FpAux uintptr FxDeleteAux uintptr FpNextAux uintptr } type AuxData = TAuxData // C documentation // // /* A cache of large TEXT or BLOB values in a VdbeCursor */ type TVdbeTxtBlbCache = struct { F__ccgo_align [0]uint32 FpCValue uintptr F__ccgo_align1 [4]byte FiOffset Ti64 FiCol int32 FcacheStatus Tu32 FcolCacheCtr Tu32 F__ccgo_pad5 [4]byte } type VdbeTxtBlbCache = TVdbeTxtBlbCache /* Types of VDBE cursors */ // C documentation // // /* // ** A VdbeCursor is an superclass (a wrapper) for various cursor objects: // ** // ** * A b-tree cursor // ** - In the main database or in an ephemeral database // ** - On either an index or a table // ** * A sorter // ** * A virtual table // ** * A one-row "pseudotable" stored in a single register // */ type TVdbeCursor = struct { F__ccgo_align [0]uint32 FeCurType Tu8 FiDb Ti8 FnullRow Tu8 FdeferredMoveto Tu8 FisTable Tu8 F__ccgo_align5 [3]byte F__ccgo8 uint8 FseekHit Tu16 Fub struct { FaAltMap [0]uintptr FpBtx uintptr } FseqCount Ti64 FcacheStatus Tu32 FseekResult int32 FpAltCursor uintptr Fuc struct { FpVCur [0]uintptr FpSorter [0]uintptr FpCursor uintptr } FpKeyInfo uintptr FiHdrOffset Tu32 FpgnoRoot TPgno FnField Ti16 FnHdrParsed Tu16 FmovetoTarget Ti64 FaOffset uintptr FaRow uintptr FpayloadSize Tu32 FszRow Tu32 FpCache uintptr FaType [1]Tu32 } type VdbeCursor = TVdbeCursor type TVdbeCursor1 = struct { F__ccgo_align [0]uint32 FeCurType Tu8 FiDb Ti8 FnullRow Tu8 FdeferredMoveto Tu8 FisTable Tu8 F__ccgo_align5 [3]byte F__ccgo8 uint8 FseekHit Tu16 Fub struct { FaAltMap [0]uintptr FpBtx uintptr } FseqCount Ti64 FcacheStatus Tu32 FseekResult int32 FpAltCursor uintptr Fuc struct { FpVCur [0]uintptr FpSorter [0]uintptr FpCursor uintptr } FpKeyInfo uintptr FiHdrOffset Tu32 FpgnoRoot TPgno FnField Ti16 FnHdrParsed Tu16 FmovetoTarget Ti64 FaOffset uintptr FaRow uintptr FpayloadSize Tu32 FszRow Tu32 FpCache uintptr FaType [1]Tu32 } type VdbeCursor1 = TVdbeCursor1 /* Return true if P is a null-only cursor */ /* ** A value for VdbeCursor.cacheStatus that means the cache is always invalid. */ /* ** Large TEXT or BLOB values can be slow to load, so we want to avoid ** loading them more than once. For that reason, large TEXT and BLOB values ** can be stored in a cache defined by this object, and attached to the ** VdbeCursor using the pCache field. */ type TVdbeTxtBlbCache1 = struct { F__ccgo_align [0]uint32 FpCValue uintptr F__ccgo_align1 [4]byte FiOffset Ti64 FiCol int32 FcacheStatus Tu32 FcolCacheCtr Tu32 F__ccgo_pad5 [4]byte } type VdbeTxtBlbCache1 = TVdbeTxtBlbCache1 // C documentation // // /* // ** When a sub-program is executed (OP_Program), a structure of this type // ** is allocated to store the current value of the program counter, as // ** well as the current memory cell array and various other frame specific // ** values stored in the Vdbe struct. When the sub-program is finished, // ** these values are copied back to the Vdbe from the VdbeFrame structure, // ** restoring the state of the VM to as it was before the sub-program // ** began executing. // ** // ** The memory for a VdbeFrame object is allocated and managed by a memory // ** cell in the parent (calling) frame. When the memory cell is deleted or // ** overwritten, the VdbeFrame object is not freed immediately. Instead, it // ** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame // ** list is deleted when the VM is reset in VdbeHalt(). The reason for doing // ** this instead of deleting the VdbeFrame immediately is to avoid recursive // ** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the // ** child frame are released. // ** // ** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is // ** set to NULL if the currently executing frame is the main program. // */ type TVdbeFrame = struct { F__ccgo_align [0]uint32 Fv uintptr FpParent uintptr FaOp uintptr FaMem uintptr FapCsr uintptr FaOnce uintptr Ftoken uintptr F__ccgo_align7 [4]byte FlastRowid Ti64 FpAuxData uintptr FnCursor int32 Fpc int32 FnOp int32 FnMem int32 FnChildMem int32 FnChildCsr int32 F__ccgo_align15 [4]byte FnChange Ti64 FnDbChange Ti64 } type VdbeFrame = TVdbeFrame type TVdbeFrame1 = struct { F__ccgo_align [0]uint32 Fv uintptr FpParent uintptr FaOp uintptr FaMem uintptr FapCsr uintptr FaOnce uintptr Ftoken uintptr F__ccgo_align7 [4]byte FlastRowid Ti64 FpAuxData uintptr FnCursor int32 Fpc int32 FnOp int32 FnMem int32 FnChildMem int32 FnChildCsr int32 F__ccgo_align15 [4]byte FnChange Ti64 FnDbChange Ti64 } type VdbeFrame1 = TVdbeFrame1 /* ** Size of struct Mem not including the Mem.zMalloc member or anything that ** follows. */ /* One or more of the following flags are set to indicate the ** representations of the value stored in the Mem struct. ** ** * MEM_Null An SQL NULL value ** ** * MEM_Null|MEM_Zero An SQL NULL with the virtual table ** UPDATE no-change flag set ** ** * MEM_Null|MEM_Term| An SQL NULL, but also contains a ** MEM_Subtype pointer accessible using ** sqlite3_value_pointer(). ** ** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal ** to other NULLs even using the IS operator. ** ** * MEM_Str A string, stored in Mem.z with ** length Mem.n. Zero-terminated if ** MEM_Term is set. This flag is ** incompatible with MEM_Blob and ** MEM_Null, but can appear with MEM_Int, ** MEM_Real, and MEM_IntReal. ** ** * MEM_Blob A blob, stored in Mem.z length Mem.n. ** Incompatible with MEM_Str, MEM_Null, ** MEM_Int, MEM_Real, and MEM_IntReal. ** ** * MEM_Blob|MEM_Zero A blob in Mem.z of length Mem.n plus ** MEM.u.i extra 0x00 bytes at the end. ** ** * MEM_Int Integer stored in Mem.u.i. ** ** * MEM_Real Real stored in Mem.u.r. ** ** * MEM_IntReal Real stored as an integer in Mem.u.i. ** ** If the MEM_Null flag is set, then the value is an SQL NULL value. ** For a pointer type created using sqlite3_bind_pointer() or ** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set. ** ** If the MEM_Str flag is set then Mem.z points at a string representation. ** Usually this is encoded in the same unicode encoding as the main ** database (see below for exceptions). If the MEM_Term flag is also ** set, then the string is nul terminated. The MEM_Int and MEM_Real ** flags may coexist with the MEM_Str flag. */ /* Extra bits that modify the meanings of the core datatypes above */ /* 0x0080 // Available */ /* Bits that determine the storage for Mem.z for a string or blob or ** aggregate accumulator. */ /* Return TRUE if Mem X contains dynamically allocated content - anything ** that needs to be deallocated to avoid a leak. */ /* ** Clear any existing type flags from a Mem and replace them with f */ /* ** True if Mem X is a NULL-nochng type. */ /* ** Return true if a memory cell has been initialized and is valid. ** is for use inside assert() statements only. ** ** A Memory cell is initialized if at least one of the ** MEM_Null, MEM_Str, MEM_Int, MEM_Real, MEM_Blob, or MEM_IntReal bits ** is set. It is "undefined" if all those bits are zero. */ /* ** Each auxiliary data pointer stored by a user defined function ** implementation calling sqlite3_set_auxdata() is stored in an instance ** of this structure. All such structures associated with a single VM ** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed ** when the VM is halted (if not before). */ type TAuxData1 = struct { FiAuxOp int32 FiAuxArg int32 FpAux uintptr FxDeleteAux uintptr FpNextAux uintptr } type AuxData1 = TAuxData1 /* ** The "context" argument for an installable function. A pointer to an ** instance of this structure is the first argument to the routines used ** implement the SQL functions. ** ** There is a typedef for this structure in sqlite.h. So all routines, ** even the public interface to SQLite, can use a pointer to this structure. ** But this file is the only place where the internal details of this ** structure are known. ** ** This structure is defined inside of vdbeInt.h because it uses substructures ** (Mem) which are only defined there. */ type Tsqlite3_context1 = struct { FpOut uintptr FpFunc uintptr FpMem uintptr FpVdbe uintptr FiOp int32 FisError int32 Fenc Tu8 FskipFlag Tu8 Fargc Tu8 Fargv [1]uintptr } type sqlite3_context1 = Tsqlite3_context1 // C documentation // // /* A bitfield type for use inside of structures. Always follow with :N where // ** N is the number of bits. // */ type Tbft = uint32 type bft = Tbft /* Bit Field Type */ // C documentation // // /* The ScanStatus object holds a single value for the // ** sqlite3_stmt_scanstatus() interface. // ** // ** aAddrRange[]: // ** This array is used by ScanStatus elements associated with EQP // ** notes that make an SQLITE_SCANSTAT_NCYCLE value available. It is // ** an array of up to 3 ranges of VM addresses for which the Vdbe.anCycle[] // ** values should be summed to calculate the NCYCLE value. Each pair of // ** integer addresses is a start and end address (both inclusive) for a range // ** instructions. A start value of 0 indicates an empty range. // */ type TScanStatus = struct { FaddrExplain int32 FaAddrRange [6]int32 FaddrLoop int32 FaddrVisit int32 FiSelectID int32 FnEst TLogEst FzName uintptr } type ScanStatus = TScanStatus type TScanStatus1 = struct { FaddrExplain int32 FaAddrRange [6]int32 FaddrLoop int32 FaddrVisit int32 FiSelectID int32 FnEst TLogEst FzName uintptr } type ScanStatus1 = TScanStatus1 // C documentation // // /* The DblquoteStr object holds the text of a double-quoted // ** string for a prepared statement. A linked list of these objects // ** is constructed during statement parsing and is held on Vdbe.pDblStr. // ** When computing a normalized SQL statement for an SQL statement, that // ** list is consulted for each double-quoted identifier to see if the // ** identifier should really be a string literal. // */ type TDblquoteStr = struct { FpNextStr uintptr Fz [8]uint8 } type DblquoteStr = TDblquoteStr type TDblquoteStr1 = struct { FpNextStr uintptr Fz [8]uint8 } type DblquoteStr1 = TDblquoteStr1 /* ** An instance of the virtual machine. This structure contains the complete ** state of the virtual machine. ** ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare() ** is really a pointer to an instance of this structure. */ type TVdbe1 = struct { F__ccgo_align [0]uint32 Fdb uintptr FppVPrev uintptr FpVNext uintptr FpParse uintptr FnVar TynVar FnMem int32 FnCursor int32 FcacheCtr Tu32 Fpc int32 Frc int32 FnChange Ti64 FiStatement int32 F__ccgo_align12 [4]byte FiCurrentTime Ti64 FnFkConstraint Ti64 FnStmtDefCons Ti64 FnStmtDefImmCons Ti64 FaMem uintptr FapArg uintptr FapCsr uintptr FaVar uintptr FaOp uintptr FnOp int32 FnOpAlloc int32 FaColName uintptr FpResultRow uintptr FzErrMsg uintptr FpVList uintptr F__ccgo_align27 [4]byte FstartTime Ti64 FnResColumn Tu16 FnResAlloc Tu16 FerrorAction Tu8 FminWriteFileFormat Tu8 FprepFlags Tu8 FeVdbeState Tu8 F__ccgo152 uint16 FbtreeMask TyDbMask FlockMask TyDbMask FaCounter [9]Tu32 FzSql uintptr FpFree uintptr FpFrame uintptr FpDelFrame uintptr FnFrame int32 Fexpmask Tu32 FpProgram uintptr FpAuxData uintptr } type Vdbe1 = TVdbe1 /* ** The following are allowed values for Vdbe.eVdbeState */ /* ** Structure used to store the context required by the ** sqlite3_preupdate_*() API functions. */ type TPreUpdate1 = struct { F__ccgo_align [0]uint32 Fv uintptr FpCsr uintptr Fop int32 FaRecord uintptr Fkeyinfo TKeyInfo FpUnpacked uintptr FpNewUnpacked uintptr FiNewReg int32 FiBlobWrite int32 FiKey1 Ti64 FiKey2 Ti64 FaNew uintptr FpTab uintptr FpPk uintptr F__ccgo_pad14 [4]byte } type PreUpdate1 = TPreUpdate1 // C documentation // // /* // ** An instance of this object is used to pass an vector of values into // ** OP_VFilter, the xFilter method of a virtual table. The vector is the // ** set of values on the right-hand side of an IN constraint. // ** // ** The value as passed into xFilter is an sqlite3_value with a "pointer" // ** type, such as is generated by sqlite3_result_pointer() and read by // ** sqlite3_value_pointer. Such values have MEM_Term|MEM_Subtype|MEM_Null // ** and a subtype of 'p'. The sqlite3_vtab_in_first() and _next() interfaces // ** know how to use this object to step through all the values in the // ** right operand of the IN constraint. // */ type TValueList = struct { FpCsr uintptr FpOut uintptr } type ValueList = TValueList type TValueList1 = struct { FpCsr uintptr FpOut uintptr } type ValueList1 = TValueList1 /************** End of vdbeInt.h *********************************************/ /************** Continuing where we left off in status.c *********************/ // C documentation // // /* // ** Variables in which to record status information. // */ type Tsqlite3StatValueType = uint32 type sqlite3StatValueType = Tsqlite3StatValueType type Tsqlite3StatType = struct { FnowValue [10]Tsqlite3StatValueType FmxValue [10]Tsqlite3StatValueType } type sqlite3StatType = Tsqlite3StatType type Tsqlite3StatType1 = struct { FnowValue [10]Tsqlite3StatValueType FmxValue [10]Tsqlite3StatValueType } type sqlite3StatType1 = Tsqlite3StatType1 var _sqlite3Stat = Tsqlite3StatType1{} // C documentation // // /* // ** Elements of sqlite3Stat[] are protected by either the memory allocator // ** mutex, or by the pcache1 mutex. The following array determines which. // */ var _statMutex = [10]uint8{ 1: uint8(1), 2: uint8(1), 7: uint8(1), } /* The "wsdStat" macro will resolve to the status information ** state vector. If writable static data is unsupported on the target, ** we have to locate the state vector at run-time. In the more common ** case where writable static data is supported, wsdStat can refer directly ** to the "sqlite3Stat" state vector declared above. */ // C documentation // // /* // ** Return the current value of a status parameter. The caller must // ** be holding the appropriate mutex. // */ func _sqlite3StatusValue(tls *libc.TLS, op int32) (r Tsqlite3_int64) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return int64(*(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*4))) } // C documentation // // /* // ** Add N to the value of a status record. The caller must hold the // ** appropriate mutex. (Locking is checked by assert()). // ** // ** The StatusUp() routine can accept positive or negative values for N. // ** The value of N is added to the current status value and the high-water // ** mark is adjusted if necessary. // ** // ** The StatusDown() routine lowers the current value by N. The highwater // ** mark is unchanged. N must be non-negative for StatusDown(). // */ func _sqlite3StatusUp(tls *libc.TLS, op int32, N int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*4)) += uint32(N) if *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*4)) > *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 40 + uintptr(op)*4)) { *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 40 + uintptr(op)*4)) = *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*4)) } } func _sqlite3StatusDown(tls *libc.TLS, op int32, N int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*4)) -= uint32(N) } // C documentation // // /* // ** Adjust the highwater mark if necessary. // ** The caller must hold the appropriate mutex. // */ func _sqlite3StatusHighwater(tls *libc.TLS, op int32, X int32) { var newValue Tsqlite3StatValueType _ = newValue _ = libc.Int32FromInt32(0) newValue = uint32(X) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if newValue > *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 40 + uintptr(op)*4)) { *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 40 + uintptr(op)*4)) = newValue } } // C documentation // // /* // ** Query status information. // */ func Xsqlite3_status64(tls *libc.TLS, op int32, pCurrent uintptr, pHighwater uintptr, resetFlag int32) (r int32) { var pMutex, v1 uintptr _, _ = pMutex, v1 if op < 0 || op >= int32(libc.Uint32FromInt64(40)/libc.Uint32FromInt64(4)) { return _sqlite3MisuseError(tls, int32(23890)) } if _statMutex[op] != 0 { v1 = _sqlite3Pcache1Mutex(tls) } else { v1 = _sqlite3MallocMutex(tls) } pMutex = v1 Xsqlite3_mutex_enter(tls, pMutex) *(*Tsqlite3_int64)(unsafe.Pointer(pCurrent)) = int64(*(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*4))) *(*Tsqlite3_int64)(unsafe.Pointer(pHighwater)) = int64(*(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 40 + uintptr(op)*4))) if resetFlag != 0 { *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 40 + uintptr(op)*4)) = *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*4)) } Xsqlite3_mutex_leave(tls, pMutex) _ = pMutex /* Prevent warning when SQLITE_THREADSAFE=0 */ return SQLITE_OK } func Xsqlite3_status(tls *libc.TLS, op int32, pCurrent uintptr, pHighwater uintptr, resetFlag int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* iCur at bp+0 */ Tsqlite3_int64 var _ /* iHwtr at bp+8 */ Tsqlite3_int64 _ = rc *(*Tsqlite3_int64)(unsafe.Pointer(bp)) = 0 *(*Tsqlite3_int64)(unsafe.Pointer(bp + 8)) = 0 rc = Xsqlite3_status64(tls, op, bp, bp+8, resetFlag) if rc == 0 { *(*int32)(unsafe.Pointer(pCurrent)) = int32(*(*Tsqlite3_int64)(unsafe.Pointer(bp))) *(*int32)(unsafe.Pointer(pHighwater)) = int32(*(*Tsqlite3_int64)(unsafe.Pointer(bp + 8))) } return rc } // C documentation // // /* // ** Return the number of LookasideSlot elements on the linked list // */ func _countLookasideSlots(tls *libc.TLS, p uintptr) (r Tu32) { var cnt Tu32 _ = cnt cnt = uint32(0) for p != 0 { p = (*TLookasideSlot)(unsafe.Pointer(p)).FpNext cnt++ } return cnt } // C documentation // // /* // ** Count the number of slots of lookaside memory that are outstanding // */ func _sqlite3LookasideUsed(tls *libc.TLS, db uintptr, pHighwater uintptr) (r int32) { var nFree, nInit Tu32 _, _ = nFree, nInit nInit = _countLookasideSlots(tls, (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpInit) nFree = _countLookasideSlots(tls, (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree) nInit += _countLookasideSlots(tls, (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit) nFree += _countLookasideSlots(tls, (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree) if pHighwater != 0 { *(*int32)(unsafe.Pointer(pHighwater)) = int32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FnSlot - nInit) } return int32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FnSlot - (nInit + nFree)) } // C documentation // // /* // ** Query status information for a single database connection // */ func Xsqlite3_db_status(tls *libc.TLS, db uintptr, op int32, pCurrent uintptr, pHighwater uintptr, resetFlag int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, i1, i2, nByte, rc, totalUsed int32 var p, p1, pBt, pPager, pPager1, pSchema, pVdbe uintptr var _ /* nByte at bp+0 */ int32 var _ /* nByte at bp+4 */ int32 var _ /* nRet at bp+8 */ Tu64 _, _, _, _, _, _, _, _, _, _, _, _, _ = i, i1, i2, nByte, p, p1, pBt, pPager, pPager1, pSchema, pVdbe, rc, totalUsed rc = SQLITE_OK /* Return code */ Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) switch op { case SQLITE_DBSTATUS_LOOKASIDE_USED: *(*int32)(unsafe.Pointer(pCurrent)) = _sqlite3LookasideUsed(tls, db, pHighwater) if resetFlag != 0 { p = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree if p != 0 { for (*TLookasideSlot)(unsafe.Pointer(p)).FpNext != 0 { p = (*TLookasideSlot)(unsafe.Pointer(p)).FpNext } (*TLookasideSlot)(unsafe.Pointer(p)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpInit (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpInit = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree = uintptr(0) } p = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree if p != 0 { for (*TLookasideSlot)(unsafe.Pointer(p)).FpNext != 0 { p = (*TLookasideSlot)(unsafe.Pointer(p)).FpNext } (*TLookasideSlot)(unsafe.Pointer(p)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree = uintptr(0) } } case int32(SQLITE_DBSTATUS_LOOKASIDE_HIT): fallthrough case int32(SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE): fallthrough case int32(SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL): _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pCurrent)) = 0 *(*int32)(unsafe.Pointer(pHighwater)) = int32(*(*Tu32)(unsafe.Pointer(db + 320 + 16 + uintptr(op-int32(SQLITE_DBSTATUS_LOOKASIDE_HIT))*4))) if resetFlag != 0 { *(*Tu32)(unsafe.Pointer(db + 320 + 16 + uintptr(op-int32(SQLITE_DBSTATUS_LOOKASIDE_HIT))*4)) = uint32(0) } break /* ** Return an approximation for the amount of memory currently used ** by all pagers associated with the given database connection. The ** highwater mark is meaningless and is returned as zero. */ fallthrough case int32(SQLITE_DBSTATUS_CACHE_USED_SHARED): fallthrough case int32(SQLITE_DBSTATUS_CACHE_USED): totalUsed = 0 _sqlite3BtreeEnterAll(tls, db) i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt if pBt != 0 { pPager = _sqlite3BtreePager(tls, pBt) nByte = _sqlite3PagerMemUsed(tls, pPager) if op == int32(SQLITE_DBSTATUS_CACHE_USED_SHARED) { nByte = nByte / _sqlite3BtreeConnectionCount(tls, pBt) } totalUsed += nByte } goto _1 _1: ; i++ } _sqlite3BtreeLeaveAll(tls, db) *(*int32)(unsafe.Pointer(pCurrent)) = totalUsed *(*int32)(unsafe.Pointer(pHighwater)) = 0 break /* ** *pCurrent gets an accurate estimate of the amount of memory used ** to store the schema for all databases (main, temp, and any ATTACHed ** databases. *pHighwater is set to zero. */ fallthrough case int32(SQLITE_DBSTATUS_SCHEMA_USED): /* Used to iterate through schemas */ *(*int32)(unsafe.Pointer(bp)) = 0 /* Used to accumulate return value */ _sqlite3BtreeEnterAll(tls, db) (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed = bp _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart i1 = 0 for { if !(i1 < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i1)*16))).FpSchema if pSchema != uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = int32(uint32(*(*int32)(unsafe.Pointer(bp))) + uint32((*(*func(*libc.TLS, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxRoundup})))(tls, int32(16)))*((*TSchema)(unsafe.Pointer(pSchema)).FtblHash.Fcount+(*TSchema)(unsafe.Pointer(pSchema)).FtrigHash.Fcount+(*TSchema)(unsafe.Pointer(pSchema)).FidxHash.Fcount+(*TSchema)(unsafe.Pointer(pSchema)).FfkeyHash.Fcount)) *(*int32)(unsafe.Pointer(bp)) = int32(uint64(*(*int32)(unsafe.Pointer(bp))) + Xsqlite3_msize(tls, (*TSchema)(unsafe.Pointer(pSchema)).FtblHash.Fht)) *(*int32)(unsafe.Pointer(bp)) = int32(uint64(*(*int32)(unsafe.Pointer(bp))) + Xsqlite3_msize(tls, (*TSchema)(unsafe.Pointer(pSchema)).FtrigHash.Fht)) *(*int32)(unsafe.Pointer(bp)) = int32(uint64(*(*int32)(unsafe.Pointer(bp))) + Xsqlite3_msize(tls, (*TSchema)(unsafe.Pointer(pSchema)).FidxHash.Fht)) *(*int32)(unsafe.Pointer(bp)) = int32(uint64(*(*int32)(unsafe.Pointer(bp))) + Xsqlite3_msize(tls, (*TSchema)(unsafe.Pointer(pSchema)).FfkeyHash.Fht)) p1 = (*THash)(unsafe.Pointer(pSchema + 40)).Ffirst for { if !(p1 != 0) { break } _sqlite3DeleteTrigger(tls, db, (*THashElem)(unsafe.Pointer(p1)).Fdata) goto _3 _3: ; p1 = (*THashElem)(unsafe.Pointer(p1)).Fnext } p1 = (*THash)(unsafe.Pointer(pSchema + 8)).Ffirst for { if !(p1 != 0) { break } _sqlite3DeleteTable(tls, db, (*THashElem)(unsafe.Pointer(p1)).Fdata) goto _4 _4: ; p1 = (*THashElem)(unsafe.Pointer(p1)).Fnext } } goto _2 _2: ; i1++ } (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpTrueEnd _sqlite3BtreeLeaveAll(tls, db) *(*int32)(unsafe.Pointer(pHighwater)) = 0 *(*int32)(unsafe.Pointer(pCurrent)) = *(*int32)(unsafe.Pointer(bp)) break /* ** *pCurrent gets an accurate estimate of the amount of memory used ** to store all prepared statements. ** *pHighwater is set to zero. */ fallthrough case int32(SQLITE_DBSTATUS_STMT_USED): /* Used to iterate through VMs */ *(*int32)(unsafe.Pointer(bp + 4)) = 0 /* Used to accumulate return value */ (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed = bp + 4 _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart pVdbe = (*Tsqlite3)(unsafe.Pointer(db)).FpVdbe for { if !(pVdbe != 0) { break } _sqlite3VdbeDelete(tls, pVdbe) goto _5 _5: ; pVdbe = (*TVdbe1)(unsafe.Pointer(pVdbe)).FpVNext } (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpTrueEnd (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed = uintptr(0) *(*int32)(unsafe.Pointer(pHighwater)) = 0 /* IMP: R-64479-57858 */ *(*int32)(unsafe.Pointer(pCurrent)) = *(*int32)(unsafe.Pointer(bp + 4)) break /* ** Set *pCurrent to the total cache hits or misses encountered by all ** pagers the database handle is connected to. *pHighwater is always set ** to zero. */ fallthrough case int32(SQLITE_DBSTATUS_CACHE_SPILL): op = libc.Int32FromInt32(SQLITE_DBSTATUS_CACHE_WRITE) + libc.Int32FromInt32(1) fallthrough case int32(SQLITE_DBSTATUS_CACHE_HIT): fallthrough case int32(SQLITE_DBSTATUS_CACHE_MISS): fallthrough case int32(SQLITE_DBSTATUS_CACHE_WRITE): *(*Tu64)(unsafe.Pointer(bp + 8)) = uint64(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) i2 = 0 for { if !(i2 < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i2)*16))).FpBt != 0 { pPager1 = _sqlite3BtreePager(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i2)*16))).FpBt) _sqlite3PagerCacheStat(tls, pPager1, op, resetFlag, bp+8) } goto _6 _6: ; i2++ } *(*int32)(unsafe.Pointer(pHighwater)) = 0 /* IMP: R-42420-56072 */ /* IMP: R-54100-20147 */ /* IMP: R-29431-39229 */ *(*int32)(unsafe.Pointer(pCurrent)) = int32(*(*Tu64)(unsafe.Pointer(bp + 8))) & int32(0x7fffffff) break /* Set *pCurrent to non-zero if there are unresolved deferred foreign ** key constraints. Set *pCurrent to zero if all foreign key constraints ** have been satisfied. The *pHighwater is always set to zero. */ fallthrough case int32(SQLITE_DBSTATUS_DEFERRED_FKS): *(*int32)(unsafe.Pointer(pHighwater)) = 0 /* IMP: R-11967-56545 */ *(*int32)(unsafe.Pointer(pCurrent)) = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons > 0 || (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons > 0) default: rc = int32(SQLITE_ERROR) } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } type Ttimex = struct { Fmodes uint32 Foffset t__syscall_slong_t Ffreq t__syscall_slong_t Fmaxerror t__syscall_slong_t Festerror t__syscall_slong_t Fstatus int32 Fconstant t__syscall_slong_t Fprecision t__syscall_slong_t Ftolerance t__syscall_slong_t Ftime Ttimeval Ftick t__syscall_slong_t Fppsfreq t__syscall_slong_t Fjitter t__syscall_slong_t Fshift int32 Fstabil t__syscall_slong_t Fjitcnt t__syscall_slong_t Fcalcnt t__syscall_slong_t Ferrcnt t__syscall_slong_t Fstbcnt t__syscall_slong_t Ftai int32 F__ccgo84 uint32 F__ccgo88 uint32 F__ccgo92 uint32 F__ccgo96 uint32 F__ccgo100 uint32 F__ccgo104 uint32 F__ccgo108 uint32 F__ccgo112 uint32 F__ccgo116 uint32 F__ccgo120 uint32 F__ccgo124 uint32 } type timex = Ttimex type Ttm = struct { Ftm_sec int32 Ftm_min int32 Ftm_hour int32 Ftm_mday int32 Ftm_mon int32 Ftm_year int32 Ftm_wday int32 Ftm_yday int32 Ftm_isdst int32 Ftm_gmtoff int32 Ftm_zone uintptr } type tm = Ttm type Titimerspec = struct { Fit_interval Ttimespec Fit_value Ttimespec } type itimerspec = Titimerspec /* ** The MSVC CRT on Windows CE may not have a localtime() function. ** So declare a substitute. The substitute function itself is ** defined in "os_win.c". */ // C documentation // // /* // ** A structure for holding a single date and time. // */ type TDateTime = struct { F__ccgo_align [0]uint32 FiJD Tsqlite3_int64 FY int32 FM int32 FD int32 Fh int32 Fm int32 Ftz int32 Fs float64 FvalidJD uint8 FrawS uint8 FvalidYMD uint8 FvalidHMS uint8 FvalidTZ uint8 FtzSet uint8 FisError uint8 FuseSubsec uint8 } type DateTime = TDateTime type TDateTime1 = struct { F__ccgo_align [0]uint32 FiJD Tsqlite3_int64 FY int32 FM int32 FD int32 Fh int32 Fm int32 Ftz int32 Fs float64 FvalidJD uint8 FrawS uint8 FvalidYMD uint8 FvalidHMS uint8 FvalidTZ uint8 FtzSet uint8 FisError uint8 FuseSubsec uint8 } type DateTime1 = TDateTime1 // C documentation // // /* // ** Convert zDate into one or more integers according to the conversion // ** specifier zFormat. // ** // ** zFormat[] contains 4 characters for each integer converted, except for // ** the last integer which is specified by three characters. The meaning // ** of a four-character format specifiers ABCD is: // ** // ** A: number of digits to convert. Always "2" or "4". // ** B: minimum value. Always "0" or "1". // ** C: maximum value, decoded as: // ** a: 12 // ** b: 14 // ** c: 24 // ** d: 31 // ** e: 59 // ** f: 9999 // ** D: the separator character, or \000 to indicate this is the // ** last number to convert. // ** // ** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would // ** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-". // ** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates // ** the 2-digit day which is the last integer in the set. // ** // ** The function returns the number of successful conversions. // */ func _getDigits(tls *libc.TLS, zDate uintptr, zFormat uintptr, va uintptr) (r int32) { var N, min, nextC, v1 uint8 var ap Tva_list var cnt, val int32 var max Tu16 _, _, _, _, _, _, _, _ = N, ap, cnt, max, min, nextC, val, v1 cnt = 0 ap = va for cond := true; cond; cond = nextC != 0 { N = uint8(int32(*(*uint8)(unsafe.Pointer(zFormat))) - int32('0')) min = uint8(int32(*(*uint8)(unsafe.Pointer(zFormat + 1))) - int32('0')) val = 0 _ = libc.Int32FromInt32(0) max = _aMx[int32(*(*uint8)(unsafe.Pointer(zFormat + 2)))-int32('a')] nextC = *(*uint8)(unsafe.Pointer(zFormat + 3)) val = 0 for { v1 = N N-- if !(v1 != 0) { break } if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zDate))])&libc.Int32FromInt32(0x04) != 0) { goto end_getDigits } val = val*int32(10) + int32(*(*uint8)(unsafe.Pointer(zDate))) - int32('0') zDate++ } if val < int32(min) || val > int32(max) || int32(nextC) != 0 && int32(nextC) != int32(*(*uint8)(unsafe.Pointer(zDate))) { goto end_getDigits } *(*int32)(unsafe.Pointer(libc.VaUintptr(&ap))) = val zDate++ cnt++ zFormat += uintptr(4) } goto end_getDigits end_getDigits: ; _ = ap return cnt } /* The aMx[] array translates the 3rd character of each format ** spec into a max size: a b c d e f */ var _aMx = [6]Tu16{ 0: uint16(12), 1: uint16(14), 2: uint16(24), 3: uint16(31), 4: uint16(59), 5: uint16(14712), } // C documentation // // /* // ** Parse a timezone extension on the end of a date-time. // ** The extension is of the form: // ** // ** (+/-)HH:MM // ** // ** Or the "zulu" notation: // ** // ** Z // ** // ** If the parse is successful, write the number of minutes // ** of change in p->tz and return 0. If a parser error occurs, // ** return non-zero. // ** // ** A missing specifier is not considered an error. // */ func _parseTimezone(tls *libc.TLS, zDate uintptr, p uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var c, sgn int32 var _ /* nHr at bp+0 */ int32 var _ /* nMn at bp+4 */ int32 _, _ = c, sgn sgn = 0 for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zDate))])&int32(0x01) != 0 { zDate++ } (*TDateTime)(unsafe.Pointer(p)).Ftz = 0 c = int32(*(*uint8)(unsafe.Pointer(zDate))) if c == int32('-') { sgn = -int32(1) } else { if c == int32('+') { sgn = +libc.Int32FromInt32(1) } else { if c == int32('Z') || c == int32('z') { zDate++ goto zulu_time } else { return libc.BoolInt32(c != 0) } } } zDate++ if _getDigits(tls, zDate, __ccgo_ts+1154, libc.VaList(bp+16, bp, bp+4)) != int32(2) { return int32(1) } zDate += uintptr(5) (*TDateTime)(unsafe.Pointer(p)).Ftz = sgn * (*(*int32)(unsafe.Pointer(bp + 4)) + *(*int32)(unsafe.Pointer(bp))*int32(60)) goto zulu_time zulu_time: ; for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zDate))])&int32(0x01) != 0 { zDate++ } (*TDateTime)(unsafe.Pointer(p)).FtzSet = uint8(1) return libc.BoolInt32(int32(*(*uint8)(unsafe.Pointer(zDate))) != 0) } // C documentation // // /* // ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF. // ** The HH, MM, and SS must each be exactly 2 digits. The // ** fractional seconds FFFF can be one or more digits. // ** // ** Return 1 if there is a parsing error and 0 on success. // */ func _parseHhMmSs(tls *libc.TLS, zDate uintptr, p uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var ms, rScale float64 var v1 int32 var _ /* h at bp+0 */ int32 var _ /* m at bp+4 */ int32 var _ /* s at bp+8 */ int32 _, _, _ = ms, rScale, v1 ms = float64(0) if _getDigits(tls, zDate, __ccgo_ts+1162, libc.VaList(bp+24, bp, bp+4)) != int32(2) { return int32(1) } zDate += uintptr(5) if int32(*(*uint8)(unsafe.Pointer(zDate))) == int32(':') { zDate++ if _getDigits(tls, zDate, __ccgo_ts+1170, libc.VaList(bp+24, bp+8)) != int32(1) { return int32(1) } zDate += uintptr(2) if int32(*(*uint8)(unsafe.Pointer(zDate))) == int32('.') && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zDate + 1))])&int32(0x04) != 0 { rScale = float64(1) zDate++ for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zDate))])&int32(0x04) != 0 { ms = ms*float64(10) + float64(*(*uint8)(unsafe.Pointer(zDate))) - libc.Float64FromUint8('0') rScale *= float64(10) zDate++ } ms /= rScale } } else { *(*int32)(unsafe.Pointer(bp + 8)) = 0 } (*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FrawS = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = uint8(1) (*TDateTime)(unsafe.Pointer(p)).Fh = *(*int32)(unsafe.Pointer(bp)) (*TDateTime)(unsafe.Pointer(p)).Fm = *(*int32)(unsafe.Pointer(bp + 4)) (*TDateTime)(unsafe.Pointer(p)).Fs = float64(*(*int32)(unsafe.Pointer(bp + 8))) + ms if _parseTimezone(tls, zDate, p) != 0 { return int32(1) } if (*TDateTime)(unsafe.Pointer(p)).Ftz != 0 { v1 = int32(1) } else { v1 = 0 } (*TDateTime)(unsafe.Pointer(p)).FvalidTZ = uint8(v1) return 0 } // C documentation // // /* // ** Put the DateTime object into its error state. // */ func _datetimeError(tls *libc.TLS, p uintptr) { libc.Xmemset(tls, p, 0, uint32(48)) (*TDateTime)(unsafe.Pointer(p)).FisError = uint8(1) } // C documentation // // /* // ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume // ** that the YYYY-MM-DD is according to the Gregorian calendar. // ** // ** Reference: Meeus page 61 // */ func _computeJD(tls *libc.TLS, p uintptr) { var A, B, D, M, X1, X2, Y int32 _, _, _, _, _, _, _ = A, B, D, M, X1, X2, Y if (*TDateTime)(unsafe.Pointer(p)).FvalidJD != 0 { return } if (*TDateTime)(unsafe.Pointer(p)).FvalidYMD != 0 { Y = (*TDateTime)(unsafe.Pointer(p)).FY M = (*TDateTime)(unsafe.Pointer(p)).FM D = (*TDateTime)(unsafe.Pointer(p)).FD } else { Y = int32(2000) /* If no YMD specified, assume 2000-Jan-01 */ M = int32(1) D = int32(1) } if Y < -int32(4713) || Y > int32(9999) || (*TDateTime)(unsafe.Pointer(p)).FrawS != 0 { _datetimeError(tls, p) return } if M <= int32(2) { Y-- M += int32(12) } A = Y / int32(100) B = int32(2) - A + A/int32(4) X1 = int32(36525) * (Y + int32(4716)) / int32(100) X2 = int32(306001) * (M + int32(1)) / int32(10000) (*TDateTime)(unsafe.Pointer(p)).FiJD = int64((float64(X1+X2+D+B) - libc.Float64FromFloat64(1524.5)) * libc.Float64FromInt32(86400000)) (*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(1) if (*TDateTime)(unsafe.Pointer(p)).FvalidHMS != 0 { *(*Tsqlite3_int64)(unsafe.Pointer(p)) += int64((*TDateTime)(unsafe.Pointer(p)).Fh*int32(3600000)+(*TDateTime)(unsafe.Pointer(p)).Fm*int32(60000)) + int64((*TDateTime)(unsafe.Pointer(p)).Fs*libc.Float64FromInt32(1000)+libc.Float64FromFloat64(0.5)) if (*TDateTime)(unsafe.Pointer(p)).FvalidTZ != 0 { *(*Tsqlite3_int64)(unsafe.Pointer(p)) -= int64((*TDateTime)(unsafe.Pointer(p)).Ftz * int32(60000)) (*TDateTime)(unsafe.Pointer(p)).FvalidYMD = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FvalidTZ = uint8(0) } } } // C documentation // // /* // ** Parse dates of the form // ** // ** YYYY-MM-DD HH:MM:SS.FFF // ** YYYY-MM-DD HH:MM:SS // ** YYYY-MM-DD HH:MM // ** YYYY-MM-DD // ** // ** Write the result into the DateTime structure and return 0 // ** on success and 1 if the input string is not a well-formed // ** date. // */ func _parseYyyyMmDd(tls *libc.TLS, zDate uintptr, p uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var neg, v1 int32 var _ /* D at bp+8 */ int32 var _ /* M at bp+4 */ int32 var _ /* Y at bp+0 */ int32 _, _ = neg, v1 if int32(*(*uint8)(unsafe.Pointer(zDate))) == int32('-') { zDate++ neg = int32(1) } else { neg = 0 } if _getDigits(tls, zDate, __ccgo_ts+1174, libc.VaList(bp+24, bp, bp+4, bp+8)) != int32(3) { return int32(1) } zDate += uintptr(10) for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zDate))])&int32(0x01) != 0 || int32('T') == int32(*(*Tu8)(unsafe.Pointer(zDate))) { zDate++ } if _parseHhMmSs(tls, zDate, p) == 0 { /* We got the time */ } else { if int32(*(*uint8)(unsafe.Pointer(zDate))) == 0 { (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = uint8(0) } else { return int32(1) } } (*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FvalidYMD = uint8(1) if neg != 0 { v1 = -*(*int32)(unsafe.Pointer(bp)) } else { v1 = *(*int32)(unsafe.Pointer(bp)) } (*TDateTime)(unsafe.Pointer(p)).FY = v1 (*TDateTime)(unsafe.Pointer(p)).FM = *(*int32)(unsafe.Pointer(bp + 4)) (*TDateTime)(unsafe.Pointer(p)).FD = *(*int32)(unsafe.Pointer(bp + 8)) if (*TDateTime)(unsafe.Pointer(p)).FvalidTZ != 0 { _computeJD(tls, p) } return 0 } // C documentation // // /* // ** Set the time to the current time reported by the VFS. // ** // ** Return the number of errors. // */ func _setDateTimeToCurrent(tls *libc.TLS, context uintptr, p uintptr) (r int32) { (*TDateTime)(unsafe.Pointer(p)).FiJD = _sqlite3StmtCurrentTime(tls, context) if (*TDateTime)(unsafe.Pointer(p)).FiJD > 0 { (*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(1) return 0 } else { return int32(1) } return r } // C documentation // // /* // ** Input "r" is a numeric quantity which might be a julian day number, // ** or the number of seconds since 1970. If the value if r is within // ** range of a julian day number, install it as such and set validJD. // ** If the value is a valid unix timestamp, put it in p->s and set p->rawS. // */ func _setRawDateNumber(tls *libc.TLS, p uintptr, r float64) { (*TDateTime)(unsafe.Pointer(p)).Fs = r (*TDateTime)(unsafe.Pointer(p)).FrawS = uint8(1) if r >= float64(0) && r < float64(5.3734845e+06) { (*TDateTime)(unsafe.Pointer(p)).FiJD = int64(r*libc.Float64FromFloat64(8.64e+07) + libc.Float64FromFloat64(0.5)) (*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(1) } } // C documentation // // /* // ** Attempt to parse the given string into a julian day number. Return // ** the number of errors. // ** // ** The following are acceptable forms for the input string: // ** // ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM // ** DDDD.DD // ** now // ** // ** In the first form, the +/-HH:MM is always optional. The fractional // ** seconds extension (the ".FFF") is optional. The seconds portion // ** (":SS.FFF") is option. The year and date can be omitted as long // ** as there is a time string. The time string can be omitted as long // ** as there is a year and date. // */ func _parseDateOrTime(tls *libc.TLS, context uintptr, zDate uintptr, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* r at bp+0 */ float64 if _parseYyyyMmDd(tls, zDate, p) == 0 { return 0 } else { if _parseHhMmSs(tls, zDate, p) == 0 { return 0 } else { if _sqlite3StrICmp(tls, zDate, __ccgo_ts+1186) == 0 && _sqlite3NotPureFunc(tls, context) != 0 { return _setDateTimeToCurrent(tls, context, p) } else { if _sqlite3AtoF(tls, zDate, bp, _sqlite3Strlen30(tls, zDate), uint8(SQLITE_UTF8)) > 0 { _setRawDateNumber(tls, p, *(*float64)(unsafe.Pointer(bp))) return 0 } else { if (_sqlite3StrICmp(tls, zDate, __ccgo_ts+1190) == 0 || _sqlite3StrICmp(tls, zDate, __ccgo_ts+1197) == 0) && _sqlite3NotPureFunc(tls, context) != 0 { (*TDateTime)(unsafe.Pointer(p)).FuseSubsec = uint8(1) return _setDateTimeToCurrent(tls, context, p) } } } } } return int32(1) } /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999. ** Multiplying this by 86400000 gives 464269060799999 as the maximum value ** for DateTime.iJD. ** ** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with ** such a large integer literal, so we have to encode it. */ // C documentation // // /* // ** Return TRUE if the given julian day number is within range. // ** // ** The input is the JulianDay times 86400000. // */ func _validJulianDay(tls *libc.TLS, iJD Tsqlite3_int64) (r int32) { return libc.BoolInt32(iJD >= 0 && iJD <= libc.Int64FromInt32(0x1a640)< int32(2) { v2 = C - int32(4716) } else { v2 = C - int32(4715) } (*TDateTime)(unsafe.Pointer(p)).FY = v2 } } (*TDateTime)(unsafe.Pointer(p)).FvalidYMD = uint8(1) } // C documentation // // /* // ** Compute the Hour, Minute, and Seconds from the julian day number. // */ func _computeHMS(tls *libc.TLS, p uintptr) { var day_min, day_ms int32 _, _ = day_min, day_ms /* milliseconds, minutes into the day */ if (*TDateTime)(unsafe.Pointer(p)).FvalidHMS != 0 { return } _computeJD(tls, p) day_ms = int32(((*TDateTime)(unsafe.Pointer(p)).FiJD + libc.Int64FromInt32(43200000)) % libc.Int64FromInt32(86400000)) (*TDateTime)(unsafe.Pointer(p)).Fs = float64(day_ms%libc.Int32FromInt32(60000)) / float64(1000) day_min = day_ms / int32(60000) (*TDateTime)(unsafe.Pointer(p)).Fm = day_min % int32(60) (*TDateTime)(unsafe.Pointer(p)).Fh = day_min / int32(60) (*TDateTime)(unsafe.Pointer(p)).FrawS = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = uint8(1) } // C documentation // // /* // ** Compute both YMD and HMS // */ func _computeYMD_HMS(tls *libc.TLS, p uintptr) { _computeYMD(tls, p) _computeHMS(tls, p) } // C documentation // // /* // ** Clear the YMD and HMS and the TZ // */ func _clearYMD_HMS_TZ(tls *libc.TLS, p uintptr) { (*TDateTime)(unsafe.Pointer(p)).FvalidYMD = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FvalidTZ = uint8(0) } /* ** On recent Windows platforms, the localtime_s() function is available ** as part of the "Secure CRT". It is essentially equivalent to ** localtime_r() available under most POSIX platforms, except that the ** order of the parameters is reversed. ** ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx. ** ** If the user has not indicated to use localtime_r() or localtime_s() ** already, check for an MSVC build environment that provides ** localtime_s(). */ // C documentation // // /* // ** The following routine implements the rough equivalent of localtime_r() // ** using whatever operating-system specific localtime facility that // ** is available. This routine returns 0 on success and // ** non-zero on any kind of error. // ** // ** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this // ** routine will always fail. If bLocaltimeFault is nonzero and // ** sqlite3GlobalConfig.xAltLocaltime is not NULL, then xAltLocaltime() is // ** invoked in place of the OS-defined localtime() function. // ** // ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C // ** library function localtime_r() is used to assist in the calculation of // ** local time. // */ func _osLocaltime(tls *libc.TLS, t uintptr, pTm uintptr) (r int32) { var mutex, pX uintptr var rc int32 _, _, _ = mutex, pX, rc mutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN)) Xsqlite3_mutex_enter(tls, mutex) pX = libc.Xlocaltime(tls, t) if _sqlite3Config.FbLocaltimeFault != 0 { if _sqlite3Config.FxAltLocaltime != uintptr(0) && 0 == (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.FxAltLocaltime})))(tls, t, pTm) { pX = pTm } else { pX = uintptr(0) } } if pX != 0 { *(*Ttm)(unsafe.Pointer(pTm)) = *(*Ttm)(unsafe.Pointer(pX)) } Xsqlite3_mutex_leave(tls, mutex) rc = libc.BoolInt32(pX == uintptr(0)) return rc } // C documentation // // /* // ** Assuming the input DateTime is UTC, move it to its localtime equivalent. // */ func _toLocaltime(tls *libc.TLS, p uintptr, pCtx uintptr) (r int32) { bp := tls.Alloc(96) defer tls.Free(96) var iYearDiff int32 var _ /* sLocal at bp+4 */ Ttm var _ /* t at bp+0 */ Ttime_t var _ /* x at bp+48 */ TDateTime _ = iYearDiff /* Initialize the contents of sLocal to avoid a compiler warning. */ libc.Xmemset(tls, bp+4, 0, uint32(44)) _computeJD(tls, p) if (*TDateTime)(unsafe.Pointer(p)).FiJD < libc.Int64FromInt32(2108667600)*libc.Int64FromInt32(100000) || (*TDateTime)(unsafe.Pointer(p)).FiJD > libc.Int64FromInt32(2130141456)*libc.Int64FromInt32(100000) { /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only ** works for years between 1970 and 2037. For dates outside this range, ** SQLite attempts to map the year into an equivalent year within this ** range, do the calculation, then map the year back. */ *(*TDateTime)(unsafe.Pointer(bp + 48)) = *(*TDateTime)(unsafe.Pointer(p)) _computeYMD_HMS(tls, bp+48) iYearDiff = int32(2000) + (*(*TDateTime)(unsafe.Pointer(bp + 48))).FY%int32(4) - (*(*TDateTime)(unsafe.Pointer(bp + 48))).FY (*(*TDateTime)(unsafe.Pointer(bp + 48))).FY += iYearDiff (*(*TDateTime)(unsafe.Pointer(bp + 48))).FvalidJD = uint8(0) _computeJD(tls, bp+48) *(*Ttime_t)(unsafe.Pointer(bp)) = int32((*(*TDateTime)(unsafe.Pointer(bp + 48))).FiJD/libc.Int64FromInt32(1000) - libc.Int64FromInt32(21086676)*libc.Int64FromInt32(10000)) } else { iYearDiff = 0 *(*Ttime_t)(unsafe.Pointer(bp)) = int32((*TDateTime)(unsafe.Pointer(p)).FiJD/libc.Int64FromInt32(1000) - libc.Int64FromInt32(21086676)*libc.Int64FromInt32(10000)) } if _osLocaltime(tls, bp, bp+4) != 0 { Xsqlite3_result_error(tls, pCtx, __ccgo_ts+1207, -int32(1)) return int32(SQLITE_ERROR) } (*TDateTime)(unsafe.Pointer(p)).FY = (*(*Ttm)(unsafe.Pointer(bp + 4))).Ftm_year + int32(1900) - iYearDiff (*TDateTime)(unsafe.Pointer(p)).FM = (*(*Ttm)(unsafe.Pointer(bp + 4))).Ftm_mon + int32(1) (*TDateTime)(unsafe.Pointer(p)).FD = (*(*Ttm)(unsafe.Pointer(bp + 4))).Ftm_mday (*TDateTime)(unsafe.Pointer(p)).Fh = (*(*Ttm)(unsafe.Pointer(bp + 4))).Ftm_hour (*TDateTime)(unsafe.Pointer(p)).Fm = (*(*Ttm)(unsafe.Pointer(bp + 4))).Ftm_min (*TDateTime)(unsafe.Pointer(p)).Fs = float64((*(*Ttm)(unsafe.Pointer(bp + 4))).Ftm_sec) + float64((*TDateTime)(unsafe.Pointer(p)).FiJD%libc.Int64FromInt32(1000))*float64(0.001) (*TDateTime)(unsafe.Pointer(p)).FvalidYMD = uint8(1) (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = uint8(1) (*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FrawS = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FvalidTZ = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FisError = uint8(0) return SQLITE_OK } // C documentation // // /* // ** The following table defines various date transformations of the form // ** // ** 'NNN days' // ** // ** Where NNN is an arbitrary floating-point number and "days" can be one // ** of several units of time. // */ var _aXformType = [6]struct { FnName Tu8 FzName [7]uint8 FrLimit float32 FrXform float32 }{ 0: { FnName: uint8(6), FzName: [7]uint8{'s', 'e', 'c', 'o', 'n', 'd'}, FrLimit: float32(4.6427e+14), FrXform: float32(1), }, 1: { FnName: uint8(6), FzName: [7]uint8{'m', 'i', 'n', 'u', 't', 'e'}, FrLimit: float32(7.7379e+12), FrXform: float32(60), }, 2: { FnName: uint8(4), FzName: [7]uint8{'h', 'o', 'u', 'r'}, FrLimit: float32(1.2897e+11), FrXform: float32(3600), }, 3: { FnName: uint8(3), FzName: [7]uint8{'d', 'a', 'y'}, FrLimit: float32(5.373485e+06), FrXform: float32(86400), }, 4: { FnName: uint8(5), FzName: [7]uint8{'m', 'o', 'n', 't', 'h'}, FrLimit: float32(176546), FrXform: float32(2.592e+06), }, 5: { FnName: uint8(4), FzName: [7]uint8{'y', 'e', 'a', 'r'}, FrLimit: float32(14713), FrXform: float32(3.1536e+07), }, } // C documentation // // /* // ** If the DateTime p is raw number, try to figure out if it is // ** a julian day number of a unix timestamp. Set the p value // ** appropriately. // */ func _autoAdjustDate(tls *libc.TLS, p uintptr) { var r float64 _ = r if !((*TDateTime)(unsafe.Pointer(p)).FrawS != 0) || (*TDateTime)(unsafe.Pointer(p)).FvalidJD != 0 { (*TDateTime)(unsafe.Pointer(p)).FrawS = uint8(0) } else { if (*TDateTime)(unsafe.Pointer(p)).Fs >= float64(int64(-libc.Int32FromInt32(21086676))*libc.Int64FromInt32(10000)) && (*TDateTime)(unsafe.Pointer(p)).Fs <= float64(libc.Int64FromInt32(25340230)*libc.Int64FromInt32(10000)+libc.Int64FromInt32(799)) { r = (*TDateTime)(unsafe.Pointer(p)).Fs*float64(1000) + float64(2.1086676e+14) _clearYMD_HMS_TZ(tls, p) (*TDateTime)(unsafe.Pointer(p)).FiJD = int64(r + libc.Float64FromFloat64(0.5)) (*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(1) (*TDateTime)(unsafe.Pointer(p)).FrawS = uint8(0) } } } // C documentation // // /* // ** Process a modifier to a date-time stamp. The modifiers are // ** as follows: // ** // ** NNN days // ** NNN hours // ** NNN minutes // ** NNN.NNNN seconds // ** NNN months // ** NNN years // ** start of month // ** start of year // ** start of week // ** start of day // ** weekday N // ** unixepoch // ** localtime // ** utc // ** // ** Return 0 on success and 1 if there is any kind of error. If the error // ** is in a system call (i.e. localtime()), then an error message is written // ** to context pCtx. If the error is an unrecognized modifier, no error is // ** written to pCtx. // */ func _parseModifier(tls *libc.TLS, pCtx uintptr, z uintptr, n int32, p uintptr, idx int32) (r int32) { bp := tls.Alloc(160) defer tls.Free(160) var Z, day Tsqlite3_int64 var cnt, i, rc, x, y, v12, v2, v5, v7, v9 int32 var iErr, iGuess, iOrigJD, v1 Ti64 var rRounder, v10 float64 var z0 uint8 var z2 uintptr var v3, v6 bool var _ /* D at bp+64 */ int32 var _ /* M at bp+60 */ int32 var _ /* Y at bp+56 */ int32 var _ /* h at bp+68 */ int32 var _ /* m at bp+72 */ int32 var _ /* new at bp+8 */ TDateTime var _ /* r at bp+0 */ float64 var _ /* tx at bp+80 */ TDateTime _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = Z, cnt, day, i, iErr, iGuess, iOrigJD, rRounder, rc, x, y, z0, z2, v1, v10, v12, v2, v3, v5, v6, v7, v9 rc = int32(1) switch int32(_sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(z))]) { case int32('a'): /* ** auto ** ** If rawS is available, then interpret as a julian day number, or ** a unix timestamp, depending on its magnitude. */ if Xsqlite3_stricmp(tls, z, __ccgo_ts+1230) == 0 { if idx > int32(1) { return int32(1) } /* IMP: R-33611-57934 */ _autoAdjustDate(tls, p) rc = 0 } case int32('j'): /* ** julianday ** ** Always interpret the prior number as a julian-day value. If this ** is not the first modifier, or if the prior argument is not a numeric ** value in the allowed range of julian day numbers understood by ** SQLite (0..5373484.5) then the result will be NULL. */ if Xsqlite3_stricmp(tls, z, __ccgo_ts+1235) == 0 { if idx > int32(1) { return int32(1) } /* IMP: R-31176-64601 */ if (*TDateTime)(unsafe.Pointer(p)).FvalidJD != 0 && (*TDateTime)(unsafe.Pointer(p)).FrawS != 0 { rc = 0 (*TDateTime)(unsafe.Pointer(p)).FrawS = uint8(0) } } case int32('l'): /* localtime ** ** Assuming the current time value is UTC (a.k.a. GMT), shift it to ** show local time. */ if Xsqlite3_stricmp(tls, z, __ccgo_ts+1245) == 0 && _sqlite3NotPureFunc(tls, pCtx) != 0 { rc = _toLocaltime(tls, p, pCtx) } case int32('u'): /* ** unixepoch ** ** Treat the current value of p->s as the number of ** seconds since 1970. Convert to a real julian day number. */ if Xsqlite3_stricmp(tls, z, __ccgo_ts+1255) == 0 && (*TDateTime)(unsafe.Pointer(p)).FrawS != 0 { if idx > int32(1) { return int32(1) } /* IMP: R-49255-55373 */ *(*float64)(unsafe.Pointer(bp)) = (*TDateTime)(unsafe.Pointer(p)).Fs*float64(1000) + float64(2.1086676e+14) if *(*float64)(unsafe.Pointer(bp)) >= float64(0) && *(*float64)(unsafe.Pointer(bp)) < float64(4.642690608e+14) { _clearYMD_HMS_TZ(tls, p) (*TDateTime)(unsafe.Pointer(p)).FiJD = int64(*(*float64)(unsafe.Pointer(bp)) + libc.Float64FromFloat64(0.5)) (*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(1) (*TDateTime)(unsafe.Pointer(p)).FrawS = uint8(0) rc = 0 } } else { if Xsqlite3_stricmp(tls, z, __ccgo_ts+1265) == 0 && _sqlite3NotPureFunc(tls, pCtx) != 0 { if int32((*TDateTime)(unsafe.Pointer(p)).FtzSet) == 0 { /* Guess at the corresponding utc time */ cnt = 0 /* Guess is off by this much */ _computeJD(tls, p) v1 = (*TDateTime)(unsafe.Pointer(p)).FiJD iOrigJD = v1 iGuess = v1 iErr = 0 for { libc.Xmemset(tls, bp+8, 0, uint32(48)) iGuess -= iErr (*(*TDateTime)(unsafe.Pointer(bp + 8))).FiJD = iGuess (*(*TDateTime)(unsafe.Pointer(bp + 8))).FvalidJD = uint8(1) rc = _toLocaltime(tls, bp+8, pCtx) if rc != 0 { return rc } _computeJD(tls, bp+8) iErr = (*(*TDateTime)(unsafe.Pointer(bp + 8))).FiJD - iOrigJD goto _4 _4: ; if v3 = iErr != 0; v3 { v2 = cnt cnt++ } if !(v3 && v2 < int32(3)) { break } } libc.Xmemset(tls, p, 0, uint32(48)) (*TDateTime)(unsafe.Pointer(p)).FiJD = iGuess (*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(1) (*TDateTime)(unsafe.Pointer(p)).FtzSet = uint8(1) } rc = SQLITE_OK } } case int32('w'): /* ** weekday N ** ** Move the date to the same time on the next occurrence of ** weekday N where 0==Sunday, 1==Monday, and so forth. If the ** date is already on the appropriate weekday, this is a no-op. */ if v6 = Xsqlite3_strnicmp(tls, z, __ccgo_ts+1269, int32(8)) == 0 && _sqlite3AtoF(tls, z+8, bp, _sqlite3Strlen30(tls, z+8), uint8(SQLITE_UTF8)) > 0 && *(*float64)(unsafe.Pointer(bp)) >= float64(0) && *(*float64)(unsafe.Pointer(bp)) < float64(7); v6 { v5 = int32(*(*float64)(unsafe.Pointer(bp))) n = v5 } if v6 && float64(v5) == *(*float64)(unsafe.Pointer(bp)) { _computeYMD_HMS(tls, p) (*TDateTime)(unsafe.Pointer(p)).FvalidTZ = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(0) _computeJD(tls, p) Z = ((*TDateTime)(unsafe.Pointer(p)).FiJD + int64(129600000)) / int64(86400000) % int64(7) if Z > int64(n) { Z -= int64(7) } *(*Tsqlite3_int64)(unsafe.Pointer(p)) += (int64(n) - Z) * int64(86400000) _clearYMD_HMS_TZ(tls, p) rc = 0 } case int32('s'): /* ** start of TTTTT ** ** Move the date backwards to the beginning of the current day, ** or month or year. ** ** subsecond ** subsec ** ** Show subsecond precision in the output of datetime() and ** unixepoch() and strftime('%s'). */ if Xsqlite3_strnicmp(tls, z, __ccgo_ts+1278, int32(9)) != 0 { if Xsqlite3_stricmp(tls, z, __ccgo_ts+1190) == 0 || Xsqlite3_stricmp(tls, z, __ccgo_ts+1197) == 0 { (*TDateTime)(unsafe.Pointer(p)).FuseSubsec = uint8(1) rc = 0 } break } if !((*TDateTime)(unsafe.Pointer(p)).FvalidJD != 0) && !((*TDateTime)(unsafe.Pointer(p)).FvalidYMD != 0) && !((*TDateTime)(unsafe.Pointer(p)).FvalidHMS != 0) { break } z += uintptr(9) _computeYMD(tls, p) (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = uint8(1) v7 = libc.Int32FromInt32(0) (*TDateTime)(unsafe.Pointer(p)).Fm = v7 (*TDateTime)(unsafe.Pointer(p)).Fh = v7 (*TDateTime)(unsafe.Pointer(p)).Fs = float64(0) (*TDateTime)(unsafe.Pointer(p)).FrawS = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FvalidTZ = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(0) if Xsqlite3_stricmp(tls, z, __ccgo_ts+1288) == 0 { (*TDateTime)(unsafe.Pointer(p)).FD = int32(1) rc = 0 } else { if Xsqlite3_stricmp(tls, z, __ccgo_ts+1294) == 0 { (*TDateTime)(unsafe.Pointer(p)).FM = int32(1) (*TDateTime)(unsafe.Pointer(p)).FD = int32(1) rc = 0 } else { if Xsqlite3_stricmp(tls, z, __ccgo_ts+1299) == 0 { rc = 0 } } } case int32('+'): fallthrough case int32('-'): fallthrough case int32('0'): fallthrough case int32('1'): fallthrough case int32('2'): fallthrough case int32('3'): fallthrough case int32('4'): fallthrough case int32('5'): fallthrough case int32('6'): fallthrough case int32('7'): fallthrough case int32('8'): fallthrough case int32('9'): z2 = z z0 = *(*uint8)(unsafe.Pointer(z)) n = int32(1) for { if !(*(*uint8)(unsafe.Pointer(z + uintptr(n))) != 0) { break } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(n)))) == int32(':') { break } if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(n)))])&int32(0x01) != 0 { break } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(n)))) == int32('-') { if n == int32(5) && _getDigits(tls, z+1, __ccgo_ts+1303, libc.VaList(bp+136, bp+56)) == int32(1) { break } if n == int32(6) && _getDigits(tls, z+1, __ccgo_ts+1307, libc.VaList(bp+136, bp+56)) == int32(1) { break } } goto _8 _8: ; n++ } if _sqlite3AtoF(tls, z, bp, n, uint8(SQLITE_UTF8)) <= 0 { _ = libc.Int32FromInt32(0) break } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(n)))) == int32('-') { /* A modifier of the form (+|-)YYYY-MM-DD adds or subtracts the ** specified number of years, months, and days. MM is limited to ** the range 0-11 and DD is limited to 0-30. */ if int32(z0) != int32('+') && int32(z0) != int32('-') { break } /* Must start with +/- */ if n == int32(5) { if _getDigits(tls, z+1, __ccgo_ts+1311, libc.VaList(bp+136, bp+56, bp+60, bp+64)) != int32(3) { break } } else { _ = libc.Int32FromInt32(0) if _getDigits(tls, z+1, __ccgo_ts+1323, libc.VaList(bp+136, bp+56, bp+60, bp+64)) != int32(3) { break } z++ } if *(*int32)(unsafe.Pointer(bp + 60)) >= int32(12) { break } /* M range 0..11 */ if *(*int32)(unsafe.Pointer(bp + 64)) >= int32(31) { break } /* D range 0..30 */ _computeYMD_HMS(tls, p) (*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(0) if int32(z0) == int32('-') { *(*int32)(unsafe.Pointer(p + 8)) -= *(*int32)(unsafe.Pointer(bp + 56)) *(*int32)(unsafe.Pointer(p + 12)) -= *(*int32)(unsafe.Pointer(bp + 60)) *(*int32)(unsafe.Pointer(bp + 64)) = -*(*int32)(unsafe.Pointer(bp + 64)) } else { *(*int32)(unsafe.Pointer(p + 8)) += *(*int32)(unsafe.Pointer(bp + 56)) *(*int32)(unsafe.Pointer(p + 12)) += *(*int32)(unsafe.Pointer(bp + 60)) } if (*TDateTime)(unsafe.Pointer(p)).FM > 0 { v9 = ((*TDateTime)(unsafe.Pointer(p)).FM - int32(1)) / int32(12) } else { v9 = ((*TDateTime)(unsafe.Pointer(p)).FM - int32(12)) / int32(12) } x = v9 *(*int32)(unsafe.Pointer(p + 8)) += x *(*int32)(unsafe.Pointer(p + 12)) -= x * int32(12) _computeJD(tls, p) (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = uint8(0) (*TDateTime)(unsafe.Pointer(p)).FvalidYMD = uint8(0) *(*Tsqlite3_int64)(unsafe.Pointer(p)) += int64(*(*int32)(unsafe.Pointer(bp + 64))) * int64(86400000) if int32(*(*uint8)(unsafe.Pointer(z + 11))) == 0 { rc = 0 break } if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + 11))])&int32(0x01) != 0 && _getDigits(tls, z+12, __ccgo_ts+1162, libc.VaList(bp+136, bp+68, bp+72)) == int32(2) { z2 = z + 12 n = int32(2) } else { break } } if int32(*(*uint8)(unsafe.Pointer(z2 + uintptr(n)))) == int32(':') { if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z2))])&libc.Int32FromInt32(0x04) != 0) { z2++ } libc.Xmemset(tls, bp+80, 0, uint32(48)) if _parseHhMmSs(tls, z2, bp+80) != 0 { break } _computeJD(tls, bp+80) (*(*TDateTime)(unsafe.Pointer(bp + 80))).FiJD -= int64(43200000) day = (*(*TDateTime)(unsafe.Pointer(bp + 80))).FiJD / int64(86400000) (*(*TDateTime)(unsafe.Pointer(bp + 80))).FiJD -= day * int64(86400000) if int32(z0) == int32('-') { (*(*TDateTime)(unsafe.Pointer(bp + 80))).FiJD = -(*(*TDateTime)(unsafe.Pointer(bp + 80))).FiJD } _computeJD(tls, p) _clearYMD_HMS_TZ(tls, p) *(*Tsqlite3_int64)(unsafe.Pointer(p)) += (*(*TDateTime)(unsafe.Pointer(bp + 80))).FiJD rc = 0 break } /* If control reaches this point, it means the transformation is ** one of the forms like "+NNN days". */ z += uintptr(n) for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z))])&int32(0x01) != 0 { z++ } n = _sqlite3Strlen30(tls, z) if n > int32(10) || n < int32(3) { break } if int32(_sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(z + uintptr(n-int32(1))))]) == int32('s') { n-- } _computeJD(tls, p) _ = libc.Int32FromInt32(0) if *(*float64)(unsafe.Pointer(bp)) < libc.Float64FromInt32(0) { v10 = -libc.Float64FromFloat64(0.5) } else { v10 = +libc.Float64FromFloat64(0.5) } rRounder = v10 i = 0 for { if !(i < int32(libc.Uint32FromInt64(96)/libc.Uint32FromInt64(16))) { break } if int32(_aXformType[i].FnName) == n && Xsqlite3_strnicmp(tls, uintptr(unsafe.Pointer(&_aXformType))+uintptr(i)*16+1, z, n) == 0 && *(*float64)(unsafe.Pointer(bp)) > float64(-_aXformType[i].FrLimit) && *(*float64)(unsafe.Pointer(bp)) < float64(_aXformType[i].FrLimit) { switch i { case int32(4): /* Special processing to add months */ _ = libc.Int32FromInt32(0) _computeYMD_HMS(tls, p) *(*int32)(unsafe.Pointer(p + 12)) += int32(*(*float64)(unsafe.Pointer(bp))) if (*TDateTime)(unsafe.Pointer(p)).FM > 0 { v12 = ((*TDateTime)(unsafe.Pointer(p)).FM - int32(1)) / int32(12) } else { v12 = ((*TDateTime)(unsafe.Pointer(p)).FM - int32(12)) / int32(12) } x = v12 *(*int32)(unsafe.Pointer(p + 8)) += x *(*int32)(unsafe.Pointer(p + 12)) -= x * int32(12) (*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(0) *(*float64)(unsafe.Pointer(bp)) -= float64(int32(*(*float64)(unsafe.Pointer(bp)))) case int32(5): /* Special processing to add years */ y = int32(*(*float64)(unsafe.Pointer(bp))) _ = libc.Int32FromInt32(0) _computeYMD_HMS(tls, p) *(*int32)(unsafe.Pointer(p + 8)) += y (*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(0) *(*float64)(unsafe.Pointer(bp)) -= float64(int32(*(*float64)(unsafe.Pointer(bp)))) break } _computeJD(tls, p) *(*Tsqlite3_int64)(unsafe.Pointer(p)) += int64(*(*float64)(unsafe.Pointer(bp))*libc.Float64FromFloat64(1000)*float64(_aXformType[i].FrXform) + rRounder) rc = 0 break } goto _11 _11: ; i++ } _clearYMD_HMS_TZ(tls, p) default: break } return rc } // C documentation // // /* // ** Process time function arguments. argv[0] is a date-time stamp. // ** argv[1] and following are modifiers. Parse them all and write // ** the resulting time into the DateTime structure p. Return 0 // ** on success and 1 if there are any errors. // ** // ** If there are zero parameters (if even argv[0] is undefined) // ** then assume a default value of "now" for argv[0]. // */ func _isDate(tls *libc.TLS, context uintptr, argc int32, argv uintptr, p uintptr) (r int32) { var eType, i, n, v1 int32 var z uintptr _, _, _, _, _ = eType, i, n, z, v1 libc.Xmemset(tls, p, 0, uint32(48)) if argc == 0 { if !(_sqlite3NotPureFunc(tls, context) != 0) { return int32(1) } return _setDateTimeToCurrent(tls, context, p) } v1 = Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) eType = v1 if v1 == int32(SQLITE_FLOAT) || eType == int32(SQLITE_INTEGER) { _setRawDateNumber(tls, p, Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv)))) } else { z = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) if !(z != 0) || _parseDateOrTime(tls, context, z, p) != 0 { return int32(1) } } i = int32(1) for { if !(i < argc) { break } z = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) n = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) if z == uintptr(0) || _parseModifier(tls, context, z, n, p, i) != 0 { return int32(1) } goto _2 _2: ; i++ } _computeJD(tls, p) if (*TDateTime)(unsafe.Pointer(p)).FisError != 0 || !(_validJulianDay(tls, (*TDateTime)(unsafe.Pointer(p)).FiJD) != 0) { return int32(1) } if argc == int32(1) && (*TDateTime)(unsafe.Pointer(p)).FvalidYMD != 0 && (*TDateTime)(unsafe.Pointer(p)).FD > int32(28) { /* Make sure a YYYY-MM-DD is normalized. ** Example: 2023-02-31 -> 2023-03-03 */ _ = libc.Int32FromInt32(0) (*TDateTime)(unsafe.Pointer(p)).FvalidYMD = uint8(0) } return 0 } /* ** The following routines implement the various date and time functions ** of SQLite. */ // C documentation // // /* // ** julianday( TIMESTRING, MOD, MOD, ...) // ** // ** Return the julian day number of the date specified in the arguments // */ func _juliandayFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* x at bp+0 */ TDateTime if _isDate(tls, context, argc, argv, bp) == 0 { _computeJD(tls, bp) Xsqlite3_result_double(tls, context, float64((*(*TDateTime)(unsafe.Pointer(bp))).FiJD)/float64(8.64e+07)) } } // C documentation // // /* // ** unixepoch( TIMESTRING, MOD, MOD, ...) // ** // ** Return the number of seconds (including fractional seconds) since // ** the unix epoch of 1970-01-01 00:00:00 GMT. // */ func _unixepochFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* x at bp+0 */ TDateTime if _isDate(tls, context, argc, argv, bp) == 0 { _computeJD(tls, bp) if (*(*TDateTime)(unsafe.Pointer(bp))).FuseSubsec != 0 { Xsqlite3_result_double(tls, context, float64((*(*TDateTime)(unsafe.Pointer(bp))).FiJD-libc.Int64FromInt32(21086676)*libc.Int64FromInt32(10000000))/float64(1000)) } else { Xsqlite3_result_int64(tls, context, (*(*TDateTime)(unsafe.Pointer(bp))).FiJD/int64(1000)-libc.Int64FromInt32(21086676)*libc.Int64FromInt32(10000)) } } } // C documentation // // /* // ** datetime( TIMESTRING, MOD, MOD, ...) // ** // ** Return YYYY-MM-DD HH:MM:SS // */ func _datetimeFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(80) defer tls.Free(80) var Y, n, s int32 var _ /* x at bp+0 */ TDateTime var _ /* zBuf at bp+48 */ [32]uint8 _, _, _ = Y, n, s if _isDate(tls, context, argc, argv, bp) == 0 { _computeYMD_HMS(tls, bp) Y = (*(*TDateTime)(unsafe.Pointer(bp))).FY if Y < 0 { Y = -Y } (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(1)] = uint8(int32('0') + Y/int32(1000)%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(2)] = uint8(int32('0') + Y/int32(100)%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(3)] = uint8(int32('0') + Y/int32(10)%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(4)] = uint8(int32('0') + Y%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(5)] = uint8('-') (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(6)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FM/int32(10)%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(7)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FM%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(8)] = uint8('-') (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(9)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FD/int32(10)%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(10)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FD%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(11)] = uint8(' ') (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(12)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fh/int32(10)%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(13)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fh%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(14)] = uint8(':') (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(15)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fm/int32(10)%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(16)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fm%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(17)] = uint8(':') if (*(*TDateTime)(unsafe.Pointer(bp))).FuseSubsec != 0 { s = int32(libc.Float64FromFloat64(1000)*(*(*TDateTime)(unsafe.Pointer(bp))).Fs + libc.Float64FromFloat64(0.5)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(18)] = uint8(int32('0') + s/int32(10000)%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(19)] = uint8(int32('0') + s/int32(1000)%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(20)] = uint8('.') (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(21)] = uint8(int32('0') + s/int32(100)%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(22)] = uint8(int32('0') + s/int32(10)%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(23)] = uint8(int32('0') + s%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(24)] = uint8(0) n = int32(24) } else { s = int32((*(*TDateTime)(unsafe.Pointer(bp))).Fs) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(18)] = uint8(int32('0') + s/int32(10)%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(19)] = uint8(int32('0') + s%int32(10)) (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[int32(20)] = uint8(0) n = int32(20) } if (*(*TDateTime)(unsafe.Pointer(bp))).FY < 0 { (*(*[32]uint8)(unsafe.Pointer(bp + 48)))[0] = uint8('-') Xsqlite3_result_text(tls, context, bp+48, n, uintptr(-libc.Int32FromInt32(1))) } else { Xsqlite3_result_text(tls, context, bp+48+1, n-int32(1), uintptr(-libc.Int32FromInt32(1))) } } } // C documentation // // /* // ** time( TIMESTRING, MOD, MOD, ...) // ** // ** Return HH:MM:SS // */ func _timeFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var n, s int32 var _ /* x at bp+0 */ TDateTime var _ /* zBuf at bp+48 */ [16]uint8 _, _ = n, s if _isDate(tls, context, argc, argv, bp) == 0 { _computeHMS(tls, bp) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[0] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fh/int32(10)%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(1)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fh%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(2)] = uint8(':') (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(3)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fm/int32(10)%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(4)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fm%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(5)] = uint8(':') if (*(*TDateTime)(unsafe.Pointer(bp))).FuseSubsec != 0 { s = int32(libc.Float64FromFloat64(1000)*(*(*TDateTime)(unsafe.Pointer(bp))).Fs + libc.Float64FromFloat64(0.5)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(6)] = uint8(int32('0') + s/int32(10000)%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(7)] = uint8(int32('0') + s/int32(1000)%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(8)] = uint8('.') (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(9)] = uint8(int32('0') + s/int32(100)%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(10)] = uint8(int32('0') + s/int32(10)%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(11)] = uint8(int32('0') + s%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(12)] = uint8(0) n = int32(12) } else { s = int32((*(*TDateTime)(unsafe.Pointer(bp))).Fs) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(6)] = uint8(int32('0') + s/int32(10)%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(7)] = uint8(int32('0') + s%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(8)] = uint8(0) n = int32(8) } Xsqlite3_result_text(tls, context, bp+48, n, uintptr(-libc.Int32FromInt32(1))) } } // C documentation // // /* // ** date( TIMESTRING, MOD, MOD, ...) // ** // ** Return YYYY-MM-DD // */ func _dateFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var Y int32 var _ /* x at bp+0 */ TDateTime var _ /* zBuf at bp+48 */ [16]uint8 _ = Y if _isDate(tls, context, argc, argv, bp) == 0 { _computeYMD(tls, bp) Y = (*(*TDateTime)(unsafe.Pointer(bp))).FY if Y < 0 { Y = -Y } (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(1)] = uint8(int32('0') + Y/int32(1000)%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(2)] = uint8(int32('0') + Y/int32(100)%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(3)] = uint8(int32('0') + Y/int32(10)%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(4)] = uint8(int32('0') + Y%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(5)] = uint8('-') (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(6)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FM/int32(10)%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(7)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FM%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(8)] = uint8('-') (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(9)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FD/int32(10)%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(10)] = uint8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FD%int32(10)) (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[int32(11)] = uint8(0) if (*(*TDateTime)(unsafe.Pointer(bp))).FY < 0 { (*(*[16]uint8)(unsafe.Pointer(bp + 48)))[0] = uint8('-') Xsqlite3_result_text(tls, context, bp+48, int32(11), uintptr(-libc.Int32FromInt32(1))) } else { Xsqlite3_result_text(tls, context, bp+48+1, int32(10), uintptr(-libc.Int32FromInt32(1))) } } } // C documentation // // /* // ** strftime( FORMAT, TIMESTRING, MOD, MOD, ...) // ** // ** Return a string described by FORMAT. Conversions as follows: // ** // ** %d day of month // ** %f ** fractional seconds SS.SSS // ** %H hour 00-24 // ** %j day of year 000-366 // ** %J ** julian day number // ** %m month 01-12 // ** %M minute 00-59 // ** %s seconds since 1970-01-01 // ** %S seconds 00-59 // ** %w day of week 0-6 Sunday==0 // ** %W week of year 00-53 // ** %Y year 0000-9999 // ** %% % // */ func _strftimeFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(160) defer tls.Free(160) var c, cf uint8 var db, zFmt, v3, v4, v5, v6, v7 uintptr var h, nDay, wd int32 var i, j, v2 Tsize_t var iS Ti64 var s float64 var _ /* sRes at bp+48 */ Tsqlite3_str var _ /* x at bp+0 */ TDateTime var _ /* y at bp+72 */ TDateTime _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, cf, db, h, i, iS, j, nDay, s, wd, zFmt, v2, v3, v4, v5, v6, v7 if argc == 0 { return } zFmt = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) if zFmt == uintptr(0) || _isDate(tls, context, argc-int32(1), argv+uintptr(1)*4, bp) != 0 { return } db = Xsqlite3_context_db_handle(tls, context) _sqlite3StrAccumInit(tls, bp+48, uintptr(0), uintptr(0), 0, *(*int32)(unsafe.Pointer(db + 120))) _computeJD(tls, bp) _computeYMD_HMS(tls, bp) v2 = libc.Uint32FromInt32(0) j = v2 i = v2 for { if !(*(*uint8)(unsafe.Pointer(zFmt + uintptr(i))) != 0) { break } if int32(*(*uint8)(unsafe.Pointer(zFmt + uintptr(i)))) != int32('%') { goto _1 } if j < i { Xsqlite3_str_append(tls, bp+48, zFmt+uintptr(j), int32(i-j)) } i++ j = i + uint32(1) cf = *(*uint8)(unsafe.Pointer(zFmt + uintptr(i))) switch int32(cf) { case int32('d'): /* Fall thru */ fallthrough case int32('e'): if int32(cf) == int32('d') { v3 = __ccgo_ts + 1335 } else { v3 = __ccgo_ts + 1340 } Xsqlite3_str_appendf(tls, bp+48, v3, libc.VaList(bp+128, (*(*TDateTime)(unsafe.Pointer(bp))).FD)) case int32('f'): s = (*(*TDateTime)(unsafe.Pointer(bp))).Fs if s > float64(59.999) { s = float64(59.999) } Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1344, libc.VaList(bp+128, s)) case int32('F'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1351, libc.VaList(bp+128, (*(*TDateTime)(unsafe.Pointer(bp))).FY, (*(*TDateTime)(unsafe.Pointer(bp))).FM, (*(*TDateTime)(unsafe.Pointer(bp))).FD)) case int32('H'): fallthrough case int32('k'): if int32(cf) == int32('H') { v4 = __ccgo_ts + 1335 } else { v4 = __ccgo_ts + 1340 } Xsqlite3_str_appendf(tls, bp+48, v4, libc.VaList(bp+128, (*(*TDateTime)(unsafe.Pointer(bp))).Fh)) case int32('I'): /* Fall thru */ fallthrough case int32('l'): h = (*(*TDateTime)(unsafe.Pointer(bp))).Fh if h > int32(12) { h -= int32(12) } if h == 0 { h = int32(12) } if int32(cf) == int32('I') { v5 = __ccgo_ts + 1335 } else { v5 = __ccgo_ts + 1340 } Xsqlite3_str_appendf(tls, bp+48, v5, libc.VaList(bp+128, h)) case int32('W'): /* Fall thru */ fallthrough case int32('j'): /* Number of days since 1st day of year */ *(*TDateTime)(unsafe.Pointer(bp + 72)) = *(*TDateTime)(unsafe.Pointer(bp)) (*(*TDateTime)(unsafe.Pointer(bp + 72))).FvalidJD = uint8(0) (*(*TDateTime)(unsafe.Pointer(bp + 72))).FM = int32(1) (*(*TDateTime)(unsafe.Pointer(bp + 72))).FD = int32(1) _computeJD(tls, bp+72) nDay = int32(((*(*TDateTime)(unsafe.Pointer(bp))).FiJD - (*(*TDateTime)(unsafe.Pointer(bp + 72))).FiJD + libc.Int64FromInt32(43200000)) / libc.Int64FromInt32(86400000)) if int32(cf) == int32('W') { /* 0=Monday, 1=Tuesday, ... 6=Sunday */ wd = int32(((*(*TDateTime)(unsafe.Pointer(bp))).FiJD + libc.Int64FromInt32(43200000)) / libc.Int64FromInt32(86400000) % libc.Int64FromInt32(7)) Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1335, libc.VaList(bp+128, (nDay+int32(7)-wd)/int32(7))) } else { Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1366, libc.VaList(bp+128, nDay+int32(1))) } case int32('J'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1371, libc.VaList(bp+128, float64((*(*TDateTime)(unsafe.Pointer(bp))).FiJD)/float64(8.64e+07))) case int32('m'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1335, libc.VaList(bp+128, (*(*TDateTime)(unsafe.Pointer(bp))).FM)) case int32('M'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1335, libc.VaList(bp+128, (*(*TDateTime)(unsafe.Pointer(bp))).Fm)) case int32('p'): /* Fall thru */ fallthrough case int32('P'): if (*(*TDateTime)(unsafe.Pointer(bp))).Fh >= int32(12) { if int32(cf) == int32('p') { v6 = __ccgo_ts + 1377 } else { v6 = __ccgo_ts + 1380 } Xsqlite3_str_append(tls, bp+48, v6, int32(2)) } else { if int32(cf) == int32('p') { v7 = __ccgo_ts + 1383 } else { v7 = __ccgo_ts + 1386 } Xsqlite3_str_append(tls, bp+48, v7, int32(2)) } case int32('R'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1389, libc.VaList(bp+128, (*(*TDateTime)(unsafe.Pointer(bp))).Fh, (*(*TDateTime)(unsafe.Pointer(bp))).Fm)) case int32('s'): if (*(*TDateTime)(unsafe.Pointer(bp))).FuseSubsec != 0 { Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1399, libc.VaList(bp+128, float64((*(*TDateTime)(unsafe.Pointer(bp))).FiJD-libc.Int64FromInt32(21086676)*libc.Int64FromInt32(10000000))/float64(1000))) } else { iS = (*(*TDateTime)(unsafe.Pointer(bp))).FiJD/libc.Int64FromInt32(1000) - libc.Int64FromInt32(21086676)*libc.Int64FromInt32(10000) Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1404, libc.VaList(bp+128, iS)) } case int32('S'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1335, libc.VaList(bp+128, int32((*(*TDateTime)(unsafe.Pointer(bp))).Fs))) case int32('T'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1409, libc.VaList(bp+128, (*(*TDateTime)(unsafe.Pointer(bp))).Fh, (*(*TDateTime)(unsafe.Pointer(bp))).Fm, int32((*(*TDateTime)(unsafe.Pointer(bp))).Fs))) case int32('u'): /* Fall thru */ fallthrough case int32('w'): c = uint8(int32(uint8(((*(*TDateTime)(unsafe.Pointer(bp))).FiJD+libc.Int64FromInt32(129600000))/libc.Int64FromInt32(86400000)%libc.Int64FromInt32(7))) + int32('0')) if int32(c) == int32('0') && int32(cf) == int32('u') { c = uint8('7') } Xsqlite3_str_appendchar(tls, bp+48, int32(1), c) case int32('Y'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1424, libc.VaList(bp+128, (*(*TDateTime)(unsafe.Pointer(bp))).FY)) case int32('%'): Xsqlite3_str_appendchar(tls, bp+48, int32(1), uint8('%')) default: Xsqlite3_str_reset(tls, bp+48) return } goto _1 _1: ; i++ } if j < i { Xsqlite3_str_append(tls, bp+48, zFmt+uintptr(j), int32(i-j)) } _sqlite3ResultStrAccum(tls, context, bp+48) } // C documentation // // /* // ** current_time() // ** // ** This function returns the same value as time('now'). // */ func _ctimeFunc(tls *libc.TLS, context uintptr, NotUsed int32, NotUsed2 uintptr) { _ = NotUsed _ = NotUsed2 _timeFunc(tls, context, 0, uintptr(0)) } // C documentation // // /* // ** current_date() // ** // ** This function returns the same value as date('now'). // */ func _cdateFunc(tls *libc.TLS, context uintptr, NotUsed int32, NotUsed2 uintptr) { _ = NotUsed _ = NotUsed2 _dateFunc(tls, context, 0, uintptr(0)) } // C documentation // // /* // ** timediff(DATE1, DATE2) // ** // ** Return the amount of time that must be added to DATE2 in order to // ** convert it into DATE2. The time difference format is: // ** // ** +YYYY-MM-DD HH:MM:SS.SSS // ** // ** The initial "+" becomes "-" if DATE1 occurs before DATE2. For // ** date/time values A and B, the following invariant should hold: // ** // ** datetime(A) == (datetime(B, timediff(A,B)) // ** // ** Both DATE arguments must be either a julian day number, or an // ** ISO-8601 string. The unix timestamps are not supported by this // ** routine. // */ func _timediffFunc(tls *libc.TLS, context uintptr, NotUsed1 int32, argv uintptr) { bp := tls.Alloc(192) defer tls.Free(192) var M, Y int32 var sign uint8 var p1, p2 uintptr var _ /* d1 at bp+0 */ TDateTime var _ /* d2 at bp+48 */ TDateTime var _ /* sRes at bp+96 */ Tsqlite3_str _, _, _, _, _ = M, Y, sign, p1, p2 _ = NotUsed1 if _isDate(tls, context, int32(1), argv, bp) != 0 { return } if _isDate(tls, context, int32(1), argv+1*4, bp+48) != 0 { return } _computeYMD_HMS(tls, bp) _computeYMD_HMS(tls, bp+48) if (*(*TDateTime)(unsafe.Pointer(bp))).FiJD >= (*(*TDateTime)(unsafe.Pointer(bp + 48))).FiJD { sign = uint8('+') Y = (*(*TDateTime)(unsafe.Pointer(bp))).FY - (*(*TDateTime)(unsafe.Pointer(bp + 48))).FY if Y != 0 { (*(*TDateTime)(unsafe.Pointer(bp + 48))).FY = (*(*TDateTime)(unsafe.Pointer(bp))).FY (*(*TDateTime)(unsafe.Pointer(bp + 48))).FvalidJD = uint8(0) _computeJD(tls, bp+48) } M = (*(*TDateTime)(unsafe.Pointer(bp))).FM - (*(*TDateTime)(unsafe.Pointer(bp + 48))).FM if M < 0 { Y-- M += int32(12) } if M != 0 { (*(*TDateTime)(unsafe.Pointer(bp + 48))).FM = (*(*TDateTime)(unsafe.Pointer(bp))).FM (*(*TDateTime)(unsafe.Pointer(bp + 48))).FvalidJD = uint8(0) _computeJD(tls, bp+48) } for (*(*TDateTime)(unsafe.Pointer(bp))).FiJD < (*(*TDateTime)(unsafe.Pointer(bp + 48))).FiJD { M-- if M < 0 { M = int32(11) Y-- } (*(*TDateTime)(unsafe.Pointer(bp + 48))).FM-- if (*(*TDateTime)(unsafe.Pointer(bp + 48))).FM < int32(1) { (*(*TDateTime)(unsafe.Pointer(bp + 48))).FM = int32(12) (*(*TDateTime)(unsafe.Pointer(bp + 48))).FY-- } (*(*TDateTime)(unsafe.Pointer(bp + 48))).FvalidJD = uint8(0) _computeJD(tls, bp+48) } (*(*TDateTime)(unsafe.Pointer(bp))).FiJD -= (*(*TDateTime)(unsafe.Pointer(bp + 48))).FiJD p1 = bp *(*Tsqlite3_int64)(unsafe.Pointer(p1)) = Tsqlite3_int64(uint64(*(*Tsqlite3_int64)(unsafe.Pointer(p1))) + libc.Uint64FromInt32(1486995408)*libc.Uint64FromInt32(100000)) } else { /* d1 (*(*TDateTime)(unsafe.Pointer(bp + 48))).FiJD { M-- if M < 0 { M = int32(11) Y-- } (*(*TDateTime)(unsafe.Pointer(bp + 48))).FM++ if (*(*TDateTime)(unsafe.Pointer(bp + 48))).FM > int32(12) { (*(*TDateTime)(unsafe.Pointer(bp + 48))).FM = int32(1) (*(*TDateTime)(unsafe.Pointer(bp + 48))).FY++ } (*(*TDateTime)(unsafe.Pointer(bp + 48))).FvalidJD = uint8(0) _computeJD(tls, bp+48) } (*(*TDateTime)(unsafe.Pointer(bp))).FiJD = (*(*TDateTime)(unsafe.Pointer(bp + 48))).FiJD - (*(*TDateTime)(unsafe.Pointer(bp))).FiJD p2 = bp *(*Tsqlite3_int64)(unsafe.Pointer(p2)) = Tsqlite3_int64(uint64(*(*Tsqlite3_int64)(unsafe.Pointer(p2))) + libc.Uint64FromInt32(1486995408)*libc.Uint64FromInt32(100000)) } (*(*TDateTime)(unsafe.Pointer(bp))).FvalidYMD = uint8(0) (*(*TDateTime)(unsafe.Pointer(bp))).FvalidHMS = uint8(0) (*(*TDateTime)(unsafe.Pointer(bp))).FvalidTZ = uint8(0) _computeYMD_HMS(tls, bp) _sqlite3StrAccumInit(tls, bp+96, uintptr(0), uintptr(0), 0, int32(100)) Xsqlite3_str_appendf(tls, bp+96, __ccgo_ts+1429, libc.VaList(bp+128, int32(sign), Y, M, (*(*TDateTime)(unsafe.Pointer(bp))).FD-int32(1), (*(*TDateTime)(unsafe.Pointer(bp))).Fh, (*(*TDateTime)(unsafe.Pointer(bp))).Fm, (*(*TDateTime)(unsafe.Pointer(bp))).Fs)) _sqlite3ResultStrAccum(tls, context, bp+96) } // C documentation // // /* // ** current_timestamp() // ** // ** This function returns the same value as datetime('now'). // */ func _ctimestampFunc(tls *libc.TLS, context uintptr, NotUsed int32, NotUsed2 uintptr) { _ = NotUsed _ = NotUsed2 _datetimeFunc(tls, context, 0, uintptr(0)) } // C documentation // // /* // ** This function registered all of the above C functions as SQL // ** functions. This should be the only routine in this file with // ** external linkage. // */ func _sqlite3RegisterDateTimeFunctions(tls *libc.TLS) { _sqlite3InsertBuiltinFuncs(tls, uintptr(unsafe.Pointer(&_aDateTimeFuncs)), int32(libc.Uint32FromInt64(400)/libc.Uint32FromInt64(40))) } var _aDateTimeFuncs = [10]TFuncDef{ 0: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)), FpUserData: uintptr(unsafe.Pointer(&_sqlite3Config)), FzName: __ccgo_ts + 1235, }, 1: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)), FpUserData: uintptr(unsafe.Pointer(&_sqlite3Config)), FzName: __ccgo_ts + 1255, }, 2: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)), FpUserData: uintptr(unsafe.Pointer(&_sqlite3Config)), FzName: __ccgo_ts + 1463, }, 3: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)), FpUserData: uintptr(unsafe.Pointer(&_sqlite3Config)), FzName: __ccgo_ts + 1468, }, 4: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)), FpUserData: uintptr(unsafe.Pointer(&_sqlite3Config)), FzName: __ccgo_ts + 1473, }, 5: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)), FpUserData: uintptr(unsafe.Pointer(&_sqlite3Config)), FzName: __ccgo_ts + 1482, }, 6: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)), FpUserData: uintptr(unsafe.Pointer(&_sqlite3Config)), FzName: __ccgo_ts + 1491, }, 7: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 1500, }, 8: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 1513, }, 9: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 1531, }, } func init() { p := unsafe.Pointer(&_aDateTimeFuncs) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_juliandayFunc) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_unixepochFunc) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_dateFunc) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_timeFunc) *(*uintptr)(unsafe.Add(p, 176)) = __ccgo_fp(_datetimeFunc) *(*uintptr)(unsafe.Add(p, 216)) = __ccgo_fp(_strftimeFunc) *(*uintptr)(unsafe.Add(p, 256)) = __ccgo_fp(_timediffFunc) *(*uintptr)(unsafe.Add(p, 296)) = __ccgo_fp(_ctimeFunc) *(*uintptr)(unsafe.Add(p, 336)) = __ccgo_fp(_ctimestampFunc) *(*uintptr)(unsafe.Add(p, 376)) = __ccgo_fp(_cdateFunc) } /************** End of date.c ************************************************/ /************** Begin file os.c **********************************************/ /* ** 2005 November 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 contains OS interface code that is common to all ** architectures. */ /* #include "sqliteInt.h" */ /* ** If we compile with the SQLITE_TEST macro set, then the following block ** of code will give us the ability to simulate a disk I/O error. This ** is used for testing the I/O recovery logic. */ /* ** When testing, also keep a count of the number of open files. */ /* ** The default SQLite sqlite3_vfs implementations do not allocate ** memory (actually, os_unix.c allocates a small amount of memory ** from within OsOpen()), but some third-party implementations may. ** So we test the effects of a malloc() failing and the sqlite3OsXXX() ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro. ** ** The following functions are instrumented for malloc() failure ** testing: ** ** sqlite3OsRead() ** sqlite3OsWrite() ** sqlite3OsSync() ** sqlite3OsFileSize() ** sqlite3OsLock() ** sqlite3OsCheckReservedLock() ** sqlite3OsFileControl() ** sqlite3OsShmMap() ** sqlite3OsOpen() ** sqlite3OsDelete() ** sqlite3OsAccess() ** sqlite3OsFullPathname() ** */ // C documentation // // /* // ** The following routines are convenience wrappers around methods // ** of the sqlite3_file object. This is mostly just syntactic sugar. All // ** of this would be completely automatic if SQLite were coded using // ** C++ instead of plain old C. // */ func _sqlite3OsClose(tls *libc.TLS, pId uintptr) { if (*Tsqlite3_file)(unsafe.Pointer(pId)).FpMethods != 0 { (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(pId)).FpMethods)).FxClose})))(tls, pId) (*Tsqlite3_file)(unsafe.Pointer(pId)).FpMethods = uintptr(0) } } func _sqlite3OsRead(tls *libc.TLS, id uintptr, pBuf uintptr, amt int32, offset Ti64) (r int32) { return (*(*func(*libc.TLS, uintptr, uintptr, int32, Tsqlite3_int64) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxRead})))(tls, id, pBuf, amt, offset) } func _sqlite3OsWrite(tls *libc.TLS, id uintptr, pBuf uintptr, amt int32, offset Ti64) (r int32) { return (*(*func(*libc.TLS, uintptr, uintptr, int32, Tsqlite3_int64) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxWrite})))(tls, id, pBuf, amt, offset) } func _sqlite3OsTruncate(tls *libc.TLS, id uintptr, size Ti64) (r int32) { return (*(*func(*libc.TLS, uintptr, Tsqlite3_int64) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxTruncate})))(tls, id, size) } func _sqlite3OsSync(tls *libc.TLS, id uintptr, flags int32) (r int32) { var v1 int32 _ = v1 if flags != 0 { v1 = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxSync})))(tls, id, flags) } else { v1 = SQLITE_OK } return v1 } func _sqlite3OsFileSize(tls *libc.TLS, id uintptr, pSize uintptr) (r int32) { return (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxFileSize})))(tls, id, pSize) } func _sqlite3OsLock(tls *libc.TLS, id uintptr, lockType int32) (r int32) { _ = libc.Int32FromInt32(0) return (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxLock})))(tls, id, lockType) } func _sqlite3OsUnlock(tls *libc.TLS, id uintptr, lockType int32) (r int32) { _ = libc.Int32FromInt32(0) return (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxUnlock})))(tls, id, lockType) } func _sqlite3OsCheckReservedLock(tls *libc.TLS, id uintptr, pResOut uintptr) (r int32) { return (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxCheckReservedLock})))(tls, id, pResOut) } // C documentation // // /* // ** Use sqlite3OsFileControl() when we are doing something that might fail // ** and we need to know about the failures. Use sqlite3OsFileControlHint() // ** when simply tossing information over the wall to the VFS and we do not // ** really care if the VFS receives and understands the information since it // ** is only a hint and can be safely ignored. The sqlite3OsFileControlHint() // ** routine has no return value since the return value would be meaningless. // */ func _sqlite3OsFileControl(tls *libc.TLS, id uintptr, op int32, pArg uintptr) (r int32) { if (*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods == uintptr(0) { return int32(SQLITE_NOTFOUND) } return (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxFileControl})))(tls, id, op, pArg) } func _sqlite3OsFileControlHint(tls *libc.TLS, id uintptr, op int32, pArg uintptr) { if (*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods != 0 { (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxFileControl})))(tls, id, op, pArg) } } func _sqlite3OsSectorSize(tls *libc.TLS, id uintptr) (r int32) { var xSectorSize uintptr var v1 int32 _, _ = xSectorSize, v1 xSectorSize = (*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxSectorSize if xSectorSize != 0 { v1 = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xSectorSize})))(tls, id) } else { v1 = int32(SQLITE_DEFAULT_SECTOR_SIZE) } return v1 } func _sqlite3OsDeviceCharacteristics(tls *libc.TLS, id uintptr) (r int32) { if (*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods == uintptr(0) { return 0 } return (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxDeviceCharacteristics})))(tls, id) } func _sqlite3OsShmLock(tls *libc.TLS, id uintptr, offset int32, n int32, flags int32) (r int32) { return (*(*func(*libc.TLS, uintptr, int32, int32, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxShmLock})))(tls, id, offset, n, flags) } func _sqlite3OsShmBarrier(tls *libc.TLS, id uintptr) { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxShmBarrier})))(tls, id) } func _sqlite3OsShmUnmap(tls *libc.TLS, id uintptr, deleteFlag int32) (r int32) { return (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxShmUnmap})))(tls, id, deleteFlag) } func _sqlite3OsShmMap(tls *libc.TLS, id uintptr, iPage int32, pgsz int32, bExtend int32, pp uintptr) (r int32) { return (*(*func(*libc.TLS, uintptr, int32, int32, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxShmMap})))(tls, id, iPage, pgsz, bExtend, pp) } // C documentation // // /* The real implementation of xFetch and xUnfetch */ func _sqlite3OsFetch(tls *libc.TLS, id uintptr, iOff Ti64, iAmt int32, pp uintptr) (r int32) { return (*(*func(*libc.TLS, uintptr, Tsqlite3_int64, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxFetch})))(tls, id, iOff, iAmt, pp) } func _sqlite3OsUnfetch(tls *libc.TLS, id uintptr, iOff Ti64, p uintptr) (r int32) { return (*(*func(*libc.TLS, uintptr, Tsqlite3_int64, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxUnfetch})))(tls, id, iOff, p) } // C documentation // // /* // ** The next group of routines are convenience wrappers around the // ** VFS methods. // */ func _sqlite3OsOpen(tls *libc.TLS, pVfs uintptr, zPath uintptr, pFile uintptr, flags int32, pFlagsOut uintptr) (r int32) { var rc int32 _ = rc /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example, ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before ** reaching the VFS. */ _ = libc.Int32FromInt32(0) rc = (*(*func(*libc.TLS, uintptr, Tsqlite3_filename, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxOpen})))(tls, pVfs, zPath, pFile, flags&int32(0x1087f7f), pFlagsOut) _ = libc.Int32FromInt32(0) return rc } func _sqlite3OsDelete(tls *libc.TLS, pVfs uintptr, zPath uintptr, dirSync int32) (r int32) { var v1 int32 _ = v1 _ = libc.Int32FromInt32(0) if (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxDelete != uintptr(0) { v1 = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxDelete})))(tls, pVfs, zPath, dirSync) } else { v1 = SQLITE_OK } return v1 } func _sqlite3OsAccess(tls *libc.TLS, pVfs uintptr, zPath uintptr, flags int32, pResOut uintptr) (r int32) { return (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxAccess})))(tls, pVfs, zPath, flags, pResOut) } func _sqlite3OsFullPathname(tls *libc.TLS, pVfs uintptr, zPath uintptr, nPathOut int32, zPathOut uintptr) (r int32) { *(*uint8)(unsafe.Pointer(zPathOut)) = uint8(0) return (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxFullPathname})))(tls, pVfs, zPath, nPathOut, zPathOut) } func _sqlite3OsDlOpen(tls *libc.TLS, pVfs uintptr, zPath uintptr) (r uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* tag-20210611-1 */ return (*(*func(*libc.TLS, uintptr, uintptr) uintptr)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxDlOpen})))(tls, pVfs, zPath) } func _sqlite3OsDlError(tls *libc.TLS, pVfs uintptr, nByte int32, zBufOut uintptr) { (*(*func(*libc.TLS, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxDlError})))(tls, pVfs, nByte, zBufOut) } func _sqlite3OsDlSym(tls *libc.TLS, pVfs uintptr, pHdle uintptr, zSym uintptr) (r uintptr) { return (*(*func(*libc.TLS, uintptr, uintptr, uintptr) uintptr)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxDlSym})))(tls, pVfs, pHdle, zSym) } func _sqlite3OsDlClose(tls *libc.TLS, pVfs uintptr, pHandle uintptr) { (*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxDlClose})))(tls, pVfs, pHandle) } func _sqlite3OsRandomness(tls *libc.TLS, pVfs uintptr, nByte int32, zBufOut uintptr) (r int32) { if _sqlite3Config.FiPrngSeed != 0 { libc.Xmemset(tls, zBufOut, 0, uint32(nByte)) if nByte > libc.Int32FromInt64(4) { nByte = int32(4) } libc.Xmemcpy(tls, zBufOut, uintptr(unsafe.Pointer(&_sqlite3Config))+276, uint32(nByte)) return SQLITE_OK } else { return (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxRandomness})))(tls, pVfs, nByte, zBufOut) } return r } func _sqlite3OsSleep(tls *libc.TLS, pVfs uintptr, nMicro int32) (r int32) { return (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxSleep})))(tls, pVfs, nMicro) } func _sqlite3OsGetLastError(tls *libc.TLS, pVfs uintptr) (r int32) { var v1 int32 _ = v1 if (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxGetLastError != 0 { v1 = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxGetLastError})))(tls, pVfs, 0, uintptr(0)) } else { v1 = 0 } return v1 } func _sqlite3OsCurrentTimeInt64(tls *libc.TLS, pVfs uintptr, pTimeOut uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* r at bp+0 */ float64 _ = rc /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64() ** method to get the current date and time if that method is available ** (if iVersion is 2 or greater and the function pointer is not NULL) and ** will fall back to xCurrentTime() if xCurrentTimeInt64() is ** unavailable. */ if (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FiVersion >= int32(2) && (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxCurrentTimeInt64 != 0 { rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxCurrentTimeInt64})))(tls, pVfs, pTimeOut) } else { rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxCurrentTime})))(tls, pVfs, bp) *(*Tsqlite3_int64)(unsafe.Pointer(pTimeOut)) = int64(*(*float64)(unsafe.Pointer(bp)) * libc.Float64FromFloat64(8.64e+07)) } return rc } func _sqlite3OsOpenMalloc(tls *libc.TLS, pVfs uintptr, zFile uintptr, ppFile uintptr, flags int32, pOutFlags uintptr) (r int32) { var pFile uintptr var rc int32 _, _ = pFile, rc pFile = _sqlite3MallocZero(tls, uint64((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FszOsFile)) if pFile != 0 { rc = _sqlite3OsOpen(tls, pVfs, zFile, pFile, flags, pOutFlags) if rc != SQLITE_OK { Xsqlite3_free(tls, pFile) *(*uintptr)(unsafe.Pointer(ppFile)) = uintptr(0) } else { *(*uintptr)(unsafe.Pointer(ppFile)) = pFile } } else { *(*uintptr)(unsafe.Pointer(ppFile)) = uintptr(0) rc = int32(SQLITE_NOMEM) } _ = libc.Int32FromInt32(0) return rc } func _sqlite3OsCloseFree(tls *libc.TLS, pFile uintptr) { _ = libc.Int32FromInt32(0) _sqlite3OsClose(tls, pFile) Xsqlite3_free(tls, pFile) } // C documentation // // /* // ** This function is a wrapper around the OS specific implementation of // ** sqlite3_os_init(). The purpose of the wrapper is to provide the // ** ability to simulate a malloc failure, so that the handling of an // ** error in sqlite3_os_init() by the upper layers can be tested. // */ func _sqlite3OsInit(tls *libc.TLS) (r int32) { var p uintptr _ = p p = Xsqlite3_malloc(tls, int32(10)) if p == uintptr(0) { return int32(SQLITE_NOMEM) } Xsqlite3_free(tls, p) return Xsqlite3_os_init(tls) } // C documentation // // /* // ** The list of all registered VFS implementations. // */ var _vfsList = uintptr(0) // C documentation // // /* // ** Locate a VFS by name. If no name is given, simply return the // ** first VFS on the list. // */ func Xsqlite3_vfs_find(tls *libc.TLS, zVfs uintptr) (r uintptr) { var mutex, pVfs uintptr var rc int32 _, _, _ = mutex, pVfs, rc pVfs = uintptr(0) rc = Xsqlite3_initialize(tls) if rc != 0 { return uintptr(0) } mutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN)) Xsqlite3_mutex_enter(tls, mutex) pVfs = _vfsList for { if !(pVfs != 0) { break } if zVfs == uintptr(0) { break } if libc.Xstrcmp(tls, zVfs, (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FzName) == 0 { break } goto _1 _1: ; pVfs = (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpNext } Xsqlite3_mutex_leave(tls, mutex) return pVfs } // C documentation // // /* // ** Unlink a VFS from the linked list // */ func _vfsUnlink(tls *libc.TLS, pVfs uintptr) { var p uintptr _ = p _ = libc.Int32FromInt32(0) if pVfs == uintptr(0) { /* No-op */ } else { if _vfsList == pVfs { _vfsList = (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpNext } else { if _vfsList != 0 { p = _vfsList for (*Tsqlite3_vfs)(unsafe.Pointer(p)).FpNext != 0 && (*Tsqlite3_vfs)(unsafe.Pointer(p)).FpNext != pVfs { p = (*Tsqlite3_vfs)(unsafe.Pointer(p)).FpNext } if (*Tsqlite3_vfs)(unsafe.Pointer(p)).FpNext == pVfs { (*Tsqlite3_vfs)(unsafe.Pointer(p)).FpNext = (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpNext } } } } } // C documentation // // /* // ** Register a VFS with the system. It is harmless to register the same // ** VFS multiple times. The new VFS becomes the default if makeDflt is // ** true. // */ func Xsqlite3_vfs_register(tls *libc.TLS, pVfs uintptr, makeDflt int32) (r int32) { var mutex uintptr var rc int32 _, _ = mutex, rc rc = Xsqlite3_initialize(tls) if rc != 0 { return rc } mutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN)) Xsqlite3_mutex_enter(tls, mutex) _vfsUnlink(tls, pVfs) if makeDflt != 0 || _vfsList == uintptr(0) { (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpNext = _vfsList _vfsList = pVfs } else { (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpNext = (*Tsqlite3_vfs)(unsafe.Pointer(_vfsList)).FpNext (*Tsqlite3_vfs)(unsafe.Pointer(_vfsList)).FpNext = pVfs } _ = libc.Int32FromInt32(0) Xsqlite3_mutex_leave(tls, mutex) return SQLITE_OK } // C documentation // // /* // ** Unregister a VFS so that it is no longer accessible. // */ func Xsqlite3_vfs_unregister(tls *libc.TLS, pVfs uintptr) (r int32) { var mutex uintptr var rc int32 _, _ = mutex, rc rc = Xsqlite3_initialize(tls) if rc != 0 { return rc } mutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN)) Xsqlite3_mutex_enter(tls, mutex) _vfsUnlink(tls, pVfs) Xsqlite3_mutex_leave(tls, mutex) return SQLITE_OK } /************** End of os.c **************************************************/ /************** Begin file fault.c *******************************************/ /* ** 2008 Jan 22 ** ** 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 contains code to support the concept of "benign" ** malloc failures (when the xMalloc() or xRealloc() method of the ** sqlite3_mem_methods structure fails to allocate a block of memory ** and returns 0). ** ** Most malloc failures are non-benign. After they occur, SQLite ** abandons the current operation and returns an error code (usually ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily ** fatal. For example, if a malloc fails while resizing a hash table, this ** is completely recoverable simply by not carrying out the resize. The ** hash table will continue to function normally. So a malloc failure ** during a hash table resize is a benign fault. */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** Global variables. // */ type TBenignMallocHooks = struct { FxBenignBegin uintptr FxBenignEnd uintptr } type BenignMallocHooks = TBenignMallocHooks type TBenignMallocHooks1 = struct { FxBenignBegin uintptr FxBenignEnd uintptr } type BenignMallocHooks1 = TBenignMallocHooks1 var _sqlite3Hooks = TBenignMallocHooks1{} /* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks ** structure. If writable static data is unsupported on the target, ** we have to locate the state vector at run-time. In the more common ** case where writable static data is supported, wsdHooks can refer directly ** to the "sqlite3Hooks" state vector declared above. */ // C documentation // // /* // ** Register hooks to call when sqlite3BeginBenignMalloc() and // ** sqlite3EndBenignMalloc() are called, respectively. // */ func _sqlite3BenignMallocHooks(tls *libc.TLS, xBenignBegin uintptr, xBenignEnd uintptr) { _sqlite3Hooks.FxBenignBegin = xBenignBegin _sqlite3Hooks.FxBenignEnd = xBenignEnd } // C documentation // // /* // ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that // ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc() // ** indicates that subsequent malloc failures are non-benign. // */ func _sqlite3BeginBenignMalloc(tls *libc.TLS) { if _sqlite3Hooks.FxBenignBegin != 0 { (*(*func(*libc.TLS))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Hooks.FxBenignBegin})))(tls) } } func _sqlite3EndBenignMalloc(tls *libc.TLS) { if _sqlite3Hooks.FxBenignEnd != 0 { (*(*func(*libc.TLS))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Hooks.FxBenignEnd})))(tls) } } /************** End of fault.c ***********************************************/ /************** Begin file mem0.c ********************************************/ /* ** 2008 October 28 ** ** 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 contains a no-op memory allocation drivers for use when ** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented ** here always fail. SQLite will not operate with these drivers. These ** are merely placeholders. Real drivers must be substituted using ** sqlite3_config() before SQLite will operate. */ /* #include "sqliteInt.h" */ /* ** This version of the memory allocator is the default. It is ** used when no other memory allocator is specified using compile-time ** macros. */ /************** End of mem0.c ************************************************/ /************** Begin file mem1.c ********************************************/ /* ** 2007 August 14 ** ** 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 contains low-level memory allocation drivers for when ** SQLite will use the standard C-library malloc/realloc/free interface ** to obtain the memory it needs. ** ** This file contains implementations of the low-level memory allocation ** routines specified in the sqlite3_mem_methods object. The content of ** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The ** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the ** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The ** default configuration is to use memory allocation routines in this ** file. ** ** C-preprocessor macro summary: ** ** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if ** the malloc_usable_size() interface exists ** on the target platform. Or, this symbol ** can be set manually, if desired. ** If an equivalent interface exists by ** a different name, using a separate -D ** option to rename it. ** ** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone ** memory allocator. Set this symbol to enable ** building on older macs. ** ** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of ** _msize() on windows systems. This might ** be necessary when compiling for Delphi, ** for example. */ /* #include "sqliteInt.h" */ /* ** This version of the memory allocator is the default. It is ** used when no other memory allocator is specified using compile-time ** macros. */ /* ** Use standard C library malloc and free on non-Apple systems. ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined. */ /* ** The malloc.h header file is needed for malloc_usable_size() function ** on some systems (e.g. Linux). */ /* ** Include the malloc.h header file, if necessary. Also set define macro ** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize() ** for MSVC and malloc_usable_size() for most other systems (e.g. Linux). ** The memory size function can always be overridden manually by defining ** the macro SQLITE_MALLOCSIZE to the desired function name. */ // C documentation // // /* // ** Like malloc(), but remember the size of the allocation // ** so that we can find it later using sqlite3MemSize(). // ** // ** For this low-level routine, we are guaranteed that nByte>0 because // ** cases of nByte<=0 will be intercepted and dealt with by higher level // ** routines. // */ func _sqlite3MemMalloc(tls *libc.TLS, nByte int32) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var p uintptr _ = p _ = libc.Int32FromInt32(0) p = libc.Xmalloc(tls, uint32(nByte+int32(8))) if p != 0 { *(*Tsqlite3_int64)(unsafe.Pointer(p)) = int64(nByte) p += 8 } else { Xsqlite3_log(tls, int32(SQLITE_NOMEM), __ccgo_ts+1544, libc.VaList(bp+8, nByte)) } return p } // C documentation // // /* // ** Like free() but works for allocations obtained from sqlite3MemMalloc() // ** or sqlite3MemRealloc(). // ** // ** For this low-level routine, we already know that pPrior!=0 since // ** cases where pPrior==0 will have been intercepted and dealt with // ** by higher-level routines. // */ func _sqlite3MemFree(tls *libc.TLS, pPrior uintptr) { var p uintptr _ = p p = pPrior _ = libc.Int32FromInt32(0) p -= 8 libc.Xfree(tls, p) } // C documentation // // /* // ** Report the allocated size of a prior return from xMalloc() // ** or xRealloc(). // */ func _sqlite3MemSize(tls *libc.TLS, pPrior uintptr) (r int32) { var p uintptr _ = p _ = libc.Int32FromInt32(0) p = pPrior p -= 8 return int32(*(*Tsqlite3_int64)(unsafe.Pointer(p))) } // C documentation // // /* // ** Like realloc(). Resize an allocation previously obtained from // ** sqlite3MemMalloc(). // ** // ** For this low-level interface, we know that pPrior!=0. Cases where // ** pPrior==0 while have been intercepted by higher-level routine and // ** redirected to xMalloc. Similarly, we know that nByte>0 because // ** cases where nByte<=0 will have been intercepted by higher-level // ** routines and redirected to xFree. // */ func _sqlite3MemRealloc(tls *libc.TLS, pPrior uintptr, nByte int32) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var p uintptr _ = p p = pPrior _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* EV: R-46199-30249 */ p -= 8 p = libc.Xrealloc(tls, p, uint32(nByte+libc.Int32FromInt32(8))) if p != 0 { *(*Tsqlite3_int64)(unsafe.Pointer(p)) = int64(nByte) p += 8 } else { Xsqlite3_log(tls, int32(SQLITE_NOMEM), __ccgo_ts+1582, libc.VaList(bp+8, _sqlite3MemSize(tls, pPrior), nByte)) } return p } // C documentation // // /* // ** Round up a request size to the next valid allocation size. // */ func _sqlite3MemRoundup(tls *libc.TLS, n int32) (r int32) { return (n + int32(7)) & ^libc.Int32FromInt32(7) } // C documentation // // /* // ** Initialize this module. // */ func _sqlite3MemInit(tls *libc.TLS, NotUsed uintptr) (r int32) { _ = NotUsed return SQLITE_OK } // C documentation // // /* // ** Deinitialize this module. // */ func _sqlite3MemShutdown(tls *libc.TLS, NotUsed uintptr) { _ = NotUsed return } // C documentation // // /* // ** This routine is the only routine in this file with external linkage. // ** // ** Populate the low-level memory allocation function pointers in // ** sqlite3GlobalConfig.m with pointers to the routines in this file. // */ func _sqlite3MemSetDefault(tls *libc.TLS) { bp := tls.Alloc(16) defer tls.Free(16) Xsqlite3_config(tls, int32(SQLITE_CONFIG_MALLOC), libc.VaList(bp+8, uintptr(unsafe.Pointer(&_defaultMethods)))) } var _defaultMethods = Tsqlite3_mem_methods{} func init() { p := unsafe.Pointer(&_defaultMethods) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_sqlite3MemMalloc) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_sqlite3MemFree) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_sqlite3MemRealloc) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_sqlite3MemSize) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_sqlite3MemRoundup) *(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_sqlite3MemInit) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_sqlite3MemShutdown) } /************** End of mem1.c ************************************************/ /************** Begin file mem2.c ********************************************/ /* ** 2007 August 15 ** ** 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 contains low-level memory allocation drivers for when ** SQLite will use the standard C-library malloc/realloc/free interface ** to obtain the memory it needs while adding lots of additional debugging ** information to each allocation in order to help detect and fix memory ** leaks and memory usage errors. ** ** This file contains implementations of the low-level memory allocation ** routines specified in the sqlite3_mem_methods object. */ /* #include "sqliteInt.h" */ /* ** This version of the memory allocator is used only if the ** SQLITE_MEMDEBUG macro is defined */ /************** End of mem2.c ************************************************/ /************** Begin file mem3.c ********************************************/ /* ** 2007 October 14 ** ** 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 contains the C functions that implement a memory ** allocation subsystem for use by SQLite. ** ** This version of the memory allocation subsystem omits all ** use of malloc(). The SQLite user supplies a block of memory ** before calling sqlite3_initialize() from which allocations ** are made and returned by the xMalloc() and xRealloc() ** implementations. Once sqlite3_initialize() has been called, ** the amount of memory available to SQLite is fixed and cannot ** be changed. ** ** This version of the memory allocation subsystem is included ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined. */ /* #include "sqliteInt.h" */ /* ** This version of the memory allocator is only built into the library ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not ** mean that the library will use a memory-pool by default, just that ** it is available. The mempool allocator is activated by calling ** sqlite3_config(). */ /************** End of mem3.c ************************************************/ /************** Begin file mem5.c ********************************************/ /* ** 2007 October 14 ** ** 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 contains the C functions that implement a memory ** allocation subsystem for use by SQLite. ** ** This version of the memory allocation subsystem omits all ** use of malloc(). The application gives SQLite a block of memory ** before calling sqlite3_initialize() from which allocations ** are made and returned by the xMalloc() and xRealloc() ** implementations. Once sqlite3_initialize() has been called, ** the amount of memory available to SQLite is fixed and cannot ** be changed. ** ** This version of the memory allocation subsystem is included ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined. ** ** This memory allocator uses the following algorithm: ** ** 1. All memory allocation sizes are rounded up to a power of 2. ** ** 2. If two adjacent free blocks are the halves of a larger block, ** then the two blocks are coalesced into the single larger block. ** ** 3. New memory is allocated from the first available free block. ** ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions ** Concerning Dynamic Storage Allocation". Journal of the Association for ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499. ** ** Let n be the size of the largest allocation divided by the minimum ** allocation size (after rounding all sizes up to a power of 2.) Let M ** be the maximum amount of memory ever outstanding at one time. Let ** N be the total amount of memory available for allocation. Robson ** proved that this memory allocator will never breakdown due to ** fragmentation as long as the following constraint holds: ** ** N >= M*(1 + log2(n)/2) - n + 1 ** ** The sqlite3_status() logic tracks the maximum values of n and M so ** that an application can, at any time, verify this constraint. */ /* #include "sqliteInt.h" */ /* ** This version of the memory allocator is used only when ** SQLITE_ENABLE_MEMSYS5 is defined. */ /************** End of mem5.c ************************************************/ /************** Begin file mutex.c *******************************************/ /* ** 2007 August 14 ** ** 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 contains the C functions that implement mutexes. ** ** This file contains code that is common across all mutex implementations. */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** Initialize the mutex system. // */ func _sqlite3MutexInit(tls *libc.TLS) (r int32) { var pFrom, pTo uintptr var rc int32 _, _, _ = pFrom, pTo, rc rc = SQLITE_OK if !(_sqlite3Config.Fmutex.FxMutexAlloc != 0) { pTo = uintptr(unsafe.Pointer(&_sqlite3Config)) + 64 if _sqlite3Config.FbCoreMutex != 0 { pFrom = _sqlite3DefaultMutex(tls) } else { pFrom = _sqlite3NoopMutex(tls) } (*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexInit = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexInit (*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexEnd = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexEnd (*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexFree = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexFree (*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexEnter = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexEnter (*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexTry = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexTry (*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexLeave = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexLeave (*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexHeld = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexHeld (*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexNotheld = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexNotheld (*Tsqlite3_mutex_methods)(unsafe.Pointer(pTo)).FxMutexAlloc = (*Tsqlite3_mutex_methods)(unsafe.Pointer(pFrom)).FxMutexAlloc } _ = libc.Int32FromInt32(0) rc = (*(*func(*libc.TLS) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fmutex.FxMutexInit})))(tls) return rc } // C documentation // // /* // ** Shutdown the mutex system. This call frees resources allocated by // ** sqlite3MutexInit(). // */ func _sqlite3MutexEnd(tls *libc.TLS) (r int32) { var rc int32 _ = rc rc = SQLITE_OK if _sqlite3Config.Fmutex.FxMutexEnd != 0 { rc = (*(*func(*libc.TLS) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fmutex.FxMutexEnd})))(tls) } return rc } // C documentation // // /* // ** Retrieve a pointer to a static mutex or allocate a new dynamic one. // */ func Xsqlite3_mutex_alloc(tls *libc.TLS, id int32) (r uintptr) { if id <= int32(SQLITE_MUTEX_RECURSIVE) && Xsqlite3_initialize(tls) != 0 { return uintptr(0) } if id > int32(SQLITE_MUTEX_RECURSIVE) && _sqlite3MutexInit(tls) != 0 { return uintptr(0) } _ = libc.Int32FromInt32(0) return (*(*func(*libc.TLS, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fmutex.FxMutexAlloc})))(tls, id) } func _sqlite3MutexAlloc(tls *libc.TLS, id int32) (r uintptr) { if !(_sqlite3Config.FbCoreMutex != 0) { return uintptr(0) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return (*(*func(*libc.TLS, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fmutex.FxMutexAlloc})))(tls, id) } // C documentation // // /* // ** Free a dynamic mutex. // */ func Xsqlite3_mutex_free(tls *libc.TLS, p uintptr) { if p != 0 { _ = libc.Int32FromInt32(0) (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fmutex.FxMutexFree})))(tls, p) } } // C documentation // // /* // ** Obtain the mutex p. If some other thread already has the mutex, block // ** until it can be obtained. // */ func Xsqlite3_mutex_enter(tls *libc.TLS, p uintptr) { if p != 0 { _ = libc.Int32FromInt32(0) (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fmutex.FxMutexEnter})))(tls, p) } } // C documentation // // /* // ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another // ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY. // */ func Xsqlite3_mutex_try(tls *libc.TLS, p uintptr) (r int32) { var rc int32 _ = rc rc = SQLITE_OK if p != 0 { _ = libc.Int32FromInt32(0) return (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fmutex.FxMutexTry})))(tls, p) } return rc } // C documentation // // /* // ** The sqlite3_mutex_leave() routine exits a mutex that was previously // ** entered by the same thread. The behavior is undefined if the mutex // ** is not currently entered. If a NULL pointer is passed as an argument // ** this function is a no-op. // */ func Xsqlite3_mutex_leave(tls *libc.TLS, p uintptr) { if p != 0 { _ = libc.Int32FromInt32(0) (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fmutex.FxMutexLeave})))(tls, p) } } /************** End of mutex.c ***********************************************/ /************** Begin file mutex_noop.c **************************************/ /* ** 2008 October 07 ** ** 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 contains the C functions that implement mutexes. ** ** This implementation in this file does not provide any mutual ** exclusion and is thus suitable for use only in applications ** that use SQLite in a single thread. The routines defined ** here are place-holders. Applications can substitute working ** mutex routines at start-time using the ** ** sqlite3_config(SQLITE_CONFIG_MUTEX,...) ** ** interface. ** ** If compiled with SQLITE_DEBUG, then additional logic is inserted ** that does error checking on mutexes to make sure they are being ** called correctly. */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** Stub routines for all mutex methods. // ** // ** This routines provide no mutual exclusion or error checking. // */ func _noopMutexInit(tls *libc.TLS) (r int32) { return SQLITE_OK } func _noopMutexEnd(tls *libc.TLS) (r int32) { return SQLITE_OK } func _noopMutexAlloc(tls *libc.TLS, id int32) (r uintptr) { _ = id return libc.UintptrFromInt32(8) } func _noopMutexFree(tls *libc.TLS, p uintptr) { _ = p return } func _noopMutexEnter(tls *libc.TLS, p uintptr) { _ = p return } func _noopMutexTry(tls *libc.TLS, p uintptr) (r int32) { _ = p return SQLITE_OK } func _noopMutexLeave(tls *libc.TLS, p uintptr) { _ = p return } func _sqlite3NoopMutex(tls *libc.TLS) (r uintptr) { return uintptr(unsafe.Pointer(&_sMutex)) } var _sMutex = Tsqlite3_mutex_methods{} func init() { p := unsafe.Pointer(&_sMutex) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_noopMutexInit) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_noopMutexEnd) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_noopMutexAlloc) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_noopMutexFree) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_noopMutexEnter) *(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_noopMutexTry) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_noopMutexLeave) } // C documentation // // /* // ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation // ** is used regardless of the run-time threadsafety setting. // */ func _sqlite3DefaultMutex(tls *libc.TLS) (r uintptr) { return _sqlite3NoopMutex(tls) } /************** End of mutex_noop.c ******************************************/ /************** Begin file mutex_unix.c **************************************/ /* ** 2007 August 28 ** ** 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 contains the C functions that implement mutexes for pthreads */ /* #include "sqliteInt.h" */ /* ** The code in this file is only used if we are compiling threadsafe ** under unix with pthreads. ** ** Note that this implementation requires a version of pthreads that ** supports recursive mutexes. */ /************** End of mutex_unix.c ******************************************/ /************** Begin file mutex_w32.c ***************************************/ /* ** 2007 August 14 ** ** 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 contains the C functions that implement mutexes for Win32. */ /* #include "sqliteInt.h" */ /* ** The code in this file is only used if we are compiling multithreaded ** on a Win32 system. */ /************** End of mutex_w32.c *******************************************/ /************** Begin file malloc.c ******************************************/ /* ** 2001 September 15 ** ** 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. ** ************************************************************************* ** ** Memory allocation functions used throughout sqlite. */ /* #include "sqliteInt.h" */ /* #include */ // C documentation // // /* // ** Attempt to release up to n bytes of non-essential memory currently // ** held by SQLite. An example of non-essential memory is memory used to // ** cache database pages that are not currently in use. // */ func Xsqlite3_release_memory(tls *libc.TLS, n int32) (r int32) { return _sqlite3PcacheReleaseMemory(tls, n) } /* ** Default value of the hard heap limit. 0 means "no limit". */ // C documentation // // /* // ** State information local to the memory allocation subsystem. // */ type TMem0Global = struct { F__ccgo_align [0]uint32 Fmutex uintptr F__ccgo_align1 [4]byte FalarmThreshold Tsqlite3_int64 FhardLimit Tsqlite3_int64 FnearlyFull int32 F__ccgo_pad4 [4]byte } type Mem0Global = TMem0Global /* ** Default value of the hard heap limit. 0 means "no limit". */ // C documentation // // /* // ** State information local to the memory allocation subsystem. // */ var _mem0 = TMem0Global{} // C documentation // // /* // ** Return the memory allocator mutex. sqlite3_status() needs it. // */ func _sqlite3MallocMutex(tls *libc.TLS) (r uintptr) { return _mem0.Fmutex } // C documentation // // /* // ** Deprecated external interface. It used to set an alarm callback // ** that was invoked when memory usage grew too large. Now it is a // ** no-op. // */ func Xsqlite3_memory_alarm(tls *libc.TLS, xCallback uintptr, pArg uintptr, iThreshold Tsqlite3_int64) (r int32) { _ = xCallback _ = pArg _ = iThreshold return SQLITE_OK } // C documentation // // /* // ** Set the soft heap-size limit for the library. An argument of // ** zero disables the limit. A negative argument is a no-op used to // ** obtain the return value. // ** // ** The return value is the value of the heap limit just before this // ** interface was called. // ** // ** If the hard heap limit is enabled, then the soft heap limit cannot // ** be disabled nor raised above the hard heap limit. // */ func Xsqlite3_soft_heap_limit64(tls *libc.TLS, n Tsqlite3_int64) (r Tsqlite3_int64) { var excess, nUsed, priorLimit Tsqlite3_int64 var rc int32 _, _, _, _ = excess, nUsed, priorLimit, rc rc = Xsqlite3_initialize(tls) if rc != 0 { return int64(-int32(1)) } Xsqlite3_mutex_enter(tls, _mem0.Fmutex) priorLimit = _mem0.FalarmThreshold if n < 0 { Xsqlite3_mutex_leave(tls, _mem0.Fmutex) return priorLimit } if _mem0.FhardLimit > 0 && (n > _mem0.FhardLimit || n == 0) { n = _mem0.FhardLimit } _mem0.FalarmThreshold = n nUsed = _sqlite3StatusValue(tls, SQLITE_STATUS_MEMORY_USED) libc.AtomicStoreNInt32(uintptr(unsafe.Pointer(&_mem0))+24, libc.BoolInt32(n > 0 && n <= nUsed), libc.Int32FromInt32(__ATOMIC_RELAXED)) Xsqlite3_mutex_leave(tls, _mem0.Fmutex) excess = Xsqlite3_memory_used(tls) - n if excess > 0 { Xsqlite3_release_memory(tls, int32(excess&libc.Int64FromInt32(0x7fffffff))) } return priorLimit } func Xsqlite3_soft_heap_limit(tls *libc.TLS, n int32) { if n < 0 { n = 0 } Xsqlite3_soft_heap_limit64(tls, int64(n)) } // C documentation // // /* // ** Set the hard heap-size limit for the library. An argument of zero // ** disables the hard heap limit. A negative argument is a no-op used // ** to obtain the return value without affecting the hard heap limit. // ** // ** The return value is the value of the hard heap limit just prior to // ** calling this interface. // ** // ** Setting the hard heap limit will also activate the soft heap limit // ** and constrain the soft heap limit to be no more than the hard heap // ** limit. // */ func Xsqlite3_hard_heap_limit64(tls *libc.TLS, n Tsqlite3_int64) (r Tsqlite3_int64) { var priorLimit Tsqlite3_int64 var rc int32 _, _ = priorLimit, rc rc = Xsqlite3_initialize(tls) if rc != 0 { return int64(-int32(1)) } Xsqlite3_mutex_enter(tls, _mem0.Fmutex) priorLimit = _mem0.FhardLimit if n >= 0 { _mem0.FhardLimit = n if n < _mem0.FalarmThreshold || _mem0.FalarmThreshold == 0 { _mem0.FalarmThreshold = n } } Xsqlite3_mutex_leave(tls, _mem0.Fmutex) return priorLimit } // C documentation // // /* // ** Initialize the memory allocation subsystem. // */ func _sqlite3MallocInit(tls *libc.TLS) (r int32) { var rc int32 _ = rc if _sqlite3Config.Fm.FxMalloc == uintptr(0) { _sqlite3MemSetDefault(tls) } _mem0.Fmutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MEM)) if _sqlite3Config.FpPage == uintptr(0) || _sqlite3Config.FszPage < int32(512) || _sqlite3Config.FnPage <= 0 { _sqlite3Config.FpPage = uintptr(0) _sqlite3Config.FszPage = 0 } rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxInit})))(tls, _sqlite3Config.Fm.FpAppData) if rc != SQLITE_OK { libc.Xmemset(tls, uintptr(unsafe.Pointer(&_mem0)), 0, uint32(32)) } return rc } // C documentation // // /* // ** Return true if the heap is currently under memory pressure - in other // ** words if the amount of heap used is close to the limit set by // ** sqlite3_soft_heap_limit(). // */ func _sqlite3HeapNearlyFull(tls *libc.TLS) (r int32) { return libc.AtomicLoadNInt32(uintptr(unsafe.Pointer(&_mem0))+24, libc.Int32FromInt32(__ATOMIC_RELAXED)) } // C documentation // // /* // ** Deinitialize the memory allocation subsystem. // */ func _sqlite3MallocEnd(tls *libc.TLS) { if _sqlite3Config.Fm.FxShutdown != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxShutdown})))(tls, _sqlite3Config.Fm.FpAppData) } libc.Xmemset(tls, uintptr(unsafe.Pointer(&_mem0)), 0, uint32(32)) } // C documentation // // /* // ** Return the amount of memory currently checked out. // */ func Xsqlite3_memory_used(tls *libc.TLS) (r Tsqlite3_int64) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* mx at bp+8 */ Tsqlite3_int64 var _ /* res at bp+0 */ Tsqlite3_int64 Xsqlite3_status64(tls, SQLITE_STATUS_MEMORY_USED, bp, bp+8, 0) return *(*Tsqlite3_int64)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Return the maximum amount of memory that has ever been // ** checked out since either the beginning of this process // ** or since the most recent reset. // */ func Xsqlite3_memory_highwater(tls *libc.TLS, resetFlag int32) (r Tsqlite3_int64) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* mx at bp+8 */ Tsqlite3_int64 var _ /* res at bp+0 */ Tsqlite3_int64 Xsqlite3_status64(tls, SQLITE_STATUS_MEMORY_USED, bp, bp+8, resetFlag) return *(*Tsqlite3_int64)(unsafe.Pointer(bp + 8)) } // C documentation // // /* // ** Trigger the alarm // */ func _sqlite3MallocAlarm(tls *libc.TLS, nByte int32) { if _mem0.FalarmThreshold <= 0 { return } Xsqlite3_mutex_leave(tls, _mem0.Fmutex) Xsqlite3_release_memory(tls, nByte) Xsqlite3_mutex_enter(tls, _mem0.Fmutex) } // C documentation // // /* // ** Do a memory allocation with statistics and alarms. Assume the // ** lock is already held. // */ func _mallocWithAlarm(tls *libc.TLS, n int32, pp uintptr) { var nFull int32 var nUsed Tsqlite3_int64 var p uintptr _, _, _ = nFull, nUsed, p _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal ** implementation of malloc_good_size(), which must be called in debug ** mode and specifically when the DMD "Dark Matter Detector" is enabled ** or else a crash results. Hence, do not attempt to optimize out the ** following xRoundup() call. */ nFull = (*(*func(*libc.TLS, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxRoundup})))(tls, n) _sqlite3StatusHighwater(tls, int32(SQLITE_STATUS_MALLOC_SIZE), n) if _mem0.FalarmThreshold > 0 { nUsed = _sqlite3StatusValue(tls, SQLITE_STATUS_MEMORY_USED) if nUsed >= _mem0.FalarmThreshold-int64(nFull) { libc.AtomicStoreNInt32(uintptr(unsafe.Pointer(&_mem0))+24, libc.Int32FromInt32(1), libc.Int32FromInt32(__ATOMIC_RELAXED)) _sqlite3MallocAlarm(tls, nFull) if _mem0.FhardLimit != 0 { nUsed = _sqlite3StatusValue(tls, SQLITE_STATUS_MEMORY_USED) if nUsed >= _mem0.FhardLimit-int64(nFull) { *(*uintptr)(unsafe.Pointer(pp)) = uintptr(0) return } } } else { libc.AtomicStoreNInt32(uintptr(unsafe.Pointer(&_mem0))+24, libc.Int32FromInt32(0), libc.Int32FromInt32(__ATOMIC_RELAXED)) } } p = (*(*func(*libc.TLS, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxMalloc})))(tls, nFull) if p == uintptr(0) && _mem0.FalarmThreshold > 0 { _sqlite3MallocAlarm(tls, nFull) p = (*(*func(*libc.TLS, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxMalloc})))(tls, nFull) } if p != 0 { nFull = _sqlite3MallocSize(tls, p) _sqlite3StatusUp(tls, SQLITE_STATUS_MEMORY_USED, nFull) _sqlite3StatusUp(tls, int32(SQLITE_STATUS_MALLOC_COUNT), int32(1)) } *(*uintptr)(unsafe.Pointer(pp)) = p } /* ** Maximum size of any single memory allocation. ** ** This is not a limit on the total amount of memory used. This is ** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc(). ** ** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391 ** This provides a 256-byte safety margin for defense against 32-bit ** signed integer overflow bugs when computing memory allocation sizes. ** Paranoid applications might want to reduce the maximum allocation size ** further for an even larger safety margin. 0x3fffffff or 0x0fffffff ** or even smaller would be reasonable upper bounds on the size of a memory ** allocations for most applications. */ // C documentation // // /* // ** Allocate memory. This routine is like sqlite3_malloc() except that it // ** assumes the memory subsystem has already been initialized. // */ func _sqlite3Malloc(tls *libc.TLS, n Tu64) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* p at bp+0 */ uintptr if n == uint64(0) || n > uint64(SQLITE_MAX_ALLOCATION_SIZE) { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } else { if _sqlite3Config.FbMemstat != 0 { Xsqlite3_mutex_enter(tls, _mem0.Fmutex) _mallocWithAlarm(tls, int32(n), bp) Xsqlite3_mutex_leave(tls, _mem0.Fmutex) } else { *(*uintptr)(unsafe.Pointer(bp)) = (*(*func(*libc.TLS, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxMalloc})))(tls, int32(n)) } } _ = libc.Int32FromInt32(0) /* IMP: R-11148-40995 */ return *(*uintptr)(unsafe.Pointer(bp)) } // C documentation // // /* // ** This version of the memory allocation is for use by the application. // ** First make sure the memory subsystem is initialized, then do the // ** allocation. // */ func Xsqlite3_malloc(tls *libc.TLS, n int32) (r uintptr) { var v1 uintptr _ = v1 if Xsqlite3_initialize(tls) != 0 { return uintptr(0) } if n <= 0 { v1 = uintptr(0) } else { v1 = _sqlite3Malloc(tls, uint64(n)) } return v1 } func Xsqlite3_malloc64(tls *libc.TLS, n Tsqlite3_uint64) (r uintptr) { if Xsqlite3_initialize(tls) != 0 { return uintptr(0) } return _sqlite3Malloc(tls, n) } // C documentation // // /* // ** TRUE if p is a lookaside memory allocation from db // */ func _isLookaside(tls *libc.TLS, db uintptr, p uintptr) (r int32) { return libc.BoolInt32(uint32(p) >= uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart) && uint32(p) < uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpTrueEnd)) } // C documentation // // /* // ** Return the size of a memory allocation previously obtained from // ** sqlite3Malloc() or sqlite3_malloc(). // */ func _sqlite3MallocSize(tls *libc.TLS, p uintptr) (r int32) { _ = libc.Int32FromInt32(0) return (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxSize})))(tls, p) } func _lookasideMallocSize(tls *libc.TLS, db uintptr, p uintptr) (r int32) { var v1 int32 _ = v1 if p < (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpMiddle { v1 = int32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue) } else { v1 = int32(LOOKASIDE_SMALL) } return v1 } func _sqlite3DbMallocSize(tls *libc.TLS, db uintptr, p uintptr) (r int32) { _ = libc.Int32FromInt32(0) if db != 0 { if uint32(p) < uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpTrueEnd) { if uint32(p) >= uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpMiddle) { _ = libc.Int32FromInt32(0) return int32(LOOKASIDE_SMALL) } if uint32(p) >= uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart) { _ = libc.Int32FromInt32(0) return int32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue) } } } return (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxSize})))(tls, p) } func Xsqlite3_msize(tls *libc.TLS, p uintptr) (r Tsqlite3_uint64) { var v1 int32 _ = v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if p != 0 { v1 = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxSize})))(tls, p) } else { v1 = 0 } return uint64(v1) } // C documentation // // /* // ** Free memory previously obtained from sqlite3Malloc(). // */ func Xsqlite3_free(tls *libc.TLS, p uintptr) { if p == uintptr(0) { return } /* IMP: R-49053-54554 */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if _sqlite3Config.FbMemstat != 0 { Xsqlite3_mutex_enter(tls, _mem0.Fmutex) _sqlite3StatusDown(tls, SQLITE_STATUS_MEMORY_USED, _sqlite3MallocSize(tls, p)) _sqlite3StatusDown(tls, int32(SQLITE_STATUS_MALLOC_COUNT), int32(1)) (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxFree})))(tls, p) Xsqlite3_mutex_leave(tls, _mem0.Fmutex) } else { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxFree})))(tls, p) } } // C documentation // // /* // ** Add the size of memory allocation "p" to the count in // ** *db->pnBytesFreed. // */ func _measureAllocationSize(tls *libc.TLS, db uintptr, p uintptr) { *(*int32)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed)) += _sqlite3DbMallocSize(tls, db, p) } // C documentation // // /* // ** Free memory that might be associated with a particular database // ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op. // ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL. // */ func _sqlite3DbFreeNN(tls *libc.TLS, db uintptr, p uintptr) { var pBuf, pBuf1 uintptr _, _ = pBuf, pBuf1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if db != 0 { if uint32(p) < uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd) { if uint32(p) >= uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpMiddle) { pBuf = p _ = libc.Int32FromInt32(0) (*TLookasideSlot)(unsafe.Pointer(pBuf)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree = pBuf return } if uint32(p) >= uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart) { pBuf1 = p _ = libc.Int32FromInt32(0) (*TLookasideSlot)(unsafe.Pointer(pBuf1)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree = pBuf1 return } } if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed != 0 { _measureAllocationSize(tls, db, p) return } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) Xsqlite3_free(tls, p) } func _sqlite3DbNNFreeNN(tls *libc.TLS, db uintptr, p uintptr) { var pBuf, pBuf1 uintptr _, _ = pBuf, pBuf1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if uint32(p) < uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd) { if uint32(p) >= uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpMiddle) { pBuf = p _ = libc.Int32FromInt32(0) (*TLookasideSlot)(unsafe.Pointer(pBuf)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree = pBuf return } if uint32(p) >= uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart) { pBuf1 = p _ = libc.Int32FromInt32(0) (*TLookasideSlot)(unsafe.Pointer(pBuf1)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree = pBuf1 return } } if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed != 0 { _measureAllocationSize(tls, db, p) return } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) Xsqlite3_free(tls, p) } func _sqlite3DbFree(tls *libc.TLS, db uintptr, p uintptr) { _ = libc.Int32FromInt32(0) if p != 0 { _sqlite3DbFreeNN(tls, db, p) } } // C documentation // // /* // ** Change the size of an existing memory allocation // */ func _sqlite3Realloc(tls *libc.TLS, pOld uintptr, nBytes Tu64) (r uintptr) { var nDiff, nNew, nOld int32 var nUsed, v1 Tsqlite3_int64 var pNew uintptr var v2 bool _, _, _, _, _, _, _ = nDiff, nNew, nOld, nUsed, pNew, v1, v2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pOld == uintptr(0) { return _sqlite3Malloc(tls, nBytes) /* IMP: R-04300-56712 */ } if nBytes == uint64(0) { Xsqlite3_free(tls, pOld) /* IMP: R-26507-47431 */ return uintptr(0) } if nBytes >= uint64(0x7fffff00) { /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ return uintptr(0) } nOld = _sqlite3MallocSize(tls, pOld) /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second ** argument to xRealloc is always a value returned by a prior call to ** xRoundup. */ nNew = (*(*func(*libc.TLS, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxRoundup})))(tls, int32(nBytes)) if nOld == nNew { pNew = pOld } else { if _sqlite3Config.FbMemstat != 0 { Xsqlite3_mutex_enter(tls, _mem0.Fmutex) _sqlite3StatusHighwater(tls, int32(SQLITE_STATUS_MALLOC_SIZE), int32(nBytes)) nDiff = nNew - nOld if v2 = nDiff > 0; v2 { v1 = _sqlite3StatusValue(tls, SQLITE_STATUS_MEMORY_USED) nUsed = v1 } if v2 && v1 >= _mem0.FalarmThreshold-int64(nDiff) { _sqlite3MallocAlarm(tls, nDiff) if _mem0.FhardLimit > 0 && nUsed >= _mem0.FhardLimit-int64(nDiff) { Xsqlite3_mutex_leave(tls, _mem0.Fmutex) return uintptr(0) } } pNew = (*(*func(*libc.TLS, uintptr, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxRealloc})))(tls, pOld, nNew) if pNew == uintptr(0) && _mem0.FalarmThreshold > 0 { _sqlite3MallocAlarm(tls, int32(nBytes)) pNew = (*(*func(*libc.TLS, uintptr, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxRealloc})))(tls, pOld, nNew) } if pNew != 0 { nNew = _sqlite3MallocSize(tls, pNew) _sqlite3StatusUp(tls, SQLITE_STATUS_MEMORY_USED, nNew-nOld) } Xsqlite3_mutex_leave(tls, _mem0.Fmutex) } else { pNew = (*(*func(*libc.TLS, uintptr, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxRealloc})))(tls, pOld, nNew) } } _ = libc.Int32FromInt32(0) /* IMP: R-11148-40995 */ return pNew } // C documentation // // /* // ** The public interface to sqlite3Realloc. Make sure that the memory // ** subsystem is initialized prior to invoking sqliteRealloc. // */ func Xsqlite3_realloc(tls *libc.TLS, pOld uintptr, n int32) (r uintptr) { if Xsqlite3_initialize(tls) != 0 { return uintptr(0) } if n < 0 { n = 0 } /* IMP: R-26507-47431 */ return _sqlite3Realloc(tls, pOld, uint64(n)) } func Xsqlite3_realloc64(tls *libc.TLS, pOld uintptr, n Tsqlite3_uint64) (r uintptr) { if Xsqlite3_initialize(tls) != 0 { return uintptr(0) } return _sqlite3Realloc(tls, pOld, n) } // C documentation // // /* // ** Allocate and zero memory. // */ func _sqlite3MallocZero(tls *libc.TLS, n Tu64) (r uintptr) { var p uintptr _ = p p = _sqlite3Malloc(tls, n) if p != 0 { libc.Xmemset(tls, p, 0, uint32(n)) } return p } // C documentation // // /* // ** Allocate and zero memory. If the allocation fails, make // ** the mallocFailed flag in the connection pointer. // */ func _sqlite3DbMallocZero(tls *libc.TLS, db uintptr, n Tu64) (r uintptr) { var p uintptr _ = p p = _sqlite3DbMallocRaw(tls, db, n) if p != 0 { libc.Xmemset(tls, p, 0, uint32(n)) } return p } // C documentation // // /* Finish the work of sqlite3DbMallocRawNN for the unusual and // ** slower case when the allocation cannot be fulfilled using lookaside. // */ func _dbMallocRawFinish(tls *libc.TLS, db uintptr, n Tu64) (r uintptr) { var p uintptr _ = p _ = libc.Int32FromInt32(0) p = _sqlite3Malloc(tls, n) if !(p != 0) { _sqlite3OomFault(tls, db) } return p } // C documentation // // /* // ** Allocate memory, either lookaside (if possible) or heap. // ** If the allocation fails, set the mallocFailed flag in // ** the connection pointer. // ** // ** If db!=0 and db->mallocFailed is true (indicating a prior malloc // ** failure on the same database connection) then always return 0. // ** Hence for a particular database connection, once malloc starts // ** failing, it fails consistently until mallocFailed is reset. // ** This is an important assumption. There are many places in the // ** code that do things like this: // ** // ** int *a = (int*)sqlite3DbMallocRaw(db, 100); // ** int *b = (int*)sqlite3DbMallocRaw(db, 200); // ** if( b ) a[10] = 9; // ** // ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed // ** that all prior mallocs (ex: "a") worked too. // ** // ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is // ** not a NULL pointer. // */ func _sqlite3DbMallocRaw(tls *libc.TLS, db uintptr, n Tu64) (r uintptr) { var p uintptr _ = p if db != 0 { return _sqlite3DbMallocRawNN(tls, db, n) } p = _sqlite3Malloc(tls, n) return p } func _sqlite3DbMallocRawNN(tls *libc.TLS, db uintptr, n Tu64) (r uintptr) { var pBuf, v1, v2, v3, v4 uintptr _, _, _, _, _ = pBuf, v1, v2, v3, v4 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if n > uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz) { if !((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable != 0) { *(*Tu32)(unsafe.Pointer(db + 320 + 16 + 1*4))++ } else { if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return uintptr(0) } } return _dbMallocRawFinish(tls, db, n) } if n <= uint64(LOOKASIDE_SMALL) { v1 = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree pBuf = v1 if v1 != uintptr(0) { (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree = (*TLookasideSlot)(unsafe.Pointer(pBuf)).FpNext *(*Tu32)(unsafe.Pointer(db + 320 + 16))++ return pBuf } else { v2 = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit pBuf = v2 if v2 != uintptr(0) { (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit = (*TLookasideSlot)(unsafe.Pointer(pBuf)).FpNext *(*Tu32)(unsafe.Pointer(db + 320 + 16))++ return pBuf } } } v3 = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree pBuf = v3 if v3 != uintptr(0) { (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree = (*TLookasideSlot)(unsafe.Pointer(pBuf)).FpNext *(*Tu32)(unsafe.Pointer(db + 320 + 16))++ return pBuf } else { v4 = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpInit pBuf = v4 if v4 != uintptr(0) { (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpInit = (*TLookasideSlot)(unsafe.Pointer(pBuf)).FpNext *(*Tu32)(unsafe.Pointer(db + 320 + 16))++ return pBuf } else { *(*Tu32)(unsafe.Pointer(db + 320 + 16 + 2*4))++ } } return _dbMallocRawFinish(tls, db, n) } // C documentation // // /* // ** Resize the block of memory pointed to by p to n bytes. If the // ** resize fails, set the mallocFailed flag in the connection object. // */ func _sqlite3DbRealloc(tls *libc.TLS, db uintptr, p uintptr, n Tu64) (r uintptr) { _ = libc.Int32FromInt32(0) if p == uintptr(0) { return _sqlite3DbMallocRawNN(tls, db, n) } _ = libc.Int32FromInt32(0) if uint32(p) < uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd) { if uint32(p) >= uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpMiddle) { if n <= uint64(LOOKASIDE_SMALL) { return p } } else { if uint32(p) >= uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart) { if n <= uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue) { return p } } } } return _dbReallocFinish(tls, db, p, n) } func _dbReallocFinish(tls *libc.TLS, db uintptr, p uintptr, n Tu64) (r uintptr) { var pNew uintptr _ = pNew pNew = uintptr(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 { if _isLookaside(tls, db, p) != 0 { pNew = _sqlite3DbMallocRawNN(tls, db, n) if pNew != 0 { libc.Xmemcpy(tls, pNew, p, uint32(_lookasideMallocSize(tls, db, p))) _sqlite3DbFree(tls, db, p) } } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pNew = _sqlite3Realloc(tls, p, n) if !(pNew != 0) { _sqlite3OomFault(tls, db) } } } return pNew } // C documentation // // /* // ** Attempt to reallocate p. If the reallocation fails, then free p // ** and set the mallocFailed flag in the database connection. // */ func _sqlite3DbReallocOrFree(tls *libc.TLS, db uintptr, p uintptr, n Tu64) (r uintptr) { var pNew uintptr _ = pNew pNew = _sqlite3DbRealloc(tls, db, p, n) if !(pNew != 0) { _sqlite3DbFree(tls, db, p) } return pNew } // C documentation // // /* // ** Make a copy of a string in memory obtained from sqliteMalloc(). These // ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This // ** is because when memory debugging is turned on, these two functions are // ** called via macros that record the current file and line number in the // ** ThreadData structure. // */ func _sqlite3DbStrDup(tls *libc.TLS, db uintptr, z uintptr) (r uintptr) { var n Tsize_t var zNew uintptr _, _ = n, zNew if z == uintptr(0) { return uintptr(0) } n = libc.Xstrlen(tls, z) + uint32(1) zNew = _sqlite3DbMallocRaw(tls, db, uint64(n)) if zNew != 0 { libc.Xmemcpy(tls, zNew, z, n) } return zNew } func _sqlite3DbStrNDup(tls *libc.TLS, db uintptr, z uintptr, n Tu64) (r uintptr) { var zNew, v1 uintptr _, _ = zNew, v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if z != 0 { v1 = _sqlite3DbMallocRawNN(tls, db, n+uint64(1)) } else { v1 = uintptr(0) } zNew = v1 if zNew != 0 { libc.Xmemcpy(tls, zNew, z, uint32(n)) *(*uint8)(unsafe.Pointer(zNew + uintptr(n))) = uint8(0) } return zNew } // C documentation // // /* // ** The text between zStart and zEnd represents a phrase within a larger // ** SQL statement. Make a copy of this phrase in space obtained form // ** sqlite3DbMalloc(). Omit leading and trailing whitespace. // */ func _sqlite3DbSpanDup(tls *libc.TLS, db uintptr, zStart uintptr, zEnd uintptr) (r uintptr) { var n int32 _ = n for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zStart))])&int32(0x01) != 0 { zStart++ } n = int32(zEnd) - int32(zStart) for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zStart + uintptr(n-int32(1))))])&int32(0x01) != 0 { n-- } return _sqlite3DbStrNDup(tls, db, zStart, uint64(n)) } // C documentation // // /* // ** Free any prior content in *pz and replace it with a copy of zNew. // */ func _sqlite3SetString(tls *libc.TLS, pz uintptr, db uintptr, zNew uintptr) { var z uintptr _ = z z = _sqlite3DbStrDup(tls, db, zNew) _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(pz))) *(*uintptr)(unsafe.Pointer(pz)) = z } // C documentation // // /* // ** Call this routine to record the fact that an OOM (out-of-memory) error // ** has happened. This routine will set db->mallocFailed, and also // ** temporarily disable the lookaside memory allocator and interrupt // ** any running VDBEs. // ** // ** Always return a NULL pointer so that this routine can be invoked using // ** // ** return sqlite3OomFault(db); // ** // ** and thereby avoid unnecessary stack frame allocations for the overwhelmingly // ** common case where no OOM occurs. // */ func _sqlite3OomFault(tls *libc.TLS, db uintptr) (r uintptr) { var pParse uintptr _ = pParse if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).FbBenignMalloc) == 0 { (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed = uint8(1) if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec > 0 { libc.AtomicStoreNInt32(db+312, libc.Int32FromInt32(1), libc.Int32FromInt32(__ATOMIC_RELAXED)) } (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable++ (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0) if (*Tsqlite3)(unsafe.Pointer(db)).FpParse != 0 { _sqlite3ErrorMsg(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpParse, __ccgo_ts+1618, 0) (*TParse)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpParse)).Frc = int32(SQLITE_NOMEM) pParse = (*TParse)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpParse)).FpOuterParse for { if !(pParse != 0) { break } (*TParse)(unsafe.Pointer(pParse)).FnErr++ (*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM) goto _1 _1: ; pParse = (*TParse)(unsafe.Pointer(pParse)).FpOuterParse } } } return uintptr(0) } // C documentation // // /* // ** This routine reactivates the memory allocator and clears the // ** db->mallocFailed flag as necessary. // ** // ** The memory allocator is not restarted if there are running // ** VDBEs. // */ func _sqlite3OomClear(tls *libc.TLS, db uintptr) { var v1 int32 _ = v1 if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec == 0 { (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed = uint8(0) libc.AtomicStoreNInt32(db+312, libc.Int32FromInt32(0), libc.Int32FromInt32(__ATOMIC_RELAXED)) _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable-- if (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable != 0 { v1 = 0 } else { v1 = int32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue) } (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(v1) } } // C documentation // // /* // ** Take actions at the end of an API call to deal with error codes. // */ func _apiHandleError(tls *libc.TLS, db uintptr, rc int32) (r int32) { if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 || rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<mallocFailed ** is unsafe, as is the call to sqlite3Error(). */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 || rc != 0 { return _apiHandleError(tls, db, rc) } return 0 } /************** End of malloc.c **********************************************/ /************** Begin file printf.c ******************************************/ /* ** The "printf" code that follows dates from the 1980's. It is in ** the public domain. ** ************************************************************************** ** ** This file contains code for a set of "printf"-like routines. These ** routines format strings much like the printf() from the standard C ** library, though the implementation here has enhancements to support ** SQLite. */ /* #include "sqliteInt.h" */ /* ** Conversion types fall into various categories as defined by the ** following enumeration. */ /* The rest are extensions, not normally found in printf() */ // C documentation // // /* // ** An "etByte" is an 8-bit unsigned value. // */ type TetByte = uint8 type etByte = TetByte // C documentation // // /* // ** Each builtin conversion character (ex: the 'd' in "%d") is described // ** by an instance of the following structure // */ type Tet_info = struct { Ffmttype uint8 Fbase TetByte Fflags TetByte Ftype1 TetByte Fcharset TetByte Fprefix TetByte } type et_info = Tet_info /* ** Allowed values for et_info.flags */ // C documentation // // /* // ** The following table is searched linearly, so it is good to put the // ** most frequently used conversion types first. // */ var _aDigits = [33]uint8{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'} var _aPrefix = [7]uint8{'-', 'x', '0', 0, 'X', '0'} var _fmtinfo = [23]Tet_info{ 0: { Ffmttype: uint8('d'), Fbase: uint8(10), Fflags: uint8(1), Ftype1: uint8(etDECIMAL), }, 1: { Ffmttype: uint8('s'), Fflags: uint8(4), Ftype1: uint8(etSTRING), }, 2: { Ffmttype: uint8('g'), Fflags: uint8(1), Ftype1: uint8(etGENERIC), Fcharset: uint8(30), }, 3: { Ffmttype: uint8('z'), Fflags: uint8(4), Ftype1: uint8(etDYNSTRING), }, 4: { Ffmttype: uint8('q'), Fflags: uint8(4), Ftype1: uint8(etSQLESCAPE), }, 5: { Ffmttype: uint8('Q'), Fflags: uint8(4), Ftype1: uint8(etSQLESCAPE2), }, 6: { Ffmttype: uint8('w'), Fflags: uint8(4), Ftype1: uint8(etSQLESCAPE3), }, 7: { Ffmttype: uint8('c'), Ftype1: uint8(etCHARX), }, 8: { Ffmttype: uint8('o'), Fbase: uint8(8), Fprefix: uint8(2), }, 9: { Ffmttype: uint8('u'), Fbase: uint8(10), Ftype1: uint8(etDECIMAL), }, 10: { Ffmttype: uint8('x'), Fbase: uint8(16), Fcharset: uint8(16), Fprefix: uint8(1), }, 11: { Ffmttype: uint8('X'), Fbase: uint8(16), Fprefix: uint8(4), }, 12: { Ffmttype: uint8('f'), Fflags: uint8(1), Ftype1: uint8(etFLOAT), }, 13: { Ffmttype: uint8('e'), Fflags: uint8(1), Ftype1: uint8(etEXP), Fcharset: uint8(30), }, 14: { Ffmttype: uint8('E'), Fflags: uint8(1), Ftype1: uint8(etEXP), Fcharset: uint8(14), }, 15: { Ffmttype: uint8('G'), Fflags: uint8(1), Ftype1: uint8(etGENERIC), Fcharset: uint8(14), }, 16: { Ffmttype: uint8('i'), Fbase: uint8(10), Fflags: uint8(1), Ftype1: uint8(etDECIMAL), }, 17: { Ffmttype: uint8('n'), Ftype1: uint8(etSIZE), }, 18: { Ffmttype: uint8('%'), Ftype1: uint8(etPERCENT), }, 19: { Ffmttype: uint8('p'), Fbase: uint8(16), Ftype1: uint8(etPOINTER), Fprefix: uint8(1), }, 20: { Ffmttype: uint8('T'), Ftype1: uint8(etTOKEN), }, 21: { Ffmttype: uint8('S'), Ftype1: uint8(etSRCITEM), }, 22: { Ffmttype: uint8('r'), Fbase: uint8(10), Fflags: uint8(1), Ftype1: uint8(etORDINAL), }, } /* Notes: ** ** %S Takes a pointer to SrcItem. Shows name or database.name ** %!S Like %S but prefer the zName over the zAlias */ // C documentation // // /* // ** Set the StrAccum object to an error mode. // */ func _sqlite3StrAccumSetError(tls *libc.TLS, p uintptr, eError Tu8) { _ = libc.Int32FromInt32(0) (*TStrAccum)(unsafe.Pointer(p)).FaccError = eError if (*TStrAccum)(unsafe.Pointer(p)).FmxAlloc != 0 { Xsqlite3_str_reset(tls, p) } if int32(eError) == int32(SQLITE_TOOBIG) { _sqlite3ErrorToParser(tls, (*TStrAccum)(unsafe.Pointer(p)).Fdb, int32(eError)) } } // C documentation // // /* // ** Extra argument values from a PrintfArguments object // */ func _getIntArg(tls *libc.TLS, p uintptr) (r Tsqlite3_int64) { var v1 int32 var v2 uintptr _, _ = v1, v2 if (*TPrintfArguments)(unsafe.Pointer(p)).FnArg <= (*TPrintfArguments)(unsafe.Pointer(p)).FnUsed { return 0 } v2 = p + 4 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ return Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer((*TPrintfArguments)(unsafe.Pointer(p)).FapArg + uintptr(v1)*4))) } func _getDoubleArg(tls *libc.TLS, p uintptr) (r float64) { var v1 int32 var v2 uintptr _, _ = v1, v2 if (*TPrintfArguments)(unsafe.Pointer(p)).FnArg <= (*TPrintfArguments)(unsafe.Pointer(p)).FnUsed { return float64(0) } v2 = p + 4 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ return Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer((*TPrintfArguments)(unsafe.Pointer(p)).FapArg + uintptr(v1)*4))) } func _getTextArg(tls *libc.TLS, p uintptr) (r uintptr) { var v1 int32 var v2 uintptr _, _ = v1, v2 if (*TPrintfArguments)(unsafe.Pointer(p)).FnArg <= (*TPrintfArguments)(unsafe.Pointer(p)).FnUsed { return uintptr(0) } v2 = p + 4 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ return Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer((*TPrintfArguments)(unsafe.Pointer(p)).FapArg + uintptr(v1)*4))) } // C documentation // // /* // ** Allocate memory for a temporary buffer needed for printf rendering. // ** // ** If the requested size of the temp buffer is larger than the size // ** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error. // ** Do the size check before the memory allocation to prevent rogue // ** SQL from requesting large allocations using the precision or width // ** field of the printf() function. // */ func _printfTempBuf(tls *libc.TLS, pAccum uintptr, n Tsqlite3_int64) (r uintptr) { var z uintptr _ = z if (*Tsqlite3_str)(unsafe.Pointer(pAccum)).FaccError != 0 { return uintptr(0) } if n > int64((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnAlloc) && n > int64((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FmxAlloc) { _sqlite3StrAccumSetError(tls, pAccum, uint8(SQLITE_TOOBIG)) return uintptr(0) } z = _sqlite3DbMallocRaw(tls, (*Tsqlite3_str)(unsafe.Pointer(pAccum)).Fdb, uint64(n)) if z == uintptr(0) { _sqlite3StrAccumSetError(tls, pAccum, uint8(SQLITE_NOMEM)) } return z } /* ** On machines with a small stack size, you can redefine the ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. */ /* ** Hard limit on the precision of floating-point conversions. */ // C documentation // // /* // ** Render a string given by "fmt" into the StrAccum object. // */ func Xsqlite3_str_vappendf(tls *libc.TLS, pAccum uintptr, fmt uintptr, ap Tva_list) { bp := tls.Alloc(128) defer tls.Free(128) var bArgList, base Tu8 var bufpt, cset, escarg, infop, pArgList, pExpr, pItem, pSel, pToken, pre, z, zExtra, zOut, v103, v11, v14, v15, v17, v20, v21, v23, v24, v4, v45, v46, v47, v48, v49, v52, v55, v57, v59, v61, v62, v64, v67, v68, v70, v72, v75, v76, v77, v78, v79, v80, v81, v82, v83, v89, v91, v94, v98, p92 uintptr var c, e2, exp, i, iRound, idx, ii, isnull, ix, j, length, nOut, nPad, needQuote, nn, precision, width, x, v10, v110, v111, v16, v18, v19, v2, v22, v3, v51, v56, v58, v60, v65, v66, v71, v73, v74, v85, v86, v87, v88, v90, v93, v96, v97 int32 var cThousand, done, flag_alternateform, flag_altform2, flag_dp, flag_leftjustify, flag_long, flag_prefix, flag_rtz, flag_zeropad, xtype, v5, v6, v7, v8, v9 TetByte var ch, px, wx uint32 var ch1, prefix, q, x1, v101, v107, v54 uint8 var i1, j1, k, n1, nCopyBytes, nPrior, szBufNeeded, v, v100, v104, v106, v108, v109 Ti64 var longvalue Tsqlite_uint64 var n Tu64 var realvalue float64 var v102, v12 bool var _ /* buf at bp+0 */ [70]uint8 var _ /* s at bp+72 */ TFpDecode _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bArgList, base, bufpt, c, cThousand, ch, ch1, cset, done, e2, escarg, exp, flag_alternateform, flag_altform2, flag_dp, flag_leftjustify, flag_long, flag_prefix, flag_rtz, flag_zeropad, i, i1, iRound, idx, ii, infop, isnull, ix, j, j1, k, length, longvalue, n, n1, nCopyBytes, nOut, nPad, nPrior, needQuote, nn, pArgList, pExpr, pItem, pSel, pToken, pre, precision, prefix, px, q, realvalue, szBufNeeded, v, width, wx, x, x1, xtype, z, zExtra, zOut, v10, v100, v101, v102, v103, v104, v106, v107, v108, v109, v11, v110, v111, v12, v14, v15, v16, v17, v18, v19, v2, v20, v21, v22, v23, v24, v3, v4, v45, v46, v47, v48, v49, v5, v51, v52, v54, v55, v56, v57, v58, v59, v6, v60, v61, v62, v64, v65, v66, v67, v68, v7, v70, v71, v72, v73, v74, v75, v76, v77, v78, v79, v8, v80, v81, v82, v83, v85, v86, v87, v88, v89, v9, v90, v91, v93, v94, v96, v97, v98, p92 /* Thousands separator for %d and %u */ xtype = uint8(etINVALID) /* Size of the rendering buffer */ zExtra = uintptr(0) /* True if trailing zeros should be removed */ pArgList = uintptr(0) /* Conversion buffer */ /* pAccum never starts out with an empty buffer that was obtained from ** malloc(). This precondition is required by the mprintf("%z...") ** optimization. */ _ = libc.Int32FromInt32(0) bufpt = uintptr(0) if int32((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FprintfFlags)&int32(SQLITE_PRINTF_SQLFUNC) != 0 { pArgList = libc.VaUintptr(&ap) bArgList = uint8(1) } else { bArgList = uint8(0) } for { v2 = int32(*(*uint8)(unsafe.Pointer(fmt))) c = v2 if !(v2 != 0) { break } if c != int32('%') { bufpt = fmt for cond := true; cond; cond = *(*uint8)(unsafe.Pointer(fmt)) != 0 && int32(*(*uint8)(unsafe.Pointer(fmt))) != int32('%') { fmt++ } Xsqlite3_str_append(tls, pAccum, bufpt, int32(fmt)-int32(bufpt)) if int32(*(*uint8)(unsafe.Pointer(fmt))) == 0 { break } } fmt++ v4 = fmt v3 = int32(*(*uint8)(unsafe.Pointer(v4))) c = v3 if v3 == 0 { Xsqlite3_str_append(tls, pAccum, __ccgo_ts+1632, int32(1)) break } /* Find out what flags are present */ v9 = libc.Uint8FromInt32(0) flag_zeropad = v9 v8 = v9 flag_altform2 = v8 v7 = v8 flag_alternateform = v7 v6 = v7 cThousand = v6 v5 = v6 flag_prefix = v5 flag_leftjustify = v5 done = uint8(0) width = 0 flag_long = uint8(0) precision = -int32(1) for { switch c { case int32('-'): flag_leftjustify = uint8(1) case int32('+'): flag_prefix = uint8('+') case int32(' '): flag_prefix = uint8(' ') case int32('#'): flag_alternateform = uint8(1) case int32('!'): flag_altform2 = uint8(1) case int32('0'): flag_zeropad = uint8(1) case int32(','): cThousand = uint8(',') default: done = uint8(1) case int32('l'): flag_long = uint8(1) fmt++ v14 = fmt c = int32(*(*uint8)(unsafe.Pointer(v14))) if c == int32('l') { fmt++ v15 = fmt c = int32(*(*uint8)(unsafe.Pointer(v15))) flag_long = uint8(2) } done = uint8(1) case int32('1'): fallthrough case int32('2'): fallthrough case int32('3'): fallthrough case int32('4'): fallthrough case int32('5'): fallthrough case int32('6'): fallthrough case int32('7'): fallthrough case int32('8'): fallthrough case int32('9'): wx = uint32(c - int32('0')) for { fmt++ v17 = fmt v16 = int32(*(*uint8)(unsafe.Pointer(v17))) c = v16 if !(v16 >= int32('0') && c <= int32('9')) { break } wx = wx*uint32(10) + uint32(c) - uint32('0') } width = int32(wx & uint32(0x7fffffff)) if c != int32('.') && c != int32('l') { done = uint8(1) } else { fmt-- } case int32('*'): if bArgList != 0 { width = int32(_getIntArg(tls, pArgList)) } else { width = libc.VaInt32(&ap) } if width < 0 { flag_leftjustify = uint8(1) if width >= -int32(2147483647) { v18 = -width } else { v18 = 0 } width = v18 } v19 = int32(*(*uint8)(unsafe.Pointer(fmt + 1))) c = v19 if v19 != int32('.') && c != int32('l') { fmt++ v20 = fmt c = int32(*(*uint8)(unsafe.Pointer(v20))) done = uint8(1) } case int32('.'): fmt++ v21 = fmt c = int32(*(*uint8)(unsafe.Pointer(v21))) if c == int32('*') { if bArgList != 0 { precision = int32(_getIntArg(tls, pArgList)) } else { precision = libc.VaInt32(&ap) } if precision < 0 { if precision >= -int32(2147483647) { v22 = -precision } else { v22 = -int32(1) } precision = v22 } fmt++ v23 = fmt c = int32(*(*uint8)(unsafe.Pointer(v23))) } else { px = uint32(0) for c >= int32('0') && c <= int32('9') { px = px*uint32(10) + uint32(c) - uint32('0') fmt++ v24 = fmt c = int32(*(*uint8)(unsafe.Pointer(v24))) } precision = int32(px & uint32(0x7fffffff)) } if c == int32('l') { fmt-- } else { done = uint8(1) } break } goto _13 _13: ; if v12 = !(done != 0); v12 { fmt++ v11 = fmt v10 = int32(*(*uint8)(unsafe.Pointer(v11))) c = v10 } if !(v12 && v10 != 0) { break } } /* Fetch the info entry for the field */ infop = uintptr(unsafe.Pointer(&_fmtinfo)) xtype = uint8(etINVALID) idx = 0 for { if !(idx < int32(libc.Uint32FromInt64(138)/libc.Uint32FromInt64(6))) { break } if c == int32(_fmtinfo[idx].Ffmttype) { infop = uintptr(unsafe.Pointer(&_fmtinfo)) + uintptr(idx)*6 xtype = (*Tet_info)(unsafe.Pointer(infop)).Ftype1 break } goto _25 _25: ; idx++ } /* ** At this point, variables are initialized as follows: ** ** flag_alternateform TRUE if a '#' is present. ** flag_altform2 TRUE if a '!' is present. ** flag_prefix '+' or ' ' or zero ** flag_leftjustify TRUE if a '-' is present or if the ** field width was negative. ** flag_zeropad TRUE if the width began with 0. ** flag_long 1 for "l", 2 for "ll" ** width The specified field width. This is ** always non-negative. Zero is the default. ** precision The specified precision. The default ** is -1. ** xtype The class of the conversion. ** infop Pointer to the appropriate info struct. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) switch int32(xtype) { case int32(etPOINTER): goto _26 case etRADIX: goto _27 case int32(etORDINAL): goto _28 case int32(etDECIMAL): goto _29 case int32(etGENERIC): goto _30 case int32(etEXP): goto _31 case int32(etFLOAT): goto _32 case int32(etSIZE): goto _33 case int32(etPERCENT): goto _34 case int32(etCHARX): goto _35 case int32(etDYNSTRING): goto _36 case int32(etSTRING): goto _37 case int32(etSQLESCAPE3): goto _38 case int32(etSQLESCAPE2): goto _39 case int32(etSQLESCAPE): goto _40 case int32(etTOKEN): goto _41 case int32(etSRCITEM): goto _42 default: goto _43 } goto _44 _26: ; flag_long = uint8(1) _28: ; _27: ; cThousand = uint8(0) _29: ; if int32((*Tet_info)(unsafe.Pointer(infop)).Fflags)&int32(FLAG_SIGNED) != 0 { if bArgList != 0 { v = _getIntArg(tls, pArgList) } else { if flag_long != 0 { if int32(flag_long) == int32(2) { v = libc.VaInt64(&ap) } else { v = int64(libc.VaInt32(&ap)) } } else { v = int64(libc.VaInt32(&ap)) } } if v < 0 { longvalue = uint64(^v) longvalue++ prefix = uint8('-') } else { longvalue = uint64(v) prefix = flag_prefix } } else { if bArgList != 0 { longvalue = uint64(_getIntArg(tls, pArgList)) } else { if flag_long != 0 { if int32(flag_long) == int32(2) { longvalue = libc.VaUint64(&ap) } else { longvalue = uint64(libc.VaUint32(&ap)) } } else { longvalue = uint64(libc.VaUint32(&ap)) } } prefix = uint8(0) } if longvalue == uint64(0) { flag_alternateform = uint8(0) } if flag_zeropad != 0 && precision < width-libc.BoolInt32(int32(prefix) != 0) { precision = width - libc.BoolInt32(int32(prefix) != 0) } if precision < libc.Int32FromInt32(SQLITE_PRINT_BUF_SIZE)-libc.Int32FromInt32(10)-libc.Int32FromInt32(SQLITE_PRINT_BUF_SIZE)/libc.Int32FromInt32(3) { nOut = int32(SQLITE_PRINT_BUF_SIZE) zOut = bp } else { n = uint64(precision) + uint64(10) if cThousand != 0 { n += uint64(precision / int32(3)) } v45 = _printfTempBuf(tls, pAccum, int64(n)) zExtra = v45 zOut = v45 if zOut == uintptr(0) { return } nOut = int32(n) } bufpt = zOut + uintptr(nOut-int32(1)) if int32(xtype) == int32(etORDINAL) { x = int32(longvalue % libc.Uint64FromInt32(10)) if x >= int32(4) || longvalue/uint64(10)%uint64(10) == uint64(1) { x = 0 } bufpt-- v46 = bufpt *(*uint8)(unsafe.Pointer(v46)) = _zOrd[x*int32(2)+int32(1)] bufpt-- v47 = bufpt *(*uint8)(unsafe.Pointer(v47)) = _zOrd[x*int32(2)] } cset = uintptr(unsafe.Pointer(&_aDigits)) + uintptr((*Tet_info)(unsafe.Pointer(infop)).Fcharset) base = (*Tet_info)(unsafe.Pointer(infop)).Fbase for cond := true; cond; cond = longvalue > uint64(0) { /* Convert to ascii */ bufpt-- v48 = bufpt *(*uint8)(unsafe.Pointer(v48)) = *(*uint8)(unsafe.Pointer(cset + uintptr(longvalue%uint64(base)))) longvalue = longvalue / uint64(base) } length = t__predefined_ptrdiff_t(zOut+uintptr(nOut-int32(1))) - int32(bufpt) for precision > length { bufpt-- v49 = bufpt *(*uint8)(unsafe.Pointer(v49)) = uint8('0') /* Zero pad */ length++ } if cThousand != 0 { nn = (length - int32(1)) / int32(3) /* Number of "," to insert */ ix = (length-int32(1))%int32(3) + int32(1) bufpt -= uintptr(nn) idx = 0 for { if !(nn > 0) { break } *(*uint8)(unsafe.Pointer(bufpt + uintptr(idx))) = *(*uint8)(unsafe.Pointer(bufpt + uintptr(idx+nn))) ix-- if ix == 0 { idx++ v51 = idx *(*uint8)(unsafe.Pointer(bufpt + uintptr(v51))) = cThousand nn-- ix = int32(3) } goto _50 _50: ; idx++ } } if prefix != 0 { bufpt-- v52 = bufpt *(*uint8)(unsafe.Pointer(v52)) = prefix } /* Add sign */ if flag_alternateform != 0 && (*Tet_info)(unsafe.Pointer(infop)).Fprefix != 0 { pre = uintptr(unsafe.Pointer(&_aPrefix)) + uintptr((*Tet_info)(unsafe.Pointer(infop)).Fprefix) for { v54 = *(*uint8)(unsafe.Pointer(pre)) x1 = v54 if !(int32(v54) != 0) { break } bufpt-- v55 = bufpt *(*uint8)(unsafe.Pointer(v55)) = x1 goto _53 _53: ; pre++ } } length = t__predefined_ptrdiff_t(zOut+uintptr(nOut-int32(1))) - int32(bufpt) goto _44 _32: ; _31: ; _30: ; if bArgList != 0 { realvalue = _getDoubleArg(tls, pArgList) } else { realvalue = libc.VaFloat64(&ap) } if precision < 0 { precision = int32(6) } /* Set default precision */ if precision > int32(SQLITE_FP_PRECISION_LIMIT) { precision = int32(SQLITE_FP_PRECISION_LIMIT) } if int32(xtype) == int32(etFLOAT) { iRound = -precision } else { if int32(xtype) == int32(etGENERIC) { if precision == 0 { precision = int32(1) } iRound = precision } else { iRound = precision + int32(1) } } if flag_altform2 != 0 { v56 = int32(26) } else { v56 = int32(16) } _sqlite3FpDecode(tls, bp+72, realvalue, iRound, v56) if (*(*TFpDecode)(unsafe.Pointer(bp + 72))).FisSpecial != 0 { if int32((*(*TFpDecode)(unsafe.Pointer(bp + 72))).FisSpecial) == int32(2) { if flag_zeropad != 0 { v57 = __ccgo_ts + 1634 } else { v57 = __ccgo_ts + 1639 } bufpt = v57 length = _sqlite3Strlen30(tls, bufpt) goto _44 } else { if flag_zeropad != 0 { *(*uint8)(unsafe.Pointer((*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fz)) = uint8('9') (*(*TFpDecode)(unsafe.Pointer(bp + 72))).FiDP = int32(1000) (*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fn = int32(1) } else { libc.Xmemcpy(tls, bp, __ccgo_ts+1643, uint32(5)) bufpt = bp if int32((*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fsign) == int32('-') { /* no-op */ } else { if flag_prefix != 0 { (*(*[70]uint8)(unsafe.Pointer(bp)))[0] = flag_prefix } else { bufpt++ } } length = _sqlite3Strlen30(tls, bufpt) goto _44 } } } if int32((*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fsign) == int32('-') { prefix = uint8('-') } else { prefix = flag_prefix } exp = (*(*TFpDecode)(unsafe.Pointer(bp + 72))).FiDP - int32(1) if int32(xtype) == int32(etGENERIC) && precision > 0 { precision-- } /* ** If the field type is etGENERIC, then convert to either etEXP ** or etFLOAT, as appropriate. */ if int32(xtype) == int32(etGENERIC) { flag_rtz = libc.BoolUint8(!(flag_alternateform != 0)) if exp < -int32(4) || exp > precision { xtype = uint8(etEXP) } else { precision = precision - exp xtype = uint8(etFLOAT) } } else { flag_rtz = flag_altform2 } if int32(xtype) == int32(etEXP) { e2 = 0 } else { e2 = (*(*TFpDecode)(unsafe.Pointer(bp + 72))).FiDP - int32(1) } bufpt = bp /* Size of a temporary buffer needed */ if e2 > 0 { v58 = e2 } else { v58 = 0 } szBufNeeded = int64(v58) + int64(precision) + int64(width) + int64(15) if cThousand != 0 && e2 > 0 { szBufNeeded += int64((e2 + int32(2)) / int32(3)) } if szBufNeeded > int64(SQLITE_PRINT_BUF_SIZE) { v59 = _printfTempBuf(tls, pAccum, szBufNeeded) zExtra = v59 bufpt = v59 if bufpt == uintptr(0) { return } } zOut = bufpt if precision > 0 { v60 = int32(1) } else { v60 = 0 } flag_dp = uint8(v60 | int32(flag_alternateform) | int32(flag_altform2)) /* The sign in front of the number */ if prefix != 0 { v61 = bufpt bufpt++ *(*uint8)(unsafe.Pointer(v61)) = prefix } /* Digits prior to the decimal point */ j = 0 if e2 < 0 { v62 = bufpt bufpt++ *(*uint8)(unsafe.Pointer(v62)) = uint8('0') } else { for { if !(e2 >= 0) { break } v64 = bufpt bufpt++ if j < (*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fn { v66 = j j++ v65 = int32(*(*uint8)(unsafe.Pointer((*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fz + uintptr(v66)))) } else { v65 = int32('0') } *(*uint8)(unsafe.Pointer(v64)) = uint8(v65) if cThousand != 0 && e2%int32(3) == 0 && e2 > int32(1) { v67 = bufpt bufpt++ *(*uint8)(unsafe.Pointer(v67)) = uint8(',') } goto _63 _63: ; e2-- } } /* The decimal point */ if flag_dp != 0 { v68 = bufpt bufpt++ *(*uint8)(unsafe.Pointer(v68)) = uint8('.') } /* "0" digits after the decimal point but before the first ** significant digit of the number */ e2++ for { if !(e2 < 0 && precision > 0) { break } v70 = bufpt bufpt++ *(*uint8)(unsafe.Pointer(v70)) = uint8('0') goto _69 _69: ; precision-- e2++ } /* Significant digits after the decimal point */ for { v71 = precision precision-- if !(v71 > 0) { break } v72 = bufpt bufpt++ if j < (*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fn { v74 = j j++ v73 = int32(*(*uint8)(unsafe.Pointer((*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fz + uintptr(v74)))) } else { v73 = int32('0') } *(*uint8)(unsafe.Pointer(v72)) = uint8(v73) } /* Remove trailing zeros and the "." if no digits follow the "." */ if flag_rtz != 0 && flag_dp != 0 { for int32(*(*uint8)(unsafe.Pointer(bufpt + uintptr(-libc.Int32FromInt32(1))))) == int32('0') { bufpt-- v75 = bufpt *(*uint8)(unsafe.Pointer(v75)) = uint8(0) } _ = libc.Int32FromInt32(0) if int32(*(*uint8)(unsafe.Pointer(bufpt + uintptr(-libc.Int32FromInt32(1))))) == int32('.') { if flag_altform2 != 0 { v76 = bufpt bufpt++ *(*uint8)(unsafe.Pointer(v76)) = uint8('0') } else { bufpt-- v77 = bufpt *(*uint8)(unsafe.Pointer(v77)) = uint8(0) } } } /* Add the "eNNN" suffix */ if int32(xtype) == int32(etEXP) { exp = (*(*TFpDecode)(unsafe.Pointer(bp + 72))).FiDP - int32(1) v78 = bufpt bufpt++ *(*uint8)(unsafe.Pointer(v78)) = _aDigits[(*Tet_info)(unsafe.Pointer(infop)).Fcharset] if exp < 0 { v79 = bufpt bufpt++ *(*uint8)(unsafe.Pointer(v79)) = uint8('-') exp = -exp } else { v80 = bufpt bufpt++ *(*uint8)(unsafe.Pointer(v80)) = uint8('+') } if exp >= int32(100) { v81 = bufpt bufpt++ *(*uint8)(unsafe.Pointer(v81)) = uint8(exp/libc.Int32FromInt32(100) + libc.Int32FromUint8('0')) /* 100's digit */ exp %= int32(100) } v82 = bufpt bufpt++ *(*uint8)(unsafe.Pointer(v82)) = uint8(exp/libc.Int32FromInt32(10) + libc.Int32FromUint8('0')) /* 10's digit */ v83 = bufpt bufpt++ *(*uint8)(unsafe.Pointer(v83)) = uint8(exp%libc.Int32FromInt32(10) + libc.Int32FromUint8('0')) /* 1's digit */ } *(*uint8)(unsafe.Pointer(bufpt)) = uint8(0) /* The converted number is in buf[] and zero terminated. Output it. ** Note that the number is in the usual order, not reversed as with ** integer conversions. */ length = int32(bufpt) - int32(zOut) bufpt = zOut /* Special case: Add leading zeros if the flag_zeropad flag is ** set and we are not left justified */ if flag_zeropad != 0 && !(flag_leftjustify != 0) && length < width { nPad = width - length i = width for { if !(i >= nPad) { break } *(*uint8)(unsafe.Pointer(bufpt + uintptr(i))) = *(*uint8)(unsafe.Pointer(bufpt + uintptr(i-nPad))) goto _84 _84: ; i-- } i = libc.BoolInt32(int32(prefix) != 0) for { v85 = nPad nPad-- if !(v85 != 0) { break } v86 = i i++ *(*uint8)(unsafe.Pointer(bufpt + uintptr(v86))) = uint8('0') } length = width } goto _44 _33: ; if !(bArgList != 0) { *(*int32)(unsafe.Pointer(libc.VaUintptr(&ap))) = int32((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnChar) } v87 = libc.Int32FromInt32(0) width = v87 length = v87 goto _44 _34: ; (*(*[70]uint8)(unsafe.Pointer(bp)))[0] = uint8('%') bufpt = bp length = int32(1) goto _44 _35: ; if bArgList != 0 { bufpt = _getTextArg(tls, pArgList) length = int32(1) if bufpt != 0 { v89 = bufpt bufpt++ v88 = int32(*(*uint8)(unsafe.Pointer(v89))) c = v88 (*(*[70]uint8)(unsafe.Pointer(bp)))[0] = uint8(v88) if c&int32(0xc0) == int32(0xc0) { for length < int32(4) && int32(*(*uint8)(unsafe.Pointer(bufpt)))&int32(0xc0) == int32(0x80) { v90 = length length++ v91 = bufpt bufpt++ (*(*[70]uint8)(unsafe.Pointer(bp)))[v90] = *(*uint8)(unsafe.Pointer(v91)) } } } else { (*(*[70]uint8)(unsafe.Pointer(bp)))[0] = uint8(0) } } else { ch = libc.VaUint32(&ap) if ch < uint32(0x00080) { (*(*[70]uint8)(unsafe.Pointer(bp)))[0] = uint8(ch & uint32(0xff)) length = int32(1) } else { if ch < uint32(0x00800) { (*(*[70]uint8)(unsafe.Pointer(bp)))[0] = uint8(int32(0xc0) + int32(uint8(ch>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x1f)))) (*(*[70]uint8)(unsafe.Pointer(bp)))[int32(1)] = uint8(int32(0x80) + int32(uint8(ch&libc.Uint32FromInt32(0x3f)))) length = int32(2) } else { if ch < uint32(0x10000) { (*(*[70]uint8)(unsafe.Pointer(bp)))[0] = uint8(int32(0xe0) + int32(uint8(ch>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x0f)))) (*(*[70]uint8)(unsafe.Pointer(bp)))[int32(1)] = uint8(int32(0x80) + int32(uint8(ch>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3f)))) (*(*[70]uint8)(unsafe.Pointer(bp)))[int32(2)] = uint8(int32(0x80) + int32(uint8(ch&libc.Uint32FromInt32(0x3f)))) length = int32(3) } else { (*(*[70]uint8)(unsafe.Pointer(bp)))[0] = uint8(int32(0xf0) + int32(uint8(ch>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x07)))) (*(*[70]uint8)(unsafe.Pointer(bp)))[int32(1)] = uint8(int32(0x80) + int32(uint8(ch>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x3f)))) (*(*[70]uint8)(unsafe.Pointer(bp)))[int32(2)] = uint8(int32(0x80) + int32(uint8(ch>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3f)))) (*(*[70]uint8)(unsafe.Pointer(bp)))[int32(3)] = uint8(int32(0x80) + int32(uint8(ch&libc.Uint32FromInt32(0x3f)))) length = int32(4) } } } } if precision > int32(1) { nPrior = int64(1) width -= precision - int32(1) if width > int32(1) && !(flag_leftjustify != 0) { Xsqlite3_str_appendchar(tls, pAccum, width-int32(1), uint8(' ')) width = 0 } Xsqlite3_str_append(tls, pAccum, bp, length) precision-- for precision > int32(1) { if nPrior > int64(precision-int32(1)) { nPrior = int64(precision - int32(1)) } nCopyBytes = int64(length) * nPrior if nCopyBytes+int64((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnChar) >= int64((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnAlloc) { _sqlite3StrAccumEnlarge(tls, pAccum, nCopyBytes) } if (*Tsqlite3_str)(unsafe.Pointer(pAccum)).FaccError != 0 { break } Xsqlite3_str_append(tls, pAccum, (*Tsqlite3_str)(unsafe.Pointer(pAccum)).FzText+uintptr(int64((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnChar)-nCopyBytes), int32(nCopyBytes)) precision = int32(int64(precision) - nPrior) nPrior *= int64(2) } } bufpt = bp flag_altform2 = uint8(1) goto adjust_width_for_utf8 _37: ; _36: ; if bArgList != 0 { bufpt = _getTextArg(tls, pArgList) xtype = uint8(etSTRING) } else { bufpt = libc.VaUintptr(&ap) } if bufpt == uintptr(0) { bufpt = __ccgo_ts + 1648 } else { if int32(xtype) == int32(etDYNSTRING) { if (*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnChar == uint32(0) && (*Tsqlite3_str)(unsafe.Pointer(pAccum)).FmxAlloc != 0 && width == 0 && precision < 0 && int32((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FaccError) == 0 { /* Special optimization for sqlite3_mprintf("%z..."): ** Extend an existing memory allocation rather than creating ** a new one. */ _ = libc.Int32FromInt32(0) (*Tsqlite3_str)(unsafe.Pointer(pAccum)).FzText = bufpt (*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnAlloc = uint32(_sqlite3DbMallocSize(tls, (*Tsqlite3_str)(unsafe.Pointer(pAccum)).Fdb, bufpt)) (*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnChar = uint32(int32(0x7fffffff) & int32(libc.Xstrlen(tls, bufpt))) p92 = pAccum + 21 *(*Tu8)(unsafe.Pointer(p92)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p92))) | libc.Int32FromInt32(SQLITE_PRINTF_MALLOCED)) length = 0 goto _44 } zExtra = bufpt } } if precision >= 0 { if flag_altform2 != 0 { /* Set length to the number of bytes needed in order to display ** precision characters */ z = bufpt for { v93 = precision precision-- if !(v93 > 0 && *(*uint8)(unsafe.Pointer(z)) != 0) { break } v94 = z z++ if int32(*(*uint8)(unsafe.Pointer(v94))) >= int32(0xc0) { for int32(*(*uint8)(unsafe.Pointer(z)))&int32(0xc0) == int32(0x80) { z++ } } } length = int32(z) - int32(bufpt) } else { length = 0 for { if !(length < precision && *(*uint8)(unsafe.Pointer(bufpt + uintptr(length))) != 0) { break } goto _95 _95: ; length++ } } } else { length = int32(0x7fffffff) & int32(libc.Xstrlen(tls, bufpt)) } goto adjust_width_for_utf8 adjust_width_for_utf8: ; if flag_altform2 != 0 && width > 0 { /* Adjust width to account for extra bytes in UTF-8 characters */ ii = length - int32(1) for ii >= 0 { v96 = ii ii-- if int32(*(*uint8)(unsafe.Pointer(bufpt + uintptr(v96))))&int32(0xc0) == int32(0x80) { width++ } } } goto _44 _40: ; /* %q: Escape ' characters */ _39: ; /* %Q: Escape ' and enclose in '...' */ _38: ; if int32(xtype) == int32(etSQLESCAPE3) { v97 = int32('"') } else { v97 = int32('\'') } q = uint8(v97) if bArgList != 0 { escarg = _getTextArg(tls, pArgList) } else { escarg = libc.VaUintptr(&ap) } isnull = libc.BoolInt32(escarg == uintptr(0)) if isnull != 0 { if int32(xtype) == int32(etSQLESCAPE2) { v98 = __ccgo_ts + 1649 } else { v98 = __ccgo_ts + 1654 } escarg = v98 } /* For %q, %Q, and %w, the precision is the number of bytes (or ** characters if the ! flags is present) to use from the input. ** Because of the extra quoting characters inserted, the number ** of output characters may be larger than the precision. */ k = int64(precision) v100 = libc.Int64FromInt32(0) n1 = v100 i1 = v100 for { if v102 = k != 0; v102 { v101 = *(*uint8)(unsafe.Pointer(escarg + uintptr(i1))) ch1 = v101 } if !(v102 && int32(v101) != 0) { break } if int32(ch1) == int32(q) { n1++ } if flag_altform2 != 0 && int32(ch1)&int32(0xc0) == int32(0xc0) { for int32(*(*uint8)(unsafe.Pointer(escarg + uintptr(i1+int64(1)))))&int32(0xc0) == int32(0x80) { i1++ } } goto _99 _99: ; i1++ k-- } needQuote = libc.BoolInt32(!(isnull != 0) && int32(xtype) == int32(etSQLESCAPE2)) n1 += i1 + int64(3) if n1 > int64(SQLITE_PRINT_BUF_SIZE) { v103 = _printfTempBuf(tls, pAccum, n1) zExtra = v103 bufpt = v103 if bufpt == uintptr(0) { return } } else { bufpt = bp } j1 = 0 if needQuote != 0 { v104 = j1 j1++ *(*uint8)(unsafe.Pointer(bufpt + uintptr(v104))) = q } k = i1 i1 = 0 for { if !(i1 < k) { break } v106 = j1 j1++ v107 = *(*uint8)(unsafe.Pointer(escarg + uintptr(i1))) ch1 = v107 *(*uint8)(unsafe.Pointer(bufpt + uintptr(v106))) = v107 if int32(ch1) == int32(q) { v108 = j1 j1++ *(*uint8)(unsafe.Pointer(bufpt + uintptr(v108))) = ch1 } goto _105 _105: ; i1++ } if needQuote != 0 { v109 = j1 j1++ *(*uint8)(unsafe.Pointer(bufpt + uintptr(v109))) = q } *(*uint8)(unsafe.Pointer(bufpt + uintptr(j1))) = uint8(0) length = int32(j1) goto adjust_width_for_utf8 _41: ; if int32((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FprintfFlags)&int32(SQLITE_PRINTF_INTERNAL) == 0 { return } if flag_alternateform != 0 { /* %#T means an Expr pointer that uses Expr.u.zToken */ pExpr = libc.VaUintptr(&ap) if pExpr != 0 && !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_IntValue)) != libc.Uint32FromInt32(0)) { Xsqlite3_str_appendall(tls, pAccum, *(*uintptr)(unsafe.Pointer(pExpr + 8))) _sqlite3RecordErrorOffsetOfExpr(tls, (*Tsqlite3_str)(unsafe.Pointer(pAccum)).Fdb, pExpr) } } else { /* %T means a Token pointer */ pToken = libc.VaUintptr(&ap) _ = libc.Int32FromInt32(0) if pToken != 0 && (*TToken)(unsafe.Pointer(pToken)).Fn != 0 { Xsqlite3_str_append(tls, pAccum, (*TToken)(unsafe.Pointer(pToken)).Fz, int32((*TToken)(unsafe.Pointer(pToken)).Fn)) _sqlite3RecordErrorByteOffset(tls, (*Tsqlite3_str)(unsafe.Pointer(pAccum)).Fdb, (*TToken)(unsafe.Pointer(pToken)).Fz) } } v110 = libc.Int32FromInt32(0) width = v110 length = v110 goto _44 _42: ; if int32((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FprintfFlags)&int32(SQLITE_PRINTF_INTERNAL) == 0 { return } pItem = libc.VaUintptr(&ap) _ = libc.Int32FromInt32(0) if (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias != 0 && !(flag_altform2 != 0) { Xsqlite3_str_appendall(tls, pAccum, (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias) } else { if (*TSrcItem)(unsafe.Pointer(pItem)).FzName != 0 { if (*TSrcItem)(unsafe.Pointer(pItem)).FzDatabase != 0 { Xsqlite3_str_appendall(tls, pAccum, (*TSrcItem)(unsafe.Pointer(pItem)).FzDatabase) Xsqlite3_str_append(tls, pAccum, __ccgo_ts+1661, int32(1)) } Xsqlite3_str_appendall(tls, pAccum, (*TSrcItem)(unsafe.Pointer(pItem)).FzName) } else { if (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias != 0 { Xsqlite3_str_appendall(tls, pAccum, (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias) } else { pSel = (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect _ = libc.Int32FromInt32(0) if (*TSelect)(unsafe.Pointer(pSel)).FselFlags&uint32(SF_NestedFrom) != 0 { Xsqlite3_str_appendf(tls, pAccum, __ccgo_ts+1663, libc.VaList(bp+120, (*TSelect)(unsafe.Pointer(pSel)).FselId)) } else { Xsqlite3_str_appendf(tls, pAccum, __ccgo_ts+1673, libc.VaList(bp+120, (*TSelect)(unsafe.Pointer(pSel)).FselId)) } } } } v111 = libc.Int32FromInt32(0) width = v111 length = v111 goto _44 _43: ; _ = libc.Int32FromInt32(0) return _44: ; /* End switch over the format type */ /* ** The text of the conversion is pointed to by "bufpt" and is ** "length" characters long. The field width is "width". Do ** the output. Both length and width are in bytes, not characters, ** at this point. If the "!" flag was present on string conversions ** indicating that width and precision should be expressed in characters, ** then the values have been translated prior to reaching this point. */ width -= length if width > 0 { if !(flag_leftjustify != 0) { Xsqlite3_str_appendchar(tls, pAccum, width, uint8(' ')) } Xsqlite3_str_append(tls, pAccum, bufpt, length) if flag_leftjustify != 0 { Xsqlite3_str_appendchar(tls, pAccum, width, uint8(' ')) } } else { Xsqlite3_str_append(tls, pAccum, bufpt, length) } if zExtra != 0 { _sqlite3DbFree(tls, (*Tsqlite3_str)(unsafe.Pointer(pAccum)).Fdb, zExtra) zExtra = uintptr(0) } goto _1 _1: ; fmt++ } /* End for loop over the format string */ } var _zOrd = [9]uint8{'t', 'h', 's', 't', 'n', 'd', 'r', 'd'} /* End of function */ // C documentation // // /* // ** The z string points to the first character of a token that is // ** associated with an error. If db does not already have an error // ** byte offset recorded, try to compute the error byte offset for // ** z and set the error byte offset in db. // */ func _sqlite3RecordErrorByteOffset(tls *libc.TLS, db uintptr, z uintptr) { var pParse, zEnd, zText uintptr _, _, _ = pParse, zEnd, zText _ = libc.Int32FromInt32(0) if db == uintptr(0) { return } if (*Tsqlite3)(unsafe.Pointer(db)).FerrByteOffset != -int32(2) { return } pParse = (*Tsqlite3)(unsafe.Pointer(db)).FpParse if pParse == uintptr(0) { return } zText = (*TParse)(unsafe.Pointer(pParse)).FzTail if zText == uintptr(0) { return } zEnd = zText + uintptr(libc.Xstrlen(tls, zText)) if uint32(z) >= uint32(zText) && uint32(z) < uint32(zEnd) { (*Tsqlite3)(unsafe.Pointer(db)).FerrByteOffset = int32(z) - int32(zText) } } // C documentation // // /* // ** If pExpr has a byte offset for the start of a token, record that as // ** as the error offset. // */ func _sqlite3RecordErrorOffsetOfExpr(tls *libc.TLS, db uintptr, pExpr uintptr) { for pExpr != 0 && ((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != uint32(0) || *(*int32)(unsafe.Pointer(pExpr + 36)) <= 0) { pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft } if pExpr == uintptr(0) { return } (*Tsqlite3)(unsafe.Pointer(db)).FerrByteOffset = *(*int32)(unsafe.Pointer(pExpr + 36)) } // C documentation // // /* // ** Enlarge the memory allocation on a StrAccum object so that it is // ** able to accept at least N more bytes of text. // ** // ** Return the number of bytes of text that StrAccum is able to accept // ** after the attempted enlargement. The value returned might be zero. // */ func _sqlite3StrAccumEnlarge(tls *libc.TLS, p uintptr, N Ti64) (r int32) { var szNew Ti64 var zNew, zOld, v1, p2 uintptr _, _, _, _, _ = szNew, zNew, zOld, v1, p2 _ = libc.Int32FromInt32(0) /* Only called if really needed */ if (*TStrAccum)(unsafe.Pointer(p)).FaccError != 0 { return 0 } if (*TStrAccum)(unsafe.Pointer(p)).FmxAlloc == uint32(0) { _sqlite3StrAccumSetError(tls, p, uint8(SQLITE_TOOBIG)) return int32((*TStrAccum)(unsafe.Pointer(p)).FnAlloc - (*TStrAccum)(unsafe.Pointer(p)).FnChar - uint32(1)) } else { if int32((*TStrAccum)(unsafe.Pointer(p)).FprintfFlags)&int32(SQLITE_PRINTF_MALLOCED) != 0 { v1 = (*TStrAccum)(unsafe.Pointer(p)).FzText } else { v1 = uintptr(0) } zOld = v1 szNew = int64((*TStrAccum)(unsafe.Pointer(p)).FnChar) + N + int64(1) if szNew+int64((*TStrAccum)(unsafe.Pointer(p)).FnChar) <= int64((*TStrAccum)(unsafe.Pointer(p)).FmxAlloc) { /* Force exponential buffer size growth as long as it does not overflow, ** to avoid having to call this routine too often */ szNew += int64((*TStrAccum)(unsafe.Pointer(p)).FnChar) } if szNew > int64((*TStrAccum)(unsafe.Pointer(p)).FmxAlloc) { Xsqlite3_str_reset(tls, p) _sqlite3StrAccumSetError(tls, p, uint8(SQLITE_TOOBIG)) return 0 } else { (*TStrAccum)(unsafe.Pointer(p)).FnAlloc = uint32(int32(szNew)) } if (*TStrAccum)(unsafe.Pointer(p)).Fdb != 0 { zNew = _sqlite3DbRealloc(tls, (*TStrAccum)(unsafe.Pointer(p)).Fdb, zOld, uint64((*TStrAccum)(unsafe.Pointer(p)).FnAlloc)) } else { zNew = _sqlite3Realloc(tls, zOld, uint64((*TStrAccum)(unsafe.Pointer(p)).FnAlloc)) } if zNew != 0 { _ = libc.Int32FromInt32(0) if !(int32((*TStrAccum)(unsafe.Pointer(p)).FprintfFlags)&libc.Int32FromInt32(SQLITE_PRINTF_MALLOCED) != libc.Int32FromInt32(0)) && (*TStrAccum)(unsafe.Pointer(p)).FnChar > uint32(0) { libc.Xmemcpy(tls, zNew, (*TStrAccum)(unsafe.Pointer(p)).FzText, (*TStrAccum)(unsafe.Pointer(p)).FnChar) } (*TStrAccum)(unsafe.Pointer(p)).FzText = zNew (*TStrAccum)(unsafe.Pointer(p)).FnAlloc = uint32(_sqlite3DbMallocSize(tls, (*TStrAccum)(unsafe.Pointer(p)).Fdb, zNew)) p2 = p + 21 *(*Tu8)(unsafe.Pointer(p2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p2))) | libc.Int32FromInt32(SQLITE_PRINTF_MALLOCED)) } else { Xsqlite3_str_reset(tls, p) _sqlite3StrAccumSetError(tls, p, uint8(SQLITE_NOMEM)) return 0 } } _ = libc.Int32FromInt32(0) return int32(N) } // C documentation // // /* // ** Append N copies of character c to the given string buffer. // */ func Xsqlite3_str_appendchar(tls *libc.TLS, p uintptr, N int32, c uint8) { var v1, v3 int32 var v2 bool var v4 Tu32 var v5 uintptr _, _, _, _, _ = v1, v2, v3, v4, v5 if v2 = int64((*Tsqlite3_str)(unsafe.Pointer(p)).FnChar)+int64(N) >= int64((*Tsqlite3_str)(unsafe.Pointer(p)).FnAlloc); v2 { v1 = _sqlite3StrAccumEnlarge(tls, p, int64(N)) N = v1 } if v2 && v1 <= 0 { return } for { v3 = N N-- if !(v3 > 0) { break } v5 = p + 16 v4 = *(*Tu32)(unsafe.Pointer(v5)) *(*Tu32)(unsafe.Pointer(v5))++ *(*uint8)(unsafe.Pointer((*Tsqlite3_str)(unsafe.Pointer(p)).FzText + uintptr(v4))) = c } } // C documentation // // /* // ** The StrAccum "p" is not large enough to accept N new bytes of z[]. // ** So enlarge if first, then do the append. // ** // ** This is a helper routine to sqlite3_str_append() that does special-case // ** work (enlarging the buffer) using tail recursion, so that the // ** sqlite3_str_append() routine can use fast calling semantics. // */ func _enlargeAndAppend(tls *libc.TLS, p uintptr, z uintptr, N int32) { N = _sqlite3StrAccumEnlarge(tls, p, int64(N)) if N > 0 { libc.Xmemcpy(tls, (*TStrAccum)(unsafe.Pointer(p)).FzText+uintptr((*TStrAccum)(unsafe.Pointer(p)).FnChar), z, uint32(N)) *(*Tu32)(unsafe.Pointer(p + 16)) += uint32(N) } } // C documentation // // /* // ** Append N bytes of text from z to the StrAccum object. Increase the // ** size of the memory allocation for StrAccum if necessary. // */ func Xsqlite3_str_append(tls *libc.TLS, p uintptr, z uintptr, N int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3_str)(unsafe.Pointer(p)).FnChar+uint32(N) >= (*Tsqlite3_str)(unsafe.Pointer(p)).FnAlloc { _enlargeAndAppend(tls, p, z, N) } else { if N != 0 { _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(p + 16)) += uint32(N) libc.Xmemcpy(tls, (*Tsqlite3_str)(unsafe.Pointer(p)).FzText+uintptr((*Tsqlite3_str)(unsafe.Pointer(p)).FnChar-uint32(N)), z, uint32(N)) } } } // C documentation // // /* // ** Append the complete text of zero-terminated string z[] to the p string. // */ func Xsqlite3_str_appendall(tls *libc.TLS, p uintptr, z uintptr) { Xsqlite3_str_append(tls, p, z, _sqlite3Strlen30(tls, z)) } // C documentation // // /* // ** Finish off a string by making sure it is zero-terminated. // ** Return a pointer to the resulting string. Return a NULL // ** pointer if any kind of error was encountered. // */ func _strAccumFinishRealloc(tls *libc.TLS, p uintptr) (r uintptr) { var zText, p1 uintptr _, _ = zText, p1 _ = libc.Int32FromInt32(0) zText = _sqlite3DbMallocRaw(tls, (*TStrAccum)(unsafe.Pointer(p)).Fdb, uint64((*TStrAccum)(unsafe.Pointer(p)).FnChar+uint32(1))) if zText != 0 { libc.Xmemcpy(tls, zText, (*TStrAccum)(unsafe.Pointer(p)).FzText, (*TStrAccum)(unsafe.Pointer(p)).FnChar+uint32(1)) p1 = p + 21 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | libc.Int32FromInt32(SQLITE_PRINTF_MALLOCED)) } else { _sqlite3StrAccumSetError(tls, p, uint8(SQLITE_NOMEM)) } (*TStrAccum)(unsafe.Pointer(p)).FzText = zText return zText } func _sqlite3StrAccumFinish(tls *libc.TLS, p uintptr) (r uintptr) { if (*TStrAccum)(unsafe.Pointer(p)).FzText != 0 { *(*uint8)(unsafe.Pointer((*TStrAccum)(unsafe.Pointer(p)).FzText + uintptr((*TStrAccum)(unsafe.Pointer(p)).FnChar))) = uint8(0) if (*TStrAccum)(unsafe.Pointer(p)).FmxAlloc > uint32(0) && !(int32((*TStrAccum)(unsafe.Pointer(p)).FprintfFlags)&libc.Int32FromInt32(SQLITE_PRINTF_MALLOCED) != libc.Int32FromInt32(0)) { return _strAccumFinishRealloc(tls, p) } } return (*TStrAccum)(unsafe.Pointer(p)).FzText } // C documentation // // /* // ** Use the content of the StrAccum passed as the second argument // ** as the result of an SQL function. // */ func _sqlite3ResultStrAccum(tls *libc.TLS, pCtx uintptr, p uintptr) { if (*TStrAccum)(unsafe.Pointer(p)).FaccError != 0 { Xsqlite3_result_error_code(tls, pCtx, int32((*TStrAccum)(unsafe.Pointer(p)).FaccError)) Xsqlite3_str_reset(tls, p) } else { if int32((*TStrAccum)(unsafe.Pointer(p)).FprintfFlags)&int32(SQLITE_PRINTF_MALLOCED) != 0 { Xsqlite3_result_text(tls, pCtx, (*TStrAccum)(unsafe.Pointer(p)).FzText, int32((*TStrAccum)(unsafe.Pointer(p)).FnChar), __ccgo_fp(_sqlite3OomClear)) } else { Xsqlite3_result_text(tls, pCtx, __ccgo_ts+1648, 0, libc.UintptrFromInt32(0)) Xsqlite3_str_reset(tls, p) } } } // C documentation // // /* // ** This singleton is an sqlite3_str object that is returned if // ** sqlite3_malloc() fails to provide space for a real one. This // ** sqlite3_str object accepts no new text and always returns // ** an SQLITE_NOMEM error. // */ var _sqlite3OomStr = Tsqlite3_str{ FaccError: uint8(SQLITE_NOMEM), } // C documentation // // /* Finalize a string created using sqlite3_str_new(). // */ func Xsqlite3_str_finish(tls *libc.TLS, p uintptr) (r uintptr) { var z uintptr _ = z if p != uintptr(0) && p != uintptr(unsafe.Pointer(&_sqlite3OomStr)) { z = _sqlite3StrAccumFinish(tls, p) Xsqlite3_free(tls, p) } else { z = uintptr(0) } return z } // C documentation // // /* Return any error code associated with p */ func Xsqlite3_str_errcode(tls *libc.TLS, p uintptr) (r int32) { var v1 int32 _ = v1 if p != 0 { v1 = int32((*Tsqlite3_str)(unsafe.Pointer(p)).FaccError) } else { v1 = int32(SQLITE_NOMEM) } return v1 } // C documentation // // /* Return the current length of p in bytes */ func Xsqlite3_str_length(tls *libc.TLS, p uintptr) (r int32) { var v1 uint32 _ = v1 if p != 0 { v1 = (*Tsqlite3_str)(unsafe.Pointer(p)).FnChar } else { v1 = uint32(0) } return int32(v1) } // C documentation // // /* Return the current value for p */ func Xsqlite3_str_value(tls *libc.TLS, p uintptr) (r uintptr) { if p == uintptr(0) || (*Tsqlite3_str)(unsafe.Pointer(p)).FnChar == uint32(0) { return uintptr(0) } *(*uint8)(unsafe.Pointer((*Tsqlite3_str)(unsafe.Pointer(p)).FzText + uintptr((*Tsqlite3_str)(unsafe.Pointer(p)).FnChar))) = uint8(0) return (*Tsqlite3_str)(unsafe.Pointer(p)).FzText } // C documentation // // /* // ** Reset an StrAccum string. Reclaim all malloced memory. // */ func Xsqlite3_str_reset(tls *libc.TLS, p uintptr) { var p1 uintptr _ = p1 if int32((*TStrAccum)(unsafe.Pointer(p)).FprintfFlags)&int32(SQLITE_PRINTF_MALLOCED) != 0 { _sqlite3DbFree(tls, (*TStrAccum)(unsafe.Pointer(p)).Fdb, (*TStrAccum)(unsafe.Pointer(p)).FzText) p1 = p + 21 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(SQLITE_PRINTF_MALLOCED)) } (*TStrAccum)(unsafe.Pointer(p)).FnAlloc = uint32(0) (*TStrAccum)(unsafe.Pointer(p)).FnChar = uint32(0) (*TStrAccum)(unsafe.Pointer(p)).FzText = uintptr(0) } // C documentation // // /* // ** Initialize a string accumulator. // ** // ** p: The accumulator to be initialized. // ** db: Pointer to a database connection. May be NULL. Lookaside // ** memory is used if not NULL. db->mallocFailed is set appropriately // ** when not NULL. // ** zBase: An initial buffer. May be NULL in which case the initial buffer // ** is malloced. // ** n: Size of zBase in bytes. If total space requirements never exceed // ** n then no memory allocations ever occur. // ** mx: Maximum number of bytes to accumulate. If mx==0 then no memory // ** allocations will ever occur. // */ func _sqlite3StrAccumInit(tls *libc.TLS, p uintptr, db uintptr, zBase uintptr, n int32, mx int32) { (*TStrAccum)(unsafe.Pointer(p)).FzText = zBase (*TStrAccum)(unsafe.Pointer(p)).Fdb = db (*TStrAccum)(unsafe.Pointer(p)).FnAlloc = uint32(n) (*TStrAccum)(unsafe.Pointer(p)).FmxAlloc = uint32(mx) (*TStrAccum)(unsafe.Pointer(p)).FnChar = uint32(0) (*TStrAccum)(unsafe.Pointer(p)).FaccError = uint8(0) (*TStrAccum)(unsafe.Pointer(p)).FprintfFlags = uint8(0) } // C documentation // // /* Allocate and initialize a new dynamic string object */ func Xsqlite3_str_new(tls *libc.TLS, db uintptr) (r uintptr) { var p uintptr var v1 int32 _, _ = p, v1 p = Xsqlite3_malloc64(tls, uint64(24)) if p != 0 { if db != 0 { v1 = *(*int32)(unsafe.Pointer(db + 120)) } else { v1 = int32(SQLITE_MAX_LENGTH) } _sqlite3StrAccumInit(tls, p, uintptr(0), uintptr(0), 0, v1) } else { p = uintptr(unsafe.Pointer(&_sqlite3OomStr)) } return p } // C documentation // // /* // ** Print into memory obtained from sqliteMalloc(). Use the internal // ** %-conversion extensions. // */ func _sqlite3VMPrintf(tls *libc.TLS, db uintptr, zFormat uintptr, ap Tva_list) (r uintptr) { bp := tls.Alloc(96) defer tls.Free(96) var z uintptr var _ /* acc at bp+72 */ TStrAccum var _ /* zBase at bp+0 */ [70]uint8 _ = z _ = libc.Int32FromInt32(0) _sqlite3StrAccumInit(tls, bp+72, db, bp, int32(70), *(*int32)(unsafe.Pointer(db + 120))) (*(*TStrAccum)(unsafe.Pointer(bp + 72))).FprintfFlags = uint8(SQLITE_PRINTF_INTERNAL) Xsqlite3_str_vappendf(tls, bp+72, zFormat, ap) z = _sqlite3StrAccumFinish(tls, bp+72) if int32((*(*TStrAccum)(unsafe.Pointer(bp + 72))).FaccError) == int32(SQLITE_NOMEM) { _sqlite3OomFault(tls, db) } return z } // C documentation // // /* // ** Print into memory obtained from sqliteMalloc(). Use the internal // ** %-conversion extensions. // */ func _sqlite3MPrintf(tls *libc.TLS, db uintptr, zFormat uintptr, va uintptr) (r uintptr) { var ap Tva_list var z uintptr _, _ = ap, z ap = va z = _sqlite3VMPrintf(tls, db, zFormat, ap) _ = ap return z } // C documentation // // /* // ** Print into memory obtained from sqlite3_malloc(). Omit the internal // ** %-conversion extensions. // */ func Xsqlite3_vmprintf(tls *libc.TLS, zFormat uintptr, ap Tva_list) (r uintptr) { bp := tls.Alloc(96) defer tls.Free(96) var z uintptr var _ /* acc at bp+72 */ TStrAccum var _ /* zBase at bp+0 */ [70]uint8 _ = z if Xsqlite3_initialize(tls) != 0 { return uintptr(0) } _sqlite3StrAccumInit(tls, bp+72, uintptr(0), bp, int32(70), int32(SQLITE_MAX_LENGTH)) Xsqlite3_str_vappendf(tls, bp+72, zFormat, ap) z = _sqlite3StrAccumFinish(tls, bp+72) return z } // C documentation // // /* // ** Print into memory obtained from sqlite3_malloc()(). Omit the internal // ** %-conversion extensions. // */ func Xsqlite3_mprintf(tls *libc.TLS, zFormat uintptr, va uintptr) (r uintptr) { var ap Tva_list var z uintptr _, _ = ap, z if Xsqlite3_initialize(tls) != 0 { return uintptr(0) } ap = va z = Xsqlite3_vmprintf(tls, zFormat, ap) _ = ap return z } // C documentation // // /* // ** sqlite3_snprintf() works like snprintf() except that it ignores the // ** current locale settings. This is important for SQLite because we // ** are not able to use a "," as the decimal point in place of "." as // ** specified by some locales. // ** // ** Oops: The first two arguments of sqlite3_snprintf() are backwards // ** from the snprintf() standard. Unfortunately, it is too late to change // ** this without breaking compatibility, so we just have to live with the // ** mistake. // ** // ** sqlite3_vsnprintf() is the varargs version. // */ func Xsqlite3_vsnprintf(tls *libc.TLS, n int32, zBuf uintptr, zFormat uintptr, ap Tva_list) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* acc at bp+0 */ TStrAccum if n <= 0 { return zBuf } _sqlite3StrAccumInit(tls, bp, uintptr(0), zBuf, n, 0) Xsqlite3_str_vappendf(tls, bp, zFormat, ap) *(*uint8)(unsafe.Pointer(zBuf + uintptr((*(*TStrAccum)(unsafe.Pointer(bp))).FnChar))) = uint8(0) return zBuf } func Xsqlite3_snprintf(tls *libc.TLS, n int32, zBuf uintptr, zFormat uintptr, va uintptr) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var ap Tva_list var _ /* acc at bp+0 */ TStrAccum _ = ap if n <= 0 { return zBuf } _sqlite3StrAccumInit(tls, bp, uintptr(0), zBuf, n, 0) ap = va Xsqlite3_str_vappendf(tls, bp, zFormat, ap) _ = ap *(*uint8)(unsafe.Pointer(zBuf + uintptr((*(*TStrAccum)(unsafe.Pointer(bp))).FnChar))) = uint8(0) return zBuf } // C documentation // // /* // ** This is the routine that actually formats the sqlite3_log() message. // ** We house it in a separate routine from sqlite3_log() to avoid using // ** stack space on small-stack systems when logging is disabled. // ** // ** sqlite3_log() must render into a static buffer. It cannot dynamically // ** allocate memory because it might be called while the memory allocator // ** mutex is held. // ** // ** sqlite3_str_vappendf() might ask for *temporary* memory allocations for // ** certain format characters (%q) or for very large precisions or widths. // ** Care must be taken that any sqlite3_log() calls that occur while the // ** memory mutex is held do not use these mechanisms. // */ func _renderLogMsg(tls *libc.TLS, iErrCode int32, zFormat uintptr, ap Tva_list) { bp := tls.Alloc(240) defer tls.Free(240) var _ /* acc at bp+0 */ TStrAccum var _ /* zMsg at bp+24 */ [210]uint8 /* Complete log message */ _sqlite3StrAccumInit(tls, bp, uintptr(0), bp+24, int32(210), 0) Xsqlite3_str_vappendf(tls, bp, zFormat, ap) (*(*func(*libc.TLS, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.FxLog})))(tls, _sqlite3Config.FpLogArg, iErrCode, _sqlite3StrAccumFinish(tls, bp)) } // C documentation // // /* // ** Format and write a message to the log if logging is enabled. // */ func Xsqlite3_log(tls *libc.TLS, iErrCode int32, zFormat uintptr, va uintptr) { var ap Tva_list _ = ap /* Vararg list */ if _sqlite3Config.FxLog != 0 { ap = va _renderLogMsg(tls, iErrCode, zFormat, ap) _ = ap } } // C documentation // // /* // ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument // ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats. // */ func Xsqlite3_str_appendf(tls *libc.TLS, p uintptr, zFormat uintptr, va uintptr) { var ap Tva_list _ = ap ap = va Xsqlite3_str_vappendf(tls, p, zFormat, ap) _ = ap } /***************************************************************************** ** Reference counted string/blob storage *****************************************************************************/ // C documentation // // /* // ** Increase the reference count of the string by one. // ** // ** The input parameter is returned. // */ func _sqlite3RCStrRef(tls *libc.TLS, z uintptr) (r uintptr) { var p uintptr _ = p p = z _ = libc.Int32FromInt32(0) p -= 8 (*TRCStr)(unsafe.Pointer(p)).FnRCRef++ return z } // C documentation // // /* // ** Decrease the reference count by one. Free the string when the // ** reference count reaches zero. // */ func _sqlite3RCStrUnref(tls *libc.TLS, z uintptr) { var p uintptr _ = p p = z _ = libc.Int32FromInt32(0) p -= 8 _ = libc.Int32FromInt32(0) if (*TRCStr)(unsafe.Pointer(p)).FnRCRef >= uint64(2) { (*TRCStr)(unsafe.Pointer(p)).FnRCRef-- } else { Xsqlite3_free(tls, p) } } // C documentation // // /* // ** Create a new string that is capable of holding N bytes of text, not counting // ** the zero byte at the end. The string is uninitialized. // ** // ** The reference count is initially 1. Call sqlite3RCStrUnref() to free the // ** newly allocated string. // ** // ** This routine returns 0 on an OOM. // */ func _sqlite3RCStrNew(tls *libc.TLS, N Tu64) (r uintptr) { var p uintptr _ = p p = Xsqlite3_malloc64(tls, N+uint64(8)+uint64(1)) if p == uintptr(0) { return uintptr(0) } (*TRCStr)(unsafe.Pointer(p)).FnRCRef = uint64(1) return p + 1*8 } // C documentation // // /* // ** Change the size of the string so that it is able to hold N bytes. // ** The string might be reallocated, so return the new allocation. // */ func _sqlite3RCStrResize(tls *libc.TLS, z uintptr, N Tu64) (r uintptr) { var p, pNew uintptr _, _ = p, pNew p = z _ = libc.Int32FromInt32(0) p -= 8 _ = libc.Int32FromInt32(0) pNew = Xsqlite3_realloc64(tls, p, N+uint64(8)+uint64(1)) if pNew == uintptr(0) { Xsqlite3_free(tls, p) return uintptr(0) } else { return pNew + 1*8 } return r } /************** End of printf.c **********************************************/ /************** Begin file treeview.c ****************************************/ /* ** 2015-06-08 ** ** 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 contains C code to implement the TreeView debugging routines. ** These routines print a parse tree to standard output for debugging and ** analysis. ** ** The interfaces in this file is only available when compiling ** with SQLITE_DEBUG. */ /* #include "sqliteInt.h" */ /************** End of treeview.c ********************************************/ /************** Begin file random.c ******************************************/ /* ** 2001 September 15 ** ** 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 contains code to implement a pseudo-random number ** generator (PRNG) for SQLite. ** ** Random numbers are used by some of the database backends in order ** to generate random integer keys for tables or random filenames. */ /* #include "sqliteInt.h" */ // C documentation // // /* All threads share a single random number generator. // ** This structure is the current state of the generator. // */ type Tsqlite3PrngType = struct { Fs [16]Tu32 Fout [64]Tu8 Fn Tu8 } type sqlite3PrngType = Tsqlite3PrngType /************** End of printf.c **********************************************/ /************** Begin file treeview.c ****************************************/ /* ** 2015-06-08 ** ** 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 contains C code to implement the TreeView debugging routines. ** These routines print a parse tree to standard output for debugging and ** analysis. ** ** The interfaces in this file is only available when compiling ** with SQLITE_DEBUG. */ /* #include "sqliteInt.h" */ /************** End of treeview.c ********************************************/ /************** Begin file random.c ******************************************/ /* ** 2001 September 15 ** ** 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 contains code to implement a pseudo-random number ** generator (PRNG) for SQLite. ** ** Random numbers are used by some of the database backends in order ** to generate random integer keys for tables or random filenames. */ /* #include "sqliteInt.h" */ // C documentation // // /* All threads share a single random number generator. // ** This structure is the current state of the generator. // */ var _sqlite3Prng Tsqlite3PrngType // C documentation // // /* The RFC-7539 ChaCha20 block function // */ func _chacha_block(tls *libc.TLS, out uintptr, in uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var i int32 var _ /* x at bp+0 */ [16]Tu32 _ = i libc.Xmemcpy(tls, bp, in, uint32(64)) i = 0 for { if !(i < int32(10)) { break } *(*Tu32)(unsafe.Pointer(bp)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(4)] *(*Tu32)(unsafe.Pointer(bp + 12*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[0] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(12)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(12)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16)) *(*Tu32)(unsafe.Pointer(bp + 8*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(12)] *(*Tu32)(unsafe.Pointer(bp + 4*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(8)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(4)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(4)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12)) *(*Tu32)(unsafe.Pointer(bp)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(4)] *(*Tu32)(unsafe.Pointer(bp + 12*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[0] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(12)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(12)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8)) *(*Tu32)(unsafe.Pointer(bp + 8*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(12)] *(*Tu32)(unsafe.Pointer(bp + 4*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(8)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(4)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(4)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7)) *(*Tu32)(unsafe.Pointer(bp + 1*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(5)] *(*Tu32)(unsafe.Pointer(bp + 13*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(1)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(13)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(13)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16)) *(*Tu32)(unsafe.Pointer(bp + 9*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(13)] *(*Tu32)(unsafe.Pointer(bp + 5*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(9)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(5)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(5)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12)) *(*Tu32)(unsafe.Pointer(bp + 1*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(5)] *(*Tu32)(unsafe.Pointer(bp + 13*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(1)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(13)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(13)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8)) *(*Tu32)(unsafe.Pointer(bp + 9*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(13)] *(*Tu32)(unsafe.Pointer(bp + 5*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(9)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(5)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(5)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7)) *(*Tu32)(unsafe.Pointer(bp + 2*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(6)] *(*Tu32)(unsafe.Pointer(bp + 14*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(2)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(14)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(14)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16)) *(*Tu32)(unsafe.Pointer(bp + 10*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(14)] *(*Tu32)(unsafe.Pointer(bp + 6*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(10)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(6)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(6)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12)) *(*Tu32)(unsafe.Pointer(bp + 2*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(6)] *(*Tu32)(unsafe.Pointer(bp + 14*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(2)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(14)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(14)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8)) *(*Tu32)(unsafe.Pointer(bp + 10*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(14)] *(*Tu32)(unsafe.Pointer(bp + 6*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(10)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(6)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(6)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7)) *(*Tu32)(unsafe.Pointer(bp + 3*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(7)] *(*Tu32)(unsafe.Pointer(bp + 15*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(3)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(15)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(15)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16)) *(*Tu32)(unsafe.Pointer(bp + 11*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(15)] *(*Tu32)(unsafe.Pointer(bp + 7*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(11)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(7)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(7)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12)) *(*Tu32)(unsafe.Pointer(bp + 3*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(7)] *(*Tu32)(unsafe.Pointer(bp + 15*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(3)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(15)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(15)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8)) *(*Tu32)(unsafe.Pointer(bp + 11*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(15)] *(*Tu32)(unsafe.Pointer(bp + 7*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(11)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(7)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(7)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7)) *(*Tu32)(unsafe.Pointer(bp)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(5)] *(*Tu32)(unsafe.Pointer(bp + 15*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[0] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(15)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(15)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16)) *(*Tu32)(unsafe.Pointer(bp + 10*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(15)] *(*Tu32)(unsafe.Pointer(bp + 5*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(10)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(5)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(5)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12)) *(*Tu32)(unsafe.Pointer(bp)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(5)] *(*Tu32)(unsafe.Pointer(bp + 15*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[0] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(15)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(15)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8)) *(*Tu32)(unsafe.Pointer(bp + 10*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(15)] *(*Tu32)(unsafe.Pointer(bp + 5*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(10)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(5)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(5)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7)) *(*Tu32)(unsafe.Pointer(bp + 1*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(6)] *(*Tu32)(unsafe.Pointer(bp + 12*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(1)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(12)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(12)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16)) *(*Tu32)(unsafe.Pointer(bp + 11*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(12)] *(*Tu32)(unsafe.Pointer(bp + 6*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(11)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(6)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(6)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12)) *(*Tu32)(unsafe.Pointer(bp + 1*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(6)] *(*Tu32)(unsafe.Pointer(bp + 12*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(1)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(12)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(12)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8)) *(*Tu32)(unsafe.Pointer(bp + 11*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(12)] *(*Tu32)(unsafe.Pointer(bp + 6*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(11)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(6)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(6)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7)) *(*Tu32)(unsafe.Pointer(bp + 2*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(7)] *(*Tu32)(unsafe.Pointer(bp + 13*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(2)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(13)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(13)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16)) *(*Tu32)(unsafe.Pointer(bp + 8*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(13)] *(*Tu32)(unsafe.Pointer(bp + 7*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(8)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(7)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(7)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12)) *(*Tu32)(unsafe.Pointer(bp + 2*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(7)] *(*Tu32)(unsafe.Pointer(bp + 13*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(2)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(13)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(13)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8)) *(*Tu32)(unsafe.Pointer(bp + 8*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(13)] *(*Tu32)(unsafe.Pointer(bp + 7*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(8)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(7)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(7)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7)) *(*Tu32)(unsafe.Pointer(bp + 3*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(4)] *(*Tu32)(unsafe.Pointer(bp + 14*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(3)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(14)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(14)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16)) *(*Tu32)(unsafe.Pointer(bp + 9*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(14)] *(*Tu32)(unsafe.Pointer(bp + 4*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(9)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(4)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(4)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12)) *(*Tu32)(unsafe.Pointer(bp + 3*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(4)] *(*Tu32)(unsafe.Pointer(bp + 14*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(3)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(14)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(14)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8)) *(*Tu32)(unsafe.Pointer(bp + 9*4)) += (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(14)] *(*Tu32)(unsafe.Pointer(bp + 4*4)) ^= (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(9)] (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(4)] = (*(*[16]Tu32)(unsafe.Pointer(bp)))[int32(4)]<>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7)) goto _1 _1: ; i++ } i = 0 for { if !(i < int32(16)) { break } *(*Tu32)(unsafe.Pointer(out + uintptr(i)*4)) = (*(*[16]Tu32)(unsafe.Pointer(bp)))[i] + *(*Tu32)(unsafe.Pointer(in + uintptr(i)*4)) goto _2 _2: ; i++ } } // C documentation // // /* // ** Return N random bytes. // */ func Xsqlite3_randomness(tls *libc.TLS, N int32, pBuf uintptr) { var mutex, pVfs, zBuf, p1 uintptr _, _, _, _ = mutex, pVfs, zBuf, p1 zBuf = pBuf if Xsqlite3_initialize(tls) != 0 { return } mutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_PRNG)) Xsqlite3_mutex_enter(tls, mutex) if N <= 0 || pBuf == uintptr(0) { *(*Tu32)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Prng)))) = uint32(0) Xsqlite3_mutex_leave(tls, mutex) return } /* Initialize the state of the random number generator once, ** the first time this routine is called. */ if *(*Tu32)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Prng)))) == uint32(0) { pVfs = Xsqlite3_vfs_find(tls, uintptr(0)) libc.Xmemcpy(tls, uintptr(unsafe.Pointer(&_sqlite3Prng)), uintptr(unsafe.Pointer(&_chacha20_init)), uint32(16)) if pVfs == uintptr(0) { libc.Xmemset(tls, uintptr(unsafe.Pointer(&_sqlite3Prng))+4*4, 0, uint32(44)) } else { _sqlite3OsRandomness(tls, pVfs, int32(44), uintptr(unsafe.Pointer(&_sqlite3Prng))+4*4) } *(*Tu32)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Prng)) + 15*4)) = *(*Tu32)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Prng)) + 12*4)) *(*Tu32)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Prng)) + 12*4)) = uint32(0) _sqlite3Prng.Fn = uint8(0) } _ = libc.Int32FromInt32(0) for int32(1) != 0 { if N <= int32(_sqlite3Prng.Fn) { libc.Xmemcpy(tls, zBuf, uintptr(unsafe.Pointer(&_sqlite3Prng))+64+uintptr(int32(_sqlite3Prng.Fn)-N), uint32(N)) p1 = uintptr(unsafe.Pointer(&_sqlite3Prng)) + 128 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) - N) break } if int32(_sqlite3Prng.Fn) > 0 { libc.Xmemcpy(tls, zBuf, uintptr(unsafe.Pointer(&_sqlite3Prng))+64, uint32(_sqlite3Prng.Fn)) N -= int32(_sqlite3Prng.Fn) zBuf += uintptr(_sqlite3Prng.Fn) } *(*Tu32)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Prng)) + 12*4))++ _chacha_block(tls, uintptr(unsafe.Pointer(&_sqlite3Prng))+64, uintptr(unsafe.Pointer(&_sqlite3Prng))) _sqlite3Prng.Fn = uint8(64) } Xsqlite3_mutex_leave(tls, mutex) } var _chacha20_init = [4]Tu32{ 0: uint32(0x61707865), 1: uint32(0x3320646e), 2: uint32(0x79622d32), 3: uint32(0x6b206574), } // C documentation // // /* // ** For testing purposes, we sometimes want to preserve the state of // ** PRNG and restore the PRNG to its saved state at a later time, or // ** to reset the PRNG to its initial state. These routines accomplish // ** those tasks. // ** // ** The sqlite3_test_control() interface calls these routines to // ** control the PRNG. // */ var _sqlite3SavedPrng Tsqlite3PrngType func _sqlite3PrngSaveState(tls *libc.TLS) { libc.Xmemcpy(tls, uintptr(unsafe.Pointer(&_sqlite3SavedPrng)), uintptr(unsafe.Pointer(&_sqlite3Prng)), uint32(132)) } func _sqlite3PrngRestoreState(tls *libc.TLS) { libc.Xmemcpy(tls, uintptr(unsafe.Pointer(&_sqlite3Prng)), uintptr(unsafe.Pointer(&_sqlite3SavedPrng)), uint32(132)) } /************** End of random.c **********************************************/ /************** Begin file threads.c *****************************************/ /* ** 2012 July 21 ** ** 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 presents a simple cross-platform threading interface for ** use internally by SQLite. ** ** A "thread" can be created using sqlite3ThreadCreate(). This thread ** runs independently of its creator until it is joined using ** sqlite3ThreadJoin(), at which point it terminates. ** ** Threads do not have to be real. It could be that the work of the ** "thread" is done by the main thread at either the sqlite3ThreadCreate() ** or sqlite3ThreadJoin() call. This is, in fact, what happens in ** single threaded systems. Nothing in SQLite requires multiple threads. ** This interface exists so that applications that want to take advantage ** of multiple cores can do so, while also allowing applications to stay ** single-threaded if desired. */ /* #include "sqliteInt.h" */ /********************************* Unix Pthreads ****************************/ /******************************** End Unix Pthreads *************************/ /********************************* Win32 Threads ****************************/ /******************************** End Win32 Threads *************************/ /********************************* Single-Threaded **************************/ /* ** This implementation does not actually create a new thread. It does the ** work of the thread in the main thread, when either the thread is created ** or when it is joined */ /* A running thread */ type TSQLiteThread1 = struct { FxTask uintptr FpIn uintptr FpResult uintptr } type SQLiteThread1 = TSQLiteThread1 // C documentation // // /* Create a new thread */ func _sqlite3ThreadCreate(tls *libc.TLS, ppThread uintptr, xTask uintptr, pIn uintptr) (r int32) { var p uintptr _ = p _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(ppThread)) = uintptr(0) p = _sqlite3Malloc(tls, uint64(12)) if p == uintptr(0) { return int32(SQLITE_NOMEM) } if int32(p)/int32(17)&int32(1) != 0 { (*TSQLiteThread)(unsafe.Pointer(p)).FxTask = xTask (*TSQLiteThread)(unsafe.Pointer(p)).FpIn = pIn } else { (*TSQLiteThread)(unsafe.Pointer(p)).FxTask = uintptr(0) (*TSQLiteThread)(unsafe.Pointer(p)).FpResult = (*(*func(*libc.TLS, uintptr) uintptr)(unsafe.Pointer(&struct{ uintptr }{xTask})))(tls, pIn) } *(*uintptr)(unsafe.Pointer(ppThread)) = p return SQLITE_OK } // C documentation // // /* Get the results of the thread */ func _sqlite3ThreadJoin(tls *libc.TLS, p uintptr, ppOut uintptr) (r int32) { _ = libc.Int32FromInt32(0) if p == uintptr(0) { return int32(SQLITE_NOMEM) } if (*TSQLiteThread)(unsafe.Pointer(p)).FxTask != 0 { *(*uintptr)(unsafe.Pointer(ppOut)) = (*(*func(*libc.TLS, uintptr) uintptr)(unsafe.Pointer(&struct{ uintptr }{(*TSQLiteThread)(unsafe.Pointer(p)).FxTask})))(tls, (*TSQLiteThread)(unsafe.Pointer(p)).FpIn) } else { *(*uintptr)(unsafe.Pointer(ppOut)) = (*TSQLiteThread)(unsafe.Pointer(p)).FpResult } Xsqlite3_free(tls, p) return SQLITE_OK } /****************************** End Single-Threaded *************************/ /************** End of threads.c *********************************************/ /************** Begin file utf.c *********************************************/ /* ** 2004 April 13 ** ** 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 contains routines used to translate between UTF-8, ** UTF-16, UTF-16BE, and UTF-16LE. ** ** Notes on UTF-8: ** ** Byte-0 Byte-1 Byte-2 Byte-3 Value ** 0xxxxxxx 00000000 00000000 0xxxxxxx ** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx ** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx ** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx ** ** ** Notes on UTF-16: (with wwww+1==uuuuu) ** ** Word-0 Word-1 Value ** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx ** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx ** ** ** BOM or Byte Order Mark: ** 0xff 0xfe little-endian utf-16 follows ** 0xfe 0xff big-endian utf-16 follows ** */ /* #include "sqliteInt.h" */ /* #include */ /* #include "vdbeInt.h" */ // C documentation // // /* // ** This lookup table is used to help decode the first byte of // ** a multi-byte UTF8 character. // */ var _sqlite3Utf8Trans1 = [64]uint8{ 1: uint8(0x01), 2: uint8(0x02), 3: uint8(0x03), 4: uint8(0x04), 5: uint8(0x05), 6: uint8(0x06), 7: uint8(0x07), 8: uint8(0x08), 9: uint8(0x09), 10: uint8(0x0a), 11: uint8(0x0b), 12: uint8(0x0c), 13: uint8(0x0d), 14: uint8(0x0e), 15: uint8(0x0f), 16: uint8(0x10), 17: uint8(0x11), 18: uint8(0x12), 19: uint8(0x13), 20: uint8(0x14), 21: uint8(0x15), 22: uint8(0x16), 23: uint8(0x17), 24: uint8(0x18), 25: uint8(0x19), 26: uint8(0x1a), 27: uint8(0x1b), 28: uint8(0x1c), 29: uint8(0x1d), 30: uint8(0x1e), 31: uint8(0x1f), 33: uint8(0x01), 34: uint8(0x02), 35: uint8(0x03), 36: uint8(0x04), 37: uint8(0x05), 38: uint8(0x06), 39: uint8(0x07), 40: uint8(0x08), 41: uint8(0x09), 42: uint8(0x0a), 43: uint8(0x0b), 44: uint8(0x0c), 45: uint8(0x0d), 46: uint8(0x0e), 47: uint8(0x0f), 49: uint8(0x01), 50: uint8(0x02), 51: uint8(0x03), 52: uint8(0x04), 53: uint8(0x05), 54: uint8(0x06), 55: uint8(0x07), 57: uint8(0x01), 58: uint8(0x02), 59: uint8(0x03), 61: uint8(0x01), } // C documentation // // /* // ** Translate a single UTF-8 character. Return the unicode value. // ** // ** During translation, assume that the byte that zTerm points // ** is a 0x00. // ** // ** Write a pointer to the next unread byte back into *pzNext. // ** // ** Notes On Invalid UTF-8: // ** // ** * This routine never allows a 7-bit character (0x00 through 0x7f) to // ** be encoded as a multi-byte character. Any multi-byte character that // ** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd. // ** // ** * This routine never allows a UTF16 surrogate value to be encoded. // ** If a multi-byte character attempts to encode a value between // ** 0xd800 and 0xe000 then it is rendered as 0xfffd. // ** // ** * Bytes in the range of 0x80 through 0xbf which occur as the first // ** byte of a character are interpreted as single-byte characters // ** and rendered as themselves even though they are technically // ** invalid characters. // ** // ** * This routine accepts over-length UTF8 encodings // ** for unicode values 0x80 and greater. It does not change over-length // ** encodings to 0xfffd as some systems recommend. // */ func _sqlite3Utf8Read(tls *libc.TLS, pz uintptr) (r Tu32) { var c uint32 var v1, v2, v3, v4 uintptr _, _, _, _, _ = c, v1, v2, v3, v4 /* Same as READ_UTF8() above but without the zTerm parameter. ** For this routine, we assume the UTF8 string is always zero-terminated. */ v2 = pz v1 = *(*uintptr)(unsafe.Pointer(v2)) *(*uintptr)(unsafe.Pointer(v2))++ c = uint32(*(*uint8)(unsafe.Pointer(v1))) if c >= uint32(0xc0) { c = uint32(_sqlite3Utf8Trans1[c-uint32(0xc0)]) for int32(*(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pz)))))&int32(0xc0) == int32(0x80) { v4 = pz v3 = *(*uintptr)(unsafe.Pointer(v4)) *(*uintptr)(unsafe.Pointer(v4))++ c = c<= uint32(0xc0) { c = uint32(_sqlite3Utf8Trans1[c-uint32(0xc0)]) if n > int32(4) { n = int32(4) } for i < n && int32(*(*Tu8)(unsafe.Pointer(z + uintptr(i))))&int32(0xc0) == int32(0x80) { c = c< UTF-16 Little-endian */ for zIn < zTerm { v2 = zIn zIn++ c = uint32(*(*uint8)(unsafe.Pointer(v2))) if c >= uint32(0xc0) { c = uint32(_sqlite3Utf8Trans1[c-uint32(0xc0)]) for zIn != zTerm && int32(*(*uint8)(unsafe.Pointer(zIn)))&int32(0xc0) == int32(0x80) { v3 = zIn zIn++ c = c<> libc.Int32FromInt32(8) & libc.Uint32FromInt32(0x00FF)) } else { v6 = z z++ *(*uint8)(unsafe.Pointer(v6)) = uint8(c>>libc.Int32FromInt32(10)&libc.Uint32FromInt32(0x003F) + (c-libc.Uint32FromInt32(0x10000))>>libc.Int32FromInt32(10)&libc.Uint32FromInt32(0x00C0)) v7 = z z++ *(*uint8)(unsafe.Pointer(v7)) = uint8(libc.Uint32FromInt32(0x00D8) + (c-libc.Uint32FromInt32(0x10000))>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x03)) v8 = z z++ *(*uint8)(unsafe.Pointer(v8)) = uint8(c & libc.Uint32FromInt32(0x00FF)) v9 = z z++ *(*uint8)(unsafe.Pointer(v9)) = uint8(libc.Uint32FromInt32(0x00DC) + c>>libc.Int32FromInt32(8)&libc.Uint32FromInt32(0x03)) } } } else { _ = libc.Int32FromInt32(0) /* UTF-8 -> UTF-16 Big-endian */ for zIn < zTerm { v10 = zIn zIn++ c = uint32(*(*uint8)(unsafe.Pointer(v10))) if c >= uint32(0xc0) { c = uint32(_sqlite3Utf8Trans1[c-uint32(0xc0)]) for zIn != zTerm && int32(*(*uint8)(unsafe.Pointer(zIn)))&int32(0xc0) == int32(0x80) { v11 = zIn zIn++ c = c<> libc.Int32FromInt32(8) & libc.Uint32FromInt32(0x00FF)) v13 = z z++ *(*uint8)(unsafe.Pointer(v13)) = uint8(c & libc.Uint32FromInt32(0x00FF)) } else { v14 = z z++ *(*uint8)(unsafe.Pointer(v14)) = uint8(libc.Uint32FromInt32(0x00D8) + (c-libc.Uint32FromInt32(0x10000))>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x03)) v15 = z z++ *(*uint8)(unsafe.Pointer(v15)) = uint8(c>>libc.Int32FromInt32(10)&libc.Uint32FromInt32(0x003F) + (c-libc.Uint32FromInt32(0x10000))>>libc.Int32FromInt32(10)&libc.Uint32FromInt32(0x00C0)) v16 = z z++ *(*uint8)(unsafe.Pointer(v16)) = uint8(libc.Uint32FromInt32(0x00DC) + c>>libc.Int32FromInt32(8)&libc.Uint32FromInt32(0x03)) v17 = z z++ *(*uint8)(unsafe.Pointer(v17)) = uint8(c & libc.Uint32FromInt32(0x00FF)) } } } (*TMem)(unsafe.Pointer(pMem)).Fn = int32(z) - int32(zOut) v18 = z z++ *(*uint8)(unsafe.Pointer(v18)) = uint8(0) } else { _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pMem)).Fenc) == int32(SQLITE_UTF16LE) { /* UTF-16 Little-endian -> UTF-8 */ for zIn < zTerm { v19 = zIn zIn++ c = uint32(*(*uint8)(unsafe.Pointer(v19))) v20 = zIn zIn++ c += uint32(int32(*(*uint8)(unsafe.Pointer(v20))) << int32(8)) if c >= uint32(0xd800) && c < uint32(0xe000) { if zIn < zTerm { v21 = zIn zIn++ c2 = int32(*(*uint8)(unsafe.Pointer(v21))) v22 = zIn zIn++ c2 += int32(*(*uint8)(unsafe.Pointer(v22))) << int32(8) c = uint32(c2&libc.Int32FromInt32(0x03FF)) + c&uint32(0x003F)<>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x1F)))) v25 = z z++ *(*uint8)(unsafe.Pointer(v25)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F)))) } else { if c < uint32(0x10000) { v26 = z z++ *(*uint8)(unsafe.Pointer(v26)) = uint8(int32(0xE0) + int32(uint8(c>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x0F)))) v27 = z z++ *(*uint8)(unsafe.Pointer(v27)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F)))) v28 = z z++ *(*uint8)(unsafe.Pointer(v28)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F)))) } else { v29 = z z++ *(*uint8)(unsafe.Pointer(v29)) = uint8(int32(0xF0) + int32(uint8(c>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x07)))) v30 = z z++ *(*uint8)(unsafe.Pointer(v30)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x3F)))) v31 = z z++ *(*uint8)(unsafe.Pointer(v31)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F)))) v32 = z z++ *(*uint8)(unsafe.Pointer(v32)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F)))) } } } } } else { /* UTF-16 Big-endian -> UTF-8 */ for zIn < zTerm { v33 = zIn zIn++ c = uint32(int32(*(*uint8)(unsafe.Pointer(v33))) << int32(8)) v34 = zIn zIn++ c += uint32(*(*uint8)(unsafe.Pointer(v34))) if c >= uint32(0xd800) && c < uint32(0xe000) { if zIn < zTerm { v35 = zIn zIn++ c21 = int32(*(*uint8)(unsafe.Pointer(v35))) << int32(8) v36 = zIn zIn++ c21 += int32(*(*uint8)(unsafe.Pointer(v36))) c = uint32(c21&libc.Int32FromInt32(0x03FF)) + c&uint32(0x003F)<>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x1F)))) v39 = z z++ *(*uint8)(unsafe.Pointer(v39)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F)))) } else { if c < uint32(0x10000) { v40 = z z++ *(*uint8)(unsafe.Pointer(v40)) = uint8(int32(0xE0) + int32(uint8(c>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x0F)))) v41 = z z++ *(*uint8)(unsafe.Pointer(v41)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F)))) v42 = z z++ *(*uint8)(unsafe.Pointer(v42)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F)))) } else { v43 = z z++ *(*uint8)(unsafe.Pointer(v43)) = uint8(int32(0xF0) + int32(uint8(c>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x07)))) v44 = z z++ *(*uint8)(unsafe.Pointer(v44)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x3F)))) v45 = z z++ *(*uint8)(unsafe.Pointer(v45)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F)))) v46 = z z++ *(*uint8)(unsafe.Pointer(v46)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F)))) } } } } } (*TMem)(unsafe.Pointer(pMem)).Fn = int32(z) - int32(zOut) } *(*uint8)(unsafe.Pointer(z)) = uint8(0) _ = libc.Int32FromInt32(0) c = uint32(libc.Int32FromInt32(MEM_Str) | libc.Int32FromInt32(MEM_Term) | int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_AffMask)|libc.Int32FromInt32(MEM_Subtype))) _sqlite3VdbeMemRelease(tls, pMem) (*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(c) (*TMem)(unsafe.Pointer(pMem)).Fenc = desiredEnc (*TMem)(unsafe.Pointer(pMem)).Fz = zOut (*TMem)(unsafe.Pointer(pMem)).FzMalloc = (*TMem)(unsafe.Pointer(pMem)).Fz (*TMem)(unsafe.Pointer(pMem)).FszMalloc = _sqlite3DbMallocSize(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).Fz) goto translate_out translate_out: ; return SQLITE_OK return r } // C documentation // // /* // ** This routine checks for a byte-order mark at the beginning of the // ** UTF-16 string stored in *pMem. If one is present, it is removed and // ** the encoding of the Mem adjusted. This routine does not do any // ** byte-swapping, it just sets Mem.enc appropriately. // ** // ** The allocation (static, dynamic etc.) and encoding of the Mem may be // ** changed by this function. // */ func _sqlite3VdbeMemHandleBom(tls *libc.TLS, pMem uintptr) (r int32) { var b1, b2, bom Tu8 var rc int32 var p1 uintptr _, _, _, _, _ = b1, b2, bom, rc, p1 rc = SQLITE_OK bom = uint8(0) _ = libc.Int32FromInt32(0) if (*TMem)(unsafe.Pointer(pMem)).Fn > int32(1) { b1 = *(*Tu8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz)) b2 = *(*Tu8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + libc.UintptrFromInt32(1))) if int32(b1) == int32(0xFE) && int32(b2) == int32(0xFF) { bom = uint8(SQLITE_UTF16BE) } if int32(b1) == int32(0xFF) && int32(b2) == int32(0xFE) { bom = uint8(SQLITE_UTF16LE) } } if bom != 0 { rc = _sqlite3VdbeMemMakeWriteable(tls, pMem) if rc == SQLITE_OK { *(*int32)(unsafe.Pointer(pMem + 12)) -= int32(2) libc.Xmemmove(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, (*TMem)(unsafe.Pointer(pMem)).Fz+2, uint32((*TMem)(unsafe.Pointer(pMem)).Fn)) *(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn))) = uint8('\000') *(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn+int32(1)))) = uint8('\000') p1 = pMem + 16 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(MEM_Term)) (*TMem)(unsafe.Pointer(pMem)).Fenc = bom } } return rc } // C documentation // // /* // ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero, // ** return the number of unicode characters in pZ up to (but not including) // ** the first 0x00 byte. If nByte is not less than zero, return the // ** number of unicode characters in the first nByte of pZ (or up to // ** the first 0x00, whichever comes first). // */ func _sqlite3Utf8CharLen(tls *libc.TLS, zIn uintptr, nByte int32) (r1 int32) { var r int32 var z, zTerm, v1 uintptr _, _, _, _ = r, z, zTerm, v1 r = 0 z = zIn if nByte >= 0 { zTerm = z + uintptr(nByte) } else { zTerm = uintptr(-libc.Int32FromInt32(1)) } _ = libc.Int32FromInt32(0) for int32(*(*Tu8)(unsafe.Pointer(z))) != 0 && z < zTerm { v1 = z z++ if int32(*(*Tu8)(unsafe.Pointer(v1))) >= int32(0xc0) { for int32(*(*Tu8)(unsafe.Pointer(z)))&int32(0xc0) == int32(0x80) { z++ } } r++ } return r } /* This test function is not currently used by the automated test-suite. ** Hence it is only available in debug builds. */ // C documentation // // /* // ** Convert a UTF-16 string in the native encoding into a UTF-8 string. // ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must // ** be freed by the calling function. // ** // ** NULL is returned if there is an allocation error. // */ func _sqlite3Utf16to8(tls *libc.TLS, db uintptr, z uintptr, nByte int32, enc Tu8) (r uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* m at bp+0 */ TMem libc.Xmemset(tls, bp, 0, uint32(40)) (*(*TMem)(unsafe.Pointer(bp))).Fdb = db _sqlite3VdbeMemSetStr(tls, bp, z, int64(nByte), enc, libc.UintptrFromInt32(0)) _sqlite3VdbeChangeEncoding(tls, bp, int32(SQLITE_UTF8)) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _sqlite3VdbeMemRelease(tls, bp) (*(*TMem)(unsafe.Pointer(bp))).Fz = uintptr(0) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return (*(*TMem)(unsafe.Pointer(bp))).Fz } // C documentation // // /* // ** zIn is a UTF-16 encoded unicode string at least nChar characters long. // ** Return the number of bytes in the first nChar unicode characters // ** in pZ. nChar must be non-negative. // */ func _sqlite3Utf16ByteLen(tls *libc.TLS, zIn uintptr, nChar int32) (r int32) { var c, n int32 var z uintptr _, _, _ = c, n, z z = zIn n = 0 if true { z++ } for n < nChar { c = int32(*(*uint8)(unsafe.Pointer(z))) z += uintptr(2) if c >= int32(0xd8) && c < int32(0xdc) && int32(*(*uint8)(unsafe.Pointer(z))) >= int32(0xdc) && int32(*(*uint8)(unsafe.Pointer(z))) < int32(0xe0) { z += uintptr(2) } n++ } return int32(z) - int32(zIn) - libc.BoolInt32(true) } type Tfloat_t = float32 type float_t = Tfloat_t type Tdouble_t = float64 type double_t = Tdouble_t const _FP_INT_UPWARD = 0 const _FP_INT_DOWNWARD = 1 const _FP_INT_TOWARDZERO = 2 const _FP_INT_TONEARESTFROMZERO = 3 const _FP_INT_TONEAREST = 4 const _FP_NAN = 0 const _FP_INFINITE = 1 const _FP_ZERO = 2 const _FP_SUBNORMAL = 3 const _FP_NORMAL = 4 /* GCC bug 66462 means we cannot use the math builtins with -fsignaling-nan, so disable builtins if this is enabled. When fixed in a newer GCC, the __SUPPORT_SNAN__ check may be skipped for those versions. */ /* Return number of classification appropriate for X. */ /* The check for __cplusplus allows the use of the builtin, even when optimization for size is on. This is provided for libstdc++, only to let its configure test work when it is built with -Os. No further use of this definition of fpclassify is expected in C++ mode, since libstdc++ provides its own version of fpclassify in cmath (which undefines fpclassify). */ /* Return nonzero value if sign of X is negative. */ /* Return nonzero value if X is not +-Inf or NaN. */ /* Return nonzero value if X is neither zero, subnormal, Inf, nor NaN. */ /* Return nonzero value if X is a NaN. We could use `fpclassify' but we already have this functions `__isnan' and it is faster. */ /* Return nonzero value if X is positive or negative infinity. */ /* Bitmasks for the math_errhandling macro. */ /* By default all math functions support both errno and exception handling (except for soft floating point implementations which may only support errno handling). If errno handling is disabled, exceptions are still supported by GLIBC. Set math_errhandling to 0 with -ffast-math (this is nonconforming but it is more useful than leaving it undefined). */ /* Define iscanonical macro. Copyright (C) 2016-2022 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, see . */ /* Return nonzero value if X is canonical. By default, we only have IEEE interchange binary formats, in which all values are canonical, but the argument must still be converted to its semantic type for any exceptions arising from the conversion, before being discarded. */ /* Return nonzero value if X is a signaling NaN. */ /* Return nonzero value if X is subnormal. */ /* Return nonzero value if X is zero. */ /* X/Open wants another strange constant. */ /* Some useful constants. */ /* GNU extension to provide float constants with similar names. */ /* The above constants are not adequate for computation using `long double's. Therefore we provide as an extension constants with similar names as a GNU extension. Provide enough digits for the 128-bit IEEE quad. */ /* ISO C99 defines some macros to compare number while taking care for unordered numbers. Many FPUs provide special instructions to support these operations. Generic support in GCC for these as builtins went in 2.97, but not all cpus added their patterns until 3.1. Therefore we enable the builtins from 3.1 onwards and use a generic implementation othwerwise. */ /* An expression whose type has the widest of the evaluation formats of X and Y (which are of floating-point types). */ /* Return X == Y but raising "invalid" and setting errno if X or Y is a NaN. */ // C documentation // // /* // ** Calls to sqlite3FaultSim() are used to simulate a failure during testing, // ** or to bypass normal error detection during testing in order to let // ** execute proceed further downstream. // ** // ** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The // ** sqlite3FaultSim() function only returns non-zero during testing. // ** // ** During testing, if the test harness has set a fault-sim callback using // ** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then // ** each call to sqlite3FaultSim() is relayed to that application-supplied // ** callback and the integer return value form the application-supplied // ** callback is returned by sqlite3FaultSim(). // ** // ** The integer argument to sqlite3FaultSim() is a code to identify which // ** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim() // ** should have a unique code. To prevent legacy testing applications from // ** breaking, the codes should not be changed or reused. // */ func _sqlite3FaultSim(tls *libc.TLS, iTest int32) (r int32) { var xCallback uintptr var v1 int32 _, _ = xCallback, v1 xCallback = _sqlite3Config.FxTestCallback if xCallback != 0 { v1 = (*(*func(*libc.TLS, int32) int32)(unsafe.Pointer(&struct{ uintptr }{xCallback})))(tls, iTest) } else { v1 = SQLITE_OK } return v1 } // C documentation // // /* // ** Return true if the floating point value is Not a Number (NaN). // ** // ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN. // ** Otherwise, we have our own implementation that works on most systems. // */ func _sqlite3IsNaN(tls *libc.TLS, _x float64) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) *(*float64)(unsafe.Pointer(bp)) = _x var rc int32 var _ /* y at bp+8 */ Tu64 _ = rc libc.Xmemcpy(tls, bp+8, bp, uint32(8)) rc = libc.BoolInt32(*(*Tu64)(unsafe.Pointer(bp + 8))&(libc.Uint64FromInt32(0x7ff)<>4)) != 0 { _ = libc.Int32FromInt32(0) return _sqlite3StdType[int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf0>>4))-int32(1)] } else { return zDflt } } return r } // C documentation // // /* // ** Helper function for sqlite3Error() - called rarely. Broken out into // ** a separate routine to avoid unnecessary register saves on entry to // ** sqlite3Error(). // */ func _sqlite3ErrorFinish(tls *libc.TLS, db uintptr, err_code int32) { if (*Tsqlite3)(unsafe.Pointer(db)).FpErr != 0 { _sqlite3ValueSetNull(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpErr) } _sqlite3SystemError(tls, db, err_code) } // C documentation // // /* // ** Set the current error code to err_code and clear any prior error message. // ** Also set iSysErrno (by calling sqlite3System) if the err_code indicates // ** that would be appropriate. // */ func _sqlite3Error(tls *libc.TLS, db uintptr, err_code int32) { _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).FerrCode = err_code if err_code != 0 || (*Tsqlite3)(unsafe.Pointer(db)).FpErr != 0 { _sqlite3ErrorFinish(tls, db, err_code) } else { (*Tsqlite3)(unsafe.Pointer(db)).FerrByteOffset = -int32(1) } } // C documentation // // /* // ** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state // ** and error message. // */ func _sqlite3ErrorClear(tls *libc.TLS, db uintptr) { _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).FerrCode = SQLITE_OK (*Tsqlite3)(unsafe.Pointer(db)).FerrByteOffset = -int32(1) if (*Tsqlite3)(unsafe.Pointer(db)).FpErr != 0 { _sqlite3ValueSetNull(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpErr) } } // C documentation // // /* // ** Load the sqlite3.iSysErrno field if that is an appropriate thing // ** to do based on the SQLite error code in rc. // */ func _sqlite3SystemError(tls *libc.TLS, db uintptr, rc int32) { if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<= (*Tsqlite3)(unsafe.Pointer(db)).FnProgressOps { if (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxProgress})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpProgressArg) != 0 { (*TParse)(unsafe.Pointer(p)).FnErr++ (*TParse)(unsafe.Pointer(p)).Frc = int32(SQLITE_INTERRUPT) } (*TParse)(unsafe.Pointer(p)).FnProgressSteps = uint32(0) } } } } // C documentation // // /* // ** Add an error message to pParse->zErrMsg and increment pParse->nErr. // ** // ** This function should be used to report any error that occurs while // ** compiling an SQL statement (i.e. within sqlite3_prepare()). The // ** last thing the sqlite3_prepare() function does is copy the error // ** stored by this function into the database handle using sqlite3Error(). // ** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used // ** during statement execution (sqlite3_step() etc.). // */ func _sqlite3ErrorMsg(tls *libc.TLS, pParse uintptr, zFormat uintptr, va uintptr) { var ap Tva_list var db, zMsg uintptr _, _, _ = ap, db, zMsg db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).FerrByteOffset = -int32(2) ap = va zMsg = _sqlite3VMPrintf(tls, db, zFormat, ap) _ = ap if (*Tsqlite3)(unsafe.Pointer(db)).FerrByteOffset < -int32(1) { (*Tsqlite3)(unsafe.Pointer(db)).FerrByteOffset = -int32(1) } if (*Tsqlite3)(unsafe.Pointer(db)).FsuppressErr != 0 { _sqlite3DbFree(tls, db, zMsg) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { (*TParse)(unsafe.Pointer(pParse)).FnErr++ (*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM) } } else { (*TParse)(unsafe.Pointer(pParse)).FnErr++ _sqlite3DbFree(tls, db, (*TParse)(unsafe.Pointer(pParse)).FzErrMsg) (*TParse)(unsafe.Pointer(pParse)).FzErrMsg = zMsg (*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_ERROR) (*TParse)(unsafe.Pointer(pParse)).FpWith = uintptr(0) } } // C documentation // // /* // ** If database connection db is currently parsing SQL, then transfer // ** error code errCode to that parser if the parser has not already // ** encountered some other kind of error. // */ func _sqlite3ErrorToParser(tls *libc.TLS, db uintptr, errCode int32) (r int32) { var pParse, v1 uintptr var v2 bool _, _, _ = pParse, v1, v2 if v2 = db == uintptr(0); !v2 { v1 = (*Tsqlite3)(unsafe.Pointer(db)).FpParse pParse = v1 } if v2 || v1 == uintptr(0) { return errCode } (*TParse)(unsafe.Pointer(pParse)).Frc = errCode (*TParse)(unsafe.Pointer(pParse)).FnErr++ return errCode } // C documentation // // /* // ** Convert an SQL-style quoted string into a normal string by removing // ** the quote characters. The conversion is done in-place. If the // ** input does not begin with a quote character, then this routine // ** is a no-op. // ** // ** The input string must be zero-terminated. A new zero-terminator // ** is added to the dequoted string. // ** // ** The return value is -1 if no dequoting occurs or the length of the // ** dequoted string, exclusive of the zero terminator, if dequoting does // ** occur. // ** // ** 2002-02-14: This routine is extended to remove MS-Access style // ** brackets from around identifiers. For example: "[a-b-c]" becomes // ** "a-b-c". // */ func _sqlite3Dequote(tls *libc.TLS, z uintptr) { var i, j, v2, v3 int32 var quote uint8 _, _, _, _, _ = i, j, quote, v2, v3 if z == uintptr(0) { return } quote = *(*uint8)(unsafe.Pointer(z)) if !(int32(_sqlite3CtypeMap[quote])&libc.Int32FromInt32(0x80) != 0) { return } if int32(quote) == int32('[') { quote = uint8(']') } i = int32(1) j = libc.Int32FromInt32(0) for { _ = libc.Int32FromInt32(0) if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) == int32(quote) { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+int32(1))))) == int32(quote) { v2 = j j++ *(*uint8)(unsafe.Pointer(z + uintptr(v2))) = quote i++ } else { break } } else { v3 = j j++ *(*uint8)(unsafe.Pointer(z + uintptr(v3))) = *(*uint8)(unsafe.Pointer(z + uintptr(i))) } goto _1 _1: ; i++ } *(*uint8)(unsafe.Pointer(z + uintptr(j))) = uint8(0) } func _sqlite3DequoteExpr(tls *libc.TLS, p uintptr) { var v1 int32 _ = v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(*(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 8))))) == int32('"') { v1 = libc.Int32FromInt32(EP_Quoted) | libc.Int32FromInt32(EP_DblQuoted) } else { v1 = int32(EP_Quoted) } *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(v1) _sqlite3Dequote(tls, *(*uintptr)(unsafe.Pointer(p + 8))) } // C documentation // // /* // ** If the input token p is quoted, try to adjust the token to remove // ** the quotes. This is not always possible: // ** // ** "abc" -> abc // ** "ab""cd" -> (not possible because of the interior "") // ** // ** Remove the quotes if possible. This is a optimization. The overall // ** system should still return the correct answer even if this routine // ** is always a no-op. // */ func _sqlite3DequoteToken(tls *libc.TLS, p uintptr) { var i uint32 _ = i if (*TToken)(unsafe.Pointer(p)).Fn < uint32(2) { return } if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer((*TToken)(unsafe.Pointer(p)).Fz))])&libc.Int32FromInt32(0x80) != 0) { return } i = uint32(1) for { if !(i < (*TToken)(unsafe.Pointer(p)).Fn-uint32(1)) { break } if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer((*TToken)(unsafe.Pointer(p)).Fz + uintptr(i)))])&int32(0x80) != 0 { return } goto _1 _1: ; i++ } *(*uint32)(unsafe.Pointer(p + 4)) -= uint32(2) (*TToken)(unsafe.Pointer(p)).Fz++ } // C documentation // // /* // ** Generate a Token object from a string // */ func _sqlite3TokenInit(tls *libc.TLS, p uintptr, z uintptr) { (*TToken)(unsafe.Pointer(p)).Fz = z (*TToken)(unsafe.Pointer(p)).Fn = uint32(_sqlite3Strlen30(tls, z)) } /* Convenient short-hand */ // C documentation // // /* // ** Some systems have stricmp(). Others have strcasecmp(). Because // ** there is no consistency, we will define our own. // ** // ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and // ** sqlite3_strnicmp() APIs allow applications and extensions to compare // ** the contents of two buffers containing UTF-8 strings in a // ** case-independent fashion, using the same definition of "case // ** independence" that SQLite uses internally when comparing identifiers. // */ func Xsqlite3_stricmp(tls *libc.TLS, zLeft uintptr, zRight uintptr) (r int32) { var v1 int32 _ = v1 if zLeft == uintptr(0) { if zRight != 0 { v1 = -int32(1) } else { v1 = 0 } return v1 } else { if zRight == uintptr(0) { return int32(1) } } return _sqlite3StrICmp(tls, zLeft, zRight) } func _sqlite3StrICmp(tls *libc.TLS, zLeft uintptr, zRight uintptr) (r int32) { var a, b uintptr var c, x int32 _, _, _, _ = a, b, c, x a = zLeft b = zRight for { c = int32(*(*uint8)(unsafe.Pointer(a))) x = int32(*(*uint8)(unsafe.Pointer(b))) if c == x { if c == 0 { break } } else { c = int32(_sqlite3UpperToLower[c]) - int32(_sqlite3UpperToLower[x]) if c != 0 { break } } a++ b++ goto _1 _1: } return c } func Xsqlite3_strnicmp(tls *libc.TLS, zLeft uintptr, zRight uintptr, N int32) (r int32) { var a, b uintptr var v1, v2, v3 int32 _, _, _, _, _ = a, b, v1, v2, v3 if zLeft == uintptr(0) { if zRight != 0 { v1 = -int32(1) } else { v1 = 0 } return v1 } else { if zRight == uintptr(0) { return int32(1) } } a = zLeft b = zRight for { v2 = N N-- if !(v2 > 0 && int32(*(*uint8)(unsafe.Pointer(a))) != 0 && int32(_sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(a))]) == int32(_sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(b))])) { break } a++ b++ } if N < 0 { v3 = 0 } else { v3 = int32(_sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(a))]) - int32(_sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(b))]) } return v3 } // C documentation // // /* // ** Compute an 8-bit hash on a string that is insensitive to case differences // */ func _sqlite3StrIHash(tls *libc.TLS, z uintptr) (r Tu8) { var h Tu8 _ = h h = uint8(0) if z == uintptr(0) { return uint8(0) } for *(*uint8)(unsafe.Pointer(z)) != 0 { h = Tu8(int32(h) + int32(_sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(z))])) z++ } return h } // C documentation // // /* Double-Double multiplication. (x[0],x[1]) *= (y,yy) // ** // ** Reference: // ** T. J. Dekker, "A Floating-Point Technique for Extending the // ** Available Precision". 1971-07-26. // */ func _dekkerMul2(tls *libc.TLS, x uintptr, _y float64, yy float64) { bp := tls.Alloc(32) defer tls.Free(32) *(*float64)(unsafe.Pointer(bp)) = _y var c, cc, p, q, tx, ty float64 var _ /* hx at bp+8 */ float64 var _ /* hy at bp+16 */ float64 var _ /* m at bp+24 */ Tu64 _, _, _, _, _, _ = c, cc, p, q, tx, ty libc.Xmemcpy(tls, bp+24, x, uint32(8)) *(*Tu64)(unsafe.Pointer(bp + 24)) &= uint64(0xfffffffffc000000) libc.Xmemcpy(tls, bp+8, bp+24, uint32(8)) tx = libc.AtomicLoadPFloat64(x) - *(*float64)(unsafe.Pointer(bp + 8)) libc.Xmemcpy(tls, bp+24, bp, uint32(8)) *(*Tu64)(unsafe.Pointer(bp + 24)) &= uint64(0xfffffffffc000000) libc.Xmemcpy(tls, bp+16, bp+24, uint32(8)) ty = *(*float64)(unsafe.Pointer(bp)) - *(*float64)(unsafe.Pointer(bp + 16)) p = *(*float64)(unsafe.Pointer(bp + 8)) * *(*float64)(unsafe.Pointer(bp + 16)) q = *(*float64)(unsafe.Pointer(bp + 8))*ty + tx**(*float64)(unsafe.Pointer(bp + 16)) c = p + q cc = p - c + q + tx*ty cc = libc.AtomicLoadPFloat64(x)*yy + libc.AtomicLoadPFloat64(x+1*8)**(*float64)(unsafe.Pointer(bp)) + cc libc.AtomicStorePFloat64(x, c+cc) libc.AtomicStorePFloat64(x+1*8, c-libc.AtomicLoadPFloat64(x)) *(*float64)(unsafe.Pointer(x + 1*8)) += cc } // C documentation // // /* // ** The string z[] is an text representation of a real number. // ** Convert this string to a double and write it into *pResult. // ** // ** The string z[] is length bytes in length (bytes, not characters) and // ** uses the encoding enc. The string is not necessarily zero-terminated. // ** // ** Return TRUE if the result is a valid real number (or integer) and FALSE // ** if the string is empty or contains extraneous text. More specifically // ** return // ** 1 => The input string is a pure integer // ** 2 or more => The input has a decimal point or eNNN clause // ** 0 or less => The input string is not a valid number // ** -1 => Not a valid number, but has a valid prefix which // ** includes a decimal point and/or an eNNN clause // ** // ** Valid numbers are in one of these formats: // ** // ** [+-]digits[E[+-]digits] // ** [+-]digits.[digits][E[+-]digits] // ** [+-].digits[E[+-]digits] // ** // ** Leading and trailing whitespace is ignored for the purpose of determining // ** validity. // ** // ** If some prefix of the input string is a valid number, this routine // ** returns FALSE but it still converts the prefix and writes the result // ** into *pResult. // */ func _sqlite3AtoF(tls *libc.TLS, z uintptr, pResult uintptr, length int32, enc Tu8) (r1 int32) { bp := tls.Alloc(16) defer tls.Free(16) var d, e, eType, eValid, esign, i, incr, nDigit, sign, v2 int32 var r, v3, v4 float64 var s, s2 Tu64 var zEnd uintptr var _ /* rr at bp+0 */ [2]float64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = d, e, eType, eValid, esign, i, incr, nDigit, r, s, s2, sign, zEnd, v2, v3, v4 /* sign * significand * (10 ^ (esign * exponent)) */ sign = int32(1) /* sign of significand */ s = uint64(0) /* significand */ d = 0 /* adjust exponent for shifting decimal point */ esign = int32(1) /* sign of exponent */ e = 0 /* exponent */ eValid = int32(1) /* True exponent is either not used or is well-formed */ nDigit = 0 /* Number of digits processed */ eType = int32(1) /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */ _ = libc.Int32FromInt32(0) *(*float64)(unsafe.Pointer(pResult)) = float64(0) /* Default return value, in case of an error */ if length == 0 { return 0 } if int32(enc) == int32(SQLITE_UTF8) { incr = int32(1) zEnd = z + uintptr(length) } else { incr = int32(2) length &= ^libc.Int32FromInt32(1) _ = libc.Int32FromInt32(0) i = int32(3) - int32(enc) for { if !(i < length && int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) == 0) { break } goto _1 _1: ; i += int32(2) } if i < length { eType = -int32(100) } zEnd = z + uintptr(i^int32(1)) z += uintptr(int32(enc) & libc.Int32FromInt32(1)) } /* skip leading spaces */ for z < zEnd && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z))])&int32(0x01) != 0 { z += uintptr(incr) } if z >= zEnd { return 0 } /* get sign of significand */ if int32(*(*uint8)(unsafe.Pointer(z))) == int32('-') { sign = -int32(1) z += uintptr(incr) } else { if int32(*(*uint8)(unsafe.Pointer(z))) == int32('+') { z += uintptr(incr) } } /* copy max significant digits to significand */ for z < zEnd && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z))])&int32(0x04) != 0 { s = s*uint64(10) + uint64(int32(*(*uint8)(unsafe.Pointer(z)))-libc.Int32FromUint8('0')) z += uintptr(incr) nDigit++ if s >= (libc.Uint64FromUint32(0xffffffff)|libc.Uint64FromUint32(0xffffffff)<= zEnd { goto do_atof_calc } /* if decimal point is present */ if int32(*(*uint8)(unsafe.Pointer(z))) == int32('.') { z += uintptr(incr) eType++ /* copy digits from after decimal to significand ** (decrease exponent by d to shift decimal right) */ for z < zEnd && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z))])&int32(0x04) != 0 { if s < (libc.Uint64FromUint32(0xffffffff)|libc.Uint64FromUint32(0xffffffff)<= zEnd { goto do_atof_calc } /* if exponent is present */ if int32(*(*uint8)(unsafe.Pointer(z))) == int32('e') || int32(*(*uint8)(unsafe.Pointer(z))) == int32('E') { z += uintptr(incr) eValid = 0 eType++ /* This branch is needed to avoid a (harmless) buffer overread. The ** special comment alerts the mutation tester that the correct answer ** is obtained even if the branch is omitted */ if z >= zEnd { goto do_atof_calc } /*PREVENTS-HARMLESS-OVERREAD*/ /* get sign of exponent */ if int32(*(*uint8)(unsafe.Pointer(z))) == int32('-') { esign = -int32(1) z += uintptr(incr) } else { if int32(*(*uint8)(unsafe.Pointer(z))) == int32('+') { z += uintptr(incr) } } /* copy digits to exponent */ for z < zEnd && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z))])&int32(0x04) != 0 { if e < int32(10000) { v2 = e*int32(10) + (int32(*(*uint8)(unsafe.Pointer(z))) - int32('0')) } else { v2 = int32(10000) } e = v2 z += uintptr(incr) eValid = int32(1) } } /* skip trailing spaces */ for z < zEnd && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z))])&int32(0x01) != 0 { z += uintptr(incr) } goto do_atof_calc do_atof_calc: ; /* Zero is a special case */ if s == uint64(0) { if sign < 0 { v3 = -libc.Float64FromFloat64(0) } else { v3 = +libc.Float64FromFloat64(0) } *(*float64)(unsafe.Pointer(pResult)) = v3 goto atof_return } /* adjust exponent by d, and update sign */ e = e*esign + d /* Try to adjust the exponent to make it smaller */ for e > 0 && s < (libc.Uint64FromUint32(0xffffffff)|libc.Uint64FromUint32(0xffffffff)< 0 { for e >= int32(100) { e -= int32(100) r = r * libc.Float64FromFloat64(1e+100) } for e >= int32(10) { e -= int32(10) r = r * libc.Float64FromFloat64(1e+10) } for e >= int32(1) { e -= int32(1) r = r * libc.Float64FromFloat64(10) } } else { for e <= -int32(100) { e += int32(100) r = r * libc.Float64FromFloat64(1e-100) } for e <= -int32(10) { e += int32(10) r = r * libc.Float64FromFloat64(1e-10) } for e <= -int32(1) { e += int32(1) r = r * libc.Float64FromFloat64(0.1) } } _ = libc.Int32FromInt32(0) if r > +libc.Float64FromFloat64(1.7976931348623157081452742373e+308) { *(*float64)(unsafe.Pointer(pResult)) = float64(+libc.X__builtin_inff(tls)) } else { *(*float64)(unsafe.Pointer(pResult)) = r } } else { (*(*[2]float64)(unsafe.Pointer(bp)))[0] = float64(s) s2 = uint64((*(*[2]float64)(unsafe.Pointer(bp)))[0]) if s >= s2 { v4 = float64(s - s2) } else { v4 = -float64(s2 - s) } (*(*[2]float64)(unsafe.Pointer(bp)))[int32(1)] = v4 if e > 0 { for e >= int32(100) { e -= int32(100) _dekkerMul2(tls, bp, float64(1e+100), -libc.Float64FromFloat64(1.5902891109759918e+83)) } for e >= int32(10) { e -= int32(10) _dekkerMul2(tls, bp, float64(1e+10), float64(0)) } for e >= int32(1) { e -= int32(1) _dekkerMul2(tls, bp, float64(10), float64(0)) } } else { for e <= -int32(100) { e += int32(100) _dekkerMul2(tls, bp, float64(1e-100), -libc.Float64FromFloat64(1.9991899802602883e-117)) } for e <= -int32(10) { e += int32(10) _dekkerMul2(tls, bp, float64(1e-10), -libc.Float64FromFloat64(3.643219731549774e-27)) } for e <= -int32(1) { e += int32(1) _dekkerMul2(tls, bp, float64(0.1), -libc.Float64FromFloat64(5.551115123125783e-18)) } } *(*float64)(unsafe.Pointer(pResult)) = (*(*[2]float64)(unsafe.Pointer(bp)))[0] + (*(*[2]float64)(unsafe.Pointer(bp)))[int32(1)] if _sqlite3IsNaN(tls, *(*float64)(unsafe.Pointer(pResult))) != 0 { *(*float64)(unsafe.Pointer(pResult)) = libc.Float64FromFloat64(1e+300) * libc.Float64FromFloat64(1e+300) } } } if sign < 0 { *(*float64)(unsafe.Pointer(pResult)) = -*(*float64)(unsafe.Pointer(pResult)) } _ = libc.Int32FromInt32(0) goto atof_return atof_return: ; /* return true if number and no extra non-whitespace characters after */ if z == zEnd && nDigit > 0 && eValid != 0 && eType > 0 { return eType } else { if eType >= int32(2) && (eType == int32(3) || eValid != 0) && nDigit > 0 { return -int32(1) } else { return 0 } } return r1 } // C documentation // // /* // ** Render an signed 64-bit integer as text. Store the result in zOut[] and // ** return the length of the string that was stored, in bytes. The value // ** returned does not include the zero terminator at the end of the output // ** string. // ** // ** The caller must ensure that zOut[] is at least 21 bytes in size. // */ func _sqlite3Int64ToText(tls *libc.TLS, v Ti64, zOut uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var i, v2 int32 var x Tu64 var v1 uint64 var _ /* zTemp at bp+0 */ [22]uint8 _, _, _, _ = i, x, v1, v2 if v < 0 { if v == int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<= int32('0') && c <= int32('9')) { break } u = u*uint64(10) + uint64(c) - uint64('0') goto _2 _2: ; i += incr } if u > uint64(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)< int32(19)*incr { v6 = int32(1) } else { v6 = _compare2pow63(tls, zNum, incr) } c = v6 if c < 0 { /* zNum is less than 9223372036854775808 so it fits */ _ = libc.Int32FromInt32(0) return rc } else { if neg != 0 { v7 = int64(-libc.Int32FromInt32(1)) - (libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)< 0 { /* zNum is greater than 9223372036854775808 so it overflows */ return int32(2) } else { /* zNum is exactly 9223372036854775808. Fits if negative. The ** special case 2 overflow if positive */ _ = libc.Int32FromInt32(0) if neg != 0 { v8 = rc } else { v8 = int32(3) } return v8 } } } return r } // C documentation // // /* // ** Transform a UTF-8 integer literal, in either decimal or hexadecimal, // ** into a 64-bit signed integer. This routine accepts hexadecimal literals, // ** whereas sqlite3Atoi64() does not. // ** // ** Returns: // ** // ** 0 Successful transformation. Fits in a 64-bit signed integer. // ** 1 Excess text after the integer value // ** 2 Integer too large for a 64-bit signed integer or is malformed // ** 3 Special case of 9223372036854775808 // */ func _sqlite3DecOrHexToI64(tls *libc.TLS, z uintptr, pOut uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, k, n int32 var _ /* u at bp+0 */ Tu64 _, _, _ = i, k, n if int32(*(*uint8)(unsafe.Pointer(z))) == int32('0') && (int32(*(*uint8)(unsafe.Pointer(z + 1))) == int32('x') || int32(*(*uint8)(unsafe.Pointer(z + 1))) == int32('X')) { *(*Tu64)(unsafe.Pointer(bp)) = uint64(0) i = int32(2) for { if !(int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) == int32('0')) { break } goto _1 _1: ; i++ } k = i for { if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(k)))])&int32(0x08) != 0) { break } *(*Tu64)(unsafe.Pointer(bp)) = *(*Tu64)(unsafe.Pointer(bp))*uint64(16) + uint64(_sqlite3HexToInt(tls, int32(*(*uint8)(unsafe.Pointer(z + uintptr(k)))))) goto _2 _2: ; k++ } libc.Xmemcpy(tls, pOut, bp, uint32(8)) if k-i > int32(16) { return int32(2) } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(k)))) != 0 { return int32(1) } return 0 } else { n = int32(libc.Uint32FromInt32(0x3fffffff) & libc.Xstrspn(tls, z, __ccgo_ts+1706)) if *(*uint8)(unsafe.Pointer(z + uintptr(n))) != 0 { n++ } return _sqlite3Atoi64(tls, z, pOut, n, uint8(SQLITE_UTF8)) } return r } // C documentation // // /* // ** If zNum represents an integer that will fit in 32-bits, then set // ** *pValue to that integer and return true. Otherwise return false. // ** // ** This routine accepts both decimal and hexadecimal notation for integers. // ** // ** Any non-numeric characters that following zNum are ignored. // ** This is different from sqlite3Atoi64() which requires the // ** input number to be zero-terminated. // */ func _sqlite3GetInt32(tls *libc.TLS, zNum uintptr, pValue uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var c, i, neg, v3 int32 var v Tsqlite_int64 var v4 bool var _ /* u at bp+0 */ Tu32 _, _, _, _, _, _ = c, i, neg, v, v3, v4 v = 0 neg = 0 if int32(*(*uint8)(unsafe.Pointer(zNum))) == int32('-') { neg = int32(1) zNum++ } else { if int32(*(*uint8)(unsafe.Pointer(zNum))) == int32('+') { zNum++ } else { if int32(*(*uint8)(unsafe.Pointer(zNum))) == int32('0') && (int32(*(*uint8)(unsafe.Pointer(zNum + 1))) == int32('x') || int32(*(*uint8)(unsafe.Pointer(zNum + 1))) == int32('X')) && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zNum + 2))])&int32(0x08) != 0 { *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) zNum += uintptr(2) for int32(*(*uint8)(unsafe.Pointer(zNum))) == int32('0') { zNum++ } i = 0 for { if !(i < int32(8) && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zNum + uintptr(i)))])&int32(0x08) != 0) { break } *(*Tu32)(unsafe.Pointer(bp)) = *(*Tu32)(unsafe.Pointer(bp))*uint32(16) + uint32(_sqlite3HexToInt(tls, int32(*(*uint8)(unsafe.Pointer(zNum + uintptr(i)))))) goto _1 _1: ; i++ } if *(*Tu32)(unsafe.Pointer(bp))&uint32(0x80000000) == uint32(0) && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zNum + uintptr(i)))])&int32(0x08) == 0 { libc.Xmemcpy(tls, pValue, bp, uint32(4)) return int32(1) } else { return 0 } } } } if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zNum))])&libc.Int32FromInt32(0x04) != 0) { return 0 } for int32(*(*uint8)(unsafe.Pointer(zNum))) == int32('0') { zNum++ } i = 0 for { if v4 = i < int32(11); v4 { v3 = int32(*(*uint8)(unsafe.Pointer(zNum + uintptr(i)))) - libc.Int32FromUint8('0') c = v3 } if !(v4 && v3 >= 0 && c <= int32(9)) { break } v = v*int64(10) + int64(c) goto _2 _2: ; i++ } /* The longest decimal representation of a 32 bit integer is 10 digits: ** ** 1234567890 ** 2^31 -> 2147483648 */ if i > int32(10) { return 0 } if v-int64(neg) > int64(2147483647) { return 0 } if neg != 0 { v = -v } *(*int32)(unsafe.Pointer(pValue)) = int32(v) return int32(1) } // C documentation // // /* // ** Return a 32-bit integer value extracted from a string. If the // ** string is not an integer, just return 0. // */ func _sqlite3Atoi(tls *libc.TLS, z uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* x at bp+0 */ int32 *(*int32)(unsafe.Pointer(bp)) = 0 _sqlite3GetInt32(tls, z, bp) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Decode a floating-point value into an approximate decimal // ** representation. // ** // ** Round the decimal representation to n significant digits if // ** n is positive. Or round to -n signficant digits after the // ** decimal point if n is negative. No rounding is performed if // ** n is zero. // ** // ** The significant digits of the decimal representation are // ** stored in p->z[] which is a often (but not always) a pointer // ** into the middle of p->zBuf[]. There are p->n significant digits. // ** The p->z[] array is *not* zero-terminated. // */ func _sqlite3FpDecode(tls *libc.TLS, p uintptr, _r float64, iRound int32, mxRound int32) { bp := tls.Alloc(32) defer tls.Free(32) *(*float64)(unsafe.Pointer(bp)) = _r var e, exp, i, j, v2, v3, v4 int32 var rr float64 var z uintptr var v1 uint64 var _ /* rr at bp+16 */ [2]float64 var _ /* v at bp+8 */ Tu64 _, _, _, _, _, _, _, _, _, _ = e, exp, i, j, rr, z, v1, v2, v3, v4 exp = 0 (*TFpDecode)(unsafe.Pointer(p)).FisSpecial = uint8(0) (*TFpDecode)(unsafe.Pointer(p)).Fz = p + 16 /* Convert negative numbers to positive. Deal with Infinity, 0.0, and ** NaN. */ if *(*float64)(unsafe.Pointer(bp)) < float64(0) { (*TFpDecode)(unsafe.Pointer(p)).Fsign = uint8('-') *(*float64)(unsafe.Pointer(bp)) = -*(*float64)(unsafe.Pointer(bp)) } else { if *(*float64)(unsafe.Pointer(bp)) == float64(0) { (*TFpDecode)(unsafe.Pointer(p)).Fsign = uint8('+') (*TFpDecode)(unsafe.Pointer(p)).Fn = int32(1) (*TFpDecode)(unsafe.Pointer(p)).FiDP = int32(1) (*TFpDecode)(unsafe.Pointer(p)).Fz = __ccgo_ts + 1722 return } else { (*TFpDecode)(unsafe.Pointer(p)).Fsign = uint8('+') } } libc.Xmemcpy(tls, bp+8, bp, uint32(8)) e = int32(*(*Tu64)(unsafe.Pointer(bp + 8)) >> int32(52)) if e&int32(0x7ff) == int32(0x7ff) { (*TFpDecode)(unsafe.Pointer(p)).FisSpecial = uint8(int32(1) + libc.BoolInt32(*(*Tu64)(unsafe.Pointer(bp + 8)) != uint64(0x7ff0000000000000))) (*TFpDecode)(unsafe.Pointer(p)).Fn = 0 (*TFpDecode)(unsafe.Pointer(p)).FiDP = 0 return } /* Multiply r by powers of ten until it lands somewhere in between ** 1.0e+19 and 1.0e+17. */ if _sqlite3Config.FbUseLongDouble != 0 { rr = *(*float64)(unsafe.Pointer(bp)) if rr >= float64(1e+19) { for rr >= libc.Float64FromFloat64(1e+119) { exp += int32(100) rr = rr * libc.Float64FromFloat64(1e-100) } for rr >= libc.Float64FromFloat64(1e+29) { exp += int32(10) rr = rr * libc.Float64FromFloat64(1e-10) } for rr >= libc.Float64FromFloat64(1e+19) { exp++ rr = rr * libc.Float64FromFloat64(0.1) } } else { for rr < libc.Float64FromFloat64(1e-97) { exp -= int32(100) rr = rr * libc.Float64FromFloat64(1e+100) } for rr < libc.Float64FromFloat64(1e+07) { exp -= int32(10) rr = rr * libc.Float64FromFloat64(1e+10) } for rr < libc.Float64FromFloat64(1e+17) { exp-- rr = rr * libc.Float64FromFloat64(10) } } *(*Tu64)(unsafe.Pointer(bp + 8)) = uint64(rr) } else { (*(*[2]float64)(unsafe.Pointer(bp + 16)))[0] = *(*float64)(unsafe.Pointer(bp)) (*(*[2]float64)(unsafe.Pointer(bp + 16)))[int32(1)] = float64(0) if (*(*[2]float64)(unsafe.Pointer(bp + 16)))[0] > float64(9.223372036854775e+18) { for (*(*[2]float64)(unsafe.Pointer(bp + 16)))[0] > float64(9.223372036854774e+118) { exp += int32(100) _dekkerMul2(tls, bp+16, float64(1e-100), -libc.Float64FromFloat64(1.9991899802602883e-117)) } for (*(*[2]float64)(unsafe.Pointer(bp + 16)))[0] > float64(9.223372036854774e+28) { exp += int32(10) _dekkerMul2(tls, bp+16, float64(1e-10), -libc.Float64FromFloat64(3.643219731549774e-27)) } for (*(*[2]float64)(unsafe.Pointer(bp + 16)))[0] > float64(9.223372036854775e+18) { exp += int32(1) _dekkerMul2(tls, bp+16, float64(0.1), -libc.Float64FromFloat64(5.551115123125783e-18)) } } else { for (*(*[2]float64)(unsafe.Pointer(bp + 16)))[0] < float64(9.223372036854775e-83) { exp -= int32(100) _dekkerMul2(tls, bp+16, float64(1e+100), -libc.Float64FromFloat64(1.5902891109759918e+83)) } for (*(*[2]float64)(unsafe.Pointer(bp + 16)))[0] < float64(9.223372036854775e+07) { exp -= int32(10) _dekkerMul2(tls, bp+16, float64(1e+10), float64(0)) } for (*(*[2]float64)(unsafe.Pointer(bp + 16)))[0] < float64(9.223372036854775e+17) { exp -= int32(1) _dekkerMul2(tls, bp+16, float64(10), float64(0)) } } if (*(*[2]float64)(unsafe.Pointer(bp + 16)))[int32(1)] < float64(0) { v1 = uint64((*(*[2]float64)(unsafe.Pointer(bp + 16)))[0]) - uint64(-(*(*[2]float64)(unsafe.Pointer(bp + 16)))[int32(1)]) } else { v1 = uint64((*(*[2]float64)(unsafe.Pointer(bp + 16)))[0]) + uint64((*(*[2]float64)(unsafe.Pointer(bp + 16)))[int32(1)]) } *(*Tu64)(unsafe.Pointer(bp + 8)) = v1 } /* Extract significant digits. */ i = int32(libc.Uint32FromInt64(24) - libc.Uint32FromInt32(1)) _ = libc.Int32FromInt32(0) for *(*Tu64)(unsafe.Pointer(bp + 8)) != 0 { v2 = i i-- *(*uint8)(unsafe.Pointer(p + 16 + uintptr(v2))) = uint8(*(*Tu64)(unsafe.Pointer(bp + 8))%uint64(10) + uint64('0')) *(*Tu64)(unsafe.Pointer(bp + 8)) /= uint64(10) } _ = libc.Int32FromInt32(0) (*TFpDecode)(unsafe.Pointer(p)).Fn = int32(libc.Uint32FromInt64(24) - libc.Uint32FromInt32(1) - uint32(i)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TFpDecode)(unsafe.Pointer(p)).FiDP = (*TFpDecode)(unsafe.Pointer(p)).Fn + exp if iRound <= 0 { iRound = (*TFpDecode)(unsafe.Pointer(p)).FiDP - iRound if iRound == 0 && int32(*(*uint8)(unsafe.Pointer(p + 16 + uintptr(i+int32(1))))) >= int32('5') { iRound = int32(1) v3 = i i-- *(*uint8)(unsafe.Pointer(p + 16 + uintptr(v3))) = uint8('0') (*TFpDecode)(unsafe.Pointer(p)).Fn++ (*TFpDecode)(unsafe.Pointer(p)).FiDP++ } } if iRound > 0 && (iRound < (*TFpDecode)(unsafe.Pointer(p)).Fn || (*TFpDecode)(unsafe.Pointer(p)).Fn > mxRound) { z = p + 16 + uintptr(i+int32(1)) if iRound > mxRound { iRound = mxRound } (*TFpDecode)(unsafe.Pointer(p)).Fn = iRound if int32(*(*uint8)(unsafe.Pointer(z + uintptr(iRound)))) >= int32('5') { j = iRound - int32(1) for int32(1) != 0 { *(*uint8)(unsafe.Pointer(z + uintptr(j)))++ if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j)))) <= int32('9') { break } *(*uint8)(unsafe.Pointer(z + uintptr(j))) = uint8('0') if j == 0 { v4 = i i-- *(*uint8)(unsafe.Pointer((*TFpDecode)(unsafe.Pointer(p)).Fz + uintptr(v4))) = uint8('1') (*TFpDecode)(unsafe.Pointer(p)).Fn++ (*TFpDecode)(unsafe.Pointer(p)).FiDP++ break } else { j-- } } } } (*TFpDecode)(unsafe.Pointer(p)).Fz = p + 16 + uintptr(i+int32(1)) _ = libc.Int32FromInt32(0) for (*TFpDecode)(unsafe.Pointer(p)).Fn > 0 && int32(*(*uint8)(unsafe.Pointer((*TFpDecode)(unsafe.Pointer(p)).Fz + uintptr((*TFpDecode)(unsafe.Pointer(p)).Fn-int32(1))))) == int32('0') { (*TFpDecode)(unsafe.Pointer(p)).Fn-- } } // C documentation // // /* // ** Try to convert z into an unsigned 32-bit integer. Return true on // ** success and false if there is an error. // ** // ** Only decimal notation is accepted. // */ func _sqlite3GetUInt32(tls *libc.TLS, z uintptr, pI uintptr) (r int32) { var i int32 var v Tu64 _, _ = i, v v = uint64(0) i = 0 for { if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x04) != 0) { break } v = v*uint64(10) + uint64(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) - uint64('0') if v > uint64(4294967296) { *(*Tu32)(unsafe.Pointer(pI)) = uint32(0) return 0 } goto _1 _1: ; i++ } if i == 0 || int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) != 0 { *(*Tu32)(unsafe.Pointer(pI)) = uint32(0) return 0 } *(*Tu32)(unsafe.Pointer(pI)) = uint32(v) return int32(1) } /* ** The variable-length integer encoding is as follows: ** ** KEY: ** A = 0xxxxxxx 7 bits of data and one flag bit ** B = 1xxxxxxx 7 bits of data and one flag bit ** C = xxxxxxxx 8 bits of data ** ** 7 bits - A ** 14 bits - BA ** 21 bits - BBA ** 28 bits - BBBA ** 35 bits - BBBBA ** 42 bits - BBBBBA ** 49 bits - BBBBBBA ** 56 bits - BBBBBBBA ** 64 bits - BBBBBBBBC */ // C documentation // // /* // ** Write a 64-bit variable-length integer to memory starting at p[0]. // ** The length of data write will be between 1 and 9 bytes. The number // ** of bytes written is returned. // ** // ** A variable-length integer consists of the lower 7 bits of each byte // ** for all bytes that have the 8th bit set and one byte with the 8th // ** bit clear. Except, if we get to the 9th byte, it stores the full // ** 8 bits and is the last byte. // */ func _putVarint64(tls *libc.TLS, p uintptr, v Tu64) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, j, n, v2 int32 var p3 uintptr var _ /* buf at bp+0 */ [10]Tu8 _, _, _, _, _ = i, j, n, v2, p3 if v&(libc.Uint64FromUint32(0xff000000)<>= uint64(8) i = int32(7) for { if !(i >= 0) { break } *(*uint8)(unsafe.Pointer(p + uintptr(i))) = uint8(v&libc.Uint64FromInt32(0x7f) | libc.Uint64FromInt32(0x80)) v >>= uint64(7) goto _1 _1: ; i-- } return int32(9) } n = 0 for cond := true; cond; cond = v != uint64(0) { v2 = n n++ (*(*[10]Tu8)(unsafe.Pointer(bp)))[v2] = uint8(v&libc.Uint64FromInt32(0x7f) | libc.Uint64FromInt32(0x80)) v >>= uint64(7) } p3 = bp *(*Tu8)(unsafe.Pointer(p3)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p3))) & libc.Int32FromInt32(0x7f)) _ = libc.Int32FromInt32(0) i = 0 j = n - libc.Int32FromInt32(1) for { if !(j >= 0) { break } *(*uint8)(unsafe.Pointer(p + uintptr(i))) = (*(*[10]Tu8)(unsafe.Pointer(bp)))[j] goto _4 _4: ; j-- i++ } return n } func _sqlite3PutVarint(tls *libc.TLS, p uintptr, v Tu64) (r int32) { if v <= uint64(0x7f) { *(*uint8)(unsafe.Pointer(p)) = uint8(v & uint64(0x7f)) return int32(1) } if v <= uint64(0x3fff) { *(*uint8)(unsafe.Pointer(p)) = uint8(v>>libc.Int32FromInt32(7)&uint64(0x7f) | uint64(0x80)) *(*uint8)(unsafe.Pointer(p + 1)) = uint8(v & uint64(0x7f)) return int32(2) } return _putVarint64(tls, p, v) } /* ** Bitmasks used by sqlite3GetVarint(). These precomputed constants ** are defined here rather than simply putting the constant expressions ** inline in order to work around bugs in the RVT compiler. ** ** SLOT_2_0 A mask for (0x7f<<14) | 0x7f ** ** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0 */ // C documentation // // /* // ** Read a 64-bit variable-length integer from memory starting at p[0]. // ** Return the number of bytes read. The value is stored in *v. // */ func _sqlite3GetVarint(tls *libc.TLS, p uintptr, v uintptr) (r Tu8) { var a, b, s Tu32 _, _, _ = a, b, s if int32(*(*int8)(unsafe.Pointer(p))) >= 0 { *(*Tu64)(unsafe.Pointer(v)) = uint64(*(*uint8)(unsafe.Pointer(p))) return uint8(1) } if int32(*(*int8)(unsafe.Pointer(p + 1))) >= 0 { *(*Tu64)(unsafe.Pointer(v)) = uint64(uint32(int32(*(*uint8)(unsafe.Pointer(p)))&libc.Int32FromInt32(0x7f))<> int32(18) *(*Tu64)(unsafe.Pointer(v)) = uint64(s)<> int32(18) *(*Tu64)(unsafe.Pointer(v)) = uint64(s)<> int32(11) *(*Tu64)(unsafe.Pointer(v)) = uint64(s)<> int32(4) *(*Tu64)(unsafe.Pointer(v)) = uint64(s)<> int32(3) s |= b *(*Tu64)(unsafe.Pointer(v)) = uint64(s)<>= uint64(7) if !(v != uint64(0)) { break } _ = libc.Int32FromInt32(0) goto _1 _1: ; i++ } return i } // C documentation // // /* // ** Read or write a four-byte big-endian integer value. // */ func _sqlite3Get4byte(tls *libc.TLS, p uintptr) (r Tu32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* x at bp+0 */ Tu32 libc.Xmemcpy(tls, bp, p, uint32(4)) return libc.X__builtin_bswap32(tls, *(*Tu32)(unsafe.Pointer(bp))) } func _sqlite3Put4byte(tls *libc.TLS, p uintptr, v Tu32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* x at bp+0 */ Tu32 *(*Tu32)(unsafe.Pointer(bp)) = libc.X__builtin_bswap32(tls, v) libc.Xmemcpy(tls, p, bp, uint32(4)) } // C documentation // // /* // ** Translate a single byte of Hex into an integer. // ** This routine only works if h really is a valid hexadecimal // ** character: 0..9a..fA..F // */ func _sqlite3HexToInt(tls *libc.TLS, h int32) (r Tu8) { _ = libc.Int32FromInt32(0) h += int32(9) * (int32(1) & (h >> int32(6))) return uint8(h & libc.Int32FromInt32(0xf)) } // C documentation // // /* // ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary // ** value. Return a pointer to its binary value. Space to hold the // ** binary value has been obtained from malloc and must be freed by // ** the calling routine. // */ func _sqlite3HexToBlob(tls *libc.TLS, db uintptr, z uintptr, n int32) (r uintptr) { var i int32 var zBlob uintptr _, _ = i, zBlob zBlob = _sqlite3DbMallocRawNN(tls, db, uint64(n/int32(2)+int32(1))) n-- if zBlob != 0 { i = 0 for { if !(i < n) { break } *(*uint8)(unsafe.Pointer(zBlob + uintptr(i/int32(2)))) = uint8(int32(_sqlite3HexToInt(tls, int32(*(*uint8)(unsafe.Pointer(z + uintptr(i))))))<= 0 { return x } if x == libc.Int32FromUint32(0x80000000) { return int32(0x7fffffff) } return -x } // C documentation // // /* // ** Find (an approximate) sum of two LogEst values. This computation is // ** not a simple "+" operator because LogEst is stored as a logarithmic // ** value. // ** // */ func _sqlite3LogEstAdd(tls *libc.TLS, a TLogEst, b TLogEst) (r TLogEst) { if int32(a) >= int32(b) { if int32(a) > int32(b)+int32(49) { return a } if int32(a) > int32(b)+int32(31) { return int16(int32(a) + int32(1)) } return int16(int32(a) + int32(_x[int32(a)-int32(b)])) } else { if int32(b) > int32(a)+int32(49) { return b } if int32(b) > int32(a)+int32(31) { return int16(int32(b) + int32(1)) } return int16(int32(b) + int32(_x[int32(b)-int32(a)])) } return r } var _x = [32]uint8{ 0: uint8(10), 1: uint8(10), 2: uint8(9), 3: uint8(9), 4: uint8(8), 5: uint8(8), 6: uint8(7), 7: uint8(7), 8: uint8(7), 9: uint8(6), 10: uint8(6), 11: uint8(6), 12: uint8(5), 13: uint8(5), 14: uint8(5), 15: uint8(4), 16: uint8(4), 17: uint8(4), 18: uint8(4), 19: uint8(3), 20: uint8(3), 21: uint8(3), 22: uint8(3), 23: uint8(3), 24: uint8(3), 25: uint8(2), 26: uint8(2), 27: uint8(2), 28: uint8(2), 29: uint8(2), 30: uint8(2), 31: uint8(2), } // C documentation // // /* // ** Convert an integer into a LogEst. In other words, compute an // ** approximation for 10*log2(x). // */ func _sqlite3LogEst(tls *libc.TLS, x Tu64) (r TLogEst) { var i int32 var y TLogEst _, _ = i, y y = int16(40) if x < uint64(8) { if x < uint64(2) { return 0 } for x < uint64(8) { y = TLogEst(int32(y) - libc.Int32FromInt32(10)) x <<= uint64(1) } } else { i = int32(60) - libc.X__builtin_clzll(tls, x) y = TLogEst(int32(y) + i*libc.Int32FromInt32(10)) x >>= uint64(i) } return int16(int32(_a[x&uint64(7)]) + int32(y) - int32(10)) } var _a = [8]TLogEst{ 1: int16(2), 2: int16(3), 3: int16(5), 4: int16(6), 5: int16(7), 6: int16(8), 7: int16(9), } // C documentation // // /* // ** Convert a double into a LogEst // ** In other words, compute an approximation for 10*log2(x). // */ func _sqlite3LogEstFromDouble(tls *libc.TLS, _x float64) (r TLogEst) { bp := tls.Alloc(16) defer tls.Free(16) *(*float64)(unsafe.Pointer(bp)) = _x var e TLogEst var _ /* a at bp+8 */ Tu64 _ = e _ = libc.Int32FromInt32(0) if *(*float64)(unsafe.Pointer(bp)) <= libc.Float64FromInt32(1) { return 0 } if *(*float64)(unsafe.Pointer(bp)) <= libc.Float64FromInt32(2000000000) { return _sqlite3LogEst(tls, uint64(*(*float64)(unsafe.Pointer(bp)))) } libc.Xmemcpy(tls, bp+8, bp, uint32(8)) e = int16(*(*Tu64)(unsafe.Pointer(bp + 8))>>libc.Int32FromInt32(52) - uint64(1022)) return int16(int32(e) * int32(10)) } // C documentation // // /* // ** Convert a LogEst into an integer. // */ func _sqlite3LogEstToInt(tls *libc.TLS, x TLogEst) (r Tu64) { var n Tu64 var v1 uint64 _, _ = n, v1 n = uint64(int32(x) % int32(10)) x = TLogEst(int32(x) / libc.Int32FromInt32(10)) if n >= uint64(5) { n -= uint64(2) } else { if n >= uint64(1) { n -= uint64(1) } } if int32(x) > int32(60) { return uint64(libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)<= int32(3) { v1 = (n + uint64(8)) << (int32(x) - int32(3)) } else { v1 = (n + uint64(8)) >> (int32(3) - int32(x)) } return v1 } // C documentation // // /* // ** Add a new name/number pair to a VList. This might require that the // ** VList object be reallocated, so return the new VList. If an OOM // ** error occurs, the original VList returned and the // ** db->mallocFailed flag is set. // ** // ** A VList is really just an array of integers. To destroy a VList, // ** simply pass it to sqlite3DbFree(). // ** // ** The first integer is the number of integers allocated for the whole // ** VList. The second integer is the number of integers actually used. // ** Each name/number pair is encoded by subsequent groups of 3 or more // ** integers. // ** // ** Each name/number pair starts with two integers which are the numeric // ** value for the pair and the size of the name/number pair, respectively. // ** The text name overlays one or more following integers. The text name // ** is always zero-terminated. // ** // ** Conceptually: // ** // ** struct VList { // ** int nAlloc; // Number of allocated slots // ** int nUsed; // Number of used slots // ** struct VListEntry { // ** int iValue; // Value for this entry // ** int nSlot; // Slots used by this entry // ** // ... variable name goes here // ** } a[0]; // ** } // ** // ** During code generation, pointers to the variable names within the // ** VList are taken. When that happens, nAlloc is set to zero as an // ** indication that the VList may never again be enlarged, since the // ** accompanying realloc() would invalidate the pointers. // */ func _sqlite3VListAdd(tls *libc.TLS, db uintptr, pIn uintptr, zName uintptr, nName int32, iVal int32) (r uintptr) { var i, nInt int32 var nAlloc Tsqlite3_int64 var pOut, z uintptr var v1 int64 _, _, _, _, _, _ = i, nAlloc, nInt, pOut, z, v1 /* Index in pIn[] where zName is stored */ nInt = nName/int32(4) + int32(3) _ = libc.Int32FromInt32(0) /* Verify ok to add new elements */ if pIn == uintptr(0) || *(*TVList)(unsafe.Pointer(pIn + 1*4))+nInt > *(*TVList)(unsafe.Pointer(pIn)) { if pIn != 0 { v1 = int64(2) * int64(*(*TVList)(unsafe.Pointer(pIn))) } else { v1 = int64(10) } /* Enlarge the allocation */ nAlloc = v1 + int64(nInt) pOut = _sqlite3DbRealloc(tls, db, pIn, uint64(nAlloc*int64(4))) if pOut == uintptr(0) { return pIn } if pIn == uintptr(0) { *(*TVList)(unsafe.Pointer(pOut + 1*4)) = int32(2) } pIn = pOut *(*TVList)(unsafe.Pointer(pIn)) = int32(nAlloc) } i = *(*TVList)(unsafe.Pointer(pIn + 1*4)) *(*TVList)(unsafe.Pointer(pIn + uintptr(i)*4)) = iVal *(*TVList)(unsafe.Pointer(pIn + uintptr(i+int32(1))*4)) = nInt z = pIn + uintptr(i+int32(2))*4 *(*TVList)(unsafe.Pointer(pIn + 1*4)) = i + nInt _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, z, zName, uint32(nName)) *(*uint8)(unsafe.Pointer(z + uintptr(nName))) = uint8(0) return pIn } // C documentation // // /* // ** Return a pointer to the name of a variable in the given VList that // ** has the value iVal. Or return a NULL if there is no such variable in // ** the list // */ func _sqlite3VListNumToName(tls *libc.TLS, pIn uintptr, iVal int32) (r uintptr) { var i, mx int32 _, _ = i, mx if pIn == uintptr(0) { return uintptr(0) } mx = *(*TVList)(unsafe.Pointer(pIn + 1*4)) i = int32(2) for cond := true; cond; cond = i < mx { if *(*TVList)(unsafe.Pointer(pIn + uintptr(i)*4)) == iVal { return pIn + uintptr(i+int32(2))*4 } i += *(*TVList)(unsafe.Pointer(pIn + uintptr(i+int32(1))*4)) } return uintptr(0) } // C documentation // // /* // ** Return the number of the variable named zName, if it is in VList. // ** or return 0 if there is no such variable. // */ func _sqlite3VListNameToNum(tls *libc.TLS, pIn uintptr, zName uintptr, nName int32) (r int32) { var i, mx int32 var z uintptr _, _, _ = i, mx, z if pIn == uintptr(0) { return 0 } mx = *(*TVList)(unsafe.Pointer(pIn + 1*4)) i = int32(2) for cond := true; cond; cond = i < mx { z = pIn + uintptr(i+int32(2))*4 if libc.Xstrncmp(tls, z, zName, uint32(nName)) == 0 && int32(*(*uint8)(unsafe.Pointer(z + uintptr(nName)))) == 0 { return *(*TVList)(unsafe.Pointer(pIn + uintptr(i)*4)) } i += *(*TVList)(unsafe.Pointer(pIn + uintptr(i+int32(1))*4)) } return 0 } /* ** High-resolution hardware timer used for debugging and testing only. */ /************** End of util.c ************************************************/ /************** Begin file hash.c ********************************************/ /* ** 2001 September 22 ** ** 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 is the implementation of generic hash-tables ** used in SQLite. */ /* #include "sqliteInt.h" */ /* #include */ // C documentation // // /* Turn bulk memory into a hash table object by initializing the // ** fields of the Hash structure. // ** // ** "pNew" is a pointer to the hash table that is to be initialized. // */ func _sqlite3HashInit(tls *libc.TLS, pNew uintptr) { _ = libc.Int32FromInt32(0) (*THash)(unsafe.Pointer(pNew)).Ffirst = uintptr(0) (*THash)(unsafe.Pointer(pNew)).Fcount = uint32(0) (*THash)(unsafe.Pointer(pNew)).Fhtsize = uint32(0) (*THash)(unsafe.Pointer(pNew)).Fht = uintptr(0) } // C documentation // // /* Remove all entries from a hash table. Reclaim all memory. // ** Call this routine to delete a hash table or to reset a hash table // ** to the empty state. // */ func _sqlite3HashClear(tls *libc.TLS, pH uintptr) { var elem, next_elem uintptr _, _ = elem, next_elem /* For looping over all elements of the table */ _ = libc.Int32FromInt32(0) elem = (*THash)(unsafe.Pointer(pH)).Ffirst (*THash)(unsafe.Pointer(pH)).Ffirst = uintptr(0) Xsqlite3_free(tls, (*THash)(unsafe.Pointer(pH)).Fht) (*THash)(unsafe.Pointer(pH)).Fht = uintptr(0) (*THash)(unsafe.Pointer(pH)).Fhtsize = uint32(0) for elem != 0 { next_elem = (*THashElem)(unsafe.Pointer(elem)).Fnext Xsqlite3_free(tls, elem) elem = next_elem } (*THash)(unsafe.Pointer(pH)).Fcount = uint32(0) } // C documentation // // /* // ** The hashing function. // */ func _strHash(tls *libc.TLS, z uintptr) (r uint32) { var c, v1 uint8 var h uint32 var v2 uintptr _, _, _, _ = c, h, v1, v2 h = uint32(0) for { v2 = z z++ v1 = *(*uint8)(unsafe.Pointer(v2)) c = v1 if !(int32(v1) != 0) { break } /*OPTIMIZATION-IF-TRUE*/ /* Knuth multiplicative hashing. (Sorting & Searching, p. 510). ** 0x9e3779b1 is 2654435761 which is the closest prime number to ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */ h += uint32(_sqlite3UpperToLower[c]) h *= uint32(0x9e3779b1) } return h } // C documentation // // /* Link pNew element into the hash table pH. If pEntry!=0 then also // ** insert pNew into the pEntry hash bucket. // */ func _insertElement(tls *libc.TLS, pH uintptr, pEntry uintptr, pNew uintptr) { var pHead, v1 uintptr _, _ = pHead, v1 /* First element already in pEntry */ if pEntry != 0 { if (*T_ht)(unsafe.Pointer(pEntry)).Fcount != 0 { v1 = (*T_ht)(unsafe.Pointer(pEntry)).Fchain } else { v1 = uintptr(0) } pHead = v1 (*T_ht)(unsafe.Pointer(pEntry)).Fcount++ (*T_ht)(unsafe.Pointer(pEntry)).Fchain = pNew } else { pHead = uintptr(0) } if pHead != 0 { (*THashElem)(unsafe.Pointer(pNew)).Fnext = pHead (*THashElem)(unsafe.Pointer(pNew)).Fprev = (*THashElem)(unsafe.Pointer(pHead)).Fprev if (*THashElem)(unsafe.Pointer(pHead)).Fprev != 0 { (*THashElem)(unsafe.Pointer((*THashElem)(unsafe.Pointer(pHead)).Fprev)).Fnext = pNew } else { (*THash)(unsafe.Pointer(pH)).Ffirst = pNew } (*THashElem)(unsafe.Pointer(pHead)).Fprev = pNew } else { (*THashElem)(unsafe.Pointer(pNew)).Fnext = (*THash)(unsafe.Pointer(pH)).Ffirst if (*THash)(unsafe.Pointer(pH)).Ffirst != 0 { (*THashElem)(unsafe.Pointer((*THash)(unsafe.Pointer(pH)).Ffirst)).Fprev = pNew } (*THashElem)(unsafe.Pointer(pNew)).Fprev = uintptr(0) (*THash)(unsafe.Pointer(pH)).Ffirst = pNew } } // C documentation // // /* Resize the hash table so that it contains "new_size" buckets. // ** // ** The hash table might fail to resize if sqlite3_malloc() fails or // ** if the new size is the same as the prior size. // ** Return TRUE if the resize occurs and false if not. // */ func _rehash(tls *libc.TLS, pH uintptr, new_size uint32) (r int32) { var elem, new_ht, next_elem uintptr var h, v1 uint32 _, _, _, _, _ = elem, h, new_ht, next_elem, v1 /* For looping over existing elements */ if new_size*uint32(8) > uint32(SQLITE_MALLOC_SOFT_LIMIT) { new_size = libc.Uint32FromInt32(SQLITE_MALLOC_SOFT_LIMIT) / libc.Uint32FromInt64(8) } if new_size == (*THash)(unsafe.Pointer(pH)).Fhtsize { return 0 } /* The inability to allocates space for a larger hash table is ** a performance hit but it is not a fatal error. So mark the ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero() ** only zeroes the requested number of bytes whereas this module will ** use the actual amount of space allocated for the hash table (which ** may be larger than the requested amount). */ _sqlite3BeginBenignMalloc(tls) new_ht = _sqlite3Malloc(tls, uint64(new_size*uint32(8))) _sqlite3EndBenignMalloc(tls) if new_ht == uintptr(0) { return 0 } Xsqlite3_free(tls, (*THash)(unsafe.Pointer(pH)).Fht) (*THash)(unsafe.Pointer(pH)).Fht = new_ht v1 = uint32(_sqlite3MallocSize(tls, new_ht)) / libc.Uint32FromInt64(8) new_size = v1 (*THash)(unsafe.Pointer(pH)).Fhtsize = v1 libc.Xmemset(tls, new_ht, 0, new_size*uint32(8)) elem = (*THash)(unsafe.Pointer(pH)).Ffirst (*THash)(unsafe.Pointer(pH)).Ffirst = libc.UintptrFromInt32(0) for { if !(elem != 0) { break } h = _strHash(tls, (*THashElem)(unsafe.Pointer(elem)).FpKey) % new_size next_elem = (*THashElem)(unsafe.Pointer(elem)).Fnext _insertElement(tls, pH, new_ht+uintptr(h)*8, elem) goto _2 _2: ; elem = next_elem } return int32(1) } // C documentation // // /* This function (for internal use only) locates an element in an // ** hash table that matches the given key. If no element is found, // ** a pointer to a static null element with HashElem.data==0 is returned. // ** If pH is not NULL, then the hash for this key is written to *pH. // */ func _findElementWithHash(tls *libc.TLS, pH uintptr, pKey uintptr, pHash uintptr) (r uintptr) { var count, h uint32 var elem, pEntry uintptr _, _, _, _ = count, elem, h, pEntry /* The computed hash */ if (*THash)(unsafe.Pointer(pH)).Fht != 0 { h = _strHash(tls, pKey) % (*THash)(unsafe.Pointer(pH)).Fhtsize pEntry = (*THash)(unsafe.Pointer(pH)).Fht + uintptr(h)*8 elem = (*T_ht)(unsafe.Pointer(pEntry)).Fchain count = (*T_ht)(unsafe.Pointer(pEntry)).Fcount } else { h = uint32(0) elem = (*THash)(unsafe.Pointer(pH)).Ffirst count = (*THash)(unsafe.Pointer(pH)).Fcount } if pHash != 0 { *(*uint32)(unsafe.Pointer(pHash)) = h } for count != 0 { _ = libc.Int32FromInt32(0) if _sqlite3StrICmp(tls, (*THashElem)(unsafe.Pointer(elem)).FpKey, pKey) == 0 { return elem } elem = (*THashElem)(unsafe.Pointer(elem)).Fnext count-- } return uintptr(unsafe.Pointer(&_nullElement)) } var _nullElement = THashElem{} // C documentation // // /* Remove a single entry from the hash table given a pointer to that // ** element and a hash on the element's key. // */ func _removeElementGivenHash(tls *libc.TLS, pH uintptr, elem uintptr, h uint32) { var pEntry uintptr _ = pEntry if (*THashElem)(unsafe.Pointer(elem)).Fprev != 0 { (*THashElem)(unsafe.Pointer((*THashElem)(unsafe.Pointer(elem)).Fprev)).Fnext = (*THashElem)(unsafe.Pointer(elem)).Fnext } else { (*THash)(unsafe.Pointer(pH)).Ffirst = (*THashElem)(unsafe.Pointer(elem)).Fnext } if (*THashElem)(unsafe.Pointer(elem)).Fnext != 0 { (*THashElem)(unsafe.Pointer((*THashElem)(unsafe.Pointer(elem)).Fnext)).Fprev = (*THashElem)(unsafe.Pointer(elem)).Fprev } if (*THash)(unsafe.Pointer(pH)).Fht != 0 { pEntry = (*THash)(unsafe.Pointer(pH)).Fht + uintptr(h)*8 if (*T_ht)(unsafe.Pointer(pEntry)).Fchain == elem { (*T_ht)(unsafe.Pointer(pEntry)).Fchain = (*THashElem)(unsafe.Pointer(elem)).Fnext } _ = libc.Int32FromInt32(0) (*T_ht)(unsafe.Pointer(pEntry)).Fcount-- } Xsqlite3_free(tls, elem) (*THash)(unsafe.Pointer(pH)).Fcount-- if (*THash)(unsafe.Pointer(pH)).Fcount == uint32(0) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3HashClear(tls, pH) } } // C documentation // // /* Attempt to locate an element of the hash table pH with a key // ** that matches pKey. Return the data for this element if it is // ** found, or NULL if there is no match. // */ func _sqlite3HashFind(tls *libc.TLS, pH uintptr, pKey uintptr) (r uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return (*THashElem)(unsafe.Pointer(_findElementWithHash(tls, pH, pKey, uintptr(0)))).Fdata } // C documentation // // /* Insert an element into the hash table pH. The key is pKey // ** and the data is "data". // ** // ** If no element exists with a matching key, then a new // ** element is created and NULL is returned. // ** // ** If another element already exists with the same key, then the // ** new data replaces the old data and the old data is returned. // ** The key is not copied in this instance. If a malloc fails, then // ** the new data is returned and the hash table is unchanged. // ** // ** If the "data" parameter to this function is NULL, then the // ** element corresponding to "key" is removed from the hash table. // */ func _sqlite3HashInsert(tls *libc.TLS, pH uintptr, pKey uintptr, data uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var elem, new_elem, old_data, v1 uintptr var _ /* h at bp+0 */ uint32 _, _, _, _ = elem, new_elem, old_data, v1 /* New element added to the pH */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) elem = _findElementWithHash(tls, pH, pKey, bp) if (*THashElem)(unsafe.Pointer(elem)).Fdata != 0 { old_data = (*THashElem)(unsafe.Pointer(elem)).Fdata if data == uintptr(0) { _removeElementGivenHash(tls, pH, elem, *(*uint32)(unsafe.Pointer(bp))) } else { (*THashElem)(unsafe.Pointer(elem)).Fdata = data (*THashElem)(unsafe.Pointer(elem)).FpKey = pKey } return old_data } if data == uintptr(0) { return uintptr(0) } new_elem = _sqlite3Malloc(tls, uint64(16)) if new_elem == uintptr(0) { return data } (*THashElem)(unsafe.Pointer(new_elem)).FpKey = pKey (*THashElem)(unsafe.Pointer(new_elem)).Fdata = data (*THash)(unsafe.Pointer(pH)).Fcount++ if (*THash)(unsafe.Pointer(pH)).Fcount >= uint32(10) && (*THash)(unsafe.Pointer(pH)).Fcount > uint32(2)*(*THash)(unsafe.Pointer(pH)).Fhtsize { if _rehash(tls, pH, (*THash)(unsafe.Pointer(pH)).Fcount*uint32(2)) != 0 { _ = libc.Int32FromInt32(0) *(*uint32)(unsafe.Pointer(bp)) = _strHash(tls, pKey) % (*THash)(unsafe.Pointer(pH)).Fhtsize } } if (*THash)(unsafe.Pointer(pH)).Fht != 0 { v1 = (*THash)(unsafe.Pointer(pH)).Fht + uintptr(*(*uint32)(unsafe.Pointer(bp)))*8 } else { v1 = uintptr(0) } _insertElement(tls, pH, v1, new_elem) return uintptr(0) } // C documentation // // /************** End of hash.c ************************************************/ // /************** Begin file opcodes.c *****************************************/ // /* Automatically generated. Do not edit */ // /* See the tool/mkopcodec.tcl script for details. */ func _sqlite3OpcodeName(tls *libc.TLS, i int32) (r uintptr) { return _azName[i] } var _azName = [190]uintptr{ 0: __ccgo_ts + 1786, 1: __ccgo_ts + 1796, 2: __ccgo_ts + 1807, 3: __ccgo_ts + 1819, 4: __ccgo_ts + 1830, 5: __ccgo_ts + 1842, 6: __ccgo_ts + 1849, 7: __ccgo_ts + 1857, 8: __ccgo_ts + 1865, 9: __ccgo_ts + 1870, 10: __ccgo_ts + 1875, 11: __ccgo_ts + 1881, 12: __ccgo_ts + 1895, 13: __ccgo_ts + 1901, 14: __ccgo_ts + 1911, 15: __ccgo_ts + 1916, 16: __ccgo_ts + 1921, 17: __ccgo_ts + 1924, 18: __ccgo_ts + 1930, 19: __ccgo_ts + 1937, 20: __ccgo_ts + 1941, 21: __ccgo_ts + 1951, 22: __ccgo_ts + 1958, 23: __ccgo_ts + 1965, 24: __ccgo_ts + 1972, 25: __ccgo_ts + 1979, 26: __ccgo_ts + 1989, 27: __ccgo_ts + 1998, 28: __ccgo_ts + 2009, 29: __ccgo_ts + 2018, 30: __ccgo_ts + 2024, 31: __ccgo_ts + 2034, 32: __ccgo_ts + 2044, 33: __ccgo_ts + 2049, 34: __ccgo_ts + 2059, 35: __ccgo_ts + 2070, 36: __ccgo_ts + 2075, 37: __ccgo_ts + 2082, 38: __ccgo_ts + 2093, 39: __ccgo_ts + 2098, 40: __ccgo_ts + 2103, 41: __ccgo_ts + 2109, 42: __ccgo_ts + 2115, 43: __ccgo_ts + 2121, 44: __ccgo_ts + 2124, 45: __ccgo_ts + 2128, 46: __ccgo_ts + 2134, 47: __ccgo_ts + 2145, 48: __ccgo_ts + 2156, 49: __ccgo_ts + 2164, 50: __ccgo_ts + 2173, 51: __ccgo_ts + 2180, 52: __ccgo_ts + 2188, 53: __ccgo_ts + 2191, 54: __ccgo_ts + 2194, 55: __ccgo_ts + 2197, 56: __ccgo_ts + 2200, 57: __ccgo_ts + 2203, 58: __ccgo_ts + 2206, 59: __ccgo_ts + 2213, 60: __ccgo_ts + 2219, 61: __ccgo_ts + 2229, 62: __ccgo_ts + 2242, 63: __ccgo_ts + 2253, 64: __ccgo_ts + 2259, 65: __ccgo_ts + 2266, 66: __ccgo_ts + 2275, 67: __ccgo_ts + 2284, 68: __ccgo_ts + 2291, 69: __ccgo_ts + 2304, 70: __ccgo_ts + 2315, 71: __ccgo_ts + 2320, 72: __ccgo_ts + 2328, 73: __ccgo_ts + 2334, 74: __ccgo_ts + 2341, 75: __ccgo_ts + 2353, 76: __ccgo_ts + 2358, 77: __ccgo_ts + 2367, 78: __ccgo_ts + 2372, 79: __ccgo_ts + 2381, 80: __ccgo_ts + 2386, 81: __ccgo_ts + 2391, 82: __ccgo_ts + 2397, 83: __ccgo_ts + 2405, 84: __ccgo_ts + 2413, 85: __ccgo_ts + 2423, 86: __ccgo_ts + 2431, 87: __ccgo_ts + 2438, 88: __ccgo_ts + 2451, 89: __ccgo_ts + 2456, 90: __ccgo_ts + 2468, 91: __ccgo_ts + 2476, 92: __ccgo_ts + 2483, 93: __ccgo_ts + 2494, 94: __ccgo_ts + 2501, 95: __ccgo_ts + 2508, 96: __ccgo_ts + 2518, 97: __ccgo_ts + 2527, 98: __ccgo_ts + 2538, 99: __ccgo_ts + 2544, 100: __ccgo_ts + 2555, 101: __ccgo_ts + 2565, 102: __ccgo_ts + 2575, 103: __ccgo_ts + 2582, 104: __ccgo_ts + 2588, 105: __ccgo_ts + 2598, 106: __ccgo_ts + 2609, 107: __ccgo_ts + 2613, 108: __ccgo_ts + 2622, 109: __ccgo_ts + 2631, 110: __ccgo_ts + 2638, 111: __ccgo_ts + 2648, 112: __ccgo_ts + 2655, 113: __ccgo_ts + 2664, 114: __ccgo_ts + 2674, 115: __ccgo_ts + 2681, 116: __ccgo_ts + 2689, 117: __ccgo_ts + 2703, 118: __ccgo_ts + 2711, 119: __ccgo_ts + 2725, 120: __ccgo_ts + 2736, 121: __ccgo_ts + 2749, 122: __ccgo_ts + 2760, 123: __ccgo_ts + 2766, 124: __ccgo_ts + 2778, 125: __ccgo_ts + 2787, 126: __ccgo_ts + 2795, 127: __ccgo_ts + 2804, 128: __ccgo_ts + 2813, 129: __ccgo_ts + 2820, 130: __ccgo_ts + 2828, 131: __ccgo_ts + 2835, 132: __ccgo_ts + 2846, 133: __ccgo_ts + 2860, 134: __ccgo_ts + 2871, 135: __ccgo_ts + 2879, 136: __ccgo_ts + 2885, 137: __ccgo_ts + 2893, 138: __ccgo_ts + 2901, 139: __ccgo_ts + 2911, 140: __ccgo_ts + 2924, 141: __ccgo_ts + 2934, 142: __ccgo_ts + 2947, 143: __ccgo_ts + 2956, 144: __ccgo_ts + 2967, 145: __ccgo_ts + 2975, 146: __ccgo_ts + 2981, 147: __ccgo_ts + 2993, 148: __ccgo_ts + 3005, 149: __ccgo_ts + 3013, 150: __ccgo_ts + 3025, 151: __ccgo_ts + 3038, 152: __ccgo_ts + 3048, 153: __ccgo_ts + 3058, 154: __ccgo_ts + 3063, 155: __ccgo_ts + 3075, 156: __ccgo_ts + 3087, 157: __ccgo_ts + 3097, 158: __ccgo_ts + 3103, 159: __ccgo_ts + 3113, 160: __ccgo_ts + 3120, 161: __ccgo_ts + 3132, 162: __ccgo_ts + 3143, 163: __ccgo_ts + 3151, 164: __ccgo_ts + 3160, 165: __ccgo_ts + 3169, 166: __ccgo_ts + 3178, 167: __ccgo_ts + 3185, 168: __ccgo_ts + 3196, 169: __ccgo_ts + 3209, 170: __ccgo_ts + 3219, 171: __ccgo_ts + 3226, 172: __ccgo_ts + 3234, 173: __ccgo_ts + 3243, 174: __ccgo_ts + 3249, 175: __ccgo_ts + 3256, 176: __ccgo_ts + 3264, 177: __ccgo_ts + 3272, 178: __ccgo_ts + 3280, 179: __ccgo_ts + 3290, 180: __ccgo_ts + 3299, 181: __ccgo_ts + 3310, 182: __ccgo_ts + 3321, 183: __ccgo_ts + 3332, 184: __ccgo_ts + 3342, 185: __ccgo_ts + 3348, 186: __ccgo_ts + 3359, 187: __ccgo_ts + 3370, 188: __ccgo_ts + 3375, 189: __ccgo_ts + 3383, } type Tstat = struct { F__ccgo_align [0]uint32 Fst_dev t__dev_t F__pad1 uint16 F__st_ino t__ino_t Fst_mode t__mode_t Fst_nlink t__nlink_t Fst_uid t__uid_t Fst_gid t__gid_t Fst_rdev t__dev_t F__pad2 uint16 F__ccgo_align9 [6]byte Fst_size t__off64_t Fst_blksize t__blksize_t F__ccgo_align11 [4]byte Fst_blocks t__blkcnt64_t Fst_atim Ttimespec Fst_mtim Ttimespec Fst_ctim Ttimespec Fst_ino t__ino64_t } type stat = Tstat type Tstat64 = struct { F__ccgo_align [0]uint32 Fst_dev t__dev_t F__pad1 uint32 F__st_ino t__ino_t Fst_mode t__mode_t Fst_nlink t__nlink_t Fst_uid t__uid_t Fst_gid t__gid_t Fst_rdev t__dev_t F__pad2 uint32 F__ccgo_align9 [4]byte Fst_size t__off64_t Fst_blksize t__blksize_t F__ccgo_align11 [4]byte Fst_blocks t__blkcnt64_t Fst_atim Ttimespec Fst_mtim Ttimespec Fst_ctim Ttimespec Fst_ino t__ino64_t } type stat64 = Tstat64 type Tstatx_timestamp = struct { F__ccgo_align [0]uint32 Ftv_sec t__int64_t Ftv_nsec t__uint32_t F__statx_timestamp_pad1 [1]t__int32_t } type statx_timestamp = Tstatx_timestamp type Tstatx = struct { F__ccgo_align [0]uint32 Fstx_mask t__uint32_t Fstx_blksize t__uint32_t Fstx_attributes t__uint64_t Fstx_nlink t__uint32_t Fstx_uid t__uint32_t Fstx_gid t__uint32_t Fstx_mode t__uint16_t F__statx_pad1 [1]t__uint16_t Fstx_ino t__uint64_t Fstx_size t__uint64_t Fstx_blocks t__uint64_t Fstx_attributes_mask t__uint64_t Fstx_atime Tstatx_timestamp Fstx_btime Tstatx_timestamp Fstx_ctime Tstatx_timestamp Fstx_mtime Tstatx_timestamp Fstx_rdev_major t__uint32_t Fstx_rdev_minor t__uint32_t Fstx_dev_major t__uint32_t Fstx_dev_minor t__uint32_t F__statx_pad2 [14]t__uint64_t } type statx = Tstatx type Tflock = struct { F__ccgo_align [0]uint32 Fl_type int16 Fl_whence int16 F__ccgo_align2 [4]byte Fl_start t__off64_t Fl_len t__off64_t Fl_pid t__pid_t F__ccgo_pad5 [4]byte } type flock = Tflock type Tflock64 = struct { F__ccgo_align [0]uint32 Fl_type int16 Fl_whence int16 F__ccgo_align2 [4]byte Fl_start t__off64_t Fl_len t__off64_t Fl_pid t__pid_t F__ccgo_pad5 [4]byte } type flock64 = Tflock64 type Tiovec = struct { Fiov_base uintptr Fiov_len Tsize_t } type iovec = Tiovec type ___pid_type = int32 const _F_OWNER_TID = 0 const _F_OWNER_PID = 1 const _F_OWNER_PGRP = 2 const _F_OWNER_GID = 2 type Tf_owner_ex = struct { Ftype1 ___pid_type Fpid t__pid_t } type f_owner_ex = Tf_owner_ex type Tfile_handle = struct { Fhandle_bytes uint32 Fhandle_type int32 } type file_handle = Tfile_handle type Twinsize = struct { Fws_row uint16 Fws_col uint16 Fws_xpixel uint16 Fws_ypixel uint16 } type winsize = Twinsize type Ttermio = struct { Fc_iflag uint16 Fc_oflag uint16 Fc_cflag uint16 Fc_lflag uint16 Fc_line uint8 Fc_cc [8]uint8 } type termio = Ttermio type Tintptr_t = int32 type intptr_t = Tintptr_t type Tsocklen_t = uint32 type socklen_t = Tsocklen_t const __PC_LINK_MAX = 0 const __PC_MAX_CANON = 1 const __PC_MAX_INPUT = 2 const __PC_NAME_MAX = 3 const __PC_PATH_MAX = 4 const __PC_PIPE_BUF = 5 const __PC_CHOWN_RESTRICTED = 6 const __PC_NO_TRUNC = 7 const __PC_VDISABLE = 8 const __PC_SYNC_IO = 9 const __PC_ASYNC_IO = 10 const __PC_PRIO_IO = 11 const __PC_SOCK_MAXBUF = 12 const __PC_FILESIZEBITS = 13 const __PC_REC_INCR_XFER_SIZE = 14 const __PC_REC_MAX_XFER_SIZE = 15 const __PC_REC_MIN_XFER_SIZE = 16 const __PC_REC_XFER_ALIGN = 17 const __PC_ALLOC_SIZE_MIN = 18 const __PC_SYMLINK_MAX = 19 const __PC_2_SYMLINKS = 20 const __SC_ARG_MAX = 0 const __SC_CHILD_MAX = 1 const __SC_CLK_TCK = 2 const __SC_NGROUPS_MAX = 3 const __SC_OPEN_MAX = 4 const __SC_STREAM_MAX = 5 const __SC_TZNAME_MAX = 6 const __SC_JOB_CONTROL = 7 const __SC_SAVED_IDS = 8 const __SC_REALTIME_SIGNALS = 9 const __SC_PRIORITY_SCHEDULING = 10 const __SC_TIMERS = 11 const __SC_ASYNCHRONOUS_IO = 12 const __SC_PRIORITIZED_IO = 13 const __SC_SYNCHRONIZED_IO = 14 const __SC_FSYNC = 15 const __SC_MAPPED_FILES = 16 const __SC_MEMLOCK = 17 const __SC_MEMLOCK_RANGE = 18 const __SC_MEMORY_PROTECTION = 19 const __SC_MESSAGE_PASSING = 20 const __SC_SEMAPHORES = 21 const __SC_SHARED_MEMORY_OBJECTS = 22 const __SC_AIO_LISTIO_MAX = 23 const __SC_AIO_MAX = 24 const __SC_AIO_PRIO_DELTA_MAX = 25 const __SC_DELAYTIMER_MAX = 26 const __SC_MQ_OPEN_MAX = 27 const __SC_MQ_PRIO_MAX = 28 const __SC_VERSION = 29 const __SC_PAGESIZE = 30 const __SC_RTSIG_MAX = 31 const __SC_SEM_NSEMS_MAX = 32 const __SC_SEM_VALUE_MAX = 33 const __SC_SIGQUEUE_MAX = 34 const __SC_TIMER_MAX = 35 const __SC_BC_BASE_MAX = 36 const __SC_BC_DIM_MAX = 37 const __SC_BC_SCALE_MAX = 38 const __SC_BC_STRING_MAX = 39 const __SC_COLL_WEIGHTS_MAX = 40 const __SC_EQUIV_CLASS_MAX = 41 const __SC_EXPR_NEST_MAX = 42 const __SC_LINE_MAX = 43 const __SC_RE_DUP_MAX = 44 const __SC_CHARCLASS_NAME_MAX = 45 const __SC_2_VERSION = 46 const __SC_2_C_BIND = 47 const __SC_2_C_DEV = 48 const __SC_2_FORT_DEV = 49 const __SC_2_FORT_RUN = 50 const __SC_2_SW_DEV = 51 const __SC_2_LOCALEDEF = 52 const __SC_PII = 53 const __SC_PII_XTI = 54 const __SC_PII_SOCKET = 55 const __SC_PII_INTERNET = 56 const __SC_PII_OSI = 57 const __SC_POLL = 58 const __SC_SELECT = 59 const __SC_UIO_MAXIOV = 60 const __SC_IOV_MAX = 60 const __SC_PII_INTERNET_STREAM = 61 const __SC_PII_INTERNET_DGRAM = 62 const __SC_PII_OSI_COTS = 63 const __SC_PII_OSI_CLTS = 64 const __SC_PII_OSI_M = 65 const __SC_T_IOV_MAX = 66 const __SC_THREADS = 67 const __SC_THREAD_SAFE_FUNCTIONS = 68 const __SC_GETGR_R_SIZE_MAX = 69 const __SC_GETPW_R_SIZE_MAX = 70 const __SC_LOGIN_NAME_MAX = 71 const __SC_TTY_NAME_MAX = 72 const __SC_THREAD_DESTRUCTOR_ITERATIONS = 73 const __SC_THREAD_KEYS_MAX = 74 const __SC_THREAD_STACK_MIN = 75 const __SC_THREAD_THREADS_MAX = 76 const __SC_THREAD_ATTR_STACKADDR = 77 const __SC_THREAD_ATTR_STACKSIZE = 78 const __SC_THREAD_PRIORITY_SCHEDULING = 79 const __SC_THREAD_PRIO_INHERIT = 80 const __SC_THREAD_PRIO_PROTECT = 81 const __SC_THREAD_PROCESS_SHARED = 82 const __SC_NPROCESSORS_CONF = 83 const __SC_NPROCESSORS_ONLN = 84 const __SC_PHYS_PAGES = 85 const __SC_AVPHYS_PAGES = 86 const __SC_ATEXIT_MAX = 87 const __SC_PASS_MAX = 88 const __SC_XOPEN_VERSION = 89 const __SC_XOPEN_XCU_VERSION = 90 const __SC_XOPEN_UNIX = 91 const __SC_XOPEN_CRYPT = 92 const __SC_XOPEN_ENH_I18N = 93 const __SC_XOPEN_SHM = 94 const __SC_2_CHAR_TERM = 95 const __SC_2_C_VERSION = 96 const __SC_2_UPE = 97 const __SC_XOPEN_XPG2 = 98 const __SC_XOPEN_XPG3 = 99 const __SC_XOPEN_XPG4 = 100 const __SC_CHAR_BIT = 101 const __SC_CHAR_MAX = 102 const __SC_CHAR_MIN = 103 const __SC_INT_MAX = 104 const __SC_INT_MIN = 105 const __SC_LONG_BIT = 106 const __SC_WORD_BIT = 107 const __SC_MB_LEN_MAX = 108 const __SC_NZERO = 109 const __SC_SSIZE_MAX = 110 const __SC_SCHAR_MAX = 111 const __SC_SCHAR_MIN = 112 const __SC_SHRT_MAX = 113 const __SC_SHRT_MIN = 114 const __SC_UCHAR_MAX = 115 const __SC_UINT_MAX = 116 const __SC_ULONG_MAX = 117 const __SC_USHRT_MAX = 118 const __SC_NL_ARGMAX = 119 const __SC_NL_LANGMAX = 120 const __SC_NL_MSGMAX = 121 const __SC_NL_NMAX = 122 const __SC_NL_SETMAX = 123 const __SC_NL_TEXTMAX = 124 const __SC_XBS5_ILP32_OFF32 = 125 const __SC_XBS5_ILP32_OFFBIG = 126 const __SC_XBS5_LP64_OFF64 = 127 const __SC_XBS5_LPBIG_OFFBIG = 128 const __SC_XOPEN_LEGACY = 129 const __SC_XOPEN_REALTIME = 130 const __SC_XOPEN_REALTIME_THREADS = 131 const __SC_ADVISORY_INFO = 132 const __SC_BARRIERS = 133 const __SC_BASE = 134 const __SC_C_LANG_SUPPORT = 135 const __SC_C_LANG_SUPPORT_R = 136 const __SC_CLOCK_SELECTION = 137 const __SC_CPUTIME = 138 const __SC_THREAD_CPUTIME = 139 const __SC_DEVICE_IO = 140 const __SC_DEVICE_SPECIFIC = 141 const __SC_DEVICE_SPECIFIC_R = 142 const __SC_FD_MGMT = 143 const __SC_FIFO = 144 const __SC_PIPE = 145 const __SC_FILE_ATTRIBUTES = 146 const __SC_FILE_LOCKING = 147 const __SC_FILE_SYSTEM = 148 const __SC_MONOTONIC_CLOCK = 149 const __SC_MULTI_PROCESS = 150 const __SC_SINGLE_PROCESS = 151 const __SC_NETWORKING = 152 const __SC_READER_WRITER_LOCKS = 153 const __SC_SPIN_LOCKS = 154 const __SC_REGEXP = 155 const __SC_REGEX_VERSION = 156 const __SC_SHELL = 157 const __SC_SIGNALS = 158 const __SC_SPAWN = 159 const __SC_SPORADIC_SERVER = 160 const __SC_THREAD_SPORADIC_SERVER = 161 const __SC_SYSTEM_DATABASE = 162 const __SC_SYSTEM_DATABASE_R = 163 const __SC_TIMEOUTS = 164 const __SC_TYPED_MEMORY_OBJECTS = 165 const __SC_USER_GROUPS = 166 const __SC_USER_GROUPS_R = 167 const __SC_2_PBS = 168 const __SC_2_PBS_ACCOUNTING = 169 const __SC_2_PBS_LOCATE = 170 const __SC_2_PBS_MESSAGE = 171 const __SC_2_PBS_TRACK = 172 const __SC_SYMLOOP_MAX = 173 const __SC_STREAMS = 174 const __SC_2_PBS_CHECKPOINT = 175 const __SC_V6_ILP32_OFF32 = 176 const __SC_V6_ILP32_OFFBIG = 177 const __SC_V6_LP64_OFF64 = 178 const __SC_V6_LPBIG_OFFBIG = 179 const __SC_HOST_NAME_MAX = 180 const __SC_TRACE = 181 const __SC_TRACE_EVENT_FILTER = 182 const __SC_TRACE_INHERIT = 183 const __SC_TRACE_LOG = 184 const __SC_LEVEL1_ICACHE_SIZE = 185 const __SC_LEVEL1_ICACHE_ASSOC = 186 const __SC_LEVEL1_ICACHE_LINESIZE = 187 const __SC_LEVEL1_DCACHE_SIZE = 188 const __SC_LEVEL1_DCACHE_ASSOC = 189 const __SC_LEVEL1_DCACHE_LINESIZE = 190 const __SC_LEVEL2_CACHE_SIZE = 191 const __SC_LEVEL2_CACHE_ASSOC = 192 const __SC_LEVEL2_CACHE_LINESIZE = 193 const __SC_LEVEL3_CACHE_SIZE = 194 const __SC_LEVEL3_CACHE_ASSOC = 195 const __SC_LEVEL3_CACHE_LINESIZE = 196 const __SC_LEVEL4_CACHE_SIZE = 197 const __SC_LEVEL4_CACHE_ASSOC = 198 const __SC_LEVEL4_CACHE_LINESIZE = 199 const __SC_IPV6 = 235 const __SC_RAW_SOCKETS = 236 const __SC_V7_ILP32_OFF32 = 237 const __SC_V7_ILP32_OFFBIG = 238 const __SC_V7_LP64_OFF64 = 239 const __SC_V7_LPBIG_OFFBIG = 240 const __SC_SS_REPL_MAX = 241 const __SC_TRACE_EVENT_NAME_MAX = 242 const __SC_TRACE_NAME_MAX = 243 const __SC_TRACE_SYS_MAX = 244 const __SC_TRACE_USER_EVENT_MAX = 245 const __SC_XOPEN_STREAMS = 246 const __SC_THREAD_ROBUST_PRIO_INHERIT = 247 const __SC_THREAD_ROBUST_PRIO_PROTECT = 248 const __SC_MINSIGSTKSZ = 249 const __SC_SIGSTKSZ = 250 const __CS_PATH = 0 const __CS_V6_WIDTH_RESTRICTED_ENVS = 1 const __CS_GNU_LIBC_VERSION = 2 const __CS_GNU_LIBPTHREAD_VERSION = 3 const __CS_V5_WIDTH_RESTRICTED_ENVS = 4 const __CS_V7_WIDTH_RESTRICTED_ENVS = 5 const __CS_LFS_CFLAGS = 1000 const __CS_LFS_LDFLAGS = 1001 const __CS_LFS_LIBS = 1002 const __CS_LFS_LINTFLAGS = 1003 const __CS_LFS64_CFLAGS = 1004 const __CS_LFS64_LDFLAGS = 1005 const __CS_LFS64_LIBS = 1006 const __CS_LFS64_LINTFLAGS = 1007 const __CS_XBS5_ILP32_OFF32_CFLAGS = 1100 const __CS_XBS5_ILP32_OFF32_LDFLAGS = 1101 const __CS_XBS5_ILP32_OFF32_LIBS = 1102 const __CS_XBS5_ILP32_OFF32_LINTFLAGS = 1103 const __CS_XBS5_ILP32_OFFBIG_CFLAGS = 1104 const __CS_XBS5_ILP32_OFFBIG_LDFLAGS = 1105 const __CS_XBS5_ILP32_OFFBIG_LIBS = 1106 const __CS_XBS5_ILP32_OFFBIG_LINTFLAGS = 1107 const __CS_XBS5_LP64_OFF64_CFLAGS = 1108 const __CS_XBS5_LP64_OFF64_LDFLAGS = 1109 const __CS_XBS5_LP64_OFF64_LIBS = 1110 const __CS_XBS5_LP64_OFF64_LINTFLAGS = 1111 const __CS_XBS5_LPBIG_OFFBIG_CFLAGS = 1112 const __CS_XBS5_LPBIG_OFFBIG_LDFLAGS = 1113 const __CS_XBS5_LPBIG_OFFBIG_LIBS = 1114 const __CS_XBS5_LPBIG_OFFBIG_LINTFLAGS = 1115 const __CS_POSIX_V6_ILP32_OFF32_CFLAGS = 1116 const __CS_POSIX_V6_ILP32_OFF32_LDFLAGS = 1117 const __CS_POSIX_V6_ILP32_OFF32_LIBS = 1118 const __CS_POSIX_V6_ILP32_OFF32_LINTFLAGS = 1119 const __CS_POSIX_V6_ILP32_OFFBIG_CFLAGS = 1120 const __CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS = 1121 const __CS_POSIX_V6_ILP32_OFFBIG_LIBS = 1122 const __CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS = 1123 const __CS_POSIX_V6_LP64_OFF64_CFLAGS = 1124 const __CS_POSIX_V6_LP64_OFF64_LDFLAGS = 1125 const __CS_POSIX_V6_LP64_OFF64_LIBS = 1126 const __CS_POSIX_V6_LP64_OFF64_LINTFLAGS = 1127 const __CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS = 1128 const __CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS = 1129 const __CS_POSIX_V6_LPBIG_OFFBIG_LIBS = 1130 const __CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS = 1131 const __CS_POSIX_V7_ILP32_OFF32_CFLAGS = 1132 const __CS_POSIX_V7_ILP32_OFF32_LDFLAGS = 1133 const __CS_POSIX_V7_ILP32_OFF32_LIBS = 1134 const __CS_POSIX_V7_ILP32_OFF32_LINTFLAGS = 1135 const __CS_POSIX_V7_ILP32_OFFBIG_CFLAGS = 1136 const __CS_POSIX_V7_ILP32_OFFBIG_LDFLAGS = 1137 const __CS_POSIX_V7_ILP32_OFFBIG_LIBS = 1138 const __CS_POSIX_V7_ILP32_OFFBIG_LINTFLAGS = 1139 const __CS_POSIX_V7_LP64_OFF64_CFLAGS = 1140 const __CS_POSIX_V7_LP64_OFF64_LDFLAGS = 1141 const __CS_POSIX_V7_LP64_OFF64_LIBS = 1142 const __CS_POSIX_V7_LP64_OFF64_LINTFLAGS = 1143 const __CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS = 1144 const __CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS = 1145 const __CS_POSIX_V7_LPBIG_OFFBIG_LIBS = 1146 const __CS_POSIX_V7_LPBIG_OFFBIG_LINTFLAGS = 1147 const __CS_V6_ENV = 1148 const __CS_V7_ENV = 1149 type Ttimezone = struct { Ftz_minuteswest int32 Ftz_dsttime int32 } type timezone = Ttimezone type ___itimer_which = int32 const _ITIMER_REAL = 0 const _ITIMER_VIRTUAL = 1 const _ITIMER_PROF = 2 type Titimerval = struct { Fit_interval Ttimeval Fit_value Ttimeval } type itimerval = Titimerval type t__itimer_which_t = int32 type Terror_t = int32 type error_t = Terror_t /* ** Try to determine if gethostuuid() is available based on standard ** macros. This might sometimes compute the wrong value for some ** obscure platforms. For those cases, simply compile with one of ** the following: ** ** -DHAVE_GETHOSTUUID=0 ** -DHAVE_GETHOSTUUID=1 ** ** None if this matters except when building on Apple products with ** -DSQLITE_ENABLE_LOCKING_STYLE. */ /* ** Allowed values of unixFile.fsFlags */ /* ** If we are to be thread-safe, include the pthreads header. */ /* # include */ /* ** Default permissions when creating a new file */ /* ** Default permissions when creating auto proxy dir */ /* ** Maximum supported path-length. */ /* ** Maximum supported symbolic links */ /* ** Remove and stub certain info for WASI (WebAssembly System ** Interface) builds. */ /* Always cast the getpid() return type for compatibility with ** kernel modules in VxWorks. */ /* ** Only set the lastErrno if the error code is a real error and not ** a normal expected return code of SQLITE_BUSY or SQLITE_OK */ // C documentation // // /* Forward references */ type TunixShm = struct { FpShmNode uintptr FpNext uintptr FhasMutex Tu8 Fid Tu8 FsharedMask Tu16 FexclMask Tu16 } type unixShm = TunixShm /* Connection shared memory */ type TunixShmNode = struct { FpInode uintptr FpShmMutex uintptr FzFilename uintptr FhShm int32 FszRegion int32 FnRegion Tu16 FisReadonly Tu8 FisUnlocked Tu8 FapRegion uintptr FnRef int32 FpFirst uintptr FaLock [8]int32 } type unixShmNode = TunixShmNode /* Shared memory instance */ type TunixInodeInfo = struct { F__ccgo_align [0]uint32 FfileId TunixFileId FpLockMutex uintptr FnShared int32 FnLock int32 FeFileLock uint8 FbProcessLock uint8 FpUnused uintptr FnRef int32 FpShmNode uintptr FpNext uintptr FpPrev uintptr F__ccgo_pad11 [4]byte } type unixInodeInfo = TunixInodeInfo /* An i-node */ type TUnixUnusedFd = struct { Ffd int32 Fflags int32 FpNext uintptr } type UnixUnusedFd = TUnixUnusedFd /* An unused file descriptor */ /* ** Sometimes, after a file handle is closed by SQLite, the file descriptor ** cannot be closed immediately. In these cases, instances of the following ** structure are used to store the file descriptor while waiting for an ** opportunity to either close or reuse it. */ type TUnixUnusedFd1 = struct { Ffd int32 Fflags int32 FpNext uintptr } type UnixUnusedFd1 = TUnixUnusedFd1 // C documentation // // /* // ** The unixFile structure is subclass of sqlite3_file specific to the unix // ** VFS implementations. // */ type TunixFile = struct { F__ccgo_align [0]uint32 FpMethod uintptr FpVfs uintptr FpInode uintptr Fh int32 FeFileLock uint8 FctrlFlags uint16 FlastErrno int32 FlockingContext uintptr FpPreallocatedUnused uintptr FzPath uintptr FpShm uintptr FszChunk int32 FnFetchOut int32 FmmapSize Tsqlite3_int64 FmmapSizeActual Tsqlite3_int64 FmmapSizeMax Tsqlite3_int64 FpMapRegion uintptr FsectorSize int32 FdeviceCharacteristics int32 F__ccgo_pad19 [4]byte } type unixFile = TunixFile type TunixFile1 = struct { F__ccgo_align [0]uint32 FpMethod uintptr FpVfs uintptr FpInode uintptr Fh int32 FeFileLock uint8 FctrlFlags uint16 FlastErrno int32 FlockingContext uintptr FpPreallocatedUnused uintptr FzPath uintptr FpShm uintptr FszChunk int32 FnFetchOut int32 FmmapSize Tsqlite3_int64 FmmapSizeActual Tsqlite3_int64 FmmapSizeMax Tsqlite3_int64 FpMapRegion uintptr FsectorSize int32 FdeviceCharacteristics int32 F__ccgo_pad19 [4]byte } type unixFile1 = TunixFile1 // C documentation // // /* This variable holds the process id (pid) from when the xRandomness() // ** method was called. If xOpen() is called from a different process id, // ** indicating that a fork() has occurred, the PRNG will be reset. // */ var _randomnessPid = int32(0) /* ** Allowed values for the unixFile.ctrlFlags bitmask: */ /* ** Include code that is common to all os_*.c files */ /* #include "os_common.h" */ /* ** Define various macros that are missing from some systems. */ /* ** The threadid macro resolves to the thread-id or to 0. Used for ** testing and debugging only. */ /* ** HAVE_MREMAP defaults to true on Linux and false everywhere else. */ /* ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek() ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined. */ /* ** Linux-specific IOCTL magic numbers used for controlling F2FS */ // C documentation // // /* // ** Different Unix systems declare open() in different ways. Same use // ** open(const char*,int,mode_t). Others use open(const char*,int,...). // ** The difference is important when using a pointer to the function. // ** // ** The safest way to deal with the problem is to always use this wrapper // ** which always has the same well-defined interface. // */ func _posixOpen(tls *libc.TLS, zFile uintptr, flags int32, mode int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) return libc.Xopen(tls, zFile, flags, libc.VaList(bp+8, mode)) } // C documentation // // /* // ** Many system calls are accessed through pointer-to-functions so that // ** they may be overridden at runtime to facilitate fault injection during // ** testing and sandboxing. The following array holds the names and pointers // ** to all overrideable system calls. // */ type Tunix_syscall = struct { FzName uintptr FpCurrent Tsqlite3_syscall_ptr FpDefault Tsqlite3_syscall_ptr } type unix_syscall = Tunix_syscall // C documentation // // /* // ** Many system calls are accessed through pointer-to-functions so that // ** they may be overridden at runtime to facilitate fault injection during // ** testing and sandboxing. The following array holds the names and pointers // ** to all overrideable system calls. // */ var _aSyscall = [29]Tunix_syscall{ 0: { FzName: __ccgo_ts + 3393, }, 1: { FzName: __ccgo_ts + 3398, }, 2: { FzName: __ccgo_ts + 3404, }, 3: { FzName: __ccgo_ts + 3411, }, 4: { FzName: __ccgo_ts + 3418, }, 5: { FzName: __ccgo_ts + 3423, }, 6: { FzName: __ccgo_ts + 3429, }, 7: { FzName: __ccgo_ts + 3439, }, 8: { FzName: __ccgo_ts + 3445, }, 9: { FzName: __ccgo_ts + 3450, }, 10: { FzName: __ccgo_ts + 3456, }, 11: { FzName: __ccgo_ts + 3464, }, 12: { FzName: __ccgo_ts + 3470, }, 13: { FzName: __ccgo_ts + 3477, }, 14: { FzName: __ccgo_ts + 3486, }, 15: { FzName: __ccgo_ts + 3493, }, 16: { FzName: __ccgo_ts + 3503, }, 17: { FzName: __ccgo_ts + 3510, }, 18: { FzName: __ccgo_ts + 3524, }, 19: { FzName: __ccgo_ts + 3530, }, 20: { FzName: __ccgo_ts + 3536, }, 21: { FzName: __ccgo_ts + 3543, }, 22: { FzName: __ccgo_ts + 3551, }, 23: { FzName: __ccgo_ts + 3556, }, 24: { FzName: __ccgo_ts + 3563, }, 25: { FzName: __ccgo_ts + 3570, }, 26: { FzName: __ccgo_ts + 3582, }, 27: { FzName: __ccgo_ts + 3591, }, 28: { FzName: __ccgo_ts + 3597, }, } func init() { p := unsafe.Pointer(&_aSyscall) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_posixOpen) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(libc.Xclose) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(libc.Xaccess) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(libc.Xgetcwd) *(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(libc.Xstat) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(libc.Xfstat) *(*uintptr)(unsafe.Add(p, 76)) = __ccgo_fp(libc.Xftruncate) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(libc.Xfcntl) *(*uintptr)(unsafe.Add(p, 100)) = __ccgo_fp(libc.Xread) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(libc.Xpread) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(libc.Xwrite) *(*uintptr)(unsafe.Add(p, 148)) = __ccgo_fp(libc.Xpwrite) *(*uintptr)(unsafe.Add(p, 172)) = __ccgo_fp(libc.Xfchmod) *(*uintptr)(unsafe.Add(p, 196)) = __ccgo_fp(libc.Xunlink) *(*uintptr)(unsafe.Add(p, 208)) = __ccgo_fp(_openDirectory) *(*uintptr)(unsafe.Add(p, 220)) = __ccgo_fp(libc.Xmkdir) *(*uintptr)(unsafe.Add(p, 232)) = __ccgo_fp(libc.Xrmdir) *(*uintptr)(unsafe.Add(p, 244)) = __ccgo_fp(libc.Xfchown) *(*uintptr)(unsafe.Add(p, 256)) = __ccgo_fp(libc.Xgeteuid) *(*uintptr)(unsafe.Add(p, 268)) = __ccgo_fp(libc.Xmmap) *(*uintptr)(unsafe.Add(p, 280)) = __ccgo_fp(libc.Xmunmap) *(*uintptr)(unsafe.Add(p, 292)) = __ccgo_fp(libc.Xmremap) *(*uintptr)(unsafe.Add(p, 304)) = __ccgo_fp(_unixGetpagesize) *(*uintptr)(unsafe.Add(p, 316)) = __ccgo_fp(libc.Xreadlink) *(*uintptr)(unsafe.Add(p, 328)) = __ccgo_fp(libc.Xlstat) } /* End of the overrideable system calls */ // C documentation // // /* // ** On some systems, calls to fchown() will trigger a message in a security // ** log if they come from non-root processes. So avoid calling fchown() if // ** we are not running as root. // */ func _robustFchown(tls *libc.TLS, fd int32, uid Tuid_t, gid Tgid_t) (r int32) { var v1 int32 _ = v1 if (*(*func(*libc.TLS) Tuid_t)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(21)].FpCurrent})))(tls) != 0 { v1 = 0 } else { v1 = (*(*func(*libc.TLS, int32, Tuid_t, Tgid_t) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(20)].FpCurrent})))(tls, fd, uid, gid) } return v1 } // C documentation // // /* // ** This is the xSetSystemCall() method of sqlite3_vfs for all of the // ** "unix" VFSes. Return SQLITE_OK upon successfully updating the // ** system call pointer, or SQLITE_NOTFOUND if there is no configurable // ** system call named zName. // */ func _unixSetSystemCall(tls *libc.TLS, pNotUsed uintptr, zName uintptr, pNewFunc Tsqlite3_syscall_ptr) (r int32) { var i uint32 var rc int32 _, _ = i, rc rc = int32(SQLITE_NOTFOUND) _ = pNotUsed if zName == uintptr(0) { /* If no zName is given, restore all system calls to their default ** settings and return NULL */ rc = SQLITE_OK i = uint32(0) for { if !(i < libc.Uint32FromInt64(348)/libc.Uint32FromInt64(12)) { break } if _aSyscall[i].FpDefault != 0 { _aSyscall[i].FpCurrent = _aSyscall[i].FpDefault } goto _1 _1: ; i++ } } else { /* If zName is specified, operate on only the one system call ** specified. */ i = uint32(0) for { if !(i < libc.Uint32FromInt64(348)/libc.Uint32FromInt64(12)) { break } if libc.Xstrcmp(tls, zName, _aSyscall[i].FzName) == 0 { if _aSyscall[i].FpDefault == uintptr(0) { _aSyscall[i].FpDefault = _aSyscall[i].FpCurrent } rc = SQLITE_OK if pNewFunc == uintptr(0) { pNewFunc = _aSyscall[i].FpDefault } _aSyscall[i].FpCurrent = pNewFunc break } goto _2 _2: ; i++ } } return rc } // C documentation // // /* // ** Return the value of a system call. Return NULL if zName is not a // ** recognized system call name. NULL is also returned if the system call // ** is currently undefined. // */ func _unixGetSystemCall(tls *libc.TLS, pNotUsed uintptr, zName uintptr) (r Tsqlite3_syscall_ptr) { var i uint32 _ = i _ = pNotUsed i = uint32(0) for { if !(i < libc.Uint32FromInt64(348)/libc.Uint32FromInt64(12)) { break } if libc.Xstrcmp(tls, zName, _aSyscall[i].FzName) == 0 { return _aSyscall[i].FpCurrent } goto _1 _1: ; i++ } return uintptr(0) } // C documentation // // /* // ** Return the name of the first system call after zName. If zName==NULL // ** then return the name of the first system call. Return NULL if zName // ** is the last system call or if zName is not the name of a valid // ** system call. // */ func _unixNextSystemCall(tls *libc.TLS, p uintptr, zName uintptr) (r uintptr) { var i int32 _ = i i = -int32(1) _ = p if zName != 0 { i = 0 for { if !(i < int32(libc.Uint32FromInt64(348)/libc.Uint32FromInt64(12))-libc.Int32FromInt32(1)) { break } if libc.Xstrcmp(tls, zName, _aSyscall[i].FzName) == 0 { break } goto _1 _1: ; i++ } } i++ for { if !(i < int32(libc.Uint32FromInt64(348)/libc.Uint32FromInt64(12))) { break } if _aSyscall[i].FpCurrent != uintptr(0) { return _aSyscall[i].FzName } goto _2 _2: ; i++ } return uintptr(0) } /* ** Do not accept any file descriptor less than this value, in order to avoid ** opening database file using file descriptors that are commonly used for ** standard input, output, and error. */ // C documentation // // /* // ** Invoke open(). Do so multiple times, until it either succeeds or // ** fails for some reason other than EINTR. // ** // ** If the file creation mode "m" is 0 then set it to the default for // ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally // ** 0644) as modified by the system umask. If m is not 0, then // ** make the file creation mode be exactly m ignoring the umask. // ** // ** The m parameter will be non-zero only when creating -wal, -journal, // ** and -shm files. We want those files to have *exactly* the same // ** permissions as their original database, unadulterated by the umask. // ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a // ** transaction crashes and leaves behind hot journals, then any // ** process that is able to write to the database will also be able to // ** recover the hot journals. // */ func _robust_open(tls *libc.TLS, z uintptr, f int32, m Tmode_t) (r int32) { bp := tls.Alloc(128) defer tls.Free(128) var fd int32 var m2 Tmode_t var v1 uint32 var _ /* statbuf at bp+0 */ Tstat _, _, _ = fd, m2, v1 if m != 0 { v1 = m } else { v1 = uint32(SQLITE_DEFAULT_FILE_PERMISSIONS) } m2 = v1 for int32(1) != 0 { fd = (*(*func(*libc.TLS, uintptr, int32, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[0].FpCurrent})))(tls, z, f|int32(__O_CLOEXEC), int32(m2)) if fd < 0 { if *(*int32)(unsafe.Pointer(libc.X__errno_location(tls))) == int32(EINTR) { continue } break } if fd >= int32(SQLITE_MINIMUM_FILE_DESCRIPTOR) { break } if f&(libc.Int32FromInt32(O_EXCL)|libc.Int32FromInt32(O_CREAT)) == libc.Int32FromInt32(O_EXCL)|libc.Int32FromInt32(O_CREAT) { (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(16)].FpCurrent})))(tls, z) } (*(*func(*libc.TLS, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(1)].FpCurrent})))(tls, fd) Xsqlite3_log(tls, int32(SQLITE_WARNING), __ccgo_ts+3603, libc.VaList(bp+112, z, fd)) fd = -int32(1) if (*(*func(*libc.TLS, uintptr, int32, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[0].FpCurrent})))(tls, __ccgo_ts+3646, O_RDONLY, int32(m)) < 0 { break } } if fd >= 0 { if m != uint32(0) { if (*(*func(*libc.TLS, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(5)].FpCurrent})))(tls, fd, bp) == 0 && (*(*Tstat)(unsafe.Pointer(bp))).Fst_size == 0 && (*(*Tstat)(unsafe.Pointer(bp))).Fst_mode&uint32(0777) != m { (*(*func(*libc.TLS, int32, Tmode_t) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(14)].FpCurrent})))(tls, fd, m) } } } return fd } // C documentation // // /* // ** Helper functions to obtain and relinquish the global mutex. The // ** global mutex is used to protect the unixInodeInfo and // ** vxworksFileId objects used by this file, all of which may be // ** shared by multiple threads. // ** // ** Function unixMutexHeld() is used to assert() that the global mutex // ** is held when required. This function is only used as part of assert() // ** statements. e.g. // ** // ** unixEnterMutex() // ** assert( unixMutexHeld() ); // ** unixEnterLeave() // ** // ** To prevent deadlock, the global unixBigLock must must be acquired // ** before the unixInodeInfo.pLockMutex mutex, if both are held. It is // ** OK to get the pLockMutex without holding unixBigLock first, but if // ** that happens, the unixBigLock mutex must not be acquired until after // ** pLockMutex is released. // ** // ** OK: enter(unixBigLock), enter(pLockInfo) // ** OK: enter(unixBigLock) // ** OK: enter(pLockInfo) // ** ERROR: enter(pLockInfo), enter(unixBigLock) // */ var _unixBigLock = uintptr(0) func _unixEnterMutex(tls *libc.TLS) { _ = libc.Int32FromInt32(0) /* Not a recursive mutex */ Xsqlite3_mutex_enter(tls, _unixBigLock) } func _unixLeaveMutex(tls *libc.TLS) { _ = libc.Int32FromInt32(0) Xsqlite3_mutex_leave(tls, _unixBigLock) } // C documentation // // /* // ** Retry ftruncate() calls that fail due to EINTR // ** // ** All calls to ftruncate() within this file should be made through // ** this wrapper. On the Android platform, bypassing the logic below // ** could lead to a corrupt database. // */ func _robust_ftruncate(tls *libc.TLS, h int32, sz Tsqlite3_int64) (r int32) { var rc int32 _ = rc for cond := true; cond; cond = rc < 0 && *(*int32)(unsafe.Pointer(libc.X__errno_location(tls))) == int32(EINTR) { rc = (*(*func(*libc.TLS, int32, Toff_t) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(6)].FpCurrent})))(tls, h, sz) } return rc } // C documentation // // /* // ** This routine translates a standard POSIX errno code into something // ** useful to the clients of the sqlite3 functions. Specifically, it is // ** intended to translate a variety of "try again" errors into SQLITE_BUSY // ** and a variety of "please close the file descriptor NOW" errors into // ** SQLITE_IOERR // ** // ** Errors during initialization of locks, or file system support for locks, // ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. // */ func _sqliteErrorFromPosixError(tls *libc.TLS, posixError int32, sqliteIOErr int32) (r int32) { _ = libc.Int32FromInt32(0) switch posixError { case int32(EACCES): fallthrough case int32(EAGAIN): fallthrough case int32(ETIMEDOUT): fallthrough case int32(EBUSY): fallthrough case int32(EINTR): fallthrough case int32(ENOLCK): /* random NFS retry error, unless during file system support * introspection, in which it actually means what it says */ return int32(SQLITE_BUSY) case int32(EPERM): return int32(SQLITE_PERM) default: return sqliteIOErr } return r } /****************************************************************************** ****************** Begin Unique File ID Utility Used By VxWorks *************** ** ** On most versions of unix, we can get a unique ID for a file by concatenating ** the device number and the inode number. But this does not work on VxWorks. ** On VxWorks, a unique file id must be based on the canonical filename. ** ** A pointer to an instance of the following structure can be used as a ** unique file ID in VxWorks. Each instance of this structure contains ** a copy of the canonical filename. There is also a reference count. ** The structure is reclaimed when the number of pointers to it drops to ** zero. ** ** There are never very many files open at one time and lookups are not ** a performance-critical path, so it is sufficient to put these ** structures on a linked list. */ type TvxworksFileId = struct { FpNext uintptr FnRef int32 FnName int32 FzCanonicalName uintptr } type vxworksFileId = TvxworksFileId /*************** End of Unique File ID Utility Used By VxWorks **************** ******************************************************************************/ /****************************************************************************** *************************** Posix Advisory Locking **************************** ** ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) ** section 6.5.2.2 lines 483 through 490 specify that when a process ** sets or clears a lock, that operation overrides any prior locks set ** by the same process. It does not explicitly say so, but this implies ** that it overrides locks set by the same process using a different ** file descriptor. Consider this test case: ** ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); ** ** Suppose ./file1 and ./file2 are really the same file (because ** one is a hard or symbolic link to the other) then if you set ** an exclusive lock on fd1, then try to get an exclusive lock ** on fd2, it works. I would have expected the second lock to ** fail since there was already a lock on the file due to fd1. ** But not so. Since both locks came from the same process, the ** second overrides the first, even though they were on different ** file descriptors opened on different file names. ** ** This means that we cannot use POSIX locks to synchronize file access ** among competing threads of the same process. POSIX locks will work fine ** to synchronize access for threads in separate processes, but not ** threads within the same process. ** ** To work around the problem, SQLite has to manage file locks internally ** on its own. Whenever a new database is opened, we have to find the ** specific inode of the database file (the inode is determined by the ** st_dev and st_ino fields of the stat structure that fstat() fills in) ** and check for locks already existing on that inode. When locks are ** created or removed, we have to look at our own internal record of the ** locks to see if another thread has previously set a lock on that same ** inode. ** ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. ** For VxWorks, we have to use the alternative unique ID system based on ** canonical filename and implemented in the previous division.) ** ** The sqlite3_file structure for POSIX is no longer just an integer file ** descriptor. It is now a structure that holds the integer file ** descriptor and a pointer to a structure that describes the internal ** locks on the corresponding inode. There is one locking structure ** per inode, so if the same inode is opened twice, both unixFile structures ** point to the same locking structure. The locking structure keeps ** a reference count (so we will know when to delete it) and a "cnt" ** field that tells us its internal lock status. cnt==0 means the ** file is unlocked. cnt==-1 means the file has an exclusive lock. ** cnt>0 means there are cnt shared locks on the file. ** ** Any attempt to lock or unlock a file first checks the locking ** structure. The fcntl() system call is only invoked to set a ** POSIX lock if the internal lock structure transitions between ** a locked and an unlocked state. ** ** But wait: there are yet more problems with POSIX advisory locks. ** ** If you close a file descriptor that points to a file that has locks, ** all locks on that file that are owned by the current process are ** released. To work around this problem, each unixInodeInfo object ** maintains a count of the number of pending locks on the inode. ** When an attempt is made to close an unixFile, if there are ** other unixFile open on the same inode that are holding locks, the call ** to close() the file descriptor is deferred until all of the locks clear. ** The unixInodeInfo structure keeps a list of file descriptors that need to ** be closed and that list is walked (and cleared) when the last lock ** clears. ** ** Yet another problem: LinuxThreads do not play well with posix locks. ** ** Many older versions of linux use the LinuxThreads library which is ** not posix compliant. Under LinuxThreads, a lock created by thread ** A cannot be modified or overridden by a different thread B. ** Only thread A can modify the lock. Locking behavior is correct ** if the application uses the newer Native Posix Thread Library (NPTL) ** on linux - with NPTL a lock created by thread A can override locks ** in thread B. But there is no way to know at compile-time which ** threading library is being used. So there is no way to know at ** compile-time whether or not thread A can override locks on thread B. ** One has to do a run-time check to discover the behavior of the ** current process. ** ** SQLite used to support LinuxThreads. But support for LinuxThreads ** was dropped beginning with version 3.7.0. SQLite will still work with ** LinuxThreads provided that (1) there is no more than one connection ** per database file in the same process and (2) database connections ** do not move across threads. */ /* ** An instance of the following structure serves as the key used ** to locate a particular unixInodeInfo object. */ type TunixFileId = struct { F__ccgo_align [0]uint32 Fdev Tdev_t Fino Tu64 } type unixFileId = TunixFileId /* ** An instance of the following structure is allocated for each open ** inode. ** ** A single inode can have multiple file descriptors, so each unixFile ** structure contains a pointer to an instance of this object and this ** object keeps a count of the number of unixFile pointing to it. ** ** Mutex rules: ** ** (1) Only the pLockMutex mutex must be held in order to read or write ** any of the locking fields: ** nShared, nLock, eFileLock, bProcessLock, pUnused ** ** (2) When nRef>0, then the following fields are unchanging and can ** be read (but not written) without holding any mutex: ** fileId, pLockMutex ** ** (3) With the exceptions above, all the fields may only be read ** or written while holding the global unixBigLock mutex. ** ** Deadlock prevention: The global unixBigLock mutex may not ** be acquired while holding the pLockMutex mutex. If both unixBigLock ** and pLockMutex are needed, then unixBigLock must be acquired first. */ type TunixInodeInfo1 = struct { F__ccgo_align [0]uint32 FfileId TunixFileId FpLockMutex uintptr FnShared int32 FnLock int32 FeFileLock uint8 FbProcessLock uint8 FpUnused uintptr FnRef int32 FpShmNode uintptr FpNext uintptr FpPrev uintptr F__ccgo_pad11 [4]byte } type unixInodeInfo1 = TunixInodeInfo1 // C documentation // // /* // ** A lists of all unixInodeInfo objects. // ** // ** Must hold unixBigLock in order to read or write this variable. // */ var _inodeList = uintptr(0) // C documentation // // /* // ** // ** This function - unixLogErrorAtLine(), is only ever called via the macro // ** unixLogError(). // ** // ** It is invoked after an error occurs in an OS function and errno has been // ** set. It logs a message using sqlite3_log() containing the current value of // ** errno and, if possible, the human-readable equivalent from strerror() or // ** strerror_r(). // ** // ** The first argument passed to the macro should be the error code that // ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). // ** The two subsequent arguments should be the name of the OS function that // ** failed (e.g. "unlink", "open") and the associated file-system path, // ** if any. // */ func _unixLogErrorAtLine(tls *libc.TLS, errcode int32, zFunc uintptr, zPath uintptr, iLine int32) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var iErrno int32 var zErr uintptr _, _ = iErrno, zErr /* Message from strerror() or equivalent */ iErrno = *(*int32)(unsafe.Pointer(libc.X__errno_location(tls))) /* Saved syscall error number */ /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use ** the strerror() function to obtain the human-readable error message ** equivalent to errno. Otherwise, use strerror_r(). */ /* This is a threadsafe build, but strerror_r() is not available. */ zErr = __ccgo_ts + 1648 if zPath == uintptr(0) { zPath = __ccgo_ts + 1648 } Xsqlite3_log(tls, errcode, __ccgo_ts+3656, libc.VaList(bp+8, iLine, iErrno, zFunc, zPath, zErr)) return errcode } // C documentation // // /* // ** Close a file descriptor. // ** // ** We assume that close() almost always works, since it is only in a // ** very sick application or on a very sick platform that it might fail. // ** If it does fail, simply leak the file descriptor, but do log the // ** error. // ** // ** Note that it is not safe to retry close() after EINTR since the // ** file descriptor might have already been reused by another thread. // ** So we don't even try to recover from an EINTR. Just log the error // ** and move on. // */ func _robust_close(tls *libc.TLS, pFile uintptr, h int32, lineno int32) { var v1 uintptr _ = v1 if (*(*func(*libc.TLS, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(1)].FpCurrent})))(tls, h) != 0 { if pFile != 0 { v1 = (*TunixFile)(unsafe.Pointer(pFile)).FzPath } else { v1 = uintptr(0) } _unixLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(16)<lastErrno. Do this in a subroutine as that provides // ** a convenient place to set a breakpoint. // */ func _storeLastErrno(tls *libc.TLS, pFile uintptr, error1 int32) { (*TunixFile)(unsafe.Pointer(pFile)).FlastErrno = error1 } // C documentation // // /* // ** Close all file descriptors accumulated in the unixInodeInfo->pUnused list. // */ func _closePendingFds(tls *libc.TLS, pFile uintptr) { var p, pInode, pNext uintptr _, _, _ = p, pInode, pNext pInode = (*TunixFile)(unsafe.Pointer(pFile)).FpInode _ = libc.Int32FromInt32(0) p = (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpUnused for { if !(p != 0) { break } pNext = (*TUnixUnusedFd)(unsafe.Pointer(p)).FpNext _robust_close(tls, pFile, (*TUnixUnusedFd)(unsafe.Pointer(p)).Ffd, int32(39388)) Xsqlite3_free(tls, p) goto _1 _1: ; p = pNext } (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpUnused = uintptr(0) } // C documentation // // /* // ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). // ** // ** The global mutex must be held when this routine is called, but the mutex // ** on the inode being deleted must NOT be held. // */ func _releaseInodeInfo(tls *libc.TLS, pFile uintptr) { var pInode uintptr _ = pInode pInode = (*TunixFile)(unsafe.Pointer(pFile)).FpInode _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pInode != 0 { (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnRef-- if (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnRef == 0 { _ = libc.Int32FromInt32(0) Xsqlite3_mutex_enter(tls, (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpLockMutex) _closePendingFds(tls, pFile) Xsqlite3_mutex_leave(tls, (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpLockMutex) if (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpPrev != 0 { _ = libc.Int32FromInt32(0) (*TunixInodeInfo)(unsafe.Pointer((*TunixInodeInfo)(unsafe.Pointer(pInode)).FpPrev)).FpNext = (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpNext } else { _ = libc.Int32FromInt32(0) _inodeList = (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpNext } if (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpNext != 0 { _ = libc.Int32FromInt32(0) (*TunixInodeInfo)(unsafe.Pointer((*TunixInodeInfo)(unsafe.Pointer(pInode)).FpNext)).FpPrev = (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpPrev } Xsqlite3_mutex_free(tls, (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpLockMutex) Xsqlite3_free(tls, pInode) } } } // C documentation // // /* // ** Given a file descriptor, locate the unixInodeInfo object that // ** describes that file descriptor. Create a new one if necessary. The // ** return value might be uninitialized if an error occurs. // ** // ** The global mutex must held when calling this routine. // ** // ** Return an appropriate error code. // */ func _findInodeInfo(tls *libc.TLS, pFile uintptr, ppInode uintptr) (r int32) { bp := tls.Alloc(128) defer tls.Free(128) var fd, rc int32 var pInode uintptr var _ /* fileId at bp+0 */ TunixFileId var _ /* statbuf at bp+16 */ Tstat _, _, _ = fd, pInode, rc /* Low-level file information */ pInode = uintptr(0) /* Candidate unixInodeInfo object */ _ = libc.Int32FromInt32(0) /* Get low-level information about the file that we can used to ** create a unique name for the file. */ fd = (*TunixFile)(unsafe.Pointer(pFile)).Fh rc = (*(*func(*libc.TLS, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(5)].FpCurrent})))(tls, fd, bp+16) if rc != 0 { _storeLastErrno(tls, pFile, *(*int32)(unsafe.Pointer(libc.X__errno_location(tls)))) return int32(SQLITE_IOERR) } libc.Xmemset(tls, bp, 0, uint32(16)) (*(*TunixFileId)(unsafe.Pointer(bp))).Fdev = (*(*Tstat)(unsafe.Pointer(bp + 16))).Fst_dev (*(*TunixFileId)(unsafe.Pointer(bp))).Fino = (*(*Tstat)(unsafe.Pointer(bp + 16))).Fst_ino _ = libc.Int32FromInt32(0) pInode = _inodeList for pInode != 0 && libc.Xmemcmp(tls, bp, pInode, uint32(16)) != 0 { pInode = (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpNext } if pInode == uintptr(0) { pInode = Xsqlite3_malloc64(tls, uint64(56)) if pInode == uintptr(0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, pInode, 0, uint32(56)) libc.Xmemcpy(tls, pInode, bp, uint32(16)) if _sqlite3Config.FbCoreMutex != 0 { (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpLockMutex = Xsqlite3_mutex_alloc(tls, SQLITE_MUTEX_FAST) if (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpLockMutex == uintptr(0) { Xsqlite3_free(tls, pInode) return int32(SQLITE_NOMEM) } } (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnRef = int32(1) _ = libc.Int32FromInt32(0) (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpNext = _inodeList (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpPrev = uintptr(0) if _inodeList != 0 { (*TunixInodeInfo)(unsafe.Pointer(_inodeList)).FpPrev = pInode } _inodeList = pInode } else { (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnRef++ } *(*uintptr)(unsafe.Pointer(ppInode)) = pInode return SQLITE_OK } // C documentation // // /* // ** Return TRUE if pFile has been renamed or unlinked since it was first opened. // */ func _fileHasMoved(tls *libc.TLS, pFile uintptr) (r int32) { bp := tls.Alloc(112) defer tls.Free(112) var _ /* buf at bp+0 */ Tstat return libc.BoolInt32((*TunixFile)(unsafe.Pointer(pFile)).FpInode != uintptr(0) && ((*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(4)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(pFile)).FzPath, bp) != 0 || (*(*Tstat)(unsafe.Pointer(bp))).Fst_ino != (*TunixInodeInfo)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(pFile)).FpInode)).FfileId.Fino)) } // C documentation // // /* // ** Check a unixFile that is a database. Verify the following: // ** // ** (1) There is exactly one hard link on the file // ** (2) The file is not a symbolic link // ** (3) The file has not been renamed or unlinked // ** // ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right. // */ func _verifyDbFile(tls *libc.TLS, pFile uintptr) { bp := tls.Alloc(128) defer tls.Free(128) var rc int32 var _ /* buf at bp+0 */ Tstat _ = rc /* These verifications occurs for the main database only */ if int32((*TunixFile)(unsafe.Pointer(pFile)).FctrlFlags)&int32(UNIXFILE_NOLOCK) != 0 { return } rc = (*(*func(*libc.TLS, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(5)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(pFile)).Fh, bp) if rc != 0 { Xsqlite3_log(tls, int32(SQLITE_WARNING), __ccgo_ts+3687, libc.VaList(bp+112, (*TunixFile)(unsafe.Pointer(pFile)).FzPath)) return } if (*(*Tstat)(unsafe.Pointer(bp))).Fst_nlink == uint32(0) { Xsqlite3_log(tls, int32(SQLITE_WARNING), __ccgo_ts+3711, libc.VaList(bp+112, (*TunixFile)(unsafe.Pointer(pFile)).FzPath)) return } if (*(*Tstat)(unsafe.Pointer(bp))).Fst_nlink > uint32(1) { Xsqlite3_log(tls, int32(SQLITE_WARNING), __ccgo_ts+3740, libc.VaList(bp+112, (*TunixFile)(unsafe.Pointer(pFile)).FzPath)) return } if _fileHasMoved(tls, pFile) != 0 { Xsqlite3_log(tls, int32(SQLITE_WARNING), __ccgo_ts+3767, libc.VaList(bp+112, (*TunixFile)(unsafe.Pointer(pFile)).FzPath)) return } } // C documentation // // /* // ** This routine checks if there is a RESERVED lock held on the specified // ** file by this or any other process. If such a lock is held, set *pResOut // ** to a non-zero value otherwise *pResOut is set to zero. The return value // ** is set to SQLITE_OK unless an I/O error occurs during lock checking. // */ func _unixCheckReservedLock(tls *libc.TLS, id uintptr, pResOut uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var pFile uintptr var rc, reserved int32 var _ /* lock at bp+0 */ Tflock _, _, _ = pFile, rc, reserved rc = SQLITE_OK reserved = 0 pFile = id _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) Xsqlite3_mutex_enter(tls, (*TunixInodeInfo)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(pFile)).FpInode)).FpLockMutex) /* Check if a thread in this process holds such a lock */ if int32((*TunixInodeInfo)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(pFile)).FpInode)).FeFileLock) > int32(SHARED_LOCK) { reserved = int32(1) } /* Otherwise see if some other process holds it. */ if !(reserved != 0) && !((*TunixInodeInfo)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(pFile)).FpInode)).FbProcessLock != 0) { (*(*Tflock)(unsafe.Pointer(bp))).Fl_whence = SEEK_SET (*(*Tflock)(unsafe.Pointer(bp))).Fl_start = int64(_sqlite3PendingByte + libc.Int32FromInt32(1)) (*(*Tflock)(unsafe.Pointer(bp))).Fl_len = int64(1) (*(*Tflock)(unsafe.Pointer(bp))).Fl_type = int16(F_WRLCK) if (*(*func(*libc.TLS, int32, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(7)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(pFile)).Fh, int32(F_GETLK64), libc.VaList(bp+40, bp)) != 0 { rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(14)<iBusyTimeout ** value is set, then it is the number of milliseconds to wait before ** failing the lock. The iBusyTimeout value is always reset back to ** zero on each call. ** ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking ** attempt to set the lock. */ // C documentation // // /* // ** Attempt to set a system-lock on the file pFile. The lock is // ** described by pLock. // ** // ** If the pFile was opened read/write from unix-excl, then the only lock // ** ever obtained is an exclusive lock, and it is obtained exactly once // ** the first time any lock is attempted. All subsequent system locking // ** operations become no-ops. Locking operations still happen internally, // ** in order to coordinate access between separate database connections // ** within this process, but all of that is handled in memory and the // ** operating system does not participate. // ** // ** This function is a pass-through to fcntl(F_SETLK) if pFile is using // ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" // ** and is read-only. // ** // ** Zero is returned if the call completes successfully, or -1 if a call // ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). // */ func _unixFileLock(tls *libc.TLS, pFile uintptr, pLock uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var pInode uintptr var rc int32 var _ /* lock at bp+0 */ Tflock _, _ = pInode, rc pInode = (*TunixFile)(unsafe.Pointer(pFile)).FpInode _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TunixFile)(unsafe.Pointer(pFile)).FctrlFlags)&(libc.Int32FromInt32(UNIXFILE_EXCL)|libc.Int32FromInt32(UNIXFILE_RDONLY)) == int32(UNIXFILE_EXCL) { if int32((*TunixInodeInfo)(unsafe.Pointer(pInode)).FbProcessLock) == 0 { _ = libc.Int32FromInt32(0) (*(*Tflock)(unsafe.Pointer(bp))).Fl_whence = SEEK_SET (*(*Tflock)(unsafe.Pointer(bp))).Fl_start = int64(_sqlite3PendingByte + libc.Int32FromInt32(2)) (*(*Tflock)(unsafe.Pointer(bp))).Fl_len = int64(SHARED_SIZE) (*(*Tflock)(unsafe.Pointer(bp))).Fl_type = int16(F_WRLCK) rc = (*(*func(*libc.TLS, int32, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(7)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(pFile)).Fh, int32(F_SETLK64), libc.VaList(bp+40, bp)) if rc < 0 { return rc } (*TunixInodeInfo)(unsafe.Pointer(pInode)).FbProcessLock = uint8(1) (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnLock++ } else { rc = 0 } } else { rc = (*(*func(*libc.TLS, int32, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(7)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(pFile)).Fh, int32(F_SETLK64), libc.VaList(bp+40, pLock)) } return rc } // C documentation // // /* // ** Lock the file with the lock specified by parameter eFileLock - one // ** of the following: // ** // ** (1) SHARED_LOCK // ** (2) RESERVED_LOCK // ** (3) PENDING_LOCK // ** (4) EXCLUSIVE_LOCK // ** // ** Sometimes when requesting one lock state, additional lock states // ** are inserted in between. The locking might fail on one of the later // ** transitions leaving the lock state different from what it started but // ** still short of its goal. The following chart shows the allowed // ** transitions and the inserted intermediate states: // ** // ** UNLOCKED -> SHARED // ** SHARED -> RESERVED // ** SHARED -> EXCLUSIVE // ** RESERVED -> (PENDING) -> EXCLUSIVE // ** PENDING -> EXCLUSIVE // ** // ** This routine will only increase a lock. Use the sqlite3OsUnlock() // ** routine to lower a locking level. // */ func _unixLock(tls *libc.TLS, id uintptr, eFileLock int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var pFile, pInode uintptr var rc, tErrno, v1 int32 var _ /* lock at bp+0 */ Tflock _, _, _, _, _ = pFile, pInode, rc, tErrno, v1 /* The following describes the implementation of the various locks and ** lock transitions in terms of the POSIX advisory shared and exclusive ** lock primitives (called read-locks and write-locks below, to avoid ** confusion with SQLite lock names). The algorithms are complicated ** slightly in order to be compatible with Windows95 systems simultaneously ** accessing the same database file, in case that is ever required. ** ** Symbols defined in os.h identify the 'pending byte' and the 'reserved ** byte', each single bytes at well known offsets, and the 'shared byte ** range', a range of 510 bytes at a well known offset. ** ** To obtain a SHARED lock, a read-lock is obtained on the 'pending ** byte'. If this is successful, 'shared byte range' is read-locked ** and the lock on the 'pending byte' released. (Legacy note: When ** SQLite was first developed, Windows95 systems were still very common, ** and Windows95 lacks a shared-lock capability. So on Windows95, a ** single randomly selected by from the 'shared byte range' is locked. ** Windows95 is now pretty much extinct, but this work-around for the ** lack of shared-locks on Windows95 lives on, for backwards ** compatibility.) ** ** A process may only obtain a RESERVED lock after it has a SHARED lock. ** A RESERVED lock is implemented by grabbing a write-lock on the ** 'reserved byte'. ** ** An EXCLUSIVE lock may only be requested after either a SHARED or ** RESERVED lock is held. An EXCLUSIVE lock is implemented by obtaining ** a write-lock on the entire 'shared byte range'. Since all other locks ** require a read-lock on one of the bytes within this range, this ensures ** that no other locks are held on the database. ** ** If a process that holds a RESERVED lock requests an EXCLUSIVE, then ** a PENDING lock is obtained first. A PENDING lock is implemented by ** obtaining a write-lock on the 'pending byte'. This ensures that no new ** SHARED locks can be obtained, but existing SHARED locks are allowed to ** persist. If the call to this function fails to obtain the EXCLUSIVE ** lock in this case, it holds the PENDING lock instead. The client may ** then re-attempt the EXCLUSIVE lock later on, after existing SHARED ** locks have cleared. */ rc = SQLITE_OK pFile = id tErrno = 0 _ = libc.Int32FromInt32(0) /* If there is already a lock of this type or more restrictive on the ** unixFile, do nothing. Don't use the end_lock: exit path, as ** unixEnterMutex() hasn't been called yet. */ if int32((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) >= eFileLock { return SQLITE_OK } /* Make sure the locking sequence is correct. ** (1) We never move from unlocked to anything higher than shared lock. ** (2) SQLite never explicitly requests a pending lock. ** (3) A shared lock is always held when a reserve lock is requested. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* This mutex is needed because pFile->pInode is shared across threads */ pInode = (*TunixFile)(unsafe.Pointer(pFile)).FpInode Xsqlite3_mutex_enter(tls, (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpLockMutex) /* If some thread using this PID has a lock via a different unixFile* ** handle that precludes the requested lock, return BUSY. */ if int32((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) != int32((*TunixInodeInfo)(unsafe.Pointer(pInode)).FeFileLock) && (int32((*TunixInodeInfo)(unsafe.Pointer(pInode)).FeFileLock) >= int32(PENDING_LOCK) || eFileLock > int32(SHARED_LOCK)) { rc = int32(SQLITE_BUSY) goto end_lock } /* If a SHARED lock is requested, and some thread using this PID already ** has a SHARED or RESERVED lock, then increment reference counts and ** return SQLITE_OK. */ if eFileLock == int32(SHARED_LOCK) && (int32((*TunixInodeInfo)(unsafe.Pointer(pInode)).FeFileLock) == int32(SHARED_LOCK) || int32((*TunixInodeInfo)(unsafe.Pointer(pInode)).FeFileLock) == int32(RESERVED_LOCK)) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TunixFile)(unsafe.Pointer(pFile)).FeFileLock = uint8(SHARED_LOCK) (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnShared++ (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnLock++ goto end_lock } /* A PENDING lock is needed before acquiring a SHARED lock and before ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will ** be released. */ (*(*Tflock)(unsafe.Pointer(bp))).Fl_len = int64(1) (*(*Tflock)(unsafe.Pointer(bp))).Fl_whence = SEEK_SET if eFileLock == int32(SHARED_LOCK) || eFileLock == int32(EXCLUSIVE_LOCK) && int32((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) == int32(RESERVED_LOCK) { if eFileLock == int32(SHARED_LOCK) { v1 = F_RDLCK } else { v1 = int32(F_WRLCK) } (*(*Tflock)(unsafe.Pointer(bp))).Fl_type = int16(v1) (*(*Tflock)(unsafe.Pointer(bp))).Fl_start = int64(_sqlite3PendingByte) if _unixFileLock(tls, pFile, bp) != 0 { tErrno = *(*int32)(unsafe.Pointer(libc.X__errno_location(tls))) rc = _sqliteErrorFromPosixError(tls, tErrno, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(15)< int32(1) { /* We are trying for an exclusive lock but another thread in this ** same process is still holding a shared lock. */ rc = int32(SQLITE_BUSY) } else { /* The request was for a RESERVED or EXCLUSIVE lock. It is ** assumed that there is a SHARED or greater lock on the file ** already. */ _ = libc.Int32FromInt32(0) (*(*Tflock)(unsafe.Pointer(bp))).Fl_type = int16(F_WRLCK) _ = libc.Int32FromInt32(0) if eFileLock == int32(RESERVED_LOCK) { (*(*Tflock)(unsafe.Pointer(bp))).Fl_start = int64(_sqlite3PendingByte + libc.Int32FromInt32(1)) (*(*Tflock)(unsafe.Pointer(bp))).Fl_len = int64(1) } else { (*(*Tflock)(unsafe.Pointer(bp))).Fl_start = int64(_sqlite3PendingByte + libc.Int32FromInt32(2)) (*(*Tflock)(unsafe.Pointer(bp))).Fl_len = int64(SHARED_SIZE) } if _unixFileLock(tls, pFile, bp) != 0 { tErrno = *(*int32)(unsafe.Pointer(libc.X__errno_location(tls))) rc = _sqliteErrorFromPosixError(tls, tErrno, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(15)< int32(SHARED_LOCK) { _ = libc.Int32FromInt32(0) /* downgrading to a shared lock on NFS involves clearing the write lock ** before establishing the readlock - to avoid a race condition we downgrade ** the lock in 2 blocks, so that part of the range will be covered by a ** write lock until the rest is covered by a read lock: ** 1: [WWWWW] ** 2: [....W] ** 3: [RRRRW] ** 4: [RRRR.] */ if eFileLock == int32(SHARED_LOCK) { _ = handleNFSUnlock _ = libc.Int32FromInt32(0) (*(*Tflock)(unsafe.Pointer(bp))).Fl_type = F_RDLCK (*(*Tflock)(unsafe.Pointer(bp))).Fl_whence = SEEK_SET (*(*Tflock)(unsafe.Pointer(bp))).Fl_start = int64(_sqlite3PendingByte + libc.Int32FromInt32(2)) (*(*Tflock)(unsafe.Pointer(bp))).Fl_len = int64(SHARED_SIZE) if _unixFileLock(tls, pFile, bp) != 0 { /* In theory, the call to unixFileLock() cannot fail because another ** process is holding an incompatible lock. If it does, this ** indicates that the other process is not following the locking ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning ** SQLITE_BUSY would confuse the upper layer (in practice it causes ** an assert to fail). */ rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(9)<= 0 { _robust_close(tls, pFile, (*TunixFile)(unsafe.Pointer(pFile)).Fh, int32(40172)) (*TunixFile)(unsafe.Pointer(pFile)).Fh = -int32(1) } Xsqlite3_free(tls, (*TunixFile)(unsafe.Pointer(pFile)).FpPreallocatedUnused) libc.Xmemset(tls, pFile, 0, uint32(88)) return SQLITE_OK } // C documentation // // /* // ** Close a file. // */ func _unixClose(tls *libc.TLS, id uintptr) (r int32) { var pFile, pInode uintptr var rc int32 _, _, _ = pFile, pInode, rc rc = SQLITE_OK pFile = id pInode = (*TunixFile)(unsafe.Pointer(pFile)).FpInode _ = libc.Int32FromInt32(0) _verifyDbFile(tls, pFile) _unixUnlock(tls, id, NO_LOCK) _ = libc.Int32FromInt32(0) _unixEnterMutex(tls) /* unixFile.pInode is always valid here. Otherwise, a different close ** routine (e.g. nolockClose()) would be called instead. */ _ = libc.Int32FromInt32(0) Xsqlite3_mutex_enter(tls, (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpLockMutex) if (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnLock != 0 { /* If there are outstanding locks, do not actually close the file just ** yet because that would clear those locks. Instead, add the file ** descriptor to pInode->pUnused list. It will be automatically closed ** when the last lock is cleared. */ _setPendingFd(tls, pFile) } Xsqlite3_mutex_leave(tls, (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpLockMutex) _releaseInodeInfo(tls, pFile) _ = libc.Int32FromInt32(0) rc = _closeUnixFile(tls, id) _unixLeaveMutex(tls) return rc } /************** End of the posix advisory lock implementation ***************** ******************************************************************************/ /****************************************************************************** ****************************** No-op Locking ********************************** ** ** Of the various locking implementations available, this is by far the ** simplest: locking is ignored. No attempt is made to lock the database ** file for reading or writing. ** ** This locking mode is appropriate for use on read-only databases ** (ex: databases that are burned into CD-ROM, for example.) It can ** also be used if the application employs some external mechanism to ** prevent simultaneous access of the same database by two or more ** database connections. But there is a serious risk of database ** corruption if this locking mode is used in situations where multiple ** database connections are accessing the same database file at the same ** time and one or more of those connections are writing. */ func _nolockCheckReservedLock(tls *libc.TLS, NotUsed uintptr, pResOut uintptr) (r int32) { _ = NotUsed *(*int32)(unsafe.Pointer(pResOut)) = 0 return SQLITE_OK } func _nolockLock(tls *libc.TLS, NotUsed uintptr, NotUsed2 int32) (r int32) { _ = NotUsed _ = NotUsed2 return SQLITE_OK } func _nolockUnlock(tls *libc.TLS, NotUsed uintptr, NotUsed2 int32) (r int32) { _ = NotUsed _ = NotUsed2 return SQLITE_OK } // C documentation // // /* // ** Close the file. // */ func _nolockClose(tls *libc.TLS, id uintptr) (r int32) { return _closeUnixFile(tls, id) } /******************* End of the no-op lock implementation ********************* ******************************************************************************/ /****************************************************************************** ************************* Begin dot-file Locking ****************************** ** ** The dotfile locking implementation uses the existence of separate lock ** files (really a directory) to control access to the database. This works ** on just about every filesystem imaginable. But there are serious downsides: ** ** (1) There is zero concurrency. A single reader blocks all other ** connections from reading or writing the database. ** ** (2) An application crash or power loss can leave stale lock files ** sitting around that need to be cleared manually. ** ** Nevertheless, a dotlock is an appropriate locking mode for use if no ** other locking strategy is available. ** ** Dotfile locking works by creating a subdirectory in the same directory as ** the database and with the same name but with a ".lock" extension added. ** The existence of a lock directory implies an EXCLUSIVE lock. All other ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. */ /* ** The file suffix added to the data base filename in order to create the ** lock directory. */ // C documentation // // /* // ** This routine checks if there is a RESERVED lock held on the specified // ** file by this or any other process. If such a lock is held, set *pResOut // ** to a non-zero value otherwise *pResOut is set to zero. The return value // ** is set to SQLITE_OK unless an I/O error occurs during lock checking. // ** // ** In dotfile locking, either a lock exists or it does not. So in this // ** variation of CheckReservedLock(), *pResOut is set to true if any lock // ** is held on the file and false if the file is unlocked. // */ func _dotlockCheckReservedLock(tls *libc.TLS, id uintptr, pResOut uintptr) (r int32) { var pFile uintptr var rc, reserved int32 _, _, _ = pFile, rc, reserved rc = SQLITE_OK reserved = 0 pFile = id _ = libc.Int32FromInt32(0) reserved = libc.BoolInt32((*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(2)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext, 0) == 0) *(*int32)(unsafe.Pointer(pResOut)) = reserved return rc } // C documentation // // /* // ** Lock the file with the lock specified by parameter eFileLock - one // ** of the following: // ** // ** (1) SHARED_LOCK // ** (2) RESERVED_LOCK // ** (3) PENDING_LOCK // ** (4) EXCLUSIVE_LOCK // ** // ** Sometimes when requesting one lock state, additional lock states // ** are inserted in between. The locking might fail on one of the later // ** transitions leaving the lock state different from what it started but // ** still short of its goal. The following chart shows the allowed // ** transitions and the inserted intermediate states: // ** // ** UNLOCKED -> SHARED // ** SHARED -> RESERVED // ** SHARED -> (PENDING) -> EXCLUSIVE // ** RESERVED -> (PENDING) -> EXCLUSIVE // ** PENDING -> EXCLUSIVE // ** // ** This routine will only increase a lock. Use the sqlite3OsUnlock() // ** routine to lower a locking level. // ** // ** With dotfile locking, we really only support state (4): EXCLUSIVE. // ** But we track the other locking levels internally. // */ func _dotlockLock(tls *libc.TLS, id uintptr, eFileLock int32) (r int32) { var pFile, zLockFile uintptr var rc, tErrno int32 _, _, _, _ = pFile, rc, tErrno, zLockFile pFile = id zLockFile = (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext rc = SQLITE_OK /* If we have any lock, then the lock file already exists. All we have ** to do is adjust our internal record of the lock level. */ if int32((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) > NO_LOCK { (*TunixFile)(unsafe.Pointer(pFile)).FeFileLock = uint8(eFileLock) /* Always update the timestamp on the old file */ libc.Xutimes(tls, zLockFile, libc.UintptrFromInt32(0)) return SQLITE_OK } /* grab an exclusive lock */ rc = (*(*func(*libc.TLS, uintptr, Tmode_t) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(18)].FpCurrent})))(tls, zLockFile, uint32(0777)) if rc < 0 { /* failed to open/create the lock directory */ tErrno = *(*int32)(unsafe.Pointer(libc.X__errno_location(tls))) if int32(EEXIST) == tErrno { rc = int32(SQLITE_BUSY) } else { rc = _sqliteErrorFromPosixError(tls, tErrno, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(15)< 0 { got = (*(*func(*libc.TLS, int32, uintptr, Tsize_t, Toff_t) Tssize_t)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(9)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(id)).Fh, pBuf, uint32(cnt), offset) if got == cnt { break } if got < 0 { if *(*int32)(unsafe.Pointer(libc.X__errno_location(tls))) == int32(EINTR) { got = int32(1) continue } prior = 0 _storeLastErrno(tls, id, *(*int32)(unsafe.Pointer(libc.X__errno_location(tls)))) break } else { if got > 0 { cnt -= got offset += int64(got) prior += got pBuf = uintptr(got) + pBuf } } } return got + prior } // C documentation // // /* // ** Read data from a file into a buffer. Return SQLITE_OK if all // ** bytes were read successfully and SQLITE_IOERR if anything goes // ** wrong. // */ func _unixRead(tls *libc.TLS, id uintptr, pBuf uintptr, amt int32, offset Tsqlite3_int64) (r int32) { var got, nCopy int32 var pFile uintptr _, _, _ = got, nCopy, pFile pFile = id _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If this is a database file (not a journal, super-journal or temp ** file), the bytes in the locking range should never be read or written. */ /* Deal with as much of this read request as possible by transferring ** data from the memory mapping using memcpy(). */ if offset < (*TunixFile)(unsafe.Pointer(pFile)).FmmapSize { if offset+int64(amt) <= (*TunixFile)(unsafe.Pointer(pFile)).FmmapSize { libc.Xmemcpy(tls, pBuf, (*TunixFile)(unsafe.Pointer(pFile)).FpMapRegion+uintptr(offset), uint32(amt)) return SQLITE_OK } else { nCopy = int32((*TunixFile)(unsafe.Pointer(pFile)).FmmapSize - offset) libc.Xmemcpy(tls, pBuf, (*TunixFile)(unsafe.Pointer(pFile)).FpMapRegion+uintptr(offset), uint32(nCopy)) pBuf = pBuf + uintptr(nCopy) amt -= nCopy offset += int64(nCopy) } } got = _seekAndRead(tls, pFile, offset, pBuf, amt) if got == amt { return SQLITE_OK } else { if got < 0 { /* pFile->lastErrno has been set by seekAndRead(). ** Usually we return SQLITE_IOERR_READ here, though for some ** kinds of errors we return SQLITE_IOERR_CORRUPTFS. The ** SQLITE_IOERR_CORRUPTFS will be converted into SQLITE_CORRUPT ** prior to returning to the application by the sqlite3ApiExit() ** routine. */ switch (*TunixFile)(unsafe.Pointer(pFile)).FlastErrno { case int32(ERANGE): fallthrough case int32(EIO): fallthrough case int32(ENXIO): return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(33)<offset then read cnt bytes into pBuf. // ** Return the number of bytes actually read. Update the offset. // ** // ** To avoid stomping the errno value on a failed write the lastErrno value // ** is set before returning. // */ func _seekAndWrite(tls *libc.TLS, id uintptr, offset Ti64, pBuf uintptr, cnt int32) (r int32) { return _seekAndWriteFd(tls, (*TunixFile)(unsafe.Pointer(id)).Fh, offset, pBuf, cnt, id+20) } // C documentation // // /* // ** Write data from a buffer into a file. Return SQLITE_OK on success // ** or some other error code on failure. // */ func _unixWrite(tls *libc.TLS, id uintptr, pBuf uintptr, amt int32, offset Tsqlite3_int64) (r int32) { var pFile uintptr var wrote, v1 int32 _, _, _ = pFile, wrote, v1 pFile = id wrote = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If this is a database file (not a journal, super-journal or temp ** file), the bytes in the locking range should never be read or written. */ for { v1 = _seekAndWrite(tls, pFile, offset, pBuf, amt) wrote = v1 if !(v1 < amt && wrote > 0) { break } amt -= wrote offset += int64(wrote) pBuf = pBuf + uintptr(wrote) } if amt > wrote { if wrote < 0 && (*TunixFile)(unsafe.Pointer(pFile)).FlastErrno != int32(ENOSPC) { /* lastErrno set by seekAndWrite */ return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(3)< 0 && int32((*(*[513]uint8)(unsafe.Pointer(bp)))[ii]) != int32('/')) { break } goto _1 _1: ; ii-- } if ii > 0 { (*(*[513]uint8)(unsafe.Pointer(bp)))[ii] = uint8('\000') } else { if int32((*(*[513]uint8)(unsafe.Pointer(bp)))[0]) != int32('/') { (*(*[513]uint8)(unsafe.Pointer(bp)))[0] = uint8('.') } (*(*[513]uint8)(unsafe.Pointer(bp)))[int32(1)] = uint8(0) } fd = _robust_open(tls, bp, libc.Int32FromInt32(O_RDONLY)|libc.Int32FromInt32(O_BINARY), uint32(0)) if fd >= 0 { } *(*int32)(unsafe.Pointer(pFd)) = fd if fd >= 0 { return SQLITE_OK } return _unixLogErrorAtLine(tls, _sqlite3CantopenError(tls, int32(41778)), __ccgo_ts+3510, bp, int32(41778)) } // C documentation // // /* // ** Make sure all writes to a particular file are committed to disk. // ** // ** If dataOnly==0 then both the file itself and its metadata (file // ** size, access time, etc) are synced. If dataOnly!=0 then only the // ** file data is synced. // ** // ** Under Unix, also make sure that the directory entry for the file // ** has been created by fsync-ing the directory that contains the file. // ** If we do not do this and we encounter a power failure, the directory // ** entry for the journal might not exist after we reboot. The next // ** SQLite to access the file will not know that the journal exists (because // ** the directory entry for the journal was never created) and the transaction // ** will not roll back - possibly leading to database corruption. // */ func _unixSync(tls *libc.TLS, id uintptr, flags int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var isDataOnly, isFullsync, rc int32 var pFile, p1 uintptr var _ /* dirfd at bp+0 */ int32 _, _, _, _, _ = isDataOnly, isFullsync, pFile, rc, p1 pFile = id isDataOnly = flags & int32(SQLITE_SYNC_DATAONLY) isFullsync = libc.BoolInt32(flags&int32(0x0F) == int32(SQLITE_SYNC_FULL)) /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ _ = libc.Int32FromInt32(0) /* Unix cannot, but some systems may return SQLITE_FULL from here. This ** line is to test that doing so does not cause any problems. */ _ = libc.Int32FromInt32(0) rc = _full_fsync(tls, (*TunixFile)(unsafe.Pointer(pFile)).Fh, isFullsync, isDataOnly) if rc != 0 { _storeLastErrno(tls, pFile, *(*int32)(unsafe.Pointer(libc.X__errno_location(tls)))) return _unixLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(4)< 0 { nByte = (nByte + int64((*TunixFile)(unsafe.Pointer(pFile)).FszChunk) - int64(1)) / int64((*TunixFile)(unsafe.Pointer(pFile)).FszChunk) * int64((*TunixFile)(unsafe.Pointer(pFile)).FszChunk) } rc = _robust_ftruncate(tls, (*TunixFile)(unsafe.Pointer(pFile)).Fh, nByte) if rc != 0 { _storeLastErrno(tls, pFile, *(*int32)(unsafe.Pointer(libc.X__errno_location(tls)))) return _unixLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(6)< 0 { /* Used to hold return values of fstat() */ if (*(*func(*libc.TLS, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(5)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(pFile)).Fh, bp) != 0 { return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(7)< (*(*Tstat)(unsafe.Pointer(bp))).Fst_size { /* If the OS does not have posix_fallocate(), fake it. Write a ** single byte to the last byte in each block that falls entirely ** within the extended region. Then, if required, a single byte ** at offset (nSize-1), to set the size of the file correctly. ** This is a similar technique to that used by glibc on systems ** that do not have a real fallocate() call. */ nBlk = (*(*Tstat)(unsafe.Pointer(bp))).Fst_blksize /* File-system block size */ nWrite = 0 /* Next offset to write to */ iWrite = (*(*Tstat)(unsafe.Pointer(bp))).Fst_size/int64(nBlk)*int64(nBlk) + int64(nBlk) - int64(1) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) for { if !(iWrite < nSize+int64(nBlk)-int64(1)) { break } if iWrite >= nSize { iWrite = nSize - int64(1) } nWrite = _seekAndWrite(tls, pFile, iWrite, __ccgo_ts+1648, int32(1)) if nWrite != int32(1) { return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(3)< 0 && nByte > (*TunixFile)(unsafe.Pointer(pFile)).FmmapSize { if (*TunixFile)(unsafe.Pointer(pFile)).FszChunk <= 0 { if _robust_ftruncate(tls, (*TunixFile)(unsafe.Pointer(pFile)).Fh, nByte) != 0 { _storeLastErrno(tls, pFile, *(*int32)(unsafe.Pointer(libc.X__errno_location(tls)))) return _unixLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(6)<ctrlFlags is set. // ** // ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. // */ func _unixModeBit(tls *libc.TLS, pFile uintptr, mask uint8, pArg uintptr) { var p1, p2 uintptr _, _ = p1, p2 if *(*int32)(unsafe.Pointer(pArg)) < 0 { *(*int32)(unsafe.Pointer(pArg)) = libc.BoolInt32(int32((*TunixFile)(unsafe.Pointer(pFile)).FctrlFlags)&int32(mask) != 0) } else { if *(*int32)(unsafe.Pointer(pArg)) == 0 { p1 = pFile + 18 *(*uint16)(unsafe.Pointer(p1)) = uint16(int32(*(*uint16)(unsafe.Pointer(p1))) & ^int32(mask)) } else { p2 = pFile + 18 *(*uint16)(unsafe.Pointer(p2)) = uint16(int32(*(*uint16)(unsafe.Pointer(p2))) | int32(mask)) } } } // C documentation // // /* // ** Information and control of an open file handle. // */ func _unixFileControl(tls *libc.TLS, id uintptr, op int32, pArg uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var newLimit Ti64 var pFile, zTFile uintptr var rc, rc1 int32 _, _, _, _, _ = newLimit, pFile, rc, rc1, zTFile pFile = id switch op { case int32(SQLITE_FCNTL_LOCKSTATE): *(*int32)(unsafe.Pointer(pArg)) = int32((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) return SQLITE_OK case int32(SQLITE_FCNTL_LAST_ERRNO): *(*int32)(unsafe.Pointer(pArg)) = (*TunixFile)(unsafe.Pointer(pFile)).FlastErrno return SQLITE_OK case int32(SQLITE_FCNTL_CHUNK_SIZE): (*TunixFile)(unsafe.Pointer(pFile)).FszChunk = *(*int32)(unsafe.Pointer(pArg)) return SQLITE_OK case int32(SQLITE_FCNTL_SIZE_HINT): rc = _fcntlSizeHint(tls, pFile, *(*Ti64)(unsafe.Pointer(pArg))) return rc case int32(SQLITE_FCNTL_PERSIST_WAL): _unixModeBit(tls, pFile, uint8(UNIXFILE_PERSIST_WAL), pArg) return SQLITE_OK case int32(SQLITE_FCNTL_POWERSAFE_OVERWRITE): _unixModeBit(tls, pFile, uint8(UNIXFILE_PSOW), pArg) return SQLITE_OK case int32(SQLITE_FCNTL_VFSNAME): *(*uintptr)(unsafe.Pointer(pArg)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+8, (*Tsqlite3_vfs)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(pFile)).FpVfs)).FzName)) return SQLITE_OK case int32(SQLITE_FCNTL_TEMPFILENAME): zTFile = Xsqlite3_malloc64(tls, uint64((*Tsqlite3_vfs)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(pFile)).FpVfs)).FmxPathname)) if zTFile != 0 { _unixGetTempname(tls, (*Tsqlite3_vfs)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(pFile)).FpVfs)).FmxPathname, zTFile) *(*uintptr)(unsafe.Pointer(pArg)) = zTFile } return SQLITE_OK case int32(SQLITE_FCNTL_HAS_MOVED): *(*int32)(unsafe.Pointer(pArg)) = _fileHasMoved(tls, pFile) return SQLITE_OK case int32(SQLITE_FCNTL_MMAP_SIZE): newLimit = *(*Ti64)(unsafe.Pointer(pArg)) rc1 = SQLITE_OK if newLimit > _sqlite3Config.FmxMmap { newLimit = _sqlite3Config.FmxMmap } /* The value of newLimit may be eventually cast to (size_t) and passed ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a ** 64-bit type. */ if newLimit > 0 && libc.Bool(uint32(4) < uint32(8)) { newLimit = newLimit & libc.Int64FromInt32(0x7FFFFFFF) } *(*Ti64)(unsafe.Pointer(pArg)) = (*TunixFile)(unsafe.Pointer(pFile)).FmmapSizeMax if newLimit >= 0 && newLimit != (*TunixFile)(unsafe.Pointer(pFile)).FmmapSizeMax && (*TunixFile)(unsafe.Pointer(pFile)).FnFetchOut == 0 { (*TunixFile)(unsafe.Pointer(pFile)).FmmapSizeMax = newLimit if (*TunixFile)(unsafe.Pointer(pFile)).FmmapSize > 0 { _unixUnmapfile(tls, pFile) rc1 = _unixMapfile(tls, pFile, int64(-int32(1))) } } return rc1 case int32(SQLITE_FCNTL_EXTERNAL_READER): return _unixFcntlExternalReader(tls, id, pArg) } return int32(SQLITE_NOTFOUND) } // C documentation // // /* // ** If pFd->sectorSize is non-zero when this function is called, it is a // ** no-op. Otherwise, the values of pFd->sectorSize and // ** pFd->deviceCharacteristics are set according to the file-system // ** characteristics. // ** // ** There are two versions of this function. One for QNX and one for all // ** other systems. // */ func _setDeviceCharacteristics(tls *libc.TLS, pFd uintptr) { _ = libc.Int32FromInt32(0) if (*TunixFile)(unsafe.Pointer(pFd)).FsectorSize == 0 { /* Set the POWERSAFE_OVERWRITE flag if requested. */ if int32((*TunixFile)(unsafe.Pointer(pFd)).FctrlFlags)&int32(UNIXFILE_PSOW) != 0 { *(*int32)(unsafe.Pointer(pFd + 80)) |= int32(SQLITE_IOCAP_POWERSAFE_OVERWRITE) } (*TunixFile)(unsafe.Pointer(pFd)).FsectorSize = int32(SQLITE_DEFAULT_SECTOR_SIZE) } } // C documentation // // /* // ** Return the sector size in bytes of the underlying block device for // ** the specified file. This is almost always 512 bytes, but may be // ** larger for some devices. // ** // ** SQLite code assumes this function cannot fail. It also assumes that // ** if two files are created in the same file-system directory (i.e. // ** a database and its journal file) that the sector size will be the // ** same for both. // */ func _unixSectorSize(tls *libc.TLS, id uintptr) (r int32) { var pFd uintptr _ = pFd pFd = id _setDeviceCharacteristics(tls, pFd) return (*TunixFile)(unsafe.Pointer(pFd)).FsectorSize } // C documentation // // /* // ** Return the device characteristics for the file. // ** // ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default. // ** However, that choice is controversial since technically the underlying // ** file system does not always provide powersafe overwrites. (In other // ** words, after a power-loss event, parts of the file that were never // ** written might end up being altered.) However, non-PSOW behavior is very, // ** very rare. And asserting PSOW makes a large reduction in the amount // ** of required I/O for journaling, since a lot of padding is eliminated. // ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control // ** available to turn it off and URI query parameter available to turn it off. // */ func _unixDeviceCharacteristics(tls *libc.TLS, id uintptr) (r int32) { var pFd uintptr _ = pFd pFd = id _setDeviceCharacteristics(tls, pFd) return (*TunixFile)(unsafe.Pointer(pFd)).FdeviceCharacteristics } // C documentation // // /* // ** Return the system page size. // ** // ** This function should not be called directly by other code in this file. // ** Instead, it should be called via macro osGetpagesize(). // */ func _unixGetpagesize(tls *libc.TLS) (r int32) { return libc.Xsysconf(tls, int32(__SC_PAGESIZE)) } /* ** Object used to represent an shared memory buffer. ** ** When multiple threads all reference the same wal-index, each thread ** has its own unixShm object, but they all point to a single instance ** of this unixShmNode object. In other words, each wal-index is opened ** only once per process. ** ** Each unixShmNode object is connected to a single unixInodeInfo object. ** We could coalesce this object into unixInodeInfo, but that would mean ** every open file that does not use shared memory (in other words, most ** open files) would have to carry around this extra information. So ** the unixInodeInfo object contains a pointer to this unixShmNode object ** and the unixShmNode object is created only when needed. ** ** unixMutexHeld() must be true when creating or destroying ** this object or while reading or writing the following fields: ** ** nRef ** ** The following fields are read-only after the object is created: ** ** hShm ** zFilename ** ** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and ** unixMutexHeld() is true when reading or writing any other field ** in this structure. ** ** aLock[SQLITE_SHM_NLOCK]: ** This array records the various locks held by clients on each of the ** SQLITE_SHM_NLOCK slots. If the aLock[] entry is set to 0, then no ** locks are held by the process on this slot. If it is set to -1, then ** some client holds an EXCLUSIVE lock on the locking slot. If the aLock[] ** value is set to a positive value, then it is the number of shared ** locks currently held on the slot. ** ** aMutex[SQLITE_SHM_NLOCK]: ** Normally, when SQLITE_ENABLE_SETLK_TIMEOUT is not defined, mutex ** pShmMutex is used to protect the aLock[] array and the right to ** call fcntl() on unixShmNode.hShm to obtain or release locks. ** ** If SQLITE_ENABLE_SETLK_TIMEOUT is defined though, we use an array ** of mutexes - one for each locking slot. To read or write locking ** slot aLock[iSlot], the caller must hold the corresponding mutex ** aMutex[iSlot]. Similarly, to call fcntl() to obtain or release a ** lock corresponding to slot iSlot, mutex aMutex[iSlot] must be held. */ type TunixShmNode1 = struct { FpInode uintptr FpShmMutex uintptr FzFilename uintptr FhShm int32 FszRegion int32 FnRegion Tu16 FisReadonly Tu8 FisUnlocked Tu8 FapRegion uintptr FnRef int32 FpFirst uintptr FaLock [8]int32 } type unixShmNode1 = TunixShmNode1 /* ** Structure used internally by this VFS to record the state of an ** open shared memory connection. ** ** The following fields are initialized when this object is created and ** are read-only thereafter: ** ** unixShm.pShmNode ** unixShm.id ** ** All other fields are read/write. The unixShm.pShmNode->pShmMutex must ** be held while accessing any read/write fields. */ type TunixShm1 = struct { FpShmNode uintptr FpNext uintptr FhasMutex Tu8 Fid Tu8 FsharedMask Tu16 FexclMask Tu16 } type unixShm1 = TunixShm1 /* ** Constants used for locking */ // C documentation // // /* // ** Use F_GETLK to check whether or not there are any readers with open // ** wal-mode transactions in other processes on database file pFile. If // ** no error occurs, return SQLITE_OK and set (*piOut) to 1 if there are // ** such transactions, or 0 otherwise. If an error occurs, return an // ** SQLite error code. The final value of *piOut is undefined in this // ** case. // */ func _unixFcntlExternalReader(tls *libc.TLS, pFile uintptr, piOut uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var pShmNode uintptr var rc int32 var _ /* f at bp+0 */ Tflock _, _ = pShmNode, rc rc = SQLITE_OK *(*int32)(unsafe.Pointer(piOut)) = 0 if (*TunixFile)(unsafe.Pointer(pFile)).FpShm != 0 { pShmNode = (*TunixShm)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(pFile)).FpShm)).FpShmNode libc.Xmemset(tls, bp, 0, uint32(32)) (*(*Tflock)(unsafe.Pointer(bp))).Fl_type = int16(F_WRLCK) (*(*Tflock)(unsafe.Pointer(bp))).Fl_whence = SEEK_SET (*(*Tflock)(unsafe.Pointer(bp))).Fl_start = int64((libc.Int32FromInt32(22)+libc.Int32FromInt32(SQLITE_SHM_NLOCK))*libc.Int32FromInt32(4) + libc.Int32FromInt32(3)) (*(*Tflock)(unsafe.Pointer(bp))).Fl_len = int64(libc.Int32FromInt32(SQLITE_SHM_NLOCK) - libc.Int32FromInt32(3)) Xsqlite3_mutex_enter(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FpShmMutex) if (*(*func(*libc.TLS, int32, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(7)].FpCurrent})))(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FhShm, int32(F_GETLK64), libc.VaList(bp+40, bp)) < 0 { rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(15)<= 0 { /* Initialize the locking parameters */ (*(*Tflock)(unsafe.Pointer(bp))).Fl_type = int16(lockType) (*(*Tflock)(unsafe.Pointer(bp))).Fl_whence = SEEK_SET (*(*Tflock)(unsafe.Pointer(bp))).Fl_start = int64(ofst) (*(*Tflock)(unsafe.Pointer(bp))).Fl_len = int64(n) res = (*(*func(*libc.TLS, int32, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(7)].FpCurrent})))(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FhShm, int32(F_SETLK64), libc.VaList(bp+40, bp)) if res == -int32(1) { rc = int32(SQLITE_BUSY) } } /* Do debug tracing */ return rc } // C documentation // // /* // ** Return the minimum number of 32KB shm regions that should be mapped at // ** a time, assuming that each mapping must be an integer multiple of the // ** current system page-size. // ** // ** Usually, this is 1. The exception seems to be systems that are configured // ** to use 64KB pages - in this case each mapping must cover at least two // ** shm regions. // */ func _unixShmRegionPerMap(tls *libc.TLS) (r int32) { var pgsz, shmsz int32 _, _ = pgsz, shmsz shmsz = libc.Int32FromInt32(32) * libc.Int32FromInt32(1024) /* SHM region size */ pgsz = (*(*func(*libc.TLS) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(25)].FpCurrent})))(tls) /* System page size */ _ = libc.Int32FromInt32(0) /* Page size must be a power of 2 */ if pgsz < shmsz { return int32(1) } return pgsz / shmsz } // C documentation // // /* // ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. // ** // ** This is not a VFS shared-memory method; it is a utility function called // ** by VFS shared-memory methods. // */ func _unixShmPurge(tls *libc.TLS, pFd uintptr) { var i, nShmPerMap int32 var p uintptr _, _, _ = i, nShmPerMap, p p = (*TunixInodeInfo)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(pFd)).FpInode)).FpShmNode _ = libc.Int32FromInt32(0) if p != 0 && (*TunixShmNode)(unsafe.Pointer(p)).FnRef == 0 { nShmPerMap = _unixShmRegionPerMap(tls) _ = libc.Int32FromInt32(0) Xsqlite3_mutex_free(tls, (*TunixShmNode)(unsafe.Pointer(p)).FpShmMutex) i = 0 for { if !(i < int32((*TunixShmNode)(unsafe.Pointer(p)).FnRegion)) { break } if (*TunixShmNode)(unsafe.Pointer(p)).FhShm >= 0 { (*(*func(*libc.TLS, uintptr, Tsize_t) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(23)].FpCurrent})))(tls, *(*uintptr)(unsafe.Pointer((*TunixShmNode)(unsafe.Pointer(p)).FapRegion + uintptr(i)*4)), uint32((*TunixShmNode)(unsafe.Pointer(p)).FszRegion)) } else { Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer((*TunixShmNode)(unsafe.Pointer(p)).FapRegion + uintptr(i)*4))) } goto _1 _1: ; i += nShmPerMap } Xsqlite3_free(tls, (*TunixShmNode)(unsafe.Pointer(p)).FapRegion) if (*TunixShmNode)(unsafe.Pointer(p)).FhShm >= 0 { _robust_close(tls, pFd, (*TunixShmNode)(unsafe.Pointer(p)).FhShm, int32(42583)) (*TunixShmNode)(unsafe.Pointer(p)).FhShm = -int32(1) } (*TunixInodeInfo)(unsafe.Pointer((*TunixShmNode)(unsafe.Pointer(p)).FpInode)).FpShmNode = uintptr(0) Xsqlite3_free(tls, p) } } // C documentation // // /* // ** The DMS lock has not yet been taken on shm file pShmNode. Attempt to // ** take it now. Return SQLITE_OK if successful, or an SQLite error // ** code otherwise. // ** // ** If the DMS cannot be locked because this is a readonly_shm=1 // ** connection and no other process already holds a lock, return // ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1. // */ func _unixLockSharedMemory(tls *libc.TLS, pDbFd uintptr, pShmNode uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var rc int32 var _ /* lock at bp+0 */ Tflock _ = rc rc = SQLITE_OK /* Use F_GETLK to determine the locks other processes are holding ** on the DMS byte. If it indicates that another process is holding ** a SHARED lock, then this process may also take a SHARED lock ** and proceed with opening the *-shm file. ** ** Or, if no other process is holding any lock, then this process ** is the first to open it. In this case take an EXCLUSIVE lock on the ** DMS byte and truncate the *-shm file to zero bytes in size. Then ** downgrade to a SHARED lock on the DMS byte. ** ** If another process is holding an EXCLUSIVE lock on the DMS byte, ** return SQLITE_BUSY to the caller (it will try again). An earlier ** version of this code attempted the SHARED lock at this point. But ** this introduced a subtle race condition: if the process holding ** EXCLUSIVE failed just before truncating the *-shm file, then this ** process might open and use the *-shm file without truncating it. ** And if the *-shm file has been corrupted by a power failure or ** system crash, the database itself may also become corrupt. */ (*(*Tflock)(unsafe.Pointer(bp))).Fl_whence = SEEK_SET (*(*Tflock)(unsafe.Pointer(bp))).Fl_start = int64((libc.Int32FromInt32(22)+libc.Int32FromInt32(SQLITE_SHM_NLOCK))*libc.Int32FromInt32(4) + libc.Int32FromInt32(SQLITE_SHM_NLOCK)) (*(*Tflock)(unsafe.Pointer(bp))).Fl_len = int64(1) (*(*Tflock)(unsafe.Pointer(bp))).Fl_type = int16(F_WRLCK) if (*(*func(*libc.TLS, int32, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(7)].FpCurrent})))(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FhShm, int32(F_GETLK64), libc.VaList(bp+40, bp)) != 0 { rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(15)<pFirst. This must be done while holding the ** pShmNode->pShmMutex. */ Xsqlite3_mutex_enter(tls, (*TunixShmNode1)(unsafe.Pointer(pShmNode)).FpShmMutex) (*TunixShm1)(unsafe.Pointer(p)).FpNext = (*TunixShmNode1)(unsafe.Pointer(pShmNode)).FpFirst (*TunixShmNode1)(unsafe.Pointer(pShmNode)).FpFirst = p Xsqlite3_mutex_leave(tls, (*TunixShmNode1)(unsafe.Pointer(pShmNode)).FpShmMutex) return rc /* Jump here on any error */ goto shm_open_err shm_open_err: ; _unixShmPurge(tls, pDbFd) /* This call frees pShmNode if required */ Xsqlite3_free(tls, p) _unixLeaveMutex(tls) return rc } // C documentation // // /* // ** This function is called to obtain a pointer to region iRegion of the // ** shared-memory associated with the database file fd. Shared-memory regions // ** are numbered starting from zero. Each shared-memory region is szRegion // ** bytes in size. // ** // ** If an error occurs, an error code is returned and *pp is set to NULL. // ** // ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory // ** region has not been allocated (by any client, including one running in a // ** separate process), then *pp is set to NULL and SQLITE_OK returned. If // ** bExtend is non-zero and the requested shared-memory region has not yet // ** been allocated, it is allocated by this function. // ** // ** If the shared-memory region has already been allocated or is allocated by // ** this call as described above, then it is mapped into this processes // ** address space (if it is not already), *pp is set to point to the mapped // ** memory and SQLITE_OK returned. // */ func _unixShmMap(tls *libc.TLS, fd uintptr, iRegion int32, szRegion int32, bExtend int32, pp uintptr) (r int32) { bp := tls.Alloc(112) defer tls.Free(112) var apNew, p, pDbFd, pMem, pShmNode, zFile, p4 uintptr var i, iPg, nByte, nMap, nReqRegion, nShmPerMap, rc, v2 int32 var _ /* sStat at bp+0 */ Tstat var _ /* x at bp+104 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = apNew, i, iPg, nByte, nMap, nReqRegion, nShmPerMap, p, pDbFd, pMem, pShmNode, rc, zFile, v2, p4 pDbFd = fd rc = SQLITE_OK nShmPerMap = _unixShmRegionPerMap(tls) /* If the shared-memory file has not yet been opened, open it now. */ if (*TunixFile)(unsafe.Pointer(pDbFd)).FpShm == uintptr(0) { rc = _unixOpenSharedMemory(tls, pDbFd) if rc != SQLITE_OK { return rc } } p = (*TunixFile)(unsafe.Pointer(pDbFd)).FpShm pShmNode = (*TunixShm)(unsafe.Pointer(p)).FpShmNode Xsqlite3_mutex_enter(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FpShmMutex) if (*TunixShmNode)(unsafe.Pointer(pShmNode)).FisUnlocked != 0 { rc = _unixLockSharedMemory(tls, pDbFd, pShmNode) if rc != SQLITE_OK { goto shmpage_out } (*TunixShmNode)(unsafe.Pointer(pShmNode)).FisUnlocked = uint8(0) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Minimum number of regions required to be mapped. */ nReqRegion = (iRegion + nShmPerMap) / nShmPerMap * nShmPerMap if int32((*TunixShmNode)(unsafe.Pointer(pShmNode)).FnRegion) < nReqRegion { /* New apRegion[] array */ nByte = nReqRegion * szRegion /* Used by fstat() */ (*TunixShmNode)(unsafe.Pointer(pShmNode)).FszRegion = szRegion if (*TunixShmNode)(unsafe.Pointer(pShmNode)).FhShm >= 0 { /* The requested region is not mapped into this processes address space. ** Check to see if it has been allocated (i.e. if the wal-index file is ** large enough to contain the requested region). */ if (*(*func(*libc.TLS, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(5)].FpCurrent})))(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FhShm, bp) != 0 { rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(19)<= 0 { if (*TunixShmNode)(unsafe.Pointer(pShmNode)).FisReadonly != 0 { v2 = int32(PROT_READ) } else { v2 = libc.Int32FromInt32(PROT_READ) | libc.Int32FromInt32(PROT_WRITE) } pMem = (*(*func(*libc.TLS, uintptr, Tsize_t, int32, int32, int32, Toff_t) uintptr)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(22)].FpCurrent})))(tls, uintptr(0), uint32(nMap), v2, int32(MAP_SHARED), (*TunixShmNode)(unsafe.Pointer(pShmNode)).FhShm, int64(szRegion)*int64((*TunixShmNode)(unsafe.Pointer(pShmNode)).FnRegion)) if pMem == uintptr(-libc.Int32FromInt32(1)) { rc = _unixLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(21)< iRegion { *(*uintptr)(unsafe.Pointer(pp)) = *(*uintptr)(unsafe.Pointer((*TunixShmNode)(unsafe.Pointer(pShmNode)).FapRegion + uintptr(iRegion)*4)) } else { *(*uintptr)(unsafe.Pointer(pp)) = uintptr(0) } if (*TunixShmNode)(unsafe.Pointer(pShmNode)).FisReadonly != 0 && rc == SQLITE_OK { rc = int32(SQLITE_READONLY) } Xsqlite3_mutex_leave(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FpShmMutex) return rc } var _pgsz = int32(4096) /* ** Check that the pShmNode->aLock[] array comports with the locking bitmasks ** held by each client. Return true if it does, or false otherwise. This ** is to be used in an assert(). e.g. ** ** assert( assertLockingArrayOk(pShmNode) ); */ // C documentation // // /* // ** Change the lock state for a shared-memory segment. // ** // ** Note that the relationship between SHARED and EXCLUSIVE locks is a little // ** different here than in posix. In xShmLock(), one can go from unlocked // ** to shared and back or from unlocked to exclusive and back. But one may // ** not go from shared to exclusive or from exclusive to shared. // */ func _unixShmLock(tls *libc.TLS, fd uintptr, ofst int32, n int32, flags int32) (r int32) { var aLock, p, pDbFd, pShmNode, p1, p2, p3, p4, p6 uintptr var bUnlock, ii, rc int32 var mask Tu16 _, _, _, _, _, _, _, _, _, _, _, _, _ = aLock, bUnlock, ii, mask, p, pDbFd, pShmNode, rc, p1, p2, p3, p4, p6 pDbFd = fd /* The underlying file iNode */ rc = SQLITE_OK /* Result code */ mask = uint16(int32(1)<<(ofst+n) - int32(1)<=3 && ofst int32(1) { bUnlock = 0 *(*int32)(unsafe.Pointer(aLock + uintptr(ofst)*4))-- p1 = p + 10 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^int32(mask)) } } if bUnlock != 0 { rc = _unixShmSystemLock(tls, pDbFd, int32(F_UNLCK), ofst+(libc.Int32FromInt32(22)+libc.Int32FromInt32(SQLITE_SHM_NLOCK))*libc.Int32FromInt32(4), n) if rc == SQLITE_OK { libc.Xmemset(tls, aLock+uintptr(ofst)*4, 0, uint32(4)*uint32(n)) p2 = p + 10 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) & ^int32(mask)) p3 = p + 12 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) & ^int32(mask)) } } } else { if flags&int32(SQLITE_SHM_SHARED) != 0 { /* Case (b) - a shared lock. */ if *(*int32)(unsafe.Pointer(aLock + uintptr(ofst)*4)) < 0 { /* An exclusive lock is held by some other connection. BUSY. */ rc = int32(SQLITE_BUSY) } else { if *(*int32)(unsafe.Pointer(aLock + uintptr(ofst)*4)) == 0 { rc = _unixShmSystemLock(tls, pDbFd, F_RDLCK, ofst+(libc.Int32FromInt32(22)+libc.Int32FromInt32(SQLITE_SHM_NLOCK))*libc.Int32FromInt32(4), n) } } /* Get the local shared locks */ if rc == SQLITE_OK { p4 = p + 10 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | int32(mask)) *(*int32)(unsafe.Pointer(aLock + uintptr(ofst)*4))++ } } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Make sure no sibling connections hold locks that will block this ** lock. If any do, return SQLITE_BUSY right away. */ ii = ofst for { if !(ii < ofst+n) { break } if *(*int32)(unsafe.Pointer(aLock + uintptr(ii)*4)) != 0 { rc = int32(SQLITE_BUSY) break } goto _5 _5: ; ii++ } /* Get the exclusive locks at the system level. Then if successful ** also update the in-memory values. */ if rc == SQLITE_OK { rc = _unixShmSystemLock(tls, pDbFd, int32(F_WRLCK), ofst+(libc.Int32FromInt32(22)+libc.Int32FromInt32(SQLITE_SHM_NLOCK))*libc.Int32FromInt32(4), n) if rc == SQLITE_OK { p6 = p + 12 *(*Tu16)(unsafe.Pointer(p6)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p6))) | int32(mask)) ii = ofst for { if !(ii < ofst+n) { break } *(*int32)(unsafe.Pointer(aLock + uintptr(ii)*4)) = -int32(1) goto _7 _7: ; ii++ } } } } } _ = libc.Int32FromInt32(0) } /* Drop the mutexes acquired above. */ Xsqlite3_mutex_leave(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FpShmMutex) } return rc } // C documentation // // /* // ** Implement a memory barrier or memory fence on shared memory. // ** // ** All loads and stores begun before the barrier must complete before // ** any load or store begun after the barrier. // */ func _unixShmBarrier(tls *libc.TLS, fd uintptr) { _ = fd /* compiler-defined memory barrier */ _ = libc.Int32FromInt32(0) _unixEnterMutex(tls) /* Also mutex, for redundancy */ _unixLeaveMutex(tls) } // C documentation // // /* // ** Close a connection to shared-memory. Delete the underlying // ** storage if deleteFlag is true. // ** // ** If there is no shared memory associated with the connection then this // ** routine is a harmless no-op. // */ func _unixShmUnmap(tls *libc.TLS, fd uintptr, deleteFlag int32) (r int32) { var p, pDbFd, pShmNode, pp uintptr _, _, _, _ = p, pDbFd, pShmNode, pp /* The underlying database file */ pDbFd = fd p = (*TunixFile)(unsafe.Pointer(pDbFd)).FpShm if p == uintptr(0) { return SQLITE_OK } pShmNode = (*TunixShm)(unsafe.Pointer(p)).FpShmNode _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Remove connection p from the set of connections associated ** with pShmNode */ Xsqlite3_mutex_enter(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FpShmMutex) pp = pShmNode + 32 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != p) { break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 4 } *(*uintptr)(unsafe.Pointer(pp)) = (*TunixShm)(unsafe.Pointer(p)).FpNext /* Free the connection p */ Xsqlite3_free(tls, p) (*TunixFile)(unsafe.Pointer(pDbFd)).FpShm = uintptr(0) Xsqlite3_mutex_leave(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FpShmMutex) /* If pShmNode->nRef has reached 0, then close the underlying ** shared-memory file, too */ _ = libc.Int32FromInt32(0) _unixEnterMutex(tls) _ = libc.Int32FromInt32(0) (*TunixShmNode)(unsafe.Pointer(pShmNode)).FnRef-- if (*TunixShmNode)(unsafe.Pointer(pShmNode)).FnRef == 0 { if deleteFlag != 0 && (*TunixShmNode)(unsafe.Pointer(pShmNode)).FhShm >= 0 { (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(16)].FpCurrent})))(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FzFilename) } _unixShmPurge(tls, pDbFd) } _unixLeaveMutex(tls) return SQLITE_OK } // C documentation // // /* // ** If it is currently memory mapped, unmap file pFd. // */ func _unixUnmapfile(tls *libc.TLS, pFd uintptr) { _ = libc.Int32FromInt32(0) if (*TunixFile)(unsafe.Pointer(pFd)).FpMapRegion != 0 { (*(*func(*libc.TLS, uintptr, Tsize_t) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(23)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(pFd)).FpMapRegion, uint32((*TunixFile)(unsafe.Pointer(pFd)).FmmapSizeActual)) (*TunixFile)(unsafe.Pointer(pFd)).FpMapRegion = uintptr(0) (*TunixFile)(unsafe.Pointer(pFd)).FmmapSize = 0 (*TunixFile)(unsafe.Pointer(pFd)).FmmapSizeActual = 0 } } // C documentation // // /* // ** Attempt to set the size of the memory mapping maintained by file // ** descriptor pFd to nNew bytes. Any existing mapping is discarded. // ** // ** If successful, this function sets the following variables: // ** // ** unixFile.pMapRegion // ** unixFile.mmapSize // ** unixFile.mmapSizeActual // ** // ** If unsuccessful, an error message is logged via sqlite3_log() and // ** the three variables above are zeroed. In this case SQLite should // ** continue accessing the database using the xRead() and xWrite() // ** methods. // */ func _unixRemapfile(tls *libc.TLS, pFd uintptr, nNew Ti64) { var flags, h int32 var nOrig, nReuse Ti64 var pNew, pOrig, pReq, zErr uintptr var v1 Tsqlite3_int64 _, _, _, _, _, _, _, _, _ = flags, h, nOrig, nReuse, pNew, pOrig, pReq, zErr, v1 zErr = __ccgo_ts + 3551 h = (*TunixFile)(unsafe.Pointer(pFd)).Fh /* File descriptor open on db file */ pOrig = (*TunixFile)(unsafe.Pointer(pFd)).FpMapRegion /* Pointer to current file mapping */ nOrig = (*TunixFile)(unsafe.Pointer(pFd)).FmmapSizeActual /* Size of pOrig region in bytes */ pNew = uintptr(0) /* Location of new mapping */ flags = int32(PROT_READ) /* Flags to pass to mmap() */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pOrig != 0 { nReuse = (*TunixFile)(unsafe.Pointer(pFd)).FmmapSize pReq = pOrig + uintptr(nReuse) /* Unmap any pages of the existing mapping that cannot be reused. */ if nReuse != nOrig { (*(*func(*libc.TLS, uintptr, Tsize_t) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(23)].FpCurrent})))(tls, pReq, uint32(nOrig-nReuse)) } pNew = (*(*func(*libc.TLS, uintptr, Tsize_t, Tsize_t, int32, uintptr) uintptr)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(24)].FpCurrent})))(tls, pOrig, uint32(nReuse), uint32(nNew), int32(MREMAP_MAYMOVE), 0) zErr = __ccgo_ts + 3563 /* The attempt to extend the existing mapping failed. Free it. */ if pNew == uintptr(-libc.Int32FromInt32(1)) || pNew == uintptr(0) { (*(*func(*libc.TLS, uintptr, Tsize_t) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(23)].FpCurrent})))(tls, pOrig, uint32(nReuse)) } } /* If pNew is still NULL, try to create an entirely new mapping. */ if pNew == uintptr(0) { pNew = (*(*func(*libc.TLS, uintptr, Tsize_t, int32, int32, int32, Toff_t) uintptr)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(22)].FpCurrent})))(tls, uintptr(0), uint32(nNew), flags, int32(MAP_SHARED), h, 0) } if pNew == uintptr(-libc.Int32FromInt32(1)) { pNew = uintptr(0) nNew = 0 _unixLogErrorAtLine(tls, SQLITE_OK, zErr, (*TunixFile)(unsafe.Pointer(pFd)).FzPath, int32(43401)) /* If the mmap() above failed, assume that all subsequent mmap() calls ** will probably fail too. Fall back to using xRead/xWrite exclusively ** in this case. */ (*TunixFile)(unsafe.Pointer(pFd)).FmmapSizeMax = 0 } (*TunixFile)(unsafe.Pointer(pFd)).FpMapRegion = pNew v1 = nNew (*TunixFile)(unsafe.Pointer(pFd)).FmmapSizeActual = v1 (*TunixFile)(unsafe.Pointer(pFd)).FmmapSize = v1 } // C documentation // // /* // ** Memory map or remap the file opened by file-descriptor pFd (if the file // ** is already mapped, the existing mapping is replaced by the new). Or, if // ** there already exists a mapping for this file, and there are still // ** outstanding xFetch() references to it, this function is a no-op. // ** // ** If parameter nByte is non-negative, then it is the requested size of // ** the mapping to create. Otherwise, if nByte is less than zero, then the // ** requested size is the size of the file on disk. The actual size of the // ** created mapping is either the requested size or the value configured // ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller. // ** // ** SQLITE_OK is returned if no error occurs (even if the mapping is not // ** recreated as a result of outstanding references) or an SQLite error // ** code otherwise. // */ func _unixMapfile(tls *libc.TLS, pFd uintptr, nMap Ti64) (r int32) { bp := tls.Alloc(112) defer tls.Free(112) var _ /* statbuf at bp+0 */ Tstat _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TunixFile)(unsafe.Pointer(pFd)).FnFetchOut > 0 { return SQLITE_OK } if nMap < 0 { /* Low-level file information */ if (*(*func(*libc.TLS, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(5)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(pFd)).Fh, bp) != 0 { return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(7)< (*TunixFile)(unsafe.Pointer(pFd)).FmmapSizeMax { nMap = (*TunixFile)(unsafe.Pointer(pFd)).FmmapSizeMax } _ = libc.Int32FromInt32(0) if nMap != (*TunixFile)(unsafe.Pointer(pFd)).FmmapSize { _unixRemapfile(tls, pFd, nMap) } return SQLITE_OK } // C documentation // // /* // ** If possible, return a pointer to a mapping of file fd starting at offset // ** iOff. The mapping must be valid for at least nAmt bytes. // ** // ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. // ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. // ** Finally, if an error does occur, return an SQLite error code. The final // ** value of *pp is undefined in this case. // ** // ** If this function does return a pointer, the caller must eventually // ** release the reference by calling unixUnfetch(). // */ func _unixFetch(tls *libc.TLS, fd uintptr, iOff Ti64, nAmt int32, pp uintptr) (r int32) { var nEofBuffer, rc int32 var pFd uintptr _, _, _ = nEofBuffer, pFd, rc pFd = fd /* The underlying database file */ *(*uintptr)(unsafe.Pointer(pp)) = uintptr(0) if (*TunixFile)(unsafe.Pointer(pFd)).FmmapSizeMax > 0 { /* Ensure that there is always at least a 256 byte buffer of addressable ** memory following the returned page. If the database is corrupt, ** SQLite may overread the page slightly (in practice only a few bytes, ** but 256 is safe, round, number). */ nEofBuffer = int32(256) if (*TunixFile)(unsafe.Pointer(pFd)).FpMapRegion == uintptr(0) { rc = _unixMapfile(tls, pFd, int64(-int32(1))) if rc != SQLITE_OK { return rc } } if (*TunixFile)(unsafe.Pointer(pFd)).FmmapSize >= iOff+int64(nAmt)+int64(nEofBuffer) { *(*uintptr)(unsafe.Pointer(pp)) = (*TunixFile)(unsafe.Pointer(pFd)).FpMapRegion + uintptr(iOff) (*TunixFile)(unsafe.Pointer(pFd)).FnFetchOut++ } } return SQLITE_OK } // C documentation // // /* // ** If the third argument is non-NULL, then this function releases a // ** reference obtained by an earlier call to unixFetch(). The second // ** argument passed to this function must be the same as the corresponding // ** argument that was passed to the unixFetch() invocation. // ** // ** Or, if the third argument is NULL, then this function is being called // ** to inform the VFS layer that, according to POSIX, any existing mapping // ** may now be invalid and should be unmapped. // */ func _unixUnfetch(tls *libc.TLS, fd uintptr, iOff Ti64, p uintptr) (r int32) { var pFd uintptr _ = pFd pFd = fd /* The underlying database file */ _ = iOff /* If p==0 (unmap the entire file) then there must be no outstanding ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), ** then there must be at least one outstanding. */ _ = libc.Int32FromInt32(0) /* If p!=0, it must match the iOff value. */ _ = libc.Int32FromInt32(0) if p != 0 { (*TunixFile)(unsafe.Pointer(pFd)).FnFetchOut-- } else { _unixUnmapfile(tls, pFd) } _ = libc.Int32FromInt32(0) return SQLITE_OK } /* ** Here ends the implementation of all sqlite3_file methods. ** ********************** End sqlite3_file Methods ******************************* ******************************************************************************/ /* ** This division contains definitions of sqlite3_io_methods objects that ** implement various file locking strategies. It also contains definitions ** of "finder" functions. A finder-function is used to locate the appropriate ** sqlite3_io_methods object for a particular database file. The pAppData ** field of the sqlite3_vfs VFS objects are initialized to be pointers to ** the correct finder-function for that VFS. ** ** Most finder functions return a pointer to a fixed sqlite3_io_methods ** object. The only interesting finder-function is autolockIoFinder, which ** looks at the filesystem type and tries to guess the best locking ** strategy from that. ** ** For finder-function F, two objects are created: ** ** (1) The real finder-function named "FImpt()". ** ** (2) A constant pointer to this function named just "F". ** ** ** A pointer to the F pointer is used as the pAppData value for VFS ** objects. We have to do this instead of letting pAppData point ** directly at the finder-function since C90 rules prevent a void* ** from be cast into a function pointer. ** ** ** Each instance of this macro generates two objects: ** ** * A constant sqlite3_io_methods object call METHOD that has locking ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. ** ** * An I/O method finder function called FINDER that returns a pointer ** to the METHOD object in the previous bullet. */ // C documentation // // /* // ** Here are all of the sqlite3_io_methods objects for each of the // ** locking strategies. Functions that return pointers to these methods // ** are also created. // */ var _posixIoMethods = Tsqlite3_io_methods{ FiVersion: int32(3), } func init() { p := unsafe.Pointer(&_posixIoMethods) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_unixClose) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_unixRead) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_unixWrite) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_unixTruncate) *(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_unixSync) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_unixFileSize) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_unixLock) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_unixUnlock) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_unixCheckReservedLock) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_unixFileControl) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_unixSectorSize) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_unixDeviceCharacteristics) *(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(_unixShmMap) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_unixShmLock) *(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_unixShmBarrier) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_unixShmUnmap) *(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_unixFetch) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_unixUnfetch) } func _posixIoFinderImpl(tls *libc.TLS, z uintptr, p uintptr) (r uintptr) { _ = z _ = p return uintptr(unsafe.Pointer(&_posixIoMethods)) } var _posixIoFinder = uintptr(0) func init() { p := unsafe.Pointer(&_posixIoFinder) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_posixIoFinderImpl) } var _nolockIoMethods = Tsqlite3_io_methods{ FiVersion: int32(3), } func init() { p := unsafe.Pointer(&_nolockIoMethods) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_nolockClose) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_unixRead) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_unixWrite) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_unixTruncate) *(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_unixSync) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_unixFileSize) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_nolockLock) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_nolockUnlock) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_nolockCheckReservedLock) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_unixFileControl) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_unixSectorSize) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_unixDeviceCharacteristics) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_unixShmLock) *(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_unixShmBarrier) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_unixShmUnmap) *(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_unixFetch) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_unixUnfetch) } func _nolockIoFinderImpl(tls *libc.TLS, z uintptr, p uintptr) (r uintptr) { _ = z _ = p return uintptr(unsafe.Pointer(&_nolockIoMethods)) } var _nolockIoFinder = uintptr(0) func init() { p := unsafe.Pointer(&_nolockIoFinder) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_nolockIoFinderImpl) } var _dotlockIoMethods = Tsqlite3_io_methods{ FiVersion: int32(1), } func init() { p := unsafe.Pointer(&_dotlockIoMethods) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_dotlockClose) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_unixRead) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_unixWrite) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_unixTruncate) *(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_unixSync) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_unixFileSize) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_dotlockLock) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_dotlockUnlock) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_dotlockCheckReservedLock) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_unixFileControl) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_unixSectorSize) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_unixDeviceCharacteristics) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_unixShmLock) *(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_unixShmBarrier) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_unixShmUnmap) *(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_unixFetch) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_unixUnfetch) } func _dotlockIoFinderImpl(tls *libc.TLS, z uintptr, p uintptr) (r uintptr) { _ = z _ = p return uintptr(unsafe.Pointer(&_dotlockIoMethods)) } var _dotlockIoFinder = uintptr(0) func init() { p := unsafe.Pointer(&_dotlockIoFinder) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_dotlockIoFinderImpl) } /* ** The proxy locking method is a "super-method" in the sense that it ** opens secondary file descriptors for the conch and lock files and ** it uses proxy, dot-file, AFP, and flock() locking methods on those ** secondary files. For this reason, the division that implements ** proxy locking is located much further down in the file. But we need ** to go ahead and define the sqlite3_io_methods and finder function ** for proxy locking here. So we forward declare the I/O methods. */ /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ // C documentation // // /* // ** An abstract type for a pointer to an IO method finder function: // */ type Tfinder_type = uintptr type finder_type = Tfinder_type /**************************************************************************** **************************** sqlite3_vfs methods **************************** ** ** This division contains the implementation of methods on the ** sqlite3_vfs object. */ // C documentation // // /* // ** Initialize the contents of the unixFile structure pointed to by pId. // */ func _fillInUnixFile(tls *libc.TLS, pVfs uintptr, h int32, pId uintptr, zFilename uintptr, ctrlFlags int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var nFilename, rc int32 var pLockingStyle, pNew, zLockFile, v1, p2, p3 uintptr _, _, _, _, _, _, _, _ = nFilename, pLockingStyle, pNew, rc, zLockFile, v1, p2, p3 pNew = pId rc = SQLITE_OK _ = libc.Int32FromInt32(0) /* No locking occurs in temporary files */ _ = libc.Int32FromInt32(0) (*TunixFile)(unsafe.Pointer(pNew)).Fh = h (*TunixFile)(unsafe.Pointer(pNew)).FpVfs = pVfs (*TunixFile)(unsafe.Pointer(pNew)).FzPath = zFilename (*TunixFile)(unsafe.Pointer(pNew)).FctrlFlags = uint16(uint8(ctrlFlags)) (*TunixFile)(unsafe.Pointer(pNew)).FmmapSizeMax = _sqlite3Config.FszMmap if ctrlFlags&int32(UNIXFILE_URI) != 0 { v1 = zFilename } else { v1 = uintptr(0) } if Xsqlite3_uri_boolean(tls, v1, __ccgo_ts+3829, int32(SQLITE_POWERSAFE_OVERWRITE)) != 0 { p2 = pNew + 18 *(*uint16)(unsafe.Pointer(p2)) = uint16(int32(*(*uint16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(UNIXFILE_PSOW)) } if libc.Xstrcmp(tls, (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FzName, __ccgo_ts+3834) == 0 { p3 = pNew + 18 *(*uint16)(unsafe.Pointer(p3)) = uint16(int32(*(*uint16)(unsafe.Pointer(p3))) | libc.Int32FromInt32(UNIXFILE_EXCL)) } if ctrlFlags&int32(UNIXFILE_NOLOCK) != 0 { pLockingStyle = uintptr(unsafe.Pointer(&_nolockIoMethods)) } else { pLockingStyle = (*(*func(*libc.TLS, uintptr, uintptr) uintptr)(unsafe.Pointer(&struct{ uintptr }{*(*uintptr)(unsafe.Pointer((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData))})))(tls, zFilename, pNew) } if pLockingStyle == uintptr(unsafe.Pointer(&_posixIoMethods)) { _unixEnterMutex(tls) rc = _findInodeInfo(tls, pNew, pNew+8) if rc != SQLITE_OK { /* If an error occurred in findInodeInfo(), close the file descriptor ** immediately, before releasing the mutex. findInodeInfo() may fail ** in two scenarios: ** ** (a) A call to fstat() failed. ** (b) A malloc failed. ** ** Scenario (b) may only occur if the process is holding no other ** file descriptors open on the same file. If there were other file ** descriptors on this file, then no malloc would be required by ** findInodeInfo(). If this is the case, it is quite safe to close ** handle h - as it is guaranteed that no posix locks will be released ** by doing so. ** ** If scenario (a) caused the error then things are not so safe. The ** implicit assumption here is that if fstat() fails, things are in ** such bad shape that dropping a lock or two doesn't matter much. */ _robust_close(tls, pNew, h, int32(43909)) h = -int32(1) } _unixLeaveMutex(tls) } else { if pLockingStyle == uintptr(unsafe.Pointer(&_dotlockIoMethods)) { _ = libc.Int32FromInt32(0) nFilename = int32(libc.Xstrlen(tls, zFilename)) + int32(6) zLockFile = Xsqlite3_malloc64(tls, uint64(nFilename)) if zLockFile == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { Xsqlite3_snprintf(tls, nFilename, zLockFile, __ccgo_ts+3844, libc.VaList(bp+8, zFilename)) } (*TunixFile)(unsafe.Pointer(pNew)).FlockingContext = zLockFile } } _storeLastErrno(tls, pNew, 0) if rc != SQLITE_OK { if h >= 0 { _robust_close(tls, pNew, h, int32(43994)) } } else { (*Tsqlite3_file)(unsafe.Pointer(pId)).FpMethods = pLockingStyle _verifyDbFile(tls, pNew) } return rc } // C documentation // // /* // ** Directories to consider for temp files. // */ var _azTempDirs = [6]uintptr{ 2: __ccgo_ts + 3852, 3: __ccgo_ts + 3861, 4: __ccgo_ts + 3870, 5: __ccgo_ts + 1661, } // C documentation // // /* // ** Initialize first two members of azTempDirs[] array. // */ func _unixTempFileInit(tls *libc.TLS) { _azTempDirs[0] = libc.Xgetenv(tls, __ccgo_ts+3875) _azTempDirs[int32(1)] = libc.Xgetenv(tls, __ccgo_ts+3889) } // C documentation // // /* // ** Return the name of a directory in which to put temporary files. // ** If no suitable temporary file directory can be found, return NULL. // */ func _unixTempFileDir(tls *libc.TLS) (r uintptr) { bp := tls.Alloc(112) defer tls.Free(112) var i, v1 uint32 var zDir uintptr var _ /* buf at bp+0 */ Tstat _, _, _ = i, zDir, v1 i = uint32(0) zDir = Xsqlite3_temp_directory for int32(1) != 0 { if zDir != uintptr(0) && (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(4)].FpCurrent})))(tls, zDir, bp) == 0 && (*(*Tstat)(unsafe.Pointer(bp))).Fst_mode&uint32(__S_IFMT) == uint32(libc.Int32FromInt32(__S_IFDIR)) && (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(2)].FpCurrent})))(tls, zDir, int32(03)) == 0 { return zDir } if i >= libc.Uint32FromInt64(24)/libc.Uint32FromInt64(4) { break } v1 = i i++ zDir = _azTempDirs[v1] } return uintptr(0) } // C documentation // // /* // ** Create a temporary file name in zBuf. zBuf must be allocated // ** by the calling process and must be big enough to hold at least // ** pVfs->mxPathname bytes. // */ func _unixGetTempname(tls *libc.TLS, nBuf int32, zBuf uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var iLimit, rc, v1 int32 var zDir uintptr var v2 bool var _ /* r at bp+0 */ Tu64 _, _, _, _, _ = iLimit, rc, zDir, v1, v2 iLimit = 0 rc = SQLITE_OK /* It's odd to simulate an io-error here, but really this is just ** using the io-error infrastructure to test that SQLite handles this ** function failing. */ *(*uint8)(unsafe.Pointer(zBuf)) = uint8(0) Xsqlite3_mutex_enter(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1))) zDir = _unixTempFileDir(tls) if zDir == uintptr(0) { rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(25)< int32(10) { rc = int32(SQLITE_ERROR) break } } } Xsqlite3_mutex_leave(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1))) return rc } // C documentation // // /* // ** Search for an unused file descriptor that was opened on the database // ** file (not a journal or super-journal file) identified by pathname // ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second // ** argument to this function. // ** // ** Such a file descriptor may exist if a database connection was closed // ** but the associated file descriptor could not be closed because some // ** other file descriptor open on the same file is holding a file-lock. // ** Refer to comments in the unixClose() function and the lengthy comment // ** describing "Posix Advisory Locking" at the start of this file for // ** further details. Also, ticket #4018. // ** // ** If a suitable file descriptor is found, then it is returned. If no // ** such file descriptor is located, -1 is returned. // */ func _findReusableFd(tls *libc.TLS, zPath uintptr, flags int32) (r uintptr) { bp := tls.Alloc(112) defer tls.Free(112) var pInode, pUnused, pp uintptr var _ /* sStat at bp+0 */ Tstat _, _, _ = pInode, pUnused, pp pUnused = uintptr(0) /* Results of stat() call */ _unixEnterMutex(tls) /* A stat() call may fail for various reasons. If this happens, it is ** almost certain that an open() call on the same path will also fail. ** For this reason, if an error occurs in the stat() call here, it is ** ignored and -1 is returned. The caller will try to open a new file ** descriptor on the same path, fail, and return an error to SQLite. ** ** Even if a subsequent open() call does succeed, the consequences of ** not searching for a reusable file descriptor are not dire. */ if _inodeList != uintptr(0) && 0 == (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(4)].FpCurrent})))(tls, zPath, bp) { pInode = _inodeList for pInode != 0 && ((*TunixInodeInfo)(unsafe.Pointer(pInode)).FfileId.Fdev != (*(*Tstat)(unsafe.Pointer(bp))).Fst_dev || (*TunixInodeInfo)(unsafe.Pointer(pInode)).FfileId.Fino != (*(*Tstat)(unsafe.Pointer(bp))).Fst_ino) { pInode = (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpNext } if pInode != 0 { _ = libc.Int32FromInt32(0) Xsqlite3_mutex_enter(tls, (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpLockMutex) flags &= libc.Int32FromInt32(SQLITE_OPEN_READONLY) | libc.Int32FromInt32(SQLITE_OPEN_READWRITE) pp = pInode + 32 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != 0 && (*TUnixUnusedFd)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).Fflags != flags) { break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 8 } pUnused = *(*uintptr)(unsafe.Pointer(pp)) if pUnused != 0 { *(*uintptr)(unsafe.Pointer(pp)) = (*TUnixUnusedFd)(unsafe.Pointer(pUnused)).FpNext } Xsqlite3_mutex_leave(tls, (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpLockMutex) } } _unixLeaveMutex(tls) return pUnused } // C documentation // // /* // ** Find the mode, uid and gid of file zFile. // */ func _getFileMode(tls *libc.TLS, zFile uintptr, pMode uintptr, pUid uintptr, pGid uintptr) (r int32) { bp := tls.Alloc(112) defer tls.Free(112) var rc int32 var _ /* sStat at bp+0 */ Tstat _ = rc /* Output of stat() on database file */ rc = SQLITE_OK if 0 == (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(4)].FpCurrent})))(tls, zFile, bp) { *(*Tmode_t)(unsafe.Pointer(pMode)) = (*(*Tstat)(unsafe.Pointer(bp))).Fst_mode & uint32(0777) *(*Tuid_t)(unsafe.Pointer(pUid)) = (*(*Tstat)(unsafe.Pointer(bp))).Fst_uid *(*Tgid_t)(unsafe.Pointer(pGid)) = (*(*Tstat)(unsafe.Pointer(bp))).Fst_gid } else { rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(7)<-journal" ** "-wal" ** "-journalNN" ** "-walNN" ** ** where NN is a decimal number. The NN naming schemes are ** used by the test_multiplex.c module. ** ** In normal operation, the journal file name will always contain ** a '-' character. However in 8+3 filename mode, or if a corrupt ** rollback journal specifies a super-journal with a goofy name, then ** the '-' might be missing or the '-' might be the first character in ** the filename. In that case, just return SQLITE_OK with *pMode==0. */ nDb = _sqlite3Strlen30(tls, zPath) - int32(1) for nDb > 0 && int32(*(*uint8)(unsafe.Pointer(zPath + uintptr(nDb)))) != int32('.') { if int32(*(*uint8)(unsafe.Pointer(zPath + uintptr(nDb)))) == int32('-') { libc.Xmemcpy(tls, bp, zPath, uint32(nDb)) (*(*[513]uint8)(unsafe.Pointer(bp)))[nDb] = uint8('\000') rc = _getFileMode(tls, bp, pMode, pUid, pGid) break } nDb-- } } else { if flags&int32(SQLITE_OPEN_DELETEONCLOSE) != 0 { *(*Tmode_t)(unsafe.Pointer(pMode)) = uint32(0600) } else { if flags&int32(SQLITE_OPEN_URI) != 0 { /* If this is a main database file and the file was opened using a URI ** filename, check for the "modeof" parameter. If present, interpret ** its value as a filename and try to copy the mode, uid and gid from ** that file. */ z = Xsqlite3_uri_parameter(tls, zPath, __ccgo_ts+3913) if z != 0 { rc = _getFileMode(tls, z, pMode, pUid, pGid) } } } } return rc } // C documentation // // /* // ** Open the file zPath. // ** // ** Previously, the SQLite OS layer used three functions in place of this // ** one: // ** // ** sqlite3OsOpenReadWrite(); // ** sqlite3OsOpenReadOnly(); // ** sqlite3OsOpenExclusive(); // ** // ** These calls correspond to the following combinations of flags: // ** // ** ReadWrite() -> (READWRITE | CREATE) // ** ReadOnly() -> (READONLY) // ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) // ** // ** The old OpenExclusive() accepted a boolean argument - "delFlag". If // ** true, the file was configured to be automatically deleted when the // ** file handle closed. To achieve the same effect using this new // ** interface, add the DELETEONCLOSE flag to those specified above for // ** OpenExclusive(). // */ func _unixOpen(tls *libc.TLS, pVfs uintptr, zPath uintptr, pFile uintptr, flags int32, pOutFlags uintptr) (r int32) { bp := tls.Alloc(528) defer tls.Free(528) var ctrlFlags, eType, fd, isCreate, isDelete, isExclusive, isNewJrnl, isReadWrite, isReadonly, noLock, openFlags, rc, rc2 int32 var p, pUnused, zName uintptr var _ /* gid at bp+524 */ Tgid_t var _ /* openMode at bp+516 */ Tmode_t var _ /* uid at bp+520 */ Tuid_t var _ /* zTmpname at bp+0 */ [514]uint8 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = ctrlFlags, eType, fd, isCreate, isDelete, isExclusive, isNewJrnl, isReadWrite, isReadonly, noLock, openFlags, p, pUnused, rc, rc2, zName p = pFile fd = -int32(1) /* File descriptor returned by open() */ openFlags = 0 /* Flags to pass to open() */ eType = flags & int32(0x0FFF00) /* True to omit locking primitives */ rc = SQLITE_OK /* Function Return Code */ ctrlFlags = 0 /* UNIXFILE_* flags */ isExclusive = flags & int32(SQLITE_OPEN_EXCLUSIVE) isDelete = flags & int32(SQLITE_OPEN_DELETEONCLOSE) isCreate = flags & int32(SQLITE_OPEN_CREATE) isReadonly = flags & int32(SQLITE_OPEN_READONLY) isReadWrite = flags & int32(SQLITE_OPEN_READWRITE) /* If creating a super- or main-file journal, this function will open ** a file-descriptor on the directory too. The first time unixSync() ** is called the directory file descriptor will be fsync()ed and close()d. */ isNewJrnl = libc.BoolInt32(isCreate != 0 && (eType == int32(SQLITE_OPEN_SUPER_JOURNAL) || eType == int32(SQLITE_OPEN_MAIN_JOURNAL) || eType == int32(SQLITE_OPEN_WAL))) zName = zPath /* Check the following statements are true: ** ** (a) Exactly one of the READWRITE and READONLY flags must be set, and ** (b) if CREATE is set, then READWRITE must also be set, and ** (c) if EXCLUSIVE is set, then CREATE must also be set. ** (d) if DELETEONCLOSE is set, then CREATE must also be set. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* The main DB, main journal, WAL file and super-journal are never ** automatically deleted. Nor are they ever temporary files. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Assert that the upper layer has set one of the "file-type" flags. */ _ = libc.Int32FromInt32(0) /* Detect a pid change and reset the PRNG. There is a race condition ** here such that two or more threads all trying to open databases at ** the same instant might all reset the PRNG. But multiple resets ** are harmless. */ if libc.AtomicLoadPInt32(uintptr(unsafe.Pointer(&_randomnessPid))) != libc.Xgetpid(tls) { libc.AtomicStorePInt32(uintptr(unsafe.Pointer(&_randomnessPid)), libc.Xgetpid(tls)) Xsqlite3_randomness(tls, 0, uintptr(0)) } libc.Xmemset(tls, p, 0, uint32(88)) if eType == int32(SQLITE_OPEN_MAIN_DB) { pUnused = _findReusableFd(tls, zName, flags) if pUnused != 0 { fd = (*TUnixUnusedFd)(unsafe.Pointer(pUnused)).Ffd } else { pUnused = Xsqlite3_malloc64(tls, uint64(12)) if !(pUnused != 0) { return int32(SQLITE_NOMEM) } } (*TunixFile)(unsafe.Pointer(p)).FpPreallocatedUnused = pUnused /* Database filenames are double-zero terminated if they are not ** URIs with parameters. Hence, they can always be passed into ** sqlite3_uri_parameter(). */ _ = libc.Int32FromInt32(0) } else { if !(zName != 0) { /* If zName is NULL, the upper layer is requesting a temp file. */ _ = libc.Int32FromInt32(0) rc = _unixGetTempname(tls, (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname, bp) if rc != SQLITE_OK { return rc } zName = bp /* Generated temporary filenames are always double-zero terminated ** for use by sqlite3_uri_parameter(). */ _ = libc.Int32FromInt32(0) } } /* Determine the value of the flags parameter passed to POSIX function ** open(). These must be calculated even if open() is not called, as ** they may be stored as part of the file handle and used by the ** 'conch file' locking functions later on. */ if isReadonly != 0 { openFlags |= O_RDONLY } if isReadWrite != 0 { openFlags |= int32(O_RDWR) } if isCreate != 0 { openFlags |= int32(O_CREAT) } if isExclusive != 0 { openFlags |= libc.Int32FromInt32(O_EXCL) | libc.Int32FromInt32(__O_NOFOLLOW) } openFlags |= libc.Int32FromInt32(__O_LARGEFILE) | libc.Int32FromInt32(O_BINARY) | libc.Int32FromInt32(__O_NOFOLLOW) if fd < 0 { /* Groupid for the file */ rc = _findCreateFileMode(tls, zName, flags, bp+516, bp+520, bp+524) if rc != SQLITE_OK { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return rc } fd = _robust_open(tls, zName, openFlags, *(*Tmode_t)(unsafe.Pointer(bp + 516))) _ = libc.Int32FromInt32(0) if fd < 0 { if isNewJrnl != 0 && *(*int32)(unsafe.Pointer(libc.X__errno_location(tls))) == int32(EACCES) && (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(2)].FpCurrent})))(tls, zName, F_OK) != 0 { /* If unable to create a journal because the directory is not ** writable, change the error code to indicate that. */ rc = libc.Int32FromInt32(SQLITE_READONLY) | libc.Int32FromInt32(6)< 0)) } else { *(*int32)(unsafe.Pointer(pResOut)) = libc.BoolInt32((*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(2)].FpCurrent})))(tls, zPath, libc.Int32FromInt32(W_OK)|libc.Int32FromInt32(R_OK)) == 0) } return SQLITE_OK } // C documentation // // /* // ** A pathname under construction // */ type TDbPath = struct { Frc int32 FnSymlink int32 FzOut uintptr FnOut int32 FnUsed int32 } type DbPath = TDbPath type TDbPath1 = struct { Frc int32 FnSymlink int32 FzOut uintptr FnOut int32 FnUsed int32 } type DbPath1 = TDbPath1 // C documentation // // /* // ** Append a single path element to the DbPath under construction // */ func _appendOnePathElement(tls *libc.TLS, pPath uintptr, zName uintptr, nName int32) { bp := tls.Alloc(4208) defer tls.Free(4208) var got Tssize_t var zIn, v2, v4, v6 uintptr var v1, v3, v5 int32 var _ /* buf at bp+0 */ Tstat var _ /* zLnk at bp+104 */ [4098]uint8 _, _, _, _, _, _, _, _ = got, zIn, v1, v2, v3, v4, v5, v6 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(*(*uint8)(unsafe.Pointer(zName))) == int32('.') { if nName == int32(1) { return } if int32(*(*uint8)(unsafe.Pointer(zName + 1))) == int32('.') && nName == int32(2) { if (*TDbPath)(unsafe.Pointer(pPath)).FnUsed > int32(1) { _ = libc.Int32FromInt32(0) for { v2 = pPath + 16 *(*int32)(unsafe.Pointer(v2))-- v1 = *(*int32)(unsafe.Pointer(v2)) if !(int32(*(*uint8)(unsafe.Pointer((*TDbPath)(unsafe.Pointer(pPath)).FzOut + uintptr(v1)))) != int32('/')) { break } } } return } } if (*TDbPath)(unsafe.Pointer(pPath)).FnUsed+nName+int32(2) >= (*TDbPath)(unsafe.Pointer(pPath)).FnOut { (*TDbPath)(unsafe.Pointer(pPath)).Frc = int32(SQLITE_ERROR) return } v4 = pPath + 16 v3 = *(*int32)(unsafe.Pointer(v4)) *(*int32)(unsafe.Pointer(v4))++ *(*uint8)(unsafe.Pointer((*TDbPath)(unsafe.Pointer(pPath)).FzOut + uintptr(v3))) = uint8('/') libc.Xmemcpy(tls, (*TDbPath)(unsafe.Pointer(pPath)).FzOut+uintptr((*TDbPath)(unsafe.Pointer(pPath)).FnUsed), zName, uint32(nName)) *(*int32)(unsafe.Pointer(pPath + 16)) += nName if (*TDbPath)(unsafe.Pointer(pPath)).Frc == SQLITE_OK { *(*uint8)(unsafe.Pointer((*TDbPath)(unsafe.Pointer(pPath)).FzOut + uintptr((*TDbPath)(unsafe.Pointer(pPath)).FnUsed))) = uint8(0) zIn = (*TDbPath)(unsafe.Pointer(pPath)).FzOut if (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(27)].FpCurrent})))(tls, zIn, bp) != 0 { if *(*int32)(unsafe.Pointer(libc.X__errno_location(tls))) != int32(ENOENT) { (*TDbPath)(unsafe.Pointer(pPath)).Frc = _unixLogErrorAtLine(tls, _sqlite3CantopenError(tls, int32(44680)), __ccgo_ts+3591, zIn, int32(44680)) } } else { if (*(*Tstat)(unsafe.Pointer(bp))).Fst_mode&uint32(__S_IFMT) == uint32(libc.Int32FromInt32(__S_IFLNK)) { v6 = pPath + 4 v5 = *(*int32)(unsafe.Pointer(v6)) *(*int32)(unsafe.Pointer(v6))++ if v5 > int32(SQLITE_MAX_SYMLINK) { (*TDbPath)(unsafe.Pointer(pPath)).Frc = _sqlite3CantopenError(tls, int32(44686)) return } got = (*(*func(*libc.TLS, uintptr, uintptr, Tsize_t) Tssize_t)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls, zIn, bp+104, libc.Uint32FromInt64(4098)-libc.Uint32FromInt32(2)) if got <= 0 || got >= libc.Int32FromInt64(4098)-libc.Int32FromInt32(2) { (*TDbPath)(unsafe.Pointer(pPath)).Frc = _unixLogErrorAtLine(tls, _sqlite3CantopenError(tls, int32(44691)), __ccgo_ts+3582, zIn, int32(44691)) return } (*(*[4098]uint8)(unsafe.Pointer(bp + 104)))[got] = uint8(0) if int32((*(*[4098]uint8)(unsafe.Pointer(bp + 104)))[0]) == int32('/') { (*TDbPath)(unsafe.Pointer(pPath)).FnUsed = 0 } else { *(*int32)(unsafe.Pointer(pPath + 16)) -= nName + int32(1) } _appendAllPathElements(tls, pPath, bp+104) } } } } // C documentation // // /* // ** Append all path elements in zPath to the DbPath under construction. // */ func _appendAllPathElements(tls *libc.TLS, pPath uintptr, zPath uintptr) { var i, j, v1 int32 _, _, _ = i, j, v1 i = 0 j = 0 for { for *(*uint8)(unsafe.Pointer(zPath + uintptr(i))) != 0 && int32(*(*uint8)(unsafe.Pointer(zPath + uintptr(i)))) != int32('/') { i++ } if i > j { _appendOnePathElement(tls, pPath, zPath+uintptr(j), i-j) } j = i + int32(1) goto _2 _2: ; v1 = i i++ if !(*(*uint8)(unsafe.Pointer(zPath + uintptr(v1))) != 0) { break } } } // C documentation // // /* // ** Turn a relative pathname into a full pathname. The relative path // ** is stored as a nul-terminated string in the buffer pointed to by // ** zPath. // ** // ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes // ** (in this case, MAX_PATHNAME bytes). The full-path is written to // ** this buffer before returning. // */ func _unixFullPathname(tls *libc.TLS, pVfs uintptr, zPath uintptr, nOut int32, zOut uintptr) (r int32) { bp := tls.Alloc(4128) defer tls.Free(4128) var _ /* path at bp+0 */ TDbPath var _ /* zPwd at bp+20 */ [4098]uint8 _ = pVfs (*(*TDbPath)(unsafe.Pointer(bp))).Frc = 0 (*(*TDbPath)(unsafe.Pointer(bp))).FnUsed = 0 (*(*TDbPath)(unsafe.Pointer(bp))).FnSymlink = 0 (*(*TDbPath)(unsafe.Pointer(bp))).FnOut = nOut (*(*TDbPath)(unsafe.Pointer(bp))).FzOut = zOut if int32(*(*uint8)(unsafe.Pointer(zPath))) != int32('/') { if (*(*func(*libc.TLS, uintptr, Tsize_t) uintptr)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(3)].FpCurrent})))(tls, bp+20, libc.Uint32FromInt64(4098)-libc.Uint32FromInt32(2)) == uintptr(0) { return _unixLogErrorAtLine(tls, _sqlite3CantopenError(tls, int32(44749)), __ccgo_ts+3411, zPath, int32(44749)) } _appendAllPathElements(tls, bp, bp+20) } _appendAllPathElements(tls, bp, zPath) *(*uint8)(unsafe.Pointer(zOut + uintptr((*(*TDbPath)(unsafe.Pointer(bp))).FnUsed))) = uint8(0) if (*(*TDbPath)(unsafe.Pointer(bp))).Frc != 0 || (*(*TDbPath)(unsafe.Pointer(bp))).FnUsed < int32(2) { return _sqlite3CantopenError(tls, int32(44755)) } if (*(*TDbPath)(unsafe.Pointer(bp))).FnSymlink != 0 { return libc.Int32FromInt32(SQLITE_OK) | libc.Int32FromInt32(2)< 0x40000200 ** ** This works well on the local file system, but shows a nearly 100x ** slowdown in read performance on AFP because the AFP client disables ** the read cache when byte-range locks are present. Enabling the read ** cache exposes a cache coherency problem that is present on all OS X ** supported network file systems. NFS and AFP both observe the ** close-to-open semantics for ensuring cache coherency ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively ** address the requirements for concurrent database access by multiple ** readers and writers ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. ** ** To address the performance and cache coherency issues, proxy file locking ** changes the way database access is controlled by limiting access to a ** single host at a time and moving file locks off of the database file ** and onto a proxy file on the local file system. ** ** ** Using proxy locks ** ----------------- ** ** C APIs ** ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE, ** | ":auto:"); ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE, ** &); ** ** ** SQL pragmas ** ** PRAGMA [database.]lock_proxy_file= | :auto: ** PRAGMA [database.]lock_proxy_file ** ** Specifying ":auto:" means that if there is a conch file with a matching ** host ID in it, the proxy path in the conch file will be used, otherwise ** a proxy path based on the user's temp dir ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the ** actual proxy file name is generated from the name and path of the ** database file. For example: ** ** For database path "/Users/me/foo.db" ** The lock path will be "/sqliteplocks/_Users_me_foo.db:auto:") ** ** Once a lock proxy is configured for a database connection, it can not ** be removed, however it may be switched to a different proxy path via ** the above APIs (assuming the conch file is not being held by another ** connection or process). ** ** ** How proxy locking works ** ----------------------- ** ** Proxy file locking relies primarily on two new supporting files: ** ** * conch file to limit access to the database file to a single host ** at a time ** ** * proxy file to act as a proxy for the advisory locks normally ** taken on the database ** ** The conch file - to use a proxy file, sqlite must first "hold the conch" ** by taking an sqlite-style shared lock on the conch file, reading the ** contents and comparing the host's unique host ID (see below) and lock ** proxy path against the values stored in the conch. The conch file is ** stored in the same directory as the database file and the file name ** is patterned after the database file name as ".-conch". ** If the conch file does not exist, or its contents do not match the ** host ID and/or proxy path, then the lock is escalated to an exclusive ** lock and the conch file contents is updated with the host ID and proxy ** path and the lock is downgraded to a shared lock again. If the conch ** is held by another process (with a shared lock), the exclusive lock ** will fail and SQLITE_BUSY is returned. ** ** The proxy file - a single-byte file used for all advisory file locks ** normally taken on the database file. This allows for safe sharing ** of the database file for multiple readers and writers on the same ** host (the conch ensures that they all use the same local lock file). ** ** Requesting the lock proxy does not immediately take the conch, it is ** only taken when the first request to lock database file is made. ** This matches the semantics of the traditional locking behavior, where ** opening a connection to a database file does not take a lock on it. ** The shared lock and an open file descriptor are maintained until ** the connection to the database is closed. ** ** The proxy file and the lock file are never deleted so they only need ** to be created the first time they are used. ** ** Configuration options ** --------------------- ** ** SQLITE_PREFER_PROXY_LOCKING ** ** Database files accessed on non-local file systems are ** automatically configured for proxy locking, lock files are ** named automatically using the same logic as ** PRAGMA lock_proxy_file=":auto:" ** ** SQLITE_PROXY_DEBUG ** ** Enables the logging of error messages during host id file ** retrieval and creation ** ** LOCKPROXYDIR ** ** Overrides the default directory used for lock proxy files that ** are named automatically via the ":auto:" setting ** ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS ** ** Permissions to use when creating a directory for storing the ** lock proxy files, only used when LOCKPROXYDIR is not set. ** ** ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will ** force proxy locking to be used for every database file opened, and 0 ** will force automatic proxy locking to be disabled for all database ** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). */ /* ** Proxy locking is only available on MacOSX */ /* ** The proxy locking style is intended for use with AFP filesystems. ** And since AFP is only supported on MacOSX, the proxy locking is also ** restricted to MacOSX. ** ** ******************* End of the proxy lock implementation ********************** ******************************************************************************/ // C documentation // // /* // ** Initialize the operating system interface. // ** // ** This routine registers all VFS implementations for unix-like operating // ** systems. This routine, and the sqlite3_os_end() routine that follows, // ** should be the only routines in this file that are visible from other // ** files. // ** // ** This routine is called once during SQLite initialization and by a // ** single thread. The memory allocation and mutex subsystems have not // ** necessarily been initialized when this routine is called, and so they // ** should not be used. // */ func Xsqlite3_os_init(tls *libc.TLS) (r int32) { var i uint32 _ = i /* Double-check that the aSyscall[] array has been constructed ** correctly. See ticket [bb3a86e890c8e96ab] */ _ = libc.Int32FromInt32(0) /* Register all VFSes defined in the aVfs[] array */ i = uint32(0) for { if !(i < libc.Uint32FromInt64(352)/libc.Uint32FromInt64(88)) { break } Xsqlite3_vfs_register(tls, uintptr(unsafe.Pointer(&_aVfs))+uintptr(i)*88, libc.BoolInt32(i == uint32(0))) goto _1 _1: ; i++ } _unixBigLock = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1)) /* Validate lock assumptions */ _ = libc.Int32FromInt32(0) /* Number of available locks */ _ = libc.Int32FromInt32(0) /* Start of locking area */ /* Locks: ** WRITE UNIX_SHM_BASE 120 ** CKPT UNIX_SHM_BASE+1 121 ** RECOVER UNIX_SHM_BASE+2 122 ** READ-0 UNIX_SHM_BASE+3 123 ** READ-1 UNIX_SHM_BASE+4 124 ** READ-2 UNIX_SHM_BASE+5 125 ** READ-3 UNIX_SHM_BASE+6 126 ** READ-4 UNIX_SHM_BASE+7 127 ** DMS UNIX_SHM_BASE+8 128 */ _ = libc.Int32FromInt32(0) /* Byte offset of the deadman-switch */ /* Initialize temp file dir array. */ _unixTempFileInit(tls) return SQLITE_OK } /* ** The following macro defines an initializer for an sqlite3_vfs object. ** The name of the VFS is NAME. The pAppData is a pointer to a pointer ** to the "finder" function. (pAppData is a pointer to a pointer because ** silly C90 rules prohibit a void* from being cast to a function pointer ** and so we have to go through the intermediate pointer to avoid problems ** when compiling with -pedantic-errors on GCC.) ** ** The FINDER parameter to this macro is the name of the pointer to the ** finder-function. The finder-function returns a pointer to the ** sqlite_io_methods object that implements the desired locking ** behaviors. See the division above that contains the IOMETHODS ** macro for addition information on finder-functions. ** ** Most finders simply return a pointer to a fixed sqlite3_io_methods ** object. But the "autolockIoFinder" available on MacOSX does a little ** more than that; it looks at the filesystem type that hosts the ** database file and tries to choose an locking method appropriate for ** that filesystem time. */ /* ** All default VFSes for unix are contained in the following array. ** ** Note that the sqlite3_vfs.pNext field of the VFS object is modified ** by the SQLite core when the VFS is registered. So the following ** array cannot be const. */ var _aVfs = [4]Tsqlite3_vfs{ 0: { FiVersion: int32(3), FszOsFile: int32(88), FmxPathname: int32(MAX_PATHNAME), FzName: __ccgo_ts + 3939, }, 1: { FiVersion: int32(3), FszOsFile: int32(88), FmxPathname: int32(MAX_PATHNAME), FzName: __ccgo_ts + 3944, }, 2: { FiVersion: int32(3), FszOsFile: int32(88), FmxPathname: int32(MAX_PATHNAME), FzName: __ccgo_ts + 3954, }, 3: { FiVersion: int32(3), FszOsFile: int32(88), FmxPathname: int32(MAX_PATHNAME), FzName: __ccgo_ts + 3834, }, } func init() { p := unsafe.Pointer(&_aVfs) *(*uintptr)(unsafe.Add(p, 20)) = uintptr(unsafe.Pointer(&_posixIoFinder)) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_unixOpen) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_unixDelete) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_unixAccess) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_unixFullPathname) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_unixDlOpen) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_unixDlError) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_unixDlSym) *(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(_unixDlClose) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_unixRandomness) *(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_unixSleep) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_unixCurrentTime) *(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_unixGetLastError) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_unixCurrentTimeInt64) *(*uintptr)(unsafe.Add(p, 76)) = __ccgo_fp(_unixSetSystemCall) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_unixGetSystemCall) *(*uintptr)(unsafe.Add(p, 84)) = __ccgo_fp(_unixNextSystemCall) *(*uintptr)(unsafe.Add(p, 108)) = uintptr(unsafe.Pointer(&_nolockIoFinder)) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_unixOpen) *(*uintptr)(unsafe.Add(p, 116)) = __ccgo_fp(_unixDelete) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_unixAccess) *(*uintptr)(unsafe.Add(p, 124)) = __ccgo_fp(_unixFullPathname) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(_unixDlOpen) *(*uintptr)(unsafe.Add(p, 132)) = __ccgo_fp(_unixDlError) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_unixDlSym) *(*uintptr)(unsafe.Add(p, 140)) = __ccgo_fp(_unixDlClose) *(*uintptr)(unsafe.Add(p, 144)) = __ccgo_fp(_unixRandomness) *(*uintptr)(unsafe.Add(p, 148)) = __ccgo_fp(_unixSleep) *(*uintptr)(unsafe.Add(p, 152)) = __ccgo_fp(_unixCurrentTime) *(*uintptr)(unsafe.Add(p, 156)) = __ccgo_fp(_unixGetLastError) *(*uintptr)(unsafe.Add(p, 160)) = __ccgo_fp(_unixCurrentTimeInt64) *(*uintptr)(unsafe.Add(p, 164)) = __ccgo_fp(_unixSetSystemCall) *(*uintptr)(unsafe.Add(p, 168)) = __ccgo_fp(_unixGetSystemCall) *(*uintptr)(unsafe.Add(p, 172)) = __ccgo_fp(_unixNextSystemCall) *(*uintptr)(unsafe.Add(p, 196)) = uintptr(unsafe.Pointer(&_dotlockIoFinder)) *(*uintptr)(unsafe.Add(p, 200)) = __ccgo_fp(_unixOpen) *(*uintptr)(unsafe.Add(p, 204)) = __ccgo_fp(_unixDelete) *(*uintptr)(unsafe.Add(p, 208)) = __ccgo_fp(_unixAccess) *(*uintptr)(unsafe.Add(p, 212)) = __ccgo_fp(_unixFullPathname) *(*uintptr)(unsafe.Add(p, 216)) = __ccgo_fp(_unixDlOpen) *(*uintptr)(unsafe.Add(p, 220)) = __ccgo_fp(_unixDlError) *(*uintptr)(unsafe.Add(p, 224)) = __ccgo_fp(_unixDlSym) *(*uintptr)(unsafe.Add(p, 228)) = __ccgo_fp(_unixDlClose) *(*uintptr)(unsafe.Add(p, 232)) = __ccgo_fp(_unixRandomness) *(*uintptr)(unsafe.Add(p, 236)) = __ccgo_fp(_unixSleep) *(*uintptr)(unsafe.Add(p, 240)) = __ccgo_fp(_unixCurrentTime) *(*uintptr)(unsafe.Add(p, 244)) = __ccgo_fp(_unixGetLastError) *(*uintptr)(unsafe.Add(p, 248)) = __ccgo_fp(_unixCurrentTimeInt64) *(*uintptr)(unsafe.Add(p, 252)) = __ccgo_fp(_unixSetSystemCall) *(*uintptr)(unsafe.Add(p, 256)) = __ccgo_fp(_unixGetSystemCall) *(*uintptr)(unsafe.Add(p, 260)) = __ccgo_fp(_unixNextSystemCall) *(*uintptr)(unsafe.Add(p, 284)) = uintptr(unsafe.Pointer(&_posixIoFinder)) *(*uintptr)(unsafe.Add(p, 288)) = __ccgo_fp(_unixOpen) *(*uintptr)(unsafe.Add(p, 292)) = __ccgo_fp(_unixDelete) *(*uintptr)(unsafe.Add(p, 296)) = __ccgo_fp(_unixAccess) *(*uintptr)(unsafe.Add(p, 300)) = __ccgo_fp(_unixFullPathname) *(*uintptr)(unsafe.Add(p, 304)) = __ccgo_fp(_unixDlOpen) *(*uintptr)(unsafe.Add(p, 308)) = __ccgo_fp(_unixDlError) *(*uintptr)(unsafe.Add(p, 312)) = __ccgo_fp(_unixDlSym) *(*uintptr)(unsafe.Add(p, 316)) = __ccgo_fp(_unixDlClose) *(*uintptr)(unsafe.Add(p, 320)) = __ccgo_fp(_unixRandomness) *(*uintptr)(unsafe.Add(p, 324)) = __ccgo_fp(_unixSleep) *(*uintptr)(unsafe.Add(p, 328)) = __ccgo_fp(_unixCurrentTime) *(*uintptr)(unsafe.Add(p, 332)) = __ccgo_fp(_unixGetLastError) *(*uintptr)(unsafe.Add(p, 336)) = __ccgo_fp(_unixCurrentTimeInt64) *(*uintptr)(unsafe.Add(p, 340)) = __ccgo_fp(_unixSetSystemCall) *(*uintptr)(unsafe.Add(p, 344)) = __ccgo_fp(_unixGetSystemCall) *(*uintptr)(unsafe.Add(p, 348)) = __ccgo_fp(_unixNextSystemCall) } // C documentation // // /* // ** Shutdown the operating system interface. // ** // ** Some operating systems might need to do some cleanup in this routine, // ** to release dynamically allocated objects. But not on unix. // ** This routine is a no-op for unix. // */ func Xsqlite3_os_end(tls *libc.TLS) (r int32) { _unixBigLock = uintptr(0) return SQLITE_OK } /************** End of os_unix.c *********************************************/ /************** Begin file os_win.c ******************************************/ /* ** 2004 May 22 ** ** 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 contains code that is specific to Windows. */ /* #include "sqliteInt.h" */ /************** End of os_win.c **********************************************/ /************** Begin file memdb.c *******************************************/ /* ** 2016-09-07 ** ** 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 implements an in-memory VFS. A database is held as a contiguous ** block of memory. ** ** This file also implements interface sqlite3_serialize() and ** sqlite3_deserialize(). */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** Forward declaration of objects used by this utility // */ type TMemVfs = struct { FiVersion int32 FszOsFile int32 FmxPathname int32 FpNext uintptr FzName uintptr FpAppData uintptr FxOpen uintptr FxDelete uintptr FxAccess uintptr FxFullPathname uintptr FxDlOpen uintptr FxDlError uintptr FxDlSym uintptr FxDlClose uintptr FxRandomness uintptr FxSleep uintptr FxCurrentTime uintptr FxGetLastError uintptr FxCurrentTimeInt64 uintptr FxSetSystemCall uintptr FxGetSystemCall uintptr FxNextSystemCall uintptr } type MemVfs = TMemVfs type TMemFile = struct { Fbase Tsqlite3_file FpStore uintptr FeLock int32 } type MemFile = TMemFile type TMemStore = struct { F__ccgo_align [0]uint32 Fsz Tsqlite3_int64 FszAlloc Tsqlite3_int64 FszMax Tsqlite3_int64 FaData uintptr FpMutex uintptr FnMmap int32 FmFlags uint32 FnRdLock int32 FnWrLock int32 FnRef int32 FzFName uintptr } type MemStore = TMemStore /* Access to a lower-level VFS that (might) implement dynamic loading, ** access to randomness, etc. */ /* Storage for a memdb file. ** ** An memdb object can be shared or separate. Shared memdb objects can be ** used by more than one database connection. Mutexes are used by shared ** memdb objects to coordinate access. Separate memdb objects are only ** connected to a single database connection and do not require additional ** mutexes. ** ** Shared memdb objects have .zFName!=0 and .pMutex!=0. They are created ** using "file:/name?vfs=memdb". The first character of the name must be ** "/" or else the object will be a separate memdb object. All shared ** memdb objects are stored in memdb_g.apMemStore[] in an arbitrary order. ** ** Separate memdb objects are created using a name that does not begin ** with "/" or using sqlite3_deserialize(). ** ** Access rules for shared MemStore objects: ** ** * .zFName is initialized when the object is created and afterwards ** is unchanged until the object is destroyed. So it can be accessed ** at any time as long as we know the object is not being destroyed, ** which means while either the SQLITE_MUTEX_STATIC_VFS1 or ** .pMutex is held or the object is not part of memdb_g.apMemStore[]. ** ** * Can .pMutex can only be changed while holding the ** SQLITE_MUTEX_STATIC_VFS1 mutex or while the object is not part ** of memdb_g.apMemStore[]. ** ** * Other fields can only be changed while holding the .pMutex mutex ** or when the .nRef is less than zero and the object is not part of ** memdb_g.apMemStore[]. ** ** * The .aData pointer has the added requirement that it can can only ** be changed (for resizing) when nMmap is zero. ** */ type TMemStore1 = struct { F__ccgo_align [0]uint32 Fsz Tsqlite3_int64 FszAlloc Tsqlite3_int64 FszMax Tsqlite3_int64 FaData uintptr FpMutex uintptr FnMmap int32 FmFlags uint32 FnRdLock int32 FnWrLock int32 FnRef int32 FzFName uintptr } type MemStore1 = TMemStore1 /* An open file */ type TMemFile1 = struct { Fbase Tsqlite3_file FpStore uintptr FeLock int32 } type MemFile1 = TMemFile1 // C documentation // // /* // ** File-scope variables for holding the memdb files that are accessible // ** to multiple database connections in separate threads. // ** // ** Must hold SQLITE_MUTEX_STATIC_VFS1 to access any part of this object. // */ type TMemFS = struct { FnMemStore int32 FapMemStore uintptr } type MemFS = TMemFS // C documentation // // /* // ** File-scope variables for holding the memdb files that are accessible // ** to multiple database connections in separate threads. // ** // ** Must hold SQLITE_MUTEX_STATIC_VFS1 to access any part of this object. // */ var _memdb_g TMemFS var _memdb_vfs = Tsqlite3_vfs{ FiVersion: int32(2), FmxPathname: int32(1024), FzName: __ccgo_ts + 3967, } func init() { p := unsafe.Pointer(&_memdb_vfs) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_memdbOpen) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_memdbAccess) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_memdbFullPathname) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_memdbDlOpen) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_memdbDlError) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_memdbDlSym) *(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(_memdbDlClose) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_memdbRandomness) *(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_memdbSleep) *(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_memdbGetLastError) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_memdbCurrentTimeInt64) } var _memdb_io_methods = Tsqlite3_io_methods{ FiVersion: int32(3), } func init() { p := unsafe.Pointer(&_memdb_io_methods) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_memdbClose) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_memdbRead) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_memdbWrite) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_memdbTruncate) *(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_memdbSync) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_memdbFileSize) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_memdbLock) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_memdbUnlock) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_memdbFileControl) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_memdbDeviceCharacteristics) *(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_memdbFetch) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_memdbUnfetch) } // C documentation // // /* // ** Enter/leave the mutex on a MemStore // */ func _memdbEnter(tls *libc.TLS, p uintptr) { Xsqlite3_mutex_enter(tls, (*TMemStore)(unsafe.Pointer(p)).FpMutex) } func _memdbLeave(tls *libc.TLS, p uintptr) { Xsqlite3_mutex_leave(tls, (*TMemStore)(unsafe.Pointer(p)).FpMutex) } // C documentation // // /* // ** Close an memdb-file. // ** Free the underlying MemStore object when its refcount drops to zero // ** or less. // */ func _memdbClose(tls *libc.TLS, pFile uintptr) (r int32) { var i, v2 int32 var p, pVfsMutex, v3 uintptr _, _, _, _, _ = i, p, pVfsMutex, v2, v3 p = (*TMemFile)(unsafe.Pointer(pFile)).FpStore if (*TMemStore)(unsafe.Pointer(p)).FzFName != 0 { pVfsMutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1)) Xsqlite3_mutex_enter(tls, pVfsMutex) i = 0 for { if !(i < _memdb_g.FnMemStore) { break } if *(*uintptr)(unsafe.Pointer(_memdb_g.FapMemStore + uintptr(i)*4)) == p { _memdbEnter(tls, p) if (*TMemStore)(unsafe.Pointer(p)).FnRef == int32(1) { v3 = uintptr(unsafe.Pointer(&_memdb_g)) *(*int32)(unsafe.Pointer(v3))-- v2 = *(*int32)(unsafe.Pointer(v3)) *(*uintptr)(unsafe.Pointer(_memdb_g.FapMemStore + uintptr(i)*4)) = *(*uintptr)(unsafe.Pointer(_memdb_g.FapMemStore + uintptr(v2)*4)) if _memdb_g.FnMemStore == 0 { Xsqlite3_free(tls, _memdb_g.FapMemStore) _memdb_g.FapMemStore = uintptr(0) } } break } goto _1 _1: ; i++ } Xsqlite3_mutex_leave(tls, pVfsMutex) } else { _memdbEnter(tls, p) } (*TMemStore)(unsafe.Pointer(p)).FnRef-- if (*TMemStore)(unsafe.Pointer(p)).FnRef <= 0 { if (*TMemStore)(unsafe.Pointer(p)).FmFlags&uint32(SQLITE_DESERIALIZE_FREEONCLOSE) != 0 { Xsqlite3_free(tls, (*TMemStore)(unsafe.Pointer(p)).FaData) } _memdbLeave(tls, p) Xsqlite3_mutex_free(tls, (*TMemStore)(unsafe.Pointer(p)).FpMutex) Xsqlite3_free(tls, p) } else { _memdbLeave(tls, p) } return SQLITE_OK } // C documentation // // /* // ** Read data from an memdb-file. // */ func _memdbRead(tls *libc.TLS, pFile uintptr, zBuf uintptr, iAmt int32, iOfst Tsqlite_int64) (r int32) { var p uintptr _ = p p = (*TMemFile)(unsafe.Pointer(pFile)).FpStore _memdbEnter(tls, p) if iOfst+int64(iAmt) > (*TMemStore)(unsafe.Pointer(p)).Fsz { libc.Xmemset(tls, zBuf, 0, uint32(iAmt)) if iOfst < (*TMemStore)(unsafe.Pointer(p)).Fsz { libc.Xmemcpy(tls, zBuf, (*TMemStore)(unsafe.Pointer(p)).FaData+uintptr(iOfst), uint32((*TMemStore)(unsafe.Pointer(p)).Fsz-iOfst)) } _memdbLeave(tls, p) return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(2)< 0 { return int32(SQLITE_FULL) } if newSz > (*TMemStore)(unsafe.Pointer(p)).FszMax { return int32(SQLITE_FULL) } newSz *= int64(2) if newSz > (*TMemStore)(unsafe.Pointer(p)).FszMax { newSz = (*TMemStore)(unsafe.Pointer(p)).FszMax } pNew = _sqlite3Realloc(tls, (*TMemStore)(unsafe.Pointer(p)).FaData, uint64(newSz)) if pNew == uintptr(0) { return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)< (*TMemStore)(unsafe.Pointer(p)).Fsz { if v2 = iOfst+int64(iAmt) > (*TMemStore)(unsafe.Pointer(p)).FszAlloc; v2 { v1 = _memdbEnlarge(tls, p, iOfst+int64(iAmt)) rc = v1 } if v2 && v1 != SQLITE_OK { _memdbLeave(tls, p) return rc } if iOfst > (*TMemStore)(unsafe.Pointer(p)).Fsz { libc.Xmemset(tls, (*TMemStore)(unsafe.Pointer(p)).FaData+uintptr((*TMemStore)(unsafe.Pointer(p)).Fsz), 0, uint32(iOfst-(*TMemStore)(unsafe.Pointer(p)).Fsz)) } (*TMemStore)(unsafe.Pointer(p)).Fsz = iOfst + int64(iAmt) } libc.Xmemcpy(tls, (*TMemStore)(unsafe.Pointer(p)).FaData+uintptr(iOfst), z, uint32(iAmt)) _memdbLeave(tls, p) return SQLITE_OK } // C documentation // // /* // ** Truncate an memdb-file. // ** // ** In rollback mode (which is always the case for memdb, as it does not // ** support WAL mode) the truncate() method is only used to reduce // ** the size of a file, never to increase the size. // */ func _memdbTruncate(tls *libc.TLS, pFile uintptr, size Tsqlite_int64) (r int32) { var p uintptr var rc int32 _, _ = p, rc p = (*TMemFile)(unsafe.Pointer(pFile)).FpStore rc = SQLITE_OK _memdbEnter(tls, p) if size > (*TMemStore)(unsafe.Pointer(p)).Fsz { /* This can only happen with a corrupt wal mode db */ rc = int32(SQLITE_CORRUPT) } else { (*TMemStore)(unsafe.Pointer(p)).Fsz = size } _memdbLeave(tls, p) return rc } // C documentation // // /* // ** Sync an memdb-file. // */ func _memdbSync(tls *libc.TLS, pFile uintptr, flags int32) (r int32) { _ = pFile _ = flags return SQLITE_OK } // C documentation // // /* // ** Return the current file-size of an memdb-file. // */ func _memdbFileSize(tls *libc.TLS, pFile uintptr, pSize uintptr) (r int32) { var p uintptr _ = p p = (*TMemFile)(unsafe.Pointer(pFile)).FpStore _memdbEnter(tls, p) *(*Tsqlite_int64)(unsafe.Pointer(pSize)) = (*TMemStore)(unsafe.Pointer(p)).Fsz _memdbLeave(tls, p) return SQLITE_OK } // C documentation // // /* // ** Lock an memdb-file. // */ func _memdbLock(tls *libc.TLS, pFile uintptr, eLock int32) (r int32) { var p, pThis uintptr var rc int32 _, _, _ = p, pThis, rc pThis = pFile p = (*TMemFile)(unsafe.Pointer(pThis)).FpStore rc = SQLITE_OK if eLock <= (*TMemFile)(unsafe.Pointer(pThis)).FeLock { return SQLITE_OK } _memdbEnter(tls, p) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if eLock > int32(SQLITE_LOCK_SHARED) && (*TMemStore)(unsafe.Pointer(p)).FmFlags&uint32(SQLITE_DESERIALIZE_READONLY) != 0 { rc = int32(SQLITE_READONLY) } else { switch eLock { case int32(SQLITE_LOCK_SHARED): _ = libc.Int32FromInt32(0) if (*TMemStore)(unsafe.Pointer(p)).FnWrLock > 0 { rc = int32(SQLITE_BUSY) } else { (*TMemStore)(unsafe.Pointer(p)).FnRdLock++ } case int32(SQLITE_LOCK_RESERVED): fallthrough case int32(SQLITE_LOCK_PENDING): _ = libc.Int32FromInt32(0) if (*TMemFile)(unsafe.Pointer(pThis)).FeLock == int32(SQLITE_LOCK_SHARED) { if (*TMemStore)(unsafe.Pointer(p)).FnWrLock > 0 { rc = int32(SQLITE_BUSY) } else { (*TMemStore)(unsafe.Pointer(p)).FnWrLock = int32(1) } } default: _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TMemStore)(unsafe.Pointer(p)).FnRdLock > int32(1) { rc = int32(SQLITE_BUSY) } else { if (*TMemFile)(unsafe.Pointer(pThis)).FeLock == int32(SQLITE_LOCK_SHARED) { (*TMemStore)(unsafe.Pointer(p)).FnWrLock = int32(1) } } break } } if rc == SQLITE_OK { (*TMemFile)(unsafe.Pointer(pThis)).FeLock = eLock } _memdbLeave(tls, p) return rc } // C documentation // // /* // ** Unlock an memdb-file. // */ func _memdbUnlock(tls *libc.TLS, pFile uintptr, eLock int32) (r int32) { var p, pThis uintptr _, _ = p, pThis pThis = pFile p = (*TMemFile)(unsafe.Pointer(pThis)).FpStore if eLock >= (*TMemFile)(unsafe.Pointer(pThis)).FeLock { return SQLITE_OK } _memdbEnter(tls, p) _ = libc.Int32FromInt32(0) if eLock == int32(SQLITE_LOCK_SHARED) { if (*TMemFile)(unsafe.Pointer(pThis)).FeLock > int32(SQLITE_LOCK_SHARED) { (*TMemStore)(unsafe.Pointer(p)).FnWrLock-- } } else { if (*TMemFile)(unsafe.Pointer(pThis)).FeLock > int32(SQLITE_LOCK_SHARED) { (*TMemStore)(unsafe.Pointer(p)).FnWrLock-- } (*TMemStore)(unsafe.Pointer(p)).FnRdLock-- } (*TMemFile)(unsafe.Pointer(pThis)).FeLock = eLock _memdbLeave(tls, p) return SQLITE_OK } // C documentation // // /* // ** File control method. For custom operations on an memdb-file. // */ func _memdbFileControl(tls *libc.TLS, pFile uintptr, op int32, pArg uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var iLimit Tsqlite3_int64 var p uintptr var rc int32 _, _, _ = iLimit, p, rc p = (*TMemFile)(unsafe.Pointer(pFile)).FpStore rc = int32(SQLITE_NOTFOUND) _memdbEnter(tls, p) if op == int32(SQLITE_FCNTL_VFSNAME) { *(*uintptr)(unsafe.Pointer(pArg)) = Xsqlite3_mprintf(tls, __ccgo_ts+3973, libc.VaList(bp+8, (*TMemStore)(unsafe.Pointer(p)).FaData, (*TMemStore)(unsafe.Pointer(p)).Fsz)) rc = SQLITE_OK } if op == int32(SQLITE_FCNTL_SIZE_LIMIT) { iLimit = *(*Tsqlite3_int64)(unsafe.Pointer(pArg)) if iLimit < (*TMemStore)(unsafe.Pointer(p)).Fsz { if iLimit < 0 { iLimit = (*TMemStore)(unsafe.Pointer(p)).FszMax } else { iLimit = (*TMemStore)(unsafe.Pointer(p)).Fsz } } (*TMemStore)(unsafe.Pointer(p)).FszMax = iLimit *(*Tsqlite3_int64)(unsafe.Pointer(pArg)) = iLimit rc = SQLITE_OK } _memdbLeave(tls, p) return rc } // C documentation // // /* // ** Return the device characteristic flags supported by an memdb-file. // */ func _memdbDeviceCharacteristics(tls *libc.TLS, pFile uintptr) (r int32) { _ = pFile return libc.Int32FromInt32(SQLITE_IOCAP_ATOMIC) | libc.Int32FromInt32(SQLITE_IOCAP_POWERSAFE_OVERWRITE) | libc.Int32FromInt32(SQLITE_IOCAP_SAFE_APPEND) | libc.Int32FromInt32(SQLITE_IOCAP_SEQUENTIAL) } // C documentation // // /* Fetch a page of a memory-mapped file */ func _memdbFetch(tls *libc.TLS, pFile uintptr, iOfst Tsqlite3_int64, iAmt int32, pp uintptr) (r int32) { var p uintptr _ = p p = (*TMemFile)(unsafe.Pointer(pFile)).FpStore _memdbEnter(tls, p) if iOfst+int64(iAmt) > (*TMemStore)(unsafe.Pointer(p)).Fsz || (*TMemStore)(unsafe.Pointer(p)).FmFlags&uint32(SQLITE_DESERIALIZE_RESIZEABLE) != uint32(0) { *(*uintptr)(unsafe.Pointer(pp)) = uintptr(0) } else { (*TMemStore)(unsafe.Pointer(p)).FnMmap++ *(*uintptr)(unsafe.Pointer(pp)) = (*TMemStore)(unsafe.Pointer(p)).FaData + uintptr(iOfst) } _memdbLeave(tls, p) return SQLITE_OK } // C documentation // // /* Release a memory-mapped page */ func _memdbUnfetch(tls *libc.TLS, pFile uintptr, iOfst Tsqlite3_int64, pPage uintptr) (r int32) { var p uintptr _ = p p = (*TMemFile)(unsafe.Pointer(pFile)).FpStore _ = iOfst _ = pPage _memdbEnter(tls, p) (*TMemStore)(unsafe.Pointer(p)).FnMmap-- _memdbLeave(tls, p) return SQLITE_OK } // C documentation // // /* // ** Open an mem file handle. // */ func _memdbOpen(tls *libc.TLS, pVfs uintptr, zName uintptr, pFd uintptr, flags int32, pOutFlags uintptr) (r int32) { var apNew, p, pFile, pVfsMutex, v3 uintptr var i, szName, v2 int32 _, _, _, _, _, _, _, _ = apNew, i, p, pFile, pVfsMutex, szName, v2, v3 pFile = pFd p = uintptr(0) _ = pVfs libc.Xmemset(tls, pFile, 0, uint32(12)) szName = _sqlite3Strlen30(tls, zName) if szName > int32(1) && (int32(*(*uint8)(unsafe.Pointer(zName))) == int32('/') || int32(*(*uint8)(unsafe.Pointer(zName))) == int32('\\')) { pVfsMutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1)) Xsqlite3_mutex_enter(tls, pVfsMutex) i = 0 for { if !(i < _memdb_g.FnMemStore) { break } if libc.Xstrcmp(tls, (*TMemStore)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(_memdb_g.FapMemStore + uintptr(i)*4)))).FzFName, zName) == 0 { p = *(*uintptr)(unsafe.Pointer(_memdb_g.FapMemStore + uintptr(i)*4)) break } goto _1 _1: ; i++ } if p == uintptr(0) { p = _sqlite3Malloc(tls, uint64(uint32(56)+uint32(szName)+uint32(3))) if p == uintptr(0) { Xsqlite3_mutex_leave(tls, pVfsMutex) return int32(SQLITE_NOMEM) } apNew = _sqlite3Realloc(tls, _memdb_g.FapMemStore, uint64(uint32(4)*uint32(_memdb_g.FnMemStore+libc.Int32FromInt32(1)))) if apNew == uintptr(0) { Xsqlite3_free(tls, p) Xsqlite3_mutex_leave(tls, pVfsMutex) return int32(SQLITE_NOMEM) } v3 = uintptr(unsafe.Pointer(&_memdb_g)) v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ *(*uintptr)(unsafe.Pointer(apNew + uintptr(v2)*4)) = p _memdb_g.FapMemStore = apNew libc.Xmemset(tls, p, 0, uint32(56)) (*TMemStore)(unsafe.Pointer(p)).FmFlags = uint32(libc.Int32FromInt32(SQLITE_DESERIALIZE_RESIZEABLE) | libc.Int32FromInt32(SQLITE_DESERIALIZE_FREEONCLOSE)) (*TMemStore)(unsafe.Pointer(p)).FszMax = _sqlite3Config.FmxMemdbSize (*TMemStore)(unsafe.Pointer(p)).FzFName = p + 1*56 libc.Xmemcpy(tls, (*TMemStore)(unsafe.Pointer(p)).FzFName, zName, uint32(szName+int32(1))) (*TMemStore)(unsafe.Pointer(p)).FpMutex = Xsqlite3_mutex_alloc(tls, SQLITE_MUTEX_FAST) if (*TMemStore)(unsafe.Pointer(p)).FpMutex == uintptr(0) { _memdb_g.FnMemStore-- Xsqlite3_free(tls, p) Xsqlite3_mutex_leave(tls, pVfsMutex) return int32(SQLITE_NOMEM) } (*TMemStore)(unsafe.Pointer(p)).FnRef = int32(1) _memdbEnter(tls, p) } else { _memdbEnter(tls, p) (*TMemStore)(unsafe.Pointer(p)).FnRef++ } Xsqlite3_mutex_leave(tls, pVfsMutex) } else { p = _sqlite3Malloc(tls, uint64(56)) if p == uintptr(0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, p, 0, uint32(56)) (*TMemStore)(unsafe.Pointer(p)).FmFlags = uint32(libc.Int32FromInt32(SQLITE_DESERIALIZE_RESIZEABLE) | libc.Int32FromInt32(SQLITE_DESERIALIZE_FREEONCLOSE)) (*TMemStore)(unsafe.Pointer(p)).FszMax = _sqlite3Config.FmxMemdbSize } (*TMemFile)(unsafe.Pointer(pFile)).FpStore = p if pOutFlags != uintptr(0) { *(*int32)(unsafe.Pointer(pOutFlags)) = flags | int32(SQLITE_OPEN_MEMORY) } (*Tsqlite3_file)(unsafe.Pointer(pFd)).FpMethods = uintptr(unsafe.Pointer(&_memdb_io_methods)) _memdbLeave(tls, p) return SQLITE_OK } // C documentation // // /* // ** Test for access permissions. Return true if the requested permission // ** is available, or false otherwise. // ** // ** With memdb, no files ever exist on disk. So always return false. // */ func _memdbAccess(tls *libc.TLS, pVfs uintptr, zPath uintptr, flags int32, pResOut uintptr) (r int32) { _ = pVfs _ = zPath _ = flags *(*int32)(unsafe.Pointer(pResOut)) = 0 return SQLITE_OK } // C documentation // // /* // ** Populate buffer zOut with the full canonical pathname corresponding // ** to the pathname in zPath. zOut is guaranteed to point to a buffer // ** of at least (INST_MAX_PATHNAME+1) bytes. // */ func _memdbFullPathname(tls *libc.TLS, pVfs uintptr, zPath uintptr, nOut int32, zOut uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) _ = pVfs Xsqlite3_snprintf(tls, nOut, zOut, __ccgo_ts+3795, libc.VaList(bp+8, zPath)) return SQLITE_OK } // C documentation // // /* // ** Open the dynamic library located at zPath and return a handle. // */ func _memdbDlOpen(tls *libc.TLS, pVfs uintptr, zPath uintptr) (r uintptr) { return (*(*func(*libc.TLS, uintptr, uintptr) uintptr)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData)).FxDlOpen})))(tls, (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData, zPath) } // C documentation // // /* // ** Populate the buffer zErrMsg (size nByte bytes) with a human readable // ** utf-8 string describing the most recent error encountered associated // ** with dynamic libraries. // */ func _memdbDlError(tls *libc.TLS, pVfs uintptr, nByte int32, zErrMsg uintptr) { (*(*func(*libc.TLS, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData)).FxDlError})))(tls, (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData, nByte, zErrMsg) } // C documentation // // /* // ** Return a pointer to the symbol zSymbol in the dynamic library pHandle. // */ func _memdbDlSym(tls *libc.TLS, pVfs uintptr, p uintptr, zSym uintptr) (r uintptr) { return (*(*func(*libc.TLS, uintptr, uintptr, uintptr) uintptr)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData)).FxDlSym})))(tls, (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData, p, zSym) } // C documentation // // /* // ** Close the dynamic library handle pHandle. // */ func _memdbDlClose(tls *libc.TLS, pVfs uintptr, pHandle uintptr) { (*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData)).FxDlClose})))(tls, (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData, pHandle) } // C documentation // // /* // ** Populate the buffer pointed to by zBufOut with nByte bytes of // ** random data. // */ func _memdbRandomness(tls *libc.TLS, pVfs uintptr, nByte int32, zBufOut uintptr) (r int32) { return (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData)).FxRandomness})))(tls, (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData, nByte, zBufOut) } // C documentation // // /* // ** Sleep for nMicro microseconds. Return the number of microseconds // ** actually slept. // */ func _memdbSleep(tls *libc.TLS, pVfs uintptr, nMicro int32) (r int32) { return (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData)).FxSleep})))(tls, (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData, nMicro) } func _memdbGetLastError(tls *libc.TLS, pVfs uintptr, a int32, b uintptr) (r int32) { return (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData)).FxGetLastError})))(tls, (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData, a, b) } func _memdbCurrentTimeInt64(tls *libc.TLS, pVfs uintptr, p uintptr) (r int32) { return (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData)).FxCurrentTimeInt64})))(tls, (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData, p) } // C documentation // // /* // ** Translate a database connection pointer and schema name into a // ** MemFile pointer. // */ func _memdbFromDbSchema(tls *libc.TLS, db uintptr, zSchema uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var pStore uintptr var rc int32 var _ /* p at bp+0 */ uintptr _, _ = pStore, rc *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) rc = Xsqlite3_file_control(tls, db, zSchema, int32(SQLITE_FCNTL_FILE_POINTER), bp) if rc != 0 { return uintptr(0) } if (*TMemFile)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fbase.FpMethods != uintptr(unsafe.Pointer(&_memdb_io_methods)) { return uintptr(0) } pStore = (*TMemFile)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpStore _memdbEnter(tls, pStore) if (*TMemStore)(unsafe.Pointer(pStore)).FzFName != uintptr(0) { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } _memdbLeave(tls, pStore) return *(*uintptr)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Return the serialization of a database // */ func Xsqlite3_serialize(tls *libc.TLS, db uintptr, zSchema uintptr, piSize uintptr, mFlags uint32) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var iDb, nPage, pgno, rc, szPage, v1 int32 var p, pBt, pOut, pPager, pStore, pTo, zSql uintptr var sz Tsqlite3_int64 var _ /* pPage at bp+4 */ uintptr var _ /* pStmt at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _ = iDb, nPage, p, pBt, pOut, pPager, pStore, pTo, pgno, rc, sz, szPage, zSql, v1 szPage = 0 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) if zSchema == uintptr(0) { zSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FzDbSName } p = _memdbFromDbSchema(tls, db, zSchema) iDb = _sqlite3FindDbName(tls, db, zSchema) if piSize != 0 { *(*Tsqlite3_int64)(unsafe.Pointer(piSize)) = int64(-int32(1)) } if iDb < 0 { return uintptr(0) } if p != 0 { pStore = (*TMemFile)(unsafe.Pointer(p)).FpStore _ = libc.Int32FromInt32(0) if piSize != 0 { *(*Tsqlite3_int64)(unsafe.Pointer(piSize)) = (*TMemStore)(unsafe.Pointer(pStore)).Fsz } if mFlags&uint32(SQLITE_SERIALIZE_NOCOPY) != 0 { pOut = (*TMemStore)(unsafe.Pointer(pStore)).FaData } else { pOut = Xsqlite3_malloc64(tls, uint64((*TMemStore)(unsafe.Pointer(pStore)).Fsz)) if pOut != 0 { libc.Xmemcpy(tls, pOut, (*TMemStore)(unsafe.Pointer(pStore)).FaData, uint32((*TMemStore)(unsafe.Pointer(pStore)).Fsz)) } } return pOut } pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt if pBt == uintptr(0) { return uintptr(0) } szPage = _sqlite3BtreeGetPageSize(tls, pBt) zSql = Xsqlite3_mprintf(tls, __ccgo_ts+3988, libc.VaList(bp+16, zSchema)) if zSql != 0 { v1 = Xsqlite3_prepare_v2(tls, db, zSql, -int32(1), bp, uintptr(0)) } else { v1 = int32(SQLITE_NOMEM) } rc = v1 Xsqlite3_free(tls, zSql) if rc != 0 { return uintptr(0) } rc = Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) if rc != int32(SQLITE_ROW) { pOut = uintptr(0) } else { sz = Xsqlite3_column_int64(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) * int64(szPage) if sz == 0 { Xsqlite3_reset(tls, *(*uintptr)(unsafe.Pointer(bp))) Xsqlite3_exec(tls, db, __ccgo_ts+4011, uintptr(0), uintptr(0), uintptr(0)) rc = Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) if rc == int32(SQLITE_ROW) { sz = Xsqlite3_column_int64(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) * int64(szPage) } } if piSize != 0 { *(*Tsqlite3_int64)(unsafe.Pointer(piSize)) = sz } if mFlags&uint32(SQLITE_SERIALIZE_NOCOPY) != 0 { pOut = uintptr(0) } else { pOut = Xsqlite3_malloc64(tls, uint64(sz)) if pOut != 0 { nPage = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) pPager = _sqlite3BtreePager(tls, pBt) pgno = int32(1) for { if !(pgno <= nPage) { break } *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) pTo = pOut + uintptr(int64(szPage)*int64(pgno-libc.Int32FromInt32(1))) rc = _sqlite3PagerGet(tls, pPager, uint32(pgno), bp+4, 0) if rc == SQLITE_OK { libc.Xmemcpy(tls, pTo, _sqlite3PagerGetData(tls, *(*uintptr)(unsafe.Pointer(bp + 4))), uint32(szPage)) } else { libc.Xmemset(tls, pTo, 0, uint32(szPage)) } _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) goto _2 _2: ; pgno++ } } } } Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) return pOut } // C documentation // // /* Convert zSchema to a MemDB and initialize its content. // */ func Xsqlite3_deserialize(tls *libc.TLS, db uintptr, zSchema uintptr, pData uintptr, szDb Tsqlite3_int64, szBuf Tsqlite3_int64, mFlags uint32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var iDb, rc int32 var p, pStore, zSql uintptr var _ /* pStmt at bp+0 */ uintptr _, _, _, _, _ = iDb, p, pStore, rc, zSql *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if zSchema == uintptr(0) { zSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FzDbSName } iDb = _sqlite3FindDbName(tls, db, zSchema) if iDb < int32(2) && iDb != 0 { rc = int32(SQLITE_ERROR) goto end_deserialize } zSql = Xsqlite3_mprintf(tls, __ccgo_ts+4036, libc.VaList(bp+16, zSchema)) if zSql == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { rc = Xsqlite3_prepare_v2(tls, db, zSql, -int32(1), bp, uintptr(0)) Xsqlite3_free(tls, zSql) } if rc != 0 { goto end_deserialize } (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(iDb) libc.SetBitFieldPtr8Uint32(db+172+8, libc.Uint32FromInt32(1), 2, 0x4) rc = Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) libc.SetBitFieldPtr8Uint32(db+172+8, libc.Uint32FromInt32(0), 2, 0x4) if rc != int32(SQLITE_DONE) { rc = int32(SQLITE_ERROR) goto end_deserialize } p = _memdbFromDbSchema(tls, db, zSchema) if p == uintptr(0) { rc = int32(SQLITE_ERROR) } else { pStore = (*TMemFile)(unsafe.Pointer(p)).FpStore (*TMemStore)(unsafe.Pointer(pStore)).FaData = pData pData = uintptr(0) (*TMemStore)(unsafe.Pointer(pStore)).Fsz = szDb (*TMemStore)(unsafe.Pointer(pStore)).FszAlloc = szBuf (*TMemStore)(unsafe.Pointer(pStore)).FszMax = szBuf if (*TMemStore)(unsafe.Pointer(pStore)).FszMax < _sqlite3Config.FmxMemdbSize { (*TMemStore)(unsafe.Pointer(pStore)).FszMax = _sqlite3Config.FmxMemdbSize } (*TMemStore)(unsafe.Pointer(pStore)).FmFlags = mFlags rc = SQLITE_OK } goto end_deserialize end_deserialize: ; Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) if pData != 0 && mFlags&uint32(SQLITE_DESERIALIZE_FREEONCLOSE) != uint32(0) { Xsqlite3_free(tls, pData) } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** Return true if the VFS is the memvfs. // */ func _sqlite3IsMemdb(tls *libc.TLS, pVfs uintptr) (r int32) { return libc.BoolInt32(pVfs == uintptr(unsafe.Pointer(&_memdb_vfs))) } // C documentation // // /* // ** This routine is called when the extension is loaded. // ** Register the new VFS. // */ func _sqlite3MemdbInit(tls *libc.TLS) (r int32) { var pLower uintptr var sz uint32 _, _ = pLower, sz pLower = Xsqlite3_vfs_find(tls, uintptr(0)) if pLower == uintptr(0) { return int32(SQLITE_ERROR) } sz = uint32((*Tsqlite3_vfs)(unsafe.Pointer(pLower)).FszOsFile) _memdb_vfs.FpAppData = pLower /* The following conditional can only be true when compiled for ** Windows x86 and SQLITE_MAX_MMAP_SIZE=0. We always leave ** it in, to be safe, but it is marked as NO_TEST since there ** is no way to reach it under most builds. */ if sz < uint32(12) { sz = uint32(12) } /*NO_TEST*/ _memdb_vfs.FszOsFile = int32(sz) return Xsqlite3_vfs_register(tls, uintptr(unsafe.Pointer(&_memdb_vfs)), 0) } /************** End of memdb.c ***********************************************/ /************** Begin file bitvec.c ******************************************/ /* ** 2008 February 16 ** ** 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 implements an object that represents a fixed-length ** bitmap. Bits are numbered starting with 1. ** ** A bitmap is used to record which pages of a database file have been ** journalled during a transaction, or which pages have the "dont-write" ** property. Usually only a few pages are meet either condition. ** So the bitmap is usually sparse and has low cardinality. ** But sometimes (for example when during a DROP of a large table) most ** or all of the pages in a database can get journalled. In those cases, ** the bitmap becomes dense with high cardinality. The algorithm needs ** to handle both cases well. ** ** The size of the bitmap is fixed when the object is created. ** ** All bits are clear when the bitmap is created. Individual bits ** may be set or cleared one at a time. ** ** Test operations are about 100 times more common that set operations. ** Clear operations are exceedingly rare. There are usually between ** 5 and 500 set operations per Bitvec object, though the number of sets can ** sometimes grow into tens of thousands or larger. The size of the ** Bitvec object is the number of pages in the database file at the ** start of a transaction, and is thus usually less than a few thousand, ** but can be as large as 2 billion for a really big database. */ /* #include "sqliteInt.h" */ /* Size of the Bitvec structure in bytes. */ /* Round the union size down to the nearest pointer boundary, since that's how ** it will be aligned within the Bitvec struct. */ /* Type of the array "element" for the bitmap representation. ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. ** Setting this to the "natural word" size of your CPU may improve ** performance. */ /* Size, in bits, of the bitmap element. */ /* Number of elements in a bitmap array. */ /* Number of bits in the bitmap array. */ /* Number of u32 values in hash table. */ /* Maximum number of entries in hash table before ** sub-dividing and re-hashing. */ /* Hashing function for the aHash representation. ** Empirical testing showed that the *37 multiplier ** (an arbitrary prime)in the hash function provided ** no fewer collisions than the no-op *1. */ /* ** A bitmap is an instance of the following structure. ** ** This bitmap records the existence of zero or more bits ** with values between 1 and iSize, inclusive. ** ** There are three possible representations of the bitmap. ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight ** bitmap. The least significant bit is bit 1. ** ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is ** a hash table that will hold up to BITVEC_MXHASH distinct values. ** ** Otherwise, the value i is redirected into one of BITVEC_NPTR ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap ** handles up to iDivisor separate values of i. apSub[0] holds ** values between 1 and iDivisor. apSub[1] holds values between ** iDivisor+1 and 2*iDivisor. apSub[N] holds values between ** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized ** to hold deal with values between 1 and iDivisor. */ type TBitvec1 = struct { FiSize Tu32 FnSet Tu32 FiDivisor Tu32 Fu struct { FaHash [0][125]Tu32 FapSub [0][125]uintptr FaBitmap [500]Tu8 } } type Bitvec1 = TBitvec1 // C documentation // // /* // ** Create a new bitmap object able to handle bits between 0 and iSize, // ** inclusive. Return a pointer to the new object. Return NULL if // ** malloc fails. // */ func _sqlite3BitvecCreate(tls *libc.TLS, iSize Tu32) (r uintptr) { var p uintptr _ = p _ = libc.Int32FromInt32(0) p = _sqlite3MallocZero(tls, uint64(512)) if p != 0 { (*TBitvec)(unsafe.Pointer(p)).FiSize = iSize } return p } // C documentation // // /* // ** Check to see if the i-th bit is set. Return true or false. // ** If p is NULL (if the bitmap has not been created) or if // ** i is out of range, then return false. // */ func _sqlite3BitvecTestNotNull(tls *libc.TLS, p uintptr, i Tu32) (r int32) { var bin, h, v1 Tu32 _, _, _ = bin, h, v1 _ = libc.Int32FromInt32(0) i-- if i >= (*TBitvec)(unsafe.Pointer(p)).FiSize { return 0 } for (*TBitvec)(unsafe.Pointer(p)).FiDivisor != 0 { bin = i / (*TBitvec)(unsafe.Pointer(p)).FiDivisor i = i % (*TBitvec)(unsafe.Pointer(p)).FiDivisor p = *(*uintptr)(unsafe.Pointer(p + 12 + uintptr(bin)*4)) if !(p != 0) { return 0 } } if (*TBitvec)(unsafe.Pointer(p)).FiSize <= (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(1)*libc.Uint32FromInt32(BITVEC_SZELEM) { return libc.BoolInt32(int32(*(*Tu8)(unsafe.Pointer(p + 12 + uintptr(i/uint32(BITVEC_SZELEM)))))&(int32(1)<<(i&uint32(libc.Int32FromInt32(BITVEC_SZELEM)-libc.Int32FromInt32(1)))) != 0) } else { v1 = i i++ h = v1 * libc.Uint32FromInt32(1) % ((libc.Uint32FromInt32(BITVEC_SZ) - libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4)) / libc.Uint32FromInt64(4) * libc.Uint32FromInt64(4) / libc.Uint32FromInt64(4)) for *(*Tu32)(unsafe.Pointer(p + 12 + uintptr(h)*4)) != 0 { if *(*Tu32)(unsafe.Pointer(p + 12 + uintptr(h)*4)) == i { return int32(1) } h = (h + uint32(1)) % ((libc.Uint32FromInt32(BITVEC_SZ) - libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4)) / libc.Uint32FromInt64(4) * libc.Uint32FromInt64(4) / libc.Uint32FromInt64(4)) } return 0 } return r } func _sqlite3BitvecTest(tls *libc.TLS, p uintptr, i Tu32) (r int32) { return libc.BoolInt32(p != uintptr(0) && _sqlite3BitvecTestNotNull(tls, p, i) != 0) } // C documentation // // /* // ** Set the i-th bit. Return 0 on success and an error code if // ** anything goes wrong. // ** // ** This routine might cause sub-bitmaps to be allocated. Failing // ** to get the memory needed to hold the sub-bitmap is the only // ** that can go wrong with an insert, assuming p and i are valid. // ** // ** The calling function must ensure that p is a valid Bitvec object // ** and that the value for "i" is within range of the Bitvec object. // ** Otherwise the behavior is undefined. // */ func _sqlite3BitvecSet(tls *libc.TLS, p uintptr, i Tu32) (r int32) { var aiValues, p1 uintptr var bin, h, v2 Tu32 var j uint32 var rc int32 _, _, _, _, _, _, _ = aiValues, bin, h, j, rc, v2, p1 if p == uintptr(0) { return SQLITE_OK } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) i-- for (*TBitvec)(unsafe.Pointer(p)).FiSize > (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(1)*libc.Uint32FromInt32(BITVEC_SZELEM) && (*TBitvec)(unsafe.Pointer(p)).FiDivisor != 0 { bin = i / (*TBitvec)(unsafe.Pointer(p)).FiDivisor i = i % (*TBitvec)(unsafe.Pointer(p)).FiDivisor if *(*uintptr)(unsafe.Pointer(p + 12 + uintptr(bin)*4)) == uintptr(0) { *(*uintptr)(unsafe.Pointer(p + 12 + uintptr(bin)*4)) = _sqlite3BitvecCreate(tls, (*TBitvec)(unsafe.Pointer(p)).FiDivisor) if *(*uintptr)(unsafe.Pointer(p + 12 + uintptr(bin)*4)) == uintptr(0) { return int32(SQLITE_NOMEM) } } p = *(*uintptr)(unsafe.Pointer(p + 12 + uintptr(bin)*4)) } if (*TBitvec)(unsafe.Pointer(p)).FiSize <= (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(1)*libc.Uint32FromInt32(BITVEC_SZELEM) { p1 = p + 12 + uintptr(i/uint32(BITVEC_SZELEM)) *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | libc.Int32FromInt32(1)<<(i&uint32(libc.Int32FromInt32(BITVEC_SZELEM)-libc.Int32FromInt32(1)))) return SQLITE_OK } v2 = i i++ h = v2 * libc.Uint32FromInt32(1) % ((libc.Uint32FromInt32(BITVEC_SZ) - libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4)) / libc.Uint32FromInt64(4) * libc.Uint32FromInt64(4) / libc.Uint32FromInt64(4)) /* if there wasn't a hash collision, and this doesn't */ /* completely fill the hash, then just add it without */ /* worrying about sub-dividing and re-hashing. */ if !(*(*Tu32)(unsafe.Pointer(p + 12 + uintptr(h)*4)) != 0) { if (*TBitvec)(unsafe.Pointer(p)).FnSet < (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4)-libc.Uint32FromInt32(1) { goto bitvec_set_end } else { goto bitvec_set_rehash } } /* there was a collision, check to see if it's already */ /* in hash, if not, try to find a spot for it */ for cond := true; cond; cond = *(*Tu32)(unsafe.Pointer(p + 12 + uintptr(h)*4)) != 0 { if *(*Tu32)(unsafe.Pointer(p + 12 + uintptr(h)*4)) == i { return SQLITE_OK } h++ if h >= (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4) { h = uint32(0) } } /* we didn't find it in the hash. h points to the first */ /* available free spot. check to see if this is going to */ /* make our hash too "full". */ goto bitvec_set_rehash bitvec_set_rehash: ; if (*TBitvec)(unsafe.Pointer(p)).FnSet >= (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4)/libc.Uint32FromInt32(2) { aiValues = _sqlite3DbMallocRaw(tls, uintptr(0), uint64(500)) if aiValues == uintptr(0) { return int32(SQLITE_NOMEM) } else { libc.Xmemcpy(tls, aiValues, p+12, uint32(500)) libc.Xmemset(tls, p+12, 0, uint32(500)) (*TBitvec)(unsafe.Pointer(p)).FiDivisor = ((*TBitvec)(unsafe.Pointer(p)).FiSize + (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4) - uint32(1)) / ((libc.Uint32FromInt32(BITVEC_SZ) - libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4)) / libc.Uint32FromInt64(4) * libc.Uint32FromInt64(4) / libc.Uint32FromInt64(4)) rc = _sqlite3BitvecSet(tls, p, i) j = uint32(0) for { if !(j < (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4)) { break } if *(*Tu32)(unsafe.Pointer(aiValues + uintptr(j)*4)) != 0 { rc |= _sqlite3BitvecSet(tls, p, *(*Tu32)(unsafe.Pointer(aiValues + uintptr(j)*4))) } goto _3 _3: ; j++ } _sqlite3DbFree(tls, uintptr(0), aiValues) return rc } } goto bitvec_set_end bitvec_set_end: ; (*TBitvec)(unsafe.Pointer(p)).FnSet++ *(*Tu32)(unsafe.Pointer(p + 12 + uintptr(h)*4)) = i return SQLITE_OK } // C documentation // // /* // ** Clear the i-th bit. // ** // ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage // ** that BitvecClear can use to rebuilt its hash table. // */ func _sqlite3BitvecClear(tls *libc.TLS, p uintptr, i Tu32, pBuf uintptr) { var aiValues, p1 uintptr var bin, h Tu32 var j uint32 _, _, _, _, _ = aiValues, bin, h, j, p1 if p == uintptr(0) { return } _ = libc.Int32FromInt32(0) i-- for (*TBitvec)(unsafe.Pointer(p)).FiDivisor != 0 { bin = i / (*TBitvec)(unsafe.Pointer(p)).FiDivisor i = i % (*TBitvec)(unsafe.Pointer(p)).FiDivisor p = *(*uintptr)(unsafe.Pointer(p + 12 + uintptr(bin)*4)) if !(p != 0) { return } } if (*TBitvec)(unsafe.Pointer(p)).FiSize <= (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(1)*libc.Uint32FromInt32(BITVEC_SZELEM) { p1 = p + 12 + uintptr(i/uint32(BITVEC_SZELEM)) *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) & ^(libc.Int32FromInt32(1) << (i & uint32(libc.Int32FromInt32(BITVEC_SZELEM)-libc.Int32FromInt32(1))))) } else { aiValues = pBuf libc.Xmemcpy(tls, aiValues, p+12, uint32(500)) libc.Xmemset(tls, p+12, 0, uint32(500)) (*TBitvec)(unsafe.Pointer(p)).FnSet = uint32(0) j = uint32(0) for { if !(j < (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4)) { break } if *(*Tu32)(unsafe.Pointer(aiValues + uintptr(j)*4)) != 0 && *(*Tu32)(unsafe.Pointer(aiValues + uintptr(j)*4)) != i+uint32(1) { h = (*(*Tu32)(unsafe.Pointer(aiValues + uintptr(j)*4)) - libc.Uint32FromInt32(1)) * libc.Uint32FromInt32(1) % ((libc.Uint32FromInt32(BITVEC_SZ) - libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4)) / libc.Uint32FromInt64(4) * libc.Uint32FromInt64(4) / libc.Uint32FromInt64(4)) (*TBitvec)(unsafe.Pointer(p)).FnSet++ for *(*Tu32)(unsafe.Pointer(p + 12 + uintptr(h)*4)) != 0 { h++ if h >= (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4) { h = uint32(0) } } *(*Tu32)(unsafe.Pointer(p + 12 + uintptr(h)*4)) = *(*Tu32)(unsafe.Pointer(aiValues + uintptr(j)*4)) } goto _2 _2: ; j++ } } } // C documentation // // /* // ** Destroy a bitmap object. Reclaim all memory used. // */ func _sqlite3BitvecDestroy(tls *libc.TLS, p uintptr) { var i uint32 _ = i if p == uintptr(0) { return } if (*TBitvec)(unsafe.Pointer(p)).FiDivisor != 0 { i = uint32(0) for { if !(i < (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4)) { break } _sqlite3BitvecDestroy(tls, *(*uintptr)(unsafe.Pointer(p + 12 + uintptr(i)*4))) goto _1 _1: ; i++ } } Xsqlite3_free(tls, p) } // C documentation // // /* // ** Return the value of the iSize parameter specified when Bitvec *p // ** was created. // */ func _sqlite3BitvecSize(tls *libc.TLS, p uintptr) (r Tu32) { return (*TBitvec)(unsafe.Pointer(p)).FiSize } /* ** Let V[] be an array of unsigned characters sufficient to hold ** up to N bits. Let I be an integer between 0 and N. 0<=I 0 { nx = 0 } pc += nx *(*int32)(unsafe.Pointer(bp)) = *(*int32)(unsafe.Pointer(bp)) & int32(0x7fffffff) % sz if op&int32(1) != 0 { p5 = pV + uintptr((*(*int32)(unsafe.Pointer(bp))+int32(1))>>int32(3)) *(*uint8)(unsafe.Pointer(p5)) = uint8(int32(*(*uint8)(unsafe.Pointer(p5))) | libc.Int32FromInt32(1)<<((*(*int32)(unsafe.Pointer(bp))+libc.Int32FromInt32(1))&libc.Int32FromInt32(7))) if op != int32(5) { if _sqlite3BitvecSet(tls, pBitvec, uint32(*(*int32)(unsafe.Pointer(bp))+int32(1))) != 0 { goto bitvec_end } } } else { p6 = pV + uintptr((*(*int32)(unsafe.Pointer(bp))+int32(1))>>int32(3)) *(*uint8)(unsafe.Pointer(p6)) = uint8(int32(*(*uint8)(unsafe.Pointer(p6))) & ^(libc.Int32FromInt32(1) << ((*(*int32)(unsafe.Pointer(bp)) + libc.Int32FromInt32(1)) & libc.Int32FromInt32(7)))) _sqlite3BitvecClear(tls, pBitvec, uint32(*(*int32)(unsafe.Pointer(bp))+int32(1)), pTmpSpace) } } /* Test to make sure the linear array exactly matches the ** Bitvec object. Start with the assumption that they do ** match (rc==0). Change rc to non-zero if a discrepancy ** is found. */ rc = int32(uint32(_sqlite3BitvecTest(tls, uintptr(0), uint32(0))+_sqlite3BitvecTest(tls, pBitvec, uint32(sz+int32(1)))+_sqlite3BitvecTest(tls, pBitvec, uint32(0))) + (_sqlite3BitvecSize(tls, pBitvec) - uint32(sz))) *(*int32)(unsafe.Pointer(bp)) = int32(1) for { if !(*(*int32)(unsafe.Pointer(bp)) <= sz) { break } if libc.BoolInt32(int32(*(*uint8)(unsafe.Pointer(pV + uintptr(*(*int32)(unsafe.Pointer(bp))>>int32(3)))))&(int32(1)<<(*(*int32)(unsafe.Pointer(bp))&int32(7))) != 0) != _sqlite3BitvecTest(tls, pBitvec, uint32(*(*int32)(unsafe.Pointer(bp)))) { rc = *(*int32)(unsafe.Pointer(bp)) break } goto _7 _7: ; *(*int32)(unsafe.Pointer(bp))++ } /* Free allocated structure */ goto bitvec_end bitvec_end: ; Xsqlite3_free(tls, pTmpSpace) Xsqlite3_free(tls, pV) _sqlite3BitvecDestroy(tls, pBitvec) return rc } /************** End of bitvec.c **********************************************/ /************** Begin file pcache.c ******************************************/ /* ** 2008 August 05 ** ** 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 implements that page cache. */ /* #include "sqliteInt.h" */ /* ** A complete page cache is an instance of this structure. Every ** entry in the cache holds a single page of the database file. The ** btree layer only operates on the cached copy of the database pages. ** ** A page cache entry is "clean" if it exactly matches what is currently ** on disk. A page is "dirty" if it has been modified and needs to be ** persisted to disk. ** ** pDirty, pDirtyTail, pSynced: ** All dirty pages are linked into the doubly linked list using ** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order ** such that p was added to the list more recently than p->pDirtyNext. ** PCache.pDirty points to the first (newest) element in the list and ** pDirtyTail to the last (oldest). ** ** The PCache.pSynced variable is used to optimize searching for a dirty ** page to eject from the cache mid-transaction. It is better to eject ** a page that does not require a journal sync than one that does. ** Therefore, pSynced is maintained so that it *almost* always points ** to either the oldest page in the pDirty/pDirtyTail list that has a ** clear PGHDR_NEED_SYNC flag or to a page that is older than this one ** (so that the right page to eject can be found by following pDirtyPrev ** pointers). */ type TPCache2 = struct { F__ccgo_align [0]uint32 FpDirty uintptr FpDirtyTail uintptr FpSynced uintptr F__ccgo_align3 [4]byte FnRefSum Ti64 FszCache int32 FszSpill int32 FszPage int32 FszExtra int32 FbPurgeable Tu8 FeCreate Tu8 FxStress uintptr FpStress uintptr FpCache uintptr } type PCache2 = TPCache2 /********************************** Test and Debug Logic **********************/ /* ** Debug tracing macros. Enable by by changing the "0" to "1" and ** recompiling. ** ** When sqlite3PcacheTrace is 1, single line trace messages are issued. ** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries ** is displayed for many operations, resulting in a lot of output. */ /* ** Return 1 if pPg is on the dirty list for pCache. Return 0 if not. ** This routine runs inside of assert() statements only. */ /* ** Check invariants on a PgHdr entry. Return true if everything is OK. ** Return false if any invariant is violated. ** ** This routine is for use inside of assert() statements only. For ** example: ** ** assert( sqlite3PcachePageSanity(pPg) ); */ /********************************** Linked List Management ********************/ /* Allowed values for second argument to pcacheManageDirtyList() */ // C documentation // // /* // ** Manage pPage's participation on the dirty list. Bits of the addRemove // ** argument determines what operation to do. The 0x01 bit means first // ** remove pPage from the dirty list. The 0x02 means add pPage back to // ** the dirty list. Doing both moves pPage to the front of the dirty list. // */ func _pcacheManageDirtyList(tls *libc.TLS, pPage uintptr, addRemove Tu8) { var p uintptr _ = p p = (*TPgHdr)(unsafe.Pointer(pPage)).FpCache if int32(addRemove)&int32(PCACHE_DIRTYLIST_REMOVE) != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Update the PCache1.pSynced variable if necessary. */ if (*TPCache)(unsafe.Pointer(p)).FpSynced == pPage { (*TPCache)(unsafe.Pointer(p)).FpSynced = (*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyPrev } if (*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyNext != 0 { (*TPgHdr)(unsafe.Pointer((*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyNext)).FpDirtyPrev = (*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyPrev } else { _ = libc.Int32FromInt32(0) (*TPCache)(unsafe.Pointer(p)).FpDirtyTail = (*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyPrev } if (*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyPrev != 0 { (*TPgHdr)(unsafe.Pointer((*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyPrev)).FpDirtyNext = (*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyNext } else { /* If there are now no dirty pages in the cache, set eCreate to 2. ** This is an optimization that allows sqlite3PcacheFetch() to skip ** searching for a dirty page to eject from the cache when it might ** otherwise have to. */ _ = libc.Int32FromInt32(0) (*TPCache)(unsafe.Pointer(p)).FpDirty = (*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyNext _ = libc.Int32FromInt32(0) if (*TPCache)(unsafe.Pointer(p)).FpDirty == uintptr(0) { /*OPTIMIZATION-IF-TRUE*/ _ = libc.Int32FromInt32(0) (*TPCache)(unsafe.Pointer(p)).FeCreate = uint8(2) } } } if int32(addRemove)&int32(PCACHE_DIRTYLIST_ADD) != 0 { (*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyPrev = uintptr(0) (*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyNext = (*TPCache)(unsafe.Pointer(p)).FpDirty if (*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyNext != 0 { _ = libc.Int32FromInt32(0) (*TPgHdr)(unsafe.Pointer((*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyNext)).FpDirtyPrev = pPage } else { (*TPCache)(unsafe.Pointer(p)).FpDirtyTail = pPage if (*TPCache)(unsafe.Pointer(p)).FbPurgeable != 0 { _ = libc.Int32FromInt32(0) (*TPCache)(unsafe.Pointer(p)).FeCreate = uint8(1) } } (*TPCache)(unsafe.Pointer(p)).FpDirty = pPage /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set ** pSynced to point to it. Checking the NEED_SYNC flag is an ** optimization, as if pSynced points to a page with the NEED_SYNC ** flag set sqlite3PcacheFetchStress() searches through all newer ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */ if !((*TPCache)(unsafe.Pointer(p)).FpSynced != 0) && 0 == int32((*TPgHdr)(unsafe.Pointer(pPage)).Fflags)&int32(PGHDR_NEED_SYNC) { (*TPCache)(unsafe.Pointer(p)).FpSynced = pPage } } } // C documentation // // /* // ** Wrapper around the pluggable caches xUnpin method. If the cache is // ** being used for an in-memory database, this function is a no-op. // */ func _pcacheUnpin(tls *libc.TLS, p uintptr) { if (*TPCache)(unsafe.Pointer((*TPgHdr)(unsafe.Pointer(p)).FpCache)).FbPurgeable != 0 { (*(*func(*libc.TLS, uintptr, uintptr, int32))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxUnpin})))(tls, (*TPCache)(unsafe.Pointer((*TPgHdr)(unsafe.Pointer(p)).FpCache)).FpCache, (*TPgHdr)(unsafe.Pointer(p)).FpPage, 0) } } // C documentation // // /* // ** Compute the number of pages of cache requested. p->szCache is the // ** cache size requested by the "PRAGMA cache_size" statement. // */ func _numberOfCachePages(tls *libc.TLS, p uintptr) (r int32) { var n Ti64 _ = n if (*TPCache)(unsafe.Pointer(p)).FszCache >= 0 { /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the ** suggested cache size is set to N. */ return (*TPCache)(unsafe.Pointer(p)).FszCache } else { /* IMPLEMENTATION-OF: R-59858-46238 If the argument N is negative, then the ** number of cache pages is adjusted to be a number of pages that would ** use approximately abs(N*1024) bytes of memory based on the current ** page size. */ n = int64(-libc.Int32FromInt32(1024)) * int64((*TPCache)(unsafe.Pointer(p)).FszCache) / int64((*TPCache)(unsafe.Pointer(p)).FszPage+(*TPCache)(unsafe.Pointer(p)).FszExtra) if n > int64(1000000000) { n = int64(1000000000) } return int32(n) } return r } // C documentation // // /*************************************************** General Interfaces ****** // ** // ** Initialize and shutdown the page cache subsystem. Neither of these // ** functions are threadsafe. // */ func _sqlite3PcacheInitialize(tls *libc.TLS) (r int32) { if _sqlite3Config.Fpcache2.FxInit == uintptr(0) { /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the ** built-in default page cache is used instead of the application defined ** page cache. */ _sqlite3PCacheSetDefault(tls) _ = libc.Int32FromInt32(0) } return (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxInit})))(tls, _sqlite3Config.Fpcache2.FpArg) } func _sqlite3PcacheShutdown(tls *libc.TLS) { if _sqlite3Config.Fpcache2.FxShutdown != 0 { /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */ (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxShutdown})))(tls, _sqlite3Config.Fpcache2.FpArg) } } // C documentation // // /* // ** Return the size in bytes of a PCache object. // */ func _sqlite3PcacheSize(tls *libc.TLS) (r int32) { return int32(56) } // C documentation // // /* // ** Create a new PCache object. Storage space to hold the object // ** has already been allocated and is passed in as the p pointer. // ** The caller discovers how much space needs to be allocated by // ** calling sqlite3PcacheSize(). // ** // ** szExtra is some extra space allocated for each page. The first // ** 8 bytes of the extra space will be zeroed as the page is allocated, // ** but remaining content will be uninitialized. Though it is opaque // ** to this module, the extra space really ends up being the MemPage // ** structure in the pager. // */ func _sqlite3PcacheOpen(tls *libc.TLS, szPage int32, szExtra int32, bPurgeable int32, xStress uintptr, pStress uintptr, p uintptr) (r int32) { libc.Xmemset(tls, p, 0, uint32(56)) (*TPCache)(unsafe.Pointer(p)).FszPage = int32(1) (*TPCache)(unsafe.Pointer(p)).FszExtra = szExtra _ = libc.Int32FromInt32(0) /* First 8 bytes will be zeroed */ (*TPCache)(unsafe.Pointer(p)).FbPurgeable = uint8(bPurgeable) (*TPCache)(unsafe.Pointer(p)).FeCreate = uint8(2) (*TPCache)(unsafe.Pointer(p)).FxStress = xStress (*TPCache)(unsafe.Pointer(p)).FpStress = pStress (*TPCache)(unsafe.Pointer(p)).FszCache = int32(100) (*TPCache)(unsafe.Pointer(p)).FszSpill = int32(1) return _sqlite3PcacheSetPageSize(tls, p, szPage) } // C documentation // // /* // ** Change the page size for PCache object. The caller must ensure that there // ** are no outstanding page references when this function is called. // */ func _sqlite3PcacheSetPageSize(tls *libc.TLS, pCache uintptr, szPage int32) (r int32) { var pNew uintptr _ = pNew _ = libc.Int32FromInt32(0) if (*TPCache)(unsafe.Pointer(pCache)).FszPage != 0 { pNew = (*(*func(*libc.TLS, int32, int32, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxCreate})))(tls, szPage, int32(uint32((*TPCache)(unsafe.Pointer(pCache)).FszExtra)+(libc.Uint32FromInt64(48)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))), int32((*TPCache)(unsafe.Pointer(pCache)).FbPurgeable)) if pNew == uintptr(0) { return int32(SQLITE_NOMEM) } (*(*func(*libc.TLS, uintptr, int32))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxCachesize})))(tls, pNew, _numberOfCachePages(tls, pCache)) if (*TPCache)(unsafe.Pointer(pCache)).FpCache != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxDestroy})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpCache) } (*TPCache)(unsafe.Pointer(pCache)).FpCache = pNew (*TPCache)(unsafe.Pointer(pCache)).FszPage = szPage } return SQLITE_OK } // C documentation // // /* // ** Try to obtain a page from the cache. // ** // ** This routine returns a pointer to an sqlite3_pcache_page object if // ** such an object is already in cache, or if a new one is created. // ** This routine returns a NULL pointer if the object was not in cache // ** and could not be created. // ** // ** The createFlags should be 0 to check for existing pages and should // ** be 3 (not 1, but 3) to try to create a new page. // ** // ** If the createFlag is 0, then NULL is always returned if the page // ** is not already in the cache. If createFlag is 1, then a new page // ** is created only if that can be done without spilling dirty pages // ** and without exceeding the cache size limit. // ** // ** The caller needs to invoke sqlite3PcacheFetchFinish() to properly // ** initialize the sqlite3_pcache_page object and convert it into a // ** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish() // ** routines are split this way for performance reasons. When separated // ** they can both (usually) operate without having to push values to // ** the stack on entry and pop them back off on exit, which saves a // ** lot of pushing and popping. // */ func _sqlite3PcacheFetch(tls *libc.TLS, pCache uintptr, pgno TPgno, createFlag int32) (r uintptr) { var eCreate int32 var pRes uintptr _, _ = eCreate, pRes _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* eCreate defines what to do if the page does not exist. ** 0 Do not allocate a new page. (createFlag==0) ** 1 Allocate a new page if doing so is inexpensive. ** (createFlag==1 AND bPurgeable AND pDirty) ** 2 Allocate a new page even it doing so is difficult. ** (createFlag==1 AND !(bPurgeable AND pDirty) */ eCreate = createFlag & int32((*TPCache)(unsafe.Pointer(pCache)).FeCreate) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pRes = (*(*func(*libc.TLS, uintptr, uint32, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxFetch})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpCache, pgno, eCreate) return pRes } // C documentation // // /* // ** If the sqlite3PcacheFetch() routine is unable to allocate a new // ** page because no clean pages are available for reuse and the cache // ** size limit has been reached, then this routine can be invoked to // ** try harder to allocate a page. This routine might invoke the stress // ** callback to spill dirty pages to the journal. It will then try to // ** allocate the new page and will only fail to allocate a new page on // ** an OOM error. // ** // ** This routine should be invoked only after sqlite3PcacheFetch() fails. // */ func _sqlite3PcacheFetchStress(tls *libc.TLS, pCache uintptr, pgno TPgno, ppPage uintptr) (r int32) { var pPg uintptr var rc, v3 int32 _, _, _ = pPg, rc, v3 if int32((*TPCache)(unsafe.Pointer(pCache)).FeCreate) == int32(2) { return 0 } if _sqlite3PcachePagecount(tls, pCache) > (*TPCache)(unsafe.Pointer(pCache)).FszSpill { /* Find a dirty page to write-out and recycle. First try to find a ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC ** cleared), but if that is not possible settle for any other ** unreferenced dirty page. ** ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC ** flag is currently referenced, then the following may leave pSynced ** set incorrectly (pointing to other than the LRU page with NEED_SYNC ** cleared). This is Ok, as pSynced is just an optimization. */ pPg = (*TPCache)(unsafe.Pointer(pCache)).FpSynced for { if !(pPg != 0 && ((*TPgHdr)(unsafe.Pointer(pPg)).FnRef != 0 || int32((*TPgHdr)(unsafe.Pointer(pPg)).Fflags)&int32(PGHDR_NEED_SYNC) != 0)) { break } goto _1 _1: ; pPg = (*TPgHdr)(unsafe.Pointer(pPg)).FpDirtyPrev } (*TPCache)(unsafe.Pointer(pCache)).FpSynced = pPg if !(pPg != 0) { pPg = (*TPCache)(unsafe.Pointer(pCache)).FpDirtyTail for { if !(pPg != 0 && (*TPgHdr)(unsafe.Pointer(pPg)).FnRef != 0) { break } goto _2 _2: ; pPg = (*TPgHdr)(unsafe.Pointer(pPg)).FpDirtyPrev } } if pPg != 0 { rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TPCache)(unsafe.Pointer(pCache)).FxStress})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpStress, pPg) if rc != SQLITE_OK && rc != int32(SQLITE_BUSY) { return rc } } } *(*uintptr)(unsafe.Pointer(ppPage)) = (*(*func(*libc.TLS, uintptr, uint32, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxFetch})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpCache, pgno, int32(2)) if *(*uintptr)(unsafe.Pointer(ppPage)) == uintptr(0) { v3 = int32(SQLITE_NOMEM) } else { v3 = SQLITE_OK } return v3 } // C documentation // // /* // ** This is a helper routine for sqlite3PcacheFetchFinish() // ** // ** In the uncommon case where the page being fetched has not been // ** initialized, this routine is invoked to do the initialization. // ** This routine is broken out into a separate function since it // ** requires extra stack manipulation that can be avoided in the common // ** case. // */ func _pcacheFetchFinishWithInit(tls *libc.TLS, pCache uintptr, pgno TPgno, pPage uintptr) (r uintptr) { var pPgHdr uintptr _ = pPgHdr _ = libc.Int32FromInt32(0) pPgHdr = (*Tsqlite3_pcache_page)(unsafe.Pointer(pPage)).FpExtra _ = libc.Int32FromInt32(0) libc.Xmemset(tls, pPgHdr+16, 0, libc.Uint32FromInt64(48)-uint32(libc.UintptrFromInt32(0)+16)) (*TPgHdr)(unsafe.Pointer(pPgHdr)).FpPage = pPage (*TPgHdr)(unsafe.Pointer(pPgHdr)).FpData = (*Tsqlite3_pcache_page)(unsafe.Pointer(pPage)).FpBuf (*TPgHdr)(unsafe.Pointer(pPgHdr)).FpExtra = pPgHdr + 1*48 libc.Xmemset(tls, (*TPgHdr)(unsafe.Pointer(pPgHdr)).FpExtra, 0, uint32(8)) (*TPgHdr)(unsafe.Pointer(pPgHdr)).FpCache = pCache (*TPgHdr)(unsafe.Pointer(pPgHdr)).Fpgno = pgno (*TPgHdr)(unsafe.Pointer(pPgHdr)).Fflags = uint16(PGHDR_CLEAN) return _sqlite3PcacheFetchFinish(tls, pCache, pgno, pPage) } // C documentation // // /* // ** This routine converts the sqlite3_pcache_page object returned by // ** sqlite3PcacheFetch() into an initialized PgHdr object. This routine // ** must be called after sqlite3PcacheFetch() in order to get a usable // ** result. // */ func _sqlite3PcacheFetchFinish(tls *libc.TLS, pCache uintptr, pgno TPgno, pPage uintptr) (r uintptr) { var pPgHdr uintptr _ = pPgHdr _ = libc.Int32FromInt32(0) pPgHdr = (*Tsqlite3_pcache_page)(unsafe.Pointer(pPage)).FpExtra if !((*TPgHdr)(unsafe.Pointer(pPgHdr)).FpPage != 0) { return _pcacheFetchFinishWithInit(tls, pCache, pgno, pPage) } (*TPCache)(unsafe.Pointer(pCache)).FnRefSum++ (*TPgHdr)(unsafe.Pointer(pPgHdr)).FnRef++ _ = libc.Int32FromInt32(0) return pPgHdr } // C documentation // // /* // ** Decrement the reference count on a page. If the page is clean and the // ** reference count drops to 0, then it is made eligible for recycling. // */ func _sqlite3PcacheRelease(tls *libc.TLS, p uintptr) { var v1 Ti64 var v2 uintptr _, _ = v1, v2 _ = libc.Int32FromInt32(0) (*TPCache)(unsafe.Pointer((*TPgHdr)(unsafe.Pointer(p)).FpCache)).FnRefSum-- v2 = p + 32 *(*Ti64)(unsafe.Pointer(v2))-- v1 = *(*Ti64)(unsafe.Pointer(v2)) if v1 == 0 { if int32((*TPgHdr)(unsafe.Pointer(p)).Fflags)&int32(PGHDR_CLEAN) != 0 { _pcacheUnpin(tls, p) } else { _pcacheManageDirtyList(tls, p, uint8(PCACHE_DIRTYLIST_FRONT)) _ = libc.Int32FromInt32(0) } } } // C documentation // // /* // ** Increase the reference count of a supplied page by 1. // */ func _sqlite3PcacheRef(tls *libc.TLS, p uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TPgHdr)(unsafe.Pointer(p)).FnRef++ (*TPCache)(unsafe.Pointer((*TPgHdr)(unsafe.Pointer(p)).FpCache)).FnRefSum++ } // C documentation // // /* // ** Drop a page from the cache. There must be exactly one reference to the // ** page. This function deletes that reference, so after it returns the // ** page pointed to by p is invalid. // */ func _sqlite3PcacheDrop(tls *libc.TLS, p uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TPgHdr)(unsafe.Pointer(p)).Fflags)&int32(PGHDR_DIRTY) != 0 { _pcacheManageDirtyList(tls, p, uint8(PCACHE_DIRTYLIST_REMOVE)) } (*TPCache)(unsafe.Pointer((*TPgHdr)(unsafe.Pointer(p)).FpCache)).FnRefSum-- (*(*func(*libc.TLS, uintptr, uintptr, int32))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxUnpin})))(tls, (*TPCache)(unsafe.Pointer((*TPgHdr)(unsafe.Pointer(p)).FpCache)).FpCache, (*TPgHdr)(unsafe.Pointer(p)).FpPage, int32(1)) } // C documentation // // /* // ** Make sure the page is marked as dirty. If it isn't dirty already, // ** make it so. // */ func _sqlite3PcacheMakeDirty(tls *libc.TLS, p uintptr) { var p1, p2 uintptr _, _ = p1, p2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TPgHdr)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(PGHDR_CLEAN)|libc.Int32FromInt32(PGHDR_DONT_WRITE)) != 0 { /*OPTIMIZATION-IF-FALSE*/ p1 = p + 28 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(PGHDR_DONT_WRITE)) if int32((*TPgHdr)(unsafe.Pointer(p)).Fflags)&int32(PGHDR_CLEAN) != 0 { p2 = p + 28 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) ^ (libc.Int32FromInt32(PGHDR_DIRTY) | libc.Int32FromInt32(PGHDR_CLEAN))) _ = libc.Int32FromInt32(0) _pcacheManageDirtyList(tls, p, uint8(PCACHE_DIRTYLIST_ADD)) _ = libc.Int32FromInt32(0) } _ = libc.Int32FromInt32(0) } } // C documentation // // /* // ** Make sure the page is marked as clean. If it isn't clean already, // ** make it so. // */ func _sqlite3PcacheMakeClean(tls *libc.TLS, p uintptr) { var p1, p2 uintptr _, _ = p1, p2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _pcacheManageDirtyList(tls, p, uint8(PCACHE_DIRTYLIST_REMOVE)) p1 = p + 28 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^(libc.Int32FromInt32(PGHDR_DIRTY) | libc.Int32FromInt32(PGHDR_NEED_SYNC) | libc.Int32FromInt32(PGHDR_WRITEABLE))) p2 = p + 28 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(PGHDR_CLEAN)) _ = libc.Int32FromInt32(0) if (*TPgHdr)(unsafe.Pointer(p)).FnRef == 0 { _pcacheUnpin(tls, p) } } // C documentation // // /* // ** Make every page in the cache clean. // */ func _sqlite3PcacheCleanAll(tls *libc.TLS, pCache uintptr) { var p, v1 uintptr _, _ = p, v1 for { v1 = (*TPCache)(unsafe.Pointer(pCache)).FpDirty p = v1 if !(v1 != uintptr(0)) { break } _sqlite3PcacheMakeClean(tls, p) } } // C documentation // // /* // ** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages. // */ func _sqlite3PcacheClearWritable(tls *libc.TLS, pCache uintptr) { var p, p2 uintptr _, _ = p, p2 p = (*TPCache)(unsafe.Pointer(pCache)).FpDirty for { if !(p != 0) { break } p2 = p + 28 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) & ^(libc.Int32FromInt32(PGHDR_NEED_SYNC) | libc.Int32FromInt32(PGHDR_WRITEABLE))) goto _1 _1: ; p = (*TPgHdr)(unsafe.Pointer(p)).FpDirtyNext } (*TPCache)(unsafe.Pointer(pCache)).FpSynced = (*TPCache)(unsafe.Pointer(pCache)).FpDirtyTail } // C documentation // // /* // ** Clear the PGHDR_NEED_SYNC flag from all dirty pages. // */ func _sqlite3PcacheClearSyncFlags(tls *libc.TLS, pCache uintptr) { var p, p2 uintptr _, _ = p, p2 p = (*TPCache)(unsafe.Pointer(pCache)).FpDirty for { if !(p != 0) { break } p2 = p + 28 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) & ^libc.Int32FromInt32(PGHDR_NEED_SYNC)) goto _1 _1: ; p = (*TPgHdr)(unsafe.Pointer(p)).FpDirtyNext } (*TPCache)(unsafe.Pointer(pCache)).FpSynced = (*TPCache)(unsafe.Pointer(pCache)).FpDirtyTail } // C documentation // // /* // ** Change the page number of page p to newPgno. // */ func _sqlite3PcacheMove(tls *libc.TLS, p uintptr, newPgno TPgno) { var pCache, pOther, pXPage uintptr _, _, _ = pCache, pOther, pXPage pCache = (*TPgHdr)(unsafe.Pointer(p)).FpCache _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pOther = (*(*func(*libc.TLS, uintptr, uint32, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxFetch})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpCache, newPgno, 0) if pOther != 0 { pXPage = (*Tsqlite3_pcache_page)(unsafe.Pointer(pOther)).FpExtra _ = libc.Int32FromInt32(0) (*TPgHdr)(unsafe.Pointer(pXPage)).FnRef++ (*TPCache)(unsafe.Pointer(pCache)).FnRefSum++ _sqlite3PcacheDrop(tls, pXPage) } (*(*func(*libc.TLS, uintptr, uintptr, uint32, uint32))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxRekey})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpCache, (*TPgHdr)(unsafe.Pointer(p)).FpPage, (*TPgHdr)(unsafe.Pointer(p)).Fpgno, newPgno) (*TPgHdr)(unsafe.Pointer(p)).Fpgno = newPgno if int32((*TPgHdr)(unsafe.Pointer(p)).Fflags)&int32(PGHDR_DIRTY) != 0 && int32((*TPgHdr)(unsafe.Pointer(p)).Fflags)&int32(PGHDR_NEED_SYNC) != 0 { _pcacheManageDirtyList(tls, p, uint8(PCACHE_DIRTYLIST_FRONT)) _ = libc.Int32FromInt32(0) } } // C documentation // // /* // ** Drop every cache entry whose page number is greater than "pgno". The // ** caller must ensure that there are no outstanding references to any pages // ** other than page 1 with a page number greater than pgno. // ** // ** If there is a reference to page 1 and the pgno parameter passed to this // ** function is 0, then the data area associated with page 1 is zeroed, but // ** the page object is not dropped. // */ func _sqlite3PcacheTruncate(tls *libc.TLS, pCache uintptr, pgno TPgno) { var p, pNext, pPage1 uintptr _, _, _ = p, pNext, pPage1 if (*TPCache)(unsafe.Pointer(pCache)).FpCache != 0 { p = (*TPCache)(unsafe.Pointer(pCache)).FpDirty for { if !(p != 0) { break } pNext = (*TPgHdr)(unsafe.Pointer(p)).FpDirtyNext /* This routine never gets call with a positive pgno except right ** after sqlite3PcacheCleanAll(). So if there are dirty pages, ** it must be that pgno==0. */ _ = libc.Int32FromInt32(0) if (*TPgHdr)(unsafe.Pointer(p)).Fpgno > pgno { _ = libc.Int32FromInt32(0) _sqlite3PcacheMakeClean(tls, p) } goto _1 _1: ; p = pNext } if pgno == uint32(0) && (*TPCache)(unsafe.Pointer(pCache)).FnRefSum != 0 { pPage1 = (*(*func(*libc.TLS, uintptr, uint32, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxFetch})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpCache, uint32(1), 0) if pPage1 != 0 { /* Page 1 is always available in cache, because ** pCache->nRefSum>0 */ libc.Xmemset(tls, (*Tsqlite3_pcache_page)(unsafe.Pointer(pPage1)).FpBuf, 0, uint32((*TPCache)(unsafe.Pointer(pCache)).FszPage)) pgno = uint32(1) } } (*(*func(*libc.TLS, uintptr, uint32))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxTruncate})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpCache, pgno+uint32(1)) } } // C documentation // // /* // ** Close a cache. // */ func _sqlite3PcacheClose(tls *libc.TLS, pCache uintptr) { _ = libc.Int32FromInt32(0) (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxDestroy})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpCache) } // C documentation // // /* // ** Discard the contents of the cache. // */ func _sqlite3PcacheClear(tls *libc.TLS, pCache uintptr) { _sqlite3PcacheTruncate(tls, pCache, uint32(0)) } // C documentation // // /* // ** Merge two lists of pages connected by pDirty and in pgno order. // ** Do not bother fixing the pDirtyPrev pointers. // */ func _pcacheMergeDirtyList(tls *libc.TLS, pA uintptr, pB uintptr) (r uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var pTail uintptr var _ /* result at bp+0 */ TPgHdr _ = pTail pTail = bp _ = libc.Int32FromInt32(0) for { if (*TPgHdr)(unsafe.Pointer(pA)).Fpgno < (*TPgHdr)(unsafe.Pointer(pB)).Fpgno { (*TPgHdr)(unsafe.Pointer(pTail)).FpDirty = pA pTail = pA pA = (*TPgHdr)(unsafe.Pointer(pA)).FpDirty if pA == uintptr(0) { (*TPgHdr)(unsafe.Pointer(pTail)).FpDirty = pB break } } else { (*TPgHdr)(unsafe.Pointer(pTail)).FpDirty = pB pTail = pB pB = (*TPgHdr)(unsafe.Pointer(pB)).FpDirty if pB == uintptr(0) { (*TPgHdr)(unsafe.Pointer(pTail)).FpDirty = pA break } } goto _1 _1: } return (*(*TPgHdr)(unsafe.Pointer(bp))).FpDirty } // C documentation // // /* // ** Sort the list of pages in ascending order by pgno. Pages are // ** connected by pDirty pointers. The pDirtyPrev pointers are // ** corrupted by this sort. // ** // ** Since there cannot be more than 2^31 distinct pages in a database, // ** there cannot be more than 31 buckets required by the merge sorter. // ** One extra bucket is added to catch overflow in case something // ** ever changes to make the previous sentence incorrect. // */ func _pcacheSortDirtyList(tls *libc.TLS, pIn uintptr) (r uintptr) { bp := tls.Alloc(128) defer tls.Free(128) var i int32 var p, v3 uintptr var _ /* a at bp+0 */ [32]uintptr _, _, _ = i, p, v3 libc.Xmemset(tls, bp, 0, uint32(128)) for pIn != 0 { p = pIn pIn = (*TPgHdr)(unsafe.Pointer(p)).FpDirty (*TPgHdr)(unsafe.Pointer(p)).FpDirty = uintptr(0) i = 0 for { if !(i < libc.Int32FromInt32(N_SORT_BUCKET)-libc.Int32FromInt32(1)) { break } if (*(*[32]uintptr)(unsafe.Pointer(bp)))[i] == uintptr(0) { (*(*[32]uintptr)(unsafe.Pointer(bp)))[i] = p break } else { p = _pcacheMergeDirtyList(tls, (*(*[32]uintptr)(unsafe.Pointer(bp)))[i], p) (*(*[32]uintptr)(unsafe.Pointer(bp)))[i] = uintptr(0) } goto _1 _1: ; i++ } if i == libc.Int32FromInt32(N_SORT_BUCKET)-libc.Int32FromInt32(1) { /* To get here, there need to be 2^(N_SORT_BUCKET) elements in ** the input list. But that is impossible. */ (*(*[32]uintptr)(unsafe.Pointer(bp)))[i] = _pcacheMergeDirtyList(tls, (*(*[32]uintptr)(unsafe.Pointer(bp)))[i], p) } } p = (*(*[32]uintptr)(unsafe.Pointer(bp)))[0] i = int32(1) for { if !(i < int32(N_SORT_BUCKET)) { break } if (*(*[32]uintptr)(unsafe.Pointer(bp)))[i] == uintptr(0) { goto _2 } if p != 0 { v3 = _pcacheMergeDirtyList(tls, p, (*(*[32]uintptr)(unsafe.Pointer(bp)))[i]) } else { v3 = (*(*[32]uintptr)(unsafe.Pointer(bp)))[i] } p = v3 goto _2 _2: ; i++ } return p } // C documentation // // /* // ** Return a list of all dirty pages in the cache, sorted by page number. // */ func _sqlite3PcacheDirtyList(tls *libc.TLS, pCache uintptr) (r uintptr) { var p uintptr _ = p p = (*TPCache)(unsafe.Pointer(pCache)).FpDirty for { if !(p != 0) { break } (*TPgHdr)(unsafe.Pointer(p)).FpDirty = (*TPgHdr)(unsafe.Pointer(p)).FpDirtyNext goto _1 _1: ; p = (*TPgHdr)(unsafe.Pointer(p)).FpDirtyNext } return _pcacheSortDirtyList(tls, (*TPCache)(unsafe.Pointer(pCache)).FpDirty) } // C documentation // // /* // ** Return the total number of references to all pages held by the cache. // ** // ** This is not the total number of pages referenced, but the sum of the // ** reference count for all pages. // */ func _sqlite3PcacheRefCount(tls *libc.TLS, pCache uintptr) (r Ti64) { return (*TPCache)(unsafe.Pointer(pCache)).FnRefSum } // C documentation // // /* // ** Return the number of references to the page supplied as an argument. // */ func _sqlite3PcachePageRefcount(tls *libc.TLS, p uintptr) (r Ti64) { return (*TPgHdr)(unsafe.Pointer(p)).FnRef } // C documentation // // /* // ** Return the total number of pages in the cache. // */ func _sqlite3PcachePagecount(tls *libc.TLS, pCache uintptr) (r int32) { _ = libc.Int32FromInt32(0) return (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxPagecount})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpCache) } // C documentation // // /* // ** Set the suggested cache-size value. // */ func _sqlite3PcacheSetCachesize(tls *libc.TLS, pCache uintptr, mxPage int32) { _ = libc.Int32FromInt32(0) (*TPCache)(unsafe.Pointer(pCache)).FszCache = mxPage (*(*func(*libc.TLS, uintptr, int32))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxCachesize})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpCache, _numberOfCachePages(tls, pCache)) } // C documentation // // /* // ** Set the suggested cache-spill value. Make no changes if if the // ** argument is zero. Return the effective cache-spill size, which will // ** be the larger of the szSpill and szCache. // */ func _sqlite3PcacheSetSpillsize(tls *libc.TLS, p uintptr, mxPage int32) (r int32) { var res int32 _ = res _ = libc.Int32FromInt32(0) if mxPage != 0 { if mxPage < 0 { mxPage = int32(int64(-libc.Int32FromInt32(1024)) * int64(mxPage) / int64((*TPCache)(unsafe.Pointer(p)).FszPage+(*TPCache)(unsafe.Pointer(p)).FszExtra)) } (*TPCache)(unsafe.Pointer(p)).FszSpill = mxPage } res = _numberOfCachePages(tls, p) if res < (*TPCache)(unsafe.Pointer(p)).FszSpill { res = (*TPCache)(unsafe.Pointer(p)).FszSpill } return res } // C documentation // // /* // ** Free up as much memory as possible from the page cache. // */ func _sqlite3PcacheShrink(tls *libc.TLS, pCache uintptr) { _ = libc.Int32FromInt32(0) (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxShrink})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpCache) } // C documentation // // /* // ** Return the size of the header added by this middleware layer // ** in the page-cache hierarchy. // */ func _sqlite3HeaderSizePcache(tls *libc.TLS) (r int32) { return int32((libc.Uint32FromInt64(48) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7))) } // C documentation // // /* // ** Return the number of dirty pages currently in the cache, as a percentage // ** of the configured cache size. // */ func _sqlite3PCachePercentDirty(tls *libc.TLS, pCache uintptr) (r int32) { var nCache, nDirty, v2 int32 var pDirty uintptr _, _, _, _ = nCache, nDirty, pDirty, v2 nDirty = 0 nCache = _numberOfCachePages(tls, pCache) pDirty = (*TPCache)(unsafe.Pointer(pCache)).FpDirty for { if !(pDirty != 0) { break } nDirty++ goto _1 _1: ; pDirty = (*TPgHdr)(unsafe.Pointer(pDirty)).FpDirtyNext } if nCache != 0 { v2 = int32(int64(nDirty) * libc.Int64FromInt32(100) / int64(nCache)) } else { v2 = 0 } return v2 } // C documentation // // /* // ** Return true if there are one or more dirty pages in the cache. Else false. // */ func _sqlite3PCacheIsDirty(tls *libc.TLS, pCache uintptr) (r int32) { return libc.BoolInt32((*TPCache)(unsafe.Pointer(pCache)).FpDirty != uintptr(0)) } /************** End of pcache.c **********************************************/ /************** Begin file pcache1.c *****************************************/ /* ** 2008 November 05 ** ** 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 implements the default page cache implementation (the ** sqlite3_pcache interface). It also contains part of the implementation ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features. ** If the default page cache implementation is overridden, then neither of ** these two features are available. ** ** A Page cache line looks like this: ** ** ------------------------------------------------------------- ** | database page content | PgHdr1 | MemPage | PgHdr | ** ------------------------------------------------------------- ** ** The database page content is up front (so that buffer overreads tend to ** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage ** is the extension added by the btree.c module containing information such ** as the database page number and how that database page is used. PgHdr ** is added by the pcache.c layer and contains information used to keep track ** of which pages are "dirty". PgHdr1 is an extension added by this ** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page. ** PgHdr1 contains information needed to look up a page by its page number. ** The superclass sqlite3_pcache_page.pBuf points to the start of the ** database page content and sqlite3_pcache_page.pExtra points to PgHdr. ** ** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at ** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The ** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this ** size can vary according to architecture, compile-time options, and ** SQLite library version number. ** ** Historical note: It used to be that if the SQLITE_PCACHE_SEPARATE_HEADER ** was defined, then the page content would be held in a separate memory ** allocation from the PgHdr1. This was intended to avoid clownshoe memory ** allocations. However, the btree layer needs a small (16-byte) overrun ** area after the page content buffer. The header serves as that overrun ** area. Therefore SQLITE_PCACHE_SEPARATE_HEADER was discontinued to avoid ** any possibility of a memory error. ** ** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates ** with this module. Information is passed back and forth as PgHdr1 pointers. ** ** The pcache.c and pager.c modules deal pointers to PgHdr objects. ** The btree.c module deals with pointers to MemPage objects. ** ** SOURCE OF PAGE CACHE MEMORY: ** ** Memory for a page might come from any of three sources: ** ** (1) The general-purpose memory allocator - sqlite3Malloc() ** (2) Global page-cache memory provided using sqlite3_config() with ** SQLITE_CONFIG_PAGECACHE. ** (3) PCache-local bulk allocation. ** ** The third case is a chunk of heap memory (defaulting to 100 pages worth) ** that is allocated when the page cache is created. The size of the local ** bulk allocation can be adjusted using ** ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N). ** ** If N is positive, then N pages worth of memory are allocated using a single ** sqlite3Malloc() call and that memory is used for the first N pages allocated. ** Or if N is negative, then -1024*N bytes of memory are allocated and used ** for as many pages as can be accommodated. ** ** Only one of (2) or (3) can be used. Once the memory available to (2) or ** (3) is exhausted, subsequent allocations fail over to the general-purpose ** memory allocator (1). ** ** Earlier versions of SQLite used only methods (1) and (2). But experiments ** show that method (3) with N==100 provides about a 5% performance boost for ** common workloads. */ /* #include "sqliteInt.h" */ type TPCache1 = struct { FpGroup uintptr FpnPurgeable uintptr FszPage int32 FszExtra int32 FszAlloc int32 FbPurgeable int32 FnMin uint32 FnMax uint32 Fn90pct uint32 FiMaxKey uint32 FnPurgeableDummy uint32 FnRecyclable uint32 FnPage uint32 FnHash uint32 FapHash uintptr FpFree uintptr FpBulk uintptr } type PCache1 = TPCache1 type TPgHdr1 = struct { Fpage Tsqlite3_pcache_page FiKey uint32 FisBulkLocal Tu16 FisAnchor Tu16 FpNext uintptr FpCache uintptr FpLruNext uintptr FpLruPrev uintptr } type PgHdr1 = TPgHdr1 type TPgFreeslot = struct { FpNext uintptr } type PgFreeslot = TPgFreeslot type TPGroup = struct { Fmutex uintptr FnMaxPage uint32 FnMinPage uint32 FmxPinned uint32 FnPurgeable uint32 Flru TPgHdr1 } type PGroup = TPGroup /* ** Each cache entry is represented by an instance of the following ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated ** directly before this structure and is used to cache the page content. ** ** When reading a corrupt database file, it is possible that SQLite might ** read a few bytes (no more than 16 bytes) past the end of the page buffer. ** It will only read past the end of the page buffer, never write. This ** object is positioned immediately after the page buffer to serve as an ** overrun area, so that overreads are harmless. ** ** Variables isBulkLocal and isAnchor were once type "u8". That works, ** but causes a 2-byte gap in the structure for most architectures (since ** pointers must be either 4 or 8-byte aligned). As this structure is located ** in memory directly after the associated page data, if the database is ** corrupt, code at the b-tree layer may overread the page buffer and ** read part of this structure before the corruption is detected. This ** can cause a valgrind error if the uninitialized gap is accessed. Using u16 ** ensures there is no such gap, and therefore no bytes of uninitialized ** memory in the structure. ** ** The pLruNext and pLruPrev pointers form a double-linked circular list ** of all pages that are unpinned. The PGroup.lru element (which should be ** the only element on the list with PgHdr1.isAnchor set to 1) forms the ** beginning and the end of the list. */ type TPgHdr11 = struct { Fpage Tsqlite3_pcache_page FiKey uint32 FisBulkLocal Tu16 FisAnchor Tu16 FpNext uintptr FpCache uintptr FpLruNext uintptr FpLruPrev uintptr } type PgHdr11 = TPgHdr11 /* ** A page is pinned if it is not on the LRU list. To be "pinned" means ** that the page is in active use and must not be deallocated. */ /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set ** of one or more PCaches that are able to recycle each other's unpinned ** pages when they are under memory pressure. A PGroup is an instance of ** the following object. ** ** This page cache implementation works in one of two modes: ** ** (1) Every PCache is the sole member of its own PGroup. There is ** one PGroup per PCache. ** ** (2) There is a single global PGroup that all PCaches are a member ** of. ** ** Mode 1 uses more memory (since PCache instances are not able to rob ** unused pages from other PCaches) but it also operates without a mutex, ** and is therefore often faster. Mode 2 requires a mutex in order to be ** threadsafe, but recycles pages more efficiently. ** ** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single ** PGroup which is the pcache1.grp global variable and its mutex is ** SQLITE_MUTEX_STATIC_LRU. */ type TPGroup1 = struct { Fmutex uintptr FnMaxPage uint32 FnMinPage uint32 FmxPinned uint32 FnPurgeable uint32 Flru TPgHdr1 } type PGroup1 = TPGroup1 /* Each page cache is an instance of the following object. Every ** open database file (including each in-memory database and each ** temporary or transient database) has a single page cache which ** is an instance of this object. ** ** Pointers to structures of this type are cast and returned as ** opaque sqlite3_pcache* handles. */ type TPCache11 = struct { FpGroup uintptr FpnPurgeable uintptr FszPage int32 FszExtra int32 FszAlloc int32 FbPurgeable int32 FnMin uint32 FnMax uint32 Fn90pct uint32 FiMaxKey uint32 FnPurgeableDummy uint32 FnRecyclable uint32 FnPage uint32 FnHash uint32 FapHash uintptr FpFree uintptr FpBulk uintptr } type PCache11 = TPCache11 /* ** Free slots in the allocator used to divide up the global page cache ** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism. */ type TPgFreeslot1 = struct { FpNext uintptr } type PgFreeslot1 = TPgFreeslot1 // C documentation // // /* // ** Global data used by this cache. // */ type TPCacheGlobal = struct { Fgrp TPGroup FisInit int32 FseparateCache int32 FnInitPage int32 FszSlot int32 FnSlot int32 FnReserve int32 FpStart uintptr FpEnd uintptr Fmutex uintptr FpFree uintptr FnFreeSlot int32 FbUnderPressure int32 } type PCacheGlobal = TPCacheGlobal // C documentation // // /* // ** Global data used by this cache. // */ var _pcache1_g TPCacheGlobal /* ** All code in this file should access the global structure above via the ** alias "pcache1". This ensures that the WSD emulation is used when ** compiling for systems that do not support real WSD. */ /* ** Macros to enter and leave the PCache LRU mutex. */ /******************************************************************************/ /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/ // C documentation // // /* // ** This function is called during initialization if a static buffer is // ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE // ** verb to sqlite3_config(). Parameter pBuf points to an allocation large // ** enough to contain 'n' buffers of 'sz' bytes each. // ** // ** This routine is called from sqlite3_initialize() and so it is guaranteed // ** to be serialized already. There is no need for further mutexing. // */ func _sqlite3PCacheBufferSetup(tls *libc.TLS, pBuf uintptr, sz int32, n int32) { var p uintptr var v1, v2, v3, v4 int32 _, _, _, _, _ = p, v1, v2, v3, v4 if libc.AtomicLoadPInt32(uintptr(unsafe.Pointer(&_pcache1_g))+52) != 0 { if pBuf == uintptr(0) { v1 = libc.Int32FromInt32(0) n = v1 sz = v1 } if n == 0 { sz = 0 } sz = sz & ^libc.Int32FromInt32(7) _pcache1_g.FszSlot = sz v2 = n _pcache1_g.FnFreeSlot = v2 _pcache1_g.FnSlot = v2 if n > int32(90) { v3 = int32(10) } else { v3 = n/int32(10) + int32(1) } _pcache1_g.FnReserve = v3 _pcache1_g.FpStart = pBuf _pcache1_g.FpFree = uintptr(0) _pcache1_g.FbUnderPressure = 0 for { v4 = n n-- if !(v4 != 0) { break } p = pBuf (*TPgFreeslot)(unsafe.Pointer(p)).FpNext = _pcache1_g.FpFree _pcache1_g.FpFree = p pBuf = pBuf + uintptr(sz) } _pcache1_g.FpEnd = pBuf } } // C documentation // // /* // ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return // ** true if pCache->pFree ends up containing one or more free pages. // */ func _pcache1InitBulk(tls *libc.TLS, pCache uintptr) (r int32) { var nBulk, v2 int32 var pX, zBulk, v1 uintptr var szBulk Ti64 _, _, _, _, _, _ = nBulk, pX, szBulk, zBulk, v1, v2 if _pcache1_g.FnInitPage == 0 { return 0 } /* Do not bother with a bulk allocation if the cache size very small */ if (*TPCache1)(unsafe.Pointer(pCache)).FnMax < uint32(3) { return 0 } _sqlite3BeginBenignMalloc(tls) if _pcache1_g.FnInitPage > 0 { szBulk = int64((*TPCache1)(unsafe.Pointer(pCache)).FszAlloc) * int64(_pcache1_g.FnInitPage) } else { szBulk = int64(-int32(1024)) * int64(_pcache1_g.FnInitPage) } if szBulk > int64((*TPCache1)(unsafe.Pointer(pCache)).FszAlloc)*int64((*TPCache1)(unsafe.Pointer(pCache)).FnMax) { szBulk = int64((*TPCache1)(unsafe.Pointer(pCache)).FszAlloc) * int64((*TPCache1)(unsafe.Pointer(pCache)).FnMax) } v1 = _sqlite3Malloc(tls, uint64(szBulk)) (*TPCache1)(unsafe.Pointer(pCache)).FpBulk = v1 zBulk = v1 _sqlite3EndBenignMalloc(tls) if zBulk != 0 { nBulk = _sqlite3MallocSize(tls, zBulk) / (*TPCache1)(unsafe.Pointer(pCache)).FszAlloc for { pX = zBulk + uintptr((*TPCache1)(unsafe.Pointer(pCache)).FszPage) (*TPgHdr1)(unsafe.Pointer(pX)).Fpage.FpBuf = zBulk (*TPgHdr1)(unsafe.Pointer(pX)).Fpage.FpExtra = pX + 1*32 (*TPgHdr1)(unsafe.Pointer(pX)).FisBulkLocal = uint16(1) (*TPgHdr1)(unsafe.Pointer(pX)).FisAnchor = uint16(0) (*TPgHdr1)(unsafe.Pointer(pX)).FpNext = (*TPCache1)(unsafe.Pointer(pCache)).FpFree (*TPgHdr1)(unsafe.Pointer(pX)).FpLruPrev = uintptr(0) /* Initializing this saves a valgrind error */ (*TPCache1)(unsafe.Pointer(pCache)).FpFree = pX zBulk += uintptr((*TPCache1)(unsafe.Pointer(pCache)).FszAlloc) goto _3 _3: ; nBulk-- v2 = nBulk if !(v2 != 0) { break } } } return libc.BoolInt32((*TPCache1)(unsafe.Pointer(pCache)).FpFree != uintptr(0)) } // C documentation // // /* // ** Malloc function used within this file to allocate space from the buffer // ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no // ** such buffer exists or there is no space left in it, this function falls // ** back to sqlite3Malloc(). // ** // ** Multiple threads can run this routine at the same time. Global variables // ** in pcache1 need to be protected via mutex. // */ func _pcache1Alloc(tls *libc.TLS, nByte int32) (r uintptr) { var p uintptr var sz int32 _, _ = p, sz p = uintptr(0) _ = libc.Int32FromInt32(0) if nByte <= _pcache1_g.FszSlot { Xsqlite3_mutex_enter(tls, _pcache1_g.Fmutex) p = _pcache1_g.FpFree if p != 0 { _pcache1_g.FpFree = (*TPgFreeslot)(unsafe.Pointer(_pcache1_g.FpFree)).FpNext _pcache1_g.FnFreeSlot-- _pcache1_g.FbUnderPressure = libc.BoolInt32(_pcache1_g.FnFreeSlot < _pcache1_g.FnReserve) _ = libc.Int32FromInt32(0) _sqlite3StatusHighwater(tls, int32(SQLITE_STATUS_PAGECACHE_SIZE), nByte) _sqlite3StatusUp(tls, int32(SQLITE_STATUS_PAGECACHE_USED), int32(1)) } Xsqlite3_mutex_leave(tls, _pcache1_g.Fmutex) } if p == uintptr(0) { /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get ** it from sqlite3Malloc instead. */ p = _sqlite3Malloc(tls, uint64(nByte)) if p != 0 { sz = _sqlite3MallocSize(tls, p) Xsqlite3_mutex_enter(tls, _pcache1_g.Fmutex) _sqlite3StatusHighwater(tls, int32(SQLITE_STATUS_PAGECACHE_SIZE), nByte) _sqlite3StatusUp(tls, int32(SQLITE_STATUS_PAGECACHE_OVERFLOW), sz) Xsqlite3_mutex_leave(tls, _pcache1_g.Fmutex) } } return p } // C documentation // // /* // ** Free an allocated buffer obtained from pcache1Alloc(). // */ func _pcache1Free(tls *libc.TLS, p uintptr) { var nFreed int32 var pSlot uintptr _, _ = nFreed, pSlot if p == uintptr(0) { return } if uint32(p) >= uint32(_pcache1_g.FpStart) && uint32(p) < uint32(_pcache1_g.FpEnd) { Xsqlite3_mutex_enter(tls, _pcache1_g.Fmutex) _sqlite3StatusDown(tls, int32(SQLITE_STATUS_PAGECACHE_USED), int32(1)) pSlot = p (*TPgFreeslot)(unsafe.Pointer(pSlot)).FpNext = _pcache1_g.FpFree _pcache1_g.FpFree = pSlot _pcache1_g.FnFreeSlot++ _pcache1_g.FbUnderPressure = libc.BoolInt32(_pcache1_g.FnFreeSlot < _pcache1_g.FnReserve) _ = libc.Int32FromInt32(0) Xsqlite3_mutex_leave(tls, _pcache1_g.Fmutex) } else { _ = libc.Int32FromInt32(0) nFreed = 0 nFreed = _sqlite3MallocSize(tls, p) Xsqlite3_mutex_enter(tls, _pcache1_g.Fmutex) _sqlite3StatusDown(tls, int32(SQLITE_STATUS_PAGECACHE_OVERFLOW), nFreed) Xsqlite3_mutex_leave(tls, _pcache1_g.Fmutex) Xsqlite3_free(tls, p) } } // C documentation // // /* // ** Return the size of a pcache allocation // */ func _pcache1MemSize(tls *libc.TLS, p uintptr) (r int32) { var iSize int32 _ = iSize if p >= _pcache1_g.FpStart && p < _pcache1_g.FpEnd { return _pcache1_g.FszSlot } else { _ = libc.Int32FromInt32(0) iSize = _sqlite3MallocSize(tls, p) return iSize } return r } // C documentation // // /* // ** Allocate a new page object initially associated with cache pCache. // */ func _pcache1AllocPage(tls *libc.TLS, pCache uintptr, benignMalloc int32) (r uintptr) { var p, pPg uintptr _, _ = p, pPg p = uintptr(0) _ = libc.Int32FromInt32(0) if (*TPCache1)(unsafe.Pointer(pCache)).FpFree != 0 || (*TPCache1)(unsafe.Pointer(pCache)).FnPage == uint32(0) && _pcache1InitBulk(tls, pCache) != 0 { _ = libc.Int32FromInt32(0) p = (*TPCache1)(unsafe.Pointer(pCache)).FpFree (*TPCache1)(unsafe.Pointer(pCache)).FpFree = (*TPgHdr1)(unsafe.Pointer(p)).FpNext (*TPgHdr1)(unsafe.Pointer(p)).FpNext = uintptr(0) } else { /* The group mutex must be released before pcache1Alloc() is called. This ** is because it might call sqlite3_release_memory(), which assumes that ** this mutex is not held. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex) if benignMalloc != 0 { _sqlite3BeginBenignMalloc(tls) } pPg = _pcache1Alloc(tls, (*TPCache1)(unsafe.Pointer(pCache)).FszAlloc) if benignMalloc != 0 { _sqlite3EndBenignMalloc(tls) } Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex) if pPg == uintptr(0) { return uintptr(0) } p = pPg + uintptr((*TPCache1)(unsafe.Pointer(pCache)).FszPage) (*TPgHdr1)(unsafe.Pointer(p)).Fpage.FpBuf = pPg (*TPgHdr1)(unsafe.Pointer(p)).Fpage.FpExtra = p + 1*32 (*TPgHdr1)(unsafe.Pointer(p)).FisBulkLocal = uint16(0) (*TPgHdr1)(unsafe.Pointer(p)).FisAnchor = uint16(0) (*TPgHdr1)(unsafe.Pointer(p)).FpLruPrev = uintptr(0) /* Initializing this saves a valgrind error */ } *(*uint32)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpnPurgeable))++ return p } // C documentation // // /* // ** Free a page object allocated by pcache1AllocPage(). // */ func _pcache1FreePage(tls *libc.TLS, p uintptr) { var pCache uintptr _ = pCache _ = libc.Int32FromInt32(0) pCache = (*TPgHdr1)(unsafe.Pointer(p)).FpCache _ = libc.Int32FromInt32(0) if (*TPgHdr1)(unsafe.Pointer(p)).FisBulkLocal != 0 { (*TPgHdr1)(unsafe.Pointer(p)).FpNext = (*TPCache1)(unsafe.Pointer(pCache)).FpFree (*TPCache1)(unsafe.Pointer(pCache)).FpFree = p } else { _pcache1Free(tls, (*TPgHdr1)(unsafe.Pointer(p)).Fpage.FpBuf) } *(*uint32)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpnPurgeable))-- } // C documentation // // /* // ** Malloc function used by SQLite to obtain space from the buffer configured // ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer // ** exists, this function falls back to sqlite3Malloc(). // */ func _sqlite3PageMalloc(tls *libc.TLS, sz int32) (r uintptr) { _ = libc.Int32FromInt32(0) /* These allocations are never very large */ return _pcache1Alloc(tls, sz) } // C documentation // // /* // ** Free an allocated buffer obtained from sqlite3PageMalloc(). // */ func _sqlite3PageFree(tls *libc.TLS, p uintptr) { _pcache1Free(tls, p) } // C documentation // // /* // ** Return true if it desirable to avoid allocating a new page cache // ** entry. // ** // ** If memory was allocated specifically to the page cache using // ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then // ** it is desirable to avoid allocating a new page cache entry because // ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient // ** for all page cache needs and we should not need to spill the // ** allocation onto the heap. // ** // ** Or, the heap is used for all page cache memory but the heap is // ** under memory pressure, then again it is desirable to avoid // ** allocating a new page cache entry in order to avoid stressing // ** the heap even further. // */ func _pcache1UnderMemoryPressure(tls *libc.TLS, pCache uintptr) (r int32) { if _pcache1_g.FnSlot != 0 && (*TPCache1)(unsafe.Pointer(pCache)).FszPage+(*TPCache1)(unsafe.Pointer(pCache)).FszExtra <= _pcache1_g.FszSlot { return _pcache1_g.FbUnderPressure } else { return _sqlite3HeapNearlyFull(tls) } return r } /******************************************************************************/ /******** General Implementation Functions ************************************/ // C documentation // // /* // ** This function is used to resize the hash table used by the cache passed // ** as the first argument. // ** // ** The PCache mutex must be held when this function is called. // */ func _pcache1ResizeHash(tls *libc.TLS, p uintptr) { var apNew, pNext, pPage, v2 uintptr var h, i, nNew uint32 _, _, _, _, _, _, _ = apNew, h, i, nNew, pNext, pPage, v2 _ = libc.Int32FromInt32(0) nNew = (*TPCache1)(unsafe.Pointer(p)).FnHash * uint32(2) if nNew < uint32(256) { nNew = uint32(256) } Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(p)).FpGroup)).Fmutex) if (*TPCache1)(unsafe.Pointer(p)).FnHash != 0 { _sqlite3BeginBenignMalloc(tls) } apNew = _sqlite3MallocZero(tls, uint64(uint32(4)*nNew)) if (*TPCache1)(unsafe.Pointer(p)).FnHash != 0 { _sqlite3EndBenignMalloc(tls) } Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(p)).FpGroup)).Fmutex) if apNew != 0 { i = uint32(0) for { if !(i < (*TPCache1)(unsafe.Pointer(p)).FnHash) { break } pNext = *(*uintptr)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(p)).FapHash + uintptr(i)*4)) for { v2 = pNext pPage = v2 if !(v2 != uintptr(0)) { break } h = (*TPgHdr1)(unsafe.Pointer(pPage)).FiKey % nNew pNext = (*TPgHdr1)(unsafe.Pointer(pPage)).FpNext (*TPgHdr1)(unsafe.Pointer(pPage)).FpNext = *(*uintptr)(unsafe.Pointer(apNew + uintptr(h)*4)) *(*uintptr)(unsafe.Pointer(apNew + uintptr(h)*4)) = pPage } goto _1 _1: ; i++ } Xsqlite3_free(tls, (*TPCache1)(unsafe.Pointer(p)).FapHash) (*TPCache1)(unsafe.Pointer(p)).FapHash = apNew (*TPCache1)(unsafe.Pointer(p)).FnHash = nNew } } // C documentation // // /* // ** This function is used internally to remove the page pPage from the // ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup // ** LRU list, then this function is a no-op. // ** // ** The PGroup mutex must be held when this function is called. // */ func _pcache1PinPage(tls *libc.TLS, pPage uintptr) (r uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TPgHdr1)(unsafe.Pointer((*TPgHdr1)(unsafe.Pointer(pPage)).FpLruPrev)).FpLruNext = (*TPgHdr1)(unsafe.Pointer(pPage)).FpLruNext (*TPgHdr1)(unsafe.Pointer((*TPgHdr1)(unsafe.Pointer(pPage)).FpLruNext)).FpLruPrev = (*TPgHdr1)(unsafe.Pointer(pPage)).FpLruPrev (*TPgHdr1)(unsafe.Pointer(pPage)).FpLruNext = uintptr(0) /* pPage->pLruPrev = 0; ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TPCache1)(unsafe.Pointer((*TPgHdr1)(unsafe.Pointer(pPage)).FpCache)).FnRecyclable-- return pPage } // C documentation // // /* // ** Remove the page supplied as an argument from the hash table // ** (PCache1.apHash structure) that it is currently stored in. // ** Also free the page if freePage is true. // ** // ** The PGroup mutex must be held when this function is called. // */ func _pcache1RemoveFromHash(tls *libc.TLS, pPage uintptr, freeFlag int32) { var h uint32 var pCache, pp uintptr _, _, _ = h, pCache, pp pCache = (*TPgHdr1)(unsafe.Pointer(pPage)).FpCache _ = libc.Int32FromInt32(0) h = (*TPgHdr1)(unsafe.Pointer(pPage)).FiKey % (*TPCache1)(unsafe.Pointer(pCache)).FnHash pp = (*TPCache1)(unsafe.Pointer(pCache)).FapHash + uintptr(h)*4 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != pPage) { break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 16 } *(*uintptr)(unsafe.Pointer(pp)) = (*TPgHdr1)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FpNext (*TPCache1)(unsafe.Pointer(pCache)).FnPage-- if freeFlag != 0 { _pcache1FreePage(tls, pPage) } } // C documentation // // /* // ** If there are currently more than nMaxPage pages allocated, try // ** to recycle pages to reduce the number allocated to nMaxPage. // */ func _pcache1EnforceMaxPage(tls *libc.TLS, pCache uintptr) { var p, pGroup, v1, v3 uintptr var v2 bool _, _, _, _, _ = p, pGroup, v1, v2, v3 pGroup = (*TPCache1)(unsafe.Pointer(pCache)).FpGroup _ = libc.Int32FromInt32(0) for { if v2 = (*TPGroup)(unsafe.Pointer(pGroup)).FnPurgeable > (*TPGroup)(unsafe.Pointer(pGroup)).FnMaxPage; v2 { v1 = (*TPGroup)(unsafe.Pointer(pGroup)).Flru.FpLruPrev p = v1 } if !(v2 && int32((*TPgHdr1)(unsafe.Pointer(v1)).FisAnchor) == 0) { break } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _pcache1PinPage(tls, p) _pcache1RemoveFromHash(tls, p, int32(1)) } if (*TPCache1)(unsafe.Pointer(pCache)).FnPage == uint32(0) && (*TPCache1)(unsafe.Pointer(pCache)).FpBulk != 0 { Xsqlite3_free(tls, (*TPCache1)(unsafe.Pointer(pCache)).FpBulk) v3 = libc.UintptrFromInt32(0) (*TPCache1)(unsafe.Pointer(pCache)).FpFree = v3 (*TPCache1)(unsafe.Pointer(pCache)).FpBulk = v3 } } // C documentation // // /* // ** Discard all pages from cache pCache with a page number (key value) // ** greater than or equal to iLimit. Any pinned pages that meet this // ** criteria are unpinned before they are discarded. // ** // ** The PCache mutex must be held when this function is called. // */ func _pcache1TruncateUnsafe(tls *libc.TLS, pCache uintptr, iLimit uint32) { var h, iStop uint32 var pPage, pp, v2 uintptr _, _, _, _, _ = h, iStop, pPage, pp, v2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TPCache1)(unsafe.Pointer(pCache)).FiMaxKey-iLimit < (*TPCache1)(unsafe.Pointer(pCache)).FnHash { /* If we are just shaving the last few pages off the end of the ** cache, then there is no point in scanning the entire hash table. ** Only scan those hash slots that might contain pages that need to ** be removed. */ h = iLimit % (*TPCache1)(unsafe.Pointer(pCache)).FnHash iStop = (*TPCache1)(unsafe.Pointer(pCache)).FiMaxKey % (*TPCache1)(unsafe.Pointer(pCache)).FnHash /* Disable the pCache->nPage validity check */ } else { /* This is the general case where many pages are being removed. ** It is necessary to scan the entire hash table */ h = (*TPCache1)(unsafe.Pointer(pCache)).FnHash / uint32(2) iStop = h - uint32(1) } for { _ = libc.Int32FromInt32(0) pp = (*TPCache1)(unsafe.Pointer(pCache)).FapHash + uintptr(h)*4 for { v2 = *(*uintptr)(unsafe.Pointer(pp)) pPage = v2 if !(v2 != uintptr(0)) { break } if (*TPgHdr1)(unsafe.Pointer(pPage)).FiKey >= iLimit { (*TPCache1)(unsafe.Pointer(pCache)).FnPage-- *(*uintptr)(unsafe.Pointer(pp)) = (*TPgHdr1)(unsafe.Pointer(pPage)).FpNext if (*TPgHdr1)(unsafe.Pointer(pPage)).FpLruNext != uintptr(0) { _pcache1PinPage(tls, pPage) } _pcache1FreePage(tls, pPage) } else { pp = pPage + 16 } } if h == iStop { break } h = (h + uint32(1)) % (*TPCache1)(unsafe.Pointer(pCache)).FnHash goto _1 _1: } _ = libc.Int32FromInt32(0) } /******************************************************************************/ /******** sqlite3_pcache Methods **********************************************/ // C documentation // // /* // ** Implementation of the sqlite3_pcache.xInit method. // */ func _pcache1Init(tls *libc.TLS, NotUsed uintptr) (r int32) { _ = NotUsed _ = libc.Int32FromInt32(0) libc.Xmemset(tls, uintptr(unsafe.Pointer(&_pcache1_g)), 0, uint32(100)) /* ** The pcache1.separateCache variable is true if each PCache has its own ** private PGroup (mode-1). pcache1.separateCache is false if the single ** PGroup in pcache1.grp is used for all page caches (mode-2). ** ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT ** ** * Use a unified cache in single-threaded applications that have ** configured a start-time buffer for use as page-cache memory using ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL ** pBuf argument. ** ** * Otherwise use separate caches (mode-1) */ _pcache1_g.FseparateCache = 0 if _sqlite3Config.FbCoreMutex != 0 { _pcache1_g.Fgrp.Fmutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_LRU)) _pcache1_g.Fmutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_PMEM)) } if _pcache1_g.FseparateCache != 0 && _sqlite3Config.FnPage != 0 && _sqlite3Config.FpPage == uintptr(0) { _pcache1_g.FnInitPage = _sqlite3Config.FnPage } else { _pcache1_g.FnInitPage = 0 } _pcache1_g.Fgrp.FmxPinned = uint32(10) libc.AtomicStorePInt32(uintptr(unsafe.Pointer(&_pcache1_g))+52, int32(1)) return SQLITE_OK } // C documentation // // /* // ** Implementation of the sqlite3_pcache.xShutdown method. // ** Note that the static mutex allocated in xInit does // ** not need to be freed. // */ func _pcache1Shutdown(tls *libc.TLS, NotUsed uintptr) { _ = NotUsed _ = libc.Int32FromInt32(0) libc.Xmemset(tls, uintptr(unsafe.Pointer(&_pcache1_g)), 0, uint32(100)) } // C documentation // // /* // ** Implementation of the sqlite3_pcache.xCreate method. // ** // ** Allocate a new cache. // */ func _pcache1Create(tls *libc.TLS, szPage int32, szExtra int32, bPurgeable int32) (r uintptr) { var pCache, pGroup, v1 uintptr var sz, v2 int32 _, _, _, _, _ = pCache, pGroup, sz, v1, v2 /* Bytes of memory required to allocate the new cache */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) sz = int32(uint32(68) + uint32(52)*uint32(_pcache1_g.FseparateCache)) pCache = _sqlite3MallocZero(tls, uint64(sz)) if pCache != 0 { if _pcache1_g.FseparateCache != 0 { pGroup = pCache + 1*68 (*TPGroup)(unsafe.Pointer(pGroup)).FmxPinned = uint32(10) } else { pGroup = uintptr(unsafe.Pointer(&_pcache1_g)) } Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer(pGroup)).Fmutex) if int32((*TPGroup)(unsafe.Pointer(pGroup)).Flru.FisAnchor) == 0 { (*TPGroup)(unsafe.Pointer(pGroup)).Flru.FisAnchor = uint16(1) v1 = pGroup + 20 (*TPGroup)(unsafe.Pointer(pGroup)).Flru.FpLruNext = v1 (*TPGroup)(unsafe.Pointer(pGroup)).Flru.FpLruPrev = v1 } (*TPCache1)(unsafe.Pointer(pCache)).FpGroup = pGroup (*TPCache1)(unsafe.Pointer(pCache)).FszPage = szPage (*TPCache1)(unsafe.Pointer(pCache)).FszExtra = szExtra (*TPCache1)(unsafe.Pointer(pCache)).FszAlloc = int32(uint32(szPage+szExtra) + (libc.Uint32FromInt64(32)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))) if bPurgeable != 0 { v2 = int32(1) } else { v2 = 0 } (*TPCache1)(unsafe.Pointer(pCache)).FbPurgeable = v2 _pcache1ResizeHash(tls, pCache) if bPurgeable != 0 { (*TPCache1)(unsafe.Pointer(pCache)).FnMin = uint32(10) *(*uint32)(unsafe.Pointer(pGroup + 8)) += (*TPCache1)(unsafe.Pointer(pCache)).FnMin (*TPGroup)(unsafe.Pointer(pGroup)).FmxPinned = (*TPGroup)(unsafe.Pointer(pGroup)).FnMaxPage + uint32(10) - (*TPGroup)(unsafe.Pointer(pGroup)).FnMinPage (*TPCache1)(unsafe.Pointer(pCache)).FpnPurgeable = pGroup + 16 } else { (*TPCache1)(unsafe.Pointer(pCache)).FpnPurgeable = pCache + 40 } Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer(pGroup)).Fmutex) if (*TPCache1)(unsafe.Pointer(pCache)).FnHash == uint32(0) { _pcache1Destroy(tls, pCache) pCache = uintptr(0) } } return pCache } // C documentation // // /* // ** Implementation of the sqlite3_pcache.xCachesize method. // ** // ** Configure the cache_size limit for a cache. // */ func _pcache1Cachesize(tls *libc.TLS, p uintptr, nMax int32) { var n Tu32 var pCache, pGroup uintptr _, _, _ = n, pCache, pGroup pCache = p _ = libc.Int32FromInt32(0) if (*TPCache1)(unsafe.Pointer(pCache)).FbPurgeable != 0 { pGroup = (*TPCache1)(unsafe.Pointer(pCache)).FpGroup Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer(pGroup)).Fmutex) n = uint32(nMax) if n > uint32(0x7fff0000)-(*TPGroup)(unsafe.Pointer(pGroup)).FnMaxPage+(*TPCache1)(unsafe.Pointer(pCache)).FnMax { n = uint32(0x7fff0000) - (*TPGroup)(unsafe.Pointer(pGroup)).FnMaxPage + (*TPCache1)(unsafe.Pointer(pCache)).FnMax } *(*uint32)(unsafe.Pointer(pGroup + 4)) += n - (*TPCache1)(unsafe.Pointer(pCache)).FnMax (*TPGroup)(unsafe.Pointer(pGroup)).FmxPinned = (*TPGroup)(unsafe.Pointer(pGroup)).FnMaxPage + uint32(10) - (*TPGroup)(unsafe.Pointer(pGroup)).FnMinPage (*TPCache1)(unsafe.Pointer(pCache)).FnMax = n (*TPCache1)(unsafe.Pointer(pCache)).Fn90pct = (*TPCache1)(unsafe.Pointer(pCache)).FnMax * uint32(9) / uint32(10) _pcache1EnforceMaxPage(tls, pCache) Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer(pGroup)).Fmutex) } } // C documentation // // /* // ** Implementation of the sqlite3_pcache.xShrink method. // ** // ** Free up as much memory as possible. // */ func _pcache1Shrink(tls *libc.TLS, p uintptr) { var pCache, pGroup uintptr var savedMaxPage uint32 _, _, _ = pCache, pGroup, savedMaxPage pCache = p if (*TPCache1)(unsafe.Pointer(pCache)).FbPurgeable != 0 { pGroup = (*TPCache1)(unsafe.Pointer(pCache)).FpGroup Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer(pGroup)).Fmutex) savedMaxPage = (*TPGroup)(unsafe.Pointer(pGroup)).FnMaxPage (*TPGroup)(unsafe.Pointer(pGroup)).FnMaxPage = uint32(0) _pcache1EnforceMaxPage(tls, pCache) (*TPGroup)(unsafe.Pointer(pGroup)).FnMaxPage = savedMaxPage Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer(pGroup)).Fmutex) } } // C documentation // // /* // ** Implementation of the sqlite3_pcache.xPagecount method. // */ func _pcache1Pagecount(tls *libc.TLS, p uintptr) (r int32) { var n int32 var pCache uintptr _, _ = n, pCache pCache = p Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex) n = int32((*TPCache1)(unsafe.Pointer(pCache)).FnPage) Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex) return n } // C documentation // // /* // ** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described // ** in the header of the pcache1Fetch() procedure. // ** // ** This steps are broken out into a separate procedure because they are // ** usually not needed, and by avoiding the stack initialization required // ** for these steps, the main pcache1Fetch() procedure can run faster. // */ func _pcache1FetchStage2(tls *libc.TLS, pCache uintptr, iKey uint32, createFlag int32) (r uintptr) { var h, nPinned uint32 var pGroup, pOther, pPage uintptr _, _, _, _, _ = h, nPinned, pGroup, pOther, pPage pGroup = (*TPCache1)(unsafe.Pointer(pCache)).FpGroup pPage = uintptr(0) /* Step 3: Abort if createFlag is 1 but the cache is nearly full */ _ = libc.Int32FromInt32(0) nPinned = (*TPCache1)(unsafe.Pointer(pCache)).FnPage - (*TPCache1)(unsafe.Pointer(pCache)).FnRecyclable _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if createFlag == int32(1) && (nPinned >= (*TPGroup)(unsafe.Pointer(pGroup)).FmxPinned || nPinned >= (*TPCache1)(unsafe.Pointer(pCache)).Fn90pct || _pcache1UnderMemoryPressure(tls, pCache) != 0 && (*TPCache1)(unsafe.Pointer(pCache)).FnRecyclable < nPinned) { return uintptr(0) } if (*TPCache1)(unsafe.Pointer(pCache)).FnPage >= (*TPCache1)(unsafe.Pointer(pCache)).FnHash { _pcache1ResizeHash(tls, pCache) } _ = libc.Int32FromInt32(0) /* Step 4. Try to recycle a page. */ if (*TPCache1)(unsafe.Pointer(pCache)).FbPurgeable != 0 && !((*TPgHdr1)(unsafe.Pointer((*TPGroup)(unsafe.Pointer(pGroup)).Flru.FpLruPrev)).FisAnchor != 0) && ((*TPCache1)(unsafe.Pointer(pCache)).FnPage+uint32(1) >= (*TPCache1)(unsafe.Pointer(pCache)).FnMax || _pcache1UnderMemoryPressure(tls, pCache) != 0) { pPage = (*TPGroup)(unsafe.Pointer(pGroup)).Flru.FpLruPrev _ = libc.Int32FromInt32(0) _pcache1RemoveFromHash(tls, pPage, 0) _pcache1PinPage(tls, pPage) pOther = (*TPgHdr1)(unsafe.Pointer(pPage)).FpCache if (*TPCache1)(unsafe.Pointer(pOther)).FszAlloc != (*TPCache1)(unsafe.Pointer(pCache)).FszAlloc { _pcache1FreePage(tls, pPage) pPage = uintptr(0) } else { *(*uint32)(unsafe.Pointer(pGroup + 16)) -= uint32((*TPCache1)(unsafe.Pointer(pOther)).FbPurgeable - (*TPCache1)(unsafe.Pointer(pCache)).FbPurgeable) } } /* Step 5. If a usable page buffer has still not been found, ** attempt to allocate a new one. */ if !(pPage != 0) { pPage = _pcache1AllocPage(tls, pCache, libc.BoolInt32(createFlag == int32(1))) } if pPage != 0 { h = iKey % (*TPCache1)(unsafe.Pointer(pCache)).FnHash (*TPCache1)(unsafe.Pointer(pCache)).FnPage++ (*TPgHdr1)(unsafe.Pointer(pPage)).FiKey = iKey (*TPgHdr1)(unsafe.Pointer(pPage)).FpNext = *(*uintptr)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FapHash + uintptr(h)*4)) (*TPgHdr1)(unsafe.Pointer(pPage)).FpCache = pCache (*TPgHdr1)(unsafe.Pointer(pPage)).FpLruNext = uintptr(0) /* pPage->pLruPrev = 0; ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */ *(*uintptr)(unsafe.Pointer((*TPgHdr1)(unsafe.Pointer(pPage)).Fpage.FpExtra)) = uintptr(0) *(*uintptr)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FapHash + uintptr(h)*4)) = pPage if iKey > (*TPCache1)(unsafe.Pointer(pCache)).FiMaxKey { (*TPCache1)(unsafe.Pointer(pCache)).FiMaxKey = iKey } } return pPage } // C documentation // // /* // ** Implementation of the sqlite3_pcache.xFetch method. // ** // ** Fetch a page by key value. // ** // ** Whether or not a new page may be allocated by this function depends on // ** the value of the createFlag argument. 0 means do not allocate a new // ** page. 1 means allocate a new page if space is easily available. 2 // ** means to try really hard to allocate a new page. // ** // ** For a non-purgeable cache (a cache used as the storage for an in-memory // ** database) there is really no difference between createFlag 1 and 2. So // ** the calling function (pcache.c) will never have a createFlag of 1 on // ** a non-purgeable cache. // ** // ** There are three different approaches to obtaining space for a page, // ** depending on the value of parameter createFlag (which may be 0, 1 or 2). // ** // ** 1. Regardless of the value of createFlag, the cache is searched for a // ** copy of the requested page. If one is found, it is returned. // ** // ** 2. If createFlag==0 and the page is not already in the cache, NULL is // ** returned. // ** // ** 3. If createFlag is 1, and the page is not already in the cache, then // ** return NULL (do not allocate a new page) if any of the following // ** conditions are true: // ** // ** (a) the number of pages pinned by the cache is greater than // ** PCache1.nMax, or // ** // ** (b) the number of pages pinned by the cache is greater than // ** the sum of nMax for all purgeable caches, less the sum of // ** nMin for all other purgeable caches, or // ** // ** 4. If none of the first three conditions apply and the cache is marked // ** as purgeable, and if one of the following is true: // ** // ** (a) The number of pages allocated for the cache is already // ** PCache1.nMax, or // ** // ** (b) The number of pages allocated for all purgeable caches is // ** already equal to or greater than the sum of nMax for all // ** purgeable caches, // ** // ** (c) The system is under memory pressure and wants to avoid // ** unnecessary pages cache entry allocations // ** // ** then attempt to recycle a page from the LRU list. If it is the right // ** size, return the recycled buffer. Otherwise, free the buffer and // ** proceed to step 5. // ** // ** 5. Otherwise, allocate and return a new page buffer. // ** // ** There are two versions of this routine. pcache1FetchWithMutex() is // ** the general case. pcache1FetchNoMutex() is a faster implementation for // ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper // ** invokes the appropriate routine. // */ func _pcache1FetchNoMutex(tls *libc.TLS, p uintptr, iKey uint32, createFlag int32) (r uintptr) { var pCache, pPage uintptr _, _ = pCache, pPage pCache = p pPage = uintptr(0) /* Step 1: Search the hash table for an existing entry. */ pPage = *(*uintptr)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FapHash + uintptr(iKey%(*TPCache1)(unsafe.Pointer(pCache)).FnHash)*4)) for pPage != 0 && (*TPgHdr1)(unsafe.Pointer(pPage)).FiKey != iKey { pPage = (*TPgHdr1)(unsafe.Pointer(pPage)).FpNext } /* Step 2: If the page was found in the hash table, then return it. ** If the page was not in the hash table and createFlag is 0, abort. ** Otherwise (page not in hash and createFlag!=0) continue with ** subsequent steps to try to create the page. */ if pPage != 0 { if (*TPgHdr1)(unsafe.Pointer(pPage)).FpLruNext != uintptr(0) { return _pcache1PinPage(tls, pPage) } else { return pPage } } else { if createFlag != 0 { /* Steps 3, 4, and 5 implemented by this subroutine */ return _pcache1FetchStage2(tls, pCache, iKey, createFlag) } else { return uintptr(0) } } return r } func _pcache1FetchWithMutex(tls *libc.TLS, p uintptr, iKey uint32, createFlag int32) (r uintptr) { var pCache, pPage uintptr _, _ = pCache, pPage pCache = p Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex) pPage = _pcache1FetchNoMutex(tls, p, iKey, createFlag) _ = libc.Int32FromInt32(0) Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex) return pPage } func _pcache1Fetch(tls *libc.TLS, p uintptr, iKey uint32, createFlag int32) (r uintptr) { var pCache uintptr _ = pCache pCache = p _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex != 0 { return _pcache1FetchWithMutex(tls, p, iKey, createFlag) } else { return _pcache1FetchNoMutex(tls, p, iKey, createFlag) } return r } // C documentation // // /* // ** Implementation of the sqlite3_pcache.xUnpin method. // ** // ** Mark a page as unpinned (eligible for asynchronous recycling). // */ func _pcache1Unpin(tls *libc.TLS, p uintptr, pPg uintptr, reuseUnlikely int32) { var pCache, pGroup, pPage, ppFirst, v1 uintptr _, _, _, _, _ = pCache, pGroup, pPage, ppFirst, v1 pCache = p pPage = pPg pGroup = (*TPCache1)(unsafe.Pointer(pCache)).FpGroup _ = libc.Int32FromInt32(0) Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer(pGroup)).Fmutex) /* It is an error to call this function if the page is already ** part of the PGroup LRU list. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if reuseUnlikely != 0 || (*TPGroup)(unsafe.Pointer(pGroup)).FnPurgeable > (*TPGroup)(unsafe.Pointer(pGroup)).FnMaxPage { _pcache1RemoveFromHash(tls, pPage, int32(1)) } else { /* Add the page to the PGroup LRU list. */ ppFirst = pGroup + 20 + 24 (*TPgHdr1)(unsafe.Pointer(pPage)).FpLruPrev = pGroup + 20 v1 = *(*uintptr)(unsafe.Pointer(ppFirst)) (*TPgHdr1)(unsafe.Pointer(pPage)).FpLruNext = v1 (*TPgHdr1)(unsafe.Pointer(v1)).FpLruPrev = pPage *(*uintptr)(unsafe.Pointer(ppFirst)) = pPage (*TPCache1)(unsafe.Pointer(pCache)).FnRecyclable++ } Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex) } // C documentation // // /* // ** Implementation of the sqlite3_pcache.xRekey method. // */ func _pcache1Rekey(tls *libc.TLS, p uintptr, pPg uintptr, iOld uint32, iNew uint32) { var hNew, hOld uint32 var pCache, pPage, pp uintptr _, _, _, _, _ = hNew, hOld, pCache, pPage, pp pCache = p pPage = pPg _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* The page number really is changing */ Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex) _ = libc.Int32FromInt32(0) /* pPg really is iOld */ hOld = iOld % (*TPCache1)(unsafe.Pointer(pCache)).FnHash pp = (*TPCache1)(unsafe.Pointer(pCache)).FapHash + uintptr(hOld)*4 for *(*uintptr)(unsafe.Pointer(pp)) != pPage { pp = *(*uintptr)(unsafe.Pointer(pp)) + 16 } *(*uintptr)(unsafe.Pointer(pp)) = (*TPgHdr1)(unsafe.Pointer(pPage)).FpNext _ = libc.Int32FromInt32(0) /* iNew not in cache */ hNew = iNew % (*TPCache1)(unsafe.Pointer(pCache)).FnHash (*TPgHdr1)(unsafe.Pointer(pPage)).FiKey = iNew (*TPgHdr1)(unsafe.Pointer(pPage)).FpNext = *(*uintptr)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FapHash + uintptr(hNew)*4)) *(*uintptr)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FapHash + uintptr(hNew)*4)) = pPage if iNew > (*TPCache1)(unsafe.Pointer(pCache)).FiMaxKey { (*TPCache1)(unsafe.Pointer(pCache)).FiMaxKey = iNew } Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex) } // C documentation // // /* // ** Implementation of the sqlite3_pcache.xTruncate method. // ** // ** Discard all unpinned pages in the cache with a page number equal to // ** or greater than parameter iLimit. Any pinned pages with a page number // ** equal to or greater than iLimit are implicitly unpinned. // */ func _pcache1Truncate(tls *libc.TLS, p uintptr, iLimit uint32) { var pCache uintptr _ = pCache pCache = p Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex) if iLimit <= (*TPCache1)(unsafe.Pointer(pCache)).FiMaxKey { _pcache1TruncateUnsafe(tls, pCache, iLimit) (*TPCache1)(unsafe.Pointer(pCache)).FiMaxKey = iLimit - uint32(1) } Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex) } // C documentation // // /* // ** Implementation of the sqlite3_pcache.xDestroy method. // ** // ** Destroy a cache allocated using pcache1Create(). // */ func _pcache1Destroy(tls *libc.TLS, p uintptr) { var pCache, pGroup uintptr _, _ = pCache, pGroup pCache = p pGroup = (*TPCache1)(unsafe.Pointer(pCache)).FpGroup _ = libc.Int32FromInt32(0) Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer(pGroup)).Fmutex) if (*TPCache1)(unsafe.Pointer(pCache)).FnPage != 0 { _pcache1TruncateUnsafe(tls, pCache, uint32(0)) } _ = libc.Int32FromInt32(0) *(*uint32)(unsafe.Pointer(pGroup + 4)) -= (*TPCache1)(unsafe.Pointer(pCache)).FnMax _ = libc.Int32FromInt32(0) *(*uint32)(unsafe.Pointer(pGroup + 8)) -= (*TPCache1)(unsafe.Pointer(pCache)).FnMin (*TPGroup)(unsafe.Pointer(pGroup)).FmxPinned = (*TPGroup)(unsafe.Pointer(pGroup)).FnMaxPage + uint32(10) - (*TPGroup)(unsafe.Pointer(pGroup)).FnMinPage _pcache1EnforceMaxPage(tls, pCache) Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer(pGroup)).Fmutex) Xsqlite3_free(tls, (*TPCache1)(unsafe.Pointer(pCache)).FpBulk) Xsqlite3_free(tls, (*TPCache1)(unsafe.Pointer(pCache)).FapHash) Xsqlite3_free(tls, pCache) } // C documentation // // /* // ** This function is called during initialization (sqlite3_initialize()) to // ** install the default pluggable cache module, assuming the user has not // ** already provided an alternative. // */ func _sqlite3PCacheSetDefault(tls *libc.TLS) { bp := tls.Alloc(16) defer tls.Free(16) Xsqlite3_config(tls, int32(SQLITE_CONFIG_PCACHE2), libc.VaList(bp+8, uintptr(unsafe.Pointer(&_defaultMethods1)))) } var _defaultMethods1 = Tsqlite3_pcache_methods2{ FiVersion: int32(1), } func init() { p := unsafe.Pointer(&_defaultMethods1) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_pcache1Init) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_pcache1Shutdown) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_pcache1Create) *(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_pcache1Cachesize) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_pcache1Pagecount) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_pcache1Fetch) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_pcache1Unpin) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_pcache1Rekey) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_pcache1Truncate) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_pcache1Destroy) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_pcache1Shrink) } // C documentation // // /* // ** Return the size of the header on each page of this PCACHE implementation. // */ func _sqlite3HeaderSizePcache1(tls *libc.TLS) (r int32) { return int32((libc.Uint32FromInt64(32) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7))) } // C documentation // // /* // ** Return the global mutex used by this PCACHE implementation. The // ** sqlite3_status() routine needs access to this mutex. // */ func _sqlite3Pcache1Mutex(tls *libc.TLS) (r uintptr) { return _pcache1_g.Fmutex } // C documentation // // /* // ** This function is called to free superfluous dynamically allocated memory // ** held by the pager system. Memory in use by any SQLite pager allocated // ** by the current thread may be sqlite3_free()ed. // ** // ** nReq is the number of bytes of memory required. Once this much has // ** been released, the function returns. The return value is the total number // ** of bytes of memory released. // */ func _sqlite3PcacheReleaseMemory(tls *libc.TLS, nReq int32) (r int32) { var nFree int32 var p, v1 uintptr var v2 bool _, _, _, _ = nFree, p, v1, v2 nFree = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if _sqlite3Config.FpPage == uintptr(0) { Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer(uintptr(unsafe.Pointer(&_pcache1_g)))).Fmutex) for { if v2 = nReq < 0 || nFree < nReq; v2 { v1 = _pcache1_g.Fgrp.Flru.FpLruPrev p = v1 } if !(v2 && v1 != uintptr(0) && int32((*TPgHdr1)(unsafe.Pointer(p)).FisAnchor) == 0) { break } nFree += _pcache1MemSize(tls, (*TPgHdr1)(unsafe.Pointer(p)).Fpage.FpBuf) _ = libc.Int32FromInt32(0) _pcache1PinPage(tls, p) _pcache1RemoveFromHash(tls, p, int32(1)) } Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer(uintptr(unsafe.Pointer(&_pcache1_g)))).Fmutex) } return nFree } /************** End of pcache1.c *********************************************/ /************** Begin file rowset.c ******************************************/ /* ** 2008 December 3 ** ** 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 module implements an object we call a "RowSet". ** ** The RowSet object is a collection of rowids. Rowids ** are inserted into the RowSet in an arbitrary order. Inserts ** can be intermixed with tests to see if a given rowid has been ** previously inserted into the RowSet. ** ** After all inserts are finished, it is possible to extract the ** elements of the RowSet in sorted order. Once this extraction ** process has started, no new elements may be inserted. ** ** Hence, the primitive operations for a RowSet are: ** ** CREATE ** INSERT ** TEST ** SMALLEST ** DESTROY ** ** The CREATE and DESTROY primitives are the constructor and destructor, ** obviously. The INSERT primitive adds a new element to the RowSet. ** TEST checks to see if an element is already in the RowSet. SMALLEST ** extracts the least value from the RowSet. ** ** The INSERT primitive might allocate additional memory. Memory is ** allocated in chunks so most INSERTs do no allocation. There is an ** upper bound on the size of allocated memory. No memory is freed ** until DESTROY. ** ** The TEST primitive includes a "batch" number. The TEST primitive ** will only see elements that were inserted before the last change ** in the batch number. In other words, if an INSERT occurs between ** two TESTs where the TESTs have the same batch number, then the ** value added by the INSERT will not be visible to the second TEST. ** The initial batch number is zero, so if the very first TEST contains ** a non-zero batch number, it will see all prior INSERTs. ** ** No INSERTs may occurs after a SMALLEST. An assertion will fail if ** that is attempted. ** ** The cost of an INSERT is roughly constant. (Sometimes new memory ** has to be allocated on an INSERT.) The cost of a TEST with a new ** batch number is O(NlogN) where N is the number of elements in the RowSet. ** The cost of a TEST using the same batch number is O(logN). The cost ** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST ** primitives are constant time. The cost of DESTROY is O(N). ** ** TEST and SMALLEST may not be used by the same RowSet. This used to ** be possible, but the feature was not used, so it was removed in order ** to simplify the code. */ /* #include "sqliteInt.h" */ /* ** Target size for allocation chunks. */ /* ** The number of rowset entries per allocation chunk. */ /* ** Each entry in a RowSet is an instance of the following object. ** ** This same object is reused to store a linked list of trees of RowSetEntry ** objects. In that alternative use, pRight points to the next entry ** in the list, pLeft points to the tree, and v is unused. The ** RowSet.pForest value points to the head of this forest list. */ type TRowSetEntry = struct { F__ccgo_align [0]uint32 Fv Ti64 FpRight uintptr FpLeft uintptr } type RowSetEntry = TRowSetEntry /* ** RowSetEntry objects are allocated in large chunks (instances of the ** following structure) to reduce memory allocation overhead. The ** chunks are kept on a linked list so that they can be deallocated ** when the RowSet is destroyed. */ type TRowSetChunk = struct { F__ccgo_align [0]uint32 FpNextChunk uintptr F__ccgo_align1 [4]byte FaEntry [63]TRowSetEntry } type RowSetChunk = TRowSetChunk /* ** A RowSet in an instance of the following structure. ** ** A typedef of this structure if found in sqliteInt.h. */ type TRowSet1 = struct { FpChunk uintptr Fdb uintptr FpEntry uintptr FpLast uintptr FpFresh uintptr FpForest uintptr FnFresh Tu16 FrsFlags Tu16 FiBatch int32 } type RowSet1 = TRowSet1 /* ** Allowed values for RowSet.rsFlags */ // C documentation // // /* // ** Allocate a RowSet object. Return NULL if a memory allocation // ** error occurs. // */ func _sqlite3RowSetInit(tls *libc.TLS, db uintptr) (r uintptr) { var N int32 var p uintptr _, _ = N, p p = _sqlite3DbMallocRawNN(tls, db, uint64(32)) if p != 0 { N = _sqlite3DbMallocSize(tls, db, p) (*TRowSet)(unsafe.Pointer(p)).FpChunk = uintptr(0) (*TRowSet)(unsafe.Pointer(p)).Fdb = db (*TRowSet)(unsafe.Pointer(p)).FpEntry = uintptr(0) (*TRowSet)(unsafe.Pointer(p)).FpLast = uintptr(0) (*TRowSet)(unsafe.Pointer(p)).FpForest = uintptr(0) (*TRowSet)(unsafe.Pointer(p)).FpFresh = uintptr((libc.Uint32FromInt64(32)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))) + p (*TRowSet)(unsafe.Pointer(p)).FnFresh = uint16((uint32(N) - (libc.Uint32FromInt64(32)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))) / libc.Uint32FromInt64(16)) (*TRowSet)(unsafe.Pointer(p)).FrsFlags = uint16(ROWSET_SORTED) (*TRowSet)(unsafe.Pointer(p)).FiBatch = 0 } return p } // C documentation // // /* // ** Deallocate all chunks from a RowSet. This frees all memory that // ** the RowSet has allocated over its lifetime. This routine is // ** the destructor for the RowSet. // */ func _sqlite3RowSetClear(tls *libc.TLS, pArg uintptr) { var p, pChunk, pNextChunk uintptr _, _, _ = p, pChunk, pNextChunk p = pArg pChunk = (*TRowSet)(unsafe.Pointer(p)).FpChunk for { if !(pChunk != 0) { break } pNextChunk = (*TRowSetChunk)(unsafe.Pointer(pChunk)).FpNextChunk _sqlite3DbFree(tls, (*TRowSet)(unsafe.Pointer(p)).Fdb, pChunk) goto _1 _1: ; pChunk = pNextChunk } (*TRowSet)(unsafe.Pointer(p)).FpChunk = uintptr(0) (*TRowSet)(unsafe.Pointer(p)).FnFresh = uint16(0) (*TRowSet)(unsafe.Pointer(p)).FpEntry = uintptr(0) (*TRowSet)(unsafe.Pointer(p)).FpLast = uintptr(0) (*TRowSet)(unsafe.Pointer(p)).FpForest = uintptr(0) (*TRowSet)(unsafe.Pointer(p)).FrsFlags = uint16(ROWSET_SORTED) } // C documentation // // /* // ** Deallocate all chunks from a RowSet. This frees all memory that // ** the RowSet has allocated over its lifetime. This routine is // ** the destructor for the RowSet. // */ func _sqlite3RowSetDelete(tls *libc.TLS, pArg uintptr) { _sqlite3RowSetClear(tls, pArg) _sqlite3DbFree(tls, (*TRowSet)(unsafe.Pointer(pArg)).Fdb, pArg) } // C documentation // // /* // ** Allocate a new RowSetEntry object that is associated with the // ** given RowSet. Return a pointer to the new and completely uninitialized // ** object. // ** // ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this // ** routine returns NULL. // */ func _rowSetEntryAlloc(tls *libc.TLS, p uintptr) (r uintptr) { var pNew, v1, v2 uintptr _, _, _ = pNew, v1, v2 _ = libc.Int32FromInt32(0) if int32((*TRowSet)(unsafe.Pointer(p)).FnFresh) == 0 { pNew = _sqlite3DbMallocRawNN(tls, (*TRowSet)(unsafe.Pointer(p)).Fdb, uint64(1016)) if pNew == uintptr(0) { return uintptr(0) } (*TRowSetChunk)(unsafe.Pointer(pNew)).FpNextChunk = (*TRowSet)(unsafe.Pointer(p)).FpChunk (*TRowSet)(unsafe.Pointer(p)).FpChunk = pNew (*TRowSet)(unsafe.Pointer(p)).FpFresh = pNew + 8 (*TRowSet)(unsafe.Pointer(p)).FnFresh = uint16(uint32(libc.Int32FromInt32(ROWSET_ALLOCATION_SIZE)-libc.Int32FromInt32(8)) / libc.Uint32FromInt64(16)) } (*TRowSet)(unsafe.Pointer(p)).FnFresh-- v2 = p + 16 v1 = *(*uintptr)(unsafe.Pointer(v2)) *(*uintptr)(unsafe.Pointer(v2)) += 16 return v1 } // C documentation // // /* // ** Insert a new value into a RowSet. // ** // ** The mallocFailed flag of the database connection is set if a // ** memory allocation fails. // */ func _sqlite3RowSetInsert(tls *libc.TLS, p uintptr, rowid Ti64) { var pEntry, pLast, p1 uintptr _, _, _ = pEntry, pLast, p1 /* The last prior entry */ /* This routine is never called after sqlite3RowSetNext() */ _ = libc.Int32FromInt32(0) pEntry = _rowSetEntryAlloc(tls, p) if pEntry == uintptr(0) { return } (*TRowSetEntry)(unsafe.Pointer(pEntry)).Fv = rowid (*TRowSetEntry)(unsafe.Pointer(pEntry)).FpRight = uintptr(0) pLast = (*TRowSet)(unsafe.Pointer(p)).FpLast if pLast != 0 { if rowid <= (*TRowSetEntry)(unsafe.Pointer(pLast)).Fv { /*OPTIMIZATION-IF-FALSE*/ /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags ** where possible */ p1 = p + 26 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(ROWSET_SORTED)) } (*TRowSetEntry)(unsafe.Pointer(pLast)).FpRight = pEntry } else { (*TRowSet)(unsafe.Pointer(p)).FpEntry = pEntry } (*TRowSet)(unsafe.Pointer(p)).FpLast = pEntry } // C documentation // // /* // ** Merge two lists of RowSetEntry objects. Remove duplicates. // ** // ** The input lists are connected via pRight pointers and are // ** assumed to each already be in sorted order. // */ func _rowSetEntryMerge(tls *libc.TLS, pA uintptr, pB uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var pTail, v2, v3 uintptr var _ /* head at bp+0 */ TRowSetEntry _, _, _ = pTail, v2, v3 pTail = bp _ = libc.Int32FromInt32(0) for { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TRowSetEntry)(unsafe.Pointer(pA)).Fv <= (*TRowSetEntry)(unsafe.Pointer(pB)).Fv { if (*TRowSetEntry)(unsafe.Pointer(pA)).Fv < (*TRowSetEntry)(unsafe.Pointer(pB)).Fv { v2 = pA (*TRowSetEntry)(unsafe.Pointer(pTail)).FpRight = v2 pTail = v2 } pA = (*TRowSetEntry)(unsafe.Pointer(pA)).FpRight if pA == uintptr(0) { (*TRowSetEntry)(unsafe.Pointer(pTail)).FpRight = pB break } } else { v3 = pB (*TRowSetEntry)(unsafe.Pointer(pTail)).FpRight = v3 pTail = v3 pB = (*TRowSetEntry)(unsafe.Pointer(pB)).FpRight if pB == uintptr(0) { (*TRowSetEntry)(unsafe.Pointer(pTail)).FpRight = pA break } } goto _1 _1: } return (*(*TRowSetEntry)(unsafe.Pointer(bp))).FpRight } // C documentation // // /* // ** Sort all elements on the list of RowSetEntry objects into order of // ** increasing v. // */ func _rowSetEntrySort(tls *libc.TLS, pIn uintptr) (r uintptr) { bp := tls.Alloc(160) defer tls.Free(160) var i uint32 var pNext, v3 uintptr var _ /* aBucket at bp+0 */ [40]uintptr _, _, _ = i, pNext, v3 libc.Xmemset(tls, bp, 0, uint32(160)) for pIn != 0 { pNext = (*TRowSetEntry)(unsafe.Pointer(pIn)).FpRight (*TRowSetEntry)(unsafe.Pointer(pIn)).FpRight = uintptr(0) i = uint32(0) for { if !((*(*[40]uintptr)(unsafe.Pointer(bp)))[i] != 0) { break } pIn = _rowSetEntryMerge(tls, (*(*[40]uintptr)(unsafe.Pointer(bp)))[i], pIn) (*(*[40]uintptr)(unsafe.Pointer(bp)))[i] = uintptr(0) goto _1 _1: ; i++ } (*(*[40]uintptr)(unsafe.Pointer(bp)))[i] = pIn pIn = pNext } pIn = (*(*[40]uintptr)(unsafe.Pointer(bp)))[0] i = uint32(1) for { if !(i < libc.Uint32FromInt64(160)/libc.Uint32FromInt64(4)) { break } if (*(*[40]uintptr)(unsafe.Pointer(bp)))[i] == uintptr(0) { goto _2 } if pIn != 0 { v3 = _rowSetEntryMerge(tls, pIn, (*(*[40]uintptr)(unsafe.Pointer(bp)))[i]) } else { v3 = (*(*[40]uintptr)(unsafe.Pointer(bp)))[i] } pIn = v3 goto _2 _2: ; i++ } return pIn } // C documentation // // /* // ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects. // ** Convert this tree into a linked list connected by the pRight pointers // ** and return pointers to the first and last elements of the new list. // */ func _rowSetTreeToList(tls *libc.TLS, pIn uintptr, ppFirst uintptr, ppLast uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* p at bp+0 */ uintptr _ = libc.Int32FromInt32(0) if (*TRowSetEntry)(unsafe.Pointer(pIn)).FpLeft != 0 { _rowSetTreeToList(tls, (*TRowSetEntry)(unsafe.Pointer(pIn)).FpLeft, ppFirst, bp) (*TRowSetEntry)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpRight = pIn } else { *(*uintptr)(unsafe.Pointer(ppFirst)) = pIn } if (*TRowSetEntry)(unsafe.Pointer(pIn)).FpRight != 0 { _rowSetTreeToList(tls, (*TRowSetEntry)(unsafe.Pointer(pIn)).FpRight, pIn+8, ppLast) } else { *(*uintptr)(unsafe.Pointer(ppLast)) = pIn } _ = libc.Int32FromInt32(0) } // C documentation // // /* // ** Convert a sorted list of elements (connected by pRight) into a binary // ** tree with depth of iDepth. A depth of 1 means the tree contains a single // ** node taken from the head of *ppList. A depth of 2 means a tree with // ** three nodes. And so forth. // ** // ** Use as many entries from the input list as required and update the // ** *ppList to point to the unused elements of the list. If the input // ** list contains too few elements, then construct an incomplete tree // ** and leave *ppList set to NULL. // ** // ** Return a pointer to the root of the constructed binary tree. // */ func _rowSetNDeepTree(tls *libc.TLS, ppList uintptr, iDepth int32) (r uintptr) { var p, pLeft, v1 uintptr _, _, _ = p, pLeft, v1 /* Left subtree */ if *(*uintptr)(unsafe.Pointer(ppList)) == uintptr(0) { /*OPTIMIZATION-IF-TRUE*/ /* Prevent unnecessary deep recursion when we run out of entries */ return uintptr(0) } if iDepth > int32(1) { /*OPTIMIZATION-IF-TRUE*/ /* This branch causes a *balanced* tree to be generated. A valid tree ** is still generated without this branch, but the tree is wildly ** unbalanced and inefficient. */ pLeft = _rowSetNDeepTree(tls, ppList, iDepth-int32(1)) p = *(*uintptr)(unsafe.Pointer(ppList)) if p == uintptr(0) { /*OPTIMIZATION-IF-FALSE*/ /* It is safe to always return here, but the resulting tree ** would be unbalanced */ return pLeft } (*TRowSetEntry)(unsafe.Pointer(p)).FpLeft = pLeft *(*uintptr)(unsafe.Pointer(ppList)) = (*TRowSetEntry)(unsafe.Pointer(p)).FpRight (*TRowSetEntry)(unsafe.Pointer(p)).FpRight = _rowSetNDeepTree(tls, ppList, iDepth-int32(1)) } else { p = *(*uintptr)(unsafe.Pointer(ppList)) *(*uintptr)(unsafe.Pointer(ppList)) = (*TRowSetEntry)(unsafe.Pointer(p)).FpRight v1 = libc.UintptrFromInt32(0) (*TRowSetEntry)(unsafe.Pointer(p)).FpRight = v1 (*TRowSetEntry)(unsafe.Pointer(p)).FpLeft = v1 } return p } // C documentation // // /* // ** Convert a sorted list of elements into a binary tree. Make the tree // ** as deep as it needs to be in order to contain the entire list. // */ func _rowSetListToTree(tls *libc.TLS, _pList uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) *(*uintptr)(unsafe.Pointer(bp)) = _pList var iDepth int32 var p, pLeft, v1 uintptr _, _, _, _ = iDepth, p, pLeft, v1 /* Left subtree */ _ = libc.Int32FromInt32(0) p = *(*uintptr)(unsafe.Pointer(bp)) *(*uintptr)(unsafe.Pointer(bp)) = (*TRowSetEntry)(unsafe.Pointer(p)).FpRight v1 = libc.UintptrFromInt32(0) (*TRowSetEntry)(unsafe.Pointer(p)).FpRight = v1 (*TRowSetEntry)(unsafe.Pointer(p)).FpLeft = v1 iDepth = int32(1) for { if !(*(*uintptr)(unsafe.Pointer(bp)) != 0) { break } pLeft = p p = *(*uintptr)(unsafe.Pointer(bp)) *(*uintptr)(unsafe.Pointer(bp)) = (*TRowSetEntry)(unsafe.Pointer(p)).FpRight (*TRowSetEntry)(unsafe.Pointer(p)).FpLeft = pLeft (*TRowSetEntry)(unsafe.Pointer(p)).FpRight = _rowSetNDeepTree(tls, bp, iDepth) goto _2 _2: ; iDepth++ } return p } // C documentation // // /* // ** Extract the smallest element from the RowSet. // ** Write the element into *pRowid. Return 1 on success. Return // ** 0 if the RowSet is already empty. // ** // ** After this routine has been called, the sqlite3RowSetInsert() // ** routine may not be called again. // ** // ** This routine may not be called after sqlite3RowSetTest() has // ** been used. Older versions of RowSet allowed that, but as the // ** capability was not used by the code generator, it was removed // ** for code economy. // */ func _sqlite3RowSetNext(tls *libc.TLS, p uintptr, pRowid uintptr) (r int32) { var p1 uintptr _ = p1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Cannot be used with sqlite3RowSetText() */ /* Merge the forest into a single sorted list on first call */ if int32((*TRowSet)(unsafe.Pointer(p)).FrsFlags)&int32(ROWSET_NEXT) == 0 { /*OPTIMIZATION-IF-FALSE*/ if int32((*TRowSet)(unsafe.Pointer(p)).FrsFlags)&int32(ROWSET_SORTED) == 0 { /*OPTIMIZATION-IF-FALSE*/ (*TRowSet)(unsafe.Pointer(p)).FpEntry = _rowSetEntrySort(tls, (*TRowSet)(unsafe.Pointer(p)).FpEntry) } p1 = p + 26 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | (libc.Int32FromInt32(ROWSET_SORTED) | libc.Int32FromInt32(ROWSET_NEXT))) } /* Return the next entry on the list */ if (*TRowSet)(unsafe.Pointer(p)).FpEntry != 0 { *(*Ti64)(unsafe.Pointer(pRowid)) = (*TRowSetEntry)(unsafe.Pointer((*TRowSet)(unsafe.Pointer(p)).FpEntry)).Fv (*TRowSet)(unsafe.Pointer(p)).FpEntry = (*TRowSetEntry)(unsafe.Pointer((*TRowSet)(unsafe.Pointer(p)).FpEntry)).FpRight if (*TRowSet)(unsafe.Pointer(p)).FpEntry == uintptr(0) { /*OPTIMIZATION-IF-TRUE*/ /* Free memory immediately, rather than waiting on sqlite3_finalize() */ _sqlite3RowSetClear(tls, p) } return int32(1) } else { return 0 } return r } // C documentation // // /* // ** Check to see if element iRowid was inserted into the rowset as // ** part of any insert batch prior to iBatch. Return 1 or 0. // ** // ** If this is the first test of a new batch and if there exist entries // ** on pRowSet->pEntry, then sort those entries into the forest at // ** pRowSet->pForest so that they can be tested. // */ func _sqlite3RowSetTest(tls *libc.TLS, pRowSet uintptr, iBatch int32, iRowid Tsqlite3_int64) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var p, pTree, ppPrevTree, v2, p3 uintptr var _ /* pAux at bp+0 */ uintptr var _ /* pTail at bp+4 */ uintptr _, _, _, _, _ = p, pTree, ppPrevTree, v2, p3 /* This routine is never called after sqlite3RowSetNext() */ _ = libc.Int32FromInt32(0) /* Sort entries into the forest on the first test of a new batch. ** To save unnecessary work, only do this when the batch number changes. */ if iBatch != (*TRowSet)(unsafe.Pointer(pRowSet)).FiBatch { /*OPTIMIZATION-IF-FALSE*/ p = (*TRowSet)(unsafe.Pointer(pRowSet)).FpEntry if p != 0 { ppPrevTree = pRowSet + 20 if int32((*TRowSet)(unsafe.Pointer(pRowSet)).FrsFlags)&int32(ROWSET_SORTED) == 0 { /*OPTIMIZATION-IF-FALSE*/ /* Only sort the current set of entries if they need it */ p = _rowSetEntrySort(tls, p) } pTree = (*TRowSet)(unsafe.Pointer(pRowSet)).FpForest for { if !(pTree != 0) { break } ppPrevTree = pTree + 8 if (*TRowSetEntry)(unsafe.Pointer(pTree)).FpLeft == uintptr(0) { (*TRowSetEntry)(unsafe.Pointer(pTree)).FpLeft = _rowSetListToTree(tls, p) break } else { _rowSetTreeToList(tls, (*TRowSetEntry)(unsafe.Pointer(pTree)).FpLeft, bp, bp+4) (*TRowSetEntry)(unsafe.Pointer(pTree)).FpLeft = uintptr(0) p = _rowSetEntryMerge(tls, *(*uintptr)(unsafe.Pointer(bp)), p) } goto _1 _1: ; pTree = (*TRowSetEntry)(unsafe.Pointer(pTree)).FpRight } if pTree == uintptr(0) { v2 = _rowSetEntryAlloc(tls, pRowSet) pTree = v2 *(*uintptr)(unsafe.Pointer(ppPrevTree)) = v2 if pTree != 0 { (*TRowSetEntry)(unsafe.Pointer(pTree)).Fv = 0 (*TRowSetEntry)(unsafe.Pointer(pTree)).FpRight = uintptr(0) (*TRowSetEntry)(unsafe.Pointer(pTree)).FpLeft = _rowSetListToTree(tls, p) } } (*TRowSet)(unsafe.Pointer(pRowSet)).FpEntry = uintptr(0) (*TRowSet)(unsafe.Pointer(pRowSet)).FpLast = uintptr(0) p3 = pRowSet + 26 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) | libc.Int32FromInt32(ROWSET_SORTED)) } (*TRowSet)(unsafe.Pointer(pRowSet)).FiBatch = iBatch } /* Test to see if the iRowid value appears anywhere in the forest. ** Return 1 if it does and 0 if not. */ pTree = (*TRowSet)(unsafe.Pointer(pRowSet)).FpForest for { if !(pTree != 0) { break } p = (*TRowSetEntry)(unsafe.Pointer(pTree)).FpLeft for p != 0 { if (*TRowSetEntry)(unsafe.Pointer(p)).Fv < iRowid { p = (*TRowSetEntry)(unsafe.Pointer(p)).FpRight } else { if (*TRowSetEntry)(unsafe.Pointer(p)).Fv > iRowid { p = (*TRowSetEntry)(unsafe.Pointer(p)).FpLeft } else { return int32(1) } } } goto _4 _4: ; pTree = (*TRowSetEntry)(unsafe.Pointer(pTree)).FpRight } return 0 } /************** End of rowset.c **********************************************/ /************** Begin file pager.c *******************************************/ /* ** 2001 September 15 ** ** 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 is the implementation of the page cache subsystem or "pager". ** ** The pager is used to access a database disk file. It implements ** atomic commit and rollback through the use of a journal file that ** is separate from the database file. The pager also implements file ** locking to prevent two processes from writing the same database ** file simultaneously, or one process from reading the database while ** another is writing. */ /* #include "sqliteInt.h" */ /************** Include wal.h in the middle of pager.c ***********************/ /************** Begin file wal.h *********************************************/ /* ** 2010 February 1 ** ** 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 header file defines the interface to the write-ahead logging ** system. Refer to the comments below and the header comment attached to ** the implementation of each function in log.c for further details. */ /* #include "sqliteInt.h" */ /* Macros for extracting appropriate sync flags for either transaction ** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)): */ // C documentation // // /* Connection to a write-ahead log (WAL) file. // ** There is one object of this type for each pager. // */ type TWal = struct { F__ccgo_align [0]uint32 FpVfs uintptr FpDbFd uintptr FpWalFd uintptr FiCallback Tu32 FmxWalSize Ti64 FnWiData int32 FszFirstBlock int32 FapWiData uintptr FszPage Tu32 FreadLock Ti16 FsyncFlags Tu8 FexclusiveMode Tu8 FwriteLock Tu8 FckptLock Tu8 FreadOnly Tu8 FtruncateOnCommit Tu8 FsyncHeader Tu8 FpadToSectorBoundary Tu8 FbShmUnreliable Tu8 Fhdr TWalIndexHdr FminFrame Tu32 FiReCksum Tu32 FzWalName uintptr FnCkpt Tu32 FpSnapshot uintptr } type Wal = TWal /************** End of wal.h *************************************************/ /************** Continuing where we left off in pager.c **********************/ /******************* NOTES ON THE DESIGN OF THE PAGER ************************ ** ** This comment block describes invariants that hold when using a rollback ** journal. These invariants do not apply for journal_mode=WAL, ** journal_mode=MEMORY, or journal_mode=OFF. ** ** Within this comment block, a page is deemed to have been synced ** automatically as soon as it is written when PRAGMA synchronous=OFF. ** Otherwise, the page is not synced until the xSync method of the VFS ** is called successfully on the file containing the page. ** ** Definition: A page of the database file is said to be "overwriteable" if ** one or more of the following are true about the page: ** ** (a) The original content of the page as it was at the beginning of ** the transaction has been written into the rollback journal and ** synced. ** ** (b) The page was a freelist leaf page at the start of the transaction. ** ** (c) The page number is greater than the largest page that existed in ** the database file at the start of the transaction. ** ** (1) A page of the database file is never overwritten unless one of the ** following are true: ** ** (a) The page and all other pages on the same sector are overwriteable. ** ** (b) The atomic page write optimization is enabled, and the entire ** transaction other than the update of the transaction sequence ** number consists of a single page change. ** ** (2) The content of a page written into the rollback journal exactly matches ** both the content in the database when the rollback journal was written ** and the content in the database at the beginning of the current ** transaction. ** ** (3) Writes to the database file are an integer multiple of the page size ** in length and are aligned on a page boundary. ** ** (4) Reads from the database file are either aligned on a page boundary and ** an integer multiple of the page size in length or are taken from the ** first 100 bytes of the database file. ** ** (5) All writes to the database file are synced prior to the rollback journal ** being deleted, truncated, or zeroed. ** ** (6) If a super-journal file is used, then all writes to the database file ** are synced prior to the super-journal being deleted. ** ** Definition: Two databases (or the same database at two points it time) ** are said to be "logically equivalent" if they give the same answer to ** all queries. Note in particular the content of freelist leaf ** pages can be changed arbitrarily without affecting the logical equivalence ** of the database. ** ** (7) At any time, if any subset, including the empty set and the total set, ** of the unsynced changes to a rollback journal are removed and the ** journal is rolled back, the resulting database file will be logically ** equivalent to the database file at the beginning of the transaction. ** ** (8) When a transaction is rolled back, the xTruncate method of the VFS ** is called to restore the database file to the same size it was at ** the beginning of the transaction. (In some VFSes, the xTruncate ** method is a no-op, but that does not change the fact the SQLite will ** invoke it.) ** ** (9) Whenever the database file is modified, at least one bit in the range ** of bytes from 24 through 39 inclusive will be changed prior to releasing ** the EXCLUSIVE lock, thus signaling other connections on the same ** database to flush their caches. ** ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less ** than one billion transactions. ** ** (11) A database file is well-formed at the beginning and at the conclusion ** of every transaction. ** ** (12) An EXCLUSIVE lock is held on the database file when writing to ** the database file. ** ** (13) A SHARED lock is held on the database file while reading any ** content out of the database file. ** ******************************************************************************/ /* ** Macros for troubleshooting. Normally turned off */ /* ** The following two macros are used within the PAGERTRACE() macros above ** to print out file-descriptors. ** ** PAGERID() takes a pointer to a Pager struct as its argument. The ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file ** struct as its argument. */ /* ** The Pager.eState variable stores the current 'state' of a pager. A ** pager may be in any one of the seven states shown in the following ** state diagram. ** ** OPEN <------+------+ ** | | | ** V | | ** +---------> READER-------+ | ** | | | ** | V | ** |<-------WRITER_LOCKED------> ERROR ** | | ^ ** | V | ** |<------WRITER_CACHEMOD-------->| ** | | | ** | V | ** |<-------WRITER_DBMOD---------->| ** | | | ** | V | ** +<------WRITER_FINISHED-------->+ ** ** ** List of state transitions and the C [function] that performs each: ** ** OPEN -> READER [sqlite3PagerSharedLock] ** READER -> OPEN [pager_unlock] ** ** READER -> WRITER_LOCKED [sqlite3PagerBegin] ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal] ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal] ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne] ** WRITER_*** -> READER [pager_end_transaction] ** ** WRITER_*** -> ERROR [pager_error] ** ERROR -> OPEN [pager_unlock] ** ** ** OPEN: ** ** The pager starts up in this state. Nothing is guaranteed in this ** state - the file may or may not be locked and the database size is ** unknown. The database may not be read or written. ** ** * No read or write transaction is active. ** * Any lock, or no lock at all, may be held on the database file. ** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted. ** ** READER: ** ** In this state all the requirements for reading the database in ** rollback (non-WAL) mode are met. Unless the pager is (or recently ** was) in exclusive-locking mode, a user-level read transaction is ** open. The database size is known in this state. ** ** A connection running with locking_mode=normal enters this state when ** it opens a read-transaction on the database and returns to state ** OPEN after the read-transaction is completed. However a connection ** running in locking_mode=exclusive (including temp databases) remains in ** this state even after the read-transaction is closed. The only way ** a locking_mode=exclusive connection can transition from READER to OPEN ** is via the ERROR state (see below). ** ** * A read transaction may be active (but a write-transaction cannot). ** * A SHARED or greater lock is held on the database file. ** * The dbSize variable may be trusted (even if a user-level read ** transaction is not active). The dbOrigSize and dbFileSize variables ** may not be trusted at this point. ** * If the database is a WAL database, then the WAL connection is open. ** * Even if a read-transaction is not open, it is guaranteed that ** there is no hot-journal in the file-system. ** ** WRITER_LOCKED: ** ** The pager moves to this state from READER when a write-transaction ** is first opened on the database. In WRITER_LOCKED state, all locks ** required to start a write-transaction are held, but no actual ** modifications to the cache or database have taken place. ** ** In rollback mode, a RESERVED or (if the transaction was opened with ** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when ** moving to this state, but the journal file is not written to or opened ** to in this state. If the transaction is committed or rolled back while ** in WRITER_LOCKED state, all that is required is to unlock the database ** file. ** ** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file. ** If the connection is running with locking_mode=exclusive, an attempt ** is made to obtain an EXCLUSIVE lock on the database file. ** ** * A write transaction is active. ** * If the connection is open in rollback-mode, a RESERVED or greater ** lock is held on the database file. ** * If the connection is open in WAL-mode, a WAL write transaction ** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully ** called). ** * The dbSize, dbOrigSize and dbFileSize variables are all valid. ** * The contents of the pager cache have not been modified. ** * The journal file may or may not be open. ** * Nothing (not even the first header) has been written to the journal. ** ** WRITER_CACHEMOD: ** ** A pager moves from WRITER_LOCKED state to this state when a page is ** first modified by the upper layer. In rollback mode the journal file ** is opened (if it is not already open) and a header written to the ** start of it. The database file on disk has not been modified. ** ** * A write transaction is active. ** * A RESERVED or greater lock is held on the database file. ** * The journal file is open and the first header has been written ** to it, but the header has not been synced to disk. ** * The contents of the page cache have been modified. ** ** WRITER_DBMOD: ** ** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state ** when it modifies the contents of the database file. WAL connections ** never enter this state (since they do not modify the database file, ** just the log file). ** ** * A write transaction is active. ** * An EXCLUSIVE or greater lock is held on the database file. ** * The journal file is open and the first header has been written ** and synced to disk. ** * The contents of the page cache have been modified (and possibly ** written to disk). ** ** WRITER_FINISHED: ** ** It is not possible for a WAL connection to enter this state. ** ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD ** state after the entire transaction has been successfully written into the ** database file. In this state the transaction may be committed simply ** by finalizing the journal file. Once in WRITER_FINISHED state, it is ** not possible to modify the database further. At this point, the upper ** layer must either commit or rollback the transaction. ** ** * A write transaction is active. ** * An EXCLUSIVE or greater lock is held on the database file. ** * All writing and syncing of journal and database data has finished. ** If no error occurred, all that remains is to finalize the journal to ** commit the transaction. If an error did occur, the caller will need ** to rollback the transaction. ** ** ERROR: ** ** The ERROR state is entered when an IO or disk-full error (including ** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it ** difficult to be sure that the in-memory pager state (cache contents, ** db size etc.) are consistent with the contents of the file-system. ** ** Temporary pager files may enter the ERROR state, but in-memory pagers ** cannot. ** ** For example, if an IO error occurs while performing a rollback, ** the contents of the page-cache may be left in an inconsistent state. ** At this point it would be dangerous to change back to READER state ** (as usually happens after a rollback). Any subsequent readers might ** report database corruption (due to the inconsistent cache), and if ** they upgrade to writers, they may inadvertently corrupt the database ** file. To avoid this hazard, the pager switches into the ERROR state ** instead of READER following such an error. ** ** Once it has entered the ERROR state, any attempt to use the pager ** to read or write data returns an error. Eventually, once all ** outstanding transactions have been abandoned, the pager is able to ** transition back to OPEN state, discarding the contents of the ** page-cache and any other in-memory state at the same time. Everything ** is reloaded from disk (and, if necessary, hot-journal rollback performed) ** when a read-transaction is next opened on the pager (transitioning ** the pager into READER state). At that point the system has recovered ** from the error. ** ** Specifically, the pager jumps into the ERROR state if: ** ** 1. An error occurs while attempting a rollback. This happens in ** function sqlite3PagerRollback(). ** ** 2. An error occurs while attempting to finalize a journal file ** following a commit in function sqlite3PagerCommitPhaseTwo(). ** ** 3. An error occurs while attempting to write to the journal or ** database file in function pagerStress() in order to free up ** memory. ** ** In other cases, the error is returned to the b-tree layer. The b-tree ** layer then attempts a rollback operation. If the error condition ** persists, the pager enters the ERROR state via condition (1) above. ** ** Condition (3) is necessary because it can be triggered by a read-only ** statement executed within a transaction. In this case, if the error ** code were simply returned to the user, the b-tree layer would not ** automatically attempt a rollback, as it assumes that an error in a ** read-only statement cannot leave the pager in an internally inconsistent ** state. ** ** * The Pager.errCode variable is set to something other than SQLITE_OK. ** * There are one or more outstanding references to pages (after the ** last reference is dropped the pager should move back to OPEN state). ** * The pager is not an in-memory pager. ** ** ** Notes: ** ** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the ** connection is open in WAL mode. A WAL connection is always in one ** of the first four states. ** ** * Normally, a connection open in exclusive mode is never in PAGER_OPEN ** state. There are two exceptions: immediately after exclusive-mode has ** been turned on (and before any read or write transactions are ** executed), and when the pager is leaving the "error state". ** ** * See also: assert_pager_state(). */ /* ** The Pager.eLock variable is almost always set to one of the ** following locking-states, according to the lock currently held on ** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK. ** This variable is kept up to date as locks are taken and released by ** the pagerLockDb() and pagerUnlockDb() wrappers. ** ** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY ** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not ** the operation was successful. In these circumstances pagerLockDb() and ** pagerUnlockDb() take a conservative approach - eLock is always updated ** when unlocking the file, and only updated when locking the file if the ** VFS call is successful. This way, the Pager.eLock variable may be set ** to a less exclusive (lower) value than the lock that is actually held ** at the system level, but it is never set to a more exclusive value. ** ** This is usually safe. If an xUnlock fails or appears to fail, there may ** be a few redundant xLock() calls or a lock may be held for longer than ** required, but nothing really goes wrong. ** ** The exception is when the database file is unlocked as the pager moves ** from ERROR to OPEN state. At this point there may be a hot-journal file ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED ** transition, by the same pager or any other). If the call to xUnlock() ** fails at this point and the pager is left holding an EXCLUSIVE lock, this ** can confuse the call to xCheckReservedLock() call made later as part ** of hot-journal detection. ** ** xCheckReservedLock() is defined as returning true "if there is a RESERVED ** lock held by this process or any others". So xCheckReservedLock may ** return true because the caller itself is holding an EXCLUSIVE lock (but ** doesn't know it because of a previous error in xUnlock). If this happens ** a hot-journal may be mistaken for a journal being created by an active ** transaction in another process, causing SQLite to read from the database ** without rolling it back. ** ** To work around this, if a call to xUnlock() fails when unlocking the ** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It ** is only changed back to a real locking state after a successful call ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE ** lock on the database file before attempting to roll it back. See function ** PagerSharedLock() for more detail. ** ** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in ** PAGER_OPEN state. */ /* ** The maximum allowed sector size. 64KiB. If the xSectorsize() method ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead. ** This could conceivably cause corruption following a power failure on ** such a system. This is currently an undocumented limit. */ // C documentation // // /* // ** An instance of the following structure is allocated for each active // ** savepoint and statement transaction in the system. All such structures // ** are stored in the Pager.aSavepoint[] array, which is allocated and // ** resized using sqlite3Realloc(). // ** // ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is // ** set to 0. If a journal-header is written into the main journal while // ** the savepoint is active, then iHdrOffset is set to the byte offset // ** immediately following the last journal record written into the main // ** journal before the journal-header. This is required during savepoint // ** rollback (see pagerPlaybackSavepoint()). // */ type TPagerSavepoint = struct { F__ccgo_align [0]uint32 FiOffset Ti64 FiHdrOffset Ti64 FpInSavepoint uintptr FnOrig TPgno FiSubRec TPgno FbTruncateOnRelease int32 FaWalData [4]Tu32 } type PagerSavepoint = TPagerSavepoint type TPagerSavepoint1 = struct { F__ccgo_align [0]uint32 FiOffset Ti64 FiHdrOffset Ti64 FpInSavepoint uintptr FnOrig TPgno FiSubRec TPgno FbTruncateOnRelease int32 FaWalData [4]Tu32 } type PagerSavepoint1 = TPagerSavepoint1 /* ** Bits of the Pager.doNotSpill flag. See further description below. */ /* ** An open page cache is an instance of struct Pager. A description of ** some of the more important member variables follows: ** ** eState ** ** The current 'state' of the pager object. See the comment and state ** diagram above for a description of the pager state. ** ** eLock ** ** For a real on-disk database, the current lock held on the database file - ** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK. ** ** For a temporary or in-memory database (neither of which require any ** locks), this variable is always set to EXCLUSIVE_LOCK. Since such ** databases always have Pager.exclusiveMode==1, this tricks the pager ** logic into thinking that it already has all the locks it will ever ** need (and no reason to release them). ** ** In some (obscure) circumstances, this variable may also be set to ** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for ** details. ** ** changeCountDone ** ** This boolean variable is used to make sure that the change-counter ** (the 4-byte header field at byte offset 24 of the database file) is ** not updated more often than necessary. ** ** It is set to true when the change-counter field is updated, which ** can only happen if an exclusive lock is held on the database file. ** It is cleared (set to false) whenever an exclusive lock is ** relinquished on the database file. Each time a transaction is committed, ** The changeCountDone flag is inspected. If it is true, the work of ** updating the change-counter is omitted for the current transaction. ** ** This mechanism means that when running in exclusive mode, a connection ** need only update the change-counter once, for the first transaction ** committed. ** ** setSuper ** ** When PagerCommitPhaseOne() is called to commit a transaction, it may ** (or may not) specify a super-journal name to be written into the ** journal file before it is synced to disk. ** ** Whether or not a journal file contains a super-journal pointer affects ** the way in which the journal file is finalized after the transaction is ** committed or rolled back when running in "journal_mode=PERSIST" mode. ** If a journal file does not contain a super-journal pointer, it is ** finalized by overwriting the first journal header with zeroes. If ** it does contain a super-journal pointer the journal file is finalized ** by truncating it to zero bytes, just as if the connection were ** running in "journal_mode=truncate" mode. ** ** Journal files that contain super-journal pointers cannot be finalized ** simply by overwriting the first journal-header with zeroes, as the ** super-journal pointer could interfere with hot-journal rollback of any ** subsequently interrupted transaction that reuses the journal file. ** ** The flag is cleared as soon as the journal file is finalized (either ** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the ** journal file from being successfully finalized, the setSuper flag ** is cleared anyway (and the pager will move to ERROR state). ** ** doNotSpill ** ** This variables control the behavior of cache-spills (calls made by ** the pcache module to the pagerStress() routine to write cached data ** to the file-system in order to free up memory). ** ** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set, ** writing to the database from pagerStress() is disabled altogether. ** The SPILLFLAG_ROLLBACK case is done in a very obscure case that ** comes up during savepoint rollback that requires the pcache module ** to allocate a new page to prevent the journal file from being written ** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF ** case is a user preference. ** ** If the SPILLFLAG_NOSYNC bit is set, writing to the database from ** pagerStress() is permitted, but syncing the journal file is not. ** This flag is set by sqlite3PagerWrite() when the file-system sector-size ** is larger than the database page-size in order to prevent a journal sync ** from happening in between the journalling of two pages on the same sector. ** ** subjInMemory ** ** This is a boolean variable. If true, then any required sub-journal ** is opened as an in-memory journal file. If false, then in-memory ** sub-journals are only used for in-memory pager files. ** ** This variable is updated by the upper layer each time a new ** write-transaction is opened. ** ** dbSize, dbOrigSize, dbFileSize ** ** Variable dbSize is set to the number of pages in the database file. ** It is valid in PAGER_READER and higher states (all states except for ** OPEN and ERROR). ** ** dbSize is set based on the size of the database file, which may be ** larger than the size of the database (the value stored at offset ** 28 of the database header by the btree). If the size of the file ** is not an integer multiple of the page-size, the value stored in ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2). ** Except, any file that is greater than 0 bytes in size is considered ** to have at least one page. (i.e. a 1KB file with 2K page-size leads ** to dbSize==1). ** ** During a write-transaction, if pages with page-numbers greater than ** dbSize are modified in the cache, dbSize is updated accordingly. ** Similarly, if the database is truncated using PagerTruncateImage(), ** dbSize is updated. ** ** Variables dbOrigSize and dbFileSize are valid in states ** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize ** variable at the start of the transaction. It is used during rollback, ** and to determine whether or not pages need to be journalled before ** being modified. ** ** Throughout a write-transaction, dbFileSize contains the size of ** the file on disk in pages. It is set to a copy of dbSize when the ** write-transaction is first opened, and updated when VFS calls are made ** to write or truncate the database file on disk. ** ** The only reason the dbFileSize variable is required is to suppress ** unnecessary calls to xTruncate() after committing a transaction. If, ** when a transaction is committed, the dbFileSize variable indicates ** that the database file is larger than the database image (Pager.dbSize), ** pager_truncate() is called. The pager_truncate() call uses xFilesize() ** to measure the database file on disk, and then truncates it if required. ** dbFileSize is not used when rolling back a transaction. In this case ** pager_truncate() is called unconditionally (which means there may be ** a call to xFilesize() that is not strictly required). In either case, ** pager_truncate() may cause the file to become smaller or larger. ** ** dbHintSize ** ** The dbHintSize variable is used to limit the number of calls made to ** the VFS xFileControl(FCNTL_SIZE_HINT) method. ** ** dbHintSize is set to a copy of the dbSize variable when a ** write-transaction is opened (at the same time as dbFileSize and ** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called, ** dbHintSize is increased to the number of pages that correspond to the ** size-hint passed to the method call. See pager_write_pagelist() for ** details. ** ** errCode ** ** The Pager.errCode variable is only ever used in PAGER_ERROR state. It ** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode ** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX ** sub-codes. ** ** syncFlags, walSyncFlags ** ** syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03). ** syncFlags is used for rollback mode. walSyncFlags is used for WAL mode ** and contains the flags used to sync the checkpoint operations in the ** lower two bits, and sync flags used for transaction commits in the WAL ** file in bits 0x04 and 0x08. In other words, to get the correct sync flags ** for checkpoint operations, use (walSyncFlags&0x03) and to get the correct ** sync flags for transaction commit, use ((walSyncFlags>>2)&0x03). Note ** that with synchronous=NORMAL in WAL mode, transaction commit is not synced ** meaning that the 0x04 and 0x08 bits are both zero. */ type TPager1 = struct { F__ccgo_align [0]uint32 FpVfs uintptr FexclusiveMode Tu8 FjournalMode Tu8 FuseJournal Tu8 FnoSync Tu8 FfullSync Tu8 FextraSync Tu8 FsyncFlags Tu8 FwalSyncFlags Tu8 FtempFile Tu8 FnoLock Tu8 FreadOnly Tu8 FmemDb Tu8 FmemVfs Tu8 FeState Tu8 FeLock Tu8 FchangeCountDone Tu8 FsetSuper Tu8 FdoNotSpill Tu8 FsubjInMemory Tu8 FbUseFetch Tu8 FhasHeldSharedLock Tu8 FdbSize TPgno FdbOrigSize TPgno FdbFileSize TPgno FdbHintSize TPgno FerrCode int32 FnRec int32 FcksumInit Tu32 FnSubRec Tu32 FpInJournal uintptr Ffd uintptr Fjfd uintptr Fsjfd uintptr F__ccgo_align34 [4]byte FjournalOff Ti64 FjournalHdr Ti64 FpBackup uintptr FaSavepoint uintptr FnSavepoint int32 FiDataVersion Tu32 FdbFileVers [16]uint8 FnMmapOut int32 F__ccgo_align42 [4]byte FszMmap Tsqlite3_int64 FpMmapFreelist uintptr FnExtra Tu16 FnReserve Ti16 FvfsFlags Tu32 FsectorSize Tu32 FmxPgno TPgno FlckPgno TPgno FpageSize Ti64 FjournalSizeLimit Ti64 FzFilename uintptr FzJournal uintptr FxBusyHandler uintptr FpBusyHandlerArg uintptr FaStat [4]Tu32 FxReiniter uintptr FxGet uintptr FpTmpSpace uintptr FpPCache uintptr FpWal uintptr FzWal uintptr } type Pager1 = TPager1 /* ** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains ** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS ** or CACHE_WRITE to sqlite3_db_status(). */ /* ** The following global variables hold counters used for ** testing purposes only. These variables do not exist in ** a non-testing build. These variables are not thread-safe. */ // C documentation // // /* // ** Journal files begin with the following magic string. The data // ** was obtained from /dev/random. It is used only as a sanity check. // ** // ** Since version 2.8.0, the journal format contains additional sanity // ** checking information. If the power fails while the journal is being // ** written, semi-random garbage data might appear in the journal // ** file after power is restored. If an attempt is then made // ** to roll the journal back, the database could be corrupted. The additional // ** sanity checking data is an attempt to discover the garbage in the // ** journal and ignore it. // ** // ** The sanity checking information for the new journal format consists // ** of a 32-bit checksum on each page of data. The checksum covers both // ** the page number and the pPager->pageSize bytes of data for the page. // ** This cksum is initialized to a 32-bit random value that appears in the // ** journal file right after the header. The random initializer is important, // ** because garbage data that appears at the end of a journal is likely // ** data that was once in other files that have now been deleted. If the // ** garbage data came from an obsolete journal file, the checksums might // ** be correct. But by initializing the checksum to random value which // ** is different for every journal, we minimize that risk. // */ var _aJournalMagic = [8]uint8{ 0: uint8(0xd9), 1: uint8(0xd5), 2: uint8(0x05), 3: uint8(0xf9), 4: uint8(0x20), 5: uint8(0xa1), 6: uint8(0x63), 7: uint8(0xd7), } /* ** The size of the of each page record in the journal is given by ** the following macro. */ /* ** The journal header size for this pager. This is usually the same ** size as a single disk sector. See also setSectorSize(). */ /* ** The macro MEMDB is true if we are dealing with an in-memory database. ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set, ** the value of MEMDB will be a constant and the compiler will optimize ** out code that would never execute. */ /* ** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch ** interfaces to access the database using memory-mapped I/O. */ /* ** The argument to this macro is a file descriptor (type sqlite3_file*). ** Return 0 if it is not open, or non-zero (but not 1) if it is. ** ** This is so that expressions can be written as: ** ** if( isOpen(pPager->jfd) ){ ... ** ** instead of ** ** if( pPager->jfd->pMethods ){ ... */ // C documentation // // /* // ** Return true if page pgno can be read directly from the database file // ** by the b-tree layer. This is the case if: // ** // ** * the database file is open, // ** * there are no dirty pages in the cache, and // ** * the desired page is not currently in the wal file. // */ func _sqlite3PagerDirectReadOk(tls *libc.TLS, pPager uintptr, pgno TPgno) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* iRead at bp+0 */ Tu32 if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods == uintptr(0) { return 0 } if _sqlite3PCacheIsDirty(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) != 0 { return 0 } if (*TPager)(unsafe.Pointer(pPager)).FpWal != 0 { *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) _sqlite3WalFindFrame(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, pgno, bp) return libc.BoolInt32(*(*Tu32)(unsafe.Pointer(bp)) == uint32(0)) } return int32(1) } // C documentation // // /* // ** Set the Pager.xGet method for the appropriate routine used to fetch // ** content from the pager. // */ func _setGetterMethod(tls *libc.TLS, pPager uintptr) { if (*TPager)(unsafe.Pointer(pPager)).FerrCode != 0 { (*TPager)(unsafe.Pointer(pPager)).FxGet = __ccgo_fp(_getPageError) } else { if (*TPager)(unsafe.Pointer(pPager)).FbUseFetch != 0 { (*TPager)(unsafe.Pointer(pPager)).FxGet = __ccgo_fp(_getPageMMap) } else { (*TPager)(unsafe.Pointer(pPager)).FxGet = __ccgo_fp(_getPageNormal) } } } // C documentation // // /* // ** Return true if it is necessary to write page *pPg into the sub-journal. // ** A page needs to be written into the sub-journal if there exists one // ** or more open savepoints for which: // ** // ** * The page-number is less than or equal to PagerSavepoint.nOrig, and // ** * The bit corresponding to the page-number is not set in // ** PagerSavepoint.pInSavepoint. // */ func _subjRequiresPage(tls *libc.TLS, pPg uintptr) (r int32) { var i int32 var p, pPager uintptr var pgno TPgno _, _, _, _ = i, p, pPager, pgno pPager = (*TPgHdr)(unsafe.Pointer(pPg)).FpPager pgno = (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno i = 0 for { if !(i < (*TPager)(unsafe.Pointer(pPager)).FnSavepoint) { break } p = (*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(i)*48 if (*TPagerSavepoint)(unsafe.Pointer(p)).FnOrig >= pgno && 0 == _sqlite3BitvecTestNotNull(tls, (*TPagerSavepoint)(unsafe.Pointer(p)).FpInSavepoint, pgno) { i = i + int32(1) for { if !(i < (*TPager)(unsafe.Pointer(pPager)).FnSavepoint) { break } (*(*TPagerSavepoint)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(i)*48))).FbTruncateOnRelease = 0 goto _2 _2: ; i++ } return int32(1) } goto _1 _1: ; i++ } return 0 } // C documentation // // /* // ** Read a 32-bit integer from the given file descriptor. Store the integer // ** that is read in *pRes. Return SQLITE_OK if everything worked, or an // ** error code is something goes wrong. // ** // ** All values are stored on disk as big-endian. // */ func _read32bits(tls *libc.TLS, fd uintptr, offset Ti64, pRes uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* ac at bp+0 */ [4]uint8 _ = rc rc = _sqlite3OsRead(tls, fd, bp, int32(4), offset) if rc == SQLITE_OK { *(*Tu32)(unsafe.Pointer(pRes)) = _sqlite3Get4byte(tls, bp) } return rc } /* ** Write a 32-bit integer into a string buffer in big-endian byte order. */ // C documentation // // /* // ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK // ** on success or an error code is something goes wrong. // */ func _write32bits(tls *libc.TLS, fd uintptr, offset Ti64, val Tu32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* ac at bp+0 */ [4]uint8 _sqlite3Put4byte(tls, bp, val) return _sqlite3OsWrite(tls, fd, bp, int32(4), offset) } // C documentation // // /* // ** Unlock the database file to level eLock, which must be either NO_LOCK // ** or SHARED_LOCK. Regardless of whether or not the call to xUnlock() // ** succeeds, set the Pager.eLock variable to match the (attempted) new lock. // ** // ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is // ** called, do not modify it. See the comment above the #define of // ** UNKNOWN_LOCK for an explanation of this. // */ func _pagerUnlockDb(tls *libc.TLS, pPager uintptr, eLock int32) (r int32) { var rc, v1 int32 _, _ = rc, v1 rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != uintptr(0) { _ = libc.Int32FromInt32(0) if (*TPager)(unsafe.Pointer(pPager)).FnoLock != 0 { v1 = SQLITE_OK } else { v1 = _sqlite3OsUnlock(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, eLock) } rc = v1 if int32((*TPager)(unsafe.Pointer(pPager)).FeLock) != libc.Int32FromInt32(EXCLUSIVE_LOCK)+libc.Int32FromInt32(1) { (*TPager)(unsafe.Pointer(pPager)).FeLock = uint8(eLock) } } (*TPager)(unsafe.Pointer(pPager)).FchangeCountDone = (*TPager)(unsafe.Pointer(pPager)).FtempFile /* ticket fb3b3024ea238d5c */ return rc } // C documentation // // /* // ** Lock the database file to level eLock, which must be either SHARED_LOCK, // ** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the // ** Pager.eLock variable to the new locking state. // ** // ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is // ** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK. // ** See the comment above the #define of UNKNOWN_LOCK for an explanation // ** of this. // */ func _pagerLockDb(tls *libc.TLS, pPager uintptr, eLock int32) (r int32) { var rc, v1 int32 _, _ = rc, v1 rc = SQLITE_OK _ = libc.Int32FromInt32(0) if int32((*TPager)(unsafe.Pointer(pPager)).FeLock) < eLock || int32((*TPager)(unsafe.Pointer(pPager)).FeLock) == libc.Int32FromInt32(EXCLUSIVE_LOCK)+libc.Int32FromInt32(1) { if (*TPager)(unsafe.Pointer(pPager)).FnoLock != 0 { v1 = SQLITE_OK } else { v1 = _sqlite3OsLock(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, eLock) } rc = v1 if rc == SQLITE_OK && (int32((*TPager)(unsafe.Pointer(pPager)).FeLock) != libc.Int32FromInt32(EXCLUSIVE_LOCK)+libc.Int32FromInt32(1) || eLock == int32(EXCLUSIVE_LOCK)) { (*TPager)(unsafe.Pointer(pPager)).FeLock = uint8(eLock) } } return rc } // C documentation // // /* // ** This function determines whether or not the atomic-write or // ** atomic-batch-write optimizations can be used with this pager. The // ** atomic-write optimization can be used if: // ** // ** (a) the value returned by OsDeviceCharacteristics() indicates that // ** a database page may be written atomically, and // ** (b) the value returned by OsSectorSize() is less than or equal // ** to the page size. // ** // ** If it can be used, then the value returned is the size of the journal // ** file when it contains rollback data for exactly one page. // ** // ** The atomic-batch-write optimization can be used if OsDeviceCharacteristics() // ** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is // ** returned in this case. // ** // ** If neither optimization can be used, 0 is returned. // */ func _jrnlBufferSize(tls *libc.TLS, pPager uintptr) (r int32) { _ = libc.Int32FromInt32(0) _ = pPager return 0 } /* ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking ** on the cache using a hash function. This is used for testing ** and debugging only. */ // C documentation // // /* // ** When this is called the journal file for pager pPager must be open. // ** This function attempts to read a super-journal file name from the // ** end of the file and, if successful, copies it into memory supplied // ** by the caller. See comments above writeSuperJournal() for the format // ** used to store a super-journal file name at the end of a journal file. // ** // ** zSuper must point to a buffer of at least nSuper bytes allocated by // ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is // ** enough space to write the super-journal name). If the super-journal // ** name in the journal is longer than nSuper bytes (including a // ** nul-terminator), then this is handled as if no super-journal name // ** were present in the journal. // ** // ** If a super-journal file name is present at the end of the journal // ** file, then it is copied into the buffer pointed to by zSuper. A // ** nul-terminator byte is appended to the buffer following the // ** super-journal file name. // ** // ** If it is determined that no super-journal file name is present // ** zSuper[0] is set to 0 and SQLITE_OK returned. // ** // ** If an error occurs while reading from the journal file, an SQLite // ** error code is returned. // */ func _readSuperJournal(tls *libc.TLS, pJrnl uintptr, zSuper uintptr, nSuper Tu32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var rc, v1, v2, v4, v6, v8 int32 var u Tu32 var v3, v5, v7, v9 bool var _ /* aMagic at bp+24 */ [8]uint8 var _ /* cksum at bp+16 */ Tu32 var _ /* len at bp+0 */ Tu32 var _ /* szJ at bp+8 */ Ti64 _, _, _, _, _, _, _, _, _, _, _ = rc, u, v1, v2, v3, v4, v5, v6, v7, v8, v9 /* A buffer to hold the magic header */ *(*uint8)(unsafe.Pointer(zSuper)) = uint8('\000') v1 = _sqlite3OsFileSize(tls, pJrnl, bp+8) rc = v1 if v3 = SQLITE_OK != v1 || *(*Ti64)(unsafe.Pointer(bp + 8)) < int64(16); !v3 { v2 = _read32bits(tls, pJrnl, *(*Ti64)(unsafe.Pointer(bp + 8))-int64(16), bp) rc = v2 } if v5 = v3 || SQLITE_OK != v2 || *(*Tu32)(unsafe.Pointer(bp)) >= nSuper || int64(*(*Tu32)(unsafe.Pointer(bp))) > *(*Ti64)(unsafe.Pointer(bp + 8))-int64(16) || *(*Tu32)(unsafe.Pointer(bp)) == uint32(0); !v5 { v4 = _read32bits(tls, pJrnl, *(*Ti64)(unsafe.Pointer(bp + 8))-int64(12), bp+16) rc = v4 } if v7 = v5 || SQLITE_OK != v4; !v7 { v6 = _sqlite3OsRead(tls, pJrnl, bp+24, int32(8), *(*Ti64)(unsafe.Pointer(bp + 8))-int64(8)) rc = v6 } if v9 = v7 || SQLITE_OK != v6 || libc.Xmemcmp(tls, bp+24, uintptr(unsafe.Pointer(&_aJournalMagic)), uint32(8)) != 0; !v9 { v8 = _sqlite3OsRead(tls, pJrnl, zSuper, int32(*(*Tu32)(unsafe.Pointer(bp))), *(*Ti64)(unsafe.Pointer(bp + 8))-int64(16)-int64(*(*Tu32)(unsafe.Pointer(bp)))) rc = v8 } if v9 || SQLITE_OK != v8 { return rc } /* See if the checksum matches the super-journal name */ u = uint32(0) for { if !(u < *(*Tu32)(unsafe.Pointer(bp))) { break } *(*Tu32)(unsafe.Pointer(bp + 16)) -= uint32(*(*uint8)(unsafe.Pointer(zSuper + uintptr(u)))) goto _10 _10: ; u++ } if *(*Tu32)(unsafe.Pointer(bp + 16)) != 0 { /* If the checksum doesn't add up, then one or more of the disk sectors ** containing the super-journal filename is corrupted. This means ** definitely roll back, so just return SQLITE_OK and report a (nul) ** super-journal filename. */ *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) } *(*uint8)(unsafe.Pointer(zSuper + uintptr(*(*Tu32)(unsafe.Pointer(bp))))) = uint8('\000') *(*uint8)(unsafe.Pointer(zSuper + uintptr(*(*Tu32)(unsafe.Pointer(bp))+uint32(1)))) = uint8('\000') return SQLITE_OK } // C documentation // // /* // ** Return the offset of the sector boundary at or immediately // ** following the value in pPager->journalOff, assuming a sector // ** size of pPager->sectorSize bytes. // ** // ** i.e for a sector size of 512: // ** // ** Pager.journalOff Return value // ** --------------------------------------- // ** 0 0 // ** 512 512 // ** 100 512 // ** 2000 2048 // ** // */ func _journalHdrOffset(tls *libc.TLS, pPager uintptr) (r Ti64) { var c, offset Ti64 _, _ = c, offset offset = 0 c = (*TPager)(unsafe.Pointer(pPager)).FjournalOff if c != 0 { offset = ((c-int64(1))/int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) + int64(1)) * int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return offset } // C documentation // // /* // ** The journal file must be open when this function is called. // ** // ** This function is a no-op if the journal file has not been written to // ** within the current transaction (i.e. if Pager.journalOff==0). // ** // ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is // ** set to 0, then truncate the journal file to zero bytes in size. Otherwise, // ** zero the 28-byte header at the start of the journal file. In either case, // ** if the pager is not in no-sync mode, sync the journal file immediately // ** after writing or truncating it. // ** // ** If Pager.journalSizeLimit is set to a positive, non-zero value, and // ** following the truncation or zeroing described above the size of the // ** journal file in bytes is larger than this value, then truncate the // ** journal file to Pager.journalSizeLimit bytes. The journal file does // ** not need to be synced following this operation. // ** // ** If an IO error occurs, abandon processing and return the IO error code. // ** Otherwise, return SQLITE_OK. // */ func _zeroJournalHdr(tls *libc.TLS, pPager uintptr, doTruncate int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iLimit Ti64 var rc int32 var _ /* sz at bp+0 */ Ti64 _, _ = iLimit, rc rc = SQLITE_OK /* Return code */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TPager)(unsafe.Pointer(pPager)).FjournalOff != 0 { iLimit = (*TPager)(unsafe.Pointer(pPager)).FjournalSizeLimit /* Local cache of jsl */ if doTruncate != 0 || iLimit == 0 { rc = _sqlite3OsTruncate(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, 0) } else { rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, uintptr(unsafe.Pointer(&_zeroHdr)), int32(28), 0) } if rc == SQLITE_OK && !((*TPager)(unsafe.Pointer(pPager)).FnoSync != 0) { rc = _sqlite3OsSync(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, int32(SQLITE_SYNC_DATAONLY)|int32((*TPager)(unsafe.Pointer(pPager)).FsyncFlags)) } /* At this point the transaction is committed but the write lock ** is still held on the file. If there is a size limit configured for ** the persistent journal and the journal file currently consumes more ** space than that limit allows for, truncate it now. There is no need ** to sync the file following this operation. */ if rc == SQLITE_OK && iLimit > 0 { rc = _sqlite3OsFileSize(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, bp) if rc == SQLITE_OK && *(*Ti64)(unsafe.Pointer(bp)) > iLimit { rc = _sqlite3OsTruncate(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iLimit) } } } return rc } var _zeroHdr = [28]uint8{} // C documentation // // /* // ** The journal file must be open when this routine is called. A journal // ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the // ** current location. // ** // ** The format for the journal header is as follows: // ** - 8 bytes: Magic identifying journal format. // ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on. // ** - 4 bytes: Random number used for page hash. // ** - 4 bytes: Initial database page count. // ** - 4 bytes: Sector size used by the process that wrote this journal. // ** - 4 bytes: Database page size. // ** // ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space. // */ func _writeJournalHdr(tls *libc.TLS, pPager uintptr) (r int32) { var ii, rc int32 var nHeader, nWrite Tu32 var zHeader uintptr var v2 Ti64 _, _, _, _, _, _ = ii, nHeader, nWrite, rc, zHeader, v2 rc = SQLITE_OK /* Return code */ zHeader = (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace /* Temporary space used to build header */ nHeader = uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize) /* Loop counter */ _ = libc.Int32FromInt32(0) /* Journal file must be open. */ if nHeader > (*TPager)(unsafe.Pointer(pPager)).FsectorSize { nHeader = (*TPager)(unsafe.Pointer(pPager)).FsectorSize } /* If there are active savepoints and any of them were created ** since the most recent journal header was written, update the ** PagerSavepoint.iHdrOffset fields now. */ ii = 0 for { if !(ii < (*TPager)(unsafe.Pointer(pPager)).FnSavepoint) { break } if (*(*TPagerSavepoint)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(ii)*48))).FiHdrOffset == 0 { (*(*TPagerSavepoint)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(ii)*48))).FiHdrOffset = (*TPager)(unsafe.Pointer(pPager)).FjournalOff } goto _1 _1: ; ii++ } v2 = _journalHdrOffset(tls, pPager) (*TPager)(unsafe.Pointer(pPager)).FjournalOff = v2 (*TPager)(unsafe.Pointer(pPager)).FjournalHdr = v2 /* ** Write the nRec Field - the number of page records that follow this ** journal header. Normally, zero is written to this value at this time. ** After the records are added to the journal (and the journal synced, ** if in full-sync mode), the zero is overwritten with the true number ** of records (see syncJournal()). ** ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When ** reading the journal this value tells SQLite to assume that the ** rest of the journal file contains valid page records. This assumption ** is dangerous, as if a failure occurred whilst writing to the journal ** file it may contain some garbage data. There are two scenarios ** where this risk can be ignored: ** ** * When the pager is in no-sync mode. Corruption can follow a ** power failure in this case anyway. ** ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees ** that garbage data is never appended to the journal file. */ _ = libc.Int32FromInt32(0) if (*TPager)(unsafe.Pointer(pPager)).FnoSync != 0 || int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) == int32(PAGER_JOURNALMODE_MEMORY) || _sqlite3OsDeviceCharacteristics(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd)&int32(SQLITE_IOCAP_SAFE_APPEND) != 0 { libc.Xmemcpy(tls, zHeader, uintptr(unsafe.Pointer(&_aJournalMagic)), uint32(8)) _sqlite3Put4byte(tls, zHeader+uintptr(8), uint32(0xffffffff)) } else { libc.Xmemset(tls, zHeader, 0, libc.Uint32FromInt64(8)+libc.Uint32FromInt32(4)) } /* The random check-hash initializer */ if int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) != int32(PAGER_JOURNALMODE_MEMORY) { Xsqlite3_randomness(tls, int32(4), pPager+52) } _sqlite3Put4byte(tls, zHeader+uintptr(libc.Uint32FromInt64(8)+libc.Uint32FromInt32(4)), (*TPager)(unsafe.Pointer(pPager)).FcksumInit) /* The initial database size */ _sqlite3Put4byte(tls, zHeader+uintptr(libc.Uint32FromInt64(8)+libc.Uint32FromInt32(8)), (*TPager)(unsafe.Pointer(pPager)).FdbOrigSize) /* The assumed sector size for this process */ _sqlite3Put4byte(tls, zHeader+uintptr(libc.Uint32FromInt64(8)+libc.Uint32FromInt32(12)), (*TPager)(unsafe.Pointer(pPager)).FsectorSize) /* The page size */ _sqlite3Put4byte(tls, zHeader+uintptr(libc.Uint32FromInt64(8)+libc.Uint32FromInt32(16)), uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize)) /* Initializing the tail of the buffer is not necessary. Everything ** works find if the following memset() is omitted. But initializing ** the memory prevents valgrind from complaining, so we are willing to ** take the performance hit. */ libc.Xmemset(tls, zHeader+uintptr(libc.Uint32FromInt64(8)+libc.Uint32FromInt32(20)), 0, nHeader-(libc.Uint32FromInt64(8)+libc.Uint32FromInt32(20))) /* In theory, it is only necessary to write the 28 bytes that the ** journal header consumes to the journal file here. Then increment the ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next ** record is written to the following sector (leaving a gap in the file ** that will be implicitly filled in by the OS). ** ** However it has been discovered that on some systems this pattern can ** be significantly slower than contiguously writing data to the file, ** even if that means explicitly writing data to the block of ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what ** is done. ** ** The loop is required here in case the sector-size is larger than the ** database page size. Since the zHeader buffer is only Pager.pageSize ** bytes in size, more than one call to sqlite3OsWrite() may be required ** to populate the entire journal header sector. */ nWrite = uint32(0) for { if !(rc == SQLITE_OK && nWrite < (*TPager)(unsafe.Pointer(pPager)).FsectorSize) { break } rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, zHeader, int32(nHeader), (*TPager)(unsafe.Pointer(pPager)).FjournalOff) _ = libc.Int32FromInt32(0) *(*Ti64)(unsafe.Pointer(pPager + 80)) += int64(nHeader) goto _3 _3: ; nWrite += nHeader } return rc } // C documentation // // /* // ** The journal file must be open when this is called. A journal header file // ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal // ** file. The current location in the journal file is given by // ** pPager->journalOff. See comments above function writeJournalHdr() for // ** a description of the journal header format. // ** // ** If the header is read successfully, *pNRec is set to the number of // ** page records following this header and *pDbSize is set to the size of the // ** database before the transaction began, in pages. Also, pPager->cksumInit // ** is set to the value read from the journal header. SQLITE_OK is returned // ** in this case. // ** // ** If the journal header file appears to be corrupted, SQLITE_DONE is // ** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes // ** cannot be read from the journal file an error code is returned. // */ func _readJournalHdr(tls *libc.TLS, pPager uintptr, isHot int32, journalSize Ti64, pNRec uintptr, pDbSize uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iHdrOff Ti64 var rc, v1, v2, v4, v6, v7 int32 var v3, v5, v8 bool var _ /* aMagic at bp+0 */ [8]uint8 var _ /* iPageSize at bp+8 */ Tu32 var _ /* iSectorSize at bp+12 */ Tu32 _, _, _, _, _, _, _, _, _, _ = iHdrOff, rc, v1, v2, v3, v4, v5, v6, v7, v8 /* Offset of journal header being read */ _ = libc.Int32FromInt32(0) /* Journal file must be open. */ /* Advance Pager.journalOff to the start of the next sector. If the ** journal file is too small for there to be a header stored at this ** point, return SQLITE_DONE. */ (*TPager)(unsafe.Pointer(pPager)).FjournalOff = _journalHdrOffset(tls, pPager) if (*TPager)(unsafe.Pointer(pPager)).FjournalOff+int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) > journalSize { return int32(SQLITE_DONE) } iHdrOff = (*TPager)(unsafe.Pointer(pPager)).FjournalOff /* Read in the first 8 bytes of the journal header. If they do not match ** the magic string found at the start of each journal header, return ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise, ** proceed. */ if isHot != 0 || iHdrOff != (*TPager)(unsafe.Pointer(pPager)).FjournalHdr { rc = _sqlite3OsRead(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, bp, int32(8), iHdrOff) if rc != 0 { return rc } if libc.Xmemcmp(tls, bp, uintptr(unsafe.Pointer(&_aJournalMagic)), uint32(8)) != 0 { return int32(SQLITE_DONE) } } /* Read the first three 32-bit fields of the journal header: The nRec ** field, the checksum-initializer and the database size at the start ** of the transaction. Return an error code if anything goes wrong. */ v1 = _read32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff+int64(8), pNRec) rc = v1 if v3 = SQLITE_OK != v1; !v3 { v2 = _read32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff+int64(12), pPager+52) rc = v2 } if v5 = v3 || SQLITE_OK != v2; !v5 { v4 = _read32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff+int64(16), pDbSize) rc = v4 } if v5 || SQLITE_OK != v4 { return rc } if (*TPager)(unsafe.Pointer(pPager)).FjournalOff == 0 { /* Sector-size field of journal header */ /* Read the page-size and sector-size journal header fields. */ v6 = _read32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff+int64(20), bp+12) rc = v6 if v8 = SQLITE_OK != v6; !v8 { v7 = _read32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff+int64(24), bp+8) rc = v7 } if v8 || SQLITE_OK != v7 { return rc } /* Versions of SQLite prior to 3.5.8 set the page-size field of the ** journal header to zero. In this case, assume that the Pager.pageSize ** variable is already set to the correct page size. */ if *(*Tu32)(unsafe.Pointer(bp + 8)) == uint32(0) { *(*Tu32)(unsafe.Pointer(bp + 8)) = uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize) } /* Check that the values read from the page-size and sector-size fields ** are within range. To be 'in range', both values need to be a power ** of two greater than or equal to 512 or 32, and not greater than their ** respective compile time maximum limits. */ if *(*Tu32)(unsafe.Pointer(bp + 8)) < uint32(512) || *(*Tu32)(unsafe.Pointer(bp + 12)) < uint32(32) || *(*Tu32)(unsafe.Pointer(bp + 8)) > uint32(SQLITE_MAX_PAGE_SIZE) || *(*Tu32)(unsafe.Pointer(bp + 12)) > uint32(MAX_SECTOR_SIZE) || (*(*Tu32)(unsafe.Pointer(bp + 8))-uint32(1))&*(*Tu32)(unsafe.Pointer(bp + 8)) != uint32(0) || (*(*Tu32)(unsafe.Pointer(bp + 12))-uint32(1))&*(*Tu32)(unsafe.Pointer(bp + 12)) != uint32(0) { /* If the either the page-size or sector-size in the journal-header is ** invalid, then the process that wrote the journal-header must have ** crashed before the header was synced. In this case stop reading ** the journal file here. */ return int32(SQLITE_DONE) } /* Update the page-size to match the value read from the journal. ** Use a testcase() macro to make sure that malloc failure within ** PagerSetPagesize() is tested. */ rc = _sqlite3PagerSetPagesize(tls, pPager, bp+8, -int32(1)) /* Update the assumed sector-size to match the value used by ** the process that created this journal. If this journal was ** created by a process other than this one, then this routine ** is being called from within pager_playback(). The local value ** of Pager.sectorSize is restored at the end of that routine. */ (*TPager)(unsafe.Pointer(pPager)).FsectorSize = *(*Tu32)(unsafe.Pointer(bp + 12)) } *(*Ti64)(unsafe.Pointer(pPager + 80)) += int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) return rc } // C documentation // // /* // ** Write the supplied super-journal name into the journal file for pager // ** pPager at the current location. The super-journal name must be the last // ** thing written to a journal file. If the pager is in full-sync mode, the // ** journal file descriptor is advanced to the next sector boundary before // ** anything is written. The format is: // ** // ** + 4 bytes: PAGER_SJ_PGNO. // ** + N bytes: super-journal filename in utf-8. // ** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator). // ** + 4 bytes: super-journal name checksum. // ** + 8 bytes: aJournalMagic[]. // ** // ** The super-journal page checksum is the sum of the bytes in the super-journal // ** name, where each byte is interpreted as a signed 8-bit integer. // ** // ** If zSuper is a NULL pointer (occurs for a single database transaction), // ** this call is a no-op. // */ func _writeSuperJournal(tls *libc.TLS, pPager uintptr, zSuper uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var cksum Tu32 var iHdrOff Ti64 var nSuper, rc, v11, v2, v3, v5, v7, v9 int32 var v10, v4, v6, v8 bool var _ /* jrnlSize at bp+0 */ Ti64 _, _, _, _, _, _, _, _, _, _, _, _, _, _ = cksum, iHdrOff, nSuper, rc, v10, v11, v2, v3, v4, v5, v6, v7, v8, v9 /* Size of journal file on disk */ cksum = uint32(0) /* Checksum of string zSuper */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if !(zSuper != 0) || int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) == int32(PAGER_JOURNALMODE_MEMORY) || !((*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != libc.UintptrFromInt32(0)) { return SQLITE_OK } (*TPager)(unsafe.Pointer(pPager)).FsetSuper = uint8(1) _ = libc.Int32FromInt32(0) /* Calculate the length in bytes and the checksum of zSuper */ nSuper = 0 for { if !(*(*uint8)(unsafe.Pointer(zSuper + uintptr(nSuper))) != 0) { break } cksum += uint32(*(*uint8)(unsafe.Pointer(zSuper + uintptr(nSuper)))) goto _1 _1: ; nSuper++ } /* If in full-sync mode, advance to the next disk sector before writing ** the super-journal name. This is in case the previous page written to ** the journal has already been synced. */ if (*TPager)(unsafe.Pointer(pPager)).FfullSync != 0 { (*TPager)(unsafe.Pointer(pPager)).FjournalOff = _journalHdrOffset(tls, pPager) } iHdrOff = (*TPager)(unsafe.Pointer(pPager)).FjournalOff /* Write the super-journal data to the end of the journal file. If ** an error occurs, return the error code to the caller. */ v2 = _write32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff, (*TPager)(unsafe.Pointer(pPager)).FlckPgno) rc = v2 if v4 = 0 != v2; !v4 { v3 = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, zSuper, nSuper, iHdrOff+int64(4)) rc = v3 } if v6 = v4 || 0 != v3; !v6 { v5 = _write32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff+int64(4)+int64(nSuper), uint32(nSuper)) rc = v5 } if v8 = v6 || 0 != v5; !v8 { v7 = _write32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff+int64(4)+int64(nSuper)+int64(4), cksum) rc = v7 } if v10 = v8 || 0 != v7; !v10 { v9 = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, uintptr(unsafe.Pointer(&_aJournalMagic)), int32(8), iHdrOff+int64(4)+int64(nSuper)+int64(8)) rc = v9 } if v10 || 0 != v9 { return rc } *(*Ti64)(unsafe.Pointer(pPager + 80)) += int64(nSuper + libc.Int32FromInt32(20)) /* If the pager is in persistent-journal mode, then the physical ** journal-file may extend past the end of the super-journal name ** and 8 bytes of magic data just written to the file. This is ** dangerous because the code to rollback a hot-journal file ** will not be able to find the super-journal name to determine ** whether or not the journal is hot. ** ** Easiest thing to do in this scenario is to truncate the journal ** file to the required size. */ v11 = _sqlite3OsFileSize(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, bp) rc = v11 if SQLITE_OK == v11 && *(*Ti64)(unsafe.Pointer(bp)) > (*TPager)(unsafe.Pointer(pPager)).FjournalOff { rc = _sqlite3OsTruncate(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, (*TPager)(unsafe.Pointer(pPager)).FjournalOff) } return rc } // C documentation // // /* // ** Discard the entire contents of the in-memory page-cache. // */ func _pager_reset(tls *libc.TLS, pPager uintptr) { (*TPager)(unsafe.Pointer(pPager)).FiDataVersion++ _sqlite3BackupRestart(tls, (*TPager)(unsafe.Pointer(pPager)).FpBackup) _sqlite3PcacheClear(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) } // C documentation // // /* // ** Return the pPager->iDataVersion value // */ func _sqlite3PagerDataVersion(tls *libc.TLS, pPager uintptr) (r Tu32) { return (*TPager)(unsafe.Pointer(pPager)).FiDataVersion } // C documentation // // /* // ** Free all structures in the Pager.aSavepoint[] array and set both // ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal // ** if it is open and the pager is not in exclusive mode. // */ func _releaseAllSavepoints(tls *libc.TLS, pPager uintptr) { var ii int32 _ = ii /* Iterator for looping through Pager.aSavepoint */ ii = 0 for { if !(ii < (*TPager)(unsafe.Pointer(pPager)).FnSavepoint) { break } _sqlite3BitvecDestroy(tls, (*(*TPagerSavepoint)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(ii)*48))).FpInSavepoint) goto _1 _1: ; ii++ } if !((*TPager)(unsafe.Pointer(pPager)).FexclusiveMode != 0) || _sqlite3JournalIsInMemory(tls, (*TPager)(unsafe.Pointer(pPager)).Fsjfd) != 0 { _sqlite3OsClose(tls, (*TPager)(unsafe.Pointer(pPager)).Fsjfd) } Xsqlite3_free(tls, (*TPager)(unsafe.Pointer(pPager)).FaSavepoint) (*TPager)(unsafe.Pointer(pPager)).FaSavepoint = uintptr(0) (*TPager)(unsafe.Pointer(pPager)).FnSavepoint = 0 (*TPager)(unsafe.Pointer(pPager)).FnSubRec = uint32(0) } // C documentation // // /* // ** Set the bit number pgno in the PagerSavepoint.pInSavepoint // ** bitvecs of all open savepoints. Return SQLITE_OK if successful // ** or SQLITE_NOMEM if a malloc failure occurs. // */ func _addToSavepointBitvecs(tls *libc.TLS, pPager uintptr, pgno TPgno) (r int32) { var ii, rc int32 var p uintptr _, _, _ = ii, p, rc /* Loop counter */ rc = SQLITE_OK /* Result code */ ii = 0 for { if !(ii < (*TPager)(unsafe.Pointer(pPager)).FnSavepoint) { break } p = (*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(ii)*48 if pgno <= (*TPagerSavepoint)(unsafe.Pointer(p)).FnOrig { rc |= _sqlite3BitvecSet(tls, (*TPagerSavepoint)(unsafe.Pointer(p)).FpInSavepoint, pgno) _ = libc.Int32FromInt32(0) } goto _1 _1: ; ii++ } return rc } // C documentation // // /* // ** This function is a no-op if the pager is in exclusive mode and not // ** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN // ** state. // ** // ** If the pager is not in exclusive-access mode, the database file is // ** completely unlocked. If the file is unlocked and the file-system does // ** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is // ** closed (if it is open). // ** // ** If the pager is in ERROR state when this function is called, the // ** contents of the pager cache are discarded before switching back to // ** the OPEN state. Regardless of whether the pager is in exclusive-mode // ** or not, any journal file left in the file-system will be treated // ** as a hot-journal and rolled back the next time a read-transaction // ** is opened (by this or by any other connection). // */ func _pager_unlock(tls *libc.TLS, pPager uintptr) { var iDc, rc, v1, v2 int32 _, _, _, _ = iDc, rc, v1, v2 _ = libc.Int32FromInt32(0) _sqlite3BitvecDestroy(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal) (*TPager)(unsafe.Pointer(pPager)).FpInJournal = uintptr(0) _releaseAllSavepoints(tls, pPager) if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) { _ = libc.Int32FromInt32(0) _sqlite3WalEndReadTransaction(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal) (*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_OPEN) } else { if !((*TPager)(unsafe.Pointer(pPager)).FexclusiveMode != 0) { if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != uintptr(0) { v1 = _sqlite3OsDeviceCharacteristics(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd) } else { v1 = 0 } /* Error code returned by pagerUnlockDb() */ iDc = v1 /* If the operating system support deletion of open files, then ** close the journal file when dropping the database lock. Otherwise ** another connection with journal_mode=delete might delete the file ** out from under us. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if 0 == iDc&int32(SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN) || int32(1) != int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode)&int32(5) { _sqlite3OsClose(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd) } /* If the pager is in the ERROR state and the call to unlock the database ** file fails, set the current lock to UNKNOWN_LOCK. See the comment ** above the #define for UNKNOWN_LOCK for an explanation of why this ** is necessary. */ rc = _pagerUnlockDb(tls, pPager, NO_LOCK) if rc != SQLITE_OK && int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_ERROR) { (*TPager)(unsafe.Pointer(pPager)).FeLock = uint8(libc.Int32FromInt32(EXCLUSIVE_LOCK) + libc.Int32FromInt32(1)) } /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here ** without clearing the error code. This is intentional - the error ** code is cleared and the cache reset in the block below. */ _ = libc.Int32FromInt32(0) (*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_OPEN) } } /* If Pager.errCode is set, the contents of the pager cache cannot be ** trusted. Now that there are no outstanding references to the pager, ** it can safely move back to PAGER_OPEN state. This happens in both ** normal and exclusive-locking mode. */ _ = libc.Int32FromInt32(0) if (*TPager)(unsafe.Pointer(pPager)).FerrCode != 0 { if int32((*TPager)(unsafe.Pointer(pPager)).FtempFile) == 0 { _pager_reset(tls, pPager) (*TPager)(unsafe.Pointer(pPager)).FchangeCountDone = uint8(0) (*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_OPEN) } else { if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != uintptr(0) { v2 = PAGER_OPEN } else { v2 = int32(PAGER_READER) } (*TPager)(unsafe.Pointer(pPager)).FeState = uint8(v2) } if (*TPager)(unsafe.Pointer(pPager)).FbUseFetch != 0 { _sqlite3OsUnfetch(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, 0, uintptr(0)) } (*TPager)(unsafe.Pointer(pPager)).FerrCode = SQLITE_OK _setGetterMethod(tls, pPager) } (*TPager)(unsafe.Pointer(pPager)).FjournalOff = 0 (*TPager)(unsafe.Pointer(pPager)).FjournalHdr = 0 (*TPager)(unsafe.Pointer(pPager)).FsetSuper = uint8(0) } // C documentation // // /* // ** This function is called whenever an IOERR or FULL error that requires // ** the pager to transition into the ERROR state may have occurred. // ** The first argument is a pointer to the pager structure, the second // ** the error-code about to be returned by a pager API function. The // ** value returned is a copy of the second argument to this function. // ** // ** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the // ** IOERR sub-codes, the pager enters the ERROR state and the error code // ** is stored in Pager.errCode. While the pager remains in the ERROR state, // ** all major API calls on the Pager will immediately return Pager.errCode. // ** // ** The ERROR state indicates that the contents of the pager-cache // ** cannot be trusted. This state can be cleared by completely discarding // ** the contents of the pager-cache. If a transaction was active when // ** the persistent error occurred, then the rollback journal may need // ** to be replayed to restore the contents of the database file (as if // ** it were a hot-journal). // */ func _pager_error(tls *libc.TLS, pPager uintptr, rc int32) (r int32) { var rc2 int32 _ = rc2 rc2 = rc & int32(0xff) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if rc2 == int32(SQLITE_FULL) || rc2 == int32(SQLITE_IOERR) { (*TPager)(unsafe.Pointer(pPager)).FerrCode = rc (*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_ERROR) _setGetterMethod(tls, pPager) } return rc } // C documentation // // /* // ** The write transaction open on pPager is being committed (bCommit==1) // ** or rolled back (bCommit==0). // ** // ** Return TRUE if and only if all dirty pages should be flushed to disk. // ** // ** Rules: // ** // ** * For non-TEMP databases, always sync to disk. This is necessary // ** for transactions to be durable. // ** // ** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing // ** file has been created already (via a spill on pagerStress()) and // ** when the number of dirty pages in memory exceeds 25% of the total // ** cache size. // */ func _pagerFlushOnCommit(tls *libc.TLS, pPager uintptr, bCommit int32) (r int32) { if int32((*TPager)(unsafe.Pointer(pPager)).FtempFile) == 0 { return int32(1) } if !(bCommit != 0) { return 0 } if !((*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != libc.UintptrFromInt32(0)) { return 0 } return libc.BoolInt32(_sqlite3PCachePercentDirty(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) >= int32(25)) } // C documentation // // /* // ** This routine ends a transaction. A transaction is usually ended by // ** either a COMMIT or a ROLLBACK operation. This routine may be called // ** after rollback of a hot-journal, or if an error occurs while opening // ** the journal file or writing the very first journal-header of a // ** database transaction. // ** // ** This routine is never called in PAGER_ERROR state. If it is called // ** in PAGER_NONE or PAGER_SHARED state and the lock held is less // ** exclusive than a RESERVED lock, it is a no-op. // ** // ** Otherwise, any active savepoints are released. // ** // ** If the journal file is open, then it is "finalized". Once a journal // ** file has been finalized it is not possible to use it to roll back a // ** transaction. Nor will it be considered to be a hot-journal by this // ** or any other database connection. Exactly how a journal is finalized // ** depends on whether or not the pager is running in exclusive mode and // ** the current journal-mode (Pager.journalMode value), as follows: // ** // ** journalMode==MEMORY // ** Journal file descriptor is simply closed. This destroys an // ** in-memory journal. // ** // ** journalMode==TRUNCATE // ** Journal file is truncated to zero bytes in size. // ** // ** journalMode==PERSIST // ** The first 28 bytes of the journal file are zeroed. This invalidates // ** the first journal header in the file, and hence the entire journal // ** file. An invalid journal file cannot be rolled back. // ** // ** journalMode==DELETE // ** The journal file is closed and deleted using sqlite3OsDelete(). // ** // ** If the pager is running in exclusive mode, this method of finalizing // ** the journal file is never used. Instead, if the journalMode is // ** DELETE and the pager is in exclusive mode, the method described under // ** journalMode==PERSIST is used instead. // ** // ** After the journal is finalized, the pager moves to PAGER_READER state. // ** If running in non-exclusive rollback mode, the lock on the file is // ** downgraded to a SHARED_LOCK. // ** // ** SQLITE_OK is returned if no error occurs. If an error occurs during // ** any of the IO operations to finalize the journal file or unlock the // ** database then the IO error code is returned to the user. If the // ** operation to finalize the journal file fails, then the code still // ** tries to unlock the database file if not in exclusive mode. If the // ** unlock operation fails as well, then the first error code related // ** to the first error encountered (the journal finalization one) is // ** returned. // */ func _pager_end_transaction(tls *libc.TLS, pPager uintptr, hasSuper int32, bCommit int32) (r int32) { var bDelete, rc, rc2, v1 int32 _, _, _, _ = bDelete, rc, rc2, v1 rc = SQLITE_OK /* Error code from journal finalization operation */ rc2 = SQLITE_OK /* Error code from db file unlock operation */ /* Do nothing if the pager does not have an open write transaction ** or at least a RESERVED lock. This function may be called when there ** is no write-transaction active but a RESERVED or greater lock is ** held under two circumstances: ** ** 1. After a successful hot-journal rollback, it is called with ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK. ** ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE ** lock switches back to locking_mode=normal and then executes a ** read-transaction, this function is called with eState==PAGER_READER ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TPager)(unsafe.Pointer(pPager)).FeState) < int32(PAGER_WRITER_LOCKED) && int32((*TPager)(unsafe.Pointer(pPager)).FeLock) < int32(RESERVED_LOCK) { return SQLITE_OK } _releaseAllSavepoints(tls, pPager) _ = libc.Int32FromInt32(0) if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != uintptr(0) { _ = libc.Int32FromInt32(0) /* Finalize the journal file. */ if _sqlite3JournalIsInMemory(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd) != 0 { /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */ _sqlite3OsClose(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd) } else { if int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) == int32(PAGER_JOURNALMODE_TRUNCATE) { if (*TPager)(unsafe.Pointer(pPager)).FjournalOff == 0 { rc = SQLITE_OK } else { rc = _sqlite3OsTruncate(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, 0) if rc == SQLITE_OK && (*TPager)(unsafe.Pointer(pPager)).FfullSync != 0 { /* Make sure the new file size is written into the inode right away. ** Otherwise the journal might resurrect following a power loss and ** cause the last transaction to roll back. See ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773 */ rc = _sqlite3OsSync(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, int32((*TPager)(unsafe.Pointer(pPager)).FsyncFlags)) } } (*TPager)(unsafe.Pointer(pPager)).FjournalOff = 0 } else { if int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) == int32(PAGER_JOURNALMODE_PERSIST) || (*TPager)(unsafe.Pointer(pPager)).FexclusiveMode != 0 && int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) != int32(PAGER_JOURNALMODE_WAL) { rc = _zeroJournalHdr(tls, pPager, libc.BoolInt32(hasSuper != 0 || (*TPager)(unsafe.Pointer(pPager)).FtempFile != 0)) (*TPager)(unsafe.Pointer(pPager)).FjournalOff = 0 } else { /* This branch may be executed with Pager.journalMode==MEMORY if ** a hot-journal was just rolled back. In this case the journal ** file should be closed and deleted. If this connection writes to ** the database file, it will do so using an in-memory journal. */ bDelete = libc.BoolInt32(!((*TPager)(unsafe.Pointer(pPager)).FtempFile != 0)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3OsClose(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd) if bDelete != 0 { rc = _sqlite3OsDelete(tls, (*TPager)(unsafe.Pointer(pPager)).FpVfs, (*TPager)(unsafe.Pointer(pPager)).FzJournal, int32((*TPager)(unsafe.Pointer(pPager)).FextraSync)) } } } } } _sqlite3BitvecDestroy(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal) (*TPager)(unsafe.Pointer(pPager)).FpInJournal = uintptr(0) (*TPager)(unsafe.Pointer(pPager)).FnRec = 0 if rc == SQLITE_OK { if (*TPager)(unsafe.Pointer(pPager)).FmemDb != 0 || _pagerFlushOnCommit(tls, pPager, bCommit) != 0 { _sqlite3PcacheCleanAll(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) } else { _sqlite3PcacheClearWritable(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) } _sqlite3PcacheTruncate(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache, (*TPager)(unsafe.Pointer(pPager)).FdbSize) } if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) { /* Drop the WAL write-lock, if any. Also, if the connection was in ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE ** lock held on the database file. */ rc2 = _sqlite3WalEndWriteTransaction(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal) _ = libc.Int32FromInt32(0) } else { if rc == SQLITE_OK && bCommit != 0 && (*TPager)(unsafe.Pointer(pPager)).FdbFileSize > (*TPager)(unsafe.Pointer(pPager)).FdbSize { /* This branch is taken when committing a transaction in rollback-journal ** mode if the database file on disk is larger than the database image. ** At this point the journal has been finalized and the transaction ** successfully committed, but the EXCLUSIVE lock is still held on the ** file. So it is safe to truncate the database file to its minimum ** required size. */ _ = libc.Int32FromInt32(0) rc = _pager_truncate(tls, pPager, (*TPager)(unsafe.Pointer(pPager)).FdbSize) } } if rc == SQLITE_OK && bCommit != 0 { rc = _sqlite3OsFileControl(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int32(SQLITE_FCNTL_COMMIT_PHASETWO), uintptr(0)) if rc == int32(SQLITE_NOTFOUND) { rc = SQLITE_OK } } if !((*TPager)(unsafe.Pointer(pPager)).FexclusiveMode != 0) && (!((*TPager)(unsafe.Pointer(pPager)).FpWal != libc.UintptrFromInt32(0)) || _sqlite3WalExclusiveMode(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, 0) != 0) { rc2 = _pagerUnlockDb(tls, pPager, int32(SHARED_LOCK)) } (*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_READER) (*TPager)(unsafe.Pointer(pPager)).FsetSuper = uint8(0) if rc == SQLITE_OK { v1 = rc2 } else { v1 = rc } return v1 } // C documentation // // /* // ** Execute a rollback if a transaction is active and unlock the // ** database file. // ** // ** If the pager has already entered the ERROR state, do not attempt // ** the rollback at this time. Instead, pager_unlock() is called. The // ** call to pager_unlock() will discard all in-memory pages, unlock // ** the database file and move the pager back to OPEN state. If this // ** means that there is a hot-journal left in the file-system, the next // ** connection to obtain a shared lock on the pager (which may be this one) // ** will roll it back. // ** // ** If the pager has not already entered the ERROR state, but an IO or // ** malloc error occurs during a rollback, then this will itself cause // ** the pager to enter the ERROR state. Which will be cleared by the // ** call to pager_unlock(), as described above. // */ func _pagerUnlockAndRollback(tls *libc.TLS, pPager uintptr) { var eLock Tu8 var errCode int32 _, _ = eLock, errCode if int32((*TPager)(unsafe.Pointer(pPager)).FeState) != int32(PAGER_ERROR) && int32((*TPager)(unsafe.Pointer(pPager)).FeState) != PAGER_OPEN { _ = libc.Int32FromInt32(0) if int32((*TPager)(unsafe.Pointer(pPager)).FeState) >= int32(PAGER_WRITER_LOCKED) { _sqlite3BeginBenignMalloc(tls) _sqlite3PagerRollback(tls, pPager) _sqlite3EndBenignMalloc(tls) } else { if !((*TPager)(unsafe.Pointer(pPager)).FexclusiveMode != 0) { _ = libc.Int32FromInt32(0) _pager_end_transaction(tls, pPager, 0, 0) } } } else { if int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_ERROR) && int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) == int32(PAGER_JOURNALMODE_MEMORY) && (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != uintptr(0) { /* Special case for a ROLLBACK due to I/O error with an in-memory ** journal: We have to rollback immediately, before the journal is ** closed, because once it is closed, all content is forgotten. */ errCode = (*TPager)(unsafe.Pointer(pPager)).FerrCode eLock = (*TPager)(unsafe.Pointer(pPager)).FeLock (*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_OPEN) (*TPager)(unsafe.Pointer(pPager)).FerrCode = SQLITE_OK (*TPager)(unsafe.Pointer(pPager)).FeLock = uint8(EXCLUSIVE_LOCK) _pager_playback(tls, pPager, int32(1)) (*TPager)(unsafe.Pointer(pPager)).FerrCode = errCode (*TPager)(unsafe.Pointer(pPager)).FeLock = eLock } } _pager_unlock(tls, pPager) } // C documentation // // /* // ** Parameter aData must point to a buffer of pPager->pageSize bytes // ** of data. Compute and return a checksum based on the contents of the // ** page of data and the current value of pPager->cksumInit. // ** // ** This is not a real checksum. It is really just the sum of the // ** random initial value (pPager->cksumInit) and every 200th byte // ** of the page data, starting with byte offset (pPager->pageSize%200). // ** Each byte is interpreted as an 8-bit unsigned integer. // ** // ** Changing the formula used to compute this checksum results in an // ** incompatible journal file format. // ** // ** If journal corruption occurs due to a power failure, the most likely // ** scenario is that one end or the other of the record will be changed. // ** It is much less likely that the two ends of the journal record will be // ** correct and the middle be corrupt. Thus, this "checksum" scheme, // ** though fast and simple, catches the mostly likely kind of corruption. // */ func _pager_cksum(tls *libc.TLS, pPager uintptr, aData uintptr) (r Tu32) { var cksum Tu32 var i int32 _, _ = cksum, i cksum = (*TPager)(unsafe.Pointer(pPager)).FcksumInit /* Checksum value to return */ i = int32((*TPager)(unsafe.Pointer(pPager)).FpageSize - int64(200)) /* Loop counter */ for i > 0 { cksum += uint32(*(*Tu8)(unsafe.Pointer(aData + uintptr(i)))) i -= int32(200) } return cksum } // C documentation // // /* // ** Read a single page from either the journal file (if isMainJrnl==1) or // ** from the sub-journal (if isMainJrnl==0) and playback that page. // ** The page begins at offset *pOffset into the file. The *pOffset // ** value is increased to the start of the next page in the journal. // ** // ** The main rollback journal uses checksums - the statement journal does // ** not. // ** // ** If the page number of the page record read from the (sub-)journal file // ** is greater than the current value of Pager.dbSize, then playback is // ** skipped and SQLITE_OK is returned. // ** // ** If pDone is not NULL, then it is a record of pages that have already // ** been played back. If the page at *pOffset has already been played back // ** (if the corresponding pDone bit is set) then skip the playback. // ** Make sure the pDone bit corresponding to the *pOffset page is set // ** prior to returning. // ** // ** If the page record is successfully read from the (sub-)journal file // ** and played back, then SQLITE_OK is returned. If an IO error occurs // ** while reading the record from the (sub-)journal file or while writing // ** to the database file, then the IO error code is returned. If data // ** is successfully read from the (sub-)journal file but appears to be // ** corrupted, SQLITE_DONE is returned. Data is considered corrupted in // ** two circumstances: // ** // ** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or // ** * If the record is being rolled back from the main journal file // ** and the checksum field does not match the record content. // ** // ** Neither of these two scenarios are possible during a savepoint rollback. // ** // ** If this is a savepoint rollback, then memory may have to be dynamically // ** allocated by this function. If this is the case and an allocation fails, // ** SQLITE_NOMEM is returned. // */ func _pager_playback_one_page(tls *libc.TLS, pPager uintptr, pOffset uintptr, pDone uintptr, isMainJrnl int32, isSavepnt int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var aData, jfd, pData, v1, p4, p5 uintptr var isSynced, rc, v2 int32 var ofst Ti64 var v3 bool var _ /* cksum at bp+8 */ Tu32 var _ /* pPg at bp+0 */ uintptr var _ /* pgno at bp+4 */ TPgno _, _, _, _, _, _, _, _, _, _, _ = aData, isSynced, jfd, ofst, pData, rc, v1, v2, v3, p4, p5 /* True if journal page is synced */ _ = libc.Int32FromInt32(0) /* isMainJrnl is 0 or 1 */ _ = libc.Int32FromInt32(0) /* isSavepnt is 0 or 1 */ _ = libc.Int32FromInt32(0) /* pDone always used on sub-journals */ _ = libc.Int32FromInt32(0) /* pDone never used on non-savepoint */ aData = (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace _ = libc.Int32FromInt32(0) /* Temp storage must have already been allocated */ _ = libc.Int32FromInt32(0) /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction ** or savepoint rollback done at the request of the caller) or this is ** a hot-journal rollback. If it is a hot-journal rollback, the pager ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback ** only reads from the main journal, not the sub-journal. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Read the page number and page data from the journal or sub-journal ** file. Return an error code to the caller if an IO error occurs. */ if isMainJrnl != 0 { v1 = (*TPager)(unsafe.Pointer(pPager)).Fjfd } else { v1 = (*TPager)(unsafe.Pointer(pPager)).Fsjfd } jfd = v1 rc = _read32bits(tls, jfd, *(*Ti64)(unsafe.Pointer(pOffset)), bp+4) if rc != SQLITE_OK { return rc } rc = _sqlite3OsRead(tls, jfd, aData, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), *(*Ti64)(unsafe.Pointer(pOffset))+int64(4)) if rc != SQLITE_OK { return rc } *(*Ti64)(unsafe.Pointer(pOffset)) += (*TPager)(unsafe.Pointer(pPager)).FpageSize + int64(4) + int64(isMainJrnl*int32(4)) /* Sanity checking on the page. This is more important that I originally ** thought. If a power failure occurs while the journal is being written, ** it could cause invalid data to be written into the journal. We need to ** detect this invalid data (with high probability) and ignore it. */ if *(*TPgno)(unsafe.Pointer(bp + 4)) == uint32(0) || *(*TPgno)(unsafe.Pointer(bp + 4)) == (*TPager)(unsafe.Pointer(pPager)).FlckPgno { _ = libc.Int32FromInt32(0) return int32(SQLITE_DONE) } if *(*TPgno)(unsafe.Pointer(bp + 4)) > (*TPager)(unsafe.Pointer(pPager)).FdbSize || _sqlite3BitvecTest(tls, pDone, *(*TPgno)(unsafe.Pointer(bp + 4))) != 0 { return SQLITE_OK } if isMainJrnl != 0 { rc = _read32bits(tls, jfd, *(*Ti64)(unsafe.Pointer(pOffset))-int64(4), bp+8) if rc != 0 { return rc } if !(isSavepnt != 0) && _pager_cksum(tls, pPager, aData) != *(*Tu32)(unsafe.Pointer(bp + 8)) { return int32(SQLITE_DONE) } } /* If this page has already been played back before during the current ** rollback, then don't bother to play it back again. */ if v3 = pDone != 0; v3 { v2 = _sqlite3BitvecSet(tls, pDone, *(*TPgno)(unsafe.Pointer(bp + 4))) rc = v2 } if v3 && v2 != SQLITE_OK { return rc } /* When playing back page 1, restore the nReserve setting */ if *(*TPgno)(unsafe.Pointer(bp + 4)) == uint32(1) && int32((*TPager)(unsafe.Pointer(pPager)).FnReserve) != int32(*(*Tu8)(unsafe.Pointer(aData + 20))) { (*TPager)(unsafe.Pointer(pPager)).FnReserve = int16(*(*Tu8)(unsafe.Pointer(aData + 20))) } /* If the pager is in CACHEMOD state, then there must be a copy of this ** page in the pager cache. In this case just update the pager cache, ** not the database file. The page is left marked dirty in this case. ** ** An exception to the above rule: If the database is in no-sync mode ** and a page is moved during an incremental vacuum then the page may ** not be in the pager cache. Later: if a malloc() or IO error occurs ** during a Movepage() call, then the page may not be in the cache ** either. So the condition described in the above paragraph is not ** assert()able. ** ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the ** pager cache if it exists and the main file. The page is then marked ** not dirty. Since this code is only executed in PAGER_OPEN state for ** a hot-journal rollback, it is guaranteed that the page-cache is empty ** if the pager is in OPEN state. ** ** Ticket #1171: The statement journal might contain page content that is ** different from the page content at the start of the transaction. ** This occurs when a page is changed prior to the start of a statement ** then changed again within the statement. When rolling back such a ** statement we must not write to the original database unless we know ** for certain that original page contents are synced into the main rollback ** journal. Otherwise, a power loss might leave modified data in the ** database file without an entry in the rollback journal that can ** restore the database to its original form. Two conditions must be ** met before writing to the database files. (1) the database must be ** locked. (2) we know that the original page content is fully synced ** in the main journal either because the page is not in cache or else ** the page is marked as needSync==0. ** ** 2008-04-14: When attempting to vacuum a corrupt database file, it ** is possible to fail a statement on a database that does not yet exist. ** Do not attempt to write if database file has never been opened. */ if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } else { *(*uintptr)(unsafe.Pointer(bp)) = _sqlite3PagerLookup(tls, pPager, *(*TPgno)(unsafe.Pointer(bp + 4))) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if isMainJrnl != 0 { isSynced = libc.BoolInt32((*TPager)(unsafe.Pointer(pPager)).FnoSync != 0 || *(*Ti64)(unsafe.Pointer(pOffset)) <= (*TPager)(unsafe.Pointer(pPager)).FjournalHdr) } else { isSynced = libc.BoolInt32(*(*uintptr)(unsafe.Pointer(bp)) == uintptr(0) || 0 == int32((*TPgHdr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fflags)&int32(PGHDR_NEED_SYNC)) } if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != uintptr(0) && (int32((*TPager)(unsafe.Pointer(pPager)).FeState) >= int32(PAGER_WRITER_DBMOD) || int32((*TPager)(unsafe.Pointer(pPager)).FeState) == PAGER_OPEN) && isSynced != 0 { ofst = int64(*(*TPgno)(unsafe.Pointer(bp + 4))-libc.Uint32FromInt32(1)) * (*TPager)(unsafe.Pointer(pPager)).FpageSize _ = libc.Int32FromInt32(0) /* Write the data read from the journal back into the database file. ** This is usually safe even for an encrypted database - as the data ** was encrypted before it was written to the journal file. The exception ** is if the data was just read from an in-memory sub-journal. In that ** case it must be encrypted here before it is copied into the database ** file. */ rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, aData, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), ofst) if *(*TPgno)(unsafe.Pointer(bp + 4)) > (*TPager)(unsafe.Pointer(pPager)).FdbFileSize { (*TPager)(unsafe.Pointer(pPager)).FdbFileSize = *(*TPgno)(unsafe.Pointer(bp + 4)) } if (*TPager)(unsafe.Pointer(pPager)).FpBackup != 0 { _sqlite3BackupUpdate(tls, (*TPager)(unsafe.Pointer(pPager)).FpBackup, *(*TPgno)(unsafe.Pointer(bp + 4)), aData) } } else { if !(isMainJrnl != 0) && *(*uintptr)(unsafe.Pointer(bp)) == uintptr(0) { /* If this is a rollback of a savepoint and data was not written to ** the database and the page is not in-memory, there is a potential ** problem. When the page is next fetched by the b-tree layer, it ** will be read from the database file, which may or may not be ** current. ** ** There are a couple of different ways this can happen. All are quite ** obscure. When running in synchronous mode, this can only happen ** if the page is on the free-list at the start of the transaction, then ** populated, then moved using sqlite3PagerMovepage(). ** ** The solution is to add an in-memory page to the cache containing ** the data just read from the sub-journal. Mark the page as dirty ** and if the pager requires a journal-sync, then mark the page as ** requiring a journal-sync before it is written. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) p4 = pPager + 21 *(*Tu8)(unsafe.Pointer(p4)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p4))) | libc.Int32FromInt32(SPILLFLAG_ROLLBACK)) rc = _sqlite3PagerGet(tls, pPager, *(*TPgno)(unsafe.Pointer(bp + 4)), bp, int32(1)) _ = libc.Int32FromInt32(0) p5 = pPager + 21 *(*Tu8)(unsafe.Pointer(p5)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p5))) & ^libc.Int32FromInt32(SPILLFLAG_ROLLBACK)) if rc != SQLITE_OK { return rc } _sqlite3PcacheMakeDirty(tls, *(*uintptr)(unsafe.Pointer(bp))) } } if *(*uintptr)(unsafe.Pointer(bp)) != 0 { pData = (*TPgHdr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpData libc.Xmemcpy(tls, pData, aData, uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize)) (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TPager)(unsafe.Pointer(pPager)).FxReiniter})))(tls, *(*uintptr)(unsafe.Pointer(bp))) /* It used to be that sqlite3PcacheMakeClean(pPg) was called here. But ** that call was dangerous and had no detectable benefit since the cache ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so ** has been removed. */ /* If this was page 1, then restore the value of Pager.dbFileVers. ** Do this before any decoding. */ if *(*TPgno)(unsafe.Pointer(bp + 4)) == uint32(1) { libc.Xmemcpy(tls, pPager+112, pData+24, uint32(16)) } _sqlite3PcacheRelease(tls, *(*uintptr)(unsafe.Pointer(bp))) } return rc } // C documentation // // /* // ** Parameter zSuper is the name of a super-journal file. A single journal // ** file that referred to the super-journal file has just been rolled back. // ** This routine checks if it is possible to delete the super-journal file, // ** and does so if it is. // ** // ** Argument zSuper may point to Pager.pTmpSpace. So that buffer is not // ** available for use within this function. // ** // ** When a super-journal file is created, it is populated with the names // ** of all of its child journals, one after another, formatted as utf-8 // ** encoded text. The end of each child journal file is marked with a // ** nul-terminator byte (0x00). i.e. the entire contents of a super-journal // ** file for a transaction involving two databases might be: // ** // ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00" // ** // ** A super-journal file may only be deleted once all of its child // ** journals have been rolled back. // ** // ** This function reads the contents of the super-journal file into // ** memory and loops through each of the child journal names. For // ** each child journal, it checks if: // ** // ** * if the child journal exists, and if so // ** * if the child journal contains a reference to super-journal // ** file zSuper // ** // ** If a child journal can be found that matches both of the criteria // ** above, this function returns without doing anything. Otherwise, if // ** no such child journal can be found, file zSuper is deleted from // ** the file-system using sqlite3OsDelete(). // ** // ** If an IO error within this function, an error code is returned. This // ** function allocates memory by calling sqlite3Malloc(). If an allocation // ** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors // ** occur, SQLITE_OK is returned. // ** // ** TODO: This function allocates a single block of memory to load // ** the entire contents of the super-journal file. This could be // ** a couple of kilobytes or so - potentially larger than the page // ** size. // */ func _pager_delsuper(tls *libc.TLS, pPager uintptr, zSuper uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var c, flags, flags1, nSuperPtr, rc int32 var pJournal, pSuper, pVfs, zFree, zJournal, zSuperJournal, zSuperPtr uintptr var v1, v2, v3 uint8 var _ /* exists at bp+8 */ int32 var _ /* nSuperJournal at bp+0 */ Ti64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, flags, flags1, nSuperPtr, pJournal, pSuper, pVfs, rc, zFree, zJournal, zSuperJournal, zSuperPtr, v1, v2, v3 pVfs = (*TPager)(unsafe.Pointer(pPager)).FpVfs /* Malloc'd child-journal file descriptor */ zSuperJournal = uintptr(0) /* Space to hold super-journal filename */ zFree = uintptr(0) /* Amount of space allocated to zSuperPtr[] */ /* Allocate space for both the pJournal and pSuper file descriptors. ** If successful, open the super-journal file for reading. */ pSuper = _sqlite3MallocZero(tls, uint64((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FszOsFile*int32(2))) if !(pSuper != 0) { rc = int32(SQLITE_NOMEM) pJournal = uintptr(0) } else { flags = libc.Int32FromInt32(SQLITE_OPEN_READONLY) | libc.Int32FromInt32(SQLITE_OPEN_SUPER_JOURNAL) rc = _sqlite3OsOpen(tls, pVfs, zSuper, pSuper, flags, uintptr(0)) pJournal = pSuper + uintptr((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FszOsFile) } if rc != SQLITE_OK { goto delsuper_out } /* Load the entire super-journal file into space obtained from ** sqlite3_malloc() and pointed to by zSuperJournal. Also obtain ** sufficient space (in zSuperPtr) to hold the names of super-journal ** files extracted from regular rollback-journals. */ rc = _sqlite3OsFileSize(tls, pSuper, bp) if rc != SQLITE_OK { goto delsuper_out } nSuperPtr = (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname + int32(1) zFree = _sqlite3Malloc(tls, uint64(int64(4)+*(*Ti64)(unsafe.Pointer(bp))+int64(nSuperPtr)+int64(2))) if !(zFree != 0) { rc = int32(SQLITE_NOMEM) goto delsuper_out } v3 = libc.Uint8FromInt32(0) *(*uint8)(unsafe.Pointer(zFree + 3)) = v3 v2 = v3 *(*uint8)(unsafe.Pointer(zFree + 2)) = v2 v1 = v2 *(*uint8)(unsafe.Pointer(zFree + 1)) = v1 *(*uint8)(unsafe.Pointer(zFree)) = v1 zSuperJournal = zFree + 4 zSuperPtr = zSuperJournal + uintptr(*(*Ti64)(unsafe.Pointer(bp))+int64(2)) rc = _sqlite3OsRead(tls, pSuper, zSuperJournal, int32(*(*Ti64)(unsafe.Pointer(bp))), 0) if rc != SQLITE_OK { goto delsuper_out } *(*uint8)(unsafe.Pointer(zSuperJournal + uintptr(*(*Ti64)(unsafe.Pointer(bp))))) = uint8(0) *(*uint8)(unsafe.Pointer(zSuperJournal + uintptr(*(*Ti64)(unsafe.Pointer(bp))+int64(1)))) = uint8(0) zJournal = zSuperJournal for int64(int32(zJournal)-int32(zSuperJournal)) < *(*Ti64)(unsafe.Pointer(bp)) { rc = _sqlite3OsAccess(tls, pVfs, zJournal, SQLITE_ACCESS_EXISTS, bp+8) if rc != SQLITE_OK { goto delsuper_out } if *(*int32)(unsafe.Pointer(bp + 8)) != 0 { flags1 = libc.Int32FromInt32(SQLITE_OPEN_READONLY) | libc.Int32FromInt32(SQLITE_OPEN_SUPER_JOURNAL) rc = _sqlite3OsOpen(tls, pVfs, zJournal, pJournal, flags1, uintptr(0)) if rc != SQLITE_OK { goto delsuper_out } rc = _readSuperJournal(tls, pJournal, zSuperPtr, uint32(nSuperPtr)) _sqlite3OsClose(tls, pJournal) if rc != SQLITE_OK { goto delsuper_out } c = libc.BoolInt32(int32(*(*uint8)(unsafe.Pointer(zSuperPtr))) != 0 && libc.Xstrcmp(tls, zSuperPtr, zSuper) == 0) if c != 0 { /* We have a match. Do not delete the super-journal file. */ goto delsuper_out } } zJournal += uintptr(_sqlite3Strlen30(tls, zJournal) + libc.Int32FromInt32(1)) } _sqlite3OsClose(tls, pSuper) rc = _sqlite3OsDelete(tls, pVfs, zSuper, 0) goto delsuper_out delsuper_out: ; Xsqlite3_free(tls, zFree) if pSuper != 0 { _sqlite3OsClose(tls, pSuper) _ = libc.Int32FromInt32(0) Xsqlite3_free(tls, pSuper) } return rc } // C documentation // // /* // ** This function is used to change the actual size of the database // ** file in the file-system. This only happens when committing a transaction, // ** or rolling back a transaction (including rolling back a hot-journal). // ** // ** If the main database file is not open, or the pager is not in either // ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size // ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes). // ** If the file on disk is currently larger than nPage pages, then use the VFS // ** xTruncate() method to truncate it. // ** // ** Or, it might be the case that the file on disk is smaller than // ** nPage pages. Some operating system implementations can get confused if // ** you try to truncate a file to some size that is larger than it // ** currently is, so detect this case and write a single zero byte to // ** the end of the new file instead. // ** // ** If successful, return SQLITE_OK. If an IO error occurs while modifying // ** the database file, return the error code to the caller. // */ func _pager_truncate(tls *libc.TLS, pPager uintptr, nPage TPgno) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pTmp uintptr var rc, szPage int32 var _ /* currentSize at bp+0 */ Ti64 var _ /* newSize at bp+8 */ Ti64 _, _, _ = pTmp, rc, szPage rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != uintptr(0) && (int32((*TPager)(unsafe.Pointer(pPager)).FeState) >= int32(PAGER_WRITER_DBMOD) || int32((*TPager)(unsafe.Pointer(pPager)).FeState) == PAGER_OPEN) { szPage = int32((*TPager)(unsafe.Pointer(pPager)).FpageSize) _ = libc.Int32FromInt32(0) /* TODO: Is it safe to use Pager.dbFileSize here? */ rc = _sqlite3OsFileSize(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, bp) *(*Ti64)(unsafe.Pointer(bp + 8)) = int64(szPage) * int64(nPage) if rc == SQLITE_OK && *(*Ti64)(unsafe.Pointer(bp)) != *(*Ti64)(unsafe.Pointer(bp + 8)) { if *(*Ti64)(unsafe.Pointer(bp)) > *(*Ti64)(unsafe.Pointer(bp + 8)) { rc = _sqlite3OsTruncate(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, *(*Ti64)(unsafe.Pointer(bp + 8))) } else { if *(*Ti64)(unsafe.Pointer(bp))+int64(szPage) <= *(*Ti64)(unsafe.Pointer(bp + 8)) { pTmp = (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace libc.Xmemset(tls, pTmp, 0, uint32(szPage)) _sqlite3OsFileControlHint(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int32(SQLITE_FCNTL_SIZE_HINT), bp+8) rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, pTmp, szPage, *(*Ti64)(unsafe.Pointer(bp + 8))-int64(szPage)) } } if rc == SQLITE_OK { (*TPager)(unsafe.Pointer(pPager)).FdbFileSize = nPage } } } return rc } // C documentation // // /* // ** Return a sanitized version of the sector-size of OS file pFile. The // ** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE. // */ func _sqlite3SectorSize(tls *libc.TLS, pFile uintptr) (r int32) { var iRet int32 _ = iRet iRet = _sqlite3OsSectorSize(tls, pFile) if iRet < int32(32) { iRet = int32(512) } else { if iRet > int32(MAX_SECTOR_SIZE) { _ = libc.Int32FromInt32(0) iRet = int32(MAX_SECTOR_SIZE) } } return iRet } // C documentation // // /* // ** Set the value of the Pager.sectorSize variable for the given // ** pager based on the value returned by the xSectorSize method // ** of the open database file. The sector size will be used // ** to determine the size and alignment of journal header and // ** super-journal pointers within created journal files. // ** // ** For temporary files the effective sector size is always 512 bytes. // ** // ** Otherwise, for non-temporary files, the effective sector size is // ** the value returned by the xSectorSize() method rounded up to 32 if // ** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it // ** is greater than MAX_SECTOR_SIZE. // ** // ** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set // ** the effective sector size to its minimum value (512). The purpose of // ** pPager->sectorSize is to define the "blast radius" of bytes that // ** might change if a crash occurs while writing to a single byte in // ** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero // ** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector // ** size. For backwards compatibility of the rollback journal file format, // ** we cannot reduce the effective sector size below 512. // */ func _setSectorSize(tls *libc.TLS, pPager uintptr) { _ = libc.Int32FromInt32(0) if (*TPager)(unsafe.Pointer(pPager)).FtempFile != 0 || _sqlite3OsDeviceCharacteristics(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd)&int32(SQLITE_IOCAP_POWERSAFE_OVERWRITE) != 0 { /* Sector size doesn't matter for temporary files. Also, the file ** may not have been opened yet, in which case the OsSectorSize() ** call will segfault. */ (*TPager)(unsafe.Pointer(pPager)).FsectorSize = uint32(512) } else { (*TPager)(unsafe.Pointer(pPager)).FsectorSize = uint32(_sqlite3SectorSize(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd)) } } // C documentation // // /* // ** Playback the journal and thus restore the database file to // ** the state it was in before we started making changes. // ** // ** The journal file format is as follows: // ** // ** (1) 8 byte prefix. A copy of aJournalMagic[]. // ** (2) 4 byte big-endian integer which is the number of valid page records // ** in the journal. If this value is 0xffffffff, then compute the // ** number of page records from the journal size. // ** (3) 4 byte big-endian integer which is the initial value for the // ** sanity checksum. // ** (4) 4 byte integer which is the number of pages to truncate the // ** database to during a rollback. // ** (5) 4 byte big-endian integer which is the sector size. The header // ** is this many bytes in size. // ** (6) 4 byte big-endian integer which is the page size. // ** (7) zero padding out to the next sector size. // ** (8) Zero or more pages instances, each as follows: // ** + 4 byte page number. // ** + pPager->pageSize bytes of data. // ** + 4 byte checksum // ** // ** When we speak of the journal header, we mean the first 7 items above. // ** Each entry in the journal is an instance of the 8th item. // ** // ** Call the value from the second bullet "nRec". nRec is the number of // ** valid page entries in the journal. In most cases, you can compute the // ** value of nRec from the size of the journal file. But if a power // ** failure occurred while the journal was being written, it could be the // ** case that the size of the journal file had already been increased but // ** the extra entries had not yet made it safely to disk. In such a case, // ** the value of nRec computed from the file size would be too large. For // ** that reason, we always use the nRec value in the header. // ** // ** If the nRec value is 0xffffffff it means that nRec should be computed // ** from the file size. This value is used when the user selects the // ** no-sync option for the journal. A power failure could lead to corruption // ** in this case. But for things like temporary table (which will be // ** deleted when the power is restored) we don't care. // ** // ** If the file opened as the journal file is not a well-formed // ** journal file then all pages up to the first corrupted page are rolled // ** back (or no pages if the journal header is corrupted). The journal file // ** is then deleted and SQLITE_OK returned, just as if no corruption had // ** been encountered. // ** // ** If an I/O or malloc() error occurs, the journal-file is not deleted // ** and an error code is returned. // ** // ** The isHot parameter indicates that we are trying to rollback a journal // ** that might be a hot journal. Or, it could be that the journal is // ** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE. // ** If the journal really is hot, reset the pager cache prior rolling // ** back any content. If the journal is merely persistent, no reset is // ** needed. // */ func _pager_playback(tls *libc.TLS, pPager uintptr, isHot int32) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var nPlayback, needPagerReset, rc int32 var pVfs, zSuper uintptr var u Tu32 var _ /* mxPg at bp+12 */ TPgno var _ /* nRec at bp+8 */ Tu32 var _ /* res at bp+16 */ int32 var _ /* savedPageSize at bp+20 */ Tu32 var _ /* szJ at bp+0 */ Ti64 _, _, _, _, _, _ = nPlayback, needPagerReset, pVfs, rc, u, zSuper pVfs = (*TPager)(unsafe.Pointer(pPager)).FpVfs /* Unsigned loop counter */ *(*TPgno)(unsafe.Pointer(bp + 12)) = uint32(0) /* Result code of a subroutine */ *(*int32)(unsafe.Pointer(bp + 16)) = int32(1) /* Value returned by sqlite3OsAccess() */ zSuper = uintptr(0) /* True to reset page prior to first page rollback */ nPlayback = 0 /* Total number of pages restored from journal */ *(*Tu32)(unsafe.Pointer(bp + 20)) = uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize) /* Figure out how many records are in the journal. Abort early if ** the journal is empty. */ _ = libc.Int32FromInt32(0) rc = _sqlite3OsFileSize(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, bp) if rc != SQLITE_OK { goto end_playback } /* Read the super-journal name from the journal, if it is present. ** If a super-journal file name is specified, but the file is not ** present on disk, then the journal is not hot and does not need to be ** played back. ** ** TODO: Technically the following is an error because it assumes that ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c, ** mxPathname is 512, which is the same as the minimum allowable value ** for pageSize. */ zSuper = (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace rc = _readSuperJournal(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, zSuper, uint32((*Tsqlite3_vfs)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).FpVfs)).FmxPathname+int32(1))) if rc == SQLITE_OK && *(*uint8)(unsafe.Pointer(zSuper)) != 0 { rc = _sqlite3OsAccess(tls, pVfs, zSuper, SQLITE_ACCESS_EXISTS, bp+16) } zSuper = uintptr(0) if rc != SQLITE_OK || !(*(*int32)(unsafe.Pointer(bp + 16)) != 0) { goto end_playback } (*TPager)(unsafe.Pointer(pPager)).FjournalOff = 0 needPagerReset = isHot /* This loop terminates either when a readJournalHdr() or ** pager_playback_one_page() call returns SQLITE_DONE or an IO error ** occurs. */ for int32(1) != 0 { /* Read the next journal header from the journal file. If there are ** not enough bytes left in the journal file for a complete header, or ** it is corrupted, then a process must have failed while writing it. ** This indicates nothing more needs to be rolled back. */ rc = _readJournalHdr(tls, pPager, isHot, *(*Ti64)(unsafe.Pointer(bp)), bp+8, bp+12) if rc != SQLITE_OK { if rc == int32(SQLITE_DONE) { rc = SQLITE_OK } goto end_playback } /* If nRec is 0xffffffff, then this journal was created by a process ** working in no-sync mode. This means that the rest of the journal ** file consists of pages, there are no more journal headers. Compute ** the value of nRec based on this assumption. */ if *(*Tu32)(unsafe.Pointer(bp + 8)) == uint32(0xffffffff) { _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(bp + 8)) = uint32(int32((*(*Ti64)(unsafe.Pointer(bp)) - int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize)) / ((*TPager)(unsafe.Pointer(pPager)).FpageSize + libc.Int64FromInt32(8)))) } /* If nRec is 0 and this rollback is of a transaction created by this ** process and if this is the final header in the journal, then it means ** that this part of the journal was being filled but has not yet been ** synced to disk. Compute the number of pages based on the remaining ** size of the file. ** ** The third term of the test was added to fix ticket #2565. ** When rolling back a hot journal, nRec==0 always means that the next ** chunk of the journal contains zero pages to be rolled back. But ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in ** the journal, it means that the journal might contain additional ** pages that need to be rolled back and that the number of pages ** should be computed based on the journal file size. */ if *(*Tu32)(unsafe.Pointer(bp + 8)) == uint32(0) && !(isHot != 0) && (*TPager)(unsafe.Pointer(pPager)).FjournalHdr+int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) == (*TPager)(unsafe.Pointer(pPager)).FjournalOff { *(*Tu32)(unsafe.Pointer(bp + 8)) = uint32(int32((*(*Ti64)(unsafe.Pointer(bp)) - (*TPager)(unsafe.Pointer(pPager)).FjournalOff) / ((*TPager)(unsafe.Pointer(pPager)).FpageSize + libc.Int64FromInt32(8)))) } /* If this is the first header read from the journal, truncate the ** database file back to its original size. */ if (*TPager)(unsafe.Pointer(pPager)).FjournalOff == int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) { rc = _pager_truncate(tls, pPager, *(*TPgno)(unsafe.Pointer(bp + 12))) if rc != SQLITE_OK { goto end_playback } (*TPager)(unsafe.Pointer(pPager)).FdbSize = *(*TPgno)(unsafe.Pointer(bp + 12)) if (*TPager)(unsafe.Pointer(pPager)).FmxPgno < *(*TPgno)(unsafe.Pointer(bp + 12)) { (*TPager)(unsafe.Pointer(pPager)).FmxPgno = *(*TPgno)(unsafe.Pointer(bp + 12)) } } /* Copy original pages out of the journal and back into the ** database file and/or page cache. */ u = uint32(0) for { if !(u < *(*Tu32)(unsafe.Pointer(bp + 8))) { break } if needPagerReset != 0 { _pager_reset(tls, pPager) needPagerReset = 0 } rc = _pager_playback_one_page(tls, pPager, pPager+80, uintptr(0), int32(1), 0) if rc == SQLITE_OK { nPlayback++ } else { if rc == int32(SQLITE_DONE) { (*TPager)(unsafe.Pointer(pPager)).FjournalOff = *(*Ti64)(unsafe.Pointer(bp)) break } else { if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(2)<= int32(PAGER_WRITER_DBMOD) || int32((*TPager)(unsafe.Pointer(pPager)).FeState) == PAGER_OPEN) { rc = _sqlite3PagerSync(tls, pPager, uintptr(0)) } if rc == SQLITE_OK { rc = _pager_end_transaction(tls, pPager, libc.BoolInt32(int32(*(*uint8)(unsafe.Pointer(zSuper))) != int32('\000')), 0) } if rc == SQLITE_OK && *(*uint8)(unsafe.Pointer(zSuper)) != 0 && *(*int32)(unsafe.Pointer(bp + 16)) != 0 { /* If there was a super-journal and this routine will return success, ** see if it is possible to delete the super-journal. */ _ = libc.Int32FromInt32(0) libc.Xmemset(tls, (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace, 0, uint32(4)) rc = _pager_delsuper(tls, pPager, zSuper) } if isHot != 0 && nPlayback != 0 { Xsqlite3_log(tls, libc.Int32FromInt32(SQLITE_NOTICE)|libc.Int32FromInt32(2)<pData. A shared lock or greater must be held on the database // ** file before this function is called. // ** // ** If page 1 is read, then the value of Pager.dbFileVers[] is set to // ** the value read from the database file. // ** // ** If an IO error occurs, then the IO error is returned to the caller. // ** Otherwise, SQLITE_OK is returned. // */ func _readDbPage(tls *libc.TLS, pPg uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var dbFileVers, pPager uintptr var iOffset Ti64 var rc int32 var _ /* iFrame at bp+0 */ Tu32 _, _, _, _ = dbFileVers, iOffset, pPager, rc pPager = (*TPgHdr)(unsafe.Pointer(pPg)).FpPager /* Pager object associated with page pPg */ rc = SQLITE_OK /* Return code */ *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) /* Frame of WAL containing pgno */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) { rc = _sqlite3WalFindFrame(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno, bp) if rc != 0 { return rc } } if *(*Tu32)(unsafe.Pointer(bp)) != 0 { rc = _sqlite3WalReadFrame(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, *(*Tu32)(unsafe.Pointer(bp)), int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), (*TPgHdr)(unsafe.Pointer(pPg)).FpData) } else { iOffset = int64((*TPgHdr)(unsafe.Pointer(pPg)).Fpgno-libc.Uint32FromInt32(1)) * (*TPager)(unsafe.Pointer(pPager)).FpageSize rc = _sqlite3OsRead(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, (*TPgHdr)(unsafe.Pointer(pPg)).FpData, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), iOffset) if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(2)<dbFileVers[] with all 0xff bytes should suffice. ** ** For an encrypted database, the situation is more complex: bytes ** 24..39 of the database are white noise. But the probability of ** white noise equaling 16 bytes of 0xff is vanishingly small so ** we should still be ok. */ libc.Xmemset(tls, pPager+112, int32(0xff), uint32(16)) } else { dbFileVers = (*TPgHdr)(unsafe.Pointer(pPg)).FpData + 24 libc.Xmemcpy(tls, pPager+112, dbFileVers, uint32(16)) } } return rc } // C documentation // // /* // ** Update the value of the change-counter at offsets 24 and 92 in // ** the header and the sqlite version number at offset 96. // ** // ** This is an unconditional update. See also the pager_incr_changecounter() // ** routine which only updates the change-counter if the update is actually // ** needed, as determined by the pPager->changeCountDone state variable. // */ func _pager_write_changecounter(tls *libc.TLS, pPg uintptr) { var change_counter Tu32 _ = change_counter if pPg == uintptr(0) { return } /* Increment the value just read and write it back to byte 24. */ change_counter = _sqlite3Get4byte(tls, (*TPgHdr)(unsafe.Pointer(pPg)).FpPager+112) + uint32(1) _sqlite3Put4byte(tls, (*TPgHdr)(unsafe.Pointer(pPg)).FpData+uintptr(24), change_counter) /* Also store the SQLite version number in bytes 96..99 and in ** bytes 92..95 store the change counter for which the version number ** is valid. */ _sqlite3Put4byte(tls, (*TPgHdr)(unsafe.Pointer(pPg)).FpData+uintptr(92), change_counter) _sqlite3Put4byte(tls, (*TPgHdr)(unsafe.Pointer(pPg)).FpData+uintptr(96), uint32(SQLITE_VERSION_NUMBER)) } // C documentation // // /* // ** This function is invoked once for each page that has already been // ** written into the log file when a WAL transaction is rolled back. // ** Parameter iPg is the page number of said page. The pCtx argument // ** is actually a pointer to the Pager structure. // ** // ** If page iPg is present in the cache, and has no outstanding references, // ** it is discarded. Otherwise, if there are one or more outstanding // ** references, the page content is reloaded from the database. If the // ** attempt to reload content from the database is required and fails, // ** return an SQLite error code. Otherwise, SQLITE_OK. // */ func _pagerUndoCallback(tls *libc.TLS, pCtx uintptr, iPg TPgno) (r int32) { var pPager, pPg uintptr var rc int32 _, _, _ = pPager, pPg, rc rc = SQLITE_OK pPager = pCtx _ = libc.Int32FromInt32(0) pPg = _sqlite3PagerLookup(tls, pPager, iPg) if pPg != 0 { if _sqlite3PcachePageRefcount(tls, pPg) == int64(1) { _sqlite3PcacheDrop(tls, pPg) } else { rc = _readDbPage(tls, pPg) if rc == SQLITE_OK { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TPager)(unsafe.Pointer(pPager)).FxReiniter})))(tls, pPg) } _sqlite3PagerUnrefNotNull(tls, pPg) } } /* Normally, if a transaction is rolled back, any backup processes are ** updated as data is copied out of the rollback journal and into the ** database. This is not generally possible with a WAL database, as ** rollback involves simply truncating the log file. Therefore, if one ** or more frames have already been written to the log (and therefore ** also copied into the backup databases) as part of this transaction, ** the backups must be restarted. */ _sqlite3BackupRestart(tls, (*TPager)(unsafe.Pointer(pPager)).FpBackup) return rc } // C documentation // // /* // ** This function is called to rollback a transaction on a WAL database. // */ func _pagerRollbackWal(tls *libc.TLS, pPager uintptr) (r int32) { var pList, pNext uintptr var rc int32 _, _, _ = pList, pNext, rc /* List of dirty pages to revert */ /* For all pages in the cache that are currently dirty or have already ** been written (but not committed) to the log file, do one of the ** following: ** ** + Discard the cached page (if refcount==0), or ** + Reload page content from the database (if refcount>0). */ (*TPager)(unsafe.Pointer(pPager)).FdbSize = (*TPager)(unsafe.Pointer(pPager)).FdbOrigSize rc = _sqlite3WalUndo(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, __ccgo_fp(_pagerUndoCallback), pPager) pList = _sqlite3PcacheDirtyList(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) for pList != 0 && rc == SQLITE_OK { pNext = (*TPgHdr)(unsafe.Pointer(pList)).FpDirty rc = _pagerUndoCallback(tls, pPager, (*TPgHdr)(unsafe.Pointer(pList)).Fpgno) pList = pNext } return rc } // C documentation // // /* // ** This function is a wrapper around sqlite3WalFrames(). As well as logging // ** the contents of the list of pages headed by pList (connected by pDirty), // ** this function notifies any active backup processes that the pages have // ** changed. // ** // ** The list of pages passed into this routine is always sorted by page number. // ** Hence, if page 1 appears anywhere on the list, it will be the first page. // */ func _pagerWalFrames(tls *libc.TLS, pPager uintptr, _pList uintptr, nTruncate TPgno, isCommit int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) *(*uintptr)(unsafe.Pointer(bp)) = _pList var nList, rc int32 var p, ppNext, v2 uintptr _, _, _, _, _ = nList, p, ppNext, rc, v2 /* For looping over pages */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if isCommit != 0 { /* If a WAL transaction is being committed, there is no point in writing ** any pages with page numbers greater than nTruncate into the WAL file. ** They will never be read by any client. So remove them from the pDirty ** list here. */ ppNext = bp nList = 0 p = *(*uintptr)(unsafe.Pointer(bp)) for { v2 = p *(*uintptr)(unsafe.Pointer(ppNext)) = v2 if !(v2 != uintptr(0)) { break } if (*TPgHdr)(unsafe.Pointer(p)).Fpgno <= nTruncate { ppNext = p + 16 nList++ } goto _1 _1: ; p = (*TPgHdr)(unsafe.Pointer(p)).FpDirty } _ = libc.Int32FromInt32(0) } else { nList = int32(1) } *(*Tu32)(unsafe.Pointer(pPager + 200 + 2*4)) += uint32(nList) if (*TPgHdr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fpgno == uint32(1) { _pager_write_changecounter(tls, *(*uintptr)(unsafe.Pointer(bp))) } rc = _sqlite3WalFrames(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), *(*uintptr)(unsafe.Pointer(bp)), nTruncate, isCommit, int32((*TPager)(unsafe.Pointer(pPager)).FwalSyncFlags)) if rc == SQLITE_OK && (*TPager)(unsafe.Pointer(pPager)).FpBackup != 0 { p = *(*uintptr)(unsafe.Pointer(bp)) for { if !(p != 0) { break } _sqlite3BackupUpdate(tls, (*TPager)(unsafe.Pointer(pPager)).FpBackup, (*TPgHdr)(unsafe.Pointer(p)).Fpgno, (*TPgHdr)(unsafe.Pointer(p)).FpData) goto _3 _3: ; p = (*TPgHdr)(unsafe.Pointer(p)).FpDirty } } return rc } // C documentation // // /* // ** Begin a read transaction on the WAL. // ** // ** This routine used to be called "pagerOpenSnapshot()" because it essentially // ** makes a snapshot of the database at the current point in time and preserves // ** that snapshot for use by the reader in spite of concurrently changes by // ** other writers or checkpointers. // */ func _pagerBeginReadTransaction(tls *libc.TLS, pPager uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* changed at bp+0 */ int32 _ = rc /* Return code */ *(*int32)(unsafe.Pointer(bp)) = 0 /* True if cache must be reset */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* sqlite3WalEndReadTransaction() was not called for the previous ** transaction in locking_mode=EXCLUSIVE. So call it now. If we ** are in locking_mode=NORMAL and EndRead() was previously called, ** the duplicate call is harmless. */ _sqlite3WalEndReadTransaction(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal) rc = _sqlite3WalBeginReadTransaction(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, bp) if rc != SQLITE_OK || *(*int32)(unsafe.Pointer(bp)) != 0 { _pager_reset(tls, pPager) if (*TPager)(unsafe.Pointer(pPager)).FbUseFetch != 0 { _sqlite3OsUnfetch(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, 0, uintptr(0)) } } return rc } // C documentation // // /* // ** This function is called as part of the transition from PAGER_OPEN // ** to PAGER_READER state to determine the size of the database file // ** in pages (assuming the page size currently stored in Pager.pageSize). // ** // ** If no error occurs, SQLITE_OK is returned and the size of the database // ** in pages is stored in *pnPage. Otherwise, an error code (perhaps // ** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified. // */ func _pagerPagecount(tls *libc.TLS, pPager uintptr, pnPage uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var nPage TPgno var rc int32 var _ /* n at bp+0 */ Ti64 _, _ = nPage, rc /* Value to return via *pnPage */ /* Query the WAL sub-system for the database size. The WalDbsize() ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or ** if the database size is not available. The database size is not ** available from the WAL sub-system if the log file is empty or ** contains no valid committed transactions. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) nPage = _sqlite3WalDbsize(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal) /* If the number of pages in the database is not available from the ** WAL sub-system, determine the page count based on the size of ** the database file. If the size of the database file is not an ** integer multiple of the page-size, round up the result. */ if nPage == uint32(0) && (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != uintptr(0) { *(*Ti64)(unsafe.Pointer(bp)) = 0 /* Size of db file in bytes */ rc = _sqlite3OsFileSize(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, bp) if rc != SQLITE_OK { return rc } nPage = uint32((*(*Ti64)(unsafe.Pointer(bp)) + (*TPager)(unsafe.Pointer(pPager)).FpageSize - libc.Int64FromInt32(1)) / (*TPager)(unsafe.Pointer(pPager)).FpageSize) } /* If the current number of pages in the file is greater than the ** configured maximum pager number, increase the allowed limit so ** that the file can be read. */ if nPage > (*TPager)(unsafe.Pointer(pPager)).FmxPgno { (*TPager)(unsafe.Pointer(pPager)).FmxPgno = nPage } *(*TPgno)(unsafe.Pointer(pnPage)) = nPage return SQLITE_OK } // C documentation // // /* // ** Check if the *-wal file that corresponds to the database opened by pPager // ** exists if the database is not empty, or verify that the *-wal file does // ** not exist (by deleting it) if the database file is empty. // ** // ** If the database is not empty and the *-wal file exists, open the pager // ** in WAL mode. If the database is empty or if no *-wal file exists and // ** if no error occurs, make sure Pager.journalMode is not set to // ** PAGER_JOURNALMODE_WAL. // ** // ** Return SQLITE_OK or an error code. // ** // ** The caller must hold a SHARED lock on the database file to call this // ** function. Because an EXCLUSIVE lock on the db file is required to delete // ** a WAL on a none-empty database, this ensures there is no race condition // ** between the xAccess() below and an xDelete() being executed by some // ** other connection. // */ func _pagerOpenWalIfPresent(tls *libc.TLS, pPager uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* isWal at bp+0 */ int32 var _ /* nPage at bp+4 */ TPgno _ = rc rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if !((*TPager)(unsafe.Pointer(pPager)).FtempFile != 0) { /* True if WAL file exists */ rc = _sqlite3OsAccess(tls, (*TPager)(unsafe.Pointer(pPager)).FpVfs, (*TPager)(unsafe.Pointer(pPager)).FzWal, SQLITE_ACCESS_EXISTS, bp) if rc == SQLITE_OK { if *(*int32)(unsafe.Pointer(bp)) != 0 { /* Size of the database file */ rc = _pagerPagecount(tls, pPager, bp+4) if rc != 0 { return rc } if *(*TPgno)(unsafe.Pointer(bp + 4)) == uint32(0) { rc = _sqlite3OsDelete(tls, (*TPager)(unsafe.Pointer(pPager)).FpVfs, (*TPager)(unsafe.Pointer(pPager)).FzWal, 0) } else { rc = _sqlite3PagerOpenWal(tls, pPager, uintptr(0)) } } else { if int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) == int32(PAGER_JOURNALMODE_WAL) { (*TPager)(unsafe.Pointer(pPager)).FjournalMode = uint8(PAGER_JOURNALMODE_DELETE) } } } } return rc } // C documentation // // /* // ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback // ** the entire super-journal file. The case pSavepoint==NULL occurs when // ** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction // ** savepoint. // ** // ** When pSavepoint is not NULL (meaning a non-transaction savepoint is // ** being rolled back), then the rollback consists of up to three stages, // ** performed in the order specified: // ** // ** * Pages are played back from the main journal starting at byte // ** offset PagerSavepoint.iOffset and continuing to // ** PagerSavepoint.iHdrOffset, or to the end of the main journal // ** file if PagerSavepoint.iHdrOffset is zero. // ** // ** * If PagerSavepoint.iHdrOffset is not zero, then pages are played // ** back starting from the journal header immediately following // ** PagerSavepoint.iHdrOffset to the end of the main journal file. // ** // ** * Pages are then played back from the sub-journal file, starting // ** with the PagerSavepoint.iSubRec and continuing to the end of // ** the journal file. // ** // ** Throughout the rollback process, each time a page is rolled back, the // ** corresponding bit is set in a bitvec structure (variable pDone in the // ** implementation below). This is used to ensure that a page is only // ** rolled back the first time it is encountered in either journal. // ** // ** If pSavepoint is NULL, then pages are only played back from the main // ** journal file. There is no need for a bitvec in this case. // ** // ** In either case, before playback commences the Pager.dbSize variable // ** is reset to the value that it held at the start of the savepoint // ** (or transaction). No page with a page-number greater than this value // ** is played back. If one is encountered it is simply skipped. // */ func _pagerPlaybackSavepoint(tls *libc.TLS, pPager uintptr, pSavepoint uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iHdrOff, szJ Ti64 var ii, ii1 Tu32 var pDone uintptr var rc int32 var v1 uint32 var v2 int64 var _ /* dummy at bp+4 */ Tu32 var _ /* nJRec at bp+0 */ Tu32 var _ /* offset at bp+8 */ Ti64 _, _, _, _, _, _, _, _ = iHdrOff, ii, ii1, pDone, rc, szJ, v1, v2 /* End of first segment of main-journal records */ rc = SQLITE_OK /* Return code */ pDone = uintptr(0) /* Bitvec to ensure pages played back only once */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Allocate a bitvec to use to store the set of pages rolled back */ if pSavepoint != 0 { pDone = _sqlite3BitvecCreate(tls, (*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FnOrig) if !(pDone != 0) { return int32(SQLITE_NOMEM) } } /* Set the database size back to the value it was before the savepoint ** being reverted was opened. */ if pSavepoint != 0 { v1 = (*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FnOrig } else { v1 = (*TPager)(unsafe.Pointer(pPager)).FdbOrigSize } (*TPager)(unsafe.Pointer(pPager)).FdbSize = v1 (*TPager)(unsafe.Pointer(pPager)).FchangeCountDone = (*TPager)(unsafe.Pointer(pPager)).FtempFile if !(pSavepoint != 0) && (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) { return _pagerRollbackWal(tls, pPager) } /* Use pPager->journalOff as the effective size of the main rollback ** journal. The actual file might be larger than this in ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything ** past pPager->journalOff is off-limits to us. */ szJ = (*TPager)(unsafe.Pointer(pPager)).FjournalOff _ = libc.Int32FromInt32(0) /* Begin by rolling back records from the main journal starting at ** PagerSavepoint.iOffset and continuing to the next journal header. ** There might be records in the main journal that have a page number ** greater than the current database size (pPager->dbSize) but those ** will be skipped automatically. Pages are added to pDone as they ** are played back. */ if pSavepoint != 0 && !((*TPager)(unsafe.Pointer(pPager)).FpWal != libc.UintptrFromInt32(0)) { if (*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FiHdrOffset != 0 { v2 = (*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FiHdrOffset } else { v2 = szJ } iHdrOff = v2 (*TPager)(unsafe.Pointer(pPager)).FjournalOff = (*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FiOffset for rc == SQLITE_OK && (*TPager)(unsafe.Pointer(pPager)).FjournalOff < iHdrOff { rc = _pager_playback_one_page(tls, pPager, pPager+80, pDone, int32(1), int32(1)) } _ = libc.Int32FromInt32(0) } else { (*TPager)(unsafe.Pointer(pPager)).FjournalOff = 0 } /* Continue rolling back records out of the main journal starting at ** the first journal header seen and continuing until the effective end ** of the main journal file. Continue to skip out-of-range pages and ** continue adding pages rolled back to pDone. */ for rc == SQLITE_OK && (*TPager)(unsafe.Pointer(pPager)).FjournalOff < szJ { /* Loop counter */ *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) rc = _readJournalHdr(tls, pPager, 0, szJ, bp, bp+4) _ = libc.Int32FromInt32(0) /* ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff" ** test is related to ticket #2565. See the discussion in the ** pager_playback() function for additional information. */ if *(*Tu32)(unsafe.Pointer(bp)) == uint32(0) && (*TPager)(unsafe.Pointer(pPager)).FjournalHdr+int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) == (*TPager)(unsafe.Pointer(pPager)).FjournalOff { *(*Tu32)(unsafe.Pointer(bp)) = uint32((szJ - (*TPager)(unsafe.Pointer(pPager)).FjournalOff) / ((*TPager)(unsafe.Pointer(pPager)).FpageSize + libc.Int64FromInt32(8))) } ii = uint32(0) for { if !(rc == SQLITE_OK && ii < *(*Tu32)(unsafe.Pointer(bp)) && (*TPager)(unsafe.Pointer(pPager)).FjournalOff < szJ) { break } rc = _pager_playback_one_page(tls, pPager, pPager+80, pDone, int32(1), int32(1)) goto _3 _3: ; ii++ } _ = libc.Int32FromInt32(0) } _ = libc.Int32FromInt32(0) /* Finally, rollback pages from the sub-journal. Page that were ** previously rolled back out of the main journal (and are hence in pDone) ** will be skipped. Out-of-range pages are also skipped. */ if pSavepoint != 0 { /* Loop counter */ *(*Ti64)(unsafe.Pointer(bp + 8)) = int64((*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FiSubRec) * (int64(4) + (*TPager)(unsafe.Pointer(pPager)).FpageSize) if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) { rc = _sqlite3WalSavepointUndo(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, pSavepoint+32) } ii1 = (*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FiSubRec for { if !(rc == SQLITE_OK && ii1 < (*TPager)(unsafe.Pointer(pPager)).FnSubRec) { break } _ = libc.Int32FromInt32(0) rc = _pager_playback_one_page(tls, pPager, bp+8, pDone, 0, int32(1)) goto _4 _4: ; ii1++ } _ = libc.Int32FromInt32(0) } _sqlite3BitvecDestroy(tls, pDone) if rc == SQLITE_OK { (*TPager)(unsafe.Pointer(pPager)).FjournalOff = szJ } return rc } // C documentation // // /* // ** Change the maximum number of in-memory pages that are allowed // ** before attempting to recycle clean and unused pages. // */ func _sqlite3PagerSetCachesize(tls *libc.TLS, pPager uintptr, mxPage int32) { _sqlite3PcacheSetCachesize(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache, mxPage) } // C documentation // // /* // ** Change the maximum number of in-memory pages that are allowed // ** before attempting to spill pages to journal. // */ func _sqlite3PagerSetSpillsize(tls *libc.TLS, pPager uintptr, mxPage int32) (r int32) { return _sqlite3PcacheSetSpillsize(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache, mxPage) } // C documentation // // /* // ** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap. // */ func _pagerFixMaplimit(tls *libc.TLS, pPager uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var fd uintptr var _ /* sz at bp+0 */ Tsqlite3_int64 _ = fd fd = (*TPager)(unsafe.Pointer(pPager)).Ffd if (*Tsqlite3_file)(unsafe.Pointer(fd)).FpMethods != uintptr(0) && (*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(fd)).FpMethods)).FiVersion >= int32(3) { *(*Tsqlite3_int64)(unsafe.Pointer(bp)) = (*TPager)(unsafe.Pointer(pPager)).FszMmap (*TPager)(unsafe.Pointer(pPager)).FbUseFetch = libc.BoolUint8(*(*Tsqlite3_int64)(unsafe.Pointer(bp)) > libc.Int64FromInt32(0)) _setGetterMethod(tls, pPager) _sqlite3OsFileControlHint(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int32(SQLITE_FCNTL_MMAP_SIZE), bp) } } // C documentation // // /* // ** Change the maximum size of any memory mapping made of the database file. // */ func _sqlite3PagerSetMmapLimit(tls *libc.TLS, pPager uintptr, szMmap Tsqlite3_int64) { (*TPager)(unsafe.Pointer(pPager)).FszMmap = szMmap _pagerFixMaplimit(tls, pPager) } // C documentation // // /* // ** Free as much memory as possible from the pager. // */ func _sqlite3PagerShrink(tls *libc.TLS, pPager uintptr) { _sqlite3PcacheShrink(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) } // C documentation // // /* // ** Adjust settings of the pager to those specified in the pgFlags parameter. // ** // ** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness // ** of the database to damage due to OS crashes or power failures by // ** changing the number of syncs()s when writing the journals. // ** There are four levels: // ** // ** OFF sqlite3OsSync() is never called. This is the default // ** for temporary and transient files. // ** // ** NORMAL The journal is synced once before writes begin on the // ** database. This is normally adequate protection, but // ** it is theoretically possible, though very unlikely, // ** that an inopertune power failure could leave the journal // ** in a state which would cause damage to the database // ** when it is rolled back. // ** // ** FULL The journal is synced twice before writes begin on the // ** database (with some additional information - the nRec field // ** of the journal header - being written in between the two // ** syncs). If we assume that writing a // ** single disk sector is atomic, then this mode provides // ** assurance that the journal will not be corrupted to the // ** point of causing damage to the database during rollback. // ** // ** EXTRA This is like FULL except that is also syncs the directory // ** that contains the rollback journal after the rollback // ** journal is unlinked. // ** // ** The above is for a rollback-journal mode. For WAL mode, OFF continues // ** to mean that no syncs ever occur. NORMAL means that the WAL is synced // ** prior to the start of checkpoint and that the database file is synced // ** at the conclusion of the checkpoint if the entire content of the WAL // ** was written back into the database. But no sync operations occur for // ** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL // ** file is synced following each commit operation, in addition to the // ** syncs associated with NORMAL. There is no difference between FULL // ** and EXTRA for WAL mode. // ** // ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The // ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync // ** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an // ** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL // ** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the // ** synchronous=FULL versus synchronous=NORMAL setting determines when // ** the xSync primitive is called and is relevant to all platforms. // ** // ** Numeric values associated with these states are OFF==1, NORMAL=2, // ** and FULL=3. // */ func _sqlite3PagerSetFlags(tls *libc.TLS, pPager uintptr, pgFlags uint32) { var level uint32 var v1, v2, v3 int32 var p4, p5, p6, p7 uintptr _, _, _, _, _, _, _, _ = level, v1, v2, v3, p4, p5, p6, p7 level = pgFlags & uint32(PAGER_SYNCHRONOUS_MASK) if (*TPager)(unsafe.Pointer(pPager)).FtempFile != 0 { (*TPager)(unsafe.Pointer(pPager)).FnoSync = uint8(1) (*TPager)(unsafe.Pointer(pPager)).FfullSync = uint8(0) (*TPager)(unsafe.Pointer(pPager)).FextraSync = uint8(0) } else { if level == uint32(PAGER_SYNCHRONOUS_OFF) { v1 = int32(1) } else { v1 = 0 } (*TPager)(unsafe.Pointer(pPager)).FnoSync = uint8(v1) if level >= uint32(PAGER_SYNCHRONOUS_FULL) { v2 = int32(1) } else { v2 = 0 } (*TPager)(unsafe.Pointer(pPager)).FfullSync = uint8(v2) if level == uint32(PAGER_SYNCHRONOUS_EXTRA) { v3 = int32(1) } else { v3 = 0 } (*TPager)(unsafe.Pointer(pPager)).FextraSync = uint8(v3) } if (*TPager)(unsafe.Pointer(pPager)).FnoSync != 0 { (*TPager)(unsafe.Pointer(pPager)).FsyncFlags = uint8(0) } else { if pgFlags&uint32(PAGER_FULLFSYNC) != 0 { (*TPager)(unsafe.Pointer(pPager)).FsyncFlags = uint8(SQLITE_SYNC_FULL) } else { (*TPager)(unsafe.Pointer(pPager)).FsyncFlags = uint8(SQLITE_SYNC_NORMAL) } } (*TPager)(unsafe.Pointer(pPager)).FwalSyncFlags = uint8(int32((*TPager)(unsafe.Pointer(pPager)).FsyncFlags) << libc.Int32FromInt32(2)) if (*TPager)(unsafe.Pointer(pPager)).FfullSync != 0 { p4 = pPager + 11 *(*Tu8)(unsafe.Pointer(p4)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p4))) | int32((*TPager)(unsafe.Pointer(pPager)).FsyncFlags)) } if pgFlags&uint32(PAGER_CKPT_FULLFSYNC) != 0 && !((*TPager)(unsafe.Pointer(pPager)).FnoSync != 0) { p5 = pPager + 11 *(*Tu8)(unsafe.Pointer(p5)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p5))) | libc.Int32FromInt32(SQLITE_SYNC_FULL)< SHARED_LOCK | Yes // ** SHARED_LOCK -> RESERVED_LOCK | No // ** SHARED_LOCK -> EXCLUSIVE_LOCK | No // ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes // ** // ** If the busy-handler callback returns non-zero, the lock is // ** retried. If it returns zero, then the SQLITE_BUSY error is // ** returned to the caller of the pager API function. // */ func _sqlite3PagerSetBusyHandler(tls *libc.TLS, pPager uintptr, xBusyHandler uintptr, pBusyHandlerArg uintptr) { var ap uintptr _ = ap (*TPager)(unsafe.Pointer(pPager)).FxBusyHandler = xBusyHandler (*TPager)(unsafe.Pointer(pPager)).FpBusyHandlerArg = pBusyHandlerArg ap = pPager + 192 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3OsFileControlHint(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int32(SQLITE_FCNTL_BUSYHANDLER), ap) } // C documentation // // /* // ** Change the page size used by the Pager object. The new page size // ** is passed in *pPageSize. // ** // ** If the pager is in the error state when this function is called, it // ** is a no-op. The value returned is the error state error code (i.e. // ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL). // ** // ** Otherwise, if all of the following are true: // ** // ** * the new page size (value of *pPageSize) is valid (a power // ** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and // ** // ** * there are no outstanding page references, and // ** // ** * the database is either not an in-memory database or it is // ** an in-memory database that currently consists of zero pages. // ** // ** then the pager object page size is set to *pPageSize. // ** // ** If the page size is changed, then this function uses sqlite3PagerMalloc() // ** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt // ** fails, SQLITE_NOMEM is returned and the page size remains unchanged. // ** In all other cases, SQLITE_OK is returned. // ** // ** If the page size is not changed, either because one of the enumerated // ** conditions above is not true, the pager was in error state when this // ** function was called, or because the memory allocation attempt failed, // ** then *pPageSize is set to the old, retained page size before returning. // */ func _sqlite3PagerSetPagesize(tls *libc.TLS, pPager uintptr, pPageSize uintptr, nReserve int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pNew uintptr var pageSize Tu32 var rc int32 var _ /* nByte at bp+0 */ Ti64 _, _, _ = pNew, pageSize, rc rc = SQLITE_OK /* It is not possible to do a full assert_pager_state() here, as this ** function may be called from within PagerOpen(), before the state ** of the Pager object is internally consistent. ** ** At one point this function returned an error if the pager was in ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that ** there is at least one outstanding page reference, this function ** is a no-op for that case anyhow. */ pageSize = *(*Tu32)(unsafe.Pointer(pPageSize)) _ = libc.Int32FromInt32(0) if (int32((*TPager)(unsafe.Pointer(pPager)).FmemDb) == 0 || (*TPager)(unsafe.Pointer(pPager)).FdbSize == uint32(0)) && _sqlite3PcacheRefCount(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) == 0 && pageSize != 0 && pageSize != uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize) { pNew = libc.UintptrFromInt32(0) /* New temp space */ *(*Ti64)(unsafe.Pointer(bp)) = 0 if int32((*TPager)(unsafe.Pointer(pPager)).FeState) > PAGER_OPEN && (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != uintptr(0) { rc = _sqlite3OsFileSize(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, bp) } if rc == SQLITE_OK { /* 8 bytes of zeroed overrun space is sufficient so that the b-tree * cell header parser will never run off the end of the allocation */ pNew = _sqlite3PageMalloc(tls, int32(pageSize+uint32(8))) if !(pNew != 0) { rc = int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, pNew+uintptr(pageSize), 0, uint32(8)) } } if rc == SQLITE_OK { _pager_reset(tls, pPager) rc = _sqlite3PcacheSetPageSize(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache, int32(pageSize)) } if rc == SQLITE_OK { _sqlite3PageFree(tls, (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace) (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace = pNew (*TPager)(unsafe.Pointer(pPager)).FdbSize = uint32((*(*Ti64)(unsafe.Pointer(bp)) + int64(pageSize) - libc.Int64FromInt32(1)) / int64(pageSize)) (*TPager)(unsafe.Pointer(pPager)).FpageSize = int64(pageSize) (*TPager)(unsafe.Pointer(pPager)).FlckPgno = uint32(_sqlite3PendingByte)/pageSize + uint32(1) } else { _sqlite3PageFree(tls, pNew) } } *(*Tu32)(unsafe.Pointer(pPageSize)) = uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize) if rc == SQLITE_OK { if nReserve < 0 { nReserve = int32((*TPager)(unsafe.Pointer(pPager)).FnReserve) } _ = libc.Int32FromInt32(0) (*TPager)(unsafe.Pointer(pPager)).FnReserve = int16(nReserve) _pagerFixMaplimit(tls, pPager) } return rc } // C documentation // // /* // ** Return a pointer to the "temporary page" buffer held internally // ** by the pager. This is a buffer that is big enough to hold the // ** entire content of a database page. This buffer is used internally // ** during rollback and will be overwritten whenever a rollback // ** occurs. But other modules are free to use it too, as long as // ** no rollbacks are happening. // */ func _sqlite3PagerTempSpace(tls *libc.TLS, pPager uintptr) (r uintptr) { return (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace } // C documentation // // /* // ** Attempt to set the maximum database page count if mxPage is positive. // ** Make no changes if mxPage is zero or negative. And never reduce the // ** maximum page count below the current size of the database. // ** // ** Regardless of mxPage, return the current maximum page count. // */ func _sqlite3PagerMaxPageCount(tls *libc.TLS, pPager uintptr, mxPage TPgno) (r TPgno) { if mxPage > uint32(0) { (*TPager)(unsafe.Pointer(pPager)).FmxPgno = mxPage } _ = libc.Int32FromInt32(0) /* Called only by OP_MaxPgcnt */ /* assert( pPager->mxPgno>=pPager->dbSize ); */ /* OP_MaxPgcnt ensures that the parameter passed to this function is not ** less than the total number of valid pages in the database. But this ** may be less than Pager.dbSize, and so the assert() above is not valid */ return (*TPager)(unsafe.Pointer(pPager)).FmxPgno } /* ** The following set of routines are used to disable the simulated ** I/O error mechanism. These routines are used to avoid simulated ** errors in places where we do not care about errors. ** ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops ** and generate no code. */ // C documentation // // /* // ** Read the first N bytes from the beginning of the file into memory // ** that pDest points to. // ** // ** If the pager was opened on a transient file (zFilename==""), or // ** opened on a file less than N bytes in size, the output buffer is // ** zeroed and SQLITE_OK returned. The rationale for this is that this // ** function is used to read database headers, and a new transient or // ** zero sized database has a header than consists entirely of zeroes. // ** // ** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered, // ** the error code is returned to the caller and the contents of the // ** output buffer undefined. // */ func _sqlite3PagerReadFileheader(tls *libc.TLS, pPager uintptr, N int32, pDest uintptr) (r int32) { var rc int32 _ = rc rc = SQLITE_OK libc.Xmemset(tls, pDest, 0, uint32(N)) _ = libc.Int32FromInt32(0) /* This routine is only called by btree immediately after creating ** the Pager object. There has not been an opportunity to transition ** to WAL mode yet. */ _ = libc.Int32FromInt32(0) if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != uintptr(0) { rc = _sqlite3OsRead(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, pDest, N, 0) if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(2)< bytes in size, then // ** this is considered a 1 page file. // */ func _sqlite3PagerPagecount(tls *libc.TLS, pPager uintptr, pnPage uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pnPage)) = int32((*TPager)(unsafe.Pointer(pPager)).FdbSize) } // C documentation // // /* // ** Try to obtain a lock of type locktype on the database file. If // ** a similar or greater lock is already held, this function is a no-op // ** (returning SQLITE_OK immediately). // ** // ** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke // ** the busy callback if the lock is currently not available. Repeat // ** until the busy callback returns false or until the attempt to // ** obtain the lock succeeds. // ** // ** Return SQLITE_OK on success and an error code if we cannot obtain // ** the lock. If the lock is obtained successfully, set the Pager.state // ** variable to locktype before returning. // */ func _pager_wait_on_lock(tls *libc.TLS, pPager uintptr, locktype int32) (r int32) { var rc int32 _ = rc /* Return code */ /* Check that this is either a no-op (because the requested lock is ** already held), or one of the transitions that the busy-handler ** may be invoked during, according to the comment above ** sqlite3PagerSetBusyhandler(). */ _ = libc.Int32FromInt32(0) for cond := true; cond; cond = rc == int32(SQLITE_BUSY) && (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TPager)(unsafe.Pointer(pPager)).FxBusyHandler})))(tls, (*TPager)(unsafe.Pointer(pPager)).FpBusyHandlerArg) != 0 { rc = _pagerLockDb(tls, pPager, locktype) } return rc } /* ** Function assertTruncateConstraint(pPager) checks that one of the ** following is true for all dirty pages currently in the page-cache: ** ** a) The page number is less than or equal to the size of the ** current database image, in pages, OR ** ** b) if the page content were written at this time, it would not ** be necessary to write the current content out to the sub-journal. ** ** If the condition asserted by this function were not true, and the ** dirty page were to be discarded from the cache via the pagerStress() ** routine, pagerStress() would not write the current page content to ** the database file. If a savepoint transaction were rolled back after ** this happened, the correct behavior would be to restore the current ** content of the page. However, since this content is not present in either ** the database file or the portion of the rollback journal and ** sub-journal rolled back the content could not be restored and the ** database image would become corrupt. It is therefore fortunate that ** this circumstance cannot arise. */ // C documentation // // /* // ** Truncate the in-memory database file image to nPage pages. This // ** function does not actually modify the database file on disk. It // ** just sets the internal state of the pager object so that the // ** truncation will be done when the current transaction is committed. // ** // ** This function is only called right before committing a transaction. // ** Once this function has been called, the transaction must either be // ** rolled back or committed. It is not safe to call this function and // ** then continue writing to the database. // */ func _sqlite3PagerTruncateImage(tls *libc.TLS, pPager uintptr, nPage TPgno) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TPager)(unsafe.Pointer(pPager)).FdbSize = nPage /* At one point the code here called assertTruncateConstraint() to ** ensure that all pages being truncated away by this operation are, ** if one or more savepoints are open, present in the savepoint ** journal so that they can be restored if the savepoint is rolled ** back. This is no longer necessary as this function is now only ** called right before committing a transaction. So although the ** Pager object may still have open savepoints (Pager.nSavepoint!=0), ** they cannot be rolled back. So the assertTruncateConstraint() call ** is no longer correct. */ } // C documentation // // /* // ** This function is called before attempting a hot-journal rollback. It // ** syncs the journal file to disk, then sets pPager->journalHdr to the // ** size of the journal file so that the pager_playback() routine knows // ** that the entire journal file has been synced. // ** // ** Syncing a hot-journal to disk before attempting to roll it back ensures // ** that if a power-failure occurs during the rollback, the process that // ** attempts rollback following system recovery sees the same journal // ** content as this process. // ** // ** If everything goes as planned, SQLITE_OK is returned. Otherwise, // ** an SQLite error code. // */ func _pagerSyncHotJournal(tls *libc.TLS, pPager uintptr) (r int32) { var rc int32 _ = rc rc = SQLITE_OK if !((*TPager)(unsafe.Pointer(pPager)).FnoSync != 0) { rc = _sqlite3OsSync(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, int32(SQLITE_SYNC_NORMAL)) } if rc == SQLITE_OK { rc = _sqlite3OsFileSize(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, pPager+88) } return rc } // C documentation // // /* // ** Obtain a reference to a memory mapped page object for page number pgno. // ** The new object will use the pointer pData, obtained from xFetch(). // ** If successful, set *ppPage to point to the new page reference // ** and return SQLITE_OK. Otherwise, return an SQLite error code and set // ** *ppPage to zero. // ** // ** Page references obtained by calling this function should be released // ** by calling pagerReleaseMapPage(). // */ func _pagerAcquireMapPage(tls *libc.TLS, pPager uintptr, pgno TPgno, pData uintptr, ppPage uintptr) (r int32) { var p, v1, v2 uintptr _, _, _ = p, v1, v2 /* Memory mapped page to return */ if (*TPager)(unsafe.Pointer(pPager)).FpMmapFreelist != 0 { v1 = (*TPager)(unsafe.Pointer(pPager)).FpMmapFreelist p = v1 *(*uintptr)(unsafe.Pointer(ppPage)) = v1 (*TPager)(unsafe.Pointer(pPager)).FpMmapFreelist = (*TPgHdr)(unsafe.Pointer(p)).FpDirty (*TPgHdr)(unsafe.Pointer(p)).FpDirty = uintptr(0) _ = libc.Int32FromInt32(0) libc.Xmemset(tls, (*TPgHdr)(unsafe.Pointer(p)).FpExtra, 0, uint32(8)) } else { v2 = _sqlite3MallocZero(tls, uint64(uint32(48)+uint32((*TPager)(unsafe.Pointer(pPager)).FnExtra))) p = v2 *(*uintptr)(unsafe.Pointer(ppPage)) = v2 if p == uintptr(0) { _sqlite3OsUnfetch(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int64(pgno-libc.Uint32FromInt32(1))*(*TPager)(unsafe.Pointer(pPager)).FpageSize, pData) return int32(SQLITE_NOMEM) } (*TPgHdr)(unsafe.Pointer(p)).FpExtra = p + 1*48 (*TPgHdr)(unsafe.Pointer(p)).Fflags = uint16(PGHDR_MMAP) (*TPgHdr)(unsafe.Pointer(p)).FnRef = int64(1) (*TPgHdr)(unsafe.Pointer(p)).FpPager = pPager } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TPgHdr)(unsafe.Pointer(p)).Fpgno = pgno (*TPgHdr)(unsafe.Pointer(p)).FpData = pData (*TPager)(unsafe.Pointer(pPager)).FnMmapOut++ return SQLITE_OK } // C documentation // // /* // ** Release a reference to page pPg. pPg must have been returned by an // ** earlier call to pagerAcquireMapPage(). // */ func _pagerReleaseMapPage(tls *libc.TLS, pPg uintptr) { var pPager uintptr _ = pPager pPager = (*TPgHdr)(unsafe.Pointer(pPg)).FpPager (*TPager)(unsafe.Pointer(pPager)).FnMmapOut-- (*TPgHdr)(unsafe.Pointer(pPg)).FpDirty = (*TPager)(unsafe.Pointer(pPager)).FpMmapFreelist (*TPager)(unsafe.Pointer(pPager)).FpMmapFreelist = pPg _ = libc.Int32FromInt32(0) _sqlite3OsUnfetch(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int64((*TPgHdr)(unsafe.Pointer(pPg)).Fpgno-libc.Uint32FromInt32(1))*(*TPager)(unsafe.Pointer(pPager)).FpageSize, (*TPgHdr)(unsafe.Pointer(pPg)).FpData) } // C documentation // // /* // ** Free all PgHdr objects stored in the Pager.pMmapFreelist list. // */ func _pagerFreeMapHdrs(tls *libc.TLS, pPager uintptr) { var p, pNext uintptr _, _ = p, pNext p = (*TPager)(unsafe.Pointer(pPager)).FpMmapFreelist for { if !(p != 0) { break } pNext = (*TPgHdr)(unsafe.Pointer(p)).FpDirty Xsqlite3_free(tls, p) goto _1 _1: ; p = pNext } } // C documentation // // /* Verify that the database file has not be deleted or renamed out from // ** under the pager. Return SQLITE_OK if the database is still where it ought // ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error // ** code from sqlite3OsAccess()) if the database has gone missing. // */ func _databaseIsUnmoved(tls *libc.TLS, pPager uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* bHasMoved at bp+0 */ int32 _ = rc *(*int32)(unsafe.Pointer(bp)) = 0 if (*TPager)(unsafe.Pointer(pPager)).FtempFile != 0 { return SQLITE_OK } if (*TPager)(unsafe.Pointer(pPager)).FdbSize == uint32(0) { return SQLITE_OK } _ = libc.Int32FromInt32(0) rc = _sqlite3OsFileControl(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int32(SQLITE_FCNTL_HAS_MOVED), bp) if rc == int32(SQLITE_NOTFOUND) { /* If the HAS_MOVED file-control is unimplemented, assume that the file ** has not been moved. That is the historical behavior of SQLite: prior to ** version 3.8.3, it never checked */ rc = SQLITE_OK } else { if rc == SQLITE_OK && *(*int32)(unsafe.Pointer(bp)) != 0 { rc = libc.Int32FromInt32(SQLITE_READONLY) | libc.Int32FromInt32(4)<errCode = 0; */ (*TPager)(unsafe.Pointer(pPager)).FexclusiveMode = uint8(0) a = uintptr(0) _ = libc.Int32FromInt32(0) if db != 0 && uint64(0) == (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_NoCkptOnClose) && SQLITE_OK == _databaseIsUnmoved(tls, pPager) { a = pTmp } _sqlite3WalClose(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, db, int32((*TPager)(unsafe.Pointer(pPager)).FwalSyncFlags), int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), a) (*TPager)(unsafe.Pointer(pPager)).FpWal = uintptr(0) _pager_reset(tls, pPager) if (*TPager)(unsafe.Pointer(pPager)).FmemDb != 0 { _pager_unlock(tls, pPager) } else { /* If it is open, sync the journal file before calling UnlockAndRollback. ** If this is not done, then an unsynced portion of the open journal ** file may be played back into the database. If a power failure occurs ** while this is happening, the database could become corrupt. ** ** If an error occurs while trying to sync the journal, shift the pager ** into the ERROR state. This causes UnlockAndRollback to unlock the ** database and close the journal file without attempting to roll it ** back or finalize it. The next database user will have to do hot-journal ** rollback before accessing the database file. */ if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != uintptr(0) { _pager_error(tls, pPager, _pagerSyncHotJournal(tls, pPager)) } _pagerUnlockAndRollback(tls, pPager) } _sqlite3EndBenignMalloc(tls) _sqlite3OsClose(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd) _sqlite3OsClose(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd) _sqlite3PageFree(tls, pTmp) _sqlite3PcacheClose(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) Xsqlite3_free(tls, pPager) return SQLITE_OK } // C documentation // // /* // ** Increment the reference count for page pPg. // */ func _sqlite3PagerRef(tls *libc.TLS, pPg uintptr) { _sqlite3PcacheRef(tls, pPg) } // C documentation // // /* // ** Sync the journal. In other words, make sure all the pages that have // ** been written to the journal have actually reached the surface of the // ** disk and can be restored in the event of a hot-journal rollback. // ** // ** If the Pager.noSync flag is set, then this function is a no-op. // ** Otherwise, the actions required depend on the journal-mode and the // ** device characteristics of the file-system, as follows: // ** // ** * If the journal file is an in-memory journal file, no action need // ** be taken. // ** // ** * Otherwise, if the device does not support the SAFE_APPEND property, // ** then the nRec field of the most recently written journal header // ** is updated to contain the number of journal records that have // ** been written following it. If the pager is operating in full-sync // ** mode, then the journal file is synced before this field is updated. // ** // ** * If the device does not support the SEQUENTIAL property, then // ** journal file is synced. // ** // ** Or, in pseudo-code: // ** // ** if( NOT ){ // ** if( NOT SAFE_APPEND ){ // ** if( ) xSync(); // ** // ** } // ** if( NOT SEQUENTIAL ) xSync(); // ** } // ** // ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every // ** page currently held in memory before returning SQLITE_OK. If an IO // ** error is encountered, then the IO error code is returned to the caller. // */ func _syncJournal(tls *libc.TLS, pPager uintptr, newHdr int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var iDc, rc, v1 int32 var iNextHdrOffset Ti64 var _ /* aMagic at bp+0 */ [8]Tu8 var _ /* zHeader at bp+8 */ [12]Tu8 _, _, _, _ = iDc, iNextHdrOffset, rc, v1 /* Return code */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3PagerExclusiveLock(tls, pPager) if rc != SQLITE_OK { return rc } if !((*TPager)(unsafe.Pointer(pPager)).FnoSync != 0) { _ = libc.Int32FromInt32(0) if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != uintptr(0) && int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) != int32(PAGER_JOURNALMODE_MEMORY) { iDc = _sqlite3OsDeviceCharacteristics(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd) _ = libc.Int32FromInt32(0) if 0 == iDc&int32(SQLITE_IOCAP_SAFE_APPEND) { libc.Xmemcpy(tls, bp+8, uintptr(unsafe.Pointer(&_aJournalMagic)), uint32(8)) _sqlite3Put4byte(tls, bp+8+uintptr(8), uint32((*TPager)(unsafe.Pointer(pPager)).FnRec)) iNextHdrOffset = _journalHdrOffset(tls, pPager) rc = _sqlite3OsRead(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, bp, int32(8), iNextHdrOffset) if rc == SQLITE_OK && 0 == libc.Xmemcmp(tls, bp, uintptr(unsafe.Pointer(&_aJournalMagic)), uint32(8)) { rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, uintptr(unsafe.Pointer(&_zerobyte)), int32(1), iNextHdrOffset) } if rc != SQLITE_OK && rc != libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(2)< (*TPager)(unsafe.Pointer(pPager)).FdbHintSize) { *(*Tsqlite3_int64)(unsafe.Pointer(bp)) = (*TPager)(unsafe.Pointer(pPager)).FpageSize * int64((*TPager)(unsafe.Pointer(pPager)).FdbSize) _sqlite3OsFileControlHint(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int32(SQLITE_FCNTL_SIZE_HINT), bp) (*TPager)(unsafe.Pointer(pPager)).FdbHintSize = (*TPager)(unsafe.Pointer(pPager)).FdbSize } for rc == SQLITE_OK && pList != 0 { pgno = (*TPgHdr)(unsafe.Pointer(pList)).Fpgno /* If there are dirty pages in the page cache with page numbers greater ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to ** make the file smaller (presumably by auto-vacuum code). Do not write ** any such pages to the file. ** ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag ** set (set by sqlite3PagerDontWrite()). */ if pgno <= (*TPager)(unsafe.Pointer(pPager)).FdbSize && 0 == int32((*TPgHdr)(unsafe.Pointer(pList)).Fflags)&int32(PGHDR_DONT_WRITE) { offset = int64(pgno-libc.Uint32FromInt32(1)) * (*TPager)(unsafe.Pointer(pPager)).FpageSize /* Data to write */ _ = libc.Int32FromInt32(0) if (*TPgHdr)(unsafe.Pointer(pList)).Fpgno == uint32(1) { _pager_write_changecounter(tls, pList) } pData = (*TPgHdr)(unsafe.Pointer(pList)).FpData /* Write out the page data. */ rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, pData, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), offset) /* If page 1 was just written, update Pager.dbFileVers to match ** the value now stored in the database file. If writing this ** page caused the database file to grow, update dbFileSize. */ if pgno == uint32(1) { libc.Xmemcpy(tls, pPager+112, pData+24, uint32(16)) } if pgno > (*TPager)(unsafe.Pointer(pPager)).FdbFileSize { (*TPager)(unsafe.Pointer(pPager)).FdbFileSize = pgno } *(*Tu32)(unsafe.Pointer(pPager + 200 + 2*4))++ /* Update any backup objects copying the contents of this pager. */ _sqlite3BackupUpdate(tls, (*TPager)(unsafe.Pointer(pPager)).FpBackup, pgno, (*TPgHdr)(unsafe.Pointer(pList)).FpData) } else { } pList = (*TPgHdr)(unsafe.Pointer(pList)).FpDirty } return rc } // C documentation // // /* // ** Ensure that the sub-journal file is open. If it is already open, this // ** function is a no-op. // ** // ** SQLITE_OK is returned if everything goes according to plan. An // ** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen() // ** fails. // */ func _openSubJournal(tls *libc.TLS, pPager uintptr) (r int32) { var flags, nStmtSpill, rc int32 _, _, _ = flags, nStmtSpill, rc rc = SQLITE_OK if !((*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fsjfd)).FpMethods != libc.UintptrFromInt32(0)) { flags = libc.Int32FromInt32(SQLITE_OPEN_SUBJOURNAL) | libc.Int32FromInt32(SQLITE_OPEN_READWRITE) | libc.Int32FromInt32(SQLITE_OPEN_CREATE) | libc.Int32FromInt32(SQLITE_OPEN_EXCLUSIVE) | libc.Int32FromInt32(SQLITE_OPEN_DELETEONCLOSE) nStmtSpill = _sqlite3Config.FnStmtSpill if int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) == int32(PAGER_JOURNALMODE_MEMORY) || (*TPager)(unsafe.Pointer(pPager)).FsubjInMemory != 0 { nStmtSpill = -int32(1) } rc = _sqlite3JournalOpen(tls, (*TPager)(unsafe.Pointer(pPager)).FpVfs, uintptr(0), (*TPager)(unsafe.Pointer(pPager)).Fsjfd, flags, nStmtSpill) } return rc } // C documentation // // /* // ** Append a record of the current state of page pPg to the sub-journal. // ** // ** If successful, set the bit corresponding to pPg->pgno in the bitvecs // ** for all open savepoints before returning. // ** // ** This function returns SQLITE_OK if everything is successful, an IO // ** error code if the attempt to write to the sub-journal fails, or // ** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint // ** bitvec. // */ func _subjournalPage(tls *libc.TLS, pPg uintptr) (r int32) { var offset Ti64 var pData, pData2, pPager uintptr var rc int32 _, _, _, _, _ = offset, pData, pData2, pPager, rc rc = SQLITE_OK pPager = (*TPgHdr)(unsafe.Pointer(pPg)).FpPager if int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) != int32(PAGER_JOURNALMODE_OFF) { /* Open the sub-journal, if it has not already been opened */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _openSubJournal(tls, pPager) /* If the sub-journal was opened successfully (or was already open), ** write the journal record into the file. */ if rc == SQLITE_OK { pData = (*TPgHdr)(unsafe.Pointer(pPg)).FpData offset = int64((*TPager)(unsafe.Pointer(pPager)).FnSubRec) * (int64(4) + (*TPager)(unsafe.Pointer(pPager)).FpageSize) pData2 = pData rc = _write32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fsjfd, offset, (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno) if rc == SQLITE_OK { rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Fsjfd, pData2, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), offset+int64(4)) } } } if rc == SQLITE_OK { (*TPager)(unsafe.Pointer(pPager)).FnSubRec++ _ = libc.Int32FromInt32(0) rc = _addToSavepointBitvecs(tls, pPager, (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno) } return rc } func _subjournalPageIfRequired(tls *libc.TLS, pPg uintptr) (r int32) { if _subjRequiresPage(tls, pPg) != 0 { return _subjournalPage(tls, pPg) } else { return SQLITE_OK } return r } // C documentation // // /* // ** This function is called by the pcache layer when it has reached some // ** soft memory limit. The first argument is a pointer to a Pager object // ** (cast as a void*). The pager is always 'purgeable' (not an in-memory // ** database). The second argument is a reference to a page that is // ** currently dirty but has no outstanding references. The page // ** is always associated with the Pager object passed as the first // ** argument. // ** // ** The job of this function is to make pPg clean by writing its contents // ** out to the database file, if possible. This may involve syncing the // ** journal file. // ** // ** If successful, sqlite3PcacheMakeClean() is called on the page and // ** SQLITE_OK returned. If an IO error occurs while trying to make the // ** page clean, the IO error code is returned. If the page cannot be // ** made clean for some other reason, but no error occurs, then SQLITE_OK // ** is returned by sqlite3PcacheMakeClean() is not called. // */ func _pagerStress(tls *libc.TLS, p uintptr, pPg uintptr) (r int32) { var pPager uintptr var rc int32 _, _ = pPager, rc pPager = p rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* The doNotSpill NOSYNC bit is set during times when doing a sync of ** journal (and adding a new header) is not allowed. This occurs ** during calls to sqlite3PagerWrite() while trying to journal multiple ** pages belonging to the same sector. ** ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling ** regardless of whether or not a sync is required. This is set during ** a rollback or by user request, respectively. ** ** Spilling is also prohibited when in an error state since that could ** lead to database corruption. In the current implementation it ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3 ** while in the error state, hence it is impossible for this routine to ** be called in the error state. Nevertheless, we include a NEVER() ** test for the error state as a safeguard against future changes. */ if (*TPager)(unsafe.Pointer(pPager)).FerrCode != 0 { return SQLITE_OK } if (*TPager)(unsafe.Pointer(pPager)).FdoNotSpill != 0 && (int32((*TPager)(unsafe.Pointer(pPager)).FdoNotSpill)&(libc.Int32FromInt32(SPILLFLAG_ROLLBACK)|libc.Int32FromInt32(SPILLFLAG_OFF)) != 0 || int32((*TPgHdr)(unsafe.Pointer(pPg)).Fflags)&int32(PGHDR_NEED_SYNC) != 0) { return SQLITE_OK } *(*Tu32)(unsafe.Pointer(pPager + 200 + 3*4))++ (*TPgHdr)(unsafe.Pointer(pPg)).FpDirty = uintptr(0) if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) { /* Write a single frame for this page to the log. */ rc = _subjournalPageIfRequired(tls, pPg) if rc == SQLITE_OK { rc = _pagerWalFrames(tls, pPager, pPg, uint32(0), 0) } } else { /* Sync the journal file if required. */ if int32((*TPgHdr)(unsafe.Pointer(pPg)).Fflags)&int32(PGHDR_NEED_SYNC) != 0 || int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_WRITER_CACHEMOD) { rc = _syncJournal(tls, pPager, int32(1)) } /* Write the contents of the page out to the database file. */ if rc == SQLITE_OK { _ = libc.Int32FromInt32(0) rc = _pager_write_pagelist(tls, pPager, pPg) } } /* Mark the page as clean. */ if rc == SQLITE_OK { _sqlite3PcacheMakeClean(tls, pPg) } return _pager_error(tls, pPager, rc) } // C documentation // // /* // ** Flush all unreferenced dirty pages to disk. // */ func _sqlite3PagerFlush(tls *libc.TLS, pPager uintptr) (r int32) { var pList, pNext uintptr var rc int32 _, _, _ = pList, pNext, rc rc = (*TPager)(unsafe.Pointer(pPager)).FerrCode if !((*TPager)(unsafe.Pointer(pPager)).FmemDb != 0) { pList = _sqlite3PcacheDirtyList(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) _ = libc.Int32FromInt32(0) for rc == SQLITE_OK && pList != 0 { pNext = (*TPgHdr)(unsafe.Pointer(pList)).FpDirty if (*TPgHdr)(unsafe.Pointer(pList)).FnRef == 0 { rc = _pagerStress(tls, pPager, pList) } pList = pNext } } return rc } // C documentation // // /* // ** Allocate and initialize a new Pager object and put a pointer to it // ** in *ppPager. The pager should eventually be freed by passing it // ** to sqlite3PagerClose(). // ** // ** The zFilename argument is the path to the database file to open. // ** If zFilename is NULL then a randomly-named temporary file is created // ** and used as the file to be cached. Temporary files are be deleted // ** automatically when they are closed. If zFilename is ":memory:" then // ** all information is held in cache. It is never written to disk. // ** This can be used to implement an in-memory database. // ** // ** The nExtra parameter specifies the number of bytes of space allocated // ** along with each page reference. This space is available to the user // ** via the sqlite3PagerGetExtra() API. When a new page is allocated, the // ** first 8 bytes of this space are zeroed but the remainder is uninitialized. // ** (The extra space is used by btree as the MemPage object.) // ** // ** The flags argument is used to specify properties that affect the // ** operation of the pager. It should be passed some bitwise combination // ** of the PAGER_* flags. // ** // ** The vfsFlags parameter is a bitmask to pass to the flags parameter // ** of the xOpen() method of the supplied VFS when opening files. // ** // ** If the pager object is allocated and the specified file opened // ** successfully, SQLITE_OK is returned and *ppPager set to point to // ** the new pager object. If an error occurs, *ppPager is set to NULL // ** and error code returned. This function may return SQLITE_NOMEM // ** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or // ** various SQLITE_IO_XXX errors. // */ func _sqlite3PagerOpen(tls *libc.TLS, pVfs uintptr, ppPager uintptr, zFilename uintptr, nExtra int32, flags int32, vfsFlags int32, xReinit uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iDc, journalFileSize, memDb, memJM, nPathname, nUriByte, pcacheSize, rc, readOnly, tempFile, useJournal, v4 int32 var pPtr, z, zPathname, zUri, v1, v5 uintptr var _ /* fout at bp+8 */ int32 var _ /* pPager at bp+0 */ uintptr var _ /* szPageDflt at bp+4 */ Tu32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = iDc, journalFileSize, memDb, memJM, nPathname, nUriByte, pPtr, pcacheSize, rc, readOnly, tempFile, useJournal, z, zPathname, zUri, v1, v4, v5 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Pager object to allocate and return */ rc = SQLITE_OK /* Return code */ tempFile = 0 /* True for temp files (incl. in-memory files) */ memDb = 0 /* True if this is an in-memory file */ memJM = 0 /* Memory journal mode */ readOnly = 0 /* Bytes to allocate for each journal fd */ zPathname = uintptr(0) /* Full path to database file */ nPathname = 0 /* Number of bytes in zPathname */ useJournal = libc.BoolInt32(flags&int32(PAGER_OMIT_JOURNAL) == 0) /* False to omit journal */ pcacheSize = _sqlite3PcacheSize(tls) /* Bytes to allocate for PCache */ *(*Tu32)(unsafe.Pointer(bp + 4)) = uint32(SQLITE_DEFAULT_PAGE_SIZE) /* Default page size */ zUri = uintptr(0) /* URI args to copy */ nUriByte = int32(1) /* Number of bytes of URI args at *zUri */ /* Figure out how much space is required for each journal file-handle ** (there are two of them, the main journal and the sub-journal). */ journalFileSize = (_sqlite3JournalSize(tls, pVfs) + int32(7)) & ^libc.Int32FromInt32(7) /* Set the output variable to NULL in case an error occurs. */ *(*uintptr)(unsafe.Pointer(ppPager)) = uintptr(0) if flags&int32(PAGER_MEMORY) != 0 { memDb = int32(1) if zFilename != 0 && *(*uint8)(unsafe.Pointer(zFilename)) != 0 { zPathname = _sqlite3DbStrDup(tls, uintptr(0), zFilename) if zPathname == uintptr(0) { return int32(SQLITE_NOMEM) } nPathname = _sqlite3Strlen30(tls, zPathname) zFilename = uintptr(0) } } /* Compute and store the full pathname in an allocated buffer pointed ** to by zPathname, length nPathname. Or, if this is a temporary file, ** leave both nPathname and zPathname set to 0. */ if zFilename != 0 && *(*uint8)(unsafe.Pointer(zFilename)) != 0 { nPathname = (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname + int32(1) zPathname = _sqlite3DbMallocRaw(tls, uintptr(0), uint64(nPathname*int32(2))) if zPathname == uintptr(0) { return int32(SQLITE_NOMEM) } *(*uint8)(unsafe.Pointer(zPathname)) = uint8(0) /* Make sure initialized even if FullPathname() fails */ rc = _sqlite3OsFullPathname(tls, pVfs, zFilename, nPathname, zPathname) if rc != SQLITE_OK { if rc == libc.Int32FromInt32(SQLITE_OK)|libc.Int32FromInt32(2)< (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname { /* This branch is taken when the journal path required by ** the database being opened will be more than pVfs->mxPathname ** bytes in length. This means the database cannot be opened, ** as it will not be possible to open the journal file or even ** check for a hot-journal before reading. */ rc = _sqlite3CantopenError(tls, int32(61558)) } if rc != SQLITE_OK { _sqlite3DbFree(tls, uintptr(0), zPathname) return rc } } /* Allocate memory for the Pager structure, PCache object, the ** three file descriptors, the database file name and the journal ** file name. The layout in memory is as follows: ** ** Pager object (sizeof(Pager) bytes) ** PCache object (sqlite3PcacheSize() bytes) ** Database file handle (pVfs->szOsFile bytes) ** Sub-journal file handle (journalFileSize bytes) ** Main journal file handle (journalFileSize bytes) ** Ptr back to the Pager (sizeof(Pager*) bytes) ** \0\0\0\0 database prefix (4 bytes) ** Database file name (nPathname+1 bytes) ** URI query parameters (nUriByte bytes) ** Journal filename (nPathname+8+1 bytes) ** WAL filename (nPathname+4+1 bytes) ** \0\0\0 terminator (3 bytes) ** ** Some 3rd-party software, over which we have no control, depends on ** the specific order of the filenames and the \0 separators between them ** so that it can (for example) find the database filename given the WAL ** filename without using the sqlite3_filename_database() API. This is a ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party ** software is in widespread use, so we try to avoid changing the filename ** order and formatting if possible. In particular, the details of the ** filename format expected by 3rd-party software should be as follows: ** ** - Main Database Path ** - \0 ** - Multiple URI components consisting of: ** - Key ** - \0 ** - Value ** - \0 ** - \0 ** - Journal Path ** - \0 ** - WAL Path (zWALName) ** - \0 ** ** The sqlite3_create_filename() interface and the databaseFilename() utility ** that is used by sqlite3_filename_database() and kin also depend on the ** specific formatting and order of the various filenames, so if the format ** changes here, be sure to change it there as well. */ _ = libc.Int32FromInt32(0) pPtr = _sqlite3MallocZero(tls, uint64((libc.Uint32FromInt64(240)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))+uint32((pcacheSize+libc.Int32FromInt32(7)) & ^libc.Int32FromInt32(7))+uint32(((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FszOsFile+libc.Int32FromInt32(7)) & ^libc.Int32FromInt32(7))+uint32(journalFileSize*int32(2))+uint32(__SIZEOF_POINTER__)+uint32(4)+uint32(nPathname)+uint32(1)+uint32(nUriByte)+uint32(nPathname)+uint32(8)+uint32(1)+uint32(nPathname)+uint32(4)+uint32(1)+uint32(3))) _ = libc.Int32FromInt32(0) if !(pPtr != 0) { _sqlite3DbFree(tls, uintptr(0), zPathname) return int32(SQLITE_NOMEM) } *(*uintptr)(unsafe.Pointer(bp)) = pPtr pPtr += uintptr((libc.Uint32FromInt64(240) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7))) (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpPCache = pPtr pPtr += uintptr((pcacheSize + libc.Int32FromInt32(7)) & ^libc.Int32FromInt32(7)) (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Ffd = pPtr pPtr += uintptr(((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FszOsFile + libc.Int32FromInt32(7)) & ^libc.Int32FromInt32(7)) (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fsjfd = pPtr pPtr += uintptr(journalFileSize) (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fjfd = pPtr pPtr += uintptr(journalFileSize) _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, pPtr, bp, uint32(__SIZEOF_POINTER__)) pPtr += uintptr(__SIZEOF_POINTER__) /* Fill in the Pager.zFilename and pPager.zQueryParam fields */ pPtr += uintptr(4) /* Skip zero prefix */ (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FzFilename = pPtr if nPathname > 0 { libc.Xmemcpy(tls, pPtr, zPathname, uint32(nPathname)) pPtr += uintptr(nPathname + int32(1)) if zUri != 0 { libc.Xmemcpy(tls, pPtr, zUri, uint32(nUriByte)) pPtr += uintptr(nUriByte) } else { pPtr++ } } /* Fill in Pager.zJournal */ if nPathname > 0 { (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FzJournal = pPtr libc.Xmemcpy(tls, pPtr, zPathname, uint32(nPathname)) pPtr += uintptr(nPathname) libc.Xmemcpy(tls, pPtr, __ccgo_ts+4078, uint32(8)) pPtr += uintptr(libc.Int32FromInt32(8) + libc.Int32FromInt32(1)) } else { (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FzJournal = uintptr(0) } /* Fill in Pager.zWal */ if nPathname > 0 { (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FzWal = pPtr libc.Xmemcpy(tls, pPtr, zPathname, uint32(nPathname)) pPtr += uintptr(nPathname) libc.Xmemcpy(tls, pPtr, __ccgo_ts+4087, uint32(4)) pPtr += uintptr(libc.Int32FromInt32(4) + libc.Int32FromInt32(1)) } else { (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FzWal = uintptr(0) } _ = pPtr /* Suppress warning about unused pPtr value */ if nPathname != 0 { _sqlite3DbFree(tls, uintptr(0), zPathname) } (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpVfs = pVfs (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FvfsFlags = uint32(vfsFlags) /* Open the pager file. */ if !(zFilename != 0 && *(*uint8)(unsafe.Pointer(zFilename)) != 0) { goto _2 } *(*int32)(unsafe.Pointer(bp + 8)) = 0 /* VFS flags returned by xOpen() */ rc = _sqlite3OsOpen(tls, pVfs, (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FzFilename, (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Ffd, vfsFlags, bp+8) _ = libc.Int32FromInt32(0) v4 = libc.BoolInt32(*(*int32)(unsafe.Pointer(bp + 8))&libc.Int32FromInt32(SQLITE_OPEN_MEMORY) != libc.Int32FromInt32(0)) memJM = v4 (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FmemVfs = uint8(v4) readOnly = libc.BoolInt32(*(*int32)(unsafe.Pointer(bp + 8))&int32(SQLITE_OPEN_READONLY) != 0) /* If the file was successfully opened for read/write access, ** choose a default page size in case we have to create the ** database file. The default page size is the maximum of: ** ** + SQLITE_DEFAULT_PAGE_SIZE, ** + The value returned by sqlite3OsSectorSize() ** + The largest page size that can be written atomically. */ if rc == SQLITE_OK { iDc = _sqlite3OsDeviceCharacteristics(tls, (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Ffd) if !(readOnly != 0) { _setSectorSize(tls, *(*uintptr)(unsafe.Pointer(bp))) _ = libc.Int32FromInt32(0) if *(*Tu32)(unsafe.Pointer(bp + 4)) < (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FsectorSize { if (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FsectorSize > uint32(SQLITE_MAX_DEFAULT_PAGE_SIZE) { *(*Tu32)(unsafe.Pointer(bp + 4)) = uint32(SQLITE_MAX_DEFAULT_PAGE_SIZE) } else { *(*Tu32)(unsafe.Pointer(bp + 4)) = (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FsectorSize } } } (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnoLock = uint8(Xsqlite3_uri_boolean(tls, (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FzFilename, __ccgo_ts+4092, 0)) if iDc&int32(SQLITE_IOCAP_IMMUTABLE) != 0 || Xsqlite3_uri_boolean(tls, (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FzFilename, __ccgo_ts+4099, 0) != 0 { vfsFlags |= int32(SQLITE_OPEN_READONLY) goto act_like_temp_file } } goto _3 _2: ; /* If a temporary file is requested, it is not opened immediately. ** In this case we accept the default page size and delay actually ** opening the file until the first call to OsWrite(). ** ** This branch is also run for an in-memory database. An in-memory ** database is the same as a temp-file that is never written out to ** disk and uses an in-memory rollback journal. ** ** This branch also runs for files marked as immutable. */ goto act_like_temp_file act_like_temp_file: ; tempFile = int32(1) (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FeState = uint8(PAGER_READER) /* Pretend we already have a lock */ (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FeLock = uint8(EXCLUSIVE_LOCK) /* Pretend we are in EXCLUSIVE mode */ (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnoLock = uint8(1) /* Do no locking */ readOnly = vfsFlags & int32(SQLITE_OPEN_READONLY) _3: ; /* The following call to PagerSetPagesize() serves to set the value of ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer. */ if rc == SQLITE_OK { _ = libc.Int32FromInt32(0) rc = _sqlite3PagerSetPagesize(tls, *(*uintptr)(unsafe.Pointer(bp)), bp+4, -int32(1)) } /* Initialize the PCache object. */ if rc == SQLITE_OK { nExtra = (nExtra + int32(7)) & ^libc.Int32FromInt32(7) _ = libc.Int32FromInt32(0) if !(memDb != 0) { v5 = __ccgo_fp(_pagerStress) } else { v5 = uintptr(0) } rc = _sqlite3PcacheOpen(tls, int32(*(*Tu32)(unsafe.Pointer(bp + 4))), nExtra, libc.BoolInt32(!(memDb != 0)), v5, *(*uintptr)(unsafe.Pointer(bp)), (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpPCache) } /* If an error occurred above, free the Pager structure and close the file. */ if rc != SQLITE_OK { _sqlite3OsClose(tls, (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Ffd) _sqlite3PageFree(tls, (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpTmpSpace) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp))) return rc } (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FuseJournal = uint8(useJournal) /* pPager->stmtOpen = 0; */ /* pPager->stmtInUse = 0; */ /* pPager->nRef = 0; */ /* pPager->stmtSize = 0; */ /* pPager->stmtJSize = 0; */ /* pPager->nPage = 0; */ (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FmxPgno = uint32(SQLITE_MAX_PAGE_COUNT) /* pPager->state = PAGER_UNLOCK; */ /* pPager->errMask = 0; */ (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FtempFile = uint8(tempFile) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FexclusiveMode = uint8(tempFile) (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FchangeCountDone = (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FtempFile (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FmemDb = uint8(memDb) (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FreadOnly = uint8(readOnly) _ = libc.Int32FromInt32(0) _sqlite3PagerSetFlags(tls, *(*uintptr)(unsafe.Pointer(bp)), uint32(libc.Int32FromInt32(SQLITE_DEFAULT_SYNCHRONOUS)+libc.Int32FromInt32(1)|libc.Int32FromInt32(PAGER_CACHESPILL))) /* pPager->pFirst = 0; */ /* pPager->pFirstSynced = 0; */ /* pPager->pLast = 0; */ (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnExtra = uint16(nExtra) (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FjournalSizeLimit = int64(-int32(1)) _ = libc.Int32FromInt32(0) _setSectorSize(tls, *(*uintptr)(unsafe.Pointer(bp))) if !(useJournal != 0) { (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FjournalMode = uint8(PAGER_JOURNALMODE_OFF) } else { if memDb != 0 || memJM != 0 { (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FjournalMode = uint8(PAGER_JOURNALMODE_MEMORY) } } /* pPager->xBusyHandler = 0; */ /* pPager->pBusyHandlerArg = 0; */ (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FxReiniter = xReinit _setGetterMethod(tls, *(*uintptr)(unsafe.Pointer(bp))) /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */ *(*uintptr)(unsafe.Pointer(ppPager)) = *(*uintptr)(unsafe.Pointer(bp)) return SQLITE_OK } // C documentation // // /* // ** Return the sqlite3_file for the main database given the name // ** of the corresponding WAL or Journal name as passed into // ** xOpen. // */ func Xsqlite3_database_file_object(tls *libc.TLS, zName uintptr) (r uintptr) { var p, pPager uintptr _, _ = p, pPager for int32(*(*uint8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(1))))) != 0 || int32(*(*uint8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(2))))) != 0 || int32(*(*uint8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(3))))) != 0 || int32(*(*uint8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(4))))) != 0 { zName-- } p = zName - uintptr(4) - uintptr(4) _ = libc.Int32FromInt32(0) pPager = *(*uintptr)(unsafe.Pointer(p)) return (*TPager)(unsafe.Pointer(pPager)).Ffd } // C documentation // // /* // ** This function is called after transitioning from PAGER_UNLOCK to // ** PAGER_SHARED state. It tests if there is a hot journal present in // ** the file-system for the given pager. A hot journal is one that // ** needs to be played back. According to this function, a hot-journal // ** file exists if the following criteria are met: // ** // ** * The journal file exists in the file system, and // ** * No process holds a RESERVED or greater lock on the database file, and // ** * The database file itself is greater than 0 bytes in size, and // ** * The first byte of the journal file exists and is not 0x00. // ** // ** If the current size of the database file is 0 but a journal file // ** exists, that is probably an old journal left over from a prior // ** database with the same name. In this case the journal file is // ** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK // ** is returned. // ** // ** This routine does not check if there is a super-journal filename // ** at the end of the file. If there is, and that super-journal file // ** does not exist, then the journal file is not really hot. In this // ** case this routine will return a false-positive. The pager_playback() // ** routine will discover that the journal file is not really hot and // ** will not roll it back. // ** // ** If a hot-journal file is found to exist, *pExists is set to 1 and // ** SQLITE_OK returned. If no hot-journal file is present, *pExists is // ** set to 0 and SQLITE_OK returned. If an IO error occurs while trying // ** to determine whether or not a hot-journal file exists, the IO error // ** code is returned and the value of *pExists is undefined. // */ func _hasHotJournal(tls *libc.TLS, pPager uintptr, pExists uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var jrnlOpen, rc int32 var pVfs uintptr var _ /* exists at bp+0 */ int32 var _ /* f at bp+12 */ int32 var _ /* first at bp+16 */ Tu8 var _ /* locked at bp+4 */ int32 var _ /* nPage at bp+8 */ TPgno _, _, _ = jrnlOpen, pVfs, rc pVfs = (*TPager)(unsafe.Pointer(pPager)).FpVfs rc = SQLITE_OK /* Return code */ *(*int32)(unsafe.Pointer(bp)) = int32(1) /* True if a journal file is present */ jrnlOpen = libc.BoolInt32(!!((*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != libc.UintptrFromInt32(0))) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pExists)) = 0 if !(jrnlOpen != 0) { rc = _sqlite3OsAccess(tls, pVfs, (*TPager)(unsafe.Pointer(pPager)).FzJournal, SQLITE_ACCESS_EXISTS, bp) } if rc == SQLITE_OK && *(*int32)(unsafe.Pointer(bp)) != 0 { *(*int32)(unsafe.Pointer(bp + 4)) = 0 /* True if some process holds a RESERVED lock */ /* Race condition here: Another process might have been holding the ** the RESERVED lock and have a journal open at the sqlite3OsAccess() ** call above, but then delete the journal and drop the lock before ** we get to the following sqlite3OsCheckReservedLock() call. If that ** is the case, this routine might think there is a hot journal when ** in fact there is none. This results in a false-positive which will ** be dealt with by the playback routine. Ticket #3883. */ rc = _sqlite3OsCheckReservedLock(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, bp+4) if rc == SQLITE_OK && !(*(*int32)(unsafe.Pointer(bp + 4)) != 0) { /* Number of pages in database file */ _ = libc.Int32FromInt32(0) rc = _pagerPagecount(tls, pPager, bp+8) if rc == SQLITE_OK { /* If the database is zero pages in size, that means that either (1) the ** journal is a remnant from a prior database with the same name where ** the database file but not the journal was deleted, or (2) the initial ** transaction that populates a new database is being rolled back. ** In either case, the journal file can be deleted. However, take care ** not to delete the journal file if it is already open due to ** journal_mode=PERSIST. */ if *(*TPgno)(unsafe.Pointer(bp + 8)) == uint32(0) && !(jrnlOpen != 0) { _sqlite3BeginBenignMalloc(tls) if _pagerLockDb(tls, pPager, int32(RESERVED_LOCK)) == SQLITE_OK { _sqlite3OsDelete(tls, pVfs, (*TPager)(unsafe.Pointer(pPager)).FzJournal, 0) if !((*TPager)(unsafe.Pointer(pPager)).FexclusiveMode != 0) { _pagerUnlockDb(tls, pPager, int32(SHARED_LOCK)) } } _sqlite3EndBenignMalloc(tls) } else { /* The journal file exists and no other connection has a reserved ** or greater lock on the database file. Now check that there is ** at least one non-zero bytes at the start of the journal file. ** If there is, then we consider this journal to be hot. If not, ** it can be ignored. */ if !(jrnlOpen != 0) { *(*int32)(unsafe.Pointer(bp + 12)) = libc.Int32FromInt32(SQLITE_OPEN_READONLY) | libc.Int32FromInt32(SQLITE_OPEN_MAIN_JOURNAL) rc = _sqlite3OsOpen(tls, pVfs, (*TPager)(unsafe.Pointer(pPager)).FzJournal, (*TPager)(unsafe.Pointer(pPager)).Fjfd, *(*int32)(unsafe.Pointer(bp + 12)), bp+12) } if rc == SQLITE_OK { *(*Tu8)(unsafe.Pointer(bp + 16)) = uint8(0) rc = _sqlite3OsRead(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, bp+16, int32(1), 0) if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(2)< (*TPager)(unsafe.Pointer(pPager)).FmxPgno { rc = int32(SQLITE_FULL) if pgno <= (*TPager)(unsafe.Pointer(pPager)).FdbSize { _sqlite3PcacheRelease(tls, pPg) pPg = uintptr(0) } goto pager_acquire_err } if noContent != 0 { /* Failure to set the bits in the InJournal bit-vectors is benign. ** It merely means that we might do some extra work to journal a ** page that does not need to be journaled. Nevertheless, be sure ** to test the case where a malloc error occurs while trying to set ** a bit in a bit vector. */ _sqlite3BeginBenignMalloc(tls) if pgno <= (*TPager)(unsafe.Pointer(pPager)).FdbOrigSize { _sqlite3BitvecSet(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal, pgno) } _addToSavepointBitvecs(tls, pPager, pgno) _sqlite3EndBenignMalloc(tls) } libc.Xmemset(tls, (*TPgHdr)(unsafe.Pointer(pPg)).FpData, 0, uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize)) } else { _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(pPager + 200 + 1*4))++ rc = _readDbPage(tls, pPg) if rc != SQLITE_OK { goto pager_acquire_err } } } return SQLITE_OK goto pager_acquire_err pager_acquire_err: ; _ = libc.Int32FromInt32(0) if pPg != 0 { _sqlite3PcacheDrop(tls, pPg) } _pagerUnlockIfUnused(tls, pPager) *(*uintptr)(unsafe.Pointer(ppPage)) = uintptr(0) return rc } // C documentation // // /* The page getter for when memory-mapped I/O is enabled */ func _getPageMMap(tls *libc.TLS, pPager uintptr, pgno TPgno, ppPage uintptr, flags int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bMmapOk, rc int32 var _ /* iFrame at bp+4 */ Tu32 var _ /* pData at bp+8 */ uintptr var _ /* pPg at bp+0 */ uintptr _, _ = bMmapOk, rc rc = SQLITE_OK *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*Tu32)(unsafe.Pointer(bp + 4)) = uint32(0) /* Frame to read from WAL file */ /* It is acceptable to use a read-only (mmap) page for any page except ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY ** flag was specified by the caller. And so long as the db is not a ** temporary or in-memory database. */ bMmapOk = libc.BoolInt32(pgno > uint32(1) && (int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_READER) || flags&int32(PAGER_GET_READONLY) != 0)) _ = libc.Int32FromInt32(0) /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here ** allows the compiler optimizer to reuse the results of the "pgno>1" ** test in the previous statement, and avoid testing pgno==0 in the ** common case where pgno is large. */ if pgno <= uint32(1) && pgno == uint32(0) { return _sqlite3CorruptError(tls, int32(62407)) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if bMmapOk != 0 && (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) { rc = _sqlite3WalFindFrame(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, pgno, bp+4) if rc != SQLITE_OK { *(*uintptr)(unsafe.Pointer(ppPage)) = uintptr(0) return rc } } if bMmapOk != 0 && *(*Tu32)(unsafe.Pointer(bp + 4)) == uint32(0) { *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) rc = _sqlite3OsFetch(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int64(pgno-libc.Uint32FromInt32(1))*(*TPager)(unsafe.Pointer(pPager)).FpageSize, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), bp+8) if rc == SQLITE_OK && *(*uintptr)(unsafe.Pointer(bp + 8)) != 0 { if int32((*TPager)(unsafe.Pointer(pPager)).FeState) > int32(PAGER_READER) || (*TPager)(unsafe.Pointer(pPager)).FtempFile != 0 { *(*uintptr)(unsafe.Pointer(bp)) = _sqlite3PagerLookup(tls, pPager, pgno) } if *(*uintptr)(unsafe.Pointer(bp)) == uintptr(0) { rc = _pagerAcquireMapPage(tls, pPager, pgno, *(*uintptr)(unsafe.Pointer(bp + 8)), bp) } else { _sqlite3OsUnfetch(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int64(pgno-libc.Uint32FromInt32(1))*(*TPager)(unsafe.Pointer(pPager)).FpageSize, *(*uintptr)(unsafe.Pointer(bp + 8))) } if *(*uintptr)(unsafe.Pointer(bp)) != 0 { _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(ppPage)) = *(*uintptr)(unsafe.Pointer(bp)) return SQLITE_OK } } if rc != SQLITE_OK { *(*uintptr)(unsafe.Pointer(ppPage)) = uintptr(0) return rc } } return _getPageNormal(tls, pPager, pgno, ppPage, flags) } // C documentation // // /* The page getter method for when the pager is an error state */ func _getPageError(tls *libc.TLS, pPager uintptr, pgno TPgno, ppPage uintptr, flags int32) (r int32) { _ = pgno _ = flags _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(ppPage)) = uintptr(0) return (*TPager)(unsafe.Pointer(pPager)).FerrCode } // C documentation // // /* Dispatch all page fetch requests to the appropriate getter method. // */ func _sqlite3PagerGet(tls *libc.TLS, pPager uintptr, pgno TPgno, ppPage uintptr, flags int32) (r int32) { /* Normal, high-speed version of sqlite3PagerGet() */ return (*(*func(*libc.TLS, uintptr, TPgno, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*TPager)(unsafe.Pointer(pPager)).FxGet})))(tls, pPager, pgno, ppPage, flags) } // C documentation // // /* // ** Acquire a page if it is already in the in-memory cache. Do // ** not read the page from disk. Return a pointer to the page, // ** or 0 if the page is not in cache. // ** // ** See also sqlite3PagerGet(). The difference between this routine // ** and sqlite3PagerGet() is that _get() will go to the disk and read // ** in the page if the page is not already in cache. This routine // ** returns NULL if the page is not in cache or if a disk I/O error // ** has ever happened. // */ func _sqlite3PagerLookup(tls *libc.TLS, pPager uintptr, pgno TPgno) (r uintptr) { var pPage uintptr _ = pPage _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pPage = _sqlite3PcacheFetch(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache, pgno, 0) _ = libc.Int32FromInt32(0) if pPage == uintptr(0) { return uintptr(0) } return _sqlite3PcacheFetchFinish(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache, pgno, pPage) } // C documentation // // /* // ** Release a page reference. // ** // ** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be used // ** if we know that the page being released is not the last reference to page1. // ** The btree layer always holds page1 open until the end, so these first // ** two routines can be used to release any page other than BtShared.pPage1. // ** The assert() at tag-20230419-2 proves that this constraint is always // ** honored. // ** // ** Use sqlite3PagerUnrefPageOne() to release page1. This latter routine // ** checks the total number of outstanding pages and if the number of // ** pages reaches zero it drops the database lock. // */ func _sqlite3PagerUnrefNotNull(tls *libc.TLS, pPg uintptr) { _ = libc.Int32FromInt32(0) if int32((*TDbPage)(unsafe.Pointer(pPg)).Fflags)&int32(PGHDR_MMAP) != 0 { _ = libc.Int32FromInt32(0) /* Page1 is never memory mapped */ _pagerReleaseMapPage(tls, pPg) } else { _sqlite3PcacheRelease(tls, pPg) } /* Do not use this routine to release the last reference to page1 */ _ = libc.Int32FromInt32(0) /* tag-20230419-2 */ } func _sqlite3PagerUnref(tls *libc.TLS, pPg uintptr) { if pPg != 0 { _sqlite3PagerUnrefNotNull(tls, pPg) } } func _sqlite3PagerUnrefPageOne(tls *libc.TLS, pPg uintptr) { var pPager uintptr _ = pPager _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Page1 is never memory mapped */ pPager = (*TDbPage)(unsafe.Pointer(pPg)).FpPager _sqlite3PcacheRelease(tls, pPg) _pagerUnlockIfUnused(tls, pPager) } // C documentation // // /* // ** This function is called at the start of every write transaction. // ** There must already be a RESERVED or EXCLUSIVE lock on the database // ** file when this routine is called. // ** // ** Open the journal file for pager pPager and write a journal header // ** to the start of it. If there are active savepoints, open the sub-journal // ** as well. This function is only used when the journal file is being // ** opened to write a rollback log for a transaction. It is not used // ** when opening a hot journal file to roll it back. // ** // ** If the journal file is already open (as it may be in exclusive mode), // ** then this function just writes a journal header to the start of the // ** already open file. // ** // ** Whether or not the journal file is opened by this function, the // ** Pager.pInJournal bitvec structure is allocated. // ** // ** Return SQLITE_OK if everything is successful. Otherwise, return // ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or // ** an IO error code if opening or writing the journal file fails. // */ func _pager_open_journal(tls *libc.TLS, pPager uintptr) (r int32) { var flags, nSpill, rc int32 var pVfs uintptr _, _, _, _ = flags, nSpill, pVfs, rc rc = SQLITE_OK /* Return code */ pVfs = (*TPager)(unsafe.Pointer(pPager)).FpVfs /* Local cache of vfs pointer */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If already in the error state, this function is a no-op. But on ** the other hand, this routine is never called if we are already in ** an error state. */ if (*TPager)(unsafe.Pointer(pPager)).FerrCode != 0 { return (*TPager)(unsafe.Pointer(pPager)).FerrCode } if !((*TPager)(unsafe.Pointer(pPager)).FpWal != libc.UintptrFromInt32(0)) && int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) != int32(PAGER_JOURNALMODE_OFF) { (*TPager)(unsafe.Pointer(pPager)).FpInJournal = _sqlite3BitvecCreate(tls, (*TPager)(unsafe.Pointer(pPager)).FdbSize) if (*TPager)(unsafe.Pointer(pPager)).FpInJournal == uintptr(0) { return int32(SQLITE_NOMEM) } /* Open the journal file if it is not already open. */ if !((*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != libc.UintptrFromInt32(0)) { if int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) == int32(PAGER_JOURNALMODE_MEMORY) { _sqlite3MemJournalOpen(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd) } else { flags = libc.Int32FromInt32(SQLITE_OPEN_READWRITE) | libc.Int32FromInt32(SQLITE_OPEN_CREATE) if (*TPager)(unsafe.Pointer(pPager)).FtempFile != 0 { flags |= libc.Int32FromInt32(SQLITE_OPEN_DELETEONCLOSE) | libc.Int32FromInt32(SQLITE_OPEN_TEMP_JOURNAL) flags |= int32(SQLITE_OPEN_EXCLUSIVE) nSpill = _sqlite3Config.FnStmtSpill } else { flags |= int32(SQLITE_OPEN_MAIN_JOURNAL) nSpill = _jrnlBufferSize(tls, pPager) } /* Verify that the database still has the same name as it did when ** it was originally opened. */ rc = _databaseIsUnmoved(tls, pPager) if rc == SQLITE_OK { rc = _sqlite3JournalOpen(tls, pVfs, (*TPager)(unsafe.Pointer(pPager)).FzJournal, (*TPager)(unsafe.Pointer(pPager)).Fjfd, flags, nSpill) } } _ = libc.Int32FromInt32(0) } /* Write the first journal header to the journal file and open ** the sub-journal if necessary. */ if rc == SQLITE_OK { /* TODO: Check if all of these are really required. */ (*TPager)(unsafe.Pointer(pPager)).FnRec = 0 (*TPager)(unsafe.Pointer(pPager)).FjournalOff = 0 (*TPager)(unsafe.Pointer(pPager)).FsetSuper = uint8(0) (*TPager)(unsafe.Pointer(pPager)).FjournalHdr = 0 rc = _writeJournalHdr(tls, pPager) } } if rc != SQLITE_OK { _sqlite3BitvecDestroy(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal) (*TPager)(unsafe.Pointer(pPager)).FpInJournal = uintptr(0) (*TPager)(unsafe.Pointer(pPager)).FjournalOff = 0 } else { _ = libc.Int32FromInt32(0) (*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_WRITER_CACHEMOD) } return rc } // C documentation // // /* // ** Begin a write-transaction on the specified pager object. If a // ** write-transaction has already been opened, this function is a no-op. // ** // ** If the exFlag argument is false, then acquire at least a RESERVED // ** lock on the database file. If exFlag is true, then acquire at least // ** an EXCLUSIVE lock. If such a lock is already held, no locking // ** functions need be called. // ** // ** If the subjInMemory argument is non-zero, then any sub-journal opened // ** within this transaction will be opened as an in-memory file. This // ** has no effect if the sub-journal is already opened (as it may be when // ** running in exclusive mode) or if the transaction does not require a // ** sub-journal. If the subjInMemory argument is zero, then any required // ** sub-journal is implemented in-memory if pPager is an in-memory database, // ** or using a temporary file otherwise. // */ func _sqlite3PagerBegin(tls *libc.TLS, pPager uintptr, exFlag int32, subjInMemory int32) (r int32) { var rc int32 _ = rc rc = SQLITE_OK if (*TPager)(unsafe.Pointer(pPager)).FerrCode != 0 { return (*TPager)(unsafe.Pointer(pPager)).FerrCode } _ = libc.Int32FromInt32(0) (*TPager)(unsafe.Pointer(pPager)).FsubjInMemory = uint8(subjInMemory) if int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_READER) { _ = libc.Int32FromInt32(0) if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) { /* If the pager is configured to use locking_mode=exclusive, and an ** exclusive lock on the database is not already held, obtain it now. */ if (*TPager)(unsafe.Pointer(pPager)).FexclusiveMode != 0 && _sqlite3WalExclusiveMode(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, -int32(1)) != 0 { rc = _pagerLockDb(tls, pPager, int32(EXCLUSIVE_LOCK)) if rc != SQLITE_OK { return rc } _sqlite3WalExclusiveMode(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, int32(1)) } /* Grab the write lock on the log file. If successful, upgrade to ** PAGER_RESERVED state. Otherwise, return an error code to the caller. ** The busy-handler is not invoked if another connection already ** holds the write-lock. If possible, the upper layer will call it. */ rc = _sqlite3WalBeginWriteTransaction(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal) } else { /* Obtain a RESERVED lock on the database file. If the exFlag parameter ** is true, then immediately upgrade this to an EXCLUSIVE lock. The ** busy-handler callback can be used when upgrading to the EXCLUSIVE ** lock, but not when obtaining the RESERVED lock. */ rc = _pagerLockDb(tls, pPager, int32(RESERVED_LOCK)) if rc == SQLITE_OK && exFlag != 0 { rc = _pager_wait_on_lock(tls, pPager, int32(EXCLUSIVE_LOCK)) } } if rc == SQLITE_OK { /* Change to WRITER_LOCKED state. ** ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD ** when it has an open transaction, but never to DBMOD or FINISHED. ** This is because in those states the code to roll back savepoint ** transactions may copy data from the sub-journal into the database ** file as well as into the page cache. Which would be incorrect in ** WAL mode. */ (*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_WRITER_LOCKED) (*TPager)(unsafe.Pointer(pPager)).FdbHintSize = (*TPager)(unsafe.Pointer(pPager)).FdbSize (*TPager)(unsafe.Pointer(pPager)).FdbFileSize = (*TPager)(unsafe.Pointer(pPager)).FdbSize (*TPager)(unsafe.Pointer(pPager)).FdbOrigSize = (*TPager)(unsafe.Pointer(pPager)).FdbSize (*TPager)(unsafe.Pointer(pPager)).FjournalOff = 0 } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } return rc } // C documentation // // /* // ** Write page pPg onto the end of the rollback journal. // */ func _pagerAddPageToRollbackJournal(tls *libc.TLS, pPg uintptr) (r int32) { var cksum Tu32 var iOff Ti64 var pData2, pPager, p1 uintptr var rc int32 _, _, _, _, _, _ = cksum, iOff, pData2, pPager, rc, p1 pPager = (*TPgHdr)(unsafe.Pointer(pPg)).FpPager iOff = (*TPager)(unsafe.Pointer(pPager)).FjournalOff /* We should never write to the journal file the page that ** contains the database locks. The following assert verifies ** that we do not. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pData2 = (*TPgHdr)(unsafe.Pointer(pPg)).FpData cksum = _pager_cksum(tls, pPager, pData2) /* Even if an IO or diskfull error occurs while journalling the ** page in the block above, set the need-sync flag for the page. ** Otherwise, when the transaction is rolled back, the logic in ** playback_one_page() will think that the page needs to be restored ** in the database file. And if an IO error occurs while doing so, ** then corruption may follow. */ p1 = pPg + 28 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(PGHDR_NEED_SYNC)) rc = _write32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iOff, (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno) if rc != SQLITE_OK { return rc } rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, pData2, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), iOff+int64(4)) if rc != SQLITE_OK { return rc } rc = _write32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iOff+(*TPager)(unsafe.Pointer(pPager)).FpageSize+int64(4), cksum) if rc != SQLITE_OK { return rc } *(*Ti64)(unsafe.Pointer(pPager + 80)) += int64(8) + (*TPager)(unsafe.Pointer(pPager)).FpageSize (*TPager)(unsafe.Pointer(pPager)).FnRec++ _ = libc.Int32FromInt32(0) rc = _sqlite3BitvecSet(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal, (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno) _ = libc.Int32FromInt32(0) rc |= _addToSavepointBitvecs(tls, pPager, (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno) _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** Mark a single data page as writeable. The page is written into the // ** main journal or sub-journal as required. If the page is written into // ** one of the journals, the corresponding bit is set in the // ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs // ** of any open savepoints as appropriate. // */ func _pager_write(tls *libc.TLS, pPg uintptr) (r int32) { var pPager, p1, p2 uintptr var rc int32 _, _, _, _ = pPager, rc, p1, p2 pPager = (*TPgHdr)(unsafe.Pointer(pPg)).FpPager rc = SQLITE_OK /* This routine is not called unless a write-transaction has already ** been started. The journal file may or may not be open at this point. ** It is never called in the ERROR state. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* The journal file needs to be opened. Higher level routines have already ** obtained the necessary locks to begin the write-transaction, but the ** rollback journal might not yet be open. Open it now if this is the case. ** ** This is done before calling sqlite3PcacheMakeDirty() on the page. ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then ** an error might occur and the pager would end up in WRITER_LOCKED state ** with pages marked as dirty in the cache. */ if int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_WRITER_LOCKED) { rc = _pager_open_journal(tls, pPager) if rc != SQLITE_OK { return rc } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Mark the page that is about to be modified as dirty. */ _sqlite3PcacheMakeDirty(tls, pPg) /* If a rollback journal is in use, them make sure the page that is about ** to change is in the rollback journal, or if the page is a new page off ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC. */ _ = libc.Int32FromInt32(0) if (*TPager)(unsafe.Pointer(pPager)).FpInJournal != uintptr(0) && _sqlite3BitvecTestNotNull(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal, (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno) == 0 { _ = libc.Int32FromInt32(0) if (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno <= (*TPager)(unsafe.Pointer(pPager)).FdbOrigSize { rc = _pagerAddPageToRollbackJournal(tls, pPg) if rc != SQLITE_OK { return rc } } else { if int32((*TPager)(unsafe.Pointer(pPager)).FeState) != int32(PAGER_WRITER_DBMOD) { p1 = pPg + 28 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(PGHDR_NEED_SYNC)) } } } /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list ** and before writing the page into the rollback journal. Wait until now, ** after the page has been successfully journalled, before setting the ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified. */ p2 = pPg + 28 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(PGHDR_WRITEABLE)) /* If the statement journal is open and the page is not in it, ** then write the page into the statement journal. */ if (*TPager)(unsafe.Pointer(pPager)).FnSavepoint > 0 { rc = _subjournalPageIfRequired(tls, pPg) } /* Update the database size and return. */ if (*TPager)(unsafe.Pointer(pPager)).FdbSize < (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno { (*TPager)(unsafe.Pointer(pPager)).FdbSize = (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno } return rc } // C documentation // // /* // ** This is a variant of sqlite3PagerWrite() that runs when the sector size // ** is larger than the page size. SQLite makes the (reasonable) assumption that // ** all bytes of a sector are written together by hardware. Hence, all bytes of // ** a sector need to be journalled in case of a power loss in the middle of // ** a write. // ** // ** Usually, the sector size is less than or equal to the page size, in which // ** case pages can be individually written. This routine only runs in the // ** exceptional case where the page size is smaller than the sector size. // */ func _pagerWriteLargeSector(tls *libc.TLS, pPg uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var ii, nPage, needSync, rc int32 var nPageCount, nPagePerSector, pg, pg1 TPgno var pPage1, pPager, v3, p1, p5, p6 uintptr var _ /* pPage at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _ = ii, nPage, nPageCount, nPagePerSector, needSync, pPage1, pPager, pg, pg1, rc, v3, p1, p5, p6 rc = SQLITE_OK /* First page of the sector pPg is located on. */ nPage = 0 /* Loop counter */ needSync = 0 /* True if any page has PGHDR_NEED_SYNC */ pPager = (*TPgHdr)(unsafe.Pointer(pPg)).FpPager /* The pager that owns pPg */ nPagePerSector = uint32(int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) / (*TPager)(unsafe.Pointer(pPager)).FpageSize) /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow ** a journal header to be written between the pages journaled by ** this function. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) p1 = pPager + 21 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | libc.Int32FromInt32(SPILLFLAG_NOSYNC)) /* This trick assumes that both the page-size and sector-size are ** an integer power of 2. It sets variable pg1 to the identifier ** of the first page of the sector pPg is located on. */ pg1 = ((*TPgHdr)(unsafe.Pointer(pPg)).Fpgno-uint32(1)) & ^(nPagePerSector-libc.Uint32FromInt32(1)) + uint32(1) nPageCount = (*TPager)(unsafe.Pointer(pPager)).FdbSize if (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno > nPageCount { nPage = int32((*TPgHdr)(unsafe.Pointer(pPg)).Fpgno - pg1 + uint32(1)) } else { if pg1+nPagePerSector-uint32(1) > nPageCount { nPage = int32(nPageCount + uint32(1) - pg1) } else { nPage = int32(nPagePerSector) } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) ii = 0 for { if !(ii < nPage && rc == SQLITE_OK) { break } pg = pg1 + uint32(ii) if pg == (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno || !(_sqlite3BitvecTest(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal, pg) != 0) { if pg != (*TPager)(unsafe.Pointer(pPager)).FlckPgno { rc = _sqlite3PagerGet(tls, pPager, pg, bp, 0) if rc == SQLITE_OK { rc = _pager_write(tls, *(*uintptr)(unsafe.Pointer(bp))) if int32((*TPgHdr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fflags)&int32(PGHDR_NEED_SYNC) != 0 { needSync = int32(1) } _sqlite3PagerUnrefNotNull(tls, *(*uintptr)(unsafe.Pointer(bp))) } } } else { v3 = _sqlite3PagerLookup(tls, pPager, pg) *(*uintptr)(unsafe.Pointer(bp)) = v3 if v3 != uintptr(0) { if int32((*TPgHdr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fflags)&int32(PGHDR_NEED_SYNC) != 0 { needSync = int32(1) } _sqlite3PagerUnrefNotNull(tls, *(*uintptr)(unsafe.Pointer(bp))) } } goto _2 _2: ; ii++ } /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages ** starting at pg1, then it needs to be set for all of them. Because ** writing to any of these nPage pages may damage the others, the ** journal file must contain sync()ed copies of all of them ** before any of them can be written out to the database file. */ if rc == SQLITE_OK && needSync != 0 { _ = libc.Int32FromInt32(0) ii = 0 for { if !(ii < nPage) { break } pPage1 = _sqlite3PagerLookup(tls, pPager, pg1+uint32(ii)) if pPage1 != 0 { p5 = pPage1 + 28 *(*Tu16)(unsafe.Pointer(p5)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p5))) | libc.Int32FromInt32(PGHDR_NEED_SYNC)) _sqlite3PagerUnrefNotNull(tls, pPage1) } goto _4 _4: ; ii++ } } _ = libc.Int32FromInt32(0) p6 = pPager + 21 *(*Tu8)(unsafe.Pointer(p6)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p6))) & ^libc.Int32FromInt32(SPILLFLAG_NOSYNC)) return rc } // C documentation // // /* // ** Mark a data page as writeable. This routine must be called before // ** making changes to a page. The caller must check the return value // ** of this function and be careful not to change any page data unless // ** this routine returns SQLITE_OK. // ** // ** The difference between this function and pager_write() is that this // ** function also deals with the special case where 2 or more pages // ** fit on a single disk sector. In this case all co-resident pages // ** must have been written to the journal file before returning. // ** // ** If an error occurs, SQLITE_NOMEM or an IO error code is returned // ** as appropriate. Otherwise, SQLITE_OK. // */ func _sqlite3PagerWrite(tls *libc.TLS, pPg uintptr) (r int32) { var pPager uintptr _ = pPager pPager = (*TPgHdr)(unsafe.Pointer(pPg)).FpPager _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TPgHdr)(unsafe.Pointer(pPg)).Fflags)&int32(PGHDR_WRITEABLE) != 0 && (*TPager)(unsafe.Pointer(pPager)).FdbSize >= (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno { if (*TPager)(unsafe.Pointer(pPager)).FnSavepoint != 0 { return _subjournalPageIfRequired(tls, pPg) } return SQLITE_OK } else { if (*TPager)(unsafe.Pointer(pPager)).FerrCode != 0 { return (*TPager)(unsafe.Pointer(pPager)).FerrCode } else { if (*TPager)(unsafe.Pointer(pPager)).FsectorSize > uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize) { _ = libc.Int32FromInt32(0) return _pagerWriteLargeSector(tls, pPg) } else { return _pager_write(tls, pPg) } } } return r } /* ** Return TRUE if the page given in the argument was previously passed ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok ** to change the content of the page. */ // C documentation // // /* // ** A call to this routine tells the pager that it is not necessary to // ** write the information on page pPg back to the disk, even though // ** that page might be marked as dirty. This happens, for example, when // ** the page has been added as a leaf of the freelist and so its // ** content no longer matters. // ** // ** The overlying software layer calls this routine when all of the data // ** on the given page is unused. The pager marks the page as clean so // ** that it does not get written to disk. // ** // ** Tests show that this optimization can quadruple the speed of large // ** DELETE operations. // ** // ** This optimization cannot be used with a temp-file, as the page may // ** have been dirty at the start of the transaction. In that case, if // ** memory pressure forces page pPg out of the cache, the data does need // ** to be written out to disk so that it may be read back in if the // ** current transaction is rolled back. // */ func _sqlite3PagerDontWrite(tls *libc.TLS, pPg uintptr) { var pPager, p1, p2 uintptr _, _, _ = pPager, p1, p2 pPager = (*TPgHdr)(unsafe.Pointer(pPg)).FpPager if !((*TPager)(unsafe.Pointer(pPager)).FtempFile != 0) && int32((*TPgHdr)(unsafe.Pointer(pPg)).Fflags)&int32(PGHDR_DIRTY) != 0 && (*TPager)(unsafe.Pointer(pPager)).FnSavepoint == 0 { p1 = pPg + 28 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(PGHDR_DONT_WRITE)) p2 = pPg + 28 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) & ^libc.Int32FromInt32(PGHDR_WRITEABLE)) } } // C documentation // // /* // ** This routine is called to increment the value of the database file // ** change-counter, stored as a 4-byte big-endian integer starting at // ** byte offset 24 of the pager file. The secondary change counter at // ** 92 is also updated, as is the SQLite version number at offset 96. // ** // ** But this only happens if the pPager->changeCountDone flag is false. // ** To avoid excess churning of page 1, the update only happens once. // ** See also the pager_write_changecounter() routine that does an // ** unconditional update of the change counters. // ** // ** If the isDirectMode flag is zero, then this is done by calling // ** sqlite3PagerWrite() on page 1, then modifying the contents of the // ** page data. In this case the file will be updated when the current // ** transaction is committed. // ** // ** The isDirectMode flag may only be non-zero if the library was compiled // ** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case, // ** if isDirect is non-zero, then the database file is updated directly // ** by writing an updated version of page 1 using a call to the // ** sqlite3OsWrite() function. // */ func _pager_incr_changecounter(tls *libc.TLS, pPager uintptr, isDirectMode int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pCopy, zBuf uintptr var rc int32 var _ /* pPgHdr at bp+0 */ uintptr _, _, _ = pCopy, rc, zBuf rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Declare and initialize constant integer 'isDirect'. If the ** atomic-write optimization is enabled in this build, then isDirect ** is initialized to the value passed as the isDirectMode parameter ** to this function. Otherwise, it is always set to zero. ** ** The idea is that if the atomic-write optimization is not ** enabled at compile time, the compiler can omit the tests of ** 'isDirect' below, as well as the block enclosed in the ** "if( isDirect )" condition. */ _ = libc.Int32FromInt32(0) _ = isDirectMode if !((*TPager)(unsafe.Pointer(pPager)).FchangeCountDone != 0) && (*TPager)(unsafe.Pointer(pPager)).FdbSize > uint32(0) { /* Reference to page 1 */ _ = libc.Int32FromInt32(0) /* Open page 1 of the file for writing. */ rc = _sqlite3PagerGet(tls, pPager, uint32(1), bp, 0) _ = libc.Int32FromInt32(0) /* If page one was fetched successfully, and this function is not ** operating in direct-mode, make page 1 writable. When not in ** direct mode, page 1 is always held in cache and hence the PagerGet() ** above is always successful - hence the ALWAYS on rc==SQLITE_OK. */ if libc.Bool(!(libc.Int32FromInt32(DIRECT_MODE) != 0)) && rc == SQLITE_OK { rc = _sqlite3PagerWrite(tls, *(*uintptr)(unsafe.Pointer(bp))) } if rc == SQLITE_OK { /* Actually do the update of the change counter */ _pager_write_changecounter(tls, *(*uintptr)(unsafe.Pointer(bp))) /* If running in direct mode, write the contents of page 1 to the file. */ if DIRECT_MODE != 0 { _ = libc.Int32FromInt32(0) zBuf = (*TPgHdr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpData if rc == SQLITE_OK { rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, zBuf, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), 0) *(*Tu32)(unsafe.Pointer(pPager + 200 + 2*4))++ } if rc == SQLITE_OK { /* Update the pager's copy of the change-counter. Otherwise, the ** next time a read transaction is opened the cache will be ** flushed (as the change-counter values will not match). */ pCopy = zBuf + 24 libc.Xmemcpy(tls, pPager+112, pCopy, uint32(16)) (*TPager)(unsafe.Pointer(pPager)).FchangeCountDone = uint8(1) } } else { (*TPager)(unsafe.Pointer(pPager)).FchangeCountDone = uint8(1) } } /* Release the page reference. */ _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp))) } return rc } // C documentation // // /* // ** Sync the database file to disk. This is a no-op for in-memory databases // ** or pages with the Pager.noSync flag set. // ** // ** If successful, or if called on a pager for which it is a no-op, this // ** function returns SQLITE_OK. Otherwise, an IO error code is returned. // */ func _sqlite3PagerSync(tls *libc.TLS, pPager uintptr, zSuper uintptr) (r int32) { var pArg uintptr var rc int32 _, _ = pArg, rc rc = SQLITE_OK pArg = zSuper rc = _sqlite3OsFileControl(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int32(SQLITE_FCNTL_SYNC), pArg) if rc == int32(SQLITE_NOTFOUND) { rc = SQLITE_OK } if rc == SQLITE_OK && !((*TPager)(unsafe.Pointer(pPager)).FnoSync != 0) { _ = libc.Int32FromInt32(0) rc = _sqlite3OsSync(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int32((*TPager)(unsafe.Pointer(pPager)).FsyncFlags)) } return rc } // C documentation // // /* // ** This function may only be called while a write-transaction is active in // ** rollback. If the connection is in WAL mode, this call is a no-op. // ** Otherwise, if the connection does not already have an EXCLUSIVE lock on // ** the database file, an attempt is made to obtain one. // ** // ** If the EXCLUSIVE lock is already held or the attempt to obtain it is // ** successful, or the connection is in WAL mode, SQLITE_OK is returned. // ** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is // ** returned. // */ func _sqlite3PagerExclusiveLock(tls *libc.TLS, pPager uintptr) (r int32) { var rc int32 _ = rc rc = (*TPager)(unsafe.Pointer(pPager)).FerrCode _ = libc.Int32FromInt32(0) if rc == SQLITE_OK { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if 0 == libc.BoolInt32((*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0)) { rc = _pager_wait_on_lock(tls, pPager, int32(EXCLUSIVE_LOCK)) } } return rc } // C documentation // // /* // ** Sync the database file for the pager pPager. zSuper points to the name // ** of a super-journal file that should be written into the individual // ** journal file. zSuper may be NULL, which is interpreted as no // ** super-journal (a single database transaction). // ** // ** This routine ensures that: // ** // ** * The database file change-counter is updated, // ** * the journal is synced (unless the atomic-write optimization is used), // ** * all dirty pages are written to the database file, // ** * the database file is truncated (if required), and // ** * the database file synced. // ** // ** The only thing that remains to commit the transaction is to finalize // ** (delete, truncate or zero the first part of) the journal file (or // ** delete the super-journal file if specified). // ** // ** Note that if zSuper==NULL, this does not overwrite a previous value // ** passed to an sqlite3PagerCommitPhaseOne() call. // ** // ** If the final parameter - noSync - is true, then the database file itself // ** is not synced. The caller must call sqlite3PagerSync() directly to // ** sync the database file before calling CommitPhaseTwo() to delete the // ** journal file in this case. // */ func _sqlite3PagerCommitPhaseOne(tls *libc.TLS, pPager uintptr, zSuper uintptr, noSync int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var nNew TPgno var pList uintptr var rc int32 var _ /* pPageOne at bp+0 */ uintptr _, _, _ = nNew, pList, rc rc = SQLITE_OK /* Return code */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If a prior error occurred, report that error again. */ if (*TPager)(unsafe.Pointer(pPager)).FerrCode != 0 { return (*TPager)(unsafe.Pointer(pPager)).FerrCode } /* Provide the ability to easily simulate an I/O error during testing */ if _sqlite3FaultSim(tls, int32(400)) != 0 { return int32(SQLITE_IOERR) } /* If no database changes have been made, return early. */ if int32((*TPager)(unsafe.Pointer(pPager)).FeState) < int32(PAGER_WRITER_CACHEMOD) { return SQLITE_OK } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if 0 == _pagerFlushOnCommit(tls, pPager, int32(1)) { /* If this is an in-memory db, or no pages have been written to, or this ** function has already been called, it is mostly a no-op. However, any ** backup in progress needs to be restarted. */ _sqlite3BackupRestart(tls, (*TPager)(unsafe.Pointer(pPager)).FpBackup) } else { if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) pList = _sqlite3PcacheDirtyList(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) if pList == uintptr(0) { /* Must have at least one page for the WAL commit flag. ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */ rc = _sqlite3PagerGet(tls, pPager, uint32(1), bp, 0) pList = *(*uintptr)(unsafe.Pointer(bp)) (*TPgHdr)(unsafe.Pointer(pList)).FpDirty = uintptr(0) } _ = libc.Int32FromInt32(0) if pList != 0 { rc = _pagerWalFrames(tls, pPager, pList, (*TPager)(unsafe.Pointer(pPager)).FdbSize, int32(1)) } _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp))) if rc == SQLITE_OK { _sqlite3PcacheCleanAll(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) } } else { /* The bBatch boolean is true if the batch-atomic-write commit method ** should be used. No rollback journal is created if batch-atomic-write ** is enabled. */ rc = _pager_incr_changecounter(tls, pPager, 0) if rc != SQLITE_OK { goto commit_phase_one_exit } /* Write the super-journal name into the journal file. If a ** super-journal file name has already been written to the journal file, ** or if zSuper is NULL (no super-journal), then this call is a no-op. */ rc = _writeSuperJournal(tls, pPager, zSuper) if rc != SQLITE_OK { goto commit_phase_one_exit } /* Sync the journal file and write all dirty pages to the database. ** If the atomic-update optimization is being used, this sync will not ** create the journal file or perform any real IO. ** ** Because the change-counter page was just modified, unless the ** atomic-update optimization is used it is almost certain that the ** journal requires a sync here. However, in locking_mode=exclusive ** on a system under memory pressure it is just possible that this is ** not the case. In this case it is likely enough that the redundant ** xSync() call will be changed to a no-op by the OS anyhow. */ rc = _syncJournal(tls, pPager, 0) if rc != SQLITE_OK { goto commit_phase_one_exit } pList = _sqlite3PcacheDirtyList(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) if true { rc = _pager_write_pagelist(tls, pPager, pList) } if rc != SQLITE_OK { _ = libc.Int32FromInt32(0) goto commit_phase_one_exit } _sqlite3PcacheCleanAll(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) /* If the file on disk is smaller than the database image, use ** pager_truncate to grow the file here. This can happen if the database ** image was extended as part of the current transaction and then the ** last page in the db image moved to the free-list. In this case the ** last page is never written out to disk, leaving the database file ** undersized. Fix this now if it is the case. */ if (*TPager)(unsafe.Pointer(pPager)).FdbSize > (*TPager)(unsafe.Pointer(pPager)).FdbFileSize { nNew = (*TPager)(unsafe.Pointer(pPager)).FdbSize - libc.BoolUint32((*TPager)(unsafe.Pointer(pPager)).FdbSize == (*TPager)(unsafe.Pointer(pPager)).FlckPgno) _ = libc.Int32FromInt32(0) rc = _pager_truncate(tls, pPager, nNew) if rc != SQLITE_OK { goto commit_phase_one_exit } } /* Finally, sync the database file. */ if !(noSync != 0) { rc = _sqlite3PagerSync(tls, pPager, zSuper) } } } goto commit_phase_one_exit commit_phase_one_exit: ; if rc == SQLITE_OK && !((*TPager)(unsafe.Pointer(pPager)).FpWal != libc.UintptrFromInt32(0)) { (*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_WRITER_FINISHED) } return rc } // C documentation // // /* // ** When this function is called, the database file has been completely // ** updated to reflect the changes made by the current transaction and // ** synced to disk. The journal file still exists in the file-system // ** though, and if a failure occurs at this point it will eventually // ** be used as a hot-journal and the current transaction rolled back. // ** // ** This function finalizes the journal file, either by deleting, // ** truncating or partially zeroing it, so that it cannot be used // ** for hot-journal rollback. Once this is done the transaction is // ** irrevocably committed. // ** // ** If an error occurs, an IO error code is returned and the pager // ** moves into the error state. Otherwise, SQLITE_OK is returned. // */ func _sqlite3PagerCommitPhaseTwo(tls *libc.TLS, pPager uintptr) (r int32) { var rc int32 _ = rc rc = SQLITE_OK /* Return code */ /* This routine should not be called if a prior error has occurred. ** But if (due to a coding error elsewhere in the system) it does get ** called, just return the same error code without doing anything. */ if (*TPager)(unsafe.Pointer(pPager)).FerrCode != 0 { return (*TPager)(unsafe.Pointer(pPager)).FerrCode } (*TPager)(unsafe.Pointer(pPager)).FiDataVersion++ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* An optimization. If the database was not actually modified during ** this transaction, the pager is running in exclusive-mode and is ** using persistent journals, then this function is a no-op. ** ** The start of the journal file currently contains a single journal ** header with the nRec field set to 0. If such a journal is used as ** a hot-journal during hot-journal rollback, 0 changes will be made ** to the database file. So there is no need to zero the journal ** header. Since the pager is in exclusive mode, there is no need ** to drop any locks either. */ if int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_WRITER_LOCKED) && (*TPager)(unsafe.Pointer(pPager)).FexclusiveMode != 0 && int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) == int32(PAGER_JOURNALMODE_PERSIST) { _ = libc.Int32FromInt32(0) (*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_READER) return SQLITE_OK } rc = _pager_end_transaction(tls, pPager, int32((*TPager)(unsafe.Pointer(pPager)).FsetSuper), int32(1)) return _pager_error(tls, pPager, rc) } // C documentation // // /* // ** If a write transaction is open, then all changes made within the // ** transaction are reverted and the current write-transaction is closed. // ** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR // ** state if an error occurs. // ** // ** If the pager is already in PAGER_ERROR state when this function is called, // ** it returns Pager.errCode immediately. No work is performed in this case. // ** // ** Otherwise, in rollback mode, this function performs two functions: // ** // ** 1) It rolls back the journal file, restoring all database file and // ** in-memory cache pages to the state they were in when the transaction // ** was opened, and // ** // ** 2) It finalizes the journal file, so that it is not used for hot // ** rollback at any point in the future. // ** // ** Finalization of the journal file (task 2) is only performed if the // ** rollback is successful. // ** // ** In WAL mode, all cache-entries containing data modified within the // ** current transaction are either expelled from the cache or reverted to // ** their pre-transaction state by re-reading data from the database or // ** WAL files. The WAL transaction is then closed. // */ func _sqlite3PagerRollback(tls *libc.TLS, pPager uintptr) (r int32) { var eState, rc, rc2 int32 _, _, _ = eState, rc, rc2 rc = SQLITE_OK /* Return code */ /* PagerRollback() is a no-op if called in READER or OPEN state. If ** the pager is already in the ERROR state, the rollback is not ** attempted here. Instead, the error code is returned to the caller. */ _ = libc.Int32FromInt32(0) if int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_ERROR) { return (*TPager)(unsafe.Pointer(pPager)).FerrCode } if int32((*TPager)(unsafe.Pointer(pPager)).FeState) <= int32(PAGER_READER) { return SQLITE_OK } if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) { rc = _sqlite3PagerSavepoint(tls, pPager, int32(SAVEPOINT_ROLLBACK), -int32(1)) rc2 = _pager_end_transaction(tls, pPager, int32((*TPager)(unsafe.Pointer(pPager)).FsetSuper), 0) if rc == SQLITE_OK { rc = rc2 } } else { if !((*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != libc.UintptrFromInt32(0)) || int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_WRITER_LOCKED) { eState = int32((*TPager)(unsafe.Pointer(pPager)).FeState) rc = _pager_end_transaction(tls, pPager, 0, 0) if !((*TPager)(unsafe.Pointer(pPager)).FmemDb != 0) && eState > int32(PAGER_WRITER_LOCKED) { /* This can happen using journal_mode=off. Move the pager to the error ** state to indicate that the contents of the cache may not be trusted. ** Any active readers will get SQLITE_ABORT. */ (*TPager)(unsafe.Pointer(pPager)).FerrCode = int32(SQLITE_ABORT) (*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_ERROR) _setGetterMethod(tls, pPager) return rc } } else { rc = _pager_playback(tls, pPager, 0) } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If an error occurs during a ROLLBACK, we can no longer trust the pager ** cache. So call pager_error() on the way out to make any error persistent. */ return _pager_error(tls, pPager, rc) } // C documentation // // /* // ** Return TRUE if the database file is opened read-only. Return FALSE // ** if the database is (in theory) writable. // */ func _sqlite3PagerIsreadonly(tls *libc.TLS, pPager uintptr) (r Tu8) { return (*TPager)(unsafe.Pointer(pPager)).FreadOnly } // C documentation // // /* // ** Return the approximate number of bytes of memory currently // ** used by the pager and its associated cache. // */ func _sqlite3PagerMemUsed(tls *libc.TLS, pPager uintptr) (r int32) { var perPageSize int32 _ = perPageSize perPageSize = int32((*TPager)(unsafe.Pointer(pPager)).FpageSize + int64((*TPager)(unsafe.Pointer(pPager)).FnExtra) + int64(int32(libc.Uint32FromInt64(48)+libc.Uint32FromInt32(5)*libc.Uint32FromInt64(4)))) return int32(int64(perPageSize*_sqlite3PcachePagecount(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache)+_sqlite3MallocSize(tls, pPager)) + (*TPager)(unsafe.Pointer(pPager)).FpageSize) } // C documentation // // /* // ** Return the number of references to the specified page. // */ func _sqlite3PagerPageRefcount(tls *libc.TLS, pPage uintptr) (r int32) { return int32(_sqlite3PcachePageRefcount(tls, pPage)) } // C documentation // // /* // ** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE, // ** or _WRITE+1. The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation // ** of SQLITE_DBSTATUS_CACHE_SPILL. The _SPILL case is not contiguous because // ** it was added later. // ** // ** Before returning, *pnVal is incremented by the // ** current cache hit or miss count, according to the value of eStat. If the // ** reset parameter is non-zero, the cache hit or miss count is zeroed before // ** returning. // */ func _sqlite3PagerCacheStat(tls *libc.TLS, pPager uintptr, eStat int32, reset int32, pnVal uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) eStat -= int32(SQLITE_DBSTATUS_CACHE_HIT) *(*Tu64)(unsafe.Pointer(pnVal)) += uint64(*(*Tu32)(unsafe.Pointer(pPager + 200 + uintptr(eStat)*4))) if reset != 0 { *(*Tu32)(unsafe.Pointer(pPager + 200 + uintptr(eStat)*4)) = uint32(0) } } // C documentation // // /* // ** Return true if this is an in-memory or temp-file backed pager. // */ func _sqlite3PagerIsMemdb(tls *libc.TLS, pPager uintptr) (r int32) { return libc.BoolInt32((*TPager)(unsafe.Pointer(pPager)).FtempFile != 0 || (*TPager)(unsafe.Pointer(pPager)).FmemVfs != 0) } // C documentation // // /* // ** Check that there are at least nSavepoint savepoints open. If there are // ** currently less than nSavepoints open, then open one or more savepoints // ** to make up the difference. If the number of savepoints is already // ** equal to nSavepoint, then this function is a no-op. // ** // ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error // ** occurs while opening the sub-journal file, then an IO error code is // ** returned. Otherwise, SQLITE_OK. // */ func _pagerOpenSavepoint(tls *libc.TLS, pPager uintptr, nSavepoint int32) (r int32) { var aNew uintptr var ii, nCurrent, rc int32 _, _, _, _ = aNew, ii, nCurrent, rc rc = SQLITE_OK /* Return code */ nCurrent = (*TPager)(unsafe.Pointer(pPager)).FnSavepoint /* New Pager.aSavepoint array */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM ** if the allocation fails. Otherwise, zero the new portion in case a ** malloc failure occurs while populating it in the for(...) loop below. */ aNew = _sqlite3Realloc(tls, (*TPager)(unsafe.Pointer(pPager)).FaSavepoint, uint64(uint32(48)*uint32(nSavepoint))) if !(aNew != 0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, aNew+uintptr(nCurrent)*48, 0, uint32(nSavepoint-nCurrent)*uint32(48)) (*TPager)(unsafe.Pointer(pPager)).FaSavepoint = aNew /* Populate the PagerSavepoint structures just allocated. */ ii = nCurrent for { if !(ii < nSavepoint) { break } (*(*TPagerSavepoint)(unsafe.Pointer(aNew + uintptr(ii)*48))).FnOrig = (*TPager)(unsafe.Pointer(pPager)).FdbSize if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != uintptr(0) && (*TPager)(unsafe.Pointer(pPager)).FjournalOff > 0 { (*(*TPagerSavepoint)(unsafe.Pointer(aNew + uintptr(ii)*48))).FiOffset = (*TPager)(unsafe.Pointer(pPager)).FjournalOff } else { (*(*TPagerSavepoint)(unsafe.Pointer(aNew + uintptr(ii)*48))).FiOffset = int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) } (*(*TPagerSavepoint)(unsafe.Pointer(aNew + uintptr(ii)*48))).FiSubRec = (*TPager)(unsafe.Pointer(pPager)).FnSubRec (*(*TPagerSavepoint)(unsafe.Pointer(aNew + uintptr(ii)*48))).FpInSavepoint = _sqlite3BitvecCreate(tls, (*TPager)(unsafe.Pointer(pPager)).FdbSize) (*(*TPagerSavepoint)(unsafe.Pointer(aNew + uintptr(ii)*48))).FbTruncateOnRelease = int32(1) if !((*(*TPagerSavepoint)(unsafe.Pointer(aNew + uintptr(ii)*48))).FpInSavepoint != 0) { return int32(SQLITE_NOMEM) } if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) { _sqlite3WalSavepoint(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, aNew+uintptr(ii)*48+32) } (*TPager)(unsafe.Pointer(pPager)).FnSavepoint = ii + int32(1) goto _1 _1: ; ii++ } _ = libc.Int32FromInt32(0) return rc } func _sqlite3PagerOpenSavepoint(tls *libc.TLS, pPager uintptr, nSavepoint int32) (r int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if nSavepoint > (*TPager)(unsafe.Pointer(pPager)).FnSavepoint && (*TPager)(unsafe.Pointer(pPager)).FuseJournal != 0 { return _pagerOpenSavepoint(tls, pPager, nSavepoint) } else { return SQLITE_OK } return r } // C documentation // // /* // ** This function is called to rollback or release (commit) a savepoint. // ** The savepoint to release or rollback need not be the most recently // ** created savepoint. // ** // ** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE. // ** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with // ** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes // ** that have occurred since the specified savepoint was created. // ** // ** The savepoint to rollback or release is identified by parameter // ** iSavepoint. A value of 0 means to operate on the outermost savepoint // ** (the first created). A value of (Pager.nSavepoint-1) means operate // ** on the most recently created savepoint. If iSavepoint is greater than // ** (Pager.nSavepoint-1), then this function is a no-op. // ** // ** If a negative value is passed to this function, then the current // ** transaction is rolled back. This is different to calling // ** sqlite3PagerRollback() because this function does not terminate // ** the transaction or unlock the database, it just restores the // ** contents of the database to its original state. // ** // ** In any case, all savepoints with an index greater than iSavepoint // ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE), // ** then savepoint iSavepoint is also destroyed. // ** // ** This function may return SQLITE_NOMEM if a memory allocation fails, // ** or an IO error code if an IO error occurs while rolling back a // ** savepoint. If no errors occur, SQLITE_OK is returned. // */ func _sqlite3PagerSavepoint(tls *libc.TLS, pPager uintptr, op int32, iSavepoint int32) (r int32) { var ii, nNew, rc, v1 int32 var pRel, pSavepoint, v3 uintptr var sz Ti64 _, _, _, _, _, _, _, _ = ii, nNew, pRel, pSavepoint, rc, sz, v1, v3 rc = (*TPager)(unsafe.Pointer(pPager)).FerrCode _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if rc == SQLITE_OK && iSavepoint < (*TPager)(unsafe.Pointer(pPager)).FnSavepoint { /* Number of remaining savepoints after this op. */ /* Figure out how many savepoints will still be active after this ** operation. Store this value in nNew. Then free resources associated ** with any savepoints that are destroyed by this operation. */ if op == int32(SAVEPOINT_RELEASE) { v1 = 0 } else { v1 = int32(1) } nNew = iSavepoint + v1 ii = nNew for { if !(ii < (*TPager)(unsafe.Pointer(pPager)).FnSavepoint) { break } _sqlite3BitvecDestroy(tls, (*(*TPagerSavepoint)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(ii)*48))).FpInSavepoint) goto _2 _2: ; ii++ } (*TPager)(unsafe.Pointer(pPager)).FnSavepoint = nNew /* Truncate the sub-journal so that it only includes the parts ** that are still in use. */ if op == int32(SAVEPOINT_RELEASE) { pRel = (*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(nNew)*48 if (*TPagerSavepoint)(unsafe.Pointer(pRel)).FbTruncateOnRelease != 0 && (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fsjfd)).FpMethods != uintptr(0) { /* Only truncate if it is an in-memory sub-journal. */ if _sqlite3JournalIsInMemory(tls, (*TPager)(unsafe.Pointer(pPager)).Fsjfd) != 0 { sz = ((*TPager)(unsafe.Pointer(pPager)).FpageSize + int64(4)) * int64((*TPagerSavepoint)(unsafe.Pointer(pRel)).FiSubRec) rc = _sqlite3OsTruncate(tls, (*TPager)(unsafe.Pointer(pPager)).Fsjfd, sz) _ = libc.Int32FromInt32(0) } (*TPager)(unsafe.Pointer(pPager)).FnSubRec = (*TPagerSavepoint)(unsafe.Pointer(pRel)).FiSubRec } } else { if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) || (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != uintptr(0) { if nNew == 0 { v3 = uintptr(0) } else { v3 = (*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(nNew-int32(1))*48 } pSavepoint = v3 rc = _pagerPlaybackSavepoint(tls, pPager, pSavepoint) _ = libc.Int32FromInt32(0) } } } return rc } // C documentation // // /* // ** Return the full pathname of the database file. // ** // ** Except, if the pager is in-memory only, then return an empty string if // ** nullIfMemDb is true. This routine is called with nullIfMemDb==1 when // ** used to report the filename to the user, for compatibility with legacy // ** behavior. But when the Btree needs to know the filename for matching to // ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can // ** participate in shared-cache. // ** // ** The return value to this routine is always safe to use with // ** sqlite3_uri_parameter() and sqlite3_filename_database() and friends. // */ func _sqlite3PagerFilename(tls *libc.TLS, pPager uintptr, nullIfMemDb int32) (r uintptr) { if nullIfMemDb != 0 && ((*TPager)(unsafe.Pointer(pPager)).FmemDb != 0 || _sqlite3IsMemdb(tls, (*TPager)(unsafe.Pointer(pPager)).FpVfs) != 0) { return uintptr(unsafe.Pointer(&_zFake)) + 4 } else { return (*TPager)(unsafe.Pointer(pPager)).FzFilename } return r } var _zFake = [8]uint8{} // C documentation // // /* // ** Return the VFS structure for the pager. // */ func _sqlite3PagerVfs(tls *libc.TLS, pPager uintptr) (r uintptr) { return (*TPager)(unsafe.Pointer(pPager)).FpVfs } // C documentation // // /* // ** Return the file handle for the database file associated // ** with the pager. This might return NULL if the file has // ** not yet been opened. // */ func _sqlite3PagerFile(tls *libc.TLS, pPager uintptr) (r uintptr) { return (*TPager)(unsafe.Pointer(pPager)).Ffd } // C documentation // // /* // ** Return the file handle for the journal file (if it exists). // ** This will be either the rollback journal or the WAL file. // */ func _sqlite3PagerJrnlFile(tls *libc.TLS, pPager uintptr) (r uintptr) { var v1 uintptr _ = v1 if (*TPager)(unsafe.Pointer(pPager)).FpWal != 0 { v1 = _sqlite3WalFile(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal) } else { v1 = (*TPager)(unsafe.Pointer(pPager)).Fjfd } return v1 } // C documentation // // /* // ** Return the full pathname of the journal file. // */ func _sqlite3PagerJournalname(tls *libc.TLS, pPager uintptr) (r uintptr) { return (*TPager)(unsafe.Pointer(pPager)).FzJournal } // C documentation // // /* // ** Move the page pPg to location pgno in the file. // ** // ** There must be no references to the page previously located at // ** pgno (which we call pPgOld) though that page is allowed to be // ** in cache. If the page previously located at pgno is not already // ** in the rollback journal, it is not put there by by this routine. // ** // ** References to the page pPg remain valid. Updating any // ** meta-data associated with pPg (i.e. data stored in the nExtra bytes // ** allocated along with the page) is the responsibility of the caller. // ** // ** A transaction must be active when this routine is called. It used to be // ** required that a statement transaction was not active, but this restriction // ** has been removed (CREATE INDEX needs to move a page when a statement // ** transaction is active). // ** // ** If the fourth argument, isCommit, is non-zero, then this page is being // ** moved as part of a database reorganization just before the transaction // ** is being committed. In this case, it is guaranteed that the database page // ** pPg refers to will not be written to again within this transaction. // ** // ** This function may return SQLITE_NOMEM or an IO error code if an error // ** occurs. Otherwise, it returns SQLITE_OK. // */ func _sqlite3PagerMovepage(tls *libc.TLS, pPager uintptr, pPg uintptr, pgno TPgno, isCommit int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var needSyncPgno, origPgno TPgno var pPgOld, p3, p4, p5 uintptr var rc, v1 int32 var v2 bool var _ /* pPgHdr at bp+0 */ uintptr _, _, _, _, _, _, _, _, _ = needSyncPgno, origPgno, pPgOld, rc, v1, v2, p3, p4, p5 /* The page being overwritten. */ needSyncPgno = uint32(0) /* The original page number */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* In order to be able to rollback, an in-memory database must journal ** the page we are moving from. */ _ = libc.Int32FromInt32(0) if (*TPager)(unsafe.Pointer(pPager)).FtempFile != 0 { rc = _sqlite3PagerWrite(tls, pPg) if rc != 0 { return rc } } /* If the page being moved is dirty and has not been saved by the latest ** savepoint, then save the current contents of the page into the ** sub-journal now. This is required to handle the following scenario: ** ** BEGIN; ** ** SAVEPOINT one; ** ** ROLLBACK TO one; ** ** If page X were not written to the sub-journal here, it would not ** be possible to restore its contents when the "ROLLBACK TO one" ** statement were is processed. ** ** subjournalPage() may need to allocate space to store pPg->pgno into ** one or more savepoint bitvecs. This is the reason this function ** may return SQLITE_NOMEM. */ if v2 = int32((*TDbPage)(unsafe.Pointer(pPg)).Fflags)&int32(PGHDR_DIRTY) != 0; v2 { v1 = _subjournalPageIfRequired(tls, pPg) rc = v1 } if v2 && SQLITE_OK != v1 { return rc } /* If the journal needs to be sync()ed before page pPg->pgno can ** be written to, store pPg->pgno in local variable needSyncPgno. ** ** If the isCommit flag is set, there is no need to remember that ** the journal needs to be sync()ed before database page pPg->pgno ** can be written to. The caller has already promised not to write to it. */ if int32((*TDbPage)(unsafe.Pointer(pPg)).Fflags)&int32(PGHDR_NEED_SYNC) != 0 && !(isCommit != 0) { needSyncPgno = (*TDbPage)(unsafe.Pointer(pPg)).Fpgno _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } /* If the cache contains a page with page-number pgno, remove it ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for ** page pgno before the 'move' operation, it needs to be retained ** for the page moved there. */ p3 = pPg + 28 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) & ^libc.Int32FromInt32(PGHDR_NEED_SYNC)) pPgOld = _sqlite3PagerLookup(tls, pPager, pgno) _ = libc.Int32FromInt32(0) if pPgOld != 0 { if (*TPgHdr)(unsafe.Pointer(pPgOld)).FnRef > int64(1) { _sqlite3PagerUnrefNotNull(tls, pPgOld) return _sqlite3CorruptError(tls, int32(63973)) } p4 = pPg + 28 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | int32((*TPgHdr)(unsafe.Pointer(pPgOld)).Fflags)&libc.Int32FromInt32(PGHDR_NEED_SYNC)) if (*TPager)(unsafe.Pointer(pPager)).FtempFile != 0 { /* Do not discard pages from an in-memory database since we might ** need to rollback later. Just move the page out of the way. */ _sqlite3PcacheMove(tls, pPgOld, (*TPager)(unsafe.Pointer(pPager)).FdbSize+uint32(1)) } else { _sqlite3PcacheDrop(tls, pPgOld) } } origPgno = (*TDbPage)(unsafe.Pointer(pPg)).Fpgno _sqlite3PcacheMove(tls, pPg, pgno) _sqlite3PcacheMakeDirty(tls, pPg) /* For an in-memory database, make sure the original page continues ** to exist, in case the transaction needs to roll back. Use pPgOld ** as the original page since it has already been allocated. */ if (*TPager)(unsafe.Pointer(pPager)).FtempFile != 0 && pPgOld != 0 { _sqlite3PcacheMove(tls, pPgOld, origPgno) _sqlite3PagerUnrefNotNull(tls, pPgOld) } if needSyncPgno != 0 { rc = _sqlite3PagerGet(tls, pPager, needSyncPgno, bp, 0) if rc != SQLITE_OK { if needSyncPgno <= (*TPager)(unsafe.Pointer(pPager)).FdbOrigSize { _ = libc.Int32FromInt32(0) _sqlite3BitvecClear(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal, needSyncPgno, (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace) } return rc } p5 = *(*uintptr)(unsafe.Pointer(bp)) + 28 *(*Tu16)(unsafe.Pointer(p5)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p5))) | libc.Int32FromInt32(PGHDR_NEED_SYNC)) _sqlite3PcacheMakeDirty(tls, *(*uintptr)(unsafe.Pointer(bp))) _sqlite3PagerUnrefNotNull(tls, *(*uintptr)(unsafe.Pointer(bp))) } return SQLITE_OK } // C documentation // // /* // ** The page handle passed as the first argument refers to a dirty page // ** with a page number other than iNew. This function changes the page's // ** page number to iNew and sets the value of the PgHdr.flags field to // ** the value passed as the third parameter. // */ func _sqlite3PagerRekey(tls *libc.TLS, pPg uintptr, iNew TPgno, flags Tu16) { _ = libc.Int32FromInt32(0) (*TDbPage)(unsafe.Pointer(pPg)).Fflags = flags _sqlite3PcacheMove(tls, pPg, iNew) } // C documentation // // /* // ** Return a pointer to the data for the specified page. // */ func _sqlite3PagerGetData(tls *libc.TLS, pPg uintptr) (r uintptr) { _ = libc.Int32FromInt32(0) return (*TDbPage)(unsafe.Pointer(pPg)).FpData } // C documentation // // /* // ** Return a pointer to the Pager.nExtra bytes of "extra" space // ** allocated along with the specified page. // */ func _sqlite3PagerGetExtra(tls *libc.TLS, pPg uintptr) (r uintptr) { return (*TDbPage)(unsafe.Pointer(pPg)).FpExtra } // C documentation // // /* // ** Get/set the locking-mode for this pager. Parameter eMode must be one // ** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or // ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then // ** the locking-mode is set to the value specified. // ** // ** The returned value is either PAGER_LOCKINGMODE_NORMAL or // ** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated) // ** locking-mode. // */ func _sqlite3PagerLockingMode(tls *libc.TLS, pPager uintptr, eMode int32) (r int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if eMode >= 0 && !((*TPager)(unsafe.Pointer(pPager)).FtempFile != 0) && !(_sqlite3WalHeapMemory(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal) != 0) { (*TPager)(unsafe.Pointer(pPager)).FexclusiveMode = uint8(eMode) } return int32((*TPager)(unsafe.Pointer(pPager)).FexclusiveMode) } // C documentation // // /* // ** Set the journal-mode for this pager. Parameter eMode must be one of: // ** // ** PAGER_JOURNALMODE_DELETE // ** PAGER_JOURNALMODE_TRUNCATE // ** PAGER_JOURNALMODE_PERSIST // ** PAGER_JOURNALMODE_OFF // ** PAGER_JOURNALMODE_MEMORY // ** PAGER_JOURNALMODE_WAL // ** // ** The journalmode is set to the value specified if the change is allowed. // ** The change may be disallowed for the following reasons: // ** // ** * An in-memory database can only have its journal_mode set to _OFF // ** or _MEMORY. // ** // ** * Temporary databases cannot have _WAL journalmode. // ** // ** The returned indicate the current (possibly updated) journal-mode. // */ func _sqlite3PagerSetJournalMode(tls *libc.TLS, pPager uintptr, eMode int32) (r int32) { var eOld Tu8 var rc, state int32 _, _, _ = eOld, rc, state eOld = (*TPager)(unsafe.Pointer(pPager)).FjournalMode /* Prior journalmode */ /* The eMode parameter is always valid */ _ = libc.Int32FromInt32(0) /* This routine is only called from the OP_JournalMode opcode, and ** the logic there will never allow a temporary file to be changed ** to WAL mode. */ _ = libc.Int32FromInt32(0) /* Do allow the journalmode of an in-memory database to be set to ** anything other than MEMORY or OFF */ if (*TPager)(unsafe.Pointer(pPager)).FmemDb != 0 { _ = libc.Int32FromInt32(0) if eMode != int32(PAGER_JOURNALMODE_MEMORY) && eMode != int32(PAGER_JOURNALMODE_OFF) { eMode = int32(eOld) } } if eMode != int32(eOld) { /* Change the journal mode. */ _ = libc.Int32FromInt32(0) (*TPager)(unsafe.Pointer(pPager)).FjournalMode = uint8(eMode) /* When transitioning from TRUNCATE or PERSIST to any other journal ** mode except WAL, unless the pager is in locking_mode=exclusive mode, ** delete the journal file. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if !((*TPager)(unsafe.Pointer(pPager)).FexclusiveMode != 0) && int32(eOld)&int32(5) == int32(1) && eMode&int32(1) == 0 { /* In this case we would like to delete the journal file. If it is ** not possible, then that is not a problem. Deleting the journal file ** here is an optimization only. ** ** Before deleting the journal file, obtain a RESERVED lock on the ** database file. This ensures that the journal file is not deleted ** while it is in use by some other client. */ _sqlite3OsClose(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd) if int32((*TPager)(unsafe.Pointer(pPager)).FeLock) >= int32(RESERVED_LOCK) { _sqlite3OsDelete(tls, (*TPager)(unsafe.Pointer(pPager)).FpVfs, (*TPager)(unsafe.Pointer(pPager)).FzJournal, 0) } else { rc = SQLITE_OK state = int32((*TPager)(unsafe.Pointer(pPager)).FeState) _ = libc.Int32FromInt32(0) if state == PAGER_OPEN { rc = _sqlite3PagerSharedLock(tls, pPager) } if int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_READER) { _ = libc.Int32FromInt32(0) rc = _pagerLockDb(tls, pPager, int32(RESERVED_LOCK)) } if rc == SQLITE_OK { _sqlite3OsDelete(tls, (*TPager)(unsafe.Pointer(pPager)).FpVfs, (*TPager)(unsafe.Pointer(pPager)).FzJournal, 0) } if rc == SQLITE_OK && state == int32(PAGER_READER) { _pagerUnlockDb(tls, pPager, int32(SHARED_LOCK)) } else { if state == PAGER_OPEN { _pager_unlock(tls, pPager) } } _ = libc.Int32FromInt32(0) } } else { if eMode == int32(PAGER_JOURNALMODE_OFF) || eMode == int32(PAGER_JOURNALMODE_MEMORY) { _sqlite3OsClose(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd) } } } /* Return the new journal mode */ return int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) } // C documentation // // /* // ** Return the current journal mode. // */ func _sqlite3PagerGetJournalMode(tls *libc.TLS, pPager uintptr) (r int32) { return int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) } // C documentation // // /* // ** Return TRUE if the pager is in a state where it is OK to change the // ** journalmode. Journalmode changes can only happen when the database // ** is unmodified. // */ func _sqlite3PagerOkToChangeJournalMode(tls *libc.TLS, pPager uintptr) (r int32) { _ = libc.Int32FromInt32(0) if int32((*TPager)(unsafe.Pointer(pPager)).FeState) >= int32(PAGER_WRITER_CACHEMOD) { return 0 } if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != uintptr(0) && (*TPager)(unsafe.Pointer(pPager)).FjournalOff > 0 { return 0 } return int32(1) } // C documentation // // /* // ** Get/set the size-limit used for persistent journal files. // ** // ** Setting the size limit to -1 means no limit is enforced. // ** An attempt to set a limit smaller than -1 is a no-op. // */ func _sqlite3PagerJournalSizeLimit(tls *libc.TLS, pPager uintptr, iLimit Ti64) (r Ti64) { if iLimit >= int64(-int32(1)) { (*TPager)(unsafe.Pointer(pPager)).FjournalSizeLimit = iLimit _sqlite3WalLimit(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, iLimit) } return (*TPager)(unsafe.Pointer(pPager)).FjournalSizeLimit } // C documentation // // /* // ** Return a pointer to the pPager->pBackup variable. The backup module // ** in backup.c maintains the content of this variable. This module // ** uses it opaquely as an argument to sqlite3BackupRestart() and // ** sqlite3BackupUpdate() only. // */ func _sqlite3PagerBackupPtr(tls *libc.TLS, pPager uintptr) (r uintptr) { return pPager + 96 } // C documentation // // /* // ** Unless this is an in-memory or temporary database, clear the pager cache. // */ func _sqlite3PagerClearCache(tls *libc.TLS, pPager uintptr) { _ = libc.Int32FromInt32(0) if int32((*TPager)(unsafe.Pointer(pPager)).FtempFile) == 0 { _pager_reset(tls, pPager) } } // C documentation // // /* // ** This function is called when the user invokes "PRAGMA wal_checkpoint", // ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint() // ** or wal_blocking_checkpoint() API functions. // ** // ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. // */ func _sqlite3PagerCheckpoint(tls *libc.TLS, pPager uintptr, db uintptr, eMode int32, pnLog uintptr, pnCkpt uintptr) (r int32) { var rc int32 var v1 uintptr _, _ = rc, v1 rc = SQLITE_OK if (*TPager)(unsafe.Pointer(pPager)).FpWal == uintptr(0) && int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) == int32(PAGER_JOURNALMODE_WAL) { /* This only happens when a database file is zero bytes in size opened and ** then "PRAGMA journal_mode=WAL" is run and then sqlite3_wal_checkpoint() ** is invoked without any intervening transactions. We need to start ** a transaction to initialize pWal. The PRAGMA table_list statement is ** used for this since it starts transactions on every database file, ** including all ATTACHed databases. This seems expensive for a single ** sqlite3_wal_checkpoint() call, but it happens very rarely. ** https://sqlite.org/forum/forumpost/fd0f19d229156939 */ Xsqlite3_exec(tls, db, __ccgo_ts+4109, uintptr(0), uintptr(0), uintptr(0)) } if (*TPager)(unsafe.Pointer(pPager)).FpWal != 0 { if eMode == SQLITE_CHECKPOINT_PASSIVE { v1 = uintptr(0) } else { v1 = (*TPager)(unsafe.Pointer(pPager)).FxBusyHandler } rc = _sqlite3WalCheckpoint(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, db, eMode, v1, (*TPager)(unsafe.Pointer(pPager)).FpBusyHandlerArg, int32((*TPager)(unsafe.Pointer(pPager)).FwalSyncFlags), int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace, pnLog, pnCkpt) } return rc } func _sqlite3PagerWalCallback(tls *libc.TLS, pPager uintptr) (r int32) { return _sqlite3WalCallback(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal) } // C documentation // // /* // ** Return true if the underlying VFS for the given pager supports the // ** primitives necessary for write-ahead logging. // */ func _sqlite3PagerWalSupported(tls *libc.TLS, pPager uintptr) (r int32) { var pMethods uintptr _ = pMethods pMethods = (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods if (*TPager)(unsafe.Pointer(pPager)).FnoLock != 0 { return 0 } return libc.BoolInt32((*TPager)(unsafe.Pointer(pPager)).FexclusiveMode != 0 || (*Tsqlite3_io_methods)(unsafe.Pointer(pMethods)).FiVersion >= int32(2) && (*Tsqlite3_io_methods)(unsafe.Pointer(pMethods)).FxShmMap != 0) } // C documentation // // /* // ** Attempt to take an exclusive lock on the database file. If a PENDING lock // ** is obtained instead, immediately release it. // */ func _pagerExclusiveLock(tls *libc.TLS, pPager uintptr) (r int32) { var eOrigLock Tu8 var rc int32 _, _ = eOrigLock, rc /* Original lock */ _ = libc.Int32FromInt32(0) eOrigLock = (*TPager)(unsafe.Pointer(pPager)).FeLock rc = _pagerLockDb(tls, pPager, int32(EXCLUSIVE_LOCK)) if rc != SQLITE_OK { /* If the attempt to grab the exclusive lock failed, release the ** pending lock that may have been obtained instead. */ _pagerUnlockDb(tls, pPager, int32(eOrigLock)) } return rc } // C documentation // // /* // ** Call sqlite3WalOpen() to open the WAL handle. If the pager is in // ** exclusive-locking mode when this function is called, take an EXCLUSIVE // ** lock on the database file and use heap-memory to store the wal-index // ** in. Otherwise, use the normal shared-memory. // */ func _pagerOpenWal(tls *libc.TLS, pPager uintptr) (r int32) { var rc int32 _ = rc rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If the pager is already in exclusive-mode, the WAL module will use ** heap-memory for the wal-index instead of the VFS shared-memory ** implementation. Take the exclusive lock now, before opening the WAL ** file, to make sure this is safe. */ if (*TPager)(unsafe.Pointer(pPager)).FexclusiveMode != 0 { rc = _pagerExclusiveLock(tls, pPager) } /* Open the connection to the log file. If this operation fails, ** (e.g. due to malloc() failure), return an error code. */ if rc == SQLITE_OK { rc = _sqlite3WalOpen(tls, (*TPager)(unsafe.Pointer(pPager)).FpVfs, (*TPager)(unsafe.Pointer(pPager)).Ffd, (*TPager)(unsafe.Pointer(pPager)).FzWal, int32((*TPager)(unsafe.Pointer(pPager)).FexclusiveMode), (*TPager)(unsafe.Pointer(pPager)).FjournalSizeLimit, pPager+232) } _pagerFixMaplimit(tls, pPager) return rc } // C documentation // // /* // ** The caller must be holding a SHARED lock on the database file to call // ** this function. // ** // ** If the pager passed as the first argument is open on a real database // ** file (not a temp file or an in-memory database), and the WAL file // ** is not already open, make an attempt to open it now. If successful, // ** return SQLITE_OK. If an error occurs or the VFS used by the pager does // ** not support the xShmXXX() methods, return an error code. *pbOpen is // ** not modified in either case. // ** // ** If the pager is open on a temp-file (or in-memory database), or if // ** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK // ** without doing anything. // */ func _sqlite3PagerOpenWal(tls *libc.TLS, pPager uintptr, pbOpen uintptr) (r int32) { var rc int32 _ = rc rc = SQLITE_OK /* Return code */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if !((*TPager)(unsafe.Pointer(pPager)).FtempFile != 0) && !((*TPager)(unsafe.Pointer(pPager)).FpWal != 0) { if !(_sqlite3PagerWalSupported(tls, pPager) != 0) { return int32(SQLITE_CANTOPEN) } /* Close any rollback journal previously open */ _sqlite3OsClose(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd) rc = _pagerOpenWal(tls, pPager) if rc == SQLITE_OK { (*TPager)(unsafe.Pointer(pPager)).FjournalMode = uint8(PAGER_JOURNALMODE_WAL) (*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_OPEN) } } else { *(*int32)(unsafe.Pointer(pbOpen)) = int32(1) } return rc } // C documentation // // /* // ** This function is called to close the connection to the log file prior // ** to switching from WAL to rollback mode. // ** // ** Before closing the log file, this function attempts to take an // ** EXCLUSIVE lock on the database file. If this cannot be obtained, an // ** error (SQLITE_BUSY) is returned and the log connection is not closed. // ** If successful, the EXCLUSIVE lock is not released before returning. // */ func _sqlite3PagerCloseWal(tls *libc.TLS, pPager uintptr, db uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* logexists at bp+0 */ int32 _ = rc rc = SQLITE_OK _ = libc.Int32FromInt32(0) /* If the log file is not already open, but does exist in the file-system, ** it may need to be checkpointed before the connection can switch to ** rollback mode. Open it now so this can happen. */ if !((*TPager)(unsafe.Pointer(pPager)).FpWal != 0) { *(*int32)(unsafe.Pointer(bp)) = 0 rc = _pagerLockDb(tls, pPager, int32(SHARED_LOCK)) if rc == SQLITE_OK { rc = _sqlite3OsAccess(tls, (*TPager)(unsafe.Pointer(pPager)).FpVfs, (*TPager)(unsafe.Pointer(pPager)).FzWal, SQLITE_ACCESS_EXISTS, bp) } if rc == SQLITE_OK && *(*int32)(unsafe.Pointer(bp)) != 0 { rc = _pagerOpenWal(tls, pPager) } } /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on ** the database file, the log and log-summary files will be deleted. */ if rc == SQLITE_OK && (*TPager)(unsafe.Pointer(pPager)).FpWal != 0 { rc = _pagerExclusiveLock(tls, pPager) if rc == SQLITE_OK { rc = _sqlite3WalClose(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, db, int32((*TPager)(unsafe.Pointer(pPager)).FwalSyncFlags), int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace) (*TPager)(unsafe.Pointer(pPager)).FpWal = uintptr(0) _pagerFixMaplimit(tls, pPager) if rc != 0 && !((*TPager)(unsafe.Pointer(pPager)).FexclusiveMode != 0) { _pagerUnlockDb(tls, pPager, int32(SHARED_LOCK)) } } } return rc } // C documentation // // /* // ** If this is a WAL database, obtain a snapshot handle for the snapshot // ** currently open. Otherwise, return an error. // */ func _sqlite3PagerSnapshotGet(tls *libc.TLS, pPager uintptr, ppSnapshot uintptr) (r int32) { var rc int32 _ = rc rc = int32(SQLITE_ERROR) if (*TPager)(unsafe.Pointer(pPager)).FpWal != 0 { rc = _sqlite3WalSnapshotGet(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, ppSnapshot) } return rc } // C documentation // // /* // ** If this is a WAL database, store a pointer to pSnapshot. Next time a // ** read transaction is opened, attempt to read from the snapshot it // ** identifies. If this is not a WAL database, return an error. // */ func _sqlite3PagerSnapshotOpen(tls *libc.TLS, pPager uintptr, pSnapshot uintptr) (r int32) { var rc int32 _ = rc rc = SQLITE_OK if (*TPager)(unsafe.Pointer(pPager)).FpWal != 0 { _sqlite3WalSnapshotOpen(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, pSnapshot) } else { rc = int32(SQLITE_ERROR) } return rc } // C documentation // // /* // ** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this // ** is not a WAL database, return an error. // */ func _sqlite3PagerSnapshotRecover(tls *libc.TLS, pPager uintptr) (r int32) { var rc int32 _ = rc if (*TPager)(unsafe.Pointer(pPager)).FpWal != 0 { rc = _sqlite3WalSnapshotRecover(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal) } else { rc = int32(SQLITE_ERROR) } return rc } // C documentation // // /* // ** The caller currently has a read transaction open on the database. // ** If this is not a WAL database, SQLITE_ERROR is returned. Otherwise, // ** this function takes a SHARED lock on the CHECKPOINTER slot and then // ** checks if the snapshot passed as the second argument is still // ** available. If so, SQLITE_OK is returned. // ** // ** If the snapshot is not available, SQLITE_ERROR is returned. Or, if // ** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error // ** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER // ** lock is released before returning. // */ func _sqlite3PagerSnapshotCheck(tls *libc.TLS, pPager uintptr, pSnapshot uintptr) (r int32) { var rc int32 _ = rc if (*TPager)(unsafe.Pointer(pPager)).FpWal != 0 { rc = _sqlite3WalSnapshotCheck(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, pSnapshot) } else { rc = int32(SQLITE_ERROR) } return rc } // C documentation // // /* // ** Release a lock obtained by an earlier successful call to // ** sqlite3PagerSnapshotCheck(). // */ func _sqlite3PagerSnapshotUnlock(tls *libc.TLS, pPager uintptr) { _ = libc.Int32FromInt32(0) _sqlite3WalSnapshotUnlock(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal) } /************** End of pager.c ***********************************************/ /************** Begin file wal.c *********************************************/ /* ** 2010 February 1 ** ** 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 contains the implementation of a write-ahead log (WAL) used in ** "journal_mode=WAL" mode. ** ** WRITE-AHEAD LOG (WAL) FILE FORMAT ** ** A WAL file consists of a header followed by zero or more "frames". ** Each frame records the revised content of a single page from the ** database file. All changes to the database are recorded by writing ** frames into the WAL. Transactions commit when a frame is written that ** contains a commit marker. A single WAL can and usually does record ** multiple transactions. Periodically, the content of the WAL is ** transferred back into the database file in an operation called a ** "checkpoint". ** ** A single WAL file can be used multiple times. In other words, the ** WAL can fill up with frames and then be checkpointed and then new ** frames can overwrite the old ones. A WAL always grows from beginning ** toward the end. Checksums and counters attached to each frame are ** used to determine which frames within the WAL are valid and which ** are leftovers from prior checkpoints. ** ** The WAL header is 32 bytes in size and consists of the following eight ** big-endian 32-bit unsigned integer values: ** ** 0: Magic number. 0x377f0682 or 0x377f0683 ** 4: File format version. Currently 3007000 ** 8: Database page size. Example: 1024 ** 12: Checkpoint sequence number ** 16: Salt-1, random integer incremented with each checkpoint ** 20: Salt-2, a different random integer changing with each ckpt ** 24: Checksum-1 (first part of checksum for first 24 bytes of header). ** 28: Checksum-2 (second part of checksum for first 24 bytes of header). ** ** Immediately following the wal-header are zero or more frames. Each ** frame consists of a 24-byte frame-header followed by a bytes ** of page data. The frame-header is six big-endian 32-bit unsigned ** integer values, as follows: ** ** 0: Page number. ** 4: For commit records, the size of the database image in pages ** after the commit. For all other records, zero. ** 8: Salt-1 (copied from the header) ** 12: Salt-2 (copied from the header) ** 16: Checksum-1. ** 20: Checksum-2. ** ** A frame is considered valid if and only if the following conditions are ** true: ** ** (1) The salt-1 and salt-2 values in the frame-header match ** salt values in the wal-header ** ** (2) The checksum values in the final 8 bytes of the frame-header ** exactly match the checksum computed consecutively on the ** WAL header and the first 8 bytes and the content of all frames ** up to and including the current frame. ** ** The checksum is computed using 32-bit big-endian integers if the ** magic number in the first 4 bytes of the WAL is 0x377f0683 and it ** is computed using little-endian if the magic number is 0x377f0682. ** The checksum values are always stored in the frame header in a ** big-endian format regardless of which byte order is used to compute ** the checksum. The checksum is computed by interpreting the input as ** an even number of unsigned 32-bit integers: x[0] through x[N]. The ** algorithm used for the checksum is as follows: ** ** for i from 0 to n-1 step 2: ** s0 += x[i] + s1; ** s1 += x[i+1] + s0; ** endfor ** ** Note that s0 and s1 are both weighted checksums using fibonacci weights ** in reverse order (the largest fibonacci weight occurs on the first element ** of the sequence being summed.) The s1 value spans all 32-bit ** terms of the sequence whereas s0 omits the final term. ** ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the ** WAL is transferred into the database, then the database is VFS.xSync-ed. ** The VFS.xSync operations serve as write barriers - all writes launched ** before the xSync must complete before any write that launches after the ** xSync begins. ** ** After each checkpoint, the salt-1 value is incremented and the salt-2 ** value is randomized. This prevents old and new frames in the WAL from ** being considered valid at the same time and being checkpointing together ** following a crash. ** ** READER ALGORITHM ** ** To read a page from the database (call it page number P), a reader ** first checks the WAL to see if it contains page P. If so, then the ** last valid instance of page P that is a followed by a commit frame ** or is a commit frame itself becomes the value read. If the WAL ** contains no copies of page P that are valid and which are a commit ** frame or are followed by a commit frame, then page P is read from ** the database file. ** ** To start a read transaction, the reader records the index of the last ** valid frame in the WAL. The reader uses this recorded "mxFrame" value ** for all subsequent read operations. New transactions can be appended ** to the WAL, but as long as the reader uses its original mxFrame value ** and ignores the newly appended content, it will see a consistent snapshot ** of the database from a single point in time. This technique allows ** multiple concurrent readers to view different versions of the database ** content simultaneously. ** ** The reader algorithm in the previous paragraphs works correctly, but ** because frames for page P can appear anywhere within the WAL, the ** reader has to scan the entire WAL looking for page P frames. If the ** WAL is large (multiple megabytes is typical) that scan can be slow, ** and read performance suffers. To overcome this problem, a separate ** data structure called the wal-index is maintained to expedite the ** search for frames of a particular page. ** ** WAL-INDEX FORMAT ** ** Conceptually, the wal-index is shared memory, though VFS implementations ** might choose to implement the wal-index using a mmapped file. Because ** the wal-index is shared memory, SQLite does not support journal_mode=WAL ** on a network filesystem. All users of the database must be able to ** share memory. ** ** In the default unix and windows implementation, the wal-index is a mmapped ** file whose name is the database name with a "-shm" suffix added. For that ** reason, the wal-index is sometimes called the "shm" file. ** ** The wal-index is transient. After a crash, the wal-index can (and should ** be) reconstructed from the original WAL file. In fact, the VFS is required ** to either truncate or zero the header of the wal-index when the last ** connection to it closes. Because the wal-index is transient, it can ** use an architecture-specific format; it does not have to be cross-platform. ** Hence, unlike the database and WAL file formats which store all values ** as big endian, the wal-index can store multi-byte values in the native ** byte order of the host computer. ** ** The purpose of the wal-index is to answer this question quickly: Given ** a page number P and a maximum frame index M, return the index of the ** last frame in the wal before frame M for page P in the WAL, or return ** NULL if there are no frames for page P in the WAL prior to M. ** ** The wal-index consists of a header region, followed by an one or ** more index blocks. ** ** The wal-index header contains the total number of frames within the WAL ** in the mxFrame field. ** ** Each index block except for the first contains information on ** HASHTABLE_NPAGE frames. The first index block contains information on ** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and ** HASHTABLE_NPAGE are selected so that together the wal-index header and ** first index block are the same size as all other index blocks in the ** wal-index. The values are: ** ** HASHTABLE_NPAGE 4096 ** HASHTABLE_NPAGE_ONE 4062 ** ** Each index block contains two sections, a page-mapping that contains the ** database page number associated with each wal frame, and a hash-table ** that allows readers to query an index block for a specific page number. ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE ** for the first index block) 32-bit page numbers. The first entry in the ** first index-block contains the database page number corresponding to the ** first frame in the WAL file. The first entry in the second index block ** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in ** the log, and so on. ** ** The last index block in a wal-index usually contains less than the full ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers, ** depending on the contents of the WAL file. This does not change the ** allocated size of the page-mapping array - the page-mapping array merely ** contains unused entries. ** ** Even without using the hash table, the last frame for page P ** can be found by scanning the page-mapping sections of each index block ** starting with the last index block and moving toward the first, and ** within each index block, starting at the end and moving toward the ** beginning. The first entry that equals P corresponds to the frame ** holding the content for that page. ** ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers. ** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the ** hash table for each page number in the mapping section, so the hash ** table is never more than half full. The expected number of collisions ** prior to finding a match is 1. Each entry of the hash table is an ** 1-based index of an entry in the mapping section of the same ** index block. Let K be the 1-based index of the largest entry in ** the mapping section. (For index blocks other than the last, K will ** always be exactly HASHTABLE_NPAGE (4096) and for the last index block ** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table ** contain a value of 0. ** ** To look for page P in the hash table, first compute a hash iKey on ** P as follows: ** ** iKey = (P * 383) % HASHTABLE_NSLOT ** ** Then start scanning entries of the hash table, starting with iKey ** (wrapping around to the beginning when the end of the hash table is ** reached) until an unused hash slot is found. Let the first unused slot ** be at index iUnused. (iUnused might be less than iKey if there was ** wrap-around.) Because the hash table is never more than half full, ** the search is guaranteed to eventually hit an unused entry. Let ** iMax be the value between iKey and iUnused, closest to iUnused, ** where aHash[iMax]==P. If there is no iMax entry (if there exists ** no hash slot such that aHash[i]==p) then page P is not in the ** current index block. Otherwise the iMax-th mapping entry of the ** current index block corresponds to the last entry that references ** page P. ** ** A hash search begins with the last index block and moves toward the ** first index block, looking for entries corresponding to page P. On ** average, only two or three slots in each index block need to be ** examined in order to either find the last entry for page P, or to ** establish that no such entry exists in the block. Each index block ** holds over 4000 entries. So two or three index blocks are sufficient ** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10 ** comparisons (on average) suffice to either locate a frame in the ** WAL or to establish that the frame does not exist in the WAL. This ** is much faster than scanning the entire 10MB WAL. ** ** Note that entries are added in order of increasing K. Hence, one ** reader might be using some value K0 and a second reader that started ** at a later time (after additional transactions were added to the WAL ** and to the wal-index) might be using a different value K1, where K1>K0. ** Both readers can use the same hash table and mapping section to get ** the correct result. There may be entries in the hash table with ** K>K0 but to the first reader, those entries will appear to be unused ** slots in the hash table and so the first reader will get an answer as ** if no values greater than K0 had ever been inserted into the hash table ** in the first place - which is what reader one wants. Meanwhile, the ** second reader using K1 will see additional values that were inserted ** later, which is exactly what reader two wants. ** ** When a rollback occurs, the value of K is decreased. Hash table entries ** that correspond to frames greater than the new K value are removed ** from the hash table at this point. */ /* #include "wal.h" */ /* ** Trace output macros */ /* ** The maximum (and only) versions of the wal and wal-index formats ** that may be interpreted by this version of SQLite. ** ** If a client begins recovering a WAL file and finds that (a) the checksum ** values in the wal-header are correct and (b) the version field is not ** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN. ** ** Similarly, if a client successfully reads a wal-index header (i.e. the ** checksum test is successful) and finds that the version field is not ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite ** returns SQLITE_CANTOPEN. */ /* ** Index numbers for various locking bytes. WAL_NREADER is the number ** of available reader locks and should be at least 3. The default ** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5. ** ** Technically, the various VFSes are free to implement these locks however ** they see fit. However, compatibility is encouraged so that VFSes can ** interoperate. The standard implementation used on both unix and windows ** is for the index number to indicate a byte offset into the ** WalCkptInfo.aLock[] array in the wal-index header. In other words, all ** locks are on the shm file. The WALINDEX_LOCK_OFFSET constant (which ** should be 120) is the location in the shm file for the first locking ** byte. */ // C documentation // // /* Object declarations */ type TWalIndexHdr = struct { FiVersion Tu32 Funused Tu32 FiChange Tu32 FisInit Tu8 FbigEndCksum Tu8 FszPage Tu16 FmxFrame Tu32 FnPage Tu32 FaFrameCksum [2]Tu32 FaSalt [2]Tu32 FaCksum [2]Tu32 } type WalIndexHdr = TWalIndexHdr type TWalIterator = struct { FiPrior Tu32 FnSegment int32 FaSegment [1]TWalSegment } type WalIterator = TWalIterator type TWalCkptInfo = struct { FnBackfill Tu32 FaReadMark [5]Tu32 FaLock [8]Tu8 FnBackfillAttempted Tu32 FnotUsed0 Tu32 } type WalCkptInfo = TWalCkptInfo /* ** The following object holds a copy of the wal-index header content. ** ** The actual header in the wal-index consists of two copies of this ** object followed by one instance of the WalCkptInfo object. ** For all versions of SQLite through 3.10.0 and probably beyond, ** the locking bytes (WalCkptInfo.aLock) start at offset 120 and ** the total header size is 136 bytes. ** ** The szPage value can be any power of 2 between 512 and 32768, inclusive. ** Or it can be 1 to represent a 65536-byte page. The latter case was ** added in 3.7.1 when support for 64K pages was added. */ type TWalIndexHdr1 = struct { FiVersion Tu32 Funused Tu32 FiChange Tu32 FisInit Tu8 FbigEndCksum Tu8 FszPage Tu16 FmxFrame Tu32 FnPage Tu32 FaFrameCksum [2]Tu32 FaSalt [2]Tu32 FaCksum [2]Tu32 } type WalIndexHdr1 = TWalIndexHdr1 /* ** A copy of the following object occurs in the wal-index immediately ** following the second copy of the WalIndexHdr. This object stores ** information used by checkpoint. ** ** nBackfill is the number of frames in the WAL that have been written ** back into the database. (We call the act of moving content from WAL to ** database "backfilling".) The nBackfill number is never greater than ** WalIndexHdr.mxFrame. nBackfill can only be increased by threads ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread). ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from ** mxFrame back to zero when the WAL is reset. ** ** nBackfillAttempted is the largest value of nBackfill that a checkpoint ** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however ** the nBackfillAttempted is set before any backfilling is done and the ** nBackfill is only set after all backfilling completes. So if a checkpoint ** crashes, nBackfillAttempted might be larger than nBackfill. The ** WalIndexHdr.mxFrame must never be less than nBackfillAttempted. ** ** The aLock[] field is a set of bytes used for locking. These bytes should ** never be read or written. ** ** There is one entry in aReadMark[] for each reader lock. If a reader ** holds read-lock K, then the value in aReadMark[K] is no greater than ** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff) ** for any aReadMark[] means that entry is unused. aReadMark[0] is ** a special case; its value is never used and it exists as a place-holder ** to avoid having to offset aReadMark[] indexes by one. Readers holding ** WAL_READ_LOCK(0) always ignore the entire WAL and read all content ** directly from the database. ** ** The value of aReadMark[K] may only be changed by a thread that ** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of ** aReadMark[K] cannot changed while there is a reader is using that mark ** since the reader will be holding a shared lock on WAL_READ_LOCK(K). ** ** The checkpointer may only transfer frames from WAL to database where ** the frame numbers are less than or equal to every aReadMark[] that is ** in use (that is, every aReadMark[j] for which there is a corresponding ** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the ** largest value and will increase an unused aReadMark[] to mxFrame if there ** is not already an aReadMark[] equal to mxFrame. The exception to the ** previous sentence is when nBackfill equals mxFrame (meaning that everything ** in the WAL has been backfilled into the database) then new readers ** will choose aReadMark[0] which has value 0 and hence such reader will ** get all their all content directly from the database file and ignore ** the WAL. ** ** Writers normally append new frames to the end of the WAL. However, ** if nBackfill equals mxFrame (meaning that all WAL content has been ** written back into the database) and if no readers are using the WAL ** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then ** the writer will first "reset" the WAL back to the beginning and start ** writing new content beginning at frame 1. ** ** We assume that 32-bit loads are atomic and so no locks are needed in ** order to read from any aReadMark[] entries. */ type TWalCkptInfo1 = struct { FnBackfill Tu32 FaReadMark [5]Tu32 FaLock [8]Tu8 FnBackfillAttempted Tu32 FnotUsed0 Tu32 } type WalCkptInfo1 = TWalCkptInfo1 /* ** This is a schematic view of the complete 136-byte header of the ** wal-index file (also known as the -shm file): ** ** +-----------------------------+ ** 0: | iVersion | ** +-----------------------------+ | ** 4: | (unused padding) | | ** +-----------------------------+ | ** 8: | iChange | | ** +-------+-------+-------------+ | ** 12: | bInit | bBig | szPage | | ** +-------+-------+-------------+ | ** 16: | mxFrame | | First copy of the ** +-----------------------------+ | WalIndexHdr object ** 20: | nPage | | ** +-----------------------------+ | ** 24: | aFrameCksum | | ** | | | ** +-----------------------------+ | ** 32: | aSalt | | ** | | | ** +-----------------------------+ | ** 40: | aCksum | | ** | | / ** +-----------------------------+ ** 48: | iVersion | ** +-----------------------------+ | ** 52: | (unused padding) | | ** +-----------------------------+ | ** 56: | iChange | | ** +-------+-------+-------------+ | ** 60: | bInit | bBig | szPage | | ** +-------+-------+-------------+ | Second copy of the ** 64: | mxFrame | | WalIndexHdr ** +-----------------------------+ | ** 68: | nPage | | ** +-----------------------------+ | ** 72: | aFrameCksum | | ** | | | ** +-----------------------------+ | ** 80: | aSalt | | ** | | | ** +-----------------------------+ | ** 88: | aCksum | | ** | | / ** +-----------------------------+ ** 96: | nBackfill | ** +-----------------------------+ ** 100: | 5 read marks | ** | | ** | | ** | | ** | | ** +-------+-------+------+------+ ** 120: | Write | Ckpt | Rcvr | Rd0 | ** +-------+-------+------+------+ ) 8 lock bytes ** | Read1 | Read2 | Rd3 | Rd4 | / ** +-------+-------+------+------+ ** 128: | nBackfillAttempted | ** +-----------------------------+ ** 132: | (unused padding) | ** +-----------------------------+ */ /* A block of WALINDEX_LOCK_RESERVED bytes beginning at ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems ** only support mandatory file-locks, we do not read or write data ** from the region of the file on which locks are applied. */ /* Size of header before each frame in wal */ /* Size of write ahead log header, including checksum. */ /* WAL magic value. Either this value, or the same value with the least ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit ** big-endian format in the first 4 bytes of a WAL file. ** ** If the LSB is set, then the checksums for each frame within the WAL ** file are calculated by treating all data as an array of 32-bit ** big-endian words. Otherwise, they are calculated by interpreting ** all data as 32-bit little-endian words. */ /* ** Return the offset of frame iFrame in the write-ahead log file, ** assuming a database page size of szPage bytes. The offset returned ** is to the start of the write-ahead log frame-header. */ /* ** An open write-ahead log file is represented by an instance of the ** following object. */ type TWal1 = struct { F__ccgo_align [0]uint32 FpVfs uintptr FpDbFd uintptr FpWalFd uintptr FiCallback Tu32 FmxWalSize Ti64 FnWiData int32 FszFirstBlock int32 FapWiData uintptr FszPage Tu32 FreadLock Ti16 FsyncFlags Tu8 FexclusiveMode Tu8 FwriteLock Tu8 FckptLock Tu8 FreadOnly Tu8 FtruncateOnCommit Tu8 FsyncHeader Tu8 FpadToSectorBoundary Tu8 FbShmUnreliable Tu8 Fhdr TWalIndexHdr FminFrame Tu32 FiReCksum Tu32 FzWalName uintptr FnCkpt Tu32 FpSnapshot uintptr } type Wal1 = TWal1 /* ** Candidate values for Wal.exclusiveMode. */ /* ** Possible values for WAL.readOnly */ // C documentation // // /* // ** Each page of the wal-index mapping contains a hash-table made up of // ** an array of HASHTABLE_NSLOT elements of the following type. // */ type Tht_slot = uint16 type ht_slot = Tht_slot /* ** This structure is used to implement an iterator that loops through ** all frames in the WAL in database page order. Where two or more frames ** correspond to the same database page, the iterator visits only the ** frame most recently written to the WAL (in other words, the frame with ** the largest index). ** ** The internals of this structure are only accessed by: ** ** walIteratorInit() - Create a new iterator, ** walIteratorNext() - Step an iterator, ** walIteratorFree() - Free an iterator. ** ** This functionality is used by the checkpoint code (see walCheckpoint()). */ type TWalIterator1 = struct { FiPrior Tu32 FnSegment int32 FaSegment [1]TWalSegment } type WalIterator1 = TWalIterator1 /* ** Define the parameters of the hash tables in the wal-index file. There ** is a hash-table following every HASHTABLE_NPAGE page numbers in the ** wal-index. ** ** Changing any of these constants will alter the wal-index format and ** create incompatibilities. */ /* ** The block of page numbers associated with the first hash-table in a ** wal-index is smaller than usual. This is so that there is a complete ** hash-table on each aligned 32KB page of the wal-index. */ /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */ /* ** Structured Exception Handling (SEH) is a Windows-specific technique ** for catching exceptions raised while accessing memory-mapped files. ** ** The -DSQLITE_USE_SEH compile-time option means to use SEH to catch and ** deal with system-level errors that arise during WAL -shm file processing. ** Without this compile-time option, any system-level faults that appear ** while accessing the memory-mapped -shm file will cause a process-wide ** signal to be deliver, which will more than likely cause the entire ** process to exit. */ // C documentation // // /* // ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index // ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are // ** numbered from zero. // ** // ** If the wal-index is currently smaller the iPage pages then the size // ** of the wal-index might be increased, but only if it is safe to do // ** so. It is safe to enlarge the wal-index if pWal->writeLock is true // ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE. // ** // ** Three possible result scenarios: // ** // ** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page // ** (2) rc>=SQLITE_ERROR and *ppPage==NULL // ** (3) rc==SQLITE_OK and *ppPage==NULL // only if iPage==0 // ** // ** Scenario (3) can only occur when pWal->writeLock is false and iPage==0 // */ func _walIndexPageRealloc(tls *libc.TLS, pWal uintptr, iPage int32, ppPage uintptr) (r int32) { var apNew, p1 uintptr var nByte Tsqlite3_int64 var rc int32 _, _, _, _ = apNew, nByte, rc, p1 rc = SQLITE_OK /* Enlarge the pWal->apWiData[] array if required */ if (*TWal)(unsafe.Pointer(pWal)).FnWiData <= iPage { nByte = int64(uint32(4) * uint32(iPage+libc.Int32FromInt32(1))) apNew = _sqlite3Realloc(tls, (*TWal)(unsafe.Pointer(pWal)).FapWiData, uint64(nByte)) if !(apNew != 0) { *(*uintptr)(unsafe.Pointer(ppPage)) = uintptr(0) return int32(SQLITE_NOMEM) } libc.Xmemset(tls, apNew+uintptr((*TWal)(unsafe.Pointer(pWal)).FnWiData)*4, 0, uint32(4)*uint32(iPage+libc.Int32FromInt32(1)-(*TWal)(unsafe.Pointer(pWal)).FnWiData)) (*TWal)(unsafe.Pointer(pWal)).FapWiData = apNew (*TWal)(unsafe.Pointer(pWal)).FnWiData = iPage + int32(1) } /* Request a pointer to the required page from the VFS */ _ = libc.Int32FromInt32(0) if int32((*TWal)(unsafe.Pointer(pWal)).FexclusiveMode) == int32(WAL_HEAPMEMORY_MODE) { *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPage)*4)) = _sqlite3MallocZero(tls, uint64(libc.Uint32FromInt64(2)*uint32(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint32FromInt32(HASHTABLE_NPAGE)*libc.Uint32FromInt64(4))) if !(*(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPage)*4)) != 0) { rc = int32(SQLITE_NOMEM) } } else { rc = _sqlite3OsShmMap(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, iPage, int32(libc.Uint32FromInt64(2)*uint32(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint32FromInt32(HASHTABLE_NPAGE)*libc.Uint32FromInt64(4)), int32((*TWal)(unsafe.Pointer(pWal)).FwriteLock), (*TWal)(unsafe.Pointer(pWal)).FapWiData+uintptr(iPage)*4) _ = libc.Int32FromInt32(0) if rc == SQLITE_OK { if iPage > 0 && _sqlite3FaultSim(tls, int32(600)) != 0 { rc = int32(SQLITE_NOMEM) } } else { if rc&int32(0xff) == int32(SQLITE_READONLY) { p1 = pWal + 46 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | libc.Int32FromInt32(WAL_SHM_RDONLY)) if rc == int32(SQLITE_READONLY) { rc = SQLITE_OK } } } } *(*uintptr)(unsafe.Pointer(ppPage)) = *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPage)*4)) _ = libc.Int32FromInt32(0) return rc } func _walIndexPage(tls *libc.TLS, pWal uintptr, iPage int32, ppPage uintptr) (r int32) { var v1 uintptr var v2 bool _, _ = v1, v2 _ = libc.Int32FromInt32(0) if v2 = (*TWal)(unsafe.Pointer(pWal)).FnWiData <= iPage; !v2 { v1 = *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPage)*4)) *(*uintptr)(unsafe.Pointer(ppPage)) = v1 } if v2 || v1 == uintptr(0) { return _walIndexPageRealloc(tls, pWal, iPage, ppPage) } return SQLITE_OK } // C documentation // // /* // ** Return a pointer to the WalCkptInfo structure in the wal-index. // */ func _walCkptInfo(tls *libc.TLS, pWal uintptr) (r uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData)) + uintptr(libc.Uint32FromInt64(48)/libc.Uint32FromInt32(2))*4 } // C documentation // // /* // ** Return a pointer to the WalIndexHdr structure in the wal-index. // */ func _walIndexHdr(tls *libc.TLS, pWal uintptr) (r uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData)) } /* ** The argument to this macro must be of type u32. On a little-endian ** architecture, it returns the u32 value that results from interpreting ** the 4 bytes as a big-endian value. On a big-endian architecture, it ** returns the value that would be produced by interpreting the 4 bytes ** of the input value as a little-endian integer. */ // C documentation // // /* // ** Generate or extend an 8 byte checksum based on the data in // ** array aByte[] and the initial values of aIn[0] and aIn[1] (or // ** initial values of 0 and 0 if aIn==NULL). // ** // ** The checksum is written back into aOut[] before returning. // ** // ** nByte must be a positive multiple of 8. // */ func _walChecksumBytes(tls *libc.TLS, nativeCksum int32, a uintptr, nByte int32, aIn uintptr, aOut uintptr) { var aData, aEnd, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v2, v3, v4, v5, v6, v7, v8, v9 uintptr var s1, s2, v1 Tu32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aData, aEnd, s1, s2, v1, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v2, v3, v4, v5, v6, v7, v8, v9 aData = a aEnd = a + uintptr(nByte) if aIn != 0 { s1 = *(*Tu32)(unsafe.Pointer(aIn)) s2 = *(*Tu32)(unsafe.Pointer(aIn + 1*4)) } else { v1 = libc.Uint32FromInt32(0) s2 = v1 s1 = v1 } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if !(nativeCksum != 0) { for cond := true; cond; cond = aData < aEnd { s1 += *(*Tu32)(unsafe.Pointer(aData))&uint32(0x000000FF)<>int32(8) + *(*Tu32)(unsafe.Pointer(aData))&uint32(0xFF000000)>>int32(24) + s2 s2 += *(*Tu32)(unsafe.Pointer(aData + 1*4))&uint32(0x000000FF)<>int32(8) + *(*Tu32)(unsafe.Pointer(aData + 1*4))&uint32(0xFF000000)>>int32(24) + s1 aData += uintptr(2) * 4 } } else { if nByte%int32(64) == 0 { for cond := true; cond; cond = aData < aEnd { v2 = aData aData += 4 s1 += *(*Tu32)(unsafe.Pointer(v2)) + s2 v3 = aData aData += 4 s2 += *(*Tu32)(unsafe.Pointer(v3)) + s1 v4 = aData aData += 4 s1 += *(*Tu32)(unsafe.Pointer(v4)) + s2 v5 = aData aData += 4 s2 += *(*Tu32)(unsafe.Pointer(v5)) + s1 v6 = aData aData += 4 s1 += *(*Tu32)(unsafe.Pointer(v6)) + s2 v7 = aData aData += 4 s2 += *(*Tu32)(unsafe.Pointer(v7)) + s1 v8 = aData aData += 4 s1 += *(*Tu32)(unsafe.Pointer(v8)) + s2 v9 = aData aData += 4 s2 += *(*Tu32)(unsafe.Pointer(v9)) + s1 v10 = aData aData += 4 s1 += *(*Tu32)(unsafe.Pointer(v10)) + s2 v11 = aData aData += 4 s2 += *(*Tu32)(unsafe.Pointer(v11)) + s1 v12 = aData aData += 4 s1 += *(*Tu32)(unsafe.Pointer(v12)) + s2 v13 = aData aData += 4 s2 += *(*Tu32)(unsafe.Pointer(v13)) + s1 v14 = aData aData += 4 s1 += *(*Tu32)(unsafe.Pointer(v14)) + s2 v15 = aData aData += 4 s2 += *(*Tu32)(unsafe.Pointer(v15)) + s1 v16 = aData aData += 4 s1 += *(*Tu32)(unsafe.Pointer(v16)) + s2 v17 = aData aData += 4 s2 += *(*Tu32)(unsafe.Pointer(v17)) + s1 } } else { for cond := true; cond; cond = aData < aEnd { v18 = aData aData += 4 s1 += *(*Tu32)(unsafe.Pointer(v18)) + s2 v19 = aData aData += 4 s2 += *(*Tu32)(unsafe.Pointer(v19)) + s1 } } } _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(aOut)) = s1 *(*Tu32)(unsafe.Pointer(aOut + 1*4)) = s2 } // C documentation // // /* // ** If there is the possibility of concurrent access to the SHM file // ** from multiple threads and/or processes, then do a memory barrier. // */ func _walShmBarrier(tls *libc.TLS, pWal uintptr) { if int32((*TWal)(unsafe.Pointer(pWal)).FexclusiveMode) != int32(WAL_HEAPMEMORY_MODE) { _sqlite3OsShmBarrier(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd) } } /* ** Add the SQLITE_NO_TSAN as part of the return-type of a function ** definition as a hint that the function contains constructs that ** might give false-positive TSAN warnings. ** ** See tag-20200519-1. */ // C documentation // // /* // ** Write the header information in pWal->hdr into the wal-index. // ** // ** The checksum on pWal->hdr is updated before it is written. // */ func _walIndexWriteHdr(tls *libc.TLS, pWal uintptr) { var aHdr uintptr var nCksum int32 _, _ = aHdr, nCksum aHdr = _walIndexHdr(tls, pWal) nCksum = int32(uint32(libc.UintptrFromInt32(0) + 40)) _ = libc.Int32FromInt32(0) (*TWal)(unsafe.Pointer(pWal)).Fhdr.FisInit = uint8(1) (*TWal)(unsafe.Pointer(pWal)).Fhdr.FiVersion = uint32(WALINDEX_MAX_VERSION) _walChecksumBytes(tls, int32(1), pWal+52, nCksum, uintptr(0), pWal+52+40) /* Possible TSAN false-positive. See tag-20200519-1 */ libc.Xmemcpy(tls, aHdr+1*48, pWal+52, uint32(48)) _walShmBarrier(tls, pWal) libc.Xmemcpy(tls, aHdr, pWal+52, uint32(48)) } // C documentation // // /* // ** This function encodes a single frame header and writes it to a buffer // ** supplied by the caller. A frame-header is made up of a series of // ** 4-byte big-endian integers, as follows: // ** // ** 0: Page number. // ** 4: For commit records, the size of the database image in pages // ** after the commit. For all other records, zero. // ** 8: Salt-1 (copied from the wal-header) // ** 12: Salt-2 (copied from the wal-header) // ** 16: Checksum-1. // ** 20: Checksum-2. // */ func _walEncodeFrame(tls *libc.TLS, pWal uintptr, iPage Tu32, nTruncate Tu32, aData uintptr, aFrame uintptr) { var aCksum uintptr var nativeCksum int32 _, _ = aCksum, nativeCksum /* True for native byte-order checksums */ aCksum = pWal + 52 + 24 _ = libc.Int32FromInt32(0) _sqlite3Put4byte(tls, aFrame, iPage) _sqlite3Put4byte(tls, aFrame+4, nTruncate) if (*TWal)(unsafe.Pointer(pWal)).FiReCksum == uint32(0) { libc.Xmemcpy(tls, aFrame+8, pWal+52+32, uint32(8)) nativeCksum = libc.BoolInt32(int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FbigEndCksum) == SQLITE_BIGENDIAN) _walChecksumBytes(tls, nativeCksum, aFrame, int32(8), aCksum, aCksum) _walChecksumBytes(tls, nativeCksum, aData, int32((*TWal)(unsafe.Pointer(pWal)).FszPage), aCksum, aCksum) _sqlite3Put4byte(tls, aFrame+16, *(*Tu32)(unsafe.Pointer(aCksum))) _sqlite3Put4byte(tls, aFrame+20, *(*Tu32)(unsafe.Pointer(aCksum + 1*4))) } else { libc.Xmemset(tls, aFrame+8, 0, uint32(16)) } } // C documentation // // /* // ** Check to see if the frame with header in aFrame[] and content // ** in aData[] is valid. If it is a valid frame, fill *piPage and // ** *pnTruncate and return true. Return if the frame is not valid. // */ func _walDecodeFrame(tls *libc.TLS, pWal uintptr, piPage uintptr, pnTruncate uintptr, aData uintptr, aFrame uintptr) (r int32) { var aCksum uintptr var nativeCksum int32 var pgno Tu32 _, _, _ = aCksum, nativeCksum, pgno /* True for native byte-order checksums */ aCksum = pWal + 52 + 24 /* Page number of the frame */ _ = libc.Int32FromInt32(0) /* A frame is only valid if the salt values in the frame-header ** match the salt values in the wal-header. */ if libc.Xmemcmp(tls, pWal+52+32, aFrame+8, uint32(8)) != 0 { return 0 } /* A frame is only valid if the page number is greater than zero. */ pgno = _sqlite3Get4byte(tls, aFrame) if pgno == uint32(0) { return 0 } /* A frame is only valid if a checksum of the WAL header, ** all prior frames, the first 16 bytes of this frame-header, ** and the frame-data matches the checksum in the last 8 ** bytes of this frame-header. */ nativeCksum = libc.BoolInt32(int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FbigEndCksum) == SQLITE_BIGENDIAN) _walChecksumBytes(tls, nativeCksum, aFrame, int32(8), aCksum, aCksum) _walChecksumBytes(tls, nativeCksum, aData, int32((*TWal)(unsafe.Pointer(pWal)).FszPage), aCksum, aCksum) if *(*Tu32)(unsafe.Pointer(aCksum)) != _sqlite3Get4byte(tls, aFrame+16) || *(*Tu32)(unsafe.Pointer(aCksum + 1*4)) != _sqlite3Get4byte(tls, aFrame+20) { /* Checksum failed. */ return 0 } /* If we reach this point, the frame is valid. Return the page number ** and the new database size. */ *(*Tu32)(unsafe.Pointer(piPage)) = pgno *(*Tu32)(unsafe.Pointer(pnTruncate)) = _sqlite3Get4byte(tls, aFrame+4) return int32(1) } // C documentation // // /* // ** Set or release locks on the WAL. Locks are either shared or exclusive. // ** A lock cannot be moved directly between shared and exclusive - it must go // ** through the unlocked state first. // ** // ** In locking_mode=EXCLUSIVE, all of these routines become no-ops. // */ func _walLockShared(tls *libc.TLS, pWal uintptr, lockIdx int32) (r int32) { var rc int32 _ = rc if (*TWal)(unsafe.Pointer(pWal)).FexclusiveMode != 0 { return SQLITE_OK } rc = _sqlite3OsShmLock(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, lockIdx, int32(1), libc.Int32FromInt32(SQLITE_SHM_LOCK)|libc.Int32FromInt32(SQLITE_SHM_SHARED)) return rc } func _walUnlockShared(tls *libc.TLS, pWal uintptr, lockIdx int32) { if (*TWal)(unsafe.Pointer(pWal)).FexclusiveMode != 0 { return } _sqlite3OsShmLock(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, lockIdx, int32(1), libc.Int32FromInt32(SQLITE_SHM_UNLOCK)|libc.Int32FromInt32(SQLITE_SHM_SHARED)) } func _walLockExclusive(tls *libc.TLS, pWal uintptr, lockIdx int32, n int32) (r int32) { var rc int32 _ = rc if (*TWal)(unsafe.Pointer(pWal)).FexclusiveMode != 0 { return SQLITE_OK } rc = _sqlite3OsShmLock(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, lockIdx, n, libc.Int32FromInt32(SQLITE_SHM_LOCK)|libc.Int32FromInt32(SQLITE_SHM_EXCLUSIVE)) return rc } func _walUnlockExclusive(tls *libc.TLS, pWal uintptr, lockIdx int32, n int32) { if (*TWal)(unsafe.Pointer(pWal)).FexclusiveMode != 0 { return } _sqlite3OsShmLock(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, lockIdx, n, libc.Int32FromInt32(SQLITE_SHM_UNLOCK)|libc.Int32FromInt32(SQLITE_SHM_EXCLUSIVE)) } // C documentation // // /* // ** Compute a hash on a page number. The resulting hash value must land // ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances // ** the hash to the next value in the event of a collision. // */ func _walHash(tls *libc.TLS, iPage Tu32) (r int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return int32(iPage * uint32(HASHTABLE_HASH_1) & uint32(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2)-libc.Int32FromInt32(1))) } func _walNextHash(tls *libc.TLS, iPriorHash int32) (r int32) { return (iPriorHash + int32(1)) & (libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2) - libc.Int32FromInt32(1)) } // C documentation // // /* // ** An instance of the WalHashLoc object is used to describe the location // ** of a page hash table in the wal-index. This becomes the return value // ** from walHashGet(). // */ type TWalHashLoc = struct { FaHash uintptr FaPgno uintptr FiZero Tu32 } type WalHashLoc = TWalHashLoc type TWalHashLoc1 = struct { FaHash uintptr FaPgno uintptr FiZero Tu32 } type WalHashLoc1 = TWalHashLoc1 // C documentation // // /* // ** Return pointers to the hash table and page number array stored on // ** page iHash of the wal-index. The wal-index is broken into 32KB pages // ** numbered starting from 0. // ** // ** Set output variable pLoc->aHash to point to the start of the hash table // ** in the wal-index file. Set pLoc->iZero to one less than the frame // ** number of the first frame indexed by this hash table. If a // ** slot in the hash table is set to N, it refers to frame number // ** (pLoc->iZero+N) in the log. // ** // ** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the // ** first frame indexed by the hash table, frame (pLoc->iZero). // */ func _walHashGet(tls *libc.TLS, pWal uintptr, iHash int32, pLoc uintptr) (r int32) { var rc int32 _ = rc /* Return code */ rc = _walIndexPage(tls, pWal, iHash, pLoc+4) _ = libc.Int32FromInt32(0) if (*TWalHashLoc)(unsafe.Pointer(pLoc)).FaPgno != 0 { (*TWalHashLoc)(unsafe.Pointer(pLoc)).FaHash = (*TWalHashLoc)(unsafe.Pointer(pLoc)).FaPgno + 4096*4 if iHash == 0 { (*TWalHashLoc)(unsafe.Pointer(pLoc)).FaPgno = (*TWalHashLoc)(unsafe.Pointer(pLoc)).FaPgno + uintptr((libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(40))/libc.Uint32FromInt64(4))*4 (*TWalHashLoc)(unsafe.Pointer(pLoc)).FiZero = uint32(0) } else { (*TWalHashLoc)(unsafe.Pointer(pLoc)).FiZero = libc.Uint32FromInt32(HASHTABLE_NPAGE) - (libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(40))/libc.Uint32FromInt64(4) + uint32((iHash-int32(1))*int32(HASHTABLE_NPAGE)) } } else { if rc == SQLITE_OK { rc = int32(SQLITE_ERROR) } } return rc } // C documentation // // /* // ** Return the number of the wal-index page that contains the hash-table // ** and page-number array that contain entries corresponding to WAL frame // ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages // ** are numbered starting from 0. // */ func _walFramePage(tls *libc.TLS, iFrame Tu32) (r int32) { var iHash int32 _ = iHash iHash = int32((iFrame + uint32(HASHTABLE_NPAGE) - (libc.Uint32FromInt32(HASHTABLE_NPAGE) - (libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(40))/libc.Uint32FromInt64(4)) - uint32(1)) / uint32(HASHTABLE_NPAGE)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return iHash } // C documentation // // /* // ** Return the page number associated with frame iFrame in this WAL. // */ func _walFramePgno(tls *libc.TLS, pWal uintptr, iFrame Tu32) (r Tu32) { var iHash int32 _ = iHash iHash = _walFramePage(tls, iFrame) _ = libc.Int32FromInt32(0) if iHash == 0 { return *(*Tu32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData)) + uintptr((libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(40))/libc.Uint32FromInt64(4)+iFrame-uint32(1))*4)) } return *(*Tu32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iHash)*4)) + uintptr((iFrame-uint32(1)-(libc.Uint32FromInt32(HASHTABLE_NPAGE)-(libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(40))/libc.Uint32FromInt64(4)))%uint32(HASHTABLE_NPAGE))*4)) } // C documentation // // /* // ** Remove entries from the hash table that point to WAL slots greater // ** than pWal->hdr.mxFrame. // ** // ** This function is called whenever pWal->hdr.mxFrame is decreased due // ** to a rollback or savepoint. // ** // ** At most only the hash table containing pWal->hdr.mxFrame needs to be // ** updated. Any later hash tables will be automatically cleared when // ** pWal->hdr.mxFrame advances to the point where those hash tables are // ** actually needed. // */ func _walCleanupHash(tls *libc.TLS, pWal uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var i, iLimit, nByte int32 var _ /* sLoc at bp+0 */ TWalHashLoc _, _, _ = i, iLimit, nByte /* Hash table location */ iLimit = 0 /* Used to iterate through aHash[] */ _ = libc.Int32FromInt32(0) if (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame == uint32(0) { return } /* Obtain pointers to the hash-table and page-number array containing ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed ** that the page said hash-table and array reside on is already mapped.(1) */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) i = _walHashGet(tls, pWal, _walFramePage(tls, (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame), bp) if i != 0 { return } /* Defense-in-depth, in case (1) above is wrong */ /* Zero all hash-table entries that correspond to frame numbers greater ** than pWal->hdr.mxFrame. */ iLimit = int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame - (*(*TWalHashLoc)(unsafe.Pointer(bp))).FiZero) _ = libc.Int32FromInt32(0) i = 0 for { if !(i < libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2)) { break } if int32(*(*Tht_slot)(unsafe.Pointer((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaHash + uintptr(i)*2))) > iLimit { *(*Tht_slot)(unsafe.Pointer((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaHash + uintptr(i)*2)) = uint16(0) } goto _1 _1: ; i++ } /* Zero the entries in the aPgno array that correspond to frames with ** frame numbers greater than pWal->hdr.mxFrame. */ nByte = int32((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaHash) - int32((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno+uintptr(iLimit)*4) _ = libc.Int32FromInt32(0) libc.Xmemset(tls, (*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno+uintptr(iLimit)*4, 0, uint32(nByte)) } // C documentation // // /* // ** Set an entry in the wal-index that will map database page number // ** pPage into WAL frame iFrame. // */ func _walIndexAppend(tls *libc.TLS, pWal uintptr, iFrame Tu32, iPage Tu32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iKey, idx, nByte, nCollide, rc, v2 int32 var _ /* sLoc at bp+0 */ TWalHashLoc _, _, _, _, _, _ = iKey, idx, nByte, nCollide, rc, v2 /* Wal-index hash table location */ rc = _walHashGet(tls, pWal, _walFramePage(tls, iFrame), bp) /* Assuming the wal-index file was successfully mapped, populate the ** page number array and hash table entry. */ if rc == SQLITE_OK { /* Number of hash collisions */ idx = int32(iFrame - (*(*TWalHashLoc)(unsafe.Pointer(bp))).FiZero) _ = libc.Int32FromInt32(0) /* If this is the first entry to be added to this hash-table, zero the ** entire hash table and aPgno[] array before proceeding. */ if idx == int32(1) { nByte = int32((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaHash+uintptr(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))*2) - int32((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno) _ = libc.Int32FromInt32(0) libc.Xmemset(tls, (*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno, 0, uint32(nByte)) } /* If the entry in aPgno[] is already set, then the previous writer ** must have exited unexpectedly in the middle of a transaction (after ** writing one or more dirty pages to the WAL to free up memory). ** Remove the remnants of that writers uncommitted transaction from ** the hash-table before writing any new entries. */ if *(*Tu32)(unsafe.Pointer((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno + uintptr(idx-int32(1))*4)) != 0 { _walCleanupHash(tls, pWal) _ = libc.Int32FromInt32(0) } /* Write the aPgno[] array entry and the hash-table slot. */ nCollide = idx iKey = _walHash(tls, iPage) for { if !(*(*Tht_slot)(unsafe.Pointer((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaHash + uintptr(iKey)*2)) != 0) { break } v2 = nCollide nCollide-- if v2 == 0 { return _sqlite3CorruptError(tls, int32(65899)) } goto _1 _1: ; iKey = _walNextHash(tls, iKey) } *(*Tu32)(unsafe.Pointer((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno + uintptr(idx-int32(1))*4)) = iPage libc.AtomicStoreNUint16((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaHash+uintptr(iKey)*2, uint16(idx), libc.Int32FromInt32(__ATOMIC_RELAXED)) } return rc } // C documentation // // /* // ** Recover the wal-index by reading the write-ahead log file. // ** // ** This routine first tries to establish an exclusive lock on the // ** wal-index to prevent other threads/processes from doing anything // ** with the WAL or wal-index while recovery is running. The // ** WAL_RECOVER_LOCK is also held so that other threads will know // ** that this thread is running recovery. If unable to establish // ** the necessary locks, this routine returns SQLITE_BUSY. // */ func _walIndexRecover(tls *libc.TLS, pWal uintptr) (r int32) { bp := tls.Alloc(80) defer tls.Free(80) var aData, aFrame, aPrivate, pInfo uintptr var aFrameCksum [2]Tu32 var i, iLock, isValid, rc, szFrame, szPage int32 var iFirst, iFrame, iLast, iLastFrame, iPg, magic, nHdr, nHdr32, version Tu32 var iOffset Ti64 var v2, v3, v5 uint32 var _ /* aBuf at bp+8 */ [32]Tu8 var _ /* aShare at bp+40 */ uintptr var _ /* nSize at bp+0 */ Ti64 var _ /* nTruncate at bp+48 */ Tu32 var _ /* pgno at bp+44 */ Tu32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aData, aFrame, aFrameCksum, aPrivate, i, iFirst, iFrame, iLast, iLastFrame, iLock, iOffset, iPg, isValid, magic, nHdr, nHdr32, pInfo, rc, szFrame, szPage, version, v2, v3, v5 /* Size of log file */ aFrameCksum = [2]Tu32{} /* Lock offset to lock for checkpoint */ /* Obtain an exclusive lock on all byte in the locking range not already ** locked by the caller. The caller is guaranteed to have locked the ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte. ** If successful, the same bytes that are locked here are unlocked before ** this function returns. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) iLock = int32(WAL_ALL_BUT_WRITE) + int32((*TWal)(unsafe.Pointer(pWal)).FckptLock) rc = _walLockExclusive(tls, pWal, iLock, libc.Int32FromInt32(3)+libc.Int32FromInt32(0)-iLock) if rc != 0 { return rc } libc.Xmemset(tls, pWal+52, 0, uint32(48)) rc = _sqlite3OsFileSize(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, bp) if rc != SQLITE_OK { goto recovery_error } if *(*Ti64)(unsafe.Pointer(bp)) > int64(WAL_HDRSIZE) { /* Buffer to load WAL header into */ aPrivate = uintptr(0) /* Heap copy of *-shm hash being populated */ aFrame = uintptr(0) /* Last frame in wal, based on nSize alone */ /* Read in the WAL header. */ rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, bp+8, int32(WAL_HDRSIZE), 0) if rc != SQLITE_OK { goto recovery_error } /* If the database page size is not a power of two, or is greater than ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid ** data. Similarly, if the 'magic' value is invalid, ignore the whole ** WAL file. */ magic = _sqlite3Get4byte(tls, bp+8) szPage = int32(_sqlite3Get4byte(tls, bp+8+8)) if magic&uint32(0xFFFFFFFE) != uint32(WAL_MAGIC) || szPage&(szPage-int32(1)) != 0 || szPage > int32(SQLITE_MAX_PAGE_SIZE) || szPage < int32(512) { goto finished } (*TWal)(unsafe.Pointer(pWal)).Fhdr.FbigEndCksum = uint8(magic & libc.Uint32FromInt32(0x00000001)) (*TWal)(unsafe.Pointer(pWal)).FszPage = uint32(szPage) (*TWal)(unsafe.Pointer(pWal)).FnCkpt = _sqlite3Get4byte(tls, bp+8+12) libc.Xmemcpy(tls, pWal+52+32, bp+8+16, uint32(8)) /* Verify that the WAL header checksum is correct */ _walChecksumBytes(tls, libc.BoolInt32(int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FbigEndCksum) == SQLITE_BIGENDIAN), bp+8, libc.Int32FromInt32(WAL_HDRSIZE)-libc.Int32FromInt32(2)*libc.Int32FromInt32(4), uintptr(0), pWal+52+24) if *(*Tu32)(unsafe.Pointer(pWal + 52 + 24)) != _sqlite3Get4byte(tls, bp+8+24) || *(*Tu32)(unsafe.Pointer(pWal + 52 + 24 + 1*4)) != _sqlite3Get4byte(tls, bp+8+28) { goto finished } /* Verify that the version number on the WAL format is one that ** are able to understand */ version = _sqlite3Get4byte(tls, bp+8+4) if version != uint32(WAL_MAX_VERSION) { rc = _sqlite3CantopenError(tls, int32(66031)) goto finished } /* Malloc a buffer to read frames into. */ szFrame = szPage + int32(WAL_FRAME_HDRSIZE) aFrame = Xsqlite3_malloc64(tls, uint64(uint32(szFrame)+(libc.Uint32FromInt64(2)*uint32(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint32FromInt32(HASHTABLE_NPAGE)*libc.Uint32FromInt64(4)))) if !(aFrame != 0) { rc = int32(SQLITE_NOMEM) goto recovery_error } aData = aFrame + 24 aPrivate = aData + uintptr(szPage) /* Read all frames from the log file. */ iLastFrame = uint32((*(*Ti64)(unsafe.Pointer(bp)) - int64(WAL_HDRSIZE)) / int64(szFrame)) iPg = uint32(0) for { if !(iPg <= uint32(_walFramePage(tls, iLastFrame))) { break } if iLastFrame < libc.Uint32FromInt32(HASHTABLE_NPAGE)-(libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(40))/libc.Uint32FromInt64(4)+iPg*uint32(HASHTABLE_NPAGE) { v2 = iLastFrame } else { v2 = libc.Uint32FromInt32(HASHTABLE_NPAGE) - (libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(40))/libc.Uint32FromInt64(4) + iPg*uint32(HASHTABLE_NPAGE) } /* Index of last frame read */ iLast = v2 if iPg == uint32(0) { v3 = uint32(0) } else { v3 = libc.Uint32FromInt32(HASHTABLE_NPAGE) - (libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(40))/libc.Uint32FromInt64(4) + (iPg-uint32(1))*uint32(HASHTABLE_NPAGE) } iFirst = uint32(1) + v3 rc = _walIndexPage(tls, pWal, int32(iPg), bp+40) _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(bp + 40)) == uintptr(0) { break } *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPg)*4)) = aPrivate iFrame = iFirst for { if !(iFrame <= iLast) { break } iOffset = libc.Int64FromInt32(WAL_HDRSIZE) + int64(iFrame-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) /* dbsize field from frame header */ /* Read and decode the next log frame. */ rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, aFrame, szFrame, iOffset) if rc != SQLITE_OK { break } isValid = _walDecodeFrame(tls, pWal, bp+44, bp+48, aData, aFrame) if !(isValid != 0) { break } rc = _walIndexAppend(tls, pWal, iFrame, *(*Tu32)(unsafe.Pointer(bp + 44))) if rc != SQLITE_OK { break } /* If nTruncate is non-zero, this is a commit record. */ if *(*Tu32)(unsafe.Pointer(bp + 48)) != 0 { (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame = iFrame (*TWal)(unsafe.Pointer(pWal)).Fhdr.FnPage = *(*Tu32)(unsafe.Pointer(bp + 48)) (*TWal)(unsafe.Pointer(pWal)).Fhdr.FszPage = uint16(szPage&libc.Int32FromInt32(0xff00) | szPage>>libc.Int32FromInt32(16)) aFrameCksum[0] = *(*Tu32)(unsafe.Pointer(pWal + 52 + 24)) aFrameCksum[int32(1)] = *(*Tu32)(unsafe.Pointer(pWal + 52 + 24 + 1*4)) } goto _4 _4: ; iFrame++ } *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPg)*4)) = *(*uintptr)(unsafe.Pointer(bp + 40)) if iPg == uint32(0) { v5 = libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2) + libc.Uint32FromInt64(40) } else { v5 = uint32(0) } nHdr = v5 nHdr32 = nHdr / uint32(4) /* Memcpy() should work fine here, on all reasonable implementations. ** Technically, memcpy() might change the destination to some ** intermediate value before setting to the final value, and that might ** cause a concurrent reader to malfunction. Memcpy() is allowed to ** do that, according to the spec, but no memcpy() implementation that ** we know of actually does that, which is why we say that memcpy() ** is safe for this. Memcpy() is certainly a lot faster. */ libc.Xmemcpy(tls, *(*uintptr)(unsafe.Pointer(bp + 40))+uintptr(nHdr32)*4, aPrivate+uintptr(nHdr32)*4, libc.Uint32FromInt64(2)*uint32(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint32FromInt32(HASHTABLE_NPAGE)*libc.Uint32FromInt64(4)-nHdr) _ = libc.Int32FromInt32(0) if iFrame <= iLast { break } goto _1 _1: ; iPg++ } Xsqlite3_free(tls, aFrame) } goto finished finished: ; if rc == SQLITE_OK { *(*Tu32)(unsafe.Pointer(pWal + 52 + 24)) = aFrameCksum[0] *(*Tu32)(unsafe.Pointer(pWal + 52 + 24 + 1*4)) = aFrameCksum[int32(1)] _walIndexWriteHdr(tls, pWal) /* Reset the checkpoint-header. This is safe because this thread is ** currently holding locks that exclude all other writers and ** checkpointers. Then set the values of read-mark slots 1 through N. */ pInfo = _walCkptInfo(tls, pWal) (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfill = uint32(0) (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfillAttempted = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame *(*Tu32)(unsafe.Pointer(pInfo + 4)) = uint32(0) i = int32(1) for { if !(i < libc.Int32FromInt32(SQLITE_SHM_NLOCK)-libc.Int32FromInt32(3)) { break } rc = _walLockExclusive(tls, pWal, int32(3)+i, int32(1)) if rc == SQLITE_OK { if i == int32(1) && (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame != 0 { *(*Tu32)(unsafe.Pointer(pInfo + 4 + uintptr(i)*4)) = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame } else { *(*Tu32)(unsafe.Pointer(pInfo + 4 + uintptr(i)*4)) = uint32(READMARK_NOT_USED) } _ = libc.Int32FromInt32(0) _walUnlockExclusive(tls, pWal, int32(3)+i, int32(1)) } else { if rc != int32(SQLITE_BUSY) { goto recovery_error } } goto _6 _6: ; i++ } /* If more than one frame was recovered from the log file, report an ** event via sqlite3_log(). This is to help with identifying performance ** problems caused by applications routinely shutting down without ** checkpointing the log file. */ if (*TWal)(unsafe.Pointer(pWal)).Fhdr.FnPage != 0 { Xsqlite3_log(tls, libc.Int32FromInt32(SQLITE_NOTICE)|libc.Int32FromInt32(1)<= 0) { break } pSegment = p + 8 + uintptr(i)*20 for (*TWalSegment)(unsafe.Pointer(pSegment)).FiNext < (*TWalSegment)(unsafe.Pointer(pSegment)).FnEntry { iPg = *(*Tu32)(unsafe.Pointer((*TWalSegment)(unsafe.Pointer(pSegment)).FaPgno + uintptr(*(*Tht_slot)(unsafe.Pointer((*TWalSegment)(unsafe.Pointer(pSegment)).FaIndex + uintptr((*TWalSegment)(unsafe.Pointer(pSegment)).FiNext)*2)))*4)) if iPg > iMin { if iPg < iRet { iRet = iPg *(*Tu32)(unsafe.Pointer(piFrame)) = uint32((*TWalSegment)(unsafe.Pointer(pSegment)).FiZero + int32(*(*Tht_slot)(unsafe.Pointer((*TWalSegment)(unsafe.Pointer(pSegment)).FaIndex + uintptr((*TWalSegment)(unsafe.Pointer(pSegment)).FiNext)*2)))) } break } (*TWalSegment)(unsafe.Pointer(pSegment)).FiNext++ } goto _1 _1: ; i-- } v2 = iRet (*TWalIterator)(unsafe.Pointer(p)).FiPrior = v2 *(*Tu32)(unsafe.Pointer(piPage)) = v2 return libc.BoolInt32(iRet == uint32(0xFFFFFFFF)) } // C documentation // // /* // ** This function merges two sorted lists into a single sorted list. // ** // ** aLeft[] and aRight[] are arrays of indices. The sort key is // ** aContent[aLeft[]] and aContent[aRight[]]. Upon entry, the following // ** is guaranteed for all J= nRight || *(*Tu32)(unsafe.Pointer(aContent + uintptr(*(*Tht_slot)(unsafe.Pointer(aLeft + uintptr(iLeft)*2)))*4)) < *(*Tu32)(unsafe.Pointer(aContent + uintptr(*(*Tht_slot)(unsafe.Pointer(aRight + uintptr(iRight)*2)))*4))) { v1 = iLeft iLeft++ logpage = *(*Tht_slot)(unsafe.Pointer(aLeft + uintptr(v1)*2)) } else { v2 = iRight iRight++ logpage = *(*Tht_slot)(unsafe.Pointer(aRight + uintptr(v2)*2)) } dbpage = *(*Tu32)(unsafe.Pointer(aContent + uintptr(logpage)*4)) v3 = iOut iOut++ *(*Tht_slot)(unsafe.Pointer(aTmp + uintptr(v3)*2)) = logpage if iLeft < nLeft && *(*Tu32)(unsafe.Pointer(aContent + uintptr(*(*Tht_slot)(unsafe.Pointer(aLeft + uintptr(iLeft)*2)))*4)) == dbpage { iLeft++ } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } *(*uintptr)(unsafe.Pointer(paRight)) = aLeft *(*int32)(unsafe.Pointer(pnRight)) = iOut libc.Xmemcpy(tls, aLeft, aTmp, uint32(2)*uint32(iOut)) } // C documentation // // /* // ** Sort the elements in list aList using aContent[] as the sort key. // ** Remove elements with duplicate keys, preferring to keep the // ** larger aList[] values. // ** // ** The aList[] entries are indices into aContent[]. The values in // ** aList[] are to be sorted so that for all J0). */ _ = libc.Int32FromInt32(0) iLast = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame /* Allocate space for the WalIterator object. */ nSegment = _walFramePage(tls, iLast) + int32(1) nByte = int64(uint32(28) + uint32(nSegment-libc.Int32FromInt32(1))*uint32(20) + iLast*uint32(2)) if iLast > uint32(HASHTABLE_NPAGE) { v1 = uint32(HASHTABLE_NPAGE) } else { v1 = iLast } p = Xsqlite3_malloc64(tls, uint64(nByte+int64(uint32(2)*v1))) if !(p != 0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, p, 0, uint32(nByte)) (*TWalIterator)(unsafe.Pointer(p)).FnSegment = nSegment aTmp = p + uintptr(nByte) i = _walFramePage(tls, nBackfill+uint32(1)) for { if !(rc == SQLITE_OK && i < nSegment) { break } rc = _walHashGet(tls, pWal, i, bp) if rc == SQLITE_OK { /* Sorted index for this segment */ if i+int32(1) == nSegment { *(*int32)(unsafe.Pointer(bp + 12)) = int32(iLast - (*(*TWalHashLoc)(unsafe.Pointer(bp))).FiZero) } else { *(*int32)(unsafe.Pointer(bp + 12)) = (int32((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaHash) - int32((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno)) / 4 } aIndex = p + 8 + uintptr((*TWalIterator)(unsafe.Pointer(p)).FnSegment)*20 + uintptr((*(*TWalHashLoc)(unsafe.Pointer(bp))).FiZero)*2 (*(*TWalHashLoc)(unsafe.Pointer(bp))).FiZero++ j = 0 for { if !(j < *(*int32)(unsafe.Pointer(bp + 12))) { break } *(*Tht_slot)(unsafe.Pointer(aIndex + uintptr(j)*2)) = uint16(j) goto _3 _3: ; j++ } _walMergesort(tls, (*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno, aTmp, aIndex, bp+12) (*(*TWalSegment)(unsafe.Pointer(p + 8 + uintptr(i)*20))).FiZero = int32((*(*TWalHashLoc)(unsafe.Pointer(bp))).FiZero) (*(*TWalSegment)(unsafe.Pointer(p + 8 + uintptr(i)*20))).FnEntry = *(*int32)(unsafe.Pointer(bp + 12)) (*(*TWalSegment)(unsafe.Pointer(p + 8 + uintptr(i)*20))).FaIndex = aIndex (*(*TWalSegment)(unsafe.Pointer(p + 8 + uintptr(i)*20))).FaPgno = (*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno } goto _2 _2: ; i++ } if rc != SQLITE_OK { _walIteratorFree(tls, p) p = uintptr(0) } *(*uintptr)(unsafe.Pointer(pp)) = p return rc } // C documentation // // /* // ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and // ** n. If the attempt fails and parameter xBusy is not NULL, then it is a // ** busy-handler function. Invoke it and retry the lock until either the // ** lock is successfully obtained or the busy-handler returns 0. // */ func _walBusyLock(tls *libc.TLS, pWal uintptr, xBusy uintptr, pBusyArg uintptr, lockIdx int32, n int32) (r int32) { var rc int32 _ = rc for cond := true; cond; cond = xBusy != 0 && rc == int32(SQLITE_BUSY) && (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xBusy})))(tls, pBusyArg) != 0 { rc = _walLockExclusive(tls, pWal, lockIdx, n) } return rc } // C documentation // // /* // ** The cache of the wal-index header must be valid to call this function. // ** Return the page-size in bytes used by the database. // */ func _walPagesize(tls *libc.TLS, pWal uintptr) (r int32) { return int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FszPage)&int32(0xfe00) + int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FszPage)&int32(0x0001)< y { _ = libc.Int32FromInt32(0) rc = _walBusyLock(tls, pWal, xBusy, pBusyArg, int32(3)+i, int32(1)) if rc == SQLITE_OK { if i == int32(1) { v2 = mxSafeFrame } else { v2 = uint32(READMARK_NOT_USED) } iMark = v2 libc.AtomicStoreNUint32(pInfo+4+uintptr(i)*4, iMark, libc.Int32FromInt32(__ATOMIC_RELAXED)) _ = libc.Int32FromInt32(0) _walUnlockExclusive(tls, pWal, int32(3)+i, int32(1)) } else { if rc == int32(SQLITE_BUSY) { mxSafeFrame = y xBusy = uintptr(0) } else { goto walcheckpoint_out } } } goto _1 _1: ; i++ } /* Allocate the iterator */ if (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfill < mxSafeFrame { rc = _walIteratorInit(tls, pWal, (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfill, bp) _ = libc.Int32FromInt32(0) } if v4 = *(*uintptr)(unsafe.Pointer(bp)) != 0; v4 { v3 = _walBusyLock(tls, pWal, xBusy, pBusyArg, libc.Int32FromInt32(3)+libc.Int32FromInt32(0), int32(1)) rc = v3 } if v4 && v3 == SQLITE_OK { nBackfill = (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfill (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfillAttempted = mxSafeFrame _ = libc.Int32FromInt32(0) /* Sync the WAL to disk */ rc = _sqlite3OsSync(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, sync_flags>>int32(2)&int32(0x03)) /* If the database may grow as a result of this checkpoint, hint ** about the eventual size of the db file to the VFS layer. */ if rc == SQLITE_OK { *(*Ti64)(unsafe.Pointer(bp + 16)) = int64(mxPage) * int64(szPage) /* Current size of database file */ _sqlite3OsFileControl(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, int32(SQLITE_FCNTL_CKPT_START), uintptr(0)) rc = _sqlite3OsFileSize(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, bp+24) if rc == SQLITE_OK && *(*Ti64)(unsafe.Pointer(bp + 24)) < *(*Ti64)(unsafe.Pointer(bp + 16)) { if *(*Ti64)(unsafe.Pointer(bp + 24))+int64(65536)+int64((*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame)*int64(szPage) < *(*Ti64)(unsafe.Pointer(bp + 16)) { /* If the size of the final database is larger than the current ** database plus the amount of data in the wal file, plus the ** maximum size of the pending-byte page (65536 bytes), then ** must be corruption somewhere. */ rc = _sqlite3CorruptError(tls, int32(66841)) } else { _sqlite3OsFileControlHint(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, int32(SQLITE_FCNTL_SIZE_HINT), bp+16) } } } /* Iterate through the contents of the WAL, copying data to the db file */ for rc == SQLITE_OK && 0 == _walIteratorNext(tls, *(*uintptr)(unsafe.Pointer(bp)), bp+4, bp+8) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if libc.AtomicLoadNInt32(db+312, libc.Int32FromInt32(__ATOMIC_RELAXED)) != 0 { if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { v5 = int32(SQLITE_NOMEM) } else { v5 = int32(SQLITE_INTERRUPT) } rc = v5 break } if *(*Tu32)(unsafe.Pointer(bp + 8)) <= nBackfill || *(*Tu32)(unsafe.Pointer(bp + 8)) > mxSafeFrame || *(*Tu32)(unsafe.Pointer(bp + 4)) > mxPage { continue } iOffset = int64(WAL_HDRSIZE) + int64(*(*Tu32)(unsafe.Pointer(bp + 8))-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) + int64(WAL_FRAME_HDRSIZE) /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */ rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, zBuf, szPage, iOffset) if rc != SQLITE_OK { break } iOffset = int64(*(*Tu32)(unsafe.Pointer(bp + 4))-libc.Uint32FromInt32(1)) * int64(szPage) rc = _sqlite3OsWrite(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, zBuf, szPage, iOffset) if rc != SQLITE_OK { break } } _sqlite3OsFileControl(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, int32(SQLITE_FCNTL_CKPT_DONE), uintptr(0)) /* If work was actually accomplished... */ if rc == SQLITE_OK { if mxSafeFrame == (*TWalIndexHdr)(unsafe.Pointer(_walIndexHdr(tls, pWal))).FmxFrame { szDb = int64((*TWal)(unsafe.Pointer(pWal)).Fhdr.FnPage) * int64(szPage) rc = _sqlite3OsTruncate(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, szDb) if rc == SQLITE_OK { rc = _sqlite3OsSync(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, sync_flags>>int32(2)&int32(0x03)) } } if rc == SQLITE_OK { libc.AtomicStoreNUint32(pInfo, mxSafeFrame, libc.Int32FromInt32(__ATOMIC_RELAXED)) _ = libc.Int32FromInt32(0) } } /* Release the reader lock held while backfilling */ _walUnlockExclusive(tls, pWal, libc.Int32FromInt32(3)+libc.Int32FromInt32(0), int32(1)) } if rc == int32(SQLITE_BUSY) { /* Reset the return code so as not to report a checkpoint failure ** just because there are active readers. */ rc = SQLITE_OK } } /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the ** entire wal file has been copied into the database file, then block ** until all readers have finished using the wal file. This ensures that ** the next process to write to the database restarts the wal file. */ if rc == SQLITE_OK && eMode != SQLITE_CHECKPOINT_PASSIVE { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfill < (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame { rc = int32(SQLITE_BUSY) } else { if eMode >= int32(SQLITE_CHECKPOINT_RESTART) { Xsqlite3_randomness(tls, int32(4), bp+32) _ = libc.Int32FromInt32(0) rc = _walBusyLock(tls, pWal, xBusy, pBusyArg, libc.Int32FromInt32(3)+libc.Int32FromInt32(1), libc.Int32FromInt32(SQLITE_SHM_NLOCK)-libc.Int32FromInt32(3)-libc.Int32FromInt32(1)) if rc == SQLITE_OK { if eMode == int32(SQLITE_CHECKPOINT_TRUNCATE) { /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as ** SQLITE_CHECKPOINT_RESTART with the addition that it also ** truncates the log file to zero bytes just prior to a ** successful return. ** ** In theory, it might be safe to do this without updating the ** wal-index header in shared memory, as all subsequent reader or ** writer clients should see that the entire log file has been ** checkpointed and behave accordingly. This seems unsafe though, ** as it would leave the system in a state where the contents of ** the wal-index header do not match the contents of the ** file-system. To avoid this, update the wal-index header to ** indicate that the log file contains zero valid frames. */ _walRestartHdr(tls, pWal, *(*Tu32)(unsafe.Pointer(bp + 32))) rc = _sqlite3OsTruncate(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, 0) } _walUnlockExclusive(tls, pWal, libc.Int32FromInt32(3)+libc.Int32FromInt32(1), libc.Int32FromInt32(SQLITE_SHM_NLOCK)-libc.Int32FromInt32(3)-libc.Int32FromInt32(1)) } } } } goto walcheckpoint_out walcheckpoint_out: ; _walIteratorFree(tls, *(*uintptr)(unsafe.Pointer(bp))) return rc } // C documentation // // /* // ** If the WAL file is currently larger than nMax bytes in size, truncate // ** it to exactly nMax bytes. If an error occurs while doing so, ignore it. // */ func _walLimitSize(tls *libc.TLS, pWal uintptr, nMax Ti64) { bp := tls.Alloc(32) defer tls.Free(32) var rx int32 var _ /* sz at bp+0 */ Ti64 _ = rx _sqlite3BeginBenignMalloc(tls) rx = _sqlite3OsFileSize(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, bp) if rx == SQLITE_OK && *(*Ti64)(unsafe.Pointer(bp)) > nMax { rx = _sqlite3OsTruncate(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, nMax) } _sqlite3EndBenignMalloc(tls) if rx != 0 { Xsqlite3_log(tls, rx, __ccgo_ts+4164, libc.VaList(bp+16, (*TWal)(unsafe.Pointer(pWal)).FzWalName)) } } // C documentation // // /* // ** Close a connection to a log file. // */ func _sqlite3WalClose(tls *libc.TLS, pWal uintptr, db uintptr, sync_flags int32, nBuf int32, zBuf uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var isDelete, rc, v1 int32 var v2 bool var _ /* bPersist at bp+0 */ int32 _, _, _, _ = isDelete, rc, v1, v2 rc = SQLITE_OK if pWal != 0 { isDelete = 0 /* True to unlink wal and wal-index files */ _ = libc.Int32FromInt32(0) /* If an EXCLUSIVE lock can be obtained on the database file (using the ** ordinary, rollback-mode locking methods, this guarantees that the ** connection associated with this log file is the only connection to ** the database. In this case checkpoint the database and unlink both ** the wal and wal-index files. ** ** The EXCLUSIVE lock is not released before returning. */ if v2 = zBuf != uintptr(0); v2 { v1 = _sqlite3OsLock(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, int32(SQLITE_LOCK_EXCLUSIVE)) rc = v1 } if v2 && SQLITE_OK == v1 { if int32((*TWal)(unsafe.Pointer(pWal)).FexclusiveMode) == WAL_NORMAL_MODE { (*TWal)(unsafe.Pointer(pWal)).FexclusiveMode = uint8(WAL_EXCLUSIVE_MODE) } rc = _sqlite3WalCheckpoint(tls, pWal, db, SQLITE_CHECKPOINT_PASSIVE, uintptr(0), uintptr(0), sync_flags, nBuf, zBuf, uintptr(0), uintptr(0)) if rc == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = -int32(1) _sqlite3OsFileControlHint(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, int32(SQLITE_FCNTL_PERSIST_WAL), bp) if *(*int32)(unsafe.Pointer(bp)) != int32(1) { /* Try to delete the WAL file if the checkpoint completed and ** fsynced (rc==SQLITE_OK) and if we are not in persistent-wal ** mode (!bPersist) */ isDelete = int32(1) } else { if (*TWal)(unsafe.Pointer(pWal)).FmxWalSize >= 0 { /* Try to truncate the WAL file to zero bytes if the checkpoint ** completed and fsynced (rc==SQLITE_OK) and we are in persistent ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a ** non-negative value (pWal->mxWalSize>=0). Note that we truncate ** to zero bytes as truncating to the journal_size_limit might ** leave a corrupt WAL file on disk. */ _walLimitSize(tls, pWal, 0) } } } } _walIndexClose(tls, pWal, isDelete) _sqlite3OsClose(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd) if isDelete != 0 { _sqlite3BeginBenignMalloc(tls) _sqlite3OsDelete(tls, (*TWal)(unsafe.Pointer(pWal)).FpVfs, (*TWal)(unsafe.Pointer(pWal)).FzWalName, 0) _sqlite3EndBenignMalloc(tls) } Xsqlite3_free(tls, (*TWal)(unsafe.Pointer(pWal)).FapWiData) Xsqlite3_free(tls, pWal) } return rc } // C documentation // // /* // ** Try to read the wal-index header. Return 0 on success and 1 if // ** there is a problem. // ** // ** The wal-index is in shared memory. Another thread or process might // ** be writing the header at the same time this procedure is trying to // ** read it, which might result in inconsistency. A dirty read is detected // ** by verifying that both copies of the header are the same and also by // ** a checksum on the header. // ** // ** If and only if the read is consistent and the header is different from // ** pWal->hdr, then pWal->hdr is updated to the content of the new header // ** and *pChanged is set to 1. // ** // ** If the checksum cannot be verified return non-zero. If the header // ** is read successfully and the checksum verified, return zero. // */ func _walIndexTryHdr(tls *libc.TLS, pWal uintptr, pChanged uintptr) (r int32) { bp := tls.Alloc(112) defer tls.Free(112) var aHdr uintptr var _ /* aCksum at bp+0 */ [2]Tu32 var _ /* h1 at bp+8 */ TWalIndexHdr var _ /* h2 at bp+56 */ TWalIndexHdr _ = aHdr /* Header in shared memory */ /* The first page of the wal-index must be mapped at this point. */ _ = libc.Int32FromInt32(0) /* Read the header. This might happen concurrently with a write to the ** same area of shared memory on a different CPU in a SMP, ** meaning it is possible that an inconsistent snapshot is read ** from the file. If this happens, return non-zero. ** ** tag-20200519-1: ** There are two copies of the header at the beginning of the wal-index. ** When reading, read [0] first then [1]. Writes are in the reverse order. ** Memory barriers are used to prevent the compiler or the hardware from ** reordering the reads and writes. TSAN and similar tools can sometimes ** give false-positive warnings about these accesses because the tools do not ** account for the double-read and the memory barrier. The use of mutexes ** here would be problematic as the memory being accessed is potentially ** shared among multiple processes and not all mutex implementations work ** reliably in that environment. */ aHdr = _walIndexHdr(tls, pWal) libc.Xmemcpy(tls, bp+8, aHdr, uint32(48)) /* Possible TSAN false-positive */ _walShmBarrier(tls, pWal) libc.Xmemcpy(tls, bp+56, aHdr+1*48, uint32(48)) if libc.Xmemcmp(tls, bp+8, bp+56, uint32(48)) != 0 { return int32(1) /* Dirty read */ } if int32((*(*TWalIndexHdr)(unsafe.Pointer(bp + 8))).FisInit) == 0 { return int32(1) /* Malformed header - probably all zeros */ } _walChecksumBytes(tls, int32(1), bp+8, int32(libc.Uint32FromInt64(48)-libc.Uint32FromInt64(8)), uintptr(0), bp) if (*(*[2]Tu32)(unsafe.Pointer(bp)))[0] != *(*Tu32)(unsafe.Pointer(bp + 8 + 40)) || (*(*[2]Tu32)(unsafe.Pointer(bp)))[int32(1)] != *(*Tu32)(unsafe.Pointer(bp + 8 + 40 + 1*4)) { return int32(1) /* Checksum does not match */ } if libc.Xmemcmp(tls, pWal+52, bp+8, uint32(48)) != 0 { *(*int32)(unsafe.Pointer(pChanged)) = int32(1) libc.Xmemcpy(tls, pWal+52, bp+8, uint32(48)) (*TWal)(unsafe.Pointer(pWal)).FszPage = uint32(int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FszPage)&int32(0xfe00) + int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FszPage)&int32(0x0001)<hdr. // ** If the wal-header appears to be corrupt, try to reconstruct the // ** wal-index from the WAL before returning. // ** // ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is // ** changed by this operation. If pWal->hdr is unchanged, set *pChanged // ** to 0. // ** // ** If the wal-index header is successfully read, return SQLITE_OK. // ** Otherwise an SQLite error code. // */ func _walIndexReadHdr(tls *libc.TLS, pWal uintptr, pChanged uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bWriteLock, badHdr, rc, v1, v2, v3, v5 int32 var v4 bool var _ /* page0 at bp+0 */ uintptr _, _, _, _, _, _, _, _ = bWriteLock, badHdr, rc, v1, v2, v3, v4, v5 /* Chunk of wal-index containing header */ /* Ensure that page 0 of the wal-index (the page that contains the ** wal-index header) is mapped. Return early if an error occurs here. */ _ = libc.Int32FromInt32(0) rc = _walIndexPage(tls, pWal, 0, bp) if rc != SQLITE_OK { _ = libc.Int32FromInt32(0) /* READONLY changed to OK in walIndexPage */ if rc == libc.Int32FromInt32(SQLITE_READONLY)|libc.Int32FromInt32(5)<writeLock ** is zero, which prevents the SHM from growing */ } _ = libc.Int32FromInt32(0) /* If the first page of the wal-index has been mapped, try to read the ** wal-index header immediately, without holding any lock. This usually ** works, but may fail if the wal-index header is corrupt or currently ** being modified by another thread or process. */ if *(*uintptr)(unsafe.Pointer(bp)) != 0 { v1 = _walIndexTryHdr(tls, pWal, pChanged) } else { v1 = int32(1) } badHdr = v1 /* If the first attempt failed, it might have been due to a race ** with a writer. So get a WRITE lock and try again. */ if badHdr != 0 { if int32((*TWal)(unsafe.Pointer(pWal)).FbShmUnreliable) == 0 && int32((*TWal)(unsafe.Pointer(pWal)).FreadOnly)&int32(WAL_SHM_RDONLY) != 0 { v2 = _walLockShared(tls, pWal, WAL_WRITE_LOCK) rc = v2 if SQLITE_OK == v2 { _walUnlockShared(tls, pWal, WAL_WRITE_LOCK) rc = libc.Int32FromInt32(SQLITE_READONLY) | libc.Int32FromInt32(1)<apWiData[] using heap memory instead of shared // ** memory. // ** // ** If this function returns SQLITE_OK, then the read transaction has // ** been successfully opened. In this case output variable (*pChanged) // ** is set to true before returning if the caller should discard the // ** contents of the page cache before proceeding. Or, if it returns // ** WAL_RETRY, then the heap memory wal-index has been discarded and // ** the caller should retry opening the read transaction from the // ** beginning (including attempting to map the *-shm file). // ** // ** If an error occurs, an SQLite error code is returned. // */ func _walBeginShmUnreliable(tls *libc.TLS, pWal uintptr, pChanged uintptr) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var aData, aFrame uintptr var aSaveCksum [2]Tu32 var i, rc, szFrame, v1, v2 int32 var iOffset Ti64 var _ /* aBuf at bp+8 */ [32]Tu8 var _ /* nTruncate at bp+48 */ Tu32 var _ /* pDummy at bp+40 */ uintptr var _ /* pgno at bp+44 */ Tu32 var _ /* szWal at bp+0 */ Ti64 _, _, _, _, _, _, _, _, _ = aData, aFrame, aSaveCksum, i, iOffset, rc, szFrame, v1, v2 /* Buffer to load WAL header into */ aFrame = uintptr(0) /* Saved copy of pWal->hdr.aFrameCksum */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Take WAL_READ_LOCK(0). This has the effect of preventing any ** writers from running a checkpoint, but does not stop them ** from running recovery. */ rc = _walLockShared(tls, pWal, libc.Int32FromInt32(3)+libc.Int32FromInt32(0)) if rc != SQLITE_OK { if rc == int32(SQLITE_BUSY) { rc = -int32(1) } goto begin_unreliable_shm_out } (*TWal)(unsafe.Pointer(pWal)).FreadLock = 0 /* Check to see if a separate writer has attached to the shared-memory area, ** thus making the shared-memory "reliable" again. Do this by invoking ** the xShmMap() routine of the VFS and looking to see if the return ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT. ** ** If the shared-memory is now "reliable" return WAL_RETRY, which will ** cause the heap-memory WAL-index to be discarded and the actual ** shared memory to be used in its place. ** ** This step is important because, even though this connection is holding ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might ** have already checkpointed the WAL file and, while the current ** is active, wrap the WAL and start overwriting frames that this ** process wants to use. ** ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations, ** even if some external agent does a "chmod" to make the shared-memory ** writable by us, until sqlite3OsShmUnmap() has been called. ** This is a requirement on the VFS implementation. */ rc = _sqlite3OsShmMap(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, 0, int32(libc.Uint32FromInt64(2)*uint32(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint32FromInt32(HASHTABLE_NPAGE)*libc.Uint32FromInt64(4)), 0, bp+40) _ = libc.Int32FromInt32(0) /* SQLITE_OK not possible for read-only connection */ if rc != libc.Int32FromInt32(SQLITE_READONLY)|libc.Int32FromInt32(5)<hdr. */ libc.Xmemcpy(tls, pWal+52, _walIndexHdr(tls, pWal), uint32(48)) /* Make sure some writer hasn't come in and changed the WAL file out ** from under us, then disconnected, while we were not looking. */ rc = _sqlite3OsFileSize(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, bp) if rc != SQLITE_OK { goto begin_unreliable_shm_out } if *(*Ti64)(unsafe.Pointer(bp)) < int64(WAL_HDRSIZE) { /* If the wal file is too small to contain a wal-header and the ** wal-index header has mxFrame==0, then it must be safe to proceed ** reading the database file only. However, the page cache cannot ** be trusted, as a read/write connection may have connected, written ** the db, run a checkpoint, truncated the wal file and disconnected ** since this client's last read transaction. */ *(*int32)(unsafe.Pointer(pChanged)) = int32(1) if (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame == uint32(0) { v2 = SQLITE_OK } else { v2 = -int32(1) } rc = v2 goto begin_unreliable_shm_out } /* Check the salt keys at the start of the wal file still match. */ rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, bp+8, int32(WAL_HDRSIZE), 0) if rc != SQLITE_OK { goto begin_unreliable_shm_out } if libc.Xmemcmp(tls, pWal+52+32, bp+8+16, uint32(8)) != 0 { /* Some writer has wrapped the WAL file while we were not looking. ** Return WAL_RETRY which will cause the in-memory WAL-index to be ** rebuilt. */ rc = -int32(1) goto begin_unreliable_shm_out } /* Allocate a buffer to read frames into */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) szFrame = int32((*TWal)(unsafe.Pointer(pWal)).FszPage + uint32(WAL_FRAME_HDRSIZE)) aFrame = Xsqlite3_malloc64(tls, uint64(szFrame)) if aFrame == uintptr(0) { rc = int32(SQLITE_NOMEM) goto begin_unreliable_shm_out } aData = aFrame + 24 /* Check to see if a complete transaction has been appended to the ** wal file since the heap-memory wal-index was created. If so, the ** heap-memory wal-index is discarded and WAL_RETRY returned to ** the caller. */ aSaveCksum[0] = *(*Tu32)(unsafe.Pointer(pWal + 52 + 24)) aSaveCksum[int32(1)] = *(*Tu32)(unsafe.Pointer(pWal + 52 + 24 + 1*4)) iOffset = libc.Int64FromInt32(WAL_HDRSIZE) + int64((*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame+libc.Uint32FromInt32(1)-libc.Uint32FromInt32(1))*int64((*TWal)(unsafe.Pointer(pWal)).FszPage+libc.Uint32FromInt32(WAL_FRAME_HDRSIZE)) for { if !(iOffset+int64(szFrame) <= *(*Ti64)(unsafe.Pointer(bp))) { break } /* dbsize field from frame header */ /* Read and decode the next log frame. */ rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, aFrame, szFrame, iOffset) if rc != SQLITE_OK { break } if !(_walDecodeFrame(tls, pWal, bp+44, bp+48, aData, aFrame) != 0) { break } /* If nTruncate is non-zero, then a complete transaction has been ** appended to this wal file. Set rc to WAL_RETRY and break out of ** the loop. */ if *(*Tu32)(unsafe.Pointer(bp + 48)) != 0 { rc = -int32(1) break } goto _3 _3: ; iOffset += int64(szFrame) } *(*Tu32)(unsafe.Pointer(pWal + 52 + 24)) = aSaveCksum[0] *(*Tu32)(unsafe.Pointer(pWal + 52 + 24 + 1*4)) = aSaveCksum[int32(1)] goto begin_unreliable_shm_out begin_unreliable_shm_out: ; Xsqlite3_free(tls, aFrame) if rc != SQLITE_OK { i = 0 for { if !(i < (*TWal)(unsafe.Pointer(pWal)).FnWiData) { break } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(i)*4))) *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(i)*4)) = uintptr(0) goto _4 _4: ; i++ } (*TWal)(unsafe.Pointer(pWal)).FbShmUnreliable = uint8(0) _sqlite3WalEndReadTransaction(tls, pWal) *(*int32)(unsafe.Pointer(pChanged)) = int32(1) } return rc } /* ** The final argument passed to walTryBeginRead() is of type (int*). The ** caller should invoke walTryBeginRead as follows: ** ** int cnt = 0; ** do { ** rc = walTryBeginRead(..., &cnt); ** }while( rc==WAL_RETRY ); ** ** The final value of "cnt" is of no use to the caller. It is used by ** the implementation of walTryBeginRead() as follows: ** ** + Each time walTryBeginRead() is called, it is incremented. Once ** it reaches WAL_RETRY_PROTOCOL_LIMIT - indicating that walTryBeginRead() ** has many times been invoked and failed with WAL_RETRY - walTryBeginRead() ** returns SQLITE_PROTOCOL. ** ** + If SQLITE_ENABLE_SETLK_TIMEOUT is defined and walTryBeginRead() failed ** because a blocking lock timed out (SQLITE_BUSY_TIMEOUT from the OS ** layer), the WAL_RETRY_BLOCKED_MASK bit is set in "cnt". In this case ** the next invocation of walTryBeginRead() may omit an expected call to ** sqlite3OsSleep(). There has already been a delay when the previous call ** waited on a lock. */ // C documentation // // /* // ** Attempt to start a read transaction. This might fail due to a race or // ** other transient condition. When that happens, it returns WAL_RETRY to // ** indicate to the caller that it is safe to retry immediately. // ** // ** On success return SQLITE_OK. On a permanent failure (such an // ** I/O error or an SQLITE_BUSY because another process is running // ** recovery) return a positive error code. // ** // ** The useWal parameter is true to force the use of the WAL and disable // ** the case where the WAL is bypassed because it has been completely // ** checkpointed. If useWal==0 then this routine calls walIndexReadHdr() // ** to make a copy of the wal-index header into pWal->hdr. If the // ** wal-index header has changed, *pChanged is set to 1 (as an indication // ** to the caller that the local page cache is obsolete and needs to be // ** flushed.) When useWal==1, the wal-index header is assumed to already // ** be loaded and the pChanged parameter is unused. // ** // ** The caller must set the cnt parameter to the number of prior calls to // ** this routine during the current read attempt that returned WAL_RETRY. // ** This routine will start taking more aggressive measures to clear the // ** race conditions after multiple WAL_RETRY returns, and after an excessive // ** number of errors will ultimately return SQLITE_PROTOCOL. The // ** SQLITE_PROTOCOL return indicates that some other process has gone rogue // ** and is not honoring the locking protocol. There is a vanishingly small // ** chance that SQLITE_PROTOCOL could be returned because of a run of really // ** bad luck when there is lots of contention for the wal-index, but that // ** possibility is so small that it can be safely neglected, we believe. // ** // ** On success, this routine obtains a read lock on // ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is // ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1) // ** that means the Wal does not hold any read lock. The reader must not // ** access any database page that is modified by a WAL frame up to and // ** including frame number aReadMark[pWal->readLock]. The reader will // ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0 // ** Or if pWal->readLock==0, then the reader will ignore the WAL // ** completely and get all content directly from the database file. // ** If the useWal parameter is 1 then the WAL will never be ignored and // ** this routine will always set pWal->readLock>0 on success. // ** When the read transaction is completed, the caller must release the // ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1. // ** // ** This routine uses the nBackfill and aReadMark[] fields of the header // ** to select a particular WAL_READ_LOCK() that strives to let the // ** checkpoint process do as much work as possible. This routine might // ** update values of the aReadMark[] array in the header, but if it does // ** so it takes care to hold an exclusive lock on the corresponding // ** WAL_READ_LOCK() while changing values. // */ func _walTryBeginRead(tls *libc.TLS, pWal uintptr, pChanged uintptr, useWal int32, pCnt uintptr) (r int32) { var cnt, i, mxI, nDelay, rc, v1, v4, v5 int32 var mxFrame, mxReadMark, thisMark Tu32 var pInfo uintptr _, _, _, _, _, _, _, _, _, _, _, _ = cnt, i, mxFrame, mxI, mxReadMark, nDelay, pInfo, rc, thisMark, v1, v4, v5 /* Loop counter */ rc = SQLITE_OK /* Wal frame to lock to */ _ = libc.Int32FromInt32(0) /* Not currently locked */ /* useWal may only be set for read/write connections */ _ = libc.Int32FromInt32(0) /* Take steps to avoid spinning forever if there is a protocol error. ** ** Circumstances that cause a RETRY should only last for the briefest ** instances of time. No I/O or other system calls are done while the ** locks are held, so the locks should not be held for very long. But ** if we are unlucky, another process that is holding a lock might get ** paged out or take a page-fault that is time-consuming to resolve, ** during the few nanoseconds that it is holding the lock. In that case, ** it might take longer than normal for the lock to free. ** ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this ** is more of a scheduler yield than an actual delay. But on the 10th ** an subsequent retries, the delays start becoming longer and longer, ** so that on the 100th (and last) RETRY we delay for 323 milliseconds. ** The total delay time before giving up is less than 10 seconds. */ *(*int32)(unsafe.Pointer(pCnt))++ if *(*int32)(unsafe.Pointer(pCnt)) > int32(5) { nDelay = int32(1) /* Pause time in microseconds */ cnt = *(*int32)(unsafe.Pointer(pCnt)) & ^libc.Int32FromInt32(WAL_RETRY_BLOCKED_MASK) if cnt > int32(WAL_RETRY_PROTOCOL_LIMIT) { return int32(SQLITE_PROTOCOL) } if *(*int32)(unsafe.Pointer(pCnt)) >= int32(10) { nDelay = (cnt - int32(9)) * (cnt - int32(9)) * int32(39) } _sqlite3OsSleep(tls, (*TWal)(unsafe.Pointer(pWal)).FpVfs, nDelay) *(*int32)(unsafe.Pointer(pCnt)) &= ^libc.Int32FromInt32(WAL_RETRY_BLOCKED_MASK) } if !(useWal != 0) { _ = libc.Int32FromInt32(0) if int32((*TWal)(unsafe.Pointer(pWal)).FbShmUnreliable) == 0 { rc = _walIndexReadHdr(tls, pWal, pChanged) } if rc == int32(SQLITE_BUSY) { /* If there is not a recovery running in another thread or process ** then convert BUSY errors to WAL_RETRY. If recovery is known to ** be running, convert BUSY to BUSY_RECOVERY. There is a race here ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY ** would be technically correct. But the race is benign since with ** WAL_RETRY this routine will be called again and will probably be ** right on the second iteration. */ if *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData)) == uintptr(0) { /* This branch is taken when the xShmMap() method returns SQLITE_BUSY. ** We assume this is a transient condition, so return WAL_RETRY. The ** xShmMap() implementation used by the default unix and win32 VFS ** modules may return SQLITE_BUSY due to a race condition in the ** code that determines whether or not the shared-memory region ** must be zeroed before the requested page is returned. */ rc = -int32(1) } else { v1 = _walLockShared(tls, pWal, int32(WAL_RECOVER_LOCK)) rc = v1 if SQLITE_OK == v1 { _walUnlockShared(tls, pWal, int32(WAL_RECOVER_LOCK)) rc = -int32(1) } else { if rc == int32(SQLITE_BUSY) { rc = libc.Int32FromInt32(SQLITE_BUSY) | libc.Int32FromInt32(1)<hdr.mxFrame and lock that entry. */ mxReadMark = uint32(0) mxI = 0 mxFrame = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame if (*TWal)(unsafe.Pointer(pWal)).FpSnapshot != 0 && (*TWalIndexHdr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FpSnapshot)).FmxFrame < mxFrame { mxFrame = (*TWalIndexHdr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FpSnapshot)).FmxFrame } i = int32(1) for { if !(i < libc.Int32FromInt32(SQLITE_SHM_NLOCK)-libc.Int32FromInt32(3)) { break } thisMark = libc.AtomicLoadNUint32(pInfo+4+uintptr(i)*4, libc.Int32FromInt32(__ATOMIC_RELAXED)) _ = libc.Int32FromInt32(0) if mxReadMark <= thisMark && thisMark <= mxFrame { _ = libc.Int32FromInt32(0) mxReadMark = thisMark mxI = i } goto _2 _2: ; i++ } if int32((*TWal)(unsafe.Pointer(pWal)).FreadOnly)&int32(WAL_SHM_RDONLY) == 0 && (mxReadMark < mxFrame || mxI == 0) { i = int32(1) for { if !(i < libc.Int32FromInt32(SQLITE_SHM_NLOCK)-libc.Int32FromInt32(3)) { break } rc = _walLockExclusive(tls, pWal, int32(3)+i, int32(1)) if rc == SQLITE_OK { libc.AtomicStoreNUint32(pInfo+4+uintptr(i)*4, mxFrame, libc.Int32FromInt32(__ATOMIC_RELAXED)) mxReadMark = mxFrame mxI = i _walUnlockExclusive(tls, pWal, int32(3)+i, int32(1)) break } else { if rc != int32(SQLITE_BUSY) { return rc } } goto _3 _3: ; i++ } } if mxI == 0 { _ = libc.Int32FromInt32(0) if rc == int32(SQLITE_BUSY) { v4 = -int32(1) } else { v4 = libc.Int32FromInt32(SQLITE_READONLY) | libc.Int32FromInt32(5)<hdr.mxFrame may have been ** copied into the database by a checkpointer. If either of these things ** happened, then reading the database with the current value of ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry ** instead. ** ** Before checking that the live wal-index header has not changed ** since it was read, set Wal.minFrame to the first frame in the wal ** file that has not yet been checkpointed. This client will not need ** to read any frames earlier than minFrame from the wal file - they ** can be safely read directly from the database file. ** ** Because a ShmBarrier() call is made between taking the copy of ** nBackfill and checking that the wal-header in shared-memory still ** matches the one cached in pWal->hdr, it is guaranteed that the ** checkpointer that set nBackfill was not working with a wal-index ** header newer than that cached in pWal->hdr. If it were, that could ** cause a problem. The checkpointer could omit to checkpoint ** a version of page X that lies before pWal->minFrame (call that version ** A) on the basis that there is a newer version (version B) of the same ** page later in the wal file. But if version B happens to like past ** frame pWal->hdr.mxFrame - then the client would incorrectly assume ** that it can read version A from the database file. However, since ** we can guarantee that the checkpointer that set nBackfill could not ** see any pages past pWal->hdr.mxFrame, this problem does not come up. */ (*TWal)(unsafe.Pointer(pWal)).FminFrame = uint32(int32(libc.AtomicLoadNUint32(pInfo, libc.Int32FromInt32(__ATOMIC_RELAXED))) + int32(1)) _ = libc.Int32FromInt32(0) _walShmBarrier(tls, pWal) if libc.AtomicLoadNUint32(pInfo+4+uintptr(mxI)*4, libc.Int32FromInt32(__ATOMIC_RELAXED)) != mxReadMark || libc.Xmemcmp(tls, _walIndexHdr(tls, pWal), pWal+52, uint32(48)) != 0 { _walUnlockShared(tls, pWal, int32(3)+mxI) return -int32(1) } else { _ = libc.Int32FromInt32(0) (*TWal)(unsafe.Pointer(pWal)).FreadLock = int16(mxI) } return rc } // C documentation // // /* // ** This function does the work of sqlite3WalSnapshotRecover(). // */ func _walSnapshotRecover(tls *libc.TLS, pWal uintptr, pBuf1 uintptr, pBuf2 uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var i, pgno Tu32 var iDbOff, iWalOff Ti64 var pInfo uintptr var rc, szPage int32 var _ /* sLoc at bp+8 */ TWalHashLoc var _ /* szDb at bp+0 */ Ti64 _, _, _, _, _, _, _ = i, iDbOff, iWalOff, pInfo, pgno, rc, szPage szPage = int32((*TWal)(unsafe.Pointer(pWal)).FszPage) /* Size of db file in bytes */ rc = _sqlite3OsFileSize(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, bp) if rc == SQLITE_OK { pInfo = _walCkptInfo(tls, pWal) i = (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfillAttempted i = (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfillAttempted for { if !(i > libc.AtomicLoadNUint32(pInfo, libc.Int32FromInt32(__ATOMIC_RELAXED))) { break } /* Offset of wal file entry */ rc = _walHashGet(tls, pWal, _walFramePage(tls, i), bp+8) if rc != SQLITE_OK { break } _ = libc.Int32FromInt32(0) pgno = *(*Tu32)(unsafe.Pointer((*(*TWalHashLoc)(unsafe.Pointer(bp + 8))).FaPgno + uintptr(i-(*(*TWalHashLoc)(unsafe.Pointer(bp + 8))).FiZero-uint32(1))*4)) iDbOff = int64(pgno-libc.Uint32FromInt32(1)) * int64(szPage) if iDbOff+int64(szPage) <= *(*Ti64)(unsafe.Pointer(bp)) { iWalOff = int64(WAL_HDRSIZE) + int64(i-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) + int64(WAL_FRAME_HDRSIZE) rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, pBuf1, szPage, iWalOff) if rc == SQLITE_OK { rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, pBuf2, szPage, iDbOff) } if rc != SQLITE_OK || 0 == libc.Xmemcmp(tls, pBuf1, pBuf2, uint32(szPage)) { break } } (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfillAttempted = i - uint32(1) goto _1 _1: ; i-- } } return rc } // C documentation // // /* // ** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted // ** variable so that older snapshots can be accessed. To do this, loop // ** through all wal frames from nBackfillAttempted to (nBackfill+1), // ** comparing their content to the corresponding page with the database // ** file, if any. Set nBackfillAttempted to the frame number of the // ** first frame for which the wal file content matches the db file. // ** // ** This is only really safe if the file-system is such that any page // ** writes made by earlier checkpointers were atomic operations, which // ** is not always true. It is also possible that nBackfillAttempted // ** may be left set to a value larger than expected, if a wal frame // ** contains content that duplicate of an earlier version of the same // ** page. // ** // ** SQLITE_OK is returned if successful, or an SQLite error code if an // ** error occurs. It is not an error if nBackfillAttempted cannot be // ** decreased at all. // */ func _sqlite3WalSnapshotRecover(tls *libc.TLS, pWal uintptr) (r int32) { var pBuf1, pBuf2 uintptr var rc int32 _, _, _ = pBuf1, pBuf2, rc _ = libc.Int32FromInt32(0) rc = _walLockExclusive(tls, pWal, int32(WAL_CKPT_LOCK), int32(1)) if rc == SQLITE_OK { pBuf1 = Xsqlite3_malloc(tls, int32((*TWal)(unsafe.Pointer(pWal)).FszPage)) pBuf2 = Xsqlite3_malloc(tls, int32((*TWal)(unsafe.Pointer(pWal)).FszPage)) if pBuf1 == uintptr(0) || pBuf2 == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { (*TWal)(unsafe.Pointer(pWal)).FckptLock = uint8(1) rc = _walSnapshotRecover(tls, pWal, pBuf1, pBuf2) _ = libc.Int32FromInt32(0) (*TWal)(unsafe.Pointer(pWal)).FckptLock = uint8(0) } Xsqlite3_free(tls, pBuf1) Xsqlite3_free(tls, pBuf2) _walUnlockExclusive(tls, pWal, int32(WAL_CKPT_LOCK), int32(1)) } return rc } // C documentation // // /* // ** This function does the work of sqlite3WalBeginReadTransaction() (see // ** below). That function simply calls this one inside an SEH_TRY{...} block. // */ func _walBeginReadTransaction(tls *libc.TLS, pWal uintptr, pChanged uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bChanged, ckptLock, rc int32 var pInfo, pSnapshot uintptr var _ /* cnt at bp+0 */ int32 _, _, _, _, _ = bChanged, ckptLock, pInfo, pSnapshot, rc /* Return code */ *(*int32)(unsafe.Pointer(bp)) = 0 /* Number of TryBeginRead attempts */ ckptLock = 0 bChanged = 0 pSnapshot = (*TWal)(unsafe.Pointer(pWal)).FpSnapshot _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pSnapshot != 0 { if libc.Xmemcmp(tls, pSnapshot, pWal+52, uint32(48)) != 0 { bChanged = int32(1) } /* It is possible that there is a checkpointer thread running ** concurrent with this code. If this is the case, it may be that the ** checkpointer has already determined that it will checkpoint ** snapshot X, where X is later in the wal file than pSnapshot, but ** has not yet set the pInfo->nBackfillAttempted variable to indicate ** its intent. To avoid the race condition this leads to, ensure that ** there is no checkpointer process by taking a shared CKPT lock ** before checking pInfo->nBackfillAttempted. */ rc = _walLockShared(tls, pWal, int32(WAL_CKPT_LOCK)) if rc != SQLITE_OK { return rc } ckptLock = int32(1) } for cond := true; cond; cond = rc == -int32(1) { rc = _walTryBeginRead(tls, pWal, pChanged, 0, bp) } if rc == SQLITE_OK { if pSnapshot != 0 && libc.Xmemcmp(tls, pSnapshot, pWal+52, uint32(48)) != 0 { /* At this point the client has a lock on an aReadMark[] slot holding ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr ** is populated with the wal-index header corresponding to the head ** of the wal file. Verify that pSnapshot is still valid before ** continuing. Reasons why pSnapshot might no longer be valid: ** ** (1) The WAL file has been reset since the snapshot was taken. ** In this case, the salt will have changed. ** ** (2) A checkpoint as been attempted that wrote frames past ** pSnapshot->mxFrame into the database file. Note that the ** checkpoint need not have completed for this to cause problems. */ pInfo = _walCkptInfo(tls, pWal) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Check that the wal file has not been wrapped. Assuming that it has ** not, also check that no checkpointer has attempted to checkpoint any ** frames beyond pSnapshot->mxFrame. If either of these conditions are ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr ** with *pSnapshot and set *pChanged as appropriate for opening the ** snapshot. */ if !(libc.Xmemcmp(tls, pSnapshot+32, pWal+52+32, uint32(8)) != 0) && (*TWalIndexHdr)(unsafe.Pointer(pSnapshot)).FmxFrame >= (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfillAttempted { _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, pWal+52, pSnapshot, uint32(48)) *(*int32)(unsafe.Pointer(pChanged)) = bChanged } else { rc = libc.Int32FromInt32(SQLITE_ERROR) | libc.Int32FromInt32(3)<= 0 { _walUnlockShared(tls, pWal, int32(3)+int32((*TWal)(unsafe.Pointer(pWal)).FreadLock)) (*TWal)(unsafe.Pointer(pWal)).FreadLock = int16(-int32(1)) } } // C documentation // // /* // ** Search the wal file for page pgno. If found, set *piRead to the frame that // ** contains the page. Otherwise, if pgno is not in the wal file, set *piRead // ** to zero. // ** // ** Return SQLITE_OK if successful, or an error code if an error occurs. If an // ** error does occur, the final value of *piRead is undefined. // */ func _walFindFrame(tls *libc.TLS, pWal uintptr, pgno TPgno, piRead uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iFrame, iH, iLast, iRead, v2 Tu32 var iHash, iKey, iMinHash, nCollide, rc, v3 int32 var _ /* sLoc at bp+0 */ TWalHashLoc _, _, _, _, _, _, _, _, _, _, _ = iFrame, iH, iHash, iKey, iLast, iMinHash, iRead, nCollide, rc, v2, v3 iRead = uint32(0) /* If !=0, WAL frame to return data from */ iLast = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame /* This routine is only be called from within a read transaction. */ _ = libc.Int32FromInt32(0) /* If the "last page" field of the wal-index header snapshot is 0, then ** no data will be read from the wal under any circumstances. Return early ** in this case as an optimization. Likewise, if pWal->readLock==0, ** then the WAL is ignored by the reader so return early, as if the ** WAL were empty. */ if iLast == uint32(0) || int32((*TWal)(unsafe.Pointer(pWal)).FreadLock) == 0 && int32((*TWal)(unsafe.Pointer(pWal)).FbShmUnreliable) == 0 { *(*Tu32)(unsafe.Pointer(piRead)) = uint32(0) return SQLITE_OK } /* Search the hash table or tables for an entry matching page number ** pgno. Each iteration of the following for() loop searches one ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames). ** ** This code might run concurrently to the code in walIndexAppend() ** that adds entries to the wal-index (and possibly to this hash ** table). This means the value just read from the hash ** slot (aHash[iKey]) may have been added before or after the ** current read transaction was opened. Values added after the ** read transaction was opened may have been written incorrectly - ** i.e. these slots may contain garbage data. However, we assume ** that any slots written before the current read transaction was ** opened remain unmodified. ** ** For the reasons above, the if(...) condition featured in the inner ** loop of the following block is more stringent that would be required ** if we had exclusive access to the hash-table: ** ** (aPgno[iFrame]==pgno): ** This condition filters out normal hash-table collisions. ** ** (iFrame<=iLast): ** This condition filters out entries that were added to the hash ** table after the current read-transaction had started. */ iMinHash = _walFramePage(tls, (*TWal)(unsafe.Pointer(pWal)).FminFrame) iHash = _walFramePage(tls, iLast) for { if !(iHash >= iMinHash) { break } rc = _walHashGet(tls, pWal, iHash, bp) if rc != SQLITE_OK { return rc } nCollide = libc.Int32FromInt32(HASHTABLE_NPAGE) * libc.Int32FromInt32(2) iKey = _walHash(tls, pgno) _ = libc.Int32FromInt32(0) for { v2 = uint32(libc.AtomicLoadNUint16((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaHash+uintptr(iKey)*2, libc.Int32FromInt32(__ATOMIC_RELAXED))) iH = v2 if !(v2 != uint32(0)) { break } iFrame = iH + (*(*TWalHashLoc)(unsafe.Pointer(bp))).FiZero if iFrame <= iLast && iFrame >= (*TWal)(unsafe.Pointer(pWal)).FminFrame && *(*Tu32)(unsafe.Pointer((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno + uintptr(iH-uint32(1))*4)) == pgno { _ = libc.Int32FromInt32(0) iRead = iFrame } v3 = nCollide nCollide-- if v3 == 0 { *(*Tu32)(unsafe.Pointer(piRead)) = uint32(0) return _sqlite3CorruptError(tls, int32(68129)) } iKey = _walNextHash(tls, iKey) } if iRead != 0 { break } goto _1 _1: ; iHash-- } *(*Tu32)(unsafe.Pointer(piRead)) = iRead return SQLITE_OK } // C documentation // // /* // ** Search the wal file for page pgno. If found, set *piRead to the frame that // ** contains the page. Otherwise, if pgno is not in the wal file, set *piRead // ** to zero. // ** // ** Return SQLITE_OK if successful, or an error code if an error occurs. If an // ** error does occur, the final value of *piRead is undefined. // ** // ** The difference between this function and walFindFrame() is that this // ** function wraps walFindFrame() in an SEH_TRY{...} block. // */ func _sqlite3WalFindFrame(tls *libc.TLS, pWal uintptr, pgno TPgno, piRead uintptr) (r int32) { var rc int32 _ = rc rc = _walFindFrame(tls, pWal, pgno, piRead) _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** Read the contents of frame iRead from the wal file into buffer pOut // ** (which is nOut bytes in size). Return SQLITE_OK if successful, or an // ** error code otherwise. // */ func _sqlite3WalReadFrame(tls *libc.TLS, pWal uintptr, iRead Tu32, nOut int32, pOut uintptr) (r int32) { var iOffset Ti64 var sz, v1 int32 _, _, _ = iOffset, sz, v1 sz = int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FszPage) sz = sz&int32(0xfe00) + sz&int32(0x0001)< sz { v1 = sz } else { v1 = nOut } return _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, pOut, v1, iOffset) } // C documentation // // /* // ** Return the size of the database in pages (or zero, if unknown). // */ func _sqlite3WalDbsize(tls *libc.TLS, pWal uintptr) (r TPgno) { if pWal != 0 && int32((*TWal)(unsafe.Pointer(pWal)).FreadLock) >= 0 { return (*TWal)(unsafe.Pointer(pWal)).Fhdr.FnPage } return uint32(0) } // C documentation // // /* // ** This function starts a write transaction on the WAL. // ** // ** A read transaction must have already been started by a prior call // ** to sqlite3WalBeginReadTransaction(). // ** // ** If another thread or process has written into the database since // ** the read transaction was started, then it is not possible for this // ** thread to write as doing so would cause a fork. So this routine // ** returns SQLITE_BUSY in that case and no write transaction is started. // ** // ** There can only be a single writer active at a time. // */ func _sqlite3WalBeginWriteTransaction(tls *libc.TLS, pWal uintptr) (r int32) { var rc int32 _ = rc /* Cannot start a write transaction without first holding a read ** transaction. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TWal)(unsafe.Pointer(pWal)).FreadOnly != 0 { return int32(SQLITE_READONLY) } /* Only one writer allowed at a time. Get the write lock. Return ** SQLITE_BUSY if unable. */ rc = _walLockExclusive(tls, pWal, WAL_WRITE_LOCK, int32(1)) if rc != 0 { return rc } (*TWal)(unsafe.Pointer(pWal)).FwriteLock = uint8(1) /* If another connection has written to the database file since the ** time the read transaction on this connection was started, then ** the write is disallowed. */ if libc.Xmemcmp(tls, pWal+52, _walIndexHdr(tls, pWal), uint32(48)) != 0 { rc = libc.Int32FromInt32(SQLITE_BUSY) | libc.Int32FromInt32(2)<hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left // ** unchanged. // ** // ** SQLITE_OK is returned if no error is encountered (regardless of whether // ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned // ** if an error occurs. // */ func _walRestartLog(tls *libc.TLS, pWal uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pInfo uintptr var rc int32 var _ /* cnt at bp+0 */ int32 var _ /* notUsed at bp+8 */ int32 var _ /* salt1 at bp+4 */ Tu32 _, _ = pInfo, rc rc = SQLITE_OK if int32((*TWal)(unsafe.Pointer(pWal)).FreadLock) == 0 { pInfo = _walCkptInfo(tls, pWal) _ = libc.Int32FromInt32(0) if (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfill > uint32(0) { Xsqlite3_randomness(tls, int32(4), bp+4) rc = _walLockExclusive(tls, pWal, libc.Int32FromInt32(3)+libc.Int32FromInt32(1), libc.Int32FromInt32(SQLITE_SHM_NLOCK)-libc.Int32FromInt32(3)-libc.Int32FromInt32(1)) if rc == SQLITE_OK { /* If all readers are using WAL_READ_LOCK(0) (in other words if no ** readers are currently using the WAL), then the transactions ** frames will overwrite the start of the existing log. Update the ** wal-index header to reflect this. ** ** In theory it would be Ok to update the cache of the header only ** at this point. But updating the actual wal-index header is also ** safe and means there is no special case for sqlite3WalUndo() ** to handle if this transaction is rolled back. */ _walRestartHdr(tls, pWal, *(*Tu32)(unsafe.Pointer(bp + 4))) _walUnlockExclusive(tls, pWal, libc.Int32FromInt32(3)+libc.Int32FromInt32(1), libc.Int32FromInt32(SQLITE_SHM_NLOCK)-libc.Int32FromInt32(3)-libc.Int32FromInt32(1)) } else { if rc != int32(SQLITE_BUSY) { return rc } } } _walUnlockShared(tls, pWal, libc.Int32FromInt32(3)+libc.Int32FromInt32(0)) (*TWal)(unsafe.Pointer(pWal)).FreadLock = int16(-int32(1)) *(*int32)(unsafe.Pointer(bp)) = 0 for cond := true; cond; cond = rc == -int32(1) { rc = _walTryBeginRead(tls, pWal, bp+8, int32(1), bp) } _ = libc.Int32FromInt32(0) /* BUSY not possible when useWal==1 */ } return rc } // C documentation // // /* // ** Information about the current state of the WAL file and where // ** the next fsync should occur - passed from sqlite3WalFrames() into // ** walWriteToLog(). // */ type TWalWriter = struct { F__ccgo_align [0]uint32 FpWal uintptr FpFd uintptr FiSyncPoint Tsqlite3_int64 FsyncFlags int32 FszPage int32 } type WalWriter = TWalWriter // C documentation // // /* // ** Write iAmt bytes of content into the WAL file beginning at iOffset. // ** Do a sync when crossing the p->iSyncPoint boundary. // ** // ** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt, // ** first write the part before iSyncPoint, then sync, then write the // ** rest. // */ func _walWriteToLog(tls *libc.TLS, p uintptr, pContent uintptr, iAmt int32, iOffset Tsqlite3_int64) (r int32) { var iFirstAmt, rc int32 _, _ = iFirstAmt, rc if iOffset < (*TWalWriter)(unsafe.Pointer(p)).FiSyncPoint && iOffset+int64(iAmt) >= (*TWalWriter)(unsafe.Pointer(p)).FiSyncPoint { iFirstAmt = int32((*TWalWriter)(unsafe.Pointer(p)).FiSyncPoint - iOffset) rc = _sqlite3OsWrite(tls, (*TWalWriter)(unsafe.Pointer(p)).FpFd, pContent, iFirstAmt, iOffset) if rc != 0 { return rc } iOffset += int64(iFirstAmt) iAmt -= iFirstAmt pContent = uintptr(iFirstAmt) + pContent _ = libc.Int32FromInt32(0) rc = _sqlite3OsSync(tls, (*TWalWriter)(unsafe.Pointer(p)).FpFd, (*TWalWriter)(unsafe.Pointer(p)).FsyncFlags&int32(0x03)) if iAmt == 0 || rc != 0 { return rc } } rc = _sqlite3OsWrite(tls, (*TWalWriter)(unsafe.Pointer(p)).FpFd, pContent, iAmt, iOffset) return rc } // C documentation // // /* // ** Write out a single frame of the WAL // */ func _walWriteOneFrame(tls *libc.TLS, p uintptr, pPage uintptr, nTruncate int32, iOffset Tsqlite3_int64) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var pData uintptr var rc int32 var _ /* aFrame at bp+0 */ [24]Tu8 _, _ = pData, rc /* Buffer to assemble frame-header in */ pData = (*TPgHdr)(unsafe.Pointer(pPage)).FpData _walEncodeFrame(tls, (*TWalWriter)(unsafe.Pointer(p)).FpWal, (*TPgHdr)(unsafe.Pointer(pPage)).Fpgno, uint32(nTruncate), pData, bp) rc = _walWriteToLog(tls, p, bp, int32(24), iOffset) if rc != 0 { return rc } /* Write the page data */ rc = _walWriteToLog(tls, p, pData, (*TWalWriter)(unsafe.Pointer(p)).FszPage, iOffset+int64(24)) return rc } // C documentation // // /* // ** This function is called as part of committing a transaction within which // ** one or more frames have been overwritten. It updates the checksums for // ** all frames written to the wal file by the current transaction starting // ** with the earliest to have been overwritten. // ** // ** SQLITE_OK is returned if successful, or an SQLite error code otherwise. // */ func _walRewriteChecksums(tls *libc.TLS, pWal uintptr, iLast Tu32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var aBuf uintptr var iCksumOff, iOff Ti64 var iPgno, iRead, nDbSize Tu32 var rc, szPage int32 var _ /* aFrame at bp+0 */ [24]Tu8 _, _, _, _, _, _, _, _ = aBuf, iCksumOff, iOff, iPgno, iRead, nDbSize, rc, szPage szPage = int32((*TWal)(unsafe.Pointer(pWal)).FszPage) /* Database page size */ rc = SQLITE_OK aBuf = Xsqlite3_malloc(tls, szPage+int32(WAL_FRAME_HDRSIZE)) if aBuf == uintptr(0) { return int32(SQLITE_NOMEM) } /* Find the checksum values to use as input for the recalculating the ** first checksum. If the first frame is frame 1 (implying that the current ** transaction restarted the wal file), these values must be read from the ** wal-file header. Otherwise, read them from the frame header of the ** previous frame. */ _ = libc.Int32FromInt32(0) if (*TWal)(unsafe.Pointer(pWal)).FiReCksum == uint32(1) { iCksumOff = int64(24) } else { iCksumOff = int64(WAL_HDRSIZE) + int64((*TWal)(unsafe.Pointer(pWal)).FiReCksum-libc.Uint32FromInt32(1)-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) + int64(16) } rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, aBuf, int32(libc.Uint32FromInt64(4)*libc.Uint32FromInt32(2)), iCksumOff) *(*Tu32)(unsafe.Pointer(pWal + 52 + 24)) = _sqlite3Get4byte(tls, aBuf) *(*Tu32)(unsafe.Pointer(pWal + 52 + 24 + 1*4)) = _sqlite3Get4byte(tls, aBuf+uintptr(4)) iRead = (*TWal)(unsafe.Pointer(pWal)).FiReCksum (*TWal)(unsafe.Pointer(pWal)).FiReCksum = uint32(0) for { if !(rc == SQLITE_OK && iRead <= iLast) { break } iOff = libc.Int64FromInt32(WAL_HDRSIZE) + int64(iRead-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, aBuf, szPage+int32(WAL_FRAME_HDRSIZE), iOff) if rc == SQLITE_OK { iPgno = _sqlite3Get4byte(tls, aBuf) nDbSize = _sqlite3Get4byte(tls, aBuf+4) _walEncodeFrame(tls, pWal, iPgno, nDbSize, aBuf+24, bp) rc = _sqlite3OsWrite(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, bp, int32(24), iOff) } goto _1 _1: ; iRead++ } Xsqlite3_free(tls, aBuf) return rc } // C documentation // // /* // ** Write a set of frames to the log. The caller must hold the write-lock // ** on the log file (obtained using sqlite3WalBeginWriteTransaction()). // */ func _walFrames(tls *libc.TLS, pWal uintptr, szPage int32, pList uintptr, nTruncate TPgno, isCommit int32, sync_flags int32) (r int32) { bp := tls.Alloc(80) defer tls.Free(80) var bSync, nDbSize, nExtra, rc, sectorSize, szFrame, v1 int32 var iFirst, iFrame Tu32 var iOff, iOffset, sz Ti64 var p, pData, pLast, pLive, p3, p5 uintptr var v4 uint32 var _ /* aCksum at bp+56 */ [2]Tu32 var _ /* aWalHdr at bp+24 */ [32]Tu8 var _ /* iWrite at bp+64 */ Tu32 var _ /* w at bp+0 */ TWalWriter _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bSync, iFirst, iFrame, iOff, iOffset, nDbSize, nExtra, p, pData, pLast, pLive, rc, sectorSize, sz, szFrame, v1, v4, p3, p5 /* Iterator to run through pList with. */ pLast = uintptr(0) /* Last frame in list */ nExtra = 0 /* The writer */ iFirst = uint32(0) /* Pointer to shared header */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If this frame set completes a transaction, then nTruncate>0. If ** nTruncate==0 then this frame set does not complete the transaction. */ _ = libc.Int32FromInt32(0) pLive = _walIndexHdr(tls, pWal) if libc.Xmemcmp(tls, pWal+52, pLive, uint32(48)) != 0 { iFirst = (*TWalIndexHdr)(unsafe.Pointer(pLive)).FmxFrame + uint32(1) } /* See if it is possible to write these frames into the start of the ** log file, instead of appending to it at pWal->hdr.mxFrame. */ v1 = _walRestartLog(tls, pWal) rc = v1 if SQLITE_OK != v1 { return rc } /* If this is the first frame written into the log, write the WAL ** header to the start of the WAL file. See comments at the top of ** this source file for a description of the WAL header format. */ iFrame = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame if iFrame == uint32(0) { /* Checksum for wal-header */ _sqlite3Put4byte(tls, bp+24, uint32(libc.Int32FromInt32(WAL_MAGIC)|libc.Int32FromInt32(SQLITE_BIGENDIAN))) _sqlite3Put4byte(tls, bp+24+4, uint32(WAL_MAX_VERSION)) _sqlite3Put4byte(tls, bp+24+8, uint32(szPage)) _sqlite3Put4byte(tls, bp+24+12, (*TWal)(unsafe.Pointer(pWal)).FnCkpt) if (*TWal)(unsafe.Pointer(pWal)).FnCkpt == uint32(0) { Xsqlite3_randomness(tls, int32(8), pWal+52+32) } libc.Xmemcpy(tls, bp+24+16, pWal+52+32, uint32(8)) _walChecksumBytes(tls, int32(1), bp+24, libc.Int32FromInt32(WAL_HDRSIZE)-libc.Int32FromInt32(2)*libc.Int32FromInt32(4), uintptr(0), bp+56) _sqlite3Put4byte(tls, bp+24+24, (*(*[2]Tu32)(unsafe.Pointer(bp + 56)))[0]) _sqlite3Put4byte(tls, bp+24+28, (*(*[2]Tu32)(unsafe.Pointer(bp + 56)))[int32(1)]) (*TWal)(unsafe.Pointer(pWal)).FszPage = uint32(szPage) (*TWal)(unsafe.Pointer(pWal)).Fhdr.FbigEndCksum = uint8(SQLITE_BIGENDIAN) *(*Tu32)(unsafe.Pointer(pWal + 52 + 24)) = (*(*[2]Tu32)(unsafe.Pointer(bp + 56)))[0] *(*Tu32)(unsafe.Pointer(pWal + 52 + 24 + 1*4)) = (*(*[2]Tu32)(unsafe.Pointer(bp + 56)))[int32(1)] (*TWal)(unsafe.Pointer(pWal)).FtruncateOnCommit = uint8(1) rc = _sqlite3OsWrite(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, bp+24, int32(32), 0) if rc != SQLITE_OK { return rc } /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise ** an out-of-order write following a WAL restart could result in ** database corruption. See the ticket: ** ** https://sqlite.org/src/info/ff5be73dee */ if (*TWal)(unsafe.Pointer(pWal)).FsyncHeader != 0 { rc = _sqlite3OsSync(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, sync_flags>>int32(2)&int32(0x03)) if rc != 0 { return rc } } } if int32((*TWal)(unsafe.Pointer(pWal)).FszPage) != szPage { return _sqlite3CorruptError(tls, int32(68652)) /* TH3 test case: cov1/corrupt155.test */ } /* Setup information needed to write frames into the WAL */ (*(*TWalWriter)(unsafe.Pointer(bp))).FpWal = pWal (*(*TWalWriter)(unsafe.Pointer(bp))).FpFd = (*TWal)(unsafe.Pointer(pWal)).FpWalFd (*(*TWalWriter)(unsafe.Pointer(bp))).FiSyncPoint = 0 (*(*TWalWriter)(unsafe.Pointer(bp))).FsyncFlags = sync_flags (*(*TWalWriter)(unsafe.Pointer(bp))).FszPage = szPage iOffset = libc.Int64FromInt32(WAL_HDRSIZE) + int64(iFrame+libc.Uint32FromInt32(1)-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) szFrame = szPage + int32(WAL_FRAME_HDRSIZE) /* Write all frames into the log file exactly once */ p = pList for { if !(p != 0) { break } /* 0 normally. Positive == commit flag */ /* Check if this page has already been written into the wal file by ** the current transaction. If so, overwrite the existing frame and ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that ** checksums must be recomputed when the transaction is committed. */ if iFirst != 0 && ((*TPgHdr)(unsafe.Pointer(p)).FpDirty != 0 || isCommit == 0) { *(*Tu32)(unsafe.Pointer(bp + 64)) = uint32(0) _walFindFrame(tls, pWal, (*TPgHdr)(unsafe.Pointer(p)).Fpgno, bp+64) _ = libc.Int32FromInt32(0) if *(*Tu32)(unsafe.Pointer(bp + 64)) >= iFirst { iOff = int64(WAL_HDRSIZE) + int64(*(*Tu32)(unsafe.Pointer(bp + 64))-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) + int64(WAL_FRAME_HDRSIZE) if (*TWal)(unsafe.Pointer(pWal)).FiReCksum == uint32(0) || *(*Tu32)(unsafe.Pointer(bp + 64)) < (*TWal)(unsafe.Pointer(pWal)).FiReCksum { (*TWal)(unsafe.Pointer(pWal)).FiReCksum = *(*Tu32)(unsafe.Pointer(bp + 64)) } pData = (*TPgHdr)(unsafe.Pointer(p)).FpData rc = _sqlite3OsWrite(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, pData, szPage, iOff) if rc != 0 { return rc } p3 = p + 28 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) & ^libc.Int32FromInt32(PGHDR_WAL_APPEND)) goto _2 } } iFrame++ _ = libc.Int32FromInt32(0) if isCommit != 0 && (*TPgHdr)(unsafe.Pointer(p)).FpDirty == uintptr(0) { v4 = nTruncate } else { v4 = uint32(0) } nDbSize = int32(v4) rc = _walWriteOneFrame(tls, bp, p, nDbSize, iOffset) if rc != 0 { return rc } pLast = p iOffset += int64(szFrame) p5 = p + 28 *(*Tu16)(unsafe.Pointer(p5)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p5))) | libc.Int32FromInt32(PGHDR_WAL_APPEND)) goto _2 _2: ; p = (*TPgHdr)(unsafe.Pointer(p)).FpDirty } /* Recalculate checksums within the wal file if required. */ if isCommit != 0 && (*TWal)(unsafe.Pointer(pWal)).FiReCksum != 0 { rc = _walRewriteChecksums(tls, pWal, iFrame) if rc != 0 { return rc } } /* If this is the end of a transaction, then we might need to pad ** the transaction and/or sync the WAL file. ** ** Padding and syncing only occur if this set of frames complete a ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL ** or synchronous==OFF, then no padding or syncing are needed. ** ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not ** needed and only the sync is done. If padding is needed, then the ** final frame is repeated (with its commit mark) until the next sector ** boundary is crossed. Only the part of the WAL prior to the last ** sector boundary is synced; the part of the last frame that extends ** past the sector boundary is written after the sync. */ if isCommit != 0 && sync_flags&int32(0x03) != 0 { bSync = int32(1) if (*TWal)(unsafe.Pointer(pWal)).FpadToSectorBoundary != 0 { sectorSize = _sqlite3SectorSize(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd) (*(*TWalWriter)(unsafe.Pointer(bp))).FiSyncPoint = (iOffset + int64(sectorSize) - int64(1)) / int64(sectorSize) * int64(sectorSize) bSync = libc.BoolInt32((*(*TWalWriter)(unsafe.Pointer(bp))).FiSyncPoint == iOffset) for iOffset < (*(*TWalWriter)(unsafe.Pointer(bp))).FiSyncPoint { rc = _walWriteOneFrame(tls, bp, pLast, int32(nTruncate), iOffset) if rc != 0 { return rc } iOffset += int64(szFrame) nExtra++ _ = libc.Int32FromInt32(0) } } if bSync != 0 { _ = libc.Int32FromInt32(0) rc = _sqlite3OsSync(tls, (*(*TWalWriter)(unsafe.Pointer(bp))).FpFd, sync_flags&int32(0x03)) } } /* If this frame set completes the first transaction in the WAL and ** if PRAGMA journal_size_limit is set, then truncate the WAL to the ** journal size limit, if possible. */ if isCommit != 0 && (*TWal)(unsafe.Pointer(pWal)).FtruncateOnCommit != 0 && (*TWal)(unsafe.Pointer(pWal)).FmxWalSize >= 0 { sz = (*TWal)(unsafe.Pointer(pWal)).FmxWalSize if int64(WAL_HDRSIZE)+int64(iFrame+uint32(nExtra)+libc.Uint32FromInt32(1)-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) > (*TWal)(unsafe.Pointer(pWal)).FmxWalSize { sz = libc.Int64FromInt32(WAL_HDRSIZE) + int64(iFrame+uint32(nExtra)+libc.Uint32FromInt32(1)-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) } _walLimitSize(tls, pWal, sz) (*TWal)(unsafe.Pointer(pWal)).FtruncateOnCommit = uint8(0) } /* Append data to the wal-index. It is not necessary to lock the ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index ** guarantees that there are no other writers, and no data that may ** be in use by existing readers is being overwritten. */ iFrame = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame p = pList for { if !(p != 0 && rc == SQLITE_OK) { break } if int32((*TPgHdr)(unsafe.Pointer(p)).Fflags)&int32(PGHDR_WAL_APPEND) == 0 { goto _6 } iFrame++ rc = _walIndexAppend(tls, pWal, iFrame, (*TPgHdr)(unsafe.Pointer(p)).Fpgno) goto _6 _6: ; p = (*TPgHdr)(unsafe.Pointer(p)).FpDirty } _ = libc.Int32FromInt32(0) for rc == SQLITE_OK && nExtra > 0 { iFrame++ nExtra-- rc = _walIndexAppend(tls, pWal, iFrame, (*TPgHdr)(unsafe.Pointer(pLast)).Fpgno) } if rc == SQLITE_OK { /* Update the private copy of the header. */ (*TWal)(unsafe.Pointer(pWal)).Fhdr.FszPage = uint16(szPage&libc.Int32FromInt32(0xff00) | szPage>>libc.Int32FromInt32(16)) (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame = iFrame if isCommit != 0 { (*TWal)(unsafe.Pointer(pWal)).Fhdr.FiChange++ (*TWal)(unsafe.Pointer(pWal)).Fhdr.FnPage = nTruncate } /* If this is a commit, update the wal-index header too. */ if isCommit != 0 { _walIndexWriteHdr(tls, pWal) (*TWal)(unsafe.Pointer(pWal)).FiCallback = iFrame } } return rc } // C documentation // // /* // ** Write a set of frames to the log. The caller must hold the write-lock // ** on the log file (obtained using sqlite3WalBeginWriteTransaction()). // ** // ** The difference between this function and walFrames() is that this // ** function wraps walFrames() in an SEH_TRY{...} block. // */ func _sqlite3WalFrames(tls *libc.TLS, pWal uintptr, szPage int32, pList uintptr, nTruncate TPgno, isCommit int32, sync_flags int32) (r int32) { var rc int32 _ = rc rc = _walFrames(tls, pWal, szPage, pList, nTruncate, isCommit, sync_flags) _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** This routine is called to implement sqlite3_wal_checkpoint() and // ** related interfaces. // ** // ** Obtain a CHECKPOINT lock and then backfill as much information as // ** we can from WAL into the database. // ** // ** If parameter xBusy is not NULL, it is a pointer to a busy-handler // ** callback. In this case this function runs a blocking checkpoint. // */ func _sqlite3WalCheckpoint(tls *libc.TLS, pWal uintptr, db uintptr, eMode int32, xBusy uintptr, pBusyArg uintptr, sync_flags int32, nBuf int32, zBuf uintptr, pnLog uintptr, pnCkpt uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var eMode2, rc, v1 int32 var xBusy2 uintptr var _ /* isChanged at bp+0 */ int32 _, _, _, _ = eMode2, rc, xBusy2, v1 /* Return code */ *(*int32)(unsafe.Pointer(bp)) = 0 /* True if a new wal-index header is loaded */ eMode2 = eMode /* Mode to pass to walCheckpoint() */ xBusy2 = xBusy /* Busy handler for eMode2 */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked ** in the SQLITE_CHECKPOINT_PASSIVE mode. */ _ = libc.Int32FromInt32(0) if (*TWal)(unsafe.Pointer(pWal)).FreadOnly != 0 { return int32(SQLITE_READONLY) } /* Enable blocking locks, if possible. */ if xBusy2 != 0 { } /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive ** "checkpoint" lock on the database file. ** EVIDENCE-OF: R-10421-19736 If any other process is running a ** checkpoint operation at the same time, the lock cannot be obtained and ** SQLITE_BUSY is returned. ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured, ** it will not be invoked in this case. */ rc = _walLockExclusive(tls, pWal, int32(WAL_CKPT_LOCK), int32(1)) if rc == SQLITE_OK { (*TWal)(unsafe.Pointer(pWal)).FckptLock = uint8(1) /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and ** TRUNCATE modes also obtain the exclusive "writer" lock on the database ** file. ** ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained ** immediately, and a busy-handler is configured, it is invoked and the ** writer lock retried until either the busy-handler returns 0 or the ** lock is successfully obtained. */ if eMode != SQLITE_CHECKPOINT_PASSIVE { rc = _walBusyLock(tls, pWal, xBusy2, pBusyArg, WAL_WRITE_LOCK, int32(1)) if rc == SQLITE_OK { (*TWal)(unsafe.Pointer(pWal)).FwriteLock = uint8(1) } else { if rc == int32(SQLITE_BUSY) { eMode2 = SQLITE_CHECKPOINT_PASSIVE xBusy2 = uintptr(0) rc = SQLITE_OK } } } } /* Read the wal-index header. */ if rc == SQLITE_OK { /* For a passive checkpoint, do not re-enable blocking locks after ** reading the wal-index header. A passive checkpoint should not block ** or invoke the busy handler. The only lock such a checkpoint may ** attempt to obtain is a lock on a read-slot, and it should give up ** immediately and do a partial checkpoint if it cannot obtain it. */ rc = _walIndexReadHdr(tls, pWal, bp) if eMode2 != SQLITE_CHECKPOINT_PASSIVE { } if *(*int32)(unsafe.Pointer(bp)) != 0 && (*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FpDbFd)).FpMethods)).FiVersion >= int32(3) { _sqlite3OsUnfetch(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, 0, uintptr(0)) } } /* Copy data from the log to the database file. */ if rc == SQLITE_OK { if (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame != 0 && _walPagesize(tls, pWal) != nBuf { rc = _sqlite3CorruptError(tls, int32(68912)) } else { rc = _walCheckpoint(tls, pWal, db, eMode2, xBusy2, pBusyArg, sync_flags, zBuf) } /* If no error occurred, set the output variables. */ if rc == SQLITE_OK || rc == int32(SQLITE_BUSY) { if pnLog != 0 { *(*int32)(unsafe.Pointer(pnLog)) = int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame) } _ = libc.Int32FromInt32(0) if pnCkpt != 0 { *(*int32)(unsafe.Pointer(pnCkpt)) = int32((*TWalCkptInfo)(unsafe.Pointer(_walCkptInfo(tls, pWal))).FnBackfill) } } } _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp)) != 0 { /* If a new wal-index header was loaded before the checkpoint was ** performed, then the pager-cache associated with pWal is now ** out of date. So zero the cached wal-index header to ensure that ** next time the pager opens a snapshot on this database it knows that ** the cache needs to be reset. */ libc.Xmemset(tls, pWal+52, 0, uint32(48)) } /* Release the locks. */ _sqlite3WalEndWriteTransaction(tls, pWal) if (*TWal)(unsafe.Pointer(pWal)).FckptLock != 0 { _walUnlockExclusive(tls, pWal, int32(WAL_CKPT_LOCK), int32(1)) (*TWal)(unsafe.Pointer(pWal)).FckptLock = uint8(0) } if rc == SQLITE_OK && eMode != eMode2 { v1 = int32(SQLITE_BUSY) } else { v1 = rc } return v1 } // C documentation // // /* Return the value to pass to a sqlite3_wal_hook callback, the // ** number of frames in the WAL at the point of the last commit since // ** sqlite3WalCallback() was called. If no commits have occurred since // ** the last call, then return 0. // */ func _sqlite3WalCallback(tls *libc.TLS, pWal uintptr) (r int32) { var ret Tu32 _ = ret ret = uint32(0) if pWal != 0 { ret = (*TWal)(unsafe.Pointer(pWal)).FiCallback (*TWal)(unsafe.Pointer(pWal)).FiCallback = uint32(0) } return int32(ret) } // C documentation // // /* // ** This function is called to change the WAL subsystem into or out // ** of locking_mode=EXCLUSIVE. // ** // ** If op is zero, then attempt to change from locking_mode=EXCLUSIVE // ** into locking_mode=NORMAL. This means that we must acquire a lock // ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL // ** or if the acquisition of the lock fails, then return 0. If the // ** transition out of exclusive-mode is successful, return 1. This // ** operation must occur while the pager is still holding the exclusive // ** lock on the main database file. // ** // ** If op is one, then change from locking_mode=NORMAL into // ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must // ** be released. Return 1 if the transition is made and 0 if the // ** WAL is already in exclusive-locking mode - meaning that this // ** routine is a no-op. The pager must already hold the exclusive lock // ** on the main database file before invoking this operation. // ** // ** If op is negative, then do a dry-run of the op==1 case but do // ** not actually change anything. The pager uses this to see if it // ** should acquire the database exclusive lock prior to invoking // ** the op==1 case. // */ func _sqlite3WalExclusiveMode(tls *libc.TLS, pWal uintptr, op int32) (r int32) { var rc int32 _ = rc _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* pWal->readLock is usually set, but might be -1 if there was a ** prior error while attempting to acquire are read-lock. This cannot ** happen if the connection is actually in exclusive mode (as no xShmLock ** locks are taken in this case). Nor should the pager attempt to ** upgrade to exclusive-mode following such an error. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if op == 0 { if int32((*TWal)(unsafe.Pointer(pWal)).FexclusiveMode) != WAL_NORMAL_MODE { (*TWal)(unsafe.Pointer(pWal)).FexclusiveMode = uint8(WAL_NORMAL_MODE) if _walLockShared(tls, pWal, int32(3)+int32((*TWal)(unsafe.Pointer(pWal)).FreadLock)) != SQLITE_OK { (*TWal)(unsafe.Pointer(pWal)).FexclusiveMode = uint8(WAL_EXCLUSIVE_MODE) } rc = libc.BoolInt32(int32((*TWal)(unsafe.Pointer(pWal)).FexclusiveMode) == WAL_NORMAL_MODE) } else { /* Already in locking_mode=NORMAL */ rc = 0 } } else { if op > 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _walUnlockShared(tls, pWal, int32(3)+int32((*TWal)(unsafe.Pointer(pWal)).FreadLock)) (*TWal)(unsafe.Pointer(pWal)).FexclusiveMode = uint8(WAL_EXCLUSIVE_MODE) rc = int32(1) } else { rc = libc.BoolInt32(int32((*TWal)(unsafe.Pointer(pWal)).FexclusiveMode) == WAL_NORMAL_MODE) } } return rc } // C documentation // // /* // ** Return true if the argument is non-NULL and the WAL module is using // ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the // ** WAL module is using shared-memory, return false. // */ func _sqlite3WalHeapMemory(tls *libc.TLS, pWal uintptr) (r int32) { return libc.BoolInt32(pWal != 0 && int32((*TWal)(unsafe.Pointer(pWal)).FexclusiveMode) == int32(WAL_HEAPMEMORY_MODE)) } // C documentation // // /* Create a snapshot object. The content of a snapshot is opaque to // ** every other subsystem, so the WAL module can put whatever it needs // ** in the object. // */ func _sqlite3WalSnapshotGet(tls *libc.TLS, pWal uintptr, ppSnapshot uintptr) (r int32) { var pRet uintptr var rc int32 _, _ = pRet, rc rc = SQLITE_OK _ = libc.Int32FromInt32(0) if libc.Xmemcmp(tls, pWal+52+24, uintptr(unsafe.Pointer(&_aZero)), uint32(16)) == 0 { *(*uintptr)(unsafe.Pointer(ppSnapshot)) = uintptr(0) return int32(SQLITE_ERROR) } pRet = Xsqlite3_malloc(tls, int32(48)) if pRet == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { libc.Xmemcpy(tls, pRet, pWal+52, uint32(48)) *(*uintptr)(unsafe.Pointer(ppSnapshot)) = pRet } return rc } var _aZero = [4]Tu32{} // C documentation // // /* Try to open on pSnapshot when the next read-transaction starts // */ func _sqlite3WalSnapshotOpen(tls *libc.TLS, pWal uintptr, pSnapshot uintptr) { (*TWal)(unsafe.Pointer(pWal)).FpSnapshot = pSnapshot } // C documentation // // /* // ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if // ** p1 is older than p2 and zero if p1 and p2 are the same snapshot. // */ func Xsqlite3_snapshot_cmp(tls *libc.TLS, p1 uintptr, p2 uintptr) (r int32) { var pHdr1, pHdr2 uintptr _, _ = pHdr1, pHdr2 pHdr1 = p1 pHdr2 = p2 /* aSalt[0] is a copy of the value stored in the wal file header. It ** is incremented each time the wal file is restarted. */ if *(*Tu32)(unsafe.Pointer(pHdr1 + 32)) < *(*Tu32)(unsafe.Pointer(pHdr2 + 32)) { return -int32(1) } if *(*Tu32)(unsafe.Pointer(pHdr1 + 32)) > *(*Tu32)(unsafe.Pointer(pHdr2 + 32)) { return +libc.Int32FromInt32(1) } if (*TWalIndexHdr)(unsafe.Pointer(pHdr1)).FmxFrame < (*TWalIndexHdr)(unsafe.Pointer(pHdr2)).FmxFrame { return -int32(1) } if (*TWalIndexHdr)(unsafe.Pointer(pHdr1)).FmxFrame > (*TWalIndexHdr)(unsafe.Pointer(pHdr2)).FmxFrame { return +libc.Int32FromInt32(1) } return 0 } // C documentation // // /* // ** The caller currently has a read transaction open on the database. // ** This function takes a SHARED lock on the CHECKPOINTER slot and then // ** checks if the snapshot passed as the second argument is still // ** available. If so, SQLITE_OK is returned. // ** // ** If the snapshot is not available, SQLITE_ERROR is returned. Or, if // ** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error // ** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER // ** lock is released before returning. // */ func _sqlite3WalSnapshotCheck(tls *libc.TLS, pWal uintptr, pSnapshot uintptr) (r int32) { var pNew uintptr var rc int32 _, _ = pNew, rc rc = _walLockShared(tls, pWal, int32(WAL_CKPT_LOCK)) if rc == SQLITE_OK { pNew = pSnapshot if libc.Xmemcmp(tls, pNew+32, pWal+52+32, uint32(8)) != 0 || (*TWalIndexHdr)(unsafe.Pointer(pNew)).FmxFrame < (*TWalCkptInfo)(unsafe.Pointer(_walCkptInfo(tls, pWal))).FnBackfillAttempted { rc = libc.Int32FromInt32(SQLITE_ERROR) | libc.Int32FromInt32(3)<mutex. */ type TMemPage1 = struct { FisInit Tu8 FintKey Tu8 FintKeyLeaf Tu8 Fpgno TPgno Fleaf Tu8 FhdrOffset Tu8 FchildPtrSize Tu8 Fmax1bytePayload Tu8 FnOverflow Tu8 FmaxLocal Tu16 FminLocal Tu16 FcellOffset Tu16 FnFree int32 FnCell Tu16 FmaskPage Tu16 FaiOvfl [4]Tu16 FapOvfl [4]uintptr FpBt uintptr FaData uintptr FaDataEnd uintptr FaCellIdx uintptr FaDataOfst uintptr FpDbPage uintptr FxCellSize uintptr FxParseCell uintptr } type MemPage1 = TMemPage1 /* ** A linked list of the following structures is stored at BtShared.pLock. ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor ** is opened on the table with root page BtShared.iTable. Locks are removed ** from this list when a transaction is committed or rolled back, or when ** a btree handle is closed. */ type TBtLock1 = struct { FpBtree uintptr FiTable TPgno FeLock Tu8 FpNext uintptr } type BtLock1 = TBtLock1 /* Candidate values for BtLock.eLock */ /* A Btree handle ** ** A database connection contains a pointer to an instance of ** this object for every database file that it has open. This structure ** is opaque to the database connection. The database connection cannot ** see the internals of this structure and only deals with pointers to ** this structure. ** ** For some database files, the same underlying database cache might be ** shared between multiple connections. In that case, each connection ** has it own instance of this object. But each instance of this object ** points to the same BtShared object. The database cache and the ** schema associated with the database file are all contained within ** the BtShared object. ** ** All fields in this structure are accessed under sqlite3.mutex. ** The pBt pointer itself may not be changed while there exists cursors ** in the referenced BtShared that point back to this Btree since those ** cursors have to go through this Btree to find their BtShared and ** they often do so without holding sqlite3.mutex. */ type TBtree1 = struct { Fdb uintptr FpBt uintptr FinTrans Tu8 Fsharable Tu8 Flocked Tu8 FhasIncrblobCur Tu8 FwantToLock int32 FnBackup int32 FiBDataVersion Tu32 FpNext uintptr FpPrev uintptr Flock TBtLock } type Btree1 = TBtree1 /* ** Btree.inTrans may take one of the following values. ** ** If the shared-data extension is enabled, there may be multiple users ** of the Btree structure. At most one of these may open a write transaction, ** but any number may have active read transactions. ** ** These values must match SQLITE_TXN_NONE, SQLITE_TXN_READ, and ** SQLITE_TXN_WRITE */ /* ** An instance of this object represents a single database file. ** ** A single database file can be in use at the same time by two ** or more database connections. When two or more connections are ** sharing the same database file, each connection has it own ** private Btree object for the file and each of those Btrees points ** to this one BtShared object. BtShared.nRef is the number of ** connections currently sharing this database file. ** ** Fields in this structure are accessed under the BtShared.mutex ** mutex, except for nRef and pNext which are accessed under the ** global SQLITE_MUTEX_STATIC_MAIN mutex. The pPager field ** may not be modified once it is initially set as long as nRef>0. ** The pSchema field may be set once under BtShared.mutex and ** thereafter is unchanged as long as nRef>0. ** ** isPending: ** ** If a BtShared client fails to obtain a write-lock on a database ** table (because there exists one or more read-locks on the table), ** the shared-cache enters 'pending-lock' state and isPending is ** set to true. ** ** The shared-cache leaves the 'pending lock' state when either of ** the following occur: ** ** 1) The current writer (BtShared.pWriter) concludes its transaction, OR ** 2) The number of locks held by other connections drops to zero. ** ** while in the 'pending-lock' state, no connection may start a new ** transaction. ** ** This feature is included to help prevent writer-starvation. */ type TBtShared1 = struct { FpPager uintptr Fdb uintptr FpCursor uintptr FpPage1 uintptr FopenFlags Tu8 FautoVacuum Tu8 FincrVacuum Tu8 FbDoTruncate Tu8 FinTransaction Tu8 Fmax1bytePayload Tu8 FnReserveWanted Tu8 FbtsFlags Tu16 FmaxLocal Tu16 FminLocal Tu16 FmaxLeaf Tu16 FminLeaf Tu16 FpageSize Tu32 FusableSize Tu32 FnTransaction int32 FnPage Tu32 FpSchema uintptr FxFreeSchema uintptr Fmutex uintptr FpHasContent uintptr FnRef int32 FpNext uintptr FpLock uintptr FpWriter uintptr FpTmpSpace uintptr FnPreformatSize int32 } type BtShared1 = TBtShared1 /* ** Allowed values for BtShared.btsFlags */ /* ** An instance of the following structure is used to hold information ** about a cell. The parseCellPtr() function fills in this structure ** based on information extract from the raw disk page. */ type TCellInfo1 = struct { F__ccgo_align [0]uint32 FnKey Ti64 FpPayload uintptr FnPayload Tu32 FnLocal Tu16 FnSize Tu16 F__ccgo_pad5 [4]byte } type CellInfo1 = TCellInfo1 /* ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than ** this will be declared corrupt. This value is calculated based on a ** maximum database size of 2^31 pages a minimum fanout of 2 for a ** root-node and 3 for all other internal nodes. ** ** If a tree that appears to be taller than this is encountered, it is ** assumed that the database is corrupt. */ /* ** A cursor is a pointer to a particular entry within a particular ** b-tree within a database file. ** ** The entry is identified by its MemPage and the index in ** MemPage.aCell[] of the entry. ** ** A single database file can be shared by two more database connections, ** but cursors cannot be shared. Each cursor is associated with a ** particular database connection identified BtCursor.pBtree.db. ** ** Fields in this structure are accessed under the BtShared.mutex ** found at self->pBt->mutex. ** ** skipNext meaning: ** The meaning of skipNext depends on the value of eState: ** ** eState Meaning of skipNext ** VALID skipNext is meaningless and is ignored ** INVALID skipNext is meaningless and is ignored ** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and ** sqlite3BtreePrevious() is no-op if skipNext<0. ** REQUIRESEEK restoreCursorPosition() restores the cursor to ** eState=SKIPNEXT if skipNext!=0 ** FAULT skipNext holds the cursor fault error code. */ type TBtCursor1 = struct { F__ccgo_align [0]uint32 FeState Tu8 FcurFlags Tu8 FcurPagerFlags Tu8 Fhints Tu8 FskipNext int32 FpBtree uintptr FaOverflow uintptr FpKey uintptr FpBt uintptr FpNext uintptr F__ccgo_align10 [4]byte Finfo TCellInfo FnKey Ti64 FpgnoRoot TPgno FiPage Ti8 FcurIntKey Tu8 Fix Tu16 FaiIdx [19]Tu16 FpKeyInfo uintptr FpPage uintptr FapPage [19]uintptr F__ccgo_pad20 [4]byte } type BtCursor1 = TBtCursor1 /* ** Legal values for BtCursor.curFlags */ /* ** Potential values for BtCursor.eState. ** ** CURSOR_INVALID: ** Cursor does not point to a valid entry. This can happen (for example) ** because the table is empty or because BtreeCursorFirst() has not been ** called. ** ** CURSOR_VALID: ** Cursor points to a valid entry. getPayload() etc. may be called. ** ** CURSOR_SKIPNEXT: ** Cursor is valid except that the Cursor.skipNext field is non-zero ** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious() ** operation should be a no-op. ** ** CURSOR_REQUIRESEEK: ** The table that this cursor was opened on still exists, but has been ** modified since the cursor was last used. The cursor position is saved ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in ** this state, restoreCursorPosition() can be called to attempt to ** seek the cursor to the saved position. ** ** CURSOR_FAULT: ** An unrecoverable error (an I/O error or a malloc failure) has occurred ** on a different connection that shares the BtShared cache with this ** cursor. The error has left the cache in an inconsistent state. ** Do nothing else with this cursor. Any attempt to use the cursor ** should return the error code stored in BtCursor.skipNext */ /* ** The database page the PENDING_BYTE occupies. This page is never used. */ /* ** These macros define the location of the pointer-map entry for a ** database page. The first argument to each is the number of usable ** bytes on each page of the database (often 1024). The second is the ** page number to look up in the pointer map. ** ** PTRMAP_PAGENO returns the database page number of the pointer-map ** page that stores the required pointer. PTRMAP_PTROFFSET returns ** the offset of the requested map entry. ** ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page, ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements ** this test. */ /* ** The pointer map is a lookup table that identifies the parent page for ** each child page in the database file. The parent page is the page that ** contains a pointer to the child. Every page in the database contains ** 0 or 1 parent pages. (In this context 'database page' refers ** to any page that is not part of the pointer map itself.) Each pointer map ** entry consists of a single byte 'type' and a 4 byte parent page number. ** The PTRMAP_XXX identifiers below are the valid types. ** ** The purpose of the pointer map is to facility moving pages from one ** position in the file to another as part of autovacuum. When a page ** is moved, the pointer in its parent must be updated to point to the ** new location. The pointer map is used to locate the parent page quickly. ** ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not ** used in this case. ** ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number ** is not used in this case. ** ** PTRMAP_OVERFLOW1: The database page is the first page in a list of ** overflow pages. The page number identifies the page that ** contains the cell with a pointer to this overflow page. ** ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of ** overflow pages. The page-number identifies the previous ** page in the overflow page list. ** ** PTRMAP_BTREE: The database page is a non-root btree page. The page number ** identifies the parent page in the btree. */ /* A bunch of assert() statements to check the transaction state variables ** of handle p (type Btree*) are internally consistent. */ /* ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine ** if the database supports auto-vacuum or not. Because it is used ** within an expression that is an argument to another macro ** (sqliteMallocRaw), it is not possible to use conditional compilation. ** So, this macro is defined instead. */ // C documentation // // /* // ** This structure is passed around through all the PRAGMA integrity_check // ** checking routines in order to keep track of some global state information. // ** // ** The aRef[] array is allocated so that there is 1 bit for each page in // ** the database. As the integrity-check proceeds, for each page used in // ** the database the corresponding bit is set. This allows integrity-check to // ** detect pages that are used twice and orphaned pages (both of which // ** indicate corruption). // */ type TIntegrityCk = struct { FpBt uintptr FpPager uintptr FaPgRef uintptr FnCkPage TPgno FmxErr int32 FnErr int32 Frc int32 FnStep Tu32 FzPfx uintptr Fv0 TPgno Fv1 TPgno Fv2 int32 FerrMsg TStrAccum Fheap uintptr Fdb uintptr } type IntegrityCk = TIntegrityCk type TIntegrityCk1 = struct { FpBt uintptr FpPager uintptr FaPgRef uintptr FnCkPage TPgno FmxErr int32 FnErr int32 Frc int32 FnStep Tu32 FzPfx uintptr Fv0 TPgno Fv1 TPgno Fv2 int32 FerrMsg TStrAccum Fheap uintptr Fdb uintptr } type IntegrityCk1 = TIntegrityCk1 /* ** Routines to read or write a two- and four-byte big-endian integer values. */ /* ** get2byteAligned(), unlike get2byte(), requires that its argument point to a ** two-byte aligned address. get2byteAligned() is only used for accessing the ** cell addresses in a btree header. */ /************** End of btreeInt.h ********************************************/ /************** Continuing where we left off in btmutex.c ********************/ // C documentation // // /* // ** Obtain the BtShared mutex associated with B-Tree handle p. Also, // ** set BtShared.db to the database handle associated with p and the // ** p->locked boolean to true. // */ func _lockBtreeMutex(tls *libc.TLS, p uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) Xsqlite3_mutex_enter(tls, (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).Fmutex) (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).Fdb = (*TBtree)(unsafe.Pointer(p)).Fdb (*TBtree)(unsafe.Pointer(p)).Flocked = uint8(1) } // C documentation // // /* // ** Release the BtShared mutex associated with B-Tree handle p and // ** clear the p->locked boolean. // */ func _unlockBtreeMutex(tls *libc.TLS, p uintptr) { var pBt uintptr _ = pBt pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) Xsqlite3_mutex_leave(tls, (*TBtShared)(unsafe.Pointer(pBt)).Fmutex) (*TBtree)(unsafe.Pointer(p)).Flocked = uint8(0) } // C documentation // // /* // ** Enter a mutex on the given BTree object. // ** // ** If the object is not sharable, then no mutex is ever required // ** and this routine is a no-op. The underlying mutex is non-recursive. // ** But we keep a reference count in Btree.wantToLock so the behavior // ** of this interface is recursive. // ** // ** To avoid deadlocks, multiple Btrees are locked in the same order // ** by all database connections. The p->pNext is a list of other // ** Btrees belonging to the same database connection as the p Btree // ** which need to be locked after p. If we cannot get a lock on // ** p, then first unlock all of the others on p->pNext, then wait // ** for the lock to become available on p, then relock all of the // ** subsequent Btrees that desire a lock. // */ func _sqlite3BtreeEnter(tls *libc.TLS, p uintptr) { /* Some basic sanity checking on the Btree. The list of Btrees ** connected by pNext and pPrev should be in sorted order by ** Btree.pBt value. All elements of the list should belong to ** the same connection. Only shared Btrees are on the list. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Check for locking consistency */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* We should already hold a lock on the database connection */ _ = libc.Int32FromInt32(0) /* Unless the database is sharable and unlocked, then BtShared.db ** should already be set correctly. */ _ = libc.Int32FromInt32(0) if !((*TBtree)(unsafe.Pointer(p)).Fsharable != 0) { return } (*TBtree)(unsafe.Pointer(p)).FwantToLock++ if (*TBtree)(unsafe.Pointer(p)).Flocked != 0 { return } _btreeLockCarefully(tls, p) } // C documentation // // /* This is a helper function for sqlite3BtreeLock(). By moving // ** complex, but seldom used logic, out of sqlite3BtreeLock() and // ** into this routine, we avoid unnecessary stack pointer changes // ** and thus help the sqlite3BtreeLock() routine to run much faster // ** in the common case. // */ func _btreeLockCarefully(tls *libc.TLS, p uintptr) { var pLater uintptr _ = pLater /* In most cases, we should be able to acquire the lock we ** want without having to go through the ascending lock ** procedure that follows. Just be sure not to block. */ if Xsqlite3_mutex_try(tls, (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).Fmutex) == SQLITE_OK { (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).Fdb = (*TBtree)(unsafe.Pointer(p)).Fdb (*TBtree)(unsafe.Pointer(p)).Flocked = uint8(1) return } /* To avoid deadlock, first release all locks with a larger ** BtShared address. Then acquire our lock. Then reacquire ** the other BtShared locks that we used to hold in ascending ** order. */ pLater = (*TBtree)(unsafe.Pointer(p)).FpNext for { if !(pLater != 0) { break } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TBtree)(unsafe.Pointer(pLater)).Flocked != 0 { _unlockBtreeMutex(tls, pLater) } goto _1 _1: ; pLater = (*TBtree)(unsafe.Pointer(pLater)).FpNext } _lockBtreeMutex(tls, p) pLater = (*TBtree)(unsafe.Pointer(p)).FpNext for { if !(pLater != 0) { break } if (*TBtree)(unsafe.Pointer(pLater)).FwantToLock != 0 { _lockBtreeMutex(tls, pLater) } goto _2 _2: ; pLater = (*TBtree)(unsafe.Pointer(pLater)).FpNext } } // C documentation // // /* // ** Exit the recursive mutex on a Btree. // */ func _sqlite3BtreeLeave(tls *libc.TLS, p uintptr) { _ = libc.Int32FromInt32(0) if (*TBtree)(unsafe.Pointer(p)).Fsharable != 0 { _ = libc.Int32FromInt32(0) (*TBtree)(unsafe.Pointer(p)).FwantToLock-- if (*TBtree)(unsafe.Pointer(p)).FwantToLock == 0 { _unlockBtreeMutex(tls, p) } } } // C documentation // // /* // ** Enter the mutex on every Btree associated with a database // ** connection. This is needed (for example) prior to parsing // ** a statement since we will be comparing table and column names // ** against all schemas and we do not want those schemas being // ** reset out from under us. // ** // ** There is a corresponding leave-all procedures. // ** // ** Enter the mutexes in ascending order by BtShared pointer address // ** to avoid the possibility of deadlock when two threads with // ** two or more btrees in common both try to lock all their btrees // ** at the same instant. // */ func _btreeEnterAll(tls *libc.TLS, db uintptr) { var i, skipOk int32 var p uintptr _, _, _ = i, p, skipOk skipOk = int32(1) _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } p = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt if p != 0 && (*TBtree)(unsafe.Pointer(p)).Fsharable != 0 { _sqlite3BtreeEnter(tls, p) skipOk = 0 } goto _1 _1: ; i++ } (*Tsqlite3)(unsafe.Pointer(db)).FnoSharedCache = uint8(skipOk) } func _sqlite3BtreeEnterAll(tls *libc.TLS, db uintptr) { if int32((*Tsqlite3)(unsafe.Pointer(db)).FnoSharedCache) == 0 { _btreeEnterAll(tls, db) } } func _btreeLeaveAll(tls *libc.TLS, db uintptr) { var i int32 var p uintptr _, _ = i, p _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } p = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt if p != 0 { _sqlite3BtreeLeave(tls, p) } goto _1 _1: ; i++ } } func _sqlite3BtreeLeaveAll(tls *libc.TLS, db uintptr) { if int32((*Tsqlite3)(unsafe.Pointer(db)).FnoSharedCache) == 0 { _btreeLeaveAll(tls, db) } } // C documentation // // /* // ** Enter a mutex on a Btree given a cursor owned by that Btree. // ** // ** These entry points are used by incremental I/O only. Enter() is required // ** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not // ** the build is threadsafe. Leave() is only required by threadsafe builds. // */ func _sqlite3BtreeEnterCursor(tls *libc.TLS, pCur uintptr) { _sqlite3BtreeEnter(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpBtree) } func _sqlite3BtreeLeaveCursor(tls *libc.TLS, pCur uintptr) { _sqlite3BtreeLeave(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpBtree) } /************** End of btmutex.c *********************************************/ /************** Begin file btree.c *******************************************/ /* ** 2004 April 6 ** ** 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 implements an external (disk-based) database using BTrees. ** See the header comment on "btreeInt.h" for additional information. ** Including a description of file format and an overview of operation. */ /* #include "btreeInt.h" */ // C documentation // // /* // ** The header string that appears at the beginning of every // ** SQLite database. // */ var _zMagicHeader = [16]uint8{'S', 'Q', 'L', 'i', 't', 'e', ' ', 'f', 'o', 'r', 'm', 'a', 't', ' ', '3'} /* ** Set this global variable to 1 to enable tracing using the TRACE ** macro. */ /* ** Extract a 2-byte big-endian integer from an array of unsigned bytes. ** But if the value is zero, make it 65536. ** ** This routine is used to extract the "offset to cell content area" value ** from the header of a btree page. If the page size is 65536 and the page ** is empty, the offset should be 65536, but the 2-byte value stores zero. ** This routine makes the necessary adjustment to 65536. */ /* ** Values passed as the 5th argument to allocateBtreePage() */ /* ** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not ** defined, or 0 if it is. For example: ** ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum); */ // C documentation // // /* // ** A list of BtShared objects that are eligible for participation // ** in shared cache. This variable has file scope during normal builds, // ** but the test harness needs to access it so we make it global for // ** test builds. // ** // ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MAIN. // */ var _sqlite3SharedCacheList = uintptr(0) // C documentation // // /* // ** Enable or disable the shared pager and schema features. // ** // ** This routine has no effect on existing database connections. // ** The shared cache setting effects only future calls to // ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(). // */ func Xsqlite3_enable_shared_cache(tls *libc.TLS, enable int32) (r int32) { _sqlite3Config.FsharedCacheEnabled = enable return SQLITE_OK } /* ** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single ** (MemPage*) as an argument. The (MemPage*) must not be NULL. ** ** If SQLITE_DEBUG is not defined, then this macro is equivalent to ** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented ** with the page number and filename associated with the (MemPage*). */ // C documentation // // /* // ** Query to see if Btree handle p may obtain a lock of type eLock // ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return // ** SQLITE_OK if the lock may be obtained (by calling // ** setSharedCacheTableLock()), or SQLITE_LOCKED if not. // */ func _querySharedCacheTableLock(tls *libc.TLS, p uintptr, iTab TPgno, eLock Tu8) (r int32) { var pBt, pIter, p2 uintptr _, _, _ = pBt, pIter, p2 pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If requesting a write-lock, then the Btree must have an open write ** transaction on this file. And, obviously, for this to be so there ** must be an open write transaction on the file itself. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* This routine is a no-op if the shared-cache is not enabled */ if !((*TBtree)(unsafe.Pointer(p)).Fsharable != 0) { return SQLITE_OK } /* If some other connection is holding an exclusive lock, the ** requested lock may not be obtained. */ if (*TBtShared)(unsafe.Pointer(pBt)).FpWriter != p && int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_EXCLUSIVE) != 0 { _sqlite3ConnectionBlocked(tls, (*TBtree)(unsafe.Pointer(p)).Fdb, (*TBtree)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpWriter)).Fdb) return libc.Int32FromInt32(SQLITE_LOCKED) | libc.Int32FromInt32(1)<eLock!=eLock) in the following if(...) ** statement is a simplification of: ** ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK) ** ** since we know that if eLock==WRITE_LOCK, then no other connection ** may hold a WRITE_LOCK on any table in this file (since there can ** only be a single writer). */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TBtLock)(unsafe.Pointer(pIter)).FpBtree != p && (*TBtLock)(unsafe.Pointer(pIter)).FiTable == iTab && int32((*TBtLock)(unsafe.Pointer(pIter)).FeLock) != int32(eLock) { _sqlite3ConnectionBlocked(tls, (*TBtree)(unsafe.Pointer(p)).Fdb, (*TBtree)(unsafe.Pointer((*TBtLock)(unsafe.Pointer(pIter)).FpBtree)).Fdb) if int32(eLock) == int32(WRITE_LOCK) { _ = libc.Int32FromInt32(0) p2 = pBt + 24 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(BTS_PENDING)) } return libc.Int32FromInt32(SQLITE_LOCKED) | libc.Int32FromInt32(1)< int32((*TBtLock)(unsafe.Pointer(pLock)).FeLock) { (*TBtLock)(unsafe.Pointer(pLock)).FeLock = eLock } return SQLITE_OK } // C documentation // // /* // ** Release all the table locks (locks obtained via calls to // ** the setSharedCacheTableLock() procedure) held by Btree object p. // ** // ** This function assumes that Btree p has an open read or write // ** transaction. If it does not, then the BTS_PENDING flag // ** may be incorrectly cleared. // */ func _clearAllSharedCacheTableLocks(tls *libc.TLS, p uintptr) { var pBt, pLock, ppIter, p1, p2 uintptr _, _, _, _, _ = pBt, pLock, ppIter, p1, p2 pBt = (*TBtree)(unsafe.Pointer(p)).FpBt ppIter = pBt + 76 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) for *(*uintptr)(unsafe.Pointer(ppIter)) != 0 { pLock = *(*uintptr)(unsafe.Pointer(ppIter)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TBtLock)(unsafe.Pointer(pLock)).FpBtree == p { *(*uintptr)(unsafe.Pointer(ppIter)) = (*TBtLock)(unsafe.Pointer(pLock)).FpNext _ = libc.Int32FromInt32(0) if (*TBtLock)(unsafe.Pointer(pLock)).FiTable != uint32(1) { Xsqlite3_free(tls, pLock) } } else { ppIter = pLock + 12 } } _ = libc.Int32FromInt32(0) if (*TBtShared)(unsafe.Pointer(pBt)).FpWriter == p { (*TBtShared)(unsafe.Pointer(pBt)).FpWriter = uintptr(0) p1 = pBt + 24 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^(libc.Int32FromInt32(BTS_EXCLUSIVE) | libc.Int32FromInt32(BTS_PENDING))) } else { if (*TBtShared)(unsafe.Pointer(pBt)).FnTransaction == int32(2) { /* This function is called when Btree p is concluding its ** transaction. If there currently exists a writer, and p is not ** that writer, then the number of locks held by connections other ** than the writer must be about to drop to zero. In this case ** set the BTS_PENDING flag to 0. ** ** If there is not currently a writer, then BTS_PENDING must ** be zero already. So this next line is harmless in that case. */ p2 = pBt + 24 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) & ^libc.Int32FromInt32(BTS_PENDING)) } } } // C documentation // // /* // ** This function changes all write-locks held by Btree p into read-locks. // */ func _downgradeAllSharedCacheTableLocks(tls *libc.TLS, p uintptr) { var pBt, pLock, p1 uintptr _, _, _ = pBt, pLock, p1 pBt = (*TBtree)(unsafe.Pointer(p)).FpBt if (*TBtShared)(unsafe.Pointer(pBt)).FpWriter == p { (*TBtShared)(unsafe.Pointer(pBt)).FpWriter = uintptr(0) p1 = pBt + 24 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^(libc.Int32FromInt32(BTS_EXCLUSIVE) | libc.Int32FromInt32(BTS_PENDING))) pLock = (*TBtShared)(unsafe.Pointer(pBt)).FpLock for { if !(pLock != 0) { break } _ = libc.Int32FromInt32(0) (*TBtLock)(unsafe.Pointer(pLock)).FeLock = uint8(READ_LOCK) goto _2 _2: ; pLock = (*TBtLock)(unsafe.Pointer(pLock)).FpNext } } } /* Forward reference */ /* ***** This routine is used inside of assert() only **** ** ** Verify that the cursor holds the mutex on its BtShared */ /* ** Invalidate the overflow cache of the cursor passed as the first argument. ** on the shared btree structure pBt. */ // C documentation // // /* // ** Invalidate the overflow page-list cache for all cursors opened // ** on the shared btree structure pBt. // */ func _invalidateAllOverflowCache(tls *libc.TLS, pBt uintptr) { var p, p2 uintptr _, _ = p, p2 _ = libc.Int32FromInt32(0) p = (*TBtShared)(unsafe.Pointer(pBt)).FpCursor for { if !(p != 0) { break } p2 = p + 1 *(*Tu8)(unsafe.Pointer(p2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p2))) & ^libc.Int32FromInt32(BTCF_ValidOvfl)) goto _1 _1: ; p = (*TBtCursor)(unsafe.Pointer(p)).FpNext } } // C documentation // // /* // ** This function is called before modifying the contents of a table // ** to invalidate any incrblob cursors that are open on the // ** row or one of the rows being modified. // ** // ** If argument isClearTable is true, then the entire contents of the // ** table is about to be deleted. In this case invalidate all incrblob // ** cursors open on any row within the table with root-page pgnoRoot. // ** // ** Otherwise, if argument isClearTable is false, then the row with // ** rowid iRow is being replaced or deleted. In this case invalidate // ** only those incrblob cursors open on that specific row. // */ func _invalidateIncrblobCursors(tls *libc.TLS, pBtree uintptr, pgnoRoot TPgno, iRow Ti64, isClearTable int32) { var p uintptr _ = p _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TBtree)(unsafe.Pointer(pBtree)).FhasIncrblobCur = uint8(0) p = (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(pBtree)).FpBt)).FpCursor for { if !(p != 0) { break } if int32((*TBtCursor)(unsafe.Pointer(p)).FcurFlags)&int32(BTCF_Incrblob) != 0 { (*TBtree)(unsafe.Pointer(pBtree)).FhasIncrblobCur = uint8(1) if (*TBtCursor)(unsafe.Pointer(p)).FpgnoRoot == pgnoRoot && (isClearTable != 0 || (*TBtCursor)(unsafe.Pointer(p)).Finfo.FnKey == iRow) { (*TBtCursor)(unsafe.Pointer(p)).FeState = uint8(CURSOR_INVALID) } } goto _1 _1: ; p = (*TBtCursor)(unsafe.Pointer(p)).FpNext } } // C documentation // // /* // ** Set bit pgno of the BtShared.pHasContent bitvec. This is called // ** when a page that previously contained data becomes a free-list leaf // ** page. // ** // ** The BtShared.pHasContent bitvec exists to work around an obscure // ** bug caused by the interaction of two useful IO optimizations surrounding // ** free-list leaf pages: // ** // ** 1) When all data is deleted from a page and the page becomes // ** a free-list leaf page, the page is not written to the database // ** (as free-list leaf pages contain no meaningful data). Sometimes // ** such a page is not even journalled (as it will not be modified, // ** why bother journalling it?). // ** // ** 2) When a free-list leaf page is reused, its content is not read // ** from the database or written to the journal file (why should it // ** be, if it is not at all meaningful?). // ** // ** By themselves, these optimizations work fine and provide a handy // ** performance boost to bulk delete or insert operations. However, if // ** a page is moved to the free-list and then reused within the same // ** transaction, a problem comes up. If the page is not journalled when // ** it is moved to the free-list and it is also not journalled when it // ** is extracted from the free-list and reused, then the original data // ** may be lost. In the event of a rollback, it may not be possible // ** to restore the database to its original configuration. // ** // ** The solution is the BtShared.pHasContent bitvec. Whenever a page is // ** moved to become a free-list leaf page, the corresponding bit is // ** set in the bitvec. Whenever a leaf page is extracted from the free-list, // ** optimization 2 above is omitted if the corresponding bit is already // ** set in BtShared.pHasContent. The contents of the bitvec are cleared // ** at the end of every transaction. // */ func _btreeSetHasContent(tls *libc.TLS, pBt uintptr, pgno TPgno) (r int32) { var rc int32 _ = rc rc = SQLITE_OK if !((*TBtShared)(unsafe.Pointer(pBt)).FpHasContent != 0) { _ = libc.Int32FromInt32(0) (*TBtShared)(unsafe.Pointer(pBt)).FpHasContent = _sqlite3BitvecCreate(tls, (*TBtShared)(unsafe.Pointer(pBt)).FnPage) if !((*TBtShared)(unsafe.Pointer(pBt)).FpHasContent != 0) { rc = int32(SQLITE_NOMEM) } } if rc == SQLITE_OK && pgno <= _sqlite3BitvecSize(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpHasContent) { rc = _sqlite3BitvecSet(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpHasContent, pgno) } return rc } // C documentation // // /* // ** Query the BtShared.pHasContent vector. // ** // ** This function is called when a free-list leaf page is removed from the // ** free-list for reuse. It returns false if it is safe to retrieve the // ** page from the pager layer with the 'no-content' flag set. True otherwise. // */ func _btreeGetHasContent(tls *libc.TLS, pBt uintptr, pgno TPgno) (r int32) { var p uintptr _ = p p = (*TBtShared)(unsafe.Pointer(pBt)).FpHasContent return libc.BoolInt32(p != 0 && (pgno > _sqlite3BitvecSize(tls, p) || _sqlite3BitvecTestNotNull(tls, p, pgno) != 0)) } // C documentation // // /* // ** Clear (destroy) the BtShared.pHasContent bitvec. This should be // ** invoked at the conclusion of each write-transaction. // */ func _btreeClearHasContent(tls *libc.TLS, pBt uintptr) { _sqlite3BitvecDestroy(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpHasContent) (*TBtShared)(unsafe.Pointer(pBt)).FpHasContent = uintptr(0) } // C documentation // // /* // ** Release all of the apPage[] pages for a cursor. // */ func _btreeReleaseAllCursorPages(tls *libc.TLS, pCur uintptr) { var i int32 _ = i if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) >= 0 { i = 0 for { if !(i < int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)) { break } _releasePageNotNull(tls, *(*uintptr)(unsafe.Pointer(pCur + 120 + uintptr(i)*4))) goto _1 _1: ; i++ } _releasePageNotNull(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpPage) (*TBtCursor)(unsafe.Pointer(pCur)).FiPage = int8(-int32(1)) } } // C documentation // // /* // ** The cursor passed as the only argument must point to a valid entry // ** when this function is called (i.e. have eState==CURSOR_VALID). This // ** function saves the current cursor key in variables pCur->nKey and // ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error // ** code otherwise. // ** // ** If the cursor is open on an intkey table, then the integer key // ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to // ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is // ** set to point to a malloced buffer pCur->nKey bytes in size containing // ** the key. // */ func _saveCursorKey(tls *libc.TLS, pCur uintptr) (r int32) { var pKey uintptr var rc int32 _, _ = pKey, rc rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TBtCursor)(unsafe.Pointer(pCur)).FcurIntKey != 0 { /* Only the rowid is required for a table btree */ (*TBtCursor)(unsafe.Pointer(pCur)).FnKey = _sqlite3BtreeIntegerKey(tls, pCur) } else { (*TBtCursor)(unsafe.Pointer(pCur)).FnKey = int64(_sqlite3BtreePayloadSize(tls, pCur)) pKey = _sqlite3Malloc(tls, uint64((*TBtCursor)(unsafe.Pointer(pCur)).FnKey+int64(9)+int64(8))) if pKey != 0 { rc = _sqlite3BtreePayload(tls, pCur, uint32(0), uint32(int32((*TBtCursor)(unsafe.Pointer(pCur)).FnKey)), pKey) if rc == SQLITE_OK { libc.Xmemset(tls, pKey+uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FnKey), 0, uint32(libc.Int32FromInt32(9)+libc.Int32FromInt32(8))) (*TBtCursor)(unsafe.Pointer(pCur)).FpKey = pKey } else { Xsqlite3_free(tls, pKey) } } else { rc = int32(SQLITE_NOMEM) } } _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** Save the current cursor position in the variables BtCursor.nKey // ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. // ** // ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID) // ** prior to calling this routine. // */ func _saveCursorPosition(tls *libc.TLS, pCur uintptr) (r int32) { var rc int32 var p1 uintptr _, _ = rc, p1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags)&int32(BTCF_Pinned) != 0 { return libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(11)< int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnAllField) { rc = _sqlite3CorruptError(tls, int32(71032)) } else { rc = _sqlite3BtreeIndexMoveto(tls, pCur, pIdxKey, pRes) } _sqlite3DbFree(tls, (*TKeyInfo1)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpKeyInfo)).Fdb, pIdxKey) } else { pIdxKey = uintptr(0) rc = _sqlite3BtreeTableMoveto(tls, pCur, nKey, bias, pRes) } return rc } // C documentation // // /* // ** Restore the cursor to the position it was in (or as close to as possible) // ** when saveCursorPosition() was called. Note that this call deletes the // ** saved position info stored by saveCursorPosition(), so there can be // ** at most one effective restoreCursorPosition() call after each // ** saveCursorPosition(). // */ func _btreeRestoreCursorPosition(tls *libc.TLS, pCur uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* skipNext at bp+0 */ int32 _ = rc *(*int32)(unsafe.Pointer(bp)) = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) == int32(CURSOR_FAULT) { return (*TBtCursor)(unsafe.Pointer(pCur)).FskipNext } (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_INVALID) if _sqlite3FaultSim(tls, int32(410)) != 0 { rc = int32(SQLITE_IOERR) } else { rc = _btreeMoveto(tls, pCur, (*TBtCursor)(unsafe.Pointer(pCur)).FpKey, (*TBtCursor)(unsafe.Pointer(pCur)).FnKey, 0, bp) } if rc == SQLITE_OK { Xsqlite3_free(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpKey) (*TBtCursor)(unsafe.Pointer(pCur)).FpKey = uintptr(0) _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp)) != 0 { (*TBtCursor)(unsafe.Pointer(pCur)).FskipNext = *(*int32)(unsafe.Pointer(bp)) } if (*TBtCursor)(unsafe.Pointer(pCur)).FskipNext != 0 && int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) == CURSOR_VALID { (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_SKIPNEXT) } } return rc } // C documentation // // /* // ** Determine whether or not a cursor has moved from the position where // ** it was last placed, or has been invalidated for any other reason. // ** Cursors can move when the row they are pointing at is deleted out // ** from under them, for example. Cursor might also move if a btree // ** is rebalanced. // ** // ** Calling this routine with a NULL cursor pointer returns false. // ** // ** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor // ** back to where it ought to be if this routine returns true. // */ func _sqlite3BtreeCursorHasMoved(tls *libc.TLS, pCur uintptr) (r int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return libc.BoolInt32(CURSOR_VALID != int32(*(*Tu8)(unsafe.Pointer(pCur)))) } // C documentation // // /* // ** Return a pointer to a fake BtCursor object that will always answer // ** false to the sqlite3BtreeCursorHasMoved() routine above. The fake // ** cursor returned must not be used with any other Btree interface. // */ func _sqlite3BtreeFakeValidCursor(tls *libc.TLS) (r uintptr) { _ = libc.Int32FromInt32(0) return uintptr(unsafe.Pointer(&_fakeCursor)) } var _fakeCursor Tu8 // C documentation // // /* // ** This routine restores a cursor back to its original position after it // ** has been moved by some outside activity (such as a btree rebalance or // ** a row having been deleted out from under the cursor). // ** // ** On success, the *pDifferentRow parameter is false if the cursor is left // ** pointing at exactly the same row. *pDifferntRow is the row the cursor // ** was pointing to has been deleted, forcing the cursor to point to some // ** nearby row. // ** // ** This routine should only be called for a cursor that just returned // ** TRUE from sqlite3BtreeCursorHasMoved(). // */ func _sqlite3BtreeCursorRestore(tls *libc.TLS, pCur uintptr, pDifferentRow uintptr) (r int32) { var rc, v1 int32 _, _ = rc, v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) >= int32(CURSOR_REQUIRESEEK) { v1 = _btreeRestoreCursorPosition(tls, pCur) } else { v1 = SQLITE_OK } rc = v1 if rc != 0 { *(*int32)(unsafe.Pointer(pDifferentRow)) = int32(1) return rc } if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID { *(*int32)(unsafe.Pointer(pDifferentRow)) = int32(1) } else { *(*int32)(unsafe.Pointer(pDifferentRow)) = 0 } return SQLITE_OK } // C documentation // // /* // ** Provide flag hints to the cursor. // */ func _sqlite3BtreeCursorHintFlags(tls *libc.TLS, pCur uintptr, x uint32) { _ = libc.Int32FromInt32(0) (*TBtCursor)(unsafe.Pointer(pCur)).Fhints = uint8(x) } // C documentation // // /* // ** Given a page number of a regular database page, return the page // ** number for the pointer-map page that contains the entry for the // ** input page number. // ** // ** Return 0 (not a valid page) for pgno==1 since there is // ** no pointer map associated with page 1. The integrity_check logic // ** requires that ptrmapPageno(*,1)!=1. // */ func _ptrmapPageno(tls *libc.TLS, pBt uintptr, pgno TPgno) (r TPgno) { var iPtrMap, ret TPgno var nPagesPerMapPage int32 _, _, _ = iPtrMap, nPagesPerMapPage, ret _ = libc.Int32FromInt32(0) if pgno < uint32(2) { return uint32(0) } nPagesPerMapPage = int32((*TBtShared)(unsafe.Pointer(pBt)).FusableSize/uint32(5) + uint32(1)) iPtrMap = (pgno - uint32(2)) / uint32(nPagesPerMapPage) ret = iPtrMap*uint32(nPagesPerMapPage) + uint32(2) if ret == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) { ret++ } return ret } // C documentation // // /* // ** Write an entry into the pointer map. // ** // ** This routine updates the pointer map entry for page number 'key' // ** so that it maps to type 'eType' and parent page number 'pgno'. // ** // ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is // ** a no-op. If an error occurs, the appropriate error code is written // ** into *pRC. // */ func _ptrmapPut(tls *libc.TLS, pBt uintptr, key TPgno, eType Tu8, parent TPgno, pRC uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var iPtrmap TPgno var offset, rc, v1 int32 var pPtrmap uintptr var _ /* pDbPage at bp+0 */ uintptr _, _, _, _, _ = iPtrmap, offset, pPtrmap, rc, v1 /* Return code from subfunctions */ if *(*int32)(unsafe.Pointer(pRC)) != 0 { return } _ = libc.Int32FromInt32(0) /* The super-journal page number must never be used as a pointer map page */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if key == uint32(0) { *(*int32)(unsafe.Pointer(pRC)) = _sqlite3CorruptError(tls, int32(71230)) return } iPtrmap = _ptrmapPageno(tls, pBt, key) rc = _sqlite3PagerGet(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, iPtrmap, bp, 0) if rc != SQLITE_OK { *(*int32)(unsafe.Pointer(pRC)) = rc return } if int32(*(*uint8)(unsafe.Pointer(_sqlite3PagerGetExtra(tls, *(*uintptr)(unsafe.Pointer(bp)))))) != 0 { /* The first byte of the extra data is the MemPage.isInit byte. ** If that byte is set, it means this page is also being used ** as a btree page. */ *(*int32)(unsafe.Pointer(pRC)) = _sqlite3CorruptError(tls, int32(71243)) goto ptrmap_exit } offset = int32(libc.Uint32FromInt32(5) * (key - iPtrmap - libc.Uint32FromInt32(1))) if offset < 0 { *(*int32)(unsafe.Pointer(pRC)) = _sqlite3CorruptError(tls, int32(71248)) goto ptrmap_exit } _ = libc.Int32FromInt32(0) pPtrmap = _sqlite3PagerGetData(tls, *(*uintptr)(unsafe.Pointer(bp))) if int32(eType) != int32(*(*Tu8)(unsafe.Pointer(pPtrmap + uintptr(offset)))) || _sqlite3Get4byte(tls, pPtrmap+uintptr(offset+int32(1))) != parent { v1 = _sqlite3PagerWrite(tls, *(*uintptr)(unsafe.Pointer(bp))) rc = v1 *(*int32)(unsafe.Pointer(pRC)) = v1 if rc == SQLITE_OK { *(*Tu8)(unsafe.Pointer(pPtrmap + uintptr(offset))) = eType _sqlite3Put4byte(tls, pPtrmap+uintptr(offset+int32(1)), parent) } } goto ptrmap_exit ptrmap_exit: ; _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp))) } // C documentation // // /* // ** Read an entry from the pointer map. // ** // ** This routine retrieves the pointer map entry for page 'key', writing // ** the type and parent page number to *pEType and *pPgno respectively. // ** An error code is returned if something goes wrong, otherwise SQLITE_OK. // */ func _ptrmapGet(tls *libc.TLS, pBt uintptr, key TPgno, pEType uintptr, pPgno uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iPtrmap, offset, rc int32 var pPtrmap uintptr var _ /* pDbPage at bp+0 */ uintptr _, _, _, _ = iPtrmap, offset, pPtrmap, rc _ = libc.Int32FromInt32(0) iPtrmap = int32(_ptrmapPageno(tls, pBt, key)) rc = _sqlite3PagerGet(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, uint32(iPtrmap), bp, 0) if rc != 0 { return rc } pPtrmap = _sqlite3PagerGetData(tls, *(*uintptr)(unsafe.Pointer(bp))) offset = int32(libc.Uint32FromInt32(5) * (key - uint32(iPtrmap) - libc.Uint32FromInt32(1))) if offset < 0 { _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp))) return _sqlite3CorruptError(tls, int32(71293)) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*Tu8)(unsafe.Pointer(pEType)) = *(*Tu8)(unsafe.Pointer(pPtrmap + uintptr(offset))) if pPgno != 0 { *(*TPgno)(unsafe.Pointer(pPgno)) = _sqlite3Get4byte(tls, pPtrmap+uintptr(offset+int32(1))) } _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp))) if int32(*(*Tu8)(unsafe.Pointer(pEType))) < int32(1) || int32(*(*Tu8)(unsafe.Pointer(pEType))) > int32(5) { return _sqlite3CorruptError(tls, int32(71301)) } return SQLITE_OK } /* ** Given a btree page and a cell index (0 means the first cell on ** the page, 1 means the second cell, and so forth) return a pointer ** to the cell content. ** ** findCellPastPtr() does the same except it skips past the initial ** 4-byte child pointer found on interior pages, if there is one. ** ** This routine works only for pages that do not contain overflow cells. */ // C documentation // // /* // ** This is common tail processing for btreeParseCellPtr() and // ** btreeParseCellPtrIndex() for the case when the cell does not fit entirely // ** on a single B-tree page. Make necessary adjustments to the CellInfo // ** structure. // */ func _btreeParseCellAdjustSizeForOverflow(tls *libc.TLS, pPage uintptr, pCell uintptr, pInfo uintptr) { var maxLocal, minLocal, surplus int32 _, _, _ = maxLocal, minLocal, surplus /* Overflow payload available for local storage */ minLocal = int32((*TMemPage)(unsafe.Pointer(pPage)).FminLocal) maxLocal = int32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) surplus = int32(uint32(minLocal) + ((*TCellInfo)(unsafe.Pointer(pInfo)).FnPayload-uint32(minLocal))%((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize-uint32(4))) if surplus <= maxLocal { (*TCellInfo)(unsafe.Pointer(pInfo)).FnLocal = uint16(surplus) } else { (*TCellInfo)(unsafe.Pointer(pInfo)).FnLocal = uint16(minLocal) } (*TCellInfo)(unsafe.Pointer(pInfo)).FnSize = uint16(int32(uint16(t__predefined_ptrdiff_t((*TCellInfo)(unsafe.Pointer(pInfo)).FpPayload+uintptr((*TCellInfo)(unsafe.Pointer(pInfo)).FnLocal))-int32(pCell))) + int32(4)) } // C documentation // // /* // ** Given a record with nPayload bytes of payload stored within btree // ** page pPage, return the number of bytes of payload stored locally. // */ func _btreePayloadToLocal(tls *libc.TLS, pPage uintptr, nPayload Ti64) (r int32) { var maxLocal, minLocal, surplus, v1 int32 _, _, _, _ = maxLocal, minLocal, surplus, v1 /* Maximum amount of payload held locally */ maxLocal = int32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) if nPayload <= int64(maxLocal) { return int32(nPayload) } else { /* Overflow payload available for local storage */ minLocal = int32((*TMemPage)(unsafe.Pointer(pPage)).FminLocal) surplus = int32(int64(minLocal) + (nPayload-int64(minLocal))%int64((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize-libc.Uint32FromInt32(4))) if surplus <= maxLocal { v1 = surplus } else { v1 = minLocal } return v1 } return r } // C documentation // // /* // ** The following routines are implementations of the MemPage.xParseCell() // ** method. // ** // ** Parse a cell content block and fill in the CellInfo structure. // ** // ** btreeParseCellPtr() => table btree leaf nodes // ** btreeParseCellNoPayload() => table btree internal nodes // ** btreeParseCellPtrIndex() => index btree nodes // ** // ** There is also a wrapper function btreeParseCell() that works for // ** all MemPage types and that references the cell by index rather than // ** by pointer. // */ func _btreeParseCellPtrNoPayload(tls *libc.TLS, pPage uintptr, pCell uintptr, pInfo uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = pPage (*TCellInfo)(unsafe.Pointer(pInfo)).FnSize = uint16(int32(4) + int32(_sqlite3GetVarint(tls, pCell+4, pInfo))) (*TCellInfo)(unsafe.Pointer(pInfo)).FnPayload = uint32(0) (*TCellInfo)(unsafe.Pointer(pInfo)).FnLocal = uint16(0) (*TCellInfo)(unsafe.Pointer(pInfo)).FpPayload = uintptr(0) return } func _btreeParseCellPtr(tls *libc.TLS, pPage uintptr, pCell uintptr, pInfo uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var nPayload Tu32 var pEnd, pIter, v1, v11, v13, v15, v16, v3, v5, v7, v9 uintptr var x, v10, v12, v14, v2, v4, v6, v8 Tu8 var _ /* iKey at bp+0 */ Tu64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = nPayload, pEnd, pIter, x, v1, v10, v11, v12, v13, v14, v15, v16, v2, v3, v4, v5, v6, v7, v8, v9 /* Extracted Key value */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pIter = pCell /* The next block of code is equivalent to: ** ** pIter += getVarint32(pIter, nPayload); ** ** The code is inlined to avoid a function call. */ nPayload = uint32(*(*Tu8)(unsafe.Pointer(pIter))) if nPayload >= uint32(0x80) { pEnd = pIter + 8 nPayload &= uint32(0x7f) for cond := true; cond; cond = int32(*(*Tu8)(unsafe.Pointer(pIter))) >= int32(0x80) && pIter < pEnd { pIter++ v1 = pIter nPayload = nPayload<nKey); ** ** The code is inlined and the loop is unrolled for performance. ** This routine is a high-runner. */ *(*Tu64)(unsafe.Pointer(bp)) = uint64(*(*Tu8)(unsafe.Pointer(pIter))) if *(*Tu64)(unsafe.Pointer(bp)) >= uint64(0x80) { pIter++ v3 = pIter v2 = *(*Tu8)(unsafe.Pointer(v3)) x = v2 *(*Tu64)(unsafe.Pointer(bp)) = *(*Tu64)(unsafe.Pointer(bp))<= int32(0x80) { pIter++ v5 = pIter v4 = *(*Tu8)(unsafe.Pointer(v5)) x = v4 *(*Tu64)(unsafe.Pointer(bp)) = *(*Tu64)(unsafe.Pointer(bp))<= int32(0x80) { pIter++ v7 = pIter v6 = *(*Tu8)(unsafe.Pointer(v7)) x = v6 *(*Tu64)(unsafe.Pointer(bp)) = *(*Tu64)(unsafe.Pointer(bp))<= int32(0x80) { pIter++ v9 = pIter v8 = *(*Tu8)(unsafe.Pointer(v9)) x = v8 *(*Tu64)(unsafe.Pointer(bp)) = *(*Tu64)(unsafe.Pointer(bp))<= int32(0x80) { pIter++ v11 = pIter v10 = *(*Tu8)(unsafe.Pointer(v11)) x = v10 *(*Tu64)(unsafe.Pointer(bp)) = *(*Tu64)(unsafe.Pointer(bp))<= int32(0x80) { pIter++ v13 = pIter v12 = *(*Tu8)(unsafe.Pointer(v13)) x = v12 *(*Tu64)(unsafe.Pointer(bp)) = *(*Tu64)(unsafe.Pointer(bp))<= int32(0x80) { pIter++ v15 = pIter v14 = *(*Tu8)(unsafe.Pointer(v15)) x = v14 *(*Tu64)(unsafe.Pointer(bp)) = *(*Tu64)(unsafe.Pointer(bp))<= int32(0x80) { pIter++ v16 = pIter *(*Tu64)(unsafe.Pointer(bp)) = *(*Tu64)(unsafe.Pointer(bp))<= uint32(0x80) { pEnd = pIter + 8 nPayload &= uint32(0x7f) for cond := true; cond; cond = int32(*(*Tu8)(unsafe.Pointer(pIter))) >= int32(0x80) && pIter < pEnd { pIter++ v1 = pIter nPayload = nPayload< table internal nodes // ** cellSizePtrTableLeaf() => table leaf nodes // ** cellSizePtr() => index internal nodes // ** cellSizeIdxLeaf() => index leaf nodes // */ func _cellSizePtr(tls *libc.TLS, pPage uintptr, pCell uintptr) (r Tu16) { var minLocal int32 var nSize Tu32 var pEnd, pIter, v1 uintptr _, _, _, _, _ = minLocal, nSize, pEnd, pIter, v1 pIter = pCell + uintptr(4) /* Size value to return */ _ = libc.Int32FromInt32(0) nSize = uint32(*(*Tu8)(unsafe.Pointer(pIter))) if nSize >= uint32(0x80) { pEnd = pIter + 8 nSize &= uint32(0x7f) for cond := true; cond; cond = int32(*(*Tu8)(unsafe.Pointer(pIter))) >= int32(0x80) && pIter < pEnd { pIter++ v1 = pIter nSize = nSize< uint32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) { nSize = uint32(minLocal) } nSize += uint32(int32(4) + int32(uint16(int32(pIter)-int32(pCell)))) } _ = libc.Int32FromInt32(0) return uint16(nSize) } func _cellSizePtrIdxLeaf(tls *libc.TLS, pPage uintptr, pCell uintptr) (r Tu16) { var minLocal int32 var nSize Tu32 var pEnd, pIter, v1 uintptr _, _, _, _, _ = minLocal, nSize, pEnd, pIter, v1 pIter = pCell /* Size value to return */ _ = libc.Int32FromInt32(0) nSize = uint32(*(*Tu8)(unsafe.Pointer(pIter))) if nSize >= uint32(0x80) { pEnd = pIter + 8 nSize &= uint32(0x7f) for cond := true; cond; cond = int32(*(*Tu8)(unsafe.Pointer(pIter))) >= int32(0x80) && pIter < pEnd { pIter++ v1 = pIter nSize = nSize< uint32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) { nSize = uint32(minLocal) } nSize += uint32(int32(4) + int32(uint16(int32(pIter)-int32(pCell)))) } _ = libc.Int32FromInt32(0) return uint16(nSize) } func _cellSizePtrNoPayload(tls *libc.TLS, pPage uintptr, pCell uintptr) (r Tu16) { var pEnd, pIter, v1 uintptr _, _, _ = pEnd, pIter, v1 pIter = pCell + uintptr(4) /* End mark for a varint */ _ = pPage _ = libc.Int32FromInt32(0) pEnd = pIter + uintptr(9) for { v1 = pIter pIter++ if !(int32(*(*Tu8)(unsafe.Pointer(v1)))&int32(0x80) != 0 && pIter < pEnd) { break } } _ = libc.Int32FromInt32(0) return uint16(int32(pIter) - int32(pCell)) } func _cellSizePtrTableLeaf(tls *libc.TLS, pPage uintptr, pCell uintptr) (r Tu16) { var minLocal int32 var nSize Tu32 var pEnd, pIter, v1, v11, v13, v15, v2, v3, v5, v7, v9 uintptr var v10, v12, v14, v16, v4, v6, v8 bool _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = minLocal, nSize, pEnd, pIter, v1, v10, v11, v12, v13, v14, v15, v16, v2, v3, v4, v5, v6, v7, v8, v9 pIter = pCell /* Size value to return */ nSize = uint32(*(*Tu8)(unsafe.Pointer(pIter))) if nSize >= uint32(0x80) { pEnd = pIter + 8 nSize &= uint32(0x7f) for cond := true; cond; cond = int32(*(*Tu8)(unsafe.Pointer(pIter))) >= int32(0x80) && pIter < pEnd { pIter++ v1 = pIter nSize = nSize< uint32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) { nSize = uint32(minLocal) } nSize += uint32(int32(4) + int32(uint16(int32(pIter)-int32(pCell)))) } _ = libc.Int32FromInt32(0) return uint16(nSize) } // C documentation // // /* // ** The cell pCell is currently part of page pSrc but will ultimately be part // ** of pPage. (pSrc and pPage are often the same.) If pCell contains a // ** pointer to an overflow page, insert an entry into the pointer-map for // ** the overflow page that will be valid after pCell has been moved to pPage. // */ func _ptrmapPutOvflPtr(tls *libc.TLS, pPage uintptr, pSrc uintptr, pCell uintptr, pRC uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var ovfl TPgno var _ /* info at bp+0 */ TCellInfo _ = ovfl if *(*int32)(unsafe.Pointer(pRC)) != 0 { return } _ = libc.Int32FromInt32(0) (*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxParseCell})))(tls, pPage, pCell, bp) if uint32((*(*TCellInfo)(unsafe.Pointer(bp))).FnLocal) < (*(*TCellInfo)(unsafe.Pointer(bp))).FnPayload { if uint32(pCell) < uint32((*TMemPage)(unsafe.Pointer(pSrc)).FaDataEnd) && uint32(pCell+uintptr((*(*TCellInfo)(unsafe.Pointer(bp))).FnLocal)) > uint32((*TMemPage)(unsafe.Pointer(pSrc)).FaDataEnd) { *(*int32)(unsafe.Pointer(pRC)) = _sqlite3CorruptError(tls, int32(71741)) return } ovfl = _sqlite3Get4byte(tls, pCell+uintptr(int32((*(*TCellInfo)(unsafe.Pointer(bp))).FnSize)-int32(4))) _ptrmapPut(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpBt, ovfl, uint8(PTRMAP_OVERFLOW1), (*TMemPage)(unsafe.Pointer(pPage)).Fpgno, pRC) } } // C documentation // // /* // ** Defragment the page given. This routine reorganizes cells within the // ** page so that there are no free-blocks on the free-block list. // ** // ** Parameter nMaxFrag is the maximum amount of fragmented space that may be // ** present in the page after this routine returns. // ** // ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a // ** b-tree page so that there are no freeblocks or fragment bytes, all // ** unused bytes are contained in the unallocated space region, and all // ** cells are packed tightly at the end of the page. // */ func _defragmentPage(tls *libc.TLS, pPage uintptr, nMaxFrag int32) (r int32) { var cbrk, cellOffset, hdr, i, iCellFirst, iCellLast, iCellStart, iFree, iFree2, nCell, pc, size, sz, sz2, top, usableSize int32 var data, pAddr, pAddr1, pEnd, src, temp uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = cbrk, cellOffset, data, hdr, i, iCellFirst, iCellLast, iCellStart, iFree, iFree2, nCell, pAddr, pAddr1, pEnd, pc, size, src, sz, sz2, temp, top, usableSize /* First cell offset in input */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) data = (*TMemPage)(unsafe.Pointer(pPage)).FaData hdr = int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset) cellOffset = int32((*TMemPage)(unsafe.Pointer(pPage)).FcellOffset) nCell = int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) _ = libc.Int32FromInt32(0) iCellFirst = cellOffset + int32(2)*nCell usableSize = int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize) /* This block handles pages with two or fewer free blocks and nMaxFrag ** or fewer fragmented bytes. In this case it is faster to move the ** two (or one) blocks of cells using memmove() and add the required ** offsets to each pointer in the cell-pointer array than it is to ** reconstruct the entire page. */ if int32(*(*uint8)(unsafe.Pointer(data + uintptr(hdr+int32(7))))) <= nMaxFrag { iFree = int32(*(*uint8)(unsafe.Pointer(data + uintptr(hdr+int32(1)))))< usableSize-int32(4) { return _sqlite3CorruptError(tls, int32(71799)) } if iFree != 0 { iFree2 = int32(*(*uint8)(unsafe.Pointer(data + uintptr(iFree))))< usableSize-int32(4) { return _sqlite3CorruptError(tls, int32(71802)) } if 0 == iFree2 || int32(*(*uint8)(unsafe.Pointer(data + uintptr(iFree2)))) == 0 && int32(*(*uint8)(unsafe.Pointer(data + uintptr(iFree2+int32(1))))) == 0 { pEnd = data + uintptr(cellOffset+nCell*int32(2)) sz2 = 0 sz = int32(*(*uint8)(unsafe.Pointer(data + uintptr(iFree+int32(2)))))<= iFree { return _sqlite3CorruptError(tls, int32(71810)) } if iFree2 != 0 { if iFree+sz > iFree2 { return _sqlite3CorruptError(tls, int32(71813)) } sz2 = int32(*(*uint8)(unsafe.Pointer(data + uintptr(iFree2+int32(2)))))< usableSize { return _sqlite3CorruptError(tls, int32(71815)) } libc.Xmemmove(tls, data+uintptr(iFree+sz+sz2), data+uintptr(iFree+sz), uint32(iFree2-(iFree+sz))) sz += sz2 } else { if iFree+sz > usableSize { return _sqlite3CorruptError(tls, int32(71819)) } } cbrk = top + sz _ = libc.Int32FromInt32(0) libc.Xmemmove(tls, data+uintptr(cbrk), data+uintptr(top), uint32(iFree-top)) pAddr = data + uintptr(cellOffset) for { if !(pAddr < pEnd) { break } pc = int32(*(*Tu8)(unsafe.Pointer(pAddr)))<> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(pAddr + 1)) = uint8(pc + sz) } else { if pc < iFree2 { *(*Tu8)(unsafe.Pointer(pAddr)) = uint8((pc + sz2) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(pAddr + 1)) = uint8(pc + sz2) } } goto _1 _1: ; pAddr += uintptr(2) } goto defragment_out } } } cbrk = usableSize iCellLast = usableSize - int32(4) iCellStart = int32(*(*uint8)(unsafe.Pointer(data + uintptr(hdr+int32(5)))))< 0 { temp = _sqlite3PagerTempSpace(tls, (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FpPager) libc.Xmemcpy(tls, temp, data, uint32(usableSize)) src = temp i = 0 for { if !(i < nCell) { break } /* The i-th cell pointer */ pAddr1 = data + uintptr(cellOffset+i*int32(2)) pc = int32(*(*Tu8)(unsafe.Pointer(pAddr1)))< iCellLast { return _sqlite3CorruptError(tls, int32(71852)) } _ = libc.Int32FromInt32(0) size = int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxCellSize})))(tls, pPage, src+uintptr(pc))) cbrk -= size if cbrk < iCellStart || pc+size > usableSize { return _sqlite3CorruptError(tls, int32(71858)) } _ = libc.Int32FromInt32(0) *(*Tu8)(unsafe.Pointer(pAddr1)) = uint8(cbrk >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(pAddr1 + 1)) = uint8(cbrk) libc.Xmemcpy(tls, data+uintptr(cbrk), src+uintptr(pc), uint32(size)) goto _2 _2: ; i++ } } *(*uint8)(unsafe.Pointer(data + uintptr(hdr+int32(7)))) = uint8(0) goto defragment_out defragment_out: ; _ = libc.Int32FromInt32(0) if int32(*(*uint8)(unsafe.Pointer(data + uintptr(hdr+int32(7)))))+cbrk-iCellFirst != (*TMemPage)(unsafe.Pointer(pPage)).FnFree { return _sqlite3CorruptError(tls, int32(71872)) } _ = libc.Int32FromInt32(0) *(*uint8)(unsafe.Pointer(data + uintptr(hdr+int32(5)))) = uint8(cbrk >> libc.Int32FromInt32(8)) *(*uint8)(unsafe.Pointer(data + uintptr(hdr+int32(5)) + 1)) = uint8(cbrk) *(*uint8)(unsafe.Pointer(data + uintptr(hdr+int32(1)))) = uint8(0) *(*uint8)(unsafe.Pointer(data + uintptr(hdr+int32(2)))) = uint8(0) libc.Xmemset(tls, data+uintptr(iCellFirst), 0, uint32(cbrk-iCellFirst)) _ = libc.Int32FromInt32(0) return SQLITE_OK } // C documentation // // /* // ** Search the free-list on page pPg for space to store a cell nByte bytes in // ** size. If one can be found, return a pointer to the space and remove it // ** from the free-list. // ** // ** If no suitable space can be found on the free-list, return NULL. // ** // ** This function may detect corruption within pPg. If corruption is // ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned. // ** // ** Slots on the free list that are between 1 and 3 bytes larger than nByte // ** will be ignored if adding the extra space to the fragmentation count // ** causes the fragmentation count to exceed 60. // */ func _pageFindSlot(tls *libc.TLS, pPg uintptr, nByte int32, pRc uintptr) (r uintptr) { var aData, pTmp, p2 uintptr var hdr, iAddr, maxPC, pc, size, x, v1 int32 _, _, _, _, _, _, _, _, _, _ = aData, hdr, iAddr, maxPC, pTmp, pc, size, x, v1, p2 hdr = int32((*TMemPage)(unsafe.Pointer(pPg)).FhdrOffset) /* Offset to page header */ aData = (*TMemPage)(unsafe.Pointer(pPg)).FaData /* Page data */ iAddr = hdr + int32(1) /* Address of ptr to pc */ pTmp = aData + uintptr(iAddr) /* Temporary ptr into aData[] */ pc = int32(*(*Tu8)(unsafe.Pointer(pTmp)))<= 0 { if x < int32(4) { /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total ** number of bytes in fragments may not exceed 60. */ if int32(*(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(7))))) > int32(57) { return uintptr(0) } /* Remove the slot from the free-list. Update the number of ** fragmented bytes within the page. */ libc.Xmemcpy(tls, aData+uintptr(iAddr), aData+uintptr(pc), uint32(2)) p2 = aData + uintptr(hdr+int32(7)) *(*Tu8)(unsafe.Pointer(p2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p2))) + int32(uint8(x))) return aData + uintptr(pc) } else { if x+pc > maxPC { /* This slot extends off the end of the usable part of the page */ *(*int32)(unsafe.Pointer(pRc)) = _sqlite3CorruptError(tls, int32(71929)) return uintptr(0) } else { /* The slot remains on the free-list. Reduce its size to account ** for the portion used by the new allocation. */ *(*Tu8)(unsafe.Pointer(aData + uintptr(pc+int32(2)))) = uint8(x >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(aData + uintptr(pc+int32(2)) + 1)) = uint8(x) } } return aData + uintptr(pc+x) } iAddr = pc pTmp = aData + uintptr(pc) pc = int32(*(*Tu8)(unsafe.Pointer(pTmp)))< maxPC+nByte-int32(4) { /* The free slot chain extends off the end of the page */ *(*int32)(unsafe.Pointer(pRc)) = _sqlite3CorruptError(tls, int32(71951)) } return uintptr(0) } // C documentation // // /* // ** Allocate nByte bytes of space from within the B-Tree page passed // ** as the first argument. Write into *pIdx the index into pPage->aData[] // ** of the first byte of allocated space. Return either SQLITE_OK or // ** an error code (usually SQLITE_CORRUPT). // ** // ** The caller guarantees that there is sufficient space to make the // ** allocation. This routine might need to defragment in order to bring // ** all the space together, however. This routine will avoid using // ** the first two bytes past the cell pointer area since presumably this // ** allocation is being made in order to insert a new cell, so we will // ** also end up needing a new cell pointer. // */ func _allocateSpace(tls *libc.TLS, pPage uintptr, nByte int32, pIdx uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var data, pSpace, pTmp uintptr var g2, gap, hdr, top, v1, v2 int32 var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _, _, _ = data, g2, gap, hdr, pSpace, pTmp, top, v1, v2 hdr = int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset) /* Local cache of pPage->hdrOffset */ data = (*TMemPage)(unsafe.Pointer(pPage)).FaData /* First byte of cell content area */ *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK /* First byte of gap between cell pointers and cell content */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Minimum cell size is 4 */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) gap = int32((*TMemPage)(unsafe.Pointer(pPage)).FcellOffset) + int32(2)*int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) _ = libc.Int32FromInt32(0) /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size ** and the reserved space is zero (the usual value for reserved space) ** then the cell content offset of an empty page wants to be 65536. ** However, that integer is too large to be stored in a 2-byte unsigned ** integer, so a value of 0 is used in its place. */ pTmp = data + uintptr(hdr+int32(5)) top = int32(*(*Tu8)(unsafe.Pointer(pTmp)))< top { if top == 0 && (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize == uint32(65536) { top = int32(65536) } else { return _sqlite3CorruptError(tls, int32(71999)) } } else { if top > int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize) { return _sqlite3CorruptError(tls, int32(72002)) } } /* If there is enough space between gap and top for one more cell pointer, ** and if the freelist is not empty, then search the ** freelist looking for a slot big enough to satisfy the request. */ if (*(*Tu8)(unsafe.Pointer(data + uintptr(hdr+int32(2)))) != 0 || *(*Tu8)(unsafe.Pointer(data + uintptr(hdr+int32(1)))) != 0) && gap+int32(2) <= top { pSpace = _pageFindSlot(tls, pPage, nByte, bp) if pSpace != 0 { _ = libc.Int32FromInt32(0) v1 = int32(pSpace) - int32(data) g2 = v1 *(*int32)(unsafe.Pointer(pIdx)) = v1 if g2 <= gap { return _sqlite3CorruptError(tls, int32(72019)) } else { return SQLITE_OK } } else { if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } } } /* The request could not be fulfilled using a freelist slot. Check ** to see if defragmentation is necessary. */ if gap+int32(2)+nByte > top { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(4) < (*TMemPage)(unsafe.Pointer(pPage)).FnFree-(int32(2)+nByte) { v2 = int32(4) } else { v2 = (*TMemPage)(unsafe.Pointer(pPage)).FnFree - (int32(2) + nByte) } *(*int32)(unsafe.Pointer(bp)) = _defragmentPage(tls, pPage, v2) if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } top = (int32(*(*Tu8)(unsafe.Pointer(data + uintptr(hdr+int32(5)))))<> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(data + uintptr(hdr+int32(5)) + 1)) = uint8(top) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pIdx)) = top return SQLITE_OK } // C documentation // // /* // ** Return a section of the pPage->aData to the freelist. // ** The first byte of the new free block is pPage->aData[iStart] // ** and the size of the block is iSize bytes. // ** // ** Adjacent freeblocks are coalesced. // ** // ** Even though the freeblock list was checked by btreeComputeFreeSpace(), // ** that routine will not detect overlap between cells or freeblocks. Nor // ** does it detect cells or freeblocks that encroach into the reserved bytes // ** at the end of the page. So do additional corruption checks inside this // ** routine and return SQLITE_CORRUPT if any problems are found. // */ func _freeSpace(tls *libc.TLS, pPage uintptr, iStart Tu16, iSize Tu16) (r int32) { var data, pTmp, p2 uintptr var hdr, nFrag Tu8 var iEnd Tu32 var iFreeBlk, iOrigSize, iPtr, x, v1 Tu16 var iPtrEnd int32 _, _, _, _, _, _, _, _, _, _, _, _ = data, hdr, iEnd, iFreeBlk, iOrigSize, iPtr, iPtrEnd, nFrag, pTmp, x, v1, p2 /* Page header size. 0 or 100 */ nFrag = uint8(0) /* Reduction in fragmentation */ iOrigSize = iSize /* Offset to cell content area */ iEnd = uint32(int32(iStart) + int32(iSize)) /* First byte past the iStart buffer */ data = (*TMemPage)(unsafe.Pointer(pPage)).FaData /* Temporary ptr into data[] */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Minimum cell size is 4 */ _ = libc.Int32FromInt32(0) /* The list of freeblocks must be in ascending order. Find the ** spot on the list where iStart should be inserted. */ hdr = (*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset iPtr = uint16(int32(hdr) + int32(1)) if int32(*(*uint8)(unsafe.Pointer(data + uintptr(int32(iPtr)+int32(1))))) == 0 && int32(*(*uint8)(unsafe.Pointer(data + uintptr(iPtr)))) == 0 { iFreeBlk = uint16(0) /* Shortcut for the case when the freelist is empty */ } else { for { v1 = uint16(int32(*(*uint8)(unsafe.Pointer(data + uintptr(iPtr))))< (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize-uint32(4) { /* TH3: corrupt081.100 */ return _sqlite3CorruptError(tls, int32(72103)) } _ = libc.Int32FromInt32(0) /* At this point: ** iFreeBlk: First freeblock after iStart, or zero if none ** iPtr: The address of a pointer to iFreeBlk ** ** Check to see if iFreeBlk should be coalesced onto the end of iStart. */ if iFreeBlk != 0 && iEnd+uint32(3) >= uint32(iFreeBlk) { nFrag = uint8(uint32(iFreeBlk) - iEnd) if iEnd > uint32(iFreeBlk) { return _sqlite3CorruptError(tls, int32(72115)) } iEnd = uint32(int32(iFreeBlk) + (int32(*(*uint8)(unsafe.Pointer(data + uintptr(int32(iFreeBlk)+int32(2)))))< (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize { return _sqlite3CorruptError(tls, int32(72118)) } iSize = uint16(iEnd - uint32(iStart)) iFreeBlk = uint16(int32(*(*uint8)(unsafe.Pointer(data + uintptr(iFreeBlk))))< int32(hdr)+int32(1) { iPtrEnd = int32(iPtr) + (int32(*(*uint8)(unsafe.Pointer(data + uintptr(int32(iPtr)+int32(2)))))<= int32(iStart) { if iPtrEnd > int32(iStart) { return _sqlite3CorruptError(tls, int32(72131)) } nFrag = Tu8(int32(nFrag) + (int32(iStart) - iPtrEnd)) iSize = uint16(iEnd - uint32(iPtr)) iStart = iPtr } } if int32(nFrag) > int32(*(*uint8)(unsafe.Pointer(data + uintptr(int32(hdr)+int32(7))))) { return _sqlite3CorruptError(tls, int32(72137)) } p2 = data + uintptr(int32(hdr)+int32(7)) *(*uint8)(unsafe.Pointer(p2)) = uint8(int32(*(*uint8)(unsafe.Pointer(p2))) - int32(nFrag)) } pTmp = data + uintptr(int32(hdr)+int32(5)) x = uint16(int32(*(*Tu8)(unsafe.Pointer(pTmp)))<> libc.Int32FromInt32(8)) *(*uint8)(unsafe.Pointer(data + uintptr(int32(hdr)+int32(1)) + 1)) = uint8(iFreeBlk) *(*uint8)(unsafe.Pointer(data + uintptr(int32(hdr)+int32(5)))) = uint8(iEnd >> libc.Int32FromInt32(8)) *(*uint8)(unsafe.Pointer(data + uintptr(int32(hdr)+int32(5)) + 1)) = uint8(iEnd) } else { /* Insert the new freeblock into the freelist */ *(*uint8)(unsafe.Pointer(data + uintptr(iPtr))) = uint8(int32(iStart) >> libc.Int32FromInt32(8)) *(*uint8)(unsafe.Pointer(data + uintptr(iPtr) + 1)) = uint8(iStart) *(*uint8)(unsafe.Pointer(data + uintptr(iStart))) = uint8(int32(iFreeBlk) >> libc.Int32FromInt32(8)) *(*uint8)(unsafe.Pointer(data + uintptr(iStart) + 1)) = uint8(iFreeBlk) *(*uint8)(unsafe.Pointer(data + uintptr(int32(iStart)+int32(2)))) = uint8(int32(iSize) >> libc.Int32FromInt32(8)) *(*uint8)(unsafe.Pointer(data + uintptr(int32(iStart)+int32(2)) + 1)) = uint8(iSize) } *(*int32)(unsafe.Pointer(pPage + 20)) += int32(iOrigSize) return SQLITE_OK } // C documentation // // /* // ** Decode the flags byte (the first byte of the header) for a page // ** and initialize fields of the MemPage structure accordingly. // ** // ** Only the following combinations are supported. Anything different // ** indicates a corrupt database files: // ** // ** PTF_ZERODATA (0x02, 2) // ** PTF_LEAFDATA | PTF_INTKEY (0x05, 5) // ** PTF_ZERODATA | PTF_LEAF (0x0a, 10) // ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF (0x0d, 13) // */ func _decodeFlags(tls *libc.TLS, pPage uintptr, flagByte int32) (r int32) { var pBt uintptr _ = pBt /* A copy of pPage->pBt */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pBt = (*TMemPage)(unsafe.Pointer(pPage)).FpBt (*TMemPage)(unsafe.Pointer(pPage)).Fmax1bytePayload = (*TBtShared)(unsafe.Pointer(pBt)).Fmax1bytePayload if flagByte >= libc.Int32FromInt32(PTF_ZERODATA)|libc.Int32FromInt32(PTF_LEAF) { (*TMemPage)(unsafe.Pointer(pPage)).FchildPtrSize = uint8(0) (*TMemPage)(unsafe.Pointer(pPage)).Fleaf = uint8(1) if flagByte == libc.Int32FromInt32(PTF_LEAFDATA)|libc.Int32FromInt32(PTF_INTKEY)|libc.Int32FromInt32(PTF_LEAF) { (*TMemPage)(unsafe.Pointer(pPage)).FintKeyLeaf = uint8(1) (*TMemPage)(unsafe.Pointer(pPage)).FxCellSize = __ccgo_fp(_cellSizePtrTableLeaf) (*TMemPage)(unsafe.Pointer(pPage)).FxParseCell = __ccgo_fp(_btreeParseCellPtr) (*TMemPage)(unsafe.Pointer(pPage)).FintKey = uint8(1) (*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal = (*TBtShared)(unsafe.Pointer(pBt)).FmaxLeaf (*TMemPage)(unsafe.Pointer(pPage)).FminLocal = (*TBtShared)(unsafe.Pointer(pBt)).FminLeaf } else { if flagByte == libc.Int32FromInt32(PTF_ZERODATA)|libc.Int32FromInt32(PTF_LEAF) { (*TMemPage)(unsafe.Pointer(pPage)).FintKey = uint8(0) (*TMemPage)(unsafe.Pointer(pPage)).FintKeyLeaf = uint8(0) (*TMemPage)(unsafe.Pointer(pPage)).FxCellSize = __ccgo_fp(_cellSizePtrIdxLeaf) (*TMemPage)(unsafe.Pointer(pPage)).FxParseCell = __ccgo_fp(_btreeParseCellPtrIndex) (*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal = (*TBtShared)(unsafe.Pointer(pBt)).FmaxLocal (*TMemPage)(unsafe.Pointer(pPage)).FminLocal = (*TBtShared)(unsafe.Pointer(pBt)).FminLocal } else { (*TMemPage)(unsafe.Pointer(pPage)).FintKey = uint8(0) (*TMemPage)(unsafe.Pointer(pPage)).FintKeyLeaf = uint8(0) (*TMemPage)(unsafe.Pointer(pPage)).FxCellSize = __ccgo_fp(_cellSizePtrIdxLeaf) (*TMemPage)(unsafe.Pointer(pPage)).FxParseCell = __ccgo_fp(_btreeParseCellPtrIndex) return _sqlite3CorruptError(tls, int32(72206)) } } } else { (*TMemPage)(unsafe.Pointer(pPage)).FchildPtrSize = uint8(4) (*TMemPage)(unsafe.Pointer(pPage)).Fleaf = uint8(0) if flagByte == int32(PTF_ZERODATA) { (*TMemPage)(unsafe.Pointer(pPage)).FintKey = uint8(0) (*TMemPage)(unsafe.Pointer(pPage)).FintKeyLeaf = uint8(0) (*TMemPage)(unsafe.Pointer(pPage)).FxCellSize = __ccgo_fp(_cellSizePtr) (*TMemPage)(unsafe.Pointer(pPage)).FxParseCell = __ccgo_fp(_btreeParseCellPtrIndex) (*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal = (*TBtShared)(unsafe.Pointer(pBt)).FmaxLocal (*TMemPage)(unsafe.Pointer(pPage)).FminLocal = (*TBtShared)(unsafe.Pointer(pBt)).FminLocal } else { if flagByte == libc.Int32FromInt32(PTF_LEAFDATA)|libc.Int32FromInt32(PTF_INTKEY) { (*TMemPage)(unsafe.Pointer(pPage)).FintKeyLeaf = uint8(0) (*TMemPage)(unsafe.Pointer(pPage)).FxCellSize = __ccgo_fp(_cellSizePtrNoPayload) (*TMemPage)(unsafe.Pointer(pPage)).FxParseCell = __ccgo_fp(_btreeParseCellPtrNoPayload) (*TMemPage)(unsafe.Pointer(pPage)).FintKey = uint8(1) (*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal = (*TBtShared)(unsafe.Pointer(pBt)).FmaxLeaf (*TMemPage)(unsafe.Pointer(pPage)).FminLocal = (*TBtShared)(unsafe.Pointer(pBt)).FminLeaf } else { (*TMemPage)(unsafe.Pointer(pPage)).FintKey = uint8(0) (*TMemPage)(unsafe.Pointer(pPage)).FintKeyLeaf = uint8(0) (*TMemPage)(unsafe.Pointer(pPage)).FxCellSize = __ccgo_fp(_cellSizePtr) (*TMemPage)(unsafe.Pointer(pPage)).FxParseCell = __ccgo_fp(_btreeParseCellPtrIndex) return _sqlite3CorruptError(tls, int32(72230)) } } } return SQLITE_OK } // C documentation // // /* // ** Compute the amount of freespace on the page. In other words, fill // ** in the pPage->nFree field. // */ func _btreeComputeFreeSpace(tls *libc.TLS, pPage uintptr) (r int32) { var data uintptr var hdr Tu8 var iCellFirst, iCellLast, nFree, pc, top, usableSize int32 var next, size Tu32 _, _, _, _, _, _, _, _, _, _ = data, hdr, iCellFirst, iCellLast, nFree, next, pc, size, top, usableSize /* Last possible cell or freeblock offset */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) usableSize = int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize) hdr = (*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset data = (*TMemPage)(unsafe.Pointer(pPage)).FaData /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates ** the start of the cell content area. A zero value for this integer is ** interpreted as 65536. */ top = (int32(*(*Tu8)(unsafe.Pointer(data + uintptr(int32(hdr)+int32(5)))))< 0 { if pc < top { /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will ** always be at least one cell before the first freeblock. */ return _sqlite3CorruptError(tls, int32(72281)) } for int32(1) != 0 { if pc > iCellLast { /* Freeblock off the end of the page */ return _sqlite3CorruptError(tls, int32(72286)) } next = uint32(int32(*(*Tu8)(unsafe.Pointer(data + uintptr(pc))))< uint32(0) { /* Freeblock not in ascending order */ return _sqlite3CorruptError(tls, int32(72296)) } if uint32(pc)+size > uint32(usableSize) { /* Last freeblock extends past page end */ return _sqlite3CorruptError(tls, int32(72300)) } } /* At this point, nFree contains the sum of the offset to the start ** of the cell-content area plus the number of free bytes within ** the cell-content area. If this is greater than the usable-size ** of the page, then the page must be corrupted. This check also ** serves to verify that the offset to the start of the cell-content ** area, according to the page header, lies within the page. */ if nFree > usableSize || nFree < iCellFirst { return _sqlite3CorruptError(tls, int32(72312)) } (*TMemPage)(unsafe.Pointer(pPage)).FnFree = int32(uint16(nFree - iCellFirst)) return SQLITE_OK } // C documentation // // /* // ** Do additional sanity check after btreeInitPage() if // ** PRAGMA cell_size_check=ON // */ func _btreeCellSizeCheck(tls *libc.TLS, pPage uintptr) (r int32) { var cellOffset, i, iCellFirst, iCellLast, pc, sz, usableSize int32 var data uintptr _, _, _, _, _, _, _, _ = cellOffset, data, i, iCellFirst, iCellLast, pc, sz, usableSize /* Start of cell content area */ iCellFirst = int32((*TMemPage)(unsafe.Pointer(pPage)).FcellOffset) + int32(2)*int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) usableSize = int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize) iCellLast = usableSize - int32(4) data = (*TMemPage)(unsafe.Pointer(pPage)).FaData cellOffset = int32((*TMemPage)(unsafe.Pointer(pPage)).FcellOffset) if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) { iCellLast-- } i = 0 for { if !(i < int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)) { break } pc = int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer(data + uintptr(cellOffset+i*int32(2)))))) if pc < iCellFirst || pc > iCellLast { return _sqlite3CorruptError(tls, int32(72343)) } sz = int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxCellSize})))(tls, pPage, data+uintptr(pc))) if pc+sz > usableSize { return _sqlite3CorruptError(tls, int32(72348)) } goto _1 _1: ; i++ } return SQLITE_OK } // C documentation // // /* // ** Initialize the auxiliary information for a disk block. // ** // ** Return SQLITE_OK on success. If we see that the page does // ** not contain a well-formed database page, then return // ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not // ** guarantee that the page is well-formed. It only shows that // ** we failed to detect any corruption. // */ func _btreeInitPage(tls *libc.TLS, pPage uintptr) (r int32) { var data, pBt uintptr _, _ = data, pBt /* The main btree structure */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pBt = (*TMemPage)(unsafe.Pointer(pPage)).FpBt data = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset) /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating ** the b-tree page type. */ if _decodeFlags(tls, pPage, int32(*(*Tu8)(unsafe.Pointer(data)))) != 0 { return _sqlite3CorruptError(tls, int32(72380)) } _ = libc.Int32FromInt32(0) (*TMemPage)(unsafe.Pointer(pPage)).FmaskPage = uint16((*TBtShared)(unsafe.Pointer(pBt)).FpageSize - libc.Uint32FromInt32(1)) (*TMemPage)(unsafe.Pointer(pPage)).FnOverflow = uint8(0) (*TMemPage)(unsafe.Pointer(pPage)).FcellOffset = uint16(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset) + int32(8) + int32((*TMemPage)(unsafe.Pointer(pPage)).FchildPtrSize)) (*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx = data + uintptr((*TMemPage)(unsafe.Pointer(pPage)).FchildPtrSize) + uintptr(8) (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr((*TBtShared)(unsafe.Pointer(pBt)).FpageSize) (*TMemPage)(unsafe.Pointer(pPage)).FaDataOfst = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr((*TMemPage)(unsafe.Pointer(pPage)).FchildPtrSize) /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the ** number of cells on the page. */ (*TMemPage)(unsafe.Pointer(pPage)).FnCell = uint16(int32(*(*Tu8)(unsafe.Pointer(data + 3)))< ((*TBtShared)(unsafe.Pointer(pBt)).FpageSize-uint32(8))/uint32(6) { /* To many cells for a single page. The page must be corrupt */ return _sqlite3CorruptError(tls, int32(72394)) } /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only ** possible for a root page of a table that contains no rows) then the ** offset to the cell content area will equal the page size minus the ** bytes of reserved space. */ _ = libc.Int32FromInt32(0) (*TMemPage)(unsafe.Pointer(pPage)).FnFree = -int32(1) /* Indicate that this value is yet uncomputed */ (*TMemPage)(unsafe.Pointer(pPage)).FisInit = uint8(1) if (*Tsqlite3)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).Fdb)).Fflags&uint64(SQLITE_CellSizeCk) != 0 { return _btreeCellSizeCheck(tls, pPage) } return SQLITE_OK } // C documentation // // /* // ** Set up a raw page so that it looks like a database page holding // ** no entries. // */ func _zeroPage(tls *libc.TLS, pPage uintptr, flags int32) { var data, pBt uintptr var first Tu16 var hdr Tu8 var v1 int32 _, _, _, _, _ = data, first, hdr, pBt, v1 data = (*TMemPage)(unsafe.Pointer(pPage)).FaData pBt = (*TMemPage)(unsafe.Pointer(pPage)).FpBt hdr = (*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_FAST_SECURE) != 0 { libc.Xmemset(tls, data+uintptr(hdr), 0, (*TBtShared)(unsafe.Pointer(pBt)).FusableSize-uint32(hdr)) } *(*uint8)(unsafe.Pointer(data + uintptr(hdr))) = uint8(flags) if flags&int32(PTF_LEAF) == 0 { v1 = int32(12) } else { v1 = int32(8) } first = uint16(int32(hdr) + v1) libc.Xmemset(tls, data+uintptr(int32(hdr)+int32(1)), 0, uint32(4)) *(*uint8)(unsafe.Pointer(data + uintptr(int32(hdr)+int32(7)))) = uint8(0) *(*uint8)(unsafe.Pointer(data + uintptr(int32(hdr)+int32(5)))) = uint8((*TBtShared)(unsafe.Pointer(pBt)).FusableSize >> libc.Int32FromInt32(8)) *(*uint8)(unsafe.Pointer(data + uintptr(int32(hdr)+int32(5)) + 1)) = uint8((*TBtShared)(unsafe.Pointer(pBt)).FusableSize) (*TMemPage)(unsafe.Pointer(pPage)).FnFree = int32(uint16((*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32(first))) _decodeFlags(tls, pPage, flags) (*TMemPage)(unsafe.Pointer(pPage)).FcellOffset = first (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd = data + uintptr((*TBtShared)(unsafe.Pointer(pBt)).FpageSize) (*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx = data + uintptr(first) (*TMemPage)(unsafe.Pointer(pPage)).FaDataOfst = data + uintptr((*TMemPage)(unsafe.Pointer(pPage)).FchildPtrSize) (*TMemPage)(unsafe.Pointer(pPage)).FnOverflow = uint8(0) _ = libc.Int32FromInt32(0) (*TMemPage)(unsafe.Pointer(pPage)).FmaskPage = uint16((*TBtShared)(unsafe.Pointer(pBt)).FpageSize - libc.Uint32FromInt32(1)) (*TMemPage)(unsafe.Pointer(pPage)).FnCell = uint16(0) (*TMemPage)(unsafe.Pointer(pPage)).FisInit = uint8(1) } // C documentation // // /* // ** Convert a DbPage obtained from the pager into a MemPage used by // ** the btree layer. // */ func _btreePageFromDbPage(tls *libc.TLS, pDbPage uintptr, pgno TPgno, pBt uintptr) (r uintptr) { var pPage uintptr var v1 int32 _, _ = pPage, v1 pPage = _sqlite3PagerGetExtra(tls, pDbPage) if pgno != (*TMemPage)(unsafe.Pointer(pPage)).Fpgno { (*TMemPage)(unsafe.Pointer(pPage)).FaData = _sqlite3PagerGetData(tls, pDbPage) (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage = pDbPage (*TMemPage)(unsafe.Pointer(pPage)).FpBt = pBt (*TMemPage)(unsafe.Pointer(pPage)).Fpgno = pgno if pgno == uint32(1) { v1 = int32(100) } else { v1 = 0 } (*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset = uint8(v1) } _ = libc.Int32FromInt32(0) return pPage } // C documentation // // /* // ** Get a page from the pager. Initialize the MemPage.pBt and // ** MemPage.aData elements if needed. See also: btreeGetUnusedPage(). // ** // ** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care // ** about the content of the page at this time. So do not go to the disk // ** to fetch the content. Just fill in the content with zeros for now. // ** If in the future we call sqlite3PagerWrite() on this page, that // ** means we have started to be concerned about content and the disk // ** read should occur at that point. // */ func _btreeGetPage(tls *libc.TLS, pBt uintptr, pgno TPgno, ppPage uintptr, flags int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* pDbPage at bp+0 */ uintptr _ = rc _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3PagerGet(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, pgno, bp, flags) if rc != 0 { return rc } *(*uintptr)(unsafe.Pointer(ppPage)) = _btreePageFromDbPage(tls, *(*uintptr)(unsafe.Pointer(bp)), pgno, pBt) return SQLITE_OK } // C documentation // // /* // ** Retrieve a page from the pager cache. If the requested page is not // ** already in the pager cache return NULL. Initialize the MemPage.pBt and // ** MemPage.aData elements if needed. // */ func _btreePageLookup(tls *libc.TLS, pBt uintptr, pgno TPgno) (r uintptr) { var pDbPage uintptr _ = pDbPage _ = libc.Int32FromInt32(0) pDbPage = _sqlite3PagerLookup(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, pgno) if pDbPage != 0 { return _btreePageFromDbPage(tls, pDbPage, pgno, pBt) } return uintptr(0) } // C documentation // // /* // ** Return the size of the database file in pages. If there is any kind of // ** error, return ((unsigned int)-1). // */ func _btreePagecount(tls *libc.TLS, pBt uintptr) (r TPgno) { return (*TBtShared)(unsafe.Pointer(pBt)).FnPage } func _sqlite3BtreeLastPage(tls *libc.TLS, p uintptr) (r TPgno) { _ = libc.Int32FromInt32(0) return _btreePagecount(tls, (*TBtree)(unsafe.Pointer(p)).FpBt) } // C documentation // // /* // ** Get a page from the pager and initialize it. // */ func _getAndInitPage(tls *libc.TLS, pBt uintptr, pgno TPgno, ppPage uintptr, bReadOnly int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pPage uintptr var rc int32 var _ /* pDbPage at bp+0 */ uintptr _, _ = pPage, rc _ = libc.Int32FromInt32(0) if pgno > _btreePagecount(tls, pBt) { *(*uintptr)(unsafe.Pointer(ppPage)) = uintptr(0) return _sqlite3CorruptError(tls, int32(72537)) } rc = _sqlite3PagerGet(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, pgno, bp, bReadOnly) if rc != 0 { *(*uintptr)(unsafe.Pointer(ppPage)) = uintptr(0) return rc } pPage = _sqlite3PagerGetExtra(tls, *(*uintptr)(unsafe.Pointer(bp))) if int32((*TMemPage)(unsafe.Pointer(pPage)).FisInit) == 0 { _btreePageFromDbPage(tls, *(*uintptr)(unsafe.Pointer(bp)), pgno, pBt) rc = _btreeInitPage(tls, pPage) if rc != SQLITE_OK { _releasePage(tls, pPage) *(*uintptr)(unsafe.Pointer(ppPage)) = uintptr(0) return rc } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(ppPage)) = pPage return SQLITE_OK } // C documentation // // /* // ** Release a MemPage. This should be called once for each prior // ** call to btreeGetPage. // ** // ** Page1 is a special case and must be released using releasePageOne(). // */ func _releasePageNotNull(tls *libc.TLS, pPage uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3PagerUnrefNotNull(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage) } func _releasePage(tls *libc.TLS, pPage uintptr) { if pPage != 0 { _releasePageNotNull(tls, pPage) } } func _releasePageOne(tls *libc.TLS, pPage uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3PagerUnrefPageOne(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage) } // C documentation // // /* // ** Get an unused page. // ** // ** This works just like btreeGetPage() with the addition: // ** // ** * If the page is already in use for some other purpose, immediately // ** release it and return an SQLITE_CURRUPT error. // ** * Make sure the isInit flag is clear // */ func _btreeGetUnusedPage(tls *libc.TLS, pBt uintptr, pgno TPgno, ppPage uintptr, flags int32) (r int32) { var rc int32 _ = rc rc = _btreeGetPage(tls, pBt, pgno, ppPage, flags) if rc == SQLITE_OK { if _sqlite3PagerPageRefcount(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(ppPage)))).FpDbPage) > int32(1) { _releasePage(tls, *(*uintptr)(unsafe.Pointer(ppPage))) *(*uintptr)(unsafe.Pointer(ppPage)) = uintptr(0) return _sqlite3CorruptError(tls, int32(72609)) } (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(ppPage)))).FisInit = uint8(0) } else { *(*uintptr)(unsafe.Pointer(ppPage)) = uintptr(0) } return rc } // C documentation // // /* // ** During a rollback, when the pager reloads information into the cache // ** so that the cache is restored to its original state at the start of // ** the transaction, for each page restored this routine is called. // ** // ** This routine needs to reset the extra data section at the end of the // ** page to agree with the restored data. // */ func _pageReinit(tls *libc.TLS, pData uintptr) { var pPage uintptr _ = pPage pPage = _sqlite3PagerGetExtra(tls, pData) _ = libc.Int32FromInt32(0) if (*TMemPage)(unsafe.Pointer(pPage)).FisInit != 0 { _ = libc.Int32FromInt32(0) (*TMemPage)(unsafe.Pointer(pPage)).FisInit = uint8(0) if _sqlite3PagerPageRefcount(tls, pData) > int32(1) { /* pPage might not be a btree page; it might be an overflow page ** or ptrmap page or a free page. In those cases, the following ** call to btreeInitPage() will likely return SQLITE_CORRUPT. ** But no harm is done by this. And it is very important that ** btreeInitPage() be called on every btree page so we make ** the call for every page that comes in for re-initializing. */ _btreeInitPage(tls, pPage) } } } // C documentation // // /* // ** Invoke the busy handler for a btree. // */ func _btreeInvokeBusyHandler(tls *libc.TLS, pArg uintptr) (r int32) { var pBt uintptr _ = pBt pBt = pArg _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return _sqlite3InvokeBusyHandler(tls, (*TBtShared)(unsafe.Pointer(pBt)).Fdb+464) } // C documentation // // /* // ** Open a database file. // ** // ** zFilename is the name of the database file. If zFilename is NULL // ** then an ephemeral database is created. The ephemeral database might // ** be exclusively in memory, or it might use a disk-based memory cache. // ** Either way, the ephemeral database will be automatically deleted // ** when sqlite3BtreeClose() is called. // ** // ** If zFilename is ":memory:" then an in-memory database is created // ** that is automatically destroyed when it is closed. // ** // ** The "flags" parameter is a bitmask that might contain bits like // ** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY. // ** // ** If the database is already opened in the same database connection // ** and we are in shared cache mode, then the open will fail with an // ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared // ** objects in the same database connection since doing so will lead // ** to problems with locking. // */ func _sqlite3BtreeOpen(tls *libc.TLS, pVfs uintptr, zFilename uintptr, db uintptr, ppBtree uintptr, flags int32, vfsFlags int32) (r int32) { bp := tls.Alloc(112) defer tls.Free(112) var i, iDb, isMemdb, isTempDb, nFilename, nFullPathname, rc, v1, v6, v7 int32 var mutexOpen, mutexShared, mutexShared1, p, pBt, pExisting, pFile, pSib, zFullPathname, v9, p4, p5 uintptr var nReserve Tu8 var _ /* zDbHeader at bp+0 */ [100]uint8 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, iDb, isMemdb, isTempDb, mutexOpen, mutexShared, mutexShared1, nFilename, nFullPathname, nReserve, p, pBt, pExisting, pFile, pSib, rc, zFullPathname, v1, v6, v7, v9, p4, p5 pBt = uintptr(0) /* Handle to return */ mutexOpen = uintptr(0) /* Prevents a race condition. Ticket #3537 */ rc = SQLITE_OK /* Database header content */ /* True if opening an ephemeral, temporary database */ isTempDb = libc.BoolInt32(zFilename == uintptr(0) || int32(*(*uint8)(unsafe.Pointer(zFilename))) == 0) /* Set the variable isMemdb to true for an in-memory database, or ** false for a file-based database. */ isMemdb = libc.BoolInt32(zFilename != 0 && libc.Xstrcmp(tls, zFilename, __ccgo_ts+4190) == 0 || isTempDb != 0 && _sqlite3TempInMemory(tls, db) != 0 || vfsFlags&int32(SQLITE_OPEN_MEMORY) != 0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* flags fit in 8 bits */ /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */ _ = libc.Int32FromInt32(0) /* A BTREE_SINGLE database is always a temporary and/or ephemeral */ _ = libc.Int32FromInt32(0) if isMemdb != 0 { flags |= int32(BTREE_MEMORY) } if vfsFlags&int32(SQLITE_OPEN_MAIN_DB) != 0 && (isMemdb != 0 || isTempDb != 0) { vfsFlags = vfsFlags & ^libc.Int32FromInt32(SQLITE_OPEN_MAIN_DB) | int32(SQLITE_OPEN_TEMP_DB) } p = _sqlite3MallocZero(tls, uint64(48)) if !(p != 0) { return int32(SQLITE_NOMEM) } (*TBtree)(unsafe.Pointer(p)).FinTrans = uint8(TRANS_NONE) (*TBtree)(unsafe.Pointer(p)).Fdb = db (*TBtree)(unsafe.Pointer(p)).Flock.FpBtree = p (*TBtree)(unsafe.Pointer(p)).Flock.FiTable = uint32(1) /* ** If this Btree is a candidate for shared cache, try to find an ** existing BtShared object that we can share with */ if isTempDb == 0 && (isMemdb == 0 || vfsFlags&int32(SQLITE_OPEN_URI) != 0) { if vfsFlags&int32(SQLITE_OPEN_SHAREDCACHE) != 0 { nFilename = _sqlite3Strlen30(tls, zFilename) + int32(1) nFullPathname = (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname + int32(1) if nFullPathname > nFilename { v1 = nFullPathname } else { v1 = nFilename } zFullPathname = _sqlite3Malloc(tls, uint64(v1)) (*TBtree)(unsafe.Pointer(p)).Fsharable = uint8(1) if !(zFullPathname != 0) { Xsqlite3_free(tls, p) return int32(SQLITE_NOMEM) } if isMemdb != 0 { libc.Xmemcpy(tls, zFullPathname, zFilename, uint32(nFilename)) } else { rc = _sqlite3OsFullPathname(tls, pVfs, zFilename, nFullPathname, zFullPathname) if rc != 0 { if rc == libc.Int32FromInt32(SQLITE_OK)|libc.Int32FromInt32(2)<= 0) { break } pExisting = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt if pExisting != 0 && (*TBtree)(unsafe.Pointer(pExisting)).FpBt == pBt { Xsqlite3_mutex_leave(tls, mutexShared) Xsqlite3_mutex_leave(tls, mutexOpen) Xsqlite3_free(tls, zFullPathname) Xsqlite3_free(tls, p) return int32(SQLITE_CONSTRAINT) } goto _3 _3: ; iDb-- } (*TBtree)(unsafe.Pointer(p)).FpBt = pBt (*TBtShared)(unsafe.Pointer(pBt)).FnRef++ break } goto _2 _2: ; pBt = (*TBtShared)(unsafe.Pointer(pBt)).FpNext } Xsqlite3_mutex_leave(tls, mutexShared) Xsqlite3_free(tls, zFullPathname) } } if pBt == uintptr(0) { /* ** The following asserts make sure that structures used by the btree are ** the right size. This is to guard against size changes that result ** when compiling on a different architecture. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Suppress false-positive compiler warning from PVS-Studio */ libc.Xmemset(tls, bp+16, 0, uint32(8)) pBt = _sqlite3MallocZero(tls, uint64(92)) if pBt == uintptr(0) { rc = int32(SQLITE_NOMEM) goto btree_open_out } rc = _sqlite3PagerOpen(tls, pVfs, pBt, zFilename, int32(84), flags, vfsFlags, __ccgo_fp(_pageReinit)) if rc == SQLITE_OK { _sqlite3PagerSetMmapLimit(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, (*Tsqlite3)(unsafe.Pointer(db)).FszMmap) rc = _sqlite3PagerReadFileheader(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, int32(100), bp) } if rc != SQLITE_OK { goto btree_open_out } (*TBtShared)(unsafe.Pointer(pBt)).FopenFlags = uint8(flags) (*TBtShared)(unsafe.Pointer(pBt)).Fdb = db _sqlite3PagerSetBusyHandler(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, __ccgo_fp(_btreeInvokeBusyHandler), pBt) (*TBtree)(unsafe.Pointer(p)).FpBt = pBt (*TBtShared)(unsafe.Pointer(pBt)).FpCursor = uintptr(0) (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 = uintptr(0) if _sqlite3PagerIsreadonly(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager) != 0 { p4 = pBt + 24 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | libc.Int32FromInt32(BTS_READ_ONLY)) } /* EVIDENCE-OF: R-51873-39618 The page size for a database file is ** determined by the 2-byte integer located at an offset of 16 bytes from ** the beginning of the database file. */ (*TBtShared)(unsafe.Pointer(pBt)).FpageSize = uint32(int32((*(*[100]uint8)(unsafe.Pointer(bp)))[int32(16)])< uint32(SQLITE_MAX_PAGE_SIZE) || ((*TBtShared)(unsafe.Pointer(pBt)).FpageSize-uint32(1))&(*TBtShared)(unsafe.Pointer(pBt)).FpageSize != uint32(0) { (*TBtShared)(unsafe.Pointer(pBt)).FpageSize = uint32(0) /* If the magic name ":memory:" will create an in-memory database, then ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a ** regular file-name. In this case the auto-vacuum applies as per normal. */ if zFilename != 0 && !(isMemdb != 0) { (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum = uint8(libc.Int32FromInt32(0)) (*TBtShared)(unsafe.Pointer(pBt)).FincrVacuum = uint8(libc.Int32FromInt32(0)) } nReserve = uint8(0) } else { /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is ** determined by the one-byte unsigned integer found at an offset of 20 ** into the database file header. */ nReserve = (*(*[100]uint8)(unsafe.Pointer(bp)))[int32(20)] p5 = pBt + 24 *(*Tu16)(unsafe.Pointer(p5)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p5))) | libc.Int32FromInt32(BTS_PAGESIZE_FIXED)) if _sqlite3Get4byte(tls, bp+uintptr(libc.Int32FromInt32(36)+libc.Int32FromInt32(4)*libc.Int32FromInt32(4))) != 0 { v6 = int32(1) } else { v6 = 0 } (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum = uint8(v6) if _sqlite3Get4byte(tls, bp+uintptr(libc.Int32FromInt32(36)+libc.Int32FromInt32(7)*libc.Int32FromInt32(4))) != 0 { v7 = int32(1) } else { v7 = 0 } (*TBtShared)(unsafe.Pointer(pBt)).FincrVacuum = uint8(v7) } rc = _sqlite3PagerSetPagesize(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, pBt+36, int32(nReserve)) if rc != 0 { goto btree_open_out } (*TBtShared)(unsafe.Pointer(pBt)).FusableSize = (*TBtShared)(unsafe.Pointer(pBt)).FpageSize - uint32(nReserve) _ = libc.Int32FromInt32(0) /* 8-byte alignment of pageSize */ /* Add the new BtShared object to the linked list sharable BtShareds. */ (*TBtShared)(unsafe.Pointer(pBt)).FnRef = int32(1) if (*TBtree)(unsafe.Pointer(p)).Fsharable != 0 { mutexShared1 = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN)) if libc.Bool(int32(SQLITE_THREADSAFE) != 0) && _sqlite3Config.FbCoreMutex != 0 { (*TBtShared)(unsafe.Pointer(pBt)).Fmutex = _sqlite3MutexAlloc(tls, SQLITE_MUTEX_FAST) if (*TBtShared)(unsafe.Pointer(pBt)).Fmutex == uintptr(0) { rc = int32(SQLITE_NOMEM) goto btree_open_out } } Xsqlite3_mutex_enter(tls, mutexShared1) (*TBtShared)(unsafe.Pointer(pBt)).FpNext = _sqlite3SharedCacheList _sqlite3SharedCacheList = pBt Xsqlite3_mutex_leave(tls, mutexShared1) } } /* If the new Btree uses a sharable pBtShared, then link the new ** Btree into the list of all sharable Btrees for the same connection. ** The list is kept in ascending order by pBt address. */ if (*TBtree)(unsafe.Pointer(p)).Fsharable != 0 { i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } v9 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt pSib = v9 if v9 != uintptr(0) && (*TBtree)(unsafe.Pointer(pSib)).Fsharable != 0 { for (*TBtree)(unsafe.Pointer(pSib)).FpPrev != 0 { pSib = (*TBtree)(unsafe.Pointer(pSib)).FpPrev } if uint32((*TBtree)(unsafe.Pointer(p)).FpBt) < uint32((*TBtree)(unsafe.Pointer(pSib)).FpBt) { (*TBtree)(unsafe.Pointer(p)).FpNext = pSib (*TBtree)(unsafe.Pointer(p)).FpPrev = uintptr(0) (*TBtree)(unsafe.Pointer(pSib)).FpPrev = p } else { for (*TBtree)(unsafe.Pointer(pSib)).FpNext != 0 && uint32((*TBtree)(unsafe.Pointer((*TBtree)(unsafe.Pointer(pSib)).FpNext)).FpBt) < uint32((*TBtree)(unsafe.Pointer(p)).FpBt) { pSib = (*TBtree)(unsafe.Pointer(pSib)).FpNext } (*TBtree)(unsafe.Pointer(p)).FpNext = (*TBtree)(unsafe.Pointer(pSib)).FpNext (*TBtree)(unsafe.Pointer(p)).FpPrev = pSib if (*TBtree)(unsafe.Pointer(p)).FpNext != 0 { (*TBtree)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpNext)).FpPrev = p } (*TBtree)(unsafe.Pointer(pSib)).FpNext = p } break } goto _8 _8: ; i++ } } *(*uintptr)(unsafe.Pointer(ppBtree)) = p goto btree_open_out btree_open_out: ; if rc != SQLITE_OK { if pBt != 0 && (*TBtShared)(unsafe.Pointer(pBt)).FpPager != 0 { _sqlite3PagerClose(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, uintptr(0)) } Xsqlite3_free(tls, pBt) Xsqlite3_free(tls, p) *(*uintptr)(unsafe.Pointer(ppBtree)) = uintptr(0) } else { /* If the B-Tree was successfully opened, set the pager-cache size to the ** default value. Except, when opening on an existing shared pager-cache, ** do not change the pager-cache size. */ if _sqlite3BtreeSchema(tls, p, 0, uintptr(0)) == uintptr(0) { _sqlite3BtreeSetCacheSize(tls, p, -int32(2000)) } pFile = _sqlite3PagerFile(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager) if (*Tsqlite3_file)(unsafe.Pointer(pFile)).FpMethods != 0 { _sqlite3OsFileControlHint(tls, pFile, int32(SQLITE_FCNTL_PDB), pBt+4) } } if mutexOpen != 0 { _ = libc.Int32FromInt32(0) Xsqlite3_mutex_leave(tls, mutexOpen) } _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** Decrement the BtShared.nRef counter. When it reaches zero, // ** remove the BtShared structure from the sharing list. Return // ** true if the BtShared.nRef counter reaches zero and return // ** false if it is still positive. // */ func _removeFromSharingList(tls *libc.TLS, pBt uintptr) (r int32) { var pList, pMainMtx uintptr var removed int32 _, _, _ = pList, pMainMtx, removed removed = 0 _ = libc.Int32FromInt32(0) pMainMtx = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN)) Xsqlite3_mutex_enter(tls, pMainMtx) (*TBtShared)(unsafe.Pointer(pBt)).FnRef-- if (*TBtShared)(unsafe.Pointer(pBt)).FnRef <= 0 { if _sqlite3SharedCacheList == pBt { _sqlite3SharedCacheList = (*TBtShared)(unsafe.Pointer(pBt)).FpNext } else { pList = _sqlite3SharedCacheList for pList != 0 && (*TBtShared)(unsafe.Pointer(pList)).FpNext != pBt { pList = (*TBtShared)(unsafe.Pointer(pList)).FpNext } if pList != 0 { (*TBtShared)(unsafe.Pointer(pList)).FpNext = (*TBtShared)(unsafe.Pointer(pBt)).FpNext } } if int32(SQLITE_THREADSAFE) != 0 { Xsqlite3_mutex_free(tls, (*TBtShared)(unsafe.Pointer(pBt)).Fmutex) } removed = int32(1) } Xsqlite3_mutex_leave(tls, pMainMtx) return removed } // C documentation // // /* // ** Make sure pBt->pTmpSpace points to an allocation of // ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child // ** pointer. // */ func _allocateTempSpace(tls *libc.TLS, pBt uintptr) (r int32) { var pCur uintptr _ = pCur _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* This routine is called only by btreeCursor() when allocating the ** first write cursor for the BtShared object */ _ = libc.Int32FromInt32(0) (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace = _sqlite3PageMalloc(tls, int32((*TBtShared)(unsafe.Pointer(pBt)).FpageSize)) if (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace == uintptr(0) { pCur = (*TBtShared)(unsafe.Pointer(pBt)).FpCursor (*TBtShared)(unsafe.Pointer(pBt)).FpCursor = (*TBtCursor)(unsafe.Pointer(pCur)).FpNext /* Unlink the cursor */ libc.Xmemset(tls, pCur, 0, uint32(200)) return int32(SQLITE_NOMEM) } /* One of the uses of pBt->pTmpSpace is to format cells before ** inserting them into a leaf page (function fillInCell()). If ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes ** by the various routines that manipulate binary cells. Which ** can mean that fillInCell() only initializes the first 2 or 3 ** bytes of pTmpSpace, but that the first 4 bytes are copied from ** it into a database page. This is not actually a problem, but it ** does cause a valgrind error when the 1 or 2 bytes of uninitialized ** data is passed to system call write(). So to avoid this error, ** zero the first 4 bytes of temp space here. ** ** Also: Provide four bytes of initialized space before the ** beginning of pTmpSpace as an area available to prepend the ** left-child pointer to the beginning of a cell. */ libc.Xmemset(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace, 0, uint32(8)) *(*uintptr)(unsafe.Pointer(pBt + 84)) += uintptr(4) return SQLITE_OK } // C documentation // // /* // ** Free the pBt->pTmpSpace allocation // */ func _freeTempSpace(tls *libc.TLS, pBt uintptr) { if (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace != 0 { *(*uintptr)(unsafe.Pointer(pBt + 84)) -= uintptr(4) _sqlite3PageFree(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace) (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace = uintptr(0) } } // C documentation // // /* // ** Close an open database and invalidate all cursors. // */ func _sqlite3BtreeClose(tls *libc.TLS, p uintptr) (r int32) { var pBt uintptr _ = pBt pBt = (*TBtree)(unsafe.Pointer(p)).FpBt /* Close all cursors opened via this handle. */ _ = libc.Int32FromInt32(0) _sqlite3BtreeEnter(tls, p) /* Verify that no other cursors have this Btree open */ /* Rollback any active transaction and free the handle structure. ** The call to sqlite3BtreeRollback() drops any table-locks held by ** this handle. */ _sqlite3BtreeRollback(tls, p, SQLITE_OK, 0) _sqlite3BtreeLeave(tls, p) /* If there are still other outstanding references to the shared-btree ** structure, return now. The remainder of this procedure cleans ** up the shared-btree. */ _ = libc.Int32FromInt32(0) if !((*TBtree)(unsafe.Pointer(p)).Fsharable != 0) || _removeFromSharingList(tls, pBt) != 0 { /* The pBt is no longer on the sharing list, so we can access ** it without having to hold the mutex. ** ** Clean out and delete the BtShared object. */ _ = libc.Int32FromInt32(0) _sqlite3PagerClose(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, (*TBtree)(unsafe.Pointer(p)).Fdb) if (*TBtShared)(unsafe.Pointer(pBt)).FxFreeSchema != 0 && (*TBtShared)(unsafe.Pointer(pBt)).FpSchema != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TBtShared)(unsafe.Pointer(pBt)).FxFreeSchema})))(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpSchema) } _sqlite3DbFree(tls, uintptr(0), (*TBtShared)(unsafe.Pointer(pBt)).FpSchema) _freeTempSpace(tls, pBt) Xsqlite3_free(tls, pBt) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TBtree)(unsafe.Pointer(p)).FpPrev != 0 { (*TBtree)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpPrev)).FpNext = (*TBtree)(unsafe.Pointer(p)).FpNext } if (*TBtree)(unsafe.Pointer(p)).FpNext != 0 { (*TBtree)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpNext)).FpPrev = (*TBtree)(unsafe.Pointer(p)).FpPrev } Xsqlite3_free(tls, p) return SQLITE_OK } // C documentation // // /* // ** Change the "soft" limit on the number of pages in the cache. // ** Unused and unmodified pages will be recycled when the number of // ** pages in the cache exceeds this soft limit. But the size of the // ** cache is allowed to grow larger than this limit if it contains // ** dirty pages or pages still in active use. // */ func _sqlite3BtreeSetCacheSize(tls *libc.TLS, p uintptr, mxPage int32) (r int32) { var pBt uintptr _ = pBt pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _ = libc.Int32FromInt32(0) _sqlite3BtreeEnter(tls, p) _sqlite3PagerSetCachesize(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, mxPage) _sqlite3BtreeLeave(tls, p) return SQLITE_OK } // C documentation // // /* // ** Change the "spill" limit on the number of pages in the cache. // ** If the number of pages exceeds this limit during a write transaction, // ** the pager might attempt to "spill" pages to the journal early in // ** order to free up memory. // ** // ** The value returned is the current spill size. If zero is passed // ** as an argument, no changes are made to the spill size setting, so // ** using mxPage of 0 is a way to query the current spill size. // */ func _sqlite3BtreeSetSpillSize(tls *libc.TLS, p uintptr, mxPage int32) (r int32) { var pBt uintptr var res int32 _, _ = pBt, res pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _ = libc.Int32FromInt32(0) _sqlite3BtreeEnter(tls, p) res = _sqlite3PagerSetSpillsize(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, mxPage) _sqlite3BtreeLeave(tls, p) return res } // C documentation // // /* // ** Change the limit on the amount of the database file that may be // ** memory mapped. // */ func _sqlite3BtreeSetMmapLimit(tls *libc.TLS, p uintptr, szMmap Tsqlite3_int64) (r int32) { var pBt uintptr _ = pBt pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _ = libc.Int32FromInt32(0) _sqlite3BtreeEnter(tls, p) _sqlite3PagerSetMmapLimit(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, szMmap) _sqlite3BtreeLeave(tls, p) return SQLITE_OK } // C documentation // // /* // ** Change the way data is synced to disk in order to increase or decrease // ** how well the database resists damage due to OS crashes and power // ** failures. Level 1 is the same as asynchronous (no syncs() occur and // ** there is a high probability of damage) Level 2 is the default. There // ** is a very low but non-zero probability of damage. Level 3 reduces the // ** probability of damage to near zero but with a write performance reduction. // */ func _sqlite3BtreeSetPagerFlags(tls *libc.TLS, p uintptr, pgFlags uint32) (r int32) { var pBt uintptr _ = pBt pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _ = libc.Int32FromInt32(0) _sqlite3BtreeEnter(tls, p) _sqlite3PagerSetFlags(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, pgFlags) _sqlite3BtreeLeave(tls, p) return SQLITE_OK } // C documentation // // /* // ** Change the default pages size and the number of reserved bytes per page. // ** Or, if the page size has already been fixed, return SQLITE_READONLY // ** without changing anything. // ** // ** The page size must be a power of 2 between 512 and 65536. If the page // ** size supplied does not meet this constraint then the page size is not // ** changed. // ** // ** Page sizes are constrained to be a power of two so that the region // ** of the database file used for locking (beginning at PENDING_BYTE, // ** the first byte past the 1GB boundary, 0x40000000) needs to occur // ** at the beginning of a page. // ** // ** If parameter nReserve is less than zero, then the number of reserved // ** bytes per page is left unchanged. // ** // ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size // ** and autovacuum mode can no longer be changed. // */ func _sqlite3BtreeSetPageSize(tls *libc.TLS, p uintptr, pageSize int32, nReserve int32, iFix int32) (r int32) { var pBt, p1 uintptr var rc, x int32 _, _, _, _ = pBt, rc, x, p1 rc = SQLITE_OK pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _ = libc.Int32FromInt32(0) _sqlite3BtreeEnter(tls, p) (*TBtShared)(unsafe.Pointer(pBt)).FnReserveWanted = uint8(nReserve) x = int32((*TBtShared)(unsafe.Pointer(pBt)).FpageSize - (*TBtShared)(unsafe.Pointer(pBt)).FusableSize) if nReserve < x { nReserve = x } if int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_PAGESIZE_FIXED) != 0 { _sqlite3BtreeLeave(tls, p) return int32(SQLITE_READONLY) } _ = libc.Int32FromInt32(0) if pageSize >= int32(512) && pageSize <= int32(SQLITE_MAX_PAGE_SIZE) && (pageSize-int32(1))&pageSize == 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if nReserve > int32(32) && pageSize == int32(512) { pageSize = int32(1024) } (*TBtShared)(unsafe.Pointer(pBt)).FpageSize = uint32(pageSize) _freeTempSpace(tls, pBt) } rc = _sqlite3PagerSetPagesize(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, pBt+36, nReserve) (*TBtShared)(unsafe.Pointer(pBt)).FusableSize = (*TBtShared)(unsafe.Pointer(pBt)).FpageSize - uint32(uint16(nReserve)) if iFix != 0 { p1 = pBt + 24 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(BTS_PAGESIZE_FIXED)) } _sqlite3BtreeLeave(tls, p) return rc } // C documentation // // /* // ** Return the currently defined page size // */ func _sqlite3BtreeGetPageSize(tls *libc.TLS, p uintptr) (r int32) { return int32((*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FpageSize) } // C documentation // // /* // ** This function is similar to sqlite3BtreeGetReserve(), except that it // ** may only be called if it is guaranteed that the b-tree mutex is already // ** held. // ** // ** This is useful in one special case in the backup API code where it is // ** known that the shared b-tree mutex is held, but the mutex on the // ** database handle that owns *p is not. In this case if sqlite3BtreeEnter() // ** were to be called, it might collide with some other operation on the // ** database handle that owns *p, causing undefined behavior. // */ func _sqlite3BtreeGetReserveNoMutex(tls *libc.TLS, p uintptr) (r int32) { var n int32 _ = n _ = libc.Int32FromInt32(0) n = int32((*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FpageSize - (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FusableSize) return n } // C documentation // // /* // ** Return the number of bytes of space at the end of every page that // ** are intentionally left unused. This is the "reserved" space that is // ** sometimes used by extensions. // ** // ** The value returned is the larger of the current reserve size and // ** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES. // ** The amount of reserve can only grow - never shrink. // */ func _sqlite3BtreeGetRequestedReserve(tls *libc.TLS, p uintptr) (r int32) { var n1, n2, v1 int32 _, _, _ = n1, n2, v1 _sqlite3BtreeEnter(tls, p) n1 = int32((*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FnReserveWanted) n2 = _sqlite3BtreeGetReserveNoMutex(tls, p) _sqlite3BtreeLeave(tls, p) if n1 > n2 { v1 = n1 } else { v1 = n2 } return v1 } // C documentation // // /* // ** Set the maximum page count for a database if mxPage is positive. // ** No changes are made if mxPage is 0 or negative. // ** Regardless of the value of mxPage, return the maximum page count. // */ func _sqlite3BtreeMaxPageCount(tls *libc.TLS, p uintptr, mxPage TPgno) (r TPgno) { var n TPgno _ = n _sqlite3BtreeEnter(tls, p) n = _sqlite3PagerMaxPageCount(tls, (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FpPager, mxPage) _sqlite3BtreeLeave(tls, p) return n } // C documentation // // /* // ** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags: // ** // ** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared // ** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared // ** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set // ** newFlag==(-1) No changes // ** // ** This routine acts as a query if newFlag is less than zero // ** // ** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but // ** freelist leaf pages are not written back to the database. Thus in-page // ** deleted content is cleared, but freelist deleted content is not. // ** // ** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition // ** that freelist leaf pages are written back into the database, increasing // ** the amount of disk I/O. // */ func _sqlite3BtreeSecureDelete(tls *libc.TLS, p uintptr, newFlag int32) (r int32) { var b int32 var p1, p2 uintptr _, _, _ = b, p1, p2 if p == uintptr(0) { return 0 } _sqlite3BtreeEnter(tls, p) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if newFlag >= 0 { p1 = (*TBtree)(unsafe.Pointer(p)).FpBt + 24 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(BTS_FAST_SECURE)) p2 = (*TBtree)(unsafe.Pointer(p)).FpBt + 24 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(BTS_SECURE_DELETE)*newFlag) } b = int32((*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FbtsFlags) & int32(BTS_FAST_SECURE) / int32(BTS_SECURE_DELETE) _sqlite3BtreeLeave(tls, p) return b } // C documentation // // /* // ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' // ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it // ** is disabled. The default value for the auto-vacuum property is // ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. // */ func _sqlite3BtreeSetAutoVacuum(tls *libc.TLS, p uintptr, autoVacuum int32) (r int32) { var av Tu8 var pBt uintptr var rc, v1, v3, v4 int32 var v2 bool _, _, _, _, _, _, _ = av, pBt, rc, v1, v2, v3, v4 pBt = (*TBtree)(unsafe.Pointer(p)).FpBt rc = SQLITE_OK av = uint8(autoVacuum) _sqlite3BtreeEnter(tls, p) if v2 = int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_PAGESIZE_FIXED) != 0; v2 { if av != 0 { v1 = int32(1) } else { v1 = 0 } } if v2 && v1 != int32((*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum) { rc = int32(SQLITE_READONLY) } else { if av != 0 { v3 = int32(1) } else { v3 = 0 } (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum = uint8(v3) if int32(av) == int32(2) { v4 = int32(1) } else { v4 = 0 } (*TBtShared)(unsafe.Pointer(pBt)).FincrVacuum = uint8(v4) } _sqlite3BtreeLeave(tls, p) return rc } // C documentation // // /* // ** Return the value of the 'auto-vacuum' property. If auto-vacuum is // ** enabled 1 is returned. Otherwise 0. // */ func _sqlite3BtreeGetAutoVacuum(tls *libc.TLS, p uintptr) (r int32) { var rc, v1, v2 int32 _, _, _ = rc, v1, v2 _sqlite3BtreeEnter(tls, p) if !((*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FautoVacuum != 0) { v1 = BTREE_AUTOVACUUM_NONE } else { if !((*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FincrVacuum != 0) { v2 = int32(BTREE_AUTOVACUUM_FULL) } else { v2 = int32(BTREE_AUTOVACUUM_INCR) } v1 = v2 } rc = v1 _sqlite3BtreeLeave(tls, p) return rc } // C documentation // // /* // ** Get a reference to pPage1 of the database file. This will // ** also acquire a readlock on that file. // ** // ** SQLITE_OK is returned on success. If the file is not a // ** well-formed database file, then SQLITE_CORRUPT is returned. // ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM // ** is returned if we run out of memory. // */ func _lockBtree(tls *libc.TLS, pBt uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var nPage, pageSize, usableSize Tu32 var page1, p1, p2, p3 uintptr var rc, v4, v5 int32 var _ /* isOpen at bp+8 */ int32 var _ /* nPageFile at bp+4 */ Tu32 var _ /* pPage1 at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _ = nPage, page1, pageSize, rc, usableSize, v4, v5, p1, p2, p3 /* Number of pages in the database */ *(*Tu32)(unsafe.Pointer(bp + 4)) = uint32(0) /* Number of pages in the database file */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3PagerSharedLock(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager) if rc != SQLITE_OK { return rc } rc = _btreeGetPage(tls, pBt, uint32(1), bp, 0) if rc != SQLITE_OK { return rc } /* Do some checking to help insure the file we opened really is ** a valid database file. */ nPage = _sqlite3Get4byte(tls, uintptr(28)+(*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData) _sqlite3PagerPagecount(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, bp+4) if nPage == uint32(0) || libc.Xmemcmp(tls, uintptr(24)+(*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData, uintptr(92)+(*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData, uint32(4)) != 0 { nPage = *(*Tu32)(unsafe.Pointer(bp + 4)) } if (*Tsqlite3)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).Fdb)).Fflags&uint64(SQLITE_ResetDatabase) != uint64(0) { nPage = uint32(0) } if nPage > uint32(0) { page1 = (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData rc = int32(SQLITE_NOTADB) /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d ** 61 74 20 33 00. */ if libc.Xmemcmp(tls, page1, uintptr(unsafe.Pointer(&_zMagicHeader)), uint32(16)) != 0 { goto page1_init_failed } if int32(*(*Tu8)(unsafe.Pointer(page1 + 18))) > int32(2) { p1 = pBt + 24 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(BTS_READ_ONLY)) } if int32(*(*Tu8)(unsafe.Pointer(page1 + 19))) > int32(2) { goto page1_init_failed } /* If the read version is set to 2, this database should be accessed ** in WAL mode. If the log is not already open, open it now. Then ** return SQLITE_OK and return without populating BtShared.pPage1. ** The caller detects this and calls this function again. This is ** required as the version of page 1 currently in the page1 buffer ** may not be the latest version - there may be a newer one in the log ** file. */ if int32(*(*Tu8)(unsafe.Pointer(page1 + 19))) == int32(2) && int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_NO_WAL) == 0 { *(*int32)(unsafe.Pointer(bp + 8)) = 0 rc = _sqlite3PagerOpenWal(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, bp+8) if rc != SQLITE_OK { goto page1_init_failed } else { if *(*int32)(unsafe.Pointer(bp + 8)) == 0 { _releasePageOne(tls, *(*uintptr)(unsafe.Pointer(bp))) return SQLITE_OK } } rc = int32(SQLITE_NOTADB) } else { } /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload ** fractions and the leaf payload fraction values must be 64, 32, and 32. ** ** The original design allowed these amounts to vary, but as of ** version 3.6.0, we require them to be fixed. */ if libc.Xmemcmp(tls, page1+21, __ccgo_ts+4199, uint32(3)) != 0 { goto page1_init_failed } /* EVIDENCE-OF: R-51873-39618 The page size for a database file is ** determined by the 2-byte integer located at an offset of 16 bytes from ** the beginning of the database file. */ pageSize = uint32(int32(*(*Tu8)(unsafe.Pointer(page1 + 16)))< uint32(SQLITE_MAX_PAGE_SIZE) || pageSize <= uint32(256) { goto page1_init_failed } _ = libc.Int32FromInt32(0) /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte ** integer at offset 20 is the number of bytes of space at the end of ** each page to reserve for extensions. ** ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is ** determined by the one-byte unsigned integer found at an offset of 20 ** into the database file header. */ usableSize = pageSize - uint32(*(*Tu8)(unsafe.Pointer(page1 + 20))) if pageSize != (*TBtShared)(unsafe.Pointer(pBt)).FpageSize { /* After reading the first page of the database assuming a page size ** of BtShared.pageSize, we have discovered that the page-size is ** actually pageSize. Unlock the database, leave pBt->pPage1 at ** zero and return SQLITE_OK. The caller will call this function ** again with the correct page-size. */ _releasePageOne(tls, *(*uintptr)(unsafe.Pointer(bp))) (*TBtShared)(unsafe.Pointer(pBt)).FusableSize = usableSize (*TBtShared)(unsafe.Pointer(pBt)).FpageSize = pageSize p2 = pBt + 24 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(BTS_PAGESIZE_FIXED)) _freeTempSpace(tls, pBt) rc = _sqlite3PagerSetPagesize(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, pBt+36, int32(pageSize-usableSize)) return rc } if nPage > *(*Tu32)(unsafe.Pointer(bp + 4)) { if _sqlite3WritableSchema(tls, (*TBtShared)(unsafe.Pointer(pBt)).Fdb) == 0 { rc = _sqlite3CorruptError(tls, int32(73547)) goto page1_init_failed } else { nPage = *(*Tu32)(unsafe.Pointer(bp + 4)) } } /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to ** be less than 480. In other words, if the page size is 512, then the ** reserved space size cannot exceed 32. */ if usableSize < uint32(480) { goto page1_init_failed } p3 = pBt + 24 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) | libc.Int32FromInt32(BTS_PAGESIZE_FIXED)) (*TBtShared)(unsafe.Pointer(pBt)).FpageSize = pageSize (*TBtShared)(unsafe.Pointer(pBt)).FusableSize = usableSize if _sqlite3Get4byte(tls, page1+uintptr(libc.Int32FromInt32(36)+libc.Int32FromInt32(4)*libc.Int32FromInt32(4))) != 0 { v4 = int32(1) } else { v4 = 0 } (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum = uint8(v4) if _sqlite3Get4byte(tls, page1+uintptr(libc.Int32FromInt32(36)+libc.Int32FromInt32(7)*libc.Int32FromInt32(4))) != 0 { v5 = int32(1) } else { v5 = 0 } (*TBtShared)(unsafe.Pointer(pBt)).FincrVacuum = uint8(v5) } /* maxLocal is the maximum amount of payload to store locally for ** a cell. Make sure it is small enough so that at least minFanout ** cells can will fit on one page. We assume a 10-byte page header. ** Besides the payload, the cell must store: ** 2-byte pointer to the cell ** 4-byte child pointer ** 9-byte nKey value ** 4-byte nData value ** 4-byte overflow page pointer ** So a cell consists of a 2-byte pointer, a header which is as much as ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow ** page pointer. */ (*TBtShared)(unsafe.Pointer(pBt)).FmaxLocal = uint16(((*TBtShared)(unsafe.Pointer(pBt)).FusableSize-libc.Uint32FromInt32(12))*libc.Uint32FromInt32(64)/libc.Uint32FromInt32(255) - libc.Uint32FromInt32(23)) (*TBtShared)(unsafe.Pointer(pBt)).FminLocal = uint16(((*TBtShared)(unsafe.Pointer(pBt)).FusableSize-libc.Uint32FromInt32(12))*libc.Uint32FromInt32(32)/libc.Uint32FromInt32(255) - libc.Uint32FromInt32(23)) (*TBtShared)(unsafe.Pointer(pBt)).FmaxLeaf = uint16((*TBtShared)(unsafe.Pointer(pBt)).FusableSize - libc.Uint32FromInt32(35)) (*TBtShared)(unsafe.Pointer(pBt)).FminLeaf = uint16(((*TBtShared)(unsafe.Pointer(pBt)).FusableSize-libc.Uint32FromInt32(12))*libc.Uint32FromInt32(32)/libc.Uint32FromInt32(255) - libc.Uint32FromInt32(23)) if int32((*TBtShared)(unsafe.Pointer(pBt)).FmaxLocal) > int32(127) { (*TBtShared)(unsafe.Pointer(pBt)).Fmax1bytePayload = uint8(127) } else { (*TBtShared)(unsafe.Pointer(pBt)).Fmax1bytePayload = uint8((*TBtShared)(unsafe.Pointer(pBt)).FmaxLocal) } _ = libc.Int32FromInt32(0) (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 = *(*uintptr)(unsafe.Pointer(bp)) (*TBtShared)(unsafe.Pointer(pBt)).FnPage = nPage return SQLITE_OK goto page1_init_failed page1_init_failed: ; _releasePageOne(tls, *(*uintptr)(unsafe.Pointer(bp))) (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 = uintptr(0) return rc } // C documentation // // /* // ** If there are no outstanding cursors and we are not in the middle // ** of a transaction but there is a read lock on the database, then // ** this routine unrefs the first page of the database file which // ** has the effect of releasing the read lock. // ** // ** If there is a transaction in progress, this routine is a no-op. // */ func _unlockBtreeIfUnused(tls *libc.TLS, pBt uintptr) { var pPage1 uintptr _ = pPage1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TBtShared)(unsafe.Pointer(pBt)).FinTransaction) == TRANS_NONE && (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 != uintptr(0) { pPage1 = (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 = uintptr(0) _releasePageOne(tls, pPage1) } } // C documentation // // /* // ** If pBt points to an empty file then convert that empty file // ** into a new empty database by initializing the first page of // ** the database. // */ func _newDatabase(tls *libc.TLS, pBt uintptr) (r int32) { var data, pP1, p1 uintptr var rc int32 _, _, _, _ = data, pP1, rc, p1 _ = libc.Int32FromInt32(0) if (*TBtShared)(unsafe.Pointer(pBt)).FnPage > uint32(0) { return SQLITE_OK } pP1 = (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 _ = libc.Int32FromInt32(0) data = (*TMemPage)(unsafe.Pointer(pP1)).FaData rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pP1)).FpDbPage) if rc != 0 { return rc } libc.Xmemcpy(tls, data, uintptr(unsafe.Pointer(&_zMagicHeader)), uint32(16)) _ = libc.Int32FromInt32(0) *(*uint8)(unsafe.Pointer(data + 16)) = uint8((*TBtShared)(unsafe.Pointer(pBt)).FpageSize >> libc.Int32FromInt32(8) & libc.Uint32FromInt32(0xff)) *(*uint8)(unsafe.Pointer(data + 17)) = uint8((*TBtShared)(unsafe.Pointer(pBt)).FpageSize >> libc.Int32FromInt32(16) & libc.Uint32FromInt32(0xff)) *(*uint8)(unsafe.Pointer(data + 18)) = uint8(1) *(*uint8)(unsafe.Pointer(data + 19)) = uint8(1) _ = libc.Int32FromInt32(0) *(*uint8)(unsafe.Pointer(data + 20)) = uint8((*TBtShared)(unsafe.Pointer(pBt)).FpageSize - (*TBtShared)(unsafe.Pointer(pBt)).FusableSize) *(*uint8)(unsafe.Pointer(data + 21)) = uint8(64) *(*uint8)(unsafe.Pointer(data + 22)) = uint8(32) *(*uint8)(unsafe.Pointer(data + 23)) = uint8(32) libc.Xmemset(tls, data+24, 0, uint32(libc.Int32FromInt32(100)-libc.Int32FromInt32(24))) _zeroPage(tls, pP1, libc.Int32FromInt32(PTF_INTKEY)|libc.Int32FromInt32(PTF_LEAF)|libc.Int32FromInt32(PTF_LEAFDATA)) p1 = pBt + 24 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(BTS_PAGESIZE_FIXED)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3Put4byte(tls, data+uintptr(libc.Int32FromInt32(36)+libc.Int32FromInt32(4)*libc.Int32FromInt32(4)), uint32((*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum)) _sqlite3Put4byte(tls, data+uintptr(libc.Int32FromInt32(36)+libc.Int32FromInt32(7)*libc.Int32FromInt32(4)), uint32((*TBtShared)(unsafe.Pointer(pBt)).FincrVacuum)) (*TBtShared)(unsafe.Pointer(pBt)).FnPage = uint32(1) *(*uint8)(unsafe.Pointer(data + 31)) = uint8(1) return SQLITE_OK } // C documentation // // /* // ** Initialize the first page of the database file (creating a database // ** consisting of a single page and no schema objects). Return SQLITE_OK // ** if successful, or an SQLite error code otherwise. // */ func _sqlite3BtreeNewDb(tls *libc.TLS, p uintptr) (r int32) { var rc int32 _ = rc _sqlite3BtreeEnter(tls, p) (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FnPage = uint32(0) rc = _newDatabase(tls, (*TBtree)(unsafe.Pointer(p)).FpBt) _sqlite3BtreeLeave(tls, p) return rc } // C documentation // // /* // ** Attempt to start a new transaction. A write-transaction // ** is started if the second argument is nonzero, otherwise a read- // ** transaction. If the second argument is 2 or more and exclusive // ** transaction is started, meaning that no other process is allowed // ** to access the database. A preexisting transaction may not be // ** upgraded to exclusive by calling this routine a second time - the // ** exclusivity flag only works for a new transaction. // ** // ** A write-transaction must be started before attempting any // ** changes to the database. None of the following routines // ** will work unless a transaction is started first: // ** // ** sqlite3BtreeCreateTable() // ** sqlite3BtreeCreateIndex() // ** sqlite3BtreeClearTable() // ** sqlite3BtreeDropTable() // ** sqlite3BtreeInsert() // ** sqlite3BtreeDelete() // ** sqlite3BtreeUpdateMeta() // ** // ** If an initial attempt to acquire the lock fails because of lock contention // ** and the database was previously unlocked, then invoke the busy handler // ** if there is one. But if there was previously a read-lock, do not // ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is // ** returned when there is already a read-lock in order to avoid a deadlock. // ** // ** Suppose there are two processes A and B. A has a read lock and B has // ** a reserved lock. B tries to promote to exclusive but is blocked because // ** of A's read lock. A tries to promote to reserved but is blocked by B. // ** One or the other of the two processes must give way or there can be // ** no progress. By returning SQLITE_BUSY and not invoking the busy callback // ** when A already has a read lock, we encourage A to give up and let B // ** proceed. // */ func _btreeBeginTrans(tls *libc.TLS, p uintptr, wrflag int32, pSchemaVersion uintptr) (r int32) { var pBlock, pBt, pIter, pPage1, pPager, p1, p3, p4, p8, p9 uintptr var rc, v5, v7 int32 var v6 bool _, _, _, _, _, _, _, _, _, _, _, _, _, _ = pBlock, pBt, pIter, pPage1, pPager, rc, v5, v6, v7, p1, p3, p4, p8, p9 pBt = (*TBtree)(unsafe.Pointer(p)).FpBt pPager = (*TBtShared)(unsafe.Pointer(pBt)).FpPager rc = SQLITE_OK _sqlite3BtreeEnter(tls, p) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If the btree is already in a write-transaction, or it ** is already in a read-transaction and a read-transaction ** is requested, this is a no-op. */ if int32((*TBtree)(unsafe.Pointer(p)).FinTrans) == int32(TRANS_WRITE) || int32((*TBtree)(unsafe.Pointer(p)).FinTrans) == int32(TRANS_READ) && !(wrflag != 0) { goto trans_begun } _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).Fdb)).Fflags&uint64(SQLITE_ResetDatabase) != 0 && int32(_sqlite3PagerIsreadonly(tls, pPager)) == 0 { p1 = pBt + 24 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(BTS_READ_ONLY)) } /* Write transactions are not possible on a read-only database */ if int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_READ_ONLY) != 0 && wrflag != 0 { rc = int32(SQLITE_READONLY) goto trans_begun } pBlock = uintptr(0) /* If another database handle has already opened a write transaction ** on this shared-btree structure and a second write transaction is ** requested, return SQLITE_LOCKED. */ if wrflag != 0 && int32((*TBtShared)(unsafe.Pointer(pBt)).FinTransaction) == int32(TRANS_WRITE) || int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_PENDING) != 0 { pBlock = (*TBtree)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpWriter)).Fdb } else { if wrflag > int32(1) { pIter = (*TBtShared)(unsafe.Pointer(pBt)).FpLock for { if !(pIter != 0) { break } if (*TBtLock)(unsafe.Pointer(pIter)).FpBtree != p { pBlock = (*TBtree)(unsafe.Pointer((*TBtLock)(unsafe.Pointer(pIter)).FpBtree)).Fdb break } goto _2 _2: ; pIter = (*TBtLock)(unsafe.Pointer(pIter)).FpNext } } } if pBlock != 0 { _sqlite3ConnectionBlocked(tls, (*TBtree)(unsafe.Pointer(p)).Fdb, pBlock) rc = libc.Int32FromInt32(SQLITE_LOCKED) | libc.Int32FromInt32(1)<pPage1 is populated or ** lockBtree() returns something other than SQLITE_OK. lockBtree() ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after ** reading page 1 it discovers that the page-size of the database ** file is not pBt->pageSize. In this case lockBtree() will update ** pBt->pageSize to the page-size of the file on disk. */ for { if v6 = (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 == uintptr(0); v6 { v5 = _lockBtree(tls, pBt) rc = v5 } if !(v6 && SQLITE_OK == v5) { break } } if rc == SQLITE_OK && wrflag != 0 { if int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_READ_ONLY) != 0 { rc = int32(SQLITE_READONLY) } else { rc = _sqlite3PagerBegin(tls, pPager, libc.BoolInt32(wrflag > int32(1)), _sqlite3TempInMemory(tls, (*TBtree)(unsafe.Pointer(p)).Fdb)) if rc == SQLITE_OK { rc = _newDatabase(tls, pBt) } else { if rc == libc.Int32FromInt32(SQLITE_BUSY)|libc.Int32FromInt32(2)< int32((*TBtShared)(unsafe.Pointer(pBt)).FinTransaction) { (*TBtShared)(unsafe.Pointer(pBt)).FinTransaction = (*TBtree)(unsafe.Pointer(p)).FinTrans } if wrflag != 0 { pPage1 = (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 _ = libc.Int32FromInt32(0) (*TBtShared)(unsafe.Pointer(pBt)).FpWriter = p p8 = pBt + 24 *(*Tu16)(unsafe.Pointer(p8)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p8))) & ^libc.Int32FromInt32(BTS_EXCLUSIVE)) if wrflag > int32(1) { p9 = pBt + 24 *(*Tu16)(unsafe.Pointer(p9)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p9))) | libc.Int32FromInt32(BTS_EXCLUSIVE)) } /* If the db-size header field is incorrect (as it may be if an old ** client has been writing the database file), update it now. Doing ** this sooner rather than later means the database size can safely ** re-read the database size from page 1 if a savepoint or transaction ** rollback occurs within the transaction. */ if (*TBtShared)(unsafe.Pointer(pBt)).FnPage != _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+28) { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FpDbPage) if rc == SQLITE_OK { _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+28, (*TBtShared)(unsafe.Pointer(pBt)).FnPage) } } } } goto trans_begun trans_begun: ; if rc == SQLITE_OK { if pSchemaVersion != 0 { *(*int32)(unsafe.Pointer(pSchemaVersion)) = int32(_sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+40)) } if wrflag != 0 { /* This call makes sure that the pager has the correct number of ** open savepoints. If the second parameter is greater than 0 and ** the sub-journal is not already open, then it will be opened here. */ rc = _sqlite3PagerOpenSavepoint(tls, pPager, (*Tsqlite3)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).Fdb)).FnSavepoint) } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3BtreeLeave(tls, p) return rc } func _sqlite3BtreeBeginTrans(tls *libc.TLS, p uintptr, wrflag int32, pSchemaVersion uintptr) (r int32) { var pBt uintptr _ = pBt if (*TBtree)(unsafe.Pointer(p)).Fsharable != 0 || int32((*TBtree)(unsafe.Pointer(p)).FinTrans) == TRANS_NONE || int32((*TBtree)(unsafe.Pointer(p)).FinTrans) == int32(TRANS_READ) && wrflag != 0 { return _btreeBeginTrans(tls, p, wrflag, pSchemaVersion) } pBt = (*TBtree)(unsafe.Pointer(p)).FpBt if pSchemaVersion != 0 { *(*int32)(unsafe.Pointer(pSchemaVersion)) = int32(_sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+40)) } if wrflag != 0 { /* This call makes sure that the pager has the correct number of ** open savepoints. If the second parameter is greater than 0 and ** the sub-journal is not already open, then it will be opened here. */ return _sqlite3PagerOpenSavepoint(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, (*Tsqlite3)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).Fdb)).FnSavepoint) } else { return SQLITE_OK } return r } // C documentation // // /* // ** Set the pointer-map entries for all children of page pPage. Also, if // ** pPage contains cells that point to overflow pages, set the pointer // ** map entries for the overflow pages as well. // */ func _setChildPtrmaps(tls *libc.TLS, pPage uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var childPgno, childPgno1, pgno TPgno var i, nCell, v1 int32 var pBt, pCell uintptr var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _, _ = childPgno, childPgno1, i, nCell, pBt, pCell, pgno, v1 /* Return code */ pBt = (*TMemPage)(unsafe.Pointer(pPage)).FpBt pgno = (*TMemPage)(unsafe.Pointer(pPage)).Fpgno _ = libc.Int32FromInt32(0) if (*TMemPage)(unsafe.Pointer(pPage)).FisInit != 0 { v1 = SQLITE_OK } else { v1 = _btreeInitPage(tls, pPage) } *(*int32)(unsafe.Pointer(bp)) = v1 if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp)) } nCell = int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) i = 0 for { if !(i < nCell) { break } pCell = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*i)))))) _ptrmapPutOvflPtr(tls, pPage, pPage, pCell, bp) if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) { childPgno = _sqlite3Get4byte(tls, pCell) _ptrmapPut(tls, pBt, childPgno, uint8(PTRMAP_BTREE), pgno, bp) } goto _2 _2: ; i++ } if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) { childPgno1 = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8))) _ptrmapPut(tls, pBt, childPgno1, uint8(PTRMAP_BTREE), pgno, bp) } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so // ** that it points to iTo. Parameter eType describes the type of pointer to // ** be modified, as follows: // ** // ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child // ** page of pPage. // ** // ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow // ** page pointed to by one of the cells on pPage. // ** // ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next // ** overflow page in the list. // */ func _modifyPagePointer(tls *libc.TLS, pPage uintptr, iFrom TPgno, iTo TPgno, eType Tu8) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var i, nCell, rc, v1 int32 var pCell uintptr var _ /* info at bp+0 */ TCellInfo _, _, _, _, _ = i, nCell, pCell, rc, v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(eType) == int32(PTRMAP_OVERFLOW2) { /* The pointer is always the first 4 bytes of the page in this case. */ if _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData) != iFrom { return _sqlite3CorruptError(tls, int32(73995)) } _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData, iTo) } else { if (*TMemPage)(unsafe.Pointer(pPage)).FisInit != 0 { v1 = SQLITE_OK } else { v1 = _btreeInitPage(tls, pPage) } rc = v1 if rc != 0 { return rc } nCell = int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) i = 0 for { if !(i < nCell) { break } pCell = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*i)))))) if int32(eType) == int32(PTRMAP_OVERFLOW1) { (*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxParseCell})))(tls, pPage, pCell, bp) if uint32((*(*TCellInfo)(unsafe.Pointer(bp))).FnLocal) < (*(*TCellInfo)(unsafe.Pointer(bp))).FnPayload { if pCell+uintptr((*(*TCellInfo)(unsafe.Pointer(bp))).FnSize) > (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize) { return _sqlite3CorruptError(tls, int32(74014)) } if iFrom == _sqlite3Get4byte(tls, pCell+uintptr((*(*TCellInfo)(unsafe.Pointer(bp))).FnSize)-uintptr(4)) { _sqlite3Put4byte(tls, pCell+uintptr((*(*TCellInfo)(unsafe.Pointer(bp))).FnSize)-uintptr(4), iTo) break } } } else { if pCell+uintptr(4) > (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize) { return _sqlite3CorruptError(tls, int32(74023)) } if _sqlite3Get4byte(tls, pCell) == iFrom { _sqlite3Put4byte(tls, pCell, iTo) break } } goto _2 _2: ; i++ } if i == nCell { if int32(eType) != int32(PTRMAP_BTREE) || _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8))) != iFrom { return _sqlite3CorruptError(tls, int32(74035)) } _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8)), iTo) } } return SQLITE_OK } // C documentation // // /* // ** Move the open database page pDbPage to location iFreePage in the // ** database. The pDbPage reference remains valid. // ** // ** The isCommit flag indicates that there is no need to remember that // ** the journal needs to be sync()ed before database page pDbPage->pgno // ** can be written to. The caller has already promised not to write to that // ** page. // */ func _relocatePage(tls *libc.TLS, pBt uintptr, pDbPage uintptr, eType Tu8, iPtrPage TPgno, iFreePage TPgno, isCommit int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iDbPage, nextOvfl TPgno var pPager uintptr var _ /* pPtrPage at bp+0 */ uintptr var _ /* rc at bp+4 */ int32 _, _, _ = iDbPage, nextOvfl, pPager /* The page that contains a pointer to pDbPage */ iDbPage = (*TMemPage)(unsafe.Pointer(pDbPage)).Fpgno pPager = (*TBtShared)(unsafe.Pointer(pBt)).FpPager _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if iDbPage < uint32(3) { return _sqlite3CorruptError(tls, int32(74070)) } /* Move page iDbPage from its current location to page number iFreePage */ *(*int32)(unsafe.Pointer(bp + 4)) = _sqlite3PagerMovepage(tls, pPager, (*TMemPage)(unsafe.Pointer(pDbPage)).FpDbPage, iFreePage, isCommit) if *(*int32)(unsafe.Pointer(bp + 4)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 4)) } (*TMemPage)(unsafe.Pointer(pDbPage)).Fpgno = iFreePage /* If pDbPage was a btree-page, then it may have child pages and/or cells ** that point to overflow pages. The pointer map entries for all these ** pages need to be changed. ** ** If pDbPage is an overflow page, then the first 4 bytes may store a ** pointer to a subsequent overflow page. If this is the case, then ** the pointer map needs to be updated for the subsequent overflow page. */ if int32(eType) == int32(PTRMAP_BTREE) || int32(eType) == int32(PTRMAP_ROOTPAGE) { *(*int32)(unsafe.Pointer(bp + 4)) = _setChildPtrmaps(tls, pDbPage) if *(*int32)(unsafe.Pointer(bp + 4)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 4)) } } else { nextOvfl = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pDbPage)).FaData) if nextOvfl != uint32(0) { _ptrmapPut(tls, pBt, nextOvfl, uint8(PTRMAP_OVERFLOW2), iFreePage, bp+4) if *(*int32)(unsafe.Pointer(bp + 4)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 4)) } } } /* Fix the database pointer on page iPtrPage that pointed at iDbPage so ** that it points at iFreePage. Also fix the pointer map entry for ** iPtrPage. */ if int32(eType) != int32(PTRMAP_ROOTPAGE) { *(*int32)(unsafe.Pointer(bp + 4)) = _btreeGetPage(tls, pBt, iPtrPage, bp, 0) if *(*int32)(unsafe.Pointer(bp + 4)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 4)) } *(*int32)(unsafe.Pointer(bp + 4)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpDbPage) if *(*int32)(unsafe.Pointer(bp + 4)) != SQLITE_OK { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) return *(*int32)(unsafe.Pointer(bp + 4)) } *(*int32)(unsafe.Pointer(bp + 4)) = _modifyPagePointer(tls, *(*uintptr)(unsafe.Pointer(bp)), iDbPage, iFreePage, eType) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK { _ptrmapPut(tls, pBt, iFreePage, eType, iPtrPage, bp+4) } } return *(*int32)(unsafe.Pointer(bp + 4)) } // C documentation // // /* // ** Perform a single step of an incremental-vacuum. If successful, return // ** SQLITE_OK. If there is no work to do (and therefore no point in // ** calling this function again), return SQLITE_DONE. Or, if an error // ** occurs, return some other error code. // ** // ** More specifically, this function attempts to re-organize the database so // ** that the last page of the file currently in use is no longer in use. // ** // ** Parameter nFin is the number of pages that this database would contain // ** were this function called until it returns SQLITE_DONE. // ** // ** If the bCommit parameter is non-zero, this function assumes that the // ** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE // ** or an error. bCommit is passed true for an auto-vacuum-on-commit // ** operation, or false for an incremental vacuum. // */ func _incrVacuumStep(tls *libc.TLS, pBt uintptr, nFin TPgno, iLastPg TPgno, bCommit int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var dbSize, iNear, nFreeList TPgno var eMode Tu8 var rc int32 var _ /* eType at bp+0 */ Tu8 var _ /* iFreePg at bp+16 */ TPgno var _ /* iFreePg at bp+8 */ TPgno var _ /* iPtrPage at bp+4 */ TPgno var _ /* pFreePg at bp+12 */ uintptr var _ /* pFreePg at bp+24 */ uintptr var _ /* pLastPg at bp+20 */ uintptr _, _, _, _, _ = dbSize, eMode, iNear, nFreeList, rc _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if !(_ptrmapPageno(tls, pBt, iLastPg) == iLastPg) && iLastPg != uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) { nFreeList = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+36) if nFreeList == uint32(0) { return int32(SQLITE_DONE) } rc = _ptrmapGet(tls, pBt, iLastPg, bp, bp+4) if rc != SQLITE_OK { return rc } if int32(*(*Tu8)(unsafe.Pointer(bp))) == int32(PTRMAP_ROOTPAGE) { return _sqlite3CorruptError(tls, int32(74168)) } if int32(*(*Tu8)(unsafe.Pointer(bp))) == int32(PTRMAP_FREEPAGE) { if bCommit == 0 { rc = _allocateBtreePage(tls, pBt, bp+12, bp+8, iLastPg, uint8(BTALLOC_EXACT)) if rc != SQLITE_OK { return rc } _ = libc.Int32FromInt32(0) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) } } else { eMode = uint8(BTALLOC_ANY) /* Mode parameter for allocateBtreePage() */ iNear = uint32(0) /* nearby parameter for allocateBtreePage() */ rc = _btreeGetPage(tls, pBt, iLastPg, bp+20, 0) if rc != SQLITE_OK { return rc } /* If bCommit is zero, this loop runs exactly once and page pLastPg ** is swapped with the first free page pulled off the free list. ** ** On the other hand, if bCommit is greater than zero, then keep ** looping until a free-page located within the first nFin pages ** of the file is found. */ if bCommit == 0 { eMode = uint8(BTALLOC_LE) iNear = nFin } for cond := true; cond; cond = bCommit != 0 && *(*TPgno)(unsafe.Pointer(bp + 16)) > nFin { dbSize = _btreePagecount(tls, pBt) rc = _allocateBtreePage(tls, pBt, bp+24, bp+16, iNear, eMode) if rc != SQLITE_OK { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 20))) return rc } _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 24))) if *(*TPgno)(unsafe.Pointer(bp + 16)) > dbSize { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 20))) return _sqlite3CorruptError(tls, int32(74220)) } } _ = libc.Int32FromInt32(0) rc = _relocatePage(tls, pBt, *(*uintptr)(unsafe.Pointer(bp + 20)), *(*Tu8)(unsafe.Pointer(bp)), *(*TPgno)(unsafe.Pointer(bp + 4)), *(*TPgno)(unsafe.Pointer(bp + 16)), bCommit) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 20))) if rc != SQLITE_OK { return rc } } } if bCommit == 0 { for cond := true; cond; cond = iLastPg == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) || _ptrmapPageno(tls, pBt, iLastPg) == iLastPg { iLastPg-- } (*TBtShared)(unsafe.Pointer(pBt)).FbDoTruncate = uint8(1) (*TBtShared)(unsafe.Pointer(pBt)).FnPage = iLastPg } return SQLITE_OK } // C documentation // // /* // ** The database opened by the first argument is an auto-vacuum database // ** nOrig pages in size containing nFree free pages. Return the expected // ** size of the database in pages following an auto-vacuum operation. // */ func _finalDbSize(tls *libc.TLS, pBt uintptr, nOrig TPgno, nFree TPgno) (r TPgno) { var nEntry int32 var nFin, nPtrmap TPgno _, _, _ = nEntry, nFin, nPtrmap /* Return value */ nEntry = int32((*TBtShared)(unsafe.Pointer(pBt)).FusableSize / uint32(5)) nPtrmap = (nFree - nOrig + _ptrmapPageno(tls, pBt, nOrig) + uint32(nEntry)) / uint32(nEntry) nFin = nOrig - nFree - nPtrmap if nOrig > uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) && nFin < uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) { nFin-- } for _ptrmapPageno(tls, pBt, nFin) == nFin || nFin == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) { nFin-- } return nFin } // C documentation // // /* // ** A write-transaction must be opened before calling this function. // ** It performs a single unit of work towards an incremental vacuum. // ** // ** If the incremental vacuum is finished after this function has run, // ** SQLITE_DONE is returned. If it is not finished, but no error occurred, // ** SQLITE_OK is returned. Otherwise an SQLite error code. // */ func _sqlite3BtreeIncrVacuum(tls *libc.TLS, p uintptr) (r int32) { var nFin, nFree, nOrig TPgno var pBt uintptr var rc int32 _, _, _, _, _ = nFin, nFree, nOrig, pBt, rc pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _sqlite3BtreeEnter(tls, p) _ = libc.Int32FromInt32(0) if !((*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0) { rc = int32(SQLITE_DONE) } else { nOrig = _btreePagecount(tls, pBt) nFree = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+36) nFin = _finalDbSize(tls, pBt, nOrig, nFree) if nOrig < nFin || nFree >= nOrig { rc = _sqlite3CorruptError(tls, int32(74288)) } else { if nFree > uint32(0) { rc = _saveAllCursors(tls, pBt, uint32(0), uintptr(0)) if rc == SQLITE_OK { _invalidateAllOverflowCache(tls, pBt) rc = _incrVacuumStep(tls, pBt, nFin, nOrig, 0) } if rc == SQLITE_OK { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FpDbPage) _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+28, (*TBtShared)(unsafe.Pointer(pBt)).FnPage) } } else { rc = int32(SQLITE_DONE) } } } _sqlite3BtreeLeave(tls, p) return rc } // C documentation // // /* // ** This routine is called prior to sqlite3PagerCommit when a transaction // ** is committed for an auto-vacuum database. // */ func _autoVacuumCommit(tls *libc.TLS, p uintptr) (r int32) { var db, pBt, pPager uintptr var iDb, rc int32 var iFree, nFin, nFree, nOrig, nVac TPgno _, _, _, _, _, _, _, _, _, _ = db, iDb, iFree, nFin, nFree, nOrig, nVac, pBt, pPager, rc rc = SQLITE_OK _ = libc.Int32FromInt32(0) pBt = (*TBtree)(unsafe.Pointer(p)).FpBt pPager = (*TBtShared)(unsafe.Pointer(pBt)).FpPager _ = libc.Int32FromInt32(0) _invalidateAllOverflowCache(tls, pBt) _ = libc.Int32FromInt32(0) if !((*TBtShared)(unsafe.Pointer(pBt)).FincrVacuum != 0) { /* Database size before freeing */ nOrig = _btreePagecount(tls, pBt) if _ptrmapPageno(tls, pBt, nOrig) == nOrig || nOrig == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) { /* It is not possible to create a database for which the final page ** is either a pointer-map page or the pending-byte page. If one ** is encountered, this indicates corruption. */ return _sqlite3CorruptError(tls, int32(74339)) } nFree = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+36) db = (*TBtree)(unsafe.Pointer(p)).Fdb if (*Tsqlite3)(unsafe.Pointer(db)).FxAutovacPages != 0 { iDb = 0 for { if !(iDb < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt == p { break } goto _1 _1: ; iDb++ } nVac = (*(*func(*libc.TLS, uintptr, uintptr, Tu32, Tu32, Tu32) uint32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxAutovacPages})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpAutovacPagesArg, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, nOrig, nFree, (*TBtShared)(unsafe.Pointer(pBt)).FpageSize) if nVac > nFree { nVac = nFree } if nVac == uint32(0) { return SQLITE_OK } } else { nVac = nFree } nFin = _finalDbSize(tls, pBt, nOrig, nVac) if nFin > nOrig { return _sqlite3CorruptError(tls, int32(74366)) } if nFin < nOrig { rc = _saveAllCursors(tls, pBt, uint32(0), uintptr(0)) } iFree = nOrig for { if !(iFree > nFin && rc == SQLITE_OK) { break } rc = _incrVacuumStep(tls, pBt, nFin, iFree, libc.BoolInt32(nVac == nFree)) goto _2 _2: ; iFree-- } if (rc == int32(SQLITE_DONE) || rc == SQLITE_OK) && nFree > uint32(0) { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FpDbPage) if nVac == nFree { _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+32, uint32(0)) _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+36, uint32(0)) } _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+28, nFin) (*TBtShared)(unsafe.Pointer(pBt)).FbDoTruncate = uint8(1) (*TBtShared)(unsafe.Pointer(pBt)).FnPage = nFin } if rc != SQLITE_OK { _sqlite3PagerRollback(tls, pPager) } } _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** This routine does the first phase of a two-phase commit. This routine // ** causes a rollback journal to be created (if it does not already exist) // ** and populated with enough information so that if a power loss occurs // ** the database can be restored to its original state by playing back // ** the journal. Then the contents of the journal are flushed out to // ** the disk. After the journal is safely on oxide, the changes to the // ** database are written into the database file and flushed to oxide. // ** At the end of this call, the rollback journal still exists on the // ** disk and we are still holding all locks, so the transaction has not // ** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the // ** commit process. // ** // ** This call is a no-op if no write-transaction is currently active on pBt. // ** // ** Otherwise, sync the database file for the btree pBt. zSuperJrnl points to // ** the name of a super-journal file that should be written into the // ** individual journal file, or is NULL, indicating no super-journal file // ** (single database transaction). // ** // ** When this is called, the super-journal should already have been // ** created, populated with this journal pointer and synced to disk. // ** // ** Once this is routine has returned, the only thing required to commit // ** the write-transaction for this database file is to delete the journal. // */ func _sqlite3BtreeCommitPhaseOne(tls *libc.TLS, p uintptr, zSuperJrnl uintptr) (r int32) { var pBt uintptr var rc int32 _, _ = pBt, rc rc = SQLITE_OK if int32((*TBtree)(unsafe.Pointer(p)).FinTrans) == int32(TRANS_WRITE) { pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _sqlite3BtreeEnter(tls, p) if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { rc = _autoVacuumCommit(tls, p) if rc != SQLITE_OK { _sqlite3BtreeLeave(tls, p) return rc } } if (*TBtShared)(unsafe.Pointer(pBt)).FbDoTruncate != 0 { _sqlite3PagerTruncateImage(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, (*TBtShared)(unsafe.Pointer(pBt)).FnPage) } rc = _sqlite3PagerCommitPhaseOne(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, zSuperJrnl, 0) _sqlite3BtreeLeave(tls, p) } return rc } // C documentation // // /* // ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback() // ** at the conclusion of a transaction. // */ func _btreeEndTransaction(tls *libc.TLS, p uintptr) { var db, pBt uintptr _, _ = db, pBt pBt = (*TBtree)(unsafe.Pointer(p)).FpBt db = (*TBtree)(unsafe.Pointer(p)).Fdb _ = libc.Int32FromInt32(0) (*TBtShared)(unsafe.Pointer(pBt)).FbDoTruncate = uint8(0) if int32((*TBtree)(unsafe.Pointer(p)).FinTrans) > TRANS_NONE && (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeRead > int32(1) { /* If there are other active statements that belong to this database ** handle, downgrade to a read-only transaction. The other statements ** may still be reading from the database. */ _downgradeAllSharedCacheTableLocks(tls, p) (*TBtree)(unsafe.Pointer(p)).FinTrans = uint8(TRANS_READ) } else { /* If the handle had any kind of transaction open, decrement the ** transaction count of the shared btree. If the transaction count ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused() ** call below will unlock the pager. */ if int32((*TBtree)(unsafe.Pointer(p)).FinTrans) != TRANS_NONE { _clearAllSharedCacheTableLocks(tls, p) (*TBtShared)(unsafe.Pointer(pBt)).FnTransaction-- if 0 == (*TBtShared)(unsafe.Pointer(pBt)).FnTransaction { (*TBtShared)(unsafe.Pointer(pBt)).FinTransaction = uint8(TRANS_NONE) } } /* Set the current transaction state to TRANS_NONE and unlock the ** pager if this call closed the only read or write transaction. */ (*TBtree)(unsafe.Pointer(p)).FinTrans = uint8(TRANS_NONE) _unlockBtreeIfUnused(tls, pBt) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } // C documentation // // /* // ** Commit the transaction currently in progress. // ** // ** This routine implements the second phase of a 2-phase commit. The // ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should // ** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne() // ** routine did all the work of writing information out to disk and flushing the // ** contents so that they are written onto the disk platter. All this // ** routine has to do is delete or truncate or zero the header in the // ** the rollback journal (which causes the transaction to commit) and // ** drop locks. // ** // ** Normally, if an error occurs while the pager layer is attempting to // ** finalize the underlying journal file, this function returns an error and // ** the upper layer will attempt a rollback. However, if the second argument // ** is non-zero then this b-tree transaction is part of a multi-file // ** transaction. In this case, the transaction has already been committed // ** (by deleting a super-journal file) and the caller will ignore this // ** functions return code. So, even if an error occurs in the pager layer, // ** reset the b-tree objects internal state to indicate that the write // ** transaction has been closed. This is quite safe, as the pager will have // ** transitioned to the error state. // ** // ** This will release the write lock on the database file. If there // ** are no active cursors, it also releases the read lock. // */ func _sqlite3BtreeCommitPhaseTwo(tls *libc.TLS, p uintptr, bCleanup int32) (r int32) { var pBt uintptr var rc int32 _, _ = pBt, rc if int32((*TBtree)(unsafe.Pointer(p)).FinTrans) == TRANS_NONE { return SQLITE_OK } _sqlite3BtreeEnter(tls, p) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If the handle has a write-transaction open, commit the shared-btrees ** transaction and set the shared state to TRANS_READ. */ if int32((*TBtree)(unsafe.Pointer(p)).FinTrans) == int32(TRANS_WRITE) { pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3PagerCommitPhaseTwo(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager) if rc != SQLITE_OK && bCleanup == 0 { _sqlite3BtreeLeave(tls, p) return rc } (*TBtree)(unsafe.Pointer(p)).FiBDataVersion-- /* Compensate for pPager->iDataVersion++; */ (*TBtShared)(unsafe.Pointer(pBt)).FinTransaction = uint8(TRANS_READ) _btreeClearHasContent(tls, pBt) } _btreeEndTransaction(tls, p) _sqlite3BtreeLeave(tls, p) return SQLITE_OK } // C documentation // // /* // ** Do both phases of a commit. // */ func _sqlite3BtreeCommit(tls *libc.TLS, p uintptr) (r int32) { var rc int32 _ = rc _sqlite3BtreeEnter(tls, p) rc = _sqlite3BtreeCommitPhaseOne(tls, p, uintptr(0)) if rc == SQLITE_OK { rc = _sqlite3BtreeCommitPhaseTwo(tls, p, 0) } _sqlite3BtreeLeave(tls, p) return rc } // C documentation // // /* // ** This routine sets the state to CURSOR_FAULT and the error // ** code to errCode for every cursor on any BtShared that pBtree // ** references. Or if the writeOnly flag is set to 1, then only // ** trip write cursors and leave read cursors unchanged. // ** // ** Every cursor is a candidate to be tripped, including cursors // ** that belong to other database connections that happen to be // ** sharing the cache with pBtree. // ** // ** This routine gets called when a rollback occurs. If the writeOnly // ** flag is true, then only write-cursors need be tripped - read-only // ** cursors save their current positions so that they may continue // ** following the rollback. Or, if writeOnly is false, all cursors are // ** tripped. In general, writeOnly is false if the transaction being // ** rolled back modified the database schema. In this case b-tree root // ** pages may be moved or deleted from the database altogether, making // ** it unsafe for read cursors to continue. // ** // ** If the writeOnly flag is true and an error is encountered while // ** saving the current position of a read-only cursor, all cursors, // ** including all read-cursors are tripped. // ** // ** SQLITE_OK is returned if successful, or if an error occurs while // ** saving a cursor position, an SQLite error code. // */ func _sqlite3BtreeTripAllCursors(tls *libc.TLS, pBtree uintptr, errCode int32, writeOnly int32) (r int32) { var p uintptr var rc int32 _, _ = p, rc rc = SQLITE_OK _ = libc.Int32FromInt32(0) if pBtree != 0 { _sqlite3BtreeEnter(tls, pBtree) p = (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(pBtree)).FpBt)).FpCursor for { if !(p != 0) { break } if writeOnly != 0 && int32((*TBtCursor)(unsafe.Pointer(p)).FcurFlags)&int32(BTCF_WriteFlag) == 0 { if int32((*TBtCursor)(unsafe.Pointer(p)).FeState) == CURSOR_VALID || int32((*TBtCursor)(unsafe.Pointer(p)).FeState) == int32(CURSOR_SKIPNEXT) { rc = _saveCursorPosition(tls, p) if rc != SQLITE_OK { _sqlite3BtreeTripAllCursors(tls, pBtree, rc, 0) break } } } else { _sqlite3BtreeClearCursor(tls, p) (*TBtCursor)(unsafe.Pointer(p)).FeState = uint8(CURSOR_FAULT) (*TBtCursor)(unsafe.Pointer(p)).FskipNext = errCode } _btreeReleaseAllCursorPages(tls, p) goto _1 _1: ; p = (*TBtCursor)(unsafe.Pointer(p)).FpNext } _sqlite3BtreeLeave(tls, pBtree) } return rc } // C documentation // // /* // ** Set the pBt->nPage field correctly, according to the current // ** state of the database. Assume pBt->pPage1 is valid. // */ func _btreeSetNPage(tls *libc.TLS, pBt uintptr, pPage1 uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* nPage at bp+0 */ int32 *(*int32)(unsafe.Pointer(bp)) = int32(_sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+28)) if *(*int32)(unsafe.Pointer(bp)) == 0 { _sqlite3PagerPagecount(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, bp) } (*TBtShared)(unsafe.Pointer(pBt)).FnPage = uint32(*(*int32)(unsafe.Pointer(bp))) } // C documentation // // /* // ** Rollback the transaction in progress. // ** // ** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped). // ** Only write cursors are tripped if writeOnly is true but all cursors are // ** tripped if writeOnly is false. Any attempt to use // ** a tripped cursor will result in an error. // ** // ** This will release the write lock on the database file. If there // ** are no active cursors, it also releases the read lock. // */ func _sqlite3BtreeRollback(tls *libc.TLS, p uintptr, tripCode int32, writeOnly int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pBt uintptr var rc, rc2, rc21, v1 int32 var _ /* pPage1 at bp+0 */ uintptr _, _, _, _, _ = pBt, rc, rc2, rc21, v1 pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3BtreeEnter(tls, p) if tripCode == SQLITE_OK { v1 = _saveAllCursors(tls, pBt, uint32(0), uintptr(0)) tripCode = v1 rc = v1 if rc != 0 { writeOnly = 0 } } else { rc = SQLITE_OK } if tripCode != 0 { rc2 = _sqlite3BtreeTripAllCursors(tls, p, tripCode, writeOnly) _ = libc.Int32FromInt32(0) if rc2 != SQLITE_OK { rc = rc2 } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TBtree)(unsafe.Pointer(p)).FinTrans) == int32(TRANS_WRITE) { _ = libc.Int32FromInt32(0) rc21 = _sqlite3PagerRollback(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager) if rc21 != SQLITE_OK { rc = rc21 } /* The rollback may have destroyed the pPage1->aData value. So ** call btreeGetPage() on page 1 again to make ** sure pPage1->aData is set correctly. */ if _btreeGetPage(tls, pBt, uint32(1), bp, 0) == SQLITE_OK { _btreeSetNPage(tls, pBt, *(*uintptr)(unsafe.Pointer(bp))) _releasePageOne(tls, *(*uintptr)(unsafe.Pointer(bp))) } _ = libc.Int32FromInt32(0) (*TBtShared)(unsafe.Pointer(pBt)).FinTransaction = uint8(TRANS_READ) _btreeClearHasContent(tls, pBt) } _btreeEndTransaction(tls, p) _sqlite3BtreeLeave(tls, p) return rc } // C documentation // // /* // ** Start a statement subtransaction. The subtransaction can be rolled // ** back independently of the main transaction. You must start a transaction // ** before starting a subtransaction. The subtransaction is ended automatically // ** if the main transaction commits or rolls back. // ** // ** Statement subtransactions are used around individual SQL statements // ** that are contained within a BEGIN...COMMIT block. If a constraint // ** error occurs within the statement, the effect of that one statement // ** can be rolled back without having to rollback the entire transaction. // ** // ** A statement sub-transaction is implemented as an anonymous savepoint. The // ** value passed as the second parameter is the total number of savepoints, // ** including the new anonymous savepoint, open on the B-Tree. i.e. if there // ** are no active savepoints and no other statement-transactions open, // ** iStatement is 1. This anonymous savepoint can be released or rolled back // ** using the sqlite3BtreeSavepoint() function. // */ func _sqlite3BtreeBeginStmt(tls *libc.TLS, p uintptr, iStatement int32) (r int32) { var pBt uintptr var rc int32 _, _ = pBt, rc pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _sqlite3BtreeEnter(tls, p) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* At the pager level, a statement transaction is a savepoint with ** an index greater than all savepoints created explicitly using ** SQL statements. It is illegal to open, release or rollback any ** such savepoints while the statement transaction savepoint is active. */ rc = _sqlite3PagerOpenSavepoint(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, iStatement) _sqlite3BtreeLeave(tls, p) return rc } // C documentation // // /* // ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK // ** or SAVEPOINT_RELEASE. This function either releases or rolls back the // ** savepoint identified by parameter iSavepoint, depending on the value // ** of op. // ** // ** Normally, iSavepoint is greater than or equal to zero. However, if op is // ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the // ** contents of the entire transaction are rolled back. This is different // ** from a normal transaction rollback, as no locks are released and the // ** transaction remains open. // */ func _sqlite3BtreeSavepoint(tls *libc.TLS, p uintptr, op int32, iSavepoint int32) (r int32) { var pBt uintptr var rc int32 _, _ = pBt, rc rc = SQLITE_OK if p != 0 && int32((*TBtree)(unsafe.Pointer(p)).FinTrans) == int32(TRANS_WRITE) { pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3BtreeEnter(tls, p) if op == int32(SAVEPOINT_ROLLBACK) { rc = _saveAllCursors(tls, pBt, uint32(0), uintptr(0)) } if rc == SQLITE_OK { rc = _sqlite3PagerSavepoint(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, op, iSavepoint) } if rc == SQLITE_OK { if iSavepoint < 0 && int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_INITIALLY_EMPTY) != 0 { (*TBtShared)(unsafe.Pointer(pBt)).FnPage = uint32(0) } rc = _newDatabase(tls, pBt) _btreeSetNPage(tls, pBt, (*TBtShared)(unsafe.Pointer(pBt)).FpPage1) /* pBt->nPage might be zero if the database was corrupt when ** the transaction was started. Otherwise, it must be at least 1. */ _ = libc.Int32FromInt32(0) } _sqlite3BtreeLeave(tls, p) } return rc } // C documentation // // /* // ** Create a new cursor for the BTree whose root is on the page // ** iTable. If a read-only cursor is requested, it is assumed that // ** the caller already has at least a read-only transaction open // ** on the database already. If a write-cursor is requested, then // ** the caller is assumed to have an open write transaction. // ** // ** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only // ** be used for reading. If the BTREE_WRCSR bit is set, then the cursor // ** can be used for reading or for writing if other conditions for writing // ** are also met. These are the conditions that must be met in order // ** for writing to be allowed: // ** // ** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR // ** // ** 2: Other database connections that share the same pager cache // ** but which are not in the READ_UNCOMMITTED state may not have // ** cursors open with wrFlag==0 on the same table. Otherwise // ** the changes made by this write cursor would be visible to // ** the read cursors in the other database connection. // ** // ** 3: The database must be writable (not on read-only media) // ** // ** 4: There must be an active transaction. // ** // ** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR // ** is set. If FORDELETE is set, that is a hint to the implementation that // ** this cursor will only be used to seek to and delete entries of an index // ** as part of a larger DELETE statement. The FORDELETE hint is not used by // ** this implementation. But in a hypothetical alternative storage engine // ** in which index entries are automatically deleted when corresponding table // ** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE // ** operations on this cursor can be no-ops and all READ operations can // ** return a null row (2-bytes: 0x01 0x00). // ** // ** No checking is done to make sure that page iTable really is the // ** root page of a b-tree. If it is not, then the cursor acquired // ** will not work correctly. // ** // ** It is assumed that the sqlite3BtreeCursorZero() has been called // ** on pCur to initialize the memory space prior to invoking this routine. // */ func _btreeCursor(tls *libc.TLS, p uintptr, iTable TPgno, wrFlag int32, pKeyInfo uintptr, pCur uintptr) (r int32) { var pBt, pX, p2, p3 uintptr _, _, _, _ = pBt, pX, p2, p3 pBt = (*TBtree)(unsafe.Pointer(p)).FpBt /* Looping over other all cursors */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* The following assert statements verify that if this is a sharable ** b-tree database, the connection is holding the required table locks, ** and that no other connection has any open cursor that conflicts with ** this lock. The iTable<1 term disables the check for corrupt schemas. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Assert that the caller has opened the required transaction. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if iTable <= uint32(1) { if iTable < uint32(1) { return _sqlite3CorruptError(tls, int32(74830)) } else { if _btreePagecount(tls, pBt) == uint32(0) { _ = libc.Int32FromInt32(0) iTable = uint32(0) } } } /* Now that no other errors can occur, finish filling in the BtCursor ** variables and link the cursor into the BtShared list. */ (*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot = iTable (*TBtCursor)(unsafe.Pointer(pCur)).FiPage = int8(-int32(1)) (*TBtCursor)(unsafe.Pointer(pCur)).FpKeyInfo = pKeyInfo (*TBtCursor)(unsafe.Pointer(pCur)).FpBtree = p (*TBtCursor)(unsafe.Pointer(pCur)).FpBt = pBt (*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags = uint8(0) /* If there are two or more cursors on the same btree, then all such ** cursors *must* have the BTCF_Multiple flag set. */ pX = (*TBtShared)(unsafe.Pointer(pBt)).FpCursor for { if !(pX != 0) { break } if (*TBtCursor)(unsafe.Pointer(pX)).FpgnoRoot == iTable { p2 = pX + 1 *(*Tu8)(unsafe.Pointer(p2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p2))) | libc.Int32FromInt32(BTCF_Multiple)) (*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags = uint8(BTCF_Multiple) } goto _1 _1: ; pX = (*TBtCursor)(unsafe.Pointer(pX)).FpNext } (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_INVALID) (*TBtCursor)(unsafe.Pointer(pCur)).FpNext = (*TBtShared)(unsafe.Pointer(pBt)).FpCursor (*TBtShared)(unsafe.Pointer(pBt)).FpCursor = pCur if wrFlag != 0 { p3 = pCur + 1 *(*Tu8)(unsafe.Pointer(p3)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p3))) | libc.Int32FromInt32(BTCF_WriteFlag)) (*TBtCursor)(unsafe.Pointer(pCur)).FcurPagerFlags = uint8(0) if (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace == uintptr(0) { return _allocateTempSpace(tls, pBt) } } else { (*TBtCursor)(unsafe.Pointer(pCur)).FcurPagerFlags = uint8(PAGER_GET_READONLY) } return SQLITE_OK } func _btreeCursorWithLock(tls *libc.TLS, p uintptr, iTable TPgno, wrFlag int32, pKeyInfo uintptr, pCur uintptr) (r int32) { var rc int32 _ = rc _sqlite3BtreeEnter(tls, p) rc = _btreeCursor(tls, p, iTable, wrFlag, pKeyInfo, pCur) _sqlite3BtreeLeave(tls, p) return rc } func _sqlite3BtreeCursor(tls *libc.TLS, p uintptr, iTable TPgno, wrFlag int32, pKeyInfo uintptr, pCur uintptr) (r int32) { if (*TBtree)(unsafe.Pointer(p)).Fsharable != 0 { return _btreeCursorWithLock(tls, p, iTable, wrFlag, pKeyInfo, pCur) } else { return _btreeCursor(tls, p, iTable, wrFlag, pKeyInfo, pCur) } return r } // C documentation // // /* // ** Return the size of a BtCursor object in bytes. // ** // ** This interfaces is needed so that users of cursors can preallocate // ** sufficient storage to hold a cursor. The BtCursor object is opaque // ** to users so they cannot do the sizeof() themselves - they must call // ** this routine. // */ func _sqlite3BtreeCursorSize(tls *libc.TLS) (r int32) { return int32((libc.Uint32FromInt64(200) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7))) } // C documentation // // /* // ** Initialize memory that will be converted into a BtCursor object. // ** // ** The simple approach here would be to memset() the entire object // ** to zero. But it turns out that the apPage[] and aiIdx[] arrays // ** do not need to be zeroed and they are large, so we can save a lot // ** of run-time by skipping the initialization of those elements. // */ func _sqlite3BtreeCursorZero(tls *libc.TLS, p uintptr) { libc.Xmemset(tls, p, 0, uint32(libc.UintptrFromInt32(0)+20)) } // C documentation // // /* // ** Close a cursor. The read lock on the database file is released // ** when the last cursor is closed. // */ func _sqlite3BtreeCloseCursor(tls *libc.TLS, pCur uintptr) (r int32) { var pBt, pBtree, pPrev uintptr _, _, _ = pBt, pBtree, pPrev pBtree = (*TBtCursor)(unsafe.Pointer(pCur)).FpBtree if pBtree != 0 { pBt = (*TBtCursor)(unsafe.Pointer(pCur)).FpBt _sqlite3BtreeEnter(tls, pBtree) _ = libc.Int32FromInt32(0) if (*TBtShared)(unsafe.Pointer(pBt)).FpCursor == pCur { (*TBtShared)(unsafe.Pointer(pBt)).FpCursor = (*TBtCursor)(unsafe.Pointer(pCur)).FpNext } else { pPrev = (*TBtShared)(unsafe.Pointer(pBt)).FpCursor for cond := true; cond; cond = pPrev != 0 { if (*TBtCursor)(unsafe.Pointer(pPrev)).FpNext == pCur { (*TBtCursor)(unsafe.Pointer(pPrev)).FpNext = (*TBtCursor)(unsafe.Pointer(pCur)).FpNext break } pPrev = (*TBtCursor)(unsafe.Pointer(pPrev)).FpNext } } _btreeReleaseAllCursorPages(tls, pCur) _unlockBtreeIfUnused(tls, pBt) Xsqlite3_free(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow) Xsqlite3_free(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpKey) if int32((*TBtShared)(unsafe.Pointer(pBt)).FopenFlags)&int32(BTREE_SINGLE) != 0 && (*TBtShared)(unsafe.Pointer(pBt)).FpCursor == uintptr(0) { /* Since the BtShared is not sharable, there is no need to ** worry about the missing sqlite3BtreeLeave() call here. */ _ = libc.Int32FromInt32(0) _sqlite3BtreeClose(tls, pBtree) } else { _sqlite3BtreeLeave(tls, pBtree) } (*TBtCursor)(unsafe.Pointer(pCur)).FpBtree = uintptr(0) } return SQLITE_OK } // C documentation // // /* // ** Make sure the BtCursor* given in the argument has a valid // ** BtCursor.info structure. If it is not already valid, call // ** btreeParseCell() to fill it in. // ** // ** BtCursor.info is a cache of the information in the current cell. // ** Using this cache reduces the number of calls to btreeParseCell(). // */ func _getCellInfo(tls *libc.TLS, pCur uintptr) { var p1 uintptr _ = p1 if int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize) == 0 { p1 = pCur + 1 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | libc.Int32FromInt32(BTCF_ValidNKey)) _btreeParseCell(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpPage, int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix), pCur+32) } else { } } func _sqlite3BtreeCursorIsValidNN(tls *libc.TLS, pCur uintptr) (r int32) { _ = libc.Int32FromInt32(0) return libc.BoolInt32(int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) == CURSOR_VALID) } // C documentation // // /* // ** Return the value of the integer key or "rowid" for a table btree. // ** This routine is only valid for a cursor that is pointing into a // ** ordinary table btree. If the cursor points to an index btree or // ** is invalid, the result of this routine is undefined. // */ func _sqlite3BtreeIntegerKey(tls *libc.TLS, pCur uintptr) (r Ti64) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _getCellInfo(tls, pCur) return (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey } // C documentation // // /* // ** Pin or unpin a cursor. // */ func _sqlite3BtreeCursorPin(tls *libc.TLS, pCur uintptr) { var p1 uintptr _ = p1 _ = libc.Int32FromInt32(0) p1 = pCur + 1 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | libc.Int32FromInt32(BTCF_Pinned)) } func _sqlite3BtreeCursorUnpin(tls *libc.TLS, pCur uintptr) { var p1 uintptr _ = p1 _ = libc.Int32FromInt32(0) p1 = pCur + 1 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(BTCF_Pinned)) } // C documentation // // /* // ** Return the offset into the database file for the start of the // ** payload to which the cursor is pointing. // */ func _sqlite3BtreeOffset(tls *libc.TLS, pCur uintptr) (r Ti64) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _getCellInfo(tls, pCur) return int64((*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FpageSize)*(int64((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).Fpgno)-int64(1)) + int64(int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload)-int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FaData)) } // C documentation // // /* // ** Return the number of bytes of payload for the entry that pCur is // ** currently pointing to. For table btrees, this will be the amount // ** of data. For index btrees, this will be the size of the key. // ** // ** The caller must guarantee that the cursor is pointing to a non-NULL // ** valid entry. In other words, the calling procedure must guarantee // ** that the cursor has Cursor.eState==CURSOR_VALID. // */ func _sqlite3BtreePayloadSize(tls *libc.TLS, pCur uintptr) (r Tu32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _getCellInfo(tls, pCur) return (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnPayload } // C documentation // // /* // ** Return an upper bound on the size of any record for the table // ** that the cursor is pointing into. // ** // ** This is an optimization. Everything will still work if this // ** routine always returns 2147483647 (which is the largest record // ** that SQLite can handle) or more. But returning a smaller value might // ** prevent large memory allocations when trying to interpret a // ** corrupt database. // ** // ** The current implementation merely returns the size of the underlying // ** database file. // */ func _sqlite3BtreeMaxRecordSize(tls *libc.TLS, pCur uintptr) (r Tsqlite3_int64) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return int64((*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FpageSize) * int64((*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FnPage) } // C documentation // // /* // ** Given the page number of an overflow page in the database (parameter // ** ovfl), this function finds the page number of the next page in the // ** linked list of overflow pages. If possible, it uses the auto-vacuum // ** pointer-map data instead of reading the content of page ovfl to do so. // ** // ** If an error occurs an SQLite error code is returned. Otherwise: // ** // ** The page number of the next overflow page in the linked list is // ** written to *pPgnoNext. If page ovfl is the last page in its linked // ** list, *pPgnoNext is set to zero. // ** // ** If ppPage is not NULL, and a reference to the MemPage object corresponding // ** to page number pOvfl was obtained, then *ppPage is set to point to that // ** reference. It is the responsibility of the caller to call releasePage() // ** on *ppPage to free the reference. In no reference was obtained (because // ** the pointer-map was used to obtain the value for *pPgnoNext), then // ** *ppPage is set to zero. // */ func _getOverflowPage(tls *libc.TLS, pBt uintptr, ovfl TPgno, ppPage uintptr, pPgnoNext uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iGuess, next TPgno var rc, v1, v2 int32 var _ /* eType at bp+8 */ Tu8 var _ /* pPage at bp+0 */ uintptr var _ /* pgno at bp+4 */ TPgno _, _, _, _, _ = iGuess, next, rc, v1, v2 next = uint32(0) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Try to find the next page in the overflow list using the ** autovacuum pointer-map pages. Guess that the next page in ** the overflow list is page number (ovfl+1). If that guess turns ** out to be wrong, fall back to loading the data of page ** number ovfl to determine the next page number. */ if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { iGuess = ovfl + uint32(1) for _ptrmapPageno(tls, pBt, iGuess) == iGuess || iGuess == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) { iGuess++ } if iGuess <= _btreePagecount(tls, pBt) { rc = _ptrmapGet(tls, pBt, iGuess, bp+8, bp+4) if rc == SQLITE_OK && int32(*(*Tu8)(unsafe.Pointer(bp + 8))) == int32(PTRMAP_OVERFLOW2) && *(*TPgno)(unsafe.Pointer(bp + 4)) == ovfl { next = iGuess rc = int32(SQLITE_DONE) } } } _ = libc.Int32FromInt32(0) if rc == SQLITE_OK { if ppPage == uintptr(0) { v1 = int32(PAGER_GET_READONLY) } else { v1 = 0 } rc = _btreeGetPage(tls, pBt, ovfl, bp, v1) _ = libc.Int32FromInt32(0) if rc == SQLITE_OK { next = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData) } } *(*TPgno)(unsafe.Pointer(pPgnoNext)) = next if ppPage != 0 { *(*uintptr)(unsafe.Pointer(ppPage)) = *(*uintptr)(unsafe.Pointer(bp)) } else { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) } if rc == int32(SQLITE_DONE) { v2 = SQLITE_OK } else { v2 = rc } return v2 } // C documentation // // /* // ** Copy data from a buffer to a page, or from a page to a buffer. // ** // ** pPayload is a pointer to data stored on database page pDbPage. // ** If argument eOp is false, then nByte bytes of data are copied // ** from pPayload to the buffer pointed at by pBuf. If eOp is true, // ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes // ** of data are copied from the buffer pBuf to pPayload. // ** // ** SQLITE_OK is returned on success, otherwise an error code. // */ func _copyPayload(tls *libc.TLS, pPayload uintptr, pBuf uintptr, nByte int32, eOp int32, pDbPage uintptr) (r int32) { var rc int32 _ = rc if eOp != 0 { /* Copy data from buffer to page (a write operation) */ rc = _sqlite3PagerWrite(tls, pDbPage) if rc != SQLITE_OK { return rc } libc.Xmemcpy(tls, pPayload, pBuf, uint32(nByte)) } else { /* Copy data from page to buffer (a read operation) */ libc.Xmemcpy(tls, pBuf, pPayload, uint32(nByte)) } return SQLITE_OK } // C documentation // // /* // ** This function is used to read or overwrite payload information // ** for the entry that the pCur cursor is pointing to. The eOp // ** argument is interpreted as follows: // ** // ** 0: The operation is a read. Populate the overflow cache. // ** 1: The operation is a write. Populate the overflow cache. // ** // ** A total of "amt" bytes are read or written beginning at "offset". // ** Data is read to or from the buffer pBuf. // ** // ** The content being read or written might appear on the main page // ** or be scattered out on multiple overflow pages. // ** // ** If the current cursor entry uses one or more overflow pages // ** this function may allocate space for and lazily populate // ** the overflow page-list cache array (BtCursor.aOverflow). // ** Subsequent calls use this cache to make seeking to the supplied offset // ** more efficient. // ** // ** Once an overflow page-list cache has been allocated, it must be // ** invalidated if some other cursor writes to the same table, or if // ** the cursor is moved to a different row. Additionally, in auto-vacuum // ** mode, the following events may invalidate an overflow page-list cache. // ** // ** * An incremental vacuum, // ** * A commit in auto_vacuum="full" mode, // ** * Creating a table (may require moving an overflow page). // */ func _accessPayload(tls *libc.TLS, pCur uintptr, offset Tu32, amt Tu32, pBuf uintptr, eOp int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var a, a1, iIdx, nOvfl, rc, v2 int32 var aNew, aPayload, aWrite, fd, pBt, pBufStart, pPage, p1 uintptr var ovflSize Tu32 var _ /* aSave at bp+4 */ [4]Tu8 var _ /* nextPage at bp+0 */ TPgno var _ /* pDbPage at bp+8 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = a, a1, aNew, aPayload, aWrite, fd, iIdx, nOvfl, ovflSize, pBt, pBufStart, pPage, rc, v2, p1 rc = SQLITE_OK iIdx = 0 pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage /* Btree page of current entry */ pBt = (*TBtCursor)(unsafe.Pointer(pCur)).FpBt /* Btree this cursor belongs to */ pBufStart = pBuf /* Start of original out buffer */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) { return _sqlite3CorruptError(tls, int32(75235)) } _ = libc.Int32FromInt32(0) _getCellInfo(tls, pCur) aPayload = (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if uint32(int32(aPayload)-int32((*TMemPage)(unsafe.Pointer(pPage)).FaData)) > (*TBtShared)(unsafe.Pointer(pBt)).FusableSize-uint32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) { /* Trying to read or write past the end of the data is an error. The ** conditional above is really: ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ** but is recast into its current form to avoid integer overflow problems */ return _sqlite3CorruptError(tls, int32(75250)) } /* Check if data must be read/written to/from the btree page itself. */ if offset < uint32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) { a = int32(amt) if uint32(a)+offset > uint32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) { a = int32(uint32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) - offset) } rc = _copyPayload(tls, aPayload+uintptr(offset), pBuf, a, eOp, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage) offset = uint32(0) pBuf += uintptr(a) amt -= uint32(a) } else { offset -= uint32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) } if rc == SQLITE_OK && amt > uint32(0) { ovflSize = (*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32(4) *(*TPgno)(unsafe.Pointer(bp)) = _sqlite3Get4byte(tls, aPayload+uintptr((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal)) /* If the BtCursor.aOverflow[] has not been allocated, allocate it now. ** ** The aOverflow[] array is sized at one entry for each overflow page ** in the overflow chain. The page number of the first overflow page is ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array ** means "not yet known" (the cache is lazily populated). */ if int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags)&int32(BTCF_ValidOvfl) == 0 { nOvfl = int32(((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnPayload - uint32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) + ovflSize - uint32(1)) / ovflSize) if (*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow == uintptr(0) || nOvfl*libc.Int32FromInt64(4) > _sqlite3MallocSize(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow) { aNew = _sqlite3Realloc(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow, uint64(uint32(nOvfl*int32(2))*uint32(4))) if aNew == uintptr(0) { return int32(SQLITE_NOMEM) } else { (*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow = aNew } } libc.Xmemset(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow, 0, uint32(nOvfl)*uint32(4)) p1 = pCur + 1 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | libc.Int32FromInt32(BTCF_ValidOvfl)) } else { /* If the overflow page-list cache has been allocated and the ** entry for the first required overflow page is valid, skip ** directly to it. */ if *(*TPgno)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow + uintptr(offset/ovflSize)*4)) != 0 { iIdx = int32(offset / ovflSize) *(*TPgno)(unsafe.Pointer(bp)) = *(*TPgno)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow + uintptr(iIdx)*4)) offset = offset % ovflSize } } _ = libc.Int32FromInt32(0) for *(*TPgno)(unsafe.Pointer(bp)) != 0 { /* If required, populate the overflow page-list cache. */ if *(*TPgno)(unsafe.Pointer(bp)) > (*TBtShared)(unsafe.Pointer(pBt)).FnPage { return _sqlite3CorruptError(tls, int32(75312)) } _ = libc.Int32FromInt32(0) *(*TPgno)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow + uintptr(iIdx)*4)) = *(*TPgno)(unsafe.Pointer(bp)) if offset >= ovflSize { /* The only reason to read this page is to obtain the page ** number for the next page in the overflow chain. The page ** data is not required. So first try to lookup the overflow ** page-list cache, if any, then fall back to the getOverflowPage() ** function. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if *(*TPgno)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow + uintptr(iIdx+int32(1))*4)) != 0 { *(*TPgno)(unsafe.Pointer(bp)) = *(*TPgno)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow + uintptr(iIdx+int32(1))*4)) } else { rc = _getOverflowPage(tls, pBt, *(*TPgno)(unsafe.Pointer(bp)), uintptr(0), bp) } offset -= ovflSize } else { /* Need to read this page properly. It contains some of the ** range of data that is being read (eOp==0) or written (eOp!=0). */ a1 = int32(amt) if uint32(a1)+offset > ovflSize { a1 = int32(ovflSize - offset) } /* If all the following are true: ** ** 1) this is a read operation, and ** 2) data is required from the start of this overflow page, and ** 3) there are no dirty pages in the page-cache ** 4) the database is file-backed, and ** 5) the page is not in the WAL file ** 6) at least 4 bytes have already been read into the output buffer ** ** then data can be read directly from the database file into the ** output buffer, bypassing the page-cache altogether. This speeds ** up loading large records that span many overflow pages. */ if eOp == 0 && offset == uint32(0) && _sqlite3PagerDirectReadOk(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, *(*TPgno)(unsafe.Pointer(bp))) != 0 && pBuf+uintptr(-libc.Int32FromInt32(4)) >= pBufStart { fd = _sqlite3PagerFile(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager) aWrite = pBuf + uintptr(-libc.Int32FromInt32(4)) _ = libc.Int32FromInt32(0) /* due to (6) */ libc.Xmemcpy(tls, bp+4, aWrite, uint32(4)) rc = _sqlite3OsRead(tls, fd, aWrite, a1+int32(4), int64((*TBtShared)(unsafe.Pointer(pBt)).FpageSize)*int64(*(*TPgno)(unsafe.Pointer(bp))-libc.Uint32FromInt32(1))) *(*TPgno)(unsafe.Pointer(bp)) = _sqlite3Get4byte(tls, aWrite) libc.Xmemcpy(tls, aWrite, bp+4, uint32(4)) } else { if eOp == 0 { v2 = int32(PAGER_GET_READONLY) } else { v2 = 0 } rc = _sqlite3PagerGet(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, *(*TPgno)(unsafe.Pointer(bp)), bp+8, v2) if rc == SQLITE_OK { aPayload = _sqlite3PagerGetData(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) *(*TPgno)(unsafe.Pointer(bp)) = _sqlite3Get4byte(tls, aPayload) rc = _copyPayload(tls, aPayload+uintptr(offset+uint32(4)), pBuf, a1, eOp, *(*uintptr)(unsafe.Pointer(bp + 8))) _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) offset = uint32(0) } } amt -= uint32(a1) if amt == uint32(0) { return rc } pBuf += uintptr(a1) } if rc != 0 { break } iIdx++ } } if rc == SQLITE_OK && amt > uint32(0) { /* Overflow chain ends prematurely */ return _sqlite3CorruptError(tls, int32(75396)) } return rc } // C documentation // // /* // ** Read part of the payload for the row at which that cursor pCur is currently // ** pointing. "amt" bytes will be transferred into pBuf[]. The transfer // ** begins at "offset". // ** // ** pCur can be pointing to either a table or an index b-tree. // ** If pointing to a table btree, then the content section is read. If // ** pCur is pointing to an index b-tree then the key section is read. // ** // ** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing // ** to a valid row in the table. For sqlite3BtreePayloadChecked(), the // ** cursor might be invalid or might need to be restored before being read. // ** // ** Return SQLITE_OK on success or an error code if anything goes // ** wrong. An error is returned if "offset+amt" is larger than // ** the available payload. // */ func _sqlite3BtreePayload(tls *libc.TLS, pCur uintptr, offset Tu32, amt Tu32, pBuf uintptr) (r int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return _accessPayload(tls, pCur, offset, amt, pBuf, 0) } // C documentation // // /* // ** This variant of sqlite3BtreePayload() works even if the cursor has not // ** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read() // ** interface. // */ func _accessPayloadChecked(tls *libc.TLS, pCur uintptr, offset Tu32, amt Tu32, pBuf uintptr) (r int32) { var rc, v1 int32 _, _ = rc, v1 if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) == int32(CURSOR_INVALID) { return int32(SQLITE_ABORT) } _ = libc.Int32FromInt32(0) rc = _btreeRestoreCursorPosition(tls, pCur) if rc != 0 { v1 = rc } else { v1 = _accessPayload(tls, pCur, offset, amt, pBuf, 0) } return v1 } func _sqlite3BtreePayloadChecked(tls *libc.TLS, pCur uintptr, offset Tu32, amt Tu32, pBuf uintptr) (r int32) { if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) == CURSOR_VALID { _ = libc.Int32FromInt32(0) return _accessPayload(tls, pCur, offset, amt, pBuf, 0) } else { return _accessPayloadChecked(tls, pCur, offset, amt, pBuf) } return r } // C documentation // // /* // ** Return a pointer to payload information from the entry that the // ** pCur cursor is pointing to. The pointer is to the beginning of // ** the key if index btrees (pPage->intKey==0) and is the data for // ** table btrees (pPage->intKey==1). The number of bytes of available // ** key/data is written into *pAmt. If *pAmt==0, then the value // ** returned will not be a valid pointer. // ** // ** This routine is an optimization. It is common for the entire key // ** and data to fit on the local page and for there to be no overflow // ** pages. When that is so, this routine can be used to access the // ** key and data without making a copy. If the key and/or data spills // ** onto overflow pages, then accessPayload() must be used to reassemble // ** the key/data and copy it into a preallocated buffer. // ** // ** The pointer returned by this routine looks directly into the cached // ** page of the database. The data might change or move the next time // ** any btree routine is called. // */ func _fetchPayload(tls *libc.TLS, pCur uintptr, pAmt uintptr) (r uintptr) { var amt, v1 int32 _, _ = amt, v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) amt = int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) if amt > int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FaDataEnd)-int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload) { /* There is too little space on the page for the expected amount ** of local content. Database must be corrupt. */ _ = libc.Int32FromInt32(0) if 0 > int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FaDataEnd)-int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload) { v1 = 0 } else { v1 = int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FaDataEnd) - int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload) } amt = v1 } *(*Tu32)(unsafe.Pointer(pAmt)) = uint32(amt) return (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload } // C documentation // // /* // ** For the entry that cursor pCur is point to, return as // ** many bytes of the key or data as are available on the local // ** b-tree page. Write the number of available bytes into *pAmt. // ** // ** The pointer returned is ephemeral. The key/data may move // ** or be destroyed on the next call to any Btree routine, // ** including calls from other threads against the same cache. // ** Hence, a mutex on the BtShared should be held prior to calling // ** this routine. // ** // ** These routines is used to get quick access to key and data // ** in the common case where no overflow pages are used. // */ func _sqlite3BtreePayloadFetch(tls *libc.TLS, pCur uintptr, pAmt uintptr) (r uintptr) { return _fetchPayload(tls, pCur, pAmt) } // C documentation // // /* // ** Move the cursor down to a new child page. The newPgno argument is the // ** page number of the child page to move to. // ** // ** This function returns SQLITE_CORRUPT if the page-header flags field of // ** the new child page does not match the flags field of the parent (i.e. // ** if an intkey page appears to be the parent of a non-intkey page, or // ** vice-versa). // */ func _moveToChild(tls *libc.TLS, pCur uintptr, newPgno Tu32) (r int32) { var rc int32 var v2 Ti8 var v3, p1 uintptr _, _, _, _ = rc, v2, v3, p1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) >= libc.Int32FromInt32(BTCURSOR_MAX_DEPTH)-libc.Int32FromInt32(1) { return _sqlite3CorruptError(tls, int32(75534)) } (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0) p1 = pCur + 1 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) & ^(libc.Int32FromInt32(BTCF_ValidNKey) | libc.Int32FromInt32(BTCF_ValidOvfl))) *(*Tu16)(unsafe.Pointer(pCur + 72 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*2)) = (*TBtCursor)(unsafe.Pointer(pCur)).Fix *(*uintptr)(unsafe.Pointer(pCur + 120 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*4)) = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage (*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(0) (*TBtCursor)(unsafe.Pointer(pCur)).FiPage++ rc = _getAndInitPage(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpBt, newPgno, pCur+116, int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurPagerFlags)) _ = libc.Int32FromInt32(0) if rc == SQLITE_OK && (int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FnCell) < int32(1) || int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FintKey) != int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurIntKey)) { _releasePage(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpPage) rc = _sqlite3CorruptError(tls, int32(75548)) } if rc != 0 { v3 = pCur + 68 *(*Ti8)(unsafe.Pointer(v3))-- v2 = *(*Ti8)(unsafe.Pointer(v3)) (*TBtCursor)(unsafe.Pointer(pCur)).FpPage = *(*uintptr)(unsafe.Pointer(pCur + 120 + uintptr(v2)*4)) } return rc } // C documentation // // /* // ** Move the cursor up to the parent page. // ** // ** pCur->idx is set to the cell index that contains the pointer // ** to the page we are coming from. If we are coming from the // ** right-most child page then pCur->idx is set to one more than // ** the largest cell index. // */ func _moveToParent(tls *libc.TLS, pCur uintptr) { var pLeaf, v3, p1 uintptr var v2 Ti8 _, _, _, _ = pLeaf, v2, v3, p1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0) p1 = pCur + 1 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) & ^(libc.Int32FromInt32(BTCF_ValidNKey) | libc.Int32FromInt32(BTCF_ValidOvfl))) (*TBtCursor)(unsafe.Pointer(pCur)).Fix = *(*Tu16)(unsafe.Pointer(pCur + 72 + uintptr(int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)-int32(1))*2)) pLeaf = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage v3 = pCur + 68 *(*Ti8)(unsafe.Pointer(v3))-- v2 = *(*Ti8)(unsafe.Pointer(v3)) (*TBtCursor)(unsafe.Pointer(pCur)).FpPage = *(*uintptr)(unsafe.Pointer(pCur + 120 + uintptr(v2)*4)) _releasePageNotNull(tls, pLeaf) } // C documentation // // /* // ** Move the cursor to point to the root page of its b-tree structure. // ** // ** If the table has a virtual root page, then the cursor is moved to point // ** to the virtual root page instead of the actual root page. A table has a // ** virtual root page when the actual root page contains no cells and a // ** single child page. This can only happen with the table rooted at page 1. // ** // ** If the b-tree structure is empty, the cursor state is set to // ** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise, // ** the cursor is set to point to the first cell located on the root // ** (or virtual root) page and the cursor state is set to CURSOR_VALID. // ** // ** If this function returns successfully, it may be assumed that the // ** page-header flags indicate that the [virtual] root-page is the expected // ** kind of b-tree page (i.e. if when opening the cursor the caller did not // ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D, // ** indicating a table b-tree, or if the caller did specify a KeyInfo // ** structure the flags byte is set to 0x02 or 0x0A, indicating an index // ** b-tree). // */ func _moveToRoot(tls *libc.TLS, pCur uintptr) (r int32) { var pRoot, v2, v3, p4 uintptr var rc int32 var subpage TPgno var v1 Ti8 _, _, _, _, _, _, _ = pRoot, rc, subpage, v1, v2, v3, p4 rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) >= 0 { if (*TBtCursor)(unsafe.Pointer(pCur)).FiPage != 0 { _releasePageNotNull(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpPage) for { v2 = pCur + 68 *(*Ti8)(unsafe.Pointer(v2))-- v1 = *(*Ti8)(unsafe.Pointer(v2)) if !(v1 != 0) { break } _releasePageNotNull(tls, *(*uintptr)(unsafe.Pointer(pCur + 120 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*4))) } v3 = *(*uintptr)(unsafe.Pointer(pCur + 120)) (*TBtCursor)(unsafe.Pointer(pCur)).FpPage = v3 pRoot = v3 goto skip_init } } else { if (*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot == uint32(0) { (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_INVALID) return int32(SQLITE_EMPTY) } else { _ = libc.Int32FromInt32(0) if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) >= int32(CURSOR_REQUIRESEEK) { if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) == int32(CURSOR_FAULT) { _ = libc.Int32FromInt32(0) return (*TBtCursor)(unsafe.Pointer(pCur)).FskipNext } _sqlite3BtreeClearCursor(tls, pCur) } rc = _getAndInitPage(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpBt, (*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot, pCur+116, int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurPagerFlags)) if rc != SQLITE_OK { (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_INVALID) return rc } (*TBtCursor)(unsafe.Pointer(pCur)).FiPage = 0 (*TBtCursor)(unsafe.Pointer(pCur)).FcurIntKey = (*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FintKey } } pRoot = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage _ = libc.Int32FromInt32(0) /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is ** NULL, the caller expects a table b-tree. If this is not the case, ** return an SQLITE_CORRUPT error. ** ** Earlier versions of SQLite assumed that this test could not fail ** if the root page was already loaded when this function was called (i.e. ** if pCur->iPage>=0). But this is not so if the database is corrupted ** in such a way that page pRoot is linked into a second b-tree table ** (or the freelist). */ _ = libc.Int32FromInt32(0) if int32((*TMemPage)(unsafe.Pointer(pRoot)).FisInit) == 0 || libc.BoolInt32((*TBtCursor)(unsafe.Pointer(pCur)).FpKeyInfo == uintptr(0)) != int32((*TMemPage)(unsafe.Pointer(pRoot)).FintKey) { return _sqlite3CorruptError(tls, int32(75683)) } goto skip_init skip_init: ; (*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(0) (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0) p4 = pCur + 1 *(*Tu8)(unsafe.Pointer(p4)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p4))) & ^(libc.Int32FromInt32(BTCF_AtLast) | libc.Int32FromInt32(BTCF_ValidNKey) | libc.Int32FromInt32(BTCF_ValidOvfl))) if int32((*TMemPage)(unsafe.Pointer(pRoot)).FnCell) > 0 { (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_VALID) } else { if !((*TMemPage)(unsafe.Pointer(pRoot)).Fleaf != 0) { if (*TMemPage)(unsafe.Pointer(pRoot)).Fpgno != uint32(1) { return _sqlite3CorruptError(tls, int32(75695)) } subpage = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pRoot)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pRoot)).FhdrOffset)+int32(8))) (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_VALID) rc = _moveToChild(tls, pCur, subpage) } else { (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_INVALID) rc = int32(SQLITE_EMPTY) } } return rc } // C documentation // // /* // ** Move the cursor down to the left-most leaf entry beneath the // ** entry to which it is currently pointing. // ** // ** The left-most leaf is the one with the smallest key - the first // ** in ascending order. // */ func _moveToLeftmost(tls *libc.TLS, pCur uintptr) (r int32) { var pPage, v1 uintptr var pgno TPgno var rc int32 var v2 bool _, _, _, _, _ = pPage, pgno, rc, v1, v2 rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) for { if v2 = rc == SQLITE_OK; v2 { v1 = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage pPage = v1 } if !(v2 && !((*TMemPage)(unsafe.Pointer(v1)).Fleaf != 0)) { break } _ = libc.Int32FromInt32(0) pgno = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix)))))))) rc = _moveToChild(tls, pCur, pgno) } return rc } // C documentation // // /* // ** Move the cursor down to the right-most leaf entry beneath the // ** page to which it is currently pointing. Notice the difference // ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() // ** finds the left-most entry beneath the *entry* whereas moveToRightmost() // ** finds the right-most entry beneath the *page*. // ** // ** The right-most entry is the one with the largest key - the last // ** key in ascending order. // */ func _moveToRightmost(tls *libc.TLS, pCur uintptr) (r int32) { var pPage, v1 uintptr var pgno TPgno var rc int32 _, _, _, _ = pPage, pgno, rc, v1 rc = SQLITE_OK pPage = uintptr(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) for { v1 = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage pPage = v1 if !!((*TMemPage)(unsafe.Pointer(v1)).Fleaf != 0) { break } pgno = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8))) (*TBtCursor)(unsafe.Pointer(pCur)).Fix = (*TMemPage)(unsafe.Pointer(pPage)).FnCell rc = _moveToChild(tls, pCur, pgno) if rc != 0 { return rc } } (*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) - int32(1)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return SQLITE_OK } // C documentation // // /* Move the cursor to the first entry in the table. Return SQLITE_OK // ** on success. Set *pRes to 0 if the cursor actually points to something // ** or set *pRes to 1 if the table is empty. // */ func _sqlite3BtreeFirst(tls *libc.TLS, pCur uintptr, pRes uintptr) (r int32) { var rc int32 _ = rc _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _moveToRoot(tls, pCur) if rc == SQLITE_OK { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pRes)) = 0 rc = _moveToLeftmost(tls, pCur) } else { if rc == int32(SQLITE_EMPTY) { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pRes)) = int32(1) rc = SQLITE_OK } } return rc } // C documentation // // /* Move the cursor to the last entry in the table. Return SQLITE_OK // ** on success. Set *pRes to 0 if the cursor actually points to something // ** or set *pRes to 1 if the table is empty. // */ func _btreeLast(tls *libc.TLS, pCur uintptr, pRes uintptr) (r int32) { var rc int32 var p1, p2 uintptr _, _, _ = rc, p1, p2 rc = _moveToRoot(tls, pCur) if rc == SQLITE_OK { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pRes)) = 0 rc = _moveToRightmost(tls, pCur) if rc == SQLITE_OK { p1 = pCur + 1 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | libc.Int32FromInt32(BTCF_AtLast)) } else { p2 = pCur + 1 *(*Tu8)(unsafe.Pointer(p2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p2))) & ^libc.Int32FromInt32(BTCF_AtLast)) } } else { if rc == int32(SQLITE_EMPTY) { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pRes)) = int32(1) rc = SQLITE_OK } } return rc } func _sqlite3BtreeLast(tls *libc.TLS, pCur uintptr, pRes uintptr) (r int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If the cursor already points to the last entry, this is a no-op. */ if CURSOR_VALID == int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) && int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags)&int32(BTCF_AtLast) != 0 { *(*int32)(unsafe.Pointer(pRes)) = 0 return SQLITE_OK } return _btreeLast(tls, pCur, pRes) } // C documentation // // /* Move the cursor so that it points to an entry in a table (a.k.a INTKEY) // ** table near the key intKey. Return a success code. // ** // ** If an exact match is not found, then the cursor is always // ** left pointing at a leaf page which would hold the entry if it // ** were present. The cursor might point to an entry that comes // ** before or after the key. // ** // ** An integer is written into *pRes which is the result of // ** comparing the key with the entry to which the cursor is // ** pointing. The meaning of the integer written into // ** *pRes is as follows: // ** // ** *pRes<0 The cursor is left pointing at an entry that // ** is smaller than intKey or if the table is empty // ** and the cursor is therefore left point to nothing. // ** // ** *pRes==0 The cursor is left pointing at an entry that // ** exactly matches intKey. // ** // ** *pRes>0 The cursor is left pointing at an entry that // ** is larger than intKey. // */ func _sqlite3BtreeTableMoveto(tls *libc.TLS, pCur uintptr, intKey Ti64, biasRight int32, pRes uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var c, idx, lwr, rc, upr int32 var chldPg TPgno var pCell, pPage, v3, p4 uintptr var _ /* nCellKey at bp+0 */ Ti64 _, _, _, _, _, _, _, _, _, _ = c, chldPg, idx, lwr, pCell, pPage, rc, upr, v3, p4 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If the cursor is already positioned at the point we are trying ** to move to, then just return without doing any work */ if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) == CURSOR_VALID && int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags)&int32(BTCF_ValidNKey) != 0 { if (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey == intKey { *(*int32)(unsafe.Pointer(pRes)) = 0 return SQLITE_OK } if (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey < intKey { if int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags)&int32(BTCF_AtLast) != 0 { *(*int32)(unsafe.Pointer(pRes)) = -int32(1) return SQLITE_OK } /* If the requested key is one more than the previous key, then ** try to get there using sqlite3BtreeNext() rather than a full ** binary search. This is an optimization only. The correct answer ** is still obtained without this case, only a little more slowly. */ if (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey+int64(1) == intKey { *(*int32)(unsafe.Pointer(pRes)) = 0 rc = _sqlite3BtreeNext(tls, pCur, 0) if rc == SQLITE_OK { _getCellInfo(tls, pCur) if (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey == intKey { return SQLITE_OK } } else { if rc != int32(SQLITE_DONE) { return rc } } } } } rc = _moveToRoot(tls, pCur) if rc != 0 { if rc == int32(SQLITE_EMPTY) { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pRes)) = -int32(1) return SQLITE_OK } return rc } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) for { pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage /* Pointer to current cell in pPage */ /* pPage->nCell must be greater than zero. If this is the root-page ** the cursor would have been INVALID above and this for(;;) loop ** not run. If this is not the root-page, then the moveToChild() routine ** would have already detected db corruption. Similarly, pPage must ** be the right kind (index or table) of b-tree page. Otherwise ** a moveToChild() or moveToRoot() call would have detected corruption. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) lwr = 0 upr = int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) - int32(1) _ = libc.Int32FromInt32(0) idx = upr >> (int32(1) - biasRight) /* idx = biasRight ? upr : (lwr+upr)/2; */ for { pCell = (*TMemPage)(unsafe.Pointer(pPage)).FaDataOfst + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx)))))) if (*TMemPage)(unsafe.Pointer(pPage)).FintKeyLeaf != 0 { for { v3 = pCell pCell++ if !(int32(0x80) <= int32(*(*Tu8)(unsafe.Pointer(v3)))) { break } if pCell >= (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd { return _sqlite3CorruptError(tls, int32(75937)) } } } _sqlite3GetVarint(tls, pCell, bp) if *(*Ti64)(unsafe.Pointer(bp)) < intKey { lwr = idx + int32(1) if lwr > upr { c = -int32(1) break } } else { if *(*Ti64)(unsafe.Pointer(bp)) > intKey { upr = idx - int32(1) if lwr > upr { c = +libc.Int32FromInt32(1) break } } else { _ = libc.Int32FromInt32(0) (*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(idx) if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) { lwr = idx goto moveto_table_next_layer } else { p4 = pCur + 1 *(*Tu8)(unsafe.Pointer(p4)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p4))) | libc.Int32FromInt32(BTCF_ValidNKey)) (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey = *(*Ti64)(unsafe.Pointer(bp)) (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0) *(*int32)(unsafe.Pointer(pRes)) = 0 return SQLITE_OK } } } _ = libc.Int32FromInt32(0) idx = (lwr + upr) >> int32(1) /* idx = (lwr+upr)/2; */ goto _2 _2: } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0 { _ = libc.Int32FromInt32(0) (*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(idx) *(*int32)(unsafe.Pointer(pRes)) = c rc = SQLITE_OK goto moveto_table_finish } goto moveto_table_next_layer moveto_table_next_layer: ; if lwr >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) { chldPg = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8))) } else { chldPg = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*lwr))))))) } (*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(lwr) rc = _moveToChild(tls, pCur, chldPg) if rc != 0 { break } goto _1 _1: } goto moveto_table_finish moveto_table_finish: ; (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0) _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** Compare the "idx"-th cell on the page the cursor pCur is currently // ** pointing to to pIdxKey using xRecordCompare. Return negative or // ** zero if the cell is less than or equal pIdxKey. Return positive // ** if unknown. // ** // ** Return value negative: Cell at pCur[idx] less than pIdxKey // ** // ** Return value is zero: Cell at pCur[idx] equals pIdxKey // ** // ** Return value positive: Nothing is known about the relationship // ** of the cell at pCur[idx] and pIdxKey. // ** // ** This routine is part of an optimization. It is always safe to return // ** a positive value as that will cause the optimization to be skipped. // */ func _indexCellCompare(tls *libc.TLS, pCur uintptr, idx int32, pIdxKey uintptr, xRecordCompare TRecordCompare) (r int32) { var c, nCell, v1 int32 var pCell, pPage uintptr var v2 bool _, _, _, _, _, _ = c, nCell, pCell, pPage, v1, v2 pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage /* Size of the pCell cell in bytes */ pCell = (*TMemPage)(unsafe.Pointer(pPage)).FaDataOfst + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx)))))) nCell = int32(*(*Tu8)(unsafe.Pointer(pCell))) if nCell <= int32((*TMemPage)(unsafe.Pointer(pPage)).Fmax1bytePayload) { /* This branch runs if the record-size field of the cell is a ** single byte varint and the record fits entirely on the main ** b-tree page. */ c = (*(*func(*libc.TLS, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xRecordCompare})))(tls, nCell, pCell+1, pIdxKey) } else { if v2 = !(int32(*(*Tu8)(unsafe.Pointer(pCell + 1)))&libc.Int32FromInt32(0x80) != 0); v2 { v1 = nCell&libc.Int32FromInt32(0x7f)<0 The cursor is left pointing at an entry that // ** is larger than pIdxKey. // ** // ** The pIdxKey->eqSeen field is set to 1 if there // ** exists an entry in the table that exactly matches pIdxKey. // */ func _sqlite3BtreeIndexMoveto(tls *libc.TLS, pCur uintptr, pIdxKey uintptr, pRes uintptr) (r int32) { var c, c1, idx, lwr, nCell, nOverrun, rc, upr, v1, v6 int32 var chldPg TPgno var pCell, pCellBody, pCellKey, pPage, v11, p3, p8, p9 uintptr var xRecordCompare TRecordCompare var v10 Ti8 var v2, v7 bool _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, c1, chldPg, idx, lwr, nCell, nOverrun, pCell, pCellBody, pCellKey, pPage, rc, upr, xRecordCompare, v1, v10, v11, v2, v6, v7, p3, p8, p9 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) xRecordCompare = _sqlite3VdbeFindCompare(tls, pIdxKey) (*TUnpackedRecord)(unsafe.Pointer(pIdxKey)).FerrCode = uint8(0) _ = libc.Int32FromInt32(0) /* Check to see if we can skip a lot of work. Two cases: ** ** (1) If the cursor is already pointing to the very last cell ** in the table and the pIdxKey search key is greater than or ** equal to that last cell, then no movement is required. ** ** (2) If the cursor is on the last page of the table and the first ** cell on that last page is less than or equal to the pIdxKey ** search key, then we can start the search on the current page ** without needing to go back to root. */ if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) == CURSOR_VALID && (*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).Fleaf != 0 && _cursorOnLastPage(tls, pCur) != 0 { if v2 = int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) == int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FnCell)-int32(1); v2 { v1 = _indexCellCompare(tls, pCur, int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix), pIdxKey, xRecordCompare) c = v1 } if v2 && v1 <= 0 && int32((*TUnpackedRecord)(unsafe.Pointer(pIdxKey)).FerrCode) == SQLITE_OK { *(*int32)(unsafe.Pointer(pRes)) = c return SQLITE_OK /* Cursor already pointing at the correct spot */ } if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) > 0 && _indexCellCompare(tls, pCur, 0, pIdxKey, xRecordCompare) <= 0 && int32((*TUnpackedRecord)(unsafe.Pointer(pIdxKey)).FerrCode) == SQLITE_OK { p3 = pCur + 1 *(*Tu8)(unsafe.Pointer(p3)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p3))) & ^libc.Int32FromInt32(BTCF_ValidOvfl)) if !((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FisInit != 0) { return _sqlite3CorruptError(tls, int32(76133)) } goto bypass_moveto_root /* Start search on the current page */ } (*TUnpackedRecord)(unsafe.Pointer(pIdxKey)).FerrCode = uint8(SQLITE_OK) } rc = _moveToRoot(tls, pCur) if rc != 0 { if rc == int32(SQLITE_EMPTY) { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pRes)) = -int32(1) return SQLITE_OK } return rc } goto bypass_moveto_root bypass_moveto_root: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) for { pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage /* Pointer to current cell in pPage */ /* pPage->nCell must be greater than zero. If this is the root-page ** the cursor would have been INVALID above and this for(;;) loop ** not run. If this is not the root-page, then the moveToChild() routine ** would have already detected db corruption. Similarly, pPage must ** be the right kind (index or table) of b-tree page. Otherwise ** a moveToChild() or moveToRoot() call would have detected corruption. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) lwr = 0 upr = int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) - int32(1) idx = upr >> int32(1) /* idx = (lwr+upr)/2; */ for { /* Size of the pCell cell in bytes */ pCell = (*TMemPage)(unsafe.Pointer(pPage)).FaDataOfst + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx)))))) /* The maximum supported page-size is 65536 bytes. This means that ** the maximum number of record bytes stored on an index B-Tree ** page is less than 16384 bytes and may be stored as a 2-byte ** varint. This information is used to attempt to avoid parsing ** the entire cell by checking for the cases where the record is ** stored entirely within the b-tree page by inspecting the first ** 2 bytes of the cell. */ nCell = int32(*(*Tu8)(unsafe.Pointer(pCell))) if nCell <= int32((*TMemPage)(unsafe.Pointer(pPage)).Fmax1bytePayload) { /* This branch runs if the record-size field of the cell is a ** single byte varint and the record fits entirely on the main ** b-tree page. */ c1 = (*(*func(*libc.TLS, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xRecordCompare})))(tls, nCell, pCell+1, pIdxKey) } else { if v7 = !(int32(*(*Tu8)(unsafe.Pointer(pCell + 1)))&libc.Int32FromInt32(0x80) != 0); v7 { v6 = nCell&libc.Int32FromInt32(0x7f)< (*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FnPage { rc = _sqlite3CorruptError(tls, int32(76220)) goto moveto_index_finish } pCellKey = _sqlite3Malloc(tls, uint64(nCell+nOverrun)) if pCellKey == uintptr(0) { rc = int32(SQLITE_NOMEM) goto moveto_index_finish } (*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(idx) rc = _accessPayload(tls, pCur, uint32(0), uint32(nCell), pCellKey, 0) libc.Xmemset(tls, pCellKey+uintptr(nCell), 0, uint32(nOverrun)) /* Fix uninit warnings */ p8 = pCur + 1 *(*Tu8)(unsafe.Pointer(p8)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p8))) & ^libc.Int32FromInt32(BTCF_ValidOvfl)) if rc != 0 { Xsqlite3_free(tls, pCellKey) goto moveto_index_finish } c1 = _sqlite3VdbeRecordCompare(tls, nCell, pCellKey, pIdxKey) Xsqlite3_free(tls, pCellKey) } } _ = libc.Int32FromInt32(0) if c1 < 0 { lwr = idx + int32(1) } else { if c1 > 0 { upr = idx - int32(1) } else { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pRes)) = 0 rc = SQLITE_OK (*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(idx) if (*TUnpackedRecord)(unsafe.Pointer(pIdxKey)).FerrCode != 0 { rc = _sqlite3CorruptError(tls, int32(76252)) } goto moveto_index_finish } } if lwr > upr { break } _ = libc.Int32FromInt32(0) idx = (lwr + upr) >> int32(1) /* idx = (lwr+upr)/2 */ goto _5 _5: } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0 { _ = libc.Int32FromInt32(0) (*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(idx) *(*int32)(unsafe.Pointer(pRes)) = c1 rc = SQLITE_OK goto moveto_index_finish } if lwr >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) { chldPg = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8))) } else { chldPg = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*lwr))))))) } /* This block is similar to an in-lined version of: ** ** pCur->ix = (u16)lwr; ** rc = moveToChild(pCur, chldPg); ** if( rc ) break; */ (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0) p9 = pCur + 1 *(*Tu8)(unsafe.Pointer(p9)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p9))) & ^(libc.Int32FromInt32(BTCF_ValidNKey) | libc.Int32FromInt32(BTCF_ValidOvfl))) if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) >= libc.Int32FromInt32(BTCURSOR_MAX_DEPTH)-libc.Int32FromInt32(1) { return _sqlite3CorruptError(tls, int32(76283)) } *(*Tu16)(unsafe.Pointer(pCur + 72 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*2)) = uint16(lwr) *(*uintptr)(unsafe.Pointer(pCur + 120 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*4)) = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage (*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(0) (*TBtCursor)(unsafe.Pointer(pCur)).FiPage++ rc = _getAndInitPage(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpBt, chldPg, pCur+116, int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurPagerFlags)) if rc == SQLITE_OK && (int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FnCell) < int32(1) || int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FintKey) != int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurIntKey)) { _releasePage(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpPage) rc = _sqlite3CorruptError(tls, int32(76294)) } if rc != 0 { v11 = pCur + 68 *(*Ti8)(unsafe.Pointer(v11))-- v10 = *(*Ti8)(unsafe.Pointer(v11)) (*TBtCursor)(unsafe.Pointer(pCur)).FpPage = *(*uintptr)(unsafe.Pointer(pCur + 120 + uintptr(v10)*4)) break } /* ***** End of in-lined moveToChild() call */ goto _4 _4: } goto moveto_index_finish moveto_index_finish: ; (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0) _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** Return TRUE if the cursor is not pointing at an entry of the table. // ** // ** TRUE will be returned after a call to sqlite3BtreeNext() moves // ** past the last entry in the table or sqlite3BtreePrev() moves past // ** the first entry. TRUE is also returned if the table is empty. // */ func _sqlite3BtreeEof(tls *libc.TLS, pCur uintptr) (r int32) { /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries ** have been deleted? This API will need to change to return an error code ** as well as the boolean result value. */ return libc.BoolInt32(CURSOR_VALID != int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState)) } // C documentation // // /* // ** Return an estimate for the number of rows in the table that pCur is // ** pointing to. Return a negative number if no estimate is currently // ** available. // */ func _sqlite3BtreeRowCountEst(tls *libc.TLS, pCur uintptr) (r Ti64) { var i Tu8 var n Ti64 _, _ = i, n _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Currently this interface is only called by the OP_IfSmaller ** opcode, and it that case the cursor will always be valid and ** will always point to a leaf node. */ if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID { return int64(-int32(1)) } if int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).Fleaf) == 0 { return int64(-int32(1)) } n = int64((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FnCell) i = uint8(0) for { if !(int32(i) < int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)) { break } n *= int64((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pCur + 120 + uintptr(i)*4)))).FnCell) goto _1 _1: ; i++ } return n } // C documentation // // /* // ** Advance the cursor to the next entry in the database. // ** Return value: // ** // ** SQLITE_OK success // ** SQLITE_DONE cursor is already pointing at the last element // ** otherwise some kind of error occurred // ** // ** The main entry point is sqlite3BtreeNext(). That routine is optimized // ** for the common case of merely incrementing the cell counter BtCursor.aiIdx // ** to the next cell on the current page. The (slower) btreeNext() helper // ** routine is called when it is necessary to move to a different page or // ** to restore the cursor. // ** // ** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the // ** cursor corresponds to an SQL index and this routine could have been // ** skipped if the SQL index had been a unique index. The F argument // ** is a hint to the implement. SQLite btree implementation does not use // ** this hint, but COMDB2 does. // */ func _btreeNext(tls *libc.TLS, pCur uintptr) (r int32) { var idx, rc, v1 int32 var pPage, v3 uintptr var v2 Tu16 _, _, _, _, _, _ = idx, pPage, rc, v1, v2, v3 _ = libc.Int32FromInt32(0) if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID { _ = libc.Int32FromInt32(0) if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) >= int32(CURSOR_REQUIRESEEK) { v1 = _btreeRestoreCursorPosition(tls, pCur) } else { v1 = SQLITE_OK } rc = v1 if rc != SQLITE_OK { return rc } if int32(CURSOR_INVALID) == int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) { return int32(SQLITE_DONE) } if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) == int32(CURSOR_SKIPNEXT) { (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_VALID) if (*TBtCursor)(unsafe.Pointer(pCur)).FskipNext > 0 { return SQLITE_OK } } } pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage v3 = pCur + 70 *(*Tu16)(unsafe.Pointer(v3))++ v2 = *(*Tu16)(unsafe.Pointer(v3)) idx = int32(v2) if _sqlite3FaultSim(tls, int32(412)) != 0 { (*TMemPage)(unsafe.Pointer(pPage)).FisInit = uint8(0) } if !((*TMemPage)(unsafe.Pointer(pPage)).FisInit != 0) { return _sqlite3CorruptError(tls, int32(76395)) } if idx >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) { if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) { rc = _moveToChild(tls, pCur, _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8)))) if rc != 0 { return rc } return _moveToLeftmost(tls, pCur) } for cond := true; cond; cond = int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) { if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) == 0 { (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_INVALID) return int32(SQLITE_DONE) } _moveToParent(tls, pCur) pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage } if (*TMemPage)(unsafe.Pointer(pPage)).FintKey != 0 { return _sqlite3BtreeNext(tls, pCur, 0) } else { return SQLITE_OK } } if (*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0 { return SQLITE_OK } else { return _moveToLeftmost(tls, pCur) } return r } func _sqlite3BtreeNext(tls *libc.TLS, pCur uintptr, flags int32) (r int32) { var pPage, v3, p1 uintptr var v2 Tu16 _, _, _, _ = pPage, v2, v3, p1 _ = flags /* Used in COMDB2 but not native SQLite */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0) p1 = pCur + 1 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) & ^(libc.Int32FromInt32(BTCF_ValidNKey) | libc.Int32FromInt32(BTCF_ValidOvfl))) if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID { return _btreeNext(tls, pCur) } pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage v3 = pCur + 70 *(*Tu16)(unsafe.Pointer(v3))++ v2 = *(*Tu16)(unsafe.Pointer(v3)) if int32(v2) >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) { (*TBtCursor)(unsafe.Pointer(pCur)).Fix-- return _btreeNext(tls, pCur) } if (*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0 { return SQLITE_OK } else { return _moveToLeftmost(tls, pCur) } return r } // C documentation // // /* // ** Step the cursor to the back to the previous entry in the database. // ** Return values: // ** // ** SQLITE_OK success // ** SQLITE_DONE the cursor is already on the first element of the table // ** otherwise some kind of error occurred // ** // ** The main entry point is sqlite3BtreePrevious(). That routine is optimized // ** for the common case of merely decrementing the cell counter BtCursor.aiIdx // ** to the previous cell on the current page. The (slower) btreePrevious() // ** helper routine is called when it is necessary to move to a different page // ** or to restore the cursor. // ** // ** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then // ** the cursor corresponds to an SQL index and this routine could have been // ** skipped if the SQL index had been a unique index. The F argument is a // ** hint to the implement. The native SQLite btree implementation does not // ** use this hint, but COMDB2 does. // */ func _btreePrevious(tls *libc.TLS, pCur uintptr) (r int32) { var idx, rc, v1 int32 var pPage uintptr _, _, _, _ = idx, pPage, rc, v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID { if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) >= int32(CURSOR_REQUIRESEEK) { v1 = _btreeRestoreCursorPosition(tls, pCur) } else { v1 = SQLITE_OK } rc = v1 if rc != SQLITE_OK { return rc } if int32(CURSOR_INVALID) == int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) { return int32(SQLITE_DONE) } if int32(CURSOR_SKIPNEXT) == int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) { (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_VALID) if (*TBtCursor)(unsafe.Pointer(pCur)).FskipNext < 0 { return SQLITE_OK } } } pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage if _sqlite3FaultSim(tls, int32(412)) != 0 { (*TMemPage)(unsafe.Pointer(pPage)).FisInit = uint8(0) } if !((*TMemPage)(unsafe.Pointer(pPage)).FisInit != 0) { return _sqlite3CorruptError(tls, int32(76488)) } if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) { idx = int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) rc = _moveToChild(tls, pCur, _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx)))))))) if rc != 0 { return rc } rc = _moveToRightmost(tls, pCur) } else { for int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) == 0 { if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) == 0 { (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_INVALID) return int32(SQLITE_DONE) } _moveToParent(tls, pCur) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TBtCursor)(unsafe.Pointer(pCur)).Fix-- pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage if (*TMemPage)(unsafe.Pointer(pPage)).FintKey != 0 && !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) { rc = _sqlite3BtreePrevious(tls, pCur, 0) } else { rc = SQLITE_OK } } return rc } func _sqlite3BtreePrevious(tls *libc.TLS, pCur uintptr, flags int32) (r int32) { var p1 uintptr _ = p1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = flags /* Used in COMDB2 but not native SQLite */ p1 = pCur + 1 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) & ^(libc.Int32FromInt32(BTCF_AtLast) | libc.Int32FromInt32(BTCF_ValidOvfl) | libc.Int32FromInt32(BTCF_ValidNKey))) (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0) if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID || int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) == 0 || int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).Fleaf) == 0 { return _btreePrevious(tls, pCur) } (*TBtCursor)(unsafe.Pointer(pCur)).Fix-- return SQLITE_OK } // C documentation // // /* // ** Allocate a new page from the database file. // ** // ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() // ** has already been called on the new page.) The new page has also // ** been referenced and the calling routine is responsible for calling // ** sqlite3PagerUnref() on the new page when it is done. // ** // ** SQLITE_OK is returned on success. Any other return value indicates // ** an error. *ppPage is set to NULL in the event of an error. // ** // ** If the "nearby" parameter is not 0, then an effort is made to // ** locate a page close to the page number "nearby". This can be used in an // ** attempt to keep related pages close to each other in the database file, // ** which in turn can make database access faster. // ** // ** If the eMode parameter is BTALLOC_EXACT and the nearby page exists // ** anywhere on the free-list, then it is guaranteed to be returned. If // ** eMode is BTALLOC_LT then the page returned will be less than or equal // ** to nearby if any such page exists. If eMode is BTALLOC_ANY then there // ** are no restrictions on which page is returned. // */ func _allocateBtreePage(tls *libc.TLS, pBt uintptr, ppPage uintptr, pPgno uintptr, nearby TPgno, eMode Tu8) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var aData, pPage1, pPrevTrunk uintptr var bNoContent, d2, dist, noContent, rc, v5, v6 int32 var closest, i, k, n, nSearch, v1 Tu32 var iNewTrunk, iPage, iTrunk, mxPage TPgno var searchList Tu8 var v2 bool var _ /* eType at bp+4 */ Tu8 var _ /* pNewTrunk at bp+8 */ uintptr var _ /* pPg at bp+12 */ uintptr var _ /* pTrunk at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aData, bNoContent, closest, d2, dist, i, iNewTrunk, iPage, iTrunk, k, mxPage, n, nSearch, noContent, pPage1, pPrevTrunk, rc, searchList, v1, v2, v5, v6 /* Number of leaves on the trunk of the freelist */ *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) pPrevTrunk = uintptr(0) /* Total size of the database file */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pPage1 = (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 mxPage = _btreePagecount(tls, pBt) /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36 ** stores the total number of pages on the freelist. */ n = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+36) if n >= mxPage { return _sqlite3CorruptError(tls, int32(76578)) } if n > uint32(0) { searchList = uint8(0) /* If the free-list must be searched for 'nearby' */ nSearch = uint32(0) /* Count of the number of search attempts */ /* If eMode==BTALLOC_EXACT and a query of the pointer-map ** shows that the page 'nearby' is somewhere on the free-list, then ** the entire-list will be searched for that page. */ if int32(eMode) == int32(BTALLOC_EXACT) { if nearby <= mxPage { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _ptrmapGet(tls, pBt, nearby, bp+4, uintptr(0)) if rc != 0 { return rc } if int32(*(*Tu8)(unsafe.Pointer(bp + 4))) == int32(PTRMAP_FREEPAGE) { searchList = uint8(1) } } } else { if int32(eMode) == int32(BTALLOC_LE) { searchList = uint8(1) } } /* Decrement the free-list count by 1. Set iTrunk to the index of the ** first free-list trunk page. iPrevTrunk is initially 1. */ rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FpDbPage) if rc != 0 { return rc } _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+36, n-uint32(1)) /* The code within this loop is run only once if the 'searchList' variable ** is not true. Otherwise, it runs once for each trunk-page on the ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT) ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT) */ for cond := true; cond; cond = searchList != 0 { pPrevTrunk = *(*uintptr)(unsafe.Pointer(bp)) if pPrevTrunk != 0 { /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page ** is the page number of the next freelist trunk page in the list or ** zero if this is the last freelist trunk page. */ iTrunk = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPrevTrunk)).FaData) } else { /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32 ** stores the page number of the first page of the freelist, or zero if ** the freelist is empty. */ iTrunk = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32) } if v2 = iTrunk > mxPage; !v2 { v1 = nSearch nSearch++ } if v2 || v1 > n { rc = _sqlite3CorruptError(tls, int32(76634)) } else { rc = _btreeGetUnusedPage(tls, pBt, iTrunk, bp, 0) } if rc != 0 { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) goto end_allocate_page } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page ** is the number of leaf page pointers to follow. */ k = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData+4) if k == uint32(0) && !(searchList != 0) { /* The trunk has no leaves and the list is not being searched. ** So extract the trunk page itself and use it as the newly ** allocated page */ _ = libc.Int32FromInt32(0) rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpDbPage) if rc != 0 { goto end_allocate_page } *(*TPgno)(unsafe.Pointer(pPgno)) = iTrunk libc.Xmemcpy(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData, uint32(4)) *(*uintptr)(unsafe.Pointer(ppPage)) = *(*uintptr)(unsafe.Pointer(bp)) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } else { if k > (*TBtShared)(unsafe.Pointer(pBt)).FusableSize/libc.Uint32FromInt32(4)-libc.Uint32FromInt32(2) { /* Value of k is out of range. Database corruption */ rc = _sqlite3CorruptError(tls, int32(76663)) goto end_allocate_page } else { if searchList != 0 && (nearby == iTrunk || iTrunk < nearby && int32(eMode) == int32(BTALLOC_LE)) { /* The list is being searched and this trunk page is the page ** to allocate, regardless of whether it has leaves. */ *(*TPgno)(unsafe.Pointer(pPgno)) = iTrunk *(*uintptr)(unsafe.Pointer(ppPage)) = *(*uintptr)(unsafe.Pointer(bp)) searchList = uint8(0) rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpDbPage) if rc != 0 { goto end_allocate_page } if k == uint32(0) { if !(pPrevTrunk != 0) { libc.Xmemcpy(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData, uint32(4)) } else { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPrevTrunk)).FpDbPage) if rc != SQLITE_OK { goto end_allocate_page } libc.Xmemcpy(tls, (*TMemPage)(unsafe.Pointer(pPrevTrunk)).FaData, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData, uint32(4)) } } else { iNewTrunk = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData+8) if iNewTrunk > mxPage { rc = _sqlite3CorruptError(tls, int32(76697)) goto end_allocate_page } rc = _btreeGetUnusedPage(tls, pBt, iNewTrunk, bp+8, 0) if rc != SQLITE_OK { goto end_allocate_page } rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FpDbPage) if rc != SQLITE_OK { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) goto end_allocate_page } libc.Xmemcpy(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FaData, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData, uint32(4)) _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FaData+4, k-uint32(1)) libc.Xmemcpy(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FaData+8, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData+12, (k-uint32(1))*uint32(4)) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) if !(pPrevTrunk != 0) { _ = libc.Int32FromInt32(0) _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32, iNewTrunk) } else { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPrevTrunk)).FpDbPage) if rc != 0 { goto end_allocate_page } _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPrevTrunk)).FaData, iNewTrunk) } } *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } else { if k > uint32(0) { aData = (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData if nearby > uint32(0) { closest = uint32(0) if int32(eMode) == int32(BTALLOC_LE) { i = uint32(0) for { if !(i < k) { break } iPage = _sqlite3Get4byte(tls, aData+uintptr(uint32(8)+i*uint32(4))) if iPage <= nearby { closest = i break } goto _3 _3: ; i++ } } else { dist = _sqlite3AbsInt32(tls, int32(_sqlite3Get4byte(tls, aData+8)-nearby)) i = uint32(1) for { if !(i < k) { break } d2 = _sqlite3AbsInt32(tls, int32(_sqlite3Get4byte(tls, aData+uintptr(uint32(8)+i*uint32(4)))-nearby)) if d2 < dist { closest = i dist = d2 } goto _4 _4: ; i++ } } } else { closest = uint32(0) } iPage = _sqlite3Get4byte(tls, aData+uintptr(uint32(8)+closest*uint32(4))) if iPage > mxPage || iPage < uint32(2) { rc = _sqlite3CorruptError(tls, int32(76762)) goto end_allocate_page } if !(searchList != 0) || (iPage == nearby || iPage < nearby && int32(eMode) == int32(BTALLOC_LE)) { *(*TPgno)(unsafe.Pointer(pPgno)) = iPage rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpDbPage) if rc != 0 { goto end_allocate_page } if closest < k-uint32(1) { libc.Xmemcpy(tls, aData+uintptr(uint32(8)+closest*uint32(4)), aData+uintptr(uint32(4)+k*uint32(4)), uint32(4)) } _sqlite3Put4byte(tls, aData+4, k-uint32(1)) if !(_btreeGetHasContent(tls, pBt, *(*TPgno)(unsafe.Pointer(pPgno))) != 0) { v5 = int32(PAGER_GET_NOCONTENT) } else { v5 = 0 } noContent = v5 rc = _btreeGetUnusedPage(tls, pBt, *(*TPgno)(unsafe.Pointer(pPgno)), ppPage, noContent) if rc == SQLITE_OK { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(ppPage)))).FpDbPage) if rc != SQLITE_OK { _releasePage(tls, *(*uintptr)(unsafe.Pointer(ppPage))) *(*uintptr)(unsafe.Pointer(ppPage)) = uintptr(0) } } searchList = uint8(0) } } } } } _releasePage(tls, pPrevTrunk) pPrevTrunk = uintptr(0) } } else { if 0 == int32((*TBtShared)(unsafe.Pointer(pBt)).FbDoTruncate) { v6 = int32(PAGER_GET_NOCONTENT) } else { v6 = 0 } /* There are no pages on the freelist, so append a new page to the ** database image. ** ** Normally, new pages allocated by this block can be requested from the ** pager layer with the 'no-content' flag set. This prevents the pager ** from trying to read the pages content from disk. However, if the ** current transaction has already run one or more incremental-vacuum ** steps, then the page we are about to allocate may contain content ** that is required in the event of a rollback. In this case, do ** not set the no-content flag. This causes the pager to load and journal ** the current page content before overwriting it. ** ** Note that the pager will not actually attempt to load or journal ** content for any page that really does lie past the end of the database ** file on disk. So the effects of disabling the no-content optimization ** here are confined to those pages that lie between the end of the ** database image and the end of the database file. */ bNoContent = v6 rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FpDbPage) if rc != 0 { return rc } (*TBtShared)(unsafe.Pointer(pBt)).FnPage++ if (*TBtShared)(unsafe.Pointer(pBt)).FnPage == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) { (*TBtShared)(unsafe.Pointer(pBt)).FnPage++ } if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 && _ptrmapPageno(tls, pBt, (*TBtShared)(unsafe.Pointer(pBt)).FnPage) == (*TBtShared)(unsafe.Pointer(pBt)).FnPage { /* If *pPgno refers to a pointer-map page, allocate two new pages ** at the end of the file instead of one. The first allocated page ** becomes a new pointer-map page, the second is used by the caller. */ *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) _ = libc.Int32FromInt32(0) rc = _btreeGetUnusedPage(tls, pBt, (*TBtShared)(unsafe.Pointer(pBt)).FnPage, bp+12, bNoContent) if rc == SQLITE_OK { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)))).FpDbPage) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) } if rc != 0 { return rc } (*TBtShared)(unsafe.Pointer(pBt)).FnPage++ if (*TBtShared)(unsafe.Pointer(pBt)).FnPage == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) { (*TBtShared)(unsafe.Pointer(pBt)).FnPage++ } } _sqlite3Put4byte(tls, uintptr(28)+(*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData, (*TBtShared)(unsafe.Pointer(pBt)).FnPage) *(*TPgno)(unsafe.Pointer(pPgno)) = (*TBtShared)(unsafe.Pointer(pBt)).FnPage _ = libc.Int32FromInt32(0) rc = _btreeGetUnusedPage(tls, pBt, *(*TPgno)(unsafe.Pointer(pPgno)), ppPage, bNoContent) if rc != 0 { return rc } rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(ppPage)))).FpDbPage) if rc != SQLITE_OK { _releasePage(tls, *(*uintptr)(unsafe.Pointer(ppPage))) *(*uintptr)(unsafe.Pointer(ppPage)) = uintptr(0) } } _ = libc.Int32FromInt32(0) goto end_allocate_page end_allocate_page: ; _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) _releasePage(tls, pPrevTrunk) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** This function is used to add page iPage to the database file free-list. // ** It is assumed that the page is not already a part of the free-list. // ** // ** The value passed as the second argument to this function is optional. // ** If the caller happens to have a pointer to the MemPage object // ** corresponding to page iPage handy, it may pass it as the second value. // ** Otherwise, it may pass NULL. // ** // ** If a pointer to a MemPage object is passed as the second argument, // ** its reference count is not altered by this function. // */ func _freePage2(tls *libc.TLS, pBt uintptr, pMemPage uintptr, iPage TPgno) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iTrunk TPgno var nFree, nLeaf Tu32 var pPage1 uintptr var v1, v3, v5 int32 var v2, v4, v6 bool var _ /* pPage at bp+4 */ uintptr var _ /* pTrunk at bp+0 */ uintptr var _ /* rc at bp+8 */ int32 _, _, _, _, _, _, _, _, _, _ = iTrunk, nFree, nLeaf, pPage1, v1, v2, v3, v4, v5, v6 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Free-list trunk page */ iTrunk = uint32(0) /* Page number of free-list trunk page */ pPage1 = (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 /* Initial number of pages on free-list */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if iPage < uint32(2) || iPage > (*TBtShared)(unsafe.Pointer(pBt)).FnPage { return _sqlite3CorruptError(tls, int32(76889)) } if pMemPage != 0 { *(*uintptr)(unsafe.Pointer(bp + 4)) = pMemPage _sqlite3PagerRef(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FpDbPage) } else { *(*uintptr)(unsafe.Pointer(bp + 4)) = _btreePageLookup(tls, pBt, iPage) } /* Increment the free page count on pPage1 */ *(*int32)(unsafe.Pointer(bp + 8)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FpDbPage) if *(*int32)(unsafe.Pointer(bp + 8)) != 0 { goto freepage_out } nFree = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+36) _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+36, nFree+uint32(1)) if int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_SECURE_DELETE) != 0 { /* If the secure_delete option is enabled, then ** always fully overwrite deleted information with zeros. */ if v2 = !(*(*uintptr)(unsafe.Pointer(bp + 4)) != 0); v2 { v1 = _btreeGetPage(tls, pBt, iPage, bp+4, 0) *(*int32)(unsafe.Pointer(bp + 8)) = v1 } if v4 = v2 && v1 != 0; !v4 { v3 = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FpDbPage) *(*int32)(unsafe.Pointer(bp + 8)) = v3 } if v4 || v3 != 0 { goto freepage_out } libc.Xmemset(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FaData, 0, (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FpBt)).FpageSize) } /* If the database supports auto-vacuum, write an entry in the pointer-map ** to indicate that the page is free. */ if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { _ptrmapPut(tls, pBt, iPage, uint8(PTRMAP_FREEPAGE), uint32(0), bp+8) if *(*int32)(unsafe.Pointer(bp + 8)) != 0 { goto freepage_out } } /* Now manipulate the actual database free-list structure. There are two ** possibilities. If the free-list is currently empty, or if the first ** trunk page in the free-list is full, then this page will become a ** new free-list trunk page. Otherwise, it will become a leaf of the ** first trunk page in the current free-list. This block tests if it ** is possible to add the page as a new free-list leaf. */ if nFree != uint32(0) { /* Initial number of leaf cells on trunk page */ iTrunk = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32) if iTrunk > _btreePagecount(tls, pBt) { *(*int32)(unsafe.Pointer(bp + 8)) = _sqlite3CorruptError(tls, int32(76936)) goto freepage_out } *(*int32)(unsafe.Pointer(bp + 8)) = _btreeGetPage(tls, pBt, iTrunk, bp, 0) if *(*int32)(unsafe.Pointer(bp + 8)) != SQLITE_OK { goto freepage_out } nLeaf = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData+4) _ = libc.Int32FromInt32(0) if nLeaf > (*TBtShared)(unsafe.Pointer(pBt)).FusableSize/uint32(4)-uint32(2) { *(*int32)(unsafe.Pointer(bp + 8)) = _sqlite3CorruptError(tls, int32(76947)) goto freepage_out } if nLeaf < (*TBtShared)(unsafe.Pointer(pBt)).FusableSize/uint32(4)-uint32(8) { /* In this case there is room on the trunk page to insert the page ** being freed as a new leaf. ** ** Note that the trunk page is not really full until it contains ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have ** coded. But due to a coding error in versions of SQLite prior to ** 3.6.0, databases with freelist trunk pages holding more than ** usableSize/4 - 8 entries will be reported as corrupt. In order ** to maintain backwards compatibility with older versions of SQLite, ** we will continue to restrict the number of entries to usableSize/4 - 8 ** for now. At some point in the future (once everyone has upgraded ** to 3.6.0 or later) we should consider fixing the conditional above ** to read "usableSize/4-2" instead of "usableSize/4-8". ** ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still ** avoid using the last six entries in the freelist trunk page array in ** order that database files created by newer versions of SQLite can be ** read by older versions of SQLite. */ *(*int32)(unsafe.Pointer(bp + 8)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpDbPage) if *(*int32)(unsafe.Pointer(bp + 8)) == SQLITE_OK { _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData+4, nLeaf+uint32(1)) _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData+uintptr(uint32(8)+nLeaf*uint32(4)), iPage) if *(*uintptr)(unsafe.Pointer(bp + 4)) != 0 && int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_SECURE_DELETE) == 0 { _sqlite3PagerDontWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FpDbPage) } *(*int32)(unsafe.Pointer(bp + 8)) = _btreeSetHasContent(tls, pBt, iPage) } goto freepage_out } } /* If control flows to this point, then it was not possible to add the ** the page being freed as a leaf page of the first trunk in the free-list. ** Possibly because the free-list is empty, or possibly because the ** first trunk in the free-list is full. Either way, the page being freed ** will become the new first trunk page in the free-list. */ if v6 = *(*uintptr)(unsafe.Pointer(bp + 4)) == uintptr(0); v6 { v5 = _btreeGetPage(tls, pBt, iPage, bp+4, 0) *(*int32)(unsafe.Pointer(bp + 8)) = v5 } if v6 && SQLITE_OK != v5 { goto freepage_out } *(*int32)(unsafe.Pointer(bp + 8)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FpDbPage) if *(*int32)(unsafe.Pointer(bp + 8)) != SQLITE_OK { goto freepage_out } _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FaData, iTrunk) _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FaData+4, uint32(0)) _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32, iPage) goto freepage_out freepage_out: ; if *(*uintptr)(unsafe.Pointer(bp + 4)) != 0 { (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FisInit = uint8(0) } _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) return *(*int32)(unsafe.Pointer(bp + 8)) } func _freePage(tls *libc.TLS, pPage uintptr, pRC uintptr) { if *(*int32)(unsafe.Pointer(pRC)) == SQLITE_OK { *(*int32)(unsafe.Pointer(pRC)) = _freePage2(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpBt, pPage, (*TMemPage)(unsafe.Pointer(pPage)).Fpgno) } } // C documentation // // /* // ** Free the overflow pages associated with the given Cell. // */ func _clearCellOverflow(tls *libc.TLS, pPage uintptr, pCell uintptr, pInfo uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var nOvfl, rc, v1 int32 var ovflPageSize Tu32 var ovflPgno TPgno var pBt, v2 uintptr var v3 bool var _ /* iNext at bp+0 */ TPgno var _ /* pOvfl at bp+4 */ uintptr _, _, _, _, _, _, _, _ = nOvfl, ovflPageSize, ovflPgno, pBt, rc, v1, v2, v3 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pCell+uintptr((*TCellInfo)(unsafe.Pointer(pInfo)).FnSize) > (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd { /* Cell extends past end of page */ return _sqlite3CorruptError(tls, int32(77036)) } ovflPgno = _sqlite3Get4byte(tls, pCell+uintptr((*TCellInfo)(unsafe.Pointer(pInfo)).FnSize)-uintptr(4)) pBt = (*TMemPage)(unsafe.Pointer(pPage)).FpBt _ = libc.Int32FromInt32(0) ovflPageSize = (*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32(4) nOvfl = int32(((*TCellInfo)(unsafe.Pointer(pInfo)).FnPayload - uint32((*TCellInfo)(unsafe.Pointer(pInfo)).FnLocal) + ovflPageSize - uint32(1)) / ovflPageSize) _ = libc.Int32FromInt32(0) for { v1 = nOvfl nOvfl-- if !(v1 != 0) { break } *(*TPgno)(unsafe.Pointer(bp)) = uint32(0) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) if ovflPgno < uint32(2) || ovflPgno > _btreePagecount(tls, pBt) { /* 0 is not a legal page number and page 1 cannot be an ** overflow page. Therefore if ovflPgno<2 or past the end of the ** file the database must be corrupt. */ return _sqlite3CorruptError(tls, int32(77053)) } if nOvfl != 0 { rc = _getOverflowPage(tls, pBt, ovflPgno, bp+4, bp) if rc != 0 { return rc } } if v3 = *(*uintptr)(unsafe.Pointer(bp + 4)) != 0; !v3 { v2 = _btreePageLookup(tls, pBt, ovflPgno) *(*uintptr)(unsafe.Pointer(bp + 4)) = v2 } if (v3 || v2 != uintptr(0)) && _sqlite3PagerPageRefcount(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FpDbPage) != int32(1) { /* There is no reason any cursor should have an outstanding reference ** to an overflow page belonging to a cell that is being deleted/updated. ** So if there exists more than one reference to this page, then it ** must not really be an overflow page and the database must be corrupt. ** It is helpful to detect this before calling freePage2(), as ** freePage2() may zero the page contents if secure-delete mode is ** enabled. If this 'overflow' page happens to be a page that the ** caller is iterating through or using in some other way, this ** can be problematic. */ rc = _sqlite3CorruptError(tls, int32(77073)) } else { rc = _freePage2(tls, pBt, *(*uintptr)(unsafe.Pointer(bp + 4)), ovflPgno) } if *(*uintptr)(unsafe.Pointer(bp + 4)) != 0 { _sqlite3PagerUnref(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FpDbPage) } if rc != 0 { return rc } ovflPgno = *(*TPgno)(unsafe.Pointer(bp)) } return SQLITE_OK } /* Call xParseCell to compute the size of a cell. If the cell contains ** overflow, then invoke cellClearOverflow to clear out that overflow. ** Store the result code (SQLITE_OK or some error code) in rc. ** ** Implemented as macro to force inlining for performance. */ // C documentation // // /* // ** Create the byte sequence used to represent a cell on page pPage // ** and write that byte sequence into pCell[]. Overflow pages are // ** allocated and filled in as necessary. The calling procedure // ** is responsible for making sure sufficient space has been allocated // ** for pCell[]. // ** // ** Note that pCell does not necessary need to point to the pPage->aData // ** area. pCell might point to some temporary storage. The cell will // ** be constructed in this temporary area then copied into pPage->aData // ** later. // */ func _fillInCell(tls *libc.TLS, pPage uintptr, pCell uintptr, pX uintptr, pnSize uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var eType Tu8 var mn, n, nHeader, nPayload, nSrc, spaceLeft, v1, v2, v3, v4 int32 var pBt, pPayload, pPrior, pSrc, pToRelease uintptr var pgnoPtrmap TPgno var _ /* pOvfl at bp+8 */ uintptr var _ /* pgnoOvfl at bp+4 */ TPgno var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = eType, mn, n, nHeader, nPayload, nSrc, pBt, pPayload, pPrior, pSrc, pToRelease, pgnoPtrmap, spaceLeft, v1, v2, v3, v4 _ = libc.Int32FromInt32(0) /* pPage is not necessarily writeable since pCell might be auxiliary ** buffer space that is separate from the pPage buffer area */ _ = libc.Int32FromInt32(0) /* Fill in the header. */ nHeader = int32((*TMemPage)(unsafe.Pointer(pPage)).FchildPtrSize) if (*TMemPage)(unsafe.Pointer(pPage)).FintKey != 0 { nPayload = (*TBtreePayload)(unsafe.Pointer(pX)).FnData + (*TBtreePayload)(unsafe.Pointer(pX)).FnZero pSrc = (*TBtreePayload)(unsafe.Pointer(pX)).FpData nSrc = (*TBtreePayload)(unsafe.Pointer(pX)).FnData _ = libc.Int32FromInt32(0) /* fillInCell() only called for leaves */ if uint32(nPayload) < libc.Uint32FromInt32(0x80) { *(*uint8)(unsafe.Pointer(pCell + uintptr(nHeader))) = uint8(nPayload) v1 = libc.Int32FromInt32(1) } else { v1 = _sqlite3PutVarint(tls, pCell+uintptr(nHeader), uint64(nPayload)) } nHeader += int32(uint8(v1)) nHeader += _sqlite3PutVarint(tls, pCell+uintptr(nHeader), *(*Tu64)(unsafe.Pointer(pX + 8))) } else { _ = libc.Int32FromInt32(0) v2 = int32((*TBtreePayload)(unsafe.Pointer(pX)).FnKey) nPayload = v2 nSrc = v2 pSrc = (*TBtreePayload)(unsafe.Pointer(pX)).FpKey if uint32(nPayload) < libc.Uint32FromInt32(0x80) { *(*uint8)(unsafe.Pointer(pCell + uintptr(nHeader))) = uint8(nPayload) v3 = libc.Int32FromInt32(1) } else { v3 = _sqlite3PutVarint(tls, pCell+uintptr(nHeader), uint64(nPayload)) } nHeader += int32(uint8(v3)) } /* Fill in the payload */ pPayload = pCell + uintptr(nHeader) if nPayload <= int32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) { /* This is the common case where everything fits on the btree page ** and no overflow pages are required. */ n = nHeader + nPayload if n < int32(4) { n = int32(4) *(*uint8)(unsafe.Pointer(pPayload + uintptr(nPayload))) = uint8(0) } *(*int32)(unsafe.Pointer(pnSize)) = n _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, pPayload, pSrc, uint32(nSrc)) libc.Xmemset(tls, pPayload+uintptr(nSrc), 0, uint32(nPayload-nSrc)) return SQLITE_OK } /* If we reach this point, it means that some of the content will need ** to spill onto overflow pages. */ mn = int32((*TMemPage)(unsafe.Pointer(pPage)).FminLocal) n = int32(uint32(mn) + uint32(nPayload-mn)%((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize-uint32(4))) if n > int32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) { n = mn } spaceLeft = n *(*int32)(unsafe.Pointer(pnSize)) = n + nHeader + int32(4) pPrior = pCell + uintptr(nHeader+n) pToRelease = uintptr(0) *(*TPgno)(unsafe.Pointer(bp + 4)) = uint32(0) pBt = (*TMemPage)(unsafe.Pointer(pPage)).FpBt /* At this point variables should be set as follows: ** ** nPayload Total payload size in bytes ** pPayload Begin writing payload here ** spaceLeft Space available at pPayload. If nPayload>spaceLeft, ** that means content must spill into overflow pages. ** *pnSize Size of the local cell (not counting overflow pages) ** pPrior Where to write the pgno of the first overflow page ** ** Use a call to btreeParseCellPtr() to verify that the values above ** were computed correctly. */ /* Write the payload into the local Cell and any extra into overflow pages */ for int32(1) != 0 { n = nPayload if n > spaceLeft { n = spaceLeft } /* If pToRelease is not zero than pPayload points into the data area ** of pToRelease. Make sure pToRelease is still writeable. */ _ = libc.Int32FromInt32(0) /* If pPayload is part of the data area of pPage, then make sure pPage ** is still writeable */ _ = libc.Int32FromInt32(0) if nSrc >= n { libc.Xmemcpy(tls, pPayload, pSrc, uint32(n)) } else { if nSrc > 0 { n = nSrc libc.Xmemcpy(tls, pPayload, pSrc, uint32(n)) } else { libc.Xmemset(tls, pPayload, 0, uint32(n)) } } nPayload -= n if nPayload <= 0 { break } pPayload += uintptr(n) pSrc += uintptr(n) nSrc -= n spaceLeft -= n if spaceLeft == 0 { *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) pgnoPtrmap = *(*TPgno)(unsafe.Pointer(bp + 4)) /* Overflow page pointer-map entry page */ if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { for cond := true; cond; cond = _ptrmapPageno(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 4))) == *(*TPgno)(unsafe.Pointer(bp + 4)) || *(*TPgno)(unsafe.Pointer(bp + 4)) == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) { *(*TPgno)(unsafe.Pointer(bp + 4))++ } } *(*int32)(unsafe.Pointer(bp)) = _allocateBtreePage(tls, pBt, bp+8, bp+4, *(*TPgno)(unsafe.Pointer(bp + 4)), uint8(0)) /* If the database supports auto-vacuum, and the second or subsequent ** overflow page is being allocated, add an entry to the pointer-map ** for that page now. ** ** If this is the first overflow page, then write a partial entry ** to the pointer-map. If we write nothing to this pointer-map slot, ** then the optimistic overflow chain processing in clearCell() ** may misinterpret the uninitialized values and delete the ** wrong pages from the database. */ if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 && *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { if pgnoPtrmap != 0 { v4 = int32(PTRMAP_OVERFLOW2) } else { v4 = int32(PTRMAP_OVERFLOW1) } eType = uint8(v4) _ptrmapPut(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 4)), eType, pgnoPtrmap, bp) if *(*int32)(unsafe.Pointer(bp)) != 0 { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) } } if *(*int32)(unsafe.Pointer(bp)) != 0 { _releasePage(tls, pToRelease) return *(*int32)(unsafe.Pointer(bp)) } /* If pToRelease is not zero than pPrior points into the data area ** of pToRelease. Make sure pToRelease is still writeable. */ _ = libc.Int32FromInt32(0) /* If pPrior is part of the data area of pPage, then make sure pPage ** is still writeable */ _ = libc.Int32FromInt32(0) _sqlite3Put4byte(tls, pPrior, *(*TPgno)(unsafe.Pointer(bp + 4))) _releasePage(tls, pToRelease) pToRelease = *(*uintptr)(unsafe.Pointer(bp + 8)) pPrior = (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FaData _sqlite3Put4byte(tls, pPrior, uint32(0)) pPayload = (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FaData + 4 spaceLeft = int32((*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32(4)) } } _releasePage(tls, pToRelease) return SQLITE_OK } // C documentation // // /* // ** Remove the i-th cell from pPage. This routine effects pPage only. // ** The cell content is not freed or deallocated. It is assumed that // ** the cell content has been copied someplace else. This routine just // ** removes the reference to the cell from pPage. // ** // ** "sz" must be the number of bytes in the cell. // */ func _dropCell(tls *libc.TLS, pPage uintptr, idx int32, sz int32, pRC uintptr) { var data, ptr uintptr var hdr, rc int32 var pc Tu32 _, _, _, _, _ = data, hdr, pc, ptr, rc /* Beginning of the header. 0 most pages. 100 page 1 */ if *(*int32)(unsafe.Pointer(pRC)) != 0 { return } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) data = (*TMemPage)(unsafe.Pointer(pPage)).FaData ptr = (*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx) _ = libc.Int32FromInt32(0) pc = uint32(int32(*(*Tu8)(unsafe.Pointer(ptr)))< (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize { *(*int32)(unsafe.Pointer(pRC)) = _sqlite3CorruptError(tls, int32(77329)) return } rc = _freeSpace(tls, pPage, uint16(pc), uint16(sz)) if rc != 0 { *(*int32)(unsafe.Pointer(pRC)) = rc return } (*TMemPage)(unsafe.Pointer(pPage)).FnCell-- if int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) == 0 { libc.Xmemset(tls, data+uintptr(hdr+int32(1)), 0, uint32(4)) *(*Tu8)(unsafe.Pointer(data + uintptr(hdr+int32(7)))) = uint8(0) *(*Tu8)(unsafe.Pointer(data + uintptr(hdr+int32(5)))) = uint8((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(data + uintptr(hdr+int32(5)) + 1)) = uint8((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize) (*TMemPage)(unsafe.Pointer(pPage)).FnFree = int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize - uint32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset) - uint32((*TMemPage)(unsafe.Pointer(pPage)).FchildPtrSize) - uint32(8)) } else { libc.Xmemmove(tls, ptr, ptr+uintptr(2), uint32(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)-idx))) *(*Tu8)(unsafe.Pointer(data + uintptr(hdr+int32(3)))) = uint8(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(data + uintptr(hdr+int32(3)) + 1)) = uint8((*TMemPage)(unsafe.Pointer(pPage)).FnCell) *(*int32)(unsafe.Pointer(pPage + 20)) += int32(2) } } // C documentation // // /* // ** Insert a new cell on pPage at cell index "i". pCell points to the // ** content of the cell. // ** // ** If the cell content will fit on the page, then put it there. If it // ** will not fit, then make a copy of the cell content into pTemp if // ** pTemp is not null. Regardless of pTemp, allocate a new entry // ** in pPage->apOvfl[] and make it point to the cell content (either // ** in pTemp or the original pCell) and also record its index. // ** Allocating a new entry in pPage->aCell[] implies that // ** pPage->nOverflow is incremented. // ** // ** The insertCellFast() routine below works exactly the same as // ** insertCell() except that it lacks the pTemp and iChild parameters // ** which are assumed zero. Other than that, the two routines are the // ** same. // ** // ** Fixes or enhancements to this routine should be reflected in // ** insertCellFast()! // */ func _insertCell(tls *libc.TLS, pPage uintptr, i int32, pCell uintptr, sz int32, pTemp uintptr, iChild TPgno) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var data, pIns, v2, v4 uintptr var j, rc int32 var v1, v3 Tu8 var _ /* idx at bp+0 */ int32 var _ /* rc2 at bp+4 */ int32 _, _, _, _, _, _, _, _ = data, j, pIns, rc, v1, v2, v3, v4 *(*int32)(unsafe.Pointer(bp)) = 0 /* The point in pPage->aCellIdx[] where no cell inserted */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TMemPage)(unsafe.Pointer(pPage)).FnOverflow != 0 || sz+int32(2) > (*TMemPage)(unsafe.Pointer(pPage)).FnFree { if pTemp != 0 { libc.Xmemcpy(tls, pTemp, pCell, uint32(sz)) pCell = pTemp } _sqlite3Put4byte(tls, pCell, iChild) v2 = pPage + 12 v1 = *(*Tu8)(unsafe.Pointer(v2)) *(*Tu8)(unsafe.Pointer(v2))++ j = int32(v1) /* Comparison against ArraySize-1 since we hold back one extra slot ** as a contingency. In other words, never need more than 3 overflow ** slots but 4 are allocated, just to be safe. */ _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pPage + 36 + uintptr(j)*4)) = pCell *(*Tu16)(unsafe.Pointer(pPage + 28 + uintptr(j)*2)) = uint16(i) /* When multiple overflows occur, they are always sequential and in ** sorted order. This invariants arise because multiple overflows can ** only occur when inserting divider cells into the parent page during ** balancing, and the dividers are adjacent and sorted. */ _ = libc.Int32FromInt32(0) /* Overflows in sorted order */ _ = libc.Int32FromInt32(0) /* Overflows are sequential */ } else { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage) if rc != SQLITE_OK { return rc } _ = libc.Int32FromInt32(0) data = (*TMemPage)(unsafe.Pointer(pPage)).FaData _ = libc.Int32FromInt32(0) rc = _allocateSpace(tls, pPage, sz, bp) if rc != 0 { return rc } /* The allocateSpace() routine guarantees the following properties ** if it returns successfully */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pPage + 20)) -= int32(uint16(libc.Int32FromInt32(2) + sz)) /* In a corrupt database where an entry in the cell index section of ** a btree page has a value of 3 or less, the pCell value might point ** as many as 4 bytes in front of the start of the aData buffer for ** the source page. Make sure this does not cause problems by not ** reading the first 4 bytes */ libc.Xmemcpy(tls, data+uintptr(*(*int32)(unsafe.Pointer(bp))+int32(4)), pCell+uintptr(4), uint32(sz-int32(4))) _sqlite3Put4byte(tls, data+uintptr(*(*int32)(unsafe.Pointer(bp))), iChild) pIns = (*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(i*int32(2)) libc.Xmemmove(tls, pIns+uintptr(2), pIns, uint32(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)-i))) *(*Tu8)(unsafe.Pointer(pIns)) = uint8(*(*int32)(unsafe.Pointer(bp)) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(pIns + 1)) = uint8(*(*int32)(unsafe.Pointer(bp))) (*TMemPage)(unsafe.Pointer(pPage)).FnCell++ /* increment the cell count */ v4 = data + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(4)) *(*Tu8)(unsafe.Pointer(v4))++ v3 = *(*Tu8)(unsafe.Pointer(v4)) if int32(v3) == 0 { *(*Tu8)(unsafe.Pointer(data + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(3))))++ } _ = libc.Int32FromInt32(0) if (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FautoVacuum != 0 { *(*int32)(unsafe.Pointer(bp + 4)) = SQLITE_OK /* The cell may contain a pointer to an overflow page. If so, write ** the entry for the overflow page into the pointer map. */ _ptrmapPutOvflPtr(tls, pPage, pPage, pCell, bp+4) if *(*int32)(unsafe.Pointer(bp + 4)) != 0 { return *(*int32)(unsafe.Pointer(bp + 4)) } } } return SQLITE_OK } // C documentation // // /* // ** This variant of insertCell() assumes that the pTemp and iChild // ** parameters are both zero. Use this variant in sqlite3BtreeInsert() // ** for performance improvement, and also so that this variant is only // ** called from that one place, and is thus inlined, and thus runs must // ** faster. // ** // ** Fixes or enhancements to this routine should be reflected into // ** the insertCell() routine. // */ func _insertCellFast(tls *libc.TLS, pPage uintptr, i int32, pCell uintptr, sz int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var data, pIns, v2, v4 uintptr var j, rc int32 var v1, v3 Tu8 var _ /* idx at bp+0 */ int32 var _ /* rc2 at bp+4 */ int32 _, _, _, _, _, _, _, _ = data, j, pIns, rc, v1, v2, v3, v4 *(*int32)(unsafe.Pointer(bp)) = 0 /* The point in pPage->aCellIdx[] where no cell inserted */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if sz+int32(2) > (*TMemPage)(unsafe.Pointer(pPage)).FnFree { v2 = pPage + 12 v1 = *(*Tu8)(unsafe.Pointer(v2)) *(*Tu8)(unsafe.Pointer(v2))++ j = int32(v1) /* Comparison against ArraySize-1 since we hold back one extra slot ** as a contingency. In other words, never need more than 3 overflow ** slots but 4 are allocated, just to be safe. */ _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pPage + 36 + uintptr(j)*4)) = pCell *(*Tu16)(unsafe.Pointer(pPage + 28 + uintptr(j)*2)) = uint16(i) /* When multiple overflows occur, they are always sequential and in ** sorted order. This invariants arise because multiple overflows can ** only occur when inserting divider cells into the parent page during ** balancing, and the dividers are adjacent and sorted. */ _ = libc.Int32FromInt32(0) /* Overflows in sorted order */ _ = libc.Int32FromInt32(0) /* Overflows are sequential */ } else { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage) if rc != SQLITE_OK { return rc } _ = libc.Int32FromInt32(0) data = (*TMemPage)(unsafe.Pointer(pPage)).FaData _ = libc.Int32FromInt32(0) rc = _allocateSpace(tls, pPage, sz, bp) if rc != 0 { return rc } /* The allocateSpace() routine guarantees the following properties ** if it returns successfully */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pPage + 20)) -= int32(uint16(libc.Int32FromInt32(2) + sz)) libc.Xmemcpy(tls, data+uintptr(*(*int32)(unsafe.Pointer(bp))), pCell, uint32(sz)) pIns = (*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(i*int32(2)) libc.Xmemmove(tls, pIns+uintptr(2), pIns, uint32(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)-i))) *(*Tu8)(unsafe.Pointer(pIns)) = uint8(*(*int32)(unsafe.Pointer(bp)) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(pIns + 1)) = uint8(*(*int32)(unsafe.Pointer(bp))) (*TMemPage)(unsafe.Pointer(pPage)).FnCell++ /* increment the cell count */ v4 = data + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(4)) *(*Tu8)(unsafe.Pointer(v4))++ v3 = *(*Tu8)(unsafe.Pointer(v4)) if int32(v3) == 0 { *(*Tu8)(unsafe.Pointer(data + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(3))))++ } _ = libc.Int32FromInt32(0) if (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FautoVacuum != 0 { *(*int32)(unsafe.Pointer(bp + 4)) = SQLITE_OK /* The cell may contain a pointer to an overflow page. If so, write ** the entry for the overflow page into the pointer map. */ _ptrmapPutOvflPtr(tls, pPage, pPage, pCell, bp+4) if *(*int32)(unsafe.Pointer(bp + 4)) != 0 { return *(*int32)(unsafe.Pointer(bp + 4)) } } } return SQLITE_OK } /* ** The following parameters determine how many adjacent pages get involved ** in a balancing operation. NN is the number of neighbors on either side ** of the page that participate in the balancing operation. NB is the ** total number of pages that participate, including the target page and ** NN neighbors on either side. ** ** The minimum value of NN is 1 (of course). Increasing NN above 1 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance ** in exchange for a larger degradation in INSERT and UPDATE performance. ** The value of NN appears to give the best results overall. ** ** (Later:) The description above makes it seem as if these values are ** tunable - as if you could change them and recompile and it would all work. ** But that is unlikely. NB has been 3 since the inception of SQLite and ** we have never tested any other value. */ // C documentation // // /* // ** A CellArray object contains a cache of pointers and sizes for a // ** consecutive sequence of cells that might be held on multiple pages. // ** // ** The cells in this array are the divider cell or cells from the pParent // ** page plus up to three child pages. There are a total of nCell cells. // ** // ** pRef is a pointer to one of the pages that contributes cells. This is // ** used to access information such as MemPage.intKey and MemPage.pBt->pageSize // ** which should be common to all pages that contribute cells to this array. // ** // ** apCell[] and szCell[] hold, respectively, pointers to the start of each // ** cell and the size of each cell. Some of the apCell[] pointers might refer // ** to overflow cells. In other words, some apCel[] pointers might not point // ** to content area of the pages. // ** // ** A szCell[] of zero means the size of that cell has not yet been computed. // ** // ** The cells come from as many as four different pages: // ** // ** ----------- // ** | Parent | // ** ----------- // ** / | ** / | ** --------- --------- --------- // ** |Child-1| |Child-2| |Child-3| // ** --------- --------- --------- // ** // ** The order of cells is in the array is for an index btree is: // ** // ** 1. All cells from Child-1 in order // ** 2. The first divider cell from Parent // ** 3. All cells from Child-2 in order // ** 4. The second divider cell from Parent // ** 5. All cells from Child-3 in order // ** // ** For a table-btree (with rowids) the items 2 and 4 are empty because // ** content exists only in leaves and there are no divider cells. // ** // ** For an index btree, the apEnd[] array holds pointer to the end of page // ** for Child-1, the Parent, Child-2, the Parent (again), and Child-3, // ** respectively. The ixNx[] array holds the number of cells contained in // ** each of these 5 stages, and all stages to the left. Hence: // ** // ** ixNx[0] = Number of cells in Child-1. // ** ixNx[1] = Number of cells in Child-1 plus 1 for first divider. // ** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider. // ** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells // ** ixNx[4] = Total number of cells. // ** // ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2] // ** are used and they point to the leaf pages only, and the ixNx value are: // ** // ** ixNx[0] = Number of cells in Child-1. // ** ixNx[1] = Number of cells in Child-1 and Child-2. // ** ixNx[2] = Total number of cells. // ** // ** Sometimes when deleting, a child page can have zero cells. In those // ** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[] // ** entries, shift down. The end result is that each ixNx[] entry should // ** be larger than the previous // */ type TCellArray = struct { FnCell int32 FpRef uintptr FapCell uintptr FszCell uintptr FapEnd [6]uintptr FixNx [6]int32 } type CellArray = TCellArray type TCellArray1 = struct { FnCell int32 FpRef uintptr FapCell uintptr FszCell uintptr FapEnd [6]uintptr FixNx [6]int32 } type CellArray1 = TCellArray1 // C documentation // // /* // ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been // ** computed. // */ func _populateCellCache(tls *libc.TLS, p uintptr, idx int32, N int32) { var pRef, szCell uintptr _, _ = pRef, szCell pRef = (*TCellArray)(unsafe.Pointer(p)).FpRef szCell = (*TCellArray)(unsafe.Pointer(p)).FszCell _ = libc.Int32FromInt32(0) for N > 0 { _ = libc.Int32FromInt32(0) if int32(*(*Tu16)(unsafe.Pointer(szCell + uintptr(idx)*2))) == 0 { *(*Tu16)(unsafe.Pointer(szCell + uintptr(idx)*2)) = (*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pRef)).FxCellSize})))(tls, pRef, *(*uintptr)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(p)).FapCell + uintptr(idx)*4))) } else { _ = libc.Int32FromInt32(0) } idx++ N-- } } // C documentation // // /* // ** Return the size of the Nth element of the cell array // */ func _computeCellSize(tls *libc.TLS, p uintptr, N int32) (r Tu16) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*Tu16)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(p)).FszCell + uintptr(N)*2)) = (*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(p)).FpRef)).FxCellSize})))(tls, (*TCellArray)(unsafe.Pointer(p)).FpRef, *(*uintptr)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(p)).FapCell + uintptr(N)*4))) return *(*Tu16)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(p)).FszCell + uintptr(N)*2)) } func _cachedCellSize(tls *libc.TLS, p uintptr, N int32) (r Tu16) { _ = libc.Int32FromInt32(0) if *(*Tu16)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(p)).FszCell + uintptr(N)*2)) != 0 { return *(*Tu16)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(p)).FszCell + uintptr(N)*2)) } return _computeCellSize(tls, p, N) } // C documentation // // /* // ** Array apCell[] contains pointers to nCell b-tree page cells. The // ** szCell[] array contains the size in bytes of each cell. This function // ** replaces the current contents of page pPg with the contents of the cell // ** array. // ** // ** Some of the cells in apCell[] may currently be stored in pPg. This // ** function works around problems caused by this by making a copy of any // ** such cells before overwriting the page data. // ** // ** The MemPage.nFree field is invalidated by this function. It is the // ** responsibility of the caller to set it correctly. // */ func _rebuildPage(tls *libc.TLS, pCArray uintptr, iFirst int32, nCell int32, pPg uintptr) (r int32) { var aData, pCell, pCellptr, pData, pEnd, pSrcEnd, pTmp uintptr var hdr, i, iEnd, k, usableSize int32 var j Tu32 var sz Tu16 _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aData, hdr, i, iEnd, j, k, pCell, pCellptr, pData, pEnd, pSrcEnd, pTmp, sz, usableSize hdr = int32((*TMemPage)(unsafe.Pointer(pPg)).FhdrOffset) /* Offset of header on pPg */ aData = (*TMemPage)(unsafe.Pointer(pPg)).FaData /* Pointer to data for pPg */ usableSize = int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPg)).FpBt)).FusableSize) pEnd = aData + uintptr(usableSize) i = iFirst /* Start of cell content area */ iEnd = i + nCell /* Loop terminator */ pCellptr = (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx pTmp = _sqlite3PagerTempSpace(tls, (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPg)).FpBt)).FpPager) /* Current pCArray->apEnd[k] value */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) j = uint32(int32(*(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(5)))))< uint32(usableSize) { j = uint32(0) } libc.Xmemcpy(tls, pTmp+uintptr(j), aData+uintptr(j), uint32(usableSize)-j) k = 0 for { if !(k < libc.Int32FromInt32(NB)*libc.Int32FromInt32(2) && *(*int32)(unsafe.Pointer(pCArray + 40 + uintptr(k)*4)) <= i) { break } goto _1 _1: ; k++ } pSrcEnd = *(*uintptr)(unsafe.Pointer(pCArray + 16 + uintptr(k)*4)) pData = pEnd for int32(1) != 0 { pCell = *(*uintptr)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(pCArray)).FapCell + uintptr(i)*4)) sz = *(*Tu16)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(pCArray)).FszCell + uintptr(i)*2)) _ = libc.Int32FromInt32(0) if uint32(pCell) >= uint32(aData+uintptr(j)) && uint32(pCell) < uint32(pEnd) { if uint32(pCell+uintptr(sz)) > uint32(pEnd) { return _sqlite3CorruptError(tls, int32(77718)) } pCell = pTmp + uintptr(int32(pCell)-int32(aData)) } else { if uint32(pCell+uintptr(sz)) > uint32(pSrcEnd) && uint32(pCell) < uint32(pSrcEnd) { return _sqlite3CorruptError(tls, int32(77723)) } } pData -= uintptr(sz) *(*Tu8)(unsafe.Pointer(pCellptr)) = uint8((int32(pData) - int32(aData)) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(pCellptr + 1)) = uint8(int32(pData) - int32(aData)) pCellptr += uintptr(2) if pData < pCellptr { return _sqlite3CorruptError(tls, int32(77729)) } libc.Xmemmove(tls, pData, pCell, uint32(sz)) _ = libc.Int32FromInt32(0) i++ if i >= iEnd { break } if *(*int32)(unsafe.Pointer(pCArray + 40 + uintptr(k)*4)) <= i { k++ pSrcEnd = *(*uintptr)(unsafe.Pointer(pCArray + 16 + uintptr(k)*4)) } } /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ (*TMemPage)(unsafe.Pointer(pPg)).FnCell = uint16(nCell) (*TMemPage)(unsafe.Pointer(pPg)).FnOverflow = uint8(0) *(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(1)))) = uint8(libc.Int32FromInt32(0) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(1)) + 1)) = uint8(libc.Int32FromInt32(0)) *(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(3)))) = uint8(int32((*TMemPage)(unsafe.Pointer(pPg)).FnCell) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(3)) + 1)) = uint8((*TMemPage)(unsafe.Pointer(pPg)).FnCell) *(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(5)))) = uint8((int32(pData) - int32(aData)) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(5)) + 1)) = uint8(int32(pData) - int32(aData)) *(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(7)))) = uint8(0x00) return SQLITE_OK } // C documentation // // /* // ** The pCArray objects contains pointers to b-tree cells and the cell sizes. // ** This function attempts to add the cells stored in the array to page pPg. // ** If it cannot (because the page needs to be defragmented before the cells // ** will fit), non-zero is returned. Otherwise, if the cells are added // ** successfully, zero is returned. // ** // ** Argument pCellptr points to the first entry in the cell-pointer array // ** (part of page pPg) to populate. After cell apCell[0] is written to the // ** page body, a 16-bit offset is written to pCellptr. And so on, for each // ** cell in the array. It is the responsibility of the caller to ensure // ** that it is safe to overwrite this part of the cell-pointer array. // ** // ** When this function is called, *ppData points to the start of the // ** content area on page pPg. If the size of the content area is extended, // ** *ppData is updated to point to the new start of the content area // ** before returning. // ** // ** Finally, argument pBegin points to the byte immediately following the // ** end of the space required by this page for the cell-pointer area (for // ** all cells - not just those inserted by the current call). If the content // ** area must be extended to before this point in order to accommodate all // ** cells in apCell[], then the cells do not fit and non-zero is returned. // */ func _pageInsertArray(tls *libc.TLS, pPg uintptr, pBegin uintptr, ppData uintptr, pCellptr uintptr, iFirst int32, nCell int32, pCArray uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var aData, pData, pEnd, pSlot, v2 uintptr var i, iEnd, k, sz int32 var v3 bool var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _ = aData, i, iEnd, k, pData, pEnd, pSlot, sz, v2, v3 i = iFirst /* Loop counter - cell index to insert */ aData = (*TMemPage)(unsafe.Pointer(pPg)).FaData /* Complete page */ pData = *(*uintptr)(unsafe.Pointer(ppData)) /* Content area. A subset of aData[] */ iEnd = iFirst + nCell /* Maximum extent of cell data */ _ = libc.Int32FromInt32(0) /* Never called on page 1 */ if iEnd <= iFirst { return 0 } k = 0 for { if !(k < libc.Int32FromInt32(NB)*libc.Int32FromInt32(2) && *(*int32)(unsafe.Pointer(pCArray + 40 + uintptr(k)*4)) <= i) { break } goto _1 _1: ; k++ } pEnd = *(*uintptr)(unsafe.Pointer(pCArray + 16 + uintptr(k)*4)) for int32(1) != 0 { _ = libc.Int32FromInt32(0) sz = int32(*(*Tu16)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(pCArray)).FszCell + uintptr(i)*2))) if v3 = int32(*(*Tu8)(unsafe.Pointer(aData + 1))) == 0 && int32(*(*Tu8)(unsafe.Pointer(aData + 2))) == 0; !v3 { v2 = _pageFindSlot(tls, pPg, sz, bp) pSlot = v2 } if v3 || v2 == uintptr(0) { if int32(pData)-int32(pBegin) < sz { return int32(1) } pData -= uintptr(sz) pSlot = pData } /* pSlot and pCArray->apCell[i] will never overlap on a well-formed ** database. But they might for a corrupt database. Hence use memmove() ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */ _ = libc.Int32FromInt32(0) if uint32(*(*uintptr)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(pCArray)).FapCell + uintptr(i)*4))+uintptr(sz)) > uint32(pEnd) && uint32(*(*uintptr)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(pCArray)).FapCell + uintptr(i)*4))) < uint32(pEnd) { _ = libc.Int32FromInt32(0) _sqlite3CorruptError(tls, int32(77814)) return int32(1) } libc.Xmemmove(tls, pSlot, *(*uintptr)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(pCArray)).FapCell + uintptr(i)*4)), uint32(sz)) *(*Tu8)(unsafe.Pointer(pCellptr)) = uint8((int32(pSlot) - int32(aData)) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(pCellptr + 1)) = uint8(int32(pSlot) - int32(aData)) pCellptr += uintptr(2) i++ if i >= iEnd { break } if *(*int32)(unsafe.Pointer(pCArray + 40 + uintptr(k)*4)) <= i { k++ pEnd = *(*uintptr)(unsafe.Pointer(pCArray + 16 + uintptr(k)*4)) } } *(*uintptr)(unsafe.Pointer(ppData)) = pData return 0 } // C documentation // // /* // ** The pCArray object contains pointers to b-tree cells and their sizes. // ** // ** This function adds the space associated with each cell in the array // ** that is currently stored within the body of pPg to the pPg free-list. // ** The cell-pointers and other fields of the page are not updated. // ** // ** This function returns the total number of cells added to the free-list. // */ func _pageFreeArray(tls *libc.TLS, pPg uintptr, iFirst int32, nCell int32, pCArray uintptr) (r int32) { var aAfter, aOfst [10]int32 var aData, pCell, pEnd, pStart uintptr var i, iAfter, iEnd, iOfst, j, nFree, nRet, sz int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aAfter, aData, aOfst, i, iAfter, iEnd, iOfst, j, nFree, nRet, pCell, pEnd, pStart, sz aData = (*TMemPage)(unsafe.Pointer(pPg)).FaData pEnd = aData + uintptr((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPg)).FpBt)).FusableSize) pStart = aData + uintptr(int32((*TMemPage)(unsafe.Pointer(pPg)).FhdrOffset)+int32(8)+int32((*TMemPage)(unsafe.Pointer(pPg)).FchildPtrSize)) nRet = 0 iEnd = iFirst + nCell nFree = 0 i = iFirst for { if !(i < iEnd) { break } pCell = *(*uintptr)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(pCArray)).FapCell + uintptr(i)*4)) if uint32(pCell) >= uint32(pStart) && uint32(pCell) < uint32(pEnd) { /* No need to use cachedCellSize() here. The sizes of all cells that ** are to be freed have already been computing while deciding which ** cells need freeing */ sz = int32(*(*Tu16)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(pCArray)).FszCell + uintptr(i)*2))) _ = libc.Int32FromInt32(0) iOfst = int32(uint16(int32(pCell) - int32(aData))) iAfter = iOfst + sz j = 0 for { if !(j < nFree) { break } if aOfst[j] == iAfter { aOfst[j] = iOfst break } else { if aAfter[j] == iOfst { aAfter[j] = iAfter break } } goto _2 _2: ; j++ } if j >= nFree { if nFree >= int32(libc.Uint32FromInt64(40)/libc.Uint32FromInt64(4)) { j = 0 for { if !(j < nFree) { break } _freeSpace(tls, pPg, uint16(aOfst[j]), uint16(aAfter[j]-aOfst[j])) goto _3 _3: ; j++ } nFree = 0 } aOfst[nFree] = iOfst aAfter[nFree] = iAfter if aData+uintptr(iAfter) > pEnd { return 0 } nFree++ } nRet++ } goto _1 _1: ; i++ } j = 0 for { if !(j < nFree) { break } _freeSpace(tls, pPg, uint16(aOfst[j]), uint16(aAfter[j]-aOfst[j])) goto _4 _4: ; j++ } return nRet } // C documentation // // /* // ** pCArray contains pointers to and sizes of all cells in the page being // ** balanced. The current page, pPg, has pPg->nCell cells starting with // ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells // ** starting at apCell[iNew]. // ** // ** This routine makes the necessary adjustments to pPg so that it contains // ** the correct cells after being balanced. // ** // ** The pPg->nFree field is invalid when this function returns. It is the // ** responsibility of the caller to set it correctly. // */ func _editPage(tls *libc.TLS, pPg uintptr, iOld int32, iNew int32, nNew int32, pCArray uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var aData, pBegin, pCellptr uintptr var hdr, i, iCell, iNewEnd, iOldEnd, nAdd, nCell, nShift, nTail, v1 int32 var _ /* pData at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _ = aData, hdr, i, iCell, iNewEnd, iOldEnd, nAdd, nCell, nShift, nTail, pBegin, pCellptr, v1 aData = (*TMemPage)(unsafe.Pointer(pPg)).FaData hdr = int32((*TMemPage)(unsafe.Pointer(pPg)).FhdrOffset) pBegin = (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx + uintptr(nNew*int32(2)) nCell = int32((*TMemPage)(unsafe.Pointer(pPg)).FnCell) iOldEnd = iOld + int32((*TMemPage)(unsafe.Pointer(pPg)).FnCell) + int32((*TMemPage)(unsafe.Pointer(pPg)).FnOverflow) iNewEnd = iNew + nNew /* Remove cells from the start and end of the page */ _ = libc.Int32FromInt32(0) if iOld < iNew { nShift = _pageFreeArray(tls, pPg, iOld, iNew-iOld, pCArray) if nShift > nCell { return _sqlite3CorruptError(tls, int32(77936)) } libc.Xmemmove(tls, (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx, (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx+uintptr(nShift*int32(2)), uint32(nCell*int32(2))) nCell -= nShift } if iNewEnd < iOldEnd { nTail = _pageFreeArray(tls, pPg, iNewEnd, iOldEnd-iNewEnd, pCArray) _ = libc.Int32FromInt32(0) nCell -= nTail } *(*uintptr)(unsafe.Pointer(bp)) = aData + uintptr(int32(*(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(5)))))< (*TMemPage)(unsafe.Pointer(pPg)).FaDataEnd { goto editpage_fail } /* Add cells to the start of the page */ if iNew < iOld { if nNew < iOld-iNew { v1 = nNew } else { v1 = iOld - iNew } nAdd = v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pCellptr = (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx libc.Xmemmove(tls, pCellptr+uintptr(nAdd*int32(2)), pCellptr, uint32(nCell*int32(2))) if _pageInsertArray(tls, pPg, pBegin, bp, pCellptr, iNew, nAdd, pCArray) != 0 { goto editpage_fail } nCell += nAdd } /* Add any overflow cells */ i = 0 for { if !(i < int32((*TMemPage)(unsafe.Pointer(pPg)).FnOverflow)) { break } iCell = iOld + int32(*(*Tu16)(unsafe.Pointer(pPg + 28 + uintptr(i)*2))) - iNew if iCell >= 0 && iCell < nNew { pCellptr = (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx + uintptr(iCell*int32(2)) if nCell > iCell { libc.Xmemmove(tls, pCellptr+2, pCellptr, uint32((nCell-iCell)*int32(2))) } nCell++ _cachedCellSize(tls, pCArray, iCell+iNew) if _pageInsertArray(tls, pPg, pBegin, bp, pCellptr, iCell+iNew, int32(1), pCArray) != 0 { goto editpage_fail } } goto _2 _2: ; i++ } /* Append cells to the end of the page */ _ = libc.Int32FromInt32(0) pCellptr = (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx + uintptr(nCell*int32(2)) if _pageInsertArray(tls, pPg, pBegin, bp, pCellptr, iNew+nCell, nNew-nCell, pCArray) != 0 { goto editpage_fail } (*TMemPage)(unsafe.Pointer(pPg)).FnCell = uint16(nNew) (*TMemPage)(unsafe.Pointer(pPg)).FnOverflow = uint8(0) *(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(3)))) = uint8(int32((*TMemPage)(unsafe.Pointer(pPg)).FnCell) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(3)) + 1)) = uint8((*TMemPage)(unsafe.Pointer(pPg)).FnCell) *(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(5)))) = uint8((int32(*(*uintptr)(unsafe.Pointer(bp))) - int32(aData)) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(5)) + 1)) = uint8(int32(*(*uintptr)(unsafe.Pointer(bp))) - int32(aData)) return SQLITE_OK goto editpage_fail editpage_fail: ; /* Unable to edit this page. Rebuild it from scratch instead. */ if nNew < int32(1) { return _sqlite3CorruptError(tls, int32(78010)) } _populateCellCache(tls, pCArray, iNew, nNew) return _rebuildPage(tls, pCArray, iNew, nNew, pPg) } // C documentation // // /* // ** This version of balance() handles the common special case where // ** a new entry is being inserted on the extreme right-end of the // ** tree, in other words, when the new entry will become the largest // ** entry in the tree. // ** // ** Instead of trying to balance the 3 right-most leaf pages, just add // ** a new page to the right-hand side and put the one new entry in // ** that page. This leaves the right side of the tree somewhat // ** unbalanced. But odds are that we will be inserting new entries // ** at the end soon afterwards so the nearly empty page will quickly // ** fill up. On average. // ** // ** pPage is the leaf page which is the right-most page in the tree. // ** pParent is its parent. pPage must have a single overflow entry // ** which is also the right-most entry on the page. // ** // ** The pSpace buffer is used to store a temporary copy of the divider // ** cell that will be inserted into pParent. Such a cell consists of a 4 // ** byte page number followed by a variable length integer. In other // ** words, at most 13 bytes. Hence the pSpace buffer must be at // ** least 13 bytes in size. // */ func _balance_quick(tls *libc.TLS, pParent uintptr, pPage uintptr, pSpace uintptr) (r int32) { bp := tls.Alloc(96) defer tls.Free(96) var pBt, pOut, pStop, v1, v3, v4 uintptr var v2 Tu8 var _ /* b at bp+20 */ TCellArray var _ /* pCell at bp+12 */ uintptr var _ /* pNew at bp+0 */ uintptr var _ /* pgnoNew at bp+8 */ TPgno var _ /* rc at bp+4 */ int32 var _ /* szCell at bp+16 */ Tu16 _, _, _, _, _, _, _ = pBt, pOut, pStop, v1, v2, v3, v4 pBt = (*TMemPage)(unsafe.Pointer(pPage)).FpBt /* Page number of pNew */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) == 0 { return _sqlite3CorruptError(tls, int32(78050)) } /* dbfuzz001.test */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Allocate a new page. This page will become the right-sibling of ** pPage. Make the parent page writable, so that the new divider cell ** may be inserted. If both these operations are successful, proceed. */ *(*int32)(unsafe.Pointer(bp + 4)) = _allocateBtreePage(tls, pBt, bp, bp+8, uint32(0), uint8(0)) if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK { pOut = pSpace + 4 *(*uintptr)(unsafe.Pointer(bp + 12)) = *(*uintptr)(unsafe.Pointer(pPage + 36)) *(*Tu16)(unsafe.Pointer(bp + 16)) = (*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxCellSize})))(tls, pPage, *(*uintptr)(unsafe.Pointer(bp + 12))) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _zeroPage(tls, *(*uintptr)(unsafe.Pointer(bp)), libc.Int32FromInt32(PTF_INTKEY)|libc.Int32FromInt32(PTF_LEAFDATA)|libc.Int32FromInt32(PTF_LEAF)) (*(*TCellArray)(unsafe.Pointer(bp + 20))).FnCell = int32(1) (*(*TCellArray)(unsafe.Pointer(bp + 20))).FpRef = pPage (*(*TCellArray)(unsafe.Pointer(bp + 20))).FapCell = bp + 12 (*(*TCellArray)(unsafe.Pointer(bp + 20))).FszCell = bp + 16 *(*uintptr)(unsafe.Pointer(bp + 20 + 16)) = (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd *(*int32)(unsafe.Pointer(bp + 20 + 40)) = int32(2) *(*int32)(unsafe.Pointer(bp + 4)) = _rebuildPage(tls, bp+20, 0, int32(1), *(*uintptr)(unsafe.Pointer(bp))) if *(*int32)(unsafe.Pointer(bp + 4)) != 0 { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) return *(*int32)(unsafe.Pointer(bp + 4)) } (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnFree = int32((*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FcellOffset) - uint32(2) - uint32(*(*Tu16)(unsafe.Pointer(bp + 16)))) /* If this is an auto-vacuum database, update the pointer map ** with entries for the new page, and any pointer from the ** cell on the page to an overflow page. If either of these ** operations fails, the return code is set, but the contents ** of the parent page are still manipulated by the code below. ** That is Ok, at this point the parent page is guaranteed to ** be marked as dirty. Returning an error code will cause a ** rollback, undoing any changes made to the parent page. */ if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { _ptrmapPut(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 8)), uint8(PTRMAP_BTREE), (*TMemPage)(unsafe.Pointer(pParent)).Fpgno, bp+4) if int32(*(*Tu16)(unsafe.Pointer(bp + 16))) > int32((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FminLocal) { _ptrmapPutOvflPtr(tls, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 12)), bp+4) } } /* Create a divider cell to insert into pParent. The divider cell ** consists of a 4-byte page number (the page number of pPage) and ** a variable length key value (which must be the same value as the ** largest key on pPage). ** ** To find the largest key value on pPage, first find the right-most ** cell on pPage. The first two fields of this cell are the ** record-length (a variable length integer at most 32-bits in size) ** and the key value (a variable length integer, may have any value). ** The first of the while(...) loops below skips over the record-length ** field. The second while(...) loop copies the key value from the ** cell on pPage into the pSpace buffer. */ *(*uintptr)(unsafe.Pointer(bp + 12)) = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)-int32(1)))))))) pStop = *(*uintptr)(unsafe.Pointer(bp + 12)) + 9 for { v1 = *(*uintptr)(unsafe.Pointer(bp + 12)) *(*uintptr)(unsafe.Pointer(bp + 12))++ if !(int32(*(*Tu8)(unsafe.Pointer(v1)))&int32(0x80) != 0 && *(*uintptr)(unsafe.Pointer(bp + 12)) < pStop) { break } } pStop = *(*uintptr)(unsafe.Pointer(bp + 12)) + 9 for { v3 = *(*uintptr)(unsafe.Pointer(bp + 12)) *(*uintptr)(unsafe.Pointer(bp + 12))++ v2 = *(*Tu8)(unsafe.Pointer(v3)) v4 = pOut pOut++ *(*Tu8)(unsafe.Pointer(v4)) = v2 if !(int32(v2)&int32(0x80) != 0 && *(*uintptr)(unsafe.Pointer(bp + 12)) < pStop) { break } } /* Insert the new divider cell into pParent. */ if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 4)) = _insertCell(tls, pParent, int32((*TMemPage)(unsafe.Pointer(pParent)).FnCell), pSpace, int32(pOut)-int32(pSpace), uintptr(0), (*TMemPage)(unsafe.Pointer(pPage)).Fpgno) } /* Set the right-child pointer of pParent to point to the new page. */ _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pParent)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pParent)).FhdrOffset)+int32(8)), *(*TPgno)(unsafe.Pointer(bp + 8))) /* Release the reference to the new page. */ _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) } return *(*int32)(unsafe.Pointer(bp + 4)) } // C documentation // // /* // ** This function is used to copy the contents of the b-tree node stored // ** on page pFrom to page pTo. If page pFrom was not a leaf page, then // ** the pointer-map entries for each child page are updated so that the // ** parent page stored in the pointer map is page pTo. If pFrom contained // ** any cells with overflow page pointers, then the corresponding pointer // ** map entries are also updated so that the parent page is page pTo. // ** // ** If pFrom is currently carrying any overflow cells (entries in the // ** MemPage.apOvfl[] array), they are not copied to pTo. // ** // ** Before returning, page pTo is reinitialized using btreeInitPage(). // ** // ** The performance of this function is not critical. It is only used by // ** the balance_shallower() and balance_deeper() procedures, neither of // ** which are called often under normal circumstances. // */ func _copyNodeContent(tls *libc.TLS, pFrom uintptr, pTo uintptr, pRC uintptr) { var aFrom, aTo, pBt uintptr var iData, iFromHdr, iToHdr, rc, v1 int32 _, _, _, _, _, _, _, _ = aFrom, aTo, iData, iFromHdr, iToHdr, pBt, rc, v1 if *(*int32)(unsafe.Pointer(pRC)) == SQLITE_OK { pBt = (*TMemPage)(unsafe.Pointer(pFrom)).FpBt aFrom = (*TMemPage)(unsafe.Pointer(pFrom)).FaData aTo = (*TMemPage)(unsafe.Pointer(pTo)).FaData iFromHdr = int32((*TMemPage)(unsafe.Pointer(pFrom)).FhdrOffset) if (*TMemPage)(unsafe.Pointer(pTo)).Fpgno == uint32(1) { v1 = int32(100) } else { v1 = 0 } iToHdr = v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Copy the b-tree node content from page pFrom to page pTo. */ iData = int32(*(*Tu8)(unsafe.Pointer(aFrom + uintptr(iFromHdr+int32(5)))))<aCell[] */ *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK /* Value of pPage->aData[0] */ iSpace1 = 0 /* First unused byte of aSpace1[] */ iOvflSpace = 0 /* Parsed information on cells being balanced */ libc.Xmemset(tls, bp+60, 0, uint32(5)) libc.Xmemset(tls, bp+68, 0, uint32(64)) pBt = (*TMemPage)(unsafe.Pointer(pParent)).FpBt _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* At this point pParent may have at most one overflow cell. And if ** this overflow cell is present, it must be the cell with ** index iParentIdx. This scenario comes about when this function ** is called (indirectly) from sqlite3BtreeDelete(). */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if !(aOvflSpace != 0) { return int32(SQLITE_NOMEM) } _ = libc.Int32FromInt32(0) /* Find the sibling pages to balance. Also locate the cells in pParent ** that divide the siblings. An attempt is made to find NN siblings on ** either side of pPage. More siblings are taken from one side, however, ** if there are fewer than NN siblings on the other side. If pParent ** has NB or fewer children then all children of pParent are taken. ** ** This loop also drops the divider cells from the parent page. This ** way, the remainder of the function does not have to deal with any ** overflow cells in the parent page, since if any existed they will ** have already been removed. */ i = int32((*TMemPage)(unsafe.Pointer(pParent)).FnOverflow) + int32((*TMemPage)(unsafe.Pointer(pParent)).FnCell) if i < int32(2) { nxDiv = 0 } else { _ = libc.Int32FromInt32(0) if iParentIdx == 0 { nxDiv = 0 } else { if iParentIdx == i { nxDiv = i - int32(2) + bBulk } else { nxDiv = iParentIdx - int32(1) } } i = int32(2) - bBulk } nOld = i + int32(1) if i+nxDiv-int32((*TMemPage)(unsafe.Pointer(pParent)).FnOverflow) == int32((*TMemPage)(unsafe.Pointer(pParent)).FnCell) { pRight = (*TMemPage)(unsafe.Pointer(pParent)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pParent)).FhdrOffset)+int32(8)) } else { pRight = (*TMemPage)(unsafe.Pointer(pParent)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pParent)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pParent)).FaCellIdx + uintptr(int32(2)*(i+nxDiv-int32((*TMemPage)(unsafe.Pointer(pParent)).FnOverflow)))))))) } *(*TPgno)(unsafe.Pointer(bp + 56)) = _sqlite3Get4byte(tls, pRight) for int32(1) != 0 { if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _getAndInitPage(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 56)), bp+4+uintptr(i)*4, 0) } if *(*int32)(unsafe.Pointer(bp)) != 0 { libc.Xmemset(tls, bp+4, 0, uint32(i+libc.Int32FromInt32(1))*uint32(4)) goto balance_cleanup } if (*TMemPage)(unsafe.Pointer((*(*[3]uintptr)(unsafe.Pointer(bp + 4)))[i])).FnFree < 0 { *(*int32)(unsafe.Pointer(bp)) = _btreeComputeFreeSpace(tls, (*(*[3]uintptr)(unsafe.Pointer(bp + 4)))[i]) if *(*int32)(unsafe.Pointer(bp)) != 0 { libc.Xmemset(tls, bp+4, 0, uint32(i)*uint32(4)) goto balance_cleanup } } nMaxCells += int32((*TMemPage)(unsafe.Pointer((*(*[3]uintptr)(unsafe.Pointer(bp + 4)))[i])).FnCell) + int32(libc.Uint32FromInt64(16)/libc.Uint32FromInt64(4)) v1 = i i-- if v1 == 0 { break } if (*TMemPage)(unsafe.Pointer(pParent)).FnOverflow != 0 && i+nxDiv == int32(*(*Tu16)(unsafe.Pointer(pParent + 28))) { apDiv[i] = *(*uintptr)(unsafe.Pointer(pParent + 36)) *(*TPgno)(unsafe.Pointer(bp + 56)) = _sqlite3Get4byte(tls, apDiv[i]) (*(*[5]int32)(unsafe.Pointer(bp + 36)))[i] = int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pParent)).FxCellSize})))(tls, pParent, apDiv[i])) (*TMemPage)(unsafe.Pointer(pParent)).FnOverflow = uint8(0) } else { apDiv[i] = (*TMemPage)(unsafe.Pointer(pParent)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pParent)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pParent)).FaCellIdx + uintptr(int32(2)*(i+nxDiv-int32((*TMemPage)(unsafe.Pointer(pParent)).FnOverflow)))))))) *(*TPgno)(unsafe.Pointer(bp + 56)) = _sqlite3Get4byte(tls, apDiv[i]) (*(*[5]int32)(unsafe.Pointer(bp + 36)))[i] = int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pParent)).FxCellSize})))(tls, pParent, apDiv[i])) /* Drop the cell from the parent page. apDiv[i] still points to ** the cell within the parent, even though it has been dropped. ** This is safe because dropping a cell only overwrites the first ** four bytes of it, and this function does not need the first ** four bytes of the divider cell. So the pointer is safe to use ** later on. ** ** But not if we are in secure-delete mode. In secure-delete mode, ** the dropCell() routine will overwrite the entire cell with zeroes. ** In this case, temporarily copy the cell into the aOvflSpace[] ** buffer. It will be copied out again as soon as the aSpace[] buffer ** is allocated. */ if int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_FAST_SECURE) != 0 { /* If the following if() condition is not true, the db is corrupted. ** The call to dropCell() below will detect this. */ iOff = int32(apDiv[i]) - int32((*TMemPage)(unsafe.Pointer(pParent)).FaData) if iOff+(*(*[5]int32)(unsafe.Pointer(bp + 36)))[i] <= int32((*TBtShared)(unsafe.Pointer(pBt)).FusableSize) { libc.Xmemcpy(tls, aOvflSpace+uintptr(iOff), apDiv[i], uint32((*(*[5]int32)(unsafe.Pointer(bp + 36)))[i])) apDiv[i] = aOvflSpace + uintptr(int32(apDiv[i])-int32((*TMemPage)(unsafe.Pointer(pParent)).FaData)) } } _dropCell(tls, pParent, i+nxDiv-int32((*TMemPage)(unsafe.Pointer(pParent)).FnOverflow), (*(*[5]int32)(unsafe.Pointer(bp + 36)))[i], bp) } } /* Make nMaxCells a multiple of 4 in order to preserve 8-byte ** alignment */ nMaxCells = (nMaxCells + int32(3)) & ^libc.Int32FromInt32(3) /* ** Allocate space for memory structures */ szScratch = int32(uint32(nMaxCells)*uint32(4) + uint32(nMaxCells)*uint32(2) + (*TBtShared)(unsafe.Pointer(pBt)).FpageSize) /* aSpace1 */ _ = libc.Int32FromInt32(0) (*(*TCellArray)(unsafe.Pointer(bp + 68))).FapCell = _sqlite3DbMallocRaw(tls, uintptr(0), uint64(szScratch)) if (*(*TCellArray)(unsafe.Pointer(bp + 68))).FapCell == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_NOMEM) goto balance_cleanup } (*(*TCellArray)(unsafe.Pointer(bp + 68))).FszCell = (*(*TCellArray)(unsafe.Pointer(bp + 68))).FapCell + uintptr(nMaxCells)*4 aSpace1 = (*(*TCellArray)(unsafe.Pointer(bp + 68))).FszCell + uintptr(nMaxCells)*2 _ = libc.Int32FromInt32(0) /* ** Load pointers to all cells on sibling pages and the divider cells ** into the local b.apCell[] array. Make copies of the divider cells ** into space obtained from aSpace1[]. The divider cells have already ** been removed from pParent. ** ** If the siblings are on leaf pages, then the child pointers of the ** divider cells are stripped from the cells before they are copied ** into aSpace1[]. In this way, all cells in b.apCell[] are without ** child pointers. If siblings are not leaves, then all cell in ** b.apCell[] include child pointers. Either way, all cells in b.apCell[] ** are alike. ** ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. ** leafData: 1 if pPage holds key+data and pParent holds only keys. */ (*(*TCellArray)(unsafe.Pointer(bp + 68))).FpRef = (*(*[3]uintptr)(unsafe.Pointer(bp + 4)))[0] leafCorrection = uint16(int32((*TMemPage)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FpRef)).Fleaf) * int32(4)) leafData = int32((*TMemPage)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FpRef)).FintKeyLeaf) i = 0 for { if !(i < nOld) { break } pOld = (*(*[3]uintptr)(unsafe.Pointer(bp + 4)))[i] limit = int32((*TMemPage)(unsafe.Pointer(pOld)).FnCell) aData = (*TMemPage)(unsafe.Pointer(pOld)).FaData maskPage = (*TMemPage)(unsafe.Pointer(pOld)).FmaskPage piCell = aData + uintptr((*TMemPage)(unsafe.Pointer(pOld)).FcellOffset) /* Verify that all sibling pages are of the same "type" (table-leaf, ** table-interior, index-leaf, or index-interior). */ if int32(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pOld)).FaData))) != int32(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer((*(*[3]uintptr)(unsafe.Pointer(bp + 4)))[0])).FaData))) { *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(78471)) goto balance_cleanup } /* Load b.apCell[] with pointers to all cells in pOld. If pOld ** contains overflow cells, include them in the b.apCell[] array ** in the correct spot. ** ** Note that when there are multiple overflow cells, it is always the ** case that they are sequential and adjacent. This invariant arises ** because multiple overflows can only occurs when inserting divider ** cells into a parent on a prior balance, and divider cells are always ** adjacent and are inserted in order. There is an assert() tagged ** with "NOTE 1" in the overflow cell insertion loop to prove this ** invariant. ** ** This must be done in advance. Once the balance starts, the cell ** offset section of the btree page will be overwritten and we will no ** long be able to find the cells if a pointer to each cell is not saved ** first. */ libc.Xmemset(tls, (*(*TCellArray)(unsafe.Pointer(bp + 68))).FszCell+uintptr((*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell)*2, 0, uint32(2)*uint32(limit+int32((*TMemPage)(unsafe.Pointer(pOld)).FnOverflow))) if int32((*TMemPage)(unsafe.Pointer(pOld)).FnOverflow) > 0 { if limit < int32(*(*Tu16)(unsafe.Pointer(pOld + 28))) { *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(78495)) goto balance_cleanup } limit = int32(*(*Tu16)(unsafe.Pointer(pOld + 28))) j = 0 for { if !(j < limit) { break } *(*uintptr)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FapCell + uintptr((*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell)*4)) = aData + uintptr(int32(maskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer(piCell))))) piCell += uintptr(2) (*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell++ goto _3 _3: ; j++ } k = 0 for { if !(k < int32((*TMemPage)(unsafe.Pointer(pOld)).FnOverflow)) { break } _ = libc.Int32FromInt32(0) /* NOTE 1 */ *(*uintptr)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FapCell + uintptr((*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell)*4)) = *(*uintptr)(unsafe.Pointer(pOld + 36 + uintptr(k)*4)) (*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell++ goto _4 _4: ; k++ } } piEnd = aData + uintptr((*TMemPage)(unsafe.Pointer(pOld)).FcellOffset) + uintptr(int32(2)*int32((*TMemPage)(unsafe.Pointer(pOld)).FnCell)) for piCell < piEnd { _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FapCell + uintptr((*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell)*4)) = aData + uintptr(int32(maskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer(piCell))))) piCell += uintptr(2) (*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell++ } _ = libc.Int32FromInt32(0) cntOld[i] = (*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell if i < nOld-int32(1) && !(leafData != 0) { sz = uint16((*(*[5]int32)(unsafe.Pointer(bp + 36)))[i]) _ = libc.Int32FromInt32(0) *(*Tu16)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FszCell + uintptr((*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell)*2)) = sz pTemp = aSpace1 + uintptr(iSpace1) iSpace1 += int32(sz) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, pTemp, apDiv[i], uint32(sz)) *(*uintptr)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FapCell + uintptr((*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell)*4)) = pTemp + uintptr(leafCorrection) _ = libc.Int32FromInt32(0) *(*Tu16)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FszCell + uintptr((*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell)*2)) = uint16(int32(*(*Tu16)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FszCell + uintptr((*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell)*2))) - int32(leafCorrection)) if !((*TMemPage)(unsafe.Pointer(pOld)).Fleaf != 0) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* The right pointer of the child page pOld becomes the left ** pointer of the divider cell */ libc.Xmemcpy(tls, *(*uintptr)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FapCell + uintptr((*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell)*4)), (*TMemPage)(unsafe.Pointer(pOld)).FaData+8, uint32(4)) } else { _ = libc.Int32FromInt32(0) for int32(*(*Tu16)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FszCell + uintptr((*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell)*2))) < int32(4) { /* Do not allow any cells smaller than 4 bytes. If a smaller cell ** does exist, pad it with 0x00 bytes. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) v5 = iSpace1 iSpace1++ *(*Tu8)(unsafe.Pointer(aSpace1 + uintptr(v5))) = uint8(0x00) *(*Tu16)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FszCell + uintptr((*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell)*2))++ } } (*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell++ } goto _2 _2: ; i++ } /* ** Figure out the number of pages needed to hold all b.nCell cells. ** Store this number in "k". Also compute szNew[] which is the total ** size of all cells on the i-th page and cntNew[] which is the index ** in b.apCell[] of the cell that divides page i from page i+1. ** cntNew[k] should equal b.nCell. ** ** Values computed by this block: ** ** k: The total number of sibling pages ** szNew[i]: Spaced used on the i-th sibling page. ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to ** the right of the i-th sibling page. ** usableSpace: Number of bytes of space available on each sibling. ** */ usableSpace = int32((*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32(12) + uint32(leafCorrection)) v7 = libc.Int32FromInt32(0) k = v7 i = v7 for { if !(i < nOld) { break } p = (*(*[3]uintptr)(unsafe.Pointer(bp + 4)))[i] *(*uintptr)(unsafe.Pointer(bp + 68 + 16 + uintptr(k)*4)) = (*TMemPage)(unsafe.Pointer(p)).FaDataEnd *(*int32)(unsafe.Pointer(bp + 68 + 40 + uintptr(k)*4)) = cntOld[i] if k != 0 && *(*int32)(unsafe.Pointer(bp + 68 + 40 + uintptr(k)*4)) == *(*int32)(unsafe.Pointer(bp + 68 + 40 + uintptr(k-int32(1))*4)) { k-- /* Omit b.ixNx[] entry for child pages with no cells */ } if !(leafData != 0) { k++ *(*uintptr)(unsafe.Pointer(bp + 68 + 16 + uintptr(k)*4)) = (*TMemPage)(unsafe.Pointer(pParent)).FaDataEnd *(*int32)(unsafe.Pointer(bp + 68 + 40 + uintptr(k)*4)) = cntOld[i] + int32(1) } _ = libc.Int32FromInt32(0) (*(*[5]int32)(unsafe.Pointer(bp + 36)))[i] = usableSpace - (*TMemPage)(unsafe.Pointer(p)).FnFree j = 0 for { if !(j < int32((*TMemPage)(unsafe.Pointer(p)).FnOverflow)) { break } *(*int32)(unsafe.Pointer(bp + 36 + uintptr(i)*4)) += int32(2) + int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(p)).FxCellSize})))(tls, p, *(*uintptr)(unsafe.Pointer(p + 36 + uintptr(j)*4)))) goto _8 _8: ; j++ } cntNew[i] = cntOld[i] goto _6 _6: ; i++ k++ } k = nOld i = 0 for { if !(i < k) { break } for (*(*[5]int32)(unsafe.Pointer(bp + 36)))[i] > usableSpace { if i+int32(1) >= k { k = i + int32(2) if k > libc.Int32FromInt32(NB)+libc.Int32FromInt32(2) { *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(78596)) goto balance_cleanup } (*(*[5]int32)(unsafe.Pointer(bp + 36)))[k-int32(1)] = 0 cntNew[k-int32(1)] = (*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell } sz1 = int32(2) + int32(_cachedCellSize(tls, bp+68, cntNew[i]-int32(1))) *(*int32)(unsafe.Pointer(bp + 36 + uintptr(i)*4)) -= sz1 if !(leafData != 0) { if cntNew[i] < (*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell { sz1 = int32(2) + int32(_cachedCellSize(tls, bp+68, cntNew[i])) } else { sz1 = 0 } } *(*int32)(unsafe.Pointer(bp + 36 + uintptr(i+int32(1))*4)) += sz1 cntNew[i]-- } for cntNew[i] < (*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell { sz1 = int32(2) + int32(_cachedCellSize(tls, bp+68, cntNew[i])) if (*(*[5]int32)(unsafe.Pointer(bp + 36)))[i]+sz1 > usableSpace { break } *(*int32)(unsafe.Pointer(bp + 36 + uintptr(i)*4)) += sz1 cntNew[i]++ if !(leafData != 0) { if cntNew[i] < (*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell { sz1 = int32(2) + int32(_cachedCellSize(tls, bp+68, cntNew[i])) } else { sz1 = 0 } } *(*int32)(unsafe.Pointer(bp + 36 + uintptr(i+int32(1))*4)) -= sz1 } if cntNew[i] >= (*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell { k = i + int32(1) } else { if i > 0 { v10 = cntNew[i-int32(1)] } else { v10 = 0 } if cntNew[i] <= v10 { *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(78629)) goto balance_cleanup } } goto _9 _9: ; i++ } /* ** The packing computed by the previous block is biased toward the siblings ** on the left side (siblings with smaller keys). The left siblings are ** always nearly full, while the right-most sibling might be nearly empty. ** The next block of code attempts to adjust the packing of siblings to ** get a better balance. ** ** This adjustment is more than an optimization. The packing above might ** be so out of balance as to be illegal. For example, the right-most ** sibling might be completely empty. This adjustment is not optional. */ i = k - int32(1) for { if !(i > 0) { break } szRight = (*(*[5]int32)(unsafe.Pointer(bp + 36)))[i] /* Size of sibling on the right */ szLeft = (*(*[5]int32)(unsafe.Pointer(bp + 36)))[i-int32(1)] /* Index of first cell to the left of right sibling */ r = cntNew[i-int32(1)] - int32(1) d = r + int32(1) - leafData _cachedCellSize(tls, bp+68, d) for cond := true; cond; cond = r >= 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) szR = int32(_cachedCellSize(tls, bp+68, r)) szD = int32(*(*Tu16)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FszCell + uintptr(d)*2))) if v14 = szRight != 0; v14 { if v13 = bBulk != 0; !v13 { if i == k-int32(1) { v12 = 0 } else { v12 = int32(2) } } } if v14 && (v13 || szRight+szD+int32(2) > szLeft-(szR+v12)) { break } szRight += szD + int32(2) szLeft -= szR + int32(2) cntNew[i-int32(1)] = r r-- d-- } (*(*[5]int32)(unsafe.Pointer(bp + 36)))[i] = szRight (*(*[5]int32)(unsafe.Pointer(bp + 36)))[i-int32(1)] = szLeft if i > int32(1) { v15 = cntNew[i-int32(2)] } else { v15 = 0 } if cntNew[i-int32(1)] <= v15 { *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(78673)) goto balance_cleanup } goto _11 _11: ; i-- } /* Sanity check: For a non-corrupt database file one of the following ** must be true: ** (1) We found one or more cells (cntNew[0])>0), or ** (2) pPage is a virtual root page. A virtual root page is when ** the real root page is page 1 and we are the only child of ** that page. */ _ = libc.Int32FromInt32(0) /* ** Allocate k new pages. Reuse old pages where possible. */ pageFlags = int32(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer((*(*[3]uintptr)(unsafe.Pointer(bp + 4)))[0])).FaData))) i = 0 for { if !(i < k) { break } if i < nOld { v17 = (*(*[3]uintptr)(unsafe.Pointer(bp + 4)))[i] (*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[i] = v17 *(*uintptr)(unsafe.Pointer(bp + 132)) = v17 (*(*[3]uintptr)(unsafe.Pointer(bp + 4)))[i] = uintptr(0) *(*int32)(unsafe.Pointer(bp)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 132)))).FpDbPage) nNew++ if _sqlite3PagerPageRefcount(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 132)))).FpDbPage) != int32(1)+libc.BoolInt32(i == iParentIdx-nxDiv) && *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(78706)) } if *(*int32)(unsafe.Pointer(bp)) != 0 { goto balance_cleanup } } else { _ = libc.Int32FromInt32(0) if bBulk != 0 { v18 = uint32(1) } else { v18 = *(*TPgno)(unsafe.Pointer(bp + 56)) } *(*int32)(unsafe.Pointer(bp)) = _allocateBtreePage(tls, pBt, bp+132, bp+56, v18, uint8(0)) if *(*int32)(unsafe.Pointer(bp)) != 0 { goto balance_cleanup } _zeroPage(tls, *(*uintptr)(unsafe.Pointer(bp + 132)), pageFlags) (*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[i] = *(*uintptr)(unsafe.Pointer(bp + 132)) nNew++ cntOld[i] = (*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell /* Set the pointer-map entry for the new sibling page. */ if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { _ptrmapPut(tls, pBt, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 132)))).Fpgno, uint8(PTRMAP_BTREE), (*TMemPage)(unsafe.Pointer(pParent)).Fpgno, bp) if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { goto balance_cleanup } } } goto _16 _16: ; i++ } /* ** Reassign page numbers so that the new pages are in ascending order. ** This helps to keep entries in the disk file in order so that a scan ** of the table is closer to a linear scan through the file. That in turn ** helps the operating system to deliver pages from the disk more rapidly. ** ** An O(N*N) sort algorithm is used, but since N is never more than NB+2 ** (5), that is not a performance concern. ** ** When NB==3, this one optimization makes the database about 25% faster ** for large insertions and deletions. */ i = 0 for { if !(i < nNew) { break } aPgno[i] = (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[i])).Fpgno _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) goto _19 _19: ; i++ } i = 0 for { if !(i < nNew-int32(1)) { break } iB = i j = i + int32(1) for { if !(j < nNew) { break } if (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[j])).Fpgno < (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[iB])).Fpgno { iB = j } goto _21 _21: ; j++ } /* If apNew[i] has a page number that is bigger than any of the ** subsequence apNew[i] entries, then swap apNew[i] with the subsequent ** entry that has the smallest page number (which we know to be ** entry apNew[iB]). */ if iB != i { pgnoA = (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[i])).Fpgno pgnoB = (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[iB])).Fpgno pgnoTemp = uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize + uint32(1) fgA = (*TDbPage)(unsafe.Pointer((*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[i])).FpDbPage)).Fflags fgB = (*TDbPage)(unsafe.Pointer((*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[iB])).FpDbPage)).Fflags _sqlite3PagerRekey(tls, (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[i])).FpDbPage, pgnoTemp, fgB) _sqlite3PagerRekey(tls, (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[iB])).FpDbPage, pgnoA, fgA) _sqlite3PagerRekey(tls, (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[i])).FpDbPage, pgnoB, fgB) (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[i])).Fpgno = pgnoB (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[iB])).Fpgno = pgnoA } goto _20 _20: ; i++ } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3Put4byte(tls, pRight, (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[nNew-int32(1)])).Fpgno) /* If the sibling pages are not leaves, ensure that the right-child pointer ** of the right-most new sibling page is set to the value that was ** originally in the same field of the right-most old sibling page. */ if pageFlags&int32(PTF_LEAF) == 0 && nOld != nNew { if nNew > nOld { v22 = bp + 16 } else { v22 = bp + 4 } pOld1 = *(*uintptr)(unsafe.Pointer(v22 + uintptr(nOld-int32(1))*4)) libc.Xmemcpy(tls, (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[nNew-int32(1)])).FaData+8, (*TMemPage)(unsafe.Pointer(pOld1)).FaData+8, uint32(4)) } /* Make any required updates to pointer map entries associated with ** cells stored on sibling pages following the balance operation. Pointer ** map entries associated with divider cells are set by the insertCell() ** routine. The associated pointer map entries are: ** ** a) if the cell contains a reference to an overflow chain, the ** entry associated with the first page in the overflow chain, and ** ** b) if the sibling pages are not leaves, the child page associated ** with the cell. ** ** If the sibling pages are not leaves, then the pointer map entry ** associated with the right-child of each sibling may also need to be ** updated. This happens below, after the sibling pages have been ** populated, not here. */ if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { v23 = (*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[0] pOld2 = v23 pNew1 = v23 cntOldNext = int32((*TMemPage)(unsafe.Pointer(pNew1)).FnCell) + int32((*TMemPage)(unsafe.Pointer(pNew1)).FnOverflow) iNew = 0 iOld = 0 i = 0 for { if !(i < (*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell) { break } pCell = *(*uintptr)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FapCell + uintptr(i)*4)) for i == cntOldNext { iOld++ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if iOld < nNew { v25 = (*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[iOld] } else { v25 = (*(*[3]uintptr)(unsafe.Pointer(bp + 4)))[iOld] } pOld2 = v25 cntOldNext += int32((*TMemPage)(unsafe.Pointer(pOld2)).FnCell) + int32((*TMemPage)(unsafe.Pointer(pOld2)).FnOverflow) + libc.BoolInt32(!(leafData != 0)) } if i == cntNew[iNew] { iNew++ v26 = iNew pNew1 = (*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[v26] if !(leafData != 0) { goto _24 } } /* Cell pCell is destined for new sibling page pNew. Originally, it ** was either part of sibling page iOld (possibly an overflow cell), ** or else the divider cell to the left of sibling page iOld. So, ** if sibling page iOld had the same page number as pNew, and if ** pCell really was a part of sibling page iOld (not a divider or ** overflow cell), we can skip updating the pointer map entries. */ if iOld >= nNew || (*TMemPage)(unsafe.Pointer(pNew1)).Fpgno != aPgno[iOld] || !(uint32(pCell) >= uint32((*TMemPage)(unsafe.Pointer(pOld2)).FaData) && uint32(pCell) < uint32((*TMemPage)(unsafe.Pointer(pOld2)).FaDataEnd)) { if !(leafCorrection != 0) { _ptrmapPut(tls, pBt, _sqlite3Get4byte(tls, pCell), uint8(PTRMAP_BTREE), (*TMemPage)(unsafe.Pointer(pNew1)).Fpgno, bp) } if int32(_cachedCellSize(tls, bp+68, i)) > int32((*TMemPage)(unsafe.Pointer(pNew1)).FminLocal) { _ptrmapPutOvflPtr(tls, pNew1, pOld2, pCell, bp) } if *(*int32)(unsafe.Pointer(bp)) != 0 { goto balance_cleanup } } goto _24 _24: ; i++ } } /* Insert new divider cells into pParent. */ i = 0 for { if !(i < nNew-int32(1)) { break } pNew2 = (*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[i] j = cntNew[i] _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pCell1 = *(*uintptr)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FapCell + uintptr(j)*4)) sz2 = int32(*(*Tu16)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FszCell + uintptr(j)*2))) + int32(leafCorrection) pTemp1 = aOvflSpace + uintptr(iOvflSpace) if !((*TMemPage)(unsafe.Pointer(pNew2)).Fleaf != 0) { libc.Xmemcpy(tls, (*TMemPage)(unsafe.Pointer(pNew2)).FaData+8, pCell1, uint32(4)) } else { if leafData != 0 { j-- (*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pNew2)).FxParseCell})))(tls, pNew2, *(*uintptr)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FapCell + uintptr(j)*4)), bp+136) pCell1 = pTemp1 sz2 = int32(4) + _sqlite3PutVarint(tls, pCell1+4, uint64((*(*TCellInfo)(unsafe.Pointer(bp + 136))).FnKey)) pTemp1 = uintptr(0) } else { pCell1 -= uintptr(4) /* Obscure case for non-leaf-data trees: If the cell at pCell was ** previously stored on a leaf node, and its reported size was 4 ** bytes, then it may actually be smaller than this ** (see btreeParseCellPtr(), 4 bytes is the minimum size of ** any cell). But it is important to pass the correct size to ** insertCell(), so reparse the cell now. ** ** This can only happen for b-trees used to evaluate "IN (SELECT ...)" ** and WITHOUT ROWID tables with exactly one column which is the ** primary key. */ if int32(*(*Tu16)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 68))).FszCell + uintptr(j)*2))) == int32(4) { _ = libc.Int32FromInt32(0) sz2 = int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pParent)).FxCellSize})))(tls, pParent, pCell1)) } } } iOvflSpace += sz2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) k = 0 for { if !(k < libc.Int32FromInt32(NB)*libc.Int32FromInt32(2) && *(*int32)(unsafe.Pointer(bp + 68 + 40 + uintptr(k)*4)) <= j) { break } goto _28 _28: ; k++ } pSrcEnd = *(*uintptr)(unsafe.Pointer(bp + 68 + 16 + uintptr(k)*4)) if uint32(pCell1) < uint32(pSrcEnd) && uint32(pCell1+uintptr(sz2)) > uint32(pSrcEnd) { *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(78906)) goto balance_cleanup } *(*int32)(unsafe.Pointer(bp)) = _insertCell(tls, pParent, nxDiv+i, pCell1, sz2, pTemp1, (*TMemPage)(unsafe.Pointer(pNew2)).Fpgno) if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { goto balance_cleanup } _ = libc.Int32FromInt32(0) goto _27 _27: ; i++ } /* Now update the actual sibling pages. The order in which they are updated ** is important, as this code needs to avoid disrupting any page from which ** cells may still to be read. In practice, this means: ** ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1]) ** then it is not safe to update page apNew[iPg] until after ** the left-hand sibling apNew[iPg-1] has been updated. ** ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1]) ** then it is not safe to update page apNew[iPg] until after ** the right-hand sibling apNew[iPg+1] has been updated. ** ** If neither of the above apply, the page is safe to update. ** ** The iPg value in the following loop starts at nNew-1 goes down ** to 0, then back up to nNew-1 again, thus making two passes over ** the pages. On the initial downward pass, only condition (1) above ** needs to be tested because (2) will always be true from the previous ** step. On the upward pass, both conditions are always true, so the ** upwards pass simply processes pages that were missed on the downward ** pass. */ i = int32(1) - nNew for { if !(i < nNew) { break } if i < 0 { v30 = -i } else { v30 = i } iPg = v30 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*(*[5]Tu8)(unsafe.Pointer(bp + 60)))[iPg] != 0 { goto _29 } /* Skip pages already processed */ if i >= 0 || cntOld[iPg-int32(1)] >= cntNew[iPg-int32(1)] { /* Verify condition (1): If cells are moving left, update iPg ** only after iPg-1 has already been updated. */ _ = libc.Int32FromInt32(0) /* Verify condition (2): If cells are moving right, update iPg ** only after iPg+1 has already been updated. */ _ = libc.Int32FromInt32(0) if iPg == 0 { v31 = libc.Int32FromInt32(0) iOld1 = v31 iNew1 = v31 nNewCell = cntNew[0] } else { if iPg < nOld { v32 = cntOld[iPg-int32(1)] + libc.BoolInt32(!(leafData != 0)) } else { v32 = (*(*TCellArray)(unsafe.Pointer(bp + 68))).FnCell } iOld1 = v32 iNew1 = cntNew[iPg-int32(1)] + libc.BoolInt32(!(leafData != 0)) nNewCell = cntNew[iPg] - iNew1 } *(*int32)(unsafe.Pointer(bp)) = _editPage(tls, (*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[iPg], iOld1, iNew1, nNewCell, bp+68) if *(*int32)(unsafe.Pointer(bp)) != 0 { goto balance_cleanup } (*(*[5]Tu8)(unsafe.Pointer(bp + 60)))[iPg]++ (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[iPg])).FnFree = usableSpace - (*(*[5]int32)(unsafe.Pointer(bp + 36)))[iPg] _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } goto _29 _29: ; i++ } /* All pages have been processed exactly once */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if isRoot != 0 && int32((*TMemPage)(unsafe.Pointer(pParent)).FnCell) == 0 && int32((*TMemPage)(unsafe.Pointer(pParent)).FhdrOffset) <= (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[0])).FnFree { /* The root page of the b-tree now contains no cells. The only sibling ** page is the right-child of the parent. Copy the contents of the ** child page into the parent, decreasing the overall height of the ** b-tree structure by one. This is described as the "balance-shallower" ** sub-algorithm in some documentation. ** ** If this is an auto-vacuum database, the call to copyNodeContent() ** sets all pointer-map entries corresponding to database image pages ** for which the pointer is stored within the content being copied. ** ** It is critical that the child page be defragmented before being ** copied into the parent, because if the parent is page 1 then it will ** by smaller than the child due to the database header, and so all the ** free space needs to be up front. */ _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp)) = _defragmentPage(tls, (*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[0], -int32(1)) _ = libc.Int32FromInt32(0) _copyNodeContent(tls, (*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[0], pParent, bp) _freePage(tls, (*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[0], bp) } else { if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 && !(leafCorrection != 0) { /* Fix the pointer map entries associated with the right-child of each ** sibling page. All other pointer map entries have already been taken ** care of. */ i = 0 for { if !(i < nNew) { break } key = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[i])).FaData+8) _ptrmapPut(tls, pBt, key, uint8(PTRMAP_BTREE), (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[i])).Fpgno, bp) goto _33 _33: ; i++ } } } _ = libc.Int32FromInt32(0) /* Free any old pages that were not reused as new pages. */ i = nNew for { if !(i < nOld) { break } _freePage(tls, (*(*[3]uintptr)(unsafe.Pointer(bp + 4)))[i], bp) goto _34 _34: ; i++ } /* ** Cleanup before returning. */ goto balance_cleanup balance_cleanup: ; _sqlite3DbFree(tls, uintptr(0), (*(*TCellArray)(unsafe.Pointer(bp + 68))).FapCell) i = 0 for { if !(i < nOld) { break } _releasePage(tls, (*(*[3]uintptr)(unsafe.Pointer(bp + 4)))[i]) goto _35 _35: ; i++ } i = 0 for { if !(i < nNew) { break } _releasePage(tls, (*(*[5]uintptr)(unsafe.Pointer(bp + 16)))[i]) goto _36 _36: ; i++ } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** This function is called when the root page of a b-tree structure is // ** overfull (has one or more overflow pages). // ** // ** A new child page is allocated and the contents of the current root // ** page, including overflow cells, are copied into the child. The root // ** page is then overwritten to make it an empty page with the right-child // ** pointer pointing to the new page. // ** // ** Before returning, all pointer-map entries corresponding to pages // ** that the new child-page now contains pointers to are updated. The // ** entry corresponding to the new right-child pointer of the root // ** page is also updated. // ** // ** If successful, *ppChild is set to contain a reference to the child // ** page and SQLITE_OK is returned. In this case the caller is required // ** to call releasePage() on *ppChild exactly once. If an error occurs, // ** an error code is returned and *ppChild is set to 0. // */ func _balance_deeper(tls *libc.TLS, pRoot uintptr, ppChild uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pBt uintptr var _ /* pChild at bp+4 */ uintptr var _ /* pgnoChild at bp+8 */ TPgno var _ /* rc at bp+0 */ int32 _ = pBt /* Return value from subprocedures */ *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) /* Pointer to a new child page */ *(*TPgno)(unsafe.Pointer(bp + 8)) = uint32(0) /* Page number of the new child page */ pBt = (*TMemPage)(unsafe.Pointer(pRoot)).FpBt /* The BTree */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Make pRoot, the root page of the b-tree, writable. Allocate a new ** page that will become the new right-child of pPage. Copy the contents ** of the node stored on pRoot into the new child page. */ *(*int32)(unsafe.Pointer(bp)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pRoot)).FpDbPage) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _allocateBtreePage(tls, pBt, bp+4, bp+8, (*TMemPage)(unsafe.Pointer(pRoot)).Fpgno, uint8(0)) _copyNodeContent(tls, pRoot, *(*uintptr)(unsafe.Pointer(bp + 4)), bp) if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { _ptrmapPut(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 8)), uint8(PTRMAP_BTREE), (*TMemPage)(unsafe.Pointer(pRoot)).Fpgno, bp) } } if *(*int32)(unsafe.Pointer(bp)) != 0 { *(*uintptr)(unsafe.Pointer(ppChild)) = uintptr(0) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) return *(*int32)(unsafe.Pointer(bp)) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Copy the overflow cells from pRoot to pChild */ libc.Xmemcpy(tls, *(*uintptr)(unsafe.Pointer(bp + 4))+28, pRoot+28, uint32((*TMemPage)(unsafe.Pointer(pRoot)).FnOverflow)*uint32(2)) libc.Xmemcpy(tls, *(*uintptr)(unsafe.Pointer(bp + 4))+36, pRoot+36, uint32((*TMemPage)(unsafe.Pointer(pRoot)).FnOverflow)*uint32(4)) (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FnOverflow = (*TMemPage)(unsafe.Pointer(pRoot)).FnOverflow /* Zero the contents of pRoot. Then install pChild as the right-child. */ _zeroPage(tls, pRoot, int32(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FaData))) & ^libc.Int32FromInt32(PTF_LEAF)) _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pRoot)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pRoot)).FhdrOffset)+int32(8)), *(*TPgno)(unsafe.Pointer(bp + 8))) *(*uintptr)(unsafe.Pointer(ppChild)) = *(*uintptr)(unsafe.Pointer(bp + 4)) return SQLITE_OK } // C documentation // // /* // ** Return SQLITE_CORRUPT if any cursor other than pCur is currently valid // ** on the same B-tree as pCur. // ** // ** This can occur if a database is corrupt with two or more SQL tables // ** pointing to the same b-tree. If an insert occurs on one SQL table // ** and causes a BEFORE TRIGGER to do a secondary insert on the other SQL // ** table linked to the same b-tree. If the secondary insert causes a // ** rebalance, that can change content out from under the cursor on the // ** first SQL table, violating invariants on the first insert. // */ func _anotherValidCursor(tls *libc.TLS, pCur uintptr) (r int32) { var pOther uintptr _ = pOther pOther = (*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FpCursor for { if !(pOther != 0) { break } if pOther != pCur && int32((*TBtCursor)(unsafe.Pointer(pOther)).FeState) == CURSOR_VALID && (*TBtCursor)(unsafe.Pointer(pOther)).FpPage == (*TBtCursor)(unsafe.Pointer(pCur)).FpPage { return _sqlite3CorruptError(tls, int32(79138)) } goto _1 _1: ; pOther = (*TBtCursor)(unsafe.Pointer(pOther)).FpNext } return SQLITE_OK } // C documentation // // /* // ** The page that pCur currently points to has just been modified in // ** some way. This function figures out if this modification means the // ** tree needs to be balanced, and if so calls the appropriate balancing // ** routine. Balancing routines are: // ** // ** balance_quick() // ** balance_deeper() // ** balance_nonroot() // */ func _balance(tls *libc.TLS, pCur uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iIdx, iPage, rc, v1, v2 int32 var pFree, pPage, pParent, pSpace uintptr var v3 bool var _ /* aBalanceQuickSpace at bp+0 */ [13]Tu8 _, _, _, _, _, _, _, _, _, _ = iIdx, iPage, pFree, pPage, pParent, pSpace, rc, v1, v2, v3 rc = SQLITE_OK pFree = uintptr(0) for cond := true; cond; cond = rc == SQLITE_OK { pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage if (*TMemPage)(unsafe.Pointer(pPage)).FnFree < 0 && _btreeComputeFreeSpace(tls, pPage) != 0 { break } if int32((*TMemPage)(unsafe.Pointer(pPage)).FnOverflow) == 0 && (*TMemPage)(unsafe.Pointer(pPage)).FnFree*int32(3) <= int32((*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FusableSize)*int32(2) { /* No rebalance required as long as: ** (1) There are no overflow cells ** (2) The amount of free space on the page is less than 2/3rds of ** the total usable space on the page. */ break } else { v1 = int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) iPage = v1 if v1 == 0 { if v3 = (*TMemPage)(unsafe.Pointer(pPage)).FnOverflow != 0; v3 { v2 = _anotherValidCursor(tls, pCur) rc = v2 } if v3 && v2 == SQLITE_OK { /* The root page of the b-tree is overfull. In this case call the ** balance_deeper() function to create a new child for the root-page ** and copy the current contents of the root-page to it. The ** next iteration of the do-loop will balance the child page. */ _ = libc.Int32FromInt32(0) rc = _balance_deeper(tls, pPage, pCur+120+1*4) if rc == SQLITE_OK { (*TBtCursor)(unsafe.Pointer(pCur)).FiPage = int8(1) (*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(0) *(*Tu16)(unsafe.Pointer(pCur + 72)) = uint16(0) *(*uintptr)(unsafe.Pointer(pCur + 120)) = pPage (*TBtCursor)(unsafe.Pointer(pCur)).FpPage = *(*uintptr)(unsafe.Pointer(pCur + 120 + 1*4)) _ = libc.Int32FromInt32(0) } } else { break } } else { if _sqlite3PagerPageRefcount(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage) > int32(1) { /* The page being written is not a root page, and there is currently ** more than one reference to it. This only happens if the page is one ** of its own ancestor pages. Corruption. */ rc = _sqlite3CorruptError(tls, int32(79198)) } else { pParent = *(*uintptr)(unsafe.Pointer(pCur + 120 + uintptr(iPage-int32(1))*4)) iIdx = int32(*(*Tu16)(unsafe.Pointer(pCur + 72 + uintptr(iPage-int32(1))*2))) rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pParent)).FpDbPage) if rc == SQLITE_OK && (*TMemPage)(unsafe.Pointer(pParent)).FnFree < 0 { rc = _btreeComputeFreeSpace(tls, pParent) } if rc == SQLITE_OK { if (*TMemPage)(unsafe.Pointer(pPage)).FintKeyLeaf != 0 && int32((*TMemPage)(unsafe.Pointer(pPage)).FnOverflow) == int32(1) && int32(*(*Tu16)(unsafe.Pointer(pPage + 28))) == int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) && (*TMemPage)(unsafe.Pointer(pParent)).Fpgno != uint32(1) && int32((*TMemPage)(unsafe.Pointer(pParent)).FnCell) == iIdx { /* Call balance_quick() to create a new sibling of pPage on which ** to store the overflow cell. balance_quick() inserts a new cell ** into pParent, which may cause pParent overflow. If this ** happens, the next iteration of the do-loop will balance pParent ** use either balance_nonroot() or balance_deeper(). Until this ** happens, the overflow cell is stored in the aBalanceQuickSpace[] ** buffer. ** ** The purpose of the following assert() is to check that only a ** single call to balance_quick() is made for each call to this ** function. If this were not verified, a subtle bug involving reuse ** of the aBalanceQuickSpace[] might sneak in. */ _ = libc.Int32FromInt32(0) rc = _balance_quick(tls, pParent, pPage, bp) } else { /* In this case, call balance_nonroot() to redistribute cells ** between pPage and up to 2 of its sibling pages. This involves ** modifying the contents of pParent, which may cause pParent to ** become overfull or underfull. The next iteration of the do-loop ** will balance the parent page to correct this. ** ** If the parent page becomes overfull, the overflow cell or cells ** are stored in the pSpace buffer allocated immediately below. ** A subsequent iteration of the do-loop will deal with this by ** calling balance_nonroot() (balance_deeper() may be called first, ** but it doesn't deal with overflow cells - just moves them to a ** different page). Once this subsequent call to balance_nonroot() ** has completed, it is safe to release the pSpace buffer used by ** the previous call, as the overflow cell data will have been ** copied either into the body of a database page or into the new ** pSpace buffer passed to the latter call to balance_nonroot(). */ pSpace = _sqlite3PageMalloc(tls, int32((*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FpageSize)) rc = _balance_nonroot(tls, pParent, iIdx, pSpace, libc.BoolInt32(iPage == int32(1)), int32((*TBtCursor)(unsafe.Pointer(pCur)).Fhints)&int32(BTREE_BULKLOAD)) if pFree != 0 { /* If pFree is not NULL, it points to the pSpace buffer used ** by a previous call to balance_nonroot(). Its contents are ** now stored either on real database pages or within the ** new pSpace buffer, so it may be safely freed here. */ _sqlite3PageFree(tls, pFree) } /* The pSpace buffer will be freed after the next call to ** balance_nonroot(), or just before this function returns, whichever ** comes first. */ pFree = pSpace } } (*TMemPage)(unsafe.Pointer(pPage)).FnOverflow = uint8(0) /* The next iteration of the do-loop balances the parent page. */ _releasePage(tls, pPage) (*TBtCursor)(unsafe.Pointer(pCur)).FiPage-- _ = libc.Int32FromInt32(0) (*TBtCursor)(unsafe.Pointer(pCur)).FpPage = *(*uintptr)(unsafe.Pointer(pCur + 120 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*4)) } } } } if pFree != 0 { _sqlite3PageFree(tls, pFree) } return rc } // C documentation // // /* Overwrite content from pX into pDest. Only do the write if the // ** content is different from what is already there. // */ func _btreeOverwriteContent(tls *libc.TLS, pPage uintptr, pDest uintptr, pX uintptr, iOffset int32, iAmt int32) (r int32) { var i, nData, rc, rc1, rc2 int32 _, _, _, _, _ = i, nData, rc, rc1, rc2 nData = (*TBtreePayload)(unsafe.Pointer(pX)).FnData - iOffset if nData <= 0 { i = 0 for { if !(i < iAmt && int32(*(*Tu8)(unsafe.Pointer(pDest + uintptr(i)))) == 0) { break } goto _1 _1: ; i++ } if i < iAmt { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage) if rc != 0 { return rc } libc.Xmemset(tls, pDest+uintptr(i), 0, uint32(iAmt-i)) } } else { if nData < iAmt { /* Mixed read data and zeros at the end. Make a recursive call ** to write the zeros then fall through to write the real data */ rc1 = _btreeOverwriteContent(tls, pPage, pDest+uintptr(nData), pX, iOffset+nData, iAmt-nData) if rc1 != 0 { return rc1 } iAmt = nData } if libc.Xmemcmp(tls, pDest, (*TBtreePayload)(unsafe.Pointer(pX)).FpData+uintptr(iOffset), uint32(iAmt)) != 0 { rc2 = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage) if rc2 != 0 { return rc2 } /* In a corrupt database, it is possible for the source and destination ** buffers to overlap. This is harmless since the database is already ** corrupt but it does cause valgrind and ASAN warnings. So use ** memmove(). */ libc.Xmemmove(tls, pDest, (*TBtreePayload)(unsafe.Pointer(pX)).FpData+uintptr(iOffset), uint32(iAmt)) } } return SQLITE_OK } // C documentation // // /* // ** Overwrite the cell that cursor pCur is pointing to with fresh content // ** contained in pX. In this variant, pCur is pointing to an overflow // ** cell. // */ func _btreeOverwriteOverflowCell(tls *libc.TLS, pCur uintptr, pX uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iOffset, nTotal, rc int32 var ovflPageSize Tu32 var ovflPgno TPgno var pBt uintptr var _ /* pPage at bp+0 */ uintptr _, _, _, _, _, _ = iOffset, nTotal, ovflPageSize, ovflPgno, pBt, rc /* Next byte of pX->pData to write */ nTotal = (*TBtreePayload)(unsafe.Pointer(pX)).FnData + (*TBtreePayload)(unsafe.Pointer(pX)).FnZero /* Return code */ *(*uintptr)(unsafe.Pointer(bp)) = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage /* Size to write on overflow page */ _ = libc.Int32FromInt32(0) /* pCur is an overflow cell */ /* Overwrite the local portion first */ rc = _btreeOverwriteContent(tls, *(*uintptr)(unsafe.Pointer(bp)), (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload, pX, 0, int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal)) if rc != 0 { return rc } /* Now overwrite the overflow pages */ iOffset = int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) ovflPgno = _sqlite3Get4byte(tls, (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload+uintptr(iOffset)) pBt = (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpBt ovflPageSize = (*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32(4) for cond := true; cond; cond = iOffset < nTotal { rc = _btreeGetPage(tls, pBt, ovflPgno, bp, 0) if rc != 0 { return rc } if _sqlite3PagerPageRefcount(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpDbPage) != int32(1) || (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FisInit != 0 { rc = _sqlite3CorruptError(tls, int32(79362)) } else { if uint32(iOffset)+ovflPageSize < uint32(nTotal) { ovflPgno = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData) } else { ovflPageSize = uint32(nTotal - iOffset) } rc = _btreeOverwriteContent(tls, *(*uintptr)(unsafe.Pointer(bp)), (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData+uintptr(4), pX, iOffset, int32(ovflPageSize)) } _sqlite3PagerUnref(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpDbPage) if rc != 0 { return rc } iOffset = int32(uint32(iOffset) + ovflPageSize) } return SQLITE_OK } // C documentation // // /* // ** Overwrite the cell that cursor pCur is pointing to with fresh content // ** contained in pX. // */ func _btreeOverwriteCell(tls *libc.TLS, pCur uintptr, pX uintptr) (r int32) { var nTotal int32 var pPage uintptr _, _ = nTotal, pPage nTotal = (*TBtreePayload)(unsafe.Pointer(pX)).FnData + (*TBtreePayload)(unsafe.Pointer(pX)).FnZero /* Total bytes of to write */ pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage /* Page being written */ if (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload+uintptr((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) > (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd || (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload < (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr((*TMemPage)(unsafe.Pointer(pPage)).FcellOffset) { return _sqlite3CorruptError(tls, int32(79390)) } if int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) == nTotal { /* The entire cell is local */ return _btreeOverwriteContent(tls, pPage, (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload, pX, 0, int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal)) } else { /* The cell contains overflow content */ return _btreeOverwriteOverflowCell(tls, pCur, pX) } return r } // C documentation // // /* // ** Insert a new record into the BTree. The content of the new record // ** is described by the pX object. The pCur cursor is used only to // ** define what table the record should be inserted into, and is left // ** pointing at a random location. // ** // ** For a table btree (used for rowid tables), only the pX.nKey value of // ** the key is used. The pX.pKey value must be NULL. The pX.nKey is the // ** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields // ** hold the content of the row. // ** // ** For an index btree (used for indexes and WITHOUT ROWID tables), the // ** key is an arbitrary byte sequence stored in pX.pKey,nKey. The // ** pX.pData,nData,nZero fields must be zero. // ** // ** If the seekResult parameter is non-zero, then a successful call to // ** sqlite3BtreeIndexMoveto() to seek cursor pCur to (pKey,nKey) has already // ** been performed. In other words, if seekResult!=0 then the cursor // ** is currently pointing to a cell that will be adjacent to the cell // ** to be inserted. If seekResult<0 then pCur points to a cell that is // ** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell // ** that is larger than (pKey,nKey). // ** // ** If seekResult==0, that means pCur is pointing at some unknown location. // ** In that case, this routine must seek the cursor to the correct insertion // ** point for (pKey,nKey) before doing the insertion. For index btrees, // ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked // ** key values and pX->aMem can be used instead of pX->pKey to avoid having // ** to decode the key. // */ func _sqlite3BtreeInsert(tls *libc.TLS, pCur uintptr, pX uintptr, flags int32, seekResult int32) (r int32) { bp := tls.Alloc(144) defer tls.Free(144) var idx int32 var newCell, oldCell, p, pPage, v3, p1, p4, p5 uintptr var ovfl TPgno var v2 Tu16 var _ /* info at bp+112 */ TCellInfo var _ /* info at bp+88 */ TCellInfo var _ /* loc at bp+4 */ int32 var _ /* r at bp+16 */ TUnpackedRecord var _ /* rc at bp+0 */ int32 var _ /* szNew at bp+8 */ int32 var _ /* x2 at bp+48 */ TBtreePayload _, _, _, _, _, _, _, _, _, _, _ = idx, newCell, oldCell, ovfl, p, pPage, v2, v3, p1, p4, p5 *(*int32)(unsafe.Pointer(bp + 4)) = seekResult /* -1: before desired location +1: after */ *(*int32)(unsafe.Pointer(bp + 8)) = 0 p = (*TBtCursor)(unsafe.Pointer(pCur)).FpBtree newCell = uintptr(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Save the positions of any other cursors open on this table. ** ** In some cases, the call to btreeMoveto() below is a no-op. For ** example, when inserting data into a table with auto-generated integer ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the ** integer key to use. It then calls this function to actually insert the ** data into the intkey B-Tree. In this case btreeMoveto() recognizes ** that the cursor is already where it needs to be and returns without ** doing any work. To avoid thwarting these optimizations, it is important ** not to clear the cursor here. */ if int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags)&int32(BTCF_Multiple) != 0 { *(*int32)(unsafe.Pointer(bp)) = _saveAllCursors(tls, (*TBtree)(unsafe.Pointer(p)).FpBt, (*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot, pCur) if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } if *(*int32)(unsafe.Pointer(bp + 4)) != 0 && int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) < 0 { /* This can only happen if the schema is corrupt such that there is more ** than one table or index with the same root page as used by the cursor. ** Which can only happen if the SQLITE_NoSchemaError flag was set when ** the schema was loaded. This cannot be asserted though, as a user might ** set the flag, load the schema, and then unset the flag. */ return _sqlite3CorruptError(tls, int32(79471)) } } /* Ensure that the cursor is not in the CURSOR_FAULT state and that it ** points to a valid cell. */ if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) >= int32(CURSOR_REQUIRESEEK) { *(*int32)(unsafe.Pointer(bp)) = _moveToRoot(tls, pCur) if *(*int32)(unsafe.Pointer(bp)) != 0 && *(*int32)(unsafe.Pointer(bp)) != int32(SQLITE_EMPTY) { return *(*int32)(unsafe.Pointer(bp)) } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Assert that the caller has been consistent. If this cursor was opened ** expecting an index b-tree, then the caller should be inserting blob ** keys with no associated data. If the cursor was opened expecting an ** intkey table, the caller should be inserting integer keys with a ** blob of associated data. */ _ = libc.Int32FromInt32(0) if (*TBtCursor)(unsafe.Pointer(pCur)).FpKeyInfo == uintptr(0) { _ = libc.Int32FromInt32(0) /* If this is an insert into a table b-tree, invalidate any incrblob ** cursors open on the row being replaced */ if (*TBtree)(unsafe.Pointer(p)).FhasIncrblobCur != 0 { _invalidateIncrblobCursors(tls, p, (*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot, (*TBtreePayload)(unsafe.Pointer(pX)).FnKey, 0) } /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing ** to a row with the same key as the new entry being inserted. */ /* On the other hand, BTREE_SAVEPOSITION==0 does not imply ** that the cursor is not pointing to a row to be overwritten. ** So do a complete check. */ if int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags)&int32(BTCF_ValidNKey) != 0 && (*TBtreePayload)(unsafe.Pointer(pX)).FnKey == (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey { /* The cursor is pointing to the entry that is to be ** overwritten */ _ = libc.Int32FromInt32(0) if int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize) != 0 && (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnPayload == uint32((*TBtreePayload)(unsafe.Pointer(pX)).FnData)+uint32((*TBtreePayload)(unsafe.Pointer(pX)).FnZero) { /* New entry is the same size as the old. Do an overwrite */ return _btreeOverwriteCell(tls, pCur, pX) } _ = libc.Int32FromInt32(0) } else { if *(*int32)(unsafe.Pointer(bp + 4)) == 0 { /* The cursor is *not* pointing to the cell to be overwritten, nor ** to an adjacent cell. Move the cursor so that it is pointing either ** to the cell to be overwritten or an adjacent cell. */ *(*int32)(unsafe.Pointer(bp)) = _sqlite3BtreeTableMoveto(tls, pCur, (*TBtreePayload)(unsafe.Pointer(pX)).FnKey, libc.BoolInt32(flags&int32(BTREE_APPEND) != 0), bp+4) if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } } } } else { /* This is an index or a WITHOUT ROWID table */ /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing ** to a row with the same key as the new entry being inserted. */ _ = libc.Int32FromInt32(0) /* If the cursor is not already pointing either to the cell to be ** overwritten, or if a new cell is being inserted, if the cursor is ** not pointing to an immediately adjacent cell, then move the cursor ** so that it does. */ if *(*int32)(unsafe.Pointer(bp + 4)) == 0 && flags&int32(BTREE_SAVEPOSITION) == 0 { if (*TBtreePayload)(unsafe.Pointer(pX)).FnMem != 0 { (*(*TUnpackedRecord)(unsafe.Pointer(bp + 16))).FpKeyInfo = (*TBtCursor)(unsafe.Pointer(pCur)).FpKeyInfo (*(*TUnpackedRecord)(unsafe.Pointer(bp + 16))).FaMem = (*TBtreePayload)(unsafe.Pointer(pX)).FaMem (*(*TUnpackedRecord)(unsafe.Pointer(bp + 16))).FnField = (*TBtreePayload)(unsafe.Pointer(pX)).FnMem (*(*TUnpackedRecord)(unsafe.Pointer(bp + 16))).Fdefault_rc = 0 (*(*TUnpackedRecord)(unsafe.Pointer(bp + 16))).FeqSeen = uint8(0) *(*int32)(unsafe.Pointer(bp)) = _sqlite3BtreeIndexMoveto(tls, pCur, bp+16, bp+4) } else { *(*int32)(unsafe.Pointer(bp)) = _btreeMoveto(tls, pCur, (*TBtreePayload)(unsafe.Pointer(pX)).FpKey, (*TBtreePayload)(unsafe.Pointer(pX)).FnKey, libc.BoolInt32(flags&int32(BTREE_APPEND) != 0), bp+4) } if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } } /* If the cursor is currently pointing to an entry to be overwritten ** and the new content is the same as as the old, then use the ** overwrite optimization. */ if *(*int32)(unsafe.Pointer(bp + 4)) == 0 { _getCellInfo(tls, pCur) if (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey == (*TBtreePayload)(unsafe.Pointer(pX)).FnKey { (*(*TBtreePayload)(unsafe.Pointer(bp + 48))).FpData = (*TBtreePayload)(unsafe.Pointer(pX)).FpKey (*(*TBtreePayload)(unsafe.Pointer(bp + 48))).FnData = int32((*TBtreePayload)(unsafe.Pointer(pX)).FnKey) (*(*TBtreePayload)(unsafe.Pointer(bp + 48))).FnZero = 0 return _btreeOverwriteCell(tls, pCur, bp+48) } } } _ = libc.Int32FromInt32(0) pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TMemPage)(unsafe.Pointer(pPage)).FnFree < 0 { if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) > int32(CURSOR_INVALID) { /* ^^^^^--- due to the moveToRoot() call above */ *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(79594)) } else { *(*int32)(unsafe.Pointer(bp)) = _btreeComputeFreeSpace(tls, pPage) } if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } } _ = libc.Int32FromInt32(0) newCell = (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FpTmpSpace _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if flags&int32(BTREE_PREFORMAT) != 0 { *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*int32)(unsafe.Pointer(bp + 8)) = (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FnPreformatSize if *(*int32)(unsafe.Pointer(bp + 8)) < int32(4) { *(*int32)(unsafe.Pointer(bp + 8)) = int32(4) *(*uint8)(unsafe.Pointer(newCell + 3)) = uint8(0) } if (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FautoVacuum != 0 && *(*int32)(unsafe.Pointer(bp + 8)) > int32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) { (*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxParseCell})))(tls, pPage, newCell, bp+88) if (*(*TCellInfo)(unsafe.Pointer(bp + 88))).FnPayload != uint32((*(*TCellInfo)(unsafe.Pointer(bp + 88))).FnLocal) { ovfl = _sqlite3Get4byte(tls, newCell+uintptr(*(*int32)(unsafe.Pointer(bp + 8))-int32(4))) _ptrmapPut(tls, (*TBtree)(unsafe.Pointer(p)).FpBt, ovfl, uint8(PTRMAP_OVERFLOW1), (*TMemPage)(unsafe.Pointer(pPage)).Fpgno, bp) if *(*int32)(unsafe.Pointer(bp)) != 0 { goto end_insert } } } } else { *(*int32)(unsafe.Pointer(bp)) = _fillInCell(tls, pPage, newCell, pX, bp+8) if *(*int32)(unsafe.Pointer(bp)) != 0 { goto end_insert } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) idx = int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0) if *(*int32)(unsafe.Pointer(bp + 4)) == 0 { _ = libc.Int32FromInt32(0) if idx >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) { return _sqlite3CorruptError(tls, int32(79636)) } *(*int32)(unsafe.Pointer(bp)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage) if *(*int32)(unsafe.Pointer(bp)) != 0 { goto end_insert } oldCell = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx)))))) if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) { libc.Xmemcpy(tls, newCell, oldCell, uint32(4)) } (*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxParseCell})))(tls, pPage, oldCell, bp+112) if uint32((*(*TCellInfo)(unsafe.Pointer(bp + 112))).FnLocal) != (*(*TCellInfo)(unsafe.Pointer(bp + 112))).FnPayload { *(*int32)(unsafe.Pointer(bp)) = _clearCellOverflow(tls, pPage, oldCell, bp+112) } else { *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK } p1 = pCur + 1 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(BTCF_ValidOvfl)) if int32((*(*TCellInfo)(unsafe.Pointer(bp + 112))).FnSize) == *(*int32)(unsafe.Pointer(bp + 8)) && uint32((*(*TCellInfo)(unsafe.Pointer(bp + 112))).FnLocal) == (*(*TCellInfo)(unsafe.Pointer(bp + 112))).FnPayload && (!((*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FautoVacuum != 0) || *(*int32)(unsafe.Pointer(bp + 8)) < int32((*TMemPage)(unsafe.Pointer(pPage)).FminLocal)) { /* Overwrite the old cell with the new if they are the same size. ** We could also try to do this if the old cell is smaller, then add ** the leftover space to the free list. But experiments show that ** doing that is no faster then skipping this optimization and just ** calling dropCell() and insertCell(). ** ** This optimization cannot be used on an autovacuum database if the ** new entry uses overflow pages, as the insertCell() call below is ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */ _ = libc.Int32FromInt32(0) /* clearCell never fails when nLocal==nPayload */ if oldCell < (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+uintptr(10) { return _sqlite3CorruptError(tls, int32(79663)) } if oldCell+uintptr(*(*int32)(unsafe.Pointer(bp + 8))) > (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd { return _sqlite3CorruptError(tls, int32(79666)) } libc.Xmemcpy(tls, oldCell, newCell, uint32(*(*int32)(unsafe.Pointer(bp + 8)))) return SQLITE_OK } _dropCell(tls, pPage, idx, int32((*(*TCellInfo)(unsafe.Pointer(bp + 112))).FnSize), bp) if *(*int32)(unsafe.Pointer(bp)) != 0 { goto end_insert } } else { if *(*int32)(unsafe.Pointer(bp + 4)) < 0 && int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) > 0 { _ = libc.Int32FromInt32(0) v3 = pCur + 70 *(*Tu16)(unsafe.Pointer(v3))++ v2 = *(*Tu16)(unsafe.Pointer(v3)) idx = int32(v2) p4 = pCur + 1 *(*Tu8)(unsafe.Pointer(p4)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p4))) & ^(libc.Int32FromInt32(BTCF_ValidNKey) | libc.Int32FromInt32(BTCF_ValidOvfl))) } else { _ = libc.Int32FromInt32(0) } } *(*int32)(unsafe.Pointer(bp)) = _insertCellFast(tls, pPage, idx, newCell, *(*int32)(unsafe.Pointer(bp + 8))) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If no error has occurred and pPage has an overflow cell, call balance() ** to redistribute the cells within the tree. Since balance() may move ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey ** variables. ** ** Previous versions of SQLite called moveToRoot() to move the cursor ** back to the root page as balance() used to invalidate the contents ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that, ** set the cursor state to "invalid". This makes common insert operations ** slightly faster. ** ** There is a subtle but important optimization here too. When inserting ** multiple records into an intkey b-tree using a single cursor (as can ** happen while processing an "INSERT INTO ... SELECT" statement), it ** is advantageous to leave the cursor pointing to the last entry in ** the b-tree if possible. If the cursor is left pointing to the last ** entry in the table, and the next row inserted has an integer key ** larger than the largest existing key, it is possible to insert the ** row without seeking the cursor. This can be a big performance boost. */ if (*TMemPage)(unsafe.Pointer(pPage)).FnOverflow != 0 { _ = libc.Int32FromInt32(0) p5 = pCur + 1 *(*Tu8)(unsafe.Pointer(p5)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p5))) & ^(libc.Int32FromInt32(BTCF_ValidNKey) | libc.Int32FromInt32(BTCF_ValidOvfl))) *(*int32)(unsafe.Pointer(bp)) = _balance(tls, pCur) /* Must make sure nOverflow is reset to zero even if the balance() ** fails. Internal data structure corruption will result otherwise. ** Also, set the cursor state to invalid. This stops saveCursorPosition() ** from trying to save the current position of the cursor. */ (*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FnOverflow = uint8(0) (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_INVALID) if flags&int32(BTREE_SAVEPOSITION) != 0 && *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { _btreeReleaseAllCursorPages(tls, pCur) if (*TBtCursor)(unsafe.Pointer(pCur)).FpKeyInfo != 0 { _ = libc.Int32FromInt32(0) (*TBtCursor)(unsafe.Pointer(pCur)).FpKey = _sqlite3Malloc(tls, uint64((*TBtreePayload)(unsafe.Pointer(pX)).FnKey)) if (*TBtCursor)(unsafe.Pointer(pCur)).FpKey == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_NOMEM) } else { libc.Xmemcpy(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpKey, (*TBtreePayload)(unsafe.Pointer(pX)).FpKey, uint32((*TBtreePayload)(unsafe.Pointer(pX)).FnKey)) } } (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_REQUIRESEEK) (*TBtCursor)(unsafe.Pointer(pCur)).FnKey = (*TBtreePayload)(unsafe.Pointer(pX)).FnKey } } _ = libc.Int32FromInt32(0) goto end_insert end_insert: ; return *(*int32)(unsafe.Pointer(bp)) return r } // C documentation // // /* // ** This function is used as part of copying the current row from cursor // ** pSrc into cursor pDest. If the cursors are open on intkey tables, then // ** parameter iKey is used as the rowid value when the record is copied // ** into pDest. Otherwise, the record is copied verbatim. // ** // ** This function does not actually write the new value to cursor pDest. // ** Instead, it creates and populates any required overflow pages and // ** writes the data for the new cell into the BtShared.pTmpSpace buffer // ** for the destination database. The size of the cell, in bytes, is left // ** in BtShared.nPreformatSize. The caller completes the insertion by // ** calling sqlite3BtreeInsert() with the BTREE_PREFORMAT flag specified. // ** // ** SQLITE_OK is returned if successful, or an SQLite error code otherwise. // */ func _sqlite3BtreeTransferRow(tls *libc.TLS, pDest uintptr, pSrc uintptr, iKey Ti64) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var aIn, aOut, pBt, pPageOut, pPgnoOut, pSrcPager, v1 uintptr var nCopy int32 var nIn, nOut, nRem Tu32 var ovflIn TPgno var v2, v3 uint32 var _ /* pNew at bp+12 */ uintptr var _ /* pPageIn at bp+4 */ uintptr var _ /* pgnoNew at bp+8 */ TPgno var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aIn, aOut, nCopy, nIn, nOut, nRem, ovflIn, pBt, pPageOut, pPgnoOut, pSrcPager, v1, v2, v3 pBt = (*TBtCursor)(unsafe.Pointer(pDest)).FpBt aOut = (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace /* Bytes of data still to copy */ _getCellInfo(tls, pSrc) if (*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnPayload < uint32(0x80) { v1 = aOut aOut++ *(*Tu8)(unsafe.Pointer(v1)) = uint8((*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnPayload) } else { aOut += uintptr(_sqlite3PutVarint(tls, aOut, uint64((*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnPayload))) } if (*TBtCursor)(unsafe.Pointer(pDest)).FpKeyInfo == uintptr(0) { aOut += uintptr(_sqlite3PutVarint(tls, aOut, uint64(iKey))) } nIn = uint32((*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnLocal) aIn = (*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FpPayload if aIn+uintptr(nIn) > (*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pSrc)).FpPage)).FaDataEnd { return _sqlite3CorruptError(tls, int32(79768)) } nRem = (*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnPayload if nIn == nRem && nIn < uint32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pDest)).FpPage)).FmaxLocal) { libc.Xmemcpy(tls, aOut, aIn, nIn) (*TBtShared)(unsafe.Pointer(pBt)).FnPreformatSize = int32(nIn + uint32(int32(aOut)-int32((*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace))) return SQLITE_OK } else { *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK pSrcPager = (*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pSrc)).FpBt)).FpPager pPgnoOut = uintptr(0) ovflIn = uint32(0) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) pPageOut = uintptr(0) /* Size of output buffer aOut[] */ nOut = uint32(_btreePayloadToLocal(tls, (*TBtCursor)(unsafe.Pointer(pDest)).FpPage, int64((*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnPayload))) (*TBtShared)(unsafe.Pointer(pBt)).FnPreformatSize = int32(nOut + uint32(int32(aOut)-int32((*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace))) if nOut < (*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnPayload { pPgnoOut = aOut + uintptr(nOut) *(*int32)(unsafe.Pointer(pBt + 88)) += int32(4) } if nRem > nIn { if aIn+uintptr(nIn)+uintptr(4) > (*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pSrc)).FpPage)).FaDataEnd { return _sqlite3CorruptError(tls, int32(79793)) } ovflIn = _sqlite3Get4byte(tls, (*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FpPayload+uintptr(nIn)) } for cond := true; cond; cond = nRem > uint32(0) && *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { nRem -= nOut for cond := true; cond; cond = *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && nOut > uint32(0) { _ = libc.Int32FromInt32(0) if nIn > uint32(0) { if nOut < nIn { v2 = nOut } else { v2 = nIn } nCopy = int32(v2) libc.Xmemcpy(tls, aOut, aIn, uint32(nCopy)) nOut -= uint32(nCopy) nIn -= uint32(nCopy) aOut += uintptr(nCopy) aIn += uintptr(nCopy) } if nOut > uint32(0) { _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) *(*int32)(unsafe.Pointer(bp)) = _sqlite3PagerGet(tls, pSrcPager, ovflIn, bp+4, int32(PAGER_GET_READONLY)) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { aIn = _sqlite3PagerGetData(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) ovflIn = _sqlite3Get4byte(tls, aIn) aIn += uintptr(4) nIn = (*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pSrc)).FpBt)).FusableSize - uint32(4) } } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && nRem > uint32(0) && pPgnoOut != 0 { *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) *(*int32)(unsafe.Pointer(bp)) = _allocateBtreePage(tls, pBt, bp+12, bp+8, uint32(0), uint8(0)) _sqlite3Put4byte(tls, pPgnoOut, *(*TPgno)(unsafe.Pointer(bp + 8))) if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 && pPageOut != 0 { _ptrmapPut(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 8)), uint8(PTRMAP_OVERFLOW2), (*TMemPage)(unsafe.Pointer(pPageOut)).Fpgno, bp) } _releasePage(tls, pPageOut) pPageOut = *(*uintptr)(unsafe.Pointer(bp + 12)) if pPageOut != 0 { pPgnoOut = (*TMemPage)(unsafe.Pointer(pPageOut)).FaData _sqlite3Put4byte(tls, pPgnoOut, uint32(0)) aOut = pPgnoOut + 4 if (*TBtShared)(unsafe.Pointer(pBt)).FusableSize-uint32(4) < nRem { v3 = (*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32(4) } else { v3 = nRem } nOut = v3 } } } _releasePage(tls, pPageOut) _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) return *(*int32)(unsafe.Pointer(bp)) } return r } // C documentation // // /* // ** Delete the entry that the cursor is pointing to. // ** // ** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then // ** the cursor is left pointing at an arbitrary location after the delete. // ** But if that bit is set, then the cursor is left in a state such that // ** the next call to BtreeNext() or BtreePrev() moves it to the same row // ** as it would have been on if the call to BtreeDelete() had been omitted. // ** // ** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes // ** associated with a single table entry and its indexes. Only one of those // ** deletes is considered the "primary" delete. The primary delete occurs // ** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete // ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag. // ** The BTREE_AUXDELETE bit is a hint that is not used by this implementation, // ** but which might be used by alternative storage engines. // */ func _sqlite3BtreeDelete(tls *libc.TLS, pCur uintptr, flags Tu8) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var bPreserve Tu8 var iCellDepth, iCellIdx, nCell int32 var n TPgno var p, pBt, pCell, pLeaf, pPage, pTmp, v2 uintptr var v1 Ti8 var _ /* info at bp+8 */ TCellInfo var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _ = bPreserve, iCellDepth, iCellIdx, n, nCell, p, pBt, pCell, pLeaf, pPage, pTmp, v1, v2 p = (*TBtCursor)(unsafe.Pointer(pCur)).FpBtree pBt = (*TBtree)(unsafe.Pointer(p)).FpBt /* Keep cursor valid. 2 for CURSOR_SKIPNEXT */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID { if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) >= int32(CURSOR_REQUIRESEEK) { *(*int32)(unsafe.Pointer(bp)) = _btreeRestoreCursorPosition(tls, pCur) _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp)) != 0 || int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID { return *(*int32)(unsafe.Pointer(bp)) } } else { return _sqlite3CorruptError(tls, int32(79889)) } } _ = libc.Int32FromInt32(0) iCellDepth = int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) iCellIdx = int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage if int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) <= iCellIdx { return _sqlite3CorruptError(tls, int32(79898)) } pCell = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*iCellIdx)))))) if (*TMemPage)(unsafe.Pointer(pPage)).FnFree < 0 && _btreeComputeFreeSpace(tls, pPage) != 0 { return _sqlite3CorruptError(tls, int32(79902)) } if pCell < (*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx+uintptr((*TMemPage)(unsafe.Pointer(pPage)).FnCell) { return _sqlite3CorruptError(tls, int32(79905)) } /* If the BTREE_SAVEPOSITION bit is on, then the cursor position must ** be preserved following this delete operation. If the current delete ** will cause a b-tree rebalance, then this is done by saving the cursor ** key and leaving the cursor in CURSOR_REQUIRESEEK state before ** returning. ** ** If the current delete will not cause a rebalance, then the cursor ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately ** before or after the deleted entry. ** ** The bPreserve value records which path is required: ** ** bPreserve==0 Not necessary to save the cursor position ** bPreserve==1 Use CURSOR_REQUIRESEEK to save the cursor position ** bPreserve==2 Cursor won't move. Set CURSOR_SKIPNEXT. */ bPreserve = libc.BoolUint8(int32(flags)&int32(BTREE_SAVEPOSITION) != 0) if bPreserve != 0 { if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) || (*TMemPage)(unsafe.Pointer(pPage)).FnFree+int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxCellSize})))(tls, pPage, pCell))+int32(2) > int32((*TBtShared)(unsafe.Pointer(pBt)).FusableSize*libc.Uint32FromInt32(2)/libc.Uint32FromInt32(3)) || int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) == int32(1) { /* A b-tree rebalance will be required after deleting this entry. ** Save the cursor key. */ *(*int32)(unsafe.Pointer(bp)) = _saveCursorKey(tls, pCur) if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } } else { bPreserve = uint8(2) } } /* If the page containing the entry to delete is not a leaf page, move ** the cursor to the largest entry in the tree that is smaller than ** the entry being deleted. This cell will replace the cell being deleted ** from the internal node. The 'previous' entry is used for this instead ** of the 'next' entry, as the previous entry is always a part of the ** sub-tree headed by the child page of the cell being deleted. This makes ** balancing the tree following the delete operation easier. */ if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) { *(*int32)(unsafe.Pointer(bp)) = _sqlite3BtreePrevious(tls, pCur, 0) _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } } /* Save the positions of any other cursors open on this table before ** making any modifications. */ if int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags)&int32(BTCF_Multiple) != 0 { *(*int32)(unsafe.Pointer(bp)) = _saveAllCursors(tls, pBt, (*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot, pCur) if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } } /* If this is a delete operation to remove a row from a table b-tree, ** invalidate any incrblob cursors open on the row being deleted. */ if (*TBtCursor)(unsafe.Pointer(pCur)).FpKeyInfo == uintptr(0) && (*TBtree)(unsafe.Pointer(p)).FhasIncrblobCur != 0 { _invalidateIncrblobCursors(tls, p, (*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot, (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey, 0) } /* Make the page containing the entry to be deleted writable. Then free any ** overflow pages associated with the entry and finally remove the cell ** itself from within the page. */ *(*int32)(unsafe.Pointer(bp)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage) if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } (*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxParseCell})))(tls, pPage, pCell, bp+8) if uint32((*(*TCellInfo)(unsafe.Pointer(bp + 8))).FnLocal) != (*(*TCellInfo)(unsafe.Pointer(bp + 8))).FnPayload { *(*int32)(unsafe.Pointer(bp)) = _clearCellOverflow(tls, pPage, pCell, bp+8) } else { *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK } _dropCell(tls, pPage, iCellIdx, int32((*(*TCellInfo)(unsafe.Pointer(bp + 8))).FnSize), bp) if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } /* If the cell deleted was not located on a leaf page, then the cursor ** is currently pointing to the largest entry in the sub-tree headed ** by the child-page of the cell that was just deleted from an internal ** node. The cell from the leaf node needs to be moved to the internal ** node to replace the deleted cell. */ if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) { pLeaf = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage if (*TMemPage)(unsafe.Pointer(pLeaf)).FnFree < 0 { *(*int32)(unsafe.Pointer(bp)) = _btreeComputeFreeSpace(tls, pLeaf) if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } } if iCellDepth < int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)-int32(1) { n = (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pCur + 120 + uintptr(iCellDepth+int32(1))*4)))).Fpgno } else { n = (*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).Fpgno } pCell = (*TMemPage)(unsafe.Pointer(pLeaf)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pLeaf)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pLeaf)).FaCellIdx + uintptr(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pLeaf)).FnCell)-int32(1)))))))) if pCell < (*TMemPage)(unsafe.Pointer(pLeaf)).FaData+4 { return _sqlite3CorruptError(tls, int32(79996)) } nCell = int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pLeaf)).FxCellSize})))(tls, pLeaf, pCell)) _ = libc.Int32FromInt32(0) pTmp = (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pLeaf)).FpDbPage) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _insertCell(tls, pPage, iCellIdx, pCell-uintptr(4), nCell+int32(4), pTmp, n) } _dropCell(tls, pLeaf, int32((*TMemPage)(unsafe.Pointer(pLeaf)).FnCell)-int32(1), nCell, bp) if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } } /* Balance the tree. If the entry deleted was located on a leaf page, ** then the cursor still points to that page. In this case the first ** call to balance() repairs the tree, and the if(...) condition is ** never true. ** ** Otherwise, if the entry deleted was on an internal node page, then ** pCur is pointing to the leaf page from which a cell was removed to ** replace the cell deleted from the internal node. This is slightly ** tricky as the leaf node may be underfull, and the internal node may ** be either under or overfull. In this case run the balancing algorithm ** on the leaf node first. If the balance proceeds far enough up the ** tree that we can be sure that any problem in the internal node has ** been corrected, so be it. Otherwise, after balancing the leaf node, ** walk the cursor up the tree to the internal node and balance it as ** well. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FnFree*int32(3) <= int32((*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FusableSize)*int32(2) { /* Optimization: If the free space is less than 2/3rds of the page, ** then balance() will always be a no-op. No need to invoke it. */ *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK } else { *(*int32)(unsafe.Pointer(bp)) = _balance(tls, pCur) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) > iCellDepth { _releasePageNotNull(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpPage) (*TBtCursor)(unsafe.Pointer(pCur)).FiPage-- for int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) > iCellDepth { v2 = pCur + 68 v1 = *(*Ti8)(unsafe.Pointer(v2)) *(*Ti8)(unsafe.Pointer(v2))-- _releasePage(tls, *(*uintptr)(unsafe.Pointer(pCur + 120 + uintptr(v1)*4))) } (*TBtCursor)(unsafe.Pointer(pCur)).FpPage = *(*uintptr)(unsafe.Pointer(pCur + 120 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*4)) *(*int32)(unsafe.Pointer(bp)) = _balance(tls, pCur) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { if int32(bPreserve) > int32(1) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_SKIPNEXT) if iCellIdx >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) { (*TBtCursor)(unsafe.Pointer(pCur)).FskipNext = -int32(1) (*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) - int32(1)) } else { (*TBtCursor)(unsafe.Pointer(pCur)).FskipNext = int32(1) } } else { *(*int32)(unsafe.Pointer(bp)) = _moveToRoot(tls, pCur) if bPreserve != 0 { _btreeReleaseAllCursorPages(tls, pCur) (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_REQUIRESEEK) } if *(*int32)(unsafe.Pointer(bp)) == int32(SQLITE_EMPTY) { *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK } } } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Create a new BTree table. Write into *piTable the page // ** number for the root page of the new table. // ** // ** The type of type is determined by the flags parameter. Only the // ** following values of flags are currently in use. Other values for // ** flags might not work: // ** // ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys // ** BTREE_ZERODATA Used for SQL indices // */ func _btreeCreateTable(tls *libc.TLS, p uintptr, piTable uintptr, createTabFlags int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var pBt uintptr var ptfFlags int32 var _ /* eType at bp+20 */ Tu8 var _ /* iPtrPage at bp+24 */ TPgno var _ /* pPageMove at bp+16 */ uintptr var _ /* pRoot at bp+0 */ uintptr var _ /* pgnoMove at bp+12 */ TPgno var _ /* pgnoRoot at bp+4 */ TPgno var _ /* rc at bp+8 */ int32 _, _ = pBt, ptfFlags pBt = (*TBtree)(unsafe.Pointer(p)).FpBt /* Page-type flags for the root page of new table */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { /* The page to move to. */ /* Creating a new table may probably require moving an existing database ** to make room for the new tables root page. In case this page turns ** out to be an overflow page, delete all overflow page-map caches ** held by open cursors. */ _invalidateAllOverflowCache(tls, pBt) /* Read the value of meta[3] from the database to determine where the ** root page of the new table should go. meta[3] is the largest root-page ** created so far, so the new root-page is (meta[3]+1). */ _sqlite3BtreeGetMeta(tls, p, int32(BTREE_LARGEST_ROOT_PAGE), bp+4) if *(*TPgno)(unsafe.Pointer(bp + 4)) > _btreePagecount(tls, pBt) { return _sqlite3CorruptError(tls, int32(80112)) } *(*TPgno)(unsafe.Pointer(bp + 4))++ /* The new root-page may not be allocated on a pointer-map page, or the ** PENDING_BYTE page. */ for *(*TPgno)(unsafe.Pointer(bp + 4)) == _ptrmapPageno(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 4))) || *(*TPgno)(unsafe.Pointer(bp + 4)) == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) { *(*TPgno)(unsafe.Pointer(bp + 4))++ } _ = libc.Int32FromInt32(0) /* Allocate a page. The page that currently resides at pgnoRoot will ** be moved to the allocated page (unless the allocated page happens ** to reside at pgnoRoot). */ *(*int32)(unsafe.Pointer(bp + 8)) = _allocateBtreePage(tls, pBt, bp+16, bp+12, *(*TPgno)(unsafe.Pointer(bp + 4)), uint8(BTALLOC_EXACT)) if *(*int32)(unsafe.Pointer(bp + 8)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 8)) } if *(*TPgno)(unsafe.Pointer(bp + 12)) != *(*TPgno)(unsafe.Pointer(bp + 4)) { /* pgnoRoot is the page that will be used for the root-page of ** the new table (assuming an error did not occur). But we were ** allocated pgnoMove. If required (i.e. if it was not allocated ** by extending the file), the current page at position pgnoMove ** is already journaled. */ *(*Tu8)(unsafe.Pointer(bp + 20)) = uint8(0) *(*TPgno)(unsafe.Pointer(bp + 24)) = uint32(0) /* Save the positions of any open cursors. This is required in ** case they are holding a reference to an xFetch reference ** corresponding to page pgnoRoot. */ *(*int32)(unsafe.Pointer(bp + 8)) = _saveAllCursors(tls, pBt, uint32(0), uintptr(0)) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) if *(*int32)(unsafe.Pointer(bp + 8)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 8)) } /* Move the page currently at pgnoRoot to pgnoMove. */ *(*int32)(unsafe.Pointer(bp + 8)) = _btreeGetPage(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 4)), bp, 0) if *(*int32)(unsafe.Pointer(bp + 8)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 8)) } *(*int32)(unsafe.Pointer(bp + 8)) = _ptrmapGet(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 4)), bp+20, bp+24) if int32(*(*Tu8)(unsafe.Pointer(bp + 20))) == int32(PTRMAP_ROOTPAGE) || int32(*(*Tu8)(unsafe.Pointer(bp + 20))) == int32(PTRMAP_FREEPAGE) { *(*int32)(unsafe.Pointer(bp + 8)) = _sqlite3CorruptError(tls, int32(80160)) } if *(*int32)(unsafe.Pointer(bp + 8)) != SQLITE_OK { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) return *(*int32)(unsafe.Pointer(bp + 8)) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp + 8)) = _relocatePage(tls, pBt, *(*uintptr)(unsafe.Pointer(bp)), *(*Tu8)(unsafe.Pointer(bp + 20)), *(*TPgno)(unsafe.Pointer(bp + 24)), *(*TPgno)(unsafe.Pointer(bp + 12)), 0) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) /* Obtain the page at pgnoRoot */ if *(*int32)(unsafe.Pointer(bp + 8)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 8)) } *(*int32)(unsafe.Pointer(bp + 8)) = _btreeGetPage(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 4)), bp, 0) if *(*int32)(unsafe.Pointer(bp + 8)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 8)) } *(*int32)(unsafe.Pointer(bp + 8)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpDbPage) if *(*int32)(unsafe.Pointer(bp + 8)) != SQLITE_OK { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) return *(*int32)(unsafe.Pointer(bp + 8)) } } else { *(*uintptr)(unsafe.Pointer(bp)) = *(*uintptr)(unsafe.Pointer(bp + 16)) } /* Update the pointer-map and meta-data with the new root-page number. */ _ptrmapPut(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 4)), uint8(PTRMAP_ROOTPAGE), uint32(0), bp+8) if *(*int32)(unsafe.Pointer(bp + 8)) != 0 { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) return *(*int32)(unsafe.Pointer(bp + 8)) } /* When the new root page was allocated, page 1 was made writable in ** order either to increase the database filesize, or to decrement the ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail. */ _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp + 8)) = _sqlite3BtreeUpdateMeta(tls, p, int32(4), *(*TPgno)(unsafe.Pointer(bp + 4))) if *(*int32)(unsafe.Pointer(bp + 8)) != 0 { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) return *(*int32)(unsafe.Pointer(bp + 8)) } } else { *(*int32)(unsafe.Pointer(bp + 8)) = _allocateBtreePage(tls, pBt, bp, bp+4, uint32(1), uint8(0)) if *(*int32)(unsafe.Pointer(bp + 8)) != 0 { return *(*int32)(unsafe.Pointer(bp + 8)) } } _ = libc.Int32FromInt32(0) if createTabFlags&int32(BTREE_INTKEY) != 0 { ptfFlags = libc.Int32FromInt32(PTF_INTKEY) | libc.Int32FromInt32(PTF_LEAFDATA) | libc.Int32FromInt32(PTF_LEAF) } else { ptfFlags = libc.Int32FromInt32(PTF_ZERODATA) | libc.Int32FromInt32(PTF_LEAF) } _zeroPage(tls, *(*uintptr)(unsafe.Pointer(bp)), ptfFlags) _sqlite3PagerUnref(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpDbPage) _ = libc.Int32FromInt32(0) *(*TPgno)(unsafe.Pointer(piTable)) = *(*TPgno)(unsafe.Pointer(bp + 4)) return SQLITE_OK } func _sqlite3BtreeCreateTable(tls *libc.TLS, p uintptr, piTable uintptr, flags int32) (r int32) { var rc int32 _ = rc _sqlite3BtreeEnter(tls, p) rc = _btreeCreateTable(tls, p, piTable, flags) _sqlite3BtreeLeave(tls, p) return rc } // C documentation // // /* // ** Erase the given database page and all its children. Return // ** the page to the freelist. // */ func _clearDatabasePage(tls *libc.TLS, pBt uintptr, pgno TPgno, freePageFlag int32, pnChange uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var hdr, i, v2 int32 var pCell uintptr var _ /* info at bp+8 */ TCellInfo var _ /* pPage at bp+0 */ uintptr var _ /* rc at bp+4 */ int32 _, _, _, _ = hdr, i, pCell, v2 _ = libc.Int32FromInt32(0) if pgno > _btreePagecount(tls, pBt) { return _sqlite3CorruptError(tls, int32(80250)) } *(*int32)(unsafe.Pointer(bp + 4)) = _getAndInitPage(tls, pBt, pgno, bp, 0) if *(*int32)(unsafe.Pointer(bp + 4)) != 0 { return *(*int32)(unsafe.Pointer(bp + 4)) } if int32((*TBtShared)(unsafe.Pointer(pBt)).FopenFlags)&int32(BTREE_SINGLE) == 0 && _sqlite3PagerPageRefcount(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpDbPage) != int32(1)+libc.BoolInt32(pgno == uint32(1)) { *(*int32)(unsafe.Pointer(bp + 4)) = _sqlite3CorruptError(tls, int32(80257)) goto cleardatabasepage_out } hdr = int32((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FhdrOffset) i = 0 for { if !(i < int32((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnCell)) { break } pCell = (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaCellIdx + uintptr(int32(2)*i)))))) if !((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fleaf != 0) { *(*int32)(unsafe.Pointer(bp + 4)) = _clearDatabasePage(tls, pBt, _sqlite3Get4byte(tls, pCell), int32(1), pnChange) if *(*int32)(unsafe.Pointer(bp + 4)) != 0 { goto cleardatabasepage_out } } (*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FxParseCell})))(tls, *(*uintptr)(unsafe.Pointer(bp)), pCell, bp+8) if uint32((*(*TCellInfo)(unsafe.Pointer(bp + 8))).FnLocal) != (*(*TCellInfo)(unsafe.Pointer(bp + 8))).FnPayload { *(*int32)(unsafe.Pointer(bp + 4)) = _clearCellOverflow(tls, *(*uintptr)(unsafe.Pointer(bp)), pCell, bp+8) } else { *(*int32)(unsafe.Pointer(bp + 4)) = SQLITE_OK } if *(*int32)(unsafe.Pointer(bp + 4)) != 0 { goto cleardatabasepage_out } goto _1 _1: ; i++ } if !((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fleaf != 0) { *(*int32)(unsafe.Pointer(bp + 4)) = _clearDatabasePage(tls, pBt, _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData+uintptr(hdr+int32(8))), int32(1), pnChange) if *(*int32)(unsafe.Pointer(bp + 4)) != 0 { goto cleardatabasepage_out } if (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FintKey != 0 { pnChange = uintptr(0) } } if pnChange != 0 { *(*Ti64)(unsafe.Pointer(pnChange)) += int64((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnCell) } if freePageFlag != 0 { _freePage(tls, *(*uintptr)(unsafe.Pointer(bp)), bp+4) } else { v2 = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpDbPage) *(*int32)(unsafe.Pointer(bp + 4)) = v2 if v2 == 0 { _zeroPage(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData + uintptr(hdr))))|int32(PTF_LEAF)) } } goto cleardatabasepage_out cleardatabasepage_out: ; _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) return *(*int32)(unsafe.Pointer(bp + 4)) } // C documentation // // /* // ** Delete all information from a single table in the database. iTable is // ** the page number of the root of the table. After this routine returns, // ** the root page is empty, but still exists. // ** // ** This routine will fail with SQLITE_LOCKED if there are any open // ** read cursors on the table. Open write cursors are moved to the // ** root of the table. // ** // ** If pnChange is not NULL, then the integer value pointed to by pnChange // ** is incremented by the number of entries in the table. // */ func _sqlite3BtreeClearTable(tls *libc.TLS, p uintptr, iTable int32, pnChange uintptr) (r int32) { var pBt uintptr var rc int32 _, _ = pBt, rc pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _sqlite3BtreeEnter(tls, p) _ = libc.Int32FromInt32(0) rc = _saveAllCursors(tls, pBt, uint32(iTable), uintptr(0)) if SQLITE_OK == rc { /* Invalidate all incrblob cursors open on table iTable (assuming iTable ** is the root of a table b-tree - if it is not, the following call is ** a no-op). */ if (*TBtree)(unsafe.Pointer(p)).FhasIncrblobCur != 0 { _invalidateIncrblobCursors(tls, p, uint32(iTable), 0, int32(1)) } rc = _clearDatabasePage(tls, pBt, uint32(iTable), 0, pnChange) } _sqlite3BtreeLeave(tls, p) return rc } // C documentation // // /* // ** Delete all information from the single table that pCur is open on. // ** // ** This routine only work for pCur on an ephemeral table. // */ func _sqlite3BtreeClearTableOfCursor(tls *libc.TLS, pCur uintptr) (r int32) { return _sqlite3BtreeClearTable(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpBtree, int32((*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot), uintptr(0)) } // C documentation // // /* // ** Erase all information in a table and add the root of the table to // ** the freelist. Except, the root of the principle table (the one on // ** page 1) is never added to the freelist. // ** // ** This routine will fail with SQLITE_LOCKED if there are any open // ** cursors on the table. // ** // ** If AUTOVACUUM is enabled and the page at iTable is not the last // ** root page in the database file, then the last root page // ** in the database file is moved into the slot formerly occupied by // ** iTable and that last slot formerly occupied by the last root page // ** is added to the freelist instead of iTable. In this say, all // ** root pages are kept at the beginning of the database file, which // ** is necessary for AUTOVACUUM to work right. *piMoved is set to the // ** page number that used to be the last root page in the file before // ** the move. If no page gets moved, *piMoved is set to 0. // ** The last root page is recorded in meta[3] and the value of // ** meta[3] is updated by this procedure. // */ func _btreeDropTable(tls *libc.TLS, p uintptr, iTable TPgno, piMoved uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pBt uintptr var _ /* maxRootPgno at bp+8 */ TPgno var _ /* pMove at bp+12 */ uintptr var _ /* pPage at bp+4 */ uintptr var _ /* rc at bp+0 */ int32 _ = pBt *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if iTable > _btreePagecount(tls, pBt) { return _sqlite3CorruptError(tls, int32(80361)) } *(*int32)(unsafe.Pointer(bp)) = _sqlite3BtreeClearTable(tls, p, int32(iTable), uintptr(0)) if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } *(*int32)(unsafe.Pointer(bp)) = _btreeGetPage(tls, pBt, iTable, bp+4, 0) if *(*int32)(unsafe.Pointer(bp)) != 0 { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) return *(*int32)(unsafe.Pointer(bp)) } *(*int32)(unsafe.Pointer(piMoved)) = 0 if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { _sqlite3BtreeGetMeta(tls, p, int32(BTREE_LARGEST_ROOT_PAGE), bp+8) if iTable == *(*TPgno)(unsafe.Pointer(bp + 8)) { /* If the table being dropped is the table with the largest root-page ** number in the database, put the root page on the free list. */ _freePage(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), bp) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp)) } } else { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) *(*int32)(unsafe.Pointer(bp)) = _btreeGetPage(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 8)), bp+12, 0) if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp)) } *(*int32)(unsafe.Pointer(bp)) = _relocatePage(tls, pBt, *(*uintptr)(unsafe.Pointer(bp + 12)), uint8(PTRMAP_ROOTPAGE), uint32(0), iTable, 0) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp)) } *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) *(*int32)(unsafe.Pointer(bp)) = _btreeGetPage(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 8)), bp+12, 0) _freePage(tls, *(*uintptr)(unsafe.Pointer(bp + 12)), bp) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp)) } *(*int32)(unsafe.Pointer(piMoved)) = int32(*(*TPgno)(unsafe.Pointer(bp + 8))) } /* Set the new 'max-root-page' value in the database header. This ** is the old value less one, less one more if that happens to ** be a root-page number, less one again if that is the ** PENDING_BYTE_PAGE. */ *(*TPgno)(unsafe.Pointer(bp + 8))-- for *(*TPgno)(unsafe.Pointer(bp + 8)) == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) || _ptrmapPageno(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 8))) == *(*TPgno)(unsafe.Pointer(bp + 8)) { *(*TPgno)(unsafe.Pointer(bp + 8))-- } _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp)) = _sqlite3BtreeUpdateMeta(tls, p, int32(4), *(*TPgno)(unsafe.Pointer(bp + 8))) } else { _freePage(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), bp) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) } return *(*int32)(unsafe.Pointer(bp)) } func _sqlite3BtreeDropTable(tls *libc.TLS, p uintptr, iTable int32, piMoved uintptr) (r int32) { var rc int32 _ = rc _sqlite3BtreeEnter(tls, p) rc = _btreeDropTable(tls, p, uint32(iTable), piMoved) _sqlite3BtreeLeave(tls, p) return rc } // C documentation // // /* // ** This function may only be called if the b-tree connection already // ** has a read or write transaction open on the database. // ** // ** Read the meta-information out of a database file. Meta[0] // ** is the number of free pages currently in the database. Meta[1] // ** through meta[15] are available for use by higher layers. Meta[0] // ** is read-only, the others are read/write. // ** // ** The schema layer numbers meta values differently. At the schema // ** layer (and the SetCookie and ReadCookie opcodes) the number of // ** free pages is not visible. So Cookie[0] is the same as Meta[1]. // ** // ** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead // ** of reading the value out of the header, it instead loads the "DataVersion" // ** from the pager. The BTREE_DATA_VERSION value is not actually stored in the // ** database file. It is a number computed by the pager. But its access // ** pattern is the same as header meta values, and so it is convenient to // ** read it from this routine. // */ func _sqlite3BtreeGetMeta(tls *libc.TLS, p uintptr, idx int32, pMeta uintptr) { var pBt uintptr _ = pBt pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _sqlite3BtreeEnter(tls, p) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if idx == int32(BTREE_DATA_VERSION) { *(*Tu32)(unsafe.Pointer(pMeta)) = _sqlite3PagerDataVersion(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager) + (*TBtree)(unsafe.Pointer(p)).FiBDataVersion } else { *(*Tu32)(unsafe.Pointer(pMeta)) = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+uintptr(int32(36)+idx*int32(4))) } /* If auto-vacuum is disabled in this build and this is an auto-vacuum ** database, mark the database as read-only. */ _sqlite3BtreeLeave(tls, p) } // C documentation // // /* // ** Write meta-information back into the database. Meta[0] is // ** read-only and may not be written. // */ func _sqlite3BtreeUpdateMeta(tls *libc.TLS, p uintptr, idx int32, iMeta Tu32) (r int32) { var pBt, pP1 uintptr var rc int32 _, _, _ = pBt, pP1, rc pBt = (*TBtree)(unsafe.Pointer(p)).FpBt _ = libc.Int32FromInt32(0) _sqlite3BtreeEnter(tls, p) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pP1 = (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FpDbPage) if rc == SQLITE_OK { _sqlite3Put4byte(tls, pP1+uintptr(int32(36)+idx*int32(4)), iMeta) if idx == int32(BTREE_INCR_VACUUM) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TBtShared)(unsafe.Pointer(pBt)).FincrVacuum = uint8(iMeta) } } _sqlite3BtreeLeave(tls, p) return rc } // C documentation // // /* // ** The first argument, pCur, is a cursor opened on some b-tree. Count the // ** number of entries in the b-tree and write the result to *pnEntry. // ** // ** SQLITE_OK is returned if the operation is successfully executed. // ** Otherwise, if an error is encountered (i.e. an IO error or database // ** corruption) an SQLite error code is returned. // */ func _sqlite3BtreeCount(tls *libc.TLS, db uintptr, pCur uintptr, pnEntry uintptr) (r int32) { var iIdx, rc int32 var nEntry Ti64 var pPage uintptr _, _, _, _ = iIdx, nEntry, pPage, rc nEntry = 0 /* Return code */ rc = _moveToRoot(tls, pCur) if rc == int32(SQLITE_EMPTY) { *(*Ti64)(unsafe.Pointer(pnEntry)) = 0 return SQLITE_OK } /* Unless an error occurs, the following loop runs one iteration for each ** page in the B-Tree structure (not including overflow pages). */ for rc == SQLITE_OK && !(libc.AtomicLoadNInt32(db+312, libc.Int32FromInt32(__ATOMIC_RELAXED)) != 0) { /* Current page of the b-tree */ /* If this is a leaf page or the tree is not an int-key tree, then ** this page contains countable entries. Increment the entry counter ** accordingly. */ pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage if (*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0 || !((*TMemPage)(unsafe.Pointer(pPage)).FintKey != 0) { nEntry += int64((*TMemPage)(unsafe.Pointer(pPage)).FnCell) } /* pPage is a leaf node. This loop navigates the cursor so that it ** points to the first interior cell that it points to the parent of ** the next page in the tree that has not yet been visited. The ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell ** of the page, or to the number of cells in the page if the next page ** to visit is the right-child of its parent. ** ** If all pages in the tree have been visited, return SQLITE_OK to the ** caller. */ if (*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0 { for cond := true; cond; cond = int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) >= int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FnCell) { if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) == 0 { /* All pages of the b-tree have been visited. Return successfully. */ *(*Ti64)(unsafe.Pointer(pnEntry)) = nEntry return _moveToRoot(tls, pCur) } _moveToParent(tls, pCur) } (*TBtCursor)(unsafe.Pointer(pCur)).Fix++ pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage } /* Descend to the child node of the cell that the cursor currently ** points at. This is the right-child if (iIdx==pPage->nCell). */ iIdx = int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) if iIdx == int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) { rc = _moveToChild(tls, pCur, _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8)))) } else { rc = _moveToChild(tls, pCur, _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&int32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*iIdx)))))))) } } /* An error has occurred. Return an error code. */ return rc } // C documentation // // /* // ** Return the pager associated with a BTree. This routine is used for // ** testing and debugging only. // */ func _sqlite3BtreePager(tls *libc.TLS, p uintptr) (r uintptr) { return (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FpPager } // C documentation // // /* // ** Record an OOM error during integrity_check // */ func _checkOom(tls *libc.TLS, pCheck uintptr) { (*TIntegrityCk)(unsafe.Pointer(pCheck)).Frc = int32(SQLITE_NOMEM) (*TIntegrityCk)(unsafe.Pointer(pCheck)).FmxErr = 0 /* Causes integrity_check processing to stop */ if (*TIntegrityCk)(unsafe.Pointer(pCheck)).FnErr == 0 { (*TIntegrityCk)(unsafe.Pointer(pCheck)).FnErr++ } } // C documentation // // /* // ** Invoke the progress handler, if appropriate. Also check for an // ** interrupt. // */ func _checkProgress(tls *libc.TLS, pCheck uintptr) { var db uintptr _ = db db = (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fdb if libc.AtomicLoadNInt32(db+312, libc.Int32FromInt32(__ATOMIC_RELAXED)) != 0 { (*TIntegrityCk)(unsafe.Pointer(pCheck)).Frc = int32(SQLITE_INTERRUPT) (*TIntegrityCk)(unsafe.Pointer(pCheck)).FnErr++ (*TIntegrityCk)(unsafe.Pointer(pCheck)).FmxErr = 0 } if (*Tsqlite3)(unsafe.Pointer(db)).FxProgress != 0 { _ = libc.Int32FromInt32(0) (*TIntegrityCk)(unsafe.Pointer(pCheck)).FnStep++ if (*TIntegrityCk)(unsafe.Pointer(pCheck)).FnStep%(*Tsqlite3)(unsafe.Pointer(db)).FnProgressOps == uint32(0) && (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxProgress})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpProgressArg) != 0 { (*TIntegrityCk)(unsafe.Pointer(pCheck)).Frc = int32(SQLITE_INTERRUPT) (*TIntegrityCk)(unsafe.Pointer(pCheck)).FnErr++ (*TIntegrityCk)(unsafe.Pointer(pCheck)).FmxErr = 0 } } } // C documentation // // /* // ** Append a message to the error message string. // */ func _checkAppendMsg(tls *libc.TLS, pCheck uintptr, zFormat uintptr, va uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var ap Tva_list _ = ap _checkProgress(tls, pCheck) if !((*TIntegrityCk)(unsafe.Pointer(pCheck)).FmxErr != 0) { return } (*TIntegrityCk)(unsafe.Pointer(pCheck)).FmxErr-- (*TIntegrityCk)(unsafe.Pointer(pCheck)).FnErr++ ap = va if (*TIntegrityCk)(unsafe.Pointer(pCheck)).FerrMsg.FnChar != 0 { Xsqlite3_str_append(tls, pCheck+48, __ccgo_ts+4203, int32(1)) } if (*TIntegrityCk)(unsafe.Pointer(pCheck)).FzPfx != 0 { Xsqlite3_str_appendf(tls, pCheck+48, (*TIntegrityCk)(unsafe.Pointer(pCheck)).FzPfx, libc.VaList(bp+8, (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv0, (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv1, (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv2)) } Xsqlite3_str_vappendf(tls, pCheck+48, zFormat, ap) _ = ap if int32((*TIntegrityCk)(unsafe.Pointer(pCheck)).FerrMsg.FaccError) == int32(SQLITE_NOMEM) { _checkOom(tls, pCheck) } } // C documentation // // /* // ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that // ** corresponds to page iPg is already set. // */ func _getPageReferenced(tls *libc.TLS, pCheck uintptr, iPg TPgno) (r int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return int32(*(*Tu8)(unsafe.Pointer((*TIntegrityCk)(unsafe.Pointer(pCheck)).FaPgRef + uintptr(iPg/uint32(8))))) & (int32(1) << (iPg & uint32(0x07))) } // C documentation // // /* // ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg. // */ func _setPageReferenced(tls *libc.TLS, pCheck uintptr, iPg TPgno) { var p1 uintptr _ = p1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) p1 = (*TIntegrityCk)(unsafe.Pointer(pCheck)).FaPgRef + uintptr(iPg/uint32(8)) *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | libc.Int32FromInt32(1)<<(iPg&libc.Uint32FromInt32(0x07))) } // C documentation // // /* // ** Add 1 to the reference count for page iPage. If this is the second // ** reference to the page, add an error message to pCheck->zErrMsg. // ** Return 1 if there are 2 or more references to the page and 0 if // ** if this is the first reference to the page. // ** // ** Also check that the page number is in bounds. // */ func _checkRef(tls *libc.TLS, pCheck uintptr, iPage TPgno) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) if iPage > (*TIntegrityCk)(unsafe.Pointer(pCheck)).FnCkPage || iPage == uint32(0) { _checkAppendMsg(tls, pCheck, __ccgo_ts+4205, libc.VaList(bp+8, iPage)) return int32(1) } if _getPageReferenced(tls, pCheck, iPage) != 0 { _checkAppendMsg(tls, pCheck, __ccgo_ts+4228, libc.VaList(bp+8, iPage)) return int32(1) } _setPageReferenced(tls, pCheck, iPage) return 0 } // C documentation // // /* // ** Check that the entry in the pointer-map for page iChild maps to // ** page iParent, pointer type ptrType. If not, append an error message // ** to pCheck. // */ func _checkPtrmap(tls *libc.TLS, pCheck uintptr, iChild TPgno, eType Tu8, iParent TPgno) { bp := tls.Alloc(64) defer tls.Free(64) var rc int32 var _ /* ePtrmapType at bp+0 */ Tu8 var _ /* iPtrmapParent at bp+4 */ TPgno _ = rc rc = _ptrmapGet(tls, (*TIntegrityCk)(unsafe.Pointer(pCheck)).FpBt, iChild, bp, bp+4) if rc != SQLITE_OK { if rc == int32(SQLITE_NOMEM) || rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)< (*TBtShared)(unsafe.Pointer((*TIntegrityCk)(unsafe.Pointer(pCheck)).FpBt)).FusableSize/uint32(4)-uint32(2) { _checkAppendMsg(tls, pCheck, __ccgo_ts+4358, libc.VaList(bp+16, iPage)) N-- } else { i = 0 for { if !(i < int32(n)) { break } iFreePage = _sqlite3Get4byte(tls, pOvflData+uintptr(int32(8)+i*int32(4))) if (*TBtShared)(unsafe.Pointer((*TIntegrityCk)(unsafe.Pointer(pCheck)).FpBt)).FautoVacuum != 0 { _checkPtrmap(tls, pCheck, iFreePage, uint8(PTRMAP_FREEPAGE), uint32(0)) } _checkRef(tls, pCheck, iFreePage) goto _1 _1: ; i++ } N -= n } } else { /* If this database supports auto-vacuum and iPage is not the last ** page in this overflow list, check that the pointer-map entry for ** the following page matches iPage. */ if (*TBtShared)(unsafe.Pointer((*TIntegrityCk)(unsafe.Pointer(pCheck)).FpBt)).FautoVacuum != 0 && N > uint32(0) { i = int32(_sqlite3Get4byte(tls, pOvflData)) _checkPtrmap(tls, pCheck, uint32(i), uint8(PTRMAP_OVERFLOW2), iPage) } } iPage = _sqlite3Get4byte(tls, pOvflData) _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp))) } if N != 0 && nErrAtStart == (*TIntegrityCk)(unsafe.Pointer(pCheck)).FnErr { if isFreeList != 0 { v2 = __ccgo_ts + 4397 } else { v2 = __ccgo_ts + 4402 } _checkAppendMsg(tls, pCheck, __ccgo_ts+4423, libc.VaList(bp+16, v2, expected-N, expected)) } } // C documentation // // /* // ** An implementation of a min-heap. // ** // ** aHeap[0] is the number of elements on the heap. aHeap[1] is the // ** root element. The daughter nodes of aHeap[N] are aHeap[N*2] // ** and aHeap[N*2+1]. // ** // ** The heap property is this: Every node is less than or equal to both // ** of its daughter nodes. A consequence of the heap property is that the // ** root node aHeap[1] is always the minimum value currently in the heap. // ** // ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto // ** the heap, preserving the heap property. The btreeHeapPull() routine // ** removes the root element from the heap (the minimum value in the heap) // ** and then moves other nodes around as necessary to preserve the heap // ** property. // ** // ** This heap is used for cell overlap and coverage testing. Each u32 // ** entry represents the span of a cell or freeblock on a btree page. // ** The upper 16 bits are the index of the first byte of a range and the // ** lower 16 bits are the index of the last byte of that range. // */ func _btreeHeapInsert(tls *libc.TLS, aHeap uintptr, x Tu32) { var i, j, v1, v3 Tu32 var v2 uintptr _, _, _, _, _ = i, j, v1, v2, v3 _ = libc.Int32FromInt32(0) v2 = aHeap *(*Tu32)(unsafe.Pointer(v2))++ v1 = *(*Tu32)(unsafe.Pointer(v2)) i = v1 *(*Tu32)(unsafe.Pointer(aHeap + uintptr(i)*4)) = x for { v3 = i / libc.Uint32FromInt32(2) j = v3 if !(v3 > uint32(0) && *(*Tu32)(unsafe.Pointer(aHeap + uintptr(j)*4)) > *(*Tu32)(unsafe.Pointer(aHeap + uintptr(i)*4))) { break } x = *(*Tu32)(unsafe.Pointer(aHeap + uintptr(j)*4)) *(*Tu32)(unsafe.Pointer(aHeap + uintptr(j)*4)) = *(*Tu32)(unsafe.Pointer(aHeap + uintptr(i)*4)) *(*Tu32)(unsafe.Pointer(aHeap + uintptr(i)*4)) = x i = j } } func _btreeHeapPull(tls *libc.TLS, aHeap uintptr, pOut uintptr) (r int32) { var i, j, x, v1, v2 Tu32 _, _, _, _, _ = i, j, x, v1, v2 v1 = *(*Tu32)(unsafe.Pointer(aHeap)) x = v1 if v1 == uint32(0) { return 0 } *(*Tu32)(unsafe.Pointer(pOut)) = *(*Tu32)(unsafe.Pointer(aHeap + 1*4)) *(*Tu32)(unsafe.Pointer(aHeap + 1*4)) = *(*Tu32)(unsafe.Pointer(aHeap + uintptr(x)*4)) *(*Tu32)(unsafe.Pointer(aHeap + uintptr(x)*4)) = uint32(0xffffffff) *(*Tu32)(unsafe.Pointer(aHeap))-- i = uint32(1) for { v2 = i * libc.Uint32FromInt32(2) j = v2 if !(v2 <= *(*Tu32)(unsafe.Pointer(aHeap))) { break } if *(*Tu32)(unsafe.Pointer(aHeap + uintptr(j)*4)) > *(*Tu32)(unsafe.Pointer(aHeap + uintptr(j+uint32(1))*4)) { j++ } if *(*Tu32)(unsafe.Pointer(aHeap + uintptr(i)*4)) < *(*Tu32)(unsafe.Pointer(aHeap + uintptr(j)*4)) { break } x = *(*Tu32)(unsafe.Pointer(aHeap + uintptr(i)*4)) *(*Tu32)(unsafe.Pointer(aHeap + uintptr(i)*4)) = *(*Tu32)(unsafe.Pointer(aHeap + uintptr(j)*4)) *(*Tu32)(unsafe.Pointer(aHeap + uintptr(j)*4)) = x i = j } return int32(1) } // C documentation // // /* // ** Do various sanity checks on a single page of a tree. Return // ** the tree depth. Root pages return 0. Parents of root pages // ** return 1, and so forth. // ** // ** These checks are done: // ** // ** 1. Make sure that cells and freeblocks do not overlap // ** but combine to completely cover the page. // ** 2. Make sure integer cell keys are in order. // ** 3. Check the integrity of overflow pages. // ** 4. Recursively call checkTreePage on all children. // ** 5. Verify that the depth of all children is the same. // */ func _checkTreePage(tls *libc.TLS, pCheck uintptr, iPage TPgno, piMinKey uintptr, _maxKey Ti64) (r int32) { bp := tls.Alloc(80) defer tls.Free(80) *(*Ti64)(unsafe.Pointer(bp)) = _maxKey var cellStart, d2, depth, doCoverageCheck, hdr, i, j, keyCanBeEqual, nCell, nFrag, pgno, rc, saved_v1, saved_v2, size1, v1, v2, v3, v5 int32 var contentOffset, nPage, pc, prev, size, usableSize Tu32 var data, heap, pBt, pCell, pCellIdx, saved_zPfx uintptr var pgnoOvfl TPgno var savedIsInit Tu8 var _ /* info at bp+16 */ TCellInfo var _ /* pPage at bp+8 */ uintptr var _ /* x at bp+12 */ Tu32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = cellStart, contentOffset, d2, data, depth, doCoverageCheck, hdr, heap, i, j, keyCanBeEqual, nCell, nFrag, nPage, pBt, pCell, pCellIdx, pc, pgno, pgnoOvfl, prev, rc, savedIsInit, saved_v1, saved_v2, saved_zPfx, size, size1, usableSize, v1, v2, v3, v5 *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) /* Result code from subroutine call */ depth = -int32(1) /* Number of cells */ doCoverageCheck = int32(1) /* True if cell coverage checking should be done */ keyCanBeEqual = int32(1) /* Offset to the start of the cell content area */ heap = uintptr(0) prev = uint32(0) /* Next and previous entry on the min-heap */ saved_zPfx = (*TIntegrityCk)(unsafe.Pointer(pCheck)).FzPfx saved_v1 = int32((*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv1) saved_v2 = (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv2 savedIsInit = uint8(0) /* Check that the page exists */ _checkProgress(tls, pCheck) if (*TIntegrityCk)(unsafe.Pointer(pCheck)).FmxErr == 0 { goto end_of_check } pBt = (*TIntegrityCk)(unsafe.Pointer(pCheck)).FpBt usableSize = (*TBtShared)(unsafe.Pointer(pBt)).FusableSize if iPage == uint32(0) { return 0 } if _checkRef(tls, pCheck, iPage) != 0 { return 0 } (*TIntegrityCk)(unsafe.Pointer(pCheck)).FzPfx = __ccgo_ts + 4449 (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv1 = iPage v1 = _btreeGetPage(tls, pBt, iPage, bp+8, 0) rc = v1 if v1 != 0 { _checkAppendMsg(tls, pCheck, __ccgo_ts+4467, libc.VaList(bp+48, rc)) if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<= 0 && (*TIntegrityCk)(unsafe.Pointer(pCheck)).FmxErr != 0) { break } /* Check cell size */ (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv2 = i _ = libc.Int32FromInt32(0) pc = uint32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer(pCellIdx)))) pCellIdx -= uintptr(2) if pc < contentOffset || pc > usableSize-uint32(4) { _checkAppendMsg(tls, pCheck, __ccgo_ts+4621, libc.VaList(bp+48, pc, contentOffset, usableSize-uint32(4))) doCoverageCheck = 0 goto _4 } pCell = data + uintptr(pc) (*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FxParseCell})))(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), pCell, bp+16) if pc+uint32((*(*TCellInfo)(unsafe.Pointer(bp + 16))).FnSize) > usableSize { _checkAppendMsg(tls, pCheck, __ccgo_ts+4651, 0) doCoverageCheck = 0 goto _4 } /* Check for integer primary key out of range */ if (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FintKey != 0 { if keyCanBeEqual != 0 { v5 = libc.BoolInt32((*(*TCellInfo)(unsafe.Pointer(bp + 16))).FnKey > *(*Ti64)(unsafe.Pointer(bp))) } else { v5 = libc.BoolInt32((*(*TCellInfo)(unsafe.Pointer(bp + 16))).FnKey >= *(*Ti64)(unsafe.Pointer(bp))) } if v5 != 0 { _checkAppendMsg(tls, pCheck, __ccgo_ts+4675, libc.VaList(bp+48, (*(*TCellInfo)(unsafe.Pointer(bp + 16))).FnKey)) } *(*Ti64)(unsafe.Pointer(bp)) = (*(*TCellInfo)(unsafe.Pointer(bp + 16))).FnKey keyCanBeEqual = 0 /* Only the first key on the page may ==maxKey */ } /* Check the content overflow list */ if (*(*TCellInfo)(unsafe.Pointer(bp + 16))).FnPayload > uint32((*(*TCellInfo)(unsafe.Pointer(bp + 16))).FnLocal) { /* First page of the overflow chain */ _ = libc.Int32FromInt32(0) nPage = ((*(*TCellInfo)(unsafe.Pointer(bp + 16))).FnPayload - uint32((*(*TCellInfo)(unsafe.Pointer(bp + 16))).FnLocal) + usableSize - uint32(5)) / (usableSize - uint32(4)) pgnoOvfl = _sqlite3Get4byte(tls, pCell+uintptr(int32((*(*TCellInfo)(unsafe.Pointer(bp + 16))).FnSize)-int32(4))) if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { _checkPtrmap(tls, pCheck, pgnoOvfl, uint8(PTRMAP_OVERFLOW1), iPage) } _checkList(tls, pCheck, 0, pgnoOvfl, nPage) } if !((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).Fleaf != 0) { /* Check sanity of left child page for internal pages */ pgno = int32(_sqlite3Get4byte(tls, pCell)) if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { _checkPtrmap(tls, pCheck, uint32(pgno), uint8(PTRMAP_BTREE), iPage) } d2 = _checkTreePage(tls, pCheck, uint32(pgno), bp, *(*Ti64)(unsafe.Pointer(bp))) keyCanBeEqual = 0 if d2 != depth { _checkAppendMsg(tls, pCheck, __ccgo_ts+4699, 0) depth = d2 } } else { /* Populate the coverage-checking heap for leaf pages */ _btreeHeapInsert(tls, heap, pc< 0 { /* For leaf pages, the min-heap has already been initialized and the ** cells have already been inserted. But for internal pages, that has ** not yet been done, so do it now */ if !((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).Fleaf != 0) { heap = (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fheap *(*Tu32)(unsafe.Pointer(heap)) = uint32(0) i = nCell - int32(1) for { if !(i >= 0) { break } pc = uint32(libc.X__builtin_bswap16(tls, *(*Tu16)(unsafe.Pointer(data + uintptr(cellStart+i*int32(2)))))) size = uint32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FxCellSize})))(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), data+uintptr(pc))) _btreeHeapInsert(tls, heap, pc< 0 { _ = libc.Int32FromInt32(0) /* Enforced by btreeComputeFreeSpace() */ size1 = int32(*(*Tu8)(unsafe.Pointer(data + uintptr(i+int32(2)))))<= *(*Tu32)(unsafe.Pointer(bp + 12))>>libc.Int32FromInt32(16) { _checkAppendMsg(tls, pCheck, __ccgo_ts+4724, libc.VaList(bp+48, *(*Tu32)(unsafe.Pointer(bp + 12))>>int32(16), iPage)) break } else { nFrag = int32(uint32(nFrag) + (*(*Tu32)(unsafe.Pointer(bp + 12))>>libc.Int32FromInt32(16) - prev&libc.Uint32FromInt32(0xffff) - libc.Uint32FromInt32(1))) prev = *(*Tu32)(unsafe.Pointer(bp + 12)) } } nFrag = int32(uint32(nFrag) + (usableSize - prev&libc.Uint32FromInt32(0xffff) - libc.Uint32FromInt32(1))) /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments ** is stored in the fifth field of the b-tree page header. ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the ** number of fragmented free bytes within the cell content area. */ if *(*Tu32)(unsafe.Pointer(heap)) == uint32(0) && nFrag != int32(*(*Tu8)(unsafe.Pointer(data + uintptr(hdr+int32(7))))) { _checkAppendMsg(tls, pCheck, __ccgo_ts+4761, libc.VaList(bp+48, nFrag, int32(*(*Tu8)(unsafe.Pointer(data + uintptr(hdr+int32(7))))), iPage)) } } goto end_of_check end_of_check: ; if !(doCoverageCheck != 0) { (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FisInit = savedIsInit } _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) (*TIntegrityCk)(unsafe.Pointer(pCheck)).FzPfx = saved_zPfx (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv1 = uint32(saved_v1) (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv2 = saved_v2 return depth + int32(1) } // C documentation // // /* // ** This routine does a complete check of the given BTree file. aRoot[] is // ** an array of pages numbers were each page number is the root page of // ** a table. nRoot is the number of entries in aRoot. // ** // ** A read-only or read-write transaction must be opened before calling // ** this function. // ** // ** Write the number of error seen in *pnErr. Except for some memory // ** allocation errors, an error message held in memory obtained from // ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is // ** returned. If a memory allocation error occurs, NULL is returned. // ** // ** If the first entry in aRoot[] is 0, that indicates that the list of // ** root pages is incomplete. This is a "partial integrity-check". This // ** happens when performing an integrity check on a single table. The // ** zero is skipped, of course. But in addition, the freelist checks // ** and the checks to make sure every page is referenced are also skipped, // ** since obviously it is not possible to know which pages are covered by // ** the unverified btrees. Except, if aRoot[1] is 1, then the freelist // ** checks are still performed. // */ func _sqlite3BtreeIntegrityCheck(tls *libc.TLS, db uintptr, p uintptr, aRoot uintptr, nRoot int32, mxErr int32, pnErr uintptr, pzOut uintptr) (r int32) { bp := tls.Alloc(224) defer tls.Free(224) var bCkFreelist, bPartial int32 var i, mx, mxInHdr TPgno var pBt uintptr var savedDbFlags Tu64 var _ /* notUsed at bp+184 */ Ti64 var _ /* sCheck at bp+0 */ TIntegrityCk var _ /* zErr at bp+80 */ [100]uint8 _, _, _, _, _, _, _ = bCkFreelist, bPartial, i, mx, mxInHdr, pBt, savedDbFlags pBt = (*TBtree)(unsafe.Pointer(p)).FpBt savedDbFlags = (*Tsqlite3)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).Fdb)).Fflags bPartial = 0 /* True if not checking all btrees */ bCkFreelist = int32(1) /* True to scan the freelist */ _ = libc.Int32FromInt32(0) /* aRoot[0]==0 means this is a partial check */ if *(*TPgno)(unsafe.Pointer(aRoot)) == uint32(0) { _ = libc.Int32FromInt32(0) bPartial = int32(1) if *(*TPgno)(unsafe.Pointer(aRoot + 1*4)) != uint32(1) { bCkFreelist = 0 } } _sqlite3BtreeEnter(tls, p) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) libc.Xmemset(tls, bp, 0, uint32(80)) (*(*TIntegrityCk)(unsafe.Pointer(bp))).Fdb = db (*(*TIntegrityCk)(unsafe.Pointer(bp))).FpBt = pBt (*(*TIntegrityCk)(unsafe.Pointer(bp))).FpPager = (*TBtShared)(unsafe.Pointer(pBt)).FpPager (*(*TIntegrityCk)(unsafe.Pointer(bp))).FnCkPage = _btreePagecount(tls, (*(*TIntegrityCk)(unsafe.Pointer(bp))).FpBt) (*(*TIntegrityCk)(unsafe.Pointer(bp))).FmxErr = mxErr _sqlite3StrAccumInit(tls, bp+48, uintptr(0), bp+80, int32(100), int32(SQLITE_MAX_LENGTH)) (*(*TIntegrityCk)(unsafe.Pointer(bp))).FerrMsg.FprintfFlags = uint8(SQLITE_PRINTF_INTERNAL) if (*(*TIntegrityCk)(unsafe.Pointer(bp))).FnCkPage == uint32(0) { goto integrity_ck_cleanup } (*(*TIntegrityCk)(unsafe.Pointer(bp))).FaPgRef = _sqlite3MallocZero(tls, uint64((*(*TIntegrityCk)(unsafe.Pointer(bp))).FnCkPage/uint32(8)+uint32(1))) if !((*(*TIntegrityCk)(unsafe.Pointer(bp))).FaPgRef != 0) { _checkOom(tls, bp) goto integrity_ck_cleanup } (*(*TIntegrityCk)(unsafe.Pointer(bp))).Fheap = _sqlite3PageMalloc(tls, int32((*TBtShared)(unsafe.Pointer(pBt)).FpageSize)) if (*(*TIntegrityCk)(unsafe.Pointer(bp))).Fheap == uintptr(0) { _checkOom(tls, bp) goto integrity_ck_cleanup } i = uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize + libc.Uint32FromInt32(1) if i <= (*(*TIntegrityCk)(unsafe.Pointer(bp))).FnCkPage { _setPageReferenced(tls, bp, i) } /* Check the integrity of the freelist */ if bCkFreelist != 0 { (*(*TIntegrityCk)(unsafe.Pointer(bp))).FzPfx = __ccgo_ts + 4813 _checkList(tls, bp, int32(1), _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+32), _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+36)) (*(*TIntegrityCk)(unsafe.Pointer(bp))).FzPfx = uintptr(0) } /* Check all the tables. */ if !(bPartial != 0) { if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { mx = uint32(0) i = uint32(0) for { if !(int32(i) < nRoot) { break } if mx < *(*TPgno)(unsafe.Pointer(aRoot + uintptr(i)*4)) { mx = *(*TPgno)(unsafe.Pointer(aRoot + uintptr(i)*4)) } goto _1 _1: ; i++ } mxInHdr = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+52) if mx != mxInHdr { _checkAppendMsg(tls, bp, __ccgo_ts+4824, libc.VaList(bp+200, mx, mxInHdr)) } } else { if _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+64) != uint32(0) { _checkAppendMsg(tls, bp, __ccgo_ts+4869, 0) } } } *(*Tu64)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).Fdb + 32)) &= ^libc.Uint64FromInt32(SQLITE_CellSizeCk) i = uint32(0) for { if !(int32(i) < nRoot && (*(*TIntegrityCk)(unsafe.Pointer(bp))).FmxErr != 0) { break } if *(*TPgno)(unsafe.Pointer(aRoot + uintptr(i)*4)) == uint32(0) { goto _2 } if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 && *(*TPgno)(unsafe.Pointer(aRoot + uintptr(i)*4)) > uint32(1) && !(bPartial != 0) { _checkPtrmap(tls, bp, *(*TPgno)(unsafe.Pointer(aRoot + uintptr(i)*4)), uint8(PTRMAP_ROOTPAGE), uint32(0)) } (*(*TIntegrityCk)(unsafe.Pointer(bp))).Fv0 = *(*TPgno)(unsafe.Pointer(aRoot + uintptr(i)*4)) _checkTreePage(tls, bp, *(*TPgno)(unsafe.Pointer(aRoot + uintptr(i)*4)), bp+184, libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<= int32(CURSOR_REQUIRESEEK) { v1 = _btreeRestoreCursorPosition(tls, pCsr) } else { v1 = SQLITE_OK } rc = v1 if rc != SQLITE_OK { return rc } _ = libc.Int32FromInt32(0) if int32((*TBtCursor)(unsafe.Pointer(pCsr)).FeState) != CURSOR_VALID { return int32(SQLITE_ABORT) } /* Save the positions of all other cursors open on this table. This is ** required in case any of them are holding references to an xFetch ** version of the b-tree page modified by the accessPayload call below. ** ** Note that pCsr must be open on a INTKEY table and saveCursorPosition() ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence ** saveAllCursors can only return SQLITE_OK. */ _saveAllCursors(tls, (*TBtCursor)(unsafe.Pointer(pCsr)).FpBt, (*TBtCursor)(unsafe.Pointer(pCsr)).FpgnoRoot, pCsr) _ = libc.Int32FromInt32(0) /* Check some assumptions: ** (a) the cursor is open for writing, ** (b) there is a read/write transaction open, ** (c) the connection holds a write-lock on the table (if required), ** (d) there are no conflicting read-locks, and ** (e) the cursor points at a valid row of an intKey table. */ if int32((*TBtCursor)(unsafe.Pointer(pCsr)).FcurFlags)&int32(BTCF_WriteFlag) == 0 { return int32(SQLITE_READONLY) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return _accessPayload(tls, pCsr, offset, amt, z, int32(1)) } // C documentation // // /* // ** Mark this cursor as an incremental blob cursor. // */ func _sqlite3BtreeIncrblobCursor(tls *libc.TLS, pCur uintptr) { var p1 uintptr _ = p1 p1 = pCur + 1 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | libc.Int32FromInt32(BTCF_Incrblob)) (*TBtree)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBtree)).FhasIncrblobCur = uint8(1) } // C documentation // // /* // ** Set both the "read version" (single byte at byte offset 18) and // ** "write version" (single byte at byte offset 19) fields in the database // ** header to iVersion. // */ func _sqlite3BtreeSetVersion(tls *libc.TLS, pBtree uintptr, iVersion int32) (r int32) { var aData, pBt, p1, p2, p3 uintptr var rc int32 _, _, _, _, _, _ = aData, pBt, rc, p1, p2, p3 pBt = (*TBtree)(unsafe.Pointer(pBtree)).FpBt /* Return code */ _ = libc.Int32FromInt32(0) /* If setting the version fields to 1, do not automatically open the ** WAL connection, even if the version fields are currently set to 2. */ p1 = pBt + 24 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(BTS_NO_WAL)) if iVersion == int32(1) { p2 = pBt + 24 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(BTS_NO_WAL)) } rc = _sqlite3BtreeBeginTrans(tls, pBtree, 0, uintptr(0)) if rc == SQLITE_OK { aData = (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData if int32(*(*Tu8)(unsafe.Pointer(aData + 18))) != int32(uint8(iVersion)) || int32(*(*Tu8)(unsafe.Pointer(aData + 19))) != int32(uint8(iVersion)) { rc = _sqlite3BtreeBeginTrans(tls, pBtree, int32(2), uintptr(0)) if rc == SQLITE_OK { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FpDbPage) if rc == SQLITE_OK { *(*Tu8)(unsafe.Pointer(aData + 18)) = uint8(iVersion) *(*Tu8)(unsafe.Pointer(aData + 19)) = uint8(iVersion) } } } } p3 = pBt + 24 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) & ^libc.Int32FromInt32(BTS_NO_WAL)) return rc } // C documentation // // /* // ** Return true if the cursor has a hint specified. This routine is // ** only used from within assert() statements // */ func _sqlite3BtreeCursorHasHint(tls *libc.TLS, pCsr uintptr, mask uint32) (r int32) { return libc.BoolInt32(uint32((*TBtCursor)(unsafe.Pointer(pCsr)).Fhints)&mask != uint32(0)) } // C documentation // // /* // ** Return true if the given Btree is read-only. // */ func _sqlite3BtreeIsReadonly(tls *libc.TLS, p uintptr) (r int32) { return libc.BoolInt32(int32((*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FbtsFlags)&int32(BTS_READ_ONLY) != 0) } // C documentation // // /* // ** Return the size of the header added to each page by this module. // */ func _sqlite3HeaderSizeBtree(tls *libc.TLS) (r int32) { return int32((libc.Uint32FromInt64(84) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7))) } // C documentation // // /* // ** If no transaction is active and the database is not a temp-db, clear // ** the in-memory pager cache. // */ func _sqlite3BtreeClearCache(tls *libc.TLS, p uintptr) { var pBt uintptr _ = pBt pBt = (*TBtree)(unsafe.Pointer(p)).FpBt if int32((*TBtShared)(unsafe.Pointer(pBt)).FinTransaction) == TRANS_NONE { _sqlite3PagerClearCache(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager) } } // C documentation // // /* // ** Return true if the Btree passed as the only argument is sharable. // */ func _sqlite3BtreeSharable(tls *libc.TLS, p uintptr) (r int32) { return int32((*TBtree)(unsafe.Pointer(p)).Fsharable) } // C documentation // // /* // ** Return the number of connections to the BtShared object accessed by // ** the Btree handle passed as the only argument. For private caches // ** this is always 1. For shared caches it may be 1 or greater. // */ func _sqlite3BtreeConnectionCount(tls *libc.TLS, p uintptr) (r int32) { return (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FnRef } /************** End of btree.c ***********************************************/ /************** Begin file backup.c ******************************************/ /* ** 2009 January 28 ** ** 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 contains the implementation of the sqlite3_backup_XXX() ** API functions and the related features. */ /* #include "sqliteInt.h" */ /* #include "btreeInt.h" */ /* ** Structure allocated for each backup operation. */ type Tsqlite3_backup1 = struct { FpDestDb uintptr FpDest uintptr FiDestSchema Tu32 FbDestLocked int32 FiNext TPgno FpSrcDb uintptr FpSrc uintptr Frc int32 FnRemaining TPgno FnPagecount TPgno FisAttached int32 FpNext uintptr } type sqlite3_backup1 = Tsqlite3_backup1 /* ** THREAD SAFETY NOTES: ** ** Once it has been created using backup_init(), a single sqlite3_backup ** structure may be accessed via two groups of thread-safe entry points: ** ** * Via the sqlite3_backup_XXX() API function backup_step() and ** backup_finish(). Both these functions obtain the source database ** handle mutex and the mutex associated with the source BtShared ** structure, in that order. ** ** * Via the BackupUpdate() and BackupRestart() functions, which are ** invoked by the pager layer to report various state changes in ** the page cache associated with the source database. The mutex ** associated with the source database BtShared structure will always ** be held when either of these functions are invoked. ** ** The other sqlite3_backup_XXX() API functions, backup_remaining() and ** backup_pagecount() are not thread-safe functions. If they are called ** while some other thread is calling backup_step() or backup_finish(), ** the values returned may be invalid. There is no way for a call to ** BackupUpdate() or BackupRestart() to interfere with backup_remaining() ** or backup_pagecount(). ** ** Depending on the SQLite configuration, the database handles and/or ** the Btree objects may have their own mutexes that require locking. ** Non-sharable Btrees (in-memory databases for example), do not have ** associated mutexes. */ // C documentation // // /* // ** Return a pointer corresponding to database zDb (i.e. "main", "temp") // ** in connection handle pDb. If such a database cannot be found, return // ** a NULL pointer and write an error message to pErrorDb. // ** // ** If the "temp" database is requested, it may need to be opened by this // ** function. If an error occurs while doing so, return 0 and write an // ** error message to pErrorDb. // */ func _findBtree(tls *libc.TLS, pErrorDb uintptr, pDb uintptr, zDb uintptr) (r uintptr) { bp := tls.Alloc(304) defer tls.Free(304) var i, rc int32 var _ /* sParse at bp+0 */ TParse _, _ = i, rc i = _sqlite3FindDbName(tls, pDb, zDb) if i == int32(1) { rc = 0 _sqlite3ParseObjectInit(tls, bp, pDb) if _sqlite3OpenTempDatabase(tls, bp) != 0 { _sqlite3ErrorWithMsg(tls, pErrorDb, (*(*TParse)(unsafe.Pointer(bp))).Frc, __ccgo_ts+3795, libc.VaList(bp+296, (*(*TParse)(unsafe.Pointer(bp))).FzErrMsg)) rc = int32(SQLITE_ERROR) } _sqlite3DbFree(tls, pErrorDb, (*(*TParse)(unsafe.Pointer(bp))).FzErrMsg) _sqlite3ParseObjectReset(tls, bp) if rc != 0 { return uintptr(0) } } if i < 0 { _sqlite3ErrorWithMsg(tls, pErrorDb, int32(SQLITE_ERROR), __ccgo_ts+4976, libc.VaList(bp+296, zDb)) return uintptr(0) } return (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(pDb)).FaDb + uintptr(i)*16))).FpBt } // C documentation // // /* // ** Attempt to set the page size of the destination to match the page size // ** of the source. // */ func _setDestPgsz(tls *libc.TLS, p uintptr) (r int32) { var rc int32 _ = rc rc = _sqlite3BtreeSetPageSize(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest, _sqlite3BtreeGetPageSize(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc), 0, 0) return rc } // C documentation // // /* // ** Check that there is no open read-transaction on the b-tree passed as the // ** second argument. If there is not, return SQLITE_OK. Otherwise, if there // ** is an open read-transaction, return SQLITE_ERROR and leave an error // ** message in database handle db. // */ func _checkReadTransaction(tls *libc.TLS, db uintptr, p uintptr) (r int32) { if _sqlite3BtreeTxnState(tls, p) != SQLITE_TXN_NONE { _sqlite3ErrorWithMsg(tls, db, int32(SQLITE_ERROR), __ccgo_ts+4996, 0) return int32(SQLITE_ERROR) } return SQLITE_OK } // C documentation // // /* // ** Create an sqlite3_backup process to copy the contents of zSrcDb from // ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return // ** a pointer to the new sqlite3_backup object. // ** // ** If an error occurs, NULL is returned and an error code and error message // ** stored in database handle pDestDb. // */ func Xsqlite3_backup_init(tls *libc.TLS, pDestDb uintptr, zDestDb uintptr, pSrcDb uintptr, zSrcDb uintptr) (r uintptr) { var p uintptr _ = p /* Value to return */ /* Lock the source database handle. The destination database ** handle is not locked in this routine, but it is locked in ** sqlite3_backup_step(). The user is required to ensure that no ** other thread accesses the destination handle for the duration ** of the backup operation. Any attempt to use the destination ** database connection while a backup is in progress may cause ** a malfunction or a deadlock. */ Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(pSrcDb)).Fmutex) Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(pDestDb)).Fmutex) if pSrcDb == pDestDb { _sqlite3ErrorWithMsg(tls, pDestDb, int32(SQLITE_ERROR), __ccgo_ts+5027, 0) p = uintptr(0) } else { /* Allocate space for a new sqlite3_backup object... ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a ** call to sqlite3_backup_init() and is destroyed by a call to ** sqlite3_backup_finish(). */ p = _sqlite3MallocZero(tls, uint64(48)) if !(p != 0) { _sqlite3Error(tls, pDestDb, int32(SQLITE_NOMEM)) } } /* If the allocation succeeded, populate the new object. */ if p != 0 { (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc = _findBtree(tls, pDestDb, pSrcDb, zSrcDb) (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest = _findBtree(tls, pDestDb, pDestDb, zDestDb) (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb = pDestDb (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrcDb = pSrcDb (*Tsqlite3_backup)(unsafe.Pointer(p)).FiNext = uint32(1) (*Tsqlite3_backup)(unsafe.Pointer(p)).FisAttached = 0 if uintptr(0) == (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc || uintptr(0) == (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest || _checkReadTransaction(tls, pDestDb, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest) != SQLITE_OK { /* One (or both) of the named databases did not exist or an OOM ** error was hit. Or there is a transaction open on the destination ** database. The error has already been written into the pDestDb ** handle. All that is left to do here is free the sqlite3_backup ** structure. */ Xsqlite3_free(tls, p) p = uintptr(0) } } if p != 0 { (*TBtree)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc)).FnBackup++ } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(pDestDb)).Fmutex) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(pSrcDb)).Fmutex) return p } // C documentation // // /* // ** Argument rc is an SQLite error code. Return true if this error is // ** considered fatal if encountered during a backup operation. All errors // ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED. // */ func _isFatalError(tls *libc.TLS, rc int32) (r int32) { return libc.BoolInt32(rc != SQLITE_OK && rc != int32(SQLITE_BUSY) && rc != int32(SQLITE_LOCKED)) } // C documentation // // /* // ** Parameter zSrcData points to a buffer containing the data for // ** page iSrcPg from the source database. Copy this data into the // ** destination database. // */ func _backupOnePage(tls *libc.TLS, p uintptr, iSrcPg TPgno, zSrcData uintptr, bUpdate int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iDest TPgno var iEnd, iOff Ti64 var nCopy, nDestPgsz, nSrcPgsz, rc, v1, v3, v4 int32 var pDestPager, zDestData, zIn, zOut uintptr var v5 bool var _ /* pDestPg at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = iDest, iEnd, iOff, nCopy, nDestPgsz, nSrcPgsz, pDestPager, rc, zDestData, zIn, zOut, v1, v3, v4, v5 pDestPager = _sqlite3BtreePager(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest) nSrcPgsz = _sqlite3BtreeGetPageSize(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc) nDestPgsz = _sqlite3BtreeGetPageSize(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest) if nSrcPgsz < nDestPgsz { v1 = nSrcPgsz } else { v1 = nDestPgsz } nCopy = v1 iEnd = int64(iSrcPg) * int64(nSrcPgsz) rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* This loop runs once for each destination page spanned by the source ** page. For each iteration, variable iOff is set to the byte offset ** of the destination page. */ iOff = iEnd - int64(nSrcPgsz) for { if !(rc == SQLITE_OK && iOff < iEnd) { break } *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) iDest = uint32(iOff/int64(nDestPgsz)) + uint32(1) if iDest == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest)).FpBt)).FpageSize+libc.Uint32FromInt32(1) { goto _2 } v3 = _sqlite3PagerGet(tls, pDestPager, iDest, bp, 0) rc = v3 if v5 = SQLITE_OK == v3; v5 { v4 = _sqlite3PagerWrite(tls, *(*uintptr)(unsafe.Pointer(bp))) rc = v4 } if v5 && SQLITE_OK == v4 { zIn = zSrcData + uintptr(iOff%int64(nSrcPgsz)) zDestData = _sqlite3PagerGetData(tls, *(*uintptr)(unsafe.Pointer(bp))) zOut = zDestData + uintptr(iOff%int64(nDestPgsz)) /* Copy the data from the source page into the destination page. ** Then clear the Btree layer MemPage.isInit flag. Both this module ** and the pager code use this trick (clearing the first byte ** of the page 'extra' space to invalidate the Btree layers ** cached parse of the page). MemPage.isInit is marked ** "MUST BE FIRST" for this purpose. */ libc.Xmemcpy(tls, zOut, zIn, uint32(nCopy)) *(*Tu8)(unsafe.Pointer(_sqlite3PagerGetExtra(tls, *(*uintptr)(unsafe.Pointer(bp))))) = uint8(0) if iOff == 0 && bUpdate == 0 { _sqlite3Put4byte(tls, zOut+28, _sqlite3BtreeLastPage(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc)) } } _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp))) goto _2 _2: ; iOff += int64(nDestPgsz) } return rc } // C documentation // // /* // ** If pFile is currently larger than iSize bytes, then truncate it to // ** exactly iSize bytes. If pFile is not larger than iSize bytes, then // ** this function is a no-op. // ** // ** Return SQLITE_OK if everything is successful, or an SQLite error // ** code if an error occurs. // */ func _backupTruncateFile(tls *libc.TLS, pFile uintptr, iSize Ti64) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* iCurrent at bp+0 */ Ti64 _ = rc rc = _sqlite3OsFileSize(tls, pFile, bp) if rc == SQLITE_OK && *(*Ti64)(unsafe.Pointer(bp)) > iSize { rc = _sqlite3OsTruncate(tls, pFile, iSize) } return rc } // C documentation // // /* // ** Register this backup object with the associated source pager for // ** callbacks when pages are changed or the cache invalidated. // */ func _attachBackupObject(tls *libc.TLS, p uintptr) { var pp uintptr _ = pp _ = libc.Int32FromInt32(0) pp = _sqlite3PagerBackupPtr(tls, _sqlite3BtreePager(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc)) (*Tsqlite3_backup)(unsafe.Pointer(p)).FpNext = *(*uintptr)(unsafe.Pointer(pp)) *(*uintptr)(unsafe.Pointer(pp)) = p (*Tsqlite3_backup)(unsafe.Pointer(p)).FisAttached = int32(1) } // C documentation // // /* // ** Copy nPage pages from the source b-tree to the destination. // */ func Xsqlite3_backup_step(tls *libc.TLS, p uintptr, nPage int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bCloseTrans, destMode, ii, nDestTruncate, nSrcPage, pgszDest, pgszSrc, ratio, rc, v1, v7 int32 var iEnd, iOff, iSize Ti64 var iPg, iSrcPg, iSrcPg1 TPgno var pDestPager, pFile, pSrcPager, zData uintptr var v2, v8 bool var v5 int64 var _ /* nDstPage at bp+4 */ int32 var _ /* pPg at bp+8 */ uintptr var _ /* pSrcPg at bp+0 */ uintptr var _ /* pSrcPg at bp+12 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bCloseTrans, destMode, iEnd, iOff, iPg, iSize, iSrcPg, iSrcPg1, ii, nDestTruncate, nSrcPage, pDestPager, pFile, pSrcPager, pgszDest, pgszSrc, ratio, rc, zData, v1, v2, v5, v7, v8 /* Destination journal mode */ pgszSrc = 0 /* Source page size */ pgszDest = 0 /* Destination page size */ Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrcDb)).Fmutex) _sqlite3BtreeEnter(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc) if (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb != 0 { Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb)).Fmutex) } rc = (*Tsqlite3_backup)(unsafe.Pointer(p)).Frc if !(_isFatalError(tls, rc) != 0) { pSrcPager = _sqlite3BtreePager(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc) /* Source pager */ pDestPager = _sqlite3BtreePager(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest) /* Iterator variable */ nSrcPage = -int32(1) /* Size of source db in pages */ bCloseTrans = 0 /* True if src db requires unlocking */ /* If the source pager is currently in a write-transaction, return ** SQLITE_BUSY immediately. */ if (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb != 0 && int32((*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc)).FpBt)).FinTransaction) == int32(TRANS_WRITE) { rc = int32(SQLITE_BUSY) } else { rc = SQLITE_OK } /* If there is no open read-transaction on the source database, open ** one now. If a transaction is opened here, then it will be closed ** before this function exits. */ if rc == SQLITE_OK && SQLITE_TXN_NONE == _sqlite3BtreeTxnState(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc) { rc = _sqlite3BtreeBeginTrans(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc, 0, uintptr(0)) bCloseTrans = int32(1) } /* If the destination database has not yet been locked (i.e. if this ** is the first call to backup_step() for the current backup operation), ** try to set its page size to the same as the source database. This ** is especially important on ZipVFS systems, as in that case it is ** not possible to create a database file that uses one page size by ** writing to it with another. */ if (*Tsqlite3_backup)(unsafe.Pointer(p)).FbDestLocked == 0 && rc == SQLITE_OK && _setDestPgsz(tls, p) == int32(SQLITE_NOMEM) { rc = int32(SQLITE_NOMEM) } /* Lock the destination database, if it is not locked already. */ if v2 = SQLITE_OK == rc && (*Tsqlite3_backup)(unsafe.Pointer(p)).FbDestLocked == 0; v2 { v1 = _sqlite3BtreeBeginTrans(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest, int32(2), p+8) rc = v1 } if v2 && SQLITE_OK == v1 { (*Tsqlite3_backup)(unsafe.Pointer(p)).FbDestLocked = int32(1) } /* Do not allow backup if the destination database is in WAL mode ** and the page sizes are different between source and destination */ pgszSrc = _sqlite3BtreeGetPageSize(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc) pgszDest = _sqlite3BtreeGetPageSize(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest) destMode = _sqlite3PagerGetJournalMode(tls, _sqlite3BtreePager(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest)) if SQLITE_OK == rc && (destMode == int32(PAGER_JOURNALMODE_WAL) || _sqlite3PagerIsMemdb(tls, pDestPager) != 0) && pgszSrc != pgszDest { rc = int32(SQLITE_READONLY) } /* Now that there is a read-lock on the source database, query the ** source pager for the number of pages in the database. */ nSrcPage = int32(_sqlite3BtreeLastPage(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc)) _ = libc.Int32FromInt32(0) ii = 0 for { if !((nPage < 0 || ii < nPage) && (*Tsqlite3_backup)(unsafe.Pointer(p)).FiNext <= uint32(nSrcPage) && !(rc != 0)) { break } iSrcPg = (*Tsqlite3_backup)(unsafe.Pointer(p)).FiNext /* Source page number */ if iSrcPg != uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc)).FpBt)).FpageSize+libc.Uint32FromInt32(1) { /* Source page object */ rc = _sqlite3PagerGet(tls, pSrcPager, iSrcPg, bp, int32(PAGER_GET_READONLY)) if rc == SQLITE_OK { rc = _backupOnePage(tls, p, iSrcPg, _sqlite3PagerGetData(tls, *(*uintptr)(unsafe.Pointer(bp))), 0) _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp))) } } (*Tsqlite3_backup)(unsafe.Pointer(p)).FiNext++ goto _3 _3: ; ii++ } if rc == SQLITE_OK { (*Tsqlite3_backup)(unsafe.Pointer(p)).FnPagecount = uint32(nSrcPage) (*Tsqlite3_backup)(unsafe.Pointer(p)).FnRemaining = uint32(nSrcPage+int32(1)) - (*Tsqlite3_backup)(unsafe.Pointer(p)).FiNext if (*Tsqlite3_backup)(unsafe.Pointer(p)).FiNext > uint32(nSrcPage) { rc = int32(SQLITE_DONE) } else { if !((*Tsqlite3_backup)(unsafe.Pointer(p)).FisAttached != 0) { _attachBackupObject(tls, p) } } } /* Update the schema version field in the destination database. This ** is to make sure that the schema-version really does change in ** the case where the source and destination databases have the ** same schema version. */ if rc == int32(SQLITE_DONE) { if nSrcPage == 0 { rc = _sqlite3BtreeNewDb(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest) nSrcPage = int32(1) } if rc == SQLITE_OK || rc == int32(SQLITE_DONE) { rc = _sqlite3BtreeUpdateMeta(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest, int32(1), (*Tsqlite3_backup)(unsafe.Pointer(p)).FiDestSchema+uint32(1)) } if rc == SQLITE_OK { if (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb != 0 { _sqlite3ResetAllSchemasOfConnection(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb) } if destMode == int32(PAGER_JOURNALMODE_WAL) { rc = _sqlite3BtreeSetVersion(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest, int32(2)) } } if rc == SQLITE_OK { /* Set nDestTruncate to the final number of pages in the destination ** database. The complication here is that the destination page ** size may be different to the source page size. ** ** If the source page size is smaller than the destination page size, ** round up. In this case the call to sqlite3OsTruncate() below will ** fix the size of the file. However it is important to call ** sqlite3PagerTruncateImage() here so that any pages in the ** destination file that lie beyond the nDestTruncate page mark are ** journalled by PagerCommitPhaseOne() before they are destroyed ** by the file truncation. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pgszSrc < pgszDest { ratio = pgszDest / pgszSrc nDestTruncate = (nSrcPage + ratio - int32(1)) / ratio if nDestTruncate == int32(uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest)).FpBt)).FpageSize+libc.Uint32FromInt32(1)) { nDestTruncate-- } } else { nDestTruncate = nSrcPage * (pgszSrc / pgszDest) } _ = libc.Int32FromInt32(0) if pgszSrc < pgszDest { /* If the source page-size is smaller than the destination page-size, ** two extra things may need to happen: ** ** * The destination may need to be truncated, and ** ** * Data stored on the pages immediately following the ** pending-byte page in the source database may need to be ** copied into the destination database. */ iSize = int64(pgszSrc) * int64(nSrcPage) pFile = _sqlite3PagerFile(tls, pDestPager) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* This block ensures that all data required to recreate the original ** database has been stored in the journal for pDestPager and the ** journal synced to disk. So at this point we may safely modify ** the database file in any way, knowing that if a power failure ** occurs, the original database will be reconstructed from the ** journal file. */ _sqlite3PagerPagecount(tls, pDestPager, bp+4) iPg = uint32(nDestTruncate) for { if !(rc == SQLITE_OK && iPg <= uint32(*(*int32)(unsafe.Pointer(bp + 4)))) { break } if iPg != uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest)).FpBt)).FpageSize+libc.Uint32FromInt32(1) { rc = _sqlite3PagerGet(tls, pDestPager, iPg, bp+8, 0) if rc == SQLITE_OK { rc = _sqlite3PagerWrite(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) } } goto _4 _4: ; iPg++ } if rc == SQLITE_OK { rc = _sqlite3PagerCommitPhaseOne(tls, pDestPager, uintptr(0), int32(1)) } /* Write the extra pages and truncate the database file as required */ if int64(_sqlite3PendingByte+pgszDest) < iSize { v5 = int64(_sqlite3PendingByte + pgszDest) } else { v5 = iSize } iEnd = v5 iOff = int64(_sqlite3PendingByte + pgszSrc) for { if !(rc == SQLITE_OK && iOff < iEnd) { break } *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) iSrcPg1 = uint32(iOff/int64(pgszSrc) + libc.Int64FromInt32(1)) rc = _sqlite3PagerGet(tls, pSrcPager, iSrcPg1, bp+12, 0) if rc == SQLITE_OK { zData = _sqlite3PagerGetData(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) rc = _sqlite3OsWrite(tls, pFile, zData, pgszSrc, iOff) } _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) goto _6 _6: ; iOff += int64(pgszSrc) } if rc == SQLITE_OK { rc = _backupTruncateFile(tls, pFile, iSize) } /* Sync the database file to disk. */ if rc == SQLITE_OK { rc = _sqlite3PagerSync(tls, pDestPager, uintptr(0)) } } else { _sqlite3PagerTruncateImage(tls, pDestPager, uint32(nDestTruncate)) rc = _sqlite3PagerCommitPhaseOne(tls, pDestPager, uintptr(0), 0) } /* Finish committing the transaction to the destination database. */ if v8 = SQLITE_OK == rc; v8 { v7 = _sqlite3BtreeCommitPhaseTwo(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest, 0) rc = v7 } if v8 && SQLITE_OK == v7 { rc = int32(SQLITE_DONE) } } } /* If bCloseTrans is true, then this function opened a read transaction ** on the source database. Close the read transaction here. There is ** no need to check the return values of the btree methods here, as ** "committing" a read-only transaction cannot fail. */ if bCloseTrans != 0 { _sqlite3BtreeCommitPhaseOne(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc, uintptr(0)) _sqlite3BtreeCommitPhaseTwo(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc, 0) _ = libc.Int32FromInt32(0) } if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<rc) should be set to either SQLITE_DONE ** or an error code. */ Xsqlite3_backup_step(tls, bp, int32(0x7FFFFFFF)) _ = libc.Int32FromInt32(0) rc = Xsqlite3_backup_finish(tls, bp) if rc == SQLITE_OK { p1 = (*TBtree)(unsafe.Pointer(pTo)).FpBt + 24 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(BTS_PAGESIZE_FIXED)) } else { _sqlite3PagerClearCache(tls, _sqlite3BtreePager(tls, (*(*Tsqlite3_backup)(unsafe.Pointer(bp))).FpDest)) } _ = libc.Int32FromInt32(0) goto copy_finished copy_finished: ; _sqlite3BtreeLeave(tls, pFrom) _sqlite3BtreeLeave(tls, pTo) return rc } /************** End of backup.c **********************************************/ /************** Begin file vdbemem.c *****************************************/ /* ** 2004 May 26 ** ** 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 contains code use to manipulate "Mem" structure. A "Mem" ** stores a single value in the VDBE. Mem is an opaque structure visible ** only within the VDBE. Interface routines refer to a Mem using the ** name sqlite_value */ /* #include "sqliteInt.h" */ /* #include "vdbeInt.h" */ /* True if X is a power of two. 0 is considered a power of two here. ** In other words, return true if X has at most one bit set. */ // C documentation // // /* // ** Render a Mem object which is one of MEM_Int, MEM_Real, or MEM_IntReal // ** into a buffer. // */ func _vdbeMemRenderNum(tls *libc.TLS, sz int32, zBuf uintptr, p uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var v1 float64 var _ /* acc at bp+0 */ TStrAccum var _ /* x at bp+24 */ Ti64 _ = v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Int) != 0 { _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, bp+24, p, uint32(int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Int)*int32(2))) (*TMem)(unsafe.Pointer(p)).Fn = _sqlite3Int64ToText(tls, *(*Ti64)(unsafe.Pointer(bp + 24)), zBuf) } else { _sqlite3StrAccumInit(tls, bp, uintptr(0), zBuf, sz, 0) if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_IntReal) != 0 { v1 = float64(*(*Ti64)(unsafe.Pointer(p))) } else { v1 = *(*float64)(unsafe.Pointer(p)) } Xsqlite3_str_appendf(tls, bp, __ccgo_ts+5067, libc.VaList(bp+40, v1)) _ = libc.Int32FromInt32(0) *(*uint8)(unsafe.Pointer(zBuf + uintptr((*(*TStrAccum)(unsafe.Pointer(bp))).FnChar))) = uint8(0) /* Fast version of sqlite3StrAccumFinish(&acc) */ (*TMem)(unsafe.Pointer(p)).Fn = int32((*(*TStrAccum)(unsafe.Pointer(bp))).FnChar) } } // C documentation // // /* // ** If pMem is an object with a valid string representation, this routine // ** ensures the internal encoding for the string representation is // ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE. // ** // ** If pMem is not a string object, or the encoding of the string // ** representation is already stored using the requested encoding, then this // ** routine is a no-op. // ** // ** SQLITE_OK is returned if the conversion is successful (or not required). // ** SQLITE_NOMEM may be returned if a malloc() fails during conversion // ** between formats. // */ func _sqlite3VdbeChangeEncoding(tls *libc.TLS, pMem uintptr, desiredEnc int32) (r int32) { var rc int32 _ = rc _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if !(int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&libc.Int32FromInt32(MEM_Str) != 0) { (*TMem)(unsafe.Pointer(pMem)).Fenc = uint8(desiredEnc) return SQLITE_OK } if int32((*TMem)(unsafe.Pointer(pMem)).Fenc) == desiredEnc { return SQLITE_OK } _ = libc.Int32FromInt32(0) /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned, ** then the encoding of the value may not have changed. */ rc = _sqlite3VdbeMemTranslate(tls, pMem, uint8(desiredEnc)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** Make sure pMem->z points to a writable allocation of at least n bytes. // ** // ** If the bPreserve argument is true, then copy of the content of // ** pMem->z into the new allocation. pMem must be either a string or // ** blob if bPreserve is true. If bPreserve is false, any prior content // ** in pMem->z is discarded. // */ func _sqlite3VdbeMemGrow(tls *libc.TLS, pMem uintptr, n int32, bPreserve int32) (r int32) { var v1, p2 uintptr _, _ = v1, p2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If the bPreserve flag is set to true, then the memory cell must already ** contain a valid string or blob value. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TMem)(unsafe.Pointer(pMem)).FszMalloc > 0 && bPreserve != 0 && (*TMem)(unsafe.Pointer(pMem)).Fz == (*TMem)(unsafe.Pointer(pMem)).FzMalloc { if (*TMem)(unsafe.Pointer(pMem)).Fdb != 0 { v1 = _sqlite3DbReallocOrFree(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).Fz, uint64(n)) (*TMem)(unsafe.Pointer(pMem)).FzMalloc = v1 (*TMem)(unsafe.Pointer(pMem)).Fz = v1 } else { (*TMem)(unsafe.Pointer(pMem)).FzMalloc = _sqlite3Realloc(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, uint64(n)) if (*TMem)(unsafe.Pointer(pMem)).FzMalloc == uintptr(0) { Xsqlite3_free(tls, (*TMem)(unsafe.Pointer(pMem)).Fz) } (*TMem)(unsafe.Pointer(pMem)).Fz = (*TMem)(unsafe.Pointer(pMem)).FzMalloc } bPreserve = 0 } else { if (*TMem)(unsafe.Pointer(pMem)).FszMalloc > 0 { _sqlite3DbFreeNN(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).FzMalloc) } (*TMem)(unsafe.Pointer(pMem)).FzMalloc = _sqlite3DbMallocRaw(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, uint64(n)) } if (*TMem)(unsafe.Pointer(pMem)).FzMalloc == uintptr(0) { _sqlite3VdbeMemSetNull(tls, pMem) (*TMem)(unsafe.Pointer(pMem)).Fz = uintptr(0) (*TMem)(unsafe.Pointer(pMem)).FszMalloc = 0 return int32(SQLITE_NOMEM) } else { (*TMem)(unsafe.Pointer(pMem)).FszMalloc = _sqlite3DbMallocSize(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).FzMalloc) } if bPreserve != 0 && (*TMem)(unsafe.Pointer(pMem)).Fz != 0 { _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TMem)(unsafe.Pointer(pMem)).FzMalloc, (*TMem)(unsafe.Pointer(pMem)).Fz, uint32((*TMem)(unsafe.Pointer(pMem)).Fn)) } if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Dyn) != 0 { _ = libc.Int32FromInt32(0) (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMem)(unsafe.Pointer(pMem)).FxDel})))(tls, (*TMem)(unsafe.Pointer(pMem)).Fz) } (*TMem)(unsafe.Pointer(pMem)).Fz = (*TMem)(unsafe.Pointer(pMem)).FzMalloc p2 = pMem + 16 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) & ^(libc.Int32FromInt32(MEM_Dyn) | libc.Int32FromInt32(MEM_Ephem) | libc.Int32FromInt32(MEM_Static))) return SQLITE_OK } // C documentation // // /* // ** Change the pMem->zMalloc allocation to be at least szNew bytes. // ** If pMem->zMalloc already meets or exceeds the requested size, this // ** routine is a no-op. // ** // ** Any prior string or blob content in the pMem object may be discarded. // ** The pMem->xDel destructor is called, if it exists. Though MEM_Str // ** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, MEM_IntReal, // ** and MEM_Null values are preserved. // ** // ** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM) // ** if unable to complete the resizing. // */ func _sqlite3VdbeMemClearAndResize(tls *libc.TLS, pMem uintptr, szNew int32) (r int32) { var p1 uintptr _ = p1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TMem)(unsafe.Pointer(pMem)).FszMalloc < szNew { return _sqlite3VdbeMemGrow(tls, pMem, szNew, 0) } _ = libc.Int32FromInt32(0) (*TMem)(unsafe.Pointer(pMem)).Fz = (*TMem)(unsafe.Pointer(pMem)).FzMalloc p1 = pMem + 16 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & (libc.Int32FromInt32(MEM_Null) | libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_Real) | libc.Int32FromInt32(MEM_IntReal))) return SQLITE_OK } // C documentation // // /* // ** If pMem is already a string, detect if it is a zero-terminated // ** string, or make it into one if possible, and mark it as such. // ** // ** This is an optimization. Correct operation continues even if // ** this routine is a no-op. // */ func _sqlite3VdbeMemZeroTerminateIfAble(tls *libc.TLS, pMem uintptr) { var p1, p2, p3 uintptr _, _, _ = p1, p2, p3 if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Term)|libc.Int32FromInt32(MEM_Ephem)|libc.Int32FromInt32(MEM_Static)) != int32(MEM_Str) { /* pMem must be a string, and it cannot be an ephemeral or static string */ return } if int32((*TMem)(unsafe.Pointer(pMem)).Fenc) != int32(SQLITE_UTF8) { return } if (*TMem)(unsafe.Pointer(pMem)).Fz == uintptr(0) { return } if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Dyn) != 0 { if (*TMem)(unsafe.Pointer(pMem)).FxDel == __ccgo_fp(Xsqlite3_free) && Xsqlite3_msize(tls, (*TMem)(unsafe.Pointer(pMem)).Fz) >= uint64((*TMem)(unsafe.Pointer(pMem)).Fn+libc.Int32FromInt32(1)) { *(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn))) = uint8(0) p1 = pMem + 16 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(MEM_Term)) return } if (*TMem)(unsafe.Pointer(pMem)).FxDel == __ccgo_fp(_sqlite3RCStrUnref) { /* Blindly assume that all RCStr objects are zero-terminated */ p2 = pMem + 16 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(MEM_Term)) return } } else { if (*TMem)(unsafe.Pointer(pMem)).FszMalloc >= (*TMem)(unsafe.Pointer(pMem)).Fn+int32(1) { *(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn))) = uint8(0) p3 = pMem + 16 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) | libc.Int32FromInt32(MEM_Term)) return } } } // C documentation // // /* // ** It is already known that pMem contains an unterminated string. // ** Add the zero terminator. // ** // ** Three bytes of zero are added. In this way, there is guaranteed // ** to be a double-zero byte at an even byte boundary in order to // ** terminate a UTF16 string, even if the initial size of the buffer // ** is an odd number of bytes. // */ func _vdbeMemAddTerminator(tls *libc.TLS, pMem uintptr) (r int32) { var p1 uintptr _ = p1 if _sqlite3VdbeMemGrow(tls, pMem, (*TMem)(unsafe.Pointer(pMem)).Fn+int32(3), int32(1)) != 0 { return int32(SQLITE_NOMEM) } *(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn))) = uint8(0) *(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn+int32(1)))) = uint8(0) *(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn+int32(2)))) = uint8(0) p1 = pMem + 16 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(MEM_Term)) return SQLITE_OK } // C documentation // // /* // ** Change pMem so that its MEM_Str or MEM_Blob value is stored in // ** MEM.zMalloc, where it can be safely written. // ** // ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails. // */ func _sqlite3VdbeMemMakeWriteable(tls *libc.TLS, pMem uintptr) (r int32) { var rc, v1 int32 var p2 uintptr _, _, _ = rc, v1, p2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) != 0 { if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Zero) != 0 { v1 = _sqlite3VdbeMemExpandBlob(tls, pMem) } else { v1 = 0 } if v1 != 0 { return int32(SQLITE_NOMEM) } if (*TMem)(unsafe.Pointer(pMem)).FszMalloc == 0 || (*TMem)(unsafe.Pointer(pMem)).Fz != (*TMem)(unsafe.Pointer(pMem)).FzMalloc { rc = _vdbeMemAddTerminator(tls, pMem) if rc != 0 { return rc } } } p2 = pMem + 16 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) & ^libc.Int32FromInt32(MEM_Ephem)) return SQLITE_OK } // C documentation // // /* // ** If the given Mem* has a zero-filled tail, turn it into an ordinary // ** blob stored in dynamically allocated space. // */ func _sqlite3VdbeMemExpandBlob(tls *libc.TLS, pMem uintptr) (r int32) { var nByte int32 var p1 uintptr _, _ = nByte, p1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Set nByte to the number of bytes required to store the expanded blob. */ nByte = (*TMem)(unsafe.Pointer(pMem)).Fn + *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pMem)).Fu)) if nByte <= 0 { if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Blob) == 0 { return SQLITE_OK } nByte = int32(1) } if _sqlite3VdbeMemGrow(tls, pMem, nByte, int32(1)) != 0 { return int32(SQLITE_NOMEM) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) libc.Xmemset(tls, (*TMem)(unsafe.Pointer(pMem)).Fz+uintptr((*TMem)(unsafe.Pointer(pMem)).Fn), 0, uint32(*(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pMem)).Fu)))) *(*int32)(unsafe.Pointer(pMem + 12)) += *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pMem)).Fu)) p1 = pMem + 16 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^(libc.Int32FromInt32(MEM_Zero) | libc.Int32FromInt32(MEM_Term))) return SQLITE_OK } // C documentation // // /* // ** Make sure the given Mem is \u0000 terminated. // */ func _sqlite3VdbeMemNulTerminate(tls *libc.TLS, pMem uintptr) (r int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Term)|libc.Int32FromInt32(MEM_Str)) != int32(MEM_Str) { return SQLITE_OK /* Nothing to do */ } else { return _vdbeMemAddTerminator(tls, pMem) } return r } // C documentation // // /* // ** Add MEM_Str to the set of representations for the given Mem. This // ** routine is only called if pMem is a number of some kind, not a NULL // ** or a BLOB. // ** // ** Existing representations MEM_Int, MEM_Real, or MEM_IntReal are invalidated // ** if bForce is true but are retained if bForce is false. // ** // ** A MEM_Null value will never be passed to this function. This function is // ** used for converting values to text for returning to the user (i.e. via // ** sqlite3_value_text()), or for ensuring that values to be used as btree // ** keys are strings. In the former case a NULL pointer is returned the // ** user and the latter is an internal programming error. // */ func _sqlite3VdbeMemStringify(tls *libc.TLS, pMem uintptr, enc Tu8, bForce Tu8) (r int32) { var nByte int32 var p1, p2 uintptr _, _, _ = nByte, p1, p2 nByte = int32(32) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if _sqlite3VdbeMemClearAndResize(tls, pMem, nByte) != 0 { (*TMem)(unsafe.Pointer(pMem)).Fenc = uint8(0) return int32(SQLITE_NOMEM) } _vdbeMemRenderNum(tls, nByte, (*TMem)(unsafe.Pointer(pMem)).Fz, pMem) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TMem)(unsafe.Pointer(pMem)).Fenc = uint8(SQLITE_UTF8) p1 = pMem + 16 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | (libc.Int32FromInt32(MEM_Str) | libc.Int32FromInt32(MEM_Term))) if bForce != 0 { p2 = pMem + 16 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) & ^(libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_Real) | libc.Int32FromInt32(MEM_IntReal))) } _sqlite3VdbeChangeEncoding(tls, pMem, int32(enc)) return SQLITE_OK } // C documentation // // /* // ** Memory cell pMem contains the context of an aggregate function. // ** This routine calls the finalize method for that function. The // ** result of the aggregate is stored back into pMem. // ** // ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK // ** otherwise. // */ func _sqlite3VdbeMemFinalize(tls *libc.TLS, pMem uintptr, pFunc uintptr) (r int32) { bp := tls.Alloc(80) defer tls.Free(80) var _ /* ctx at bp+0 */ Tsqlite3_context var _ /* t at bp+32 */ TMem _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) libc.Xmemset(tls, bp, 0, uint32(32)) libc.Xmemset(tls, bp+32, 0, uint32(40)) (*(*TMem)(unsafe.Pointer(bp + 32))).Fflags = uint16(MEM_Null) (*(*TMem)(unsafe.Pointer(bp + 32))).Fdb = (*TMem)(unsafe.Pointer(pMem)).Fdb (*(*Tsqlite3_context)(unsafe.Pointer(bp))).FpOut = bp + 32 (*(*Tsqlite3_context)(unsafe.Pointer(bp))).FpMem = pMem (*(*Tsqlite3_context)(unsafe.Pointer(bp))).FpFunc = pFunc (*(*Tsqlite3_context)(unsafe.Pointer(bp))).Fenc = (*Tsqlite3)(unsafe.Pointer((*(*TMem)(unsafe.Pointer(bp + 32))).Fdb)).Fenc (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFuncDef)(unsafe.Pointer(pFunc)).FxFinalize})))(tls, bp) /* IMP: R-24505-23230 */ _ = libc.Int32FromInt32(0) if (*TMem)(unsafe.Pointer(pMem)).FszMalloc > 0 { _sqlite3DbFreeNN(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).FzMalloc) } libc.Xmemcpy(tls, pMem, bp+32, uint32(40)) return (*(*Tsqlite3_context)(unsafe.Pointer(bp))).FisError } // C documentation // // /* // ** Memory cell pAccum contains the context of an aggregate function. // ** This routine calls the xValue method for that function and stores // ** the results in memory cell pMem. // ** // ** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK // ** otherwise. // */ func _sqlite3VdbeMemAggValue(tls *libc.TLS, pAccum uintptr, pOut uintptr, pFunc uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* ctx at bp+0 */ Tsqlite3_context _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) libc.Xmemset(tls, bp, 0, uint32(32)) _sqlite3VdbeMemSetNull(tls, pOut) (*(*Tsqlite3_context)(unsafe.Pointer(bp))).FpOut = pOut (*(*Tsqlite3_context)(unsafe.Pointer(bp))).FpMem = pAccum (*(*Tsqlite3_context)(unsafe.Pointer(bp))).FpFunc = pFunc (*(*Tsqlite3_context)(unsafe.Pointer(bp))).Fenc = (*Tsqlite3)(unsafe.Pointer((*TMem)(unsafe.Pointer(pAccum)).Fdb)).Fenc (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFuncDef)(unsafe.Pointer(pFunc)).FxValue})))(tls, bp) return (*(*Tsqlite3_context)(unsafe.Pointer(bp))).FisError } // C documentation // // /* // ** If the memory cell contains a value that must be freed by // ** invoking the external callback in Mem.xDel, then this routine // ** will free that value. It also sets Mem.flags to MEM_Null. // ** // ** This is a helper routine for sqlite3VdbeMemSetNull() and // ** for sqlite3VdbeMemRelease(). Use those other routines as the // ** entry point for releasing Mem resources. // */ func _vdbeMemClearExternAndSetNull(tls *libc.TLS, p uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Agg) != 0 { _sqlite3VdbeMemFinalize(tls, p, *(*uintptr)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(p)).Fu))) _ = libc.Int32FromInt32(0) } if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Dyn) != 0 { _ = libc.Int32FromInt32(0) (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMem)(unsafe.Pointer(p)).FxDel})))(tls, (*TMem)(unsafe.Pointer(p)).Fz) } (*TMem)(unsafe.Pointer(p)).Fflags = uint16(MEM_Null) } // C documentation // // /* // ** Release memory held by the Mem p, both external memory cleared // ** by p->xDel and memory in p->zMalloc. // ** // ** This is a helper routine invoked by sqlite3VdbeMemRelease() in // ** the unusual case where there really is memory in p that needs // ** to be freed. // */ func _vdbeMemClear(tls *libc.TLS, p uintptr) { if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 { _vdbeMemClearExternAndSetNull(tls, p) } if (*TMem)(unsafe.Pointer(p)).FszMalloc != 0 { _sqlite3DbFreeNN(tls, (*TMem)(unsafe.Pointer(p)).Fdb, (*TMem)(unsafe.Pointer(p)).FzMalloc) (*TMem)(unsafe.Pointer(p)).FszMalloc = 0 } (*TMem)(unsafe.Pointer(p)).Fz = uintptr(0) } // C documentation // // /* // ** Release any memory resources held by the Mem. Both the memory that is // ** free by Mem.xDel and the Mem.zMalloc allocation are freed. // ** // ** Use this routine prior to clean up prior to abandoning a Mem, or to // ** reset a Mem back to its minimum memory utilization. // ** // ** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space // ** prior to inserting new content into the Mem. // */ func _sqlite3VdbeMemRelease(tls *libc.TLS, p uintptr) { _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 || (*TMem)(unsafe.Pointer(p)).FszMalloc != 0 { _vdbeMemClear(tls, p) } } // C documentation // // /* Like sqlite3VdbeMemRelease() but faster for cases where we // ** know in advance that the Mem is not MEM_Dyn or MEM_Agg. // */ func _sqlite3VdbeMemReleaseMalloc(tls *libc.TLS, p uintptr) { _ = libc.Int32FromInt32(0) if (*TMem)(unsafe.Pointer(p)).FszMalloc != 0 { _vdbeMemClear(tls, p) } } // C documentation // // /* // ** Return some kind of integer value which is the best we can do // ** at representing the value that *pMem describes as an integer. // ** If pMem is an integer, then the value is exact. If pMem is // ** a floating-point then the value returned is the integer part. // ** If pMem is a string or blob, then we make an attempt to convert // ** it into an integer and return that. If pMem represents an // ** an SQL-NULL value, return 0. // ** // ** If pMem represents a string value, its encoding might be changed. // */ func _memIntValue(tls *libc.TLS, pMem uintptr) (r Ti64) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* value at bp+0 */ Ti64 *(*Ti64)(unsafe.Pointer(bp)) = 0 _sqlite3Atoi64(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, bp, (*TMem)(unsafe.Pointer(pMem)).Fn, (*TMem)(unsafe.Pointer(pMem)).Fenc) return *(*Ti64)(unsafe.Pointer(bp)) } func _sqlite3VdbeIntValue(tls *libc.TLS, pMem uintptr) (r Ti64) { var flags int32 _ = flags _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) flags = int32((*TMem)(unsafe.Pointer(pMem)).Fflags) if flags&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 { return *(*Ti64)(unsafe.Pointer(pMem)) } else { if flags&int32(MEM_Real) != 0 { return _sqlite3RealToI64(tls, *(*float64)(unsafe.Pointer(pMem))) } else { if flags&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) != 0 && (*TMem)(unsafe.Pointer(pMem)).Fz != uintptr(0) { return _memIntValue(tls, pMem) } else { return 0 } } } return r } // C documentation // // /* // ** Return the best representation of pMem that we can get into a // ** double. If pMem is already a double or an integer, return its // ** value. If it is a string or blob, try to convert it to a double. // ** If it is a NULL, return 0.0. // */ func _memRealValue(tls *libc.TLS, pMem uintptr) (r float64) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* val at bp+0 */ float64 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ *(*float64)(unsafe.Pointer(bp)) = libc.Float64FromInt32(0) _sqlite3AtoF(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, bp, (*TMem)(unsafe.Pointer(pMem)).Fn, (*TMem)(unsafe.Pointer(pMem)).Fenc) return *(*float64)(unsafe.Pointer(bp)) } func _sqlite3VdbeRealValue(tls *libc.TLS, pMem uintptr) (r float64) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Real) != 0 { return *(*float64)(unsafe.Pointer(pMem)) } else { if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 { return float64(*(*Ti64)(unsafe.Pointer(pMem))) } else { if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) != 0 { return _memRealValue(tls, pMem) } else { /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ return libc.Float64FromInt32(0) } } } return r } // C documentation // // /* // ** Return 1 if pMem represents true, and return 0 if pMem represents false. // ** Return the value ifNull if pMem is NULL. // */ func _sqlite3VdbeBooleanValue(tls *libc.TLS, pMem uintptr, ifNull int32) (r int32) { if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 { return libc.BoolInt32(*(*Ti64)(unsafe.Pointer(pMem)) != 0) } if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Null) != 0 { return ifNull } return libc.BoolInt32(_sqlite3VdbeRealValue(tls, pMem) != float64(0)) } // C documentation // // /* // ** The MEM structure is already a MEM_Real or MEM_IntReal. Try to // ** make it a MEM_Int if we can. // */ func _sqlite3VdbeIntegerAffinity(tls *libc.TLS, pMem uintptr) { var ix Ti64 _ = ix _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_IntReal) != 0 { (*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pMem)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Int)) } else { ix = _sqlite3RealToI64(tls, *(*float64)(unsafe.Pointer(pMem))) /* Only mark the value as an integer if ** ** (1) the round-trip conversion real->int->real is a no-op, and ** (2) The integer is neither the largest nor the smallest ** possible integer (ticket #3922) ** ** The second and third terms in the following conditional enforces ** the second condition under the assumption that addition overflow causes ** values to wrap around. */ if *(*float64)(unsafe.Pointer(pMem)) == float64(ix) && ix > int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<= -int64(2251799813685248) && i < int64(2251799813685248)) } // C documentation // // /* Convert a floating point value to its closest integer. Do so in // ** a way that avoids 'outside the range of representable values' warnings // ** from UBSAN. // */ func _sqlite3RealToI64(tls *libc.TLS, r float64) (r1 Ti64) { if r < -libc.Float64FromFloat64(9.223372036854775e+18) { return int64(-libc.Int32FromInt32(1)) - (libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)< +libc.Float64FromFloat64(9.223372036854775e+18) { return libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)<>libc.Int32FromInt32(3)) _sqlite3ValueApplyAffinity(tls, pMem, uint8(SQLITE_AFF_TEXT), encoding) _ = libc.Int32FromInt32(0) p3 = pMem + 16 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) & ^(libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_Real) | libc.Int32FromInt32(MEM_IntReal) | libc.Int32FromInt32(MEM_Blob) | libc.Int32FromInt32(MEM_Zero))) if int32(encoding) != int32(SQLITE_UTF8) { *(*int32)(unsafe.Pointer(pMem + 12)) &= ^libc.Int32FromInt32(1) } rc = _sqlite3VdbeChangeEncoding(tls, pMem, int32(encoding)) if rc != 0 { return rc } _sqlite3VdbeMemZeroTerminateIfAble(tls, pMem) } return SQLITE_OK } // C documentation // // /* // ** Initialize bulk memory to be a consistent Mem object. // ** // ** The minimum amount of initialization feasible is performed. // */ func _sqlite3VdbeMemInit(tls *libc.TLS, pMem uintptr, db uintptr, flags Tu16) { _ = libc.Int32FromInt32(0) (*TMem)(unsafe.Pointer(pMem)).Fflags = flags (*TMem)(unsafe.Pointer(pMem)).Fdb = db (*TMem)(unsafe.Pointer(pMem)).FszMalloc = 0 } // C documentation // // /* // ** Delete any previous value and set the value stored in *pMem to NULL. // ** // ** This routine calls the Mem.xDel destructor to dispose of values that // ** require the destructor. But it preserves the Mem.zMalloc memory allocation. // ** To free all resources, use sqlite3VdbeMemRelease(), which both calls this // ** routine to invoke the destructor and deallocates Mem.zMalloc. // ** // ** Use this routine to reset the Mem prior to insert a new value. // ** // ** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it. // */ func _sqlite3VdbeMemSetNull(tls *libc.TLS, pMem uintptr) { if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 { _vdbeMemClearExternAndSetNull(tls, pMem) } else { (*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(MEM_Null) } } func _sqlite3ValueSetNull(tls *libc.TLS, p uintptr) { _sqlite3VdbeMemSetNull(tls, p) } // C documentation // // /* // ** Delete any previous value and set the value to be a BLOB of length // ** n containing all zeros. // */ func _sqlite3VdbeMemSetZeroBlob(tls *libc.TLS, pMem uintptr, n int32) { _sqlite3VdbeMemRelease(tls, pMem) (*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(libc.Int32FromInt32(MEM_Blob) | libc.Int32FromInt32(MEM_Zero)) (*TMem)(unsafe.Pointer(pMem)).Fn = 0 if n < 0 { n = 0 } *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pMem)).Fu)) = n (*TMem)(unsafe.Pointer(pMem)).Fenc = uint8(SQLITE_UTF8) (*TMem)(unsafe.Pointer(pMem)).Fz = uintptr(0) } // C documentation // // /* // ** The pMem is known to contain content that needs to be destroyed prior // ** to a value change. So invoke the destructor, then set the value to // ** a 64-bit integer. // */ func _vdbeReleaseAndSetInt64(tls *libc.TLS, pMem uintptr, val Ti64) { _sqlite3VdbeMemSetNull(tls, pMem) *(*Ti64)(unsafe.Pointer(pMem)) = val (*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(MEM_Int) } // C documentation // // /* // ** Delete any previous value and set the value stored in *pMem to val, // ** manifest type INTEGER. // */ func _sqlite3VdbeMemSetInt64(tls *libc.TLS, pMem uintptr, val Ti64) { if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 { _vdbeReleaseAndSetInt64(tls, pMem, val) } else { *(*Ti64)(unsafe.Pointer(pMem)) = val (*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(MEM_Int) } } // C documentation // // /* A no-op destructor */ func _sqlite3NoopDestructor(tls *libc.TLS, p uintptr) { _ = p } // C documentation // // /* // ** Set the value stored in *pMem should already be a NULL. // ** Also store a pointer to go with it. // */ func _sqlite3VdbeMemSetPointer(tls *libc.TLS, pMem uintptr, pPtr uintptr, zPType uintptr, xDestructor uintptr) { var v1, v2 uintptr _, _ = v1, v2 _ = libc.Int32FromInt32(0) _vdbeMemClear(tls, pMem) if zPType != 0 { v1 = zPType } else { v1 = __ccgo_ts + 1648 } *(*uintptr)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pMem)).Fu)) = v1 (*TMem)(unsafe.Pointer(pMem)).Fz = pPtr (*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(libc.Int32FromInt32(MEM_Null) | libc.Int32FromInt32(MEM_Dyn) | libc.Int32FromInt32(MEM_Subtype) | libc.Int32FromInt32(MEM_Term)) (*TMem)(unsafe.Pointer(pMem)).FeSubtype = uint8('p') if xDestructor != 0 { v2 = xDestructor } else { v2 = __ccgo_fp(_sqlite3NoopDestructor) } (*TMem)(unsafe.Pointer(pMem)).FxDel = v2 } // C documentation // // /* // ** Delete any previous value and set the value stored in *pMem to val, // ** manifest type REAL. // */ func _sqlite3VdbeMemSetDouble(tls *libc.TLS, pMem uintptr, val float64) { _sqlite3VdbeMemSetNull(tls, pMem) if !(_sqlite3IsNaN(tls, val) != 0) { *(*float64)(unsafe.Pointer(pMem)) = val (*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(MEM_Real) } } // C documentation // // /* // ** Delete any previous value and set the value of pMem to be an // ** empty boolean index. // ** // ** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation // ** error occurs. // */ func _sqlite3VdbeMemSetRowSet(tls *libc.TLS, pMem uintptr) (r int32) { var db, p uintptr _, _ = db, p db = (*TMem)(unsafe.Pointer(pMem)).Fdb _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeMemRelease(tls, pMem) p = _sqlite3RowSetInit(tls, db) if p == uintptr(0) { return int32(SQLITE_NOMEM) } (*TMem)(unsafe.Pointer(pMem)).Fz = p (*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(libc.Int32FromInt32(MEM_Blob) | libc.Int32FromInt32(MEM_Dyn)) (*TMem)(unsafe.Pointer(pMem)).FxDel = __ccgo_fp(_sqlite3RowSetDelete) return SQLITE_OK } // C documentation // // /* // ** Return true if the Mem object contains a TEXT or BLOB that is // ** too large - whose size exceeds SQLITE_MAX_LENGTH. // */ func _sqlite3VdbeMemTooBig(tls *libc.TLS, p uintptr) (r int32) { var n int32 _ = n _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) != 0 { n = (*TMem)(unsafe.Pointer(p)).Fn if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Zero) != 0 { n += *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(p)).Fu)) } return libc.BoolInt32(n > *(*int32)(unsafe.Pointer((*TMem)(unsafe.Pointer(p)).Fdb + 120))) } return 0 } // C documentation // // /* // ** Make an shallow copy of pFrom into pTo. Prior contents of // ** pTo are freed. The pFrom->z field is not duplicated. If // ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z // ** and flags gets srcType (either MEM_Ephem or MEM_Static). // */ func _vdbeClrCopy(tls *libc.TLS, pTo uintptr, pFrom uintptr, eType int32) { _vdbeMemClearExternAndSetNull(tls, pTo) _ = libc.Int32FromInt32(0) _sqlite3VdbeMemShallowCopy(tls, pTo, pFrom, eType) } func _sqlite3VdbeMemShallowCopy(tls *libc.TLS, pTo uintptr, pFrom uintptr, srcType int32) { var p1, p2 uintptr _, _ = p1, p2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pTo)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 { _vdbeClrCopy(tls, pTo, pFrom, srcType) return } libc.Xmemcpy(tls, pTo, pFrom, uint32(libc.UintptrFromInt32(0)+20)) if int32((*TMem)(unsafe.Pointer(pFrom)).Fflags)&int32(MEM_Static) == 0 { p1 = pTo + 16 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^(libc.Int32FromInt32(MEM_Dyn) | libc.Int32FromInt32(MEM_Static) | libc.Int32FromInt32(MEM_Ephem))) _ = libc.Int32FromInt32(0) p2 = pTo + 16 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | srcType) } } // C documentation // // /* // ** Make a full copy of pFrom into pTo. Prior contents of pTo are // ** freed before the copy is made. // */ func _sqlite3VdbeMemCopy(tls *libc.TLS, pTo uintptr, pFrom uintptr) (r int32) { var rc int32 var p1, p2 uintptr _, _, _ = rc, p1, p2 rc = SQLITE_OK _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pTo)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 { _vdbeMemClearExternAndSetNull(tls, pTo) } libc.Xmemcpy(tls, pTo, pFrom, uint32(libc.UintptrFromInt32(0)+20)) p1 = pTo + 16 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(MEM_Dyn)) if int32((*TMem)(unsafe.Pointer(pTo)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) != 0 { if 0 == int32((*TMem)(unsafe.Pointer(pFrom)).Fflags)&int32(MEM_Static) { p2 = pTo + 16 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(MEM_Ephem)) rc = _sqlite3VdbeMemMakeWriteable(tls, pTo) } } return rc } // C documentation // // /* // ** Transfer the contents of pFrom to pTo. Any existing value in pTo is // ** freed. If pFrom contains ephemeral data, a copy is made. // ** // ** pFrom contains an SQL NULL when this routine returns. // */ func _sqlite3VdbeMemMove(tls *libc.TLS, pTo uintptr, pFrom uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeMemRelease(tls, pTo) libc.Xmemcpy(tls, pTo, pFrom, uint32(40)) (*TMem)(unsafe.Pointer(pFrom)).Fflags = uint16(MEM_Null) (*TMem)(unsafe.Pointer(pFrom)).FszMalloc = 0 } // C documentation // // /* // ** Change the value of a Mem to be a string or a BLOB. // ** // ** The memory management strategy depends on the value of the xDel // ** parameter. If the value passed is SQLITE_TRANSIENT, then the // ** string is copied into a (possibly existing) buffer managed by the // ** Mem structure. Otherwise, any existing buffer is freed and the // ** pointer copied. // ** // ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH // ** size limit) then no memory allocation occurs. If the string can be // ** stored without allocating memory, then it is. If a memory allocation // ** is required to store the string, then value of pMem is unchanged. In // ** either case, SQLITE_TOOBIG is returned. // ** // ** The "enc" parameter is the text encoding for the string, or zero // ** to store a blob. // ** // ** If n is negative, then the string consists of all bytes up to but // ** excluding the first zero character. The n parameter must be // ** non-negative for blobs. // */ func _sqlite3VdbeMemSetStr(tls *libc.TLS, pMem uintptr, z uintptr, n Ti64, enc Tu8, xDel uintptr) (r int32) { var flags Tu16 var iLimit, v2, v4 int32 var nAlloc, nByte Ti64 var v3 int64 _, _, _, _, _, _, _ = flags, iLimit, nAlloc, nByte, v2, v3, v4 nByte = n /* New value for pMem->flags */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If z is a NULL pointer, set pMem to contain an SQL NULL. */ if !(z != 0) { _sqlite3VdbeMemSetNull(tls, pMem) return SQLITE_OK } if (*TMem)(unsafe.Pointer(pMem)).Fdb != 0 { iLimit = *(*int32)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fdb + 120)) } else { iLimit = int32(SQLITE_MAX_LENGTH) } if nByte < 0 { _ = libc.Int32FromInt32(0) if int32(enc) == int32(SQLITE_UTF8) { nByte = int64(libc.Xstrlen(tls, z)) } else { nByte = 0 for { if !(nByte <= int64(iLimit) && int32(*(*uint8)(unsafe.Pointer(z + uintptr(nByte))))|int32(*(*uint8)(unsafe.Pointer(z + uintptr(nByte+int64(1))))) != 0) { break } goto _1 _1: ; nByte += int64(2) } } flags = uint16(libc.Int32FromInt32(MEM_Str) | libc.Int32FromInt32(MEM_Term)) } else { if int32(enc) == 0 { flags = uint16(MEM_Blob) enc = uint8(SQLITE_UTF8) } else { flags = uint16(MEM_Str) } } if nByte > int64(iLimit) { if xDel != 0 && xDel != uintptr(-libc.Int32FromInt32(1)) { if xDel == __ccgo_fp(_sqlite3OomClear) { _sqlite3DbFree(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, z) } else { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{xDel})))(tls, z) } } _sqlite3VdbeMemSetNull(tls, pMem) return _sqlite3ErrorToParser(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, int32(SQLITE_TOOBIG)) } /* The following block sets the new values of Mem.z and Mem.xDel. It ** also sets a flag in local variable "flags" to indicate the memory ** management (one of MEM_Dyn or MEM_Static). */ if xDel == uintptr(-libc.Int32FromInt32(1)) { nAlloc = nByte if int32(flags)&int32(MEM_Term) != 0 { if int32(enc) == int32(SQLITE_UTF8) { v2 = int32(1) } else { v2 = int32(2) } nAlloc += int64(v2) } if nAlloc > int64(libc.Int32FromInt32(32)) { v3 = nAlloc } else { v3 = int64(libc.Int32FromInt32(32)) } if _sqlite3VdbeMemClearAndResize(tls, pMem, int32(v3)) != 0 { return int32(SQLITE_NOMEM) } libc.Xmemcpy(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, z, uint32(nAlloc)) } else { _sqlite3VdbeMemRelease(tls, pMem) (*TMem)(unsafe.Pointer(pMem)).Fz = z if xDel == __ccgo_fp(_sqlite3OomClear) { (*TMem)(unsafe.Pointer(pMem)).FzMalloc = (*TMem)(unsafe.Pointer(pMem)).Fz (*TMem)(unsafe.Pointer(pMem)).FszMalloc = _sqlite3DbMallocSize(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).FzMalloc) } else { (*TMem)(unsafe.Pointer(pMem)).FxDel = xDel if xDel == libc.UintptrFromInt32(0) { v4 = int32(MEM_Static) } else { v4 = int32(MEM_Dyn) } flags = Tu16(int32(flags) | v4) } } (*TMem)(unsafe.Pointer(pMem)).Fn = int32(nByte & libc.Int64FromInt32(0x7fffffff)) (*TMem)(unsafe.Pointer(pMem)).Fflags = flags (*TMem)(unsafe.Pointer(pMem)).Fenc = enc if int32(enc) > int32(SQLITE_UTF8) && _sqlite3VdbeMemHandleBom(tls, pMem) != 0 { return int32(SQLITE_NOMEM) } return SQLITE_OK } // C documentation // // /* // ** Move data out of a btree key or data field and into a Mem structure. // ** The data is payload from the entry that pCur is currently pointing // ** to. offset and amt determine what portion of the data or key to retrieve. // ** The result is written into the pMem element. // ** // ** The pMem object must have been initialized. This routine will use // ** pMem->zMalloc to hold the content from the btree, if possible. New // ** pMem->zMalloc space will be allocated if necessary. The calling routine // ** is responsible for making sure that the pMem object is eventually // ** destroyed. // ** // ** If this routine fails for any reason (malloc returns NULL or unable // ** to read from the disk) then the pMem is left in an inconsistent state. // */ func _sqlite3VdbeMemFromBtree(tls *libc.TLS, pCur uintptr, offset Tu32, amt Tu32, pMem uintptr) (r int32) { var rc, v1 int32 _, _ = rc, v1 (*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(MEM_Null) if _sqlite3BtreeMaxRecordSize(tls, pCur) < int64(offset+amt) { return _sqlite3CorruptError(tls, int32(83638)) } v1 = _sqlite3VdbeMemClearAndResize(tls, pMem, int32(amt+uint32(1))) rc = v1 if SQLITE_OK == v1 { rc = _sqlite3BtreePayload(tls, pCur, offset, amt, (*TMem)(unsafe.Pointer(pMem)).Fz) if rc == SQLITE_OK { *(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr(amt))) = uint8(0) /* Overrun area used when reading malformed records */ (*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(MEM_Blob) (*TMem)(unsafe.Pointer(pMem)).Fn = int32(amt) } else { _sqlite3VdbeMemRelease(tls, pMem) } } return rc } func _sqlite3VdbeMemFromBtreeZeroOffset(tls *libc.TLS, pCur uintptr, amt Tu32, pMem uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* available at bp+0 */ Tu32 _ = rc *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) /* Number of bytes available on the local btree page */ rc = SQLITE_OK /* Return code */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() ** that both the BtShared and database handle mutexes are held. */ _ = libc.Int32FromInt32(0) (*TMem)(unsafe.Pointer(pMem)).Fz = _sqlite3BtreePayloadFetch(tls, pCur, bp) _ = libc.Int32FromInt32(0) if amt <= *(*Tu32)(unsafe.Pointer(bp)) { (*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(libc.Int32FromInt32(MEM_Blob) | libc.Int32FromInt32(MEM_Ephem)) (*TMem)(unsafe.Pointer(pMem)).Fn = int32(amt) } else { rc = _sqlite3VdbeMemFromBtree(tls, pCur, uint32(0), amt, pMem) } return rc } // C documentation // // /* // ** The pVal argument is known to be a value other than NULL. // ** Convert it into a string with encoding enc and return a pointer // ** to a zero-terminated version of that string. // */ func _valueToText(tls *libc.TLS, pVal uintptr, enc Tu8) (r uintptr) { var v1 int32 var p2 uintptr _, _ = v1, p2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fflags)&(libc.Int32FromInt32(MEM_Blob)|libc.Int32FromInt32(MEM_Str)) != 0 { if int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fflags)&int32(MEM_Zero) != 0 { v1 = _sqlite3VdbeMemExpandBlob(tls, pVal) } else { v1 = 0 } if v1 != 0 { return uintptr(0) } p2 = pVal + 16 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(MEM_Str)) if int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fenc) != int32(enc) & ^libc.Int32FromInt32(SQLITE_UTF16_ALIGNED) { _sqlite3VdbeChangeEncoding(tls, pVal, int32(enc) & ^libc.Int32FromInt32(SQLITE_UTF16_ALIGNED)) } if int32(enc)&int32(SQLITE_UTF16_ALIGNED) != 0 && int32(1) == int32(1)&int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fz) { _ = libc.Int32FromInt32(0) if _sqlite3VdbeMemMakeWriteable(tls, pVal) != SQLITE_OK { return uintptr(0) } } _sqlite3VdbeMemNulTerminate(tls, pVal) /* IMP: R-31275-44060 */ } else { _sqlite3VdbeMemStringify(tls, pVal, enc, uint8(0)) _ = libc.Int32FromInt32(0) } _ = libc.Int32FromInt32(0) if int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fenc) == int32(enc) & ^libc.Int32FromInt32(SQLITE_UTF16_ALIGNED) { _ = libc.Int32FromInt32(0) return (*Tsqlite3_value)(unsafe.Pointer(pVal)).Fz } else { return uintptr(0) } return r } // C documentation // // /* This function is only available internally, it is not part of the // ** external API. It works in a similar way to sqlite3_value_text(), // ** except the data returned is in the encoding specified by the second // ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or // ** SQLITE_UTF8. // ** // ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED. // ** If that is the case, then the result must be aligned on an even byte // ** boundary. // */ func _sqlite3ValueText(tls *libc.TLS, pVal uintptr, enc Tu8) (r uintptr) { if !(pVal != 0) { return uintptr(0) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Term)) == libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Term) && int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fenc) == int32(enc) { _ = libc.Int32FromInt32(0) return (*Tsqlite3_value)(unsafe.Pointer(pVal)).Fz } if int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fflags)&int32(MEM_Null) != 0 { return uintptr(0) } return _valueToText(tls, pVal, enc) } // C documentation // // /* Return true if sqlit3_value object pVal is a string or blob value // ** that uses the destructor specified in the second argument. // ** // ** TODO: Maybe someday promote this interface into a published API so // ** that third-party extensions can get access to it? // */ func _sqlite3ValueIsOfClass(tls *libc.TLS, pVal uintptr, xFree uintptr) (r int32) { if pVal != uintptr(0) && int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) != 0 && int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fflags)&int32(MEM_Dyn) != 0 && (*Tsqlite3_value)(unsafe.Pointer(pVal)).FxDel == xFree { return int32(1) } else { return 0 } return r } // C documentation // // /* // ** Create a new sqlite3_value object. // */ func _sqlite3ValueNew(tls *libc.TLS, db uintptr) (r uintptr) { var p uintptr _ = p p = _sqlite3DbMallocZero(tls, db, uint64(40)) if p != 0 { (*TMem)(unsafe.Pointer(p)).Fflags = uint16(MEM_Null) (*TMem)(unsafe.Pointer(p)).Fdb = db } return p } /* ** Context object passed by sqlite3Stat4ProbeSetValue() through to ** valueNew(). See comments above valueNew() for details. */ type TValueNewStat4Ctx = struct { FpParse uintptr FpIdx uintptr FppRec uintptr FiVal int32 } type ValueNewStat4Ctx = TValueNewStat4Ctx // C documentation // // /* // ** Allocate and return a pointer to a new sqlite3_value object. If // ** the second argument to this function is NULL, the object is allocated // ** by calling sqlite3ValueNew(). // ** // ** Otherwise, if the second argument is non-zero, then this function is // ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not // ** already been allocated, allocate the UnpackedRecord structure that // ** that function will return to its caller here. Then return a pointer to // ** an sqlite3_value within the UnpackedRecord.a[] array. // */ func _valueNew(tls *libc.TLS, db uintptr, p uintptr) (r uintptr) { var i, nByte, nCol int32 var pIdx, pRec uintptr _, _, _, _, _ = i, nByte, nCol, pIdx, pRec if p != 0 { pRec = *(*uintptr)(unsafe.Pointer((*TValueNewStat4Ctx)(unsafe.Pointer(p)).FppRec)) if pRec == uintptr(0) { pIdx = (*TValueNewStat4Ctx)(unsafe.Pointer(p)).FpIdx /* Counter variable */ nCol = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) /* Number of index columns including rowid */ nByte = int32(uint32(40)*uint32(nCol) + (libc.Uint32FromInt64(32)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))) pRec = _sqlite3DbMallocZero(tls, db, uint64(nByte)) if pRec != 0 { (*TUnpackedRecord)(unsafe.Pointer(pRec)).FpKeyInfo = _sqlite3KeyInfoOfIndex(tls, (*TValueNewStat4Ctx)(unsafe.Pointer(p)).FpParse, pIdx) if (*TUnpackedRecord)(unsafe.Pointer(pRec)).FpKeyInfo != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem = pRec + uintptr((libc.Uint32FromInt64(32)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))) i = 0 for { if !(i < nCol) { break } (*(*TMem)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem + uintptr(i)*40))).Fflags = uint16(MEM_Null) (*(*TMem)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem + uintptr(i)*40))).Fdb = db goto _1 _1: ; i++ } } else { _sqlite3DbFreeNN(tls, db, pRec) pRec = uintptr(0) } } if pRec == uintptr(0) { return uintptr(0) } *(*uintptr)(unsafe.Pointer((*TValueNewStat4Ctx)(unsafe.Pointer(p)).FppRec)) = pRec } (*TUnpackedRecord)(unsafe.Pointer(pRec)).FnField = uint16((*TValueNewStat4Ctx)(unsafe.Pointer(p)).FiVal + int32(1)) _sqlite3VdbeMemSetNull(tls, (*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem+uintptr((*TValueNewStat4Ctx)(unsafe.Pointer(p)).FiVal)*40) return (*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem + uintptr((*TValueNewStat4Ctx)(unsafe.Pointer(p)).FiVal)*40 } return _sqlite3ValueNew(tls, db) } // C documentation // // /* // ** The expression object indicated by the second argument is guaranteed // ** to be a scalar SQL function. If // ** // ** * all function arguments are SQL literals, // ** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and // ** * the SQLITE_FUNC_NEEDCOLL function flag is not set, // ** // ** then this routine attempts to invoke the SQL function. Assuming no // ** error occurs, output parameter (*ppVal) is set to point to a value // ** object containing the result before returning SQLITE_OK. // ** // ** Affinity aff is applied to the result of the function before returning. // ** If the result is a text value, the sqlite3_value object uses encoding // ** enc. // ** // ** If the conditions above are not met, this function returns SQLITE_OK // ** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to // ** NULL and an SQLite error code returned. // */ func _valueFromFunction(tls *libc.TLS, db uintptr, p uintptr, enc Tu8, aff Tu8, ppVal uintptr, pCtx uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var apVal, pFunc, pList, pVal uintptr var i, nVal, rc int32 var _ /* ctx at bp+0 */ Tsqlite3_context _, _, _, _, _, _, _ = apVal, i, nVal, pFunc, pList, pVal, rc /* Context object for function invocation */ apVal = uintptr(0) /* Function arguments */ nVal = 0 /* Size of apVal[] array */ pFunc = uintptr(0) /* Function definition */ pVal = uintptr(0) /* New value */ rc = SQLITE_OK /* Return code */ pList = uintptr(0) /* Iterator variable */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pList = *(*uintptr)(unsafe.Pointer(p + 20)) if pList != 0 { nVal = (*TExprList)(unsafe.Pointer(pList)).FnExpr } _ = libc.Int32FromInt32(0) pFunc = _sqlite3FindFunction(tls, db, *(*uintptr)(unsafe.Pointer(p + 8)), nVal, enc, uint8(0)) _ = libc.Int32FromInt32(0) if (*TFuncDef)(unsafe.Pointer(pFunc)).FfuncFlags&uint32(libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)|libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG)) == uint32(0) || (*TFuncDef)(unsafe.Pointer(pFunc)).FfuncFlags&uint32(libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)|libc.Int32FromInt32(SQLITE_FUNC_RUNONLY)) != uint32(0) { return SQLITE_OK } if pList != 0 { apVal = _sqlite3DbMallocZero(tls, db, uint64(uint32(4)*uint32(nVal))) if apVal == uintptr(0) { rc = int32(SQLITE_NOMEM) goto value_from_function_out } i = 0 for { if !(i < nVal) { break } rc = _sqlite3ValueFromExpr(tls, db, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr, enc, aff, apVal+uintptr(i)*4) if *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*4)) == uintptr(0) || rc != SQLITE_OK { goto value_from_function_out } goto _1 _1: ; i++ } } pVal = _valueNew(tls, db, pCtx) if pVal == uintptr(0) { rc = int32(SQLITE_NOMEM) goto value_from_function_out } libc.Xmemset(tls, bp, 0, uint32(32)) (*(*Tsqlite3_context)(unsafe.Pointer(bp))).FpOut = pVal (*(*Tsqlite3_context)(unsafe.Pointer(bp))).FpFunc = pFunc (*(*Tsqlite3_context)(unsafe.Pointer(bp))).Fenc = (*Tsqlite3)(unsafe.Pointer(db)).Fenc (*(*func(*libc.TLS, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFuncDef)(unsafe.Pointer(pFunc)).FxSFunc})))(tls, bp, nVal, apVal) if (*(*Tsqlite3_context)(unsafe.Pointer(bp))).FisError != 0 { rc = (*(*Tsqlite3_context)(unsafe.Pointer(bp))).FisError _sqlite3ErrorMsg(tls, (*TValueNewStat4Ctx)(unsafe.Pointer(pCtx)).FpParse, __ccgo_ts+3795, libc.VaList(bp+40, Xsqlite3_value_text(tls, pVal))) } else { _sqlite3ValueApplyAffinity(tls, pVal, aff, uint8(SQLITE_UTF8)) _ = libc.Int32FromInt32(0) rc = _sqlite3VdbeChangeEncoding(tls, pVal, int32(enc)) if rc == SQLITE_OK && _sqlite3VdbeMemTooBig(tls, pVal) != 0 { rc = int32(SQLITE_TOOBIG) (*TParse)(unsafe.Pointer((*TValueNewStat4Ctx)(unsafe.Pointer(pCtx)).FpParse)).FnErr++ } } goto value_from_function_out value_from_function_out: ; if rc != SQLITE_OK { pVal = uintptr(0) (*TParse)(unsafe.Pointer((*TValueNewStat4Ctx)(unsafe.Pointer(pCtx)).FpParse)).Frc = rc } if apVal != 0 { i = 0 for { if !(i < nVal) { break } _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*4))) goto _2 _2: ; i++ } _sqlite3DbFreeNN(tls, db, apVal) } *(*uintptr)(unsafe.Pointer(ppVal)) = pVal return rc } // C documentation // // /* // ** Extract a value from the supplied expression in the manner described // ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object // ** using valueNew(). // ** // ** If pCtx is NULL and an error occurs after the sqlite3_value object // ** has been allocated, it is freed before returning. Or, if pCtx is not // ** NULL, it is assumed that the caller will free any allocated object // ** in all cases. // */ func _valueFromExpr(tls *libc.TLS, db uintptr, pExpr uintptr, enc Tu8, affinity Tu8, ppVal uintptr, pCtx uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var aff Tu8 var nVal, negInt, op, rc, v1, v2 int32 var zNeg, zVal, p3 uintptr var _ /* pVal at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _ = aff, nVal, negInt, op, rc, zNeg, zVal, v1, v2, p3 zVal = uintptr(0) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) negInt = int32(1) zNeg = __ccgo_ts + 1648 rc = SQLITE_OK _ = libc.Int32FromInt32(0) for { v1 = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) op = v1 if !(v1 == int32(TK_UPLUS) || op == int32(TK_SPAN)) { break } pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft } if op == int32(TK_REGISTER) { op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop2) } /* Compressed expressions only appear when parsing the DEFAULT clause ** on a table column definition, and hence only when pCtx==0. This ** check ensures that an EP_TokenOnly expression is never passed down ** into valueFromFunction(). */ _ = libc.Int32FromInt32(0) if op == int32(TK_CAST) { _ = libc.Int32FromInt32(0) aff = _sqlite3AffinityType(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), uintptr(0)) rc = _valueFromExpr(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, enc, aff, ppVal, pCtx) if *(*uintptr)(unsafe.Pointer(ppVal)) != 0 { if int32((*Tsqlite3_value)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(ppVal)))).Fflags)&int32(MEM_Zero) != 0 { v2 = _sqlite3VdbeMemExpandBlob(tls, *(*uintptr)(unsafe.Pointer(ppVal))) } else { v2 = 0 } rc = v2 _sqlite3VdbeMemCast(tls, *(*uintptr)(unsafe.Pointer(ppVal)), aff, enc) _sqlite3ValueApplyAffinity(tls, *(*uintptr)(unsafe.Pointer(ppVal)), affinity, enc) } return rc } /* Handle negative integers in a single step. This is needed in the ** case when the value is -9223372036854775808. */ if op == int32(TK_UMINUS) && (int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft)).Fop) == int32(TK_INTEGER) || int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft)).Fop) == int32(TK_FLOAT)) { pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) negInt = -int32(1) zNeg = __ccgo_ts + 5074 } if op == int32(TK_STRING) || op == int32(TK_FLOAT) || op == int32(TK_INTEGER) { *(*uintptr)(unsafe.Pointer(bp)) = _valueNew(tls, db, pCtx) if *(*uintptr)(unsafe.Pointer(bp)) == uintptr(0) { goto no_mem } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_IntValue)) != uint32(0) { _sqlite3VdbeMemSetInt64(tls, *(*uintptr)(unsafe.Pointer(bp)), int64(*(*int32)(unsafe.Pointer(pExpr + 8)))*int64(negInt)) } else { zVal = _sqlite3MPrintf(tls, db, __ccgo_ts+5076, libc.VaList(bp+16, zNeg, *(*uintptr)(unsafe.Pointer(pExpr + 8)))) if zVal == uintptr(0) { goto no_mem } _sqlite3ValueSetStr(tls, *(*uintptr)(unsafe.Pointer(bp)), -int32(1), zVal, uint8(SQLITE_UTF8), __ccgo_fp(_sqlite3OomClear)) } if (op == int32(TK_INTEGER) || op == int32(TK_FLOAT)) && int32(affinity) == int32(SQLITE_AFF_BLOB) { _sqlite3ValueApplyAffinity(tls, *(*uintptr)(unsafe.Pointer(bp)), uint8(SQLITE_AFF_NUMERIC), uint8(SQLITE_UTF8)) } else { _sqlite3ValueApplyAffinity(tls, *(*uintptr)(unsafe.Pointer(bp)), affinity, uint8(SQLITE_UTF8)) } _ = libc.Int32FromInt32(0) if int32((*Tsqlite3_value)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)|libc.Int32FromInt32(MEM_Real)) != 0 { p3 = *(*uintptr)(unsafe.Pointer(bp)) + 16 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) & ^libc.Int32FromInt32(MEM_Str)) } if int32(enc) != int32(SQLITE_UTF8) { rc = _sqlite3VdbeChangeEncoding(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(enc)) } } else { if op == int32(TK_UMINUS) { /* This branch happens for multiple negative signs. Ex: -(-5) */ if SQLITE_OK == _valueFromExpr(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, enc, affinity, bp, pCtx) && *(*uintptr)(unsafe.Pointer(bp)) != uintptr(0) { _sqlite3VdbeMemNumerify(tls, *(*uintptr)(unsafe.Pointer(bp))) if int32((*Tsqlite3_value)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fflags)&int32(MEM_Real) != 0 { *(*float64)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))) = -*(*float64)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))) } else { if *(*Ti64)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))) == int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)< nRec || iHdr >= *(*int32)(unsafe.Pointer(bp + 4)) { return _sqlite3CorruptError(tls, int32(84303)) } iField = *(*int32)(unsafe.Pointer(bp + 4)) i = 0 for { if !(i <= iCol) { break } if int32(*(*Tu8)(unsafe.Pointer(a + uintptr(iHdr)))) < int32(libc.Uint8FromInt32(0x80)) { *(*Tu32)(unsafe.Pointer(bp)) = uint32(*(*Tu8)(unsafe.Pointer(a + uintptr(iHdr)))) v3 = libc.Int32FromInt32(1) } else { v3 = int32(_sqlite3GetVarint32(tls, a+uintptr(iHdr), bp)) } iHdr += int32(uint8(v3)) if iHdr > *(*int32)(unsafe.Pointer(bp + 4)) { return _sqlite3CorruptError(tls, int32(84309)) } szField = int32(_sqlite3VdbeSerialTypeLen(tls, *(*Tu32)(unsafe.Pointer(bp)))) iField += szField goto _2 _2: ; i++ } if iField > nRec { return _sqlite3CorruptError(tls, int32(84315)) } if pMem == uintptr(0) { v4 = _sqlite3ValueNew(tls, db) *(*uintptr)(unsafe.Pointer(ppVal)) = v4 pMem = v4 if pMem == uintptr(0) { return int32(SQLITE_NOMEM) } } _sqlite3VdbeSerialGet(tls, a+uintptr(iField-szField), *(*Tu32)(unsafe.Pointer(bp)), pMem) (*TMem)(unsafe.Pointer(pMem)).Fenc = (*Tsqlite3)(unsafe.Pointer(db)).Fenc return SQLITE_OK } // C documentation // // /* // ** Unless it is NULL, the argument must be an UnpackedRecord object returned // ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes // ** the object. // */ func _sqlite3Stat4ProbeFree(tls *libc.TLS, pRec uintptr) { var aMem, db uintptr var i, nCol int32 _, _, _, _ = aMem, db, i, nCol if pRec != 0 { nCol = int32((*TKeyInfo)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(pRec)).FpKeyInfo)).FnAllField) aMem = (*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem db = (*(*TMem)(unsafe.Pointer(aMem))).Fdb i = 0 for { if !(i < nCol) { break } _sqlite3VdbeMemRelease(tls, aMem+uintptr(i)*40) goto _1 _1: ; i++ } _sqlite3KeyInfoUnref(tls, (*TUnpackedRecord)(unsafe.Pointer(pRec)).FpKeyInfo) _sqlite3DbFreeNN(tls, db, pRec) } } // C documentation // // /* // ** Change the string value of an sqlite3_value object // */ func _sqlite3ValueSetStr(tls *libc.TLS, v uintptr, n int32, z uintptr, enc Tu8, xDel uintptr) { if v != 0 { _sqlite3VdbeMemSetStr(tls, v, z, int64(n), enc, xDel) } } // C documentation // // /* // ** Free an sqlite3_value object // */ func _sqlite3ValueFree(tls *libc.TLS, v uintptr) { if !(v != 0) { return } _sqlite3VdbeMemRelease(tls, v) _sqlite3DbFreeNN(tls, (*TMem)(unsafe.Pointer(v)).Fdb, v) } // C documentation // // /* // ** The sqlite3ValueBytes() routine returns the number of bytes in the // ** sqlite3_value object assuming that it uses the encoding "enc". // ** The valueBytes() routine is a helper function. // */ func _valueBytes(tls *libc.TLS, pVal uintptr, enc Tu8) (r int32) { var v1 int32 _ = v1 if _valueToText(tls, pVal, enc) != uintptr(0) { v1 = (*Tsqlite3_value)(unsafe.Pointer(pVal)).Fn } else { v1 = 0 } return v1 } func _sqlite3ValueBytes(tls *libc.TLS, pVal uintptr, enc Tu8) (r int32) { var p uintptr _ = p p = pVal _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Str) != 0 && int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fenc) == int32(enc) { return (*TMem)(unsafe.Pointer(p)).Fn } if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Str) != 0 && int32(enc) != int32(SQLITE_UTF8) && int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fenc) != int32(SQLITE_UTF8) { return (*TMem)(unsafe.Pointer(p)).Fn } if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Blob) != 0 { if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Zero) != 0 { return (*TMem)(unsafe.Pointer(p)).Fn + *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(p)).Fu)) } else { return (*TMem)(unsafe.Pointer(p)).Fn } } if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Null) != 0 { return 0 } return _valueBytes(tls, pVal, enc) } // C documentation // // /* // ** Create a new virtual database engine. // */ func _sqlite3VdbeCreate(tls *libc.TLS, pParse uintptr) (r uintptr) { var db, p uintptr _, _ = db, p db = (*TParse)(unsafe.Pointer(pParse)).Fdb p = _sqlite3DbMallocRawNN(tls, db, uint64(232)) if p == uintptr(0) { return uintptr(0) } libc.Xmemset(tls, p+104, 0, libc.Uint32FromInt64(232)-uint32(libc.UintptrFromInt32(0)+104)) (*TVdbe)(unsafe.Pointer(p)).Fdb = db if (*Tsqlite3)(unsafe.Pointer(db)).FpVdbe != 0 { (*TVdbe1)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpVdbe)).FppVPrev = p + 8 } (*TVdbe)(unsafe.Pointer(p)).FpVNext = (*Tsqlite3)(unsafe.Pointer(db)).FpVdbe (*TVdbe)(unsafe.Pointer(p)).FppVPrev = db + 4 (*Tsqlite3)(unsafe.Pointer(db)).FpVdbe = p _ = libc.Int32FromInt32(0) (*TVdbe)(unsafe.Pointer(p)).FpParse = pParse (*TParse)(unsafe.Pointer(pParse)).FpVdbe = p _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, p, int32(OP_Init), 0, int32(1)) return p } // C documentation // // /* // ** Return the Parse object that owns a Vdbe object. // */ func _sqlite3VdbeParser(tls *libc.TLS, p uintptr) (r uintptr) { return (*TVdbe)(unsafe.Pointer(p)).FpParse } // C documentation // // /* // ** Change the error string stored in Vdbe.zErrMsg // */ func _sqlite3VdbeError(tls *libc.TLS, p uintptr, zFormat uintptr, va uintptr) { var ap Tva_list _ = ap _sqlite3DbFree(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, (*TVdbe)(unsafe.Pointer(p)).FzErrMsg) ap = va (*TVdbe)(unsafe.Pointer(p)).FzErrMsg = _sqlite3VMPrintf(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, zFormat, ap) _ = ap } // C documentation // // /* // ** Remember the SQL string for a prepared statement. // */ func _sqlite3VdbeSetSql(tls *libc.TLS, p uintptr, z uintptr, n int32, prepFlags Tu8) { if p == uintptr(0) { return } (*TVdbe)(unsafe.Pointer(p)).FprepFlags = prepFlags if int32(prepFlags)&int32(SQLITE_PREPARE_SAVESQL) == 0 { (*TVdbe)(unsafe.Pointer(p)).Fexpmask = uint32(0) } _ = libc.Int32FromInt32(0) (*TVdbe)(unsafe.Pointer(p)).FzSql = _sqlite3DbStrNDup(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, z, uint64(n)) } // C documentation // // /* // ** Swap byte-code between two VDBE structures. // ** // ** This happens after pB was previously run and returned // ** SQLITE_SCHEMA. The statement was then reprepared in pA. // ** This routine transfers the new bytecode in pA over to pB // ** so that pB can be run again. The old pB byte code is // ** moved back to pA so that it will be cleaned up when pA is // ** finalized. // */ func _sqlite3VdbeSwap(tls *libc.TLS, pA uintptr, pB uintptr) { var pTmp, ppTmp, zTmp uintptr var tmp TVdbe _, _, _, _ = pTmp, ppTmp, tmp, zTmp _ = libc.Int32FromInt32(0) tmp = *(*TVdbe)(unsafe.Pointer(pA)) *(*TVdbe)(unsafe.Pointer(pA)) = *(*TVdbe)(unsafe.Pointer(pB)) *(*TVdbe)(unsafe.Pointer(pB)) = tmp pTmp = (*TVdbe)(unsafe.Pointer(pA)).FpVNext (*TVdbe)(unsafe.Pointer(pA)).FpVNext = (*TVdbe)(unsafe.Pointer(pB)).FpVNext (*TVdbe)(unsafe.Pointer(pB)).FpVNext = pTmp ppTmp = (*TVdbe)(unsafe.Pointer(pA)).FppVPrev (*TVdbe)(unsafe.Pointer(pA)).FppVPrev = (*TVdbe)(unsafe.Pointer(pB)).FppVPrev (*TVdbe)(unsafe.Pointer(pB)).FppVPrev = ppTmp zTmp = (*TVdbe)(unsafe.Pointer(pA)).FzSql (*TVdbe)(unsafe.Pointer(pA)).FzSql = (*TVdbe)(unsafe.Pointer(pB)).FzSql (*TVdbe)(unsafe.Pointer(pB)).FzSql = zTmp (*TVdbe)(unsafe.Pointer(pB)).Fexpmask = (*TVdbe)(unsafe.Pointer(pA)).Fexpmask (*TVdbe)(unsafe.Pointer(pB)).FprepFlags = (*TVdbe)(unsafe.Pointer(pA)).FprepFlags libc.Xmemcpy(tls, pB+164, pA+164, uint32(36)) *(*Tu32)(unsafe.Pointer(pB + 164 + 5*4))++ } // C documentation // // /* // ** Resize the Vdbe.aOp array so that it is at least nOp elements larger // ** than its current size. nOp is guaranteed to be less than or equal // ** to 1024/sizeof(Op). // ** // ** If an out-of-memory error occurs while resizing the array, return // ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain // ** unchanged (this is so that any opcodes already allocated can be // ** correctly deallocated along with the rest of the Vdbe). // */ func _growOpArray(tls *libc.TLS, v uintptr, nOp int32) (r int32) { var nNew Tsqlite3_int64 var p, pNew uintptr var v1 int64 var v2 int32 _, _, _, _, _ = nNew, p, pNew, v1, v2 p = (*TVdbe)(unsafe.Pointer(v)).FpParse if (*TVdbe)(unsafe.Pointer(v)).FnOpAlloc != 0 { v1 = int64(2) * int64((*TVdbe)(unsafe.Pointer(v)).FnOpAlloc) } else { v1 = int64(libc.Uint32FromInt32(1024) / libc.Uint32FromInt64(20)) } /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force ** more frequent reallocs and hence provide more opportunities for ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array ** by the minimum* amount required until the size reaches 512. Normal ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current ** size of the op array or add 1KB of space, whichever is smaller. */ nNew = v1 _ = nOp /* Ensure that the size of a VDBE does not grow too large */ if nNew > int64(*(*int32)(unsafe.Pointer((*TParse)(unsafe.Pointer(p)).Fdb + 120 + 5*4))) { _sqlite3OomFault(tls, (*TParse)(unsafe.Pointer(p)).Fdb) return int32(SQLITE_NOMEM) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pNew = _sqlite3DbRealloc(tls, (*TParse)(unsafe.Pointer(p)).Fdb, (*TVdbe)(unsafe.Pointer(v)).FaOp, uint64(nNew*int64(20))) if pNew != 0 { (*TParse)(unsafe.Pointer(p)).FszOpAlloc = _sqlite3DbMallocSize(tls, (*TParse)(unsafe.Pointer(p)).Fdb, pNew) (*TVdbe)(unsafe.Pointer(v)).FnOpAlloc = int32(uint32((*TParse)(unsafe.Pointer(p)).FszOpAlloc) / uint32(20)) (*TVdbe)(unsafe.Pointer(v)).FaOp = pNew } if pNew != 0 { v2 = SQLITE_OK } else { v2 = int32(SQLITE_NOMEM) } return v2 } // C documentation // // /* // ** Slow paths for sqlite3VdbeAddOp3() and sqlite3VdbeAddOp4Int() for the // ** unusual case when we need to increase the size of the Vdbe.aOp[] array // ** before adding the new opcode. // */ func _growOp3(tls *libc.TLS, p uintptr, op int32, p1 int32, p2 int32, p3 int32) (r int32) { _ = libc.Int32FromInt32(0) if _growOpArray(tls, p, int32(1)) != 0 { return int32(1) } _ = libc.Int32FromInt32(0) return _sqlite3VdbeAddOp3(tls, p, op, p1, p2, p3) } func _addOp4IntSlow(tls *libc.TLS, p uintptr, op int32, p1 int32, p2 int32, p3 int32, p4 int32) (r int32) { var addr int32 var pOp uintptr _, _ = addr, pOp addr = _sqlite3VdbeAddOp3(tls, p, op, p1, p2, p3) if int32((*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FmallocFailed) == 0 { pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(addr)*20 (*TVdbeOp)(unsafe.Pointer(pOp)).Fp4type = int8(-libc.Int32FromInt32(3)) *(*int32)(unsafe.Pointer(pOp + 16)) = p4 } return addr } // C documentation // // /* // ** Add a new instruction to the list of instructions current in the // ** VDBE. Return the address of the new instruction. // ** // ** Parameters: // ** // ** p Pointer to the VDBE // ** // ** op The opcode for this instruction // ** // ** p1, p2, p3, p4 Operands // */ func _sqlite3VdbeAddOp0(tls *libc.TLS, p uintptr, op int32) (r int32) { return _sqlite3VdbeAddOp3(tls, p, op, 0, 0, 0) } func _sqlite3VdbeAddOp1(tls *libc.TLS, p uintptr, op int32, p1 int32) (r int32) { return _sqlite3VdbeAddOp3(tls, p, op, p1, 0, 0) } func _sqlite3VdbeAddOp2(tls *libc.TLS, p uintptr, op int32, p1 int32, p2 int32) (r int32) { return _sqlite3VdbeAddOp3(tls, p, op, p1, p2, 0) } func _sqlite3VdbeAddOp3(tls *libc.TLS, p uintptr, op int32, p1 int32, p2 int32, p3 int32) (r int32) { var i int32 var pOp uintptr _, _ = i, pOp i = (*TVdbe)(unsafe.Pointer(p)).FnOp _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TVdbe)(unsafe.Pointer(p)).FnOpAlloc <= i { return _growOp3(tls, p, op, p1, p2, p3) } _ = libc.Int32FromInt32(0) (*TVdbe)(unsafe.Pointer(p)).FnOp++ pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(i)*20 _ = libc.Int32FromInt32(0) (*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode = uint8(op) (*TVdbeOp)(unsafe.Pointer(pOp)).Fp5 = uint16(0) (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = p1 (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 = p2 (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 = p3 *(*uintptr)(unsafe.Pointer(pOp + 16)) = uintptr(0) (*TVdbeOp)(unsafe.Pointer(pOp)).Fp4type = P4_NOTUSED /* Replicate this logic in sqlite3VdbeAddOp4Int() ** vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */ /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ** Replicate in sqlite3VdbeAddOp4Int() */ return i } func _sqlite3VdbeAddOp4Int(tls *libc.TLS, p uintptr, op int32, p1 int32, p2 int32, p3 int32, p4 int32) (r int32) { var i int32 var pOp uintptr _, _ = i, pOp i = (*TVdbe)(unsafe.Pointer(p)).FnOp if (*TVdbe)(unsafe.Pointer(p)).FnOpAlloc <= i { return _addOp4IntSlow(tls, p, op, p1, p2, p3, p4) } (*TVdbe)(unsafe.Pointer(p)).FnOp++ pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(i)*20 _ = libc.Int32FromInt32(0) (*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode = uint8(op) (*TVdbeOp)(unsafe.Pointer(pOp)).Fp5 = uint16(0) (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = p1 (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 = p2 (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 = p3 *(*int32)(unsafe.Pointer(pOp + 16)) = p4 (*TVdbeOp)(unsafe.Pointer(pOp)).Fp4type = int8(-libc.Int32FromInt32(3)) /* Replicate this logic in sqlite3VdbeAddOp3() ** vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */ /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ** Replicate in sqlite3VdbeAddOp3() */ return i } // C documentation // // /* Generate code for an unconditional jump to instruction iDest // */ func _sqlite3VdbeGoto(tls *libc.TLS, p uintptr, iDest int32) (r int32) { return _sqlite3VdbeAddOp3(tls, p, int32(OP_Goto), 0, iDest, 0) } // C documentation // // /* Generate code to cause the string zStr to be loaded into // ** register iDest // */ func _sqlite3VdbeLoadString(tls *libc.TLS, p uintptr, iDest int32, zStr uintptr) (r int32) { return _sqlite3VdbeAddOp4(tls, p, int32(OP_String8), 0, iDest, 0, zStr, 0) } // C documentation // // /* // ** Generate code that initializes multiple registers to string or integer // ** constants. The registers begin with iDest and increase consecutively. // ** One register is initialized for each characgter in zTypes[]. For each // ** "s" character in zTypes[], the register is a string if the argument is // ** not NULL, or OP_Null if the value is a null pointer. For each "i" character // ** in zTypes[], the register is initialized to an integer. // ** // ** If the input string does not end with "X" then an OP_ResultRow instruction // ** is generated for the values inserted. // */ func _sqlite3VdbeMultiLoad(tls *libc.TLS, p uintptr, iDest int32, zTypes uintptr, va uintptr) { var ap Tva_list var c, v2 uint8 var i, v3 int32 var z uintptr _, _, _, _, _, _ = ap, c, i, z, v2, v3 ap = va i = 0 for { v2 = *(*uint8)(unsafe.Pointer(zTypes + uintptr(i))) c = v2 if !(int32(v2) != 0) { break } if int32(c) == int32('s') { z = libc.VaUintptr(&ap) if z == uintptr(0) { v3 = int32(OP_Null) } else { v3 = int32(OP_String8) } _sqlite3VdbeAddOp4(tls, p, v3, 0, iDest+i, 0, z, 0) } else { if int32(c) == int32('i') { _sqlite3VdbeAddOp2(tls, p, int32(OP_Integer), libc.VaInt32(&ap), iDest+i) } else { goto skip_op_resultrow } } goto _1 _1: ; i++ } _sqlite3VdbeAddOp2(tls, p, int32(OP_ResultRow), iDest, i) goto skip_op_resultrow skip_op_resultrow: ; _ = ap } // C documentation // // /* // ** Add an opcode that includes the p4 value as a pointer. // */ func _sqlite3VdbeAddOp4(tls *libc.TLS, p uintptr, op int32, p1 int32, p2 int32, p3 int32, zP4 uintptr, p4type int32) (r int32) { var addr int32 _ = addr addr = _sqlite3VdbeAddOp3(tls, p, op, p1, p2, p3) _sqlite3VdbeChangeP4(tls, p, addr, zP4, p4type) return addr } // C documentation // // /* // ** Add an OP_Function or OP_PureFunc opcode. // ** // ** The eCallCtx argument is information (typically taken from Expr.op2) // ** that describes the calling context of the function. 0 means a general // ** function call. NC_IsCheck means called by a check constraint, // ** NC_IdxExpr means called as part of an index expression. NC_PartIdx // ** means in the WHERE clause of a partial index. NC_GenCol means called // ** while computing a generated column value. 0 is the usual case. // */ func _sqlite3VdbeAddFunctionCall(tls *libc.TLS, pParse uintptr, p1 int32, p2 int32, p3 int32, nArg int32, pFunc uintptr, eCallCtx int32) (r int32) { var addr, nByte, v1 int32 var pCtx, v uintptr _, _, _, _, _ = addr, nByte, pCtx, v, v1 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) nByte = int32(uint32(32) + uint32(nArg-libc.Int32FromInt32(1))*uint32(4)) pCtx = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(nByte)) if pCtx == uintptr(0) { _ = libc.Int32FromInt32(0) _freeEphemeralFunction(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pFunc) return 0 } (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut = uintptr(0) (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpFunc = pFunc (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpVdbe = uintptr(0) (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FisError = 0 (*Tsqlite3_context)(unsafe.Pointer(pCtx)).Fargc = uint8(nArg) (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FiOp = _sqlite3VdbeCurrentAddr(tls, v) if eCallCtx != 0 { v1 = int32(OP_PureFunc) } else { v1 = int32(OP_Function) } addr = _sqlite3VdbeAddOp4(tls, v, v1, p1, p2, p3, pCtx, -int32(15)) _sqlite3VdbeChangeP5(tls, v, uint16(eCallCtx&int32(NC_SelfRef))) _sqlite3MayAbort(tls, pParse) return addr } // C documentation // // /* // ** Add an opcode that includes the p4 value with a P4_INT64 or // ** P4_REAL type. // */ func _sqlite3VdbeAddOp4Dup8(tls *libc.TLS, p uintptr, op int32, p1 int32, p2 int32, p3 int32, zP4 uintptr, p4type int32) (r int32) { var p4copy uintptr _ = p4copy p4copy = _sqlite3DbMallocRawNN(tls, _sqlite3VdbeDb(tls, p), uint64(8)) if p4copy != 0 { libc.Xmemcpy(tls, p4copy, zP4, uint32(8)) } return _sqlite3VdbeAddOp4(tls, p, op, p1, p2, p3, p4copy, p4type) } // C documentation // // /* // ** Return the address of the current EXPLAIN QUERY PLAN baseline. // ** 0 means "none". // */ func _sqlite3VdbeExplainParent(tls *libc.TLS, pParse uintptr) (r int32) { var pOp uintptr _ = pOp if (*TParse)(unsafe.Pointer(pParse)).FaddrExplain == 0 { return 0 } pOp = _sqlite3VdbeGetOp(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, (*TParse)(unsafe.Pointer(pParse)).FaddrExplain) return (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 } /* ** Set a debugger breakpoint on the following routine in order to ** monitor the EXPLAIN QUERY PLAN code generation. */ // C documentation // // /* // ** Add a new OP_Explain opcode. // ** // ** If the bPush flag is true, then make this opcode the parent for // ** subsequent Explains until sqlite3VdbeExplainPop() is called. // */ func _sqlite3VdbeExplain(tls *libc.TLS, pParse uintptr, bPush Tu8, zFmt uintptr, va uintptr) (r int32) { var addr, iThis int32 var ap Tva_list var v, zMsg uintptr _, _, _, _, _ = addr, ap, iThis, v, zMsg addr = 0 /* Always include the OP_Explain opcodes if SQLITE_DEBUG is defined. ** But omit them (for performance) during production builds */ if int32((*TParse)(unsafe.Pointer(pParse)).Fexplain) == int32(2) || libc.Bool(0 != 0) { ap = va zMsg = _sqlite3VMPrintf(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, zFmt, ap) _ = ap v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe iThis = (*TVdbe)(unsafe.Pointer(v)).FnOp addr = _sqlite3VdbeAddOp4(tls, v, int32(OP_Explain), iThis, (*TParse)(unsafe.Pointer(pParse)).FaddrExplain, 0, zMsg, -int32(6)) if bPush != 0 { (*TParse)(unsafe.Pointer(pParse)).FaddrExplain = iThis } } return addr } // C documentation // // /* // ** Pop the EXPLAIN QUERY PLAN stack one level. // */ func _sqlite3VdbeExplainPop(tls *libc.TLS, pParse uintptr) { (*TParse)(unsafe.Pointer(pParse)).FaddrExplain = _sqlite3VdbeExplainParent(tls, pParse) } // C documentation // // /* // ** Add an OP_ParseSchema opcode. This routine is broken out from // ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees // ** as having been used. // ** // ** The zWhere string must have been obtained from sqlite3_malloc(). // ** This routine will take ownership of the allocated memory. // */ func _sqlite3VdbeAddParseSchemaOp(tls *libc.TLS, p uintptr, iDb int32, zWhere uintptr, p5 Tu16) { var j int32 _ = j _sqlite3VdbeAddOp4(tls, p, int32(OP_ParseSchema), iDb, 0, 0, zWhere, -int32(6)) _sqlite3VdbeChangeP5(tls, p, p5) j = 0 for { if !(j < (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FnDb) { break } _sqlite3VdbeUsesBtree(tls, p, j) goto _1 _1: ; j++ } _sqlite3MayAbort(tls, (*TVdbe)(unsafe.Pointer(p)).FpParse) } // C documentation // // /* Insert the end of a co-routine // */ func _sqlite3VdbeEndCoroutine(tls *libc.TLS, v uintptr, regYield int32) { _sqlite3VdbeAddOp1(tls, v, int32(OP_EndCoroutine), regYield) /* Clear the temporary register cache, thereby ensuring that each ** co-routine has its own independent set of registers, because co-routines ** might expect their registers to be preserved across an OP_Yield, and ** that could cause problems if two or more co-routines are using the same ** temporary register. */ (*TParse)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(v)).FpParse)).FnTempReg = uint8(0) (*TParse)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(v)).FpParse)).FnRangeReg = 0 } // C documentation // // /* // ** Create a new symbolic label for an instruction that has yet to be // ** coded. The symbolic label is really just a negative number. The // ** label can be used as the P2 value of an operation. Later, when // ** the label is resolved to a specific address, the VDBE will scan // ** through its operation list and change all values of P2 which match // ** the label into the resolved address. // ** // ** The VDBE knows that a P2 value is a label because labels are // ** always negative and P2 values are suppose to be non-negative. // ** Hence, a negative P2 value is a label that has yet to be resolved. // ** (Later:) This is only true for opcodes that have the OPFLG_JUMP // ** property. // ** // ** Variable usage notes: // ** // ** Parse.aLabel[x] Stores the address that the x-th label resolves // ** into. For testing (SQLITE_DEBUG), unresolved // ** labels stores -1, but that is not required. // ** Parse.nLabelAlloc Number of slots allocated to Parse.aLabel[] // ** Parse.nLabel The *negative* of the number of labels that have // ** been issued. The negative is stored because // ** that gives a performance improvement over storing // ** the equivalent positive value. // */ func _sqlite3VdbeMakeLabel(tls *libc.TLS, pParse uintptr) (r int32) { var v1 int32 var v2 uintptr _, _ = v1, v2 v2 = pParse + 56 *(*int32)(unsafe.Pointer(v2))-- v1 = *(*int32)(unsafe.Pointer(v2)) return v1 } // C documentation // // /* // ** Resolve label "x" to be the address of the next instruction to // ** be inserted. The parameter "x" must have been obtained from // ** a prior call to sqlite3VdbeMakeLabel(). // */ func _resizeResolveLabel(tls *libc.TLS, p uintptr, v uintptr, j int32) { var nNewSize int32 _ = nNewSize nNewSize = int32(10) - (*TParse)(unsafe.Pointer(p)).FnLabel (*TParse)(unsafe.Pointer(p)).FaLabel = _sqlite3DbReallocOrFree(tls, (*TParse)(unsafe.Pointer(p)).Fdb, (*TParse)(unsafe.Pointer(p)).FaLabel, uint64(uint32(nNewSize)*uint32(4))) if (*TParse)(unsafe.Pointer(p)).FaLabel == uintptr(0) { (*TParse)(unsafe.Pointer(p)).FnLabelAlloc = 0 } else { if nNewSize >= int32(100) && nNewSize/int32(100) > (*TParse)(unsafe.Pointer(p)).FnLabelAlloc/int32(100) { _sqlite3ProgressCheck(tls, p) } (*TParse)(unsafe.Pointer(p)).FnLabelAlloc = nNewSize *(*int32)(unsafe.Pointer((*TParse)(unsafe.Pointer(p)).FaLabel + uintptr(j)*4)) = (*TVdbe)(unsafe.Pointer(v)).FnOp } } func _sqlite3VdbeResolveLabel(tls *libc.TLS, v uintptr, x int32) { var j int32 var p uintptr _, _ = j, p p = (*TVdbe)(unsafe.Pointer(v)).FpParse j = ^x _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(p)).FnLabelAlloc+(*TParse)(unsafe.Pointer(p)).FnLabel < 0 { _resizeResolveLabel(tls, p, v, j) } else { _ = libc.Int32FromInt32(0) /* Labels may only be resolved once */ *(*int32)(unsafe.Pointer((*TParse)(unsafe.Pointer(p)).FaLabel + uintptr(j)*4)) = (*TVdbe)(unsafe.Pointer(v)).FnOp } } // C documentation // // /* // ** Mark the VDBE as one that can only be run one time. // */ func _sqlite3VdbeRunOnlyOnce(tls *libc.TLS, p uintptr) { _sqlite3VdbeAddOp2(tls, p, int32(OP_Expire), int32(1), int32(1)) } // C documentation // // /* // ** Mark the VDBE as one that can be run multiple times. // */ func _sqlite3VdbeReusable(tls *libc.TLS, p uintptr) { var i int32 _ = i i = int32(1) for { if !(i < (*TVdbe)(unsafe.Pointer(p)).FnOp) { break } if int32((*(*TOp)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(i)*20))).Fopcode) == int32(OP_Expire) { (*(*TOp)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FaOp + 1*20))).Fopcode = uint8(OP_Noop) break } goto _1 _1: ; i++ } } // C documentation // // /* // ** This routine is called after all opcodes have been inserted. It loops // ** through all the opcodes and fixes up some details. // ** // ** (1) For each jump instruction with a negative P2 value (a label) // ** resolve the P2 value to an actual address. // ** // ** (2) Compute the maximum number of arguments used by any SQL function // ** and store that value in *pMaxFuncArgs. // ** // ** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately // ** indicate what the prepared statement actually does. // ** // ** (4) (discontinued) // ** // ** (5) Reclaim the memory allocated for storing labels. // ** // ** This routine will only function correctly if the mkopcodeh.tcl generator // ** script numbers the opcodes correctly. Changes to this routine must be // ** coordinated with changes to mkopcodeh.tcl. // */ func _resolveP2Values(tls *libc.TLS, p uintptr, pMaxFuncArgs uintptr) { var aLabel, pOp, pParse uintptr var n, nMaxArgs int32 _, _, _, _, _ = aLabel, n, nMaxArgs, pOp, pParse nMaxArgs = *(*int32)(unsafe.Pointer(pMaxFuncArgs)) pParse = (*TVdbe)(unsafe.Pointer(p)).FpParse aLabel = (*TParse)(unsafe.Pointer(pParse)).FaLabel _ = libc.Int32FromInt32(0) /* tag-20230419-1 */ libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(1), 6, 0x40) libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 7, 0x80) pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr((*TVdbe)(unsafe.Pointer(p)).FnOp-int32(1))*20 _ = libc.Int32FromInt32(0) for int32(1) != 0 { /* Only JUMP opcodes and the short list of special opcodes in the switch ** below need to be considered. The mkopcodeh.tcl generator script groups ** all these opcodes together near the front of the opcode list. Skip ** any opcode that does not need processing by virtual of the fact that ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization. */ if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) <= int32(SQLITE_MX_JUMP_OPCODE) { /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing ** cases from this switch! */ switch int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) { case int32(OP_Transaction): if (*TOp)(unsafe.Pointer(pOp)).Fp2 != 0 { libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 6, 0x40) } fallthrough case int32(OP_AutoCommit): fallthrough case OP_Savepoint: libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(1), 7, 0x80) case int32(OP_Checkpoint): fallthrough case int32(OP_Vacuum): fallthrough case int32(OP_JournalMode): libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 6, 0x40) libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(1), 7, 0x80) case int32(OP_Init): _ = libc.Int32FromInt32(0) goto resolve_p2_values_loop_exit case int32(OP_VUpdate): if (*TOp)(unsafe.Pointer(pOp)).Fp2 > nMaxArgs { nMaxArgs = (*TOp)(unsafe.Pointer(pOp)).Fp2 } case int32(OP_VFilter): _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) n = (*(*TOp)(unsafe.Pointer(pOp + uintptr(-libc.Int32FromInt32(1))*20))).Fp1 if n > nMaxArgs { nMaxArgs = n } fallthrough default: if (*TOp)(unsafe.Pointer(pOp)).Fp2 < 0 { /* The mkopcodeh.tcl script has so arranged things that the only ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to ** have non-negative values for P2. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* True because of tag-20230419-1 */ (*TOp)(unsafe.Pointer(pOp)).Fp2 = *(*int32)(unsafe.Pointer(aLabel + uintptr(^(*TOp)(unsafe.Pointer(pOp)).Fp2)*4)) } break } /* The mkopcodeh.tcl script has so arranged things that the only ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to ** have non-negative values for P2. */ _ = libc.Int32FromInt32(0) } _ = libc.Int32FromInt32(0) pOp -= 20 } goto resolve_p2_values_loop_exit resolve_p2_values_loop_exit: ; if aLabel != 0 { _sqlite3DbNNFreeNN(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, (*TParse)(unsafe.Pointer(pParse)).FaLabel) (*TParse)(unsafe.Pointer(pParse)).FaLabel = uintptr(0) } (*TParse)(unsafe.Pointer(pParse)).FnLabel = 0 *(*int32)(unsafe.Pointer(pMaxFuncArgs)) = nMaxArgs _ = libc.Int32FromInt32(0) } // C documentation // // /* // ** Return the address of the next instruction to be inserted. // */ func _sqlite3VdbeCurrentAddr(tls *libc.TLS, p uintptr) (r int32) { _ = libc.Int32FromInt32(0) return (*TVdbe)(unsafe.Pointer(p)).FnOp } /* ** Verify that at least N opcode slots are available in p without ** having to malloc for more space (except when compiled using ** SQLITE_TEST_REALLOC_STRESS). This interface is used during testing ** to verify that certain calls to sqlite3VdbeAddOpList() can never ** fail due to a OOM fault and hence that the return value from ** sqlite3VdbeAddOpList() will always be non-NULL. */ /* ** Verify that the VM passed as the only argument does not contain ** an OP_ResultRow opcode. Fail an assert() if it does. This is used ** by code in pragma.c to ensure that the implementation of certain ** pragmas comports with the flags specified in the mkpragmatab.tcl ** script. */ /* ** Generate code (a single OP_Abortable opcode) that will ** verify that the VDBE program can safely call Abort in the current ** context. */ // C documentation // // /* // ** This function returns a pointer to the array of opcodes associated with // ** the Vdbe passed as the first argument. It is the callers responsibility // ** to arrange for the returned array to be eventually freed using the // ** vdbeFreeOpArray() function. // ** // ** Before returning, *pnOp is set to the number of entries in the returned // ** array. Also, *pnMaxArg is set to the larger of its current value and // ** the number of entries in the Vdbe.apArg[] array required to execute the // ** returned program. // */ func _sqlite3VdbeTakeOpArray(tls *libc.TLS, p uintptr, pnOp uintptr, pnMaxArg uintptr) (r uintptr) { var aOp uintptr _ = aOp aOp = (*TVdbe)(unsafe.Pointer(p)).FaOp _ = libc.Int32FromInt32(0) /* Check that sqlite3VdbeUsesBtree() was not called on this VM */ _ = libc.Int32FromInt32(0) _resolveP2Values(tls, p, pnMaxArg) *(*int32)(unsafe.Pointer(pnOp)) = (*TVdbe)(unsafe.Pointer(p)).FnOp (*TVdbe)(unsafe.Pointer(p)).FaOp = uintptr(0) return aOp } // C documentation // // /* // ** Add a whole list of operations to the operation stack. Return a // ** pointer to the first operation inserted. // ** // ** Non-zero P2 arguments to jump instructions are automatically adjusted // ** so that the jump target is relative to the first operation inserted. // */ func _sqlite3VdbeAddOpList(tls *libc.TLS, p uintptr, nOp int32, aOp uintptr, iLineno int32) (r uintptr) { var i int32 var pFirst, pOut, v1 uintptr _, _, _, _ = i, pFirst, pOut, v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TVdbe)(unsafe.Pointer(p)).FnOp+nOp > (*TVdbe)(unsafe.Pointer(p)).FnOpAlloc && _growOpArray(tls, p, nOp) != 0 { return uintptr(0) } v1 = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr((*TVdbe)(unsafe.Pointer(p)).FnOp)*20 pOut = v1 pFirst = v1 i = 0 for { if !(i < nOp) { break } (*TVdbeOp)(unsafe.Pointer(pOut)).Fopcode = (*TVdbeOpList)(unsafe.Pointer(aOp)).Fopcode (*TVdbeOp)(unsafe.Pointer(pOut)).Fp1 = int32((*TVdbeOpList)(unsafe.Pointer(aOp)).Fp1) (*TVdbeOp)(unsafe.Pointer(pOut)).Fp2 = int32((*TVdbeOpList)(unsafe.Pointer(aOp)).Fp2) _ = libc.Int32FromInt32(0) if int32(_sqlite3OpcodeProperty[(*TVdbeOpList)(unsafe.Pointer(aOp)).Fopcode])&int32(OPFLG_JUMP) != 0 && int32((*TVdbeOpList)(unsafe.Pointer(aOp)).Fp2) > 0 { *(*int32)(unsafe.Pointer(pOut + 8)) += (*TVdbe)(unsafe.Pointer(p)).FnOp } (*TVdbeOp)(unsafe.Pointer(pOut)).Fp3 = int32((*TVdbeOpList)(unsafe.Pointer(aOp)).Fp3) (*TVdbeOp)(unsafe.Pointer(pOut)).Fp4type = P4_NOTUSED *(*uintptr)(unsafe.Pointer(pOut + 16)) = uintptr(0) (*TVdbeOp)(unsafe.Pointer(pOut)).Fp5 = uint16(0) _ = iLineno goto _2 _2: ; i++ aOp += 4 pOut += 20 } *(*int32)(unsafe.Pointer(p + 108)) += nOp return pFirst } // C documentation // // /* // ** Change the value of the opcode, or P1, P2, P3, or P5 operands // ** for a specific instruction. // */ func _sqlite3VdbeChangeOpcode(tls *libc.TLS, p uintptr, addr int32, iNewOpcode Tu8) { _ = libc.Int32FromInt32(0) (*TVdbeOp)(unsafe.Pointer(_sqlite3VdbeGetOp(tls, p, addr))).Fopcode = iNewOpcode } func _sqlite3VdbeChangeP1(tls *libc.TLS, p uintptr, addr int32, val int32) { _ = libc.Int32FromInt32(0) (*TVdbeOp)(unsafe.Pointer(_sqlite3VdbeGetOp(tls, p, addr))).Fp1 = val } func _sqlite3VdbeChangeP2(tls *libc.TLS, p uintptr, addr int32, val int32) { _ = libc.Int32FromInt32(0) (*TVdbeOp)(unsafe.Pointer(_sqlite3VdbeGetOp(tls, p, addr))).Fp2 = val } func _sqlite3VdbeChangeP3(tls *libc.TLS, p uintptr, addr int32, val int32) { _ = libc.Int32FromInt32(0) (*TVdbeOp)(unsafe.Pointer(_sqlite3VdbeGetOp(tls, p, addr))).Fp3 = val } func _sqlite3VdbeChangeP5(tls *libc.TLS, p uintptr, p5 Tu16) { _ = libc.Int32FromInt32(0) if (*TVdbe)(unsafe.Pointer(p)).FnOp > 0 { (*(*TOp)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr((*TVdbe)(unsafe.Pointer(p)).FnOp-int32(1))*20))).Fp5 = p5 } } // C documentation // // /* // ** If the previous opcode is an OP_Column that delivers results // ** into register iDest, then add the OPFLAG_TYPEOFARG flag to that // ** opcode. // */ func _sqlite3VdbeTypeofColumn(tls *libc.TLS, p uintptr, iDest int32) { var pOp, p1 uintptr _, _ = pOp, p1 pOp = _sqlite3VdbeGetLastOp(tls, p) if (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 == iDest && int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Column) { p1 = pOp + 2 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(OPFLAG_TYPEOFARG)) } } // C documentation // // /* // ** Change the P2 operand of instruction addr so that it points to // ** the address of the next instruction to be coded. // */ func _sqlite3VdbeJumpHere(tls *libc.TLS, p uintptr, addr int32) { _sqlite3VdbeChangeP2(tls, p, addr, (*TVdbe)(unsafe.Pointer(p)).FnOp) } // C documentation // // /* // ** Change the P2 operand of the jump instruction at addr so that // ** the jump lands on the next opcode. Or if the jump instruction was // ** the previous opcode (and is thus a no-op) then simply back up // ** the next instruction counter by one slot so that the jump is // ** overwritten by the next inserted opcode. // ** // ** This routine is an optimization of sqlite3VdbeJumpHere() that // ** strives to omit useless byte-code like this: // ** // ** 7 Once 0 8 0 // ** 8 ... // */ func _sqlite3VdbeJumpHereOrPopInst(tls *libc.TLS, p uintptr, addr int32) { if addr == (*TVdbe)(unsafe.Pointer(p)).FnOp-int32(1) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TVdbe)(unsafe.Pointer(p)).FnOp-- } else { _sqlite3VdbeChangeP2(tls, p, addr, (*TVdbe)(unsafe.Pointer(p)).FnOp) } } // C documentation // // /* // ** If the input FuncDef structure is ephemeral, then free it. If // ** the FuncDef is not ephemeral, then do nothing. // */ func _freeEphemeralFunction(tls *libc.TLS, db uintptr, pDef uintptr) { _ = libc.Int32FromInt32(0) if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_EPHEM) != uint32(0) { _sqlite3DbNNFreeNN(tls, db, pDef) } } // C documentation // // /* // ** Delete a P4 value if necessary. // */ func _freeP4Mem(tls *libc.TLS, db uintptr, p uintptr) { if (*TMem)(unsafe.Pointer(p)).FszMalloc != 0 { _sqlite3DbFree(tls, db, (*TMem)(unsafe.Pointer(p)).FzMalloc) } _sqlite3DbNNFreeNN(tls, db, p) } func _freeP4FuncCtx(tls *libc.TLS, db uintptr, p uintptr) { _ = libc.Int32FromInt32(0) _freeEphemeralFunction(tls, db, (*Tsqlite3_context)(unsafe.Pointer(p)).FpFunc) _sqlite3DbNNFreeNN(tls, db, p) } func _freeP4(tls *libc.TLS, db uintptr, p4type int32, p4 uintptr) { _ = libc.Int32FromInt32(0) switch p4type { case -int32(15): _freeP4FuncCtx(tls, db, p4) case -int32(12): fallthrough case -int32(13): fallthrough case -int32(6): fallthrough case -int32(14): if p4 != 0 { _sqlite3DbNNFreeNN(tls, db, p4) } case -int32(8): if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0) { _sqlite3KeyInfoUnref(tls, p4) } case -int32(7): _freeEphemeralFunction(tls, db, p4) case -int32(10): if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0) { _sqlite3ValueFree(tls, p4) } else { _freeP4Mem(tls, db, p4) } case -int32(11): if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0) { _sqlite3VtabUnlock(tls, p4) } case -int32(16): if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0) { _sqlite3DeleteTable(tls, db, p4) } break } } // C documentation // // /* // ** Free the space allocated for aOp and any p4 values allocated for the // ** opcodes contained within. If aOp is not NULL it is assumed to contain // ** nOp entries. // */ func _vdbeFreeOpArray(tls *libc.TLS, db uintptr, aOp uintptr, nOp int32) { var pOp uintptr _ = pOp _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if aOp != 0 { pOp = aOp + uintptr(nOp-int32(1))*20 for int32(1) != 0 { /* Exit via break */ if int32((*TOp)(unsafe.Pointer(pOp)).Fp4type) <= -int32(6) { _freeP4(tls, db, int32((*TOp)(unsafe.Pointer(pOp)).Fp4type), *(*uintptr)(unsafe.Pointer(pOp + 16))) } if pOp == aOp { break } pOp -= 20 } _sqlite3DbNNFreeNN(tls, db, aOp) } } // C documentation // // /* // ** Link the SubProgram object passed as the second argument into the linked // ** list at Vdbe.pSubProgram. This list is used to delete all sub-program // ** objects when the VM is no longer required. // */ func _sqlite3VdbeLinkSubProgram(tls *libc.TLS, pVdbe uintptr, p uintptr) { (*TSubProgram)(unsafe.Pointer(p)).FpNext = (*TVdbe)(unsafe.Pointer(pVdbe)).FpProgram (*TVdbe)(unsafe.Pointer(pVdbe)).FpProgram = p } // C documentation // // /* // ** Return true if the given Vdbe has any SubPrograms. // */ func _sqlite3VdbeHasSubProgram(tls *libc.TLS, pVdbe uintptr) (r int32) { return libc.BoolInt32((*TVdbe)(unsafe.Pointer(pVdbe)).FpProgram != uintptr(0)) } // C documentation // // /* // ** Change the opcode at addr into OP_Noop // */ func _sqlite3VdbeChangeToNoop(tls *libc.TLS, p uintptr, addr int32) (r int32) { var pOp uintptr _ = pOp if (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FmallocFailed != 0 { return 0 } _ = libc.Int32FromInt32(0) pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(addr)*20 _freeP4(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fp4type), *(*uintptr)(unsafe.Pointer(pOp + 16))) (*TVdbeOp)(unsafe.Pointer(pOp)).Fp4type = P4_NOTUSED *(*uintptr)(unsafe.Pointer(pOp + 16)) = uintptr(0) (*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode = uint8(OP_Noop) return int32(1) } // C documentation // // /* // ** If the last opcode is "op" and it is not a jump destination, // ** then remove it. Return true if and only if an opcode was removed. // */ func _sqlite3VdbeDeletePriorOpcode(tls *libc.TLS, p uintptr, op Tu8) (r int32) { if (*TVdbe)(unsafe.Pointer(p)).FnOp > 0 && int32((*(*TOp)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr((*TVdbe)(unsafe.Pointer(p)).FnOp-int32(1))*20))).Fopcode) == int32(op) { return _sqlite3VdbeChangeToNoop(tls, p, (*TVdbe)(unsafe.Pointer(p)).FnOp-int32(1)) } else { return 0 } return r } // C documentation // // /* // ** Change the value of the P4 operand for a specific instruction. // ** This routine is useful when a large program is loaded from a // ** static array using sqlite3VdbeAddOpList but we want to make a // ** few minor changes to the program. // ** // ** If n>=0 then the P4 operand is dynamic, meaning that a copy of // ** the string is made into memory obtained from sqlite3_malloc(). // ** A value of n==0 means copy bytes of zP4 up to and including the // ** first null byte. If n>0 then copy n+1 bytes of zP4. // ** // ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points // ** to a string or structure that is guaranteed to exist for the lifetime of // ** the Vdbe. In these cases we can just copy the pointer. // ** // ** If addr<0 then change P4 on the most recently inserted instruction. // */ func _vdbeChangeP4Full(tls *libc.TLS, p uintptr, pOp uintptr, zP4 uintptr, n int32) { if (*TOp)(unsafe.Pointer(pOp)).Fp4type != 0 { _ = libc.Int32FromInt32(0) (*TOp)(unsafe.Pointer(pOp)).Fp4type = 0 *(*uintptr)(unsafe.Pointer(pOp + 16)) = uintptr(0) } if n < 0 { _sqlite3VdbeChangeP4(tls, p, (int32(pOp)-int32((*TVdbe)(unsafe.Pointer(p)).FaOp))/20, zP4, n) } else { if n == 0 { n = _sqlite3Strlen30(tls, zP4) } *(*uintptr)(unsafe.Pointer(pOp + 16)) = _sqlite3DbStrNDup(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, zP4, uint64(n)) (*TOp)(unsafe.Pointer(pOp)).Fp4type = int8(-libc.Int32FromInt32(6)) } } func _sqlite3VdbeChangeP4(tls *libc.TLS, p uintptr, addr int32, _zP4 uintptr, n int32) { bp := tls.Alloc(16) defer tls.Free(16) *(*uintptr)(unsafe.Pointer(bp)) = _zP4 var db, pOp uintptr _, _ = db, pOp _ = libc.Int32FromInt32(0) db = (*TVdbe)(unsafe.Pointer(p)).Fdb _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { if n != -int32(11) { _freeP4(tls, db, n, *(*uintptr)(unsafe.Pointer(bp))) } return } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if addr < 0 { addr = (*TVdbe)(unsafe.Pointer(p)).FnOp - int32(1) } pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(addr)*20 if n >= 0 || (*TOp)(unsafe.Pointer(pOp)).Fp4type != 0 { _vdbeChangeP4Full(tls, p, pOp, *(*uintptr)(unsafe.Pointer(bp)), n) return } if n == -int32(3) { /* Note: this cast is safe, because the origin data point was an int ** that was cast to a (const char *). */ *(*int32)(unsafe.Pointer(pOp + 16)) = int32(*(*uintptr)(unsafe.Pointer(bp))) (*TOp)(unsafe.Pointer(pOp)).Fp4type = int8(-libc.Int32FromInt32(3)) } else { if *(*uintptr)(unsafe.Pointer(bp)) != uintptr(0) { _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pOp + 16)) = *(*uintptr)(unsafe.Pointer(bp)) (*TOp)(unsafe.Pointer(pOp)).Fp4type = int8(n) if n == -int32(11) { _sqlite3VtabLock(tls, *(*uintptr)(unsafe.Pointer(bp))) } } } } // C documentation // // /* // ** Change the P4 operand of the most recently coded instruction // ** to the value defined by the arguments. This is a high-speed // ** version of sqlite3VdbeChangeP4(). // ** // ** The P4 operand must not have been previously defined. And the new // ** P4 must not be P4_INT32. Use sqlite3VdbeChangeP4() in either of // ** those cases. // */ func _sqlite3VdbeAppendP4(tls *libc.TLS, p uintptr, pP4 uintptr, n int32) { var pOp uintptr _ = pOp _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FmallocFailed != 0 { _freeP4(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, n, pP4) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr((*TVdbe)(unsafe.Pointer(p)).FnOp-int32(1))*20 _ = libc.Int32FromInt32(0) (*TVdbeOp)(unsafe.Pointer(pOp)).Fp4type = int8(n) *(*uintptr)(unsafe.Pointer(pOp + 16)) = pP4 } } // C documentation // // /* // ** Set the P4 on the most recently added opcode to the KeyInfo for the // ** index given. // */ func _sqlite3VdbeSetP4KeyInfo(tls *libc.TLS, pParse uintptr, pIdx uintptr) { var pKeyInfo, v uintptr _, _ = pKeyInfo, v v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pKeyInfo = _sqlite3KeyInfoOfIndex(tls, pParse, pIdx) if pKeyInfo != 0 { _sqlite3VdbeAppendP4(tls, v, pKeyInfo, -int32(8)) } } // C documentation // // /* // ** Return the opcode for a given address. The address must be non-negative. // ** See sqlite3VdbeGetLastOp() to get the most recently added opcode. // ** // ** If a memory allocation error has occurred prior to the calling of this // ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode // ** is readable but not writable, though it is cast to a writable value. // ** The return of a dummy opcode allows the call to continue functioning // ** after an OOM fault without having to check to see if the return from // ** this routine is a valid pointer. But because the dummy.opcode is 0, // ** dummy will never be written to. This is verified by code inspection and // ** by running with Valgrind. // */ func _sqlite3VdbeGetOp(tls *libc.TLS, p uintptr, addr int32) (r uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FmallocFailed != 0 { return uintptr(unsafe.Pointer(&_dummy)) } else { return (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(addr)*20 } return r } /* C89 specifies that the constant "dummy" will be initialized to all ** zeros, which is correct. MSVC generates a warning, nevertheless. */ var _dummy TVdbeOp // C documentation // // /* Return the most recently added opcode // */ func _sqlite3VdbeGetLastOp(tls *libc.TLS, p uintptr) (r uintptr) { return _sqlite3VdbeGetOp(tls, p, (*TVdbe)(unsafe.Pointer(p)).FnOp-int32(1)) } // C documentation // // /* // ** Compute a string that describes the P4 parameter for an opcode. // ** Use zTemp for any required temporary buffer space. // */ func _sqlite3VdbeDisplayP4(tls *libc.TLS, db uintptr, pOp uintptr) (r uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var ai, pColl, pColl1, pDef, pDef1, pKeyInfo, pMem, pVtab, zColl, zP4, v2, v3, v4 uintptr var i, n Tu32 var j, v6 int32 var _ /* x at bp+0 */ TStrAccum _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = ai, i, j, n, pColl, pColl1, pDef, pDef1, pKeyInfo, pMem, pVtab, zColl, zP4, v2, v3, v4, v6 zP4 = uintptr(0) _sqlite3StrAccumInit(tls, bp, uintptr(0), uintptr(0), 0, int32(SQLITE_MAX_LENGTH)) switch int32((*TOp)(unsafe.Pointer(pOp)).Fp4type) { case -int32(8): pKeyInfo = *(*uintptr)(unsafe.Pointer(pOp + 16)) _ = libc.Int32FromInt32(0) Xsqlite3_str_appendf(tls, bp, __ccgo_ts+5081, libc.VaList(bp+32, int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnKeyField))) j = 0 for { if !(j < int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnKeyField)) { break } pColl = *(*uintptr)(unsafe.Pointer(pKeyInfo + 20 + uintptr(j)*4)) if pColl != 0 { v2 = (*TCollSeq)(unsafe.Pointer(pColl)).FzName } else { v2 = __ccgo_ts + 1648 } zColl = v2 if libc.Xstrcmp(tls, zColl, __ccgo_ts+5086) == 0 { zColl = __ccgo_ts + 5093 } if int32(*(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags + uintptr(j))))&int32(KEYINFO_ORDER_DESC) != 0 { v3 = __ccgo_ts + 5074 } else { v3 = __ccgo_ts + 1648 } if int32(*(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags + uintptr(j))))&int32(KEYINFO_ORDER_BIGNULL) != 0 { v4 = __ccgo_ts + 5095 } else { v4 = __ccgo_ts + 1648 } Xsqlite3_str_appendf(tls, bp, __ccgo_ts+5098, libc.VaList(bp+32, v3, v4, zColl)) goto _1 _1: ; j++ } Xsqlite3_str_append(tls, bp, __ccgo_ts+5106, int32(1)) case -int32(2): pColl1 = *(*uintptr)(unsafe.Pointer(pOp + 16)) _ = libc.Int32FromInt32(0) Xsqlite3_str_appendf(tls, bp, __ccgo_ts+5122, libc.VaList(bp+32, (*TCollSeq)(unsafe.Pointer(pColl1)).FzName, _encnames[(*TCollSeq)(unsafe.Pointer(pColl1)).Fenc])) case -int32(7): pDef = *(*uintptr)(unsafe.Pointer(pOp + 16)) Xsqlite3_str_appendf(tls, bp, __ccgo_ts+5131, libc.VaList(bp+32, (*TFuncDef)(unsafe.Pointer(pDef)).FzName, int32((*TFuncDef)(unsafe.Pointer(pDef)).FnArg))) case -int32(15): pDef1 = (*Tsqlite3_context)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FpFunc Xsqlite3_str_appendf(tls, bp, __ccgo_ts+5131, libc.VaList(bp+32, (*TFuncDef)(unsafe.Pointer(pDef1)).FzName, int32((*TFuncDef)(unsafe.Pointer(pDef1)).FnArg))) case -int32(13): Xsqlite3_str_appendf(tls, bp, __ccgo_ts+1404, libc.VaList(bp+32, *(*Ti64)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))))) case -int32(3): Xsqlite3_str_appendf(tls, bp, __ccgo_ts+5138, libc.VaList(bp+32, *(*int32)(unsafe.Pointer(pOp + 16)))) case -int32(12): Xsqlite3_str_appendf(tls, bp, __ccgo_ts+1371, libc.VaList(bp+32, *(*float64)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))))) case -int32(10): pMem = *(*uintptr)(unsafe.Pointer(pOp + 16)) if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Str) != 0 { zP4 = (*TMem)(unsafe.Pointer(pMem)).Fz } else { if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+1404, libc.VaList(bp+32, *(*Ti64)(unsafe.Pointer(pMem)))) } else { if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Real) != 0 { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+1371, libc.VaList(bp+32, *(*float64)(unsafe.Pointer(pMem)))) } else { if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Null) != 0 { zP4 = __ccgo_ts + 1649 } else { _ = libc.Int32FromInt32(0) zP4 = __ccgo_ts + 5141 } } } } case -int32(11): pVtab = (*TVTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FpVtab Xsqlite3_str_appendf(tls, bp, __ccgo_ts+5148, libc.VaList(bp+32, pVtab)) case -int32(14): ai = *(*uintptr)(unsafe.Pointer(pOp + 16)) n = *(*Tu32)(unsafe.Pointer(ai)) /* The first element of an INTARRAY is always the ** count of the number of elements to follow */ i = uint32(1) for { if !(i <= n) { break } if i == uint32(1) { v6 = int32('[') } else { v6 = int32(',') } Xsqlite3_str_appendf(tls, bp, __ccgo_ts+5156, libc.VaList(bp+32, v6, *(*Tu32)(unsafe.Pointer(ai + uintptr(i)*4)))) goto _5 _5: ; i++ } Xsqlite3_str_append(tls, bp, __ccgo_ts+5161, int32(1)) case -int32(4): zP4 = __ccgo_ts + 5163 case -int32(5): zP4 = (*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FzName default: zP4 = *(*uintptr)(unsafe.Pointer(pOp + 16)) } if zP4 != 0 { Xsqlite3_str_appendall(tls, bp, zP4) } if int32((*(*TStrAccum)(unsafe.Pointer(bp))).FaccError)&int32(SQLITE_NOMEM) != 0 { _sqlite3OomFault(tls, db) } return _sqlite3StrAccumFinish(tls, bp) } var _encnames = [4]uintptr{ 0: __ccgo_ts + 5108, 1: __ccgo_ts + 5110, 2: __ccgo_ts + 5112, 3: __ccgo_ts + 5117, } // C documentation // // /* // ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. // ** // ** The prepared statements need to know in advance the complete set of // ** attached databases that will be use. A mask of these databases // ** is maintained in p->btreeMask. The p->lockMask value is the subset of // ** p->btreeMask of databases that will require a lock. // */ func _sqlite3VdbeUsesBtree(tls *libc.TLS, p uintptr, i int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*TyDbMask)(unsafe.Pointer(p + 156)) |= libc.Uint32FromInt32(1) << i if i != int32(1) && _sqlite3BtreeSharable(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FaDb + uintptr(i)*16))).FpBt) != 0 { *(*TyDbMask)(unsafe.Pointer(p + 160)) |= libc.Uint32FromInt32(1) << i } } // C documentation // // /* // ** If SQLite is compiled to support shared-cache mode and to be threadsafe, // ** this routine obtains the mutex associated with each BtShared structure // ** that may be accessed by the VM passed as an argument. In doing so it also // ** sets the BtShared.db member of each of the BtShared structures, ensuring // ** that the correct busy-handler callback is invoked if required. // ** // ** If SQLite is not threadsafe but does support shared-cache mode, then // ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables // ** of all of BtShared structures accessible via the database handle // ** associated with the VM. // ** // ** If SQLite is not threadsafe and does not support shared-cache mode, this // ** function is a no-op. // ** // ** The p->btreeMask field is a bitmask of all btrees that the prepared // ** statement p will ever use. Let N be the number of bits in p->btreeMask // ** corresponding to btrees that use shared cache. Then the runtime of // ** this routine is N*N. But as N is rarely more than 1, this should not // ** be a problem. // */ func _sqlite3VdbeEnter(tls *libc.TLS, p uintptr) { var aDb, db uintptr var i, nDb int32 _, _, _, _ = aDb, db, i, nDb if (*TVdbe)(unsafe.Pointer(p)).FlockMask == uint32(0) { return } /* The common case */ db = (*TVdbe)(unsafe.Pointer(p)).Fdb aDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb nDb = (*Tsqlite3)(unsafe.Pointer(db)).FnDb i = 0 for { if !(i < nDb) { break } if i != int32(1) && (*TVdbe)(unsafe.Pointer(p)).FlockMask&(libc.Uint32FromInt32(1)< 0 { for { (*TMem)(unsafe.Pointer(p)).Fflags = flags (*TMem)(unsafe.Pointer(p)).Fdb = db (*TMem)(unsafe.Pointer(p)).FszMalloc = 0 p += 40 goto _2 _2: ; N-- v1 = N if !(v1 > 0) { break } } } } // C documentation // // /* // ** Release auxiliary memory held in an array of N Mem elements. // ** // ** After this routine returns, all Mem elements in the array will still // ** be valid. Those Mem elements that were not holding auxiliary resources // ** will be unchanged. Mem elements which had something freed will be // ** set to MEM_Undefined. // */ func _releaseMemArray(tls *libc.TLS, p uintptr, N int32) { var db, pEnd, v1, v3 uintptr _, _, _, _ = db, pEnd, v1, v3 if p != 0 && N != 0 { pEnd = p + uintptr(N)*40 db = (*TMem)(unsafe.Pointer(p)).Fdb if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed != 0 { for { if (*TMem)(unsafe.Pointer(p)).FszMalloc != 0 { _sqlite3DbFree(tls, db, (*TMem)(unsafe.Pointer(p)).FzMalloc) } goto _2 _2: ; p += 40 v1 = p if !(v1 < pEnd) { break } } return } for { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* This block is really an inlined version of sqlite3VdbeMemRelease() ** that takes advantage of the fact that the memory cell value is ** being set to NULL after releasing any dynamic resources. ** ** The justification for duplicating code is that according to ** callgrind, this causes a certain test case to hit the CPU 4.7 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if ** sqlite3MemRelease() were called from here. With -O2, this jumps ** to 6.6 percent. The test case is inserting 1000 rows into a table ** with no indexes using a single prepared INSERT statement, bind() ** and reset(). Inserts are grouped into a transaction. */ if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 { _sqlite3VdbeMemRelease(tls, p) (*TMem)(unsafe.Pointer(p)).Fflags = uint16(MEM_Undefined) } else { if (*TMem)(unsafe.Pointer(p)).FszMalloc != 0 { _sqlite3DbNNFreeNN(tls, db, (*TMem)(unsafe.Pointer(p)).FzMalloc) (*TMem)(unsafe.Pointer(p)).FszMalloc = 0 (*TMem)(unsafe.Pointer(p)).Fflags = uint16(MEM_Undefined) } } goto _4 _4: ; p += 40 v3 = p if !(v3 < pEnd) { break } } } } // C documentation // // /* // ** This is a destructor on a Mem object (which is really an sqlite3_value) // ** that deletes the Frame object that is attached to it as a blob. // ** // ** This routine does not delete the Frame right away. It merely adds the // ** frame to a list of frames to be deleted when the Vdbe halts. // */ func _sqlite3VdbeFrameMemDel(tls *libc.TLS, pArg uintptr) { var pFrame uintptr _ = pFrame pFrame = pArg _ = libc.Int32FromInt32(0) (*TVdbeFrame)(unsafe.Pointer(pFrame)).FpParent = (*TVdbe)(unsafe.Pointer((*TVdbeFrame)(unsafe.Pointer(pFrame)).Fv)).FpDelFrame (*TVdbe)(unsafe.Pointer((*TVdbeFrame)(unsafe.Pointer(pFrame)).Fv)).FpDelFrame = pFrame } // C documentation // // /* // ** Locate the next opcode to be displayed in EXPLAIN or EXPLAIN // ** QUERY PLAN output. // ** // ** Return SQLITE_ROW on success. Return SQLITE_DONE if there are no // ** more opcodes to be displayed. // */ func _sqlite3VdbeNextOpcode(tls *libc.TLS, p uintptr, pSub uintptr, eMode int32, piPc uintptr, piAddr uintptr, paOp uintptr) (r int32) { var aOp, apSub uintptr var i, iPc, j, j1, nByte, nRow, nSub, rc, v2, v5 int32 _, _, _, _, _, _, _, _, _, _, _, _ = aOp, apSub, i, iPc, j, j1, nByte, nRow, nSub, rc, v2, v5 /* Stop when row count reaches this */ nSub = 0 /* Number of sub-vdbes seen so far */ apSub = uintptr(0) /* Next instruction address */ rc = SQLITE_OK /* Result code */ aOp = uintptr(0) /* Rowid. Copy of value in *piPc */ /* When the number of output rows reaches nRow, that means the ** listing has finished and sqlite3_step() should return SQLITE_DONE. ** nRow is the sum of the number of rows in the main program, plus ** the sum of the number of rows in all trigger subprograms encountered ** so far. The nRow value will increase as new trigger subprograms are ** encountered, but p->pc will eventually catch up to nRow. */ nRow = (*TVdbe)(unsafe.Pointer(p)).FnOp if pSub != uintptr(0) { if int32((*TMem)(unsafe.Pointer(pSub)).Fflags)&int32(MEM_Blob) != 0 { /* pSub is initiallly NULL. It is initialized to a BLOB by ** the P4_SUBPROGRAM processing logic below */ nSub = int32(uint32((*TMem)(unsafe.Pointer(pSub)).Fn) / uint32(4)) apSub = (*TMem)(unsafe.Pointer(pSub)).Fz } i = 0 for { if !(i < nSub) { break } nRow += (*TSubProgram)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(apSub + uintptr(i)*4)))).FnOp goto _1 _1: ; i++ } } iPc = *(*int32)(unsafe.Pointer(piPc)) for int32(1) != 0 { /* Loop exits via break */ v2 = iPc iPc++ i = v2 if i >= nRow { (*TVdbe)(unsafe.Pointer(p)).Frc = SQLITE_OK rc = int32(SQLITE_DONE) break } if i < (*TVdbe)(unsafe.Pointer(p)).FnOp { /* The rowid is small enough that we are still in the ** main program. */ aOp = (*TVdbe)(unsafe.Pointer(p)).FaOp } else { i -= (*TVdbe)(unsafe.Pointer(p)).FnOp _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) j = 0 for { if !(i >= (*TSubProgram)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(apSub + uintptr(j)*4)))).FnOp) { break } i -= (*TSubProgram)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(apSub + uintptr(j)*4)))).FnOp _ = libc.Int32FromInt32(0) goto _3 _3: ; j++ } aOp = (*TSubProgram)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(apSub + uintptr(j)*4)))).FaOp } /* When an OP_Program opcode is encounter (the only opcode that has ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms ** kept in p->aMem[9].z to hold the new program - assuming this subprogram ** has not already been seen. */ if pSub != uintptr(0) && int32((*(*TOp)(unsafe.Pointer(aOp + uintptr(i)*20))).Fp4type) == -int32(4) { nByte = int32(uint32(nSub+libc.Int32FromInt32(1)) * uint32(4)) j1 = 0 for { if !(j1 < nSub) { break } if *(*uintptr)(unsafe.Pointer(apSub + uintptr(j1)*4)) == *(*uintptr)(unsafe.Pointer(aOp + uintptr(i)*20 + 16)) { break } goto _4 _4: ; j1++ } if j1 == nSub { (*TVdbe)(unsafe.Pointer(p)).Frc = _sqlite3VdbeMemGrow(tls, pSub, nByte, libc.BoolInt32(nSub != 0)) if (*TVdbe)(unsafe.Pointer(p)).Frc != SQLITE_OK { rc = int32(SQLITE_ERROR) break } apSub = (*TMem)(unsafe.Pointer(pSub)).Fz v5 = nSub nSub++ *(*uintptr)(unsafe.Pointer(apSub + uintptr(v5)*4)) = *(*uintptr)(unsafe.Pointer(aOp + uintptr(i)*20 + 16)) (*TMem)(unsafe.Pointer(pSub)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pSub)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Blob)) (*TMem)(unsafe.Pointer(pSub)).Fn = int32(uint32(nSub) * uint32(4)) nRow += (*TSubProgram)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aOp + uintptr(i)*20 + 16)))).FnOp } } if eMode == 0 { break } _ = libc.Int32FromInt32(0) if int32((*(*TOp)(unsafe.Pointer(aOp + uintptr(i)*20))).Fopcode) == int32(OP_Explain) { break } if int32((*(*TOp)(unsafe.Pointer(aOp + uintptr(i)*20))).Fopcode) == int32(OP_Init) && iPc > int32(1) { break } } *(*int32)(unsafe.Pointer(piPc)) = iPc *(*int32)(unsafe.Pointer(piAddr)) = i *(*uintptr)(unsafe.Pointer(paOp)) = aOp return rc } // C documentation // // /* // ** Delete a VdbeFrame object and its contents. VdbeFrame objects are // ** allocated by the OP_Program opcode in sqlite3VdbeExec(). // */ func _sqlite3VdbeFrameDelete(tls *libc.TLS, p uintptr) { var aMem, apCsr uintptr var i int32 _, _, _ = aMem, apCsr, i aMem = p + uintptr((libc.Uint32FromInt64(88)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))) apCsr = aMem + uintptr((*TVdbeFrame)(unsafe.Pointer(p)).FnChildMem)*40 _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*TVdbeFrame)(unsafe.Pointer(p)).FnChildCsr) { break } if *(*uintptr)(unsafe.Pointer(apCsr + uintptr(i)*4)) != 0 { _sqlite3VdbeFreeCursorNN(tls, (*TVdbeFrame)(unsafe.Pointer(p)).Fv, *(*uintptr)(unsafe.Pointer(apCsr + uintptr(i)*4))) } goto _1 _1: ; i++ } _releaseMemArray(tls, aMem, (*TVdbeFrame)(unsafe.Pointer(p)).FnChildMem) _sqlite3VdbeDeleteAuxData(tls, (*TVdbe)(unsafe.Pointer((*TVdbeFrame)(unsafe.Pointer(p)).Fv)).Fdb, p+40, -int32(1), 0) _sqlite3DbFree(tls, (*TVdbe)(unsafe.Pointer((*TVdbeFrame)(unsafe.Pointer(p)).Fv)).Fdb, p) } // C documentation // // /* // ** Give a listing of the program in the virtual machine. // ** // ** The interface is the same as sqlite3VdbeExec(). But instead of // ** running the code, it invokes the callback once for each instruction. // ** This feature is used to implement "EXPLAIN". // ** // ** When p->explain==1, each instruction is listed. When // ** p->explain==2, only OP_Explain instructions are listed and these // ** are shown in a different format. p->explain==2 is used to implement // ** EXPLAIN QUERY PLAN. // ** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers // ** are also shown, so that the boundaries between the main program and // ** each trigger are clear. // ** // ** When p->explain==1, first the main program is listed, then each of // ** the trigger subprograms are listed one by one. // */ func _sqlite3VdbeList(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bListSubprogs, rc int32 var db, pMem, pOp, pSub, zP4 uintptr var _ /* aOp at bp+4 */ uintptr var _ /* i at bp+0 */ int32 _, _, _, _, _, _, _ = bListSubprogs, db, pMem, pOp, pSub, rc, zP4 pSub = uintptr(0) /* Memory cell hold array of subprogs */ db = (*TVdbe)(unsafe.Pointer(p)).Fdb /* Loop counter */ rc = SQLITE_OK /* Return code */ pMem = (*TVdbe)(unsafe.Pointer(p)).FaMem + 1*40 /* First Mem of result set */ bListSubprogs = libc.BoolInt32(int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2)) == int32(1) || (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_TriggerEQP) != uint64(0)) /* Current opcode */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Even though this opcode does not use dynamic strings for ** the result, result columns may become dynamic if the user calls ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. */ _releaseMemArray(tls, pMem, int32(8)) if (*TVdbe)(unsafe.Pointer(p)).Frc == int32(SQLITE_NOMEM) { /* This happens if a malloc() inside a call to sqlite3_column_text() or ** sqlite3_column_text16() failed. */ _sqlite3OomFault(tls, db) return int32(SQLITE_ERROR) } if bListSubprogs != 0 { /* The first 8 memory cells are used for the result set. So we will ** commandeer the 9th cell to use as storage for an array of pointers ** to trigger subprograms. The VDBE is guaranteed to have at least 9 ** cells. */ _ = libc.Int32FromInt32(0) pSub = (*TVdbe)(unsafe.Pointer(p)).FaMem + 9*40 } else { pSub = uintptr(0) } /* Figure out which opcode is next to display */ rc = _sqlite3VdbeNextOpcode(tls, p, pSub, libc.BoolInt32(int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2)) == int32(2)), p+32, bp, bp+4) if rc == SQLITE_OK { pOp = *(*uintptr)(unsafe.Pointer(bp + 4)) + uintptr(*(*int32)(unsafe.Pointer(bp)))*20 if libc.AtomicLoadNInt32(db+312, libc.Int32FromInt32(__ATOMIC_RELAXED)) != 0 { (*TVdbe)(unsafe.Pointer(p)).Frc = int32(SQLITE_INTERRUPT) rc = int32(SQLITE_ERROR) _sqlite3VdbeError(tls, p, _sqlite3ErrStr(tls, (*TVdbe)(unsafe.Pointer(p)).Frc), 0) } else { zP4 = _sqlite3VdbeDisplayP4(tls, db, pOp) if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2)) == int32(2) { _sqlite3VdbeMemSetInt64(tls, pMem, int64((*TOp)(unsafe.Pointer(pOp)).Fp1)) _sqlite3VdbeMemSetInt64(tls, pMem+uintptr(1)*40, int64((*TOp)(unsafe.Pointer(pOp)).Fp2)) _sqlite3VdbeMemSetInt64(tls, pMem+uintptr(2)*40, int64((*TOp)(unsafe.Pointer(pOp)).Fp3)) _sqlite3VdbeMemSetStr(tls, pMem+uintptr(3)*40, zP4, int64(-int32(1)), uint8(SQLITE_UTF8), __ccgo_fp(Xsqlite3_free)) _ = libc.Int32FromInt32(0) } else { _sqlite3VdbeMemSetInt64(tls, pMem+uintptr(0)*40, int64(*(*int32)(unsafe.Pointer(bp)))) _sqlite3VdbeMemSetStr(tls, pMem+uintptr(1)*40, _sqlite3OpcodeName(tls, int32((*TOp)(unsafe.Pointer(pOp)).Fopcode)), int64(-int32(1)), uint8(SQLITE_UTF8), libc.UintptrFromInt32(0)) _sqlite3VdbeMemSetInt64(tls, pMem+uintptr(2)*40, int64((*TOp)(unsafe.Pointer(pOp)).Fp1)) _sqlite3VdbeMemSetInt64(tls, pMem+uintptr(3)*40, int64((*TOp)(unsafe.Pointer(pOp)).Fp2)) _sqlite3VdbeMemSetInt64(tls, pMem+uintptr(4)*40, int64((*TOp)(unsafe.Pointer(pOp)).Fp3)) /* pMem+5 for p4 is done last */ _sqlite3VdbeMemSetInt64(tls, pMem+uintptr(6)*40, int64((*TOp)(unsafe.Pointer(pOp)).Fp5)) _sqlite3VdbeMemSetNull(tls, pMem+uintptr(7)*40) _sqlite3VdbeMemSetStr(tls, pMem+uintptr(5)*40, zP4, int64(-int32(1)), uint8(SQLITE_UTF8), __ccgo_fp(Xsqlite3_free)) _ = libc.Int32FromInt32(0) } (*TVdbe)(unsafe.Pointer(p)).FpResultRow = pMem if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { (*TVdbe)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) rc = int32(SQLITE_ERROR) } else { (*TVdbe)(unsafe.Pointer(p)).Frc = SQLITE_OK rc = int32(SQLITE_ROW) } } } return rc } /* An instance of this object describes bulk memory available for use ** by subcomponents of a prepared statement. Space is allocated out ** of a ReusableSpace object by the allocSpace() routine below. */ type TReusableSpace = struct { F__ccgo_align [0]uint32 FpSpace uintptr F__ccgo_align1 [4]byte FnFree Tsqlite3_int64 FnNeeded Tsqlite3_int64 } type ReusableSpace = TReusableSpace // C documentation // // /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf // ** from the ReusableSpace object. Return a pointer to the allocated // ** memory on success. If insufficient memory is available in the // ** ReusableSpace object, increase the ReusableSpace.nNeeded // ** value by the amount needed and return NULL. // ** // ** If pBuf is not initially NULL, that means that the memory has already // ** been allocated by a prior call to this routine, so just return a copy // ** of pBuf and leave ReusableSpace unchanged. // ** // ** This allocator is employed to repurpose unused slots at the end of the // ** opcode array of prepared state for other memory needs of the prepared // ** statement. // */ func _allocSpace(tls *libc.TLS, p uintptr, pBuf uintptr, nByte Tsqlite3_int64) (r uintptr) { _ = libc.Int32FromInt32(0) if pBuf == uintptr(0) { nByte = (nByte + libc.Int64FromInt32(7)) & int64(^libc.Int32FromInt32(7)) if nByte <= (*TReusableSpace)(unsafe.Pointer(p)).FnFree { *(*Tsqlite3_int64)(unsafe.Pointer(p + 8)) -= nByte pBuf = (*TReusableSpace)(unsafe.Pointer(p)).FpSpace + uintptr((*TReusableSpace)(unsafe.Pointer(p)).FnFree) } else { *(*Tsqlite3_int64)(unsafe.Pointer(p + 16)) += nByte } } _ = libc.Int32FromInt32(0) return pBuf } // C documentation // // /* // ** Rewind the VDBE back to the beginning in preparation for // ** running it. // */ func _sqlite3VdbeRewind(tls *libc.TLS, p uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* There should be at least one opcode. */ _ = libc.Int32FromInt32(0) (*TVdbe)(unsafe.Pointer(p)).FeVdbeState = uint8(VDBE_READY_STATE) (*TVdbe)(unsafe.Pointer(p)).Fpc = -int32(1) (*TVdbe)(unsafe.Pointer(p)).Frc = SQLITE_OK (*TVdbe)(unsafe.Pointer(p)).FerrorAction = uint8(OE_Abort) (*TVdbe)(unsafe.Pointer(p)).FnChange = 0 (*TVdbe)(unsafe.Pointer(p)).FcacheCtr = uint32(1) (*TVdbe)(unsafe.Pointer(p)).FminWriteFileFormat = uint8(255) (*TVdbe)(unsafe.Pointer(p)).FiStatement = 0 (*TVdbe)(unsafe.Pointer(p)).FnFkConstraint = 0 } // C documentation // // /* // ** Prepare a virtual machine for execution for the first time after // ** creating the virtual machine. This involves things such // ** as allocating registers and initializing the program counter. // ** After the VDBE has be prepped, it can be executed by one or more // ** calls to sqlite3VdbeExec(). // ** // ** This function may be called exactly once on each virtual machine. // ** After this routine is called the VM has been "packaged" and is ready // ** to run. After this routine is called, further calls to // ** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects // ** the Vdbe from the Parse object that helped generate it so that the // ** the Vdbe becomes an independent entity and the Parse object can be // ** destroyed. // ** // ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back // ** to its initial state after it has been run. // */ func _sqlite3VdbeMakeReady(tls *libc.TLS, p uintptr, pParse uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var db, v1 uintptr var n, nCursor, nMem, nVar int32 var _ /* nArg at bp+0 */ int32 var _ /* x at bp+8 */ TReusableSpace _, _, _, _, _, _ = db, n, nCursor, nMem, nVar, v1 /* Reusable bulk memory */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TVdbe)(unsafe.Pointer(p)).FpVList = (*TParse)(unsafe.Pointer(pParse)).FpVList (*TParse)(unsafe.Pointer(pParse)).FpVList = uintptr(0) db = (*TVdbe)(unsafe.Pointer(p)).Fdb _ = libc.Int32FromInt32(0) nVar = int32((*TParse)(unsafe.Pointer(pParse)).FnVar) nMem = (*TParse)(unsafe.Pointer(pParse)).FnMem nCursor = (*TParse)(unsafe.Pointer(pParse)).FnTab *(*int32)(unsafe.Pointer(bp)) = (*TParse)(unsafe.Pointer(pParse)).FnMaxArg /* Each cursor uses a memory cell. The first cursor (cursor 0) can ** use aMem[0] which is not otherwise used by the VDBE program. Allocate ** space at the end of aMem[] for cursors 1 and greater. ** See also: allocateCursor(). */ nMem += nCursor if nCursor == 0 && nMem > 0 { nMem++ } /* Space for aMem[0] even if not used */ /* Figure out how much reusable memory is available at the end of the ** opcode array. This extra memory will be reallocated for other elements ** of the prepared statement. */ n = int32((libc.Uint32FromInt64(20)*uint32((*TVdbe)(unsafe.Pointer(p)).FnOp) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7))) /* Bytes of opcode memory used */ (*(*TReusableSpace)(unsafe.Pointer(bp + 8))).FpSpace = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(n) /* Unused opcode memory */ _ = libc.Int32FromInt32(0) (*(*TReusableSpace)(unsafe.Pointer(bp + 8))).FnFree = int64(((*TParse)(unsafe.Pointer(pParse)).FszOpAlloc - n) & ^libc.Int32FromInt32(7)) /* Bytes of unused memory */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _resolveP2Values(tls, p, bp) libc.SetBitFieldPtr16Uint32(p+152, uint32(libc.BoolUint8((*TParse)(unsafe.Pointer(pParse)).FisMultiWrite != 0 && (*TParse)(unsafe.Pointer(pParse)).FmayAbort != 0)), 5, 0x20) if (*TParse)(unsafe.Pointer(pParse)).Fexplain != 0 { if nMem < int32(10) { nMem = int32(10) } libc.SetBitFieldPtr16Uint32(p+152, uint32((*TParse)(unsafe.Pointer(pParse)).Fexplain), 2, 0xc) (*TVdbe)(unsafe.Pointer(p)).FnResColumn = uint16(int32(12) - int32(4)*int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2))) } libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 0, 0x3) /* Memory for registers, parameters, cursor, etc, is allocated in one or two ** passes. On the first pass, we try to reuse unused memory at the ** end of the opcode array. If we are unable to satisfy all memory ** requirements by reusing the opcode array tail, then the second ** pass will fill in the remainder using a fresh memory allocation. ** ** This two-pass approach that reuses as much memory as possible from ** the leftover memory at the end of the opcode array. This can significantly ** reduce the amount of memory held by a prepared statement. */ (*(*TReusableSpace)(unsafe.Pointer(bp + 8))).FnNeeded = 0 (*TVdbe)(unsafe.Pointer(p)).FaMem = _allocSpace(tls, bp+8, uintptr(0), int64(uint32(nMem)*uint32(40))) (*TVdbe)(unsafe.Pointer(p)).FaVar = _allocSpace(tls, bp+8, uintptr(0), int64(uint32(nVar)*uint32(40))) (*TVdbe)(unsafe.Pointer(p)).FapArg = _allocSpace(tls, bp+8, uintptr(0), int64(uint32(*(*int32)(unsafe.Pointer(bp)))*uint32(4))) (*TVdbe)(unsafe.Pointer(p)).FapCsr = _allocSpace(tls, bp+8, uintptr(0), int64(uint32(nCursor)*uint32(4))) if (*(*TReusableSpace)(unsafe.Pointer(bp + 8))).FnNeeded != 0 { v1 = _sqlite3DbMallocRawNN(tls, db, uint64((*(*TReusableSpace)(unsafe.Pointer(bp + 8))).FnNeeded)) (*TVdbe)(unsafe.Pointer(p)).FpFree = v1 (*(*TReusableSpace)(unsafe.Pointer(bp + 8))).FpSpace = v1 (*(*TReusableSpace)(unsafe.Pointer(bp + 8))).FnFree = (*(*TReusableSpace)(unsafe.Pointer(bp + 8))).FnNeeded if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { (*TVdbe)(unsafe.Pointer(p)).FaMem = _allocSpace(tls, bp+8, (*TVdbe)(unsafe.Pointer(p)).FaMem, int64(uint32(nMem)*uint32(40))) (*TVdbe)(unsafe.Pointer(p)).FaVar = _allocSpace(tls, bp+8, (*TVdbe)(unsafe.Pointer(p)).FaVar, int64(uint32(nVar)*uint32(40))) (*TVdbe)(unsafe.Pointer(p)).FapArg = _allocSpace(tls, bp+8, (*TVdbe)(unsafe.Pointer(p)).FapArg, int64(uint32(*(*int32)(unsafe.Pointer(bp)))*uint32(4))) (*TVdbe)(unsafe.Pointer(p)).FapCsr = _allocSpace(tls, bp+8, (*TVdbe)(unsafe.Pointer(p)).FapCsr, int64(uint32(nCursor)*uint32(4))) } } if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { (*TVdbe)(unsafe.Pointer(p)).FnVar = 0 (*TVdbe)(unsafe.Pointer(p)).FnCursor = 0 (*TVdbe)(unsafe.Pointer(p)).FnMem = 0 } else { (*TVdbe)(unsafe.Pointer(p)).FnCursor = nCursor (*TVdbe)(unsafe.Pointer(p)).FnVar = int16(nVar) _initMemArray(tls, (*TVdbe)(unsafe.Pointer(p)).FaVar, nVar, db, uint16(MEM_Null)) (*TVdbe)(unsafe.Pointer(p)).FnMem = nMem _initMemArray(tls, (*TVdbe)(unsafe.Pointer(p)).FaMem, nMem, db, uint16(MEM_Undefined)) libc.Xmemset(tls, (*TVdbe)(unsafe.Pointer(p)).FapCsr, 0, uint32(nCursor)*uint32(4)) } _sqlite3VdbeRewind(tls, p) } // C documentation // // /* // ** Close a VDBE cursor and release all the resources that cursor // ** happens to hold. // */ func _sqlite3VdbeFreeCursor(tls *libc.TLS, p uintptr, pCx uintptr) { if pCx != 0 { _sqlite3VdbeFreeCursorNN(tls, p, pCx) } } func _freeCursorWithCache(tls *libc.TLS, p uintptr, pCx uintptr) { var pCache uintptr _ = pCache pCache = (*TVdbeCursor)(unsafe.Pointer(pCx)).FpCache _ = libc.Int32FromInt32(0) libc.SetBitFieldPtr8Uint32(pCx+8, libc.Uint32FromInt32(0), 4, 0x10) (*TVdbeCursor)(unsafe.Pointer(pCx)).FpCache = uintptr(0) if (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FpCValue != 0 { _sqlite3RCStrUnref(tls, (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FpCValue) (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FpCValue = uintptr(0) } _sqlite3DbFree(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, pCache) _sqlite3VdbeFreeCursorNN(tls, p, pCx) } func _sqlite3VdbeFreeCursorNN(tls *libc.TLS, p uintptr, pCx uintptr) { var pModule, pVCur uintptr _, _ = pModule, pVCur if int32(TBool(*(*uint8)(unsafe.Pointer(pCx + 8))&0x10>>4)) != 0 { _freeCursorWithCache(tls, p, pCx) return } switch int32((*TVdbeCursor)(unsafe.Pointer(pCx)).FeCurType) { case int32(CURTYPE_SORTER): _sqlite3VdbeSorterClose(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, pCx) case CURTYPE_BTREE: _ = libc.Int32FromInt32(0) _sqlite3BtreeCloseCursor(tls, *(*uintptr)(unsafe.Pointer(pCx + 36))) case int32(CURTYPE_VTAB): pVCur = *(*uintptr)(unsafe.Pointer(pCx + 36)) pModule = (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVCur)).FpVtab)).FpModule _ = libc.Int32FromInt32(0) (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVCur)).FpVtab)).FnRef-- (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule)).FxClose})))(tls, pVCur) break } } // C documentation // // /* // ** Close all cursors in the current frame. // */ func _closeCursorsInFrame(tls *libc.TLS, p uintptr) { var i int32 var pC uintptr _, _ = i, pC i = 0 for { if !(i < (*TVdbe)(unsafe.Pointer(p)).FnCursor) { break } pC = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr(i)*4)) if pC != 0 { _sqlite3VdbeFreeCursorNN(tls, p, pC) *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr(i)*4)) = uintptr(0) } goto _1 _1: ; i++ } } // C documentation // // /* // ** Copy the values stored in the VdbeFrame structure to its Vdbe. This // ** is used, for example, when a trigger sub-program is halted to restore // ** control to the main program. // */ func _sqlite3VdbeFrameRestore(tls *libc.TLS, pFrame uintptr) (r int32) { var v uintptr _ = v v = (*TVdbeFrame)(unsafe.Pointer(pFrame)).Fv _closeCursorsInFrame(tls, v) (*TVdbe)(unsafe.Pointer(v)).FaOp = (*TVdbeFrame)(unsafe.Pointer(pFrame)).FaOp (*TVdbe)(unsafe.Pointer(v)).FnOp = (*TVdbeFrame)(unsafe.Pointer(pFrame)).FnOp (*TVdbe)(unsafe.Pointer(v)).FaMem = (*TVdbeFrame)(unsafe.Pointer(pFrame)).FaMem (*TVdbe)(unsafe.Pointer(v)).FnMem = (*TVdbeFrame)(unsafe.Pointer(pFrame)).FnMem (*TVdbe)(unsafe.Pointer(v)).FapCsr = (*TVdbeFrame)(unsafe.Pointer(pFrame)).FapCsr (*TVdbe)(unsafe.Pointer(v)).FnCursor = (*TVdbeFrame)(unsafe.Pointer(pFrame)).FnCursor (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(v)).Fdb)).FlastRowid = (*TVdbeFrame)(unsafe.Pointer(pFrame)).FlastRowid (*TVdbe)(unsafe.Pointer(v)).FnChange = (*TVdbeFrame)(unsafe.Pointer(pFrame)).FnChange (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(v)).Fdb)).FnChange = (*TVdbeFrame)(unsafe.Pointer(pFrame)).FnDbChange _sqlite3VdbeDeleteAuxData(tls, (*TVdbe)(unsafe.Pointer(v)).Fdb, v+228, -int32(1), 0) (*TVdbe)(unsafe.Pointer(v)).FpAuxData = (*TVdbeFrame)(unsafe.Pointer(pFrame)).FpAuxData (*TVdbeFrame)(unsafe.Pointer(pFrame)).FpAuxData = uintptr(0) return (*TVdbeFrame)(unsafe.Pointer(pFrame)).Fpc } // C documentation // // /* // ** Close all cursors. // ** // ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory // ** cell array. This is necessary as the memory cell array may contain // ** pointers to VdbeFrame objects, which may in turn contain pointers to // ** open cursors. // */ func _closeAllCursors(tls *libc.TLS, p uintptr) { var pDel, pFrame uintptr _, _ = pDel, pFrame if (*TVdbe)(unsafe.Pointer(p)).FpFrame != 0 { pFrame = (*TVdbe)(unsafe.Pointer(p)).FpFrame for { if !((*TVdbeFrame)(unsafe.Pointer(pFrame)).FpParent != 0) { break } goto _1 _1: ; pFrame = (*TVdbeFrame)(unsafe.Pointer(pFrame)).FpParent } _sqlite3VdbeFrameRestore(tls, pFrame) (*TVdbe)(unsafe.Pointer(p)).FpFrame = uintptr(0) (*TVdbe)(unsafe.Pointer(p)).FnFrame = 0 } _ = libc.Int32FromInt32(0) _closeCursorsInFrame(tls, p) _releaseMemArray(tls, (*TVdbe)(unsafe.Pointer(p)).FaMem, (*TVdbe)(unsafe.Pointer(p)).FnMem) for (*TVdbe)(unsafe.Pointer(p)).FpDelFrame != 0 { pDel = (*TVdbe)(unsafe.Pointer(p)).FpDelFrame (*TVdbe)(unsafe.Pointer(p)).FpDelFrame = (*TVdbeFrame)(unsafe.Pointer(pDel)).FpParent _sqlite3VdbeFrameDelete(tls, pDel) } /* Delete any auxdata allocations made by the VM */ if (*TVdbe)(unsafe.Pointer(p)).FpAuxData != 0 { _sqlite3VdbeDeleteAuxData(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, p+228, -int32(1), 0) } _ = libc.Int32FromInt32(0) } // C documentation // // /* // ** Set the number of result columns that will be returned by this SQL // ** statement. This is now set at compile time, rather than during // ** execution of the vdbe program so that sqlite3_column_count() can // ** be called on an SQL statement before sqlite3_step(). // */ func _sqlite3VdbeSetNumCols(tls *libc.TLS, p uintptr, nResColumn int32) { var db uintptr var n int32 var v1 Tu16 _, _, _ = db, n, v1 db = (*TVdbe)(unsafe.Pointer(p)).Fdb if (*TVdbe)(unsafe.Pointer(p)).FnResAlloc != 0 { _releaseMemArray(tls, (*TVdbe)(unsafe.Pointer(p)).FaColName, int32((*TVdbe)(unsafe.Pointer(p)).FnResAlloc)*int32(COLNAME_N)) _sqlite3DbFree(tls, db, (*TVdbe)(unsafe.Pointer(p)).FaColName) } n = nResColumn * int32(COLNAME_N) v1 = uint16(nResColumn) (*TVdbe)(unsafe.Pointer(p)).FnResAlloc = v1 (*TVdbe)(unsafe.Pointer(p)).FnResColumn = v1 (*TVdbe)(unsafe.Pointer(p)).FaColName = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(40)*uint32(n))) if (*TVdbe)(unsafe.Pointer(p)).FaColName == uintptr(0) { return } _initMemArray(tls, (*TVdbe)(unsafe.Pointer(p)).FaColName, n, db, uint16(MEM_Null)) } // C documentation // // /* // ** Set the name of the idx'th column to be returned by the SQL statement. // ** zName must be a pointer to a nul terminated string. // ** // ** This call must be made after a call to sqlite3VdbeSetNumCols(). // ** // ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC // ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed // ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed. // */ func _sqlite3VdbeSetColName(tls *libc.TLS, p uintptr, idx int32, var1 int32, zName uintptr, xDel uintptr) (r int32) { var pColName uintptr var rc int32 _, _ = pColName, rc _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FmallocFailed != 0 { _ = libc.Int32FromInt32(0) return int32(SQLITE_NOMEM) } _ = libc.Int32FromInt32(0) pColName = (*TVdbe)(unsafe.Pointer(p)).FaColName + uintptr(idx+var1*int32((*TVdbe)(unsafe.Pointer(p)).FnResAlloc))*40 rc = _sqlite3VdbeMemSetStr(tls, pColName, zName, int64(-int32(1)), uint8(SQLITE_UTF8), xDel) _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** A read or write transaction may or may not be active on database handle // ** db. If a transaction is active, commit it. If there is a // ** write-transaction spanning more than one database file, this routine // ** takes care of the super-journal trickery. // */ func _vdbeCommit(tls *libc.TLS, db uintptr, p uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var i, nMainFile, nTrans, needXcommit, rc, retryCount, v5 int32 var offset Ti64 var pBt, pBt1, pBt2, pBt3, pBt4, pBt5, pPager, pVfs, zFile, zMainFile, zSuper uintptr var v6 bool var _ /* iRandom at bp+8 */ Tu32 var _ /* pSuperJrnl at bp+0 */ uintptr var _ /* res at bp+4 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, nMainFile, nTrans, needXcommit, offset, pBt, pBt1, pBt2, pBt3, pBt4, pBt5, pPager, pVfs, rc, retryCount, zFile, zMainFile, zSuper, v5, v6 nTrans = 0 /* Number of databases with an active write-transaction ** that are candidates for a two-phase commit using a ** super-journal */ rc = SQLITE_OK needXcommit = 0 /* Before doing anything else, call the xSync() callback for any ** virtual module tables written in this transaction. This has to ** be done before determining whether a super-journal file is ** required, as an xSync() callback may add an attached database ** to the transaction. */ rc = _sqlite3VtabSync(tls, db, p) /* This loop determines (a) if the commit hook should be invoked and ** (b) how many database files have open write transactions, not ** including the temp database. (b) is important because if more than ** one database file has an open write transaction, a super-journal ** file is required for an atomic commit. */ i = 0 for { if !(rc == SQLITE_OK && i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt if _sqlite3BtreeTxnState(tls, pBt) == int32(SQLITE_TXN_WRITE) { needXcommit = int32(1) _sqlite3BtreeEnter(tls, pBt) pPager = _sqlite3BtreePager(tls, pBt) if int32((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).Fsafety_level) != int32(PAGER_SYNCHRONOUS_OFF) && _aMJNeeded[_sqlite3PagerGetJournalMode(tls, pPager)] != 0 && _sqlite3PagerIsMemdb(tls, pPager) == 0 { _ = libc.Int32FromInt32(0) nTrans++ } rc = _sqlite3PagerExclusiveLock(tls, pPager) _sqlite3BtreeLeave(tls, pBt) } goto _1 _1: ; i++ } if rc != SQLITE_OK { return rc } /* If there are any write-transactions at all, invoke the commit hook */ if needXcommit != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FxCommitCallback != 0 { rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxCommitCallback})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpCommitArg) if rc != 0 { return libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(2)< int32(100) { Xsqlite3_log(tls, int32(SQLITE_FULL), __ccgo_ts+5183, libc.VaList(bp+24, zSuper)) _sqlite3OsDelete(tls, pVfs, zSuper, 0) break } else { if retryCount == int32(1) { Xsqlite3_log(tls, int32(SQLITE_FULL), __ccgo_ts+5197, libc.VaList(bp+24, zSuper)) } } } retryCount++ Xsqlite3_randomness(tls, int32(4), bp+8) Xsqlite3_snprintf(tls, int32(13), zSuper+uintptr(nMainFile), __ccgo_ts+5212, libc.VaList(bp+24, *(*Tu32)(unsafe.Pointer(bp + 8))>>libc.Int32FromInt32(8)&uint32(0xffffff), *(*Tu32)(unsafe.Pointer(bp + 8))&uint32(0xff))) /* The antipenultimate character of the super-journal name must ** be "9" to avoid name collisions when using 8+3 filenames. */ _ = libc.Int32FromInt32(0) rc = _sqlite3OsAccess(tls, pVfs, zSuper, SQLITE_ACCESS_EXISTS, bp+4) } if rc == SQLITE_OK { /* Open the super-journal. */ rc = _sqlite3OsOpenMalloc(tls, pVfs, zSuper, bp, libc.Int32FromInt32(SQLITE_OPEN_READWRITE)|libc.Int32FromInt32(SQLITE_OPEN_CREATE)|libc.Int32FromInt32(SQLITE_OPEN_EXCLUSIVE)|libc.Int32FromInt32(SQLITE_OPEN_SUPER_JOURNAL), uintptr(0)) } if rc != SQLITE_OK { _sqlite3DbFree(tls, db, zSuper-uintptr(4)) return rc } /* Write the name of each database file in the transaction into the new ** super-journal file. If an error occurs at this point close ** and delete the super-journal file. All the individual journal files ** still have 'null' as the super-journal pointer, so they will roll ** back independently if a failure occurs. */ i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pBt3 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt if _sqlite3BtreeTxnState(tls, pBt3) == int32(SQLITE_TXN_WRITE) { zFile = _sqlite3BtreeGetJournalname(tls, pBt3) if zFile == uintptr(0) { goto _4 /* Ignore TEMP and :memory: databases */ } _ = libc.Int32FromInt32(0) rc = _sqlite3OsWrite(tls, *(*uintptr)(unsafe.Pointer(bp)), zFile, _sqlite3Strlen30(tls, zFile)+int32(1), offset) offset += int64(_sqlite3Strlen30(tls, zFile) + int32(1)) if rc != SQLITE_OK { _sqlite3OsCloseFree(tls, *(*uintptr)(unsafe.Pointer(bp))) _sqlite3OsDelete(tls, pVfs, zSuper, 0) _sqlite3DbFree(tls, db, zSuper-uintptr(4)) return rc } } goto _4 _4: ; i++ } /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device ** flag is set this is not required. */ if v6 = 0 == _sqlite3OsDeviceCharacteristics(tls, *(*uintptr)(unsafe.Pointer(bp)))&int32(SQLITE_IOCAP_SEQUENTIAL); v6 { v5 = _sqlite3OsSync(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(SQLITE_SYNC_NORMAL)) rc = v5 } if v6 && SQLITE_OK != v5 { _sqlite3OsCloseFree(tls, *(*uintptr)(unsafe.Pointer(bp))) _sqlite3OsDelete(tls, pVfs, zSuper, 0) _sqlite3DbFree(tls, db, zSuper-uintptr(4)) return rc } /* Sync all the db files involved in the transaction. The same call ** sets the super-journal pointer in each individual journal. If ** an error occurs here, do not delete the super-journal file. ** ** If the error occurs during the first call to ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the ** super-journal file will be orphaned. But we cannot delete it, ** in case the super-journal file name was written into the journal ** file before the failure occurred. */ i = 0 for { if !(rc == SQLITE_OK && i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pBt4 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt if pBt4 != 0 { rc = _sqlite3BtreeCommitPhaseOne(tls, pBt4, zSuper) } goto _7 _7: ; i++ } _sqlite3OsCloseFree(tls, *(*uintptr)(unsafe.Pointer(bp))) _ = libc.Int32FromInt32(0) if rc != SQLITE_OK { _sqlite3DbFree(tls, db, zSuper-uintptr(4)) return rc } /* Delete the super-journal file. This commits the transaction. After ** doing this the directory is synced again before any individual ** transaction files are deleted. */ rc = _sqlite3OsDelete(tls, pVfs, zSuper, int32(1)) _sqlite3DbFree(tls, db, zSuper-uintptr(4)) zSuper = uintptr(0) if rc != 0 { return rc } /* All files and directories have already been synced, so the following ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and ** deleting or truncating journals. If something goes wrong while ** this is happening we don't really care. The integrity of the ** transaction is already guaranteed, but some stray 'cold' journals ** may be lying around. Returning an error code won't help matters. */ _sqlite3BeginBenignMalloc(tls) i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pBt5 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt if pBt5 != 0 { _sqlite3BtreeCommitPhaseTwo(tls, pBt5, int32(1)) } goto _8 _8: ; i++ } _sqlite3EndBenignMalloc(tls) _sqlite3VtabCommit(tls, db) } return rc } /* Whether or not a database might need a super-journal depends upon ** its journal mode (among other things). This matrix determines which ** journal modes use a super-journal and which do not */ var _aMJNeeded = [6]Tu8{ 0: uint8(1), 1: uint8(1), 3: uint8(1), } /* ** This routine checks that the sqlite3.nVdbeActive count variable ** matches the number of vdbe's in the list sqlite3.pVdbe that are ** currently active. An assertion fails if the two counts do not match. ** This is an internal self-check only - it is not an essential processing ** step. ** ** This is a no-op if NDEBUG is defined. */ // C documentation // // /* // ** If the Vdbe passed as the first argument opened a statement-transaction, // ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or // ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement // ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the // ** statement transaction is committed. // ** // ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. // ** Otherwise SQLITE_OK. // */ func _vdbeCloseStatement(tls *libc.TLS, p uintptr, eOp int32) (r int32) { var db, pBt uintptr var i, iSavepoint, rc, rc2 int32 _, _, _, _, _, _ = db, i, iSavepoint, pBt, rc, rc2 db = (*TVdbe)(unsafe.Pointer(p)).Fdb rc = SQLITE_OK iSavepoint = (*TVdbe)(unsafe.Pointer(p)).FiStatement - int32(1) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } rc2 = SQLITE_OK pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt if pBt != 0 { if eOp == int32(SAVEPOINT_ROLLBACK) { rc2 = _sqlite3BtreeSavepoint(tls, pBt, int32(SAVEPOINT_ROLLBACK), iSavepoint) } if rc2 == SQLITE_OK { rc2 = _sqlite3BtreeSavepoint(tls, pBt, int32(SAVEPOINT_RELEASE), iSavepoint) } if rc == SQLITE_OK { rc = rc2 } } goto _1 _1: ; i++ } (*Tsqlite3)(unsafe.Pointer(db)).FnStatement-- (*TVdbe)(unsafe.Pointer(p)).FiStatement = 0 if rc == SQLITE_OK { if eOp == int32(SAVEPOINT_ROLLBACK) { rc = _sqlite3VtabSavepoint(tls, db, int32(SAVEPOINT_ROLLBACK), iSavepoint) } if rc == SQLITE_OK { rc = _sqlite3VtabSavepoint(tls, db, int32(SAVEPOINT_RELEASE), iSavepoint) } } /* If the statement transaction is being rolled back, also restore the ** database handles deferred constraint counter to the value it had when ** the statement transaction was opened. */ if eOp == int32(SAVEPOINT_ROLLBACK) { (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons = (*TVdbe)(unsafe.Pointer(p)).FnStmtDefCons (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons = (*TVdbe)(unsafe.Pointer(p)).FnStmtDefImmCons } return rc } func _sqlite3VdbeCloseStatement(tls *libc.TLS, p uintptr, eOp int32) (r int32) { if (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FnStatement != 0 && (*TVdbe)(unsafe.Pointer(p)).FiStatement != 0 { return _vdbeCloseStatement(tls, p, eOp) } return SQLITE_OK } // C documentation // // /* // ** This function is called when a transaction opened by the database // ** handle associated with the VM passed as an argument is about to be // ** committed. If there are outstanding deferred foreign key constraint // ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK. // ** // ** If there are outstanding FK violations and this function returns // ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY // ** and write an error message to it. Then return SQLITE_ERROR. // */ func _sqlite3VdbeCheckFk(tls *libc.TLS, p uintptr, deferred int32) (r int32) { var db uintptr _ = db db = (*TVdbe)(unsafe.Pointer(p)).Fdb if deferred != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons+(*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons > 0 || !(deferred != 0) && (*TVdbe)(unsafe.Pointer(p)).FnFkConstraint > 0 { (*TVdbe)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(3)<>7)) != 0 { /* Primary error code from p->rc */ eStatementOp = 0 /* Set to true if a 'special' error */ /* Lock all btrees used by the statement */ _sqlite3VdbeEnter(tls, p) /* Check for one of the special errors */ if (*TVdbe)(unsafe.Pointer(p)).Frc != 0 { mrc = (*TVdbe)(unsafe.Pointer(p)).Frc & int32(0xff) isSpecialError = libc.BoolInt32(mrc == int32(SQLITE_NOMEM) || mrc == int32(SQLITE_IOERR) || mrc == int32(SQLITE_INTERRUPT) || mrc == int32(SQLITE_FULL)) } else { v1 = libc.Int32FromInt32(0) isSpecialError = v1 mrc = v1 } if isSpecialError != 0 { /* If the query was read-only and the error code is SQLITE_INTERRUPT, ** no rollback is necessary. Otherwise, at least a savepoint ** transaction must be rolled back to restore the database to a ** consistent state. ** ** Even if the statement is read-only, it is important to perform ** a statement or transaction rollback operation. If the error ** occurred while writing to the journal, sub-journal or database ** file as part of an effort to free up cache space (see function ** pagerStress() in pager.c), the rollback is required to restore ** the pager to a consistent state. */ if !(int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x40>>6)) != 0) || mrc != int32(SQLITE_INTERRUPT) { if (mrc == int32(SQLITE_NOMEM) || mrc == int32(SQLITE_FULL)) && int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x20>>5)) != 0 { eStatementOp = int32(SAVEPOINT_ROLLBACK) } else { /* We are forced to roll back the active transaction. Before doing ** so, abort any other statements this handle currently has active. */ _sqlite3RollbackAll(tls, db, libc.Int32FromInt32(SQLITE_ABORT)|libc.Int32FromInt32(2)< 0 && (*Tsqlite3)(unsafe.Pointer(db)).FaVTrans == uintptr(0)) && (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite == libc.BoolInt32(int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x40>>6)) == 0) { if (*TVdbe)(unsafe.Pointer(p)).Frc == SQLITE_OK || int32((*TVdbe)(unsafe.Pointer(p)).FerrorAction) == int32(OE_Fail) && !(isSpecialError != 0) { rc = _sqlite3VdbeCheckFk(tls, p, int32(1)) if rc != SQLITE_OK { if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x40>>6)) != 0 { _sqlite3VdbeLeave(tls, p) return int32(SQLITE_ERROR) } rc = libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(3)<>6)) != 0 { _sqlite3VdbeLeave(tls, p) return int32(SQLITE_BUSY) } else { if rc != SQLITE_OK { _sqlite3SystemError(tls, db, rc) (*TVdbe)(unsafe.Pointer(p)).Frc = rc _sqlite3RollbackAll(tls, db, SQLITE_OK) (*TVdbe)(unsafe.Pointer(p)).FnChange = 0 } else { (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons = 0 (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons = 0 *(*Tu64)(unsafe.Pointer(db + 32)) &= ^libc.Uint64FromInt32(SQLITE_DeferFKs) _sqlite3CommitInternalChanges(tls, db) } } } else { if (*TVdbe)(unsafe.Pointer(p)).Frc == int32(SQLITE_SCHEMA) && (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive > int32(1) { (*TVdbe)(unsafe.Pointer(p)).FnChange = 0 } else { _sqlite3RollbackAll(tls, db, SQLITE_OK) (*TVdbe)(unsafe.Pointer(p)).FnChange = 0 } } (*Tsqlite3)(unsafe.Pointer(db)).FnStatement = 0 } else { if eStatementOp == 0 { if (*TVdbe)(unsafe.Pointer(p)).Frc == SQLITE_OK || int32((*TVdbe)(unsafe.Pointer(p)).FerrorAction) == int32(OE_Fail) { eStatementOp = int32(SAVEPOINT_RELEASE) } else { if int32((*TVdbe)(unsafe.Pointer(p)).FerrorAction) == int32(OE_Abort) { eStatementOp = int32(SAVEPOINT_ROLLBACK) } else { _sqlite3RollbackAll(tls, db, libc.Int32FromInt32(SQLITE_ABORT)|libc.Int32FromInt32(2)<>4)) != 0 { if eStatementOp != int32(SAVEPOINT_ROLLBACK) { _sqlite3VdbeSetChanges(tls, db, (*TVdbe)(unsafe.Pointer(p)).FnChange) } else { _sqlite3VdbeSetChanges(tls, db, 0) } (*TVdbe)(unsafe.Pointer(p)).FnChange = 0 } /* Release the locks */ _sqlite3VdbeLeave(tls, p) } /* We have successfully halted and closed the VM. Record this fact. */ (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive-- if !(int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x40>>6)) != 0) { (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite-- } if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x80>>7)) != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeRead-- } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TVdbe)(unsafe.Pointer(p)).FeVdbeState = uint8(VDBE_HALT_STATE) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { (*TVdbe)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) } /* If the auto-commit flag is set to true, then any locks that were held ** by connection db have now been released. Call sqlite3ConnectionUnlocked() ** to invoke any required unlock-notify callbacks. */ if (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0 { _sqlite3ConnectionUnlocked(tls, db) } _ = libc.Int32FromInt32(0) if (*TVdbe)(unsafe.Pointer(p)).Frc == int32(SQLITE_BUSY) { v2 = int32(SQLITE_BUSY) } else { v2 = SQLITE_OK } return v2 } // C documentation // // /* // ** Each VDBE holds the result of the most recent sqlite3_step() call // ** in p->rc. This routine sets that result back to SQLITE_OK. // */ func _sqlite3VdbeResetStepResult(tls *libc.TLS, p uintptr) { (*TVdbe)(unsafe.Pointer(p)).Frc = SQLITE_OK } // C documentation // // /* // ** Copy the error code and error message belonging to the VDBE passed // ** as the first argument to its database handle (so that they will be // ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()). // ** // ** This function does not clear the VDBE error code or message, just // ** copies them to the database handle. // */ func _sqlite3VdbeTransferError(tls *libc.TLS, p uintptr) (r int32) { var db uintptr var rc int32 _, _ = db, rc db = (*TVdbe)(unsafe.Pointer(p)).Fdb rc = (*TVdbe)(unsafe.Pointer(p)).Frc if (*TVdbe)(unsafe.Pointer(p)).FzErrMsg != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FbBenignMalloc++ _sqlite3BeginBenignMalloc(tls) if (*Tsqlite3)(unsafe.Pointer(db)).FpErr == uintptr(0) { (*Tsqlite3)(unsafe.Pointer(db)).FpErr = _sqlite3ValueNew(tls, db) } _sqlite3ValueSetStr(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpErr, -int32(1), (*TVdbe)(unsafe.Pointer(p)).FzErrMsg, uint8(SQLITE_UTF8), uintptr(-libc.Int32FromInt32(1))) _sqlite3EndBenignMalloc(tls) (*Tsqlite3)(unsafe.Pointer(db)).FbBenignMalloc-- } else { if (*Tsqlite3)(unsafe.Pointer(db)).FpErr != 0 { _sqlite3ValueSetNull(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpErr) } } (*Tsqlite3)(unsafe.Pointer(db)).FerrCode = rc (*Tsqlite3)(unsafe.Pointer(db)).FerrByteOffset = -int32(1) return rc } // C documentation // // /* // ** Clean up a VDBE after execution but do not delete the VDBE just yet. // ** Write any error messages into *pzErrMsg. Return the result code. // ** // ** After this routine is run, the VDBE should be ready to be executed // ** again. // ** // ** To look at it another way, this routine resets the state of the // ** virtual machine from VDBE_RUN_STATE or VDBE_HALT_STATE back to // ** VDBE_READY_STATE. // */ func _sqlite3VdbeReset(tls *libc.TLS, p uintptr) (r int32) { var db uintptr _ = db db = (*TVdbe)(unsafe.Pointer(p)).Fdb /* If the VM did not run to completion or if it encountered an ** error, then it might not have been halted properly. So halt ** it now. */ if int32((*TVdbe)(unsafe.Pointer(p)).FeVdbeState) == int32(VDBE_RUN_STATE) { _sqlite3VdbeHalt(tls, p) } /* If the VDBE has been run even partially, then transfer the error code ** and error message from the VDBE into the main database structure. But ** if the VDBE has just been set to run but has not actually executed any ** instructions yet, leave the main database error information unchanged. */ if (*TVdbe)(unsafe.Pointer(p)).Fpc >= 0 { if (*Tsqlite3)(unsafe.Pointer(db)).FpErr != 0 || (*TVdbe)(unsafe.Pointer(p)).FzErrMsg != 0 { _sqlite3VdbeTransferError(tls, p) } else { (*Tsqlite3)(unsafe.Pointer(db)).FerrCode = (*TVdbe)(unsafe.Pointer(p)).Frc } } /* Reset register contents and reclaim error message memory. */ if (*TVdbe)(unsafe.Pointer(p)).FzErrMsg != 0 { _sqlite3DbFree(tls, db, (*TVdbe)(unsafe.Pointer(p)).FzErrMsg) (*TVdbe)(unsafe.Pointer(p)).FzErrMsg = uintptr(0) } (*TVdbe)(unsafe.Pointer(p)).FpResultRow = uintptr(0) /* Save profiling information from this VDBE run. */ return (*TVdbe)(unsafe.Pointer(p)).Frc & (*Tsqlite3)(unsafe.Pointer(db)).FerrMask } // C documentation // // /* // ** Clean up and delete a VDBE after execution. Return an integer which is // ** the result code. Write any error message text into *pzErrMsg. // */ func _sqlite3VdbeFinalize(tls *libc.TLS, p uintptr) (r int32) { var rc int32 _ = rc rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TVdbe)(unsafe.Pointer(p)).FeVdbeState) >= int32(VDBE_READY_STATE) { rc = _sqlite3VdbeReset(tls, p) _ = libc.Int32FromInt32(0) } _sqlite3VdbeDelete(tls, p) return rc } // C documentation // // /* // ** If parameter iOp is less than zero, then invoke the destructor for // ** all auxiliary data pointers currently cached by the VM passed as // ** the first argument. // ** // ** Or, if iOp is greater than or equal to zero, then the destructor is // ** only invoked for those auxiliary data pointers created by the user // ** function invoked by the OP_Function opcode at instruction iOp of // ** VM pVdbe, and only then if: // ** // ** * the associated function parameter is the 32nd or later (counting // ** from left to right), or // ** // ** * the corresponding bit in argument mask is clear (where the first // ** function parameter corresponds to bit 0 etc.). // */ func _sqlite3VdbeDeleteAuxData(tls *libc.TLS, db uintptr, pp uintptr, iOp int32, mask int32) { var pAux uintptr _ = pAux for *(*uintptr)(unsafe.Pointer(pp)) != 0 { pAux = *(*uintptr)(unsafe.Pointer(pp)) if iOp < 0 || (*TAuxData)(unsafe.Pointer(pAux)).FiAuxOp == iOp && (*TAuxData)(unsafe.Pointer(pAux)).FiAuxArg >= 0 && ((*TAuxData)(unsafe.Pointer(pAux)).FiAuxArg > int32(31) || !(uint32(mask)&(libc.Uint32FromInt32(1)<<(*TAuxData)(unsafe.Pointer(pAux)).FiAuxArg) != 0)) { if (*TAuxData)(unsafe.Pointer(pAux)).FxDeleteAux != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TAuxData)(unsafe.Pointer(pAux)).FxDeleteAux})))(tls, (*TAuxData)(unsafe.Pointer(pAux)).FpAux) } *(*uintptr)(unsafe.Pointer(pp)) = (*TAuxData)(unsafe.Pointer(pAux)).FpNextAux _sqlite3DbFree(tls, db, pAux) } else { pp = pAux + 16 } } } // C documentation // // /* // ** Free all memory associated with the Vdbe passed as the second argument, // ** except for object itself, which is preserved. // ** // ** The difference between this function and sqlite3VdbeDelete() is that // ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with // ** the database connection and frees the object itself. // */ func _sqlite3VdbeClearObject(tls *libc.TLS, db uintptr, p uintptr) { var pNext, pSub uintptr _, _ = pNext, pSub _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TVdbe)(unsafe.Pointer(p)).FaColName != 0 { _releaseMemArray(tls, (*TVdbe)(unsafe.Pointer(p)).FaColName, int32((*TVdbe)(unsafe.Pointer(p)).FnResAlloc)*int32(COLNAME_N)) _sqlite3DbNNFreeNN(tls, db, (*TVdbe)(unsafe.Pointer(p)).FaColName) } pSub = (*TVdbe)(unsafe.Pointer(p)).FpProgram for { if !(pSub != 0) { break } pNext = (*TSubProgram)(unsafe.Pointer(pSub)).FpNext _vdbeFreeOpArray(tls, db, (*TSubProgram)(unsafe.Pointer(pSub)).FaOp, (*TSubProgram)(unsafe.Pointer(pSub)).FnOp) _sqlite3DbFree(tls, db, pSub) goto _1 _1: ; pSub = pNext } if int32((*TVdbe)(unsafe.Pointer(p)).FeVdbeState) != VDBE_INIT_STATE { _releaseMemArray(tls, (*TVdbe)(unsafe.Pointer(p)).FaVar, int32((*TVdbe)(unsafe.Pointer(p)).FnVar)) if (*TVdbe)(unsafe.Pointer(p)).FpVList != 0 { _sqlite3DbNNFreeNN(tls, db, (*TVdbe)(unsafe.Pointer(p)).FpVList) } if (*TVdbe)(unsafe.Pointer(p)).FpFree != 0 { _sqlite3DbNNFreeNN(tls, db, (*TVdbe)(unsafe.Pointer(p)).FpFree) } } _vdbeFreeOpArray(tls, db, (*TVdbe)(unsafe.Pointer(p)).FaOp, (*TVdbe)(unsafe.Pointer(p)).FnOp) if (*TVdbe)(unsafe.Pointer(p)).FzSql != 0 { _sqlite3DbNNFreeNN(tls, db, (*TVdbe)(unsafe.Pointer(p)).FzSql) } } // C documentation // // /* // ** Delete an entire VDBE. // */ func _sqlite3VdbeDelete(tls *libc.TLS, p uintptr) { var db uintptr _ = db _ = libc.Int32FromInt32(0) db = (*TVdbe)(unsafe.Pointer(p)).Fdb _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeClearObject(tls, db, p) if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0) { _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FppVPrev)) = (*TVdbe)(unsafe.Pointer(p)).FpVNext if (*TVdbe)(unsafe.Pointer(p)).FpVNext != 0 { (*TVdbe)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FpVNext)).FppVPrev = (*TVdbe)(unsafe.Pointer(p)).FppVPrev } } _sqlite3DbNNFreeNN(tls, db, p) } // C documentation // // /* // ** The cursor "p" has a pending seek operation that has not yet been // ** carried out. Seek the cursor now. If an error occurs, return // ** the appropriate error code. // */ func _sqlite3VdbeFinishMoveto(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* res at bp+0 */ int32 _ = rc _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3BtreeTableMoveto(tls, *(*uintptr)(unsafe.Pointer(p + 36)), (*TVdbeCursor)(unsafe.Pointer(p)).FmovetoTarget, 0, bp) if rc != 0 { return rc } if *(*int32)(unsafe.Pointer(bp)) != 0 { return _sqlite3CorruptError(tls, int32(88164)) } (*TVdbeCursor)(unsafe.Pointer(p)).FdeferredMoveto = uint8(0) (*TVdbeCursor)(unsafe.Pointer(p)).FcacheStatus = uint32(CACHE_STALE) return SQLITE_OK } // C documentation // // /* // ** Something has moved cursor "p" out of place. Maybe the row it was // ** pointed to was deleted out from under it. Or maybe the btree was // ** rebalanced. Whatever the cause, try to restore "p" to the place it // ** is supposed to be pointing. If the row was deleted out from under the // ** cursor, set the cursor to point to a NULL row. // */ func _sqlite3VdbeHandleMovedCursor(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* isDifferentRow at bp+0 */ int32 _ = rc _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3BtreeCursorRestore(tls, *(*uintptr)(unsafe.Pointer(p + 36)), bp) (*TVdbeCursor)(unsafe.Pointer(p)).FcacheStatus = uint32(CACHE_STALE) if *(*int32)(unsafe.Pointer(bp)) != 0 { (*TVdbeCursor)(unsafe.Pointer(p)).FnullRow = uint8(1) } return rc } // C documentation // // /* // ** Check to ensure that the cursor is valid. Restore the cursor // ** if need be. Return any I/O error from the restore operation. // */ func _sqlite3VdbeCursorRestore(tls *libc.TLS, p uintptr) (r int32) { _ = libc.Int32FromInt32(0) if _sqlite3BtreeCursorHasMoved(tls, *(*uintptr)(unsafe.Pointer(p + 36))) != 0 { return _sqlite3VdbeHandleMovedCursor(tls, p) } return SQLITE_OK } /* ** The following functions: ** ** sqlite3VdbeSerialType() ** sqlite3VdbeSerialTypeLen() ** sqlite3VdbeSerialLen() ** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02 ** sqlite3VdbeSerialGet() ** ** encapsulate the code that serializes values for storage in SQLite ** data and index records. Each serialized value consists of a ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned ** integer, stored as a varint. ** ** In an SQLite index record, the serial type is stored directly before ** the blob of data that it corresponds to. In a table record, all serial ** types are stored at the start of the record, and the blobs of data at ** the end. Hence these functions allow the caller to handle the ** serial-type and data blob separately. ** ** The following table describes the various storage classes for data: ** ** serial type bytes of data type ** -------------- --------------- --------------- ** 0 0 NULL ** 1 1 signed integer ** 2 2 signed integer ** 3 3 signed integer ** 4 4 signed integer ** 5 6 signed integer ** 6 8 signed integer ** 7 8 IEEE float ** 8 0 Integer constant 0 ** 9 0 Integer constant 1 ** 10,11 reserved for expansion ** N>=12 and even (N-12)/2 BLOB ** N>=13 and odd (N-13)/2 text ** ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions ** of SQLite will not understand those serial types. */ // C documentation // // /* // ** The sizes for serial types less than 128 // */ var _sqlite3SmallTypeSizes = [128]Tu8{ 1: uint8(1), 2: uint8(2), 3: uint8(3), 4: uint8(4), 5: uint8(6), 6: uint8(8), 7: uint8(8), 14: uint8(1), 15: uint8(1), 16: uint8(2), 17: uint8(2), 18: uint8(3), 19: uint8(3), 20: uint8(4), 21: uint8(4), 22: uint8(5), 23: uint8(5), 24: uint8(6), 25: uint8(6), 26: uint8(7), 27: uint8(7), 28: uint8(8), 29: uint8(8), 30: uint8(9), 31: uint8(9), 32: uint8(10), 33: uint8(10), 34: uint8(11), 35: uint8(11), 36: uint8(12), 37: uint8(12), 38: uint8(13), 39: uint8(13), 40: uint8(14), 41: uint8(14), 42: uint8(15), 43: uint8(15), 44: uint8(16), 45: uint8(16), 46: uint8(17), 47: uint8(17), 48: uint8(18), 49: uint8(18), 50: uint8(19), 51: uint8(19), 52: uint8(20), 53: uint8(20), 54: uint8(21), 55: uint8(21), 56: uint8(22), 57: uint8(22), 58: uint8(23), 59: uint8(23), 60: uint8(24), 61: uint8(24), 62: uint8(25), 63: uint8(25), 64: uint8(26), 65: uint8(26), 66: uint8(27), 67: uint8(27), 68: uint8(28), 69: uint8(28), 70: uint8(29), 71: uint8(29), 72: uint8(30), 73: uint8(30), 74: uint8(31), 75: uint8(31), 76: uint8(32), 77: uint8(32), 78: uint8(33), 79: uint8(33), 80: uint8(34), 81: uint8(34), 82: uint8(35), 83: uint8(35), 84: uint8(36), 85: uint8(36), 86: uint8(37), 87: uint8(37), 88: uint8(38), 89: uint8(38), 90: uint8(39), 91: uint8(39), 92: uint8(40), 93: uint8(40), 94: uint8(41), 95: uint8(41), 96: uint8(42), 97: uint8(42), 98: uint8(43), 99: uint8(43), 100: uint8(44), 101: uint8(44), 102: uint8(45), 103: uint8(45), 104: uint8(46), 105: uint8(46), 106: uint8(47), 107: uint8(47), 108: uint8(48), 109: uint8(48), 110: uint8(49), 111: uint8(49), 112: uint8(50), 113: uint8(50), 114: uint8(51), 115: uint8(51), 116: uint8(52), 117: uint8(52), 118: uint8(53), 119: uint8(53), 120: uint8(54), 121: uint8(54), 122: uint8(55), 123: uint8(55), 124: uint8(56), 125: uint8(56), 126: uint8(57), 127: uint8(57), } // C documentation // // /* // ** Return the length of the data corresponding to the supplied serial-type. // */ func _sqlite3VdbeSerialTypeLen(tls *libc.TLS, serial_type Tu32) (r Tu32) { if serial_type >= uint32(128) { return (serial_type - uint32(12)) / uint32(2) } else { _ = libc.Int32FromInt32(0) return uint32(_sqlite3SmallTypeSizes[serial_type]) } return r } func _sqlite3VdbeOneByteSerialTypeLen(tls *libc.TLS, serial_type Tu8) (r Tu8) { _ = libc.Int32FromInt32(0) return _sqlite3SmallTypeSizes[serial_type] } /* ** If we are on an architecture with mixed-endian floating ** points (ex: ARM7) then swap the lower 4 bytes with the ** upper 4 bytes. Return the result. ** ** For most architectures, this is a no-op. ** ** (later): It is reported to me that the mixed-endian problem ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems ** that early versions of GCC stored the two words of a 64-bit ** float in the wrong order. And that error has been propagated ** ever since. The blame is not necessarily with GCC, though. ** GCC might have just copying the problem from a prior compiler. ** I am also told that newer versions of GCC that follow a different ** ABI get the byte order right. ** ** Developers using SQLite on an ARM7 should compile and run their ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG ** enabled, some asserts below will ensure that the byte order of ** floating point values is correct. ** ** (2007-08-30) Frank van Vugt has studied this problem closely ** and has send his findings to the SQLite developers. Frank ** writes that some Linux kernels offer floating point hardware ** emulation that uses only 32-bit mantissas instead of a full ** 48-bits as required by the IEEE standard. (This is the ** CONFIG_FPE_FASTFPE option.) On such systems, floating point ** byte swapping becomes very complicated. To avoid problems, ** the necessary byte swapping is carried out using a 64-bit integer ** rather than a 64-bit float. Frank assures us that the code here ** works for him. We, the developers, have no way to independently ** verify this, but Frank seems to know what he is talking about ** so we trust him. */ /* Input "x" is a sequence of unsigned characters that represent a ** big-endian integer. Return the equivalent native integer */ // C documentation // // /* // ** Deserialize the data blob pointed to by buf as serial type serial_type // ** and store the result in pMem. // ** // ** This function is implemented as two separate routines for performance. // ** The few cases that require local variables are broken out into a separate // ** routine so that in most cases the overhead of moving the stack pointer // ** is avoided. // */ func _serialGet(tls *libc.TLS, buf uintptr, serial_type Tu32, pMem uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var y Tu32 var v1 int32 var _ /* x at bp+0 */ Tu64 _, _ = y, v1 *(*Tu64)(unsafe.Pointer(bp)) = uint64(uint32(*(*uint8)(unsafe.Pointer(buf)))<flags = 0; // sqlite3VdbeSerialGet() will set this for us */ (*TMem)(unsafe.Pointer(pMem)).FszMalloc = 0 (*TMem)(unsafe.Pointer(pMem)).Fz = uintptr(0) _sqlite3VdbeSerialGet(tls, aKey+uintptr(d), *(*Tu32)(unsafe.Pointer(bp + 4)), pMem) d += _sqlite3VdbeSerialTypeLen(tls, *(*Tu32)(unsafe.Pointer(bp + 4))) pMem += 40 u++ v3 = u if int32(v3) >= int32((*TUnpackedRecord)(unsafe.Pointer(p)).FnField) { break } } if d > uint32(nKey) && u != 0 { _ = libc.Int32FromInt32(0) /* In a corrupt record entry, the last pMem might have been set up using ** uninitialized memory. Overwrite its value with NULL, to prevent ** warnings from MSAN. */ _sqlite3VdbeMemSetNull(tls, pMem-uintptr(1)*40) } _ = libc.Int32FromInt32(0) (*TUnpackedRecord)(unsafe.Pointer(p)).FnField = u } // C documentation // // /* // ** Both *pMem1 and *pMem2 contain string values. Compare the two values // ** using the collation sequence pColl. As usual, return a negative , zero // ** or positive value if *pMem1 is less than, equal to or greater than // ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);". // */ func _vdbeCompareMemString(tls *libc.TLS, pMem1 uintptr, pMem2 uintptr, pColl uintptr, prcErr uintptr) (r int32) { bp := tls.Alloc(80) defer tls.Free(80) var rc int32 var v1, v2 uintptr var _ /* c1 at bp+0 */ TMem var _ /* c2 at bp+40 */ TMem _, _, _ = rc, v1, v2 if int32((*TMem)(unsafe.Pointer(pMem1)).Fenc) == int32((*TCollSeq)(unsafe.Pointer(pColl)).Fenc) { /* The strings are already in the correct encoding. Call the ** comparison function directly */ return (*(*func(*libc.TLS, uintptr, int32, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TCollSeq)(unsafe.Pointer(pColl)).FxCmp})))(tls, (*TCollSeq)(unsafe.Pointer(pColl)).FpUser, (*TMem)(unsafe.Pointer(pMem1)).Fn, (*TMem)(unsafe.Pointer(pMem1)).Fz, (*TMem)(unsafe.Pointer(pMem2)).Fn, (*TMem)(unsafe.Pointer(pMem2)).Fz) } else { _sqlite3VdbeMemInit(tls, bp, (*TMem)(unsafe.Pointer(pMem1)).Fdb, uint16(MEM_Null)) _sqlite3VdbeMemInit(tls, bp+40, (*TMem)(unsafe.Pointer(pMem1)).Fdb, uint16(MEM_Null)) _sqlite3VdbeMemShallowCopy(tls, bp, pMem1, int32(MEM_Ephem)) _sqlite3VdbeMemShallowCopy(tls, bp+40, pMem2, int32(MEM_Ephem)) v1 = _sqlite3ValueText(tls, bp, (*TCollSeq)(unsafe.Pointer(pColl)).Fenc) v2 = _sqlite3ValueText(tls, bp+40, (*TCollSeq)(unsafe.Pointer(pColl)).Fenc) if v1 == uintptr(0) || v2 == uintptr(0) { if prcErr != 0 { *(*Tu8)(unsafe.Pointer(prcErr)) = uint8(SQLITE_NOMEM) } rc = 0 } else { rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TCollSeq)(unsafe.Pointer(pColl)).FxCmp})))(tls, (*TCollSeq)(unsafe.Pointer(pColl)).FpUser, (*(*TMem)(unsafe.Pointer(bp))).Fn, v1, (*(*TMem)(unsafe.Pointer(bp + 40))).Fn, v2) } _sqlite3VdbeMemReleaseMalloc(tls, bp) _sqlite3VdbeMemReleaseMalloc(tls, bp+40) return rc } return r } // C documentation // // /* // ** The input pBlob is guaranteed to be a Blob that is not marked // ** with MEM_Zero. Return true if it could be a zero-blob. // */ func _isAllZero(tls *libc.TLS, z uintptr, n int32) (r int32) { var i int32 _ = i i = 0 for { if !(i < n) { break } if *(*uint8)(unsafe.Pointer(z + uintptr(i))) != 0 { return 0 } goto _1 _1: ; i++ } return int32(1) } // C documentation // // /* // ** Compare two blobs. Return negative, zero, or positive if the first // ** is less than, equal to, or greater than the second, respectively. // ** If one blob is a prefix of the other, then the shorter is the lessor. // */ func _sqlite3BlobCompare(tls *libc.TLS, pB1 uintptr, pB2 uintptr) (r int32) { var c, n1, n2, v1 int32 _, _, _, _ = c, n1, n2, v1 n1 = (*TMem)(unsafe.Pointer(pB1)).Fn n2 = (*TMem)(unsafe.Pointer(pB2)).Fn /* It is possible to have a Blob value that has some non-zero content ** followed by zero content. But that only comes up for Blobs formed ** by the OP_MakeRecord opcode, and such Blobs never get passed into ** sqlite3MemCompare(). */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (int32((*TMem)(unsafe.Pointer(pB1)).Fflags)|int32((*TMem)(unsafe.Pointer(pB2)).Fflags))&int32(MEM_Zero) != 0 { if int32((*TMem)(unsafe.Pointer(pB1)).Fflags)&int32((*TMem)(unsafe.Pointer(pB2)).Fflags)&int32(MEM_Zero) != 0 { return *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pB1)).Fu)) - *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pB2)).Fu)) } else { if int32((*TMem)(unsafe.Pointer(pB1)).Fflags)&int32(MEM_Zero) != 0 { if !(_isAllZero(tls, (*TMem)(unsafe.Pointer(pB2)).Fz, (*TMem)(unsafe.Pointer(pB2)).Fn) != 0) { return -int32(1) } return *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pB1)).Fu)) - n2 } else { if !(_isAllZero(tls, (*TMem)(unsafe.Pointer(pB1)).Fz, (*TMem)(unsafe.Pointer(pB1)).Fn) != 0) { return +libc.Int32FromInt32(1) } return n1 - *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pB2)).Fu)) } } } if n1 > n2 { v1 = n2 } else { v1 = n1 } c = libc.Xmemcmp(tls, (*TMem)(unsafe.Pointer(pB1)).Fz, (*TMem)(unsafe.Pointer(pB2)).Fz, uint32(v1)) if c != 0 { return c } return n1 - n2 } /* The following two functions are used only within testcase() to prove ** test coverage. These functions do no exist for production builds. ** We must use separate SQLITE_NOINLINE functions here, since otherwise ** optimizer code movement causes gcov to become very confused. */ // C documentation // // /* // ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point // ** number. Return negative, zero, or positive if the first (i64) is less than, // ** equal to, or greater than the second (double). // */ func _sqlite3IntFloatCompare(tls *libc.TLS, i Ti64, r float64) (r1 int32) { var s, x float64 var y Ti64 var v1, v2 int32 _, _, _, _, _ = s, x, y, v1, v2 if _sqlite3IsNaN(tls, r) != 0 { /* SQLite considers NaN to be a NULL. And all integer values are greater ** than NULL */ return int32(1) } if _sqlite3Config.FbUseLongDouble != 0 { x = float64(i) if x < r { v1 = -int32(1) } else { v1 = libc.BoolInt32(x > r) } return v1 } else { if r < -libc.Float64FromFloat64(9.223372036854776e+18) { return +libc.Int32FromInt32(1) } if r >= float64(9.223372036854776e+18) { return -int32(1) } y = int64(r) if i < y { return -int32(1) } if i > y { return +libc.Int32FromInt32(1) } s = float64(i) if s < r { v2 = -int32(1) } else { v2 = libc.BoolInt32(s > r) } return v2 } return r1 } // C documentation // // /* // ** Compare the values contained by the two memory cells, returning // ** negative, zero or positive if pMem1 is less than, equal to, or greater // ** than pMem2. Sorting order is NULL's first, followed by numbers (integers // ** and reals) sorted numerically, followed by text ordered by the collating // ** sequence pColl and finally blob's ordered by memcmp(). // ** // ** Two NULL values are considered equal by this function. // */ func _sqlite3MemCompare(tls *libc.TLS, pMem1 uintptr, pMem2 uintptr, pColl uintptr) (r int32) { var combined_flags, f1, f2 int32 _, _, _ = combined_flags, f1, f2 f1 = int32((*TMem)(unsafe.Pointer(pMem1)).Fflags) f2 = int32((*TMem)(unsafe.Pointer(pMem2)).Fflags) combined_flags = f1 | f2 _ = libc.Int32FromInt32(0) /* If one value is NULL, it is less than the other. If both values ** are NULL, return 0. */ if combined_flags&int32(MEM_Null) != 0 { return f2&int32(MEM_Null) - f1&int32(MEM_Null) } /* At least one of the two values is a number */ if combined_flags&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)) != 0 { if f1&f2&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 { if *(*Ti64)(unsafe.Pointer(pMem1)) < *(*Ti64)(unsafe.Pointer(pMem2)) { return -int32(1) } if *(*Ti64)(unsafe.Pointer(pMem1)) > *(*Ti64)(unsafe.Pointer(pMem2)) { return +libc.Int32FromInt32(1) } return 0 } if f1&f2&int32(MEM_Real) != 0 { if *(*float64)(unsafe.Pointer(pMem1)) < *(*float64)(unsafe.Pointer(pMem2)) { return -int32(1) } if *(*float64)(unsafe.Pointer(pMem1)) > *(*float64)(unsafe.Pointer(pMem2)) { return +libc.Int32FromInt32(1) } return 0 } if f1&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 { if f2&int32(MEM_Real) != 0 { return _sqlite3IntFloatCompare(tls, *(*Ti64)(unsafe.Pointer(pMem1)), *(*float64)(unsafe.Pointer(pMem2))) } else { if f2&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 { if *(*Ti64)(unsafe.Pointer(pMem1)) < *(*Ti64)(unsafe.Pointer(pMem2)) { return -int32(1) } if *(*Ti64)(unsafe.Pointer(pMem1)) > *(*Ti64)(unsafe.Pointer(pMem2)) { return +libc.Int32FromInt32(1) } return 0 } else { return -int32(1) } } } if f1&int32(MEM_Real) != 0 { if f2&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 { return -_sqlite3IntFloatCompare(tls, *(*Ti64)(unsafe.Pointer(pMem2)), *(*float64)(unsafe.Pointer(pMem1))) } else { return -int32(1) } } return +libc.Int32FromInt32(1) } /* If one value is a string and the other is a blob, the string is less. ** If both are strings, compare using the collating functions. */ if combined_flags&int32(MEM_Str) != 0 { if f1&int32(MEM_Str) == 0 { return int32(1) } if f2&int32(MEM_Str) == 0 { return -int32(1) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* The collation sequence must be defined at this point, even if ** the user deletes the collation sequence after the vdbe program is ** compiled (this was not always the case). */ _ = libc.Int32FromInt32(0) if pColl != 0 { return _vdbeCompareMemString(tls, pMem1, pMem2, pColl, uintptr(0)) } /* If a NULL pointer was passed as the collate function, fall through ** to the blob case and use memcmp(). */ } /* Both values must be blobs. Compare using memcmp(). */ return _sqlite3BlobCompare(tls, pMem1, pMem2) } // C documentation // // /* // ** The first argument passed to this function is a serial-type that // ** corresponds to an integer - all values between 1 and 9 inclusive // ** except 7. The second points to a buffer containing an integer value // ** serialized according to serial_type. This function deserializes // ** and returns the value. // */ func _vdbeRecordDecodeInt(tls *libc.TLS, serial_type Tu32, aKey uintptr) (r Ti64) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* x at bp+8 */ Tu64 var _ /* y at bp+0 */ Tu32 _ = libc.Int32FromInt32(0) switch serial_type { case uint32(0): fallthrough case uint32(1): return int64(int8(*(*Tu8)(unsafe.Pointer(aKey)))) case uint32(2): return int64(libc.Int32FromInt32(256)*int32(int8(*(*Tu8)(unsafe.Pointer(aKey)))) | int32(*(*Tu8)(unsafe.Pointer(aKey + 1)))) case uint32(3): return int64(libc.Int32FromInt32(65536)*int32(int8(*(*Tu8)(unsafe.Pointer(aKey)))) | int32(*(*Tu8)(unsafe.Pointer(aKey + 1)))<default_rc is // ** returned. // ** // ** If database corruption is discovered, set pPKey2->errCode to // ** SQLITE_CORRUPT and return 0. If an OOM error is encountered, // ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the // ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db). // */ func _sqlite3VdbeRecordCompareWithSkip(tls *libc.TLS, nKey1 int32, pKey1 uintptr, pPKey2 uintptr, bSkip int32) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var aKey1, pKeyInfo, pRhs, v4 uintptr var d1, idx1, v1 Tu32 var i, nCmp, nCmp1, nStr, rc, sortFlags, v2, v3, v6, v7 int32 var lhs, rhs Ti64 var v5 bool var _ /* mem1 at bp+8 */ TMem var _ /* s1 at bp+48 */ Tu32 var _ /* serial_type at bp+52 */ Tu32 var _ /* szHdr1 at bp+0 */ Tu32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aKey1, d1, i, idx1, lhs, nCmp, nCmp1, nStr, pKeyInfo, pRhs, rc, rhs, sortFlags, v1, v2, v3, v4, v5, v6, v7 /* Offset of first type in header */ rc = 0 /* Return value */ pRhs = (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FaMem aKey1 = pKey1 /* If bSkip is true, then the caller has already determined that the first ** two elements in the keys are equal. Fix the various stack variables so ** that this routine begins comparing at the second field. */ if bSkip != 0 { *(*Tu32)(unsafe.Pointer(bp + 48)) = uint32(*(*uint8)(unsafe.Pointer(aKey1 + 1))) if *(*Tu32)(unsafe.Pointer(bp + 48)) < uint32(0x80) { idx1 = uint32(2) } else { idx1 = uint32(int32(1) + int32(_sqlite3GetVarint32(tls, aKey1+1, bp+48))) } *(*Tu32)(unsafe.Pointer(bp)) = uint32(*(*uint8)(unsafe.Pointer(aKey1))) d1 = *(*Tu32)(unsafe.Pointer(bp)) + _sqlite3VdbeSerialTypeLen(tls, *(*Tu32)(unsafe.Pointer(bp + 48))) i = int32(1) pRhs += 40 } else { v1 = uint32(*(*uint8)(unsafe.Pointer(aKey1))) *(*Tu32)(unsafe.Pointer(bp)) = v1 if v1 < uint32(0x80) { idx1 = uint32(1) } else { idx1 = uint32(_sqlite3GetVarint32(tls, aKey1, bp)) } d1 = *(*Tu32)(unsafe.Pointer(bp)) i = 0 } if d1 > uint32(nKey1) { (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FerrCode = uint8(_sqlite3CorruptError(tls, int32(89131))) return 0 /* Corruption */ } /* Only needed by assert() statements */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) for int32(1) != 0 { /* RHS is an integer */ if int32((*TMem)(unsafe.Pointer(pRhs)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 { *(*Tu32)(unsafe.Pointer(bp + 52)) = uint32(*(*uint8)(unsafe.Pointer(aKey1 + uintptr(idx1)))) if *(*Tu32)(unsafe.Pointer(bp + 52)) >= uint32(10) { if *(*Tu32)(unsafe.Pointer(bp + 52)) == uint32(10) { v2 = -int32(1) } else { v2 = +libc.Int32FromInt32(1) } rc = v2 } else { if *(*Tu32)(unsafe.Pointer(bp + 52)) == uint32(0) { rc = -int32(1) } else { if *(*Tu32)(unsafe.Pointer(bp + 52)) == uint32(7) { _serialGet7(tls, aKey1+uintptr(d1), bp+8) rc = -_sqlite3IntFloatCompare(tls, *(*Ti64)(unsafe.Pointer(pRhs)), *(*float64)(unsafe.Pointer(bp + 8))) } else { lhs = _vdbeRecordDecodeInt(tls, *(*Tu32)(unsafe.Pointer(bp + 52)), aKey1+uintptr(d1)) rhs = *(*Ti64)(unsafe.Pointer(pRhs)) if lhs < rhs { rc = -int32(1) } else { if lhs > rhs { rc = +libc.Int32FromInt32(1) } } } } } } else { if int32((*TMem)(unsafe.Pointer(pRhs)).Fflags)&int32(MEM_Real) != 0 { *(*Tu32)(unsafe.Pointer(bp + 52)) = uint32(*(*uint8)(unsafe.Pointer(aKey1 + uintptr(idx1)))) if *(*Tu32)(unsafe.Pointer(bp + 52)) >= uint32(10) { /* Serial types 12 or greater are strings and blobs (greater than ** numbers). Types 10 and 11 are currently "reserved for future ** use", so it doesn't really matter what the results of comparing ** them to numeric values are. */ if *(*Tu32)(unsafe.Pointer(bp + 52)) == uint32(10) { v3 = -int32(1) } else { v3 = +libc.Int32FromInt32(1) } rc = v3 } else { if *(*Tu32)(unsafe.Pointer(bp + 52)) == uint32(0) { rc = -int32(1) } else { if *(*Tu32)(unsafe.Pointer(bp + 52)) == uint32(7) { if _serialGet7(tls, aKey1+uintptr(d1), bp+8) != 0 { rc = -int32(1) /* mem1 is a NaN */ } else { if *(*float64)(unsafe.Pointer(bp + 8)) < *(*float64)(unsafe.Pointer(pRhs)) { rc = -int32(1) } else { if *(*float64)(unsafe.Pointer(bp + 8)) > *(*float64)(unsafe.Pointer(pRhs)) { rc = +libc.Int32FromInt32(1) } else { _ = libc.Int32FromInt32(0) } } } } else { _sqlite3VdbeSerialGet(tls, aKey1+uintptr(d1), *(*Tu32)(unsafe.Pointer(bp + 52)), bp+8) rc = _sqlite3IntFloatCompare(tls, *(*Ti64)(unsafe.Pointer(bp + 8)), *(*float64)(unsafe.Pointer(pRhs))) } } } } else { if int32((*TMem)(unsafe.Pointer(pRhs)).Fflags)&int32(MEM_Str) != 0 { *(*Tu32)(unsafe.Pointer(bp + 52)) = uint32(*(*uint8)(unsafe.Pointer(aKey1 + uintptr(idx1)))) if *(*Tu32)(unsafe.Pointer(bp + 52)) >= uint32(0x80) { _sqlite3GetVarint32(tls, aKey1+uintptr(idx1), bp+52) } if *(*Tu32)(unsafe.Pointer(bp + 52)) < uint32(12) { rc = -int32(1) } else { if !(*(*Tu32)(unsafe.Pointer(bp + 52))&libc.Uint32FromInt32(0x01) != 0) { rc = +libc.Int32FromInt32(1) } else { (*(*TMem)(unsafe.Pointer(bp + 8))).Fn = int32((*(*Tu32)(unsafe.Pointer(bp + 52)) - uint32(12)) / uint32(2)) if v5 = d1+uint32((*(*TMem)(unsafe.Pointer(bp + 8))).Fn) > uint32(nKey1); !v5 { v4 = (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FpKeyInfo pKeyInfo = v4 } if v5 || int32((*TKeyInfo)(unsafe.Pointer(v4)).FnAllField) <= i { (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FerrCode = uint8(_sqlite3CorruptError(tls, int32(89212))) return 0 /* Corruption */ } else { if *(*uintptr)(unsafe.Pointer(pKeyInfo + 20 + uintptr(i)*4)) != 0 { (*(*TMem)(unsafe.Pointer(bp + 8))).Fenc = (*TKeyInfo)(unsafe.Pointer(pKeyInfo)).Fenc (*(*TMem)(unsafe.Pointer(bp + 8))).Fdb = (*TKeyInfo)(unsafe.Pointer(pKeyInfo)).Fdb (*(*TMem)(unsafe.Pointer(bp + 8))).Fflags = uint16(MEM_Str) (*(*TMem)(unsafe.Pointer(bp + 8))).Fz = aKey1 + uintptr(d1) rc = _vdbeCompareMemString(tls, bp+8, pRhs, *(*uintptr)(unsafe.Pointer(pKeyInfo + 20 + uintptr(i)*4)), pPKey2+23) } else { if (*(*TMem)(unsafe.Pointer(bp + 8))).Fn < (*TMem)(unsafe.Pointer(pRhs)).Fn { v6 = (*(*TMem)(unsafe.Pointer(bp + 8))).Fn } else { v6 = (*TMem)(unsafe.Pointer(pRhs)).Fn } nCmp = v6 rc = libc.Xmemcmp(tls, aKey1+uintptr(d1), (*TMem)(unsafe.Pointer(pRhs)).Fz, uint32(nCmp)) if rc == 0 { rc = (*(*TMem)(unsafe.Pointer(bp + 8))).Fn - (*TMem)(unsafe.Pointer(pRhs)).Fn } } } } } } else { if int32((*TMem)(unsafe.Pointer(pRhs)).Fflags)&int32(MEM_Blob) != 0 { _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(bp + 52)) = uint32(*(*uint8)(unsafe.Pointer(aKey1 + uintptr(idx1)))) if *(*Tu32)(unsafe.Pointer(bp + 52)) >= uint32(0x80) { _sqlite3GetVarint32(tls, aKey1+uintptr(idx1), bp+52) } if *(*Tu32)(unsafe.Pointer(bp + 52)) < uint32(12) || *(*Tu32)(unsafe.Pointer(bp + 52))&uint32(0x01) != 0 { rc = -int32(1) } else { nStr = int32((*(*Tu32)(unsafe.Pointer(bp + 52)) - uint32(12)) / uint32(2)) if d1+uint32(nStr) > uint32(nKey1) { (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FerrCode = uint8(_sqlite3CorruptError(tls, int32(89242))) return 0 /* Corruption */ } else { if int32((*TMem)(unsafe.Pointer(pRhs)).Fflags)&int32(MEM_Zero) != 0 { if !(_isAllZero(tls, aKey1+uintptr(d1), nStr) != 0) { rc = int32(1) } else { rc = nStr - *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pRhs)).Fu)) } } else { if nStr < (*TMem)(unsafe.Pointer(pRhs)).Fn { v7 = nStr } else { v7 = (*TMem)(unsafe.Pointer(pRhs)).Fn } nCmp1 = v7 rc = libc.Xmemcmp(tls, aKey1+uintptr(d1), (*TMem)(unsafe.Pointer(pRhs)).Fz, uint32(nCmp1)) if rc == 0 { rc = nStr - (*TMem)(unsafe.Pointer(pRhs)).Fn } } } } } else { *(*Tu32)(unsafe.Pointer(bp + 52)) = uint32(*(*uint8)(unsafe.Pointer(aKey1 + uintptr(idx1)))) if *(*Tu32)(unsafe.Pointer(bp + 52)) == uint32(0) || *(*Tu32)(unsafe.Pointer(bp + 52)) == uint32(10) || *(*Tu32)(unsafe.Pointer(bp + 52)) == uint32(7) && _serialGet7(tls, aKey1+uintptr(d1), bp+8) != 0 { _ = libc.Int32FromInt32(0) } else { rc = int32(1) } } } } } if rc != 0 { sortFlags = int32(*(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FpKeyInfo)).FaSortFlags + uintptr(i)))) if sortFlags != 0 { if sortFlags&int32(KEYINFO_ORDER_BIGNULL) == 0 || sortFlags&int32(KEYINFO_ORDER_DESC) != libc.BoolInt32(*(*Tu32)(unsafe.Pointer(bp + 52)) == uint32(0) || int32((*TMem)(unsafe.Pointer(pRhs)).Fflags)&int32(MEM_Null) != 0) { rc = -rc } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* See comment below */ return rc } i++ if i == int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FnField) { break } pRhs += 40 d1 += _sqlite3VdbeSerialTypeLen(tls, *(*Tu32)(unsafe.Pointer(bp + 52))) if d1 > uint32(nKey1) { break } idx1 += uint32(_sqlite3VarintLen(tls, uint64(*(*Tu32)(unsafe.Pointer(bp + 52))))) if idx1 >= *(*Tu32)(unsafe.Pointer(bp)) { (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FerrCode = uint8(_sqlite3CorruptError(tls, int32(89293))) return 0 /* Corrupt index */ } } /* No memory allocation is ever used on mem1. Prove this using ** the following assert(). If the assert() fails, it indicates a ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */ _ = libc.Int32FromInt32(0) /* rc==0 here means that one or both of the keys ran out of fields and ** all the fields up to that point were equal. Return the default_rc ** value. */ _ = libc.Int32FromInt32(0) (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FeqSeen = uint8(1) return int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fdefault_rc) } func _sqlite3VdbeRecordCompare(tls *libc.TLS, nKey1 int32, pKey1 uintptr, pPKey2 uintptr) (r int32) { return _sqlite3VdbeRecordCompareWithSkip(tls, nKey1, pKey1, pPKey2, 0) } // C documentation // // /* // ** This function is an optimized version of sqlite3VdbeRecordCompare() // ** that (a) the first field of pPKey2 is an integer, and (b) the // ** size-of-header varint at the start of (pKey1/nKey1) fits in a single // ** byte (i.e. is less than 128). // ** // ** To avoid concerns about buffer overreads, this routine is only used // ** on schemas where the maximum valid header size is 63 bytes or less. // */ func _vdbeRecordCompareInt(tls *libc.TLS, nKey1 int32, pKey1 uintptr, pPKey2 uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var aKey uintptr var lhs, v Ti64 var res, serial_type int32 var _ /* x at bp+8 */ Tu64 var _ /* y at bp+0 */ Tu32 _, _, _, _, _ = aKey, lhs, res, serial_type, v aKey = pKey1 + uintptr(int32(*(*Tu8)(unsafe.Pointer(pKey1)))&int32(0x3F)) serial_type = int32(*(*Tu8)(unsafe.Pointer(pKey1 + 1))) _ = libc.Int32FromInt32(0) switch serial_type { case int32(1): /* 1-byte signed integer */ lhs = int64(int8(*(*Tu8)(unsafe.Pointer(aKey)))) case int32(2): /* 2-byte signed integer */ lhs = int64(libc.Int32FromInt32(256)*int32(int8(*(*Tu8)(unsafe.Pointer(aKey)))) | int32(*(*Tu8)(unsafe.Pointer(aKey + 1)))) case int32(3): /* 3-byte signed integer */ lhs = int64(libc.Int32FromInt32(65536)*int32(int8(*(*Tu8)(unsafe.Pointer(aKey)))) | int32(*(*Tu8)(unsafe.Pointer(aKey + 1)))< lhs { res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr1) } else { if v < lhs { res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr2) } else { if int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FnField) > int32(1) { /* The first fields of the two keys are equal. Compare the trailing ** fields. */ res = _sqlite3VdbeRecordCompareWithSkip(tls, nKey1, pKey1, pPKey2, int32(1)) } else { /* The first fields of the two keys are equal and there are no trailing ** fields. Return pPKey2->default_rc in this case. */ res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fdefault_rc) (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FeqSeen = uint8(1) } } } _ = libc.Int32FromInt32(0) return res } // C documentation // // /* // ** This function is an optimized version of sqlite3VdbeRecordCompare() // ** that (a) the first field of pPKey2 is a string, that (b) the first field // ** uses the collation sequence BINARY and (c) that the size-of-header varint // ** at the start of (pKey1/nKey1) fits in a single byte. // */ func _vdbeRecordCompareString(tls *libc.TLS, nKey1 int32, pKey1 uintptr, pPKey2 uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var aKey1 uintptr var nCmp, nStr, res, szHdr, v1 int32 var _ /* serial_type at bp+0 */ int32 _, _, _, _, _, _ = aKey1, nCmp, nStr, res, szHdr, v1 aKey1 = pKey1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp)) = int32(int8(*(*Tu8)(unsafe.Pointer(aKey1 + 1)))) goto vrcs_restart vrcs_restart: ; if *(*int32)(unsafe.Pointer(bp)) < int32(12) { if *(*int32)(unsafe.Pointer(bp)) < 0 { _sqlite3GetVarint32(tls, aKey1+1, bp) if *(*int32)(unsafe.Pointer(bp)) >= int32(12) { goto vrcs_restart } _ = libc.Int32FromInt32(0) } res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr1) /* (pKey1/nKey1) is a number or a null */ } else { if !(*(*int32)(unsafe.Pointer(bp))&libc.Int32FromInt32(0x01) != 0) { res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr2) /* (pKey1/nKey1) is a blob */ } else { szHdr = int32(*(*Tu8)(unsafe.Pointer(aKey1))) nStr = (*(*int32)(unsafe.Pointer(bp)) - int32(12)) / int32(2) if szHdr+nStr > nKey1 { (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FerrCode = uint8(_sqlite3CorruptError(tls, int32(89456))) return 0 /* Corruption */ } if (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fn < nStr { v1 = (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fn } else { v1 = nStr } nCmp = v1 res = libc.Xmemcmp(tls, aKey1+uintptr(szHdr), (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fu.Fz, uint32(nCmp)) if res > 0 { res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr2) } else { if res < 0 { res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr1) } else { res = nStr - (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fn if res == 0 { if int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FnField) > int32(1) { res = _sqlite3VdbeRecordCompareWithSkip(tls, nKey1, pKey1, pPKey2, int32(1)) } else { res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fdefault_rc) (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FeqSeen = uint8(1) } } else { if res > 0 { res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr2) } else { res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr1) } } } } } } _ = libc.Int32FromInt32(0) return res } // C documentation // // /* // ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function // ** suitable for comparing serialized records to the unpacked record passed // ** as the only argument. // */ func _sqlite3VdbeFindCompare(tls *libc.TLS, p uintptr) (r TRecordCompare) { var flags int32 _ = flags /* varintRecordCompareInt() and varintRecordCompareString() both assume ** that the size-of-header varint that occurs at the start of each record ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt() ** also assumes that it is safe to overread a buffer by at least the ** maximum possible legal header size plus 8 bytes. Because there is ** guaranteed to be at least 74 (but not 136) bytes of padding following each ** buffer passed to varintRecordCompareInt() this makes it convenient to ** limit the size of the header to 64 bytes in cases where the first field ** is an integer. ** ** The easiest way to enforce this limit is to consider only records with ** 13 fields or less. If the first field is an integer, the maximum legal ** header size is (12*5 + 1 + 1) bytes. */ if int32((*TKeyInfo)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FpKeyInfo)).FnAllField) <= int32(13) { flags = int32((*(*TMem)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FaMem))).Fflags) if *(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FpKeyInfo)).FaSortFlags)) != 0 { if int32(*(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FpKeyInfo)).FaSortFlags)))&int32(KEYINFO_ORDER_BIGNULL) != 0 { return __ccgo_fp(_sqlite3VdbeRecordCompare) } (*TUnpackedRecord)(unsafe.Pointer(p)).Fr1 = int8(1) (*TUnpackedRecord)(unsafe.Pointer(p)).Fr2 = int8(-int32(1)) } else { (*TUnpackedRecord)(unsafe.Pointer(p)).Fr1 = int8(-int32(1)) (*TUnpackedRecord)(unsafe.Pointer(p)).Fr2 = int8(1) } if flags&int32(MEM_Int) != 0 { *(*Ti64)(unsafe.Pointer(p + 8)) = *(*Ti64)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FaMem)) return __ccgo_fp(_vdbeRecordCompareInt) } if flags&(libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)|libc.Int32FromInt32(MEM_Null)|libc.Int32FromInt32(MEM_Blob)) == 0 && *(*uintptr)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FpKeyInfo + 20)) == uintptr(0) { _ = libc.Int32FromInt32(0) (*TUnpackedRecord)(unsafe.Pointer(p)).Fu.Fz = (*(*TMem)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FaMem))).Fz (*TUnpackedRecord)(unsafe.Pointer(p)).Fn = (*(*TMem)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FaMem))).Fn return __ccgo_fp(_vdbeRecordCompareString) } } return __ccgo_fp(_sqlite3VdbeRecordCompare) } // C documentation // // /* // ** pCur points at an index entry created using the OP_MakeRecord opcode. // ** Read the rowid (the last field in the record) and store it in *rowid. // ** Return SQLITE_OK if everything works, or an error code otherwise. // ** // ** pCur might be pointing to text obtained from a corrupt database file. // ** So the content cannot be trusted. Do appropriate checks on the content. // */ func _sqlite3VdbeIdxRowid(tls *libc.TLS, db uintptr, pCur uintptr, rowid uintptr) (r int32) { bp := tls.Alloc(96) defer tls.Free(96) var lenRowid Tu32 var nCellKey Ti64 var rc int32 var _ /* m at bp+8 */ TMem var _ /* szHdr at bp+0 */ Tu32 var _ /* typeRowid at bp+4 */ Tu32 var _ /* v at bp+48 */ TMem _, _, _ = lenRowid, nCellKey, rc nCellKey = 0 /* Get the size of the index entry. Only indices entries of less ** than 2GiB are support - anything large must be database corruption. ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so ** this code can safely assume that nCellKey is 32-bits */ _ = libc.Int32FromInt32(0) nCellKey = int64(_sqlite3BtreePayloadSize(tls, pCur)) _ = libc.Int32FromInt32(0) /* Read in the complete content of the index entry */ _sqlite3VdbeMemInit(tls, bp+8, db, uint16(0)) rc = _sqlite3VdbeMemFromBtreeZeroOffset(tls, pCur, uint32(nCellKey), bp+8) if rc != 0 { return rc } /* The index entry must begin with a header size */ *(*Tu32)(unsafe.Pointer(bp)) = uint32(*(*Tu8)(unsafe.Pointer((*(*TMem)(unsafe.Pointer(bp + 8))).Fz))) if *(*Tu32)(unsafe.Pointer(bp)) >= uint32(0x80) { _sqlite3GetVarint32(tls, (*(*TMem)(unsafe.Pointer(bp + 8))).Fz, bp) } _ = libc.Int32FromInt32(0) if *(*Tu32)(unsafe.Pointer(bp)) < uint32(3) || *(*Tu32)(unsafe.Pointer(bp)) > uint32((*(*TMem)(unsafe.Pointer(bp + 8))).Fn) { goto idx_rowid_corruption } /* The last field of the index should be an integer - the ROWID. ** Verify that the last entry really is an integer. */ *(*Tu32)(unsafe.Pointer(bp + 4)) = uint32(*(*Tu8)(unsafe.Pointer((*(*TMem)(unsafe.Pointer(bp + 8))).Fz + uintptr(*(*Tu32)(unsafe.Pointer(bp))-uint32(1))))) if *(*Tu32)(unsafe.Pointer(bp + 4)) >= uint32(0x80) { _sqlite3GetVarint32(tls, (*(*TMem)(unsafe.Pointer(bp + 8))).Fz+uintptr(*(*Tu32)(unsafe.Pointer(bp))-uint32(1)), bp+4) } if *(*Tu32)(unsafe.Pointer(bp + 4)) < uint32(1) || *(*Tu32)(unsafe.Pointer(bp + 4)) > uint32(9) || *(*Tu32)(unsafe.Pointer(bp + 4)) == uint32(7) { goto idx_rowid_corruption } lenRowid = uint32(_sqlite3SmallTypeSizes[*(*Tu32)(unsafe.Pointer(bp + 4))]) if uint32((*(*TMem)(unsafe.Pointer(bp + 8))).Fn) < *(*Tu32)(unsafe.Pointer(bp))+lenRowid { goto idx_rowid_corruption } /* Fetch the integer off the end of the index record */ _sqlite3VdbeSerialGet(tls, (*(*TMem)(unsafe.Pointer(bp + 8))).Fz+uintptr(uint32((*(*TMem)(unsafe.Pointer(bp + 8))).Fn)-lenRowid), *(*Tu32)(unsafe.Pointer(bp + 4)), bp+48) *(*Ti64)(unsafe.Pointer(rowid)) = *(*Ti64)(unsafe.Pointer(bp + 48)) _sqlite3VdbeMemReleaseMalloc(tls, bp+8) return SQLITE_OK /* Jump here if database corruption is detected after m has been ** allocated. Free the m object and return SQLITE_CORRUPT. */ goto idx_rowid_corruption idx_rowid_corruption: ; _sqlite3VdbeMemReleaseMalloc(tls, bp+8) return _sqlite3CorruptError(tls, int32(89614)) } // C documentation // // /* // ** Compare the key of the index entry that cursor pC is pointing to against // ** the key string in pUnpacked. Write into *pRes a number // ** that is negative, zero, or positive if pC is less than, equal to, // ** or greater than pUnpacked. Return SQLITE_OK on success. // ** // ** pUnpacked is either created without a rowid or is truncated so that it // ** omits the rowid at the end. The rowid at the end of the index entry // ** is ignored as well. Hence, this routine only compares the prefixes // ** of the keys prior to the final rowid, not the entire key. // */ func _sqlite3VdbeIdxKeyCompare(tls *libc.TLS, db uintptr, pC uintptr, pUnpacked uintptr, res uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var nCellKey Ti64 var pCur uintptr var rc int32 var _ /* m at bp+0 */ TMem _, _, _ = nCellKey, pCur, rc nCellKey = 0 _ = libc.Int32FromInt32(0) pCur = *(*uintptr)(unsafe.Pointer(pC + 36)) _ = libc.Int32FromInt32(0) nCellKey = int64(_sqlite3BtreePayloadSize(tls, pCur)) /* nCellKey will always be between 0 and 0xffffffff because of the way ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ if nCellKey <= 0 || nCellKey > int64(0x7fffffff) { *(*int32)(unsafe.Pointer(res)) = 0 return _sqlite3CorruptError(tls, int32(89647)) } _sqlite3VdbeMemInit(tls, bp, db, uint16(0)) rc = _sqlite3VdbeMemFromBtreeZeroOffset(tls, pCur, uint32(nCellKey), bp) if rc != 0 { return rc } *(*int32)(unsafe.Pointer(res)) = _sqlite3VdbeRecordCompareWithSkip(tls, (*(*TMem)(unsafe.Pointer(bp))).Fn, (*(*TMem)(unsafe.Pointer(bp))).Fz, pUnpacked, 0) _sqlite3VdbeMemReleaseMalloc(tls, bp) return SQLITE_OK } // C documentation // // /* // ** This routine sets the value to be returned by subsequent calls to // ** sqlite3_changes() on the database handle 'db'. // */ func _sqlite3VdbeSetChanges(tls *libc.TLS, db uintptr, nChange Ti64) { _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).FnChange = nChange *(*Ti64)(unsafe.Pointer(db + 112)) += nChange } // C documentation // // /* // ** Set a flag in the vdbe to update the change counter when it is finalised // ** or reset. // */ func _sqlite3VdbeCountChanges(tls *libc.TLS, v uintptr) { libc.SetBitFieldPtr16Uint32(v+152, libc.Uint32FromInt32(1), 4, 0x10) } // C documentation // // /* // ** Mark every prepared statement associated with a database connection // ** as expired. // ** // ** An expired statement means that recompilation of the statement is // ** recommend. Statements expire when things happen that make their // ** programs obsolete. Removing user-defined functions or collating // ** sequences, or changing an authorization function are the types of // ** things that make prepared statements obsolete. // ** // ** If iCode is 1, then expiration is advisory. The statement should // ** be reprepared before being restarted, but if it is already running // ** it is allowed to run to completion. // ** // ** Internally, this function just sets the Vdbe.expired flag on all // ** prepared statements. The flag is set to 1 for an immediate expiration // ** and set to 2 for an advisory expiration. // */ func _sqlite3ExpirePreparedStatements(tls *libc.TLS, db uintptr, iCode int32) { var p uintptr _ = p p = (*Tsqlite3)(unsafe.Pointer(db)).FpVdbe for { if !(p != 0) { break } libc.SetBitFieldPtr16Uint32(p+152, uint32(iCode+libc.Int32FromInt32(1)), 0, 0x3) goto _1 _1: ; p = (*TVdbe)(unsafe.Pointer(p)).FpVNext } } // C documentation // // /* // ** Return the database associated with the Vdbe. // */ func _sqlite3VdbeDb(tls *libc.TLS, v uintptr) (r uintptr) { return (*TVdbe)(unsafe.Pointer(v)).Fdb } // C documentation // // /* // ** Return the SQLITE_PREPARE flags for a Vdbe. // */ func _sqlite3VdbePrepareFlags(tls *libc.TLS, v uintptr) (r Tu8) { return (*TVdbe)(unsafe.Pointer(v)).FprepFlags } // C documentation // // /* // ** Return a pointer to an sqlite3_value structure containing the value bound // ** parameter iVar of VM v. Except, if the value is an SQL NULL, return // ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_* // ** constants) to the value before returning it. // ** // ** The returned value must be freed by the caller using sqlite3ValueFree(). // */ func _sqlite3VdbeGetBoundValue(tls *libc.TLS, v uintptr, iVar int32, aff Tu8) (r uintptr) { var pMem, pRet uintptr _, _ = pMem, pRet _ = libc.Int32FromInt32(0) if v != 0 { pMem = (*TVdbe)(unsafe.Pointer(v)).FaVar + uintptr(iVar-int32(1))*40 _ = libc.Int32FromInt32(0) if 0 == int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Null) { pRet = _sqlite3ValueNew(tls, (*TVdbe)(unsafe.Pointer(v)).Fdb) if pRet != 0 { _sqlite3VdbeMemCopy(tls, pRet, pMem) _sqlite3ValueApplyAffinity(tls, pRet, aff, uint8(SQLITE_UTF8)) } return pRet } } return uintptr(0) } // C documentation // // /* // ** Configure SQL variable iVar so that binding a new value to it signals // ** to sqlite3_reoptimize() that re-preparing the statement may result // ** in a better query plan. // */ func _sqlite3VdbeSetVarmask(tls *libc.TLS, v uintptr, iVar int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if iVar >= int32(32) { *(*Tu32)(unsafe.Pointer(v + 220)) |= uint32(0x80000000) } else { *(*Tu32)(unsafe.Pointer(v + 220)) |= libc.Uint32FromInt32(1) << (iVar - libc.Int32FromInt32(1)) } } // C documentation // // /* // ** Cause a function to throw an error if it was call from OP_PureFunc // ** rather than OP_Function. // ** // ** OP_PureFunc means that the function must be deterministic, and should // ** throw an error if it is given inputs that would make it non-deterministic. // ** This routine is invoked by date/time functions that use non-deterministic // ** features such as 'now'. // */ func _sqlite3NotPureFunc(tls *libc.TLS, pCtx uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var pOp, zContext, zMsg uintptr _, _, _ = pOp, zContext, zMsg if (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpVdbe == uintptr(0) { return int32(1) } pOp = (*TVdbe)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpVdbe)).FaOp + uintptr((*Tsqlite3_context)(unsafe.Pointer(pCtx)).FiOp)*20 if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_PureFunc) { if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fp5)&int32(NC_IsCheck) != 0 { zContext = __ccgo_ts + 5255 } else { if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fp5)&int32(NC_GenCol) != 0 { zContext = __ccgo_ts + 5274 } else { zContext = __ccgo_ts + 5293 } } zMsg = Xsqlite3_mprintf(tls, __ccgo_ts+5302, libc.VaList(bp+8, (*TFuncDef)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpFunc)).FzName, zContext)) Xsqlite3_result_error(tls, pCtx, zMsg, -int32(1)) Xsqlite3_free(tls, zMsg) return 0 } return int32(1) } // C documentation // // /* // ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored // ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored // ** in memory obtained from sqlite3DbMalloc). // */ func _sqlite3VtabImportErrmsg(tls *libc.TLS, p uintptr, pVtab uintptr) { var db uintptr _ = db if (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg != 0 { db = (*TVdbe)(unsafe.Pointer(p)).Fdb _sqlite3DbFree(tls, db, (*TVdbe)(unsafe.Pointer(p)).FzErrMsg) (*TVdbe)(unsafe.Pointer(p)).FzErrMsg = _sqlite3DbStrDup(tls, db, (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg) Xsqlite3_free(tls, (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg) (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg = uintptr(0) } } // C documentation // // /* // ** If the second argument is not NULL, release any allocations associated // ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord // ** structure itself, using sqlite3DbFree(). // ** // ** This function is used to free UnpackedRecord structures allocated by // ** the vdbeUnpackRecord() function found in vdbeapi.c. // */ func _vdbeFreeUnpacked(tls *libc.TLS, db uintptr, nField int32, p uintptr) { var i int32 var pMem uintptr _, _ = i, pMem _ = libc.Int32FromInt32(0) if p != 0 { i = 0 for { if !(i < nField) { break } pMem = (*TUnpackedRecord)(unsafe.Pointer(p)).FaMem + uintptr(i)*40 if (*TMem)(unsafe.Pointer(pMem)).FzMalloc != 0 { _sqlite3VdbeMemReleaseMalloc(tls, pMem) } goto _1 _1: ; i++ } _sqlite3DbNNFreeNN(tls, db, p) } } // C documentation // // /* // ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call, // ** then cursor passed as the second argument should point to the row about // ** to be update or deleted. If the application calls sqlite3_preupdate_old(), // ** the required value will be read from the row the cursor points to. // */ func _sqlite3VdbePreUpdateHook(tls *libc.TLS, v uintptr, pCsr uintptr, op int32, zDb uintptr, pTab uintptr, iKey1 Ti64, iReg int32, iBlobWrite int32) { bp := tls.Alloc(96) defer tls.Free(96) var db, zTbl uintptr var i int32 var iKey2, v1 Ti64 var _ /* preupdate at bp+0 */ TPreUpdate _, _, _, _, _ = db, i, iKey2, zTbl, v1 db = (*TVdbe)(unsafe.Pointer(v)).Fdb zTbl = (*TTable)(unsafe.Pointer(pTab)).FzName _ = libc.Int32FromInt32(0) libc.Xmemset(tls, bp, 0, uint32(88)) if libc.BoolInt32((*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0)) == 0 { v1 = libc.Int64FromInt32(0) iKey2 = v1 iKey1 = v1 (*(*TPreUpdate)(unsafe.Pointer(bp))).FpPk = _sqlite3PrimaryKeyIndex(tls, pTab) } else { if op == int32(SQLITE_UPDATE) { iKey2 = *(*Ti64)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(v)).FaMem + uintptr(iReg)*40)) } else { iKey2 = iKey1 } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*(*TPreUpdate)(unsafe.Pointer(bp))).Fv = v (*(*TPreUpdate)(unsafe.Pointer(bp))).FpCsr = pCsr (*(*TPreUpdate)(unsafe.Pointer(bp))).Fop = op (*(*TPreUpdate)(unsafe.Pointer(bp))).FiNewReg = iReg (*(*TPreUpdate)(unsafe.Pointer(bp))).Fkeyinfo.Fdb = db (*(*TPreUpdate)(unsafe.Pointer(bp))).Fkeyinfo.Fenc = (*Tsqlite3)(unsafe.Pointer(db)).Fenc (*(*TPreUpdate)(unsafe.Pointer(bp))).Fkeyinfo.FnKeyField = uint16((*TTable)(unsafe.Pointer(pTab)).FnCol) (*(*TPreUpdate)(unsafe.Pointer(bp))).Fkeyinfo.FaSortFlags = uintptr(unsafe.Pointer(&_fakeSortOrder)) (*(*TPreUpdate)(unsafe.Pointer(bp))).FiKey1 = iKey1 (*(*TPreUpdate)(unsafe.Pointer(bp))).FiKey2 = iKey2 (*(*TPreUpdate)(unsafe.Pointer(bp))).FpTab = pTab (*(*TPreUpdate)(unsafe.Pointer(bp))).FiBlobWrite = iBlobWrite (*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdate = bp (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr, uintptr, Tsqlite3_int64, Tsqlite3_int64))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2) (*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdate = uintptr(0) _sqlite3DbFree(tls, db, (*(*TPreUpdate)(unsafe.Pointer(bp))).FaRecord) _vdbeFreeUnpacked(tls, db, int32((*(*TPreUpdate)(unsafe.Pointer(bp))).Fkeyinfo.FnKeyField)+int32(1), (*(*TPreUpdate)(unsafe.Pointer(bp))).FpUnpacked) _vdbeFreeUnpacked(tls, db, int32((*(*TPreUpdate)(unsafe.Pointer(bp))).Fkeyinfo.FnKeyField)+int32(1), (*(*TPreUpdate)(unsafe.Pointer(bp))).FpNewUnpacked) if (*(*TPreUpdate)(unsafe.Pointer(bp))).FaNew != 0 { i = 0 for { if !(i < int32((*TVdbeCursor)(unsafe.Pointer(pCsr)).FnField)) { break } _sqlite3VdbeMemRelease(tls, (*(*TPreUpdate)(unsafe.Pointer(bp))).FaNew+uintptr(i)*40) goto _2 _2: ; i++ } _sqlite3DbNNFreeNN(tls, db, (*(*TPreUpdate)(unsafe.Pointer(bp))).FaNew) } } var _fakeSortOrder Tu8 /************** End of vdbeaux.c *********************************************/ /************** Begin file vdbeapi.c *****************************************/ /* ** 2004 May 26 ** ** 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 contains code use to implement APIs that are part of the ** VDBE. */ /* #include "sqliteInt.h" */ /* #include "vdbeInt.h" */ /* #include "opcodes.h" */ // C documentation // // /* // ** Return TRUE (non-zero) of the statement supplied as an argument needs // ** to be recompiled. A statement needs to be recompiled whenever the // ** execution environment changes in a way that would alter the program // ** that sqlite3_prepare() generates. For example, if new functions or // ** collating sequences are registered or if an authorizer function is // ** added or changed. // */ func Xsqlite3_expired(tls *libc.TLS, pStmt uintptr) (r int32) { var p uintptr _ = p p = pStmt return libc.BoolInt32(p == uintptr(0) || int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x3>>0)) != 0) } // C documentation // // /* // ** Check on a Vdbe to make sure it has not been finalized. Log // ** an error and return true if it has been finalized (or is otherwise // ** invalid). Return false if it is ok. // */ func _vdbeSafety(tls *libc.TLS, p uintptr) (r int32) { if (*TVdbe)(unsafe.Pointer(p)).Fdb == uintptr(0) { Xsqlite3_log(tls, int32(SQLITE_MISUSE), __ccgo_ts+5338, 0) return int32(1) } else { return 0 } return r } func _vdbeSafetyNotNull(tls *libc.TLS, p uintptr) (r int32) { if p == uintptr(0) { Xsqlite3_log(tls, int32(SQLITE_MISUSE), __ccgo_ts+5383, 0) return int32(1) } else { return _vdbeSafety(tls, p) } return r } // C documentation // // /* // ** Invoke the profile callback. This routine is only called if we already // ** know that the profile callback is defined and needs to be invoked. // */ func _invokeProfileCallback(tls *libc.TLS, db uintptr, p uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* iElapse at bp+8 */ Tsqlite3_int64 var _ /* iNow at bp+0 */ Tsqlite3_int64 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3OsCurrentTimeInt64(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, bp) *(*Tsqlite3_int64)(unsafe.Pointer(bp + 8)) = (*(*Tsqlite3_int64)(unsafe.Pointer(bp)) - (*TVdbe)(unsafe.Pointer(p)).FstartTime) * int64(1000000) if (*Tsqlite3)(unsafe.Pointer(db)).FxProfile != 0 { (*(*func(*libc.TLS, uintptr, uintptr, Tu64))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxProfile})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpProfileArg, (*TVdbe)(unsafe.Pointer(p)).FzSql, uint64(*(*Tsqlite3_int64)(unsafe.Pointer(bp + 8)))) } if int32((*Tsqlite3)(unsafe.Pointer(db)).FmTrace)&int32(SQLITE_TRACE_PROFILE) != 0 { (*(*func(*libc.TLS, Tu32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{*(*uintptr)(unsafe.Pointer(&(*Tsqlite3)(unsafe.Pointer(db)).Ftrace))})))(tls, uint32(SQLITE_TRACE_PROFILE), (*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg, p, bp+8) } (*TVdbe)(unsafe.Pointer(p)).FstartTime = 0 } /* ** The checkProfileCallback(DB,P) macro checks to see if a profile callback ** is needed, and it invokes the callback if it is needed. */ // C documentation // // /* // ** The following routine destroys a virtual machine that is created by // ** the sqlite3_compile() routine. The integer returned is an SQLITE_ // ** success/failure code that describes the result of executing the virtual // ** machine. // ** // ** This routine sets the error code and string returned by // ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). // */ func Xsqlite3_finalize(tls *libc.TLS, pStmt uintptr) (r int32) { var db, v uintptr var rc int32 _, _, _ = db, rc, v if pStmt == uintptr(0) { /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL ** pointer is a harmless no-op. */ rc = SQLITE_OK } else { v = pStmt db = (*TVdbe)(unsafe.Pointer(v)).Fdb if _vdbeSafety(tls, v) != 0 { return _sqlite3MisuseError(tls, int32(90035)) } Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if (*TVdbe)(unsafe.Pointer(v)).FstartTime > 0 { _invokeProfileCallback(tls, db, v) } _ = libc.Int32FromInt32(0) rc = _sqlite3VdbeReset(tls, v) _sqlite3VdbeDelete(tls, v) rc = _sqlite3ApiExit(tls, db, rc) _sqlite3LeaveMutexAndCloseZombie(tls, db) } return rc } // C documentation // // /* // ** Terminate the current execution of an SQL statement and reset it // ** back to its starting state so that it can be reused. A success code from // ** the prior execution is returned. // ** // ** This routine sets the error code and string returned by // ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). // */ func Xsqlite3_reset(tls *libc.TLS, pStmt uintptr) (r int32) { var db, v uintptr var rc int32 _, _, _ = db, rc, v if pStmt == uintptr(0) { rc = SQLITE_OK } else { v = pStmt db = (*TVdbe)(unsafe.Pointer(v)).Fdb Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if (*TVdbe)(unsafe.Pointer(v)).FstartTime > 0 { _invokeProfileCallback(tls, db, v) } rc = _sqlite3VdbeReset(tls, v) _sqlite3VdbeRewind(tls, v) _ = libc.Int32FromInt32(0) rc = _sqlite3ApiExit(tls, db, rc) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) } return rc } // C documentation // // /* // ** Set all the parameters in the compiled SQL statement to NULL. // */ func Xsqlite3_clear_bindings(tls *libc.TLS, pStmt uintptr) (r int32) { var i, rc int32 var mutex, p uintptr _, _, _, _ = i, mutex, p, rc rc = SQLITE_OK p = pStmt mutex = (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex Xsqlite3_mutex_enter(tls, mutex) i = 0 for { if !(i < int32((*TVdbe)(unsafe.Pointer(p)).FnVar)) { break } _sqlite3VdbeMemRelease(tls, (*TVdbe)(unsafe.Pointer(p)).FaVar+uintptr(i)*40) (*(*TMem)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FaVar + uintptr(i)*40))).Fflags = uint16(MEM_Null) goto _1 _1: ; i++ } _ = libc.Int32FromInt32(0) if (*TVdbe)(unsafe.Pointer(p)).Fexpmask != 0 { libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(1), 0, 0x3) } Xsqlite3_mutex_leave(tls, mutex) return rc } // C documentation // // /**************************** sqlite3_value_ ******************************* // ** The following routines extract information from a Mem or sqlite3_value // ** structure. // */ func Xsqlite3_value_blob(tls *libc.TLS, pVal uintptr) (r uintptr) { var p, v3, p2 uintptr var v1 int32 _, _, _, _ = p, v1, v3, p2 p = pVal if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Blob)|libc.Int32FromInt32(MEM_Str)) != 0 { if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Zero) != 0 { v1 = _sqlite3VdbeMemExpandBlob(tls, p) } else { v1 = 0 } if v1 != SQLITE_OK { _ = libc.Int32FromInt32(0) return uintptr(0) } p2 = p + 16 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(MEM_Blob)) if (*TMem)(unsafe.Pointer(p)).Fn != 0 { v3 = (*TMem)(unsafe.Pointer(p)).Fz } else { v3 = uintptr(0) } return v3 } else { return Xsqlite3_value_text(tls, pVal) } return r } func Xsqlite3_value_bytes(tls *libc.TLS, pVal uintptr) (r int32) { return _sqlite3ValueBytes(tls, pVal, uint8(SQLITE_UTF8)) } func Xsqlite3_value_bytes16(tls *libc.TLS, pVal uintptr) (r int32) { return _sqlite3ValueBytes(tls, pVal, uint8(SQLITE_UTF16LE)) } func Xsqlite3_value_double(tls *libc.TLS, pVal uintptr) (r float64) { return _sqlite3VdbeRealValue(tls, pVal) } func Xsqlite3_value_int(tls *libc.TLS, pVal uintptr) (r int32) { return int32(_sqlite3VdbeIntValue(tls, pVal)) } func Xsqlite3_value_int64(tls *libc.TLS, pVal uintptr) (r Tsqlite_int64) { return _sqlite3VdbeIntValue(tls, pVal) } func Xsqlite3_value_subtype(tls *libc.TLS, pVal uintptr) (r uint32) { var pMem uintptr var v1 int32 _, _ = pMem, v1 pMem = pVal if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Subtype) != 0 { v1 = int32((*TMem)(unsafe.Pointer(pMem)).FeSubtype) } else { v1 = 0 } return uint32(v1) } func Xsqlite3_value_pointer(tls *libc.TLS, pVal uintptr, zPType uintptr) (r uintptr) { var p uintptr _ = p p = pVal if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Term)|libc.Int32FromInt32(MEM_Subtype)) == libc.Int32FromInt32(MEM_Null)|libc.Int32FromInt32(MEM_Term)|libc.Int32FromInt32(MEM_Subtype) && zPType != uintptr(0) && int32((*TMem)(unsafe.Pointer(p)).FeSubtype) == int32('p') && libc.Xstrcmp(tls, *(*uintptr)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(p)).Fu)), zPType) == 0 { return (*TMem)(unsafe.Pointer(p)).Fz } else { return uintptr(0) } return r } func Xsqlite3_value_text(tls *libc.TLS, pVal uintptr) (r uintptr) { return _sqlite3ValueText(tls, pVal, uint8(SQLITE_UTF8)) } func Xsqlite3_value_text16(tls *libc.TLS, pVal uintptr) (r uintptr) { return _sqlite3ValueText(tls, pVal, uint8(SQLITE_UTF16LE)) } func Xsqlite3_value_text16be(tls *libc.TLS, pVal uintptr) (r uintptr) { return _sqlite3ValueText(tls, pVal, uint8(SQLITE_UTF16BE)) } func Xsqlite3_value_text16le(tls *libc.TLS, pVal uintptr) (r uintptr) { return _sqlite3ValueText(tls, pVal, uint8(SQLITE_UTF16LE)) } // C documentation // // /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five // ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating // ** point number string BLOB NULL // */ func Xsqlite3_value_type(tls *libc.TLS, pVal uintptr) (r int32) { return int32(_aType[int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fflags)&int32(MEM_AffMask)]) } var _aType = [64]Tu8{ 0: uint8(SQLITE_BLOB), 1: uint8(SQLITE_NULL), 2: uint8(SQLITE_TEXT), 3: uint8(SQLITE_NULL), 4: uint8(SQLITE_INTEGER), 5: uint8(SQLITE_NULL), 6: uint8(SQLITE_INTEGER), 7: uint8(SQLITE_NULL), 8: uint8(SQLITE_FLOAT), 9: uint8(SQLITE_NULL), 10: uint8(SQLITE_FLOAT), 11: uint8(SQLITE_NULL), 12: uint8(SQLITE_INTEGER), 13: uint8(SQLITE_NULL), 14: uint8(SQLITE_INTEGER), 15: uint8(SQLITE_NULL), 16: uint8(SQLITE_BLOB), 17: uint8(SQLITE_NULL), 18: uint8(SQLITE_TEXT), 19: uint8(SQLITE_NULL), 20: uint8(SQLITE_INTEGER), 21: uint8(SQLITE_NULL), 22: uint8(SQLITE_INTEGER), 23: uint8(SQLITE_NULL), 24: uint8(SQLITE_FLOAT), 25: uint8(SQLITE_NULL), 26: uint8(SQLITE_FLOAT), 27: uint8(SQLITE_NULL), 28: uint8(SQLITE_INTEGER), 29: uint8(SQLITE_NULL), 30: uint8(SQLITE_INTEGER), 31: uint8(SQLITE_NULL), 32: uint8(SQLITE_FLOAT), 33: uint8(SQLITE_NULL), 34: uint8(SQLITE_FLOAT), 35: uint8(SQLITE_NULL), 36: uint8(SQLITE_FLOAT), 37: uint8(SQLITE_NULL), 38: uint8(SQLITE_FLOAT), 39: uint8(SQLITE_NULL), 40: uint8(SQLITE_FLOAT), 41: uint8(SQLITE_NULL), 42: uint8(SQLITE_FLOAT), 43: uint8(SQLITE_NULL), 44: uint8(SQLITE_FLOAT), 45: uint8(SQLITE_NULL), 46: uint8(SQLITE_FLOAT), 47: uint8(SQLITE_NULL), 48: uint8(SQLITE_BLOB), 49: uint8(SQLITE_NULL), 50: uint8(SQLITE_TEXT), 51: uint8(SQLITE_NULL), 52: uint8(SQLITE_FLOAT), 53: uint8(SQLITE_NULL), 54: uint8(SQLITE_FLOAT), 55: uint8(SQLITE_NULL), 56: uint8(SQLITE_FLOAT), 57: uint8(SQLITE_NULL), 58: uint8(SQLITE_FLOAT), 59: uint8(SQLITE_NULL), 60: uint8(SQLITE_FLOAT), 61: uint8(SQLITE_NULL), 62: uint8(SQLITE_FLOAT), 63: uint8(SQLITE_NULL), } func Xsqlite3_value_encoding(tls *libc.TLS, pVal uintptr) (r int32) { return int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fenc) } // C documentation // // /* Return true if a parameter to xUpdate represents an unchanged column */ func Xsqlite3_value_nochange(tls *libc.TLS, pVal uintptr) (r int32) { return libc.BoolInt32(int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fflags)&(libc.Int32FromInt32(MEM_Null)|libc.Int32FromInt32(MEM_Zero)) == libc.Int32FromInt32(MEM_Null)|libc.Int32FromInt32(MEM_Zero)) } // C documentation // // /* Return true if a parameter value originated from an sqlite3_bind() */ func Xsqlite3_value_frombind(tls *libc.TLS, pVal uintptr) (r int32) { return libc.BoolInt32(int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fflags)&int32(MEM_FromBind) != 0) } // C documentation // // /* Make a copy of an sqlite3_value object // */ func Xsqlite3_value_dup(tls *libc.TLS, pOrig uintptr) (r uintptr) { var pNew, p1, p2, p3, p4 uintptr _, _, _, _, _ = pNew, p1, p2, p3, p4 if pOrig == uintptr(0) { return uintptr(0) } pNew = Xsqlite3_malloc(tls, int32(40)) if pNew == uintptr(0) { return uintptr(0) } libc.Xmemset(tls, pNew, 0, uint32(40)) libc.Xmemcpy(tls, pNew, pOrig, uint32(libc.UintptrFromInt32(0)+20)) p1 = pNew + 16 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(MEM_Dyn)) (*Tsqlite3_value)(unsafe.Pointer(pNew)).Fdb = uintptr(0) if int32((*Tsqlite3_value)(unsafe.Pointer(pNew)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) != 0 { p2 = pNew + 16 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) & ^(libc.Int32FromInt32(MEM_Static) | libc.Int32FromInt32(MEM_Dyn))) p3 = pNew + 16 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) | libc.Int32FromInt32(MEM_Ephem)) if _sqlite3VdbeMemMakeWriteable(tls, pNew) != SQLITE_OK { _sqlite3ValueFree(tls, pNew) pNew = uintptr(0) } } else { if int32((*Tsqlite3_value)(unsafe.Pointer(pNew)).Fflags)&int32(MEM_Null) != 0 { /* Do not duplicate pointer values */ p4 = pNew + 16 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) & ^(libc.Int32FromInt32(MEM_Term) | libc.Int32FromInt32(MEM_Subtype))) } } return pNew } // C documentation // // /* Destroy an sqlite3_value object previously obtained from // ** sqlite3_value_dup(). // */ func Xsqlite3_value_free(tls *libc.TLS, pOld uintptr) { _sqlite3ValueFree(tls, pOld) } // C documentation // // /**************************** sqlite3_result_ ******************************* // ** The following routines are used by user-defined functions to specify // ** the function result. // ** // ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the // ** result as a string or blob. Appropriate errors are set if the string/blob // ** is too big or if an OOM occurs. // ** // ** The invokeValueDestructor(P,X) routine invokes destructor function X() // ** on value P if P is not going to be used and need to be destroyed. // */ func _setResultStrOrError(tls *libc.TLS, pCtx uintptr, z uintptr, n int32, enc Tu8, xDel uintptr) { var pOut uintptr var rc int32 _, _ = pOut, rc pOut = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut rc = _sqlite3VdbeMemSetStr(tls, pOut, z, int64(n), enc, xDel) if rc != 0 { if rc == int32(SQLITE_TOOBIG) { Xsqlite3_result_error_toobig(tls, pCtx) } else { /* The only errors possible from sqlite3VdbeMemSetStr are ** SQLITE_TOOBIG and SQLITE_NOMEM */ _ = libc.Int32FromInt32(0) Xsqlite3_result_error_nomem(tls, pCtx) } return } _sqlite3VdbeChangeEncoding(tls, pOut, int32((*Tsqlite3_context)(unsafe.Pointer(pCtx)).Fenc)) if _sqlite3VdbeMemTooBig(tls, pOut) != 0 { Xsqlite3_result_error_toobig(tls, pCtx) } } func _invokeValueDestructor(tls *libc.TLS, p uintptr, xDel uintptr, pCtx uintptr) (r int32) { _ = libc.Int32FromInt32(0) if xDel == uintptr(0) { /* noop */ } else { if xDel == uintptr(-libc.Int32FromInt32(1)) { /* noop */ } else { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{xDel})))(tls, p) } } _ = libc.Int32FromInt32(0) Xsqlite3_result_error_toobig(tls, pCtx) return int32(SQLITE_TOOBIG) } func Xsqlite3_result_blob(tls *libc.TLS, pCtx uintptr, z uintptr, n int32, xDel uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _setResultStrOrError(tls, pCtx, z, n, uint8(0), xDel) } func Xsqlite3_result_blob64(tls *libc.TLS, pCtx uintptr, z uintptr, n Tsqlite3_uint64, xDel uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if n > uint64(0x7fffffff) { _invokeValueDestructor(tls, z, xDel, pCtx) } else { _setResultStrOrError(tls, pCtx, z, int32(n), uint8(0), xDel) } } func Xsqlite3_result_double(tls *libc.TLS, pCtx uintptr, rVal float64) { _ = libc.Int32FromInt32(0) _sqlite3VdbeMemSetDouble(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut, rVal) } func Xsqlite3_result_error(tls *libc.TLS, pCtx uintptr, z uintptr, n int32) { _ = libc.Int32FromInt32(0) (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FisError = int32(SQLITE_ERROR) _sqlite3VdbeMemSetStr(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut, z, int64(n), uint8(SQLITE_UTF8), uintptr(-libc.Int32FromInt32(1))) } func Xsqlite3_result_error16(tls *libc.TLS, pCtx uintptr, z uintptr, n int32) { _ = libc.Int32FromInt32(0) (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FisError = int32(SQLITE_ERROR) _sqlite3VdbeMemSetStr(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut, z, int64(n), uint8(SQLITE_UTF16LE), uintptr(-libc.Int32FromInt32(1))) } func Xsqlite3_result_int(tls *libc.TLS, pCtx uintptr, iVal int32) { _ = libc.Int32FromInt32(0) _sqlite3VdbeMemSetInt64(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut, int64(iVal)) } func Xsqlite3_result_int64(tls *libc.TLS, pCtx uintptr, iVal Ti64) { _ = libc.Int32FromInt32(0) _sqlite3VdbeMemSetInt64(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut, iVal) } func Xsqlite3_result_null(tls *libc.TLS, pCtx uintptr) { _ = libc.Int32FromInt32(0) _sqlite3VdbeMemSetNull(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut) } func Xsqlite3_result_pointer(tls *libc.TLS, pCtx uintptr, pPtr uintptr, zPType uintptr, xDestructor uintptr) { var pOut uintptr _ = pOut pOut = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut _ = libc.Int32FromInt32(0) _sqlite3VdbeMemRelease(tls, pOut) (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Null) _sqlite3VdbeMemSetPointer(tls, pOut, pPtr, zPType, xDestructor) } func Xsqlite3_result_subtype(tls *libc.TLS, pCtx uintptr, eSubtype uint32) { var pOut, p1 uintptr _, _ = pOut, p1 pOut = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut _ = libc.Int32FromInt32(0) (*TMem)(unsafe.Pointer(pOut)).FeSubtype = uint8(eSubtype & uint32(0xff)) p1 = pOut + 16 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(MEM_Subtype)) } func Xsqlite3_result_text(tls *libc.TLS, pCtx uintptr, z uintptr, n int32, xDel uintptr) { _ = libc.Int32FromInt32(0) _setResultStrOrError(tls, pCtx, z, n, uint8(SQLITE_UTF8), xDel) } func Xsqlite3_result_text64(tls *libc.TLS, pCtx uintptr, z uintptr, n Tsqlite3_uint64, xDel uintptr, enc uint8) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(enc) != int32(SQLITE_UTF8) { if int32(enc) == int32(SQLITE_UTF16) { enc = uint8(SQLITE_UTF16LE) } n &= ^libc.Uint64FromInt32(1) } if n > uint64(0x7fffffff) { _invokeValueDestructor(tls, z, xDel, pCtx) } else { _setResultStrOrError(tls, pCtx, z, int32(n), enc, xDel) _sqlite3VdbeMemZeroTerminateIfAble(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut) } } func Xsqlite3_result_text16(tls *libc.TLS, pCtx uintptr, z uintptr, n int32, xDel uintptr) { _ = libc.Int32FromInt32(0) _setResultStrOrError(tls, pCtx, z, int32(uint64(n) & ^libc.Uint64FromInt32(1)), uint8(SQLITE_UTF16LE), xDel) } func Xsqlite3_result_text16be(tls *libc.TLS, pCtx uintptr, z uintptr, n int32, xDel uintptr) { _ = libc.Int32FromInt32(0) _setResultStrOrError(tls, pCtx, z, int32(uint64(n) & ^libc.Uint64FromInt32(1)), uint8(SQLITE_UTF16BE), xDel) } func Xsqlite3_result_text16le(tls *libc.TLS, pCtx uintptr, z uintptr, n int32, xDel uintptr) { _ = libc.Int32FromInt32(0) _setResultStrOrError(tls, pCtx, z, int32(uint64(n) & ^libc.Uint64FromInt32(1)), uint8(SQLITE_UTF16LE), xDel) } func Xsqlite3_result_value(tls *libc.TLS, pCtx uintptr, pValue uintptr) { var pOut uintptr _ = pOut pOut = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut _ = libc.Int32FromInt32(0) _sqlite3VdbeMemCopy(tls, pOut, pValue) _sqlite3VdbeChangeEncoding(tls, pOut, int32((*Tsqlite3_context)(unsafe.Pointer(pCtx)).Fenc)) if _sqlite3VdbeMemTooBig(tls, pOut) != 0 { Xsqlite3_result_error_toobig(tls, pCtx) } } func Xsqlite3_result_zeroblob(tls *libc.TLS, pCtx uintptr, n int32) { var v1 int32 _ = v1 if n > 0 { v1 = n } else { v1 = 0 } Xsqlite3_result_zeroblob64(tls, pCtx, uint64(v1)) } func Xsqlite3_result_zeroblob64(tls *libc.TLS, pCtx uintptr, n Tu64) (r int32) { var pOut uintptr _ = pOut pOut = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut _ = libc.Int32FromInt32(0) if n > uint64(*(*int32)(unsafe.Pointer((*TMem)(unsafe.Pointer(pOut)).Fdb + 120))) { Xsqlite3_result_error_toobig(tls, pCtx) return int32(SQLITE_TOOBIG) } _sqlite3VdbeMemSetZeroBlob(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut, int32(n)) return SQLITE_OK } func Xsqlite3_result_error_code(tls *libc.TLS, pCtx uintptr, errCode int32) { var v1 int32 _ = v1 if errCode != 0 { v1 = errCode } else { v1 = -int32(1) } (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FisError = v1 if int32((*TMem)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut)).Fflags)&int32(MEM_Null) != 0 { _setResultStrOrError(tls, pCtx, _sqlite3ErrStr(tls, errCode), -int32(1), uint8(SQLITE_UTF8), libc.UintptrFromInt32(0)) } } // C documentation // // /* Force an SQLITE_TOOBIG error. */ func Xsqlite3_result_error_toobig(tls *libc.TLS, pCtx uintptr) { _ = libc.Int32FromInt32(0) (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FisError = int32(SQLITE_TOOBIG) _sqlite3VdbeMemSetStr(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut, __ccgo_ts+5423, int64(-int32(1)), uint8(SQLITE_UTF8), libc.UintptrFromInt32(0)) } // C documentation // // /* An SQLITE_NOMEM error. */ func Xsqlite3_result_error_nomem(tls *libc.TLS, pCtx uintptr) { _ = libc.Int32FromInt32(0) _sqlite3VdbeMemSetNull(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut) (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FisError = int32(SQLITE_NOMEM) _sqlite3OomFault(tls, (*TMem)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut)).Fdb) } // C documentation // // /* Force the INT64 value currently stored as the result to be // ** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL // ** test-control. // */ func _sqlite3ResultIntReal(tls *libc.TLS, pCtx uintptr) { var p1, p2 uintptr _, _ = p1, p2 _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut)).Fflags)&int32(MEM_Int) != 0 { p1 = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut + 16 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(MEM_Int)) p2 = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut + 16 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(MEM_IntReal)) } } // C documentation // // /* // ** This function is called after a transaction has been committed. It // ** invokes callbacks registered with sqlite3_wal_hook() as required. // */ func _doWalCallbacks(tls *libc.TLS, db uintptr) (r int32) { var i, nEntry, rc int32 var pBt uintptr _, _, _, _ = i, nEntry, pBt, rc rc = SQLITE_OK i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt if pBt != 0 { _sqlite3BtreeEnter(tls, pBt) nEntry = _sqlite3PagerWalCallback(tls, _sqlite3BtreePager(tls, pBt)) _sqlite3BtreeLeave(tls, pBt) if nEntry > 0 && (*Tsqlite3)(unsafe.Pointer(db)).FxWalCallback != 0 && rc == SQLITE_OK { rc = (*(*func(*libc.TLS, uintptr, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxWalCallback})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpWalArg, db, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FzDbSName, nEntry) } } goto _1 _1: ; i++ } return rc } // C documentation // // /* // ** Execute the statement pStmt, either until a row of data is ready, the // ** statement is completely executed or an error occurs. // ** // ** This routine implements the bulk of the logic behind the sqlite_step() // ** API. The only thing omitted is the automatic recompile if a // ** schema change has occurred. That detail is handled by the // ** outer sqlite3_step() wrapper procedure. // */ func _sqlite3Step(tls *libc.TLS, p uintptr) (r int32) { var db uintptr var rc int32 _, _ = db, rc _ = libc.Int32FromInt32(0) db = (*TVdbe)(unsafe.Pointer(p)).Fdb if int32((*TVdbe)(unsafe.Pointer(p)).FeVdbeState) != int32(VDBE_RUN_STATE) { goto restart_step restart_step: ; if int32((*TVdbe)(unsafe.Pointer(p)).FeVdbeState) == int32(VDBE_READY_STATE) { if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x3>>0)) != 0 { (*TVdbe)(unsafe.Pointer(p)).Frc = int32(SQLITE_SCHEMA) rc = int32(SQLITE_ERROR) if int32((*TVdbe)(unsafe.Pointer(p)).FprepFlags)&int32(SQLITE_PREPARE_SAVESQL) != 0 { /* If this statement was prepared using saved SQL and an ** error has occurred, then return the error code in p->rc to the ** caller. Set the error code in the database handle to the same ** value. */ rc = _sqlite3VdbeTransferError(tls, p) } goto end_of_step } /* If there are no other statements currently running, then ** reset the interrupt flag. This prevents a call to sqlite3_interrupt ** from interrupting a statement that has not yet started. */ if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive == 0 { libc.AtomicStoreNInt32(db+312, libc.Int32FromInt32(0), libc.Int32FromInt32(__ATOMIC_RELAXED)) } _ = libc.Int32FromInt32(0) if int32((*Tsqlite3)(unsafe.Pointer(db)).FmTrace)&(libc.Int32FromInt32(SQLITE_TRACE_PROFILE)|libc.Int32FromInt32(SQLITE_TRACE_XPROFILE)) != 0 && !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) && (*TVdbe)(unsafe.Pointer(p)).FzSql != 0 { _sqlite3OsCurrentTimeInt64(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, p+136) } else { _ = libc.Int32FromInt32(0) } (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive++ if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x40>>6)) == 0 { (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite++ } if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x80>>7)) != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeRead++ } (*TVdbe)(unsafe.Pointer(p)).Fpc = 0 (*TVdbe)(unsafe.Pointer(p)).FeVdbeState = uint8(VDBE_RUN_STATE) } else { if int32((*TVdbe)(unsafe.Pointer(p)).FeVdbeState) == int32(VDBE_HALT_STATE) { /* We used to require that sqlite3_reset() be called before retrying ** sqlite3_step() after any error or after SQLITE_DONE. But beginning ** with version 3.7.0, we changed this so that sqlite3_reset() would ** be called automatically instead of throwing the SQLITE_MISUSE error. ** This "automatic-reset" change is not technically an incompatibility, ** since any application that receives an SQLITE_MISUSE is broken by ** definition. ** ** Nevertheless, some published applications that were originally written ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE ** returns, and those were broken by the automatic-reset change. As a ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the ** legacy behavior of returning SQLITE_MISUSE for cases where the ** previous sqlite3_step() returned something other than a SQLITE_LOCKED ** or SQLITE_BUSY error. */ Xsqlite3_reset(tls, p) _ = libc.Int32FromInt32(0) goto restart_step } } } if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2)) != 0 { rc = _sqlite3VdbeList(tls, p) } else { (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec++ rc = _sqlite3VdbeExec(tls, p) (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec-- } if rc == int32(SQLITE_ROW) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).FerrCode = int32(SQLITE_ROW) return int32(SQLITE_ROW) } else { /* If the statement completed successfully, invoke the profile callback */ if (*TVdbe)(unsafe.Pointer(p)).FstartTime > 0 { _invokeProfileCallback(tls, db, p) } (*TVdbe)(unsafe.Pointer(p)).FpResultRow = uintptr(0) if rc == int32(SQLITE_DONE) && (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0 { _ = libc.Int32FromInt32(0) (*TVdbe)(unsafe.Pointer(p)).Frc = _doWalCallbacks(tls, db) if (*TVdbe)(unsafe.Pointer(p)).Frc != SQLITE_OK { rc = int32(SQLITE_ERROR) } } else { if rc != int32(SQLITE_DONE) && int32((*TVdbe)(unsafe.Pointer(p)).FprepFlags)&int32(SQLITE_PREPARE_SAVESQL) != 0 { /* If this statement was prepared using saved SQL and an ** error has occurred, then return the error code in p->rc to the ** caller. Set the error code in the database handle to the same value. */ rc = _sqlite3VdbeTransferError(tls, p) } } } (*Tsqlite3)(unsafe.Pointer(db)).FerrCode = rc if int32(SQLITE_NOMEM) == _sqlite3ApiExit(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, (*TVdbe)(unsafe.Pointer(p)).Frc) { (*TVdbe)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) if int32((*TVdbe)(unsafe.Pointer(p)).FprepFlags)&int32(SQLITE_PREPARE_SAVESQL) != 0 { rc = (*TVdbe)(unsafe.Pointer(p)).Frc } } goto end_of_step end_of_step: ; /* There are only a limited number of result codes allowed from the ** statements prepared using the legacy sqlite3_prepare() interface */ _ = libc.Int32FromInt32(0) return rc & (*Tsqlite3)(unsafe.Pointer(db)).FerrMask } // C documentation // // /* // ** This is the top-level implementation of sqlite3_step(). Call // ** sqlite3Step() to do most of the work. If a schema error occurs, // ** call sqlite3Reprepare() and try again. // */ func Xsqlite3_step(tls *libc.TLS, pStmt uintptr) (r int32) { var cnt, rc, savedPc, v1, v2, v4, v5 int32 var db, v, zErr uintptr var v3 bool _, _, _, _, _, _, _, _, _, _, _ = cnt, db, rc, savedPc, v, zErr, v1, v2, v3, v4, v5 rc = SQLITE_OK /* Result from sqlite3Step() */ v = pStmt /* the prepared statement */ cnt = 0 /* The database connection */ if _vdbeSafetyNotNull(tls, v) != 0 { return _sqlite3MisuseError(tls, int32(90829)) } db = (*TVdbe)(unsafe.Pointer(v)).Fdb Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) for { v1 = _sqlite3Step(tls, v) rc = v1 if v3 = v1 == int32(SQLITE_SCHEMA); v3 { v2 = cnt cnt++ } if !(v3 && v2 < int32(SQLITE_MAX_SCHEMA_RETRY)) { break } savedPc = (*TVdbe)(unsafe.Pointer(v)).Fpc rc = _sqlite3Reprepare(tls, v) if rc != SQLITE_OK { /* This case occurs after failing to recompile an sql statement. ** The error message from the SQL compiler has already been loaded ** into the database handle. This block copies the error message ** from the database handle into the statement and sets the statement ** program counter to 0 to ensure that when the statement is ** finalized or reset the parser error message is available via ** sqlite3_errmsg() and sqlite3_errcode(). */ zErr = Xsqlite3_value_text(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpErr) _sqlite3DbFree(tls, db, (*TVdbe)(unsafe.Pointer(v)).FzErrMsg) if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { (*TVdbe)(unsafe.Pointer(v)).FzErrMsg = _sqlite3DbStrDup(tls, db, zErr) v4 = _sqlite3ApiExit(tls, db, rc) rc = v4 (*TVdbe)(unsafe.Pointer(v)).Frc = v4 } else { (*TVdbe)(unsafe.Pointer(v)).FzErrMsg = uintptr(0) v5 = libc.Int32FromInt32(SQLITE_NOMEM) rc = v5 (*TVdbe)(unsafe.Pointer(v)).Frc = v5 } break } Xsqlite3_reset(tls, pStmt) if savedPc >= 0 { /* Setting minWriteFileFormat to 254 is a signal to the OP_Init and ** OP_Trace opcodes to *not* perform SQLITE_TRACE_STMT because it has ** already been done once on a prior invocation that failed due to ** SQLITE_SCHEMA. tag-20220401a */ (*TVdbe)(unsafe.Pointer(v)).FminWriteFileFormat = uint8(254) } _ = libc.Int32FromInt32(0) } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** Extract the user data from a sqlite3_context structure and return a // ** pointer to it. // */ func Xsqlite3_user_data(tls *libc.TLS, p uintptr) (r uintptr) { _ = libc.Int32FromInt32(0) return (*TFuncDef)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(p)).FpFunc)).FpUserData } // C documentation // // /* // ** Extract the user data from a sqlite3_context structure and return a // ** pointer to it. // ** // ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface // ** returns a copy of the pointer to the database connection (the 1st // ** parameter) of the sqlite3_create_function() and // ** sqlite3_create_function16() routines that originally registered the // ** application defined function. // */ func Xsqlite3_context_db_handle(tls *libc.TLS, p uintptr) (r uintptr) { _ = libc.Int32FromInt32(0) return (*TMem)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(p)).FpOut)).Fdb } // C documentation // // /* // ** If this routine is invoked from within an xColumn method of a virtual // ** table, then it returns true if and only if the the call is during an // ** UPDATE operation and the value of the column will not be modified // ** by the UPDATE. // ** // ** If this routine is called from any context other than within the // ** xColumn method of a virtual table, then the return value is meaningless // ** and arbitrary. // ** // ** Virtual table implements might use this routine to optimize their // ** performance by substituting a NULL result, or some other light-weight // ** value, as a signal to the xUpdate routine that the column is unchanged. // */ func Xsqlite3_vtab_nochange(tls *libc.TLS, p uintptr) (r int32) { _ = libc.Int32FromInt32(0) return Xsqlite3_value_nochange(tls, (*Tsqlite3_context)(unsafe.Pointer(p)).FpOut) } // C documentation // // /* // ** The destructor function for a ValueList object. This needs to be // ** a separate function, unknowable to the application, to ensure that // ** calls to sqlite3_vtab_in_first()/sqlite3_vtab_in_next() that are not // ** preceded by activation of IN processing via sqlite3_vtab_int() do not // ** try to access a fake ValueList object inserted by a hostile extension. // */ func _sqlite3VdbeValueListFree(tls *libc.TLS, pToDelete uintptr) { Xsqlite3_free(tls, pToDelete) } // C documentation // // /* // ** Implementation of sqlite3_vtab_in_first() (if bNext==0) and // ** sqlite3_vtab_in_next() (if bNext!=0). // */ func _valueFromValueList(tls *libc.TLS, pVal uintptr, ppOut uintptr, bNext int32) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var iOff, rc, v1 int32 var pOut, pRhs, zBuf uintptr var sz Tu32 var _ /* dummy at bp+0 */ int32 var _ /* iSerial at bp+48 */ Tu32 var _ /* sMem at bp+8 */ TMem _, _, _, _, _, _, _ = iOff, pOut, pRhs, rc, sz, zBuf, v1 *(*uintptr)(unsafe.Pointer(ppOut)) = uintptr(0) if pVal == uintptr(0) { return _sqlite3MisuseError(tls, int32(90950)) } if int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fflags)&int32(MEM_Dyn) == 0 || (*Tsqlite3_value)(unsafe.Pointer(pVal)).FxDel != __ccgo_fp(_sqlite3VdbeValueListFree) { return int32(SQLITE_ERROR) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pRhs = (*Tsqlite3_value)(unsafe.Pointer(pVal)).Fz } if bNext != 0 { rc = _sqlite3BtreeNext(tls, (*TValueList)(unsafe.Pointer(pRhs)).FpCsr, 0) } else { *(*int32)(unsafe.Pointer(bp)) = 0 rc = _sqlite3BtreeFirst(tls, (*TValueList)(unsafe.Pointer(pRhs)).FpCsr, bp) _ = libc.Int32FromInt32(0) if _sqlite3BtreeEof(tls, (*TValueList)(unsafe.Pointer(pRhs)).FpCsr) != 0 { rc = int32(SQLITE_DONE) } } if rc == SQLITE_OK { /* Raw content of current row */ libc.Xmemset(tls, bp+8, 0, uint32(40)) sz = _sqlite3BtreePayloadSize(tls, (*TValueList)(unsafe.Pointer(pRhs)).FpCsr) rc = _sqlite3VdbeMemFromBtreeZeroOffset(tls, (*TValueList)(unsafe.Pointer(pRhs)).FpCsr, uint32(int32(sz)), bp+8) if rc == SQLITE_OK { zBuf = (*(*TMem)(unsafe.Pointer(bp + 8))).Fz pOut = (*TValueList)(unsafe.Pointer(pRhs)).FpOut if int32(*(*Tu8)(unsafe.Pointer(zBuf + 1))) < int32(libc.Uint8FromInt32(0x80)) { *(*Tu32)(unsafe.Pointer(bp + 48)) = uint32(*(*Tu8)(unsafe.Pointer(zBuf + 1))) v1 = libc.Int32FromInt32(1) } else { v1 = int32(_sqlite3GetVarint32(tls, zBuf+1, bp+48)) } iOff = int32(1) + int32(uint8(v1)) _sqlite3VdbeSerialGet(tls, zBuf+uintptr(iOff), *(*Tu32)(unsafe.Pointer(bp + 48)), pOut) (*Tsqlite3_value)(unsafe.Pointer(pOut)).Fenc = (*Tsqlite3)(unsafe.Pointer((*Tsqlite3_value)(unsafe.Pointer(pOut)).Fdb)).Fenc if int32((*Tsqlite3_value)(unsafe.Pointer(pOut)).Fflags)&int32(MEM_Ephem) != 0 && _sqlite3VdbeMemMakeWriteable(tls, pOut) != 0 { rc = int32(SQLITE_NOMEM) } else { *(*uintptr)(unsafe.Pointer(ppOut)) = pOut } } _sqlite3VdbeMemRelease(tls, bp+8) } return rc } // C documentation // // /* // ** Set the iterator value pVal to point to the first value in the set. // ** Set (*ppOut) to point to this value before returning. // */ func Xsqlite3_vtab_in_first(tls *libc.TLS, pVal uintptr, ppOut uintptr) (r int32) { return _valueFromValueList(tls, pVal, ppOut, 0) } // C documentation // // /* // ** Set the iterator value pVal to point to the next value in the set. // ** Set (*ppOut) to point to this value before returning. // */ func Xsqlite3_vtab_in_next(tls *libc.TLS, pVal uintptr, ppOut uintptr) (r int32) { return _valueFromValueList(tls, pVal, ppOut, int32(1)) } // C documentation // // /* // ** Return the current time for a statement. If the current time // ** is requested more than once within the same run of a single prepared // ** statement, the exact same time is returned for each invocation regardless // ** of the amount of time that elapses between invocations. In other words, // ** the time returned is always the time of the first call. // */ func _sqlite3StmtCurrentTime(tls *libc.TLS, p uintptr) (r Tsqlite3_int64) { bp := tls.Alloc(16) defer tls.Free(16) var piTime, v1 uintptr var rc int32 var _ /* iTime at bp+0 */ Tsqlite3_int64 _, _, _ = piTime, rc, v1 *(*Tsqlite3_int64)(unsafe.Pointer(bp)) = 0 if (*Tsqlite3_context)(unsafe.Pointer(p)).FpVdbe != uintptr(0) { v1 = (*Tsqlite3_context)(unsafe.Pointer(p)).FpVdbe + 56 } else { v1 = bp } piTime = v1 if *(*Tsqlite3_int64)(unsafe.Pointer(piTime)) == 0 { rc = _sqlite3OsCurrentTimeInt64(tls, (*Tsqlite3)(unsafe.Pointer((*TMem)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(p)).FpOut)).Fdb)).FpVfs, piTime) if rc != 0 { *(*Tsqlite3_int64)(unsafe.Pointer(piTime)) = 0 } } return *(*Tsqlite3_int64)(unsafe.Pointer(piTime)) } // C documentation // // /* // ** Create a new aggregate context for p and return a pointer to // ** its pMem->z element. // */ func _createAggContext(tls *libc.TLS, p uintptr, nByte int32) (r uintptr) { var pMem uintptr _ = pMem pMem = (*Tsqlite3_context)(unsafe.Pointer(p)).FpMem _ = libc.Int32FromInt32(0) if nByte <= 0 { _sqlite3VdbeMemSetNull(tls, pMem) (*TMem)(unsafe.Pointer(pMem)).Fz = uintptr(0) } else { _sqlite3VdbeMemClearAndResize(tls, pMem, nByte) (*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(MEM_Agg) *(*uintptr)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pMem)).Fu)) = (*Tsqlite3_context)(unsafe.Pointer(p)).FpFunc if (*TMem)(unsafe.Pointer(pMem)).Fz != 0 { libc.Xmemset(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, 0, uint32(nByte)) } } return (*TMem)(unsafe.Pointer(pMem)).Fz } // C documentation // // /* // ** Allocate or return the aggregate context for a user function. A new // ** context is allocated on the first call. Subsequent calls return the // ** same context that was returned on prior calls. // */ func Xsqlite3_aggregate_context(tls *libc.TLS, p uintptr, nByte int32) (r uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(p)).FpMem)).Fflags)&int32(MEM_Agg) == 0 { return _createAggContext(tls, p, nByte) } else { return (*TMem)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(p)).FpMem)).Fz } return r } // C documentation // // /* // ** Return the auxiliary data pointer, if any, for the iArg'th argument to // ** the user-function defined by pCtx. // ** // ** The left-most argument is 0. // ** // ** Undocumented behavior: If iArg is negative then access a cache of // ** auxiliary data pointers that is available to all functions within a // ** single prepared statement. The iArg values must match. // */ func Xsqlite3_get_auxdata(tls *libc.TLS, pCtx uintptr, iArg int32) (r uintptr) { var pAuxData uintptr _ = pAuxData _ = libc.Int32FromInt32(0) if (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpVdbe == uintptr(0) { return uintptr(0) } pAuxData = (*TVdbe)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpVdbe)).FpAuxData for { if !(pAuxData != 0) { break } if (*TAuxData)(unsafe.Pointer(pAuxData)).FiAuxArg == iArg && ((*TAuxData)(unsafe.Pointer(pAuxData)).FiAuxOp == (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FiOp || iArg < 0) { return (*TAuxData)(unsafe.Pointer(pAuxData)).FpAux } goto _1 _1: ; pAuxData = (*TAuxData)(unsafe.Pointer(pAuxData)).FpNextAux } return uintptr(0) } // C documentation // // /* // ** Set the auxiliary data pointer and delete function, for the iArg'th // ** argument to the user-function defined by pCtx. Any previous value is // ** deleted by calling the delete function specified when it was set. // ** // ** The left-most argument is 0. // ** // ** Undocumented behavior: If iArg is negative then make the data available // ** to all functions within the current prepared statement using iArg as an // ** access code. // */ func Xsqlite3_set_auxdata(tls *libc.TLS, pCtx uintptr, iArg int32, pAux uintptr, xDelete uintptr) { var pAuxData, pVdbe uintptr _, _ = pAuxData, pVdbe pVdbe = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpVdbe _ = libc.Int32FromInt32(0) if pVdbe == uintptr(0) { goto failed } pAuxData = (*TVdbe)(unsafe.Pointer(pVdbe)).FpAuxData for { if !(pAuxData != 0) { break } if (*TAuxData)(unsafe.Pointer(pAuxData)).FiAuxArg == iArg && ((*TAuxData)(unsafe.Pointer(pAuxData)).FiAuxOp == (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FiOp || iArg < 0) { break } goto _1 _1: ; pAuxData = (*TAuxData)(unsafe.Pointer(pAuxData)).FpNextAux } if pAuxData == uintptr(0) { pAuxData = _sqlite3DbMallocZero(tls, (*TVdbe)(unsafe.Pointer(pVdbe)).Fdb, uint64(20)) if !(pAuxData != 0) { goto failed } (*TAuxData)(unsafe.Pointer(pAuxData)).FiAuxOp = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FiOp (*TAuxData)(unsafe.Pointer(pAuxData)).FiAuxArg = iArg (*TAuxData)(unsafe.Pointer(pAuxData)).FpNextAux = (*TVdbe)(unsafe.Pointer(pVdbe)).FpAuxData (*TVdbe)(unsafe.Pointer(pVdbe)).FpAuxData = pAuxData if (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FisError == 0 { (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FisError = -int32(1) } } else { if (*TAuxData)(unsafe.Pointer(pAuxData)).FxDeleteAux != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TAuxData)(unsafe.Pointer(pAuxData)).FxDeleteAux})))(tls, (*TAuxData)(unsafe.Pointer(pAuxData)).FpAux) } } (*TAuxData)(unsafe.Pointer(pAuxData)).FpAux = pAux (*TAuxData)(unsafe.Pointer(pAuxData)).FxDeleteAux = xDelete return goto failed failed: ; if xDelete != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{xDelete})))(tls, pAux) } } // C documentation // // /* // ** Return the number of times the Step function of an aggregate has been // ** called. // ** // ** This function is deprecated. Do not use it for new code. It is // ** provide only to avoid breaking legacy code. New aggregate function // ** implementations should keep their own counts within their aggregate // ** context. // */ func Xsqlite3_aggregate_count(tls *libc.TLS, p uintptr) (r int32) { _ = libc.Int32FromInt32(0) return (*TMem)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(p)).FpMem)).Fn } // C documentation // // /* // ** Return the number of columns in the result set for the statement pStmt. // */ func Xsqlite3_column_count(tls *libc.TLS, pStmt uintptr) (r int32) { var pVm uintptr _ = pVm pVm = pStmt if pVm == uintptr(0) { return 0 } return int32((*TVdbe)(unsafe.Pointer(pVm)).FnResColumn) } // C documentation // // /* // ** Return the number of values available from the current row of the // ** currently executing statement pStmt. // */ func Xsqlite3_data_count(tls *libc.TLS, pStmt uintptr) (r int32) { var pVm uintptr _ = pVm pVm = pStmt if pVm == uintptr(0) || (*TVdbe)(unsafe.Pointer(pVm)).FpResultRow == uintptr(0) { return 0 } return int32((*TVdbe)(unsafe.Pointer(pVm)).FnResColumn) } // C documentation // // /* // ** Return a pointer to static memory containing an SQL NULL value. // */ func _columnNullValue(tls *libc.TLS) (r uintptr) { return uintptr(unsafe.Pointer(&_nullMem)) } /* Even though the Mem structure contains an element ** of type i64, on certain architectures (x86) with certain compiler ** switches (-Os), gcc may align this Mem object on a 4-byte boundary ** instead of an 8-byte one. This all works fine, except that when ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s ** that a Mem structure is located on an 8-byte boundary. To prevent ** these assert()s from failing, when building with SQLITE_DEBUG defined ** using gcc, we force nullMem to be 8-byte aligned using the magical ** __attribute__((aligned(8))) macro. */ var _nullMem = TMem{ Fflags: libc.Uint16FromInt32(MEM_Null), } // C documentation // // /* // ** Check to see if column iCol of the given statement is valid. If // ** it is, return a pointer to the Mem for the value of that column. // ** If iCol is not valid, return a pointer to a Mem which has a value // ** of NULL. // */ func _columnMem(tls *libc.TLS, pStmt uintptr, i int32) (r uintptr) { var pOut, pVm uintptr _, _ = pOut, pVm pVm = pStmt if pVm == uintptr(0) { return _columnNullValue(tls) } _ = libc.Int32FromInt32(0) Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(pVm)).Fdb)).Fmutex) if (*TVdbe)(unsafe.Pointer(pVm)).FpResultRow != uintptr(0) && i < int32((*TVdbe)(unsafe.Pointer(pVm)).FnResColumn) && i >= 0 { pOut = (*TVdbe)(unsafe.Pointer(pVm)).FpResultRow + uintptr(i)*40 } else { _sqlite3Error(tls, (*TVdbe)(unsafe.Pointer(pVm)).Fdb, int32(SQLITE_RANGE)) pOut = _columnNullValue(tls) } return pOut } // C documentation // // /* // ** This function is called after invoking an sqlite3_value_XXX function on a // ** column value (i.e. a value returned by evaluating an SQL expression in the // ** select list of a SELECT statement) that may cause a malloc() failure. If // ** malloc() has failed, the threads mallocFailed flag is cleared and the result // ** code of statement pStmt set to SQLITE_NOMEM. // ** // ** Specifically, this is called from within: // ** // ** sqlite3_column_int() // ** sqlite3_column_int64() // ** sqlite3_column_text() // ** sqlite3_column_text16() // ** sqlite3_column_real() // ** sqlite3_column_bytes() // ** sqlite3_column_bytes16() // ** sqlite3_column_blob() // */ func _columnMallocFailure(tls *libc.TLS, pStmt uintptr) { var p uintptr _ = p /* If malloc() failed during an encoding conversion within an ** sqlite3_column_XXX API, then set the return code of the statement to ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR ** and _finalize() will return NOMEM. */ p = pStmt if p != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TVdbe)(unsafe.Pointer(p)).Frc = _sqlite3ApiExit(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, (*TVdbe)(unsafe.Pointer(p)).Frc) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) } } // C documentation // // /**************************** sqlite3_column_ ******************************* // ** The following routines are used to access elements of the current row // ** in the result set. // */ func Xsqlite3_column_blob(tls *libc.TLS, pStmt uintptr, i int32) (r uintptr) { var val uintptr _ = val val = Xsqlite3_value_blob(tls, _columnMem(tls, pStmt, i)) /* Even though there is no encoding conversion, value_blob() might ** need to call malloc() to expand the result of a zeroblob() ** expression. */ _columnMallocFailure(tls, pStmt) return val } func Xsqlite3_column_bytes(tls *libc.TLS, pStmt uintptr, i int32) (r int32) { var val int32 _ = val val = Xsqlite3_value_bytes(tls, _columnMem(tls, pStmt, i)) _columnMallocFailure(tls, pStmt) return val } func Xsqlite3_column_bytes16(tls *libc.TLS, pStmt uintptr, i int32) (r int32) { var val int32 _ = val val = Xsqlite3_value_bytes16(tls, _columnMem(tls, pStmt, i)) _columnMallocFailure(tls, pStmt) return val } func Xsqlite3_column_double(tls *libc.TLS, pStmt uintptr, i int32) (r float64) { var val float64 _ = val val = Xsqlite3_value_double(tls, _columnMem(tls, pStmt, i)) _columnMallocFailure(tls, pStmt) return val } func Xsqlite3_column_int(tls *libc.TLS, pStmt uintptr, i int32) (r int32) { var val int32 _ = val val = Xsqlite3_value_int(tls, _columnMem(tls, pStmt, i)) _columnMallocFailure(tls, pStmt) return val } func Xsqlite3_column_int64(tls *libc.TLS, pStmt uintptr, i int32) (r Tsqlite_int64) { var val Tsqlite_int64 _ = val val = Xsqlite3_value_int64(tls, _columnMem(tls, pStmt, i)) _columnMallocFailure(tls, pStmt) return val } func Xsqlite3_column_text(tls *libc.TLS, pStmt uintptr, i int32) (r uintptr) { var val uintptr _ = val val = Xsqlite3_value_text(tls, _columnMem(tls, pStmt, i)) _columnMallocFailure(tls, pStmt) return val } func Xsqlite3_column_value(tls *libc.TLS, pStmt uintptr, i int32) (r uintptr) { var pOut, p1, p2 uintptr _, _, _ = pOut, p1, p2 pOut = _columnMem(tls, pStmt, i) if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&int32(MEM_Static) != 0 { p1 = pOut + 16 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(MEM_Static)) p2 = pOut + 16 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(MEM_Ephem)) } _columnMallocFailure(tls, pStmt) return pOut } func Xsqlite3_column_text16(tls *libc.TLS, pStmt uintptr, i int32) (r uintptr) { var val uintptr _ = val val = Xsqlite3_value_text16(tls, _columnMem(tls, pStmt, i)) _columnMallocFailure(tls, pStmt) return val } func Xsqlite3_column_type(tls *libc.TLS, pStmt uintptr, i int32) (r int32) { var iType int32 _ = iType iType = Xsqlite3_value_type(tls, _columnMem(tls, pStmt, i)) _columnMallocFailure(tls, pStmt) return iType } // C documentation // // /* // ** Column names appropriate for EXPLAIN or EXPLAIN QUERY PLAN. // */ var _azExplainColNames8 = [12]uintptr{ 0: __ccgo_ts + 5446, 1: __ccgo_ts + 5451, 2: __ccgo_ts + 5458, 3: __ccgo_ts + 5461, 4: __ccgo_ts + 5464, 5: __ccgo_ts + 5467, 6: __ccgo_ts + 5470, 7: __ccgo_ts + 5473, 8: __ccgo_ts + 5481, 9: __ccgo_ts + 5484, 10: __ccgo_ts + 5491, 11: __ccgo_ts + 5499, } var _azExplainColNames16data = [60]Tu16{ 0: uint16('a'), 1: uint16('d'), 2: uint16('d'), 3: uint16('r'), 5: uint16('o'), 6: uint16('p'), 7: uint16('c'), 8: uint16('o'), 9: uint16('d'), 10: uint16('e'), 12: uint16('p'), 13: uint16('1'), 15: uint16('p'), 16: uint16('2'), 18: uint16('p'), 19: uint16('3'), 21: uint16('p'), 22: uint16('4'), 24: uint16('p'), 25: uint16('5'), 27: uint16('c'), 28: uint16('o'), 29: uint16('m'), 30: uint16('m'), 31: uint16('e'), 32: uint16('n'), 33: uint16('t'), 35: uint16('i'), 36: uint16('d'), 38: uint16('p'), 39: uint16('a'), 40: uint16('r'), 41: uint16('e'), 42: uint16('n'), 43: uint16('t'), 45: uint16('n'), 46: uint16('o'), 47: uint16('t'), 48: uint16('u'), 49: uint16('s'), 50: uint16('e'), 51: uint16('d'), 53: uint16('d'), 54: uint16('e'), 55: uint16('t'), 56: uint16('a'), 57: uint16('i'), 58: uint16('l'), } var _iExplainColNames16 = [12]Tu8{ 1: uint8(5), 2: uint8(12), 3: uint8(15), 4: uint8(18), 5: uint8(21), 6: uint8(24), 7: uint8(27), 8: uint8(35), 9: uint8(38), 10: uint8(45), 11: uint8(53), } // C documentation // // /* // ** Convert the N-th element of pStmt->pColName[] into a string using // ** xFunc() then return that string. If N is out of range, return 0. // ** // ** There are up to 5 names for each column. useType determines which // ** name is returned. Here are the names: // ** // ** 0 The column name as it should be displayed for output // ** 1 The datatype name for the column // ** 2 The name of the database that the column derives from // ** 3 The name of the table that the column derives from // ** 4 The name of the table column that the result column derives from // ** // ** If the result is not a simple column reference (if it is an expression // ** or a constant) then useTypes 2, 3, and 4 return NULL. // */ func _columnName(tls *libc.TLS, pStmt uintptr, N int32, useUtf16 int32, useType int32) (r uintptr) { var db, p, ret uintptr var i, n, v1 int32 var prior_mallocFailed Tu8 _, _, _, _, _, _, _ = db, i, n, p, prior_mallocFailed, ret, v1 if N < 0 { return uintptr(0) } ret = uintptr(0) p = pStmt db = (*TVdbe)(unsafe.Pointer(p)).Fdb _ = libc.Int32FromInt32(0) Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2)) != 0 { if useType > 0 { goto columnName_end } if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2)) == int32(1) { v1 = int32(8) } else { v1 = int32(4) } n = v1 if N >= n { goto columnName_end } if useUtf16 != 0 { i = int32(_iExplainColNames16[N+int32(8)*int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2))-int32(8)]) ret = uintptr(unsafe.Pointer(&_azExplainColNames16data)) + uintptr(i)*2 } else { ret = _azExplainColNames8[N+int32(8)*int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2))-int32(8)] } goto columnName_end } n = int32((*TVdbe)(unsafe.Pointer(p)).FnResColumn) if N < n { prior_mallocFailed = (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed N += useType * n if useUtf16 != 0 { ret = Xsqlite3_value_text16(tls, (*TVdbe)(unsafe.Pointer(p)).FaColName+uintptr(N)*40) } else { ret = Xsqlite3_value_text(tls, (*TVdbe)(unsafe.Pointer(p)).FaColName+uintptr(N)*40) } /* A malloc may have failed inside of the _text() call. If this ** is the case, clear the mallocFailed flag and return NULL. */ _ = libc.Int32FromInt32(0) if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) > int32(prior_mallocFailed) { _sqlite3OomClear(tls, db) ret = uintptr(0) } } goto columnName_end columnName_end: ; Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return ret } // C documentation // // /* // ** Return the name of the Nth column of the result set returned by SQL // ** statement pStmt. // */ func Xsqlite3_column_name(tls *libc.TLS, pStmt uintptr, N int32) (r uintptr) { return _columnName(tls, pStmt, N, 0, COLNAME_NAME) } func Xsqlite3_column_name16(tls *libc.TLS, pStmt uintptr, N int32) (r uintptr) { return _columnName(tls, pStmt, N, int32(1), COLNAME_NAME) } /* ** Constraint: If you have ENABLE_COLUMN_METADATA then you must ** not define OMIT_DECLTYPE. */ // C documentation // // /* // ** Return the column declaration type (if applicable) of the 'i'th column // ** of the result set of SQL statement pStmt. // */ func Xsqlite3_column_decltype(tls *libc.TLS, pStmt uintptr, N int32) (r uintptr) { return _columnName(tls, pStmt, N, 0, int32(COLNAME_DECLTYPE)) } func Xsqlite3_column_decltype16(tls *libc.TLS, pStmt uintptr, N int32) (r uintptr) { return _columnName(tls, pStmt, N, int32(1), int32(COLNAME_DECLTYPE)) } // C documentation // // /* // ** Return the name of the database from which a result column derives. // ** NULL is returned if the result column is an expression or constant or // ** anything else which is not an unambiguous reference to a database column. // */ func Xsqlite3_column_database_name(tls *libc.TLS, pStmt uintptr, N int32) (r uintptr) { return _columnName(tls, pStmt, N, 0, int32(COLNAME_DATABASE)) } func Xsqlite3_column_database_name16(tls *libc.TLS, pStmt uintptr, N int32) (r uintptr) { return _columnName(tls, pStmt, N, int32(1), int32(COLNAME_DATABASE)) } // C documentation // // /* // ** Return the name of the table from which a result column derives. // ** NULL is returned if the result column is an expression or constant or // ** anything else which is not an unambiguous reference to a database column. // */ func Xsqlite3_column_table_name(tls *libc.TLS, pStmt uintptr, N int32) (r uintptr) { return _columnName(tls, pStmt, N, 0, int32(COLNAME_TABLE)) } func Xsqlite3_column_table_name16(tls *libc.TLS, pStmt uintptr, N int32) (r uintptr) { return _columnName(tls, pStmt, N, int32(1), int32(COLNAME_TABLE)) } // C documentation // // /* // ** Return the name of the table column from which a result column derives. // ** NULL is returned if the result column is an expression or constant or // ** anything else which is not an unambiguous reference to a database column. // */ func Xsqlite3_column_origin_name(tls *libc.TLS, pStmt uintptr, N int32) (r uintptr) { return _columnName(tls, pStmt, N, 0, int32(COLNAME_COLUMN)) } func Xsqlite3_column_origin_name16(tls *libc.TLS, pStmt uintptr, N int32) (r uintptr) { return _columnName(tls, pStmt, N, int32(1), int32(COLNAME_COLUMN)) } // C documentation // // /******************************* sqlite3_bind_ *************************** // ** // ** Routines used to attach values to wildcards in a compiled SQL statement. // */ // /* // ** Unbind the value bound to variable i in virtual machine p. This is the // ** the same as binding a NULL value to the column. If the "i" parameter is // ** out of range, then SQLITE_RANGE is returned. Otherwise SQLITE_OK. // ** // ** A successful evaluation of this routine acquires the mutex on p. // ** the mutex is released if any kind of error occurs. // ** // ** The error code stored in database p->db is overwritten with the return // ** value in any case. // */ func _vdbeUnbind(tls *libc.TLS, p uintptr, i uint32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pVar uintptr var v1 uint32 var v2 bool _, _, _ = pVar, v1, v2 if _vdbeSafetyNotNull(tls, p) != 0 { return _sqlite3MisuseError(tls, int32(91554)) } Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) if int32((*TVdbe)(unsafe.Pointer(p)).FeVdbeState) != int32(VDBE_READY_STATE) { _sqlite3Error(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, _sqlite3MisuseError(tls, int32(91558))) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) Xsqlite3_log(tls, int32(SQLITE_MISUSE), __ccgo_ts+5506, libc.VaList(bp+8, (*TVdbe)(unsafe.Pointer(p)).FzSql)) return _sqlite3MisuseError(tls, int32(91562)) } if i >= uint32((*TVdbe)(unsafe.Pointer(p)).FnVar) { _sqlite3Error(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, int32(SQLITE_RANGE)) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) return int32(SQLITE_RANGE) } pVar = (*TVdbe)(unsafe.Pointer(p)).FaVar + uintptr(i)*40 _sqlite3VdbeMemRelease(tls, pVar) (*TMem)(unsafe.Pointer(pVar)).Fflags = uint16(MEM_Null) (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FerrCode = SQLITE_OK /* If the bit corresponding to this variable in Vdbe.expmask is set, then ** binding a new value to this variable invalidates the current query plan. ** ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host ** parameter in the WHERE clause might influence the choice of query plan ** for a statement, then the statement will be automatically recompiled, ** as if there had been a schema change, on the first sqlite3_step() call ** following any change to the bindings of that parameter. */ _ = libc.Int32FromInt32(0) if v2 = (*TVdbe)(unsafe.Pointer(p)).Fexpmask != uint32(0); v2 { if i >= uint32(31) { v1 = uint32(0x80000000) } else { v1 = libc.Uint32FromInt32(1) << i } } if v2 && (*TVdbe)(unsafe.Pointer(p)).Fexpmask&v1 != uint32(0) { libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(1), 0, 0x3) } return SQLITE_OK } // C documentation // // /* // ** Bind a text or BLOB value. // */ func _bindText(tls *libc.TLS, pStmt uintptr, i int32, zData uintptr, nData Ti64, xDel uintptr, encoding Tu8) (r int32) { var p, pVar uintptr var rc int32 _, _, _ = p, pVar, rc p = pStmt rc = _vdbeUnbind(tls, p, uint32(i-libc.Int32FromInt32(1))) if rc == SQLITE_OK { if zData != uintptr(0) { pVar = (*TVdbe)(unsafe.Pointer(p)).FaVar + uintptr(i-int32(1))*40 rc = _sqlite3VdbeMemSetStr(tls, pVar, zData, nData, encoding, xDel) if rc == SQLITE_OK && int32(encoding) != 0 { rc = _sqlite3VdbeChangeEncoding(tls, pVar, int32((*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fenc)) } if rc != 0 { _sqlite3Error(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, rc) rc = _sqlite3ApiExit(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, rc) } } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) } else { if xDel != libc.UintptrFromInt32(0) && xDel != uintptr(-libc.Int32FromInt32(1)) { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{xDel})))(tls, zData) } } return rc } // C documentation // // /* // ** Bind a blob value to an SQL statement variable. // */ func Xsqlite3_bind_blob(tls *libc.TLS, pStmt uintptr, i int32, zData uintptr, nData int32, xDel uintptr) (r int32) { return _bindText(tls, pStmt, i, zData, int64(nData), xDel, uint8(0)) } func Xsqlite3_bind_blob64(tls *libc.TLS, pStmt uintptr, i int32, zData uintptr, nData Tsqlite3_uint64, xDel uintptr) (r int32) { _ = libc.Int32FromInt32(0) return _bindText(tls, pStmt, i, zData, int64(nData), xDel, uint8(0)) } func Xsqlite3_bind_double(tls *libc.TLS, pStmt uintptr, i int32, rValue float64) (r int32) { var p uintptr var rc int32 _, _ = p, rc p = pStmt rc = _vdbeUnbind(tls, p, uint32(i-libc.Int32FromInt32(1))) if rc == SQLITE_OK { _sqlite3VdbeMemSetDouble(tls, (*TVdbe)(unsafe.Pointer(p)).FaVar+uintptr(i-int32(1))*40, rValue) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) } return rc } func Xsqlite3_bind_int(tls *libc.TLS, p uintptr, i int32, iValue int32) (r int32) { return Xsqlite3_bind_int64(tls, p, i, int64(iValue)) } func Xsqlite3_bind_int64(tls *libc.TLS, pStmt uintptr, i int32, iValue Tsqlite_int64) (r int32) { var p uintptr var rc int32 _, _ = p, rc p = pStmt rc = _vdbeUnbind(tls, p, uint32(i-libc.Int32FromInt32(1))) if rc == SQLITE_OK { _sqlite3VdbeMemSetInt64(tls, (*TVdbe)(unsafe.Pointer(p)).FaVar+uintptr(i-int32(1))*40, iValue) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) } return rc } func Xsqlite3_bind_null(tls *libc.TLS, pStmt uintptr, i int32) (r int32) { var p uintptr var rc int32 _, _ = p, rc p = pStmt rc = _vdbeUnbind(tls, p, uint32(i-libc.Int32FromInt32(1))) if rc == SQLITE_OK { Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) } return rc } func Xsqlite3_bind_pointer(tls *libc.TLS, pStmt uintptr, i int32, pPtr uintptr, zPTtype uintptr, xDestructor uintptr) (r int32) { var p uintptr var rc int32 _, _ = p, rc p = pStmt rc = _vdbeUnbind(tls, p, uint32(i-libc.Int32FromInt32(1))) if rc == SQLITE_OK { _sqlite3VdbeMemSetPointer(tls, (*TVdbe)(unsafe.Pointer(p)).FaVar+uintptr(i-int32(1))*40, pPtr, zPTtype, xDestructor) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) } else { if xDestructor != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{xDestructor})))(tls, pPtr) } } return rc } func Xsqlite3_bind_text(tls *libc.TLS, pStmt uintptr, i int32, zData uintptr, nData int32, xDel uintptr) (r int32) { return _bindText(tls, pStmt, i, zData, int64(nData), xDel, uint8(SQLITE_UTF8)) } func Xsqlite3_bind_text64(tls *libc.TLS, pStmt uintptr, i int32, zData uintptr, nData Tsqlite3_uint64, xDel uintptr, enc uint8) (r int32) { _ = libc.Int32FromInt32(0) if int32(enc) != int32(SQLITE_UTF8) { if int32(enc) == int32(SQLITE_UTF16) { enc = uint8(SQLITE_UTF16LE) } nData &= uint64(^int32(libc.Uint16FromInt32(1))) } return _bindText(tls, pStmt, i, zData, int64(nData), xDel, enc) } func Xsqlite3_bind_text16(tls *libc.TLS, pStmt uintptr, i int32, zData uintptr, n int32, xDel uintptr) (r int32) { return _bindText(tls, pStmt, i, zData, int64(uint64(n) & ^libc.Uint64FromInt32(1)), xDel, uint8(SQLITE_UTF16LE)) } func Xsqlite3_bind_value(tls *libc.TLS, pStmt uintptr, i int32, pValue uintptr) (r int32) { var rc int32 var v1 float64 _, _ = rc, v1 switch Xsqlite3_value_type(tls, pValue) { case int32(SQLITE_INTEGER): rc = Xsqlite3_bind_int64(tls, pStmt, i, *(*Ti64)(unsafe.Pointer(pValue))) case int32(SQLITE_FLOAT): _ = libc.Int32FromInt32(0) if int32((*Tsqlite3_value)(unsafe.Pointer(pValue)).Fflags)&int32(MEM_Real) != 0 { v1 = *(*float64)(unsafe.Pointer(pValue)) } else { v1 = float64(*(*Ti64)(unsafe.Pointer(pValue))) } rc = Xsqlite3_bind_double(tls, pStmt, i, v1) case int32(SQLITE_BLOB): if int32((*Tsqlite3_value)(unsafe.Pointer(pValue)).Fflags)&int32(MEM_Zero) != 0 { rc = Xsqlite3_bind_zeroblob(tls, pStmt, i, *(*int32)(unsafe.Pointer(&(*Tsqlite3_value)(unsafe.Pointer(pValue)).Fu))) } else { rc = Xsqlite3_bind_blob(tls, pStmt, i, (*Tsqlite3_value)(unsafe.Pointer(pValue)).Fz, (*Tsqlite3_value)(unsafe.Pointer(pValue)).Fn, uintptr(-libc.Int32FromInt32(1))) } case int32(SQLITE_TEXT): rc = _bindText(tls, pStmt, i, (*Tsqlite3_value)(unsafe.Pointer(pValue)).Fz, int64((*Tsqlite3_value)(unsafe.Pointer(pValue)).Fn), uintptr(-libc.Int32FromInt32(1)), (*Tsqlite3_value)(unsafe.Pointer(pValue)).Fenc) default: rc = Xsqlite3_bind_null(tls, pStmt, i) break } return rc } func Xsqlite3_bind_zeroblob(tls *libc.TLS, pStmt uintptr, i int32, n int32) (r int32) { var p uintptr var rc int32 _, _ = p, rc p = pStmt rc = _vdbeUnbind(tls, p, uint32(i-libc.Int32FromInt32(1))) if rc == SQLITE_OK { _sqlite3VdbeMemSetZeroBlob(tls, (*TVdbe)(unsafe.Pointer(p)).FaVar+uintptr(i-int32(1))*40, n) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) } return rc } func Xsqlite3_bind_zeroblob64(tls *libc.TLS, pStmt uintptr, i int32, n Tsqlite3_uint64) (r int32) { var p uintptr var rc int32 _, _ = p, rc p = pStmt Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) if n > uint64(*(*int32)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb + 120))) { rc = int32(SQLITE_TOOBIG) } else { _ = libc.Int32FromInt32(0) rc = Xsqlite3_bind_zeroblob(tls, pStmt, i, int32(n)) } rc = _sqlite3ApiExit(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, rc) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) return rc } // C documentation // // /* // ** Return the number of wildcards that can be potentially bound to. // ** This routine is added to support DBD::SQLite. // */ func Xsqlite3_bind_parameter_count(tls *libc.TLS, pStmt uintptr) (r int32) { var p uintptr var v1 int32 _, _ = p, v1 p = pStmt if p != 0 { v1 = int32((*TVdbe)(unsafe.Pointer(p)).FnVar) } else { v1 = 0 } return v1 } // C documentation // // /* // ** Return the name of a wildcard parameter. Return NULL if the index // ** is out of range or if the wildcard is unnamed. // ** // ** The result is always UTF-8. // */ func Xsqlite3_bind_parameter_name(tls *libc.TLS, pStmt uintptr, i int32) (r uintptr) { var p uintptr _ = p p = pStmt if p == uintptr(0) { return uintptr(0) } return _sqlite3VListNumToName(tls, (*TVdbe)(unsafe.Pointer(p)).FpVList, i) } // C documentation // // /* // ** Given a wildcard parameter name, return the index of the variable // ** with that name. If there is no variable with the given name, // ** return 0. // */ func _sqlite3VdbeParameterIndex(tls *libc.TLS, p uintptr, zName uintptr, nName int32) (r int32) { if p == uintptr(0) || zName == uintptr(0) { return 0 } return _sqlite3VListNameToNum(tls, (*TVdbe)(unsafe.Pointer(p)).FpVList, zName, nName) } func Xsqlite3_bind_parameter_index(tls *libc.TLS, pStmt uintptr, zName uintptr) (r int32) { return _sqlite3VdbeParameterIndex(tls, pStmt, zName, _sqlite3Strlen30(tls, zName)) } // C documentation // // /* // ** Transfer all bindings from the first statement over to the second. // */ func _sqlite3TransferBindings(tls *libc.TLS, pFromStmt uintptr, pToStmt uintptr) (r int32) { var i int32 var pFrom, pTo uintptr _, _, _ = i, pFrom, pTo pFrom = pFromStmt pTo = pToStmt _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(pTo)).Fdb)).Fmutex) i = 0 for { if !(i < int32((*TVdbe)(unsafe.Pointer(pFrom)).FnVar)) { break } _sqlite3VdbeMemMove(tls, (*TVdbe)(unsafe.Pointer(pTo)).FaVar+uintptr(i)*40, (*TVdbe)(unsafe.Pointer(pFrom)).FaVar+uintptr(i)*40) goto _1 _1: ; i++ } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(pTo)).Fdb)).Fmutex) return SQLITE_OK } // C documentation // // /* // ** Deprecated external interface. Internal/core SQLite code // ** should call sqlite3TransferBindings. // ** // ** It is misuse to call this routine with statements from different // ** database connections. But as this is a deprecated interface, we // ** will not bother to check for that condition. // ** // ** If the two statements contain a different number of bindings, then // ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise // ** SQLITE_OK is returned. // */ func Xsqlite3_transfer_bindings(tls *libc.TLS, pFromStmt uintptr, pToStmt uintptr) (r int32) { var pFrom, pTo uintptr _, _ = pFrom, pTo pFrom = pFromStmt pTo = pToStmt if int32((*TVdbe)(unsafe.Pointer(pFrom)).FnVar) != int32((*TVdbe)(unsafe.Pointer(pTo)).FnVar) { return int32(SQLITE_ERROR) } _ = libc.Int32FromInt32(0) if (*TVdbe)(unsafe.Pointer(pTo)).Fexpmask != 0 { libc.SetBitFieldPtr16Uint32(pTo+152, libc.Uint32FromInt32(1), 0, 0x3) } _ = libc.Int32FromInt32(0) if (*TVdbe)(unsafe.Pointer(pFrom)).Fexpmask != 0 { libc.SetBitFieldPtr16Uint32(pFrom+152, libc.Uint32FromInt32(1), 0, 0x3) } return _sqlite3TransferBindings(tls, pFromStmt, pToStmt) } // C documentation // // /* // ** Return the sqlite3* database handle to which the prepared statement given // ** in the argument belongs. This is the same database handle that was // ** the first argument to the sqlite3_prepare() that was used to create // ** the statement in the first place. // */ func Xsqlite3_db_handle(tls *libc.TLS, pStmt uintptr) (r uintptr) { var v1 uintptr _ = v1 if pStmt != 0 { v1 = (*TVdbe)(unsafe.Pointer(pStmt)).Fdb } else { v1 = uintptr(0) } return v1 } // C documentation // // /* // ** Return true if the prepared statement is guaranteed to not modify the // ** database. // */ func Xsqlite3_stmt_readonly(tls *libc.TLS, pStmt uintptr) (r int32) { var v1 int32 _ = v1 if pStmt != 0 { v1 = int32(Tbft(*(*uint16)(unsafe.Pointer(pStmt + 152)) & 0x40 >> 6)) } else { v1 = int32(1) } return v1 } // C documentation // // /* // ** Return 1 if the statement is an EXPLAIN and return 2 if the // ** statement is an EXPLAIN QUERY PLAN // */ func Xsqlite3_stmt_isexplain(tls *libc.TLS, pStmt uintptr) (r int32) { var v1 int32 _ = v1 if pStmt != 0 { v1 = int32(Tbft(*(*uint16)(unsafe.Pointer(pStmt + 152)) & 0xc >> 2)) } else { v1 = 0 } return v1 } // C documentation // // /* // ** Set the explain mode for a statement. // */ func Xsqlite3_stmt_explain(tls *libc.TLS, pStmt uintptr, eMode int32) (r int32) { var rc int32 var v uintptr _, _ = rc, v v = pStmt Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(v)).Fdb)).Fmutex) if int32(Tbft(*(*uint16)(unsafe.Pointer(v + 152))&0xc>>2)) == eMode { rc = SQLITE_OK } else { if eMode < 0 || eMode > int32(2) { rc = int32(SQLITE_ERROR) } else { if int32((*TVdbe)(unsafe.Pointer(v)).FprepFlags)&int32(SQLITE_PREPARE_SAVESQL) == 0 { rc = int32(SQLITE_ERROR) } else { if int32((*TVdbe)(unsafe.Pointer(v)).FeVdbeState) != int32(VDBE_READY_STATE) { rc = int32(SQLITE_BUSY) } else { if (*TVdbe)(unsafe.Pointer(v)).FnMem >= int32(10) && (eMode != int32(2) || int32(Tbft(*(*uint16)(unsafe.Pointer(v + 152))&0x100>>8)) != 0) { /* No reprepare necessary */ libc.SetBitFieldPtr16Uint32(v+152, uint32(eMode), 2, 0xc) rc = SQLITE_OK } else { libc.SetBitFieldPtr16Uint32(v+152, uint32(eMode), 2, 0xc) rc = _sqlite3Reprepare(tls, v) libc.SetBitFieldPtr16Uint32(v+152, libc.BoolUint32(eMode == libc.Int32FromInt32(2)), 8, 0x100) } } } } } if int32(Tbft(*(*uint16)(unsafe.Pointer(v + 152))&0xc>>2)) != 0 { (*TVdbe)(unsafe.Pointer(v)).FnResColumn = uint16(int32(12) - int32(4)*int32(Tbft(*(*uint16)(unsafe.Pointer(v + 152))&0xc>>2))) } else { (*TVdbe)(unsafe.Pointer(v)).FnResColumn = (*TVdbe)(unsafe.Pointer(v)).FnResAlloc } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(v)).Fdb)).Fmutex) return rc } // C documentation // // /* // ** Return true if the prepared statement is in need of being reset. // */ func Xsqlite3_stmt_busy(tls *libc.TLS, pStmt uintptr) (r int32) { var v uintptr _ = v v = pStmt return libc.BoolInt32(v != uintptr(0) && int32((*TVdbe)(unsafe.Pointer(v)).FeVdbeState) == int32(VDBE_RUN_STATE)) } // C documentation // // /* // ** Return a pointer to the next prepared statement after pStmt associated // ** with database connection pDb. If pStmt is NULL, return the first // ** prepared statement for the database connection. Return NULL if there // ** are no more. // */ func Xsqlite3_next_stmt(tls *libc.TLS, pDb uintptr, pStmt uintptr) (r uintptr) { var pNext uintptr _ = pNext Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(pDb)).Fmutex) if pStmt == uintptr(0) { pNext = (*Tsqlite3)(unsafe.Pointer(pDb)).FpVdbe } else { pNext = (*TVdbe)(unsafe.Pointer(pStmt)).FpVNext } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(pDb)).Fmutex) return pNext } // C documentation // // /* // ** Return the value of a status counter for a prepared statement // */ func Xsqlite3_stmt_status(tls *libc.TLS, pStmt uintptr, op int32, resetFlag int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var db, pVdbe uintptr var _ /* v at bp+0 */ Tu32 _, _ = db, pVdbe pVdbe = pStmt if op == int32(SQLITE_STMTSTATUS_MEMUSED) { db = (*TVdbe)(unsafe.Pointer(pVdbe)).Fdb Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed = bp _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart _sqlite3VdbeDelete(tls, pVdbe) (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpTrueEnd Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) } else { *(*Tu32)(unsafe.Pointer(bp)) = *(*Tu32)(unsafe.Pointer(pVdbe + 164 + uintptr(op)*4)) if resetFlag != 0 { *(*Tu32)(unsafe.Pointer(pVdbe + 164 + uintptr(op)*4)) = uint32(0) } } return int32(*(*Tu32)(unsafe.Pointer(bp))) } // C documentation // // /* // ** Return the SQL associated with a prepared statement // */ func Xsqlite3_sql(tls *libc.TLS, pStmt uintptr) (r uintptr) { var p, v1 uintptr _, _ = p, v1 p = pStmt if p != 0 { v1 = (*TVdbe)(unsafe.Pointer(p)).FzSql } else { v1 = uintptr(0) } return v1 } // C documentation // // /* // ** Return the SQL associated with a prepared statement with // ** bound parameters expanded. Space to hold the returned string is // ** obtained from sqlite3_malloc(). The caller is responsible for // ** freeing the returned string by passing it to sqlite3_free(). // ** // ** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of // ** expanded bound parameters. // */ func Xsqlite3_expanded_sql(tls *libc.TLS, pStmt uintptr) (r uintptr) { var p, z, zSql uintptr _, _, _ = p, z, zSql z = uintptr(0) zSql = Xsqlite3_sql(tls, pStmt) if zSql != 0 { p = pStmt Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) z = _sqlite3VdbeExpandSql(tls, p, zSql) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) } return z } // C documentation // // /* // ** Allocate and populate an UnpackedRecord structure based on the serialized // ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure // ** if successful, or a NULL pointer if an OOM error is encountered. // */ func _vdbeUnpackRecord(tls *libc.TLS, pKeyInfo uintptr, nKey int32, pKey uintptr) (r uintptr) { var pRet uintptr _ = pRet /* Return value */ pRet = _sqlite3VdbeAllocUnpackedRecord(tls, pKeyInfo) if pRet != 0 { libc.Xmemset(tls, (*TUnpackedRecord)(unsafe.Pointer(pRet)).FaMem, 0, uint32(40)*uint32(int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnKeyField)+libc.Int32FromInt32(1))) _sqlite3VdbeRecordUnpack(tls, pKeyInfo, nKey, pKey, pRet) } return pRet } // C documentation // // /* // ** This function is called from within a pre-update callback to retrieve // ** a field of the row currently being updated or deleted. // */ func Xsqlite3_preupdate_old(tls *libc.TLS, db uintptr, iIdx int32, ppValue uintptr) (r int32) { var aRec, p, pMem, v1 uintptr var nRec Tu32 var rc int32 _, _, _, _, _, _ = aRec, nRec, p, pMem, rc, v1 rc = SQLITE_OK p = (*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdate /* Test that this call is being made from within an SQLITE_DELETE or ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ if !(p != 0) || (*TPreUpdate)(unsafe.Pointer(p)).Fop == int32(SQLITE_INSERT) { rc = _sqlite3MisuseError(tls, int32(92098)) goto preupdate_old_out } if (*TPreUpdate)(unsafe.Pointer(p)).FpPk != 0 { iIdx = int32(_sqlite3TableColumnToIndex(tls, (*TPreUpdate)(unsafe.Pointer(p)).FpPk, int16(iIdx))) } if iIdx >= int32((*TVdbeCursor)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpCsr)).FnField) || iIdx < 0 { rc = int32(SQLITE_RANGE) goto preupdate_old_out } /* If the old.* record has not yet been loaded into memory, do so now. */ if (*TPreUpdate)(unsafe.Pointer(p)).FpUnpacked == uintptr(0) { _ = libc.Int32FromInt32(0) nRec = _sqlite3BtreePayloadSize(tls, *(*uintptr)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpCsr + 36))) aRec = _sqlite3DbMallocRaw(tls, db, uint64(nRec)) if !(aRec != 0) { goto preupdate_old_out } rc = _sqlite3BtreePayload(tls, *(*uintptr)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpCsr + 36)), uint32(0), nRec, aRec) if rc == SQLITE_OK { (*TPreUpdate)(unsafe.Pointer(p)).FpUnpacked = _vdbeUnpackRecord(tls, p+16, int32(nRec), aRec) if !((*TPreUpdate)(unsafe.Pointer(p)).FpUnpacked != 0) { rc = int32(SQLITE_NOMEM) } } if rc != SQLITE_OK { _sqlite3DbFree(tls, db, aRec) goto preupdate_old_out } (*TPreUpdate)(unsafe.Pointer(p)).FaRecord = aRec } v1 = (*TUnpackedRecord)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpUnpacked)).FaMem + uintptr(iIdx)*40 *(*uintptr)(unsafe.Pointer(ppValue)) = v1 pMem = v1 if iIdx == int32((*TTable)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpTab)).FiPKey) { _sqlite3VdbeMemSetInt64(tls, pMem, (*TPreUpdate)(unsafe.Pointer(p)).FiKey1) } else { if iIdx >= int32((*TUnpackedRecord)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpUnpacked)).FnField) { *(*uintptr)(unsafe.Pointer(ppValue)) = _columnNullValue(tls) } else { if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpTab)).FaCol + uintptr(iIdx)*12))).Faffinity) == int32(SQLITE_AFF_REAL) { if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 { _sqlite3VdbeMemRealify(tls, pMem) } } } } goto preupdate_old_out preupdate_old_out: ; _sqlite3Error(tls, db, rc) return _sqlite3ApiExit(tls, db, rc) } // C documentation // // /* // ** This function is called from within a pre-update callback to retrieve // ** the number of columns in the row being updated, deleted or inserted. // */ func Xsqlite3_preupdate_count(tls *libc.TLS, db uintptr) (r int32) { var p uintptr var v1 int32 _, _ = p, v1 p = (*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdate if p != 0 { v1 = int32((*TPreUpdate)(unsafe.Pointer(p)).Fkeyinfo.FnKeyField) } else { v1 = 0 } return v1 } // C documentation // // /* // ** This function is designed to be called from within a pre-update callback // ** only. It returns zero if the change that caused the callback was made // ** immediately by a user SQL statement. Or, if the change was made by a // ** trigger program, it returns the number of trigger programs currently // ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a // ** top-level trigger etc.). // ** // ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL // ** or SET DEFAULT action is considered a trigger. // */ func Xsqlite3_preupdate_depth(tls *libc.TLS, db uintptr) (r int32) { var p uintptr var v1 int32 _, _ = p, v1 p = (*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdate if p != 0 { v1 = (*TVdbe)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).Fv)).FnFrame } else { v1 = 0 } return v1 } // C documentation // // /* // ** This function is designed to be called from within a pre-update callback // ** only. // */ func Xsqlite3_preupdate_blobwrite(tls *libc.TLS, db uintptr) (r int32) { var p uintptr var v1 int32 _, _ = p, v1 p = (*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdate if p != 0 { v1 = (*TPreUpdate)(unsafe.Pointer(p)).FiBlobWrite } else { v1 = -int32(1) } return v1 } // C documentation // // /* // ** This function is called from within a pre-update callback to retrieve // ** a field of the row currently being updated or inserted. // */ func Xsqlite3_preupdate_new(tls *libc.TLS, db uintptr, iIdx int32, ppValue uintptr) (r int32) { var p, pData, pMem, pUnpack uintptr var rc, v1 int32 _, _, _, _, _, _ = p, pData, pMem, pUnpack, rc, v1 rc = SQLITE_OK p = (*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdate if !(p != 0) || (*TPreUpdate)(unsafe.Pointer(p)).Fop == int32(SQLITE_DELETE) { rc = _sqlite3MisuseError(tls, int32(92221)) goto preupdate_new_out } if (*TPreUpdate)(unsafe.Pointer(p)).FpPk != 0 && (*TPreUpdate)(unsafe.Pointer(p)).Fop != int32(SQLITE_UPDATE) { iIdx = int32(_sqlite3TableColumnToIndex(tls, (*TPreUpdate)(unsafe.Pointer(p)).FpPk, int16(iIdx))) } if iIdx >= int32((*TVdbeCursor)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpCsr)).FnField) || iIdx < 0 { rc = int32(SQLITE_RANGE) goto preupdate_new_out } if (*TPreUpdate)(unsafe.Pointer(p)).Fop == int32(SQLITE_INSERT) { /* For an INSERT, memory cell p->iNewReg contains the serialized record ** that is being inserted. Deserialize it. */ pUnpack = (*TPreUpdate)(unsafe.Pointer(p)).FpNewUnpacked if !(pUnpack != 0) { pData = (*TVdbe)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).Fv)).FaMem + uintptr((*TPreUpdate)(unsafe.Pointer(p)).FiNewReg)*40 if int32((*TMem)(unsafe.Pointer(pData)).Fflags)&int32(MEM_Zero) != 0 { v1 = _sqlite3VdbeMemExpandBlob(tls, pData) } else { v1 = 0 } rc = v1 if rc != SQLITE_OK { goto preupdate_new_out } pUnpack = _vdbeUnpackRecord(tls, p+16, (*TMem)(unsafe.Pointer(pData)).Fn, (*TMem)(unsafe.Pointer(pData)).Fz) if !(pUnpack != 0) { rc = int32(SQLITE_NOMEM) goto preupdate_new_out } (*TPreUpdate)(unsafe.Pointer(p)).FpNewUnpacked = pUnpack } pMem = (*TUnpackedRecord)(unsafe.Pointer(pUnpack)).FaMem + uintptr(iIdx)*40 if iIdx == int32((*TTable)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpTab)).FiPKey) { _sqlite3VdbeMemSetInt64(tls, pMem, (*TPreUpdate)(unsafe.Pointer(p)).FiKey2) } else { if iIdx >= int32((*TUnpackedRecord)(unsafe.Pointer(pUnpack)).FnField) { pMem = _columnNullValue(tls) } } } else { /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required ** value. Make a copy of the cell contents and return a pointer to it. ** It is not safe to return a pointer to the memory cell itself as the ** caller may modify the value text encoding. */ _ = libc.Int32FromInt32(0) if !((*TPreUpdate)(unsafe.Pointer(p)).FaNew != 0) { (*TPreUpdate)(unsafe.Pointer(p)).FaNew = _sqlite3DbMallocZero(tls, db, uint64(uint32(40)*uint32((*TVdbeCursor)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpCsr)).FnField))) if !((*TPreUpdate)(unsafe.Pointer(p)).FaNew != 0) { rc = int32(SQLITE_NOMEM) goto preupdate_new_out } } _ = libc.Int32FromInt32(0) pMem = (*TPreUpdate)(unsafe.Pointer(p)).FaNew + uintptr(iIdx)*40 if int32((*TMem)(unsafe.Pointer(pMem)).Fflags) == 0 { if iIdx == int32((*TTable)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpTab)).FiPKey) { _sqlite3VdbeMemSetInt64(tls, pMem, (*TPreUpdate)(unsafe.Pointer(p)).FiKey2) } else { rc = _sqlite3VdbeMemCopy(tls, pMem, (*TVdbe)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).Fv)).FaMem+uintptr((*TPreUpdate)(unsafe.Pointer(p)).FiNewReg+int32(1)+iIdx)*40) if rc != SQLITE_OK { goto preupdate_new_out } } } } *(*uintptr)(unsafe.Pointer(ppValue)) = pMem goto preupdate_new_out preupdate_new_out: ; _sqlite3Error(tls, db, rc) return _sqlite3ApiExit(tls, db, rc) } /************** End of vdbeapi.c *********************************************/ /************** Begin file vdbetrace.c ***************************************/ /* ** 2009 November 25 ** ** 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 contains code used to insert the values of host parameters ** (aka "wildcards") into the SQL text output by sqlite3_trace(). ** ** The Vdbe parse-tree explainer is also found here. */ /* #include "sqliteInt.h" */ /* #include "vdbeInt.h" */ // C documentation // // /* // ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of // ** bytes in this text up to but excluding the first character in // ** a host parameter. If the text contains no host parameters, return // ** the total number of bytes in the text. // */ func _findNextHostParameter(tls *libc.TLS, zSql uintptr, pnToken uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var n, nTotal int32 var _ /* tokenType at bp+0 */ int32 _, _ = n, nTotal nTotal = 0 *(*int32)(unsafe.Pointer(pnToken)) = 0 for *(*uint8)(unsafe.Pointer(zSql)) != 0 { n = _sqlite3GetToken(tls, zSql, bp) _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp)) == int32(TK_VARIABLE) { *(*int32)(unsafe.Pointer(pnToken)) = n break } nTotal += n zSql += uintptr(n) } return nTotal } // C documentation // // /* // ** This function returns a pointer to a nul-terminated string in memory // ** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the // ** string contains a copy of zRawSql but with host parameters expanded to // ** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1, // ** then the returned string holds a copy of zRawSql with "-- " prepended // ** to each line of text. // ** // ** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then // ** then long strings and blobs are truncated to that many bytes. This // ** can be used to prevent unreasonably large trace strings when dealing // ** with large (multi-megabyte) strings and blobs. // ** // ** The calling function is responsible for making sure the memory returned // ** is eventually freed. // ** // ** ALGORITHM: Scan the input string looking for host parameters in any of // ** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within // ** string literals, quoted identifier names, and comments. For text forms, // ** the host parameter index is found by scanning the prepared // ** statement for the corresponding OP_Variable opcode. Once the host // ** parameter index is known, locate the value in p->aVar[]. Then render // ** the value as a literal in place of the host parameter name. // */ func _sqlite3VdbeExpandSql(tls *libc.TLS, p uintptr, zRawSql uintptr) (r uintptr) { bp := tls.Alloc(96) defer tls.Free(96) var db, pVar, zStart, v1 uintptr var enc Tu8 var i, n, nOut, nOut1, nextIndex, v2 int32 var _ /* idx at bp+0 */ int32 var _ /* nToken at bp+4 */ int32 var _ /* out at bp+8 */ TStrAccum var _ /* utf8 at bp+32 */ TMem _, _, _, _, _, _, _, _, _, _, _ = db, enc, i, n, nOut, nOut1, nextIndex, pVar, zStart, v1, v2 /* The database connection */ *(*int32)(unsafe.Pointer(bp)) = 0 /* Index of a host parameter */ nextIndex = int32(1) /* Used to convert UTF16 into UTF8 for display */ db = (*TVdbe)(unsafe.Pointer(p)).Fdb _sqlite3StrAccumInit(tls, bp+8, uintptr(0), uintptr(0), 0, *(*int32)(unsafe.Pointer(db + 120))) if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec > int32(1) { for *(*uint8)(unsafe.Pointer(zRawSql)) != 0 { zStart = zRawSql for { v1 = zRawSql zRawSql++ if !(int32(*(*uint8)(unsafe.Pointer(v1))) != int32('\n') && *(*uint8)(unsafe.Pointer(zRawSql)) != 0) { break } } Xsqlite3_str_append(tls, bp+8, __ccgo_ts+5546, int32(3)) _ = libc.Int32FromInt32(0) Xsqlite3_str_append(tls, bp+8, zStart, int32(zRawSql)-int32(zStart)) } } else { if int32((*TVdbe)(unsafe.Pointer(p)).FnVar) == 0 { Xsqlite3_str_append(tls, bp+8, zRawSql, _sqlite3Strlen30(tls, zRawSql)) } else { for *(*uint8)(unsafe.Pointer(zRawSql)) != 0 { n = _findNextHostParameter(tls, zRawSql, bp+4) _ = libc.Int32FromInt32(0) Xsqlite3_str_append(tls, bp+8, zRawSql, n) zRawSql += uintptr(n) _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp + 4)) == 0 { break } if int32(*(*uint8)(unsafe.Pointer(zRawSql))) == int32('?') { if *(*int32)(unsafe.Pointer(bp + 4)) > int32(1) { _ = libc.Int32FromInt32(0) _sqlite3GetInt32(tls, zRawSql+1, bp) } else { *(*int32)(unsafe.Pointer(bp)) = nextIndex } } else { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp)) = _sqlite3VdbeParameterIndex(tls, p, zRawSql, *(*int32)(unsafe.Pointer(bp + 4))) _ = libc.Int32FromInt32(0) } zRawSql += uintptr(*(*int32)(unsafe.Pointer(bp + 4))) if *(*int32)(unsafe.Pointer(bp))+int32(1) > nextIndex { v2 = *(*int32)(unsafe.Pointer(bp)) + int32(1) } else { v2 = nextIndex } nextIndex = v2 _ = libc.Int32FromInt32(0) pVar = (*TVdbe)(unsafe.Pointer(p)).FaVar + uintptr(*(*int32)(unsafe.Pointer(bp))-int32(1))*40 if int32((*TMem)(unsafe.Pointer(pVar)).Fflags)&int32(MEM_Null) != 0 { Xsqlite3_str_append(tls, bp+8, __ccgo_ts+1649, int32(4)) } else { if int32((*TMem)(unsafe.Pointer(pVar)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 { Xsqlite3_str_appendf(tls, bp+8, __ccgo_ts+1404, libc.VaList(bp+80, *(*Ti64)(unsafe.Pointer(pVar)))) } else { if int32((*TMem)(unsafe.Pointer(pVar)).Fflags)&int32(MEM_Real) != 0 { Xsqlite3_str_appendf(tls, bp+8, __ccgo_ts+5067, libc.VaList(bp+80, *(*float64)(unsafe.Pointer(pVar)))) } else { if int32((*TMem)(unsafe.Pointer(pVar)).Fflags)&int32(MEM_Str) != 0 { /* Number of bytes of the string text to include in output */ enc = (*Tsqlite3)(unsafe.Pointer(db)).Fenc if int32(enc) != int32(SQLITE_UTF8) { libc.Xmemset(tls, bp+32, 0, uint32(40)) (*(*TMem)(unsafe.Pointer(bp + 32))).Fdb = db _sqlite3VdbeMemSetStr(tls, bp+32, (*TMem)(unsafe.Pointer(pVar)).Fz, int64((*TMem)(unsafe.Pointer(pVar)).Fn), enc, libc.UintptrFromInt32(0)) if int32(SQLITE_NOMEM) == _sqlite3VdbeChangeEncoding(tls, bp+32, int32(SQLITE_UTF8)) { (*(*TStrAccum)(unsafe.Pointer(bp + 8))).FaccError = uint8(SQLITE_NOMEM) (*(*TStrAccum)(unsafe.Pointer(bp + 8))).FnAlloc = uint32(0) } pVar = bp + 32 } nOut = (*TMem)(unsafe.Pointer(pVar)).Fn Xsqlite3_str_appendf(tls, bp+8, __ccgo_ts+5550, libc.VaList(bp+80, nOut, (*TMem)(unsafe.Pointer(pVar)).Fz)) if int32(enc) != int32(SQLITE_UTF8) { _sqlite3VdbeMemRelease(tls, bp+32) } } else { if int32((*TMem)(unsafe.Pointer(pVar)).Fflags)&int32(MEM_Zero) != 0 { Xsqlite3_str_appendf(tls, bp+8, __ccgo_ts+5557, libc.VaList(bp+80, *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pVar)).Fu)))) } else { /* Number of bytes of the blob to include in output */ _ = libc.Int32FromInt32(0) Xsqlite3_str_append(tls, bp+8, __ccgo_ts+5570, int32(2)) nOut1 = (*TMem)(unsafe.Pointer(pVar)).Fn i = 0 for { if !(i < nOut1) { break } Xsqlite3_str_appendf(tls, bp+8, __ccgo_ts+5573, libc.VaList(bp+80, int32(*(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pVar)).Fz + uintptr(i))))&int32(0xff))) goto _3 _3: ; i++ } Xsqlite3_str_append(tls, bp+8, __ccgo_ts+5578, int32(1)) } } } } } } } } if (*(*TStrAccum)(unsafe.Pointer(bp + 8))).FaccError != 0 { Xsqlite3_str_reset(tls, bp+8) } return _sqlite3StrAccumFinish(tls, bp+8) } /************** End of vdbetrace.c *******************************************/ /************** Begin file vdbe.c ********************************************/ /* ** 2001 September 15 ** ** 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. ** ************************************************************************* ** The code in this file implements the function that runs the ** bytecode of a prepared statement. ** ** Various scripts scan this source file in order to generate HTML ** documentation, headers files, or other derived files. The formatting ** of the code in this file is, therefore, important. See other comments ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. */ /* #include "sqliteInt.h" */ /* #include "vdbeInt.h" */ /* ** Invoke this macro on memory cells just prior to changing the ** value of the cell. This macro verifies that shallow copies are ** not misused. A shallow copy of a string or blob just copies a ** pointer to the string or blob, not the content. If the original ** is changed while the copy is still in use, the string or blob might ** be changed out from under the copy. This macro verifies that nothing ** like that ever happens. */ /* ** The following global variable is incremented every time a cursor ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test ** procedures use this information to make sure that indices are ** working correctly. This variable has no function other than to ** help verify the correct operation of the library. */ /* ** When this global variable is positive, it gets decremented once before ** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted ** field of the sqlite3 structure is set in order to simulate an interrupt. ** ** This facility is used for testing purposes only. It does not function ** in an ordinary build. */ /* ** The next global variable is incremented each type the OP_Sort opcode ** is executed. The test procedures use this information to make sure that ** sorting is occurring or not occurring at appropriate times. This variable ** has no function other than to help verify the correct operation of the ** library. */ /* ** The next global variable records the size of the largest MEM_Blob ** or MEM_Str that has been used by a VDBE opcode. The test procedures ** use this information to make sure that the zero-blob functionality ** is working correctly. This variable has no function other than to ** help verify the correct operation of the library. */ /* ** This macro evaluates to true if either the update hook or the preupdate ** hook are enabled for database connect DB. */ /* ** The next global variable is incremented each time the OP_Found opcode ** is executed. This is used to test whether or not the foreign key ** operation implemented using OP_FkIsZero is working. This variable ** has no function other than to help verify the correct operation of the ** library. */ /* ** Test a register to see if it exceeds the current maximum blob size. ** If it does, record the new maximum blob size. */ /* ** Invoke the VDBE coverage callback, if that callback is defined. This ** feature is used for test suite validation only and does not appear an ** production builds. ** ** M is the type of branch. I is the direction taken for this instance of ** the branch. ** ** M: 2 - two-way branch (I=0: fall-thru 1: jump ) ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL ) ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3) ** ** In other words, if M is 2, then I is either 0 (for fall-through) or ** 1 (for when the branch is taken). If M is 3, the I is 0 for an ** ordinary fall-through, I is 1 if the branch was taken, and I is 2 ** if the result of comparison is NULL. For M=3, I=2 the jump may or ** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5. ** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2 ** depending on if the operands are less than, equal, or greater than. ** ** iSrcLine is the source code line (from the __LINE__ macro) that ** generated the VDBE instruction combined with flag bits. The source ** code line number is in the lower 24 bits of iSrcLine and the upper ** 8 bytes are flags. The lower three bits of the flags indicate ** values for I that should never occur. For example, if the branch is ** always taken, the flags should be 0x05 since the fall-through and ** alternate branch are never taken. If a branch is never taken then ** flags should be 0x06 since only the fall-through approach is allowed. ** ** Bit 0x08 of the flags indicates an OP_Jump opcode that is only ** interested in equal or not-equal. In other words, I==0 and I==2 ** should be treated as equivalent ** ** Since only a line number is retained, not the filename, this macro ** only works for amalgamation builds. But that is ok, since these macros ** should be no-ops except for special builds used to measure test coverage. */ /* ** An ephemeral string value (signified by the MEM_Ephem flag) contains ** a pointer to a dynamically allocated string where some other entity ** is responsible for deallocating that string. Because the register ** does not control the string, it might be deleted without the register ** knowing it. ** ** This routine converts an ephemeral string into a dynamically allocated ** string that the register itself controls. In other words, it ** converts an MEM_Ephem string into a string with P.z==P.zMalloc. */ /* Return true if the cursor was opened using the OP_OpenSorter opcode. */ // C documentation // // /* // ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL // ** if we run out of memory. // */ func _allocateCursor(tls *libc.TLS, p uintptr, iCur int32, nField int32, eCurType Tu8) (r uintptr) { var nByte, v2 int32 var pCx, pMem, v1, v3, v4 uintptr _, _, _, _, _, _, _ = nByte, pCx, pMem, v1, v2, v3, v4 if iCur > 0 { v1 = (*TVdbe)(unsafe.Pointer(p)).FaMem + uintptr((*TVdbe)(unsafe.Pointer(p)).FnMem-iCur)*40 } else { v1 = (*TVdbe)(unsafe.Pointer(p)).FaMem } /* Find the memory cell that will be used to store the blob of memory ** required for this VdbeCursor structure. It is convenient to use a ** vdbe memory cell to manage the memory allocation required for a ** VdbeCursor structure for the following reasons: ** ** * Sometimes cursor numbers are used for a couple of different ** purposes in a vdbe program. The different uses might require ** different sized allocations. Memory cells provide growable ** allocations. ** ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can ** be freed lazily via the sqlite3_release_memory() API. This ** minimizes the number of malloc calls made by the system. ** ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from ** the top of the register space. Cursor 1 is at Mem[p->nMem-1]. ** Cursor 2 is at Mem[p->nMem-2]. And so forth. */ pMem = v1 pCx = uintptr(0) if int32(eCurType) == CURTYPE_BTREE { v2 = _sqlite3BtreeCursorSize(tls) } else { v2 = 0 } nByte = int32((libc.Uint32FromInt64(88)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)) + libc.Uint32FromInt32(2)*libc.Uint32FromInt64(4)*uint32(nField) + uint32(v2)) _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr(iCur)*4)) != 0 { /*OPTIMIZATION-IF-FALSE*/ _sqlite3VdbeFreeCursorNN(tls, p, *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr(iCur)*4))) *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr(iCur)*4)) = uintptr(0) } /* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure ** the pMem used to hold space for the cursor has enough storage available ** in pMem->zMalloc. But for the special case of the aMem[] entries used ** to hold cursors, it is faster to in-line the logic. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TMem)(unsafe.Pointer(pMem)).FszMalloc < nByte { if (*TMem)(unsafe.Pointer(pMem)).FszMalloc > 0 { _sqlite3DbFreeNN(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).FzMalloc) } v3 = _sqlite3DbMallocRaw(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, uint64(nByte)) (*TMem)(unsafe.Pointer(pMem)).FzMalloc = v3 (*TMem)(unsafe.Pointer(pMem)).Fz = v3 if (*TMem)(unsafe.Pointer(pMem)).FzMalloc == uintptr(0) { (*TMem)(unsafe.Pointer(pMem)).FszMalloc = 0 return uintptr(0) } (*TMem)(unsafe.Pointer(pMem)).FszMalloc = nByte } v4 = (*TMem)(unsafe.Pointer(pMem)).FzMalloc pCx = v4 *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr(iCur)*4)) = v4 libc.Xmemset(tls, pCx, 0, uint32(libc.UintptrFromInt32(0)+32)) (*TVdbeCursor)(unsafe.Pointer(pCx)).FeCurType = eCurType (*TVdbeCursor)(unsafe.Pointer(pCx)).FnField = int16(nField) (*TVdbeCursor)(unsafe.Pointer(pCx)).FaOffset = pCx + 84 + uintptr(nField)*4 if int32(eCurType) == CURTYPE_BTREE { *(*uintptr)(unsafe.Pointer(pCx + 36)) = (*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((libc.Uint32FromInt64(88)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))+libc.Uint32FromInt32(2)*libc.Uint32FromInt64(4)*uint32(nField)) _sqlite3BtreeCursorZero(tls, *(*uintptr)(unsafe.Pointer(pCx + 36))) } return pCx } // C documentation // // /* // ** The string in pRec is known to look like an integer and to have a // ** floating point value of rValue. Return true and set *piValue to the // ** integer value if the string is in range to be an integer. Otherwise, // ** return false. // */ func _alsoAnInt(tls *libc.TLS, pRec uintptr, rValue float64, piValue uintptr) (r int32) { var iValue Ti64 _ = iValue iValue = _sqlite3RealToI64(tls, rValue) if _sqlite3RealSameAsInt(tls, rValue, iValue) != 0 { *(*Ti64)(unsafe.Pointer(piValue)) = iValue return int32(1) } return libc.BoolInt32(0 == _sqlite3Atoi64(tls, (*TMem)(unsafe.Pointer(pRec)).Fz, piValue, (*TMem)(unsafe.Pointer(pRec)).Fn, (*TMem)(unsafe.Pointer(pRec)).Fenc)) } // C documentation // // /* // ** Try to convert a value into a numeric representation if we can // ** do so without loss of information. In other words, if the string // ** looks like a number, convert it into a number. If it does not // ** look like a number, leave it alone. // ** // ** If the bTryForInt flag is true, then extra effort is made to give // ** an integer representation. Strings that look like floating point // ** values but which have no fractional component (example: '48.00') // ** will have a MEM_Int representation when bTryForInt is true. // ** // ** If bTryForInt is false, then if the input string contains a decimal // ** point or exponential notation, the result is only MEM_Real, even // ** if there is an exact integer representation of the quantity. // */ func _applyNumericAffinity(tls *libc.TLS, pRec uintptr, bTryForInt int32) { bp := tls.Alloc(16) defer tls.Free(16) var enc Tu8 var rc int32 var p1, p2, p3 uintptr var _ /* rValue at bp+0 */ float64 _, _, _, _, _ = enc, rc, p1, p2, p3 enc = (*TMem)(unsafe.Pointer(pRec)).Fenc _ = libc.Int32FromInt32(0) rc = _sqlite3AtoF(tls, (*TMem)(unsafe.Pointer(pRec)).Fz, bp, (*TMem)(unsafe.Pointer(pRec)).Fn, enc) if rc <= 0 { return } if rc == int32(1) && _alsoAnInt(tls, pRec, *(*float64)(unsafe.Pointer(bp)), pRec) != 0 { p1 = pRec + 16 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(MEM_Int)) } else { *(*float64)(unsafe.Pointer(pRec)) = *(*float64)(unsafe.Pointer(bp)) p2 = pRec + 16 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(MEM_Real)) if bTryForInt != 0 { _sqlite3VdbeIntegerAffinity(tls, pRec) } } /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the ** string representation after computing a numeric equivalent, because the ** string representation might not be the canonical representation for the ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */ p3 = pRec + 16 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) & ^libc.Int32FromInt32(MEM_Str)) } // C documentation // // /* // ** Processing is determine by the affinity parameter: // ** // ** SQLITE_AFF_INTEGER: // ** SQLITE_AFF_REAL: // ** SQLITE_AFF_NUMERIC: // ** Try to convert pRec to an integer representation or a // ** floating-point representation if an integer representation // ** is not possible. Note that the integer representation is // ** always preferred, even if the affinity is REAL, because // ** an integer representation is more space efficient on disk. // ** // ** SQLITE_AFF_FLEXNUM: // ** If the value is text, then try to convert it into a number of // ** some kind (integer or real) but do not make any other changes. // ** // ** SQLITE_AFF_TEXT: // ** Convert pRec to a text representation. // ** // ** SQLITE_AFF_BLOB: // ** SQLITE_AFF_NONE: // ** No-op. pRec is unchanged. // */ func _applyAffinity(tls *libc.TLS, pRec uintptr, affinity uint8, enc Tu8) { var p1 uintptr _ = p1 if int32(affinity) >= int32(SQLITE_AFF_NUMERIC) { _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Int) == 0 { /*OPTIMIZATION-IF-FALSE*/ if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&(libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)) == 0 { if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Str) != 0 { _applyNumericAffinity(tls, pRec, int32(1)) } } else { if int32(affinity) <= int32(SQLITE_AFF_REAL) { _sqlite3VdbeIntegerAffinity(tls, pRec) } } } } else { if int32(affinity) == int32(SQLITE_AFF_TEXT) { /* Only attempt the conversion to TEXT if there is an integer or real ** representation (blob and NULL do not get converted) but no string ** representation. It would be harmless to repeat the conversion if ** there is already a string rep, but it is pointless to waste those ** CPU cycles. */ if 0 == int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Str) { /*OPTIMIZATION-IF-FALSE*/ if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&(libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 { _sqlite3VdbeMemStringify(tls, pRec, enc, uint8(1)) } } p1 = pRec + 16 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^(libc.Int32FromInt32(MEM_Real) | libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_IntReal))) } } } // C documentation // // /* // ** Try to convert the type of a function argument or a result column // ** into a numeric representation. Use either INTEGER or REAL whichever // ** is appropriate. But only do the conversion if it is possible without // ** loss of information and return the revised type of the argument. // */ func Xsqlite3_value_numeric_type(tls *libc.TLS, pVal uintptr) (r int32) { var eType int32 var pMem uintptr _, _ = eType, pMem eType = Xsqlite3_value_type(tls, pVal) if eType == int32(SQLITE_TEXT) { pMem = pVal _applyNumericAffinity(tls, pMem, 0) eType = Xsqlite3_value_type(tls, pVal) } return eType } // C documentation // // /* // ** Exported version of applyAffinity(). This one works on sqlite3_value*, // ** not the internal Mem* type. // */ func _sqlite3ValueApplyAffinity(tls *libc.TLS, pVal uintptr, affinity Tu8, enc Tu8) { _applyAffinity(tls, pVal, affinity, enc) } // C documentation // // /* // ** pMem currently only holds a string type (or maybe a BLOB that we can // ** interpret as a string if we want to). Compute its corresponding // ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields // ** accordingly. // */ func _computeNumericType(tls *libc.TLS, pMem uintptr) (r Tu16) { bp := tls.Alloc(16) defer tls.Free(16) var rc, v1 int32 var _ /* ix at bp+0 */ Tsqlite3_int64 _, _ = rc, v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Zero) != 0 { v1 = _sqlite3VdbeMemExpandBlob(tls, pMem) } else { v1 = 0 } if v1 != 0 { *(*Ti64)(unsafe.Pointer(pMem)) = 0 return uint16(MEM_Int) } rc = _sqlite3AtoF(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, pMem, (*TMem)(unsafe.Pointer(pMem)).Fn, (*TMem)(unsafe.Pointer(pMem)).Fenc) if rc <= 0 { if rc == 0 && _sqlite3Atoi64(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, bp, (*TMem)(unsafe.Pointer(pMem)).Fn, (*TMem)(unsafe.Pointer(pMem)).Fenc) <= int32(1) { *(*Ti64)(unsafe.Pointer(pMem)) = *(*Tsqlite3_int64)(unsafe.Pointer(bp)) return uint16(MEM_Int) } else { return uint16(MEM_Real) } } else { if rc == int32(1) && _sqlite3Atoi64(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, bp, (*TMem)(unsafe.Pointer(pMem)).Fn, (*TMem)(unsafe.Pointer(pMem)).Fenc) == 0 { *(*Ti64)(unsafe.Pointer(pMem)) = *(*Tsqlite3_int64)(unsafe.Pointer(bp)) return uint16(MEM_Int) } } return uint16(MEM_Real) } // C documentation // // /* // ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or // ** none. // ** // ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. // ** But it does set pMem->u.r and pMem->u.i appropriately. // */ func _numericType(tls *libc.TLS, pMem uintptr) (r Tu16) { _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)|libc.Int32FromInt32(MEM_Null)) != 0 { return uint16(int32((*TMem)(unsafe.Pointer(pMem)).Fflags) & (libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_Real) | libc.Int32FromInt32(MEM_IntReal) | libc.Int32FromInt32(MEM_Null))) } _ = libc.Int32FromInt32(0) return _computeNumericType(tls, pMem) return uint16(0) } // C documentation // // /* // ** Return the register of pOp->p2 after first preparing it to be // ** overwritten with an integer value. // */ func _out2PrereleaseWithClear(tls *libc.TLS, pOut uintptr) (r uintptr) { _sqlite3VdbeMemSetNull(tls, pOut) (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Int) return pOut } func _out2Prerelease(tls *libc.TLS, p uintptr, pOp uintptr) (r uintptr) { var pOut uintptr _ = pOut _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pOut = (*TVdbe)(unsafe.Pointer(p)).FaMem + uintptr((*TVdbeOp)(unsafe.Pointer(pOp)).Fp2)*40 if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 { /*OPTIMIZATION-IF-FALSE*/ return _out2PrereleaseWithClear(tls, pOut) } else { (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Int) return pOut } return r } // C documentation // // /* // ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning // ** with pOp->p3. Return the hash. // */ func _filterHash(tls *libc.TLS, aMem uintptr, pOp uintptr) (r Tu64) { var h Tu64 var i, mx int32 var p uintptr _, _, _, _ = h, i, mx, p h = uint64(0) _ = libc.Int32FromInt32(0) i = (*TOp)(unsafe.Pointer(pOp)).Fp3 mx = i + *(*int32)(unsafe.Pointer(pOp + 16)) for { if !(i < mx) { break } p = aMem + uintptr(i)*40 if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 { h += uint64(*(*Ti64)(unsafe.Pointer(p))) } else { if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Real) != 0 { h += uint64(_sqlite3VdbeIntValue(tls, p)) } else { if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) != 0 { /* All strings have the same hash and all blobs have the same hash, ** though, at least, those hashes are different from each other and ** from NULL. */ h += uint64(int32(4093) + int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob))) } } } goto _1 _1: ; i++ } return h } // C documentation // // /* // ** For OP_Column, factor out the case where content is loaded from // ** overflow pages, so that the code to implement this case is separate // ** the common case where all content fits on the page. Factoring out // ** the code reduces register pressure and helps the common case // ** to run faster. // */ func _vdbeColumnFromOverflow(tls *libc.TLS, pC uintptr, iCol int32, t int32, iOffset Ti64, cacheStatus Tu32, colCacheCtr Tu32, pDest uintptr) (r int32) { var db, pBuf, pCache, v1, p2, p3, p4 uintptr var encoding, len1, rc int32 _, _, _, _, _, _, _, _, _, _ = db, encoding, len1, pBuf, pCache, rc, v1, p2, p3, p4 db = (*TMem)(unsafe.Pointer(pDest)).Fdb encoding = int32((*TMem)(unsafe.Pointer(pDest)).Fenc) len1 = int32(_sqlite3VdbeSerialTypeLen(tls, uint32(t))) _ = libc.Int32FromInt32(0) if len1 > *(*int32)(unsafe.Pointer(db + 120)) { return int32(SQLITE_TOOBIG) } if len1 > int32(4000) && (*TVdbeCursor)(unsafe.Pointer(pC)).FpKeyInfo == uintptr(0) { if int32(TBool(*(*uint8)(unsafe.Pointer(pC + 8))&0x10>>4)) == 0 { (*TVdbeCursor)(unsafe.Pointer(pC)).FpCache = _sqlite3DbMallocZero(tls, db, uint64(32)) if (*TVdbeCursor)(unsafe.Pointer(pC)).FpCache == uintptr(0) { return int32(SQLITE_NOMEM) } libc.SetBitFieldPtr8Uint32(pC+8, libc.Uint32FromInt32(1), 4, 0x10) } pCache = (*TVdbeCursor)(unsafe.Pointer(pC)).FpCache if (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FpCValue == uintptr(0) || (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FiCol != iCol || (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FcacheStatus != cacheStatus || (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FcolCacheCtr != colCacheCtr || (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FiOffset != _sqlite3BtreeOffset(tls, *(*uintptr)(unsafe.Pointer(pC + 36))) { if (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FpCValue != 0 { _sqlite3RCStrUnref(tls, (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FpCValue) } v1 = _sqlite3RCStrNew(tls, uint64(len1+int32(3))) (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FpCValue = v1 pBuf = v1 if pBuf == uintptr(0) { return int32(SQLITE_NOMEM) } rc = _sqlite3BtreePayload(tls, *(*uintptr)(unsafe.Pointer(pC + 36)), uint32(iOffset), uint32(len1), pBuf) if rc != 0 { return rc } *(*uint8)(unsafe.Pointer(pBuf + uintptr(len1))) = uint8(0) *(*uint8)(unsafe.Pointer(pBuf + uintptr(len1+int32(1)))) = uint8(0) *(*uint8)(unsafe.Pointer(pBuf + uintptr(len1+int32(2)))) = uint8(0) (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FiCol = iCol (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FcacheStatus = cacheStatus (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FcolCacheCtr = colCacheCtr (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FiOffset = _sqlite3BtreeOffset(tls, *(*uintptr)(unsafe.Pointer(pC + 36))) } else { pBuf = (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FpCValue } _ = libc.Int32FromInt32(0) _sqlite3RCStrRef(tls, pBuf) if t&int32(1) != 0 { rc = _sqlite3VdbeMemSetStr(tls, pDest, pBuf, int64(len1), uint8(encoding), __ccgo_fp(_sqlite3RCStrUnref)) p2 = pDest + 16 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(MEM_Term)) } else { rc = _sqlite3VdbeMemSetStr(tls, pDest, pBuf, int64(len1), uint8(0), __ccgo_fp(_sqlite3RCStrUnref)) } } else { rc = _sqlite3VdbeMemFromBtree(tls, *(*uintptr)(unsafe.Pointer(pC + 36)), uint32(iOffset), uint32(len1), pDest) if rc != 0 { return rc } _sqlite3VdbeSerialGet(tls, (*TMem)(unsafe.Pointer(pDest)).Fz, uint32(t), pDest) if t&int32(1) != 0 && encoding == int32(SQLITE_UTF8) { *(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pDest)).Fz + uintptr(len1))) = uint8(0) p3 = pDest + 16 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) | libc.Int32FromInt32(MEM_Term)) } } p4 = pDest + 16 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) & ^libc.Int32FromInt32(MEM_Ephem)) return rc } // C documentation // // /* // ** Return the symbolic name for the data type of a pMem // */ func _vdbeMemTypeName(tls *libc.TLS, pMem uintptr) (r uintptr) { return _azTypes[Xsqlite3_value_type(tls, pMem)-int32(1)] } var _azTypes = [5]uintptr{ 0: __ccgo_ts + 1132, 1: __ccgo_ts + 1144, 2: __ccgo_ts + 1149, 3: __ccgo_ts + 1127, 4: __ccgo_ts + 1649, } // C documentation // // /* // ** Execute as much of a VDBE program as we can. // ** This is the core of sqlite3_step(). // */ func _sqlite3VdbeExec(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(768) defer tls.Free(768) var aCol, aMem, aOffset, aOp, aPermute, aRoot, apArg, apArg1, db, pArgc, pBt, pBt1, pBt2, pBt3, pC, pC1, pC10, pC11, pC12, pC13, pC14, pC15, pC16, pC17, pC18, pC19, pC2, pC20, pC21, pC22, pC23, pC24, pC25, pC26, pC27, pC28, pC29, pC3, pC30, pC31, pC32, pC4, pC5, pC6, pC7, pC8, pC9, pCaller, pColl, pCrsr, pCrsr1, pCrsr2, pCrsr3, pCrsr4, pCrsr5, pCrsr6, pCrsr7, pCtx, pCtx1, pCtx2, pCur, pCur1, pCur2, pCur3, pCur4, pCur5, pCur6, pCx, pCx1, pCx2, pCx3, pData, pData0, pDb, pDb1, pDb2, pDb3, pDest, pDest1, pDest2, pEnd, pFrame, pFrame1, pFrame2, pFrame3, pFrame4, pIdxKey, pIn, pIn1, pIn2, pIn3, pKey, pKeyInfo, pKeyInfo1, pKeyInfo2, pLast, pMem, pMem1, pMem2, pMem3, pMem4, pModule, pModule1, pModule2, pModule3, pModule4, pModule5, pModule6, pName, pNew, pOp, pOrig, pOut, pPager, pProgram, pQuery, pRec, pReg, pRhs, pRt, pSavepoint, pSrc, pTab, pTab1, pTab2, pTab3, pTabCur, pTmp, pVCur1, pVTab, pVar, pVtab, pVtab1, pVtab2, pVtab3, pVtab4, pVtab5, pVtab6, pVtab7, pX, pX1, pnErr, t1, z1, z2, z3, zAffinity, zAffinity1, zData, zDb, zDb1, zEndHdr, zFilename, zHdr, zHdr1, zName, zPayload, zSchema, zSql, zTab, zTrace, v240, v241, v242, v250, v251, v252, v253, v255, v263, v278, v279, v286, v288, v297, v298, p189, p192, p193, p196, p199, p208, p209, p212, p227, p228, p229, p230, p231, p232, p233, p234, p235, p236, p237, p238, p239, p293, p294, p295, p296 uintptr var affinity uint8 var alreadyExists, bRev, c, c1, c2, cnt, cnt1, desiredAutoCommit, eNew, eOld, eqOnly, exists, i, i1, i2, i4, i5, i6, i7, i8, i9, iCompare, iCookie, iDb, iDb1, iDb2, iDb3, iQuery, iRollback, iSavepoint, iSet, ii, ii1, isLegacy, isSchemaChange, isTransaction, len1, n, n1, n2, n4, nArg, nArg1, nByte2, nField, nField1, nField2, nHdr, nKeyCol, nMem, nName, nRoot, nStep, nVarint, oc, opflags, p1, p11, p12, p13, p2, p21, pcDest, pcx, rc, res, res10, res11, res12, res21, seekResult, v11, v21, wrFlag, v188, v191, v194, v197, v198, v205, v206, v207, v223, v224, v244, v245, v248, v249, v256, v260, v265, v266, v269, v270, v272, v273, v274, v275, v284, v287, v291 int32 var colCacheCtr, iAddr, iMap, iPrior, idx, len11, n3, p22, p23, serialType, serial_type, v213, v214, v216, v221, v222 Tu32 var encoding, isWriteLock, mTrace, op, p5, resetSchemaOnFault, vtabOnConflict, v225 Tu8 var flags1, flags11, flags2, flags3, flags31, newType, nullFlag, type1, type2, typeMask, v190 Tu16 var h, h1, iKey1, nData, nProgressLimit, nVmStep, offset64, uu Tu64 var i3, iA, iB1, iKey, iKey2, nByte, nByte1, nCellKey, nZero, sz, v254, v262 Ti64 var newMax, v211 uint32 var rA, rB float64 var xAuth Tsqlite3_xauth var v204, v271, v281 int64 var v215, v267, v282, v299 bool var _ /* aRes at bp+584 */ [3]int32 var _ /* iA at bp+8 */ Ti64 var _ /* iB at bp+0 */ Ti64 var _ /* iMeta at bp+88 */ int32 var _ /* iMeta at bp+92 */ int32 var _ /* iMoved at bp+504 */ int32 var _ /* initData at bp+528 */ TInitData var _ /* m at bp+464 */ TMem var _ /* nChange at bp+512 */ Ti64 var _ /* nEntry at bp+80 */ Ti64 var _ /* nErr at bp+556 */ int32 var _ /* nullFunc at bp+680 */ TFuncDef var _ /* pVCur at bp+640 */ uintptr var _ /* pgno at bp+520 */ TPgno var _ /* r at bp+104 */ TUnpackedRecord var _ /* r at bp+144 */ TUnpackedRecord var _ /* r at bp+176 */ TUnpackedRecord var _ /* r at bp+392 */ TUnpackedRecord var _ /* r at bp+432 */ TUnpackedRecord var _ /* res at bp+136 */ int32 var _ /* res at bp+208 */ int32 var _ /* res at bp+264 */ int32 var _ /* res at bp+312 */ int32 var _ /* res at bp+328 */ int32 var _ /* res at bp+332 */ int32 var _ /* res at bp+336 */ int32 var _ /* res at bp+384 */ int32 var _ /* res at bp+96 */ int32 var _ /* rowid at bp+424 */ Ti64 var _ /* rowid at bp+720 */ Tsqlite_int64 var _ /* sContext at bp+648 */ Tsqlite3_context var _ /* sMem at bp+24 */ TMem var _ /* sMem at bp+600 */ TMem var _ /* t at bp+64 */ Tu32 var _ /* uA at bp+16 */ Tu64 var _ /* v at bp+256 */ Ti64 var _ /* v at bp+320 */ Ti64 var _ /* v at bp+72 */ Tu64 var _ /* val at bp+568 */ Ti64 var _ /* x at bp+216 */ TMem var _ /* x at bp+272 */ TBtreePayload var _ /* x at bp+344 */ TBtreePayload var _ /* x at bp+576 */ Ti64 var _ /* z at bp+560 */ uintptr var _ /* zErr at bp+524 */ uintptr var _ /* zErr at bp+644 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aCol, aMem, aOffset, aOp, aPermute, aRoot, affinity, alreadyExists, apArg, apArg1, bRev, c, c1, c2, cnt, cnt1, colCacheCtr, db, desiredAutoCommit, eNew, eOld, encoding, eqOnly, exists, flags1, flags11, flags2, flags3, flags31, h, h1, i, i1, i2, i3, i4, i5, i6, i7, i8, i9, iA, iAddr, iB1, iCompare, iCookie, iDb, iDb1, iDb2, iDb3, iKey, iKey1, iKey2, iMap, iPrior, iQuery, iRollback, iSavepoint, iSet, idx, ii, ii1, isLegacy, isSchemaChange, isTransaction, isWriteLock, len1, len11, mTrace, n, n1, n2, n3, n4, nArg, nArg1, nByte, nByte1, nByte2, nCellKey, nData, nField, nField1, nField2, nHdr, nKeyCol, nMem, nName, nProgressLimit, nRoot, nStep, nVarint, nVmStep, nZero, newMax, newType, nullFlag, oc, offset64, op, opflags, p1, p11, p12, p13, p2, p21, p22, p23, p5, pArgc, pBt, pBt1, pBt2, pBt3, pC, pC1, pC10, pC11, pC12, pC13, pC14, pC15, pC16, pC17, pC18, pC19, pC2, pC20, pC21, pC22, pC23, pC24, pC25, pC26, pC27, pC28, pC29, pC3, pC30, pC31, pC32, pC4, pC5, pC6, pC7, pC8, pC9, pCaller, pColl, pCrsr, pCrsr1, pCrsr2, pCrsr3, pCrsr4, pCrsr5, pCrsr6, pCrsr7, pCtx, pCtx1, pCtx2, pCur, pCur1, pCur2, pCur3, pCur4, pCur5, pCur6, pCx, pCx1, pCx2, pCx3, pData, pData0, pDb, pDb1, pDb2, pDb3, pDest, pDest1, pDest2, pEnd, pFrame, pFrame1, pFrame2, pFrame3, pFrame4, pIdxKey, pIn, pIn1, pIn2, pIn3, pKey, pKeyInfo, pKeyInfo1, pKeyInfo2, pLast, pMem, pMem1, pMem2, pMem3, pMem4, pModule, pModule1, pModule2, pModule3, pModule4, pModule5, pModule6, pName, pNew, pOp, pOrig, pOut, pPager, pProgram, pQuery, pRec, pReg, pRhs, pRt, pSavepoint, pSrc, pTab, pTab1, pTab2, pTab3, pTabCur, pTmp, pVCur1, pVTab, pVar, pVtab, pVtab1, pVtab2, pVtab3, pVtab4, pVtab5, pVtab6, pVtab7, pX, pX1, pcDest, pcx, pnErr, rA, rB, rc, res, res10, res11, res12, res21, resetSchemaOnFault, seekResult, serialType, serial_type, sz, t1, type1, type2, typeMask, uu, v11, v21, vtabOnConflict, wrFlag, xAuth, z1, z2, z3, zAffinity, zAffinity1, zData, zDb, zDb1, zEndHdr, zFilename, zHdr, zHdr1, zName, zPayload, zSchema, zSql, zTab, zTrace, v188, v190, v191, v194, v197, v198, v204, v205, v206, v207, v211, v213, v214, v215, v216, v221, v222, v223, v224, v225, v240, v241, v242, v244, v245, v248, v249, v250, v251, v252, v253, v254, v255, v256, v260, v262, v263, v265, v266, v267, v269, v270, v271, v272, v273, v274, v275, v278, v279, v281, v282, v284, v286, v287, v288, v291, v297, v298, v299, p189, p192, p193, p196, p199, p208, p209, p212, p227, p228, p229, p230, p231, p232, p233, p234, p235, p236, p237, p238, p239, p293, p294, p295, p296 aOp = (*TVdbe)(unsafe.Pointer(p)).FaOp /* Copy of p->aOp */ pOp = aOp /* Current operation */ rc = SQLITE_OK /* Value to return */ db = (*TVdbe)(unsafe.Pointer(p)).Fdb /* The database */ resetSchemaOnFault = uint8(0) /* Reset schema after an error if positive */ encoding = (*Tsqlite3)(unsafe.Pointer(db)).Fenc /* The database encoding */ iCompare = 0 /* Result of last comparison */ nVmStep = uint64(0) /* Invoke xProgress() when nVmStep reaches this */ aMem = (*TVdbe)(unsafe.Pointer(p)).FaMem /* Copy of p->aMem */ pIn1 = uintptr(0) /* 1st input operand */ pIn2 = uintptr(0) /* 2nd input operand */ pIn3 = uintptr(0) /* 3rd input operand */ pOut = uintptr(0) /* Output operand */ colCacheCtr = uint32(0) /* Column cache counter */ /*** INSERT STACK UNION HERE ***/ _ = libc.Int32FromInt32(0) /* sqlite3_step() verifies this */ if (*TVdbe)(unsafe.Pointer(p)).FlockMask != uint32(0) { _sqlite3VdbeEnter(tls, p) } if (*Tsqlite3)(unsafe.Pointer(db)).FxProgress != 0 { iPrior = *(*Tu32)(unsafe.Pointer(p + 164 + 4*4)) _ = libc.Int32FromInt32(0) nProgressLimit = uint64((*Tsqlite3)(unsafe.Pointer(db)).FnProgressOps - iPrior%(*Tsqlite3)(unsafe.Pointer(db)).FnProgressOps) } else { nProgressLimit = libc.Uint64FromUint32(0xffffffff) | libc.Uint64FromUint32(0xffffffff)<= nProgressLimit && (*Tsqlite3)(unsafe.Pointer(db)).FxProgress != uintptr(0) { _ = libc.Int32FromInt32(0) nProgressLimit += uint64((*Tsqlite3)(unsafe.Pointer(db)).FnProgressOps) if (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxProgress})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpProgressArg) != 0 { nProgressLimit = libc.Uint64FromUint32(0xffffffff) | libc.Uint64FromUint32(0xffffffff)< *(*int32)(unsafe.Pointer(db + 120)) { goto too_big } (*TOp)(unsafe.Pointer(pOp)).Fopcode = uint8(OP_String) _ = libc.Int32FromInt32(0) /* Opcode: String P1 P2 P3 P4 P5 ** Synopsis: r[P2]='P4' (len=P1) ** ** The string value P4 of length P1 (bytes) is stored in register P2. ** ** If P3 is not zero and the content of register P3 is equal to P5, then ** the datatype of the register P2 is converted to BLOB. The content is ** the same sequence of bytes, it is merely interpreted as a BLOB instead ** of a string, as if it had been CAST. In other words: ** ** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB) */ _14: ; /* out2 */ _ = libc.Int32FromInt32(0) pOut = _out2Prerelease(tls, p, pOp) (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(libc.Int32FromInt32(MEM_Str) | libc.Int32FromInt32(MEM_Static) | libc.Int32FromInt32(MEM_Term)) (*TMem)(unsafe.Pointer(pOut)).Fz = *(*uintptr)(unsafe.Pointer(pOp + 16)) (*TMem)(unsafe.Pointer(pOut)).Fn = (*TOp)(unsafe.Pointer(pOp)).Fp1 (*TMem)(unsafe.Pointer(pOut)).Fenc = encoding goto _187 /* Opcode: BeginSubrtn * P2 * * * ** Synopsis: r[P2]=NULL ** ** Mark the beginning of a subroutine that can be entered in-line ** or that can be called using OP_Gosub. The subroutine should ** be terminated by an OP_Return instruction that has a P1 operand that ** is the same as the P2 operand to this opcode and that has P3 set to 1. ** If the subroutine is entered in-line, then the OP_Return will simply ** fall through. But if the subroutine is entered using OP_Gosub, then ** the OP_Return will jump back to the first instruction after the OP_Gosub. ** ** This routine works by loading a NULL into the P2 register. When the ** return address register contains a NULL, the OP_Return instruction is ** a no-op that simply falls through to the next instruction (assuming that ** the OP_Return opcode has a P3 value of 1). Thus if the subroutine is ** entered in-line, then the OP_Return will cause in-line execution to ** continue. But if the subroutine is entered via OP_Gosub, then the ** OP_Return will cause a return to the address following the OP_Gosub. ** ** This opcode is identical to OP_Null. It has a different name ** only to make the byte code easier to read and verify. */ /* Opcode: Null P1 P2 P3 * * ** Synopsis: r[P2..P3]=NULL ** ** Write a NULL into registers P2. If P3 greater than P2, then also write ** NULL into register P3 and every register in between P2 and P3. If P3 ** is less than P2 (typically P3 is zero) then only register P2 is ** set to NULL. ** ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that ** NULL values will not compare equal even if SQLITE_NULLEQ is set on ** OP_Ne or OP_Eq. */ _16: ; _15: ; pOut = _out2Prerelease(tls, p, pOp) cnt = (*TOp)(unsafe.Pointer(pOp)).Fp3 - (*TOp)(unsafe.Pointer(pOp)).Fp2 _ = libc.Int32FromInt32(0) if (*TOp)(unsafe.Pointer(pOp)).Fp1 != 0 { v191 = libc.Int32FromInt32(MEM_Null) | libc.Int32FromInt32(MEM_Cleared) } else { v191 = int32(MEM_Null) } v190 = uint16(v191) nullFlag = v190 (*TMem)(unsafe.Pointer(pOut)).Fflags = v190 (*TMem)(unsafe.Pointer(pOut)).Fn = 0 for cnt > 0 { pOut += 40 _sqlite3VdbeMemSetNull(tls, pOut) (*TMem)(unsafe.Pointer(pOut)).Fflags = nullFlag (*TMem)(unsafe.Pointer(pOut)).Fn = 0 cnt-- } goto _187 /* Opcode: SoftNull P1 * * * * ** Synopsis: r[P1]=NULL ** ** Set register P1 to have the value NULL as seen by the OP_MakeRecord ** instruction, but do not free any string or blob memory associated with ** the register, so that if the value was a string or blob that was ** previously copied using OP_SCopy, the copies will continue to be valid. */ _17: ; _ = libc.Int32FromInt32(0) pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_Undefined)|libc.Int32FromInt32(MEM_AffMask)) | int32(MEM_Null)) goto _187 /* Opcode: Blob P1 P2 * P4 * ** Synopsis: r[P2]=P4 (len=P1) ** ** P4 points to a blob of data P1 bytes long. Store this ** blob in register P2. If P4 is a NULL pointer, then construct ** a zero-filled blob that is P1 bytes long in P2. */ _18: ; /* out2 */ _ = libc.Int32FromInt32(0) pOut = _out2Prerelease(tls, p, pOp) if *(*uintptr)(unsafe.Pointer(pOp + 16)) == uintptr(0) { _sqlite3VdbeMemSetZeroBlob(tls, pOut, (*TOp)(unsafe.Pointer(pOp)).Fp1) if _sqlite3VdbeMemExpandBlob(tls, pOut) != 0 { goto no_mem } } else { _sqlite3VdbeMemSetStr(tls, pOut, *(*uintptr)(unsafe.Pointer(pOp + 16)), int64((*TOp)(unsafe.Pointer(pOp)).Fp1), uint8(0), uintptr(0)) } (*TMem)(unsafe.Pointer(pOut)).Fenc = encoding goto _187 /* Opcode: Variable P1 P2 * P4 * ** Synopsis: r[P2]=parameter(P1,P4) ** ** Transfer the values of bound parameter P1 into register P2 ** ** If the parameter is named, then its name appears in P4. ** The P4 value is used by sqlite3_bind_parameter_name(). */ _19: ; /* Value being transferred */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pVar = (*TVdbe)(unsafe.Pointer(p)).FaVar + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1-int32(1))*40 if _sqlite3VdbeMemTooBig(tls, pVar) != 0 { goto too_big } pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 { _sqlite3VdbeMemSetNull(tls, pOut) } libc.Xmemcpy(tls, pOut, pVar, uint32(libc.UintptrFromInt32(0)+20)) p192 = pOut + 16 *(*Tu16)(unsafe.Pointer(p192)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p192))) & ^(libc.Int32FromInt32(MEM_Dyn) | libc.Int32FromInt32(MEM_Ephem))) p193 = pOut + 16 *(*Tu16)(unsafe.Pointer(p193)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p193))) | (libc.Int32FromInt32(MEM_Static) | libc.Int32FromInt32(MEM_FromBind))) goto _187 /* Opcode: Move P1 P2 P3 * * ** Synopsis: r[P2@P3]=r[P1@P3] ** ** Move the P3 values in register P1..P1+P3-1 over into ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are ** left holding a NULL. It is an error for register ranges ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error ** for P3 to be less than 1. */ _20: ; /* Register to copy to */ n = (*TOp)(unsafe.Pointer(pOp)).Fp3 p1 = (*TOp)(unsafe.Pointer(pOp)).Fp1 p2 = (*TOp)(unsafe.Pointer(pOp)).Fp2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pIn1 = aMem + uintptr(p1)*40 pOut = aMem + uintptr(p2)*40 for { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeMemMove(tls, pOut, pIn1) if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&int32(MEM_Ephem) != 0 && _sqlite3VdbeMemMakeWriteable(tls, pOut) != 0 { goto no_mem } pIn1 += 40 pOut += 40 goto _195 _195: ; n-- v194 = n if !(v194 != 0) { break } } goto _187 /* Opcode: Copy P1 P2 P3 * P5 ** Synopsis: r[P2@P3+1]=r[P1@P3+1] ** ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. ** ** If the 0x0002 bit of P5 is set then also clear the MEM_Subtype flag in the ** destination. The 0x0001 bit of P5 indicates that this Copy opcode cannot ** be merged. The 0x0001 bit is used by the query planner and does not ** come into play during query execution. ** ** This instruction makes a deep copy of the value. A duplicate ** is made of any string or blob constant. See also OP_SCopy. */ _21: ; n1 = (*TOp)(unsafe.Pointer(pOp)).Fp3 pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 _ = libc.Int32FromInt32(0) for int32(1) != 0 { _sqlite3VdbeMemShallowCopy(tls, pOut, pIn1, int32(MEM_Ephem)) if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&int32(MEM_Ephem) != 0 && _sqlite3VdbeMemMakeWriteable(tls, pOut) != 0 { goto no_mem } if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&int32(MEM_Subtype) != 0 && int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(0x0002) != 0 { p196 = pOut + 16 *(*Tu16)(unsafe.Pointer(p196)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p196))) & ^libc.Int32FromInt32(MEM_Subtype)) } v197 = n1 n1-- if v197 == 0 { break } pOut += 40 pIn1 += 40 } goto _187 /* Opcode: SCopy P1 P2 * * * ** Synopsis: r[P2]=r[P1] ** ** Make a shallow copy of register P1 into register P2. ** ** This instruction makes a shallow copy of the value. If the value ** is a string or blob, then the copy is only a pointer to the ** original and hence if the original changes so will the copy. ** Worse, if the original is deallocated, the copy becomes invalid. ** Thus the program must guarantee that the original will not change ** during the lifetime of the copy. Use OP_Copy to make a complete ** copy. */ _22: ; /* out2 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 _ = libc.Int32FromInt32(0) _sqlite3VdbeMemShallowCopy(tls, pOut, pIn1, int32(MEM_Ephem)) goto _187 /* Opcode: IntCopy P1 P2 * * * ** Synopsis: r[P2]=r[P1] ** ** Transfer the integer value held in register P1 into register P2. ** ** This is an optimized version of SCopy that works only for integer ** values. */ _23: ; /* out2 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 _ = libc.Int32FromInt32(0) pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 _sqlite3VdbeMemSetInt64(tls, pOut, *(*Ti64)(unsafe.Pointer(pIn1))) goto _187 /* Opcode: FkCheck * * * * * ** ** Halt with an SQLITE_CONSTRAINT error if there are any unresolved ** foreign key constraint violations. If there are no foreign key ** constraint violations, this is a no-op. ** ** FK constraint violations are also checked when the prepared statement ** exits. This opcode is used to raise foreign key constraint errors prior ** to returning results such as a row change count or the result of a ** RETURNING clause. */ _24: ; v198 = _sqlite3VdbeCheckFk(tls, p, 0) rc = v198 if v198 != SQLITE_OK { goto abort_due_to_error } goto _187 /* Opcode: ResultRow P1 P2 * * * ** Synopsis: output=r[P1@P2] ** ** The registers P1 through P1+P2-1 contain a single row of ** results. This opcode causes the sqlite3_step() call to terminate ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt ** structure to provide access to the r(P1)..r(P1+P2-1) values as ** the result row. */ _25: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TVdbe)(unsafe.Pointer(p)).FcacheCtr = (*TVdbe)(unsafe.Pointer(p)).FcacheCtr + uint32(2) | uint32(1) (*TVdbe)(unsafe.Pointer(p)).FpResultRow = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto no_mem } if int32((*Tsqlite3)(unsafe.Pointer(db)).FmTrace)&int32(SQLITE_TRACE_ROW) != 0 { (*(*func(*libc.TLS, Tu32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{*(*uintptr)(unsafe.Pointer(&(*Tsqlite3)(unsafe.Pointer(db)).Ftrace))})))(tls, uint32(SQLITE_TRACE_ROW), (*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg, p, uintptr(0)) } (*TVdbe)(unsafe.Pointer(p)).Fpc = (int32(pOp)-int32(aOp))/20 + int32(1) rc = int32(SQLITE_ROW) goto vdbe_return /* Opcode: Concat P1 P2 P3 * * ** Synopsis: r[P3]=r[P2]+r[P1] ** ** Add the text in register P1 onto the end of the text in ** register P2 and store the result in register P3. ** If either the P1 or P2 text are NULL then store NULL in P3. ** ** P3 = P2 || P1 ** ** It is illegal for P1 and P3 to be the same register. Sometimes, ** if P3 is the same register as P2, the implementation is able ** to avoid a memcpy(). */ _26: ; /* Initial flags for P2 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 _ = libc.Int32FromInt32(0) flags1 = (*TMem)(unsafe.Pointer(pIn1)).Fflags if (int32(flags1)|int32((*TMem)(unsafe.Pointer(pIn2)).Fflags))&int32(MEM_Null) != 0 { _sqlite3VdbeMemSetNull(tls, pOut) goto _187 } if int32(flags1)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) == 0 { if _sqlite3VdbeMemStringify(tls, pIn1, encoding, uint8(0)) != 0 { goto no_mem } flags1 = uint16(int32((*TMem)(unsafe.Pointer(pIn1)).Fflags) & ^libc.Int32FromInt32(MEM_Str)) } else { if int32(flags1)&int32(MEM_Zero) != 0 { if _sqlite3VdbeMemExpandBlob(tls, pIn1) != 0 { goto no_mem } flags1 = uint16(int32((*TMem)(unsafe.Pointer(pIn1)).Fflags) & ^libc.Int32FromInt32(MEM_Str)) } } flags2 = (*TMem)(unsafe.Pointer(pIn2)).Fflags if int32(flags2)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) == 0 { if _sqlite3VdbeMemStringify(tls, pIn2, encoding, uint8(0)) != 0 { goto no_mem } flags2 = uint16(int32((*TMem)(unsafe.Pointer(pIn2)).Fflags) & ^libc.Int32FromInt32(MEM_Str)) } else { if int32(flags2)&int32(MEM_Zero) != 0 { if _sqlite3VdbeMemExpandBlob(tls, pIn2) != 0 { goto no_mem } flags2 = uint16(int32((*TMem)(unsafe.Pointer(pIn2)).Fflags) & ^libc.Int32FromInt32(MEM_Str)) } } nByte = int64((*TMem)(unsafe.Pointer(pIn1)).Fn + (*TMem)(unsafe.Pointer(pIn2)).Fn) if nByte > int64(*(*int32)(unsafe.Pointer(db + 120))) { goto too_big } if _sqlite3VdbeMemGrow(tls, pOut, int32(nByte)+int32(2), libc.BoolInt32(pOut == pIn2)) != 0 { goto no_mem } (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Str)) if pOut != pIn2 { libc.Xmemcpy(tls, (*TMem)(unsafe.Pointer(pOut)).Fz, (*TMem)(unsafe.Pointer(pIn2)).Fz, uint32((*TMem)(unsafe.Pointer(pIn2)).Fn)) _ = libc.Int32FromInt32(0) (*TMem)(unsafe.Pointer(pIn2)).Fflags = flags2 } libc.Xmemcpy(tls, (*TMem)(unsafe.Pointer(pOut)).Fz+uintptr((*TMem)(unsafe.Pointer(pIn2)).Fn), (*TMem)(unsafe.Pointer(pIn1)).Fz, uint32((*TMem)(unsafe.Pointer(pIn1)).Fn)) _ = libc.Int32FromInt32(0) (*TMem)(unsafe.Pointer(pIn1)).Fflags = flags1 if int32(encoding) > int32(SQLITE_UTF8) { nByte &= int64(^libc.Int32FromInt32(1)) } *(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pOut)).Fz + uintptr(nByte))) = uint8(0) *(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pOut)).Fz + uintptr(nByte+int64(1)))) = uint8(0) p199 = pOut + 16 *(*Tu16)(unsafe.Pointer(p199)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p199))) | libc.Int32FromInt32(MEM_Term)) (*TMem)(unsafe.Pointer(pOut)).Fn = int32(nByte) (*TMem)(unsafe.Pointer(pOut)).Fenc = encoding goto _187 /* Opcode: Add P1 P2 P3 * * ** Synopsis: r[P3]=r[P1]+r[P2] ** ** Add the value in register P1 to the value in register P2 ** and store the result in register P3. ** If either input is NULL, the result is NULL. */ /* Opcode: Multiply P1 P2 P3 * * ** Synopsis: r[P3]=r[P1]*r[P2] ** ** ** Multiply the value in register P1 by the value in register P2 ** and store the result in register P3. ** If either input is NULL, the result is NULL. */ /* Opcode: Subtract P1 P2 P3 * * ** Synopsis: r[P3]=r[P2]-r[P1] ** ** Subtract the value in register P1 from the value in register P2 ** and store the result in register P3. ** If either input is NULL, the result is NULL. */ /* Opcode: Divide P1 P2 P3 * * ** Synopsis: r[P3]=r[P2]/r[P1] ** ** Divide the value in register P1 by the value in register P2 ** and store the result in register P3 (P3=P2/P1). If the value in ** register P1 is zero, then the result is NULL. If either input is ** NULL, the result is NULL. */ /* Opcode: Remainder P1 P2 P3 * * ** Synopsis: r[P3]=r[P2]%r[P1] ** ** Compute the remainder after integer register P2 is divided by ** register P1 and store the result in register P3. ** If the value in register P1 is zero the result is NULL. ** If either operand is NULL, the result is NULL. */ _31: ; /* same as TK_PLUS, in1, in2, out3 */ _30: ; /* same as TK_MINUS, in1, in2, out3 */ _29: ; /* same as TK_STAR, in1, in2, out3 */ _28: ; /* same as TK_SLASH, in1, in2, out3 */ _27: ; /* Real value of right operand */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 type1 = (*TMem)(unsafe.Pointer(pIn1)).Fflags pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 type2 = (*TMem)(unsafe.Pointer(pIn2)).Fflags pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 if !(int32(type1)&int32(type2)&int32(MEM_Int) != 0) { goto _200 } goto int_math int_math: ; iA = *(*Ti64)(unsafe.Pointer(pIn1)) *(*Ti64)(unsafe.Pointer(bp)) = *(*Ti64)(unsafe.Pointer(pIn2)) switch int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) { case int32(OP_Add): if _sqlite3AddInt64(tls, bp, iA) != 0 { goto fp_math } case int32(OP_Subtract): if _sqlite3SubInt64(tls, bp, iA) != 0 { goto fp_math } case int32(OP_Multiply): if _sqlite3MulInt64(tls, bp, iA) != 0 { goto fp_math } case int32(OP_Divide): if iA == 0 { goto arithmetic_result_is_null } if iA == int64(-int32(1)) && *(*Ti64)(unsafe.Pointer(bp)) == int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<>r[P1] ** ** Shift the integer value in register P2 to the right by the ** number of bits specified by the integer in register P1. ** Store the result in register P3. ** If either input is NULL, the result is NULL. */ _36: ; /* same as TK_BITAND, in1, in2, out3 */ _35: ; /* same as TK_BITOR, in1, in2, out3 */ _34: ; /* same as TK_LSHIFT, in1, in2, out3 */ _33: ; pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 if (int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)|int32((*TMem)(unsafe.Pointer(pIn2)).Fflags))&int32(MEM_Null) != 0 { _sqlite3VdbeMemSetNull(tls, pOut) goto _187 } *(*Ti64)(unsafe.Pointer(bp + 8)) = _sqlite3VdbeIntValue(tls, pIn2) iB1 = _sqlite3VdbeIntValue(tls, pIn1) op = (*TOp)(unsafe.Pointer(pOp)).Fopcode if int32(op) == int32(OP_BitAnd) { *(*Ti64)(unsafe.Pointer(bp + 8)) &= iB1 } else { if int32(op) == int32(OP_BitOr) { *(*Ti64)(unsafe.Pointer(bp + 8)) |= iB1 } else { if iB1 != 0 { _ = libc.Int32FromInt32(0) /* If shifting by a negative amount, shift in the other direction */ if iB1 < 0 { _ = libc.Int32FromInt32(0) op = uint8(libc.Int32FromInt32(2)*libc.Int32FromInt32(OP_ShiftLeft) + libc.Int32FromInt32(1) - int32(op)) if iB1 > int64(-libc.Int32FromInt32(64)) { v204 = -iB1 } else { v204 = int64(64) } iB1 = v204 } if iB1 >= int64(64) { if *(*Ti64)(unsafe.Pointer(bp + 8)) >= 0 || int32(op) == int32(OP_ShiftLeft) { v205 = 0 } else { v205 = -int32(1) } *(*Ti64)(unsafe.Pointer(bp + 8)) = int64(v205) } else { libc.Xmemcpy(tls, bp+16, bp+8, uint32(8)) if int32(op) == int32(OP_ShiftLeft) { *(*Tu64)(unsafe.Pointer(bp + 16)) <<= uint64(iB1) } else { *(*Tu64)(unsafe.Pointer(bp + 16)) >>= uint64(iB1) /* Sign-extend on a right shift of a negative number */ if *(*Ti64)(unsafe.Pointer(bp + 8)) < 0 { *(*Tu64)(unsafe.Pointer(bp + 16)) |= (libc.Uint64FromUint32(0xffffffff)< **
  • P2=='A' → BLOB **
  • P2=='B' → TEXT **
  • P2=='C' → NUMERIC **
  • P2=='D' → INTEGER **
  • P2=='E' → REAL ** ** ** A NULL value is not changed by this routine. It remains NULL. */ _40: ; /* in1 */ _ = libc.Int32FromInt32(0) pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Zero) != 0 { v206 = _sqlite3VdbeMemExpandBlob(tls, pIn1) } else { v206 = 0 } rc = v206 if rc != 0 { goto abort_due_to_error } rc = _sqlite3VdbeMemCast(tls, pIn1, uint8((*TOp)(unsafe.Pointer(pOp)).Fp2), encoding) if rc != 0 { goto abort_due_to_error } goto _187 /* Opcode: Eq P1 P2 P3 P4 P5 ** Synopsis: IF r[P3]==r[P1] ** ** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then ** jump to address P2. ** ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made ** to coerce both inputs according to this affinity before the ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric ** affinity is used. Note that the affinity conversions are stored ** back into the input registers P1 and P3. So this opcode can cause ** persistent changes to registers P1 and P3. ** ** Once any conversions have taken place, and neither value is NULL, ** the values are compared. If both values are blobs then memcmp() is ** used to determine the results of the comparison. If both values ** are text, then the appropriate collating function specified in ** P4 is used to do the comparison. If P4 is not specified then ** memcmp() is used to compare text string. If both values are ** numeric, then a numeric comparison is used. If the two values ** are of different types, then numbers are considered less than ** strings and strings are considered less than blobs. ** ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either ** true or false and is never NULL. If both operands are NULL then the result ** of comparison is true. If either operand is NULL then the result is false. ** If neither operand is NULL the result is the same as it would be if ** the SQLITE_NULLEQ flag were omitted from P5. ** ** This opcode saves the result of comparison for use by the new ** OP_Jump opcode. */ /* Opcode: Ne P1 P2 P3 P4 P5 ** Synopsis: IF r[P3]!=r[P1] ** ** This works just like the Eq opcode except that the jump is taken if ** the operands in registers P1 and P3 are not equal. See the Eq opcode for ** additional information. */ /* Opcode: Lt P1 P2 P3 P4 P5 ** Synopsis: IF r[P3]r[P1] ** ** This works just like the Lt opcode except that the jump is taken if ** the content of register P3 is greater than the content of ** register P1. See the Lt opcode for additional information. */ /* Opcode: Ge P1 P2 P3 P4 P5 ** Synopsis: IF r[P3]>=r[P1] ** ** This works just like the Lt opcode except that the jump is taken if ** the content of register P3 is greater than or equal to the content of ** register P1. See the Lt opcode for additional information. */ _46: ; /* same as TK_EQ, jump, in1, in3 */ _45: ; /* same as TK_NE, jump, in1, in3 */ _44: ; /* same as TK_LT, jump, in1, in3 */ _43: ; /* same as TK_LE, jump, in1, in3 */ _42: ; /* same as TK_GT, jump, in1, in3 */ _41: ; /* Copy of initial value of pIn3->flags */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 flags11 = (*TMem)(unsafe.Pointer(pIn1)).Fflags flags3 = (*TMem)(unsafe.Pointer(pIn3)).Fflags if int32(flags11)&int32(flags3)&int32(MEM_Int) != 0 { /* Common case of comparison of two integers */ if *(*Ti64)(unsafe.Pointer(pIn3)) > *(*Ti64)(unsafe.Pointer(pIn1)) { if *(*uint8)(unsafe.Pointer(_sqlite3aGTb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fopcode))) != 0 { goto jump_to_p2 } iCompare = +libc.Int32FromInt32(1) } else { if *(*Ti64)(unsafe.Pointer(pIn3)) < *(*Ti64)(unsafe.Pointer(pIn1)) { if *(*uint8)(unsafe.Pointer(_sqlite3aLTb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fopcode))) != 0 { goto jump_to_p2 } iCompare = -int32(1) } else { if *(*uint8)(unsafe.Pointer(_sqlite3aEQb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fopcode))) != 0 { goto jump_to_p2 } iCompare = 0 } } goto _187 } if (int32(flags11)|int32(flags3))&int32(MEM_Null) != 0 { /* One or both operands are NULL */ if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(SQLITE_NULLEQ) != 0 { /* If SQLITE_NULLEQ is set (which will only happen if the operator is ** OP_Eq or OP_Ne) then take the jump or not depending on whether ** or not both operands are null. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(flags11)&int32(flags3)&int32(MEM_Null) != 0 && int32(flags3)&int32(MEM_Cleared) == 0 { res = 0 /* Operands are equal */ } else { if int32(flags3)&int32(MEM_Null) != 0 { v207 = -int32(1) } else { v207 = +libc.Int32FromInt32(1) } res = v207 /* Operands are not equal */ } } else { /* SQLITE_NULLEQ is clear and at least one operand is NULL, ** then the result is always NULL. ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. */ if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(SQLITE_JUMPIFNULL) != 0 { goto jump_to_p2 } iCompare = int32(1) /* Operands are not equal */ goto _187 } } else { /* Neither operand is NULL and we couldn't do the special high-speed ** integer comparison case. So do a general-case comparison. */ affinity = uint8(int32((*TOp)(unsafe.Pointer(pOp)).Fp5) & int32(SQLITE_AFF_MASK)) if int32(affinity) >= int32(SQLITE_AFF_NUMERIC) { if (int32(flags11)|int32(flags3))&int32(MEM_Str) != 0 { if int32(flags11)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)|libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_Str)) == int32(MEM_Str) { _applyNumericAffinity(tls, pIn1, 0) _ = libc.Int32FromInt32(0) flags3 = (*TMem)(unsafe.Pointer(pIn3)).Fflags } if int32(flags3)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)|libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_Str)) == int32(MEM_Str) { _applyNumericAffinity(tls, pIn3, 0) } } } else { if int32(affinity) == int32(SQLITE_AFF_TEXT) && (int32(flags11)|int32(flags3))&int32(MEM_Str) != 0 { if int32(flags11)&int32(MEM_Str) != 0 { p208 = pIn1 + 16 *(*Tu16)(unsafe.Pointer(p208)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p208))) & ^(libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_Real) | libc.Int32FromInt32(MEM_IntReal))) } else { if int32(flags11)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)) != 0 { _sqlite3VdbeMemStringify(tls, pIn1, encoding, uint8(1)) flags11 = uint16(int32((*TMem)(unsafe.Pointer(pIn1)).Fflags) & ^libc.Int32FromInt32(MEM_TypeMask) | int32(flags11)&int32(MEM_TypeMask)) if pIn1 == pIn3 { flags3 = uint16(int32(flags11) | int32(MEM_Str)) } } } if int32(flags3)&int32(MEM_Str) != 0 { p209 = pIn3 + 16 *(*Tu16)(unsafe.Pointer(p209)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p209))) & ^(libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_Real) | libc.Int32FromInt32(MEM_IntReal))) } else { if int32(flags3)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)) != 0 { _sqlite3VdbeMemStringify(tls, pIn3, encoding, uint8(1)) flags3 = uint16(int32((*TMem)(unsafe.Pointer(pIn3)).Fflags) & ^libc.Int32FromInt32(MEM_TypeMask) | int32(flags3)&int32(MEM_TypeMask)) } } } } _ = libc.Int32FromInt32(0) res = _sqlite3MemCompare(tls, pIn3, pIn1, *(*uintptr)(unsafe.Pointer(pOp + 16))) } /* At this point, res is negative, zero, or positive if reg[P1] is ** less than, equal to, or greater than reg[P3], respectively. Compute ** the answer to this operator in res2, depending on what the comparison ** operator actually is. The next block of code depends on the fact ** that the 6 comparison operators are consecutive integers in this ** order: NE, EQ, GT, LE, LT, GE */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if res < 0 { res21 = int32(*(*uint8)(unsafe.Pointer(_sqlite3aLTb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fopcode)))) } else { if res == 0 { res21 = int32(*(*uint8)(unsafe.Pointer(_sqlite3aEQb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fopcode)))) } else { res21 = int32(*(*uint8)(unsafe.Pointer(_sqlite3aGTb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fopcode)))) } } iCompare = res /* Undo any changes made by applyAffinity() to the input registers. */ _ = libc.Int32FromInt32(0) (*TMem)(unsafe.Pointer(pIn3)).Fflags = flags3 _ = libc.Int32FromInt32(0) (*TMem)(unsafe.Pointer(pIn1)).Fflags = flags11 if res21 != 0 { goto jump_to_p2 } goto _187 /* Opcode: ElseEq * P2 * * * ** ** This opcode must follow an OP_Lt or OP_Gt comparison operator. There ** can be zero or more OP_ReleaseReg opcodes intervening, but no other ** opcodes are allowed to occur between this instruction and the previous ** OP_Lt or OP_Gt. ** ** If the result of an OP_Eq comparison on the same two operands as ** the prior OP_Lt or OP_Gt would have been true, then jump to P2. If ** the result of an OP_Eq comparison on the two previous operands ** would have been false or NULL, then fall through. */ _47: ; /* same as TK_ESCAPE, jump */ _ = libc.Int32FromInt32(0) if iCompare == 0 { goto jump_to_p2 } goto _187 /* Opcode: Permutation * * * P4 * ** ** Set the permutation used by the OP_Compare operator in the next ** instruction. The permutation is stored in the P4 operand. ** ** The permutation is only valid for the next opcode which must be ** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5. ** ** The first integer in the P4 integer array is the length of the array ** and does not become part of the permutation. */ _48: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) goto _187 /* Opcode: Compare P1 P2 P3 P4 P5 ** Synopsis: r[P1@P3] <-> r[P2@P3] ** ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of ** the comparison for use by the next OP_Jump instruct. ** ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is ** determined by the most recent OP_Permutation operator. If the ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential ** order. ** ** P4 is a KeyInfo structure that defines collating sequences and sort ** orders for the comparison. The permutation applies to registers ** only. The KeyInfo elements are used sequentially. ** ** The comparison is a sort comparison, so NULLs compare equal, ** NULLs are less than numbers, numbers are less than strings, ** and strings are less than blobs. ** ** This opcode must be immediately followed by an OP_Jump opcode. */ _49: ; /* The permutation */ if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_PERMUTE) == 0 { aPermute = uintptr(0) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) aPermute = *(*uintptr)(unsafe.Pointer(pOp + uintptr(-libc.Int32FromInt32(1))*20 + 16)) + uintptr(1)*4 _ = libc.Int32FromInt32(0) } n2 = (*TOp)(unsafe.Pointer(pOp)).Fp3 pKeyInfo = *(*uintptr)(unsafe.Pointer(pOp + 16)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) p11 = (*TOp)(unsafe.Pointer(pOp)).Fp1 p21 = (*TOp)(unsafe.Pointer(pOp)).Fp2 i = 0 for { if !(i < n2) { break } if aPermute != 0 { v211 = *(*Tu32)(unsafe.Pointer(aPermute + uintptr(i)*4)) } else { v211 = uint32(i) } idx = v211 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pColl = *(*uintptr)(unsafe.Pointer(pKeyInfo + 20 + uintptr(i)*4)) bRev = int32(*(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags + uintptr(i)))) & int32(KEYINFO_ORDER_DESC) iCompare = _sqlite3MemCompare(tls, aMem+uintptr(uint32(p11)+idx)*40, aMem+uintptr(uint32(p21)+idx)*40, pColl) if iCompare != 0 { if int32(*(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags + uintptr(i))))&int32(KEYINFO_ORDER_BIGNULL) != 0 && (int32((*(*TMem)(unsafe.Pointer(aMem + uintptr(uint32(p11)+idx)*40))).Fflags)&int32(MEM_Null) != 0 || int32((*(*TMem)(unsafe.Pointer(aMem + uintptr(uint32(p21)+idx)*40))).Fflags)&int32(MEM_Null) != 0) { iCompare = -iCompare } if bRev != 0 { iCompare = -iCompare } break } goto _210 _210: ; i++ } _ = libc.Int32FromInt32(0) goto _187 /* Opcode: Jump P1 P2 P3 * * ** ** Jump to the instruction at address P1, P2, or P3 depending on whether ** in the most recent OP_Compare instruction the P1 vector was less than, ** equal to, or greater than the P2 vector, respectively. ** ** This opcode must immediately follow an OP_Compare opcode. */ _50: ; /* jump */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if iCompare < 0 { pOp = aOp + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1-int32(1))*20 } else { if iCompare == 0 { pOp = aOp + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2-int32(1))*20 } else { pOp = aOp + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3-int32(1))*20 } } goto _187 /* Opcode: And P1 P2 P3 * * ** Synopsis: r[P3]=(r[P1] && r[P2]) ** ** Take the logical AND of the values in registers P1 and P2 and ** write the result into register P3. ** ** If either P1 or P2 is 0 (false) then the result is 0 even if ** the other input is NULL. A NULL and true or two NULLs give ** a NULL output. */ /* Opcode: Or P1 P2 P3 * * ** Synopsis: r[P3]=(r[P1] || r[P2]) ** ** Take the logical OR of the values in register P1 and P2 and ** store the answer in register P3. ** ** If either P1 or P2 is nonzero (true) then the result is 1 (true) ** even if the other input is NULL. A NULL and false or two NULLs ** give a NULL output. */ _52: ; /* same as TK_AND, in1, in2, out3 */ _51: ; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ v11 = _sqlite3VdbeBooleanValue(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40, int32(2)) v21 = _sqlite3VdbeBooleanValue(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40, int32(2)) if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_And) { v11 = int32(_and_logic[v11*int32(3)+v21]) } else { v11 = int32(_or_logic[v11*int32(3)+v21]) } pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 if v11 == int32(2) { (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Null)) } else { *(*Ti64)(unsafe.Pointer(pOut)) = int64(v11) (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Int)) } goto _187 /* Opcode: IsTrue P1 P2 P3 P4 * ** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 ** ** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and ** IS NOT FALSE operators. ** ** Interpret the value in register P1 as a boolean value. Store that ** boolean (a 0 or 1) in register P2. Or if the value in register P1 is ** NULL, then the P3 is stored in register P2. Invert the answer if P4 ** is 1. ** ** The logic is summarized like this: ** **
      **
    • If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE **
    • If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE **
    • If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE **
    • If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE **
    */ _53: ; /* in1, out2 */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeMemSetInt64(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40, int64(_sqlite3VdbeBooleanValue(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40, (*TOp)(unsafe.Pointer(pOp)).Fp3)^*(*int32)(unsafe.Pointer(pOp + 16)))) goto _187 /* Opcode: Not P1 P2 * * * ** Synopsis: r[P2]= !r[P1] ** ** Interpret the value in register P1 as a boolean value. Store the ** boolean complement in register P2. If the value in register P1 is ** NULL, then a NULL is stored in P2. */ _54: ; /* same as TK_NOT, in1, out2 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Null) == 0 { _sqlite3VdbeMemSetInt64(tls, pOut, libc.BoolInt64(!(_sqlite3VdbeBooleanValue(tls, pIn1, 0) != 0))) } else { _sqlite3VdbeMemSetNull(tls, pOut) } goto _187 /* Opcode: BitNot P1 P2 * * * ** Synopsis: r[P2]= ~r[P1] ** ** Interpret the content of register P1 as an integer. Store the ** ones-complement of the P1 value into register P2. If P1 holds ** a NULL then store a NULL in P2. */ _55: ; /* same as TK_BITNOT, in1, out2 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 _sqlite3VdbeMemSetNull(tls, pOut) if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Null) == 0 { (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Int) *(*Ti64)(unsafe.Pointer(pOut)) = ^_sqlite3VdbeIntValue(tls, pIn1) } goto _187 /* Opcode: Once P1 P2 * * * ** ** Fall through to the next instruction the first time this opcode is ** encountered on each invocation of the byte-code program. Jump to P2 ** on the second and all subsequent encounters during the same invocation. ** ** Top-level programs determine first invocation by comparing the P1 ** operand against the P1 operand on the OP_Init opcode at the beginning ** of the program. If the P1 values differ, then fall through and make ** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are ** the same then take the jump. ** ** For subprograms, there is a bitmask in the VdbeFrame that determines ** whether or not the jump should be taken. The bitmask is necessary ** because the self-altering code trick does not work for recursive ** triggers. */ _56: ; /* Address of this instruction */ _ = libc.Int32FromInt32(0) if (*TVdbe)(unsafe.Pointer(p)).FpFrame != 0 { iAddr = uint32((int32(pOp) - int32((*TVdbe)(unsafe.Pointer(p)).FaOp)) / 20) if int32(*(*Tu8)(unsafe.Pointer((*TVdbeFrame)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FpFrame)).FaOnce + uintptr(iAddr/uint32(8)))))&(int32(1)<<(iAddr&uint32(7))) != 0 { goto jump_to_p2 } p212 = (*TVdbeFrame)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FpFrame)).FaOnce + uintptr(iAddr/uint32(8)) *(*Tu8)(unsafe.Pointer(p212)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p212))) | libc.Int32FromInt32(1)<<(iAddr&libc.Uint32FromInt32(7))) } else { if (*(*TOp)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FaOp))).Fp1 == (*TOp)(unsafe.Pointer(pOp)).Fp1 { goto jump_to_p2 } } (*TOp)(unsafe.Pointer(pOp)).Fp1 = (*(*TOp)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FaOp))).Fp1 goto _187 /* Opcode: If P1 P2 P3 * * ** ** Jump to P2 if the value in register P1 is true. The value ** is considered true if it is numeric and non-zero. If the value ** in P1 is NULL then take the jump if and only if P3 is non-zero. */ _57: ; c = _sqlite3VdbeBooleanValue(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40, (*TOp)(unsafe.Pointer(pOp)).Fp3) if c != 0 { goto jump_to_p2 } goto _187 /* Opcode: IfNot P1 P2 P3 * * ** ** Jump to P2 if the value in register P1 is False. The value ** is considered false if it has a numeric value of zero. If the value ** in P1 is NULL then take the jump if and only if P3 is non-zero. */ _58: ; c1 = libc.BoolInt32(!(_sqlite3VdbeBooleanValue(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40, libc.BoolInt32(!((*TOp)(unsafe.Pointer(pOp)).Fp3 != 0))) != 0)) if c1 != 0 { goto jump_to_p2 } goto _187 /* Opcode: IsNull P1 P2 * * * ** Synopsis: if r[P1]==NULL goto P2 ** ** Jump to P2 if the value in register P1 is NULL. */ _59: ; /* same as TK_ISNULL, jump, in1 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Null) != 0 { goto jump_to_p2 } goto _187 /* Opcode: IsType P1 P2 P3 P4 P5 ** Synopsis: if typeof(P1.P3) in P5 goto P2 ** ** Jump to P2 if the type of a column in a btree is one of the types specified ** by the P5 bitmask. ** ** P1 is normally a cursor on a btree for which the row decode cache is ** valid through at least column P3. In other words, there should have been ** a prior OP_Column for column P3 or greater. If the cursor is not valid, ** then this opcode might give spurious results. ** The the btree row has fewer than P3 columns, then use P4 as the ** datatype. ** ** If P1 is -1, then P3 is a register number and the datatype is taken ** from the value in that register. ** ** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant ** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04. ** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10. ** ** WARNING: This opcode does not reliably distinguish between NULL and REAL ** when P1>=0. If the database contains a NaN value, this opcode will think ** that the datatype is REAL when it should be NULL. When P1<0 and the value ** is already stored in register P3, then this opcode does reliably ** distinguish between NULL and REAL. The problem only arises then P1>=0. ** ** Take the jump to address P2 if and only if the datatype of the ** value determined by P1 and P3 corresponds to one of the bits in the ** P5 bitmask. ** */ _60: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TOp)(unsafe.Pointer(pOp)).Fp1 >= 0 { pC = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TOp)(unsafe.Pointer(pOp)).Fp3 < int32((*TVdbeCursor)(unsafe.Pointer(pC)).FnHdrParsed) { serialType = *(*Tu32)(unsafe.Pointer(pC + 84 + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*4)) if serialType >= uint32(12) { if serialType&uint32(1) != 0 { typeMask = uint16(0x04) /* SQLITE_TEXT */ } else { typeMask = uint16(0x08) /* SQLITE_BLOB */ } } else { typeMask = uint16(_aMask[serialType]) } } else { typeMask = uint16(int32(1) << (*(*int32)(unsafe.Pointer(pOp + 16)) - int32(1))) } } else { _ = libc.Int32FromInt32(0) typeMask = uint16(int32(1) << (Xsqlite3_value_type(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40) - int32(1))) } if int32(typeMask)&int32((*TOp)(unsafe.Pointer(pOp)).Fp5) != 0 { goto jump_to_p2 } goto _187 /* Opcode: ZeroOrNull P1 P2 P3 * * ** Synopsis: r[P2] = 0 OR NULL ** ** If both registers P1 and P3 are NOT NULL, then store a zero in ** register P2. If either registers P1 or P3 are NULL then put ** a NULL in register P2. */ _61: ; /* in1, in2, out2, in3 */ if int32((*(*TMem)(unsafe.Pointer(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40))).Fflags)&int32(MEM_Null) != 0 || int32((*(*TMem)(unsafe.Pointer(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40))).Fflags)&int32(MEM_Null) != 0 { _sqlite3VdbeMemSetNull(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40) } else { _sqlite3VdbeMemSetInt64(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40, 0) } goto _187 /* Opcode: NotNull P1 P2 * * * ** Synopsis: if r[P1]!=NULL goto P2 ** ** Jump to P2 if the value in register P1 is not NULL. */ _62: ; /* same as TK_NOTNULL, jump, in1 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Null) == 0 { goto jump_to_p2 } goto _187 /* Opcode: IfNullRow P1 P2 P3 * * ** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2 ** ** Check the cursor P1 to see if it is currently pointing at a NULL row. ** If it is, then set register P3 to NULL and jump immediately to P2. ** If P1 is not on a NULL row, then fall through without making any ** changes. ** ** If P1 is not an open cursor, then this opcode is a no-op. */ _63: ; _ = libc.Int32FromInt32(0) pC1 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) if pC1 != 0 && (*TVdbeCursor)(unsafe.Pointer(pC1)).FnullRow != 0 { _sqlite3VdbeMemSetNull(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40) goto jump_to_p2 } goto _187 /* Opcode: Offset P1 P2 P3 * * ** Synopsis: r[P3] = sqlite_offset(P1) ** ** Store in register r[P3] the byte offset into the database file that is the ** start of the payload for the record at which that cursor P1 is currently ** pointing. ** ** P2 is the column number for the argument to the sqlite_offset() function. ** This opcode does not use P2 itself, but the P2 value is used by the ** code generator. The P1, P2, and P3 operands to this opcode are the ** same as for OP_Column. ** ** This opcode is only available if SQLite is compiled with the ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option. */ _64: ; /* The VDBE cursor */ _ = libc.Int32FromInt32(0) pC2 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) pOut = (*TVdbe)(unsafe.Pointer(p)).FaMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 if pC2 == uintptr(0) || int32((*TVdbeCursor)(unsafe.Pointer(pC2)).FeCurType) != CURTYPE_BTREE { _sqlite3VdbeMemSetNull(tls, pOut) } else { if (*TVdbeCursor)(unsafe.Pointer(pC2)).FdeferredMoveto != 0 { rc = _sqlite3VdbeFinishMoveto(tls, pC2) if rc != 0 { goto abort_due_to_error } } if _sqlite3BtreeEof(tls, *(*uintptr)(unsafe.Pointer(pC2 + 36))) != 0 { _sqlite3VdbeMemSetNull(tls, pOut) } else { _sqlite3VdbeMemSetInt64(tls, pOut, _sqlite3BtreeOffset(tls, *(*uintptr)(unsafe.Pointer(pC2 + 36)))) } } goto _187 /* Opcode: Column P1 P2 P3 P4 P5 ** Synopsis: r[P3]=PX cursor P1 column P2 ** ** Interpret the data that cursor P1 points to as a structure built using ** the MakeRecord instruction. (See the MakeRecord opcode for additional ** information about the format of the data.) Extract the P2-th column ** from this record. If there are less than (P2+1) ** values in the record, extract a NULL. ** ** The value extracted is stored in register P3. ** ** If the record contains fewer than P2 fields, then extract a NULL. Or, ** if the P4 argument is a P4_MEM use the value of the P4 argument as ** the result. ** ** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed ** to only be used by the length() function or the equivalent. The content ** of large blobs is not loaded, thus saving CPU cycles. If the ** OPFLAG_TYPEOFARG bit is set then the result will only be used by the ** typeof() function or the IS NULL or IS NOT NULL operators or the ** equivalent. In this case, all content loading can be omitted. */ _65: ; /* PseudoTable input register */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pC3 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) p22 = uint32((*TOp)(unsafe.Pointer(pOp)).Fp2) goto op_column_restart op_column_restart: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) aOffset = (*TVdbeCursor)(unsafe.Pointer(pC3)).FaOffset _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TVdbeCursor)(unsafe.Pointer(pC3)).FcacheStatus != (*TVdbe)(unsafe.Pointer(p)).FcacheCtr { /*OPTIMIZATION-IF-FALSE*/ if (*TVdbeCursor)(unsafe.Pointer(pC3)).FnullRow != 0 { if int32((*TVdbeCursor)(unsafe.Pointer(pC3)).FeCurType) == int32(CURTYPE_PSEUDO) && (*TVdbeCursor)(unsafe.Pointer(pC3)).FseekResult > 0 { /* For the special case of as pseudo-cursor, the seekResult field ** identifies the register that holds the record */ pReg = aMem + uintptr((*TVdbeCursor)(unsafe.Pointer(pC3)).FseekResult)*40 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) v213 = uint32((*TMem)(unsafe.Pointer(pReg)).Fn) (*TVdbeCursor)(unsafe.Pointer(pC3)).FszRow = v213 (*TVdbeCursor)(unsafe.Pointer(pC3)).FpayloadSize = v213 (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow = (*TMem)(unsafe.Pointer(pReg)).Fz } else { pDest = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 _sqlite3VdbeMemSetNull(tls, pDest) goto op_column_out } } else { pCrsr = *(*uintptr)(unsafe.Pointer(pC3 + 36)) if (*TVdbeCursor)(unsafe.Pointer(pC3)).FdeferredMoveto != 0 { _ = libc.Int32FromInt32(0) if v215 = *(*uintptr)(unsafe.Pointer(pC3 + 12)) != 0; v215 { v214 = *(*Tu32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pC3 + 12)) + uintptr(uint32(1)+p22)*4)) iMap = v214 } if v215 && v214 > uint32(0) { pC3 = (*TVdbeCursor)(unsafe.Pointer(pC3)).FpAltCursor p22 = iMap - uint32(1) goto op_column_restart } rc = _sqlite3VdbeFinishMoveto(tls, pC3) if rc != 0 { goto abort_due_to_error } } else { if _sqlite3BtreeCursorHasMoved(tls, pCrsr) != 0 { rc = _sqlite3VdbeHandleMovedCursor(tls, pC3) if rc != 0 { goto abort_due_to_error } goto op_column_restart } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TVdbeCursor)(unsafe.Pointer(pC3)).FpayloadSize = _sqlite3BtreePayloadSize(tls, pCrsr) (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow = _sqlite3BtreePayloadFetch(tls, pCrsr, pC3+76) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Maximum page size is 64KiB */ } (*TVdbeCursor)(unsafe.Pointer(pC3)).FcacheStatus = (*TVdbe)(unsafe.Pointer(p)).FcacheCtr v216 = uint32(*(*Tu8)(unsafe.Pointer((*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow))) *(*Tu32)(unsafe.Pointer(aOffset)) = v216 if v216 < uint32(0x80) { (*TVdbeCursor)(unsafe.Pointer(pC3)).FiHdrOffset = uint32(1) } else { (*TVdbeCursor)(unsafe.Pointer(pC3)).FiHdrOffset = uint32(_sqlite3GetVarint32(tls, (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow, aOffset)) } (*TVdbeCursor)(unsafe.Pointer(pC3)).FnHdrParsed = uint16(0) if (*TVdbeCursor)(unsafe.Pointer(pC3)).FszRow < *(*Tu32)(unsafe.Pointer(aOffset)) { /*OPTIMIZATION-IF-FALSE*/ /* pC->aRow does not have to hold the entire row, but it does at least ** need to cover the header of the record. If pC->aRow does not contain ** the complete header, then set it to zero, forcing the header to be ** dynamically allocated. */ (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow = uintptr(0) (*TVdbeCursor)(unsafe.Pointer(pC3)).FszRow = uint32(0) /* Make sure a corrupt database has not given us an oversize header. ** Do this now to avoid an oversize memory allocation. ** ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte ** types use so much data space that there can only be 4096 and 32 of ** them, respectively. So the maximum header length results from a ** 3-byte type for each of the maximum of 32768 columns plus three ** extra bytes for the header length itself. 32768*3 + 3 = 98307. */ if *(*Tu32)(unsafe.Pointer(aOffset)) > uint32(98307) || *(*Tu32)(unsafe.Pointer(aOffset)) > (*TVdbeCursor)(unsafe.Pointer(pC3)).FpayloadSize { goto op_column_corrupt } } else { /* This is an optimization. By skipping over the first few tests ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a ** measurable performance gain. ** ** This branch is taken even if aOffset[0]==0. Such a record is never ** generated by SQLite, and could be considered corruption, but we ** accept it for historical reasons. When aOffset[0]==0, the code this ** branch jumps to reads past the end of the record, but never more ** than a few bytes. Even if the record occurs at the end of the page ** content area, the "page header" comes after the page content and so ** this overread is harmless. Similar overreads can occur for a corrupt ** database file. */ zData = (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow _ = libc.Int32FromInt32(0) /* Conditional skipped */ goto op_column_read_header } } else { if _sqlite3BtreeCursorHasMoved(tls, *(*uintptr)(unsafe.Pointer(pC3 + 36))) != 0 { rc = _sqlite3VdbeHandleMovedCursor(tls, pC3) if rc != 0 { goto abort_due_to_error } goto op_column_restart } } /* Make sure at least the first p2+1 entries of the header have been ** parsed and valid information is in aOffset[] and pC->aType[]. */ if !(uint32((*TVdbeCursor)(unsafe.Pointer(pC3)).FnHdrParsed) <= p22) { goto _217 } /* If there is more header available for parsing in the record, try ** to extract additional fields up through the p2+1-th field */ if !((*TVdbeCursor)(unsafe.Pointer(pC3)).FiHdrOffset < *(*Tu32)(unsafe.Pointer(aOffset))) { goto _219 } /* Make sure zData points to enough of the record to cover the header. */ if (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow == uintptr(0) { libc.Xmemset(tls, bp+24, 0, uint32(40)) rc = _sqlite3VdbeMemFromBtreeZeroOffset(tls, *(*uintptr)(unsafe.Pointer(pC3 + 36)), *(*Tu32)(unsafe.Pointer(aOffset)), bp+24) if rc != SQLITE_OK { goto abort_due_to_error } zData = (*(*TMem)(unsafe.Pointer(bp + 24))).Fz } else { zData = (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow } /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */ goto op_column_read_header op_column_read_header: ; i1 = int32((*TVdbeCursor)(unsafe.Pointer(pC3)).FnHdrParsed) offset64 = uint64(*(*Tu32)(unsafe.Pointer(aOffset + uintptr(i1)*4))) zHdr = zData + uintptr((*TVdbeCursor)(unsafe.Pointer(pC3)).FiHdrOffset) zEndHdr = zData + uintptr(*(*Tu32)(unsafe.Pointer(aOffset))) for cond := true; cond; cond = uint32(i1) <= p22 && zHdr < zEndHdr { v222 = uint32(*(*Tu8)(unsafe.Pointer(zHdr))) *(*Tu32)(unsafe.Pointer(bp + 64)) = v222 v221 = v222 *(*Tu32)(unsafe.Pointer(pC3 + 84 + uintptr(i1)*4)) = v221 if v221 < uint32(0x80) { zHdr++ offset64 += uint64(_sqlite3VdbeOneByteSerialTypeLen(tls, uint8(*(*Tu32)(unsafe.Pointer(bp + 64))))) } else { zHdr += uintptr(_sqlite3GetVarint32(tls, zHdr, bp+64)) *(*Tu32)(unsafe.Pointer(pC3 + 84 + uintptr(i1)*4)) = *(*Tu32)(unsafe.Pointer(bp + 64)) offset64 += uint64(_sqlite3VdbeSerialTypeLen(tls, *(*Tu32)(unsafe.Pointer(bp + 64)))) } i1++ v223 = i1 *(*Tu32)(unsafe.Pointer(aOffset + uintptr(v223)*4)) = uint32(offset64 & libc.Uint64FromUint32(0xffffffff)) } /* The record is corrupt if any of the following are true: ** (1) the bytes of the header extend past the declared header size ** (2) the entire header was used but not all data was used ** (3) the end of the data extends beyond the end of the record. */ if zHdr >= zEndHdr && (zHdr > zEndHdr || offset64 != uint64((*TVdbeCursor)(unsafe.Pointer(pC3)).FpayloadSize)) || offset64 > uint64((*TVdbeCursor)(unsafe.Pointer(pC3)).FpayloadSize) { if *(*Tu32)(unsafe.Pointer(aOffset)) == uint32(0) { i1 = 0 zHdr = zEndHdr } else { if (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow == uintptr(0) { _sqlite3VdbeMemRelease(tls, bp+24) } goto op_column_corrupt } } (*TVdbeCursor)(unsafe.Pointer(pC3)).FnHdrParsed = uint16(i1) (*TVdbeCursor)(unsafe.Pointer(pC3)).FiHdrOffset = uint32(int32(zHdr) - int32(zData)) if (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow == uintptr(0) { _sqlite3VdbeMemRelease(tls, bp+24) } goto _220 _219: ; *(*Tu32)(unsafe.Pointer(bp + 64)) = uint32(0) _220: ; /* If after trying to extract new entries from the header, nHdrParsed is ** still not up to p2, that means that the record has fewer than p2 ** columns. So the result will be either the default value or a NULL. */ if uint32((*TVdbeCursor)(unsafe.Pointer(pC3)).FnHdrParsed) <= p22 { pDest = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 if int32((*TOp)(unsafe.Pointer(pOp)).Fp4type) == -int32(10) { _sqlite3VdbeMemShallowCopy(tls, pDest, *(*uintptr)(unsafe.Pointer(pOp + 16)), int32(MEM_Static)) } else { _sqlite3VdbeMemSetNull(tls, pDest) } goto op_column_out } goto _218 _217: ; *(*Tu32)(unsafe.Pointer(bp + 64)) = *(*Tu32)(unsafe.Pointer(pC3 + 84 + uintptr(p22)*4)) _218: ; /* Extract the content for the p2+1-th column. Control can only ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are ** all valid. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pDest = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pDest)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 { _sqlite3VdbeMemSetNull(tls, pDest) } _ = libc.Int32FromInt32(0) if (*TVdbeCursor)(unsafe.Pointer(pC3)).FszRow >= *(*Tu32)(unsafe.Pointer(aOffset + uintptr(p22+uint32(1))*4)) { /* This is the common case where the desired content fits on the original ** page - where the content is not on an overflow page */ zData = (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow + uintptr(*(*Tu32)(unsafe.Pointer(aOffset + uintptr(p22)*4))) if *(*Tu32)(unsafe.Pointer(bp + 64)) < uint32(12) { _sqlite3VdbeSerialGet(tls, zData, *(*Tu32)(unsafe.Pointer(bp + 64)), pDest) } else { v224 = int32((*(*Tu32)(unsafe.Pointer(bp + 64)) - libc.Uint32FromInt32(12)) / libc.Uint32FromInt32(2)) len1 = v224 (*TMem)(unsafe.Pointer(pDest)).Fn = v224 (*TMem)(unsafe.Pointer(pDest)).Fenc = encoding if (*TMem)(unsafe.Pointer(pDest)).FszMalloc < len1+int32(2) { if len1 > *(*int32)(unsafe.Pointer(db + 120)) { goto too_big } (*TMem)(unsafe.Pointer(pDest)).Fflags = uint16(MEM_Null) if _sqlite3VdbeMemGrow(tls, pDest, len1+int32(2), 0) != 0 { goto no_mem } } else { (*TMem)(unsafe.Pointer(pDest)).Fz = (*TMem)(unsafe.Pointer(pDest)).FzMalloc } libc.Xmemcpy(tls, (*TMem)(unsafe.Pointer(pDest)).Fz, zData, uint32(len1)) *(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pDest)).Fz + uintptr(len1))) = uint8(0) *(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pDest)).Fz + uintptr(len1+int32(1)))) = uint8(0) (*TMem)(unsafe.Pointer(pDest)).Fflags = _aFlag1[*(*Tu32)(unsafe.Pointer(bp + 64))&uint32(1)] } } else { (*TMem)(unsafe.Pointer(pDest)).Fenc = encoding _ = libc.Int32FromInt32(0) /* This branch happens only when content is on overflow pages */ v225 = uint8(int32((*TOp)(unsafe.Pointer(pOp)).Fp5) & libc.Int32FromInt32(OPFLAG_BYTELENARG)) p5 = v225 if int32(v225) != 0 && (int32(p5) == int32(OPFLAG_TYPEOFARG) || *(*Tu32)(unsafe.Pointer(bp + 64)) >= uint32(12) && (*(*Tu32)(unsafe.Pointer(bp + 64))&uint32(1) == uint32(0) || int32(p5) == int32(OPFLAG_BYTELENARG))) || _sqlite3VdbeSerialTypeLen(tls, *(*Tu32)(unsafe.Pointer(bp + 64))) == uint32(0) { /* Content is irrelevant for ** 1. the typeof() function, ** 2. the length(X) function if X is a blob, and ** 3. if the content length is zero. ** So we might as well use bogus content rather than reading ** content from disk. ** ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the ** buffer passed to it, debugging function VdbeMemPrettyPrint() may ** read more. Use the global constant sqlite3CtypeMap[] as the array, ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint()) ** and it begins with a bunch of zeros. */ _sqlite3VdbeSerialGet(tls, uintptr(unsafe.Pointer(&_sqlite3CtypeMap)), *(*Tu32)(unsafe.Pointer(bp + 64)), pDest) } else { rc = _vdbeColumnFromOverflow(tls, pC3, int32(p22), int32(*(*Tu32)(unsafe.Pointer(bp + 64))), int64(*(*Tu32)(unsafe.Pointer(aOffset + uintptr(p22)*4))), (*TVdbe)(unsafe.Pointer(p)).FcacheCtr, colCacheCtr, pDest) if rc != 0 { if rc == int32(SQLITE_NOMEM) { goto no_mem } if rc == int32(SQLITE_TOOBIG) { goto too_big } goto abort_due_to_error } } } goto op_column_out op_column_out: ; goto _187 goto op_column_corrupt op_column_corrupt: ; if (*(*TOp)(unsafe.Pointer(aOp))).Fp3 > 0 { pOp = aOp + uintptr((*(*TOp)(unsafe.Pointer(aOp))).Fp3-int32(1))*20 goto _187 } else { rc = _sqlite3CorruptError(tls, int32(95863)) goto abort_due_to_error } /* Opcode: TypeCheck P1 P2 P3 P4 * ** Synopsis: typecheck(r[P1@P2]) ** ** Apply affinities to the range of P2 registers beginning with P1. ** Take the affinities from the Table object in P4. If any value ** cannot be coerced into the correct type, then raise an error. ** ** This opcode is similar to OP_Affinity except that this opcode ** forces the register type to the Table column type. This is used ** to implement "strict affinity". ** ** GENERATED ALWAYS AS ... STATIC columns are only checked if P3 ** is zero. When P3 is non-zero, no type checking occurs for ** static generated columns. Virtual columns are computed at query time ** and so they are never checked. ** ** Preconditions: ** **
      **
    • P2 should be the number of non-virtual columns in the ** table of P4. **
    • Table P4 should be a STRICT table. **
    ** ** If any precondition is false, an assertion fault occurs. */ _66: ; _ = libc.Int32FromInt32(0) pTab = *(*uintptr)(unsafe.Pointer(pOp + 16)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) aCol = (*TTable)(unsafe.Pointer(pTab)).FaCol pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 i2 = 0 for { if !(i2 < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if int32((*(*TColumn)(unsafe.Pointer(aCol + uintptr(i2)*12))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { if int32((*(*TColumn)(unsafe.Pointer(aCol + uintptr(i2)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 { goto _226 } if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 { pIn1 += 40 goto _226 } } _ = libc.Int32FromInt32(0) _applyAffinity(tls, pIn1, (*(*TColumn)(unsafe.Pointer(aCol + uintptr(i2)*12))).Faffinity, encoding) if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Null) == 0 { switch int32(uint32(*(*uint8)(unsafe.Pointer(aCol + uintptr(i2)*12 + 4)) & 0xf0 >> 4)) { case int32(COLTYPE_BLOB): if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Blob) == 0 { goto vdbe_type_error } case int32(COLTYPE_INTEGER): fallthrough case int32(COLTYPE_INT): if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Int) == 0 { goto vdbe_type_error } case int32(COLTYPE_TEXT): if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Str) == 0 { goto vdbe_type_error } case int32(COLTYPE_REAL): _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Int) != 0 { /* When applying REAL affinity, if the result is still an MEM_Int ** that will fit in 6 bytes, then change the type to MEM_IntReal ** so that we keep the high-resolution integer value but know that ** the type really wants to be REAL. */ if *(*Ti64)(unsafe.Pointer(pIn1)) <= int64(140737488355327) && *(*Ti64)(unsafe.Pointer(pIn1)) >= -int64(140737488355328) { p227 = pIn1 + 16 *(*Tu16)(unsafe.Pointer(p227)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p227))) | libc.Int32FromInt32(MEM_IntReal)) p228 = pIn1 + 16 *(*Tu16)(unsafe.Pointer(p228)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p228))) & ^libc.Int32FromInt32(MEM_Int)) } else { *(*float64)(unsafe.Pointer(pIn1)) = float64(*(*Ti64)(unsafe.Pointer(pIn1))) p229 = pIn1 + 16 *(*Tu16)(unsafe.Pointer(p229)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p229))) | libc.Int32FromInt32(MEM_Real)) p230 = pIn1 + 16 *(*Tu16)(unsafe.Pointer(p230)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p230))) & ^libc.Int32FromInt32(MEM_Int)) } } else { if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&(libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)) == 0 { goto vdbe_type_error } } default: /* COLTYPE_ANY. Accept anything. */ break } } pIn1 += 40 goto _226 _226: ; i2++ } _ = libc.Int32FromInt32(0) goto _187 goto vdbe_type_error vdbe_type_error: ; _sqlite3VdbeError(tls, p, __ccgo_ts+5666, libc.VaList(bp+736, _vdbeMemTypeName(tls, pIn1), _sqlite3StdType[int32(uint32(*(*uint8)(unsafe.Pointer(aCol + uintptr(i2)*12 + 4))&0xf0>>4))-int32(1)], (*TTable)(unsafe.Pointer(pTab)).FzName, (*(*TColumn)(unsafe.Pointer(aCol + uintptr(i2)*12))).FzCnName)) rc = libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(12)<= -int64(140737488355328) { p231 = pIn1 + 16 *(*Tu16)(unsafe.Pointer(p231)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p231))) | libc.Int32FromInt32(MEM_IntReal)) p232 = pIn1 + 16 *(*Tu16)(unsafe.Pointer(p232)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p232))) & ^libc.Int32FromInt32(MEM_Int)) } else { *(*float64)(unsafe.Pointer(pIn1)) = float64(*(*Ti64)(unsafe.Pointer(pIn1))) p233 = pIn1 + 16 *(*Tu16)(unsafe.Pointer(p233)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p233))) | libc.Int32FromInt32(MEM_Real)) p234 = pIn1 + 16 *(*Tu16)(unsafe.Pointer(p234)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p234))) & ^(libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_Str))) } } zAffinity++ if int32(*(*uint8)(unsafe.Pointer(zAffinity))) == 0 { break } pIn1 += 40 } goto _187 /* Opcode: MakeRecord P1 P2 P3 P4 * ** Synopsis: r[P3]=mkrec(r[P1@P2]) ** ** Convert P2 registers beginning with P1 into the [record format] ** use as a data record in a database table or as a key ** in an index. The OP_Column opcode can decode the record later. ** ** P4 may be a string that is P2 characters long. The N-th character of the ** string indicates the column affinity that should be used for the N-th ** field of the index key. ** ** The mapping from character to affinity is given by the SQLITE_AFF_ ** macros defined in sqliteInt.h. ** ** If P4 is NULL then all index fields have the affinity BLOB. ** ** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM ** compile-time option is enabled: ** ** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index ** of the right-most table that can be null-trimmed. ** ** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value ** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to ** accept no-change records with serial_type 10. This value is ** only used inside an assert() and does not affect the end result. */ _68: ; /* Where to write next byte of the payload */ /* Assuming the record contains N fields, the record format looks ** like this: ** ** ------------------------------------------------------------------------ ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | ** ------------------------------------------------------------------------ ** ** Data(0) is taken from register P1. Data(1) comes from register P1+1 ** and so forth. ** ** Each type field is a varint representing the serial type of the ** corresponding data element (see sqlite3VdbeSerialType()). The ** hdr-size field is also a varint which is the offset from the beginning ** of the record to data0. */ nData = uint64(0) /* Number of bytes of data space */ nHdr = 0 /* Number of bytes of header space */ nZero = 0 /* Number of zero bytes at the end of the record */ nField = (*TOp)(unsafe.Pointer(pOp)).Fp1 zAffinity1 = *(*uintptr)(unsafe.Pointer(pOp + 16)) _ = libc.Int32FromInt32(0) pData0 = aMem + uintptr(nField)*40 nField = (*TOp)(unsafe.Pointer(pOp)).Fp2 pLast = pData0 + uintptr(nField-int32(1))*40 /* Identify the output register */ _ = libc.Int32FromInt32(0) pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 /* Apply the requested affinity to all inputs */ _ = libc.Int32FromInt32(0) if zAffinity1 != 0 { pRec = pData0 for cond := true; cond; cond = *(*uint8)(unsafe.Pointer(zAffinity1)) != 0 { _applyAffinity(tls, pRec, *(*uint8)(unsafe.Pointer(zAffinity1)), encoding) if int32(*(*uint8)(unsafe.Pointer(zAffinity1))) == int32(SQLITE_AFF_REAL) && int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Int) != 0 { p235 = pRec + 16 *(*Tu16)(unsafe.Pointer(p235)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p235))) | libc.Int32FromInt32(MEM_IntReal)) p236 = pRec + 16 *(*Tu16)(unsafe.Pointer(p236)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p236))) & ^libc.Int32FromInt32(MEM_Int)) } zAffinity1++ pRec += 40 _ = libc.Int32FromInt32(0) } } /* Loop through the elements that will make up the record to figure ** out how much space is required for the new record. After this loop, ** the Mem.uTemp field of each term should hold the serial-type that will ** be used for that term in the generated record: ** ** Mem.uTemp value type ** --------------- --------------- ** 0 NULL ** 1 1-byte signed integer ** 2 2-byte signed integer ** 3 3-byte signed integer ** 4 4-byte signed integer ** 5 6-byte signed integer ** 6 8-byte signed integer ** 7 IEEE float ** 8 Integer constant 0 ** 9 Integer constant 1 ** 10,11 reserved for expansion ** N>=12 and even BLOB ** N>=13 and odd text ** ** The following additional values are computed: ** nHdr Number of bytes needed for the record header ** nData Number of bytes of data space needed for the record ** nZero Zero bytes at the end of the record */ pRec = pLast for cond := true; cond; cond = int32(1) != 0 { _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Null) != 0 { if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Zero) != 0 { /* Values with MEM_Null and MEM_Zero are created by xColumn virtual ** table methods that never invoke sqlite3_result_xxxxx() while ** computing an unchanging column value in an UPDATE statement. ** Give such values a special internal-use-only serial-type of 10 ** so that they can be passed through to xUpdate and have ** a true sqlite3_value_nochange(). */ _ = libc.Int32FromInt32(0) (*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(10) } else { (*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(0) } nHdr++ } else { if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 { /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ i3 = *(*Ti64)(unsafe.Pointer(pRec)) if i3 < 0 { uu = uint64(^i3) } else { uu = uint64(i3) } nHdr++ if uu <= uint64(127) { if i3&int64(1) == i3 && int32((*TVdbe)(unsafe.Pointer(p)).FminWriteFileFormat) >= int32(4) { (*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(8) + uint32(uu) } else { nData++ (*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(1) } } else { if uu <= uint64(32767) { nData += uint64(2) (*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(2) } else { if uu <= uint64(8388607) { nData += uint64(3) (*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(3) } else { if uu <= uint64(2147483647) { nData += uint64(4) (*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(4) } else { if uu <= uint64(140737488355327) { nData += uint64(6) (*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(5) } else { nData += uint64(8) if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_IntReal) != 0 { /* If the value is IntReal and is going to take up 8 bytes to store ** as an integer, then we might as well make it an 8-byte floating ** point value */ *(*float64)(unsafe.Pointer(pRec)) = float64(*(*Ti64)(unsafe.Pointer(pRec))) p237 = pRec + 16 *(*Tu16)(unsafe.Pointer(p237)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p237))) & ^libc.Int32FromInt32(MEM_IntReal)) p238 = pRec + 16 *(*Tu16)(unsafe.Pointer(p238)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p238))) | libc.Int32FromInt32(MEM_Real)) (*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(7) } else { (*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(6) } } } } } } } else { if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Real) != 0 { nHdr++ nData += uint64(8) (*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(7) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) len11 = uint32((*TMem)(unsafe.Pointer(pRec)).Fn) serial_type = len11*uint32(2) + uint32(12) + libc.BoolUint32(int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&libc.Int32FromInt32(MEM_Str) != libc.Int32FromInt32(0)) if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Zero) != 0 { serial_type += uint32(*(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pRec)).Fu)) * int32(2)) if nData != 0 { if _sqlite3VdbeMemExpandBlob(tls, pRec) != 0 { goto no_mem } len11 += uint32(*(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pRec)).Fu))) } else { nZero += int64(*(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pRec)).Fu))) } } nData += uint64(len11) nHdr += _sqlite3VarintLen(tls, uint64(serial_type)) (*TMem)(unsafe.Pointer(pRec)).FuTemp = serial_type } } } if pRec == pData0 { break } pRec -= 40 } /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint ** which determines the total number of bytes in the header. The varint ** value is the size of the header in bytes including the size varint ** itself. */ if nHdr <= int32(126) { /* The common case */ nHdr += int32(1) } else { /* Rare case of a really large header */ nVarint = _sqlite3VarintLen(tls, uint64(nHdr)) nHdr += nVarint if nVarint < _sqlite3VarintLen(tls, uint64(nHdr)) { nHdr++ } } nByte1 = int64(uint64(nHdr) + nData) /* Make sure the output register has a buffer large enough to store ** the new record. The output register (pOp->p3) is not allowed to ** be one of the input registers (because the following call to ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). */ if nByte1+nZero <= int64((*TMem)(unsafe.Pointer(pOut)).FszMalloc) { /* The output register is already large enough to hold the record. ** No error checks or buffer enlargement is required */ (*TMem)(unsafe.Pointer(pOut)).Fz = (*TMem)(unsafe.Pointer(pOut)).FzMalloc } else { /* Need to make sure that the output is not too big and then enlarge ** the output register to hold the full result */ if nByte1+nZero > int64(*(*int32)(unsafe.Pointer(db + 120))) { goto too_big } if _sqlite3VdbeMemClearAndResize(tls, pOut, int32(nByte1)) != 0 { goto no_mem } } (*TMem)(unsafe.Pointer(pOut)).Fn = int32(nByte1) (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Blob) if nZero != 0 { *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pOut)).Fu)) = int32(nZero) p239 = pOut + 16 *(*Tu16)(unsafe.Pointer(p239)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p239))) | libc.Int32FromInt32(MEM_Zero)) } zHdr1 = (*TMem)(unsafe.Pointer(pOut)).Fz zPayload = zHdr1 + uintptr(nHdr) /* Write the record */ if nHdr < int32(0x80) { v240 = zHdr1 zHdr1++ *(*Tu8)(unsafe.Pointer(v240)) = uint8(nHdr) } else { zHdr1 += uintptr(_sqlite3PutVarint(tls, zHdr1, uint64(nHdr))) } _ = libc.Int32FromInt32(0) pRec = pData0 for int32(1) != 0 { serial_type = (*TMem)(unsafe.Pointer(pRec)).FuTemp /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more ** additional varints, one per column. ** EVIDENCE-OF: R-64536-51728 The values for each column in the record ** immediately follow the header. */ if serial_type <= uint32(7) { v241 = zHdr1 zHdr1++ *(*Tu8)(unsafe.Pointer(v241)) = uint8(serial_type) if serial_type == uint32(0) { /* NULL value. No change in zPayload */ } else { if serial_type == uint32(7) { _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, bp+72, pRec, uint32(8)) } else { *(*Tu64)(unsafe.Pointer(bp + 72)) = uint64(*(*Ti64)(unsafe.Pointer(pRec))) } len11 = uint32(_sqlite3SmallTypeSizes[serial_type]) _ = libc.Int32FromInt32(0) switch len11 { default: *(*Tu8)(unsafe.Pointer(zPayload + 7)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 72)) & libc.Uint64FromInt32(0xff)) *(*Tu64)(unsafe.Pointer(bp + 72)) >>= uint64(8) *(*Tu8)(unsafe.Pointer(zPayload + 6)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 72)) & libc.Uint64FromInt32(0xff)) *(*Tu64)(unsafe.Pointer(bp + 72)) >>= uint64(8) fallthrough case uint32(6): *(*Tu8)(unsafe.Pointer(zPayload + 5)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 72)) & libc.Uint64FromInt32(0xff)) *(*Tu64)(unsafe.Pointer(bp + 72)) >>= uint64(8) *(*Tu8)(unsafe.Pointer(zPayload + 4)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 72)) & libc.Uint64FromInt32(0xff)) *(*Tu64)(unsafe.Pointer(bp + 72)) >>= uint64(8) fallthrough case uint32(4): *(*Tu8)(unsafe.Pointer(zPayload + 3)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 72)) & libc.Uint64FromInt32(0xff)) *(*Tu64)(unsafe.Pointer(bp + 72)) >>= uint64(8) fallthrough case uint32(3): *(*Tu8)(unsafe.Pointer(zPayload + 2)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 72)) & libc.Uint64FromInt32(0xff)) *(*Tu64)(unsafe.Pointer(bp + 72)) >>= uint64(8) fallthrough case uint32(2): *(*Tu8)(unsafe.Pointer(zPayload + 1)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 72)) & libc.Uint64FromInt32(0xff)) *(*Tu64)(unsafe.Pointer(bp + 72)) >>= uint64(8) fallthrough case uint32(1): *(*Tu8)(unsafe.Pointer(zPayload)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 72)) & libc.Uint64FromInt32(0xff)) } zPayload += uintptr(len11) } } else { if serial_type < uint32(0x80) { v242 = zHdr1 zHdr1++ *(*Tu8)(unsafe.Pointer(v242)) = uint8(serial_type) if serial_type >= uint32(14) && (*TMem)(unsafe.Pointer(pRec)).Fn > 0 { _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, zPayload, (*TMem)(unsafe.Pointer(pRec)).Fz, uint32((*TMem)(unsafe.Pointer(pRec)).Fn)) zPayload += uintptr((*TMem)(unsafe.Pointer(pRec)).Fn) } } else { zHdr1 += uintptr(_sqlite3PutVarint(tls, zHdr1, uint64(serial_type))) if (*TMem)(unsafe.Pointer(pRec)).Fn != 0 { _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, zPayload, (*TMem)(unsafe.Pointer(pRec)).Fz, uint32((*TMem)(unsafe.Pointer(pRec)).Fn)) zPayload += uintptr((*TMem)(unsafe.Pointer(pRec)).Fn) } } } if pRec == pLast { break } pRec += 40 } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) goto _187 /* Opcode: Count P1 P2 P3 * * ** Synopsis: r[P2]=count() ** ** Store the number of entries (an integer value) in the table or index ** opened by cursor P1 in register P2. ** ** If P3==0, then an exact count is obtained, which involves visiting ** every btree page of the table. But if P3 is non-zero, an estimate ** is returned based on the current cursor position. */ _69: ; _ = libc.Int32FromInt32(0) pCrsr1 = *(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) + 36)) _ = libc.Int32FromInt32(0) if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 { *(*Ti64)(unsafe.Pointer(bp + 80)) = _sqlite3BtreeRowCountEst(tls, pCrsr1) } else { *(*Ti64)(unsafe.Pointer(bp + 80)) = 0 /* Not needed. Only used to silence a warning. */ rc = _sqlite3BtreeCount(tls, db, pCrsr1, bp+80) if rc != 0 { goto abort_due_to_error } } pOut = _out2Prerelease(tls, p, pOp) *(*Ti64)(unsafe.Pointer(pOut)) = *(*Ti64)(unsafe.Pointer(bp + 80)) goto check_for_interrupt /* Opcode: Savepoint P1 * * P4 * ** ** Open, release or rollback the savepoint named by parameter P4, depending ** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN). ** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE). ** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK). */ _70: ; p12 = (*TOp)(unsafe.Pointer(pOp)).Fp1 zName = *(*uintptr)(unsafe.Pointer(pOp + 16)) /* Assert that the p1 parameter is valid. Also that if there is no open ** transaction, then there cannot be any savepoints. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if p12 == SAVEPOINT_BEGIN { if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite > 0 { /* A new savepoint cannot be created if there are active write ** statements (i.e. open read/write incremental blob handles). */ _sqlite3VdbeError(tls, p, __ccgo_ts+5707, 0) rc = int32(SQLITE_BUSY) } else { nName = _sqlite3Strlen30(tls, zName) /* This call is Ok even if this savepoint is actually a transaction ** savepoint (and therefore should not prompt xSavepoint()) callbacks. ** If this is a transaction savepoint being opened, it is guaranteed ** that the db->aVTrans[] array is empty. */ _ = libc.Int32FromInt32(0) rc = _sqlite3VtabSavepoint(tls, db, SAVEPOINT_BEGIN, (*Tsqlite3)(unsafe.Pointer(db)).FnStatement+(*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint) if rc != SQLITE_OK { goto abort_due_to_error } /* Create a new savepoint structure. */ pNew = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(32)+uint32(nName)+uint32(1))) if pNew != 0 { (*TSavepoint)(unsafe.Pointer(pNew)).FzName = pNew + 1*32 libc.Xmemcpy(tls, (*TSavepoint)(unsafe.Pointer(pNew)).FzName, zName, uint32(nName+int32(1))) /* If there is no open transaction, then mark this as a special ** "transaction savepoint". */ if (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(0) (*Tsqlite3)(unsafe.Pointer(db)).FisTransactionSavepoint = uint8(1) } else { (*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint++ } /* Link the new savepoint into the database handle's list. */ (*TSavepoint)(unsafe.Pointer(pNew)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).FpSavepoint (*Tsqlite3)(unsafe.Pointer(db)).FpSavepoint = pNew (*TSavepoint)(unsafe.Pointer(pNew)).FnDeferredCons = (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons (*TSavepoint)(unsafe.Pointer(pNew)).FnDeferredImmCons = (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons } } } else { _ = libc.Int32FromInt32(0) iSavepoint = 0 /* Find the named savepoint. If there is no such savepoint, then an ** an error is returned to the user. */ pSavepoint = (*Tsqlite3)(unsafe.Pointer(db)).FpSavepoint for { if !(pSavepoint != 0 && _sqlite3StrICmp(tls, (*TSavepoint)(unsafe.Pointer(pSavepoint)).FzName, zName) != 0) { break } iSavepoint++ goto _243 _243: ; pSavepoint = (*TSavepoint)(unsafe.Pointer(pSavepoint)).FpNext } if !(pSavepoint != 0) { _sqlite3VdbeError(tls, p, __ccgo_ts+5758, libc.VaList(bp+736, zName)) rc = int32(SQLITE_ERROR) } else { if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite > 0 && p12 == int32(SAVEPOINT_RELEASE) { /* It is not possible to release (commit) a savepoint if there are ** active write statements. */ _sqlite3VdbeError(tls, p, __ccgo_ts+5780, 0) rc = int32(SQLITE_BUSY) } else { /* Determine whether or not this is a transaction savepoint. If so, ** and this is a RELEASE command, then the current transaction ** is committed. */ isTransaction = libc.BoolInt32((*TSavepoint)(unsafe.Pointer(pSavepoint)).FpNext == uintptr(0) && (*Tsqlite3)(unsafe.Pointer(db)).FisTransactionSavepoint != 0) if isTransaction != 0 && p12 == int32(SAVEPOINT_RELEASE) { v244 = _sqlite3VdbeCheckFk(tls, p, int32(1)) rc = v244 if v244 != SQLITE_OK { goto vdbe_return } (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(1) if _sqlite3VdbeHalt(tls, p) == int32(SQLITE_BUSY) { (*TVdbe)(unsafe.Pointer(p)).Fpc = (int32(pOp) - int32(aOp)) / 20 (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(0) v245 = libc.Int32FromInt32(SQLITE_BUSY) rc = v245 (*TVdbe)(unsafe.Pointer(p)).Frc = v245 goto vdbe_return } rc = (*TVdbe)(unsafe.Pointer(p)).Frc if rc != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(0) } else { (*Tsqlite3)(unsafe.Pointer(db)).FisTransactionSavepoint = uint8(0) } } else { iSavepoint = (*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint - iSavepoint - int32(1) if p12 == int32(SAVEPOINT_ROLLBACK) { isSchemaChange = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_SchemaChange) != uint32(0)) ii = 0 for { if !(ii < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } rc = _sqlite3BtreeTripAllCursors(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii)*16))).FpBt, libc.Int32FromInt32(SQLITE_ABORT)|libc.Int32FromInt32(2)< 0 { /* If this instruction implements a COMMIT and other VMs are writing ** return an error indicating that the other VMs must complete first. */ _sqlite3VdbeError(tls, p, __ccgo_ts+5834, 0) rc = int32(SQLITE_BUSY) goto abort_due_to_error } else { v248 = _sqlite3VdbeCheckFk(tls, p, int32(1)) rc = v248 if v248 != SQLITE_OK { goto vdbe_return } else { (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(desiredAutoCommit) } } } if _sqlite3VdbeHalt(tls, p) == int32(SQLITE_BUSY) { (*TVdbe)(unsafe.Pointer(p)).Fpc = (int32(pOp) - int32(aOp)) / 20 (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(libc.Int32FromInt32(1) - desiredAutoCommit) v249 = libc.Int32FromInt32(SQLITE_BUSY) rc = v249 (*TVdbe)(unsafe.Pointer(p)).Frc = v249 goto vdbe_return } _sqlite3CloseSavepoints(tls, db) if (*TVdbe)(unsafe.Pointer(p)).Frc == SQLITE_OK { rc = int32(SQLITE_DONE) } else { rc = int32(SQLITE_ERROR) } goto vdbe_return } else { if !(desiredAutoCommit != 0) { v250 = __ccgo_ts + 5889 } else { if iRollback != 0 { v251 = __ccgo_ts + 5937 } else { v251 = __ccgo_ts + 5980 } v250 = v251 } _sqlite3VdbeError(tls, p, v250, 0) rc = int32(SQLITE_ERROR) goto abort_due_to_error } _ = libc.Int32FromInt32(0) /* Opcode: Transaction P1 P2 P3 P4 P5 ** ** Begin a transaction on database P1 if a transaction is not already ** active. ** If P2 is non-zero, then a write-transaction is started, or if a ** read-transaction is already active, it is upgraded to a write-transaction. ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more ** then an exclusive transaction is started. ** ** P1 is the index of the database file on which the transaction is ** started. Index 0 is the main database file and index 1 is the ** file used for temporary tables. Indices of 2 or more are used for ** attached databases. ** ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is ** true (this flag is set if the Vdbe may modify more than one row and may ** throw an ABORT exception), a statement transaction may also be opened. ** More specifically, a statement transaction is opened iff the database ** connection is currently not in autocommit mode, or if there are other ** active statements. A statement transaction allows the changes made by this ** VDBE to be rolled back after an error without having to roll back the ** entire transaction. If no error is encountered, the statement transaction ** will automatically commit when the VDBE halts. ** ** If P5!=0 then this opcode also checks the schema cookie against P3 ** and the schema generation counter against P4. ** The cookie changes its value whenever the database schema changes. ** This operation is used to detect when that the cookie has changed ** and that the current process needs to reread the schema. If the schema ** cookie in P3 differs from the schema cookie in the database header or ** if the schema generation counter in P4 differs from the current ** generation counter, then an SQLITE_SCHEMA error is raised and execution ** halts. The sqlite3_step() wrapper function might then reprepare the ** statement and rerun it from the beginning. */ _72: ; *(*int32)(unsafe.Pointer(bp + 88)) = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TOp)(unsafe.Pointer(pOp)).Fp2 != 0 && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(libc.Uint64FromInt32(SQLITE_QueryOnly)|uint64(libc.Int32FromInt32(0x00002))<>5)) != 0 && (*TOp)(unsafe.Pointer(pOp)).Fp2 != 0 && (int32((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit) == 0 || (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeRead > int32(1)) { _ = libc.Int32FromInt32(0) if (*TVdbe)(unsafe.Pointer(p)).FiStatement == 0 { _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).FnStatement++ (*TVdbe)(unsafe.Pointer(p)).FiStatement = (*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint + (*Tsqlite3)(unsafe.Pointer(db)).FnStatement } rc = _sqlite3VtabSavepoint(tls, db, SAVEPOINT_BEGIN, (*TVdbe)(unsafe.Pointer(p)).FiStatement-int32(1)) if rc == SQLITE_OK { rc = _sqlite3BtreeBeginStmt(tls, pBt, (*TVdbe)(unsafe.Pointer(p)).FiStatement) } /* Store the current value of the database handles deferred constraint ** counter. If the statement transaction needs to be rolled back, ** the value of this counter needs to be restored too. */ (*TVdbe)(unsafe.Pointer(p)).FnStmtDefCons = (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons (*TVdbe)(unsafe.Pointer(p)).FnStmtDefImmCons = (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons } } _ = libc.Int32FromInt32(0) if rc == SQLITE_OK && (*TOp)(unsafe.Pointer(pOp)).Fp5 != 0 && (*(*int32)(unsafe.Pointer(bp + 88)) != (*TOp)(unsafe.Pointer(pOp)).Fp3 || (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).FiGeneration != *(*int32)(unsafe.Pointer(pOp + 16))) { /* ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema ** version is checked to ensure that the schema has not changed since the ** SQL statement was prepared. */ _sqlite3DbFree(tls, db, (*TVdbe)(unsafe.Pointer(p)).FzErrMsg) (*TVdbe)(unsafe.Pointer(p)).FzErrMsg = _sqlite3DbStrDup(tls, db, __ccgo_ts+6021) /* If the schema-cookie from the database file matches the cookie ** stored with the in-memory representation of the schema, do ** not reload the schema from the database file. ** ** If virtual-tables are in use, this is not just an optimization. ** Often, v-tables store their data in other SQLite tables, which ** are queried from within xNext() and other v-table methods using ** prepared queries. If such a query is out-of-date, we do not want to ** discard the database schema, as the user code implementing the ** v-table would have to be ready for the sqlite3_vtab structure itself ** to be invalidated whenever sqlite3_step() is called from within ** a v-table method. */ if (*TSchema)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16))).FpSchema)).Fschema_cookie != *(*int32)(unsafe.Pointer(bp + 88)) { _sqlite3ResetOneSchema(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1) } libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(1), 0, 0x3) rc = int32(SQLITE_SCHEMA) /* Set changeCntOn to 0 to prevent the value returned by sqlite3_changes() ** from being modified in sqlite3VdbeHalt(). If this statement is ** reprepared, changeCntOn will be set again. */ libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 4, 0x10) } if rc != 0 { goto abort_due_to_error } goto _187 /* Opcode: ReadCookie P1 P2 P3 * * ** ** Read cookie number P3 from database P1 and write it into register P2. ** P3==1 is the schema version. P3==2 is the database format. ** P3==3 is the recommended pager cache size, and so forth. P1==0 is ** the main database file and P1==1 is the database file used to store ** temporary tables. ** ** There must be a read-lock on the database (either a transaction ** must be started or there must be an open cursor) before ** executing this instruction. */ _73: ; _ = libc.Int32FromInt32(0) iDb = (*TOp)(unsafe.Pointer(pOp)).Fp1 iCookie = (*TOp)(unsafe.Pointer(pOp)).Fp3 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3BtreeGetMeta(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt, iCookie, bp+92) pOut = _out2Prerelease(tls, p, pOp) *(*Ti64)(unsafe.Pointer(pOut)) = int64(*(*int32)(unsafe.Pointer(bp + 92))) goto _187 /* Opcode: SetCookie P1 P2 P3 * P5 ** ** Write the integer value P3 into cookie number P2 of database P1. ** P2==1 is the schema version. P2==2 is the database format. ** P2==3 is the recommended pager cache ** size, and so forth. P1==0 is the main database file and P1==1 is the ** database file used to store temporary tables. ** ** A transaction must be started before executing this opcode. ** ** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement ** has P5 set to 1, so that the internal schema version will be different ** from the database schema version, resulting in a schema reset. */ _74: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pDb1 = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* See note about index shifting on OP_ReadCookie */ rc = _sqlite3BtreeUpdateMeta(tls, (*TDb)(unsafe.Pointer(pDb1)).FpBt, (*TOp)(unsafe.Pointer(pOp)).Fp2, uint32((*TOp)(unsafe.Pointer(pOp)).Fp3)) if (*TOp)(unsafe.Pointer(pOp)).Fp2 == int32(BTREE_SCHEMA_VERSION) { /* When the schema cookie changes, record the new cookie internally */ *(*Tu32)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb1)).FpSchema)) = *(*Tu32)(unsafe.Pointer(pOp + 12)) - uint32((*TOp)(unsafe.Pointer(pOp)).Fp5) *(*Tu32)(unsafe.Pointer(db + 24)) |= uint32(DBFLAG_SchemaChange) _sqlite3FkClearTriggerCache(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1) } else { if (*TOp)(unsafe.Pointer(pOp)).Fp2 == int32(BTREE_FILE_FORMAT) { /* Record changes in the file format */ (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb1)).FpSchema)).Ffile_format = uint8((*TOp)(unsafe.Pointer(pOp)).Fp3) } } if (*TOp)(unsafe.Pointer(pOp)).Fp1 == int32(1) { /* Invalidate all prepared statements whenever the TEMP database ** schema is changed. Ticket #1644 */ _sqlite3ExpirePreparedStatements(tls, db, 0) libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 0, 0x3) } if rc != 0 { goto abort_due_to_error } goto _187 /* Opcode: OpenRead P1 P2 P3 P4 P5 ** Synopsis: root=P2 iDb=P3 ** ** Open a read-only cursor for the database table whose root page is ** P2 in a database file. The database file is determined by P3. ** P3==0 means the main database, P3==1 means the database used for ** temporary tables, and P3>1 means used the corresponding attached ** database. Give the new cursor an identifier of P1. The P1 ** values need not be contiguous but all P1 values should be small integers. ** It is an error for P1 to be negative. ** ** Allowed P5 bits: **
      **
    • 0x02 OPFLAG_SEEKEQ: This cursor will only be used for ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT ** of OP_SeekLE/OP_IdxLT) **
    ** ** The P4 value may be either an integer (P4_INT32) or a pointer to ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo ** object, then table being opened must be an [index b-tree] where the ** KeyInfo object defines the content and collating ** sequence of that index b-tree. Otherwise, if P4 is an integer ** value, then the table being opened must be a [table b-tree] with a ** number of columns no less than the value of P4. ** ** See also: OpenWrite, ReopenIdx */ /* Opcode: ReopenIdx P1 P2 P3 P4 P5 ** Synopsis: root=P2 iDb=P3 ** ** The ReopenIdx opcode works like OP_OpenRead except that it first ** checks to see if the cursor on P1 is already open on the same ** b-tree and if it is this opcode becomes a no-op. In other words, ** if the cursor is already open, do not reopen it. ** ** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ ** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must ** be the same as every other ReopenIdx or OpenRead for the same cursor ** number. ** ** Allowed P5 bits: **
      **
    • 0x02 OPFLAG_SEEKEQ: This cursor will only be used for ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT ** of OP_SeekLE/OP_IdxLT) **
    ** ** See also: OP_OpenRead, OP_OpenWrite */ /* Opcode: OpenWrite P1 P2 P3 P4 P5 ** Synopsis: root=P2 iDb=P3 ** ** Open a read/write cursor named P1 on the table or index whose root ** page is P2 (or whose root page is held in register P2 if the ** OPFLAG_P2ISREG bit is set in P5 - see below). ** ** The P4 value may be either an integer (P4_INT32) or a pointer to ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo ** object, then table being opened must be an [index b-tree] where the ** KeyInfo object defines the content and collating ** sequence of that index b-tree. Otherwise, if P4 is an integer ** value, then the table being opened must be a [table b-tree] with a ** number of columns no less than the value of P4. ** ** Allowed P5 bits: **
      **
    • 0x02 OPFLAG_SEEKEQ: This cursor will only be used for ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT ** of OP_SeekLE/OP_IdxLT) **
    • 0x08 OPFLAG_FORDELETE: This cursor is used only to seek ** and subsequently delete entries in an index btree. This is a ** hint to the storage engine that the storage engine is allowed to ** ignore. The hint is not used by the official SQLite b*tree storage ** engine, but is used by COMDB2. **
    • 0x10 OPFLAG_P2ISREG: Use the content of register P2 ** as the root page, not the value of P2 itself. **
    ** ** This instruction works like OpenRead except that it opens the cursor ** in read/write mode. ** ** See also: OP_OpenRead, OP_ReopenIdx */ _77: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pCur = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) if pCur != 0 && (*TVdbeCursor)(unsafe.Pointer(pCur)).FpgnoRoot == uint32((*TOp)(unsafe.Pointer(pOp)).Fp2) { _ = libc.Int32FromInt32(0) /* Guaranteed by the code generator */ _ = libc.Int32FromInt32(0) _sqlite3BtreeClearCursor(tls, *(*uintptr)(unsafe.Pointer(pCur + 36))) goto open_cursor_set_hints } /* If the cursor is not currently open or is open on a different ** index, then fall through into OP_OpenRead to force a reopen */ _76: ; /* ncycle */ _75: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x3>>0)) == int32(1) { rc = libc.Int32FromInt32(SQLITE_ABORT) | libc.Int32FromInt32(2)<>2))), 2, 0x4) *(*uintptr)(unsafe.Pointer(pCx + 12)) = *(*uintptr)(unsafe.Pointer(pOrig + 12)) libc.SetBitFieldPtr8Uint32(pCx+8, libc.Uint32FromInt32(1), 3, 0x8) libc.SetBitFieldPtr8Uint32(pOrig+8, libc.Uint32FromInt32(1), 3, 0x8) rc = _sqlite3BtreeCursor(tls, *(*uintptr)(unsafe.Pointer(pCx + 12)), (*TVdbeCursor)(unsafe.Pointer(pCx)).FpgnoRoot, int32(BTREE_WRCSR), (*TVdbeCursor)(unsafe.Pointer(pCx)).FpKeyInfo, *(*uintptr)(unsafe.Pointer(pCx + 36))) /* The sqlite3BtreeCursor() routine can only fail for the first cursor ** opened for a database. Since there is already an open cursor when this ** opcode is run, the sqlite3BtreeCursor() cannot fail */ _ = libc.Int32FromInt32(0) goto _187 /* Opcode: OpenEphemeral P1 P2 P3 P4 P5 ** Synopsis: nColumn=P2 ** ** Open a new cursor P1 to a transient table. ** The cursor is always opened read/write even if ** the main database is read-only. The ephemeral ** table is deleted automatically when the cursor is closed. ** ** If the cursor P1 is already opened on an ephemeral table, the table ** is cleared (all content is erased). ** ** P2 is the number of columns in the ephemeral table. ** The cursor points to a BTree table if P4==0 and to a BTree index ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure ** that defines the format of keys in the index. ** ** The P5 parameter can be a mask of the BTREE_* flags defined ** in btree.h. These flags control aspects of the operation of ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are ** added automatically. ** ** If P3 is positive, then reg[P3] is modified slightly so that it ** can be used as zero-length data for OP_Insert. This is an optimization ** that avoids an extra OP_Blob opcode to initialize that register. */ /* Opcode: OpenAutoindex P1 P2 * P4 * ** Synopsis: nColumn=P2 ** ** This opcode works the same as OP_OpenEphemeral. It has a ** different name to distinguish its use. Tables created using ** by this opcode will be used for automatically created transient ** indices in joins. */ _80: ; /* ncycle */ _79: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TOp)(unsafe.Pointer(pOp)).Fp3 > 0 { /* Make register reg[P3] into a value that can be used as the data ** form sqlite3BtreeInsert() where the length of the data is zero. */ _ = libc.Int32FromInt32(0) /* Only used when number of columns is zero */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*(*TMem)(unsafe.Pointer(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40))).Fn = 0 (*(*TMem)(unsafe.Pointer(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40))).Fz = __ccgo_ts + 1648 } pCx1 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) if pCx1 != 0 && !(int32(TBool(*(*uint8)(unsafe.Pointer(pCx1 + 8))&0x8>>3)) != 0) && (*TOp)(unsafe.Pointer(pOp)).Fp2 <= int32((*TVdbeCursor)(unsafe.Pointer(pCx1)).FnField) { /* If the ephemeral table is already open and has no duplicates from ** OP_OpenDup, then erase all existing content so that the table is ** empty again, rather than creating a new table. */ _ = libc.Int32FromInt32(0) (*TVdbeCursor)(unsafe.Pointer(pCx1)).FseqCount = 0 (*TVdbeCursor)(unsafe.Pointer(pCx1)).FcacheStatus = uint32(CACHE_STALE) rc = _sqlite3BtreeClearTable(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 12)), int32((*TVdbeCursor)(unsafe.Pointer(pCx1)).FpgnoRoot), uintptr(0)) } else { pCx1 = _allocateCursor(tls, p, (*TOp)(unsafe.Pointer(pOp)).Fp1, (*TOp)(unsafe.Pointer(pOp)).Fp2, uint8(CURTYPE_BTREE)) if pCx1 == uintptr(0) { goto no_mem } libc.SetBitFieldPtr8Uint32(pCx1+8, libc.Uint32FromInt32(1), 0, 0x1) rc = _sqlite3BtreeOpen(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, uintptr(0), db, pCx1+12, libc.Int32FromInt32(BTREE_OMIT_JOURNAL)|libc.Int32FromInt32(BTREE_SINGLE)|int32((*TOp)(unsafe.Pointer(pOp)).Fp5), _vfsFlags) if rc == SQLITE_OK { rc = _sqlite3BtreeBeginTrans(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 12)), int32(1), uintptr(0)) if rc == SQLITE_OK { /* If a transient index is required, create it by calling ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before ** opening it. If a transient table is required, just use the ** automatically created table with root-page 1 (an BLOB_INTKEY table). */ v253 = *(*uintptr)(unsafe.Pointer(pOp + 16)) pKeyInfo2 = v253 v252 = v253 (*TVdbeCursor)(unsafe.Pointer(pCx1)).FpKeyInfo = v252 if v252 != uintptr(0) { _ = libc.Int32FromInt32(0) rc = _sqlite3BtreeCreateTable(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 12)), pCx1+48, int32(BTREE_BLOBKEY)|int32((*TOp)(unsafe.Pointer(pOp)).Fp5)) if rc == SQLITE_OK { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3BtreeCursor(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 12)), (*TVdbeCursor)(unsafe.Pointer(pCx1)).FpgnoRoot, int32(BTREE_WRCSR), pKeyInfo2, *(*uintptr)(unsafe.Pointer(pCx1 + 36))) } (*TVdbeCursor)(unsafe.Pointer(pCx1)).FisTable = uint8(0) } else { (*TVdbeCursor)(unsafe.Pointer(pCx1)).FpgnoRoot = uint32(SCHEMA_ROOT) rc = _sqlite3BtreeCursor(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 12)), uint32(SCHEMA_ROOT), int32(BTREE_WRCSR), uintptr(0), *(*uintptr)(unsafe.Pointer(pCx1 + 36))) (*TVdbeCursor)(unsafe.Pointer(pCx1)).FisTable = uint8(1) } } libc.SetBitFieldPtr8Uint32(pCx1+8, libc.BoolUint32(int32((*TOp)(unsafe.Pointer(pOp)).Fp5) != libc.Int32FromInt32(BTREE_UNORDERED)), 2, 0x4) if rc != 0 { _sqlite3BtreeClose(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 12))) } } } if rc != 0 { goto abort_due_to_error } (*TVdbeCursor)(unsafe.Pointer(pCx1)).FnullRow = uint8(1) goto _187 /* Opcode: SorterOpen P1 P2 P3 P4 * ** ** This opcode works like OP_OpenEphemeral except that it opens ** a transient index that is specifically designed to sort large ** tables using an external merge-sort algorithm. ** ** If argument P3 is non-zero, then it indicates that the sorter may ** assume that a stable sort considering the first P3 fields of each ** key is sufficient to produce the required results. */ _81: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pCx2 = _allocateCursor(tls, p, (*TOp)(unsafe.Pointer(pOp)).Fp1, (*TOp)(unsafe.Pointer(pOp)).Fp2, uint8(CURTYPE_SORTER)) if pCx2 == uintptr(0) { goto no_mem } (*TVdbeCursor)(unsafe.Pointer(pCx2)).FpKeyInfo = *(*uintptr)(unsafe.Pointer(pOp + 16)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3VdbeSorterInit(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp3, pCx2) if rc != 0 { goto abort_due_to_error } goto _187 /* Opcode: SequenceTest P1 P2 * * * ** Synopsis: if( cursor[P1].ctr++ ) pc = P2 ** ** P1 is a sorter cursor. If the sequence counter is currently zero, jump ** to P2. Regardless of whether or not the jump is taken, increment the ** the sequence value. */ _82: ; _ = libc.Int32FromInt32(0) pC4 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) v255 = pC4 + 16 v254 = *(*Ti64)(unsafe.Pointer(v255)) *(*Ti64)(unsafe.Pointer(v255))++ if v254 == 0 { goto jump_to_p2 } goto _187 /* Opcode: OpenPseudo P1 P2 P3 * * ** Synopsis: P3 columns in r[P2] ** ** Open a new cursor that points to a fake table that contains a single ** row of data. The content of that one row is the content of memory ** register P2. In other words, cursor P1 becomes an alias for the ** MEM_Blob content contained in register P2. ** ** A pseudo-table created by this opcode is used to hold a single ** row output from the sorter so that the row can be decomposed into ** individual columns using the OP_Column opcode. The OP_Column opcode ** is the only cursor opcode that works with a pseudo-table. ** ** P3 is the number of fields in the records that will be stored by ** the pseudo-table. */ _83: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pCx3 = _allocateCursor(tls, p, (*TOp)(unsafe.Pointer(pOp)).Fp1, (*TOp)(unsafe.Pointer(pOp)).Fp3, uint8(CURTYPE_PSEUDO)) if pCx3 == uintptr(0) { goto no_mem } (*TVdbeCursor)(unsafe.Pointer(pCx3)).FnullRow = uint8(1) (*TVdbeCursor)(unsafe.Pointer(pCx3)).FseekResult = (*TOp)(unsafe.Pointer(pOp)).Fp2 (*TVdbeCursor)(unsafe.Pointer(pCx3)).FisTable = uint8(1) /* Give this pseudo-cursor a fake BtCursor pointer so that pCx ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto() ** which is a performance optimization */ *(*uintptr)(unsafe.Pointer(pCx3 + 36)) = _sqlite3BtreeFakeValidCursor(tls) _ = libc.Int32FromInt32(0) goto _187 /* Opcode: Close P1 * * * * ** ** Close a cursor previously opened as P1. If P1 is not ** currently open, this instruction is a no-op. */ _84: ; /* ncycle */ _ = libc.Int32FromInt32(0) _sqlite3VdbeFreeCursor(tls, p, *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))) *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) = uintptr(0) goto _187 /* Opcode: SeekGE P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), ** use the value in register P3 as the key. If cursor P1 refers ** to an SQL index, then P3 is the first in an array of P4 registers ** that are used as an unpacked index key. ** ** Reposition cursor P1 so that it points to the smallest entry that ** is greater than or equal to the key value. If there are no records ** greater than or equal to the key and P2 is not zero, then jump to P2. ** ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this ** opcode will either land on a record that exactly matches the key, or ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ, ** this opcode must be followed by an IdxLE opcode with the same arguments. ** The IdxGT opcode will be skipped if this opcode succeeds, but the ** IdxGT opcode will be used on subsequent loop iterations. The ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this ** is an equality search. ** ** This opcode leaves the cursor configured to move in forward order, ** from the beginning toward the end. In other words, the cursor is ** configured to use Next, not Prev. ** ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe */ /* Opcode: SeekGT P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), ** use the value in register P3 as a key. If cursor P1 refers ** to an SQL index, then P3 is the first in an array of P4 registers ** that are used as an unpacked index key. ** ** Reposition cursor P1 so that it points to the smallest entry that ** is greater than the key value. If there are no records greater than ** the key and P2 is not zero, then jump to P2. ** ** This opcode leaves the cursor configured to move in forward order, ** from the beginning toward the end. In other words, the cursor is ** configured to use Next, not Prev. ** ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe */ /* Opcode: SeekLT P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), ** use the value in register P3 as a key. If cursor P1 refers ** to an SQL index, then P3 is the first in an array of P4 registers ** that are used as an unpacked index key. ** ** Reposition cursor P1 so that it points to the largest entry that ** is less than the key value. If there are no records less than ** the key and P2 is not zero, then jump to P2. ** ** This opcode leaves the cursor configured to move in reverse order, ** from the end toward the beginning. In other words, the cursor is ** configured to use Prev, not Next. ** ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe */ /* Opcode: SeekLE P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), ** use the value in register P3 as a key. If cursor P1 refers ** to an SQL index, then P3 is the first in an array of P4 registers ** that are used as an unpacked index key. ** ** Reposition cursor P1 so that it points to the largest entry that ** is less than or equal to the key value. If there are no records ** less than or equal to the key and P2 is not zero, then jump to P2. ** ** This opcode leaves the cursor configured to move in reverse order, ** from the end toward the beginning. In other words, the cursor is ** configured to use Prev, not Next. ** ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this ** opcode will either land on a record that exactly matches the key, or ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ, ** this opcode must be followed by an IdxLE opcode with the same arguments. ** The IdxGE opcode will be skipped if this opcode succeeds, but the ** IdxGE opcode will be used on subsequent loop iterations. The ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this ** is an equality search. ** ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt */ _88: ; /* jump, in3, group, ncycle */ _87: ; /* jump, in3, group, ncycle */ _86: ; /* jump, in3, group, ncycle */ _85: ; /* Only interested in == results */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pC5 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) oc = int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) eqOnly = 0 (*TVdbeCursor)(unsafe.Pointer(pC5)).FnullRow = uint8(0) (*TVdbeCursor)(unsafe.Pointer(pC5)).FdeferredMoveto = uint8(0) (*TVdbeCursor)(unsafe.Pointer(pC5)).FcacheStatus = uint32(CACHE_STALE) if (*TVdbeCursor)(unsafe.Pointer(pC5)).FisTable != 0 { /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */ _ = libc.Int32FromInt32(0) /* The input value in P3 might be of any type: integer, real, string, ** blob, or NULL. But it needs to be an integer before we can do ** the seek, so convert it. */ pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 flags31 = (*TMem)(unsafe.Pointer(pIn3)).Fflags if int32(flags31)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)|libc.Int32FromInt32(MEM_Str)) == int32(MEM_Str) { _applyNumericAffinity(tls, pIn3, 0) } iKey = _sqlite3VdbeIntValue(tls, pIn3) /* Get the integer key value */ newType = (*TMem)(unsafe.Pointer(pIn3)).Fflags /* Record the type after applying numeric affinity */ (*TMem)(unsafe.Pointer(pIn3)).Fflags = flags31 /* But convert the type back to its original */ /* If the P3 value could not be converted into an integer without ** loss of information, then special processing is required... */ if int32(newType)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) == 0 { if int32(newType)&int32(MEM_Real) == 0 { if int32(newType)&int32(MEM_Null) != 0 || oc >= int32(OP_SeekGE) { goto jump_to_p2 } else { rc = _sqlite3BtreeLast(tls, *(*uintptr)(unsafe.Pointer(pC5 + 36)), bp+96) if rc != SQLITE_OK { goto abort_due_to_error } goto seek_not_found } } c2 = _sqlite3IntFloatCompare(tls, iKey, *(*float64)(unsafe.Pointer(pIn3))) /* If the approximation iKey is larger than the actual real search ** term, substitute >= for > and < for <=. e.g. if the search term ** is 4.9 and the integer approximation 5: ** ** (x > 4.9) -> (x >= 5) ** (x <= 4.9) -> (x < 5) */ if c2 > 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if oc&int32(0x0001) == libc.Int32FromInt32(OP_SeekGT)&libc.Int32FromInt32(0x0001) { oc-- } } else { if c2 < 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if oc&int32(0x0001) == libc.Int32FromInt32(OP_SeekLT)&libc.Int32FromInt32(0x0001) { oc++ } } } } rc = _sqlite3BtreeTableMoveto(tls, *(*uintptr)(unsafe.Pointer(pC5 + 36)), int64(uint64(iKey)), 0, bp+96) (*TVdbeCursor)(unsafe.Pointer(pC5)).FmovetoTarget = iKey /* Used by OP_Delete */ if rc != SQLITE_OK { goto abort_due_to_error } } else { /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively, ** with the same key. */ if _sqlite3BtreeCursorHasHint(tls, *(*uintptr)(unsafe.Pointer(pC5 + 36)), uint32(BTREE_SEEK_EQ)) != 0 { eqOnly = int32(1) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } nField2 = *(*int32)(unsafe.Pointer(pOp + 16)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*(*TUnpackedRecord)(unsafe.Pointer(bp + 104))).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pC5)).FpKeyInfo (*(*TUnpackedRecord)(unsafe.Pointer(bp + 104))).FnField = uint16(nField2) /* The next line of code computes as follows, only faster: ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ ** r.default_rc = -1; ** }else{ ** r.default_rc = +1; ** } */ if int32(1)&(oc-int32(OP_SeekLT)) != 0 { v256 = -int32(1) } else { v256 = +libc.Int32FromInt32(1) } (*(*TUnpackedRecord)(unsafe.Pointer(bp + 104))).Fdefault_rc = int8(v256) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*(*TUnpackedRecord)(unsafe.Pointer(bp + 104))).FaMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 (*(*TUnpackedRecord)(unsafe.Pointer(bp + 104))).FeqSeen = uint8(0) rc = _sqlite3BtreeIndexMoveto(tls, *(*uintptr)(unsafe.Pointer(pC5 + 36)), bp+104, bp+96) if rc != SQLITE_OK { goto abort_due_to_error } if eqOnly != 0 && int32((*(*TUnpackedRecord)(unsafe.Pointer(bp + 104))).FeqSeen) == 0 { _ = libc.Int32FromInt32(0) goto seek_not_found } } if oc >= int32(OP_SeekGE) { _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp + 96)) < 0 || *(*int32)(unsafe.Pointer(bp + 96)) == 0 && oc == int32(OP_SeekGT) { *(*int32)(unsafe.Pointer(bp + 96)) = 0 rc = _sqlite3BtreeNext(tls, *(*uintptr)(unsafe.Pointer(pC5 + 36)), 0) if rc != SQLITE_OK { if rc == int32(SQLITE_DONE) { rc = SQLITE_OK *(*int32)(unsafe.Pointer(bp + 96)) = int32(1) } else { goto abort_due_to_error } } } else { *(*int32)(unsafe.Pointer(bp + 96)) = 0 } } else { _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp + 96)) > 0 || *(*int32)(unsafe.Pointer(bp + 96)) == 0 && oc == int32(OP_SeekLT) { *(*int32)(unsafe.Pointer(bp + 96)) = 0 rc = _sqlite3BtreePrevious(tls, *(*uintptr)(unsafe.Pointer(pC5 + 36)), 0) if rc != SQLITE_OK { if rc == int32(SQLITE_DONE) { rc = SQLITE_OK *(*int32)(unsafe.Pointer(bp + 96)) = int32(1) } else { goto abort_due_to_error } } } else { /* res might be negative because the table is empty. Check to ** see if this is the case. */ *(*int32)(unsafe.Pointer(bp + 96)) = _sqlite3BtreeEof(tls, *(*uintptr)(unsafe.Pointer(pC5 + 36))) } } goto seek_not_found seek_not_found: ; _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp + 96)) != 0 { goto jump_to_p2 } else { if eqOnly != 0 { _ = libc.Int32FromInt32(0) pOp += 20 /* Skip the OP_IdxLt or OP_IdxGT that follows */ } } goto _187 /* Opcode: SeekScan P1 P2 * * P5 ** Synopsis: Scan-ahead up to P1 rows ** ** This opcode is a prefix opcode to OP_SeekGE. In other words, this ** opcode must be immediately followed by OP_SeekGE. This constraint is ** checked by assert() statements. ** ** This opcode uses the P1 through P4 operands of the subsequent ** OP_SeekGE. In the text that follows, the operands of the subsequent ** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only ** the P1, P2 and P5 operands of this opcode are also used, and are called ** This.P1, This.P2 and This.P5. ** ** This opcode helps to optimize IN operators on a multi-column index ** where the IN operator is on the later terms of the index by avoiding ** unnecessary seeks on the btree, substituting steps to the next row ** of the b-tree instead. A correct answer is obtained if this opcode ** is omitted or is a no-op. ** ** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which ** is the desired entry that we want the cursor SeekGE.P1 to be pointing ** to. Call this SeekGE.P3/P4 row the "target". ** ** If the SeekGE.P1 cursor is not currently pointing to a valid row, ** then this opcode is a no-op and control passes through into the OP_SeekGE. ** ** If the SeekGE.P1 cursor is pointing to a valid row, then that row ** might be the target row, or it might be near and slightly before the ** target row, or it might be after the target row. If the cursor is ** currently before the target row, then this opcode attempts to position ** the cursor on or after the target row by invoking sqlite3BtreeStep() ** on the cursor between 1 and This.P1 times. ** ** The This.P5 parameter is a flag that indicates what to do if the ** cursor ends up pointing at a valid row that is past the target ** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0 ** case occurs when there are no inequality constraints to the right of ** the IN constraint. The jump to SeekGE.P2 ends the loop. The P5!=0 case ** occurs when there are inequality constraints to the right of the IN ** operator. In that case, the This.P2 will point either directly to or ** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for ** loop terminate. ** ** Possible outcomes from this opcode:
      ** **
    1. If the cursor is initially not pointed to any valid row, then ** fall through into the subsequent OP_SeekGE opcode. ** **
    2. If the cursor is left pointing to a row that is before the target ** row, even after making as many as This.P1 calls to ** sqlite3BtreeNext(), then also fall through into OP_SeekGE. ** **
    3. If the cursor is left pointing at the target row, either because it ** was at the target row to begin with or because one or more ** sqlite3BtreeNext() calls moved the cursor to the target row, ** then jump to This.P2.., ** **
    4. If the cursor started out before the target row and a call to ** to sqlite3BtreeNext() moved the cursor off the end of the index ** (indicating that the target row definitely does not exist in the ** btree) then jump to SeekGE.P2, ending the loop. ** **
    5. If the cursor ends up on a valid row that is past the target row ** (indicating that the target row does not exist in the btree) then ** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0. **
    */ _89: ; _ = libc.Int32FromInt32(0) /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first ** opcode past the OP_SeekGE itself. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pC6 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*(*TOp)(unsafe.Pointer(pOp + 1*20))).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if !(_sqlite3BtreeCursorIsValidNN(tls, *(*uintptr)(unsafe.Pointer(pC6 + 36))) != 0) { goto _187 } nStep = (*TOp)(unsafe.Pointer(pOp)).Fp1 _ = libc.Int32FromInt32(0) (*(*TUnpackedRecord)(unsafe.Pointer(bp + 144))).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pC6)).FpKeyInfo (*(*TUnpackedRecord)(unsafe.Pointer(bp + 144))).FnField = uint16(*(*int32)(unsafe.Pointer(pOp + 1*20 + 16))) (*(*TUnpackedRecord)(unsafe.Pointer(bp + 144))).Fdefault_rc = 0 (*(*TUnpackedRecord)(unsafe.Pointer(bp + 144))).FaMem = aMem + uintptr((*(*TOp)(unsafe.Pointer(pOp + 1*20))).Fp3)*40 *(*int32)(unsafe.Pointer(bp + 136)) = 0 /* Not needed. Only used to silence a warning. */ _258: ; if !(int32(1) != 0) { goto _257 } rc = _sqlite3VdbeIdxKeyCompare(tls, db, pC6, bp+144, bp+136) if rc != 0 { goto abort_due_to_error } if !(*(*int32)(unsafe.Pointer(bp + 136)) > 0 && int32((*TOp)(unsafe.Pointer(pOp)).Fp5) == 0) { goto _259 } goto seekscan_search_fail seekscan_search_fail: ; /* Jump to SeekGE.P2, ending the loop */ pOp += 20 goto jump_to_p2 _259: ; if *(*int32)(unsafe.Pointer(bp + 136)) >= 0 { /* Jump to This.P2, bypassing the OP_SeekGE opcode */ goto jump_to_p2 goto _257 } if nStep <= 0 { goto _257 } nStep-- (*TVdbeCursor)(unsafe.Pointer(pC6)).FcacheStatus = uint32(CACHE_STALE) rc = _sqlite3BtreeNext(tls, *(*uintptr)(unsafe.Pointer(pC6 + 36)), 0) if rc != 0 { if rc == int32(SQLITE_DONE) { rc = SQLITE_OK goto seekscan_search_fail } else { goto abort_due_to_error } } goto _258 _257: ; goto _187 /* Opcode: SeekHit P1 P2 P3 * * ** Synopsis: set P2<=seekHit<=P3 ** ** Increase or decrease the seekHit value for cursor P1, if necessary, ** so that it is no less than P2 and no greater than P3. ** ** The seekHit integer represents the maximum of terms in an index for which ** there is known to be at least one match. If the seekHit value is smaller ** than the total number of equality terms in an index lookup, then the ** OP_IfNoHope opcode might run to see if the IN loop can be abandoned ** early, thus saving work. This is part of the IN-early-out optimization. ** ** P1 must be a valid b-tree cursor. */ _90: ; _ = libc.Int32FromInt32(0) pC7 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TVdbeCursor)(unsafe.Pointer(pC7)).FseekHit) < (*TOp)(unsafe.Pointer(pOp)).Fp2 { (*TVdbeCursor)(unsafe.Pointer(pC7)).FseekHit = uint16((*TOp)(unsafe.Pointer(pOp)).Fp2) } else { if int32((*TVdbeCursor)(unsafe.Pointer(pC7)).FseekHit) > (*TOp)(unsafe.Pointer(pOp)).Fp3 { (*TVdbeCursor)(unsafe.Pointer(pC7)).FseekHit = uint16((*TOp)(unsafe.Pointer(pOp)).Fp3) } } goto _187 /* Opcode: IfNotOpen P1 P2 * * * ** Synopsis: if( !csr[P1] ) goto P2 ** ** If cursor P1 is not open or if P1 is set to a NULL row using the ** OP_NullRow opcode, then jump to instruction P2. Otherwise, fall through. */ _91: ; _ = libc.Int32FromInt32(0) pCur1 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) if pCur1 == uintptr(0) || (*TVdbeCursor)(unsafe.Pointer(pCur1)).FnullRow != 0 { goto jump_to_p2_and_check_for_interrupt } goto _187 /* Opcode: Found P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If ** P4>0 then register P3 is the first of P4 registers that form an unpacked ** record. ** ** Cursor P1 is on an index btree. If the record identified by P3 and P4 ** is a prefix of any entry in P1 then a jump is made to P2 and ** P1 is left pointing at the matching entry. ** ** This operation leaves the cursor in a state where it can be ** advanced in the forward direction. The Next instruction will work, ** but not the Prev instruction. ** ** See also: NotFound, NoConflict, NotExists. SeekGe */ /* Opcode: NotFound P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If ** P4>0 then register P3 is the first of P4 registers that form an unpacked ** record. ** ** Cursor P1 is on an index btree. If the record identified by P3 and P4 ** is not the prefix of any entry in P1 then a jump is made to P2. If P1 ** does contain an entry whose prefix matches the P3/P4 record then control ** falls through to the next instruction and P1 is left pointing at the ** matching entry. ** ** This operation leaves the cursor in a state where it cannot be ** advanced in either direction. In other words, the Next and Prev ** opcodes do not work after this operation. ** ** See also: Found, NotExists, NoConflict, IfNoHope */ /* Opcode: IfNoHope P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** ** Register P3 is the first of P4 registers that form an unpacked ** record. Cursor P1 is an index btree. P2 is a jump destination. ** In other words, the operands to this opcode are the same as the ** operands to OP_NotFound and OP_IdxGT. ** ** This opcode is an optimization attempt only. If this opcode always ** falls through, the correct answer is still obtained, but extra work ** is performed. ** ** A value of N in the seekHit flag of cursor P1 means that there exists ** a key P3:N that will match some record in the index. We want to know ** if it is possible for a record P3:P4 to match some record in the ** index. If it is not possible, we can skip some work. So if seekHit ** is less than P4, attempt to find out if a match is possible by running ** OP_NotFound. ** ** This opcode is used in IN clause processing for a multi-column key. ** If an IN clause is attached to an element of the key other than the ** left-most element, and if there are no matches on the most recent ** seek over the whole key, then it might be that one of the key element ** to the left is prohibiting a match, and hence there is "no hope" of ** any match regardless of how many IN clause elements are checked. ** In such a case, we abandon the IN clause search early, using this ** opcode. The opcode name comes from the fact that the ** jump is taken if there is "no hope" of achieving a match. ** ** See also: NotFound, SeekHit */ /* Opcode: NoConflict P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If ** P4>0 then register P3 is the first of P4 registers that form an unpacked ** record. ** ** Cursor P1 is on an index btree. If the record identified by P3 and P4 ** contains any NULL value, jump immediately to P2. If all terms of the ** record are not-NULL then a check is done to determine if any row in the ** P1 index btree has a matching key prefix. If there are no matches, jump ** immediately to P2. If there is a match, fall through and leave the P1 ** cursor pointing to the matching row. ** ** This opcode is similar to OP_NotFound with the exceptions that the ** branch is always taken if any part of the search key input is NULL. ** ** This operation leaves the cursor in a state where it cannot be ** advanced in either direction. In other words, the Next and Prev ** opcodes do not work after this operation. ** ** See also: NotFound, Found, NotExists */ _92: ; _ = libc.Int32FromInt32(0) pC8 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) if int32((*TVdbeCursor)(unsafe.Pointer(pC8)).FseekHit) >= *(*int32)(unsafe.Pointer(pOp + 16)) { goto _187 } _95: ; /* jump, in3, ncycle */ _94: ; /* jump, in3, ncycle */ _93: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pC9 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) (*(*TUnpackedRecord)(unsafe.Pointer(bp + 176))).FaMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*(*TUnpackedRecord)(unsafe.Pointer(bp + 176))).FnField = uint16(*(*int32)(unsafe.Pointer(pOp + 16))) if int32((*(*TUnpackedRecord)(unsafe.Pointer(bp + 176))).FnField) > 0 { /* Key values in an array of registers */ (*(*TUnpackedRecord)(unsafe.Pointer(bp + 176))).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pC9)).FpKeyInfo (*(*TUnpackedRecord)(unsafe.Pointer(bp + 176))).Fdefault_rc = 0 rc = _sqlite3BtreeIndexMoveto(tls, *(*uintptr)(unsafe.Pointer(pC9 + 36)), bp+176, pC9+28) } else { /* Composite key generated by OP_MakeRecord */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer((*(*TUnpackedRecord)(unsafe.Pointer(bp + 176))).FaMem)).Fflags)&int32(MEM_Zero) != 0 { v260 = _sqlite3VdbeMemExpandBlob(tls, (*(*TUnpackedRecord)(unsafe.Pointer(bp + 176))).FaMem) } else { v260 = 0 } rc = v260 _ = libc.Int32FromInt32(0) if rc != 0 { goto no_mem } pIdxKey = _sqlite3VdbeAllocUnpackedRecord(tls, (*TVdbeCursor)(unsafe.Pointer(pC9)).FpKeyInfo) if pIdxKey == uintptr(0) { goto no_mem } _sqlite3VdbeRecordUnpack(tls, (*TVdbeCursor)(unsafe.Pointer(pC9)).FpKeyInfo, (*TMem)(unsafe.Pointer((*(*TUnpackedRecord)(unsafe.Pointer(bp + 176))).FaMem)).Fn, (*TMem)(unsafe.Pointer((*(*TUnpackedRecord)(unsafe.Pointer(bp + 176))).FaMem)).Fz, pIdxKey) (*TUnpackedRecord)(unsafe.Pointer(pIdxKey)).Fdefault_rc = 0 rc = _sqlite3BtreeIndexMoveto(tls, *(*uintptr)(unsafe.Pointer(pC9 + 36)), pIdxKey, pC9+28) _sqlite3DbFreeNN(tls, db, pIdxKey) } if rc != SQLITE_OK { goto abort_due_to_error } alreadyExists = libc.BoolInt32((*TVdbeCursor)(unsafe.Pointer(pC9)).FseekResult == 0) (*TVdbeCursor)(unsafe.Pointer(pC9)).FnullRow = uint8(int32(1) - alreadyExists) (*TVdbeCursor)(unsafe.Pointer(pC9)).FdeferredMoveto = uint8(0) (*TVdbeCursor)(unsafe.Pointer(pC9)).FcacheStatus = uint32(CACHE_STALE) if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Found) { if alreadyExists != 0 { goto jump_to_p2 } } else { if !(alreadyExists != 0) { goto jump_to_p2 } if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_NoConflict) { /* For the OP_NoConflict opcode, take the jump if any of the ** input fields are NULL, since any key with a NULL will not ** conflict */ ii1 = 0 for { if !(ii1 < int32((*(*TUnpackedRecord)(unsafe.Pointer(bp + 176))).FnField)) { break } if int32((*(*TMem)(unsafe.Pointer((*(*TUnpackedRecord)(unsafe.Pointer(bp + 176))).FaMem + uintptr(ii1)*40))).Fflags)&int32(MEM_Null) != 0 { goto jump_to_p2 } goto _261 _261: ; ii1++ } } if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_IfNoHope) { (*TVdbeCursor)(unsafe.Pointer(pC9)).FseekHit = uint16(*(*int32)(unsafe.Pointer(pOp + 16))) } } goto _187 /* Opcode: SeekRowid P1 P2 P3 * * ** Synopsis: intkey=r[P3] ** ** P1 is the index of a cursor open on an SQL table btree (with integer ** keys). If register P3 does not contain an integer or if P1 does not ** contain a record with rowid P3 then jump immediately to P2. ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain ** a record with rowid P3 then ** leave the cursor pointing at that record and fall through to the next ** instruction. ** ** The OP_NotExists opcode performs the same operation, but with OP_NotExists ** the P3 register must be guaranteed to contain an integer value. With this ** opcode, register P3 might not contain an integer. ** ** The OP_NotFound opcode performs the same operation on index btrees ** (with arbitrary multi-value keys). ** ** This opcode leaves the cursor in a state where it cannot be advanced ** in either direction. In other words, the Next and Prev opcodes will ** not work following this opcode. ** ** See also: Found, NotFound, NoConflict, SeekRowid */ /* Opcode: NotExists P1 P2 P3 * * ** Synopsis: intkey=r[P3] ** ** P1 is the index of a cursor open on an SQL table btree (with integer ** keys). P3 is an integer rowid. If P1 does not contain a record with ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then ** leave the cursor pointing at that record and fall through to the next ** instruction. ** ** The OP_SeekRowid opcode performs the same operation but also allows the ** P3 register to contain a non-integer value, in which case the jump is ** always taken. This opcode requires that P3 always contain an integer. ** ** The OP_NotFound opcode performs the same operation on index btrees ** (with arbitrary multi-value keys). ** ** This opcode leaves the cursor in a state where it cannot be advanced ** in either direction. In other words, the Next and Prev opcodes will ** not work following this opcode. ** ** See also: Found, NotFound, NoConflict, SeekRowid */ _97: ; pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 if int32((*TMem)(unsafe.Pointer(pIn3)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) == 0 { /* If pIn3->u.i does not contain an integer, compute iKey as the ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted ** into an integer without loss of information. Take care to avoid ** changing the datatype of pIn3, however, as it is used by other ** parts of the prepared statement. */ *(*TMem)(unsafe.Pointer(bp + 216)) = TMem{} /* If pIn3->u.i does not contain an integer, compute iKey as the ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted ** into an integer without loss of information. Take care to avoid ** changing the datatype of pIn3, however, as it is used by other ** parts of the prepared statement. */ *(*Tsqlite3_value1)(unsafe.Pointer(bp + 216)) = *(*TMem)(unsafe.Pointer(pIn3)) _applyAffinity(tls, bp+216, uint8(SQLITE_AFF_NUMERIC), encoding) if int32((*(*TMem)(unsafe.Pointer(bp + 216))).Fflags)&int32(MEM_Int) == 0 { goto jump_to_p2 } iKey1 = uint64(*(*Ti64)(unsafe.Pointer(bp + 216))) goto notExistsWithKey } _96: ; /* jump, in3, ncycle */ pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) iKey1 = uint64(*(*Ti64)(unsafe.Pointer(pIn3))) goto notExistsWithKey notExistsWithKey: ; pC10 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pCrsr2 = *(*uintptr)(unsafe.Pointer(pC10 + 36)) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp + 208)) = 0 rc = _sqlite3BtreeTableMoveto(tls, pCrsr2, int64(iKey1), 0, bp+208) _ = libc.Int32FromInt32(0) (*TVdbeCursor)(unsafe.Pointer(pC10)).FmovetoTarget = int64(iKey1) /* Used by OP_Delete */ (*TVdbeCursor)(unsafe.Pointer(pC10)).FnullRow = uint8(0) (*TVdbeCursor)(unsafe.Pointer(pC10)).FcacheStatus = uint32(CACHE_STALE) (*TVdbeCursor)(unsafe.Pointer(pC10)).FdeferredMoveto = uint8(0) (*TVdbeCursor)(unsafe.Pointer(pC10)).FseekResult = *(*int32)(unsafe.Pointer(bp + 208)) if *(*int32)(unsafe.Pointer(bp + 208)) != 0 { _ = libc.Int32FromInt32(0) if (*TOp)(unsafe.Pointer(pOp)).Fp2 == 0 { rc = _sqlite3CorruptError(tls, int32(98111)) } else { goto jump_to_p2 } } if rc != 0 { goto abort_due_to_error } goto _187 /* Opcode: Sequence P1 P2 * * * ** Synopsis: r[P2]=cursor[P1].ctr++ ** ** Find the next available sequence number for cursor P1. ** Write the sequence number into register P2. ** The sequence number on the cursor is incremented after this ** instruction. */ _98: ; /* out2 */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pOut = _out2Prerelease(tls, p, pOp) v263 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) + 16 v262 = *(*Ti64)(unsafe.Pointer(v263)) *(*Ti64)(unsafe.Pointer(v263))++ *(*Ti64)(unsafe.Pointer(pOut)) = v262 goto _187 /* Opcode: NewRowid P1 P2 P3 * * ** Synopsis: r[P2]=rowid ** ** Get a new integer record number (a.k.a "rowid") used as the key to a table. ** The record number is not previously used as a key in the database ** table that cursor P1 points to. The new record number is written ** written to register P2. ** ** If P3>0 then P3 is a register in the root frame of this VDBE that holds ** the largest previously generated record number. No new record numbers are ** allowed to be less than this value. When this value reaches its maximum, ** an SQLITE_FULL error is generated. The P3 register is updated with the ' ** generated record number. This P3 mechanism is used to help implement the ** AUTOINCREMENT feature. */ _99: ; /* Root frame of VDBE */ *(*Ti64)(unsafe.Pointer(bp + 256)) = 0 *(*int32)(unsafe.Pointer(bp + 264)) = 0 pOut = _out2Prerelease(tls, p, pOp) _ = libc.Int32FromInt32(0) pC11 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* The next rowid or record number (different terms for the same ** thing) is obtained in a two-step algorithm. ** ** First we attempt to find the largest existing rowid and add one ** to that. But if the largest existing rowid is already the maximum ** positive integer, we have to fall through to the second ** probabilistic algorithm ** ** The second algorithm is to select a rowid at random and see if ** it already exists in the table. If it does not exist, we have ** succeeded. If the random rowid does exist, we select a new one ** and try again, up to 100 times. */ _ = libc.Int32FromInt32(0) /* Some compilers complain about constants of the form 0x7fffffffffffffff. ** Others complain about 0x7ffffffffffffffffLL. The following macro seems ** to provide the constant while making all compilers happy. */ if !(int32(TBool(*(*uint8)(unsafe.Pointer(pC11 + 8))&0x2>>1)) != 0) { rc = _sqlite3BtreeLast(tls, *(*uintptr)(unsafe.Pointer(pC11 + 36)), bp+264) if rc != SQLITE_OK { goto abort_due_to_error } if *(*int32)(unsafe.Pointer(bp + 264)) != 0 { *(*Ti64)(unsafe.Pointer(bp + 256)) = int64(1) /* IMP: R-61914-48074 */ } else { _ = libc.Int32FromInt32(0) *(*Ti64)(unsafe.Pointer(bp + 256)) = _sqlite3BtreeIntegerKey(tls, *(*uintptr)(unsafe.Pointer(pC11 + 36))) if *(*Ti64)(unsafe.Pointer(bp + 256)) >= int64(libc.Uint64FromInt32(0x7fffffff)<>1)) != 0 { rc = int32(SQLITE_FULL) /* IMP: R-17817-00630 */ goto abort_due_to_error } if *(*Ti64)(unsafe.Pointer(bp + 256)) < *(*Ti64)(unsafe.Pointer(pMem))+int64(1) { *(*Ti64)(unsafe.Pointer(bp + 256)) = *(*Ti64)(unsafe.Pointer(pMem)) + int64(1) } *(*Ti64)(unsafe.Pointer(pMem)) = *(*Ti64)(unsafe.Pointer(bp + 256)) } if int32(TBool(*(*uint8)(unsafe.Pointer(pC11 + 8))&0x2>>1)) != 0 { /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the ** largest possible integer (9223372036854775807) then the database ** engine starts picking positive candidate ROWIDs at random until ** it finds one that is not previously used. */ _ = libc.Int32FromInt32(0) /* We cannot be in random rowid mode if this is ** an AUTOINCREMENT table. */ cnt1 = 0 for { Xsqlite3_randomness(tls, int32(8), bp+256) *(*Ti64)(unsafe.Pointer(bp + 256)) &= int64(libc.Uint64FromInt32(0x7fffffff)<> libc.Int32FromInt32(1) *(*Ti64)(unsafe.Pointer(bp + 256))++ /* Ensure that v is greater than zero */ goto _268 _268: ; v265 = _sqlite3BtreeTableMoveto(tls, *(*uintptr)(unsafe.Pointer(pC11 + 36)), int64(uint64(*(*Ti64)(unsafe.Pointer(bp + 256)))), 0, bp+264) rc = v265 if v267 = v265 == SQLITE_OK && *(*int32)(unsafe.Pointer(bp + 264)) == 0; v267 { cnt1++ v266 = cnt1 } if !(v267 && v266 < int32(100)) { break } } if rc != 0 { goto abort_due_to_error } if *(*int32)(unsafe.Pointer(bp + 264)) == 0 { rc = int32(SQLITE_FULL) /* IMP: R-38219-53002 */ goto abort_due_to_error } _ = libc.Int32FromInt32(0) /* EV: R-40812-03570 */ } (*TVdbeCursor)(unsafe.Pointer(pC11)).FdeferredMoveto = uint8(0) (*TVdbeCursor)(unsafe.Pointer(pC11)).FcacheStatus = uint32(CACHE_STALE) *(*Ti64)(unsafe.Pointer(pOut)) = *(*Ti64)(unsafe.Pointer(bp + 256)) goto _187 /* Opcode: Insert P1 P2 P3 P4 P5 ** Synopsis: intkey=r[P3] data=r[P2] ** ** Write an entry into the table of cursor P1. A new entry is ** created if it doesn't already exist or the data for an existing ** entry is overwritten. The data is the value MEM_Blob stored in register ** number P2. The key is stored in register P3. The key must ** be a MEM_Int. ** ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, ** then rowid is stored for subsequent return by the ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). ** ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might ** run faster by avoiding an unnecessary seek on cursor P1. However, ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior ** seeks on the cursor or if the most recent seek used a key equal to P3. ** ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an ** UPDATE operation. Otherwise (if the flag is clear) then this opcode ** is part of an INSERT operation. The difference is only important to ** the update hook. ** ** Parameter P4 may point to a Table structure, or may be NULL. If it is ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked ** following a successful insert. ** ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically ** allocated, then ownership of P2 is transferred to the pseudo-cursor ** and register P2 becomes ephemeral. If the cursor is changed, the ** value of register P2 will then change. Make sure this does not ** cause any problems.) ** ** This instruction only works on tables. The equivalent instruction ** for indices is OP_IdxInsert. */ _100: ; /* Payload to be inserted */ pData = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pC12 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pKey = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*(*TBtreePayload)(unsafe.Pointer(bp + 272))).FnKey = *(*Ti64)(unsafe.Pointer(pKey)) if int32((*TOp)(unsafe.Pointer(pOp)).Fp4type) == -int32(5) && ((*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 || (*Tsqlite3)(unsafe.Pointer(db)).FxUpdateCallback != 0) { _ = libc.Int32FromInt32(0) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TVdbeCursor)(unsafe.Pointer(pC12)).FiDb)*16))).FzDbSName pTab1 = *(*uintptr)(unsafe.Pointer(pOp + 16)) _ = libc.Int32FromInt32(0) } else { pTab1 = uintptr(0) zDb = uintptr(0) } /* Invoke the pre-update hook, if any */ if pTab1 != 0 { if (*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 && !(int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&libc.Int32FromInt32(OPFLAG_ISUPDATE) != 0) { _sqlite3VdbePreUpdateHook(tls, p, pC12, int32(SQLITE_INSERT), zDb, pTab1, (*(*TBtreePayload)(unsafe.Pointer(bp + 272))).FnKey, (*TOp)(unsafe.Pointer(pOp)).Fp2, -int32(1)) } if (*Tsqlite3)(unsafe.Pointer(db)).FxUpdateCallback == uintptr(0) || (*TTable)(unsafe.Pointer(pTab1)).FaCol == uintptr(0) { /* Prevent post-update hook from running in cases when it should not */ pTab1 = uintptr(0) } } if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_ISNOOP) != 0 { goto _187 } _ = libc.Int32FromInt32(0) if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_NCHANGE) != 0 { (*TVdbe)(unsafe.Pointer(p)).FnChange++ if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_LASTROWID) != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FlastRowid = (*(*TBtreePayload)(unsafe.Pointer(bp + 272))).FnKey } } _ = libc.Int32FromInt32(0) (*(*TBtreePayload)(unsafe.Pointer(bp + 272))).FpData = (*TMem)(unsafe.Pointer(pData)).Fz (*(*TBtreePayload)(unsafe.Pointer(bp + 272))).FnData = (*TMem)(unsafe.Pointer(pData)).Fn if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_USESEEKRESULT) != 0 { v269 = (*TVdbeCursor)(unsafe.Pointer(pC12)).FseekResult } else { v269 = 0 } seekResult = v269 if int32((*TMem)(unsafe.Pointer(pData)).Fflags)&int32(MEM_Zero) != 0 { (*(*TBtreePayload)(unsafe.Pointer(bp + 272))).FnZero = *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pData)).Fu)) } else { (*(*TBtreePayload)(unsafe.Pointer(bp + 272))).FnZero = 0 } (*(*TBtreePayload)(unsafe.Pointer(bp + 272))).FpKey = uintptr(0) _ = libc.Int32FromInt32(0) rc = _sqlite3BtreeInsert(tls, *(*uintptr)(unsafe.Pointer(pC12 + 36)), bp+272, int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&(libc.Int32FromInt32(OPFLAG_APPEND)|libc.Int32FromInt32(OPFLAG_SAVEPOSITION)|libc.Int32FromInt32(OPFLAG_PREFORMAT)), seekResult) (*TVdbeCursor)(unsafe.Pointer(pC12)).FdeferredMoveto = uint8(0) (*TVdbeCursor)(unsafe.Pointer(pC12)).FcacheStatus = uint32(CACHE_STALE) colCacheCtr++ /* Invoke the update-hook if required. */ if rc != 0 { goto abort_due_to_error } if pTab1 != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_ISUPDATE) != 0 { v270 = int32(SQLITE_UPDATE) } else { v270 = int32(SQLITE_INSERT) } (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, Tsqlite_int64))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxUpdateCallback})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpUpdateArg, v270, zDb, (*TTable)(unsafe.Pointer(pTab1)).FzName, (*(*TBtreePayload)(unsafe.Pointer(bp + 272))).FnKey) } goto _187 /* Opcode: RowCell P1 P2 P3 * * ** ** P1 and P2 are both open cursors. Both must be opened on the same type ** of table - intkey or index. This opcode is used as part of copying ** the current row from P2 into P1. If the cursors are opened on intkey ** tables, register P3 contains the rowid to use with the new record in ** P1. If they are opened on index tables, P3 is not used. ** ** This opcode must be followed by either an Insert or InsertIdx opcode ** with the OPFLAG_PREFORMAT flag set to complete the insert operation. */ _101: ; /* Rowid value to insert with */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pDest1 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) pSrc = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*4)) if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 { v271 = *(*Ti64)(unsafe.Pointer(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40)) } else { v271 = 0 } iKey2 = v271 rc = _sqlite3BtreeTransferRow(tls, *(*uintptr)(unsafe.Pointer(pDest1 + 36)), *(*uintptr)(unsafe.Pointer(pSrc + 36)), iKey2) if rc != SQLITE_OK { goto abort_due_to_error } goto _187 /* Opcode: Delete P1 P2 P3 P4 P5 ** ** Delete the record at which the P1 cursor is currently pointing. ** ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then ** the cursor will be left pointing at either the next or the previous ** record in the table. If it is left pointing at the next record, then ** the next Next instruction will be a no-op. As a result, in this case ** it is ok to delete a record from within a Next loop. If ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be ** left in an undefined state. ** ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this ** delete is one of several associated with deleting a table row and ** all its associated index entries. Exactly one of those deletes is ** the "primary" delete. The others are all on OPFLAG_FORDELETE ** cursors or else are marked with the AUXDELETE flag. ** ** If the OPFLAG_NCHANGE (0x01) flag of P2 (NB: P2 not P5) is set, then ** the row change count is incremented (otherwise not). ** ** If the OPFLAG_ISNOOP (0x40) flag of P2 (not P5!) is set, then the ** pre-update-hook for deletes is run, but the btree is otherwise unchanged. ** This happens when the OP_Delete is to be shortly followed by an OP_Insert ** with the same key, causing the btree entry to be overwritten. ** ** P1 must not be pseudo-table. It has to be a real table with ** multiple rows. ** ** If P4 is not NULL then it points to a Table object. In this case either ** the update or pre-update hook, or both, may be invoked. The P1 cursor must ** have been positioned using OP_NotFound prior to invoking this opcode in ** this case. Specifically, if one is configured, the pre-update hook is ** invoked if P4 is not NULL. The update-hook is invoked if one is configured, ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2. ** ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address ** of the memory cell that contains the value that the rowid of the row will ** be set to by the update. */ _102: ; opflags = (*TOp)(unsafe.Pointer(pOp)).Fp2 _ = libc.Int32FromInt32(0) pC13 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If the update-hook or pre-update-hook will be invoked, set zDb to ** the name of the db to pass as to it. Also set local pTab to a copy ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set ** VdbeCursor.movetoTarget to the current rowid. */ if int32((*TOp)(unsafe.Pointer(pOp)).Fp4type) == -int32(5) && ((*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 || (*Tsqlite3)(unsafe.Pointer(db)).FxUpdateCallback != 0) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) zDb1 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TVdbeCursor)(unsafe.Pointer(pC13)).FiDb)*16))).FzDbSName pTab2 = *(*uintptr)(unsafe.Pointer(pOp + 16)) if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_SAVEPOSITION) != 0 && (*TVdbeCursor)(unsafe.Pointer(pC13)).FisTable != 0 { (*TVdbeCursor)(unsafe.Pointer(pC13)).FmovetoTarget = _sqlite3BtreeIntegerKey(tls, *(*uintptr)(unsafe.Pointer(pC13 + 36))) } } else { zDb1 = uintptr(0) pTab2 = uintptr(0) } /* Invoke the pre-update-hook if required. */ _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 && pTab2 != 0 { _ = libc.Int32FromInt32(0) if opflags&int32(OPFLAG_ISUPDATE) != 0 { v272 = int32(SQLITE_UPDATE) } else { v272 = int32(SQLITE_DELETE) } _sqlite3VdbePreUpdateHook(tls, p, pC13, v272, zDb1, pTab2, (*TVdbeCursor)(unsafe.Pointer(pC13)).FmovetoTarget, (*TOp)(unsafe.Pointer(pOp)).Fp3, -int32(1)) } if opflags&int32(OPFLAG_ISNOOP) != 0 { goto _187 } /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3BtreeDelete(tls, *(*uintptr)(unsafe.Pointer(pC13 + 36)), uint8((*TOp)(unsafe.Pointer(pOp)).Fp5)) (*TVdbeCursor)(unsafe.Pointer(pC13)).FcacheStatus = uint32(CACHE_STALE) colCacheCtr++ (*TVdbeCursor)(unsafe.Pointer(pC13)).FseekResult = 0 if rc != 0 { goto abort_due_to_error } /* Invoke the update-hook if required. */ if opflags&int32(OPFLAG_NCHANGE) != 0 { (*TVdbe)(unsafe.Pointer(p)).FnChange++ if (*Tsqlite3)(unsafe.Pointer(db)).FxUpdateCallback != 0 && pTab2 != uintptr(0) && (*TTable)(unsafe.Pointer(pTab2)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, Tsqlite_int64))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxUpdateCallback})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpUpdateArg, int32(SQLITE_DELETE), zDb1, (*TTable)(unsafe.Pointer(pTab2)).FzName, (*TVdbeCursor)(unsafe.Pointer(pC13)).FmovetoTarget) _ = libc.Int32FromInt32(0) } } goto _187 /* Opcode: ResetCount * * * * * ** ** The value of the change counter is copied to the database handle ** change counter (returned by subsequent calls to sqlite3_changes()). ** Then the VMs internal change counter resets to 0. ** This is used by trigger programs. */ _103: ; _sqlite3VdbeSetChanges(tls, db, (*TVdbe)(unsafe.Pointer(p)).FnChange) (*TVdbe)(unsafe.Pointer(p)).FnChange = 0 goto _187 /* Opcode: SorterCompare P1 P2 P3 P4 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 ** ** P1 is a sorter cursor. This instruction compares a prefix of the ** record blob in register P3 against a prefix of the entry that ** the sorter cursor currently points to. Only the first P4 fields ** of r[P3] and the sorter record are compared. ** ** If either P3 or the sorter contains a NULL in one of their significant ** fields (not counting the P4 fields at the end which are ignored) then ** the comparison is assumed to be equal. ** ** Fall through to next instruction if the two records compare equal to ** each other. Jump to P2 if they are different. */ _104: ; pC14 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 nKeyCol = *(*int32)(unsafe.Pointer(pOp + 16)) *(*int32)(unsafe.Pointer(bp + 312)) = 0 rc = _sqlite3VdbeSorterCompare(tls, pC14, pIn3, nKeyCol, bp+312) if rc != 0 { goto abort_due_to_error } if *(*int32)(unsafe.Pointer(bp + 312)) != 0 { goto jump_to_p2 } goto _187 /* Opcode: SorterData P1 P2 P3 * * ** Synopsis: r[P2]=data ** ** Write into register P2 the current sorter data for sorter cursor P1. ** Then clear the column header cache on cursor P3. ** ** This opcode is normally used to move a record out of the sorter and into ** a register that is the source for a pseudo-table cursor created using ** OpenPseudo. That pseudo-table cursor is the one that is identified by ** parameter P3. Clearing the P3 column cache as part of this opcode saves ** us from having to issue a separate NullRow instruction to clear that cache. */ _105: ; pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 pC15 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) rc = _sqlite3VdbeSorterRowkey(tls, pC15, pOut) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if rc != 0 { goto abort_due_to_error } (*TVdbeCursor)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*4)))).FcacheStatus = uint32(CACHE_STALE) goto _187 /* Opcode: RowData P1 P2 P3 * * ** Synopsis: r[P2]=data ** ** Write into register P2 the complete row content for the row at ** which cursor P1 is currently pointing. ** There is no interpretation of the data. ** It is just copied onto the P2 register exactly as ** it is found in the database file. ** ** If cursor P1 is an index, then the content is the key of the row. ** If cursor P2 is a table, then the content extracted is the data. ** ** If the P1 cursor must be pointing to a valid row (not a NULL row) ** of a real table, not a pseudo-table. ** ** If P3!=0 then this opcode is allowed to make an ephemeral pointer ** into the database page. That means that the content of the output ** register will be invalidated as soon as the cursor moves - including ** moves caused by other cursors that "save" the current cursors ** position in order that they can write to the same table. If P3==0 ** then a copy of the data is made into memory. P3!=0 is faster, but ** P3==0 is safer. ** ** If P3!=0 then the content of the P2 register is unsuitable for use ** in OP_Result and any OP_Result will invalidate the P2 register content. ** The P2 register content is invalidated by opcodes like OP_Function or ** by any use of another cursor pointing to the same table. */ _106: ; pOut = _out2Prerelease(tls, p, pOp) _ = libc.Int32FromInt32(0) pC16 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pCrsr3 = *(*uintptr)(unsafe.Pointer(pC16 + 36)) /* The OP_RowData opcodes always follow OP_NotExists or ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions ** that might invalidate the cursor. ** If this where not the case, on of the following assert()s ** would fail. Should this ever change (because of changes in the code ** generator) then the fix would be to insert a call to ** sqlite3VdbeCursorMoveto(). */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) n3 = _sqlite3BtreePayloadSize(tls, pCrsr3) if n3 > uint32(*(*int32)(unsafe.Pointer(db + 120))) { goto too_big } rc = _sqlite3VdbeMemFromBtreeZeroOffset(tls, pCrsr3, n3, pOut) if rc != 0 { goto abort_due_to_error } if !((*TOp)(unsafe.Pointer(pOp)).Fp3 != 0) { if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&int32(MEM_Ephem) != 0 && _sqlite3VdbeMemMakeWriteable(tls, pOut) != 0 { goto no_mem } } goto _187 /* Opcode: Rowid P1 P2 * * * ** Synopsis: r[P2]=PX rowid of P1 ** ** Store in register P2 an integer which is the key of the table entry that ** P1 is currently point to. ** ** P1 can be either an ordinary table or a virtual table. There used to ** be a separate OP_VRowid opcode for use with virtual tables, but this ** one opcode now works for both table types. */ _107: ; pOut = _out2Prerelease(tls, p, pOp) _ = libc.Int32FromInt32(0) pC17 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TVdbeCursor)(unsafe.Pointer(pC17)).FnullRow != 0 { (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Null) goto _187 } else { if (*TVdbeCursor)(unsafe.Pointer(pC17)).FdeferredMoveto != 0 { *(*Ti64)(unsafe.Pointer(bp + 320)) = (*TVdbeCursor)(unsafe.Pointer(pC17)).FmovetoTarget } else { if int32((*TVdbeCursor)(unsafe.Pointer(pC17)).FeCurType) == int32(CURTYPE_VTAB) { _ = libc.Int32FromInt32(0) pVtab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pC17 + 36)))).FpVtab pModule = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FpModule _ = libc.Int32FromInt32(0) rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule)).FxRowid})))(tls, *(*uintptr)(unsafe.Pointer(pC17 + 36)), bp+320) _sqlite3VtabImportErrmsg(tls, p, pVtab) if rc != 0 { goto abort_due_to_error } } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3VdbeCursorRestore(tls, pC17) if rc != 0 { goto abort_due_to_error } if (*TVdbeCursor)(unsafe.Pointer(pC17)).FnullRow != 0 { (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Null) goto _187 } *(*Ti64)(unsafe.Pointer(bp + 320)) = _sqlite3BtreeIntegerKey(tls, *(*uintptr)(unsafe.Pointer(pC17 + 36))) } } } *(*Ti64)(unsafe.Pointer(pOut)) = *(*Ti64)(unsafe.Pointer(bp + 320)) goto _187 /* Opcode: NullRow P1 * * * * ** ** Move the cursor P1 to a null row. Any OP_Column operations ** that occur while the cursor is on the null row will always ** write a NULL. ** ** If cursor P1 is not previously opened, open it now to a special ** pseudo-cursor that always returns NULL for every column. */ _108: ; _ = libc.Int32FromInt32(0) pC18 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) if pC18 == uintptr(0) { /* If the cursor is not already open, create a special kind of ** pseudo-cursor that always gives null rows. */ pC18 = _allocateCursor(tls, p, (*TOp)(unsafe.Pointer(pOp)).Fp1, int32(1), uint8(CURTYPE_PSEUDO)) if pC18 == uintptr(0) { goto no_mem } (*TVdbeCursor)(unsafe.Pointer(pC18)).FseekResult = 0 (*TVdbeCursor)(unsafe.Pointer(pC18)).FisTable = uint8(1) libc.SetBitFieldPtr8Uint32(pC18+8, libc.Uint32FromInt32(1), 3, 0x8) *(*uintptr)(unsafe.Pointer(pC18 + 36)) = _sqlite3BtreeFakeValidCursor(tls) } (*TVdbeCursor)(unsafe.Pointer(pC18)).FnullRow = uint8(1) (*TVdbeCursor)(unsafe.Pointer(pC18)).FcacheStatus = uint32(CACHE_STALE) if int32((*TVdbeCursor)(unsafe.Pointer(pC18)).FeCurType) == CURTYPE_BTREE { _ = libc.Int32FromInt32(0) _sqlite3BtreeClearCursor(tls, *(*uintptr)(unsafe.Pointer(pC18 + 36))) } goto _187 /* Opcode: SeekEnd P1 * * * * ** ** Position cursor P1 at the end of the btree for the purpose of ** appending a new entry onto the btree. ** ** It is assumed that the cursor is used only for appending and so ** if the cursor is valid, then the cursor must already be pointing ** at the end of the btree and so no changes are made to ** the cursor. */ /* Opcode: Last P1 P2 * * * ** ** The next use of the Rowid or Column or Prev instruction for P1 ** will refer to the last entry in the database table or index. ** If the table or index is empty and P2>0, then jump immediately to P2. ** If P2 is 0 or if the table or index is not empty, fall through ** to the following instruction. ** ** This opcode leaves the cursor configured to move in reverse order, ** from the end toward the beginning. In other words, the cursor is ** configured to use Prev, not Next. */ _110: ; /* ncycle */ _109: ; _ = libc.Int32FromInt32(0) pC19 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pCrsr4 = *(*uintptr)(unsafe.Pointer(pC19 + 36)) *(*int32)(unsafe.Pointer(bp + 328)) = 0 _ = libc.Int32FromInt32(0) if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_SeekEnd) { _ = libc.Int32FromInt32(0) (*TVdbeCursor)(unsafe.Pointer(pC19)).FseekResult = -int32(1) if _sqlite3BtreeCursorIsValidNN(tls, pCrsr4) != 0 { goto _187 } } rc = _sqlite3BtreeLast(tls, pCrsr4, bp+328) (*TVdbeCursor)(unsafe.Pointer(pC19)).FnullRow = uint8(*(*int32)(unsafe.Pointer(bp + 328))) (*TVdbeCursor)(unsafe.Pointer(pC19)).FdeferredMoveto = uint8(0) (*TVdbeCursor)(unsafe.Pointer(pC19)).FcacheStatus = uint32(CACHE_STALE) if rc != 0 { goto abort_due_to_error } if (*TOp)(unsafe.Pointer(pOp)).Fp2 > 0 { if *(*int32)(unsafe.Pointer(bp + 328)) != 0 { goto jump_to_p2 } } goto _187 /* Opcode: IfSmaller P1 P2 P3 * * ** ** Estimate the number of rows in the table P1. Jump to P2 if that ** estimate is less than approximately 2**(0.1*P3). */ _111: ; _ = libc.Int32FromInt32(0) pC20 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) pCrsr5 = *(*uintptr)(unsafe.Pointer(pC20 + 36)) _ = libc.Int32FromInt32(0) rc = _sqlite3BtreeFirst(tls, pCrsr5, bp+332) if rc != 0 { goto abort_due_to_error } if *(*int32)(unsafe.Pointer(bp + 332)) == 0 { sz = _sqlite3BtreeRowCountEst(tls, pCrsr5) if sz >= 0 && int32(_sqlite3LogEst(tls, uint64(sz))) < (*TOp)(unsafe.Pointer(pOp)).Fp3 { *(*int32)(unsafe.Pointer(bp + 332)) = int32(1) } } if *(*int32)(unsafe.Pointer(bp + 332)) != 0 { goto jump_to_p2 } goto _187 /* Opcode: SorterSort P1 P2 * * * ** ** After all records have been inserted into the Sorter object ** identified by P1, invoke this opcode to actually do the sorting. ** Jump to P2 if there are no records to be sorted. ** ** This opcode is an alias for OP_Sort and OP_Rewind that is used ** for Sorter objects. */ /* Opcode: Sort P1 P2 * * * ** ** This opcode does exactly the same thing as OP_Rewind except that ** it increments an undocumented global variable used for testing. ** ** Sorting is accomplished by writing records into a sorting index, ** then rewinding that index and playing it back from beginning to ** end. We use the OP_Sort opcode instead of OP_Rewind to do the ** rewinding so that the global variable will be incremented and ** regression tests can determine whether or not the optimizer is ** correctly optimizing out sorts. */ _113: ; /* jump ncycle */ _112: ; /* jump ncycle */ *(*Tu32)(unsafe.Pointer(p + 164 + 2*4))++ /* Opcode: Rewind P1 P2 * * * ** ** The next use of the Rowid or Column or Next instruction for P1 ** will refer to the first entry in the database table or index. ** If the table or index is empty, jump immediately to P2. ** If the table or index is not empty, fall through to the following ** instruction. ** ** If P2 is zero, that is an assertion that the P1 table is never ** empty and hence the jump will never be taken. ** ** This opcode leaves the cursor configured to move in forward order, ** from the beginning toward the end. In other words, the cursor is ** configured to use Next, not Prev. */ _114: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pC21 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp + 336)) = int32(1) if int32((*TVdbeCursor)(unsafe.Pointer(pC21)).FeCurType) == int32(CURTYPE_SORTER) { rc = _sqlite3VdbeSorterRewind(tls, pC21, bp+336) } else { _ = libc.Int32FromInt32(0) pCrsr6 = *(*uintptr)(unsafe.Pointer(pC21 + 36)) _ = libc.Int32FromInt32(0) rc = _sqlite3BtreeFirst(tls, pCrsr6, bp+336) (*TVdbeCursor)(unsafe.Pointer(pC21)).FdeferredMoveto = uint8(0) (*TVdbeCursor)(unsafe.Pointer(pC21)).FcacheStatus = uint32(CACHE_STALE) } if rc != 0 { goto abort_due_to_error } (*TVdbeCursor)(unsafe.Pointer(pC21)).FnullRow = uint8(*(*int32)(unsafe.Pointer(bp + 336))) if (*TOp)(unsafe.Pointer(pOp)).Fp2 > 0 { if *(*int32)(unsafe.Pointer(bp + 336)) != 0 { goto jump_to_p2 } } goto _187 /* Opcode: Next P1 P2 P3 * P5 ** ** Advance cursor P1 so that it points to the next key/data pair in its ** table or index. If there are no more key/value pairs then fall through ** to the following instruction. But if the cursor advance was successful, ** jump immediately to P2. ** ** The Next opcode is only valid following an SeekGT, SeekGE, or ** OP_Rewind opcode used to position the cursor. Next is not allowed ** to follow SeekLT, SeekLE, or OP_Last. ** ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have ** been opened prior to this opcode or the program will segfault. ** ** The P3 value is a hint to the btree implementation. If P3==1, that ** means P1 is an SQL index and that this instruction could have been ** omitted if that index had been unique. P3 is usually 0. P3 is ** always either 0 or 1. ** ** If P5 is positive and the jump is taken, then event counter ** number P5-1 in the prepared statement is incremented. ** ** See also: Prev */ /* Opcode: Prev P1 P2 P3 * P5 ** ** Back up cursor P1 so that it points to the previous key/data pair in its ** table or index. If there is no previous key/value pairs then fall through ** to the following instruction. But if the cursor backup was successful, ** jump immediately to P2. ** ** ** The Prev opcode is only valid following an SeekLT, SeekLE, or ** OP_Last opcode used to position the cursor. Prev is not allowed ** to follow SeekGT, SeekGE, or OP_Rewind. ** ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is ** not open then the behavior is undefined. ** ** The P3 value is a hint to the btree implementation. If P3==1, that ** means P1 is an SQL index and that this instruction could have been ** omitted if that index had been unique. P3 is usually 0. P3 is ** always either 0 or 1. ** ** If P5 is positive and the jump is taken, then event counter ** number P5-1 in the prepared statement is incremented. */ /* Opcode: SorterNext P1 P2 * * P5 ** ** This opcode works just like OP_Next except that P1 must be a ** sorter object for which the OP_SorterSort opcode has been ** invoked. This opcode advances the cursor to the next sorted ** record, or jumps to P2 if there are no more sorted records. */ _117: ; pC22 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) rc = _sqlite3VdbeSorterNext(tls, db, pC22) goto next_tail _115: ; /* jump, ncycle */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pC22 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3BtreePrevious(tls, *(*uintptr)(unsafe.Pointer(pC22 + 36)), (*TOp)(unsafe.Pointer(pOp)).Fp3) goto next_tail _116: ; /* jump, ncycle */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pC22 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3BtreeNext(tls, *(*uintptr)(unsafe.Pointer(pC22 + 36)), (*TOp)(unsafe.Pointer(pOp)).Fp3) goto next_tail next_tail: ; (*TVdbeCursor)(unsafe.Pointer(pC22)).FcacheStatus = uint32(CACHE_STALE) if rc == SQLITE_OK { (*TVdbeCursor)(unsafe.Pointer(pC22)).FnullRow = uint8(0) *(*Tu32)(unsafe.Pointer(p + 164 + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp5)*4))++ goto jump_to_p2_and_check_for_interrupt } if rc != int32(SQLITE_DONE) { goto abort_due_to_error } rc = SQLITE_OK (*TVdbeCursor)(unsafe.Pointer(pC22)).FnullRow = uint8(1) goto check_for_interrupt /* Opcode: IdxInsert P1 P2 P3 P4 P5 ** Synopsis: key=r[P2] ** ** Register P2 holds an SQL index key made using the ** MakeRecord instructions. This opcode writes that key ** into the index P1. Data for the entry is nil. ** ** If P4 is not zero, then it is the number of values in the unpacked ** key of reg(P2). In that case, P3 is the index of the first register ** for the unpacked key. The availability of the unpacked key can sometimes ** be an optimization. ** ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer ** that this insert is likely to be an append. ** ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, ** then the change counter is unchanged. ** ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might ** run faster by avoiding an unnecessary seek on cursor P1. However, ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior ** seeks on the cursor or if the most recent seek used a key equivalent ** to P2. ** ** This instruction only works for indices. The equivalent instruction ** for tables is OP_Insert. */ _118: ; _ = libc.Int32FromInt32(0) pC23 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 _ = libc.Int32FromInt32(0) if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_NCHANGE) != 0 { (*TVdbe)(unsafe.Pointer(p)).FnChange++ } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pIn2)).Fflags)&int32(MEM_Zero) != 0 { v273 = _sqlite3VdbeMemExpandBlob(tls, pIn2) } else { v273 = 0 } rc = v273 if rc != 0 { goto abort_due_to_error } (*(*TBtreePayload)(unsafe.Pointer(bp + 344))).FnKey = int64((*TMem)(unsafe.Pointer(pIn2)).Fn) (*(*TBtreePayload)(unsafe.Pointer(bp + 344))).FpKey = (*TMem)(unsafe.Pointer(pIn2)).Fz (*(*TBtreePayload)(unsafe.Pointer(bp + 344))).FaMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 (*(*TBtreePayload)(unsafe.Pointer(bp + 344))).FnMem = uint16(*(*int32)(unsafe.Pointer(pOp + 16))) if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_USESEEKRESULT) != 0 { v274 = (*TVdbeCursor)(unsafe.Pointer(pC23)).FseekResult } else { v274 = 0 } rc = _sqlite3BtreeInsert(tls, *(*uintptr)(unsafe.Pointer(pC23 + 36)), bp+344, int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&(libc.Int32FromInt32(OPFLAG_APPEND)|libc.Int32FromInt32(OPFLAG_SAVEPOSITION)|libc.Int32FromInt32(OPFLAG_PREFORMAT)), v274) _ = libc.Int32FromInt32(0) (*TVdbeCursor)(unsafe.Pointer(pC23)).FcacheStatus = uint32(CACHE_STALE) if rc != 0 { goto abort_due_to_error } goto _187 /* Opcode: SorterInsert P1 P2 * * * ** Synopsis: key=r[P2] ** ** Register P2 holds an SQL index key made using the ** MakeRecord instructions. This opcode writes that key ** into the sorter P1. Data for the entry is nil. */ _119: ; _ = libc.Int32FromInt32(0) pC24 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pIn2)).Fflags)&int32(MEM_Zero) != 0 { v275 = _sqlite3VdbeMemExpandBlob(tls, pIn2) } else { v275 = 0 } rc = v275 if rc != 0 { goto abort_due_to_error } rc = _sqlite3VdbeSorterWrite(tls, pC24, pIn2) if rc != 0 { goto abort_due_to_error } goto _187 /* Opcode: IdxDelete P1 P2 P3 * P5 ** Synopsis: key=r[P2@P3] ** ** The content of P3 registers starting at register P2 form ** an unpacked index key. This opcode removes that entry from the ** index opened by cursor P1. ** ** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error ** if no matching index entry is found. This happens when running ** an UPDATE or DELETE statement and the index entry to be updated ** or deleted is not found. For some uses of IdxDelete ** (example: the EXCEPT operator) it does not matter that no matching ** entry is found. For those cases, P5 is zero. Also, do not raise ** this (self-correcting and non-critical) error if in writable_schema mode. */ _120: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pC25 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pCrsr7 = *(*uintptr)(unsafe.Pointer(pC25 + 36)) _ = libc.Int32FromInt32(0) (*(*TUnpackedRecord)(unsafe.Pointer(bp + 392))).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pC25)).FpKeyInfo (*(*TUnpackedRecord)(unsafe.Pointer(bp + 392))).FnField = uint16((*TOp)(unsafe.Pointer(pOp)).Fp3) (*(*TUnpackedRecord)(unsafe.Pointer(bp + 392))).Fdefault_rc = 0 (*(*TUnpackedRecord)(unsafe.Pointer(bp + 392))).FaMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 rc = _sqlite3BtreeIndexMoveto(tls, pCrsr7, bp+392, bp+384) if rc != 0 { goto abort_due_to_error } if *(*int32)(unsafe.Pointer(bp + 384)) == 0 { rc = _sqlite3BtreeDelete(tls, pCrsr7, uint8(BTREE_AUXDELETE)) if rc != 0 { goto abort_due_to_error } } else { if (*TOp)(unsafe.Pointer(pOp)).Fp5 != 0 && !(_sqlite3WritableSchema(tls, db) != 0) { rc = _sqlite3ReportError(tls, libc.Int32FromInt32(SQLITE_CORRUPT)|libc.Int32FromInt32(3)< int64(0x7fffffff) { rc = _sqlite3CorruptError(tls, int32(99398)) goto abort_due_to_error } _sqlite3VdbeMemInit(tls, bp+464, db, uint16(0)) rc = _sqlite3VdbeMemFromBtreeZeroOffset(tls, pCur2, uint32(nCellKey), bp+464) if rc != 0 { goto abort_due_to_error } res10 = _sqlite3VdbeRecordCompareWithSkip(tls, (*(*TMem)(unsafe.Pointer(bp + 464))).Fn, (*(*TMem)(unsafe.Pointer(bp + 464))).Fz, bp+432, 0) _sqlite3VdbeMemReleaseMalloc(tls, bp+464) /* End of inlined sqlite3VdbeIdxKeyCompare() */ _ = libc.Int32FromInt32(0) if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode)&int32(1) == libc.Int32FromInt32(OP_IdxLT)&libc.Int32FromInt32(1) { _ = libc.Int32FromInt32(0) res10 = -res10 } else { _ = libc.Int32FromInt32(0) res10++ } _ = libc.Int32FromInt32(0) if res10 > 0 { goto jump_to_p2 } goto _187 /* Opcode: Destroy P1 P2 P3 * * ** ** Delete an entire database table or index whose root page in the database ** file is given by P1. ** ** The table being destroyed is in the main database file if P3==0. If ** P3==1 then the table to be destroyed is in the auxiliary database file ** that is used to store tables create using CREATE TEMPORARY TABLE. ** ** If AUTOVACUUM is enabled then it is possible that another root page ** might be moved into the newly deleted root page in order to keep all ** root pages contiguous at the beginning of the database. The former ** value of the root page that moved - its value before the move occurred - ** is stored in register P2. If no page movement was required (because the ** table being dropped was already the last one in the database) then a ** zero is stored in register P2. If AUTOVACUUM is disabled then a zero ** is stored in register P2. ** ** This opcode throws an error if there are any active reader VMs when ** it is invoked. This is done to avoid the difficulty associated with ** updating existing cursors when a root page is moved in an AUTOVACUUM ** database. This error is thrown even if the database is not an AUTOVACUUM ** db in order to avoid introducing an incompatibility between autovacuum ** and non-autovacuum modes. ** ** See also: Clear */ _128: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pOut = _out2Prerelease(tls, p, pOp) (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Null) if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeRead > (*Tsqlite3)(unsafe.Pointer(db)).FnVDestroy+int32(1) { rc = int32(SQLITE_LOCKED) (*TVdbe)(unsafe.Pointer(p)).FerrorAction = uint8(OE_Abort) goto abort_due_to_error } else { iDb2 = (*TOp)(unsafe.Pointer(pOp)).Fp3 _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp + 504)) = 0 /* Not needed. Only to silence a warning. */ rc = _sqlite3BtreeDropTable(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb2)*16))).FpBt, (*TOp)(unsafe.Pointer(pOp)).Fp1, bp+504) (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Int) *(*Ti64)(unsafe.Pointer(pOut)) = int64(*(*int32)(unsafe.Pointer(bp + 504))) if rc != 0 { goto abort_due_to_error } if *(*int32)(unsafe.Pointer(bp + 504)) != 0 { _sqlite3RootPageMoved(tls, db, iDb2, uint32(*(*int32)(unsafe.Pointer(bp + 504))), uint32((*TOp)(unsafe.Pointer(pOp)).Fp1)) /* All OP_Destroy operations occur on the same btree */ _ = libc.Int32FromInt32(0) resetSchemaOnFault = uint8(iDb2 + int32(1)) } } goto _187 /* Opcode: Clear P1 P2 P3 ** ** Delete all contents of the database table or index whose root page ** in the database file is given by P1. But, unlike Destroy, do not ** remove the table or index from the database file. ** ** The table being cleared is in the main database file if P2==0. If ** P2==1 then the table to be cleared is in the auxiliary database file ** that is used to store tables create using CREATE TEMPORARY TABLE. ** ** If the P3 value is non-zero, then the row change count is incremented ** by the number of rows in the table being cleared. If P3 is greater ** than zero, then the value stored in register P3 is also incremented ** by the number of rows in the table being cleared. ** ** See also: Destroy */ _129: ; *(*Ti64)(unsafe.Pointer(bp + 512)) = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3BtreeClearTable(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*16))).FpBt, int32(uint32((*TOp)(unsafe.Pointer(pOp)).Fp1)), bp+512) if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 { *(*Ti64)(unsafe.Pointer(p + 40)) += *(*Ti64)(unsafe.Pointer(bp + 512)) if (*TOp)(unsafe.Pointer(pOp)).Fp3 > 0 { _ = libc.Int32FromInt32(0) *(*Ti64)(unsafe.Pointer(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40)) += *(*Ti64)(unsafe.Pointer(bp + 512)) } } if rc != 0 { goto abort_due_to_error } goto _187 /* Opcode: ResetSorter P1 * * * * ** ** Delete all contents from the ephemeral table or sorter ** that is open on cursor P1. ** ** This opcode only works for cursors used for sorting and ** opened with OP_OpenEphemeral or OP_SorterOpen. */ _130: ; _ = libc.Int32FromInt32(0) pC29 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) if int32((*TVdbeCursor)(unsafe.Pointer(pC29)).FeCurType) == int32(CURTYPE_SORTER) { _sqlite3VdbeSorterReset(tls, db, *(*uintptr)(unsafe.Pointer(pC29 + 36))) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3BtreeClearTableOfCursor(tls, *(*uintptr)(unsafe.Pointer(pC29 + 36))) if rc != 0 { goto abort_due_to_error } } goto _187 /* Opcode: CreateBtree P1 P2 P3 * * ** Synopsis: r[P2]=root iDb=P1 flags=P3 ** ** Allocate a new b-tree in the main database file if P1==0 or in the ** TEMP database file if P1==1 or in an attached database if ** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table ** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table. ** The root page number of the new b-tree is stored in register P2. */ _131: ; pOut = _out2Prerelease(tls, p, pOp) *(*TPgno)(unsafe.Pointer(bp + 520)) = uint32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pDb3 = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16 _ = libc.Int32FromInt32(0) rc = _sqlite3BtreeCreateTable(tls, (*TDb)(unsafe.Pointer(pDb3)).FpBt, bp+520, (*TOp)(unsafe.Pointer(pOp)).Fp3) if rc != 0 { goto abort_due_to_error } *(*Ti64)(unsafe.Pointer(pOut)) = int64(*(*TPgno)(unsafe.Pointer(bp + 520))) goto _187 /* Opcode: SqlExec * * * P4 * ** ** Run the SQL statement or statements specified in the P4 string. ** Disable Auth and Trace callbacks while those statements are running if ** P1 is true. */ _132: ; (*Tsqlite3)(unsafe.Pointer(db)).FnSqlExec++ *(*uintptr)(unsafe.Pointer(bp + 524)) = uintptr(0) xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth mTrace = (*Tsqlite3)(unsafe.Pointer(db)).FmTrace if (*TOp)(unsafe.Pointer(pOp)).Fp1 != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).FmTrace = uint8(0) } rc = Xsqlite3_exec(tls, db, *(*uintptr)(unsafe.Pointer(pOp + 16)), uintptr(0), uintptr(0), bp+524) (*Tsqlite3)(unsafe.Pointer(db)).FnSqlExec-- (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth (*Tsqlite3)(unsafe.Pointer(db)).FmTrace = mTrace if *(*uintptr)(unsafe.Pointer(bp + 524)) != 0 || rc != 0 { _sqlite3VdbeError(tls, p, __ccgo_ts+3795, libc.VaList(bp+736, *(*uintptr)(unsafe.Pointer(bp + 524)))) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 524))) if rc == int32(SQLITE_NOMEM) { goto no_mem } goto abort_due_to_error } goto _187 /* Opcode: ParseSchema P1 * * P4 * ** ** Read and parse all entries from the schema table of database P1 ** that match the WHERE clause P4. If P4 is a NULL pointer, then the ** entire schema for P1 is reparsed. ** ** This opcode invokes the parser to create a new virtual machine, ** then runs the new virtual machine. It is thus a re-entrant opcode. */ _133: ; /* Any prepared statement that invokes this opcode will hold mutexes ** on every btree. This is a prerequisite for invoking ** sqlite3InitCallback(). */ iDb3 = (*TOp)(unsafe.Pointer(pOp)).Fp1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(pOp + 16)) == uintptr(0) { _sqlite3SchemaClear(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb3)*16))).FpSchema) *(*Tu32)(unsafe.Pointer(db + 24)) &= uint32(^libc.Int32FromInt32(DBFLAG_SchemaKnownOk)) rc = _sqlite3InitOne(tls, db, iDb3, p+124, uint32((*TOp)(unsafe.Pointer(pOp)).Fp5)) *(*Tu32)(unsafe.Pointer(db + 24)) |= uint32(DBFLAG_SchemaChange) libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 0, 0x3) } else { zSchema = __ccgo_ts + 6066 (*(*TInitData)(unsafe.Pointer(bp + 528))).Fdb = db (*(*TInitData)(unsafe.Pointer(bp + 528))).FiDb = iDb3 (*(*TInitData)(unsafe.Pointer(bp + 528))).FpzErrMsg = p + 124 (*(*TInitData)(unsafe.Pointer(bp + 528))).FmInitFlags = uint32(0) (*(*TInitData)(unsafe.Pointer(bp + 528))).FmxPage = _sqlite3BtreeLastPage(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb3)*16))).FpBt) zSql = _sqlite3MPrintf(tls, db, __ccgo_ts+6080, libc.VaList(bp+736, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb3)*16))).FzDbSName, zSchema, *(*uintptr)(unsafe.Pointer(pOp + 16)))) if zSql == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy = uint8(1) (*(*TInitData)(unsafe.Pointer(bp + 528))).Frc = SQLITE_OK (*(*TInitData)(unsafe.Pointer(bp + 528))).FnInitRow = uint32(0) _ = libc.Int32FromInt32(0) rc = Xsqlite3_exec(tls, db, zSql, __ccgo_fp(_sqlite3InitCallback), bp+528, uintptr(0)) if rc == SQLITE_OK { rc = (*(*TInitData)(unsafe.Pointer(bp + 528))).Frc } if rc == SQLITE_OK && (*(*TInitData)(unsafe.Pointer(bp + 528))).FnInitRow == uint32(0) { /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse ** at least one SQL statement. Any less than that indicates that ** the sqlite_schema table is corrupt. */ rc = _sqlite3CorruptError(tls, int32(99678)) } _sqlite3DbFreeNN(tls, db, zSql) (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy = uint8(0) } } if rc != 0 { _sqlite3ResetAllSchemasOfConnection(tls, db) if rc == int32(SQLITE_NOMEM) { goto no_mem } goto abort_due_to_error } goto _187 /* Opcode: LoadAnalysis P1 * * * * ** ** Read the sqlite_stat1 table for database P1 and load the content ** of that table into the internal index hash table. This will cause ** the analysis to be used when preparing all subsequent queries. */ _134: ; _ = libc.Int32FromInt32(0) rc = _sqlite3AnalysisLoad(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1) if rc != 0 { goto abort_due_to_error } goto _187 /* Opcode: DropTable P1 * * P4 * ** ** Remove the internal (in-memory) data structures that describe ** the table named P4 in database P1. This is called after a table ** is dropped from disk (using the Destroy opcode) in order to keep ** the internal representation of the ** schema consistent with what is on disk. */ _135: ; _sqlite3UnlinkAndDeleteTable(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1, *(*uintptr)(unsafe.Pointer(pOp + 16))) goto _187 /* Opcode: DropIndex P1 * * P4 * ** ** Remove the internal (in-memory) data structures that describe ** the index named P4 in database P1. This is called after an index ** is dropped from disk (using the Destroy opcode) ** in order to keep the internal representation of the ** schema consistent with what is on disk. */ _136: ; _sqlite3UnlinkAndDeleteIndex(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1, *(*uintptr)(unsafe.Pointer(pOp + 16))) goto _187 /* Opcode: DropTrigger P1 * * P4 * ** ** Remove the internal (in-memory) data structures that describe ** the trigger named P4 in database P1. This is called after a trigger ** is dropped from disk (using the Destroy opcode) in order to keep ** the internal representation of the ** schema consistent with what is on disk. */ _137: ; _sqlite3UnlinkAndDeleteTrigger(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1, *(*uintptr)(unsafe.Pointer(pOp + 16))) goto _187 /* Opcode: IntegrityCk P1 P2 P3 P4 P5 ** ** Do an analysis of the currently open database. Store in ** register P1 the text of an error message describing any problems. ** If no problems are found, store a NULL in register P1. ** ** The register P3 contains one less than the maximum number of allowed errors. ** At most reg(P3) errors will be reported. ** In other words, the analysis stops as soon as reg(P1) errors are ** seen. Reg(P1) is updated with the number of errors remaining. ** ** The root page numbers of all tables in the database are integers ** stored in P4_INTARRAY argument. ** ** If P5 is not zero, the check is done on the auxiliary database ** file, not the main database file. ** ** This opcode is used to implement the integrity_check pragma. */ _138: ; /* Register keeping track of errors remaining */ _ = libc.Int32FromInt32(0) nRoot = (*TOp)(unsafe.Pointer(pOp)).Fp2 aRoot = *(*uintptr)(unsafe.Pointer(pOp + 16)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pnErr = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3BtreeIntegrityCheck(tls, db, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp5)*16))).FpBt, aRoot+1*4, nRoot, int32(*(*Ti64)(unsafe.Pointer(pnErr)))+int32(1), bp+556, bp+560) _sqlite3VdbeMemSetNull(tls, pIn1) if *(*int32)(unsafe.Pointer(bp + 556)) == 0 { _ = libc.Int32FromInt32(0) } else { if rc != 0 { Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 560))) goto abort_due_to_error } else { *(*Ti64)(unsafe.Pointer(pnErr)) -= int64(*(*int32)(unsafe.Pointer(bp + 556)) - int32(1)) _sqlite3VdbeMemSetStr(tls, pIn1, *(*uintptr)(unsafe.Pointer(bp + 560)), int64(-int32(1)), uint8(SQLITE_UTF8), __ccgo_fp(Xsqlite3_free)) } } _sqlite3VdbeChangeEncoding(tls, pIn1, int32(encoding)) goto check_for_interrupt /* Opcode: RowSetAdd P1 P2 * * * ** Synopsis: rowset(P1)=r[P2] ** ** Insert the integer value held by register P2 into a RowSet object ** held in register P1. ** ** An assertion fails if P2 is not an integer. */ _139: ; /* in1, in2 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Blob) == 0 { if _sqlite3VdbeMemSetRowSet(tls, pIn1) != 0 { goto no_mem } } _ = libc.Int32FromInt32(0) _sqlite3RowSetInsert(tls, (*TMem)(unsafe.Pointer(pIn1)).Fz, *(*Ti64)(unsafe.Pointer(pIn2))) goto _187 /* Opcode: RowSetRead P1 P2 P3 * * ** Synopsis: r[P3]=rowset(P1) ** ** Extract the smallest value from the RowSet object in P1 ** and put that value into register P3. ** Or, if RowSet object P1 is initially empty, leave P3 ** unchanged and jump to instruction P2. */ _140: ; pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 _ = libc.Int32FromInt32(0) if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Blob) == 0 || _sqlite3RowSetNext(tls, (*TMem)(unsafe.Pointer(pIn1)).Fz, bp+568) == 0 { /* The boolean index is empty */ _sqlite3VdbeMemSetNull(tls, pIn1) goto jump_to_p2_and_check_for_interrupt } else { /* A value was pulled from the index */ _sqlite3VdbeMemSetInt64(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40, *(*Ti64)(unsafe.Pointer(bp + 568))) } goto check_for_interrupt /* Opcode: RowSetTest P1 P2 P3 P4 ** Synopsis: if r[P3] in rowset(P1) goto P2 ** ** Register P3 is assumed to hold a 64-bit integer value. If register P1 ** contains a RowSet object and that RowSet object contains ** the value held in P3, jump to register P2. Otherwise, insert the ** integer in P3 into the RowSet and continue on to the ** next opcode. ** ** The RowSet object is optimized for the case where sets of integers ** are inserted in distinct phases, which each set contains no duplicates. ** Each set is identified by a unique P4 value. The first set ** must have P4==0, the final set must have P4==-1, and for all other sets ** must have P4>0. ** ** This allows optimizations: (a) when P4==0 there is no need to test ** the RowSet object for P3, as it is guaranteed not to contain it, ** (b) when P4==-1 there is no need to insert the value, as it will ** never be tested for, and (c) when a value that is part of set X is ** inserted, there is no need to search to see if the same value was ** previously inserted as part of set X (only if it was previously ** inserted as part of some other set). */ _141: ; pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 iSet = *(*int32)(unsafe.Pointer(pOp + 16)) _ = libc.Int32FromInt32(0) /* If there is anything other than a rowset object in memory cell P1, ** delete it now and initialize P1 with an empty rowset */ if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Blob) == 0 { if _sqlite3VdbeMemSetRowSet(tls, pIn1) != 0 { goto no_mem } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if iSet != 0 { exists = _sqlite3RowSetTest(tls, (*TMem)(unsafe.Pointer(pIn1)).Fz, iSet, *(*Ti64)(unsafe.Pointer(pIn3))) if exists != 0 { goto jump_to_p2 } } if iSet >= 0 { _sqlite3RowSetInsert(tls, (*TMem)(unsafe.Pointer(pIn1)).Fz, *(*Ti64)(unsafe.Pointer(pIn3))) } goto _187 /* Opcode: Program P1 P2 P3 P4 P5 ** ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). ** ** P1 contains the address of the memory cell that contains the first memory ** cell in an array of values used as arguments to the sub-program. P2 ** contains the address to jump to if the sub-program throws an IGNORE ** exception using the RAISE() function. Register P3 contains the address ** of a memory cell in this (the parent) VM that is used to allocate the ** memory required by the sub-vdbe at runtime. ** ** P4 is a pointer to the VM containing the trigger program. ** ** If P5 is non-zero, then recursive program invocation is enabled. */ _142: ; /* Token identifying trigger */ pProgram = *(*uintptr)(unsafe.Pointer(pOp + 16)) pRt = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 _ = libc.Int32FromInt32(0) /* If the p5 flag is clear, then recursive invocation of triggers is ** disabled for backwards compatibility (p5 is set if this sub-program ** is really a trigger, not a foreign key action, and the flag set ** and cleared by the "PRAGMA recursive_triggers" command is clear). ** ** It is recursive invocation of triggers, at the SQL level, that is ** disabled. In some cases a single trigger may generate more than one ** SubProgram (if the trigger may be executed with more than one different ** ON CONFLICT algorithm). SubProgram structures associated with a ** single trigger all have the same value for the SubProgram.token ** variable. */ if (*TOp)(unsafe.Pointer(pOp)).Fp5 != 0 { t1 = (*TSubProgram)(unsafe.Pointer(pProgram)).Ftoken pFrame2 = (*TVdbe)(unsafe.Pointer(p)).FpFrame for { if !(pFrame2 != 0 && (*TVdbeFrame)(unsafe.Pointer(pFrame2)).Ftoken != t1) { break } goto _276 _276: ; pFrame2 = (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FpParent } if pFrame2 != 0 { goto _187 } } if (*TVdbe)(unsafe.Pointer(p)).FnFrame >= *(*int32)(unsafe.Pointer(db + 120 + 10*4)) { rc = int32(SQLITE_ERROR) _sqlite3VdbeError(tls, p, __ccgo_ts+6123, 0) goto abort_due_to_error } /* Register pRt is used to store the memory required to save the state ** of the current program, and the memory required at runtime to execute ** the trigger program. If this trigger has been fired before, then pRt ** is already allocated. Otherwise, it must be initialized. */ if int32((*TMem)(unsafe.Pointer(pRt)).Fflags)&int32(MEM_Blob) == 0 { /* SubProgram.nMem is set to the number of memory cells used by the ** program stored in SubProgram.aOp. As well as these, one memory ** cell is required for each cursor used by the program. Set local ** variable nMem (and later, VdbeFrame.nChildMem) to this value. */ nMem = (*TSubProgram)(unsafe.Pointer(pProgram)).FnMem + (*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr _ = libc.Int32FromInt32(0) if (*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr == 0 { nMem++ } nByte2 = int32((libc.Uint32FromInt64(88)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)) + uint32(nMem)*uint32(40) + uint32((*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr)*uint32(4) + uint32(((*TSubProgram)(unsafe.Pointer(pProgram)).FnOp+int32(7))/int32(8))) pFrame2 = _sqlite3DbMallocZero(tls, db, uint64(nByte2)) if !(pFrame2 != 0) { goto no_mem } _sqlite3VdbeMemRelease(tls, pRt) (*TMem)(unsafe.Pointer(pRt)).Fflags = uint16(libc.Int32FromInt32(MEM_Blob) | libc.Int32FromInt32(MEM_Dyn)) (*TMem)(unsafe.Pointer(pRt)).Fz = pFrame2 (*TMem)(unsafe.Pointer(pRt)).Fn = nByte2 (*TMem)(unsafe.Pointer(pRt)).FxDel = __ccgo_fp(_sqlite3VdbeFrameMemDel) (*TVdbeFrame)(unsafe.Pointer(pFrame2)).Fv = p (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnChildMem = nMem (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnChildCsr = (*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr (*TVdbeFrame)(unsafe.Pointer(pFrame2)).Fpc = (int32(pOp) - int32(aOp)) / 20 (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FaMem = (*TVdbe)(unsafe.Pointer(p)).FaMem (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnMem = (*TVdbe)(unsafe.Pointer(p)).FnMem (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FapCsr = (*TVdbe)(unsafe.Pointer(p)).FapCsr (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnCursor = (*TVdbe)(unsafe.Pointer(p)).FnCursor (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FaOp = (*TVdbe)(unsafe.Pointer(p)).FaOp (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnOp = (*TVdbe)(unsafe.Pointer(p)).FnOp (*TVdbeFrame)(unsafe.Pointer(pFrame2)).Ftoken = (*TSubProgram)(unsafe.Pointer(pProgram)).Ftoken pEnd = pFrame2 + uintptr((libc.Uint32FromInt64(88)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))) + uintptr((*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnChildMem)*40 pMem1 = pFrame2 + uintptr((libc.Uint32FromInt64(88)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))) for { if !(pMem1 != pEnd) { break } (*TMem)(unsafe.Pointer(pMem1)).Fflags = uint16(MEM_Undefined) (*TMem)(unsafe.Pointer(pMem1)).Fdb = db goto _277 _277: ; pMem1 += 40 } } else { pFrame2 = (*TMem)(unsafe.Pointer(pRt)).Fz _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } (*TVdbe)(unsafe.Pointer(p)).FnFrame++ (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FpParent = (*TVdbe)(unsafe.Pointer(p)).FpFrame (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FlastRowid = (*Tsqlite3)(unsafe.Pointer(db)).FlastRowid (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnChange = (*TVdbe)(unsafe.Pointer(p)).FnChange (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnDbChange = (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FnChange _ = libc.Int32FromInt32(0) (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FpAuxData = (*TVdbe)(unsafe.Pointer(p)).FpAuxData (*TVdbe)(unsafe.Pointer(p)).FpAuxData = uintptr(0) (*TVdbe)(unsafe.Pointer(p)).FnChange = 0 (*TVdbe)(unsafe.Pointer(p)).FpFrame = pFrame2 v278 = pFrame2 + uintptr((libc.Uint32FromInt64(88)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))) aMem = v278 (*TVdbe)(unsafe.Pointer(p)).FaMem = v278 (*TVdbe)(unsafe.Pointer(p)).FnMem = (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnChildMem (*TVdbe)(unsafe.Pointer(p)).FnCursor = int32(uint16((*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnChildCsr)) (*TVdbe)(unsafe.Pointer(p)).FapCsr = aMem + uintptr((*TVdbe)(unsafe.Pointer(p)).FnMem)*40 (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FaOnce = (*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr)*4 libc.Xmemset(tls, (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FaOnce, 0, uint32(((*TSubProgram)(unsafe.Pointer(pProgram)).FnOp+int32(7))/int32(8))) v279 = (*TSubProgram)(unsafe.Pointer(pProgram)).FaOp aOp = v279 (*TVdbe)(unsafe.Pointer(p)).FaOp = v279 (*TVdbe)(unsafe.Pointer(p)).FnOp = (*TSubProgram)(unsafe.Pointer(pProgram)).FnOp pOp = aOp + uintptr(-libc.Int32FromInt32(1))*20 goto check_for_interrupt /* Opcode: Param P1 P2 * * * ** ** This opcode is only ever present in sub-programs called via the ** OP_Program instruction. Copy a value currently stored in a memory ** cell of the calling (parent) frame to cell P2 in the current frames ** address space. This is used by trigger programs to access the new.* ** and old.* values. ** ** The address of the cell in the parent frame is determined by adding ** the value of the P1 argument to the value of the P1 argument to the ** calling OP_Program instruction. */ _143: ; pOut = _out2Prerelease(tls, p, pOp) pFrame3 = (*TVdbe)(unsafe.Pointer(p)).FpFrame pIn = (*TVdbeFrame)(unsafe.Pointer(pFrame3)).FaMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1+(*(*TOp)(unsafe.Pointer((*TVdbeFrame)(unsafe.Pointer(pFrame3)).FaOp + uintptr((*TVdbeFrame)(unsafe.Pointer(pFrame3)).Fpc)*20))).Fp1)*40 _sqlite3VdbeMemShallowCopy(tls, pOut, pIn, int32(MEM_Ephem)) goto _187 /* Opcode: FkCounter P1 P2 * * * ** Synopsis: fkctr[P1]+=P2 ** ** Increment a "constraint counter" by P2 (P2 may be negative or positive). ** If P1 is non-zero, the database constraint counter is incremented ** (deferred foreign key constraints). Otherwise, if P1 is zero, the ** statement counter is incremented (immediate foreign key constraints). */ _144: ; if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_DeferFKs) != 0 { *(*Ti64)(unsafe.Pointer(db + 536)) += int64((*TOp)(unsafe.Pointer(pOp)).Fp2) } else { if (*TOp)(unsafe.Pointer(pOp)).Fp1 != 0 { *(*Ti64)(unsafe.Pointer(db + 528)) += int64((*TOp)(unsafe.Pointer(pOp)).Fp2) } else { *(*Ti64)(unsafe.Pointer(p + 64)) += int64((*TOp)(unsafe.Pointer(pOp)).Fp2) } } goto _187 /* Opcode: FkIfZero P1 P2 * * * ** Synopsis: if fkctr[P1]==0 goto P2 ** ** This opcode tests if a foreign key constraint-counter is currently zero. ** If so, jump to instruction P2. Otherwise, fall through to the next ** instruction. ** ** If P1 is non-zero, then the jump is taken if the database constraint-counter ** is zero (the one that counts deferred constraint violations). If P1 is ** zero, the jump is taken if the statement constraint-counter is zero ** (immediate foreign key constraint violations). */ _145: ; /* jump */ if (*TOp)(unsafe.Pointer(pOp)).Fp1 != 0 { if (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons == 0 && (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons == 0 { goto jump_to_p2 } } else { if (*TVdbe)(unsafe.Pointer(p)).FnFkConstraint == 0 && (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons == 0 { goto jump_to_p2 } } goto _187 /* Opcode: MemMax P1 P2 * * * ** Synopsis: r[P1]=max(r[P1],r[P2]) ** ** P1 is a register in the root frame of this VM (the root frame is ** different from the current frame if this instruction is being executed ** within a sub-program). Set the value of register P1 to the maximum of ** its current value and the value in register P2. ** ** This instruction throws an error if the memory cell is not initially ** an integer. */ _146: ; if (*TVdbe)(unsafe.Pointer(p)).FpFrame != 0 { pFrame4 = (*TVdbe)(unsafe.Pointer(p)).FpFrame for { if !((*TVdbeFrame)(unsafe.Pointer(pFrame4)).FpParent != 0) { break } goto _280 _280: ; pFrame4 = (*TVdbeFrame)(unsafe.Pointer(pFrame4)).FpParent } pIn1 = (*TVdbeFrame)(unsafe.Pointer(pFrame4)).FaMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 } else { pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 } _ = libc.Int32FromInt32(0) _sqlite3VdbeMemIntegerify(tls, pIn1) pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 _sqlite3VdbeMemIntegerify(tls, pIn2) if *(*Ti64)(unsafe.Pointer(pIn1)) < *(*Ti64)(unsafe.Pointer(pIn2)) { *(*Ti64)(unsafe.Pointer(pIn1)) = *(*Ti64)(unsafe.Pointer(pIn2)) } goto _187 /* Opcode: IfPos P1 P2 P3 * * ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 ** ** Register P1 must contain an integer. ** If the value of register P1 is 1 or greater, subtract P3 from the ** value in P1 and jump to P2. ** ** If the initial value of register P1 is less than 1, then the ** value is unchanged and control passes through to the next instruction. */ _147: ; /* jump, in1 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 _ = libc.Int32FromInt32(0) if *(*Ti64)(unsafe.Pointer(pIn1)) > 0 { *(*Ti64)(unsafe.Pointer(pIn1)) -= int64((*TOp)(unsafe.Pointer(pOp)).Fp3) goto jump_to_p2 } goto _187 /* Opcode: OffsetLimit P1 P2 P3 * * ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) ** ** This opcode performs a commonly used computation associated with ** LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3] ** holds the offset counter. The opcode computes the combined value ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] ** value computed is the total number of rows that will need to be ** visited in order to complete the query. ** ** If r[P3] is zero or negative, that means there is no OFFSET ** and r[P2] is set to be the value of the LIMIT, r[P1]. ** ** if r[P1] is zero or negative, that means there is no LIMIT ** and r[P2] is set to -1. ** ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3]. */ _148: ; pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 pOut = _out2Prerelease(tls, p, pOp) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*Ti64)(unsafe.Pointer(bp + 576)) = *(*Ti64)(unsafe.Pointer(pIn1)) if v282 = *(*Ti64)(unsafe.Pointer(bp + 576)) <= 0; !v282 { if *(*Ti64)(unsafe.Pointer(pIn3)) > 0 { v281 = *(*Ti64)(unsafe.Pointer(pIn3)) } else { v281 = 0 } } if v282 || _sqlite3AddInt64(tls, bp+576, v281) != 0 { /* If the LIMIT is less than or equal to zero, loop forever. This ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then ** also loop forever. This is undocumented. In fact, one could argue ** that the loop should terminate. But assuming 1 billion iterations ** per second (far exceeding the capabilities of any current hardware) ** it would take nearly 300 years to actually reach the limit. So ** looping forever is a reasonable approximation. */ *(*Ti64)(unsafe.Pointer(pOut)) = int64(-int32(1)) } else { *(*Ti64)(unsafe.Pointer(pOut)) = *(*Ti64)(unsafe.Pointer(bp + 576)) } goto _187 /* Opcode: IfNotZero P1 P2 * * * ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2 ** ** Register P1 must contain an integer. If the content of register P1 is ** initially greater than zero, then decrement the value in register P1. ** If it is non-zero (negative or positive) and then also jump to P2. ** If register P1 is initially zero, leave it unchanged and fall through. */ _149: ; /* jump, in1 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 _ = libc.Int32FromInt32(0) if *(*Ti64)(unsafe.Pointer(pIn1)) != 0 { if *(*Ti64)(unsafe.Pointer(pIn1)) > 0 { *(*Ti64)(unsafe.Pointer(pIn1))-- } goto jump_to_p2 } goto _187 /* Opcode: DecrJumpZero P1 P2 * * * ** Synopsis: if (--r[P1])==0 goto P2 ** ** Register P1 must hold an integer. Decrement the value in P1 ** and jump to P2 if the new value is exactly zero. */ _150: ; /* jump, in1 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 _ = libc.Int32FromInt32(0) if *(*Ti64)(unsafe.Pointer(pIn1)) > int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<= 0) { break } *(*uintptr)(unsafe.Pointer(pCtx1 + 28 + uintptr(i4)*4)) = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2+i4)*40 goto _283 _283: ; i4-- } } (*TMem)(unsafe.Pointer(pMem2)).Fn++ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TOp)(unsafe.Pointer(pOp)).Fp1 != 0 { (*(*func(*libc.TLS, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFuncDef)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FpFunc)).FxInverse})))(tls, pCtx1, int32((*Tsqlite3_context)(unsafe.Pointer(pCtx1)).Fargc), pCtx1+28) } else { (*(*func(*libc.TLS, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFuncDef)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FpFunc)).FxSFunc})))(tls, pCtx1, int32((*Tsqlite3_context)(unsafe.Pointer(pCtx1)).Fargc), pCtx1+28) } /* IMP: R-24505-23230 */ if (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FisError != 0 { if (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FisError > 0 { _sqlite3VdbeError(tls, p, __ccgo_ts+3795, libc.VaList(bp+736, Xsqlite3_value_text(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FpOut))) rc = (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FisError } if (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FskipFlag != 0 { _ = libc.Int32FromInt32(0) i4 = (*(*TOp)(unsafe.Pointer(pOp + uintptr(-libc.Int32FromInt32(1))*20))).Fp1 if i4 != 0 { _sqlite3VdbeMemSetInt64(tls, aMem+uintptr(i4)*40, int64(1)) } (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FskipFlag = uint8(0) } _sqlite3VdbeMemRelease(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FpOut) (*TMem)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FpOut)).Fflags = uint16(MEM_Null) (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FisError = 0 if rc != 0 { goto abort_due_to_error } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) goto _187 /* Opcode: AggFinal P1 P2 * P4 * ** Synopsis: accum=r[P1] N=P2 ** ** P1 is the memory location that is the accumulator for an aggregate ** or window function. Execute the finalizer function ** for an aggregate and store the result in P1. ** ** P2 is the number of arguments that the step function takes and ** P4 is a pointer to the FuncDef for this function. The P2 ** argument is not used by this opcode. It is only there to disambiguate ** functions that can take varying numbers of arguments. The ** P4 argument is only needed for the case where ** the step function was not previously called. */ /* Opcode: AggValue * P2 P3 P4 * ** Synopsis: r[P3]=value N=P2 ** ** Invoke the xValue() function and store the result in register P3. ** ** P2 is the number of arguments that the step function takes and ** P4 is a pointer to the FuncDef for this function. The P2 ** argument is not used by this opcode. It is only there to disambiguate ** functions that can take varying numbers of arguments. The ** P4 argument is only needed for the case where ** the step function was not previously called. */ _155: ; _154: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pMem3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 _ = libc.Int32FromInt32(0) if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 { rc = _sqlite3VdbeMemAggValue(tls, pMem3, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40, *(*uintptr)(unsafe.Pointer(pOp + 16))) pMem3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 } else { rc = _sqlite3VdbeMemFinalize(tls, pMem3, *(*uintptr)(unsafe.Pointer(pOp + 16))) } if rc != 0 { _sqlite3VdbeError(tls, p, __ccgo_ts+3795, libc.VaList(bp+736, Xsqlite3_value_text(tls, pMem3))) goto abort_due_to_error } _sqlite3VdbeChangeEncoding(tls, pMem3, int32(encoding)) goto _187 /* Opcode: Checkpoint P1 P2 P3 * * ** ** Checkpoint database P1. This is a no-op if P1 is not currently in ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL, ** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns ** SQLITE_BUSY or not, respectively. Write the number of pages in the ** WAL after the checkpoint into mem[P3+1] and the number of pages ** in the WAL that have been checkpointed after the checkpoint ** completes into mem[P3+2]. However on an error, mem[P3+1] and ** mem[P3+2] are initialized to -1. */ _156: ; /* Write results here */ _ = libc.Int32FromInt32(0) (*(*[3]int32)(unsafe.Pointer(bp + 584)))[0] = 0 v284 = -libc.Int32FromInt32(1) (*(*[3]int32)(unsafe.Pointer(bp + 584)))[int32(2)] = v284 (*(*[3]int32)(unsafe.Pointer(bp + 584)))[int32(1)] = v284 _ = libc.Int32FromInt32(0) rc = _sqlite3Checkpoint(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1, (*TOp)(unsafe.Pointer(pOp)).Fp2, bp+584+1*4, bp+584+2*4) if rc != 0 { if rc != int32(SQLITE_BUSY) { goto abort_due_to_error } rc = SQLITE_OK (*(*[3]int32)(unsafe.Pointer(bp + 584)))[0] = int32(1) } i5 = 0 pMem4 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 for { if !(i5 < int32(3)) { break } _sqlite3VdbeMemSetInt64(tls, pMem4, int64((*(*[3]int32)(unsafe.Pointer(bp + 584)))[i5])) goto _285 _285: ; i5++ pMem4 += 40 } goto _187 /* Opcode: JournalMode P1 P2 P3 * * ** ** Change the journal mode of database P1 to P3. P3 must be one of the ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback ** modes (delete, truncate, persist, off and memory), this is a simple ** operation. No IO is required. ** ** If changing into or out of WAL mode the procedure is more complicated. ** ** Write a string containing the final journal-mode to register P2. */ _157: ; /* Name of database file for pPager */ pOut = _out2Prerelease(tls, p, pOp) eNew = (*TOp)(unsafe.Pointer(pOp)).Fp3 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pBt1 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16))).FpBt pPager = _sqlite3BtreePager(tls, pBt1) eOld = _sqlite3PagerGetJournalMode(tls, pPager) if eNew == -int32(1) { eNew = eOld } _ = libc.Int32FromInt32(0) if !(_sqlite3PagerOkToChangeJournalMode(tls, pPager) != 0) { eNew = eOld } zFilename = _sqlite3PagerFilename(tls, pPager, int32(1)) /* Do not allow a transition to journal_mode=WAL for a database ** in temporary storage or if the VFS does not support shared memory */ if eNew == int32(PAGER_JOURNALMODE_WAL) && (_sqlite3Strlen30(tls, zFilename) == 0 || !(_sqlite3PagerWalSupported(tls, pPager) != 0)) { eNew = eOld } if eNew != eOld && (eOld == int32(PAGER_JOURNALMODE_WAL) || eNew == int32(PAGER_JOURNALMODE_WAL)) { if !((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0) || (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeRead > int32(1) { rc = int32(SQLITE_ERROR) if eNew == int32(PAGER_JOURNALMODE_WAL) { v286 = __ccgo_ts + 6160 } else { v286 = __ccgo_ts + 6165 } _sqlite3VdbeError(tls, p, __ccgo_ts+6172, libc.VaList(bp+736, v286)) goto abort_due_to_error } else { if eOld == int32(PAGER_JOURNALMODE_WAL) { /* If leaving WAL mode, close the log file. If successful, the call ** to PagerCloseWal() checkpoints and deletes the write-ahead-log ** file. An EXCLUSIVE lock may still be held on the database file ** after a successful return. */ rc = _sqlite3PagerCloseWal(tls, pPager, db) if rc == SQLITE_OK { _sqlite3PagerSetJournalMode(tls, pPager, eNew) } } else { if eOld == int32(PAGER_JOURNALMODE_MEMORY) { /* Cannot transition directly from MEMORY to WAL. Use mode OFF ** as an intermediate */ _sqlite3PagerSetJournalMode(tls, pPager, int32(PAGER_JOURNALMODE_OFF)) } } /* Open a transaction on the database file. Regardless of the journal ** mode, this transaction always uses a rollback journal. */ _ = libc.Int32FromInt32(0) if rc == SQLITE_OK { if eNew == int32(PAGER_JOURNALMODE_WAL) { v287 = int32(2) } else { v287 = int32(1) } rc = _sqlite3BtreeSetVersion(tls, pBt1, v287) } } } if rc != 0 { eNew = eOld } eNew = _sqlite3PagerSetJournalMode(tls, pPager, eNew) (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(libc.Int32FromInt32(MEM_Str) | libc.Int32FromInt32(MEM_Static) | libc.Int32FromInt32(MEM_Term)) (*TMem)(unsafe.Pointer(pOut)).Fz = _sqlite3JournalModename(tls, eNew) (*TMem)(unsafe.Pointer(pOut)).Fn = _sqlite3Strlen30(tls, (*TMem)(unsafe.Pointer(pOut)).Fz) (*TMem)(unsafe.Pointer(pOut)).Fenc = uint8(SQLITE_UTF8) _sqlite3VdbeChangeEncoding(tls, pOut, int32(encoding)) if rc != 0 { goto abort_due_to_error } goto _187 /* Opcode: Vacuum P1 P2 * * * ** ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more ** for an attached database. The "temp" database may not be vacuumed. ** ** If P2 is not zero, then it is a register holding a string which is ** the file into which the result of vacuum should be written. When ** P2 is zero, the vacuum overwrites the original database. */ _158: ; _ = libc.Int32FromInt32(0) if (*TOp)(unsafe.Pointer(pOp)).Fp2 != 0 { v288 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 } else { v288 = uintptr(0) } rc = _sqlite3RunVacuum(tls, p+124, db, (*TOp)(unsafe.Pointer(pOp)).Fp1, v288) if rc != 0 { goto abort_due_to_error } goto _187 /* Opcode: IncrVacuum P1 P2 * * * ** ** Perform a single step of the incremental vacuum procedure on ** the P1 database. If the vacuum has finished, jump to instruction ** P2. Otherwise, fall through to the next instruction. */ _159: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pBt2 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16))).FpBt rc = _sqlite3BtreeIncrVacuum(tls, pBt2) if rc != 0 { if rc != int32(SQLITE_DONE) { goto abort_due_to_error } rc = SQLITE_OK goto jump_to_p2 } goto _187 /* Opcode: Expire P1 P2 * * * ** ** Cause precompiled statements to expire. When an expired statement ** is executed using sqlite3_step() it will either automatically ** reprepare itself (if it was originally created using sqlite3_prepare_v2()) ** or it will fail with SQLITE_SCHEMA. ** ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, ** then only the currently executing statement is expired. ** ** If P2 is 0, then SQL statements are expired immediately. If P2 is 1, ** then running SQL statements are allowed to continue to run to completion. ** The P2==1 case occurs when a CREATE INDEX or similar schema change happens ** that might help the statement run faster but which does not affect the ** correctness of operation. */ _160: ; _ = libc.Int32FromInt32(0) if !((*TOp)(unsafe.Pointer(pOp)).Fp1 != 0) { _sqlite3ExpirePreparedStatements(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp2) } else { libc.SetBitFieldPtr16Uint32(p+152, uint32((*TOp)(unsafe.Pointer(pOp)).Fp2+libc.Int32FromInt32(1)), 0, 0x3) } goto _187 /* Opcode: CursorLock P1 * * * * ** ** Lock the btree to which cursor P1 is pointing so that the btree cannot be ** written by an other cursor. */ _161: ; _ = libc.Int32FromInt32(0) pC30 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3BtreeCursorPin(tls, *(*uintptr)(unsafe.Pointer(pC30 + 36))) goto _187 /* Opcode: CursorUnlock P1 * * * * ** ** Unlock the btree to which cursor P1 is pointing so that it can be ** written by other cursors. */ _162: ; _ = libc.Int32FromInt32(0) pC31 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3BtreeCursorUnpin(tls, *(*uintptr)(unsafe.Pointer(pC31 + 36))) goto _187 /* Opcode: TableLock P1 P2 P3 P4 * ** Synopsis: iDb=P1 root=P2 write=P3 ** ** Obtain a lock on a particular table. This instruction is only used when ** the shared-cache feature is enabled. ** ** P1 is the index of the database in sqlite3.aDb[] of the database ** on which the lock is acquired. A readlock is obtained if P3==0 or ** a write lock if P3==1. ** ** P2 contains the root-page of the table to lock. ** ** P4 contains a pointer to the name of the table being locked. This is only ** used to generate an error message if the lock cannot be obtained. */ _163: ; isWriteLock = uint8((*TOp)(unsafe.Pointer(pOp)).Fp3) if isWriteLock != 0 || uint64(0) == (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00004))< 0 { _sqlite3VdbeError(tls, p, __ccgo_ts+3795, libc.VaList(bp+736, Xsqlite3_value_text(tls, pDest2))) rc = (*(*Tsqlite3_context)(unsafe.Pointer(bp + 648))).FisError } _sqlite3VdbeChangeEncoding(tls, pDest2, int32(encoding)) if rc != 0 { goto abort_due_to_error } goto _187 /* Opcode: VNext P1 P2 * * * ** ** Advance virtual table P1 to the next row in its result set and ** jump to instruction P2. Or, if the virtual table has reached ** the end of its result set, then fall through to the next instruction. */ _172: ; pCur6 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TVdbeCursor)(unsafe.Pointer(pCur6)).FnullRow != 0 { goto _187 } pVtab5 = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pCur6 + 36)))).FpVtab pModule5 = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab5)).FpModule _ = libc.Int32FromInt32(0) /* Invoke the xNext() method of the module. There is no way for the ** underlying implementation to return an error if one occurs during ** xNext(). Instead, if an error occurs, true is returned (indicating that ** data is available) and the error code returned when xColumn or ** some other method is next invoked on the save virtual table cursor. */ rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule5)).FxNext})))(tls, *(*uintptr)(unsafe.Pointer(pCur6 + 36))) _sqlite3VtabImportErrmsg(tls, p, pVtab5) if rc != 0 { goto abort_due_to_error } res12 = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule5)).FxEof})))(tls, *(*uintptr)(unsafe.Pointer(pCur6 + 36))) if !(res12 != 0) { /* If there is data, jump to P2 */ goto jump_to_p2_and_check_for_interrupt } goto check_for_interrupt /* Opcode: VRename P1 * * P4 * ** ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. ** This opcode invokes the corresponding xRename method. The value ** in register P1 is passed as the zName argument to the xRename method. */ _173: ; isLegacy = int32((*Tsqlite3)(unsafe.Pointer(db)).Fflags & libc.Uint64FromInt32(SQLITE_LegacyAlter)) *(*Tu64)(unsafe.Pointer(db + 32)) |= uint64(SQLITE_LegacyAlter) pVtab6 = (*TVTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FpVtab pName = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _sqlite3VdbeChangeEncoding(tls, pName, int32(SQLITE_UTF8)) if rc != 0 { goto abort_due_to_error } rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer(pVtab6)).FpModule)).FxRename})))(tls, pVtab6, (*TMem)(unsafe.Pointer(pName)).Fz) if isLegacy == 0 { *(*Tu64)(unsafe.Pointer(db + 32)) &= ^libc.Uint64FromInt32(SQLITE_LegacyAlter) } _sqlite3VtabImportErrmsg(tls, p, pVtab6) libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 0, 0x3) if rc != 0 { goto abort_due_to_error } goto _187 /* Opcode: VUpdate P1 P2 P3 P4 P5 ** Synopsis: data=r[P3@P2] ** ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. ** This opcode invokes the corresponding xUpdate method. P2 values ** are contiguous memory cells starting at P3 to pass to the xUpdate ** invocation. The value in register (P3+P2-1) corresponds to the ** p2th element of the argv array passed to xUpdate. ** ** The xUpdate method will do a DELETE or an INSERT or both. ** The argv[0] element (which corresponds to memory cell P3) ** is the rowid of a row to delete. If argv[0] is NULL then no ** deletion occurs. The argv[1] element is the rowid of the new ** row. This can be NULL to have the virtual table select the new ** rowid for itself. The subsequent elements in the array are ** the values of columns in the new row. ** ** If P2==1 then no insert is performed. argv[0] is the rowid of ** a row to delete. ** ** P1 is a boolean flag. If it is set to true and the xUpdate call ** is successful, then the value returned by sqlite3_last_insert_rowid() ** is set to the value of the rowid for the row just inserted. ** ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to ** apply in the case of a constraint failure on an insert or update. */ _174: ; *(*Tsqlite_int64)(unsafe.Pointer(bp + 720)) = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto no_mem } pVtab7 = (*TVTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FpVtab if pVtab7 == uintptr(0) || (*Tsqlite3_vtab)(unsafe.Pointer(pVtab7)).FpModule == uintptr(0) { rc = int32(SQLITE_LOCKED) goto abort_due_to_error } pModule6 = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab7)).FpModule nArg1 = (*TOp)(unsafe.Pointer(pOp)).Fp2 _ = libc.Int32FromInt32(0) if (*Tsqlite3_module)(unsafe.Pointer(pModule6)).FxUpdate != 0 { vtabOnConflict = (*Tsqlite3)(unsafe.Pointer(db)).FvtabOnConflict apArg1 = (*TVdbe)(unsafe.Pointer(p)).FapArg pX1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 i7 = 0 for { if !(i7 < nArg1) { break } _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(apArg1 + uintptr(i7)*4)) = pX1 pX1 += 40 goto _290 _290: ; i7++ } (*Tsqlite3)(unsafe.Pointer(db)).FvtabOnConflict = uint8((*TOp)(unsafe.Pointer(pOp)).Fp5) rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule6)).FxUpdate})))(tls, pVtab7, nArg1, apArg1, bp+720) (*Tsqlite3)(unsafe.Pointer(db)).FvtabOnConflict = vtabOnConflict _sqlite3VtabImportErrmsg(tls, p, pVtab7) if rc == SQLITE_OK && (*TOp)(unsafe.Pointer(pOp)).Fp1 != 0 { _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).FlastRowid = *(*Tsqlite_int64)(unsafe.Pointer(bp + 720)) } if rc&int32(0xff) == int32(SQLITE_CONSTRAINT) && (*TVTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FbConstraint != 0 { if int32((*TOp)(unsafe.Pointer(pOp)).Fp5) == int32(OE_Ignore) { rc = SQLITE_OK } else { if int32((*TOp)(unsafe.Pointer(pOp)).Fp5) == int32(OE_Replace) { v291 = int32(OE_Abort) } else { v291 = int32((*TOp)(unsafe.Pointer(pOp)).Fp5) } (*TVdbe)(unsafe.Pointer(p)).FerrorAction = uint8(v291) } } else { (*TVdbe)(unsafe.Pointer(p)).FnChange++ } if rc != 0 { goto abort_due_to_error } } goto _187 /* Opcode: Pagecount P1 P2 * * * ** ** Write the current number of pages in database P1 to memory cell P2. */ _175: ; /* out2 */ pOut = _out2Prerelease(tls, p, pOp) *(*Ti64)(unsafe.Pointer(pOut)) = int64(_sqlite3BtreeLastPage(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16))).FpBt)) goto _187 /* Opcode: MaxPgcnt P1 P2 P3 * * ** ** Try to set the maximum page count for database P1 to the value in P3. ** Do not let the maximum page count fall below the current page count and ** do not change the maximum page count value if P3==0. ** ** Store the maximum page count after the change in register P2. */ _176: ; pOut = _out2Prerelease(tls, p, pOp) pBt3 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16))).FpBt newMax = uint32(0) if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 { newMax = _sqlite3BtreeLastPage(tls, pBt3) if newMax < uint32((*TOp)(unsafe.Pointer(pOp)).Fp3) { newMax = uint32((*TOp)(unsafe.Pointer(pOp)).Fp3) } } *(*Ti64)(unsafe.Pointer(pOut)) = int64(_sqlite3BtreeMaxPageCount(tls, pBt3, newMax)) goto _187 /* Opcode: Function P1 P2 P3 P4 * ** Synopsis: r[P3]=func(r[P2@NP]) ** ** Invoke a user function (P4 is a pointer to an sqlite3_context object that ** contains a pointer to the function to be run) with arguments taken ** from register P2 and successors. The number of arguments is in ** the sqlite3_context object that P4 points to. ** The result of the function is stored ** in register P3. Register P3 must not be one of the function inputs. ** ** P1 is a 32-bit bitmask indicating whether or not each argument to the ** function was determined to be constant at compile time. If the first ** argument was constant then bit 0 of P1 is set. This is used to determine ** whether meta data associated with a user function argument using the ** sqlite3_set_auxdata() API may be safely retained until the next ** invocation of this opcode. ** ** See also: AggStep, AggFinal, PureFunc */ /* Opcode: PureFunc P1 P2 P3 P4 * ** Synopsis: r[P3]=func(r[P2@NP]) ** ** Invoke a user function (P4 is a pointer to an sqlite3_context object that ** contains a pointer to the function to be run) with arguments taken ** from register P2 and successors. The number of arguments is in ** the sqlite3_context object that P4 points to. ** The result of the function is stored ** in register P3. Register P3 must not be one of the function inputs. ** ** P1 is a 32-bit bitmask indicating whether or not each argument to the ** function was determined to be constant at compile time. If the first ** argument was constant then bit 0 of P1 is set. This is used to determine ** whether meta data associated with a user function argument using the ** sqlite3_set_auxdata() API may be safely retained until the next ** invocation of this opcode. ** ** This opcode works exactly like OP_Function. The only difference is in ** its name. This opcode is used in places where the function must be ** purely non-deterministic. Some built-in date/time functions can be ** either deterministic of non-deterministic, depending on their arguments. ** When those function are used in a non-deterministic way, they will check ** to see if they were called using OP_PureFunc instead of OP_Function, and ** if they were, they throw an error. ** ** See also: AggStep, AggFinal, Function */ _178: ; /* group */ _177: ; _ = libc.Int32FromInt32(0) pCtx2 = *(*uintptr)(unsafe.Pointer(pOp + 16)) /* If this function is inside of a trigger, the register array in aMem[] ** might change from one evaluation to the next. The next block of code ** checks to see if the register array has changed, and if so it ** reinitializes the relevant parts of the sqlite3_context object */ pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40 if (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FpOut != pOut { (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FpVdbe = p (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FpOut = pOut (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).Fenc = encoding i8 = int32((*Tsqlite3_context)(unsafe.Pointer(pCtx2)).Fargc) - int32(1) for { if !(i8 >= 0) { break } *(*uintptr)(unsafe.Pointer(pCtx2 + 28 + uintptr(i8)*4)) = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2+i8)*40 goto _292 _292: ; i8-- } } _ = libc.Int32FromInt32(0) (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Null)) _ = libc.Int32FromInt32(0) (*(*func(*libc.TLS, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFuncDef)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FpFunc)).FxSFunc})))(tls, pCtx2, int32((*Tsqlite3_context)(unsafe.Pointer(pCtx2)).Fargc), pCtx2+28) /* IMP: R-24505-23230 */ /* If the function returned an error, throw an exception */ if (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FisError != 0 { if (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FisError > 0 { _sqlite3VdbeError(tls, p, __ccgo_ts+3795, libc.VaList(bp+736, Xsqlite3_value_text(tls, pOut))) rc = (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FisError } _sqlite3VdbeDeleteAuxData(tls, db, p+228, (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FiOp, (*TOp)(unsafe.Pointer(pOp)).Fp1) (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FisError = 0 if rc != 0 { goto abort_due_to_error } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) goto _187 /* Opcode: ClrSubtype P1 * * * * ** Synopsis: r[P1].subtype = 0 ** ** Clear the subtype from register P1. */ _179: ; /* in1 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 p293 = pIn1 + 16 *(*Tu16)(unsafe.Pointer(p293)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p293))) & ^libc.Int32FromInt32(MEM_Subtype)) goto _187 /* Opcode: GetSubtype P1 P2 * * * ** Synopsis: r[P2] = r[P1].subtype ** ** Extract the subtype value from register P1 and write that subtype ** into register P2. If P1 has no subtype, then P1 gets a NULL. */ _180: ; /* in1 out2 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Subtype) != 0 { _sqlite3VdbeMemSetInt64(tls, pOut, int64((*TMem)(unsafe.Pointer(pIn1)).FeSubtype)) } else { _sqlite3VdbeMemSetNull(tls, pOut) } goto _187 /* Opcode: SetSubtype P1 P2 * * * ** Synopsis: r[P2].subtype = r[P1] ** ** Set the subtype value of register P2 to the integer from register P1. ** If P1 is NULL, clear the subtype from p2. */ _181: ; /* in1 out2 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40 if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Null) != 0 { p294 = pOut + 16 *(*Tu16)(unsafe.Pointer(p294)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p294))) & ^libc.Int32FromInt32(MEM_Subtype)) } else { _ = libc.Int32FromInt32(0) p295 = pOut + 16 *(*Tu16)(unsafe.Pointer(p295)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p295))) | libc.Int32FromInt32(MEM_Subtype)) (*TMem)(unsafe.Pointer(pOut)).FeSubtype = uint8(*(*Ti64)(unsafe.Pointer(pIn1)) & libc.Int64FromInt32(0xff)) } goto _187 /* Opcode: FilterAdd P1 * P3 P4 * ** Synopsis: filter(P1) += key(P3@P4) ** ** Compute a hash on the P4 registers starting with r[P3] and ** add that hash to the bloom filter contained in r[P1]. */ _182: ; _ = libc.Int32FromInt32(0) pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) h = _filterHash(tls, aMem, pOp) h %= uint64((*TMem)(unsafe.Pointer(pIn1)).Fn * libc.Int32FromInt32(8)) p296 = (*TMem)(unsafe.Pointer(pIn1)).Fz + uintptr(h/uint64(8)) *(*uint8)(unsafe.Pointer(p296)) = uint8(int32(*(*uint8)(unsafe.Pointer(p296))) | libc.Int32FromInt32(1)<<(h&libc.Uint64FromInt32(7))) goto _187 /* Opcode: Filter P1 P2 P3 P4 * ** Synopsis: if key(P3@P4) not in filter(P1) goto P2 ** ** Compute a hash on the key contained in the P4 registers starting ** with r[P3]. Check to see if that hash is found in the ** bloom filter hosted by register P1. If it is not present then ** maybe jump to P2. Otherwise fall through. ** ** False negatives are harmless. It is always safe to fall through, ** even if the value is in the bloom filter. A false negative causes ** more CPU cycles to be used, but it should still yield the correct ** answer. However, an incorrect answer may well arise from a ** false positive - if the jump is taken when it should fall through. */ _183: ; _ = libc.Int32FromInt32(0) pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) h1 = _filterHash(tls, aMem, pOp) h1 %= uint64((*TMem)(unsafe.Pointer(pIn1)).Fn * libc.Int32FromInt32(8)) if int32(*(*uint8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pIn1)).Fz + uintptr(h1/uint64(8)))))&(int32(1)<<(h1&uint64(7))) == 0 { *(*Tu32)(unsafe.Pointer(p + 164 + 8*4))++ goto jump_to_p2 } else { *(*Tu32)(unsafe.Pointer(p + 164 + 7*4))++ } goto _187 /* Opcode: Trace P1 P2 * P4 * ** ** Write P4 on the statement trace output if statement tracing is ** enabled. ** ** Operand P1 must be 0x7fffffff and P2 must positive. */ /* Opcode: Init P1 P2 P3 P4 * ** Synopsis: Start at P2 ** ** Programs contain a single instance of this opcode as the very first ** opcode. ** ** If tracing is enabled (by the sqlite3_trace()) interface, then ** the UTF-8 string contained in P4 is emitted on the trace callback. ** Or if P4 is blank, use the string returned by sqlite3_sql(). ** ** If P2 is not zero, jump to instruction P2. ** ** Increment the value of P1 so that OP_Once opcodes will jump the ** first time they are evaluated for this run. ** ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT ** error is encountered. */ _185: ; _184: ; /* If the P4 argument is not NULL, then it must be an SQL comment string. ** The "--" string is broken up to prevent false-positives with srcck1.c. ** ** This assert() provides evidence for: ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that ** would have been returned by the legacy sqlite3_trace() interface by ** using the X argument when X begins with "--" and invoking ** sqlite3_expanded_sql(P) otherwise. */ _ = libc.Int32FromInt32(0) /* OP_Init is always instruction 0 */ _ = libc.Int32FromInt32(0) if v299 = int32((*Tsqlite3)(unsafe.Pointer(db)).FmTrace)&(libc.Int32FromInt32(SQLITE_TRACE_STMT)|libc.Int32FromInt32(SQLITE_TRACE_LEGACY)) != 0 && int32((*TVdbe)(unsafe.Pointer(p)).FminWriteFileFormat) != int32(254); v299 { if *(*uintptr)(unsafe.Pointer(pOp + 16)) != 0 { v298 = *(*uintptr)(unsafe.Pointer(pOp + 16)) } else { v298 = (*TVdbe)(unsafe.Pointer(p)).FzSql } v297 = v298 zTrace = v297 } if v299 && v297 != uintptr(0) { if int32((*Tsqlite3)(unsafe.Pointer(db)).FmTrace)&int32(SQLITE_TRACE_LEGACY) != 0 { z2 = _sqlite3VdbeExpandSql(tls, p, zTrace) (*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).Ftrace.FxLegacy})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg, z2) Xsqlite3_free(tls, z2) } else { if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec > int32(1) { z3 = _sqlite3MPrintf(tls, db, __ccgo_ts+6263, libc.VaList(bp+736, zTrace)) (*(*func(*libc.TLS, Tu32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{*(*uintptr)(unsafe.Pointer(&(*Tsqlite3)(unsafe.Pointer(db)).Ftrace))})))(tls, uint32(SQLITE_TRACE_STMT), (*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg, p, z3) _sqlite3DbFree(tls, db, z3) } else { (*(*func(*libc.TLS, Tu32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{*(*uintptr)(unsafe.Pointer(&(*Tsqlite3)(unsafe.Pointer(db)).Ftrace))})))(tls, uint32(SQLITE_TRACE_STMT), (*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg, p, zTrace) } } } _ = libc.Int32FromInt32(0) if (*TOp)(unsafe.Pointer(pOp)).Fp1 >= _sqlite3Config.FiOnceResetThreshold { if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Trace) { goto _187 } i9 = int32(1) for { if !(i9 < (*TVdbe)(unsafe.Pointer(p)).FnOp) { break } if int32((*(*TOp)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(i9)*20))).Fopcode) == int32(OP_Once) { (*(*TOp)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(i9)*20))).Fp1 = 0 } goto _300 _300: ; i9++ } (*TOp)(unsafe.Pointer(pOp)).Fp1 = 0 } (*TOp)(unsafe.Pointer(pOp)).Fp1++ *(*Tu32)(unsafe.Pointer(p + 164 + 6*4))++ goto jump_to_p2 /* Opcode: Noop * * * * * ** ** Do nothing. This instruction is often useful as a jump ** destination. */ /* ** The magic Explain opcode are only inserted when explain==2 (which ** is to say when the EXPLAIN QUERY PLAN syntax is used.) ** This opcode records information from the optimizer. It is the ** the same as a no-op. This opcodesnever appears in a real VM program. */ _186: ; /* This is really OP_Noop, OP_Explain */ _ = libc.Int32FromInt32(0) goto _187 /***************************************************************************** ** The cases of the switch statement above this line should all be indented ** by 6 spaces. But the left-most 6 spaces have been removed to improve the ** readability. From this point on down, the normal indentation rules are ** restored. *****************************************************************************/ _187: ; /* The following code adds nothing to the actual functionality ** of the program. It is only here for testing and debugging. ** On the other hand, it does burn CPU cycles every time through ** the evaluator loop. So we can leave it out when NDEBUG is defined. */ goto _1 _1: ; pOp += 20 } /* The end of the for(;;) loop the loops through opcodes */ /* If we reach this point, it means that execution is finished with ** an error of some kind. */ goto abort_due_to_error abort_due_to_error: ; if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { rc = int32(SQLITE_NOMEM) } else { if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(33)< 0 { _sqlite3ResetOneSchema(tls, db, int32(resetSchemaOnFault)-int32(1)) } /* This is the only way out of this procedure. We have to ** release the mutexes on btrees that were acquired at the ** top. */ goto vdbe_return vdbe_return: ; for nVmStep >= nProgressLimit && (*Tsqlite3)(unsafe.Pointer(db)).FxProgress != uintptr(0) { nProgressLimit += uint64((*Tsqlite3)(unsafe.Pointer(db)).FnProgressOps) if (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxProgress})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpProgressArg) != 0 { nProgressLimit = libc.Uint64FromUint32(0xffffffff) | libc.Uint64FromUint32(0xffffffff)< int32(4) { (*TVdbe)(unsafe.Pointer(v)).Fpc = int32(4) _ = libc.Int32FromInt32(0) rc = _sqlite3VdbeExec(tls, v) } else { rc = Xsqlite3_step(tls, (*TIncrblob)(unsafe.Pointer(p)).FpStmt) } if rc == int32(SQLITE_ROW) { pC = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(v)).FapCsr)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TVdbeCursor)(unsafe.Pointer(pC)).FnHdrParsed) > int32((*TIncrblob)(unsafe.Pointer(p)).FiCol) { v1 = *(*Tu32)(unsafe.Pointer(pC + 84 + uintptr((*TIncrblob)(unsafe.Pointer(p)).FiCol)*4)) } else { v1 = uint32(0) } type1 = v1 if type1 < uint32(12) { if type1 == uint32(0) { v2 = __ccgo_ts + 1634 } else { if type1 == uint32(7) { v3 = __ccgo_ts + 6301 } else { v3 = __ccgo_ts + 6306 } v2 = v3 } zErr = _sqlite3MPrintf(tls, (*TIncrblob)(unsafe.Pointer(p)).Fdb, __ccgo_ts+6314, libc.VaList(bp+8, v2)) rc = int32(SQLITE_ERROR) Xsqlite3_finalize(tls, (*TIncrblob)(unsafe.Pointer(p)).FpStmt) (*TIncrblob)(unsafe.Pointer(p)).FpStmt = uintptr(0) } else { (*TIncrblob)(unsafe.Pointer(p)).FiOffset = int32(*(*Tu32)(unsafe.Pointer(pC + 84 + uintptr(int32((*TIncrblob)(unsafe.Pointer(p)).FiCol)+int32((*TVdbeCursor)(unsafe.Pointer(pC)).FnField))*4))) (*TIncrblob)(unsafe.Pointer(p)).FnByte = int32(_sqlite3VdbeSerialTypeLen(tls, type1)) (*TIncrblob)(unsafe.Pointer(p)).FpCsr = *(*uintptr)(unsafe.Pointer(pC + 36)) _sqlite3BtreeIncrblobCursor(tls, (*TIncrblob)(unsafe.Pointer(p)).FpCsr) } } if rc == int32(SQLITE_ROW) { rc = SQLITE_OK } else { if (*TIncrblob)(unsafe.Pointer(p)).FpStmt != 0 { rc = Xsqlite3_finalize(tls, (*TIncrblob)(unsafe.Pointer(p)).FpStmt) (*TIncrblob)(unsafe.Pointer(p)).FpStmt = uintptr(0) if rc == SQLITE_OK { zErr = _sqlite3MPrintf(tls, (*TIncrblob)(unsafe.Pointer(p)).Fdb, __ccgo_ts+6343, libc.VaList(bp+8, iRow)) rc = int32(SQLITE_ERROR) } else { zErr = _sqlite3MPrintf(tls, (*TIncrblob)(unsafe.Pointer(p)).Fdb, __ccgo_ts+3795, libc.VaList(bp+8, Xsqlite3_errmsg(tls, (*TIncrblob)(unsafe.Pointer(p)).Fdb))) } } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pzErr)) = zErr return rc } // C documentation // // /* // ** Open a blob handle. // */ func Xsqlite3_blob_open(tls *libc.TLS, db uintptr, zDb uintptr, zTable uintptr, zColumn uintptr, iRow Tsqlite_int64, wrFlag int32, ppBlob uintptr) (r int32) { bp := tls.Alloc(304) defer tls.Free(304) var aOp, pBlob, pFKey, pIdx, pTab, v, zFault, v7 uintptr var iCol, iDb, j, j1, nAttempt, rc, v6 int32 var _ /* sParse at bp+4 */ TParse var _ /* zErr at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aOp, iCol, iDb, j, j1, nAttempt, pBlob, pFKey, pIdx, pTab, rc, v, zFault, v6, v7 nAttempt = 0 /* Index of zColumn in row-record */ rc = SQLITE_OK *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) pBlob = uintptr(0) *(*uintptr)(unsafe.Pointer(ppBlob)) = uintptr(0) wrFlag = libc.BoolInt32(!!(wrFlag != 0)) /* wrFlag = (wrFlag ? 1 : 0); */ Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) pBlob = _sqlite3DbMallocZero(tls, db, uint64(32)) for int32(1) != 0 { _sqlite3ParseObjectInit(tls, bp+4, db) if !(pBlob != 0) { goto blob_open_out } _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) _sqlite3BtreeEnterAll(tls, db) pTab = _sqlite3LocateTable(tls, bp+4, uint32(0), zTable, zDb) if pTab != 0 && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { pTab = uintptr(0) _sqlite3ErrorMsg(tls, bp+4, __ccgo_ts+6363, libc.VaList(bp+296, zTable)) } if pTab != 0 && !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { pTab = uintptr(0) _sqlite3ErrorMsg(tls, bp+4, __ccgo_ts+6393, libc.VaList(bp+296, zTable)) } if pTab != 0 && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { pTab = uintptr(0) _sqlite3ErrorMsg(tls, bp+4, __ccgo_ts+6429, libc.VaList(bp+296, zTable)) } if !(pTab != 0) { if (*(*TParse)(unsafe.Pointer(bp + 4))).FzErrMsg != 0 { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = (*(*TParse)(unsafe.Pointer(bp + 4))).FzErrMsg (*(*TParse)(unsafe.Pointer(bp + 4))).FzErrMsg = uintptr(0) } rc = int32(SQLITE_ERROR) _sqlite3BtreeLeaveAll(tls, db) goto blob_open_out } (*TIncrblob)(unsafe.Pointer(pBlob)).FpTab = pTab (*TIncrblob)(unsafe.Pointer(pBlob)).FzDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(_sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema))*16))).FzDbSName /* Now search pTab for the exact column. */ iCol = 0 for { if !(iCol < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if _sqlite3StrICmp(tls, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName, zColumn) == 0 { break } goto _1 _1: ; iCol++ } if iCol == int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = _sqlite3MPrintf(tls, db, __ccgo_ts+6450, libc.VaList(bp+296, zColumn)) rc = int32(SQLITE_ERROR) _sqlite3BtreeLeaveAll(tls, db) goto blob_open_out } /* If the value is being opened for writing, check that the ** column is not indexed, and that it is not part of a foreign key. */ if wrFlag != 0 { zFault = uintptr(0) if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) != 0 { _ = libc.Int32FromInt32(0) pFKey = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpFKey for { if !(pFKey != 0) { break } j = 0 for { if !(j < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) { break } if (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(j)*8))).FiFrom == iCol { zFault = __ccgo_ts + 6471 } goto _3 _3: ; j++ } goto _2 _2: ; pFKey = (*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom } } pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0) { break } j1 = 0 for { if !(j1 < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) { break } /* FIXME: Be smarter about indexes that use expressions */ if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j1)*2))) == iCol || int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j1)*2))) == -int32(2) { zFault = __ccgo_ts + 6483 } goto _5 _5: ; j1++ } goto _4 _4: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } if zFault != 0 { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = _sqlite3MPrintf(tls, db, __ccgo_ts+6491, libc.VaList(bp+296, zFault)) rc = int32(SQLITE_ERROR) _sqlite3BtreeLeaveAll(tls, db) goto blob_open_out } } (*TIncrblob)(unsafe.Pointer(pBlob)).FpStmt = _sqlite3VdbeCreate(tls, bp+4) _ = libc.Int32FromInt32(0) if (*TIncrblob)(unsafe.Pointer(pBlob)).FpStmt != 0 { v = (*TIncrblob)(unsafe.Pointer(pBlob)).FpStmt iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Transaction), iDb, wrFlag, (*TSchema)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FpSchema)).Fschema_cookie, (*TSchema)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FpSchema)).FiGeneration) _sqlite3VdbeChangeP5(tls, v, uint16(1)) _ = libc.Int32FromInt32(0) aOp = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(24)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_openBlob)), _iLn) /* Make sure a mutex is held on the table to be accessed */ _sqlite3VdbeUsesBtree(tls, v, iDb) if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 { _ = libc.Int32FromInt32(0) /* Configure the OP_TableLock instruction */ (*(*TVdbeOp)(unsafe.Pointer(aOp))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp))).Fp2 = int32((*TTable)(unsafe.Pointer(pTab)).Ftnum) (*(*TVdbeOp)(unsafe.Pointer(aOp))).Fp3 = wrFlag _sqlite3VdbeChangeP4(tls, v, int32(2), (*TTable)(unsafe.Pointer(pTab)).FzName, P4_TRANSIENT) } if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 { /* Remove either the OP_OpenWrite or OpenRead. Set the P2 ** parameter of the other to pTab->tnum. */ if wrFlag != 0 { (*(*TVdbeOp)(unsafe.Pointer(aOp + 1*20))).Fopcode = uint8(OP_OpenWrite) } (*(*TVdbeOp)(unsafe.Pointer(aOp + 1*20))).Fp2 = int32((*TTable)(unsafe.Pointer(pTab)).Ftnum) (*(*TVdbeOp)(unsafe.Pointer(aOp + 1*20))).Fp3 = iDb /* Configure the number of columns. Configure the cursor to ** think that the table has one more column than it really ** does. An OP_Column to retrieve this imaginary column will ** always return an SQL NULL. This is useful because it means ** we can invoke OP_Column to fill in the vdbe cursors type ** and offset cache without causing any IO. */ (*(*TVdbeOp)(unsafe.Pointer(aOp + 1*20))).Fp4type = int8(-libc.Int32FromInt32(3)) *(*int32)(unsafe.Pointer(aOp + 1*20 + 16)) = int32((*TTable)(unsafe.Pointer(pTab)).FnCol) + int32(1) (*(*TVdbeOp)(unsafe.Pointer(aOp + 3*20))).Fp2 = int32((*TTable)(unsafe.Pointer(pTab)).FnCol) (*(*TParse)(unsafe.Pointer(bp + 4))).FnVar = 0 (*(*TParse)(unsafe.Pointer(bp + 4))).FnMem = int32(1) (*(*TParse)(unsafe.Pointer(bp + 4))).FnTab = int32(1) _sqlite3VdbeMakeReady(tls, v, bp+4) } } (*TIncrblob)(unsafe.Pointer(pBlob)).FiCol = uint16(iCol) (*TIncrblob)(unsafe.Pointer(pBlob)).Fdb = db _sqlite3BtreeLeaveAll(tls, db) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto blob_open_out } rc = _blobSeekToRow(tls, pBlob, iRow, bp) nAttempt++ v6 = nAttempt if v6 >= int32(SQLITE_MAX_SCHEMA_RETRY) || rc != int32(SQLITE_SCHEMA) { break } _sqlite3ParseObjectReset(tls, bp+4) } goto blob_open_out blob_open_out: ; if rc == SQLITE_OK && int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 { *(*uintptr)(unsafe.Pointer(ppBlob)) = pBlob } else { if pBlob != 0 && (*TIncrblob)(unsafe.Pointer(pBlob)).FpStmt != 0 { _sqlite3VdbeFinalize(tls, (*TIncrblob)(unsafe.Pointer(pBlob)).FpStmt) } _sqlite3DbFree(tls, db, pBlob) } if *(*uintptr)(unsafe.Pointer(bp)) != 0 { v7 = __ccgo_ts + 3795 } else { v7 = libc.UintptrFromInt32(0) } _sqlite3ErrorWithMsg(tls, db, rc, v7, libc.VaList(bp+296, *(*uintptr)(unsafe.Pointer(bp)))) _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp))) _sqlite3ParseObjectReset(tls, bp+4) rc = _sqlite3ApiExit(tls, db, rc) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } /* This VDBE program seeks a btree cursor to the identified ** db/table/row entry. The reason for using a vdbe program instead ** of writing code to use the b-tree layer directly is that the ** vdbe program will take advantage of the various transaction, ** locking and error handling infrastructure built into the vdbe. ** ** After seeking the cursor, the vdbe executes an OP_ResultRow. ** Code external to the Vdbe then "borrows" the b-tree cursor and ** uses it to implement the blob_read(), blob_write() and ** blob_bytes() functions. ** ** The sqlite3_blob_close() function finalizes the vdbe program, ** which closes the b-tree cursor and (possibly) commits the ** transaction. */ var _iLn int32 var _openBlob = [6]TVdbeOpList{ 0: { Fopcode: uint8(OP_TableLock), }, 1: { Fopcode: uint8(OP_OpenRead), }, 2: { Fopcode: uint8(OP_NotExists), Fp2: int8(5), Fp3: int8(1), }, 3: { Fopcode: uint8(OP_Column), Fp3: int8(1), }, 4: { Fopcode: uint8(OP_ResultRow), Fp1: int8(1), }, 5: { Fopcode: uint8(OP_Halt), }, } // C documentation // // /* // ** Close a blob handle that was previously created using // ** sqlite3_blob_open(). // */ func Xsqlite3_blob_close(tls *libc.TLS, pBlob uintptr) (r int32) { var db, p, pStmt uintptr var rc int32 _, _, _, _ = db, p, pStmt, rc p = pBlob if p != 0 { pStmt = (*TIncrblob)(unsafe.Pointer(p)).FpStmt db = (*TIncrblob)(unsafe.Pointer(p)).Fdb Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) _sqlite3DbFree(tls, db, p) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) rc = Xsqlite3_finalize(tls, pStmt) } else { rc = SQLITE_OK } return rc } // C documentation // // /* // ** Perform a read or write operation on a blob // */ func _blobReadWrite(tls *libc.TLS, pBlob uintptr, z uintptr, n int32, iOffset int32, xCall uintptr) (r int32) { var db, p, v uintptr var iKey Tsqlite3_int64 var rc int32 _, _, _, _, _ = db, iKey, p, rc, v p = pBlob if p == uintptr(0) { return _sqlite3MisuseError(tls, int32(102190)) } db = (*TIncrblob)(unsafe.Pointer(p)).Fdb Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) v = (*TIncrblob)(unsafe.Pointer(p)).FpStmt if n < 0 || iOffset < 0 || int64(iOffset)+int64(n) > int64((*TIncrblob)(unsafe.Pointer(p)).FnByte) { /* Request is out of range. Return a transient error. */ rc = int32(SQLITE_ERROR) } else { if v == uintptr(0) { /* If there is no statement handle, then the blob-handle has ** already been invalidated. Return SQLITE_ABORT in this case. */ rc = int32(SQLITE_ABORT) } else { /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is ** returned, clean-up the statement handle. */ _ = libc.Int32FromInt32(0) _sqlite3BtreeEnterCursor(tls, (*TIncrblob)(unsafe.Pointer(p)).FpCsr) if xCall == __ccgo_fp(_sqlite3BtreePutData) && (*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 { iKey = _sqlite3BtreeIntegerKey(tls, (*TIncrblob)(unsafe.Pointer(p)).FpCsr) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbePreUpdateHook(tls, v, *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(v)).FapCsr)), int32(SQLITE_DELETE), (*TIncrblob)(unsafe.Pointer(p)).FzDb, (*TIncrblob)(unsafe.Pointer(p)).FpTab, iKey, -int32(1), int32((*TIncrblob)(unsafe.Pointer(p)).FiCol)) } rc = (*(*func(*libc.TLS, uintptr, Tu32, Tu32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xCall})))(tls, (*TIncrblob)(unsafe.Pointer(p)).FpCsr, uint32(iOffset+(*TIncrblob)(unsafe.Pointer(p)).FiOffset), uint32(n), z) _sqlite3BtreeLeaveCursor(tls, (*TIncrblob)(unsafe.Pointer(p)).FpCsr) if rc == int32(SQLITE_ABORT) { _sqlite3VdbeFinalize(tls, v) (*TIncrblob)(unsafe.Pointer(p)).FpStmt = uintptr(0) } else { (*TVdbe)(unsafe.Pointer(v)).Frc = rc } } } _sqlite3Error(tls, db, rc) rc = _sqlite3ApiExit(tls, db, rc) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** Read data from a blob handle. // */ func Xsqlite3_blob_read(tls *libc.TLS, pBlob uintptr, z uintptr, n int32, iOffset int32) (r int32) { return _blobReadWrite(tls, pBlob, z, n, iOffset, __ccgo_fp(_sqlite3BtreePayloadChecked)) } // C documentation // // /* // ** Write data to a blob handle. // */ func Xsqlite3_blob_write(tls *libc.TLS, pBlob uintptr, z uintptr, n int32, iOffset int32) (r int32) { return _blobReadWrite(tls, pBlob, z, n, iOffset, __ccgo_fp(_sqlite3BtreePutData)) } // C documentation // // /* // ** Query a blob handle for the size of the data. // ** // ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob // ** so no mutex is required for access. // */ func Xsqlite3_blob_bytes(tls *libc.TLS, pBlob uintptr) (r int32) { var p uintptr var v1 int32 _, _ = p, v1 p = pBlob if p != 0 && (*TIncrblob)(unsafe.Pointer(p)).FpStmt != 0 { v1 = (*TIncrblob)(unsafe.Pointer(p)).FnByte } else { v1 = 0 } return v1 } // C documentation // // /* // ** Move an existing blob handle to point to a different row of the same // ** database table. // ** // ** If an error occurs, or if the specified row does not exist or does not // ** contain a blob or text value, then an error code is returned and the // ** database handle error code and message set. If this happens, then all // ** subsequent calls to sqlite3_blob_xxx() functions (except blob_close()) // ** immediately return SQLITE_ABORT. // */ func Xsqlite3_blob_reopen(tls *libc.TLS, pBlob uintptr, iRow Tsqlite3_int64) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var db, p, v1 uintptr var rc int32 var _ /* zErr at bp+0 */ uintptr _, _, _, _ = db, p, rc, v1 p = pBlob if p == uintptr(0) { return _sqlite3MisuseError(tls, int32(102290)) } db = (*TIncrblob)(unsafe.Pointer(p)).Fdb Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if (*TIncrblob)(unsafe.Pointer(p)).FpStmt == uintptr(0) { /* If there is no statement handle, then the blob-handle has ** already been invalidated. Return SQLITE_ABORT in this case. */ rc = int32(SQLITE_ABORT) } else { (*TVdbe)(unsafe.Pointer((*TIncrblob)(unsafe.Pointer(p)).FpStmt)).Frc = SQLITE_OK rc = _blobSeekToRow(tls, p, iRow, bp) if rc != SQLITE_OK { if *(*uintptr)(unsafe.Pointer(bp)) != 0 { v1 = __ccgo_ts + 3795 } else { v1 = libc.UintptrFromInt32(0) } _sqlite3ErrorWithMsg(tls, db, rc, v1, libc.VaList(bp+16, *(*uintptr)(unsafe.Pointer(bp)))) _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp))) } _ = libc.Int32FromInt32(0) } rc = _sqlite3ApiExit(tls, db, rc) _ = libc.Int32FromInt32(0) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } /************** End of vdbeblob.c ********************************************/ /************** Begin file vdbesort.c ****************************************/ /* ** 2011-07-09 ** ** 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 contains code for the VdbeSorter object, used in concert with ** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements ** or by SELECT statements with ORDER BY clauses that cannot be satisfied ** using indexes and without LIMIT clauses. ** ** The VdbeSorter object implements a multi-threaded external merge sort ** algorithm that is efficient even if the number of elements being sorted ** exceeds the available memory. ** ** Here is the (internal, non-API) interface between this module and the ** rest of the SQLite system: ** ** sqlite3VdbeSorterInit() Create a new VdbeSorter object. ** ** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter ** object. The row is a binary blob in the ** OP_MakeRecord format that contains both ** the ORDER BY key columns and result columns ** in the case of a SELECT w/ ORDER BY, or ** the complete record for an index entry ** in the case of a CREATE INDEX. ** ** sqlite3VdbeSorterRewind() Sort all content previously added. ** Position the read cursor on the ** first sorted element. ** ** sqlite3VdbeSorterNext() Advance the read cursor to the next sorted ** element. ** ** sqlite3VdbeSorterRowkey() Return the complete binary blob for the ** row currently under the read cursor. ** ** sqlite3VdbeSorterCompare() Compare the binary blob for the row ** currently under the read cursor against ** another binary blob X and report if ** X is strictly less than the read cursor. ** Used to enforce uniqueness in a ** CREATE UNIQUE INDEX statement. ** ** sqlite3VdbeSorterClose() Close the VdbeSorter object and reclaim ** all resources. ** ** sqlite3VdbeSorterReset() Refurbish the VdbeSorter for reuse. This ** is like Close() followed by Init() only ** much faster. ** ** The interfaces above must be called in a particular order. Write() can ** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and ** Compare() can only occur in between Rewind() and Close()/Reset(). i.e. ** ** Init() ** for each record: Write() ** Rewind() ** Rowkey()/Compare() ** Next() ** Close() ** ** Algorithm: ** ** Records passed to the sorter via calls to Write() are initially held ** unsorted in main memory. Assuming the amount of memory used never exceeds ** a threshold, when Rewind() is called the set of records is sorted using ** an in-memory merge sort. In this case, no temporary files are required ** and subsequent calls to Rowkey(), Next() and Compare() read records ** directly from main memory. ** ** If the amount of space used to store records in main memory exceeds the ** threshold, then the set of records currently in memory are sorted and ** written to a temporary file in "Packed Memory Array" (PMA) format. ** A PMA created at this point is known as a "level-0 PMA". Higher levels ** of PMAs may be created by merging existing PMAs together - for example ** merging two or more level-0 PMAs together creates a level-1 PMA. ** ** The threshold for the amount of main memory to use before flushing ** records to a PMA is roughly the same as the limit configured for the ** page-cache of the main database. Specifically, the threshold is set to ** the value returned by "PRAGMA main.page_size" multiplied by ** that returned by "PRAGMA main.cache_size", in bytes. ** ** If the sorter is running in single-threaded mode, then all PMAs generated ** are appended to a single temporary file. Or, if the sorter is running in ** multi-threaded mode then up to (N+1) temporary files may be opened, where ** N is the configured number of worker threads. In this case, instead of ** sorting the records and writing the PMA to a temporary file itself, the ** calling thread usually launches a worker thread to do so. Except, if ** there are already N worker threads running, the main thread does the work ** itself. ** ** The sorter is running in multi-threaded mode if (a) the library was built ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater ** than zero, and (b) worker threads have been enabled at runtime by calling ** "PRAGMA threads=N" with some value of N greater than 0. ** ** When Rewind() is called, any data remaining in memory is flushed to a ** final PMA. So at this point the data is stored in some number of sorted ** PMAs within temporary files on disk. ** ** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the ** sorter is running in single-threaded mode, then these PMAs are merged ** incrementally as keys are retrieved from the sorter by the VDBE. The ** MergeEngine object, described in further detail below, performs this ** merge. ** ** Or, if running in multi-threaded mode, then a background thread is ** launched to merge the existing PMAs. Once the background thread has ** merged T bytes of data into a single sorted PMA, the main thread ** begins reading keys from that PMA while the background thread proceeds ** with merging the next T bytes of data. And so on. ** ** Parameter T is set to half the value of the memory threshold used ** by Write() above to determine when to create a new PMA. ** ** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when ** Rewind() is called, then a hierarchy of incremental-merges is used. ** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on ** disk are merged together. Then T bytes of data from the second set, and ** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT ** PMAs at a time. This done is to improve locality. ** ** If running in multi-threaded mode and there are more than ** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more ** than one background thread may be created. Specifically, there may be ** one background thread for each temporary file on disk, and one background ** thread to merge the output of each of the others to a single PMA for ** the main thread to read from. */ /* #include "sqliteInt.h" */ /* #include "vdbeInt.h" */ /* ** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various ** messages to stderr that may be helpful in understanding the performance ** characteristics of the sorter in multi-threaded mode. */ /* ** Hard-coded maximum amount of data to accumulate in memory before flushing ** to a level 0 PMA. The purpose of this limit is to prevent various integer ** overflows. 512MiB. */ // C documentation // // /* // ** Private objects used by the sorter // */ type TMergeEngine = struct { FnTree int32 FpTask uintptr FaTree uintptr FaReadr uintptr } type MergeEngine = TMergeEngine /* Merge PMAs together */ type TPmaReader = struct { F__ccgo_align [0]uint32 FiReadOff Ti64 FiEof Ti64 FnAlloc int32 FnKey int32 FpFd uintptr FaAlloc uintptr FaKey uintptr FaBuffer uintptr FnBuffer int32 FaMap uintptr FpIncr uintptr F__ccgo_pad11 [4]byte } type PmaReader = TPmaReader /* Incrementally read one PMA */ type TPmaWriter = struct { F__ccgo_align [0]uint32 FeFWErr int32 FaBuffer uintptr FnBuffer int32 FiBufStart int32 FiBufEnd int32 F__ccgo_align5 [4]byte FiWriteOff Ti64 FpFd uintptr F__ccgo_pad7 [4]byte } type PmaWriter = TPmaWriter /* Incrementally write one PMA */ type TSorterRecord = struct { FnVal int32 Fu struct { FiNext [0]int32 FpNext uintptr } } type SorterRecord = TSorterRecord /* A record being sorted */ type TSortSubtask = struct { F__ccgo_align [0]uint32 FpThread uintptr FbDone int32 FnPMA int32 FpSorter uintptr FpUnpacked uintptr F__ccgo_align5 [4]byte Flist TSorterList FxCompare TSorterCompare F__ccgo_align7 [4]byte Ffile TSorterFile Ffile2 TSorterFile } type SortSubtask = TSortSubtask /* A sub-task in the sort process */ type TSorterFile = struct { F__ccgo_align [0]uint32 FpFd uintptr F__ccgo_align1 [4]byte FiEof Ti64 } type SorterFile = TSorterFile /* Temporary file object wrapper */ type TSorterList = struct { F__ccgo_align [0]uint32 FpList uintptr FaMemory uintptr FszPMA Ti64 } type SorterList = TSorterList /* In-memory list of records */ type TIncrMerger = struct { F__ccgo_align [0]uint32 FpTask uintptr FpMerger uintptr FiStartOff Ti64 FmxSz int32 FbEof int32 FbUseThread int32 F__ccgo_align6 [4]byte FaFile [2]TSorterFile } type IncrMerger = TIncrMerger /* Read & merge multiple PMAs */ /* ** A container for a temp file handle and the current amount of data ** stored in the file. */ type TSorterFile1 = struct { F__ccgo_align [0]uint32 FpFd uintptr F__ccgo_align1 [4]byte FiEof Ti64 } type SorterFile1 = TSorterFile1 /* ** An in-memory list of objects to be sorted. ** ** If aMemory==0 then each object is allocated separately and the objects ** are connected using SorterRecord.u.pNext. If aMemory!=0 then all objects ** are stored in the aMemory[] bulk memory, one right after the other, and ** are connected using SorterRecord.u.iNext. */ type TSorterList1 = struct { F__ccgo_align [0]uint32 FpList uintptr FaMemory uintptr FszPMA Ti64 } type SorterList1 = TSorterList1 /* ** The MergeEngine object is used to combine two or more smaller PMAs into ** one big PMA using a merge operation. Separate PMAs all need to be ** combined into one big PMA in order to be able to step through the sorted ** records in order. ** ** The aReadr[] array contains a PmaReader object for each of the PMAs being ** merged. An aReadr[] object either points to a valid key or else is at EOF. ** ("EOF" means "End Of File". When aReadr[] is at EOF there is no more data.) ** For the purposes of the paragraphs below, we assume that the array is ** actually N elements in size, where N is the smallest power of 2 greater ** to or equal to the number of PMAs being merged. The extra aReadr[] elements ** are treated as if they are empty (always at EOF). ** ** The aTree[] array is also N elements in size. The value of N is stored in ** the MergeEngine.nTree variable. ** ** The final (N/2) elements of aTree[] contain the results of comparing ** pairs of PMA keys together. Element i contains the result of ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the ** aTree element is set to the index of it. ** ** For the purposes of this comparison, EOF is considered greater than any ** other key value. If the keys are equal (only possible with two EOF ** values), it doesn't matter which index is stored. ** ** The (N/4) elements of aTree[] that precede the final (N/2) described ** above contains the index of the smallest of each block of 4 PmaReaders ** And so on. So that aTree[1] contains the index of the PmaReader that ** currently points to the smallest key value. aTree[0] is unused. ** ** Example: ** ** aReadr[0] -> Banana ** aReadr[1] -> Feijoa ** aReadr[2] -> Elderberry ** aReadr[3] -> Currant ** aReadr[4] -> Grapefruit ** aReadr[5] -> Apple ** aReadr[6] -> Durian ** aReadr[7] -> EOF ** ** aTree[] = { X, 5 0, 5 0, 3, 5, 6 } ** ** The current element is "Apple" (the value of the key indicated by ** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will ** be advanced to the next key in its segment. Say the next key is ** "Eggplant": ** ** aReadr[5] -> Eggplant ** ** The contents of aTree[] are updated first by comparing the new PmaReader ** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader ** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree. ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader ** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Bananafile2. And instead of using a ** background thread to prepare data for the PmaReader, with a single ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with ** keys from pMerger by the calling thread whenever the PmaReader runs out ** of data. */ type TIncrMerger1 = struct { F__ccgo_align [0]uint32 FpTask uintptr FpMerger uintptr FiStartOff Ti64 FmxSz int32 FbEof int32 FbUseThread int32 F__ccgo_align6 [4]byte FaFile [2]TSorterFile } type IncrMerger1 = TIncrMerger1 /* ** An instance of this object is used for writing a PMA. ** ** The PMA is written one record at a time. Each record is of an arbitrary ** size. But I/O is more efficient if it occurs in page-sized blocks where ** each block is aligned on a page boundary. This object caches writes to ** the PMA so that aligned, page-size blocks are written. */ type TPmaWriter1 = struct { F__ccgo_align [0]uint32 FeFWErr int32 FaBuffer uintptr FnBuffer int32 FiBufStart int32 FiBufEnd int32 F__ccgo_align5 [4]byte FiWriteOff Ti64 FpFd uintptr F__ccgo_pad7 [4]byte } type PmaWriter1 = TPmaWriter1 /* ** This object is the header on a single record while that record is being ** held in memory and prior to being written out as part of a PMA. ** ** How the linked list is connected depends on how memory is being managed ** by this module. If using a separate allocation for each in-memory record ** (VdbeSorter.list.aMemory==0), then the list is always connected using the ** SorterRecord.u.pNext pointers. ** ** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0), ** then while records are being accumulated the list is linked using the ** SorterRecord.u.iNext offset. This is because the aMemory[] array may ** be sqlite3Realloc()ed while records are being accumulated. Once the VM ** has finished passing records to the sorter, or when the in-memory buffer ** is full, the list is sorted. As part of the sorting process, it is ** converted to use the SorterRecord.u.pNext pointers. See function ** vdbeSorterSort() for details. */ type TSorterRecord1 = struct { FnVal int32 Fu struct { FiNext [0]int32 FpNext uintptr } } type SorterRecord1 = TSorterRecord1 // C documentation // // /* // ** Free all memory belonging to the PmaReader object passed as the // ** argument. All structure fields are set to zero before returning. // */ func _vdbePmaReaderClear(tls *libc.TLS, pReadr uintptr) { Xsqlite3_free(tls, (*TPmaReader)(unsafe.Pointer(pReadr)).FaAlloc) Xsqlite3_free(tls, (*TPmaReader)(unsafe.Pointer(pReadr)).FaBuffer) if (*TPmaReader)(unsafe.Pointer(pReadr)).FaMap != 0 { _sqlite3OsUnfetch(tls, (*TPmaReader)(unsafe.Pointer(pReadr)).FpFd, 0, (*TPmaReader)(unsafe.Pointer(pReadr)).FaMap) } _vdbeIncrFree(tls, (*TPmaReader)(unsafe.Pointer(pReadr)).FpIncr) libc.Xmemset(tls, pReadr, 0, uint32(56)) } // C documentation // // /* // ** Read the next nByte bytes of data from the PMA p. // ** If successful, set *ppOut to point to a buffer containing the data // ** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite // ** error code. // ** // ** The buffer returned in *ppOut is only valid until the // ** next call to this function. // */ func _vdbePmaReadBlob(tls *libc.TLS, p uintptr, nByte int32, ppOut uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var aNew uintptr var iBuf, nAvail, nCopy, nRead, nRem, rc, rc1 int32 var nNew Tsqlite3_int64 var v1 int64 var _ /* aNext at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _ = aNew, iBuf, nAvail, nCopy, nNew, nRead, nRem, rc, rc1, v1 /* Bytes of data available in buffer */ if (*TPmaReader)(unsafe.Pointer(p)).FaMap != 0 { *(*uintptr)(unsafe.Pointer(ppOut)) = (*TPmaReader)(unsafe.Pointer(p)).FaMap + uintptr((*TPmaReader)(unsafe.Pointer(p)).FiReadOff) *(*Ti64)(unsafe.Pointer(p)) += int64(nByte) return SQLITE_OK } _ = libc.Int32FromInt32(0) /* If there is no more data to be read from the buffer, read the next ** p->nBuffer bytes of data from the file into it. Or, if there are less ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */ iBuf = int32((*TPmaReader)(unsafe.Pointer(p)).FiReadOff % int64((*TPmaReader)(unsafe.Pointer(p)).FnBuffer)) if iBuf == 0 { /* sqlite3OsRead() return code */ /* Determine how many bytes of data to read. */ if (*TPmaReader)(unsafe.Pointer(p)).FiEof-(*TPmaReader)(unsafe.Pointer(p)).FiReadOff > int64((*TPmaReader)(unsafe.Pointer(p)).FnBuffer) { nRead = (*TPmaReader)(unsafe.Pointer(p)).FnBuffer } else { nRead = int32((*TPmaReader)(unsafe.Pointer(p)).FiEof - (*TPmaReader)(unsafe.Pointer(p)).FiReadOff) } _ = libc.Int32FromInt32(0) /* Readr data from the file. Return early if an error occurs. */ rc = _sqlite3OsRead(tls, (*TPmaReader)(unsafe.Pointer(p)).FpFd, (*TPmaReader)(unsafe.Pointer(p)).FaBuffer, nRead, (*TPmaReader)(unsafe.Pointer(p)).FiReadOff) _ = libc.Int32FromInt32(0) if rc != SQLITE_OK { return rc } } nAvail = (*TPmaReader)(unsafe.Pointer(p)).FnBuffer - iBuf if nByte <= nAvail { /* The requested data is available in the in-memory buffer. In this ** case there is no need to make a copy of the data, just return a ** pointer into the buffer to the caller. */ *(*uintptr)(unsafe.Pointer(ppOut)) = (*TPmaReader)(unsafe.Pointer(p)).FaBuffer + uintptr(iBuf) *(*Ti64)(unsafe.Pointer(p)) += int64(nByte) } else { /* Bytes remaining to copy */ /* Extend the p->aAlloc[] allocation if required. */ if (*TPmaReader)(unsafe.Pointer(p)).FnAlloc < nByte { if int64(libc.Int32FromInt32(128)) > int64(2)*int64((*TPmaReader)(unsafe.Pointer(p)).FnAlloc) { v1 = int64(libc.Int32FromInt32(128)) } else { v1 = int64(2) * int64((*TPmaReader)(unsafe.Pointer(p)).FnAlloc) } nNew = v1 for int64(nByte) > nNew { nNew = nNew * int64(2) } aNew = _sqlite3Realloc(tls, (*TPmaReader)(unsafe.Pointer(p)).FaAlloc, uint64(nNew)) if !(aNew != 0) { return int32(SQLITE_NOMEM) } (*TPmaReader)(unsafe.Pointer(p)).FnAlloc = int32(nNew) (*TPmaReader)(unsafe.Pointer(p)).FaAlloc = aNew } /* Copy as much data as is available in the buffer into the start of ** p->aAlloc[]. */ libc.Xmemcpy(tls, (*TPmaReader)(unsafe.Pointer(p)).FaAlloc, (*TPmaReader)(unsafe.Pointer(p)).FaBuffer+uintptr(iBuf), uint32(nAvail)) *(*Ti64)(unsafe.Pointer(p)) += int64(nAvail) nRem = nByte - nAvail /* The following loop copies up to p->nBuffer bytes per iteration into ** the p->aAlloc[] buffer. */ for nRem > 0 { /* Pointer to buffer to copy data from */ nCopy = nRem if nRem > (*TPmaReader)(unsafe.Pointer(p)).FnBuffer { nCopy = (*TPmaReader)(unsafe.Pointer(p)).FnBuffer } rc1 = _vdbePmaReadBlob(tls, p, nCopy, bp) if rc1 != SQLITE_OK { return rc1 } _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TPmaReader)(unsafe.Pointer(p)).FaAlloc+uintptr(nByte-nRem), *(*uintptr)(unsafe.Pointer(bp)), uint32(nCopy)) nRem -= nCopy } *(*uintptr)(unsafe.Pointer(ppOut)) = (*TPmaReader)(unsafe.Pointer(p)).FaAlloc } return SQLITE_OK } // C documentation // // /* // ** Read a varint from the stream of data accessed by p. Set *pnOut to // ** the value read. // */ func _vdbePmaReadVarint(tls *libc.TLS, p uintptr, pnOut uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var i, iBuf, rc, v1 int32 var _ /* a at bp+16 */ uintptr var _ /* aVarint at bp+0 */ [16]Tu8 _, _, _, _ = i, iBuf, rc, v1 if (*TPmaReader)(unsafe.Pointer(p)).FaMap != 0 { *(*Ti64)(unsafe.Pointer(p)) += int64(_sqlite3GetVarint(tls, (*TPmaReader)(unsafe.Pointer(p)).FaMap+uintptr((*TPmaReader)(unsafe.Pointer(p)).FiReadOff), pnOut)) } else { iBuf = int32((*TPmaReader)(unsafe.Pointer(p)).FiReadOff % int64((*TPmaReader)(unsafe.Pointer(p)).FnBuffer)) if iBuf != 0 && (*TPmaReader)(unsafe.Pointer(p)).FnBuffer-iBuf >= int32(9) { *(*Ti64)(unsafe.Pointer(p)) += int64(_sqlite3GetVarint(tls, (*TPmaReader)(unsafe.Pointer(p)).FaBuffer+uintptr(iBuf), pnOut)) } else { i = 0 for cond := true; cond; cond = int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))))&int32(0x80) != 0 { rc = _vdbePmaReadBlob(tls, p, int32(1), bp+16) if rc != 0 { return rc } v1 = i i++ (*(*[16]Tu8)(unsafe.Pointer(bp)))[v1&int32(0xf)] = *(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))) } _sqlite3GetVarint(tls, bp, pnOut) } } return SQLITE_OK } // C documentation // // /* // ** Attempt to memory map file pFile. If successful, set *pp to point to the // ** new mapping and return SQLITE_OK. If the mapping is not attempted // ** (because the file is too large or the VFS layer is configured not to use // ** mmap), return SQLITE_OK and set *pp to NULL. // ** // ** Or, if an error occurs, return an SQLite error code. The final value of // ** *pp is undefined in this case. // */ func _vdbeSorterMapFile(tls *libc.TLS, pTask uintptr, pFile uintptr, pp uintptr) (r int32) { var pFd uintptr var rc int32 _, _ = pFd, rc rc = SQLITE_OK if (*TSorterFile)(unsafe.Pointer(pFile)).FiEof <= int64((*Tsqlite3)(unsafe.Pointer((*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).Fdb)).FnMaxSorterMmap) { pFd = (*TSorterFile)(unsafe.Pointer(pFile)).FpFd if (*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(pFd)).FpMethods)).FiVersion >= int32(3) { rc = _sqlite3OsFetch(tls, pFd, 0, int32((*TSorterFile)(unsafe.Pointer(pFile)).FiEof), pp) } } return rc } // C documentation // // /* // ** Attach PmaReader pReadr to file pFile (if it is not already attached to // ** that file) and seek it to offset iOff within the file. Return SQLITE_OK // ** if successful, or an SQLite error code if an error occurs. // */ func _vdbePmaReaderSeek(tls *libc.TLS, pTask uintptr, pReadr uintptr, pFile uintptr, iOff Ti64) (r int32) { var iBuf, nRead, pgsz, rc int32 _, _, _, _ = iBuf, nRead, pgsz, rc rc = SQLITE_OK _ = libc.Int32FromInt32(0) if _sqlite3FaultSim(tls, int32(201)) != 0 { return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(1)< (*TPmaReader)(unsafe.Pointer(pReadr)).FiEof { nRead = int32((*TPmaReader)(unsafe.Pointer(pReadr)).FiEof - (*TPmaReader)(unsafe.Pointer(pReadr)).FiReadOff) } rc = _sqlite3OsRead(tls, (*TPmaReader)(unsafe.Pointer(pReadr)).FpFd, (*TPmaReader)(unsafe.Pointer(pReadr)).FaBuffer+uintptr(iBuf), nRead, (*TPmaReader)(unsafe.Pointer(pReadr)).FiReadOff) } } return rc } // C documentation // // /* // ** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if // ** no error occurs, or an SQLite error code if one does. // */ func _vdbePmaReaderNext(tls *libc.TLS, pReadr uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bEof, rc int32 var pIncr uintptr var _ /* nRec at bp+0 */ Tu64 _, _, _ = bEof, pIncr, rc rc = SQLITE_OK /* Return Code */ *(*Tu64)(unsafe.Pointer(bp)) = uint64(0) /* Size of record in bytes */ if (*TPmaReader)(unsafe.Pointer(pReadr)).FiReadOff >= (*TPmaReader)(unsafe.Pointer(pReadr)).FiEof { pIncr = (*TPmaReader)(unsafe.Pointer(pReadr)).FpIncr bEof = int32(1) if pIncr != 0 { rc = _vdbeIncrSwap(tls, pIncr) if rc == SQLITE_OK && (*TIncrMerger)(unsafe.Pointer(pIncr)).FbEof == 0 { rc = _vdbePmaReaderSeek(tls, (*TIncrMerger)(unsafe.Pointer(pIncr)).FpTask, pReadr, pIncr+32, (*TIncrMerger)(unsafe.Pointer(pIncr)).FiStartOff) bEof = 0 } } if bEof != 0 { /* This is an EOF condition */ _vdbePmaReaderClear(tls, pReadr) return rc } } if rc == SQLITE_OK { rc = _vdbePmaReadVarint(tls, pReadr, bp) } if rc == SQLITE_OK { (*TPmaReader)(unsafe.Pointer(pReadr)).FnKey = int32(*(*Tu64)(unsafe.Pointer(bp))) rc = _vdbePmaReadBlob(tls, pReadr, int32(*(*Tu64)(unsafe.Pointer(bp))), pReadr+32) } return rc } // C documentation // // /* // ** Initialize PmaReader pReadr to scan through the PMA stored in file pFile // ** starting at offset iStart and ending at offset iEof-1. This function // ** leaves the PmaReader pointing to the first key in the PMA (or EOF if the // ** PMA is empty). // ** // ** If the pnByte parameter is NULL, then it is assumed that the file // ** contains a single PMA, and that that PMA omits the initial length varint. // */ func _vdbePmaReaderInit(tls *libc.TLS, pTask uintptr, pFile uintptr, iStart Ti64, pReadr uintptr, pnByte uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var p1 uintptr var _ /* nByte at bp+0 */ Tu64 _, _ = rc, p1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _vdbePmaReaderSeek(tls, pTask, pReadr, pFile, iStart) if rc == SQLITE_OK { *(*Tu64)(unsafe.Pointer(bp)) = uint64(0) /* Size of PMA in bytes */ rc = _vdbePmaReadVarint(tls, pReadr, bp) (*TPmaReader)(unsafe.Pointer(pReadr)).FiEof = int64(uint64((*TPmaReader)(unsafe.Pointer(pReadr)).FiReadOff) + *(*Tu64)(unsafe.Pointer(bp))) p1 = pnByte *(*Ti64)(unsafe.Pointer(p1)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(p1))) + *(*Tu64)(unsafe.Pointer(bp))) } if rc == SQLITE_OK { rc = _vdbePmaReaderNext(tls, pReadr) } return rc } // C documentation // // /* // ** A version of vdbeSorterCompare() that assumes that it has already been // ** determined that the first field of key1 is equal to the first field of // ** key2. // */ func _vdbeSorterCompareTail(tls *libc.TLS, pTask uintptr, pbKey2Cached uintptr, pKey1 uintptr, nKey1 int32, pKey2 uintptr, nKey2 int32) (r int32) { var r2 uintptr _ = r2 r2 = (*TSortSubtask)(unsafe.Pointer(pTask)).FpUnpacked if *(*int32)(unsafe.Pointer(pbKey2Cached)) == 0 { _sqlite3VdbeRecordUnpack(tls, (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).FpKeyInfo, nKey2, pKey2, r2) *(*int32)(unsafe.Pointer(pbKey2Cached)) = int32(1) } return _sqlite3VdbeRecordCompareWithSkip(tls, nKey1, pKey1, r2, int32(1)) } // C documentation // // /* // ** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, // ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences // ** used by the comparison. Return the result of the comparison. // ** // ** If IN/OUT parameter *pbKey2Cached is true when this function is called, // ** it is assumed that (pTask->pUnpacked) contains the unpacked version // ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked // ** version of key2 and *pbKey2Cached set to true before returning. // ** // ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set // ** to SQLITE_NOMEM. // */ func _vdbeSorterCompare(tls *libc.TLS, pTask uintptr, pbKey2Cached uintptr, pKey1 uintptr, nKey1 int32, pKey2 uintptr, nKey2 int32) (r int32) { var r2 uintptr _ = r2 r2 = (*TSortSubtask)(unsafe.Pointer(pTask)).FpUnpacked if !(*(*int32)(unsafe.Pointer(pbKey2Cached)) != 0) { _sqlite3VdbeRecordUnpack(tls, (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).FpKeyInfo, nKey2, pKey2, r2) *(*int32)(unsafe.Pointer(pbKey2Cached)) = int32(1) } return _sqlite3VdbeRecordCompare(tls, nKey1, pKey1, r2) } // C documentation // // /* // ** A specially optimized version of vdbeSorterCompare() that assumes that // ** the first field of each key is a TEXT value and that the collation // ** sequence to compare them with is BINARY. // */ func _vdbeSorterCompareText(tls *libc.TLS, pTask uintptr, pbKey2Cached uintptr, pKey1 uintptr, nKey1 int32, pKey2 uintptr, nKey2 int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var p1, p2, v1, v2 uintptr var res, v11 int32 var _ /* n1 at bp+0 */ int32 var _ /* n2 at bp+4 */ int32 _, _, _, _, _, _ = p1, p2, res, v1, v2, v11 p1 = pKey1 p2 = pKey2 v1 = p1 + uintptr(*(*Tu8)(unsafe.Pointer(p1))) /* Pointer to value 1 */ v2 = p2 + uintptr(*(*Tu8)(unsafe.Pointer(p2))) *(*int32)(unsafe.Pointer(bp)) = int32(uint32(*(*Tu8)(unsafe.Pointer(p1 + 1)))) if *(*int32)(unsafe.Pointer(bp)) >= int32(0x80) { _sqlite3GetVarint32(tls, p1+1, bp) } *(*int32)(unsafe.Pointer(bp + 4)) = int32(uint32(*(*Tu8)(unsafe.Pointer(p2 + 1)))) if *(*int32)(unsafe.Pointer(bp + 4)) >= int32(0x80) { _sqlite3GetVarint32(tls, p2+1, bp+4) } if *(*int32)(unsafe.Pointer(bp)) < *(*int32)(unsafe.Pointer(bp + 4)) { v11 = *(*int32)(unsafe.Pointer(bp)) } else { v11 = *(*int32)(unsafe.Pointer(bp + 4)) } res = libc.Xmemcmp(tls, v1, v2, uint32((v11-int32(13))/int32(2))) if res == 0 { res = *(*int32)(unsafe.Pointer(bp)) - *(*int32)(unsafe.Pointer(bp + 4)) } if res == 0 { if int32((*TKeyInfo)(unsafe.Pointer((*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).FpKeyInfo)).FnKeyField) > int32(1) { res = _vdbeSorterCompareTail(tls, pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2) } } else { _ = libc.Int32FromInt32(0) if *(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer((*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).FpKeyInfo)).FaSortFlags)) != 0 { res = res * -int32(1) } } return res } // C documentation // // /* // ** A specially optimized version of vdbeSorterCompare() that assumes that // ** the first field of each key is an INTEGER value. // */ func _vdbeSorterCompareInt(tls *libc.TLS, pTask uintptr, pbKey2Cached uintptr, pKey1 uintptr, nKey1 int32, pKey2 uintptr, nKey2 int32) (r int32) { var i, res, s1, s2, v21, v3 int32 var n Tu8 var p1, p2, v1, v2 uintptr _, _, _, _, _, _, _, _, _, _, _ = i, n, p1, p2, res, s1, s2, v1, v2, v21, v3 p1 = pKey1 p2 = pKey2 s1 = int32(*(*Tu8)(unsafe.Pointer(p1 + 1))) /* Left hand serial type */ s2 = int32(*(*Tu8)(unsafe.Pointer(p2 + 1))) /* Right hand serial type */ v1 = p1 + uintptr(*(*Tu8)(unsafe.Pointer(p1))) /* Pointer to value 1 */ v2 = p2 + uintptr(*(*Tu8)(unsafe.Pointer(p2))) /* Return value */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if s1 == s2 { n = _aLen[s1] res = 0 i = 0 for { if !(i < int32(n)) { break } v21 = int32(*(*Tu8)(unsafe.Pointer(v1 + uintptr(i)))) - int32(*(*Tu8)(unsafe.Pointer(v2 + uintptr(i)))) res = v21 if v21 != 0 { if (int32(*(*Tu8)(unsafe.Pointer(v1)))^int32(*(*Tu8)(unsafe.Pointer(v2))))&int32(0x80) != 0 { if int32(*(*Tu8)(unsafe.Pointer(v1)))&int32(0x80) != 0 { v3 = -int32(1) } else { v3 = +libc.Int32FromInt32(1) } res = v3 } break } goto _1 _1: ; i++ } } else { if s1 > int32(7) && s2 > int32(7) { res = s1 - s2 } else { if s2 > int32(7) { res = +libc.Int32FromInt32(1) } else { if s1 > int32(7) { res = -int32(1) } else { res = s1 - s2 } } _ = libc.Int32FromInt32(0) if res > 0 { if int32(*(*Tu8)(unsafe.Pointer(v1)))&int32(0x80) != 0 { res = -int32(1) } } else { if int32(*(*Tu8)(unsafe.Pointer(v2)))&int32(0x80) != 0 { res = +libc.Int32FromInt32(1) } } } } if res == 0 { if int32((*TKeyInfo)(unsafe.Pointer((*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).FpKeyInfo)).FnKeyField) > int32(1) { res = _vdbeSorterCompareTail(tls, pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2) } } else { if *(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer((*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).FpKeyInfo)).FaSortFlags)) != 0 { _ = libc.Int32FromInt32(0) res = res * -int32(1) } } return res } /* The two values have the same sign. Compare using memcmp(). */ var _aLen = [10]Tu8{ 1: uint8(1), 2: uint8(2), 3: uint8(3), 4: uint8(4), 5: uint8(6), 6: uint8(8), } // C documentation // // /* // ** Initialize the temporary index cursor just opened as a sorter cursor. // ** // ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField) // ** to determine the number of fields that should be compared from the // ** records being sorted. However, if the value passed as argument nField // ** is non-zero and the sorter is able to guarantee a stable sort, nField // ** is used instead. This is used when sorting records for a CREATE INDEX // ** statement. In this case, keys are always delivered to the sorter in // ** order of the primary key, which happens to be make up the final part // ** of the records being sorted. So if the sort is stable, there is never // ** any reason to compare PK fields and they can be ignored for a small // ** performance boost. // ** // ** The sorter can guarantee a stable sort when running in single-threaded // ** mode, but not in multi-threaded mode. // ** // ** SQLITE_OK is returned if successful, or an SQLite error code otherwise. // */ func _sqlite3VdbeSorterInit(tls *libc.TLS, db uintptr, nField int32, pCsr uintptr) (r int32) { var i, nWorker, pgsz, rc, sz, szKeyInfo, v2, v5 int32 var mxCache Ti64 var pBt, pKeyInfo, pSorter, pTask, v1 uintptr var szPma Tu32 var v4 int64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, mxCache, nWorker, pBt, pKeyInfo, pSorter, pTask, pgsz, rc, sz, szKeyInfo, szPma, v1, v2, v4, v5 /* Size of pSorter in bytes */ rc = SQLITE_OK /* Initialize the upper limit on the number of worker threads */ if _sqlite3TempInMemory(tls, db) != 0 || int32(_sqlite3Config.FbCoreMutex) == 0 { nWorker = 0 } else { nWorker = *(*int32)(unsafe.Pointer(db + 120 + 11*4)) } /* Do not allow the total number of threads (main thread + all workers) ** to exceed the maximum merge count */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) szKeyInfo = int32(uint32(24) + uint32(int32((*TKeyInfo)(unsafe.Pointer((*TVdbeCursor)(unsafe.Pointer(pCsr)).FpKeyInfo)).FnKeyField)-libc.Int32FromInt32(1))*uint32(4)) sz = int32(uint32(152) + uint32(nWorker)*uint32(80)) pSorter = _sqlite3DbMallocZero(tls, db, uint64(sz+szKeyInfo)) *(*uintptr)(unsafe.Pointer(pCsr + 36)) = pSorter if pSorter == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpBt v1 = pSorter + uintptr(sz) pKeyInfo = v1 (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpKeyInfo = v1 libc.Xmemcpy(tls, pKeyInfo, (*TVdbeCursor)(unsafe.Pointer(pCsr)).FpKeyInfo, uint32(szKeyInfo)) (*TKeyInfo)(unsafe.Pointer(pKeyInfo)).Fdb = uintptr(0) if nField != 0 && nWorker == 0 { (*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnKeyField = uint16(nField) } _sqlite3BtreeEnter(tls, pBt) v2 = _sqlite3BtreeGetPageSize(tls, pBt) pgsz = v2 (*TVdbeSorter)(unsafe.Pointer(pSorter)).Fpgsz = v2 _sqlite3BtreeLeave(tls, pBt) (*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask = uint8(nWorker + int32(1)) (*TVdbeSorter)(unsafe.Pointer(pSorter)).FiPrev = uint8(nWorker - libc.Int32FromInt32(1)) (*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUseThreads = libc.BoolUint8(int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask) > libc.Int32FromInt32(1)) (*TVdbeSorter)(unsafe.Pointer(pSorter)).Fdb = db i = 0 for { if !(i < int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)) { break } pTask = pSorter + 72 + uintptr(i)*80 (*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter = pSorter goto _3 _3: ; i++ } if !(_sqlite3TempInMemory(tls, db) != 0) { /* Cache size in bytes*/ szPma = _sqlite3Config.FszPma (*TVdbeSorter)(unsafe.Pointer(pSorter)).FmnPmaSize = int32(szPma * uint32(pgsz)) mxCache = int64((*TSchema)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema)).Fcache_size) if mxCache < 0 { /* A negative cache-size value C indicates that the cache is abs(C) ** KiB in size. */ mxCache = mxCache * int64(-int32(1024)) } else { mxCache = mxCache * int64(pgsz) } if mxCache < int64(libc.Int32FromInt32(1)< int32(mxCache) { v5 = (*TVdbeSorter)(unsafe.Pointer(pSorter)).FmnPmaSize } else { v5 = int32(mxCache) } (*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxPmaSize = v5 /* Avoid large memory allocations if the application has requested ** SQLITE_CONFIG_SMALL_MALLOC. */ if int32(_sqlite3Config.FbSmallMalloc) == 0 { _ = libc.Int32FromInt32(0) (*TVdbeSorter)(unsafe.Pointer(pSorter)).FnMemory = pgsz (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory = _sqlite3Malloc(tls, uint64(pgsz)) if !((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory != 0) { rc = int32(SQLITE_NOMEM) } } } if int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnAllField) < int32(13) && (*(*uintptr)(unsafe.Pointer(pKeyInfo + 20)) == uintptr(0) || *(*uintptr)(unsafe.Pointer(pKeyInfo + 20)) == (*Tsqlite3)(unsafe.Pointer(db)).FpDfltColl) && int32(*(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags)))&int32(KEYINFO_ORDER_BIGNULL) == 0 { (*TVdbeSorter)(unsafe.Pointer(pSorter)).FtypeMask = uint8(libc.Int32FromInt32(SORTER_TYPE_INTEGER) | libc.Int32FromInt32(SORTER_TYPE_TEXT)) } } return rc } // C documentation // // /* // ** Free the list of sorted records starting at pRecord. // */ func _vdbeSorterRecordFree(tls *libc.TLS, db uintptr, pRecord uintptr) { var p, pNext uintptr _, _ = p, pNext p = pRecord for { if !(p != 0) { break } pNext = *(*uintptr)(unsafe.Pointer(p + 4)) _sqlite3DbFree(tls, db, p) goto _1 _1: ; p = pNext } } // C documentation // // /* // ** Free all resources owned by the object indicated by argument pTask. All // ** fields of *pTask are zeroed before returning. // */ func _vdbeSortSubtaskCleanup(tls *libc.TLS, db uintptr, pTask uintptr) { _sqlite3DbFree(tls, db, (*TSortSubtask)(unsafe.Pointer(pTask)).FpUnpacked) /* pTask->list.aMemory can only be non-zero if it was handed memory ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */ if (*TSortSubtask)(unsafe.Pointer(pTask)).Flist.FaMemory != 0 { Xsqlite3_free(tls, (*TSortSubtask)(unsafe.Pointer(pTask)).Flist.FaMemory) } else { _ = libc.Int32FromInt32(0) _vdbeSorterRecordFree(tls, uintptr(0), (*TSortSubtask)(unsafe.Pointer(pTask)).Flist.FpList) } if (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile.FpFd != 0 { _sqlite3OsCloseFree(tls, (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile.FpFd) } if (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile2.FpFd != 0 { _sqlite3OsCloseFree(tls, (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile2.FpFd) } libc.Xmemset(tls, pTask, 0, uint32(80)) } // C documentation // // /* // ** Join thread pTask->thread. // */ func _vdbeSorterJoinThread(tls *libc.TLS, pTask uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* pRet at bp+0 */ uintptr _ = rc rc = SQLITE_OK if (*TSortSubtask)(unsafe.Pointer(pTask)).FpThread != 0 { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(libc.Int32FromInt32(SQLITE_ERROR)) _sqlite3ThreadJoin(tls, (*TSortSubtask)(unsafe.Pointer(pTask)).FpThread, bp) rc = int32(*(*uintptr)(unsafe.Pointer(bp))) _ = libc.Int32FromInt32(0) (*TSortSubtask)(unsafe.Pointer(pTask)).FbDone = 0 (*TSortSubtask)(unsafe.Pointer(pTask)).FpThread = uintptr(0) } return rc } // C documentation // // /* // ** Launch a background thread to run xTask(pIn). // */ func _vdbeSorterCreateThread(tls *libc.TLS, pTask uintptr, xTask uintptr, pIn uintptr) (r int32) { _ = libc.Int32FromInt32(0) return _sqlite3ThreadCreate(tls, pTask, xTask, pIn) } // C documentation // // /* // ** Join all outstanding threads launched by SorterWrite() to create // ** level-0 PMAs. // */ func _vdbeSorterJoinAll(tls *libc.TLS, pSorter uintptr, rcin int32) (r int32) { var i, rc, rc2 int32 var pTask uintptr _, _, _, _ = i, pTask, rc, rc2 rc = rcin /* This function is always called by the main user thread. ** ** If this function is being called after SorterRewind() has been called, ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread ** is currently attempt to join one of the other threads. To avoid a race ** condition where this thread also attempts to join the same object, join ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */ i = int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask) - int32(1) for { if !(i >= 0) { break } pTask = pSorter + 72 + uintptr(i)*80 rc2 = _vdbeSorterJoinThread(tls, pTask) if rc == SQLITE_OK { rc = rc2 } goto _1 _1: ; i-- } return rc } // C documentation // // /* // ** Allocate a new MergeEngine object capable of handling up to // ** nReader PmaReader inputs. // ** // ** nReader is automatically rounded up to the next power of two. // ** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up. // */ func _vdbeMergeEngineNew(tls *libc.TLS, nReader int32) (r uintptr) { var N, nByte int32 var pNew, v1 uintptr _, _, _, _ = N, nByte, pNew, v1 N = int32(2) /* Pointer to allocated object to return */ _ = libc.Int32FromInt32(0) for N < nReader { N += N } nByte = int32(uint32(16) + uint32(N)*(libc.Uint32FromInt64(4)+libc.Uint32FromInt64(56))) if _sqlite3FaultSim(tls, int32(100)) != 0 { v1 = uintptr(0) } else { v1 = _sqlite3MallocZero(tls, uint64(nByte)) } pNew = v1 if pNew != 0 { (*TMergeEngine)(unsafe.Pointer(pNew)).FnTree = N (*TMergeEngine)(unsafe.Pointer(pNew)).FpTask = uintptr(0) (*TMergeEngine)(unsafe.Pointer(pNew)).FaReadr = pNew + 1*16 (*TMergeEngine)(unsafe.Pointer(pNew)).FaTree = (*TMergeEngine)(unsafe.Pointer(pNew)).FaReadr + uintptr(N)*56 } return pNew } // C documentation // // /* // ** Free the MergeEngine object passed as the only argument. // */ func _vdbeMergeEngineFree(tls *libc.TLS, pMerger uintptr) { var i int32 _ = i if pMerger != 0 { i = 0 for { if !(i < (*TMergeEngine)(unsafe.Pointer(pMerger)).FnTree) { break } _vdbePmaReaderClear(tls, (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr+uintptr(i)*56) goto _1 _1: ; i++ } } Xsqlite3_free(tls, pMerger) } // C documentation // // /* // ** Free all resources associated with the IncrMerger object indicated by // ** the first argument. // */ func _vdbeIncrFree(tls *libc.TLS, pIncr uintptr) { if pIncr != 0 { if (*TIncrMerger)(unsafe.Pointer(pIncr)).FbUseThread != 0 { _vdbeSorterJoinThread(tls, (*TIncrMerger)(unsafe.Pointer(pIncr)).FpTask) if (*(*TSorterFile)(unsafe.Pointer(pIncr + 32))).FpFd != 0 { _sqlite3OsCloseFree(tls, (*(*TSorterFile)(unsafe.Pointer(pIncr + 32))).FpFd) } if (*(*TSorterFile)(unsafe.Pointer(pIncr + 32 + 1*16))).FpFd != 0 { _sqlite3OsCloseFree(tls, (*(*TSorterFile)(unsafe.Pointer(pIncr + 32 + 1*16))).FpFd) } } _vdbeMergeEngineFree(tls, (*TIncrMerger)(unsafe.Pointer(pIncr)).FpMerger) Xsqlite3_free(tls, pIncr) } } // C documentation // // /* // ** Reset a sorting cursor back to its original empty state. // */ func _sqlite3VdbeSorterReset(tls *libc.TLS, db uintptr, pSorter uintptr) { var i int32 var pTask uintptr _, _ = i, pTask _vdbeSorterJoinAll(tls, pSorter, SQLITE_OK) _ = libc.Int32FromInt32(0) if (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpReader != 0 { _vdbePmaReaderClear(tls, (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpReader) _sqlite3DbFree(tls, db, (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpReader) (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpReader = uintptr(0) } _vdbeMergeEngineFree(tls, (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpMerger) (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpMerger = uintptr(0) i = 0 for { if !(i < int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)) { break } pTask = pSorter + 72 + uintptr(i)*80 _vdbeSortSubtaskCleanup(tls, db, pTask) (*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter = pSorter goto _1 _1: ; i++ } if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory == uintptr(0) { _vdbeSorterRecordFree(tls, uintptr(0), (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList) } (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList = uintptr(0) (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FszPMA = 0 (*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUsePMA = uint8(0) (*TVdbeSorter)(unsafe.Pointer(pSorter)).FiMemory = 0 (*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxKeysize = 0 _sqlite3DbFree(tls, db, (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpUnpacked) (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpUnpacked = uintptr(0) } // C documentation // // /* // ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines. // */ func _sqlite3VdbeSorterClose(tls *libc.TLS, db uintptr, pCsr uintptr) { var pSorter uintptr _ = pSorter _ = libc.Int32FromInt32(0) pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 36)) if pSorter != 0 { _sqlite3VdbeSorterReset(tls, db, pSorter) Xsqlite3_free(tls, (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory) _sqlite3DbFree(tls, db, pSorter) *(*uintptr)(unsafe.Pointer(pCsr + 36)) = uintptr(0) } } // C documentation // // /* // ** The first argument is a file-handle open on a temporary file. The file // ** is guaranteed to be nByte bytes or smaller in size. This function // ** attempts to extend the file to nByte bytes in size and to ensure that // ** the VFS has memory mapped it. // ** // ** Whether or not the file does end up memory mapped of course depends on // ** the specific VFS implementation. // */ func _vdbeSorterExtendFile(tls *libc.TLS, db uintptr, pFd uintptr, _nByte Ti64) { bp := tls.Alloc(16) defer tls.Free(16) *(*Ti64)(unsafe.Pointer(bp)) = _nByte var _ /* chunksize at bp+12 */ int32 var _ /* p at bp+8 */ uintptr if *(*Ti64)(unsafe.Pointer(bp)) <= int64((*Tsqlite3)(unsafe.Pointer(db)).FnMaxSorterMmap) && (*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(pFd)).FpMethods)).FiVersion >= int32(3) { *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 12)) = libc.Int32FromInt32(4) * libc.Int32FromInt32(1024) _sqlite3OsFileControlHint(tls, pFd, int32(SQLITE_FCNTL_CHUNK_SIZE), bp+12) _sqlite3OsFileControlHint(tls, pFd, int32(SQLITE_FCNTL_SIZE_HINT), bp) _sqlite3OsFetch(tls, pFd, 0, int32(*(*Ti64)(unsafe.Pointer(bp))), bp+8) if *(*uintptr)(unsafe.Pointer(bp + 8)) != 0 { _sqlite3OsUnfetch(tls, pFd, 0, *(*uintptr)(unsafe.Pointer(bp + 8))) } } } // C documentation // // /* // ** Allocate space for a file-handle and open a temporary file. If successful, // ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK. // ** Otherwise, set *ppFd to 0 and return an SQLite error code. // */ func _vdbeSorterOpenTempFile(tls *libc.TLS, db uintptr, nExtend Ti64, ppFd uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* max at bp+8 */ Ti64 var _ /* rc at bp+0 */ int32 if _sqlite3FaultSim(tls, int32(202)) != 0 { return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(13)< 0 { _vdbeSorterExtendFile(tls, db, *(*uintptr)(unsafe.Pointer(ppFd)), nExtend) } } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** If it has not already been allocated, allocate the UnpackedRecord // ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or // ** if no allocation was required), or SQLITE_NOMEM otherwise. // */ func _vdbeSortAllocUnpacked(tls *libc.TLS, pTask uintptr) (r int32) { if (*TSortSubtask)(unsafe.Pointer(pTask)).FpUnpacked == uintptr(0) { (*TSortSubtask)(unsafe.Pointer(pTask)).FpUnpacked = _sqlite3VdbeAllocUnpackedRecord(tls, (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).FpKeyInfo) if (*TSortSubtask)(unsafe.Pointer(pTask)).FpUnpacked == uintptr(0) { return int32(SQLITE_NOMEM) } (*TUnpackedRecord)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpUnpacked)).FnField = (*TKeyInfo)(unsafe.Pointer((*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).FpKeyInfo)).FnKeyField (*TUnpackedRecord)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpUnpacked)).FerrCode = uint8(0) } return SQLITE_OK } // C documentation // // /* // ** Merge the two sorted lists p1 and p2 into a single list. // */ func _vdbeSorterMerge(tls *libc.TLS, pTask uintptr, p1 uintptr, p2 uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var pp uintptr var res int32 var _ /* bCached at bp+4 */ int32 var _ /* pFinal at bp+0 */ uintptr _, _ = pp, res *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) pp = bp *(*int32)(unsafe.Pointer(bp + 4)) = 0 _ = libc.Int32FromInt32(0) for { res = (*(*func(*libc.TLS, uintptr, uintptr, uintptr, int32, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*TSortSubtask)(unsafe.Pointer(pTask)).FxCompare})))(tls, pTask, bp+4, p1+libc.UintptrFromInt32(1)*8, (*TSorterRecord)(unsafe.Pointer(p1)).FnVal, p2+libc.UintptrFromInt32(1)*8, (*TSorterRecord)(unsafe.Pointer(p2)).FnVal) if res <= 0 { *(*uintptr)(unsafe.Pointer(pp)) = p1 pp = p1 + 4 p1 = *(*uintptr)(unsafe.Pointer(p1 + 4)) if p1 == uintptr(0) { *(*uintptr)(unsafe.Pointer(pp)) = p2 break } } else { *(*uintptr)(unsafe.Pointer(pp)) = p2 pp = p2 + 4 p2 = *(*uintptr)(unsafe.Pointer(p2 + 4)) *(*int32)(unsafe.Pointer(bp + 4)) = 0 if p2 == uintptr(0) { *(*uintptr)(unsafe.Pointer(pp)) = p1 break } } goto _1 _1: } return *(*uintptr)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Return the SorterCompare function to compare values collected by the // ** sorter object passed as the only argument. // */ func _vdbeSorterGetCompare(tls *libc.TLS, p uintptr) (r TSorterCompare) { if int32((*TVdbeSorter)(unsafe.Pointer(p)).FtypeMask) == int32(SORTER_TYPE_INTEGER) { return __ccgo_fp(_vdbeSorterCompareInt) } else { if int32((*TVdbeSorter)(unsafe.Pointer(p)).FtypeMask) == int32(SORTER_TYPE_TEXT) { return __ccgo_fp(_vdbeSorterCompareText) } } return __ccgo_fp(_vdbeSorterCompare) } // C documentation // // /* // ** Sort the linked list of records headed at pTask->pList. Return // ** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if // ** an error occurs. // */ func _vdbeSorterSort(tls *libc.TLS, pTask uintptr, pList uintptr) (r int32) { bp := tls.Alloc(256) defer tls.Free(256) var i, rc int32 var p, pNext, v3 uintptr var _ /* aSlot at bp+0 */ [64]uintptr _, _, _, _, _ = i, p, pNext, rc, v3 rc = _vdbeSortAllocUnpacked(tls, pTask) if rc != SQLITE_OK { return rc } p = (*TSorterList)(unsafe.Pointer(pList)).FpList (*TSortSubtask)(unsafe.Pointer(pTask)).FxCompare = _vdbeSorterGetCompare(tls, (*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter) libc.Xmemset(tls, bp, 0, uint32(256)) for p != 0 { if (*TSorterList)(unsafe.Pointer(pList)).FaMemory != 0 { if p == (*TSorterList)(unsafe.Pointer(pList)).FaMemory { pNext = uintptr(0) } else { _ = libc.Int32FromInt32(0) pNext = (*TSorterList)(unsafe.Pointer(pList)).FaMemory + uintptr(*(*int32)(unsafe.Pointer(p + 4))) } } else { pNext = *(*uintptr)(unsafe.Pointer(p + 4)) } *(*uintptr)(unsafe.Pointer(p + 4)) = uintptr(0) i = 0 for { if !((*(*[64]uintptr)(unsafe.Pointer(bp)))[i] != 0) { break } p = _vdbeSorterMerge(tls, pTask, p, (*(*[64]uintptr)(unsafe.Pointer(bp)))[i]) (*(*[64]uintptr)(unsafe.Pointer(bp)))[i] = uintptr(0) goto _1 _1: ; i++ } (*(*[64]uintptr)(unsafe.Pointer(bp)))[i] = p p = pNext } p = uintptr(0) i = 0 for { if !(i < int32(libc.Uint32FromInt64(256)/libc.Uint32FromInt64(4))) { break } if (*(*[64]uintptr)(unsafe.Pointer(bp)))[i] == uintptr(0) { goto _2 } if p != 0 { v3 = _vdbeSorterMerge(tls, pTask, p, (*(*[64]uintptr)(unsafe.Pointer(bp)))[i]) } else { v3 = (*(*[64]uintptr)(unsafe.Pointer(bp)))[i] } p = v3 goto _2 _2: ; i++ } (*TSorterList)(unsafe.Pointer(pList)).FpList = p _ = libc.Int32FromInt32(0) return int32((*TUnpackedRecord)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpUnpacked)).FerrCode) } // C documentation // // /* // ** Initialize a PMA-writer object. // */ func _vdbePmaWriterInit(tls *libc.TLS, pFd uintptr, p uintptr, nBuf int32, iStart Ti64) { var v1 int32 _ = v1 libc.Xmemset(tls, p, 0, uint32(40)) (*TPmaWriter)(unsafe.Pointer(p)).FaBuffer = _sqlite3Malloc(tls, uint64(nBuf)) if !((*TPmaWriter)(unsafe.Pointer(p)).FaBuffer != 0) { (*TPmaWriter)(unsafe.Pointer(p)).FeFWErr = int32(SQLITE_NOMEM) } else { v1 = int32(iStart % int64(nBuf)) (*TPmaWriter)(unsafe.Pointer(p)).FiBufStart = v1 (*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd = v1 (*TPmaWriter)(unsafe.Pointer(p)).FiWriteOff = iStart - int64((*TPmaWriter)(unsafe.Pointer(p)).FiBufStart) (*TPmaWriter)(unsafe.Pointer(p)).FnBuffer = nBuf (*TPmaWriter)(unsafe.Pointer(p)).FpFd = pFd } } // C documentation // // /* // ** Write nData bytes of data to the PMA. Return SQLITE_OK // ** if successful, or an SQLite error code if an error occurs. // */ func _vdbePmaWriteBlob(tls *libc.TLS, p uintptr, pData uintptr, nData int32) { var nCopy, nRem, v1 int32 _, _, _ = nCopy, nRem, v1 nRem = nData for nRem > 0 && (*TPmaWriter)(unsafe.Pointer(p)).FeFWErr == 0 { nCopy = nRem if nCopy > (*TPmaWriter)(unsafe.Pointer(p)).FnBuffer-(*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd { nCopy = (*TPmaWriter)(unsafe.Pointer(p)).FnBuffer - (*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd } libc.Xmemcpy(tls, (*TPmaWriter)(unsafe.Pointer(p)).FaBuffer+uintptr((*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd), pData+uintptr(nData-nRem), uint32(nCopy)) *(*int32)(unsafe.Pointer(p + 16)) += nCopy if (*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd == (*TPmaWriter)(unsafe.Pointer(p)).FnBuffer { (*TPmaWriter)(unsafe.Pointer(p)).FeFWErr = _sqlite3OsWrite(tls, (*TPmaWriter)(unsafe.Pointer(p)).FpFd, (*TPmaWriter)(unsafe.Pointer(p)).FaBuffer+uintptr((*TPmaWriter)(unsafe.Pointer(p)).FiBufStart), (*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd-(*TPmaWriter)(unsafe.Pointer(p)).FiBufStart, (*TPmaWriter)(unsafe.Pointer(p)).FiWriteOff+int64((*TPmaWriter)(unsafe.Pointer(p)).FiBufStart)) v1 = libc.Int32FromInt32(0) (*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd = v1 (*TPmaWriter)(unsafe.Pointer(p)).FiBufStart = v1 *(*Ti64)(unsafe.Pointer(p + 24)) += int64((*TPmaWriter)(unsafe.Pointer(p)).FnBuffer) } _ = libc.Int32FromInt32(0) nRem -= nCopy } } // C documentation // // /* // ** Flush any buffered data to disk and clean up the PMA-writer object. // ** The results of using the PMA-writer after this call are undefined. // ** Return SQLITE_OK if flushing the buffered data succeeds or is not // ** required. Otherwise, return an SQLite error code. // ** // ** Before returning, set *piEof to the offset immediately following the // ** last byte written to the file. // */ func _vdbePmaWriterFinish(tls *libc.TLS, p uintptr, piEof uintptr) (r int32) { var rc int32 _ = rc if (*TPmaWriter)(unsafe.Pointer(p)).FeFWErr == 0 && (*TPmaWriter)(unsafe.Pointer(p)).FaBuffer != 0 && (*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd > (*TPmaWriter)(unsafe.Pointer(p)).FiBufStart { (*TPmaWriter)(unsafe.Pointer(p)).FeFWErr = _sqlite3OsWrite(tls, (*TPmaWriter)(unsafe.Pointer(p)).FpFd, (*TPmaWriter)(unsafe.Pointer(p)).FaBuffer+uintptr((*TPmaWriter)(unsafe.Pointer(p)).FiBufStart), (*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd-(*TPmaWriter)(unsafe.Pointer(p)).FiBufStart, (*TPmaWriter)(unsafe.Pointer(p)).FiWriteOff+int64((*TPmaWriter)(unsafe.Pointer(p)).FiBufStart)) } *(*Ti64)(unsafe.Pointer(piEof)) = (*TPmaWriter)(unsafe.Pointer(p)).FiWriteOff + int64((*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd) Xsqlite3_free(tls, (*TPmaWriter)(unsafe.Pointer(p)).FaBuffer) rc = (*TPmaWriter)(unsafe.Pointer(p)).FeFWErr libc.Xmemset(tls, p, 0, uint32(40)) return rc } // C documentation // // /* // ** Write value iVal encoded as a varint to the PMA. Return // ** SQLITE_OK if successful, or an SQLite error code if an error occurs. // */ func _vdbePmaWriteVarint(tls *libc.TLS, p uintptr, iVal Tu64) { bp := tls.Alloc(16) defer tls.Free(16) var nByte int32 var _ /* aByte at bp+0 */ [10]Tu8 _ = nByte nByte = _sqlite3PutVarint(tls, bp, iVal) _vdbePmaWriteBlob(tls, p, bp, nByte) } // C documentation // // /* // ** Write the current contents of in-memory linked-list pList to a level-0 // ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if // ** successful, or an SQLite error code otherwise. // ** // ** The format of a PMA is: // ** // ** * A varint. This varint contains the total number of bytes of content // ** in the PMA (not including the varint itself). // ** // ** * One or more records packed end-to-end in order of ascending keys. // ** Each record consists of a varint followed by a blob of data (the // ** key). The varint is the number of bytes in the blob of data. // */ func _vdbeSorterListToPMA(tls *libc.TLS, pTask uintptr, pList uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var db, p, pNext uintptr var rc int32 var _ /* writer at bp+0 */ TPmaWriter _, _, _, _ = db, p, pNext, rc db = (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).Fdb rc = SQLITE_OK /* Object used to write to the file */ libc.Xmemset(tls, bp, 0, uint32(40)) _ = libc.Int32FromInt32(0) /* If the first temporary PMA file has not been opened, open it now. */ if (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile.FpFd == uintptr(0) { rc = _vdbeSorterOpenTempFile(tls, db, 0, pTask+48) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } /* Try to get the file to memory map */ if rc == SQLITE_OK { _vdbeSorterExtendFile(tls, db, (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile.FpFd, (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile.FiEof+(*TSorterList)(unsafe.Pointer(pList)).FszPMA+int64(9)) } /* Sort the list */ if rc == SQLITE_OK { rc = _vdbeSorterSort(tls, pTask, pList) } if rc == SQLITE_OK { pNext = uintptr(0) _vdbePmaWriterInit(tls, (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile.FpFd, bp, (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).Fpgsz, (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile.FiEof) (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA++ _vdbePmaWriteVarint(tls, bp, uint64((*TSorterList)(unsafe.Pointer(pList)).FszPMA)) p = (*TSorterList)(unsafe.Pointer(pList)).FpList for { if !(p != 0) { break } pNext = *(*uintptr)(unsafe.Pointer(p + 4)) _vdbePmaWriteVarint(tls, bp, uint64((*TSorterRecord)(unsafe.Pointer(p)).FnVal)) _vdbePmaWriteBlob(tls, bp, p+libc.UintptrFromInt32(1)*8, (*TSorterRecord)(unsafe.Pointer(p)).FnVal) if (*TSorterList)(unsafe.Pointer(pList)).FaMemory == uintptr(0) { Xsqlite3_free(tls, p) } goto _1 _1: ; p = pNext } (*TSorterList)(unsafe.Pointer(pList)).FpList = p rc = _vdbePmaWriterFinish(tls, bp, pTask+48+8) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** Advance the MergeEngine to its next entry. // ** Set *pbEof to true there is no next entry because // ** the MergeEngine has reached the end of all its inputs. // ** // ** Return SQLITE_OK if successful or an error code if an error occurs. // */ func _vdbeMergeEngineStep(tls *libc.TLS, pMerger uintptr, pbEof uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, iPrev, iRes, rc, v2 int32 var pReadr1, pReadr2, pTask uintptr var _ /* bCached at bp+0 */ int32 _, _, _, _, _, _, _, _ = i, iPrev, iRes, pReadr1, pReadr2, pTask, rc, v2 iPrev = *(*int32)(unsafe.Pointer((*TMergeEngine)(unsafe.Pointer(pMerger)).FaTree + 1*4)) /* Index of PmaReader to advance */ pTask = (*TMergeEngine)(unsafe.Pointer(pMerger)).FpTask /* Advance the current PmaReader */ rc = _vdbePmaReaderNext(tls, (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr+uintptr(iPrev)*56) /* Update contents of aTree[] */ if rc == SQLITE_OK { /* Second PmaReader to compare */ *(*int32)(unsafe.Pointer(bp)) = 0 /* Find the first two PmaReaders to compare. The one that was just ** advanced (iPrev) and the one next to it in the array. */ pReadr1 = (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr + uintptr(iPrev&libc.Int32FromInt32(0xFFFE))*56 pReadr2 = (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr + uintptr(iPrev|libc.Int32FromInt32(0x0001))*56 i = ((*TMergeEngine)(unsafe.Pointer(pMerger)).FnTree + iPrev) / int32(2) for { if !(i > 0) { break } if (*TPmaReader)(unsafe.Pointer(pReadr1)).FpFd == uintptr(0) { iRes = +libc.Int32FromInt32(1) } else { if (*TPmaReader)(unsafe.Pointer(pReadr2)).FpFd == uintptr(0) { iRes = -int32(1) } else { iRes = (*(*func(*libc.TLS, uintptr, uintptr, uintptr, int32, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*TSortSubtask)(unsafe.Pointer(pTask)).FxCompare})))(tls, pTask, bp, (*TPmaReader)(unsafe.Pointer(pReadr1)).FaKey, (*TPmaReader)(unsafe.Pointer(pReadr1)).FnKey, (*TPmaReader)(unsafe.Pointer(pReadr2)).FaKey, (*TPmaReader)(unsafe.Pointer(pReadr2)).FnKey) } } /* If pReadr1 contained the smaller value, set aTree[i] to its index. ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this ** case there is no cache of pReadr2 in pTask->pUnpacked, so set ** pKey2 to point to the record belonging to pReadr2. ** ** Alternatively, if pReadr2 contains the smaller of the two values, ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare() ** was actually called above, then pTask->pUnpacked now contains ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent ** vdbeSorterCompare() from decoding pReadr2 again. ** ** If the two values were equal, then the value from the oldest ** PMA should be considered smaller. The VdbeSorter.aReadr[] array ** is sorted from oldest to newest, so pReadr1 contains older values ** than pReadr2 iff (pReadr1nTask-1) tasks. Except, if ** the background thread from a sub-tasks previous turn is still running, ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy, ** fall back to using the final sub-task. The first (pSorter->nTask-1) ** sub-tasks are preferred as they use background threads - the final ** sub-task uses the main thread. */ i = 0 for { if !(i < nWorker) { break } iTest = (int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FiPrev) + i + int32(1)) % nWorker pTask = pSorter + 72 + uintptr(iTest)*80 if (*TSortSubtask)(unsafe.Pointer(pTask)).FbDone != 0 { rc = _vdbeSorterJoinThread(tls, pTask) } if rc != SQLITE_OK || (*TSortSubtask)(unsafe.Pointer(pTask)).FpThread == uintptr(0) { break } goto _1 _1: ; i++ } if rc == SQLITE_OK { if i == nWorker { /* Use the foreground thread for this operation */ rc = _vdbeSorterListToPMA(tls, pSorter+72+uintptr(nWorker)*80, pSorter+40) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) aMem = (*TSortSubtask)(unsafe.Pointer(pTask)).Flist.FaMemory pCtx = pTask (*TVdbeSorter)(unsafe.Pointer(pSorter)).FiPrev = uint8((int32(pTask) - t__predefined_ptrdiff_t(pSorter+72)) / 80) (*TSortSubtask)(unsafe.Pointer(pTask)).Flist = (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList = uintptr(0) (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FszPMA = 0 if aMem != 0 { (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory = aMem (*TVdbeSorter)(unsafe.Pointer(pSorter)).FnMemory = _sqlite3MallocSize(tls, aMem) } else { if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory != 0 { (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory = _sqlite3Malloc(tls, uint64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnMemory)) if !((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory != 0) { return int32(SQLITE_NOMEM) } } } rc = _vdbeSorterCreateThread(tls, pTask, __ccgo_fp(_vdbeSorterFlushThread), pCtx) } } return rc } // C documentation // // /* // ** Add a record to the sorter. // */ func _sqlite3VdbeSorterWrite(tls *libc.TLS, pCsr uintptr, pVal uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var aNew, pNew, pSorter, p1, p2, p3 uintptr var bFlush, iListOff, nMin, rc int32 var nNew Tsqlite3_int64 var nPMA, nReq Ti64 var _ /* t at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _ = aNew, bFlush, iListOff, nMin, nNew, nPMA, nReq, pNew, pSorter, rc, p1, p2, p3 rc = SQLITE_OK /* serial type of first record field */ _ = libc.Int32FromInt32(0) pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 36)) *(*int32)(unsafe.Pointer(bp)) = int32(uint32(*(*Tu8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pVal)).Fz + 1)))) if *(*int32)(unsafe.Pointer(bp)) >= int32(0x80) { _sqlite3GetVarint32(tls, (*TMem)(unsafe.Pointer(pVal)).Fz+1, bp) } if *(*int32)(unsafe.Pointer(bp)) > 0 && *(*int32)(unsafe.Pointer(bp)) < int32(10) && *(*int32)(unsafe.Pointer(bp)) != int32(7) { p1 = pSorter + 68 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) & libc.Int32FromInt32(SORTER_TYPE_INTEGER)) } else { if *(*int32)(unsafe.Pointer(bp)) > int32(10) && *(*int32)(unsafe.Pointer(bp))&int32(0x01) != 0 { p2 = pSorter + 68 *(*Tu8)(unsafe.Pointer(p2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p2))) & libc.Int32FromInt32(SORTER_TYPE_TEXT)) } else { (*TVdbeSorter)(unsafe.Pointer(pSorter)).FtypeMask = uint8(0) } } _ = libc.Int32FromInt32(0) /* Figure out whether or not the current contents of memory should be ** flushed to a PMA before continuing. If so, do so. ** ** If using the single large allocation mode (pSorter->aMemory!=0), then ** flush the contents of memory to a new PMA if (a) at least one value is ** already in memory and (b) the new value will not fit in memory. ** ** Or, if using separate allocations for each record, flush the contents ** of memory to a PMA if either of the following are true: ** ** * The total memory allocated for the in-memory list is greater ** than (page-size * cache-size), or ** ** * The total memory allocated for the in-memory list is greater ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true. */ nReq = int64(uint32((*TMem)(unsafe.Pointer(pVal)).Fn) + uint32(8)) nPMA = int64((*TMem)(unsafe.Pointer(pVal)).Fn + _sqlite3VarintLen(tls, uint64((*TMem)(unsafe.Pointer(pVal)).Fn))) if (*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxPmaSize != 0 { if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory != 0 { bFlush = libc.BoolInt32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FiMemory != 0 && int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FiMemory)+nReq > int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxPmaSize)) } else { bFlush = libc.BoolInt32((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FszPMA > int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxPmaSize) || (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FszPMA > int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FmnPmaSize) && _sqlite3HeapNearlyFull(tls) != 0) } if bFlush != 0 { rc = _vdbeSorterFlushPMA(tls, pSorter) (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FszPMA = 0 (*TVdbeSorter)(unsafe.Pointer(pSorter)).FiMemory = 0 _ = libc.Int32FromInt32(0) } } (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FszPMA += nPMA if nPMA > int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxKeysize) { (*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxKeysize = int32(nPMA) } if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory != 0 { nMin = int32(int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FiMemory) + nReq) if nMin > (*TVdbeSorter)(unsafe.Pointer(pSorter)).FnMemory { nNew = int64(2) * int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnMemory) iListOff = -int32(1) if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList != 0 { iListOff = int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList) - int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory) } for nNew < int64(nMin) { nNew = nNew * int64(2) } if nNew > int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxPmaSize) { nNew = int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxPmaSize) } if nNew < int64(nMin) { nNew = int64(nMin) } aNew = _sqlite3Realloc(tls, (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory, uint64(nNew)) if !(aNew != 0) { return int32(SQLITE_NOMEM) } if iListOff >= 0 { (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList = aNew + uintptr(iListOff) } (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory = aNew (*TVdbeSorter)(unsafe.Pointer(pSorter)).FnMemory = int32(nNew) } pNew = (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory + uintptr((*TVdbeSorter)(unsafe.Pointer(pSorter)).FiMemory) p3 = pSorter + 56 *(*int32)(unsafe.Pointer(p3)) = int32(int64(*(*int32)(unsafe.Pointer(p3))) + (nReq+libc.Int64FromInt32(7))&int64(^libc.Int32FromInt32(7))) if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList != 0 { *(*int32)(unsafe.Pointer(pNew + 4)) = int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList) - int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory) } } else { pNew = _sqlite3Malloc(tls, uint64(nReq)) if pNew == uintptr(0) { return int32(SQLITE_NOMEM) } *(*uintptr)(unsafe.Pointer(pNew + 4)) = (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList } libc.Xmemcpy(tls, pNew+libc.UintptrFromInt32(1)*8, (*TMem)(unsafe.Pointer(pVal)).Fz, uint32((*TMem)(unsafe.Pointer(pVal)).Fn)) (*TSorterRecord)(unsafe.Pointer(pNew)).FnVal = (*TMem)(unsafe.Pointer(pVal)).Fn (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList = pNew return rc } // C documentation // // /* // ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format // ** of the data stored in aFile[1] is the same as that used by regular PMAs, // ** except that the number-of-bytes varint is omitted from the start. // */ func _vdbeIncrPopulate(tls *libc.TLS, pIncr uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var iEof, iStart Ti64 var nKey, rc, rc2 int32 var pMerger, pOut, pReader, pTask uintptr var _ /* dummy at bp+40 */ int32 var _ /* writer at bp+0 */ TPmaWriter _, _, _, _, _, _, _, _, _ = iEof, iStart, nKey, pMerger, pOut, pReader, pTask, rc, rc2 rc = SQLITE_OK iStart = (*TIncrMerger)(unsafe.Pointer(pIncr)).FiStartOff pOut = pIncr + 32 + 1*16 pTask = (*TIncrMerger)(unsafe.Pointer(pIncr)).FpTask pMerger = (*TIncrMerger)(unsafe.Pointer(pIncr)).FpMerger _ = libc.Int32FromInt32(0) _vdbePmaWriterInit(tls, (*TSorterFile)(unsafe.Pointer(pOut)).FpFd, bp, (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).Fpgsz, iStart) for rc == SQLITE_OK { pReader = (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr + uintptr(*(*int32)(unsafe.Pointer((*TMergeEngine)(unsafe.Pointer(pMerger)).FaTree + 1*4)))*56 nKey = (*TPmaReader)(unsafe.Pointer(pReader)).FnKey iEof = (*(*TPmaWriter)(unsafe.Pointer(bp))).FiWriteOff + int64((*(*TPmaWriter)(unsafe.Pointer(bp))).FiBufEnd) /* Check if the output file is full or if the input has been exhausted. ** In either case exit the loop. */ if (*TPmaReader)(unsafe.Pointer(pReader)).FpFd == uintptr(0) { break } if iEof+int64(nKey)+int64(_sqlite3VarintLen(tls, uint64(nKey))) > iStart+int64((*TIncrMerger)(unsafe.Pointer(pIncr)).FmxSz) { break } /* Write the next key to the output. */ _vdbePmaWriteVarint(tls, bp, uint64(nKey)) _vdbePmaWriteBlob(tls, bp, (*TPmaReader)(unsafe.Pointer(pReader)).FaKey, nKey) _ = libc.Int32FromInt32(0) rc = _vdbeMergeEngineStep(tls, (*TIncrMerger)(unsafe.Pointer(pIncr)).FpMerger, bp+40) } rc2 = _vdbePmaWriterFinish(tls, bp, pOut+8) if rc == SQLITE_OK { rc = rc2 } return rc } // C documentation // // /* // ** The main routine for background threads that populate aFile[1] of // ** multi-threaded IncrMerger objects. // */ func _vdbeIncrPopulateThread(tls *libc.TLS, pCtx uintptr) (r uintptr) { var pIncr, pRet uintptr _, _ = pIncr, pRet pIncr = pCtx pRet = uintptr(_vdbeIncrPopulate(tls, pIncr)) (*TSortSubtask)(unsafe.Pointer((*TIncrMerger)(unsafe.Pointer(pIncr)).FpTask)).FbDone = int32(1) return pRet } // C documentation // // /* // ** Launch a background thread to populate aFile[1] of pIncr. // */ func _vdbeIncrBgPopulate(tls *libc.TLS, pIncr uintptr) (r int32) { var p uintptr _ = p p = pIncr _ = libc.Int32FromInt32(0) return _vdbeSorterCreateThread(tls, (*TIncrMerger)(unsafe.Pointer(pIncr)).FpTask, __ccgo_fp(_vdbeIncrPopulateThread), p) } // C documentation // // /* // ** This function is called when the PmaReader corresponding to pIncr has // ** finished reading the contents of aFile[0]. Its purpose is to "refill" // ** aFile[0] such that the PmaReader should start rereading it from the // ** beginning. // ** // ** For single-threaded objects, this is accomplished by literally reading // ** keys from pIncr->pMerger and repopulating aFile[0]. // ** // ** For multi-threaded objects, all that is required is to wait until the // ** background thread is finished (if it is not already) and then swap // ** aFile[0] and aFile[1] in place. If the contents of pMerger have not // ** been exhausted, this function also launches a new background thread // ** to populate the new aFile[1]. // ** // ** SQLITE_OK is returned on success, or an SQLite error code otherwise. // */ func _vdbeIncrSwap(tls *libc.TLS, pIncr uintptr) (r int32) { var f0 TSorterFile var rc int32 _, _ = f0, rc rc = SQLITE_OK if (*TIncrMerger)(unsafe.Pointer(pIncr)).FbUseThread != 0 { rc = _vdbeSorterJoinThread(tls, (*TIncrMerger)(unsafe.Pointer(pIncr)).FpTask) if rc == SQLITE_OK { f0 = *(*TSorterFile)(unsafe.Pointer(pIncr + 32)) *(*TSorterFile)(unsafe.Pointer(pIncr + 32)) = *(*TSorterFile)(unsafe.Pointer(pIncr + 32 + 1*16)) *(*TSorterFile)(unsafe.Pointer(pIncr + 32 + 1*16)) = f0 } if rc == SQLITE_OK { if (*(*TSorterFile)(unsafe.Pointer(pIncr + 32))).FiEof == (*TIncrMerger)(unsafe.Pointer(pIncr)).FiStartOff { (*TIncrMerger)(unsafe.Pointer(pIncr)).FbEof = int32(1) } else { rc = _vdbeIncrBgPopulate(tls, pIncr) } } } else { rc = _vdbeIncrPopulate(tls, pIncr) *(*TSorterFile)(unsafe.Pointer(pIncr + 32)) = *(*TSorterFile)(unsafe.Pointer(pIncr + 32 + 1*16)) if (*(*TSorterFile)(unsafe.Pointer(pIncr + 32))).FiEof == (*TIncrMerger)(unsafe.Pointer(pIncr)).FiStartOff { (*TIncrMerger)(unsafe.Pointer(pIncr)).FbEof = int32(1) } } return rc } // C documentation // // /* // ** Allocate and return a new IncrMerger object to read data from pMerger. // ** // ** If an OOM condition is encountered, return NULL. In this case free the // ** pMerger argument before returning. // */ func _vdbeIncrMergerNew(tls *libc.TLS, pTask uintptr, pMerger uintptr, ppOut uintptr) (r int32) { var pIncr, v1, v2 uintptr var rc, v3 int32 _, _, _, _, _ = pIncr, rc, v1, v2, v3 rc = SQLITE_OK if _sqlite3FaultSim(tls, int32(100)) != 0 { v2 = uintptr(0) } else { v2 = _sqlite3MallocZero(tls, uint64(64)) } v1 = v2 *(*uintptr)(unsafe.Pointer(ppOut)) = v1 pIncr = v1 if pIncr != 0 { (*TIncrMerger)(unsafe.Pointer(pIncr)).FpMerger = pMerger (*TIncrMerger)(unsafe.Pointer(pIncr)).FpTask = pTask if (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).FmxKeysize+int32(9) > (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).FmxPmaSize/int32(2) { v3 = (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).FmxKeysize + int32(9) } else { v3 = (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).FmxPmaSize / int32(2) } (*TIncrMerger)(unsafe.Pointer(pIncr)).FmxSz = v3 (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile2.FiEof += int64((*TIncrMerger)(unsafe.Pointer(pIncr)).FmxSz) } else { _vdbeMergeEngineFree(tls, pMerger) rc = int32(SQLITE_NOMEM) } _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** Set the "use-threads" flag on object pIncr. // */ func _vdbeIncrMergerSetThreads(tls *libc.TLS, pIncr uintptr) { (*TIncrMerger)(unsafe.Pointer(pIncr)).FbUseThread = int32(1) (*TSortSubtask)(unsafe.Pointer((*TIncrMerger)(unsafe.Pointer(pIncr)).FpTask)).Ffile2.FiEof -= int64((*TIncrMerger)(unsafe.Pointer(pIncr)).FmxSz) } // C documentation // // /* // ** Recompute pMerger->aTree[iOut] by comparing the next keys on the // ** two PmaReaders that feed that entry. Neither of the PmaReaders // ** are advanced. This routine merely does the comparison. // */ func _vdbeMergeEngineCompare(tls *libc.TLS, pMerger uintptr, iOut int32) { bp := tls.Alloc(16) defer tls.Free(16) var i1, i2, iRes, res int32 var p1, p2, pTask uintptr var _ /* bCached at bp+0 */ int32 _, _, _, _, _, _, _ = i1, i2, iRes, p1, p2, pTask, res _ = libc.Int32FromInt32(0) if iOut >= (*TMergeEngine)(unsafe.Pointer(pMerger)).FnTree/int32(2) { i1 = (iOut - (*TMergeEngine)(unsafe.Pointer(pMerger)).FnTree/int32(2)) * int32(2) i2 = i1 + int32(1) } else { i1 = *(*int32)(unsafe.Pointer((*TMergeEngine)(unsafe.Pointer(pMerger)).FaTree + uintptr(iOut*int32(2))*4)) i2 = *(*int32)(unsafe.Pointer((*TMergeEngine)(unsafe.Pointer(pMerger)).FaTree + uintptr(iOut*int32(2)+int32(1))*4)) } p1 = (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr + uintptr(i1)*56 p2 = (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr + uintptr(i2)*56 if (*TPmaReader)(unsafe.Pointer(p1)).FpFd == uintptr(0) { iRes = i2 } else { if (*TPmaReader)(unsafe.Pointer(p2)).FpFd == uintptr(0) { iRes = i1 } else { pTask = (*TMergeEngine)(unsafe.Pointer(pMerger)).FpTask *(*int32)(unsafe.Pointer(bp)) = 0 _ = libc.Int32FromInt32(0) /* from vdbeSortSubtaskMain() */ res = (*(*func(*libc.TLS, uintptr, uintptr, uintptr, int32, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*TSortSubtask)(unsafe.Pointer(pTask)).FxCompare})))(tls, pTask, bp, (*TPmaReader)(unsafe.Pointer(p1)).FaKey, (*TPmaReader)(unsafe.Pointer(p1)).FnKey, (*TPmaReader)(unsafe.Pointer(p2)).FaKey, (*TPmaReader)(unsafe.Pointer(p2)).FnKey) if res <= 0 { iRes = i1 } else { iRes = i2 } } } *(*int32)(unsafe.Pointer((*TMergeEngine)(unsafe.Pointer(pMerger)).FaTree + uintptr(iOut)*4)) = iRes } // C documentation // // /* // ** Initialize the MergeEngine object passed as the second argument. Once this // ** function returns, the first key of merged data may be read from the // ** MergeEngine object in the usual fashion. // ** // ** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge // ** objects attached to the PmaReader objects that the merger reads from have // ** already been populated, but that they have not yet populated aFile[0] and // ** set the PmaReader objects up to read from it. In this case all that is // ** required is to call vdbePmaReaderNext() on each PmaReader to point it at // ** its first key. // ** // ** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use // ** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data // ** to pMerger. // ** // ** SQLITE_OK is returned if successful, or an SQLite error code otherwise. // */ func _vdbeMergeEngineInit(tls *libc.TLS, pTask uintptr, pMerger uintptr, eMode int32) (r int32) { var i, nTree, rc int32 _, _, _ = i, nTree, rc rc = SQLITE_OK /* Number of subtrees to merge */ /* Failure to allocate the merge would have been detected prior to ** invoking this routine */ _ = libc.Int32FromInt32(0) /* eMode is always INCRINIT_NORMAL in single-threaded mode */ _ = libc.Int32FromInt32(0) /* Verify that the MergeEngine is assigned to a single thread */ _ = libc.Int32FromInt32(0) (*TMergeEngine)(unsafe.Pointer(pMerger)).FpTask = pTask nTree = (*TMergeEngine)(unsafe.Pointer(pMerger)).FnTree i = 0 for { if !(i < nTree) { break } if libc.Bool(int32(SQLITE_MAX_WORKER_THREADS) > 0) && eMode == int32(INCRINIT_ROOT) { /* PmaReaders should be normally initialized in order, as if they are ** reading from the same temp file this makes for more linear file IO. ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is ** in use it will block the vdbePmaReaderNext() call while it uses ** the main thread to fill its buffer. So calling PmaReaderNext() ** on this PmaReader before any of the multi-threaded PmaReaders takes ** better advantage of multi-processor hardware. */ rc = _vdbePmaReaderNext(tls, (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr+uintptr(nTree-i-int32(1))*56) } else { rc = _vdbePmaReaderIncrInit(tls, (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr+uintptr(i)*56, INCRINIT_NORMAL) } if rc != SQLITE_OK { return rc } goto _1 _1: ; i++ } i = (*TMergeEngine)(unsafe.Pointer(pMerger)).FnTree - int32(1) for { if !(i > 0) { break } _vdbeMergeEngineCompare(tls, pMerger, i) goto _2 _2: ; i-- } return int32((*TUnpackedRecord)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpUnpacked)).FerrCode) } // C documentation // // /* // ** The PmaReader passed as the first argument is guaranteed to be an // ** incremental-reader (pReadr->pIncr!=0). This function serves to open // ** and/or initialize the temp file related fields of the IncrMerge // ** object at (pReadr->pIncr). // ** // ** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders // ** in the sub-tree headed by pReadr are also initialized. Data is then // ** loaded into the buffers belonging to pReadr and it is set to point to // ** the first key in its range. // ** // ** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed // ** to be a multi-threaded PmaReader and this function is being called in a // ** background thread. In this case all PmaReaders in the sub-tree are // ** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to // ** pReadr is populated. However, pReadr itself is not set up to point // ** to its first key. A call to vdbePmaReaderNext() is still required to do // ** that. // ** // ** The reason this function does not call vdbePmaReaderNext() immediately // ** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has // ** to block on thread (pTask->thread) before accessing aFile[1]. But, since // ** this entire function is being run by thread (pTask->thread), that will // ** lead to the current background thread attempting to join itself. // ** // ** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed // ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all // ** child-trees have already been initialized using IncrInit(INCRINIT_TASK). // ** In this case vdbePmaReaderNext() is called on all child PmaReaders and // ** the current PmaReader set to point to the first key in its range. // ** // ** SQLITE_OK is returned if successful, or an SQLite error code otherwise. // */ func _vdbePmaReaderIncrMergeInit(tls *libc.TLS, pReadr uintptr, eMode int32) (r int32) { var db, pIncr, pTask uintptr var mxSz, rc int32 _, _, _, _, _ = db, mxSz, pIncr, pTask, rc rc = SQLITE_OK pIncr = (*TPmaReader)(unsafe.Pointer(pReadr)).FpIncr pTask = (*TIncrMerger)(unsafe.Pointer(pIncr)).FpTask db = (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).Fdb /* eMode is always INCRINIT_NORMAL in single-threaded mode */ _ = libc.Int32FromInt32(0) rc = _vdbeMergeEngineInit(tls, pTask, (*TIncrMerger)(unsafe.Pointer(pIncr)).FpMerger, eMode) /* Set up the required files for pIncr. A multi-threaded IncrMerge object ** requires two temp files to itself, whereas a single-threaded object ** only requires a region of pTask->file2. */ if rc == SQLITE_OK { mxSz = (*TIncrMerger)(unsafe.Pointer(pIncr)).FmxSz if (*TIncrMerger)(unsafe.Pointer(pIncr)).FbUseThread != 0 { rc = _vdbeSorterOpenTempFile(tls, db, int64(mxSz), pIncr+32) if rc == SQLITE_OK { rc = _vdbeSorterOpenTempFile(tls, db, int64(mxSz), pIncr+32+1*16) } } else { /*if( !pIncr->bUseThread )*/ if (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile2.FpFd == uintptr(0) { _ = libc.Int32FromInt32(0) rc = _vdbeSorterOpenTempFile(tls, db, (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile2.FiEof, pTask+64) (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile2.FiEof = 0 } if rc == SQLITE_OK { (*(*TSorterFile)(unsafe.Pointer(pIncr + 32 + 1*16))).FpFd = (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile2.FpFd (*TIncrMerger)(unsafe.Pointer(pIncr)).FiStartOff = (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile2.FiEof (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile2.FiEof += int64(mxSz) } } } if rc == SQLITE_OK && (*TIncrMerger)(unsafe.Pointer(pIncr)).FbUseThread != 0 { /* Use the current thread to populate aFile[1], even though this ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object, ** then this function is already running in background thread ** pIncr->pTask->thread. ** ** If this is the INCRINIT_ROOT object, then it is running in the ** main VDBE thread. But that is Ok, as that thread cannot return ** control to the VDBE or proceed with anything useful until the ** first results are ready from this merger object anyway. */ _ = libc.Int32FromInt32(0) rc = _vdbeIncrPopulate(tls, pIncr) } if rc == SQLITE_OK && (libc.Bool(false) || eMode != int32(INCRINIT_TASK)) { rc = _vdbePmaReaderNext(tls, pReadr) } return rc } // C documentation // // /* // ** The main routine for vdbePmaReaderIncrMergeInit() operations run in // ** background threads. // */ func _vdbePmaReaderBgIncrInit(tls *libc.TLS, pCtx uintptr) (r uintptr) { var pReader, pRet uintptr _, _ = pReader, pRet pReader = pCtx pRet = uintptr(_vdbePmaReaderIncrMergeInit(tls, pReader, int32(INCRINIT_TASK))) (*TSortSubtask)(unsafe.Pointer((*TIncrMerger)(unsafe.Pointer((*TPmaReader)(unsafe.Pointer(pReader)).FpIncr)).FpTask)).FbDone = int32(1) return pRet } // C documentation // // /* // ** If the PmaReader passed as the first argument is not an incremental-reader // ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes // ** the vdbePmaReaderIncrMergeInit() function with the parameters passed to // ** this routine to initialize the incremental merge. // ** // ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1), // ** then a background thread is launched to call vdbePmaReaderIncrMergeInit(). // ** Or, if the IncrMerger is single threaded, the same function is called // ** using the current thread. // */ func _vdbePmaReaderIncrInit(tls *libc.TLS, pReadr uintptr, eMode int32) (r int32) { var pCtx, pIncr uintptr var rc int32 _, _, _ = pCtx, pIncr, rc pIncr = (*TPmaReader)(unsafe.Pointer(pReadr)).FpIncr /* Incremental merger */ rc = SQLITE_OK /* Return code */ if pIncr != 0 { _ = libc.Int32FromInt32(0) if (*TIncrMerger)(unsafe.Pointer(pIncr)).FbUseThread != 0 { pCtx = pReadr rc = _vdbeSorterCreateThread(tls, (*TIncrMerger)(unsafe.Pointer(pIncr)).FpTask, __ccgo_fp(_vdbePmaReaderBgIncrInit), pCtx) } else { rc = _vdbePmaReaderIncrMergeInit(tls, pReadr, eMode) } } return rc } // C documentation // // /* // ** Allocate a new MergeEngine object to merge the contents of nPMA level-0 // ** PMAs from pTask->file. If no error occurs, set *ppOut to point to // ** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut // ** to NULL and return an SQLite error code. // ** // ** When this function is called, *piOffset is set to the offset of the // ** first PMA to read from pTask->file. Assuming no error occurs, it is // ** set to the offset immediately following the last byte of the last // ** PMA before returning. If an error does occur, then the final value of // ** *piOffset is undefined. // */ func _vdbeMergeEngineLevel0(tls *libc.TLS, pTask uintptr, nPMA int32, piOffset uintptr, ppOut uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, rc int32 var iOff Ti64 var pNew, pReadr, v1 uintptr var _ /* nDummy at bp+0 */ Ti64 _, _, _, _, _, _ = i, iOff, pNew, pReadr, rc, v1 /* Merge engine to return */ iOff = *(*Ti64)(unsafe.Pointer(piOffset)) rc = SQLITE_OK v1 = _vdbeMergeEngineNew(tls, nPMA) pNew = v1 *(*uintptr)(unsafe.Pointer(ppOut)) = v1 if pNew == uintptr(0) { rc = int32(SQLITE_NOMEM) } i = 0 for { if !(i < nPMA && rc == SQLITE_OK) { break } *(*Ti64)(unsafe.Pointer(bp)) = 0 pReadr = (*TMergeEngine)(unsafe.Pointer(pNew)).FaReadr + uintptr(i)*56 rc = _vdbePmaReaderInit(tls, pTask, pTask+48, iOff, pReadr, bp) iOff = (*TPmaReader)(unsafe.Pointer(pReadr)).FiEof goto _2 _2: ; i++ } if rc != SQLITE_OK { _vdbeMergeEngineFree(tls, pNew) *(*uintptr)(unsafe.Pointer(ppOut)) = uintptr(0) } *(*Ti64)(unsafe.Pointer(piOffset)) = iOff return rc } // C documentation // // /* // ** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of // ** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes. // ** // ** i.e. // ** // ** nPMA<=16 -> TreeDepth() == 0 // ** nPMA<=256 -> TreeDepth() == 1 // ** nPMA<=65536 -> TreeDepth() == 2 // */ func _vdbeSorterTreeDepth(tls *libc.TLS, nPMA int32) (r int32) { var nDepth int32 var nDiv Ti64 _, _ = nDepth, nDiv nDepth = 0 nDiv = int64(SORTER_MAX_MERGE_COUNT) for nDiv < int64(nPMA) { nDiv = nDiv * int64(SORTER_MAX_MERGE_COUNT) nDepth++ } return nDepth } // C documentation // // /* // ** pRoot is the root of an incremental merge-tree with depth nDepth (according // ** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the // ** tree, counting from zero. This function adds pLeaf to the tree. // ** // ** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error // ** code is returned and pLeaf is freed. // */ func _vdbeSorterAddToTree(tls *libc.TLS, pTask uintptr, nDepth int32, iSeq int32, pRoot uintptr, pLeaf uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, iIter, nDiv, rc int32 var p, pNew, pReadr uintptr var _ /* pIncr at bp+0 */ uintptr _, _, _, _, _, _, _ = i, iIter, nDiv, p, pNew, pReadr, rc rc = SQLITE_OK nDiv = int32(1) p = pRoot rc = _vdbeIncrMergerNew(tls, pTask, pLeaf, bp) i = int32(1) for { if !(i < nDepth) { break } nDiv = nDiv * int32(SORTER_MAX_MERGE_COUNT) goto _1 _1: ; i++ } i = int32(1) for { if !(i < nDepth && rc == SQLITE_OK) { break } iIter = iSeq / nDiv % int32(SORTER_MAX_MERGE_COUNT) pReadr = (*TMergeEngine)(unsafe.Pointer(p)).FaReadr + uintptr(iIter)*56 if (*TPmaReader)(unsafe.Pointer(pReadr)).FpIncr == uintptr(0) { pNew = _vdbeMergeEngineNew(tls, int32(SORTER_MAX_MERGE_COUNT)) if pNew == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { rc = _vdbeIncrMergerNew(tls, pTask, pNew, pReadr+48) } } if rc == SQLITE_OK { p = (*TIncrMerger)(unsafe.Pointer((*TPmaReader)(unsafe.Pointer(pReadr)).FpIncr)).FpMerger nDiv = nDiv / int32(SORTER_MAX_MERGE_COUNT) } goto _2 _2: ; i++ } if rc == SQLITE_OK { (*(*TPmaReader)(unsafe.Pointer((*TMergeEngine)(unsafe.Pointer(p)).FaReadr + uintptr(iSeq%int32(SORTER_MAX_MERGE_COUNT))*56))).FpIncr = *(*uintptr)(unsafe.Pointer(bp)) } else { _vdbeIncrFree(tls, *(*uintptr)(unsafe.Pointer(bp))) } return rc } // C documentation // // /* // ** This function is called as part of a SorterRewind() operation on a sorter // ** that has already written two or more level-0 PMAs to one or more temp // ** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that // ** can be used to incrementally merge all PMAs on disk. // ** // ** If successful, SQLITE_OK is returned and *ppOut set to point to the // ** MergeEngine object at the root of the tree before returning. Or, if an // ** error occurs, an SQLite error code is returned and the final value // ** of *ppOut is undefined. // */ func _vdbeSorterMergeTreeBuild(tls *libc.TLS, pSorter uintptr, ppOut uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var i, iSeq, iTask, nDepth, nReader, rc, v3, v4 int32 var pMain, pTask uintptr var _ /* iReadOff at bp+8 */ Ti64 var _ /* pMerger at bp+16 */ uintptr var _ /* pRoot at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _ = i, iSeq, iTask, nDepth, nReader, pMain, pTask, rc, v3, v4 pMain = uintptr(0) rc = SQLITE_OK /* If the sorter uses more than one task, then create the top-level ** MergeEngine here. This MergeEngine will read data from exactly ** one PmaReader per sub-task. */ _ = libc.Int32FromInt32(0) if int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask) > int32(1) { pMain = _vdbeMergeEngineNew(tls, int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)) if pMain == uintptr(0) { rc = int32(SQLITE_NOMEM) } } iTask = 0 for { if !(rc == SQLITE_OK && iTask < int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)) { break } pTask = pSorter + 72 + uintptr(iTask)*80 _ = libc.Int32FromInt32(0) if libc.Bool(false) || (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA != 0 { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Root node of tree for this task */ nDepth = _vdbeSorterTreeDepth(tls, (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA) *(*Ti64)(unsafe.Pointer(bp + 8)) = 0 if (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA <= int32(SORTER_MAX_MERGE_COUNT) { rc = _vdbeMergeEngineLevel0(tls, pTask, (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA, bp+8, bp) } else { iSeq = 0 *(*uintptr)(unsafe.Pointer(bp)) = _vdbeMergeEngineNew(tls, int32(SORTER_MAX_MERGE_COUNT)) if *(*uintptr)(unsafe.Pointer(bp)) == uintptr(0) { rc = int32(SQLITE_NOMEM) } i = 0 for { if !(i < (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA && rc == SQLITE_OK) { break } *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) /* Number of level-0 PMAs to merge */ if (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA-i < int32(SORTER_MAX_MERGE_COUNT) { v3 = (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA - i } else { v3 = int32(SORTER_MAX_MERGE_COUNT) } nReader = v3 rc = _vdbeMergeEngineLevel0(tls, pTask, nReader, bp+8, bp+16) if rc == SQLITE_OK { v4 = iSeq iSeq++ rc = _vdbeSorterAddToTree(tls, pTask, nDepth, v4, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 16))) } goto _2 _2: ; i += int32(SORTER_MAX_MERGE_COUNT) } } if rc == SQLITE_OK { if pMain != uintptr(0) { rc = _vdbeIncrMergerNew(tls, pTask, *(*uintptr)(unsafe.Pointer(bp)), (*TMergeEngine)(unsafe.Pointer(pMain)).FaReadr+uintptr(iTask)*56+48) } else { _ = libc.Int32FromInt32(0) pMain = *(*uintptr)(unsafe.Pointer(bp)) } } else { _vdbeMergeEngineFree(tls, *(*uintptr)(unsafe.Pointer(bp))) } } goto _1 _1: ; iTask++ } if rc != SQLITE_OK { _vdbeMergeEngineFree(tls, pMain) pMain = uintptr(0) } *(*uintptr)(unsafe.Pointer(ppOut)) = pMain return rc } // C documentation // // /* // ** This function is called as part of an sqlite3VdbeSorterRewind() operation // ** on a sorter that has written two or more PMAs to temporary files. It sets // ** up either VdbeSorter.pMerger (for single threaded sorters) or pReader // ** (for multi-threaded sorters) so that it can be used to iterate through // ** all records stored in the sorter. // ** // ** SQLITE_OK is returned if successful, or an SQLite error code otherwise. // */ func _vdbeSorterSetupMerge(tls *libc.TLS, pSorter uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var db, p, pIncr, pLast, pReadr, pTask0, v3 uintptr var i, iTask, rc int32 var xCompare TSorterCompare var _ /* pMain at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _ = db, i, iTask, p, pIncr, pLast, pReadr, pTask0, rc, xCompare, v3 /* Return code */ pTask0 = pSorter + 72 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) db = (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask0)).FpSorter)).Fdb xCompare = _vdbeSorterGetCompare(tls, pSorter) i = 0 for { if !(i < int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)) { break } (*(*TSortSubtask)(unsafe.Pointer(pSorter + 72 + uintptr(i)*80))).FxCompare = xCompare goto _1 _1: ; i++ } rc = _vdbeSorterMergeTreeBuild(tls, pSorter, bp) if rc == SQLITE_OK { _ = libc.Int32FromInt32(0) if (*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUseThreads != 0 { pReadr = uintptr(0) pLast = pSorter + 72 + uintptr(int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)-int32(1))*80 rc = _vdbeSortAllocUnpacked(tls, pLast) if rc == SQLITE_OK { pReadr = _sqlite3DbMallocZero(tls, db, uint64(56)) (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpReader = pReadr if pReadr == uintptr(0) { rc = int32(SQLITE_NOMEM) } } if rc == SQLITE_OK { rc = _vdbeIncrMergerNew(tls, pLast, *(*uintptr)(unsafe.Pointer(bp)), pReadr+48) if rc == SQLITE_OK { _vdbeIncrMergerSetThreads(tls, (*TPmaReader)(unsafe.Pointer(pReadr)).FpIncr) iTask = 0 for { if !(iTask < int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)-int32(1)) { break } v3 = (*(*TPmaReader)(unsafe.Pointer((*TMergeEngine)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaReadr + uintptr(iTask)*56))).FpIncr pIncr = v3 if v3 != 0 { _vdbeIncrMergerSetThreads(tls, pIncr) _ = libc.Int32FromInt32(0) } goto _2 _2: ; iTask++ } iTask = 0 for { if !(rc == SQLITE_OK && iTask < int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)) { break } /* Check that: ** ** a) The incremental merge object is configured to use the ** right task, and ** b) If it is using task (nTask-1), it is configured to run ** in single-threaded mode. This is important, as the ** root merge (INCRINIT_ROOT) will be using the same task ** object. */ p = (*TMergeEngine)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaReadr + uintptr(iTask)*56 _ = libc.Int32FromInt32(0) rc = _vdbePmaReaderIncrInit(tls, p, int32(INCRINIT_TASK)) goto _4 _4: ; iTask++ } } *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } if rc == SQLITE_OK { rc = _vdbePmaReaderIncrMergeInit(tls, pReadr, int32(INCRINIT_ROOT)) } } else { rc = _vdbeMergeEngineInit(tls, pTask0, *(*uintptr)(unsafe.Pointer(bp)), INCRINIT_NORMAL) (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpMerger = *(*uintptr)(unsafe.Pointer(bp)) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } } if rc != SQLITE_OK { _vdbeMergeEngineFree(tls, *(*uintptr)(unsafe.Pointer(bp))) } return rc } // C documentation // // /* // ** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite, // ** this function is called to prepare for iterating through the records // ** in sorted order. // */ func _sqlite3VdbeSorterRewind(tls *libc.TLS, pCsr uintptr, pbEof uintptr) (r int32) { var pSorter uintptr var rc int32 _, _ = pSorter, rc rc = SQLITE_OK /* Return code */ _ = libc.Int32FromInt32(0) pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 36)) _ = libc.Int32FromInt32(0) /* If no data has been written to disk, then do not do so now. Instead, ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly ** from the in-memory list. */ if int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUsePMA) == 0 { if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList != 0 { *(*int32)(unsafe.Pointer(pbEof)) = 0 rc = _vdbeSorterSort(tls, pSorter+72, pSorter+40) } else { *(*int32)(unsafe.Pointer(pbEof)) = int32(1) } return rc } /* Write the current in-memory list to a PMA. When the VdbeSorterWrite() ** function flushes the contents of memory to disk, it immediately always ** creates a new list consisting of a single key immediately afterwards. ** So the list is never empty at this point. */ _ = libc.Int32FromInt32(0) rc = _vdbeSorterFlushPMA(tls, pSorter) /* Join all threads */ rc = _vdbeSorterJoinAll(tls, pSorter, rc) /* Assuming no errors have occurred, set up a merger structure to ** incrementally read and merge all remaining PMAs. */ _ = libc.Int32FromInt32(0) if rc == SQLITE_OK { rc = _vdbeSorterSetupMerge(tls, pSorter) *(*int32)(unsafe.Pointer(pbEof)) = 0 } return rc } // C documentation // // /* // ** Advance to the next element in the sorter. Return value: // ** // ** SQLITE_OK success // ** SQLITE_DONE end of data // ** otherwise some kind of error. // */ func _sqlite3VdbeSorterNext(tls *libc.TLS, db uintptr, pCsr uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pFree, pSorter uintptr var rc, v1 int32 var _ /* res at bp+0 */ int32 _, _, _, _ = pFree, pSorter, rc, v1 /* Return code */ _ = libc.Int32FromInt32(0) pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 36)) _ = libc.Int32FromInt32(0) if (*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUsePMA != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUseThreads != 0 { rc = _vdbePmaReaderNext(tls, (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpReader) if rc == SQLITE_OK && (*TPmaReader)(unsafe.Pointer((*TVdbeSorter)(unsafe.Pointer(pSorter)).FpReader)).FpFd == uintptr(0) { rc = int32(SQLITE_DONE) } } else { /*if( !pSorter->bUseThreads )*/ *(*int32)(unsafe.Pointer(bp)) = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) rc = _vdbeMergeEngineStep(tls, (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpMerger, bp) if rc == SQLITE_OK && *(*int32)(unsafe.Pointer(bp)) != 0 { rc = int32(SQLITE_DONE) } } } else { pFree = (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList = *(*uintptr)(unsafe.Pointer(pFree + 4)) *(*uintptr)(unsafe.Pointer(pFree + 4)) = uintptr(0) if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory == uintptr(0) { _vdbeSorterRecordFree(tls, db, pFree) } if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList != 0 { v1 = SQLITE_OK } else { v1 = int32(SQLITE_DONE) } rc = v1 } return rc } // C documentation // // /* // ** Return a pointer to a buffer owned by the sorter that contains the // ** current key. // */ func _vdbeSorterRowkey(tls *libc.TLS, pSorter uintptr, pnKey uintptr) (r uintptr) { var pKey, pReader uintptr _, _ = pKey, pReader if (*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUsePMA != 0 { if (*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUseThreads != 0 { pReader = (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpReader } else { /*if( !pSorter->bUseThreads )*/ pReader = (*TMergeEngine)(unsafe.Pointer((*TVdbeSorter)(unsafe.Pointer(pSorter)).FpMerger)).FaReadr + uintptr(*(*int32)(unsafe.Pointer((*TMergeEngine)(unsafe.Pointer((*TVdbeSorter)(unsafe.Pointer(pSorter)).FpMerger)).FaTree + 1*4)))*56 } *(*int32)(unsafe.Pointer(pnKey)) = (*TPmaReader)(unsafe.Pointer(pReader)).FnKey pKey = (*TPmaReader)(unsafe.Pointer(pReader)).FaKey } else { *(*int32)(unsafe.Pointer(pnKey)) = (*TSorterRecord)(unsafe.Pointer((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList)).FnVal pKey = (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList + libc.UintptrFromInt32(1)*8 } return pKey } // C documentation // // /* // ** Copy the current sorter key into the memory cell pOut. // */ func _sqlite3VdbeSorterRowkey(tls *libc.TLS, pCsr uintptr, pOut uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pKey, pSorter uintptr var _ /* nKey at bp+0 */ int32 _, _ = pKey, pSorter /* Sorter key to copy into pOut */ _ = libc.Int32FromInt32(0) pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 36)) pKey = _vdbeSorterRowkey(tls, pSorter, bp) if _sqlite3VdbeMemClearAndResize(tls, pOut, *(*int32)(unsafe.Pointer(bp))) != 0 { return int32(SQLITE_NOMEM) } (*TMem)(unsafe.Pointer(pOut)).Fn = *(*int32)(unsafe.Pointer(bp)) (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Blob)) libc.Xmemcpy(tls, (*TMem)(unsafe.Pointer(pOut)).Fz, pKey, uint32(*(*int32)(unsafe.Pointer(bp)))) return SQLITE_OK } // C documentation // // /* // ** Compare the key in memory cell pVal with the key that the sorter cursor // ** passed as the first argument currently points to. For the purposes of // ** the comparison, ignore the rowid field at the end of each record. // ** // ** If the sorter cursor key contains any NULL values, consider it to be // ** less than pVal. Even if pVal also contains NULL values. // ** // ** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM). // ** Otherwise, set *pRes to a negative, zero or positive value if the // ** key in pVal is smaller than, equal to or larger than the current sorter // ** key. // ** // ** This routine forms the core of the OP_SorterCompare opcode, which in // ** turn is used to verify uniqueness when constructing a UNIQUE INDEX. // */ func _sqlite3VdbeSorterCompare(tls *libc.TLS, pCsr uintptr, pVal uintptr, nKeyCol int32, pRes uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i int32 var pKey, pKeyInfo, pSorter, r2, v1 uintptr var _ /* nKey at bp+0 */ int32 _, _, _, _, _, _ = i, pKey, pKeyInfo, pSorter, r2, v1 /* Sorter key to compare pVal with */ _ = libc.Int32FromInt32(0) pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 36)) r2 = (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpUnpacked pKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pCsr)).FpKeyInfo if r2 == uintptr(0) { v1 = _sqlite3VdbeAllocUnpackedRecord(tls, pKeyInfo) (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpUnpacked = v1 r2 = v1 if r2 == uintptr(0) { return int32(SQLITE_NOMEM) } (*TUnpackedRecord)(unsafe.Pointer(r2)).FnField = uint16(nKeyCol) } _ = libc.Int32FromInt32(0) pKey = _vdbeSorterRowkey(tls, pSorter, bp) _sqlite3VdbeRecordUnpack(tls, pKeyInfo, *(*int32)(unsafe.Pointer(bp)), pKey, r2) i = 0 for { if !(i < nKeyCol) { break } if int32((*(*TMem)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(r2)).FaMem + uintptr(i)*40))).Fflags)&int32(MEM_Null) != 0 { *(*int32)(unsafe.Pointer(pRes)) = -int32(1) return SQLITE_OK } goto _2 _2: ; i++ } *(*int32)(unsafe.Pointer(pRes)) = _sqlite3VdbeRecordCompare(tls, (*TMem)(unsafe.Pointer(pVal)).Fn, (*TMem)(unsafe.Pointer(pVal)).Fz, r2) return SQLITE_OK } /************** End of vdbesort.c ********************************************/ /************** Begin file vdbevtab.c ****************************************/ /* ** 2020-03-23 ** ** 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 implements virtual-tables for examining the bytecode content ** of a prepared statement. */ /* #include "sqliteInt.h" */ /************** End of vdbevtab.c ********************************************/ /************** Begin file memjournal.c **************************************/ /* ** 2008 October 7 ** ** 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 contains code use to implement an in-memory rollback journal. ** The in-memory rollback journal is used to journal transactions for ** ":memory:" databases and when the journal_mode=MEMORY pragma is used. ** ** Update: The in-memory journal is also used to temporarily cache ** smaller journals that are not critical for power-loss recovery. ** For example, statement journals that are not too big will be held ** entirely in memory, thus reducing the number of file I/O calls, and ** more importantly, reducing temporary file creation events. If these ** journals become too large for memory, they are spilled to disk. But ** in the common case, they are usually small and no file I/O needs to ** occur. */ /* #include "sqliteInt.h" */ // C documentation // // /* Forward references to internal structures */ type TMemJournal = struct { F__ccgo_align [0]uint32 FpMethod uintptr FnChunkSize int32 FnSpill int32 FpFirst uintptr Fendpoint TFilePoint Freadpoint TFilePoint Fflags int32 FpVfs uintptr FzJournal uintptr F__ccgo_pad9 [4]byte } type MemJournal = TMemJournal type TFilePoint = struct { F__ccgo_align [0]uint32 FiOffset Tsqlite3_int64 FpChunk uintptr F__ccgo_pad2 [4]byte } type FilePoint = TFilePoint type TFileChunk = struct { FpNext uintptr FzChunk [8]Tu8 } type FileChunk = TFileChunk /* ** The rollback journal is composed of a linked list of these structures. ** ** The zChunk array is always at least 8 bytes in size - usually much more. ** Its actual size is stored in the MemJournal.nChunkSize variable. */ type TFileChunk1 = struct { FpNext uintptr FzChunk [8]Tu8 } type FileChunk1 = TFileChunk1 /* ** By default, allocate this many bytes of memory for each FileChunk object. */ /* ** For chunk size nChunkSize, return the number of bytes that should ** be allocated for each FileChunk structure. */ /* ** An instance of this object serves as a cursor into the rollback journal. ** The cursor can be either for reading or writing. */ type TFilePoint1 = struct { F__ccgo_align [0]uint32 FiOffset Tsqlite3_int64 FpChunk uintptr F__ccgo_pad2 [4]byte } type FilePoint1 = TFilePoint1 /* ** This structure is a subclass of sqlite3_file. Each open memory-journal ** is an instance of this class. */ type TMemJournal1 = struct { F__ccgo_align [0]uint32 FpMethod uintptr FnChunkSize int32 FnSpill int32 FpFirst uintptr Fendpoint TFilePoint Freadpoint TFilePoint Fflags int32 FpVfs uintptr FzJournal uintptr F__ccgo_pad9 [4]byte } type MemJournal1 = TMemJournal1 // C documentation // // /* // ** Read data from the in-memory journal file. This is the implementation // ** of the sqlite3_vfs.xRead method. // */ func _memjrnlRead(tls *libc.TLS, pJfd uintptr, zBuf uintptr, iAmt int32, iOfst Tsqlite_int64) (r int32) { var iChunkOffset, iSpace, nCopy, nRead, v5 int32 var iOff Tsqlite3_int64 var p, pChunk, zOut, v2 uintptr var v3 bool var v6 int64 _, _, _, _, _, _, _, _, _, _, _, _ = iChunkOffset, iOff, iSpace, nCopy, nRead, p, pChunk, zOut, v2, v3, v5, v6 p = pJfd zOut = zBuf nRead = iAmt if int64(iAmt)+iOfst > (*TMemJournal)(unsafe.Pointer(p)).Fendpoint.FiOffset { return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(2)<= 0; v3 { v2 = (*TFileChunk)(unsafe.Pointer(pChunk)).FpNext pChunk = v2 } if !(v3 && v2 != uintptr(0) && nRead > 0) { break } } if pChunk != 0 { v6 = iOfst + int64(iAmt) } else { v6 = 0 } (*TMemJournal)(unsafe.Pointer(p)).Freadpoint.FiOffset = v6 (*TMemJournal)(unsafe.Pointer(p)).Freadpoint.FpChunk = pChunk return SQLITE_OK } // C documentation // // /* // ** Free the list of FileChunk structures headed at MemJournal.pFirst. // */ func _memjrnlFreeChunks(tls *libc.TLS, pFirst uintptr) { var pIter, pNext uintptr _, _ = pIter, pNext pIter = pFirst for { if !(pIter != 0) { break } pNext = (*TFileChunk)(unsafe.Pointer(pIter)).FpNext Xsqlite3_free(tls, pIter) goto _1 _1: ; pIter = pNext } } // C documentation // // /* // ** Flush the contents of memory to a real file on disk. // */ func _memjrnlCreateFile(tls *libc.TLS, p uintptr) (r int32) { var copy1 TMemJournal var iOff Ti64 var nChunk, rc int32 var pIter, pReal uintptr _, _, _, _, _, _ = copy1, iOff, nChunk, pIter, pReal, rc pReal = p copy1 = *(*TMemJournal)(unsafe.Pointer(p)) libc.Xmemset(tls, p, 0, uint32(64)) rc = _sqlite3OsOpen(tls, copy1.FpVfs, copy1.FzJournal, pReal, copy1.Fflags, uintptr(0)) if rc == SQLITE_OK { nChunk = copy1.FnChunkSize iOff = 0 pIter = copy1.FpFirst for { if !(pIter != 0) { break } if iOff+int64(nChunk) > copy1.Fendpoint.FiOffset { nChunk = int32(copy1.Fendpoint.FiOffset - iOff) } rc = _sqlite3OsWrite(tls, pReal, pIter+4, nChunk, iOff) if rc != 0 { break } iOff += int64(nChunk) goto _1 _1: ; pIter = (*TFileChunk)(unsafe.Pointer(pIter)).FpNext } if rc == SQLITE_OK { /* No error has occurred. Free the in-memory buffers. */ _memjrnlFreeChunks(tls, copy1.FpFirst) } } if rc != SQLITE_OK { /* If an error occurred while creating or writing to the file, restore ** the original before returning. This way, SQLite uses the in-memory ** journal data to roll back changes made to the internal page-cache ** before this function was called. */ _sqlite3OsClose(tls, pReal) *(*TMemJournal)(unsafe.Pointer(p)) = copy1 } return rc } // C documentation // // /* // ** Write data to the file. // */ func _memjrnlWrite(tls *libc.TLS, pJfd uintptr, zBuf uintptr, iAmt int32, iOfst Tsqlite_int64) (r int32) { var iChunkOffset, iSpace, nWrite, rc, v1 int32 var p, pChunk, pNew, zWrite, v2 uintptr _, _, _, _, _, _, _, _, _, _ = iChunkOffset, iSpace, nWrite, p, pChunk, pNew, rc, zWrite, v1, v2 p = pJfd nWrite = iAmt zWrite = zBuf /* If the file should be created now, create it and write the new data ** into the file on disk. */ if (*TMemJournal)(unsafe.Pointer(p)).FnSpill > 0 && int64(iAmt)+iOfst > int64((*TMemJournal)(unsafe.Pointer(p)).FnSpill) { rc = _memjrnlCreateFile(tls, p) if rc == SQLITE_OK { rc = _sqlite3OsWrite(tls, pJfd, zBuf, iAmt, iOfst) } return rc } else { /* An in-memory journal file should only ever be appended to. Random ** access writes are not required. The only exception to this is when ** the in-memory journal is being used by a connection using the ** atomic-write optimization. In this case the first 28 bytes of the ** journal file may be written as part of committing the transaction. */ _ = libc.Int32FromInt32(0) if iOfst > 0 && iOfst != (*TMemJournal)(unsafe.Pointer(p)).Fendpoint.FiOffset { _memjrnlTruncate(tls, pJfd, iOfst) } if iOfst == 0 && (*TMemJournal)(unsafe.Pointer(p)).FpFirst != 0 { _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TMemJournal)(unsafe.Pointer(p)).FpFirst+4, zBuf, uint32(iAmt)) } else { for nWrite > 0 { pChunk = (*TMemJournal)(unsafe.Pointer(p)).Fendpoint.FpChunk iChunkOffset = int32((*TMemJournal)(unsafe.Pointer(p)).Fendpoint.FiOffset % int64((*TMemJournal)(unsafe.Pointer(p)).FnChunkSize)) if nWrite < (*TMemJournal)(unsafe.Pointer(p)).FnChunkSize-iChunkOffset { v1 = nWrite } else { v1 = (*TMemJournal)(unsafe.Pointer(p)).FnChunkSize - iChunkOffset } iSpace = v1 _ = libc.Int32FromInt32(0) if iChunkOffset == 0 { /* New chunk is required to extend the file. */ pNew = Xsqlite3_malloc(tls, int32(libc.Uint32FromInt64(12)+uint32((*TMemJournal)(unsafe.Pointer(p)).FnChunkSize-libc.Int32FromInt32(8)))) if !(pNew != 0) { return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)< 0 { (*TMemJournal)(unsafe.Pointer(p)).FnChunkSize = nSpill } else { (*TMemJournal)(unsafe.Pointer(p)).FnChunkSize = int32(uint32(libc.Int32FromInt32(8)+libc.Int32FromInt32(MEMJOURNAL_DFLT_FILECHUNKSIZE)) - libc.Uint32FromInt64(12)) _ = libc.Int32FromInt32(0) } (*Tsqlite3_file)(unsafe.Pointer(pJfd)).FpMethods = uintptr(unsafe.Pointer(&_MemJournalMethods)) (*TMemJournal)(unsafe.Pointer(p)).FnSpill = nSpill (*TMemJournal)(unsafe.Pointer(p)).Fflags = flags (*TMemJournal)(unsafe.Pointer(p)).FzJournal = zName (*TMemJournal)(unsafe.Pointer(p)).FpVfs = pVfs return SQLITE_OK } // C documentation // // /* // ** Open an in-memory journal file. // */ func _sqlite3MemJournalOpen(tls *libc.TLS, pJfd uintptr) { _sqlite3JournalOpen(tls, uintptr(0), uintptr(0), pJfd, 0, -int32(1)) } // C documentation // // /* // ** The file-handle passed as the only argument is open on a journal file. // ** Return true if this "journal file" is currently stored in heap memory, // ** or false otherwise. // */ func _sqlite3JournalIsInMemory(tls *libc.TLS, p uintptr) (r int32) { return libc.BoolInt32((*Tsqlite3_file)(unsafe.Pointer(p)).FpMethods == uintptr(unsafe.Pointer(&_MemJournalMethods))) } // C documentation // // /* // ** Return the number of bytes required to store a JournalFile that uses vfs // ** pVfs to create the underlying on-disk files. // */ func _sqlite3JournalSize(tls *libc.TLS, pVfs uintptr) (r int32) { var v1 int32 _ = v1 if (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FszOsFile > libc.Int32FromInt64(64) { v1 = (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FszOsFile } else { v1 = libc.Int32FromInt64(64) } return v1 } /************** End of memjournal.c ******************************************/ /************** Begin file walker.c ******************************************/ /* ** 2008 August 16 ** ** 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 contains routines used for walking the parser tree for ** an SQL statement. */ /* #include "sqliteInt.h" */ /* #include */ /* #include */ // C documentation // // /* // ** Walk all expressions linked into the list of Window objects passed // ** as the second argument. // */ func _walkWindowList(tls *libc.TLS, pWalker uintptr, pList uintptr, bOneOnly int32) (r int32) { var pWin uintptr var rc int32 _, _ = pWin, rc pWin = pList for { if !(pWin != 0) { break } rc = _sqlite3WalkExprList(tls, pWalker, (*TWindow)(unsafe.Pointer(pWin)).FpOrderBy) if rc != 0 { return int32(WRC_Abort) } rc = _sqlite3WalkExprList(tls, pWalker, (*TWindow)(unsafe.Pointer(pWin)).FpPartition) if rc != 0 { return int32(WRC_Abort) } rc = _sqlite3WalkExpr(tls, pWalker, (*TWindow)(unsafe.Pointer(pWin)).FpFilter) if rc != 0 { return int32(WRC_Abort) } rc = _sqlite3WalkExpr(tls, pWalker, (*TWindow)(unsafe.Pointer(pWin)).FpStart) if rc != 0 { return int32(WRC_Abort) } rc = _sqlite3WalkExpr(tls, pWalker, (*TWindow)(unsafe.Pointer(pWin)).FpEnd) if rc != 0 { return int32(WRC_Abort) } if bOneOnly != 0 { break } goto _1 _1: ; pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin } return WRC_Continue } // C documentation // // /* // ** Walk an expression tree. Invoke the callback once for each node // ** of the expression, while descending. (In other words, the callback // ** is invoked before visiting children.) // ** // ** The return value from the callback should be one of the WRC_* // ** constants to specify how to proceed with the walk. // ** // ** WRC_Continue Continue descending down the tree. // ** // ** WRC_Prune Do not descend into child nodes, but allow // ** the walk to continue with sibling nodes. // ** // ** WRC_Abort Do no more callbacks. Unwind the stack and // ** return from the top-level walk call. // ** // ** The return value from this routine is WRC_Abort to abandon the tree walk // ** and WRC_Continue to continue. // */ func _sqlite3WalkExprNN(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var rc int32 _ = rc for int32(1) != 0 { rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TWalker)(unsafe.Pointer(pWalker)).FxExprCallback})))(tls, pWalker, pExpr) if rc != 0 { return rc & int32(WRC_Abort) } if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_TokenOnly)|libc.Int32FromInt32(EP_Leaf)) != libc.Uint32FromInt32(0)) { _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 && _sqlite3WalkExprNN(tls, pWalker, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) != 0 { return int32(WRC_Abort) } if (*TExpr)(unsafe.Pointer(pExpr)).FpRight != 0 { _ = libc.Int32FromInt32(0) pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpRight continue } else { if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) { _ = libc.Int32FromInt32(0) if _sqlite3WalkSelect(tls, pWalker, *(*uintptr)(unsafe.Pointer(pExpr + 20))) != 0 { return int32(WRC_Abort) } } else { if *(*uintptr)(unsafe.Pointer(pExpr + 20)) != 0 { if _sqlite3WalkExprList(tls, pWalker, *(*uintptr)(unsafe.Pointer(pExpr + 20))) != 0 { return int32(WRC_Abort) } } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { if _walkWindowList(tls, pWalker, *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)), int32(1)) != 0 { return int32(WRC_Abort) } } } } } break } return WRC_Continue } func _sqlite3WalkExpr(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var v1 int32 _ = v1 if pExpr != 0 { v1 = _sqlite3WalkExprNN(tls, pWalker, pExpr) } else { v1 = WRC_Continue } return v1 } // C documentation // // /* // ** Call sqlite3WalkExpr() for every expression in list p or until // ** an abort request is seen. // */ func _sqlite3WalkExprList(tls *libc.TLS, pWalker uintptr, p uintptr) (r int32) { var i int32 var pItem uintptr _, _ = i, pItem if p != 0 { i = (*TExprList)(unsafe.Pointer(p)).FnExpr pItem = p + 8 for { if !(i > 0) { break } if _sqlite3WalkExpr(tls, pWalker, (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr) != 0 { return int32(WRC_Abort) } goto _1 _1: ; i-- pItem += 20 } } return WRC_Continue } // C documentation // // /* // ** This is a no-op callback for Walker->xSelectCallback2. If this // ** callback is set, then the Select->pWinDefn list is traversed. // */ func _sqlite3WalkWinDefnDummyCallback(tls *libc.TLS, pWalker uintptr, p uintptr) { _ = pWalker _ = p /* No-op */ } // C documentation // // /* // ** Walk all expressions associated with SELECT statement p. Do // ** not invoke the SELECT callback on p, but do (of course) invoke // ** any expr callbacks and SELECT callbacks that come from subqueries. // ** Return WRC_Abort or WRC_Continue. // */ func _sqlite3WalkSelectExpr(tls *libc.TLS, pWalker uintptr, p uintptr) (r int32) { var pParse, v1 uintptr var rc int32 var v2 bool _, _, _, _ = pParse, rc, v1, v2 if _sqlite3WalkExprList(tls, pWalker, (*TSelect)(unsafe.Pointer(p)).FpEList) != 0 { return int32(WRC_Abort) } if _sqlite3WalkExpr(tls, pWalker, (*TSelect)(unsafe.Pointer(p)).FpWhere) != 0 { return int32(WRC_Abort) } if _sqlite3WalkExprList(tls, pWalker, (*TSelect)(unsafe.Pointer(p)).FpGroupBy) != 0 { return int32(WRC_Abort) } if _sqlite3WalkExpr(tls, pWalker, (*TSelect)(unsafe.Pointer(p)).FpHaving) != 0 { return int32(WRC_Abort) } if _sqlite3WalkExprList(tls, pWalker, (*TSelect)(unsafe.Pointer(p)).FpOrderBy) != 0 { return int32(WRC_Abort) } if _sqlite3WalkExpr(tls, pWalker, (*TSelect)(unsafe.Pointer(p)).FpLimit) != 0 { return int32(WRC_Abort) } if (*TSelect)(unsafe.Pointer(p)).FpWinDefn != 0 { if v2 = (*TWalker)(unsafe.Pointer(pWalker)).FxSelectCallback2 == __ccgo_fp(_sqlite3WalkWinDefnDummyCallback); !v2 { v1 = (*TWalker)(unsafe.Pointer(pWalker)).FpParse pParse = v1 } if v2 || v1 != uintptr(0) && int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) || (*TWalker)(unsafe.Pointer(pWalker)).FxSelectCallback2 == __ccgo_fp(_sqlite3SelectPopWith) { /* The following may return WRC_Abort if there are unresolvable ** symbols (e.g. a table that does not exist) in a window definition. */ rc = _walkWindowList(tls, pWalker, (*TSelect)(unsafe.Pointer(p)).FpWinDefn, 0) return rc } } return WRC_Continue } // C documentation // // /* // ** Walk the parse trees associated with all subqueries in the // ** FROM clause of SELECT statement p. Do not invoke the select // ** callback on p, but do invoke it on each FROM clause subquery // ** and on any subqueries further down in the tree. Return // ** WRC_Abort or WRC_Continue; // */ func _sqlite3WalkSelectFrom(tls *libc.TLS, pWalker uintptr, p uintptr) (r int32) { var i int32 var pItem, pSrc uintptr _, _, _ = i, pItem, pSrc pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc if pSrc != 0 { i = (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc pItem = pSrc + 8 for { if !(i > 0) { break } if (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect != 0 && _sqlite3WalkSelect(tls, pWalker, (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect) != 0 { return int32(WRC_Abort) } if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x4>>2)) != 0 && _sqlite3WalkExprList(tls, pWalker, *(*uintptr)(unsafe.Pointer(pItem + 64))) != 0 { return int32(WRC_Abort) } goto _1 _1: ; i-- pItem += 72 } } return WRC_Continue } // C documentation // // /* // ** Call sqlite3WalkExpr() for every expression in Select statement p. // ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and // ** on the compound select chain, p->pPrior. // ** // ** If it is not NULL, the xSelectCallback() callback is invoked before // ** the walk of the expressions and FROM clause. The xSelectCallback2() // ** method is invoked following the walk of the expressions and FROM clause, // ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL // ** and if the expressions and FROM clause both return WRC_Continue; // ** // ** Return WRC_Continue under normal conditions. Return WRC_Abort if // ** there is an abort request. // ** // ** If the Walker does not have an xSelectCallback() then this routine // ** is a no-op returning WRC_Continue. // */ func _sqlite3WalkSelect(tls *libc.TLS, pWalker uintptr, p uintptr) (r int32) { var rc int32 _ = rc if p == uintptr(0) { return WRC_Continue } if (*TWalker)(unsafe.Pointer(pWalker)).FxSelectCallback == uintptr(0) { return WRC_Continue } for cond := true; cond; cond = p != uintptr(0) { rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TWalker)(unsafe.Pointer(pWalker)).FxSelectCallback})))(tls, pWalker, p) if rc != 0 { return rc & int32(WRC_Abort) } if _sqlite3WalkSelectExpr(tls, pWalker, p) != 0 || _sqlite3WalkSelectFrom(tls, pWalker, p) != 0 { return int32(WRC_Abort) } if (*TWalker)(unsafe.Pointer(pWalker)).FxSelectCallback2 != 0 { (*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TWalker)(unsafe.Pointer(pWalker)).FxSelectCallback2})))(tls, pWalker, p) } p = (*TSelect)(unsafe.Pointer(p)).FpPrior } return WRC_Continue } // C documentation // // /* Increase the walkerDepth when entering a subquery, and // ** decrease when leaving the subquery. // */ func _sqlite3WalkerDepthIncrease(tls *libc.TLS, pWalker uintptr, pSelect uintptr) (r int32) { _ = pSelect (*TWalker)(unsafe.Pointer(pWalker)).FwalkerDepth++ return WRC_Continue } func _sqlite3WalkerDepthDecrease(tls *libc.TLS, pWalker uintptr, pSelect uintptr) { _ = pSelect (*TWalker)(unsafe.Pointer(pWalker)).FwalkerDepth-- } // C documentation // // /* // ** No-op routine for the parse-tree walker. // ** // ** When this routine is the Walker.xExprCallback then expression trees // ** are walked without any actions being taken at each node. Presumably, // ** when this routine is used for Walker.xExprCallback then // ** Walker.xSelectCallback is set to do something useful for every // ** subquery in the parser tree. // */ func _sqlite3ExprWalkNoop(tls *libc.TLS, NotUsed uintptr, NotUsed2 uintptr) (r int32) { _ = NotUsed _ = NotUsed2 return WRC_Continue } // C documentation // // /* // ** No-op routine for the parse-tree walker for SELECT statements. // ** subquery in the parser tree. // */ func _sqlite3SelectWalkNoop(tls *libc.TLS, NotUsed uintptr, NotUsed2 uintptr) (r int32) { _ = NotUsed _ = NotUsed2 return WRC_Continue } /************** End of walker.c **********************************************/ /************** Begin file resolve.c *****************************************/ /* ** 2008 August 18 ** ** 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 contains routines used for walking the parser tree and ** resolve all identifiers by associating them with a particular ** table and column. */ /* #include "sqliteInt.h" */ /* ** Magic table number to mean the EXCLUDED table in an UPSERT statement. */ // C documentation // // /* // ** Walk the expression tree pExpr and increase the aggregate function // ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node. // ** This needs to occur when copying a TK_AGG_FUNCTION node from an // ** outer query into an inner subquery. // ** // ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) // ** is a helper function - a callback for the tree walker. // ** // ** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c // */ func _incrAggDepth(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var p1 uintptr _ = p1 if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_FUNCTION) { p1 = pExpr + 2 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) + *(*int32)(unsafe.Pointer(pWalker + 24))) } return WRC_Continue } func _incrAggFunctionDepth(tls *libc.TLS, pExpr uintptr, N int32) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* w at bp+0 */ TWalker if N > 0 { libc.Xmemset(tls, bp, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_incrAggDepth) *(*int32)(unsafe.Pointer(bp + 24)) = N _sqlite3WalkExpr(tls, bp, pExpr) } } // C documentation // // /* // ** Turn the pExpr expression into an alias for the iCol-th column of the // ** result set in pEList. // ** // ** If the reference is followed by a COLLATE operator, then make sure // ** the COLLATE operator is preserved. For example: // ** // ** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase; // ** // ** Should be transformed into: // ** // ** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase; // ** // ** The nSubquery parameter specifies how many levels of subquery the // ** alias is removed from the original expression. The usual value is // ** zero but it might be more if the alias is contained within a subquery // ** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION // ** structures must be increased by the nSubquery amount. // */ func _resolveAlias(tls *libc.TLS, pParse uintptr, pEList uintptr, iCol int32, pExpr uintptr, nSubquery int32) { bp := tls.Alloc(64) defer tls.Free(64) var db, pDup, pOrig uintptr var _ /* temp at bp+0 */ TExpr _, _, _ = db, pDup, pOrig /* The database connection */ _ = libc.Int32FromInt32(0) pOrig = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(iCol)*20))).FpExpr _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo != 0 { return } db = (*TParse)(unsafe.Pointer(pParse)).Fdb pDup = _sqlite3ExprDup(tls, db, pOrig, 0) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _sqlite3ExprDelete(tls, db, pDup) pDup = uintptr(0) } else { _incrAggFunctionDepth(tls, pDup, nSubquery) if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLLATE) { _ = libc.Int32FromInt32(0) pDup = _sqlite3ExprAddCollateString(tls, pParse, pDup, *(*uintptr)(unsafe.Pointer(pExpr + 8))) } libc.Xmemcpy(tls, bp, pDup, uint32(52)) libc.Xmemcpy(tls, pDup, pExpr, uint32(52)) libc.Xmemcpy(tls, pExpr, bp, uint32(52)) if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { if *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)) != uintptr(0) { (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)))).FpOwner = pExpr } } _sqlite3ExprDeferredDelete(tls, pParse, pDup) } } // C documentation // // /* // ** Subqueries store the original database, table and column names for their // ** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN", // ** and mark the expression-list item by setting ExprList.a[].fg.eEName // ** to ENAME_TAB. // ** // ** Check to see if the zSpan/eEName of the expression-list item passed to this // ** routine matches the zDb, zTab, and zCol. If any of zDb, zTab, and zCol are // ** NULL then those fields will match anything. Return true if there is a match, // ** or false otherwise. // ** // ** SF_NestedFrom subqueries also store an entry for the implicit rowid (or // ** _rowid_, or oid) column by setting ExprList.a[].fg.eEName to ENAME_ROWID, // ** and setting zSpan to "DATABASE.TABLE.". This type of pItem // ** argument matches if zCol is a rowid alias. If it is not NULL, (*pbRowid) // ** is set to 1 if there is this kind of match. // */ func _sqlite3MatchEName(tls *libc.TLS, pItem uintptr, zCol uintptr, zTab uintptr, zDb uintptr, pbRowid uintptr) (r int32) { var eEName, n int32 var zSpan uintptr _, _, _ = eEName, n, zSpan eEName = int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 8 + 4)) & 0x3 >> 0)) if eEName != int32(ENAME_TAB) && (eEName != int32(ENAME_ROWID) || pbRowid == uintptr(0)) { return 0 } _ = libc.Int32FromInt32(0) zSpan = (*TExprList_item)(unsafe.Pointer(pItem)).FzEName n = 0 for { if !(*(*uint8)(unsafe.Pointer(zSpan + uintptr(n))) != 0 && int32(*(*uint8)(unsafe.Pointer(zSpan + uintptr(n)))) != int32('.')) { break } goto _1 _1: ; n++ } if zDb != 0 && (Xsqlite3_strnicmp(tls, zSpan, zDb, n) != 0 || int32(*(*uint8)(unsafe.Pointer(zDb + uintptr(n)))) != 0) { return 0 } zSpan += uintptr(n + int32(1)) n = 0 for { if !(*(*uint8)(unsafe.Pointer(zSpan + uintptr(n))) != 0 && int32(*(*uint8)(unsafe.Pointer(zSpan + uintptr(n)))) != int32('.')) { break } goto _2 _2: ; n++ } if zTab != 0 && (Xsqlite3_strnicmp(tls, zSpan, zTab, n) != 0 || int32(*(*uint8)(unsafe.Pointer(zTab + uintptr(n)))) != 0) { return 0 } zSpan += uintptr(n + int32(1)) if zCol != 0 { if eEName == int32(ENAME_TAB) && _sqlite3StrICmp(tls, zSpan, zCol) != 0 { return 0 } if eEName == int32(ENAME_ROWID) && _sqlite3IsRowid(tls, zCol) == 0 { return 0 } } if eEName == int32(ENAME_ROWID) { *(*int32)(unsafe.Pointer(pbRowid)) = int32(1) } return int32(1) } // C documentation // // /* // ** Return TRUE if the double-quoted string mis-feature should be supported. // */ func _areDoubleQuotedStringsEnabled(tls *libc.TLS, db uintptr, pTopNC uintptr) (r int32) { if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 { return int32(1) } /* Always support for legacy schemas */ if (*TNameContext)(unsafe.Pointer(pTopNC)).FncFlags&int32(NC_IsDDL) != 0 { /* Currently parsing a DDL statement */ if _sqlite3WritableSchema(tls, db) != 0 && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_DqsDML) != uint64(0) { return int32(1) } return libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_DqsDDL) != uint64(0)) } else { /* Currently parsing a DML statement */ return libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_DqsDML) != uint64(0)) } return r } // C documentation // // /* // ** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN. // ** return the appropriate colUsed mask. // */ func _sqlite3ExprColUsed(tls *libc.TLS, pExpr uintptr) (r TBitmask) { var n int32 var pExTab uintptr var v1 uint64 _, _, _ = n, pExTab, v1 n = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) _ = libc.Int32FromInt32(0) pExTab = (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TTable)(unsafe.Pointer(pExTab)).FtabFlags&uint32(TF_HasGenerated) != uint32(0) && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pExTab)).FaCol + uintptr(n)*12))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { if int32((*TTable)(unsafe.Pointer(pExTab)).FnCol) >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) { v1 = uint64(-libc.Int32FromInt32(1)) } else { v1 = libc.Uint64FromInt32(1)<<(*TTable)(unsafe.Pointer(pExTab)).FnCol - uint64(1) } return v1 } else { if n >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) { n = int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1) } return libc.Uint64FromInt32(1) << n } return r } // C documentation // // /* // ** Create a new expression term for the column specified by pMatch and // ** iColumn. Append this new expression term to the FULL JOIN Match set // ** in *ppList. Create a new *ppList if this is the first term in the // ** set. // */ func _extendFJMatch(tls *libc.TLS, pParse uintptr, ppList uintptr, pMatch uintptr, iColumn Ti16) { var pNew uintptr _ = pNew pNew = _sqlite3ExprAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_COLUMN), uintptr(0), 0) if pNew != 0 { (*TExpr)(unsafe.Pointer(pNew)).FiTable = (*TSrcItem)(unsafe.Pointer(pMatch)).FiCursor (*TExpr)(unsafe.Pointer(pNew)).FiColumn = iColumn (*TExpr)(unsafe.Pointer(pNew)).Fy.FpTab = (*TSrcItem)(unsafe.Pointer(pMatch)).FpTab _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(pNew + 4)) |= uint32(libc.Int32FromInt32(EP_CanBeNull)) *(*uintptr)(unsafe.Pointer(ppList)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(ppList)), pNew) } } // C documentation // // /* // ** Return TRUE (non-zero) if zTab is a valid name for the schema table pTab. // */ func _isValidSchemaTableName(tls *libc.TLS, zTab uintptr, pTab uintptr, pSchema uintptr) (r int32) { var zLegacy uintptr _ = zLegacy _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if Xsqlite3_strnicmp(tls, zTab, __ccgo_ts+6525, int32(7)) != 0 { return 0 } zLegacy = (*TTable)(unsafe.Pointer(pTab)).FzName if libc.Xstrcmp(tls, zLegacy+uintptr(7), __ccgo_ts+6533+7) == 0 { if _sqlite3StrICmp(tls, zTab+uintptr(7), __ccgo_ts+6552+7) == 0 { return int32(1) } if pSchema == uintptr(0) { return 0 } if _sqlite3StrICmp(tls, zTab+uintptr(7), __ccgo_ts+6066+7) == 0 { return int32(1) } if _sqlite3StrICmp(tls, zTab+uintptr(7), __ccgo_ts+6571+7) == 0 { return int32(1) } } else { if _sqlite3StrICmp(tls, zTab+uintptr(7), __ccgo_ts+6571+7) == 0 { return int32(1) } } return 0 } // C documentation // // /* // ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up // ** that name in the set of source tables in pSrcList and make the pExpr // ** expression node refer back to that source column. The following changes // ** are made to pExpr: // ** // ** pExpr->iDb Set the index in db->aDb[] of the database X // ** (even if X is implied). // ** pExpr->iTable Set to the cursor number for the table obtained // ** from pSrcList. // ** pExpr->y.pTab Points to the Table structure of X.Y (even if // ** X and/or Y are implied.) // ** pExpr->iColumn Set to the column number within the table. // ** pExpr->op Set to TK_COLUMN. // ** pExpr->pLeft Any expression this points to is deleted // ** pExpr->pRight Any expression this points to is deleted. // ** // ** The zDb variable is the name of the database (the "X"). This value may be // ** NULL meaning that name is of the form Y.Z or Z. Any available database // ** can be used. The zTable variable is the name of the table (the "Y"). This // ** value can be NULL if zDb is also NULL. If zTable is NULL it // ** means that the form of the name is Z and that columns from any table // ** can be used. // ** // ** If the name cannot be resolved unambiguously, leave an error message // ** in pParse and return WRC_Abort. Return WRC_Prune on success. // */ func _lookupName(tls *libc.TLS, pParse uintptr, zDb uintptr, zTab uintptr, zCol uintptr, pNC uintptr, pExpr uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var cnt, cntTab, eNewExprOp, hit, i, iCol, j, nSubquery, op, v5 int32 var db, pCol, pEList, pItem, pMatch, pOrig, pSchema, pSrcList, pTab, pTopNC, pUpsert, zAs, zErr, v10 uintptr var hCol, hCol1 Tu8 var v7, v8 uint32 var _ /* bRowid at bp+4 */ int32 var _ /* pFJMatch at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = cnt, cntTab, db, eNewExprOp, hCol, hCol1, hit, i, iCol, j, nSubquery, op, pCol, pEList, pItem, pMatch, pOrig, pSchema, pSrcList, pTab, pTopNC, pUpsert, zAs, zErr, v10, v5, v7, v8 /* Loop counters */ cnt = 0 /* Number of matching column names */ cntTab = 0 /* Number of potential "rowid" matches */ nSubquery = 0 /* How many levels of subquery */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Use for looping over pSrcList items */ pMatch = uintptr(0) /* The matching pSrcList item */ pTopNC = pNC /* First namecontext in the list */ pSchema = uintptr(0) /* Schema of the expression */ eNewExprOp = int32(TK_COLUMN) /* New value for pExpr->op on success */ pTab = uintptr(0) /* A column of pTab */ *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Matches for FULL JOIN .. USING */ _ = libc.Int32FromInt32(0) /* the name context cannot be NULL. */ _ = libc.Int32FromInt32(0) /* The Z in X.Y.Z cannot be NULL */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Initialize the node to no-match */ (*TExpr)(unsafe.Pointer(pExpr)).FiTable = -int32(1) /* Translate the schema name in zDb into a pointer to the corresponding ** schema. If not found, pSchema will remain NULL and nothing will match ** resulting in an appropriate error message toward the end of this routine */ if zDb != 0 { if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&(libc.Int32FromInt32(NC_PartIdx)|libc.Int32FromInt32(NC_IsCheck)) != 0 { /* Silently ignore database qualifiers inside CHECK constraints and ** partial indices. Do not raise errors because that might break ** legacy and because it does not hurt anything to just ignore the ** database name. */ zDb = uintptr(0) } else { i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } _ = libc.Int32FromInt32(0) if _sqlite3StrICmp(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FzDbSName, zDb) == 0 { pSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema break } goto _1 _1: ; i++ } if i == (*Tsqlite3)(unsafe.Pointer(db)).FnDb && _sqlite3StrICmp(tls, __ccgo_ts+6585, zDb) == 0 { /* This branch is taken when the main database has been renamed ** using SQLITE_DBCONFIG_MAINDBNAME. */ pSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FzDbSName } } } /* Start at the inner-most context and move outward until a match is found */ _ = libc.Int32FromInt32(0) for cond := true; cond; cond = pNC != 0 { pSrcList = (*TNameContext)(unsafe.Pointer(pNC)).FpSrcList if pSrcList != 0 { i = 0 pItem = pSrcList + 8 for { if !(i < (*TSrcList)(unsafe.Pointer(pSrcList)).FnSrc) { break } pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x2000>>13)) != 0 { /* In this case, pItem is a subquery that has been formed from a ** parenthesized subset of the FROM clause terms. Example: ** .... FROM t1 LEFT JOIN (t2 RIGHT JOIN t3 USING(x)) USING(y) ... ** \_________________________/ ** This pItem -------------^ */ hit = 0 _ = libc.Int32FromInt32(0) pEList = (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpSelect)).FpEList _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) j = 0 for { if !(j < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } *(*int32)(unsafe.Pointer(bp + 4)) = 0 /* True if possible rowid match */ if !(_sqlite3MatchEName(tls, pEList+8+uintptr(j)*20, zCol, zTab, zDb, bp+4) != 0) { goto _3 } if *(*int32)(unsafe.Pointer(bp + 4)) == 0 { if cnt > 0 { if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x400>>10)) == 0 || _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pItem + 48)), zCol) < 0 { /* Two or more tables have the same column name which is ** not joined by USING. This is an error. Signal as much ** by clearing pFJMatch and letting cnt go above 1. */ _sqlite3ExprListDelete(tls, db, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } else { if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_RIGHT) == 0 { /* An INNER or LEFT JOIN. Use the left-most table */ goto _3 } else { if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_LEFT) == 0 { /* A RIGHT JOIN. Use the right-most table */ cnt = 0 _sqlite3ExprListDelete(tls, db, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } else { /* For a FULL JOIN, we must construct a coalesce() func */ _extendFJMatch(tls, pParse, bp, pMatch, (*TExpr)(unsafe.Pointer(pExpr)).FiColumn) } } } } cnt++ hit = int32(1) } else { if cnt > 0 { /* This is a potential rowid match, but there has already been ** a real match found. So this can be ignored. */ goto _3 } } cntTab++ pMatch = pItem (*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(j) libc.SetBitFieldPtr16Uint32(pEList+8+uintptr(j)*20+8+4, libc.Uint32FromInt32(1), 6, 0x40) /* rowid cannot be part of a USING clause - assert() this. */ _ = libc.Int32FromInt32(0) if int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(j)*20 + 8 + 4))&0x80>>7)) != 0 { break } goto _3 _3: ; j++ } if hit != 0 || zTab == uintptr(0) { goto _2 } } _ = libc.Int32FromInt32(0) if zTab != 0 { if zDb != 0 { if (*TTable)(unsafe.Pointer(pTab)).FpSchema != pSchema { goto _2 } if pSchema == uintptr(0) && libc.Xstrcmp(tls, zDb, __ccgo_ts+6590) != 0 { goto _2 } } if (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias != uintptr(0) { if _sqlite3StrICmp(tls, zTab, (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias) != 0 { goto _2 } } else { if _sqlite3StrICmp(tls, zTab, (*TTable)(unsafe.Pointer(pTab)).FzName) != 0 { if (*TTable)(unsafe.Pointer(pTab)).Ftnum != uint32(1) { goto _2 } if !(_isValidSchemaTableName(tls, zTab, pTab, pSchema) != 0) { goto _2 } } } _ = libc.Int32FromInt32(0) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias != 0 { _sqlite3RenameTokenRemap(tls, pParse, uintptr(0), pExpr+44) } } hCol = _sqlite3StrIHash(tls, zCol) j = 0 pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol for { if !(j < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if int32((*TColumn)(unsafe.Pointer(pCol)).FhName) == int32(hCol) && _sqlite3StrICmp(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, zCol) == 0 { if cnt > 0 { if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x400>>10)) == 0 || _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pItem + 48)), zCol) < 0 { /* Two or more tables have the same column name which is ** not joined by USING. This is an error. Signal as much ** by clearing pFJMatch and letting cnt go above 1. */ _sqlite3ExprListDelete(tls, db, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } else { if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_RIGHT) == 0 { /* An INNER or LEFT JOIN. Use the left-most table */ goto _4 } else { if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_LEFT) == 0 { /* A RIGHT JOIN. Use the right-most table */ cnt = 0 _sqlite3ExprListDelete(tls, db, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } else { /* For a FULL JOIN, we must construct a coalesce() func */ _extendFJMatch(tls, pParse, bp, pMatch, (*TExpr)(unsafe.Pointer(pExpr)).FiColumn) } } } } cnt++ pMatch = pItem /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ if j == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { v5 = -int32(1) } else { v5 = int32(int16(j)) } (*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(v5) if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x2000>>13)) != 0 { _sqlite3SrcItemColumnUsed(tls, pItem, j) } break } goto _4 _4: ; j++ pCol += 12 } if 0 == cnt && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_NoVisibleRowid) == uint32(0) { /* pTab is a potential ROWID match. Keep track of it and match ** the ROWID later if that seems appropriate. (Search for "cntTab" ** to find related code.) Only allow a ROWID match if there is ** a single ROWID match candidate. */ /* The (much more common) non-SQLITE_ALLOW_ROWID_IN_VIEW case is ** simpler since we require exactly one candidate, which will ** always be a non-VIEW */ cntTab++ pMatch = pItem } goto _2 _2: ; i++ pItem += 72 } if pMatch != 0 { (*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TSrcItem)(unsafe.Pointer(pMatch)).FiCursor _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab = (*TSrcItem)(unsafe.Pointer(pMatch)).FpTab if int32((*TSrcItem)(unsafe.Pointer(pMatch)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)) != 0 { *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_CanBeNull)) } pSchema = (*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab)).FpSchema } } /* if( pSrcList ) */ /* If we have not already resolved the name, then maybe ** it is a new.* or old.* trigger argument reference. Or ** maybe it is an excluded.* from an upsert. Or maybe it is ** a reference in the RETURNING clause to a table being modified. */ if cnt == 0 && zDb == uintptr(0) { pTab = uintptr(0) if (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab != uintptr(0) { op = int32((*TParse)(unsafe.Pointer(pParse)).FeTriggerOp) _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FbReturning != 0 { if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_UBaseReg) != 0 && (zTab == uintptr(0) || _sqlite3StrICmp(tls, zTab, (*TTable)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).FpTriggerTab)).FzName) == 0) { (*TExpr)(unsafe.Pointer(pExpr)).FiTable = libc.BoolInt32(op != int32(TK_DELETE)) pTab = (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab } } else { if op != int32(TK_DELETE) && zTab != 0 && _sqlite3StrICmp(tls, __ccgo_ts+6592, zTab) == 0 { (*TExpr)(unsafe.Pointer(pExpr)).FiTable = int32(1) pTab = (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab } else { if op != int32(TK_INSERT) && zTab != 0 && _sqlite3StrICmp(tls, __ccgo_ts+6596, zTab) == 0 { (*TExpr)(unsafe.Pointer(pExpr)).FiTable = 0 pTab = (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab } } } } if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_UUpsert) != 0 && zTab != uintptr(0) { pUpsert = *(*uintptr)(unsafe.Pointer(pNC + 8)) if pUpsert != 0 && _sqlite3StrICmp(tls, __ccgo_ts+6600, zTab) == 0 { pTab = (*(*TSrcItem)(unsafe.Pointer((*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertSrc + 8))).FpTab (*TExpr)(unsafe.Pointer(pExpr)).FiTable = int32(EXCLUDED_TABLE_NUMBER) } } if pTab != 0 { hCol1 = _sqlite3StrIHash(tls, zCol) pSchema = (*TTable)(unsafe.Pointer(pTab)).FpSchema cntTab++ iCol = 0 pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol for { if !(iCol < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if int32((*TColumn)(unsafe.Pointer(pCol)).FhName) == int32(hCol1) && _sqlite3StrICmp(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, zCol) == 0 { if iCol == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { iCol = -int32(1) } break } goto _6 _6: ; iCol++ pCol += 12 } if iCol >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) && _sqlite3IsRowid(tls, zCol) != 0 && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_NoVisibleRowid) == uint32(0) { /* IMP: R-51414-32910 */ iCol = -int32(1) } if iCol < int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { cnt++ pMatch = uintptr(0) if (*TExpr)(unsafe.Pointer(pExpr)).FiTable == int32(EXCLUDED_TABLE_NUMBER) { _ = libc.Int32FromInt32(0) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { (*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(iCol) (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab = pTab eNewExprOp = int32(TK_COLUMN) } else { (*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TUpsert)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNC + 8)))).FregData + int32(_sqlite3TableColumnToStorage(tls, pTab, int16(iCol))) eNewExprOp = int32(TK_REGISTER) } } else { _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab = pTab if (*TParse)(unsafe.Pointer(pParse)).FbReturning != 0 { eNewExprOp = int32(TK_REGISTER) (*TExpr)(unsafe.Pointer(pExpr)).Fop2 = uint8(TK_COLUMN) (*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(iCol) (*TExpr)(unsafe.Pointer(pExpr)).FiTable = *(*int32)(unsafe.Pointer(pNC + 8)) + (int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+int32(1))*(*TExpr)(unsafe.Pointer(pExpr)).FiTable + int32(_sqlite3TableColumnToStorage(tls, pTab, int16(iCol))) + int32(1) } else { (*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(iCol) eNewExprOp = int32(TK_TRIGGER) if iCol < 0 { (*TExpr)(unsafe.Pointer(pExpr)).FaffExpr = uint8(SQLITE_AFF_INTEGER) } else { if (*TExpr)(unsafe.Pointer(pExpr)).FiTable == 0 { if iCol >= int32(32) { v7 = uint32(0xffffffff) } else { v7 = libc.Uint32FromInt32(1) << iCol } *(*Tu32)(unsafe.Pointer(pParse + 148)) |= v7 } else { if iCol >= int32(32) { v8 = uint32(0xffffffff) } else { v8 = libc.Uint32FromInt32(1) << iCol } *(*Tu32)(unsafe.Pointer(pParse + 152)) |= v8 } } } } } } } /* ** Perhaps the name is a reference to the ROWID */ if cnt == 0 && cntTab >= int32(1) && pMatch != 0 && (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&(libc.Int32FromInt32(NC_IdxExpr)|libc.Int32FromInt32(NC_GenCol)) == 0 && _sqlite3IsRowid(tls, zCol) != 0 && ((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pMatch)).FpTab)).FtabFlags&uint32(TF_NoVisibleRowid) == uint32(0) || int32(uint32(*(*uint16)(unsafe.Pointer(pMatch + 36 + 4))&0x2000>>13)) != 0) { cnt = cntTab if int32(uint32(*(*uint16)(unsafe.Pointer(pMatch + 36 + 4))&0x2000>>13)) == 0 { (*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(-int32(1)) } (*TExpr)(unsafe.Pointer(pExpr)).FaffExpr = uint8(SQLITE_AFF_INTEGER) } /* ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z ** might refer to an result-set alias. This happens, for example, when ** we are resolving names in the WHERE clause of the following command: ** ** SELECT a+b AS x FROM table WHERE x<10; ** ** In cases like this, replace pExpr with a copy of the expression that ** forms the result set entry ("a+b" in the example) and return immediately. ** Note that the expression in the result set should have already been ** resolved by the time the WHERE clause is resolved. ** ** The ability to use an output result-set column in the WHERE, GROUP BY, ** or HAVING clauses, or as part of a larger expression in the ORDER BY ** clause is not standard SQL. This is a (goofy) SQLite extension, that ** is supported for backwards compatibility only. Hence, we issue a warning ** on sqlite3_log() whenever the capability is used. */ if cnt == 0 && (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_UEList) != 0 && zTab == uintptr(0) { pEList = *(*uintptr)(unsafe.Pointer(pNC + 8)) _ = libc.Int32FromInt32(0) j = 0 for { if !(j < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } zAs = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(j)*20))).FzEName if int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(j)*20 + 8 + 4))&0x3>>0)) == ENAME_NAME && Xsqlite3_stricmp(tls, zAs, zCol) == 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pOrig = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(j)*20))).FpExpr if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_AllowAgg) == 0 && (*TExpr)(unsafe.Pointer(pOrig)).Fflags&uint32(libc.Int32FromInt32(EP_Agg)) != uint32(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+6609, libc.VaList(bp+16, zAs)) return int32(WRC_Abort) } if (*TExpr)(unsafe.Pointer(pOrig)).Fflags&uint32(libc.Int32FromInt32(EP_Win)) != uint32(0) && ((*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_AllowWin) == 0 || pNC != pTopNC) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+6640, libc.VaList(bp+16, zAs)) return int32(WRC_Abort) } if _sqlite3ExprVectorSize(tls, pOrig) != int32(1) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+6677, 0) return int32(WRC_Abort) } _resolveAlias(tls, pParse, pEList, j, pExpr, nSubquery) cnt = int32(1) pMatch = uintptr(0) _ = libc.Int32FromInt32(0) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenRemap(tls, pParse, uintptr(0), pExpr) } goto lookupname_end } goto _9 _9: ; j++ } } /* Advance to the next name context. The loop will exit when either ** we have a match (cnt>0) or when we run out of name contexts. */ if cnt != 0 { break } pNC = (*TNameContext)(unsafe.Pointer(pNC)).FpNext nSubquery++ } /* ** If X and Y are NULL (in other words if only the column name Z is ** supplied) and the value of Z is enclosed in double-quotes, then ** Z is a string literal if it doesn't match any column names. In that ** case, we need to return right away and not make any changes to ** pExpr. ** ** Because no reference was made to outer contexts, the pNC->nRef ** fields are not changed in any context. */ if cnt == 0 && zTab == uintptr(0) { _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_DblQuoted)) != uint32(0) && _areDoubleQuotedStringsEnabled(tls, db, pTopNC) != 0 { /* If a double-quoted identifier does not match any known column name, ** then treat it as a string. ** ** This hack was added in the early days of SQLite in a misguided attempt ** to be compatible with MySQL 3.x, which used double-quotes for strings. ** I now sorely regret putting in this hack. The effect of this hack is ** that misspelled identifier names are silently converted into strings ** rather than causing an error, to the frustration of countless ** programmers. To all those frustrated programmers, my apologies. ** ** Someday, I hope to get rid of this hack. Unfortunately there is ** a huge amount of legacy SQL that uses it. So for now, we just ** issue a warning. */ Xsqlite3_log(tls, int32(SQLITE_WARNING), __ccgo_ts+6695, libc.VaList(bp+16, zCol)) (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_STRING) libc.Xmemset(tls, pExpr+44, 0, uint32(8)) return int32(WRC_Prune) } if _sqlite3ExprIdToTrueFalse(tls, pExpr) != 0 { return int32(WRC_Prune) } } /* ** cnt==0 means there was not match. ** cnt>1 means there were two or more matches. ** ** cnt==0 is always an error. cnt>1 is often an error, but might ** be multiple matches for a NATURAL LEFT JOIN or a LEFT JOIN USING. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if cnt != int32(1) { if *(*uintptr)(unsafe.Pointer(bp)) != 0 { if (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnExpr == cnt-int32(1) { if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Leaf)) != uint32(0) { *(*Tu32)(unsafe.Pointer(pExpr + 4)) &= uint32(^libc.Int32FromInt32(EP_Leaf)) } else { _sqlite3ExprDelete(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) (*TExpr)(unsafe.Pointer(pExpr)).FpLeft = uintptr(0) _sqlite3ExprDelete(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) (*TExpr)(unsafe.Pointer(pExpr)).FpRight = uintptr(0) } _extendFJMatch(tls, pParse, bp, pMatch, (*TExpr)(unsafe.Pointer(pExpr)).FiColumn) (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_FUNCTION) *(*uintptr)(unsafe.Pointer(pExpr + 8)) = __ccgo_ts + 6730 *(*uintptr)(unsafe.Pointer(pExpr + 20)) = *(*uintptr)(unsafe.Pointer(bp)) cnt = int32(1) goto lookupname_end } else { _sqlite3ExprListDelete(tls, db, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } } if cnt == 0 { v10 = __ccgo_ts + 6739 } else { v10 = __ccgo_ts + 6754 } zErr = v10 if zDb != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+6776, libc.VaList(bp+16, zErr, zDb, zTab, zCol)) } else { if zTab != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+6789, libc.VaList(bp+16, zErr, zTab, zCol)) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+6799, libc.VaList(bp+16, zErr, zCol)) } } _sqlite3RecordErrorOffsetOfExpr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr) (*TParse)(unsafe.Pointer(pParse)).FcheckSchema = uint8(1) (*TNameContext)(unsafe.Pointer(pTopNC)).FnNcErr++ eNewExprOp = int32(TK_NULL) } _ = libc.Int32FromInt32(0) /* Remove all substructure from pExpr */ if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_TokenOnly)|libc.Int32FromInt32(EP_Leaf)) != libc.Uint32FromInt32(0)) { _sqlite3ExprDelete(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) (*TExpr)(unsafe.Pointer(pExpr)).FpLeft = uintptr(0) _sqlite3ExprDelete(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) (*TExpr)(unsafe.Pointer(pExpr)).FpRight = uintptr(0) *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_Leaf)) } /* If a column from a table in pSrcList is referenced, then record ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes ** bit 0 to be set. Column 1 sets bit 1. And so forth. Bit 63 is ** set if the 63rd or any subsequent column is used. ** ** The colUsed mask is an optimization used to help determine if an ** index is a covering index. The correct answer is still obtained ** if the mask contains extra set bits. However, it is important to ** avoid setting bits beyond the maximum column number of the table. ** (See ticket [b92e5e8ec2cdbaa1]). ** ** If a generated column is referenced, set bits for every column ** of the table. */ if int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) >= 0 && cnt == int32(1) && pMatch != uintptr(0) { *(*TBitmask)(unsafe.Pointer(pMatch + 56)) |= _sqlite3ExprColUsed(tls, pExpr) } (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(eNewExprOp) goto lookupname_end lookupname_end: ; if cnt == int32(1) { _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FxAuth != 0 && (int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_TRIGGER)) { _sqlite3AuthRead(tls, pParse, pExpr, pSchema, (*TNameContext)(unsafe.Pointer(pNC)).FpSrcList) } /* Increment the nRef value on all name contexts from TopNC up to ** the point where the name matched. */ for { _ = libc.Int32FromInt32(0) (*TNameContext)(unsafe.Pointer(pTopNC)).FnRef++ if pTopNC == pNC { break } pTopNC = (*TNameContext)(unsafe.Pointer(pTopNC)).FpNext goto _11 _11: } return int32(WRC_Prune) } else { return int32(WRC_Abort) } return r } // C documentation // // /* // ** Allocate and return a pointer to an expression to load the column iCol // ** from datasource iSrc in SrcList pSrc. // */ func _sqlite3CreateColumnExpr(tls *libc.TLS, db uintptr, pSrc uintptr, iSrc int32, iCol int32) (r uintptr) { var p, pItem, pTab, v1 uintptr var v2 uint64 var v3 int32 _, _, _, _, _, _ = p, pItem, pTab, v1, v2, v3 p = _sqlite3ExprAlloc(tls, db, int32(TK_COLUMN), uintptr(0), 0) if p != 0 { pItem = pSrc + 8 + uintptr(iSrc)*72 _ = libc.Int32FromInt32(0) v1 = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab (*TExpr)(unsafe.Pointer(p)).Fy.FpTab = v1 pTab = v1 (*TExpr)(unsafe.Pointer(p)).FiTable = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor if int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).Fy.FpTab)).FiPKey) == iCol { (*TExpr)(unsafe.Pointer(p)).FiColumn = int16(-int32(1)) } else { (*TExpr)(unsafe.Pointer(p)).FiColumn = int16(iCol) if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != uint32(0) && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { if int32((*TTable)(unsafe.Pointer(pTab)).FnCol) >= int32(64) { v2 = uint64(-libc.Int32FromInt32(1)) } else { v2 = libc.Uint64FromInt32(1)<<(*TTable)(unsafe.Pointer(pTab)).FnCol - uint64(1) } (*TSrcItem)(unsafe.Pointer(pItem)).FcolUsed = v2 } else { if iCol >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) { v3 = int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1) } else { v3 = iCol } *(*TBitmask)(unsafe.Pointer(pItem + 56)) |= libc.Uint64FromInt32(1) << v3 } } } return p } // C documentation // // /* // ** Report an error that an expression is not valid for some set of // ** pNC->ncFlags values determined by validMask. // ** // ** static void notValid( // ** Parse *pParse, // Leave error message here // ** NameContext *pNC, // The name context // ** const char *zMsg, // Type of error // ** int validMask, // Set of contexts for which prohibited // ** Expr *pExpr // Invalidate this expression on error // ** ){...} // ** // ** As an optimization, since the conditional is almost always false // ** (because errors are rare), the conditional is moved outside of the // ** function call using a macro. // */ func _notValidImpl(tls *libc.TLS, pParse uintptr, pNC uintptr, zMsg uintptr, pExpr uintptr, pError uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var zIn uintptr _ = zIn zIn = __ccgo_ts + 6806 if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_IdxExpr) != 0 { zIn = __ccgo_ts + 6834 } else { if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_IsCheck) != 0 { zIn = __ccgo_ts + 6852 } else { if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_GenCol) != 0 { zIn = __ccgo_ts + 6870 } } } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+6888, libc.VaList(bp+8, zMsg, zIn)) if pExpr != 0 { (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_NULL) } _sqlite3RecordErrorOffsetOfExpr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pError) } // C documentation // // /* // ** Expression p should encode a floating point value between 1.0 and 0.0. // ** Return 1024 times this value. Or return -1 if p is not a floating point // ** value between 1.0 and 0.0. // */ func _exprProbability(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* r at bp+0 */ float64 *(*float64)(unsafe.Pointer(bp)) = -libc.Float64FromFloat64(1) if int32((*TExpr)(unsafe.Pointer(p)).Fop) != int32(TK_FLOAT) { return -int32(1) } _ = libc.Int32FromInt32(0) _sqlite3AtoF(tls, *(*uintptr)(unsafe.Pointer(p + 8)), bp, _sqlite3Strlen30(tls, *(*uintptr)(unsafe.Pointer(p + 8))), uint8(SQLITE_UTF8)) _ = libc.Int32FromInt32(0) if *(*float64)(unsafe.Pointer(bp)) > float64(1) { return -int32(1) } return int32(*(*float64)(unsafe.Pointer(bp)) * libc.Float64FromFloat64(1.34217728e+08)) } // C documentation // // /* // ** This routine is callback for sqlite3WalkExpr(). // ** // ** Resolve symbolic names into TK_COLUMN operators for the current // ** node in the expression tree. Return 0 to continue the search down // ** the tree or 2 to abort the tree walk. // ** // ** This routine also does error checking and name resolution for // ** function names. The operator for aggregate functions is changed // ** to TK_AGG_FUNCTION. // */ func _resolveExprStep(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var anRef [8]int32 var auth, i, is_agg, n, nLeft, nRef, nRight, no_such_func, rc, savedAllowFlags, wrong_num_args, v12, v4, v6, v7 int32 var enc Tu8 var p, pDef, pItem, pLeft, pList, pNC, pNC2, pParse, pRight, pRight1, pSel, pSrcList, pWin, zColumn, zDb, zId, zTable, zType, v5, v8, p10, p11, p9 uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = anRef, auth, enc, i, is_agg, n, nLeft, nRef, nRight, no_such_func, p, pDef, pItem, pLeft, pList, pNC, pNC2, pParse, pRight, pRight1, pSel, pSrcList, pWin, rc, savedAllowFlags, wrong_num_args, zColumn, zDb, zId, zTable, zType, v12, v4, v5, v6, v7, v8, p10, p11, p9 pNC = *(*uintptr)(unsafe.Pointer(pWalker + 24)) _ = libc.Int32FromInt32(0) pParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse _ = libc.Int32FromInt32(0) switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) { /* The special operator TK_ROW means use the rowid for the first ** column in the FROM clause. This is used by the LIMIT and ORDER BY ** clause processing on UPDATE and DELETE statements, and by ** UPDATE ... FROM statement processing. */ case int32(TK_ROW): pSrcList = (*TNameContext)(unsafe.Pointer(pNC)).FpSrcList _ = libc.Int32FromInt32(0) pItem = pSrcList + 8 (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_COLUMN) _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab (*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor (*TExpr)(unsafe.Pointer(pExpr)).FiColumn-- (*TExpr)(unsafe.Pointer(pExpr)).FaffExpr = uint8(SQLITE_AFF_INTEGER) break /* An optimization: Attempt to convert ** ** "expr IS NOT NULL" --> "TRUE" ** "expr IS NULL" --> "FALSE" ** ** if we can prove that "expr" is never NULL. Call this the ** "NOT NULL strength reduction optimization". ** ** If this optimization occurs, also restore the NameContext ref-counts ** to the state they where in before the "column" LHS expression was ** resolved. This prevents "column" from being counted as having been ** referenced, which might prevent a SELECT from being erroneously ** marked as correlated. ** ** 2024-03-28: Beware of aggregates. A bare column of aggregated table ** can still evaluate to NULL even though it is marked as NOT NULL. ** Example: ** ** CREATE TABLE t1(a INT NOT NULL); ** SELECT a, a IS NULL, a IS NOT NULL, count(*) FROM t1; ** ** The "a IS NULL" and "a IS NOT NULL" expressions cannot be optimized ** here because at the time this case is hit, we do not yet know whether ** or not t1 is being aggregated. We have to assume the worst and omit ** the optimization. The only time it is safe to apply this optimization ** is within the WHERE clause. */ fallthrough case int32(TK_NOTNULL): fallthrough case int32(TK_ISNULL): i = 0 p = pNC for { if !(p != 0 && i < int32(libc.Uint32FromInt64(32)/libc.Uint32FromInt64(4))) { break } anRef[i] = (*TNameContext)(unsafe.Pointer(p)).FnRef goto _1 _1: ; p = (*TNameContext)(unsafe.Pointer(p)).FpNext i++ } _sqlite3WalkExpr(tls, pWalker, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { return int32(WRC_Prune) } if _sqlite3ExprCanBeNull(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) != 0 { /* The expression can be NULL. So the optimization does not apply */ return int32(WRC_Prune) } i = 0 p = pNC for { if !(p != 0) { break } if (*TNameContext)(unsafe.Pointer(p)).FncFlags&int32(NC_Where) == 0 { return int32(WRC_Prune) /* Not in a WHERE clause. Unsafe to optimize. */ } goto _2 _2: ; p = (*TNameContext)(unsafe.Pointer(p)).FpNext i++ } _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pExpr + 8)) = libc.BoolInt32(int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_NOTNULL)) *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(EP_IntValue) (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_INTEGER) i = 0 p = pNC for { if !(p != 0 && i < int32(libc.Uint32FromInt64(32)/libc.Uint32FromInt64(4))) { break } (*TNameContext)(unsafe.Pointer(p)).FnRef = anRef[i] goto _3 _3: ; p = (*TNameContext)(unsafe.Pointer(p)).FpNext i++ } _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) (*TExpr)(unsafe.Pointer(pExpr)).FpLeft = uintptr(0) return int32(WRC_Prune) /* A column name: ID ** Or table name and column name: ID.ID ** Or a database, table and column: ID.ID.ID ** ** The TK_ID and TK_OUT cases are combined so that there will only ** be one call to lookupName(). Then the compiler will in-line ** lookupName() for a size reduction and performance increase. */ fallthrough case int32(TK_ID): fallthrough case int32(TK_DOT): if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_ID) { zDb = uintptr(0) zTable = uintptr(0) _ = libc.Int32FromInt32(0) zColumn = *(*uintptr)(unsafe.Pointer(pExpr + 8)) } else { pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft _ = libc.Int32FromInt32(0) if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&(libc.Int32FromInt32(NC_IdxExpr)|libc.Int32FromInt32(NC_GenCol)) != 0 { _notValidImpl(tls, pParse, pNC, __ccgo_ts+6908, uintptr(0), pExpr) } pRight = (*TExpr)(unsafe.Pointer(pExpr)).FpRight if int32((*TExpr)(unsafe.Pointer(pRight)).Fop) == int32(TK_ID) { zDb = uintptr(0) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) zDb = *(*uintptr)(unsafe.Pointer(pLeft + 8)) pLeft = (*TExpr)(unsafe.Pointer(pRight)).FpLeft pRight = (*TExpr)(unsafe.Pointer(pRight)).FpRight } _ = libc.Int32FromInt32(0) zTable = *(*uintptr)(unsafe.Pointer(pLeft + 8)) zColumn = *(*uintptr)(unsafe.Pointer(pRight + 8)) _ = libc.Int32FromInt32(0) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenRemap(tls, pParse, pExpr, pRight) _sqlite3RenameTokenRemap(tls, pParse, pExpr+44, pLeft) } } return _lookupName(tls, pParse, zDb, zTable, zColumn, pNC, pExpr) /* Resolve function names */ fallthrough case int32(TK_FUNCTION): pList = *(*uintptr)(unsafe.Pointer(pExpr + 20)) if pList != 0 { v4 = (*TExprList)(unsafe.Pointer(pList)).FnExpr } else { v4 = 0 } /* The argument list */ n = v4 /* Number of arguments */ no_such_func = 0 /* True if no such function exists */ wrong_num_args = 0 /* True if wrong number of arguments */ is_agg = 0 /* Information about the function */ enc = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fenc /* The database encoding */ savedAllowFlags = (*TNameContext)(unsafe.Pointer(pNC)).FncFlags & (libc.Int32FromInt32(NC_AllowAgg) | libc.Int32FromInt32(NC_AllowWin)) if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) && int32((*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)))).FeFrmType) != int32(TK_FILTER) { v5 = *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)) } else { v5 = uintptr(0) } pWin = v5 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) zId = *(*uintptr)(unsafe.Pointer(pExpr + 8)) pDef = _sqlite3FindFunction(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, zId, n, enc, uint8(0)) if pDef == uintptr(0) { pDef = _sqlite3FindFunction(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, zId, -int32(2), enc, uint8(0)) if pDef == uintptr(0) { no_such_func = int32(1) } else { wrong_num_args = int32(1) } } else { is_agg = libc.BoolInt32((*TFuncDef)(unsafe.Pointer(pDef)).FxFinalize != uintptr(0)) if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_UNLIKELY) != 0 { *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_Unlikely)) if n == int32(2) { (*TExpr)(unsafe.Pointer(pExpr)).FiTable = _exprProbability(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + 1*20))).FpExpr) if (*TExpr)(unsafe.Pointer(pExpr)).FiTable < 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+6925, libc.VaList(bp+8, pExpr)) (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr++ } } else { /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is ** equivalent to likelihood(X, 0.0625). ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is ** short-hand for likelihood(X,0.0625). ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand ** for likelihood(X,0.9375). ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent ** to likelihood(X,0.9375). */ /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */ if int32(*(*uint8)(unsafe.Pointer((*TFuncDef)(unsafe.Pointer(pDef)).FzName))) == int32('u') { v6 = int32(8388608) } else { v6 = int32(125829120) } (*TExpr)(unsafe.Pointer(pExpr)).FiTable = v6 } } auth = _sqlite3AuthCheck(tls, pParse, int32(SQLITE_FUNCTION), uintptr(0), (*TFuncDef)(unsafe.Pointer(pDef)).FzName, uintptr(0)) if auth != SQLITE_OK { if auth == int32(SQLITE_DENY) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+6989, libc.VaList(bp+8, pExpr)) (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr++ } (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_NULL) return int32(WRC_Prune) } if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)|libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG)) != 0 { /* For the purposes of the EP_ConstFunc flag, date and time ** functions and other functions that change slowly are considered ** constant because they are constant for the duration of one query. ** This allows them to be factored out of inner loops. */ *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_ConstFunc)) } if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_CONSTANT) == uint32(0) { /* Clearly non-deterministic functions like random(), but also ** date/time functions that use 'now', and other functions like ** sqlite_version() that might change over time cannot be used ** in an index or generated column. Curiously, they can be used ** in a CHECK constraint. SQLServer, MySQL, and PostgreSQL all ** all this. */ _ = libc.Int32FromInt32(0) if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&(libc.Int32FromInt32(NC_IdxExpr)|libc.Int32FromInt32(NC_PartIdx)|libc.Int32FromInt32(NC_GenCol)) != 0 { _notValidImpl(tls, pParse, pNC, __ccgo_ts+7025, uintptr(0), pExpr) } } else { _ = libc.Int32FromInt32(0) /* Must fit in 8 bits */ (*TExpr)(unsafe.Pointer(pExpr)).Fop2 = uint8((*TNameContext)(unsafe.Pointer(pNC)).FncFlags & int32(NC_SelfRef)) if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_FromDDL) != 0 { *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_FromDDL)) } } if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_INTERNAL) != uint32(0) && int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmDbFlags&uint32(DBFLAG_InternalFunc) == uint32(0) { /* Internal-use-only functions are disallowed unless the ** SQL is being compiled using sqlite3NestedParse() or ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be ** used to activate internal functions for testing purposes */ no_such_func = int32(1) pDef = uintptr(0) } else { if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(libc.Int32FromInt32(SQLITE_FUNC_DIRECT)|libc.Int32FromInt32(SQLITE_FUNC_UNSAFE)) != uint32(0) && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) { _sqlite3ExprFunctionUsable(tls, pParse, pExpr, pDef) } } } if 0 == libc.BoolInt32(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME)) { _ = libc.Int32FromInt32(0) if pDef != 0 && (*TFuncDef)(unsafe.Pointer(pDef)).FxValue == uintptr(0) && pWin != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7053, libc.VaList(bp+8, pExpr)) (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr++ } else { if is_agg != 0 && (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_AllowAgg) == 0 || is_agg != 0 && (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_WINDOW) != 0 && !(pWin != 0) || is_agg != 0 && pWin != 0 && (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_AllowWin) == 0 { if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_WINDOW) != 0 || pWin != 0 { zType = __ccgo_ts + 7096 } else { zType = __ccgo_ts + 7103 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7113, libc.VaList(bp+8, zType, pExpr)) (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr++ is_agg = 0 } else { if no_such_func != 0 && int32((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Finit1.Fbusy) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7141, libc.VaList(bp+8, pExpr)) (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr++ } else { if wrong_num_args != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7163, libc.VaList(bp+8, pExpr)) (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr++ } else { if is_agg == 0 && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7207, libc.VaList(bp+8, pExpr)) (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr++ } else { if is_agg == 0 && (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 { _sqlite3ExprOrderByAggregateError(tls, pParse, pExpr) (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr++ } } } } } } if is_agg != 0 { /* Window functions may not be arguments of aggregate functions. ** Or arguments of other window functions. But aggregate functions ** may be arguments for window functions. */ if !(pWin != 0) { v7 = int32(NC_AllowAgg) } else { v7 = 0 } *(*int32)(unsafe.Pointer(pNC + 24)) &= ^(libc.Int32FromInt32(NC_AllowWin) | v7) } } else { if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { is_agg = int32(1) } } _sqlite3WalkExprList(tls, pWalker, pList) if is_agg != 0 { if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3WalkExprList(tls, pWalker, *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft + 20))) } if pWin != 0 { pSel = (*TNameContext)(unsafe.Pointer(pNC)).FpWinSelect _ = libc.Int32FromInt32(0) if libc.BoolInt32(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME)) == 0 { if pSel != 0 { v8 = (*TSelect)(unsafe.Pointer(pSel)).FpWinDefn } else { v8 = uintptr(0) } _sqlite3WindowUpdate(tls, pParse, v8, pWin, pDef) if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 { break } } _sqlite3WalkExprList(tls, pWalker, (*TWindow)(unsafe.Pointer(pWin)).FpPartition) _sqlite3WalkExprList(tls, pWalker, (*TWindow)(unsafe.Pointer(pWin)).FpOrderBy) _sqlite3WalkExpr(tls, pWalker, (*TWindow)(unsafe.Pointer(pWin)).FpFilter) _sqlite3WindowLink(tls, pSel, pWin) *(*int32)(unsafe.Pointer(pNC + 24)) |= int32(NC_HasWin) } else { /* For looping up thru outer contexts */ (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_AGG_FUNCTION) (*TExpr)(unsafe.Pointer(pExpr)).Fop2 = uint8(0) if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { _sqlite3WalkExpr(tls, pWalker, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)))).FpFilter) } pNC2 = pNC for pNC2 != 0 && _sqlite3ReferencesSrcList(tls, pParse, pExpr, (*TNameContext)(unsafe.Pointer(pNC2)).FpSrcList) == 0 { p9 = pExpr + 2 *(*Tu8)(unsafe.Pointer(p9)) = Tu8(uint32(*(*Tu8)(unsafe.Pointer(p9))) + (libc.Uint32FromInt32(1) + (*TNameContext)(unsafe.Pointer(pNC2)).FnNestedSelect)) pNC2 = (*TNameContext)(unsafe.Pointer(pNC2)).FpNext } _ = libc.Int32FromInt32(0) if pNC2 != 0 && pDef != 0 { p10 = pExpr + 2 *(*Tu8)(unsafe.Pointer(p10)) = Tu8(uint32(*(*Tu8)(unsafe.Pointer(p10))) + (*TNameContext)(unsafe.Pointer(pNC2)).FnNestedSelect) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) p11 = pNC2 + 24 *(*int32)(unsafe.Pointer(p11)) = int32(uint32(*(*int32)(unsafe.Pointer(p11))) | (libc.Uint32FromInt32(NC_HasAgg) | ((*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags^libc.Uint32FromInt32(SQLITE_FUNC_ANYORDER))&uint32(libc.Int32FromInt32(SQLITE_FUNC_MINMAX)|libc.Int32FromInt32(SQLITE_FUNC_ANYORDER)))) } } *(*int32)(unsafe.Pointer(pNC + 24)) |= savedAllowFlags } /* FIX ME: Compute pExpr->affinity based on the expected return ** type of the function */ return int32(WRC_Prune) case int32(TK_SELECT): fallthrough case int32(TK_EXISTS): fallthrough case int32(TK_IN): if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) { nRef = (*TNameContext)(unsafe.Pointer(pNC)).FnRef if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_SelfRef) != 0 { _notValidImpl(tls, pParse, pNC, __ccgo_ts+7255, pExpr, pExpr) } else { _sqlite3WalkSelect(tls, pWalker, *(*uintptr)(unsafe.Pointer(pExpr + 20))) } _ = libc.Int32FromInt32(0) if nRef != (*TNameContext)(unsafe.Pointer(pNC)).FnRef { *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_VarSelect)) } *(*int32)(unsafe.Pointer(pNC + 24)) |= int32(NC_Subquery) } case int32(TK_VARIABLE): _ = libc.Int32FromInt32(0) if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&(libc.Int32FromInt32(NC_IsCheck)|libc.Int32FromInt32(NC_PartIdx)|libc.Int32FromInt32(NC_IdxExpr)|libc.Int32FromInt32(NC_GenCol)) != 0 { _notValidImpl(tls, pParse, pNC, __ccgo_ts+7266, pExpr, pExpr) } case int32(TK_IS): fallthrough case int32(TK_ISNOT): pRight1 = _sqlite3ExprSkipCollateAndLikely(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) _ = libc.Int32FromInt32(0) /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE", ** and "x IS NOT FALSE". */ if pRight1 != 0 && (int32((*TExpr)(unsafe.Pointer(pRight1)).Fop) == int32(TK_ID) || int32((*TExpr)(unsafe.Pointer(pRight1)).Fop) == int32(TK_TRUEFALSE)) { rc = _resolveExprStep(tls, pWalker, pRight1) if rc == int32(WRC_Abort) { return int32(WRC_Abort) } if int32((*TExpr)(unsafe.Pointer(pRight1)).Fop) == int32(TK_TRUEFALSE) { (*TExpr)(unsafe.Pointer(pExpr)).Fop2 = (*TExpr)(unsafe.Pointer(pExpr)).Fop (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_TRUTH) return WRC_Continue } } fallthrough case int32(TK_BETWEEN): fallthrough case int32(TK_EQ): fallthrough case int32(TK_NE): fallthrough case int32(TK_LT): fallthrough case int32(TK_LE): fallthrough case int32(TK_GT): fallthrough case int32(TK_GE): if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 { break } _ = libc.Int32FromInt32(0) nLeft = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_BETWEEN) { _ = libc.Int32FromInt32(0) nRight = _sqlite3ExprVectorSize(tls, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8))).FpExpr) if nRight == nLeft { nRight = _sqlite3ExprVectorSize(tls, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8 + 1*20))).FpExpr) } } else { _ = libc.Int32FromInt32(0) nRight = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) } if nLeft != nRight { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+6677, 0) _sqlite3RecordErrorOffsetOfExpr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr) } break } _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { v12 = int32(WRC_Abort) } else { v12 = WRC_Continue } return v12 } // C documentation // // /* // ** pEList is a list of expressions which are really the result set of the // ** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause. // ** This routine checks to see if pE is a simple identifier which corresponds // ** to the AS-name of one of the terms of the expression list. If it is, // ** this routine return an integer between 1 and N where N is the number of // ** elements in pEList, corresponding to the matching entry. If there is // ** no match, or if pE is not a simple identifier, then this routine // ** return 0. // ** // ** pEList has been resolved. pE has not. // */ func _resolveAsName(tls *libc.TLS, pParse uintptr, pEList uintptr, pE uintptr) (r int32) { var i int32 var zCol uintptr _, _ = i, zCol /* Loop counter */ _ = pParse if int32((*TExpr)(unsafe.Pointer(pE)).Fop) == int32(TK_ID) { _ = libc.Int32FromInt32(0) zCol = *(*uintptr)(unsafe.Pointer(pE + 8)) i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } if int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(i)*20 + 8 + 4))&0x3>>0)) == ENAME_NAME && Xsqlite3_stricmp(tls, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FzEName, zCol) == 0 { return i + int32(1) } goto _1 _1: ; i++ } } return 0 } // C documentation // // /* // ** pE is a pointer to an expression which is a single term in the // ** ORDER BY of a compound SELECT. The expression has not been // ** name resolved. // ** // ** At the point this routine is called, we already know that the // ** ORDER BY term is not an integer index into the result set. That // ** case is handled by the calling routine. // ** // ** Attempt to match pE against result set columns in the left-most // ** SELECT statement. Return the index i of the matching column, // ** as an indication to the caller that it should sort by the i-th column. // ** The left-most column is 1. In other words, the value returned is the // ** same integer value that would be used in the SQL statement to indicate // ** the column. // ** // ** If there is no match, return 0. Return -1 if an error occurs. // */ func _resolveOrderByTermToExprList(tls *libc.TLS, pParse uintptr, pSelect uintptr, pE uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var db, pEList uintptr var i, rc int32 var savedSuppErr Tu8 var _ /* nc at bp+0 */ TNameContext _, _, _, _, _ = db, i, pEList, rc, savedSuppErr /* Saved value of db->suppressErr */ _ = libc.Int32FromInt32(0) pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList /* Resolve all names in the ORDER BY term expression */ libc.Xmemset(tls, bp, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp))).FpParse = pParse (*(*TNameContext)(unsafe.Pointer(bp))).FpSrcList = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc *(*uintptr)(unsafe.Pointer(bp + 8)) = pEList (*(*TNameContext)(unsafe.Pointer(bp))).FncFlags = libc.Int32FromInt32(NC_AllowAgg) | libc.Int32FromInt32(NC_UEList) | libc.Int32FromInt32(NC_NoSelect) (*(*TNameContext)(unsafe.Pointer(bp))).FnNcErr = 0 db = (*TParse)(unsafe.Pointer(pParse)).Fdb savedSuppErr = (*Tsqlite3)(unsafe.Pointer(db)).FsuppressErr (*Tsqlite3)(unsafe.Pointer(db)).FsuppressErr = uint8(1) rc = _sqlite3ResolveExprNames(tls, bp, pE) (*Tsqlite3)(unsafe.Pointer(db)).FsuppressErr = savedSuppErr if rc != 0 { return 0 } /* Try to match the ORDER BY expression against an expression ** in the result set. Return an 1-based index of the matching ** result-set entry. */ i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } if _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FpExpr, pE, -int32(1)) < int32(2) { return i + int32(1) } goto _1 _1: ; i++ } /* If no match, return 0. */ return 0 } // C documentation // // /* // ** Generate an ORDER BY or GROUP BY term out-of-range error. // */ func _resolveOutOfRangeError(tls *libc.TLS, pParse uintptr, zType uintptr, i int32, mx int32, pError uintptr) { bp := tls.Alloc(32) defer tls.Free(32) _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7277, libc.VaList(bp+8, i, zType, mx)) _sqlite3RecordErrorOffsetOfExpr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pError) } // C documentation // // /* // ** Analyze the ORDER BY clause in a compound SELECT statement. Modify // ** each term of the ORDER BY clause is a constant integer between 1 // ** and N where N is the number of columns in the compound SELECT. // ** // ** ORDER BY terms that are already an integer between 1 and N are // ** unmodified. ORDER BY terms that are integers outside the range of // ** 1 through N generate an error. ORDER BY terms that are expressions // ** are matched against result set expressions of compound SELECT // ** beginning with the left-most SELECT and working toward the right. // ** At the first match, the ORDER BY expression is transformed into // ** the integer column number. // ** // ** Return the number of errors seen. // */ func _resolveCompoundOrderBy(tls *libc.TLS, pParse uintptr, pSelect uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var db, pDup, pE, pEList, pItem, pNew, pOrderBy, pParent uintptr var i, moreToDo int32 var _ /* iCol at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _ = db, i, moreToDo, pDup, pE, pEList, pItem, pNew, pOrderBy, pParent moreToDo = int32(1) pOrderBy = (*TSelect)(unsafe.Pointer(pSelect)).FpOrderBy if pOrderBy == uintptr(0) { return 0 } db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr > *(*int32)(unsafe.Pointer(db + 120 + 2*4)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7333, 0) return int32(1) } i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) { break } libc.SetBitFieldPtr16Uint32(pOrderBy+8+uintptr(i)*20+8+4, libc.Uint32FromInt32(0), 2, 0x4) goto _1 _1: ; i++ } (*TSelect)(unsafe.Pointer(pSelect)).FpNext = uintptr(0) for (*TSelect)(unsafe.Pointer(pSelect)).FpPrior != 0 { (*TSelect)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpPrior)).FpNext = pSelect pSelect = (*TSelect)(unsafe.Pointer(pSelect)).FpPrior } for pSelect != 0 && moreToDo != 0 { moreToDo = 0 pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList _ = libc.Int32FromInt32(0) i = 0 pItem = pOrderBy + 8 for { if !(i < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) { break } *(*int32)(unsafe.Pointer(bp)) = -int32(1) if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 8 + 4))&0x4>>2)) != 0 { goto _2 } pE = _sqlite3ExprSkipCollateAndLikely(tls, (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr) if pE == uintptr(0) { goto _2 } if _sqlite3ExprIsInteger(tls, pE, bp) != 0 { if *(*int32)(unsafe.Pointer(bp)) <= 0 || *(*int32)(unsafe.Pointer(bp)) > (*TExprList)(unsafe.Pointer(pEList)).FnExpr { _resolveOutOfRangeError(tls, pParse, __ccgo_ts+7367, i+int32(1), (*TExprList)(unsafe.Pointer(pEList)).FnExpr, pE) return int32(1) } } else { *(*int32)(unsafe.Pointer(bp)) = _resolveAsName(tls, pParse, pEList, pE) if *(*int32)(unsafe.Pointer(bp)) == 0 { /* Now test if expression pE matches one of the values returned ** by pSelect. In the usual case this is done by duplicating the ** expression, resolving any symbols in it, and then comparing ** it against each expression returned by the SELECT statement. ** Once the comparisons are finished, the duplicate expression ** is deleted. ** ** If this is running as part of an ALTER TABLE operation and ** the symbols resolve successfully, also resolve the symbols in the ** actual expression. This allows the code in alter.c to modify ** column references within the ORDER BY expression as required. */ pDup = _sqlite3ExprDup(tls, db, pE, 0) if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp)) = _resolveOrderByTermToExprList(tls, pParse, pSelect, pDup) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && *(*int32)(unsafe.Pointer(bp)) > 0 { _resolveOrderByTermToExprList(tls, pParse, pSelect, pE) } } _sqlite3ExprDelete(tls, db, pDup) } } if *(*int32)(unsafe.Pointer(bp)) > 0 { /* Convert the ORDER BY term into an integer column number iCol, ** taking care to preserve the COLLATE clause if it exists. */ if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) { pNew = _sqlite3Expr(tls, db, int32(TK_INTEGER), uintptr(0)) if pNew == uintptr(0) { return int32(1) } *(*Tu32)(unsafe.Pointer(pNew + 4)) |= uint32(EP_IntValue) *(*int32)(unsafe.Pointer(pNew + 8)) = *(*int32)(unsafe.Pointer(bp)) if (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr == pE { (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr = pNew } else { pParent = (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr _ = libc.Int32FromInt32(0) for int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pParent)).FpLeft)).Fop) == int32(TK_COLLATE) { pParent = (*TExpr)(unsafe.Pointer(pParent)).FpLeft } _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer(pParent)).FpLeft = pNew } _sqlite3ExprDelete(tls, db, pE) (*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem + 16))).FiOrderByCol = uint16(*(*int32)(unsafe.Pointer(bp))) } libc.SetBitFieldPtr16Uint32(pItem+8+4, libc.Uint32FromInt32(1), 2, 0x4) } else { moreToDo = int32(1) } goto _2 _2: ; i++ pItem += 20 } pSelect = (*TSelect)(unsafe.Pointer(pSelect)).FpNext } i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) { break } if int32(uint32(*(*uint16)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20 + 8 + 4))&0x4>>2)) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7373, libc.VaList(bp+16, i+int32(1))) return int32(1) } goto _3 _3: ; i++ } return 0 } // C documentation // // /* // ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of // ** the SELECT statement pSelect. If any term is reference to a // ** result set expression (as determined by the ExprList.a.u.x.iOrderByCol // ** field) then convert that term into a copy of the corresponding result set // ** column. // ** // ** If any errors are detected, add an error message to pParse and // ** return non-zero. Return zero if no errors are seen. // */ func _sqlite3ResolveOrderGroupBy(tls *libc.TLS, pParse uintptr, pSelect uintptr, pOrderBy uintptr, zType uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var db, pEList, pItem uintptr var i int32 _, _, _, _ = db, i, pEList, pItem db = (*TParse)(unsafe.Pointer(pParse)).Fdb if pOrderBy == uintptr(0) || (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 || int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { return 0 } if (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr > *(*int32)(unsafe.Pointer(db + 120 + 2*4)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7434, libc.VaList(bp+8, zType)) return int32(1) } pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList _ = libc.Int32FromInt32(0) /* sqlite3SelectNew() guarantees this */ i = 0 pItem = pOrderBy + 8 for { if !(i < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) { break } if (*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem + 16))).FiOrderByCol != 0 { if int32((*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem + 16))).FiOrderByCol) > (*TExprList)(unsafe.Pointer(pEList)).FnExpr { _resolveOutOfRangeError(tls, pParse, zType, i+int32(1), (*TExprList)(unsafe.Pointer(pEList)).FnExpr, uintptr(0)) return int32(1) } _resolveAlias(tls, pParse, pEList, int32((*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem + 16))).FiOrderByCol)-int32(1), (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr, 0) } goto _1 _1: ; i++ pItem += 20 } return 0 } // C documentation // // /* // ** Walker callback for windowRemoveExprFromSelect(). // */ func _resolveRemoveWindowsCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var pWin uintptr _ = pWin _ = pWalker if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { pWin = *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)) _sqlite3WindowUnlinkFromSelect(tls, pWin) } return WRC_Continue } // C documentation // // /* // ** Remove any Window objects owned by the expression pExpr from the // ** Select.pWin list of Select object pSelect. // */ func _windowRemoveExprFromSelect(tls *libc.TLS, pSelect uintptr, pExpr uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* sWalker at bp+0 */ TWalker if (*TSelect)(unsafe.Pointer(pSelect)).FpWin != 0 { libc.Xmemset(tls, bp, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_resolveRemoveWindowsCb) *(*uintptr)(unsafe.Pointer(bp + 24)) = pSelect _sqlite3WalkExpr(tls, bp, pExpr) } } // C documentation // // /* // ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect. // ** The Name context of the SELECT statement is pNC. zType is either // ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is. // ** // ** This routine resolves each term of the clause into an expression. // ** If the order-by term is an integer I between 1 and N (where N is the // ** number of columns in the result set of the SELECT) then the expression // ** in the resolution is a copy of the I-th result-set expression. If // ** the order-by term is an identifier that corresponds to the AS-name of // ** a result-set expression, then the term resolves to a copy of the // ** result-set expression. Otherwise, the expression is resolved in // ** the usual way - using sqlite3ResolveExprNames(). // ** // ** This routine returns the number of errors. If errors occur, then // ** an appropriate error message might be left in pParse. (OOM errors // ** excepted.) // */ func _resolveOrderGroupBy(tls *libc.TLS, pNC uintptr, pSelect uintptr, pOrderBy uintptr, zType uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, j, nResult int32 var pE, pE2, pItem, pParse uintptr var _ /* iCol at bp+0 */ int32 _, _, _, _, _, _, _ = i, j, nResult, pE, pE2, pItem, pParse /* Number of terms in the result set */ _ = libc.Int32FromInt32(0) nResult = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpEList)).FnExpr pParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse i = 0 pItem = pOrderBy + 8 for { if !(i < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) { break } pE = (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr pE2 = _sqlite3ExprSkipCollateAndLikely(tls, pE) if pE2 == uintptr(0) { goto _1 } if int32(*(*uint8)(unsafe.Pointer(zType))) != int32('G') { *(*int32)(unsafe.Pointer(bp)) = _resolveAsName(tls, pParse, (*TSelect)(unsafe.Pointer(pSelect)).FpEList, pE2) if *(*int32)(unsafe.Pointer(bp)) > 0 { /* If an AS-name match is found, mark this ORDER BY column as being ** a copy of the iCol-th result-set column. The subsequent call to ** sqlite3ResolveOrderGroupBy() will convert the expression to a ** copy of the iCol-th result-set expression. */ (*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem + 16))).FiOrderByCol = uint16(*(*int32)(unsafe.Pointer(bp))) goto _1 } } if _sqlite3ExprIsInteger(tls, pE2, bp) != 0 { /* The ORDER BY term is an integer constant. Again, set the column ** number so that sqlite3ResolveOrderGroupBy() will convert the ** order-by term to a copy of the result-set expression */ if *(*int32)(unsafe.Pointer(bp)) < int32(1) || *(*int32)(unsafe.Pointer(bp)) > int32(0xffff) { _resolveOutOfRangeError(tls, pParse, zType, i+int32(1), nResult, pE2) return int32(1) } (*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem + 16))).FiOrderByCol = uint16(*(*int32)(unsafe.Pointer(bp))) goto _1 } /* Otherwise, treat the ORDER BY term as an ordinary expression */ (*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem + 16))).FiOrderByCol = uint16(0) if _sqlite3ResolveExprNames(tls, pNC, pE) != 0 { return int32(1) } j = 0 for { if !(j < (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpEList)).FnExpr) { break } if _sqlite3ExprCompare(tls, uintptr(0), pE, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpEList + 8 + uintptr(j)*20))).FpExpr, -int32(1)) == 0 { /* Since this expression is being changed into a reference ** to an identical expression in the result set, remove all Window ** objects belonging to the expression from the Select.pWin list. */ _windowRemoveExprFromSelect(tls, pSelect, pE) (*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem + 16))).FiOrderByCol = uint16(j + int32(1)) } goto _2 _2: ; j++ } goto _1 _1: ; i++ pItem += 20 } return _sqlite3ResolveOrderGroupBy(tls, pParse, pSelect, pOrderBy, zType) } // C documentation // // /* // ** Resolve names in the SELECT statement p and all of its descendants. // */ func _resolveSelectStep(tls *libc.TLS, pWalker uintptr, p uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var db, pGroupBy, pItem, pItem1, pItem2, pLeftmost, pOuterNC, pParse, pSub, pSub1, pWin, zSavedContext uintptr var i, isCompound, nCompound, nRef, v1, v3 int32 var _ /* sNC at bp+0 */ TNameContext _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, i, isCompound, nCompound, nRef, pGroupBy, pItem, pItem1, pItem2, pLeftmost, pOuterNC, pParse, pSub, pSub1, pWin, zSavedContext, v1, v3 /* Database connection */ _ = libc.Int32FromInt32(0) if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Resolved) != 0 { return int32(WRC_Prune) } pOuterNC = *(*uintptr)(unsafe.Pointer(pWalker + 24)) pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Normally sqlite3SelectExpand() will be called first and will have ** already expanded this SELECT. However, if this is a subquery within ** an expression, sqlite3ResolveExprNames() will be called without a ** prior call to sqlite3SelectExpand(). When that happens, let ** sqlite3SelectPrep() do all of the processing for this SELECT. ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and ** this routine in the correct order. */ if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Expanded) == uint32(0) { _sqlite3SelectPrep(tls, pParse, p, pOuterNC) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { v1 = int32(WRC_Abort) } else { v1 = int32(WRC_Prune) } return v1 } isCompound = libc.BoolInt32((*TSelect)(unsafe.Pointer(p)).FpPrior != uintptr(0)) nCompound = 0 pLeftmost = p for p != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(SF_Resolved) /* Resolve the expressions in the LIMIT and OFFSET clauses. These ** are not allowed to refer to any names, so pass an empty NameContext. */ libc.Xmemset(tls, bp, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp))).FpParse = pParse (*(*TNameContext)(unsafe.Pointer(bp))).FpWinSelect = p if _sqlite3ResolveExprNames(tls, bp, (*TSelect)(unsafe.Pointer(p)).FpLimit) != 0 { return int32(WRC_Abort) } /* If the SF_Converted flags is set, then this Select object was ** was created by the convertCompoundSelectToSubquery() function. ** In this case the ORDER BY clause (p->pOrderBy) should be resolved ** as if it were part of the sub-query, not the parent. This block ** moves the pOrderBy down to the sub-query. It will be moved back ** after the names have been resolved. */ if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Converted) != 0 { pSub = (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpSelect _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy (*TSelect)(unsafe.Pointer(p)).FpOrderBy = uintptr(0) } /* Recursively resolve names in all subqueries in the FROM clause */ if pOuterNC != 0 { (*TNameContext)(unsafe.Pointer(pOuterNC)).FnNestedSelect++ } i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc)).FnSrc) { break } pItem = (*TSelect)(unsafe.Pointer(p)).FpSrc + 8 + uintptr(i)*72 if (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect != 0 && (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpSelect)).FselFlags&uint32(SF_Resolved) == uint32(0) { if pOuterNC != 0 { v3 = (*TNameContext)(unsafe.Pointer(pOuterNC)).FnRef } else { v3 = 0 } nRef = v3 zSavedContext = (*TParse)(unsafe.Pointer(pParse)).FzAuthContext if (*TSrcItem)(unsafe.Pointer(pItem)).FzName != 0 { (*TParse)(unsafe.Pointer(pParse)).FzAuthContext = (*TSrcItem)(unsafe.Pointer(pItem)).FzName } _sqlite3ResolveSelectNames(tls, pParse, (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect, pOuterNC) (*TParse)(unsafe.Pointer(pParse)).FzAuthContext = zSavedContext if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return int32(WRC_Abort) } _ = libc.Int32FromInt32(0) /* If the number of references to the outer context changed when ** expressions in the sub-select were resolved, the sub-select ** is correlated. It is not required to check the refcount on any ** but the innermost outer context object, as lookupName() increments ** the refcount on all contexts between the current one and the ** context containing the column when it resolves a name. */ if pOuterNC != 0 { _ = libc.Int32FromInt32(0) libc.SetBitFieldPtr16Uint32(pItem+36+4, libc.BoolUint32((*TNameContext)(unsafe.Pointer(pOuterNC)).FnRef > nRef), 3, 0x8) } } goto _2 _2: ; i++ } if pOuterNC != 0 && (*TNameContext)(unsafe.Pointer(pOuterNC)).FnNestedSelect > uint32(0) { (*TNameContext)(unsafe.Pointer(pOuterNC)).FnNestedSelect-- } /* Set up the local name-context to pass to sqlite3ResolveExprNames() to ** resolve the result-set expression list. */ (*(*TNameContext)(unsafe.Pointer(bp))).FncFlags = libc.Int32FromInt32(NC_AllowAgg) | libc.Int32FromInt32(NC_AllowWin) (*(*TNameContext)(unsafe.Pointer(bp))).FpSrcList = (*TSelect)(unsafe.Pointer(p)).FpSrc (*(*TNameContext)(unsafe.Pointer(bp))).FpNext = pOuterNC /* Resolve names in the result set. */ if _sqlite3ResolveExprListNames(tls, bp, (*TSelect)(unsafe.Pointer(p)).FpEList) != 0 { return int32(WRC_Abort) } (*(*TNameContext)(unsafe.Pointer(bp))).FncFlags &= ^libc.Int32FromInt32(NC_AllowWin) /* If there are no aggregate functions in the result-set, and no GROUP BY ** expression, do not allow aggregates in any of the other expressions. */ _ = libc.Int32FromInt32(0) pGroupBy = (*TSelect)(unsafe.Pointer(p)).FpGroupBy if pGroupBy != 0 || (*(*TNameContext)(unsafe.Pointer(bp))).FncFlags&int32(NC_HasAgg) != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(int32(SF_Aggregate) | (*(*TNameContext)(unsafe.Pointer(bp))).FncFlags&(libc.Int32FromInt32(NC_MinMaxAgg)|libc.Int32FromInt32(NC_OrderAgg))) } else { (*(*TNameContext)(unsafe.Pointer(bp))).FncFlags &= ^libc.Int32FromInt32(NC_AllowAgg) } /* Add the output column list to the name-context before parsing the ** other expressions in the SELECT statement. This is so that ** expressions in the WHERE clause (etc.) can refer to expressions by ** aliases in the result set. ** ** Minor point: If this is the case, then the expression will be ** re-evaluated for each reference to it. */ _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(bp + 8)) = (*TSelect)(unsafe.Pointer(p)).FpEList (*(*TNameContext)(unsafe.Pointer(bp))).FncFlags |= int32(NC_UEList) if (*TSelect)(unsafe.Pointer(p)).FpHaving != 0 { if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Aggregate) == uint32(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7465, 0) return int32(WRC_Abort) } if _sqlite3ResolveExprNames(tls, bp, (*TSelect)(unsafe.Pointer(p)).FpHaving) != 0 { return int32(WRC_Abort) } } (*(*TNameContext)(unsafe.Pointer(bp))).FncFlags |= int32(NC_Where) if _sqlite3ResolveExprNames(tls, bp, (*TSelect)(unsafe.Pointer(p)).FpWhere) != 0 { return int32(WRC_Abort) } (*(*TNameContext)(unsafe.Pointer(bp))).FncFlags &= ^libc.Int32FromInt32(NC_Where) /* Resolve names in table-valued-function arguments */ i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc)).FnSrc) { break } pItem1 = (*TSelect)(unsafe.Pointer(p)).FpSrc + 8 + uintptr(i)*72 if int32(uint32(*(*uint16)(unsafe.Pointer(pItem1 + 36 + 4))&0x4>>2)) != 0 && _sqlite3ResolveExprListNames(tls, bp, *(*uintptr)(unsafe.Pointer(pItem1 + 64))) != 0 { return int32(WRC_Abort) } goto _4 _4: ; i++ } if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { pWin = (*TSelect)(unsafe.Pointer(p)).FpWinDefn for { if !(pWin != 0) { break } if _sqlite3ResolveExprListNames(tls, bp, (*TWindow)(unsafe.Pointer(pWin)).FpOrderBy) != 0 || _sqlite3ResolveExprListNames(tls, bp, (*TWindow)(unsafe.Pointer(pWin)).FpPartition) != 0 { return int32(WRC_Abort) } goto _5 _5: ; pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin } } /* The ORDER BY and GROUP BY clauses may not refer to terms in ** outer queries */ (*(*TNameContext)(unsafe.Pointer(bp))).FpNext = uintptr(0) (*(*TNameContext)(unsafe.Pointer(bp))).FncFlags |= libc.Int32FromInt32(NC_AllowAgg) | libc.Int32FromInt32(NC_AllowWin) /* If this is a converted compound query, move the ORDER BY clause from ** the sub-query back to the parent query. At this point each term ** within the ORDER BY clause has been transformed to an integer value. ** These integers will be replaced by copies of the corresponding result ** set expressions by the call to resolveOrderGroupBy() below. */ if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Converted) != 0 { pSub1 = (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpSelect (*TSelect)(unsafe.Pointer(p)).FpOrderBy = (*TSelect)(unsafe.Pointer(pSub1)).FpOrderBy (*TSelect)(unsafe.Pointer(pSub1)).FpOrderBy = uintptr(0) } /* Process the ORDER BY clause for singleton SELECT statements. ** The ORDER BY clause for compounds SELECT statements is handled ** below, after all of the result-sets for all of the elements of ** the compound have been resolved. ** ** If there is an ORDER BY clause on a term of a compound-select other ** than the right-most term, then that is a syntax error. But the error ** is not detected until much later, and so we need to go ahead and ** resolve those symbols on the incorrect ORDER BY for consistency. */ if (*TSelect)(unsafe.Pointer(p)).FpOrderBy != uintptr(0) && isCompound <= nCompound && _resolveOrderGroupBy(tls, bp, p, (*TSelect)(unsafe.Pointer(p)).FpOrderBy, __ccgo_ts+7367) != 0 { return int32(WRC_Abort) } if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return int32(WRC_Abort) } (*(*TNameContext)(unsafe.Pointer(bp))).FncFlags &= ^libc.Int32FromInt32(NC_AllowWin) /* Resolve the GROUP BY clause. At the same time, make sure ** the GROUP BY clause does not contain aggregate functions. */ if pGroupBy != 0 { if _resolveOrderGroupBy(tls, bp, p, pGroupBy, __ccgo_ts+7504) != 0 || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return int32(WRC_Abort) } i = 0 pItem2 = pGroupBy + 8 for { if !(i < (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr) { break } if (*TExpr)(unsafe.Pointer((*TExprList_item)(unsafe.Pointer(pItem2)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Agg)) != uint32(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7510, 0) return int32(WRC_Abort) } goto _6 _6: ; i++ pItem2 += 20 } } /* If this is part of a compound SELECT, check that it has the right ** number of expressions in the select list. */ if (*TSelect)(unsafe.Pointer(p)).FpNext != 0 && (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr != (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpNext)).FpEList)).FnExpr { _sqlite3SelectWrongNumTermsError(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpNext) return int32(WRC_Abort) } /* Advance to the next term of the compound */ p = (*TSelect)(unsafe.Pointer(p)).FpPrior nCompound++ } /* Resolve the ORDER BY on a compound SELECT after all terms of ** the compound have been resolved. */ if isCompound != 0 && _resolveCompoundOrderBy(tls, pParse, pLeftmost) != 0 { return int32(WRC_Abort) } return int32(WRC_Prune) } // C documentation // // /* // ** This routine walks an expression tree and resolves references to // ** table columns and result-set columns. At the same time, do error // ** checking on function usage and set a flag if any aggregate functions // ** are seen. // ** // ** To resolve table columns references we look for nodes (or subtrees) of the // ** form X.Y.Z or Y.Z or just Z where // ** // ** X: The name of a database. Ex: "main" or "temp" or // ** the symbolic name assigned to an ATTACH-ed database. // ** // ** Y: The name of a table in a FROM clause. Or in a trigger // ** one of the special names "old" or "new". // ** // ** Z: The name of a column in table Y. // ** // ** The node at the root of the subtree is modified as follows: // ** // ** Expr.op Changed to TK_COLUMN // ** Expr.pTab Points to the Table object for X.Y // ** Expr.iColumn The column index in X.Y. -1 for the rowid. // ** Expr.iTable The VDBE cursor number for X.Y // ** // ** // ** To resolve result-set references, look for expression nodes of the // ** form Z (with no X and Y prefix) where the Z matches the right-hand // ** size of an AS clause in the result-set of a SELECT. The Z expression // ** is replaced by a copy of the left-hand side of the result-set expression. // ** Table-name and function resolution occurs on the substituted expression // ** tree. For example, in: // ** // ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x; // ** // ** The "x" term of the order by is replaced by "a+b" to render: // ** // ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b; // ** // ** Function calls are checked to make sure that the function is // ** defined and that the correct number of arguments are specified. // ** If the function is an aggregate function, then the NC_HasAgg flag is // ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION. // ** If an expression contains aggregate functions then the EP_Agg // ** property on the expression is set. // ** // ** An error message is left in pParse if anything is amiss. The number // ** if errors is returned. // */ func _sqlite3ResolveExprNames(tls *libc.TLS, pNC uintptr, pExpr uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var savedHasAgg int32 var v1 uintptr var _ /* w at bp+0 */ TWalker _, _ = savedHasAgg, v1 if pExpr == uintptr(0) { return SQLITE_OK } savedHasAgg = (*TNameContext)(unsafe.Pointer(pNC)).FncFlags & (libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_MinMaxAgg) | libc.Int32FromInt32(NC_HasWin) | libc.Int32FromInt32(NC_OrderAgg)) *(*int32)(unsafe.Pointer(pNC + 24)) &= ^(libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_MinMaxAgg) | libc.Int32FromInt32(NC_HasWin) | libc.Int32FromInt32(NC_OrderAgg)) (*(*TWalker)(unsafe.Pointer(bp))).FpParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_resolveExprStep) if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_NoSelect) != 0 { v1 = uintptr(0) } else { v1 = __ccgo_fp(_resolveSelectStep) } (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = v1 (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback2 = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 24)) = pNC *(*int32)(unsafe.Pointer((*(*TWalker)(unsafe.Pointer(bp))).FpParse + 228)) += (*TExpr)(unsafe.Pointer(pExpr)).FnHeight if _sqlite3ExprCheckHeight(tls, (*(*TWalker)(unsafe.Pointer(bp))).FpParse, (*TParse)(unsafe.Pointer((*(*TWalker)(unsafe.Pointer(bp))).FpParse)).FnHeight) != 0 { return int32(SQLITE_ERROR) } _ = libc.Int32FromInt32(0) _sqlite3WalkExprNN(tls, bp, pExpr) *(*int32)(unsafe.Pointer((*(*TWalker)(unsafe.Pointer(bp))).FpParse + 228)) -= (*TExpr)(unsafe.Pointer(pExpr)).FnHeight _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32((*TNameContext)(unsafe.Pointer(pNC)).FncFlags & (libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_HasWin))) *(*int32)(unsafe.Pointer(pNC + 24)) |= savedHasAgg return libc.BoolInt32((*TNameContext)(unsafe.Pointer(pNC)).FnNcErr > 0 || (*TParse)(unsafe.Pointer((*(*TWalker)(unsafe.Pointer(bp))).FpParse)).FnErr > 0) } // C documentation // // /* // ** Resolve all names for all expression in an expression list. This is // ** just like sqlite3ResolveExprNames() except that it works for an expression // ** list rather than a single expression. // */ func _sqlite3ResolveExprListNames(tls *libc.TLS, pNC uintptr, pList uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var i, savedHasAgg int32 var pExpr uintptr var _ /* w at bp+0 */ TWalker _, _, _ = i, pExpr, savedHasAgg savedHasAgg = 0 if pList == uintptr(0) { return WRC_Continue } (*(*TWalker)(unsafe.Pointer(bp))).FpParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_resolveExprStep) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_resolveSelectStep) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback2 = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 24)) = pNC savedHasAgg = (*TNameContext)(unsafe.Pointer(pNC)).FncFlags & (libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_MinMaxAgg) | libc.Int32FromInt32(NC_HasWin) | libc.Int32FromInt32(NC_OrderAgg)) *(*int32)(unsafe.Pointer(pNC + 24)) &= ^(libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_MinMaxAgg) | libc.Int32FromInt32(NC_HasWin) | libc.Int32FromInt32(NC_OrderAgg)) i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } pExpr = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr if pExpr == uintptr(0) { goto _1 } *(*int32)(unsafe.Pointer((*(*TWalker)(unsafe.Pointer(bp))).FpParse + 228)) += (*TExpr)(unsafe.Pointer(pExpr)).FnHeight if _sqlite3ExprCheckHeight(tls, (*(*TWalker)(unsafe.Pointer(bp))).FpParse, (*TParse)(unsafe.Pointer((*(*TWalker)(unsafe.Pointer(bp))).FpParse)).FnHeight) != 0 { return int32(WRC_Abort) } _sqlite3WalkExprNN(tls, bp, pExpr) *(*int32)(unsafe.Pointer((*(*TWalker)(unsafe.Pointer(bp))).FpParse + 228)) -= (*TExpr)(unsafe.Pointer(pExpr)).FnHeight _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&(libc.Int32FromInt32(NC_HasAgg)|libc.Int32FromInt32(NC_MinMaxAgg)|libc.Int32FromInt32(NC_HasWin)|libc.Int32FromInt32(NC_OrderAgg)) != 0 { *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32((*TNameContext)(unsafe.Pointer(pNC)).FncFlags & (libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_HasWin))) savedHasAgg |= (*TNameContext)(unsafe.Pointer(pNC)).FncFlags & (libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_MinMaxAgg) | libc.Int32FromInt32(NC_HasWin) | libc.Int32FromInt32(NC_OrderAgg)) *(*int32)(unsafe.Pointer(pNC + 24)) &= ^(libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_MinMaxAgg) | libc.Int32FromInt32(NC_HasWin) | libc.Int32FromInt32(NC_OrderAgg)) } if (*TParse)(unsafe.Pointer((*(*TWalker)(unsafe.Pointer(bp))).FpParse)).FnErr > 0 { return int32(WRC_Abort) } goto _1 _1: ; i++ } *(*int32)(unsafe.Pointer(pNC + 24)) |= savedHasAgg return WRC_Continue } // C documentation // // /* // ** Resolve all names in all expressions of a SELECT and in all // ** descendants of the SELECT, including compounds off of p->pPrior, // ** subqueries in expressions, and subqueries used as FROM clause // ** terms. // ** // ** See sqlite3ResolveExprNames() for a description of the kinds of // ** transformations that occur. // ** // ** All SELECT statements should have been expanded using // ** sqlite3SelectExpand() prior to invoking this routine. // */ func _sqlite3ResolveSelectNames(tls *libc.TLS, pParse uintptr, p uintptr, pOuterNC uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* w at bp+0 */ TWalker _ = libc.Int32FromInt32(0) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_resolveExprStep) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_resolveSelectStep) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback2 = uintptr(0) (*(*TWalker)(unsafe.Pointer(bp))).FpParse = pParse *(*uintptr)(unsafe.Pointer(bp + 24)) = pOuterNC _sqlite3WalkSelect(tls, bp, p) } // C documentation // // /* // ** Resolve names in expressions that can only reference a single table // ** or which cannot reference any tables at all. Examples: // ** // ** "type" flag // ** ------------ // ** (1) CHECK constraints NC_IsCheck // ** (2) WHERE clauses on partial indices NC_PartIdx // ** (3) Expressions in indexes on expressions NC_IdxExpr // ** (4) Expression arguments to VACUUM INTO. 0 // ** (5) GENERATED ALWAYS as expressions NC_GenCol // ** // ** In all cases except (4), the Expr.iTable value for Expr.op==TK_COLUMN // ** nodes of the expression is set to -1 and the Expr.iColumn value is // ** set to the column number. In case (4), TK_COLUMN nodes cause an error. // ** // ** Any errors cause an error message to be set in pParse. // */ func _sqlite3ResolveSelfReference(tls *libc.TLS, pParse uintptr, pTab uintptr, type1 int32, pExpr uintptr, pList uintptr) (r int32) { bp := tls.Alloc(128) defer tls.Free(128) var rc, v1 int32 var _ /* sNC at bp+80 */ TNameContext var _ /* sSrc at bp+0 */ TSrcList _, _ = rc, v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) libc.Xmemset(tls, bp+80, 0, uint32(36)) libc.Xmemset(tls, bp, 0, uint32(80)) if pTab != 0 { (*(*TSrcList)(unsafe.Pointer(bp))).FnSrc = int32(1) (*(*TSrcItem)(unsafe.Pointer(bp + 8))).FzName = (*TTable)(unsafe.Pointer(pTab)).FzName (*(*TSrcItem)(unsafe.Pointer(bp + 8))).FpTab = pTab (*(*TSrcItem)(unsafe.Pointer(bp + 8))).FiCursor = -int32(1) if (*TTable)(unsafe.Pointer(pTab)).FpSchema != (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + 1*16))).FpSchema { /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP ** schema elements */ type1 |= int32(NC_FromDDL) } } (*(*TNameContext)(unsafe.Pointer(bp + 80))).FpParse = pParse (*(*TNameContext)(unsafe.Pointer(bp + 80))).FpSrcList = bp (*(*TNameContext)(unsafe.Pointer(bp + 80))).FncFlags = type1 | int32(NC_IsDDL) v1 = _sqlite3ResolveExprNames(tls, bp+80, pExpr) rc = v1 if v1 != SQLITE_OK { return rc } if pList != 0 { rc = _sqlite3ResolveExprListNames(tls, bp+80, pList) } return rc } // C documentation // // /* // ** Return the affinity character for a single column of a table. // */ func _sqlite3TableColumnAffinity(tls *libc.TLS, pTab uintptr, iCol int32) (r uint8) { if iCol < 0 || iCol >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { return uint8(SQLITE_AFF_INTEGER) } return (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).Faffinity } // C documentation // // /* // ** Return the 'affinity' of the expression pExpr if any. // ** // ** If pExpr is a column, a reference to a column via an 'AS' alias, // ** or a sub-select with a column as the return value, then the // ** affinity of that column is returned. Otherwise, 0x00 is returned, // ** indicating no affinity for the expression. // ** // ** i.e. the WHERE clause expressions in the following statements all // ** have an affinity: // ** // ** CREATE TABLE t1(a); // ** SELECT * FROM t1 WHERE a; // ** SELECT a AS b FROM t1 WHERE b; // ** SELECT * FROM t1 WHERE (select a from t1); // */ func _sqlite3ExprAffinity(tls *libc.TLS, pExpr uintptr) (r uint8) { var op, v1 int32 var v2 bool _, _, _ = op, v1, v2 op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) for int32(1) != 0 { if op == int32(TK_COLUMN) || op == int32(TK_AGG_COLUMN) && (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab != uintptr(0) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return _sqlite3TableColumnAffinity(tls, (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab, int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)) } if op == int32(TK_SELECT) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpEList + 8))).FpExpr) } if op == int32(TK_CAST) { _ = libc.Int32FromInt32(0) return _sqlite3AffinityType(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), uintptr(0)) } if op == int32(TK_SELECT_COLUMN) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft + 20)))).FpEList + 8 + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)*20))).FpExpr) } if op == int32(TK_VECTOR) { _ = libc.Int32FromInt32(0) return _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8))).FpExpr) } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Skip)|libc.Int32FromInt32(EP_IfNullRow)) != uint32(0) { _ = libc.Int32FromInt32(0) pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) continue } if v2 = op != int32(TK_REGISTER); !v2 { v1 = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop2) op = v1 } if v2 || v1 == int32(TK_REGISTER) { break } } return (*TExpr)(unsafe.Pointer(pExpr)).FaffExpr } // C documentation // // /* // ** Make a guess at all the possible datatypes of the result that could // ** be returned by an expression. Return a bitmask indicating the answer: // ** // ** 0x01 Numeric // ** 0x02 Text // ** 0x04 Blob // ** // ** If the expression must return NULL, then 0x00 is returned. // */ func _sqlite3ExprDataType(tls *libc.TLS, pExpr uintptr) (r int32) { var aff, ii, res int32 var pList uintptr _, _, _, _ = aff, ii, pList, res for pExpr != 0 { switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) { case int32(TK_COLLATE): fallthrough case int32(TK_IF_NULL_ROW): fallthrough case int32(TK_UPLUS): pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft case int32(TK_NULL): pExpr = uintptr(0) case int32(TK_STRING): return int32(0x02) case int32(TK_BLOB): return int32(0x04) case int32(TK_CONCAT): return int32(0x06) case int32(TK_VARIABLE): fallthrough case int32(TK_AGG_FUNCTION): fallthrough case int32(TK_FUNCTION): return int32(0x07) case int32(TK_COLUMN): fallthrough case int32(TK_AGG_COLUMN): fallthrough case int32(TK_SELECT): fallthrough case int32(TK_CAST): fallthrough case int32(TK_SELECT_COLUMN): fallthrough case int32(TK_VECTOR): aff = int32(_sqlite3ExprAffinity(tls, pExpr)) if aff >= int32(SQLITE_AFF_NUMERIC) { return int32(0x05) } if aff == int32(SQLITE_AFF_TEXT) { return int32(0x06) } return int32(0x07) case int32(TK_CASE): res = 0 pList = *(*uintptr)(unsafe.Pointer(pExpr + 20)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) ii = int32(1) for { if !(ii < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } res |= _sqlite3ExprDataType(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*20))).FpExpr) goto _1 _1: ; ii += int32(2) } if (*TExprList)(unsafe.Pointer(pList)).FnExpr%int32(2) != 0 { res |= _sqlite3ExprDataType(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr((*TExprList)(unsafe.Pointer(pList)).FnExpr-int32(1))*20))).FpExpr) } return res default: return int32(0x01) } /* End of switch(op) */ } /* End of while(pExpr) */ return 0x00 } // C documentation // // /* // ** Set the collating sequence for expression pExpr to be the collating // ** sequence named by pToken. Return a pointer to a new Expr node that // ** implements the COLLATE operator. // ** // ** If a memory allocation error occurs, that fact is recorded in pParse->db // ** and the pExpr parameter is returned unchanged. // */ func _sqlite3ExprAddCollateToken(tls *libc.TLS, pParse uintptr, pExpr uintptr, pCollName uintptr, dequote int32) (r uintptr) { var pNew uintptr _ = pNew if (*TToken)(unsafe.Pointer(pCollName)).Fn > uint32(0) { pNew = _sqlite3ExprAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_COLLATE), pCollName, dequote) if pNew != 0 { (*TExpr)(unsafe.Pointer(pNew)).FpLeft = pExpr *(*Tu32)(unsafe.Pointer(pNew + 4)) |= uint32(libc.Int32FromInt32(EP_Collate) | libc.Int32FromInt32(EP_Skip)) pExpr = pNew } } return pExpr } func _sqlite3ExprAddCollateString(tls *libc.TLS, pParse uintptr, pExpr uintptr, zC uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* s at bp+0 */ TToken _ = libc.Int32FromInt32(0) _sqlite3TokenInit(tls, bp, zC) return _sqlite3ExprAddCollateToken(tls, pParse, pExpr, bp, 0) } // C documentation // // /* // ** Skip over any TK_COLLATE operators. // */ func _sqlite3ExprSkipCollate(tls *libc.TLS, pExpr uintptr) (r uintptr) { for pExpr != 0 && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Skip)) != uint32(0) { _ = libc.Int32FromInt32(0) pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft } return pExpr } // C documentation // // /* // ** Skip over any TK_COLLATE operators and/or any unlikely() // ** or likelihood() or likely() functions at the root of an // ** expression. // */ func _sqlite3ExprSkipCollateAndLikely(tls *libc.TLS, pExpr uintptr) (r uintptr) { for pExpr != 0 && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Skip)|libc.Int32FromInt32(EP_Unlikely)) != uint32(0) { if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Unlikely)) != uint32(0) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pExpr = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8))).FpExpr } else { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLLATE) { pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft } else { break } } } return pExpr } // C documentation // // /* // ** Return the collation sequence for the expression pExpr. If // ** there is no defined collating sequence, return NULL. // ** // ** See also: sqlite3ExprNNCollSeq() // ** // ** The sqlite3ExprNNCollSeq() works the same exact that it returns the // ** default collation if pExpr has no defined collation. // ** // ** The collating sequence might be determined by a COLLATE operator // ** or by the presence of a column with a defined collating sequence. // ** COLLATE operators take first precedence. Left operands take // ** precedence over right operands. // */ func _sqlite3ExprCollSeq(tls *libc.TLS, pParse uintptr, pExpr uintptr) (r uintptr) { var db, p, pColl, pNext, zColl uintptr var i, j, op, v1 int32 _, _, _, _, _, _, _, _, _ = db, i, j, op, p, pColl, pNext, zColl, v1 db = (*TParse)(unsafe.Pointer(pParse)).Fdb pColl = uintptr(0) p = pExpr for p != 0 { op = int32((*TExpr)(unsafe.Pointer(p)).Fop) if op == int32(TK_REGISTER) { op = int32((*TExpr)(unsafe.Pointer(p)).Fop2) } if op == int32(TK_AGG_COLUMN) && (*TExpr)(unsafe.Pointer(p)).Fy.FpTab != uintptr(0) || op == int32(TK_COLUMN) || op == int32(TK_TRIGGER) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) v1 = int32((*TExpr)(unsafe.Pointer(p)).FiColumn) j = v1 if v1 >= 0 { zColl = _sqlite3ColumnColl(tls, (*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).Fy.FpTab)).FaCol+uintptr(j)*12) pColl = _sqlite3FindCollSeq(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).Fenc, zColl, 0) } break } if op == int32(TK_CAST) || op == int32(TK_UPLUS) { p = (*TExpr)(unsafe.Pointer(p)).FpLeft continue } if op == int32(TK_VECTOR) { _ = libc.Int32FromInt32(0) p = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)) + 8))).FpExpr continue } if op == int32(TK_COLLATE) { _ = libc.Int32FromInt32(0) pColl = _sqlite3GetCollSeq(tls, pParse, (*Tsqlite3)(unsafe.Pointer(db)).Fenc, uintptr(0), *(*uintptr)(unsafe.Pointer(p + 8))) break } if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_Collate) != 0 { if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 && (*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).FpLeft)).Fflags&uint32(EP_Collate) != uint32(0) { p = (*TExpr)(unsafe.Pointer(p)).FpLeft } else { pNext = (*TExpr)(unsafe.Pointer(p)).FpRight /* The Expr.x union is never used at the same time as Expr.pRight */ _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_xIsSelect) == uint32(0) && *(*uintptr)(unsafe.Pointer(p + 20)) != uintptr(0) && !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)))).FnExpr) { break } if (*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)) + 8 + uintptr(i)*20))).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Collate)) != uint32(0) { pNext = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)) + 8 + uintptr(i)*20))).FpExpr break } goto _2 _2: ; i++ } } p = pNext } } else { break } } if _sqlite3CheckCollSeq(tls, pParse, pColl) != 0 { pColl = uintptr(0) } return pColl } // C documentation // // /* // ** Return the collation sequence for the expression pExpr. If // ** there is no defined collating sequence, return a pointer to the // ** default collation sequence. // ** // ** See also: sqlite3ExprCollSeq() // ** // ** The sqlite3ExprCollSeq() routine works the same except that it // ** returns NULL if there is no defined collation. // */ func _sqlite3ExprNNCollSeq(tls *libc.TLS, pParse uintptr, pExpr uintptr) (r uintptr) { var p uintptr _ = p p = _sqlite3ExprCollSeq(tls, pParse, pExpr) if p == uintptr(0) { p = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FpDfltColl } _ = libc.Int32FromInt32(0) return p } // C documentation // // /* // ** Return TRUE if the two expressions have equivalent collating sequences. // */ func _sqlite3ExprCollSeqMatch(tls *libc.TLS, pParse uintptr, pE1 uintptr, pE2 uintptr) (r int32) { var pColl1, pColl2 uintptr _, _ = pColl1, pColl2 pColl1 = _sqlite3ExprNNCollSeq(tls, pParse, pE1) pColl2 = _sqlite3ExprNNCollSeq(tls, pParse, pE2) return libc.BoolInt32(_sqlite3StrICmp(tls, (*TCollSeq)(unsafe.Pointer(pColl1)).FzName, (*TCollSeq)(unsafe.Pointer(pColl2)).FzName) == 0) } // C documentation // // /* // ** pExpr is an operand of a comparison operator. aff2 is the // ** type affinity of the other operand. This routine returns the // ** type affinity that should be used for the comparison operator. // */ func _sqlite3CompareAffinity(tls *libc.TLS, pExpr uintptr, aff2 uint8) (r uint8) { var aff1 uint8 var v1 int32 _, _ = aff1, v1 aff1 = _sqlite3ExprAffinity(tls, pExpr) if int32(aff1) > int32(SQLITE_AFF_NONE) && int32(aff2) > int32(SQLITE_AFF_NONE) { /* Both sides of the comparison are columns. If one has numeric ** affinity, use that. Otherwise use no affinity. */ if int32(aff1) >= int32(SQLITE_AFF_NUMERIC) || int32(aff2) >= int32(SQLITE_AFF_NUMERIC) { return uint8(SQLITE_AFF_NUMERIC) } else { return uint8(SQLITE_AFF_BLOB) } } else { /* One side is a column, the other is not. Use the columns affinity. */ _ = libc.Int32FromInt32(0) if int32(aff1) <= int32(SQLITE_AFF_NONE) { v1 = int32(aff2) } else { v1 = int32(aff1) } return uint8(v1 | int32(SQLITE_AFF_NONE)) } return r } // C documentation // // /* // ** pExpr is a comparison operator. Return the type affinity that should // ** be applied to both operands prior to doing the comparison. // */ func _comparisonAffinity(tls *libc.TLS, pExpr uintptr) (r uint8) { var aff uint8 _ = aff _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) aff = _sqlite3ExprAffinity(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) if (*TExpr)(unsafe.Pointer(pExpr)).FpRight != 0 { aff = _sqlite3CompareAffinity(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, aff) } else { if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) { aff = _sqlite3CompareAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpEList + 8))).FpExpr, aff) } else { if int32(aff) == 0 { aff = uint8(SQLITE_AFF_BLOB) } } } return aff } // C documentation // // /* // ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. // ** idx_affinity is the affinity of an indexed column. Return true // ** if the index with affinity idx_affinity may be used to implement // ** the comparison in pExpr. // */ func _sqlite3IndexAffinityOk(tls *libc.TLS, pExpr uintptr, idx_affinity uint8) (r int32) { var aff uint8 _ = aff aff = _comparisonAffinity(tls, pExpr) if int32(aff) < int32(SQLITE_AFF_TEXT) { return int32(1) } if int32(aff) == int32(SQLITE_AFF_TEXT) { return libc.BoolInt32(int32(idx_affinity) == int32(SQLITE_AFF_TEXT)) } return libc.BoolInt32(int32(idx_affinity) >= int32(SQLITE_AFF_NUMERIC)) } // C documentation // // /* // ** Return the P5 value that should be used for a binary comparison // ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. // */ func _binaryCompareP5(tls *libc.TLS, pExpr1 uintptr, pExpr2 uintptr, jumpIfNull int32) (r Tu8) { var aff Tu8 _ = aff aff = _sqlite3ExprAffinity(tls, pExpr2) aff = uint8(int32(_sqlite3CompareAffinity(tls, pExpr1, aff)) | int32(uint8(jumpIfNull))) return aff } // C documentation // // /* // ** Return a pointer to the collation sequence that should be used by // ** a binary comparison operator comparing pLeft and pRight. // ** // ** If the left hand expression has a collating sequence type, then it is // ** used. Otherwise the collation sequence for the right hand expression // ** is used, or the default (BINARY) if neither expression has a collating // ** type. // ** // ** Argument pRight (but not pLeft) may be a null pointer. In this case, // ** it is not considered. // */ func _sqlite3BinaryCompareCollSeq(tls *libc.TLS, pParse uintptr, pLeft uintptr, pRight uintptr) (r uintptr) { var pColl uintptr _ = pColl _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(pLeft)).Fflags&uint32(EP_Collate) != 0 { pColl = _sqlite3ExprCollSeq(tls, pParse, pLeft) } else { if pRight != 0 && (*TExpr)(unsafe.Pointer(pRight)).Fflags&uint32(EP_Collate) != uint32(0) { pColl = _sqlite3ExprCollSeq(tls, pParse, pRight) } else { pColl = _sqlite3ExprCollSeq(tls, pParse, pLeft) if !(pColl != 0) { pColl = _sqlite3ExprCollSeq(tls, pParse, pRight) } } } return pColl } // C documentation // // /* Expression p is a comparison operator. Return a collation sequence // ** appropriate for the comparison operator. // ** // ** This is normally just a wrapper around sqlite3BinaryCompareCollSeq(). // ** However, if the OP_Commuted flag is set, then the order of the operands // ** is reversed in the sqlite3BinaryCompareCollSeq() call so that the // ** correct collating sequence is found. // */ func _sqlite3ExprCompareCollSeq(tls *libc.TLS, pParse uintptr, p uintptr) (r uintptr) { if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_Commuted)) != uint32(0) { return _sqlite3BinaryCompareCollSeq(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpRight, (*TExpr)(unsafe.Pointer(p)).FpLeft) } else { return _sqlite3BinaryCompareCollSeq(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpLeft, (*TExpr)(unsafe.Pointer(p)).FpRight) } return r } // C documentation // // /* // ** Generate code for a comparison operator. // */ func _codeCompare(tls *libc.TLS, pParse uintptr, pLeft uintptr, pRight uintptr, opcode int32, in1 int32, in2 int32, dest int32, jumpIfNull int32, isCommuted int32) (r int32) { var addr, p5 int32 var p4 uintptr _, _, _ = addr, p4, p5 if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return 0 } if isCommuted != 0 { p4 = _sqlite3BinaryCompareCollSeq(tls, pParse, pRight, pLeft) } else { p4 = _sqlite3BinaryCompareCollSeq(tls, pParse, pLeft, pRight) } p5 = int32(_binaryCompareP5(tls, pLeft, pRight, jumpIfNull)) addr = _sqlite3VdbeAddOp4(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, opcode, in2, dest, in1, p4, -int32(2)) _sqlite3VdbeChangeP5(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, uint16(uint8(p5))) return addr } // C documentation // // /* // ** Return true if expression pExpr is a vector, or false otherwise. // ** // ** A vector is defined as any expression that results in two or more // ** columns of result. Every TK_VECTOR node is an vector because the // ** parser will not generate a TK_VECTOR with fewer than two entries. // ** But a TK_SELECT might be either a vector or a scalar. It is only // ** considered a vector if it has two or more result columns. // */ func _sqlite3ExprIsVector(tls *libc.TLS, pExpr uintptr) (r int32) { return libc.BoolInt32(_sqlite3ExprVectorSize(tls, pExpr) > int32(1)) } // C documentation // // /* // ** If the expression passed as the only argument is of type TK_VECTOR // ** return the number of expressions in the vector. Or, if the expression // ** is a sub-select, return the number of columns in the sub-select. For // ** any other type of expression, return 1. // */ func _sqlite3ExprVectorSize(tls *libc.TLS, pExpr uintptr) (r int32) { var op Tu8 _ = op op = (*TExpr)(unsafe.Pointer(pExpr)).Fop if int32(op) == int32(TK_REGISTER) { op = (*TExpr)(unsafe.Pointer(pExpr)).Fop2 } if int32(op) == int32(TK_VECTOR) { _ = libc.Int32FromInt32(0) return (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr } else { if int32(op) == int32(TK_SELECT) { _ = libc.Int32FromInt32(0) return (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpEList)).FnExpr } else { return int32(1) } } return r } // C documentation // // /* // ** Return a pointer to a subexpression of pVector that is the i-th // ** column of the vector (numbered starting with 0). The caller must // ** ensure that i is within range. // ** // ** If pVector is really a scalar (and "scalar" here includes subqueries // ** that return a single column!) then return pVector unmodified. // ** // ** pVector retains ownership of the returned subexpression. // ** // ** If the vector is a (SELECT ...) then the expression returned is // ** just the expression for the i-th term of the result set, and may // ** not be ready for evaluation because the table cursor has not yet // ** been positioned. // */ func _sqlite3VectorFieldSubexpr(tls *libc.TLS, pVector uintptr, i int32) (r uintptr) { _ = libc.Int32FromInt32(0) if _sqlite3ExprIsVector(tls, pVector) != 0 { _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pVector)).Fop) == int32(TK_SELECT) || int32((*TExpr)(unsafe.Pointer(pVector)).Fop2) == int32(TK_SELECT) { _ = libc.Int32FromInt32(0) return (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pVector + 20)))).FpEList + 8 + uintptr(i)*20))).FpExpr } else { _ = libc.Int32FromInt32(0) return (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pVector + 20)) + 8 + uintptr(i)*20))).FpExpr } } return pVector } // C documentation // // /* // ** Compute and return a new Expr object which when passed to // ** sqlite3ExprCode() will generate all necessary code to compute // ** the iField-th column of the vector expression pVector. // ** // ** It is ok for pVector to be a scalar (as long as iField==0). // ** In that case, this routine works like sqlite3ExprDup(). // ** // ** The caller owns the returned Expr object and is responsible for // ** ensuring that the returned value eventually gets freed. // ** // ** The caller retains ownership of pVector. If pVector is a TK_SELECT, // ** then the returned object will reference pVector and so pVector must remain // ** valid for the life of the returned object. If pVector is a TK_VECTOR // ** or a scalar expression, then it can be deleted as soon as this routine // ** returns. // ** // ** A trick to cause a TK_SELECT pVector to be deleted together with // ** the returned Expr object is to attach the pVector to the pRight field // ** of the returned TK_SELECT_COLUMN Expr object. // */ func _sqlite3ExprForVectorField(tls *libc.TLS, pParse uintptr, pVector uintptr, iField int32, nField int32) (r uintptr) { var pRet, ppVector uintptr _, _ = pRet, ppVector if int32((*TExpr)(unsafe.Pointer(pVector)).Fop) == int32(TK_SELECT) { _ = libc.Int32FromInt32(0) /* The TK_SELECT_COLUMN Expr node: ** ** pLeft: pVector containing TK_SELECT. Not deleted. ** pRight: not used. But recursively deleted. ** iColumn: Index of a column in pVector ** iTable: 0 or the number of columns on the LHS of an assignment ** pLeft->iTable: First in an array of register holding result, or 0 ** if the result is not yet computed. ** ** sqlite3ExprDelete() specifically skips the recursive delete of ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector ** can be attached to pRight to cause this node to take ownership of ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes ** with the same pLeft pointer to the pVector, but only one of them ** will own the pVector. */ pRet = _sqlite3PExpr(tls, pParse, int32(TK_SELECT_COLUMN), uintptr(0), uintptr(0)) if pRet != 0 { *(*Tu32)(unsafe.Pointer(pRet + 4)) |= uint32(libc.Int32FromInt32(EP_FullSize)) (*TExpr)(unsafe.Pointer(pRet)).FiTable = nField (*TExpr)(unsafe.Pointer(pRet)).FiColumn = int16(iField) (*TExpr)(unsafe.Pointer(pRet)).FpLeft = pVector } } else { if int32((*TExpr)(unsafe.Pointer(pVector)).Fop) == int32(TK_VECTOR) { _ = libc.Int32FromInt32(0) ppVector = *(*uintptr)(unsafe.Pointer(pVector + 20)) + 8 + uintptr(iField)*20 pVector = *(*uintptr)(unsafe.Pointer(ppVector)) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { /* This must be a vector UPDATE inside a trigger */ *(*uintptr)(unsafe.Pointer(ppVector)) = uintptr(0) return pVector } } pRet = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pVector, 0) } return pRet } // C documentation // // /* // ** If expression pExpr is of type TK_SELECT, generate code to evaluate // ** it. Return the register in which the result is stored (or, if the // ** sub-select returns more than one column, the first in an array // ** of registers in which the result is stored). // ** // ** If pExpr is not a TK_SELECT expression, return 0. // */ func _exprCodeSubselect(tls *libc.TLS, pParse uintptr, pExpr uintptr) (r int32) { var reg int32 _ = reg reg = 0 if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_SELECT) { reg = _sqlite3CodeSubselect(tls, pParse, pExpr) } return reg } // C documentation // // /* // ** Argument pVector points to a vector expression - either a TK_VECTOR // ** or TK_SELECT that returns more than one column. This function returns // ** the register number of a register that contains the value of // ** element iField of the vector. // ** // ** If pVector is a TK_SELECT expression, then code for it must have // ** already been generated using the exprCodeSubselect() routine. In this // ** case parameter regSelect should be the first in an array of registers // ** containing the results of the sub-select. // ** // ** If pVector is of type TK_VECTOR, then code for the requested field // ** is generated. In this case (*pRegFree) may be set to the number of // ** a temporary register to be freed by the caller before returning. // ** // ** Before returning, output parameter (*ppExpr) is set to point to the // ** Expr object corresponding to element iElem of the vector. // */ func _exprVectorRegister(tls *libc.TLS, pParse uintptr, pVector uintptr, iField int32, regSelect int32, ppExpr uintptr, pRegFree uintptr) (r int32) { var op Tu8 _ = op op = (*TExpr)(unsafe.Pointer(pVector)).Fop _ = libc.Int32FromInt32(0) if int32(op) == int32(TK_REGISTER) { *(*uintptr)(unsafe.Pointer(ppExpr)) = _sqlite3VectorFieldSubexpr(tls, pVector, iField) return (*TExpr)(unsafe.Pointer(pVector)).FiTable + iField } if int32(op) == int32(TK_SELECT) { _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(ppExpr)) = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pVector + 20)))).FpEList + 8 + uintptr(iField)*20))).FpExpr return regSelect + iField } if int32(op) == int32(TK_VECTOR) { _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(ppExpr)) = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pVector + 20)) + 8 + uintptr(iField)*20))).FpExpr return _sqlite3ExprCodeTemp(tls, pParse, *(*uintptr)(unsafe.Pointer(ppExpr)), pRegFree) } return 0 } // C documentation // // /* // ** Expression pExpr is a comparison between two vector values. Compute // ** the result of the comparison (1, 0, or NULL) and write that // ** result into register dest. // ** // ** The caller must satisfy the following preconditions: // ** // ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ // ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ // ** otherwise: op==pExpr->op and p5==0 // */ func _codeVectorCompare(tls *libc.TLS, pParse uintptr, pExpr uintptr, dest int32, op Tu8, p5 Tu8) { bp := tls.Alloc(16) defer tls.Free(16) var addrCmp, addrDone, i, isCommuted, nLeft, r1, r2, regLeft, regRight int32 var opx Tu8 var pLeft, pRight, v uintptr var _ /* pL at bp+8 */ uintptr var _ /* pR at bp+12 */ uintptr var _ /* regFree1 at bp+0 */ int32 var _ /* regFree2 at bp+4 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _ = addrCmp, addrDone, i, isCommuted, nLeft, opx, pLeft, pRight, r1, r2, regLeft, regRight, v v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft pRight = (*TExpr)(unsafe.Pointer(pExpr)).FpRight nLeft = _sqlite3ExprVectorSize(tls, pLeft) regLeft = 0 regRight = 0 opx = op addrCmp = 0 addrDone = _sqlite3VdbeMakeLabel(tls, pParse) isCommuted = libc.BoolInt32((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Commuted)) != uint32(0)) _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return } if nLeft != _sqlite3ExprVectorSize(tls, pRight) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+6677, 0) return } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(op) == int32(TK_LE) { opx = uint8(TK_LT) } if int32(op) == int32(TK_GE) { opx = uint8(TK_GT) } if int32(op) == int32(TK_NE) { opx = uint8(TK_EQ) } regLeft = _exprCodeSubselect(tls, pParse, pLeft) regRight = _exprCodeSubselect(tls, pParse, pRight) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), dest) i = 0 for { if !(int32(1) != 0) { break } *(*int32)(unsafe.Pointer(bp)) = 0 *(*int32)(unsafe.Pointer(bp + 4)) = 0 *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) _ = libc.Int32FromInt32(0) if addrCmp != 0 { _sqlite3VdbeJumpHere(tls, v, addrCmp) } r1 = _exprVectorRegister(tls, pParse, pLeft, i, regLeft, bp+8, bp) r2 = _exprVectorRegister(tls, pParse, pRight, i, regRight, bp+12, bp+4) addrCmp = _sqlite3VdbeCurrentAddr(tls, v) _codeCompare(tls, pParse, *(*uintptr)(unsafe.Pointer(bp + 8)), *(*uintptr)(unsafe.Pointer(bp + 12)), int32(opx), r1, r2, addrDone, int32(p5), isCommuted) _sqlite3ReleaseTempReg(tls, pParse, *(*int32)(unsafe.Pointer(bp))) _sqlite3ReleaseTempReg(tls, pParse, *(*int32)(unsafe.Pointer(bp + 4))) if (int32(opx) == int32(TK_LT) || int32(opx) == int32(TK_GT)) && i < nLeft-int32(1) { addrCmp = _sqlite3VdbeAddOp0(tls, v, int32(OP_ElseEq)) } if int32(p5) == int32(SQLITE_NULLEQ) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, dest) } else { _sqlite3VdbeAddOp3(tls, v, int32(OP_ZeroOrNull), r1, dest, r2) } if i == nLeft-int32(1) { break } if int32(opx) == int32(TK_EQ) { _sqlite3VdbeAddOp2(tls, v, int32(OP_NotNull), dest, addrDone) } else { _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addrDone) if i == nLeft-int32(2) { opx = op } } goto _1 _1: ; i++ } _sqlite3VdbeJumpHere(tls, v, addrCmp) _sqlite3VdbeResolveLabel(tls, v, addrDone) if int32(op) == int32(TK_NE) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Not), dest, dest) } } // C documentation // // /* // ** Check that argument nHeight is less than or equal to the maximum // ** expression depth allowed. If it is not, leave an error message in // ** pParse. // */ func _sqlite3ExprCheckHeight(tls *libc.TLS, pParse uintptr, nHeight int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var mxHeight, rc int32 _, _ = mxHeight, rc rc = SQLITE_OK mxHeight = *(*int32)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb + 120 + 3*4)) if nHeight > mxHeight { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7569, libc.VaList(bp+8, mxHeight)) rc = int32(SQLITE_ERROR) } return rc } // C documentation // // /* The following three functions, heightOfExpr(), heightOfExprList() // ** and heightOfSelect(), are used to determine the maximum height // ** of any expression tree referenced by the structure passed as the // ** first argument. // ** // ** If this maximum height is greater than the current value pointed // ** to by pnHeight, the second parameter, then set *pnHeight to that // ** value. // */ func _heightOfExpr(tls *libc.TLS, p uintptr, pnHeight uintptr) { if p != 0 { if (*TExpr)(unsafe.Pointer(p)).FnHeight > *(*int32)(unsafe.Pointer(pnHeight)) { *(*int32)(unsafe.Pointer(pnHeight)) = (*TExpr)(unsafe.Pointer(p)).FnHeight } } } func _heightOfExprList(tls *libc.TLS, p uintptr, pnHeight uintptr) { var i int32 _ = i if p != 0 { i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(p)).FnExpr) { break } _heightOfExpr(tls, (*(*TExprList_item)(unsafe.Pointer(p + 8 + uintptr(i)*20))).FpExpr, pnHeight) goto _1 _1: ; i++ } } } func _heightOfSelect(tls *libc.TLS, pSelect uintptr, pnHeight uintptr) { var p uintptr _ = p p = pSelect for { if !(p != 0) { break } _heightOfExpr(tls, (*TSelect)(unsafe.Pointer(p)).FpWhere, pnHeight) _heightOfExpr(tls, (*TSelect)(unsafe.Pointer(p)).FpHaving, pnHeight) _heightOfExpr(tls, (*TSelect)(unsafe.Pointer(p)).FpLimit, pnHeight) _heightOfExprList(tls, (*TSelect)(unsafe.Pointer(p)).FpEList, pnHeight) _heightOfExprList(tls, (*TSelect)(unsafe.Pointer(p)).FpGroupBy, pnHeight) _heightOfExprList(tls, (*TSelect)(unsafe.Pointer(p)).FpOrderBy, pnHeight) goto _1 _1: ; p = (*TSelect)(unsafe.Pointer(p)).FpPrior } } // C documentation // // /* // ** Set the Expr.nHeight variable in the structure passed as an // ** argument. An expression with no children, Expr.pList or // ** Expr.pSelect member has a height of 1. Any other expression // ** has a height equal to the maximum height of any other // ** referenced Expr plus one. // ** // ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags, // ** if appropriate. // */ func _exprSetHeight(tls *libc.TLS, p uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var v1 int32 var _ /* nHeight at bp+0 */ int32 _ = v1 if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 { v1 = (*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).FpLeft)).FnHeight } else { v1 = 0 } *(*int32)(unsafe.Pointer(bp)) = v1 if (*TExpr)(unsafe.Pointer(p)).FpRight != 0 && (*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).FpRight)).FnHeight > *(*int32)(unsafe.Pointer(bp)) { *(*int32)(unsafe.Pointer(bp)) = (*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).FpRight)).FnHeight } if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_xIsSelect) != uint32(0) { _heightOfSelect(tls, *(*uintptr)(unsafe.Pointer(p + 20)), bp) } else { if *(*uintptr)(unsafe.Pointer(p + 20)) != 0 { _heightOfExprList(tls, *(*uintptr)(unsafe.Pointer(p + 20)), bp) *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(libc.Int32FromInt32(EP_Collate)|libc.Int32FromInt32(EP_Subquery)|libc.Int32FromInt32(EP_HasFunc)) & _sqlite3ExprListFlags(tls, *(*uintptr)(unsafe.Pointer(p + 20))) } } (*TExpr)(unsafe.Pointer(p)).FnHeight = *(*int32)(unsafe.Pointer(bp)) + int32(1) } // C documentation // // /* // ** Set the Expr.nHeight variable using the exprSetHeight() function. If // ** the height is greater than the maximum allowed expression depth, // ** leave an error in pParse. // ** // ** Also propagate all EP_Propagate flags from the Expr.x.pList into // ** Expr.flags. // */ func _sqlite3ExprSetHeightAndFlags(tls *libc.TLS, pParse uintptr, p uintptr) { if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return } _exprSetHeight(tls, p) _sqlite3ExprCheckHeight(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FnHeight) } // C documentation // // /* // ** Return the maximum height of any expression tree referenced // ** by the select statement passed as an argument. // */ func _sqlite3SelectExprHeight(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* nHeight at bp+0 */ int32 *(*int32)(unsafe.Pointer(bp)) = 0 _heightOfSelect(tls, p, bp) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Set the error offset for an Expr node, if possible. // */ func _sqlite3ExprSetErrorOffset(tls *libc.TLS, pExpr uintptr, iOfst int32) { if pExpr == uintptr(0) { return } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_InnerON)|libc.Int32FromInt32(EP_OuterON)) != uint32(0) { return } *(*int32)(unsafe.Pointer(pExpr + 36)) = iOfst } // C documentation // // /* // ** This routine is the core allocator for Expr nodes. // ** // ** Construct a new expression node and return a pointer to it. Memory // ** for this node and for the pToken argument is a single allocation // ** obtained from sqlite3DbMalloc(). The calling function // ** is responsible for making sure the node eventually gets freed. // ** // ** If dequote is true, then the token (if it exists) is dequoted. // ** If dequote is false, no dequoting is performed. The deQuote // ** parameter is ignored if pToken is NULL or if the token does not // ** appear to be quoted. If the quotes were of the form "..." (double-quotes) // ** then the EP_DblQuoted flag is set on the expression node. // ** // ** Special case: If op==TK_INTEGER and pToken points to a string that // ** can be translated into a 32-bit integer, then the token is not // ** stored in u.zToken. Instead, the integer values is written // ** into u.iValue and the EP_IntValue flag is set. No extra storage // ** is allocated to hold the integer text and the dequote flag is ignored. // */ func _sqlite3ExprAlloc(tls *libc.TLS, db uintptr, op int32, pToken uintptr, dequote int32) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var nExtra, v1 int32 var pNew uintptr var _ /* iValue at bp+0 */ int32 _, _, _ = nExtra, pNew, v1 nExtra = 0 *(*int32)(unsafe.Pointer(bp)) = 0 _ = libc.Int32FromInt32(0) if pToken != 0 { if op != int32(TK_INTEGER) || (*TToken)(unsafe.Pointer(pToken)).Fz == uintptr(0) || _sqlite3GetInt32(tls, (*TToken)(unsafe.Pointer(pToken)).Fz, bp) == 0 { nExtra = int32((*TToken)(unsafe.Pointer(pToken)).Fn + uint32(1)) _ = libc.Int32FromInt32(0) } } pNew = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(52)+uint32(nExtra))) if pNew != 0 { libc.Xmemset(tls, pNew, 0, uint32(52)) (*TExpr)(unsafe.Pointer(pNew)).Fop = uint8(op) (*TExpr)(unsafe.Pointer(pNew)).FiAgg = int16(-int32(1)) if pToken != 0 { if nExtra == 0 { if *(*int32)(unsafe.Pointer(bp)) != 0 { v1 = int32(EP_IsTrue) } else { v1 = int32(EP_IsFalse) } *(*Tu32)(unsafe.Pointer(pNew + 4)) |= uint32(libc.Int32FromInt32(EP_IntValue) | libc.Int32FromInt32(EP_Leaf) | v1) *(*int32)(unsafe.Pointer(pNew + 8)) = *(*int32)(unsafe.Pointer(bp)) } else { *(*uintptr)(unsafe.Pointer(pNew + 8)) = pNew + 1*52 _ = libc.Int32FromInt32(0) if (*TToken)(unsafe.Pointer(pToken)).Fn != 0 { libc.Xmemcpy(tls, *(*uintptr)(unsafe.Pointer(pNew + 8)), (*TToken)(unsafe.Pointer(pToken)).Fz, (*TToken)(unsafe.Pointer(pToken)).Fn) } *(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNew + 8)) + uintptr((*TToken)(unsafe.Pointer(pToken)).Fn))) = uint8(0) if dequote != 0 && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNew + 8))))])&int32(0x80) != 0 { _sqlite3DequoteExpr(tls, pNew) } } } (*TExpr)(unsafe.Pointer(pNew)).FnHeight = int32(1) } return pNew } // C documentation // // /* // ** Allocate a new expression node from a zero-terminated token that has // ** already been dequoted. // */ func _sqlite3Expr(tls *libc.TLS, db uintptr, op int32, zToken uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* x at bp+0 */ TToken (*(*TToken)(unsafe.Pointer(bp))).Fz = zToken (*(*TToken)(unsafe.Pointer(bp))).Fn = uint32(_sqlite3Strlen30(tls, zToken)) return _sqlite3ExprAlloc(tls, db, op, bp, 0) } // C documentation // // /* // ** Attach subtrees pLeft and pRight to the Expr node pRoot. // ** // ** If pRoot==NULL that means that a memory allocation error has occurred. // ** In that case, delete the subtrees pLeft and pRight. // */ func _sqlite3ExprAttachSubtrees(tls *libc.TLS, db uintptr, pRoot uintptr, pLeft uintptr, pRight uintptr) { if pRoot == uintptr(0) { _ = libc.Int32FromInt32(0) _sqlite3ExprDelete(tls, db, pLeft) _sqlite3ExprDelete(tls, db, pRight) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pRight != 0 { (*TExpr)(unsafe.Pointer(pRoot)).FpRight = pRight *(*Tu32)(unsafe.Pointer(pRoot + 4)) |= uint32(libc.Int32FromInt32(EP_Collate)|libc.Int32FromInt32(EP_Subquery)|libc.Int32FromInt32(EP_HasFunc)) & (*TExpr)(unsafe.Pointer(pRight)).Fflags (*TExpr)(unsafe.Pointer(pRoot)).FnHeight = (*TExpr)(unsafe.Pointer(pRight)).FnHeight + int32(1) } else { (*TExpr)(unsafe.Pointer(pRoot)).FnHeight = int32(1) } if pLeft != 0 { (*TExpr)(unsafe.Pointer(pRoot)).FpLeft = pLeft *(*Tu32)(unsafe.Pointer(pRoot + 4)) |= uint32(libc.Int32FromInt32(EP_Collate)|libc.Int32FromInt32(EP_Subquery)|libc.Int32FromInt32(EP_HasFunc)) & (*TExpr)(unsafe.Pointer(pLeft)).Fflags if (*TExpr)(unsafe.Pointer(pLeft)).FnHeight >= (*TExpr)(unsafe.Pointer(pRoot)).FnHeight { (*TExpr)(unsafe.Pointer(pRoot)).FnHeight = (*TExpr)(unsafe.Pointer(pLeft)).FnHeight + int32(1) } } } } // C documentation // // /* // ** Allocate an Expr node which joins as many as two subtrees. // ** // ** One or both of the subtrees can be NULL. Return a pointer to the new // ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, // ** free the subtrees and return NULL. // */ func _sqlite3PExpr(tls *libc.TLS, pParse uintptr, op int32, pLeft uintptr, pRight uintptr) (r uintptr) { var p uintptr _ = p p = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(52)) if p != 0 { libc.Xmemset(tls, p, 0, uint32(52)) (*TExpr)(unsafe.Pointer(p)).Fop = uint8(op & int32(0xff)) (*TExpr)(unsafe.Pointer(p)).FiAgg = int16(-int32(1)) _sqlite3ExprAttachSubtrees(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, p, pLeft, pRight) _sqlite3ExprCheckHeight(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FnHeight) } else { _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pLeft) _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pRight) } return p } // C documentation // // /* // ** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due // ** do a memory allocation failure) then delete the pSelect object. // */ func _sqlite3PExprAddSelect(tls *libc.TLS, pParse uintptr, pExpr uintptr, pSelect uintptr) { if pExpr != 0 { *(*uintptr)(unsafe.Pointer(pExpr + 20)) = pSelect *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_xIsSelect) | libc.Int32FromInt32(EP_Subquery)) _sqlite3ExprSetHeightAndFlags(tls, pParse, pExpr) } else { _ = libc.Int32FromInt32(0) _sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSelect) } } // C documentation // // /* // ** Expression list pEList is a list of vector values. This function // ** converts the contents of pEList to a VALUES(...) Select statement // ** returning 1 row for each element of the list. For example, the // ** expression list: // ** // ** ( (1,2), (3,4) (5,6) ) // ** // ** is translated to the equivalent of: // ** // ** VALUES(1,2), (3,4), (5,6) // ** // ** Each of the vector values in pEList must contain exactly nElem terms. // ** If a list element that is not a vector or does not contain nElem terms, // ** an error message is left in pParse. // ** // ** This is used as part of processing IN(...) expressions with a list // ** of vectors on the RHS. e.g. "... IN ((1,2), (3,4), (5,6))". // */ func _sqlite3ExprListToValues(tls *libc.TLS, pParse uintptr, nElem int32, pEList uintptr) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var ii, nExprElem int32 var pExpr, pRet, pSel, v2 uintptr _, _, _, _, _, _ = ii, nExprElem, pExpr, pRet, pSel, v2 pRet = uintptr(0) _ = libc.Int32FromInt32(0) ii = 0 for { if !(ii < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } pExpr = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(ii)*20))).FpExpr if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_VECTOR) { _ = libc.Int32FromInt32(0) nExprElem = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr } else { nExprElem = int32(1) } if nExprElem != nElem { if nExprElem > int32(1) { v2 = __ccgo_ts + 7617 } else { v2 = __ccgo_ts + 1648 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7619, libc.VaList(bp+8, nExprElem, v2, nElem)) break } _ = libc.Int32FromInt32(0) pSel = _sqlite3SelectNew(tls, pParse, *(*uintptr)(unsafe.Pointer(pExpr + 20)), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(SF_Values), uintptr(0)) *(*uintptr)(unsafe.Pointer(pExpr + 20)) = uintptr(0) if pSel != 0 { if pRet != 0 { (*TSelect)(unsafe.Pointer(pSel)).Fop = uint8(TK_ALL) (*TSelect)(unsafe.Pointer(pSel)).FpPrior = pRet } pRet = pSel } goto _1 _1: ; ii++ } if pRet != 0 && (*TSelect)(unsafe.Pointer(pRet)).FpPrior != 0 { *(*Tu32)(unsafe.Pointer(pRet + 4)) |= uint32(SF_MultiValue) } _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pEList) return pRet } // C documentation // // /* // ** Join two expressions using an AND operator. If either expression is // ** NULL, then just return the other expression. // ** // ** If one side or the other of the AND is known to be false, and neither side // ** is part of an ON clause, then instead of returning an AND expression, // ** just return a constant expression with a value of false. // */ func _sqlite3ExprAnd(tls *libc.TLS, pParse uintptr, pLeft uintptr, pRight uintptr) (r uintptr) { var db uintptr var f Tu32 _, _ = db, f db = (*TParse)(unsafe.Pointer(pParse)).Fdb if pLeft == uintptr(0) { return pRight } else { if pRight == uintptr(0) { return pLeft } else { f = (*TExpr)(unsafe.Pointer(pLeft)).Fflags | (*TExpr)(unsafe.Pointer(pRight)).Fflags if f&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)|libc.Int32FromInt32(EP_IsFalse)) == uint32(EP_IsFalse) && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) { _sqlite3ExprDeferredDelete(tls, pParse, pLeft) _sqlite3ExprDeferredDelete(tls, pParse, pRight) return _sqlite3Expr(tls, db, int32(TK_INTEGER), __ccgo_ts+1722) } else { return _sqlite3PExpr(tls, pParse, int32(TK_AND), pLeft, pRight) } } } return r } // C documentation // // /* // ** Construct a new expression node for a function with multiple // ** arguments. // */ func _sqlite3ExprFunction(tls *libc.TLS, pParse uintptr, pList uintptr, pToken uintptr, eDistinct int32) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, pNew uintptr _, _ = db, pNew db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) pNew = _sqlite3ExprAlloc(tls, db, int32(TK_FUNCTION), pToken, int32(1)) if pNew == uintptr(0) { _sqlite3ExprListDelete(tls, db, pList) /* Avoid memory leak when malloc fails */ return uintptr(0) } _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pNew + 36)) = int32((*TToken)(unsafe.Pointer(pToken)).Fz) - int32((*TParse)(unsafe.Pointer(pParse)).FzTail) if pList != 0 && (*TExprList)(unsafe.Pointer(pList)).FnExpr > *(*int32)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb + 120 + 6*4)) && !((*TParse)(unsafe.Pointer(pParse)).Fnested != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7663, libc.VaList(bp+8, pToken)) } *(*uintptr)(unsafe.Pointer(pNew + 20)) = pList *(*Tu32)(unsafe.Pointer(pNew + 4)) |= uint32(libc.Int32FromInt32(EP_HasFunc)) _ = libc.Int32FromInt32(0) _sqlite3ExprSetHeightAndFlags(tls, pParse, pNew) if eDistinct == int32(SF_Distinct) { *(*Tu32)(unsafe.Pointer(pNew + 4)) |= uint32(libc.Int32FromInt32(EP_Distinct)) } return pNew } // C documentation // // /* // ** Report an error when attempting to use an ORDER BY clause within // ** the arguments of a non-aggregate function. // */ func _sqlite3ExprOrderByAggregateError(tls *libc.TLS, pParse uintptr, p uintptr) { bp := tls.Alloc(16) defer tls.Free(16) _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7697, libc.VaList(bp+8, p)) } // C documentation // // /* // ** Attach an ORDER BY clause to a function call. // ** // ** functionname( arguments ORDER BY sortlist ) // ** \_____________________/ \______/ // ** pExpr pOrderBy // ** // ** The ORDER BY clause is inserted into a new Expr node of type TK_ORDER // ** and added to the Expr.pLeft field of the parent TK_FUNCTION node. // */ func _sqlite3ExprAddFunctionOrderBy(tls *libc.TLS, pParse uintptr, pExpr uintptr, pOrderBy uintptr) { var db, pOB uintptr _, _ = db, pOB db = (*TParse)(unsafe.Pointer(pParse)).Fdb if pOrderBy == uintptr(0) { _ = libc.Int32FromInt32(0) return } if pExpr == uintptr(0) { _ = libc.Int32FromInt32(0) _sqlite3ExprListDelete(tls, db, pOrderBy) return } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(pExpr + 20)) == uintptr(0) || (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr == 0 { /* Ignore ORDER BY on zero-argument aggregates */ _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3ExprListDeleteGeneric), pOrderBy) return } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) && int32((*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)))).FeFrmType) != int32(TK_FILTER) { _sqlite3ExprOrderByAggregateError(tls, pParse, pExpr) _sqlite3ExprListDelete(tls, db, pOrderBy) return } pOB = _sqlite3ExprAlloc(tls, db, int32(TK_ORDER), uintptr(0), 0) if pOB == uintptr(0) { _sqlite3ExprListDelete(tls, db, pOrderBy) return } *(*uintptr)(unsafe.Pointer(pOB + 20)) = pOrderBy _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer(pExpr)).FpLeft = pOB *(*Tu32)(unsafe.Pointer(pOB + 4)) |= uint32(libc.Int32FromInt32(EP_FullSize)) } // C documentation // // /* // ** Check to see if a function is usable according to current access // ** rules: // ** // ** SQLITE_FUNC_DIRECT - Only usable from top-level SQL // ** // ** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from // ** top-level SQL // ** // ** If the function is not usable, create an error. // */ func _sqlite3ExprFunctionUsable(tls *libc.TLS, pParse uintptr, pExpr uintptr, pDef uintptr) { bp := tls.Alloc(16) defer tls.Free(16) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_FromDDL)) != uint32(0) { if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_DIRECT) != uint32(0) || (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&uint64(SQLITE_TrustedSchema) == uint64(0) { /* Functions prohibited in triggers and views if: ** (1) tagged with SQLITE_DIRECTONLY ** (2) not tagged with SQLITE_INNOCUOUS (which means it ** is tagged with SQLITE_FUNC_UNSAFE) and ** SQLITE_DBCONFIG_TRUSTED_SCHEMA is off (meaning ** that the schema is possibly tainted). */ _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7747, libc.VaList(bp+8, pExpr)) } } } // C documentation // // /* // ** Assign a variable number to an expression that encodes a wildcard // ** in the original SQL statement. // ** // ** Wildcards consisting of a single "?" are assigned the next sequential // ** variable number. // ** // ** Wildcards of the form "?nnn" are assigned the number "nnn". We make // ** sure "nnn" is not too big to avoid a denial of service attack when // ** the SQL statement comes from an external source. // ** // ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number // ** as the previous instance of the same wildcard. Or if this is the first // ** instance of the wildcard, the next sequential variable number is // ** assigned. // */ func _sqlite3ExprAssignVarNumber(tls *libc.TLS, pParse uintptr, pExpr uintptr, n Tu32) { bp := tls.Alloc(32) defer tls.Free(32) var bOk, doAdd int32 var db, z, v2, v4 uintptr var x, v1, v3 TynVar var _ /* i at bp+0 */ Ti64 _, _, _, _, _, _, _, _, _ = bOk, db, doAdd, x, z, v1, v2, v3, v4 db = (*TParse)(unsafe.Pointer(pParse)).Fdb if pExpr == uintptr(0) { return } _ = libc.Int32FromInt32(0) z = *(*uintptr)(unsafe.Pointer(pExpr + 8)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(*(*uint8)(unsafe.Pointer(z + 1))) == 0 { /* Wildcard of the form "?". Assign the next variable number */ _ = libc.Int32FromInt32(0) v2 = pParse + 216 *(*TynVar)(unsafe.Pointer(v2))++ v1 = *(*TynVar)(unsafe.Pointer(v2)) x = v1 } else { doAdd = 0 if int32(*(*uint8)(unsafe.Pointer(z))) == int32('?') { if n == uint32(2) { /*OPTIMIZATION-IF-TRUE*/ *(*Ti64)(unsafe.Pointer(bp)) = int64(int32(*(*uint8)(unsafe.Pointer(z + 1))) - int32('0')) /* The common case of ?N for a single digit N */ bOk = int32(1) } else { bOk = libc.BoolInt32(0 == _sqlite3Atoi64(tls, z+1, bp, int32(n-uint32(1)), uint8(SQLITE_UTF8))) } if bOk == 0 || *(*Ti64)(unsafe.Pointer(bp)) < int64(1) || *(*Ti64)(unsafe.Pointer(bp)) > int64(*(*int32)(unsafe.Pointer(db + 120 + 9*4))) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7767, libc.VaList(bp+16, *(*int32)(unsafe.Pointer(db + 120 + 9*4)))) _sqlite3RecordErrorOffsetOfExpr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr) return } x = int16(*(*Ti64)(unsafe.Pointer(bp))) if int32(x) > int32((*TParse)(unsafe.Pointer(pParse)).FnVar) { (*TParse)(unsafe.Pointer(pParse)).FnVar = int16(int32(x)) doAdd = int32(1) } else { if _sqlite3VListNumToName(tls, (*TParse)(unsafe.Pointer(pParse)).FpVList, int32(x)) == uintptr(0) { doAdd = int32(1) } } } else { /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable ** number as the prior appearance of the same name, or if the name ** has never appeared before, reuse the same variable number */ x = int16(_sqlite3VListNameToNum(tls, (*TParse)(unsafe.Pointer(pParse)).FpVList, z, int32(n))) if int32(x) == 0 { v4 = pParse + 216 *(*TynVar)(unsafe.Pointer(v4))++ v3 = *(*TynVar)(unsafe.Pointer(v4)) x = v3 doAdd = int32(1) } } if doAdd != 0 { (*TParse)(unsafe.Pointer(pParse)).FpVList = _sqlite3VListAdd(tls, db, (*TParse)(unsafe.Pointer(pParse)).FpVList, z, int32(n), int32(x)) } } (*TExpr)(unsafe.Pointer(pExpr)).FiColumn = x if int32(x) > *(*int32)(unsafe.Pointer(db + 120 + 9*4)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7810, 0) _sqlite3RecordErrorOffsetOfExpr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr) } } // C documentation // // /* // ** Recursively delete an expression tree. // */ func _sqlite3ExprDeleteNN(tls *libc.TLS, db uintptr, p uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if !((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_TokenOnly)|libc.Int32FromInt32(EP_Leaf)) != libc.Uint32FromInt32(0)) { /* The Expr.x union is never used at the same time as Expr.pRight */ _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 && int32((*TExpr)(unsafe.Pointer(p)).Fop) != int32(TK_SELECT_COLUMN) { _sqlite3ExprDeleteNN(tls, db, (*TExpr)(unsafe.Pointer(p)).FpLeft) } if (*TExpr)(unsafe.Pointer(p)).FpRight != 0 { _ = libc.Int32FromInt32(0) _sqlite3ExprDeleteNN(tls, db, (*TExpr)(unsafe.Pointer(p)).FpRight) } else { if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_xIsSelect) != uint32(0) { _ = libc.Int32FromInt32(0) _sqlite3SelectDelete(tls, db, *(*uintptr)(unsafe.Pointer(p + 20))) } else { _sqlite3ExprListDelete(tls, db, *(*uintptr)(unsafe.Pointer(p + 20))) if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { _sqlite3WindowDelete(tls, db, *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(p)).Fy))) } } } } if !((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_Static)) != libc.Uint32FromInt32(0)) { _sqlite3DbNNFreeNN(tls, db, p) } } func _sqlite3ExprDelete(tls *libc.TLS, db uintptr, p uintptr) { if p != 0 { _sqlite3ExprDeleteNN(tls, db, p) } } func _sqlite3ExprDeleteGeneric(tls *libc.TLS, db uintptr, p uintptr) { if p != 0 { _sqlite3ExprDeleteNN(tls, db, p) } } // C documentation // // /* // ** Clear both elements of an OnOrUsing object // */ func _sqlite3ClearOnOrUsing(tls *libc.TLS, db uintptr, p uintptr) { if p == uintptr(0) { /* Nothing to clear */ } else { if (*TOnOrUsing)(unsafe.Pointer(p)).FpOn != 0 { _sqlite3ExprDeleteNN(tls, db, (*TOnOrUsing)(unsafe.Pointer(p)).FpOn) } else { if (*TOnOrUsing)(unsafe.Pointer(p)).FpUsing != 0 { _sqlite3IdListDelete(tls, db, (*TOnOrUsing)(unsafe.Pointer(p)).FpUsing) } } } } // C documentation // // /* // ** Arrange to cause pExpr to be deleted when the pParse is deleted. // ** This is similar to sqlite3ExprDelete() except that the delete is // ** deferred until the pParse is deleted. // ** // ** The pExpr might be deleted immediately on an OOM error. // ** // ** The deferred delete is (currently) implemented by adding the // ** pExpr to the pParse->pConstExpr list with a register number of 0. // */ func _sqlite3ExprDeferredDelete(tls *libc.TLS, pParse uintptr, pExpr uintptr) { _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3ExprDeleteGeneric), pExpr) } // C documentation // // /* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the // ** expression. // */ func _sqlite3ExprUnmapAndDelete(tls *libc.TLS, pParse uintptr, p uintptr) { if p != 0 { if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameExprUnmap(tls, pParse, p) } _sqlite3ExprDeleteNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, p) } } // C documentation // // /* // ** Return the number of bytes allocated for the expression structure // ** passed as the first argument. This is always one of EXPR_FULLSIZE, // ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE. // */ func _exprStructSize(tls *libc.TLS, p uintptr) (r int32) { if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_TokenOnly)) != uint32(0) { return int32(uint32(libc.UintptrFromInt32(0) + 12)) } if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_Reduced)) != uint32(0) { return int32(uint32(libc.UintptrFromInt32(0) + 28)) } return int32(52) } // C documentation // // /* // ** The dupedExpr*Size() routines each return the number of bytes required // ** to store a copy of an expression or expression tree. They differ in // ** how much of the tree is measured. // ** // ** dupedExprStructSize() Size of only the Expr structure // ** dupedExprNodeSize() Size of Expr + space for token // ** dupedExprSize() Expr + token + subtree components // ** // *************************************************************************** // ** // ** The dupedExprStructSize() function returns two values OR-ed together: // ** (1) the space required for a copy of the Expr structure only and // ** (2) the EP_xxx flags that indicate what the structure size should be. // ** The return values is always one of: // ** // ** EXPR_FULLSIZE // ** EXPR_REDUCEDSIZE | EP_Reduced // ** EXPR_TOKENONLYSIZE | EP_TokenOnly // ** // ** The size of the structure can be found by masking the return value // ** of this routine with 0xfff. The flags can be found by masking the // ** return value with EP_Reduced|EP_TokenOnly. // ** // ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size // ** (unreduced) Expr objects as they or originally constructed by the parser. // ** During expression analysis, extra information is computed and moved into // ** later parts of the Expr object and that extra information might get chopped // ** off if the expression is reduced. Note also that it does not work to // ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal // ** to reduce a pristine expression tree from the parser. The implementation // ** of dupedExprStructSize() contain multiple assert() statements that attempt // ** to enforce this constraint. // */ func _dupedExprStructSize(tls *libc.TLS, p uintptr, flags int32) (r int32) { var nSize int32 _ = nSize _ = libc.Int32FromInt32(0) /* Only one flag value allowed */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if 0 == flags || (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_FullSize)) != uint32(0) { nSize = int32(52) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 || *(*uintptr)(unsafe.Pointer(p + 20)) != 0 { nSize = int32(uint32(libc.UintptrFromInt32(0)+28) | libc.Uint32FromInt32(EP_Reduced)) } else { _ = libc.Int32FromInt32(0) nSize = int32(uint32(libc.UintptrFromInt32(0)+12) | libc.Uint32FromInt32(EP_TokenOnly)) } } return nSize } // C documentation // // /* // ** This function returns the space in bytes required to store the copy // ** of the Expr structure and a copy of the Expr.u.zToken string (if that // ** string is defined.) // */ func _dupedExprNodeSize(tls *libc.TLS, p uintptr, flags int32) (r int32) { var nByte int32 _ = nByte nByte = _dupedExprStructSize(tls, p, flags) & int32(0xfff) if !((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_IntValue)) != libc.Uint32FromInt32(0)) && *(*uintptr)(unsafe.Pointer(p + 8)) != 0 { nByte = int32(uint32(nByte) + (libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(p + 8)))&libc.Uint32FromInt32(0x3fffffff) + libc.Uint32FromInt32(1))) } return (nByte + int32(7)) & ^libc.Int32FromInt32(7) } // C documentation // // /* // ** Return the number of bytes required to create a duplicate of the // ** expression passed as the first argument. // ** // ** The value returned includes space to create a copy of the Expr struct // ** itself and the buffer referred to by Expr.u.zToken, if any. // ** // ** The return value includes space to duplicate all Expr nodes in the // ** tree formed by Expr.pLeft and Expr.pRight, but not any other // ** substructure such as Expr.x.pList, Expr.x.pSelect, and Expr.y.pWin. // */ func _dupedExprSize(tls *libc.TLS, p uintptr) (r int32) { var nByte int32 _ = nByte _ = libc.Int32FromInt32(0) nByte = _dupedExprNodeSize(tls, p, int32(EXPRDUP_REDUCE)) if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 { nByte += _dupedExprSize(tls, (*TExpr)(unsafe.Pointer(p)).FpLeft) } if (*TExpr)(unsafe.Pointer(p)).FpRight != 0 { nByte += _dupedExprSize(tls, (*TExpr)(unsafe.Pointer(p)).FpRight) } _ = libc.Int32FromInt32(0) return nByte } // C documentation // // /* // ** An EdupBuf is a memory allocation used to stored multiple Expr objects // ** together with their Expr.zToken content. This is used to help implement // ** compression while doing sqlite3ExprDup(). The top-level Expr does the // ** allocation for itself and many of its decendents, then passes an instance // ** of the structure down into exprDup() so that they decendents can have // ** access to that memory. // */ type TEdupBuf = struct { FzAlloc uintptr } type EdupBuf = TEdupBuf type TEdupBuf1 = struct { FzAlloc uintptr } type EdupBuf1 = TEdupBuf1 // C documentation // // /* // ** This function is similar to sqlite3ExprDup(), except that if pEdupBuf // ** is not NULL then it points to memory that can be used to store a copy // ** of the input Expr p together with its p->u.zToken (if any). pEdupBuf // ** is updated with the new buffer tail prior to returning. // */ func _exprDup(tls *libc.TLS, db uintptr, p uintptr, dupFlags int32, pEdupBuf uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var nAlloc, nNewSize, nToken, v2 int32 var nSize, staticFlag Tu32 var nStructSize uint32 var pNew, zToken, v1, v3, v4 uintptr var _ /* sEdupBuf at bp+0 */ TEdupBuf _, _, _, _, _, _, _, _, _, _, _, _ = nAlloc, nNewSize, nSize, nStructSize, nToken, pNew, staticFlag, zToken, v1, v2, v3, v4 /* EP_Static if space not obtained from malloc */ nToken = -int32(1) /* Space needed for p->u.zToken. -1 means unknown */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Figure out where to write the new Expr structure. */ if pEdupBuf != 0 { (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc = (*TEdupBuf)(unsafe.Pointer(pEdupBuf)).FzAlloc staticFlag = uint32(EP_Static) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } else { if dupFlags != 0 { nAlloc = _dupedExprSize(tls, p) } else { if !((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_IntValue)) != libc.Uint32FromInt32(0)) && *(*uintptr)(unsafe.Pointer(p + 8)) != 0 { nToken = int32(libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(p + 8)))&uint32(0x3fffffff) + uint32(1)) nAlloc = int32((libc.Uint32FromInt64(52) + uint32(nToken) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7))) } else { nToken = 0 nAlloc = int32((libc.Uint32FromInt64(52) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7))) } } _ = libc.Int32FromInt32(0) (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc = _sqlite3DbMallocRawNN(tls, db, uint64(nAlloc)) staticFlag = uint32(0) } pNew = (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc _ = libc.Int32FromInt32(0) if pNew != 0 { /* Set nNewSize to the size allocated for the structure pointed to ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed ** by the copy of the p->u.zToken string (if any). */ nStructSize = uint32(_dupedExprStructSize(tls, p, dupFlags)) nNewSize = int32(nStructSize & uint32(0xfff)) if nToken < 0 { if !((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_IntValue)) != libc.Uint32FromInt32(0)) && *(*uintptr)(unsafe.Pointer(p + 8)) != 0 { nToken = _sqlite3Strlen30(tls, *(*uintptr)(unsafe.Pointer(p + 8))) + int32(1) } else { nToken = 0 } } if dupFlags != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc, p, uint32(nNewSize)) } else { nSize = uint32(_exprStructSize(tls, p)) _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc, p, nSize) if nSize < uint32(52) { libc.Xmemset(tls, (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc+uintptr(nSize), 0, uint32(52)-nSize) } nNewSize = int32(52) } /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ *(*Tu32)(unsafe.Pointer(pNew + 4)) &= uint32(^(libc.Int32FromInt32(EP_Reduced) | libc.Int32FromInt32(EP_TokenOnly) | libc.Int32FromInt32(EP_Static))) *(*Tu32)(unsafe.Pointer(pNew + 4)) |= nStructSize & uint32(libc.Int32FromInt32(EP_Reduced)|libc.Int32FromInt32(EP_TokenOnly)) *(*Tu32)(unsafe.Pointer(pNew + 4)) |= staticFlag if dupFlags != 0 { } /* Copy the p->u.zToken string, if any. */ _ = libc.Int32FromInt32(0) if nToken > 0 { v1 = (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc + uintptr(nNewSize) *(*uintptr)(unsafe.Pointer(pNew + 8)) = v1 zToken = v1 libc.Xmemcpy(tls, zToken, *(*uintptr)(unsafe.Pointer(p + 8)), uint32(nToken)) nNewSize += nToken } (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc += uintptr((nNewSize + libc.Int32FromInt32(7)) & ^libc.Int32FromInt32(7)) if ((*TExpr)(unsafe.Pointer(p)).Fflags|(*TExpr)(unsafe.Pointer(pNew)).Fflags)&uint32(libc.Int32FromInt32(EP_TokenOnly)|libc.Int32FromInt32(EP_Leaf)) == uint32(0) { /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_xIsSelect) != uint32(0) { *(*uintptr)(unsafe.Pointer(pNew + 20)) = _sqlite3SelectDup(tls, db, *(*uintptr)(unsafe.Pointer(p + 20)), dupFlags) } else { if int32((*TExpr)(unsafe.Pointer(p)).Fop) != int32(TK_ORDER) { v2 = dupFlags } else { v2 = 0 } *(*uintptr)(unsafe.Pointer(pNew + 20)) = _sqlite3ExprListDup(tls, db, *(*uintptr)(unsafe.Pointer(p + 20)), v2) } if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pNew)).Fy)) = _sqlite3WindowDup(tls, db, pNew, *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(p)).Fy))) _ = libc.Int32FromInt32(0) } /* Fill in pNew->pLeft and pNew->pRight. */ if dupFlags != 0 { if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_SELECT_COLUMN) { (*TExpr)(unsafe.Pointer(pNew)).FpLeft = (*TExpr)(unsafe.Pointer(p)).FpLeft _ = libc.Int32FromInt32(0) } else { if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 { v3 = _exprDup(tls, db, (*TExpr)(unsafe.Pointer(p)).FpLeft, int32(EXPRDUP_REDUCE), bp) } else { v3 = uintptr(0) } (*TExpr)(unsafe.Pointer(pNew)).FpLeft = v3 } if (*TExpr)(unsafe.Pointer(p)).FpRight != 0 { v4 = _exprDup(tls, db, (*TExpr)(unsafe.Pointer(p)).FpRight, int32(EXPRDUP_REDUCE), bp) } else { v4 = uintptr(0) } (*TExpr)(unsafe.Pointer(pNew)).FpRight = v4 } else { if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_SELECT_COLUMN) { (*TExpr)(unsafe.Pointer(pNew)).FpLeft = (*TExpr)(unsafe.Pointer(p)).FpLeft _ = libc.Int32FromInt32(0) } else { (*TExpr)(unsafe.Pointer(pNew)).FpLeft = _sqlite3ExprDup(tls, db, (*TExpr)(unsafe.Pointer(p)).FpLeft, 0) } (*TExpr)(unsafe.Pointer(pNew)).FpRight = _sqlite3ExprDup(tls, db, (*TExpr)(unsafe.Pointer(p)).FpRight, 0) } } } if pEdupBuf != 0 { libc.Xmemcpy(tls, pEdupBuf, bp, uint32(4)) } _ = libc.Int32FromInt32(0) return pNew } // C documentation // // /* // ** Create and return a deep copy of the object passed as the second // ** argument. If an OOM condition is encountered, NULL is returned // ** and the db->mallocFailed flag set. // */ func _sqlite3WithDup(tls *libc.TLS, db uintptr, p uintptr) (r uintptr) { var i int32 var nByte Tsqlite3_int64 var pRet uintptr _, _, _ = i, nByte, pRet pRet = uintptr(0) if p != 0 { nByte = int64(uint32(36) + uint32(24)*uint32((*TWith)(unsafe.Pointer(p)).FnCte-libc.Int32FromInt32(1))) pRet = _sqlite3DbMallocZero(tls, db, uint64(nByte)) if pRet != 0 { (*TWith)(unsafe.Pointer(pRet)).FnCte = (*TWith)(unsafe.Pointer(p)).FnCte i = 0 for { if !(i < (*TWith)(unsafe.Pointer(p)).FnCte) { break } (*(*TCte)(unsafe.Pointer(pRet + 12 + uintptr(i)*24))).FpSelect = _sqlite3SelectDup(tls, db, (*(*TCte)(unsafe.Pointer(p + 12 + uintptr(i)*24))).FpSelect, 0) (*(*TCte)(unsafe.Pointer(pRet + 12 + uintptr(i)*24))).FpCols = _sqlite3ExprListDup(tls, db, (*(*TCte)(unsafe.Pointer(p + 12 + uintptr(i)*24))).FpCols, 0) (*(*TCte)(unsafe.Pointer(pRet + 12 + uintptr(i)*24))).FzName = _sqlite3DbStrDup(tls, db, (*(*TCte)(unsafe.Pointer(p + 12 + uintptr(i)*24))).FzName) (*(*TCte)(unsafe.Pointer(pRet + 12 + uintptr(i)*24))).FeM10d = (*(*TCte)(unsafe.Pointer(p + 12 + uintptr(i)*24))).FeM10d goto _1 _1: ; i++ } } } return pRet } // C documentation // // /* // ** The gatherSelectWindows() procedure and its helper routine // ** gatherSelectWindowsCallback() are used to scan all the expressions // ** an a newly duplicated SELECT statement and gather all of the Window // ** objects found there, assembling them onto the linked list at Select->pWin. // */ func _gatherSelectWindowsCallback(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var pSelect, pWin uintptr _, _ = pSelect, pWin if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_FUNCTION) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { pSelect = *(*uintptr)(unsafe.Pointer(pWalker + 24)) pWin = *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3WindowLink(tls, pSelect, pWin) } return WRC_Continue } func _gatherSelectWindowsSelectCallback(tls *libc.TLS, pWalker uintptr, p uintptr) (r int32) { var v1 int32 _ = v1 if p == *(*uintptr)(unsafe.Pointer(pWalker + 24)) { v1 = WRC_Continue } else { v1 = int32(WRC_Prune) } return v1 } func _gatherSelectWindows(tls *libc.TLS, p uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* w at bp+0 */ TWalker (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_gatherSelectWindowsCallback) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_gatherSelectWindowsSelectCallback) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback2 = uintptr(0) (*(*TWalker)(unsafe.Pointer(bp))).FpParse = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 24)) = p _sqlite3WalkSelect(tls, bp, p) } // C documentation // // /* // ** The following group of routines make deep copies of expressions, // ** expression lists, ID lists, and select statements. The copies can // ** be deleted (by being passed to their respective ...Delete() routines) // ** without effecting the originals. // ** // ** The expression list, ID, and source lists return by sqlite3ExprListDup(), // ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded // ** by subsequent calls to sqlite*ListAppend() routines. // ** // ** Any tables that the SrcList might point to are not duplicated. // ** // ** The flags parameter contains a combination of the EXPRDUP_XXX flags. // ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a // ** truncated version of the usual Expr structure that will be stored as // ** part of the in-memory representation of the database schema. // */ func _sqlite3ExprDup(tls *libc.TLS, db uintptr, p uintptr, flags int32) (r uintptr) { var v1 uintptr _ = v1 _ = libc.Int32FromInt32(0) if p != 0 { v1 = _exprDup(tls, db, p, flags, uintptr(0)) } else { v1 = uintptr(0) } return v1 } func _sqlite3ExprListDup(tls *libc.TLS, db uintptr, p uintptr, flags int32) (r uintptr) { var i int32 var pItem, pNew, pNewExpr, pOldExpr, pOldItem, pPriorSelectColNew, pPriorSelectColOld, v2 uintptr var v3 bool _, _, _, _, _, _, _, _, _, _ = i, pItem, pNew, pNewExpr, pOldExpr, pOldItem, pPriorSelectColNew, pPriorSelectColOld, v2, v3 pPriorSelectColOld = uintptr(0) pPriorSelectColNew = uintptr(0) _ = libc.Int32FromInt32(0) if p == uintptr(0) { return uintptr(0) } pNew = _sqlite3DbMallocRawNN(tls, db, uint64(_sqlite3DbMallocSize(tls, db, p))) if pNew == uintptr(0) { return uintptr(0) } (*TExprList)(unsafe.Pointer(pNew)).FnExpr = (*TExprList)(unsafe.Pointer(p)).FnExpr (*TExprList)(unsafe.Pointer(pNew)).FnAlloc = (*TExprList)(unsafe.Pointer(p)).FnAlloc pItem = pNew + 8 pOldItem = p + 8 i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(p)).FnExpr) { break } pOldExpr = (*TExprList_item)(unsafe.Pointer(pOldItem)).FpExpr (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr = _sqlite3ExprDup(tls, db, pOldExpr, flags) if v3 = pOldExpr != 0 && int32((*TExpr)(unsafe.Pointer(pOldExpr)).Fop) == int32(TK_SELECT_COLUMN); v3 { v2 = (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr pNewExpr = v2 } if v3 && v2 != uintptr(0) { if (*TExpr)(unsafe.Pointer(pNewExpr)).FpRight != 0 { pPriorSelectColOld = (*TExpr)(unsafe.Pointer(pOldExpr)).FpRight pPriorSelectColNew = (*TExpr)(unsafe.Pointer(pNewExpr)).FpRight (*TExpr)(unsafe.Pointer(pNewExpr)).FpLeft = (*TExpr)(unsafe.Pointer(pNewExpr)).FpRight } else { if (*TExpr)(unsafe.Pointer(pOldExpr)).FpLeft != pPriorSelectColOld { pPriorSelectColOld = (*TExpr)(unsafe.Pointer(pOldExpr)).FpLeft pPriorSelectColNew = _sqlite3ExprDup(tls, db, pPriorSelectColOld, flags) (*TExpr)(unsafe.Pointer(pNewExpr)).FpRight = pPriorSelectColNew } (*TExpr)(unsafe.Pointer(pNewExpr)).FpLeft = pPriorSelectColNew } } (*TExprList_item)(unsafe.Pointer(pItem)).FzEName = _sqlite3DbStrDup(tls, db, (*TExprList_item)(unsafe.Pointer(pOldItem)).FzEName) (*TExprList_item)(unsafe.Pointer(pItem)).Ffg = (*TExprList_item)(unsafe.Pointer(pOldItem)).Ffg libc.SetBitFieldPtr16Uint32(pItem+8+4, libc.Uint32FromInt32(0), 2, 0x4) (*TExprList_item)(unsafe.Pointer(pItem)).Fu = (*TExprList_item)(unsafe.Pointer(pOldItem)).Fu goto _1 _1: ; i++ pItem += 20 pOldItem += 20 } return pNew } // C documentation // // /* // ** If cursors, triggers, views and subqueries are all omitted from // ** the build, then none of the following routines, except for // ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes // ** called with a NULL argument. // */ func _sqlite3SrcListDup(tls *libc.TLS, db uintptr, p uintptr, flags int32) (r uintptr) { var i, nByte int32 var pNew, pNewItem, pOldItem, pTab, v4 uintptr var v1 uint32 var v2 Tu32 _, _, _, _, _, _, _, _, _ = i, nByte, pNew, pNewItem, pOldItem, pTab, v1, v2, v4 _ = libc.Int32FromInt32(0) if p == uintptr(0) { return uintptr(0) } if (*TSrcList)(unsafe.Pointer(p)).FnSrc > 0 { v1 = uint32(72) * uint32((*TSrcList)(unsafe.Pointer(p)).FnSrc-libc.Int32FromInt32(1)) } else { v1 = uint32(0) } nByte = int32(uint32(80) + v1) pNew = _sqlite3DbMallocRawNN(tls, db, uint64(nByte)) if pNew == uintptr(0) { return uintptr(0) } v2 = uint32((*TSrcList)(unsafe.Pointer(p)).FnSrc) (*TSrcList)(unsafe.Pointer(pNew)).FnAlloc = v2 (*TSrcList)(unsafe.Pointer(pNew)).FnSrc = int32(v2) i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(p)).FnSrc) { break } pNewItem = pNew + 8 + uintptr(i)*72 pOldItem = p + 8 + uintptr(i)*72 (*TSrcItem)(unsafe.Pointer(pNewItem)).FpSchema = (*TSrcItem)(unsafe.Pointer(pOldItem)).FpSchema (*TSrcItem)(unsafe.Pointer(pNewItem)).FzDatabase = _sqlite3DbStrDup(tls, db, (*TSrcItem)(unsafe.Pointer(pOldItem)).FzDatabase) (*TSrcItem)(unsafe.Pointer(pNewItem)).FzName = _sqlite3DbStrDup(tls, db, (*TSrcItem)(unsafe.Pointer(pOldItem)).FzName) (*TSrcItem)(unsafe.Pointer(pNewItem)).FzAlias = _sqlite3DbStrDup(tls, db, (*TSrcItem)(unsafe.Pointer(pOldItem)).FzAlias) (*TSrcItem)(unsafe.Pointer(pNewItem)).Ffg = (*TSrcItem)(unsafe.Pointer(pOldItem)).Ffg (*TSrcItem)(unsafe.Pointer(pNewItem)).FiCursor = (*TSrcItem)(unsafe.Pointer(pOldItem)).FiCursor (*TSrcItem)(unsafe.Pointer(pNewItem)).FaddrFillSub = (*TSrcItem)(unsafe.Pointer(pOldItem)).FaddrFillSub (*TSrcItem)(unsafe.Pointer(pNewItem)).FregReturn = (*TSrcItem)(unsafe.Pointer(pOldItem)).FregReturn if int32(uint32(*(*uint16)(unsafe.Pointer(pNewItem + 36 + 4))&0x2>>1)) != 0 { *(*uintptr)(unsafe.Pointer(pNewItem + 64)) = _sqlite3DbStrDup(tls, db, *(*uintptr)(unsafe.Pointer(pOldItem + 64))) } (*TSrcItem)(unsafe.Pointer(pNewItem)).Fu2 = (*TSrcItem)(unsafe.Pointer(pOldItem)).Fu2 if int32(uint32(*(*uint16)(unsafe.Pointer(pNewItem + 36 + 4))&0x100>>8)) != 0 { (*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNewItem + 68)))).FnUse++ } if int32(uint32(*(*uint16)(unsafe.Pointer(pNewItem + 36 + 4))&0x4>>2)) != 0 { *(*uintptr)(unsafe.Pointer(pNewItem + 64)) = _sqlite3ExprListDup(tls, db, *(*uintptr)(unsafe.Pointer(pOldItem + 64)), flags) } v4 = (*TSrcItem)(unsafe.Pointer(pOldItem)).FpTab (*TSrcItem)(unsafe.Pointer(pNewItem)).FpTab = v4 pTab = v4 if pTab != 0 { (*TTable)(unsafe.Pointer(pTab)).FnTabRef++ } (*TSrcItem)(unsafe.Pointer(pNewItem)).FpSelect = _sqlite3SelectDup(tls, db, (*TSrcItem)(unsafe.Pointer(pOldItem)).FpSelect, flags) if int32(uint32(*(*uint16)(unsafe.Pointer(pOldItem + 36 + 4))&0x400>>10)) != 0 { _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pNewItem + 48)) = _sqlite3IdListDup(tls, db, *(*uintptr)(unsafe.Pointer(pOldItem + 48))) } else { *(*uintptr)(unsafe.Pointer(pNewItem + 48)) = _sqlite3ExprDup(tls, db, *(*uintptr)(unsafe.Pointer(pOldItem + 48)), flags) } (*TSrcItem)(unsafe.Pointer(pNewItem)).FcolUsed = (*TSrcItem)(unsafe.Pointer(pOldItem)).FcolUsed goto _3 _3: ; i++ } return pNew } func _sqlite3IdListDup(tls *libc.TLS, db uintptr, p uintptr) (r uintptr) { var i int32 var pNew, pNewItem, pOldItem uintptr _, _, _, _ = i, pNew, pNewItem, pOldItem _ = libc.Int32FromInt32(0) if p == uintptr(0) { return uintptr(0) } _ = libc.Int32FromInt32(0) pNew = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(16)+uint32((*TIdList)(unsafe.Pointer(p)).FnId-libc.Int32FromInt32(1))*uint32(8))) if pNew == uintptr(0) { return uintptr(0) } (*TIdList)(unsafe.Pointer(pNew)).FnId = (*TIdList)(unsafe.Pointer(p)).FnId (*TIdList)(unsafe.Pointer(pNew)).FeU4 = (*TIdList)(unsafe.Pointer(p)).FeU4 i = 0 for { if !(i < (*TIdList)(unsafe.Pointer(p)).FnId) { break } pNewItem = pNew + 8 + uintptr(i)*8 pOldItem = p + 8 + uintptr(i)*8 (*TIdList_item)(unsafe.Pointer(pNewItem)).FzName = _sqlite3DbStrDup(tls, db, (*TIdList_item)(unsafe.Pointer(pOldItem)).FzName) (*TIdList_item)(unsafe.Pointer(pNewItem)).Fu4 = (*TIdList_item)(unsafe.Pointer(pOldItem)).Fu4 goto _1 _1: ; i++ } return pNew } func _sqlite3SelectDup(tls *libc.TLS, db uintptr, pDup uintptr, flags int32) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var p, pNew, pNext, pp uintptr var _ /* pRet at bp+0 */ uintptr _, _, _, _ = p, pNew, pNext, pp *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) pNext = uintptr(0) pp = bp _ = libc.Int32FromInt32(0) p = pDup for { if !(p != 0) { break } pNew = _sqlite3DbMallocRawNN(tls, db, uint64(76)) if pNew == uintptr(0) { break } (*TSelect)(unsafe.Pointer(pNew)).FpEList = _sqlite3ExprListDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpEList, flags) (*TSelect)(unsafe.Pointer(pNew)).FpSrc = _sqlite3SrcListDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpSrc, flags) (*TSelect)(unsafe.Pointer(pNew)).FpWhere = _sqlite3ExprDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpWhere, flags) (*TSelect)(unsafe.Pointer(pNew)).FpGroupBy = _sqlite3ExprListDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpGroupBy, flags) (*TSelect)(unsafe.Pointer(pNew)).FpHaving = _sqlite3ExprDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpHaving, flags) (*TSelect)(unsafe.Pointer(pNew)).FpOrderBy = _sqlite3ExprListDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpOrderBy, flags) (*TSelect)(unsafe.Pointer(pNew)).Fop = (*TSelect)(unsafe.Pointer(p)).Fop (*TSelect)(unsafe.Pointer(pNew)).FpNext = pNext (*TSelect)(unsafe.Pointer(pNew)).FpPrior = uintptr(0) (*TSelect)(unsafe.Pointer(pNew)).FpLimit = _sqlite3ExprDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpLimit, flags) (*TSelect)(unsafe.Pointer(pNew)).FiLimit = 0 (*TSelect)(unsafe.Pointer(pNew)).FiOffset = 0 (*TSelect)(unsafe.Pointer(pNew)).FselFlags = (*TSelect)(unsafe.Pointer(p)).FselFlags & uint32(^libc.Int32FromInt32(SF_UsesEphemeral)) *(*int32)(unsafe.Pointer(pNew + 20)) = -int32(1) *(*int32)(unsafe.Pointer(pNew + 20 + 1*4)) = -int32(1) (*TSelect)(unsafe.Pointer(pNew)).FnSelectRow = (*TSelect)(unsafe.Pointer(p)).FnSelectRow (*TSelect)(unsafe.Pointer(pNew)).FpWith = _sqlite3WithDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpWith) (*TSelect)(unsafe.Pointer(pNew)).FpWin = uintptr(0) (*TSelect)(unsafe.Pointer(pNew)).FpWinDefn = _sqlite3WindowListDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpWinDefn) if (*TSelect)(unsafe.Pointer(p)).FpWin != 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 { _gatherSelectWindows(tls, pNew) } (*TSelect)(unsafe.Pointer(pNew)).FselId = (*TSelect)(unsafe.Pointer(p)).FselId if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { /* Any prior OOM might have left the Select object incomplete. ** Delete the whole thing rather than allow an incomplete Select ** to be used by the code generator. */ (*TSelect)(unsafe.Pointer(pNew)).FpNext = uintptr(0) _sqlite3SelectDelete(tls, db, pNew) break } *(*uintptr)(unsafe.Pointer(pp)) = pNew pp = pNew + 52 pNext = pNew goto _1 _1: ; p = (*TSelect)(unsafe.Pointer(p)).FpPrior } return *(*uintptr)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Add a new element to the end of an expression list. If pList is // ** initially NULL, then create a new expression list. // ** // ** The pList argument must be either NULL or a pointer to an ExprList // ** obtained from a prior call to sqlite3ExprListAppend(). // ** // ** If a memory allocation error occurs, the entire list is freed and // ** NULL is returned. If non-NULL is returned, then it is guaranteed // ** that the new entry was successfully appended. // */ type TExprList_item = struct { FpExpr uintptr FzEName uintptr Ffg struct { F__ccgo_align [0]uint32 FsortFlags Tu8 F__ccgo_align1 [2]byte F__ccgo4 uint16 } Fu struct { FiConstExprReg [0]int32 Fx struct { FiOrderByCol Tu16 FiAlias Tu16 } } } type ExprList_item = TExprList_item // C documentation // // /* // ** Add a new element to the end of an expression list. If pList is // ** initially NULL, then create a new expression list. // ** // ** The pList argument must be either NULL or a pointer to an ExprList // ** obtained from a prior call to sqlite3ExprListAppend(). // ** // ** If a memory allocation error occurs, the entire list is freed and // ** NULL is returned. If non-NULL is returned, then it is guaranteed // ** that the new entry was successfully appended. // */ var _zeroItem = TExprList_item{} func _sqlite3ExprListAppendNew(tls *libc.TLS, db uintptr, pExpr uintptr) (r uintptr) { var pItem, pList uintptr _, _ = pItem, pList pList = _sqlite3DbMallocRawNN(tls, db, uint64(libc.Uint32FromInt64(28)+libc.Uint32FromInt64(20)*libc.Uint32FromInt32(4))) if pList == uintptr(0) { _sqlite3ExprDelete(tls, db, pExpr) return uintptr(0) } (*TExprList)(unsafe.Pointer(pList)).FnAlloc = int32(4) (*TExprList)(unsafe.Pointer(pList)).FnExpr = int32(1) pItem = pList + 8 *(*TExprList_item)(unsafe.Pointer(pItem)) = _zeroItem (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr = pExpr return pList } func _sqlite3ExprListAppendGrow(tls *libc.TLS, db uintptr, pList uintptr, pExpr uintptr) (r uintptr) { var pItem, pNew, v2 uintptr var v1 int32 _, _, _, _ = pItem, pNew, v1, v2 *(*int32)(unsafe.Pointer(pList + 4)) *= int32(2) pNew = _sqlite3DbRealloc(tls, db, pList, uint64(uint32(28)+uint32((*TExprList)(unsafe.Pointer(pList)).FnAlloc-libc.Int32FromInt32(1))*uint32(20))) if pNew == uintptr(0) { _sqlite3ExprListDelete(tls, db, pList) _sqlite3ExprDelete(tls, db, pExpr) return uintptr(0) } else { pList = pNew } v2 = pList v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ pItem = pList + 8 + uintptr(v1)*20 *(*TExprList_item)(unsafe.Pointer(pItem)) = _zeroItem (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr = pExpr return pList } func _sqlite3ExprListAppend(tls *libc.TLS, pParse uintptr, pList uintptr, pExpr uintptr) (r uintptr) { var pItem, v2 uintptr var v1 int32 _, _, _ = pItem, v1, v2 if pList == uintptr(0) { return _sqlite3ExprListAppendNew(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr) } if (*TExprList)(unsafe.Pointer(pList)).FnAlloc < (*TExprList)(unsafe.Pointer(pList)).FnExpr+int32(1) { return _sqlite3ExprListAppendGrow(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pList, pExpr) } v2 = pList v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ pItem = pList + 8 + uintptr(v1)*20 *(*TExprList_item)(unsafe.Pointer(pItem)) = _zeroItem (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr = pExpr return pList } // C documentation // // /* // ** pColumns and pExpr form a vector assignment which is part of the SET // ** clause of an UPDATE statement. Like this: // ** // ** (a,b,c) = (expr1,expr2,expr3) // ** Or: (a,b,c) = (SELECT x,y,z FROM ....) // ** // ** For each term of the vector assignment, append new entries to the // ** expression list pList. In the case of a subquery on the RHS, append // ** TK_SELECT_COLUMN expressions. // */ func _sqlite3ExprListAppendVector(tls *libc.TLS, pParse uintptr, pList uintptr, pColumns uintptr, pExpr uintptr) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var db, pFirst, pSubExpr uintptr var i, iFirst, n, v1, v2 int32 var v3 bool _, _, _, _, _, _, _, _, _ = db, i, iFirst, n, pFirst, pSubExpr, v1, v2, v3 db = (*TParse)(unsafe.Pointer(pParse)).Fdb if pList != 0 { v1 = (*TExprList)(unsafe.Pointer(pList)).FnExpr } else { v1 = 0 } iFirst = v1 /* pColumns can only be NULL due to an OOM but an OOM will cause an ** exit prior to this routine being invoked */ if pColumns == uintptr(0) { goto vector_append_error } if pExpr == uintptr(0) { goto vector_append_error } /* If the RHS is a vector, then we can immediately check to see that ** the size of the RHS and LHS match. But if the RHS is a SELECT, ** wildcards ("*") in the result set of the SELECT must be expanded before ** we can do the size check, so defer the size check until code generation. */ if v3 = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_SELECT); v3 { v2 = _sqlite3ExprVectorSize(tls, pExpr) n = v2 } if v3 && (*TIdList)(unsafe.Pointer(pColumns)).FnId != v2 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7833, libc.VaList(bp+8, (*TIdList)(unsafe.Pointer(pColumns)).FnId, n)) goto vector_append_error } i = 0 for { if !(i < (*TIdList)(unsafe.Pointer(pColumns)).FnId) { break } pSubExpr = _sqlite3ExprForVectorField(tls, pParse, pExpr, i, (*TIdList)(unsafe.Pointer(pColumns)).FnId) _ = libc.Int32FromInt32(0) if pSubExpr == uintptr(0) { goto _4 } pList = _sqlite3ExprListAppend(tls, pParse, pList, pSubExpr) if pList != 0 { _ = libc.Int32FromInt32(0) (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr((*TExprList)(unsafe.Pointer(pList)).FnExpr-int32(1))*20))).FzEName = (*(*TIdList_item)(unsafe.Pointer(pColumns + 8 + uintptr(i)*8))).FzName (*(*TIdList_item)(unsafe.Pointer(pColumns + 8 + uintptr(i)*8))).FzName = uintptr(0) } goto _4 _4: ; i++ } if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_SELECT) && pList != uintptr(0) { pFirst = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(iFirst)*20))).FpExpr _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Store the SELECT statement in pRight so it will be deleted when ** sqlite3ExprListDelete() is called */ (*TExpr)(unsafe.Pointer(pFirst)).FpRight = pExpr pExpr = uintptr(0) /* Remember the size of the LHS in iTable so that we can check that ** the RHS and LHS sizes match during code generation. */ (*TExpr)(unsafe.Pointer(pFirst)).FiTable = (*TIdList)(unsafe.Pointer(pColumns)).FnId } goto vector_append_error vector_append_error: ; _sqlite3ExprUnmapAndDelete(tls, pParse, pExpr) _sqlite3IdListDelete(tls, db, pColumns) return pList } // C documentation // // /* // ** Set the sort order for the last element on the given ExprList. // */ func _sqlite3ExprListSetSortOrder(tls *libc.TLS, p uintptr, iSortOrder int32, eNulls int32) { var pItem, p1 uintptr _, _ = pItem, p1 if p == uintptr(0) { return } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pItem = p + 8 + uintptr((*TExprList)(unsafe.Pointer(p)).FnExpr-int32(1))*20 _ = libc.Int32FromInt32(0) if iSortOrder == -int32(1) { iSortOrder = SQLITE_SO_ASC } (*TExprList_item)(unsafe.Pointer(pItem)).Ffg.FsortFlags = uint8(iSortOrder) if eNulls != -int32(1) { libc.SetBitFieldPtr16Uint32(pItem+8+4, libc.Uint32FromInt32(1), 5, 0x20) if iSortOrder != eNulls { p1 = pItem + 8 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | libc.Int32FromInt32(KEYINFO_ORDER_BIGNULL)) } } } // C documentation // // /* // ** Set the ExprList.a[].zEName element of the most recently added item // ** on the expression list. // ** // ** pList might be NULL following an OOM error. But pName should never be // ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag // ** is set. // */ func _sqlite3ExprListSetName(tls *libc.TLS, pParse uintptr, pList uintptr, pName uintptr, dequote int32) { var pItem uintptr _ = pItem _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pList != 0 { _ = libc.Int32FromInt32(0) pItem = pList + 8 + uintptr((*TExprList)(unsafe.Pointer(pList)).FnExpr-int32(1))*20 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TExprList_item)(unsafe.Pointer(pItem)).FzEName = _sqlite3DbStrNDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TToken)(unsafe.Pointer(pName)).Fz, uint64((*TToken)(unsafe.Pointer(pName)).Fn)) if dequote != 0 { /* If dequote==0, then pName->z does not point to part of a DDL ** statement handled by the parser. And so no token need be added ** to the token-map. */ _sqlite3Dequote(tls, (*TExprList_item)(unsafe.Pointer(pItem)).FzEName) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenMap(tls, pParse, (*TExprList_item)(unsafe.Pointer(pItem)).FzEName, pName) } } } } // C documentation // // /* // ** Set the ExprList.a[].zSpan element of the most recently added item // ** on the expression list. // ** // ** pList might be NULL following an OOM error. But pSpan should never be // ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag // ** is set. // */ func _sqlite3ExprListSetSpan(tls *libc.TLS, pParse uintptr, pList uintptr, zStart uintptr, zEnd uintptr) { var db, pItem uintptr _, _ = db, pItem db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) if pList != 0 { pItem = pList + 8 + uintptr((*TExprList)(unsafe.Pointer(pList)).FnExpr-int32(1))*20 _ = libc.Int32FromInt32(0) if (*TExprList_item)(unsafe.Pointer(pItem)).FzEName == uintptr(0) { (*TExprList_item)(unsafe.Pointer(pItem)).FzEName = _sqlite3DbSpanDup(tls, db, zStart, zEnd) libc.SetBitFieldPtr16Uint32(pItem+8+4, libc.Uint32FromInt32(ENAME_SPAN), 0, 0x3) } } } // C documentation // // /* // ** If the expression list pEList contains more than iLimit elements, // ** leave an error message in pParse. // */ func _sqlite3ExprListCheckLength(tls *libc.TLS, pParse uintptr, pEList uintptr, zObject uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var mx int32 _ = mx mx = *(*int32)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb + 120 + 2*4)) if pEList != 0 && (*TExprList)(unsafe.Pointer(pEList)).FnExpr > mx { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7863, libc.VaList(bp+8, zObject)) } } // C documentation // // /* // ** Delete an entire expression list. // */ func _exprListDeleteNN(tls *libc.TLS, db uintptr, pList uintptr) { var i, v1 int32 var pItem uintptr _, _, _ = i, pItem, v1 i = (*TExprList)(unsafe.Pointer(pList)).FnExpr pItem = pList + 8 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) for { _sqlite3ExprDelete(tls, db, (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr) if (*TExprList_item)(unsafe.Pointer(pItem)).FzEName != 0 { _sqlite3DbNNFreeNN(tls, db, (*TExprList_item)(unsafe.Pointer(pItem)).FzEName) } pItem += 20 goto _2 _2: ; i-- v1 = i if !(v1 > 0) { break } } _sqlite3DbNNFreeNN(tls, db, pList) } func _sqlite3ExprListDelete(tls *libc.TLS, db uintptr, pList uintptr) { if pList != 0 { _exprListDeleteNN(tls, db, pList) } } func _sqlite3ExprListDeleteGeneric(tls *libc.TLS, db uintptr, pList uintptr) { if pList != 0 { _exprListDeleteNN(tls, db, pList) } } // C documentation // // /* // ** Return the bitwise-OR of all Expr.flags fields in the given // ** ExprList. // */ func _sqlite3ExprListFlags(tls *libc.TLS, pList uintptr) (r Tu32) { var i int32 var m Tu32 var pExpr uintptr _, _, _ = i, m, pExpr m = uint32(0) _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } pExpr = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr _ = libc.Int32FromInt32(0) m |= (*TExpr)(unsafe.Pointer(pExpr)).Fflags goto _1 _1: ; i++ } return m } // C documentation // // /* // ** This is a SELECT-node callback for the expression walker that // ** always "fails". By "fail" in this case, we mean set // ** pWalker->eCode to zero and abort. // ** // ** This callback is used by multiple expression walkers. // */ func _sqlite3SelectWalkFail(tls *libc.TLS, pWalker uintptr, NotUsed uintptr) (r int32) { _ = NotUsed (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0) return int32(WRC_Abort) } // C documentation // // /* // ** Check the input string to see if it is "true" or "false" (in any case). // ** // ** If the string is.... Return // ** "true" EP_IsTrue // ** "false" EP_IsFalse // ** anything else 0 // */ func _sqlite3IsTrueOrFalse(tls *libc.TLS, zIn uintptr) (r Tu32) { if _sqlite3StrICmp(tls, zIn, __ccgo_ts+7886) == 0 { return uint32(EP_IsTrue) } if _sqlite3StrICmp(tls, zIn, __ccgo_ts+7891) == 0 { return uint32(EP_IsFalse) } return uint32(0) } // C documentation // // /* // ** If the input expression is an ID with the name "true" or "false" // ** then convert it into an TK_TRUEFALSE term. Return non-zero if // ** the conversion happened, and zero if the expression is unaltered. // */ func _sqlite3ExprIdToTrueFalse(tls *libc.TLS, pExpr uintptr) (r int32) { var v, v1 Tu32 var v2 bool _, _, _ = v, v1, v2 _ = libc.Int32FromInt32(0) if v2 = !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Quoted)|libc.Int32FromInt32(EP_IntValue)) != libc.Uint32FromInt32(0)); v2 { v1 = _sqlite3IsTrueOrFalse(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8))) v = v1 } if v2 && v1 != uint32(0) { (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_TRUEFALSE) *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= v return int32(1) } return 0 } // C documentation // // /* // ** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE // ** and 0 if it is FALSE. // */ func _sqlite3ExprTruthValue(tls *libc.TLS, pExpr uintptr) (r int32) { pExpr = _sqlite3ExprSkipCollateAndLikely(tls, pExpr) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return libc.BoolInt32(int32(*(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 8)) + 4))) == 0) } // C documentation // // /* // ** If pExpr is an AND or OR expression, try to simplify it by eliminating // ** terms that are always true or false. Return the simplified expression. // ** Or return the original expression if no simplification is possible. // ** // ** Examples: // ** // ** (x<10) AND true => (x<10) // ** (x<10) AND false => false // ** (x<10) AND (y=22 OR false) => (x<10) AND (y=22) // ** (x<10) AND (y=22 OR true) => (x<10) // ** (y=22) OR true => true // */ func _sqlite3ExprSimplifiedAndOr(tls *libc.TLS, pExpr uintptr) (r uintptr) { var pLeft, pRight, v1, v2 uintptr _, _, _, _ = pLeft, pRight, v1, v2 _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AND) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_OR) { pRight = _sqlite3ExprSimplifiedAndOr(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) pLeft = _sqlite3ExprSimplifiedAndOr(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) if (*TExpr)(unsafe.Pointer(pLeft)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_IsTrue)) == uint32(EP_IsTrue) || (*TExpr)(unsafe.Pointer(pRight)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_IsFalse)) == uint32(EP_IsFalse) { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AND) { v1 = pRight } else { v1 = pLeft } pExpr = v1 } else { if (*TExpr)(unsafe.Pointer(pRight)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_IsTrue)) == uint32(EP_IsTrue) || (*TExpr)(unsafe.Pointer(pLeft)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_IsFalse)) == uint32(EP_IsFalse) { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AND) { v2 = pLeft } else { v2 = pRight } pExpr = v2 } } } return pExpr } // C documentation // // /* // ** These routines are Walker callbacks used to check expressions to // ** see if they are "constant" for some definition of constant. The // ** Walker.eCode value determines the type of "constant" we are looking // ** for. // ** // ** These callback routines are used to implement the following: // ** // ** sqlite3ExprIsConstant() pWalker->eCode==1 // ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 // ** sqlite3ExprIsTableConstant() pWalker->eCode==3 // ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 // ** // ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression // ** is found to not be a constant. // ** // ** The sqlite3ExprIsConstantOrFunction() is used for evaluating DEFAULT // ** expressions in a CREATE TABLE statement. The Walker.eCode value is 5 // ** when parsing an existing schema out of the sqlite_schema table and 4 // ** when processing a new CREATE TABLE statement. A bound parameter raises // ** an error for new statements, but is silently converted // ** to NULL for existing schemas. This allows sqlite_schema tables that // ** contain a bound parameter because they were generated by older versions // ** of SQLite to be parsed by newer versions of SQLite without raising a // ** malformed schema error. // */ func _exprNodeIsConstant(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { /* If pWalker->eCode is 2 then any term of the expression that comes from ** the ON or USING clauses of an outer join disqualifies the expression ** from being considered constant. */ if int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) == int32(2) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) { (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0) return int32(WRC_Abort) } switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) { /* Consider functions to be constant if all their arguments are constant ** and either pWalker->eCode==4 or 5 or the function has the ** SQLITE_FUNC_CONST flag. */ case int32(TK_FUNCTION): if (int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) >= int32(4) || (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_ConstFunc)) != uint32(0)) && !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != libc.Uint32FromInt32(0)) { if int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) == int32(5) { *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_FromDDL)) } return WRC_Continue } else { (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0) return int32(WRC_Abort) } fallthrough case int32(TK_ID): /* Convert "true" or "false" in a DEFAULT clause into the ** appropriate TK_TRUEFALSE operator */ if _sqlite3ExprIdToTrueFalse(tls, pExpr) != 0 { return int32(WRC_Prune) } fallthrough case int32(TK_COLUMN): fallthrough case int32(TK_AGG_FUNCTION): fallthrough case int32(TK_AGG_COLUMN): if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_FixedCol)) != uint32(0) && int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) != int32(2) { return WRC_Continue } if int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) == int32(3) && (*TExpr)(unsafe.Pointer(pExpr)).FiTable == *(*int32)(unsafe.Pointer(pWalker + 24)) { return WRC_Continue } fallthrough case int32(TK_IF_NULL_ROW): fallthrough case int32(TK_REGISTER): fallthrough case int32(TK_DOT): (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0) return int32(WRC_Abort) case int32(TK_VARIABLE): if int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) == int32(5) { /* Silently convert bound parameters that appear inside of CREATE ** statements into a NULL when parsing the CREATE statement text out ** of the sqlite_schema table */ (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_NULL) } else { if int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) == int32(4) { /* A bound parameter in a CREATE statement that originates from ** sqlite3_prepare() causes an error */ (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0) return int32(WRC_Abort) } } fallthrough default: /* sqlite3SelectWalkFail() disallows */ /* sqlite3SelectWalkFail() disallows */ return WRC_Continue } return r } func _exprIsConst(tls *libc.TLS, p uintptr, initFlag int32, iCur int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* w at bp+0 */ TWalker (*(*TWalker)(unsafe.Pointer(bp))).FeCode = uint16(initFlag) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_exprNodeIsConstant) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkFail) *(*int32)(unsafe.Pointer(bp + 24)) = iCur _sqlite3WalkExpr(tls, bp, p) return int32((*(*TWalker)(unsafe.Pointer(bp))).FeCode) } // C documentation // // /* // ** Walk an expression tree. Return non-zero if the expression is constant // ** and 0 if it involves variables or function calls. // ** // ** For the purposes of this function, a double-quoted string (ex: "abc") // ** is considered a variable but a single-quoted string (ex: 'abc') is // ** a constant. // */ func _sqlite3ExprIsConstant(tls *libc.TLS, p uintptr) (r int32) { return _exprIsConst(tls, p, int32(1), 0) } // C documentation // // /* // ** Walk an expression tree. Return non-zero if // ** // ** (1) the expression is constant, and // ** (2) the expression does originate in the ON or USING clause // ** of a LEFT JOIN, and // ** (3) the expression does not contain any EP_FixedCol TK_COLUMN // ** operands created by the constant propagation optimization. // ** // ** When this routine returns true, it indicates that the expression // ** can be added to the pParse->pConstExpr list and evaluated once when // ** the prepared statement starts up. See sqlite3ExprCodeRunJustOnce(). // */ func _sqlite3ExprIsConstantNotJoin(tls *libc.TLS, p uintptr) (r int32) { return _exprIsConst(tls, p, int32(2), 0) } // C documentation // // /* // ** Walk an expression tree. Return non-zero if the expression is constant // ** for any single row of the table with cursor iCur. In other words, the // ** expression must not refer to any non-deterministic function nor any // ** table other than iCur. // */ func _sqlite3ExprIsTableConstant(tls *libc.TLS, p uintptr, iCur int32) (r int32) { return _exprIsConst(tls, p, int32(3), iCur) } // C documentation // // /* // ** Check pExpr to see if it is an constraint on the single data source // ** pSrc = &pSrcList->a[iSrc]. In other words, check to see if pExpr // ** constrains pSrc but does not depend on any other tables or data // ** sources anywhere else in the query. Return true (non-zero) if pExpr // ** is a constraint on pSrc only. // ** // ** This is an optimization. False negatives will perhaps cause slower // ** queries, but false positives will yield incorrect answers. So when in // ** doubt, return 0. // ** // ** To be an single-source constraint, the following must be true: // ** // ** (1) pExpr cannot refer to any table other than pSrc->iCursor. // ** // ** (2) pExpr cannot use subqueries or non-deterministic functions. // ** // ** (3) pSrc cannot be part of the left operand for a RIGHT JOIN. // ** (Is there some way to relax this constraint?) // ** // ** (4) If pSrc is the right operand of a LEFT JOIN, then... // ** (4a) pExpr must come from an ON clause.. // ** (4b) and specifically the ON clause associated with the LEFT JOIN. // ** // ** (5) If pSrc is not the right operand of a LEFT JOIN or the left // ** operand of a RIGHT JOIN, then pExpr must be from the WHERE // ** clause, not an ON clause. // ** // ** (6) Either: // ** // ** (6a) pExpr does not originate in an ON or USING clause, or // ** // ** (6b) The ON or USING clause from which pExpr is derived is // ** not to the left of a RIGHT JOIN (or FULL JOIN). // ** // ** Without this restriction, accepting pExpr as a single-table // ** constraint might move the the ON/USING filter expression // ** from the left side of a RIGHT JOIN over to the right side, // ** which leads to incorrect answers. See also restriction (9) // ** on push-down. // */ func _sqlite3ExprIsSingleTableConstraint(tls *libc.TLS, pExpr uintptr, pSrcList uintptr, iSrc int32) (r int32) { var jj int32 var pSrc uintptr _, _ = jj, pSrc pSrc = pSrcList + 8 + uintptr(iSrc)*72 if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&int32(JT_LTORJ) != 0 { return 0 /* rule (3) */ } if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&int32(JT_LEFT) != 0 { if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != libc.Uint32FromInt32(0)) { return 0 } /* rule (4a) */ if *(*int32)(unsafe.Pointer(pExpr + 36)) != (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor { return 0 } /* rule (4b) */ } else { if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) { return 0 } /* rule (5) */ } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != uint32(0) && int32((*(*TSrcItem)(unsafe.Pointer(pSrcList + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 { jj = 0 for { if !(jj < iSrc) { break } if *(*int32)(unsafe.Pointer(pExpr + 36)) == (*(*TSrcItem)(unsafe.Pointer(pSrcList + 8 + uintptr(jj)*72))).FiCursor { if int32((*(*TSrcItem)(unsafe.Pointer(pSrcList + 8 + uintptr(jj)*72))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 { return 0 /* restriction (6) */ } break } goto _1 _1: ; jj++ } } return _sqlite3ExprIsTableConstant(tls, pExpr, (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor) /* rules (1), (2) */ } // C documentation // // /* // ** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy(). // */ func _exprNodeIsConstantOrGroupBy(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var i int32 var p, pColl, pGroupBy uintptr _, _, _, _ = i, p, pColl, pGroupBy pGroupBy = *(*uintptr)(unsafe.Pointer(pWalker + 24)) /* Check if pExpr is identical to any GROUP BY term. If so, consider ** it constant. */ i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr) { break } p = (*(*TExprList_item)(unsafe.Pointer(pGroupBy + 8 + uintptr(i)*20))).FpExpr if _sqlite3ExprCompare(tls, uintptr(0), pExpr, p, -int32(1)) < int32(2) { pColl = _sqlite3ExprNNCollSeq(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, p) if _sqlite3IsBinary(tls, pColl) != 0 { return int32(WRC_Prune) } } goto _1 _1: ; i++ } /* Check if pExpr is a sub-select. If so, consider it variable. */ if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) { (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0) return int32(WRC_Abort) } return _exprNodeIsConstant(tls, pWalker, pExpr) } // C documentation // // /* // ** Walk the expression tree passed as the first argument. Return non-zero // ** if the expression consists entirely of constants or copies of terms // ** in pGroupBy that sort with the BINARY collation sequence. // ** // ** This routine is used to determine if a term of the HAVING clause can // ** be promoted into the WHERE clause. In order for such a promotion to work, // ** the value of the HAVING clause term must be the same for all members of // ** a "group". The requirement that the GROUP BY term must be BINARY // ** assumes that no other collating sequence will have a finer-grained // ** grouping than binary. In other words (A=B COLLATE binary) implies // ** A=B in every other collating sequence. The requirement that the // ** GROUP BY be BINARY is stricter than necessary. It would also work // ** to promote HAVING clauses that use the same alternative collating // ** sequence as the GROUP BY term, but that is much harder to check, // ** alternative collating sequences are uncommon, and this is only an // ** optimization, so we take the easy way out and simply require the // ** GROUP BY to use the BINARY collating sequence. // */ func _sqlite3ExprIsConstantOrGroupBy(tls *libc.TLS, pParse uintptr, p uintptr, pGroupBy uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* w at bp+0 */ TWalker (*(*TWalker)(unsafe.Pointer(bp))).FeCode = uint16(1) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_exprNodeIsConstantOrGroupBy) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 24)) = pGroupBy (*(*TWalker)(unsafe.Pointer(bp))).FpParse = pParse _sqlite3WalkExpr(tls, bp, p) return int32((*(*TWalker)(unsafe.Pointer(bp))).FeCode) } // C documentation // // /* // ** Walk an expression tree for the DEFAULT field of a column definition // ** in a CREATE TABLE statement. Return non-zero if the expression is // ** acceptable for use as a DEFAULT. That is to say, return non-zero if // ** the expression is constant or a function call with constant arguments. // ** Return and 0 if there are any variables. // ** // ** isInit is true when parsing from sqlite_schema. isInit is false when // ** processing a new CREATE TABLE statement. When isInit is true, parameters // ** (such as ? or $abc) in the expression are converted into NULL. When // ** isInit is false, parameters raise an error. Parameters should not be // ** allowed in a CREATE TABLE statement, but some legacy versions of SQLite // ** allowed it, so we need to support it when reading sqlite_schema for // ** backwards compatibility. // ** // ** If isInit is true, set EP_FromDDL on every TK_FUNCTION node. // ** // ** For the purposes of this function, a double-quoted string (ex: "abc") // ** is considered a variable but a single-quoted string (ex: 'abc') is // ** a constant. // */ func _sqlite3ExprIsConstantOrFunction(tls *libc.TLS, p uintptr, isInit Tu8) (r int32) { _ = libc.Int32FromInt32(0) return _exprIsConst(tls, p, int32(4)+int32(isInit), 0) } // C documentation // // /* // ** If the expression p codes a constant integer that is small enough // ** to fit in a 32-bit integer, return 1 and put the value of the integer // ** in *pValue. If the expression is not an integer or if it is too big // ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. // */ func _sqlite3ExprIsInteger(tls *libc.TLS, p uintptr, pValue uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* v at bp+0 */ int32 _ = rc rc = 0 if p == uintptr(0) { return 0 } /* Used to only happen following on OOM */ /* If an expression is an integer literal that fits in a signed 32-bit ** integer, then the EP_IntValue flag will have already been set */ _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_IntValue) != 0 { *(*int32)(unsafe.Pointer(pValue)) = *(*int32)(unsafe.Pointer(p + 8)) return int32(1) } switch int32((*TExpr)(unsafe.Pointer(p)).Fop) { case int32(TK_UPLUS): rc = _sqlite3ExprIsInteger(tls, (*TExpr)(unsafe.Pointer(p)).FpLeft, pValue) case int32(TK_UMINUS): *(*int32)(unsafe.Pointer(bp)) = 0 if _sqlite3ExprIsInteger(tls, (*TExpr)(unsafe.Pointer(p)).FpLeft, bp) != 0 { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pValue)) = -*(*int32)(unsafe.Pointer(bp)) rc = int32(1) } default: break } return rc } // C documentation // // /* // ** Return FALSE if there is no chance that the expression can be NULL. // ** // ** If the expression might be NULL or if the expression is too complex // ** to tell return TRUE. // ** // ** This routine is used as an optimization, to skip OP_IsNull opcodes // ** when we know that a value cannot be NULL. Hence, a false positive // ** (returning TRUE when in fact the expression can never be NULL) might // ** be a small performance hit but is otherwise harmless. On the other // ** hand, a false negative (returning FALSE when the result could be NULL) // ** will likely result in an incorrect answer. So when in doubt, return // ** TRUE. // */ func _sqlite3ExprCanBeNull(tls *libc.TLS, p uintptr) (r int32) { var op Tu8 _ = op _ = libc.Int32FromInt32(0) for int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_UPLUS) || int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_UMINUS) { p = (*TExpr)(unsafe.Pointer(p)).FpLeft _ = libc.Int32FromInt32(0) } op = (*TExpr)(unsafe.Pointer(p)).Fop if int32(op) == int32(TK_REGISTER) { op = (*TExpr)(unsafe.Pointer(p)).Fop2 } switch int32(op) { case int32(TK_INTEGER): fallthrough case int32(TK_STRING): fallthrough case int32(TK_FLOAT): fallthrough case int32(TK_BLOB): return 0 case int32(TK_COLUMN): _ = libc.Int32FromInt32(0) return libc.BoolInt32((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_CanBeNull)) != uint32(0) || (*TExpr)(unsafe.Pointer(p)).Fy.FpTab == uintptr(0) || int32((*TExpr)(unsafe.Pointer(p)).FiColumn) >= 0 && (*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).Fy.FpTab)).FaCol != uintptr(0) && int32((*TExpr)(unsafe.Pointer(p)).FiColumn) < int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).Fy.FpTab)).FnCol) && int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).Fy.FpTab)).FaCol + uintptr((*TExpr)(unsafe.Pointer(p)).FiColumn)*12 + 4))&0xf>>0)) == 0) default: return int32(1) } return r } // C documentation // // /* // ** Return TRUE if the given expression is a constant which would be // ** unchanged by OP_Affinity with the affinity given in the second // ** argument. // ** // ** This routine is used to determine if the OP_Affinity operation // ** can be omitted. When in doubt return FALSE. A false negative // ** is harmless. A false positive, however, can result in the wrong // ** answer. // */ func _sqlite3ExprNeedsNoAffinityChange(tls *libc.TLS, p uintptr, aff uint8) (r int32) { var op Tu8 var unaryMinus int32 _, _ = op, unaryMinus unaryMinus = 0 if int32(aff) == int32(SQLITE_AFF_BLOB) { return int32(1) } for int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_UPLUS) || int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_UMINUS) { if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_UMINUS) { unaryMinus = int32(1) } p = (*TExpr)(unsafe.Pointer(p)).FpLeft } op = (*TExpr)(unsafe.Pointer(p)).Fop if int32(op) == int32(TK_REGISTER) { op = (*TExpr)(unsafe.Pointer(p)).Fop2 } switch int32(op) { case int32(TK_INTEGER): return libc.BoolInt32(int32(aff) >= int32(SQLITE_AFF_NUMERIC)) case int32(TK_FLOAT): return libc.BoolInt32(int32(aff) >= int32(SQLITE_AFF_NUMERIC)) case int32(TK_STRING): return libc.BoolInt32(!(unaryMinus != 0) && int32(aff) == int32(SQLITE_AFF_TEXT)) case int32(TK_BLOB): return libc.BoolInt32(!(unaryMinus != 0)) case int32(TK_COLUMN): _ = libc.Int32FromInt32(0) /* p cannot be part of a CHECK constraint */ return libc.BoolInt32(int32(aff) >= int32(SQLITE_AFF_NUMERIC) && int32((*TExpr)(unsafe.Pointer(p)).FiColumn) < 0) default: return 0 } return r } // C documentation // // /* // ** Return TRUE if the given string is a row-id column name. // */ func _sqlite3IsRowid(tls *libc.TLS, z uintptr) (r int32) { if _sqlite3StrICmp(tls, z, __ccgo_ts+7897) == 0 { return int32(1) } if _sqlite3StrICmp(tls, z, __ccgo_ts+7905) == 0 { return int32(1) } if _sqlite3StrICmp(tls, z, __ccgo_ts+7911) == 0 { return int32(1) } return 0 } // C documentation // // /* // ** Return a pointer to a buffer containing a usable rowid alias for table // ** pTab. An alias is usable if there is not an explicit user-defined column // ** of the same name. // */ func _sqlite3RowidAlias(tls *libc.TLS, pTab uintptr) (r uintptr) { var azOpt [3]uintptr var iCol, ii int32 _, _, _ = azOpt, iCol, ii azOpt = [3]uintptr{ 0: __ccgo_ts + 7897, 1: __ccgo_ts + 7905, 2: __ccgo_ts + 7911, } _ = libc.Int32FromInt32(0) ii = 0 for { if !(ii < int32(libc.Uint32FromInt64(12)/libc.Uint32FromInt64(4))) { break } iCol = 0 for { if !(iCol < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if Xsqlite3_stricmp(tls, azOpt[ii], (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName) == 0 { break } goto _2 _2: ; iCol++ } if iCol == int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { return azOpt[ii] } goto _1 _1: ; ii++ } return uintptr(0) } // C documentation // // /* // ** pX is the RHS of an IN operator. If pX is a SELECT statement // ** that can be simplified to a direct table access, then return // ** a pointer to the SELECT statement. If pX is not a SELECT statement, // ** or if the SELECT statement needs to be materialized into a transient // ** table, then return NULL. // */ func _isCandidateForInOpt(tls *libc.TLS, pX uintptr) (r uintptr) { var i int32 var p, pEList, pRes, pSrc, pTab uintptr _, _, _, _, _, _ = i, p, pEList, pRes, pSrc, pTab if !((*TExpr)(unsafe.Pointer(pX)).Fflags&libc.Uint32FromInt32(EP_xIsSelect) != libc.Uint32FromInt32(0)) { return uintptr(0) } /* Not a subquery */ if (*TExpr)(unsafe.Pointer(pX)).Fflags&uint32(libc.Int32FromInt32(EP_VarSelect)) != uint32(0) { return uintptr(0) } /* Correlated subq */ p = *(*uintptr)(unsafe.Pointer(pX + 20)) if (*TSelect)(unsafe.Pointer(p)).FpPrior != 0 { return uintptr(0) } /* Not a compound SELECT */ if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(libc.Int32FromInt32(SF_Distinct)|libc.Int32FromInt32(SF_Aggregate)) != 0 { return uintptr(0) /* No DISTINCT keyword and no aggregate functions */ } _ = libc.Int32FromInt32(0) /* Has no GROUP BY clause */ if (*TSelect)(unsafe.Pointer(p)).FpLimit != 0 { return uintptr(0) } /* Has no LIMIT clause */ if (*TSelect)(unsafe.Pointer(p)).FpWhere != 0 { return uintptr(0) } /* Has no WHERE clause */ pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc _ = libc.Int32FromInt32(0) if (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc != int32(1) { return uintptr(0) } /* Single term in FROM clause */ if (*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FpSelect != 0 { return uintptr(0) } /* FROM is not a subquery or view */ pTab = (*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FpTab _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* FROM clause is not a view */ if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { return uintptr(0) } /* FROM clause not a virtual table */ pEList = (*TSelect)(unsafe.Pointer(p)).FpEList _ = libc.Int32FromInt32(0) /* All SELECT results must be columns. */ i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } pRes = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FpExpr if int32((*TExpr)(unsafe.Pointer(pRes)).Fop) != int32(TK_COLUMN) { return uintptr(0) } _ = libc.Int32FromInt32(0) /* Not a correlated subquery */ goto _1 _1: ; i++ } return p } // C documentation // // /* // ** Generate code that checks the left-most column of index table iCur to see if // ** it contains any NULL entries. Cause the register at regHasNull to be set // ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull // ** to be set to NULL if iCur contains one or more NULL values. // */ func _sqlite3SetHasNullFlag(tls *libc.TLS, v uintptr, iCur int32, regHasNull int32) { var addr1 int32 _ = addr1 _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regHasNull) addr1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), iCur) _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iCur, 0, regHasNull) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_TYPEOFARG)) _sqlite3VdbeJumpHere(tls, v, addr1) } // C documentation // // /* // ** The argument is an IN operator with a list (not a subquery) on the // ** right-hand side. Return TRUE if that list is constant. // */ func _sqlite3InRhsIsConstant(tls *libc.TLS, pIn uintptr) (r int32) { var pLHS uintptr var res int32 _, _ = pLHS, res _ = libc.Int32FromInt32(0) pLHS = (*TExpr)(unsafe.Pointer(pIn)).FpLeft (*TExpr)(unsafe.Pointer(pIn)).FpLeft = uintptr(0) res = _sqlite3ExprIsConstant(tls, pIn) (*TExpr)(unsafe.Pointer(pIn)).FpLeft = pLHS return res } // C documentation // // /* // ** This function is used by the implementation of the IN (...) operator. // ** The pX parameter is the expression on the RHS of the IN operator, which // ** might be either a list of expressions or a subquery. // ** // ** The job of this routine is to find or create a b-tree object that can // ** be used either to test for membership in the RHS set or to iterate through // ** all members of the RHS set, skipping duplicates. // ** // ** A cursor is opened on the b-tree object that is the RHS of the IN operator // ** and the *piTab parameter is set to the index of that cursor. // ** // ** The returned value of this function indicates the b-tree type, as follows: // ** // ** IN_INDEX_ROWID - The cursor was opened on a database table. // ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index. // ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index. // ** IN_INDEX_EPH - The cursor was opened on a specially created and // ** populated ephemeral table. // ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be // ** implemented as a sequence of comparisons. // ** // ** An existing b-tree might be used if the RHS expression pX is a simple // ** subquery such as: // ** // ** SELECT , ... FROM // ** // ** If the RHS of the IN operator is a list or a more complex subquery, then // ** an ephemeral table might need to be generated from the RHS and then // ** pX->iTable made to point to the ephemeral table instead of an // ** existing table. In this case, the creation and initialization of the // ** ephemeral table might be put inside of a subroutine, the EP_Subrtn flag // ** will be set on pX and the pX->y.sub fields will be set to show where // ** the subroutine is coded. // ** // ** The inFlags parameter must contain, at a minimum, one of the bits // ** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains // ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast // ** membership test. When the IN_INDEX_LOOP bit is set, the IN index will // ** be used to loop over all values of the RHS of the IN operator. // ** // ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate // ** through the set members) then the b-tree must not contain duplicates. // ** An ephemeral table will be created unless the selected columns are guaranteed // ** to be unique - either because it is an INTEGER PRIMARY KEY or due to // ** a UNIQUE constraint or index. // ** // ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used // ** for fast set membership tests) then an ephemeral table must // ** be used unless is a single INTEGER PRIMARY KEY column or an // ** index can be found with the specified as its left-most. // ** // ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and // ** if the RHS of the IN operator is a list (not a subquery) then this // ** routine might decide that creating an ephemeral b-tree for membership // ** testing is too expensive and return IN_INDEX_NOOP. In that case, the // ** calling routine should implement the IN operator using a sequence // ** of Eq or Ne comparison operations. // ** // ** When the b-tree is being used for membership tests, the calling function // ** might need to know whether or not the RHS side of the IN operator // ** contains a NULL. If prRhsHasNull is not a NULL pointer and // ** if there is any chance that the (...) might contain a NULL value at // ** runtime, then a register is allocated and the register number written // ** to *prRhsHasNull. If there is no chance that the (...) contains a // ** NULL value, then *prRhsHasNull is left unchanged. // ** // ** If a register is allocated and its location stored in *prRhsHasNull, then // ** the value in that register will be NULL if the b-tree contains one or more // ** NULL values, and it will be some non-NULL value if the b-tree contains no // ** NULL values. // ** // ** If the aiMap parameter is not NULL, it must point to an array containing // ** one element for each column returned by the SELECT statement on the RHS // ** of the IN(...) operator. The i'th entry of the array is populated with the // ** offset of the index column that matches the i'th column returned by the // ** SELECT. For example, if the expression and selected index are: // ** // ** (?,?,?) IN (SELECT a, b, c FROM t1) // ** CREATE INDEX i1 ON t1(b, c, a); // ** // ** then aiMap[] is populated with {2, 0, 1}. // */ func _sqlite3FindInIndex(tls *libc.TLS, pParse uintptr, pX uintptr, inFlags Tu32, prRhsHasNull uintptr, aiMap uintptr, piTab uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var affinity_ok, eType, i, i1, i2, iAddr, iAddr1, iCol, iDb, iTab, j, mustBeUnique, n, nExpr, rMayHaveNull, v1, v10, v12, v13 int32 var cmpaff, idxaff uint8 var colUsed, mCol TBitmask var db, p, pEList, pEList1, pIdx, pLhs, pLhs1, pReq, pRhs, pTab, v, v11, v14, v2, v4 uintptr var savedNQueryLoop Tu32 var v5 bool _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = affinity_ok, cmpaff, colUsed, db, eType, i, i1, i2, iAddr, iAddr1, iCol, iDb, iTab, idxaff, j, mCol, mustBeUnique, n, nExpr, p, pEList, pEList1, pIdx, pLhs, pLhs1, pReq, pRhs, pTab, rMayHaveNull, savedNQueryLoop, v, v1, v10, v11, v12, v13, v14, v2, v4, v5 /* SELECT to the right of IN operator */ eType = 0 /* True if RHS must be unique */ v = _sqlite3GetVdbe(tls, pParse) /* Virtual machine being coded */ _ = libc.Int32FromInt32(0) mustBeUnique = libc.BoolInt32(inFlags&uint32(IN_INDEX_LOOP) != uint32(0)) v2 = pParse + 40 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ iTab = v1 /* If the RHS of this IN(...) operator is a SELECT, and if it matters ** whether or not the SELECT result contains NULL values, check whether ** or not NULL is actually possible (it may not be, for example, due ** to NOT NULL constraints in the schema). If no NULL values are possible, ** set prRhsHasNull to 0 before continuing. */ if prRhsHasNull != 0 && (*TExpr)(unsafe.Pointer(pX)).Fflags&uint32(EP_xIsSelect) != uint32(0) { pEList = (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pX + 20)))).FpEList i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } if _sqlite3ExprCanBeNull(tls, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FpExpr) != 0 { break } goto _3 _3: ; i++ } if i == (*TExprList)(unsafe.Pointer(pEList)).FnExpr { prRhsHasNull = uintptr(0) } } /* Check to see if an existing table or index can be used to ** satisfy the query. This is preferable to generating a new ** ephemeral table. */ if v5 = (*TParse)(unsafe.Pointer(pParse)).FnErr == 0; v5 { v4 = _isCandidateForInOpt(tls, pX) p = v4 } if v5 && v4 != uintptr(0) { db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Database idx for pTab */ pEList1 = (*TSelect)(unsafe.Pointer(p)).FpEList nExpr = (*TExprList)(unsafe.Pointer(pEList1)).FnExpr _ = libc.Int32FromInt32(0) /* Because of isCandidateForInOpt(p) */ _ = libc.Int32FromInt32(0) /* Because of isCandidateForInOpt(p) */ _ = libc.Int32FromInt32(0) /* Because of isCandidateForInOpt(p) */ pTab = (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpTab /* Code an OP_Transaction and OP_TableLock for
    . */ iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) _ = libc.Int32FromInt32(0) _sqlite3CodeVerifySchema(tls, pParse, iDb) _sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pTab)).FzName) _ = libc.Int32FromInt32(0) /* sqlite3GetVdbe() has always been previously called */ if nExpr == int32(1) && int32((*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pEList1 + 8))).FpExpr)).FiColumn) < 0 { /* The "x IN (SELECT rowid FROM table)" case */ iAddr = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once)) _sqlite3OpenTable(tls, pParse, iTab, iDb, pTab, int32(OP_OpenRead)) eType = int32(IN_INDEX_ROWID) _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+7915, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) _sqlite3VdbeJumpHere(tls, v, iAddr) } else { /* Iterator variable */ affinity_ok = int32(1) /* Check that the affinity that will be used to perform each ** comparison is the same as the affinity of each column in table ** on the RHS of the IN operator. If it not, it is not possible to ** use any index of the RHS table. */ i1 = 0 for { if !(i1 < nExpr && affinity_ok != 0) { break } pLhs = _sqlite3VectorFieldSubexpr(tls, (*TExpr)(unsafe.Pointer(pX)).FpLeft, i1) iCol = int32((*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pEList1 + 8 + uintptr(i1)*20))).FpExpr)).FiColumn) idxaff = _sqlite3TableColumnAffinity(tls, pTab, iCol) /* RHS table */ cmpaff = _sqlite3CompareAffinity(tls, pLhs, idxaff) switch int32(cmpaff) { case int32(SQLITE_AFF_BLOB): case int32(SQLITE_AFF_TEXT): /* sqlite3CompareAffinity() only returns TEXT if one side or the ** other has no affinity and the other side is TEXT. Hence, ** the only way for cmpaff to be TEXT is for idxaff to be TEXT ** and for the term on the LHS of the IN to have no affinity. */ _ = libc.Int32FromInt32(0) default: affinity_ok = libc.BoolInt32(int32(idxaff) >= int32(SQLITE_AFF_NUMERIC)) } goto _6 _6: ; i1++ } if affinity_ok != 0 { /* Search for an existing index that will work for this IN operator */ pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0 && eType == 0) { break } /* Mask for the current column */ if int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) < nExpr { goto _7 } if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != uintptr(0) { goto _7 } /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute ** BITMASK(nExpr) without overflowing */ if int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) { goto _7 } if mustBeUnique != 0 { if int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) > nExpr || int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) > nExpr && !(int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != libc.Int32FromInt32(OE_None)) { goto _7 /* This index is not unique over the IN RHS columns */ } } colUsed = uint64(0) /* Columns of index used so far */ i1 = 0 for { if !(i1 < nExpr) { break } pLhs1 = _sqlite3VectorFieldSubexpr(tls, (*TExpr)(unsafe.Pointer(pX)).FpLeft, i1) pRhs = (*(*TExprList_item)(unsafe.Pointer(pEList1 + 8 + uintptr(i1)*20))).FpExpr pReq = _sqlite3BinaryCompareCollSeq(tls, pParse, pLhs1, pRhs) j = 0 for { if !(j < nExpr) { break } if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j)*2))) != int32((*TExpr)(unsafe.Pointer(pRhs)).FiColumn) { goto _9 } _ = libc.Int32FromInt32(0) if pReq != uintptr(0) && _sqlite3StrICmp(tls, (*TCollSeq)(unsafe.Pointer(pReq)).FzName, *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(j)*4))) != 0 { goto _9 } break goto _9 _9: ; j++ } if j == nExpr { break } mCol = libc.Uint64FromInt32(1) << j if mCol&colUsed != 0 { break } /* Each column used only once */ colUsed |= mCol if aiMap != 0 { *(*int32)(unsafe.Pointer(aiMap + uintptr(i1)*4)) = j } goto _8 _8: ; i1++ } _ = libc.Int32FromInt32(0) if colUsed == libc.Uint64FromInt32(1)<iTable, // ** however the cursor number returned might not be the same, as it might // ** have been duplicated using OP_OpenDup. // ** // ** If the LHS expression ("x" in the examples) is a column value, or // ** the SELECT statement returns a column value, then the affinity of that // ** column is used to build the index keys. If both 'x' and the // ** SELECT... statement are columns, then numeric affinity is used // ** if either column has NUMERIC or INTEGER affinity. If neither // ** 'x' nor the SELECT... statement are columns, then numeric affinity // ** is used. // */ func _sqlite3CodeRhsOfIN(tls *libc.TLS, pParse uintptr, pExpr uintptr, iTab int32) { bp := tls.Alloc(64) defer tls.Free(64) var addr, addrOnce, i, i1, nVal, r1, r2, rc, v1, v4 int32 var p, pCopy, pE2, pEList, pItem, pKeyInfo, pLeft, pList, pSelect, v, v2, v3 uintptr var _ /* affinity at bp+28 */ uint8 var _ /* dest at bp+0 */ TSelectDest _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, addrOnce, i, i1, nVal, p, pCopy, pE2, pEList, pItem, pKeyInfo, pLeft, pList, pSelect, r1, r2, rc, v, v1, v2, v3, v4 addrOnce = 0 /* the LHS of the IN operator */ pKeyInfo = uintptr(0) /* The prepared statement under construction */ v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) /* The evaluation of the IN must be repeated every time it ** is encountered if any of the following is true: ** ** * The right-hand side is a correlated subquery ** * The right-hand side is an expression list containing variables ** * We are inside a trigger ** ** If all of the above are false, then we can compute the RHS just once ** and reuse it many names. */ if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_VarSelect)) != libc.Uint32FromInt32(0)) && (*TParse)(unsafe.Pointer(pParse)).FiSelfTab == 0 { /* Reuse of the RHS is allowed */ /* If this routine has already been coded, but the previous code ** might not have been invoked yet, so invoke it now as a subroutine. */ if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Subrtn)) != uint32(0) { addrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once)) if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) { _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+8037, libc.VaList(bp+40, (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FselId)) } _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 44))).FregReturn, (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 44))).FiAddr) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), iTab, (*TExpr)(unsafe.Pointer(pExpr)).FiTable) _sqlite3VdbeJumpHere(tls, v, addrOnce) return } /* Begin coding the subroutine */ _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_Subrtn)) _ = libc.Int32FromInt32(0) v2 = pParse + 44 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 44))).FregReturn = v1 (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 44))).FiAddr = _sqlite3VdbeAddOp2(tls, v, int32(OP_BeginSubrtn), 0, (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 44))).FregReturn) + int32(1) addrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once)) } /* Check to see if this is a vector IN operator */ pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft nVal = _sqlite3ExprVectorSize(tls, pLeft) /* Construct the ephemeral table that will contain the content of ** RHS of the IN operator. */ (*TExpr)(unsafe.Pointer(pExpr)).FiTable = iTab addr = _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (*TExpr)(unsafe.Pointer(pExpr)).FiTable, nVal) pKeyInfo = _sqlite3KeyInfoAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, nVal, int32(1)) if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) { /* Case 1: expr IN (SELECT ...) ** ** Generate code to write the results of the select into the temporary ** table allocated and opened above. */ pSelect = *(*uintptr)(unsafe.Pointer(pExpr + 20)) pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList if addrOnce != 0 { v3 = __ccgo_ts + 1648 } else { v3 = __ccgo_ts + 8060 } _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+8072, libc.VaList(bp+40, v3, (*TSelect)(unsafe.Pointer(pSelect)).FselId)) /* If the LHS and RHS of the IN operator do not match, that ** error will have been caught long before we reach this point. */ if (*TExprList)(unsafe.Pointer(pEList)).FnExpr == nVal { _sqlite3SelectDestInit(tls, bp, int32(SRT_Set), iTab) (*(*TSelectDest)(unsafe.Pointer(bp))).FzAffSdst = _exprINAffinity(tls, pParse, pExpr) (*TSelect)(unsafe.Pointer(pSelect)).FiLimit = 0 /* Caused by OOM in sqlite3KeyInfoAlloc() */ pCopy = _sqlite3SelectDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSelect, 0) if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 { v4 = int32(1) } else { v4 = _sqlite3Select(tls, pParse, pCopy, bp) } rc = v4 _sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pCopy) _sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*(*TSelectDest)(unsafe.Pointer(bp))).FzAffSdst) if rc != 0 { _sqlite3KeyInfoUnref(tls, pKeyInfo) return } _ = libc.Int32FromInt32(0) /* OOM will cause exit after sqlite3Select() */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) i = 0 for { if !(i < nVal) { break } p = _sqlite3VectorFieldSubexpr(tls, pLeft, i) *(*uintptr)(unsafe.Pointer(pKeyInfo + 20 + uintptr(i)*4)) = _sqlite3BinaryCompareCollSeq(tls, pParse, p, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FpExpr) goto _5 _5: ; i++ } } } else { if *(*uintptr)(unsafe.Pointer(pExpr + 20)) != uintptr(0) { pList = *(*uintptr)(unsafe.Pointer(pExpr + 20)) *(*uint8)(unsafe.Pointer(bp + 28)) = _sqlite3ExprAffinity(tls, pLeft) if int32(*(*uint8)(unsafe.Pointer(bp + 28))) <= int32(SQLITE_AFF_NONE) { *(*uint8)(unsafe.Pointer(bp + 28)) = uint8(SQLITE_AFF_BLOB) } else { if int32(*(*uint8)(unsafe.Pointer(bp + 28))) == int32(SQLITE_AFF_REAL) { *(*uint8)(unsafe.Pointer(bp + 28)) = uint8(SQLITE_AFF_NUMERIC) } } if pKeyInfo != 0 { _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pKeyInfo + 20)) = _sqlite3ExprCollSeq(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) } /* Loop through each expression in . */ r1 = _sqlite3GetTempReg(tls, pParse) r2 = _sqlite3GetTempReg(tls, pParse) i1 = (*TExprList)(unsafe.Pointer(pList)).FnExpr pItem = pList + 8 for { if !(i1 > 0) { break } pE2 = (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr /* If the expression is not constant then we will need to ** disable the test that was generated above that makes sure ** this code only executes once. Because for a non-constant ** expression we need to rerun this code each time. */ if addrOnce != 0 && !(_sqlite3ExprIsConstant(tls, pE2) != 0) { _sqlite3VdbeChangeToNoop(tls, v, addrOnce-int32(1)) _sqlite3VdbeChangeToNoop(tls, v, addrOnce) *(*Tu32)(unsafe.Pointer(pExpr + 4)) &= uint32(^libc.Int32FromInt32(EP_Subrtn)) addrOnce = 0 } /* Evaluate the expression and insert it into the temp table */ _sqlite3ExprCode(tls, pParse, pE2, r1) _sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), r1, int32(1), r2, bp+28, int32(1)) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iTab, r2, r1, int32(1)) goto _6 _6: ; i1-- pItem += 20 } _sqlite3ReleaseTempReg(tls, pParse, r1) _sqlite3ReleaseTempReg(tls, pParse, r2) } } if pKeyInfo != 0 { _sqlite3VdbeChangeP4(tls, v, addr, pKeyInfo, -int32(8)) } if addrOnce != 0 { _sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), iTab) _sqlite3VdbeJumpHere(tls, v, addrOnce) /* Subroutine return */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, int32(OP_Return), (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 44))).FregReturn, (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 44))).FiAddr, int32(1)) _sqlite3ClearTempRegCache(tls, pParse) } } // C documentation // // /* // ** Generate code for scalar subqueries used as a subquery expression // ** or EXISTS operator: // ** // ** (SELECT a FROM b) -- subquery // ** EXISTS (SELECT a FROM b) -- EXISTS subquery // ** // ** The pExpr parameter is the SELECT or EXISTS operator to be coded. // ** // ** Return the register that holds the result. For a multi-column SELECT, // ** the result is stored in a contiguous array of registers and the // ** return value is the register of the left-most result column. // ** Return 0 if an error occurs. // */ func _sqlite3CodeSubselect(tls *libc.TLS, pParse uintptr, pExpr uintptr) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var addrOnce, nReg, rReg, v1, v4, v5 int32 var db, pLimit, pSel, v, v2, v3 uintptr var _ /* dest at bp+0 */ TSelectDest _, _, _, _, _, _, _, _, _, _, _, _ = addrOnce, db, nReg, pLimit, pSel, rReg, v, v1, v2, v3, v4, v5 addrOnce = 0 /* Address of OP_Once at top of subroutine */ rReg = 0 /* New limit expression */ v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return 0 } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pSel = *(*uintptr)(unsafe.Pointer(pExpr + 20)) /* If this routine has already been coded, then invoke it as a ** subroutine. */ if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Subrtn)) != uint32(0) { _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+8091, libc.VaList(bp+40, (*TSelect)(unsafe.Pointer(pSel)).FselId)) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 44))).FregReturn, (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 44))).FiAddr) return (*TExpr)(unsafe.Pointer(pExpr)).FiTable } /* Begin coding the subroutine */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_Subrtn)) v2 = pParse + 44 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 44))).FregReturn = v1 (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 44))).FiAddr = _sqlite3VdbeAddOp2(tls, v, int32(OP_BeginSubrtn), 0, (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 44))).FregReturn) + int32(1) /* The evaluation of the EXISTS/SELECT must be repeated every time it ** is encountered if any of the following is true: ** ** * The right-hand side is a correlated subquery ** * The right-hand side is an expression list containing variables ** * We are inside a trigger ** ** If all of the above are false, then we can run this code just once ** save the results, and reuse the same result on subsequent invocations. */ if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_VarSelect)) != libc.Uint32FromInt32(0)) { addrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once)) } /* For a SELECT, generate code to put the values for all columns of ** the first row into an array of registers and return the index of ** the first register. ** ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists) ** into a register and return that register number. ** ** In both cases, the query is augmented with "LIMIT 1". Any ** preexisting limit is discarded in place of the new LIMIT 1. */ if addrOnce != 0 { v3 = __ccgo_ts + 1648 } else { v3 = __ccgo_ts + 8060 } _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+8109, libc.VaList(bp+40, v3, (*TSelect)(unsafe.Pointer(pSel)).FselId)) if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_SELECT) { v4 = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSel)).FpEList)).FnExpr } else { v4 = int32(1) } nReg = v4 _sqlite3SelectDestInit(tls, bp, 0, (*TParse)(unsafe.Pointer(pParse)).FnMem+int32(1)) *(*int32)(unsafe.Pointer(pParse + 44)) += nReg if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_SELECT) { (*(*TSelectDest)(unsafe.Pointer(bp))).FeDest = uint8(SRT_Mem) (*(*TSelectDest)(unsafe.Pointer(bp))).FiSdst = (*(*TSelectDest)(unsafe.Pointer(bp))).FiSDParm (*(*TSelectDest)(unsafe.Pointer(bp))).FnSdst = nReg _sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, (*(*TSelectDest)(unsafe.Pointer(bp))).FiSDParm, (*(*TSelectDest)(unsafe.Pointer(bp))).FiSDParm+nReg-int32(1)) } else { (*(*TSelectDest)(unsafe.Pointer(bp))).FeDest = uint8(SRT_Exists) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (*(*TSelectDest)(unsafe.Pointer(bp))).FiSDParm) } if (*TSelect)(unsafe.Pointer(pSel)).FpLimit != 0 { /* The subquery already has a limit. If the pre-existing limit is X ** then make the new limit X<>0 so that the new limit is either 1 or 0 */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb pLimit = _sqlite3Expr(tls, db, int32(TK_INTEGER), __ccgo_ts+1722) if pLimit != 0 { (*TExpr)(unsafe.Pointer(pLimit)).FaffExpr = uint8(SQLITE_AFF_NUMERIC) pLimit = _sqlite3PExpr(tls, pParse, int32(TK_NE), _sqlite3ExprDup(tls, db, (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSel)).FpLimit)).FpLeft, 0), pLimit) } _sqlite3ExprDeferredDelete(tls, pParse, (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSel)).FpLimit)).FpLeft) (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSel)).FpLimit)).FpLeft = pLimit } else { /* If there is no pre-existing limit add a limit of 1 */ pLimit = _sqlite3Expr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_INTEGER), __ccgo_ts+8130) (*TSelect)(unsafe.Pointer(pSel)).FpLimit = _sqlite3PExpr(tls, pParse, int32(TK_LIMIT), pLimit, uintptr(0)) } (*TSelect)(unsafe.Pointer(pSel)).FiLimit = 0 if _sqlite3Select(tls, pParse, pSel, bp) != 0 { (*TExpr)(unsafe.Pointer(pExpr)).Fop2 = (*TExpr)(unsafe.Pointer(pExpr)).Fop (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_ERROR) return 0 } v5 = (*(*TSelectDest)(unsafe.Pointer(bp))).FiSDParm rReg = v5 (*TExpr)(unsafe.Pointer(pExpr)).FiTable = v5 if addrOnce != 0 { _sqlite3VdbeJumpHere(tls, v, addrOnce) } /* Subroutine return */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, int32(OP_Return), (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 44))).FregReturn, (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 44))).FiAddr, int32(1)) _sqlite3ClearTempRegCache(tls, pParse) return rReg } // C documentation // // /* // ** Expr pIn is an IN(...) expression. This function checks that the // ** sub-select on the RHS of the IN() operator has the same number of // ** columns as the vector on the LHS. Or, if the RHS of the IN() is not // ** a sub-query, that the LHS is a vector of size 1. // */ func _sqlite3ExprCheckIN(tls *libc.TLS, pParse uintptr, pIn uintptr) (r int32) { var nVector int32 _ = nVector nVector = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pIn)).FpLeft) if (*TExpr)(unsafe.Pointer(pIn)).Fflags&uint32(EP_xIsSelect) != uint32(0) && !((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0) { if nVector != (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pIn + 20)))).FpEList)).FnExpr { _sqlite3SubselectError(tls, pParse, (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pIn + 20)))).FpEList)).FnExpr, nVector) return int32(1) } } else { if nVector != int32(1) { _sqlite3VectorErrorMsg(tls, pParse, (*TExpr)(unsafe.Pointer(pIn)).FpLeft) return int32(1) } } return 0 } // C documentation // // /* // ** Generate code for an IN expression. // ** // ** x IN (SELECT ...) // ** x IN (value, value, ...) // ** // ** The left-hand side (LHS) is a scalar or vector expression. The // ** right-hand side (RHS) is an array of zero or more scalar values, or a // ** subquery. If the RHS is a subquery, the number of result columns must // ** match the number of columns in the vector on the LHS. If the RHS is // ** a list of values, the LHS must be a scalar. // ** // ** The IN operator is true if the LHS value is contained within the RHS. // ** The result is false if the LHS is definitely not in the RHS. The // ** result is NULL if the presence of the LHS in the RHS cannot be // ** determined due to NULLs. // ** // ** This routine generates code that jumps to destIfFalse if the LHS is not // ** contained within the RHS. If due to NULLs we cannot determine if the LHS // ** is contained in the RHS then jump to destIfNull. If the LHS is contained // ** within the RHS then fall through. // ** // ** See the separate in-operator.md documentation file in the canonical // ** SQLite source tree for additional information. // */ func _sqlite3ExprCodeIN(tls *libc.TLS, pParse uintptr, pExpr uintptr, destIfFalse int32, destIfNull int32) { bp := tls.Alloc(16) defer tls.Free(16) var addrTop, addrTruthOp, destNotNull, destStep2, destStep6, eType, i, ii, labelOk, nVector, op, op1, r2, r3, rLhs, rLhsOrig, regCkNull, v5, v6, v7 int32 var aiMap, p, p1, pColl, pColl1, pLeft, pList, v, zAff, v1 uintptr var okConstFactor Tu8 var _ /* iDummy at bp+4 */ int32 var _ /* iTab at bp+8 */ int32 var _ /* rRhsHasNull at bp+0 */ int32 var _ /* regToFree at bp+12 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrTop, addrTruthOp, aiMap, destNotNull, destStep2, destStep6, eType, i, ii, labelOk, nVector, okConstFactor, op, op1, p, p1, pColl, pColl1, pLeft, pList, r2, r3, rLhs, rLhsOrig, regCkNull, v, zAff, v1, v5, v6, v7 *(*int32)(unsafe.Pointer(bp)) = 0 /* Statement under construction */ aiMap = uintptr(0) /* Map from vector field to index column */ zAff = uintptr(0) /* Where to jump when NULLs seen in step 2 */ destStep6 = 0 /* Top of the step-6 loop */ *(*int32)(unsafe.Pointer(bp + 8)) = 0 /* Index to use */ okConstFactor = (*TParse)(unsafe.Pointer(pParse)).FokConstFactor _ = libc.Int32FromInt32(0) pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft if _sqlite3ExprCheckIN(tls, pParse, pExpr) != 0 { return } zAff = _exprINAffinity(tls, pParse, pExpr) nVector = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) aiMap = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(nVector)*(libc.Uint32FromInt64(4)+libc.Uint32FromInt64(1))+uint32(1))) if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 { goto sqlite3ExprCodeIN_oom_error } /* Attempt to compute the RHS. After this step, if anything other than ** IN_INDEX_NOOP is returned, the table opened with cursor iTab ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned, ** the RHS has not yet been coded. */ v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) /* OOM detected prior to this routine */ if destIfFalse == destIfNull { v1 = uintptr(0) } else { v1 = bp } eType = _sqlite3FindInIndex(tls, pParse, pExpr, uint32(libc.Int32FromInt32(IN_INDEX_MEMBERSHIP)|libc.Int32FromInt32(IN_INDEX_NOOP_OK)), v1, aiMap, bp+8) _ = libc.Int32FromInt32(0) /* Code the LHS, the from " IN (...)". If the LHS is a ** vector, then it is stored in an array of nVector registers starting ** at r1. ** ** sqlite3FindInIndex() might have reordered the fields of the LHS vector ** so that the fields are in the same order as an existing index. The ** aiMap[] array contains a mapping from the original LHS field order to ** the field order that matches the RHS index. ** ** Avoid factoring the LHS of the IN(...) expression out of the loop, ** even if it is constant, as OP_Affinity may be used on the register ** by code generated below. */ _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).FokConstFactor = uint8(0) rLhsOrig = _exprCodeVector(tls, pParse, pLeft, bp+4) (*TParse)(unsafe.Pointer(pParse)).FokConstFactor = okConstFactor i = 0 for { if !(i < nVector && *(*int32)(unsafe.Pointer(aiMap + uintptr(i)*4)) == i) { break } goto _2 _2: ; i++ } /* Are LHS fields reordered? */ if i == nVector { /* LHS fields are not reordered */ rLhs = rLhsOrig } else { /* Need to reorder the LHS fields according to aiMap */ rLhs = _sqlite3GetTempRange(tls, pParse, nVector) i = 0 for { if !(i < nVector) { break } _sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), rLhsOrig+i, rLhs+*(*int32)(unsafe.Pointer(aiMap + uintptr(i)*4)), 0) goto _3 _3: ; i++ } } /* If sqlite3FindInIndex() did not find or create an index that is ** suitable for evaluating the IN operator, then evaluate using a ** sequence of comparisons. ** ** This is step (1) in the in-operator.md optimized algorithm. */ if eType == int32(IN_INDEX_NOOP) { labelOk = _sqlite3VdbeMakeLabel(tls, pParse) regCkNull = 0 _ = libc.Int32FromInt32(0) pList = *(*uintptr)(unsafe.Pointer(pExpr + 20)) pColl = _sqlite3ExprCollSeq(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) if destIfNull != destIfFalse { regCkNull = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp3(tls, v, int32(OP_BitAnd), rLhs, rLhs, regCkNull) } ii = 0 for { if !(ii < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } r2 = _sqlite3ExprCodeTemp(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*20))).FpExpr, bp+12) if regCkNull != 0 && _sqlite3ExprCanBeNull(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*20))).FpExpr) != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_BitAnd), regCkNull, r2, regCkNull) } _sqlite3ReleaseTempReg(tls, pParse, *(*int32)(unsafe.Pointer(bp + 12))) if ii < (*TExprList)(unsafe.Pointer(pList)).FnExpr-int32(1) || destIfNull != destIfFalse { if rLhs != r2 { v5 = int32(OP_Eq) } else { v5 = int32(OP_NotNull) } op = v5 _sqlite3VdbeAddOp4(tls, v, op, rLhs, labelOk, r2, pColl, -int32(2)) _sqlite3VdbeChangeP5(tls, v, uint16(*(*uint8)(unsafe.Pointer(zAff)))) } else { if rLhs != r2 { v6 = int32(OP_Ne) } else { v6 = int32(OP_IsNull) } op1 = v6 _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp4(tls, v, op1, rLhs, destIfFalse, r2, pColl, -int32(2)) _sqlite3VdbeChangeP5(tls, v, uint16(int32(*(*uint8)(unsafe.Pointer(zAff)))|int32(SQLITE_JUMPIFNULL))) } goto _4 _4: ; ii++ } if regCkNull != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regCkNull, destIfNull) _sqlite3VdbeGoto(tls, v, destIfFalse) } _sqlite3VdbeResolveLabel(tls, v, labelOk) _sqlite3ReleaseTempReg(tls, pParse, regCkNull) goto sqlite3ExprCodeIN_finished } /* Step 2: Check to see if the LHS contains any NULL columns. If the ** LHS does contain NULLs then the result must be either FALSE or NULL. ** We will then skip the binary search of the RHS. */ if destIfNull == destIfFalse { destStep2 = destIfFalse } else { v7 = _sqlite3VdbeMakeLabel(tls, pParse) destStep6 = v7 destStep2 = v7 } i = 0 for { if !(i < nVector) { break } p = _sqlite3VectorFieldSubexpr(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, i) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto sqlite3ExprCodeIN_oom_error } if _sqlite3ExprCanBeNull(tls, p) != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), rLhs+i, destStep2) } goto _8 _8: ; i++ } /* Step 3. The LHS is now known to be non-NULL. Do the binary search ** of the RHS using the LHS as a probe. If found, the result is ** true. */ if eType == int32(IN_INDEX_ROWID) { /* In this case, the RHS is the ROWID of table b-tree and so we also ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4 ** into a single opcode. */ _sqlite3VdbeAddOp3(tls, v, int32(OP_SeekRowid), *(*int32)(unsafe.Pointer(bp + 8)), destIfFalse, rLhs) addrTruthOp = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto)) /* Return True */ } else { _sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), rLhs, nVector, 0, zAff, nVector) if destIfFalse == destIfNull { /* Combine Step 3 and Step 5 into a single opcode */ _sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), *(*int32)(unsafe.Pointer(bp + 8)), destIfFalse, rLhs, nVector) goto sqlite3ExprCodeIN_finished } /* Ordinary Step 3, for the case where FALSE and NULL are distinct */ addrTruthOp = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), *(*int32)(unsafe.Pointer(bp + 8)), 0, rLhs, nVector) } /* Step 4. If the RHS is known to be non-NULL and we did not find ** an match on the search above, then the result must be FALSE. */ if *(*int32)(unsafe.Pointer(bp)) != 0 && nVector == int32(1) { _sqlite3VdbeAddOp2(tls, v, int32(OP_NotNull), *(*int32)(unsafe.Pointer(bp)), destIfFalse) } /* Step 5. If we do not care about the difference between NULL and ** FALSE, then just return false. */ if destIfFalse == destIfNull { _sqlite3VdbeGoto(tls, v, destIfFalse) } /* Step 6: Loop through rows of the RHS. Compare each row to the LHS. ** If any comparison is NULL, then the result is NULL. If all ** comparisons are FALSE then the final result is FALSE. ** ** For a scalar LHS, it is sufficient to check just the first row ** of the RHS. */ if destStep6 != 0 { _sqlite3VdbeResolveLabel(tls, v, destStep6) } addrTop = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), *(*int32)(unsafe.Pointer(bp + 8)), destIfFalse) if nVector > int32(1) { destNotNull = _sqlite3VdbeMakeLabel(tls, pParse) } else { /* For nVector==1, combine steps 6 and 7 by immediately returning ** FALSE if the first comparison is not NULL */ destNotNull = destIfFalse } i = 0 for { if !(i < nVector) { break } r3 = _sqlite3GetTempReg(tls, pParse) p1 = _sqlite3VectorFieldSubexpr(tls, pLeft, i) pColl1 = _sqlite3ExprCollSeq(tls, pParse, p1) _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), *(*int32)(unsafe.Pointer(bp + 8)), i, r3) _sqlite3VdbeAddOp4(tls, v, int32(OP_Ne), rLhs+i, destNotNull, r3, pColl1, -int32(2)) _sqlite3ReleaseTempReg(tls, pParse, r3) goto _9 _9: ; i++ } _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, destIfNull) if nVector > int32(1) { _sqlite3VdbeResolveLabel(tls, v, destNotNull) _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), *(*int32)(unsafe.Pointer(bp + 8)), addrTop+int32(1)) /* Step 7: If we reach this point, we know that the result must ** be false. */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, destIfFalse) } /* Jumps here in order to return true. */ _sqlite3VdbeJumpHere(tls, v, addrTruthOp) goto sqlite3ExprCodeIN_finished sqlite3ExprCodeIN_finished: ; if rLhs != rLhsOrig { _sqlite3ReleaseTempReg(tls, pParse, rLhs) } goto sqlite3ExprCodeIN_oom_error sqlite3ExprCodeIN_oom_error: ; _sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, aiMap) _sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, zAff) } // C documentation // // /* // ** Generate an instruction that will put the floating point // ** value described by z[0..n-1] into register iMem. // ** // ** The z[] string will probably not be zero-terminated. But the // ** z[n] character is guaranteed to be something that does not look // ** like the continuation of the number. // */ func _codeReal(tls *libc.TLS, v uintptr, z uintptr, negateFlag int32, iMem int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* value at bp+0 */ float64 if z != uintptr(0) { _sqlite3AtoF(tls, z, bp, _sqlite3Strlen30(tls, z), uint8(SQLITE_UTF8)) _ = libc.Int32FromInt32(0) /* The new AtoF never returns NaN */ if negateFlag != 0 { *(*float64)(unsafe.Pointer(bp)) = -*(*float64)(unsafe.Pointer(bp)) } _sqlite3VdbeAddOp4Dup8(tls, v, int32(OP_Real), 0, iMem, 0, bp, -int32(12)) } } // C documentation // // /* // ** Generate an instruction that will put the integer describe by // ** text z[0..n-1] into register iMem. // ** // ** Expr.u.zToken is always UTF8 and zero-terminated. // */ func _codeInteger(tls *libc.TLS, pParse uintptr, pExpr uintptr, negFlag int32, iMem int32) { bp := tls.Alloc(32) defer tls.Free(32) var c, i int32 var v, z, v1 uintptr var v2 int64 var _ /* value at bp+0 */ Ti64 _, _, _, _, _, _ = c, i, v, z, v1, v2 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_IntValue) != 0 { i = *(*int32)(unsafe.Pointer(pExpr + 8)) _ = libc.Int32FromInt32(0) if negFlag != 0 { i = -i } _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), i, iMem) } else { z = *(*uintptr)(unsafe.Pointer(pExpr + 8)) _ = libc.Int32FromInt32(0) c = _sqlite3DecOrHexToI64(tls, z, bp) if c == int32(3) && !(negFlag != 0) || c == int32(2) || negFlag != 0 && *(*Ti64)(unsafe.Pointer(bp)) == int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)< 0 { iAddr = _sqlite3VdbeAddOp3(tls, v, int32(OP_IfNullRow), (*TParse)(unsafe.Pointer(pParse)).FiSelfTab-int32(1), 0, regOut) } else { iAddr = 0 } _sqlite3ExprCodeCopy(tls, pParse, _sqlite3ColumnExpr(tls, pTab, pCol), regOut) if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) >= int32(SQLITE_AFF_TEXT) { _sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), regOut, int32(1), 0, pCol+5, int32(1)) } if iAddr != 0 { _sqlite3VdbeJumpHere(tls, v, iAddr) } if (*TParse)(unsafe.Pointer(pParse)).FnErr > nErr { (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FerrByteOffset = -int32(1) } } // C documentation // // /* // ** Generate code to extract the value of the iCol-th column of a table. // */ func _sqlite3ExprCodeGetColumnOfTable(tls *libc.TLS, v uintptr, pTab uintptr, iTabCur int32, iCol int32, regOut int32) { bp := tls.Alloc(16) defer tls.Free(16) var op, savedSelfTab, x int32 var pCol, pParse, v1, p2, p3 uintptr _, _, _, _, _, _, _, _ = op, pCol, pParse, savedSelfTab, x, v1, p2, p3 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if iCol < 0 || iCol == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iTabCur, regOut) } else { if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { op = int32(OP_VColumn) x = iCol } else { v1 = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12 pCol = v1 if int32((*TColumn)(unsafe.Pointer(v1)).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 { pParse = _sqlite3VdbeParser(tls, v) if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_BUSY) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8162, libc.VaList(bp+8, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)) } else { savedSelfTab = (*TParse)(unsafe.Pointer(pParse)).FiSelfTab p2 = pCol + 10 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(COLFLAG_BUSY)) (*TParse)(unsafe.Pointer(pParse)).FiSelfTab = iTabCur + int32(1) _sqlite3ExprCodeGeneratedColumn(tls, pParse, pTab, pCol, regOut) (*TParse)(unsafe.Pointer(pParse)).FiSelfTab = savedSelfTab p3 = pCol + 10 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) & ^libc.Int32FromInt32(COLFLAG_BUSY)) } return } else { if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { x = int32(_sqlite3TableColumnToIndex(tls, _sqlite3PrimaryKeyIndex(tls, pTab), int16(iCol))) op = int32(OP_Column) } else { x = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(iCol))) op = int32(OP_Column) } } } _sqlite3VdbeAddOp3(tls, v, op, iTabCur, x, regOut) _sqlite3ColumnDefault(tls, v, pTab, iCol, regOut) } } // C documentation // // /* // ** Generate code that will extract the iColumn-th column from // ** table pTab and store the column value in register iReg. // ** // ** There must be an open cursor to pTab in iTable when this routine // ** is called. If iColumn<0 then code is generated that extracts the rowid. // */ func _sqlite3ExprCodeGetColumn(tls *libc.TLS, pParse uintptr, pTab uintptr, iColumn int32, iTable int32, iReg int32, p5 Tu8) (r int32) { var pOp uintptr _ = pOp _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3ExprCodeGetColumnOfTable(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, pTab, iTable, iColumn, iReg) if p5 != 0 { pOp = _sqlite3VdbeGetLastOp(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe) if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Column) { (*TVdbeOp)(unsafe.Pointer(pOp)).Fp5 = uint16(p5) } if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_VColumn) { (*TVdbeOp)(unsafe.Pointer(pOp)).Fp5 = uint16(int32(p5) & libc.Int32FromInt32(OPFLAG_NOCHNG)) } } return iReg } // C documentation // // /* // ** Generate code to move content from registers iFrom...iFrom+nReg-1 // ** over to iTo..iTo+nReg-1. // */ func _sqlite3ExprCodeMove(tls *libc.TLS, pParse uintptr, iFrom int32, iTo int32, nReg int32) { _sqlite3VdbeAddOp3(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(OP_Move), iFrom, iTo, nReg) } // C documentation // // /* // ** Convert a scalar expression node to a TK_REGISTER referencing // ** register iReg. The caller must ensure that iReg already contains // ** the correct value for the expression. // */ func _exprToRegister(tls *libc.TLS, pExpr uintptr, iReg int32) { var p uintptr _ = p p = _sqlite3ExprSkipCollateAndLikely(tls, pExpr) if p == uintptr(0) { return } (*TExpr)(unsafe.Pointer(p)).Fop2 = (*TExpr)(unsafe.Pointer(p)).Fop (*TExpr)(unsafe.Pointer(p)).Fop = uint8(TK_REGISTER) (*TExpr)(unsafe.Pointer(p)).FiTable = iReg *(*Tu32)(unsafe.Pointer(p + 4)) &= uint32(^libc.Int32FromInt32(EP_Skip)) } // C documentation // // /* // ** Evaluate an expression (either a vector or a scalar expression) and store // ** the result in contiguous temporary registers. Return the index of // ** the first register used to store the result. // ** // ** If the returned result register is a temporary scalar, then also write // ** that register number into *piFreeable. If the returned result register // ** is not a temporary or if the expression is a vector set *piFreeable // ** to 0. // */ func _exprCodeVector(tls *libc.TLS, pParse uintptr, p uintptr, piFreeable uintptr) (r int32) { var i, iResult, nResult int32 _, _, _ = i, iResult, nResult nResult = _sqlite3ExprVectorSize(tls, p) if nResult == int32(1) { iResult = _sqlite3ExprCodeTemp(tls, pParse, p, piFreeable) } else { *(*int32)(unsafe.Pointer(piFreeable)) = 0 if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_SELECT) { iResult = _sqlite3CodeSubselect(tls, pParse, p) } else { iResult = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nResult _ = libc.Int32FromInt32(0) i = 0 for { if !(i < nResult) { break } _sqlite3ExprCodeFactorable(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)) + 8 + uintptr(i)*20))).FpExpr, i+iResult) goto _1 _1: ; i++ } } } return iResult } // C documentation // // /* // ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5) // ** so that a subsequent copy will not be merged into this one. // */ func _setDoNotMergeFlagOnCopy(tls *libc.TLS, v uintptr) { if int32((*TVdbeOp)(unsafe.Pointer(_sqlite3VdbeGetLastOp(tls, v))).Fopcode) == int32(OP_Copy) { _sqlite3VdbeChangeP5(tls, v, uint16(1)) /* Tag trailing OP_Copy as not mergeable */ } } // C documentation // // /* // ** Generate code to implement special SQL functions that are implemented // ** in-line rather than by using the usual callbacks. // */ func _exprCodeInlineFunction(tls *libc.TLS, pParse uintptr, pFarg uintptr, iFuncId int32, target int32) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var aff uint8 var azAff [6]uintptr var endCoalesce, i, nFarg int32 var pA1, pArg, v, v2 uintptr var _ /* caseExpr at bp+0 */ TExpr _, _, _, _, _, _, _, _, _ = aff, azAff, endCoalesce, i, nFarg, pA1, pArg, v, v2 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) nFarg = (*TExprList)(unsafe.Pointer(pFarg)).FnExpr _ = libc.Int32FromInt32(0) /* All in-line functions have at least one argument */ switch iFuncId { case INLINEFUNC_coalesce: /* Attempt a direct implementation of the built-in COALESCE() and ** IFNULL() functions. This avoids unnecessary evaluation of ** arguments past the first non-NULL argument. */ endCoalesce = _sqlite3VdbeMakeLabel(tls, pParse) _ = libc.Int32FromInt32(0) _sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr, target) i = int32(1) for { if !(i < nFarg) { break } _sqlite3VdbeAddOp2(tls, v, int32(OP_NotNull), target, endCoalesce) _sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + uintptr(i)*20))).FpExpr, target) goto _1 _1: ; i++ } _setDoNotMergeFlagOnCopy(tls, v) _sqlite3VdbeResolveLabel(tls, v, endCoalesce) case int32(INLINEFUNC_iif): libc.Xmemset(tls, bp, 0, uint32(52)) (*(*TExpr)(unsafe.Pointer(bp))).Fop = uint8(TK_CASE) *(*uintptr)(unsafe.Pointer(bp + 20)) = pFarg return _sqlite3ExprCodeTarget(tls, pParse, bp, target) case int32(INLINEFUNC_sqlite_offset): pArg = (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr if int32((*TExpr)(unsafe.Pointer(pArg)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pArg)).FiTable >= 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Offset), (*TExpr)(unsafe.Pointer(pArg)).FiTable, int32((*TExpr)(unsafe.Pointer(pArg)).FiColumn), target) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, target) } default: /* The UNLIKELY() function is a no-op. The result is the value ** of the first argument. */ _ = libc.Int32FromInt32(0) target = _sqlite3ExprCodeTarget(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr, target) break /*********************************************************************** ** Test-only SQL functions that are only usable if enabled ** via SQLITE_TESTCTRL_INTERNAL_FUNCTIONS */ fallthrough case int32(INLINEFUNC_expr_compare): /* Compare two expressions using sqlite3ExprCompare() */ _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + 1*20))).FpExpr, -int32(1)), target) case int32(INLINEFUNC_expr_implies_expr): /* Compare two expressions using sqlite3ExprImpliesExpr() */ _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), _sqlite3ExprImpliesExpr(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + 1*20))).FpExpr, -int32(1)), target) case int32(INLINEFUNC_implies_nonnull_row): _ = libc.Int32FromInt32(0) pA1 = (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + 1*20))).FpExpr if int32((*TExpr)(unsafe.Pointer(pA1)).Fop) == int32(TK_COLUMN) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), _sqlite3ExprImpliesNonNullRow(tls, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr, (*TExpr)(unsafe.Pointer(pA1)).FiTable, int32(1)), target) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, target) } case int32(INLINEFUNC_affinity): /* The AFFINITY() function evaluates to a string that describes ** the type affinity of the argument. This is used for testing of ** the SQLite type logic. */ azAff = [6]uintptr{ 0: __ccgo_ts + 8192, 1: __ccgo_ts + 8197, 2: __ccgo_ts + 8202, 3: __ccgo_ts + 6306, 4: __ccgo_ts + 6301, 5: __ccgo_ts + 8210, } _ = libc.Int32FromInt32(0) aff = _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr) _ = libc.Int32FromInt32(0) if int32(aff) <= int32(SQLITE_AFF_NONE) { v2 = __ccgo_ts + 8218 } else { v2 = azAff[int32(aff)-int32(SQLITE_AFF_BLOB)] } _sqlite3VdbeLoadString(tls, v, target, v2) break } return target } // C documentation // // /* // ** Check to see if pExpr is one of the indexed expressions on pParse->pIdxEpr. // ** If it is, then resolve the expression by reading from the index and // ** return the register into which the value has been read. If pExpr is // ** not an indexed expression, then return negative. // */ func _sqlite3IndexedExprLookup(tls *libc.TLS, pParse uintptr, pExpr uintptr, target int32) (r int32) { var addr, iDataCur int32 var exprAff Tu8 var p, v uintptr _, _, _, _, _ = addr, exprAff, iDataCur, p, v p = (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr for { if !(p != 0) { break } iDataCur = (*TIndexedExpr)(unsafe.Pointer(p)).FiDataCur if iDataCur < 0 { goto _1 } if (*TParse)(unsafe.Pointer(pParse)).FiSelfTab != 0 { if (*TIndexedExpr)(unsafe.Pointer(p)).FiDataCur != (*TParse)(unsafe.Pointer(pParse)).FiSelfTab-int32(1) { goto _1 } iDataCur = -int32(1) } if _sqlite3ExprCompare(tls, uintptr(0), pExpr, (*TIndexedExpr)(unsafe.Pointer(p)).FpExpr, iDataCur) != 0 { goto _1 } _ = libc.Int32FromInt32(0) exprAff = _sqlite3ExprAffinity(tls, pExpr) if int32(exprAff) <= int32(SQLITE_AFF_BLOB) && int32((*TIndexedExpr)(unsafe.Pointer(p)).Faff) != int32(SQLITE_AFF_BLOB) || int32(exprAff) == int32(SQLITE_AFF_TEXT) && int32((*TIndexedExpr)(unsafe.Pointer(p)).Faff) != int32(SQLITE_AFF_TEXT) || int32(exprAff) >= int32(SQLITE_AFF_NUMERIC) && int32((*TIndexedExpr)(unsafe.Pointer(p)).Faff) != int32(SQLITE_AFF_NUMERIC) { /* Affinity mismatch on a generated column */ goto _1 } v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) if (*TIndexedExpr)(unsafe.Pointer(p)).FbMaybeNullRow != 0 { /* If the index is on a NULL row due to an outer join, then we ** cannot extract the value from the index. The value must be ** computed using the original expression. */ addr = _sqlite3VdbeCurrentAddr(tls, v) _sqlite3VdbeAddOp3(tls, v, int32(OP_IfNullRow), (*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCur, addr+int32(3), target) _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCur, (*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCol, target) _sqlite3VdbeGoto(tls, v, 0) p = (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr = uintptr(0) _sqlite3ExprCode(tls, pParse, pExpr, target) (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr = p _sqlite3VdbeJumpHere(tls, v, addr+int32(2)) } else { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCur, (*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCol, target) } return target goto _1 _1: ; p = (*TIndexedExpr)(unsafe.Pointer(p)).FpIENext } return -int32(1) /* Not found */ } // C documentation // // /* // ** Expresion pExpr is guaranteed to be a TK_COLUMN or equivalent. This // ** function checks the Parse.pIdxPartExpr list to see if this column // ** can be replaced with a constant value. If so, it generates code to // ** put the constant value in a register (ideally, but not necessarily, // ** register iTarget) and returns the register number. // ** // ** Or, if the TK_COLUMN cannot be replaced by a constant, zero is // ** returned. // */ func _exprPartidxExprLookup(tls *libc.TLS, pParse uintptr, pExpr uintptr, iTarget int32) (r int32) { var addr, ret int32 var p, v uintptr _, _, _, _ = addr, p, ret, v p = (*TParse)(unsafe.Pointer(pParse)).FpIdxPartExpr for { if !(p != 0) { break } if int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) == (*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCol && (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*TIndexedExpr)(unsafe.Pointer(p)).FiDataCur { v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe addr = 0 if (*TIndexedExpr)(unsafe.Pointer(p)).FbMaybeNullRow != 0 { addr = _sqlite3VdbeAddOp1(tls, v, int32(OP_IfNullRow), (*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCur) } ret = _sqlite3ExprCodeTarget(tls, pParse, (*TIndexedExpr)(unsafe.Pointer(p)).FpExpr, iTarget) _sqlite3VdbeAddOp4(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(OP_Affinity), ret, int32(1), 0, p+17, int32(1)) if addr != 0 { _sqlite3VdbeJumpHere(tls, v, addr) _sqlite3VdbeChangeP3(tls, v, addr, ret) } return ret } goto _1 _1: ; p = (*TIndexedExpr)(unsafe.Pointer(p)).FpIENext } return 0 } // C documentation // // /* // ** Generate code into the current Vdbe to evaluate the given // ** expression. Attempt to store the results in register "target". // ** Return the register where results are stored. // ** // ** With this routine, there is no guarantee that results will // ** be stored in target. The result might be stored in some other // ** register if it is convenient to do so. The calling function // ** must check the return code and move the results to the desired // ** register. // */ func _sqlite3ExprCodeTarget(tls *libc.TLS, pParse uintptr, pExpr uintptr, target int32) (r int32) { bp := tls.Alloc(144) defer tls.Free(144) var aListelem, db, db1, pAggInfo, pAggInfo1, pCol, pCol1, pColl, pDef, pDel, pEList, pFarg, pInfo, pLeft, pLeft1, pLeft2, pTab, pTab1, pTab2, pTest, pX, v, z, z1, zBlob, zId, v12, p3, p4 uintptr var addr, addrINR, aff, bNormal, destIfFalse, destIfNull, endLabel, i, i1, iCol, iCol1, iReg, iSrc, iTab, inReg, isTrue, n, n1, nCol, nExpr, nFarg, nextCase, op, p1, p5, r1, r2, v1, v10, v14, v5, v7, v8 int32 var constMask Tu32 var enc, exprOp, okConstFactor Tu8 var v11, v2, v6 bool var _ /* opCompare at bp+60 */ TExpr var _ /* regFree1 at bp+0 */ int32 var _ /* regFree2 at bp+4 */ int32 var _ /* tempX at bp+8 */ TExpr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aListelem, addr, addrINR, aff, bNormal, constMask, db, db1, destIfFalse, destIfNull, enc, endLabel, exprOp, i, i1, iCol, iCol1, iReg, iSrc, iTab, inReg, isTrue, n, n1, nCol, nExpr, nFarg, nextCase, okConstFactor, op, p1, p5, pAggInfo, pAggInfo1, pCol, pCol1, pColl, pDef, pDel, pEList, pFarg, pInfo, pLeft, pLeft1, pLeft2, pTab, pTab1, pTab2, pTest, pX, r1, r2, v, z, z1, zBlob, zId, v1, v10, v11, v12, v14, v2, v5, v6, v7, v8, p3, p4 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* The opcode being coded */ inReg = target /* Results stored in register inReg */ *(*int32)(unsafe.Pointer(bp)) = 0 /* If non-zero free this temporary register */ *(*int32)(unsafe.Pointer(bp + 4)) = 0 /* Temporary expression node */ p5 = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) goto expr_code_doover expr_code_doover: ; if pExpr == uintptr(0) { op = int32(TK_NULL) } else { if v2 = (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr != uintptr(0) && !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Leaf)) != libc.Uint32FromInt32(0)); v2 { v1 = _sqlite3IndexedExprLookup(tls, pParse, pExpr, target) r1 = v1 } if v2 && v1 >= 0 { return r1 } else { _ = libc.Int32FromInt32(0) op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) } } _ = libc.Int32FromInt32(0) switch op { case int32(TK_AGG_COLUMN): pAggInfo = (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg) >= (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn { /* Happens when the left table of a RIGHT JOIN is null and ** is using an expression index */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, target) break } pCol = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiAgg)*16 if !((*TAggInfo)(unsafe.Pointer(pAggInfo)).FdirectMode != 0) { _ = libc.Int32FromInt32(0) return (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg + int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg) } else { if (*TAggInfo)(unsafe.Pointer(pAggInfo)).FuseSortingIdx != 0 { pTab = (*TAggInfo_col)(unsafe.Pointer(pCol)).FpTab _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdxPTab, int32((*TAggInfo_col)(unsafe.Pointer(pCol)).FiSorterColumn), target) if pTab == uintptr(0) { /* No comment added */ } else { if int32((*TAggInfo_col)(unsafe.Pointer(pCol)).FiColumn) < 0 { } else { if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr((*TAggInfo_col)(unsafe.Pointer(pCol)).FiColumn)*12))).Faffinity) == int32(SQLITE_AFF_REAL) { _sqlite3VdbeAddOp1(tls, v, int32(OP_RealAffinity), target) } } } return target } else { if (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab == uintptr(0) { /* This case happens when the argument to an aggregate function ** is rewritten by aggregateConvertIndexedExprRefToColumn() */ _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TExpr)(unsafe.Pointer(pExpr)).FiTable, int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn), target) return target } } } fallthrough case int32(TK_COLUMN): iTab = (*TExpr)(unsafe.Pointer(pExpr)).FiTable if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_FixedCol)) != uint32(0) { iReg = _sqlite3ExprCodeTarget(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, target) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) aff = int32(_sqlite3TableColumnAffinity(tls, (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab, int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn))) if aff > int32(SQLITE_AFF_BLOB) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), iReg, int32(1), 0, uintptr(unsafe.Pointer(&_zAff))+uintptr((aff-int32('B'))*int32(2)), -int32(1)) } return iReg } if iTab < 0 { if (*TParse)(unsafe.Pointer(pParse)).FiSelfTab < 0 { iCol = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) _ = libc.Int32FromInt32(0) pTab1 = (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if iCol < 0 { return -int32(1) - (*TParse)(unsafe.Pointer(pParse)).FiSelfTab } pCol1 = (*TTable)(unsafe.Pointer(pTab1)).FaCol + uintptr(iCol)*12 iSrc = int32(_sqlite3TableColumnToStorage(tls, pTab1, int16(iCol))) - (*TParse)(unsafe.Pointer(pParse)).FiSelfTab if int32((*TColumn)(unsafe.Pointer(pCol1)).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { if int32((*TColumn)(unsafe.Pointer(pCol1)).FcolFlags)&int32(COLFLAG_BUSY) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8162, libc.VaList(bp+120, (*TColumn)(unsafe.Pointer(pCol1)).FzCnName)) return 0 } p3 = pCol1 + 10 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) | libc.Int32FromInt32(COLFLAG_BUSY)) if int32((*TColumn)(unsafe.Pointer(pCol1)).FcolFlags)&int32(COLFLAG_NOTAVAIL) != 0 { _sqlite3ExprCodeGeneratedColumn(tls, pParse, pTab1, pCol1, iSrc) } p4 = pCol1 + 10 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) & ^(libc.Int32FromInt32(COLFLAG_BUSY) | libc.Int32FromInt32(COLFLAG_NOTAVAIL))) return iSrc } else { if int32((*TColumn)(unsafe.Pointer(pCol1)).Faffinity) == int32(SQLITE_AFF_REAL) { _sqlite3VdbeAddOp2(tls, v, int32(OP_SCopy), iSrc, target) _sqlite3VdbeAddOp1(tls, v, int32(OP_RealAffinity), target) return target } else { return iSrc } } } else { /* Coding an expression that is part of an index where column names ** in the index refer to the table to which the index belongs */ iTab = (*TParse)(unsafe.Pointer(pParse)).FiSelfTab - int32(1) } } else { if v6 = (*TParse)(unsafe.Pointer(pParse)).FpIdxPartExpr != 0; v6 { v5 = _exprPartidxExprLookup(tls, pParse, pExpr, target) r1 = v5 } if v6 && 0 != v5 { return r1 } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) iReg = _sqlite3ExprCodeGetColumn(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab, int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn), iTab, target, (*TExpr)(unsafe.Pointer(pExpr)).Fop2) return iReg case int32(TK_INTEGER): _codeInteger(tls, pParse, pExpr, 0, target) return target case int32(TK_TRUEFALSE): _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), _sqlite3ExprTruthValue(tls, pExpr), target) return target case int32(TK_FLOAT): _ = libc.Int32FromInt32(0) _codeReal(tls, v, *(*uintptr)(unsafe.Pointer(pExpr + 8)), 0, target) return target case int32(TK_STRING): _ = libc.Int32FromInt32(0) _sqlite3VdbeLoadString(tls, v, target, *(*uintptr)(unsafe.Pointer(pExpr + 8))) return target default: /* Make NULL the default case so that if a bug causes an illegal ** Expr node to be passed into this function, it will be handled ** sanely and not crash. But keep the assert() to bring the problem ** to the attention of the developers. */ _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, target) return target case int32(TK_BLOB): _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) z = *(*uintptr)(unsafe.Pointer(pExpr + 8)) + 2 n = _sqlite3Strlen30(tls, z) - int32(1) _ = libc.Int32FromInt32(0) zBlob = _sqlite3HexToBlob(tls, _sqlite3VdbeDb(tls, v), z, n) _sqlite3VdbeAddOp4(tls, v, int32(OP_Blob), n/int32(2), target, 0, zBlob, -int32(6)) return target case int32(TK_VARIABLE): _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Variable), int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn), target) if int32(*(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 8)) + 1))) != 0 { z1 = _sqlite3VListNumToName(tls, (*TParse)(unsafe.Pointer(pParse)).FpVList, int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)) _ = libc.Int32FromInt32(0) *(*TVList)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).FpVList)) = 0 /* Indicate VList may no longer be enlarged */ _sqlite3VdbeAppendP4(tls, v, z1, -int32(1)) } return target case int32(TK_REGISTER): return (*TExpr)(unsafe.Pointer(pExpr)).FiTable case int32(TK_CAST): /* Expressions of the form: CAST(pLeft AS token) */ _sqlite3ExprCode(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, target) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Cast), target, int32(_sqlite3AffinityType(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), uintptr(0)))) return inReg case int32(TK_IS): fallthrough case int32(TK_ISNOT): if op == int32(TK_IS) { v7 = int32(TK_EQ) } else { v7 = int32(TK_NE) } op = v7 p5 = int32(SQLITE_NULLEQ) /* fall-through */ fallthrough case int32(TK_LT): fallthrough case int32(TK_LE): fallthrough case int32(TK_GT): fallthrough case int32(TK_GE): fallthrough case int32(TK_NE): fallthrough case int32(TK_EQ): pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft if _sqlite3ExprIsVector(tls, pLeft) != 0 { _codeVectorCompare(tls, pParse, pExpr, target, uint8(op), uint8(p5)) } else { r1 = _sqlite3ExprCodeTemp(tls, pParse, pLeft, bp) r2 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, bp+4) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), inReg) _codeCompare(tls, pParse, pLeft, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, op, r1, r2, _sqlite3VdbeCurrentAddr(tls, v)+int32(2), p5, libc.BoolInt32((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Commuted)) != uint32(0))) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if p5 == int32(SQLITE_NULLEQ) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, inReg) } else { _sqlite3VdbeAddOp3(tls, v, int32(OP_ZeroOrNull), r1, inReg, r2) } } case int32(TK_AND): fallthrough case int32(TK_OR): fallthrough case int32(TK_PLUS): fallthrough case int32(TK_STAR): fallthrough case int32(TK_MINUS): fallthrough case int32(TK_REM): fallthrough case int32(TK_BITAND): fallthrough case int32(TK_BITOR): fallthrough case int32(TK_SLASH): fallthrough case int32(TK_LSHIFT): fallthrough case int32(TK_RSHIFT): fallthrough case int32(TK_CONCAT): _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) r1 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp) r2 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, bp+4) _sqlite3VdbeAddOp3(tls, v, op, r2, r1, target) case int32(TK_UMINUS): pLeft1 = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pLeft1)).Fop) == int32(TK_INTEGER) { _codeInteger(tls, pParse, pLeft1, int32(1), target) return target } else { if int32((*TExpr)(unsafe.Pointer(pLeft1)).Fop) == int32(TK_FLOAT) { _ = libc.Int32FromInt32(0) _codeReal(tls, v, *(*uintptr)(unsafe.Pointer(pLeft1 + 8)), int32(1), target) return target } else { (*(*TExpr)(unsafe.Pointer(bp + 8))).Fop = uint8(TK_INTEGER) (*(*TExpr)(unsafe.Pointer(bp + 8))).Fflags = uint32(libc.Int32FromInt32(EP_IntValue) | libc.Int32FromInt32(EP_TokenOnly)) *(*int32)(unsafe.Pointer(bp + 8 + 8)) = 0 r1 = _sqlite3ExprCodeTemp(tls, pParse, bp+8, bp) r2 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp+4) _sqlite3VdbeAddOp3(tls, v, int32(OP_Subtract), r2, r1, target) } } case int32(TK_BITNOT): fallthrough case int32(TK_NOT): _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) r1 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp) _sqlite3VdbeAddOp2(tls, v, op, r1, inReg) case int32(TK_TRUTH): /* IS TRUE or IS FALSE */ r1 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp) isTrue = _sqlite3ExprTruthValue(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) bNormal = libc.BoolInt32(int32((*TExpr)(unsafe.Pointer(pExpr)).Fop2) == int32(TK_IS)) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IsTrue), r1, inReg, libc.BoolInt32(!(isTrue != 0)), isTrue^bNormal) case int32(TK_ISNULL): fallthrough case int32(TK_NOTNULL): _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), target) r1 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp) addr = _sqlite3VdbeAddOp1(tls, v, op, r1) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, target) _sqlite3VdbeJumpHere(tls, v, addr) case int32(TK_AGG_FUNCTION): pInfo = (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo if pInfo == uintptr(0) || int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg) < 0 || int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg) >= (*TAggInfo)(unsafe.Pointer(pInfo)).FnFunc { _ = libc.Int32FromInt32(0) _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8223, libc.VaList(bp+120, pExpr)) } else { _ = libc.Int32FromInt32(0) return (*TAggInfo)(unsafe.Pointer(pInfo)).FiFirstReg + (*TAggInfo)(unsafe.Pointer(pInfo)).FnColumn + int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg) } case int32(TK_FUNCTION): /* The function name */ constMask = uint32(0) /* Loop counter */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* The database connection */ enc = (*Tsqlite3)(unsafe.Pointer(db)).Fenc /* The text encoding used by this database */ pColl = uintptr(0) /* A collating sequence */ if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { return (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)))).FregResult } if (*TParse)(unsafe.Pointer(pParse)).FokConstFactor != 0 && _sqlite3ExprIsConstantNotJoin(tls, pExpr) != 0 { /* SQL functions can be expensive. So try to avoid running them ** multiple times if we know they always give the same result */ return _sqlite3ExprCodeRunJustOnce(tls, pParse, pExpr, -int32(1)) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pFarg = *(*uintptr)(unsafe.Pointer(pExpr + 20)) if pFarg != 0 { v8 = (*TExprList)(unsafe.Pointer(pFarg)).FnExpr } else { v8 = 0 } nFarg = v8 _ = libc.Int32FromInt32(0) zId = *(*uintptr)(unsafe.Pointer(pExpr + 8)) pDef = _sqlite3FindFunction(tls, db, zId, nFarg, enc, uint8(0)) if pDef == uintptr(0) || (*TFuncDef)(unsafe.Pointer(pDef)).FxFinalize != uintptr(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8250, libc.VaList(bp+120, pExpr)) break } if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_INLINE) != uint32(0) && pFarg != uintptr(0) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return _exprCodeInlineFunction(tls, pParse, pFarg, int32((*TFuncDef)(unsafe.Pointer(pDef)).FpUserData), target) } else { if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(libc.Int32FromInt32(SQLITE_FUNC_DIRECT)|libc.Int32FromInt32(SQLITE_FUNC_UNSAFE)) != 0 { _sqlite3ExprFunctionUsable(tls, pParse, pExpr, pDef) } } i = 0 for { if !(i < nFarg) { break } if i < int32(32) && _sqlite3ExprIsConstant(tls, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + uintptr(i)*20))).FpExpr) != 0 { constMask |= libc.Uint32FromInt32(1) << i } if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) != uint32(0) && !(pColl != 0) { pColl = _sqlite3ExprCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + uintptr(i)*20))).FpExpr) } goto _9 _9: ; i++ } if pFarg != 0 { if constMask != 0 { r1 = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nFarg } else { r1 = _sqlite3GetTempRange(tls, pParse, nFarg) } /* For length() and typeof() and octet_length() functions, ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG ** or OPFLAG_TYPEOFARG or OPFLAG_BYTELENARG respectively, to avoid ** unnecessary data loading. */ if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(libc.Int32FromInt32(SQLITE_FUNC_LENGTH)|libc.Int32FromInt32(SQLITE_FUNC_TYPEOF)) != uint32(0) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) exprOp = (*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr)).Fop if int32(exprOp) == int32(TK_COLUMN) || int32(exprOp) == int32(TK_AGG_COLUMN) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr)).Fop2 = uint8((*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags & uint32(OPFLAG_BYTELENARG)) } } _sqlite3ExprCodeExprList(tls, pParse, pFarg, r1, 0, uint8(SQLITE_ECEL_FACTOR)) } else { r1 = 0 } /* Possibly overload the function if the first argument is ** a virtual table column. ** ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the ** second argument, not the first, as the argument to test to ** see if it is a column in a virtual table. This is done because ** the left operand of infix functions (the operand we want to ** control overloading) ends up as the second argument to the ** function. The expression "A glob B" is equivalent to ** "glob(B,A). We want to use the A in "A glob B" to test ** for function overloading. But we use the B term in "glob(B,A)". */ if nFarg >= int32(2) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_InfixFunc)) != uint32(0) { pDef = _sqlite3VtabOverloadFunction(tls, db, pDef, nFarg, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + 1*20))).FpExpr) } else { if nFarg > 0 { pDef = _sqlite3VtabOverloadFunction(tls, db, pDef, nFarg, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr) } } if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) != 0 { if !(pColl != 0) { pColl = (*Tsqlite3)(unsafe.Pointer(db)).FpDfltColl } _sqlite3VdbeAddOp4(tls, v, int32(OP_CollSeq), 0, 0, 0, pColl, -int32(2)) } _sqlite3VdbeAddFunctionCall(tls, pParse, int32(constMask), r1, target, nFarg, pDef, int32((*TExpr)(unsafe.Pointer(pExpr)).Fop2)) if nFarg != 0 { if constMask == uint32(0) { _sqlite3ReleaseTempRange(tls, pParse, r1, nFarg) } else { } } return target case int32(TK_EXISTS): fallthrough case int32(TK_SELECT): if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 { return 0 } else { if v11 = op == int32(TK_SELECT) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0); v11 { v10 = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpEList)).FnExpr nCol = v10 } if v11 && v10 != int32(1) { _sqlite3SubselectError(tls, pParse, nCol, int32(1)) } else { return _sqlite3CodeSubselect(tls, pParse, pExpr) } } case int32(TK_SELECT_COLUMN): pLeft2 = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft if (*TExpr)(unsafe.Pointer(pLeft2)).FiTable == 0 || int32((*TParse)(unsafe.Pointer(pParse)).FwithinRJSubrtn) > int32((*TExpr)(unsafe.Pointer(pLeft2)).Fop2) { (*TExpr)(unsafe.Pointer(pLeft2)).FiTable = _sqlite3CodeSubselect(tls, pParse, pLeft2) (*TExpr)(unsafe.Pointer(pLeft2)).Fop2 = (*TParse)(unsafe.Pointer(pParse)).FwithinRJSubrtn } _ = libc.Int32FromInt32(0) n1 = _sqlite3ExprVectorSize(tls, pLeft2) if (*TExpr)(unsafe.Pointer(pExpr)).FiTable != n1 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7833, libc.VaList(bp+120, (*TExpr)(unsafe.Pointer(pExpr)).FiTable, n1)) } return (*TExpr)(unsafe.Pointer(pLeft2)).FiTable + int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) case int32(TK_IN): destIfFalse = _sqlite3VdbeMakeLabel(tls, pParse) destIfNull = _sqlite3VdbeMakeLabel(tls, pParse) _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, target) _sqlite3ExprCodeIN(tls, pParse, pExpr, destIfFalse, destIfNull) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), target) _sqlite3VdbeResolveLabel(tls, v, destIfFalse) _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), target, 0) _sqlite3VdbeResolveLabel(tls, v, destIfNull) return target /* ** x BETWEEN y AND z ** ** This is equivalent to ** ** x>=y AND x<=z ** ** X is stored in pExpr->pLeft. ** Y is stored in pExpr->pList->a[0].pExpr. ** Z is stored in pExpr->pList->a[1].pExpr. */ fallthrough case int32(TK_BETWEEN): _exprCodeBetween(tls, pParse, pExpr, target, uintptr(0), 0) return target case int32(TK_COLLATE): if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Collate)) != libc.Uint32FromInt32(0)) { /* A TK_COLLATE Expr node without the EP_Collate tag is a so-called ** "SOFT-COLLATE" that is added to constraints that are pushed down ** from outer queries into sub-queries by the push-down optimization. ** Clear subtypes as subtypes may not cross a subquery boundary. */ _ = libc.Int32FromInt32(0) _sqlite3ExprCode(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, target) _sqlite3VdbeAddOp1(tls, v, int32(OP_ClrSubtype), target) return target } else { pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft goto expr_code_doover /* 2018-04-28: Prevent deep recursion. */ } fallthrough case int32(TK_SPAN): fallthrough case int32(TK_UPLUS): pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft goto expr_code_doover /* 2018-04-28: Prevent deep recursion. OSSFuzz. */ case int32(TK_TRIGGER): _ = libc.Int32FromInt32(0) pTab2 = (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab iCol1 = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) p1 = (*TExpr)(unsafe.Pointer(pExpr)).FiTable*(int32((*TTable)(unsafe.Pointer(pTab2)).FnCol)+int32(1)) + int32(1) + int32(_sqlite3TableColumnToStorage(tls, pTab2, int16(iCol1))) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Param), p1, target) /* If the column has REAL affinity, it may currently be stored as an ** integer. Use OP_RealAffinity to make sure it is really real. ** ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to ** floating point when extracting it from the record. */ if iCol1 >= 0 && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab2)).FaCol + uintptr(iCol1)*12))).Faffinity) == int32(SQLITE_AFF_REAL) { _sqlite3VdbeAddOp1(tls, v, int32(OP_RealAffinity), target) } case int32(TK_VECTOR): _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+6677, 0) break /* TK_IF_NULL_ROW Expr nodes are inserted ahead of expressions ** that derive from the right-hand table of a LEFT JOIN. The ** Expr.iTable value is the table number for the right-hand table. ** The expression is only evaluated if that table is not currently ** on a LEFT JOIN NULL row. */ fallthrough case int32(TK_IF_NULL_ROW): okConstFactor = (*TParse)(unsafe.Pointer(pParse)).FokConstFactor pAggInfo1 = (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo if pAggInfo1 != 0 { _ = libc.Int32FromInt32(0) if !((*TAggInfo)(unsafe.Pointer(pAggInfo1)).FdirectMode != 0) { _ = libc.Int32FromInt32(0) inReg = (*TAggInfo)(unsafe.Pointer(pAggInfo1)).FiFirstReg + int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg) break } if (*TAggInfo)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo)).FuseSortingIdx != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TAggInfo)(unsafe.Pointer(pAggInfo1)).FsortingIdxPTab, int32((*(*TAggInfo_col)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo1)).FaCol + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiAgg)*16))).FiSorterColumn), target) inReg = target break } } addrINR = _sqlite3VdbeAddOp3(tls, v, int32(OP_IfNullRow), (*TExpr)(unsafe.Pointer(pExpr)).FiTable, 0, target) /* The OP_IfNullRow opcode above can overwrite the result register with ** NULL. So we have to ensure that the result register is not a value ** that is suppose to be a constant. Two defenses are needed: ** (1) Temporarily disable factoring of constant expressions ** (2) Make sure the computed value really is stored in register ** "target" and not someplace else. */ (*TParse)(unsafe.Pointer(pParse)).FokConstFactor = uint8(0) /* note (1) above */ _sqlite3ExprCode(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, target) _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).FokConstFactor = okConstFactor _sqlite3VdbeJumpHere(tls, v, addrINR) break /* ** Form A: ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END ** ** Form B: ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END ** ** Form A is can be transformed into the equivalent form B as follows: ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... ** WHEN x=eN THEN rN ELSE y END ** ** X (if it exists) is in pExpr->pLeft. ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is ** odd. The Y is also optional. If the number of elements in x.pList ** is even, then Y is omitted and the "otherwise" result is NULL. ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. ** ** The result of the expression is the Ri for the first matching Ei, ** or if there is no matching Ei, the ELSE term Y, or if there is ** no ELSE term, NULL. */ fallthrough case int32(TK_CASE): /* The X expression */ pTest = uintptr(0) /* X==Ei (form A) or just Ei (form B) */ pDel = uintptr(0) db1 = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pEList = *(*uintptr)(unsafe.Pointer(pExpr + 20)) aListelem = pEList + 8 nExpr = (*TExprList)(unsafe.Pointer(pEList)).FnExpr endLabel = _sqlite3VdbeMakeLabel(tls, pParse) v12 = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft pX = v12 if v12 != uintptr(0) { pDel = _sqlite3ExprDup(tls, db1, pX, 0) if (*Tsqlite3)(unsafe.Pointer(db1)).FmallocFailed != 0 { _sqlite3ExprDelete(tls, db1, pDel) break } _exprToRegister(tls, pDel, _exprCodeVector(tls, pParse, pDel, bp)) libc.Xmemset(tls, bp+60, 0, uint32(52)) (*(*TExpr)(unsafe.Pointer(bp + 60))).Fop = uint8(TK_EQ) (*(*TExpr)(unsafe.Pointer(bp + 60))).FpLeft = pDel pTest = bp + 60 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: ** The value in regFree1 might get SCopy-ed into the file result. ** So make sure that the regFree1 register is not reused for other ** purposes and possibly overwritten. */ *(*int32)(unsafe.Pointer(bp)) = 0 } i1 = 0 for { if !(i1 < nExpr-int32(1)) { break } if pX != 0 { _ = libc.Int32FromInt32(0) (*(*TExpr)(unsafe.Pointer(bp + 60))).FpRight = (*(*TExprList_item)(unsafe.Pointer(aListelem + uintptr(i1)*20))).FpExpr } else { pTest = (*(*TExprList_item)(unsafe.Pointer(aListelem + uintptr(i1)*20))).FpExpr } nextCase = _sqlite3VdbeMakeLabel(tls, pParse) _sqlite3ExprIfFalse(tls, pParse, pTest, nextCase, int32(SQLITE_JUMPIFNULL)) _sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(aListelem + uintptr(i1+int32(1))*20))).FpExpr, target) _sqlite3VdbeGoto(tls, v, endLabel) _sqlite3VdbeResolveLabel(tls, v, nextCase) goto _13 _13: ; i1 = i1 + int32(2) } if nExpr&int32(1) != 0 { _sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(nExpr-int32(1))*20))).FpExpr, target) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, target) } _sqlite3ExprDelete(tls, db1, pDel) _setDoNotMergeFlagOnCopy(tls, v) _sqlite3VdbeResolveLabel(tls, v, endLabel) case int32(TK_RAISE): _ = libc.Int32FromInt32(0) if !((*TParse)(unsafe.Pointer(pParse)).FpTriggerTab != 0) && !((*TParse)(unsafe.Pointer(pParse)).Fnested != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8274, 0) return 0 } if int32((*TExpr)(unsafe.Pointer(pExpr)).FaffExpr) == int32(OE_Abort) { _sqlite3MayAbort(tls, pParse) } _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pExpr)).FaffExpr) == int32(OE_Ignore) { _sqlite3VdbeAddOp4(tls, v, int32(OP_Halt), SQLITE_OK, int32(OE_Ignore), 0, *(*uintptr)(unsafe.Pointer(pExpr + 8)), 0) } else { if (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab != 0 { v14 = libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(7)<0 then the result is always stored in that register and the // ** result is not reusable. If regDest<0 then this routine is free to // ** store the value wherever it wants. The register where the expression // ** is stored is returned. When regDest<0, two identical expressions might // ** code to the same register, if they do not contain function calls and hence // ** are factored out into the initialization section at the end of the // ** prepared statement. // */ func _sqlite3ExprCodeRunJustOnce(tls *libc.TLS, pParse uintptr, pExpr uintptr, regDest int32) (r int32) { var addr, i, v2, v4 int32 var p, pItem, pItem1, v, v3, v5 uintptr _, _, _, _, _, _, _, _, _, _ = addr, i, p, pItem, pItem1, v, v2, v3, v4, v5 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) p = (*TParse)(unsafe.Pointer(pParse)).FpConstExpr if regDest < 0 && p != 0 { pItem = p + 8 i = (*TExprList)(unsafe.Pointer(p)).FnExpr for { if !(i > 0) { break } if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 8 + 4))&0x8>>3)) != 0 && _sqlite3ExprCompare(tls, uintptr(0), (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr, pExpr, -int32(1)) == 0 { return *(*int32)(unsafe.Pointer(pItem + 16)) } goto _1 _1: ; pItem += 20 i-- } } pExpr = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr, 0) if pExpr != uintptr(0) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_HasFunc)) != uint32(0) { v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) addr = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once)) (*TParse)(unsafe.Pointer(pParse)).FokConstFactor = uint8(0) if !((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0) { if regDest < 0 { v3 = pParse + 44 *(*int32)(unsafe.Pointer(v3))++ v2 = *(*int32)(unsafe.Pointer(v3)) regDest = v2 } _sqlite3ExprCode(tls, pParse, pExpr, regDest) } (*TParse)(unsafe.Pointer(pParse)).FokConstFactor = uint8(1) _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr) _sqlite3VdbeJumpHere(tls, v, addr) } else { p = _sqlite3ExprListAppend(tls, pParse, p, pExpr) if p != 0 { pItem1 = p + 8 + uintptr((*TExprList)(unsafe.Pointer(p)).FnExpr-int32(1))*20 libc.SetBitFieldPtr16Uint32(pItem1+8+4, libc.BoolUint32(regDest < libc.Int32FromInt32(0)), 3, 0x8) if regDest < 0 { v5 = pParse + 44 *(*int32)(unsafe.Pointer(v5))++ v4 = *(*int32)(unsafe.Pointer(v5)) regDest = v4 } *(*int32)(unsafe.Pointer(pItem1 + 16)) = regDest } (*TParse)(unsafe.Pointer(pParse)).FpConstExpr = p } return regDest } // C documentation // // /* // ** Generate code to evaluate an expression and store the results // ** into a register. Return the register number where the results // ** are stored. // ** // ** If the register is a temporary register that can be deallocated, // ** then write its number into *pReg. If the result register is not // ** a temporary, then set *pReg to zero. // ** // ** If pExpr is a constant, then this routine might generate this // ** code to fill the register in the initialization section of the // ** VDBE program, in order to factor it out of the evaluation loop. // */ func _sqlite3ExprCodeTemp(tls *libc.TLS, pParse uintptr, pExpr uintptr, pReg uintptr) (r int32) { var r1, r2 int32 _, _ = r1, r2 pExpr = _sqlite3ExprSkipCollateAndLikely(tls, pExpr) if (*TParse)(unsafe.Pointer(pParse)).FokConstFactor != 0 && pExpr != uintptr(0) && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_REGISTER) && _sqlite3ExprIsConstantNotJoin(tls, pExpr) != 0 { *(*int32)(unsafe.Pointer(pReg)) = 0 r2 = _sqlite3ExprCodeRunJustOnce(tls, pParse, pExpr, -int32(1)) } else { r1 = _sqlite3GetTempReg(tls, pParse) r2 = _sqlite3ExprCodeTarget(tls, pParse, pExpr, r1) if r2 == r1 { *(*int32)(unsafe.Pointer(pReg)) = r1 } else { _sqlite3ReleaseTempReg(tls, pParse, r1) *(*int32)(unsafe.Pointer(pReg)) = 0 } } return r2 } // C documentation // // /* // ** Generate code that will evaluate expression pExpr and store the // ** results in register target. The results are guaranteed to appear // ** in register target. // */ func _sqlite3ExprCode(tls *libc.TLS, pParse uintptr, pExpr uintptr, target int32) { var inReg int32 var op Tu8 var pX uintptr _, _, _ = inReg, op, pX _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FpVdbe == uintptr(0) { return } inReg = _sqlite3ExprCodeTarget(tls, pParse, pExpr, target) if inReg != target { pX = _sqlite3ExprSkipCollateAndLikely(tls, pExpr) if pX != 0 && ((*TExpr)(unsafe.Pointer(pX)).Fflags&uint32(libc.Int32FromInt32(EP_Subquery)) != uint32(0) || int32((*TExpr)(unsafe.Pointer(pX)).Fop) == int32(TK_REGISTER)) { op = uint8(OP_Copy) } else { op = uint8(OP_SCopy) } _sqlite3VdbeAddOp2(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(op), inReg, target) } } // C documentation // // /* // ** Make a transient copy of expression pExpr and then code it using // ** sqlite3ExprCode(). This routine works just like sqlite3ExprCode() // ** except that the input expression is guaranteed to be unchanged. // */ func _sqlite3ExprCodeCopy(tls *libc.TLS, pParse uintptr, pExpr uintptr, target int32) { var db uintptr _ = db db = (*TParse)(unsafe.Pointer(pParse)).Fdb pExpr = _sqlite3ExprDup(tls, db, pExpr, 0) if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { _sqlite3ExprCode(tls, pParse, pExpr, target) } _sqlite3ExprDelete(tls, db, pExpr) } // C documentation // // /* // ** Generate code that will evaluate expression pExpr and store the // ** results in register target. The results are guaranteed to appear // ** in register target. If the expression is constant, then this routine // ** might choose to code the expression at initialization time. // */ func _sqlite3ExprCodeFactorable(tls *libc.TLS, pParse uintptr, pExpr uintptr, target int32) { if (*TParse)(unsafe.Pointer(pParse)).FokConstFactor != 0 && _sqlite3ExprIsConstantNotJoin(tls, pExpr) != 0 { _sqlite3ExprCodeRunJustOnce(tls, pParse, pExpr, target) } else { _sqlite3ExprCodeCopy(tls, pParse, pExpr, target) } } // C documentation // // /* // ** Generate code that pushes the value of every element of the given // ** expression list into a sequence of registers beginning at target. // ** // ** Return the number of elements evaluated. The number returned will // ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF // ** is defined. // ** // ** The SQLITE_ECEL_DUP flag prevents the arguments from being // ** filled using OP_SCopy. OP_Copy must be used instead. // ** // ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be // ** factored out into initialization code. // ** // ** The SQLITE_ECEL_REF flag means that expressions in the list with // ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored // ** in registers at srcReg, and so the value can be copied from there. // ** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0 // ** are simply omitted rather than being copied from srcReg. // */ func _sqlite3ExprCodeExprList(tls *libc.TLS, pParse uintptr, pList uintptr, target int32, srcReg int32, flags Tu8) (r int32) { var copyOp Tu8 var i, inReg, j, n, v1, v3 int32 var pExpr, pItem, pOp, v, v5 uintptr var v4, v6 bool _, _, _, _, _, _, _, _, _, _, _, _, _, _ = copyOp, i, inReg, j, n, pExpr, pItem, pOp, v, v1, v3, v4, v5, v6 if int32(flags)&int32(SQLITE_ECEL_DUP) != 0 { v1 = int32(OP_Copy) } else { v1 = int32(OP_SCopy) } copyOp = uint8(v1) v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Never gets this far otherwise */ n = (*TExprList)(unsafe.Pointer(pList)).FnExpr if !((*TParse)(unsafe.Pointer(pParse)).FokConstFactor != 0) { flags = Tu8(int32(flags) & ^libc.Int32FromInt32(SQLITE_ECEL_FACTOR)) } pItem = pList + 8 i = libc.Int32FromInt32(0) for { if !(i < n) { break } pExpr = (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr if v4 = int32(flags)&int32(SQLITE_ECEL_REF) != 0; v4 { v3 = int32((*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem + 16))).FiOrderByCol) j = v3 } if v4 && v3 > 0 { if int32(flags)&int32(SQLITE_ECEL_OMITREF) != 0 { i-- n-- } else { _sqlite3VdbeAddOp2(tls, v, int32(copyOp), j+srcReg-int32(1), target+i) } } else { if int32(flags)&int32(SQLITE_ECEL_FACTOR) != 0 && _sqlite3ExprIsConstantNotJoin(tls, pExpr) != 0 { _sqlite3ExprCodeRunJustOnce(tls, pParse, pExpr, target+i) } else { inReg = _sqlite3ExprCodeTarget(tls, pParse, pExpr, target+i) if inReg != target+i { if v6 = int32(copyOp) == int32(OP_Copy); v6 { v5 = _sqlite3VdbeGetLastOp(tls, v) pOp = v5 } if v6 && int32((*TVdbeOp)(unsafe.Pointer(v5)).Fopcode) == int32(OP_Copy) && (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1+(*TVdbeOp)(unsafe.Pointer(pOp)).Fp3+int32(1) == inReg && (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2+(*TVdbeOp)(unsafe.Pointer(pOp)).Fp3+int32(1) == target+i && int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fp5) == 0 { (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3++ } else { _sqlite3VdbeAddOp2(tls, v, int32(copyOp), inReg, target+i) } } } } goto _2 _2: ; i++ pItem += 20 } return n } // C documentation // // /* // ** Generate code for a BETWEEN operator. // ** // ** x BETWEEN y AND z // ** // ** The above is equivalent to // ** // ** x>=y AND x<=z // ** // ** Code it as such, taking care to do the common subexpression // ** elimination of x. // ** // ** The xJumpIf parameter determines details: // ** // ** NULL: Store the boolean result in reg[dest] // ** sqlite3ExprIfTrue: Jump to dest if true // ** sqlite3ExprIfFalse: Jump to dest if false // ** // ** The jumpIfNull parameter is ignored if xJumpIf is NULL. // */ func _exprCodeBetween(tls *libc.TLS, pParse uintptr, pExpr uintptr, dest int32, xJump uintptr, jumpIfNull int32) { bp := tls.Alloc(160) defer tls.Free(160) var db, pDel uintptr var _ /* compLeft at bp+52 */ TExpr var _ /* compRight at bp+104 */ TExpr var _ /* exprAnd at bp+0 */ TExpr var _ /* regFree1 at bp+156 */ int32 _, _ = db, pDel /* The x<=z term */ *(*int32)(unsafe.Pointer(bp + 156)) = 0 /* Temporary use register */ pDel = uintptr(0) db = (*TParse)(unsafe.Pointer(pParse)).Fdb libc.Xmemset(tls, bp+52, 0, uint32(52)) libc.Xmemset(tls, bp+104, 0, uint32(52)) libc.Xmemset(tls, bp, 0, uint32(52)) _ = libc.Int32FromInt32(0) pDel = _sqlite3ExprDup(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, 0) if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 { (*(*TExpr)(unsafe.Pointer(bp))).Fop = uint8(TK_AND) (*(*TExpr)(unsafe.Pointer(bp))).FpLeft = bp + 52 (*(*TExpr)(unsafe.Pointer(bp))).FpRight = bp + 104 (*(*TExpr)(unsafe.Pointer(bp + 52))).Fop = uint8(TK_GE) (*(*TExpr)(unsafe.Pointer(bp + 52))).FpLeft = pDel (*(*TExpr)(unsafe.Pointer(bp + 52))).FpRight = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8))).FpExpr (*(*TExpr)(unsafe.Pointer(bp + 104))).Fop = uint8(TK_LE) (*(*TExpr)(unsafe.Pointer(bp + 104))).FpLeft = pDel (*(*TExpr)(unsafe.Pointer(bp + 104))).FpRight = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8 + 1*20))).FpExpr _exprToRegister(tls, pDel, _exprCodeVector(tls, pParse, pDel, bp+156)) if xJump != 0 { (*(*func(*libc.TLS, uintptr, uintptr, int32, int32))(unsafe.Pointer(&struct{ uintptr }{xJump})))(tls, pParse, bp, dest, jumpIfNull) } else { /* Mark the expression is being from the ON or USING clause of a join ** so that the sqlite3ExprCodeTarget() routine will not attempt to move ** it into the Parse.pConstExpr list. We should use a new bit for this, ** for clarity, but we are out of bits in the Expr.flags field so we ** have to reuse the EP_OuterON bit. Bummer. */ *(*Tu32)(unsafe.Pointer(pDel + 4)) |= uint32(EP_OuterON) _sqlite3ExprCodeTarget(tls, pParse, bp, dest) } _sqlite3ReleaseTempReg(tls, pParse, *(*int32)(unsafe.Pointer(bp + 156))) } _sqlite3ExprDelete(tls, db, pDel) /* Ensure adequate test coverage */ } // C documentation // // /* // ** Generate code for a boolean expression such that a jump is made // ** to the label "dest" if the expression is true but execution // ** continues straight thru if the expression is false. // ** // ** If the expression evaluates to NULL (neither true nor false), then // ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. // ** // ** This code depends on the fact that certain token values (ex: TK_EQ) // ** are the same as opcode values (ex: OP_Eq) that implement the corresponding // ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in // ** the make process cause these values to align. Assert()s in the code // ** below verify that the numbers are aligned correctly. // */ func _sqlite3ExprIfTrue(tls *libc.TLS, pParse uintptr, pExpr uintptr, dest int32, jumpIfNull int32) { bp := tls.Alloc(16) defer tls.Free(16) var d2, destIfFalse, destIfNull, isNot, isTrue, op, r1, r2, v19, v20, v21, v22 int32 var pAlt, v uintptr var _ /* regFree1 at bp+0 */ int32 var _ /* regFree2 at bp+4 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _ = d2, destIfFalse, destIfNull, isNot, isTrue, op, pAlt, r1, r2, v, v19, v20, v21, v22 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe op = 0 *(*int32)(unsafe.Pointer(bp)) = 0 *(*int32)(unsafe.Pointer(bp + 4)) = 0 _ = libc.Int32FromInt32(0) if v == uintptr(0) { return } /* Existence of VDBE checked by caller */ if pExpr == uintptr(0) { return } /* No way this can happen */ _ = libc.Int32FromInt32(0) op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) switch op { case int32(TK_OR): goto _1 case int32(TK_AND): goto _2 case int32(TK_NOT): goto _3 case int32(TK_TRUTH): goto _4 case int32(TK_ISNOT): goto _5 case int32(TK_IS): goto _6 case int32(TK_EQ): goto _7 case int32(TK_NE): goto _8 case int32(TK_GE): goto _9 case int32(TK_GT): goto _10 case int32(TK_LE): goto _11 case int32(TK_LT): goto _12 case int32(TK_NOTNULL): goto _13 case int32(TK_ISNULL): goto _14 case int32(TK_BETWEEN): goto _15 case int32(TK_IN): goto _16 default: goto _17 } goto _18 _2: ; _1: ; pAlt = _sqlite3ExprSimplifiedAndOr(tls, pExpr) if pAlt != pExpr { _sqlite3ExprIfTrue(tls, pParse, pAlt, dest, jumpIfNull) } else { if op == int32(TK_AND) { d2 = _sqlite3VdbeMakeLabel(tls, pParse) _sqlite3ExprIfFalse(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, d2, jumpIfNull^int32(SQLITE_JUMPIFNULL)) _sqlite3ExprIfTrue(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, dest, jumpIfNull) _sqlite3VdbeResolveLabel(tls, v, d2) } else { _sqlite3ExprIfTrue(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, dest, jumpIfNull) _sqlite3ExprIfTrue(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, dest, jumpIfNull) } } goto _18 _3: ; _sqlite3ExprIfFalse(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, dest, jumpIfNull) goto _18 _4: ; /* IS TRUE or IS NOT TRUE */ isNot = libc.BoolInt32(int32((*TExpr)(unsafe.Pointer(pExpr)).Fop2) == int32(TK_ISNOT)) isTrue = _sqlite3ExprTruthValue(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) if isTrue^isNot != 0 { if isNot != 0 { v19 = int32(SQLITE_JUMPIFNULL) } else { v19 = 0 } _sqlite3ExprIfTrue(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, dest, v19) } else { if isNot != 0 { v20 = int32(SQLITE_JUMPIFNULL) } else { v20 = 0 } _sqlite3ExprIfFalse(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, dest, v20) } goto _18 _6: ; _5: ; if op == int32(TK_IS) { v21 = int32(TK_EQ) } else { v21 = int32(TK_NE) } op = v21 jumpIfNull = int32(SQLITE_NULLEQ) _12: ; _11: ; _10: ; _9: ; _8: ; _7: ; if _sqlite3ExprIsVector(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) != 0 { goto default_expr } r1 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp) r2 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, bp+4) _codeCompare(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, op, r1, r2, dest, jumpIfNull, libc.BoolInt32((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Commuted)) != uint32(0))) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) goto _18 _14: ; _13: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) r1 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp) _sqlite3VdbeTypeofColumn(tls, v, r1) _sqlite3VdbeAddOp2(tls, v, op, r1, dest) goto _18 _15: ; _exprCodeBetween(tls, pParse, pExpr, dest, __ccgo_fp(_sqlite3ExprIfTrue), jumpIfNull) goto _18 _16: ; destIfFalse = _sqlite3VdbeMakeLabel(tls, pParse) if jumpIfNull != 0 { v22 = dest } else { v22 = destIfFalse } destIfNull = v22 _sqlite3ExprCodeIN(tls, pParse, pExpr, destIfFalse, destIfNull) _sqlite3VdbeGoto(tls, v, dest) _sqlite3VdbeResolveLabel(tls, v, destIfFalse) goto _18 _17: ; goto default_expr default_expr: ; if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_IsTrue)) == uint32(EP_IsTrue) { _sqlite3VdbeGoto(tls, v, dest) } else { if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_IsFalse)) == uint32(EP_IsFalse) { /* No-op */ } else { r1 = _sqlite3ExprCodeTemp(tls, pParse, pExpr, bp) _sqlite3VdbeAddOp3(tls, v, int32(OP_If), r1, dest, libc.BoolInt32(jumpIfNull != 0)) } } goto _18 _18: ; _sqlite3ReleaseTempReg(tls, pParse, *(*int32)(unsafe.Pointer(bp))) _sqlite3ReleaseTempReg(tls, pParse, *(*int32)(unsafe.Pointer(bp + 4))) } // C documentation // // /* // ** Generate code for a boolean expression such that a jump is made // ** to the label "dest" if the expression is false but execution // ** continues straight thru if the expression is true. // ** // ** If the expression evaluates to NULL (neither true nor false) then // ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull // ** is 0. // */ func _sqlite3ExprIfFalse(tls *libc.TLS, pParse uintptr, pExpr uintptr, dest int32, jumpIfNull int32) { bp := tls.Alloc(16) defer tls.Free(16) var d2, destIfNull, isNot, isTrue, op, r1, r2, v19, v20, v21 int32 var pAlt, v uintptr var _ /* regFree1 at bp+0 */ int32 var _ /* regFree2 at bp+4 */ int32 _, _, _, _, _, _, _, _, _, _, _, _ = d2, destIfNull, isNot, isTrue, op, pAlt, r1, r2, v, v19, v20, v21 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe op = 0 *(*int32)(unsafe.Pointer(bp)) = 0 *(*int32)(unsafe.Pointer(bp + 4)) = 0 _ = libc.Int32FromInt32(0) if v == uintptr(0) { return } /* Existence of VDBE checked by caller */ if pExpr == uintptr(0) { return } _ = libc.Int32FromInt32(0) /* The value of pExpr->op and op are related as follows: ** ** pExpr->op op ** --------- ---------- ** TK_ISNULL OP_NotNull ** TK_NOTNULL OP_IsNull ** TK_NE OP_Eq ** TK_EQ OP_Ne ** TK_GT OP_Le ** TK_LE OP_Gt ** TK_GE OP_Lt ** TK_LT OP_Ge ** ** For other values of pExpr->op, op is undefined and unused. ** The value of TK_ and OP_ constants are arranged such that we ** can compute the mapping above using the following expression. ** Assert()s verify that the computation is correct. */ op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) + libc.Int32FromInt32(TK_ISNULL)&libc.Int32FromInt32(1) ^ int32(1) - libc.Int32FromInt32(TK_ISNULL)&libc.Int32FromInt32(1) /* Verify correct alignment of TK_ and OP_ constants */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) { case int32(TK_OR): goto _1 case int32(TK_AND): goto _2 case int32(TK_NOT): goto _3 case int32(TK_TRUTH): goto _4 case int32(TK_ISNOT): goto _5 case int32(TK_IS): goto _6 case int32(TK_EQ): goto _7 case int32(TK_NE): goto _8 case int32(TK_GE): goto _9 case int32(TK_GT): goto _10 case int32(TK_LE): goto _11 case int32(TK_LT): goto _12 case int32(TK_NOTNULL): goto _13 case int32(TK_ISNULL): goto _14 case int32(TK_BETWEEN): goto _15 case int32(TK_IN): goto _16 default: goto _17 } goto _18 _2: ; _1: ; pAlt = _sqlite3ExprSimplifiedAndOr(tls, pExpr) if pAlt != pExpr { _sqlite3ExprIfFalse(tls, pParse, pAlt, dest, jumpIfNull) } else { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AND) { _sqlite3ExprIfFalse(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, dest, jumpIfNull) _sqlite3ExprIfFalse(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, dest, jumpIfNull) } else { d2 = _sqlite3VdbeMakeLabel(tls, pParse) _sqlite3ExprIfTrue(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, d2, jumpIfNull^int32(SQLITE_JUMPIFNULL)) _sqlite3ExprIfFalse(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, dest, jumpIfNull) _sqlite3VdbeResolveLabel(tls, v, d2) } } goto _18 _3: ; _sqlite3ExprIfTrue(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, dest, jumpIfNull) goto _18 _4: ; /* IS TRUE or IS NOT TRUE */ isNot = libc.BoolInt32(int32((*TExpr)(unsafe.Pointer(pExpr)).Fop2) == int32(TK_ISNOT)) isTrue = _sqlite3ExprTruthValue(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) if isTrue^isNot != 0 { /* IS TRUE and IS NOT FALSE */ if isNot != 0 { v19 = 0 } else { v19 = int32(SQLITE_JUMPIFNULL) } _sqlite3ExprIfFalse(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, dest, v19) } else { /* IS FALSE and IS NOT TRUE */ if isNot != 0 { v20 = 0 } else { v20 = int32(SQLITE_JUMPIFNULL) } _sqlite3ExprIfTrue(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, dest, v20) } goto _18 _6: ; _5: ; if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_IS) { v21 = int32(TK_NE) } else { v21 = int32(TK_EQ) } op = v21 jumpIfNull = int32(SQLITE_NULLEQ) _12: ; _11: ; _10: ; _9: ; _8: ; _7: ; if _sqlite3ExprIsVector(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) != 0 { goto default_expr } r1 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp) r2 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, bp+4) _codeCompare(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, op, r1, r2, dest, jumpIfNull, libc.BoolInt32((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Commuted)) != uint32(0))) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) goto _18 _14: ; _13: ; r1 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp) _sqlite3VdbeTypeofColumn(tls, v, r1) _sqlite3VdbeAddOp2(tls, v, op, r1, dest) goto _18 _15: ; _exprCodeBetween(tls, pParse, pExpr, dest, __ccgo_fp(_sqlite3ExprIfFalse), jumpIfNull) goto _18 _16: ; if jumpIfNull != 0 { _sqlite3ExprCodeIN(tls, pParse, pExpr, dest, dest) } else { destIfNull = _sqlite3VdbeMakeLabel(tls, pParse) _sqlite3ExprCodeIN(tls, pParse, pExpr, dest, destIfNull) _sqlite3VdbeResolveLabel(tls, v, destIfNull) } goto _18 _17: ; goto default_expr default_expr: ; if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_IsFalse)) == uint32(EP_IsFalse) { _sqlite3VdbeGoto(tls, v, dest) } else { if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_IsTrue)) == uint32(EP_IsTrue) { /* no-op */ } else { r1 = _sqlite3ExprCodeTemp(tls, pParse, pExpr, bp) _sqlite3VdbeAddOp3(tls, v, int32(OP_IfNot), r1, dest, libc.BoolInt32(jumpIfNull != 0)) } } goto _18 _18: ; _sqlite3ReleaseTempReg(tls, pParse, *(*int32)(unsafe.Pointer(bp))) _sqlite3ReleaseTempReg(tls, pParse, *(*int32)(unsafe.Pointer(bp + 4))) } // C documentation // // /* // ** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before // ** code generation, and that copy is deleted after code generation. This // ** ensures that the original pExpr is unchanged. // */ func _sqlite3ExprIfFalseDup(tls *libc.TLS, pParse uintptr, pExpr uintptr, dest int32, jumpIfNull int32) { var db, pCopy uintptr _, _ = db, pCopy db = (*TParse)(unsafe.Pointer(pParse)).Fdb pCopy = _sqlite3ExprDup(tls, db, pExpr, 0) if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 { _sqlite3ExprIfFalse(tls, pParse, pCopy, dest, jumpIfNull) } _sqlite3ExprDelete(tls, db, pCopy) } // C documentation // // /* // ** Expression pVar is guaranteed to be an SQL variable. pExpr may be any // ** type of expression. // ** // ** If pExpr is a simple SQL value - an integer, real, string, blob // ** or NULL value - then the VDBE currently being prepared is configured // ** to re-prepare each time a new value is bound to variable pVar. // ** // ** Additionally, if pExpr is a simple SQL value and the value is the // ** same as that currently bound to variable pVar, non-zero is returned. // ** Otherwise, if the values are not the same or if pExpr is not a simple // ** SQL value, zero is returned. // */ func _exprCompareVariable(tls *libc.TLS, pParse uintptr, pVar uintptr, pExpr uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iVar, res int32 var pL uintptr var _ /* pR at bp+0 */ uintptr _, _, _ = iVar, pL, res res = 0 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) _sqlite3ValueFromExpr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr, uint8(SQLITE_UTF8), uint8(SQLITE_AFF_BLOB), bp) if *(*uintptr)(unsafe.Pointer(bp)) != 0 { iVar = int32((*TExpr)(unsafe.Pointer(pVar)).FiColumn) _sqlite3VdbeSetVarmask(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, iVar) pL = _sqlite3VdbeGetBoundValue(tls, (*TParse)(unsafe.Pointer(pParse)).FpReprepare, iVar, uint8(SQLITE_AFF_BLOB)) if pL != 0 { if Xsqlite3_value_type(tls, pL) == int32(SQLITE_TEXT) { Xsqlite3_value_text(tls, pL) /* Make sure the encoding is UTF-8 */ } res = libc.BoolInt32(0 == _sqlite3MemCompare(tls, pL, *(*uintptr)(unsafe.Pointer(bp)), uintptr(0))) } _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer(bp))) _sqlite3ValueFree(tls, pL) } return res } // C documentation // // /* // ** Do a deep comparison of two expression trees. Return 0 if the two // ** expressions are completely identical. Return 1 if they differ only // ** by a COLLATE operator at the top level. Return 2 if there are differences // ** other than the top-level COLLATE operator. // ** // ** If any subelement of pB has Expr.iTable==(-1) then it is allowed // ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. // ** // ** The pA side might be using TK_REGISTER. If that is the case and pB is // ** not using TK_REGISTER but is otherwise equivalent, then still return 0. // ** // ** Sometimes this routine will return 2 even if the two expressions // ** really are equivalent. If we cannot prove that the expressions are // ** identical, we return 2 just to be safe. So if this routine // ** returns 2, then you do not really know for certain if the two // ** expressions are the same. But if you get a 0 or 1 return, then you // ** can be sure the expressions are the same. In the places where // ** this routine is used, it does not hurt to get an extra 2 - that // ** just might result in some slightly slower code. But returning // ** an incorrect 0 or 1 could lead to a malfunction. // ** // ** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in // ** pParse->pReprepare can be matched against literals in pB. The // ** pParse->pVdbe->expmask bitmask is updated for each variable referenced. // ** If pParse is NULL (the normal case) then any TK_VARIABLE term in // ** Argument pParse should normally be NULL. If it is not NULL and pA or // ** pB causes a return value of 2. // */ func _sqlite3ExprCompare(tls *libc.TLS, pParse uintptr, pA uintptr, pB uintptr, iTab int32) (r int32) { var combinedFlags Tu32 var v1 int32 _, _ = combinedFlags, v1 if pA == uintptr(0) || pB == uintptr(0) { if pB == pA { v1 = 0 } else { v1 = int32(2) } return v1 } if pParse != 0 && int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_VARIABLE) && _exprCompareVariable(tls, pParse, pA, pB) != 0 { return 0 } combinedFlags = (*TExpr)(unsafe.Pointer(pA)).Fflags | (*TExpr)(unsafe.Pointer(pB)).Fflags if combinedFlags&uint32(EP_IntValue) != 0 { if (*TExpr)(unsafe.Pointer(pA)).Fflags&(*TExpr)(unsafe.Pointer(pB)).Fflags&uint32(EP_IntValue) != uint32(0) && *(*int32)(unsafe.Pointer(pA + 8)) == *(*int32)(unsafe.Pointer(pB + 8)) { return 0 } return int32(2) } if int32((*TExpr)(unsafe.Pointer(pA)).Fop) != int32((*TExpr)(unsafe.Pointer(pB)).Fop) || int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_RAISE) { if int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_COLLATE) && _sqlite3ExprCompare(tls, pParse, (*TExpr)(unsafe.Pointer(pA)).FpLeft, pB, iTab) < int32(2) { return int32(1) } if int32((*TExpr)(unsafe.Pointer(pB)).Fop) == int32(TK_COLLATE) && _sqlite3ExprCompare(tls, pParse, pA, (*TExpr)(unsafe.Pointer(pB)).FpLeft, iTab) < int32(2) { return int32(1) } if int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_AGG_COLUMN) && int32((*TExpr)(unsafe.Pointer(pB)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pB)).FiTable < 0 && (*TExpr)(unsafe.Pointer(pA)).FiTable == iTab { /* fall through */ } else { return int32(2) } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(pA + 8)) != 0 { if int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_FUNCTION) || int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_AGG_FUNCTION) { if _sqlite3StrICmp(tls, *(*uintptr)(unsafe.Pointer(pA + 8)), *(*uintptr)(unsafe.Pointer(pB + 8))) != 0 { return int32(2) } _ = libc.Int32FromInt32(0) if libc.BoolInt32((*TExpr)(unsafe.Pointer(pA)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0)) != libc.BoolInt32((*TExpr)(unsafe.Pointer(pB)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0)) { return int32(2) } if (*TExpr)(unsafe.Pointer(pA)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { if _sqlite3WindowCompare(tls, pParse, *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pA)).Fy)), *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pB)).Fy)), int32(1)) != 0 { return int32(2) } } } else { if int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_NULL) { return 0 } else { if int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_COLLATE) { if Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer(pA + 8)), *(*uintptr)(unsafe.Pointer(pB + 8))) != 0 { return int32(2) } } else { if *(*uintptr)(unsafe.Pointer(pB + 8)) != uintptr(0) && int32((*TExpr)(unsafe.Pointer(pA)).Fop) != int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(pA)).Fop) != int32(TK_AGG_COLUMN) && libc.Xstrcmp(tls, *(*uintptr)(unsafe.Pointer(pA + 8)), *(*uintptr)(unsafe.Pointer(pB + 8))) != 0 { return int32(2) } } } } } if (*TExpr)(unsafe.Pointer(pA)).Fflags&uint32(libc.Int32FromInt32(EP_Distinct)|libc.Int32FromInt32(EP_Commuted)) != (*TExpr)(unsafe.Pointer(pB)).Fflags&uint32(libc.Int32FromInt32(EP_Distinct)|libc.Int32FromInt32(EP_Commuted)) { return int32(2) } if combinedFlags&uint32(EP_TokenOnly) == uint32(0) { if combinedFlags&uint32(EP_xIsSelect) != 0 { return int32(2) } if combinedFlags&uint32(EP_FixedCol) == uint32(0) && _sqlite3ExprCompare(tls, pParse, (*TExpr)(unsafe.Pointer(pA)).FpLeft, (*TExpr)(unsafe.Pointer(pB)).FpLeft, iTab) != 0 { return int32(2) } if _sqlite3ExprCompare(tls, pParse, (*TExpr)(unsafe.Pointer(pA)).FpRight, (*TExpr)(unsafe.Pointer(pB)).FpRight, iTab) != 0 { return int32(2) } if _sqlite3ExprListCompare(tls, *(*uintptr)(unsafe.Pointer(pA + 20)), *(*uintptr)(unsafe.Pointer(pB + 20)), iTab) != 0 { return int32(2) } if int32((*TExpr)(unsafe.Pointer(pA)).Fop) != int32(TK_STRING) && int32((*TExpr)(unsafe.Pointer(pA)).Fop) != int32(TK_TRUEFALSE) && combinedFlags&uint32(EP_Reduced) == uint32(0) { if int32((*TExpr)(unsafe.Pointer(pA)).FiColumn) != int32((*TExpr)(unsafe.Pointer(pB)).FiColumn) { return int32(2) } if int32((*TExpr)(unsafe.Pointer(pA)).Fop2) != int32((*TExpr)(unsafe.Pointer(pB)).Fop2) && int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_TRUTH) { return int32(2) } if int32((*TExpr)(unsafe.Pointer(pA)).Fop) != int32(TK_IN) && (*TExpr)(unsafe.Pointer(pA)).FiTable != (*TExpr)(unsafe.Pointer(pB)).FiTable && (*TExpr)(unsafe.Pointer(pA)).FiTable != iTab { return int32(2) } } } return 0 } // C documentation // // /* // ** Compare two ExprList objects. Return 0 if they are identical, 1 // ** if they are certainly different, or 2 if it is not possible to // ** determine if they are identical or not. // ** // ** If any subelement of pB has Expr.iTable==(-1) then it is allowed // ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. // ** // ** This routine might return non-zero for equivalent ExprLists. The // ** only consequence will be disabled optimizations. But this routine // ** must never return 0 if the two ExprList objects are different, or // ** a malfunction will result. // ** // ** Two NULL pointers are considered to be the same. But a NULL pointer // ** always differs from a non-NULL pointer. // */ func _sqlite3ExprListCompare(tls *libc.TLS, pA uintptr, pB uintptr, iTab int32) (r int32) { var i, res, v2 int32 var pExprA, pExprB uintptr _, _, _, _, _ = i, pExprA, pExprB, res, v2 if pA == uintptr(0) && pB == uintptr(0) { return 0 } if pA == uintptr(0) || pB == uintptr(0) { return int32(1) } if (*TExprList)(unsafe.Pointer(pA)).FnExpr != (*TExprList)(unsafe.Pointer(pB)).FnExpr { return int32(1) } i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pA)).FnExpr) { break } pExprA = (*(*TExprList_item)(unsafe.Pointer(pA + 8 + uintptr(i)*20))).FpExpr pExprB = (*(*TExprList_item)(unsafe.Pointer(pB + 8 + uintptr(i)*20))).FpExpr if int32((*(*TExprList_item)(unsafe.Pointer(pA + 8 + uintptr(i)*20))).Ffg.FsortFlags) != int32((*(*TExprList_item)(unsafe.Pointer(pB + 8 + uintptr(i)*20))).Ffg.FsortFlags) { return int32(1) } v2 = _sqlite3ExprCompare(tls, uintptr(0), pExprA, pExprB, iTab) res = v2 if v2 != 0 { return res } goto _1 _1: ; i++ } return 0 } // C documentation // // /* // ** Like sqlite3ExprCompare() except COLLATE operators at the top-level // ** are ignored. // */ func _sqlite3ExprCompareSkip(tls *libc.TLS, pA uintptr, pB uintptr, iTab int32) (r int32) { return _sqlite3ExprCompare(tls, uintptr(0), _sqlite3ExprSkipCollate(tls, pA), _sqlite3ExprSkipCollate(tls, pB), iTab) } // C documentation // // /* // ** Return non-zero if Expr p can only be true if pNN is not NULL. // ** // ** Or if seenNot is true, return non-zero if Expr p can only be // ** non-NULL if pNN is not NULL // */ func _exprImpliesNotNull(tls *libc.TLS, pParse uintptr, p uintptr, pNN uintptr, iTab int32, seenNot int32) (r int32) { var pList uintptr _ = pList _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if _sqlite3ExprCompare(tls, pParse, p, pNN, iTab) == 0 { return libc.BoolInt32(int32((*TExpr)(unsafe.Pointer(pNN)).Fop) != int32(TK_NULL)) } switch int32((*TExpr)(unsafe.Pointer(p)).Fop) { case int32(TK_IN): if seenNot != 0 && (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_xIsSelect)) != uint32(0) { return 0 } _ = libc.Int32FromInt32(0) return _exprImpliesNotNull(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpLeft, pNN, iTab, int32(1)) case int32(TK_BETWEEN): _ = libc.Int32FromInt32(0) pList = *(*uintptr)(unsafe.Pointer(p + 20)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if seenNot != 0 { return 0 } if _exprImpliesNotNull(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8))).FpExpr, pNN, iTab, int32(1)) != 0 || _exprImpliesNotNull(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + 1*20))).FpExpr, pNN, iTab, int32(1)) != 0 { return int32(1) } return _exprImpliesNotNull(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpLeft, pNN, iTab, int32(1)) case int32(TK_EQ): fallthrough case int32(TK_NE): fallthrough case int32(TK_LT): fallthrough case int32(TK_LE): fallthrough case int32(TK_GT): fallthrough case int32(TK_GE): fallthrough case int32(TK_PLUS): fallthrough case int32(TK_MINUS): fallthrough case int32(TK_BITOR): fallthrough case int32(TK_LSHIFT): fallthrough case int32(TK_RSHIFT): fallthrough case int32(TK_CONCAT): seenNot = int32(1) fallthrough case int32(TK_STAR): fallthrough case int32(TK_REM): fallthrough case int32(TK_BITAND): fallthrough case int32(TK_SLASH): if _exprImpliesNotNull(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpRight, pNN, iTab, seenNot) != 0 { return int32(1) } fallthrough case int32(TK_SPAN): fallthrough case int32(TK_COLLATE): fallthrough case int32(TK_UPLUS): fallthrough case int32(TK_UMINUS): return _exprImpliesNotNull(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpLeft, pNN, iTab, seenNot) case int32(TK_TRUTH): if seenNot != 0 { return 0 } if int32((*TExpr)(unsafe.Pointer(p)).Fop2) != int32(TK_IS) { return 0 } return _exprImpliesNotNull(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpLeft, pNN, iTab, int32(1)) case int32(TK_BITNOT): fallthrough case int32(TK_NOT): return _exprImpliesNotNull(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpLeft, pNN, iTab, int32(1)) } return 0 } // C documentation // // /* // ** Return true if we can prove the pE2 will always be true if pE1 is // ** true. Return false if we cannot complete the proof or if pE2 might // ** be false. Examples: // ** // ** pE1: x==5 pE2: x==5 Result: true // ** pE1: x>0 pE2: x==5 Result: false // ** pE1: x=21 pE2: x=21 OR y=43 Result: true // ** pE1: x!=123 pE2: x IS NOT NULL Result: true // ** pE1: x!=?1 pE2: x IS NOT NULL Result: true // ** pE1: x IS NULL pE2: x IS NOT NULL Result: false // ** pE1: x IS ?2 pE2: x IS NOT NULL Result: false // ** // ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has // ** Expr.iTable<0 then assume a table number given by iTab. // ** // ** If pParse is not NULL, then the values of bound variables in pE1 are // ** compared against literal values in pE2 and pParse->pVdbe->expmask is // ** modified to record which bound variables are referenced. If pParse // ** is NULL, then false will be returned if pE1 contains any bound variables. // ** // ** When in doubt, return false. Returning true might give a performance // ** improvement. Returning false might cause a performance reduction, but // ** it will always give the correct answer and is hence always safe. // */ func _sqlite3ExprImpliesExpr(tls *libc.TLS, pParse uintptr, pE1 uintptr, pE2 uintptr, iTab int32) (r int32) { if _sqlite3ExprCompare(tls, pParse, pE1, pE2, iTab) == 0 { return int32(1) } if int32((*TExpr)(unsafe.Pointer(pE2)).Fop) == int32(TK_OR) && (_sqlite3ExprImpliesExpr(tls, pParse, pE1, (*TExpr)(unsafe.Pointer(pE2)).FpLeft, iTab) != 0 || _sqlite3ExprImpliesExpr(tls, pParse, pE1, (*TExpr)(unsafe.Pointer(pE2)).FpRight, iTab) != 0) { return int32(1) } if int32((*TExpr)(unsafe.Pointer(pE2)).Fop) == int32(TK_NOTNULL) && _exprImpliesNotNull(tls, pParse, pE1, (*TExpr)(unsafe.Pointer(pE2)).FpLeft, iTab, 0) != 0 { return int32(1) } return 0 } // C documentation // // /* This is a helper function to impliesNotNullRow(). In this routine, // ** set pWalker->eCode to one only if *both* of the input expressions // ** separately have the implies-not-null-row property. // */ func _bothImplyNotNullRow(tls *libc.TLS, pWalker uintptr, pE1 uintptr, pE2 uintptr) { if int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) == 0 { _sqlite3WalkExpr(tls, pWalker, pE1) if (*TWalker)(unsafe.Pointer(pWalker)).FeCode != 0 { (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0) _sqlite3WalkExpr(tls, pWalker, pE2) } } } // C documentation // // /* // ** This is the Expr node callback for sqlite3ExprImpliesNonNullRow(). // ** If the expression node requires that the table at pWalker->iCur // ** have one or more non-NULL column, then set pWalker->eCode to 1 and abort. // ** // ** pWalker->mWFlags is non-zero if this inquiry is being undertaking on // ** behalf of a RIGHT JOIN (or FULL JOIN). That makes a difference when // ** evaluating terms in the ON clause of an inner join. // ** // ** This routine controls an optimization. False positives (setting // ** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives // ** (never setting pWalker->eCode) is a harmless missed optimization. // */ func _impliesNotNullRow(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var pLeft, pRight uintptr _, _ = pLeft, pRight if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) { return int32(WRC_Prune) } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_InnerON)) != uint32(0) && (*TWalker)(unsafe.Pointer(pWalker)).FmWFlags != 0 { /* If iCur is used in an inner-join ON clause to the left of a ** RIGHT JOIN, that does *not* mean that the table must be non-null. ** But it is difficult to check for that condition precisely. ** To keep things simple, any use of iCur from any inner-join is ** ignored while attempting to simplify a RIGHT JOIN. */ return int32(WRC_Prune) } switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) { case int32(TK_ISNOT): fallthrough case int32(TK_ISNULL): fallthrough case int32(TK_NOTNULL): fallthrough case int32(TK_IS): fallthrough case int32(TK_VECTOR): fallthrough case int32(TK_FUNCTION): fallthrough case int32(TK_TRUTH): fallthrough case int32(TK_CASE): return int32(WRC_Prune) case int32(TK_COLUMN): if *(*int32)(unsafe.Pointer(pWalker + 24)) == (*TExpr)(unsafe.Pointer(pExpr)).FiTable { (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(1) return int32(WRC_Abort) } return int32(WRC_Prune) case int32(TK_OR): fallthrough case int32(TK_AND): /* Both sides of an AND or OR must separately imply non-null-row. ** Consider these cases: ** 1. NOT (x AND y) ** 2. x OR y ** If only one of x or y is non-null-row, then the overall expression ** can be true if the other arm is false (case 1) or true (case 2). */ _bothImplyNotNullRow(tls, pWalker, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) return int32(WRC_Prune) case int32(TK_IN): /* Beware of "x NOT IN ()" and "x NOT IN (SELECT 1 WHERE false)", ** both of which can be true. But apart from these cases, if ** the left-hand side of the IN is NULL then the IN itself will be ** NULL. */ if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) == uint32(0) && (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr > 0 { _sqlite3WalkExpr(tls, pWalker, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) } return int32(WRC_Prune) case int32(TK_BETWEEN): /* In "x NOT BETWEEN y AND z" either x must be non-null-row or else ** both y and z must be non-null row */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3WalkExpr(tls, pWalker, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) _bothImplyNotNullRow(tls, pWalker, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8))).FpExpr, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8 + 1*20))).FpExpr) return int32(WRC_Prune) /* Virtual tables are allowed to use constraints like x=NULL. So ** a term of the form x=y does not prove that y is not null if x ** is the column of a virtual table */ fallthrough case int32(TK_EQ): fallthrough case int32(TK_NE): fallthrough case int32(TK_LT): fallthrough case int32(TK_LE): fallthrough case int32(TK_GT): fallthrough case int32(TK_GE): pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft pRight = (*TExpr)(unsafe.Pointer(pExpr)).FpRight /* The y.pTab=0 assignment in wherecode.c always happens after the ** impliesNotNullRow() test */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pLeft)).Fy.FpTab != uintptr(0) && int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pLeft)).Fy.FpTab)).FeTabType) == int32(TABTYP_VTAB) || int32((*TExpr)(unsafe.Pointer(pRight)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pRight)).Fy.FpTab != uintptr(0) && int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pRight)).Fy.FpTab)).FeTabType) == int32(TABTYP_VTAB) { return int32(WRC_Prune) } fallthrough default: return WRC_Continue } return r } // C documentation // // /* // ** Return true (non-zero) if expression p can only be true if at least // ** one column of table iTab is non-null. In other words, return true // ** if expression p will always be NULL or false if every column of iTab // ** is NULL. // ** // ** False negatives are acceptable. In other words, it is ok to return // ** zero even if expression p will never be true of every column of iTab // ** is NULL. A false negative is merely a missed optimization opportunity. // ** // ** False positives are not allowed, however. A false positive may result // ** in an incorrect answer. // ** // ** Terms of p that are marked with EP_OuterON (and hence that come from // ** the ON or USING clauses of OUTER JOINS) are excluded from the analysis. // ** // ** This routine is used to check if a LEFT JOIN can be converted into // ** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE // ** clause requires that some column of the right table of the LEFT JOIN // ** be non-NULL, then the LEFT JOIN can be safely converted into an // ** ordinary join. // */ func _sqlite3ExprImpliesNonNullRow(tls *libc.TLS, p uintptr, iTab int32, isRJ int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* w at bp+0 */ TWalker p = _sqlite3ExprSkipCollateAndLikely(tls, p) if p == uintptr(0) { return 0 } if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_NOTNULL) { p = (*TExpr)(unsafe.Pointer(p)).FpLeft } else { for int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_AND) { if _sqlite3ExprImpliesNonNullRow(tls, (*TExpr)(unsafe.Pointer(p)).FpLeft, iTab, isRJ) != 0 { return int32(1) } p = (*TExpr)(unsafe.Pointer(p)).FpRight } } (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_impliesNotNullRow) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = uintptr(0) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback2 = uintptr(0) (*(*TWalker)(unsafe.Pointer(bp))).FeCode = uint16(0) (*(*TWalker)(unsafe.Pointer(bp))).FmWFlags = libc.BoolUint16(isRJ != 0) *(*int32)(unsafe.Pointer(bp + 24)) = iTab _sqlite3WalkExpr(tls, bp, p) return int32((*(*TWalker)(unsafe.Pointer(bp))).FeCode) } /* ** An instance of the following structure is used by the tree walker ** to determine if an expression can be evaluated by reference to the ** index only, without having to do a search for the corresponding ** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur ** is the cursor for the table. */ type TIdxCover = struct { FpIdx uintptr FiCur int32 } type IdxCover = TIdxCover // C documentation // // /* // ** Check to see if there are references to columns in table // ** pWalker->u.pIdxCover->iCur can be satisfied using the index // ** pWalker->u.pIdxCover->pIdx. // */ func _exprIdxCover(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*TIdxCover)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalker + 24)))).FiCur && int32(_sqlite3TableColumnToIndex(tls, (*TIdxCover)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalker + 24)))).FpIdx, (*TExpr)(unsafe.Pointer(pExpr)).FiColumn)) < 0 { (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(1) return int32(WRC_Abort) } return WRC_Continue } // C documentation // // /* // ** Determine if an index pIdx on table with cursor iCur contains will // ** the expression pExpr. Return true if the index does cover the // ** expression and false if the pExpr expression references table columns // ** that are not found in the index pIdx. // ** // ** An index covering an expression means that the expression can be // ** evaluated using only the index and without having to lookup the // ** corresponding table entry. // */ func _sqlite3ExprCoveredByIndex(tls *libc.TLS, pExpr uintptr, iCur int32, pIdx uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* w at bp+0 */ TWalker var _ /* xcov at bp+32 */ TIdxCover libc.Xmemset(tls, bp, 0, uint32(28)) (*(*TIdxCover)(unsafe.Pointer(bp + 32))).FiCur = iCur (*(*TIdxCover)(unsafe.Pointer(bp + 32))).FpIdx = pIdx (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_exprIdxCover) *(*uintptr)(unsafe.Pointer(bp + 24)) = bp + 32 _sqlite3WalkExpr(tls, bp, pExpr) return libc.BoolInt32(!((*(*TWalker)(unsafe.Pointer(bp))).FeCode != 0)) } /* Structure used to pass information throughout the Walker in order to ** implement sqlite3ReferencesSrcList(). */ type TRefSrcList = struct { F__ccgo_align [0]uint32 Fdb uintptr FpRef uintptr FnExclude Ti64 FaiExclude uintptr F__ccgo_pad4 [4]byte } type RefSrcList = TRefSrcList // C documentation // // /* // ** Walker SELECT callbacks for sqlite3ReferencesSrcList(). // ** // ** When entering a new subquery on the pExpr argument, add all FROM clause // ** entries for that subquery to the exclude list. // ** // ** When leaving the subquery, remove those entries from the exclude list. // */ func _selectRefEnter(tls *libc.TLS, pWalker uintptr, pSelect uintptr) (r int32) { var i, j Ti64 var p, pSrc, piNew uintptr _, _, _, _, _ = i, j, p, pSrc, piNew p = *(*uintptr)(unsafe.Pointer(pWalker + 24)) pSrc = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc if (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc == 0 { return WRC_Continue } j = (*TRefSrcList)(unsafe.Pointer(p)).FnExclude *(*Ti64)(unsafe.Pointer(p + 8)) += int64((*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) piNew = _sqlite3DbRealloc(tls, (*TRefSrcList)(unsafe.Pointer(p)).Fdb, (*TRefSrcList)(unsafe.Pointer(p)).FaiExclude, uint64((*TRefSrcList)(unsafe.Pointer(p)).FnExclude*int64(4))) if piNew == uintptr(0) { (*TRefSrcList)(unsafe.Pointer(p)).FnExclude = 0 return int32(WRC_Abort) } else { (*TRefSrcList)(unsafe.Pointer(p)).FaiExclude = piNew } i = 0 for { if !(i < int64((*TSrcList)(unsafe.Pointer(pSrc)).FnSrc)) { break } *(*int32)(unsafe.Pointer((*TRefSrcList)(unsafe.Pointer(p)).FaiExclude + uintptr(j)*4)) = (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72))).FiCursor goto _1 _1: ; i++ j++ } return WRC_Continue } func _selectRefLeave(tls *libc.TLS, pWalker uintptr, pSelect uintptr) { var p, pSrc uintptr _, _ = p, pSrc p = *(*uintptr)(unsafe.Pointer(pWalker + 24)) pSrc = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc if (*TRefSrcList)(unsafe.Pointer(p)).FnExclude != 0 { _ = libc.Int32FromInt32(0) *(*Ti64)(unsafe.Pointer(p + 8)) -= int64((*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) } } // C documentation // // /* This is the Walker EXPR callback for sqlite3ReferencesSrcList(). // ** // ** Set the 0x01 bit of pWalker->eCode if there is a reference to any // ** of the tables shown in RefSrcList.pRef. // ** // ** Set the 0x02 bit of pWalker->eCode if there is a reference to a // ** table is in neither RefSrcList.pRef nor RefSrcList.aiExclude. // */ func _exprRefToSrcList(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var i, nSrc, v1 int32 var p, pSrc, p3, p5 uintptr _, _, _, _, _, _, _ = i, nSrc, p, pSrc, v1, p3, p5 if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_COLUMN) { p = *(*uintptr)(unsafe.Pointer(pWalker + 24)) pSrc = (*TRefSrcList)(unsafe.Pointer(p)).FpRef if pSrc != 0 { v1 = (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc } else { v1 = 0 } nSrc = v1 i = 0 for { if !(i < nSrc) { break } if (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72))).FiCursor { p3 = pWalker + 20 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) | libc.Int32FromInt32(1)) return WRC_Continue } goto _2 _2: ; i++ } i = 0 for { if !(int64(i) < (*TRefSrcList)(unsafe.Pointer(p)).FnExclude && *(*int32)(unsafe.Pointer((*TRefSrcList)(unsafe.Pointer(p)).FaiExclude + uintptr(i)*4)) != (*TExpr)(unsafe.Pointer(pExpr)).FiTable) { break } goto _4 _4: ; i++ } if int64(i) >= (*TRefSrcList)(unsafe.Pointer(p)).FnExclude { p5 = pWalker + 20 *(*Tu16)(unsafe.Pointer(p5)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p5))) | libc.Int32FromInt32(2)) } } return WRC_Continue } // C documentation // // /* // ** Check to see if pExpr references any tables in pSrcList. // ** Possible return values: // ** // ** 1 pExpr does references a table in pSrcList. // ** // ** 0 pExpr references some table that is not defined in either // ** pSrcList or in subqueries of pExpr itself. // ** // ** -1 pExpr only references no tables at all, or it only // ** references tables defined in subqueries of pExpr itself. // ** // ** As currently used, pExpr is always an aggregate function call. That // ** fact is exploited for efficiency. // */ func _sqlite3ReferencesSrcList(tls *libc.TLS, pParse uintptr, pExpr uintptr, pSrcList uintptr) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var _ /* w at bp+0 */ TWalker var _ /* x at bp+32 */ TRefSrcList _ = libc.Int32FromInt32(0) libc.Xmemset(tls, bp, 0, uint32(28)) libc.Xmemset(tls, bp+32, 0, uint32(24)) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_exprRefToSrcList) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_selectRefEnter) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback2 = __ccgo_fp(_selectRefLeave) *(*uintptr)(unsafe.Pointer(bp + 24)) = bp + 32 (*(*TRefSrcList)(unsafe.Pointer(bp + 32))).Fdb = (*TParse)(unsafe.Pointer(pParse)).Fdb (*(*TRefSrcList)(unsafe.Pointer(bp + 32))).FpRef = pSrcList _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3WalkExprList(tls, bp, *(*uintptr)(unsafe.Pointer(pExpr + 20))) if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3WalkExprList(tls, bp, *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft + 20))) } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { _sqlite3WalkExpr(tls, bp, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)))).FpFilter) } if (*(*TRefSrcList)(unsafe.Pointer(bp + 32))).FaiExclude != 0 { _sqlite3DbNNFreeNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*(*TRefSrcList)(unsafe.Pointer(bp + 32))).FaiExclude) } if int32((*(*TWalker)(unsafe.Pointer(bp))).FeCode)&int32(0x01) != 0 { return int32(1) } else { if (*(*TWalker)(unsafe.Pointer(bp))).FeCode != 0 { return 0 } else { return -int32(1) } } return r } // C documentation // // /* // ** This is a Walker expression node callback. // ** // ** For Expr nodes that contain pAggInfo pointers, make sure the AggInfo // ** object that is referenced does not refer directly to the Expr. If // ** it does, make a copy. This is done because the pExpr argument is // ** subject to change. // ** // ** The copy is scheduled for deletion using the sqlite3ExprDeferredDelete() // ** which builds on the sqlite3ParserAddCleanup() mechanism. // */ func _agginfoPersistExprCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var db, pAggInfo, pParse uintptr var iAgg int32 _, _, _, _ = db, iAgg, pAggInfo, pParse if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_TokenOnly)|libc.Int32FromInt32(EP_Reduced)) != libc.Uint32FromInt32(0)) && (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo != uintptr(0) { pAggInfo = (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo iAgg = int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg) pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_AGG_FUNCTION) { if iAgg < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn && (*(*TAggInfo_col)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr(iAgg)*16))).FpCExpr == pExpr { pExpr = _sqlite3ExprDup(tls, db, pExpr, 0) if pExpr != 0 { (*(*TAggInfo_col)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr(iAgg)*16))).FpCExpr = pExpr _sqlite3ExprDeferredDelete(tls, pParse, pExpr) } } } else { _ = libc.Int32FromInt32(0) if iAgg < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc && (*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(iAgg)*24))).FpFExpr == pExpr { pExpr = _sqlite3ExprDup(tls, db, pExpr, 0) if pExpr != 0 { (*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(iAgg)*24))).FpFExpr = pExpr _sqlite3ExprDeferredDelete(tls, pParse, pExpr) } } } } return WRC_Continue } // C documentation // // /* // ** Initialize a Walker object so that will persist AggInfo entries referenced // ** by the tree that is walked. // */ func _sqlite3AggInfoPersistWalkerInit(tls *libc.TLS, pWalker uintptr, pParse uintptr) { libc.Xmemset(tls, pWalker, 0, uint32(28)) (*TWalker)(unsafe.Pointer(pWalker)).FpParse = pParse (*TWalker)(unsafe.Pointer(pWalker)).FxExprCallback = __ccgo_fp(_agginfoPersistExprCb) (*TWalker)(unsafe.Pointer(pWalker)).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop) } // C documentation // // /* // ** Add a new element to the pAggInfo->aCol[] array. Return the index of // ** the new element. Return a negative number if malloc fails. // */ func _addAggInfoColumn(tls *libc.TLS, db uintptr, pInfo uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* i at bp+0 */ int32 (*TAggInfo)(unsafe.Pointer(pInfo)).FaCol = _sqlite3ArrayAllocate(tls, db, (*TAggInfo)(unsafe.Pointer(pInfo)).FaCol, int32(16), pInfo+24, bp) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Add a new element to the pAggInfo->aFunc[] array. Return the index of // ** the new element. Return a negative number if malloc fails. // */ func _addAggInfoFunc(tls *libc.TLS, db uintptr, pInfo uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* i at bp+0 */ int32 (*TAggInfo)(unsafe.Pointer(pInfo)).FaFunc = _sqlite3ArrayAllocate(tls, db, (*TAggInfo)(unsafe.Pointer(pInfo)).FaFunc, int32(24), pInfo+36, bp) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Search the AggInfo object for an aCol[] entry that has iTable and iColumn. // ** Return the index in aCol[] of the entry that describes that column. // ** // ** If no prior entry is found, create a new one and return -1. The // ** new column will have an index of pAggInfo->nColumn-1. // */ func _findOrCreateAggInfoColumn(tls *libc.TLS, pParse uintptr, pAggInfo uintptr, pExpr uintptr) { var j, k, n int32 var pCol, pE, pGB, pTerm, v4 uintptr var v3 Tu16 _, _, _, _, _, _, _, _, _ = j, k, n, pCol, pE, pGB, pTerm, v3, v4 _ = libc.Int32FromInt32(0) pCol = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol k = 0 for { if !(k < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn) { break } if (*TAggInfo_col)(unsafe.Pointer(pCol)).FpCExpr == pExpr { return } if (*TAggInfo_col)(unsafe.Pointer(pCol)).FiTable == (*TExpr)(unsafe.Pointer(pExpr)).FiTable && int32((*TAggInfo_col)(unsafe.Pointer(pCol)).FiColumn) == int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_IF_NULL_ROW) { goto fix_up_expr } goto _1 _1: ; k++ pCol += 16 } k = _addAggInfoColumn(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pAggInfo) if k < 0 { /* OOM on resize */ _ = libc.Int32FromInt32(0) return } pCol = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr(k)*16 _ = libc.Int32FromInt32(0) (*TAggInfo_col)(unsafe.Pointer(pCol)).FpTab = (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab (*TAggInfo_col)(unsafe.Pointer(pCol)).FiTable = (*TExpr)(unsafe.Pointer(pExpr)).FiTable (*TAggInfo_col)(unsafe.Pointer(pCol)).FiColumn = (*TExpr)(unsafe.Pointer(pExpr)).FiColumn (*TAggInfo_col)(unsafe.Pointer(pCol)).FiSorterColumn = int16(-int32(1)) (*TAggInfo_col)(unsafe.Pointer(pCol)).FpCExpr = pExpr if (*TAggInfo)(unsafe.Pointer(pAggInfo)).FpGroupBy != 0 && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_IF_NULL_ROW) { pGB = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FpGroupBy pTerm = pGB + 8 n = (*TExprList)(unsafe.Pointer(pGB)).FnExpr j = 0 for { if !(j < n) { break } pE = (*TExprList_item)(unsafe.Pointer(pTerm)).FpExpr if int32((*TExpr)(unsafe.Pointer(pE)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pE)).FiTable == (*TExpr)(unsafe.Pointer(pExpr)).FiTable && int32((*TExpr)(unsafe.Pointer(pE)).FiColumn) == int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) { (*TAggInfo_col)(unsafe.Pointer(pCol)).FiSorterColumn = int16(j) break } goto _2 _2: ; j++ pTerm += 20 } } if int32((*TAggInfo_col)(unsafe.Pointer(pCol)).FiSorterColumn) < 0 { v4 = pAggInfo + 2 v3 = *(*Tu16)(unsafe.Pointer(v4)) *(*Tu16)(unsafe.Pointer(v4))++ (*TAggInfo_col)(unsafe.Pointer(pCol)).FiSorterColumn = int16(v3) } goto fix_up_expr fix_up_expr: ; _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo = pAggInfo if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) { (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_AGG_COLUMN) } (*TExpr)(unsafe.Pointer(pExpr)).FiAgg = int16(k) } // C documentation // // /* // ** This is the xExprCallback for a tree walker. It is used to // ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates // ** for additional information. // */ func _analyzeAggregate(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var enc Tu8 var i, iDataCur, nArg, v5, v6, v8 int32 var pAggInfo, pIEpr, pItem, pItem1, pNC, pOBList, pParse, pSrcList, v7, v9 uintptr var _ /* tmp at bp+0 */ TExpr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = enc, i, iDataCur, nArg, pAggInfo, pIEpr, pItem, pItem1, pNC, pOBList, pParse, pSrcList, v5, v6, v7, v8, v9 pNC = *(*uintptr)(unsafe.Pointer(pWalker + 24)) pParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse pSrcList = (*TNameContext)(unsafe.Pointer(pNC)).FpSrcList pAggInfo = *(*uintptr)(unsafe.Pointer(pNC + 8)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) { default: _ = libc.Int32FromInt32(0) if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_InAggFunc) == 0 { break } if (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr == uintptr(0) { break } pIEpr = (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr for { if !(pIEpr != 0) { break } iDataCur = (*TIndexedExpr)(unsafe.Pointer(pIEpr)).FiDataCur if iDataCur < 0 { goto _1 } if _sqlite3ExprCompare(tls, uintptr(0), pExpr, (*TIndexedExpr)(unsafe.Pointer(pIEpr)).FpExpr, iDataCur) == 0 { break } goto _1 _1: ; pIEpr = (*TIndexedExpr)(unsafe.Pointer(pIEpr)).FpIENext } if pIEpr == uintptr(0) { break } if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)|libc.Int32FromInt32(EP_Subrtn)) == libc.Uint32FromInt32(0)) { break } i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(pSrcList)).FnSrc) { break } if (*(*TSrcItem)(unsafe.Pointer(pSrcList + 8))).FiCursor == (*TIndexedExpr)(unsafe.Pointer(pIEpr)).FiDataCur { break } goto _2 _2: ; i++ } if i >= (*TSrcList)(unsafe.Pointer(pSrcList)).FnSrc { break } if (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo != uintptr(0) { break } /* Resolved by outer context */ if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return int32(WRC_Abort) } /* If we reach this point, it means that expression pExpr can be ** translated into a reference to an index column as described by ** pIEpr. */ libc.Xmemset(tls, bp, 0, uint32(52)) (*(*TExpr)(unsafe.Pointer(bp))).Fop = uint8(TK_AGG_COLUMN) (*(*TExpr)(unsafe.Pointer(bp))).FiTable = (*TIndexedExpr)(unsafe.Pointer(pIEpr)).FiIdxCur (*(*TExpr)(unsafe.Pointer(bp))).FiColumn = int16((*TIndexedExpr)(unsafe.Pointer(pIEpr)).FiIdxCol) _findOrCreateAggInfoColumn(tls, pParse, pAggInfo, bp) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return int32(WRC_Abort) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*(*TAggInfo_col)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr((*(*TExpr)(unsafe.Pointer(bp))).FiAgg)*16))).FpCExpr = pExpr (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo = pAggInfo (*TExpr)(unsafe.Pointer(pExpr)).FiAgg = (*(*TExpr)(unsafe.Pointer(bp))).FiAgg return int32(WRC_Prune) case int32(TK_IF_NULL_ROW): fallthrough case int32(TK_AGG_COLUMN): fallthrough case int32(TK_COLUMN): /* Check to see if the column is in one of the tables in the FROM ** clause of the aggregate query */ if pSrcList != uintptr(0) { pItem = pSrcList + 8 i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(pSrcList)).FnSrc) { break } _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor { _findOrCreateAggInfoColumn(tls, pParse, pAggInfo, pExpr) break } /* endif pExpr->iTable==pItem->iCursor */ goto _3 _3: ; i++ pItem += 72 } /* end loop over pSrcList */ } return WRC_Continue case int32(TK_AGG_FUNCTION): if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_InAggFunc) == 0 && (*TWalker)(unsafe.Pointer(pWalker)).FwalkerDepth == int32((*TExpr)(unsafe.Pointer(pExpr)).Fop2) && (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo == uintptr(0) { /* Check to see if pExpr is a duplicate of another aggregate ** function that is already in the pAggInfo structure */ pItem1 = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc i = 0 for { if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc) { break } if (*TAggInfo_func)(unsafe.Pointer(pItem1)).FpFExpr == pExpr { break } if _sqlite3ExprCompare(tls, uintptr(0), (*TAggInfo_func)(unsafe.Pointer(pItem1)).FpFExpr, pExpr, -int32(1)) == 0 { break } goto _4 _4: ; i++ pItem1 += 24 } if i >= (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc { /* pExpr is original. Make a new entry in pAggInfo->aFunc[] */ enc = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fenc i = _addAggInfoFunc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pAggInfo) if i >= 0 { _ = libc.Int32FromInt32(0) pItem1 = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(i)*24 (*TAggInfo_func)(unsafe.Pointer(pItem1)).FpFExpr = pExpr _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(pExpr + 20)) != 0 { v5 = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr } else { v5 = 0 } nArg = v5 (*TAggInfo_func)(unsafe.Pointer(pItem1)).FpFunc = _sqlite3FindFunction(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(pExpr + 8)), nArg, enc, uint8(0)) _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 && (*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pItem1)).FpFunc)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) == uint32(0) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) v7 = pParse + 40 v6 = *(*int32)(unsafe.Pointer(v7)) *(*int32)(unsafe.Pointer(v7))++ (*TAggInfo_func)(unsafe.Pointer(pItem1)).FiOBTab = v6 pOBList = *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft + 20)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TExprList)(unsafe.Pointer(pOBList)).FnExpr == int32(1) && nArg == int32(1) && _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pOBList + 8))).FpExpr, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8))).FpExpr, 0) == 0 { (*TAggInfo_func)(unsafe.Pointer(pItem1)).FbOBPayload = uint8(0) (*TAggInfo_func)(unsafe.Pointer(pItem1)).FbOBUnique = libc.BoolUint8((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Distinct)) != libc.Uint32FromInt32(0)) } else { (*TAggInfo_func)(unsafe.Pointer(pItem1)).FbOBPayload = uint8(1) } (*TAggInfo_func)(unsafe.Pointer(pItem1)).FbUseSubtype = libc.BoolUint8((*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pItem1)).FpFunc)).FfuncFlags&uint32(SQLITE_SUBTYPE) != uint32(0)) } else { (*TAggInfo_func)(unsafe.Pointer(pItem1)).FiOBTab = -int32(1) } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Distinct)) != uint32(0) && !((*TAggInfo_func)(unsafe.Pointer(pItem1)).FbOBUnique != 0) { v9 = pParse + 40 v8 = *(*int32)(unsafe.Pointer(v9)) *(*int32)(unsafe.Pointer(v9))++ (*TAggInfo_func)(unsafe.Pointer(pItem1)).FiDistinct = v8 } else { (*TAggInfo_func)(unsafe.Pointer(pItem1)).FiDistinct = -int32(1) } } } /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry */ _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer(pExpr)).FiAgg = int16(i) (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo = pAggInfo return int32(WRC_Prune) } else { return WRC_Continue } } return WRC_Continue } // C documentation // // /* // ** Analyze the pExpr expression looking for aggregate functions and // ** for variables that need to be added to AggInfo object that pNC->pAggInfo // ** points to. Additional entries are made on the AggInfo object as // ** necessary. // ** // ** This routine should only be called after the expression has been // ** analyzed by sqlite3ResolveExprNames(). // */ func _sqlite3ExprAnalyzeAggregates(tls *libc.TLS, pNC uintptr, pExpr uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* w at bp+0 */ TWalker (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_analyzeAggregate) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_sqlite3WalkerDepthIncrease) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback2 = __ccgo_fp(_sqlite3WalkerDepthDecrease) (*(*TWalker)(unsafe.Pointer(bp))).FwalkerDepth = 0 *(*uintptr)(unsafe.Pointer(bp + 24)) = pNC (*(*TWalker)(unsafe.Pointer(bp))).FpParse = uintptr(0) _ = libc.Int32FromInt32(0) _sqlite3WalkExpr(tls, bp, pExpr) } // C documentation // // /* // ** Call sqlite3ExprAnalyzeAggregates() for every expression in an // ** expression list. Return the number of errors. // ** // ** If an error is found, the analysis is cut short. // */ func _sqlite3ExprAnalyzeAggList(tls *libc.TLS, pNC uintptr, pList uintptr) { var i int32 var pItem uintptr _, _ = i, pItem if pList != 0 { pItem = pList + 8 i = libc.Int32FromInt32(0) for { if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } _sqlite3ExprAnalyzeAggregates(tls, pNC, (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr) goto _1 _1: ; i++ pItem += 20 } } } // C documentation // // /* // ** Allocate a single new register for use to hold some intermediate result. // */ func _sqlite3GetTempReg(tls *libc.TLS, pParse uintptr) (r int32) { var v1 int32 var v2, v4 uintptr var v3 Tu8 _, _, _, _ = v1, v2, v3, v4 if int32((*TParse)(unsafe.Pointer(pParse)).FnTempReg) == 0 { v2 = pParse + 44 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) return v1 } v4 = pParse + 19 *(*Tu8)(unsafe.Pointer(v4))-- v3 = *(*Tu8)(unsafe.Pointer(v4)) return *(*int32)(unsafe.Pointer(pParse + 164 + uintptr(v3)*4)) } // C documentation // // /* // ** Deallocate a register, making available for reuse for some other // ** purpose. // */ func _sqlite3ReleaseTempReg(tls *libc.TLS, pParse uintptr, iReg int32) { var v1 Tu8 var v2 uintptr _, _ = v1, v2 if iReg != 0 { if int32((*TParse)(unsafe.Pointer(pParse)).FnTempReg) < int32(libc.Uint32FromInt64(32)/libc.Uint32FromInt64(4)) { v2 = pParse + 19 v1 = *(*Tu8)(unsafe.Pointer(v2)) *(*Tu8)(unsafe.Pointer(v2))++ *(*int32)(unsafe.Pointer(pParse + 164 + uintptr(v1)*4)) = iReg } } } // C documentation // // /* // ** Allocate or deallocate a block of nReg consecutive registers. // */ func _sqlite3GetTempRange(tls *libc.TLS, pParse uintptr, nReg int32) (r int32) { var i, n int32 _, _ = i, n if nReg == int32(1) { return _sqlite3GetTempReg(tls, pParse) } i = (*TParse)(unsafe.Pointer(pParse)).FiRangeReg n = (*TParse)(unsafe.Pointer(pParse)).FnRangeReg if nReg <= n { *(*int32)(unsafe.Pointer(pParse + 32)) += nReg *(*int32)(unsafe.Pointer(pParse + 28)) -= nReg } else { i = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nReg } return i } func _sqlite3ReleaseTempRange(tls *libc.TLS, pParse uintptr, iReg int32, nReg int32) { if nReg == int32(1) { _sqlite3ReleaseTempReg(tls, pParse, iReg) return } if nReg > (*TParse)(unsafe.Pointer(pParse)).FnRangeReg { (*TParse)(unsafe.Pointer(pParse)).FnRangeReg = nReg (*TParse)(unsafe.Pointer(pParse)).FiRangeReg = iReg } } // C documentation // // /* // ** Mark all temporary registers as being unavailable for reuse. // ** // ** Always invoke this procedure after coding a subroutine or co-routine // ** that might be invoked from other parts of the code, to ensure that // ** the sub/co-routine does not use registers in common with the code that // ** invokes the sub/co-routine. // */ func _sqlite3ClearTempRegCache(tls *libc.TLS, pParse uintptr) { (*TParse)(unsafe.Pointer(pParse)).FnTempReg = uint8(0) (*TParse)(unsafe.Pointer(pParse)).FnRangeReg = 0 } // C documentation // // /* // ** Make sure sufficient registers have been allocated so that // ** iReg is a valid register number. // */ func _sqlite3TouchRegister(tls *libc.TLS, pParse uintptr, iReg int32) { if (*TParse)(unsafe.Pointer(pParse)).FnMem < iReg { (*TParse)(unsafe.Pointer(pParse)).FnMem = iReg } } // C documentation // // /* // ** Return the latest reusable register in the set of all registers. // ** The value returned is no less than iMin. If any register iMin or // ** greater is in permanent use, then return one more than that last // ** permanent register. // */ func _sqlite3FirstAvailableRegister(tls *libc.TLS, pParse uintptr, iMin int32) (r int32) { var i int32 var pList uintptr _, _ = i, pList pList = (*TParse)(unsafe.Pointer(pParse)).FpConstExpr if pList != 0 { i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } if *(*int32)(unsafe.Pointer(pList + 8 + uintptr(i)*20 + 16)) >= iMin { iMin = *(*int32)(unsafe.Pointer(pList + 8 + uintptr(i)*20 + 16)) + int32(1) } goto _1 _1: ; i++ } } (*TParse)(unsafe.Pointer(pParse)).FnTempReg = uint8(0) (*TParse)(unsafe.Pointer(pParse)).FnRangeReg = 0 return iMin } /* ** Validate that no temporary register falls within the range of ** iFirst..iLast, inclusive. This routine is only call from within assert() ** statements. */ /************** End of expr.c ************************************************/ /************** Begin file alter.c *******************************************/ /* ** 2005 February 15 ** ** 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 contains C code routines that used to generate VDBE code ** that implements the ALTER TABLE command. */ /* #include "sqliteInt.h" */ /* ** The code in this file only exists if we are not omitting the ** ALTER TABLE logic from the build. */ // C documentation // // /* // ** Parameter zName is the name of a table that is about to be altered // ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN). // ** If the table is a system table, this function leaves an error message // ** in pParse->zErr (system tables may not be altered) and returns non-zero. // ** // ** Or, if zName is not a system table, zero is returned. // */ func _isAlterableTable(tls *libc.TLS, pParse uintptr, pTab uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) if 0 == Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName, __ccgo_ts+6525, int32(7)) || (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Eponymous) != uint32(0) || (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Shadow) != uint32(0) && _sqlite3ReadOnlyShadowTables(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8324, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) return int32(1) } return 0 } // C documentation // // /* // ** Generate code to verify that the schemas of database zDb and, if // ** bTemp is not true, database "temp", can still be parsed. This is // ** called at the end of the generation of an ALTER TABLE ... RENAME ... // ** statement to ensure that the operation has not rendered any schema // ** objects unusable. // */ func _renameTestSchema(tls *libc.TLS, pParse uintptr, zDb uintptr, bTemp int32, zWhen uintptr, bNoDQS int32) { bp := tls.Alloc(48) defer tls.Free(48) (*TParse)(unsafe.Pointer(pParse)).FcolNamesSet = uint8(1) _sqlite3NestedParse(tls, pParse, __ccgo_ts+8352, libc.VaList(bp+8, zDb, zDb, bTemp, zWhen, bNoDQS)) if bTemp == 0 { _sqlite3NestedParse(tls, pParse, __ccgo_ts+8527, libc.VaList(bp+8, zDb, zWhen, bNoDQS)) } } // C documentation // // /* // ** Generate VM code to replace any double-quoted strings (but not double-quoted // ** identifiers) within the "sql" column of the sqlite_schema table in // ** database zDb with their single-quoted equivalents. If argument bTemp is // ** not true, similarly update all SQL statements in the sqlite_schema table // ** of the temp db. // */ func _renameFixQuotes(tls *libc.TLS, pParse uintptr, zDb uintptr, bTemp int32) { bp := tls.Alloc(32) defer tls.Free(32) _sqlite3NestedParse(tls, pParse, __ccgo_ts+8701, libc.VaList(bp+8, zDb, zDb)) if bTemp == 0 { _sqlite3NestedParse(tls, pParse, __ccgo_ts+8848, 0) } } // C documentation // // /* // ** Generate code to reload the schema for database iDb. And, if iDb!=1, for // ** the temp database as well. // */ func _renameReloadSchema(tls *libc.TLS, pParse uintptr, iDb int32, p5 Tu16) { var v uintptr _ = v v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe if v != 0 { _sqlite3ChangeCookie(tls, pParse, iDb) _sqlite3VdbeAddParseSchemaOp(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, iDb, uintptr(0), p5) if iDb != int32(1) { _sqlite3VdbeAddParseSchemaOp(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(1), uintptr(0), p5) } } } // C documentation // // /* // ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" // ** command. // */ func _sqlite3AlterRenameTable(tls *libc.TLS, pParse uintptr, pSrc uintptr, pName uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var db, pTab, pVTab, v, zDb, zName, zTabName, v2 uintptr var i, iDb, nTabName, v1 int32 _, _, _, _, _, _, _, _, _, _, _, _ = db, i, iDb, nTabName, pTab, pVTab, v, zDb, zName, zTabName, v1, v2 /* Table being renamed */ zName = uintptr(0) /* NULL-terminated version of pName */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb pVTab = uintptr(0) /* Non-zero if this is a v-tab with an xRename() */ if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto exit_rename_table } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pTab = _sqlite3LocateTableItem(tls, pParse, uint32(0), pSrc+8) if !(pTab != 0) { goto exit_rename_table } iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTable)(unsafe.Pointer(pTab)).FpSchema) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName /* Get a NULL terminated version of the new table name. */ zName = _sqlite3NameFromToken(tls, db, pName) if !(zName != 0) { goto exit_rename_table } /* Check that a table or index named 'zName' does not already exist ** in database iDb. If so, this is an error. */ if _sqlite3FindTable(tls, db, zName, zDb) != 0 || _sqlite3FindIndex(tls, db, zName, zDb) != 0 || _sqlite3IsShadowTableOf(tls, db, pTab, zName) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8999, libc.VaList(bp+8, zName)) goto exit_rename_table } /* Make sure it is not a system table being altered, or a reserved name ** that the table is being renamed to. */ if SQLITE_OK != _isAlterableTable(tls, pParse, pTab) { goto exit_rename_table } if SQLITE_OK != _sqlite3CheckObjectName(tls, pParse, zName, __ccgo_ts+9058, zName) { goto exit_rename_table } if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9064, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) goto exit_rename_table } /* Invoke the authorization callback. */ if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_ALTER_TABLE), zDb, (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0)) != 0 { goto exit_rename_table } if _sqlite3ViewGetColumnNames(tls, pParse, pTab) != 0 { goto exit_rename_table } if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { pVTab = _sqlite3GetVTable(tls, db, pTab) if (*Tsqlite3_module)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer((*TVTable)(unsafe.Pointer(pVTab)).FpVtab)).FpModule)).FxRename == uintptr(0) { pVTab = uintptr(0) } } /* Begin a transaction for database iDb. Then modify the schema cookie ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(), ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the ** nested SQL may raise an exception. */ v = _sqlite3GetVdbe(tls, pParse) if v == uintptr(0) { goto exit_rename_table } _sqlite3MayAbort(tls, pParse) /* figure out how many UTF-8 characters are in zName */ zTabName = (*TTable)(unsafe.Pointer(pTab)).FzName nTabName = _sqlite3Utf8CharLen(tls, zTabName, -int32(1)) /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in ** the schema to use the new table name. */ _sqlite3NestedParse(tls, pParse, __ccgo_ts+9091, libc.VaList(bp+8, zDb, zDb, zTabName, zName, libc.BoolInt32(iDb == int32(1)), zTabName)) /* Update the tbl_name and name columns of the sqlite_schema table ** as required. */ _sqlite3NestedParse(tls, pParse, __ccgo_ts+9275, libc.VaList(bp+8, zDb, zName, zName, zName, nTabName, zTabName)) /* If the sqlite_sequence table exists in this database, then update ** it with the new table name. */ if _sqlite3FindTable(tls, db, __ccgo_ts+9580, zDb) != 0 { _sqlite3NestedParse(tls, pParse, __ccgo_ts+9596, libc.VaList(bp+8, zDb, zName, (*TTable)(unsafe.Pointer(pTab)).FzName)) } /* If the table being renamed is not itself part of the temp database, ** edit view and trigger definitions within the temp database ** as required. */ if iDb != int32(1) { _sqlite3NestedParse(tls, pParse, __ccgo_ts+9654, libc.VaList(bp+8, zDb, zTabName, zName, zTabName, zDb, zName)) } /* If this is a virtual table, invoke the xRename() function if ** one is defined. The xRename() callback will modify the names ** of any resources used by the v-table implementation (including other ** SQLite tables) that are identified by the name of the virtual table. */ if pVTab != 0 { v2 = pParse + 44 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) i = v1 _sqlite3VdbeLoadString(tls, v, i, zName) _sqlite3VdbeAddOp4(tls, v, int32(OP_VRename), i, 0, 0, pVTab, -int32(11)) } _renameReloadSchema(tls, pParse, iDb, uint16(INITFLAG_AlterRename)) _renameTestSchema(tls, pParse, zDb, libc.BoolInt32(iDb == int32(1)), __ccgo_ts+9919, 0) goto exit_rename_table exit_rename_table: ; _sqlite3SrcListDelete(tls, db, pSrc) _sqlite3DbFree(tls, db, zName) } // C documentation // // /* // ** Write code that will raise an error if the table described by // ** zDb and zTab is not empty. // */ func _sqlite3ErrorIfNotEmpty(tls *libc.TLS, pParse uintptr, zDb uintptr, zTab uintptr, zErr uintptr) { bp := tls.Alloc(32) defer tls.Free(32) _sqlite3NestedParse(tls, pParse, __ccgo_ts+9932, libc.VaList(bp+8, zErr, zDb, zTab)) } // C documentation // // /* // ** This function is called after an "ALTER TABLE ... ADD" statement // ** has been parsed. Argument pColDef contains the text of the new // ** column definition. // ** // ** The Table structure pParse->pNewTable was extended to include // ** the new column during parsing. // */ func _sqlite3AlterFinishAddColumn(tls *libc.TLS, pParse uintptr, pColDef uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var db, pCol, pDflt, pNew, pTab, v, zCol, zDb, zEnd, zTab, v1 uintptr var iDb, r1, rc int32 var _ /* pVal at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, iDb, pCol, pDflt, pNew, pTab, r1, rc, v, zCol, zDb, zEnd, zTab, v1 /* Temporary registers */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return } _ = libc.Int32FromInt32(0) pNew = (*TParse)(unsafe.Pointer(pParse)).FpNewTable _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pNew)).FpSchema) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName zTab = (*TTable)(unsafe.Pointer(pNew)).FzName + 16 /* Skip the "sqlite_altertab_" prefix on the name */ pCol = (*TTable)(unsafe.Pointer(pNew)).FaCol + uintptr(int32((*TTable)(unsafe.Pointer(pNew)).FnCol)-int32(1))*12 pDflt = _sqlite3ColumnExpr(tls, pNew, pCol) pTab = _sqlite3FindTable(tls, db, zTab, zDb) _ = libc.Int32FromInt32(0) /* Invoke the authorization callback. */ if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_ALTER_TABLE), zDb, (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0)) != 0 { return } /* Check that the new column is not specified as PRIMARY KEY or UNIQUE. ** If there is a NOT NULL constraint, then the default value for the ** column must not be NULL. */ if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9970, 0) return } if (*TTable)(unsafe.Pointer(pNew)).FpIndex != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+10002, 0) return } if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_GENERATED) == 0 { /* If the default value for the new column was specified with a ** literal NULL, then set pDflt to 0. This simplifies checking ** for an SQL NULL default below. */ _ = libc.Int32FromInt32(0) if pDflt != 0 && int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pDflt)).FpLeft)).Fop) == int32(TK_NULL) { pDflt = uintptr(0) } _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) != 0 && (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pNew + 44))).FpFKey != 0 && pDflt != 0 { _sqlite3ErrorIfNotEmpty(tls, pParse, zDb, zTab, __ccgo_ts+10029) } if int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf>>0)) != 0 && !(pDflt != 0) { _sqlite3ErrorIfNotEmpty(tls, pParse, zDb, zTab, __ccgo_ts+10088) } /* Ensure the default expression is something that sqlite3ValueFromExpr() ** can handle (i.e. not CURRENT_TIME etc.) */ if pDflt != 0 { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) rc = _sqlite3ValueFromExpr(tls, db, pDflt, uint8(SQLITE_UTF8), uint8(SQLITE_AFF_BLOB), bp) _ = libc.Int32FromInt32(0) if rc != SQLITE_OK { _ = libc.Int32FromInt32(0) return } if !(*(*uintptr)(unsafe.Pointer(bp)) != 0) { _sqlite3ErrorIfNotEmpty(tls, pParse, zDb, zTab, __ccgo_ts+10141) } _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer(bp))) } } else { if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_STORED) != 0 { _sqlite3ErrorIfNotEmpty(tls, pParse, zDb, zTab, __ccgo_ts+10187) } } /* Modify the CREATE TABLE statement. */ zCol = _sqlite3DbStrNDup(tls, db, (*TToken)(unsafe.Pointer(pColDef)).Fz, uint64((*TToken)(unsafe.Pointer(pColDef)).Fn)) if zCol != 0 { zEnd = zCol + uintptr((*TToken)(unsafe.Pointer(pColDef)).Fn-uint32(1)) for zEnd > zCol && (int32(*(*uint8)(unsafe.Pointer(zEnd))) == int32(';') || int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zEnd))])&int32(0x01) != 0) { v1 = zEnd zEnd-- *(*uint8)(unsafe.Pointer(v1)) = uint8('\000') } /* substr() operations on characters, but addColOffset is in bytes. So we ** have to use printf() to translate between these units: */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3NestedParse(tls, pParse, __ccgo_ts+10214, libc.VaList(bp+16, zDb, (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pNew + 44))).FaddColOffset, zCol, (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pNew + 44))).FaddColOffset, zTab)) _sqlite3DbFree(tls, db, zCol) } v = _sqlite3GetVdbe(tls, pParse) if v != 0 { /* Make sure the schema version is at least 3. But do not upgrade ** from less than 3 to 4, as that will corrupt any preexisting DESC ** index. */ r1 = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp3(tls, v, int32(OP_ReadCookie), iDb, r1, int32(BTREE_FILE_FORMAT)) _sqlite3VdbeUsesBtree(tls, v, iDb) _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), r1, -int32(2)) _sqlite3VdbeAddOp2(tls, v, int32(OP_IfPos), r1, _sqlite3VdbeCurrentAddr(tls, v)+int32(2)) _sqlite3VdbeAddOp3(tls, v, int32(OP_SetCookie), iDb, int32(BTREE_FILE_FORMAT), int32(3)) _sqlite3ReleaseTempReg(tls, pParse, r1) /* Reload the table definition */ _renameReloadSchema(tls, pParse, iDb, uint16(INITFLAG_AlterAdd)) /* Verify that constraints are still satisfied */ if (*TTable)(unsafe.Pointer(pNew)).FpCheck != uintptr(0) || int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf>>0)) != 0 && int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_GENERATED) != 0 || (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Strict) != uint32(0) { _sqlite3NestedParse(tls, pParse, __ccgo_ts+10360, libc.VaList(bp+16, zTab, zDb)) } } } // C documentation // // /* // ** This function is called by the parser after the table-name in // ** an "ALTER TABLE ADD" statement is parsed. Argument // ** pSrc is the full-name of the table being altered. // ** // ** This routine makes a (partial) copy of the Table structure // ** for the table being altered and sets Parse.pNewTable to point // ** to it. Routines called by the parser as the column definition // ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to // ** the copy. The copy of the Table structure is deleted by tokenize.c // ** after parsing is finished. // ** // ** Routine sqlite3AlterFinishAddColumn() will be called to complete // ** coding the "ALTER TABLE ... ADD" statement. // */ func _sqlite3AlterBeginAddColumn(tls *libc.TLS, pParse uintptr, pSrc uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, pCol, pNew, pTab uintptr var i, iDb, nAlloc int32 _, _, _, _, _, _, _ = db, i, iDb, nAlloc, pCol, pNew, pTab db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Look up the table being altered. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto exit_begin_add_column } pTab = _sqlite3LocateTableItem(tls, pParse, uint32(0), pSrc+8) if !(pTab != 0) { goto exit_begin_add_column } if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+10713, 0) goto exit_begin_add_column } /* Make sure this is not an attempt to ALTER a view. */ if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+10747, 0) goto exit_begin_add_column } if SQLITE_OK != _isAlterableTable(tls, pParse, pTab) { goto exit_begin_add_column } _sqlite3MayAbort(tls, pParse) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) /* Put a copy of the Table struct in Parse.pNewTable for the ** sqlite3AddColumn() function and friends to modify. But modify ** the name by adding an "sqlite_altertab_" prefix. By adding this ** prefix, we insure that the name will not collide with an existing ** table because user table are not allowed to have the "sqlite_" ** prefix on their name. */ pNew = _sqlite3DbMallocZero(tls, db, uint64(64)) if !(pNew != 0) { goto exit_begin_add_column } (*TParse)(unsafe.Pointer(pParse)).FpNewTable = pNew (*TTable)(unsafe.Pointer(pNew)).FnTabRef = uint32(1) (*TTable)(unsafe.Pointer(pNew)).FnCol = (*TTable)(unsafe.Pointer(pTab)).FnCol _ = libc.Int32FromInt32(0) nAlloc = (int32((*TTable)(unsafe.Pointer(pNew)).FnCol)-int32(1))/int32(8)*int32(8) + int32(8) _ = libc.Int32FromInt32(0) (*TTable)(unsafe.Pointer(pNew)).FaCol = _sqlite3DbMallocZero(tls, db, uint64(uint32(12)*uint32(nAlloc))) (*TTable)(unsafe.Pointer(pNew)).FzName = _sqlite3MPrintf(tls, db, __ccgo_ts+10777, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) if !((*TTable)(unsafe.Pointer(pNew)).FaCol != 0) || !((*TTable)(unsafe.Pointer(pNew)).FzName != 0) { _ = libc.Int32FromInt32(0) goto exit_begin_add_column } libc.Xmemcpy(tls, (*TTable)(unsafe.Pointer(pNew)).FaCol, (*TTable)(unsafe.Pointer(pTab)).FaCol, uint32(12)*uint32((*TTable)(unsafe.Pointer(pNew)).FnCol)) i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pNew)).FnCol)) { break } pCol = (*TTable)(unsafe.Pointer(pNew)).FaCol + uintptr(i)*12 (*TColumn)(unsafe.Pointer(pCol)).FzCnName = _sqlite3DbStrDup(tls, db, (*TColumn)(unsafe.Pointer(pCol)).FzCnName) (*TColumn)(unsafe.Pointer(pCol)).FhName = _sqlite3StrIHash(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName) goto _1 _1: ; i++ } _ = libc.Int32FromInt32(0) (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pNew + 44))).FpDfltList = _sqlite3ExprListDup(tls, db, (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpDfltList, 0) (*TTable)(unsafe.Pointer(pNew)).FpSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pNew + 44))).FaddColOffset = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FaddColOffset _ = libc.Int32FromInt32(0) goto exit_begin_add_column exit_begin_add_column: ; _sqlite3SrcListDelete(tls, db, pSrc) return } // C documentation // // /* // ** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN // ** command. This function checks if the table is a view or virtual // ** table (columns of views or virtual tables may not be renamed). If so, // ** it loads an error message into pParse and returns non-zero. // ** // ** Or, if pTab is not a view or virtual table, zero is returned. // */ func _isRealTable(tls *libc.TLS, pParse uintptr, pTab uintptr, bDrop int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var zType, v1 uintptr _, _ = zType, v1 zType = uintptr(0) if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { zType = __ccgo_ts + 10796 } if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { zType = __ccgo_ts + 10801 } if zType != 0 { if bDrop != 0 { v1 = __ccgo_ts + 10815 } else { v1 = __ccgo_ts + 10832 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+10850, libc.VaList(bp+8, v1, zType, (*TTable)(unsafe.Pointer(pTab)).FzName)) return int32(1) } return 0 } // C documentation // // /* // ** Handles the following parser reduction: // ** // ** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew // */ func _sqlite3AlterRenameColumn(tls *libc.TLS, pParse uintptr, pSrc uintptr, pOld uintptr, pNew uintptr) { bp := tls.Alloc(80) defer tls.Free(80) var bQuote, iCol, iSchema int32 var db, pTab, zDb, zNew, zOld uintptr _, _, _, _, _, _, _, _ = bQuote, db, iCol, iSchema, pTab, zDb, zNew, zOld db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Index of column being renamed */ zOld = uintptr(0) /* Old column name */ zNew = uintptr(0) /* True to quote the new name */ /* Locate the table to be altered */ pTab = _sqlite3LocateTableItem(tls, pParse, uint32(0), pSrc+8) if !(pTab != 0) { goto exit_rename_column } /* Cannot alter a system table */ if SQLITE_OK != _isAlterableTable(tls, pParse, pTab) { goto exit_rename_column } if SQLITE_OK != _isRealTable(tls, pParse, pTab, 0) { goto exit_rename_column } /* Which schema holds the table to be altered */ iSchema = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) _ = libc.Int32FromInt32(0) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iSchema)*16))).FzDbSName /* Invoke the authorization callback. */ if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_ALTER_TABLE), zDb, (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0)) != 0 { goto exit_rename_column } /* Make sure the old name really is a column name in the table to be ** altered. Set iCol to be the index of the column being renamed */ zOld = _sqlite3NameFromToken(tls, db, pOld) if !(zOld != 0) { goto exit_rename_column } iCol = 0 for { if !(iCol < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if 0 == _sqlite3StrICmp(tls, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName, zOld) { break } goto _1 _1: ; iCol++ } if iCol == int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+10868, libc.VaList(bp+8, pOld)) goto exit_rename_column } /* Ensure the schema contains no double-quoted strings */ _renameTestSchema(tls, pParse, zDb, libc.BoolInt32(iSchema == int32(1)), __ccgo_ts+1648, 0) _renameFixQuotes(tls, pParse, zDb, libc.BoolInt32(iSchema == int32(1))) /* Do the rename operation using a recursive UPDATE statement that ** uses the sqlite_rename_column() SQL function to compute the new ** CREATE statement text for the sqlite_schema table. */ _sqlite3MayAbort(tls, pParse) zNew = _sqlite3NameFromToken(tls, db, pNew) if !(zNew != 0) { goto exit_rename_column } _ = libc.Int32FromInt32(0) bQuote = int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer((*TToken)(unsafe.Pointer(pNew)).Fz))]) & int32(0x80) _sqlite3NestedParse(tls, pParse, __ccgo_ts+10889, libc.VaList(bp+8, zDb, zDb, (*TTable)(unsafe.Pointer(pTab)).FzName, iCol, zNew, bQuote, libc.BoolInt32(iSchema == int32(1)), (*TTable)(unsafe.Pointer(pTab)).FzName)) _sqlite3NestedParse(tls, pParse, __ccgo_ts+11071, libc.VaList(bp+8, zDb, (*TTable)(unsafe.Pointer(pTab)).FzName, iCol, zNew, bQuote)) /* Drop and reload the database schema. */ _renameReloadSchema(tls, pParse, iSchema, uint16(INITFLAG_AlterRename)) _renameTestSchema(tls, pParse, zDb, libc.BoolInt32(iSchema == int32(1)), __ccgo_ts+9919, int32(1)) goto exit_rename_column exit_rename_column: ; _sqlite3SrcListDelete(tls, db, pSrc) _sqlite3DbFree(tls, db, zOld) _sqlite3DbFree(tls, db, zNew) return } /* ** Each RenameToken object maps an element of the parse tree into ** the token that generated that element. The parse tree element ** might be one of: ** ** * A pointer to an Expr that represents an ID ** * The name of a table column in Column.zName ** ** A list of RenameToken objects can be constructed during parsing. ** Each new object is created by sqlite3RenameTokenMap(). ** As the parse tree is transformed, the sqlite3RenameTokenRemap() ** routine is used to keep the mapping current. ** ** After the parse finishes, renameTokenFind() routine can be used ** to look up the actual token value that created some element in ** the parse tree. */ type TRenameToken1 = struct { Fp uintptr Ft TToken FpNext uintptr } type RenameToken1 = TRenameToken1 // C documentation // // /* // ** The context of an ALTER TABLE RENAME COLUMN operation that gets passed // ** down into the Walker. // */ type TRenameCtx = struct { FpList uintptr FnList int32 FiCol int32 FpTab uintptr FzOld uintptr } type RenameCtx = TRenameCtx type TRenameCtx1 = struct { FpList uintptr FnList int32 FiCol int32 FpTab uintptr FzOld uintptr } type RenameCtx1 = TRenameCtx1 // C documentation // // /* // ** Remember that the parser tree element pPtr was created using // ** the token pToken. // ** // ** In other words, construct a new RenameToken object and add it // ** to the list of RenameToken objects currently being built up // ** in pParse->pRename. // ** // ** The pPtr argument is returned so that this routine can be used // ** with tail recursion in tokenExpr() routine, for a small performance // ** improvement. // */ func _sqlite3RenameTokenMap(tls *libc.TLS, pParse uintptr, pPtr uintptr, pToken uintptr) (r uintptr) { var pNew uintptr _ = pNew _ = libc.Int32FromInt32(0) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != int32(PARSE_MODE_UNMAP) { pNew = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(16)) if pNew != 0 { (*TRenameToken)(unsafe.Pointer(pNew)).Fp = pPtr (*TRenameToken)(unsafe.Pointer(pNew)).Ft = *(*TToken)(unsafe.Pointer(pToken)) (*TRenameToken)(unsafe.Pointer(pNew)).FpNext = (*TParse)(unsafe.Pointer(pParse)).FpRename (*TParse)(unsafe.Pointer(pParse)).FpRename = pNew } } return pPtr } // C documentation // // /* // ** It is assumed that there is already a RenameToken object associated // ** with parse tree element pFrom. This function remaps the associated token // ** to parse tree element pTo. // */ func _sqlite3RenameTokenRemap(tls *libc.TLS, pParse uintptr, pTo uintptr, pFrom uintptr) { var p uintptr _ = p p = (*TParse)(unsafe.Pointer(pParse)).FpRename for { if !(p != 0) { break } if (*TRenameToken)(unsafe.Pointer(p)).Fp == pFrom { (*TRenameToken)(unsafe.Pointer(p)).Fp = pTo break } goto _1 _1: ; p = (*TRenameToken)(unsafe.Pointer(p)).FpNext } } // C documentation // // /* // ** Walker callback used by sqlite3RenameExprUnmap(). // */ func _renameUnmapExprCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var pParse uintptr _ = pParse pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse _sqlite3RenameTokenRemap(tls, pParse, uintptr(0), pExpr) if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)|libc.Int32FromInt32(EP_Subrtn)) == uint32(0) { _sqlite3RenameTokenRemap(tls, pParse, uintptr(0), pExpr+44) } return WRC_Continue } // C documentation // // /* // ** Iterate through the Select objects that are part of WITH clauses attached // ** to select statement pSelect. // */ func _renameWalkWith(tls *libc.TLS, pWalker uintptr, pSelect uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var i int32 var p, pCopy, pParse, pWith uintptr var _ /* sNC at bp+0 */ TNameContext _, _, _, _, _ = i, p, pCopy, pParse, pWith pWith = (*TSelect)(unsafe.Pointer(pSelect)).FpWith if pWith != 0 { pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse pCopy = uintptr(0) _ = libc.Int32FromInt32(0) if (*TSelect)(unsafe.Pointer((*(*TCte)(unsafe.Pointer(pWith + 12))).FpSelect)).FselFlags&uint32(SF_Expanded) == uint32(0) { /* Push a copy of the With object onto the with-stack. We use a copy ** here as the original will be expanded and resolved (flags SF_Expanded ** and SF_Resolved) below. And the parser code that uses the with-stack ** fails if the Select objects on it have already been expanded and ** resolved. */ pCopy = _sqlite3WithDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pWith) pCopy = _sqlite3WithPush(tls, pParse, pCopy, uint8(1)) } i = 0 for { if !(i < (*TWith)(unsafe.Pointer(pWith)).FnCte) { break } p = (*(*TCte)(unsafe.Pointer(pWith + 12 + uintptr(i)*24))).FpSelect libc.Xmemset(tls, bp, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp))).FpParse = pParse if pCopy != 0 { _sqlite3SelectPrep(tls, (*(*TNameContext)(unsafe.Pointer(bp))).FpParse, p, bp) } if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer((*(*TNameContext)(unsafe.Pointer(bp))).FpParse)).Fdb)).FmallocFailed != 0 { return } _sqlite3WalkSelect(tls, pWalker, p) _sqlite3RenameExprlistUnmap(tls, pParse, (*(*TCte)(unsafe.Pointer(pWith + 12 + uintptr(i)*24))).FpCols) goto _1 _1: ; i++ } if pCopy != 0 && (*TParse)(unsafe.Pointer(pParse)).FpWith == pCopy { (*TParse)(unsafe.Pointer(pParse)).FpWith = (*TWith)(unsafe.Pointer(pCopy)).FpOuter } } } // C documentation // // /* // ** Unmap all tokens in the IdList object passed as the second argument. // */ func _unmapColumnIdlistNames(tls *libc.TLS, pParse uintptr, pIdList uintptr) { var ii int32 _ = ii _ = libc.Int32FromInt32(0) ii = 0 for { if !(ii < (*TIdList)(unsafe.Pointer(pIdList)).FnId) { break } _sqlite3RenameTokenRemap(tls, pParse, uintptr(0), (*(*TIdList_item)(unsafe.Pointer(pIdList + 8 + uintptr(ii)*8))).FzName) goto _1 _1: ; ii++ } } // C documentation // // /* // ** Walker callback used by sqlite3RenameExprUnmap(). // */ func _renameUnmapSelectCb(tls *libc.TLS, pWalker uintptr, p uintptr) (r int32) { var i int32 var pList, pParse, pSrc uintptr _, _, _, _ = i, pList, pParse, pSrc pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return int32(WRC_Abort) } if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(libc.Int32FromInt32(SF_View)|libc.Int32FromInt32(SF_CopyCte)) != 0 { return int32(WRC_Prune) } if (*TSelect)(unsafe.Pointer(p)).FpEList != 0 { pList = (*TSelect)(unsafe.Pointer(p)).FpEList i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } if (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FzEName != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pList + 8 + uintptr(i)*20 + 8 + 4))&0x3>>0)) == ENAME_NAME { _sqlite3RenameTokenRemap(tls, pParse, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FzEName) } goto _1 _1: ; i++ } } if (*TSelect)(unsafe.Pointer(p)).FpSrc != 0 { /* Every Select as a SrcList, even if it is empty */ pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) { break } _sqlite3RenameTokenRemap(tls, pParse, uintptr(0), (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72))).FzName) if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72 + 36 + 4))&0x400>>10)) == 0 { _sqlite3WalkExpr(tls, pWalker, *(*uintptr)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72 + 48))) } else { _unmapColumnIdlistNames(tls, pParse, *(*uintptr)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72 + 48))) } goto _2 _2: ; i++ } } _renameWalkWith(tls, pWalker, p) return WRC_Continue } // C documentation // // /* // ** Remove all nodes that are part of expression pExpr from the rename list. // */ func _sqlite3RenameExprUnmap(tls *libc.TLS, pParse uintptr, pExpr uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var eMode Tu8 var _ /* sWalker at bp+0 */ TWalker _ = eMode eMode = (*TParse)(unsafe.Pointer(pParse)).FeParseMode libc.Xmemset(tls, bp, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp))).FpParse = pParse (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_renameUnmapExprCb) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_renameUnmapSelectCb) (*TParse)(unsafe.Pointer(pParse)).FeParseMode = uint8(PARSE_MODE_UNMAP) _sqlite3WalkExpr(tls, bp, pExpr) (*TParse)(unsafe.Pointer(pParse)).FeParseMode = eMode } // C documentation // // /* // ** Remove all nodes that are part of expression-list pEList from the // ** rename list. // */ func _sqlite3RenameExprlistUnmap(tls *libc.TLS, pParse uintptr, pEList uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var i int32 var _ /* sWalker at bp+0 */ TWalker _ = i if pEList != 0 { libc.Xmemset(tls, bp, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp))).FpParse = pParse (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_renameUnmapExprCb) _sqlite3WalkExprList(tls, bp, pEList) i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } if int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(i)*20 + 8 + 4))&0x3>>0)) == ENAME_NAME { _sqlite3RenameTokenRemap(tls, pParse, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FzEName) } goto _1 _1: ; i++ } } } // C documentation // // /* // ** Free the list of RenameToken objects given in the second argument // */ func _renameTokenFree(tls *libc.TLS, db uintptr, pToken uintptr) { var p, pNext uintptr _, _ = p, pNext p = pToken for { if !(p != 0) { break } pNext = (*TRenameToken)(unsafe.Pointer(p)).FpNext _sqlite3DbFree(tls, db, p) goto _1 _1: ; p = pNext } } // C documentation // // /* // ** Search the Parse object passed as the first argument for a RenameToken // ** object associated with parse tree element pPtr. If found, return a pointer // ** to it. Otherwise, return NULL. // ** // ** If the second argument passed to this function is not NULL and a matching // ** RenameToken object is found, remove it from the Parse object and add it to // ** the list maintained by the RenameCtx object. // */ func _renameTokenFind(tls *libc.TLS, pParse uintptr, pCtx uintptr, pPtr uintptr) (r uintptr) { var pToken, pp uintptr _, _ = pToken, pp if pPtr == uintptr(0) { return uintptr(0) } pp = pParse + 280 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != 0) { break } if (*TRenameToken)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).Fp == pPtr { pToken = *(*uintptr)(unsafe.Pointer(pp)) if pCtx != 0 { *(*uintptr)(unsafe.Pointer(pp)) = (*TRenameToken)(unsafe.Pointer(pToken)).FpNext (*TRenameToken)(unsafe.Pointer(pToken)).FpNext = (*TRenameCtx1)(unsafe.Pointer(pCtx)).FpList (*TRenameCtx1)(unsafe.Pointer(pCtx)).FpList = pToken (*TRenameCtx1)(unsafe.Pointer(pCtx)).FnList++ } return pToken } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 12 } return uintptr(0) } // C documentation // // /* // ** This is a Walker select callback. It does nothing. It is only required // ** because without a dummy callback, sqlite3WalkExpr() and similar do not // ** descend into sub-select statements. // */ func _renameColumnSelectCb(tls *libc.TLS, pWalker uintptr, p uintptr) (r int32) { if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(libc.Int32FromInt32(SF_View)|libc.Int32FromInt32(SF_CopyCte)) != 0 { return int32(WRC_Prune) } _renameWalkWith(tls, pWalker, p) return WRC_Continue } // C documentation // // /* // ** This is a Walker expression callback. // ** // ** For every TK_COLUMN node in the expression tree, search to see // ** if the column being references is the column being renamed by an // ** ALTER TABLE statement. If it is, then attach its associated // ** RenameToken object to the list of RenameToken objects being // ** constructed in RenameCtx object at pWalker->u.pRename. // */ func _renameColumnExprCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var p uintptr _ = p p = *(*uintptr)(unsafe.Pointer(pWalker + 24)) if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_TRIGGER) && int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) == (*TRenameCtx)(unsafe.Pointer(p)).FiCol && (*TParse)(unsafe.Pointer((*TWalker)(unsafe.Pointer(pWalker)).FpParse)).FpTriggerTab == (*TRenameCtx)(unsafe.Pointer(p)).FpTab { _renameTokenFind(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, p, pExpr) } else { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) == (*TRenameCtx)(unsafe.Pointer(p)).FiCol && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)|libc.Int32FromInt32(EP_Subrtn)) == uint32(0) && (*TRenameCtx)(unsafe.Pointer(p)).FpTab == (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab { _renameTokenFind(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, p, pExpr) } } return WRC_Continue } // C documentation // // /* // ** The RenameCtx contains a list of tokens that reference a column that // ** is being renamed by an ALTER TABLE statement. Return the "last" // ** RenameToken in the RenameCtx and remove that RenameToken from the // ** RenameContext. "Last" means the last RenameToken encountered when // ** the input SQL is parsed from left to right. Repeated calls to this routine // ** return all column name tokens in the order that they are encountered // ** in the SQL statement. // */ func _renameColumnTokenNext(tls *libc.TLS, pCtx uintptr) (r uintptr) { var pBest, pToken, pp uintptr _, _, _ = pBest, pToken, pp pBest = (*TRenameCtx)(unsafe.Pointer(pCtx)).FpList pToken = (*TRenameToken)(unsafe.Pointer(pBest)).FpNext for { if !(pToken != 0) { break } if (*TRenameToken)(unsafe.Pointer(pToken)).Ft.Fz > (*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz { pBest = pToken } goto _1 _1: ; pToken = (*TRenameToken)(unsafe.Pointer(pToken)).FpNext } pp = pCtx for { if !(*(*uintptr)(unsafe.Pointer(pp)) != pBest) { break } goto _2 _2: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 12 } *(*uintptr)(unsafe.Pointer(pp)) = (*TRenameToken)(unsafe.Pointer(pBest)).FpNext return pBest } // C documentation // // /* // ** An error occurred while parsing or otherwise processing a database // ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an // ** ALTER TABLE RENAME COLUMN program. The error message emitted by the // ** sub-routine is currently stored in pParse->zErrMsg. This function // ** adds context to the error message and then stores it in pCtx. // */ func _renameColumnParseError(tls *libc.TLS, pCtx uintptr, zWhen uintptr, pType uintptr, pObject uintptr, pParse uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var zErr, zN, zT, v1 uintptr _, _, _, _ = zErr, zN, zT, v1 zT = Xsqlite3_value_text(tls, pType) zN = Xsqlite3_value_text(tls, pObject) if *(*uint8)(unsafe.Pointer(zWhen)) != 0 { v1 = __ccgo_ts + 11202 } else { v1 = __ccgo_ts + 1648 } zErr = _sqlite3MPrintf(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, __ccgo_ts+11204, libc.VaList(bp+8, zT, zN, v1, zWhen, (*TParse)(unsafe.Pointer(pParse)).FzErrMsg)) Xsqlite3_result_error(tls, pCtx, zErr, -int32(1)) _sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, zErr) } // C documentation // // /* // ** For each name in the the expression-list pEList (i.e. each // ** pEList->a[i].zName) that matches the string in zOld, extract the // ** corresponding rename-token from Parse object pParse and add it // ** to the RenameCtx pCtx. // */ func _renameColumnElistNames(tls *libc.TLS, pParse uintptr, pCtx uintptr, pEList uintptr, zOld uintptr) { var i int32 var zName uintptr _, _ = i, zName if pEList != 0 { i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } zName = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FzEName if int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(i)*20 + 8 + 4))&0x3>>0)) == ENAME_NAME && zName != uintptr(0) && 0 == Xsqlite3_stricmp(tls, zName, zOld) { _renameTokenFind(tls, pParse, pCtx, zName) } goto _1 _1: ; i++ } } } // C documentation // // /* // ** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName) // ** that matches the string in zOld, extract the corresponding rename-token // ** from Parse object pParse and add it to the RenameCtx pCtx. // */ func _renameColumnIdlistNames(tls *libc.TLS, pParse uintptr, pCtx uintptr, pIdList uintptr, zOld uintptr) { var i int32 var zName uintptr _, _ = i, zName if pIdList != 0 { i = 0 for { if !(i < (*TIdList)(unsafe.Pointer(pIdList)).FnId) { break } zName = (*(*TIdList_item)(unsafe.Pointer(pIdList + 8 + uintptr(i)*8))).FzName if 0 == Xsqlite3_stricmp(tls, zName, zOld) { _renameTokenFind(tls, pParse, pCtx, zName) } goto _1 _1: ; i++ } } } // C documentation // // /* // ** Parse the SQL statement zSql using Parse object (*p). The Parse object // ** is initialized by this function before it is used. // */ func _renameParseSql(tls *libc.TLS, p uintptr, zDb uintptr, db uintptr, zSql uintptr, bTemp int32) (r int32) { var rc, v1 int32 _, _ = rc, v1 _sqlite3ParseObjectInit(tls, p, db) if zSql == uintptr(0) { return int32(SQLITE_NOMEM) } if Xsqlite3_strnicmp(tls, zSql, __ccgo_ts+11227, int32(7)) != 0 { return _sqlite3CorruptError(tls, int32(116682)) } if bTemp != 0 { v1 = int32(1) } else { v1 = _sqlite3FindDbName(tls, db, zDb) } (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(v1) (*TParse)(unsafe.Pointer(p)).FeParseMode = uint8(PARSE_MODE_RENAME) (*TParse)(unsafe.Pointer(p)).Fdb = db (*TParse)(unsafe.Pointer(p)).FnQueryLoop = int16(1) rc = _sqlite3RunParser(tls, p, zSql) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { rc = int32(SQLITE_NOMEM) } if rc == SQLITE_OK && ((*TParse)(unsafe.Pointer(p)).FpNewTable == uintptr(0) && (*TParse)(unsafe.Pointer(p)).FpNewIndex == uintptr(0) && (*TParse)(unsafe.Pointer(p)).FpNewTrigger == uintptr(0)) { rc = _sqlite3CorruptError(tls, int32(116693)) } (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(0) return rc } // C documentation // // /* // ** This function edits SQL statement zSql, replacing each token identified // ** by the linked list pRename with the text of zNew. If argument bQuote is // ** true, then zNew is always quoted first. If no error occurs, the result // ** is loaded into context object pCtx as the result. // ** // ** Or, if an error occurs (i.e. an OOM condition), an error is left in // ** pCtx and an SQLite error code returned. // */ func _renameEditSql(tls *libc.TLS, pCtx uintptr, pRename uintptr, zSql uintptr, zNew uintptr, bQuote int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var db, pBest, zBuf1, zBuf2, zOut, zQuot, zReplace, v1 uintptr var iOff, nOut, rc int32 var nNew, nQuot, nSql Ti64 var nReplace Tu32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, iOff, nNew, nOut, nQuot, nReplace, nSql, pBest, rc, zBuf1, zBuf2, zOut, zQuot, zReplace, v1 nNew = int64(_sqlite3Strlen30(tls, zNew)) nSql = int64(_sqlite3Strlen30(tls, zSql)) db = Xsqlite3_context_db_handle(tls, pCtx) rc = SQLITE_OK zQuot = uintptr(0) nQuot = 0 zBuf1 = uintptr(0) zBuf2 = uintptr(0) if zNew != 0 { /* Set zQuot to point to a buffer containing a quoted copy of the ** identifier zNew. If the corresponding identifier in the original ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to ** point to zQuot so that all substitutions are made using the ** quoted version of the new column name. */ zQuot = _sqlite3MPrintf(tls, db, __ccgo_ts+11235, libc.VaList(bp+8, zNew)) if zQuot == uintptr(0) { return int32(SQLITE_NOMEM) } else { nQuot = int64(_sqlite3Strlen30(tls, zQuot) - int32(1)) } _ = libc.Int32FromInt32(0) zOut = _sqlite3DbMallocZero(tls, db, uint64(nSql+int64((*TRenameCtx)(unsafe.Pointer(pRename)).FnList)*nQuot+int64(1))) } else { zOut = _sqlite3DbMallocZero(tls, db, uint64((nSql*int64(2)+int64(1))*int64(3))) if zOut != 0 { zBuf1 = zOut + uintptr(nSql*int64(2)+int64(1)) zBuf2 = zOut + uintptr(nSql*int64(4)+int64(2)) } } /* At this point pRename->pList contains a list of RenameToken objects ** corresponding to all tokens in the input SQL that must be replaced ** with the new column name, or with single-quoted versions of themselves. ** All that remains is to construct and return the edited SQL string. */ if zOut != 0 { nOut = int32(nSql) libc.Xmemcpy(tls, zOut, zSql, uint32(nSql)) for (*TRenameCtx)(unsafe.Pointer(pRename)).FpList != 0 { pBest = _renameColumnTokenNext(tls, pRename) if zNew != 0 { if bQuote == 0 && _sqlite3IsIdChar(tls, *(*uint8)(unsafe.Pointer((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz))) != 0 { nReplace = uint32(nNew) zReplace = zNew } else { nReplace = uint32(nQuot) zReplace = zQuot if int32(*(*uint8)(unsafe.Pointer((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz + uintptr((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn)))) == int32('"') { nReplace++ } } } else { /* Dequote the double-quoted token. Then requote it again, this time ** using single quotes. If the character immediately following the ** original token within the input SQL was a single quote ('), then ** add another space after the new, single-quoted version of the ** token. This is so that (SELECT "string"'alias') maps to ** (SELECT 'string' 'alias'), and not (SELECT 'string''alias'). */ libc.Xmemcpy(tls, zBuf1, (*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz, (*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn) *(*uint8)(unsafe.Pointer(zBuf1 + uintptr((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn))) = uint8(0) _sqlite3Dequote(tls, zBuf1) if int32(*(*uint8)(unsafe.Pointer((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz + uintptr((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn)))) == int32('\'') { v1 = __ccgo_ts + 11202 } else { v1 = __ccgo_ts + 1648 } Xsqlite3_snprintf(tls, int32(nSql*int64(2)), zBuf2, __ccgo_ts+11241, libc.VaList(bp+8, zBuf1, v1)) zReplace = zBuf2 nReplace = uint32(_sqlite3Strlen30(tls, zReplace)) } iOff = int32((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz) - int32(zSql) if (*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn != nReplace { libc.Xmemmove(tls, zOut+uintptr(uint32(iOff)+nReplace), zOut+uintptr(uint32(iOff)+(*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn), uint32(nOut)-(uint32(iOff)+(*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn)) nOut = int32(uint32(nOut) + (nReplace - (*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn)) *(*uint8)(unsafe.Pointer(zOut + uintptr(nOut))) = uint8('\000') } libc.Xmemcpy(tls, zOut+uintptr(iOff), zReplace, nReplace) _sqlite3DbFree(tls, db, pBest) } Xsqlite3_result_text(tls, pCtx, zOut, -int32(1), uintptr(-libc.Int32FromInt32(1))) _sqlite3DbFree(tls, db, zOut) } else { rc = int32(SQLITE_NOMEM) } Xsqlite3_free(tls, zQuot) return rc } // C documentation // // /* // ** Set all pEList->a[].fg.eEName fields in the expression-list to val. // */ func _renameSetENames(tls *libc.TLS, pEList uintptr, val int32) { var i int32 _ = i if pEList != 0 { i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } _ = libc.Int32FromInt32(0) libc.SetBitFieldPtr16Uint32(pEList+8+uintptr(i)*20+8+4, uint32(val), 0, 0x3) goto _1 _1: ; i++ } } } // C documentation // // /* // ** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming // ** it was read from the schema of database zDb. Return SQLITE_OK if // ** successful. Otherwise, return an SQLite error code and leave an error // ** message in the Parse object. // */ func _renameResolveTrigger(tls *libc.TLS, pParse uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var db, p, pNew, pSel, pSrc, pStep, pUpsert, pUpsertSet uintptr var i, rc, v2 int32 var _ /* sNC at bp+0 */ TNameContext _, _, _, _, _, _, _, _, _, _, _ = db, i, p, pNew, pSel, pSrc, pStep, pUpsert, pUpsertSet, rc, v2 db = (*TParse)(unsafe.Pointer(pParse)).Fdb pNew = (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger rc = SQLITE_OK libc.Xmemset(tls, bp, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp))).FpParse = pParse _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab = _sqlite3FindTable(tls, db, (*TTrigger)(unsafe.Pointer(pNew)).Ftable, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(_sqlite3SchemaToIndex(tls, db, (*TTrigger)(unsafe.Pointer(pNew)).FpTabSchema))*16))).FzDbSName) (*TParse)(unsafe.Pointer(pParse)).FeTriggerOp = (*TTrigger)(unsafe.Pointer(pNew)).Fop /* ALWAYS() because if the table of the trigger does not exist, the ** error would have been hit before this point */ if (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab != 0 { rc = _sqlite3ViewGetColumnNames(tls, pParse, (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab) } /* Resolve symbols in WHEN clause */ if rc == SQLITE_OK && (*TTrigger)(unsafe.Pointer(pNew)).FpWhen != 0 { rc = _sqlite3ResolveExprNames(tls, bp, (*TTrigger)(unsafe.Pointer(pNew)).FpWhen) } pStep = (*TTrigger)(unsafe.Pointer(pNew)).Fstep_list for { if !(rc == SQLITE_OK && pStep != 0) { break } if (*TTriggerStep)(unsafe.Pointer(pStep)).FpSelect != 0 { _sqlite3SelectPrep(tls, pParse, (*TTriggerStep)(unsafe.Pointer(pStep)).FpSelect, bp) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { rc = (*TParse)(unsafe.Pointer(pParse)).Frc } } if rc == SQLITE_OK && (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget != 0 { pSrc = _sqlite3TriggerStepSrc(tls, pParse, pStep) if pSrc != 0 { pSel = _sqlite3SelectNew(tls, pParse, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList, pSrc, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(0), uintptr(0)) if pSel == uintptr(0) { (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList = uintptr(0) pSrc = uintptr(0) rc = int32(SQLITE_NOMEM) } else { /* pStep->pExprList contains an expression-list used for an UPDATE ** statement. So the a[].zEName values are the RHS of the ** "= " clauses of the UPDATE statement. So, before ** running SelectPrep(), change all the eEName values in ** pStep->pExprList to ENAME_SPAN (from their current value of ** ENAME_NAME). This is to prevent any ids in ON() clauses that are ** part of pSrc from being incorrectly resolved against the ** a[].zEName values as if they were column aliases. */ _renameSetENames(tls, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList, int32(ENAME_SPAN)) _sqlite3SelectPrep(tls, pParse, pSel, uintptr(0)) _renameSetENames(tls, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList, ENAME_NAME) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { v2 = int32(SQLITE_ERROR) } else { v2 = SQLITE_OK } rc = v2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList != 0 { (*TSelect)(unsafe.Pointer(pSel)).FpEList = uintptr(0) } (*TSelect)(unsafe.Pointer(pSel)).FpSrc = uintptr(0) _sqlite3SelectDelete(tls, db, pSel) } if (*TTriggerStep)(unsafe.Pointer(pStep)).FpFrom != 0 { i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(pStep)).FpFrom)).FnSrc && rc == SQLITE_OK) { break } p = (*TTriggerStep)(unsafe.Pointer(pStep)).FpFrom + 8 + uintptr(i)*72 if (*TSrcItem)(unsafe.Pointer(p)).FpSelect != 0 { _sqlite3SelectPrep(tls, pParse, (*TSrcItem)(unsafe.Pointer(p)).FpSelect, uintptr(0)) } goto _3 _3: ; i++ } } if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { rc = int32(SQLITE_NOMEM) } (*(*TNameContext)(unsafe.Pointer(bp))).FpSrcList = pSrc if rc == SQLITE_OK && (*TTriggerStep)(unsafe.Pointer(pStep)).FpWhere != 0 { rc = _sqlite3ResolveExprNames(tls, bp, (*TTriggerStep)(unsafe.Pointer(pStep)).FpWhere) } if rc == SQLITE_OK { rc = _sqlite3ResolveExprListNames(tls, bp, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList) } _ = libc.Int32FromInt32(0) if (*TTriggerStep)(unsafe.Pointer(pStep)).FpUpsert != 0 && rc == SQLITE_OK { pUpsert = (*TTriggerStep)(unsafe.Pointer(pStep)).FpUpsert (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertSrc = pSrc *(*uintptr)(unsafe.Pointer(bp + 8)) = pUpsert (*(*TNameContext)(unsafe.Pointer(bp))).FncFlags = int32(NC_UUpsert) rc = _sqlite3ResolveExprListNames(tls, bp, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTarget) if rc == SQLITE_OK { pUpsertSet = (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertSet rc = _sqlite3ResolveExprListNames(tls, bp, pUpsertSet) } if rc == SQLITE_OK { rc = _sqlite3ResolveExprNames(tls, bp, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertWhere) } if rc == SQLITE_OK { rc = _sqlite3ResolveExprNames(tls, bp, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTargetWhere) } (*(*TNameContext)(unsafe.Pointer(bp))).FncFlags = 0 } (*(*TNameContext)(unsafe.Pointer(bp))).FpSrcList = uintptr(0) _sqlite3SrcListDelete(tls, db, pSrc) } else { rc = int32(SQLITE_NOMEM) } } goto _1 _1: ; pStep = (*TTriggerStep)(unsafe.Pointer(pStep)).FpNext } return rc } // C documentation // // /* // ** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr // ** objects that are part of the trigger passed as the second argument. // */ func _renameWalkTrigger(tls *libc.TLS, pWalker uintptr, pTrigger uintptr) { var i int32 var pStep, pUpsert uintptr _, _, _ = i, pStep, pUpsert /* Find tokens to edit in WHEN clause */ _sqlite3WalkExpr(tls, pWalker, (*TTrigger)(unsafe.Pointer(pTrigger)).FpWhen) /* Find tokens to edit in trigger steps */ pStep = (*TTrigger)(unsafe.Pointer(pTrigger)).Fstep_list for { if !(pStep != 0) { break } _sqlite3WalkSelect(tls, pWalker, (*TTriggerStep)(unsafe.Pointer(pStep)).FpSelect) _sqlite3WalkExpr(tls, pWalker, (*TTriggerStep)(unsafe.Pointer(pStep)).FpWhere) _sqlite3WalkExprList(tls, pWalker, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList) if (*TTriggerStep)(unsafe.Pointer(pStep)).FpUpsert != 0 { pUpsert = (*TTriggerStep)(unsafe.Pointer(pStep)).FpUpsert _sqlite3WalkExprList(tls, pWalker, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTarget) _sqlite3WalkExprList(tls, pWalker, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertSet) _sqlite3WalkExpr(tls, pWalker, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertWhere) _sqlite3WalkExpr(tls, pWalker, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTargetWhere) } if (*TTriggerStep)(unsafe.Pointer(pStep)).FpFrom != 0 { i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(pStep)).FpFrom)).FnSrc) { break } _sqlite3WalkSelect(tls, pWalker, (*(*TSrcItem)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(pStep)).FpFrom + 8 + uintptr(i)*72))).FpSelect) goto _2 _2: ; i++ } } goto _1 _1: ; pStep = (*TTriggerStep)(unsafe.Pointer(pStep)).FpNext } } // C documentation // // /* // ** Free the contents of Parse object (*pParse). Do not free the memory // ** occupied by the Parse object itself. // */ func _renameParseCleanup(tls *libc.TLS, pParse uintptr) { var db, pIdx, v1 uintptr _, _, _ = db, pIdx, v1 db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*TParse)(unsafe.Pointer(pParse)).FpVdbe != 0 { _sqlite3VdbeFinalize(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe) } _sqlite3DeleteTable(tls, db, (*TParse)(unsafe.Pointer(pParse)).FpNewTable) for { v1 = (*TParse)(unsafe.Pointer(pParse)).FpNewIndex pIdx = v1 if !(v1 != uintptr(0)) { break } (*TParse)(unsafe.Pointer(pParse)).FpNewIndex = (*TIndex)(unsafe.Pointer(pIdx)).FpNext _sqlite3FreeIndex(tls, db, pIdx) } _sqlite3DeleteTrigger(tls, db, (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger) _sqlite3DbFree(tls, db, (*TParse)(unsafe.Pointer(pParse)).FzErrMsg) _renameTokenFree(tls, db, (*TParse)(unsafe.Pointer(pParse)).FpRename) _sqlite3ParseObjectReset(tls, pParse) } // C documentation // // /* // ** SQL function: // ** // ** sqlite_rename_column(SQL,TYPE,OBJ,DB,TABLE,COL,NEWNAME,QUOTE,TEMP) // ** // ** 0. zSql: SQL statement to rewrite // ** 1. type: Type of object ("table", "view" etc.) // ** 2. object: Name of object // ** 3. Database: Database name (e.g. "main") // ** 4. Table: Table name // ** 5. iCol: Index of column to rename // ** 6. zNew: New column name // ** 7. bQuote: Non-zero if the new column name should be quoted. // ** 8. bTemp: True if zSql comes from temp schema // ** // ** Do a column rename operation on the CREATE statement given in zSql. // ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol // ** into zNew. The name should be quoted if bQuote is true. // ** // ** This function is used internally by the ALTER TABLE RENAME COLUMN command. // ** It is only accessible to SQL created using sqlite3NestedParse(). It is // ** not reachable from ordinary SQL passed into sqlite3_prepare() unless the // ** SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test setting is enabled. // */ func _renameColumnFunc(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) { bp := tls.Alloc(336) defer tls.Free(336) var bFKOnly, bQuote, bTemp, i, iCol, rc, v1, v2 int32 var db, pExpr, pFKey, pIdx, pSelect, pStep, pTab, pTarget, pUpsertSet, zDb, zNew, zOld, zSql, zTable uintptr var xAuth Tsqlite3_xauth var _ /* sCtx at bp+0 */ TRenameCtx var _ /* sParse at bp+20 */ TParse var _ /* sWalker at bp+304 */ TWalker _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bFKOnly, bQuote, bTemp, db, i, iCol, pExpr, pFKey, pIdx, pSelect, pStep, pTab, pTarget, pUpsertSet, rc, xAuth, zDb, zNew, zOld, zSql, zTable, v1, v2 db = Xsqlite3_context_db_handle(tls, context) zSql = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) zDb = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 3*4))) zTable = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 4*4))) iCol = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 5*4))) zNew = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 6*4))) bQuote = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 7*4))) bTemp = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 8*4))) xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth _ = NotUsed if zSql == uintptr(0) { return } if zTable == uintptr(0) { return } if zNew == uintptr(0) { return } if iCol < 0 { return } _sqlite3BtreeEnterAll(tls, db) pTab = _sqlite3FindTable(tls, db, zTable, zDb) if pTab == uintptr(0) || iCol >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { _sqlite3BtreeLeaveAll(tls, db) return } zOld = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName libc.Xmemset(tls, bp, 0, uint32(20)) if iCol == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { v1 = -int32(1) } else { v1 = iCol } (*(*TRenameCtx)(unsafe.Pointer(bp))).FiCol = v1 (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0) rc = _renameParseSql(tls, bp+20, zDb, db, zSql, bTemp) /* Find tokens that need to be replaced. */ libc.Xmemset(tls, bp+304, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp + 304))).FpParse = bp + 20 (*(*TWalker)(unsafe.Pointer(bp + 304))).FxExprCallback = __ccgo_fp(_renameColumnExprCb) (*(*TWalker)(unsafe.Pointer(bp + 304))).FxSelectCallback = __ccgo_fp(_renameColumnSelectCb) *(*uintptr)(unsafe.Pointer(bp + 304 + 24)) = bp (*(*TRenameCtx)(unsafe.Pointer(bp))).FpTab = pTab if rc != SQLITE_OK { goto renameColumnFunc_done } if (*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable != 0 { if int32((*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable)).FeTabType) == int32(TABTYP_VIEW) { pSelect = (*(*struct { FpSelect uintptr })(unsafe.Pointer(&(*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable)).Fu))).FpSelect *(*Tu32)(unsafe.Pointer(pSelect + 4)) &= uint32(^libc.Int32FromInt32(SF_View)) (*(*TParse)(unsafe.Pointer(bp + 20))).Frc = SQLITE_OK _sqlite3SelectPrep(tls, bp+20, pSelect, uintptr(0)) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { v2 = int32(SQLITE_NOMEM) } else { v2 = (*(*TParse)(unsafe.Pointer(bp + 20))).Frc } rc = v2 if rc == SQLITE_OK { _sqlite3WalkSelect(tls, bp+304, pSelect) } if rc != SQLITE_OK { goto renameColumnFunc_done } } else { if int32((*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable)).FeTabType) == TABTYP_NORM { /* A regular table */ bFKOnly = Xsqlite3_stricmp(tls, zTable, (*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable)).FzName) (*(*TRenameCtx)(unsafe.Pointer(bp))).FpTab = (*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable if bFKOnly == 0 { if iCol < int32((*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable)).FnCol) { _renameTokenFind(tls, bp+20, bp, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable)).FaCol + uintptr(iCol)*12))).FzCnName) } if (*(*TRenameCtx)(unsafe.Pointer(bp))).FiCol < 0 { _renameTokenFind(tls, bp+20, bp, (*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable+32) } _sqlite3WalkExprList(tls, bp+304, (*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable)).FpCheck) pIdx = (*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable)).FpIndex for { if !(pIdx != 0) { break } _sqlite3WalkExprList(tls, bp+304, (*TIndex)(unsafe.Pointer(pIdx)).FaColExpr) goto _3 _3: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } pIdx = (*(*TParse)(unsafe.Pointer(bp + 20))).FpNewIndex for { if !(pIdx != 0) { break } _sqlite3WalkExprList(tls, bp+304, (*TIndex)(unsafe.Pointer(pIdx)).FaColExpr) goto _4 _4: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable)).FnCol)) { break } pExpr = _sqlite3ColumnExpr(tls, (*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable, (*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable)).FaCol+uintptr(i)*12) _sqlite3WalkExpr(tls, bp+304, pExpr) goto _5 _5: ; i++ } } _ = libc.Int32FromInt32(0) pFKey = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTable + 44))).FpFKey for { if !(pFKey != 0) { break } i = 0 for { if !(i < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) { break } if bFKOnly == 0 && (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(i)*8))).FiFrom == iCol { _renameTokenFind(tls, bp+20, bp, pFKey+36+uintptr(i)*8) } if 0 == Xsqlite3_stricmp(tls, (*TFKey)(unsafe.Pointer(pFKey)).FzTo, zTable) && 0 == Xsqlite3_stricmp(tls, (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(i)*8))).FzCol, zOld) { _renameTokenFind(tls, bp+20, bp, (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(i)*8))).FzCol) } goto _7 _7: ; i++ } goto _6 _6: ; pFKey = (*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom } } } } else { if (*(*TParse)(unsafe.Pointer(bp + 20))).FpNewIndex != 0 { _sqlite3WalkExprList(tls, bp+304, (*TIndex)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewIndex)).FaColExpr) _sqlite3WalkExpr(tls, bp+304, (*TIndex)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewIndex)).FpPartIdxWhere) } else { rc = _renameResolveTrigger(tls, bp+20) if rc != SQLITE_OK { goto renameColumnFunc_done } pStep = (*TTrigger)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTrigger)).Fstep_list for { if !(pStep != 0) { break } if (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget != 0 { pTarget = _sqlite3LocateTable(tls, bp+20, uint32(0), (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget, zDb) if pTarget == pTab { if (*TTriggerStep)(unsafe.Pointer(pStep)).FpUpsert != 0 { pUpsertSet = (*TUpsert)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(pStep)).FpUpsert)).FpUpsertSet _renameColumnElistNames(tls, bp+20, bp, pUpsertSet, zOld) } _renameColumnIdlistNames(tls, bp+20, bp, (*TTriggerStep)(unsafe.Pointer(pStep)).FpIdList, zOld) _renameColumnElistNames(tls, bp+20, bp, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList, zOld) } } goto _8 _8: ; pStep = (*TTriggerStep)(unsafe.Pointer(pStep)).FpNext } /* Find tokens to edit in UPDATE OF clause */ if (*(*TParse)(unsafe.Pointer(bp + 20))).FpTriggerTab == pTab { _renameColumnIdlistNames(tls, bp+20, bp, (*TTrigger)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTrigger)).FpColumns, zOld) } /* Find tokens to edit in various expressions and selects */ _renameWalkTrigger(tls, bp+304, (*(*TParse)(unsafe.Pointer(bp + 20))).FpNewTrigger) } } _ = libc.Int32FromInt32(0) rc = _renameEditSql(tls, context, bp, zSql, zNew, bQuote) goto renameColumnFunc_done renameColumnFunc_done: ; if rc != SQLITE_OK { if rc == int32(SQLITE_ERROR) && _sqlite3WritableSchema(tls, db) != 0 { Xsqlite3_result_value(tls, context, *(*uintptr)(unsafe.Pointer(argv))) } else { if (*(*TParse)(unsafe.Pointer(bp + 20))).FzErrMsg != 0 { _renameColumnParseError(tls, context, __ccgo_ts+1648, *(*uintptr)(unsafe.Pointer(argv + 1*4)), *(*uintptr)(unsafe.Pointer(argv + 2*4)), bp+20) } else { Xsqlite3_result_error_code(tls, context, rc) } } } _renameParseCleanup(tls, bp+20) _renameTokenFree(tls, db, (*(*TRenameCtx)(unsafe.Pointer(bp))).FpList) (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth _sqlite3BtreeLeaveAll(tls, db) } // C documentation // // /* // ** Walker expression callback used by "RENAME TABLE". // */ func _renameTableExprCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var p uintptr _ = p p = *(*uintptr)(unsafe.Pointer(pWalker + 24)) if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)|libc.Int32FromInt32(EP_Subrtn)) == uint32(0) && (*TRenameCtx)(unsafe.Pointer(p)).FpTab == (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab { _renameTokenFind(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, p, pExpr+44) } return WRC_Continue } // C documentation // // /* // ** Walker select callback used by "RENAME TABLE". // */ func _renameTableSelectCb(tls *libc.TLS, pWalker uintptr, pSelect uintptr) (r int32) { var i int32 var p, pItem, pSrc uintptr _, _, _, _ = i, p, pItem, pSrc p = *(*uintptr)(unsafe.Pointer(pWalker + 24)) pSrc = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc if (*TSelect)(unsafe.Pointer(pSelect)).FselFlags&uint32(libc.Int32FromInt32(SF_View)|libc.Int32FromInt32(SF_CopyCte)) != 0 { return int32(WRC_Prune) } if pSrc == uintptr(0) { _ = libc.Int32FromInt32(0) return int32(WRC_Abort) } i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) { break } pItem = pSrc + 8 + uintptr(i)*72 if (*TSrcItem)(unsafe.Pointer(pItem)).FpTab == (*TRenameCtx)(unsafe.Pointer(p)).FpTab { _renameTokenFind(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, p, (*TSrcItem)(unsafe.Pointer(pItem)).FzName) } goto _1 _1: ; i++ } _renameWalkWith(tls, pWalker, pSelect) return WRC_Continue } // C documentation // // /* // ** This C function implements an SQL user function that is used by SQL code // ** generated by the ALTER TABLE ... RENAME command to modify the definition // ** of any foreign key constraints that use the table being renamed as the // ** parent table. It is passed three arguments: // ** // ** 0: The database containing the table being renamed. // ** 1. type: Type of object ("table", "view" etc.) // ** 2. object: Name of object // ** 3: The complete text of the schema statement being modified, // ** 4: The old name of the table being renamed, and // ** 5: The new name of the table being renamed. // ** 6: True if the schema statement comes from the temp db. // ** // ** It returns the new schema statement. For example: // ** // ** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0) // ** -> 'CREATE TABLE t1(a REFERENCES t3)' // */ func _renameTableFunc(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) { bp := tls.Alloc(368) defer tls.Free(368) var bQuote, bTemp, i, isLegacy, rc int32 var db, pFKey, pItem, pSelect, pStep, pTab, pTrigger, zDb, zInput, zNew, zOld uintptr var xAuth Tsqlite3_xauth var _ /* sCtx at bp+284 */ TRenameCtx var _ /* sNC at bp+332 */ TNameContext var _ /* sParse at bp+0 */ TParse var _ /* sWalker at bp+304 */ TWalker _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bQuote, bTemp, db, i, isLegacy, pFKey, pItem, pSelect, pStep, pTab, pTrigger, rc, xAuth, zDb, zInput, zNew, zOld db = Xsqlite3_context_db_handle(tls, context) zDb = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) zInput = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 3*4))) zOld = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 4*4))) zNew = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 5*4))) bTemp = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 6*4))) _ = NotUsed if zInput != 0 && zOld != 0 && zNew != 0 { bQuote = int32(1) xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0) _sqlite3BtreeEnterAll(tls, db) libc.Xmemset(tls, bp+284, 0, uint32(20)) (*(*TRenameCtx)(unsafe.Pointer(bp + 284))).FpTab = _sqlite3FindTable(tls, db, zOld, zDb) libc.Xmemset(tls, bp+304, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp + 304))).FpParse = bp (*(*TWalker)(unsafe.Pointer(bp + 304))).FxExprCallback = __ccgo_fp(_renameTableExprCb) (*(*TWalker)(unsafe.Pointer(bp + 304))).FxSelectCallback = __ccgo_fp(_renameTableSelectCb) *(*uintptr)(unsafe.Pointer(bp + 304 + 24)) = bp + 284 rc = _renameParseSql(tls, bp, zDb, db, zInput, bTemp) if rc == SQLITE_OK { isLegacy = int32((*Tsqlite3)(unsafe.Pointer(db)).Fflags & libc.Uint64FromInt32(SQLITE_LegacyAlter)) if (*(*TParse)(unsafe.Pointer(bp))).FpNewTable != 0 { pTab = (*(*TParse)(unsafe.Pointer(bp))).FpNewTable if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { if isLegacy == 0 { pSelect = (*(*struct { FpSelect uintptr })(unsafe.Pointer(&(*TTable)(unsafe.Pointer(pTab)).Fu))).FpSelect libc.Xmemset(tls, bp+332, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp + 332))).FpParse = bp _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(pSelect + 4)) &= uint32(^libc.Int32FromInt32(SF_View)) _sqlite3SelectPrep(tls, bp, (*(*struct { FpSelect uintptr })(unsafe.Pointer(&(*TTable)(unsafe.Pointer(pTab)).Fu))).FpSelect, bp+332) if (*(*TParse)(unsafe.Pointer(bp))).FnErr != 0 { rc = (*(*TParse)(unsafe.Pointer(bp))).Frc } else { _sqlite3WalkSelect(tls, bp+304, (*(*struct { FpSelect uintptr })(unsafe.Pointer(&(*TTable)(unsafe.Pointer(pTab)).Fu))).FpSelect) } } } else { /* Modify any FK definitions to point to the new table. */ if (isLegacy == 0 || (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) != 0) && !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) { _ = libc.Int32FromInt32(0) pFKey = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpFKey for { if !(pFKey != 0) { break } if Xsqlite3_stricmp(tls, (*TFKey)(unsafe.Pointer(pFKey)).FzTo, zOld) == 0 { _renameTokenFind(tls, bp, bp+284, (*TFKey)(unsafe.Pointer(pFKey)).FzTo) } goto _1 _1: ; pFKey = (*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom } } /* If this is the table being altered, fix any table refs in CHECK ** expressions. Also update the name that appears right after the ** "CREATE [VIRTUAL] TABLE" bit. */ if Xsqlite3_stricmp(tls, zOld, (*TTable)(unsafe.Pointer(pTab)).FzName) == 0 { (*(*TRenameCtx)(unsafe.Pointer(bp + 284))).FpTab = pTab if isLegacy == 0 { _sqlite3WalkExprList(tls, bp+304, (*TTable)(unsafe.Pointer(pTab)).FpCheck) } _renameTokenFind(tls, bp, bp+284, (*TTable)(unsafe.Pointer(pTab)).FzName) } } } else { if (*(*TParse)(unsafe.Pointer(bp))).FpNewIndex != 0 { _renameTokenFind(tls, bp, bp+284, (*TIndex)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewIndex)).FzName) if isLegacy == 0 { _sqlite3WalkExpr(tls, bp+304, (*TIndex)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewIndex)).FpPartIdxWhere) } } else { pTrigger = (*(*TParse)(unsafe.Pointer(bp))).FpNewTrigger if 0 == Xsqlite3_stricmp(tls, (*TTrigger)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewTrigger)).Ftable, zOld) && (*TTable)(unsafe.Pointer((*(*TRenameCtx)(unsafe.Pointer(bp + 284))).FpTab)).FpSchema == (*TTrigger)(unsafe.Pointer(pTrigger)).FpTabSchema { _renameTokenFind(tls, bp, bp+284, (*TTrigger)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewTrigger)).Ftable) } if isLegacy == 0 { rc = _renameResolveTrigger(tls, bp) if rc == SQLITE_OK { _renameWalkTrigger(tls, bp+304, pTrigger) pStep = (*TTrigger)(unsafe.Pointer(pTrigger)).Fstep_list for { if !(pStep != 0) { break } if (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget != 0 && 0 == Xsqlite3_stricmp(tls, (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget, zOld) { _renameTokenFind(tls, bp, bp+284, (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget) } if (*TTriggerStep)(unsafe.Pointer(pStep)).FpFrom != 0 { i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(pStep)).FpFrom)).FnSrc) { break } pItem = (*TTriggerStep)(unsafe.Pointer(pStep)).FpFrom + 8 + uintptr(i)*72 if 0 == Xsqlite3_stricmp(tls, (*TSrcItem)(unsafe.Pointer(pItem)).FzName, zOld) { _renameTokenFind(tls, bp, bp+284, (*TSrcItem)(unsafe.Pointer(pItem)).FzName) } goto _3 _3: ; i++ } } goto _2 _2: ; pStep = (*TTriggerStep)(unsafe.Pointer(pStep)).FpNext } } } } } } if rc == SQLITE_OK { rc = _renameEditSql(tls, context, bp+284, zInput, zNew, bQuote) } if rc != SQLITE_OK { if rc == int32(SQLITE_ERROR) && _sqlite3WritableSchema(tls, db) != 0 { Xsqlite3_result_value(tls, context, *(*uintptr)(unsafe.Pointer(argv + 3*4))) } else { if (*(*TParse)(unsafe.Pointer(bp))).FzErrMsg != 0 { _renameColumnParseError(tls, context, __ccgo_ts+1648, *(*uintptr)(unsafe.Pointer(argv + 1*4)), *(*uintptr)(unsafe.Pointer(argv + 2*4)), bp) } else { Xsqlite3_result_error_code(tls, context, rc) } } } _renameParseCleanup(tls, bp) _renameTokenFree(tls, db, (*(*TRenameCtx)(unsafe.Pointer(bp + 284))).FpList) _sqlite3BtreeLeaveAll(tls, db) (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth } return } func _renameQuotefixExprCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_STRING) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_DblQuoted) != 0 { _renameTokenFind(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, *(*uintptr)(unsafe.Pointer(pWalker + 24)), pExpr) } return WRC_Continue } // C documentation // // /* SQL function: sqlite_rename_quotefix(DB,SQL) // ** // ** Rewrite the DDL statement "SQL" so that any string literals that use // ** double-quotes use single quotes instead. // ** // ** Two arguments must be passed: // ** // ** 0: Database name ("main", "temp" etc.). // ** 1: SQL statement to edit. // ** // ** The returned value is the modified SQL statement. For example, given // ** the database schema: // ** // ** CREATE TABLE t1(a, b, c); // ** // ** SELECT sqlite_rename_quotefix('main', // ** 'CREATE VIEW v1 AS SELECT "a", "string" FROM t1' // ** ); // ** // ** returns the string: // ** // ** CREATE VIEW v1 AS SELECT "a", 'string' FROM t1 // ** // ** If there is a error in the input SQL, then raise an error, except // ** if PRAGMA writable_schema=ON, then just return the input string // ** unmodified following an error. // */ func _renameQuotefixFunc(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) { bp := tls.Alloc(336) defer tls.Free(336) var db, pSelect, zDb, zInput uintptr var i, rc, v1 int32 var xAuth Tsqlite3_xauth var _ /* sCtx at bp+284 */ TRenameCtx var _ /* sParse at bp+0 */ TParse var _ /* sWalker at bp+304 */ TWalker _, _, _, _, _, _, _, _ = db, i, pSelect, rc, xAuth, zDb, zInput, v1 db = Xsqlite3_context_db_handle(tls, context) zDb = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) zInput = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0) _sqlite3BtreeEnterAll(tls, db) _ = NotUsed if zDb != 0 && zInput != 0 { rc = _renameParseSql(tls, bp, zDb, db, zInput, 0) if rc == SQLITE_OK { /* Walker to find tokens that need to be replaced. */ libc.Xmemset(tls, bp+284, 0, uint32(20)) libc.Xmemset(tls, bp+304, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp + 304))).FpParse = bp (*(*TWalker)(unsafe.Pointer(bp + 304))).FxExprCallback = __ccgo_fp(_renameQuotefixExprCb) (*(*TWalker)(unsafe.Pointer(bp + 304))).FxSelectCallback = __ccgo_fp(_renameColumnSelectCb) *(*uintptr)(unsafe.Pointer(bp + 304 + 24)) = bp + 284 if (*(*TParse)(unsafe.Pointer(bp))).FpNewTable != 0 { if int32((*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewTable)).FeTabType) == int32(TABTYP_VIEW) { pSelect = (*(*struct { FpSelect uintptr })(unsafe.Pointer(&(*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewTable)).Fu))).FpSelect *(*Tu32)(unsafe.Pointer(pSelect + 4)) &= uint32(^libc.Int32FromInt32(SF_View)) (*(*TParse)(unsafe.Pointer(bp))).Frc = SQLITE_OK _sqlite3SelectPrep(tls, bp, pSelect, uintptr(0)) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { v1 = int32(SQLITE_NOMEM) } else { v1 = (*(*TParse)(unsafe.Pointer(bp))).Frc } rc = v1 if rc == SQLITE_OK { _sqlite3WalkSelect(tls, bp+304, pSelect) } } else { _sqlite3WalkExprList(tls, bp+304, (*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewTable)).FpCheck) i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewTable)).FnCol)) { break } _sqlite3WalkExpr(tls, bp+304, _sqlite3ColumnExpr(tls, (*(*TParse)(unsafe.Pointer(bp))).FpNewTable, (*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewTable)).FaCol+uintptr(i)*12)) goto _2 _2: ; i++ } } } else { if (*(*TParse)(unsafe.Pointer(bp))).FpNewIndex != 0 { _sqlite3WalkExprList(tls, bp+304, (*TIndex)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewIndex)).FaColExpr) _sqlite3WalkExpr(tls, bp+304, (*TIndex)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewIndex)).FpPartIdxWhere) } else { rc = _renameResolveTrigger(tls, bp) if rc == SQLITE_OK { _renameWalkTrigger(tls, bp+304, (*(*TParse)(unsafe.Pointer(bp))).FpNewTrigger) } } } if rc == SQLITE_OK { rc = _renameEditSql(tls, context, bp+284, zInput, uintptr(0), 0) } _renameTokenFree(tls, db, (*(*TRenameCtx)(unsafe.Pointer(bp + 284))).FpList) } if rc != SQLITE_OK { if _sqlite3WritableSchema(tls, db) != 0 && rc == int32(SQLITE_ERROR) { Xsqlite3_result_value(tls, context, *(*uintptr)(unsafe.Pointer(argv + 1*4))) } else { Xsqlite3_result_error_code(tls, context, rc) } } _renameParseCleanup(tls, bp) } (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth _sqlite3BtreeLeaveAll(tls, db) } // C documentation // // /* Function: sqlite_rename_test(DB,SQL,TYPE,NAME,ISTEMP,WHEN,DQS) // ** // ** An SQL user function that checks that there are no parse or symbol // ** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement. // ** After an ALTER TABLE .. RENAME operation is performed and the schema // ** reloaded, this function is called on each SQL statement in the schema // ** to ensure that it is still usable. // ** // ** 0: Database name ("main", "temp" etc.). // ** 1: SQL statement. // ** 2: Object type ("view", "table", "trigger" or "index"). // ** 3: Object name. // ** 4: True if object is from temp schema. // ** 5: "when" part of error message. // ** 6: True to disable the DQS quirk when parsing SQL. // ** // ** The return value is computed as follows: // ** // ** A. If an error is seen and not in PRAGMA writable_schema=ON mode, // ** then raise the error. // ** B. Else if a trigger is created and the the table that the trigger is // ** attached to is in database zDb, then return 1. // ** C. Otherwise return NULL. // */ func _renameTableTest(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) { bp := tls.Alloc(320) defer tls.Free(320) var bNoDQS, bTemp, flags, i1, i2, isLegacy, rc int32 var db, zDb, zInput, zWhen uintptr var xAuth Tsqlite3_xauth var _ /* sNC at bp+284 */ TNameContext var _ /* sParse at bp+0 */ TParse _, _, _, _, _, _, _, _, _, _, _, _ = bNoDQS, bTemp, db, flags, i1, i2, isLegacy, rc, xAuth, zDb, zInput, zWhen db = Xsqlite3_context_db_handle(tls, context) zDb = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) zInput = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) bTemp = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 4*4))) isLegacy = int32((*Tsqlite3)(unsafe.Pointer(db)).Fflags & libc.Uint64FromInt32(SQLITE_LegacyAlter)) zWhen = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 5*4))) bNoDQS = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 6*4))) xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0) _ = NotUsed if zDb != 0 && zInput != 0 { flags = int32((*Tsqlite3)(unsafe.Pointer(db)).Fflags) if bNoDQS != 0 { *(*Tu64)(unsafe.Pointer(db + 32)) &= uint64(^(libc.Int32FromInt32(SQLITE_DqsDML) | libc.Int32FromInt32(SQLITE_DqsDDL))) } rc = _renameParseSql(tls, bp, zDb, db, zInput, bTemp) *(*Tu64)(unsafe.Pointer(db + 32)) |= uint64(flags & (libc.Int32FromInt32(SQLITE_DqsDML) | libc.Int32FromInt32(SQLITE_DqsDDL))) if rc == SQLITE_OK { if isLegacy == 0 && (*(*TParse)(unsafe.Pointer(bp))).FpNewTable != 0 && int32((*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewTable)).FeTabType) == int32(TABTYP_VIEW) { libc.Xmemset(tls, bp+284, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp + 284))).FpParse = bp _sqlite3SelectPrep(tls, bp, (*(*struct { FpSelect uintptr })(unsafe.Pointer(&(*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewTable)).Fu))).FpSelect, bp+284) if (*(*TParse)(unsafe.Pointer(bp))).FnErr != 0 { rc = (*(*TParse)(unsafe.Pointer(bp))).Frc } } else { if (*(*TParse)(unsafe.Pointer(bp))).FpNewTrigger != 0 { if isLegacy == 0 { rc = _renameResolveTrigger(tls, bp) } if rc == SQLITE_OK { i1 = _sqlite3SchemaToIndex(tls, db, (*TTrigger)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewTrigger)).FpTabSchema) i2 = _sqlite3FindDbName(tls, db, zDb) if i1 == i2 { /* Handle output case B */ Xsqlite3_result_int(tls, context, int32(1)) } } } } } if rc != SQLITE_OK && zWhen != 0 && !(_sqlite3WritableSchema(tls, db) != 0) { /* Output case A */ _renameColumnParseError(tls, context, zWhen, *(*uintptr)(unsafe.Pointer(argv + 2*4)), *(*uintptr)(unsafe.Pointer(argv + 3*4)), bp) } _renameParseCleanup(tls, bp) } (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth } // C documentation // // /* // ** The implementation of internal UDF sqlite_drop_column(). // ** // ** Arguments: // ** // ** argv[0]: An integer - the index of the schema containing the table // ** argv[1]: CREATE TABLE statement to modify. // ** argv[2]: An integer - the index of the column to remove. // ** // ** The value returned is a string containing the CREATE TABLE statement // ** with column argv[2] removed. // */ func _dropColumnFunc(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) { bp := tls.Alloc(320) defer tls.Free(320) var db, pCol, pEnd, pTab, zDb, zEnd, zNew, zSql uintptr var iCol, iSchema, rc int32 var xAuth Tsqlite3_xauth var _ /* sParse at bp+0 */ TParse _, _, _, _, _, _, _, _, _, _, _, _ = db, iCol, iSchema, pCol, pEnd, pTab, rc, xAuth, zDb, zEnd, zNew, zSql db = Xsqlite3_context_db_handle(tls, context) iSchema = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv))) zSql = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) iCol = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4))) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iSchema)*16))).FzDbSName zNew = uintptr(0) xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0) _ = NotUsed rc = _renameParseSql(tls, bp, zDb, db, zSql, libc.BoolInt32(iSchema == int32(1))) if rc != SQLITE_OK { goto drop_column_done } pTab = (*(*TParse)(unsafe.Pointer(bp))).FpNewTable if pTab == uintptr(0) || int32((*TTable)(unsafe.Pointer(pTab)).FnCol) == int32(1) || iCol >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { /* This can happen if the sqlite_schema table is corrupt */ rc = _sqlite3CorruptError(tls, int32(117652)) goto drop_column_done } pCol = _renameTokenFind(tls, bp, uintptr(0), (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName) if iCol < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)-int32(1) { pEnd = _renameTokenFind(tls, bp, uintptr(0), (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol+int32(1))*12))).FzCnName) zEnd = (*TRenameToken)(unsafe.Pointer(pEnd)).Ft.Fz } else { _ = libc.Int32FromInt32(0) zEnd = zSql + uintptr((*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FaddColOffset) for int32(*(*uint8)(unsafe.Pointer((*TRenameToken)(unsafe.Pointer(pCol)).Ft.Fz))) != 0 && int32(*(*uint8)(unsafe.Pointer((*TRenameToken)(unsafe.Pointer(pCol)).Ft.Fz))) != int32(',') { (*TRenameToken)(unsafe.Pointer(pCol)).Ft.Fz-- } } zNew = _sqlite3MPrintf(tls, db, __ccgo_ts+11246, libc.VaList(bp+296, int32((*TRenameToken)(unsafe.Pointer(pCol)).Ft.Fz)-int32(zSql), zSql, zEnd)) Xsqlite3_result_text(tls, context, zNew, -int32(1), uintptr(-libc.Int32FromInt32(1))) Xsqlite3_free(tls, zNew) goto drop_column_done drop_column_done: ; _renameParseCleanup(tls, bp) (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth if rc != SQLITE_OK { Xsqlite3_result_error_code(tls, context, rc) } } // C documentation // // /* // ** This function is called by the parser upon parsing an // ** // ** ALTER TABLE pSrc DROP COLUMN pName // ** // ** statement. Argument pSrc contains the possibly qualified name of the // ** table being edited, and token pName the name of the column to drop. // */ func _sqlite3AlterDropColumn(tls *libc.TLS, pParse uintptr, pSrc uintptr, pName uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var addr, i, iCol, iColPos, iCur, iDb, iPos, nField, reg, regOut, regRec, v2, v4, v7 int32 var db, pPk, pTab, v, zCol, zDb, v1, v3, v5, v8 uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, db, i, iCol, iColPos, iCur, iDb, iPos, nField, pPk, pTab, reg, regOut, regRec, v, zCol, zDb, v1, v2, v3, v4, v5, v7, v8 db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Database containing pTab ("main" etc.) */ zCol = uintptr(0) /* Index of column zCol in pTab->aCol[] */ /* Look up the table being altered. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto exit_drop_column } pTab = _sqlite3LocateTableItem(tls, pParse, uint32(0), pSrc+8) if !(pTab != 0) { goto exit_drop_column } /* Make sure this is not an attempt to ALTER a view, virtual table or ** system table. */ if SQLITE_OK != _isAlterableTable(tls, pParse, pTab) { goto exit_drop_column } if SQLITE_OK != _isRealTable(tls, pParse, pTab, int32(1)) { goto exit_drop_column } /* Find the index of the column being dropped. */ zCol = _sqlite3NameFromToken(tls, db, pName) if zCol == uintptr(0) { _ = libc.Int32FromInt32(0) goto exit_drop_column } iCol = _sqlite3ColumnIndex(tls, pTab, zCol) if iCol < 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+10868, libc.VaList(bp+8, pName)) goto exit_drop_column } /* Do not allow the user to drop a PRIMARY KEY column or a column ** constrained by a UNIQUE constraint. */ if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FcolFlags)&(libc.Int32FromInt32(COLFLAG_PRIMKEY)|libc.Int32FromInt32(COLFLAG_UNIQUE)) != 0 { if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 { v1 = __ccgo_ts + 11253 } else { v1 = __ccgo_ts + 5589 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+11265, libc.VaList(bp+8, v1, zCol)) goto exit_drop_column } /* Do not allow the number of columns to go to zero */ if int32((*TTable)(unsafe.Pointer(pTab)).FnCol) <= int32(1) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+11293, libc.VaList(bp+8, zCol)) goto exit_drop_column } /* Edit the sqlite_schema table */ iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) _ = libc.Int32FromInt32(0) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName /* Invoke the authorization callback. */ if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_ALTER_TABLE), zDb, (*TTable)(unsafe.Pointer(pTab)).FzName, zCol) != 0 { goto exit_drop_column } _renameTestSchema(tls, pParse, zDb, libc.BoolInt32(iDb == int32(1)), __ccgo_ts+1648, 0) _renameFixQuotes(tls, pParse, zDb, libc.BoolInt32(iDb == int32(1))) _sqlite3NestedParse(tls, pParse, __ccgo_ts+11341, libc.VaList(bp+8, zDb, iDb, iCol, (*TTable)(unsafe.Pointer(pTab)).FzName)) /* Drop and reload the database schema. */ _renameReloadSchema(tls, pParse, iDb, uint16(INITFLAG_AlterDrop)) _renameTestSchema(tls, pParse, zDb, libc.BoolInt32(iDb == int32(1)), __ccgo_ts+11462, int32(1)) /* Edit rows of table on disk */ if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 { pPk = uintptr(0) nField = 0 v = _sqlite3GetVdbe(tls, pParse) v3 = pParse + 40 v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ iCur = v2 _sqlite3OpenTable(tls, pParse, iCur, iDb, pTab, int32(OP_OpenWrite)) addr = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), iCur) v5 = pParse + 44 *(*int32)(unsafe.Pointer(v5))++ v4 = *(*int32)(unsafe.Pointer(v5)) reg = v4 if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iCur, reg) *(*int32)(unsafe.Pointer(pParse + 44)) += int32((*TTable)(unsafe.Pointer(pTab)).FnCol) } else { pPk = _sqlite3PrimaryKeyIndex(tls, pTab) *(*int32)(unsafe.Pointer(pParse + 44)) += int32((*TIndex)(unsafe.Pointer(pPk)).FnColumn) i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) { break } _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iCur, i, reg+i+int32(1)) goto _6 _6: ; i++ } nField = int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol) } v8 = pParse + 44 *(*int32)(unsafe.Pointer(v8))++ v7 = *(*int32)(unsafe.Pointer(v8)) regRec = v7 i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if i != iCol && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 { if pPk != 0 { iPos = int32(_sqlite3TableColumnToIndex(tls, pPk, int16(i))) iColPos = int32(_sqlite3TableColumnToIndex(tls, pPk, int16(iCol))) if iPos < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol) { goto _9 } regOut = reg + int32(1) + iPos - libc.BoolInt32(iPos > iColPos) } else { regOut = reg + int32(1) + nField } if i == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regOut) } else { _sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iCur, i, regOut) } nField++ } goto _9 _9: ; i++ } if nField == 0 { /* dbsqlfuzz 5f09e7bcc78b4954d06bf9f2400d7715f48d1fef */ (*TParse)(unsafe.Pointer(pParse)).FnMem++ _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, reg+int32(1)) nField = int32(1) } _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), reg+int32(1), nField, regRec) if pPk != 0 { _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iCur, regRec, reg+int32(1), int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) } else { _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iCur, regRec, reg) } _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_SAVEPOSITION)) _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iCur, addr+int32(1)) _sqlite3VdbeJumpHere(tls, v, addr) } goto exit_drop_column exit_drop_column: ; _sqlite3DbFree(tls, db, zCol) _sqlite3SrcListDelete(tls, db, pSrc) } // C documentation // // /* // ** Register built-in functions used to help implement ALTER TABLE // */ func _sqlite3AlterFunctions(tls *libc.TLS) { _sqlite3InsertBuiltinFuncs(tls, uintptr(unsafe.Pointer(&_aAlterTableFuncs)), int32(libc.Uint32FromInt64(200)/libc.Uint32FromInt64(40))) } var _aAlterTableFuncs = [5]TFuncDef{ 0: { FnArg: int8(9), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_INTERNAL) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)), FzName: __ccgo_ts + 11480, }, 1: { FnArg: int8(7), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_INTERNAL) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)), FzName: __ccgo_ts + 11501, }, 2: { FnArg: int8(7), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_INTERNAL) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)), FzName: __ccgo_ts + 11521, }, 3: { FnArg: int8(3), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_INTERNAL) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)), FzName: __ccgo_ts + 11540, }, 4: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_INTERNAL) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)), FzName: __ccgo_ts + 11559, }, } func init() { p := unsafe.Pointer(&_aAlterTableFuncs) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_renameColumnFunc) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_renameTableFunc) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_renameTableTest) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_dropColumnFunc) *(*uintptr)(unsafe.Add(p, 176)) = __ccgo_fp(_renameQuotefixFunc) } /************** End of alter.c ***********************************************/ /************** Begin file analyze.c *****************************************/ /* ** 2005-07-08 ** ** 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 contains code associated with the ANALYZE command. ** ** The ANALYZE command gather statistics about the content of tables ** and indices. These statistics are made available to the query planner ** to help it make better decisions about how to perform queries. ** ** The following system tables are or have been supported: ** ** CREATE TABLE sqlite_stat1(tbl, idx, stat); ** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample); ** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample); ** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample); ** ** Additional tables might be added in future releases of SQLite. ** The sqlite_stat2 table is not created or used unless the SQLite version ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated. ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only ** created and used by SQLite versions 3.7.9 through 3.29.0 when ** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3 ** is a superset of sqlite_stat2 and is also now deprecated. The ** sqlite_stat4 is an enhanced version of sqlite_stat3 and is only ** available when compiled with SQLITE_ENABLE_STAT4 and in SQLite ** versions 3.8.1 and later. STAT4 is the only variant that is still ** supported. ** ** For most applications, sqlite_stat1 provides all the statistics required ** for the query planner to make good choices. ** ** Format of sqlite_stat1: ** ** There is normally one row per index, with the index identified by the ** name in the idx column. The tbl column is the name of the table to ** which the index belongs. In each such row, the stat column will be ** a string consisting of a list of integers. The first integer in this ** list is the number of rows in the index. (This is the same as the ** number of rows in the table, except for partial indices.) The second ** integer is the average number of rows in the index that have the same ** value in the first column of the index. The third integer is the average ** number of rows in the index that have the same value for the first two ** columns. The N-th integer (for N>1) is the average number of rows in ** the index which have the same value for the first N-1 columns. For ** a K-column index, there will be K+1 integers in the stat column. If ** the index is unique, then the last integer will be 1. ** ** The list of integers in the stat column can optionally be followed ** by the keyword "unordered". The "unordered" keyword, if it is present, ** must be separated from the last integer by a single space. If the ** "unordered" keyword is present, then the query planner assumes that ** the index is unordered and will not use the index for a range query. ** ** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat ** column contains a single integer which is the (estimated) number of ** rows in the table identified by sqlite_stat1.tbl. ** ** Format of sqlite_stat2: ** ** The sqlite_stat2 is only created and is only used if SQLite is compiled ** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between ** 3.6.18 and 3.7.8. The "stat2" table contains additional information ** about the distribution of keys within an index. The index is identified by ** the "idx" column and the "tbl" column is the name of the table to which ** the index belongs. There are usually 10 rows in the sqlite_stat2 ** table for each index. ** ** The sqlite_stat2 entries for an index that have sampleno between 0 and 9 ** inclusive are samples of the left-most key value in the index taken at ** evenly spaced points along the index. Let the number of samples be S ** (10 in the standard build) and let C be the number of rows in the index. ** Then the sampled rows are given by: ** ** rownumber = (i*C*2 + C)/(S*2) ** ** For i between 0 and S-1. Conceptually, the index space is divided into ** S uniform buckets and the samples are the middle row from each bucket. ** ** The format for sqlite_stat2 is recorded here for legacy reference. This ** version of SQLite does not support sqlite_stat2. It neither reads nor ** writes the sqlite_stat2 table. This version of SQLite only supports ** sqlite_stat3. ** ** Format for sqlite_stat3: ** ** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the ** sqlite_stat4 format will be described first. Further information ** about sqlite_stat3 follows the sqlite_stat4 description. ** ** Format for sqlite_stat4: ** ** As with sqlite_stat2, the sqlite_stat4 table contains histogram data ** to aid the query planner in choosing good indices based on the values ** that indexed columns are compared against in the WHERE clauses of ** queries. ** ** The sqlite_stat4 table contains multiple entries for each index. ** The idx column names the index and the tbl column is the table of the ** index. If the idx and tbl columns are the same, then the sample is ** of the INTEGER PRIMARY KEY. The sample column is a blob which is the ** binary encoding of a key from the index. The nEq column is a ** list of integers. The first integer is the approximate number ** of entries in the index whose left-most column exactly matches ** the left-most column of the sample. The second integer in nEq ** is the approximate number of entries in the index where the ** first two columns match the first two columns of the sample. ** And so forth. nLt is another list of integers that show the approximate ** number of entries that are strictly less than the sample. The first ** integer in nLt contains the number of entries in the index where the ** left-most column is less than the left-most column of the sample. ** The K-th integer in the nLt entry is the number of index entries ** where the first K columns are less than the first K columns of the ** sample. The nDLt column is like nLt except that it contains the ** number of distinct entries in the index that are less than the ** sample. ** ** There can be an arbitrary number of sqlite_stat4 entries per index. ** The ANALYZE command will typically generate sqlite_stat4 tables ** that contain between 10 and 40 samples which are distributed across ** the key space, though not uniformly, and which include samples with ** large nEq values. ** ** Format for sqlite_stat3 redux: ** ** The sqlite_stat3 table is like sqlite_stat4 except that it only ** looks at the left-most column of the index. The sqlite_stat3.sample ** column contains the actual value of the left-most column instead ** of a blob encoding of the complete index key as is found in ** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3 ** all contain just a single integer which is the same as the first ** integer in the equivalent columns in sqlite_stat4. */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** This routine generates code that opens the sqlite_statN tables. // ** The sqlite_stat1 table is always relevant. sqlite_stat2 is now // ** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when // ** appropriate compile-time options are provided. // ** // ** If the sqlite_statN tables do not previously exist, it is created. // ** // ** Argument zWhere may be a pointer to a buffer containing a table name, // ** or it may be a NULL pointer. If it is not NULL, then all entries in // ** the sqlite_statN tables associated with the named table are deleted. // ** If zWhere==0, then code is generated to delete all stat table entries. // */ func _openStatTable(tls *libc.TLS, pParse uintptr, iDb int32, iStatCur int32, zWhere uintptr, zWhereType uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var aCreateTbl [3]Tu8 var aRoot [3]Tu32 var db, pDb, pStat, v, zTab, v3 uintptr var i, nToOpen, v1 int32 _, _, _, _, _, _, _, _, _, _, _ = aCreateTbl, aRoot, db, i, nToOpen, pDb, pStat, v, zTab, v1, v3 db = (*TParse)(unsafe.Pointer(pParse)).Fdb v = _sqlite3GetVdbe(tls, pParse) if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) == uint32(0) { v1 = int32(2) } else { v1 = int32(1) } nToOpen = v1 if v == uintptr(0) { return } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16 /* Create new statistic tables if they do not exist, or clear them ** if they do already exist. */ i = 0 for { if !(i < int32(libc.Uint32FromInt64(24)/libc.Uint32FromInt64(8))) { break } zTab = _aTable[i].FzName aCreateTbl[i] = uint8(0) v3 = _sqlite3FindTable(tls, db, zTab, (*TDb)(unsafe.Pointer(pDb)).FzDbSName) pStat = v3 if v3 == uintptr(0) { if i < nToOpen { /* The sqlite_statN table does not exist. Create it. Note that a ** side-effect of the CREATE TABLE statement is to leave the rootpage ** of the new table in register pParse->regRoot. This is important ** because the OpenWrite opcode below will be needing it. */ _sqlite3NestedParse(tls, pParse, __ccgo_ts+11662, libc.VaList(bp+8, (*TDb)(unsafe.Pointer(pDb)).FzDbSName, zTab, _aTable[i].FzCols)) aRoot[i] = uint32((*TParse)(unsafe.Pointer(pParse)).FregRoot) aCreateTbl[i] = uint8(OPFLAG_P2ISREG) } } else { /* The table already exists. If zWhere is not NULL, delete all entries ** associated with the table zWhere. If zWhere is NULL, delete the ** entire contents of the table. */ aRoot[i] = (*TTable)(unsafe.Pointer(pStat)).Ftnum _sqlite3TableLock(tls, pParse, iDb, aRoot[i], uint8(1), zTab) if zWhere != 0 { _sqlite3NestedParse(tls, pParse, __ccgo_ts+11685, libc.VaList(bp+8, (*TDb)(unsafe.Pointer(pDb)).FzDbSName, zTab, zWhereType, zWhere)) } else { if (*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 { _sqlite3NestedParse(tls, pParse, __ccgo_ts+11715, libc.VaList(bp+8, (*TDb)(unsafe.Pointer(pDb)).FzDbSName, zTab)) } else { /* The sqlite_stat[134] table already exists. Delete all rows. */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Clear), int32(aRoot[i]), iDb) } } } goto _2 _2: ; i++ } /* Open the sqlite_stat[134] tables for writing. */ i = 0 for { if !(i < nToOpen) { break } _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_OpenWrite), iStatCur+i, int32(aRoot[i]), iDb, int32(3)) _sqlite3VdbeChangeP5(tls, v, uint16(aCreateTbl[i])) goto _4 _4: ; i++ } } var _aTable = [3]struct { FzName uintptr FzCols uintptr }{ 0: { FzName: __ccgo_ts + 11582, FzCols: __ccgo_ts + 11595, }, 1: { FzName: __ccgo_ts + 11608, FzCols: __ccgo_ts + 11621, }, 2: { FzName: __ccgo_ts + 11649, }, } /* ** Recommended number of samples for sqlite_stat4 */ // C documentation // // /* // ** Three SQL functions - stat_init(), stat_push(), and stat_get() - // ** share an instance of the following structure to hold their state // ** information. // */ type TStatAccum = struct { F__ccgo_align [0]uint32 Fdb uintptr F__ccgo_align1 [4]byte FnEst TtRowcnt FnRow TtRowcnt FnLimit int32 FnCol int32 FnKeyCol int32 FnSkipAhead Tu8 F__ccgo_align7 [3]byte Fcurrent TStatSample FnPSample TtRowcnt FmxSample int32 FiPrn Tu32 FaBest uintptr FiMin int32 FnSample int32 FnMaxEqZero int32 FiGet int32 Fa uintptr } type StatAccum = TStatAccum type TStatSample = struct { F__ccgo_align [0]uint32 FanDLt uintptr FanEq uintptr FanLt uintptr F__ccgo_align3 [4]byte Fu struct { F__ccgo_align [0]uint32 FaRowid [0]uintptr FiRowid Ti64 } FnRowid Tu32 FisPSample Tu8 FiCol int32 FiHash Tu32 } type StatSample = TStatSample type TStatSample1 = struct { F__ccgo_align [0]uint32 FanDLt uintptr FanEq uintptr FanLt uintptr F__ccgo_align3 [4]byte Fu struct { F__ccgo_align [0]uint32 FaRowid [0]uintptr FiRowid Ti64 } FnRowid Tu32 FisPSample Tu8 FiCol int32 FiHash Tu32 } type StatSample1 = TStatSample1 type TStatAccum1 = struct { F__ccgo_align [0]uint32 Fdb uintptr F__ccgo_align1 [4]byte FnEst TtRowcnt FnRow TtRowcnt FnLimit int32 FnCol int32 FnKeyCol int32 FnSkipAhead Tu8 F__ccgo_align7 [3]byte Fcurrent TStatSample FnPSample TtRowcnt FmxSample int32 FiPrn Tu32 FaBest uintptr FiMin int32 FnSample int32 FnMaxEqZero int32 FiGet int32 Fa uintptr } type StatAccum1 = TStatAccum1 // C documentation // // /* Reclaim memory used by a StatSample // */ func _sampleClear(tls *libc.TLS, db uintptr, p uintptr) { _ = libc.Int32FromInt32(0) if (*TStatSample)(unsafe.Pointer(p)).FnRowid != 0 { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(&(*TStatSample)(unsafe.Pointer(p)).Fu))) (*TStatSample)(unsafe.Pointer(p)).FnRowid = uint32(0) } } // C documentation // // /* Initialize the BLOB value of a ROWID // */ func _sampleSetRowid(tls *libc.TLS, db uintptr, p uintptr, n int32, pData uintptr) { _ = libc.Int32FromInt32(0) if (*TStatSample)(unsafe.Pointer(p)).FnRowid != 0 { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(&(*TStatSample)(unsafe.Pointer(p)).Fu))) } *(*uintptr)(unsafe.Pointer(&(*TStatSample)(unsafe.Pointer(p)).Fu)) = _sqlite3DbMallocRawNN(tls, db, uint64(n)) if *(*uintptr)(unsafe.Pointer(&(*TStatSample)(unsafe.Pointer(p)).Fu)) != 0 { (*TStatSample)(unsafe.Pointer(p)).FnRowid = uint32(n) libc.Xmemcpy(tls, *(*uintptr)(unsafe.Pointer(&(*TStatSample)(unsafe.Pointer(p)).Fu)), pData, uint32(n)) } else { (*TStatSample)(unsafe.Pointer(p)).FnRowid = uint32(0) } } // C documentation // // /* Initialize the INTEGER value of a ROWID. // */ func _sampleSetRowidInt64(tls *libc.TLS, db uintptr, p uintptr, iRowid Ti64) { _ = libc.Int32FromInt32(0) if (*TStatSample)(unsafe.Pointer(p)).FnRowid != 0 { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(&(*TStatSample)(unsafe.Pointer(p)).Fu))) } (*TStatSample)(unsafe.Pointer(p)).FnRowid = uint32(0) *(*Ti64)(unsafe.Pointer(p + 16)) = iRowid } // C documentation // // /* // ** Copy the contents of object (*pFrom) into (*pTo). // */ func _sampleCopy(tls *libc.TLS, p uintptr, pTo uintptr, pFrom uintptr) { (*TStatSample)(unsafe.Pointer(pTo)).FisPSample = (*TStatSample)(unsafe.Pointer(pFrom)).FisPSample (*TStatSample)(unsafe.Pointer(pTo)).FiCol = (*TStatSample)(unsafe.Pointer(pFrom)).FiCol (*TStatSample)(unsafe.Pointer(pTo)).FiHash = (*TStatSample)(unsafe.Pointer(pFrom)).FiHash libc.Xmemcpy(tls, (*TStatSample)(unsafe.Pointer(pTo)).FanEq, (*TStatSample)(unsafe.Pointer(pFrom)).FanEq, uint32(8)*uint32((*TStatAccum)(unsafe.Pointer(p)).FnCol)) libc.Xmemcpy(tls, (*TStatSample)(unsafe.Pointer(pTo)).FanLt, (*TStatSample)(unsafe.Pointer(pFrom)).FanLt, uint32(8)*uint32((*TStatAccum)(unsafe.Pointer(p)).FnCol)) libc.Xmemcpy(tls, (*TStatSample)(unsafe.Pointer(pTo)).FanDLt, (*TStatSample)(unsafe.Pointer(pFrom)).FanDLt, uint32(8)*uint32((*TStatAccum)(unsafe.Pointer(p)).FnCol)) if (*TStatSample)(unsafe.Pointer(pFrom)).FnRowid != 0 { _sampleSetRowid(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, pTo, int32((*TStatSample)(unsafe.Pointer(pFrom)).FnRowid), *(*uintptr)(unsafe.Pointer(&(*TStatSample)(unsafe.Pointer(pFrom)).Fu))) } else { _sampleSetRowidInt64(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, pTo, *(*Ti64)(unsafe.Pointer(pFrom + 16))) } } // C documentation // // /* // ** Reclaim all memory of a StatAccum structure. // */ func _statAccumDestructor(tls *libc.TLS, pOld uintptr) { var i int32 var p uintptr _, _ = i, p p = pOld if (*TStatAccum)(unsafe.Pointer(p)).FmxSample != 0 { i = 0 for { if !(i < (*TStatAccum)(unsafe.Pointer(p)).FnCol) { break } _sampleClear(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, (*TStatAccum)(unsafe.Pointer(p)).FaBest+uintptr(i)*40) goto _1 _1: ; i++ } i = 0 for { if !(i < (*TStatAccum)(unsafe.Pointer(p)).FmxSample) { break } _sampleClear(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, (*TStatAccum)(unsafe.Pointer(p)).Fa+uintptr(i)*40) goto _2 _2: ; i++ } _sampleClear(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, p+40) } _sqlite3DbFree(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, p) } // C documentation // // /* // ** Implementation of the stat_init(N,K,C,L) SQL function. The four parameters // ** are: // ** N: The number of columns in the index including the rowid/pk (note 1) // ** K: The number of columns in the index excluding the rowid/pk. // ** C: Estimated number of rows in the index // ** L: A limit on the number of rows to scan, or 0 for no-limit // ** // ** Note 1: In the special case of the covering index that implements a // ** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the // ** total number of columns in the table. // ** // ** For indexes on ordinary rowid tables, N==K+1. But for indexes on // ** WITHOUT ROWID tables, N=K+P where P is the number of columns in the // ** PRIMARY KEY of the table. The covering index that implements the // ** original WITHOUT ROWID table as N==K as a special case. // ** // ** This routine allocates the StatAccum object in heap memory. The return // ** value is a pointer to the StatAccum object. The datatype of the // ** return value is BLOB, but it is really just a pointer to the StatAccum // ** object. // */ func _statInit(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var db, p, pSpace uintptr var i, mxSample, n, nCol, nColUp, nKeyCol, v1, v2 int32 _, _, _, _, _, _, _, _, _, _, _ = db, i, mxSample, n, nCol, nColUp, nKeyCol, p, pSpace, v1, v2 /* Bytes of space to allocate */ db = Xsqlite3_context_db_handle(tls, context) if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) == uint32(0) { v1 = int32(SQLITE_STAT4_SAMPLES) } else { v1 = 0 } /* Database connection */ /* Maximum number of samples. 0 if STAT4 data is not collected */ mxSample = v1 /* Decode the three function arguments */ _ = argc nCol = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv))) _ = libc.Int32FromInt32(0) nColUp = nCol nKeyCol = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Allocate the space required for the StatAccum object */ n = int32(uint32(120) + uint32(8)*uint32(nColUp)) /* StatAccum.anDLt */ n = int32(uint32(n) + libc.Uint32FromInt64(8)*uint32(nColUp)) /* StatAccum.anEq */ if mxSample != 0 { n = int32(uint32(n) + (libc.Uint32FromInt64(8)*uint32(nColUp) + libc.Uint32FromInt64(40)*uint32(nCol+mxSample) + libc.Uint32FromInt64(8)*libc.Uint32FromInt32(3)*uint32(nColUp)*uint32(nCol+mxSample))) } p = _sqlite3DbMallocZero(tls, db, uint64(n)) if p == uintptr(0) { Xsqlite3_result_error_nomem(tls, context) return } (*TStatAccum)(unsafe.Pointer(p)).Fdb = db (*TStatAccum)(unsafe.Pointer(p)).FnEst = uint64(Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4)))) (*TStatAccum)(unsafe.Pointer(p)).FnRow = uint64(0) (*TStatAccum)(unsafe.Pointer(p)).FnLimit = int32(Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv + 3*4)))) (*TStatAccum)(unsafe.Pointer(p)).FnCol = nCol (*TStatAccum)(unsafe.Pointer(p)).FnKeyCol = nKeyCol (*TStatAccum)(unsafe.Pointer(p)).FnSkipAhead = uint8(0) (*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanDLt = p + 1*120 (*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq = (*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanDLt + uintptr(nColUp)*8 if (*TStatAccum)(unsafe.Pointer(p)).FnLimit == 0 { v2 = mxSample } else { v2 = 0 } (*TStatAccum)(unsafe.Pointer(p)).FmxSample = v2 if mxSample != 0 { /* Used to iterate through p->aSample[] */ (*TStatAccum)(unsafe.Pointer(p)).FiGet = -int32(1) (*TStatAccum)(unsafe.Pointer(p)).FnPSample = (*TStatAccum)(unsafe.Pointer(p)).FnEst/uint64(mxSample/libc.Int32FromInt32(3)+libc.Int32FromInt32(1)) + libc.Uint64FromInt32(1) (*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanLt = (*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq + uintptr(nColUp)*8 (*TStatAccum)(unsafe.Pointer(p)).FiPrn = uint32(0x689e962d)*uint32(nCol) ^ uint32(0xd0944565)*uint32(Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4)))) /* Set up the StatAccum.a[] and aBest[] arrays */ (*TStatAccum)(unsafe.Pointer(p)).Fa = (*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanLt + uintptr(nColUp)*8 (*TStatAccum)(unsafe.Pointer(p)).FaBest = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(mxSample)*40 pSpace = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(mxSample+nCol)*40 i = 0 for { if !(i < mxSample+nCol) { break } (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*40))).FanEq = pSpace pSpace += uintptr(libc.Uint32FromInt64(8) * uint32(nColUp)) (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*40))).FanLt = pSpace pSpace += uintptr(libc.Uint32FromInt64(8) * uint32(nColUp)) (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*40))).FanDLt = pSpace pSpace += uintptr(libc.Uint32FromInt64(8) * uint32(nColUp)) goto _3 _3: ; i++ } _ = libc.Int32FromInt32(0) i = 0 for { if !(i < nCol) { break } (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).FaBest + uintptr(i)*40))).FiCol = i goto _4 _4: ; i++ } } /* Return a pointer to the allocated object to the caller. Note that ** only the pointer (the 2nd parameter) matters. The size of the object ** (given by the 3rd parameter) is never used and can be any positive ** value. */ Xsqlite3_result_blob(tls, context, p, int32(120), __ccgo_fp(_statAccumDestructor)) } var _statInitFuncdef = TFuncDef{ FnArg: int8(4), FfuncFlags: uint32(SQLITE_UTF8), FzName: __ccgo_ts + 11733, } func init() { p := unsafe.Pointer(&_statInitFuncdef) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_statInit) } // C documentation // // /* // ** pNew and pOld are both candidate non-periodic samples selected for // ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and // ** considering only any trailing columns and the sample hash value, this // ** function returns true if sample pNew is to be preferred over pOld. // ** In other words, if we assume that the cardinalities of the selected // ** column for pNew and pOld are equal, is pNew to be preferred over pOld. // ** // ** This function assumes that for each argument sample, the contents of // ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid. // */ func _sampleIsBetterPost(tls *libc.TLS, pAccum uintptr, pNew uintptr, pOld uintptr) (r int32) { var i, nCol int32 _, _ = i, nCol nCol = (*TStatAccum)(unsafe.Pointer(pAccum)).FnCol _ = libc.Int32FromInt32(0) i = (*TStatSample)(unsafe.Pointer(pNew)).FiCol + int32(1) for { if !(i < nCol) { break } if *(*TtRowcnt)(unsafe.Pointer((*TStatSample)(unsafe.Pointer(pNew)).FanEq + uintptr(i)*8)) > *(*TtRowcnt)(unsafe.Pointer((*TStatSample)(unsafe.Pointer(pOld)).FanEq + uintptr(i)*8)) { return int32(1) } if *(*TtRowcnt)(unsafe.Pointer((*TStatSample)(unsafe.Pointer(pNew)).FanEq + uintptr(i)*8)) < *(*TtRowcnt)(unsafe.Pointer((*TStatSample)(unsafe.Pointer(pOld)).FanEq + uintptr(i)*8)) { return 0 } goto _1 _1: ; i++ } if (*TStatSample)(unsafe.Pointer(pNew)).FiHash > (*TStatSample)(unsafe.Pointer(pOld)).FiHash { return int32(1) } return 0 } // C documentation // // /* // ** Return true if pNew is to be preferred over pOld. // ** // ** This function assumes that for each argument sample, the contents of // ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid. // */ func _sampleIsBetter(tls *libc.TLS, pAccum uintptr, pNew uintptr, pOld uintptr) (r int32) { var nEqNew, nEqOld TtRowcnt _, _ = nEqNew, nEqOld nEqNew = *(*TtRowcnt)(unsafe.Pointer((*TStatSample)(unsafe.Pointer(pNew)).FanEq + uintptr((*TStatSample)(unsafe.Pointer(pNew)).FiCol)*8)) nEqOld = *(*TtRowcnt)(unsafe.Pointer((*TStatSample)(unsafe.Pointer(pOld)).FanEq + uintptr((*TStatSample)(unsafe.Pointer(pOld)).FiCol)*8)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if nEqNew > nEqOld { return int32(1) } if nEqNew == nEqOld { if (*TStatSample)(unsafe.Pointer(pNew)).FiCol < (*TStatSample)(unsafe.Pointer(pOld)).FiCol { return int32(1) } return libc.BoolInt32((*TStatSample)(unsafe.Pointer(pNew)).FiCol == (*TStatSample)(unsafe.Pointer(pOld)).FiCol && _sampleIsBetterPost(tls, pAccum, pNew, pOld) != 0) } return 0 } // C documentation // // /* // ** Copy the contents of sample *pNew into the p->a[] array. If necessary, // ** remove the least desirable sample from p->a[] to make room. // */ func _sampleInsert(tls *libc.TLS, p uintptr, pNew uintptr, nEqZero int32) { var anDLt, anEq, anLt, pMin, pOld, pSample, pUpgrade uintptr var i, iMin int32 _, _, _, _, _, _, _, _, _ = anDLt, anEq, anLt, i, iMin, pMin, pOld, pSample, pUpgrade pSample = uintptr(0) _ = libc.Int32FromInt32(0) /* StatAccum.nMaxEqZero is set to the maximum number of leading 0 ** values in the anEq[] array of any sample in StatAccum.a[]. In ** other words, if nMaxEqZero is n, then it is guaranteed that there ** are no samples with StatSample.anEq[m]==0 for (m>=n). */ if nEqZero > (*TStatAccum)(unsafe.Pointer(p)).FnMaxEqZero { (*TStatAccum)(unsafe.Pointer(p)).FnMaxEqZero = nEqZero } if int32((*TStatSample)(unsafe.Pointer(pNew)).FisPSample) == 0 { pUpgrade = uintptr(0) _ = libc.Int32FromInt32(0) /* This sample is being added because the prefix that ends in column ** iCol occurs many times in the table. However, if we have already ** added a sample that shares this prefix, there is no need to add ** this one. Instead, upgrade the priority of the highest priority ** existing sample that shares this prefix. */ i = (*TStatAccum)(unsafe.Pointer(p)).FnSample - int32(1) for { if !(i >= 0) { break } pOld = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*40 if *(*TtRowcnt)(unsafe.Pointer((*TStatSample)(unsafe.Pointer(pOld)).FanEq + uintptr((*TStatSample)(unsafe.Pointer(pNew)).FiCol)*8)) == uint64(0) { if (*TStatSample)(unsafe.Pointer(pOld)).FisPSample != 0 { return } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pUpgrade == uintptr(0) || _sampleIsBetter(tls, p, pOld, pUpgrade) != 0 { pUpgrade = pOld } } goto _1 _1: ; i-- } if pUpgrade != 0 { (*TStatSample)(unsafe.Pointer(pUpgrade)).FiCol = (*TStatSample)(unsafe.Pointer(pNew)).FiCol *(*TtRowcnt)(unsafe.Pointer((*TStatSample)(unsafe.Pointer(pUpgrade)).FanEq + uintptr((*TStatSample)(unsafe.Pointer(pUpgrade)).FiCol)*8)) = *(*TtRowcnt)(unsafe.Pointer((*TStatSample)(unsafe.Pointer(pNew)).FanEq + uintptr((*TStatSample)(unsafe.Pointer(pUpgrade)).FiCol)*8)) goto find_new_min } } /* If necessary, remove sample iMin to make room for the new sample. */ if (*TStatAccum)(unsafe.Pointer(p)).FnSample >= (*TStatAccum)(unsafe.Pointer(p)).FmxSample { pMin = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FiMin)*40 anEq = (*TStatSample)(unsafe.Pointer(pMin)).FanEq anLt = (*TStatSample)(unsafe.Pointer(pMin)).FanLt anDLt = (*TStatSample)(unsafe.Pointer(pMin)).FanDLt _sampleClear(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, pMin) libc.Xmemmove(tls, pMin, pMin+1*40, uint32(40)*uint32((*TStatAccum)(unsafe.Pointer(p)).FnSample-(*TStatAccum)(unsafe.Pointer(p)).FiMin-libc.Int32FromInt32(1))) pSample = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FnSample-int32(1))*40 (*TStatSample)(unsafe.Pointer(pSample)).FnRowid = uint32(0) (*TStatSample)(unsafe.Pointer(pSample)).FanEq = anEq (*TStatSample)(unsafe.Pointer(pSample)).FanDLt = anDLt (*TStatSample)(unsafe.Pointer(pSample)).FanLt = anLt (*TStatAccum)(unsafe.Pointer(p)).FnSample = (*TStatAccum)(unsafe.Pointer(p)).FmxSample - int32(1) } /* The "rows less-than" for the rowid column must be greater than that ** for the last sample in the p->a[] array. Otherwise, the samples would ** be out of order. */ _ = libc.Int32FromInt32(0) /* Insert the new sample */ pSample = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FnSample)*40 _sampleCopy(tls, p, pSample, pNew) (*TStatAccum)(unsafe.Pointer(p)).FnSample++ /* Zero the first nEqZero entries in the anEq[] array. */ libc.Xmemset(tls, (*TStatSample)(unsafe.Pointer(pSample)).FanEq, 0, uint32(8)*uint32(nEqZero)) goto find_new_min find_new_min: ; if (*TStatAccum)(unsafe.Pointer(p)).FnSample >= (*TStatAccum)(unsafe.Pointer(p)).FmxSample { iMin = -int32(1) i = 0 for { if !(i < (*TStatAccum)(unsafe.Pointer(p)).FmxSample) { break } if (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*40))).FisPSample != 0 { goto _2 } if iMin < 0 || _sampleIsBetter(tls, p, (*TStatAccum)(unsafe.Pointer(p)).Fa+uintptr(iMin)*40, (*TStatAccum)(unsafe.Pointer(p)).Fa+uintptr(i)*40) != 0 { iMin = i } goto _2 _2: ; i++ } _ = libc.Int32FromInt32(0) (*TStatAccum)(unsafe.Pointer(p)).FiMin = iMin } } // C documentation // // /* // ** Field iChng of the index being scanned has changed. So at this point // ** p->current contains a sample that reflects the previous row of the // ** index. The value of anEq[iChng] and subsequent anEq[] elements are // ** correct at this point. // */ func _samplePushPrevious(tls *libc.TLS, p uintptr, iChng int32) { var i, j, j1 int32 var pBest uintptr _, _, _, _ = i, j, j1, pBest /* Check if any samples from the aBest[] array should be pushed ** into IndexSample.a[] at this point. */ i = (*TStatAccum)(unsafe.Pointer(p)).FnCol - int32(2) for { if !(i >= iChng) { break } pBest = (*TStatAccum)(unsafe.Pointer(p)).FaBest + uintptr(i)*40 *(*TtRowcnt)(unsafe.Pointer((*TStatSample)(unsafe.Pointer(pBest)).FanEq + uintptr(i)*8)) = *(*TtRowcnt)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq + uintptr(i)*8)) if (*TStatAccum)(unsafe.Pointer(p)).FnSample < (*TStatAccum)(unsafe.Pointer(p)).FmxSample || _sampleIsBetter(tls, p, pBest, (*TStatAccum)(unsafe.Pointer(p)).Fa+uintptr((*TStatAccum)(unsafe.Pointer(p)).FiMin)*40) != 0 { _sampleInsert(tls, p, pBest, i) } goto _1 _1: ; i-- } /* Check that no sample contains an anEq[] entry with an index of ** p->nMaxEqZero or greater set to zero. */ i = (*TStatAccum)(unsafe.Pointer(p)).FnSample - int32(1) for { if !(i >= 0) { break } j = (*TStatAccum)(unsafe.Pointer(p)).FnMaxEqZero for { if !(j < (*TStatAccum)(unsafe.Pointer(p)).FnCol) { break } _ = libc.Int32FromInt32(0) goto _3 _3: ; j++ } goto _2 _2: ; i-- } /* Update the anEq[] fields of any samples already collected. */ if iChng < (*TStatAccum)(unsafe.Pointer(p)).FnMaxEqZero { i = (*TStatAccum)(unsafe.Pointer(p)).FnSample - int32(1) for { if !(i >= 0) { break } j1 = iChng for { if !(j1 < (*TStatAccum)(unsafe.Pointer(p)).FnCol) { break } if *(*TtRowcnt)(unsafe.Pointer((*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*40))).FanEq + uintptr(j1)*8)) == uint64(0) { *(*TtRowcnt)(unsafe.Pointer((*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*40))).FanEq + uintptr(j1)*8)) = *(*TtRowcnt)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq + uintptr(j1)*8)) } goto _5 _5: ; j1++ } goto _4 _4: ; i-- } (*TStatAccum)(unsafe.Pointer(p)).FnMaxEqZero = iChng } } // C documentation // // /* // ** Implementation of the stat_push SQL function: stat_push(P,C,R) // ** Arguments: // ** // ** P Pointer to the StatAccum object created by stat_init() // ** C Index of left-most column to differ from previous row // ** R Rowid for the current row. Might be a key record for // ** WITHOUT ROWID tables. // ** // ** The purpose of this routine is to collect statistical data and/or // ** samples from the index being analyzed into the StatAccum object. // ** The stat_get() SQL function will be used afterwards to // ** retrieve the information gathered. // ** // ** This SQL function usually returns NULL, but might return an integer // ** if it wants the byte-code to do special processing. // ** // ** The R parameter is only used for STAT4 // */ func _statPush(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var i, iChng int32 var nLt TtRowcnt var p uintptr var v4 Tu32 _, _, _, _, _ = i, iChng, nLt, p, v4 /* The three function arguments */ p = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv))) iChng = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) _ = argc _ = context _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TStatAccum)(unsafe.Pointer(p)).FnRow == uint64(0) { /* This is the first call to this function. Do initialization. */ i = 0 for { if !(i < (*TStatAccum)(unsafe.Pointer(p)).FnCol) { break } *(*TtRowcnt)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq + uintptr(i)*8)) = uint64(1) goto _1 _1: ; i++ } } else { /* Second and subsequent calls get processed here */ if (*TStatAccum)(unsafe.Pointer(p)).FmxSample != 0 { _samplePushPrevious(tls, p, iChng) } /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply ** to the current row of the index. */ i = 0 for { if !(i < iChng) { break } *(*TtRowcnt)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq + uintptr(i)*8))++ goto _2 _2: ; i++ } i = iChng for { if !(i < (*TStatAccum)(unsafe.Pointer(p)).FnCol) { break } *(*TtRowcnt)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanDLt + uintptr(i)*8))++ if (*TStatAccum)(unsafe.Pointer(p)).FmxSample != 0 { *(*TtRowcnt)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanLt + uintptr(i)*8)) += *(*TtRowcnt)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq + uintptr(i)*8)) } *(*TtRowcnt)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq + uintptr(i)*8)) = uint64(1) goto _3 _3: ; i++ } } (*TStatAccum)(unsafe.Pointer(p)).FnRow++ if (*TStatAccum)(unsafe.Pointer(p)).FmxSample != 0 { if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4))) == int32(SQLITE_INTEGER) { _sampleSetRowidInt64(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, p+40, Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4)))) } else { _sampleSetRowid(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, p+40, Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4))), Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4)))) } v4 = (*TStatAccum)(unsafe.Pointer(p)).FiPrn*libc.Uint32FromInt32(1103515245) + libc.Uint32FromInt32(12345) (*TStatAccum)(unsafe.Pointer(p)).FiPrn = v4 (*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FiHash = v4 nLt = *(*TtRowcnt)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanLt + uintptr((*TStatAccum)(unsafe.Pointer(p)).FnCol-int32(1))*8)) /* Check if this is to be a periodic sample. If so, add it. */ if nLt/(*TStatAccum)(unsafe.Pointer(p)).FnPSample != (nLt+uint64(1))/(*TStatAccum)(unsafe.Pointer(p)).FnPSample { (*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FisPSample = uint8(1) (*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FiCol = 0 _sampleInsert(tls, p, p+40, (*TStatAccum)(unsafe.Pointer(p)).FnCol-int32(1)) (*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FisPSample = uint8(0) } /* Update the aBest[] array. */ i = 0 for { if !(i < (*TStatAccum)(unsafe.Pointer(p)).FnCol-int32(1)) { break } (*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FiCol = i if i >= iChng || _sampleIsBetterPost(tls, p, p+40, (*TStatAccum)(unsafe.Pointer(p)).FaBest+uintptr(i)*40) != 0 { _sampleCopy(tls, p, (*TStatAccum)(unsafe.Pointer(p)).FaBest+uintptr(i)*40, p+40) } goto _5 _5: ; i++ } } else { if (*TStatAccum)(unsafe.Pointer(p)).FnLimit != 0 && (*TStatAccum)(unsafe.Pointer(p)).FnRow > uint64((*TStatAccum)(unsafe.Pointer(p)).FnLimit)*uint64(int32((*TStatAccum)(unsafe.Pointer(p)).FnSkipAhead)+libc.Int32FromInt32(1)) { (*TStatAccum)(unsafe.Pointer(p)).FnSkipAhead++ Xsqlite3_result_int(tls, context, libc.BoolInt32(*(*TtRowcnt)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanDLt)) > uint64(0))) } } } var _statPushFuncdef = TFuncDef{ FnArg: int8(libc.Int32FromInt32(2) + libc.Int32FromInt32(IsStat4)), FfuncFlags: uint32(SQLITE_UTF8), FzName: __ccgo_ts + 11743, } func init() { p := unsafe.Pointer(&_statPushFuncdef) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_statPush) } // C documentation // // /* // ** Implementation of the stat_get(P,J) SQL function. This routine is // ** used to query statistical information that has been gathered into // ** the StatAccum object by prior calls to stat_push(). The P parameter // ** has type BLOB but it is really just a pointer to the StatAccum object. // ** The content to returned is determined by the parameter J // ** which is one of the STAT_GET_xxxx values defined above. // ** // ** The stat_get(P,J) function is not available to generic SQL. It is // ** inserted as part of a manually constructed bytecode program. (See // ** the callStatGet() routine below.) It is guaranteed that the P // ** parameter will always be a pointer to a StatAccum object, never a // ** NULL. // ** // ** If STAT4 is not enabled, then J is always // ** STAT_GET_STAT1 and is hence omitted and this routine becomes // ** a one-parameter function, stat_get(P), that always returns the // ** stat1 table entry information. // */ func _statGet(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var aCnt, p, pS uintptr var eCall, i, i1 int32 var iVal, nDistinct Tu64 var v1 uint64 var _ /* sStat at bp+0 */ Tsqlite3_str var _ /* sStat at bp+24 */ Tsqlite3_str _, _, _, _, _, _, _, _, _ = aCnt, eCall, i, i1, iVal, nDistinct, p, pS, v1 p = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv))) /* STAT4 has a parameter on this routine. */ eCall = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if eCall == STAT_GET_STAT1 { /* Loop counter */ _sqlite3StrAccumInit(tls, bp, uintptr(0), uintptr(0), 0, ((*TStatAccum)(unsafe.Pointer(p)).FnKeyCol+int32(1))*int32(100)) if (*TStatAccum)(unsafe.Pointer(p)).FnSkipAhead != 0 { v1 = (*TStatAccum)(unsafe.Pointer(p)).FnEst } else { v1 = (*TStatAccum)(unsafe.Pointer(p)).FnRow } Xsqlite3_str_appendf(tls, bp, __ccgo_ts+11753, libc.VaList(bp+56, v1)) i = 0 for { if !(i < (*TStatAccum)(unsafe.Pointer(p)).FnKeyCol) { break } nDistinct = *(*TtRowcnt)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanDLt + uintptr(i)*8)) + uint64(1) iVal = ((*TStatAccum)(unsafe.Pointer(p)).FnRow + nDistinct - uint64(1)) / nDistinct if iVal == uint64(2) && (*TStatAccum)(unsafe.Pointer(p)).FnRow*uint64(10) <= nDistinct*uint64(11) { iVal = uint64(1) } Xsqlite3_str_appendf(tls, bp, __ccgo_ts+11758, libc.VaList(bp+56, iVal)) _ = libc.Int32FromInt32(0) goto _2 _2: ; i++ } _sqlite3ResultStrAccum(tls, context, bp) } else { if eCall == int32(STAT_GET_ROWID) { if (*TStatAccum)(unsafe.Pointer(p)).FiGet < 0 { _samplePushPrevious(tls, p, 0) (*TStatAccum)(unsafe.Pointer(p)).FiGet = 0 } if (*TStatAccum)(unsafe.Pointer(p)).FiGet < (*TStatAccum)(unsafe.Pointer(p)).FnSample { pS = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FiGet)*40 if (*TStatSample)(unsafe.Pointer(pS)).FnRowid == uint32(0) { Xsqlite3_result_int64(tls, context, *(*Ti64)(unsafe.Pointer(pS + 16))) } else { Xsqlite3_result_blob(tls, context, *(*uintptr)(unsafe.Pointer(&(*TStatSample)(unsafe.Pointer(pS)).Fu)), int32((*TStatSample)(unsafe.Pointer(pS)).FnRowid), uintptr(-libc.Int32FromInt32(1))) } } } else { aCnt = uintptr(0) _ = libc.Int32FromInt32(0) switch eCall { case int32(STAT_GET_NEQ): aCnt = (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FiGet)*40))).FanEq case int32(STAT_GET_NLT): aCnt = (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FiGet)*40))).FanLt default: aCnt = (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FiGet)*40))).FanDLt (*TStatAccum)(unsafe.Pointer(p)).FiGet++ break } _sqlite3StrAccumInit(tls, bp+24, uintptr(0), uintptr(0), 0, (*TStatAccum)(unsafe.Pointer(p)).FnCol*int32(100)) i1 = 0 for { if !(i1 < (*TStatAccum)(unsafe.Pointer(p)).FnCol) { break } Xsqlite3_str_appendf(tls, bp+24, __ccgo_ts+11764, libc.VaList(bp+56, *(*TtRowcnt)(unsafe.Pointer(aCnt + uintptr(i1)*8)))) goto _3 _3: ; i1++ } if (*(*Tsqlite3_str)(unsafe.Pointer(bp + 24))).FnChar != 0 { (*(*Tsqlite3_str)(unsafe.Pointer(bp + 24))).FnChar-- } _sqlite3ResultStrAccum(tls, context, bp+24) } } _ = argc } var _statGetFuncdef = TFuncDef{ FnArg: int8(libc.Int32FromInt32(1) + libc.Int32FromInt32(IsStat4)), FfuncFlags: uint32(SQLITE_UTF8), FzName: __ccgo_ts + 11770, } func init() { p := unsafe.Pointer(&_statGetFuncdef) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_statGet) } func _callStatGet(tls *libc.TLS, pParse uintptr, regStat int32, iParam int32, regOut int32) { _sqlite3VdbeAddOp2(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(OP_Integer), iParam, regStat+int32(1)) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddFunctionCall(tls, pParse, 0, regStat, regOut, libc.Int32FromInt32(1)+libc.Int32FromInt32(IsStat4), uintptr(unsafe.Pointer(&_statGetFuncdef)), 0) } // C documentation // // /* // ** Generate code to do an analysis of all indices associated with // ** a single table. // */ func _analyzeOneTable(tls *libc.TLS, pParse uintptr, pTab uintptr, pOnlyIdx uintptr, iStatCur int32, iMem int32, iTab int32) { var aGotoChng, db, pColl, pIdx, pPk, pStat1, pX, v, zIdxName uintptr var addrIsNull, addrNext, addrNextRow, addrRewind, doOnce, endDistinctTest, i, iDb, iIdxCur, iTabCur, j, j1, j2, j3, jZeroRows, k, mxCol, nCol, nColTest, nColX, regChng, regCol, regDLt, regEq, regIdxname, regKey, regLt, regNewRowid, regPrev, regRowid, regSample, regSampleRowid, regStat, regStat1, regTabname, regTemp, regTemp2, v1, v10, v11, v12, v14, v18, v2, v3, v4, v5, v6, v7, v8, v9 int32 var needTableCnt, seekOp Tu8 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aGotoChng, addrIsNull, addrNext, addrNextRow, addrRewind, db, doOnce, endDistinctTest, i, iDb, iIdxCur, iTabCur, j, j1, j2, j3, jZeroRows, k, mxCol, nCol, nColTest, nColX, needTableCnt, pColl, pIdx, pPk, pStat1, pX, regChng, regCol, regDLt, regEq, regIdxname, regKey, regLt, regNewRowid, regPrev, regRowid, regSample, regSampleRowid, regStat, regStat1, regTabname, regTemp, regTemp2, seekOp, v, zIdxName, v1, v10, v11, v12, v14, v18, v2, v3, v4, v5, v6, v7, v8, v9 db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Loop counter */ jZeroRows = -int32(1) /* Index of database containing pTab */ needTableCnt = uint8(1) v1 = iMem iMem++ /* True to count the table */ regNewRowid = v1 v2 = iMem iMem++ /* Rowid for the inserted record */ regStat = v2 v3 = iMem iMem++ /* Register to hold StatAccum object */ regChng = v3 v4 = iMem iMem++ /* Index of changed index field */ regRowid = v4 v5 = iMem iMem++ /* Rowid argument passed to stat_push() */ regTemp = v5 v6 = iMem iMem++ /* Temporary use register */ regTemp2 = v6 v7 = iMem iMem++ /* Second temporary use register */ regTabname = v7 v8 = iMem iMem++ /* Register containing table name */ regIdxname = v8 v9 = iMem iMem++ /* Register containing index name */ regStat1 = v9 /* Value for the stat column of sqlite_stat1 */ regPrev = iMem /* MUST BE LAST (see below) */ doOnce = int32(1) /* Flag for a one-time computation */ pStat1 = uintptr(0) _sqlite3TouchRegister(tls, pParse, iMem) _ = libc.Int32FromInt32(0) v = _sqlite3GetVdbe(tls, pParse) if v == uintptr(0) || pTab == uintptr(0) { return } if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) { /* Do not gather statistics on views or virtual tables */ return } if Xsqlite3_strlike(tls, __ccgo_ts+11779, (*TTable)(unsafe.Pointer(pTab)).FzName, uint32('\\')) == 0 { /* Do not gather statistics on system tables */ return } _ = libc.Int32FromInt32(0) iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_ANALYZE), (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0), (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName) != 0 { return } if (*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 { pStat1 = _sqlite3DbMallocZero(tls, db, uint64(libc.Uint32FromInt64(64)+libc.Uint32FromInt32(13))) if pStat1 == uintptr(0) { return } (*TTable)(unsafe.Pointer(pStat1)).FzName = pStat1 + 1*64 libc.Xmemcpy(tls, (*TTable)(unsafe.Pointer(pStat1)).FzName, __ccgo_ts+11582, uint32(13)) (*TTable)(unsafe.Pointer(pStat1)).FnCol = int16(3) (*TTable)(unsafe.Pointer(pStat1)).FiPKey = int16(-int32(1)) _sqlite3VdbeAddOp4(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(OP_Noop), 0, 0, 0, pStat1, -int32(6)) } /* Establish a read-lock on the table at the shared-cache level. ** Open a read-only cursor on the table. Also allocate a cursor number ** to use for scanning indexes (iIdxCur). No index cursor is opened at ** this time though. */ _sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pTab)).FzName) v10 = iTab iTab++ iTabCur = v10 v11 = iTab iTab++ iIdxCur = v11 if (*TParse)(unsafe.Pointer(pParse)).FnTab > iTab { v12 = (*TParse)(unsafe.Pointer(pParse)).FnTab } else { v12 = iTab } (*TParse)(unsafe.Pointer(pParse)).FnTab = v12 _sqlite3OpenTable(tls, pParse, iTabCur, iDb, pTab, int32(OP_OpenRead)) _sqlite3VdbeLoadString(tls, v, regTabname, (*TTable)(unsafe.Pointer(pTab)).FzName) pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0) { break } /* Number of columns to test for changes */ if pOnlyIdx != 0 && pOnlyIdx != pIdx { goto _13 } if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere == uintptr(0) { needTableCnt = uint8(0) } if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) { nCol = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) zIdxName = (*TTable)(unsafe.Pointer(pTab)).FzName nColTest = nCol - int32(1) } else { nCol = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) zIdxName = (*TIndex)(unsafe.Pointer(pIdx)).FzName if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x8>>3)) != 0 { v14 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) - int32(1) } else { v14 = nCol - int32(1) } nColTest = v14 } /* Populate the register containing the index name. */ _sqlite3VdbeLoadString(tls, v, regIdxname, zIdxName) /* ** Pseudo-code for loop that calls stat_push(): ** ** Rewind csr ** if eof(csr) goto end_of_scan; ** regChng = 0 ** goto chng_addr_0; ** ** next_row: ** regChng = 0 ** if( idx(0) != regPrev(0) ) goto chng_addr_0 ** regChng = 1 ** if( idx(1) != regPrev(1) ) goto chng_addr_1 ** ... ** regChng = N ** goto chng_addr_N ** ** chng_addr_0: ** regPrev(0) = idx(0) ** chng_addr_1: ** regPrev(1) = idx(1) ** ... ** ** endDistinctTest: ** regRowid = idx(rowid) ** stat_push(P, regChng, regRowid) ** Next csr ** if !eof(csr) goto next_row; ** ** end_of_scan: */ /* Make sure there are enough memory cells allocated to accommodate ** the regPrev array and a trailing rowid (the rowid slot is required ** when building a record to insert into the sample column of ** the sqlite_stat4 table. */ _sqlite3TouchRegister(tls, pParse, regPrev+nColTest) /* Open a read-only cursor on the index being analyzed. */ _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, int32(OP_OpenRead), iIdxCur, int32((*TIndex)(unsafe.Pointer(pIdx)).Ftnum), iDb) _sqlite3VdbeSetP4KeyInfo(tls, pParse, pIdx) /* Invoke the stat_init() function. The arguments are: ** ** (1) the number of columns in the index including the rowid ** (or for a WITHOUT ROWID table, the number of PK columns), ** (2) the number of columns in the key without the rowid/pk ** (3) estimated number of rows in the index, */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), nCol, regStat+int32(1)) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol), regRowid) if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) == uint32(0) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Count), iIdxCur, regTemp) addrRewind = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), iIdxCur) } else { addrRewind = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), iIdxCur) _sqlite3VdbeAddOp3(tls, v, int32(OP_Count), iIdxCur, regTemp, int32(1)) } _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit, regTemp2) _sqlite3VdbeAddFunctionCall(tls, pParse, 0, regStat+int32(1), regStat, int32(4), uintptr(unsafe.Pointer(&_statInitFuncdef)), 0) /* Implementation of the following: ** ** Rewind csr ** if eof(csr) goto end_of_scan; ** regChng = 0 ** goto next_push_0; ** */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regChng) addrNextRow = _sqlite3VdbeCurrentAddr(tls, v) if nColTest > 0 { endDistinctTest = _sqlite3VdbeMakeLabel(tls, pParse) /* Array of jump instruction addresses */ aGotoChng = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(4)*uint32(nColTest))) if aGotoChng == uintptr(0) { goto _13 } /* ** next_row: ** regChng = 0 ** if( idx(0) != regPrev(0) ) goto chng_addr_0 ** regChng = 1 ** if( idx(1) != regPrev(1) ) goto chng_addr_1 ** ... ** regChng = N ** goto endDistinctTest */ _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto)) addrNextRow = _sqlite3VdbeCurrentAddr(tls, v) if nColTest == int32(1) && int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) == int32(1) && int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != OE_None { /* For a single-column UNIQUE index, once we have found a non-NULL ** row, we know that all the rest will be distinct, so skip ** subsequent distinctness tests. */ _sqlite3VdbeAddOp2(tls, v, int32(OP_NotNull), regPrev, endDistinctTest) } i = 0 for { if !(i < nColTest) { break } pColl = _sqlite3LocateCollSeq(tls, pParse, *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(i)*4))) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), i, regChng) _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iIdxCur, i, regTemp) *(*int32)(unsafe.Pointer(aGotoChng + uintptr(i)*4)) = _sqlite3VdbeAddOp4(tls, v, int32(OP_Ne), regTemp, 0, regPrev+i, pColl, -int32(2)) _sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_NULLEQ)) goto _15 _15: ; i++ } _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), nColTest, regChng) _sqlite3VdbeGoto(tls, v, endDistinctTest) /* ** chng_addr_0: ** regPrev(0) = idx(0) ** chng_addr_1: ** regPrev(1) = idx(1) ** ... */ _sqlite3VdbeJumpHere(tls, v, addrNextRow-int32(1)) i = 0 for { if !(i < nColTest) { break } _sqlite3VdbeJumpHere(tls, v, *(*int32)(unsafe.Pointer(aGotoChng + uintptr(i)*4))) _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iIdxCur, i, regPrev+i) goto _16 _16: ; i++ } _sqlite3VdbeResolveLabel(tls, v, endDistinctTest) _sqlite3DbFree(tls, db, aGotoChng) } /* ** chng_addr_N: ** regRowid = idx(rowid) // STAT4 only ** stat_push(P, regChng, regRowid) // 3rd parameter STAT4 only ** Next csr ** if !eof(csr) goto next_row; */ if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) == uint32(0) { _ = libc.Int32FromInt32(0) if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { _sqlite3VdbeAddOp2(tls, v, int32(OP_IdxRowid), iIdxCur, regRowid) } else { pPk = _sqlite3PrimaryKeyIndex(tls, (*TIndex)(unsafe.Pointer(pIdx)).FpTable) regKey = _sqlite3GetTempRange(tls, pParse, int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) j = 0 for { if !(j < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) { break } k = int32(_sqlite3TableColumnToIndex(tls, pIdx, *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(j)*2)))) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iIdxCur, k, regKey+j) goto _17 _17: ; j++ } _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regKey, int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol), regRowid) _sqlite3ReleaseTempRange(tls, pParse, regKey, int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) } } _ = libc.Int32FromInt32(0) _sqlite3VdbeAddFunctionCall(tls, pParse, int32(1), regStat, regTemp, libc.Int32FromInt32(2)+libc.Int32FromInt32(IsStat4), uintptr(unsafe.Pointer(&_statPushFuncdef)), 0) if (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit != 0 { j1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), regTemp) j2 = _sqlite3VdbeAddOp1(tls, v, int32(OP_If), regTemp) j3 = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_SeekGT), iIdxCur, 0, regPrev, int32(1)) _sqlite3VdbeJumpHere(tls, v, j1) _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iIdxCur, addrNextRow) _sqlite3VdbeJumpHere(tls, v, j2) _sqlite3VdbeJumpHere(tls, v, j3) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iIdxCur, addrNextRow) } /* Add the entry to the stat1 table. */ _callStatGet(tls, pParse, regStat, STAT_GET_STAT1, regStat1) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), regTabname, int32(3), regTemp, __ccgo_ts+11789, 0) _sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), iStatCur, regNewRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iStatCur, regTemp, regNewRowid) _sqlite3VdbeChangeP4(tls, v, -int32(1), pStat1, -int32(5)) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_APPEND)) /* Add the entries to the stat4 table. */ if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) == uint32(0) && (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit == 0 { regEq = regStat1 regLt = regStat1 + int32(1) regDLt = regStat1 + int32(2) regSample = regStat1 + int32(3) regCol = regStat1 + int32(4) regSampleRowid = regCol + nCol if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { v18 = int32(OP_NotExists) } else { v18 = int32(OP_NotFound) } seekOp = uint8(v18) if doOnce != 0 { mxCol = nCol /* Compute the maximum number of columns in any index */ pX = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pX != 0) { break } /* Number of columns in pX */ if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pX + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) { nColX = int32((*TIndex)(unsafe.Pointer(pX)).FnKeyCol) } else { nColX = int32((*TIndex)(unsafe.Pointer(pX)).FnColumn) } if nColX > mxCol { mxCol = nColX } goto _19 _19: ; pX = (*TIndex)(unsafe.Pointer(pX)).FpNext } /* Allocate space to compute results for the largest index */ _sqlite3TouchRegister(tls, pParse, regCol+mxCol) doOnce = 0 _sqlite3ClearTempRegCache(tls, pParse) /* tag-20230325-1 */ _ = libc.Int32FromInt32(0) } _ = libc.Int32FromInt32(0) addrNext = _sqlite3VdbeCurrentAddr(tls, v) _callStatGet(tls, pParse, regStat, int32(STAT_GET_ROWID), regSampleRowid) addrIsNull = _sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), regSampleRowid) _callStatGet(tls, pParse, regStat, int32(STAT_GET_NEQ), regEq) _callStatGet(tls, pParse, regStat, int32(STAT_GET_NLT), regLt) _callStatGet(tls, pParse, regStat, int32(STAT_GET_NDLT), regDLt) _sqlite3VdbeAddOp4Int(tls, v, int32(seekOp), iTabCur, addrNext, regSampleRowid, 0) i = 0 for { if !(i < nCol) { break } _sqlite3ExprCodeLoadIndexColumn(tls, pParse, pIdx, iTabCur, i, regCol+i) goto _20 _20: ; i++ } _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regCol, nCol, regSample) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regTabname, int32(6), regTemp) _sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), iStatCur+int32(1), regNewRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iStatCur+int32(1), regTemp, regNewRowid) _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), int32(1), addrNext) /* P1==1 for end-of-loop */ _sqlite3VdbeJumpHere(tls, v, addrIsNull) } /* End of analysis */ _sqlite3VdbeJumpHere(tls, v, addrRewind) goto _13 _13: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } /* Create a single sqlite_stat1 entry containing NULL as the index ** name and the row count as the content. */ if pOnlyIdx == uintptr(0) && needTableCnt != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Count), iTabCur, regStat1) jZeroRows = _sqlite3VdbeAddOp1(tls, v, int32(OP_IfNot), regStat1) _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regIdxname) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), regTabname, int32(3), regTemp, __ccgo_ts+11789, 0) _sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), iStatCur, regNewRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iStatCur, regTemp, regNewRowid) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_APPEND)) _sqlite3VdbeChangeP4(tls, v, -int32(1), pStat1, -int32(5)) _sqlite3VdbeJumpHere(tls, v, jZeroRows) } } // C documentation // // /* // ** Generate code that will cause the most recent index analysis to // ** be loaded into internal hash tables where is can be used. // */ func _loadAnalysis(tls *libc.TLS, pParse uintptr, iDb int32) { var v uintptr _ = v v = _sqlite3GetVdbe(tls, pParse) if v != 0 { _sqlite3VdbeAddOp1(tls, v, int32(OP_LoadAnalysis), iDb) } } // C documentation // // /* // ** Generate code that will do an analysis of an entire database // */ func _analyzeDatabase(tls *libc.TLS, pParse uintptr, iDb int32) { var db, k, pSchema, pTab uintptr var iMem, iStatCur, iTab int32 _, _, _, _, _, _, _ = db, iMem, iStatCur, iTab, k, pSchema, pTab db = (*TParse)(unsafe.Pointer(pParse)).Fdb pSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema _sqlite3BeginWriteOperation(tls, pParse, 0, iDb) iStatCur = (*TParse)(unsafe.Pointer(pParse)).FnTab *(*int32)(unsafe.Pointer(pParse + 40)) += int32(3) _openStatTable(tls, pParse, iDb, iStatCur, uintptr(0), uintptr(0)) iMem = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) iTab = (*TParse)(unsafe.Pointer(pParse)).FnTab _ = libc.Int32FromInt32(0) k = (*THash)(unsafe.Pointer(pSchema + 8)).Ffirst for { if !(k != 0) { break } pTab = (*THashElem)(unsafe.Pointer(k)).Fdata _analyzeOneTable(tls, pParse, pTab, uintptr(0), iStatCur, iMem, iTab) iMem = _sqlite3FirstAvailableRegister(tls, pParse, iMem) goto _1 _1: ; k = (*THashElem)(unsafe.Pointer(k)).Fnext } _loadAnalysis(tls, pParse, iDb) } // C documentation // // /* // ** Generate code that will do an analysis of a single table in // ** a database. If pOnlyIdx is not NULL then it is a single index // ** in pTab that should be analyzed. // */ func _analyzeTable(tls *libc.TLS, pParse uintptr, pTab uintptr, pOnlyIdx uintptr) { var iDb, iStatCur int32 _, _ = iDb, iStatCur _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTable)(unsafe.Pointer(pTab)).FpSchema) _sqlite3BeginWriteOperation(tls, pParse, 0, iDb) iStatCur = (*TParse)(unsafe.Pointer(pParse)).FnTab *(*int32)(unsafe.Pointer(pParse + 40)) += int32(3) if pOnlyIdx != 0 { _openStatTable(tls, pParse, iDb, iStatCur, (*TIndex)(unsafe.Pointer(pOnlyIdx)).FzName, __ccgo_ts+11793) } else { _openStatTable(tls, pParse, iDb, iStatCur, (*TTable)(unsafe.Pointer(pTab)).FzName, __ccgo_ts+11797) } _analyzeOneTable(tls, pParse, pTab, pOnlyIdx, iStatCur, (*TParse)(unsafe.Pointer(pParse)).FnMem+int32(1), (*TParse)(unsafe.Pointer(pParse)).FnTab) _loadAnalysis(tls, pParse, iDb) } // C documentation // // /* // ** Generate code for the ANALYZE command. The parser calls this routine // ** when it recognizes an ANALYZE command. // ** // ** ANALYZE -- 1 // ** ANALYZE -- 2 // ** ANALYZE ?.? -- 3 // ** // ** Form 1 causes all indices in all attached databases to be analyzed. // ** Form 2 analyzes all indices the single database named. // ** Form 3 analyzes all indices associated with the named table. // */ func _sqlite3Analyze(tls *libc.TLS, pParse uintptr, pName1 uintptr, pName2 uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, pIdx, pTab, v, z, zDb, v4, v5, v6, v7 uintptr var i, iDb, v2 int32 var v3, v8 bool var _ /* pTableName at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, i, iDb, pIdx, pTab, v, z, zDb, v2, v3, v4, v5, v6, v7, v8 db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Read the database schema. If an error occurs, leave an error message ** and code in pParse and return NULL. */ _ = libc.Int32FromInt32(0) if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) { return } _ = libc.Int32FromInt32(0) if pName1 == uintptr(0) { /* Form 1: Analyze everything */ i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } if i == int32(1) { goto _1 } /* Do not analyze the TEMP database */ _analyzeDatabase(tls, pParse, i) goto _1 _1: ; i++ } } else { if v3 = (*TToken)(unsafe.Pointer(pName2)).Fn == uint32(0); v3 { v2 = _sqlite3FindDb(tls, db, pName1) iDb = v2 } if v3 && v2 >= 0 { /* Analyze the schema named as the argument */ _analyzeDatabase(tls, pParse, iDb) } else { /* Form 3: Analyze the table or index named as an argument */ iDb = _sqlite3TwoPartName(tls, pParse, pName1, pName2, bp) if iDb >= 0 { if (*TToken)(unsafe.Pointer(pName2)).Fn != 0 { v4 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName } else { v4 = uintptr(0) } zDb = v4 z = _sqlite3NameFromToken(tls, db, *(*uintptr)(unsafe.Pointer(bp))) if z != 0 { v5 = _sqlite3FindIndex(tls, db, z, zDb) pIdx = v5 if v5 != uintptr(0) { _analyzeTable(tls, pParse, (*TIndex)(unsafe.Pointer(pIdx)).FpTable, pIdx) } else { v6 = _sqlite3LocateTable(tls, pParse, uint32(0), z, zDb) pTab = v6 if v6 != uintptr(0) { _analyzeTable(tls, pParse, pTab, uintptr(0)) } } _sqlite3DbFree(tls, db, z) } } } } if v8 = int32((*Tsqlite3)(unsafe.Pointer(db)).FnSqlExec) == 0; v8 { v7 = _sqlite3GetVdbe(tls, pParse) v = v7 } if v8 && v7 != uintptr(0) { _sqlite3VdbeAddOp0(tls, v, int32(OP_Expire)) } } // C documentation // // /* // ** Used to pass information from the analyzer reader through to the // ** callback routine. // */ type TanalysisInfo = struct { Fdb uintptr FzDatabase uintptr } type analysisInfo = TanalysisInfo type TanalysisInfo1 = struct { Fdb uintptr FzDatabase uintptr } type analysisInfo1 = TanalysisInfo1 // C documentation // // /* // ** The first argument points to a nul-terminated string containing a // ** list of space separated integers. Read the first nOut of these into // ** the array aOut[]. // */ func _decodeIntArray(tls *libc.TLS, zIntArray uintptr, nOut int32, aOut uintptr, aLog uintptr, pIndex uintptr) { var c, i, sz, v2 int32 var v TtRowcnt var z uintptr _, _, _, _, _, _ = c, i, sz, v, z, v2 z = zIntArray if z == uintptr(0) { z = __ccgo_ts + 1648 } i = 0 for { if !(*(*uint8)(unsafe.Pointer(z)) != 0 && i < nOut) { break } v = uint64(0) for { v2 = int32(*(*uint8)(unsafe.Pointer(z))) c = v2 if !(v2 >= int32('0') && c <= int32('9')) { break } v = v*uint64(10) + uint64(c) - uint64('0') z++ } if aOut != 0 { *(*TtRowcnt)(unsafe.Pointer(aOut + uintptr(i)*8)) = v } if aLog != 0 { *(*TLogEst)(unsafe.Pointer(aLog + uintptr(i)*2)) = _sqlite3LogEst(tls, v) } if int32(*(*uint8)(unsafe.Pointer(z))) == int32(' ') { z++ } goto _1 _1: ; i++ } if pIndex != 0 { libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(0), 2, 0x4) libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(0), 6, 0x40) for *(*uint8)(unsafe.Pointer(z)) != 0 { if Xsqlite3_strglob(tls, __ccgo_ts+11801, z) == 0 { libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 2, 0x4) } else { if Xsqlite3_strglob(tls, __ccgo_ts+11812, z) == 0 { sz = _sqlite3Atoi(tls, z+uintptr(3)) if sz < int32(2) { sz = int32(2) } (*TIndex)(unsafe.Pointer(pIndex)).FszIdxRow = _sqlite3LogEst(tls, uint64(sz)) } else { if Xsqlite3_strglob(tls, __ccgo_ts+11822, z) == 0 { libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 6, 0x40) } } } for int32(*(*uint8)(unsafe.Pointer(z))) != 0 && int32(*(*uint8)(unsafe.Pointer(z))) != int32(' ') { z++ } for int32(*(*uint8)(unsafe.Pointer(z))) == int32(' ') { z++ } } /* Set the bLowQual flag if the peak number of rows obtained ** from a full equality match is so large that a full table scan ** seems likely to be faster than using the index. */ if int32(*(*TLogEst)(unsafe.Pointer(aLog))) > int32(66) && int32(*(*TLogEst)(unsafe.Pointer(aLog))) <= int32(*(*TLogEst)(unsafe.Pointer(aLog + uintptr(nOut-int32(1))*2))) { libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 8, 0x100) } } } // C documentation // // /* // ** This callback is invoked once for each index when reading the // ** sqlite_stat1 table. // ** // ** argv[0] = name of the table // ** argv[1] = name of the index (might be NULL) // ** argv[2] = results of analysis - on integer for each column // ** // ** Entries for which argv[1]==NULL simply record the number of rows in // ** the table. // */ func _analysisLoader(tls *libc.TLS, pData uintptr, argc int32, argv uintptr, NotUsed uintptr) (r int32) { bp := tls.Alloc(112) defer tls.Free(112) var aiRowEst, pIndex, pInfo, pTable, z uintptr var nCol int32 var _ /* fakeIdx at bp+0 */ TIndex _, _, _, _, _, _ = aiRowEst, nCol, pIndex, pInfo, pTable, z pInfo = pData _ = libc.Int32FromInt32(0) _ = NotUsed _ = argc if argv == uintptr(0) || *(*uintptr)(unsafe.Pointer(argv)) == uintptr(0) || *(*uintptr)(unsafe.Pointer(argv + 2*4)) == uintptr(0) { return 0 } pTable = _sqlite3FindTable(tls, (*TanalysisInfo)(unsafe.Pointer(pInfo)).Fdb, *(*uintptr)(unsafe.Pointer(argv)), (*TanalysisInfo)(unsafe.Pointer(pInfo)).FzDatabase) if pTable == uintptr(0) { return 0 } if *(*uintptr)(unsafe.Pointer(argv + 1*4)) == uintptr(0) { pIndex = uintptr(0) } else { if Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer(argv)), *(*uintptr)(unsafe.Pointer(argv + 1*4))) == 0 { pIndex = _sqlite3PrimaryKeyIndex(tls, pTable) } else { pIndex = _sqlite3FindIndex(tls, (*TanalysisInfo)(unsafe.Pointer(pInfo)).Fdb, *(*uintptr)(unsafe.Pointer(argv + 1*4)), (*TanalysisInfo)(unsafe.Pointer(pInfo)).FzDatabase) } } z = *(*uintptr)(unsafe.Pointer(argv + 2*4)) if pIndex != 0 { aiRowEst = uintptr(0) nCol = int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol) + int32(1) /* Index.aiRowEst may already be set here if there are duplicate ** sqlite_stat1 entries for this index. In that case just clobber ** the old data with the new instead of allocating a new array. */ if (*TIndex)(unsafe.Pointer(pIndex)).FaiRowEst == uintptr(0) { (*TIndex)(unsafe.Pointer(pIndex)).FaiRowEst = _sqlite3MallocZero(tls, uint64(uint32(8)*uint32(nCol))) if (*TIndex)(unsafe.Pointer(pIndex)).FaiRowEst == uintptr(0) { _sqlite3OomFault(tls, (*TanalysisInfo)(unsafe.Pointer(pInfo)).Fdb) } } aiRowEst = (*TIndex)(unsafe.Pointer(pIndex)).FaiRowEst libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(0), 2, 0x4) _decodeIntArray(tls, z, nCol, aiRowEst, (*TIndex)(unsafe.Pointer(pIndex)).FaiRowLogEst, pIndex) libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 7, 0x80) if (*TIndex)(unsafe.Pointer(pIndex)).FpPartIdxWhere == uintptr(0) { (*TTable)(unsafe.Pointer(pTable)).FnRowLogEst = *(*TLogEst)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FaiRowLogEst)) *(*Tu32)(unsafe.Pointer(pTable + 28)) |= uint32(TF_HasStat1) } } else { (*(*TIndex)(unsafe.Pointer(bp))).FszIdxRow = (*TTable)(unsafe.Pointer(pTable)).FszTabRow _decodeIntArray(tls, z, int32(1), uintptr(0), pTable+38, bp) (*TTable)(unsafe.Pointer(pTable)).FszTabRow = (*(*TIndex)(unsafe.Pointer(bp))).FszIdxRow *(*Tu32)(unsafe.Pointer(pTable + 28)) |= uint32(TF_HasStat1) } return 0 } // C documentation // // /* // ** If the Index.aSample variable is not NULL, delete the aSample[] array // ** and its contents. // */ func _sqlite3DeleteIndexSamples(tls *libc.TLS, db uintptr, pIdx uintptr) { var j int32 var p uintptr _, _ = j, p _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TIndex)(unsafe.Pointer(pIdx)).FaSample != 0 { j = 0 for { if !(j < (*TIndex)(unsafe.Pointer(pIdx)).FnSample) { break } p = (*TIndex)(unsafe.Pointer(pIdx)).FaSample + uintptr(j)*20 _sqlite3DbFree(tls, db, (*TIndexSample)(unsafe.Pointer(p)).Fp) goto _1 _1: ; j++ } _sqlite3DbFree(tls, db, (*TIndex)(unsafe.Pointer(pIdx)).FaSample) } if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0) { (*TIndex)(unsafe.Pointer(pIdx)).FnSample = 0 (*TIndex)(unsafe.Pointer(pIdx)).FaSample = uintptr(0) } } // C documentation // // /* // ** Populate the pIdx->aAvgEq[] array based on the samples currently // ** stored in pIdx->aSample[]. // */ func _initAvgEq(tls *libc.TLS, pIdx uintptr) { var aSample, pFinal uintptr var avgEq, nRow, sumEq TtRowcnt var i, iCol, nCol, nSample int32 var nDist100, nSum100 Ti64 _, _, _, _, _, _, _, _, _, _, _ = aSample, avgEq, i, iCol, nCol, nDist100, nRow, nSample, nSum100, pFinal, sumEq if pIdx != 0 { aSample = (*TIndex)(unsafe.Pointer(pIdx)).FaSample pFinal = aSample + uintptr((*TIndex)(unsafe.Pointer(pIdx)).FnSample-int32(1))*20 nCol = int32(1) if (*TIndex)(unsafe.Pointer(pIdx)).FnSampleCol > int32(1) { /* If this is stat4 data, then calculate aAvgEq[] values for all ** sample columns except the last. The last is always set to 1, as ** once the trailing PK fields are considered all index keys are ** unique. */ nCol = (*TIndex)(unsafe.Pointer(pIdx)).FnSampleCol - int32(1) *(*TtRowcnt)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaAvgEq + uintptr(nCol)*8)) = uint64(1) } iCol = 0 for { if !(iCol < nCol) { break } nSample = (*TIndex)(unsafe.Pointer(pIdx)).FnSample /* Used to iterate through samples */ sumEq = uint64(0) /* Sum of the nEq values */ avgEq = uint64(0) /* Number of rows in index */ nSum100 = 0 /* Number of distinct values in index */ if !((*TIndex)(unsafe.Pointer(pIdx)).FaiRowEst != 0) || iCol >= int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) || *(*TtRowcnt)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiRowEst + uintptr(iCol+int32(1))*8)) == uint64(0) { nRow = *(*TtRowcnt)(unsafe.Pointer((*TIndexSample)(unsafe.Pointer(pFinal)).FanLt + uintptr(iCol)*8)) nDist100 = int64(uint64(libc.Int64FromInt32(100)) * *(*TtRowcnt)(unsafe.Pointer((*TIndexSample)(unsafe.Pointer(pFinal)).FanDLt + uintptr(iCol)*8))) nSample-- } else { nRow = *(*TtRowcnt)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiRowEst)) nDist100 = int64(uint64(libc.Int64FromInt32(100)) * *(*TtRowcnt)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiRowEst)) / *(*TtRowcnt)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiRowEst + uintptr(iCol+int32(1))*8))) } (*TIndex)(unsafe.Pointer(pIdx)).FnRowEst0 = nRow /* Set nSum to the number of distinct (iCol+1) field prefixes that ** occur in the stat4 table for this index. Set sumEq to the sum of ** the nEq values for column iCol for the same set (adding the value ** only once where there exist duplicate prefixes). */ i = 0 for { if !(i < nSample) { break } if i == (*TIndex)(unsafe.Pointer(pIdx)).FnSample-int32(1) || *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(i)*20))).FanDLt + uintptr(iCol)*8)) != *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(i+int32(1))*20))).FanDLt + uintptr(iCol)*8)) { sumEq += *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(i)*20))).FanEq + uintptr(iCol)*8)) nSum100 += int64(100) } goto _2 _2: ; i++ } if nDist100 > nSum100 && sumEq < nRow { avgEq = uint64(libc.Int64FromInt32(100)) * (nRow - sumEq) / uint64(nDist100-nSum100) } if avgEq == uint64(0) { avgEq = uint64(1) } *(*TtRowcnt)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaAvgEq + uintptr(iCol)*8)) = avgEq goto _1 _1: ; iCol++ } } } // C documentation // // /* // ** Look up an index by name. Or, if the name of a WITHOUT ROWID table // ** is supplied instead, find the PRIMARY KEY index for that table. // */ func _findIndexOrPrimaryKey(tls *libc.TLS, db uintptr, zName uintptr, zDb uintptr) (r uintptr) { var pIdx, pTab uintptr _, _ = pIdx, pTab pIdx = _sqlite3FindIndex(tls, db, zName, zDb) if pIdx == uintptr(0) { pTab = _sqlite3FindTable(tls, db, zName, zDb) if pTab != 0 && !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { pIdx = _sqlite3PrimaryKeyIndex(tls, pTab) } } return pIdx } // C documentation // // /* // ** Load the content from either the sqlite_stat4 // ** into the relevant Index.aSample[] arrays. // ** // ** Arguments zSql1 and zSql2 must point to SQL statements that return // ** data equivalent to the following: // ** // ** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx // ** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4 // ** // ** where %Q is replaced with the database name before the SQL is executed. // */ func _loadStatTbl(tls *libc.TLS, db uintptr, zSql1 uintptr, zSql2 uintptr, zDb uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var i, nByte, nCol, nIdxCol, nSample, rc int32 var pIdx, pIdx1, pPrevIdx, pSample, pSpace, zIndex, zIndex1, zSql uintptr var _ /* pStmt at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, nByte, nCol, nIdxCol, nSample, pIdx, pIdx1, pPrevIdx, pSample, pSpace, rc, zIndex, zIndex1, zSql /* Result codes from subroutines */ *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Text of the SQL statement */ pPrevIdx = uintptr(0) /* A slot in pIdx->aSample[] */ _ = libc.Int32FromInt32(0) zSql = _sqlite3MPrintf(tls, db, zSql1, libc.VaList(bp+16, zDb)) if !(zSql != 0) { return int32(SQLITE_NOMEM) } rc = Xsqlite3_prepare(tls, db, zSql, -int32(1), bp, uintptr(0)) _sqlite3DbFree(tls, db, zSql) if rc != 0 { return rc } for Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) == int32(SQLITE_ROW) { nIdxCol = int32(1) zIndex = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) if zIndex == uintptr(0) { continue } nSample = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) pIdx = _findIndexOrPrimaryKey(tls, db, zIndex, zDb) _ = libc.Int32FromInt32(0) if pIdx == uintptr(0) { continue } if (*TIndex)(unsafe.Pointer(pIdx)).FaSample != uintptr(0) { /* The same index appears in sqlite_stat4 under multiple names */ continue } _ = libc.Int32FromInt32(0) if !((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) { nIdxCol = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) } else { nIdxCol = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) } (*TIndex)(unsafe.Pointer(pIdx)).FnSampleCol = nIdxCol (*TIndex)(unsafe.Pointer(pIdx)).FmxSample = nSample nByte = int32(uint32(20) * uint32(nSample)) nByte = int32(uint32(nByte) + libc.Uint32FromInt64(8)*uint32(nIdxCol)*libc.Uint32FromInt32(3)*uint32(nSample)) nByte = int32(uint32(nByte) + uint32(nIdxCol)*libc.Uint32FromInt64(8)) /* Space for Index.aAvgEq[] */ (*TIndex)(unsafe.Pointer(pIdx)).FaSample = _sqlite3DbMallocZero(tls, db, uint64(nByte)) if (*TIndex)(unsafe.Pointer(pIdx)).FaSample == uintptr(0) { Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) return int32(SQLITE_NOMEM) } pSpace = (*TIndex)(unsafe.Pointer(pIdx)).FaSample + uintptr(nSample)*20 (*TIndex)(unsafe.Pointer(pIdx)).FaAvgEq = pSpace pSpace += uintptr(nIdxCol) * 8 *(*Tu32)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable + 28)) |= uint32(TF_HasStat4) i = 0 for { if !(i < nSample) { break } (*(*TIndexSample)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaSample + uintptr(i)*20))).FanEq = pSpace pSpace += uintptr(nIdxCol) * 8 (*(*TIndexSample)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaSample + uintptr(i)*20))).FanLt = pSpace pSpace += uintptr(nIdxCol) * 8 (*(*TIndexSample)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaSample + uintptr(i)*20))).FanDLt = pSpace pSpace += uintptr(nIdxCol) * 8 goto _1 _1: ; i++ } _ = libc.Int32FromInt32(0) } rc = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) if rc != 0 { return rc } zSql = _sqlite3MPrintf(tls, db, zSql2, libc.VaList(bp+16, zDb)) if !(zSql != 0) { return int32(SQLITE_NOMEM) } rc = Xsqlite3_prepare(tls, db, zSql, -int32(1), bp, uintptr(0)) _sqlite3DbFree(tls, db, zSql) if rc != 0 { return rc } for Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) == int32(SQLITE_ROW) { /* Pointer to the index object */ nCol = int32(1) /* Number of columns in index */ zIndex1 = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) if zIndex1 == uintptr(0) { continue } pIdx1 = _findIndexOrPrimaryKey(tls, db, zIndex1, zDb) if pIdx1 == uintptr(0) { continue } if (*TIndex)(unsafe.Pointer(pIdx1)).FnSample >= (*TIndex)(unsafe.Pointer(pIdx1)).FmxSample { /* Too many slots used because the same index appears in ** sqlite_stat4 using multiple names */ continue } /* This next condition is true if data has already been loaded from ** the sqlite_stat4 table. */ nCol = (*TIndex)(unsafe.Pointer(pIdx1)).FnSampleCol if pIdx1 != pPrevIdx { _initAvgEq(tls, pPrevIdx) pPrevIdx = pIdx1 } pSample = (*TIndex)(unsafe.Pointer(pIdx1)).FaSample + uintptr((*TIndex)(unsafe.Pointer(pIdx1)).FnSample)*20 _decodeIntArray(tls, Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)), nCol, (*TIndexSample)(unsafe.Pointer(pSample)).FanEq, uintptr(0), uintptr(0)) _decodeIntArray(tls, Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(2)), nCol, (*TIndexSample)(unsafe.Pointer(pSample)).FanLt, uintptr(0), uintptr(0)) _decodeIntArray(tls, Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(3)), nCol, (*TIndexSample)(unsafe.Pointer(pSample)).FanDLt, uintptr(0), uintptr(0)) /* Take a copy of the sample. Add 8 extra 0x00 bytes the end of the buffer. ** This is in case the sample record is corrupted. In that case, the ** sqlite3VdbeRecordCompare() may read up to two varints past the ** end of the allocated buffer before it realizes it is dealing with ** a corrupt record. Or it might try to read a large integer from the ** buffer. In any case, eight 0x00 bytes prevents this from causing ** a buffer overread. */ (*TIndexSample)(unsafe.Pointer(pSample)).Fn = Xsqlite3_column_bytes(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(4)) (*TIndexSample)(unsafe.Pointer(pSample)).Fp = _sqlite3DbMallocZero(tls, db, uint64((*TIndexSample)(unsafe.Pointer(pSample)).Fn+int32(8))) if (*TIndexSample)(unsafe.Pointer(pSample)).Fp == uintptr(0) { Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) return int32(SQLITE_NOMEM) } if (*TIndexSample)(unsafe.Pointer(pSample)).Fn != 0 { libc.Xmemcpy(tls, (*TIndexSample)(unsafe.Pointer(pSample)).Fp, Xsqlite3_column_blob(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(4)), uint32((*TIndexSample)(unsafe.Pointer(pSample)).Fn)) } (*TIndex)(unsafe.Pointer(pIdx1)).FnSample++ } rc = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) if rc == SQLITE_OK { _initAvgEq(tls, pPrevIdx) } return rc } // C documentation // // /* // ** Load content from the sqlite_stat4 table into // ** the Index.aSample[] arrays of all indices. // */ func _loadStat4(tls *libc.TLS, db uintptr, zDb uintptr) (r int32) { var pStat4, v1 uintptr var rc int32 var v2 bool _, _, _, _ = pStat4, rc, v1, v2 rc = SQLITE_OK _ = libc.Int32FromInt32(0) if v2 = (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) == uint32(0); v2 { v1 = _sqlite3FindTable(tls, db, __ccgo_ts+11608, zDb) pStat4 = v1 } if v2 && v1 != uintptr(0) && int32((*TTable)(unsafe.Pointer(pStat4)).FeTabType) == TABTYP_NORM { rc = _loadStatTbl(tls, db, __ccgo_ts+11834, __ccgo_ts+11903, zDb) } return rc } // C documentation // // /* // ** Load the content of the sqlite_stat1 and sqlite_stat4 tables. The // ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[] // ** arrays. The contents of sqlite_stat4 are used to populate the // ** Index.aSample[] arrays. // ** // ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR // ** is returned. In this case, even if SQLITE_ENABLE_STAT4 was defined // ** during compilation and the sqlite_stat4 table is present, no data is // ** read from it. // ** // ** If SQLITE_ENABLE_STAT4 was defined during compilation and the // ** sqlite_stat4 table is not present in the database, SQLITE_ERROR is // ** returned. However, in this case, data is read from the sqlite_stat1 // ** table (if it is present) before returning. // ** // ** If an OOM error occurs, this function always sets db->mallocFailed. // ** This means if the caller does not care about other errors, the return // ** code may be ignored. // */ func _sqlite3AnalysisLoad(tls *libc.TLS, db uintptr, iDb int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var i, pIdx, pIdx1, pIdx2, pSchema, pStat1, pTab, zSql, v3 uintptr var rc, v5 int32 var _ /* sInfo at bp+0 */ TanalysisInfo _, _, _, _, _, _, _, _, _, _, _ = i, pIdx, pIdx1, pIdx2, pSchema, pStat1, pTab, rc, zSql, v3, v5 rc = SQLITE_OK pSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Clear any prior statistics */ _ = libc.Int32FromInt32(0) i = (*THash)(unsafe.Pointer(pSchema + 8)).Ffirst for { if !(i != 0) { break } pTab = (*THashElem)(unsafe.Pointer(i)).Fdata *(*Tu32)(unsafe.Pointer(pTab + 28)) &= uint32(^libc.Int32FromInt32(TF_HasStat1)) goto _1 _1: ; i = (*THashElem)(unsafe.Pointer(i)).Fnext } i = (*THash)(unsafe.Pointer(pSchema + 24)).Ffirst for { if !(i != 0) { break } pIdx = (*THashElem)(unsafe.Pointer(i)).Fdata libc.SetBitFieldPtr16Uint32(pIdx+56, libc.Uint32FromInt32(0), 7, 0x80) _sqlite3DeleteIndexSamples(tls, db, pIdx) (*TIndex)(unsafe.Pointer(pIdx)).FaSample = uintptr(0) goto _2 _2: ; i = (*THashElem)(unsafe.Pointer(i)).Fnext } /* Load new statistics out of the sqlite_stat1 table */ (*(*TanalysisInfo)(unsafe.Pointer(bp))).Fdb = db (*(*TanalysisInfo)(unsafe.Pointer(bp))).FzDatabase = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName v3 = _sqlite3FindTable(tls, db, __ccgo_ts+11582, (*(*TanalysisInfo)(unsafe.Pointer(bp))).FzDatabase) pStat1 = v3 if v3 != 0 && int32((*TTable)(unsafe.Pointer(pStat1)).FeTabType) == TABTYP_NORM { zSql = _sqlite3MPrintf(tls, db, __ccgo_ts+11955, libc.VaList(bp+16, (*(*TanalysisInfo)(unsafe.Pointer(bp))).FzDatabase)) if zSql == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { rc = Xsqlite3_exec(tls, db, zSql, __ccgo_fp(_analysisLoader), bp, uintptr(0)) _sqlite3DbFree(tls, db, zSql) } } /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */ _ = libc.Int32FromInt32(0) i = (*THash)(unsafe.Pointer(pSchema + 24)).Ffirst for { if !(i != 0) { break } pIdx1 = (*THashElem)(unsafe.Pointer(i)).Fdata if !(int32(uint32(*(*uint16)(unsafe.Pointer(pIdx1 + 56))&0x80>>7)) != 0) { _sqlite3DefaultRowEst(tls, pIdx1) } goto _4 _4: ; i = (*THashElem)(unsafe.Pointer(i)).Fnext } /* Load the statistics from the sqlite_stat4 table. */ if rc == SQLITE_OK { (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable++ (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0) rc = _loadStat4(tls, db, (*(*TanalysisInfo)(unsafe.Pointer(bp))).FzDatabase) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable-- if (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable != 0 { v5 = 0 } else { v5 = int32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue) } (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(v5) } i = (*THash)(unsafe.Pointer(pSchema + 24)).Ffirst for { if !(i != 0) { break } pIdx2 = (*THashElem)(unsafe.Pointer(i)).Fdata Xsqlite3_free(tls, (*TIndex)(unsafe.Pointer(pIdx2)).FaiRowEst) (*TIndex)(unsafe.Pointer(pIdx2)).FaiRowEst = uintptr(0) goto _6 _6: ; i = (*THashElem)(unsafe.Pointer(i)).Fnext } if rc == int32(SQLITE_NOMEM) { _sqlite3OomFault(tls, db) } return rc } /************** End of analyze.c *********************************************/ /************** Begin file attach.c ******************************************/ /* ** 2003 April 6 ** ** 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 contains code used to implement the ATTACH and DETACH commands. */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** Resolve an expression that was part of an ATTACH or DETACH statement. This // ** is slightly different from resolving a normal SQL expression, because simple // ** identifiers are treated as strings, not possible column names or aliases. // ** // ** i.e. if the parser sees: // ** // ** ATTACH DATABASE abc AS def // ** // ** it treats the two expressions as literal strings 'abc' and 'def' instead of // ** looking for columns of the same name. // ** // ** This only applies to the root node of pExpr, so the statement: // ** // ** ATTACH DATABASE abc||def AS 'db2' // ** // ** will fail because neither abc or def can be resolved. // */ func _resolveAttachExpr(tls *libc.TLS, pName uintptr, pExpr uintptr) (r int32) { var rc int32 _ = rc rc = SQLITE_OK if pExpr != 0 { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_ID) { rc = _sqlite3ResolveExprNames(tls, pName, pExpr) } else { (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_STRING) } } return rc } // C documentation // // /* // ** Return true if zName points to a name that may be used to refer to // ** database iDb attached to handle db. // */ func _sqlite3DbIsNamed(tls *libc.TLS, db uintptr, iDb int32, zName uintptr) (r int32) { return libc.BoolInt32(_sqlite3StrICmp(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, zName) == 0 || iDb == 0 && _sqlite3StrICmp(tls, __ccgo_ts+6585, zName) == 0) } // C documentation // // /* // ** An SQL user-function registered to do the work of an ATTACH statement. The // ** three arguments to the function come directly from an attach statement: // ** // ** ATTACH DATABASE x AS y KEY z // ** // ** SELECT sqlite_attach(x, y, z) // ** // ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the // ** third argument. // ** // ** If the db->init.reopenMemdb flags is set, then instead of attaching a // ** new database, close the database on db->init.iDb and reopen it as an // ** empty MemDB. // */ func _attachFunc(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var aNew, db, pNew, pNewSchema, pPager, zFile, zName uintptr var i, iDb, rc int32 var _ /* flags at bp+8 */ uint32 var _ /* pNewBt at bp+20 */ uintptr var _ /* pVfs at bp+16 */ uintptr var _ /* zErr at bp+4 */ uintptr var _ /* zErrDyn at bp+12 */ uintptr var _ /* zPath at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _ = aNew, db, i, iDb, pNew, pNewSchema, pPager, rc, zFile, zName rc = 0 db = Xsqlite3_context_db_handle(tls, context) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) /* New array of Db pointers */ pNew = uintptr(0) /* Db object for the newly attached database */ *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) _ = NotUsed zFile = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) zName = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) if zFile == uintptr(0) { zFile = __ccgo_ts + 1648 } if zName == uintptr(0) { zName = __ccgo_ts + 1648 } if int32(uint32(*(*uint8)(unsafe.Pointer(db + 172 + 8))&0x4>>2)) != 0 { /* This is not a real ATTACH. Instead, this routine is being called ** from sqlite3_deserialize() to close database db->init.iDb and ** reopen it as a MemDB */ *(*uintptr)(unsafe.Pointer(bp + 20)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 16)) = Xsqlite3_vfs_find(tls, __ccgo_ts+3967) if *(*uintptr)(unsafe.Pointer(bp + 16)) == uintptr(0) { return } rc = _sqlite3BtreeOpen(tls, *(*uintptr)(unsafe.Pointer(bp + 16)), __ccgo_ts+11996, db, bp+20, 0, int32(SQLITE_OPEN_MAIN_DB)) if rc == SQLITE_OK { pNewSchema = _sqlite3SchemaGet(tls, db, *(*uintptr)(unsafe.Pointer(bp + 20))) if pNewSchema != 0 { /* Both the Btree and the new Schema were allocated successfully. ** Close the old db and update the aDb[] slot with the new memdb ** values. */ pNew = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb)*16 if (*TDb)(unsafe.Pointer(pNew)).FpBt != 0 { _sqlite3BtreeClose(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt) } (*TDb)(unsafe.Pointer(pNew)).FpBt = *(*uintptr)(unsafe.Pointer(bp + 20)) (*TDb)(unsafe.Pointer(pNew)).FpSchema = pNewSchema } else { _sqlite3BtreeClose(tls, *(*uintptr)(unsafe.Pointer(bp + 20))) rc = int32(SQLITE_NOMEM) } } if rc != 0 { goto attach_error } } else { /* This is a real ATTACH ** ** Check for the following errors: ** ** * Too many attached databases, ** * Transaction currently open ** * Specified database name already being used. */ if (*Tsqlite3)(unsafe.Pointer(db)).FnDb >= *(*int32)(unsafe.Pointer(db + 120 + 7*4))+int32(2) { *(*uintptr)(unsafe.Pointer(bp + 12)) = _sqlite3MPrintf(tls, db, __ccgo_ts+11999, libc.VaList(bp+32, *(*int32)(unsafe.Pointer(db + 120 + 7*4)))) goto attach_error } i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } _ = libc.Int32FromInt32(0) if _sqlite3DbIsNamed(tls, db, i, zName) != 0 { *(*uintptr)(unsafe.Pointer(bp + 12)) = _sqlite3MPrintf(tls, db, __ccgo_ts+12036, libc.VaList(bp+32, zName)) goto attach_error } goto _1 _1: ; i++ } /* Allocate the new entry in the db->aDb[] array and initialize the schema ** hash tables. */ if (*Tsqlite3)(unsafe.Pointer(db)).FaDb == db+476 { aNew = _sqlite3DbMallocRawNN(tls, db, uint64(libc.Uint32FromInt64(16)*libc.Uint32FromInt32(3))) if aNew == uintptr(0) { return } libc.Xmemcpy(tls, aNew, (*Tsqlite3)(unsafe.Pointer(db)).FaDb, libc.Uint32FromInt64(16)*libc.Uint32FromInt32(2)) } else { aNew = _sqlite3DbRealloc(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).FaDb, uint64(uint32(16)*uint32((*Tsqlite3)(unsafe.Pointer(db)).FnDb+libc.Int32FromInt32(1)))) if aNew == uintptr(0) { return } } (*Tsqlite3)(unsafe.Pointer(db)).FaDb = aNew pNew = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*Tsqlite3)(unsafe.Pointer(db)).FnDb)*16 libc.Xmemset(tls, pNew, 0, uint32(16)) /* Open the database file. If the btree is successfully opened, use ** it to obtain the database schema. At this point the schema may ** or may not be initialized. */ *(*uint32)(unsafe.Pointer(bp + 8)) = (*Tsqlite3)(unsafe.Pointer(db)).FopenFlags rc = _sqlite3ParseUri(tls, (*Tsqlite3_vfs)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpVfs)).FzName, zFile, bp+8, bp+16, bp, bp+4) if rc != SQLITE_OK { if rc == int32(SQLITE_NOMEM) { _sqlite3OomFault(tls, db) } Xsqlite3_result_error(tls, context, *(*uintptr)(unsafe.Pointer(bp + 4)), -int32(1)) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) return } _ = libc.Int32FromInt32(0) *(*uint32)(unsafe.Pointer(bp + 8)) |= uint32(SQLITE_OPEN_MAIN_DB) rc = _sqlite3BtreeOpen(tls, *(*uintptr)(unsafe.Pointer(bp + 16)), *(*uintptr)(unsafe.Pointer(bp)), db, pNew+4, 0, int32(*(*uint32)(unsafe.Pointer(bp + 8)))) (*Tsqlite3)(unsafe.Pointer(db)).FnDb++ (*TDb)(unsafe.Pointer(pNew)).FzDbSName = _sqlite3DbStrDup(tls, db, zName) } (*Tsqlite3)(unsafe.Pointer(db)).FnoSharedCache = uint8(0) if rc == int32(SQLITE_CONSTRAINT) { rc = int32(SQLITE_ERROR) *(*uintptr)(unsafe.Pointer(bp + 12)) = _sqlite3MPrintf(tls, db, __ccgo_ts+12066, 0) } else { if rc == SQLITE_OK { (*TDb)(unsafe.Pointer(pNew)).FpSchema = _sqlite3SchemaGet(tls, db, (*TDb)(unsafe.Pointer(pNew)).FpBt) if !((*TDb)(unsafe.Pointer(pNew)).FpSchema != 0) { rc = int32(SQLITE_NOMEM) } else { if (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pNew)).FpSchema)).Ffile_format != 0 && int32((*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pNew)).FpSchema)).Fenc) != int32((*Tsqlite3)(unsafe.Pointer(db)).Fenc) { *(*uintptr)(unsafe.Pointer(bp + 12)) = _sqlite3MPrintf(tls, db, __ccgo_ts+12095, 0) rc = int32(SQLITE_ERROR) } } _sqlite3BtreeEnter(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt) pPager = _sqlite3BtreePager(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt) _sqlite3PagerLockingMode(tls, pPager, int32((*Tsqlite3)(unsafe.Pointer(db)).FdfltLockMode)) _sqlite3BtreeSecureDelete(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt, _sqlite3BtreeSecureDelete(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpBt, -int32(1))) _sqlite3BtreeSetPagerFlags(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt, uint32(uint64(PAGER_SYNCHRONOUS_FULL)|(*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(PAGER_FLAGS_MASK))) _sqlite3BtreeLeave(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt) } } (*TDb)(unsafe.Pointer(pNew)).Fsafety_level = uint8(libc.Int32FromInt32(SQLITE_DEFAULT_SYNCHRONOUS) + libc.Int32FromInt32(1)) if rc == SQLITE_OK && (*TDb)(unsafe.Pointer(pNew)).FzDbSName == uintptr(0) { rc = int32(SQLITE_NOMEM) } Xsqlite3_free_filename(tls, *(*uintptr)(unsafe.Pointer(bp))) /* If the file was opened successfully, read the schema for the new database. ** If this fails, or if opening the file failed, then close the file and ** remove the entry from the db->aDb[] array. i.e. put everything back the ** way we found it. */ if rc == SQLITE_OK { _sqlite3BtreeEnterAll(tls, db) (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(0) *(*Tu32)(unsafe.Pointer(db + 24)) &= uint32(^libc.Int32FromInt32(DBFLAG_SchemaKnownOk)) if !(int32(uint32(*(*uint8)(unsafe.Pointer(db + 172 + 8))&0x4>>2)) != 0) { rc = _sqlite3Init(tls, db, bp+12) } _sqlite3BtreeLeaveAll(tls, db) _ = libc.Int32FromInt32(0) } if rc != 0 { if !(int32(uint32(*(*uint8)(unsafe.Pointer(db + 172 + 8))&0x4>>2)) != 0) { iDb = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1) _ = libc.Int32FromInt32(0) if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt != 0 { _sqlite3BtreeClose(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt) (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt = uintptr(0) (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema = uintptr(0) } _sqlite3ResetAllSchemasOfConnection(tls, db) (*Tsqlite3)(unsafe.Pointer(db)).FnDb = iDb if rc == int32(SQLITE_NOMEM) || rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<= (*Tsqlite3)(unsafe.Pointer(db)).FnDb { Xsqlite3_snprintf(tls, int32(128), bp, __ccgo_ts+12191, libc.VaList(bp+136, zName)) goto detach_error } if i < int32(2) { Xsqlite3_snprintf(tls, int32(128), bp, __ccgo_ts+12212, libc.VaList(bp+136, zName)) goto detach_error } if _sqlite3BtreeTxnState(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) != SQLITE_TXN_NONE || _sqlite3BtreeIsInBackup(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) != 0 { Xsqlite3_snprintf(tls, int32(128), bp, __ccgo_ts+12238, libc.VaList(bp+136, zName)) goto detach_error } /* If any TEMP triggers reference the schema being detached, move those ** triggers to reference the TEMP schema itself. */ _ = libc.Int32FromInt32(0) pEntry = (*THash)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema + 40)).Ffirst for pEntry != 0 { pTrig = (*THashElem)(unsafe.Pointer(pEntry)).Fdata if (*TTrigger)(unsafe.Pointer(pTrig)).FpTabSchema == (*TDb)(unsafe.Pointer(pDb)).FpSchema { (*TTrigger)(unsafe.Pointer(pTrig)).FpTabSchema = (*TTrigger)(unsafe.Pointer(pTrig)).FpSchema } pEntry = (*THashElem)(unsafe.Pointer(pEntry)).Fnext } _sqlite3BtreeClose(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) (*TDb)(unsafe.Pointer(pDb)).FpBt = uintptr(0) (*TDb)(unsafe.Pointer(pDb)).FpSchema = uintptr(0) _sqlite3CollapseDatabaseArray(tls, db) return goto detach_error detach_error: ; Xsqlite3_result_error(tls, context, bp, -int32(1)) } // C documentation // // /* // ** This procedure generates VDBE code for a single invocation of either the // ** sqlite_detach() or sqlite_attach() SQL user functions. // */ func _codeAttach(tls *libc.TLS, pParse uintptr, type1 int32, pFunc uintptr, pAuthArg uintptr, pFilename uintptr, pDbname uintptr, pKey uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var db, v, zAuthArg uintptr var rc, regArgs int32 var _ /* sName at bp+0 */ TNameContext _, _, _, _, _ = db, rc, regArgs, v, zAuthArg db = (*TParse)(unsafe.Pointer(pParse)).Fdb if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) { goto attach_end } if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto attach_end } libc.Xmemset(tls, bp, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp))).FpParse = pParse if SQLITE_OK != _resolveAttachExpr(tls, bp, pFilename) || SQLITE_OK != _resolveAttachExpr(tls, bp, pDbname) || SQLITE_OK != _resolveAttachExpr(tls, bp, pKey) { goto attach_end } if pAuthArg != 0 { if int32((*TExpr)(unsafe.Pointer(pAuthArg)).Fop) == int32(TK_STRING) { _ = libc.Int32FromInt32(0) zAuthArg = *(*uintptr)(unsafe.Pointer(pAuthArg + 8)) } else { zAuthArg = uintptr(0) } rc = _sqlite3AuthCheck(tls, pParse, type1, zAuthArg, uintptr(0), uintptr(0)) if rc != SQLITE_OK { goto attach_end } } v = _sqlite3GetVdbe(tls, pParse) regArgs = _sqlite3GetTempRange(tls, pParse, int32(4)) _sqlite3ExprCode(tls, pParse, pFilename, regArgs) _sqlite3ExprCode(tls, pParse, pDbname, regArgs+int32(1)) _sqlite3ExprCode(tls, pParse, pKey, regArgs+int32(2)) _ = libc.Int32FromInt32(0) if v != 0 { _sqlite3VdbeAddFunctionCall(tls, pParse, 0, regArgs+int32(3)-int32((*TFuncDef)(unsafe.Pointer(pFunc)).FnArg), regArgs+int32(3), int32((*TFuncDef)(unsafe.Pointer(pFunc)).FnArg), pFunc, 0) /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this ** statement only). For DETACH, set it to false (expire all existing ** statements). */ _sqlite3VdbeAddOp1(tls, v, int32(OP_Expire), libc.BoolInt32(type1 == int32(SQLITE_ATTACH))) } goto attach_end attach_end: ; _sqlite3ExprDelete(tls, db, pFilename) _sqlite3ExprDelete(tls, db, pDbname) _sqlite3ExprDelete(tls, db, pKey) } // C documentation // // /* // ** Called by the parser to compile a DETACH statement. // ** // ** DETACH pDbname // */ func _sqlite3Detach(tls *libc.TLS, pParse uintptr, pDbname uintptr) { _codeAttach(tls, pParse, int32(SQLITE_DETACH), uintptr(unsafe.Pointer(&_detach_func)), pDbname, uintptr(0), uintptr(0), pDbname) } var _detach_func = TFuncDef{ FnArg: int8(1), FfuncFlags: uint32(SQLITE_UTF8), FzName: __ccgo_ts + 12260, } func init() { p := unsafe.Pointer(&_detach_func) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_detachFunc) } // C documentation // // /* // ** Called by the parser to compile an ATTACH statement. // ** // ** ATTACH p AS pDbname KEY pKey // */ func _sqlite3Attach(tls *libc.TLS, pParse uintptr, p uintptr, pDbname uintptr, pKey uintptr) { _codeAttach(tls, pParse, int32(SQLITE_ATTACH), uintptr(unsafe.Pointer(&_attach_func)), p, p, pDbname, pKey) } var _attach_func = TFuncDef{ FnArg: int8(3), FfuncFlags: uint32(SQLITE_UTF8), FzName: __ccgo_ts + 12274, } func init() { p := unsafe.Pointer(&_attach_func) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_attachFunc) } // C documentation // // /* // ** Expression callback used by sqlite3FixAAAA() routines. // */ func _fixExprCb(tls *libc.TLS, p uintptr, pExpr uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pFix uintptr _ = pFix pFix = *(*uintptr)(unsafe.Pointer(p + 24)) if !((*TDbFixer)(unsafe.Pointer(pFix)).FbTemp != 0) { *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_FromDDL)) } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_VARIABLE) { if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer((*TDbFixer)(unsafe.Pointer(pFix)).FpParse)).Fdb)).Finit1.Fbusy != 0 { (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_NULL) } else { _sqlite3ErrorMsg(tls, (*TDbFixer)(unsafe.Pointer(pFix)).FpParse, __ccgo_ts+12288, libc.VaList(bp+8, (*TDbFixer)(unsafe.Pointer(pFix)).FzType)) return int32(WRC_Abort) } } return WRC_Continue } // C documentation // // /* // ** Select callback used by sqlite3FixAAAA() routines. // */ func _fixSelectCb(tls *libc.TLS, p uintptr, pSelect uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var db, pFix, pItem, pList uintptr var i, iDb int32 _, _, _, _, _, _ = db, i, iDb, pFix, pItem, pList pFix = *(*uintptr)(unsafe.Pointer(p + 24)) db = (*TParse)(unsafe.Pointer((*TDbFixer)(unsafe.Pointer(pFix)).FpParse)).Fdb iDb = _sqlite3FindDbName(tls, db, (*TDbFixer)(unsafe.Pointer(pFix)).FzDb) pList = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc if pList == uintptr(0) { return WRC_Continue } i = 0 pItem = pList + 8 for { if !(i < (*TSrcList)(unsafe.Pointer(pList)).FnSrc) { break } if int32((*TDbFixer)(unsafe.Pointer(pFix)).FbTemp) == 0 { if (*TSrcItem)(unsafe.Pointer(pItem)).FzDatabase != 0 { if iDb != _sqlite3FindDbName(tls, db, (*TSrcItem)(unsafe.Pointer(pItem)).FzDatabase) { _sqlite3ErrorMsg(tls, (*TDbFixer)(unsafe.Pointer(pFix)).FpParse, __ccgo_ts+12312, libc.VaList(bp+8, (*TDbFixer)(unsafe.Pointer(pFix)).FzType, (*TDbFixer)(unsafe.Pointer(pFix)).FpName, (*TSrcItem)(unsafe.Pointer(pItem)).FzDatabase)) return int32(WRC_Abort) } _sqlite3DbFree(tls, db, (*TSrcItem)(unsafe.Pointer(pItem)).FzDatabase) (*TSrcItem)(unsafe.Pointer(pItem)).FzDatabase = uintptr(0) libc.SetBitFieldPtr16Uint32(pItem+36+4, libc.Uint32FromInt32(1), 9, 0x200) } (*TSrcItem)(unsafe.Pointer(pItem)).FpSchema = (*TDbFixer)(unsafe.Pointer(pFix)).FpSchema libc.SetBitFieldPtr16Uint32(pItem+36+4, libc.Uint32FromInt32(1), 7, 0x80) } if int32(uint32(*(*uint16)(unsafe.Pointer(pList + 8 + uintptr(i)*72 + 36 + 4))&0x400>>10)) == 0 && _sqlite3WalkExpr(tls, pFix+4, *(*uintptr)(unsafe.Pointer(pList + 8 + uintptr(i)*72 + 48))) != 0 { return int32(WRC_Abort) } goto _1 _1: ; i++ pItem += 72 } if (*TSelect)(unsafe.Pointer(pSelect)).FpWith != 0 { i = 0 for { if !(i < (*TWith)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpWith)).FnCte) { break } if _sqlite3WalkSelect(tls, p, (*(*TCte)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpWith + 12 + uintptr(i)*24))).FpSelect) != 0 { return int32(WRC_Abort) } goto _2 _2: ; i++ } } return WRC_Continue } // C documentation // // /* // ** Initialize a DbFixer structure. This routine must be called prior // ** to passing the structure to one of the sqliteFixAAAA() routines below. // */ func _sqlite3FixInit(tls *libc.TLS, pFix uintptr, pParse uintptr, iDb int32, zType uintptr, pName uintptr) { var db uintptr _ = db db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) (*TDbFixer)(unsafe.Pointer(pFix)).FpParse = pParse (*TDbFixer)(unsafe.Pointer(pFix)).FzDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName (*TDbFixer)(unsafe.Pointer(pFix)).FpSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema (*TDbFixer)(unsafe.Pointer(pFix)).FzType = zType (*TDbFixer)(unsafe.Pointer(pFix)).FpName = pName (*TDbFixer)(unsafe.Pointer(pFix)).FbTemp = libc.BoolUint8(iDb == libc.Int32FromInt32(1)) (*TDbFixer)(unsafe.Pointer(pFix)).Fw.FpParse = pParse (*TDbFixer)(unsafe.Pointer(pFix)).Fw.FxExprCallback = __ccgo_fp(_fixExprCb) (*TDbFixer)(unsafe.Pointer(pFix)).Fw.FxSelectCallback = __ccgo_fp(_fixSelectCb) (*TDbFixer)(unsafe.Pointer(pFix)).Fw.FxSelectCallback2 = __ccgo_fp(_sqlite3WalkWinDefnDummyCallback) (*TDbFixer)(unsafe.Pointer(pFix)).Fw.FwalkerDepth = 0 (*TDbFixer)(unsafe.Pointer(pFix)).Fw.FeCode = uint16(0) *(*uintptr)(unsafe.Pointer(pFix + 4 + 24)) = pFix } // C documentation // // /* // ** The following set of routines walk through the parse tree and assign // ** a specific database to all table references where the database name // ** was left unspecified in the original SQL statement. The pFix structure // ** must have been initialized by a prior call to sqlite3FixInit(). // ** // ** These routines are used to make sure that an index, trigger, or // ** view in one database does not refer to objects in a different database. // ** (Exception: indices, triggers, and views in the TEMP database are // ** allowed to refer to anything.) If a reference is explicitly made // ** to an object in a different database, an error message is added to // ** pParse->zErrMsg and these routines return non-zero. If everything // ** checks out, these routines return 0. // */ func _sqlite3FixSrcList(tls *libc.TLS, pFix uintptr, pList uintptr) (r int32) { bp := tls.Alloc(80) defer tls.Free(80) var res int32 var _ /* s at bp+0 */ TSelect _ = res res = 0 if pList != 0 { libc.Xmemset(tls, bp, 0, uint32(76)) (*(*TSelect)(unsafe.Pointer(bp))).FpSrc = pList res = _sqlite3WalkSelect(tls, pFix+4, bp) } return res } func _sqlite3FixSelect(tls *libc.TLS, pFix uintptr, pSelect uintptr) (r int32) { return _sqlite3WalkSelect(tls, pFix+4, pSelect) } func _sqlite3FixExpr(tls *libc.TLS, pFix uintptr, pExpr uintptr) (r int32) { return _sqlite3WalkExpr(tls, pFix+4, pExpr) } func _sqlite3FixTriggerStep(tls *libc.TLS, pFix uintptr, pStep uintptr) (r int32) { var pUp uintptr _ = pUp for pStep != 0 { if _sqlite3WalkSelect(tls, pFix+4, (*TTriggerStep)(unsafe.Pointer(pStep)).FpSelect) != 0 || _sqlite3WalkExpr(tls, pFix+4, (*TTriggerStep)(unsafe.Pointer(pStep)).FpWhere) != 0 || _sqlite3WalkExprList(tls, pFix+4, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList) != 0 || _sqlite3FixSrcList(tls, pFix, (*TTriggerStep)(unsafe.Pointer(pStep)).FpFrom) != 0 { return int32(1) } pUp = (*TTriggerStep)(unsafe.Pointer(pStep)).FpUpsert for { if !(pUp != 0) { break } if _sqlite3WalkExprList(tls, pFix+4, (*TUpsert)(unsafe.Pointer(pUp)).FpUpsertTarget) != 0 || _sqlite3WalkExpr(tls, pFix+4, (*TUpsert)(unsafe.Pointer(pUp)).FpUpsertTargetWhere) != 0 || _sqlite3WalkExprList(tls, pFix+4, (*TUpsert)(unsafe.Pointer(pUp)).FpUpsertSet) != 0 || _sqlite3WalkExpr(tls, pFix+4, (*TUpsert)(unsafe.Pointer(pUp)).FpUpsertWhere) != 0 { return int32(1) } goto _1 _1: ; pUp = (*TUpsert)(unsafe.Pointer(pUp)).FpNextUpsert } pStep = (*TTriggerStep)(unsafe.Pointer(pStep)).FpNext } return 0 } /************** End of attach.c **********************************************/ /************** Begin file auth.c ********************************************/ /* ** 2003 January 11 ** ** 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 contains code used to implement the sqlite3_set_authorizer() ** API. This facility is an optional feature of the library. Embedded ** systems that do not need this facility may omit it by recompiling ** the library with -DSQLITE_OMIT_AUTHORIZATION=1 */ /* #include "sqliteInt.h" */ /* ** All of the code in this file may be omitted by defining a single ** macro. */ // C documentation // // /* // ** Set or clear the access authorization function. // ** // ** The access authorization function is be called during the compilation // ** phase to verify that the user has read and/or write access permission on // ** various fields of the database. The first argument to the auth function // ** is a copy of the 3rd argument to this routine. The second argument // ** to the auth function is one of these constants: // ** // ** SQLITE_CREATE_INDEX // ** SQLITE_CREATE_TABLE // ** SQLITE_CREATE_TEMP_INDEX // ** SQLITE_CREATE_TEMP_TABLE // ** SQLITE_CREATE_TEMP_TRIGGER // ** SQLITE_CREATE_TEMP_VIEW // ** SQLITE_CREATE_TRIGGER // ** SQLITE_CREATE_VIEW // ** SQLITE_DELETE // ** SQLITE_DROP_INDEX // ** SQLITE_DROP_TABLE // ** SQLITE_DROP_TEMP_INDEX // ** SQLITE_DROP_TEMP_TABLE // ** SQLITE_DROP_TEMP_TRIGGER // ** SQLITE_DROP_TEMP_VIEW // ** SQLITE_DROP_TRIGGER // ** SQLITE_DROP_VIEW // ** SQLITE_INSERT // ** SQLITE_PRAGMA // ** SQLITE_READ // ** SQLITE_SELECT // ** SQLITE_TRANSACTION // ** SQLITE_UPDATE // ** // ** The third and fourth arguments to the auth function are the name of // ** the table and the column that are being accessed. The auth function // ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If // ** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY // ** means that the SQL statement will never-run - the sqlite3_exec() call // ** will return with an error. SQLITE_IGNORE means that the SQL statement // ** should run but attempts to read the specified column will return NULL // ** and attempts to write the column will be ignored. // ** // ** Setting the auth function to NULL disables this hook. The default // ** setting of the auth function is NULL. // */ func Xsqlite3_set_authorizer(tls *libc.TLS, db uintptr, xAuth uintptr, pArg uintptr) (r int32) { Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth (*Tsqlite3)(unsafe.Pointer(db)).FpAuthArg = pArg if (*Tsqlite3)(unsafe.Pointer(db)).FxAuth != 0 { _sqlite3ExpirePreparedStatements(tls, db, int32(1)) } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return SQLITE_OK } // C documentation // // /* // ** Write an error message into pParse->zErrMsg that explains that the // ** user-supplied authorization function returned an illegal value. // */ func _sqliteAuthBadReturnCode(tls *libc.TLS, pParse uintptr) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12358, 0) (*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_ERROR) } // C documentation // // /* // ** Invoke the authorization callback for permission to read column zCol from // ** table zTab in database zDb. This function assumes that an authorization // ** callback has been registered (i.e. that sqlite3.xAuth is not NULL). // ** // ** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed // ** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE // ** is treated as SQLITE_DENY. In this case an error is left in pParse. // */ func _sqlite3AuthReadCol(tls *libc.TLS, pParse uintptr, zTab uintptr, zCol uintptr, iDb int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var db, z, zDb uintptr var rc int32 _, _, _, _ = db, rc, z, zDb db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Database handle */ zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName /* Auth callback return code */ if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 { return SQLITE_OK } rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxAuth})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpAuthArg, int32(SQLITE_READ), zTab, zCol, zDb, (*TParse)(unsafe.Pointer(pParse)).FzAuthContext) if rc == int32(SQLITE_DENY) { z = Xsqlite3_mprintf(tls, __ccgo_ts+12381, libc.VaList(bp+8, zTab, zCol)) if (*Tsqlite3)(unsafe.Pointer(db)).FnDb > int32(2) || iDb != 0 { z = Xsqlite3_mprintf(tls, __ccgo_ts+12387, libc.VaList(bp+8, zDb, z)) } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12393, libc.VaList(bp+8, z)) (*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_AUTH) } else { if rc != int32(SQLITE_IGNORE) && rc != SQLITE_OK { _sqliteAuthBadReturnCode(tls, pParse) } } return rc } // C documentation // // /* // ** The pExpr should be a TK_COLUMN expression. The table referred to // ** is in pTabList or else it is the NEW or OLD table of a trigger. // ** Check to see if it is OK to read this particular column. // ** // ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN // ** instruction into a TK_NULL. If the auth function returns SQLITE_DENY, // ** then generate an error. // */ func _sqlite3AuthRead(tls *libc.TLS, pParse uintptr, pExpr uintptr, pSchema uintptr, pTabList uintptr) { var iCol, iDb, iSrc int32 var pTab, zCol uintptr _, _, _, _, _ = iCol, iDb, iSrc, pTab, zCol pTab = uintptr(0) /* Index of column in table */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSchema) if iDb < 0 { /* An attempt to read a column out of a subquery or other ** temporary table. */ return } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_TRIGGER) { pTab = (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab } else { _ = libc.Int32FromInt32(0) iSrc = 0 for { if !(iSrc < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) { break } if (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(iSrc)*72))).FiCursor { pTab = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(iSrc)*72))).FpTab break } goto _1 _1: ; iSrc++ } } iCol = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) if pTab == uintptr(0) { return } if iCol >= 0 { _ = libc.Int32FromInt32(0) zCol = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName } else { if int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) >= 0 { _ = libc.Int32FromInt32(0) zCol = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr((*TTable)(unsafe.Pointer(pTab)).FiPKey)*12))).FzCnName } else { zCol = __ccgo_ts + 7905 } } _ = libc.Int32FromInt32(0) if int32(SQLITE_IGNORE) == _sqlite3AuthReadCol(tls, pParse, (*TTable)(unsafe.Pointer(pTab)).FzName, zCol, iDb) { (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_NULL) } } // C documentation // // /* // ** Do an authorization check using the code and arguments given. Return // ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY // ** is returned, then the error count and error message in pParse are // ** modified appropriately. // */ func _sqlite3AuthCheck(tls *libc.TLS, pParse uintptr, code int32, zArg1 uintptr, zArg2 uintptr, zArg3 uintptr) (r int32) { var db uintptr var rc int32 _, _ = db, rc db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Don't do any authorization checks if the database is initializing ** or if the parser is being invoked from within sqlite3_declare_vtab. */ _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FxAuth == uintptr(0) || (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 || int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != PARSE_MODE_NORMAL { return SQLITE_OK } /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the ** callback are either NULL pointers or zero-terminated strings that ** contain additional details about the action to be authorized. ** ** The following testcase() macros show that any of the 3rd through 6th ** parameters can be either NULL or a string. */ rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxAuth})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpAuthArg, code, zArg1, zArg2, zArg3, (*TParse)(unsafe.Pointer(pParse)).FzAuthContext) if rc == int32(SQLITE_DENY) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12420, 0) (*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_AUTH) } else { if rc != SQLITE_OK && rc != int32(SQLITE_IGNORE) { rc = int32(SQLITE_DENY) _sqliteAuthBadReturnCode(tls, pParse) } } return rc } // C documentation // // /* // ** Push an authorization context. After this routine is called, the // ** zArg3 argument to authorization callbacks will be zContext until // ** popped. Or if pParse==0, this routine is a no-op. // */ func _sqlite3AuthContextPush(tls *libc.TLS, pParse uintptr, pContext uintptr, zContext uintptr) { _ = libc.Int32FromInt32(0) (*TAuthContext)(unsafe.Pointer(pContext)).FpParse = pParse (*TAuthContext)(unsafe.Pointer(pContext)).FzAuthContext = (*TParse)(unsafe.Pointer(pParse)).FzAuthContext (*TParse)(unsafe.Pointer(pParse)).FzAuthContext = zContext } // C documentation // // /* // ** Pop an authorization context that was previously pushed // ** by sqlite3AuthContextPush // */ func _sqlite3AuthContextPop(tls *libc.TLS, pContext uintptr) { if (*TAuthContext)(unsafe.Pointer(pContext)).FpParse != 0 { (*TParse)(unsafe.Pointer((*TAuthContext)(unsafe.Pointer(pContext)).FpParse)).FzAuthContext = (*TAuthContext)(unsafe.Pointer(pContext)).FzAuthContext (*TAuthContext)(unsafe.Pointer(pContext)).FpParse = uintptr(0) } } /************** End of auth.c ************************************************/ /************** Begin file build.c *******************************************/ /* ** 2001 September 15 ** ** 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 contains C code routines that are called by the SQLite parser ** when syntax rules are reduced. The routines in this file handle the ** following kinds of SQL syntax: ** ** CREATE TABLE ** DROP TABLE ** CREATE INDEX ** DROP INDEX ** creating ID lists ** BEGIN TRANSACTION ** COMMIT ** ROLLBACK */ /* #include "sqliteInt.h" */ /* ** The TableLock structure is only used by the sqlite3TableLock() and ** codeTableLocks() functions. */ type TTableLock1 = struct { FiDb int32 FiTab TPgno FisWriteLock Tu8 FzLockName uintptr } type TableLock1 = TTableLock1 // C documentation // // /* // ** Record the fact that we want to lock a table at run-time. // ** // ** The table to be locked has root page iTab and is found in database iDb. // ** A read or a write lock can be taken depending on isWritelock. // ** // ** This routine just records the fact that the lock is desired. The // ** code to make the lock occur is generated by a later call to // ** codeTableLocks() which occurs during sqlite3FinishCoding(). // */ func _lockTable(tls *libc.TLS, pParse uintptr, iDb int32, iTab TPgno, isWriteLock Tu8, zName uintptr) { var i, nBytes, v3 int32 var p, pToplevel, v1, v4 uintptr _, _, _, _, _, _, _ = i, nBytes, p, pToplevel, v1, v3, v4 _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 { v1 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel } else { v1 = pParse } pToplevel = v1 i = 0 for { if !(i < (*TParse)(unsafe.Pointer(pToplevel)).FnTableLock) { break } p = (*TParse)(unsafe.Pointer(pToplevel)).FaTableLock + uintptr(i)*16 if (*TTableLock)(unsafe.Pointer(p)).FiDb == iDb && (*TTableLock)(unsafe.Pointer(p)).FiTab == iTab { (*TTableLock)(unsafe.Pointer(p)).FisWriteLock = libc.BoolUint8((*TTableLock)(unsafe.Pointer(p)).FisWriteLock != 0 || isWriteLock != 0) return } goto _2 _2: ; i++ } nBytes = int32(uint32(16) * uint32((*TParse)(unsafe.Pointer(pToplevel)).FnTableLock+libc.Int32FromInt32(1))) (*TParse)(unsafe.Pointer(pToplevel)).FaTableLock = _sqlite3DbReallocOrFree(tls, (*TParse)(unsafe.Pointer(pToplevel)).Fdb, (*TParse)(unsafe.Pointer(pToplevel)).FaTableLock, uint64(nBytes)) if (*TParse)(unsafe.Pointer(pToplevel)).FaTableLock != 0 { v4 = pToplevel + 116 v3 = *(*int32)(unsafe.Pointer(v4)) *(*int32)(unsafe.Pointer(v4))++ p = (*TParse)(unsafe.Pointer(pToplevel)).FaTableLock + uintptr(v3)*16 (*TTableLock)(unsafe.Pointer(p)).FiDb = iDb (*TTableLock)(unsafe.Pointer(p)).FiTab = iTab (*TTableLock)(unsafe.Pointer(p)).FisWriteLock = isWriteLock (*TTableLock)(unsafe.Pointer(p)).FzLockName = zName } else { (*TParse)(unsafe.Pointer(pToplevel)).FnTableLock = 0 _sqlite3OomFault(tls, (*TParse)(unsafe.Pointer(pToplevel)).Fdb) } } func _sqlite3TableLock(tls *libc.TLS, pParse uintptr, iDb int32, iTab TPgno, isWriteLock Tu8, zName uintptr) { if iDb == int32(1) { return } if !(_sqlite3BtreeSharable(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + uintptr(iDb)*16))).FpBt) != 0) { return } _lockTable(tls, pParse, iDb, iTab, isWriteLock, zName) } // C documentation // // /* // ** Code an OP_TableLock instruction for each table locked by the // ** statement (configured by calls to sqlite3TableLock()). // */ func _codeTableLocks(tls *libc.TLS, pParse uintptr) { var i, p1 int32 var p, pVdbe uintptr _, _, _, _ = i, p, p1, pVdbe pVdbe = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*TParse)(unsafe.Pointer(pParse)).FnTableLock) { break } p = (*TParse)(unsafe.Pointer(pParse)).FaTableLock + uintptr(i)*16 p1 = (*TTableLock)(unsafe.Pointer(p)).FiDb _sqlite3VdbeAddOp4(tls, pVdbe, int32(OP_TableLock), p1, int32((*TTableLock)(unsafe.Pointer(p)).FiTab), int32((*TTableLock)(unsafe.Pointer(p)).FisWriteLock), (*TTableLock)(unsafe.Pointer(p)).FzLockName, -int32(1)) goto _1 _1: ; i++ } } /* ** Return TRUE if the given yDbMask object is empty - if it contains no ** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero() ** macros when SQLITE_MAX_ATTACHED is greater than 30. */ // C documentation // // /* // ** This routine is called after a single SQL statement has been // ** parsed and a VDBE program to execute that statement has been // ** prepared. This routine puts the finishing touches on the // ** VDBE program and resets the pParse structure for the next // ** parse. // ** // ** Note that if an error occurred, it might be the case that // ** no VDBE code was generated. // */ func _sqlite3FinishCoding(tls *libc.TLS, pParse uintptr) { var addrRewind, i, iDb, reg, v2 int32 var db, pEL, pRet, pReturning, pSchema, v, vtab uintptr _, _, _, _, _, _, _, _, _, _, _, _ = addrRewind, db, i, iDb, pEL, pRet, pReturning, pSchema, reg, v, vtab, v2 _ = libc.Int32FromInt32(0) db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).Fnested != 0 { return } if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { (*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM) } return } _ = libc.Int32FromInt32(0) /* Begin by generating some termination code at the end of the ** vdbe program */ v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe if v == uintptr(0) { if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 { (*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_DONE) return } v = _sqlite3GetVdbe(tls, pParse) if v == uintptr(0) { (*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_ERROR) } } _ = libc.Int32FromInt32(0) if v != 0 { if (*TParse)(unsafe.Pointer(pParse)).FbReturning != 0 { pReturning = *(*uintptr)(unsafe.Pointer(pParse + 144)) if (*TReturning)(unsafe.Pointer(pReturning)).FnRetCol != 0 { _sqlite3VdbeAddOp0(tls, v, int32(OP_FkCheck)) addrRewind = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), (*TReturning)(unsafe.Pointer(pReturning)).FiRetCur) reg = (*TReturning)(unsafe.Pointer(pReturning)).FiRetReg i = 0 for { if !(i < (*TReturning)(unsafe.Pointer(pReturning)).FnRetCol) { break } _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TReturning)(unsafe.Pointer(pReturning)).FiRetCur, i, reg+i) goto _1 _1: ; i++ } _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), reg, i) _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), (*TReturning)(unsafe.Pointer(pReturning)).FiRetCur, addrRewind+int32(1)) _sqlite3VdbeJumpHere(tls, v, addrRewind) } } _sqlite3VdbeAddOp0(tls, v, int32(OP_Halt)) /* The cookie mask contains one bit for each database file open. ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are ** set for each database that is used. Generate code to start a ** transaction on each used database and to verify the schema cookie ** on each used database. */ _ = libc.Int32FromInt32(0) _sqlite3VdbeJumpHere(tls, v, 0) _ = libc.Int32FromInt32(0) iDb = 0 for { if libc.BoolInt32((*TParse)(unsafe.Pointer(pParse)).FcookieMask&(libc.Uint32FromInt32(1)<= (*Tsqlite3)(unsafe.Pointer(db)).FnDb { /* No match against the official names. But always match "main" ** to schema 0 as a legacy fallback. */ if _sqlite3StrICmp(tls, zDatabase, __ccgo_ts+6585) == 0 { i = 0 } else { return uintptr(0) } } p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema+8, zName) if p == uintptr(0) && Xsqlite3_strnicmp(tls, zName, __ccgo_ts+6525, int32(7)) == 0 { if i == int32(1) { if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6552+7) == 0 || _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6571+7) == 0 || _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6066+7) == 0 { p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema+8, __ccgo_ts+6533) } } else { if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6571+7) == 0 { p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema+8, __ccgo_ts+6066) } } } } else { /* Match against TEMP first */ p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema+8, zName) if p != 0 { return p } /* The main database is second */ p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema+8, zName) if p != 0 { return p } /* Attached databases are in order of attachment */ i = int32(2) for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } _ = libc.Int32FromInt32(0) p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema+8, zName) if p != 0 { break } goto _2 _2: ; i++ } if p == uintptr(0) && Xsqlite3_strnicmp(tls, zName, __ccgo_ts+6525, int32(7)) == 0 { if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6571+7) == 0 { p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema+8, __ccgo_ts+6066) } else { if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6552+7) == 0 { p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema+8, __ccgo_ts+6533) } } } } return p } // C documentation // // /* // ** Locate the in-memory structure that describes a particular database // ** table given the name of that table and (optionally) the name of the // ** database containing the table. Return NULL if not found. Also leave an // ** error message in pParse->zErrMsg. // ** // ** The difference between this routine and sqlite3FindTable() is that this // ** routine leaves an error message in pParse->zErrMsg where // ** sqlite3FindTable() does not. // */ func _sqlite3LocateTable(tls *libc.TLS, pParse uintptr, flags Tu32, zName uintptr, zDbase uintptr) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var db, p, pMod, zMsg, v1 uintptr _, _, _, _, _ = db, p, pMod, zMsg, v1 db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Read the database schema. If an error occurs, leave an error message ** and code in pParse and return NULL. */ if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_SchemaKnownOk) == uint32(0) && SQLITE_OK != _sqlite3ReadSchema(tls, pParse) { return uintptr(0) } p = _sqlite3FindTable(tls, db, zName, zDbase) if p == uintptr(0) { /* If zName is the not the name of a table in the schema created using ** CREATE, then check to see if it is the name of an virtual table that ** can be an eponymous virtual table. */ if int32((*TParse)(unsafe.Pointer(pParse)).FprepFlags)&int32(SQLITE_PREPARE_NO_VTAB) == 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) == 0 { pMod = _sqlite3HashFind(tls, db+404, zName) if pMod == uintptr(0) && Xsqlite3_strnicmp(tls, zName, __ccgo_ts+12435, int32(7)) == 0 { pMod = _sqlite3PragmaVtabRegister(tls, db, zName) } if pMod != 0 && _sqlite3VtabEponymousTableInit(tls, pParse, pMod) != 0 { return (*TModule)(unsafe.Pointer(pMod)).FpEpoTab } } if flags&uint32(LOCATE_NOERR) != 0 { return uintptr(0) } (*TParse)(unsafe.Pointer(pParse)).FcheckSchema = uint8(1) } else { if int32((*TTable)(unsafe.Pointer(p)).FeTabType) == int32(TABTYP_VTAB) && int32((*TParse)(unsafe.Pointer(pParse)).FprepFlags)&int32(SQLITE_PREPARE_NO_VTAB) != 0 { p = uintptr(0) } } if p == uintptr(0) { if flags&uint32(LOCATE_VIEW) != 0 { v1 = __ccgo_ts + 12443 } else { v1 = __ccgo_ts + 12456 } zMsg = v1 if zDbase != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+6789, libc.VaList(bp+8, zMsg, zDbase, zName)) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+6799, libc.VaList(bp+8, zMsg, zName)) } } else { _ = libc.Int32FromInt32(0) } return p } // C documentation // // /* // ** Locate the table identified by *p. // ** // ** This is a wrapper around sqlite3LocateTable(). The difference between // ** sqlite3LocateTable() and this function is that this function restricts // ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be // ** non-NULL if it is part of a view or trigger program definition. See // ** sqlite3FixSrcList() for details. // */ func _sqlite3LocateTableItem(tls *libc.TLS, pParse uintptr, flags Tu32, p uintptr) (r uintptr) { var iDb int32 var zDb uintptr _, _ = iDb, zDb _ = libc.Int32FromInt32(0) if (*TSrcItem)(unsafe.Pointer(p)).FpSchema != 0 { iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TSrcItem)(unsafe.Pointer(p)).FpSchema) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + uintptr(iDb)*16))).FzDbSName } else { zDb = (*TSrcItem)(unsafe.Pointer(p)).FzDatabase } return _sqlite3LocateTable(tls, pParse, flags, (*TSrcItem)(unsafe.Pointer(p)).FzName, zDb) } // C documentation // // /* // ** Return the preferred table name for system tables. Translate legacy // ** names into the new preferred names, as appropriate. // */ func _sqlite3PreferredTableName(tls *libc.TLS, zName uintptr) (r uintptr) { if Xsqlite3_strnicmp(tls, zName, __ccgo_ts+6525, int32(7)) == 0 { if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6066+7) == 0 { return __ccgo_ts + 6571 } if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6533+7) == 0 { return __ccgo_ts + 6552 } } return zName } // C documentation // // /* // ** Locate the in-memory structure that describes // ** a particular index given the name of that index // ** and the name of the database that contains the index. // ** Return NULL if not found. // ** // ** If zDatabase is 0, all databases are searched for the // ** table and the first matching index is returned. (No checking // ** for duplicate index names is done.) The search order is // ** TEMP first, then MAIN, then any auxiliary databases added // ** using the ATTACH command. // */ func _sqlite3FindIndex(tls *libc.TLS, db uintptr, zName uintptr, zDb uintptr) (r uintptr) { var i, j, v2 int32 var p, pSchema uintptr _, _, _, _, _ = i, j, p, pSchema, v2 p = uintptr(0) /* All mutexes are required for schema access. Make sure we hold them. */ _ = libc.Int32FromInt32(0) i = OMIT_TEMPDB for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } if i < int32(2) { v2 = i ^ int32(1) } else { v2 = i } j = v2 /* Search TEMP before MAIN */ pSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(j)*16))).FpSchema _ = libc.Int32FromInt32(0) if zDb != 0 && _sqlite3DbIsNamed(tls, db, j, zDb) == 0 { goto _1 } _ = libc.Int32FromInt32(0) p = _sqlite3HashFind(tls, pSchema+24, zName) if p != 0 { break } goto _1 _1: ; i++ } return p } // C documentation // // /* // ** Reclaim the memory used by an index // */ func _sqlite3FreeIndex(tls *libc.TLS, db uintptr, p uintptr) { _sqlite3DeleteIndexSamples(tls, db, p) _sqlite3ExprDelete(tls, db, (*TIndex)(unsafe.Pointer(p)).FpPartIdxWhere) _sqlite3ExprListDelete(tls, db, (*TIndex)(unsafe.Pointer(p)).FaColExpr) _sqlite3DbFree(tls, db, (*TIndex)(unsafe.Pointer(p)).FzColAff) if int32(uint32(*(*uint16)(unsafe.Pointer(p + 56))&0x10>>4)) != 0 { _sqlite3DbFree(tls, db, (*TIndex)(unsafe.Pointer(p)).FazColl) } Xsqlite3_free(tls, (*TIndex)(unsafe.Pointer(p)).FaiRowEst) _sqlite3DbFree(tls, db, p) } // C documentation // // /* // ** For the index called zIdxName which is found in the database iDb, // ** unlike that index from its Table then remove the index from // ** the index hash table and free all memory structures associated // ** with the index. // */ func _sqlite3UnlinkAndDeleteIndex(tls *libc.TLS, db uintptr, iDb int32, zIdxName uintptr) { var p, pHash, pIndex uintptr _, _, _ = p, pHash, pIndex _ = libc.Int32FromInt32(0) pHash = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema + 24 pIndex = _sqlite3HashInsert(tls, pHash, zIdxName, uintptr(0)) if pIndex != 0 { if (*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FpTable)).FpIndex == pIndex { (*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FpTable)).FpIndex = (*TIndex)(unsafe.Pointer(pIndex)).FpNext } else { /* Justification of ALWAYS(); The index must be on the list of ** indices. */ p = (*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FpTable)).FpIndex for p != 0 && (*TIndex)(unsafe.Pointer(p)).FpNext != pIndex { p = (*TIndex)(unsafe.Pointer(p)).FpNext } if p != 0 && (*TIndex)(unsafe.Pointer(p)).FpNext == pIndex { (*TIndex)(unsafe.Pointer(p)).FpNext = (*TIndex)(unsafe.Pointer(pIndex)).FpNext } } _sqlite3FreeIndex(tls, db, pIndex) } *(*Tu32)(unsafe.Pointer(db + 24)) |= uint32(DBFLAG_SchemaChange) } // C documentation // // /* // ** Look through the list of open database files in db->aDb[] and if // ** any have been closed, remove them from the list. Reallocate the // ** db->aDb[] structure to a smaller size, if possible. // ** // ** Entry 0 (the "main" database) and entry 1 (the "temp" database) // ** are never candidates for being collapsed. // */ func _sqlite3CollapseDatabaseArray(tls *libc.TLS, db uintptr) { var i, j, v2 int32 var pDb uintptr _, _, _, _ = i, j, pDb, v2 v2 = libc.Int32FromInt32(2) j = v2 i = v2 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16 if (*TDb1)(unsafe.Pointer(pDb)).FpBt == uintptr(0) { _sqlite3DbFree(tls, db, (*TDb1)(unsafe.Pointer(pDb)).FzDbSName) (*TDb1)(unsafe.Pointer(pDb)).FzDbSName = uintptr(0) goto _1 } if j < i { *(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(j)*16)) = *(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16)) } j++ goto _1 _1: ; i++ } (*Tsqlite3)(unsafe.Pointer(db)).FnDb = j if (*Tsqlite3)(unsafe.Pointer(db)).FnDb <= int32(2) && (*Tsqlite3)(unsafe.Pointer(db)).FaDb != db+476 { libc.Xmemcpy(tls, db+476, (*Tsqlite3)(unsafe.Pointer(db)).FaDb, libc.Uint32FromInt32(2)*libc.Uint32FromInt64(16)) _sqlite3DbFree(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).FaDb) (*Tsqlite3)(unsafe.Pointer(db)).FaDb = db + 476 } } // C documentation // // /* // ** Reset the schema for the database at index iDb. Also reset the // ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero. // ** Deferred resets may be run by calling with iDb<0. // */ func _sqlite3ResetOneSchema(tls *libc.TLS, db uintptr, iDb int32) { var i int32 var p1, p2 uintptr _, _, _ = i, p1, p2 _ = libc.Int32FromInt32(0) if iDb >= 0 { _ = libc.Int32FromInt32(0) p1 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema + 78 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(DB_ResetWanted)) p2 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema + 78 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(DB_ResetWanted)) *(*Tu32)(unsafe.Pointer(db + 24)) &= uint32(^libc.Int32FromInt32(DBFLAG_SchemaKnownOk)) } if (*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock == uint32(0) { i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } if int32((*TSchema)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema)).FschemaFlags)&int32(DB_ResetWanted) == int32(DB_ResetWanted) { _sqlite3SchemaClear(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema) } goto _3 _3: ; i++ } } } // C documentation // // /* // ** Erase all schema information from all attached databases (including // ** "main" and "temp") for a single database connection. // */ func _sqlite3ResetAllSchemasOfConnection(tls *libc.TLS, db uintptr) { var i int32 var pDb, p2 uintptr _, _, _ = i, pDb, p2 _sqlite3BtreeEnterAll(tls, db) i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16 if (*TDb)(unsafe.Pointer(pDb)).FpSchema != 0 { if (*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock == uint32(0) { _sqlite3SchemaClear(tls, (*TDb)(unsafe.Pointer(pDb)).FpSchema) } else { p2 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema + 78 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(DB_ResetWanted)) } } goto _1 _1: ; i++ } *(*Tu32)(unsafe.Pointer(db + 24)) &= uint32(^(libc.Int32FromInt32(DBFLAG_SchemaChange) | libc.Int32FromInt32(DBFLAG_SchemaKnownOk))) _sqlite3VtabUnlockList(tls, db) _sqlite3BtreeLeaveAll(tls, db) if (*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock == uint32(0) { _sqlite3CollapseDatabaseArray(tls, db) } } // C documentation // // /* // ** This routine is called when a commit occurs. // */ func _sqlite3CommitInternalChanges(tls *libc.TLS, db uintptr) { *(*Tu32)(unsafe.Pointer(db + 24)) &= uint32(^libc.Int32FromInt32(DBFLAG_SchemaChange)) } // C documentation // // /* // ** Set the expression associated with a column. This is usually // ** the DEFAULT value, but might also be the expression that computes // ** the value for a generated column. // */ func _sqlite3ColumnSetExpr(tls *libc.TLS, pParse uintptr, pTab uintptr, pCol uintptr, pExpr uintptr) { var pList uintptr var v1 int32 _, _ = pList, v1 _ = libc.Int32FromInt32(0) pList = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpDfltList if int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt) == 0 || pList == uintptr(0) || (*TExprList)(unsafe.Pointer(pList)).FnExpr < int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt) { if pList == uintptr(0) { v1 = int32(1) } else { v1 = (*TExprList)(unsafe.Pointer(pList)).FnExpr + int32(1) } (*TColumn)(unsafe.Pointer(pCol)).FiDflt = uint16(v1) (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpDfltList = _sqlite3ExprListAppend(tls, pParse, pList, pExpr) } else { _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt)-int32(1))*20))).FpExpr) (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt)-int32(1))*20))).FpExpr = pExpr } } // C documentation // // /* // ** Return the expression associated with a column. The expression might be // ** the DEFAULT clause or the AS clause of a generated column. // ** Return NULL if the column has no associated expression. // */ func _sqlite3ColumnExpr(tls *libc.TLS, pTab uintptr, pCol uintptr) (r uintptr) { if int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt) == 0 { return uintptr(0) } if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) { return uintptr(0) } if (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpDfltList == uintptr(0) { return uintptr(0) } if (*TExprList)(unsafe.Pointer((*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpDfltList)).FnExpr < int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt) { return uintptr(0) } return (*(*TExprList_item)(unsafe.Pointer((*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpDfltList + 8 + uintptr(int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt)-int32(1))*20))).FpExpr } // C documentation // // /* // ** Set the collating sequence name for a column. // */ func _sqlite3ColumnSetColl(tls *libc.TLS, db uintptr, pCol uintptr, zColl uintptr) { var n, nColl Ti64 var zNew, p1 uintptr _, _, _, _ = n, nColl, zNew, p1 _ = libc.Int32FromInt32(0) n = int64(_sqlite3Strlen30(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName) + int32(1)) if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_HASTYPE) != 0 { n += int64(_sqlite3Strlen30(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName+uintptr(n)) + int32(1)) } nColl = int64(_sqlite3Strlen30(tls, zColl) + int32(1)) zNew = _sqlite3DbRealloc(tls, db, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, uint64(nColl+n)) if zNew != 0 { (*TColumn)(unsafe.Pointer(pCol)).FzCnName = zNew libc.Xmemcpy(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName+uintptr(n), zColl, uint32(nColl)) p1 = pCol + 10 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(COLFLAG_HASCOLL)) } } // C documentation // // /* // ** Return the collating sequence name for a column // */ func _sqlite3ColumnColl(tls *libc.TLS, pCol uintptr) (r uintptr) { var z uintptr _ = z if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_HASCOLL) == 0 { return uintptr(0) } z = (*TColumn)(unsafe.Pointer(pCol)).FzCnName for *(*uint8)(unsafe.Pointer(z)) != 0 { z++ } if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_HASTYPE) != 0 { for cond := true; cond; cond = *(*uint8)(unsafe.Pointer(z)) != 0 { z++ } } return z + uintptr(1) } // C documentation // // /* // ** Delete memory allocated for the column names of a table or view (the // ** Table.aCol[] array). // */ func _sqlite3DeleteColumnNames(tls *libc.TLS, db uintptr, pTable uintptr) { var i int32 var pCol, v1 uintptr _, _, _ = i, pCol, v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) v1 = (*TTable)(unsafe.Pointer(pTable)).FaCol pCol = v1 if v1 != uintptr(0) { i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTable)).FnCol)) { break } _ = libc.Int32FromInt32(0) _sqlite3DbFree(tls, db, (*TColumn)(unsafe.Pointer(pCol)).FzCnName) goto _2 _2: ; i++ pCol += 12 } _sqlite3DbNNFreeNN(tls, db, (*TTable)(unsafe.Pointer(pTable)).FaCol) if int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == TABTYP_NORM { _sqlite3ExprListDelete(tls, db, (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTable + 44))).FpDfltList) } if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0) { (*TTable)(unsafe.Pointer(pTable)).FaCol = uintptr(0) (*TTable)(unsafe.Pointer(pTable)).FnCol = 0 if int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == TABTYP_NORM { (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTable + 44))).FpDfltList = uintptr(0) } } } } // C documentation // // /* // ** Remove the memory data structures associated with the given // ** Table. No changes are made to disk by this routine. // ** // ** This routine just deletes the data structure. It does not unlink // ** the table data structure from the hash table. But it does destroy // ** memory structures of the indices and foreign keys associated with // ** the table. // ** // ** The db parameter is optional. It is needed if the Table object // ** contains lookaside memory. (Table objects in the schema do not use // ** lookaside memory, but some ephemeral Table objects do.) Or the // ** db parameter can be used with db->pnBytesFreed to measure the memory // ** used by the Table object. // */ func _deleteTable(tls *libc.TLS, db uintptr, pTable uintptr) { var pIndex, pNext, zName uintptr _, _, _ = pIndex, pNext, zName /* Delete all indices associated with this table. */ pIndex = (*TTable)(unsafe.Pointer(pTable)).FpIndex for { if !(pIndex != 0) { break } pNext = (*TIndex)(unsafe.Pointer(pIndex)).FpNext _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0) && !(int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) { zName = (*TIndex)(unsafe.Pointer(pIndex)).FzName _sqlite3HashInsert(tls, (*TIndex)(unsafe.Pointer(pIndex)).FpSchema+24, zName, uintptr(0)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } _sqlite3FreeIndex(tls, db, pIndex) goto _1 _1: ; pIndex = pNext } if int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == TABTYP_NORM { _sqlite3FkDelete(tls, db, pTable) } else { if int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == int32(TABTYP_VTAB) { _sqlite3VtabClear(tls, db, pTable) } else { _ = libc.Int32FromInt32(0) _sqlite3SelectDelete(tls, db, (*(*struct { FpSelect uintptr })(unsafe.Pointer(&(*TTable)(unsafe.Pointer(pTable)).Fu))).FpSelect) } } /* Delete the Table structure itself. */ _sqlite3DeleteColumnNames(tls, db, pTable) _sqlite3DbFree(tls, db, (*TTable)(unsafe.Pointer(pTable)).FzName) _sqlite3DbFree(tls, db, (*TTable)(unsafe.Pointer(pTable)).FzColAff) _sqlite3ExprListDelete(tls, db, (*TTable)(unsafe.Pointer(pTable)).FpCheck) _sqlite3DbFree(tls, db, pTable) /* Verify that no lookaside memory was used by schema tables */ _ = libc.Int32FromInt32(0) } func _sqlite3DeleteTable(tls *libc.TLS, db uintptr, pTable uintptr) { var v1 Tu32 var v2 uintptr var v3 bool _, _, _ = v1, v2, v3 /* Do not delete the table until the reference count reaches zero. */ _ = libc.Int32FromInt32(0) if !(pTable != 0) { return } if v3 = (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0); v3 { v2 = pTable + 24 *(*Tu32)(unsafe.Pointer(v2))-- v1 = *(*Tu32)(unsafe.Pointer(v2)) } if v3 && v1 > uint32(0) { return } _deleteTable(tls, db, pTable) } func _sqlite3DeleteTableGeneric(tls *libc.TLS, db uintptr, pTable uintptr) { _sqlite3DeleteTable(tls, db, pTable) } // C documentation // // /* // ** Unlink the given table from the hash tables and the delete the // ** table structure with all its indices and foreign keys. // */ func _sqlite3UnlinkAndDeleteTable(tls *libc.TLS, db uintptr, iDb int32, zTabName uintptr) { var p, pDb uintptr _, _ = p, pDb _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Zero-length table names are allowed */ pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16 p = _sqlite3HashInsert(tls, (*TDb)(unsafe.Pointer(pDb)).FpSchema+8, zTabName, uintptr(0)) _sqlite3DeleteTable(tls, db, p) *(*Tu32)(unsafe.Pointer(db + 24)) |= uint32(DBFLAG_SchemaChange) } // C documentation // // /* // ** Given a token, return a string that consists of the text of that // ** token. Space to hold the returned string // ** is obtained from sqliteMalloc() and must be freed by the calling // ** function. // ** // ** Any quotation marks (ex: "name", 'name', [name], or `name`) that // ** surround the body of the token are removed. // ** // ** Tokens are often just pointers into the original SQL text and so // ** are not \000 terminated and are not persistent. The returned string // ** is \000 terminated and is persistent. // */ func _sqlite3NameFromToken(tls *libc.TLS, db uintptr, pName uintptr) (r uintptr) { var zName uintptr _ = zName if pName != 0 { zName = _sqlite3DbStrNDup(tls, db, (*TToken)(unsafe.Pointer(pName)).Fz, uint64((*TToken)(unsafe.Pointer(pName)).Fn)) _sqlite3Dequote(tls, zName) } else { zName = uintptr(0) } return zName } // C documentation // // /* // ** Open the sqlite_schema table stored in database number iDb for // ** writing. The table is opened using cursor 0. // */ func _sqlite3OpenSchemaTable(tls *libc.TLS, p uintptr, iDb int32) { var v uintptr _ = v v = _sqlite3GetVdbe(tls, p) _sqlite3TableLock(tls, p, iDb, uint32(SCHEMA_ROOT), uint8(1), __ccgo_ts+6066) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_OpenWrite), 0, int32(SCHEMA_ROOT), iDb, int32(5)) if (*TParse)(unsafe.Pointer(p)).FnTab == 0 { (*TParse)(unsafe.Pointer(p)).FnTab = int32(1) } } // C documentation // // /* // ** Parameter zName points to a nul-terminated buffer containing the name // ** of a database ("main", "temp" or the name of an attached db). This // ** function returns the index of the named database in db->aDb[], or // ** -1 if the named db cannot be found. // */ func _sqlite3FindDbName(tls *libc.TLS, db uintptr, zName uintptr) (r int32) { var i int32 var pDb uintptr _, _ = i, pDb i = -int32(1) /* Database number */ if zName != 0 { i = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1) pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16 for { if !(i >= 0) { break } if 0 == Xsqlite3_stricmp(tls, (*TDb)(unsafe.Pointer(pDb)).FzDbSName, zName) { break } /* "main" is always an acceptable alias for the primary database ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */ if i == 0 && 0 == Xsqlite3_stricmp(tls, __ccgo_ts+6585, zName) { break } goto _1 _1: ; i-- pDb -= 16 } } return i } // C documentation // // /* // ** The token *pName contains the name of a database (either "main" or // ** "temp" or the name of an attached db). This routine returns the // ** index of the named database in db->aDb[], or -1 if the named db // ** does not exist. // */ func _sqlite3FindDb(tls *libc.TLS, db uintptr, pName uintptr) (r int32) { var i int32 var zName uintptr _, _ = i, zName /* Name we are searching for */ zName = _sqlite3NameFromToken(tls, db, pName) i = _sqlite3FindDbName(tls, db, zName) _sqlite3DbFree(tls, db, zName) return i } // C documentation // // /* The table or view or trigger name is passed to this routine via tokens // ** pName1 and pName2. If the table name was fully qualified, for example: // ** // ** CREATE TABLE xxx.yyy (...); // ** // ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if // ** the table name is not fully qualified, i.e.: // ** // ** CREATE TABLE yyy(...); // ** // ** Then pName1 is set to "yyy" and pName2 is "". // ** // ** This routine sets the *ppUnqual pointer to point at the token (pName1 or // ** pName2) that stores the unqualified table name. The index of the // ** database "xxx" is returned. // */ func _sqlite3TwoPartName(tls *libc.TLS, pParse uintptr, pName1 uintptr, pName2 uintptr, pUnqual uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var db uintptr var iDb int32 _, _ = db, iDb /* Database holding the object */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) if (*TToken)(unsafe.Pointer(pName2)).Fn > uint32(0) { if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12470, 0) return -int32(1) } *(*uintptr)(unsafe.Pointer(pUnqual)) = pName2 iDb = _sqlite3FindDb(tls, db, pName1) if iDb < 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12487, libc.VaList(bp+8, pName1)) return -int32(1) } } else { _ = libc.Int32FromInt32(0) iDb = int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb) *(*uintptr)(unsafe.Pointer(pUnqual)) = pName1 } return iDb } // C documentation // // /* // ** True if PRAGMA writable_schema is ON // */ func _sqlite3WritableSchema(tls *libc.TLS, db uintptr) (r int32) { return libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(libc.Int32FromInt32(SQLITE_WriteSchema)|libc.Int32FromInt32(SQLITE_Defensive)) == uint64(SQLITE_WriteSchema)) } // C documentation // // /* // ** This routine is used to check if the UTF-8 string zName is a legal // ** unqualified name for a new schema object (table, index, view or // ** trigger). All names are legal except those that begin with the string // ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace // ** is reserved for internal use. // ** // ** When parsing the sqlite_schema table, this routine also checks to // ** make sure the "type", "name", and "tbl_name" columns are consistent // ** with the SQL. // */ func _sqlite3CheckObjectName(tls *libc.TLS, pParse uintptr, zName uintptr, zType uintptr, zTblName uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var db uintptr _ = db db = (*TParse)(unsafe.Pointer(pParse)).Fdb if _sqlite3WritableSchema(tls, db) != 0 || int32(uint32(*(*uint8)(unsafe.Pointer(db + 172 + 8))&0x2>>1)) != 0 || !(_sqlite3Config.FbExtraSchemaChecks != 0) { /* Skip these error checks for writable_schema=ON */ return SQLITE_OK } if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 { if Xsqlite3_stricmp(tls, zType, *(*uintptr)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FazInit))) != 0 || Xsqlite3_stricmp(tls, zName, *(*uintptr)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FazInit + 1*4))) != 0 || Xsqlite3_stricmp(tls, zTblName, *(*uintptr)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FazInit + 2*4))) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+1648, 0) /* corruptSchema() will supply the error */ return int32(SQLITE_ERROR) } } else { if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 && 0 == Xsqlite3_strnicmp(tls, zName, __ccgo_ts+6525, int32(7)) || _sqlite3ReadOnlyShadowTables(tls, db) != 0 && _sqlite3ShadowTableName(tls, db, zName) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12507, libc.VaList(bp+8, zName)) return int32(SQLITE_ERROR) } } return SQLITE_OK } // C documentation // // /* // ** Return the PRIMARY KEY index of a table // */ func _sqlite3PrimaryKeyIndex(tls *libc.TLS, pTab uintptr) (r uintptr) { var p uintptr _ = p p = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(p != 0 && !(int32(uint32(*(*uint16)(unsafe.Pointer(p + 56))&0x3>>0)) == libc.Int32FromInt32(SQLITE_IDXTYPE_PRIMARYKEY))) { break } goto _1 _1: ; p = (*TIndex)(unsafe.Pointer(p)).FpNext } return p } // C documentation // // /* // ** Convert an table column number into a index column number. That is, // ** for the column iCol in the table (as defined by the CREATE TABLE statement) // ** find the (first) offset of that column in index pIdx. Or return -1 // ** if column iCol is not used in index pIdx. // */ func _sqlite3TableColumnToIndex(tls *libc.TLS, pIdx uintptr, iCol Ti16) (r Ti16) { var i int32 _ = i i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) { break } if int32(iCol) == int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))) { return int16(i) } goto _1 _1: ; i++ } return int16(-int32(1)) } // C documentation // // /* Convert a storage column number into a table column number. // ** // ** The storage column number (0,1,2,....) is the index of the value // ** as it appears in the record on disk. The true column number // ** is the index (0,1,2,...) of the column in the CREATE TABLE statement. // ** // ** The storage column number is less than the table column number if // ** and only there are VIRTUAL columns to the left. // ** // ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro. // */ func _sqlite3StorageColumnToTable(tls *libc.TLS, pTab uintptr, iCol Ti16) (r Ti16) { var i int32 _ = i if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasVirtual) != 0 { i = 0 for { if !(i <= int32(iCol)) { break } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 { iCol++ } goto _1 _1: ; i++ } } return iCol } // C documentation // // /* Convert a table column number into a storage column number. // ** // ** The storage column number (0,1,2,....) is the index of the value // ** as it appears in the record on disk. Or, if the input column is // ** the N-th virtual column (zero-based) then the storage number is // ** the number of non-virtual columns in the table plus N. // ** // ** The true column number is the index (0,1,2,...) of the column in // ** the CREATE TABLE statement. // ** // ** If the input column is a VIRTUAL column, then it should not appear // ** in storage. But the value sometimes is cached in registers that // ** follow the range of registers used to construct storage. This // ** avoids computing the same VIRTUAL column multiple times, and provides // ** values for use by OP_Param opcodes in triggers. Hence, if the // ** input column is a VIRTUAL table, put it after all the other columns. // ** // ** In the following, N means "normal column", S means STORED, and // ** V means VIRTUAL. Suppose the CREATE TABLE has columns like this: // ** // ** CREATE TABLE ex(N,S,V,N,S,V,N,S,V); // ** -- 0 1 2 3 4 5 6 7 8 // ** // ** Then the mapping from this function is as follows: // ** // ** INPUTS: 0 1 2 3 4 5 6 7 8 // ** OUTPUTS: 0 1 6 2 3 7 4 5 8 // ** // ** So, in other words, this routine shifts all the virtual columns to // ** the end. // ** // ** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and // ** this routine is a no-op macro. If the pTab does not have any virtual // ** columns, then this routine is no-op that always return iCol. If iCol // ** is negative (indicating the ROWID column) then this routine return iCol. // */ func _sqlite3TableColumnToStorage(tls *libc.TLS, pTab uintptr, iCol Ti16) (r Ti16) { var i int32 var n Ti16 _, _ = i, n _ = libc.Int32FromInt32(0) if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasVirtual) == uint32(0) || int32(iCol) < 0 { return iCol } i = 0 n = libc.Int16FromInt32(0) for { if !(i < int32(iCol)) { break } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 { n++ } goto _1 _1: ; i++ } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 { /* iCol is a virtual column itself */ return int16(int32((*TTable)(unsafe.Pointer(pTab)).FnNVCol) + i - int32(n)) } else { /* iCol is a normal or stored column */ return n } return r } // C documentation // // /* // ** Insert a single OP_JournalMode query opcode in order to force the // ** prepared statement to return false for sqlite3_stmt_readonly(). This // ** is used by CREATE TABLE IF NOT EXISTS and similar if the table already // ** exists, so that the prepared statement for CREATE TABLE IF NOT EXISTS // ** will return false for sqlite3_stmt_readonly() even if that statement // ** is a read-only no-op. // */ func _sqlite3ForceNotReadOnly(tls *libc.TLS, pParse uintptr) { var iReg, v1 int32 var v, v2 uintptr _, _, _, _ = iReg, v, v1, v2 v2 = pParse + 44 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) iReg = v1 v = _sqlite3GetVdbe(tls, pParse) if v != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_JournalMode), 0, iReg, -int32(1)) _sqlite3VdbeUsesBtree(tls, v, 0) } } // C documentation // // /* // ** Begin constructing a new table representation in memory. This is // ** the first of several action routines that get called in response // ** to a CREATE TABLE statement. In particular, this routine is called // ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp // ** flag is true if the table should be stored in the auxiliary database // ** file instead of in the main database file. This is normally the case // ** when the "TEMP" or "TEMPORARY" keyword occurs in between // ** CREATE and TABLE. // ** // ** The new table record is initialized and put in pParse->pNewTable. // ** As more of the CREATE TABLE statement is parsed, additional action // ** routines will be called to add more information to this record. // ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine // ** is called to complete the construction of the new table record. // */ func _sqlite3StartTable(tls *libc.TLS, pParse uintptr, pName1 uintptr, pName2 uintptr, isTemp int32, isView int32, isVirtual int32, noErr int32) { bp := tls.Alloc(32) defer tls.Free(32) var addr1, fileFormat, iDb, reg1, reg2, reg3, v10, v11, v13, v15, v7, v8 int32 var db, pTable, v, zDb, zDb1, zName, v1, v12, v14, v2, v3, v4, v5, v9 uintptr var v6 bool var _ /* pName at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr1, db, fileFormat, iDb, pTable, reg1, reg2, reg3, v, zDb, zDb1, zName, v1, v10, v11, v12, v13, v14, v15, v2, v3, v4, v5, v6, v7, v8, v9 zName = uintptr(0) /* The name of the new table */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Unqualified name of the table to create */ if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 && (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FnewTnum == uint32(1) { /* Special case: Parsing the sqlite_schema or sqlite_temp_schema schema */ iDb = int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb) if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) { v1 = __ccgo_ts + 6533 } else { v1 = __ccgo_ts + 6066 } zName = _sqlite3DbStrDup(tls, db, v1) *(*uintptr)(unsafe.Pointer(bp)) = pName1 } else { /* The common case */ iDb = _sqlite3TwoPartName(tls, pParse, pName1, pName2, bp) if iDb < 0 { return } if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && isTemp != 0 && (*TToken)(unsafe.Pointer(pName2)).Fn > uint32(0) && iDb != int32(1) { /* If creating a temp table, the name may not be qualified. Unless ** the database name is "temp" anyway. */ _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12549, 0) return } if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && isTemp != 0 { iDb = int32(1) } zName = _sqlite3NameFromToken(tls, db, *(*uintptr)(unsafe.Pointer(bp))) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenMap(tls, pParse, zName, *(*uintptr)(unsafe.Pointer(bp))) } } (*TParse)(unsafe.Pointer(pParse)).FsNameToken = *(*TToken)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))) if zName == uintptr(0) { return } if isView != 0 { v2 = __ccgo_ts + 10796 } else { v2 = __ccgo_ts + 9058 } if _sqlite3CheckObjectName(tls, pParse, zName, v2, zName) != 0 { goto begin_table_error } if int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb) == int32(1) { isTemp = int32(1) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && isTemp == int32(1) { v3 = __ccgo_ts + 6533 } else { v3 = __ccgo_ts + 6066 } if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_INSERT), v3, uintptr(0), zDb) != 0 { goto begin_table_error } if !(isVirtual != 0) && _sqlite3AuthCheck(tls, pParse, int32(_aCode[isTemp+int32(2)*isView]), zName, uintptr(0), zDb) != 0 { goto begin_table_error } /* Make sure the new table name does not collide with an existing ** index or table name in the same database. Issue an error message if ** it does. The exception is if the statement being parsed was passed ** to an sqlite3_declare_vtab() call. In that case only the column names ** and types will be used, so there is no need to test for namespace ** collisions. */ if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != libc.Int32FromInt32(PARSE_MODE_NORMAL)) { zDb1 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) { goto begin_table_error } pTable = _sqlite3FindTable(tls, db, zName, zDb1) if pTable != 0 { if !(noErr != 0) { if int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == int32(TABTYP_VIEW) { v4 = __ccgo_ts + 10796 } else { v4 = __ccgo_ts + 9058 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12590, libc.VaList(bp+16, v4, *(*uintptr)(unsafe.Pointer(bp)))) } else { _ = libc.Int32FromInt32(0) _sqlite3CodeVerifySchema(tls, pParse, iDb) _sqlite3ForceNotReadOnly(tls, pParse) } goto begin_table_error } if _sqlite3FindIndex(tls, db, zName, zDb1) != uintptr(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12611, libc.VaList(bp+16, zName)) goto begin_table_error } } pTable = _sqlite3DbMallocZero(tls, db, uint64(64)) if pTable == uintptr(0) { _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM) (*TParse)(unsafe.Pointer(pParse)).FnErr++ goto begin_table_error } (*TTable)(unsafe.Pointer(pTable)).FzName = zName (*TTable)(unsafe.Pointer(pTable)).FiPKey = int16(-int32(1)) (*TTable)(unsafe.Pointer(pTable)).FpSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema (*TTable)(unsafe.Pointer(pTable)).FnTabRef = uint32(1) (*TTable)(unsafe.Pointer(pTable)).FnRowLogEst = int16(200) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).FpNewTable = pTable /* Begin generating the code that will insert the table record into ** the schema table. Note in particular that we must go ahead ** and allocate the record number for the table entry now. Before any ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause ** indices to be created and the table record must come before the ** indices. Hence, the record number for the table must be allocated ** now. */ if v6 = !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0); v6 { v5 = _sqlite3GetVdbe(tls, pParse) v = v5 } if v6 && v5 != uintptr(0) { _sqlite3BeginWriteOperation(tls, pParse, int32(1), iDb) if isVirtual != 0 { _sqlite3VdbeAddOp0(tls, v, int32(OP_VBegin)) } /* If the file format and encoding in the database have not been set, ** set them now. */ v9 = pParse + 44 *(*int32)(unsafe.Pointer(v9))++ v8 = *(*int32)(unsafe.Pointer(v9)) v7 = v8 (*TParse)(unsafe.Pointer(pParse)).FregRowid = v7 reg1 = v7 v12 = pParse + 44 *(*int32)(unsafe.Pointer(v12))++ v11 = *(*int32)(unsafe.Pointer(v12)) v10 = v11 (*TParse)(unsafe.Pointer(pParse)).FregRoot = v10 reg2 = v10 v14 = pParse + 44 *(*int32)(unsafe.Pointer(v14))++ v13 = *(*int32)(unsafe.Pointer(v14)) reg3 = v13 _sqlite3VdbeAddOp3(tls, v, int32(OP_ReadCookie), iDb, reg3, int32(BTREE_FILE_FORMAT)) _sqlite3VdbeUsesBtree(tls, v, iDb) addr1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_If), reg3) if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_LegacyFileFmt) != uint64(0) { v15 = int32(1) } else { v15 = int32(SQLITE_MAX_FILE_FORMAT) } fileFormat = v15 _sqlite3VdbeAddOp3(tls, v, int32(OP_SetCookie), iDb, int32(BTREE_FILE_FORMAT), fileFormat) _sqlite3VdbeAddOp3(tls, v, int32(OP_SetCookie), iDb, int32(BTREE_TEXT_ENCODING), int32((*Tsqlite3)(unsafe.Pointer(db)).Fenc)) _sqlite3VdbeJumpHere(tls, v, addr1) /* This just creates a place-holder record in the sqlite_schema table. ** The record created does not contain anything yet. It will be replaced ** by the real entry in code generated at sqlite3EndTable(). ** ** The rowid for the new entry is left in register pParse->regRowid. ** The root page number of the new table is left in reg pParse->regRoot. ** The rowid and root page number values are needed by the code that ** sqlite3EndTable will generate. */ if isView != 0 || isVirtual != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, reg2) } else { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pParse + 144)) = _sqlite3VdbeAddOp3(tls, v, int32(OP_CreateBtree), iDb, reg2, int32(BTREE_INTKEY)) } _sqlite3OpenSchemaTable(tls, pParse, iDb) _sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), 0, reg1) _sqlite3VdbeAddOp4(tls, v, int32(OP_Blob), int32(6), reg3, 0, uintptr(unsafe.Pointer(&_nullRow)), -int32(1)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), 0, reg3, reg1) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_APPEND)) _sqlite3VdbeAddOp0(tls, v, int32(OP_Close)) } /* Normal (non-error) return. */ return /* If an error occurs, we jump here */ goto begin_table_error begin_table_error: ; (*TParse)(unsafe.Pointer(pParse)).FcheckSchema = uint8(1) _sqlite3DbFree(tls, db, zName) return } var _aCode = [4]Tu8{ 0: uint8(SQLITE_CREATE_TABLE), 1: uint8(SQLITE_CREATE_TEMP_TABLE), 2: uint8(SQLITE_CREATE_VIEW), 3: uint8(SQLITE_CREATE_TEMP_VIEW), } /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */ var _nullRow = [6]uint8{ 0: uint8(6), } /* Set properties of a table column based on the (magical) ** name of the column. */ // C documentation // // /* // ** Clean up the data structures associated with the RETURNING clause. // */ func _sqlite3DeleteReturning(tls *libc.TLS, db uintptr, pArg uintptr) { var pHash, pRet uintptr _, _ = pHash, pRet pRet = pArg pHash = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema + 40 _sqlite3HashInsert(tls, pHash, pRet+104, uintptr(0)) _sqlite3ExprListDelete(tls, db, (*TReturning)(unsafe.Pointer(pRet)).FpReturnEL) _sqlite3DbFree(tls, db, pRet) } // C documentation // // /* // ** Add the RETURNING clause to the parse currently underway. // ** // ** This routine creates a special TEMP trigger that will fire for each row // ** of the DML statement. That TEMP trigger contains a single SELECT // ** statement with a result set that is the argument of the RETURNING clause. // ** The trigger has the Trigger.bReturning flag and an opcode of // ** TK_RETURNING instead of TK_SELECT, so that the trigger code generator // ** knows to handle it specially. The TEMP trigger is automatically // ** removed at the end of the parse. // ** // ** When this routine is called, we do not yet know if the RETURNING clause // ** is attached to a DELETE, INSERT, or UPDATE, so construct it as a // ** RETURNING trigger instead. It will then be converted into the appropriate // ** type on the first call to sqlite3TriggersExist(). // */ func _sqlite3AddReturning(tls *libc.TLS, pParse uintptr, pList uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, pHash, pRet uintptr _, _, _ = db, pHash, pRet db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12646, 0) } else { _ = libc.Int32FromInt32(0) } (*TParse)(unsafe.Pointer(pParse)).FbReturning = uint8(1) pRet = _sqlite3DbMallocZero(tls, db, uint64(144)) if pRet == uintptr(0) { _sqlite3ExprListDelete(tls, db, pList) return } *(*uintptr)(unsafe.Pointer(pParse + 144)) = pRet (*TReturning)(unsafe.Pointer(pRet)).FpParse = pParse (*TReturning)(unsafe.Pointer(pRet)).FpReturnEL = pList _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3DeleteReturning), pRet) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return } Xsqlite3_snprintf(tls, int32(40), pRet+104, __ccgo_ts+12680, libc.VaList(bp+8, pParse)) (*TReturning)(unsafe.Pointer(pRet)).FretTrig.FzName = pRet + 104 (*TReturning)(unsafe.Pointer(pRet)).FretTrig.Fop = uint8(TK_RETURNING) (*TReturning)(unsafe.Pointer(pRet)).FretTrig.Ftr_tm = uint8(TRIGGER_AFTER) (*TReturning)(unsafe.Pointer(pRet)).FretTrig.FbReturning = uint8(1) (*TReturning)(unsafe.Pointer(pRet)).FretTrig.FpSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema (*TReturning)(unsafe.Pointer(pRet)).FretTrig.FpTabSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema (*TReturning)(unsafe.Pointer(pRet)).FretTrig.Fstep_list = pRet + 44 (*TReturning)(unsafe.Pointer(pRet)).FretTStep.Fop = uint8(TK_RETURNING) (*TReturning)(unsafe.Pointer(pRet)).FretTStep.FpTrig = pRet + 8 (*TReturning)(unsafe.Pointer(pRet)).FretTStep.FpExprList = pList pHash = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema + 40 _ = libc.Int32FromInt32(0) if _sqlite3HashInsert(tls, pHash, pRet+104, pRet+8) == pRet+8 { _sqlite3OomFault(tls, db) } } // C documentation // // /* // ** Add a new column to the table currently being constructed. // ** // ** The parser calls this routine once for each column declaration // ** in a CREATE TABLE statement. sqlite3StartTable() gets called // ** first to get things going. Then this routine is called for each // ** column. // */ func _sqlite3AddColumn(tls *libc.TLS, pParse uintptr, _sName TToken, _sType TToken) { bp := tls.Alloc(32) defer tls.Free(32) *(*TToken)(unsafe.Pointer(bp)) = _sName *(*TToken)(unsafe.Pointer(bp + 8)) = _sType var aNew, db, p, pCol, z, zType, v1, p4 uintptr var affinity uint8 var eType, hName, szEst Tu8 var i int32 _, _, _, _, _, _, _, _, _, _, _, _, _ = aNew, affinity, db, eType, hName, i, p, pCol, szEst, z, zType, v1, p4 db = (*TParse)(unsafe.Pointer(pParse)).Fdb eType = uint8(COLTYPE_CUSTOM) szEst = uint8(1) affinity = uint8(SQLITE_AFF_BLOB) v1 = (*TParse)(unsafe.Pointer(pParse)).FpNewTable p = v1 if v1 == uintptr(0) { return } if int32((*TTable)(unsafe.Pointer(p)).FnCol)+int32(1) > *(*int32)(unsafe.Pointer(db + 120 + 2*4)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12700, libc.VaList(bp+24, (*TTable)(unsafe.Pointer(p)).FzName)) return } if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) { _sqlite3DequoteToken(tls, bp) } /* Because keywords GENERATE ALWAYS can be converted into identifiers ** by the parser, we can sometimes end up with a typename that ends ** with "generated always". Check for this case and omit the surplus ** text. */ if (*(*TToken)(unsafe.Pointer(bp + 8))).Fn >= uint32(16) && Xsqlite3_strnicmp(tls, (*(*TToken)(unsafe.Pointer(bp + 8))).Fz+uintptr((*(*TToken)(unsafe.Pointer(bp + 8))).Fn-libc.Uint32FromInt32(6)), __ccgo_ts+12723, int32(6)) == 0 { (*(*TToken)(unsafe.Pointer(bp + 8))).Fn -= uint32(6) for (*(*TToken)(unsafe.Pointer(bp + 8))).Fn > uint32(0) && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer((*(*TToken)(unsafe.Pointer(bp + 8))).Fz + uintptr((*(*TToken)(unsafe.Pointer(bp + 8))).Fn-uint32(1))))])&int32(0x01) != 0 { (*(*TToken)(unsafe.Pointer(bp + 8))).Fn-- } if (*(*TToken)(unsafe.Pointer(bp + 8))).Fn >= uint32(9) && Xsqlite3_strnicmp(tls, (*(*TToken)(unsafe.Pointer(bp + 8))).Fz+uintptr((*(*TToken)(unsafe.Pointer(bp + 8))).Fn-libc.Uint32FromInt32(9)), __ccgo_ts+12730, int32(9)) == 0 { (*(*TToken)(unsafe.Pointer(bp + 8))).Fn -= uint32(9) for (*(*TToken)(unsafe.Pointer(bp + 8))).Fn > uint32(0) && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer((*(*TToken)(unsafe.Pointer(bp + 8))).Fz + uintptr((*(*TToken)(unsafe.Pointer(bp + 8))).Fn-uint32(1))))])&int32(0x01) != 0 { (*(*TToken)(unsafe.Pointer(bp + 8))).Fn-- } } } /* Check for standard typenames. For standard typenames we will ** set the Column.eType field rather than storing the typename after ** the column name, in order to save space. */ if (*(*TToken)(unsafe.Pointer(bp + 8))).Fn >= uint32(3) { _sqlite3DequoteToken(tls, bp+8) i = 0 for { if !(i < int32(SQLITE_N_STDTYPE)) { break } if (*(*TToken)(unsafe.Pointer(bp + 8))).Fn == uint32(_sqlite3StdTypeLen[i]) && Xsqlite3_strnicmp(tls, (*(*TToken)(unsafe.Pointer(bp + 8))).Fz, _sqlite3StdType[i], int32((*(*TToken)(unsafe.Pointer(bp + 8))).Fn)) == 0 { (*(*TToken)(unsafe.Pointer(bp + 8))).Fn = uint32(0) eType = uint8(i + int32(1)) affinity = _sqlite3StdTypeAffinity[i] if int32(affinity) <= int32(SQLITE_AFF_TEXT) { szEst = uint8(5) } break } goto _2 _2: ; i++ } } z = _sqlite3DbMallocRaw(tls, db, uint64(int64((*(*TToken)(unsafe.Pointer(bp))).Fn)+int64(1)+int64((*(*TToken)(unsafe.Pointer(bp + 8))).Fn)+libc.BoolInt64((*(*TToken)(unsafe.Pointer(bp + 8))).Fn > libc.Uint32FromInt32(0)))) if z == uintptr(0) { return } if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenMap(tls, pParse, z, bp) } libc.Xmemcpy(tls, z, (*(*TToken)(unsafe.Pointer(bp))).Fz, (*(*TToken)(unsafe.Pointer(bp))).Fn) *(*uint8)(unsafe.Pointer(z + uintptr((*(*TToken)(unsafe.Pointer(bp))).Fn))) = uint8(0) _sqlite3Dequote(tls, z) hName = _sqlite3StrIHash(tls, z) i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(p)).FnCol)) { break } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(p)).FaCol + uintptr(i)*12))).FhName) == int32(hName) && _sqlite3StrICmp(tls, z, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(p)).FaCol + uintptr(i)*12))).FzCnName) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12740, libc.VaList(bp+24, z)) _sqlite3DbFree(tls, db, z) return } goto _3 _3: ; i++ } aNew = _sqlite3DbRealloc(tls, db, (*TTable)(unsafe.Pointer(p)).FaCol, uint64((int64((*TTable)(unsafe.Pointer(p)).FnCol)+int64(1))*int64(12))) if aNew == uintptr(0) { _sqlite3DbFree(tls, db, z) return } (*TTable)(unsafe.Pointer(p)).FaCol = aNew pCol = (*TTable)(unsafe.Pointer(p)).FaCol + uintptr((*TTable)(unsafe.Pointer(p)).FnCol)*12 libc.Xmemset(tls, pCol, 0, uint32(12)) (*TColumn)(unsafe.Pointer(pCol)).FzCnName = z (*TColumn)(unsafe.Pointer(pCol)).FhName = hName if (*(*TToken)(unsafe.Pointer(bp + 8))).Fn == uint32(0) { /* If there is no type specified, columns have the default affinity ** 'BLOB' with a default size of 4 bytes. */ (*TColumn)(unsafe.Pointer(pCol)).Faffinity = affinity libc.SetBitFieldPtr8Uint32(pCol+4, uint32(eType), 4, 0xf0) (*TColumn)(unsafe.Pointer(pCol)).FszEst = szEst } else { zType = z + uintptr(_sqlite3Strlen30(tls, z)) + uintptr(1) libc.Xmemcpy(tls, zType, (*(*TToken)(unsafe.Pointer(bp + 8))).Fz, (*(*TToken)(unsafe.Pointer(bp + 8))).Fn) *(*uint8)(unsafe.Pointer(zType + uintptr((*(*TToken)(unsafe.Pointer(bp + 8))).Fn))) = uint8(0) _sqlite3Dequote(tls, zType) (*TColumn)(unsafe.Pointer(pCol)).Faffinity = _sqlite3AffinityType(tls, zType, pCol) p4 = pCol + 10 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | libc.Int32FromInt32(COLFLAG_HASTYPE)) } (*TTable)(unsafe.Pointer(p)).FnCol++ (*TTable)(unsafe.Pointer(p)).FnNVCol++ (*TParse)(unsafe.Pointer(pParse)).FconstraintName.Fn = uint32(0) } // C documentation // // /* // ** This routine is called by the parser while in the middle of // ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has // ** been seen on a column. This routine sets the notNull flag on // ** the column currently under construction. // */ func _sqlite3AddNotNull(tls *libc.TLS, pParse uintptr, onError int32) { var p, pCol, pIdx uintptr _, _, _ = p, pCol, pIdx p = (*TParse)(unsafe.Pointer(pParse)).FpNewTable if p == uintptr(0) || int32((*TTable)(unsafe.Pointer(p)).FnCol) < int32(1) { return } pCol = (*TTable)(unsafe.Pointer(p)).FaCol + uintptr(int32((*TTable)(unsafe.Pointer(p)).FnCol)-int32(1))*12 libc.SetBitFieldPtr8Uint32(pCol+4, uint32(uint8(onError)), 0, 0xf) *(*Tu32)(unsafe.Pointer(p + 28)) |= uint32(TF_HasNotNull) /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created ** on this column. */ if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_UNIQUE) != 0 { pIdx = (*TTable)(unsafe.Pointer(p)).FpIndex for { if !(pIdx != 0) { break } _ = libc.Int32FromInt32(0) if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn))) == int32((*TTable)(unsafe.Pointer(p)).FnCol)-int32(1) { libc.SetBitFieldPtr16Uint32(pIdx+56, libc.Uint32FromInt32(1), 3, 0x8) } goto _1 _1: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } } } // C documentation // // /* // ** Scan the column type name zType (length nType) and return the // ** associated affinity type. // ** // ** This routine does a case-independent search of zType for the // ** substrings in the following table. If one of the substrings is // ** found, the corresponding affinity is returned. If zType contains // ** more than one of the substrings, entries toward the top of // ** the table take priority. For example, if zType is 'BLOBINT', // ** SQLITE_AFF_INTEGER is returned. // ** // ** Substring | Affinity // ** -------------------------------- // ** 'INT' | SQLITE_AFF_INTEGER // ** 'CHAR' | SQLITE_AFF_TEXT // ** 'CLOB' | SQLITE_AFF_TEXT // ** 'TEXT' | SQLITE_AFF_TEXT // ** 'BLOB' | SQLITE_AFF_BLOB // ** 'REAL' | SQLITE_AFF_REAL // ** 'FLOA' | SQLITE_AFF_REAL // ** 'DOUB' | SQLITE_AFF_REAL // ** // ** If none of the substrings in the above table are found, // ** SQLITE_AFF_NUMERIC is returned. // */ func _sqlite3AffinityType(tls *libc.TLS, zIn uintptr, pCol uintptr) (r uint8) { bp := tls.Alloc(16) defer tls.Free(16) var aff uint8 var h Tu32 var x Tu8 var zChar uintptr var _ /* v at bp+0 */ int32 _, _, _, _ = aff, h, x, zChar h = uint32(0) aff = uint8(SQLITE_AFF_NUMERIC) zChar = uintptr(0) _ = libc.Int32FromInt32(0) for *(*uint8)(unsafe.Pointer(zIn)) != 0 { x = *(*Tu8)(unsafe.Pointer(zIn)) h = h< r=(k/4+1) */ _sqlite3GetInt32(tls, zChar, bp) break } zChar++ } } else { *(*int32)(unsafe.Pointer(bp)) = int32(16) /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/ } } *(*int32)(unsafe.Pointer(bp)) = *(*int32)(unsafe.Pointer(bp))/int32(4) + int32(1) if *(*int32)(unsafe.Pointer(bp)) > int32(255) { *(*int32)(unsafe.Pointer(bp)) = int32(255) } (*TColumn)(unsafe.Pointer(pCol)).FszEst = uint8(*(*int32)(unsafe.Pointer(bp))) } return aff } // C documentation // // /* // ** The expression is the default value for the most recently added column // ** of the table currently under construction. // ** // ** Default value expressions must be constant. Raise an exception if this // ** is not the case. // ** // ** This routine is called by the parser while in the middle of // ** parsing a CREATE TABLE statement. // */ func _sqlite3AddDefaultValue(tls *libc.TLS, pParse uintptr, pExpr uintptr, zStart uintptr, zEnd uintptr) { bp := tls.Alloc(80) defer tls.Free(80) var db, p, pCol, pDfltExpr uintptr var isInit int32 var _ /* x at bp+0 */ TExpr _, _, _, _, _ = db, isInit, p, pCol, pDfltExpr db = (*TParse)(unsafe.Pointer(pParse)).Fdb p = (*TParse)(unsafe.Pointer(pParse)).FpNewTable if p != uintptr(0) { isInit = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb) != int32(1)) pCol = (*TTable)(unsafe.Pointer(p)).FaCol + uintptr(int32((*TTable)(unsafe.Pointer(p)).FnCol)-int32(1))*12 if !(_sqlite3ExprIsConstantOrFunction(tls, pExpr, uint8(isInit)) != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12766, libc.VaList(bp+64, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)) } else { if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12811, 0) } else { libc.Xmemset(tls, bp, 0, uint32(52)) (*(*TExpr)(unsafe.Pointer(bp))).Fop = uint8(TK_SPAN) *(*uintptr)(unsafe.Pointer(bp + 8)) = _sqlite3DbSpanDup(tls, db, zStart, zEnd) (*(*TExpr)(unsafe.Pointer(bp))).FpLeft = pExpr (*(*TExpr)(unsafe.Pointer(bp))).Fflags = uint32(EP_Skip) pDfltExpr = _sqlite3ExprDup(tls, db, bp, int32(EXPRDUP_REDUCE)) _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp + 8))) _sqlite3ColumnSetExpr(tls, pParse, p, pCol, pDfltExpr) } } } if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameExprUnmap(tls, pParse, pExpr) } _sqlite3ExprDelete(tls, db, pExpr) } // C documentation // // /* // ** Backwards Compatibility Hack: // ** // ** Historical versions of SQLite accepted strings as column names in // ** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example: // ** // ** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim) // ** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC); // ** // ** This is goofy. But to preserve backwards compatibility we continue to // ** accept it. This routine does the necessary conversion. It converts // ** the expression given in its argument from a TK_STRING into a TK_ID // ** if the expression is just a TK_STRING with an optional COLLATE clause. // ** If the expression is anything other than TK_STRING, the expression is // ** unchanged. // */ func _sqlite3StringToId(tls *libc.TLS, p uintptr) { if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_STRING) { (*TExpr)(unsafe.Pointer(p)).Fop = uint8(TK_ID) } else { if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_COLLATE) && int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).FpLeft)).Fop) == int32(TK_STRING) { (*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).FpLeft)).Fop = uint8(TK_ID) } } } // C documentation // // /* // ** Tag the given column as being part of the PRIMARY KEY // */ func _makeColumnPartOfPrimaryKey(tls *libc.TLS, pParse uintptr, pCol uintptr) { var p1 uintptr _ = p1 p1 = pCol + 10 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(COLFLAG_PRIMKEY)) if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12852, 0) } } // C documentation // // /* // ** Designate the PRIMARY KEY for the table. pList is a list of names // ** of columns that form the primary key. If pList is NULL, then the // ** most recently added column of the table is the primary key. // ** // ** A table can have at most one primary key. If the table already has // ** a primary key (and this is the second primary key) then create an // ** error. // ** // ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, // ** then we will try to use that column as the rowid. Set the Table.iPKey // ** field of the table under construction to be the index of the // ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is // ** no INTEGER PRIMARY KEY. // ** // ** If the key is not an INTEGER PRIMARY KEY, then create a unique // ** index for the key. No index is created for INTEGER PRIMARY KEYs. // */ func _sqlite3AddPrimaryKey(tls *libc.TLS, pParse uintptr, pList uintptr, onError int32, autoInc int32, sortOrder int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, iCol, nTerm int32 var pCExpr, pCExpr1, pCol, pTab, zCName uintptr _, _, _, _, _, _, _, _ = i, iCol, nTerm, pCExpr, pCExpr1, pCol, pTab, zCName pTab = (*TParse)(unsafe.Pointer(pParse)).FpNewTable pCol = uintptr(0) iCol = -int32(1) if pTab == uintptr(0) { goto primary_key_exit } if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasPrimaryKey) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12904, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) goto primary_key_exit } *(*Tu32)(unsafe.Pointer(pTab + 28)) |= uint32(TF_HasPrimaryKey) if pList == uintptr(0) { iCol = int32((*TTable)(unsafe.Pointer(pTab)).FnCol) - int32(1) pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12 _makeColumnPartOfPrimaryKey(tls, pParse, pCol) nTerm = int32(1) } else { nTerm = (*TExprList)(unsafe.Pointer(pList)).FnExpr i = 0 for { if !(i < nTerm) { break } pCExpr = _sqlite3ExprSkipCollate(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr) _ = libc.Int32FromInt32(0) _sqlite3StringToId(tls, pCExpr) if int32((*TExpr)(unsafe.Pointer(pCExpr)).Fop) == int32(TK_ID) { _ = libc.Int32FromInt32(0) zCName = *(*uintptr)(unsafe.Pointer(pCExpr + 8)) iCol = 0 for { if !(iCol < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if _sqlite3StrICmp(tls, zCName, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName) == 0 { pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12 _makeColumnPartOfPrimaryKey(tls, pParse, pCol) break } goto _2 _2: ; iCol++ } } goto _1 _1: ; i++ } } if nTerm == int32(1) && pCol != 0 && int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf0>>4)) == int32(COLTYPE_INTEGER) && sortOrder != int32(SQLITE_SO_DESC) { if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && pList != 0 { pCExpr1 = _sqlite3ExprSkipCollate(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8))).FpExpr) _sqlite3RenameTokenRemap(tls, pParse, pTab+32, pCExpr1) } (*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(iCol) (*TTable)(unsafe.Pointer(pTab)).FkeyConf = uint8(onError) _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(pTab + 28)) |= uint32(autoInc * int32(TF_Autoincrement)) if pList != 0 { (*TParse)(unsafe.Pointer(pParse)).FiPkSortOrder = (*(*TExprList_item)(unsafe.Pointer(pList + 8))).Ffg.FsortFlags } _sqlite3HasExplicitNulls(tls, pParse, pList) } else { if autoInc != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12945, 0) } else { _sqlite3CreateIndex(tls, pParse, uintptr(0), uintptr(0), uintptr(0), pList, onError, uintptr(0), uintptr(0), sortOrder, 0, uint8(SQLITE_IDXTYPE_PRIMARYKEY)) pList = uintptr(0) } } goto primary_key_exit primary_key_exit: ; _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pList) return } // C documentation // // /* // ** Add a new CHECK constraint to the table currently under construction. // */ func _sqlite3AddCheckConstraint(tls *libc.TLS, pParse uintptr, pCheckExpr uintptr, zStart uintptr, zEnd uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, pTab uintptr var _ /* t at bp+0 */ TToken _, _ = db, pTab pTab = (*TParse)(unsafe.Pointer(pParse)).FpNewTable db = (*TParse)(unsafe.Pointer(pParse)).Fdb if pTab != 0 && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) == libc.Int32FromInt32(PARSE_MODE_DECLARE_VTAB)) && !(_sqlite3BtreeIsReadonly(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb)*16))).FpBt) != 0) { (*TTable)(unsafe.Pointer(pTab)).FpCheck = _sqlite3ExprListAppend(tls, pParse, (*TTable)(unsafe.Pointer(pTab)).FpCheck, pCheckExpr) if (*TParse)(unsafe.Pointer(pParse)).FconstraintName.Fn != 0 { _sqlite3ExprListSetName(tls, pParse, (*TTable)(unsafe.Pointer(pTab)).FpCheck, pParse+80, int32(1)) } else { zStart++ for { if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zStart))])&int32(0x01) != 0) { break } goto _1 _1: ; zStart++ } for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zEnd + uintptr(-libc.Int32FromInt32(1))))])&int32(0x01) != 0 { zEnd-- } (*(*TToken)(unsafe.Pointer(bp))).Fz = zStart (*(*TToken)(unsafe.Pointer(bp))).Fn = uint32(int32(zEnd) - int32((*(*TToken)(unsafe.Pointer(bp))).Fz)) _sqlite3ExprListSetName(tls, pParse, (*TTable)(unsafe.Pointer(pTab)).FpCheck, bp, int32(1)) } } else { _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pCheckExpr) } } // C documentation // // /* // ** Set the collation function of the most recently parsed table column // ** to the CollSeq given. // */ func _sqlite3AddCollateType(tls *libc.TLS, pParse uintptr, pToken uintptr) { var db, p, pIdx, zColl, v1 uintptr var i int32 _, _, _, _, _, _ = db, i, p, pIdx, zColl, v1 v1 = (*TParse)(unsafe.Pointer(pParse)).FpNewTable p = v1 if v1 == uintptr(0) || int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { return } i = int32((*TTable)(unsafe.Pointer(p)).FnCol) - int32(1) db = (*TParse)(unsafe.Pointer(pParse)).Fdb zColl = _sqlite3NameFromToken(tls, db, pToken) if !(zColl != 0) { return } if _sqlite3LocateCollSeq(tls, pParse, zColl) != 0 { _sqlite3ColumnSetColl(tls, db, (*TTable)(unsafe.Pointer(p)).FaCol+uintptr(i)*12, zColl) /* If the column is declared as " PRIMARY KEY COLLATE ", ** then an index may have been created on this column before the ** collation type was added. Correct this if it is the case. */ pIdx = (*TTable)(unsafe.Pointer(p)).FpIndex for { if !(pIdx != 0) { break } _ = libc.Int32FromInt32(0) if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn))) == i { *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl)) = _sqlite3ColumnColl(tls, (*TTable)(unsafe.Pointer(p)).FaCol+uintptr(i)*12) } goto _2 _2: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } } _sqlite3DbFree(tls, db, zColl) } // C documentation // // /* Change the most recently parsed column to be a GENERATED ALWAYS AS // ** column. // */ func _sqlite3AddGenerated(tls *libc.TLS, pParse uintptr, pExpr uintptr, pType uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var eType Tu8 var pCol, pTab, p1 uintptr _, _, _, _ = eType, pCol, pTab, p1 eType = uint8(COLFLAG_VIRTUAL) pTab = (*TParse)(unsafe.Pointer(pParse)).FpNewTable if pTab == uintptr(0) { /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */ goto generated_done } pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(int32((*TTable)(unsafe.Pointer(pTab)).FnCol)-int32(1))*12 if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) == int32(PARSE_MODE_DECLARE_VTAB) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13001, 0) goto generated_done } if int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt) > 0 { goto generated_error } if pType != 0 { if (*TToken)(unsafe.Pointer(pType)).Fn == uint32(7) && Xsqlite3_strnicmp(tls, __ccgo_ts+13044, (*TToken)(unsafe.Pointer(pType)).Fz, int32(7)) == 0 { /* no-op */ } else { if (*TToken)(unsafe.Pointer(pType)).Fn == uint32(6) && Xsqlite3_strnicmp(tls, __ccgo_ts+13052, (*TToken)(unsafe.Pointer(pType)).Fz, int32(6)) == 0 { eType = uint8(COLFLAG_STORED) } else { goto generated_error } } } if int32(eType) == int32(COLFLAG_VIRTUAL) { (*TTable)(unsafe.Pointer(pTab)).FnNVCol-- } p1 = pCol + 10 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | int32(eType)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(pTab + 28)) |= uint32(eType) if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 { _makeColumnPartOfPrimaryKey(tls, pParse, pCol) /* For the error message */ } if pExpr != 0 && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_ID) { /* The value of a generated column needs to be a real expression, not ** just a reference to another column, in order for covering index ** optimizations to work correctly. So if the value is not an expression, ** turn it into one by adding a unary "+" operator. */ pExpr = _sqlite3PExpr(tls, pParse, int32(TK_UPLUS), pExpr, uintptr(0)) } if pExpr != 0 && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_RAISE) { (*TExpr)(unsafe.Pointer(pExpr)).FaffExpr = (*TColumn)(unsafe.Pointer(pCol)).Faffinity } _sqlite3ColumnSetExpr(tls, pParse, pTab, pCol, pExpr) pExpr = uintptr(0) goto generated_done goto generated_error generated_error: ; _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13059, libc.VaList(bp+8, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)) goto generated_done generated_done: ; _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr) } // C documentation // // /* // ** Generate code that will increment the schema cookie. // ** // ** The schema cookie is used to determine when the schema for the // ** database changes. After each schema change, the cookie value // ** changes. When a process first reads the schema it records the // ** cookie. Thereafter, whenever it goes to access the database, // ** it checks the cookie to make sure the schema has not changed // ** since it was last read. // ** // ** This plan is not completely bullet-proof. It is possible for // ** the schema to change multiple times and for the cookie to be // ** set back to prior value. But schema changes are infrequent // ** and the probability of hitting the same cookie value is only // ** 1 chance in 2^32. So we're safe enough. // ** // ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments // ** the schema-version whenever the schema changes. // */ func _sqlite3ChangeCookie(tls *libc.TLS, pParse uintptr, iDb int32) { var db, v uintptr _, _ = db, v db = (*TParse)(unsafe.Pointer(pParse)).Fdb v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, int32(OP_SetCookie), iDb, int32(BTREE_SCHEMA_VERSION), int32(libc.Uint32FromInt32(1)+uint32((*TSchema)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema)).Fschema_cookie))) } // C documentation // // /* // ** Measure the number of characters needed to output the given // ** identifier. The number returned includes any quotes used // ** but does not include the null terminator. // ** // ** The estimate is conservative. It might be larger that what is // ** really needed. // */ func _identLength(tls *libc.TLS, z uintptr) (r int32) { var n int32 _ = n n = 0 for { if !(*(*uint8)(unsafe.Pointer(z)) != 0) { break } if int32(*(*uint8)(unsafe.Pointer(z))) == int32('"') { n++ } goto _1 _1: ; n++ z++ } return n + int32(2) } // C documentation // // /* // ** The first parameter is a pointer to an output buffer. The second // ** parameter is a pointer to an integer that contains the offset at // ** which to write into the output buffer. This function copies the // ** nul-terminated string pointed to by the third parameter, zSignedIdent, // ** to the specified offset in the buffer and updates *pIdx to refer // ** to the first byte after the last byte written before returning. // ** // ** If the string zSignedIdent consists entirely of alphanumeric // ** characters, does not begin with a digit and is not an SQL keyword, // ** then it is copied to the output buffer exactly as it is. Otherwise, // ** it is quoted using double-quotes. // */ func _identPut(tls *libc.TLS, z uintptr, pIdx uintptr, zSignedIdent uintptr) { var i, j, needQuote, v2, v4, v5, v6 int32 var zIdent uintptr _, _, _, _, _, _, _, _ = i, j, needQuote, zIdent, v2, v4, v5, v6 zIdent = zSignedIdent i = *(*int32)(unsafe.Pointer(pIdx)) j = 0 for { if !(*(*uint8)(unsafe.Pointer(zIdent + uintptr(j))) != 0) { break } if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zIdent + uintptr(j)))])&libc.Int32FromInt32(0x06) != 0) && int32(*(*uint8)(unsafe.Pointer(zIdent + uintptr(j)))) != int32('_') { break } goto _1 _1: ; j++ } needQuote = libc.BoolInt32(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zIdent))])&int32(0x04) != 0 || _sqlite3KeywordCode(tls, zIdent, j) != int32(TK_ID) || int32(*(*uint8)(unsafe.Pointer(zIdent + uintptr(j)))) != 0 || j == 0) if needQuote != 0 { v2 = i i++ *(*uint8)(unsafe.Pointer(z + uintptr(v2))) = uint8('"') } j = 0 for { if !(*(*uint8)(unsafe.Pointer(zIdent + uintptr(j))) != 0) { break } v4 = i i++ *(*uint8)(unsafe.Pointer(z + uintptr(v4))) = *(*uint8)(unsafe.Pointer(zIdent + uintptr(j))) if int32(*(*uint8)(unsafe.Pointer(zIdent + uintptr(j)))) == int32('"') { v5 = i i++ *(*uint8)(unsafe.Pointer(z + uintptr(v5))) = uint8('"') } goto _3 _3: ; j++ } if needQuote != 0 { v6 = i i++ *(*uint8)(unsafe.Pointer(z + uintptr(v6))) = uint8('"') } *(*uint8)(unsafe.Pointer(z + uintptr(i))) = uint8(0) *(*int32)(unsafe.Pointer(pIdx)) = i } // C documentation // // /* // ** Generate a CREATE TABLE statement appropriate for the given // ** table. Memory to hold the text of the statement is obtained // ** from sqliteMalloc() and must be freed by the calling function. // */ func _createTableStmt(tls *libc.TLS, db uintptr, p uintptr) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var i, len1, n, v2 int32 var pCol, zEnd, zSep, zSep2, zStmt, zType uintptr var _ /* k at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _ = i, len1, n, pCol, zEnd, zSep, zSep2, zStmt, zType, v2 n = 0 pCol = (*TTable)(unsafe.Pointer(p)).FaCol i = libc.Int32FromInt32(0) for { if !(i < int32((*TTable)(unsafe.Pointer(p)).FnCol)) { break } n += _identLength(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName) + int32(5) goto _1 _1: ; i++ pCol += 12 } n += _identLength(tls, (*TTable)(unsafe.Pointer(p)).FzName) if n < int32(50) { zSep = __ccgo_ts + 1648 zSep2 = __ccgo_ts + 13090 zEnd = __ccgo_ts + 5106 } else { zSep = __ccgo_ts + 13092 zSep2 = __ccgo_ts + 13096 zEnd = __ccgo_ts + 13101 } n += int32(35) + int32(6)*int32((*TTable)(unsafe.Pointer(p)).FnCol) zStmt = _sqlite3DbMallocRaw(tls, uintptr(0), uint64(n)) if zStmt == uintptr(0) { _sqlite3OomFault(tls, db) return uintptr(0) } Xsqlite3_snprintf(tls, n, zStmt, __ccgo_ts+13104, 0) *(*int32)(unsafe.Pointer(bp)) = _sqlite3Strlen30(tls, zStmt) _identPut(tls, zStmt, bp, (*TTable)(unsafe.Pointer(p)).FzName) v2 = *(*int32)(unsafe.Pointer(bp)) *(*int32)(unsafe.Pointer(bp))++ *(*uint8)(unsafe.Pointer(zStmt + uintptr(v2))) = uint8('(') pCol = (*TTable)(unsafe.Pointer(p)).FaCol i = libc.Int32FromInt32(0) for { if !(i < int32((*TTable)(unsafe.Pointer(p)).FnCol)) { break } Xsqlite3_snprintf(tls, n-*(*int32)(unsafe.Pointer(bp)), zStmt+uintptr(*(*int32)(unsafe.Pointer(bp))), zSep, 0) *(*int32)(unsafe.Pointer(bp)) += _sqlite3Strlen30(tls, zStmt+uintptr(*(*int32)(unsafe.Pointer(bp)))) zSep = zSep2 _identPut(tls, zStmt, bp, (*TColumn)(unsafe.Pointer(pCol)).FzCnName) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) zType = _azType1[int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity)-int32(SQLITE_AFF_BLOB)] len1 = _sqlite3Strlen30(tls, zType) _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, zStmt+uintptr(*(*int32)(unsafe.Pointer(bp))), zType, uint32(len1)) *(*int32)(unsafe.Pointer(bp)) += len1 _ = libc.Int32FromInt32(0) goto _3 _3: ; i++ pCol += 12 } Xsqlite3_snprintf(tls, n-*(*int32)(unsafe.Pointer(bp)), zStmt+uintptr(*(*int32)(unsafe.Pointer(bp))), __ccgo_ts+3795, libc.VaList(bp+16, zEnd)) return zStmt } var _azType1 = [6]uintptr{ 0: __ccgo_ts + 1648, 1: __ccgo_ts + 13118, 2: __ccgo_ts + 13124, 3: __ccgo_ts + 13129, 4: __ccgo_ts + 13134, 5: __ccgo_ts + 13124, } // C documentation // // /* // ** Resize an Index object to hold N columns total. Return SQLITE_OK // ** on success and SQLITE_NOMEM on an OOM error. // */ func _resizeIndexObject(tls *libc.TLS, db uintptr, pIdx uintptr, N int32) (r int32) { var nByte int32 var zExtra uintptr _, _ = nByte, zExtra if int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) >= N { return SQLITE_OK } _ = libc.Int32FromInt32(0) nByte = int32((libc.Uint32FromInt64(4) + libc.Uint32FromInt64(2) + libc.Uint32FromInt64(2) + libc.Uint32FromInt32(1)) * uint32(N)) zExtra = _sqlite3DbMallocZero(tls, db, uint64(nByte)) if zExtra == uintptr(0) { return int32(SQLITE_NOMEM) } libc.Xmemcpy(tls, zExtra, (*TIndex)(unsafe.Pointer(pIdx)).FazColl, uint32(4)*uint32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) (*TIndex)(unsafe.Pointer(pIdx)).FazColl = zExtra zExtra += uintptr(uint32(4) * uint32(N)) libc.Xmemcpy(tls, zExtra, (*TIndex)(unsafe.Pointer(pIdx)).FaiRowLogEst, uint32(2)*uint32(int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)+libc.Int32FromInt32(1))) (*TIndex)(unsafe.Pointer(pIdx)).FaiRowLogEst = zExtra zExtra += uintptr(uint32(2) * uint32(N)) libc.Xmemcpy(tls, zExtra, (*TIndex)(unsafe.Pointer(pIdx)).FaiColumn, uint32(2)*uint32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) (*TIndex)(unsafe.Pointer(pIdx)).FaiColumn = zExtra zExtra += uintptr(uint32(2) * uint32(N)) libc.Xmemcpy(tls, zExtra, (*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder, uint32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) (*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder = zExtra (*TIndex)(unsafe.Pointer(pIdx)).FnColumn = uint16(N) libc.SetBitFieldPtr16Uint32(pIdx+56, libc.Uint32FromInt32(1), 4, 0x10) return SQLITE_OK } // C documentation // // /* // ** Estimate the total row width for a table. // */ func _estimateTableWidth(tls *libc.TLS, pTab uintptr) { var i int32 var pTabCol uintptr var wTable uint32 _, _, _ = i, pTabCol, wTable wTable = uint32(0) i = int32((*TTable)(unsafe.Pointer(pTab)).FnCol) pTabCol = (*TTable)(unsafe.Pointer(pTab)).FaCol for { if !(i > 0) { break } wTable += uint32((*TColumn)(unsafe.Pointer(pTabCol)).FszEst) goto _1 _1: ; i-- pTabCol += 12 } if int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) < 0 { wTable++ } (*TTable)(unsafe.Pointer(pTab)).FszTabRow = _sqlite3LogEst(tls, uint64(wTable*uint32(4))) } // C documentation // // /* // ** Estimate the average size of a row for an index. // */ func _estimateIndexWidth(tls *libc.TLS, pIdx uintptr) { var aCol uintptr var i, v2 int32 var wIndex uint32 var x Ti16 _, _, _, _, _ = aCol, i, wIndex, x, v2 wIndex = uint32(0) aCol = (*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) { break } x = *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2)) _ = libc.Int32FromInt32(0) if int32(x) < 0 { v2 = int32(1) } else { v2 = int32((*(*TColumn)(unsafe.Pointer(aCol + uintptr(x)*12))).FszEst) } wIndex += uint32(v2) goto _1 _1: ; i++ } (*TIndex)(unsafe.Pointer(pIdx)).FszIdxRow = _sqlite3LogEst(tls, uint64(wIndex*uint32(4))) } // C documentation // // /* Return true if column number x is any of the first nCol entries of aiCol[]. // ** This is used to determine if the column number x appears in any of the // ** first nCol entries of an index. // */ func _hasColumn(tls *libc.TLS, aiCol uintptr, nCol int32, x int32) (r int32) { var v1 int32 var v2 uintptr _, _ = v1, v2 for { v1 = nCol nCol-- if !(v1 > 0) { break } v2 = aiCol aiCol += 2 if x == int32(*(*Ti16)(unsafe.Pointer(v2))) { return int32(1) } } return 0 } // C documentation // // /* // ** Return true if any of the first nKey entries of index pIdx exactly // ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID // ** PRIMARY KEY index. pIdx is an index on the same table. pIdx may // ** or may not be the same index as pPk. // ** // ** The first nKey entries of pIdx are guaranteed to be ordinary columns, // ** not a rowid or expression. // ** // ** This routine differs from hasColumn() in that both the column and the // ** collating sequence must match for this routine, but for hasColumn() only // ** the column name must match. // */ func _isDupColumn(tls *libc.TLS, pIdx uintptr, nKey int32, pPk uintptr, iCol int32) (r int32) { var i, j int32 _, _ = i, j _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) j = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(iCol)*2))) _ = libc.Int32FromInt32(0) i = 0 for { if !(i < nKey) { break } _ = libc.Int32FromInt32(0) if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))) == j && _sqlite3StrICmp(tls, *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(i)*4)), *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(iCol)*4))) == 0 { return int32(1) } goto _1 _1: ; i++ } return 0 } // C documentation // // /* Recompute the colNotIdxed field of the Index. // ** // ** colNotIdxed is a bitmask that has a 0 bit representing each indexed // ** columns that are within the first 63 columns of the table and a 1 for // ** all other bits (all columns that are not in the index). The // ** high-order bit of colNotIdxed is always 1. All unindexed columns // ** of the table have a 1. // ** // ** 2019-10-24: For the purpose of this computation, virtual columns are // ** not considered to be covered by the index, even if they are in the // ** index, because we do not trust the logic in whereIndexExprTrans() to be // ** able to find all instances of a reference to the indexed table column // ** and convert them into references to the index. Hence we always want // ** the actual table at hand in order to recompute the virtual column, if // ** necessary. // ** // ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask // ** to determine if the index is covering index. // */ func _recomputeColumnsNotIndexed(tls *libc.TLS, pIdx uintptr) { var j, x int32 var m TBitmask var pTab uintptr _, _, _, _ = j, m, pTab, x m = uint64(0) pTab = (*TIndex)(unsafe.Pointer(pIdx)).FpTable j = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) - int32(1) for { if !(j >= 0) { break } x = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j)*2))) if x >= 0 && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(x)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 { if x < int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) { m |= libc.Uint64FromInt32(1) << x } } goto _1 _1: ; j-- } (*TIndex)(unsafe.Pointer(pIdx)).FcolNotIdxed = ^m _ = libc.Int32FromInt32(0) /* See note-20221022-a */ } // C documentation // // /* // ** This routine runs at the end of parsing a CREATE TABLE statement that // ** has a WITHOUT ROWID clause. The job of this routine is to convert both // ** internal schema data structures and the generated VDBE code so that they // ** are appropriate for a WITHOUT ROWID table instead of a rowid table. // ** Changes include: // ** // ** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL. // ** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY // ** into BTREE_BLOBKEY. // ** (3) Bypass the creation of the sqlite_schema table entry // ** for the PRIMARY KEY as the primary key index is now // ** identified by the sqlite_schema table entry of the table itself. // ** (4) Set the Index.tnum of the PRIMARY KEY Index object in the // ** schema to the rootpage from the main table. // ** (5) Add all table columns to the PRIMARY KEY Index object // ** so that the PRIMARY KEY is a covering index. The surplus // ** columns are part of KeyInfo.nAllField and are not used for // ** sorting or lookup or uniqueness checks. // ** (6) Replace the rowid tail on all automatically generated UNIQUE // ** indices with the PRIMARY KEY columns. // ** // ** For virtual tables, only (1) is performed. // */ func _convertToWithoutRowidTable(tls *libc.TLS, pParse uintptr, pTab uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, pIdx, pList, pPk, v uintptr var i, j, n, nExtra, nPk, v3, v4, v8 int32 var v5 Tu16 var _ /* ipkToken at bp+0 */ TToken _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, i, j, n, nExtra, nPk, pIdx, pList, pPk, v, v3, v4, v5, v8 db = (*TParse)(unsafe.Pointer(pParse)).Fdb v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables) */ if !(int32(uint32(*(*uint8)(unsafe.Pointer(db + 172 + 8))&0x2>>1)) != 0) { i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 && int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12 + 4))&0xf>>0)) == OE_None { libc.SetBitFieldPtr8Uint32((*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(i)*12+4, libc.Uint32FromInt32(OE_Abort), 0, 0xf) } goto _1 _1: ; i++ } *(*Tu32)(unsafe.Pointer(pTab + 28)) |= uint32(TF_HasNotNull) } /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY ** into BTREE_BLOBKEY. */ _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(pParse + 144)) != 0 { _ = libc.Int32FromInt32(0) _sqlite3VdbeChangeP3(tls, v, *(*int32)(unsafe.Pointer(pParse + 144)), int32(BTREE_BLOBKEY)) } /* Locate the PRIMARY KEY index. Or, if this table was originally ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. */ if int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) >= 0 { _sqlite3TokenInit(tls, bp, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr((*TTable)(unsafe.Pointer(pTab)).FiPKey)*12))).FzCnName) pList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp, 0)) if pList == uintptr(0) { *(*Tu32)(unsafe.Pointer(pTab + 28)) &= uint32(^libc.Int32FromInt32(TF_WithoutRowid)) return } if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenRemap(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8))).FpExpr, pTab+32) } (*(*TExprList_item)(unsafe.Pointer(pList + 8))).Ffg.FsortFlags = (*TParse)(unsafe.Pointer(pParse)).FiPkSortOrder _ = libc.Int32FromInt32(0) (*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(-int32(1)) _sqlite3CreateIndex(tls, pParse, uintptr(0), uintptr(0), uintptr(0), pList, int32((*TTable)(unsafe.Pointer(pTab)).FkeyConf), uintptr(0), uintptr(0), 0, 0, uint8(SQLITE_IDXTYPE_PRIMARYKEY)) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { *(*Tu32)(unsafe.Pointer(pTab + 28)) &= uint32(^libc.Int32FromInt32(TF_WithoutRowid)) return } _ = libc.Int32FromInt32(0) pPk = _sqlite3PrimaryKeyIndex(tls, pTab) _ = libc.Int32FromInt32(0) } else { pPk = _sqlite3PrimaryKeyIndex(tls, pTab) _ = libc.Int32FromInt32(0) /* ** Remove all redundant columns from the PRIMARY KEY. For example, change ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later ** code assumes the PRIMARY KEY contains no repeated columns. */ v3 = libc.Int32FromInt32(1) j = v3 i = v3 for { if !(i < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) { break } if _isDupColumn(tls, pPk, j, pPk, i) != 0 { (*TIndex)(unsafe.Pointer(pPk)).FnColumn-- } else { *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(j)*4)) = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(i)*4)) *(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaSortOrder + uintptr(j))) = *(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaSortOrder + uintptr(i))) v4 = j j++ *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(v4)*2)) = *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2)) } goto _2 _2: ; i++ } (*TIndex)(unsafe.Pointer(pPk)).FnKeyCol = uint16(j) } _ = libc.Int32FromInt32(0) libc.SetBitFieldPtr16Uint32(pPk+56, libc.Uint32FromInt32(1), 5, 0x20) if !(int32(uint32(*(*uint8)(unsafe.Pointer(db + 172 + 8))&0x2>>1)) != 0) { libc.SetBitFieldPtr16Uint32(pPk+56, libc.Uint32FromInt32(1), 3, 0x8) } v5 = (*TIndex)(unsafe.Pointer(pPk)).FnKeyCol (*TIndex)(unsafe.Pointer(pPk)).FnColumn = v5 nPk = int32(v5) /* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema ** table entry. This is only required if currently generating VDBE ** code for a CREATE TABLE (not when parsing one as part of reading ** a database schema). */ if v != 0 && (*TIndex)(unsafe.Pointer(pPk)).Ftnum > uint32(0) { _ = libc.Int32FromInt32(0) _sqlite3VdbeChangeOpcode(tls, v, int32((*TIndex)(unsafe.Pointer(pPk)).Ftnum), uint8(OP_Goto)) } /* The root page of the PRIMARY KEY is the table root page */ (*TIndex)(unsafe.Pointer(pPk)).Ftnum = (*TTable)(unsafe.Pointer(pTab)).Ftnum /* Update the in-memory representation of all UNIQUE indices by converting ** the final rowid column into one or more columns of the PRIMARY KEY. */ pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0) { break } if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) { goto _6 } v8 = libc.Int32FromInt32(0) n = v8 i = v8 for { if !(i < nPk) { break } if !(_isDupColumn(tls, pIdx, int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol), pPk, i) != 0) { n++ } goto _7 _7: ; i++ } if n == 0 { /* This index is a superset of the primary key */ (*TIndex)(unsafe.Pointer(pIdx)).FnColumn = (*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol goto _6 } if _resizeIndexObject(tls, db, pIdx, int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)+n) != 0 { return } i = 0 j = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) for { if !(i < nPk) { break } if !(_isDupColumn(tls, pIdx, int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol), pPk, i) != 0) { *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j)*2)) = *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2)) *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(j)*4)) = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(i)*4)) if *(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaSortOrder + uintptr(i))) != 0 { /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */ libc.SetBitFieldPtr16Uint32(pIdx+56, libc.Uint32FromInt32(1), 10, 0x400) } j++ } goto _9 _9: ; i++ } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) goto _6 _6: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } /* Add all table columns to the PRIMARY KEY index */ nExtra = 0 i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if !(_hasColumn(tls, (*TIndex)(unsafe.Pointer(pPk)).FaiColumn, nPk, i) != 0) && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 { nExtra++ } goto _10 _10: ; i++ } if _resizeIndexObject(tls, db, pPk, nPk+nExtra) != 0 { return } i = 0 j = nPk for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if !(_hasColumn(tls, (*TIndex)(unsafe.Pointer(pPk)).FaiColumn, j, i) != 0) && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 { _ = libc.Int32FromInt32(0) *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(j)*2)) = int16(i) *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(j)*4)) = uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) j++ } goto _11 _11: ; i++ } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _recomputeColumnsNotIndexed(tls, pPk) } // C documentation // // /* // ** Return true if pTab is a virtual table and zName is a shadow table name // ** for that virtual table. // */ func _sqlite3IsShadowTableOf(tls *libc.TLS, db uintptr, pTab uintptr, zName uintptr) (r int32) { var nName int32 var pMod uintptr _, _ = nName, pMod /* Module for the virtual table */ if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) { return 0 } nName = _sqlite3Strlen30(tls, (*TTable)(unsafe.Pointer(pTab)).FzName) if Xsqlite3_strnicmp(tls, zName, (*TTable)(unsafe.Pointer(pTab)).FzName, nName) != 0 { return 0 } if int32(*(*uint8)(unsafe.Pointer(zName + uintptr(nName)))) != int32('_') { return 0 } pMod = _sqlite3HashFind(tls, db+404, *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).FazArg))) if pMod == uintptr(0) { return 0 } if (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FiVersion < int32(3) { return 0 } if (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxShadowName == uintptr(0) { return 0 } return (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxShadowName})))(tls, zName+uintptr(nName)+uintptr(1)) } // C documentation // // /* // ** Table pTab is a virtual table. If it the virtual table implementation // ** exists and has an xShadowName method, then loop over all other ordinary // ** tables within the same schema looking for shadow tables of pTab, and mark // ** any shadow tables seen using the TF_Shadow flag. // */ func _sqlite3MarkAllShadowTablesOf(tls *libc.TLS, db uintptr, pTab uintptr) { var k, pMod, pOther uintptr var nName int32 _, _, _, _ = k, nName, pMod, pOther /* For looping through the symbol table */ _ = libc.Int32FromInt32(0) pMod = _sqlite3HashFind(tls, db+404, *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).FazArg))) if pMod == uintptr(0) { return } if (*TModule)(unsafe.Pointer(pMod)).FpModule == uintptr(0) { return } if (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FiVersion < int32(3) { return } if (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxShadowName == uintptr(0) { return } _ = libc.Int32FromInt32(0) nName = _sqlite3Strlen30(tls, (*TTable)(unsafe.Pointer(pTab)).FzName) k = (*THash)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FpSchema + 8)).Ffirst for { if !(k != 0) { break } pOther = (*THashElem)(unsafe.Pointer(k)).Fdata _ = libc.Int32FromInt32(0) if !(int32((*TTable)(unsafe.Pointer(pOther)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) { goto _1 } if (*TTable)(unsafe.Pointer(pOther)).FtabFlags&uint32(TF_Shadow) != 0 { goto _1 } if Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pOther)).FzName, (*TTable)(unsafe.Pointer(pTab)).FzName, nName) == 0 && int32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer(pOther)).FzName + uintptr(nName)))) == int32('_') && (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxShadowName})))(tls, (*TTable)(unsafe.Pointer(pOther)).FzName+uintptr(nName)+uintptr(1)) != 0 { *(*Tu32)(unsafe.Pointer(pOther + 28)) |= uint32(TF_Shadow) } goto _1 _1: ; k = (*THashElem)(unsafe.Pointer(k)).Fnext } } // C documentation // // /* // ** Return true if zName is a shadow table name in the current database // ** connection. // ** // ** zName is temporarily modified while this routine is running, but is // ** restored to its original value prior to this routine returning. // */ func _sqlite3ShadowTableName(tls *libc.TLS, db uintptr, zName uintptr) (r int32) { var pTab, zTail uintptr _, _ = pTab, zTail /* Table that zName is a shadow of */ zTail = libc.Xstrrchr(tls, zName, int32('_')) if zTail == uintptr(0) { return 0 } *(*uint8)(unsafe.Pointer(zTail)) = uint8(0) pTab = _sqlite3FindTable(tls, db, zName, uintptr(0)) *(*uint8)(unsafe.Pointer(zTail)) = uint8('_') if pTab == uintptr(0) { return 0 } if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) { return 0 } return _sqlite3IsShadowTableOf(tls, db, pTab, zName) } // C documentation // // /* // ** This routine is called to report the final ")" that terminates // ** a CREATE TABLE statement. // ** // ** The table structure that other action routines have been building // ** is added to the internal hash tables, assuming no errors have // ** occurred. // ** // ** An entry for the table is made in the schema table on disk, unless // ** this is a temporary table or db->init.busy==1. When db->init.busy==1 // ** it means we are reading the sqlite_schema table because we just // ** connected to the database or because the sqlite_schema table has // ** recently changed, so the entry for this table already exists in // ** the sqlite_schema table. We do not want to create it again. // ** // ** If the pSelect argument is not NULL, it means that this routine // ** was called to create a table generated from a // ** "CREATE TABLE ... AS SELECT ..." statement. The column names of // ** the new table will match the result set of the SELECT. // */ func _sqlite3EndTable(tls *libc.TLS, pParse uintptr, pCons uintptr, pEnd uintptr, tabOpts Tu32, pSelect uintptr) { bp := tls.Alloc(96) defer tls.Free(96) var addrInsLoop, addrTop, iDb, ii, ii1, n, nNG, regRec, regRowid, regYield, v4, v6, v8 int32 var colFlags Tu32 var db, p, pCol, pDb, pEnd2, pIdx, pOld, pSchema, pSelTab, pX, v, zStmt, zType, zType2, v11, v5, v7, v9 uintptr var v10 Ti16 var _ /* dest at bp+0 */ TSelectDest _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrInsLoop, addrTop, colFlags, db, iDb, ii, ii1, n, nNG, p, pCol, pDb, pEnd2, pIdx, pOld, pSchema, pSelTab, pX, regRec, regRowid, regYield, v, zStmt, zType, zType2, v10, v11, v4, v5, v6, v7, v8, v9 /* The new table */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* An implied index of the table */ if pEnd == uintptr(0) && pSelect == uintptr(0) { return } p = (*TParse)(unsafe.Pointer(pParse)).FpNewTable if p == uintptr(0) { return } if pSelect == uintptr(0) && _sqlite3ShadowTableName(tls, db, (*TTable)(unsafe.Pointer(p)).FzName) != 0 { *(*Tu32)(unsafe.Pointer(p + 28)) |= uint32(TF_Shadow) } /* If the db->init.busy is 1 it means we are reading the SQL off the ** "sqlite_schema" or "sqlite_temp_schema" table on the disk. ** So do not write to the disk again. Extract the root page number ** for the table from the db->init.newTnum field. (The page number ** should have been put there by the sqliteOpenCb routine.) ** ** If the root page number is 1, that means this is the sqlite_schema ** table itself. So mark it read-only. */ if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 { if pSelect != 0 || !(int32((*TTable)(unsafe.Pointer(p)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) && (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FnewTnum != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+1648, 0) return } (*TTable)(unsafe.Pointer(p)).Ftnum = (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FnewTnum if (*TTable)(unsafe.Pointer(p)).Ftnum == uint32(1) { *(*Tu32)(unsafe.Pointer(p + 28)) |= uint32(TF_Readonly) } } /* Special processing for tables that include the STRICT keyword: ** ** * Do not allow custom column datatypes. Every column must have ** a datatype that is one of INT, INTEGER, REAL, TEXT, or BLOB. ** ** * If a PRIMARY KEY is defined, other than the INTEGER PRIMARY KEY, ** then all columns of the PRIMARY KEY must have a NOT NULL ** constraint. */ if tabOpts&uint32(TF_Strict) != 0 { *(*Tu32)(unsafe.Pointer(p + 28)) |= uint32(TF_Strict) ii = 0 for { if !(ii < int32((*TTable)(unsafe.Pointer(p)).FnCol)) { break } pCol = (*TTable)(unsafe.Pointer(p)).FaCol + uintptr(ii)*12 if int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf0>>4)) == COLTYPE_CUSTOM { if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_HASTYPE) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13140, libc.VaList(bp+40, (*TTable)(unsafe.Pointer(p)).FzName, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, _sqlite3ColumnType(tls, pCol, __ccgo_ts+1648))) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13173, libc.VaList(bp+40, (*TTable)(unsafe.Pointer(p)).FzName, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)) } return } else { if int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf0>>4)) == int32(COLTYPE_ANY) { (*TColumn)(unsafe.Pointer(pCol)).Faffinity = uint8(SQLITE_AFF_BLOB) } } if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 && int32((*TTable)(unsafe.Pointer(p)).FiPKey) != ii && int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf>>0)) == OE_None { libc.SetBitFieldPtr8Uint32(pCol+4, libc.Uint32FromInt32(OE_Abort), 0, 0xf) *(*Tu32)(unsafe.Pointer(p + 28)) |= uint32(TF_HasNotNull) } goto _1 _1: ; ii++ } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Special processing for WITHOUT ROWID Tables */ if tabOpts&uint32(TF_WithoutRowid) != 0 { if (*TTable)(unsafe.Pointer(p)).FtabFlags&uint32(TF_Autoincrement) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13200, 0) return } if (*TTable)(unsafe.Pointer(p)).FtabFlags&uint32(TF_HasPrimaryKey) == uint32(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13250, libc.VaList(bp+40, (*TTable)(unsafe.Pointer(p)).FzName)) return } *(*Tu32)(unsafe.Pointer(p + 28)) |= uint32(libc.Int32FromInt32(TF_WithoutRowid) | libc.Int32FromInt32(TF_NoVisibleRowid)) _convertToWithoutRowidTable(tls, pParse, p) } iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(p)).FpSchema) /* Resolve names in all CHECK constraint expressions. */ if (*TTable)(unsafe.Pointer(p)).FpCheck != 0 { _sqlite3ResolveSelfReference(tls, pParse, p, int32(NC_IsCheck), uintptr(0), (*TTable)(unsafe.Pointer(p)).FpCheck) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { /* If errors are seen, delete the CHECK constraints now, else they might ** actually be used if PRAGMA writable_schema=ON is set. */ _sqlite3ExprListDelete(tls, db, (*TTable)(unsafe.Pointer(p)).FpCheck) (*TTable)(unsafe.Pointer(p)).FpCheck = uintptr(0) } else { } } if (*TTable)(unsafe.Pointer(p)).FtabFlags&uint32(TF_HasGenerated) != 0 { nNG = 0 ii1 = 0 for { if !(ii1 < int32((*TTable)(unsafe.Pointer(p)).FnCol)) { break } colFlags = uint32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(p)).FaCol + uintptr(ii1)*12))).FcolFlags) if colFlags&uint32(COLFLAG_GENERATED) != uint32(0) { pX = _sqlite3ColumnExpr(tls, p, (*TTable)(unsafe.Pointer(p)).FaCol+uintptr(ii1)*12) if _sqlite3ResolveSelfReference(tls, pParse, p, int32(NC_GenCol), pX, uintptr(0)) != 0 { /* If there are errors in resolving the expression, change the ** expression to a NULL. This prevents code generators that operate ** on the expression from inserting extra parts into the expression ** tree that have been allocated from lookaside memory, which is ** illegal in a schema and will lead to errors or heap corruption ** when the database connection closes. */ _sqlite3ColumnSetExpr(tls, pParse, p, (*TTable)(unsafe.Pointer(p)).FaCol+uintptr(ii1)*12, _sqlite3ExprAlloc(tls, db, int32(TK_NULL), uintptr(0), 0)) } } else { nNG++ } goto _2 _2: ; ii1++ } if nNG == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13282, 0) return } } /* Estimate the average row size for the table and for all implied indices */ _estimateTableWidth(tls, p) pIdx = (*TTable)(unsafe.Pointer(p)).FpIndex for { if !(pIdx != 0) { break } _estimateIndexWidth(tls, pIdx) goto _3 _3: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } /* If not initializing, then create a record for the new table ** in the schema table of the database. ** ** If this is a TEMPORARY table, write the entry into the auxiliary ** file instead of into the main database file. */ if !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) { /* Text of the CREATE TABLE or CREATE VIEW statement */ v = _sqlite3GetVdbe(tls, pParse) if v == uintptr(0) { return } _sqlite3VdbeAddOp1(tls, v, int32(OP_Close), 0) /* ** Initialize zType for the new view or table. */ if int32((*TTable)(unsafe.Pointer(p)).FeTabType) == TABTYP_NORM { /* A regular table */ zType = __ccgo_ts + 9058 zType2 = __ccgo_ts + 13326 } else { /* A view */ zType = __ccgo_ts + 10796 zType2 = __ccgo_ts + 13332 } /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT ** statement to populate the new table. The root-page number for the ** new table is in register pParse->regRoot. ** ** Once the SELECT has been coded by sqlite3Select(), it is in a ** suitable state to query for the column names and types to be used ** by the new table. ** ** A shared-cache write-lock is not required to write to the new table, ** as a schema-lock must have already been obtained to create it. Since ** a schema-lock excludes all other database users, the write-lock would ** be redundant. */ if pSelect != 0 { /* A table that describes the SELECT results */ if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != PARSE_MODE_NORMAL { (*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_ERROR) (*TParse)(unsafe.Pointer(pParse)).FnErr++ return } v5 = pParse + 44 *(*int32)(unsafe.Pointer(v5))++ v4 = *(*int32)(unsafe.Pointer(v5)) regYield = v4 v7 = pParse + 44 *(*int32)(unsafe.Pointer(v7))++ v6 = *(*int32)(unsafe.Pointer(v7)) regRec = v6 v9 = pParse + 44 *(*int32)(unsafe.Pointer(v9))++ v8 = *(*int32)(unsafe.Pointer(v9)) regRowid = v8 _ = libc.Int32FromInt32(0) _sqlite3MayAbort(tls, pParse) _sqlite3VdbeAddOp3(tls, v, int32(OP_OpenWrite), int32(1), (*TParse)(unsafe.Pointer(pParse)).FregRoot, iDb) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_P2ISREG)) (*TParse)(unsafe.Pointer(pParse)).FnTab = int32(2) addrTop = _sqlite3VdbeCurrentAddr(tls, v) + int32(1) _sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), regYield, 0, addrTop) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return } pSelTab = _sqlite3ResultSetOfSelect(tls, pParse, pSelect, uint8(SQLITE_AFF_BLOB)) if pSelTab == uintptr(0) { return } _ = libc.Int32FromInt32(0) v10 = (*TTable)(unsafe.Pointer(pSelTab)).FnCol (*TTable)(unsafe.Pointer(p)).FnNVCol = v10 (*TTable)(unsafe.Pointer(p)).FnCol = v10 (*TTable)(unsafe.Pointer(p)).FaCol = (*TTable)(unsafe.Pointer(pSelTab)).FaCol (*TTable)(unsafe.Pointer(pSelTab)).FnCol = 0 (*TTable)(unsafe.Pointer(pSelTab)).FaCol = uintptr(0) _sqlite3DeleteTable(tls, db, pSelTab) _sqlite3SelectDestInit(tls, bp, int32(SRT_Coroutine), regYield) _sqlite3Select(tls, pParse, pSelect, bp) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return } _sqlite3VdbeEndCoroutine(tls, v, regYield) _sqlite3VdbeJumpHere(tls, v, addrTop-int32(1)) addrInsLoop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), (*(*TSelectDest)(unsafe.Pointer(bp))).FiSDParm) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), (*(*TSelectDest)(unsafe.Pointer(bp))).FiSdst, (*(*TSelectDest)(unsafe.Pointer(bp))).FnSdst, regRec) _sqlite3TableAffinity(tls, v, p, 0) _sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), int32(1), regRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), int32(1), regRec, regRowid) _sqlite3VdbeGoto(tls, v, addrInsLoop) _sqlite3VdbeJumpHere(tls, v, addrInsLoop) _sqlite3VdbeAddOp1(tls, v, int32(OP_Close), int32(1)) } /* Compute the complete text of the CREATE statement */ if pSelect != 0 { zStmt = _createTableStmt(tls, db, p) } else { if tabOpts != 0 { v11 = pParse + 208 } else { v11 = pEnd } pEnd2 = v11 n = int32((*TToken)(unsafe.Pointer(pEnd2)).Fz) - int32((*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fz) if int32(*(*uint8)(unsafe.Pointer((*TToken)(unsafe.Pointer(pEnd2)).Fz))) != int32(';') { n = int32(uint32(n) + (*TToken)(unsafe.Pointer(pEnd2)).Fn) } zStmt = _sqlite3MPrintf(tls, db, __ccgo_ts+13337, libc.VaList(bp+40, zType2, n, (*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fz)) } /* A slot for the record has already been allocated in the ** schema table. We just need to update that slot with all ** the information we've collected. */ _sqlite3NestedParse(tls, pParse, __ccgo_ts+13352, libc.VaList(bp+40, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, zType, (*TTable)(unsafe.Pointer(p)).FzName, (*TTable)(unsafe.Pointer(p)).FzName, (*TParse)(unsafe.Pointer(pParse)).FregRoot, zStmt, (*TParse)(unsafe.Pointer(pParse)).FregRowid)) _sqlite3DbFree(tls, db, zStmt) _sqlite3ChangeCookie(tls, pParse, iDb) /* Check to see if we need to create an sqlite_sequence table for ** keeping track of autoincrement keys. */ if (*TTable)(unsafe.Pointer(p)).FtabFlags&uint32(TF_Autoincrement) != uint32(0) && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != libc.Int32FromInt32(PARSE_MODE_NORMAL)) { pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16 _ = libc.Int32FromInt32(0) if (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).FpSeqTab == uintptr(0) { _sqlite3NestedParse(tls, pParse, __ccgo_ts+13450, libc.VaList(bp+40, (*TDb)(unsafe.Pointer(pDb)).FzDbSName)) } } /* Reparse everything to update our internal data structures */ _sqlite3VdbeAddParseSchemaOp(tls, v, iDb, _sqlite3MPrintf(tls, db, __ccgo_ts+13492, libc.VaList(bp+40, (*TTable)(unsafe.Pointer(p)).FzName)), uint16(0)) /* Test for cycles in generated columns and illegal expressions ** in CHECK constraints and in DEFAULT clauses. */ if (*TTable)(unsafe.Pointer(p)).FtabFlags&uint32(TF_HasGenerated) != 0 { _sqlite3VdbeAddOp4(tls, v, int32(OP_SqlExec), int32(1), 0, 0, _sqlite3MPrintf(tls, db, __ccgo_ts+13526, libc.VaList(bp+40, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, (*TTable)(unsafe.Pointer(p)).FzName)), -int32(6)) } _sqlite3VdbeAddOp4(tls, v, int32(OP_SqlExec), int32(1), 0, 0, _sqlite3MPrintf(tls, db, __ccgo_ts+13547, libc.VaList(bp+40, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, (*TTable)(unsafe.Pointer(p)).FzName)), -int32(6)) } /* Add the table to the in-memory representation of the database. */ if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 { pSchema = (*TTable)(unsafe.Pointer(p)).FpSchema _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pOld = _sqlite3HashInsert(tls, pSchema+8, (*TTable)(unsafe.Pointer(p)).FzName, p) if pOld != 0 { _ = libc.Int32FromInt32(0) /* Malloc must have failed inside HashInsert() */ _sqlite3OomFault(tls, db) return } (*TParse)(unsafe.Pointer(pParse)).FpNewTable = uintptr(0) *(*Tu32)(unsafe.Pointer(db + 24)) |= uint32(DBFLAG_SchemaChange) /* If this is the magic sqlite_sequence table used by autoincrement, ** then record a pointer to this table in the main database structure ** so that INSERT can find the table easily. */ _ = libc.Int32FromInt32(0) if libc.Xstrcmp(tls, (*TTable)(unsafe.Pointer(p)).FzName, __ccgo_ts+9580) == 0 { _ = libc.Int32FromInt32(0) (*TSchema)(unsafe.Pointer((*TTable)(unsafe.Pointer(p)).FpSchema)).FpSeqTab = p } } if !(pSelect != 0) && int32((*TTable)(unsafe.Pointer(p)).FeTabType) == TABTYP_NORM { _ = libc.Int32FromInt32(0) if (*TToken)(unsafe.Pointer(pCons)).Fz == uintptr(0) { pCons = pEnd } (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(p + 44))).FaddColOffset = int32(13) + (int32((*TToken)(unsafe.Pointer(pCons)).Fz) - int32((*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fz)) } } // C documentation // // /* // ** The parser calls this routine in order to create a new VIEW // */ func _sqlite3CreateView(tls *libc.TLS, pParse uintptr, pBegin uintptr, pName1 uintptr, pName2 uintptr, pCNames uintptr, pSelect uintptr, isTemp int32, noErr int32) { bp := tls.Alloc(64) defer tls.Free(64) var db, p, z uintptr var iDb, n int32 var _ /* pName at bp+60 */ uintptr var _ /* sEnd at bp+0 */ TToken var _ /* sFix at bp+8 */ TDbFixer _, _, _, _, _ = db, iDb, n, p, z *(*uintptr)(unsafe.Pointer(bp + 60)) = uintptr(0) db = (*TParse)(unsafe.Pointer(pParse)).Fdb if int32((*TParse)(unsafe.Pointer(pParse)).FnVar) > 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13579, 0) goto create_view_fail } _sqlite3StartTable(tls, pParse, pName1, pName2, isTemp, int32(1), 0, noErr) p = (*TParse)(unsafe.Pointer(pParse)).FpNewTable if p == uintptr(0) || (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto create_view_fail } /* Legacy versions of SQLite allowed the use of the magic "rowid" column ** on a view, even though views do not have rowids. The following flag ** setting fixes this problem. But the fix can be disabled by compiling ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that ** depend upon the old buggy behavior. The ability can also be toggled ** using sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW,...) */ *(*Tu32)(unsafe.Pointer(p + 28)) |= uint32(TF_NoVisibleRowid) /* Never allow rowid in view */ _sqlite3TwoPartName(tls, pParse, pName1, pName2, bp+60) iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(p)).FpSchema) _sqlite3FixInit(tls, bp+8, pParse, iDb, __ccgo_ts+10796, *(*uintptr)(unsafe.Pointer(bp + 60))) if _sqlite3FixSelect(tls, bp+8, pSelect) != 0 { goto create_view_fail } /* Make a copy of the entire SELECT statement that defines the view. ** This will force all the Expr.token.z values to be dynamically ** allocated rather than point to the input string - which means that ** they will persist after the current sqlite3_exec() call returns. */ *(*Tu32)(unsafe.Pointer(pSelect + 4)) |= uint32(SF_View) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { (*(*struct { FpSelect uintptr })(unsafe.Pointer(&(*TTable)(unsafe.Pointer(p)).Fu))).FpSelect = pSelect pSelect = uintptr(0) } else { (*(*struct { FpSelect uintptr })(unsafe.Pointer(&(*TTable)(unsafe.Pointer(p)).Fu))).FpSelect = _sqlite3SelectDup(tls, db, pSelect, int32(EXPRDUP_REDUCE)) } (*TTable)(unsafe.Pointer(p)).FpCheck = _sqlite3ExprListDup(tls, db, pCNames, int32(EXPRDUP_REDUCE)) (*TTable)(unsafe.Pointer(p)).FeTabType = uint8(TABTYP_VIEW) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto create_view_fail } /* Locate the end of the CREATE VIEW statement. Make sEnd point to ** the end. */ *(*TToken)(unsafe.Pointer(bp)) = (*TParse)(unsafe.Pointer(pParse)).FsLastToken _ = libc.Int32FromInt32(0) if int32(*(*uint8)(unsafe.Pointer((*(*TToken)(unsafe.Pointer(bp))).Fz))) != int32(';') { (*(*TToken)(unsafe.Pointer(bp))).Fz += uintptr((*(*TToken)(unsafe.Pointer(bp))).Fn) } (*(*TToken)(unsafe.Pointer(bp))).Fn = uint32(0) n = int32((*(*TToken)(unsafe.Pointer(bp))).Fz) - int32((*TToken)(unsafe.Pointer(pBegin)).Fz) _ = libc.Int32FromInt32(0) z = (*TToken)(unsafe.Pointer(pBegin)).Fz for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(n-int32(1))))])&int32(0x01) != 0 { n-- } (*(*TToken)(unsafe.Pointer(bp))).Fz = z + uintptr(n-int32(1)) (*(*TToken)(unsafe.Pointer(bp))).Fn = uint32(1) /* Use sqlite3EndTable() to add the view to the schema table */ _sqlite3EndTable(tls, pParse, uintptr(0), bp, uint32(0), uintptr(0)) goto create_view_fail create_view_fail: ; _sqlite3SelectDelete(tls, db, pSelect) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameExprlistUnmap(tls, pParse, pCNames) } _sqlite3ExprListDelete(tls, db, pCNames) return } // C documentation // // /* // ** The Table structure pTable is really a VIEW. Fill in the names of // ** the columns of the view in the pTable structure. Return the number // ** of errors. If an error is seen leave an error message in pParse->zErrMsg. // */ func _viewGetColumnNames(tls *libc.TLS, pParse uintptr, pTable uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var db, pSel, pSelTab, p2 uintptr var eParseMode Tu8 var nErr, nSelect, nTab, rc, v1 int32 var xAuth Tsqlite3_xauth _, _, _, _, _, _, _, _, _, _, _ = db, eParseMode, nErr, nSelect, nTab, pSel, pSelTab, rc, xAuth, v1, p2 /* Copy of the SELECT that implements the view */ nErr = 0 /* Number of errors encountered */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Saved xAuth pointer */ _ = libc.Int32FromInt32(0) if int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == int32(TABTYP_VTAB) { (*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock++ rc = _sqlite3VtabCallConnect(tls, pParse, pTable) (*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock-- return rc } /* A positive nCol means the columns names for this view are ** already known. This routine is not called unless either the ** table is virtual or nCol is zero. */ _ = libc.Int32FromInt32(0) /* A negative nCol is a special marker meaning that we are currently ** trying to compute the column names. If we enter this routine with ** a negative nCol, it means two or more views form a loop, like this: ** ** CREATE VIEW one AS SELECT * FROM two; ** CREATE VIEW two AS SELECT * FROM one; ** ** Actually, the error above is now caught prior to reaching this point. ** But the following test is still important as it does come up ** in the following: ** ** CREATE TABLE main.ex1(a); ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1; ** SELECT * FROM temp.ex1; */ if int32((*TTable)(unsafe.Pointer(pTable)).FnCol) < 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13615, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTable)).FzName)) return int32(1) } _ = libc.Int32FromInt32(0) /* If we get this far, it means we need to compute the table names. ** Note that the call to sqlite3ResultSetOfSelect() will expand any ** "*" elements in the results set of the view and will assign cursors ** to the elements of the FROM clause. But we do not want these changes ** to be permanent. So the computation is done on a copy of the SELECT ** statement that defines the view. */ _ = libc.Int32FromInt32(0) pSel = _sqlite3SelectDup(tls, db, (*(*struct { FpSelect uintptr })(unsafe.Pointer(&(*TTable)(unsafe.Pointer(pTable)).Fu))).FpSelect, 0) if pSel != 0 { eParseMode = (*TParse)(unsafe.Pointer(pParse)).FeParseMode nTab = (*TParse)(unsafe.Pointer(pParse)).FnTab nSelect = (*TParse)(unsafe.Pointer(pParse)).FnSelect (*TParse)(unsafe.Pointer(pParse)).FeParseMode = uint8(PARSE_MODE_NORMAL) _sqlite3SrcListAssignCursors(tls, pParse, (*TSelect)(unsafe.Pointer(pSel)).FpSrc) (*TTable)(unsafe.Pointer(pTable)).FnCol = int16(-int32(1)) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable++ (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0) xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0) pSelTab = _sqlite3ResultSetOfSelect(tls, pParse, pSel, uint8(SQLITE_AFF_NONE)) (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth (*TParse)(unsafe.Pointer(pParse)).FnTab = nTab (*TParse)(unsafe.Pointer(pParse)).FnSelect = nSelect if pSelTab == uintptr(0) { (*TTable)(unsafe.Pointer(pTable)).FnCol = 0 nErr++ } else { if (*TTable)(unsafe.Pointer(pTable)).FpCheck != 0 { /* CREATE VIEW name(arglist) AS ... ** The names of the columns in the table are taken from ** arglist which is stored in pTable->pCheck. The pCheck field ** normally holds CHECK constraints on an ordinary table, but for ** a VIEW it holds the list of column names. */ _sqlite3ColumnsFromExprList(tls, pParse, (*TTable)(unsafe.Pointer(pTable)).FpCheck, pTable+34, pTable+4) if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 && int32((*TTable)(unsafe.Pointer(pTable)).FnCol) == (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSel)).FpEList)).FnExpr { _ = libc.Int32FromInt32(0) _sqlite3SubqueryColumnTypes(tls, pParse, pTable, pSel, uint8(SQLITE_AFF_NONE)) } } else { /* CREATE VIEW name AS... without an argument list. Construct ** the column names from the SELECT statement that defines the view. */ _ = libc.Int32FromInt32(0) (*TTable)(unsafe.Pointer(pTable)).FnCol = (*TTable)(unsafe.Pointer(pSelTab)).FnCol (*TTable)(unsafe.Pointer(pTable)).FaCol = (*TTable)(unsafe.Pointer(pSelTab)).FaCol *(*Tu32)(unsafe.Pointer(pTable + 28)) |= (*TTable)(unsafe.Pointer(pSelTab)).FtabFlags & uint32(COLFLAG_NOINSERT) (*TTable)(unsafe.Pointer(pSelTab)).FnCol = 0 (*TTable)(unsafe.Pointer(pSelTab)).FaCol = uintptr(0) _ = libc.Int32FromInt32(0) } } (*TTable)(unsafe.Pointer(pTable)).FnNVCol = (*TTable)(unsafe.Pointer(pTable)).FnCol _sqlite3DeleteTable(tls, db, pSelTab) _sqlite3SelectDelete(tls, db, pSel) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable-- if (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable != 0 { v1 = 0 } else { v1 = int32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue) } (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(v1) (*TParse)(unsafe.Pointer(pParse)).FeParseMode = eParseMode } else { nErr++ } p2 = (*TTable)(unsafe.Pointer(pTable)).FpSchema + 78 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(DB_UnresetViews)) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _sqlite3DeleteColumnNames(tls, db, pTable) } return nErr } func _sqlite3ViewGetColumnNames(tls *libc.TLS, pParse uintptr, pTable uintptr) (r int32) { _ = libc.Int32FromInt32(0) if !(int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) && int32((*TTable)(unsafe.Pointer(pTable)).FnCol) > 0 { return 0 } return _viewGetColumnNames(tls, pParse, pTable) } // C documentation // // /* // ** Clear the column names from every VIEW in database idx. // */ func _sqliteViewResetAll(tls *libc.TLS, db uintptr, idx int32) { var i, pTab, p2 uintptr _, _, _ = i, pTab, p2 _ = libc.Int32FromInt32(0) if !(int32((*TSchema)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(idx)*16))).FpSchema)).FschemaFlags)&libc.Int32FromInt32(DB_UnresetViews) == libc.Int32FromInt32(DB_UnresetViews)) { return } i = (*THash)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(idx)*16))).FpSchema + 8)).Ffirst for { if !(i != 0) { break } pTab = (*THashElem)(unsafe.Pointer(i)).Fdata if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { _sqlite3DeleteColumnNames(tls, db, pTab) } goto _1 _1: ; i = (*THashElem)(unsafe.Pointer(i)).Fnext } p2 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(idx)*16))).FpSchema + 78 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) & ^libc.Int32FromInt32(DB_UnresetViews)) } // C documentation // // /* // ** This function is called by the VDBE to adjust the internal schema // ** used by SQLite when the btree layer moves a table root page. The // ** root-page of a table or index in database iDb has changed from iFrom // ** to iTo. // ** // ** Ticket #1728: The symbol table might still contain information // ** on tables and/or indices that are the process of being deleted. // ** If you are unlucky, one of those deleted indices or tables might // ** have the same rootpage number as the real table or index that is // ** being moved. So we cannot stop searching after the first match // ** because the first match might be for one of the deleted indices // ** or tables and not the table/index that is actually being moved. // ** We must continue looping until all tables and indices with // ** rootpage==iFrom have been converted to have a rootpage of iTo // ** in order to be certain that we got the right one. // */ func _sqlite3RootPageMoved(tls *libc.TLS, db uintptr, iDb int32, iFrom TPgno, iTo TPgno) { var pDb, pElem, pHash, pIdx, pTab uintptr _, _, _, _, _ = pDb, pElem, pHash, pIdx, pTab _ = libc.Int32FromInt32(0) pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16 pHash = (*TDb)(unsafe.Pointer(pDb)).FpSchema + 8 pElem = (*THash)(unsafe.Pointer(pHash)).Ffirst for { if !(pElem != 0) { break } pTab = (*THashElem)(unsafe.Pointer(pElem)).Fdata if (*TTable)(unsafe.Pointer(pTab)).Ftnum == iFrom { (*TTable)(unsafe.Pointer(pTab)).Ftnum = iTo } goto _1 _1: ; pElem = (*THashElem)(unsafe.Pointer(pElem)).Fnext } pHash = (*TDb)(unsafe.Pointer(pDb)).FpSchema + 24 pElem = (*THash)(unsafe.Pointer(pHash)).Ffirst for { if !(pElem != 0) { break } pIdx = (*THashElem)(unsafe.Pointer(pElem)).Fdata if (*TIndex)(unsafe.Pointer(pIdx)).Ftnum == iFrom { (*TIndex)(unsafe.Pointer(pIdx)).Ftnum = iTo } goto _2 _2: ; pElem = (*THashElem)(unsafe.Pointer(pElem)).Fnext } } // C documentation // // /* // ** Write code to erase the table with root-page iTable from database iDb. // ** Also write code to modify the sqlite_schema table and internal schema // ** if a root-page of another table is moved by the btree-layer whilst // ** erasing iTable (this can happen with an auto-vacuum database). // */ func _destroyRootPage(tls *libc.TLS, pParse uintptr, iTable int32, iDb int32) { bp := tls.Alloc(48) defer tls.Free(48) var r1 int32 var v uintptr _, _ = r1, v v = _sqlite3GetVdbe(tls, pParse) r1 = _sqlite3GetTempReg(tls, pParse) if iTable < int32(2) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13645, 0) } _sqlite3VdbeAddOp3(tls, v, int32(OP_Destroy), iTable, r1, iDb) _sqlite3MayAbort(tls, pParse) /* OP_Destroy stores an in integer r1. If this integer ** is non-zero, then it is the root page number of a table moved to ** location iTable. The following code modifies the sqlite_schema table to ** reflect this. ** ** The "#NNN" in the SQL is a special constant that means whatever value ** is in register NNN. See grammar rules associated with the TK_REGISTER ** token for additional information. */ _sqlite3NestedParse(tls, pParse, __ccgo_ts+13660, libc.VaList(bp+8, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + uintptr(iDb)*16))).FzDbSName, iTable, r1, r1)) _sqlite3ReleaseTempReg(tls, pParse, r1) } // C documentation // // /* // ** Write VDBE code to erase table pTab and all associated indices on disk. // ** Code to update the sqlite_schema tables and internal schema definitions // ** in case a root-page belonging to another table is moved by the btree layer // ** is also added (this can happen with an auto-vacuum database). // */ func _destroyTable(tls *libc.TLS, pParse uintptr, pTab uintptr) { var iDb int32 var iDestroyed, iIdx, iLargest, iTab TPgno var pIdx uintptr _, _, _, _, _, _ = iDb, iDestroyed, iIdx, iLargest, iTab, pIdx /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM ** is not defined), then it is important to call OP_Destroy on the ** table and index root-pages in order, starting with the numerically ** largest root-page number. This guarantees that none of the root-pages ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the ** following were coded: ** ** OP_Destroy 4 0 ** ... ** OP_Destroy 5 0 ** ** and root page 5 happened to be the largest root-page number in the ** database, then root page 5 would be moved to page 4 by the ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit ** a free-list page. */ iTab = (*TTable)(unsafe.Pointer(pTab)).Ftnum iDestroyed = uint32(0) for int32(1) != 0 { iLargest = uint32(0) if iDestroyed == uint32(0) || iTab < iDestroyed { iLargest = iTab } pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0) { break } iIdx = (*TIndex)(unsafe.Pointer(pIdx)).Ftnum _ = libc.Int32FromInt32(0) if (iDestroyed == uint32(0) || iIdx < iDestroyed) && iIdx > iLargest { iLargest = iIdx } goto _1 _1: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } if iLargest == uint32(0) { return } else { iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTable)(unsafe.Pointer(pTab)).FpSchema) _ = libc.Int32FromInt32(0) _destroyRootPage(tls, pParse, int32(iLargest), iDb) iDestroyed = iLargest } } } // C documentation // // /* // ** Remove entries from the sqlite_statN tables (for N in (1,2,3)) // ** after a DROP INDEX or DROP TABLE command. // */ func _sqlite3ClearStatTables(tls *libc.TLS, pParse uintptr, iDb int32, zType uintptr, zName uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var i int32 var zDbName uintptr var _ /* zTab at bp+0 */ [24]uint8 _, _ = i, zDbName zDbName = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + uintptr(iDb)*16))).FzDbSName i = int32(1) for { if !(i <= int32(4)) { break } Xsqlite3_snprintf(tls, int32(24), bp, __ccgo_ts+13727, libc.VaList(bp+32, i)) if _sqlite3FindTable(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, bp, zDbName) != 0 { _sqlite3NestedParse(tls, pParse, __ccgo_ts+11685, libc.VaList(bp+32, zDbName, bp, zType, zName)) } goto _1 _1: ; i++ } } // C documentation // // /* // ** Generate code to drop a table. // */ func _sqlite3CodeDropTable(tls *libc.TLS, pParse uintptr, pTab uintptr, iDb int32, isView int32) { bp := tls.Alloc(32) defer tls.Free(32) var db, pDb, pTrigger, v uintptr _, _, _, _ = db, pDb, pTrigger, v db = (*TParse)(unsafe.Pointer(pParse)).Fdb pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16 v = _sqlite3GetVdbe(tls, pParse) _ = libc.Int32FromInt32(0) _sqlite3BeginWriteOperation(tls, pParse, int32(1), iDb) if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { _sqlite3VdbeAddOp0(tls, v, int32(OP_VBegin)) } /* Drop all triggers associated with the table being dropped. Code ** is generated to remove entries from sqlite_schema and/or ** sqlite_temp_schema if required. */ pTrigger = _sqlite3TriggerList(tls, pParse, pTab) for pTrigger != 0 { _ = libc.Int32FromInt32(0) _sqlite3DropTriggerPtr(tls, pParse, pTrigger) pTrigger = (*TTrigger)(unsafe.Pointer(pTrigger)).FpNext } /* Remove any entries of the sqlite_sequence table associated with ** the table being dropped. This is done before the table is dropped ** at the btree level, in case the sqlite_sequence table needs to ** move as a result of the drop (can happen in auto-vacuum mode). */ if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Autoincrement) != 0 { _sqlite3NestedParse(tls, pParse, __ccgo_ts+13741, libc.VaList(bp+8, (*TDb)(unsafe.Pointer(pDb)).FzDbSName, (*TTable)(unsafe.Pointer(pTab)).FzName)) } /* Drop all entries in the schema table that refer to the ** table. The program name loops through the schema table and deletes ** every row that refers to a table of the same name as the one being ** dropped. Triggers are handled separately because a trigger can be ** created in the temp database that refers to a table in another ** database. */ _sqlite3NestedParse(tls, pParse, __ccgo_ts+13786, libc.VaList(bp+8, (*TDb)(unsafe.Pointer(pDb)).FzDbSName, (*TTable)(unsafe.Pointer(pTab)).FzName)) if !(isView != 0) && !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) { _destroyTable(tls, pParse, pTab) } /* Remove the table entry from SQLite's internal schema and modify ** the schema cookie. */ if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { _sqlite3VdbeAddOp4(tls, v, int32(OP_VDestroy), iDb, 0, 0, (*TTable)(unsafe.Pointer(pTab)).FzName, 0) _sqlite3MayAbort(tls, pParse) } _sqlite3VdbeAddOp4(tls, v, int32(OP_DropTable), iDb, 0, 0, (*TTable)(unsafe.Pointer(pTab)).FzName, 0) _sqlite3ChangeCookie(tls, pParse, iDb) _sqliteViewResetAll(tls, db, iDb) } // C documentation // // /* // ** Return TRUE if shadow tables should be read-only in the current // ** context. // */ func _sqlite3ReadOnlyShadowTables(tls *libc.TLS, db uintptr) (r int32) { if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_Defensive) != uint64(0) && (*Tsqlite3)(unsafe.Pointer(db)).FpVtabCtx == uintptr(0) && (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec == 0 && !((*Tsqlite3)(unsafe.Pointer(db)).FnVTrans > 0 && (*Tsqlite3)(unsafe.Pointer(db)).FaVTrans == uintptr(0)) { return int32(1) } return 0 } // C documentation // // /* // ** Return true if it is not allowed to drop the given table // */ func _tableMayNotBeDropped(tls *libc.TLS, db uintptr, pTab uintptr) (r int32) { if Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName, __ccgo_ts+6525, int32(7)) == 0 { if Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName+uintptr(7), __ccgo_ts+3418, int32(4)) == 0 { return 0 } if Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName+uintptr(7), __ccgo_ts+7266, int32(10)) == 0 { return 0 } return int32(1) } if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Shadow) != uint32(0) && _sqlite3ReadOnlyShadowTables(tls, db) != 0 { return int32(1) } if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Eponymous) != 0 { return int32(1) } return 0 } // C documentation // // /* // ** This routine is called to do the work of a DROP TABLE statement. // ** pName is the name of the table to be dropped. // */ func _sqlite3DropTable(tls *libc.TLS, pParse uintptr, pName uintptr, isView int32, noErr int32) { bp := tls.Alloc(16) defer tls.Free(16) var code, iDb int32 var db, pTab, v, zArg2, zDb, zTab, v1 uintptr _, _, _, _, _, _, _, _, _ = code, db, iDb, pTab, v, zArg2, zDb, zTab, v1 db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto exit_drop_table } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if _sqlite3ReadSchema(tls, pParse) != 0 { goto exit_drop_table } if noErr != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FsuppressErr++ } _ = libc.Int32FromInt32(0) pTab = _sqlite3LocateTableItem(tls, pParse, uint32(isView), pName+8) if noErr != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FsuppressErr-- } if pTab == uintptr(0) { if noErr != 0 { _sqlite3CodeVerifyNamedSchema(tls, pParse, (*(*TSrcItem)(unsafe.Pointer(pName + 8))).FzDatabase) _sqlite3ForceNotReadOnly(tls, pParse) } goto exit_drop_table } iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) _ = libc.Int32FromInt32(0) /* If pTab is a virtual table, call ViewGetColumnNames() to ensure ** it is initialized. */ if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) && _sqlite3ViewGetColumnNames(tls, pParse, pTab) != 0 { goto exit_drop_table } if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) { v1 = __ccgo_ts + 6533 } else { v1 = __ccgo_ts + 6066 } zTab = v1 zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName zArg2 = uintptr(0) if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_DELETE), zTab, uintptr(0), zDb) != 0 { goto exit_drop_table } if isView != 0 { if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) { code = int32(SQLITE_DROP_TEMP_VIEW) } else { code = int32(SQLITE_DROP_VIEW) } } else { if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { code = int32(SQLITE_DROP_VTABLE) zArg2 = (*TModule)(unsafe.Pointer((*TVTable)(unsafe.Pointer(_sqlite3GetVTable(tls, db, pTab))).FpMod)).FzName } else { if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) { code = int32(SQLITE_DROP_TEMP_TABLE) } else { code = int32(SQLITE_DROP_TABLE) } } } if _sqlite3AuthCheck(tls, pParse, code, (*TTable)(unsafe.Pointer(pTab)).FzName, zArg2, zDb) != 0 { goto exit_drop_table } if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_DELETE), (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0), zDb) != 0 { goto exit_drop_table } if _tableMayNotBeDropped(tls, db, pTab) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13853, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) goto exit_drop_table } /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used ** on a table. */ if isView != 0 && !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VIEW)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13881, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) goto exit_drop_table } if !(isView != 0) && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13915, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) goto exit_drop_table } /* Generate code to remove the table from the schema table ** on disk. */ v = _sqlite3GetVdbe(tls, pParse) if v != 0 { _sqlite3BeginWriteOperation(tls, pParse, int32(1), iDb) if !(isView != 0) { _sqlite3ClearStatTables(tls, pParse, iDb, __ccgo_ts+11797, (*TTable)(unsafe.Pointer(pTab)).FzName) _sqlite3FkDropTable(tls, pParse, pName, pTab) } _sqlite3CodeDropTable(tls, pParse, pTab, iDb, isView) } goto exit_drop_table exit_drop_table: ; _sqlite3SrcListDelete(tls, db, pName) } // C documentation // // /* // ** This routine is called to create a new foreign key on the table // ** currently under construction. pFromCol determines which columns // ** in the current table point to the foreign key. If pFromCol==0 then // ** connect the key to the last column inserted. pTo is the name of // ** the table referred to (a.k.a the "parent" table). pToCol is a list // ** of tables in the parent pTo table. flags contains all // ** information about the conflict resolution algorithms specified // ** in the ON DELETE, ON UPDATE and ON INSERT clauses. // ** // ** An FKey structure is created and added to the table currently // ** under construction in the pParse->pNewTable field. // ** // ** The foreign key is set for IMMEDIATE processing. A subsequent call // ** to sqlite3DeferForeignKey() might change this to DEFERRED. // */ func _sqlite3CreateForeignKey(tls *libc.TLS, pParse uintptr, pFromCol uintptr, pTo uintptr, pToCol uintptr, flags int32) { bp := tls.Alloc(32) defer tls.Free(32) var db, p, pFKey, pNextTo, z uintptr var i, iCol, j, n, nCol int32 var nByte Ti64 _, _, _, _, _, _, _, _, _, _, _ = db, i, iCol, j, n, nByte, nCol, p, pFKey, pNextTo, z db = (*TParse)(unsafe.Pointer(pParse)).Fdb pFKey = uintptr(0) p = (*TParse)(unsafe.Pointer(pParse)).FpNewTable _ = libc.Int32FromInt32(0) if p == uintptr(0) || int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) == int32(PARSE_MODE_DECLARE_VTAB) { goto fk_end } if pFromCol == uintptr(0) { iCol = int32((*TTable)(unsafe.Pointer(p)).FnCol) - int32(1) if iCol < 0 { goto fk_end } if pToCol != 0 && (*TExprList)(unsafe.Pointer(pToCol)).FnExpr != int32(1) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13947, libc.VaList(bp+8, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(p)).FaCol + uintptr(iCol)*12))).FzCnName, pTo)) goto fk_end } nCol = int32(1) } else { if pToCol != 0 && (*TExprList)(unsafe.Pointer(pToCol)).FnExpr != (*TExprList)(unsafe.Pointer(pFromCol)).FnExpr { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14010, 0) goto fk_end } else { nCol = (*TExprList)(unsafe.Pointer(pFromCol)).FnExpr } } nByte = int64(uint32(44) + uint32(nCol-libc.Int32FromInt32(1))*uint32(8) + (*TToken)(unsafe.Pointer(pTo)).Fn + uint32(1)) if pToCol != 0 { i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pToCol)).FnExpr) { break } nByte += int64(_sqlite3Strlen30(tls, (*(*TExprList_item)(unsafe.Pointer(pToCol + 8 + uintptr(i)*20))).FzEName) + int32(1)) goto _1 _1: ; i++ } } pFKey = _sqlite3DbMallocZero(tls, db, uint64(nByte)) if pFKey == uintptr(0) { goto fk_end } (*TFKey)(unsafe.Pointer(pFKey)).FpFrom = p _ = libc.Int32FromInt32(0) (*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(p + 44))).FpFKey z = pFKey + 36 + uintptr(nCol)*8 (*TFKey)(unsafe.Pointer(pFKey)).FzTo = z if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenMap(tls, pParse, z, pTo) } libc.Xmemcpy(tls, z, (*TToken)(unsafe.Pointer(pTo)).Fz, (*TToken)(unsafe.Pointer(pTo)).Fn) *(*uint8)(unsafe.Pointer(z + uintptr((*TToken)(unsafe.Pointer(pTo)).Fn))) = uint8(0) _sqlite3Dequote(tls, z) z += uintptr((*TToken)(unsafe.Pointer(pTo)).Fn + uint32(1)) (*TFKey)(unsafe.Pointer(pFKey)).FnCol = nCol if pFromCol == uintptr(0) { (*(*TsColMap)(unsafe.Pointer(pFKey + 36))).FiFrom = int32((*TTable)(unsafe.Pointer(p)).FnCol) - int32(1) } else { i = 0 for { if !(i < nCol) { break } j = 0 for { if !(j < int32((*TTable)(unsafe.Pointer(p)).FnCol)) { break } if _sqlite3StrICmp(tls, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(p)).FaCol + uintptr(j)*12))).FzCnName, (*(*TExprList_item)(unsafe.Pointer(pFromCol + 8 + uintptr(i)*20))).FzEName) == 0 { (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(i)*8))).FiFrom = j break } goto _3 _3: ; j++ } if j >= int32((*TTable)(unsafe.Pointer(p)).FnCol) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14104, libc.VaList(bp+8, (*(*TExprList_item)(unsafe.Pointer(pFromCol + 8 + uintptr(i)*20))).FzEName)) goto fk_end } if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenRemap(tls, pParse, pFKey+36+uintptr(i)*8, (*(*TExprList_item)(unsafe.Pointer(pFromCol + 8 + uintptr(i)*20))).FzEName) } goto _2 _2: ; i++ } } if pToCol != 0 { i = 0 for { if !(i < nCol) { break } n = _sqlite3Strlen30(tls, (*(*TExprList_item)(unsafe.Pointer(pToCol + 8 + uintptr(i)*20))).FzEName) (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(i)*8))).FzCol = z if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenRemap(tls, pParse, z, (*(*TExprList_item)(unsafe.Pointer(pToCol + 8 + uintptr(i)*20))).FzEName) } libc.Xmemcpy(tls, z, (*(*TExprList_item)(unsafe.Pointer(pToCol + 8 + uintptr(i)*20))).FzEName, uint32(n)) *(*uint8)(unsafe.Pointer(z + uintptr(n))) = uint8(0) z += uintptr(n + int32(1)) goto _4 _4: ; i++ } } (*TFKey)(unsafe.Pointer(pFKey)).FisDeferred = uint8(0) *(*Tu8)(unsafe.Pointer(pFKey + 25)) = uint8(flags & libc.Int32FromInt32(0xff)) /* ON DELETE action */ *(*Tu8)(unsafe.Pointer(pFKey + 25 + 1)) = uint8(flags >> libc.Int32FromInt32(8) & libc.Int32FromInt32(0xff)) /* ON UPDATE action */ _ = libc.Int32FromInt32(0) pNextTo = _sqlite3HashInsert(tls, (*TTable)(unsafe.Pointer(p)).FpSchema+56, (*TFKey)(unsafe.Pointer(pFKey)).FzTo, pFKey) if pNextTo == pFKey { _sqlite3OomFault(tls, db) goto fk_end } if pNextTo != 0 { _ = libc.Int32FromInt32(0) (*TFKey)(unsafe.Pointer(pFKey)).FpNextTo = pNextTo (*TFKey)(unsafe.Pointer(pNextTo)).FpPrevTo = pFKey } /* Link the foreign key to the table as the last step. */ _ = libc.Int32FromInt32(0) (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(p + 44))).FpFKey = pFKey pFKey = uintptr(0) goto fk_end fk_end: ; _sqlite3DbFree(tls, db, pFKey) _sqlite3ExprListDelete(tls, db, pFromCol) _sqlite3ExprListDelete(tls, db, pToCol) } // C documentation // // /* // ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED // ** clause is seen as part of a foreign key definition. The isDeferred // ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. // ** The behavior of the most recently created foreign key is adjusted // ** accordingly. // */ func _sqlite3DeferForeignKey(tls *libc.TLS, pParse uintptr, isDeferred int32) { var pFKey, pTab, v1, v2 uintptr _, _, _, _ = pFKey, pTab, v1, v2 v1 = (*TParse)(unsafe.Pointer(pParse)).FpNewTable pTab = v1 if v1 == uintptr(0) { return } if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) { return } v2 = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpFKey pFKey = v2 if v2 == uintptr(0) { return } _ = libc.Int32FromInt32(0) /* EV: R-30323-21917 */ (*TFKey)(unsafe.Pointer(pFKey)).FisDeferred = uint8(isDeferred) } // C documentation // // /* // ** Generate code that will erase and refill index *pIdx. This is // ** used to initialize a newly created index or to recompute the // ** content of an index in response to a REINDEX command. // ** // ** if memRootPage is not negative, it means that the index is newly // ** created. The register specified by memRootPage contains the // ** root page number of the index. If memRootPage is negative, then // ** the index already exists and must be cleared before being refilled and // ** the root page number of the index is taken from pIndex->tnum. // */ func _sqlite3RefillIndex(tls *libc.TLS, pParse uintptr, pIndex uintptr, memRootPage int32) { bp := tls.Alloc(16) defer tls.Free(16) var addr1, addr2, iDb, iIdx, iSorter, iTab, j2, regRecord, v1, v3, v5, v7 int32 var db, pKey, pTab, v, v2, v4, v6 uintptr var tnum TPgno var _ /* iPartIdxLabel at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr1, addr2, db, iDb, iIdx, iSorter, iTab, j2, pKey, pTab, regRecord, tnum, v, v1, v2, v3, v4, v5, v6, v7 pTab = (*TIndex)(unsafe.Pointer(pIndex)).FpTable v2 = pParse + 40 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ /* The table that is indexed */ iTab = v1 v4 = pParse + 40 v3 = *(*int32)(unsafe.Pointer(v4)) *(*int32)(unsafe.Pointer(v4))++ /* Btree cursor used for pTab */ iIdx = v3 /* Register holding assembled index record */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* The database connection */ iDb = _sqlite3SchemaToIndex(tls, db, (*TIndex)(unsafe.Pointer(pIndex)).FpSchema) if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_REINDEX), (*TIndex)(unsafe.Pointer(pIndex)).FzName, uintptr(0), (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName) != 0 { return } /* Require a write-lock on the table to perform this operation */ _sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab)).Ftnum, uint8(1), (*TTable)(unsafe.Pointer(pTab)).FzName) v = _sqlite3GetVdbe(tls, pParse) if v == uintptr(0) { return } if memRootPage >= 0 { tnum = uint32(memRootPage) } else { tnum = (*TIndex)(unsafe.Pointer(pIndex)).Ftnum } pKey = _sqlite3KeyInfoOfIndex(tls, pParse, pIndex) _ = libc.Int32FromInt32(0) /* Open the sorter cursor if we are to use one. */ v6 = pParse + 40 v5 = *(*int32)(unsafe.Pointer(v6)) *(*int32)(unsafe.Pointer(v6))++ iSorter = v5 _sqlite3VdbeAddOp4(tls, v, int32(OP_SorterOpen), iSorter, 0, int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol), _sqlite3KeyInfoRef(tls, pKey), -int32(8)) /* Open the table. Loop through all rows of the table, inserting index ** records into the sorter. */ _sqlite3OpenTable(tls, pParse, iTab, iDb, pTab, int32(OP_OpenRead)) addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), iTab, 0) regRecord = _sqlite3GetTempReg(tls, pParse) _sqlite3MultiWrite(tls, pParse) _sqlite3GenerateIndexKey(tls, pParse, pIndex, iTab, regRecord, 0, bp, uintptr(0), 0) _sqlite3VdbeAddOp2(tls, v, int32(OP_SorterInsert), iSorter, regRecord) _sqlite3ResolvePartIdxLabel(tls, pParse, *(*int32)(unsafe.Pointer(bp))) _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iTab, addr1+int32(1)) _sqlite3VdbeJumpHere(tls, v, addr1) if memRootPage < 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Clear), int32(tnum), iDb) } _sqlite3VdbeAddOp4(tls, v, int32(OP_OpenWrite), iIdx, int32(tnum), iDb, pKey, -int32(8)) if memRootPage >= 0 { v7 = int32(OPFLAG_P2ISREG) } else { v7 = 0 } _sqlite3VdbeChangeP5(tls, v, uint16(int32(OPFLAG_BULKCSR)|v7)) addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_SorterSort), iSorter, 0) if int32((*TIndex)(unsafe.Pointer(pIndex)).FonError) != OE_None { j2 = _sqlite3VdbeGoto(tls, v, int32(1)) addr2 = _sqlite3VdbeCurrentAddr(tls, v) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_SorterCompare), iSorter, j2, regRecord, int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol)) _sqlite3UniqueConstraint(tls, pParse, int32(OE_Abort), pIndex) _sqlite3VdbeJumpHere(tls, v, j2) } else { /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not ** abort. The exception is if one of the indexed expressions contains a ** user function that throws an exception when it is evaluated. But the ** overhead of adding a statement journal to a CREATE INDEX statement is ** very small (since most of the pages written do not contain content that ** needs to be restored if the statement aborts), so we call ** sqlite3MayAbort() for all CREATE INDEX statements. */ _sqlite3MayAbort(tls, pParse) addr2 = _sqlite3VdbeCurrentAddr(tls, v) } _sqlite3VdbeAddOp3(tls, v, int32(OP_SorterData), iSorter, regRecord, iIdx) if !(int32(uint32(*(*uint16)(unsafe.Pointer(pIndex + 56))&0x400>>10)) != 0) { /* This OP_SeekEnd opcode makes index insert for a REINDEX go much ** faster by avoiding unnecessary seeks. But the optimization does ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables ** with DESC primary keys, since those indexes have there keys in ** a different order from the main table. ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf */ _sqlite3VdbeAddOp1(tls, v, int32(OP_SeekEnd), iIdx) } _sqlite3VdbeAddOp2(tls, v, int32(OP_IdxInsert), iIdx, regRecord) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_USESEEKRESULT)) _sqlite3ReleaseTempReg(tls, pParse, regRecord) _sqlite3VdbeAddOp2(tls, v, int32(OP_SorterNext), iSorter, addr2) _sqlite3VdbeJumpHere(tls, v, addr1) _sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iTab) _sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iIdx) _sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iSorter) } // C documentation // // /* // ** Allocate heap space to hold an Index object with nCol columns. // ** // ** Increase the allocation size to provide an extra nExtra bytes // ** of 8-byte aligned space after the Index object and return a // ** pointer to this extra space in *ppExtra. // */ func _sqlite3AllocateIndexObject(tls *libc.TLS, db uintptr, nCol Ti16, nExtra int32, ppExtra uintptr) (r uintptr) { var nByte int32 var p, pExtra uintptr _, _, _ = nByte, p, pExtra /* Bytes of space for Index object + arrays */ nByte = int32((libc.Uint32FromInt64(104)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)) + (uint32(4)*uint32(nCol)+uint32(7))&uint32(^libc.Int32FromInt32(7)) + (uint32(2)*uint32(int32(nCol)+libc.Int32FromInt32(1))+uint32(2)*uint32(nCol)+uint32(1)*uint32(nCol)+uint32(7))&uint32(^libc.Int32FromInt32(7))) /* Index.aSortOrder */ p = _sqlite3DbMallocZero(tls, db, uint64(nByte+nExtra)) if p != 0 { pExtra = p + uintptr((libc.Uint32FromInt64(104)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))) (*TIndex)(unsafe.Pointer(p)).FazColl = pExtra pExtra += uintptr((libc.Uint32FromInt64(4)*uint32(nCol) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7))) (*TIndex)(unsafe.Pointer(p)).FaiRowLogEst = pExtra pExtra += uintptr(uint32(2) * uint32(int32(nCol)+libc.Int32FromInt32(1))) (*TIndex)(unsafe.Pointer(p)).FaiColumn = pExtra pExtra += uintptr(uint32(2) * uint32(nCol)) (*TIndex)(unsafe.Pointer(p)).FaSortOrder = pExtra (*TIndex)(unsafe.Pointer(p)).FnColumn = uint16(nCol) (*TIndex)(unsafe.Pointer(p)).FnKeyCol = uint16(int32(nCol) - int32(1)) *(*uintptr)(unsafe.Pointer(ppExtra)) = p + uintptr(nByte) } return p } // C documentation // // /* // ** If expression list pList contains an expression that was parsed with // ** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in // ** pParse and return non-zero. Otherwise, return zero. // */ func _sqlite3HasExplicitNulls(tls *libc.TLS, pParse uintptr, pList uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i int32 var sf Tu8 var v2 uintptr _, _, _ = i, sf, v2 if pList != 0 { i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } if int32(uint32(*(*uint16)(unsafe.Pointer(pList + 8 + uintptr(i)*20 + 8 + 4))&0x20>>5)) != 0 { sf = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).Ffg.FsortFlags if int32(sf) == 0 || int32(sf) == int32(3) { v2 = __ccgo_ts + 14150 } else { v2 = __ccgo_ts + 14156 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14161, libc.VaList(bp+8, v2)) return int32(1) } goto _1 _1: ; i++ } } return 0 } // C documentation // // /* // ** Create a new index for an SQL table. pName1.pName2 is the name of the index // ** and pTblList is the name of the table that is to be indexed. Both will // ** be NULL for a primary key or an index that is created to satisfy a // ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable // ** as the table to be indexed. pParse->pNewTable is a table that is // ** currently being constructed by a CREATE TABLE statement. // ** // ** pList is a list of columns to be indexed. pList will be NULL if this // ** is a primary key or unique-constraint on the most recent column added // ** to the table currently under construction. // */ func _sqlite3CreateIndex(tls *libc.TLS, pParse uintptr, pName1 uintptr, pName2 uintptr, pTblName uintptr, pList uintptr, onError int32, pStart uintptr, pPIWhere uintptr, sortOrder int32, ifNotExist int32, idxType Tu8) { bp := tls.Alloc(128) defer tls.Free(128) var db, p, pCExpr, pCol, pDb, pExpr, pIdx, pIndex, pListItem, pLoop, pNext, pPk, pTab, pThis, ppFrom, v, z1, z2, zColl, zDb, zName, zStmt, v12, v13, v15, v16, v2, p3 uintptr var i, iDb, iMem, j, k, n, n1, nColl, nExtra, nExtraCol, nName, requestedSortOrder, sortOrderMask, x, v11, v5 int32 var _ /* pName at bp+52 */ uintptr var _ /* prevCol at bp+64 */ TToken var _ /* sFix at bp+0 */ TDbFixer var _ /* zExtra at bp+56 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, i, iDb, iMem, j, k, n, n1, nColl, nExtra, nExtraCol, nName, p, pCExpr, pCol, pDb, pExpr, pIdx, pIndex, pListItem, pLoop, pNext, pPk, pTab, pThis, ppFrom, requestedSortOrder, sortOrderMask, v, x, z1, z2, zColl, zDb, zName, zStmt, v11, v12, v13, v15, v16, v2, v5, p3 pTab = uintptr(0) /* Table to be indexed */ pIndex = uintptr(0) /* The index to be created */ zName = uintptr(0) /* 1 to honor DESC in index. 0 to ignore. */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Index of the database that is being written */ *(*uintptr)(unsafe.Pointer(bp + 52)) = uintptr(0) /* For looping over pList */ nExtra = 0 /* Number of extra columns needed */ *(*uintptr)(unsafe.Pointer(bp + 56)) = uintptr(0) /* Extra space after the Index object */ pPk = uintptr(0) /* PRIMARY KEY index for WITHOUT ROWID tables */ _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto exit_create_index } _ = libc.Int32FromInt32(0) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) == int32(PARSE_MODE_DECLARE_VTAB) && int32(idxType) != int32(SQLITE_IDXTYPE_PRIMARYKEY) { goto exit_create_index } if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) { goto exit_create_index } if _sqlite3HasExplicitNulls(tls, pParse, pList) != 0 { goto exit_create_index } /* ** Find the table that is to be indexed. Return early if not found. */ if pTblName != uintptr(0) { /* Use the two-part index name to determine the database ** to search for the table. 'Fix' the table name to this db ** before looking up the table. */ _ = libc.Int32FromInt32(0) iDb = _sqlite3TwoPartName(tls, pParse, pName1, pName2, bp+52) if iDb < 0 { goto exit_create_index } _ = libc.Int32FromInt32(0) /* If the index name was unqualified, check if the table ** is a temp table. If so, set the database to 1. Do not do this ** if initializing a database schema. */ if !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) { pTab = _sqlite3SrcListLookup(tls, pParse, pTblName) if (*TToken)(unsafe.Pointer(pName2)).Fn == uint32(0) && pTab != 0 && (*TTable)(unsafe.Pointer(pTab)).FpSchema == (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema { iDb = int32(1) } } _sqlite3FixInit(tls, bp, pParse, iDb, __ccgo_ts+14189, *(*uintptr)(unsafe.Pointer(bp + 52))) if _sqlite3FixSrcList(tls, bp, pTblName) != 0 { /* Because the parser constructs pTblName from a single identifier, ** sqlite3FixSrcList can never fail. */ _ = libc.Int32FromInt32(0) } pTab = _sqlite3LocateTableItem(tls, pParse, uint32(0), pTblName+8) _ = libc.Int32FromInt32(0) if pTab == uintptr(0) { goto exit_create_index } if iDb == int32(1) && (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema != (*TTable)(unsafe.Pointer(pTab)).FpSchema { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14195, libc.VaList(bp+80, (*TTable)(unsafe.Pointer(pTab)).FzName)) goto exit_create_index } if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { pPk = _sqlite3PrimaryKeyIndex(tls, pTab) } } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pTab = (*TParse)(unsafe.Pointer(pParse)).FpNewTable if !(pTab != 0) { goto exit_create_index } iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) } pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16 _ = libc.Int32FromInt32(0) if Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName, __ccgo_ts+6525, int32(7)) == 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) == 0 && pTblName != uintptr(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14245, libc.VaList(bp+80, (*TTable)(unsafe.Pointer(pTab)).FzName)) goto exit_create_index } if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14273, 0) goto exit_create_index } if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14298, 0) goto exit_create_index } /* ** Find the name of the index. Make sure there is not already another ** index or table with the same name. ** ** Exception: If we are reading the names of permanent indices from the ** sqlite_schema table (because some other process changed the schema) and ** one of the index names collides with the name of a temporary table or ** index, then we will continue to process this index. ** ** If pName==0 it means that we are ** dealing with a primary key or UNIQUE constraint. We have to invent our ** own name. */ if *(*uintptr)(unsafe.Pointer(bp + 52)) != 0 { zName = _sqlite3NameFromToken(tls, db, *(*uintptr)(unsafe.Pointer(bp + 52))) if zName == uintptr(0) { goto exit_create_index } _ = libc.Int32FromInt32(0) if SQLITE_OK != _sqlite3CheckObjectName(tls, pParse, zName, __ccgo_ts+14189, (*TTable)(unsafe.Pointer(pTab)).FzName) { goto exit_create_index } if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) { if !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) { if _sqlite3FindTable(tls, db, zName, (*TDb)(unsafe.Pointer(pDb)).FzDbSName) != uintptr(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14332, libc.VaList(bp+80, zName)) goto exit_create_index } } if _sqlite3FindIndex(tls, db, zName, (*TDb)(unsafe.Pointer(pDb)).FzDbSName) != uintptr(0) { if !(ifNotExist != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14366, libc.VaList(bp+80, zName)) } else { _ = libc.Int32FromInt32(0) _sqlite3CodeVerifySchema(tls, pParse, iDb) _sqlite3ForceNotReadOnly(tls, pParse) } goto exit_create_index } } } else { pLoop = (*TTable)(unsafe.Pointer(pTab)).FpIndex n = libc.Int32FromInt32(1) for { if !(pLoop != 0) { break } goto _1 _1: ; pLoop = (*TIndex)(unsafe.Pointer(pLoop)).FpNext n++ } zName = _sqlite3MPrintf(tls, db, __ccgo_ts+14390, libc.VaList(bp+80, (*TTable)(unsafe.Pointer(pTab)).FzName, n)) if zName == uintptr(0) { goto exit_create_index } /* Automatic index names generated from within sqlite3_declare_vtab() ** must have names that are distinct from normal automatic index names. ** The following statement converts "sqlite3_autoindex..." into ** "sqlite3_butoindex..." in order to make the names distinct. ** The "vtab_err.test" test demonstrates the need of this statement. */ if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != PARSE_MODE_NORMAL { *(*uint8)(unsafe.Pointer(zName + 7))++ } } /* Check for authorization to create an index. */ if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) { zDb = (*TDb)(unsafe.Pointer(pDb)).FzDbSName if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) { v2 = __ccgo_ts + 6533 } else { v2 = __ccgo_ts + 6066 } if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_INSERT), v2, uintptr(0), zDb) != 0 { goto exit_create_index } i = int32(SQLITE_CREATE_INDEX) if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) { i = int32(SQLITE_CREATE_TEMP_INDEX) } if _sqlite3AuthCheck(tls, pParse, i, zName, (*TTable)(unsafe.Pointer(pTab)).FzName, zDb) != 0 { goto exit_create_index } } /* If pList==0, it means this routine was called to make a primary ** key out of the last column added to the table under construction. ** So create a fake list to simulate this. */ if pList == uintptr(0) { pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(int32((*TTable)(unsafe.Pointer(pTab)).FnCol)-int32(1))*12 p3 = pCol + 10 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) | libc.Int32FromInt32(COLFLAG_UNIQUE)) _sqlite3TokenInit(tls, bp+64, (*TColumn)(unsafe.Pointer(pCol)).FzCnName) pList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+64, 0)) if pList == uintptr(0) { goto exit_create_index } _ = libc.Int32FromInt32(0) _sqlite3ExprListSetSortOrder(tls, pList, sortOrder, -int32(1)) } else { _sqlite3ExprListCheckLength(tls, pParse, pList, __ccgo_ts+14189) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto exit_create_index } } /* Figure out how many bytes of space are required to store explicitly ** specified collation sequence names. */ i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } pExpr = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLLATE) { _ = libc.Int32FromInt32(0) nExtra += int32(1) + _sqlite3Strlen30(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8))) } goto _4 _4: ; i++ } /* ** Allocate the index structure. */ nName = _sqlite3Strlen30(tls, zName) if pPk != 0 { v5 = int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol) } else { v5 = int32(1) } nExtraCol = v5 _ = libc.Int32FromInt32(0) pIndex = _sqlite3AllocateIndexObject(tls, db, int16((*TExprList)(unsafe.Pointer(pList)).FnExpr+nExtraCol), nName+nExtra+int32(1), bp+56) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto exit_create_index } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TIndex)(unsafe.Pointer(pIndex)).FzName = *(*uintptr)(unsafe.Pointer(bp + 56)) *(*uintptr)(unsafe.Pointer(bp + 56)) += uintptr(nName + int32(1)) libc.Xmemcpy(tls, (*TIndex)(unsafe.Pointer(pIndex)).FzName, zName, uint32(nName+int32(1))) (*TIndex)(unsafe.Pointer(pIndex)).FpTable = pTab (*TIndex)(unsafe.Pointer(pIndex)).FonError = uint8(onError) libc.SetBitFieldPtr16Uint32(pIndex+56, libc.BoolUint32(onError != libc.Int32FromInt32(OE_None)), 3, 0x8) libc.SetBitFieldPtr16Uint32(pIndex+56, uint32(idxType), 0, 0x3) (*TIndex)(unsafe.Pointer(pIndex)).FpSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema (*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol = uint16((*TExprList)(unsafe.Pointer(pList)).FnExpr) if pPIWhere != 0 { _sqlite3ResolveSelfReference(tls, pParse, pTab, int32(NC_PartIdx), pPIWhere, uintptr(0)) (*TIndex)(unsafe.Pointer(pIndex)).FpPartIdxWhere = pPIWhere pPIWhere = uintptr(0) } _ = libc.Int32FromInt32(0) /* Check to see if we should honor DESC requests on index columns */ if int32((*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Ffile_format) >= int32(4) { sortOrderMask = -int32(1) /* Honor DESC */ } else { sortOrderMask = 0 /* Ignore DESC */ } /* Analyze the list of expressions that form the terms of the index and ** report any errors. In the common case where the expression is exactly ** a table column, store that column in aiColumn[]. For general expressions, ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[]. ** ** TODO: Issue a warning if two or more columns of the index are identical. ** TODO: Issue a warning if the table primary key is used as part of the ** index key. */ pListItem = pList + 8 if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { (*TIndex)(unsafe.Pointer(pIndex)).FaColExpr = pList pList = uintptr(0) } i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol)) { break } /* Collation sequence name */ _sqlite3StringToId(tls, (*TExprList_item)(unsafe.Pointer(pListItem)).FpExpr) _sqlite3ResolveSelfReference(tls, pParse, pTab, int32(NC_IdxExpr), (*TExprList_item)(unsafe.Pointer(pListItem)).FpExpr, uintptr(0)) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto exit_create_index } pCExpr = _sqlite3ExprSkipCollate(tls, (*TExprList_item)(unsafe.Pointer(pListItem)).FpExpr) if int32((*TExpr)(unsafe.Pointer(pCExpr)).Fop) != int32(TK_COLUMN) { if pTab == (*TParse)(unsafe.Pointer(pParse)).FpNewTable { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14413, 0) goto exit_create_index } if (*TIndex)(unsafe.Pointer(pIndex)).FaColExpr == uintptr(0) { (*TIndex)(unsafe.Pointer(pIndex)).FaColExpr = pList pList = uintptr(0) } j = -int32(2) *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(i)*2)) = int16(-libc.Int32FromInt32(2)) libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(0), 3, 0x8) libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 12, 0x1000) } else { j = int32((*TExpr)(unsafe.Pointer(pCExpr)).FiColumn) _ = libc.Int32FromInt32(0) if j < 0 { j = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) } else { if int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12 + 4))&0xf>>0)) == 0 { libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(0), 3, 0x8) } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 { libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 11, 0x800) libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 12, 0x1000) } } *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(i)*2)) = int16(j) } zColl = uintptr(0) if int32((*TExpr)(unsafe.Pointer((*TExprList_item)(unsafe.Pointer(pListItem)).FpExpr)).Fop) == int32(TK_COLLATE) { _ = libc.Int32FromInt32(0) zColl = *(*uintptr)(unsafe.Pointer((*TExprList_item)(unsafe.Pointer(pListItem)).FpExpr + 8)) nColl = _sqlite3Strlen30(tls, zColl) + int32(1) _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, *(*uintptr)(unsafe.Pointer(bp + 56)), zColl, uint32(nColl)) zColl = *(*uintptr)(unsafe.Pointer(bp + 56)) *(*uintptr)(unsafe.Pointer(bp + 56)) += uintptr(nColl) nExtra -= nColl } else { if j >= 0 { zColl = _sqlite3ColumnColl(tls, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(j)*12) } } if !(zColl != 0) { zColl = uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) } if !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) && !(_sqlite3LocateCollSeq(tls, pParse, zColl) != 0) { goto exit_create_index } *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FazColl + uintptr(i)*4)) = zColl requestedSortOrder = int32((*TExprList_item)(unsafe.Pointer(pListItem)).Ffg.FsortFlags) & sortOrderMask *(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FaSortOrder + uintptr(i))) = uint8(requestedSortOrder) goto _6 _6: ; i++ pListItem += 20 } /* Append the table key to the end of the index. For WITHOUT ROWID ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For ** normal tables (when pPk==0) this will be the rowid. */ if pPk != 0 { j = 0 for { if !(j < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) { break } x = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(j)*2))) _ = libc.Int32FromInt32(0) if _isDupColumn(tls, pIndex, int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol), pPk, j) != 0 { (*TIndex)(unsafe.Pointer(pIndex)).FnColumn-- } else { *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(i)*2)) = int16(x) *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FazColl + uintptr(i)*4)) = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(j)*4)) *(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FaSortOrder + uintptr(i))) = *(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaSortOrder + uintptr(j))) i++ } goto _7 _7: ; j++ } _ = libc.Int32FromInt32(0) } else { *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(i)*2)) = int16(-libc.Int32FromInt32(1)) *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FazColl + uintptr(i)*4)) = uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) } _sqlite3DefaultRowEst(tls, pIndex) if (*TParse)(unsafe.Pointer(pParse)).FpNewTable == uintptr(0) { _estimateIndexWidth(tls, pIndex) } /* If this index contains every column of its table, then mark ** it as a covering index */ _ = libc.Int32FromInt32(0) _recomputeColumnsNotIndexed(tls, pIndex) if pTblName != uintptr(0) && int32((*TIndex)(unsafe.Pointer(pIndex)).FnColumn) >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 5, 0x20) j = 0 for { if !(j < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if j == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { goto _8 } if int32(_sqlite3TableColumnToIndex(tls, pIndex, int16(j))) >= 0 { goto _8 } libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(0), 5, 0x20) break goto _8 _8: ; j++ } } if pTab == (*TParse)(unsafe.Pointer(pParse)).FpNewTable { pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0) { break } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) != int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol) { goto _9 } k = 0 for { if !(k < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) { break } _ = libc.Int32FromInt32(0) if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(k)*2))) != int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(k)*2))) { break } z1 = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(k)*4)) z2 = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FazColl + uintptr(k)*4)) if _sqlite3StrICmp(tls, z1, z2) != 0 { break } goto _10 _10: ; k++ } if k == int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) { if int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != int32((*TIndex)(unsafe.Pointer(pIndex)).FonError) { /* This constraint creates the same index as a previous ** constraint specified somewhere in the CREATE TABLE statement. ** However the ON CONFLICT clauses are different. If both this ** constraint and the previous equivalent constraint have explicit ** ON CONFLICT clauses this is an error. Otherwise, use the ** explicitly specified behavior for the index. */ if !(int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) == int32(OE_Default) || int32((*TIndex)(unsafe.Pointer(pIndex)).FonError) == int32(OE_Default)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14474, libc.VaList(bp+80, 0)) } if int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) == int32(OE_Default) { (*TIndex)(unsafe.Pointer(pIdx)).FonError = (*TIndex)(unsafe.Pointer(pIndex)).FonError } } if int32(idxType) == int32(SQLITE_IDXTYPE_PRIMARYKEY) { libc.SetBitFieldPtr16Uint32(pIdx+56, uint32(idxType), 0, 0x3) } if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { (*TIndex)(unsafe.Pointer(pIndex)).FpNext = (*TParse)(unsafe.Pointer(pParse)).FpNewIndex (*TParse)(unsafe.Pointer(pParse)).FpNewIndex = pIndex pIndex = uintptr(0) } goto exit_create_index } goto _9 _9: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } } if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) { /* Link the new Index structure to its table and to the other ** in-memory database structures. */ _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pTblName != uintptr(0) { (*TIndex)(unsafe.Pointer(pIndex)).Ftnum = (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FnewTnum if _sqlite3IndexHasDuplicateRootPage(tls, pIndex) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14516, 0) (*TParse)(unsafe.Pointer(pParse)).Frc = _sqlite3CorruptError(tls, int32(125123)) goto exit_create_index } } p = _sqlite3HashInsert(tls, (*TIndex)(unsafe.Pointer(pIndex)).FpSchema+24, (*TIndex)(unsafe.Pointer(pIndex)).FzName, pIndex) if p != 0 { _ = libc.Int32FromInt32(0) /* Malloc must have failed */ _sqlite3OomFault(tls, db) goto exit_create_index } *(*Tu32)(unsafe.Pointer(db + 24)) |= uint32(DBFLAG_SchemaChange) } else { if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) || pTblName != uintptr(0) { v12 = pParse + 44 *(*int32)(unsafe.Pointer(v12))++ v11 = *(*int32)(unsafe.Pointer(v12)) iMem = v11 v = _sqlite3GetVdbe(tls, pParse) if v == uintptr(0) { goto exit_create_index } _sqlite3BeginWriteOperation(tls, pParse, int32(1), iDb) /* Create the rootpage for the index using CreateIndex. But before ** doing so, code a Noop instruction and store its address in ** Index.tnum. This is required in case this index is actually a ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In ** that case the convertToWithoutRowidTable() routine will replace ** the Noop with a Goto to jump over the VDBE code generated below. */ (*TIndex)(unsafe.Pointer(pIndex)).Ftnum = uint32(_sqlite3VdbeAddOp0(tls, v, int32(OP_Noop))) _sqlite3VdbeAddOp3(tls, v, int32(OP_CreateBtree), iDb, iMem, int32(BTREE_BLOBKEY)) /* Gather the complete text of the CREATE INDEX statement into ** the zStmt variable */ _ = libc.Int32FromInt32(0) if pStart != 0 { n1 = int32(uint32(int32((*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fz)-int32((*TToken)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 52)))).Fz)) + (*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fn) if int32(*(*uint8)(unsafe.Pointer((*TToken)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 52)))).Fz + uintptr(n1-int32(1))))) == int32(';') { n1-- } /* A named index with an explicit CREATE INDEX statement */ if onError == OE_None { v13 = __ccgo_ts + 1648 } else { v13 = __ccgo_ts + 14533 } zStmt = _sqlite3MPrintf(tls, db, __ccgo_ts+14541, libc.VaList(bp+80, v13, n1, (*TToken)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 52)))).Fz)) } else { /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ /* zStmt = sqlite3MPrintf(""); */ zStmt = uintptr(0) } /* Add an entry in sqlite_schema for this index */ _sqlite3NestedParse(tls, pParse, __ccgo_ts+14561, libc.VaList(bp+80, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, (*TIndex)(unsafe.Pointer(pIndex)).FzName, (*TTable)(unsafe.Pointer(pTab)).FzName, iMem, zStmt)) _sqlite3DbFree(tls, db, zStmt) /* Fill the index with data and reparse the schema. Code an OP_Expire ** to invalidate all pre-compiled statements. */ if pTblName != 0 { _sqlite3RefillIndex(tls, pParse, pIndex, iMem) _sqlite3ChangeCookie(tls, pParse, iDb) _sqlite3VdbeAddParseSchemaOp(tls, v, iDb, _sqlite3MPrintf(tls, db, __ccgo_ts+14620, libc.VaList(bp+80, (*TIndex)(unsafe.Pointer(pIndex)).FzName)), uint16(0)) _sqlite3VdbeAddOp2(tls, v, int32(OP_Expire), 0, int32(1)) } _sqlite3VdbeJumpHere(tls, v, int32((*TIndex)(unsafe.Pointer(pIndex)).Ftnum)) } } } if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 || pTblName == uintptr(0) { (*TIndex)(unsafe.Pointer(pIndex)).FpNext = (*TTable)(unsafe.Pointer(pTab)).FpIndex (*TTable)(unsafe.Pointer(pTab)).FpIndex = pIndex pIndex = uintptr(0) } else { if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).FpNewIndex = pIndex pIndex = uintptr(0) } } /* Clean up before exiting */ goto exit_create_index exit_create_index: ; if pIndex != 0 { _sqlite3FreeIndex(tls, db, pIndex) } if pTab != 0 { ppFrom = pTab + 8 for { v15 = *(*uintptr)(unsafe.Pointer(ppFrom)) pThis = v15 if !(v15 != uintptr(0)) { break } if int32((*TIndex)(unsafe.Pointer(pThis)).FonError) != int32(OE_Replace) { goto _14 } for { v16 = (*TIndex)(unsafe.Pointer(pThis)).FpNext pNext = v16 if !(v16 != uintptr(0) && int32((*TIndex)(unsafe.Pointer(pNext)).FonError) != int32(OE_Replace)) { break } *(*uintptr)(unsafe.Pointer(ppFrom)) = pNext (*TIndex)(unsafe.Pointer(pThis)).FpNext = (*TIndex)(unsafe.Pointer(pNext)).FpNext (*TIndex)(unsafe.Pointer(pNext)).FpNext = pThis ppFrom = pNext + 20 } break goto _14 _14: ; ppFrom = pThis + 20 } } _sqlite3ExprDelete(tls, db, pPIWhere) _sqlite3ExprListDelete(tls, db, pList) _sqlite3SrcListDelete(tls, db, pTblName) _sqlite3DbFree(tls, db, zName) } // C documentation // // /* // ** Fill the Index.aiRowEst[] array with default information - information // ** to be used when we have not run the ANALYZE command. // ** // ** aiRowEst[0] is supposed to contain the number of elements in the index. // ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the // ** number of rows in the table that match any particular value of the // ** first column of the index. aiRowEst[2] is an estimate of the number // ** of rows that match any particular combination of the first 2 columns // ** of the index. And so forth. It must always be the case that // * // ** aiRowEst[N]<=aiRowEst[N-1] // ** aiRowEst[N]>=1 // ** // ** Apart from that, we have little to go on besides intuition as to // ** how aiRowEst[] should be initialized. The numbers generated here // ** are based on typical values found in actual indices. // */ func _sqlite3DefaultRowEst(tls *libc.TLS, pIdx uintptr) { var a uintptr var i, nCopy, v1 int32 var x, v2 TLogEst _, _, _, _, _, _ = a, i, nCopy, x, v1, v2 a = (*TIndex)(unsafe.Pointer(pIdx)).FaiRowLogEst if int32(libc.Uint32FromInt64(10)/libc.Uint32FromInt64(2)) < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) { v1 = int32(libc.Uint32FromInt64(10) / libc.Uint32FromInt64(2)) } else { v1 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) } nCopy = v1 /* Indexes with default row estimates should not have stat1 data */ _ = libc.Int32FromInt32(0) /* Set the first entry (number of rows in the index) to the estimated ** number of rows in the table, or half the number of rows in the table ** for a partial index. ** ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1 ** table but other parts we are having to guess at, then do not let the ** estimated number of rows in the table be less than 1000 (LogEst 99). ** Failure to do this can cause the indexes for which we do not have ** stat1 data to be ignored by the query planner. */ x = (*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FnRowLogEst _ = libc.Int32FromInt32(0) if int32(x) < int32(99) { v2 = libc.Int16FromInt32(99) x = v2 (*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FnRowLogEst = v2 } if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != uintptr(0) { x = TLogEst(int32(x) - libc.Int32FromInt32(10)) _ = libc.Int32FromInt32(0) } *(*TLogEst)(unsafe.Pointer(a)) = x /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is ** 6 and each subsequent value (if any) is 5. */ libc.Xmemcpy(tls, a+1*2, uintptr(unsafe.Pointer(&_aVal)), uint32(nCopy)*uint32(2)) i = nCopy + int32(1) for { if !(i <= int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) { break } *(*TLogEst)(unsafe.Pointer(a + uintptr(i)*2)) = int16(23) _ = libc.Int32FromInt32(0) goto _3 _3: ; i++ } _ = libc.Int32FromInt32(0) if int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != OE_None { *(*TLogEst)(unsafe.Pointer(a + uintptr((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)*2)) = 0 } } /* 10, 9, 8, 7, 6 */ var _aVal = [5]TLogEst{ 0: int16(33), 1: int16(32), 2: int16(30), 3: int16(28), 4: int16(26), } // C documentation // // /* // ** This routine will drop an existing named index. This routine // ** implements the DROP INDEX statement. // */ func _sqlite3DropIndex(tls *libc.TLS, pParse uintptr, pName uintptr, ifExists int32) { bp := tls.Alloc(32) defer tls.Free(32) var code, iDb int32 var db, pIndex, pTab, v, zDb, zTab, v1 uintptr _, _, _, _, _, _, _, _, _ = code, db, iDb, pIndex, pTab, v, zDb, zTab, v1 db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto exit_drop_index } _ = libc.Int32FromInt32(0) /* Never called with prior non-OOM errors */ _ = libc.Int32FromInt32(0) if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) { goto exit_drop_index } pIndex = _sqlite3FindIndex(tls, db, (*(*TSrcItem)(unsafe.Pointer(pName + 8))).FzName, (*(*TSrcItem)(unsafe.Pointer(pName + 8))).FzDatabase) if pIndex == uintptr(0) { if !(ifExists != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14647, libc.VaList(bp+8, pName+8)) } else { _sqlite3CodeVerifyNamedSchema(tls, pParse, (*(*TSrcItem)(unsafe.Pointer(pName + 8))).FzDatabase) _sqlite3ForceNotReadOnly(tls, pParse) } (*TParse)(unsafe.Pointer(pParse)).FcheckSchema = uint8(1) goto exit_drop_index } if int32(uint32(*(*uint16)(unsafe.Pointer(pIndex + 56))&0x3>>0)) != SQLITE_IDXTYPE_APPDEF { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14665, libc.VaList(bp+8, 0)) goto exit_drop_index } iDb = _sqlite3SchemaToIndex(tls, db, (*TIndex)(unsafe.Pointer(pIndex)).FpSchema) code = int32(SQLITE_DROP_INDEX) pTab = (*TIndex)(unsafe.Pointer(pIndex)).FpTable zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) { v1 = __ccgo_ts + 6533 } else { v1 = __ccgo_ts + 6066 } zTab = v1 if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_DELETE), zTab, uintptr(0), zDb) != 0 { goto exit_drop_index } if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) { code = int32(SQLITE_DROP_TEMP_INDEX) } if _sqlite3AuthCheck(tls, pParse, code, (*TIndex)(unsafe.Pointer(pIndex)).FzName, (*TTable)(unsafe.Pointer(pTab)).FzName, zDb) != 0 { goto exit_drop_index } /* Generate code to remove the index and from the schema table */ v = _sqlite3GetVdbe(tls, pParse) if v != 0 { _sqlite3BeginWriteOperation(tls, pParse, int32(1), iDb) _sqlite3NestedParse(tls, pParse, __ccgo_ts+14738, libc.VaList(bp+8, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, (*TIndex)(unsafe.Pointer(pIndex)).FzName)) _sqlite3ClearStatTables(tls, pParse, iDb, __ccgo_ts+11793, (*TIndex)(unsafe.Pointer(pIndex)).FzName) _sqlite3ChangeCookie(tls, pParse, iDb) _destroyRootPage(tls, pParse, int32((*TIndex)(unsafe.Pointer(pIndex)).Ftnum), iDb) _sqlite3VdbeAddOp4(tls, v, int32(OP_DropIndex), iDb, 0, 0, (*TIndex)(unsafe.Pointer(pIndex)).FzName, 0) } goto exit_drop_index exit_drop_index: ; _sqlite3SrcListDelete(tls, db, pName) } // C documentation // // /* // ** pArray is a pointer to an array of objects. Each object in the // ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc() // ** to extend the array so that there is space for a new object at the end. // ** // ** When this function is called, *pnEntry contains the current size of // ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes // ** in total). // ** // ** If the realloc() is successful (i.e. if no OOM condition occurs), the // ** space allocated for the new object is zeroed, *pnEntry updated to // ** reflect the new size of the array and a pointer to the new allocation // ** returned. *pIdx is set to the index of the new array entry in this case. // ** // ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains // ** unchanged and a copy of pArray returned. // */ func _sqlite3ArrayAllocate(tls *libc.TLS, db uintptr, pArray uintptr, szEntry int32, pnEntry uintptr, pIdx uintptr) (r uintptr) { var n, sz Tsqlite3_int64 var pNew, z uintptr var v1 int32 var v2 int64 _, _, _, _, _, _ = n, pNew, sz, z, v1, v2 v1 = *(*int32)(unsafe.Pointer(pnEntry)) *(*int32)(unsafe.Pointer(pIdx)) = v1 n = int64(v1) if n&(n-int64(1)) == 0 { if n == 0 { v2 = int64(1) } else { v2 = int64(2) * n } sz = v2 pNew = _sqlite3DbRealloc(tls, db, pArray, uint64(sz*int64(szEntry))) if pNew == uintptr(0) { *(*int32)(unsafe.Pointer(pIdx)) = -int32(1) return pArray } pArray = pNew } z = pArray libc.Xmemset(tls, z+uintptr(n*int64(szEntry)), 0, uint32(szEntry)) *(*int32)(unsafe.Pointer(pnEntry))++ return pArray } // C documentation // // /* // ** Append a new element to the given IdList. Create a new IdList if // ** need be. // ** // ** A new IdList is returned, or NULL if malloc() fails. // */ func _sqlite3IdListAppend(tls *libc.TLS, pParse uintptr, pList uintptr, pToken uintptr) (r uintptr) { var db, pNew, v2 uintptr var i, v1 int32 _, _, _, _, _ = db, i, pNew, v1, v2 db = (*TParse)(unsafe.Pointer(pParse)).Fdb if pList == uintptr(0) { pList = _sqlite3DbMallocZero(tls, db, uint64(16)) if pList == uintptr(0) { return uintptr(0) } } else { pNew = _sqlite3DbRealloc(tls, db, pList, uint64(uint32(16)+uint32((*TIdList)(unsafe.Pointer(pList)).FnId)*uint32(8))) if pNew == uintptr(0) { _sqlite3IdListDelete(tls, db, pList) return uintptr(0) } pList = pNew } v2 = pList v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ i = v1 (*(*TIdList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*8))).FzName = _sqlite3NameFromToken(tls, db, pToken) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && (*(*TIdList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*8))).FzName != 0 { _sqlite3RenameTokenMap(tls, pParse, (*(*TIdList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*8))).FzName, pToken) } return pList } // C documentation // // /* // ** Delete an IdList. // */ func _sqlite3IdListDelete(tls *libc.TLS, db uintptr, pList uintptr) { var i int32 _ = i _ = libc.Int32FromInt32(0) if pList == uintptr(0) { return } _ = libc.Int32FromInt32(0) /* EU4_EXPR mode is not currently used */ i = 0 for { if !(i < (*TIdList)(unsafe.Pointer(pList)).FnId) { break } _sqlite3DbFree(tls, db, (*(*TIdList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*8))).FzName) goto _1 _1: ; i++ } _sqlite3DbNNFreeNN(tls, db, pList) } // C documentation // // /* // ** Return the index in pList of the identifier named zId. Return -1 // ** if not found. // */ func _sqlite3IdListIndex(tls *libc.TLS, pList uintptr, zName uintptr) (r int32) { var i int32 _ = i _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*TIdList)(unsafe.Pointer(pList)).FnId) { break } if _sqlite3StrICmp(tls, (*(*TIdList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*8))).FzName, zName) == 0 { return i } goto _1 _1: ; i++ } return -int32(1) } /* ** Maximum size of a SrcList object. ** The SrcList object is used to represent the FROM clause of a ** SELECT statement, and the query planner cannot deal with more ** than 64 tables in a join. So any value larger than 64 here ** is sufficient for most uses. Smaller values, like say 10, are ** appropriate for small and memory-limited applications. */ // C documentation // // /* // ** Expand the space allocated for the given SrcList object by // ** creating nExtra new slots beginning at iStart. iStart is zero based. // ** New slots are zeroed. // ** // ** For example, suppose a SrcList initially contains two entries: A,B. // ** To append 3 new entries onto the end, do this: // ** // ** sqlite3SrcListEnlarge(db, pSrclist, 3, 2); // ** // ** After the call above it would contain: A, B, nil, nil, nil. // ** If the iStart argument had been 1 instead of 2, then the result // ** would have been: A, nil, nil, nil, B. To prepend the new slots, // ** the iStart value would be 0. The result then would // ** be: nil, nil, nil, A, B. // ** // ** If a memory allocation fails or the SrcList becomes too large, leave // ** the original SrcList unchanged, return NULL, and leave an error message // ** in pParse. // */ func _sqlite3SrcListEnlarge(tls *libc.TLS, pParse uintptr, pSrc uintptr, nExtra int32, iStart int32) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, pNew uintptr var i int32 var nAlloc Tsqlite3_int64 _, _, _, _ = db, i, nAlloc, pNew /* Sanity checking on calling parameters */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Allocate additional space if needed */ if uint32((*TSrcList)(unsafe.Pointer(pSrc)).FnSrc)+uint32(nExtra) > (*TSrcList)(unsafe.Pointer(pSrc)).FnAlloc { nAlloc = int64(2)*int64((*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) + int64(nExtra) db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc+nExtra >= int32(SQLITE_MAX_SRCLIST) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14798, libc.VaList(bp+8, int32(SQLITE_MAX_SRCLIST))) return uintptr(0) } if nAlloc > int64(SQLITE_MAX_SRCLIST) { nAlloc = int64(SQLITE_MAX_SRCLIST) } pNew = _sqlite3DbRealloc(tls, db, pSrc, uint64(int64(80)+(nAlloc-int64(1))*int64(72))) if pNew == uintptr(0) { _ = libc.Int32FromInt32(0) return uintptr(0) } pSrc = pNew (*TSrcList)(unsafe.Pointer(pSrc)).FnAlloc = uint32(nAlloc) } /* Move existing slots that come after the newly inserted slots ** out of the way */ i = (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc - int32(1) for { if !(i >= iStart) { break } *(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i+nExtra)*72)) = *(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72)) goto _1 _1: ; i-- } *(*int32)(unsafe.Pointer(pSrc)) += nExtra /* Zero the newly allocated slots */ libc.Xmemset(tls, pSrc+8+uintptr(iStart)*72, 0, uint32(72)*uint32(nExtra)) i = iStart for { if !(i < iStart+nExtra) { break } (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72))).FiCursor = -int32(1) goto _2 _2: ; i++ } /* Return a pointer to the enlarged SrcList */ return pSrc } // C documentation // // /* // ** Append a new table name to the given SrcList. Create a new SrcList if // ** need be. A new entry is created in the SrcList even if pTable is NULL. // ** // ** A SrcList is returned, or NULL if there is an OOM error or if the // ** SrcList grows to large. The returned // ** SrcList might be the same as the SrcList that was input or it might be // ** a new one. If an OOM error does occurs, then the prior value of pList // ** that is input to this routine is automatically freed. // ** // ** If pDatabase is not null, it means that the table has an optional // ** database name prefix. Like this: "database.table". The pDatabase // ** points to the table name and the pTable points to the database name. // ** The SrcList.a[].zName field is filled with the table name which might // ** come from pTable (if pDatabase is NULL) or from pDatabase. // ** SrcList.a[].zDatabase is filled with the database name from pTable, // ** or with NULL if no database is specified. // ** // ** In other words, if call like this: // ** // ** sqlite3SrcListAppend(D,A,B,0); // ** // ** Then B is a table name and the database name is unspecified. If called // ** like this: // ** // ** sqlite3SrcListAppend(D,A,B,C); // ** // ** Then C is the table name and B is the database name. If C is defined // ** then so is B. In other words, we never have a case where: // ** // ** sqlite3SrcListAppend(D,A,0,C); // ** // ** Both pTable and pDatabase are assumed to be quoted. They are dequoted // ** before being added to the SrcList. // */ func _sqlite3SrcListAppend(tls *libc.TLS, pParse uintptr, pList uintptr, pTable uintptr, pDatabase uintptr) (r uintptr) { var db, pItem, pNew uintptr _, _, _ = db, pItem, pNew _ = libc.Int32FromInt32(0) /* Cannot have C without B */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) db = (*TParse)(unsafe.Pointer(pParse)).Fdb if pList == uintptr(0) { pList = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(80)) if pList == uintptr(0) { return uintptr(0) } (*TSrcList)(unsafe.Pointer(pList)).FnAlloc = uint32(1) (*TSrcList)(unsafe.Pointer(pList)).FnSrc = int32(1) libc.Xmemset(tls, pList+8, 0, uint32(72)) (*(*TSrcItem)(unsafe.Pointer(pList + 8))).FiCursor = -int32(1) } else { pNew = _sqlite3SrcListEnlarge(tls, pParse, pList, int32(1), (*TSrcList)(unsafe.Pointer(pList)).FnSrc) if pNew == uintptr(0) { _sqlite3SrcListDelete(tls, db, pList) return uintptr(0) } else { pList = pNew } } pItem = pList + 8 + uintptr((*TSrcList)(unsafe.Pointer(pList)).FnSrc-int32(1))*72 if pDatabase != 0 && (*TToken)(unsafe.Pointer(pDatabase)).Fz == uintptr(0) { pDatabase = uintptr(0) } if pDatabase != 0 { (*TSrcItem)(unsafe.Pointer(pItem)).FzName = _sqlite3NameFromToken(tls, db, pDatabase) (*TSrcItem)(unsafe.Pointer(pItem)).FzDatabase = _sqlite3NameFromToken(tls, db, pTable) } else { (*TSrcItem)(unsafe.Pointer(pItem)).FzName = _sqlite3NameFromToken(tls, db, pTable) (*TSrcItem)(unsafe.Pointer(pItem)).FzDatabase = uintptr(0) } return pList } // C documentation // // /* // ** Assign VdbeCursor index numbers to all tables in a SrcList // */ func _sqlite3SrcListAssignCursors(tls *libc.TLS, pParse uintptr, pList uintptr) { var i, v2 int32 var pItem, v3 uintptr _, _, _, _ = i, pItem, v2, v3 _ = libc.Int32FromInt32(0) if pList != 0 { i = 0 pItem = pList + 8 for { if !(i < (*TSrcList)(unsafe.Pointer(pList)).FnSrc) { break } if (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor >= 0 { goto _1 } v3 = pParse + 40 v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor = v2 if (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect != 0 { _sqlite3SrcListAssignCursors(tls, pParse, (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpSelect)).FpSrc) } goto _1 _1: ; i++ pItem += 72 } } } // C documentation // // /* // ** Delete an entire SrcList including all its substructure. // */ func _sqlite3SrcListDelete(tls *libc.TLS, db uintptr, pList uintptr) { var i int32 var pItem uintptr _, _ = i, pItem _ = libc.Int32FromInt32(0) if pList == uintptr(0) { return } pItem = pList + 8 i = libc.Int32FromInt32(0) for { if !(i < (*TSrcList)(unsafe.Pointer(pList)).FnSrc) { break } if (*TSrcItem)(unsafe.Pointer(pItem)).FzDatabase != 0 { _sqlite3DbNNFreeNN(tls, db, (*TSrcItem)(unsafe.Pointer(pItem)).FzDatabase) } if (*TSrcItem)(unsafe.Pointer(pItem)).FzName != 0 { _sqlite3DbNNFreeNN(tls, db, (*TSrcItem)(unsafe.Pointer(pItem)).FzName) } if (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias != 0 { _sqlite3DbNNFreeNN(tls, db, (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias) } if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x2>>1)) != 0 { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(pItem + 64))) } if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x4>>2)) != 0 { _sqlite3ExprListDelete(tls, db, *(*uintptr)(unsafe.Pointer(pItem + 64))) } _sqlite3DeleteTable(tls, db, (*TSrcItem)(unsafe.Pointer(pItem)).FpTab) if (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect != 0 { _sqlite3SelectDelete(tls, db, (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect) } if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x400>>10)) != 0 { _sqlite3IdListDelete(tls, db, *(*uintptr)(unsafe.Pointer(pItem + 48))) } else { if *(*uintptr)(unsafe.Pointer(pItem + 48)) != 0 { _sqlite3ExprDelete(tls, db, *(*uintptr)(unsafe.Pointer(pItem + 48))) } } goto _1 _1: ; i++ pItem += 72 } _sqlite3DbNNFreeNN(tls, db, pList) } // C documentation // // /* // ** This routine is called by the parser to add a new term to the // ** end of a growing FROM clause. The "p" parameter is the part of // ** the FROM clause that has already been constructed. "p" is NULL // ** if this is the first term of the FROM clause. pTable and pDatabase // ** are the name of the table and database named in the FROM clause term. // ** pDatabase is NULL if the database name qualifier is missing - the // ** usual case. If the term has an alias, then pAlias points to the // ** alias token. If the term is a subquery, then pSubquery is the // ** SELECT statement that the subquery encodes. The pTable and // ** pDatabase parameters are NULL for subqueries. The pOn and pUsing // ** parameters are the content of the ON and USING clauses. // ** // ** Return a new SrcList which encodes is the FROM with the new // ** term added. // */ func _sqlite3SrcListAppendFromTerm(tls *libc.TLS, pParse uintptr, p uintptr, pTable uintptr, pDatabase uintptr, pAlias uintptr, pSubquery uintptr, pOnUsing uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, pItem, pToken, v1, v2 uintptr _, _, _, _, _ = db, pItem, pToken, v1, v2 db = (*TParse)(unsafe.Pointer(pParse)).Fdb if !(p != 0) && pOnUsing != uintptr(0) && ((*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpOn != 0 || (*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpUsing != 0) { if (*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpOn != 0 { v1 = __ccgo_ts + 14834 } else { v1 = __ccgo_ts + 14837 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14843, libc.VaList(bp+8, v1)) goto append_from_error } p = _sqlite3SrcListAppend(tls, pParse, p, pTable, pDatabase) if p == uintptr(0) { goto append_from_error } _ = libc.Int32FromInt32(0) pItem = p + 8 + uintptr((*TSrcList)(unsafe.Pointer(p)).FnSrc-int32(1))*72 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && (*TSrcItem)(unsafe.Pointer(pItem)).FzName != 0 { if pDatabase != 0 && (*TToken)(unsafe.Pointer(pDatabase)).Fz != 0 { v2 = pDatabase } else { v2 = pTable } pToken = v2 _sqlite3RenameTokenMap(tls, pParse, (*TSrcItem)(unsafe.Pointer(pItem)).FzName, pToken) } _ = libc.Int32FromInt32(0) if (*TToken)(unsafe.Pointer(pAlias)).Fn != 0 { (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias = _sqlite3NameFromToken(tls, db, pAlias) } if pSubquery != 0 { (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect = pSubquery if (*TSelect)(unsafe.Pointer(pSubquery)).FselFlags&uint32(SF_NestedFrom) != 0 { libc.SetBitFieldPtr16Uint32(pItem+36+4, libc.Uint32FromInt32(1), 13, 0x2000) } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pOnUsing == uintptr(0) { *(*uintptr)(unsafe.Pointer(pItem + 48)) = uintptr(0) } else { if (*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpUsing != 0 { libc.SetBitFieldPtr16Uint32(pItem+36+4, libc.Uint32FromInt32(1), 10, 0x400) *(*uintptr)(unsafe.Pointer(pItem + 48)) = (*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpUsing } else { *(*uintptr)(unsafe.Pointer(pItem + 48)) = (*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpOn } } return p goto append_from_error append_from_error: ; _ = libc.Int32FromInt32(0) _sqlite3ClearOnOrUsing(tls, db, pOnUsing) _sqlite3SelectDelete(tls, db, pSubquery) return uintptr(0) } // C documentation // // /* // ** Add an INDEXED BY or NOT INDEXED clause to the most recently added // ** element of the source-list passed as the second argument. // */ func _sqlite3SrcListIndexedBy(tls *libc.TLS, pParse uintptr, p uintptr, pIndexedBy uintptr) { var pItem uintptr _ = pItem _ = libc.Int32FromInt32(0) if p != 0 && (*TToken)(unsafe.Pointer(pIndexedBy)).Fn > uint32(0) { _ = libc.Int32FromInt32(0) pItem = p + 8 + uintptr((*TSrcList)(unsafe.Pointer(p)).FnSrc-int32(1))*72 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TToken)(unsafe.Pointer(pIndexedBy)).Fn == uint32(1) && !((*TToken)(unsafe.Pointer(pIndexedBy)).Fz != 0) { /* A "NOT INDEXED" clause was supplied. See parse.y ** construct "indexed_opt" for details. */ libc.SetBitFieldPtr16Uint32(pItem+36+4, libc.Uint32FromInt32(1), 0, 0x1) } else { *(*uintptr)(unsafe.Pointer(pItem + 64)) = _sqlite3NameFromToken(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pIndexedBy) libc.SetBitFieldPtr16Uint32(pItem+36+4, libc.Uint32FromInt32(1), 1, 0x2) _ = libc.Int32FromInt32(0) /* No collision on union u2 */ } } } // C documentation // // /* // ** Append the contents of SrcList p2 to SrcList p1 and return the resulting // ** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2 // ** are deleted by this function. // */ func _sqlite3SrcListAppendList(tls *libc.TLS, pParse uintptr, p1 uintptr, p2 uintptr) (r uintptr) { var pNew, p11 uintptr _, _ = pNew, p11 _ = libc.Int32FromInt32(0) if p2 != 0 { pNew = _sqlite3SrcListEnlarge(tls, pParse, p1, (*TSrcList)(unsafe.Pointer(p2)).FnSrc, int32(1)) if pNew == uintptr(0) { _sqlite3SrcListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, p2) } else { p1 = pNew libc.Xmemcpy(tls, p1+8+1*72, p2+8, uint32((*TSrcList)(unsafe.Pointer(p2)).FnSrc)*uint32(72)) _sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, p2) p11 = p1 + 8 + 36 *(*Tu8)(unsafe.Pointer(p11)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p11))) | libc.Int32FromInt32(JT_LTORJ)&int32((*(*TSrcItem)(unsafe.Pointer(p1 + 8 + 1*72))).Ffg.Fjointype)) } } return p1 } // C documentation // // /* // ** Add the list of function arguments to the SrcList entry for a // ** table-valued-function. // */ func _sqlite3SrcListFuncArgs(tls *libc.TLS, pParse uintptr, p uintptr, pList uintptr) { var pItem uintptr _ = pItem if p != 0 { pItem = p + 8 + uintptr((*TSrcList)(unsafe.Pointer(p)).FnSrc-int32(1))*72 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pItem + 64)) = pList libc.SetBitFieldPtr16Uint32(pItem+36+4, libc.Uint32FromInt32(1), 2, 0x4) } else { _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pList) } } // C documentation // // /* // ** When building up a FROM clause in the parser, the join operator // ** is initially attached to the left operand. But the code generator // ** expects the join operator to be on the right operand. This routine // ** Shifts all join operators from left to right for an entire FROM // ** clause. // ** // ** Example: Suppose the join is like this: // ** // ** A natural cross join B // ** // ** The operator is "natural cross join". The A and B operands are stored // ** in p->a[0] and p->a[1], respectively. The parser initially stores the // ** operator with A. This routine shifts that operator over to B. // ** // ** Additional changes: // ** // ** * All tables to the left of the right-most RIGHT JOIN are tagged with // ** JT_LTORJ (mnemonic: Left Table Of Right Join) so that the // ** code generator can easily tell that the table is part of // ** the left operand of at least one RIGHT JOIN. // */ func _sqlite3SrcListShiftJoinType(tls *libc.TLS, pParse uintptr, p uintptr) { var allFlags, v3 Tu8 var i, v1, v5 int32 var p7 uintptr _, _, _, _, _, _ = allFlags, i, v1, v3, v5, p7 _ = pParse if p != 0 && (*TSrcList)(unsafe.Pointer(p)).FnSrc > int32(1) { i = (*TSrcList)(unsafe.Pointer(p)).FnSrc - int32(1) allFlags = uint8(0) for { v3 = (*(*TSrcItem)(unsafe.Pointer(p + 8 + uintptr(i-int32(1))*72))).Ffg.Fjointype (*(*TSrcItem)(unsafe.Pointer(p + 8 + uintptr(i)*72))).Ffg.Fjointype = v3 allFlags = Tu8(int32(allFlags) | int32(v3)) goto _2 _2: ; i-- v1 = i if !(v1 > 0) { break } } (*(*TSrcItem)(unsafe.Pointer(p + 8))).Ffg.Fjointype = uint8(0) /* All terms to the left of a RIGHT JOIN should be tagged with the ** JT_LTORJ flags */ if int32(allFlags)&int32(JT_RIGHT) != 0 { i = (*TSrcList)(unsafe.Pointer(p)).FnSrc - int32(1) for { if !(i > 0 && int32((*(*TSrcItem)(unsafe.Pointer(p + 8 + uintptr(i)*72))).Ffg.Fjointype)&int32(JT_RIGHT) == 0) { break } goto _4 _4: ; i-- } i-- _ = libc.Int32FromInt32(0) for { p7 = p + 8 + uintptr(i)*72 + 36 *(*Tu8)(unsafe.Pointer(p7)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p7))) | libc.Int32FromInt32(JT_LTORJ)) goto _6 _6: ; i-- v5 = i if !(v5 >= 0) { break } } } } } // C documentation // // /* // ** Generate VDBE code for a BEGIN statement. // */ func _sqlite3BeginTransaction(tls *libc.TLS, pParse uintptr, type1 int32) { var db, pBt, v uintptr var eTxnType, i int32 _, _, _, _, _ = db, eTxnType, i, pBt, v _ = libc.Int32FromInt32(0) db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_TRANSACTION), __ccgo_ts+14879, uintptr(0), uintptr(0)) != 0 { return } v = _sqlite3GetVdbe(tls, pParse) if !(v != 0) { return } if type1 != int32(TK_DEFERRED) { i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt if pBt != 0 && _sqlite3BtreeIsReadonly(tls, pBt) != 0 { eTxnType = 0 /* Read txn */ } else { if type1 == int32(TK_EXCLUSIVE) { eTxnType = int32(2) /* Exclusive txn */ } else { eTxnType = int32(1) /* Write txn */ } } _sqlite3VdbeAddOp2(tls, v, int32(OP_Transaction), i, eTxnType) _sqlite3VdbeUsesBtree(tls, v, i) goto _1 _1: ; i++ } } _sqlite3VdbeAddOp0(tls, v, int32(OP_AutoCommit)) } // C documentation // // /* // ** Generate VDBE code for a COMMIT or ROLLBACK statement. // ** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise // ** code is generated for a COMMIT. // */ func _sqlite3EndTransaction(tls *libc.TLS, pParse uintptr, eType int32) { var isRollback int32 var v, v1 uintptr _, _, _ = isRollback, v, v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) isRollback = libc.BoolInt32(eType == int32(TK_ROLLBACK)) if isRollback != 0 { v1 = __ccgo_ts + 14885 } else { v1 = __ccgo_ts + 14894 } if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_TRANSACTION), v1, uintptr(0), uintptr(0)) != 0 { return } v = _sqlite3GetVdbe(tls, pParse) if v != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_AutoCommit), int32(1), isRollback) } } // C documentation // // /* // ** This function is called by the parser when it parses a command to create, // ** release or rollback an SQL savepoint. // */ func _sqlite3Savepoint(tls *libc.TLS, pParse uintptr, op int32, pName uintptr) { var v, zName uintptr _, _ = v, zName zName = _sqlite3NameFromToken(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pName) if zName != 0 { v = _sqlite3GetVdbe(tls, pParse) _ = libc.Int32FromInt32(0) if !(v != 0) || _sqlite3AuthCheck(tls, pParse, int32(SQLITE_SAVEPOINT), _az[op], zName, uintptr(0)) != 0 { _sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, zName) return } _sqlite3VdbeAddOp4(tls, v, OP_Savepoint, op, 0, 0, zName, -int32(6)) } } var _az = [3]uintptr{ 0: __ccgo_ts + 14879, 1: __ccgo_ts + 14901, 2: __ccgo_ts + 14885, } // C documentation // // /* // ** Make sure the TEMP database is open and available for use. Return // ** the number of errors. Leave any error messages in the pParse structure. // */ func _sqlite3OpenTempDatabase(tls *libc.TLS, pParse uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var db uintptr var rc int32 var _ /* pBt at bp+0 */ uintptr _, _ = db, rc db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt == uintptr(0) && !((*TParse)(unsafe.Pointer(pParse)).Fexplain != 0) { rc = _sqlite3BtreeOpen(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, uintptr(0), db, bp, 0, _flags) if rc != SQLITE_OK { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14909, 0) (*TParse)(unsafe.Pointer(pParse)).Frc = rc return int32(1) } (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt = *(*uintptr)(unsafe.Pointer(bp)) _ = libc.Int32FromInt32(0) if int32(SQLITE_NOMEM) == _sqlite3BtreeSetPageSize(tls, *(*uintptr)(unsafe.Pointer(bp)), (*Tsqlite3)(unsafe.Pointer(db)).FnextPagesize, 0, 0) { _sqlite3OomFault(tls, db) return int32(1) } } return 0 } var _flags = libc.Int32FromInt32(SQLITE_OPEN_READWRITE) | libc.Int32FromInt32(SQLITE_OPEN_CREATE) | libc.Int32FromInt32(SQLITE_OPEN_EXCLUSIVE) | libc.Int32FromInt32(SQLITE_OPEN_DELETEONCLOSE) | libc.Int32FromInt32(SQLITE_OPEN_TEMP_DB) // C documentation // // /* // ** Record the fact that the schema cookie will need to be verified // ** for database iDb. The code to actually verify the schema cookie // ** will occur at the end of the top-level VDBE and will be generated // ** later, by sqlite3FinishCoding(). // */ func _sqlite3CodeVerifySchemaAtToplevel(tls *libc.TLS, pToplevel uintptr, iDb int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if libc.BoolInt32((*TParse)(unsafe.Pointer(pToplevel)).FcookieMask&(libc.Uint32FromInt32(1)<>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) { v2 = libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(6)<= 0 { zMsg = _sqlite3MPrintf(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, __ccgo_ts+12381, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr((*TTable)(unsafe.Pointer(pTab)).FiPKey)*12))).FzCnName)) rc = libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(6)<= 0 && 0 == _sqlite3StrICmp(tls, z, zColl) { return int32(1) } goto _1 _1: ; i++ } return 0 } // C documentation // // /* // ** Recompute all indices of pTab that use the collating sequence pColl. // ** If pColl==0 then recompute all indices of pTab. // */ func _reindexTable(tls *libc.TLS, pParse uintptr, pTab uintptr, zColl uintptr) { var iDb int32 var pIndex uintptr _, _ = iDb, pIndex if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) { /* An index associated with pTab */ pIndex = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIndex != 0) { break } if zColl == uintptr(0) || _collationMatch(tls, zColl, pIndex) != 0 { iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTable)(unsafe.Pointer(pTab)).FpSchema) _sqlite3BeginWriteOperation(tls, pParse, 0, iDb) _sqlite3RefillIndex(tls, pParse, pIndex, -int32(1)) } goto _1 _1: ; pIndex = (*TIndex)(unsafe.Pointer(pIndex)).FpNext } } } // C documentation // // /* // ** Recompute all indices of all tables in all databases where the // ** indices use the collating sequence pColl. If pColl==0 then recompute // ** all indices everywhere. // */ func _reindexDatabases(tls *libc.TLS, pParse uintptr, zColl uintptr) { var db, k, pDb, pTab uintptr var iDb int32 _, _, _, _, _ = db, iDb, k, pDb, pTab /* The database index number */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* A table in the database */ _ = libc.Int32FromInt32(0) /* Needed for schema access */ iDb = 0 pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb for { if !(iDb < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } _ = libc.Int32FromInt32(0) k = (*THash)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema + 8)).Ffirst for { if !(k != 0) { break } pTab = (*THashElem)(unsafe.Pointer(k)).Fdata _reindexTable(tls, pParse, pTab, zColl) goto _2 _2: ; k = (*THashElem)(unsafe.Pointer(k)).Fnext } goto _1 _1: ; iDb++ pDb += 16 } } // C documentation // // /* // ** Generate code for the REINDEX command. // ** // ** REINDEX -- 1 // ** REINDEX -- 2 // ** REINDEX ?.? -- 3 // ** REINDEX ?.? -- 4 // ** // ** Form 1 causes all indices in all attached databases to be rebuilt. // ** Form 2 rebuilds all indices in all databases that use the named // ** collating function. Forms 3 and 4 rebuild the named index or all // ** indices associated with the named table. // */ func _sqlite3Reindex(tls *libc.TLS, pParse uintptr, pName1 uintptr, pName2 uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, pColl, pIndex, pTab, z, zColl, zDb, v1 uintptr var iDb int32 var _ /* pObjName at bp+0 */ uintptr _, _, _, _, _, _, _, _, _ = db, iDb, pColl, pIndex, pTab, z, zColl, zDb, v1 /* The database index number */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Name of the table or index to be reindexed */ /* Read the database schema. If an error occurs, leave an error message ** and code in pParse and return NULL. */ if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) { return } if pName1 == uintptr(0) { _reindexDatabases(tls, pParse, uintptr(0)) return } else { if pName2 == uintptr(0) || (*TToken)(unsafe.Pointer(pName2)).Fz == uintptr(0) { _ = libc.Int32FromInt32(0) zColl = _sqlite3NameFromToken(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pName1) if !(zColl != 0) { return } pColl = _sqlite3FindCollSeq(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).Fenc, zColl, 0) if pColl != 0 { _reindexDatabases(tls, pParse, zColl) _sqlite3DbFree(tls, db, zColl) return } _sqlite3DbFree(tls, db, zColl) } } iDb = _sqlite3TwoPartName(tls, pParse, pName1, pName2, bp) if iDb < 0 { return } z = _sqlite3NameFromToken(tls, db, *(*uintptr)(unsafe.Pointer(bp))) if z == uintptr(0) { return } if (*TToken)(unsafe.Pointer(pName2)).Fn != 0 { v1 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName } else { v1 = uintptr(0) } zDb = v1 pTab = _sqlite3FindTable(tls, db, z, zDb) if pTab != 0 { _reindexTable(tls, pParse, pTab, uintptr(0)) _sqlite3DbFree(tls, db, z) return } pIndex = _sqlite3FindIndex(tls, db, z, zDb) _sqlite3DbFree(tls, db, z) if pIndex != 0 { iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FpTable)).FpSchema) _sqlite3BeginWriteOperation(tls, pParse, 0, iDb) _sqlite3RefillIndex(tls, pParse, pIndex, -int32(1)) return } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15002, 0) } // C documentation // // /* // ** Return a KeyInfo structure that is appropriate for the given Index. // ** // ** The caller should invoke sqlite3KeyInfoUnref() on the returned object // ** when it has finished using it. // */ func _sqlite3KeyInfoOfIndex(tls *libc.TLS, pParse uintptr, pIdx uintptr) (r uintptr) { var i, nCol, nKey int32 var pKey, zColl, v2 uintptr _, _, _, _, _, _ = i, nCol, nKey, pKey, zColl, v2 nCol = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) nKey = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return uintptr(0) } if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x8>>3)) != 0 { pKey = _sqlite3KeyInfoAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, nKey, nCol-nKey) } else { pKey = _sqlite3KeyInfoAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, nCol, 0) } if pKey != 0 { _ = libc.Int32FromInt32(0) i = 0 for { if !(i < nCol) { break } zColl = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(i)*4)) if zColl == uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) { v2 = uintptr(0) } else { v2 = _sqlite3LocateCollSeq(tls, pParse, zColl) } *(*uintptr)(unsafe.Pointer(pKey + 20 + uintptr(i)*4)) = v2 *(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pKey)).FaSortFlags + uintptr(i))) = *(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder + uintptr(i))) _ = libc.Int32FromInt32(0) goto _1 _1: ; i++ } if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { _ = libc.Int32FromInt32(0) if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x200>>9)) == 0 { /* Deactivate the index because it contains an unknown collating ** sequence. The only way to reactive the index is to reload the ** schema. Adding the missing collating sequence later does not ** reactive the index. The application had the chance to register ** the missing index using the collation-needed callback. For ** simplicity, SQLite will not give the application a second chance. */ libc.SetBitFieldPtr16Uint32(pIdx+56, libc.Uint32FromInt32(1), 9, 0x200) (*TParse)(unsafe.Pointer(pParse)).Frc = libc.Int32FromInt32(SQLITE_ERROR) | libc.Int32FromInt32(2)< UTF-16 conversion if // ** possible. // */ func _synthCollSeq(tls *libc.TLS, db uintptr, pColl uintptr) (r int32) { var i int32 var pColl2, z uintptr _, _, _ = i, pColl2, z z = (*TCollSeq)(unsafe.Pointer(pColl)).FzName i = 0 for { if !(i < int32(3)) { break } pColl2 = _sqlite3FindCollSeq(tls, db, _aEnc[i], z, 0) if (*TCollSeq)(unsafe.Pointer(pColl2)).FxCmp != uintptr(0) { libc.Xmemcpy(tls, pColl, pColl2, uint32(20)) (*TCollSeq)(unsafe.Pointer(pColl)).FxDel = uintptr(0) /* Do not copy the destructor */ return SQLITE_OK } goto _1 _1: ; i++ } return int32(SQLITE_ERROR) } var _aEnc = [3]Tu8{ 0: uint8(SQLITE_UTF16BE), 1: uint8(SQLITE_UTF16LE), 2: uint8(SQLITE_UTF8), } // C documentation // // /* // ** This routine is called on a collation sequence before it is used to // ** check that it is defined. An undefined collation sequence exists when // ** a database is loaded that contains references to collation sequences // ** that have not been defined by sqlite3_create_collation() etc. // ** // ** If required, this routine calls the 'collation needed' callback to // ** request a definition of the collating sequence. If this doesn't work, // ** an equivalent collating sequence that uses a text encoding different // ** from the main database is substituted, if one is available. // */ func _sqlite3CheckCollSeq(tls *libc.TLS, pParse uintptr, pColl uintptr) (r int32) { var db, p, zName uintptr _, _, _ = db, p, zName if pColl != 0 && (*TCollSeq)(unsafe.Pointer(pColl)).FxCmp == uintptr(0) { zName = (*TCollSeq)(unsafe.Pointer(pColl)).FzName db = (*TParse)(unsafe.Pointer(pParse)).Fdb p = _sqlite3GetCollSeq(tls, pParse, (*Tsqlite3)(unsafe.Pointer(db)).Fenc, pColl, zName) if !(p != 0) { return int32(SQLITE_ERROR) } _ = libc.Int32FromInt32(0) } return SQLITE_OK } // C documentation // // /* // ** Locate and return an entry from the db.aCollSeq hash table. If the entry // ** specified by zName and nName is not found and parameter 'create' is // ** true, then create a new entry. Otherwise return NULL. // ** // ** Each pointer stored in the sqlite3.aCollSeq hash table contains an // ** array of three CollSeq structures. The first is the collation sequence // ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be. // ** // ** Stored immediately after the three collation sequences is a copy of // ** the collation sequence name. A pointer to this string is stored in // ** each collation sequence structure. // */ func _findCollSeqEntry(tls *libc.TLS, db uintptr, zName uintptr, create int32) (r uintptr) { var nName int32 var pColl, pDel uintptr _, _, _ = nName, pColl, pDel pColl = _sqlite3HashFind(tls, db+448, zName) if uintptr(0) == pColl && create != 0 { nName = _sqlite3Strlen30(tls, zName) + int32(1) pColl = _sqlite3DbMallocZero(tls, db, uint64(libc.Uint32FromInt32(3)*libc.Uint32FromInt64(20)+uint32(nName))) if pColl != 0 { pDel = uintptr(0) (*(*TCollSeq)(unsafe.Pointer(pColl))).FzName = pColl + 3*20 (*(*TCollSeq)(unsafe.Pointer(pColl))).Fenc = uint8(SQLITE_UTF8) (*(*TCollSeq)(unsafe.Pointer(pColl + 1*20))).FzName = pColl + 3*20 (*(*TCollSeq)(unsafe.Pointer(pColl + 1*20))).Fenc = uint8(SQLITE_UTF16LE) (*(*TCollSeq)(unsafe.Pointer(pColl + 2*20))).FzName = pColl + 3*20 (*(*TCollSeq)(unsafe.Pointer(pColl + 2*20))).Fenc = uint8(SQLITE_UTF16BE) libc.Xmemcpy(tls, (*(*TCollSeq)(unsafe.Pointer(pColl))).FzName, zName, uint32(nName)) pDel = _sqlite3HashInsert(tls, db+448, (*(*TCollSeq)(unsafe.Pointer(pColl))).FzName, pColl) /* If a malloc() failure occurred in sqlite3HashInsert(), it will ** return the pColl pointer to be deleted (because it wasn't added ** to the hash table). */ _ = libc.Int32FromInt32(0) if pDel != uintptr(0) { _sqlite3OomFault(tls, db) _sqlite3DbFree(tls, db, pDel) pColl = uintptr(0) } } } return pColl } // C documentation // // /* // ** Parameter zName points to a UTF-8 encoded string nName bytes long. // ** Return the CollSeq* pointer for the collation sequence named zName // ** for the encoding 'enc' from the database 'db'. // ** // ** If the entry specified is not found and 'create' is true, then create a // ** new entry. Otherwise return NULL. // ** // ** A separate function sqlite3LocateCollSeq() is a wrapper around // ** this routine. sqlite3LocateCollSeq() invokes the collation factory // ** if necessary and generates an error message if the collating sequence // ** cannot be found. // ** // ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq() // */ func _sqlite3FindCollSeq(tls *libc.TLS, db uintptr, enc Tu8, zName uintptr, create int32) (r uintptr) { var pColl uintptr _ = pColl _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if zName != 0 { pColl = _findCollSeqEntry(tls, db, zName, create) if pColl != 0 { pColl += uintptr(int32(enc)-int32(1)) * 20 } } else { pColl = (*Tsqlite3)(unsafe.Pointer(db)).FpDfltColl } return pColl } // C documentation // // /* // ** Change the text encoding for a database connection. This means that // ** the pDfltColl must change as well. // */ func _sqlite3SetTextEncoding(tls *libc.TLS, db uintptr, enc Tu8) { _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).Fenc = enc /* EVIDENCE-OF: R-08308-17224 The default collating function for all ** strings is BINARY. */ (*Tsqlite3)(unsafe.Pointer(db)).FpDfltColl = _sqlite3FindCollSeq(tls, db, enc, uintptr(unsafe.Pointer(&_sqlite3StrBINARY)), 0) _sqlite3ExpirePreparedStatements(tls, db, int32(1)) } // C documentation // // /* // ** This function is responsible for invoking the collation factory callback // ** or substituting a collation sequence of a different encoding when the // ** requested collation sequence is not available in the desired encoding. // ** // ** If it is not NULL, then pColl must point to the database native encoding // ** collation sequence with name zName, length nName. // ** // ** The return value is either the collation sequence to be used in database // ** db for collation type name zName, length nName, or NULL, if no collation // ** sequence can be found. If no collation is found, leave an error message. // ** // ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq() // */ func _sqlite3GetCollSeq(tls *libc.TLS, pParse uintptr, enc Tu8, pColl uintptr, zName uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, p uintptr _, _ = db, p db = (*TParse)(unsafe.Pointer(pParse)).Fdb p = pColl if !(p != 0) { p = _sqlite3FindCollSeq(tls, db, enc, zName, 0) } if !(p != 0) || !((*TCollSeq)(unsafe.Pointer(p)).FxCmp != 0) { /* No collation sequence of this type for this encoding is registered. ** Call the collation factory to see if it can supply us with one. */ _callCollNeeded(tls, db, int32(enc), zName) p = _sqlite3FindCollSeq(tls, db, enc, zName, 0) } if p != 0 && !((*TCollSeq)(unsafe.Pointer(p)).FxCmp != 0) && _synthCollSeq(tls, db, p) != 0 { p = uintptr(0) } _ = libc.Int32FromInt32(0) if p == uintptr(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15078, libc.VaList(bp+8, zName)) (*TParse)(unsafe.Pointer(pParse)).Frc = libc.Int32FromInt32(SQLITE_ERROR) | libc.Int32FromInt32(1)<nArg // ** is also -1. In other words, we are searching for a function that // ** takes a variable number of arguments. // ** // ** If nArg is -2 that means that we are searching for any function // ** regardless of the number of arguments it uses, so return a positive // ** match score for any // ** // ** The returned value is always between 0 and 6, as follows: // ** // ** 0: Not a match. // ** 1: UTF8/16 conversion required and function takes any number of arguments. // ** 2: UTF16 byte order change required and function takes any number of args. // ** 3: encoding matches and function takes any number of arguments // ** 4: UTF8/16 conversion required - argument count matches exactly // ** 5: UTF16 byte order conversion required - argument count matches exactly // ** 6: Perfect match: encoding and argument count match exactly. // ** // ** If nArg==(-2) then any function with a non-null xSFunc is // ** a perfect match and any function with xSFunc NULL is // ** a non-match. // */ func _matchQuality(tls *libc.TLS, p uintptr, nArg int32, enc Tu8) (r int32) { var match, v1 int32 _, _ = match, v1 _ = libc.Int32FromInt32(0) /* Wrong number of arguments means "no match" */ if int32((*TFuncDef)(unsafe.Pointer(p)).FnArg) != nArg { if nArg == -int32(2) { if (*TFuncDef)(unsafe.Pointer(p)).FxSFunc == uintptr(0) { v1 = 0 } else { v1 = int32(FUNC_PERFECT_MATCH) } return v1 } if int32((*TFuncDef)(unsafe.Pointer(p)).FnArg) >= 0 { return 0 } } /* Give a better score to a function with a specific number of arguments ** than to function that accepts any number of arguments. */ if int32((*TFuncDef)(unsafe.Pointer(p)).FnArg) == nArg { match = int32(4) } else { match = int32(1) } /* Bonus points if the text encoding matches */ if uint32(enc) == (*TFuncDef)(unsafe.Pointer(p)).FfuncFlags&uint32(SQLITE_FUNC_ENCMASK) { match += int32(2) /* Exact encoding match */ } else { if uint32(enc)&(*TFuncDef)(unsafe.Pointer(p)).FfuncFlags&uint32(2) != uint32(0) { match += int32(1) /* Both are UTF16, but with different byte orders */ } } return match } // C documentation // // /* // ** Search a FuncDefHash for a function with the given name. Return // ** a pointer to the matching FuncDef if found, or 0 if there is no match. // */ func _sqlite3FunctionSearch(tls *libc.TLS, h int32, zFunc uintptr) (r uintptr) { var p uintptr _ = p p = *(*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)) + uintptr(h)*4)) for { if !(p != 0) { break } _ = libc.Int32FromInt32(0) if _sqlite3StrICmp(tls, (*TFuncDef)(unsafe.Pointer(p)).FzName, zFunc) == 0 { return p } goto _1 _1: ; p = *(*uintptr)(unsafe.Pointer(p + 36)) } return uintptr(0) } // C documentation // // /* // ** Insert a new FuncDef into a FuncDefHash hash table. // */ func _sqlite3InsertBuiltinFuncs(tls *libc.TLS, aDef uintptr, nDef int32) { var h, i, nName int32 var pOther, zName uintptr _, _, _, _, _ = h, i, nName, pOther, zName i = 0 for { if !(i < nDef) { break } zName = (*(*TFuncDef)(unsafe.Pointer(aDef + uintptr(i)*40))).FzName nName = _sqlite3Strlen30(tls, zName) h = (int32(*(*uint8)(unsafe.Pointer(zName))) + nName) % int32(SQLITE_FUNC_HASH_SZ) _ = libc.Int32FromInt32(0) pOther = _sqlite3FunctionSearch(tls, h, zName) if pOther != 0 { _ = libc.Int32FromInt32(0) (*(*TFuncDef)(unsafe.Pointer(aDef + uintptr(i)*40))).FpNext = (*TFuncDef)(unsafe.Pointer(pOther)).FpNext (*TFuncDef)(unsafe.Pointer(pOther)).FpNext = aDef + uintptr(i)*40 } else { (*(*TFuncDef)(unsafe.Pointer(aDef + uintptr(i)*40))).FpNext = uintptr(0) *(*uintptr)(unsafe.Pointer(aDef + uintptr(i)*40 + 36)) = *(*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)) + uintptr(h)*4)) *(*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)) + uintptr(h)*4)) = aDef + uintptr(i)*40 } goto _1 _1: ; i++ } } // C documentation // // /* // ** Locate a user function given a name, a number of arguments and a flag // ** indicating whether the function prefers UTF-16 over UTF-8. Return a // ** pointer to the FuncDef structure that defines that function, or return // ** NULL if the function does not exist. // ** // ** If the createFlag argument is true, then a new (blank) FuncDef // ** structure is created and liked into the "db" structure if a // ** no matching function previously existed. // ** // ** If nArg is -2, then the first valid function found is returned. A // ** function is valid if xSFunc is non-zero. The nArg==(-2) // ** case is used to see if zName is a valid function name for some number // ** of arguments. If nArg is -2, then createFlag must be 0. // ** // ** If createFlag is false, then a function with the required name and // ** number of arguments may be returned even if the eTextRep flag does not // ** match that requested. // */ func _sqlite3FindFunction(tls *libc.TLS, db uintptr, zName uintptr, nArg int32, enc Tu8, createFlag Tu8) (r uintptr) { var bestScore, h, nName, score, score1 int32 var p, pBest, pOther, z, v1 uintptr var v2 bool _, _, _, _, _, _, _, _, _, _, _ = bestScore, h, nName, p, pBest, pOther, score, score1, z, v1, v2 /* Iterator variable */ pBest = uintptr(0) /* Best match found so far */ bestScore = 0 /* Length of the name */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) nName = _sqlite3Strlen30(tls, zName) /* First search for a match amongst the application-defined functions. */ p = _sqlite3HashFind(tls, db+432, zName) for p != 0 { score = _matchQuality(tls, p, nArg, enc) if score > bestScore { pBest = p bestScore = score } p = (*TFuncDef)(unsafe.Pointer(p)).FpNext } /* If no match is found, search the built-in functions. ** ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in ** functions even if a prior app-defined function was found. And give ** priority to built-in functions. ** ** Except, if createFlag is true, that means that we are trying to ** install a new function. Whatever FuncDef structure is returned it will ** have fields overwritten with new information appropriate for the ** new function. But the FuncDefs for built-in functions are read-only. ** So we must not search for built-ins when creating a new function. */ if !(createFlag != 0) && (pBest == uintptr(0) || (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_PreferBuiltin) != uint32(0)) { bestScore = 0 h = (int32(_sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(zName))]) + nName) % int32(SQLITE_FUNC_HASH_SZ) p = _sqlite3FunctionSearch(tls, h, zName) for p != 0 { score1 = _matchQuality(tls, p, nArg, enc) if score1 > bestScore { pBest = p bestScore = score1 } p = (*TFuncDef)(unsafe.Pointer(p)).FpNext } } /* If the createFlag parameter is true and the search did not reveal an ** exact match for the name, number of arguments and encoding, then add a ** new entry to the hash table and return it. */ if v2 = createFlag != 0 && bestScore < int32(FUNC_PERFECT_MATCH); v2 { v1 = _sqlite3DbMallocZero(tls, db, uint64(uint32(40)+uint32(nName)+uint32(1))) pBest = v1 } if v2 && v1 != uintptr(0) { (*TFuncDef)(unsafe.Pointer(pBest)).FzName = pBest + 1*40 (*TFuncDef)(unsafe.Pointer(pBest)).FnArg = int8(uint16(nArg)) (*TFuncDef)(unsafe.Pointer(pBest)).FfuncFlags = uint32(enc) libc.Xmemcpy(tls, pBest+1*40, zName, uint32(nName+int32(1))) z = (*TFuncDef)(unsafe.Pointer(pBest)).FzName for { if !(*(*Tu8)(unsafe.Pointer(z)) != 0) { break } *(*Tu8)(unsafe.Pointer(z)) = _sqlite3UpperToLower[*(*Tu8)(unsafe.Pointer(z))] goto _3 _3: ; z++ } pOther = _sqlite3HashInsert(tls, db+432, (*TFuncDef)(unsafe.Pointer(pBest)).FzName, pBest) if pOther == pBest { _sqlite3DbFree(tls, db, pBest) _sqlite3OomFault(tls, db) return uintptr(0) } else { (*TFuncDef)(unsafe.Pointer(pBest)).FpNext = pOther } } if pBest != 0 && ((*TFuncDef)(unsafe.Pointer(pBest)).FxSFunc != 0 || createFlag != 0) { return pBest } return uintptr(0) } // C documentation // // /* // ** Free all resources held by the schema structure. The void* argument points // ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the // ** pointer itself, it just cleans up subsidiary resources (i.e. the contents // ** of the schema hash tables). // ** // ** The Schema.cache_size variable is not cleared. // */ func _sqlite3SchemaClear(tls *libc.TLS, p uintptr) { bp := tls.Alloc(608) defer tls.Free(608) var pElem, pSchema, pTab, p3 uintptr var _ /* temp1 at bp+0 */ THash var _ /* temp2 at bp+16 */ THash var _ /* xdb at bp+32 */ Tsqlite3 _, _, _, _ = pElem, pSchema, pTab, p3 pSchema = p libc.Xmemset(tls, bp+32, 0, uint32(576)) *(*THash)(unsafe.Pointer(bp)) = (*TSchema)(unsafe.Pointer(pSchema)).FtblHash *(*THash)(unsafe.Pointer(bp + 16)) = (*TSchema)(unsafe.Pointer(pSchema)).FtrigHash _sqlite3HashInit(tls, pSchema+40) _sqlite3HashClear(tls, pSchema+24) pElem = (*THash)(unsafe.Pointer(bp + 16)).Ffirst for { if !(pElem != 0) { break } _sqlite3DeleteTrigger(tls, bp+32, (*THashElem)(unsafe.Pointer(pElem)).Fdata) goto _1 _1: ; pElem = (*THashElem)(unsafe.Pointer(pElem)).Fnext } _sqlite3HashClear(tls, bp+16) _sqlite3HashInit(tls, pSchema+8) pElem = (*THash)(unsafe.Pointer(bp)).Ffirst for { if !(pElem != 0) { break } pTab = (*THashElem)(unsafe.Pointer(pElem)).Fdata _sqlite3DeleteTable(tls, bp+32, pTab) goto _2 _2: ; pElem = (*THashElem)(unsafe.Pointer(pElem)).Fnext } _sqlite3HashClear(tls, bp) _sqlite3HashClear(tls, pSchema+56) (*TSchema)(unsafe.Pointer(pSchema)).FpSeqTab = uintptr(0) if int32((*TSchema)(unsafe.Pointer(pSchema)).FschemaFlags)&int32(DB_SchemaLoaded) != 0 { (*TSchema)(unsafe.Pointer(pSchema)).FiGeneration++ } p3 = pSchema + 78 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) & ^(libc.Int32FromInt32(DB_SchemaLoaded) | libc.Int32FromInt32(DB_ResetWanted))) } // C documentation // // /* // ** Find and return the schema associated with a BTree. Create // ** a new one if necessary. // */ func _sqlite3SchemaGet(tls *libc.TLS, db uintptr, pBt uintptr) (r uintptr) { var p uintptr _ = p if pBt != 0 { p = _sqlite3BtreeSchema(tls, pBt, int32(84), __ccgo_fp(_sqlite3SchemaClear)) } else { p = _sqlite3DbMallocZero(tls, uintptr(0), uint64(84)) } if !(p != 0) { _sqlite3OomFault(tls, db) } else { if 0 == int32((*TSchema)(unsafe.Pointer(p)).Ffile_format) { _sqlite3HashInit(tls, p+8) _sqlite3HashInit(tls, p+24) _sqlite3HashInit(tls, p+40) _sqlite3HashInit(tls, p+56) (*TSchema)(unsafe.Pointer(p)).Fenc = uint8(SQLITE_UTF8) } } return p } /************** End of callback.c ********************************************/ /************** Begin file delete.c ******************************************/ /* ** 2001 September 15 ** ** 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 contains C code routines that are called by the parser ** in order to generate code for DELETE FROM statements. */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** While a SrcList can in general represent multiple tables and subqueries // ** (as in the FROM clause of a SELECT statement) in this case it contains // ** the name of a single table, as one might find in an INSERT, DELETE, // ** or UPDATE statement. Look up that table in the symbol table and // ** return a pointer. Set an error message and return NULL if the table // ** name is not found or if any other error occurs. // ** // ** The following fields are initialized appropriate in pSrc: // ** // ** pSrc->a[0].pTab Pointer to the Table object // ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one // ** // */ func _sqlite3SrcListLookup(tls *libc.TLS, pParse uintptr, pSrc uintptr) (r uintptr) { var pItem, pTab uintptr _, _ = pItem, pTab pItem = pSrc + 8 _ = libc.Int32FromInt32(0) pTab = _sqlite3LocateTableItem(tls, pParse, uint32(0), pItem) if (*TSrcItem)(unsafe.Pointer(pItem)).FpTab != 0 { _sqlite3DeleteTable(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TSrcItem)(unsafe.Pointer(pItem)).FpTab) } (*TSrcItem)(unsafe.Pointer(pItem)).FpTab = pTab libc.SetBitFieldPtr16Uint32(pItem+36+4, libc.Uint32FromInt32(1), 9, 0x200) if pTab != 0 { (*TTable)(unsafe.Pointer(pTab)).FnTabRef++ if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x2>>1)) != 0 && _sqlite3IndexedByLookup(tls, pParse, pItem) != 0 { pTab = uintptr(0) } } return pTab } // C documentation // // /* Generate byte-code that will report the number of rows modified // ** by a DELETE, INSERT, or UPDATE statement. // */ func _sqlite3CodeChangeCount(tls *libc.TLS, v uintptr, regCounter int32, zColName uintptr) { _sqlite3VdbeAddOp0(tls, v, int32(OP_FkCheck)) _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), regCounter, int32(1)) _sqlite3VdbeSetNumCols(tls, v, int32(1)) _sqlite3VdbeSetColName(tls, v, 0, COLNAME_NAME, zColName, libc.UintptrFromInt32(0)) } // C documentation // // /* Return true if table pTab is read-only. // ** // ** A table is read-only if any of the following are true: // ** // ** 1) It is a virtual table and no implementation of the xUpdate method // ** has been provided // ** // ** 2) A trigger is currently being coded and the table is a virtual table // ** that is SQLITE_VTAB_DIRECTONLY or if PRAGMA trusted_schema=OFF and // ** the table is not SQLITE_VTAB_INNOCUOUS. // ** // ** 3) It is a system table (i.e. sqlite_schema), this call is not // ** part of a nested parse and writable_schema pragma has not // ** been specified // ** // ** 4) The table is a shadow table, the database connection is in // ** defensive mode, and the current sqlite3_prepare() // ** is for a top-level SQL statement. // */ func _vtabIsReadOnly(tls *libc.TLS, pParse uintptr, pTab uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) if (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer((*TVTable)(unsafe.Pointer(_sqlite3GetVTable(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pTab))).FpMod)).FpModule)).FxUpdate == uintptr(0) { return int32(1) } /* Within triggers: ** * Do not allow DELETE, INSERT, or UPDATE of SQLITE_VTAB_DIRECTONLY ** virtual tables ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS ** virtual tables if PRAGMA trusted_schema=ON. */ if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != uintptr(0) && int32((*TVTable)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).Fp)).FeVtabRisk) > libc.BoolInt32((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&uint64(SQLITE_TrustedSchema) != uint64(0)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15109, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) } return 0 } func _tabIsReadOnly(tls *libc.TLS, pParse uintptr, pTab uintptr) (r int32) { var db uintptr _ = db if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { return _vtabIsReadOnly(tls, pParse, pTab) } if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(libc.Int32FromInt32(TF_Readonly)|libc.Int32FromInt32(TF_Shadow)) == uint32(0) { return 0 } db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Readonly) != uint32(0) { return libc.BoolInt32(_sqlite3WritableSchema(tls, db) == 0 && int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0) } _ = libc.Int32FromInt32(0) return _sqlite3ReadOnlyShadowTables(tls, db) } // C documentation // // /* // ** Check to make sure the given table is writable. // ** // ** If pTab is not writable -> generate an error message and return 1. // ** If pTab is writable but other errors have occurred -> return 1. // ** If pTab is writable and no prior errors -> return 0; // */ func _sqlite3IsReadOnly(tls *libc.TLS, pParse uintptr, pTab uintptr, pTrigger uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) if _tabIsReadOnly(tls, pParse, pTab) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15142, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) return int32(1) } if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) && (pTrigger == uintptr(0) || (*TTrigger)(unsafe.Pointer(pTrigger)).FbReturning != 0 && (*TTrigger)(unsafe.Pointer(pTrigger)).FpNext == uintptr(0)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15171, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) return int32(1) } return 0 } // C documentation // // /* // ** Evaluate a view and store its result in an ephemeral table. The // ** pWhere argument is an optional WHERE clause that restricts the // ** set of rows in the view that are to be added to the ephemeral table. // */ func _sqlite3MaterializeView(tls *libc.TLS, pParse uintptr, pView uintptr, pWhere uintptr, pOrderBy uintptr, pLimit uintptr, iCur int32) { bp := tls.Alloc(32) defer tls.Free(32) var db, pFrom, pSel uintptr var iDb int32 var _ /* dest at bp+0 */ TSelectDest _, _, _, _ = db, iDb, pFrom, pSel db = (*TParse)(unsafe.Pointer(pParse)).Fdb iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pView)).FpSchema) pWhere = _sqlite3ExprDup(tls, db, pWhere, 0) pFrom = _sqlite3SrcListAppend(tls, pParse, uintptr(0), uintptr(0), uintptr(0)) if pFrom != 0 { _ = libc.Int32FromInt32(0) (*(*TSrcItem)(unsafe.Pointer(pFrom + 8))).FzName = _sqlite3DbStrDup(tls, db, (*TTable)(unsafe.Pointer(pView)).FzName) (*(*TSrcItem)(unsafe.Pointer(pFrom + 8))).FzDatabase = _sqlite3DbStrDup(tls, db, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } pSel = _sqlite3SelectNew(tls, pParse, uintptr(0), pFrom, pWhere, uintptr(0), uintptr(0), pOrderBy, uint32(SF_IncludeHidden), pLimit) _sqlite3SelectDestInit(tls, bp, int32(SRT_EphemTab), iCur) _sqlite3Select(tls, pParse, pSel, bp) _sqlite3SelectDelete(tls, db, pSel) } /* && !defined(SQLITE_OMIT_SUBQUERY) */ // C documentation // // /* // ** Generate code for a DELETE FROM statement. // ** // ** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL; // ** \________/ \________________/ // ** pTabList pWhere // */ func _sqlite3DeleteFrom(tls *libc.TLS, pParse uintptr, pTabList uintptr, pWhere uintptr, pOrderBy uintptr, pLimit uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var aToOpen, db, pIdx, pPk, pTab, pTrigger, pVTab, pWInfo, v, v13, v15, v18, v20, v3, v7 uintptr var addrBypass, addrEphOpen, addrLoop, bComplex, count, eOnePass, i, iAddrOnce, iDb, iEphCur, iKey, iPk, iRowSet, iTabCur, isView, memCnt, nIdx, rcauth, v1, v10, v11, v12, v14, v17, v19, v2, v5, v6, v8 int32 var nKey, nPk Ti16 var wcf Tu16 var _ /* aiCurOnePass at bp+56 */ [2]int32 var _ /* iDataCur at bp+0 */ int32 var _ /* iIdxCur at bp+4 */ int32 var _ /* sContext at bp+8 */ TAuthContext var _ /* sNC at bp+16 */ TNameContext _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aToOpen, addrBypass, addrEphOpen, addrLoop, bComplex, count, db, eOnePass, i, iAddrOnce, iDb, iEphCur, iKey, iPk, iRowSet, iTabCur, isView, memCnt, nIdx, nKey, nPk, pIdx, pPk, pTab, pTrigger, pVTab, pWInfo, rcauth, v, wcf, v1, v10, v11, v12, v13, v14, v15, v17, v18, v19, v2, v20, v3, v5, v6, v7, v8 /* Cursor number for the table */ *(*int32)(unsafe.Pointer(bp)) = 0 /* VDBE cursor for the canonical data source */ *(*int32)(unsafe.Pointer(bp + 4)) = 0 /* Database number */ memCnt = 0 /* The write cursors opened by WHERE_ONEPASS */ aToOpen = uintptr(0) /* The PRIMARY KEY index on the table */ iPk = 0 /* First of nPk registers holding PRIMARY KEY value */ nPk = int16(1) /* Number of memory cells in the row key */ iEphCur = 0 /* Ephemeral table holding all primary key values */ iRowSet = 0 /* Register for rowset of rows to delete */ addrBypass = 0 /* Address of jump over the delete logic */ addrLoop = 0 /* Top of the delete loop */ addrEphOpen = 0 /* List of table triggers, if required */ libc.Xmemset(tls, bp+8, 0, uint32(8)) db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto delete_from_cleanup } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Locate the table which we want to delete. This table has to be ** put in an SrcList structure because some of the subroutines we ** will be calling are designed to work with multiple tables and expect ** an SrcList* parameter instead of just a Table* parameter. */ pTab = _sqlite3SrcListLookup(tls, pParse, pTabList) if pTab == uintptr(0) { goto delete_from_cleanup } /* Figure out if we have any triggers and if the table being ** deleted from is a view */ pTrigger = _sqlite3TriggersExist(tls, pParse, pTab, int32(TK_DELETE), uintptr(0), uintptr(0)) isView = libc.BoolInt32(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW)) bComplex = libc.BoolInt32(pTrigger != 0 || _sqlite3FkRequired(tls, pParse, pTab, uintptr(0), 0) != 0) /* If pTab is really a view, make sure it has been initialized. */ if _sqlite3ViewGetColumnNames(tls, pParse, pTab) != 0 { goto delete_from_cleanup } if _sqlite3IsReadOnly(tls, pParse, pTab, pTrigger) != 0 { goto delete_from_cleanup } iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) _ = libc.Int32FromInt32(0) rcauth = _sqlite3AuthCheck(tls, pParse, int32(SQLITE_DELETE), (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0), (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName) _ = libc.Int32FromInt32(0) if rcauth == int32(SQLITE_DENY) { goto delete_from_cleanup } _ = libc.Int32FromInt32(0) /* Assign cursor numbers to the table and all its indices. */ _ = libc.Int32FromInt32(0) v3 = pParse + 40 v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ v1 = v2 (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FiCursor = v1 iTabCur = v1 nIdx = 0 pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0) { break } (*TParse)(unsafe.Pointer(pParse)).FnTab++ goto _4 _4: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext nIdx++ } /* Start the view context */ if isView != 0 { _sqlite3AuthContextPush(tls, pParse, bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName) } /* Begin generating code. */ v = _sqlite3GetVdbe(tls, pParse) if v == uintptr(0) { goto delete_from_cleanup } if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 { _sqlite3VdbeCountChanges(tls, v) } _sqlite3BeginWriteOperation(tls, pParse, bComplex, iDb) /* If we are trying to delete from a view, realize that view into ** an ephemeral table. */ if isView != 0 { _sqlite3MaterializeView(tls, pParse, pTab, pWhere, pOrderBy, pLimit, iTabCur) v5 = iTabCur *(*int32)(unsafe.Pointer(bp + 4)) = v5 *(*int32)(unsafe.Pointer(bp)) = v5 pOrderBy = uintptr(0) pLimit = uintptr(0) } /* Resolve the column names in the WHERE clause. */ libc.Xmemset(tls, bp+16, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp + 16))).FpParse = pParse (*(*TNameContext)(unsafe.Pointer(bp + 16))).FpSrcList = pTabList if _sqlite3ResolveExprNames(tls, bp+16, pWhere) != 0 { goto delete_from_cleanup } /* Initialize the counter of the number of rows deleted, if ** we are counting rows. */ if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00001))<>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) && !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { if memCnt != 0 { v10 = memCnt } else { v10 = -int32(1) } _sqlite3VdbeAddOp3(tls, v, int32(OP_Clear), int32((*TIndex)(unsafe.Pointer(pIdx)).Ftnum), iDb, v10) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Clear), int32((*TIndex)(unsafe.Pointer(pIdx)).Ftnum), iDb) } goto _9 _9: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } } else { wcf = uint16(libc.Int32FromInt32(WHERE_ONEPASS_DESIRED) | libc.Int32FromInt32(WHERE_DUPLICATES_OK)) if (*(*TNameContext)(unsafe.Pointer(bp + 16))).FncFlags&int32(NC_Subquery) != 0 { bComplex = int32(1) } if bComplex != 0 { v11 = 0 } else { v11 = int32(WHERE_ONEPASS_MULTIROW) } wcf = Tu16(int32(wcf) | v11) if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { /* For a rowid table, initialize the RowSet to an empty set */ pPk = uintptr(0) _ = libc.Int32FromInt32(0) v13 = pParse + 44 *(*int32)(unsafe.Pointer(v13))++ v12 = *(*int32)(unsafe.Pointer(v13)) iRowSet = v12 _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, iRowSet) } else { /* For a WITHOUT ROWID table, create an ephemeral table used to ** hold all primary keys for rows to be deleted. */ pPk = _sqlite3PrimaryKeyIndex(tls, pTab) _ = libc.Int32FromInt32(0) nPk = int16((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol) iPk = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += int32(nPk) v15 = pParse + 40 v14 = *(*int32)(unsafe.Pointer(v15)) *(*int32)(unsafe.Pointer(v15))++ iEphCur = v14 addrEphOpen = _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), iEphCur, int32(nPk)) _sqlite3VdbeSetP4KeyInfo(tls, pParse, pPk) } /* Construct a query to find the rowid or primary key for every row ** to be deleted, based on the WHERE clause. Set variable eOnePass ** to indicate the strategy used to implement this delete: ** ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values. ** ONEPASS_SINGLE: One-pass approach - at most one row deleted. ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted. */ pWInfo = _sqlite3WhereBegin(tls, pParse, pTabList, pWhere, uintptr(0), uintptr(0), uintptr(0), wcf, iTabCur+int32(1)) if pWInfo == uintptr(0) { goto delete_from_cleanup } eOnePass = _sqlite3WhereOkOnePass(tls, pWInfo, bp+56) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if eOnePass != int32(ONEPASS_SINGLE) { _sqlite3MultiWrite(tls, pParse) } if _sqlite3WhereUsesDeferredSeek(tls, pWInfo) != 0 { _sqlite3VdbeAddOp1(tls, v, int32(OP_FinishSeek), iTabCur) } /* Keep track of the number of rows to be deleted */ if memCnt != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), memCnt, int32(1)) } /* Extract the rowid or primary key for the current row */ if pPk != 0 { i = 0 for { if !(i < int32(nPk)) { break } _ = libc.Int32FromInt32(0) _sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iTabCur, int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2))), iPk+i) goto _16 _16: ; i++ } iKey = iPk } else { v18 = pParse + 44 *(*int32)(unsafe.Pointer(v18))++ v17 = *(*int32)(unsafe.Pointer(v18)) iKey = v17 _sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iTabCur, -int32(1), iKey) } if eOnePass != ONEPASS_OFF { /* For ONEPASS, no need to store the rowid/primary-key. There is only ** one, so just keep it in its register(s) and fall through to the ** delete code. */ nKey = nPk /* OP_Found will use an unpacked key */ aToOpen = _sqlite3DbMallocRawNN(tls, db, uint64(nIdx+int32(2))) if aToOpen == uintptr(0) { _sqlite3WhereEnd(tls, pWInfo) goto delete_from_cleanup } libc.Xmemset(tls, aToOpen, int32(1), uint32(nIdx+int32(1))) *(*Tu8)(unsafe.Pointer(aToOpen + uintptr(nIdx+int32(1)))) = uint8(0) if (*(*[2]int32)(unsafe.Pointer(bp + 56)))[0] >= 0 { *(*Tu8)(unsafe.Pointer(aToOpen + uintptr((*(*[2]int32)(unsafe.Pointer(bp + 56)))[0]-iTabCur))) = uint8(0) } if (*(*[2]int32)(unsafe.Pointer(bp + 56)))[int32(1)] >= 0 { *(*Tu8)(unsafe.Pointer(aToOpen + uintptr((*(*[2]int32)(unsafe.Pointer(bp + 56)))[int32(1)]-iTabCur))) = uint8(0) } if addrEphOpen != 0 { _sqlite3VdbeChangeToNoop(tls, v, addrEphOpen) } addrBypass = _sqlite3VdbeMakeLabel(tls, pParse) } else { if pPk != 0 { /* Add the PK key for this row to the temporary table */ v20 = pParse + 44 *(*int32)(unsafe.Pointer(v20))++ v19 = *(*int32)(unsafe.Pointer(v20)) iKey = v19 nKey = 0 /* Zero tells OP_Found to use a composite key */ _sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), iPk, int32(nPk), iKey, _sqlite3IndexAffinityStr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pPk), int32(nPk)) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iEphCur, iKey, iPk, int32(nPk)) } else { /* Add the rowid of the row to be deleted to the RowSet */ nKey = int16(1) /* OP_DeferredSeek always uses a single rowid */ _sqlite3VdbeAddOp2(tls, v, int32(OP_RowSetAdd), iRowSet, iKey) } _sqlite3WhereEnd(tls, pWInfo) } /* Unless this is a view, open cursors for the table we are ** deleting from and all its indices. If this is a view, then the ** only effect this statement has is to fire the INSTEAD OF ** triggers. */ if !(isView != 0) { iAddrOnce = 0 if eOnePass == int32(ONEPASS_MULTI) { iAddrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once)) } _sqlite3OpenTableAndIndices(tls, pParse, pTab, int32(OP_OpenWrite), uint8(OPFLAG_FORDELETE), iTabCur, aToOpen, bp, bp+4) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if eOnePass == int32(ONEPASS_MULTI) { _sqlite3VdbeJumpHereOrPopInst(tls, v, iAddrOnce) } } /* Set up a loop over the rowids/primary-keys that were found in the ** where-clause loop above. */ if eOnePass != ONEPASS_OFF { _ = libc.Int32FromInt32(0) /* OP_Found will use an unpacked key */ if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) && *(*Tu8)(unsafe.Pointer(aToOpen + uintptr(*(*int32)(unsafe.Pointer(bp))-iTabCur))) != 0 { _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), *(*int32)(unsafe.Pointer(bp)), addrBypass, iKey, int32(nKey)) } } else { if pPk != 0 { addrLoop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), iEphCur) if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iEphCur, 0, iKey) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_RowData), iEphCur, iKey) } _ = libc.Int32FromInt32(0) /* OP_Found will use a composite key */ } else { addrLoop = _sqlite3VdbeAddOp3(tls, v, int32(OP_RowSetRead), iRowSet, 0, iKey) _ = libc.Int32FromInt32(0) } } /* Delete the row */ if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { pVTab = _sqlite3GetVTable(tls, db, pTab) _sqlite3VtabMakeWritable(tls, pParse, pTab) _ = libc.Int32FromInt32(0) _sqlite3MayAbort(tls, pParse) if eOnePass == int32(ONEPASS_SINGLE) { _sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iTabCur) if (*TParse)(unsafe.Pointer(pParse)).FpToplevel == uintptr(0) { (*TParse)(unsafe.Pointer(pParse)).FisMultiWrite = uint8(0) } } _sqlite3VdbeAddOp4(tls, v, int32(OP_VUpdate), 0, int32(1), iKey, pVTab, -int32(11)) _sqlite3VdbeChangeP5(tls, v, uint16(OE_Abort)) } else { count = libc.BoolInt32(int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0) /* True to count changes */ _sqlite3GenerateRowDelete(tls, pParse, pTab, pTrigger, *(*int32)(unsafe.Pointer(bp)), *(*int32)(unsafe.Pointer(bp + 4)), iKey, nKey, uint8(count), uint8(OE_Default), uint8(eOnePass), (*(*[2]int32)(unsafe.Pointer(bp + 56)))[int32(1)]) } /* End of the loop over all rowids/primary-keys. */ if eOnePass != ONEPASS_OFF { _sqlite3VdbeResolveLabel(tls, v, addrBypass) _sqlite3WhereEnd(tls, pWInfo) } else { if pPk != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iEphCur, addrLoop+int32(1)) _sqlite3VdbeJumpHere(tls, v, addrLoop) } else { _sqlite3VdbeGoto(tls, v, addrLoop) _sqlite3VdbeJumpHere(tls, v, addrLoop) } } } /* End non-truncate path */ /* Update the sqlite_sequence table by storing the content of the ** maximum rowid counter values recorded while inserting into ** autoincrement tables. */ if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 && (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab == uintptr(0) { _sqlite3AutoincrementEnd(tls, pParse) } /* Return the number of rows that were deleted. If this routine is ** generating code because of a call to sqlite3NestedParse(), do not ** invoke the callback function. */ if memCnt != 0 { _sqlite3CodeChangeCount(tls, v, memCnt, __ccgo_ts+15209) } goto delete_from_cleanup delete_from_cleanup: ; _sqlite3AuthContextPop(tls, bp+8) _sqlite3SrcListDelete(tls, db, pTabList) _sqlite3ExprDelete(tls, db, pWhere) if aToOpen != 0 { _sqlite3DbNNFreeNN(tls, db, aToOpen) } return } /* Make sure "isView" and other macros defined above are undefined. Otherwise ** they may interfere with compilation of other functions in this file ** (or in another file, if this file becomes part of the amalgamation). */ // C documentation // // /* // ** This routine generates VDBE code that causes a single row of a // ** single table to be deleted. Both the original table entry and // ** all indices are removed. // ** // ** Preconditions: // ** // ** 1. iDataCur is an open cursor on the btree that is the canonical data // ** store for the table. (This will be either the table itself, // ** in the case of a rowid table, or the PRIMARY KEY index in the case // ** of a WITHOUT ROWID table.) // ** // ** 2. Read/write cursors for all indices of pTab must be open as // ** cursor number iIdxCur+i for the i-th index. // ** // ** 3. The primary key for the row to be deleted must be stored in a // ** sequence of nPk memory cells starting at iPk. If nPk==0 that means // ** that a search record formed from OP_MakeRecord is contained in the // ** single memory location iPk. // ** // ** eMode: // ** Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or // ** ONEPASS_MULTI. If eMode is not ONEPASS_OFF, then the cursor // ** iDataCur already points to the row to delete. If eMode is ONEPASS_OFF // ** then this function must seek iDataCur to the entry identified by iPk // ** and nPk before reading from it. // ** // ** If eMode is ONEPASS_MULTI, then this call is being made as part // ** of a ONEPASS delete that affects multiple rows. In this case, if // ** iIdxNoSeek is a valid cursor number (>=0) and is not the same as // ** iDataCur, then its position should be preserved following the delete // ** operation. Or, if iIdxNoSeek is not a valid cursor number, the // ** position of iDataCur should be preserved instead. // ** // ** iIdxNoSeek: // ** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur, // ** then it identifies an index cursor (from within array of cursors // ** starting at iIdxCur) that already points to the index entry to be deleted. // ** Except, this optimization is disabled if there are BEFORE triggers since // ** the trigger body might have moved the cursor. // */ func _sqlite3GenerateRowDelete(tls *libc.TLS, pParse uintptr, pTab uintptr, pTrigger uintptr, iDataCur int32, iIdxCur int32, iPk int32, nPk Ti16, count Tu8, onconf Tu8, eMode Tu8, iIdxNoSeek int32) { var addrStart, iCol, iLabel, iOld, kk, v1, v3 int32 var mask Tu32 var opSeek, p5 Tu8 var v uintptr _, _, _, _, _, _, _, _, _, _, _ = addrStart, iCol, iLabel, iOld, kk, mask, opSeek, p5, v, v1, v3 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* Vdbe */ iOld = 0 /* Seek opcode */ /* Vdbe is guaranteed to have been allocated by this stage. */ _ = libc.Int32FromInt32(0) /* Seek cursor iCur to the row to delete. If this row no longer exists ** (this can happen if a trigger program has already deleted it), do ** not attempt to delete it or fire any DELETE triggers. */ iLabel = _sqlite3VdbeMakeLabel(tls, pParse) if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { v1 = int32(OP_NotExists) } else { v1 = int32(OP_NotFound) } opSeek = uint8(v1) if int32(eMode) == ONEPASS_OFF { _sqlite3VdbeAddOp4Int(tls, v, int32(opSeek), iDataCur, iLabel, iPk, int32(nPk)) } /* If there are any triggers to fire, allocate a range of registers to ** use for the old.* references in the triggers. */ if _sqlite3FkRequired(tls, pParse, pTab, uintptr(0), 0) != 0 || pTrigger != 0 { /* Start of BEFORE trigger programs */ /* TODO: Could use temporary registers here. Also could attempt to ** avoid copying the contents of the rowid register. */ mask = _sqlite3TriggerColmask(tls, pParse, pTrigger, uintptr(0), 0, libc.Int32FromInt32(TRIGGER_BEFORE)|libc.Int32FromInt32(TRIGGER_AFTER), pTab, int32(onconf)) mask |= _sqlite3FkOldmask(tls, pParse, pTab) iOld = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += int32(1) + int32((*TTable)(unsafe.Pointer(pTab)).FnCol) /* Populate the OLD.* pseudo-table register array. These values will be ** used by any BEFORE and AFTER triggers that exist. */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), iPk, iOld) iCol = 0 for { if !(iCol < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if mask == uint32(0xffffffff) || iCol <= int32(31) && mask&(libc.Uint32FromInt32(1)<= 0 && iIdxNoSeek != iDataCur { _sqlite3VdbeAddOp1(tls, v, int32(OP_Delete), iIdxNoSeek) } if int32(eMode) == int32(ONEPASS_MULTI) { p5 = Tu8(int32(p5) | libc.Int32FromInt32(OPFLAG_SAVEPOSITION)) } _sqlite3VdbeChangeP5(tls, v, uint16(p5)) } /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to ** handle rows (possibly in other tables) that refer via a foreign key ** to the row just deleted. */ _sqlite3FkActions(tls, pParse, pTab, uintptr(0), iOld, uintptr(0), 0) /* Invoke AFTER DELETE trigger programs. */ if pTrigger != 0 { _sqlite3CodeRowTrigger(tls, pParse, pTrigger, int32(TK_DELETE), uintptr(0), int32(TRIGGER_AFTER), pTab, iOld, int32(onconf), iLabel) } /* Jump here if the row had already been deleted before any BEFORE ** trigger programs were invoked. Or if a trigger program throws a ** RAISE(IGNORE) exception. */ _sqlite3VdbeResolveLabel(tls, v, iLabel) } // C documentation // // /* // ** This routine generates VDBE code that causes the deletion of all // ** index entries associated with a single row of a single table, pTab // ** // ** Preconditions: // ** // ** 1. A read/write cursor "iDataCur" must be open on the canonical storage // ** btree for the table pTab. (This will be either the table itself // ** for rowid tables or to the primary key index for WITHOUT ROWID // ** tables.) // ** // ** 2. Read/write cursors for all indices of pTab must be open as // ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex // ** index is the 0-th index.) // ** // ** 3. The "iDataCur" cursor must be already be positioned on the row // ** that is to be deleted. // */ func _sqlite3GenerateRowIndexDelete(tls *libc.TLS, pParse uintptr, pTab uintptr, iDataCur int32, iIdxCur int32, aRegIdx uintptr, iIdxNoSeek int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, r1, v3 int32 var pIdx, pPk, pPrior, v, v1 uintptr var _ /* iPartIdxLabel at bp+0 */ int32 _, _, _, _, _, _, _, _ = i, pIdx, pPk, pPrior, r1, v, v1, v3 /* Index loop counter */ r1 = -int32(1) /* Current index */ pPrior = uintptr(0) /* PRIMARY KEY index, or NULL for rowid tables */ v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { v1 = uintptr(0) } else { v1 = _sqlite3PrimaryKeyIndex(tls, pTab) } pPk = v1 i = 0 pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0) { break } _ = libc.Int32FromInt32(0) if aRegIdx != uintptr(0) && *(*int32)(unsafe.Pointer(aRegIdx + uintptr(i)*4)) == 0 { goto _2 } if pIdx == pPk { goto _2 } if iIdxCur+i == iIdxNoSeek { goto _2 } r1 = _sqlite3GenerateIndexKey(tls, pParse, pIdx, iDataCur, 0, int32(1), bp, pPrior, r1) if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x8>>3)) != 0 { v3 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) } else { v3 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) } _sqlite3VdbeAddOp3(tls, v, int32(OP_IdxDelete), iIdxCur+i, r1, v3) _sqlite3VdbeChangeP5(tls, v, uint16(1)) /* Cause IdxDelete to error if no entry found */ _sqlite3ResolvePartIdxLabel(tls, pParse, *(*int32)(unsafe.Pointer(bp))) pPrior = pIdx goto _2 _2: ; i++ pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } } // C documentation // // /* // ** Generate code that will assemble an index key and stores it in register // ** regOut. The key with be for index pIdx which is an index on pTab. // ** iCur is the index of a cursor open on the pTab table and pointing to // ** the entry that needs indexing. If pTab is a WITHOUT ROWID table, then // ** iCur must be the cursor of the PRIMARY KEY index. // ** // ** Return a register number which is the first in a block of // ** registers that holds the elements of the index key. The // ** block of registers has already been deallocated by the time // ** this routine returns. // ** // ** If *piPartIdxLabel is not NULL, fill it in with a label and jump // ** to that label if pIdx is a partial index that should be skipped. // ** The label should be resolved using sqlite3ResolvePartIdxLabel(). // ** A partial index should be skipped if its WHERE clause evaluates // ** to false or null. If pIdx is not a partial index, *piPartIdxLabel // ** will be set to zero which is an empty label that is ignored by // ** sqlite3ResolvePartIdxLabel(). // ** // ** The pPrior and regPrior parameters are used to implement a cache to // ** avoid unnecessary register loads. If pPrior is not NULL, then it is // ** a pointer to a different index for which an index key has just been // ** computed into register regPrior. If the current pIdx index is generating // ** its key into the same sequence of registers and if pPrior and pIdx share // ** a column in common, then the register corresponding to that column already // ** holds the correct value and the loading of that register is skipped. // ** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK // ** on a table with multiple indices, and especially with the ROWID or // ** PRIMARY KEY columns of the index. // */ func _sqlite3GenerateIndexKey(tls *libc.TLS, pParse uintptr, pIdx uintptr, iDataCur int32, regOut int32, prefixOnly int32, piPartIdxLabel uintptr, pPrior uintptr, regPrior int32) (r int32) { var j, nCol, regBase, v1 int32 var v uintptr _, _, _, _, _ = j, nCol, regBase, v, v1 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe if piPartIdxLabel != 0 { if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 { *(*int32)(unsafe.Pointer(piPartIdxLabel)) = _sqlite3VdbeMakeLabel(tls, pParse) (*TParse)(unsafe.Pointer(pParse)).FiSelfTab = iDataCur + int32(1) _sqlite3ExprIfFalseDup(tls, pParse, (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere, *(*int32)(unsafe.Pointer(piPartIdxLabel)), int32(SQLITE_JUMPIFNULL)) (*TParse)(unsafe.Pointer(pParse)).FiSelfTab = 0 pPrior = uintptr(0) /* Ticket a9efb42811fa41ee 2019-11-02; ** pPartIdxWhere may have corrupted regPrior registers */ } else { *(*int32)(unsafe.Pointer(piPartIdxLabel)) = 0 } } if prefixOnly != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x8>>3)) != 0 { v1 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) } else { v1 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) } nCol = v1 regBase = _sqlite3GetTempRange(tls, pParse, nCol) if pPrior != 0 && (regBase != regPrior || (*TIndex)(unsafe.Pointer(pPrior)).FpPartIdxWhere != 0) { pPrior = uintptr(0) } j = 0 for { if !(j < nCol) { break } if pPrior != 0 && int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPrior)).FaiColumn + uintptr(j)*2))) == int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j)*2))) && int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPrior)).FaiColumn + uintptr(j)*2))) != -int32(2) { /* This column was already computed by the previous index */ goto _2 } _sqlite3ExprCodeLoadIndexColumn(tls, pParse, pIdx, iDataCur, j, regBase+j) if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j)*2))) >= 0 { /* If the column affinity is REAL but the number is an integer, then it ** might be stored in the table as an integer (using a compact ** representation) then converted to REAL by an OP_RealAffinity opcode. ** But we are getting ready to store this value back into an index, where ** it should be converted by to INTEGER again. So omit the ** OP_RealAffinity opcode if it is present */ _sqlite3VdbeDeletePriorOpcode(tls, v, uint8(OP_RealAffinity)) } goto _2 _2: ; j++ } if regOut != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regBase, nCol, regOut) } _sqlite3ReleaseTempRange(tls, pParse, regBase, nCol) return regBase } // C documentation // // /* // ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label // ** because it was a partial index, then this routine should be called to // ** resolve that label. // */ func _sqlite3ResolvePartIdxLabel(tls *libc.TLS, pParse uintptr, iLabel int32) { if iLabel != 0 { _sqlite3VdbeResolveLabel(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, iLabel) } } /************** End of delete.c **********************************************/ /************** Begin file func.c ********************************************/ /* ** 2002 February 23 ** ** 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 contains the C-language implementations for many of the SQL ** functions of SQLite. (Some function, and in particular the date and ** time functions, are implemented separately.) */ /* #include "sqliteInt.h" */ /* #include */ /* #include */ /* #include */ /* #include "vdbeInt.h" */ // C documentation // // /* // ** Return the collating function associated with a function. // */ func _sqlite3GetFuncCollSeq(tls *libc.TLS, context uintptr) (r uintptr) { var pOp uintptr _ = pOp _ = libc.Int32FromInt32(0) pOp = (*TVdbe)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(context)).FpVdbe)).FaOp + uintptr((*Tsqlite3_context)(unsafe.Pointer(context)).FiOp-int32(1))*20 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return *(*uintptr)(unsafe.Pointer(pOp + 16)) } // C documentation // // /* // ** Indicate that the accumulator load should be skipped on this // ** iteration of the aggregate loop. // */ func _sqlite3SkipAccumulatorLoad(tls *libc.TLS, context uintptr) { _ = libc.Int32FromInt32(0) (*Tsqlite3_context)(unsafe.Pointer(context)).FisError = -int32(1) (*Tsqlite3_context)(unsafe.Pointer(context)).FskipFlag = uint8(1) } // C documentation // // /* // ** Implementation of the non-aggregate min() and max() functions // */ func _minmaxFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var i, iBest, mask, v1 int32 var pColl uintptr _, _, _, _, _ = i, iBest, mask, pColl, v1 _ = libc.Int32FromInt32(0) if Xsqlite3_user_data(tls, context) == uintptr(0) { v1 = 0 } else { v1 = -int32(1) } mask = v1 pColl = _sqlite3GetFuncCollSeq(tls, context) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) iBest = 0 if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) == int32(SQLITE_NULL) { return } i = int32(1) for { if !(i < argc) { break } if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) == int32(SQLITE_NULL) { return } if _sqlite3MemCompare(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(iBest)*4)), *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4)), pColl)^mask >= 0 { iBest = i } goto _2 _2: ; i++ } Xsqlite3_result_value(tls, context, *(*uintptr)(unsafe.Pointer(argv + uintptr(iBest)*4))) } // C documentation // // /* // ** Return the type of the argument. // */ func _typeofFunc(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) { var i int32 _ = i i = Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) - int32(1) _ = NotUsed _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns ** the datatype code for the initial datatype of the sqlite3_value object ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT, ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */ Xsqlite3_result_text(tls, context, _azType2[i], -int32(1), libc.UintptrFromInt32(0)) } var _azType2 = [5]uintptr{ 0: __ccgo_ts + 6306, 1: __ccgo_ts + 6301, 2: __ccgo_ts + 8197, 3: __ccgo_ts + 8192, 4: __ccgo_ts + 1634, } // C documentation // // /* subtype(X) // ** // ** Return the subtype of X // */ func _subtypeFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { _ = argc Xsqlite3_result_int(tls, context, int32(Xsqlite3_value_subtype(tls, *(*uintptr)(unsafe.Pointer(argv))))) } // C documentation // // /* // ** Implementation of the length() function // */ func _lengthFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var c, v1 uint8 var z, z0 uintptr _, _, _, _ = c, z, z0, v1 _ = libc.Int32FromInt32(0) _ = argc switch Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) { case int32(SQLITE_BLOB): fallthrough case int32(SQLITE_INTEGER): fallthrough case int32(SQLITE_FLOAT): Xsqlite3_result_int(tls, context, Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv)))) case int32(SQLITE_TEXT): z = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) if z == uintptr(0) { return } z0 = z for { v1 = *(*uint8)(unsafe.Pointer(z)) c = v1 if !(int32(v1) != 0) { break } z++ if int32(c) >= int32(0xc0) { for int32(*(*uint8)(unsafe.Pointer(z)))&int32(0xc0) == int32(0x80) { z++ z0++ } } } Xsqlite3_result_int(tls, context, int32(z)-int32(z0)) default: Xsqlite3_result_null(tls, context) break } } // C documentation // // /* // ** Implementation of the octet_length() function // */ func _bytelengthFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var m Ti64 var v1 int32 _, _ = m, v1 _ = libc.Int32FromInt32(0) _ = argc switch Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) { case int32(SQLITE_BLOB): Xsqlite3_result_int(tls, context, Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv)))) case int32(SQLITE_INTEGER): fallthrough case int32(SQLITE_FLOAT): if int32((*Tsqlite3)(unsafe.Pointer(Xsqlite3_context_db_handle(tls, context))).Fenc) <= int32(SQLITE_UTF8) { v1 = int32(1) } else { v1 = int32(2) } m = int64(v1) Xsqlite3_result_int64(tls, context, int64(Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv))))*m) case int32(SQLITE_TEXT): if Xsqlite3_value_encoding(tls, *(*uintptr)(unsafe.Pointer(argv))) <= int32(SQLITE_UTF8) { Xsqlite3_result_int(tls, context, Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv)))) } else { Xsqlite3_result_int(tls, context, Xsqlite3_value_bytes16(tls, *(*uintptr)(unsafe.Pointer(argv)))) } default: Xsqlite3_result_null(tls, context) break } } // C documentation // // /* // ** Implementation of the abs() function. // ** // ** IMP: R-23979-26855 The abs(X) function returns the absolute value of // ** the numeric argument X. // */ func _absFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var iVal Ti64 var rVal float64 _, _ = iVal, rVal _ = libc.Int32FromInt32(0) _ = argc switch Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) { case int32(SQLITE_INTEGER): iVal = Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv))) if iVal < 0 { if iVal == int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)< 0 { if typeHaystack == int32(SQLITE_BLOB) && typeNeedle == int32(SQLITE_BLOB) { zHaystack = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv))) zNeedle = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) isText = 0 } else { if typeHaystack != int32(SQLITE_BLOB) && typeNeedle != int32(SQLITE_BLOB) { zHaystack = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) zNeedle = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) isText = int32(1) } else { pC1 = Xsqlite3_value_dup(tls, *(*uintptr)(unsafe.Pointer(argv))) zHaystack = Xsqlite3_value_text(tls, pC1) if zHaystack == uintptr(0) { goto endInstrOOM } nHaystack = Xsqlite3_value_bytes(tls, pC1) pC2 = Xsqlite3_value_dup(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) zNeedle = Xsqlite3_value_text(tls, pC2) if zNeedle == uintptr(0) { goto endInstrOOM } nNeedle = Xsqlite3_value_bytes(tls, pC2) isText = int32(1) } } if zNeedle == uintptr(0) || nHaystack != 0 && zHaystack == uintptr(0) { goto endInstrOOM } firstChar = *(*uint8)(unsafe.Pointer(zNeedle)) for nNeedle <= nHaystack && (int32(*(*uint8)(unsafe.Pointer(zHaystack))) != int32(firstChar) || libc.Xmemcmp(tls, zHaystack, zNeedle, uint32(nNeedle)) != 0) { N++ for cond := true; cond; cond = isText != 0 && int32(*(*uint8)(unsafe.Pointer(zHaystack)))&int32(0xc0) == int32(0x80) { nHaystack-- zHaystack++ } } if nNeedle > nHaystack { N = 0 } } Xsqlite3_result_int(tls, context, N) goto endInstr endInstr: ; Xsqlite3_value_free(tls, pC1) Xsqlite3_value_free(tls, pC2) return goto endInstrOOM endInstrOOM: ; Xsqlite3_result_error_nomem(tls, context) goto endInstr } // C documentation // // /* // ** Implementation of the printf() (a.k.a. format()) SQL function. // */ func _printfFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var db, zFormat, v1 uintptr var n int32 var v2 bool var _ /* str at bp+12 */ TStrAccum var _ /* x at bp+0 */ TPrintfArguments _, _, _, _, _ = db, n, zFormat, v1, v2 db = Xsqlite3_context_db_handle(tls, context) if v2 = argc >= int32(1); v2 { v1 = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) zFormat = v1 } if v2 && v1 != uintptr(0) { (*(*TPrintfArguments)(unsafe.Pointer(bp))).FnArg = argc - int32(1) (*(*TPrintfArguments)(unsafe.Pointer(bp))).FnUsed = 0 (*(*TPrintfArguments)(unsafe.Pointer(bp))).FapArg = argv + uintptr(1)*4 _sqlite3StrAccumInit(tls, bp+12, db, uintptr(0), 0, *(*int32)(unsafe.Pointer(db + 120))) (*(*TStrAccum)(unsafe.Pointer(bp + 12))).FprintfFlags = uint8(SQLITE_PRINTF_SQLFUNC) Xsqlite3_str_appendf(tls, bp+12, zFormat, libc.VaList(bp+48, bp)) n = int32((*(*TStrAccum)(unsafe.Pointer(bp + 12))).FnChar) Xsqlite3_result_text(tls, context, _sqlite3StrAccumFinish(tls, bp+12), n, __ccgo_fp(_sqlite3OomClear)) } } // C documentation // // /* // ** Implementation of the substr() function. // ** // ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. // ** p1 is 1-indexed. So substr(x,1,1) returns the first character // ** of x. If x is text, then we actually count UTF-8 characters. // ** If x is a blob, then we count bytes. // ** // ** If p1 is negative, then we begin abs(p1) from the end of x[]. // ** // ** If p2 is negative, return the p2 characters preceding p1. // */ func _substrFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var len1, negP2, p0type int32 var p1, p2 Ti64 var z, z2, v2, v3, v5 uintptr _, _, _, _, _, _, _, _, _, _ = len1, negP2, p0type, p1, p2, z, z2, v2, v3, v5 negP2 = 0 _ = libc.Int32FromInt32(0) if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) == int32(SQLITE_NULL) || argc == int32(3) && Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4))) == int32(SQLITE_NULL) { return } p0type = Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) p1 = int64(Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4)))) if p0type == int32(SQLITE_BLOB) { len1 = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv))) z = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv))) if z == uintptr(0) { return } _ = libc.Int32FromInt32(0) } else { z = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) if z == uintptr(0) { return } len1 = 0 if p1 < 0 { z2 = z for { if !(*(*uint8)(unsafe.Pointer(z2)) != 0) { break } v2 = z2 z2++ if int32(*(*uint8)(unsafe.Pointer(v2))) >= int32(0xc0) { for int32(*(*uint8)(unsafe.Pointer(z2)))&int32(0xc0) == int32(0x80) { z2++ } } goto _1 _1: ; len1++ } } } if argc == int32(3) { p2 = int64(Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4)))) if p2 < 0 { p2 = -p2 negP2 = int32(1) } } else { p2 = int64(*(*int32)(unsafe.Pointer(Xsqlite3_context_db_handle(tls, context) + 120))) } if p1 < 0 { p1 += int64(len1) if p1 < 0 { p2 += p1 if p2 < 0 { p2 = 0 } p1 = 0 } } else { if p1 > 0 { p1-- } else { if p2 > 0 { p2-- } } } if negP2 != 0 { p1 -= p2 if p1 < 0 { p2 += p1 p1 = 0 } } _ = libc.Int32FromInt32(0) if p0type != int32(SQLITE_BLOB) { for *(*uint8)(unsafe.Pointer(z)) != 0 && p1 != 0 { v3 = z z++ if int32(*(*uint8)(unsafe.Pointer(v3))) >= int32(0xc0) { for int32(*(*uint8)(unsafe.Pointer(z)))&int32(0xc0) == int32(0x80) { z++ } } p1-- } z2 = z for { if !(*(*uint8)(unsafe.Pointer(z2)) != 0 && p2 != 0) { break } v5 = z2 z2++ if int32(*(*uint8)(unsafe.Pointer(v5))) >= int32(0xc0) { for int32(*(*uint8)(unsafe.Pointer(z2)))&int32(0xc0) == int32(0x80) { z2++ } } goto _4 _4: ; p2-- } Xsqlite3_result_text64(tls, context, z, uint64(int32(z2)-int32(z)), uintptr(-libc.Int32FromInt32(1)), uint8(SQLITE_UTF8)) } else { if p1+p2 > int64(len1) { p2 = int64(len1) - p1 if p2 < 0 { p2 = 0 } } Xsqlite3_result_blob64(tls, context, z+uintptr(p1), uint64(p2), uintptr(-libc.Int32FromInt32(1))) } } // C documentation // // /* // ** Implementation of the round() function // */ func _roundFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var n int32 var zBuf uintptr var v1 float64 var _ /* r at bp+0 */ float64 _, _, _ = n, zBuf, v1 n = 0 _ = libc.Int32FromInt32(0) if argc == int32(2) { if int32(SQLITE_NULL) == Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) { return } n = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) if n > int32(30) { n = int32(30) } if n < 0 { n = 0 } } if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) == int32(SQLITE_NULL) { return } *(*float64)(unsafe.Pointer(bp)) = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv))) /* If Y==0 and X will fit in a 64-bit int, ** handle the rounding directly, ** otherwise use printf. */ if *(*float64)(unsafe.Pointer(bp)) < -libc.Float64FromFloat64(4.503599627370496e+15) || *(*float64)(unsafe.Pointer(bp)) > +libc.Float64FromFloat64(4.503599627370496e+15) { /* The value has no fractional part so there is nothing to round */ } else { if n == 0 { if *(*float64)(unsafe.Pointer(bp)) < libc.Float64FromInt32(0) { v1 = -libc.Float64FromFloat64(0.5) } else { v1 = +libc.Float64FromFloat64(0.5) } *(*float64)(unsafe.Pointer(bp)) = float64(int64(*(*float64)(unsafe.Pointer(bp)) + v1)) } else { zBuf = Xsqlite3_mprintf(tls, __ccgo_ts+15239, libc.VaList(bp+16, n, *(*float64)(unsafe.Pointer(bp)))) if zBuf == uintptr(0) { Xsqlite3_result_error_nomem(tls, context) return } _sqlite3AtoF(tls, zBuf, bp, _sqlite3Strlen30(tls, zBuf), uint8(SQLITE_UTF8)) Xsqlite3_free(tls, zBuf) } } Xsqlite3_result_double(tls, context, *(*float64)(unsafe.Pointer(bp))) } // C documentation // // /* // ** Allocate nByte bytes of space using sqlite3Malloc(). If the // ** allocation fails, call sqlite3_result_error_nomem() to notify // ** the database handle that malloc() has failed and return NULL. // ** If nByte is larger than the maximum string or blob length, then // ** raise an SQLITE_TOOBIG exception and return NULL. // */ func _contextMalloc(tls *libc.TLS, context uintptr, nByte Ti64) (r uintptr) { var db, z uintptr _, _ = db, z db = Xsqlite3_context_db_handle(tls, context) _ = libc.Int32FromInt32(0) if nByte > int64(*(*int32)(unsafe.Pointer(db + 120))) { Xsqlite3_result_error_toobig(tls, context) z = uintptr(0) } else { z = _sqlite3Malloc(tls, uint64(nByte)) if !(z != 0) { Xsqlite3_result_error_nomem(tls, context) } } return z } // C documentation // // /* // ** Implementation of the upper() and lower() SQL functions. // */ func _upperFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var i, n int32 var z1, z2 uintptr _, _, _, _ = i, n, z1, z2 _ = argc z2 = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) n = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv))) /* Verify that the call to _bytes() does not invalidate the _text() pointer */ _ = libc.Int32FromInt32(0) if z2 != 0 { z1 = _contextMalloc(tls, context, int64(n)+int64(1)) if z1 != 0 { i = 0 for { if !(i < n) { break } *(*uint8)(unsafe.Pointer(z1 + uintptr(i))) = uint8(int32(*(*uint8)(unsafe.Pointer(z2 + uintptr(i)))) & ^(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z2 + uintptr(i)))]) & libc.Int32FromInt32(0x20))) goto _1 _1: ; i++ } Xsqlite3_result_text(tls, context, z1, n, __ccgo_fp(Xsqlite3_free)) } } } func _lowerFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var i, n int32 var z1, z2 uintptr _, _, _, _ = i, n, z1, z2 _ = argc z2 = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) n = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv))) /* Verify that the call to _bytes() does not invalidate the _text() pointer */ _ = libc.Int32FromInt32(0) if z2 != 0 { z1 = _contextMalloc(tls, context, int64(n)+int64(1)) if z1 != 0 { i = 0 for { if !(i < n) { break } *(*uint8)(unsafe.Pointer(z1 + uintptr(i))) = _sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(z2 + uintptr(i)))] goto _1 _1: ; i++ } Xsqlite3_result_text(tls, context, z1, n, __ccgo_fp(Xsqlite3_free)) } } } /* ** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented ** as VDBE code so that unused argument values do not have to be computed. ** However, we still need some kind of function implementation for this ** routines in the function table. The noopFunc macro provides this. ** noopFunc will never be called so it doesn't matter what the implementation ** is. We might as well use the "version()" function as a substitute. */ // C documentation // // /* // ** Implementation of random(). Return a random integer. // */ func _randomFunc(tls *libc.TLS, context uintptr, NotUsed int32, NotUsed2 uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* r at bp+0 */ Tsqlite_int64 _ = NotUsed _ = NotUsed2 Xsqlite3_randomness(tls, int32(8), bp) if *(*Tsqlite_int64)(unsafe.Pointer(bp)) < 0 { /* We need to prevent a random number of 0x8000000000000000 ** (or -9223372036854775808) since when you do abs() of that ** number of you get the same value back again. To do this ** in a way that is testable, mask the sign bit off of negative ** values, resulting in a positive value. Then take the ** 2s complement of that positive value. The end result can ** therefore be no less than -9223372036854775807. */ *(*Tsqlite_int64)(unsafe.Pointer(bp)) = -(*(*Tsqlite_int64)(unsafe.Pointer(bp)) & (libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)<= int32(0xc0) { for int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))))&int32(0xc0) == int32(0x80) { *(*uintptr)(unsafe.Pointer(bp + 4))++ } } } return int32(SQLITE_NOWILDCARDMATCH) } } } /* At this point variable c contains the first character of the ** pattern string past the "*". Search in the input string for the ** first matching character and recursively continue the match from ** that point. ** ** For a case-insensitive search, set variable cx to be the same as ** c but in the other case and search the input string for either ** c or cx. */ if c < uint32(0x80) { if noCase != 0 { (*(*[3]uint8)(unsafe.Pointer(bp + 9)))[0] = uint8(c & uint32(^(int32(_sqlite3CtypeMap[uint8(c)]) & libc.Int32FromInt32(0x20)))) (*(*[3]uint8)(unsafe.Pointer(bp + 9)))[int32(1)] = _sqlite3UpperToLower[uint8(c)] (*(*[3]uint8)(unsafe.Pointer(bp + 9)))[int32(2)] = uint8(0) } else { (*(*[3]uint8)(unsafe.Pointer(bp + 9)))[0] = uint8(c) (*(*[3]uint8)(unsafe.Pointer(bp + 9)))[int32(1)] = uint8(0) } for int32(1) != 0 { *(*uintptr)(unsafe.Pointer(bp + 4)) += uintptr(libc.Xstrcspn(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), bp+9)) if int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4))))) == 0 { break } *(*uintptr)(unsafe.Pointer(bp + 4))++ bMatch1 = _patternCompare(tls, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 4)), pInfo, matchOther) if bMatch1 != int32(SQLITE_NOMATCH) { return bMatch1 } } } else { for { if int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4))))) < int32(0x80) { v10 = *(*uintptr)(unsafe.Pointer(bp + 4)) *(*uintptr)(unsafe.Pointer(bp + 4))++ v9 = uint32(*(*Tu8)(unsafe.Pointer(v10))) } else { v9 = _sqlite3Utf8Read(tls, bp+4) } v8 = v9 c2 = v8 if !(v8 != uint32(0)) { break } if c2 != c { continue } bMatch2 = _patternCompare(tls, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 4)), pInfo, matchOther) if bMatch2 != int32(SQLITE_NOMATCH) { return bMatch2 } } } return int32(SQLITE_NOWILDCARDMATCH) } if c == matchOther { if int32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchSet) == 0 { c = _sqlite3Utf8Read(tls, bp) if c == uint32(0) { return int32(SQLITE_NOMATCH) } zEscaped = *(*uintptr)(unsafe.Pointer(bp)) } else { prior_c = uint32(0) seen = 0 invert = 0 c = _sqlite3Utf8Read(tls, bp+4) if c == uint32(0) { return int32(SQLITE_NOMATCH) } c2 = _sqlite3Utf8Read(tls, bp) if c2 == uint32('^') { invert = int32(1) c2 = _sqlite3Utf8Read(tls, bp) } if c2 == uint32(']') { if c == uint32(']') { seen = int32(1) } c2 = _sqlite3Utf8Read(tls, bp) } for c2 != 0 && c2 != uint32(']') { if c2 == uint32('-') && int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp))))) != int32(']') && int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp))))) != 0 && prior_c > uint32(0) { c2 = _sqlite3Utf8Read(tls, bp) if c >= prior_c && c <= c2 { seen = int32(1) } prior_c = uint32(0) } else { if c == c2 { seen = int32(1) } prior_c = c2 } c2 = _sqlite3Utf8Read(tls, bp) } if c2 == uint32(0) || seen^invert == 0 { return int32(SQLITE_NOMATCH) } continue } } if int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4))))) < int32(0x80) { v12 = *(*uintptr)(unsafe.Pointer(bp + 4)) *(*uintptr)(unsafe.Pointer(bp + 4))++ v11 = uint32(*(*Tu8)(unsafe.Pointer(v12))) } else { v11 = _sqlite3Utf8Read(tls, bp+4) } c2 = v11 if c == c2 { continue } if noCase != 0 && int32(_sqlite3UpperToLower[uint8(c)]) == int32(_sqlite3UpperToLower[uint8(c2)]) && c < uint32(0x80) && c2 < uint32(0x80) { continue } if c == matchOne && *(*uintptr)(unsafe.Pointer(bp)) != zEscaped && c2 != uint32(0) { continue } return int32(SQLITE_NOMATCH) } if int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4))))) == 0 { v13 = SQLITE_MATCH } else { v13 = int32(SQLITE_NOMATCH) } return v13 } // C documentation // // /* // ** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and // ** non-zero if there is no match. // */ func Xsqlite3_strglob(tls *libc.TLS, zGlobPattern uintptr, zString uintptr) (r int32) { if zString == uintptr(0) { return libc.BoolInt32(zGlobPattern != uintptr(0)) } else { if zGlobPattern == uintptr(0) { return int32(1) } else { return _patternCompare(tls, zGlobPattern, zString, uintptr(unsafe.Pointer(&_globInfo)), uint32('[')) } } return r } // C documentation // // /* // ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for // ** a miss - like strcmp(). // */ func Xsqlite3_strlike(tls *libc.TLS, zPattern uintptr, zStr uintptr, esc uint32) (r int32) { if zStr == uintptr(0) { return libc.BoolInt32(zPattern != uintptr(0)) } else { if zPattern == uintptr(0) { return int32(1) } else { return _patternCompare(tls, zPattern, zStr, uintptr(unsafe.Pointer(&_likeInfoNorm)), esc) } } return r } /* ** Count the number of times that the LIKE operator (or GLOB which is ** just a variation of LIKE) gets called. This is used for testing ** only. */ // C documentation // // /* // ** Implementation of the like() SQL function. This function implements // ** the built-in LIKE operator. The first argument to the function is the // ** pattern and the second argument is the string. So, the SQL statements: // ** // ** A LIKE B // ** // ** is implemented as like(B,A). // ** // ** This same function (with a different compareInfo structure) computes // ** the GLOB operator. // */ func _likeFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, pInfo, zA, zB uintptr var escape Tu32 var nPat int32 var _ /* backupInfo at bp+0 */ TcompareInfo var _ /* zEsc at bp+4 */ uintptr _, _, _, _, _, _ = db, escape, nPat, pInfo, zA, zB db = Xsqlite3_context_db_handle(tls, context) pInfo = Xsqlite3_user_data(tls, context) if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) == int32(SQLITE_BLOB) || Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) == int32(SQLITE_BLOB) { Xsqlite3_result_int(tls, context, 0) return } /* Limit the length of the LIKE or GLOB pattern to avoid problems ** of deep recursion and N*N behavior in patternCompare(). */ nPat = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv))) if nPat > *(*int32)(unsafe.Pointer(db + 120 + 8*4)) { Xsqlite3_result_error(tls, context, __ccgo_ts+15245, -int32(1)) return } if argc == int32(3) { /* The escape character string must consist of a single UTF-8 character. ** Otherwise, return an error. */ *(*uintptr)(unsafe.Pointer(bp + 4)) = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4))) if *(*uintptr)(unsafe.Pointer(bp + 4)) == uintptr(0) { return } if _sqlite3Utf8CharLen(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), -int32(1)) != int32(1) { Xsqlite3_result_error(tls, context, __ccgo_ts+15278, -int32(1)) return } escape = _sqlite3Utf8Read(tls, bp+4) if escape == uint32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchAll) || escape == uint32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchOne) { libc.Xmemcpy(tls, bp, pInfo, uint32(4)) pInfo = bp if escape == uint32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchAll) { (*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchAll = uint8(0) } if escape == uint32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchOne) { (*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchOne = uint8(0) } } } else { escape = uint32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchSet) } zB = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) zA = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) if zA != 0 && zB != 0 { Xsqlite3_result_int(tls, context, libc.BoolInt32(_patternCompare(tls, zB, zA, pInfo, escape) == SQLITE_MATCH)) } } // C documentation // // /* // ** Implementation of the NULLIF(x,y) function. The result is the first // ** argument if the arguments are different. The result is NULL if the // ** arguments are equal to each other. // */ func _nullifFunc(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) { var pColl uintptr _ = pColl pColl = _sqlite3GetFuncCollSeq(tls, context) _ = NotUsed if _sqlite3MemCompare(tls, *(*uintptr)(unsafe.Pointer(argv)), *(*uintptr)(unsafe.Pointer(argv + 1*4)), pColl) != 0 { Xsqlite3_result_value(tls, context, *(*uintptr)(unsafe.Pointer(argv))) } } // C documentation // // /* // ** Implementation of the sqlite_version() function. The result is the version // ** of the SQLite library that is running. // */ func _versionFunc(tls *libc.TLS, context uintptr, NotUsed int32, NotUsed2 uintptr) { _ = NotUsed _ = NotUsed2 /* IMP: R-48699-48617 This function is an SQL wrapper around the ** sqlite3_libversion() C-interface. */ Xsqlite3_result_text(tls, context, Xsqlite3_libversion(tls), -int32(1), libc.UintptrFromInt32(0)) } // C documentation // // /* // ** Implementation of the sqlite_source_id() function. The result is a string // ** that identifies the particular version of the source code used to build // ** SQLite. // */ func _sourceidFunc(tls *libc.TLS, context uintptr, NotUsed int32, NotUsed2 uintptr) { _ = NotUsed _ = NotUsed2 /* IMP: R-24470-31136 This function is an SQL wrapper around the ** sqlite3_sourceid() C interface. */ Xsqlite3_result_text(tls, context, Xsqlite3_sourceid(tls), -int32(1), libc.UintptrFromInt32(0)) } // C documentation // // /* // ** Implementation of the sqlite_log() function. This is a wrapper around // ** sqlite3_log(). The return value is NULL. The function exists purely for // ** its side-effects. // */ func _errlogFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(16) defer tls.Free(16) _ = argc _ = context Xsqlite3_log(tls, Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv))), __ccgo_ts+3795, libc.VaList(bp+8, Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))))) } // C documentation // // /* // ** Implementation of the sqlite_compileoption_used() function. // ** The result is an integer that identifies if the compiler option // ** was used to build SQLite. // */ func _compileoptionusedFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var zOptName, v1 uintptr _, _ = zOptName, v1 _ = libc.Int32FromInt32(0) _ = argc /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL ** function is a wrapper around the sqlite3_compileoption_used() C/C++ ** function. */ v1 = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) zOptName = v1 if v1 != uintptr(0) { Xsqlite3_result_int(tls, context, Xsqlite3_compileoption_used(tls, zOptName)) } } // C documentation // // /* // ** Implementation of the sqlite_compileoption_get() function. // ** The result is a string that identifies the compiler options // ** used to build SQLite. // */ func _compileoptiongetFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var n int32 _ = n _ = libc.Int32FromInt32(0) _ = argc /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function ** is a wrapper around the sqlite3_compileoption_get() C/C++ function. */ n = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv))) Xsqlite3_result_text(tls, context, Xsqlite3_compileoption_get(tls, n), -int32(1), libc.UintptrFromInt32(0)) } // C documentation // // /* Array for converting from half-bytes (nybbles) into ASCII hex // ** digits. */ var _hexdigits = [16]uint8{ 0: uint8('0'), 1: uint8('1'), 2: uint8('2'), 3: uint8('3'), 4: uint8('4'), 5: uint8('5'), 6: uint8('6'), 7: uint8('7'), 8: uint8('8'), 9: uint8('9'), 10: uint8('A'), 11: uint8('B'), 12: uint8('C'), 13: uint8('D'), 14: uint8('E'), 15: uint8('F'), } // C documentation // // /* // ** Append to pStr text that is the SQL literal representation of the // ** value contained in pValue. // */ func _sqlite3QuoteValue(tls *libc.TLS, pStr uintptr, pValue uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var i int32 var nBlob Ti64 var r1 float64 var zArg, zBlob, zText, zVal uintptr var _ /* r2 at bp+0 */ float64 _, _, _, _, _, _, _ = i, nBlob, r1, zArg, zBlob, zText, zVal /* As currently implemented, the string must be initially empty. ** we might relax this requirement in the future, but that will ** require enhancements to the implementation. */ _ = libc.Int32FromInt32(0) switch Xsqlite3_value_type(tls, pValue) { case int32(SQLITE_FLOAT): r1 = Xsqlite3_value_double(tls, pValue) Xsqlite3_str_appendf(tls, pStr, __ccgo_ts+15323, libc.VaList(bp+16, r1)) zVal = Xsqlite3_str_value(tls, pStr) if zVal != 0 { _sqlite3AtoF(tls, zVal, bp, int32((*TStrAccum)(unsafe.Pointer(pStr)).FnChar), uint8(SQLITE_UTF8)) if r1 != *(*float64)(unsafe.Pointer(bp)) { Xsqlite3_str_reset(tls, pStr) Xsqlite3_str_appendf(tls, pStr, __ccgo_ts+15331, libc.VaList(bp+16, r1)) } } case int32(SQLITE_INTEGER): Xsqlite3_str_appendf(tls, pStr, __ccgo_ts+1404, libc.VaList(bp+16, Xsqlite3_value_int64(tls, pValue))) case int32(SQLITE_BLOB): zBlob = Xsqlite3_value_blob(tls, pValue) nBlob = int64(Xsqlite3_value_bytes(tls, pValue)) _ = libc.Int32FromInt32(0) /* No encoding change */ _sqlite3StrAccumEnlarge(tls, pStr, nBlob*int64(2)+int64(4)) if int32((*TStrAccum)(unsafe.Pointer(pStr)).FaccError) == 0 { zText = (*TStrAccum)(unsafe.Pointer(pStr)).FzText i = 0 for { if !(int64(i) < nBlob) { break } *(*uint8)(unsafe.Pointer(zText + uintptr(i*int32(2)+int32(2)))) = _hexdigits[int32(*(*uint8)(unsafe.Pointer(zBlob + uintptr(i))))>>int32(4)&int32(0x0F)] *(*uint8)(unsafe.Pointer(zText + uintptr(i*int32(2)+int32(3)))) = _hexdigits[int32(*(*uint8)(unsafe.Pointer(zBlob + uintptr(i))))&int32(0x0F)] goto _1 _1: ; i++ } *(*uint8)(unsafe.Pointer(zText + uintptr(nBlob*int64(2)+int64(2)))) = uint8('\'') *(*uint8)(unsafe.Pointer(zText + uintptr(nBlob*int64(2)+int64(3)))) = uint8('\000') *(*uint8)(unsafe.Pointer(zText)) = uint8('X') *(*uint8)(unsafe.Pointer(zText + 1)) = uint8('\'') (*TStrAccum)(unsafe.Pointer(pStr)).FnChar = uint32(nBlob*int64(2) + int64(3)) } case int32(SQLITE_TEXT): zArg = Xsqlite3_value_text(tls, pValue) Xsqlite3_str_appendf(tls, pStr, __ccgo_ts+15339, libc.VaList(bp+16, zArg)) default: _ = libc.Int32FromInt32(0) Xsqlite3_str_append(tls, pStr, __ccgo_ts+1649, int32(4)) break } } // C documentation // // /* // ** Implementation of the QUOTE() function. // ** // ** The quote(X) function returns the text of an SQL literal which is the // ** value of its argument suitable for inclusion into an SQL statement. // ** Strings are surrounded by single-quotes with escapes on interior quotes // ** as needed. BLOBs are encoded as hexadecimal literals. Strings with // ** embedded NUL characters cannot be represented as string literals in SQL // ** and hence the returned string literal is truncated prior to the first NUL. // */ func _quoteFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var db uintptr var _ /* str at bp+0 */ Tsqlite3_str _ = db db = Xsqlite3_context_db_handle(tls, context) _ = libc.Int32FromInt32(0) _ = argc _sqlite3StrAccumInit(tls, bp, db, uintptr(0), 0, *(*int32)(unsafe.Pointer(db + 120))) _sqlite3QuoteValue(tls, bp, *(*uintptr)(unsafe.Pointer(argv))) Xsqlite3_result_text(tls, context, _sqlite3StrAccumFinish(tls, bp), int32((*(*Tsqlite3_str)(unsafe.Pointer(bp))).FnChar), __ccgo_fp(_sqlite3OomClear)) if int32((*(*Tsqlite3_str)(unsafe.Pointer(bp))).FaccError) != SQLITE_OK { Xsqlite3_result_null(tls, context) Xsqlite3_result_error_code(tls, context, int32((*(*Tsqlite3_str)(unsafe.Pointer(bp))).FaccError)) } } // C documentation // // /* // ** The unicode() function. Return the integer unicode code-point value // ** for the first character of the input string. // */ func _unicodeFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* z at bp+0 */ uintptr *(*uintptr)(unsafe.Pointer(bp)) = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) _ = argc if *(*uintptr)(unsafe.Pointer(bp)) != 0 && *(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))) != 0 { Xsqlite3_result_int(tls, context, int32(_sqlite3Utf8Read(tls, bp))) } } // C documentation // // /* // ** The char() function takes zero or more arguments, each of which is // ** an integer. It constructs a string where each character of the string // ** is the unicode character for the corresponding integer argument. // */ func _charFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var c uint32 var i int32 var x Tsqlite3_int64 var z, zOut, v1, v10, v11, v12, v3, v4, v5, v6, v7, v8, v9 uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, i, x, z, zOut, v1, v10, v11, v12, v3, v4, v5, v6, v7, v8, v9 v1 = Xsqlite3_malloc64(tls, uint64(argc*int32(4)+int32(1))) z = v1 zOut = v1 if z == uintptr(0) { Xsqlite3_result_error_nomem(tls, context) return } i = 0 for { if !(i < argc) { break } x = Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) if x < 0 || x > int64(0x10ffff) { x = int64(0xfffd) } c = uint32(x & libc.Int64FromInt32(0x1fffff)) if c < uint32(0x00080) { v3 = zOut zOut++ *(*uint8)(unsafe.Pointer(v3)) = uint8(c & libc.Uint32FromInt32(0xFF)) } else { if c < uint32(0x00800) { v4 = zOut zOut++ *(*uint8)(unsafe.Pointer(v4)) = uint8(int32(0xC0) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x1F)))) v5 = zOut zOut++ *(*uint8)(unsafe.Pointer(v5)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F)))) } else { if c < uint32(0x10000) { v6 = zOut zOut++ *(*uint8)(unsafe.Pointer(v6)) = uint8(int32(0xE0) + int32(uint8(c>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x0F)))) v7 = zOut zOut++ *(*uint8)(unsafe.Pointer(v7)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F)))) v8 = zOut zOut++ *(*uint8)(unsafe.Pointer(v8)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F)))) } else { v9 = zOut zOut++ *(*uint8)(unsafe.Pointer(v9)) = uint8(int32(0xF0) + int32(uint8(c>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x07)))) v10 = zOut zOut++ *(*uint8)(unsafe.Pointer(v10)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x3F)))) v11 = zOut zOut++ *(*uint8)(unsafe.Pointer(v11)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F)))) v12 = zOut zOut++ *(*uint8)(unsafe.Pointer(v12)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F)))) } } } goto _2 _2: ; i++ } *(*uint8)(unsafe.Pointer(zOut)) = uint8(0) Xsqlite3_result_text64(tls, context, z, uint64(int32(zOut)-int32(z)), __ccgo_fp(Xsqlite3_free), uint8(SQLITE_UTF8)) } // C documentation // // /* // ** The hex() function. Interpret the argument as a blob. Return // ** a hexadecimal rendering as text. // */ func _hexFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var c uint8 var i, n int32 var pBlob, z, zHex, v1, v3, v4 uintptr _, _, _, _, _, _, _, _, _ = c, i, n, pBlob, z, zHex, v1, v3, v4 _ = libc.Int32FromInt32(0) _ = argc pBlob = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv))) n = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv))) _ = libc.Int32FromInt32(0) /* No encoding change */ v1 = _contextMalloc(tls, context, int64(n)*int64(2)+int64(1)) zHex = v1 z = v1 if zHex != 0 { i = 0 for { if !(i < n) { break } c = *(*uint8)(unsafe.Pointer(pBlob)) v3 = z z++ *(*uint8)(unsafe.Pointer(v3)) = _hexdigits[int32(c)>>int32(4)&int32(0xf)] v4 = z z++ *(*uint8)(unsafe.Pointer(v4)) = _hexdigits[int32(c)&int32(0xf)] goto _2 _2: ; i++ pBlob++ } *(*uint8)(unsafe.Pointer(z)) = uint8(0) Xsqlite3_result_text64(tls, context, zHex, uint64(int32(z)-int32(zHex)), __ccgo_fp(Xsqlite3_free), uint8(SQLITE_UTF8)) } } // C documentation // // /* // ** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr // ** contains character ch, or 0 if it does not. // */ func _strContainsChar(tls *libc.TLS, zStr uintptr, nStr int32, ch Tu32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var tst Tu32 var zEnd, v2 uintptr var v1 uint32 var _ /* z at bp+0 */ uintptr _, _, _, _ = tst, zEnd, v1, v2 zEnd = zStr + uintptr(nStr) *(*uintptr)(unsafe.Pointer(bp)) = zStr for *(*uintptr)(unsafe.Pointer(bp)) < zEnd { if int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp))))) < int32(0x80) { v2 = *(*uintptr)(unsafe.Pointer(bp)) *(*uintptr)(unsafe.Pointer(bp))++ v1 = uint32(*(*Tu8)(unsafe.Pointer(v2))) } else { v1 = _sqlite3Utf8Read(tls, bp) } tst = v1 if tst == ch { return int32(1) } } return 0 } // C documentation // // /* // ** The unhex() function. This function may be invoked with either one or // ** two arguments. In both cases the first argument is interpreted as text // ** a text value containing a set of pairs of hexadecimal digits which are // ** decoded and returned as a blob. // ** // ** If there is only a single argument, then it must consist only of an // ** even number of hexadecimal digits. Otherwise, return NULL. // ** // ** Or, if there is a second argument, then any character that appears in // ** the second argument is also allowed to appear between pairs of hexadecimal // ** digits in the first argument. If any other character appears in the // ** first argument, or if one of the allowed characters appears between // ** two hexadecimal digits that make up a single byte, NULL is returned. // ** // ** The following expressions are all true: // ** // ** unhex('ABCD') IS x'ABCD' // ** unhex('AB CD') IS NULL // ** unhex('AB CD', ' ') IS x'ABCD' // ** unhex('A BCD', ' ') IS NULL // */ func _unhexFunc(tls *libc.TLS, pCtx uintptr, argc int32, argv uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var c, d, v2 Tu8 var ch Tu32 var nHex, nPass int32 var p, pBlob, zPass, v1, v4, v5, v6 uintptr var v3 uint32 var _ /* zHex at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, ch, d, nHex, nPass, p, pBlob, zPass, v1, v2, v3, v4, v5, v6 zPass = __ccgo_ts + 1648 nPass = 0 *(*uintptr)(unsafe.Pointer(bp)) = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) nHex = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv))) pBlob = uintptr(0) p = uintptr(0) _ = libc.Int32FromInt32(0) if argc == int32(2) { zPass = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) nPass = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) } if !(*(*uintptr)(unsafe.Pointer(bp)) != 0) || !(zPass != 0) { return } v1 = _contextMalloc(tls, pCtx, int64(nHex/int32(2)+int32(1))) pBlob = v1 p = v1 if pBlob != 0 { /* Least significant digit of next byte */ for { v2 = *(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))) c = v2 if !(int32(v2) != 0x00) { break } for !(int32(_sqlite3CtypeMap[c])&libc.Int32FromInt32(0x08) != 0) { if int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp))))) < int32(0x80) { v4 = *(*uintptr)(unsafe.Pointer(bp)) *(*uintptr)(unsafe.Pointer(bp))++ v3 = uint32(*(*Tu8)(unsafe.Pointer(v4))) } else { v3 = _sqlite3Utf8Read(tls, bp) } ch = v3 _ = libc.Int32FromInt32(0) if !(_strContainsChar(tls, zPass, nPass, ch) != 0) { goto unhex_null } c = *(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))) if int32(c) == 0x00 { goto unhex_done } } *(*uintptr)(unsafe.Pointer(bp))++ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) v5 = *(*uintptr)(unsafe.Pointer(bp)) *(*uintptr)(unsafe.Pointer(bp))++ d = *(*Tu8)(unsafe.Pointer(v5)) if !(int32(_sqlite3CtypeMap[d])&libc.Int32FromInt32(0x08) != 0) { goto unhex_null } v6 = p p++ *(*Tu8)(unsafe.Pointer(v6)) = uint8(int32(_sqlite3HexToInt(tls, int32(c)))< nPattern { nOut += int64(nRep - nPattern) if nOut-int64(1) > int64(*(*int32)(unsafe.Pointer(db + 120))) { Xsqlite3_result_error_toobig(tls, context) Xsqlite3_free(tls, zOut) return } cntExpand++ if cntExpand&(cntExpand-uint32(1)) == uint32(0) { zOld = zOut zOut = _sqlite3Realloc(tls, zOut, uint64(int64(int32(nOut))+(nOut-int64(nStr)-int64(1)))) if zOut == uintptr(0) { Xsqlite3_result_error_nomem(tls, context) Xsqlite3_free(tls, zOld) return } } } libc.Xmemcpy(tls, zOut+uintptr(j), zRep, uint32(nRep)) j += nRep i += nPattern - int32(1) } goto _1 _1: ; i++ } _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, zOut+uintptr(j), zStr+uintptr(i), uint32(nStr-i)) j += nStr - i _ = libc.Int32FromInt32(0) *(*uint8)(unsafe.Pointer(zOut + uintptr(j))) = uint8(0) Xsqlite3_result_text(tls, context, zOut, j, __ccgo_fp(Xsqlite3_free)) } // C documentation // // /* // ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. // ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. // */ func _trimFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var aLen, azChar, z, zCharSet, zIn, v1, v3, v5 uintptr var flags, i, nChar int32 var len1, len11, nIn uint32 _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aLen, azChar, flags, i, len1, len11, nChar, nIn, z, zCharSet, zIn, v1, v3, v5 /* Loop counter */ aLen = uintptr(0) /* Length of each character in zCharSet */ azChar = uintptr(0) /* Number of characters in zCharSet */ if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) == int32(SQLITE_NULL) { return } zIn = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) if zIn == uintptr(0) { return } nIn = uint32(Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv)))) _ = libc.Int32FromInt32(0) if argc == int32(1) { nChar = int32(1) aLen = uintptr(unsafe.Pointer(&_lenOne)) azChar = uintptr(unsafe.Pointer(&_azOne)) zCharSet = uintptr(0) } else { v1 = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) zCharSet = v1 if v1 == uintptr(0) { return } else { z = zCharSet nChar = libc.Int32FromInt32(0) for { if !(*(*uint8)(unsafe.Pointer(z)) != 0) { break } v3 = z z++ if int32(*(*uint8)(unsafe.Pointer(v3))) >= int32(0xc0) { for int32(*(*uint8)(unsafe.Pointer(z)))&int32(0xc0) == int32(0x80) { z++ } } goto _2 _2: ; nChar++ } if nChar > 0 { azChar = _contextMalloc(tls, context, int64(nChar)*int64(libc.Uint32FromInt64(4)+libc.Uint32FromInt64(4))) if azChar == uintptr(0) { return } aLen = azChar + uintptr(nChar)*4 z = zCharSet nChar = libc.Int32FromInt32(0) for { if !(*(*uint8)(unsafe.Pointer(z)) != 0) { break } *(*uintptr)(unsafe.Pointer(azChar + uintptr(nChar)*4)) = z v5 = z z++ if int32(*(*uint8)(unsafe.Pointer(v5))) >= int32(0xc0) { for int32(*(*uint8)(unsafe.Pointer(z)))&int32(0xc0) == int32(0x80) { z++ } } *(*uint32)(unsafe.Pointer(aLen + uintptr(nChar)*4)) = uint32(int32(z) - int32(*(*uintptr)(unsafe.Pointer(azChar + uintptr(nChar)*4)))) goto _4 _4: ; nChar++ } } } } if nChar > 0 { flags = int32(Xsqlite3_user_data(tls, context)) if flags&int32(1) != 0 { for nIn > uint32(0) { len1 = uint32(0) i = 0 for { if !(i < nChar) { break } len1 = *(*uint32)(unsafe.Pointer(aLen + uintptr(i)*4)) if len1 <= nIn && libc.Xmemcmp(tls, zIn, *(*uintptr)(unsafe.Pointer(azChar + uintptr(i)*4)), len1) == 0 { break } goto _6 _6: ; i++ } if i >= nChar { break } zIn += uintptr(len1) nIn -= len1 } } if flags&int32(2) != 0 { for nIn > uint32(0) { len11 = uint32(0) i = 0 for { if !(i < nChar) { break } len11 = *(*uint32)(unsafe.Pointer(aLen + uintptr(i)*4)) if len11 <= nIn && libc.Xmemcmp(tls, zIn+uintptr(nIn-len11), *(*uintptr)(unsafe.Pointer(azChar + uintptr(i)*4)), len11) == 0 { break } goto _7 _7: ; i++ } if i >= nChar { break } nIn -= len11 } } if zCharSet != 0 { Xsqlite3_free(tls, azChar) } } Xsqlite3_result_text(tls, context, zIn, int32(nIn), uintptr(-libc.Int32FromInt32(1))) } var _lenOne = [1]uint32{ 0: uint32(1), } var _azOne = [1]uintptr{ 0: __ccgo_ts + 11202, } // C documentation // // /* The core implementation of the CONCAT(...) and CONCAT_WS(SEP,...) // ** functions. // ** // ** Return a string value that is the concatenation of all non-null // ** entries in argv[]. Use zSep as the separator. // */ func _concatFuncCore(tls *libc.TLS, context uintptr, argc int32, argv uintptr, nSep int32, zSep uintptr) { var i int32 var j, k, n Ti64 var v, z uintptr _, _, _, _, _, _ = i, j, k, n, v, z n = 0 i = 0 for { if !(i < argc) { break } n += int64(Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4)))) goto _1 _1: ; i++ } n += int64((argc - int32(1)) * nSep) z = Xsqlite3_malloc64(tls, uint64(n+int64(1))) if z == uintptr(0) { Xsqlite3_result_error_nomem(tls, context) return } j = 0 i = 0 for { if !(i < argc) { break } k = int64(Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4)))) if k > 0 { v = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) if v != uintptr(0) { if j > 0 && nSep > 0 { libc.Xmemcpy(tls, z+uintptr(j), zSep, uint32(nSep)) j += int64(nSep) } libc.Xmemcpy(tls, z+uintptr(j), v, uint32(k)) j += k } } goto _2 _2: ; i++ } *(*uint8)(unsafe.Pointer(z + uintptr(j))) = uint8(0) _ = libc.Int32FromInt32(0) Xsqlite3_result_text64(tls, context, z, uint64(j), __ccgo_fp(Xsqlite3_free), uint8(SQLITE_UTF8)) } // C documentation // // /* // ** The CONCAT(...) function. Generate a string result that is the // ** concatentation of all non-null arguments. // */ func _concatFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { _concatFuncCore(tls, context, argc, argv, 0, __ccgo_ts+1648) } // C documentation // // /* // ** The CONCAT_WS(separator, ...) function. // ** // ** Generate a string that is the concatenation of 2nd through the Nth // ** argument. Use the first argument (which must be non-NULL) as the // ** separator. // */ func _concatwsFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var nSep int32 var zSep uintptr _, _ = nSep, zSep nSep = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv))) zSep = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) if zSep == uintptr(0) { return } _concatFuncCore(tls, context, argc-int32(1), argv+uintptr(1)*4, nSep, zSep) } // C documentation // // /* IMP: R-25361-16150 This function is omitted from SQLite by default. It // ** is only available if the SQLITE_SOUNDEX compile-time option is used // ** when SQLite is built. // */ // /* // ** Compute the soundex encoding of a word. // ** // ** IMP: R-59782-00072 The soundex(X) function returns a string that is the // ** soundex encoding of the string X. // */ func _soundexFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var code, i, j, v3, v4 int32 var prevcode Tu8 var zIn uintptr var _ /* zResult at bp+0 */ [8]uint8 _, _, _, _, _, _, _ = code, i, j, prevcode, zIn, v3, v4 _ = libc.Int32FromInt32(0) zIn = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) if zIn == uintptr(0) { zIn = __ccgo_ts + 1648 } i = 0 for { if !(*(*Tu8)(unsafe.Pointer(zIn + uintptr(i))) != 0 && !(int32(_sqlite3CtypeMap[*(*Tu8)(unsafe.Pointer(zIn + uintptr(i)))])&libc.Int32FromInt32(0x02) != 0)) { break } goto _1 _1: ; i++ } if *(*Tu8)(unsafe.Pointer(zIn + uintptr(i))) != 0 { prevcode = _iCode[int32(*(*Tu8)(unsafe.Pointer(zIn + uintptr(i))))&int32(0x7f)] (*(*[8]uint8)(unsafe.Pointer(bp)))[0] = uint8(int32(*(*Tu8)(unsafe.Pointer(zIn + uintptr(i)))) & ^(int32(_sqlite3CtypeMap[*(*Tu8)(unsafe.Pointer(zIn + uintptr(i)))]) & libc.Int32FromInt32(0x20))) j = int32(1) for { if !(j < int32(4) && *(*Tu8)(unsafe.Pointer(zIn + uintptr(i))) != 0) { break } code = int32(_iCode[int32(*(*Tu8)(unsafe.Pointer(zIn + uintptr(i))))&int32(0x7f)]) if code > 0 { if code != int32(prevcode) { prevcode = uint8(code) v3 = j j++ (*(*[8]uint8)(unsafe.Pointer(bp)))[v3] = uint8(code + int32('0')) } } else { prevcode = uint8(0) } goto _2 _2: ; i++ } for j < int32(4) { v4 = j j++ (*(*[8]uint8)(unsafe.Pointer(bp)))[v4] = uint8('0') } (*(*[8]uint8)(unsafe.Pointer(bp)))[j] = uint8(0) Xsqlite3_result_text(tls, context, bp, int32(4), uintptr(-libc.Int32FromInt32(1))) } else { /* IMP: R-64894-50321 The string "?000" is returned if the argument ** is NULL or contains no ASCII alphabetic characters. */ Xsqlite3_result_text(tls, context, __ccgo_ts+15342, int32(4), libc.UintptrFromInt32(0)) } } var _iCode = [128]uint8{ 66: uint8(1), 67: uint8(2), 68: uint8(3), 70: uint8(1), 71: uint8(2), 74: uint8(2), 75: uint8(2), 76: uint8(4), 77: uint8(5), 78: uint8(5), 80: uint8(1), 81: uint8(2), 82: uint8(6), 83: uint8(2), 84: uint8(3), 86: uint8(1), 88: uint8(2), 90: uint8(2), 98: uint8(1), 99: uint8(2), 100: uint8(3), 102: uint8(1), 103: uint8(2), 106: uint8(2), 107: uint8(2), 108: uint8(4), 109: uint8(5), 110: uint8(5), 112: uint8(1), 113: uint8(2), 114: uint8(6), 115: uint8(2), 116: uint8(3), 118: uint8(1), 120: uint8(2), 122: uint8(2), } // C documentation // // /* // ** A function that loads a shared-library extension then returns NULL. // */ func _loadExt(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, zFile, zProc uintptr var _ /* zErrMsg at bp+0 */ uintptr _, _, _ = db, zFile, zProc zFile = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) db = Xsqlite3_context_db_handle(tls, context) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc ** flag is set. See the sqlite3_enable_load_extension() API. */ if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_LoadExtFunc) == uint64(0) { Xsqlite3_result_error(tls, context, __ccgo_ts+12420, -int32(1)) return } if argc == int32(2) { zProc = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) } else { zProc = uintptr(0) } if zFile != 0 && Xsqlite3_load_extension(tls, db, zFile, zProc, bp) != 0 { Xsqlite3_result_error(tls, context, *(*uintptr)(unsafe.Pointer(bp)), -int32(1)) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp))) } } // C documentation // // /* // ** An instance of the following structure holds the context of a // ** sum() or avg() aggregate computation. // */ type TSumCtx = struct { F__ccgo_align [0]uint32 FrSum float64 FrErr float64 FiSum Ti64 Fcnt Ti64 Fapprox Tu8 Fovrfl Tu8 F__ccgo_pad6 [6]byte } type SumCtx = TSumCtx type TSumCtx1 = struct { F__ccgo_align [0]uint32 FrSum float64 FrErr float64 FiSum Ti64 Fcnt Ti64 Fapprox Tu8 Fovrfl Tu8 F__ccgo_pad6 [6]byte } type SumCtx1 = TSumCtx1 // C documentation // // /* // ** Do one step of the Kahan-Babushka-Neumaier summation. // ** // ** https://en.wikipedia.org/wiki/Kahan_summation_algorithm // ** // ** Variables are marked "volatile" to defeat c89 x86 floating point // ** optimizations can mess up this algorithm. // */ func _kahanBabuskaNeumaierStep(tls *libc.TLS, pSum uintptr, r float64) { var s, t float64 _, _ = s, t s = (*TSumCtx)(unsafe.Pointer(pSum)).FrSum t = s + r if libc.Xfabs(tls, s) > libc.Xfabs(tls, r) { *(*float64)(unsafe.Pointer(pSum + 8)) += s - t + r } else { *(*float64)(unsafe.Pointer(pSum + 8)) += r - t + s } (*TSumCtx)(unsafe.Pointer(pSum)).FrSum = t } // C documentation // // /* // ** Add a (possibly large) integer to the running sum. // */ func _kahanBabuskaNeumaierStepInt64(tls *libc.TLS, pSum uintptr, iVal Ti64) { var iBig, iSm Ti64 _, _ = iBig, iSm if iVal <= -int64(4503599627370496) || iVal >= +libc.Int64FromInt64(4503599627370496) { iSm = iVal % int64(16384) iBig = iVal - iSm _kahanBabuskaNeumaierStep(tls, pSum, float64(iBig)) _kahanBabuskaNeumaierStep(tls, pSum, float64(iSm)) } else { _kahanBabuskaNeumaierStep(tls, pSum, float64(iVal)) } } // C documentation // // /* // ** Initialize the Kahan-Babaska-Neumaier sum from a 64-bit integer // */ func _kahanBabuskaNeumaierInit(tls *libc.TLS, p uintptr, iVal Ti64) { var iSm Ti64 _ = iSm if iVal <= -int64(4503599627370496) || iVal >= +libc.Int64FromInt64(4503599627370496) { iSm = iVal % int64(16384) (*TSumCtx)(unsafe.Pointer(p)).FrSum = float64(iVal - iSm) (*TSumCtx)(unsafe.Pointer(p)).FrErr = float64(iSm) } else { (*TSumCtx)(unsafe.Pointer(p)).FrSum = float64(iVal) (*TSumCtx)(unsafe.Pointer(p)).FrErr = float64(0) } } // C documentation // // /* // ** Routines used to compute the sum, average, and total. // ** // ** The SUM() function follows the (broken) SQL standard which means // ** that it returns NULL if it sums over no inputs. TOTAL returns // ** 0.0 in that case. In addition, TOTAL always returns a float where // ** SUM might return an integer if it never encounters a floating point // ** value. TOTAL never fails, but SUM might through an exception if // ** it overflows an integer. // */ func _sumStep(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var p uintptr var type1 int32 var _ /* x at bp+0 */ Ti64 _, _ = p, type1 _ = libc.Int32FromInt32(0) _ = argc p = Xsqlite3_aggregate_context(tls, context, int32(40)) type1 = Xsqlite3_value_numeric_type(tls, *(*uintptr)(unsafe.Pointer(argv))) if p != 0 && type1 != int32(SQLITE_NULL) { (*TSumCtx)(unsafe.Pointer(p)).Fcnt++ if int32((*TSumCtx)(unsafe.Pointer(p)).Fapprox) == 0 { if type1 != int32(SQLITE_INTEGER) { _kahanBabuskaNeumaierInit(tls, p, (*TSumCtx)(unsafe.Pointer(p)).FiSum) (*TSumCtx)(unsafe.Pointer(p)).Fapprox = uint8(1) _kahanBabuskaNeumaierStep(tls, p, Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv)))) } else { *(*Ti64)(unsafe.Pointer(bp)) = (*TSumCtx)(unsafe.Pointer(p)).FiSum if _sqlite3AddInt64(tls, bp, Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv)))) == 0 { (*TSumCtx)(unsafe.Pointer(p)).FiSum = *(*Ti64)(unsafe.Pointer(bp)) } else { (*TSumCtx)(unsafe.Pointer(p)).Fovrfl = uint8(1) _kahanBabuskaNeumaierInit(tls, p, (*TSumCtx)(unsafe.Pointer(p)).FiSum) (*TSumCtx)(unsafe.Pointer(p)).Fapprox = uint8(1) _kahanBabuskaNeumaierStepInt64(tls, p, Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv)))) } } } else { if type1 == int32(SQLITE_INTEGER) { _kahanBabuskaNeumaierStepInt64(tls, p, Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv)))) } else { (*TSumCtx)(unsafe.Pointer(p)).Fovrfl = uint8(0) _kahanBabuskaNeumaierStep(tls, p, Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv)))) } } } } func _sumInverse(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var iVal Ti64 var p uintptr var type1 int32 _, _, _ = iVal, p, type1 _ = libc.Int32FromInt32(0) _ = argc p = Xsqlite3_aggregate_context(tls, context, int32(40)) type1 = Xsqlite3_value_numeric_type(tls, *(*uintptr)(unsafe.Pointer(argv))) /* p is always non-NULL because sumStep() will have been called first ** to initialize it */ if p != 0 && type1 != int32(SQLITE_NULL) { _ = libc.Int32FromInt32(0) (*TSumCtx)(unsafe.Pointer(p)).Fcnt-- if !((*TSumCtx)(unsafe.Pointer(p)).Fapprox != 0) { *(*Ti64)(unsafe.Pointer(p + 16)) -= Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv))) } else { if type1 == int32(SQLITE_INTEGER) { iVal = Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv))) if iVal != int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)< 0 { if (*TSumCtx)(unsafe.Pointer(p)).Fapprox != 0 { if (*TSumCtx)(unsafe.Pointer(p)).Fovrfl != 0 { Xsqlite3_result_error(tls, context, __ccgo_ts+15222, -int32(1)) } else { if !(_sqlite3IsOverflow(tls, (*TSumCtx)(unsafe.Pointer(p)).FrErr) != 0) { Xsqlite3_result_double(tls, context, (*TSumCtx)(unsafe.Pointer(p)).FrSum+(*TSumCtx)(unsafe.Pointer(p)).FrErr) } else { Xsqlite3_result_double(tls, context, (*TSumCtx)(unsafe.Pointer(p)).FrSum) } } } else { Xsqlite3_result_int64(tls, context, (*TSumCtx)(unsafe.Pointer(p)).FiSum) } } } func _avgFinalize(tls *libc.TLS, context uintptr) { var p uintptr var r float64 _, _ = p, r p = Xsqlite3_aggregate_context(tls, context, 0) if p != 0 && (*TSumCtx)(unsafe.Pointer(p)).Fcnt > 0 { if (*TSumCtx)(unsafe.Pointer(p)).Fapprox != 0 { r = (*TSumCtx)(unsafe.Pointer(p)).FrSum if !(_sqlite3IsOverflow(tls, (*TSumCtx)(unsafe.Pointer(p)).FrErr) != 0) { r += (*TSumCtx)(unsafe.Pointer(p)).FrErr } } else { r = float64((*TSumCtx)(unsafe.Pointer(p)).FiSum) } Xsqlite3_result_double(tls, context, r/float64((*TSumCtx)(unsafe.Pointer(p)).Fcnt)) } } func _totalFinalize(tls *libc.TLS, context uintptr) { var p uintptr var r float64 _, _ = p, r r = float64(0) p = Xsqlite3_aggregate_context(tls, context, 0) if p != 0 { if (*TSumCtx)(unsafe.Pointer(p)).Fapprox != 0 { r = (*TSumCtx)(unsafe.Pointer(p)).FrSum if !(_sqlite3IsOverflow(tls, (*TSumCtx)(unsafe.Pointer(p)).FrErr) != 0) { r += (*TSumCtx)(unsafe.Pointer(p)).FrErr } } else { r = float64((*TSumCtx)(unsafe.Pointer(p)).FiSum) } } Xsqlite3_result_double(tls, context, r) } // C documentation // // /* // ** The following structure keeps track of state information for the // ** count() aggregate function. // */ type TCountCtx = struct { F__ccgo_align [0]uint32 Fn Ti64 } type CountCtx = TCountCtx type TCountCtx1 = struct { F__ccgo_align [0]uint32 Fn Ti64 } type CountCtx1 = TCountCtx1 // C documentation // // /* // ** Routines to implement the count() aggregate function. // */ func _countStep(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var p uintptr _ = p p = Xsqlite3_aggregate_context(tls, context, int32(8)) if (argc == 0 || int32(SQLITE_NULL) != Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv)))) && p != 0 { (*TCountCtx)(unsafe.Pointer(p)).Fn++ } /* The sqlite3_aggregate_count() function is deprecated. But just to make ** sure it still operates correctly, verify that its count agrees with our ** internal count when using count(*) and when the total count can be ** expressed as a 32-bit integer. */ _ = libc.Int32FromInt32(0) } func _countFinalize(tls *libc.TLS, context uintptr) { var p uintptr var v1 int64 _, _ = p, v1 p = Xsqlite3_aggregate_context(tls, context, 0) if p != 0 { v1 = (*TCountCtx)(unsafe.Pointer(p)).Fn } else { v1 = 0 } Xsqlite3_result_int64(tls, context, v1) } func _countInverse(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { var p uintptr _ = p p = Xsqlite3_aggregate_context(tls, ctx, int32(8)) /* p is always non-NULL since countStep() will have been called first */ if (argc == 0 || int32(SQLITE_NULL) != Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv)))) && p != 0 { (*TCountCtx)(unsafe.Pointer(p)).Fn-- } } // C documentation // // /* // ** Routines to implement min() and max() aggregate functions. // */ func _minmaxStep(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) { var cmp, max int32 var pArg, pBest, pColl uintptr _, _, _, _, _ = cmp, max, pArg, pBest, pColl pArg = *(*uintptr)(unsafe.Pointer(argv)) _ = NotUsed pBest = Xsqlite3_aggregate_context(tls, context, int32(40)) if !(pBest != 0) { return } if Xsqlite3_value_type(tls, pArg) == int32(SQLITE_NULL) { if (*TMem)(unsafe.Pointer(pBest)).Fflags != 0 { _sqlite3SkipAccumulatorLoad(tls, context) } } else { if (*TMem)(unsafe.Pointer(pBest)).Fflags != 0 { pColl = _sqlite3GetFuncCollSeq(tls, context) /* This step function is used for both the min() and max() aggregates, ** the only difference between the two being that the sense of the ** comparison is inverted. For the max() aggregate, the ** sqlite3_user_data() function returns (void *)-1. For min() it ** returns (void *)db, where db is the sqlite3* database pointer. ** Therefore the next statement sets variable 'max' to 1 for the max() ** aggregate, or 0 for min(). */ max = libc.BoolInt32(Xsqlite3_user_data(tls, context) != uintptr(0)) cmp = _sqlite3MemCompare(tls, pBest, pArg, pColl) if max != 0 && cmp < 0 || !(max != 0) && cmp > 0 { _sqlite3VdbeMemCopy(tls, pBest, pArg) } else { _sqlite3SkipAccumulatorLoad(tls, context) } } else { (*TMem)(unsafe.Pointer(pBest)).Fdb = Xsqlite3_context_db_handle(tls, context) _sqlite3VdbeMemCopy(tls, pBest, pArg) } } } func _minMaxValueFinalize(tls *libc.TLS, context uintptr, bValue int32) { var pRes uintptr _ = pRes pRes = Xsqlite3_aggregate_context(tls, context, 0) if pRes != 0 { if (*Tsqlite3_value)(unsafe.Pointer(pRes)).Fflags != 0 { Xsqlite3_result_value(tls, context, pRes) } if bValue == 0 { _sqlite3VdbeMemRelease(tls, pRes) } } } func _minMaxValue(tls *libc.TLS, context uintptr) { _minMaxValueFinalize(tls, context, int32(1)) } func _minMaxFinalize(tls *libc.TLS, context uintptr) { _minMaxValueFinalize(tls, context, 0) } // C documentation // // /* // ** group_concat(EXPR, ?SEPARATOR?) // ** string_agg(EXPR, SEPARATOR) // ** // ** The SEPARATOR goes before the EXPR string. This is tragic. The // ** groupConcatInverse() implementation would have been easier if the // ** SEPARATOR were appended after EXPR. And the order is undocumented, // ** so we could change it, in theory. But the old behavior has been // ** around for so long that we dare not, for fear of breaking something. // */ type TGroupConcatCtx = struct { Fstr TStrAccum FnAccum int32 FnFirstSepLength int32 FpnSepLengths uintptr } type GroupConcatCtx = TGroupConcatCtx func _groupConcatStep(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var db, pGCC, pnsl, zSep, zVal uintptr var firstTerm, i, nA, nSep, nVal, v1 int32 _, _, _, _, _, _, _, _, _, _, _ = db, firstTerm, i, nA, nSep, nVal, pGCC, pnsl, zSep, zVal, v1 _ = libc.Int32FromInt32(0) if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) == int32(SQLITE_NULL) { return } pGCC = Xsqlite3_aggregate_context(tls, context, int32(36)) if pGCC != 0 { db = Xsqlite3_context_db_handle(tls, context) firstTerm = libc.BoolInt32((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FmxAlloc == uint32(0)) (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FmxAlloc = uint32(*(*int32)(unsafe.Pointer(db + 120))) if argc == int32(1) { if !(firstTerm != 0) { Xsqlite3_str_appendchar(tls, pGCC, int32(1), uint8(',')) } else { (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnFirstSepLength = int32(1) } } else { if !(firstTerm != 0) { zSep = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) nSep = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) if zSep != 0 { Xsqlite3_str_append(tls, pGCC, zSep, nSep) } else { nSep = 0 } if nSep != (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnFirstSepLength || (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths != uintptr(0) { pnsl = (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths if pnsl == uintptr(0) { /* First separator length variation seen, start tracking them. */ pnsl = Xsqlite3_malloc64(tls, uint64(uint32((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum+libc.Int32FromInt32(1))*uint32(4))) if pnsl != uintptr(0) { i = 0 nA = (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum - int32(1) for i < nA { v1 = i i++ *(*int32)(unsafe.Pointer(pnsl + uintptr(v1)*4)) = (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnFirstSepLength } } } else { pnsl = Xsqlite3_realloc64(tls, pnsl, uint64(uint32((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum)*uint32(4))) } if pnsl != uintptr(0) { if (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum > 0 { *(*int32)(unsafe.Pointer(pnsl + uintptr((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum-int32(1))*4)) = nSep } (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths = pnsl } else { _sqlite3StrAccumSetError(tls, pGCC, uint8(SQLITE_NOMEM)) } } } else { (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnFirstSepLength = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) } } *(*int32)(unsafe.Pointer(pGCC + 24)) += int32(1) zVal = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) nVal = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv))) if zVal != 0 { Xsqlite3_str_append(tls, pGCC, zVal, nVal) } } } func _groupConcatInverse(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var nVS int32 var pGCC uintptr _, _ = nVS, pGCC _ = libc.Int32FromInt32(0) _ = argc /* Suppress unused parameter warning */ if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) == int32(SQLITE_NULL) { return } pGCC = Xsqlite3_aggregate_context(tls, context, int32(36)) /* pGCC is always non-NULL since groupConcatStep() will have always ** run first to initialize it */ if pGCC != 0 { /* Must call sqlite3_value_text() to convert the argument into text prior ** to invoking sqlite3_value_bytes(), in case the text encoding is UTF16 */ Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) nVS = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv))) *(*int32)(unsafe.Pointer(pGCC + 24)) -= int32(1) if (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths != uintptr(0) { _ = libc.Int32FromInt32(0) if (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum > 0 { nVS += *(*int32)(unsafe.Pointer((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths)) libc.Xmemmove(tls, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths+uintptr(1)*4, uint32((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum-libc.Int32FromInt32(1))*uint32(4)) } } else { /* If removing single accumulated string, harmlessly over-do. */ nVS += (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnFirstSepLength } if nVS >= int32((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FnChar) { (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FnChar = uint32(0) } else { (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FnChar -= uint32(nVS) libc.Xmemmove(tls, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FzText, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FzText+uintptr(nVS), (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FnChar) } if (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FnChar == uint32(0) { (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FmxAlloc = uint32(0) Xsqlite3_free(tls, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths) (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths = uintptr(0) } } } func _groupConcatFinalize(tls *libc.TLS, context uintptr) { var pGCC uintptr _ = pGCC pGCC = Xsqlite3_aggregate_context(tls, context, 0) if pGCC != 0 { _sqlite3ResultStrAccum(tls, context, pGCC) Xsqlite3_free(tls, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths) } } func _groupConcatValue(tls *libc.TLS, context uintptr) { var pAccum, pGCC, zText uintptr _, _, _ = pAccum, pGCC, zText pGCC = Xsqlite3_aggregate_context(tls, context, 0) if pGCC != 0 { pAccum = pGCC if int32((*TStrAccum)(unsafe.Pointer(pAccum)).FaccError) == int32(SQLITE_TOOBIG) { Xsqlite3_result_error_toobig(tls, context) } else { if int32((*TStrAccum)(unsafe.Pointer(pAccum)).FaccError) == int32(SQLITE_NOMEM) { Xsqlite3_result_error_nomem(tls, context) } else { zText = Xsqlite3_str_value(tls, pAccum) Xsqlite3_result_text(tls, context, zText, int32((*TStrAccum)(unsafe.Pointer(pAccum)).FnChar), uintptr(-libc.Int32FromInt32(1))) } } } } // C documentation // // /* // ** This routine does per-connection function registration. Most // ** of the built-in functions above are part of the global function set. // ** This routine only deals with those that are not global. // */ func _sqlite3RegisterPerConnectionBuiltinFunctions(tls *libc.TLS, db uintptr) { var rc int32 _ = rc rc = Xsqlite3_overload_function(tls, db, __ccgo_ts+15347, int32(2)) _ = libc.Int32FromInt32(0) if rc == int32(SQLITE_NOMEM) { _sqlite3OomFault(tls, db) } } // C documentation // // /* // ** Re-register the built-in LIKE functions. The caseSensitive // ** parameter determines whether or not the LIKE operator is case // ** sensitive. // */ func _sqlite3RegisterLikeFunctions(tls *libc.TLS, db uintptr, caseSensitive int32) { var flags, nArg int32 var pDef, pInfo uintptr _, _, _, _ = flags, nArg, pDef, pInfo if caseSensitive != 0 { pInfo = uintptr(unsafe.Pointer(&_likeInfoAlt)) flags = libc.Int32FromInt32(SQLITE_FUNC_LIKE) | libc.Int32FromInt32(SQLITE_FUNC_CASE) } else { pInfo = uintptr(unsafe.Pointer(&_likeInfoNorm)) flags = int32(SQLITE_FUNC_LIKE) } nArg = int32(2) for { if !(nArg <= int32(3)) { break } _sqlite3CreateFunc(tls, db, __ccgo_ts+15353, nArg, int32(SQLITE_UTF8), pInfo, __ccgo_fp(_likeFunc), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0)) pDef = _sqlite3FindFunction(tls, db, __ccgo_ts+15353, nArg, uint8(SQLITE_UTF8), uint8(0)) *(*Tu32)(unsafe.Pointer(pDef + 4)) |= uint32(flags) *(*Tu32)(unsafe.Pointer(pDef + 4)) &= uint32(^libc.Int32FromInt32(SQLITE_FUNC_UNSAFE)) goto _1 _1: ; nArg++ } } // C documentation // // /* // ** pExpr points to an expression which implements a function. If // ** it is appropriate to apply the LIKE optimization to that function // ** then set aWc[0] through aWc[2] to the wildcard characters and the // ** escape character and then return TRUE. If the function is not a // ** LIKE-style function then return FALSE. // ** // ** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE // ** operator if c is a string literal that is exactly one byte in length. // ** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is // ** no ESCAPE clause. // ** // ** *pIsNocase is set to true if uppercase and lowercase are equivalent for // ** the function (default for LIKE). If the function makes the distinction // ** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to // ** false. // */ func _sqlite3IsLikeFunction(tls *libc.TLS, db uintptr, pExpr uintptr, pIsNocase uintptr, aWc uintptr) (r int32) { var nExpr int32 var pDef, pEscape, zEscape uintptr _, _, _, _ = nExpr, pDef, pEscape, zEscape _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if !(*(*uintptr)(unsafe.Pointer(pExpr + 20)) != 0) { return 0 } nExpr = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr _ = libc.Int32FromInt32(0) pDef = _sqlite3FindFunction(tls, db, *(*uintptr)(unsafe.Pointer(pExpr + 8)), nExpr, uint8(SQLITE_UTF8), uint8(0)) if pDef == uintptr(0) || (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_LIKE) == uint32(0) { return 0 } /* The memcpy() statement assumes that the wildcard characters are ** the first three statements in the compareInfo structure. The ** asserts() that follow verify that assumption */ libc.Xmemcpy(tls, aWc, (*TFuncDef)(unsafe.Pointer(pDef)).FpUserData, uint32(3)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if nExpr < int32(3) { *(*uint8)(unsafe.Pointer(aWc + 3)) = uint8(0) } else { pEscape = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8 + 2*20))).FpExpr if int32((*TExpr)(unsafe.Pointer(pEscape)).Fop) != int32(TK_STRING) { return 0 } _ = libc.Int32FromInt32(0) zEscape = *(*uintptr)(unsafe.Pointer(pEscape + 8)) if int32(*(*uint8)(unsafe.Pointer(zEscape))) == 0 || int32(*(*uint8)(unsafe.Pointer(zEscape + 1))) != 0 { return 0 } if int32(*(*uint8)(unsafe.Pointer(zEscape))) == int32(*(*uint8)(unsafe.Pointer(aWc))) { return 0 } if int32(*(*uint8)(unsafe.Pointer(zEscape))) == int32(*(*uint8)(unsafe.Pointer(aWc + 1))) { return 0 } *(*uint8)(unsafe.Pointer(aWc + 3)) = *(*uint8)(unsafe.Pointer(zEscape)) } *(*int32)(unsafe.Pointer(pIsNocase)) = libc.BoolInt32((*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_CASE) == uint32(0)) return int32(1) } /* Mathematical Constants */ // C documentation // // /* Extra math functions that require linking with -lm // */ // /* // ** Implementation SQL functions: // ** // ** ceil(X) // ** ceiling(X) // ** floor(X) // ** // ** The sqlite3_user_data() pointer is a pointer to the libm implementation // ** of the underlying C function. // */ func _ceilingFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var x uintptr _ = x _ = libc.Int32FromInt32(0) switch Xsqlite3_value_numeric_type(tls, *(*uintptr)(unsafe.Pointer(argv))) { case int32(SQLITE_INTEGER): Xsqlite3_result_int64(tls, context, Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv)))) case int32(SQLITE_FLOAT): x = Xsqlite3_user_data(tls, context) Xsqlite3_result_double(tls, context, (*(*func(*libc.TLS, float64) float64)(unsafe.Pointer(&struct{ uintptr }{x})))(tls, Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv))))) default: break } } // C documentation // // /* // ** On some systems, ceil() and floor() are intrinsic function. You are // ** unable to take a pointer to these functions. Hence, we here wrap them // ** in our own actual functions. // */ func _xCeil(tls *libc.TLS, x float64) (r float64) { return libc.Xceil(tls, x) } func _xFloor(tls *libc.TLS, x float64) (r float64) { return libc.Xfloor(tls, x) } /* ** Some systems do not have log2() and log10() in their standard math ** libraries. */ // C documentation // // /* // ** Implementation of SQL functions: // ** // ** ln(X) - natural logarithm // ** log(X) - log X base 10 // ** log10(X) - log X base 10 // ** log(B,X) - log X base B // */ func _logFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var ans, b, x float64 _, _, _ = ans, b, x _ = libc.Int32FromInt32(0) switch Xsqlite3_value_numeric_type(tls, *(*uintptr)(unsafe.Pointer(argv))) { case int32(SQLITE_INTEGER): fallthrough case int32(SQLITE_FLOAT): x = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv))) if x <= float64(0) { return } default: return } if argc == int32(2) { switch Xsqlite3_value_numeric_type(tls, *(*uintptr)(unsafe.Pointer(argv))) { case int32(SQLITE_INTEGER): fallthrough case int32(SQLITE_FLOAT): b = libc.Xlog(tls, x) if b <= float64(0) { return } x = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) if x <= float64(0) { return } default: return } ans = libc.Xlog(tls, x) / b } else { switch int32(Xsqlite3_user_data(tls, context)) { case int32(1): ans = libc.Xlog10(tls, x) case int32(2): ans = libc.X__builtin_log2(tls, x) default: ans = libc.Xlog(tls, x) break } } Xsqlite3_result_double(tls, context, ans) } // C documentation // // /* // ** Functions to converts degrees to radians and radians to degrees. // */ func _degToRad(tls *libc.TLS, x float64) (r float64) { return x * (libc.Float64FromFloat64(3.141592653589793) / libc.Float64FromFloat64(180)) } func _radToDeg(tls *libc.TLS, x float64) (r float64) { return x * (libc.Float64FromFloat64(180) / libc.Float64FromFloat64(3.141592653589793)) } // C documentation // // /* // ** Implementation of 1-argument SQL math functions: // ** // ** exp(X) - Compute e to the X-th power // */ func _math1Func(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var ans, v0 float64 var type0 int32 var x uintptr _, _, _, _ = ans, type0, v0, x _ = libc.Int32FromInt32(0) type0 = Xsqlite3_value_numeric_type(tls, *(*uintptr)(unsafe.Pointer(argv))) if type0 != int32(SQLITE_INTEGER) && type0 != int32(SQLITE_FLOAT) { return } v0 = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv))) x = Xsqlite3_user_data(tls, context) ans = (*(*func(*libc.TLS, float64) float64)(unsafe.Pointer(&struct{ uintptr }{x})))(tls, v0) Xsqlite3_result_double(tls, context, ans) } // C documentation // // /* // ** Implementation of 2-argument SQL math functions: // ** // ** power(X,Y) - Compute X to the Y-th power // */ func _math2Func(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var ans, v0, v1 float64 var type0, type1 int32 var x uintptr _, _, _, _, _, _ = ans, type0, type1, v0, v1, x _ = libc.Int32FromInt32(0) type0 = Xsqlite3_value_numeric_type(tls, *(*uintptr)(unsafe.Pointer(argv))) if type0 != int32(SQLITE_INTEGER) && type0 != int32(SQLITE_FLOAT) { return } type1 = Xsqlite3_value_numeric_type(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) if type1 != int32(SQLITE_INTEGER) && type1 != int32(SQLITE_FLOAT) { return } v0 = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv))) v1 = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) x = Xsqlite3_user_data(tls, context) ans = (*(*func(*libc.TLS, float64, float64) float64)(unsafe.Pointer(&struct{ uintptr }{x})))(tls, v0, v1) Xsqlite3_result_double(tls, context, ans) } // C documentation // // /* // ** Implementation of 0-argument pi() function. // */ func _piFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { _ = libc.Int32FromInt32(0) _ = argv Xsqlite3_result_double(tls, context, float64(3.141592653589793)) } // C documentation // // /* // ** Implementation of sign(X) function. // */ func _signFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var type0, v1, v2 int32 var x float64 _, _, _, _ = type0, x, v1, v2 _ = argc _ = libc.Int32FromInt32(0) type0 = Xsqlite3_value_numeric_type(tls, *(*uintptr)(unsafe.Pointer(argv))) if type0 != int32(SQLITE_INTEGER) && type0 != int32(SQLITE_FLOAT) { return } x = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv))) if x < float64(0) { v1 = -int32(1) } else { if x > float64(0) { v2 = +libc.Int32FromInt32(1) } else { v2 = 0 } v1 = v2 } Xsqlite3_result_int(tls, context, v1) } // C documentation // // /* // ** All of the FuncDef structures in the aBuiltinFunc[] array above // ** to the global function hash table. This occurs at start-time (as // ** a consequence of calling sqlite3_initialize()). // ** // ** After this routine runs // */ func _sqlite3RegisterBuiltinFunctions(tls *libc.TLS) { _sqlite3AlterFunctions(tls) _sqlite3WindowFunctions(tls) _sqlite3RegisterDateTimeFunctions(tls) _sqlite3RegisterJsonFunctions(tls) _sqlite3InsertBuiltinFuncs(tls, uintptr(unsafe.Pointer(&_aBuiltinFunc)), int32(libc.Uint32FromInt64(4400)/libc.Uint32FromInt64(40))) } /* ** The following array holds FuncDef structures for all of the functions ** defined in this file. ** ** The array cannot be constant since changes are made to the ** FuncDef.pHash elements at start-time. The elements of this array ** are read-only after initialization is complete. ** ** For peak efficiency, put the most frequently used function last. */ var _aBuiltinFunc = [110]TFuncDef{ 0: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INTERNAL) | libc.Int32FromInt32(SQLITE_FUNC_TEST) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)), FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_implies_nonnull_row)), FzName: __ccgo_ts + 15358, }, 1: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INTERNAL) | libc.Int32FromInt32(SQLITE_FUNC_TEST) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)), FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_expr_compare)), FzName: __ccgo_ts + 15378, }, 2: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INTERNAL) | libc.Int32FromInt32(SQLITE_FUNC_TEST) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)), FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_expr_implies_expr)), FzName: __ccgo_ts + 15391, }, 3: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INTERNAL) | libc.Int32FromInt32(SQLITE_FUNC_TEST) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)), FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_affinity)), FzName: __ccgo_ts + 15409, }, 4: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15418, }, 5: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DIRECTONLY) | libc.Int32FromInt32(SQLITE_FUNC_UNSAFE)), FzName: __ccgo_ts + 15426, }, 6: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DIRECTONLY) | libc.Int32FromInt32(SQLITE_FUNC_UNSAFE)), FzName: __ccgo_ts + 15426, }, 7: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15441, }, 8: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15467, }, 9: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_FUNC_UNLIKELY)), FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_unlikely)), FzName: __ccgo_ts + 15492, }, 10: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_FUNC_UNLIKELY)), FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_unlikely)), FzName: __ccgo_ts + 15501, }, 11: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_FUNC_UNLIKELY)), FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_unlikely)), FzName: __ccgo_ts + 15512, }, 12: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)), FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_sqlite_offset)), FzName: __ccgo_ts + 15519, }, 13: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FpUserData: uintptr(libc.Int32FromInt32(1)), FzName: __ccgo_ts + 15533, }, 14: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FpUserData: uintptr(libc.Int32FromInt32(1)), FzName: __ccgo_ts + 15533, }, 15: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FpUserData: uintptr(libc.Int32FromInt32(2)), FzName: __ccgo_ts + 15539, }, 16: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FpUserData: uintptr(libc.Int32FromInt32(2)), FzName: __ccgo_ts + 15539, }, 17: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FpUserData: uintptr(libc.Int32FromInt32(3)), FzName: __ccgo_ts + 15545, }, 18: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FpUserData: uintptr(libc.Int32FromInt32(3)), FzName: __ccgo_ts + 15545, }, 19: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15550, }, 20: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15550, }, 21: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_MINMAX) | libc.Int32FromInt32(SQLITE_FUNC_ANYORDER)), FzName: __ccgo_ts + 15550, }, 22: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FpUserData: uintptr(libc.Int32FromInt32(1)), FzName: __ccgo_ts + 15554, }, 23: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FpUserData: uintptr(libc.Int32FromInt32(1)), FzName: __ccgo_ts + 15554, }, 24: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_MINMAX) | libc.Int32FromInt32(SQLITE_FUNC_ANYORDER)), FpUserData: uintptr(libc.Int32FromInt32(1)), FzName: __ccgo_ts + 15554, }, 25: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_TYPEOF)), FzName: __ccgo_ts + 15558, }, 26: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_TYPEOF)), FzName: __ccgo_ts + 15565, }, 27: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_LENGTH)), FzName: __ccgo_ts + 15573, }, 28: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_BYTELEN)), FzName: __ccgo_ts + 15580, }, 29: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15593, }, 30: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15599, }, 31: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15606, }, 32: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15613, }, 33: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15621, }, 34: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15626, }, 35: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15630, }, 36: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15630, }, 37: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15636, }, 38: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15642, }, 39: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15648, }, 40: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15652, }, 41: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15652, }, 42: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15658, }, 43: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15658, }, 44: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15665, }, 45: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15665, }, 46: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15665, }, 47: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)), FzName: __ccgo_ts + 15675, }, 48: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15682, }, 49: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15689, }, 50: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15700, }, 51: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15707, }, 52: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15722, }, 53: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15739, }, 54: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15750, }, 55: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15756, }, 56: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15774, }, 57: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15782, }, 58: { FnArg: int8(3), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15796, }, 59: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15804, }, 60: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15813, }, 61: { FnArg: int8(3), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15813, }, 62: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15820, }, 63: { FnArg: int8(3), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15820, }, 64: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(0)), FzName: __ccgo_ts + 15830, }, 65: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(0)), FzName: __ccgo_ts + 15834, }, 66: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(0)), FzName: __ccgo_ts + 15840, }, 67: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_COUNT) | libc.Int32FromInt32(SQLITE_FUNC_ANYORDER)), FzName: __ccgo_ts + 15844, }, 68: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_ANYORDER)), FzName: __ccgo_ts + 15844, }, 69: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(0)), FzName: __ccgo_ts + 15850, }, 70: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(0)), FzName: __ccgo_ts + 15850, }, 71: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(0)), FzName: __ccgo_ts + 15863, }, 72: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_LIKE) | libc.Int32FromInt32(SQLITE_FUNC_CASE)), FpUserData: uintptr(unsafe.Pointer(&_globInfo)), FzName: __ccgo_ts + 15874, }, 73: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_LIKE)), FpUserData: uintptr(unsafe.Pointer(&_likeInfoNorm)), FzName: __ccgo_ts + 15353, }, 74: { FnArg: int8(3), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_LIKE)), FpUserData: uintptr(unsafe.Pointer(&_likeInfoNorm)), FzName: __ccgo_ts + 15353, }, 75: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 6730, }, 76: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 6730, }, 77: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15879, }, 78: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15884, }, 79: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15892, }, 80: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15898, }, 81: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15904, }, 82: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FpUserData: uintptr(libc.Int32FromInt32(1)), FzName: __ccgo_ts + 15907, }, 83: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FpUserData: uintptr(libc.Int32FromInt32(1)), FzName: __ccgo_ts + 15911, }, 84: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FpUserData: uintptr(libc.Int32FromInt32(2)), FzName: __ccgo_ts + 15917, }, 85: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 15907, }, 86: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15922, }, 87: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15926, }, 88: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15930, }, 89: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15936, }, 90: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15940, }, 91: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15945, }, 92: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15950, }, 93: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15955, }, 94: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15961, }, 95: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15965, }, 96: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15969, }, 97: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15973, }, 98: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15978, }, 99: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15983, }, 100: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15988, }, 101: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15994, }, 102: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16000, }, 103: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16006, }, 104: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16011, }, 105: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16019, }, 106: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 16027, }, 107: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 16030, }, 108: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)), FzName: __ccgo_ts + 6730, }, 109: { FnArg: int8(3), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)), FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_iif)), FzName: __ccgo_ts + 16035, }, } func init() { p := unsafe.Pointer(&_aBuiltinFunc) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 176)) = __ccgo_fp(_soundexFunc) *(*uintptr)(unsafe.Add(p, 216)) = __ccgo_fp(_loadExt) *(*uintptr)(unsafe.Add(p, 256)) = __ccgo_fp(_loadExt) *(*uintptr)(unsafe.Add(p, 296)) = __ccgo_fp(_compileoptionusedFunc) *(*uintptr)(unsafe.Add(p, 336)) = __ccgo_fp(_compileoptiongetFunc) *(*uintptr)(unsafe.Add(p, 376)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 416)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 456)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 496)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 536)) = __ccgo_fp(_trimFunc) *(*uintptr)(unsafe.Add(p, 576)) = __ccgo_fp(_trimFunc) *(*uintptr)(unsafe.Add(p, 616)) = __ccgo_fp(_trimFunc) *(*uintptr)(unsafe.Add(p, 656)) = __ccgo_fp(_trimFunc) *(*uintptr)(unsafe.Add(p, 696)) = __ccgo_fp(_trimFunc) *(*uintptr)(unsafe.Add(p, 736)) = __ccgo_fp(_trimFunc) *(*uintptr)(unsafe.Add(p, 776)) = __ccgo_fp(_minmaxFunc) *(*uintptr)(unsafe.Add(p, 856)) = __ccgo_fp(_minmaxStep) *(*uintptr)(unsafe.Add(p, 860)) = __ccgo_fp(_minMaxFinalize) *(*uintptr)(unsafe.Add(p, 864)) = __ccgo_fp(_minMaxValue) *(*uintptr)(unsafe.Add(p, 896)) = __ccgo_fp(_minmaxFunc) *(*uintptr)(unsafe.Add(p, 976)) = __ccgo_fp(_minmaxStep) *(*uintptr)(unsafe.Add(p, 980)) = __ccgo_fp(_minMaxFinalize) *(*uintptr)(unsafe.Add(p, 984)) = __ccgo_fp(_minMaxValue) *(*uintptr)(unsafe.Add(p, 1016)) = __ccgo_fp(_typeofFunc) *(*uintptr)(unsafe.Add(p, 1056)) = __ccgo_fp(_subtypeFunc) *(*uintptr)(unsafe.Add(p, 1096)) = __ccgo_fp(_lengthFunc) *(*uintptr)(unsafe.Add(p, 1136)) = __ccgo_fp(_bytelengthFunc) *(*uintptr)(unsafe.Add(p, 1176)) = __ccgo_fp(_instrFunc) *(*uintptr)(unsafe.Add(p, 1216)) = __ccgo_fp(_printfFunc) *(*uintptr)(unsafe.Add(p, 1256)) = __ccgo_fp(_printfFunc) *(*uintptr)(unsafe.Add(p, 1296)) = __ccgo_fp(_unicodeFunc) *(*uintptr)(unsafe.Add(p, 1336)) = __ccgo_fp(_charFunc) *(*uintptr)(unsafe.Add(p, 1376)) = __ccgo_fp(_absFunc) *(*uintptr)(unsafe.Add(p, 1416)) = __ccgo_fp(_roundFunc) *(*uintptr)(unsafe.Add(p, 1456)) = __ccgo_fp(_roundFunc) *(*uintptr)(unsafe.Add(p, 1496)) = __ccgo_fp(_upperFunc) *(*uintptr)(unsafe.Add(p, 1536)) = __ccgo_fp(_lowerFunc) *(*uintptr)(unsafe.Add(p, 1576)) = __ccgo_fp(_hexFunc) *(*uintptr)(unsafe.Add(p, 1616)) = __ccgo_fp(_unhexFunc) *(*uintptr)(unsafe.Add(p, 1656)) = __ccgo_fp(_unhexFunc) *(*uintptr)(unsafe.Add(p, 1696)) = __ccgo_fp(_concatFunc) *(*uintptr)(unsafe.Add(p, 1776)) = __ccgo_fp(_concatwsFunc) *(*uintptr)(unsafe.Add(p, 1896)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 1936)) = __ccgo_fp(_randomFunc) *(*uintptr)(unsafe.Add(p, 1976)) = __ccgo_fp(_randomBlob) *(*uintptr)(unsafe.Add(p, 2016)) = __ccgo_fp(_nullifFunc) *(*uintptr)(unsafe.Add(p, 2056)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 2096)) = __ccgo_fp(_sourceidFunc) *(*uintptr)(unsafe.Add(p, 2136)) = __ccgo_fp(_errlogFunc) *(*uintptr)(unsafe.Add(p, 2176)) = __ccgo_fp(_quoteFunc) *(*uintptr)(unsafe.Add(p, 2216)) = __ccgo_fp(_last_insert_rowid) *(*uintptr)(unsafe.Add(p, 2256)) = __ccgo_fp(_changes) *(*uintptr)(unsafe.Add(p, 2296)) = __ccgo_fp(_total_changes) *(*uintptr)(unsafe.Add(p, 2336)) = __ccgo_fp(_replaceFunc) *(*uintptr)(unsafe.Add(p, 2376)) = __ccgo_fp(_zeroblobFunc) *(*uintptr)(unsafe.Add(p, 2416)) = __ccgo_fp(_substrFunc) *(*uintptr)(unsafe.Add(p, 2456)) = __ccgo_fp(_substrFunc) *(*uintptr)(unsafe.Add(p, 2496)) = __ccgo_fp(_substrFunc) *(*uintptr)(unsafe.Add(p, 2536)) = __ccgo_fp(_substrFunc) *(*uintptr)(unsafe.Add(p, 2576)) = __ccgo_fp(_sumStep) *(*uintptr)(unsafe.Add(p, 2580)) = __ccgo_fp(_sumFinalize) *(*uintptr)(unsafe.Add(p, 2584)) = __ccgo_fp(_sumFinalize) *(*uintptr)(unsafe.Add(p, 2588)) = __ccgo_fp(_sumInverse) *(*uintptr)(unsafe.Add(p, 2616)) = __ccgo_fp(_sumStep) *(*uintptr)(unsafe.Add(p, 2620)) = __ccgo_fp(_totalFinalize) *(*uintptr)(unsafe.Add(p, 2624)) = __ccgo_fp(_totalFinalize) *(*uintptr)(unsafe.Add(p, 2628)) = __ccgo_fp(_sumInverse) *(*uintptr)(unsafe.Add(p, 2656)) = __ccgo_fp(_sumStep) *(*uintptr)(unsafe.Add(p, 2660)) = __ccgo_fp(_avgFinalize) *(*uintptr)(unsafe.Add(p, 2664)) = __ccgo_fp(_avgFinalize) *(*uintptr)(unsafe.Add(p, 2668)) = __ccgo_fp(_sumInverse) *(*uintptr)(unsafe.Add(p, 2696)) = __ccgo_fp(_countStep) *(*uintptr)(unsafe.Add(p, 2700)) = __ccgo_fp(_countFinalize) *(*uintptr)(unsafe.Add(p, 2704)) = __ccgo_fp(_countFinalize) *(*uintptr)(unsafe.Add(p, 2708)) = __ccgo_fp(_countInverse) *(*uintptr)(unsafe.Add(p, 2736)) = __ccgo_fp(_countStep) *(*uintptr)(unsafe.Add(p, 2740)) = __ccgo_fp(_countFinalize) *(*uintptr)(unsafe.Add(p, 2744)) = __ccgo_fp(_countFinalize) *(*uintptr)(unsafe.Add(p, 2748)) = __ccgo_fp(_countInverse) *(*uintptr)(unsafe.Add(p, 2776)) = __ccgo_fp(_groupConcatStep) *(*uintptr)(unsafe.Add(p, 2780)) = __ccgo_fp(_groupConcatFinalize) *(*uintptr)(unsafe.Add(p, 2784)) = __ccgo_fp(_groupConcatValue) *(*uintptr)(unsafe.Add(p, 2788)) = __ccgo_fp(_groupConcatInverse) *(*uintptr)(unsafe.Add(p, 2816)) = __ccgo_fp(_groupConcatStep) *(*uintptr)(unsafe.Add(p, 2820)) = __ccgo_fp(_groupConcatFinalize) *(*uintptr)(unsafe.Add(p, 2824)) = __ccgo_fp(_groupConcatValue) *(*uintptr)(unsafe.Add(p, 2828)) = __ccgo_fp(_groupConcatInverse) *(*uintptr)(unsafe.Add(p, 2856)) = __ccgo_fp(_groupConcatStep) *(*uintptr)(unsafe.Add(p, 2860)) = __ccgo_fp(_groupConcatFinalize) *(*uintptr)(unsafe.Add(p, 2864)) = __ccgo_fp(_groupConcatValue) *(*uintptr)(unsafe.Add(p, 2868)) = __ccgo_fp(_groupConcatInverse) *(*uintptr)(unsafe.Add(p, 2896)) = __ccgo_fp(_likeFunc) *(*uintptr)(unsafe.Add(p, 2936)) = __ccgo_fp(_likeFunc) *(*uintptr)(unsafe.Add(p, 2976)) = __ccgo_fp(_likeFunc) *(*uintptr)(unsafe.Add(p, 3088)) = __ccgo_fp(_xCeil) *(*uintptr)(unsafe.Add(p, 3096)) = __ccgo_fp(_ceilingFunc) *(*uintptr)(unsafe.Add(p, 3128)) = __ccgo_fp(_xCeil) *(*uintptr)(unsafe.Add(p, 3136)) = __ccgo_fp(_ceilingFunc) *(*uintptr)(unsafe.Add(p, 3168)) = __ccgo_fp(_xFloor) *(*uintptr)(unsafe.Add(p, 3176)) = __ccgo_fp(_ceilingFunc) *(*uintptr)(unsafe.Add(p, 3208)) = __ccgo_fp(libc.Xtrunc) *(*uintptr)(unsafe.Add(p, 3216)) = __ccgo_fp(_ceilingFunc) *(*uintptr)(unsafe.Add(p, 3256)) = __ccgo_fp(_logFunc) *(*uintptr)(unsafe.Add(p, 3296)) = __ccgo_fp(_logFunc) *(*uintptr)(unsafe.Add(p, 3336)) = __ccgo_fp(_logFunc) *(*uintptr)(unsafe.Add(p, 3376)) = __ccgo_fp(_logFunc) *(*uintptr)(unsafe.Add(p, 3416)) = __ccgo_fp(_logFunc) *(*uintptr)(unsafe.Add(p, 3448)) = __ccgo_fp(libc.Xexp) *(*uintptr)(unsafe.Add(p, 3456)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 3488)) = __ccgo_fp(libc.Xpow) *(*uintptr)(unsafe.Add(p, 3496)) = __ccgo_fp(_math2Func) *(*uintptr)(unsafe.Add(p, 3528)) = __ccgo_fp(libc.Xpow) *(*uintptr)(unsafe.Add(p, 3536)) = __ccgo_fp(_math2Func) *(*uintptr)(unsafe.Add(p, 3568)) = __ccgo_fp(libc.Xfmod) *(*uintptr)(unsafe.Add(p, 3576)) = __ccgo_fp(_math2Func) *(*uintptr)(unsafe.Add(p, 3608)) = __ccgo_fp(libc.Xacos) *(*uintptr)(unsafe.Add(p, 3616)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 3648)) = __ccgo_fp(libc.Xasin) *(*uintptr)(unsafe.Add(p, 3656)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 3688)) = __ccgo_fp(libc.Xatan) *(*uintptr)(unsafe.Add(p, 3696)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 3728)) = __ccgo_fp(libc.Xatan2) *(*uintptr)(unsafe.Add(p, 3736)) = __ccgo_fp(_math2Func) *(*uintptr)(unsafe.Add(p, 3768)) = __ccgo_fp(libc.Xcos) *(*uintptr)(unsafe.Add(p, 3776)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 3808)) = __ccgo_fp(libc.Xsin) *(*uintptr)(unsafe.Add(p, 3816)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 3848)) = __ccgo_fp(libc.Xtan) *(*uintptr)(unsafe.Add(p, 3856)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 3888)) = __ccgo_fp(libc.Xcosh) *(*uintptr)(unsafe.Add(p, 3896)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 3928)) = __ccgo_fp(libc.Xsinh) *(*uintptr)(unsafe.Add(p, 3936)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 3968)) = __ccgo_fp(libc.Xtanh) *(*uintptr)(unsafe.Add(p, 3976)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 4008)) = __ccgo_fp(libc.Xacosh) *(*uintptr)(unsafe.Add(p, 4016)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 4048)) = __ccgo_fp(libc.Xasinh) *(*uintptr)(unsafe.Add(p, 4056)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 4088)) = __ccgo_fp(libc.Xatanh) *(*uintptr)(unsafe.Add(p, 4096)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 4128)) = __ccgo_fp(libc.Xsqrt) *(*uintptr)(unsafe.Add(p, 4136)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 4168)) = __ccgo_fp(_degToRad) *(*uintptr)(unsafe.Add(p, 4176)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 4208)) = __ccgo_fp(_radToDeg) *(*uintptr)(unsafe.Add(p, 4216)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 4256)) = __ccgo_fp(_piFunc) *(*uintptr)(unsafe.Add(p, 4296)) = __ccgo_fp(_signFunc) *(*uintptr)(unsafe.Add(p, 4336)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 4376)) = __ccgo_fp(_versionFunc) } /************** End of func.c ************************************************/ /************** Begin file fkey.c ********************************************/ /* ** ** 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 contains code used by the compiler to add foreign key ** support to compiled SQL statements. */ /* #include "sqliteInt.h" */ /* ** Deferred and Immediate FKs ** -------------------------- ** ** Foreign keys in SQLite come in two flavours: deferred and immediate. ** If an immediate foreign key constraint is violated, ** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current ** statement transaction rolled back. If a ** deferred foreign key constraint is violated, no action is taken ** immediately. However if the application attempts to commit the ** transaction before fixing the constraint violation, the attempt fails. ** ** Deferred constraints are implemented using a simple counter associated ** with the database handle. The counter is set to zero each time a ** database transaction is opened. Each time a statement is executed ** that causes a foreign key violation, the counter is incremented. Each ** time a statement is executed that removes an existing violation from ** the database, the counter is decremented. When the transaction is ** committed, the commit fails if the current value of the counter is ** greater than zero. This scheme has two big drawbacks: ** ** * When a commit fails due to a deferred foreign key constraint, ** there is no way to tell which foreign constraint is not satisfied, ** or which row it is not satisfied for. ** ** * If the database contains foreign key violations when the ** transaction is opened, this may cause the mechanism to malfunction. ** ** Despite these problems, this approach is adopted as it seems simpler ** than the alternatives. ** ** INSERT operations: ** ** I.1) For each FK for which the table is the child table, search ** the parent table for a match. If none is found increment the ** constraint counter. ** ** I.2) For each FK for which the table is the parent table, ** search the child table for rows that correspond to the new ** row in the parent table. Decrement the counter for each row ** found (as the constraint is now satisfied). ** ** DELETE operations: ** ** D.1) For each FK for which the table is the child table, ** search the parent table for a row that corresponds to the ** deleted row in the child table. If such a row is not found, ** decrement the counter. ** ** D.2) For each FK for which the table is the parent table, search ** the child table for rows that correspond to the deleted row ** in the parent table. For each found increment the counter. ** ** UPDATE operations: ** ** An UPDATE command requires that all 4 steps above are taken, but only ** for FK constraints for which the affected columns are actually ** modified (values must be compared at runtime). ** ** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2. ** This simplifies the implementation a bit. ** ** For the purposes of immediate FK constraints, the OR REPLACE conflict ** resolution is considered to delete rows before the new row is inserted. ** If a delete caused by OR REPLACE violates an FK constraint, an exception ** is thrown, even if the FK constraint would be satisfied after the new ** row is inserted. ** ** Immediate constraints are usually handled similarly. The only difference ** is that the counter used is stored as part of each individual statement ** object (struct Vdbe). If, after the statement has run, its immediate ** constraint counter is greater than zero, ** it returns SQLITE_CONSTRAINT_FOREIGNKEY ** and the statement transaction is rolled back. An exception is an INSERT ** statement that inserts a single row only (no triggers). In this case, ** instead of using a counter, an exception is thrown immediately if the ** INSERT violates a foreign key constraint. This is necessary as such ** an INSERT does not open a statement transaction. ** ** TODO: How should dropping a table be handled? How should renaming a ** table be handled? ** ** ** Query API Notes ** --------------- ** ** Before coding an UPDATE or DELETE row operation, the code-generator ** for those two operations needs to know whether or not the operation ** requires any FK processing and, if so, which columns of the original ** row are required by the FK processing VDBE code (i.e. if FKs were ** implemented using triggers, which of the old.* columns would be ** accessed). No information is required by the code-generator before ** coding an INSERT operation. The functions used by the UPDATE/DELETE ** generation code to query for this information are: ** ** sqlite3FkRequired() - Test to see if FK processing is required. ** sqlite3FkOldmask() - Query for the set of required old.* columns. ** ** ** Externally accessible module functions ** -------------------------------------- ** ** sqlite3FkCheck() - Check for foreign key violations. ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions. ** sqlite3FkDelete() - Delete an FKey structure. */ /* ** VDBE Calling Convention ** ----------------------- ** ** Example: ** ** For the following INSERT statement: ** ** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c); ** INSERT INTO t1 VALUES(1, 2, 3.1); ** ** Register (x): 2 (type integer) ** Register (x+1): 1 (type integer) ** Register (x+2): NULL (type NULL) ** Register (x+3): 3.1 (type real) */ // C documentation // // /* // ** A foreign key constraint requires that the key columns in the parent // ** table are collectively subject to a UNIQUE or PRIMARY KEY constraint. // ** Given that pParent is the parent table for foreign key constraint pFKey, // ** search the schema for a unique index on the parent key columns. // ** // ** If successful, zero is returned. If the parent key is an INTEGER PRIMARY // ** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx // ** is set to point to the unique index. // ** // ** If the parent key consists of a single column (the foreign key constraint // ** is not a composite foreign key), output variable *paiCol is set to NULL. // ** Otherwise, it is set to point to an allocated array of size N, where // ** N is the number of columns in the parent key. The first element of the // ** array is the index of the child table column that is mapped by the FK // ** constraint to the parent table column stored in the left-most column // ** of index *ppIdx. The second element of the array is the index of the // ** child table column that corresponds to the second left-most column of // ** *ppIdx, and so on. // ** // ** If the required index cannot be found, either because: // ** // ** 1) The named parent key columns do not exist, or // ** // ** 2) The named parent key columns do exist, but are not subject to a // ** UNIQUE or PRIMARY KEY constraint, or // ** // ** 3) No parent key columns were provided explicitly as part of the // ** foreign key definition, and the parent table does not have a // ** PRIMARY KEY, or // ** // ** 4) No parent key columns were provided explicitly as part of the // ** foreign key definition, and the PRIMARY KEY of the parent table // ** consists of a different number of columns to the child key in // ** the child table. // ** // ** then non-zero is returned, and a "foreign key mismatch" error loaded // ** into pParse. If an OOM error occurs, non-zero is returned and the // ** pParse->db->mallocFailed flag is set. // */ func _sqlite3FkLocateIndex(tls *libc.TLS, pParse uintptr, pParent uintptr, pFKey uintptr, ppIdx uintptr, paiCol uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var aiCol, pIdx, zDfltColl, zIdxCol, zKey uintptr var i, i1, j, nCol int32 var iCol Ti16 _, _, _, _, _, _, _, _, _, _ = aiCol, i, i1, iCol, j, nCol, pIdx, zDfltColl, zIdxCol, zKey pIdx = uintptr(0) /* Value to return via *ppIdx */ aiCol = uintptr(0) /* Value to return via *paiCol */ nCol = (*TFKey)(unsafe.Pointer(pFKey)).FnCol /* Number of columns in parent key */ zKey = (*(*TsColMap)(unsafe.Pointer(pFKey + 36))).FzCol /* Name of left-most parent key column */ /* The caller is responsible for zeroing output parameters. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If this is a non-composite (single column) foreign key, check if it ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx ** and *paiCol set to zero and return early. ** ** Otherwise, for a composite foreign key (more than one column), allocate ** space for the aiCol array (returned via output parameter *paiCol). ** Non-composite foreign keys do not require the aiCol array. */ if nCol == int32(1) { /* The FK maps to the IPK if any of the following are true: ** ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly ** mapped to the primary key of table pParent, or ** 2) The FK is explicitly mapped to a column declared as INTEGER ** PRIMARY KEY. */ if int32((*TTable)(unsafe.Pointer(pParent)).FiPKey) >= 0 { if !(zKey != 0) { return 0 } if !(_sqlite3StrICmp(tls, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pParent)).FaCol + uintptr((*TTable)(unsafe.Pointer(pParent)).FiPKey)*12))).FzCnName, zKey) != 0) { return 0 } } } else { if paiCol != 0 { _ = libc.Int32FromInt32(0) aiCol = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(nCol)*uint32(4))) if !(aiCol != 0) { return int32(1) } *(*uintptr)(unsafe.Pointer(paiCol)) = aiCol } } pIdx = (*TTable)(unsafe.Pointer(pParent)).FpIndex for { if !(pIdx != 0) { break } if int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) == nCol && int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != OE_None && (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere == uintptr(0) { /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number ** of columns. If each indexed column corresponds to a foreign key ** column of pFKey, then this index is a winner. */ if zKey == uintptr(0) { /* If zKey is NULL, then this foreign key is implicitly mapped to ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be ** identified by the test. */ if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) { if aiCol != 0 { i = 0 for { if !(i < nCol) { break } *(*int32)(unsafe.Pointer(aiCol + uintptr(i)*4)) = (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(i)*8))).FiFrom goto _2 _2: ; i++ } } break } } else { i1 = 0 for { if !(i1 < nCol) { break } iCol = *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i1)*2)) /* Name of indexed column */ if int32(iCol) < 0 { break } /* No foreign keys against expression indexes */ /* If the index uses a collation sequence that is different from ** the default collation sequence for the column, this index is ** unusable. Bail out early in this case. */ zDfltColl = _sqlite3ColumnColl(tls, (*TTable)(unsafe.Pointer(pParent)).FaCol+uintptr(iCol)*12) if !(zDfltColl != 0) { zDfltColl = uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) } if _sqlite3StrICmp(tls, *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(i1)*4)), zDfltColl) != 0 { break } zIdxCol = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pParent)).FaCol + uintptr(iCol)*12))).FzCnName j = 0 for { if !(j < nCol) { break } if _sqlite3StrICmp(tls, (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(j)*8))).FzCol, zIdxCol) == 0 { if aiCol != 0 { *(*int32)(unsafe.Pointer(aiCol + uintptr(i1)*4)) = (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(j)*8))).FiFrom } break } goto _4 _4: ; j++ } if j == nCol { break } goto _3 _3: ; i1++ } if i1 == nCol { break } /* pIdx is usable */ } } goto _1 _1: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } if !(pIdx != 0) { if !((*TParse)(unsafe.Pointer(pParse)).FdisableTriggers != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16039, libc.VaList(bp+8, (*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FzName, (*TFKey)(unsafe.Pointer(pFKey)).FzTo)) } _sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, aiCol) return int32(1) } *(*uintptr)(unsafe.Pointer(ppIdx)) = pIdx return 0 } // C documentation // // /* // ** This function is called when a row is inserted into or deleted from the // ** child table of foreign key constraint pFKey. If an SQL UPDATE is executed // ** on the child table of pFKey, this function is invoked twice for each row // ** affected - once to "delete" the old row, and then again to "insert" the // ** new row. // ** // ** Each time it is called, this function generates VDBE code to locate the // ** row in the parent table that corresponds to the row being inserted into // ** or deleted from the child table. If the parent row can be found, no // ** special action is taken. Otherwise, if the parent row can *not* be // ** found in the parent table: // ** // ** Operation | FK type | Action taken // ** -------------------------------------------------------------------------- // ** INSERT immediate Increment the "immediate constraint counter". // ** // ** DELETE immediate Decrement the "immediate constraint counter". // ** // ** INSERT deferred Increment the "deferred constraint counter". // ** // ** DELETE deferred Decrement the "deferred constraint counter". // ** // ** These operations are identified in the comment at the top of this file // ** (fkey.c) as "I.1" and "D.1". // */ func _fkLookupParent(tls *libc.TLS, pParse uintptr, iDb int32, pTab uintptr, pIdx uintptr, pFKey uintptr, aiCol uintptr, regData int32, nIncr int32, isIgnore int32) { var i, iChild, iCur, iJump, iMustBeInt, iOk, iParent, iReg, nCol, regTemp, regTemp1 int32 var v uintptr _, _, _, _, _, _, _, _, _, _, _, _ = i, iChild, iCur, iJump, iMustBeInt, iOk, iParent, iReg, nCol, regTemp, regTemp1, v /* Iterator variable */ v = _sqlite3GetVdbe(tls, pParse) /* Vdbe to add code to */ iCur = (*TParse)(unsafe.Pointer(pParse)).FnTab - int32(1) /* Cursor number to use */ iOk = _sqlite3VdbeMakeLabel(tls, pParse) /* jump here if parent key found */ /* If nIncr is less than zero, then check at runtime if there are any ** outstanding constraints to resolve. If there are not, there is no need ** to check if deleting this row resolves any outstanding violations. ** ** Check if any of the key columns in the child table row are NULL. If ** any are, then the constraint is considered satisfied. No need to ** search for a matching row in the parent table. */ if nIncr < 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_FkIfZero), int32((*TFKey)(unsafe.Pointer(pFKey)).FisDeferred), iOk) } i = 0 for { if !(i < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) { break } iReg = int32(_sqlite3TableColumnToStorage(tls, (*TFKey)(unsafe.Pointer(pFKey)).FpFrom, int16(*(*int32)(unsafe.Pointer(aiCol + uintptr(i)*4))))) + regData + int32(1) _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), iReg, iOk) goto _1 _1: ; i++ } if isIgnore == 0 { if pIdx == uintptr(0) { /* Address of MustBeInt instruction */ regTemp = _sqlite3GetTempReg(tls, pParse) /* Invoke MustBeInt to coerce the child key value to an integer (i.e. ** apply the affinity of the parent key). If this fails, then there ** is no matching parent key. Before using MustBeInt, make a copy of ** the value. Otherwise, the value inserted into the child key column ** will have INTEGER affinity applied to it, which may not be correct. */ _sqlite3VdbeAddOp2(tls, v, int32(OP_SCopy), int32(_sqlite3TableColumnToStorage(tls, (*TFKey)(unsafe.Pointer(pFKey)).FpFrom, int16(*(*int32)(unsafe.Pointer(aiCol)))))+int32(1)+regData, regTemp) iMustBeInt = _sqlite3VdbeAddOp2(tls, v, int32(OP_MustBeInt), regTemp, 0) /* If the parent table is the same as the child table, and we are about ** to increment the constraint-counter (i.e. this is an INSERT operation), ** then check if the row being inserted matches itself. If so, do not ** increment the constraint-counter. */ if pTab == (*TFKey)(unsafe.Pointer(pFKey)).FpFrom && nIncr == int32(1) { _sqlite3VdbeAddOp3(tls, v, int32(OP_Eq), regData, iOk, regTemp) _sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_NOTNULL)) } _sqlite3OpenTable(tls, pParse, iCur, iDb, pTab, int32(OP_OpenRead)) _sqlite3VdbeAddOp3(tls, v, int32(OP_NotExists), iCur, 0, regTemp) _sqlite3VdbeGoto(tls, v, iOk) _sqlite3VdbeJumpHere(tls, v, _sqlite3VdbeCurrentAddr(tls, v)-int32(2)) _sqlite3VdbeJumpHere(tls, v, iMustBeInt) _sqlite3ReleaseTempReg(tls, pParse, regTemp) } else { nCol = (*TFKey)(unsafe.Pointer(pFKey)).FnCol regTemp1 = _sqlite3GetTempRange(tls, pParse, nCol) _sqlite3VdbeAddOp3(tls, v, int32(OP_OpenRead), iCur, int32((*TIndex)(unsafe.Pointer(pIdx)).Ftnum), iDb) _sqlite3VdbeSetP4KeyInfo(tls, pParse, pIdx) i = 0 for { if !(i < nCol) { break } _sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), int32(_sqlite3TableColumnToStorage(tls, (*TFKey)(unsafe.Pointer(pFKey)).FpFrom, int16(*(*int32)(unsafe.Pointer(aiCol + uintptr(i)*4)))))+int32(1)+regData, regTemp1+i) goto _2 _2: ; i++ } /* If the parent table is the same as the child table, and we are about ** to increment the constraint-counter (i.e. this is an INSERT operation), ** then check if the row being inserted matches itself. If so, do not ** increment the constraint-counter. ** ** If any of the parent-key values are NULL, then the row cannot match ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any ** of the parent-key values are NULL (at this point it is known that ** none of the child key values are). */ if pTab == (*TFKey)(unsafe.Pointer(pFKey)).FpFrom && nIncr == int32(1) { iJump = _sqlite3VdbeCurrentAddr(tls, v) + nCol + int32(1) i = 0 for { if !(i < nCol) { break } iChild = int32(_sqlite3TableColumnToStorage(tls, (*TFKey)(unsafe.Pointer(pFKey)).FpFrom, int16(*(*int32)(unsafe.Pointer(aiCol + uintptr(i)*4))))) + int32(1) + regData iParent = int32(1) + regData iParent += int32(_sqlite3TableColumnToStorage(tls, (*TIndex)(unsafe.Pointer(pIdx)).FpTable, *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2)))) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))) == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { /* The parent key is a composite key that includes the IPK column */ iParent = regData } _sqlite3VdbeAddOp3(tls, v, int32(OP_Ne), iChild, iJump, iParent) _sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_JUMPIFNULL)) goto _3 _3: ; i++ } _sqlite3VdbeGoto(tls, v, iOk) } _sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), regTemp1, nCol, 0, _sqlite3IndexAffinityStr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pIdx), nCol) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), iCur, iOk, regTemp1, nCol) _sqlite3ReleaseTempRange(tls, pParse, regTemp1, nCol) } } if !((*TFKey)(unsafe.Pointer(pFKey)).FisDeferred != 0) && !((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&libc.Uint64FromInt32(SQLITE_DeferFKs) != 0) && !((*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0) && !((*TParse)(unsafe.Pointer(pParse)).FisMultiWrite != 0) { /* Special case: If this is an INSERT statement that will insert exactly ** one row into the table, raise a constraint immediately instead of ** incrementing a counter. This is necessary as the VM code is being ** generated for will not open a statement transaction. */ _ = libc.Int32FromInt32(0) _sqlite3HaltConstraint(tls, pParse, libc.Int32FromInt32(SQLITE_CONSTRAINT)|libc.Int32FromInt32(3)< 0 && int32((*TFKey)(unsafe.Pointer(pFKey)).FisDeferred) == 0 { _sqlite3MayAbort(tls, pParse) } _sqlite3VdbeAddOp2(tls, v, int32(OP_FkCounter), int32((*TFKey)(unsafe.Pointer(pFKey)).FisDeferred), nIncr) } _sqlite3VdbeResolveLabel(tls, v, iOk) _sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iCur) } // C documentation // // /* // ** Return an Expr object that refers to a memory register corresponding // ** to column iCol of table pTab. // ** // ** regBase is the first of an array of register that contains the data // ** for pTab. regBase itself holds the rowid. regBase+1 holds the first // ** column. regBase+2 holds the second column, and so forth. // */ func _exprTableRegister(tls *libc.TLS, pParse uintptr, pTab uintptr, regBase int32, iCol Ti16) (r uintptr) { var db, pCol, pExpr, zColl uintptr _, _, _, _ = db, pCol, pExpr, zColl db = (*TParse)(unsafe.Pointer(pParse)).Fdb pExpr = _sqlite3Expr(tls, db, int32(TK_REGISTER), uintptr(0)) if pExpr != 0 { if int32(iCol) >= 0 && int32(iCol) != int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12 (*TExpr)(unsafe.Pointer(pExpr)).FiTable = regBase + int32(_sqlite3TableColumnToStorage(tls, pTab, iCol)) + int32(1) (*TExpr)(unsafe.Pointer(pExpr)).FaffExpr = (*TColumn)(unsafe.Pointer(pCol)).Faffinity zColl = _sqlite3ColumnColl(tls, pCol) if zColl == uintptr(0) { zColl = (*TCollSeq)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpDfltColl)).FzName } pExpr = _sqlite3ExprAddCollateString(tls, pParse, pExpr, zColl) } else { (*TExpr)(unsafe.Pointer(pExpr)).FiTable = regBase (*TExpr)(unsafe.Pointer(pExpr)).FaffExpr = uint8(SQLITE_AFF_INTEGER) } } return pExpr } // C documentation // // /* // ** Return an Expr object that refers to column iCol of table pTab which // ** has cursor iCur. // */ func _exprTableColumn(tls *libc.TLS, db uintptr, pTab uintptr, iCursor int32, iCol Ti16) (r uintptr) { var pExpr uintptr _ = pExpr pExpr = _sqlite3Expr(tls, db, int32(TK_COLUMN), uintptr(0)) if pExpr != 0 { _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab = pTab (*TExpr)(unsafe.Pointer(pExpr)).FiTable = iCursor (*TExpr)(unsafe.Pointer(pExpr)).FiColumn = iCol } return pExpr } // C documentation // // /* // ** This function is called to generate code executed when a row is deleted // ** from the parent table of foreign key constraint pFKey and, if pFKey is // ** deferred, when a row is inserted into the same table. When generating // ** code for an SQL UPDATE operation, this function may be called twice - // ** once to "delete" the old row and once to "insert" the new row. // ** // ** Parameter nIncr is passed -1 when inserting a row (as this may decrease // ** the number of FK violations in the db) or +1 when deleting one (as this // ** may increase the number of FK constraint problems). // ** // ** The code generated by this function scans through the rows in the child // ** table that correspond to the parent table row being deleted or inserted. // ** For each child row found, one of the following actions is taken: // ** // ** Operation | FK type | Action taken // ** -------------------------------------------------------------------------- // ** DELETE immediate Increment the "immediate constraint counter". // ** // ** INSERT immediate Decrement the "immediate constraint counter". // ** // ** DELETE deferred Increment the "deferred constraint counter". // ** // ** INSERT deferred Decrement the "deferred constraint counter". // ** // ** These operations are identified in the comment at the top of this file // ** (fkey.c) as "I.2" and "D.2". // */ func _fkScanChildren(tls *libc.TLS, pParse uintptr, pSrc uintptr, pTab uintptr, pIdx uintptr, pFKey uintptr, aiCol uintptr, regData int32, nIncr int32) { bp := tls.Alloc(48) defer tls.Free(48) var db, pAll, pEq, pEq1, pLeft, pLeft1, pNe, pRight, pRight1, pWInfo, pWhere, v, zCol uintptr var i, iFkIfZero, v2, v3 int32 var iCol, iCol1 Ti16 var _ /* sNameContext at bp+0 */ TNameContext _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, i, iCol, iCol1, iFkIfZero, pAll, pEq, pEq1, pLeft, pLeft1, pNe, pRight, pRight1, pWInfo, pWhere, v, zCol, v2, v3 db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Iterator variable */ pWhere = uintptr(0) /* Context used by sqlite3WhereXXX() */ iFkIfZero = 0 /* Address of OP_FkIfZero */ v = _sqlite3GetVdbe(tls, pParse) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if nIncr < 0 { iFkIfZero = _sqlite3VdbeAddOp2(tls, v, int32(OP_FkIfZero), int32((*TFKey)(unsafe.Pointer(pFKey)).FisDeferred), 0) } /* Create an Expr object representing an SQL expression like: ** ** = AND = ... ** ** The collation sequence used for the comparison should be that of ** the parent key columns. The affinity of the parent key column should ** be applied to each child key value before the comparison takes place. */ i = 0 for { if !(i < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) { break } /* Name of column in child table */ if pIdx != 0 { v2 = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))) } else { v2 = -int32(1) } iCol = int16(v2) pLeft = _exprTableRegister(tls, pParse, pTab, regData, iCol) if aiCol != 0 { v3 = *(*int32)(unsafe.Pointer(aiCol + uintptr(i)*4)) } else { v3 = (*(*TsColMap)(unsafe.Pointer(pFKey + 36))).FiFrom } iCol = int16(v3) _ = libc.Int32FromInt32(0) zCol = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FaCol + uintptr(iCol)*12))).FzCnName pRight = _sqlite3Expr(tls, db, int32(TK_ID), zCol) pEq = _sqlite3PExpr(tls, pParse, int32(TK_EQ), pLeft, pRight) pWhere = _sqlite3ExprAnd(tls, pParse, pWhere, pEq) goto _1 _1: ; i++ } /* If the child table is the same as the parent table, then add terms ** to the WHERE clause that prevent this entry from being scanned. ** The added WHERE clause terms are like this: ** ** $current_rowid!=rowid ** NOT( $current_a==a AND $current_b==b AND ... ) ** ** The first form is used for rowid tables. The second form is used ** for WITHOUT ROWID tables. In the second form, the *parent* key is ** (a,b,...). Either the parent or primary key could be used to ** uniquely identify the current row, but the parent key is more convenient ** as the required values have already been loaded into registers ** by the caller. */ if pTab == (*TFKey)(unsafe.Pointer(pFKey)).FpFrom && nIncr > 0 { /* Column ref to child table */ if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { pLeft1 = _exprTableRegister(tls, pParse, pTab, regData, int16(-int32(1))) pRight1 = _exprTableColumn(tls, db, pTab, (*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FiCursor, int16(-int32(1))) pNe = _sqlite3PExpr(tls, pParse, int32(TK_NE), pLeft1, pRight1) } else { pAll = uintptr(0) _ = libc.Int32FromInt32(0) i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) { break } iCol1 = *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2)) _ = libc.Int32FromInt32(0) pLeft1 = _exprTableRegister(tls, pParse, pTab, regData, iCol1) pRight1 = _sqlite3Expr(tls, db, int32(TK_ID), (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol1)*12))).FzCnName) pEq1 = _sqlite3PExpr(tls, pParse, int32(TK_IS), pLeft1, pRight1) pAll = _sqlite3ExprAnd(tls, pParse, pAll, pEq1) goto _4 _4: ; i++ } pNe = _sqlite3PExpr(tls, pParse, int32(TK_NOT), pAll, uintptr(0)) } pWhere = _sqlite3ExprAnd(tls, pParse, pWhere, pNe) } /* Resolve the references in the WHERE clause. */ libc.Xmemset(tls, bp, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp))).FpSrcList = pSrc (*(*TNameContext)(unsafe.Pointer(bp))).FpParse = pParse _sqlite3ResolveExprNames(tls, bp, pWhere) /* Create VDBE to loop through the entries in pSrc that match the WHERE ** clause. For each row found, increment either the deferred or immediate ** foreign key constraint counter. */ if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 { pWInfo = _sqlite3WhereBegin(tls, pParse, pSrc, pWhere, uintptr(0), uintptr(0), uintptr(0), uint16(0), 0) _sqlite3VdbeAddOp2(tls, v, int32(OP_FkCounter), int32((*TFKey)(unsafe.Pointer(pFKey)).FisDeferred), nIncr) if pWInfo != 0 { _sqlite3WhereEnd(tls, pWInfo) } } /* Clean up the WHERE clause constructed above. */ _sqlite3ExprDelete(tls, db, pWhere) if iFkIfZero != 0 { _sqlite3VdbeJumpHereOrPopInst(tls, v, iFkIfZero) } } // C documentation // // /* // ** This function returns a linked list of FKey objects (connected by // ** FKey.pNextTo) holding all children of table pTab. For example, // ** given the following schema: // ** // ** CREATE TABLE t1(a PRIMARY KEY); // ** CREATE TABLE t2(b REFERENCES t1(a); // ** // ** Calling this function with table "t1" as an argument returns a pointer // ** to the FKey structure representing the foreign key constraint on table // ** "t2". Calling this function with "t2" as the argument would return a // ** NULL pointer (as there are no FK constraints for which t2 is the parent // ** table). // */ func _sqlite3FkReferences(tls *libc.TLS, pTab uintptr) (r uintptr) { return _sqlite3HashFind(tls, (*TTable)(unsafe.Pointer(pTab)).FpSchema+56, (*TTable)(unsafe.Pointer(pTab)).FzName) } // C documentation // // /* // ** The second argument is a Trigger structure allocated by the // ** fkActionTrigger() routine. This function deletes the Trigger structure // ** and all of its sub-components. // ** // ** The Trigger structure or any of its sub-components may be allocated from // ** the lookaside buffer belonging to database handle dbMem. // */ func _fkTriggerDelete(tls *libc.TLS, dbMem uintptr, p uintptr) { var pStep uintptr _ = pStep if p != 0 { pStep = (*TTrigger)(unsafe.Pointer(p)).Fstep_list _sqlite3ExprDelete(tls, dbMem, (*TTriggerStep)(unsafe.Pointer(pStep)).FpWhere) _sqlite3ExprListDelete(tls, dbMem, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList) _sqlite3SelectDelete(tls, dbMem, (*TTriggerStep)(unsafe.Pointer(pStep)).FpSelect) _sqlite3ExprDelete(tls, dbMem, (*TTrigger)(unsafe.Pointer(p)).FpWhen) _sqlite3DbFree(tls, dbMem, p) } } // C documentation // // /* // ** Clear the apTrigger[] cache of CASCADE triggers for all foreign keys // ** in a particular database. This needs to happen when the schema // ** changes. // */ func _sqlite3FkClearTriggerCache(tls *libc.TLS, db uintptr, iDb int32) { var k, pFKey, pHash, pTab uintptr _, _, _, _ = k, pFKey, pHash, pTab pHash = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema + 8 k = (*THash)(unsafe.Pointer(pHash)).Ffirst for { if !(k != 0) { break } pTab = (*THashElem)(unsafe.Pointer(k)).Fdata if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) { goto _1 } pFKey = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpFKey for { if !(pFKey != 0) { break } _fkTriggerDelete(tls, db, *(*uintptr)(unsafe.Pointer(pFKey + 28))) *(*uintptr)(unsafe.Pointer(pFKey + 28)) = uintptr(0) _fkTriggerDelete(tls, db, *(*uintptr)(unsafe.Pointer(pFKey + 28 + 1*4))) *(*uintptr)(unsafe.Pointer(pFKey + 28 + 1*4)) = uintptr(0) goto _2 _2: ; pFKey = (*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom } goto _1 _1: ; k = (*THashElem)(unsafe.Pointer(k)).Fnext } } // C documentation // // /* // ** This function is called to generate code that runs when table pTab is // ** being dropped from the database. The SrcList passed as the second argument // ** to this function contains a single entry guaranteed to resolve to // ** table pTab. // ** // ** Normally, no code is required. However, if either // ** // ** (a) The table is the parent table of a FK constraint, or // ** (b) The table is the child table of a deferred FK constraint and it is // ** determined at runtime that there are outstanding deferred FK // ** constraint violations in the database, // ** // ** then the equivalent of "DELETE FROM " is executed before dropping // ** the table from the database. Triggers are disabled while running this // ** DELETE, but foreign key actions are not. // */ func _sqlite3FkDropTable(tls *libc.TLS, pParse uintptr, pName uintptr, pTab uintptr) { var db, p, v uintptr var iSkip int32 _, _, _, _ = db, iSkip, p, v db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) != 0 && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == TABTYP_NORM { iSkip = 0 v = _sqlite3GetVdbe(tls, pParse) _ = libc.Int32FromInt32(0) /* VDBE has already been allocated */ _ = libc.Int32FromInt32(0) if _sqlite3FkReferences(tls, pTab) == uintptr(0) { p = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpFKey for { if !(p != 0) { break } if (*TFKey)(unsafe.Pointer(p)).FisDeferred != 0 || (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_DeferFKs) != 0 { break } goto _1 _1: ; p = (*TFKey)(unsafe.Pointer(p)).FpNextFrom } if !(p != 0) { return } iSkip = _sqlite3VdbeMakeLabel(tls, pParse) _sqlite3VdbeAddOp2(tls, v, int32(OP_FkIfZero), int32(1), iSkip) } (*TParse)(unsafe.Pointer(pParse)).FdisableTriggers = uint8(1) _sqlite3DeleteFrom(tls, pParse, _sqlite3SrcListDup(tls, db, pName, 0), uintptr(0), uintptr(0), uintptr(0)) (*TParse)(unsafe.Pointer(pParse)).FdisableTriggers = uint8(0) /* If the DELETE has generated immediate foreign key constraint ** violations, halt the VDBE and return an error at this point, before ** any modifications to the schema are made. This is because statement ** transactions are not able to rollback schema changes. ** ** If the SQLITE_DeferFKs flag is set, then this is not required, as ** the statement transaction will not be rolled back even if FK ** constraints are violated. */ if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_DeferFKs) == uint64(0) { _sqlite3VdbeAddOp2(tls, v, int32(OP_FkIfZero), 0, _sqlite3VdbeCurrentAddr(tls, v)+int32(2)) _sqlite3HaltConstraint(tls, pParse, libc.Int32FromInt32(SQLITE_CONSTRAINT)|libc.Int32FromInt32(3)<= 0 { return int32(1) } if iChildKey == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) && bChngRowid != 0 { return int32(1) } goto _1 _1: ; i++ } return 0 } // C documentation // // /* // ** The second argument points to an FKey object representing a foreign key // ** for which pTab is the parent table. An UPDATE statement against pTab // ** is currently being processed. For each column of the table that is // ** actually updated, the corresponding element in the aChange[] array // ** is zero or greater (if a column is unmodified the corresponding element // ** is set to -1). If the rowid column is modified by the UPDATE statement // ** the bChngRowid argument is non-zero. // ** // ** This function returns true if any of the columns that are part of the // ** parent key for FK constraint *p are modified. // */ func _fkParentIsModified(tls *libc.TLS, pTab uintptr, p uintptr, aChange uintptr, bChngRowid int32) (r int32) { var i, iKey int32 var pCol, zKey uintptr _, _, _, _ = i, iKey, pCol, zKey i = 0 for { if !(i < (*TFKey)(unsafe.Pointer(p)).FnCol) { break } zKey = (*(*TsColMap)(unsafe.Pointer(p + 36 + uintptr(i)*8))).FzCol iKey = 0 for { if !(iKey < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if *(*int32)(unsafe.Pointer(aChange + uintptr(iKey)*4)) >= 0 || iKey == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) && bChngRowid != 0 { pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iKey)*12 if zKey != 0 { if 0 == _sqlite3StrICmp(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, zKey) { return int32(1) } } else { if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 { return int32(1) } } } goto _2 _2: ; iKey++ } goto _1 _1: ; i++ } return 0 } // C documentation // // /* // ** Return true if the parser passed as the first argument is being // ** used to code a trigger that is really a "SET NULL" action belonging // ** to trigger pFKey. // */ func _isSetNullAction(tls *libc.TLS, pParse uintptr, pFKey uintptr) (r int32) { var p, pTop, v1 uintptr _, _, _ = p, pTop, v1 if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 { v1 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel } else { v1 = pParse } pTop = v1 if (*TParse)(unsafe.Pointer(pTop)).FpTriggerPrg != 0 { p = (*TTriggerPrg)(unsafe.Pointer((*TParse)(unsafe.Pointer(pTop)).FpTriggerPrg)).FpTrigger if p == *(*uintptr)(unsafe.Pointer(pFKey + 28)) && int32(*(*Tu8)(unsafe.Pointer(pFKey + 25))) == int32(OE_SetNull) || p == *(*uintptr)(unsafe.Pointer(pFKey + 28 + 1*4)) && int32(*(*Tu8)(unsafe.Pointer(pFKey + 25 + 1))) == int32(OE_SetNull) { _ = libc.Int32FromInt32(0) return int32(1) } } return 0 } // C documentation // // /* // ** This function is called when inserting, deleting or updating a row of // ** table pTab to generate VDBE code to perform foreign key constraint // ** processing for the operation. // ** // ** For a DELETE operation, parameter regOld is passed the index of the // ** first register in an array of (pTab->nCol+1) registers containing the // ** rowid of the row being deleted, followed by each of the column values // ** of the row being deleted, from left to right. Parameter regNew is passed // ** zero in this case. // ** // ** For an INSERT operation, regOld is passed zero and regNew is passed the // ** first register of an array of (pTab->nCol+1) registers containing the new // ** row data. // ** // ** For an UPDATE operation, this function is called twice. Once before // ** the original record is deleted from the table using the calling convention // ** described for DELETE. Then again after the original record is deleted // ** but before the new record is inserted using the INSERT convention. // */ func _sqlite3FkCheck(tls *libc.TLS, pParse uintptr, pTab uintptr, regOld int32, regNew int32, aChange uintptr, bChngRowid int32) { bp := tls.Alloc(32) defer tls.Free(32) var aiCol, db, pFKey, pItem, pSrc, pTo, v, zCol, zDb, v7 uintptr var bIgnore, eAction, i, iDb, iFromCol, iJump, iReg, isIgnoreErrors, rcauth, v4, v6 int32 var _ /* aiCol at bp+16 */ uintptr var _ /* aiFree at bp+4 */ uintptr var _ /* iCol at bp+8 */ int32 var _ /* pIdx at bp+0 */ uintptr var _ /* pIdx at bp+12 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aiCol, bIgnore, db, eAction, i, iDb, iFromCol, iJump, iReg, isIgnoreErrors, pFKey, pItem, pSrc, pTo, rcauth, v, zCol, zDb, v4, v6, v7 db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Name of database containing pTab */ isIgnoreErrors = int32((*TParse)(unsafe.Pointer(pParse)).FdisableTriggers) /* Exactly one of regOld and regNew should be non-zero. */ _ = libc.Int32FromInt32(0) /* If foreign-keys are disabled, this function is a no-op. */ if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) == uint64(0) { return } if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) { return } iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName /* Loop through all the foreign key constraints for which pTab is the ** child table (the table that the foreign key definition is part of). */ pFKey = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpFKey for { if !(pFKey != 0) { break } /* Parent table of foreign key pFKey */ *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Index on key columns in pTo */ *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) bIgnore = 0 if aChange != 0 && Xsqlite3_stricmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName, (*TFKey)(unsafe.Pointer(pFKey)).FzTo) != 0 && _fkChildIsModified(tls, pTab, pFKey, aChange, bChngRowid) == 0 { goto _1 } /* Find the parent table of this foreign key. Also find a unique index ** on the parent key columns in the parent table. If either of these ** schema items cannot be located, set an error in pParse and return ** early. */ if (*TParse)(unsafe.Pointer(pParse)).FdisableTriggers != 0 { pTo = _sqlite3FindTable(tls, db, (*TFKey)(unsafe.Pointer(pFKey)).FzTo, zDb) } else { pTo = _sqlite3LocateTable(tls, pParse, uint32(0), (*TFKey)(unsafe.Pointer(pFKey)).FzTo, zDb) } if !(pTo != 0) || _sqlite3FkLocateIndex(tls, pParse, pTo, pFKey, bp, bp+4) != 0 { _ = libc.Int32FromInt32(0) if !(isIgnoreErrors != 0) || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return } if pTo == uintptr(0) { /* If isIgnoreErrors is true, then a table is being dropped. In this ** case SQLite runs a "DELETE FROM xxx" on the table being dropped ** before actually dropping it in order to check FK constraints. ** If the parent table of an FK constraint on the current table is ** missing, behave as if it is empty. i.e. decrement the relevant ** FK counter for each row of the current table with non-NULL keys. */ v = _sqlite3GetVdbe(tls, pParse) iJump = _sqlite3VdbeCurrentAddr(tls, v) + (*TFKey)(unsafe.Pointer(pFKey)).FnCol + int32(1) i = 0 for { if !(i < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) { break } iFromCol = (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(i)*8))).FiFrom iReg = int32(_sqlite3TableColumnToStorage(tls, (*TFKey)(unsafe.Pointer(pFKey)).FpFrom, int16(iFromCol))) + regOld + int32(1) _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), iReg, iJump) goto _2 _2: ; i++ } _sqlite3VdbeAddOp2(tls, v, int32(OP_FkCounter), int32((*TFKey)(unsafe.Pointer(pFKey)).FisDeferred), -int32(1)) } goto _1 } _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(bp + 4)) != 0 { aiCol = *(*uintptr)(unsafe.Pointer(bp + 4)) } else { *(*int32)(unsafe.Pointer(bp + 8)) = (*(*TsColMap)(unsafe.Pointer(pFKey + 36))).FiFrom aiCol = bp + 8 } i = 0 for { if !(i < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) { break } if *(*int32)(unsafe.Pointer(aiCol + uintptr(i)*4)) == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { *(*int32)(unsafe.Pointer(aiCol + uintptr(i)*4)) = -int32(1) } _ = libc.Int32FromInt32(0) /* Request permission to read the parent key columns. If the ** authorization callback returns SQLITE_IGNORE, behave as if any ** values read from the parent table are NULL. */ if (*Tsqlite3)(unsafe.Pointer(db)).FxAuth != 0 { if *(*uintptr)(unsafe.Pointer(bp)) != 0 { v4 = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaiColumn + uintptr(i)*2))) } else { v4 = int32((*TTable)(unsafe.Pointer(pTo)).FiPKey) } zCol = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTo)).FaCol + uintptr(v4)*12))).FzCnName rcauth = _sqlite3AuthReadCol(tls, pParse, (*TTable)(unsafe.Pointer(pTo)).FzName, zCol, iDb) bIgnore = libc.BoolInt32(rcauth == int32(SQLITE_IGNORE)) } goto _3 _3: ; i++ } /* Take a shared-cache advisory read-lock on the parent table. Allocate ** a cursor to use to search the unique index on the parent key columns ** in the parent table. */ _sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTo)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pTo)).FzName) (*TParse)(unsafe.Pointer(pParse)).FnTab++ if regOld != 0 { /* A row is being removed from the child table. Search for the parent. ** If the parent does not exist, removing the child row resolves an ** outstanding foreign key constraint violation. */ _fkLookupParent(tls, pParse, iDb, pTo, *(*uintptr)(unsafe.Pointer(bp)), pFKey, aiCol, regOld, -int32(1), bIgnore) } if regNew != 0 && !(_isSetNullAction(tls, pParse, pFKey) != 0) { /* A row is being added to the child table. If a parent row cannot ** be found, adding the child row has violated the FK constraint. ** ** If this operation is being performed as part of a trigger program ** that is actually a "SET NULL" action belonging to this very ** foreign key, then omit this scan altogether. As all child key ** values are guaranteed to be NULL, it is not possible for adding ** this row to cause an FK violation. */ _fkLookupParent(tls, pParse, iDb, pTo, *(*uintptr)(unsafe.Pointer(bp)), pFKey, aiCol, regNew, +libc.Int32FromInt32(1), bIgnore) } _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp + 4))) goto _1 _1: ; pFKey = (*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom } /* Loop through all the foreign key constraints that refer to this table. ** (the "child" constraints) */ pFKey = _sqlite3FkReferences(tls, pTab) for { if !(pFKey != 0) { break } *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) if aChange != 0 && _fkParentIsModified(tls, pTab, pFKey, aChange, bChngRowid) == 0 { goto _5 } if !((*TFKey)(unsafe.Pointer(pFKey)).FisDeferred != 0) && !((*Tsqlite3)(unsafe.Pointer(db)).Fflags&libc.Uint64FromInt32(SQLITE_DeferFKs) != 0) && !((*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0) && !((*TParse)(unsafe.Pointer(pParse)).FisMultiWrite != 0) { _ = libc.Int32FromInt32(0) /* Inserting a single row into a parent table cannot cause (or fix) ** an immediate foreign key violation. So do nothing in this case. */ goto _5 } if _sqlite3FkLocateIndex(tls, pParse, pTab, pFKey, bp+12, bp+16) != 0 { if !(isIgnoreErrors != 0) || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return } goto _5 } _ = libc.Int32FromInt32(0) /* Create a SrcList structure containing the child table. We need the ** child table as a SrcList for sqlite3WhereBegin() */ pSrc = _sqlite3SrcListAppend(tls, pParse, uintptr(0), uintptr(0), uintptr(0)) if pSrc != 0 { pItem = pSrc + 8 (*TSrcItem)(unsafe.Pointer(pItem)).FpTab = (*TFKey)(unsafe.Pointer(pFKey)).FpFrom (*TSrcItem)(unsafe.Pointer(pItem)).FzName = (*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FzName (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpTab)).FnTabRef++ v7 = pParse + 40 v6 = *(*int32)(unsafe.Pointer(v7)) *(*int32)(unsafe.Pointer(v7))++ (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor = v6 if regNew != 0 { _fkScanChildren(tls, pParse, pSrc, pTab, *(*uintptr)(unsafe.Pointer(bp + 12)), pFKey, *(*uintptr)(unsafe.Pointer(bp + 16)), regNew, -int32(1)) } if regOld != 0 { eAction = int32(*(*Tu8)(unsafe.Pointer(pFKey + 25 + libc.BoolUintptr(aChange != uintptr(0))))) if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00008))< int32(31) { v3 = uint32(0xffffffff) } else { v3 = libc.Uint32FromInt32(1) << (*(*TsColMap)(unsafe.Pointer(p + 36 + uintptr(i)*8))).FiFrom } mask |= v3 goto _2 _2: ; i++ } goto _1 _1: ; p = (*TFKey)(unsafe.Pointer(p)).FpNextFrom } p = _sqlite3FkReferences(tls, pTab) for { if !(p != 0) { break } *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) _sqlite3FkLocateIndex(tls, pParse, pTab, p, bp, uintptr(0)) if *(*uintptr)(unsafe.Pointer(bp)) != 0 { i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnKeyCol)) { break } _ = libc.Int32FromInt32(0) if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaiColumn + uintptr(i)*2))) > int32(31) { v6 = uint32(0xffffffff) } else { v6 = libc.Uint32FromInt32(1) << *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaiColumn + uintptr(i)*2)) } mask |= v6 goto _5 _5: ; i++ } } goto _4 _4: ; p = (*TFKey)(unsafe.Pointer(p)).FpNextTo } } return mask } // C documentation // // /* // ** This function is called before generating code to update or delete a // ** row contained in table pTab. If the operation is a DELETE, then // ** parameter aChange is passed a NULL value. For an UPDATE, aChange points // ** to an array of size N, where N is the number of columns in table pTab. // ** If the i'th column is not modified by the UPDATE, then the corresponding // ** entry in the aChange[] array is set to -1. If the column is modified, // ** the value is 0 or greater. Parameter chngRowid is set to true if the // ** UPDATE statement modifies the rowid fields of the table. // ** // ** If any foreign key processing will be required, this function returns // ** non-zero. If there is no foreign key related processing, this function // ** returns zero. // ** // ** For an UPDATE, this function returns 2 if: // ** // ** * There are any FKs for which pTab is the child and the parent table // ** and any FK processing at all is required (even of a different FK), or // ** // ** * the UPDATE modifies one or more parent keys for which the action is // ** not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL). // ** // ** Or, assuming some other foreign key processing is required, 1. // */ func _sqlite3FkRequired(tls *libc.TLS, pParse uintptr, pTab uintptr, aChange uintptr, chngRowid int32) (r int32) { var bHaveFK, eRet, v3 int32 var p uintptr _, _, _, _ = bHaveFK, eRet, p, v3 eRet = int32(1) /* Value to return if bHaveFK is true */ bHaveFK = 0 /* If FK processing is required */ if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&uint64(SQLITE_ForeignKeys) != 0 && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == TABTYP_NORM { if !(aChange != 0) { /* A DELETE operation. Foreign key processing is required if the ** table in question is either the child or parent table for any ** foreign key constraint. */ bHaveFK = libc.BoolInt32(_sqlite3FkReferences(tls, pTab) != 0 || (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpFKey != 0) } else { /* Check if any child key columns are being modified. */ p = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpFKey for { if !(p != 0) { break } if _fkChildIsModified(tls, pTab, p, aChange, chngRowid) != 0 { if 0 == Xsqlite3_stricmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName, (*TFKey)(unsafe.Pointer(p)).FzTo) { eRet = int32(2) } bHaveFK = int32(1) } goto _1 _1: ; p = (*TFKey)(unsafe.Pointer(p)).FpNextFrom } /* Check if any parent key columns are being modified. */ p = _sqlite3FkReferences(tls, pTab) for { if !(p != 0) { break } if _fkParentIsModified(tls, pTab, p, aChange, chngRowid) != 0 { if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&(uint64(libc.Int32FromInt32(0x00008))< parent key cols */ pStep = uintptr(0) /* First (only) step of trigger program */ pWhere = uintptr(0) /* WHERE clause of trigger step */ pList = uintptr(0) /* Changes list if ON UPDATE CASCADE */ pSelect = uintptr(0) /* Iterator variable */ pWhen = uintptr(0) /* WHEN clause for the trigger */ if _sqlite3FkLocateIndex(tls, pParse, pTab, pFKey, bp, bp+4) != 0 { return uintptr(0) } _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) { break } *(*TToken)(unsafe.Pointer(bp + 8)) = TToken{ Fz: __ccgo_ts + 6596, Fn: uint32(3), } /* Literal "old" token */ *(*TToken)(unsafe.Pointer(bp + 16)) = TToken{ Fz: __ccgo_ts + 6592, Fn: uint32(3), } /* tFromCol = OLD.tToCol */ if *(*uintptr)(unsafe.Pointer(bp + 4)) != 0 { v2 = *(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)) + uintptr(i)*4)) } else { v2 = (*(*TsColMap)(unsafe.Pointer(pFKey + 36))).FiFrom } iFromCol = v2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(bp)) != 0 { v3 = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaiColumn + uintptr(i)*2))) } else { v3 = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) } _sqlite3TokenInit(tls, bp+32, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(v3)*12))).FzCnName) _sqlite3TokenInit(tls, bp+24, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FaCol + uintptr(iFromCol)*12))).FzCnName) /* Create the expression "OLD.zToCol = zFromCol". It is important ** that the "OLD.zToCol" term is on the LHS of the = operator, so ** that the affinity and collation sequence associated with the ** parent table are used for the comparison. */ pEq = _sqlite3PExpr(tls, pParse, int32(TK_EQ), _sqlite3PExpr(tls, pParse, int32(TK_DOT), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+8, 0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+32, 0)), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+24, 0)) pWhere = _sqlite3ExprAnd(tls, pParse, pWhere, pEq) /* For ON UPDATE, construct the next term of the WHEN clause. ** The final WHEN clause will be like this: ** ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN) */ if pChanges != 0 { pEq = _sqlite3PExpr(tls, pParse, int32(TK_IS), _sqlite3PExpr(tls, pParse, int32(TK_DOT), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+8, 0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+32, 0)), _sqlite3PExpr(tls, pParse, int32(TK_DOT), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+16, 0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+32, 0))) pWhen = _sqlite3ExprAnd(tls, pParse, pWhen, pEq) } if action != int32(OE_Restrict) && (action != int32(OE_Cascade) || pChanges != 0) { if action == int32(OE_Cascade) { pNew = _sqlite3PExpr(tls, pParse, int32(TK_DOT), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+16, 0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+32, 0)) } else { if action == int32(OE_SetDflt) { pCol = (*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FaCol + uintptr(iFromCol)*12 if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { pDflt = uintptr(0) } else { pDflt = _sqlite3ColumnExpr(tls, (*TFKey)(unsafe.Pointer(pFKey)).FpFrom, pCol) } if pDflt != 0 { pNew = _sqlite3ExprDup(tls, db, pDflt, 0) } else { pNew = _sqlite3ExprAlloc(tls, db, int32(TK_NULL), uintptr(0), 0) } } else { pNew = _sqlite3ExprAlloc(tls, db, int32(TK_NULL), uintptr(0), 0) } } pList = _sqlite3ExprListAppend(tls, pParse, pList, pNew) _sqlite3ExprListSetName(tls, pParse, pList, bp+24, 0) } goto _1 _1: ; i++ } _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp + 4))) zFrom = (*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FzName nFrom = _sqlite3Strlen30(tls, zFrom) if action == int32(OE_Restrict) { iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) pRaise = _sqlite3Expr(tls, db, int32(TK_RAISE), __ccgo_ts+5225) if pRaise != 0 { (*TExpr)(unsafe.Pointer(pRaise)).FaffExpr = uint8(OE_Abort) } pSrc = _sqlite3SrcListAppend(tls, pParse, uintptr(0), uintptr(0), uintptr(0)) if pSrc != 0 { _ = libc.Int32FromInt32(0) (*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FzName = _sqlite3DbStrDup(tls, db, zFrom) (*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FzDatabase = _sqlite3DbStrDup(tls, db, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName) } pSelect = _sqlite3SelectNew(tls, pParse, _sqlite3ExprListAppend(tls, pParse, uintptr(0), pRaise), pSrc, pWhere, uintptr(0), uintptr(0), uintptr(0), uint32(0), uintptr(0)) pWhere = uintptr(0) } /* Disable lookaside memory allocation */ (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable++ (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0) pTrigger = _sqlite3DbMallocZero(tls, db, uint64(libc.Uint32FromInt64(36)+libc.Uint32FromInt64(48)+uint32(nFrom)+uint32(1))) if pTrigger != 0 { v4 = pTrigger + 1*36 (*TTrigger)(unsafe.Pointer(pTrigger)).Fstep_list = v4 pStep = v4 (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget = pStep + 1*48 libc.Xmemcpy(tls, (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget, zFrom, uint32(nFrom)) (*TTriggerStep)(unsafe.Pointer(pStep)).FpWhere = _sqlite3ExprDup(tls, db, pWhere, int32(EXPRDUP_REDUCE)) (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList = _sqlite3ExprListDup(tls, db, pList, int32(EXPRDUP_REDUCE)) (*TTriggerStep)(unsafe.Pointer(pStep)).FpSelect = _sqlite3SelectDup(tls, db, pSelect, int32(EXPRDUP_REDUCE)) if pWhen != 0 { pWhen = _sqlite3PExpr(tls, pParse, int32(TK_NOT), pWhen, uintptr(0)) (*TTrigger)(unsafe.Pointer(pTrigger)).FpWhen = _sqlite3ExprDup(tls, db, pWhen, int32(EXPRDUP_REDUCE)) } } /* Re-enable the lookaside buffer, if it was disabled earlier. */ (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable-- if (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable != 0 { v5 = 0 } else { v5 = int32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue) } (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(v5) _sqlite3ExprDelete(tls, db, pWhere) _sqlite3ExprDelete(tls, db, pWhen) _sqlite3ExprListDelete(tls, db, pList) _sqlite3SelectDelete(tls, db, pSelect) if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == int32(1) { _fkTriggerDelete(tls, db, pTrigger) return uintptr(0) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) switch action { case int32(OE_Restrict): (*TTriggerStep)(unsafe.Pointer(pStep)).Fop = uint8(TK_SELECT) case int32(OE_Cascade): if !(pChanges != 0) { (*TTriggerStep)(unsafe.Pointer(pStep)).Fop = uint8(TK_DELETE) break } fallthrough default: (*TTriggerStep)(unsafe.Pointer(pStep)).Fop = uint8(TK_UPDATE) } (*TTriggerStep)(unsafe.Pointer(pStep)).FpTrig = pTrigger (*TTrigger)(unsafe.Pointer(pTrigger)).FpSchema = (*TTable)(unsafe.Pointer(pTab)).FpSchema (*TTrigger)(unsafe.Pointer(pTrigger)).FpTabSchema = (*TTable)(unsafe.Pointer(pTab)).FpSchema *(*uintptr)(unsafe.Pointer(pFKey + 28 + uintptr(iAction)*4)) = pTrigger if pChanges != 0 { v6 = int32(TK_UPDATE) } else { v6 = int32(TK_DELETE) } (*TTrigger)(unsafe.Pointer(pTrigger)).Fop = uint8(v6) } return pTrigger } // C documentation // // /* // ** This function is called when deleting or updating a row to implement // ** any required CASCADE, SET NULL or SET DEFAULT actions. // */ func _sqlite3FkActions(tls *libc.TLS, pParse uintptr, pTab uintptr, pChanges uintptr, regOld int32, aChange uintptr, bChngRowid int32) { var pAct, pFKey uintptr _, _ = pAct, pFKey /* If foreign-key support is enabled, iterate through all FKs that ** refer to table pTab. If there is an action associated with the FK ** for this operation (either update or delete), invoke the associated ** trigger sub-program. */ if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&uint64(SQLITE_ForeignKeys) != 0 { /* Iterator variable */ pFKey = _sqlite3FkReferences(tls, pTab) for { if !(pFKey != 0) { break } if aChange == uintptr(0) || _fkParentIsModified(tls, pTab, pFKey, aChange, bChngRowid) != 0 { pAct = _fkActionTrigger(tls, pParse, pTab, pFKey, pChanges) if pAct != 0 { _sqlite3CodeRowTriggerDirect(tls, pParse, pAct, pTab, regOld, int32(OE_Abort), 0) } } goto _1 _1: ; pFKey = (*TFKey)(unsafe.Pointer(pFKey)).FpNextTo } } } // C documentation // // /* // ** Free all memory associated with foreign key definitions attached to // ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash // ** hash table. // */ func _sqlite3FkDelete(tls *libc.TLS, db uintptr, pTab uintptr) { var pFKey, pNext, z, v2 uintptr _, _, _, _ = pFKey, pNext, z, v2 /* Copy of pFKey->pNextFrom */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pFKey = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 44))).FpFKey for { if !(pFKey != 0) { break } _ = libc.Int32FromInt32(0) /* Remove the FK from the fkeyHash hash table. */ if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0) { if (*TFKey)(unsafe.Pointer(pFKey)).FpPrevTo != 0 { (*TFKey)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpPrevTo)).FpNextTo = (*TFKey)(unsafe.Pointer(pFKey)).FpNextTo } else { if (*TFKey)(unsafe.Pointer(pFKey)).FpNextTo != 0 { v2 = (*TFKey)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpNextTo)).FzTo } else { v2 = (*TFKey)(unsafe.Pointer(pFKey)).FzTo } z = v2 _sqlite3HashInsert(tls, (*TTable)(unsafe.Pointer(pTab)).FpSchema+56, z, (*TFKey)(unsafe.Pointer(pFKey)).FpNextTo) } if (*TFKey)(unsafe.Pointer(pFKey)).FpNextTo != 0 { (*TFKey)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpNextTo)).FpPrevTo = (*TFKey)(unsafe.Pointer(pFKey)).FpPrevTo } } /* EV: R-30323-21917 Each foreign key constraint in SQLite is ** classified as either immediate or deferred. */ _ = libc.Int32FromInt32(0) /* Delete any triggers created to implement actions for this FK. */ _fkTriggerDelete(tls, db, *(*uintptr)(unsafe.Pointer(pFKey + 28))) _fkTriggerDelete(tls, db, *(*uintptr)(unsafe.Pointer(pFKey + 28 + 1*4))) pNext = (*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom _sqlite3DbFree(tls, db, pFKey) goto _1 _1: ; pFKey = pNext } } /************** End of fkey.c ************************************************/ /************** Begin file insert.c ******************************************/ /* ** 2001 September 15 ** ** 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 contains C code routines that are called by the parser ** to handle INSERT statements in SQLite. */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** Generate code that will // ** // ** (1) acquire a lock for table pTab then // ** (2) open pTab as cursor iCur. // ** // ** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index // ** for that table that is actually opened. // */ func _sqlite3OpenTable(tls *libc.TLS, pParse uintptr, iCur int32, iDb int32, pTab uintptr, opcode int32) { var pPk, v uintptr var v1 int32 _, _, _ = pPk, v, v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) if !((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FnoSharedCache != 0) { if opcode == int32(OP_OpenWrite) { v1 = int32(1) } else { v1 = 0 } _sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab)).Ftnum, uint8(v1), (*TTable)(unsafe.Pointer(pTab)).FzName) } if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { _sqlite3VdbeAddOp4Int(tls, v, opcode, iCur, int32((*TTable)(unsafe.Pointer(pTab)).Ftnum), iDb, int32((*TTable)(unsafe.Pointer(pTab)).FnNVCol)) } else { pPk = _sqlite3PrimaryKeyIndex(tls, pTab) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, opcode, iCur, int32((*TIndex)(unsafe.Pointer(pPk)).Ftnum), iDb) _sqlite3VdbeSetP4KeyInfo(tls, pParse, pPk) } } // C documentation // // /* // ** Return a pointer to the column affinity string associated with index // ** pIdx. A column affinity string has one character for each column in // ** the table, according to the affinity of the column: // ** // ** Character Column affinity // ** ------------------------------ // ** 'A' BLOB // ** 'B' TEXT // ** 'C' NUMERIC // ** 'D' INTEGER // ** 'F' REAL // ** // ** An extra 'D' is appended to the end of the string to cover the // ** rowid that appears as the last column in every index. // ** // ** Memory for the buffer containing the column index affinity string // ** is managed along with the rest of the Index structure. It will be // ** released when sqlite3DeleteIndex() is called. // */ func _computeIndexAffStr(tls *libc.TLS, db uintptr, pIdx uintptr) (r uintptr) { var aff uint8 var n int32 var pTab uintptr var x Ti16 _, _, _, _ = aff, n, pTab, x pTab = (*TIndex)(unsafe.Pointer(pIdx)).FpTable (*TIndex)(unsafe.Pointer(pIdx)).FzColAff = _sqlite3DbMallocRaw(tls, uintptr(0), uint64(int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)+int32(1))) if !((*TIndex)(unsafe.Pointer(pIdx)).FzColAff != 0) { _sqlite3OomFault(tls, db) return uintptr(0) } n = 0 for { if !(n < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) { break } x = *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(n)*2)) if int32(x) >= 0 { aff = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(x)*12))).Faffinity } else { if int32(x) == -int32(1) { aff = uint8(SQLITE_AFF_INTEGER) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) aff = _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(n)*20))).FpExpr) } } if int32(aff) < int32(SQLITE_AFF_BLOB) { aff = uint8(SQLITE_AFF_BLOB) } if int32(aff) > int32(SQLITE_AFF_NUMERIC) { aff = uint8(SQLITE_AFF_NUMERIC) } *(*uint8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FzColAff + uintptr(n))) = aff goto _1 _1: ; n++ } *(*uint8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FzColAff + uintptr(n))) = uint8(0) return (*TIndex)(unsafe.Pointer(pIdx)).FzColAff } func _sqlite3IndexAffinityStr(tls *libc.TLS, db uintptr, pIdx uintptr) (r uintptr) { if !((*TIndex)(unsafe.Pointer(pIdx)).FzColAff != 0) { return _computeIndexAffStr(tls, db, pIdx) } return (*TIndex)(unsafe.Pointer(pIdx)).FzColAff } // C documentation // // /* // ** Compute an affinity string for a table. Space is obtained // ** from sqlite3DbMalloc(). The caller is responsible for freeing // ** the space when done. // */ func _sqlite3TableAffinityStr(tls *libc.TLS, db uintptr, pTab uintptr) (r uintptr) { var i, j, v2, v3, v4 int32 var zColAff uintptr _, _, _, _, _, _ = i, j, zColAff, v2, v3, v4 zColAff = _sqlite3DbMallocRaw(tls, db, uint64(int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+int32(1))) if zColAff != 0 { v2 = libc.Int32FromInt32(0) j = v2 i = v2 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 { v3 = j j++ *(*uint8)(unsafe.Pointer(zColAff + uintptr(v3))) = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).Faffinity } goto _1 _1: ; i++ } for cond := true; cond; cond = j >= 0 && int32(*(*uint8)(unsafe.Pointer(zColAff + uintptr(j)))) <= int32(SQLITE_AFF_BLOB) { v4 = j j-- *(*uint8)(unsafe.Pointer(zColAff + uintptr(v4))) = uint8(0) } } return zColAff } // C documentation // // /* // ** Make changes to the evolving bytecode to do affinity transformations // ** of values that are about to be gathered into a row for table pTab. // ** // ** For ordinary (legacy, non-strict) tables: // ** ----------------------------------------- // ** // ** Compute the affinity string for table pTab, if it has not already been // ** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities. // ** // ** If the affinity string is empty (because it was all SQLITE_AFF_BLOB entries // ** which were then optimized out) then this routine becomes a no-op. // ** // ** Otherwise if iReg>0 then code an OP_Affinity opcode that will set the // ** affinities for register iReg and following. Or if iReg==0, // ** then just set the P4 operand of the previous opcode (which should be // ** an OP_MakeRecord) to the affinity string. // ** // ** A column affinity string has one character per column: // ** // ** Character Column affinity // ** --------- --------------- // ** 'A' BLOB // ** 'B' TEXT // ** 'C' NUMERIC // ** 'D' INTEGER // ** 'E' REAL // ** // ** For STRICT tables: // ** ------------------ // ** // ** Generate an appropriate OP_TypeCheck opcode that will verify the // ** datatypes against the column definitions in pTab. If iReg==0, that // ** means an OP_MakeRecord opcode has already been generated and should be // ** the last opcode generated. The new OP_TypeCheck needs to be inserted // ** before the OP_MakeRecord. The new OP_TypeCheck should use the same // ** register set as the OP_MakeRecord. If iReg>0 then register iReg is // ** the first of a series of registers that will form the new record. // ** Apply the type checking to that array of registers. // */ func _sqlite3TableAffinity(tls *libc.TLS, v uintptr, pTab uintptr, iReg int32) { var i int32 var pPrev, zColAff uintptr _, _, _ = i, pPrev, zColAff if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Strict) != 0 { if iReg == 0 { _sqlite3VdbeAppendP4(tls, v, pTab, -int32(5)) pPrev = _sqlite3VdbeGetLastOp(tls, v) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TVdbeOp)(unsafe.Pointer(pPrev)).Fopcode = uint8(OP_TypeCheck) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), (*TVdbeOp)(unsafe.Pointer(pPrev)).Fp1, (*TVdbeOp)(unsafe.Pointer(pPrev)).Fp2, (*TVdbeOp)(unsafe.Pointer(pPrev)).Fp3) } else { /* Insert an isolated OP_Typecheck */ _sqlite3VdbeAddOp2(tls, v, int32(OP_TypeCheck), iReg, int32((*TTable)(unsafe.Pointer(pTab)).FnNVCol)) _sqlite3VdbeAppendP4(tls, v, pTab, -int32(5)) } return } zColAff = (*TTable)(unsafe.Pointer(pTab)).FzColAff if zColAff == uintptr(0) { zColAff = _sqlite3TableAffinityStr(tls, uintptr(0), pTab) if !(zColAff != 0) { _sqlite3OomFault(tls, _sqlite3VdbeDb(tls, v)) return } (*TTable)(unsafe.Pointer(pTab)).FzColAff = zColAff } _ = libc.Int32FromInt32(0) i = int32(libc.Xstrlen(tls, zColAff) & libc.Uint32FromInt32(0x3fffffff)) if i != 0 { if iReg != 0 { _sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), iReg, i, 0, zColAff, i) } else { _ = libc.Int32FromInt32(0) _sqlite3VdbeChangeP4(tls, v, -int32(1), zColAff, i) } } } // C documentation // // /* // ** Return non-zero if the table pTab in database iDb or any of its indices // ** have been opened at any point in the VDBE program. This is used to see if // ** a statement of the form "INSERT INTO SELECT ..." can // ** run without using a temporary table for the results of the SELECT. // */ func _readsTable(tls *libc.TLS, p uintptr, iDb int32, pTab uintptr) (r int32) { var i, iEnd int32 var pIndex, pOp, pVTab, v, v1 uintptr var tnum TPgno _, _, _, _, _, _, _, _ = i, iEnd, pIndex, pOp, pVTab, tnum, v, v1 v = _sqlite3GetVdbe(tls, p) iEnd = _sqlite3VdbeCurrentAddr(tls, v) if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { v1 = _sqlite3GetVTable(tls, (*TParse)(unsafe.Pointer(p)).Fdb, pTab) } else { v1 = uintptr(0) } pVTab = v1 i = int32(1) for { if !(i < iEnd) { break } pOp = _sqlite3VdbeGetOp(tls, v, i) _ = libc.Int32FromInt32(0) if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_OpenRead) && (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 == iDb { tnum = uint32((*TVdbeOp)(unsafe.Pointer(pOp)).Fp2) if tnum == (*TTable)(unsafe.Pointer(pTab)).Ftnum { return int32(1) } pIndex = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIndex != 0) { break } if tnum == (*TIndex)(unsafe.Pointer(pIndex)).Ftnum { return int32(1) } goto _3 _3: ; pIndex = (*TIndex)(unsafe.Pointer(pIndex)).FpNext } } if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_VOpen) && *(*uintptr)(unsafe.Pointer(pOp + 16)) == pVTab { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return int32(1) } goto _2 _2: ; i++ } return 0 } // C documentation // // /* This walker callback will compute the union of colFlags flags for all // ** referenced columns in a CHECK constraint or generated column expression. // */ func _exprColumnFlagUnion(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var p1 uintptr _ = p1 if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) >= 0 { _ = libc.Int32FromInt32(0) p1 = pWalker + 20 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | int32((*(*TColumn)(unsafe.Pointer((*TTable1)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalker + 24)))).FaCol + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)*12))).FcolFlags)) } return WRC_Continue } // C documentation // // /* // ** All regular columns for table pTab have been puts into registers // ** starting with iRegStore. The registers that correspond to STORED // ** or VIRTUAL columns have not yet been initialized. This routine goes // ** back and computes the values for those columns based on the previously // ** computed normal columns. // */ func _sqlite3ComputeGeneratedColumns(tls *libc.TLS, pParse uintptr, iRegStore int32, pTab uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var eProgress, i, ii, jj, x, v2 int32 var pCol, pOp, pRedo, zP4, p4, p6, p7, p8 uintptr var _ /* w at bp+0 */ TWalker _, _, _, _, _, _, _, _, _, _, _, _, _, _ = eProgress, i, ii, jj, pCol, pOp, pRedo, x, zP4, v2, p4, p6, p7, p8 _ = libc.Int32FromInt32(0) /* Before computing generated columns, first go through and make sure ** that appropriate affinity has been applied to the regular columns */ _sqlite3TableAffinity(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, pTab, iRegStore) if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasStored) != uint32(0) { pOp = _sqlite3VdbeGetLastOp(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe) if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Affinity) { zP4 = *(*uintptr)(unsafe.Pointer(pOp + 16)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) v2 = libc.Int32FromInt32(0) jj = v2 ii = v2 for { if !(*(*uint8)(unsafe.Pointer(zP4 + uintptr(jj))) != 0) { break } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(ii)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 { goto _1 } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(ii)*12))).FcolFlags)&int32(COLFLAG_STORED) != 0 { *(*uint8)(unsafe.Pointer(zP4 + uintptr(jj))) = uint8(SQLITE_AFF_NONE) } jj++ goto _1 _1: ; ii++ } } else { if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_TypeCheck) { /* If an OP_TypeCheck was generated because the table is STRICT, ** then set the P3 operand to indicate that generated columns should ** not be checked */ (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 = int32(1) } } } /* Because there can be multiple generated columns that refer to one another, ** this is a two-pass algorithm. On the first pass, mark all generated ** columns as "not available". */ i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { p4 = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12 + 10 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | libc.Int32FromInt32(COLFLAG_NOTAVAIL)) } goto _3 _3: ; i++ } *(*uintptr)(unsafe.Pointer(bp + 24)) = pTab (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_exprColumnFlagUnion) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = uintptr(0) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback2 = uintptr(0) /* On the second pass, compute the value of each NOT-AVAILABLE column. ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as ** they are needed. */ (*TParse)(unsafe.Pointer(pParse)).FiSelfTab = -iRegStore for cond := true; cond; cond = pRedo != 0 && eProgress != 0 { eProgress = 0 pRedo = uintptr(0) i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12 if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_NOTAVAIL) != 0 { p6 = pCol + 10 *(*Tu16)(unsafe.Pointer(p6)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p6))) | libc.Int32FromInt32(COLFLAG_BUSY)) (*(*TWalker)(unsafe.Pointer(bp))).FeCode = uint16(0) _sqlite3WalkExpr(tls, bp, _sqlite3ColumnExpr(tls, pTab, pCol)) p7 = pCol + 10 *(*Tu16)(unsafe.Pointer(p7)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p7))) & ^libc.Int32FromInt32(COLFLAG_BUSY)) if int32((*(*TWalker)(unsafe.Pointer(bp))).FeCode)&int32(COLFLAG_NOTAVAIL) != 0 { pRedo = pCol goto _5 } eProgress = int32(1) _ = libc.Int32FromInt32(0) x = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(i))) + iRegStore _sqlite3ExprCodeGeneratedColumn(tls, pParse, pTab, pCol, x) p8 = pCol + 10 *(*Tu16)(unsafe.Pointer(p8)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p8))) & ^libc.Int32FromInt32(COLFLAG_NOTAVAIL)) } goto _5 _5: ; i++ } } if pRedo != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8162, libc.VaList(bp+40, (*TColumn)(unsafe.Pointer(pRedo)).FzCnName)) } (*TParse)(unsafe.Pointer(pParse)).FiSelfTab = 0 } // C documentation // // /* // ** Locate or create an AutoincInfo structure associated with table pTab // ** which is in database iDb. Return the register number for the register // ** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT // ** table. (Also return zero when doing a VACUUM since we do not want to // ** update the AUTOINCREMENT counters during a VACUUM.) // ** // ** There is at most one AutoincInfo structure per table even if the // ** same table is autoincremented multiple times due to inserts within // ** triggers. A new AutoincInfo structure is created if this is the // ** first use of table pTab. On 2nd and subsequent uses, the original // ** AutoincInfo structure is used. // ** // ** Four consecutive registers are allocated: // ** // ** (1) The name of the pTab table. // ** (2) The maximum ROWID of pTab. // ** (3) The rowid in sqlite_sequence of pTab // ** (4) The original value of the max ROWID in pTab, or NULL if none // ** // ** The 2nd register is the one that is returned. That is all the // ** insert routine needs to know about. // */ func _autoIncBegin(tls *libc.TLS, pParse uintptr, iDb int32, pTab uintptr) (r int32) { var memId, v2 int32 var pInfo, pSeqTab, pToplevel, v1, v3 uintptr _, _, _, _, _, _, _ = memId, pInfo, pSeqTab, pToplevel, v1, v2, v3 memId = 0 /* Register holding maximum rowid */ _ = libc.Int32FromInt32(0) if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Autoincrement) != uint32(0) && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmDbFlags&uint32(DBFLAG_Vacuum) == uint32(0) { if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 { v1 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel } else { v1 = pParse } pToplevel = v1 pSeqTab = (*TSchema)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + uintptr(iDb)*16))).FpSchema)).FpSeqTab /* Verify that the sqlite_sequence table exists and is an ordinary ** rowid table with exactly two columns. ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */ if pSeqTab == uintptr(0) || !((*TTable)(unsafe.Pointer(pSeqTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) || int32((*TTable)(unsafe.Pointer(pSeqTab)).FeTabType) == int32(TABTYP_VTAB) || int32((*TTable)(unsafe.Pointer(pSeqTab)).FnCol) != int32(2) { (*TParse)(unsafe.Pointer(pParse)).FnErr++ (*TParse)(unsafe.Pointer(pParse)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(2)< 0 { _sqlite3VdbeAddOp2(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(OP_MemMax), memId, regRowid) } } // C documentation // // /* // ** This routine generates the code needed to write autoincrement // ** maximum rowid values back into the sqlite_sequence register. // ** Every statement that might do an INSERT into an autoincrement // ** table (either directly or through triggers) needs to call this // ** routine just before the "exit" code. // */ func _autoIncrementEnd(tls *libc.TLS, pParse uintptr) { var aOp, db, p, pDb, v uintptr var iRec, memId int32 _, _, _, _, _, _, _ = aOp, db, iRec, memId, p, pDb, v v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) p = (*TParse)(unsafe.Pointer(pParse)).FpAinc for { if !(p != 0) { break } pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TAutoincInfo)(unsafe.Pointer(p)).FiDb)*16 memId = (*TAutoincInfo)(unsafe.Pointer(p)).FregCtr iRec = _sqlite3GetTempReg(tls, pParse) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, int32(OP_Le), memId+int32(2), _sqlite3VdbeCurrentAddr(tls, v)+int32(7), memId) _sqlite3OpenTable(tls, pParse, 0, (*TAutoincInfo)(unsafe.Pointer(p)).FiDb, (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).FpSeqTab, int32(OP_OpenWrite)) aOp = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(20)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_autoIncEnd)), _iLn2) if aOp == uintptr(0) { break } (*(*TVdbeOp)(unsafe.Pointer(aOp))).Fp1 = memId + int32(1) (*(*TVdbeOp)(unsafe.Pointer(aOp + 1*20))).Fp2 = memId + int32(1) (*(*TVdbeOp)(unsafe.Pointer(aOp + 2*20))).Fp1 = memId - int32(1) (*(*TVdbeOp)(unsafe.Pointer(aOp + 2*20))).Fp3 = iRec (*(*TVdbeOp)(unsafe.Pointer(aOp + 3*20))).Fp2 = iRec (*(*TVdbeOp)(unsafe.Pointer(aOp + 3*20))).Fp3 = memId + int32(1) (*(*TVdbeOp)(unsafe.Pointer(aOp + 3*20))).Fp5 = uint16(OPFLAG_APPEND) _sqlite3ReleaseTempReg(tls, pParse, iRec) goto _1 _1: ; p = (*TAutoincInfo)(unsafe.Pointer(p)).FpNext } } var _iLn2 int32 var _autoIncEnd = [5]TVdbeOpList{ 0: { Fopcode: uint8(OP_NotNull), Fp2: int8(2), }, 1: { Fopcode: uint8(OP_NewRowid), }, 2: { Fopcode: uint8(OP_MakeRecord), Fp2: int8(2), }, 3: { Fopcode: uint8(OP_Insert), }, 4: { Fopcode: uint8(OP_Close), }, } func _sqlite3AutoincrementEnd(tls *libc.TLS, pParse uintptr) { if (*TParse)(unsafe.Pointer(pParse)).FpAinc != 0 { _autoIncrementEnd(tls, pParse) } } // C documentation // // /* // ** This routine is called to handle SQL of the following forms: // ** // ** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),... // ** insert into TABLE (IDLIST) select // ** insert into TABLE (IDLIST) default values // ** // ** The IDLIST following the table name is always optional. If omitted, // ** then a list of all (non-hidden) columns for the table is substituted. // ** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST // ** is omitted. // ** // ** For the pSelect parameter holds the values to be inserted for the // ** first two forms shown above. A VALUES clause is really just short-hand // ** for a SELECT statement that omits the FROM clause and everything else // ** that follows. If the pSelect parameter is NULL, that means that the // ** DEFAULT VALUES form of the INSERT statement is intended. // ** // ** The code generated follows one of four templates. For a simple // ** insert with data coming from a single-row VALUES clause, the code executes // ** once straight down through. Pseudo-code follows (we call this // ** the "1st template"): // ** // ** open write cursor to
    and its indices // ** put VALUES clause expressions into registers // ** write the resulting record into
    // ** cleanup // ** // ** The three remaining templates assume the statement is of the form // ** // ** INSERT INTO
    SELECT ... // ** // ** If the SELECT clause is of the restricted form "SELECT * FROM " - // ** in other words if the SELECT pulls all columns from a single table // ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and // ** if and are distinct tables but have identical // ** schemas, including all the same indices, then a special optimization // ** is invoked that copies raw records from over to . // ** See the xferOptimization() function for the implementation of this // ** template. This is the 2nd template. // ** // ** open a write cursor to
    // ** open read cursor on // ** transfer all records in over to
    // ** close cursors // ** foreach index on
    // ** open a write cursor on the
    index // ** open a read cursor on the corresponding index // ** transfer all records from the read to the write cursors // ** close cursors // ** end foreach // ** // ** The 3rd template is for when the second template does not apply // ** and the SELECT clause does not read from
    at any time. // ** The generated code follows this template: // ** // ** X <- A // ** goto B // ** A: setup for the SELECT // ** loop over the rows in the SELECT // ** load values into registers R..R+n // ** yield X // ** end loop // ** cleanup after the SELECT // ** end-coroutine X // ** B: open write cursor to
    and its indices // ** C: yield X, at EOF goto D // ** insert the select result into
    from R..R+n // ** goto C // ** D: cleanup // ** // ** The 4th template is used if the insert statement takes its // ** values from a SELECT but the data is being inserted into a table // ** that is also read as part of the SELECT. In the third form, // ** we have to use an intermediate table to store the results of // ** the select. The template is like this: // ** // ** X <- A // ** goto B // ** A: setup for the SELECT // ** loop over the tables in the SELECT // ** load value into register R..R+n // ** yield X // ** end loop // ** cleanup after the SELECT // ** end co-routine R // ** B: open temp table // ** L: yield X, at EOF goto M // ** insert row from R..R+n into temp table // ** goto L // ** M: open write cursor to
    and its indices // ** rewind temp table // ** C: loop over rows of intermediate table // ** transfer values form intermediate table into
    // ** end loop // ** D: cleanup // */ func _sqlite3Insert(tls *libc.TLS, pParse uintptr, pTabList uintptr, pSelect uintptr, pColumn uintptr, onError int32, pUpsert uintptr) { bp := tls.Alloc(112) defer tls.Free(112) var aRegIdx, db, pIdx, pIpk, pList, pNx, pTab, pTrigger, pVTab, pX, v, v13, v16, v18, v6, v9 uintptr var addr1, addr11, addrCont, addrInsTop, addrL, addrTop, bUseSeek, endOfLoop, i, iDb, iRegStore, ipkColumn, isView, j, k, nColumn, nHidden, nIdx, rc, regAutoinc, regCols, regData, regFromSelect, regIns, regRec, regRowCount, regRowid, regTempRowid, regYield, srcTab, y, v1, v12, v15, v17, v19, v23, v24, v5, v7, v8 int32 var appendFlag, bIdListInOrder, useTempTable, withoutRowid Tu8 var colFlags, v21 Tu32 var _ /* dest at bp+8 */ TSelectDest var _ /* iDataCur at bp+0 */ int32 var _ /* iIdxCur at bp+4 */ int32 var _ /* isReplace at bp+76 */ int32 var _ /* sNC at bp+40 */ TNameContext var _ /* tmask at bp+36 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aRegIdx, addr1, addr11, addrCont, addrInsTop, addrL, addrTop, appendFlag, bIdListInOrder, bUseSeek, colFlags, db, endOfLoop, i, iDb, iRegStore, ipkColumn, isView, j, k, nColumn, nHidden, nIdx, pIdx, pIpk, pList, pNx, pTab, pTrigger, pVTab, pX, rc, regAutoinc, regCols, regData, regFromSelect, regIns, regRec, regRowCount, regRowid, regTempRowid, regYield, srcTab, useTempTable, v, withoutRowid, y, v1, v12, v13, v15, v16, v17, v18, v19, v21, v23, v24, v5, v6, v7, v8, v9 /* Number of columns in the data */ nHidden = 0 /* Number of hidden columns if TABLE is virtual */ *(*int32)(unsafe.Pointer(bp)) = 0 /* VDBE cursor that is the main data repository */ *(*int32)(unsafe.Pointer(bp + 4)) = 0 /* First index cursor */ ipkColumn = -int32(1) /* Label for the end of the insertion loop */ srcTab = 0 /* Data comes from this temporary cursor if >=0 */ addrInsTop = 0 /* Jump to label "D" */ addrCont = 0 /* Index of database holding TABLE */ useTempTable = uint8(0) /* Store SELECT results in intermediate table */ appendFlag = uint8(0) /* True if IDLIST is in table order */ pList = uintptr(0) /* Register in which to store next column */ /* Register allocations */ regFromSelect = 0 /* Base register for data coming from SELECT */ regAutoinc = 0 /* Register holding the AUTOINCREMENT counter */ regRowCount = 0 /* register holding first column to insert */ aRegIdx = uintptr(0) /* Mask of trigger times */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto insert_cleanup } _ = libc.Int32FromInt32(0) (*(*TSelectDest)(unsafe.Pointer(bp + 8))).FiSDParm = 0 /* Suppress a harmless compiler warning */ /* If the Select object is really just a simple VALUES() list with a ** single row (the common case) then keep that one row of values ** and discard the other (unused) parts of the pSelect object */ if pSelect != 0 && (*TSelect)(unsafe.Pointer(pSelect)).FselFlags&uint32(SF_Values) != uint32(0) && (*TSelect)(unsafe.Pointer(pSelect)).FpPrior == uintptr(0) { pList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList (*TSelect)(unsafe.Pointer(pSelect)).FpEList = uintptr(0) _sqlite3SelectDelete(tls, db, pSelect) pSelect = uintptr(0) } /* Locate the table into which we will be inserting new information. */ _ = libc.Int32FromInt32(0) pTab = _sqlite3SrcListLookup(tls, pParse, pTabList) if pTab == uintptr(0) { goto insert_cleanup } iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) _ = libc.Int32FromInt32(0) if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_INSERT), (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0), (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName) != 0 { goto insert_cleanup } withoutRowid = libc.BoolUint8(!((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0))) /* Figure out if we have any triggers and if the table being ** inserted into is a view */ pTrigger = _sqlite3TriggersExist(tls, pParse, pTab, int32(TK_INSERT), uintptr(0), bp+36) isView = libc.BoolInt32(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW)) _ = libc.Int32FromInt32(0) /* If pTab is really a view, make sure it has been initialized. ** ViewGetColumnNames() is a no-op if pTab is not a view. */ if _sqlite3ViewGetColumnNames(tls, pParse, pTab) != 0 { goto insert_cleanup } /* Cannot insert into a read-only table. */ if _sqlite3IsReadOnly(tls, pParse, pTab, pTrigger) != 0 { goto insert_cleanup } /* Allocate a VDBE */ v = _sqlite3GetVdbe(tls, pParse) if v == uintptr(0) { goto insert_cleanup } if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 { _sqlite3VdbeCountChanges(tls, v) } _sqlite3BeginWriteOperation(tls, pParse, libc.BoolInt32(pSelect != 0 || pTrigger != 0), iDb) /* If the statement is of the form ** ** INSERT INTO SELECT * FROM ; ** ** Then special optimizations can be applied that make the transfer ** very fast and which reduce fragmentation of indices. ** ** This is the 2nd template. */ if pColumn == uintptr(0) && pSelect != uintptr(0) && pTrigger == uintptr(0) && _xferOptimization(tls, pParse, pTab, pSelect, onError, iDb) != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) goto insert_end } /* If this is an AUTOINCREMENT table, look up the sequence number in the ** sqlite_sequence table and store it in memory cell regAutoinc. */ regAutoinc = _autoIncBegin(tls, pParse, iDb, pTab) /* Allocate a block registers to hold the rowid and the values ** for all columns of the new row. */ v1 = (*TParse)(unsafe.Pointer(pParse)).FnMem + libc.Int32FromInt32(1) regIns = v1 regRowid = v1 *(*int32)(unsafe.Pointer(pParse + 44)) += int32((*TTable)(unsafe.Pointer(pTab)).FnCol) + int32(1) if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { regRowid++ (*TParse)(unsafe.Pointer(pParse)).FnMem++ } regData = regRowid + int32(1) /* If the INSERT statement included an IDLIST term, then make sure ** all elements of the IDLIST really are columns of the table and ** remember the column indices. ** ** If the table has an INTEGER PRIMARY KEY column and that column ** is named in the IDLIST, then record in the ipkColumn variable ** the index into IDLIST of the primary key column. ipkColumn is ** the index of the primary key as it appears in IDLIST, not as ** is appears in the original table. (The index of the INTEGER ** PRIMARY KEY in the original table is pTab->iPKey.) After this ** loop, if ipkColumn==(-1), that means that integer primary key ** is unspecified, and hence the table is either WITHOUT ROWID or ** it will automatically generated an integer primary key. ** ** bIdListInOrder is true if the columns in IDLIST are in storage ** order. This enables an optimization that avoids shuffling the ** columns into storage order. False negatives are harmless, ** but false positives will cause database corruption. */ bIdListInOrder = libc.BoolUint8((*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(libc.Int32FromInt32(TF_OOOHidden)|libc.Int32FromInt32(TF_HasStored)) == uint32(0)) if pColumn != 0 { _ = libc.Int32FromInt32(0) (*TIdList)(unsafe.Pointer(pColumn)).FeU4 = uint8(EU4_IDX) i = 0 for { if !(i < (*TIdList)(unsafe.Pointer(pColumn)).FnId) { break } *(*int32)(unsafe.Pointer(pColumn + 8 + uintptr(i)*8 + 4)) = -int32(1) goto _2 _2: ; i++ } i = 0 for { if !(i < (*TIdList)(unsafe.Pointer(pColumn)).FnId) { break } j = 0 for { if !(j < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if _sqlite3StrICmp(tls, (*(*TIdList_item)(unsafe.Pointer(pColumn + 8 + uintptr(i)*8))).FzName, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FzCnName) == 0 { *(*int32)(unsafe.Pointer(pColumn + 8 + uintptr(i)*8 + 4)) = j if i != j { bIdListInOrder = uint8(0) } if j == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { ipkColumn = i _ = libc.Int32FromInt32(0) } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FcolFlags)&(libc.Int32FromInt32(COLFLAG_STORED)|libc.Int32FromInt32(COLFLAG_VIRTUAL)) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16084, libc.VaList(bp+88, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FzCnName)) goto insert_cleanup } break } goto _4 _4: ; j++ } if j >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { if _sqlite3IsRowid(tls, (*(*TIdList_item)(unsafe.Pointer(pColumn + 8 + uintptr(i)*8))).FzName) != 0 && !(withoutRowid != 0) { ipkColumn = i bIdListInOrder = uint8(0) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16125, libc.VaList(bp+88, pTabList+8, (*(*TIdList_item)(unsafe.Pointer(pColumn + 8 + uintptr(i)*8))).FzName)) (*TParse)(unsafe.Pointer(pParse)).FcheckSchema = uint8(1) goto insert_cleanup } } goto _3 _3: ; i++ } } /* Figure out how many columns of data are supplied. If the data ** is coming from a SELECT statement, then generate a co-routine that ** produces a single row of the SELECT on each invocation. The ** co-routine is the common header to the 3rd and 4th templates. */ if pSelect != 0 { /* Result code */ v6 = pParse + 44 *(*int32)(unsafe.Pointer(v6))++ v5 = *(*int32)(unsafe.Pointer(v6)) regYield = v5 addrTop = _sqlite3VdbeCurrentAddr(tls, v) + int32(1) _sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), regYield, 0, addrTop) _sqlite3SelectDestInit(tls, bp+8, int32(SRT_Coroutine), regYield) if bIdListInOrder != 0 { v7 = regData } else { v7 = 0 } (*(*TSelectDest)(unsafe.Pointer(bp + 8))).FiSdst = v7 (*(*TSelectDest)(unsafe.Pointer(bp + 8))).FnSdst = int32((*TTable)(unsafe.Pointer(pTab)).FnCol) rc = _sqlite3Select(tls, pParse, pSelect, bp+8) regFromSelect = (*(*TSelectDest)(unsafe.Pointer(bp + 8))).FiSdst _ = libc.Int32FromInt32(0) if rc != 0 || (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto insert_cleanup } _ = libc.Int32FromInt32(0) _sqlite3VdbeEndCoroutine(tls, v, regYield) _sqlite3VdbeJumpHere(tls, v, addrTop-int32(1)) /* label B: */ _ = libc.Int32FromInt32(0) nColumn = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpEList)).FnExpr /* Set useTempTable to TRUE if the result of the SELECT statement ** should be written into a temporary table (template 4). Set to ** FALSE if each output row of the SELECT can be written directly into ** the destination table (template 3). ** ** A temp table must be used if the table being updated is also one ** of the tables being read by the SELECT statement. Also use a ** temp table in the case of row triggers. */ if pTrigger != 0 || _readsTable(tls, pParse, iDb, pTab) != 0 { useTempTable = uint8(1) } if useTempTable != 0 { /* Label "L" */ v9 = pParse + 40 v8 = *(*int32)(unsafe.Pointer(v9)) *(*int32)(unsafe.Pointer(v9))++ srcTab = v8 regRec = _sqlite3GetTempReg(tls, pParse) regTempRowid = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), srcTab, nColumn) addrL = _sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), (*(*TSelectDest)(unsafe.Pointer(bp + 8))).FiSDParm) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regFromSelect, nColumn, regRec) _sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), srcTab, regTempRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), srcTab, regRec, regTempRowid) _sqlite3VdbeGoto(tls, v, addrL) _sqlite3VdbeJumpHere(tls, v, addrL) _sqlite3ReleaseTempReg(tls, pParse, regRec) _sqlite3ReleaseTempReg(tls, pParse, regTempRowid) } } else { libc.Xmemset(tls, bp+40, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp + 40))).FpParse = pParse srcTab = -int32(1) _ = libc.Int32FromInt32(0) if pList != 0 { nColumn = (*TExprList)(unsafe.Pointer(pList)).FnExpr if _sqlite3ResolveExprListNames(tls, bp+40, pList) != 0 { goto insert_cleanup } } else { nColumn = 0 } } /* If there is no IDLIST term but the table has an integer primary ** key, the set the ipkColumn variable to the integer primary key ** column index in the original table definition. */ if pColumn == uintptr(0) && nColumn > 0 { ipkColumn = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) if ipkColumn >= 0 && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != uint32(0) { i = ipkColumn - int32(1) for { if !(i >= 0) { break } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { ipkColumn-- } goto _10 _10: ; i-- } } /* Make sure the number of columns in the source data matches the number ** of columns to be inserted into the table. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(libc.Int32FromInt32(TF_HasGenerated)|libc.Int32FromInt32(TF_HasHidden)) != uint32(0) { i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_NOINSERT) != 0 { nHidden++ } goto _11 _11: ; i++ } } if nColumn != int32((*TTable)(unsafe.Pointer(pTab)).FnCol)-nHidden { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16157, libc.VaList(bp+88, pTabList+8, int32((*TTable)(unsafe.Pointer(pTab)).FnCol)-nHidden, nColumn)) goto insert_cleanup } } if pColumn != uintptr(0) && nColumn != (*TIdList)(unsafe.Pointer(pColumn)).FnId { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16209, libc.VaList(bp+88, nColumn, (*TIdList)(unsafe.Pointer(pColumn)).FnId)) goto insert_cleanup } /* Initialize the count of rows to be inserted */ if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00001))< ** end loop ** D: ... */ addrInsTop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), srcTab) addrCont = _sqlite3VdbeCurrentAddr(tls, v) } else { if pSelect != 0 { /* This block codes the top of loop only. The complete loop is the ** following pseudocode (template 3): ** ** C: yield X, at EOF goto D ** insert the select result into
    from R..R+n ** goto C ** D: ... */ v19 = _sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), (*(*TSelectDest)(unsafe.Pointer(bp + 8))).FiSDParm) addrCont = v19 addrInsTop = v19 if ipkColumn >= 0 { /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the ** SELECT, go ahead and copy the value into the rowid slot now, so that ** the value does not get overwritten by a NULL at tag-20191021-002. */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), regFromSelect+ipkColumn, regRowid) } } } /* Compute data for ordinary columns of the new entry. Values ** are written in storage order into registers starting with regData. ** Only ordinary columns are computed in this loop. The rowid ** (if there is one) is computed later and generated columns are ** computed after the rowid since they might depend on the value ** of the rowid. */ nHidden = 0 iRegStore = regData _ = libc.Int32FromInt32(0) i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } _ = libc.Int32FromInt32(0) if i == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled ** using the rowid. So put a NULL in the IPK slot of the record to avoid ** using excess space. The file format definition requires this extra ** NULL - we cannot optimize further by skipping the column completely */ _sqlite3VdbeAddOp1(tls, v, int32(OP_SoftNull), iRegStore) goto _20 } v21 = uint32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags) colFlags = v21 if v21&uint32(COLFLAG_NOINSERT) != uint32(0) { nHidden++ if colFlags&uint32(COLFLAG_VIRTUAL) != uint32(0) { /* Virtual columns do not participate in OP_MakeRecord. So back up ** iRegStore by one slot to compensate for the iRegStore++ in the ** outer for() loop */ iRegStore-- goto _20 } else { if colFlags&uint32(COLFLAG_STORED) != uint32(0) { /* Stored columns are computed later. But if there are BEFORE ** triggers, the slots used for stored columns will be OP_Copy-ed ** to a second block of registers, so the register needs to be ** initialized to NULL to avoid an uninitialized register read */ if *(*int32)(unsafe.Pointer(bp + 36))&int32(TRIGGER_BEFORE) != 0 { _sqlite3VdbeAddOp1(tls, v, int32(OP_SoftNull), iRegStore) } goto _20 } else { if pColumn == uintptr(0) { /* Hidden columns that are not explicitly named in the INSERT ** get there default value */ _sqlite3ExprCodeFactorable(tls, pParse, _sqlite3ColumnExpr(tls, pTab, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(i)*12), iRegStore) goto _20 } } } } if pColumn != 0 { _ = libc.Int32FromInt32(0) j = 0 for { if !(j < (*TIdList)(unsafe.Pointer(pColumn)).FnId && *(*int32)(unsafe.Pointer(pColumn + 8 + uintptr(j)*8 + 4)) != i) { break } goto _22 _22: ; j++ } if j >= (*TIdList)(unsafe.Pointer(pColumn)).FnId { /* A column not named in the insert column list gets its ** default value */ _sqlite3ExprCodeFactorable(tls, pParse, _sqlite3ColumnExpr(tls, pTab, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(i)*12), iRegStore) goto _20 } k = j } else { if nColumn == 0 { /* This is INSERT INTO ... DEFAULT VALUES. Load the default value. */ _sqlite3ExprCodeFactorable(tls, pParse, _sqlite3ColumnExpr(tls, pTab, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(i)*12), iRegStore) goto _20 } else { k = i - nHidden } } if useTempTable != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), srcTab, k, iRegStore) } else { if pSelect != 0 { if regFromSelect != regData { _sqlite3VdbeAddOp2(tls, v, int32(OP_SCopy), regFromSelect+k, iRegStore) } } else { pX = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(k)*20))).FpExpr y = _sqlite3ExprCodeTarget(tls, pParse, pX, iRegStore) if y != iRegStore { if (*TExpr)(unsafe.Pointer(pX)).Fflags&uint32(libc.Int32FromInt32(EP_Subquery)) != uint32(0) { v23 = int32(OP_Copy) } else { v23 = int32(OP_SCopy) } _sqlite3VdbeAddOp2(tls, v, v23, y, iRegStore) } } } goto _20 _20: ; i++ iRegStore++ } /* Run the BEFORE and INSTEAD OF triggers, if there are any */ endOfLoop = _sqlite3VdbeMakeLabel(tls, pParse) if *(*int32)(unsafe.Pointer(bp + 36))&int32(TRIGGER_BEFORE) != 0 { regCols = _sqlite3GetTempRange(tls, pParse, int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+int32(1)) /* build the NEW.* reference row. Note that if there is an INTEGER ** PRIMARY KEY into which a NULL is being inserted, that NULL will be ** translated into a unique ID for the row. But on a BEFORE trigger, ** we do not know what the unique ID will be (because the insert has ** not happened yet) so we substitute a rowid of -1 */ if ipkColumn < 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), -int32(1), regCols) } else { _ = libc.Int32FromInt32(0) if useTempTable != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), srcTab, ipkColumn, regCols) } else { _ = libc.Int32FromInt32(0) /* Otherwise useTempTable is true */ _sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ipkColumn)*20))).FpExpr, regCols) } addr1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_NotNull), regCols) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), -int32(1), regCols) _sqlite3VdbeJumpHere(tls, v, addr1) _sqlite3VdbeAddOp1(tls, v, int32(OP_MustBeInt), regCols) } /* Copy the new data already generated. */ _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regRowid+int32(1), regCols+int32(1), int32((*TTable)(unsafe.Pointer(pTab)).FnNVCol)-int32(1)) /* Compute the new value for generated columns after all other ** columns have already been computed. This must be done after ** computing the ROWID in case one of the generated columns ** refers to the ROWID. */ if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != 0 { _sqlite3ComputeGeneratedColumns(tls, pParse, regCols+int32(1), pTab) } /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, ** do not attempt any conversions before assembling the record. ** If this is a real table, attempt conversions as required by the ** table column affinities. */ if !(isView != 0) { _sqlite3TableAffinity(tls, v, pTab, regCols+int32(1)) } /* Fire BEFORE or INSTEAD OF triggers */ _sqlite3CodeRowTrigger(tls, pParse, pTrigger, int32(TK_INSERT), uintptr(0), int32(TRIGGER_BEFORE), pTab, regCols-int32((*TTable)(unsafe.Pointer(pTab)).FnCol)-int32(1), onError, endOfLoop) _sqlite3ReleaseTempRange(tls, pParse, regCols, int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+int32(1)) } if !(isView != 0) { if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { /* The row that the VUpdate opcode will delete: none */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regIns) } if ipkColumn >= 0 { /* Compute the new rowid */ if useTempTable != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), srcTab, ipkColumn, regRowid) } else { if pSelect != 0 { /* Rowid already initialized at tag-20191021-001 */ } else { pIpk = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ipkColumn)*20))).FpExpr if int32((*TExpr)(unsafe.Pointer(pIpk)).Fop) == int32(TK_NULL) && !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) { _sqlite3VdbeAddOp3(tls, v, int32(OP_NewRowid), *(*int32)(unsafe.Pointer(bp)), regRowid, regAutoinc) appendFlag = uint8(1) } else { _sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ipkColumn)*20))).FpExpr, regRowid) } } } /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid ** to generate a unique primary key value. */ if !(appendFlag != 0) { if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) { addr11 = _sqlite3VdbeAddOp1(tls, v, int32(OP_NotNull), regRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_NewRowid), *(*int32)(unsafe.Pointer(bp)), regRowid, regAutoinc) _sqlite3VdbeJumpHere(tls, v, addr11) } else { addr11 = _sqlite3VdbeCurrentAddr(tls, v) _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regRowid, addr11+int32(2)) } _sqlite3VdbeAddOp1(tls, v, int32(OP_MustBeInt), regRowid) } } else { if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) || withoutRowid != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regRowid) } else { _sqlite3VdbeAddOp3(tls, v, int32(OP_NewRowid), *(*int32)(unsafe.Pointer(bp)), regRowid, regAutoinc) appendFlag = uint8(1) } } _autoIncStep(tls, pParse, regAutoinc, regRowid) /* Compute the new value for generated columns after all other ** columns have already been computed. This must be done after ** computing the ROWID in case one of the generated columns ** is derived from the INTEGER PRIMARY KEY. */ if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != 0 { _sqlite3ComputeGeneratedColumns(tls, pParse, regRowid+int32(1), pTab) } /* Generate code to check constraints and generate index keys and ** do the insertion. */ if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { pVTab = _sqlite3GetVTable(tls, db, pTab) _sqlite3VtabMakeWritable(tls, pParse, pTab) _sqlite3VdbeAddOp4(tls, v, int32(OP_VUpdate), int32(1), int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+int32(2), regIns, pVTab, -int32(11)) if onError == int32(OE_Default) { v24 = int32(OE_Abort) } else { v24 = onError } _sqlite3VdbeChangeP5(tls, v, uint16(v24)) _sqlite3MayAbort(tls, pParse) } else { *(*int32)(unsafe.Pointer(bp + 76)) = 0 /* True to use OPFLAG_SEEKRESULT */ _sqlite3GenerateConstraintChecks(tls, pParse, pTab, aRegIdx, *(*int32)(unsafe.Pointer(bp)), *(*int32)(unsafe.Pointer(bp + 4)), regIns, 0, libc.BoolUint8(ipkColumn >= 0), uint8(onError), endOfLoop, bp+76, uintptr(0), pUpsert) if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) != 0 { _sqlite3FkCheck(tls, pParse, pTab, 0, regIns, uintptr(0), 0) } /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE ** constraints or (b) there are no triggers and this table is not a ** parent table in a foreign key constraint. It is safe to set the ** flag in the second case as if any REPLACE constraint is hit, an ** OP_Delete or OP_IdxDelete instruction will be executed on each ** cursor that is disturbed. And these instructions both clear the ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT ** functionality. */ bUseSeek = libc.BoolInt32(*(*int32)(unsafe.Pointer(bp + 76)) == 0 || !(_sqlite3VdbeHasSubProgram(tls, v) != 0)) _sqlite3CompleteInsertion(tls, pParse, pTab, *(*int32)(unsafe.Pointer(bp)), *(*int32)(unsafe.Pointer(bp + 4)), regIns, aRegIdx, 0, int32(appendFlag), bUseSeek) } } /* Update the count of rows that are inserted */ if regRowCount != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), regRowCount, int32(1)) } if pTrigger != 0 { /* Code AFTER triggers */ _sqlite3CodeRowTrigger(tls, pParse, pTrigger, int32(TK_INSERT), uintptr(0), int32(TRIGGER_AFTER), pTab, regData-int32(2)-int32((*TTable)(unsafe.Pointer(pTab)).FnCol), onError, endOfLoop) } /* The bottom of the main insertion loop, if the data source ** is a SELECT statement. */ _sqlite3VdbeResolveLabel(tls, v, endOfLoop) if useTempTable != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), srcTab, addrCont) _sqlite3VdbeJumpHere(tls, v, addrInsTop) _sqlite3VdbeAddOp1(tls, v, int32(OP_Close), srcTab) } else { if pSelect != 0 { _sqlite3VdbeGoto(tls, v, addrCont) _sqlite3VdbeJumpHere(tls, v, addrInsTop) } } goto insert_end insert_end: ; /* Update the sqlite_sequence table by storing the content of the ** maximum rowid counter values recorded while inserting into ** autoincrement tables. */ if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 && (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab == uintptr(0) { _sqlite3AutoincrementEnd(tls, pParse) } /* ** Return the number of rows inserted. If this routine is ** generating code because of a call to sqlite3NestedParse(), do not ** invoke the callback function. */ if regRowCount != 0 { _sqlite3CodeChangeCount(tls, v, regRowCount, __ccgo_ts+16301) } goto insert_cleanup insert_cleanup: ; _sqlite3SrcListDelete(tls, db, pTabList) _sqlite3ExprListDelete(tls, db, pList) _sqlite3UpsertDelete(tls, db, pUpsert) _sqlite3SelectDelete(tls, db, pSelect) _sqlite3IdListDelete(tls, db, pColumn) if aRegIdx != 0 { _sqlite3DbNNFreeNN(tls, db, aRegIdx) } } /* Make sure "isView" and other macros defined above are undefined. Otherwise ** they may interfere with compilation of other functions in this file ** (or in another file, if this file becomes part of the amalgamation). */ /* ** Meanings of bits in of pWalker->eCode for ** sqlite3ExprReferencesUpdatedColumn() */ // C documentation // // /* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn(). // * Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this // ** expression node references any of the // ** columns that are being modified by an UPDATE statement. // */ func _checkConstraintExprNode(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var p1, p2 uintptr _, _ = p1, p2 if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) { _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) >= 0 { if *(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalker + 24)) + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)*4)) >= 0 { p1 = pWalker + 20 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(CKCNSTRNT_COLUMN)) } } else { p2 = pWalker + 20 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(CKCNSTRNT_ROWID)) } } return WRC_Continue } // C documentation // // /* // ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The // ** only columns that are modified by the UPDATE are those for which // ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true. // ** // ** Return true if CHECK constraint pExpr uses any of the // ** changing columns (or the rowid if it is changing). In other words, // ** return true if this CHECK constraint must be validated for // ** the new row in the UPDATE statement. // ** // ** 2018-09-15: pExpr might also be an expression for an index-on-expressions. // ** The operation of this routine is the same - return true if an only if // ** the expression uses one or more of columns identified by the second and // ** third arguments. // */ func _sqlite3ExprReferencesUpdatedColumn(tls *libc.TLS, pExpr uintptr, aiChng uintptr, chngRowid int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var p1 uintptr var _ /* w at bp+0 */ TWalker _ = p1 libc.Xmemset(tls, bp, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp))).FeCode = uint16(0) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_checkConstraintExprNode) *(*uintptr)(unsafe.Pointer(bp + 24)) = aiChng _sqlite3WalkExpr(tls, bp, pExpr) if !(chngRowid != 0) { p1 = bp + 20 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(CKCNSTRNT_ROWID)) } return libc.BoolInt32(int32((*(*TWalker)(unsafe.Pointer(bp))).FeCode) != 0) } // C documentation // // /* // ** The sqlite3GenerateConstraintChecks() routine usually wants to visit // ** the indexes of a table in the order provided in the Table->pIndex list. // ** However, sometimes (rarely - when there is an upsert) it wants to visit // ** the indexes in a different order. The following data structures accomplish // ** this. // ** // ** The IndexIterator object is used to walk through all of the indexes // ** of a table in either Index.pNext order, or in some other order established // ** by an array of IndexListTerm objects. // */ type TIndexListTerm = struct { Fp uintptr Fix int32 } type IndexListTerm = TIndexListTerm type TIndexIterator = struct { FeType int32 Fi int32 Fu struct { Fax [0]struct { FnIdx int32 FaIdx uintptr } Flx struct { FpIdx uintptr } F__ccgo_pad2 [4]byte } } type IndexIterator = TIndexIterator type TIndexIterator1 = struct { FeType int32 Fi int32 Fu struct { Fax [0]struct { FnIdx int32 FaIdx uintptr } Flx struct { FpIdx uintptr } F__ccgo_pad2 [4]byte } } type IndexIterator1 = TIndexIterator1 /* When IndexIterator.eType==1, then each index is an array of instances ** of the following object */ type TIndexListTerm1 = struct { Fp uintptr Fix int32 } type IndexListTerm1 = TIndexListTerm1 // C documentation // // /* Return the first index on the list */ func _indexIteratorFirst(tls *libc.TLS, pIter uintptr, pIx uintptr) (r uintptr) { _ = libc.Int32FromInt32(0) if (*TIndexIterator)(unsafe.Pointer(pIter)).FeType != 0 { *(*int32)(unsafe.Pointer(pIx)) = (*(*TIndexListTerm)(unsafe.Pointer((*(*struct { FnIdx int32 FaIdx uintptr })(unsafe.Pointer(pIter + 8))).FaIdx))).Fix return (*(*TIndexListTerm)(unsafe.Pointer((*(*struct { FnIdx int32 FaIdx uintptr })(unsafe.Pointer(pIter + 8))).FaIdx))).Fp } else { *(*int32)(unsafe.Pointer(pIx)) = 0 return (*TIndexIterator)(unsafe.Pointer(pIter)).Fu.Flx.FpIdx } return r } // C documentation // // /* Return the next index from the list. Return NULL when out of indexes */ func _indexIteratorNext(tls *libc.TLS, pIter uintptr, pIx uintptr) (r uintptr) { var i, v1 int32 var v2 uintptr _, _, _ = i, v1, v2 if (*TIndexIterator)(unsafe.Pointer(pIter)).FeType != 0 { v2 = pIter + 4 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) i = v1 if i >= (*(*struct { FnIdx int32 FaIdx uintptr })(unsafe.Pointer(pIter + 8))).FnIdx { *(*int32)(unsafe.Pointer(pIx)) = i return uintptr(0) } *(*int32)(unsafe.Pointer(pIx)) = (*(*TIndexListTerm)(unsafe.Pointer((*(*struct { FnIdx int32 FaIdx uintptr })(unsafe.Pointer(pIter + 8))).FaIdx + uintptr(i)*8))).Fix return (*(*TIndexListTerm)(unsafe.Pointer((*(*struct { FnIdx int32 FaIdx uintptr })(unsafe.Pointer(pIter + 8))).FaIdx + uintptr(i)*8))).Fp } else { *(*int32)(unsafe.Pointer(pIx))++ (*TIndexIterator)(unsafe.Pointer(pIter)).Fu.Flx.FpIdx = (*TIndex)(unsafe.Pointer((*TIndexIterator)(unsafe.Pointer(pIter)).Fu.Flx.FpIdx)).FpNext return (*TIndexIterator)(unsafe.Pointer(pIter)).Fu.Flx.FpIdx } return r } // C documentation // // /* // ** Generate code to do constraint checks prior to an INSERT or an UPDATE // ** on table pTab. // ** // ** The regNewData parameter is the first register in a range that contains // ** the data to be inserted or the data after the update. There will be // ** pTab->nCol+1 registers in this range. The first register (the one // ** that regNewData points to) will contain the new rowid, or NULL in the // ** case of a WITHOUT ROWID table. The second register in the range will // ** contain the content of the first table column. The third register will // ** contain the content of the second table column. And so forth. // ** // ** The regOldData parameter is similar to regNewData except that it contains // ** the data prior to an UPDATE rather than afterwards. regOldData is zero // ** for an INSERT. This routine can distinguish between UPDATE and INSERT by // ** checking regOldData for zero. // ** // ** For an UPDATE, the pkChng boolean is true if the true primary key (the // ** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table) // ** might be modified by the UPDATE. If pkChng is false, then the key of // ** the iDataCur content table is guaranteed to be unchanged by the UPDATE. // ** // ** For an INSERT, the pkChng boolean indicates whether or not the rowid // ** was explicitly specified as part of the INSERT statement. If pkChng // ** is zero, it means that the either rowid is computed automatically or // ** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT, // ** pkChng will only be true if the INSERT statement provides an integer // ** value for either the rowid column or its INTEGER PRIMARY KEY alias. // ** // ** The code generated by this routine will store new index entries into // ** registers identified by aRegIdx[]. No index entry is created for // ** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is // ** the same as the order of indices on the linked list of indices // ** at pTab->pIndex. // ** // ** (2019-05-07) The generated code also creates a new record for the // ** main table, if pTab is a rowid table, and stores that record in the // ** register identified by aRegIdx[nIdx] - in other words in the first // ** entry of aRegIdx[] past the last index. It is important that the // ** record be generated during constraint checks to avoid affinity changes // ** to the register content that occur after constraint checks but before // ** the new record is inserted. // ** // ** The caller must have already opened writeable cursors on the main // ** table and all applicable indices (that is to say, all indices for which // ** aRegIdx[] is not zero). iDataCur is the cursor for the main table when // ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY // ** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor // ** for the first index in the pTab->pIndex list. Cursors for other indices // ** are at iIdxCur+N for the N-th element of the pTab->pIndex list. // ** // ** This routine also generates code to check constraints. NOT NULL, // ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, // ** then the appropriate action is performed. There are five possible // ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. // ** // ** Constraint type Action What Happens // ** --------------- ---------- ---------------------------------------- // ** any ROLLBACK The current transaction is rolled back and // ** sqlite3_step() returns immediately with a // ** return code of SQLITE_CONSTRAINT. // ** // ** any ABORT Back out changes from the current command // ** only (do not do a complete rollback) then // ** cause sqlite3_step() to return immediately // ** with SQLITE_CONSTRAINT. // ** // ** any FAIL Sqlite3_step() returns immediately with a // ** return code of SQLITE_CONSTRAINT. The // ** transaction is not rolled back and any // ** changes to prior rows are retained. // ** // ** any IGNORE The attempt in insert or update the current // ** row is skipped, without throwing an error. // ** Processing continues with the next row. // ** (There is an immediate jump to ignoreDest.) // ** // ** NOT NULL REPLACE The NULL value is replace by the default // ** value for that column. If the default value // ** is NULL, the action is the same as ABORT. // ** // ** UNIQUE REPLACE The other row that conflicts with the row // ** being inserted is removed. // ** // ** CHECK REPLACE Illegal. The results in an exception. // ** // ** Which action to take is determined by the overrideError parameter. // ** Or if overrideError==OE_Default, then the pParse->onError parameter // ** is used. Or if pParse->onError==OE_Default then the onError value // ** for the constraint is used. // */ func _sqlite3GenerateConstraintChecks(tls *libc.TLS, pParse uintptr, pTab uintptr, aRegIdx uintptr, iDataCur int32, iIdxCur int32, regNewData int32, regOldData int32, pkChng Tu8, overrideError Tu8, ignoreDest int32, pbMayReplace uintptr, aiChng uintptr, pUpsert uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var addr1, addrBypass, addrConflictCk, addrJump, addrRecheck, addrRowidOk, addrUniqueOk, allOk, b2ndPass, i, iField, iReg, iThisCur, ipkBottom, ipkTop, isGenerated, jj, lblRecheckOk, nCol, nConflictCk, nGenerated, nIdx, nPkField, nReplaceTrig, nSeenReplace, onError, op, p2, regCmp, regIdx, regR, regRec, regTrigCnt, seenReplace, upsertIpkDelay, upsertIpkReturn, x, x1, v11, v13, v15, v2, v7 int32 var bAffinityDone, isUpdate Tu8 var bUsed, db, p4, pCheck, pCol, pCopy, pExpr, pIdx, pPk, pTerm, pTrigger, pUpsertClause, v, zMsg, zName, zP4, v16, v8 uintptr var nByte Tu64 var _ /* ix at bp+0 */ int32 var _ /* sIdxIter at bp+4 */ TIndexIterator var _ /* x at bp+20 */ TVdbeOp _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr1, addrBypass, addrConflictCk, addrJump, addrRecheck, addrRowidOk, addrUniqueOk, allOk, b2ndPass, bAffinityDone, bUsed, db, i, iField, iReg, iThisCur, ipkBottom, ipkTop, isGenerated, isUpdate, jj, lblRecheckOk, nByte, nCol, nConflictCk, nGenerated, nIdx, nPkField, nReplaceTrig, nSeenReplace, onError, op, p2, p4, pCheck, pCol, pCopy, pExpr, pIdx, pPk, pTerm, pTrigger, pUpsertClause, regCmp, regIdx, regR, regRec, regTrigCnt, seenReplace, upsertIpkDelay, upsertIpkReturn, v, x, x1, zMsg, zName, zP4, v11, v13, v15, v16, v2, v7, v8 /* Pointer to one of the indices */ pPk = uintptr(0) /* Conflict resolution strategy */ seenReplace = 0 /* Number of fields in PRIMARY KEY. 1 for ROWID tables */ pUpsertClause = uintptr(0) /* True if this is an UPDATE operation */ bAffinityDone = uint8(0) /* True if the OP_Affinity operation has been run */ upsertIpkReturn = 0 /* Address of Goto at end of IPK uniqueness check */ upsertIpkDelay = 0 /* Address of Goto to bypass initial IPK check */ ipkTop = 0 /* Top of the IPK uniqueness check */ ipkBottom = 0 /* Register used to count replace trigger invocations */ addrRecheck = 0 /* Jump here to recheck all uniqueness constraints */ lblRecheckOk = 0 /* List of DELETE triggers on the table pTab */ nReplaceTrig = 0 /* Index iterator */ isUpdate = libc.BoolUint8(regOldData != 0) db = (*TParse)(unsafe.Pointer(pParse)).Fdb v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* This table is not a VIEW */ nCol = int32((*TTable)(unsafe.Pointer(pTab)).FnCol) /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for ** normal rowid tables. nPkField is the number of key fields in the ** pPk index or 1 for a rowid table. In other words, nPkField is the ** number of fields in the true primary key of the table. */ if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { pPk = uintptr(0) nPkField = int32(1) } else { pPk = _sqlite3PrimaryKeyIndex(tls, pTab) nPkField = int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol) } /* Record that this module has started */ /* Test all NOT NULL constraints. */ if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasNotNull) != 0 { b2ndPass = 0 /* True if currently running 2nd pass */ nSeenReplace = 0 /* Number of ON CONFLICT REPLACE operations */ nGenerated = 0 /* Number of generated columns with NOT NULL */ for int32(1) != 0 { /* Make 2 passes over columns. Exit loop via "break" */ i = 0 for { if !(i < nCol) { break } /* Register holding column value */ pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12 /* non-zero if column is generated */ onError = int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4)) & 0xf >> 0)) if onError == OE_None { goto _1 } /* No NOT NULL on this column */ if i == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { goto _1 /* ROWID is never NULL */ } isGenerated = int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags) & int32(COLFLAG_GENERATED) if isGenerated != 0 && !(b2ndPass != 0) { nGenerated++ goto _1 /* Generated columns processed on 2nd pass */ } if aiChng != 0 && *(*int32)(unsafe.Pointer(aiChng + uintptr(i)*4)) < 0 && !(isGenerated != 0) { /* Do not check NOT NULL on columns that do not change */ goto _1 } if int32(overrideError) != int32(OE_Default) { onError = int32(overrideError) } else { if onError == int32(OE_Default) { onError = int32(OE_Abort) } } if onError == int32(OE_Replace) { if b2ndPass != 0 || int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt) == 0 { onError = int32(OE_Abort) } else { _ = libc.Int32FromInt32(0) } } else { if b2ndPass != 0 && !(isGenerated != 0) { goto _1 } } _ = libc.Int32FromInt32(0) iReg = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(i))) + regNewData + int32(1) switch onError { case int32(OE_Replace): addr1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_NotNull), iReg) _ = libc.Int32FromInt32(0) nSeenReplace++ _sqlite3ExprCodeCopy(tls, pParse, _sqlite3ColumnExpr(tls, pTab, pCol), iReg) _sqlite3VdbeJumpHere(tls, v, addr1) case int32(OE_Abort): _sqlite3MayAbort(tls, pParse) fallthrough case int32(OE_Rollback): fallthrough case int32(OE_Fail): zMsg = _sqlite3MPrintf(tls, db, __ccgo_ts+12381, libc.VaList(bp+48, (*TTable)(unsafe.Pointer(pTab)).FzName, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)) _sqlite3VdbeAddOp3(tls, v, int32(OP_HaltIfNull), libc.Int32FromInt32(SQLITE_CONSTRAINT)|libc.Int32FromInt32(5)< 0 && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != uint32(0) { /* If any NOT NULL ON CONFLICT REPLACE constraints fired on the ** first pass, recomputed values for all generated columns, as ** those values might depend on columns affected by the REPLACE. */ _sqlite3ComputeGeneratedColumns(tls, pParse, regNewData+int32(1), pTab) } } /* end of 2-pass loop */ } /* end if( has-not-null-constraints ) */ /* Test all CHECK constraints */ if (*TTable)(unsafe.Pointer(pTab)).FpCheck != 0 && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_IgnoreChecks) == uint64(0) { pCheck = (*TTable)(unsafe.Pointer(pTab)).FpCheck (*TParse)(unsafe.Pointer(pParse)).FiSelfTab = -(regNewData + int32(1)) if int32(overrideError) != int32(OE_Default) { v2 = int32(overrideError) } else { v2 = int32(OE_Abort) } onError = v2 i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pCheck)).FnExpr) { break } pExpr = (*(*TExprList_item)(unsafe.Pointer(pCheck + 8 + uintptr(i)*20))).FpExpr if aiChng != 0 && !(_sqlite3ExprReferencesUpdatedColumn(tls, pExpr, aiChng, int32(pkChng)) != 0) { /* The check constraints do not reference any of the columns being ** updated so there is no point it verifying the check constraint */ goto _3 } if int32(bAffinityDone) == 0 { _sqlite3TableAffinity(tls, v, pTab, regNewData+int32(1)) bAffinityDone = uint8(1) } allOk = _sqlite3VdbeMakeLabel(tls, pParse) pCopy = _sqlite3ExprDup(tls, db, pExpr, 0) if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { _sqlite3ExprIfTrue(tls, pParse, pCopy, allOk, int32(SQLITE_JUMPIFNULL)) } _sqlite3ExprDelete(tls, db, pCopy) if onError == int32(OE_Ignore) { _sqlite3VdbeGoto(tls, v, ignoreDest) } else { zName = (*(*TExprList_item)(unsafe.Pointer(pCheck + 8 + uintptr(i)*20))).FzEName _ = libc.Int32FromInt32(0) if onError == int32(OE_Replace) { onError = int32(OE_Abort) } /* IMP: R-26383-51744 */ _sqlite3HaltConstraint(tls, pParse, libc.Int32FromInt32(SQLITE_CONSTRAINT)|libc.Int32FromInt32(1)<>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) { v13 = regIdx } else { v13 = regR } regCmp = v13 i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) { break } p4 = _sqlite3LocateCollSeq(tls, pParse, *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(i)*4))) x1 = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2))) _ = libc.Int32FromInt32(0) if i == int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)-int32(1) { addrJump = addrUniqueOk op = int32(OP_Eq) } x1 = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(x1))) _sqlite3VdbeAddOp4(tls, v, op, regOldData+int32(1)+x1, addrJump, regCmp+i, p4, -int32(2)) _sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_NOTNULL)) goto _14 _14: ; i++ } } } } /* Generate code that executes if the new index entry is not unique */ _ = libc.Int32FromInt32(0) switch onError { case int32(OE_Rollback): fallthrough case int32(OE_Abort): fallthrough case int32(OE_Fail): _sqlite3UniqueConstraint(tls, pParse, onError, pIdx) case int32(OE_Update): _sqlite3UpsertDoUpdate(tls, pParse, pUpsert, pTab, pIdx, iIdxCur+*(*int32)(unsafe.Pointer(bp))) fallthrough case int32(OE_Ignore): _sqlite3VdbeGoto(tls, v, ignoreDest) default: /* Number of opcodes in conflict check logic */ _ = libc.Int32FromInt32(0) nConflictCk = _sqlite3VdbeCurrentAddr(tls, v) - addrConflictCk _ = libc.Int32FromInt32(0) if regTrigCnt != 0 { _sqlite3MultiWrite(tls, pParse) nReplaceTrig++ } if pTrigger != 0 && isUpdate != 0 { _sqlite3VdbeAddOp1(tls, v, int32(OP_CursorLock), iDataCur) } if pIdx == pPk { v15 = int32(ONEPASS_SINGLE) } else { v15 = ONEPASS_OFF } _sqlite3GenerateRowDelete(tls, pParse, pTab, pTrigger, iDataCur, iIdxCur, regR, int16(nPkField), uint8(0), uint8(OE_Replace), uint8(v15), iThisCur) if pTrigger != 0 && isUpdate != 0 { _sqlite3VdbeAddOp1(tls, v, int32(OP_CursorUnlock), iDataCur) } if regTrigCnt != 0 { /* Jump destination to bypass recheck logic */ _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), regTrigCnt, int32(1)) /* incr trigger cnt */ addrBypass = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto)) /* Bypass recheck */ /* Here we insert code that will be invoked after all constraint ** checks have run, if and only if one or more replace triggers ** fired. */ _sqlite3VdbeResolveLabel(tls, v, lblRecheckOk) lblRecheckOk = _sqlite3VdbeMakeLabel(tls, pParse) if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 { /* Bypass the recheck if this partial index is not defined ** for the current row */ _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regIdx-int32(1), lblRecheckOk) } /* Copy the constraint check code from above, except change ** the constraint-ok jump destination to be the address of ** the next retest block */ for nConflictCk > 0 { /* Conflict check opcode to copy */ /* The sqlite3VdbeAddOp4() call might reallocate the opcode array. ** Hence, make a complete copy of the opcode, rather than using ** a pointer to the opcode. */ *(*TVdbeOp)(unsafe.Pointer(bp + 20)) = *(*TVdbeOp)(unsafe.Pointer(_sqlite3VdbeGetOp(tls, v, addrConflictCk))) if int32((*(*TVdbeOp)(unsafe.Pointer(bp + 20))).Fopcode) != int32(OP_IdxRowid) { if int32(_sqlite3OpcodeProperty[(*(*TVdbeOp)(unsafe.Pointer(bp + 20))).Fopcode])&int32(OPFLG_JUMP) != 0 { p2 = lblRecheckOk } else { p2 = (*(*TVdbeOp)(unsafe.Pointer(bp + 20))).Fp2 } if int32((*(*TVdbeOp)(unsafe.Pointer(bp + 20))).Fp4type) == -int32(3) { v16 = uintptr(*(*int32)(unsafe.Pointer(bp + 20 + 16))) } else { v16 = *(*uintptr)(unsafe.Pointer(bp + 20 + 16)) } zP4 = v16 _sqlite3VdbeAddOp4(tls, v, int32((*(*TVdbeOp)(unsafe.Pointer(bp + 20))).Fopcode), (*(*TVdbeOp)(unsafe.Pointer(bp + 20))).Fp1, p2, (*(*TVdbeOp)(unsafe.Pointer(bp + 20))).Fp3, zP4, int32((*(*TVdbeOp)(unsafe.Pointer(bp + 20))).Fp4type)) _sqlite3VdbeChangeP5(tls, v, (*(*TVdbeOp)(unsafe.Pointer(bp + 20))).Fp5) } nConflictCk-- addrConflictCk++ } /* If the retest fails, issue an abort */ _sqlite3UniqueConstraint(tls, pParse, int32(OE_Abort), pIdx) _sqlite3VdbeJumpHere(tls, v, addrBypass) /* Terminate the recheck bypass */ } seenReplace = int32(1) break } _sqlite3VdbeResolveLabel(tls, v, addrUniqueOk) if regR != regIdx { _sqlite3ReleaseTempRange(tls, pParse, regR, nPkField) } if pUpsertClause != 0 && upsertIpkReturn != 0 && _sqlite3UpsertNextIsIPK(tls, pUpsertClause) != 0 { _sqlite3VdbeGoto(tls, v, upsertIpkDelay+int32(1)) _sqlite3VdbeJumpHere(tls, v, upsertIpkReturn) upsertIpkReturn = 0 } goto _9 _9: ; pIdx = _indexIteratorNext(tls, bp+4, bp) } /* If the IPK constraint is a REPLACE, run it last */ if ipkTop != 0 { _sqlite3VdbeGoto(tls, v, ipkTop) _ = libc.Int32FromInt32(0) _sqlite3VdbeJumpHere(tls, v, ipkBottom) } /* Recheck all uniqueness constraints after replace triggers have run */ _ = libc.Int32FromInt32(0) if nReplaceTrig != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_IfNot), regTrigCnt, lblRecheckOk) if !(pPk != 0) { if isUpdate != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Eq), regNewData, addrRecheck, regOldData) _sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_NOTNULL)) } _sqlite3VdbeAddOp3(tls, v, int32(OP_NotExists), iDataCur, addrRecheck, regNewData) _sqlite3RowidConstraint(tls, pParse, int32(OE_Abort), pTab) } else { _sqlite3VdbeGoto(tls, v, addrRecheck) } _sqlite3VdbeResolveLabel(tls, v, lblRecheckOk) } /* Generate the table record */ if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { regRec = *(*int32)(unsafe.Pointer(aRegIdx + uintptr(*(*int32)(unsafe.Pointer(bp)))*4)) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regNewData+int32(1), int32((*TTable)(unsafe.Pointer(pTab)).FnNVCol), regRec) if !(bAffinityDone != 0) { _sqlite3TableAffinity(tls, v, pTab, 0) } } *(*int32)(unsafe.Pointer(pbMayReplace)) = seenReplace } // C documentation // // /* // ** Table pTab is a WITHOUT ROWID table that is being written to. The cursor // ** number is iCur, and register regData contains the new record for the // ** PK index. This function adds code to invoke the pre-update hook, // ** if one is registered. // */ func _codeWithoutRowidPreupdate(tls *libc.TLS, pParse uintptr, pTab uintptr, iCur int32, regData int32) { var r int32 var v uintptr _, _ = r, v v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe r = _sqlite3GetTempReg(tls, pParse) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, r) _sqlite3VdbeAddOp4(tls, v, int32(OP_Insert), iCur, regData, r, pTab, -int32(5)) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_ISNOOP)) _sqlite3ReleaseTempReg(tls, pParse, r) } // C documentation // // /* // ** This routine generates code to finish the INSERT or UPDATE operation // ** that was started by a prior call to sqlite3GenerateConstraintChecks. // ** A consecutive range of registers starting at regNewData contains the // ** rowid and the content to be inserted. // ** // ** The arguments to this routine should be the same as the first six // ** arguments to sqlite3GenerateConstraintChecks. // */ func _sqlite3CompleteInsertion(tls *libc.TLS, pParse uintptr, pTab uintptr, iDataCur int32, iIdxCur int32, regNewData int32, aRegIdx uintptr, update_flags int32, appendBias int32, useSeekResult int32) { var i, v2, v3, v4 int32 var pIdx, v uintptr var pik_flags Tu8 _, _, _, _, _, _, _ = i, pIdx, pik_flags, v, v2, v3, v4 /* Loop counter */ _ = libc.Int32FromInt32(0) v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* This table is not a VIEW */ i = 0 pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0) { break } /* All REPLACE indexes are at the end of the list */ _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(aRegIdx + uintptr(i)*4)) == 0 { goto _1 } if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), *(*int32)(unsafe.Pointer(aRegIdx + uintptr(i)*4)), _sqlite3VdbeCurrentAddr(tls, v)+int32(2)) } if useSeekResult != 0 { v2 = int32(OPFLAG_USESEEKRESULT) } else { v2 = 0 } pik_flags = uint8(v2) if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) && !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { pik_flags = Tu8(int32(pik_flags) | libc.Int32FromInt32(OPFLAG_NCHANGE)) pik_flags = Tu8(int32(pik_flags) | update_flags&libc.Int32FromInt32(OPFLAG_SAVEPOSITION)) if update_flags == 0 { _codeWithoutRowidPreupdate(tls, pParse, pTab, iIdxCur+i, *(*int32)(unsafe.Pointer(aRegIdx + uintptr(i)*4))) } } if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x8>>3)) != 0 { v3 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) } else { v3 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) } _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iIdxCur+i, *(*int32)(unsafe.Pointer(aRegIdx + uintptr(i)*4)), *(*int32)(unsafe.Pointer(aRegIdx + uintptr(i)*4))+int32(1), v3) _sqlite3VdbeChangeP5(tls, v, uint16(pik_flags)) goto _1 _1: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext i++ } if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { return } if (*TParse)(unsafe.Pointer(pParse)).Fnested != 0 { pik_flags = uint8(0) } else { pik_flags = uint8(OPFLAG_NCHANGE) if update_flags != 0 { v4 = update_flags } else { v4 = int32(OPFLAG_LASTROWID) } pik_flags = Tu8(int32(pik_flags) | v4) } if appendBias != 0 { pik_flags = Tu8(int32(pik_flags) | libc.Int32FromInt32(OPFLAG_APPEND)) } if useSeekResult != 0 { pik_flags = Tu8(int32(pik_flags) | libc.Int32FromInt32(OPFLAG_USESEEKRESULT)) } _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iDataCur, *(*int32)(unsafe.Pointer(aRegIdx + uintptr(i)*4)), regNewData) if !((*TParse)(unsafe.Pointer(pParse)).Fnested != 0) { _sqlite3VdbeAppendP4(tls, v, pTab, -int32(5)) } _sqlite3VdbeChangeP5(tls, v, uint16(pik_flags)) } // C documentation // // /* // ** Allocate cursors for the pTab table and all its indices and generate // ** code to open and initialized those cursors. // ** // ** The cursor for the object that contains the complete data (normally // ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT // ** ROWID table) is returned in *piDataCur. The first index cursor is // ** returned in *piIdxCur. The number of indices is returned. // ** // ** Use iBase as the first cursor (either the *piDataCur for rowid tables // ** or the first index for WITHOUT ROWID tables) if it is non-negative. // ** If iBase is negative, then allocate the next available cursor. // ** // ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur. // ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range // ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the // ** pTab->pIndex list. // ** // ** If pTab is a virtual table, then this routine is a no-op and the // ** *piDataCur and *piIdxCur values are left uninitialized. // */ func _sqlite3OpenTableAndIndices(tls *libc.TLS, pParse uintptr, pTab uintptr, op int32, p5 Tu8, iBase int32, aToOpen uintptr, piDataCur uintptr, piIdxCur uintptr) (r int32) { var i, iDataCur, iDb, iIdxCur, v1, v2, v4 int32 var pIdx, v uintptr _, _, _, _, _, _, _, _, _ = i, iDataCur, iDb, iIdxCur, pIdx, v, v1, v2, v4 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { /* This routine is a no-op for virtual tables. Leave the output ** variables *piDataCur and *piIdxCur set to illegal cursor numbers ** for improved error detection. */ v1 = -libc.Int32FromInt32(999) *(*int32)(unsafe.Pointer(piIdxCur)) = v1 *(*int32)(unsafe.Pointer(piDataCur)) = v1 return 0 } iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTable)(unsafe.Pointer(pTab)).FpSchema) v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) if iBase < 0 { iBase = (*TParse)(unsafe.Pointer(pParse)).FnTab } v2 = iBase iBase++ iDataCur = v2 *(*int32)(unsafe.Pointer(piDataCur)) = iDataCur if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) && (aToOpen == uintptr(0) || *(*Tu8)(unsafe.Pointer(aToOpen)) != 0) { _sqlite3OpenTable(tls, pParse, iDataCur, iDb, pTab, op) } else { if int32((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FnoSharedCache) == 0 { _sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab)).Ftnum, libc.BoolUint8(op == int32(OP_OpenWrite)), (*TTable)(unsafe.Pointer(pTab)).FzName) } } *(*int32)(unsafe.Pointer(piIdxCur)) = iBase i = 0 pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0) { break } v4 = iBase iBase++ iIdxCur = v4 _ = libc.Int32FromInt32(0) if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) && !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { *(*int32)(unsafe.Pointer(piDataCur)) = iIdxCur p5 = uint8(0) } if aToOpen == uintptr(0) || *(*Tu8)(unsafe.Pointer(aToOpen + uintptr(i+int32(1)))) != 0 { _sqlite3VdbeAddOp3(tls, v, op, iIdxCur, int32((*TIndex)(unsafe.Pointer(pIdx)).Ftnum), iDb) _sqlite3VdbeSetP4KeyInfo(tls, pParse, pIdx) _sqlite3VdbeChangeP5(tls, v, uint16(p5)) } goto _3 _3: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext i++ } if iBase > (*TParse)(unsafe.Pointer(pParse)).FnTab { (*TParse)(unsafe.Pointer(pParse)).FnTab = iBase } return i } // C documentation // // /* // ** Check to see if index pSrc is compatible as a source of data // ** for index pDest in an insert transfer optimization. The rules // ** for a compatible index: // ** // ** * The index is over the same set of columns // ** * The same DESC and ASC markings occurs on all columns // ** * The same onError processing (OE_Abort, OE_Ignore, etc) // ** * The same collating sequence on each column // ** * The index has the exact same WHERE clause // */ func _xferCompatibleIndex(tls *libc.TLS, pDest uintptr, pSrc uintptr) (r int32) { var i int32 _ = i _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TIndex)(unsafe.Pointer(pDest)).FnKeyCol) != int32((*TIndex)(unsafe.Pointer(pSrc)).FnKeyCol) || int32((*TIndex)(unsafe.Pointer(pDest)).FnColumn) != int32((*TIndex)(unsafe.Pointer(pSrc)).FnColumn) { return 0 /* Different number of columns */ } if int32((*TIndex)(unsafe.Pointer(pDest)).FonError) != int32((*TIndex)(unsafe.Pointer(pSrc)).FonError) { return 0 /* Different conflict resolution strategies */ } i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pSrc)).FnKeyCol)) { break } if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pSrc)).FaiColumn + uintptr(i)*2))) != int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pDest)).FaiColumn + uintptr(i)*2))) { return 0 /* Different columns indexed */ } if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pSrc)).FaiColumn + uintptr(i)*2))) == -int32(2) { _ = libc.Int32FromInt32(0) if _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pSrc)).FaColExpr + 8 + uintptr(i)*20))).FpExpr, (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pDest)).FaColExpr + 8 + uintptr(i)*20))).FpExpr, -int32(1)) != 0 { return 0 /* Different expressions in the index */ } } if int32(*(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pSrc)).FaSortOrder + uintptr(i)))) != int32(*(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pDest)).FaSortOrder + uintptr(i)))) { return 0 /* Different sort orders */ } if Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pSrc)).FazColl + uintptr(i)*4)), *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pDest)).FazColl + uintptr(i)*4))) != 0 { return 0 /* Different collating sequences */ } goto _1 _1: ; i++ } if _sqlite3ExprCompare(tls, uintptr(0), (*TIndex)(unsafe.Pointer(pSrc)).FpPartIdxWhere, (*TIndex)(unsafe.Pointer(pDest)).FpPartIdxWhere, -int32(1)) != 0 { return 0 /* Different WHERE clauses */ } /* If no test above fails then the indices must be compatible */ return int32(1) } // C documentation // // /* // ** Attempt the transfer optimization on INSERTs of the form // ** // ** INSERT INTO tab1 SELECT * FROM tab2; // ** // ** The xfer optimization transfers raw records from tab2 over to tab1. // ** Columns are not decoded and reassembled, which greatly improves // ** performance. Raw index records are transferred in the same way. // ** // ** The xfer optimization is only attempted if tab1 and tab2 are compatible. // ** There are lots of rules for determining compatibility - see comments // ** embedded in the code for details. // ** // ** This routine returns TRUE if the optimization is guaranteed to be used. // ** Sometimes the xfer optimization will only work if the destination table // ** is empty - a factor that can only be determined at run-time. In that // ** case, this routine generates code for the xfer optimization but also // ** does a test to see if the destination table is empty and jumps over the // ** xfer optimization code if the test fails. In that case, this routine // ** returns FALSE so that the caller will know to go ahead and generate // ** an unoptimized transfer. This routine also returns FALSE if there // ** is no chance that the xfer optimization can be applied. // ** // ** This optimization is particularly useful at making VACUUM run faster. // */ func _xferOptimization(tls *libc.TLS, pParse uintptr, pDest uintptr, pSelect uintptr, onError int32, iDbDest int32) (r int32) { var addr1, addr2, destHasUniqueIdx, emptyDestTest, emptySrcTest, i, iDbSrc, iDest, iSrc, regAutoinc, regData, regRowid, v4, v6 int32 var db, pDestCol, pDestExpr, pDestIdx, pEList, pItem, pSrc, pSrcCol, pSrcExpr, pSrcIdx, v, zColl, v5, v7 uintptr var idxInsFlags, insFlags Tu8 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr1, addr2, db, destHasUniqueIdx, emptyDestTest, emptySrcTest, i, iDbSrc, iDest, iSrc, idxInsFlags, insFlags, pDestCol, pDestExpr, pDestIdx, pEList, pItem, pSrc, pSrcCol, pSrcExpr, pSrcIdx, regAutoinc, regData, regRowid, v, zColl, v4, v5, v6, v7 db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Loop addresses */ emptyDestTest = 0 /* Address of test for empty pDest */ emptySrcTest = 0 /* Memory register used by AUTOINC */ destHasUniqueIdx = 0 /* Registers holding data and rowid */ _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FpWith != 0 || (*TSelect)(unsafe.Pointer(pSelect)).FpWith != 0 { /* Do not attempt to process this query if there are an WITH clauses ** attached to it. Proceeding may generate a false "no such table: xxx" ** error if pSelect reads from a CTE named "xxx". */ return 0 } if int32((*TTable)(unsafe.Pointer(pDest)).FeTabType) == int32(TABTYP_VTAB) { return 0 /* tab1 must not be a virtual table */ } if onError == int32(OE_Default) { if int32((*TTable)(unsafe.Pointer(pDest)).FiPKey) >= 0 { onError = int32((*TTable)(unsafe.Pointer(pDest)).FkeyConf) } if onError == int32(OE_Default) { onError = int32(OE_Abort) } } _ = libc.Int32FromInt32(0) /* allocated even if there is no FROM clause */ if (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpSrc)).FnSrc != int32(1) { return 0 /* FROM clause must have exactly one term */ } if (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpSrc + 8))).FpSelect != 0 { return 0 /* FROM clause cannot contain a subquery */ } if (*TSelect)(unsafe.Pointer(pSelect)).FpWhere != 0 { return 0 /* SELECT may not have a WHERE clause */ } if (*TSelect)(unsafe.Pointer(pSelect)).FpOrderBy != 0 { return 0 /* SELECT may not have an ORDER BY clause */ } /* Do not need to test for a HAVING clause. If HAVING is present but ** there is no ORDER BY, we will get an error. */ if (*TSelect)(unsafe.Pointer(pSelect)).FpGroupBy != 0 { return 0 /* SELECT may not have a GROUP BY clause */ } if (*TSelect)(unsafe.Pointer(pSelect)).FpLimit != 0 { return 0 /* SELECT may not have a LIMIT clause */ } if (*TSelect)(unsafe.Pointer(pSelect)).FpPrior != 0 { return 0 /* SELECT may not be a compound query */ } if (*TSelect)(unsafe.Pointer(pSelect)).FselFlags&uint32(SF_Distinct) != 0 { return 0 /* SELECT may not be DISTINCT */ } pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList _ = libc.Int32FromInt32(0) if (*TExprList)(unsafe.Pointer(pEList)).FnExpr != int32(1) { return 0 /* The result set must have exactly one column */ } _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pEList + 8))).FpExpr)).Fop) != int32(TK_ASTERISK) { return 0 /* The result set must be the special operator "*" */ } /* At this point we have established that the statement is of the ** correct syntactic form to participate in this optimization. Now ** we have to check the semantics. */ pItem = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc + 8 pSrc = _sqlite3LocateTableItem(tls, pParse, uint32(0), pItem) if pSrc == uintptr(0) { return 0 /* FROM clause does not contain a real table */ } if (*TTable)(unsafe.Pointer(pSrc)).Ftnum == (*TTable)(unsafe.Pointer(pDest)).Ftnum && (*TTable)(unsafe.Pointer(pSrc)).FpSchema == (*TTable)(unsafe.Pointer(pDest)).FpSchema { /* Possible due to bad sqlite_schema.rootpage */ return 0 /* tab1 and tab2 may not be the same table */ } if libc.BoolInt32((*TTable)(unsafe.Pointer(pDest)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0)) != libc.BoolInt32((*TTable)(unsafe.Pointer(pSrc)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0)) { return 0 /* source and destination must both be WITHOUT ROWID or not */ } if !(int32((*TTable)(unsafe.Pointer(pSrc)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) { return 0 /* tab2 may not be a view or virtual table */ } if int32((*TTable)(unsafe.Pointer(pDest)).FnCol) != int32((*TTable)(unsafe.Pointer(pSrc)).FnCol) { return 0 /* Number of columns must be the same in tab1 and tab2 */ } if int32((*TTable)(unsafe.Pointer(pDest)).FiPKey) != int32((*TTable)(unsafe.Pointer(pSrc)).FiPKey) { return 0 /* Both tables must have the same INTEGER PRIMARY KEY */ } if (*TTable)(unsafe.Pointer(pDest)).FtabFlags&uint32(TF_Strict) != uint32(0) && (*TTable)(unsafe.Pointer(pSrc)).FtabFlags&uint32(TF_Strict) == uint32(0) { return 0 /* Cannot feed from a non-strict into a strict table */ } i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pDest)).FnCol)) { break } pDestCol = (*TTable)(unsafe.Pointer(pDest)).FaCol + uintptr(i)*12 pSrcCol = (*TTable)(unsafe.Pointer(pSrc)).FaCol + uintptr(i)*12 /* Even if tables t1 and t2 have identical schemas, if they contain ** generated columns, then this statement is semantically incorrect: ** ** INSERT INTO t2 SELECT * FROM t1; ** ** The reason is that generated column values are returned by the ** the SELECT statement on the right but the INSERT statement on the ** left wants them to be omitted. ** ** Nevertheless, this is a useful notational shorthand to tell SQLite ** to do a bulk transfer all of the content from t1 over to t2. ** ** We could, in theory, disable this (except for internal use by the ** VACUUM command where it is actually needed). But why do that? It ** seems harmless enough, and provides a useful service. */ if int32((*TColumn)(unsafe.Pointer(pDestCol)).FcolFlags)&int32(COLFLAG_GENERATED) != int32((*TColumn)(unsafe.Pointer(pSrcCol)).FcolFlags)&int32(COLFLAG_GENERATED) { return 0 /* Both columns have the same generated-column type */ } /* But the transfer is only allowed if both the source and destination ** tables have the exact same expressions for generated columns. ** This requirement could be relaxed for VIRTUAL columns, I suppose. */ if int32((*TColumn)(unsafe.Pointer(pDestCol)).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { if _sqlite3ExprCompare(tls, uintptr(0), _sqlite3ColumnExpr(tls, pSrc, pSrcCol), _sqlite3ColumnExpr(tls, pDest, pDestCol), -int32(1)) != 0 { return 0 /* Different generator expressions */ } } if int32((*TColumn)(unsafe.Pointer(pDestCol)).Faffinity) != int32((*TColumn)(unsafe.Pointer(pSrcCol)).Faffinity) { return 0 /* Affinity must be the same on all columns */ } if Xsqlite3_stricmp(tls, _sqlite3ColumnColl(tls, pDestCol), _sqlite3ColumnColl(tls, pSrcCol)) != 0 { return 0 /* Collating sequence must be the same on all columns */ } if int32(uint32(*(*uint8)(unsafe.Pointer(pDestCol + 4))&0xf>>0)) != 0 && !(int32(uint32(*(*uint8)(unsafe.Pointer(pSrcCol + 4))&0xf>>0)) != 0) { return 0 /* tab2 must be NOT NULL if tab1 is */ } /* Default values for second and subsequent columns need to match. */ if int32((*TColumn)(unsafe.Pointer(pDestCol)).FcolFlags)&int32(COLFLAG_GENERATED) == 0 && i > 0 { pDestExpr = _sqlite3ColumnExpr(tls, pDest, pDestCol) pSrcExpr = _sqlite3ColumnExpr(tls, pSrc, pSrcCol) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if libc.BoolInt32(pDestExpr == uintptr(0)) != libc.BoolInt32(pSrcExpr == uintptr(0)) || pDestExpr != uintptr(0) && libc.Xstrcmp(tls, *(*uintptr)(unsafe.Pointer(pDestExpr + 8)), *(*uintptr)(unsafe.Pointer(pSrcExpr + 8))) != 0 { return 0 /* Default values must be the same for all columns */ } } goto _1 _1: ; i++ } pDestIdx = (*TTable)(unsafe.Pointer(pDest)).FpIndex for { if !(pDestIdx != 0) { break } if int32((*TIndex)(unsafe.Pointer(pDestIdx)).FonError) != OE_None { destHasUniqueIdx = int32(1) } pSrcIdx = (*TTable)(unsafe.Pointer(pSrc)).FpIndex for { if !(pSrcIdx != 0) { break } if _xferCompatibleIndex(tls, pDestIdx, pSrcIdx) != 0 { break } goto _3 _3: ; pSrcIdx = (*TIndex)(unsafe.Pointer(pSrcIdx)).FpNext } if pSrcIdx == uintptr(0) { return 0 /* pDestIdx has no corresponding index in pSrc */ } if (*TIndex)(unsafe.Pointer(pSrcIdx)).Ftnum == (*TIndex)(unsafe.Pointer(pDestIdx)).Ftnum && (*TTable)(unsafe.Pointer(pSrc)).FpSchema == (*TTable)(unsafe.Pointer(pDest)).FpSchema && _sqlite3FaultSim(tls, int32(411)) == SQLITE_OK { /* The sqlite3FaultSim() call allows this corruption test to be ** bypassed during testing, in order to exercise other corruption tests ** further downstream. */ return 0 /* Corrupt schema - two indexes on the same btree */ } goto _2 _2: ; pDestIdx = (*TIndex)(unsafe.Pointer(pDestIdx)).FpNext } if (*TTable)(unsafe.Pointer(pDest)).FpCheck != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) == uint32(0) && _sqlite3ExprListCompare(tls, (*TTable)(unsafe.Pointer(pSrc)).FpCheck, (*TTable)(unsafe.Pointer(pDest)).FpCheck, -int32(1)) != 0 { return 0 /* Tables have different CHECK constraints. Ticket #2252 */ } /* Disallow the transfer optimization if the destination table contains ** any foreign key constraints. This is more restrictive than necessary. ** But the main beneficiary of the transfer optimization is the VACUUM ** command, and the VACUUM command disables foreign key constraints. So ** the extra complication to make this rule less restrictive is probably ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e] */ _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) != uint64(0) && (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pDest + 44))).FpFKey != uintptr(0) { return 0 } if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00001))<= 0 { addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iSrc, regRowid) if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) == uint32(0) { addr2 = _sqlite3VdbeAddOp3(tls, v, int32(OP_NotExists), iDest, 0, regRowid) _sqlite3RowidConstraint(tls, pParse, onError, pDest) _sqlite3VdbeJumpHere(tls, v, addr2) } _autoIncStep(tls, pParse, regAutoinc, regRowid) } else { if (*TTable)(unsafe.Pointer(pDest)).FpIndex == uintptr(0) && !((*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&libc.Uint32FromInt32(DBFLAG_VacuumInto) != 0) { addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), iDest, regRowid) } else { addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iSrc, regRowid) _ = libc.Int32FromInt32(0) } } if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) != 0 { _sqlite3VdbeAddOp1(tls, v, int32(OP_SeekEnd), iDest) insFlags = uint8(libc.Int32FromInt32(OPFLAG_APPEND) | libc.Int32FromInt32(OPFLAG_USESEEKRESULT) | libc.Int32FromInt32(OPFLAG_PREFORMAT)) } else { insFlags = uint8(libc.Int32FromInt32(OPFLAG_NCHANGE) | libc.Int32FromInt32(OPFLAG_LASTROWID) | libc.Int32FromInt32(OPFLAG_APPEND) | libc.Int32FromInt32(OPFLAG_PREFORMAT)) } if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) == uint32(0) { _sqlite3VdbeAddOp3(tls, v, int32(OP_RowData), iSrc, regData, int32(1)) insFlags = Tu8(int32(insFlags) & ^libc.Int32FromInt32(OPFLAG_PREFORMAT)) } else { _sqlite3VdbeAddOp3(tls, v, int32(OP_RowCell), iDest, iSrc, regRowid) } _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iDest, regData, regRowid) if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) == uint32(0) { _sqlite3VdbeChangeP4(tls, v, -int32(1), pDest, -int32(5)) } _sqlite3VdbeChangeP5(tls, v, uint16(insFlags)) _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iSrc, addr1) _sqlite3VdbeAddOp2(tls, v, int32(OP_Close), iSrc, 0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Close), iDest, 0) } else { _sqlite3TableLock(tls, pParse, iDbDest, (*TTable)(unsafe.Pointer(pDest)).Ftnum, uint8(1), (*TTable)(unsafe.Pointer(pDest)).FzName) _sqlite3TableLock(tls, pParse, iDbSrc, (*TTable)(unsafe.Pointer(pSrc)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pSrc)).FzName) } pDestIdx = (*TTable)(unsafe.Pointer(pDest)).FpIndex for { if !(pDestIdx != 0) { break } idxInsFlags = uint8(0) pSrcIdx = (*TTable)(unsafe.Pointer(pSrc)).FpIndex for { if !(pSrcIdx != 0) { break } if _xferCompatibleIndex(tls, pDestIdx, pSrcIdx) != 0 { break } goto _9 _9: ; pSrcIdx = (*TIndex)(unsafe.Pointer(pSrcIdx)).FpNext } _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, int32(OP_OpenRead), iSrc, int32((*TIndex)(unsafe.Pointer(pSrcIdx)).Ftnum), iDbSrc) _sqlite3VdbeSetP4KeyInfo(tls, pParse, pSrcIdx) _sqlite3VdbeAddOp3(tls, v, int32(OP_OpenWrite), iDest, int32((*TIndex)(unsafe.Pointer(pDestIdx)).Ftnum), iDbDest) _sqlite3VdbeSetP4KeyInfo(tls, pParse, pDestIdx) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_BULKCSR)) addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), iSrc, 0) if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) != 0 { /* This INSERT command is part of a VACUUM operation, which guarantees ** that the destination table is empty. If all indexed columns use ** collation sequence BINARY, then it can also be assumed that the ** index will be populated by inserting keys in strictly sorted ** order. In this case, instead of seeking within the b-tree as part ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the ** OP_IdxInsert to seek to the point within the b-tree where each key ** should be inserted. This is faster. ** ** If any of the indexed columns use a collation sequence other than ** BINARY, this optimization is disabled. This is because the user ** might change the definition of a collation sequence and then run ** a VACUUM command. In that case keys may not be written in strictly ** sorted order. */ i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pSrcIdx)).FnColumn)) { break } zColl = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pSrcIdx)).FazColl + uintptr(i)*4)) if Xsqlite3_stricmp(tls, uintptr(unsafe.Pointer(&_sqlite3StrBINARY)), zColl) != 0 { break } goto _10 _10: ; i++ } if i == int32((*TIndex)(unsafe.Pointer(pSrcIdx)).FnColumn) { idxInsFlags = uint8(libc.Int32FromInt32(OPFLAG_USESEEKRESULT) | libc.Int32FromInt32(OPFLAG_PREFORMAT)) _sqlite3VdbeAddOp1(tls, v, int32(OP_SeekEnd), iDest) _sqlite3VdbeAddOp2(tls, v, int32(OP_RowCell), iDest, iSrc) } } else { if !((*TTable)(unsafe.Pointer(pSrc)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pDestIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) { idxInsFlags = Tu8(int32(idxInsFlags) | libc.Int32FromInt32(OPFLAG_NCHANGE)) } } if int32(idxInsFlags) != libc.Int32FromInt32(OPFLAG_USESEEKRESULT)|libc.Int32FromInt32(OPFLAG_PREFORMAT) { _sqlite3VdbeAddOp3(tls, v, int32(OP_RowData), iSrc, regData, int32(1)) if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) == uint32(0) && !((*TTable)(unsafe.Pointer(pDest)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pDestIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) { _codeWithoutRowidPreupdate(tls, pParse, pDest, iDest, regData) } } _sqlite3VdbeAddOp2(tls, v, int32(OP_IdxInsert), iDest, regData) _sqlite3VdbeChangeP5(tls, v, uint16(int32(idxInsFlags)|int32(OPFLAG_APPEND))) _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iSrc, addr1+int32(1)) _sqlite3VdbeJumpHere(tls, v, addr1) _sqlite3VdbeAddOp2(tls, v, int32(OP_Close), iSrc, 0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Close), iDest, 0) goto _8 _8: ; pDestIdx = (*TIndex)(unsafe.Pointer(pDestIdx)).FpNext } if emptySrcTest != 0 { _sqlite3VdbeJumpHere(tls, v, emptySrcTest) } _sqlite3ReleaseTempReg(tls, pParse, regRowid) _sqlite3ReleaseTempReg(tls, pParse, regData) if emptyDestTest != 0 { _sqlite3AutoincrementEnd(tls, pParse) _sqlite3VdbeAddOp2(tls, v, int32(OP_Halt), SQLITE_OK, 0) _sqlite3VdbeJumpHere(tls, v, emptyDestTest) _sqlite3VdbeAddOp2(tls, v, int32(OP_Close), iDest, 0) return 0 } else { return int32(1) } return r } /************** End of insert.c **********************************************/ /************** Begin file legacy.c ******************************************/ /* ** 2001 September 15 ** ** 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. ** ************************************************************************* ** Main file for the SQLite library. The routines in this file ** implement the programmer interface to the library. Routines in ** other files are for internal use by SQLite and should not be ** accessed by users of the library. */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** Execute SQL code. Return one of the SQLITE_ success/failure // ** codes. Also write an error message into memory obtained from // ** malloc() and make *pzErrMsg point to that message. // ** // ** If the SQL is a query, then for each row in the query result // ** the xCallback() function is called. pArg becomes the first // ** argument to xCallback(). If xCallback=NULL then no callback // ** is invoked, even for queries. // */ func Xsqlite3_exec(tls *libc.TLS, db uintptr, zSql uintptr, xCallback Tsqlite3_callback, pArg uintptr, pzErrMsg uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var azCols, azVals uintptr var callbackIsInit, i, nCol, rc int32 var _ /* pStmt at bp+4 */ uintptr var _ /* zLeftover at bp+0 */ uintptr _, _, _, _, _, _ = azCols, azVals, callbackIsInit, i, nCol, rc rc = SQLITE_OK /* Tail of unprocessed SQL */ *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) /* The current SQL statement */ azCols = uintptr(0) /* True if callback data is initialized */ if !(_sqlite3SafetyCheckOk(tls, db) != 0) { return _sqlite3MisuseError(tls, int32(135482)) } if zSql == uintptr(0) { zSql = __ccgo_ts + 1648 } Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) _sqlite3Error(tls, db, SQLITE_OK) for rc == SQLITE_OK && *(*uint8)(unsafe.Pointer(zSql)) != 0 { nCol = 0 azVals = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) rc = Xsqlite3_prepare_v2(tls, db, zSql, -int32(1), bp+4, bp) _ = libc.Int32FromInt32(0) if rc != SQLITE_OK { continue } if !(*(*uintptr)(unsafe.Pointer(bp + 4)) != 0) { /* this happens for a comment or white-space */ zSql = *(*uintptr)(unsafe.Pointer(bp)) continue } callbackIsInit = 0 for int32(1) != 0 { rc = Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) /* Invoke the callback function if required */ if xCallback != 0 && (int32(SQLITE_ROW) == rc || int32(SQLITE_DONE) == rc && !(callbackIsInit != 0) && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_NullCallback) != 0) { if !(callbackIsInit != 0) { nCol = Xsqlite3_column_count(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) azCols = _sqlite3DbMallocRaw(tls, db, uint64(uint32(libc.Int32FromInt32(2)*nCol+libc.Int32FromInt32(1))*uint32(4))) if azCols == uintptr(0) { goto exec_out } i = 0 for { if !(i < nCol) { break } *(*uintptr)(unsafe.Pointer(azCols + uintptr(i)*4)) = Xsqlite3_column_name(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), i) /* sqlite3VdbeSetColName() installs column names as UTF8 ** strings so there is no way for sqlite3_column_name() to fail. */ _ = libc.Int32FromInt32(0) goto _1 _1: ; i++ } callbackIsInit = int32(1) } if rc == int32(SQLITE_ROW) { azVals = azCols + uintptr(nCol)*4 i = 0 for { if !(i < nCol) { break } *(*uintptr)(unsafe.Pointer(azVals + uintptr(i)*4)) = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), i) if !(*(*uintptr)(unsafe.Pointer(azVals + uintptr(i)*4)) != 0) && Xsqlite3_column_type(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), i) != int32(SQLITE_NULL) { _sqlite3OomFault(tls, db) goto exec_out } goto _2 _2: ; i++ } *(*uintptr)(unsafe.Pointer(azVals + uintptr(i)*4)) = uintptr(0) } if (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xCallback})))(tls, pArg, nCol, azVals, azCols) != 0 { /* EVIDENCE-OF: R-38229-40159 If the callback function to ** sqlite3_exec() returns non-zero, then sqlite3_exec() will ** return SQLITE_ABORT. */ rc = int32(SQLITE_ABORT) _sqlite3VdbeFinalize(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) _sqlite3Error(tls, db, int32(SQLITE_ABORT)) goto exec_out } } if rc != int32(SQLITE_ROW) { rc = _sqlite3VdbeFinalize(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) zSql = *(*uintptr)(unsafe.Pointer(bp)) for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zSql))])&int32(0x01) != 0 { zSql++ } break } } _sqlite3DbFree(tls, db, azCols) azCols = uintptr(0) } goto exec_out exec_out: ; if *(*uintptr)(unsafe.Pointer(bp + 4)) != 0 { _sqlite3VdbeFinalize(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) } _sqlite3DbFree(tls, db, azCols) rc = _sqlite3ApiExit(tls, db, rc) if rc != SQLITE_OK && pzErrMsg != 0 { *(*uintptr)(unsafe.Pointer(pzErrMsg)) = _sqlite3DbStrDup(tls, uintptr(0), Xsqlite3_errmsg(tls, db)) if *(*uintptr)(unsafe.Pointer(pzErrMsg)) == uintptr(0) { rc = int32(SQLITE_NOMEM) _sqlite3Error(tls, db, int32(SQLITE_NOMEM)) } } else { if pzErrMsg != 0 { *(*uintptr)(unsafe.Pointer(pzErrMsg)) = uintptr(0) } } _ = libc.Int32FromInt32(0) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } /************** End of legacy.c **********************************************/ /************** Begin file loadext.c *****************************************/ /* ** 2006 June 7 ** ** 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 contains code used to dynamically load extensions into ** the SQLite library. */ /************** Include sqlite3ext.h in the middle of loadext.c **************/ /************** Begin file sqlite3ext.h **************************************/ /* ** 2006 June 7 ** ** 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 header file defines the SQLite interface for use by ** shared libraries that want to be imported as extensions into ** an SQLite instance. Shared libraries that intend to be loaded ** as extensions by SQLite should #include this file instead of ** sqlite3.h. */ /* #include "sqlite3.h" */ /* ** The following structure holds pointers to all of the SQLite API ** routines. ** ** WARNING: In order to maintain backwards compatibility, add new ** interfaces to the end of this structure only. If you insert new ** interfaces in the middle of this structure, then older different ** versions of SQLite will not be able to load each other's shared ** libraries! */ type Tsqlite3_api_routines1 = struct { Faggregate_context uintptr Faggregate_count uintptr Fbind_blob uintptr Fbind_double uintptr Fbind_int uintptr Fbind_int64 uintptr Fbind_null uintptr Fbind_parameter_count uintptr Fbind_parameter_index uintptr Fbind_parameter_name uintptr Fbind_text uintptr Fbind_text16 uintptr Fbind_value uintptr Fbusy_handler uintptr Fbusy_timeout uintptr Fchanges uintptr Fclose1 uintptr Fcollation_needed uintptr Fcollation_needed16 uintptr Fcolumn_blob uintptr Fcolumn_bytes uintptr Fcolumn_bytes16 uintptr Fcolumn_count uintptr Fcolumn_database_name uintptr Fcolumn_database_name16 uintptr Fcolumn_decltype uintptr Fcolumn_decltype16 uintptr Fcolumn_double uintptr Fcolumn_int uintptr Fcolumn_int64 uintptr Fcolumn_name uintptr Fcolumn_name16 uintptr Fcolumn_origin_name uintptr Fcolumn_origin_name16 uintptr Fcolumn_table_name uintptr Fcolumn_table_name16 uintptr Fcolumn_text uintptr Fcolumn_text16 uintptr Fcolumn_type uintptr Fcolumn_value uintptr Fcommit_hook uintptr Fcomplete uintptr Fcomplete16 uintptr Fcreate_collation uintptr Fcreate_collation16 uintptr Fcreate_function uintptr Fcreate_function16 uintptr Fcreate_module uintptr Fdata_count uintptr Fdb_handle uintptr Fdeclare_vtab uintptr Fenable_shared_cache uintptr Ferrcode uintptr Ferrmsg uintptr Ferrmsg16 uintptr Fexec uintptr Fexpired uintptr Ffinalize uintptr Ffree uintptr Ffree_table uintptr Fget_autocommit uintptr Fget_auxdata uintptr Fget_table uintptr Fglobal_recover uintptr Finterruptx uintptr Flast_insert_rowid uintptr Flibversion uintptr Flibversion_number uintptr Fmalloc uintptr Fmprintf uintptr Fopen uintptr Fopen16 uintptr Fprepare uintptr Fprepare16 uintptr Fprofile uintptr Fprogress_handler uintptr Frealloc uintptr Freset uintptr Fresult_blob uintptr Fresult_double uintptr Fresult_error uintptr Fresult_error16 uintptr Fresult_int uintptr Fresult_int64 uintptr Fresult_null uintptr Fresult_text uintptr Fresult_text16 uintptr Fresult_text16be uintptr Fresult_text16le uintptr Fresult_value uintptr Frollback_hook uintptr Fset_authorizer uintptr Fset_auxdata uintptr Fxsnprintf uintptr Fstep uintptr Ftable_column_metadata uintptr Fthread_cleanup uintptr Ftotal_changes uintptr Ftrace uintptr Ftransfer_bindings uintptr Fupdate_hook uintptr Fuser_data uintptr Fvalue_blob uintptr Fvalue_bytes uintptr Fvalue_bytes16 uintptr Fvalue_double uintptr Fvalue_int uintptr Fvalue_int64 uintptr Fvalue_numeric_type uintptr Fvalue_text uintptr Fvalue_text16 uintptr Fvalue_text16be uintptr Fvalue_text16le uintptr Fvalue_type uintptr Fvmprintf uintptr Foverload_function uintptr Fprepare_v2 uintptr Fprepare16_v2 uintptr Fclear_bindings uintptr Fcreate_module_v2 uintptr Fbind_zeroblob uintptr Fblob_bytes uintptr Fblob_close uintptr Fblob_open uintptr Fblob_read uintptr Fblob_write uintptr Fcreate_collation_v2 uintptr Ffile_control uintptr Fmemory_highwater uintptr Fmemory_used uintptr Fmutex_alloc uintptr Fmutex_enter uintptr Fmutex_free uintptr Fmutex_leave uintptr Fmutex_try uintptr Fopen_v2 uintptr Frelease_memory uintptr Fresult_error_nomem uintptr Fresult_error_toobig uintptr Fsleep uintptr Fsoft_heap_limit uintptr Fvfs_find uintptr Fvfs_register uintptr Fvfs_unregister uintptr Fxthreadsafe uintptr Fresult_zeroblob uintptr Fresult_error_code uintptr Ftest_control uintptr Frandomness uintptr Fcontext_db_handle uintptr Fextended_result_codes uintptr Flimit uintptr Fnext_stmt uintptr Fsql uintptr Fstatus uintptr Fbackup_finish uintptr Fbackup_init uintptr Fbackup_pagecount uintptr Fbackup_remaining uintptr Fbackup_step uintptr Fcompileoption_get uintptr Fcompileoption_used uintptr Fcreate_function_v2 uintptr Fdb_config uintptr Fdb_mutex uintptr Fdb_status uintptr Fextended_errcode uintptr Flog uintptr Fsoft_heap_limit64 uintptr Fsourceid uintptr Fstmt_status uintptr Fstrnicmp uintptr Funlock_notify uintptr Fwal_autocheckpoint uintptr Fwal_checkpoint uintptr Fwal_hook uintptr Fblob_reopen uintptr Fvtab_config uintptr Fvtab_on_conflict uintptr Fclose_v2 uintptr Fdb_filename uintptr Fdb_readonly uintptr Fdb_release_memory uintptr Ferrstr uintptr Fstmt_busy uintptr Fstmt_readonly uintptr Fstricmp uintptr Furi_boolean uintptr Furi_int64 uintptr Furi_parameter uintptr Fxvsnprintf uintptr Fwal_checkpoint_v2 uintptr Fauto_extension uintptr Fbind_blob64 uintptr Fbind_text64 uintptr Fcancel_auto_extension uintptr Fload_extension uintptr Fmalloc64 uintptr Fmsize uintptr Frealloc64 uintptr Freset_auto_extension uintptr Fresult_blob64 uintptr Fresult_text64 uintptr Fstrglob uintptr Fvalue_dup uintptr Fvalue_free uintptr Fresult_zeroblob64 uintptr Fbind_zeroblob64 uintptr Fvalue_subtype uintptr Fresult_subtype uintptr Fstatus64 uintptr Fstrlike uintptr Fdb_cacheflush uintptr Fsystem_errno uintptr Ftrace_v2 uintptr Fexpanded_sql uintptr Fset_last_insert_rowid uintptr Fprepare_v3 uintptr Fprepare16_v3 uintptr Fbind_pointer uintptr Fresult_pointer uintptr Fvalue_pointer uintptr Fvtab_nochange uintptr Fvalue_nochange uintptr Fvtab_collation uintptr Fkeyword_count uintptr Fkeyword_name uintptr Fkeyword_check uintptr Fstr_new uintptr Fstr_finish uintptr Fstr_appendf uintptr Fstr_vappendf uintptr Fstr_append uintptr Fstr_appendall uintptr Fstr_appendchar uintptr Fstr_reset uintptr Fstr_errcode uintptr Fstr_length uintptr Fstr_value uintptr Fcreate_window_function uintptr Fnormalized_sql uintptr Fstmt_isexplain uintptr Fvalue_frombind uintptr Fdrop_modules uintptr Fhard_heap_limit64 uintptr Furi_key uintptr Ffilename_database uintptr Ffilename_journal uintptr Ffilename_wal uintptr Fcreate_filename uintptr Ffree_filename uintptr Fdatabase_file_object uintptr Ftxn_state uintptr Fchanges64 uintptr Ftotal_changes64 uintptr Fautovacuum_pages uintptr Ferror_offset uintptr Fvtab_rhs_value uintptr Fvtab_distinct uintptr Fvtab_in uintptr Fvtab_in_first uintptr Fvtab_in_next uintptr Fdeserialize uintptr Fserialize uintptr Fdb_name uintptr Fvalue_encoding uintptr Fis_interrupted uintptr Fstmt_explain uintptr Fget_clientdata uintptr Fset_clientdata uintptr } type sqlite3_api_routines1 = Tsqlite3_api_routines1 // C documentation // // /* // ** This is the function signature used for all extension entry points. It // ** is also defined in the file "loadext.c". // */ type Tsqlite3_loadext_entry = uintptr type sqlite3_loadext_entry = Tsqlite3_loadext_entry /* ** The following macros redefine the API routines so that they are ** redirected through the global sqlite3_api structure. ** ** This header file is also used by the loadext.c source file ** (part of the main SQLite library - not an extension) so that ** it can get access to the sqlite3_api_routines structure ** definition. But the main library does not want to redefine ** the API. So the redefinition macros are only valid if the ** SQLITE_CORE macros is undefined. */ /* This case when the file is being statically linked into the ** application */ /************** End of sqlite3ext.h ******************************************/ /************** Continuing where we left off in loadext.c ********************/ /* #include "sqliteInt.h" */ /* ** Some API routines are omitted when various features are ** excluded from a build of SQLite. Substitute a NULL pointer ** for any missing APIs. */ // C documentation // // /* // ** The following structure contains pointers to all SQLite API routines. // ** A pointer to this structure is passed into extensions when they are // ** loaded so that the extension can make calls back into the SQLite // ** library. // ** // ** When adding new APIs, add them to the bottom of this structure // ** in order to preserve backwards compatibility. // ** // ** Extensions that use newer APIs should first call the // ** sqlite3_libversion_number() to make sure that the API they // ** intend to use is supported by the library. Extensions should // ** also check to make sure that the pointer to the function is // ** not NULL before calling it. // */ var _sqlite3Apis = Tsqlite3_api_routines{} func init() { p := unsafe.Pointer(&_sqlite3Apis) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(Xsqlite3_aggregate_context) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(Xsqlite3_aggregate_count) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(Xsqlite3_bind_blob) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(Xsqlite3_bind_double) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(Xsqlite3_bind_int) *(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(Xsqlite3_bind_int64) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(Xsqlite3_bind_null) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(Xsqlite3_bind_parameter_count) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(Xsqlite3_bind_parameter_index) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(Xsqlite3_bind_parameter_name) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(Xsqlite3_bind_text) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(Xsqlite3_bind_text16) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(Xsqlite3_bind_value) *(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(Xsqlite3_busy_handler) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(Xsqlite3_busy_timeout) *(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(Xsqlite3_changes) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(Xsqlite3_close) *(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(Xsqlite3_collation_needed) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(Xsqlite3_collation_needed16) *(*uintptr)(unsafe.Add(p, 76)) = __ccgo_fp(Xsqlite3_column_blob) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(Xsqlite3_column_bytes) *(*uintptr)(unsafe.Add(p, 84)) = __ccgo_fp(Xsqlite3_column_bytes16) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(Xsqlite3_column_count) *(*uintptr)(unsafe.Add(p, 92)) = __ccgo_fp(Xsqlite3_column_database_name) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(Xsqlite3_column_database_name16) *(*uintptr)(unsafe.Add(p, 100)) = __ccgo_fp(Xsqlite3_column_decltype) *(*uintptr)(unsafe.Add(p, 104)) = __ccgo_fp(Xsqlite3_column_decltype16) *(*uintptr)(unsafe.Add(p, 108)) = __ccgo_fp(Xsqlite3_column_double) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(Xsqlite3_column_int) *(*uintptr)(unsafe.Add(p, 116)) = __ccgo_fp(Xsqlite3_column_int64) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(Xsqlite3_column_name) *(*uintptr)(unsafe.Add(p, 124)) = __ccgo_fp(Xsqlite3_column_name16) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(Xsqlite3_column_origin_name) *(*uintptr)(unsafe.Add(p, 132)) = __ccgo_fp(Xsqlite3_column_origin_name16) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(Xsqlite3_column_table_name) *(*uintptr)(unsafe.Add(p, 140)) = __ccgo_fp(Xsqlite3_column_table_name16) *(*uintptr)(unsafe.Add(p, 144)) = __ccgo_fp(Xsqlite3_column_text) *(*uintptr)(unsafe.Add(p, 148)) = __ccgo_fp(Xsqlite3_column_text16) *(*uintptr)(unsafe.Add(p, 152)) = __ccgo_fp(Xsqlite3_column_type) *(*uintptr)(unsafe.Add(p, 156)) = __ccgo_fp(Xsqlite3_column_value) *(*uintptr)(unsafe.Add(p, 160)) = __ccgo_fp(Xsqlite3_commit_hook) *(*uintptr)(unsafe.Add(p, 164)) = __ccgo_fp(Xsqlite3_complete) *(*uintptr)(unsafe.Add(p, 168)) = __ccgo_fp(Xsqlite3_complete16) *(*uintptr)(unsafe.Add(p, 172)) = __ccgo_fp(Xsqlite3_create_collation) *(*uintptr)(unsafe.Add(p, 176)) = __ccgo_fp(Xsqlite3_create_collation16) *(*uintptr)(unsafe.Add(p, 180)) = __ccgo_fp(Xsqlite3_create_function) *(*uintptr)(unsafe.Add(p, 184)) = __ccgo_fp(Xsqlite3_create_function16) *(*uintptr)(unsafe.Add(p, 188)) = __ccgo_fp(Xsqlite3_create_module) *(*uintptr)(unsafe.Add(p, 192)) = __ccgo_fp(Xsqlite3_data_count) *(*uintptr)(unsafe.Add(p, 196)) = __ccgo_fp(Xsqlite3_db_handle) *(*uintptr)(unsafe.Add(p, 200)) = __ccgo_fp(Xsqlite3_declare_vtab) *(*uintptr)(unsafe.Add(p, 204)) = __ccgo_fp(Xsqlite3_enable_shared_cache) *(*uintptr)(unsafe.Add(p, 208)) = __ccgo_fp(Xsqlite3_errcode) *(*uintptr)(unsafe.Add(p, 212)) = __ccgo_fp(Xsqlite3_errmsg) *(*uintptr)(unsafe.Add(p, 216)) = __ccgo_fp(Xsqlite3_errmsg16) *(*uintptr)(unsafe.Add(p, 220)) = __ccgo_fp(Xsqlite3_exec) *(*uintptr)(unsafe.Add(p, 224)) = __ccgo_fp(Xsqlite3_expired) *(*uintptr)(unsafe.Add(p, 228)) = __ccgo_fp(Xsqlite3_finalize) *(*uintptr)(unsafe.Add(p, 232)) = __ccgo_fp(Xsqlite3_free) *(*uintptr)(unsafe.Add(p, 236)) = __ccgo_fp(Xsqlite3_free_table) *(*uintptr)(unsafe.Add(p, 240)) = __ccgo_fp(Xsqlite3_get_autocommit) *(*uintptr)(unsafe.Add(p, 244)) = __ccgo_fp(Xsqlite3_get_auxdata) *(*uintptr)(unsafe.Add(p, 248)) = __ccgo_fp(Xsqlite3_get_table) *(*uintptr)(unsafe.Add(p, 256)) = __ccgo_fp(Xsqlite3_interrupt) *(*uintptr)(unsafe.Add(p, 260)) = __ccgo_fp(Xsqlite3_last_insert_rowid) *(*uintptr)(unsafe.Add(p, 264)) = __ccgo_fp(Xsqlite3_libversion) *(*uintptr)(unsafe.Add(p, 268)) = __ccgo_fp(Xsqlite3_libversion_number) *(*uintptr)(unsafe.Add(p, 272)) = __ccgo_fp(Xsqlite3_malloc) *(*uintptr)(unsafe.Add(p, 276)) = __ccgo_fp(Xsqlite3_mprintf) *(*uintptr)(unsafe.Add(p, 280)) = __ccgo_fp(Xsqlite3_open) *(*uintptr)(unsafe.Add(p, 284)) = __ccgo_fp(Xsqlite3_open16) *(*uintptr)(unsafe.Add(p, 288)) = __ccgo_fp(Xsqlite3_prepare) *(*uintptr)(unsafe.Add(p, 292)) = __ccgo_fp(Xsqlite3_prepare16) *(*uintptr)(unsafe.Add(p, 296)) = __ccgo_fp(Xsqlite3_profile) *(*uintptr)(unsafe.Add(p, 300)) = __ccgo_fp(Xsqlite3_progress_handler) *(*uintptr)(unsafe.Add(p, 304)) = __ccgo_fp(Xsqlite3_realloc) *(*uintptr)(unsafe.Add(p, 308)) = __ccgo_fp(Xsqlite3_reset) *(*uintptr)(unsafe.Add(p, 312)) = __ccgo_fp(Xsqlite3_result_blob) *(*uintptr)(unsafe.Add(p, 316)) = __ccgo_fp(Xsqlite3_result_double) *(*uintptr)(unsafe.Add(p, 320)) = __ccgo_fp(Xsqlite3_result_error) *(*uintptr)(unsafe.Add(p, 324)) = __ccgo_fp(Xsqlite3_result_error16) *(*uintptr)(unsafe.Add(p, 328)) = __ccgo_fp(Xsqlite3_result_int) *(*uintptr)(unsafe.Add(p, 332)) = __ccgo_fp(Xsqlite3_result_int64) *(*uintptr)(unsafe.Add(p, 336)) = __ccgo_fp(Xsqlite3_result_null) *(*uintptr)(unsafe.Add(p, 340)) = __ccgo_fp(Xsqlite3_result_text) *(*uintptr)(unsafe.Add(p, 344)) = __ccgo_fp(Xsqlite3_result_text16) *(*uintptr)(unsafe.Add(p, 348)) = __ccgo_fp(Xsqlite3_result_text16be) *(*uintptr)(unsafe.Add(p, 352)) = __ccgo_fp(Xsqlite3_result_text16le) *(*uintptr)(unsafe.Add(p, 356)) = __ccgo_fp(Xsqlite3_result_value) *(*uintptr)(unsafe.Add(p, 360)) = __ccgo_fp(Xsqlite3_rollback_hook) *(*uintptr)(unsafe.Add(p, 364)) = __ccgo_fp(Xsqlite3_set_authorizer) *(*uintptr)(unsafe.Add(p, 368)) = __ccgo_fp(Xsqlite3_set_auxdata) *(*uintptr)(unsafe.Add(p, 372)) = __ccgo_fp(Xsqlite3_snprintf) *(*uintptr)(unsafe.Add(p, 376)) = __ccgo_fp(Xsqlite3_step) *(*uintptr)(unsafe.Add(p, 380)) = __ccgo_fp(Xsqlite3_table_column_metadata) *(*uintptr)(unsafe.Add(p, 384)) = __ccgo_fp(Xsqlite3_thread_cleanup) *(*uintptr)(unsafe.Add(p, 388)) = __ccgo_fp(Xsqlite3_total_changes) *(*uintptr)(unsafe.Add(p, 392)) = __ccgo_fp(Xsqlite3_trace) *(*uintptr)(unsafe.Add(p, 396)) = __ccgo_fp(Xsqlite3_transfer_bindings) *(*uintptr)(unsafe.Add(p, 400)) = __ccgo_fp(Xsqlite3_update_hook) *(*uintptr)(unsafe.Add(p, 404)) = __ccgo_fp(Xsqlite3_user_data) *(*uintptr)(unsafe.Add(p, 408)) = __ccgo_fp(Xsqlite3_value_blob) *(*uintptr)(unsafe.Add(p, 412)) = __ccgo_fp(Xsqlite3_value_bytes) *(*uintptr)(unsafe.Add(p, 416)) = __ccgo_fp(Xsqlite3_value_bytes16) *(*uintptr)(unsafe.Add(p, 420)) = __ccgo_fp(Xsqlite3_value_double) *(*uintptr)(unsafe.Add(p, 424)) = __ccgo_fp(Xsqlite3_value_int) *(*uintptr)(unsafe.Add(p, 428)) = __ccgo_fp(Xsqlite3_value_int64) *(*uintptr)(unsafe.Add(p, 432)) = __ccgo_fp(Xsqlite3_value_numeric_type) *(*uintptr)(unsafe.Add(p, 436)) = __ccgo_fp(Xsqlite3_value_text) *(*uintptr)(unsafe.Add(p, 440)) = __ccgo_fp(Xsqlite3_value_text16) *(*uintptr)(unsafe.Add(p, 444)) = __ccgo_fp(Xsqlite3_value_text16be) *(*uintptr)(unsafe.Add(p, 448)) = __ccgo_fp(Xsqlite3_value_text16le) *(*uintptr)(unsafe.Add(p, 452)) = __ccgo_fp(Xsqlite3_value_type) *(*uintptr)(unsafe.Add(p, 456)) = __ccgo_fp(Xsqlite3_vmprintf) *(*uintptr)(unsafe.Add(p, 460)) = __ccgo_fp(Xsqlite3_overload_function) *(*uintptr)(unsafe.Add(p, 464)) = __ccgo_fp(Xsqlite3_prepare_v2) *(*uintptr)(unsafe.Add(p, 468)) = __ccgo_fp(Xsqlite3_prepare16_v2) *(*uintptr)(unsafe.Add(p, 472)) = __ccgo_fp(Xsqlite3_clear_bindings) *(*uintptr)(unsafe.Add(p, 476)) = __ccgo_fp(Xsqlite3_create_module_v2) *(*uintptr)(unsafe.Add(p, 480)) = __ccgo_fp(Xsqlite3_bind_zeroblob) *(*uintptr)(unsafe.Add(p, 484)) = __ccgo_fp(Xsqlite3_blob_bytes) *(*uintptr)(unsafe.Add(p, 488)) = __ccgo_fp(Xsqlite3_blob_close) *(*uintptr)(unsafe.Add(p, 492)) = __ccgo_fp(Xsqlite3_blob_open) *(*uintptr)(unsafe.Add(p, 496)) = __ccgo_fp(Xsqlite3_blob_read) *(*uintptr)(unsafe.Add(p, 500)) = __ccgo_fp(Xsqlite3_blob_write) *(*uintptr)(unsafe.Add(p, 504)) = __ccgo_fp(Xsqlite3_create_collation_v2) *(*uintptr)(unsafe.Add(p, 508)) = __ccgo_fp(Xsqlite3_file_control) *(*uintptr)(unsafe.Add(p, 512)) = __ccgo_fp(Xsqlite3_memory_highwater) *(*uintptr)(unsafe.Add(p, 516)) = __ccgo_fp(Xsqlite3_memory_used) *(*uintptr)(unsafe.Add(p, 520)) = __ccgo_fp(Xsqlite3_mutex_alloc) *(*uintptr)(unsafe.Add(p, 524)) = __ccgo_fp(Xsqlite3_mutex_enter) *(*uintptr)(unsafe.Add(p, 528)) = __ccgo_fp(Xsqlite3_mutex_free) *(*uintptr)(unsafe.Add(p, 532)) = __ccgo_fp(Xsqlite3_mutex_leave) *(*uintptr)(unsafe.Add(p, 536)) = __ccgo_fp(Xsqlite3_mutex_try) *(*uintptr)(unsafe.Add(p, 540)) = __ccgo_fp(Xsqlite3_open_v2) *(*uintptr)(unsafe.Add(p, 544)) = __ccgo_fp(Xsqlite3_release_memory) *(*uintptr)(unsafe.Add(p, 548)) = __ccgo_fp(Xsqlite3_result_error_nomem) *(*uintptr)(unsafe.Add(p, 552)) = __ccgo_fp(Xsqlite3_result_error_toobig) *(*uintptr)(unsafe.Add(p, 556)) = __ccgo_fp(Xsqlite3_sleep) *(*uintptr)(unsafe.Add(p, 560)) = __ccgo_fp(Xsqlite3_soft_heap_limit) *(*uintptr)(unsafe.Add(p, 564)) = __ccgo_fp(Xsqlite3_vfs_find) *(*uintptr)(unsafe.Add(p, 568)) = __ccgo_fp(Xsqlite3_vfs_register) *(*uintptr)(unsafe.Add(p, 572)) = __ccgo_fp(Xsqlite3_vfs_unregister) *(*uintptr)(unsafe.Add(p, 576)) = __ccgo_fp(Xsqlite3_threadsafe) *(*uintptr)(unsafe.Add(p, 580)) = __ccgo_fp(Xsqlite3_result_zeroblob) *(*uintptr)(unsafe.Add(p, 584)) = __ccgo_fp(Xsqlite3_result_error_code) *(*uintptr)(unsafe.Add(p, 588)) = __ccgo_fp(Xsqlite3_test_control) *(*uintptr)(unsafe.Add(p, 592)) = __ccgo_fp(Xsqlite3_randomness) *(*uintptr)(unsafe.Add(p, 596)) = __ccgo_fp(Xsqlite3_context_db_handle) *(*uintptr)(unsafe.Add(p, 600)) = __ccgo_fp(Xsqlite3_extended_result_codes) *(*uintptr)(unsafe.Add(p, 604)) = __ccgo_fp(Xsqlite3_limit) *(*uintptr)(unsafe.Add(p, 608)) = __ccgo_fp(Xsqlite3_next_stmt) *(*uintptr)(unsafe.Add(p, 612)) = __ccgo_fp(Xsqlite3_sql) *(*uintptr)(unsafe.Add(p, 616)) = __ccgo_fp(Xsqlite3_status) *(*uintptr)(unsafe.Add(p, 620)) = __ccgo_fp(Xsqlite3_backup_finish) *(*uintptr)(unsafe.Add(p, 624)) = __ccgo_fp(Xsqlite3_backup_init) *(*uintptr)(unsafe.Add(p, 628)) = __ccgo_fp(Xsqlite3_backup_pagecount) *(*uintptr)(unsafe.Add(p, 632)) = __ccgo_fp(Xsqlite3_backup_remaining) *(*uintptr)(unsafe.Add(p, 636)) = __ccgo_fp(Xsqlite3_backup_step) *(*uintptr)(unsafe.Add(p, 640)) = __ccgo_fp(Xsqlite3_compileoption_get) *(*uintptr)(unsafe.Add(p, 644)) = __ccgo_fp(Xsqlite3_compileoption_used) *(*uintptr)(unsafe.Add(p, 648)) = __ccgo_fp(Xsqlite3_create_function_v2) *(*uintptr)(unsafe.Add(p, 652)) = __ccgo_fp(Xsqlite3_db_config) *(*uintptr)(unsafe.Add(p, 656)) = __ccgo_fp(Xsqlite3_db_mutex) *(*uintptr)(unsafe.Add(p, 660)) = __ccgo_fp(Xsqlite3_db_status) *(*uintptr)(unsafe.Add(p, 664)) = __ccgo_fp(Xsqlite3_extended_errcode) *(*uintptr)(unsafe.Add(p, 668)) = __ccgo_fp(Xsqlite3_log) *(*uintptr)(unsafe.Add(p, 672)) = __ccgo_fp(Xsqlite3_soft_heap_limit64) *(*uintptr)(unsafe.Add(p, 676)) = __ccgo_fp(Xsqlite3_sourceid) *(*uintptr)(unsafe.Add(p, 680)) = __ccgo_fp(Xsqlite3_stmt_status) *(*uintptr)(unsafe.Add(p, 684)) = __ccgo_fp(Xsqlite3_strnicmp) *(*uintptr)(unsafe.Add(p, 688)) = __ccgo_fp(Xsqlite3_unlock_notify) *(*uintptr)(unsafe.Add(p, 692)) = __ccgo_fp(Xsqlite3_wal_autocheckpoint) *(*uintptr)(unsafe.Add(p, 696)) = __ccgo_fp(Xsqlite3_wal_checkpoint) *(*uintptr)(unsafe.Add(p, 700)) = __ccgo_fp(Xsqlite3_wal_hook) *(*uintptr)(unsafe.Add(p, 704)) = __ccgo_fp(Xsqlite3_blob_reopen) *(*uintptr)(unsafe.Add(p, 708)) = __ccgo_fp(Xsqlite3_vtab_config) *(*uintptr)(unsafe.Add(p, 712)) = __ccgo_fp(Xsqlite3_vtab_on_conflict) *(*uintptr)(unsafe.Add(p, 716)) = __ccgo_fp(Xsqlite3_close_v2) *(*uintptr)(unsafe.Add(p, 720)) = __ccgo_fp(Xsqlite3_db_filename) *(*uintptr)(unsafe.Add(p, 724)) = __ccgo_fp(Xsqlite3_db_readonly) *(*uintptr)(unsafe.Add(p, 728)) = __ccgo_fp(Xsqlite3_db_release_memory) *(*uintptr)(unsafe.Add(p, 732)) = __ccgo_fp(Xsqlite3_errstr) *(*uintptr)(unsafe.Add(p, 736)) = __ccgo_fp(Xsqlite3_stmt_busy) *(*uintptr)(unsafe.Add(p, 740)) = __ccgo_fp(Xsqlite3_stmt_readonly) *(*uintptr)(unsafe.Add(p, 744)) = __ccgo_fp(Xsqlite3_stricmp) *(*uintptr)(unsafe.Add(p, 748)) = __ccgo_fp(Xsqlite3_uri_boolean) *(*uintptr)(unsafe.Add(p, 752)) = __ccgo_fp(Xsqlite3_uri_int64) *(*uintptr)(unsafe.Add(p, 756)) = __ccgo_fp(Xsqlite3_uri_parameter) *(*uintptr)(unsafe.Add(p, 760)) = __ccgo_fp(Xsqlite3_vsnprintf) *(*uintptr)(unsafe.Add(p, 764)) = __ccgo_fp(Xsqlite3_wal_checkpoint_v2) *(*uintptr)(unsafe.Add(p, 768)) = __ccgo_fp(Xsqlite3_auto_extension) *(*uintptr)(unsafe.Add(p, 772)) = __ccgo_fp(Xsqlite3_bind_blob64) *(*uintptr)(unsafe.Add(p, 776)) = __ccgo_fp(Xsqlite3_bind_text64) *(*uintptr)(unsafe.Add(p, 780)) = __ccgo_fp(Xsqlite3_cancel_auto_extension) *(*uintptr)(unsafe.Add(p, 784)) = __ccgo_fp(Xsqlite3_load_extension) *(*uintptr)(unsafe.Add(p, 788)) = __ccgo_fp(Xsqlite3_malloc64) *(*uintptr)(unsafe.Add(p, 792)) = __ccgo_fp(Xsqlite3_msize) *(*uintptr)(unsafe.Add(p, 796)) = __ccgo_fp(Xsqlite3_realloc64) *(*uintptr)(unsafe.Add(p, 800)) = __ccgo_fp(Xsqlite3_reset_auto_extension) *(*uintptr)(unsafe.Add(p, 804)) = __ccgo_fp(Xsqlite3_result_blob64) *(*uintptr)(unsafe.Add(p, 808)) = __ccgo_fp(Xsqlite3_result_text64) *(*uintptr)(unsafe.Add(p, 812)) = __ccgo_fp(Xsqlite3_strglob) *(*uintptr)(unsafe.Add(p, 816)) = __ccgo_fp(Xsqlite3_value_dup) *(*uintptr)(unsafe.Add(p, 820)) = __ccgo_fp(Xsqlite3_value_free) *(*uintptr)(unsafe.Add(p, 824)) = __ccgo_fp(Xsqlite3_result_zeroblob64) *(*uintptr)(unsafe.Add(p, 828)) = __ccgo_fp(Xsqlite3_bind_zeroblob64) *(*uintptr)(unsafe.Add(p, 832)) = __ccgo_fp(Xsqlite3_value_subtype) *(*uintptr)(unsafe.Add(p, 836)) = __ccgo_fp(Xsqlite3_result_subtype) *(*uintptr)(unsafe.Add(p, 840)) = __ccgo_fp(Xsqlite3_status64) *(*uintptr)(unsafe.Add(p, 844)) = __ccgo_fp(Xsqlite3_strlike) *(*uintptr)(unsafe.Add(p, 848)) = __ccgo_fp(Xsqlite3_db_cacheflush) *(*uintptr)(unsafe.Add(p, 852)) = __ccgo_fp(Xsqlite3_system_errno) *(*uintptr)(unsafe.Add(p, 856)) = __ccgo_fp(Xsqlite3_trace_v2) *(*uintptr)(unsafe.Add(p, 860)) = __ccgo_fp(Xsqlite3_expanded_sql) *(*uintptr)(unsafe.Add(p, 864)) = __ccgo_fp(Xsqlite3_set_last_insert_rowid) *(*uintptr)(unsafe.Add(p, 868)) = __ccgo_fp(Xsqlite3_prepare_v3) *(*uintptr)(unsafe.Add(p, 872)) = __ccgo_fp(Xsqlite3_prepare16_v3) *(*uintptr)(unsafe.Add(p, 876)) = __ccgo_fp(Xsqlite3_bind_pointer) *(*uintptr)(unsafe.Add(p, 880)) = __ccgo_fp(Xsqlite3_result_pointer) *(*uintptr)(unsafe.Add(p, 884)) = __ccgo_fp(Xsqlite3_value_pointer) *(*uintptr)(unsafe.Add(p, 888)) = __ccgo_fp(Xsqlite3_vtab_nochange) *(*uintptr)(unsafe.Add(p, 892)) = __ccgo_fp(Xsqlite3_value_nochange) *(*uintptr)(unsafe.Add(p, 896)) = __ccgo_fp(Xsqlite3_vtab_collation) *(*uintptr)(unsafe.Add(p, 900)) = __ccgo_fp(Xsqlite3_keyword_count) *(*uintptr)(unsafe.Add(p, 904)) = __ccgo_fp(Xsqlite3_keyword_name) *(*uintptr)(unsafe.Add(p, 908)) = __ccgo_fp(Xsqlite3_keyword_check) *(*uintptr)(unsafe.Add(p, 912)) = __ccgo_fp(Xsqlite3_str_new) *(*uintptr)(unsafe.Add(p, 916)) = __ccgo_fp(Xsqlite3_str_finish) *(*uintptr)(unsafe.Add(p, 920)) = __ccgo_fp(Xsqlite3_str_appendf) *(*uintptr)(unsafe.Add(p, 924)) = __ccgo_fp(Xsqlite3_str_vappendf) *(*uintptr)(unsafe.Add(p, 928)) = __ccgo_fp(Xsqlite3_str_append) *(*uintptr)(unsafe.Add(p, 932)) = __ccgo_fp(Xsqlite3_str_appendall) *(*uintptr)(unsafe.Add(p, 936)) = __ccgo_fp(Xsqlite3_str_appendchar) *(*uintptr)(unsafe.Add(p, 940)) = __ccgo_fp(Xsqlite3_str_reset) *(*uintptr)(unsafe.Add(p, 944)) = __ccgo_fp(Xsqlite3_str_errcode) *(*uintptr)(unsafe.Add(p, 948)) = __ccgo_fp(Xsqlite3_str_length) *(*uintptr)(unsafe.Add(p, 952)) = __ccgo_fp(Xsqlite3_str_value) *(*uintptr)(unsafe.Add(p, 956)) = __ccgo_fp(Xsqlite3_create_window_function) *(*uintptr)(unsafe.Add(p, 964)) = __ccgo_fp(Xsqlite3_stmt_isexplain) *(*uintptr)(unsafe.Add(p, 968)) = __ccgo_fp(Xsqlite3_value_frombind) *(*uintptr)(unsafe.Add(p, 972)) = __ccgo_fp(Xsqlite3_drop_modules) *(*uintptr)(unsafe.Add(p, 976)) = __ccgo_fp(Xsqlite3_hard_heap_limit64) *(*uintptr)(unsafe.Add(p, 980)) = __ccgo_fp(Xsqlite3_uri_key) *(*uintptr)(unsafe.Add(p, 984)) = __ccgo_fp(Xsqlite3_filename_database) *(*uintptr)(unsafe.Add(p, 988)) = __ccgo_fp(Xsqlite3_filename_journal) *(*uintptr)(unsafe.Add(p, 992)) = __ccgo_fp(Xsqlite3_filename_wal) *(*uintptr)(unsafe.Add(p, 996)) = __ccgo_fp(Xsqlite3_create_filename) *(*uintptr)(unsafe.Add(p, 1000)) = __ccgo_fp(Xsqlite3_free_filename) *(*uintptr)(unsafe.Add(p, 1004)) = __ccgo_fp(Xsqlite3_database_file_object) *(*uintptr)(unsafe.Add(p, 1008)) = __ccgo_fp(Xsqlite3_txn_state) *(*uintptr)(unsafe.Add(p, 1012)) = __ccgo_fp(Xsqlite3_changes64) *(*uintptr)(unsafe.Add(p, 1016)) = __ccgo_fp(Xsqlite3_total_changes64) *(*uintptr)(unsafe.Add(p, 1020)) = __ccgo_fp(Xsqlite3_autovacuum_pages) *(*uintptr)(unsafe.Add(p, 1024)) = __ccgo_fp(Xsqlite3_error_offset) *(*uintptr)(unsafe.Add(p, 1028)) = __ccgo_fp(Xsqlite3_vtab_rhs_value) *(*uintptr)(unsafe.Add(p, 1032)) = __ccgo_fp(Xsqlite3_vtab_distinct) *(*uintptr)(unsafe.Add(p, 1036)) = __ccgo_fp(Xsqlite3_vtab_in) *(*uintptr)(unsafe.Add(p, 1040)) = __ccgo_fp(Xsqlite3_vtab_in_first) *(*uintptr)(unsafe.Add(p, 1044)) = __ccgo_fp(Xsqlite3_vtab_in_next) *(*uintptr)(unsafe.Add(p, 1048)) = __ccgo_fp(Xsqlite3_deserialize) *(*uintptr)(unsafe.Add(p, 1052)) = __ccgo_fp(Xsqlite3_serialize) *(*uintptr)(unsafe.Add(p, 1056)) = __ccgo_fp(Xsqlite3_db_name) *(*uintptr)(unsafe.Add(p, 1060)) = __ccgo_fp(Xsqlite3_value_encoding) *(*uintptr)(unsafe.Add(p, 1064)) = __ccgo_fp(Xsqlite3_is_interrupted) *(*uintptr)(unsafe.Add(p, 1068)) = __ccgo_fp(Xsqlite3_stmt_explain) *(*uintptr)(unsafe.Add(p, 1072)) = __ccgo_fp(Xsqlite3_get_clientdata) *(*uintptr)(unsafe.Add(p, 1076)) = __ccgo_fp(Xsqlite3_set_clientdata) } /* True if x is the directory separator character */ // C documentation // // /* // ** Attempt to load an SQLite extension library contained in the file // ** zFile. The entry point is zProc. zProc may be 0 in which case a // ** default entry point name (sqlite3_extension_init) is used. Use // ** of the default name is recommended. // ** // ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong. // ** // ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with // ** error message text. The calling function should free this memory // ** by calling sqlite3DbFree(db, ). // */ func _sqlite3LoadExtension(tls *libc.TLS, db uintptr, zFile uintptr, zProc uintptr, pzErrMsg uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var aHandle, handle, pVfs, zAltEntry, zAltFile, zEntry, v1, v10, v7, v9 uintptr var c, iEntry, iFile, ii, ncFile, rc, v5, v6, v8 int32 var nMsg Tu64 var xInit Tsqlite3_loadext_entry var _ /* zErrmsg at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aHandle, c, handle, iEntry, iFile, ii, nMsg, ncFile, pVfs, rc, xInit, zAltEntry, zAltFile, zEntry, v1, v10, v5, v6, v7, v8, v9 pVfs = (*Tsqlite3)(unsafe.Pointer(db)).FpVfs *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) zAltEntry = uintptr(0) nMsg = uint64(libc.Xstrlen(tls, zFile)) if pzErrMsg != 0 { *(*uintptr)(unsafe.Pointer(pzErrMsg)) = uintptr(0) } /* Ticket #1863. To avoid a creating security problems for older ** applications that relink against newer versions of SQLite, the ** ability to run load_extension is turned off by default. One ** must call either sqlite3_enable_load_extension(db) or ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0) ** to turn on extension loading. */ if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_LoadExtension) == uint64(0) { if pzErrMsg != 0 { *(*uintptr)(unsafe.Pointer(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+12420, 0) } return int32(SQLITE_ERROR) } if zProc != 0 { v1 = zProc } else { v1 = __ccgo_ts + 16318 } zEntry = v1 /* tag-20210611-1. Some dlopen() implementations will segfault if given ** an oversize filename. Most filesystems have a pathname limit of 4K, ** so limit the extension filename length to about twice that. ** https://sqlite.org/forum/forumpost/08a0d6d9bf ** ** Later (2023-03-25): Save an extra 6 bytes for the filename suffix. ** See https://sqlite.org/forum/forumpost/24083b579d. */ if nMsg > uint64(FILENAME_MAX) { goto extension_not_found } /* Do not allow sqlite3_load_extension() to link to a copy of the ** running application, by passing in an empty filename. */ if nMsg == uint64(0) { goto extension_not_found } handle = _sqlite3OsDlOpen(tls, pVfs, zFile) ii = 0 for { if !(ii < int32(libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4)) && handle == uintptr(0)) { break } zAltFile = Xsqlite3_mprintf(tls, __ccgo_ts+12381, libc.VaList(bp+16, zFile, _azEndings[ii])) if zAltFile == uintptr(0) { return int32(SQLITE_NOMEM) } if nMsg+uint64(libc.Xstrlen(tls, _azEndings[ii]))+uint64(1) <= uint64(FILENAME_MAX) { handle = _sqlite3OsDlOpen(tls, pVfs, zAltFile) } Xsqlite3_free(tls, zAltFile) goto _2 _2: ; ii++ } if handle == uintptr(0) { goto extension_not_found } xInit = _sqlite3OsDlSym(tls, pVfs, handle, zEntry) /* If no entry point was specified and the default legacy ** entry point name "sqlite3_extension_init" was not found, then ** construct an entry point name "sqlite3_X_init" where the X is ** replaced by the lowercase value of every ASCII alphabetic ** character in the filename after the last "/" upto the first ".", ** and eliding the first three characters if they are "lib". ** Examples: ** ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init */ if xInit == uintptr(0) && zProc == uintptr(0) { ncFile = _sqlite3Strlen30(tls, zFile) zAltEntry = Xsqlite3_malloc64(tls, uint64(ncFile+int32(30))) if zAltEntry == uintptr(0) { _sqlite3OsDlClose(tls, pVfs, handle) return int32(SQLITE_NOMEM) } libc.Xmemcpy(tls, zAltEntry, __ccgo_ts+16341, uint32(8)) iFile = ncFile - int32(1) for { if !(iFile >= 0 && !(int32(*(*uint8)(unsafe.Pointer(zFile + uintptr(iFile)))) == libc.Int32FromUint8('/'))) { break } goto _3 _3: ; iFile-- } iFile++ if Xsqlite3_strnicmp(tls, zFile+uintptr(iFile), __ccgo_ts+16350, int32(3)) == 0 { iFile += int32(3) } iEntry = int32(8) for { v5 = int32(*(*uint8)(unsafe.Pointer(zFile + uintptr(iFile)))) c = v5 if !(v5 != 0 && c != int32('.')) { break } if int32(_sqlite3CtypeMap[uint8(c)])&int32(0x02) != 0 { v6 = iEntry iEntry++ *(*uint8)(unsafe.Pointer(zAltEntry + uintptr(v6))) = _sqlite3UpperToLower[uint32(c)] } goto _4 _4: ; iFile++ } libc.Xmemcpy(tls, zAltEntry+uintptr(iEntry), __ccgo_ts+16354, uint32(6)) zEntry = zAltEntry xInit = _sqlite3OsDlSym(tls, pVfs, handle, zEntry) } if xInit == uintptr(0) { if pzErrMsg != 0 { nMsg += uint64(libc.Xstrlen(tls, zEntry) + uint32(300)) v7 = Xsqlite3_malloc64(tls, nMsg) *(*uintptr)(unsafe.Pointer(bp)) = v7 *(*uintptr)(unsafe.Pointer(pzErrMsg)) = v7 if *(*uintptr)(unsafe.Pointer(bp)) != 0 { _ = libc.Int32FromInt32(0) /* zErrmsg would be NULL if not so */ Xsqlite3_snprintf(tls, int32(nMsg), *(*uintptr)(unsafe.Pointer(bp)), __ccgo_ts+16360, libc.VaList(bp+16, zEntry, zFile)) _sqlite3OsDlError(tls, pVfs, int32(nMsg-uint64(1)), *(*uintptr)(unsafe.Pointer(bp))) } } _sqlite3OsDlClose(tls, pVfs, handle) Xsqlite3_free(tls, zAltEntry) return int32(SQLITE_ERROR) } Xsqlite3_free(tls, zAltEntry) rc = (*(*func(*libc.TLS, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xInit})))(tls, db, bp, uintptr(unsafe.Pointer(&_sqlite3Apis))) if rc != 0 { if rc == libc.Int32FromInt32(SQLITE_OK)|libc.Int32FromInt32(1)<aExtension array. */ aHandle = _sqlite3DbMallocZero(tls, db, uint64(uint32(4)*uint32((*Tsqlite3)(unsafe.Pointer(db)).FnExtension+libc.Int32FromInt32(1)))) if aHandle == uintptr(0) { return int32(SQLITE_NOMEM) } if (*Tsqlite3)(unsafe.Pointer(db)).FnExtension > 0 { libc.Xmemcpy(tls, aHandle, (*Tsqlite3)(unsafe.Pointer(db)).FaExtension, uint32(4)*uint32((*Tsqlite3)(unsafe.Pointer(db)).FnExtension)) } _sqlite3DbFree(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).FaExtension) (*Tsqlite3)(unsafe.Pointer(db)).FaExtension = aHandle v9 = db + 208 v8 = *(*int32)(unsafe.Pointer(v9)) *(*int32)(unsafe.Pointer(v9))++ *(*uintptr)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaExtension + uintptr(v8)*4)) = handle return SQLITE_OK goto extension_not_found extension_not_found: ; if pzErrMsg != 0 { nMsg += uint64(300) v10 = Xsqlite3_malloc64(tls, nMsg) *(*uintptr)(unsafe.Pointer(bp)) = v10 *(*uintptr)(unsafe.Pointer(pzErrMsg)) = v10 if *(*uintptr)(unsafe.Pointer(bp)) != 0 { _ = libc.Int32FromInt32(0) /* zErrmsg would be NULL if not so */ Xsqlite3_snprintf(tls, int32(nMsg), *(*uintptr)(unsafe.Pointer(bp)), __ccgo_ts+16435, libc.VaList(bp+16, int32(FILENAME_MAX), zFile)) _sqlite3OsDlError(tls, pVfs, int32(nMsg-uint64(1)), *(*uintptr)(unsafe.Pointer(bp))) } } return int32(SQLITE_ERROR) } /* Shared library endings to try if zFile cannot be loaded as written */ var _azEndings = [1]uintptr{ 0: __ccgo_ts + 16315, } func Xsqlite3_load_extension(tls *libc.TLS, db uintptr, zFile uintptr, zProc uintptr, pzErrMsg uintptr) (r int32) { var rc int32 _ = rc Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) rc = _sqlite3LoadExtension(tls, db, zFile, zProc, pzErrMsg) rc = _sqlite3ApiExit(tls, db, rc) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** Call this routine when the database connection is closing in order // ** to clean up loaded extensions // */ func _sqlite3CloseExtensions(tls *libc.TLS, db uintptr) { var i int32 _ = i _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnExtension) { break } _sqlite3OsDlClose(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, *(*uintptr)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaExtension + uintptr(i)*4))) goto _1 _1: ; i++ } _sqlite3DbFree(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).FaExtension) } // C documentation // // /* // ** Enable or disable extension loading. Extension loading is disabled by // ** default so as not to open security holes in older applications. // */ func Xsqlite3_enable_load_extension(tls *libc.TLS, db uintptr, onoff int32) (r int32) { Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if onoff != 0 { *(*Tu64)(unsafe.Pointer(db + 32)) |= uint64(libc.Int32FromInt32(SQLITE_LoadExtension) | libc.Int32FromInt32(SQLITE_LoadExtFunc)) } else { *(*Tu64)(unsafe.Pointer(db + 32)) &= ^uint64(libc.Int32FromInt32(SQLITE_LoadExtension) | libc.Int32FromInt32(SQLITE_LoadExtFunc)) } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return SQLITE_OK } // C documentation // // /* // ** The following object holds the list of automatically loaded // ** extensions. // ** // ** This list is shared across threads. The SQLITE_MUTEX_STATIC_MAIN // ** mutex must be held while accessing this list. // */ type Tsqlite3AutoExtList = struct { FnExt Tu32 FaExt uintptr } type sqlite3AutoExtList = Tsqlite3AutoExtList type Tsqlite3AutoExtList1 = struct { FnExt Tu32 FaExt uintptr } type sqlite3AutoExtList1 = Tsqlite3AutoExtList1 var _sqlite3Autoext = Tsqlite3AutoExtList1{} /* The "wsdAutoext" macro will resolve to the autoextension ** state vector. If writable static data is unsupported on the target, ** we have to locate the state vector at run-time. In the more common ** case where writable static data is supported, wsdStat can refer directly ** to the "sqlite3Autoext" state vector declared above. */ // C documentation // // /* // ** Register a statically linked extension that is automatically // ** loaded by every new database connection. // */ func Xsqlite3_auto_extension(tls *libc.TLS, xInit uintptr) (r int32) { var aNew, mutex uintptr var i Tu32 var nByte Tu64 var rc int32 _, _, _, _, _ = aNew, i, mutex, nByte, rc rc = SQLITE_OK rc = Xsqlite3_initialize(tls) if rc != 0 { return rc } else { mutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN)) Xsqlite3_mutex_enter(tls, mutex) i = uint32(0) for { if !(i < _sqlite3Autoext.FnExt) { break } if *(*uintptr)(unsafe.Pointer(_sqlite3Autoext.FaExt + uintptr(i)*4)) == xInit { break } goto _1 _1: ; i++ } if i == _sqlite3Autoext.FnExt { nByte = uint64((_sqlite3Autoext.FnExt + uint32(1)) * uint32(4)) aNew = Xsqlite3_realloc64(tls, _sqlite3Autoext.FaExt, nByte) if aNew == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { _sqlite3Autoext.FaExt = aNew *(*uintptr)(unsafe.Pointer(_sqlite3Autoext.FaExt + uintptr(_sqlite3Autoext.FnExt)*4)) = xInit _sqlite3Autoext.FnExt++ } } Xsqlite3_mutex_leave(tls, mutex) _ = libc.Int32FromInt32(0) return rc } return r } // C documentation // // /* // ** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the // ** set of routines that is invoked for each new database connection, if it // ** is currently on the list. If xInit is not on the list, then this // ** routine is a no-op. // ** // ** Return 1 if xInit was found on the list and removed. Return 0 if xInit // ** was not on the list. // */ func Xsqlite3_cancel_auto_extension(tls *libc.TLS, xInit uintptr) (r int32) { var i, n int32 var mutex uintptr _, _, _ = i, mutex, n mutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN)) n = 0 Xsqlite3_mutex_enter(tls, mutex) i = int32(_sqlite3Autoext.FnExt) - int32(1) for { if !(i >= 0) { break } if *(*uintptr)(unsafe.Pointer(_sqlite3Autoext.FaExt + uintptr(i)*4)) == xInit { _sqlite3Autoext.FnExt-- *(*uintptr)(unsafe.Pointer(_sqlite3Autoext.FaExt + uintptr(i)*4)) = *(*uintptr)(unsafe.Pointer(_sqlite3Autoext.FaExt + uintptr(_sqlite3Autoext.FnExt)*4)) n++ break } goto _1 _1: ; i-- } Xsqlite3_mutex_leave(tls, mutex) return n } // C documentation // // /* // ** Reset the automatic extension loading mechanism. // */ func Xsqlite3_reset_auto_extension(tls *libc.TLS) { var mutex uintptr _ = mutex if Xsqlite3_initialize(tls) == SQLITE_OK { mutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN)) Xsqlite3_mutex_enter(tls, mutex) Xsqlite3_free(tls, _sqlite3Autoext.FaExt) _sqlite3Autoext.FaExt = uintptr(0) _sqlite3Autoext.FnExt = uint32(0) Xsqlite3_mutex_leave(tls, mutex) } } // C documentation // // /* // ** Load all automatic extensions. // ** // ** If anything goes wrong, set an error in the database connection. // */ func _sqlite3AutoLoadExtensions(tls *libc.TLS, db uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var go1, rc, v2 int32 var i Tu32 var mutex, pThunk uintptr var xInit Tsqlite3_loadext_entry var v3 bool var _ /* zErrmsg at bp+0 */ uintptr _, _, _, _, _, _, _, _ = go1, i, mutex, pThunk, rc, xInit, v2, v3 go1 = int32(1) if _sqlite3Autoext.FnExt == uint32(0) { /* Common case: early out without every having to acquire a mutex */ return } i = uint32(0) for { if !(go1 != 0) { break } mutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN)) pThunk = uintptr(unsafe.Pointer(&_sqlite3Apis)) Xsqlite3_mutex_enter(tls, mutex) if i >= _sqlite3Autoext.FnExt { xInit = uintptr(0) go1 = 0 } else { xInit = *(*uintptr)(unsafe.Pointer(_sqlite3Autoext.FaExt + uintptr(i)*4)) } Xsqlite3_mutex_leave(tls, mutex) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) if v3 = xInit != 0; v3 { v2 = (*(*func(*libc.TLS, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xInit})))(tls, db, bp, pThunk) rc = v2 } if v3 && v2 != 0 { _sqlite3ErrorWithMsg(tls, db, rc, __ccgo_ts+16472, libc.VaList(bp+16, *(*uintptr)(unsafe.Pointer(bp)))) go1 = 0 } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp))) goto _1 _1: ; i++ } } /************** End of loadext.c *********************************************/ /************** Begin file pragma.c ******************************************/ /* ** 2003 April 6 ** ** 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 contains code used to implement the PRAGMA command. */ /* #include "sqliteInt.h" */ /*************************************************************************** ** The "pragma.h" include file is an automatically generated file that ** that includes the PragType_XXXX macro definitions and the aPragmaName[] ** object. This ensures that the aPragmaName[] table is arranged in ** lexicographical order to facility a binary search of the pragma name. ** Do not edit pragma.h directly. Edit and rerun the script in at ** ../tool/mkpragmatab.tcl. */ /************** Include pragma.h in the middle of pragma.c *******************/ /************** Begin file pragma.h ******************************************/ /* DO NOT EDIT! ** This file is automatically generated by the script at ** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit ** that script and rerun it. */ /* The various pragma types */ /* Property flags associated with various pragma. */ // C documentation // // /* Names of columns for pragmas that return multi-column result // ** or that return single-column results where the name of the // ** result column is different from the name of the pragma // */ var _pragCName = [57]uintptr{ 0: __ccgo_ts + 5481, 1: __ccgo_ts + 16511, 2: __ccgo_ts + 9058, 3: __ccgo_ts + 16515, 4: __ccgo_ts + 16520, 5: __ccgo_ts + 16523, 6: __ccgo_ts + 16533, 7: __ccgo_ts + 16543, 8: __ccgo_ts + 16549, 9: __ccgo_ts + 16553, 10: __ccgo_ts + 16558, 11: __ccgo_ts + 16563, 12: __ccgo_ts + 16571, 13: __ccgo_ts + 16582, 14: __ccgo_ts + 16585, 15: __ccgo_ts + 16592, 16: __ccgo_ts + 16553, 17: __ccgo_ts + 16558, 18: __ccgo_ts + 16599, 19: __ccgo_ts + 16604, 20: __ccgo_ts + 16607, 21: __ccgo_ts + 16614, 22: __ccgo_ts + 16549, 23: __ccgo_ts + 16553, 24: __ccgo_ts + 16620, 25: __ccgo_ts + 16625, 26: __ccgo_ts + 16630, 27: __ccgo_ts + 16553, 28: __ccgo_ts + 16634, 29: __ccgo_ts + 16558, 30: __ccgo_ts + 16642, 31: __ccgo_ts + 16646, 32: __ccgo_ts + 16651, 33: __ccgo_ts + 11797, 34: __ccgo_ts + 11793, 35: __ccgo_ts + 16657, 36: __ccgo_ts + 16662, 37: __ccgo_ts + 16667, 38: __ccgo_ts + 16511, 39: __ccgo_ts + 16553, 40: __ccgo_ts + 16672, 41: __ccgo_ts + 16679, 42: __ccgo_ts + 16686, 43: __ccgo_ts + 9058, 44: __ccgo_ts + 16694, 45: __ccgo_ts + 5484, 46: __ccgo_ts + 16700, 47: __ccgo_ts + 16511, 48: __ccgo_ts + 16553, 49: __ccgo_ts + 16705, 50: __ccgo_ts + 16710, 51: __ccgo_ts + 15907, 52: __ccgo_ts + 16715, 53: __ccgo_ts + 16728, 54: __ccgo_ts + 16737, 55: __ccgo_ts + 16744, 56: __ccgo_ts + 16755, } // C documentation // // /* Definitions of all built-in pragmas */ type TPragmaName = struct { F__ccgo_align [0]uint32 FzName uintptr FePragTyp Tu8 FmPragFlg Tu8 FiPragCName Tu8 FnPragCName Tu8 FiArg Tu64 } type PragmaName = TPragmaName var _aPragmaName = [66]TPragmaName{ 0: { FzName: __ccgo_ts + 16763, FePragTyp: uint8(PragTyp_ANALYSIS_LIMIT), FmPragFlg: uint8(PragFlg_Result0), }, 1: { FzName: __ccgo_ts + 16778, FePragTyp: uint8(PragTyp_HEADER_VALUE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NoColumns1) | libc.Int32FromInt32(PragFlg_Result0)), FiArg: uint64(BTREE_APPLICATION_ID), }, 2: { FzName: __ccgo_ts + 16793, FePragTyp: uint8(PragTyp_AUTO_VACUUM), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq) | libc.Int32FromInt32(PragFlg_NoColumns1)), }, 3: { FzName: __ccgo_ts + 16805, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_AutoIndex), }, 4: { FzName: __ccgo_ts + 16821, FePragTyp: uint8(PragTyp_BUSY_TIMEOUT), FmPragFlg: uint8(PragFlg_Result0), FiPragCName: uint8(56), FnPragCName: uint8(1), }, 5: { FzName: __ccgo_ts + 16744, FePragTyp: uint8(PragTyp_CACHE_SIZE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq) | libc.Int32FromInt32(PragFlg_NoColumns1)), }, 6: { FzName: __ccgo_ts + 16834, FePragTyp: uint8(PragTyp_CACHE_SPILL), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq) | libc.Int32FromInt32(PragFlg_NoColumns1)), }, 7: { FzName: __ccgo_ts + 16846, FePragTyp: uint8(PragTyp_CASE_SENSITIVE_LIKE), FmPragFlg: uint8(PragFlg_NoColumns), }, 8: { FzName: __ccgo_ts + 16866, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_CellSizeCk), }, 9: { FzName: __ccgo_ts + 16882, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_CkptFullFSync), }, 10: { FzName: __ccgo_ts + 16903, FePragTyp: uint8(PragTyp_COLLATION_LIST), FmPragFlg: uint8(PragFlg_Result0), FiPragCName: uint8(38), FnPragCName: uint8(2), }, 11: { FzName: __ccgo_ts + 16918, FePragTyp: uint8(PragTyp_COMPILE_OPTIONS), FmPragFlg: uint8(PragFlg_Result0), }, 12: { FzName: __ccgo_ts + 16934, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(libc.Int32FromInt32(0x00001)) << libc.Int32FromInt32(32), }, 13: { FzName: __ccgo_ts + 16948, FePragTyp: uint8(PragTyp_HEADER_VALUE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_ReadOnly) | libc.Int32FromInt32(PragFlg_Result0)), FiArg: uint64(BTREE_DATA_VERSION), }, 14: { FzName: __ccgo_ts + 16961, FePragTyp: uint8(PragTyp_DATABASE_LIST), FmPragFlg: uint8(PragFlg_Result0), FiPragCName: uint8(47), FnPragCName: uint8(3), }, 15: { FzName: __ccgo_ts + 16975, FePragTyp: uint8(PragTyp_DEFAULT_CACHE_SIZE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiPragCName: uint8(55), FnPragCName: uint8(1), }, 16: { FzName: __ccgo_ts + 16994, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_DeferFKs), }, 17: { FzName: __ccgo_ts + 17013, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_NullCallback), }, 18: { FzName: __ccgo_ts + 17036, FePragTyp: uint8(PragTyp_ENCODING), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), }, 19: { FzName: __ccgo_ts + 17045, FePragTyp: uint8(PragTyp_FOREIGN_KEY_CHECK), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_Result1) | libc.Int32FromInt32(PragFlg_SchemaOpt)), FiPragCName: uint8(43), FnPragCName: uint8(4), }, 20: { FzName: __ccgo_ts + 17063, FePragTyp: uint8(PragTyp_FOREIGN_KEY_LIST), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result1) | libc.Int32FromInt32(PragFlg_SchemaOpt)), FnPragCName: uint8(8), }, 21: { FzName: __ccgo_ts + 17080, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_ForeignKeys), }, 22: { FzName: __ccgo_ts + 17093, FePragTyp: uint8(PragTyp_HEADER_VALUE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_ReadOnly) | libc.Int32FromInt32(PragFlg_Result0)), }, 23: { FzName: __ccgo_ts + 17108, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_FullColNames), }, 24: { FzName: __ccgo_ts + 17126, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_FullFSync), }, 25: { FzName: __ccgo_ts + 17136, FePragTyp: uint8(PragTyp_FUNCTION_LIST), FmPragFlg: uint8(PragFlg_Result0), FiPragCName: uint8(27), FnPragCName: uint8(6), }, 26: { FzName: __ccgo_ts + 17150, FePragTyp: uint8(PragTyp_HARD_HEAP_LIMIT), FmPragFlg: uint8(PragFlg_Result0), }, 27: { FzName: __ccgo_ts + 17166, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_IgnoreChecks), }, 28: { FzName: __ccgo_ts + 17191, FePragTyp: uint8(PragTyp_INCREMENTAL_VACUUM), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_NoColumns)), }, 29: { FzName: __ccgo_ts + 17210, FePragTyp: uint8(PragTyp_INDEX_INFO), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result1) | libc.Int32FromInt32(PragFlg_SchemaOpt)), FiPragCName: uint8(21), FnPragCName: uint8(3), }, 30: { FzName: __ccgo_ts + 17221, FePragTyp: uint8(PragTyp_INDEX_LIST), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result1) | libc.Int32FromInt32(PragFlg_SchemaOpt)), FiPragCName: uint8(38), FnPragCName: uint8(5), }, 31: { FzName: __ccgo_ts + 17232, FePragTyp: uint8(PragTyp_INDEX_INFO), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result1) | libc.Int32FromInt32(PragFlg_SchemaOpt)), FiPragCName: uint8(21), FnPragCName: uint8(6), FiArg: uint64(1), }, 32: { FzName: __ccgo_ts + 17244, FePragTyp: uint8(PragTyp_INTEGRITY_CHECK), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_Result1) | libc.Int32FromInt32(PragFlg_SchemaOpt)), }, 33: { FzName: __ccgo_ts + 17260, FePragTyp: uint8(PragTyp_JOURNAL_MODE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq)), }, 34: { FzName: __ccgo_ts + 17273, FePragTyp: uint8(PragTyp_JOURNAL_SIZE_LIMIT), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq)), }, 35: { FzName: __ccgo_ts + 17292, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_LegacyAlter), }, 36: { FzName: __ccgo_ts + 17311, FePragTyp: uint8(PragTyp_LOCKING_MODE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq)), }, 37: { FzName: __ccgo_ts + 17324, FePragTyp: uint8(PragTyp_PAGE_COUNT), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq)), }, 38: { FzName: __ccgo_ts + 17339, FePragTyp: uint8(PragTyp_MMAP_SIZE), }, 39: { FzName: __ccgo_ts + 17349, FePragTyp: uint8(PragTyp_MODULE_LIST), FmPragFlg: uint8(PragFlg_Result0), FiPragCName: uint8(9), FnPragCName: uint8(1), }, 40: { FzName: __ccgo_ts + 17361, FePragTyp: uint8(PragTyp_OPTIMIZE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result1) | libc.Int32FromInt32(PragFlg_NeedSchema)), }, 41: { FzName: __ccgo_ts + 17370, FePragTyp: uint8(PragTyp_PAGE_COUNT), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq)), }, 42: { FzName: __ccgo_ts + 17381, FePragTyp: uint8(PragTyp_PAGE_SIZE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq) | libc.Int32FromInt32(PragFlg_NoColumns1)), }, 43: { FzName: __ccgo_ts + 17391, FePragTyp: uint8(PragTyp_PRAGMA_LIST), FmPragFlg: uint8(PragFlg_Result0), FiPragCName: uint8(9), FnPragCName: uint8(1), }, 44: { FzName: __ccgo_ts + 17403, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_QueryOnly), }, 45: { FzName: __ccgo_ts + 17414, FePragTyp: uint8(PragTyp_INTEGRITY_CHECK), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_Result1) | libc.Int32FromInt32(PragFlg_SchemaOpt)), }, 46: { FzName: __ccgo_ts + 17426, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(libc.Int32FromInt32(0x00004)) << libc.Int32FromInt32(32), }, 47: { FzName: __ccgo_ts + 17443, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_RecTriggers), }, 48: { FzName: __ccgo_ts + 17462, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_ReverseOrder), }, 49: { FzName: __ccgo_ts + 17488, FePragTyp: uint8(PragTyp_HEADER_VALUE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NoColumns1) | libc.Int32FromInt32(PragFlg_Result0)), FiArg: uint64(BTREE_SCHEMA_VERSION), }, 50: { FzName: __ccgo_ts + 17503, FePragTyp: uint8(PragTyp_SECURE_DELETE), FmPragFlg: uint8(PragFlg_Result0), }, 51: { FzName: __ccgo_ts + 17517, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_ShortColNames), }, 52: { FzName: __ccgo_ts + 17536, FePragTyp: uint8(PragTyp_SHRINK_MEMORY), FmPragFlg: uint8(PragFlg_NoColumns), }, 53: { FzName: __ccgo_ts + 17550, FePragTyp: uint8(PragTyp_SOFT_HEAP_LIMIT), FmPragFlg: uint8(PragFlg_Result0), }, 54: { FzName: __ccgo_ts + 17566, FePragTyp: uint8(PragTyp_SYNCHRONOUS), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq) | libc.Int32FromInt32(PragFlg_NoColumns1)), }, 55: { FzName: __ccgo_ts + 17578, FePragTyp: uint8(PragTyp_TABLE_INFO), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result1) | libc.Int32FromInt32(PragFlg_SchemaOpt)), FiPragCName: uint8(8), FnPragCName: uint8(6), }, 56: { FzName: __ccgo_ts + 17589, FePragTyp: uint8(PragTyp_TABLE_LIST), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result1)), FiPragCName: uint8(15), FnPragCName: uint8(6), }, 57: { FzName: __ccgo_ts + 17600, FePragTyp: uint8(PragTyp_TABLE_INFO), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result1) | libc.Int32FromInt32(PragFlg_SchemaOpt)), FiPragCName: uint8(8), FnPragCName: uint8(7), FiArg: uint64(1), }, 58: { FzName: __ccgo_ts + 17612, FePragTyp: uint8(PragTyp_TEMP_STORE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), }, 59: { FzName: __ccgo_ts + 17623, FePragTyp: uint8(PragTyp_TEMP_STORE_DIRECTORY), FmPragFlg: uint8(PragFlg_NoColumns1), }, 60: { FzName: __ccgo_ts + 17644, FePragTyp: uint8(PragTyp_THREADS), FmPragFlg: uint8(PragFlg_Result0), }, 61: { FzName: __ccgo_ts + 17652, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_TrustedSchema), }, 62: { FzName: __ccgo_ts + 17667, FePragTyp: uint8(PragTyp_HEADER_VALUE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NoColumns1) | libc.Int32FromInt32(PragFlg_Result0)), FiArg: uint64(BTREE_USER_VERSION), }, 63: { FzName: __ccgo_ts + 17680, FePragTyp: uint8(PragTyp_WAL_AUTOCHECKPOINT), }, 64: { FzName: __ccgo_ts + 17699, FePragTyp: uint8(PragTyp_WAL_CHECKPOINT), FmPragFlg: uint8(PragFlg_NeedSchema), FiPragCName: uint8(50), FnPragCName: uint8(3), }, 65: { FzName: __ccgo_ts + 17714, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(libc.Int32FromInt32(SQLITE_WriteSchema) | libc.Int32FromInt32(SQLITE_NoSchemaError)), }, } /* Number of pragmas: 68 on by default, 78 total. */ /************** End of pragma.h **********************************************/ /************** Continuing where we left off in pragma.c *********************/ // C documentation // // /* // ** Interpret the given string as a safety level. Return 0 for OFF, // ** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or // ** unrecognized string argument. The FULL and EXTRA option is disallowed // ** if the omitFull parameter it 1. // ** // ** Note that the values returned are one less that the values that // ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done // ** to support legacy SQL code. The safety level used to be boolean // ** and older scripts may have used numbers 0 for OFF and 1 for ON. // */ func _getSafetyLevel(tls *libc.TLS, z uintptr, omitFull int32, dflt Tu8) (r Tu8) { var i, n int32 _, _ = i, n if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z))])&int32(0x04) != 0 { return uint8(_sqlite3Atoi(tls, z)) } n = _sqlite3Strlen30(tls, z) i = 0 for { if !(i < int32(libc.Uint32FromInt64(8)/libc.Uint32FromInt64(1))) { break } if int32(_iLength[i]) == n && Xsqlite3_strnicmp(tls, uintptr(unsafe.Pointer(&_zText))+uintptr(_iOffset[i]), z, n) == 0 && (!(omitFull != 0) || int32(_iValue[i]) <= int32(1)) { return _iValue[i] } goto _1 _1: ; i++ } return dflt } /* 123456789 123456789 123 */ var _zText = [25]uint8{'o', 'n', 'o', 'f', 'f', 'a', 'l', 's', 'e', 'y', 'e', 's', 't', 'r', 'u', 'e', 'x', 't', 'r', 'a', 'f', 'u', 'l', 'l'} var _iOffset = [8]Tu8{ 1: uint8(1), 2: uint8(2), 3: uint8(4), 4: uint8(9), 5: uint8(12), 6: uint8(15), 7: uint8(20), } var _iLength = [8]Tu8{ 0: uint8(2), 1: uint8(2), 2: uint8(3), 3: uint8(5), 4: uint8(3), 5: uint8(4), 6: uint8(5), 7: uint8(4), } var _iValue = [8]Tu8{ 0: uint8(1), 4: uint8(1), 5: uint8(1), 6: uint8(3), 7: uint8(2), } // C documentation // // /* // ** Interpret the given string as a boolean value. // */ func _sqlite3GetBoolean(tls *libc.TLS, z uintptr, dflt Tu8) (r Tu8) { return libc.BoolUint8(int32(_getSafetyLevel(tls, z, int32(1), dflt)) != 0) } /* The sqlite3GetBoolean() function is used by other modules but the ** remainder of this file is specific to PRAGMA processing. So omit ** the rest of the file if PRAGMAs are omitted from the build. */ // C documentation // // /* // ** Interpret the given string as a locking mode value. // */ func _getLockingMode(tls *libc.TLS, z uintptr) (r int32) { if z != 0 { if 0 == _sqlite3StrICmp(tls, z, __ccgo_ts+17730) { return int32(PAGER_LOCKINGMODE_EXCLUSIVE) } if 0 == _sqlite3StrICmp(tls, z, __ccgo_ts+17740) { return PAGER_LOCKINGMODE_NORMAL } } return -int32(1) } // C documentation // // /* // ** Interpret the given string as an auto-vacuum mode value. // ** // ** The following strings, "none", "full" and "incremental" are // ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively. // */ func _getAutoVacuum(tls *libc.TLS, z uintptr) (r int32) { var i, v1 int32 _, _ = i, v1 if 0 == _sqlite3StrICmp(tls, z, __ccgo_ts+8218) { return BTREE_AUTOVACUUM_NONE } if 0 == _sqlite3StrICmp(tls, z, __ccgo_ts+17747) { return int32(BTREE_AUTOVACUUM_FULL) } if 0 == _sqlite3StrICmp(tls, z, __ccgo_ts+17752) { return int32(BTREE_AUTOVACUUM_INCR) } i = _sqlite3Atoi(tls, z) if i >= 0 && i <= int32(2) { v1 = i } else { v1 = 0 } return int32(uint8(v1)) } // C documentation // // /* // ** Interpret the given string as a temp db location. Return 1 for file // ** backed temporary databases, 2 for the Red-Black tree in memory database // ** and 0 to use the compile-time default. // */ func _getTempStore(tls *libc.TLS, z uintptr) (r int32) { if int32(*(*uint8)(unsafe.Pointer(z))) >= int32('0') && int32(*(*uint8)(unsafe.Pointer(z))) <= int32('2') { return int32(*(*uint8)(unsafe.Pointer(z))) - int32('0') } else { if _sqlite3StrICmp(tls, z, __ccgo_ts+16705) == 0 { return int32(1) } else { if _sqlite3StrICmp(tls, z, __ccgo_ts+17764) == 0 { return int32(2) } else { return 0 } } } return r } // C documentation // // /* // ** Invalidate temp storage, either when the temp storage is changed // ** from default, or when 'file' and the temp_store_directory has changed // */ func _invalidateTempStorage(tls *libc.TLS, pParse uintptr) (r int32) { var db uintptr _ = db db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt != uintptr(0) { if !((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0) || _sqlite3BtreeTxnState(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt) != SQLITE_TXN_NONE { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+17771, 0) return int32(SQLITE_ERROR) } _sqlite3BtreeClose(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt) (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt = uintptr(0) _sqlite3ResetAllSchemasOfConnection(tls, db) } return SQLITE_OK } // C documentation // // /* // ** If the TEMP database is open, close it and mark the database schema // ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE // ** or DEFAULT_TEMP_STORE pragmas. // */ func _changeTempStorage(tls *libc.TLS, pParse uintptr, zStorageType uintptr) (r int32) { var db uintptr var ts int32 _, _ = db, ts ts = _getTempStore(tls, zStorageType) db = (*TParse)(unsafe.Pointer(pParse)).Fdb if int32((*Tsqlite3)(unsafe.Pointer(db)).Ftemp_store) == ts { return SQLITE_OK } if _invalidateTempStorage(tls, pParse) != SQLITE_OK { return int32(SQLITE_ERROR) } (*Tsqlite3)(unsafe.Pointer(db)).Ftemp_store = uint8(ts) return SQLITE_OK } // C documentation // // /* // ** Set result column names for a pragma. // */ func _setPragmaResultColumnNames(tls *libc.TLS, v uintptr, pPragma uintptr) { var i, j, v1 int32 var n Tu8 _, _, _, _ = i, j, n, v1 n = (*TPragmaName)(unsafe.Pointer(pPragma)).FnPragCName if int32(n) == 0 { v1 = int32(1) } else { v1 = int32(n) } _sqlite3VdbeSetNumCols(tls, v, v1) if int32(n) == 0 { _sqlite3VdbeSetColName(tls, v, 0, COLNAME_NAME, (*TPragmaName)(unsafe.Pointer(pPragma)).FzName, libc.UintptrFromInt32(0)) } else { i = 0 j = int32((*TPragmaName)(unsafe.Pointer(pPragma)).FiPragCName) for { if !(i < int32(n)) { break } _sqlite3VdbeSetColName(tls, v, i, COLNAME_NAME, _pragCName[j], libc.UintptrFromInt32(0)) goto _2 _2: ; i++ j++ } } } // C documentation // // /* // ** Generate code to return a single integer value. // */ func _returnSingleInt(tls *libc.TLS, v uintptr, _value Ti64) { bp := tls.Alloc(16) defer tls.Free(16) *(*Ti64)(unsafe.Pointer(bp)) = _value _sqlite3VdbeAddOp4Dup8(tls, v, int32(OP_Int64), 0, int32(1), 0, bp, -int32(13)) _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), int32(1), int32(1)) } // C documentation // // /* // ** Generate code to return a single text value. // */ func _returnSingleText(tls *libc.TLS, v uintptr, zValue uintptr) { if zValue != 0 { _sqlite3VdbeLoadString(tls, v, int32(1), zValue) _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), int32(1), int32(1)) } } // C documentation // // /* // ** Set the safety_level and pager flags for pager iDb. Or if iDb<0 // ** set these values for all pagers. // */ func _setAllPagerFlags(tls *libc.TLS, db uintptr) { var n, v1 int32 var pDb uintptr _, _, _ = n, pDb, v1 if (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0 { pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb n = (*Tsqlite3)(unsafe.Pointer(db)).FnDb _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) for { v1 = n n-- if !(v1 > 0) { break } if (*TDb)(unsafe.Pointer(pDb)).FpBt != 0 { _sqlite3BtreeSetPagerFlags(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, uint32(uint64((*TDb)(unsafe.Pointer(pDb)).Fsafety_level)|(*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(PAGER_FLAGS_MASK))) } pDb += 16 } } } // C documentation // // /* // ** Return a human-readable name for a constraint resolution action. // */ func _actionName(tls *libc.TLS, action Tu8) (r uintptr) { var zName uintptr _ = zName switch int32(action) { case int32(OE_SetNull): zName = __ccgo_ts + 17833 case int32(OE_SetDflt): zName = __ccgo_ts + 17842 case int32(OE_Cascade): zName = __ccgo_ts + 17854 case int32(OE_Restrict): zName = __ccgo_ts + 17862 default: zName = __ccgo_ts + 17871 _ = libc.Int32FromInt32(0) break } return zName } // C documentation // // /* // ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants // ** defined in pager.h. This function returns the associated lowercase // ** journal-mode name. // */ func _sqlite3JournalModename(tls *libc.TLS, eMode int32) (r uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if eMode == int32(libc.Uint32FromInt64(24)/libc.Uint32FromInt64(4)) { return uintptr(0) } return _azModeName[eMode] } var _azModeName = [6]uintptr{ 0: __ccgo_ts + 17881, 1: __ccgo_ts + 17888, 2: __ccgo_ts + 17896, 3: __ccgo_ts + 17900, 4: __ccgo_ts + 17764, 5: __ccgo_ts + 17909, } // C documentation // // /* // ** Locate a pragma in the aPragmaName[] array. // */ func _pragmaLocate(tls *libc.TLS, zName uintptr) (r uintptr) { var lwr, mid, rc, upr int32 var v1 uintptr _, _, _, _, _ = lwr, mid, rc, upr, v1 mid = 0 lwr = 0 upr = int32(libc.Uint32FromInt64(1056)/libc.Uint32FromInt64(16)) - libc.Int32FromInt32(1) for lwr <= upr { mid = (lwr + upr) / int32(2) rc = Xsqlite3_stricmp(tls, zName, _aPragmaName[mid].FzName) if rc == 0 { break } if rc < 0 { upr = mid - int32(1) } else { lwr = mid + int32(1) } } if lwr > upr { v1 = uintptr(0) } else { v1 = uintptr(unsafe.Pointer(&_aPragmaName)) + uintptr(mid)*16 } return v1 } // C documentation // // /* // ** Create zero or more entries in the output for the SQL functions // ** defined by FuncDef p. // */ func _pragmaFunclistLine(tls *libc.TLS, v uintptr, p uintptr, isBuiltin int32, showInternFuncs int32) { bp := tls.Alloc(64) defer tls.Free(64) var mask Tu32 var zType uintptr _, _ = mask, zType mask = uint32(libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_DIRECTONLY) | libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(SQLITE_INNOCUOUS) | libc.Int32FromInt32(SQLITE_FUNC_INTERNAL)) if showInternFuncs != 0 { mask = uint32(0xffffffff) } for { if !(p != 0) { break } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TFuncDef)(unsafe.Pointer(p)).FxSFunc == uintptr(0) { goto _1 } if (*TFuncDef)(unsafe.Pointer(p)).FfuncFlags&uint32(SQLITE_FUNC_INTERNAL) != uint32(0) && showInternFuncs == 0 { goto _1 } if (*TFuncDef)(unsafe.Pointer(p)).FxValue != uintptr(0) { zType = __ccgo_ts + 17934 } else { if (*TFuncDef)(unsafe.Pointer(p)).FxFinalize != uintptr(0) { zType = __ccgo_ts + 17936 } else { zType = __ccgo_ts + 7617 } } _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+17938, libc.VaList(bp+8, (*TFuncDef)(unsafe.Pointer(p)).FzName, isBuiltin, zType, _azEnc[(*TFuncDef)(unsafe.Pointer(p)).FfuncFlags&uint32(SQLITE_FUNC_ENCMASK)], int32((*TFuncDef)(unsafe.Pointer(p)).FnArg), (*TFuncDef)(unsafe.Pointer(p)).FfuncFlags&mask^uint32(SQLITE_INNOCUOUS))) goto _1 _1: ; p = (*TFuncDef)(unsafe.Pointer(p)).FpNext } } var _azEnc = [4]uintptr{ 1: __ccgo_ts + 17913, 2: __ccgo_ts + 17918, 3: __ccgo_ts + 17926, } // C documentation // // /* // ** Helper subroutine for PRAGMA integrity_check: // ** // ** Generate code to output a single-column result row with a value of the // ** string held in register 3. Decrement the result count in register 1 // ** and halt if the maximum number of result rows have been issued. // */ func _integrityCheckResultRow(tls *libc.TLS, v uintptr) (r int32) { var addr int32 _ = addr _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), int32(3), int32(1)) addr = _sqlite3VdbeAddOp3(tls, v, int32(OP_IfPos), int32(1), _sqlite3VdbeCurrentAddr(tls, v)+int32(2), int32(1)) _sqlite3VdbeAddOp0(tls, v, int32(OP_Halt)) return addr } // C documentation // // /* // ** Process a pragma statement. // ** // ** Pragmas are of this form: // ** // ** PRAGMA [schema.]id [= value] // ** // ** The identifier might also be a string. The value is a string, and // ** identifier, or a number. If minusFlag is true, then the value is // ** a number that was preceded by a minus sign. // ** // ** If the left side is "database.id" then pId1 is the database name // ** and pId2 is the id. If the left side is just "id" then pId1 is the // ** id and pId2 is any empty string. // */ func _sqlite3Pragma(tls *libc.TLS, pParse uintptr, pId1 uintptr, pId2 uintptr, pValue uintptr, minusFlag int32) { bp := tls.Alloc(208) defer tls.Free(208) var a1, a11, addr, addr1, addrCkFault, addrCkOk, addrOk, addrTop, b, bStrict, ckUniq, cnt, doTypeCheck, eAuto, eMode, eMode1, eMode2, i, i1, i10, i2, i3, i4, i5, i6, i7, i8, i9, iAddr, iBt, iCol, iCol1, iCookie, iDb, iDbLast, iIdxDb, iLevel, iReg, iTabCur, iTabDb, iTabDb1, ii, ii1, ii2, ii3, ii4, initNCol, isHidden, isQuick, j2, j3, j4, jmp, jmp2, jmp21, jmp3, jmp4, jmp5, jmp6, jmp61, jmp7, k, k3, kk, label6, labelError, labelOk, loopTop, mx, mxCol, mxIdx, n, nHidden, nIdx, p11, p3, p4, r1, r11, r2, rc, regResult, regRow, showInternFunc, size, size1, size2, uniqOk, x1, v10, v15, v18, v2, v26, v33, v38, v4, v43, v45, v47, v60, v62, v63, v64, v65, v68 int32 var aOp, aOp1, aOp2, aOp3, aOp4, aRoot, db, j, j1, k1, k2, k4, p, p1, pBt, pBt1, pBt2, pCheck, pCol, pCol1, pColExpr, pColl, pDb, pEnc, pFK, pFK1, pHash, pIdx, pIdx1, pIdx3, pIdx4, pIdx5, pIdx6, pMod, pObjTab, pPager, pPager1, pParent, pPk, pPk1, pPragma, pPrior, pSchema, pTab, pTab1, pTab10, pTab11, pTab2, pTab3, pTab4, pTab5, pTab6, pTab7, pTab8, pTab9, pTbls, pVTab, v, x2, zDb, zErr, zErr1, zErr2, zLeft, zMod, zMode, zOpt, zRet, zRight, zSql, zSubSql, zType, v1, v14, v16, v22, v34, v39, v5, v61, v66, v8 uintptr var azOrigin [3]uintptr var cnum Ti16 var enc Tu8 var iPrior Tsqlite3_int64 var mask Tu64 var opMask Tu32 var szThreshold TLogEst var _ /* N at bp+104 */ Tsqlite3_int64 var _ /* N at bp+112 */ Tsqlite3_int64 var _ /* N at bp+120 */ Tsqlite3_int64 var _ /* N at bp+96 */ Tsqlite3_int64 var _ /* aFcntl at bp+4 */ [4]uintptr var _ /* aiCols at bp+68 */ uintptr var _ /* iDataCur at bp+76 */ int32 var _ /* iIdxCur at bp+80 */ int32 var _ /* iLimit at bp+32 */ Ti64 var _ /* iLimit at bp+40 */ int32 var _ /* jmp3 at bp+88 */ int32 var _ /* mxErr at bp+72 */ int32 var _ /* pDfltValue at bp+84 */ uintptr var _ /* pDummy at bp+60 */ uintptr var _ /* pId at bp+0 */ uintptr var _ /* pIdx at bp+64 */ uintptr var _ /* res at bp+56 */ int32 var _ /* size at bp+44 */ int32 var _ /* sz at bp+48 */ Tsqlite3_int64 var _ /* x at bp+24 */ Ti64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = a1, a11, aOp, aOp1, aOp2, aOp3, aOp4, aRoot, addr, addr1, addrCkFault, addrCkOk, addrOk, addrTop, azOrigin, b, bStrict, ckUniq, cnt, cnum, db, doTypeCheck, eAuto, eMode, eMode1, eMode2, enc, i, i1, i10, i2, i3, i4, i5, i6, i7, i8, i9, iAddr, iBt, iCol, iCol1, iCookie, iDb, iDbLast, iIdxDb, iLevel, iPrior, iReg, iTabCur, iTabDb, iTabDb1, ii, ii1, ii2, ii3, ii4, initNCol, isHidden, isQuick, j, j1, j2, j3, j4, jmp, jmp2, jmp21, jmp3, jmp4, jmp5, jmp6, jmp61, jmp7, k, k1, k2, k3, k4, kk, label6, labelError, labelOk, loopTop, mask, mx, mxCol, mxIdx, n, nHidden, nIdx, opMask, p, p1, p11, p3, p4, pBt, pBt1, pBt2, pCheck, pCol, pCol1, pColExpr, pColl, pDb, pEnc, pFK, pFK1, pHash, pIdx, pIdx1, pIdx3, pIdx4, pIdx5, pIdx6, pMod, pObjTab, pPager, pPager1, pParent, pPk, pPk1, pPragma, pPrior, pSchema, pTab, pTab1, pTab10, pTab11, pTab2, pTab3, pTab4, pTab5, pTab6, pTab7, pTab8, pTab9, pTbls, pVTab, r1, r11, r2, rc, regResult, regRow, showInternFunc, size, size1, size2, szThreshold, uniqOk, v, x1, x2, zDb, zErr, zErr1, zErr2, zLeft, zMod, zMode, zOpt, zRet, zRight, zSql, zSubSql, zType, v1, v10, v14, v15, v16, v18, v2, v22, v26, v33, v34, v38, v39, v4, v43, v45, v47, v5, v60, v61, v62, v63, v64, v65, v66, v68, v8 zLeft = uintptr(0) /* Nul-terminated UTF-8 string */ zRight = uintptr(0) /* Nul-terminated UTF-8 string , or NULL */ zDb = uintptr(0) /* return value form SQLITE_FCNTL_PRAGMA */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* The specific database being pragmaed */ v = _sqlite3GetVdbe(tls, pParse) /* The pragma */ if v == uintptr(0) { return } _sqlite3VdbeRunOnlyOnce(tls, v) (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(2) /* Interpret the [schema.] part of the pragma statement. iDb is the ** index of the database this pragma is being applied to in db.aDb[]. */ iDb = _sqlite3TwoPartName(tls, pParse, pId1, pId2, bp) if iDb < 0 { return } pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16 /* If the temp database has been explicitly named as part of the ** pragma, make sure it is open. */ if iDb == int32(1) && _sqlite3OpenTempDatabase(tls, pParse) != 0 { return } zLeft = _sqlite3NameFromToken(tls, db, *(*uintptr)(unsafe.Pointer(bp))) if !(zLeft != 0) { return } if minusFlag != 0 { zRight = _sqlite3MPrintf(tls, db, __ccgo_ts+17945, libc.VaList(bp+136, pValue)) } else { zRight = _sqlite3NameFromToken(tls, db, pValue) } _ = libc.Int32FromInt32(0) if (*TToken)(unsafe.Pointer(pId2)).Fn > uint32(0) { v1 = (*TDb)(unsafe.Pointer(pDb)).FzDbSName } else { v1 = uintptr(0) } zDb = v1 if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_PRAGMA), zLeft, zRight, zDb) != 0 { goto pragma_out } /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS ** connection. If it returns SQLITE_OK, then assume that the VFS ** handled the pragma and generate a no-op prepared statement. ** ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed, ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file ** object corresponding to the database file to which the pragma ** statement refers. ** ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA ** file control is an array of pointers to strings (char**) in which the ** second element of the array is the name of the pragma and the third ** element is the argument to the pragma or NULL if the pragma has no ** argument. */ (*(*[4]uintptr)(unsafe.Pointer(bp + 4)))[0] = uintptr(0) (*(*[4]uintptr)(unsafe.Pointer(bp + 4)))[int32(1)] = zLeft (*(*[4]uintptr)(unsafe.Pointer(bp + 4)))[int32(2)] = zRight (*(*[4]uintptr)(unsafe.Pointer(bp + 4)))[int32(3)] = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).FbusyHandler.FnBusy = 0 rc = Xsqlite3_file_control(tls, db, zDb, int32(SQLITE_FCNTL_PRAGMA), bp+4) if rc == SQLITE_OK { _sqlite3VdbeSetNumCols(tls, v, int32(1)) _sqlite3VdbeSetColName(tls, v, 0, COLNAME_NAME, (*(*[4]uintptr)(unsafe.Pointer(bp + 4)))[0], uintptr(-libc.Int32FromInt32(1))) _returnSingleText(tls, v, (*(*[4]uintptr)(unsafe.Pointer(bp + 4)))[0]) Xsqlite3_free(tls, (*(*[4]uintptr)(unsafe.Pointer(bp + 4)))[0]) goto pragma_out } if rc != int32(SQLITE_NOTFOUND) { if (*(*[4]uintptr)(unsafe.Pointer(bp + 4)))[0] != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+3795, libc.VaList(bp+136, (*(*[4]uintptr)(unsafe.Pointer(bp + 4)))[0])) Xsqlite3_free(tls, (*(*[4]uintptr)(unsafe.Pointer(bp + 4)))[0]) } (*TParse)(unsafe.Pointer(pParse)).FnErr++ (*TParse)(unsafe.Pointer(pParse)).Frc = rc goto pragma_out } /* Locate the pragma in the lookup table */ pPragma = _pragmaLocate(tls, zLeft) if pPragma == uintptr(0) { /* IMP: R-43042-22504 No error messages are generated if an ** unknown pragma is issued. */ goto pragma_out } /* Make sure the database schema is loaded if the pragma requires that */ if int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&int32(PragFlg_NeedSchema) != 0 { if _sqlite3ReadSchema(tls, pParse) != 0 { goto pragma_out } } /* Register the result column names for pragmas that return results */ if int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&int32(PragFlg_NoColumns) == 0 && (int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&int32(PragFlg_NoColumns1) == 0 || zRight == uintptr(0)) { _setPragmaResultColumnNames(tls, v, pPragma) } /* Jump to the appropriate pragma handler */ switch int32((*TPragmaName)(unsafe.Pointer(pPragma)).FePragTyp) { /* ** PRAGMA [schema.]default_cache_size ** PRAGMA [schema.]default_cache_size=N ** ** The first form reports the current persistent setting for the ** page cache size. The value returned is the maximum number of ** pages in the page cache. The second form sets both the current ** page cache size value and the persistent page cache size value ** stored in the database file. ** ** Older versions of SQLite would set the default cache size to a ** negative number to indicate synchronous=OFF. These days, synchronous ** is always on by default regardless of the sign of the default cache ** size. But continue to take the absolute value of the default cache ** size of historical compatibility. */ case int32(PragTyp_DEFAULT_CACHE_SIZE): _sqlite3VdbeUsesBtree(tls, v, iDb) if !(zRight != 0) { *(*int32)(unsafe.Pointer(pParse + 44)) += int32(2) aOp = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(36)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_getCacheSize)), _iLn3) if 0 != 0 { break } (*(*TVdbeOp)(unsafe.Pointer(aOp))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp + 1*20))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp + 6*20))).Fp1 = -int32(2000) } else { size = _sqlite3AbsInt32(tls, _sqlite3Atoi(tls, zRight)) _sqlite3BeginWriteOperation(tls, pParse, 0, iDb) _sqlite3VdbeAddOp3(tls, v, int32(OP_SetCookie), iDb, int32(BTREE_DEFAULT_CACHE_SIZE), size) _ = libc.Int32FromInt32(0) (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size = size _sqlite3BtreeSetCacheSize(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size) } break /* ** PRAGMA [schema.]page_size ** PRAGMA [schema.]page_size=N ** ** The first form reports the current setting for the ** database page size in bytes. The second form sets the ** database page size value. The value can only be set if ** the database has not yet been created. */ fallthrough case int32(PragTyp_PAGE_SIZE): pBt = (*TDb)(unsafe.Pointer(pDb)).FpBt _ = libc.Int32FromInt32(0) if !(zRight != 0) { if pBt != 0 { v2 = _sqlite3BtreeGetPageSize(tls, pBt) } else { v2 = 0 } size1 = v2 _returnSingleInt(tls, v, int64(size1)) } else { /* Malloc may fail when setting the page-size, as there is an internal ** buffer that the pager module resizes using sqlite3_realloc(). */ (*Tsqlite3)(unsafe.Pointer(db)).FnextPagesize = _sqlite3Atoi(tls, zRight) if int32(SQLITE_NOMEM) == _sqlite3BtreeSetPageSize(tls, pBt, (*Tsqlite3)(unsafe.Pointer(db)).FnextPagesize, 0, 0) { _sqlite3OomFault(tls, db) } } break /* ** PRAGMA [schema.]secure_delete ** PRAGMA [schema.]secure_delete=ON/OFF/FAST ** ** The first form reports the current setting for the ** secure_delete flag. The second form changes the secure_delete ** flag setting and reports the new value. */ fallthrough case int32(PragTyp_SECURE_DELETE): pBt1 = (*TDb)(unsafe.Pointer(pDb)).FpBt b = -int32(1) _ = libc.Int32FromInt32(0) if zRight != 0 { if Xsqlite3_stricmp(tls, zRight, __ccgo_ts+17949) == 0 { b = int32(2) } else { b = int32(_sqlite3GetBoolean(tls, zRight, uint8(0))) } } if (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0) && b >= 0 { ii = 0 for { if !(ii < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } _sqlite3BtreeSecureDelete(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii)*16))).FpBt, b) goto _3 _3: ; ii++ } } b = _sqlite3BtreeSecureDelete(tls, pBt1, b) _returnSingleInt(tls, v, int64(b)) break /* ** PRAGMA [schema.]max_page_count ** PRAGMA [schema.]max_page_count=N ** ** The first form reports the current setting for the ** maximum number of pages in the database file. The ** second form attempts to change this setting. Both ** forms return the current setting. ** ** The absolute value of N is used. This is undocumented and might ** change. The only purpose is to provide an easy way to test ** the sqlite3AbsInt32() function. ** ** PRAGMA [schema.]page_count ** ** Return the number of pages in the specified database. */ fallthrough case int32(PragTyp_PAGE_COUNT): *(*Ti64)(unsafe.Pointer(bp + 24)) = 0 _sqlite3CodeVerifySchema(tls, pParse, iDb) v5 = pParse + 44 *(*int32)(unsafe.Pointer(v5))++ v4 = *(*int32)(unsafe.Pointer(v5)) iReg = v4 if int32(_sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(zLeft))]) == int32('p') { _sqlite3VdbeAddOp2(tls, v, int32(OP_Pagecount), iDb, iReg) } else { if zRight != 0 && _sqlite3DecOrHexToI64(tls, zRight, bp+24) == 0 { if *(*Ti64)(unsafe.Pointer(bp + 24)) < 0 { *(*Ti64)(unsafe.Pointer(bp + 24)) = 0 } else { if *(*Ti64)(unsafe.Pointer(bp + 24)) > libc.Int64FromUint32(0xfffffffe) { *(*Ti64)(unsafe.Pointer(bp + 24)) = libc.Int64FromUint32(0xfffffffe) } } } else { *(*Ti64)(unsafe.Pointer(bp + 24)) = 0 } _sqlite3VdbeAddOp3(tls, v, int32(OP_MaxPgcnt), iDb, iReg, int32(*(*Ti64)(unsafe.Pointer(bp + 24)))) } _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), iReg, int32(1)) break /* ** PRAGMA [schema.]locking_mode ** PRAGMA [schema.]locking_mode = (normal|exclusive) */ fallthrough case int32(PragTyp_LOCKING_MODE): zRet = __ccgo_ts + 17740 eMode = _getLockingMode(tls, zRight) if (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0) && eMode == -int32(1) { /* Simple "PRAGMA locking_mode;" statement. This is a query for ** the current default locking mode (which may be different to ** the locking-mode of the main database). */ eMode = int32((*Tsqlite3)(unsafe.Pointer(db)).FdfltLockMode) } else { if (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0) { _ = libc.Int32FromInt32(0) ii1 = int32(2) for { if !(ii1 < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pPager = _sqlite3BtreePager(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii1)*16))).FpBt) _sqlite3PagerLockingMode(tls, pPager, eMode) goto _6 _6: ; ii1++ } (*Tsqlite3)(unsafe.Pointer(db)).FdfltLockMode = uint8(eMode) } pPager = _sqlite3BtreePager(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) eMode = _sqlite3PagerLockingMode(tls, pPager, eMode) } _ = libc.Int32FromInt32(0) if eMode == int32(PAGER_LOCKINGMODE_EXCLUSIVE) { zRet = __ccgo_ts + 17730 } _returnSingleText(tls, v, zRet) break /* ** PRAGMA [schema.]journal_mode ** PRAGMA [schema.]journal_mode = ** (delete|persist|off|truncate|memory|wal|off) */ fallthrough case int32(PragTyp_JOURNAL_MODE): /* Loop counter */ if zRight == uintptr(0) { /* If there is no "=MODE" part of the pragma, do a query for the ** current mode */ eMode1 = -int32(1) } else { n = _sqlite3Strlen30(tls, zRight) eMode1 = 0 for { v8 = _sqlite3JournalModename(tls, eMode1) zMode = v8 if !(v8 != uintptr(0)) { break } if Xsqlite3_strnicmp(tls, zRight, zMode, n) == 0 { break } goto _7 _7: ; eMode1++ } if !(zMode != 0) { /* If the "=MODE" part does not match any known journal mode, ** then do a query */ eMode1 = -int32(1) } if eMode1 == int32(PAGER_JOURNALMODE_OFF) && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_Defensive) != uint64(0) { /* Do not allow journal-mode "OFF" in defensive since the database ** can become corrupted using ordinary SQL when the journal is off */ eMode1 = -int32(1) } } if eMode1 == -int32(1) && (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0) { /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */ iDb = 0 (*TToken)(unsafe.Pointer(pId2)).Fn = uint32(1) } ii2 = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1) for { if !(ii2 >= 0) { break } if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii2)*16))).FpBt != 0 && (ii2 == iDb || (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0)) { _sqlite3VdbeUsesBtree(tls, v, ii2) _sqlite3VdbeAddOp3(tls, v, int32(OP_JournalMode), ii2, int32(1), eMode1) } goto _9 _9: ; ii2-- } _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), int32(1), int32(1)) break /* ** PRAGMA [schema.]journal_size_limit ** PRAGMA [schema.]journal_size_limit=N ** ** Get or set the size limit on rollback journal files. */ fallthrough case int32(PragTyp_JOURNAL_SIZE_LIMIT): pPager1 = _sqlite3BtreePager(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) *(*Ti64)(unsafe.Pointer(bp + 32)) = int64(-int32(2)) if zRight != 0 { _sqlite3DecOrHexToI64(tls, zRight, bp+32) if *(*Ti64)(unsafe.Pointer(bp + 32)) < int64(-int32(1)) { *(*Ti64)(unsafe.Pointer(bp + 32)) = int64(-int32(1)) } } *(*Ti64)(unsafe.Pointer(bp + 32)) = _sqlite3PagerJournalSizeLimit(tls, pPager1, *(*Ti64)(unsafe.Pointer(bp + 32))) _returnSingleInt(tls, v, *(*Ti64)(unsafe.Pointer(bp + 32))) break /* ** PRAGMA [schema.]auto_vacuum ** PRAGMA [schema.]auto_vacuum=N ** ** Get or set the value of the database 'auto-vacuum' parameter. ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL */ fallthrough case int32(PragTyp_AUTO_VACUUM): pBt2 = (*TDb)(unsafe.Pointer(pDb)).FpBt _ = libc.Int32FromInt32(0) if !(zRight != 0) { _returnSingleInt(tls, v, int64(_sqlite3BtreeGetAutoVacuum(tls, pBt2))) } else { eAuto = _getAutoVacuum(tls, zRight) _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).FnextAutovac = int8(uint8(eAuto)) /* Call SetAutoVacuum() to set initialize the internal auto and ** incr-vacuum flags. This is required in case this connection ** creates the database file. It is important that it is created ** as an auto-vacuum capable db. */ rc = _sqlite3BtreeSetAutoVacuum(tls, pBt2, eAuto) if rc == SQLITE_OK && (eAuto == int32(1) || eAuto == int32(2)) { iAddr = _sqlite3VdbeCurrentAddr(tls, v) aOp1 = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(20)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_setMeta6)), _iLn11) if 0 != 0 { break } (*(*TVdbeOp)(unsafe.Pointer(aOp1))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp1 + 1*20))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp1 + 2*20))).Fp2 = iAddr + int32(4) (*(*TVdbeOp)(unsafe.Pointer(aOp1 + 4*20))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp1 + 4*20))).Fp3 = eAuto - int32(1) _sqlite3VdbeUsesBtree(tls, v, iDb) } } break /* ** PRAGMA [schema.]incremental_vacuum(N) ** ** Do N steps of incremental vacuuming on a database. */ fallthrough case int32(PragTyp_INCREMENTAL_VACUUM): *(*int32)(unsafe.Pointer(bp + 40)) = 0 if zRight == uintptr(0) || !(_sqlite3GetInt32(tls, zRight, bp+40) != 0) || *(*int32)(unsafe.Pointer(bp + 40)) <= 0 { *(*int32)(unsafe.Pointer(bp + 40)) = int32(0x7fffffff) } _sqlite3BeginWriteOperation(tls, pParse, 0, iDb) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), *(*int32)(unsafe.Pointer(bp + 40)), int32(1)) addr = _sqlite3VdbeAddOp1(tls, v, int32(OP_IncrVacuum), iDb) _sqlite3VdbeAddOp1(tls, v, int32(OP_ResultRow), int32(1)) _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), int32(1), -int32(1)) _sqlite3VdbeAddOp2(tls, v, int32(OP_IfPos), int32(1), addr) _sqlite3VdbeJumpHere(tls, v, addr) break /* ** PRAGMA [schema.]cache_size ** PRAGMA [schema.]cache_size=N ** ** The first form reports the current local setting for the ** page cache size. The second form sets the local ** page cache size value. If N is positive then that is the ** number of pages in the cache. If N is negative, then the ** number of pages is adjusted so that the cache uses -N kibibytes ** of memory. */ fallthrough case int32(PragTyp_CACHE_SIZE): _ = libc.Int32FromInt32(0) if !(zRight != 0) { _returnSingleInt(tls, v, int64((*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size)) } else { size2 = _sqlite3Atoi(tls, zRight) (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size = size2 _sqlite3BtreeSetCacheSize(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size) } break /* ** PRAGMA [schema.]cache_spill ** PRAGMA cache_spill=BOOLEAN ** PRAGMA [schema.]cache_spill=N ** ** The first form reports the current local setting for the ** page cache spill size. The second form turns cache spill on ** or off. When turning cache spill on, the size is set to the ** current cache_size. The third form sets a spill size that ** may be different form the cache size. ** If N is positive then that is the ** number of pages in the cache. If N is negative, then the ** number of pages is adjusted so that the cache uses -N kibibytes ** of memory. ** ** If the number of cache_spill pages is less then the number of ** cache_size pages, no spilling occurs until the page count exceeds ** the number of cache_size pages. ** ** The cache_spill=BOOLEAN setting applies to all attached schemas, ** not just the schema specified. */ fallthrough case int32(PragTyp_CACHE_SPILL): _ = libc.Int32FromInt32(0) if !(zRight != 0) { if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_CacheSpill) == uint64(0) { v10 = 0 } else { v10 = _sqlite3BtreeSetSpillSize(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, 0) } _returnSingleInt(tls, v, int64(v10)) } else { *(*int32)(unsafe.Pointer(bp + 44)) = int32(1) if _sqlite3GetInt32(tls, zRight, bp+44) != 0 { _sqlite3BtreeSetSpillSize(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, *(*int32)(unsafe.Pointer(bp + 44))) } if _sqlite3GetBoolean(tls, zRight, libc.BoolUint8(*(*int32)(unsafe.Pointer(bp + 44)) != 0)) != 0 { *(*Tu64)(unsafe.Pointer(db + 32)) |= uint64(SQLITE_CacheSpill) } else { *(*Tu64)(unsafe.Pointer(db + 32)) &= ^libc.Uint64FromInt32(SQLITE_CacheSpill) } _setAllPagerFlags(tls, db) } break /* ** PRAGMA [schema.]mmap_size(N) ** ** Used to set mapping size limit. The mapping size limit is ** used to limit the aggregate size of all memory mapped regions of the ** database file. If this parameter is set to zero, then memory mapping ** is not used at all. If N is negative, then the default memory map ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set. ** The parameter N is measured in bytes. ** ** This value is advisory. The underlying VFS is free to memory map ** as little or as much as it wants. Except, if N is set to 0 then the ** upper layers will never invoke the xFetch interfaces to the VFS. */ fallthrough case int32(PragTyp_MMAP_SIZE): _ = libc.Int32FromInt32(0) if zRight != 0 { _sqlite3DecOrHexToI64(tls, zRight, bp+48) if *(*Tsqlite3_int64)(unsafe.Pointer(bp + 48)) < 0 { *(*Tsqlite3_int64)(unsafe.Pointer(bp + 48)) = _sqlite3Config.FszMmap } if (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0) { (*Tsqlite3)(unsafe.Pointer(db)).FszMmap = *(*Tsqlite3_int64)(unsafe.Pointer(bp + 48)) } ii3 = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1) for { if !(ii3 >= 0) { break } if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii3)*16))).FpBt != 0 && (ii3 == iDb || (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0)) { _sqlite3BtreeSetMmapLimit(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii3)*16))).FpBt, *(*Tsqlite3_int64)(unsafe.Pointer(bp + 48))) } goto _11 _11: ; ii3-- } } *(*Tsqlite3_int64)(unsafe.Pointer(bp + 48)) = int64(-int32(1)) rc = Xsqlite3_file_control(tls, db, zDb, int32(SQLITE_FCNTL_MMAP_SIZE), bp+48) if rc == SQLITE_OK { _returnSingleInt(tls, v, *(*Tsqlite3_int64)(unsafe.Pointer(bp + 48))) } else { if rc != int32(SQLITE_NOTFOUND) { (*TParse)(unsafe.Pointer(pParse)).FnErr++ (*TParse)(unsafe.Pointer(pParse)).Frc = rc } } break /* ** PRAGMA temp_store ** PRAGMA temp_store = "default"|"memory"|"file" ** ** Return or set the local value of the temp_store flag. Changing ** the local value does not make changes to the disk file and the default ** value will be restored the next time the database is opened. ** ** Note that it is possible for the library compile-time options to ** override this setting */ fallthrough case int32(PragTyp_TEMP_STORE): if !(zRight != 0) { _returnSingleInt(tls, v, int64((*Tsqlite3)(unsafe.Pointer(db)).Ftemp_store)) } else { _changeTempStorage(tls, pParse, zRight) } break /* ** PRAGMA temp_store_directory ** PRAGMA temp_store_directory = ""|"directory_name" ** ** Return or set the local value of the temp_store_directory flag. Changing ** the value sets a specific directory to be used for temporary files. ** Setting to a null string reverts to the default temporary directory search. ** If temporary directory is changed, then invalidateTempStorage. ** */ fallthrough case int32(PragTyp_TEMP_STORE_DIRECTORY): Xsqlite3_mutex_enter(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1))) if !(zRight != 0) { _returnSingleText(tls, v, Xsqlite3_temp_directory) } else { if *(*uint8)(unsafe.Pointer(zRight)) != 0 { rc = _sqlite3OsAccess(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, zRight, int32(SQLITE_ACCESS_READWRITE), bp+56) if rc != SQLITE_OK || *(*int32)(unsafe.Pointer(bp + 56)) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+17954, 0) Xsqlite3_mutex_leave(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1))) goto pragma_out } } if libc.Bool(false) || libc.Bool(true) && int32((*Tsqlite3)(unsafe.Pointer(db)).Ftemp_store) <= int32(1) || libc.Bool(libc.Bool(false) && int32((*Tsqlite3)(unsafe.Pointer(db)).Ftemp_store) == int32(1)) { _invalidateTempStorage(tls, pParse) } Xsqlite3_free(tls, Xsqlite3_temp_directory) if *(*uint8)(unsafe.Pointer(zRight)) != 0 { Xsqlite3_temp_directory = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+136, zRight)) } else { Xsqlite3_temp_directory = uintptr(0) } } Xsqlite3_mutex_leave(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1))) break /* ** PRAGMA [schema.]synchronous ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA ** ** Return or set the local value of the synchronous flag. Changing ** the local value does not make changes to the disk file and the ** default value will be restored the next time the database is ** opened. */ fallthrough case int32(PragTyp_SYNCHRONOUS): if !(zRight != 0) { _returnSingleInt(tls, v, int64(int32((*TDb)(unsafe.Pointer(pDb)).Fsafety_level)-int32(1))) } else { if !((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+17979, 0) } else { if iDb != int32(1) { iLevel = (int32(_getSafetyLevel(tls, zRight, 0, uint8(1))) + int32(1)) & int32(PAGER_SYNCHRONOUS_MASK) if iLevel == 0 { iLevel = int32(1) } (*TDb)(unsafe.Pointer(pDb)).Fsafety_level = uint8(iLevel) (*TDb)(unsafe.Pointer(pDb)).FbSyncSet = uint8(1) _setAllPagerFlags(tls, db) } } } case int32(PragTyp_FLAG): if zRight == uintptr(0) { _setPragmaResultColumnNames(tls, v, pPragma) _returnSingleInt(tls, v, libc.BoolInt64((*Tsqlite3)(unsafe.Pointer(db)).Fflags&(*TPragmaName)(unsafe.Pointer(pPragma)).FiArg != uint64(0))) } else { mask = (*TPragmaName)(unsafe.Pointer(pPragma)).FiArg /* Mask of bits to set or clear. */ if int32((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit) == 0 { /* Foreign key support may not be enabled or disabled while not ** in auto-commit mode. */ mask &= uint64(^libc.Int32FromInt32(SQLITE_ForeignKeys)) } if _sqlite3GetBoolean(tls, zRight, uint8(0)) != 0 { if mask&uint64(SQLITE_WriteSchema) == uint64(0) || (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_Defensive) == uint64(0) { *(*Tu64)(unsafe.Pointer(db + 32)) |= mask } } else { *(*Tu64)(unsafe.Pointer(db + 32)) &= ^mask if mask == uint64(SQLITE_DeferFKs) { (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons = 0 } if mask&uint64(SQLITE_WriteSchema) != uint64(0) && Xsqlite3_stricmp(tls, zRight, __ccgo_ts+18032) == 0 { /* IMP: R-60817-01178 If the argument is "RESET" then schema ** writing is disabled (as with "PRAGMA writable_schema=OFF") and, ** in addition, the schema is reloaded. */ _sqlite3ResetAllSchemasOfConnection(tls, db) } } /* Many of the flag-pragmas modify the code generated by the SQL ** compiler (eg. count_changes). So add an opcode to expire all ** compiled SQL statements after modifying a pragma value. */ _sqlite3VdbeAddOp0(tls, v, int32(OP_Expire)) _setAllPagerFlags(tls, db) } break /* ** PRAGMA table_info(
    ) ** ** Return a single row for each column of the named table. The columns of ** the returned data set are: ** ** cid: Column id (numbered from left to right, starting at 0) ** name: Column name ** type: Column declaration type. ** notnull: True if 'NOT NULL' is part of column declaration ** dflt_value: The default value for the column, if any. ** pk: Non-zero for PK fields. */ fallthrough case int32(PragTyp_TABLE_INFO): if zRight != 0 { _sqlite3CodeVerifyNamedSchema(tls, pParse, zDb) pTab = _sqlite3LocateTable(tls, pParse, uint32(LOCATE_NOERR), zRight, zDb) if pTab != 0 { nHidden = 0 pPk = _sqlite3PrimaryKeyIndex(tls, pTab) (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(7) _sqlite3ViewGetColumnNames(tls, pParse, pTab) i = 0 pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } isHidden = 0 if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_NOINSERT) != 0 { if (*TPragmaName)(unsafe.Pointer(pPragma)).FiArg == uint64(0) { nHidden++ goto _12 } if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 { isHidden = int32(2) /* GENERATED ALWAYS AS ... VIRTUAL */ } else { if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_STORED) != 0 { isHidden = int32(3) /* GENERATED ALWAYS AS ... STORED */ } else { _ = libc.Int32FromInt32(0) isHidden = int32(1) /* HIDDEN */ } } } if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_PRIMKEY) == 0 { k = 0 } else { if pPk == uintptr(0) { k = int32(1) } else { k = int32(1) for { if !(k <= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) && int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(k-int32(1))*2))) != i) { break } goto _13 _13: ; k++ } } } pColExpr = _sqlite3ColumnExpr(tls, pTab, pCol) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TPragmaName)(unsafe.Pointer(pPragma)).FiArg != 0 { v14 = __ccgo_ts + 18038 } else { v14 = __ccgo_ts + 18046 } if int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf>>0)) != 0 { v15 = int32(1) } else { v15 = 0 } if isHidden >= int32(2) || pColExpr == uintptr(0) { v16 = uintptr(0) } else { v16 = *(*uintptr)(unsafe.Pointer(pColExpr + 8)) } _sqlite3VdbeMultiLoad(tls, v, int32(1), v14, libc.VaList(bp+136, i-nHidden, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, _sqlite3ColumnType(tls, pCol, __ccgo_ts+1648), v15, v16, k, isHidden)) goto _12 _12: ; i++ pCol += 12 } } } break /* ** PRAGMA table_list ** ** Return a single row for each table, virtual table, or view in the ** entire schema. ** ** schema: Name of attached database hold this table ** name: Name of the table itself ** type: "table", "view", "virtual", "shadow" ** ncol: Number of columns ** wr: True for a WITHOUT ROWID table ** strict: True for a STRICT table */ fallthrough case int32(PragTyp_TABLE_LIST): (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(6) _sqlite3CodeVerifyNamedSchema(tls, pParse, zDb) ii4 = 0 for { if !(ii4 < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } if zDb != 0 && Xsqlite3_stricmp(tls, zDb, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii4)*16))).FzDbSName) != 0 { goto _17 } /* Ensure that the Table.nCol field is initialized for all views ** and virtual tables. Each time we initialize a Table.nCol value ** for a table, that can potentially disrupt the hash table, so restart ** the initialization scan. */ pHash = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii4)*16))).FpSchema + 8 initNCol = int32((*THash)(unsafe.Pointer(pHash)).Fcount) for { v18 = initNCol initNCol-- if !(v18 != 0) { break } k1 = (*THash)(unsafe.Pointer(pHash)).Ffirst for { if !(int32(1) != 0) { break } if k1 == uintptr(0) { initNCol = 0 break } pTab1 = (*THashElem)(unsafe.Pointer(k1)).Fdata if int32((*TTable)(unsafe.Pointer(pTab1)).FnCol) == 0 { zSql = _sqlite3MPrintf(tls, db, __ccgo_ts+18053, libc.VaList(bp+136, (*TTable)(unsafe.Pointer(pTab1)).FzName)) if zSql != 0 { *(*uintptr)(unsafe.Pointer(bp + 60)) = uintptr(0) Xsqlite3_prepare(tls, db, zSql, -int32(1), bp+60, uintptr(0)) Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp + 60))) _sqlite3DbFree(tls, db, zSql) } if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _sqlite3ErrorMsg(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpParse, __ccgo_ts+1618, 0) (*TParse)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpParse)).Frc = int32(SQLITE_NOMEM) } pHash = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii4)*16))).FpSchema + 8 break } goto _19 _19: ; k1 = (*THashElem)(unsafe.Pointer(k1)).Fnext } } k1 = (*THash)(unsafe.Pointer(pHash)).Ffirst for { if !(k1 != 0) { break } pTab2 = (*THashElem)(unsafe.Pointer(k1)).Fdata if zRight != 0 && Xsqlite3_stricmp(tls, zRight, (*TTable)(unsafe.Pointer(pTab2)).FzName) != 0 { goto _20 } if int32((*TTable)(unsafe.Pointer(pTab2)).FeTabType) == int32(TABTYP_VIEW) { zType = __ccgo_ts + 10796 } else { if int32((*TTable)(unsafe.Pointer(pTab2)).FeTabType) == int32(TABTYP_VTAB) { zType = __ccgo_ts + 13044 } else { if (*TTable)(unsafe.Pointer(pTab2)).FtabFlags&uint32(TF_Shadow) != 0 { zType = __ccgo_ts + 18069 } else { zType = __ccgo_ts + 9058 } } } _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+18076, libc.VaList(bp+136, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii4)*16))).FzDbSName, _sqlite3PreferredTableName(tls, (*TTable)(unsafe.Pointer(pTab2)).FzName), zType, int32((*TTable)(unsafe.Pointer(pTab2)).FnCol), libc.BoolInt32((*TTable)(unsafe.Pointer(pTab2)).FtabFlags&uint32(TF_WithoutRowid) != uint32(0)), libc.BoolInt32((*TTable)(unsafe.Pointer(pTab2)).FtabFlags&uint32(TF_Strict) != uint32(0)))) goto _20 _20: ; k1 = (*THashElem)(unsafe.Pointer(k1)).Fnext } goto _17 _17: ; ii4++ } case int32(PragTyp_INDEX_INFO): if zRight != 0 { pIdx = _sqlite3FindIndex(tls, db, zRight, zDb) if pIdx == uintptr(0) { /* If there is no index named zRight, check to see if there is a ** WITHOUT ROWID table named zRight, and if there is, show the ** structure of the PRIMARY KEY index for that table. */ pTab3 = _sqlite3LocateTable(tls, pParse, uint32(LOCATE_NOERR), zRight, zDb) if pTab3 != 0 && !((*TTable)(unsafe.Pointer(pTab3)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { pIdx = _sqlite3PrimaryKeyIndex(tls, pTab3) } } if pIdx != 0 { iIdxDb = _sqlite3SchemaToIndex(tls, db, (*TIndex)(unsafe.Pointer(pIdx)).FpSchema) if (*TPragmaName)(unsafe.Pointer(pPragma)).FiArg != 0 { /* PRAGMA index_xinfo (newer version with more rows and columns) */ mx = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(6) } else { /* PRAGMA index_info (legacy version) */ mx = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(3) } pTab3 = (*TIndex)(unsafe.Pointer(pIdx)).FpTable _sqlite3CodeVerifySchema(tls, pParse, iIdxDb) _ = libc.Int32FromInt32(0) i1 = 0 for { if !(i1 < mx) { break } cnum = *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i1)*2)) if int32(cnum) < 0 { v22 = uintptr(0) } else { v22 = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab3)).FaCol + uintptr(cnum)*12))).FzCnName } _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+18083, libc.VaList(bp+136, i1, int32(cnum), v22)) if (*TPragmaName)(unsafe.Pointer(pPragma)).FiArg != 0 { _sqlite3VdbeMultiLoad(tls, v, int32(4), __ccgo_ts+18088, libc.VaList(bp+136, int32(*(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder + uintptr(i1)))), *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(i1)*4)), libc.BoolInt32(i1 < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)))) } _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), int32(1), (*TParse)(unsafe.Pointer(pParse)).FnMem) goto _21 _21: ; i1++ } } } case int32(PragTyp_INDEX_LIST): if zRight != 0 { pTab4 = _sqlite3FindTable(tls, db, zRight, zDb) if pTab4 != 0 { iTabDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab4)).FpSchema) (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(5) _sqlite3CodeVerifySchema(tls, pParse, iTabDb) pIdx1 = (*TTable)(unsafe.Pointer(pTab4)).FpIndex i2 = libc.Int32FromInt32(0) for { if !(pIdx1 != 0) { break } azOrigin = [3]uintptr{ 0: __ccgo_ts + 18093, 1: __ccgo_ts + 18095, 2: __ccgo_ts + 16582, } _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+18097, libc.VaList(bp+136, i2, (*TIndex)(unsafe.Pointer(pIdx1)).FzName, libc.BoolInt32(int32((*TIndex)(unsafe.Pointer(pIdx1)).FonError) != OE_None), azOrigin[int32(uint32(*(*uint16)(unsafe.Pointer(pIdx1 + 56))&0x3>>0))], libc.BoolInt32((*TIndex)(unsafe.Pointer(pIdx1)).FpPartIdxWhere != uintptr(0)))) goto _23 _23: ; pIdx1 = (*TIndex)(unsafe.Pointer(pIdx1)).FpNext i2++ } } } case int32(PragTyp_DATABASE_LIST): (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(3) i3 = 0 for { if !(i3 < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i3)*16))).FpBt == uintptr(0) { goto _24 } _ = libc.Int32FromInt32(0) _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+18103, libc.VaList(bp+136, i3, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i3)*16))).FzDbSName, _sqlite3BtreeGetFilename(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i3)*16))).FpBt))) goto _24 _24: ; i3++ } case int32(PragTyp_COLLATION_LIST): i4 = 0 (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(2) p = (*THash)(unsafe.Pointer(db + 448)).Ffirst for { if !(p != 0) { break } pColl = (*THashElem)(unsafe.Pointer(p)).Fdata v26 = i4 i4++ _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+18107, libc.VaList(bp+136, v26, (*TCollSeq)(unsafe.Pointer(pColl)).FzName)) goto _25 _25: ; p = (*THashElem)(unsafe.Pointer(p)).Fnext } case int32(PragTyp_FUNCTION_LIST): showInternFunc = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_InternalFunc) != uint32(0)) (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(6) i5 = 0 for { if !(i5 < int32(SQLITE_FUNC_HASH_SZ)) { break } p1 = *(*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)) + uintptr(i5)*4)) for { if !(p1 != 0) { break } _ = libc.Int32FromInt32(0) _pragmaFunclistLine(tls, v, p1, int32(1), showInternFunc) goto _28 _28: ; p1 = *(*uintptr)(unsafe.Pointer(p1 + 36)) } goto _27 _27: ; i5++ } j = (*THash)(unsafe.Pointer(db + 432)).Ffirst for { if !(j != 0) { break } p1 = (*THashElem)(unsafe.Pointer(j)).Fdata _ = libc.Int32FromInt32(0) _pragmaFunclistLine(tls, v, p1, 0, showInternFunc) goto _29 _29: ; j = (*THashElem)(unsafe.Pointer(j)).Fnext } case int32(PragTyp_MODULE_LIST): (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(1) j1 = (*THash)(unsafe.Pointer(db + 404)).Ffirst for { if !(j1 != 0) { break } pMod = (*THashElem)(unsafe.Pointer(j1)).Fdata _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+7617, libc.VaList(bp+136, (*TModule)(unsafe.Pointer(pMod)).FzName)) goto _30 _30: ; j1 = (*THashElem)(unsafe.Pointer(j1)).Fnext } case int32(PragTyp_PRAGMA_LIST): i6 = 0 for { if !(i6 < int32(libc.Uint32FromInt64(1056)/libc.Uint32FromInt64(16))) { break } _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+7617, libc.VaList(bp+136, _aPragmaName[i6].FzName)) goto _31 _31: ; i6++ } case int32(PragTyp_FOREIGN_KEY_LIST): if zRight != 0 { pTab5 = _sqlite3FindTable(tls, db, zRight, zDb) if pTab5 != 0 && int32((*TTable)(unsafe.Pointer(pTab5)).FeTabType) == TABTYP_NORM { pFK = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab5 + 44))).FpFKey if pFK != 0 { iTabDb1 = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab5)).FpSchema) i7 = 0 (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(8) _sqlite3CodeVerifySchema(tls, pParse, iTabDb1) for pFK != 0 { j2 = 0 for { if !(j2 < (*TFKey)(unsafe.Pointer(pFK)).FnCol) { break } _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+18110, libc.VaList(bp+136, i7, j2, (*TFKey)(unsafe.Pointer(pFK)).FzTo, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab5)).FaCol + uintptr((*(*TsColMap)(unsafe.Pointer(pFK + 36 + uintptr(j2)*8))).FiFrom)*12))).FzCnName, (*(*TsColMap)(unsafe.Pointer(pFK + 36 + uintptr(j2)*8))).FzCol, _actionName(tls, *(*Tu8)(unsafe.Pointer(pFK + 25 + 1))), _actionName(tls, *(*Tu8)(unsafe.Pointer(pFK + 25))), __ccgo_ts+18119)) goto _32 _32: ; j2++ } i7++ pFK = (*TFKey)(unsafe.Pointer(pFK)).FpNextFrom } } } } case int32(PragTyp_FOREIGN_KEY_CHECK): /* child to parent column mapping */ regResult = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += int32(4) v34 = pParse + 44 *(*int32)(unsafe.Pointer(v34))++ v33 = *(*int32)(unsafe.Pointer(v34)) regRow = v33 k2 = (*THash)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema + 8)).Ffirst for k2 != 0 { if zRight != 0 { pTab6 = _sqlite3LocateTable(tls, pParse, uint32(0), zRight, zDb) k2 = uintptr(0) } else { pTab6 = (*THashElem)(unsafe.Pointer(k2)).Fdata k2 = (*THashElem)(unsafe.Pointer(k2)).Fnext } if pTab6 == uintptr(0) || !(int32((*TTable)(unsafe.Pointer(pTab6)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) || (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab6 + 44))).FpFKey == uintptr(0) { continue } iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab6)).FpSchema) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName _sqlite3CodeVerifySchema(tls, pParse, iDb) _sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab6)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pTab6)).FzName) _sqlite3TouchRegister(tls, pParse, int32((*TTable)(unsafe.Pointer(pTab6)).FnCol)+regRow) _sqlite3OpenTable(tls, pParse, 0, iDb, pTab6, int32(OP_OpenRead)) _sqlite3VdbeLoadString(tls, v, regResult, (*TTable)(unsafe.Pointer(pTab6)).FzName) _ = libc.Int32FromInt32(0) i8 = int32(1) pFK1 = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab6 + 44))).FpFKey for { if !(pFK1 != 0) { break } pParent = _sqlite3FindTable(tls, db, (*TFKey)(unsafe.Pointer(pFK1)).FzTo, zDb) if pParent == uintptr(0) { goto _35 } *(*uintptr)(unsafe.Pointer(bp + 64)) = uintptr(0) _sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pParent)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pParent)).FzName) x1 = _sqlite3FkLocateIndex(tls, pParse, pParent, pFK1, bp+64, uintptr(0)) if x1 == 0 { if *(*uintptr)(unsafe.Pointer(bp + 64)) == uintptr(0) { _sqlite3OpenTable(tls, pParse, i8, iDb, pParent, int32(OP_OpenRead)) } else { _sqlite3VdbeAddOp3(tls, v, int32(OP_OpenRead), i8, int32((*TIndex)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 64)))).Ftnum), iDb) _sqlite3VdbeSetP4KeyInfo(tls, pParse, *(*uintptr)(unsafe.Pointer(bp + 64))) } } else { k2 = uintptr(0) break } goto _35 _35: ; i8++ pFK1 = (*TFKey)(unsafe.Pointer(pFK1)).FpNextFrom } _ = libc.Int32FromInt32(0) if pFK1 != 0 { break } if (*TParse)(unsafe.Pointer(pParse)).FnTab < i8 { (*TParse)(unsafe.Pointer(pParse)).FnTab = i8 } addrTop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), 0) _ = libc.Int32FromInt32(0) i8 = int32(1) pFK1 = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab6 + 44))).FpFKey for { if !(pFK1 != 0) { break } pParent = _sqlite3FindTable(tls, db, (*TFKey)(unsafe.Pointer(pFK1)).FzTo, zDb) *(*uintptr)(unsafe.Pointer(bp + 64)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 68)) = uintptr(0) if pParent != 0 { x1 = _sqlite3FkLocateIndex(tls, pParse, pParent, pFK1, bp+64, bp+68) _ = libc.Int32FromInt32(0) } addrOk = _sqlite3VdbeMakeLabel(tls, pParse) /* Generate code to read the child key values into registers ** regRow..regRow+n. If any of the child key values are NULL, this ** row cannot cause an FK violation. Jump directly to addrOk in ** this case. */ _sqlite3TouchRegister(tls, pParse, regRow+(*TFKey)(unsafe.Pointer(pFK1)).FnCol) j3 = 0 for { if !(j3 < (*TFKey)(unsafe.Pointer(pFK1)).FnCol) { break } if *(*uintptr)(unsafe.Pointer(bp + 68)) != 0 { v38 = *(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 68)) + uintptr(j3)*4)) } else { v38 = (*(*TsColMap)(unsafe.Pointer(pFK1 + 36 + uintptr(j3)*8))).FiFrom } iCol = v38 _sqlite3ExprCodeGetColumnOfTable(tls, v, pTab6, 0, iCol, regRow+j3) _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regRow+j3, addrOk) goto _37 _37: ; j3++ } /* Generate code to query the parent index for a matching parent ** key. If a match is found, jump to addrOk. */ if *(*uintptr)(unsafe.Pointer(bp + 64)) != 0 { _sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), regRow, (*TFKey)(unsafe.Pointer(pFK1)).FnCol, 0, _sqlite3IndexAffinityStr(tls, db, *(*uintptr)(unsafe.Pointer(bp + 64))), (*TFKey)(unsafe.Pointer(pFK1)).FnCol) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), i8, addrOk, regRow, (*TFKey)(unsafe.Pointer(pFK1)).FnCol) } else { if pParent != 0 { jmp = _sqlite3VdbeCurrentAddr(tls, v) + int32(2) _sqlite3VdbeAddOp3(tls, v, int32(OP_SeekRowid), i8, jmp, regRow) _sqlite3VdbeGoto(tls, v, addrOk) _ = libc.Int32FromInt32(0) } } /* Generate code to report an FK violation to the caller. */ if (*TTable)(unsafe.Pointer(pTab6)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), 0, regResult+int32(1)) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regResult+int32(1)) } _sqlite3VdbeMultiLoad(tls, v, regResult+int32(2), __ccgo_ts+18124, libc.VaList(bp+136, (*TFKey)(unsafe.Pointer(pFK1)).FzTo, i8-int32(1))) _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), regResult, int32(4)) _sqlite3VdbeResolveLabel(tls, v, addrOk) _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp + 68))) goto _36 _36: ; i8++ pFK1 = (*TFKey)(unsafe.Pointer(pFK1)).FpNextFrom } _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), 0, addrTop+int32(1)) _sqlite3VdbeJumpHere(tls, v, addrTop) } break /* Reinstall the LIKE and GLOB functions. The variant of LIKE ** used will be case sensitive or not depending on the RHS. */ fallthrough case int32(PragTyp_CASE_SENSITIVE_LIKE): if zRight != 0 { _sqlite3RegisterLikeFunctions(tls, db, int32(_sqlite3GetBoolean(tls, zRight, uint8(0)))) } break /* PRAGMA integrity_check ** PRAGMA integrity_check(N) ** PRAGMA quick_check ** PRAGMA quick_check(N) ** ** Verify the integrity of the database. ** ** The "quick_check" is reduced version of ** integrity_check designed to detect most database corruption ** without the overhead of cross-checking indexes. Quick_check ** is linear time whereas integrity_check is O(NlogN). ** ** The maximum number of errors is 100 by default. A different default ** can be specified using a numeric parameter N. ** ** Or, the parameter N can be the name of a table. In that case, only ** the one table named is verified. The freelist is only verified if ** the named table is "sqlite_schema" (or one of its aliases). ** ** All schemas are checked by default. To check just a single ** schema, use the form: ** ** PRAGMA schema.integrity_check; */ fallthrough case int32(PragTyp_INTEGRITY_CHECK): pObjTab = uintptr(0) /* Check only this one table, if not NULL */ isQuick = libc.BoolInt32(int32(_sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(zLeft))]) == int32('q')) /* If the PRAGMA command was of the form "PRAGMA .integrity_check", ** then iDb is set to the index of the database identified by . ** In this case, the integrity of database iDb only is verified by ** the VDBE created below. ** ** Otherwise, if the command was simply "PRAGMA integrity_check" (or ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb ** to -1 here, to indicate that the VDBE should verify the integrity ** of all attached databases. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TToken)(unsafe.Pointer(pId2)).Fz == uintptr(0) { iDb = -int32(1) } /* Initialize the VDBE program */ (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(6) /* Set the maximum error count */ *(*int32)(unsafe.Pointer(bp + 72)) = int32(SQLITE_INTEGRITY_CHECK_ERROR_MAX) if zRight != 0 { if _sqlite3GetInt32(tls, zRight, bp+72) != 0 { if *(*int32)(unsafe.Pointer(bp + 72)) <= 0 { *(*int32)(unsafe.Pointer(bp + 72)) = int32(SQLITE_INTEGRITY_CHECK_ERROR_MAX) } } else { if iDb >= 0 { v39 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName } else { v39 = uintptr(0) } pObjTab = _sqlite3LocateTable(tls, pParse, uint32(0), zRight, v39) } } _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), *(*int32)(unsafe.Pointer(bp + 72))-int32(1), int32(1)) /* reg[1] holds errors left */ /* Do an integrity check on each database file */ i9 = 0 for { if !(i9 < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } /* Array of root page numbers of all btrees */ cnt = 0 /* Number of entries in aRoot[] */ mxIdx = 0 /* Maximum number of indexes for any table */ if libc.Bool(OMIT_TEMPDB != 0) && i9 == int32(1) { goto _40 } if iDb >= 0 && i9 != iDb { goto _40 } _sqlite3CodeVerifySchema(tls, pParse, i9) (*TParse)(unsafe.Pointer(pParse)).FokConstFactor = uint8(0) /* tag-20230327-1 */ /* Do an integrity check of the B-Tree ** ** Begin by finding the root pages numbers ** for all tables and indices in the database. */ _ = libc.Int32FromInt32(0) pTbls = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i9)*16))).FpSchema + 8 cnt = 0 x2 = (*THash)(unsafe.Pointer(pTbls)).Ffirst for { if !(x2 != 0) { break } pTab7 = (*THashElem)(unsafe.Pointer(x2)).Fdata /* Number of indexes on pTab */ if pObjTab != 0 && pObjTab != pTab7 { goto _41 } if (*TTable)(unsafe.Pointer(pTab7)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { cnt++ } nIdx = 0 pIdx3 = (*TTable)(unsafe.Pointer(pTab7)).FpIndex for { if !(pIdx3 != 0) { break } cnt++ goto _42 _42: ; pIdx3 = (*TIndex)(unsafe.Pointer(pIdx3)).FpNext nIdx++ } if nIdx > mxIdx { mxIdx = nIdx } goto _41 _41: ; x2 = (*THashElem)(unsafe.Pointer(x2)).Fnext } if cnt == 0 { goto _40 } if pObjTab != 0 { cnt++ } aRoot = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(4)*uint32(cnt+libc.Int32FromInt32(1)))) if aRoot == uintptr(0) { break } cnt = 0 if pObjTab != 0 { cnt++ v43 = cnt *(*int32)(unsafe.Pointer(aRoot + uintptr(v43)*4)) = 0 } x2 = (*THash)(unsafe.Pointer(pTbls)).Ffirst for { if !(x2 != 0) { break } pTab8 = (*THashElem)(unsafe.Pointer(x2)).Fdata if pObjTab != 0 && pObjTab != pTab8 { goto _44 } if (*TTable)(unsafe.Pointer(pTab8)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { cnt++ v45 = cnt *(*int32)(unsafe.Pointer(aRoot + uintptr(v45)*4)) = int32((*TTable)(unsafe.Pointer(pTab8)).Ftnum) } pIdx4 = (*TTable)(unsafe.Pointer(pTab8)).FpIndex for { if !(pIdx4 != 0) { break } cnt++ v47 = cnt *(*int32)(unsafe.Pointer(aRoot + uintptr(v47)*4)) = int32((*TIndex)(unsafe.Pointer(pIdx4)).Ftnum) goto _46 _46: ; pIdx4 = (*TIndex)(unsafe.Pointer(pIdx4)).FpNext } goto _44 _44: ; x2 = (*THashElem)(unsafe.Pointer(x2)).Fnext } *(*int32)(unsafe.Pointer(aRoot)) = cnt /* Make sure sufficient number of registers have been allocated */ _sqlite3TouchRegister(tls, pParse, int32(8)+mxIdx) _sqlite3ClearTempRegCache(tls, pParse) /* Do the b-tree integrity checks */ _sqlite3VdbeAddOp4(tls, v, int32(OP_IntegrityCk), int32(2), cnt, int32(1), aRoot, -int32(14)) _sqlite3VdbeChangeP5(tls, v, uint16(uint8(i9))) addr1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), int32(2)) _sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, _sqlite3MPrintf(tls, db, __ccgo_ts+18128, libc.VaList(bp+136, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i9)*16))).FzDbSName)), -int32(6)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(2), int32(3), int32(3)) _integrityCheckResultRow(tls, v) _sqlite3VdbeJumpHere(tls, v, addr1) /* Make sure all the indices are constructed correctly. */ x2 = (*THash)(unsafe.Pointer(pTbls)).Ffirst for { if !(x2 != 0) { break } pTab9 = (*THashElem)(unsafe.Pointer(x2)).Fdata pPrior = uintptr(0) r1 = -int32(1) /* Maximum non-virtual column number */ if pObjTab != 0 && pObjTab != pTab9 { goto _48 } if !(int32((*TTable)(unsafe.Pointer(pTab9)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) { goto _48 } if isQuick != 0 || (*TTable)(unsafe.Pointer(pTab9)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { pPk1 = uintptr(0) r2 = 0 } else { pPk1 = _sqlite3PrimaryKeyIndex(tls, pTab9) r2 = _sqlite3GetTempRange(tls, pParse, int32((*TIndex)(unsafe.Pointer(pPk1)).FnKeyCol)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Null), int32(1), r2, r2+int32((*TIndex)(unsafe.Pointer(pPk1)).FnKeyCol)-int32(1)) } _sqlite3OpenTableAndIndices(tls, pParse, pTab9, int32(OP_OpenRead), uint8(0), int32(1), uintptr(0), bp+76, bp+80) /* reg[7] counts the number of entries in the table. ** reg[8+i] counts the number of entries in the i-th index */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, int32(7)) j4 = 0 pIdx5 = (*TTable)(unsafe.Pointer(pTab9)).FpIndex for { if !(pIdx5 != 0) { break } _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, int32(8)+j4) /* index entries counter */ goto _49 _49: ; pIdx5 = (*TIndex)(unsafe.Pointer(pIdx5)).FpNext j4++ } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), *(*int32)(unsafe.Pointer(bp + 76)), 0) loopTop = _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), int32(7), int32(1)) /* Fetch the right-most column from the table. This will cause ** the entire record header to be parsed and sanity checked. It ** will also prepopulate the cursor column cache that is used ** by the OP_IsType code, so it is a required step. */ _ = libc.Int32FromInt32(0) if (*TTable)(unsafe.Pointer(pTab9)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { mxCol = -int32(1) j4 = 0 for { if !(j4 < int32((*TTable)(unsafe.Pointer(pTab9)).FnCol)) { break } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab9)).FaCol + uintptr(j4)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 { mxCol++ } goto _50 _50: ; j4++ } if mxCol == int32((*TTable)(unsafe.Pointer(pTab9)).FiPKey) { mxCol-- } } else { /* COLFLAG_VIRTUAL columns are not included in the WITHOUT ROWID ** PK index column-count, so there is no need to account for them ** in this case. */ mxCol = int32((*TIndex)(unsafe.Pointer(_sqlite3PrimaryKeyIndex(tls, pTab9))).FnColumn) - int32(1) } if mxCol >= 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), *(*int32)(unsafe.Pointer(bp + 76)), mxCol, int32(3)) _sqlite3VdbeTypeofColumn(tls, v, int32(3)) } if !(isQuick != 0) { if pPk1 != 0 { a1 = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxGT), *(*int32)(unsafe.Pointer(bp + 76)), 0, r2, int32((*TIndex)(unsafe.Pointer(pPk1)).FnKeyCol)) _sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), r2) zErr = _sqlite3MPrintf(tls, db, __ccgo_ts+18152, libc.VaList(bp+136, (*TTable)(unsafe.Pointer(pTab9)).FzName)) _sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, zErr, -int32(6)) _integrityCheckResultRow(tls, v) _sqlite3VdbeJumpHere(tls, v, a1) _sqlite3VdbeJumpHere(tls, v, a1+int32(1)) j4 = 0 for { if !(j4 < int32((*TIndex)(unsafe.Pointer(pPk1)).FnKeyCol)) { break } _sqlite3ExprCodeLoadIndexColumn(tls, pParse, pPk1, *(*int32)(unsafe.Pointer(bp + 76)), j4, r2+j4) goto _51 _51: ; j4++ } } } /* Verify datatypes for all columns: ** ** (1) NOT NULL columns may not contain a NULL ** (2) Datatype must be exact for non-ANY columns in STRICT tables ** (3) Datatype for TEXT columns in non-STRICT tables must be ** NULL, TEXT, or BLOB. ** (4) Datatype for numeric columns in non-STRICT tables must not ** be a TEXT value that can be losslessly converted to numeric. */ bStrict = libc.BoolInt32((*TTable)(unsafe.Pointer(pTab9)).FtabFlags&uint32(TF_Strict) != uint32(0)) j4 = 0 for { if !(j4 < int32((*TTable)(unsafe.Pointer(pTab9)).FnCol)) { break } pCol1 = (*TTable)(unsafe.Pointer(pTab9)).FaCol + uintptr(j4)*12 /* Check datatypes (besides NOT NULL) */ if j4 == int32((*TTable)(unsafe.Pointer(pTab9)).FiPKey) { goto _52 } if bStrict != 0 { doTypeCheck = libc.BoolInt32(int32(uint32(*(*uint8)(unsafe.Pointer(pCol1 + 4))&0xf0>>4)) > int32(COLTYPE_ANY)) } else { doTypeCheck = libc.BoolInt32(int32((*TColumn)(unsafe.Pointer(pCol1)).Faffinity) > int32(SQLITE_AFF_BLOB)) } if int32(uint32(*(*uint8)(unsafe.Pointer(pCol1 + 4))&0xf>>0)) == 0 && !(doTypeCheck != 0) { goto _52 } /* Compute the operands that will be needed for OP_IsType */ p4 = int32(SQLITE_NULL) if int32((*TColumn)(unsafe.Pointer(pCol1)).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 { _sqlite3ExprCodeGetColumnOfTable(tls, v, pTab9, *(*int32)(unsafe.Pointer(bp + 76)), j4, int32(3)) p11 = -int32(1) p3 = int32(3) } else { if (*TColumn)(unsafe.Pointer(pCol1)).FiDflt != 0 { *(*uintptr)(unsafe.Pointer(bp + 84)) = uintptr(0) _sqlite3ValueFromExpr(tls, db, _sqlite3ColumnExpr(tls, pTab9, pCol1), (*Tsqlite3)(unsafe.Pointer(db)).Fenc, (*TColumn)(unsafe.Pointer(pCol1)).Faffinity, bp+84) if *(*uintptr)(unsafe.Pointer(bp + 84)) != 0 { p4 = Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(bp + 84))) _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer(bp + 84))) } } p11 = *(*int32)(unsafe.Pointer(bp + 76)) if !((*TTable)(unsafe.Pointer(pTab9)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { p3 = int32(_sqlite3TableColumnToIndex(tls, _sqlite3PrimaryKeyIndex(tls, pTab9), int16(j4))) } else { p3 = int32(_sqlite3TableColumnToStorage(tls, pTab9, int16(j4))) } } labelError = _sqlite3VdbeMakeLabel(tls, pParse) labelOk = _sqlite3VdbeMakeLabel(tls, pParse) if int32(uint32(*(*uint8)(unsafe.Pointer(pCol1 + 4))&0xf>>0)) != 0 { jmp2 = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IsType), p11, labelOk, p3, p4) if p11 < 0 { _sqlite3VdbeChangeP5(tls, v, uint16(0x0f)) /* INT, REAL, TEXT, or BLOB */ jmp3 = jmp2 } else { _sqlite3VdbeChangeP5(tls, v, uint16(0x0d)) /* INT, TEXT, or BLOB */ /* OP_IsType does not detect NaN values in the database file ** which should be treated as a NULL. So if the header type ** is REAL, we have to load the actual data using OP_Column ** to reliably determine if the value is a NULL. */ _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), p11, p3, int32(3)) _sqlite3ColumnDefault(tls, v, pTab9, j4, int32(3)) jmp3 = _sqlite3VdbeAddOp2(tls, v, int32(OP_NotNull), int32(3), labelOk) } zErr1 = _sqlite3MPrintf(tls, db, __ccgo_ts+18188, libc.VaList(bp+136, (*TTable)(unsafe.Pointer(pTab9)).FzName, (*TColumn)(unsafe.Pointer(pCol1)).FzCnName)) _sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, zErr1, -int32(6)) if doTypeCheck != 0 { _sqlite3VdbeGoto(tls, v, labelError) _sqlite3VdbeJumpHere(tls, v, jmp2) _sqlite3VdbeJumpHere(tls, v, jmp3) } else { /* VDBE byte code will fall thru */ } } if bStrict != 0 && doTypeCheck != 0 { _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IsType), p11, labelOk, p3, p4) _ = libc.Int32FromInt32(0) _sqlite3VdbeChangeP5(tls, v, uint16(_aStdTypeMask[int32(uint32(*(*uint8)(unsafe.Pointer(pCol1 + 4))&0xf0>>4))-int32(1)])) zErr1 = _sqlite3MPrintf(tls, db, __ccgo_ts+18208, libc.VaList(bp+136, _sqlite3StdType[int32(uint32(*(*uint8)(unsafe.Pointer(pCol1 + 4))&0xf0>>4))-int32(1)], (*TTable)(unsafe.Pointer(pTab9)).FzName, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab9)).FaCol + uintptr(j4)*12))).FzCnName)) _sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, zErr1, -int32(6)) } else { if !(bStrict != 0) && int32((*TColumn)(unsafe.Pointer(pCol1)).Faffinity) == int32(SQLITE_AFF_TEXT) { /* (3) Datatype for TEXT columns in non-STRICT tables must be ** NULL, TEXT, or BLOB. */ _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IsType), p11, labelOk, p3, p4) _sqlite3VdbeChangeP5(tls, v, uint16(0x1c)) /* NULL, TEXT, or BLOB */ zErr1 = _sqlite3MPrintf(tls, db, __ccgo_ts+18230, libc.VaList(bp+136, (*TTable)(unsafe.Pointer(pTab9)).FzName, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab9)).FaCol + uintptr(j4)*12))).FzCnName)) _sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, zErr1, -int32(6)) } else { if !(bStrict != 0) && int32((*TColumn)(unsafe.Pointer(pCol1)).Faffinity) >= int32(SQLITE_AFF_NUMERIC) { /* (4) Datatype for numeric columns in non-STRICT tables must not ** be a TEXT value that can be converted to numeric. */ _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IsType), p11, labelOk, p3, p4) _sqlite3VdbeChangeP5(tls, v, uint16(0x1b)) /* NULL, INT, FLOAT, or BLOB */ if p11 >= 0 { _sqlite3ExprCodeGetColumnOfTable(tls, v, pTab9, *(*int32)(unsafe.Pointer(bp + 76)), j4, int32(3)) } _sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), int32(3), int32(1), 0, __ccgo_ts+18253, -int32(1)) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IsType), -int32(1), labelOk, int32(3), p4) _sqlite3VdbeChangeP5(tls, v, uint16(0x1c)) /* NULL, TEXT, or BLOB */ zErr1 = _sqlite3MPrintf(tls, db, __ccgo_ts+18255, libc.VaList(bp+136, (*TTable)(unsafe.Pointer(pTab9)).FzName, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab9)).FaCol + uintptr(j4)*12))).FzCnName)) _sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, zErr1, -int32(6)) } } } _sqlite3VdbeResolveLabel(tls, v, labelError) _integrityCheckResultRow(tls, v) _sqlite3VdbeResolveLabel(tls, v, labelOk) goto _52 _52: ; j4++ } /* Verify CHECK constraints */ if (*TTable)(unsafe.Pointer(pTab9)).FpCheck != 0 && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_IgnoreChecks) == uint64(0) { pCheck = _sqlite3ExprListDup(tls, db, (*TTable)(unsafe.Pointer(pTab9)).FpCheck, 0) if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 { addrCkFault = _sqlite3VdbeMakeLabel(tls, pParse) addrCkOk = _sqlite3VdbeMakeLabel(tls, pParse) (*TParse)(unsafe.Pointer(pParse)).FiSelfTab = *(*int32)(unsafe.Pointer(bp + 76)) + int32(1) k3 = (*TExprList)(unsafe.Pointer(pCheck)).FnExpr - int32(1) for { if !(k3 > 0) { break } _sqlite3ExprIfFalse(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pCheck + 8 + uintptr(k3)*20))).FpExpr, addrCkFault, 0) goto _53 _53: ; k3-- } _sqlite3ExprIfTrue(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pCheck + 8))).FpExpr, addrCkOk, int32(SQLITE_JUMPIFNULL)) _sqlite3VdbeResolveLabel(tls, v, addrCkFault) (*TParse)(unsafe.Pointer(pParse)).FiSelfTab = 0 zErr2 = _sqlite3MPrintf(tls, db, __ccgo_ts+18275, libc.VaList(bp+136, (*TTable)(unsafe.Pointer(pTab9)).FzName)) _sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, zErr2, -int32(6)) _integrityCheckResultRow(tls, v) _sqlite3VdbeResolveLabel(tls, v, addrCkOk) } _sqlite3ExprListDelete(tls, db, pCheck) } if !(isQuick != 0) { /* Omit the remaining tests for quick_check */ /* Validate index entries for the current row */ j4 = 0 pIdx5 = (*TTable)(unsafe.Pointer(pTab9)).FpIndex for { if !(pIdx5 != 0) { break } ckUniq = _sqlite3VdbeMakeLabel(tls, pParse) if pPk1 == pIdx5 { goto _54 } r1 = _sqlite3GenerateIndexKey(tls, pParse, pIdx5, *(*int32)(unsafe.Pointer(bp + 76)), 0, 0, bp+88, pPrior, r1) pPrior = pIdx5 _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), int32(8)+j4, int32(1)) /* increment entry count */ /* Verify that an index entry exists for the current table row */ jmp21 = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), *(*int32)(unsafe.Pointer(bp + 80))+j4, ckUniq, r1, int32((*TIndex)(unsafe.Pointer(pIdx5)).FnColumn)) _sqlite3VdbeLoadString(tls, v, int32(3), __ccgo_ts+18305) _sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(7), int32(3), int32(3)) _sqlite3VdbeLoadString(tls, v, int32(4), __ccgo_ts+18310) _sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(4), int32(3), int32(3)) jmp5 = _sqlite3VdbeLoadString(tls, v, int32(4), (*TIndex)(unsafe.Pointer(pIdx5)).FzName) _sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(4), int32(3), int32(3)) jmp4 = _integrityCheckResultRow(tls, v) _sqlite3VdbeJumpHere(tls, v, jmp21) /* The OP_IdxRowid opcode is an optimized version of OP_Column ** that extracts the rowid off the end of the index record. ** But it only works correctly if index record does not have ** any extra bytes at the end. Verify that this is the case. */ if (*TTable)(unsafe.Pointer(pTab9)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { _sqlite3VdbeAddOp2(tls, v, int32(OP_IdxRowid), *(*int32)(unsafe.Pointer(bp + 80))+j4, int32(3)) jmp7 = _sqlite3VdbeAddOp3(tls, v, int32(OP_Eq), int32(3), 0, r1+int32((*TIndex)(unsafe.Pointer(pIdx5)).FnColumn)-int32(1)) _sqlite3VdbeLoadString(tls, v, int32(3), __ccgo_ts+18331) _sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(7), int32(3), int32(3)) _sqlite3VdbeLoadString(tls, v, int32(4), __ccgo_ts+18367) _sqlite3VdbeGoto(tls, v, jmp5-int32(1)) _sqlite3VdbeJumpHere(tls, v, jmp7) } /* Any indexed columns with non-BINARY collations must still hold ** the exact same text value as the table. */ label6 = 0 kk = 0 for { if !(kk < int32((*TIndex)(unsafe.Pointer(pIdx5)).FnKeyCol)) { break } if *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx5)).FazColl + uintptr(kk)*4)) == uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) { goto _55 } if label6 == 0 { label6 = _sqlite3VdbeMakeLabel(tls, pParse) } _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), *(*int32)(unsafe.Pointer(bp + 80))+j4, kk, int32(3)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Ne), int32(3), label6, r1+kk) goto _55 _55: ; kk++ } if label6 != 0 { jmp6 = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto)) _sqlite3VdbeResolveLabel(tls, v, label6) _sqlite3VdbeLoadString(tls, v, int32(3), __ccgo_ts+18305) _sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(7), int32(3), int32(3)) _sqlite3VdbeLoadString(tls, v, int32(4), __ccgo_ts+18378) _sqlite3VdbeGoto(tls, v, jmp5-int32(1)) _sqlite3VdbeJumpHere(tls, v, jmp6) } /* For UNIQUE indexes, verify that only one entry exists with the ** current key. The entry is unique if (1) any column is NULL ** or (2) the next entry has a different key */ if int32((*TIndex)(unsafe.Pointer(pIdx5)).FonError) != OE_None { uniqOk = _sqlite3VdbeMakeLabel(tls, pParse) kk = 0 for { if !(kk < int32((*TIndex)(unsafe.Pointer(pIdx5)).FnKeyCol)) { break } iCol1 = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx5)).FaiColumn + uintptr(kk)*2))) _ = libc.Int32FromInt32(0) if iCol1 >= 0 && int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab9)).FaCol + uintptr(iCol1)*12 + 4))&0xf>>0)) != 0 { goto _56 } _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), r1+kk, uniqOk) goto _56 _56: ; kk++ } jmp61 = _sqlite3VdbeAddOp1(tls, v, int32(OP_Next), *(*int32)(unsafe.Pointer(bp + 80))+j4) _sqlite3VdbeGoto(tls, v, uniqOk) _sqlite3VdbeJumpHere(tls, v, jmp61) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxGT), *(*int32)(unsafe.Pointer(bp + 80))+j4, uniqOk, r1, int32((*TIndex)(unsafe.Pointer(pIdx5)).FnKeyCol)) _sqlite3VdbeLoadString(tls, v, int32(3), __ccgo_ts+18405) _sqlite3VdbeGoto(tls, v, jmp5) _sqlite3VdbeResolveLabel(tls, v, uniqOk) } _sqlite3VdbeJumpHere(tls, v, jmp4) _sqlite3ResolvePartIdxLabel(tls, pParse, *(*int32)(unsafe.Pointer(bp + 88))) goto _54 _54: ; pIdx5 = (*TIndex)(unsafe.Pointer(pIdx5)).FpNext j4++ } } _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), *(*int32)(unsafe.Pointer(bp + 76)), loopTop) _sqlite3VdbeJumpHere(tls, v, loopTop-int32(1)) if !(isQuick != 0) { _sqlite3VdbeLoadString(tls, v, int32(2), __ccgo_ts+18432) j4 = 0 pIdx5 = (*TTable)(unsafe.Pointer(pTab9)).FpIndex for { if !(pIdx5 != 0) { break } if pPk1 == pIdx5 { goto _57 } _sqlite3VdbeAddOp2(tls, v, int32(OP_Count), *(*int32)(unsafe.Pointer(bp + 80))+j4, int32(3)) addr1 = _sqlite3VdbeAddOp3(tls, v, int32(OP_Eq), int32(8)+j4, 0, int32(3)) _sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_NOTNULL)) _sqlite3VdbeLoadString(tls, v, int32(4), (*TIndex)(unsafe.Pointer(pIdx5)).FzName) _sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(4), int32(2), int32(3)) _integrityCheckResultRow(tls, v) _sqlite3VdbeJumpHere(tls, v, addr1) goto _57 _57: ; pIdx5 = (*TIndex)(unsafe.Pointer(pIdx5)).FpNext j4++ } if pPk1 != 0 { _sqlite3ReleaseTempRange(tls, pParse, r2, int32((*TIndex)(unsafe.Pointer(pPk1)).FnKeyCol)) } } goto _48 _48: ; x2 = (*THashElem)(unsafe.Pointer(x2)).Fnext } /* Second pass to invoke the xIntegrity method on all virtual ** tables. */ x2 = (*THash)(unsafe.Pointer(pTbls)).Ffirst for { if !(x2 != 0) { break } pTab10 = (*THashElem)(unsafe.Pointer(x2)).Fdata if pObjTab != 0 && pObjTab != pTab10 { goto _58 } if int32((*TTable)(unsafe.Pointer(pTab10)).FeTabType) == TABTYP_NORM { goto _58 } if !(int32((*TTable)(unsafe.Pointer(pTab10)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) { goto _58 } if int32((*TTable)(unsafe.Pointer(pTab10)).FnCol) <= 0 { zMod = *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab10 + 44))).FazArg)) if _sqlite3HashFind(tls, db+404, zMod) == uintptr(0) { goto _58 } } _sqlite3ViewGetColumnNames(tls, pParse, pTab10) if (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab10 + 44))).Fp == uintptr(0) { goto _58 } pVTab = (*TVTable)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab10 + 44))).Fp)).FpVtab if pVTab == uintptr(0) { goto _58 } if (*Tsqlite3_vtab)(unsafe.Pointer(pVTab)).FpModule == uintptr(0) { goto _58 } if (*Tsqlite3_module)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer(pVTab)).FpModule)).FiVersion < int32(4) { goto _58 } if (*Tsqlite3_module)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer(pVTab)).FpModule)).FxIntegrity == uintptr(0) { goto _58 } _sqlite3VdbeAddOp3(tls, v, int32(OP_VCheck), i9, int32(3), isQuick) (*TTable)(unsafe.Pointer(pTab10)).FnTabRef++ _sqlite3VdbeAppendP4(tls, v, pTab10, -int32(16)) a11 = _sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), int32(3)) _integrityCheckResultRow(tls, v) _sqlite3VdbeJumpHere(tls, v, a11) goto _58 goto _58 _58: ; x2 = (*THashElem)(unsafe.Pointer(x2)).Fnext } goto _40 _40: ; i9++ } aOp2 = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(28)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_endCode)), _iLn21) if aOp2 != 0 { (*(*TVdbeOp)(unsafe.Pointer(aOp2))).Fp2 = int32(1) - *(*int32)(unsafe.Pointer(bp + 72)) (*(*TVdbeOp)(unsafe.Pointer(aOp2 + 2*20))).Fp4type = int8(-libc.Int32FromInt32(1)) *(*uintptr)(unsafe.Pointer(aOp2 + 2*20 + 16)) = __ccgo_ts + 18461 (*(*TVdbeOp)(unsafe.Pointer(aOp2 + 5*20))).Fp4type = int8(-libc.Int32FromInt32(1)) *(*uintptr)(unsafe.Pointer(aOp2 + 5*20 + 16)) = _sqlite3ErrStr(tls, int32(SQLITE_CORRUPT)) } _sqlite3VdbeChangeP3(tls, v, 0, _sqlite3VdbeCurrentAddr(tls, v)-int32(2)) break /* ** PRAGMA encoding ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be" ** ** In its first form, this pragma returns the encoding of the main ** database. If the database is not initialized, it is initialized now. ** ** The second form of this pragma is a no-op if the main database file ** has not already been initialized. In this case it sets the default ** encoding that will be used for the main database file if a new file ** is created. If an existing main database file is opened, then the ** default text encoding for the existing database is used. ** ** In all cases new databases created using the ATTACH command are ** created to use the same default text encoding as the main database. If ** the main database has not been initialized and/or created when ATTACH ** is executed, this is done before the ATTACH operation. ** ** In the second form this pragma sets the text encoding to be used in ** new database files created using this database handle. It is only ** useful if invoked immediately after the main database i */ fallthrough case int32(PragTyp_ENCODING): if !(zRight != 0) { /* "PRAGMA encoding" */ if _sqlite3ReadSchema(tls, pParse) != 0 { goto pragma_out } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _returnSingleText(tls, v, _encnames1[(*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fenc].FzName) } else { /* "PRAGMA encoding = XXX" */ /* Only change the value of sqlite.enc if the database handle is not ** initialized. If the main database exists, the new sqlite.enc value ** will be overwritten when the schema is next loaded. If it does not ** already exists, it will be created to use the new encoding value. */ if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_EncodingFixed) == uint32(0) { pEnc = uintptr(unsafe.Pointer(&_encnames1)) for { if !((*struct { FzName uintptr Fenc Tu8 })(unsafe.Pointer(pEnc)).FzName != 0) { break } if 0 == _sqlite3StrICmp(tls, zRight, (*struct { FzName uintptr Fenc Tu8 })(unsafe.Pointer(pEnc)).FzName) { if (*struct { FzName uintptr Fenc Tu8 })(unsafe.Pointer(pEnc)).Fenc != 0 { v60 = int32((*struct { FzName uintptr Fenc Tu8 })(unsafe.Pointer(pEnc)).Fenc) } else { v60 = int32(SQLITE_UTF16LE) } enc = uint8(v60) (*TSchema)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema)).Fenc = enc _sqlite3SetTextEncoding(tls, db, enc) break } goto _59 _59: ; pEnc += 8 } if !((*struct { FzName uintptr Fenc Tu8 })(unsafe.Pointer(pEnc)).FzName != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+18522, libc.VaList(bp+136, zRight)) } } } break /* ** PRAGMA [schema.]schema_version ** PRAGMA [schema.]schema_version = ** ** PRAGMA [schema.]user_version ** PRAGMA [schema.]user_version = ** ** PRAGMA [schema.]freelist_count ** ** PRAGMA [schema.]data_version ** ** PRAGMA [schema.]application_id ** PRAGMA [schema.]application_id = ** ** The pragma's schema_version and user_version are used to set or get ** the value of the schema-version and user-version, respectively. Both ** the schema-version and the user-version are 32-bit signed integers ** stored in the database header. ** ** The schema-cookie is usually only manipulated internally by SQLite. It ** is incremented by SQLite whenever the database schema is modified (by ** creating or dropping a table or index). The schema version is used by ** SQLite each time a query is executed to ensure that the internal cache ** of the schema used when compiling the SQL query matches the schema of ** the database against which the compiled query is actually executed. ** Subverting this mechanism by using "PRAGMA schema_version" to modify ** the schema-version is potentially dangerous and may lead to program ** crashes or database corruption. Use with caution! ** ** The user-version is not used internally by SQLite. It may be used by ** applications for any purpose. */ fallthrough case int32(PragTyp_HEADER_VALUE): iCookie = int32((*TPragmaName)(unsafe.Pointer(pPragma)).FiArg) /* Which cookie to read or write */ _sqlite3VdbeUsesBtree(tls, v, iDb) if zRight != 0 && int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&int32(PragFlg_ReadOnly) == 0 { aOp3 = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(8)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_setCookie)), 0) if 0 != 0 { break } (*(*TVdbeOp)(unsafe.Pointer(aOp3))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp3 + 1*20))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp3 + 1*20))).Fp2 = iCookie (*(*TVdbeOp)(unsafe.Pointer(aOp3 + 1*20))).Fp3 = _sqlite3Atoi(tls, zRight) (*(*TVdbeOp)(unsafe.Pointer(aOp3 + 1*20))).Fp5 = uint16(1) if iCookie == int32(BTREE_SCHEMA_VERSION) && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_Defensive) != uint64(0) { /* Do not allow the use of PRAGMA schema_version=VALUE in defensive ** mode. Change the OP_SetCookie opcode into a no-op. */ (*(*TVdbeOp)(unsafe.Pointer(aOp3 + 1*20))).Fopcode = uint8(OP_Noop) } } else { aOp4 = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(12)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_readCookie)), 0) if 0 != 0 { break } (*(*TVdbeOp)(unsafe.Pointer(aOp4))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp4 + 1*20))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp4 + 1*20))).Fp3 = iCookie _sqlite3VdbeReusable(tls, v) } break /* ** PRAGMA compile_options ** ** Return the names of all compile-time options used in this build, ** one option per row. */ fallthrough case int32(PragTyp_COMPILE_OPTIONS): i10 = 0 (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(1) for { v62 = i10 i10++ v61 = Xsqlite3_compileoption_get(tls, v62) zOpt = v61 if !(v61 != uintptr(0)) { break } _sqlite3VdbeLoadString(tls, v, int32(1), zOpt) _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), int32(1), int32(1)) } _sqlite3VdbeReusable(tls, v) break /* ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate ** ** Checkpoint the database. */ fallthrough case int32(PragTyp_WAL_CHECKPOINT): if (*TToken)(unsafe.Pointer(pId2)).Fz != 0 { v63 = iDb } else { v63 = libc.Int32FromInt32(SQLITE_MAX_ATTACHED) + libc.Int32FromInt32(2) } iBt = v63 eMode2 = SQLITE_CHECKPOINT_PASSIVE if zRight != 0 { if _sqlite3StrICmp(tls, zRight, __ccgo_ts+17747) == 0 { eMode2 = int32(SQLITE_CHECKPOINT_FULL) } else { if _sqlite3StrICmp(tls, zRight, __ccgo_ts+18547) == 0 { eMode2 = int32(SQLITE_CHECKPOINT_RESTART) } else { if _sqlite3StrICmp(tls, zRight, __ccgo_ts+17900) == 0 { eMode2 = int32(SQLITE_CHECKPOINT_TRUNCATE) } } } } (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(3) _sqlite3VdbeAddOp3(tls, v, int32(OP_Checkpoint), iBt, eMode2, int32(1)) _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), int32(1), int32(3)) break /* ** PRAGMA wal_autocheckpoint ** PRAGMA wal_autocheckpoint = N ** ** Configure a database connection to automatically checkpoint a database ** after accumulating N frames in the log. Or query for the current value ** of N. */ fallthrough case int32(PragTyp_WAL_AUTOCHECKPOINT): if zRight != 0 { Xsqlite3_wal_autocheckpoint(tls, db, _sqlite3Atoi(tls, zRight)) } if (*Tsqlite3)(unsafe.Pointer(db)).FxWalCallback == __ccgo_fp(_sqlite3WalDefaultHook) { v64 = int32((*Tsqlite3)(unsafe.Pointer(db)).FpWalArg) } else { v64 = 0 } _returnSingleInt(tls, v, int64(v64)) break /* ** PRAGMA shrink_memory ** ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database ** connection on which it is invoked to free up as much memory as it ** can, by calling sqlite3_db_release_memory(). */ fallthrough case int32(PragTyp_SHRINK_MEMORY): Xsqlite3_db_release_memory(tls, db) break /* ** PRAGMA optimize ** PRAGMA optimize(MASK) ** PRAGMA schema.optimize ** PRAGMA schema.optimize(MASK) ** ** Attempt to optimize the database. All schemas are optimized in the first ** two forms, and only the specified schema is optimized in the latter two. ** ** The details of optimizations performed by this pragma are expected ** to change and improve over time. Applications should anticipate that ** this pragma will perform new optimizations in future releases. ** ** The optional argument is a bitmask of optimizations to perform: ** ** 0x0001 Debugging mode. Do not actually perform any optimizations ** but instead return one line of text for each optimization ** that would have been done. Off by default. ** ** 0x0002 Run ANALYZE on tables that might benefit. On by default. ** See below for additional information. ** ** 0x0004 (Not yet implemented) Record usage and performance ** information from the current session in the ** database file so that it will be available to "optimize" ** pragmas run by future database connections. ** ** 0x0008 (Not yet implemented) Create indexes that might have ** been helpful to recent queries ** ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all ** of the optimizations listed above except Debug Mode, including new ** optimizations that have not yet been invented. If new optimizations are ** ever added that should be off by default, those off-by-default ** optimizations will have bitmasks of 0x10000 or larger. ** ** DETERMINATION OF WHEN TO RUN ANALYZE ** ** In the current implementation, a table is analyzed if only if all of ** the following are true: ** ** (1) MASK bit 0x02 is set. ** ** (2) The query planner used sqlite_stat1-style statistics for one or ** more indexes of the table at some point during the lifetime of ** the current connection. ** ** (3) One or more indexes of the table are currently unanalyzed OR ** the number of rows in the table has increased by 25 times or more ** since the last time ANALYZE was run. ** ** The rules for when tables are analyzed are likely to change in ** future releases. */ fallthrough case int32(PragTyp_OPTIMIZE): /* Mask of operations to perform */ if zRight != 0 { opMask = uint32(_sqlite3Atoi(tls, zRight)) if opMask&uint32(0x02) == uint32(0) { break } } else { opMask = uint32(0xfffe) } v66 = pParse + 40 v65 = *(*int32)(unsafe.Pointer(v66)) *(*int32)(unsafe.Pointer(v66))++ iTabCur = v65 if zDb != 0 { v68 = iDb } else { v68 = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1) } iDbLast = v68 for { if !(iDb <= iDbLast) { break } if iDb == int32(1) { goto _67 } _sqlite3CodeVerifySchema(tls, pParse, iDb) pSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema k4 = (*THash)(unsafe.Pointer(pSchema + 8)).Ffirst for { if !(k4 != 0) { break } pTab11 = (*THashElem)(unsafe.Pointer(k4)).Fdata /* If table pTab has not been used in a way that would benefit from ** having analysis statistics during the current session, then skip it. ** This also has the effect of skipping virtual tables and views */ if (*TTable)(unsafe.Pointer(pTab11)).FtabFlags&uint32(TF_StatsUsed) == uint32(0) { goto _69 } /* Reanalyze if the table is 25 times larger than the last analysis */ szThreshold = int16(int32((*TTable)(unsafe.Pointer(pTab11)).FnRowLogEst) + int32(46)) _ = libc.Int32FromInt32(0) pIdx6 = (*TTable)(unsafe.Pointer(pTab11)).FpIndex for { if !(pIdx6 != 0) { break } if !(int32(uint32(*(*uint16)(unsafe.Pointer(pIdx6 + 56))&0x80>>7)) != 0) { szThreshold = 0 /* Always analyze if any index lacks statistics */ break } goto _70 _70: ; pIdx6 = (*TIndex)(unsafe.Pointer(pIdx6)).FpNext } if szThreshold != 0 { _sqlite3OpenTable(tls, pParse, iTabCur, iDb, pTab11, int32(OP_OpenRead)) _sqlite3VdbeAddOp3(tls, v, int32(OP_IfSmaller), iTabCur, int32(uint32(_sqlite3VdbeCurrentAddr(tls, v)+int32(2))+opMask&uint32(1)), int32(szThreshold)) } zSubSql = _sqlite3MPrintf(tls, db, __ccgo_ts+18555, libc.VaList(bp+136, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, (*TTable)(unsafe.Pointer(pTab11)).FzName)) if opMask&uint32(0x01) != 0 { r11 = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, r11, 0, zSubSql, -int32(6)) _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), r11, int32(1)) } else { _sqlite3VdbeAddOp4(tls, v, int32(OP_SqlExec), 0, 0, 0, zSubSql, -int32(6)) } goto _69 _69: ; k4 = (*THashElem)(unsafe.Pointer(k4)).Fnext } goto _67 _67: ; iDb++ } _sqlite3VdbeAddOp0(tls, v, int32(OP_Expire)) break /* ** PRAGMA busy_timeout ** PRAGMA busy_timeout = N ** ** Call sqlite3_busy_timeout(db, N). Return the current timeout value ** if one is set. If no busy handler or a different busy handler is set ** then 0 is returned. Setting the busy_timeout to 0 or negative ** disables the timeout. */ /*case PragTyp_BUSY_TIMEOUT*/ fallthrough default: _ = libc.Int32FromInt32(0) if zRight != 0 { Xsqlite3_busy_timeout(tls, db, _sqlite3Atoi(tls, zRight)) } _returnSingleInt(tls, v, int64((*Tsqlite3)(unsafe.Pointer(db)).FbusyTimeout)) break /* ** PRAGMA soft_heap_limit ** PRAGMA soft_heap_limit = N ** ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the ** sqlite3_soft_heap_limit64() interface with the argument N, if N is ** specified and is a non-negative integer. ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always ** returns the same integer that would be returned by the ** sqlite3_soft_heap_limit64(-1) C-language function. */ fallthrough case int32(PragTyp_SOFT_HEAP_LIMIT): if zRight != 0 && _sqlite3DecOrHexToI64(tls, zRight, bp+96) == SQLITE_OK { Xsqlite3_soft_heap_limit64(tls, *(*Tsqlite3_int64)(unsafe.Pointer(bp + 96))) } _returnSingleInt(tls, v, Xsqlite3_soft_heap_limit64(tls, int64(-int32(1)))) break /* ** PRAGMA hard_heap_limit ** PRAGMA hard_heap_limit = N ** ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap ** limit. The hard heap limit can be activated or lowered by this ** pragma, but not raised or deactivated. Only the ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate ** the hard heap limit. This allows an application to set a heap limit ** constraint that cannot be relaxed by an untrusted SQL script. */ fallthrough case int32(PragTyp_HARD_HEAP_LIMIT): if zRight != 0 && _sqlite3DecOrHexToI64(tls, zRight, bp+104) == SQLITE_OK { iPrior = Xsqlite3_hard_heap_limit64(tls, int64(-int32(1))) if *(*Tsqlite3_int64)(unsafe.Pointer(bp + 104)) > 0 && (iPrior == 0 || iPrior > *(*Tsqlite3_int64)(unsafe.Pointer(bp + 104))) { Xsqlite3_hard_heap_limit64(tls, *(*Tsqlite3_int64)(unsafe.Pointer(bp + 104))) } } _returnSingleInt(tls, v, Xsqlite3_hard_heap_limit64(tls, int64(-int32(1)))) break /* ** PRAGMA threads ** PRAGMA threads = N ** ** Configure the maximum number of worker threads. Return the new ** maximum, which might be less than requested. */ fallthrough case int32(PragTyp_THREADS): if zRight != 0 && _sqlite3DecOrHexToI64(tls, zRight, bp+112) == SQLITE_OK && *(*Tsqlite3_int64)(unsafe.Pointer(bp + 112)) >= 0 { Xsqlite3_limit(tls, db, int32(SQLITE_LIMIT_WORKER_THREADS), int32(*(*Tsqlite3_int64)(unsafe.Pointer(bp + 112))&libc.Int64FromInt32(0x7fffffff))) } _returnSingleInt(tls, v, int64(Xsqlite3_limit(tls, db, int32(SQLITE_LIMIT_WORKER_THREADS), -int32(1)))) break /* ** PRAGMA analysis_limit ** PRAGMA analysis_limit = N ** ** Configure the maximum number of rows that ANALYZE will examine ** in each index that it looks at. Return the new limit. */ fallthrough case int32(PragTyp_ANALYSIS_LIMIT): if zRight != 0 && _sqlite3DecOrHexToI64(tls, zRight, bp+120) == SQLITE_OK && *(*Tsqlite3_int64)(unsafe.Pointer(bp + 120)) >= 0 { (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit = int32(*(*Tsqlite3_int64)(unsafe.Pointer(bp + 120)) & libc.Int64FromInt32(0x7fffffff)) } _returnSingleInt(tls, v, int64((*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit)) /* IMP: R-57594-65522 */ break } /* End of the PRAGMA switch */ /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only ** purpose is to execute assert() statements to verify that if the ** PragFlg_NoColumns1 flag is set and the caller specified an argument ** to the PRAGMA, the implementation has not added any OP_ResultRow ** instructions to the VM. */ if int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&int32(PragFlg_NoColumns1) != 0 && zRight != 0 { } goto pragma_out pragma_out: ; _sqlite3DbFree(tls, db, zLeft) _sqlite3DbFree(tls, db, zRight) } var _iLn3 int32 var _getCacheSize = [9]TVdbeOpList{ 0: { Fopcode: uint8(OP_Transaction), }, 1: { Fopcode: uint8(OP_ReadCookie), Fp2: int8(1), Fp3: int8(BTREE_DEFAULT_CACHE_SIZE), }, 2: { Fopcode: uint8(OP_IfPos), Fp1: int8(1), Fp2: int8(8), }, 3: { Fopcode: uint8(OP_Integer), Fp2: int8(2), }, 4: { Fopcode: uint8(OP_Subtract), Fp1: int8(1), Fp2: int8(2), Fp3: int8(1), }, 5: { Fopcode: uint8(OP_IfPos), Fp1: int8(1), Fp2: int8(8), }, 6: { Fopcode: uint8(OP_Integer), Fp2: int8(1), }, 7: { Fopcode: uint8(OP_Noop), }, 8: { Fopcode: uint8(OP_ResultRow), Fp1: int8(1), Fp2: int8(1), }, } /* When setting the auto_vacuum mode to either "full" or ** "incremental", write the value of meta[6] in the database ** file. Before writing to meta[6], check that meta[3] indicates ** that this really is an auto-vacuum capable database. */ var _iLn11 int32 var _setMeta6 = [5]TVdbeOpList{ 0: { Fopcode: uint8(OP_Transaction), Fp2: int8(1), }, 1: { Fopcode: uint8(OP_ReadCookie), Fp2: int8(1), Fp3: int8(BTREE_LARGEST_ROOT_PAGE), }, 2: { Fopcode: uint8(OP_If), Fp1: int8(1), }, 3: { Fopcode: uint8(OP_Halt), Fp2: int8(OE_Abort), }, 4: { Fopcode: uint8(OP_SetCookie), Fp2: int8(BTREE_INCR_VACUUM), }, } /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/ var _aStdTypeMask = [6]uint8{ 0: uint8(0x1f), 1: uint8(0x18), 2: uint8(0x11), 3: uint8(0x11), 4: uint8(0x13), 5: uint8(0x14), } var _iLn21 int32 var _endCode = [7]TVdbeOpList{ 0: { Fopcode: uint8(OP_AddImm), Fp1: int8(1), }, 1: { Fopcode: uint8(OP_IfNotZero), Fp1: int8(1), Fp2: int8(4), }, 2: { Fopcode: uint8(OP_String8), Fp2: int8(3), }, 3: { Fopcode: uint8(OP_ResultRow), Fp1: int8(3), Fp2: int8(1), }, 4: { Fopcode: uint8(OP_Halt), }, 5: { Fopcode: uint8(OP_String8), Fp2: int8(3), }, 6: { Fopcode: uint8(OP_Goto), Fp2: int8(3), }, } var _encnames1 = [9]struct { FzName uintptr Fenc Tu8 }{ 0: { FzName: __ccgo_ts + 18464, Fenc: uint8(SQLITE_UTF8), }, 1: { FzName: __ccgo_ts + 18469, Fenc: uint8(SQLITE_UTF8), }, 2: { FzName: __ccgo_ts + 18475, Fenc: uint8(SQLITE_UTF16LE), }, 3: { FzName: __ccgo_ts + 18484, Fenc: uint8(SQLITE_UTF16BE), }, 4: { FzName: __ccgo_ts + 18493, Fenc: uint8(SQLITE_UTF16LE), }, 5: { FzName: __ccgo_ts + 18501, Fenc: uint8(SQLITE_UTF16BE), }, 6: { FzName: __ccgo_ts + 18509, }, 7: { FzName: __ccgo_ts + 18516, }, 8: {}, } /* Write the specified cookie value */ var _setCookie = [2]TVdbeOpList{ 0: { Fopcode: uint8(OP_Transaction), Fp2: int8(1), }, 1: { Fopcode: uint8(OP_SetCookie), }, } /* Read the specified cookie value */ var _readCookie = [3]TVdbeOpList{ 0: { Fopcode: uint8(OP_Transaction), }, 1: { Fopcode: uint8(OP_ReadCookie), Fp2: int8(1), }, 2: { Fopcode: uint8(OP_ResultRow), Fp1: int8(1), Fp2: int8(1), }, } // C documentation // // /***************************************************************************** // ** Implementation of an eponymous virtual table that runs a pragma. // ** // */ type TPragmaVtab = struct { Fbase Tsqlite3_vtab Fdb uintptr FpName uintptr FnHidden Tu8 FiHidden Tu8 } type PragmaVtab = TPragmaVtab type TPragmaVtabCursor = struct { F__ccgo_align [0]uint32 Fbase Tsqlite3_vtab_cursor FpPragma uintptr FiRowid Tsqlite_int64 FazArg [2]uintptr } type PragmaVtabCursor = TPragmaVtabCursor type TPragmaVtab1 = struct { Fbase Tsqlite3_vtab Fdb uintptr FpName uintptr FnHidden Tu8 FiHidden Tu8 } type PragmaVtab1 = TPragmaVtab1 type TPragmaVtabCursor1 = struct { F__ccgo_align [0]uint32 Fbase Tsqlite3_vtab_cursor FpPragma uintptr FiRowid Tsqlite_int64 FazArg [2]uintptr } type PragmaVtabCursor1 = TPragmaVtabCursor1 // C documentation // // /* // ** Pragma virtual table module xConnect method. // */ func _pragmaVtabConnect(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) { bp := tls.Alloc(256) defer tls.Free(256) var cSep uint8 var i, j, rc int32 var pPragma, pTab uintptr var _ /* acc at bp+0 */ TStrAccum var _ /* zBuf at bp+24 */ [200]uint8 _, _, _, _, _, _ = cSep, i, j, pPragma, pTab, rc pPragma = pAux pTab = uintptr(0) cSep = uint8('(') _ = argc _ = argv _sqlite3StrAccumInit(tls, bp, uintptr(0), bp+24, int32(200), 0) Xsqlite3_str_appendall(tls, bp, __ccgo_ts+18573) i = 0 j = int32((*TPragmaName)(unsafe.Pointer(pPragma)).FiPragCName) for { if !(i < int32((*TPragmaName)(unsafe.Pointer(pPragma)).FnPragCName)) { break } Xsqlite3_str_appendf(tls, bp, __ccgo_ts+18588, libc.VaList(bp+232, int32(cSep), _pragCName[j])) cSep = uint8(',') goto _1 _1: ; i++ j++ } if i == 0 { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+18595, libc.VaList(bp+232, (*TPragmaName)(unsafe.Pointer(pPragma)).FzName)) i++ } j = 0 if int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&int32(PragFlg_Result1) != 0 { Xsqlite3_str_appendall(tls, bp, __ccgo_ts+18601) j++ } if int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&(libc.Int32FromInt32(PragFlg_SchemaOpt)|libc.Int32FromInt32(PragFlg_SchemaReq)) != 0 { Xsqlite3_str_appendall(tls, bp, __ccgo_ts+18613) j++ } Xsqlite3_str_append(tls, bp, __ccgo_ts+5106, int32(1)) _sqlite3StrAccumFinish(tls, bp) _ = libc.Int32FromInt32(0) rc = Xsqlite3_declare_vtab(tls, db, bp+24) if rc == SQLITE_OK { pTab = Xsqlite3_malloc(tls, int32(24)) if pTab == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, pTab, 0, uint32(24)) (*TPragmaVtab)(unsafe.Pointer(pTab)).FpName = pPragma (*TPragmaVtab)(unsafe.Pointer(pTab)).Fdb = db (*TPragmaVtab)(unsafe.Pointer(pTab)).FiHidden = uint8(i) (*TPragmaVtab)(unsafe.Pointer(pTab)).FnHidden = uint8(j) } } else { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+232, Xsqlite3_errmsg(tls, db))) } *(*uintptr)(unsafe.Pointer(ppVtab)) = pTab return rc } // C documentation // // /* // ** Pragma virtual table module xDisconnect method. // */ func _pragmaVtabDisconnect(tls *libc.TLS, pVtab uintptr) (r int32) { var pTab uintptr _ = pTab pTab = pVtab Xsqlite3_free(tls, pTab) return SQLITE_OK } // C documentation // // /* Figure out the best index to use to search a pragma virtual table. // ** // ** There are not really any index choices. But we want to encourage the // ** query planner to give == constraints on as many hidden parameters as // ** possible, and especially on the first hidden parameter. So return a // ** high cost if hidden parameters are unconstrained. // */ func _pragmaVtabBestIndex(tls *libc.TLS, tab uintptr, pIdxInfo uintptr) (r int32) { var i, j int32 var pConstraint, pTab uintptr var seen [2]int32 _, _, _, _, _ = i, j, pConstraint, pTab, seen pTab = tab (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = libc.Float64FromInt32(1) if int32((*TPragmaVtab)(unsafe.Pointer(pTab)).FnHidden) == 0 { return SQLITE_OK } pConstraint = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint seen[0] = 0 seen[int32(1)] = 0 i = 0 for { if !(i < (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint) { break } if int32((*Tsqlite3_index_constraint)(unsafe.Pointer(pConstraint)).Fusable) == 0 { goto _1 } if int32((*Tsqlite3_index_constraint)(unsafe.Pointer(pConstraint)).Fop) != int32(SQLITE_INDEX_CONSTRAINT_EQ) { goto _1 } if (*Tsqlite3_index_constraint)(unsafe.Pointer(pConstraint)).FiColumn < int32((*TPragmaVtab)(unsafe.Pointer(pTab)).FiHidden) { goto _1 } j = (*Tsqlite3_index_constraint)(unsafe.Pointer(pConstraint)).FiColumn - int32((*TPragmaVtab)(unsafe.Pointer(pTab)).FiHidden) _ = libc.Int32FromInt32(0) seen[j] = i + int32(1) goto _1 _1: ; i++ pConstraint += 12 } if seen[0] == 0 { (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = libc.Float64FromInt32(2147483647) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = int64(2147483647) return SQLITE_OK } j = seen[0] - int32(1) (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(j)*8))).FargvIndex = int32(1) (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(j)*8))).Fomit = uint8(1) if seen[int32(1)] == 0 { (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = libc.Float64FromInt32(1000) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = int64(1000) return SQLITE_OK } (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = libc.Float64FromInt32(20) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = int64(20) j = seen[int32(1)] - int32(1) (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(j)*8))).FargvIndex = int32(2) (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(j)*8))).Fomit = uint8(1) return SQLITE_OK } // C documentation // // /* Create a new cursor for the pragma virtual table */ func _pragmaVtabOpen(tls *libc.TLS, pVtab uintptr, ppCursor uintptr) (r int32) { var pCsr uintptr _ = pCsr pCsr = Xsqlite3_malloc(tls, int32(24)) if pCsr == uintptr(0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, pCsr, 0, uint32(24)) (*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab = pVtab *(*uintptr)(unsafe.Pointer(ppCursor)) = pCsr return SQLITE_OK } // C documentation // // /* Clear all content from pragma virtual table cursor. */ func _pragmaVtabCursorClear(tls *libc.TLS, pCsr uintptr) { var i int32 _ = i Xsqlite3_finalize(tls, (*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).FpPragma) (*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).FpPragma = uintptr(0) i = 0 for { if !(i < int32(libc.Uint32FromInt64(8)/libc.Uint32FromInt64(4))) { break } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(pCsr + 16 + uintptr(i)*4))) *(*uintptr)(unsafe.Pointer(pCsr + 16 + uintptr(i)*4)) = uintptr(0) goto _1 _1: ; i++ } } // C documentation // // /* Close a pragma virtual table cursor */ func _pragmaVtabClose(tls *libc.TLS, cur uintptr) (r int32) { var pCsr uintptr _ = pCsr pCsr = cur _pragmaVtabCursorClear(tls, pCsr) Xsqlite3_free(tls, pCsr) return SQLITE_OK } // C documentation // // /* Advance the pragma virtual table cursor to the next row */ func _pragmaVtabNext(tls *libc.TLS, pVtabCursor uintptr) (r int32) { var pCsr uintptr var rc int32 _, _ = pCsr, rc pCsr = pVtabCursor rc = SQLITE_OK /* Increment the xRowid value */ (*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).FiRowid++ _ = libc.Int32FromInt32(0) if int32(SQLITE_ROW) != Xsqlite3_step(tls, (*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).FpPragma) { rc = Xsqlite3_finalize(tls, (*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).FpPragma) (*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).FpPragma = uintptr(0) _pragmaVtabCursorClear(tls, pCsr) } return rc } // C documentation // // /* // ** Pragma virtual table module xFilter method. // */ func _pragmaVtabFilter(tls *libc.TLS, pVtabCursor uintptr, idxNum int32, idxStr uintptr, argc int32, argv uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var i, j, rc, v1 int32 var pCsr, pTab, zSql, zText uintptr var _ /* acc at bp+0 */ TStrAccum _, _, _, _, _, _, _, _ = i, j, pCsr, pTab, rc, zSql, zText, v1 pCsr = pVtabCursor pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVtabCursor)).FpVtab _ = idxNum _ = idxStr _pragmaVtabCursorClear(tls, pCsr) if int32((*TPragmaName)(unsafe.Pointer((*TPragmaVtab)(unsafe.Pointer(pTab)).FpName)).FmPragFlg)&int32(PragFlg_Result1) != 0 { v1 = 0 } else { v1 = int32(1) } j = v1 i = 0 for { if !(i < argc) { break } zText = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if zText != 0 { *(*uintptr)(unsafe.Pointer(pCsr + 16 + uintptr(j)*4)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+32, zText)) if *(*uintptr)(unsafe.Pointer(pCsr + 16 + uintptr(j)*4)) == uintptr(0) { return int32(SQLITE_NOMEM) } } goto _2 _2: ; i++ j++ } _sqlite3StrAccumInit(tls, bp, uintptr(0), uintptr(0), 0, *(*int32)(unsafe.Pointer((*TPragmaVtab)(unsafe.Pointer(pTab)).Fdb + 120 + 1*4))) Xsqlite3_str_appendall(tls, bp, __ccgo_ts+18628) if *(*uintptr)(unsafe.Pointer(pCsr + 16 + 1*4)) != 0 { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+18636, libc.VaList(bp+32, *(*uintptr)(unsafe.Pointer(pCsr + 16 + 1*4)))) } Xsqlite3_str_appendall(tls, bp, (*TPragmaName)(unsafe.Pointer((*TPragmaVtab)(unsafe.Pointer(pTab)).FpName)).FzName) if *(*uintptr)(unsafe.Pointer(pCsr + 16)) != 0 { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+18640, libc.VaList(bp+32, *(*uintptr)(unsafe.Pointer(pCsr + 16)))) } zSql = _sqlite3StrAccumFinish(tls, bp) if zSql == uintptr(0) { return int32(SQLITE_NOMEM) } rc = Xsqlite3_prepare_v2(tls, (*TPragmaVtab)(unsafe.Pointer(pTab)).Fdb, zSql, -int32(1), pCsr+4, uintptr(0)) Xsqlite3_free(tls, zSql) if rc != SQLITE_OK { (*TPragmaVtab)(unsafe.Pointer(pTab)).Fbase.FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+32, Xsqlite3_errmsg(tls, (*TPragmaVtab)(unsafe.Pointer(pTab)).Fdb))) return rc } return _pragmaVtabNext(tls, pVtabCursor) } // C documentation // // /* // ** Pragma virtual table module xEof method. // */ func _pragmaVtabEof(tls *libc.TLS, pVtabCursor uintptr) (r int32) { var pCsr uintptr _ = pCsr pCsr = pVtabCursor return libc.BoolInt32((*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).FpPragma == uintptr(0)) } // C documentation // // /* The xColumn method simply returns the corresponding column from // ** the PRAGMA. // */ func _pragmaVtabColumn(tls *libc.TLS, pVtabCursor uintptr, ctx uintptr, i int32) (r int32) { var pCsr, pTab uintptr _, _ = pCsr, pTab pCsr = pVtabCursor pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVtabCursor)).FpVtab if i < int32((*TPragmaVtab)(unsafe.Pointer(pTab)).FiHidden) { Xsqlite3_result_value(tls, ctx, Xsqlite3_column_value(tls, (*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).FpPragma, i)) } else { Xsqlite3_result_text(tls, ctx, *(*uintptr)(unsafe.Pointer(pCsr + 16 + uintptr(i-int32((*TPragmaVtab)(unsafe.Pointer(pTab)).FiHidden))*4)), -int32(1), uintptr(-libc.Int32FromInt32(1))) } return SQLITE_OK } // C documentation // // /* // ** Pragma virtual table module xRowid method. // */ func _pragmaVtabRowid(tls *libc.TLS, pVtabCursor uintptr, p uintptr) (r int32) { var pCsr uintptr _ = pCsr pCsr = pVtabCursor *(*Tsqlite_int64)(unsafe.Pointer(p)) = (*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).FiRowid return SQLITE_OK } // C documentation // // /* The pragma virtual table object */ var _pragmaVtabModule = Tsqlite3_module{} func init() { p := unsafe.Pointer(&_pragmaVtabModule) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_pragmaVtabConnect) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_pragmaVtabBestIndex) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_pragmaVtabDisconnect) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_pragmaVtabOpen) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_pragmaVtabClose) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_pragmaVtabFilter) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_pragmaVtabNext) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_pragmaVtabEof) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_pragmaVtabColumn) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_pragmaVtabRowid) } // C documentation // // /* // ** Check to see if zTabName is really the name of a pragma. If it is, // ** then register an eponymous virtual table for that pragma and return // ** a pointer to the Module object for the new virtual table. // */ func _sqlite3PragmaVtabRegister(tls *libc.TLS, db uintptr, zName uintptr) (r uintptr) { var pName uintptr _ = pName _ = libc.Int32FromInt32(0) pName = _pragmaLocate(tls, zName+uintptr(7)) if pName == uintptr(0) { return uintptr(0) } if int32((*TPragmaName)(unsafe.Pointer(pName)).FmPragFlg)&(libc.Int32FromInt32(PragFlg_Result0)|libc.Int32FromInt32(PragFlg_Result1)) == 0 { return uintptr(0) } _ = libc.Int32FromInt32(0) return _sqlite3VtabCreateModule(tls, db, zName, uintptr(unsafe.Pointer(&_pragmaVtabModule)), pName, uintptr(0)) } /************** End of pragma.c **********************************************/ /************** Begin file prepare.c *****************************************/ /* ** 2005 May 25 ** ** 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 contains the implementation of the sqlite3_prepare() ** interface, and routines that contribute to loading the database schema ** from disk. */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** Fill the InitData structure with an error message that indicates // ** that the database is corrupt. // */ func _corruptSchema(tls *libc.TLS, pData uintptr, azObj uintptr, zExtra uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var db, z, zObj, v1 uintptr _, _, _, _ = db, z, zObj, v1 db = (*TInitData)(unsafe.Pointer(pData)).Fdb if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { (*TInitData)(unsafe.Pointer(pData)).Frc = int32(SQLITE_NOMEM) } else { if *(*uintptr)(unsafe.Pointer((*TInitData)(unsafe.Pointer(pData)).FpzErrMsg)) != uintptr(0) { /* A error message has already been generated. Do not overwrite it */ } else { if (*TInitData)(unsafe.Pointer(pData)).FmInitFlags&uint32(libc.Int32FromInt32(INITFLAG_AlterMask)) != 0 { *(*uintptr)(unsafe.Pointer((*TInitData)(unsafe.Pointer(pData)).FpzErrMsg)) = _sqlite3MPrintf(tls, db, __ccgo_ts+18674, libc.VaList(bp+8, *(*uintptr)(unsafe.Pointer(azObj)), *(*uintptr)(unsafe.Pointer(azObj + 1*4)), _azAlterType[(*TInitData)(unsafe.Pointer(pData)).FmInitFlags&uint32(INITFLAG_AlterMask)-uint32(1)], zExtra)) (*TInitData)(unsafe.Pointer(pData)).Frc = int32(SQLITE_ERROR) } else { if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_WriteSchema) != 0 { (*TInitData)(unsafe.Pointer(pData)).Frc = _sqlite3CorruptError(tls, int32(140891)) } else { if *(*uintptr)(unsafe.Pointer(azObj + 1*4)) != 0 { v1 = *(*uintptr)(unsafe.Pointer(azObj + 1*4)) } else { v1 = __ccgo_ts + 5108 } zObj = v1 z = _sqlite3MPrintf(tls, db, __ccgo_ts+18702, libc.VaList(bp+8, zObj)) if zExtra != 0 && *(*uint8)(unsafe.Pointer(zExtra)) != 0 { z = _sqlite3MPrintf(tls, db, __ccgo_ts+18733, libc.VaList(bp+8, z, zExtra)) } *(*uintptr)(unsafe.Pointer((*TInitData)(unsafe.Pointer(pData)).FpzErrMsg)) = z (*TInitData)(unsafe.Pointer(pData)).Frc = _sqlite3CorruptError(tls, int32(140898)) } } } } } var _azAlterType = [3]uintptr{ 0: __ccgo_ts + 18644, 1: __ccgo_ts + 18651, 2: __ccgo_ts + 18663, } // C documentation // // /* // ** Check to see if any sibling index (another index on the same table) // ** of pIndex has the same root page number, and if it does, return true. // ** This would indicate a corrupt schema. // */ func _sqlite3IndexHasDuplicateRootPage(tls *libc.TLS, pIndex uintptr) (r int32) { var p uintptr _ = p p = (*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FpTable)).FpIndex for { if !(p != 0) { break } if (*TIndex)(unsafe.Pointer(p)).Ftnum == (*TIndex)(unsafe.Pointer(pIndex)).Ftnum && p != pIndex { return int32(1) } goto _1 _1: ; p = (*TIndex)(unsafe.Pointer(p)).FpNext } return 0 } // C documentation // // /* // ** This is the callback routine for the code that initializes the // ** database. See sqlite3Init() below for additional information. // ** This routine is also called from the OP_ParseSchema opcode of the VDBE. // ** // ** Each callback contains the following information: // ** // ** argv[0] = type of object: "table", "index", "trigger", or "view". // ** argv[1] = name of thing being created // ** argv[2] = associated table if an index or trigger // ** argv[3] = root page number for table or index. 0 for trigger or view. // ** argv[4] = SQL text for the CREATE statement. // ** // */ func _sqlite3InitCallback(tls *libc.TLS, pInit uintptr, argc int32, argv uintptr, NotUsed uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var db, pData, pIndex uintptr var iDb, rc int32 var saved_iDb Tu8 var _ /* pStmt at bp+0 */ uintptr _, _, _, _, _, _ = db, iDb, pData, pIndex, rc, saved_iDb pData = pInit db = (*TInitData)(unsafe.Pointer(pData)).Fdb iDb = (*TInitData)(unsafe.Pointer(pData)).FiDb _ = libc.Int32FromInt32(0) _ = NotUsed _ = argc _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(db + 24)) |= uint32(DBFLAG_EncodingFixed) if argv == uintptr(0) { return 0 } /* Might happen if EMPTY_RESULT_CALLBACKS are on */ (*TInitData)(unsafe.Pointer(pData)).FnInitRow++ if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _corruptSchema(tls, pData, argv, uintptr(0)) return int32(1) } _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(argv + 3*4)) == uintptr(0) { _corruptSchema(tls, pData, argv, uintptr(0)) } else { if *(*uintptr)(unsafe.Pointer(argv + 4*4)) != 0 && int32('c') == int32(_sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(argv + 4*4))))]) && int32('r') == int32(_sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(argv + 4*4)) + 1))]) { saved_iDb = (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb /* Return code from sqlite3_prepare() */ _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(iDb) if _sqlite3GetUInt32(tls, *(*uintptr)(unsafe.Pointer(argv + 3*4)), db+172) == 0 || (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FnewTnum > (*TInitData)(unsafe.Pointer(pData)).FmxPage && (*TInitData)(unsafe.Pointer(pData)).FmxPage > uint32(0) { if _sqlite3Config.FbExtraSchemaChecks != 0 { _corruptSchema(tls, pData, argv, __ccgo_ts+14516) } } libc.SetBitFieldPtr8Uint32(db+172+8, libc.Uint32FromInt32(0), 0, 0x1) (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FazInit = argv *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) _sqlite3Prepare(tls, db, *(*uintptr)(unsafe.Pointer(argv + 4*4)), -int32(1), uint32(0), uintptr(0), bp, uintptr(0)) rc = (*Tsqlite3)(unsafe.Pointer(db)).FerrCode _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = saved_iDb /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */ if SQLITE_OK != rc { if int32(uint32(*(*uint8)(unsafe.Pointer(db + 172 + 8))&0x1>>0)) != 0 { _ = libc.Int32FromInt32(0) } else { if rc > (*TInitData)(unsafe.Pointer(pData)).Frc { (*TInitData)(unsafe.Pointer(pData)).Frc = rc } if rc == int32(SQLITE_NOMEM) { _sqlite3OomFault(tls, db) } else { if rc != int32(SQLITE_INTERRUPT) && rc&int32(0xFF) != int32(SQLITE_LOCKED) { _corruptSchema(tls, pData, argv, Xsqlite3_errmsg(tls, db)) } } } } (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FazInit = uintptr(unsafe.Pointer(&_sqlite3StdType)) /* Any array of string ptrs will do */ Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) } else { if *(*uintptr)(unsafe.Pointer(argv + 1*4)) == uintptr(0) || *(*uintptr)(unsafe.Pointer(argv + 4*4)) != uintptr(0) && int32(*(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(argv + 4*4))))) != 0 { _corruptSchema(tls, pData, argv, uintptr(0)) } else { pIndex = _sqlite3FindIndex(tls, db, *(*uintptr)(unsafe.Pointer(argv + 1*4)), (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName) if pIndex == uintptr(0) { _corruptSchema(tls, pData, argv, __ccgo_ts+18741) } else { if _sqlite3GetUInt32(tls, *(*uintptr)(unsafe.Pointer(argv + 3*4)), pIndex+44) == 0 || (*TIndex)(unsafe.Pointer(pIndex)).Ftnum < uint32(2) || (*TIndex)(unsafe.Pointer(pIndex)).Ftnum > (*TInitData)(unsafe.Pointer(pData)).FmxPage || _sqlite3IndexHasDuplicateRootPage(tls, pIndex) != 0 { if _sqlite3Config.FbExtraSchemaChecks != 0 { _corruptSchema(tls, pData, argv, __ccgo_ts+14516) } } } } } } return 0 } // C documentation // // /* // ** Attempt to read the database schema and initialize internal // ** data structures for a single database file. The index of the // ** database file is given by iDb. iDb==0 is used for the main // ** database. iDb==1 should never be used. iDb>=2 is used for // ** auxiliary databases. Return one of the SQLITE_ error codes to // ** indicate success or failure. // */ func _sqlite3InitOne(tls *libc.TLS, db uintptr, iDb int32, pzErrMsg uintptr, mFlags Tu32) (r int32) { bp := tls.Alloc(96) defer tls.Free(96) var encoding Tu8 var i, mask, openedTransaction, rc, size int32 var pDb, zSchemaTabName, zSql, v1, v2, p3, p5 uintptr var xAuth Tsqlite3_xauth var _ /* azArg at bp+0 */ [6]uintptr var _ /* initData at bp+44 */ TInitData var _ /* meta at bp+24 */ [5]int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _ = encoding, i, mask, openedTransaction, pDb, rc, size, xAuth, zSchemaTabName, zSql, v1, v2, p3, p5 openedTransaction = 0 mask = int32((*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&libc.Uint32FromInt32(DBFLAG_EncodingFixed) | uint32(^libc.Int32FromInt32(DBFLAG_EncodingFixed))) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy = uint8(1) /* Construct the in-memory representation schema tables (sqlite_schema or ** sqlite_temp_schema) by invoking the parser directly. The appropriate ** table name will be inserted automatically by the parser so we can just ** use the abbreviation "x" here. The parser will also automatically tag ** the schema table as read-only. */ (*(*[6]uintptr)(unsafe.Pointer(bp)))[0] = __ccgo_ts + 9058 if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) { v2 = __ccgo_ts + 6533 } else { v2 = __ccgo_ts + 6066 } v1 = v2 zSchemaTabName = v1 (*(*[6]uintptr)(unsafe.Pointer(bp)))[int32(1)] = v1 (*(*[6]uintptr)(unsafe.Pointer(bp)))[int32(2)] = (*(*[6]uintptr)(unsafe.Pointer(bp)))[int32(1)] (*(*[6]uintptr)(unsafe.Pointer(bp)))[int32(3)] = __ccgo_ts + 8130 (*(*[6]uintptr)(unsafe.Pointer(bp)))[int32(4)] = __ccgo_ts + 18754 (*(*[6]uintptr)(unsafe.Pointer(bp)))[int32(5)] = uintptr(0) (*(*TInitData)(unsafe.Pointer(bp + 44))).Fdb = db (*(*TInitData)(unsafe.Pointer(bp + 44))).FiDb = iDb (*(*TInitData)(unsafe.Pointer(bp + 44))).Frc = SQLITE_OK (*(*TInitData)(unsafe.Pointer(bp + 44))).FpzErrMsg = pzErrMsg (*(*TInitData)(unsafe.Pointer(bp + 44))).FmInitFlags = mFlags (*(*TInitData)(unsafe.Pointer(bp + 44))).FnInitRow = uint32(0) (*(*TInitData)(unsafe.Pointer(bp + 44))).FmxPage = uint32(0) _sqlite3InitCallback(tls, bp+44, int32(5), bp, uintptr(0)) *(*Tu32)(unsafe.Pointer(db + 24)) &= uint32(mask) if (*(*TInitData)(unsafe.Pointer(bp + 44))).Frc != 0 { rc = (*(*TInitData)(unsafe.Pointer(bp + 44))).Frc goto error_out } /* Create a cursor to hold the database open */ pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16 if (*TDb)(unsafe.Pointer(pDb)).FpBt == uintptr(0) { _ = libc.Int32FromInt32(0) p3 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema + 78 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) | libc.Int32FromInt32(DB_SchemaLoaded)) rc = SQLITE_OK goto error_out } /* If there is not already a read-only (or read-write) transaction opened ** on the b-tree database, open one now. If a transaction is opened, it ** will be closed before this function returns. */ _sqlite3BtreeEnter(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) if _sqlite3BtreeTxnState(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) == SQLITE_TXN_NONE { rc = _sqlite3BtreeBeginTrans(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, 0, uintptr(0)) if rc != SQLITE_OK { _sqlite3SetString(tls, pzErrMsg, db, _sqlite3ErrStr(tls, rc)) goto initone_error_out } openedTransaction = int32(1) } /* Get the database meta information. ** ** Meta values are as follows: ** meta[0] Schema cookie. Changes with each schema change. ** meta[1] File format of schema layer. ** meta[2] Size of the page cache. ** meta[3] Largest rootpage (auto/incr_vacuum mode) ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE ** meta[5] User version ** meta[6] Incremental vacuum mode ** meta[7] unused ** meta[8] unused ** meta[9] unused ** ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to ** the possible values of meta[4]. */ i = 0 for { if !(i < int32(libc.Uint32FromInt64(20)/libc.Uint32FromInt64(4))) { break } _sqlite3BtreeGetMeta(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, i+int32(1), bp+24+uintptr(i)*4) goto _4 _4: ; i++ } if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ResetDatabase) != uint64(0) { libc.Xmemset(tls, bp+24, 0, uint32(20)) } (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fschema_cookie = (*(*[5]int32)(unsafe.Pointer(bp + 24)))[libc.Int32FromInt32(BTREE_SCHEMA_VERSION)-libc.Int32FromInt32(1)] /* If opening a non-empty database, check the text encoding. For the ** main database, set sqlite3.enc to the encoding of the main database. ** For an attached db, it is an error if the encoding is not the same ** as sqlite3.enc. */ if (*(*[5]int32)(unsafe.Pointer(bp + 24)))[libc.Int32FromInt32(BTREE_TEXT_ENCODING)-libc.Int32FromInt32(1)] != 0 { /* text encoding */ if iDb == 0 && (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_EncodingFixed) == uint32(0) { /* If opening the main database, set ENC(db). */ encoding = uint8(int32(uint8((*(*[5]int32)(unsafe.Pointer(bp + 24)))[libc.Int32FromInt32(BTREE_TEXT_ENCODING)-libc.Int32FromInt32(1)])) & int32(3)) if int32(encoding) == 0 { encoding = uint8(SQLITE_UTF8) } if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive > 0 && int32(encoding) != int32((*Tsqlite3)(unsafe.Pointer(db)).Fenc) && (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) == uint32(0) { rc = int32(SQLITE_LOCKED) goto initone_error_out } else { _sqlite3SetTextEncoding(tls, db, encoding) } } else { /* If opening an attached database, the encoding much match ENC(db) */ if (*(*[5]int32)(unsafe.Pointer(bp + 24)))[libc.Int32FromInt32(BTREE_TEXT_ENCODING)-libc.Int32FromInt32(1)]&int32(3) != int32((*Tsqlite3)(unsafe.Pointer(db)).Fenc) { _sqlite3SetString(tls, pzErrMsg, db, __ccgo_ts+12095) rc = int32(SQLITE_ERROR) goto initone_error_out } } } (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fenc = (*Tsqlite3)(unsafe.Pointer(db)).Fenc if (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size == 0 { size = _sqlite3AbsInt32(tls, (*(*[5]int32)(unsafe.Pointer(bp + 24)))[libc.Int32FromInt32(BTREE_DEFAULT_CACHE_SIZE)-libc.Int32FromInt32(1)]) if size == 0 { size = -int32(2000) } (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size = size _sqlite3BtreeSetCacheSize(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size) } /* ** file_format==1 Version 3.0.0. ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants */ (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Ffile_format = uint8((*(*[5]int32)(unsafe.Pointer(bp + 24)))[libc.Int32FromInt32(BTREE_FILE_FORMAT)-libc.Int32FromInt32(1)]) if int32((*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Ffile_format) == 0 { (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Ffile_format = uint8(1) } if int32((*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Ffile_format) > int32(SQLITE_MAX_FILE_FORMAT) { _sqlite3SetString(tls, pzErrMsg, db, __ccgo_ts+18826) rc = int32(SQLITE_ERROR) goto initone_error_out } /* Ticket #2804: When we open a database in the newer file format, ** clear the legacy_file_format pragma flag so that a VACUUM will ** not downgrade the database and thus invalidate any descending ** indices that the user might have created. */ if iDb == 0 && (*(*[5]int32)(unsafe.Pointer(bp + 24)))[libc.Int32FromInt32(BTREE_FILE_FORMAT)-libc.Int32FromInt32(1)] >= int32(4) { *(*Tu64)(unsafe.Pointer(db + 32)) &= ^libc.Uint64FromInt32(SQLITE_LegacyFileFmt) } /* Read the schema information out of the schema tables */ _ = libc.Int32FromInt32(0) (*(*TInitData)(unsafe.Pointer(bp + 44))).FmxPage = _sqlite3BtreeLastPage(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) zSql = _sqlite3MPrintf(tls, db, __ccgo_ts+18850, libc.VaList(bp+80, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, zSchemaTabName)) xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0) rc = Xsqlite3_exec(tls, db, zSql, __ccgo_fp(_sqlite3InitCallback), bp+44, uintptr(0)) (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth if rc == SQLITE_OK { rc = (*(*TInitData)(unsafe.Pointer(bp + 44))).Frc } _sqlite3DbFree(tls, db, zSql) if rc == SQLITE_OK { _sqlite3AnalysisLoad(tls, db, iDb) } _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { rc = int32(SQLITE_NOMEM) _sqlite3ResetAllSchemasOfConnection(tls, db) pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16 } else { if rc == SQLITE_OK || (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_NoSchemaError) != 0 && rc != int32(SQLITE_NOMEM) { /* Hack: If the SQLITE_NoSchemaError flag is set, then consider ** the schema loaded, even if errors (other than OOM) occurred. In ** this situation the current sqlite3_prepare() operation will fail, ** but the following one will attempt to compile the supplied statement ** against whatever subset of the schema was loaded before the error ** occurred. ** ** The primary purpose of this is to allow access to the sqlite_schema ** table even when its contents have been corrupted. */ p5 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema + 78 *(*Tu16)(unsafe.Pointer(p5)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p5))) | libc.Int32FromInt32(DB_SchemaLoaded)) rc = SQLITE_OK } } /* Jump here for an error that occurs after successfully allocating ** curMain and calling sqlite3BtreeEnter(). For an error that occurs ** before that point, jump to error_out. */ goto initone_error_out initone_error_out: ; if openedTransaction != 0 { _sqlite3BtreeCommit(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) } _sqlite3BtreeLeave(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) goto error_out error_out: ; if rc != 0 { if rc == int32(SQLITE_NOMEM) || rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)< 0) { break } _ = libc.Int32FromInt32(0) if !(int32((*TSchema)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema)).FschemaFlags)&libc.Int32FromInt32(DB_SchemaLoaded) == libc.Int32FromInt32(DB_SchemaLoaded)) { rc = _sqlite3InitOne(tls, db, i, pzErrMsg, uint32(0)) if rc != 0 { return rc } } goto _1 _1: ; i-- } if commit_internal != 0 { _sqlite3CommitInternalChanges(tls, db) } return SQLITE_OK } // C documentation // // /* // ** This routine is a no-op if the database schema is already initialized. // ** Otherwise, the schema is loaded. An error code is returned. // */ func _sqlite3ReadSchema(tls *libc.TLS, pParse uintptr) (r int32) { var db uintptr var rc int32 _, _ = db, rc rc = SQLITE_OK db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) if !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) { rc = _sqlite3Init(tls, db, pParse+4) if rc != SQLITE_OK { (*TParse)(unsafe.Pointer(pParse)).Frc = rc (*TParse)(unsafe.Pointer(pParse)).FnErr++ } else { if (*Tsqlite3)(unsafe.Pointer(db)).FnoSharedCache != 0 { *(*Tu32)(unsafe.Pointer(db + 24)) |= uint32(DBFLAG_SchemaKnownOk) } } } return rc } // C documentation // // /* // ** Check schema cookies in all databases. If any cookie is out // ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies // ** make no changes to pParse->rc. // */ func _schemaIsValid(tls *libc.TLS, pParse uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, pBt uintptr var iDb, openedTransaction, rc int32 var _ /* cookie at bp+0 */ int32 _, _, _, _, _ = db, iDb, openedTransaction, pBt, rc db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) iDb = 0 for { if !(iDb < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } openedTransaction = 0 /* True if a transaction is opened */ pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt /* Btree database to read cookie from */ if pBt == uintptr(0) { goto _1 } /* If there is not already a read-only (or read-write) transaction opened ** on the b-tree database, open one now. If a transaction is opened, it ** will be closed immediately after reading the meta-value. */ if _sqlite3BtreeTxnState(tls, pBt) == SQLITE_TXN_NONE { rc = _sqlite3BtreeBeginTrans(tls, pBt, 0, uintptr(0)) if rc == int32(SQLITE_NOMEM) || rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<aDb[] the schema refers to. // ** // ** If the same database is attached more than once, the first // ** attached database is returned. // */ func _sqlite3SchemaToIndex(tls *libc.TLS, db uintptr, pSchema uintptr) (r int32) { var i int32 _ = i i = -int32(32768) /* If pSchema is NULL, then return -32768. This happens when code in ** expr.c is trying to resolve a reference to a transient table (i.e. one ** created by a sub-select). In this case the return value of this ** function should never be used. ** ** We return -32768 instead of the more usual -1 simply because using ** -32768 as the incorrect index into db->aDb[] is much ** more likely to cause a segfault than -1 (of course there are assert() ** statements too, but it never hurts to play the odds) and ** -32768 will still fit into a 16-bit signed integer. */ _ = libc.Int32FromInt32(0) if pSchema != 0 { i = 0 for { if !(int32(1) != 0) { break } _ = libc.Int32FromInt32(0) if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema == pSchema { break } goto _1 _1: ; i++ } _ = libc.Int32FromInt32(0) } return i } // C documentation // // /* // ** Free all memory allocations in the pParse object // */ func _sqlite3ParseObjectReset(tls *libc.TLS, pParse uintptr) { var db, pCleanup uintptr var v1 int32 _, _, _ = db, pCleanup, v1 db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FaTableLock != 0 { _sqlite3DbNNFreeNN(tls, db, (*TParse)(unsafe.Pointer(pParse)).FaTableLock) } for (*TParse)(unsafe.Pointer(pParse)).FpCleanup != 0 { pCleanup = (*TParse)(unsafe.Pointer(pParse)).FpCleanup (*TParse)(unsafe.Pointer(pParse)).FpCleanup = (*TParseCleanup)(unsafe.Pointer(pCleanup)).FpNext (*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TParseCleanup)(unsafe.Pointer(pCleanup)).FxCleanup})))(tls, db, (*TParseCleanup)(unsafe.Pointer(pCleanup)).FpPtr) _sqlite3DbNNFreeNN(tls, db, pCleanup) } if (*TParse)(unsafe.Pointer(pParse)).FaLabel != 0 { _sqlite3DbNNFreeNN(tls, db, (*TParse)(unsafe.Pointer(pParse)).FaLabel) } if (*TParse)(unsafe.Pointer(pParse)).FpConstExpr != 0 { _sqlite3ExprListDelete(tls, db, (*TParse)(unsafe.Pointer(pParse)).FpConstExpr) } _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable -= uint32((*TParse)(unsafe.Pointer(pParse)).FdisableLookaside) if (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable != 0 { v1 = 0 } else { v1 = int32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue) } (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(v1) _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).FpParse = (*TParse)(unsafe.Pointer(pParse)).FpOuterParse } // C documentation // // /* // ** Add a new cleanup operation to a Parser. The cleanup should happen when // ** the parser object is destroyed. But, beware: the cleanup might happen // ** immediately. // ** // ** Use this mechanism for uncommon cleanups. There is a higher setup // ** cost for this mechanism (an extra malloc), so it should not be used // ** for common cleanups that happen on most calls. But for less // ** common cleanups, we save a single NULL-pointer comparison in // ** sqlite3ParseObjectReset(), which reduces the total CPU cycle count. // ** // ** If a memory allocation error occurs, then the cleanup happens immediately. // ** When either SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the // ** pParse->earlyCleanup flag is set in that case. Calling code show verify // ** that test cases exist for which this happens, to guard against possible // ** use-after-free errors following an OOM. The preferred way to do this is // ** to immediately follow the call to this routine with: // ** // ** testcase( pParse->earlyCleanup ); // ** // ** This routine returns a copy of its pPtr input (the third parameter) // ** except if an early cleanup occurs, in which case it returns NULL. So // ** another way to check for early cleanup is to check the return value. // ** Or, stop using the pPtr parameter with this call and use only its // ** return value thereafter. Something like this: // ** // ** pObj = sqlite3ParserAddCleanup(pParse, destructor, pObj); // */ func _sqlite3ParserAddCleanup(tls *libc.TLS, pParse uintptr, xCleanup uintptr, pPtr uintptr) (r uintptr) { var pCleanup uintptr _ = pCleanup pCleanup = _sqlite3DbMallocRaw(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(12)) if pCleanup != 0 { (*TParseCleanup)(unsafe.Pointer(pCleanup)).FpNext = (*TParse)(unsafe.Pointer(pParse)).FpCleanup (*TParse)(unsafe.Pointer(pParse)).FpCleanup = pCleanup (*TParseCleanup)(unsafe.Pointer(pCleanup)).FpPtr = pPtr (*TParseCleanup)(unsafe.Pointer(pCleanup)).FxCleanup = xCleanup } else { (*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{xCleanup})))(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pPtr) pPtr = uintptr(0) } return pPtr } // C documentation // // /* // ** Turn bulk memory into a valid Parse object and link that Parse object // ** into database connection db. // ** // ** Call sqlite3ParseObjectReset() to undo this operation. // ** // ** Caution: Do not confuse this routine with sqlite3ParseObjectInit() which // ** is generated by Lemon. // */ func _sqlite3ParseObjectInit(tls *libc.TLS, pParse uintptr, db uintptr) { libc.Xmemset(tls, pParse+uintptr(uint32(libc.UintptrFromInt32(0)+4)), 0, uint32(libc.UintptrFromInt32(0)+164)-uint32(libc.UintptrFromInt32(0)+4)) libc.Xmemset(tls, pParse+uintptr(uint32(libc.UintptrFromInt32(0)+208)), 0, libc.Uint32FromInt64(284)-uint32(libc.UintptrFromInt32(0)+208)) _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).FpOuterParse = (*Tsqlite3)(unsafe.Pointer(db)).FpParse (*Tsqlite3)(unsafe.Pointer(db)).FpParse = pParse (*TParse)(unsafe.Pointer(pParse)).Fdb = db if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+1618, 0) } } /* ** Maximum number of times that we will try again to prepare a statement ** that returns SQLITE_ERROR_RETRY. */ // C documentation // // /* // ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. // */ func _sqlite3Prepare(tls *libc.TLS, db uintptr, zSql uintptr, nBytes int32, prepFlags Tu32, pReprepare uintptr, ppStmt uintptr, pzTail uintptr) (r int32) { bp := tls.Alloc(304) defer tls.Free(304) var i, mxLen, rc, v1 int32 var pBt, pT, zDb, zSqlCopy uintptr var _ /* sParse at bp+0 */ TParse _, _, _, _, _, _, _, _ = i, mxLen, pBt, pT, rc, zDb, zSqlCopy, v1 rc = SQLITE_OK /* Parsing context */ /* sqlite3ParseObjectInit(&sParse, db); // inlined for performance */ libc.Xmemset(tls, bp+uintptr(uint32(libc.UintptrFromInt32(0)+4)), 0, uint32(libc.UintptrFromInt32(0)+164)-uint32(libc.UintptrFromInt32(0)+4)) libc.Xmemset(tls, bp+uintptr(uint32(libc.UintptrFromInt32(0)+208)), 0, libc.Uint32FromInt64(284)-uint32(libc.UintptrFromInt32(0)+208)) (*(*TParse)(unsafe.Pointer(bp))).FpOuterParse = (*Tsqlite3)(unsafe.Pointer(db)).FpParse (*Tsqlite3)(unsafe.Pointer(db)).FpParse = bp (*(*TParse)(unsafe.Pointer(bp))).Fdb = db if pReprepare != 0 { (*(*TParse)(unsafe.Pointer(bp))).FpReprepare = pReprepare (*(*TParse)(unsafe.Pointer(bp))).Fexplain = uint8(Xsqlite3_stmt_isexplain(tls, pReprepare)) } else { _ = libc.Int32FromInt32(0) } _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _sqlite3ErrorMsg(tls, bp, __ccgo_ts+1618, 0) v1 = libc.Int32FromInt32(SQLITE_NOMEM) rc = v1 (*Tsqlite3)(unsafe.Pointer(db)).FerrCode = v1 goto end_prepare } _ = libc.Int32FromInt32(0) /* For a long-term use prepared statement avoid the use of ** lookaside memory. */ if prepFlags&uint32(SQLITE_PREPARE_PERSISTENT) != 0 { (*(*TParse)(unsafe.Pointer(bp))).FdisableLookaside++ (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable++ (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0) } (*(*TParse)(unsafe.Pointer(bp))).FprepFlags = uint8(prepFlags & uint32(0xff)) /* Check to verify that it is possible to get a read lock on all ** database schemas. The inability to get a read lock indicates that ** some other database connection is holding a write-lock, which in ** turn means that the other connection has made uncommitted changes ** to the schema. ** ** Were we to proceed and prepare the statement against the uncommitted ** schema changes and if those schema changes are subsequently rolled ** back and different changes are made in their place, then when this ** prepared statement goes to run the schema cookie would fail to detect ** the schema change. Disaster would follow. ** ** This thread is currently holding mutexes on all Btrees (because ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it ** is not possible for another thread to start a new schema change ** while this routine is running. Hence, we do not need to hold ** locks on the schema, we just need to make sure nobody else is ** holding them. ** ** Note that setting READ_UNCOMMITTED overrides most lock detection, ** but it does *not* override schema lock detection, so this all still ** works even if READ_UNCOMMITTED is set. */ if !((*Tsqlite3)(unsafe.Pointer(db)).FnoSharedCache != 0) { i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt if pBt != 0 { _ = libc.Int32FromInt32(0) rc = _sqlite3BtreeSchemaLocked(tls, pBt) if rc != 0 { zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FzDbSName _sqlite3ErrorWithMsg(tls, db, rc, __ccgo_ts+18884, libc.VaList(bp+296, zDb)) goto end_prepare } } goto _2 _2: ; i++ } } if (*Tsqlite3)(unsafe.Pointer(db)).FpDisconnect != 0 { _sqlite3VtabUnlockList(tls, db) } if nBytes >= 0 && (nBytes == 0 || int32(*(*uint8)(unsafe.Pointer(zSql + uintptr(nBytes-int32(1))))) != 0) { mxLen = *(*int32)(unsafe.Pointer(db + 120 + 1*4)) if nBytes > mxLen { _sqlite3ErrorWithMsg(tls, db, int32(SQLITE_TOOBIG), __ccgo_ts+18914, 0) rc = _sqlite3ApiExit(tls, db, int32(SQLITE_TOOBIG)) goto end_prepare } zSqlCopy = _sqlite3DbStrNDup(tls, db, zSql, uint64(nBytes)) if zSqlCopy != 0 { _sqlite3RunParser(tls, bp, zSqlCopy) (*(*TParse)(unsafe.Pointer(bp))).FzTail = zSql + uintptr(int32((*(*TParse)(unsafe.Pointer(bp))).FzTail)-int32(zSqlCopy)) _sqlite3DbFree(tls, db, zSqlCopy) } else { (*(*TParse)(unsafe.Pointer(bp))).FzTail = zSql + uintptr(nBytes) } } else { _sqlite3RunParser(tls, bp, zSql) } _ = libc.Int32FromInt32(0) if pzTail != 0 { *(*uintptr)(unsafe.Pointer(pzTail)) = (*(*TParse)(unsafe.Pointer(bp))).FzTail } if int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) == 0 { _sqlite3VdbeSetSql(tls, (*(*TParse)(unsafe.Pointer(bp))).FpVdbe, zSql, int32((*(*TParse)(unsafe.Pointer(bp))).FzTail)-int32(zSql), uint8(prepFlags)) } if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { (*(*TParse)(unsafe.Pointer(bp))).Frc = int32(SQLITE_NOMEM) (*(*TParse)(unsafe.Pointer(bp))).FcheckSchema = uint8(0) } if (*(*TParse)(unsafe.Pointer(bp))).Frc != SQLITE_OK && (*(*TParse)(unsafe.Pointer(bp))).Frc != int32(SQLITE_DONE) { if (*(*TParse)(unsafe.Pointer(bp))).FcheckSchema != 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) == 0 { _schemaIsValid(tls, bp) } if (*(*TParse)(unsafe.Pointer(bp))).FpVdbe != 0 { _sqlite3VdbeFinalize(tls, (*(*TParse)(unsafe.Pointer(bp))).FpVdbe) } _ = libc.Int32FromInt32(0) rc = (*(*TParse)(unsafe.Pointer(bp))).Frc if (*(*TParse)(unsafe.Pointer(bp))).FzErrMsg != 0 { _sqlite3ErrorWithMsg(tls, db, rc, __ccgo_ts+3795, libc.VaList(bp+296, (*(*TParse)(unsafe.Pointer(bp))).FzErrMsg)) _sqlite3DbFree(tls, db, (*(*TParse)(unsafe.Pointer(bp))).FzErrMsg) } else { _sqlite3Error(tls, db, rc) } } else { _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(ppStmt)) = (*(*TParse)(unsafe.Pointer(bp))).FpVdbe rc = SQLITE_OK _sqlite3ErrorClear(tls, db) } /* Delete any TriggerPrg structures allocated while parsing this statement. */ for (*(*TParse)(unsafe.Pointer(bp))).FpTriggerPrg != 0 { pT = (*(*TParse)(unsafe.Pointer(bp))).FpTriggerPrg (*(*TParse)(unsafe.Pointer(bp))).FpTriggerPrg = (*TTriggerPrg)(unsafe.Pointer(pT)).FpNext _sqlite3DbFree(tls, db, pT) } goto end_prepare end_prepare: ; _sqlite3ParseObjectReset(tls, bp) return rc } func _sqlite3LockAndPrepare(tls *libc.TLS, db uintptr, zSql uintptr, nBytes int32, prepFlags Tu32, pOld uintptr, ppStmt uintptr, pzTail uintptr) (r int32) { var cnt, rc, v1, v3 int32 var v2, v4, v5 bool _, _, _, _, _, _, _ = cnt, rc, v1, v2, v3, v4, v5 cnt = 0 *(*uintptr)(unsafe.Pointer(ppStmt)) = uintptr(0) if !(_sqlite3SafetyCheckOk(tls, db) != 0) || zSql == uintptr(0) { return _sqlite3MisuseError(tls, int32(141699)) } Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) _sqlite3BtreeEnterAll(tls, db) for { /* Make multiple attempts to compile the SQL, until it either succeeds ** or encounters a permanent error. A schema problem after one schema ** reset is considered a permanent error. */ rc = _sqlite3Prepare(tls, db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail) _ = libc.Int32FromInt32(0) if rc == SQLITE_OK || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { break } goto _6 _6: ; if v2 = rc == libc.Int32FromInt32(SQLITE_ERROR)|libc.Int32FromInt32(2)<= 0 { z = zSql sz = 0 for { if !(sz < nBytes && (int32(*(*uint8)(unsafe.Pointer(z + uintptr(sz)))) != 0 || int32(*(*uint8)(unsafe.Pointer(z + uintptr(sz+int32(1))))) != 0)) { break } goto _1 _1: ; sz += int32(2) } nBytes = sz } Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) zSql8 = _sqlite3Utf16to8(tls, db, zSql, nBytes, uint8(SQLITE_UTF16LE)) if zSql8 != 0 { rc = _sqlite3LockAndPrepare(tls, db, zSql8, -int32(1), prepFlags, uintptr(0), ppStmt, bp) } if *(*uintptr)(unsafe.Pointer(bp)) != 0 && pzTail != 0 { /* If sqlite3_prepare returns a tail pointer, we calculate the ** equivalent pointer into the UTF-16 string by counting the unicode ** characters between zSql8 and zTail8, and then returning a pointer ** the same number of characters into the UTF-16 string. */ chars_parsed = _sqlite3Utf8CharLen(tls, zSql8, int32(*(*uintptr)(unsafe.Pointer(bp)))-int32(zSql8)) *(*uintptr)(unsafe.Pointer(pzTail)) = zSql + uintptr(_sqlite3Utf16ByteLen(tls, zSql, chars_parsed)) } _sqlite3DbFree(tls, db, zSql8) rc = _sqlite3ApiExit(tls, db, rc) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** Two versions of the official API. Legacy and new use. In the legacy // ** version, the original SQL text is not saved in the prepared statement // ** and so if a schema change occurs, SQLITE_SCHEMA is returned by // ** sqlite3_step(). In the new version, the original SQL text is retained // ** and the statement is automatically recompiled if an schema change // ** occurs. // */ func Xsqlite3_prepare16(tls *libc.TLS, db uintptr, zSql uintptr, nBytes int32, ppStmt uintptr, pzTail uintptr) (r int32) { var rc int32 _ = rc rc = _sqlite3Prepare16(tls, db, zSql, nBytes, uint32(0), ppStmt, pzTail) _ = libc.Int32FromInt32(0) /* VERIFY: F13021 */ return rc } func Xsqlite3_prepare16_v2(tls *libc.TLS, db uintptr, zSql uintptr, nBytes int32, ppStmt uintptr, pzTail uintptr) (r int32) { var rc int32 _ = rc rc = _sqlite3Prepare16(tls, db, zSql, nBytes, uint32(SQLITE_PREPARE_SAVESQL), ppStmt, pzTail) _ = libc.Int32FromInt32(0) /* VERIFY: F13021 */ return rc } func Xsqlite3_prepare16_v3(tls *libc.TLS, db uintptr, zSql uintptr, nBytes int32, prepFlags uint32, ppStmt uintptr, pzTail uintptr) (r int32) { var rc int32 _ = rc rc = _sqlite3Prepare16(tls, db, zSql, nBytes, uint32(SQLITE_PREPARE_SAVESQL)|prepFlags&uint32(SQLITE_PREPARE_MASK), ppStmt, pzTail) _ = libc.Int32FromInt32(0) /* VERIFY: F13021 */ return rc } /************** End of prepare.c *********************************************/ /************** Begin file select.c ******************************************/ /* ** 2001 September 15 ** ** 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 contains C code routines that are called by the parser ** to handle SELECT statements in SQLite. */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** An instance of the following object is used to record information about // ** how to process the DISTINCT keyword, to simplify passing that information // ** into the selectInnerLoop() routine. // */ type TDistinctCtx = struct { FisTnct Tu8 FeTnctType Tu8 FtabTnct int32 FaddrTnct int32 } type DistinctCtx = TDistinctCtx type TDistinctCtx1 = struct { FisTnct Tu8 FeTnctType Tu8 FtabTnct int32 FaddrTnct int32 } type DistinctCtx1 = TDistinctCtx1 // C documentation // // /* // ** An instance of the following object is used to record information about // ** the ORDER BY (or GROUP BY) clause of query is being coded. // ** // ** The aDefer[] array is used by the sorter-references optimization. For // ** example, assuming there is no index that can be used for the ORDER BY, // ** for the query: // ** // ** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10; // ** // ** it may be more efficient to add just the "a" values to the sorter, and // ** retrieve the associated "bigblob" values directly from table t1 as the // ** 10 smallest "a" values are extracted from the sorter. // ** // ** When the sorter-reference optimization is used, there is one entry in the // ** aDefer[] array for each database table that may be read as values are // ** extracted from the sorter. // */ type TSortCtx = struct { FpOrderBy uintptr FnOBSat int32 FiECursor int32 FregReturn int32 FlabelBkOut int32 FaddrSortIndex int32 FlabelDone int32 FlabelOBLopt int32 FsortFlags Tu8 FpDeferredRowLoad uintptr } type SortCtx = TSortCtx type TSortCtx1 = struct { FpOrderBy uintptr FnOBSat int32 FiECursor int32 FregReturn int32 FlabelBkOut int32 FaddrSortIndex int32 FlabelDone int32 FlabelOBLopt int32 FsortFlags Tu8 FpDeferredRowLoad uintptr } type SortCtx1 = TSortCtx1 // C documentation // // /* // ** Delete all the content of a Select structure. Deallocate the structure // ** itself depending on the value of bFree // ** // ** If bFree==1, call sqlite3DbFree() on the p object. // ** If bFree==0, Leave the first Select object unfreed // */ func _clearSelect(tls *libc.TLS, db uintptr, p uintptr, bFree int32) { var pPrior uintptr _ = pPrior _ = libc.Int32FromInt32(0) for p != 0 { pPrior = (*TSelect)(unsafe.Pointer(p)).FpPrior _sqlite3ExprListDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpEList) _sqlite3SrcListDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpSrc) _sqlite3ExprDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpWhere) _sqlite3ExprListDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpGroupBy) _sqlite3ExprDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpHaving) _sqlite3ExprListDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpOrderBy) _sqlite3ExprDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpLimit) if (*TSelect)(unsafe.Pointer(p)).FpWith != 0 { _sqlite3WithDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpWith) } if (*TSelect)(unsafe.Pointer(p)).FpWinDefn != 0 { _sqlite3WindowListDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpWinDefn) } for (*TSelect)(unsafe.Pointer(p)).FpWin != 0 { _ = libc.Int32FromInt32(0) _sqlite3WindowUnlinkFromSelect(tls, (*TSelect)(unsafe.Pointer(p)).FpWin) } if bFree != 0 { _sqlite3DbNNFreeNN(tls, db, p) } p = pPrior bFree = int32(1) } } // C documentation // // /* // ** Initialize a SelectDest structure. // */ func _sqlite3SelectDestInit(tls *libc.TLS, pDest uintptr, eDest int32, iParm int32) { (*TSelectDest)(unsafe.Pointer(pDest)).FeDest = uint8(eDest) (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm = iParm (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm2 = 0 (*TSelectDest)(unsafe.Pointer(pDest)).FzAffSdst = uintptr(0) (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst = 0 (*TSelectDest)(unsafe.Pointer(pDest)).FnSdst = 0 } // C documentation // // /* // ** Allocate a new Select structure and return a pointer to that // ** structure. // */ func _sqlite3SelectNew(tls *libc.TLS, pParse uintptr, pEList uintptr, pSrc uintptr, pWhere uintptr, pGroupBy uintptr, pHaving uintptr, pOrderBy uintptr, selFlags Tu32, pLimit uintptr) (r uintptr) { bp := tls.Alloc(80) defer tls.Free(80) var pAllocated, pNew, v1, v3 uintptr var v2 int32 var _ /* standin at bp+0 */ TSelect _, _, _, _, _ = pAllocated, pNew, v1, v2, v3 v1 = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(76)) pNew = v1 pAllocated = v1 if pNew == uintptr(0) { _ = libc.Int32FromInt32(0) pNew = bp } if pEList == uintptr(0) { pEList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), _sqlite3Expr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_ASTERISK), uintptr(0))) } (*TSelect)(unsafe.Pointer(pNew)).FpEList = pEList (*TSelect)(unsafe.Pointer(pNew)).Fop = uint8(TK_SELECT) (*TSelect)(unsafe.Pointer(pNew)).FselFlags = selFlags (*TSelect)(unsafe.Pointer(pNew)).FiLimit = 0 (*TSelect)(unsafe.Pointer(pNew)).FiOffset = 0 v3 = pParse + 108 *(*int32)(unsafe.Pointer(v3))++ v2 = *(*int32)(unsafe.Pointer(v3)) (*TSelect)(unsafe.Pointer(pNew)).FselId = uint32(v2) *(*int32)(unsafe.Pointer(pNew + 20)) = -int32(1) *(*int32)(unsafe.Pointer(pNew + 20 + 1*4)) = -int32(1) (*TSelect)(unsafe.Pointer(pNew)).FnSelectRow = 0 if pSrc == uintptr(0) { pSrc = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(80)) } (*TSelect)(unsafe.Pointer(pNew)).FpSrc = pSrc (*TSelect)(unsafe.Pointer(pNew)).FpWhere = pWhere (*TSelect)(unsafe.Pointer(pNew)).FpGroupBy = pGroupBy (*TSelect)(unsafe.Pointer(pNew)).FpHaving = pHaving (*TSelect)(unsafe.Pointer(pNew)).FpOrderBy = pOrderBy (*TSelect)(unsafe.Pointer(pNew)).FpPrior = uintptr(0) (*TSelect)(unsafe.Pointer(pNew)).FpNext = uintptr(0) (*TSelect)(unsafe.Pointer(pNew)).FpLimit = pLimit (*TSelect)(unsafe.Pointer(pNew)).FpWith = uintptr(0) (*TSelect)(unsafe.Pointer(pNew)).FpWin = uintptr(0) (*TSelect)(unsafe.Pointer(pNew)).FpWinDefn = uintptr(0) if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 { _clearSelect(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pNew, libc.BoolInt32(pNew != bp)) pAllocated = uintptr(0) } else { _ = libc.Int32FromInt32(0) } return pAllocated } // C documentation // // /* // ** Delete the given Select structure and all of its substructures. // */ func _sqlite3SelectDelete(tls *libc.TLS, db uintptr, p uintptr) { if p != 0 { _clearSelect(tls, db, p, int32(1)) } } func _sqlite3SelectDeleteGeneric(tls *libc.TLS, db uintptr, p uintptr) { if p != 0 { _clearSelect(tls, db, p, int32(1)) } } // C documentation // // /* // ** Return a pointer to the right-most SELECT statement in a compound. // */ func _findRightmost(tls *libc.TLS, p uintptr) (r uintptr) { for (*TSelect)(unsafe.Pointer(p)).FpNext != 0 { p = (*TSelect)(unsafe.Pointer(p)).FpNext } return p } // C documentation // // /* // ** Given 1 to 3 identifiers preceding the JOIN keyword, determine the // ** type of join. Return an integer constant that expresses that type // ** in terms of the following bit values: // ** // ** JT_INNER // ** JT_CROSS // ** JT_OUTER // ** JT_NATURAL // ** JT_LEFT // ** JT_RIGHT // ** // ** A full outer join is the combination of JT_LEFT and JT_RIGHT. // ** // ** If an illegal or unsupported join type is seen, then still return // ** a join type, but put an error in the pParse structure. // ** // ** These are the valid join types: // ** // ** // ** pA pB pC Return Value // ** ------- ----- ----- ------------ // ** CROSS - - JT_CROSS // ** INNER - - JT_INNER // ** LEFT - - JT_LEFT|JT_OUTER // ** LEFT OUTER - JT_LEFT|JT_OUTER // ** RIGHT - - JT_RIGHT|JT_OUTER // ** RIGHT OUTER - JT_RIGHT|JT_OUTER // ** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER // ** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER // ** NATURAL INNER - JT_NATURAL|JT_INNER // ** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER // ** NATURAL LEFT OUTER JT_NATURAL|JT_LEFT|JT_OUTER // ** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER // ** NATURAL RIGHT OUTER JT_NATURAL|JT_RIGHT|JT_OUTER // ** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT // ** NATURAL FULL OUTER JT_NATRUAL|JT_LEFT|JT_RIGHT // ** // ** To preserve historical compatibly, SQLite also accepts a variety // ** of other non-standard and in many cases nonsensical join types. // ** This routine makes as much sense at it can from the nonsense join // ** type and returns a result. Examples of accepted nonsense join types // ** include but are not limited to: // ** // ** INNER CROSS JOIN -> same as JOIN // ** NATURAL CROSS JOIN -> same as NATURAL JOIN // ** OUTER LEFT JOIN -> same as LEFT JOIN // ** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN // ** LEFT RIGHT JOIN -> same as FULL JOIN // ** RIGHT OUTER FULL JOIN -> same as FULL JOIN // ** CROSS CROSS CROSS JOIN -> same as JOIN // ** // ** The only restrictions on the join type name are: // ** // ** * "INNER" cannot appear together with "OUTER", "LEFT", "RIGHT", // ** or "FULL". // ** // ** * "CROSS" cannot appear together with "OUTER", "LEFT", "RIGHT, // ** or "FULL". // ** // ** * If "OUTER" is present then there must also be one of // ** "LEFT", "RIGHT", or "FULL" // */ func _sqlite3JoinType(tls *libc.TLS, pParse uintptr, pA uintptr, pB uintptr, pC uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var apAll [3]uintptr var i, j, jointype int32 var p, zSp1, zSp2 uintptr _, _, _, _, _, _, _ = apAll, i, j, jointype, p, zSp1, zSp2 jointype = 0 apAll[0] = pA apAll[int32(1)] = pB apAll[int32(2)] = pC i = 0 for { if !(i < int32(3) && apAll[i] != 0) { break } p = apAll[i] j = 0 for { if !(j < int32(libc.Uint32FromInt64(21)/libc.Uint32FromInt64(3))) { break } if (*TToken)(unsafe.Pointer(p)).Fn == uint32(_aKeyword[j].FnChar) && Xsqlite3_strnicmp(tls, (*TToken)(unsafe.Pointer(p)).Fz, uintptr(unsafe.Pointer(&_zKeyText))+uintptr(_aKeyword[j].Fi), int32((*TToken)(unsafe.Pointer(p)).Fn)) == 0 { jointype |= int32(_aKeyword[j].Fcode) break } goto _2 _2: ; j++ } if j >= int32(libc.Uint32FromInt64(21)/libc.Uint32FromInt64(3)) { jointype |= int32(JT_ERROR) break } goto _1 _1: ; i++ } if jointype&(libc.Int32FromInt32(JT_INNER)|libc.Int32FromInt32(JT_OUTER)) == libc.Int32FromInt32(JT_INNER)|libc.Int32FromInt32(JT_OUTER) || jointype&int32(JT_ERROR) != 0 || jointype&(libc.Int32FromInt32(JT_OUTER)|libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_RIGHT)) == int32(JT_OUTER) { zSp1 = __ccgo_ts + 11202 zSp2 = __ccgo_ts + 11202 if pB == uintptr(0) { zSp1++ } if pC == uintptr(0) { zSp2++ } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+18933, libc.VaList(bp+8, pA, zSp1, pB, zSp2, pC)) jointype = int32(JT_INNER) } return jointype } /* 0123456789 123456789 123456789 123 */ var _zKeyText = [34]uint8{'n', 'a', 't', 'u', 'r', 'a', 'l', 'e', 'f', 't', 'o', 'u', 't', 'e', 'r', 'i', 'g', 'h', 't', 'f', 'u', 'l', 'l', 'i', 'n', 'n', 'e', 'r', 'c', 'r', 'o', 's', 's'} var _aKeyword = [7]struct { Fi Tu8 FnChar Tu8 Fcode Tu8 }{ 0: { FnChar: uint8(7), Fcode: uint8(JT_NATURAL), }, 1: { Fi: uint8(6), FnChar: uint8(4), Fcode: uint8(libc.Int32FromInt32(JT_LEFT) | libc.Int32FromInt32(JT_OUTER)), }, 2: { Fi: uint8(10), FnChar: uint8(5), Fcode: uint8(JT_OUTER), }, 3: { Fi: uint8(14), FnChar: uint8(5), Fcode: uint8(libc.Int32FromInt32(JT_RIGHT) | libc.Int32FromInt32(JT_OUTER)), }, 4: { Fi: uint8(19), FnChar: uint8(4), Fcode: uint8(libc.Int32FromInt32(JT_LEFT) | libc.Int32FromInt32(JT_RIGHT) | libc.Int32FromInt32(JT_OUTER)), }, 5: { Fi: uint8(23), FnChar: uint8(5), Fcode: uint8(JT_INNER), }, 6: { Fi: uint8(28), FnChar: uint8(5), Fcode: uint8(libc.Int32FromInt32(JT_INNER) | libc.Int32FromInt32(JT_CROSS)), }, } // C documentation // // /* // ** Return the index of a column in a table. Return -1 if the column // ** is not contained in the table. // */ func _sqlite3ColumnIndex(tls *libc.TLS, pTab uintptr, zCol uintptr) (r int32) { var h Tu8 var i int32 var pCol uintptr _, _, _ = h, i, pCol h = _sqlite3StrIHash(tls, zCol) pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol i = libc.Int32FromInt32(0) for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if int32((*TColumn)(unsafe.Pointer(pCol)).FhName) == int32(h) && _sqlite3StrICmp(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, zCol) == 0 { return i } goto _1 _1: ; pCol += 12 i++ } return -int32(1) } // C documentation // // /* // ** Mark a subquery result column as having been used. // */ func _sqlite3SrcItemColumnUsed(tls *libc.TLS, pItem uintptr, iCol int32) { var pResults uintptr _ = pResults _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x2000>>13)) != 0 { _ = libc.Int32FromInt32(0) pResults = (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpSelect)).FpEList _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) libc.SetBitFieldPtr16Uint32(pResults+8+uintptr(iCol)*20+8+4, libc.Uint32FromInt32(1), 6, 0x40) } } // C documentation // // /* // ** Search the tables iStart..iEnd (inclusive) in pSrc, looking for a // ** table that has a column named zCol. The search is left-to-right. // ** The first match found is returned. // ** // ** When found, set *piTab and *piCol to the table index and column index // ** of the matching column and return TRUE. // ** // ** If not found, return FALSE. // */ func _tableAndColumnIndex(tls *libc.TLS, pSrc uintptr, iStart int32, iEnd int32, zCol uintptr, piTab uintptr, piCol uintptr, bIgnoreHidden int32) (r int32) { var i, iCol int32 _, _ = i, iCol /* Index of column matching zCol */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Both or neither are NULL */ i = iStart for { if !(i <= iEnd) { break } iCol = _sqlite3ColumnIndex(tls, (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72))).FpTab, zCol) if iCol >= 0 && (bIgnoreHidden == 0 || libc.BoolInt32(int32((*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72))).FpTab)).FaCol+uintptr(iCol)*12)).FcolFlags)&int32(COLFLAG_HIDDEN) != 0) == 0) { if piTab != 0 { _sqlite3SrcItemColumnUsed(tls, pSrc+8+uintptr(i)*72, iCol) *(*int32)(unsafe.Pointer(piTab)) = i *(*int32)(unsafe.Pointer(piCol)) = iCol } return int32(1) } goto _1 _1: ; i++ } return 0 } // C documentation // // /* // ** Set the EP_OuterON property on all terms of the given expression. // ** And set the Expr.w.iJoin to iTable for every term in the // ** expression. // ** // ** The EP_OuterON property is used on terms of an expression to tell // ** the OUTER JOIN processing logic that this term is part of the // ** join restriction specified in the ON or USING clause and not a part // ** of the more general WHERE clause. These terms are moved over to the // ** WHERE clause during join processing but we need to remember that they // ** originated in the ON or USING clause. // ** // ** The Expr.w.iJoin tells the WHERE clause processing that the // ** expression depends on table w.iJoin even if that table is not // ** explicitly mentioned in the expression. That information is needed // ** for cases like this: // ** // ** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5 // ** // ** The where clause needs to defer the handling of the t1.x=5 // ** term until after the t2 loop of the join. In that way, a // ** NULL t2 row will be inserted whenever t1.x!=5. If we do not // ** defer the handling of t1.x=5, it will be processed immediately // ** after the t1 loop and rows with t1.x!=5 will never appear in // ** the output, which is incorrect. // */ func _sqlite3SetJoinExpr(tls *libc.TLS, p uintptr, iTable int32, joinFlag Tu32) { var i int32 _ = i _ = libc.Int32FromInt32(0) for p != 0 { *(*Tu32)(unsafe.Pointer(p + 4)) |= joinFlag _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(p + 36)) = iTable if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_FUNCTION) { _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(p + 20)) != 0 { i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)))).FnExpr) { break } _sqlite3SetJoinExpr(tls, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)) + 8 + uintptr(i)*20))).FpExpr, iTable, joinFlag) goto _1 _1: ; i++ } } } _sqlite3SetJoinExpr(tls, (*TExpr)(unsafe.Pointer(p)).FpLeft, iTable, joinFlag) p = (*TExpr)(unsafe.Pointer(p)).FpRight } } // C documentation // // /* Undo the work of sqlite3SetJoinExpr(). This is used when a LEFT JOIN // ** is simplified into an ordinary JOIN, and when an ON expression is // ** "pushed down" into the WHERE clause of a subquery. // ** // ** Convert every term that is marked with EP_OuterON and w.iJoin==iTable into // ** an ordinary term that omits the EP_OuterON mark. Or if iTable<0, then // ** just clear every EP_OuterON and EP_InnerON mark from the expression tree. // ** // ** If nullable is true, that means that Expr p might evaluate to NULL even // ** if it is a reference to a NOT NULL column. This can happen, for example, // ** if the table that p references is on the left side of a RIGHT JOIN. // ** If nullable is true, then take care to not remove the EP_CanBeNull bit. // ** See forum thread https://sqlite.org/forum/forumpost/b40696f50145d21c // */ func _unsetJoinExpr(tls *libc.TLS, p uintptr, iTable int32, nullable int32) { var i int32 _ = i for p != 0 { if iTable < 0 || (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) && *(*int32)(unsafe.Pointer(p + 36)) == iTable { *(*Tu32)(unsafe.Pointer(p + 4)) &= uint32(^(libc.Int32FromInt32(EP_OuterON) | libc.Int32FromInt32(EP_InnerON))) if iTable >= 0 { *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(libc.Int32FromInt32(EP_InnerON)) } } if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(p)).FiTable == iTable && !(nullable != 0) { *(*Tu32)(unsafe.Pointer(p + 4)) &= uint32(^libc.Int32FromInt32(EP_CanBeNull)) } if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_FUNCTION) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(p + 20)) != 0 { i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)))).FnExpr) { break } _unsetJoinExpr(tls, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)) + 8 + uintptr(i)*20))).FpExpr, iTable, nullable) goto _1 _1: ; i++ } } } _unsetJoinExpr(tls, (*TExpr)(unsafe.Pointer(p)).FpLeft, iTable, nullable) p = (*TExpr)(unsafe.Pointer(p)).FpRight } } // C documentation // // /* // ** This routine processes the join information for a SELECT statement. // ** // ** * A NATURAL join is converted into a USING join. After that, we // ** do not need to be concerned with NATURAL joins and we only have // ** think about USING joins. // ** // ** * ON and USING clauses result in extra terms being added to the // ** WHERE clause to enforce the specified constraints. The extra // ** WHERE clause terms will be tagged with EP_OuterON or // ** EP_InnerON so that we know that they originated in ON/USING. // ** // ** The terms of a FROM clause are contained in the Select.pSrc structure. // ** The left most table is the first entry in Select.pSrc. The right-most // ** table is the last entry. The join operator is held in the entry to // ** the right. Thus entry 1 contains the join operator for the join between // ** entries 0 and 1. Any ON or USING clauses associated with the join are // ** also attached to the right entry. // ** // ** This routine returns the number of errors encountered. // */ func _sqlite3ProcessJoin(tls *libc.TLS, pParse uintptr, p uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var db, pE1, pE2, pEq, pFuncArgs, pLeft, pList, pRight, pRightTab, pSrc, pUsing, zName, zName1 uintptr var i, iRightCol, j, v2 int32 var joinType Tu32 var _ /* iLeft at bp+0 */ int32 var _ /* iLeftCol at bp+4 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, i, iRightCol, j, joinType, pE1, pE2, pEq, pFuncArgs, pLeft, pList, pRight, pRightTab, pSrc, pUsing, zName, zName1, v2 /* Right table being joined */ pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc pLeft = pSrc + 8 pRight = pLeft + 1*72 i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc-int32(1)) { break } pRightTab = (*TSrcItem)(unsafe.Pointer(pRight)).FpTab if (*TSrcItem)(unsafe.Pointer(pLeft)).FpTab == uintptr(0) || pRightTab == uintptr(0) { goto _1 } if int32((*TSrcItem)(unsafe.Pointer(pRight)).Ffg.Fjointype)&int32(JT_OUTER) != 0 { v2 = int32(EP_OuterON) } else { v2 = int32(EP_InnerON) } joinType = uint32(v2) /* If this is a NATURAL join, synthesize an appropriate USING clause ** to specify which columns should be joined. */ if int32((*TSrcItem)(unsafe.Pointer(pRight)).Ffg.Fjointype)&int32(JT_NATURAL) != 0 { pUsing = uintptr(0) if int32(uint32(*(*uint16)(unsafe.Pointer(pRight + 36 + 4))&0x400>>10)) != 0 || *(*uintptr)(unsafe.Pointer(pRight + 48)) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+18963, libc.VaList(bp+16, 0)) return int32(1) } j = 0 for { if !(j < int32((*TTable)(unsafe.Pointer(pRightTab)).FnCol)) { break } /* Name of column in the right table */ if int32((*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pRightTab)).FaCol+uintptr(j)*12)).FcolFlags)&int32(COLFLAG_HIDDEN) != 0 { goto _3 } zName = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pRightTab)).FaCol + uintptr(j)*12))).FzCnName if _tableAndColumnIndex(tls, pSrc, 0, i, zName, uintptr(0), uintptr(0), int32(1)) != 0 { pUsing = _sqlite3IdListAppend(tls, pParse, pUsing, uintptr(0)) if pUsing != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*(*TIdList_item)(unsafe.Pointer(pUsing + 8 + uintptr((*TIdList)(unsafe.Pointer(pUsing)).FnId-int32(1))*8))).FzName = _sqlite3DbStrDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, zName) } } goto _3 _3: ; j++ } if pUsing != 0 { libc.SetBitFieldPtr16Uint32(pRight+36+4, libc.Uint32FromInt32(1), 10, 0x400) libc.SetBitFieldPtr16Uint32(pRight+36+4, libc.Uint32FromInt32(1), 12, 0x1000) *(*uintptr)(unsafe.Pointer(pRight + 48)) = pUsing } if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return int32(1) } } /* Create extra terms on the WHERE clause for each column named ** in the USING clause. Example: If the two tables to be joined are ** A and B and the USING clause names X, Y, and Z, then add this ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z ** Report an error if any column mentioned in the USING clause is ** not contained in both tables to be joined. */ if int32(uint32(*(*uint16)(unsafe.Pointer(pRight + 36 + 4))&0x400>>10)) != 0 { pList = *(*uintptr)(unsafe.Pointer(pRight + 48)) db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) j = 0 for { if !(j < (*TIdList)(unsafe.Pointer(pList)).FnId) { break } /* Equality constraint. pE1 == pE2 */ zName1 = (*(*TIdList_item)(unsafe.Pointer(pList + 8 + uintptr(j)*8))).FzName iRightCol = _sqlite3ColumnIndex(tls, pRightTab, zName1) if iRightCol < 0 || _tableAndColumnIndex(tls, pSrc, 0, i, zName1, bp, bp+4, int32(uint32(*(*uint16)(unsafe.Pointer(pRight + 36 + 4))&0x1000>>12))) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19013, libc.VaList(bp+16, zName1)) return int32(1) } pE1 = _sqlite3CreateColumnExpr(tls, db, pSrc, *(*int32)(unsafe.Pointer(bp)), *(*int32)(unsafe.Pointer(bp + 4))) _sqlite3SrcItemColumnUsed(tls, pSrc+8+uintptr(*(*int32)(unsafe.Pointer(bp)))*72, *(*int32)(unsafe.Pointer(bp + 4))) if int32((*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 { /* This branch runs if the query contains one or more RIGHT or FULL ** JOINs. If only a single table on the left side of this join ** contains the zName column, then this branch is a no-op. ** But if there are two or more tables on the left side ** of the join, construct a coalesce() function that gathers all ** such tables. Raise an error if more than one of those references ** to zName is not also within a prior USING clause. ** ** We really ought to raise an error if there are two or more ** non-USING references to zName on the left of an INNER or LEFT ** JOIN. But older versions of SQLite do not do that, so we avoid ** adding a new error so as to not break legacy applications. */ pFuncArgs = uintptr(0) /* Arguments to the coalesce() */ for _tableAndColumnIndex(tls, pSrc, *(*int32)(unsafe.Pointer(bp))+int32(1), i, zName1, bp, bp+4, int32(uint32(*(*uint16)(unsafe.Pointer(pRight + 36 + 4))&0x1000>>12))) != 0 { if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 8 + uintptr(*(*int32)(unsafe.Pointer(bp)))*72 + 36 + 4))&0x400>>10)) == 0 || _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pSrc + 8 + uintptr(*(*int32)(unsafe.Pointer(bp)))*72 + 48)), zName1) < 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19077, libc.VaList(bp+16, zName1)) break } pFuncArgs = _sqlite3ExprListAppend(tls, pParse, pFuncArgs, pE1) pE1 = _sqlite3CreateColumnExpr(tls, db, pSrc, *(*int32)(unsafe.Pointer(bp)), *(*int32)(unsafe.Pointer(bp + 4))) _sqlite3SrcItemColumnUsed(tls, pSrc+8+uintptr(*(*int32)(unsafe.Pointer(bp)))*72, *(*int32)(unsafe.Pointer(bp + 4))) } if pFuncArgs != 0 { pFuncArgs = _sqlite3ExprListAppend(tls, pParse, pFuncArgs, pE1) pE1 = _sqlite3ExprFunction(tls, pParse, pFuncArgs, uintptr(unsafe.Pointer(&_tkCoalesce)), 0) } } pE2 = _sqlite3CreateColumnExpr(tls, db, pSrc, i+int32(1), iRightCol) _sqlite3SrcItemColumnUsed(tls, pRight, iRightCol) pEq = _sqlite3PExpr(tls, pParse, int32(TK_EQ), pE1, pE2) _ = libc.Int32FromInt32(0) if pEq != 0 { *(*Tu32)(unsafe.Pointer(pEq + 4)) |= joinType _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pEq + 36)) = (*TExpr)(unsafe.Pointer(pE2)).FiTable } (*TSelect)(unsafe.Pointer(p)).FpWhere = _sqlite3ExprAnd(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpWhere, pEq) goto _4 _4: ; j++ } } else { if *(*uintptr)(unsafe.Pointer(pRight + 48)) != 0 { _sqlite3SetJoinExpr(tls, *(*uintptr)(unsafe.Pointer(pRight + 48)), (*TSrcItem)(unsafe.Pointer(pRight)).FiCursor, joinType) (*TSelect)(unsafe.Pointer(p)).FpWhere = _sqlite3ExprAnd(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpWhere, *(*uintptr)(unsafe.Pointer(pRight + 48))) *(*uintptr)(unsafe.Pointer(pRight + 48)) = uintptr(0) libc.SetBitFieldPtr16Uint32(pRight+36+4, libc.Uint32FromInt32(1), 11, 0x800) } } goto _1 _1: ; i++ pRight += 72 pLeft += 72 } return 0 } var _tkCoalesce = TToken{ Fz: __ccgo_ts + 6730, Fn: uint32(8), } // C documentation // // /* // ** An instance of this object holds information (beyond pParse and pSelect) // ** needed to load the next result row that is to be added to the sorter. // */ type TRowLoadInfo = struct { FregResult int32 FecelFlags Tu8 } type RowLoadInfo = TRowLoadInfo type TRowLoadInfo1 = struct { FregResult int32 FecelFlags Tu8 } type RowLoadInfo1 = TRowLoadInfo1 // C documentation // // /* // ** This routine does the work of loading query data into an array of // ** registers so that it can be added to the sorter. // */ func _innerLoopLoadRow(tls *libc.TLS, pParse uintptr, pSelect uintptr, pInfo uintptr) { _sqlite3ExprCodeExprList(tls, pParse, (*TSelect)(unsafe.Pointer(pSelect)).FpEList, (*TRowLoadInfo)(unsafe.Pointer(pInfo)).FregResult, 0, (*TRowLoadInfo)(unsafe.Pointer(pInfo)).FecelFlags) } // C documentation // // /* // ** Code the OP_MakeRecord instruction that generates the entry to be // ** added into the sorter. // ** // ** Return the register in which the result is stored. // */ func _makeSorterRecord(tls *libc.TLS, pParse uintptr, pSort uintptr, pSelect uintptr, regBase int32, nBase int32) (r int32) { var nOBSat, regOut, v1 int32 var v, v2 uintptr _, _, _, _, _ = nOBSat, regOut, v, v1, v2 nOBSat = (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe v2 = pParse + 44 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) regOut = v1 if (*TSortCtx)(unsafe.Pointer(pSort)).FpDeferredRowLoad != 0 { _innerLoopLoadRow(tls, pParse, pSelect, (*TSortCtx)(unsafe.Pointer(pSort)).FpDeferredRowLoad) } _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regBase+nOBSat, nBase-nOBSat, regOut) return regOut } // C documentation // // /* // ** Generate code that will push the record in registers regData // ** through regData+nData-1 onto the sorter. // */ func _pushOntoSorter(tls *libc.TLS, pParse uintptr, pSort uintptr, pSelect uintptr, regData int32, regOrigData int32, nData int32, nPrefixReg int32) { var addrFirst, addrJmp, bSeq, iCsr, iLimit, iSkip, nBase, nExpr, nKey, nOBSat, op, regBase, regPrevKey, regRecord, v1, v2, v3, v5 int32 var pKI, pOp, v, v4 uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrFirst, addrJmp, bSeq, iCsr, iLimit, iSkip, nBase, nExpr, nKey, nOBSat, op, pKI, pOp, regBase, regPrevKey, regRecord, v, v1, v2, v3, v4, v5 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* Stmt under construction */ bSeq = libc.BoolInt32(int32((*TSortCtx)(unsafe.Pointer(pSort)).FsortFlags)&int32(SORTFLAG_UseSorter) == 0) nExpr = (*TExprList)(unsafe.Pointer((*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy)).FnExpr /* No. of ORDER BY terms */ nBase = nExpr + bSeq + nData /* Regs for sorter record */ regRecord = 0 /* Assembled sorter record */ nOBSat = (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat /* LIMIT counter */ iSkip = 0 /* End of the sorter insert loop */ _ = libc.Int32FromInt32(0) /* Three cases: ** (1) The data to be sorted has already been packed into a Record ** by a prior OP_MakeRecord. In this case nData==1 and regData ** will be completely unrelated to regOrigData. ** (2) All output columns are included in the sort record. In that ** case regData==regOrigData. ** (3) Some output columns are omitted from the sort record due to ** the SQLITE_ENABLE_SORTER_REFERENCES optimization, or due to the ** SQLITE_ECEL_OMITREF optimization, or due to the ** SortCtx.pDeferredRowLoad optimization. In any of these cases ** regOrigData is 0 to prevent this routine from trying to copy ** values that might not yet exist. */ _ = libc.Int32FromInt32(0) if nPrefixReg != 0 { _ = libc.Int32FromInt32(0) regBase = regData - nPrefixReg } else { regBase = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nBase } _ = libc.Int32FromInt32(0) if (*TSelect)(unsafe.Pointer(pSelect)).FiOffset != 0 { v1 = (*TSelect)(unsafe.Pointer(pSelect)).FiOffset + int32(1) } else { v1 = (*TSelect)(unsafe.Pointer(pSelect)).FiLimit } iLimit = v1 (*TSortCtx)(unsafe.Pointer(pSort)).FlabelDone = _sqlite3VdbeMakeLabel(tls, pParse) if regOrigData != 0 { v2 = int32(SQLITE_ECEL_REF) } else { v2 = 0 } _sqlite3ExprCodeExprList(tls, pParse, (*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy, regBase, regOrigData, uint8(int32(SQLITE_ECEL_DUP)|v2)) if bSeq != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Sequence), (*TSortCtx)(unsafe.Pointer(pSort)).FiECursor, regBase+nExpr) } if nPrefixReg == 0 && nData > 0 { _sqlite3ExprCodeMove(tls, pParse, regData, regBase+nExpr+bSeq, nData) } if nOBSat > 0 { /* Original KeyInfo on the sorter table */ regRecord = _makeSorterRecord(tls, pParse, pSort, pSelect, regBase, nBase) regPrevKey = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat nKey = nExpr - (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat + bSeq if bSeq != 0 { addrFirst = _sqlite3VdbeAddOp1(tls, v, int32(OP_IfNot), regBase+nExpr) } else { addrFirst = _sqlite3VdbeAddOp1(tls, v, int32(OP_SequenceTest), (*TSortCtx)(unsafe.Pointer(pSort)).FiECursor) } _sqlite3VdbeAddOp3(tls, v, int32(OP_Compare), regPrevKey, regBase, (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat) pOp = _sqlite3VdbeGetOp(tls, v, (*TSortCtx)(unsafe.Pointer(pSort)).FaddrSortIndex) if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 { return } (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 = nKey + nData pKI = *(*uintptr)(unsafe.Pointer(pOp + 16)) libc.Xmemset(tls, (*TKeyInfo)(unsafe.Pointer(pKI)).FaSortFlags, 0, uint32((*TKeyInfo)(unsafe.Pointer(pKI)).FnKeyField)) /* Makes OP_Jump testable */ _sqlite3VdbeChangeP4(tls, v, -int32(1), pKI, -int32(8)) *(*uintptr)(unsafe.Pointer(pOp + 16)) = _sqlite3KeyInfoFromExprList(tls, pParse, (*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy, nOBSat, int32((*TKeyInfo)(unsafe.Pointer(pKI)).FnAllField)-int32((*TKeyInfo)(unsafe.Pointer(pKI)).FnKeyField)-int32(1)) pOp = uintptr(0) /* Ensure pOp not used after sqlite3VdbeAddOp3() */ addrJmp = _sqlite3VdbeCurrentAddr(tls, v) _sqlite3VdbeAddOp3(tls, v, int32(OP_Jump), addrJmp+int32(1), 0, addrJmp+int32(1)) (*TSortCtx)(unsafe.Pointer(pSort)).FlabelBkOut = _sqlite3VdbeMakeLabel(tls, pParse) v4 = pParse + 44 *(*int32)(unsafe.Pointer(v4))++ v3 = *(*int32)(unsafe.Pointer(v4)) (*TSortCtx)(unsafe.Pointer(pSort)).FregReturn = v3 _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TSortCtx)(unsafe.Pointer(pSort)).FregReturn, (*TSortCtx)(unsafe.Pointer(pSort)).FlabelBkOut) _sqlite3VdbeAddOp1(tls, v, int32(OP_ResetSorter), (*TSortCtx)(unsafe.Pointer(pSort)).FiECursor) if iLimit != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_IfNot), iLimit, (*TSortCtx)(unsafe.Pointer(pSort)).FlabelDone) } _sqlite3VdbeJumpHere(tls, v, addrFirst) _sqlite3ExprCodeMove(tls, pParse, regBase, regPrevKey, (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat) _sqlite3VdbeJumpHere(tls, v, addrJmp) } if iLimit != 0 { /* At this point the values for the new sorter entry are stored ** in an array of registers. They need to be composed into a record ** and inserted into the sorter if either (a) there are currently ** less than LIMIT+OFFSET items or (b) the new record is smaller than ** the largest record currently in the sorter. If (b) is true and there ** are already LIMIT+OFFSET items in the sorter, delete the largest ** entry before inserting the new one. This way there are never more ** than LIMIT+OFFSET items in the sorter. ** ** If the new record does not need to be inserted into the sorter, ** jump to the next iteration of the loop. If the pSort->labelOBLopt ** value is not zero, then it is a label of where to jump. Otherwise, ** just bypass the row insert logic. See the header comment on the ** sqlite3WhereOrderByLimitOptLabel() function for additional info. */ iCsr = (*TSortCtx)(unsafe.Pointer(pSort)).FiECursor _sqlite3VdbeAddOp2(tls, v, int32(OP_IfNotZero), iLimit, _sqlite3VdbeCurrentAddr(tls, v)+int32(4)) _sqlite3VdbeAddOp2(tls, v, int32(OP_Last), iCsr, 0) iSkip = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxLE), iCsr, 0, regBase+nOBSat, nExpr-nOBSat) _sqlite3VdbeAddOp1(tls, v, int32(OP_Delete), iCsr) } if regRecord == 0 { regRecord = _makeSorterRecord(tls, pParse, pSort, pSelect, regBase, nBase) } if int32((*TSortCtx)(unsafe.Pointer(pSort)).FsortFlags)&int32(SORTFLAG_UseSorter) != 0 { op = int32(OP_SorterInsert) } else { op = int32(OP_IdxInsert) } _sqlite3VdbeAddOp4Int(tls, v, op, (*TSortCtx)(unsafe.Pointer(pSort)).FiECursor, regRecord, regBase+nOBSat, nBase-nOBSat) if iSkip != 0 { if (*TSortCtx)(unsafe.Pointer(pSort)).FlabelOBLopt != 0 { v5 = (*TSortCtx)(unsafe.Pointer(pSort)).FlabelOBLopt } else { v5 = _sqlite3VdbeCurrentAddr(tls, v) } _sqlite3VdbeChangeP2(tls, v, iSkip, v5) } } // C documentation // // /* // ** Add code to implement the OFFSET // */ func _codeOffset(tls *libc.TLS, v uintptr, iOffset int32, iContinue int32) { if iOffset > 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_IfPos), iOffset, iContinue, int32(1)) } } // C documentation // // /* // ** Add code that will check to make sure the array of registers starting at // ** iMem form a distinct entry. This is used by both "SELECT DISTINCT ..." and // ** distinct aggregates ("SELECT count(DISTINCT ) ..."). Three strategies // ** are available. Which is used depends on the value of parameter eTnctType, // ** as follows: // ** // ** WHERE_DISTINCT_UNORDERED/WHERE_DISTINCT_NOOP: // ** Build an ephemeral table that contains all entries seen before and // ** skip entries which have been seen before. // ** // ** Parameter iTab is the cursor number of an ephemeral table that must // ** be opened before the VM code generated by this routine is executed. // ** The ephemeral cursor table is queried for a record identical to the // ** record formed by the current array of registers. If one is found, // ** jump to VM address addrRepeat. Otherwise, insert a new record into // ** the ephemeral cursor and proceed. // ** // ** The returned value in this case is a copy of parameter iTab. // ** // ** WHERE_DISTINCT_ORDERED: // ** In this case rows are being delivered sorted order. The ephemeral // ** table is not required. Instead, the current set of values // ** is compared against previous row. If they match, the new row // ** is not distinct and control jumps to VM address addrRepeat. Otherwise, // ** the VM program proceeds with processing the new row. // ** // ** The returned value in this case is the register number of the first // ** in an array of registers used to store the previous result row so that // ** it can be compared to the next. The caller must ensure that this // ** register is initialized to NULL. (The fixDistinctOpenEph() routine // ** will take care of this initialization.) // ** // ** WHERE_DISTINCT_UNIQUE: // ** In this case it has already been determined that the rows are distinct. // ** No special action is required. The return value is zero. // ** // ** Parameter pEList is the list of expressions used to generated the // ** contents of each row. It is used by this routine to determine (a) // ** how many elements there are in the array of registers and (b) the // ** collation sequences that should be used for the comparisons if // ** eTnctType is WHERE_DISTINCT_ORDERED. // */ func _codeDistinct(tls *libc.TLS, pParse uintptr, eTnctType int32, iTab int32, addrRepeat int32, pEList uintptr, regElem int32) (r int32) { var i, iJump, iRet, nResultCol, r1, regPrev, v1 int32 var pColl, v uintptr _, _, _, _, _, _, _, _, _ = i, iJump, iRet, nResultCol, pColl, r1, regPrev, v, v1 iRet = 0 nResultCol = (*TExprList)(unsafe.Pointer(pEList)).FnExpr v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe switch eTnctType { case int32(WHERE_DISTINCT_ORDERED): /* Previous row content */ /* Allocate space for the previous row */ v1 = (*TParse)(unsafe.Pointer(pParse)).FnMem + libc.Int32FromInt32(1) regPrev = v1 iRet = v1 *(*int32)(unsafe.Pointer(pParse + 44)) += nResultCol iJump = _sqlite3VdbeCurrentAddr(tls, v) + nResultCol i = 0 for { if !(i < nResultCol) { break } pColl = _sqlite3ExprCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FpExpr) if i < nResultCol-int32(1) { _sqlite3VdbeAddOp3(tls, v, int32(OP_Ne), regElem+i, iJump, regPrev+i) } else { _sqlite3VdbeAddOp3(tls, v, int32(OP_Eq), regElem+i, addrRepeat, regPrev+i) } _sqlite3VdbeChangeP4(tls, v, -int32(1), pColl, -int32(2)) _sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_NULLEQ)) goto _2 _2: ; i++ } _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regElem, regPrev, nResultCol-int32(1)) case int32(WHERE_DISTINCT_UNIQUE): /* nothing to do */ default: r1 = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), iTab, addrRepeat, regElem, nResultCol) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regElem, nResultCol, r1) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iTab, r1, regElem, nResultCol) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_USESEEKRESULT)) _sqlite3ReleaseTempReg(tls, pParse, r1) iRet = iTab break } return iRet } // C documentation // // /* // ** This routine runs after codeDistinct(). It makes necessary // ** adjustments to the OP_OpenEphemeral opcode that the codeDistinct() // ** routine made use of. This processing must be done separately since // ** sometimes codeDistinct is called before the OP_OpenEphemeral is actually // ** laid down. // ** // ** WHERE_DISTINCT_NOOP: // ** WHERE_DISTINCT_UNORDERED: // ** // ** No adjustments necessary. This function is a no-op. // ** // ** WHERE_DISTINCT_UNIQUE: // ** // ** The ephemeral table is not needed. So change the // ** OP_OpenEphemeral opcode into an OP_Noop. // ** // ** WHERE_DISTINCT_ORDERED: // ** // ** The ephemeral table is not needed. But we do need register // ** iVal to be initialized to NULL. So change the OP_OpenEphemeral // ** into an OP_Null on the iVal register. // */ func _fixDistinctOpenEph(tls *libc.TLS, pParse uintptr, eTnctType int32, iVal int32, iOpenEphAddr int32) { var pOp, v uintptr _, _ = pOp, v if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 && (eTnctType == int32(WHERE_DISTINCT_UNIQUE) || eTnctType == int32(WHERE_DISTINCT_ORDERED)) { v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _sqlite3VdbeChangeToNoop(tls, v, iOpenEphAddr) if int32((*TVdbeOp)(unsafe.Pointer(_sqlite3VdbeGetOp(tls, v, iOpenEphAddr+int32(1)))).Fopcode) == int32(OP_Explain) { _sqlite3VdbeChangeToNoop(tls, v, iOpenEphAddr+int32(1)) } if eTnctType == int32(WHERE_DISTINCT_ORDERED) { /* Change the OP_OpenEphemeral to an OP_Null that sets the MEM_Cleared ** bit on the first register of the previous value. This will cause the ** OP_Ne added in codeDistinct() to always fail on the first iteration of ** the loop even if the first row is all NULLs. */ pOp = _sqlite3VdbeGetOp(tls, v, iOpenEphAddr) (*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode = uint8(OP_Null) (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = int32(1) (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 = iVal } } } // C documentation // // /* // ** This routine generates the code for the inside of the inner loop // ** of a SELECT. // ** // ** If srcTab is negative, then the p->pEList expressions // ** are evaluated in order to get the data for this row. If srcTab is // ** zero or more, then data is pulled from srcTab and p->pEList is used only // ** to get the number of columns and the collation sequence for each column. // */ func _selectInnerLoop(tls *libc.TLS, pParse uintptr, p uintptr, srcTab int32, pSort uintptr, pDistinct uintptr, pDest uintptr, iContinue int32, iBreak int32) { bp := tls.Alloc(16) defer tls.Free(16) var addr, addrTest, eDest, eType, hasDistinct, i, i2, iParm, iTab, j, nKey, nPrefixReg, nResultCol, r1, r11, r12, r13, r14, r2, r21, r3, regOrig, regResult, v1, v2, v5 int32 var ecelFlags Tu8 var pEList, pSO, v uintptr var _ /* sRowLoadInfo at bp+0 */ TRowLoadInfo _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, addrTest, eDest, eType, ecelFlags, hasDistinct, i, i2, iParm, iTab, j, nKey, nPrefixReg, nResultCol, pEList, pSO, r1, r11, r12, r13, r14, r2, r21, r3, regOrig, regResult, v, v1, v2, v5 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* True if the DISTINCT keyword is present */ eDest = int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) /* How to dispose of results */ iParm = (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm /* Number of result columns */ nPrefixReg = 0 /* Start of memory holding full result (or 0) */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pDistinct != 0 { v1 = int32((*TDistinctCtx)(unsafe.Pointer(pDistinct)).FeTnctType) } else { v1 = WHERE_DISTINCT_NOOP } hasDistinct = v1 if pSort != 0 && (*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy == uintptr(0) { pSort = uintptr(0) } if pSort == uintptr(0) && !(hasDistinct != 0) { _ = libc.Int32FromInt32(0) _codeOffset(tls, v, (*TSelect)(unsafe.Pointer(p)).FiOffset, iContinue) } /* Pull the requested columns. */ nResultCol = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr if (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst == 0 { if pSort != 0 { nPrefixReg = (*TExprList)(unsafe.Pointer((*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy)).FnExpr if !(int32((*TSortCtx)(unsafe.Pointer(pSort)).FsortFlags)&libc.Int32FromInt32(SORTFLAG_UseSorter) != 0) { nPrefixReg++ } *(*int32)(unsafe.Pointer(pParse + 44)) += nPrefixReg } (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nResultCol } else { if (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst+nResultCol > (*TParse)(unsafe.Pointer(pParse)).FnMem { /* This is an error condition that can result, for example, when a SELECT ** on the right-hand side of an INSERT contains more result columns than ** there are columns in the table on the left. The error will be caught ** and reported later. But we need to make sure enough memory is allocated ** to avoid other spurious errors in the meantime. */ *(*int32)(unsafe.Pointer(pParse + 44)) += nResultCol } } (*TSelectDest)(unsafe.Pointer(pDest)).FnSdst = nResultCol v2 = (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst regResult = v2 regOrig = v2 if srcTab >= 0 { i = 0 for { if !(i < nResultCol) { break } _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), srcTab, i, regResult+i) goto _3 _3: ; i++ } } else { if eDest != int32(SRT_Exists) { if eDest == int32(SRT_Mem) || eDest == int32(SRT_Output) || eDest == int32(SRT_Coroutine) { ecelFlags = uint8(SQLITE_ECEL_DUP) } else { ecelFlags = uint8(0) } if pSort != 0 && hasDistinct == 0 && eDest != int32(SRT_EphemTab) && eDest != int32(SRT_Table) { /* For each expression in p->pEList that is a copy of an expression in ** the ORDER BY clause (pSort->pOrderBy), set the associated ** iOrderByCol value to one more than the index of the ORDER BY ** expression within the sort-key that pushOntoSorter() will generate. ** This allows the p->pEList field to be omitted from the sorted record, ** saving space and CPU cycles. */ ecelFlags = Tu8(int32(ecelFlags) | (libc.Int32FromInt32(SQLITE_ECEL_OMITREF) | libc.Int32FromInt32(SQLITE_ECEL_REF))) i = (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat for { if !(i < (*TExprList)(unsafe.Pointer((*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy)).FnExpr) { break } v5 = int32(*(*Tu16)(unsafe.Pointer((*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy + 8 + uintptr(i)*20 + 16))) j = v5 if v5 > 0 { *(*Tu16)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList + 8 + uintptr(j-int32(1))*20 + 16)) = uint16(i + int32(1) - (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat) } goto _4 _4: ; i++ } /* Adjust nResultCol to account for columns that are omitted ** from the sorter by the optimizations in this branch */ pEList = (*TSelect)(unsafe.Pointer(p)).FpEList i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } if int32(*(*Tu16)(unsafe.Pointer(pEList + 8 + uintptr(i)*20 + 16))) > 0 { nResultCol-- regOrig = 0 } goto _6 _6: ; i++ } _ = libc.Int32FromInt32(0) } (*(*TRowLoadInfo)(unsafe.Pointer(bp))).FregResult = regResult (*(*TRowLoadInfo)(unsafe.Pointer(bp))).FecelFlags = ecelFlags if (*TSelect)(unsafe.Pointer(p)).FiLimit != 0 && int32(ecelFlags)&int32(SQLITE_ECEL_OMITREF) != 0 && nPrefixReg > 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TSortCtx)(unsafe.Pointer(pSort)).FpDeferredRowLoad = bp regOrig = 0 } else { _innerLoopLoadRow(tls, pParse, p, bp) } } } /* If the DISTINCT keyword was present on the SELECT statement ** and this row has been seen before, then do not make this row ** part of the result. */ if hasDistinct != 0 { eType = int32((*TDistinctCtx)(unsafe.Pointer(pDistinct)).FeTnctType) iTab = (*TDistinctCtx)(unsafe.Pointer(pDistinct)).FtabTnct _ = libc.Int32FromInt32(0) iTab = _codeDistinct(tls, pParse, eType, iTab, iContinue, (*TSelect)(unsafe.Pointer(p)).FpEList, regResult) _fixDistinctOpenEph(tls, pParse, eType, iTab, (*TDistinctCtx)(unsafe.Pointer(pDistinct)).FaddrTnct) if pSort == uintptr(0) { _codeOffset(tls, v, (*TSelect)(unsafe.Pointer(p)).FiOffset, iContinue) } } switch eDest { /* In this mode, write each query result to the key of the temporary ** table iParm. */ case int32(SRT_Union): r1 = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regResult, nResultCol, r1) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm, r1, regResult, nResultCol) _sqlite3ReleaseTempReg(tls, pParse, r1) break /* Construct a record from the query result, but instead of ** saving that record, use it as a key to delete elements from ** the temporary table iParm. */ fallthrough case int32(SRT_Except): _sqlite3VdbeAddOp3(tls, v, int32(OP_IdxDelete), iParm, regResult, nResultCol) break /* Store the result as data using a unique key. */ fallthrough case int32(SRT_Fifo): fallthrough case int32(SRT_DistFifo): fallthrough case int32(SRT_Table): fallthrough case int32(SRT_EphemTab): r11 = _sqlite3GetTempRange(tls, pParse, nPrefixReg+int32(1)) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regResult, nResultCol, r11+nPrefixReg) if eDest == int32(SRT_DistFifo) { /* If the destination is DistFifo, then cursor (iParm+1) is open ** on an ephemeral index. If the current row is already present ** in the index, do not write it to the output. If not, add the ** current row to the index and proceed with writing it to the ** output table as well. */ addr = _sqlite3VdbeCurrentAddr(tls, v) + int32(4) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), iParm+int32(1), addr, r11, 0) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm+int32(1), r11, regResult, nResultCol) _ = libc.Int32FromInt32(0) } if pSort != 0 { _ = libc.Int32FromInt32(0) _pushOntoSorter(tls, pParse, pSort, p, r11+nPrefixReg, regOrig, int32(1), nPrefixReg) } else { r2 = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), iParm, r2) _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iParm, r11, r2) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_APPEND)) _sqlite3ReleaseTempReg(tls, pParse, r2) } _sqlite3ReleaseTempRange(tls, pParse, r11, nPrefixReg+int32(1)) case int32(SRT_Upfrom): if pSort != 0 { _pushOntoSorter(tls, pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg) } else { i2 = (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm2 r12 = _sqlite3GetTempReg(tls, pParse) /* If the UPDATE FROM join is an aggregate that matches no rows, it ** might still be trying to return one row, because that is what ** aggregates do. Don't record that empty row in the output table. */ _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regResult, iBreak) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regResult+libc.BoolInt32(i2 < 0), nResultCol-libc.BoolInt32(i2 < 0), r12) if i2 < 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iParm, r12, regResult) } else { _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm, r12, regResult, i2) } } break /* If we are creating a set for an "expr IN (SELECT ...)" construct, ** then there should be a single item on the stack. Write this ** item into the set table with bogus data. */ fallthrough case int32(SRT_Set): if pSort != 0 { /* At first glance you would think we could optimize out the ** ORDER BY in this case since the order of entries in the set ** does not matter. But there might be a LIMIT clause, in which ** case the order does matter */ _pushOntoSorter(tls, pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg) } else { r13 = _sqlite3GetTempReg(tls, pParse) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), regResult, nResultCol, r13, (*TSelectDest)(unsafe.Pointer(pDest)).FzAffSdst, nResultCol) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm, r13, regResult, nResultCol) _sqlite3ReleaseTempReg(tls, pParse, r13) } break /* If any row exist in the result set, record that fact and abort. */ fallthrough case int32(SRT_Exists): _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), iParm) /* The LIMIT clause will terminate the loop for us */ break /* If this is a scalar select that is part of an expression, then ** store the results in the appropriate memory cell or array of ** memory cells and break out of the scan loop. */ fallthrough case int32(SRT_Mem): if pSort != 0 { _ = libc.Int32FromInt32(0) _pushOntoSorter(tls, pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* The LIMIT clause will jump out of the loop for us */ } case int32(SRT_Coroutine): /* Send data to a co-routine */ fallthrough case int32(SRT_Output): /* Return the results */ if pSort != 0 { _pushOntoSorter(tls, pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg) } else { if eDest == int32(SRT_Coroutine) { _sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), regResult, nResultCol) } } break /* Write the results into a priority queue that is order according to ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an ** index with pSO->nExpr+2 columns. Build a key using pSO for the first ** pSO->nExpr columns, then make sure all keys are unique by adding a ** final OP_Sequence column. The last column is the record as a blob. */ fallthrough case int32(SRT_DistQueue): fallthrough case int32(SRT_Queue): addrTest = 0 pSO = (*TSelectDest)(unsafe.Pointer(pDest)).FpOrderBy _ = libc.Int32FromInt32(0) nKey = (*TExprList)(unsafe.Pointer(pSO)).FnExpr r14 = _sqlite3GetTempReg(tls, pParse) r21 = _sqlite3GetTempRange(tls, pParse, nKey+int32(2)) r3 = r21 + nKey + int32(1) if eDest == int32(SRT_DistQueue) { /* If the destination is DistQueue, then cursor (iParm+1) is open ** on a second ephemeral index that holds all values every previously ** added to the queue. */ addrTest = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), iParm+int32(1), 0, regResult, nResultCol) } _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regResult, nResultCol, r3) if eDest == int32(SRT_DistQueue) { _sqlite3VdbeAddOp2(tls, v, int32(OP_IdxInsert), iParm+int32(1), r3) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_USESEEKRESULT)) } i = 0 for { if !(i < nKey) { break } _sqlite3VdbeAddOp2(tls, v, int32(OP_SCopy), regResult+int32(*(*Tu16)(unsafe.Pointer(pSO + 8 + uintptr(i)*20 + 16)))-int32(1), r21+i) goto _7 _7: ; i++ } _sqlite3VdbeAddOp2(tls, v, int32(OP_Sequence), iParm, r21+nKey) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), r21, nKey+int32(2), r14) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm, r14, r21, nKey+int32(2)) if addrTest != 0 { _sqlite3VdbeJumpHere(tls, v, addrTest) } _sqlite3ReleaseTempReg(tls, pParse, r14) _sqlite3ReleaseTempRange(tls, pParse, r21, nKey+int32(2)) break /* Discard the results. This is used for SELECT statements inside ** the body of a TRIGGER. The purpose of such selects is to call ** user-defined functions that have side effects. We do not care ** about the actual results of the select. */ fallthrough default: _ = libc.Int32FromInt32(0) break } /* Jump to the end of the loop if the LIMIT is reached. Except, if ** there is a sorter, in which case the sorter has already limited ** the output for us. */ if pSort == uintptr(0) && (*TSelect)(unsafe.Pointer(p)).FiLimit != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_DecrJumpZero), (*TSelect)(unsafe.Pointer(p)).FiLimit, iBreak) } } // C documentation // // /* // ** Allocate a KeyInfo object sufficient for an index of N key columns and // ** X extra columns. // */ func _sqlite3KeyInfoAlloc(tls *libc.TLS, db uintptr, N int32, X int32) (r uintptr) { var nExtra int32 var p uintptr _, _ = nExtra, p nExtra = int32(uint32(N+X)*(libc.Uint32FromInt64(4)+libc.Uint32FromInt32(1)) - uint32(4)) p = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(24)+uint32(nExtra))) if p != 0 { (*TKeyInfo)(unsafe.Pointer(p)).FaSortFlags = p + 20 + uintptr(N+X)*4 (*TKeyInfo)(unsafe.Pointer(p)).FnKeyField = uint16(N) (*TKeyInfo)(unsafe.Pointer(p)).FnAllField = uint16(N + X) (*TKeyInfo)(unsafe.Pointer(p)).Fenc = (*Tsqlite3)(unsafe.Pointer(db)).Fenc (*TKeyInfo)(unsafe.Pointer(p)).Fdb = db (*TKeyInfo)(unsafe.Pointer(p)).FnRef = uint32(1) libc.Xmemset(tls, p+1*24, 0, uint32(nExtra)) } else { return _sqlite3OomFault(tls, db) } return p } // C documentation // // /* // ** Deallocate a KeyInfo object // */ func _sqlite3KeyInfoUnref(tls *libc.TLS, p uintptr) { if p != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TKeyInfo)(unsafe.Pointer(p)).FnRef-- if (*TKeyInfo)(unsafe.Pointer(p)).FnRef == uint32(0) { _sqlite3DbNNFreeNN(tls, (*TKeyInfo)(unsafe.Pointer(p)).Fdb, p) } } } // C documentation // // /* // ** Make a new pointer to a KeyInfo object // */ func _sqlite3KeyInfoRef(tls *libc.TLS, p uintptr) (r uintptr) { if p != 0 { _ = libc.Int32FromInt32(0) (*TKeyInfo)(unsafe.Pointer(p)).FnRef++ } return p } // C documentation // // /* // ** Given an expression list, generate a KeyInfo structure that records // ** the collating sequence for each expression in that expression list. // ** // ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting // ** KeyInfo structure is appropriate for initializing a virtual index to // ** implement that clause. If the ExprList is the result set of a SELECT // ** then the KeyInfo structure is appropriate for initializing a virtual // ** index to implement a DISTINCT test. // ** // ** Space to hold the KeyInfo structure is obtained from malloc. The calling // ** function is responsible for seeing that this structure is eventually // ** freed. // */ func _sqlite3KeyInfoFromExprList(tls *libc.TLS, pParse uintptr, pList uintptr, iStart int32, nExtra int32) (r uintptr) { var db, pInfo, pItem uintptr var i, nExpr int32 _, _, _, _, _ = db, i, nExpr, pInfo, pItem db = (*TParse)(unsafe.Pointer(pParse)).Fdb nExpr = (*TExprList)(unsafe.Pointer(pList)).FnExpr pInfo = _sqlite3KeyInfoAlloc(tls, db, nExpr-iStart, nExtra+int32(1)) if pInfo != 0 { _ = libc.Int32FromInt32(0) i = iStart pItem = pList + 8 + uintptr(iStart)*20 for { if !(i < nExpr) { break } *(*uintptr)(unsafe.Pointer(pInfo + 20 + uintptr(i-iStart)*4)) = _sqlite3ExprNNCollSeq(tls, pParse, (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr) *(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pInfo)).FaSortFlags + uintptr(i-iStart))) = (*TExprList_item)(unsafe.Pointer(pItem)).Ffg.FsortFlags goto _1 _1: ; i++ pItem += 20 } } return pInfo } // C documentation // // /* // ** Name of the connection operator, used for error messages. // */ func _sqlite3SelectOpName(tls *libc.TLS, id int32) (r uintptr) { var z uintptr _ = z switch id { case int32(TK_ALL): z = __ccgo_ts + 19114 case int32(TK_INTERSECT): z = __ccgo_ts + 19124 case int32(TK_EXCEPT): z = __ccgo_ts + 19134 default: z = __ccgo_ts + 19141 break } return z } // C documentation // // /* // ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function // ** is a no-op. Otherwise, it adds a single row of output to the EQP result, // ** where the caption is of the form: // ** // ** "USE TEMP B-TREE FOR xxx" // ** // ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which // ** is determined by the zUsage argument. // */ func _explainTempTable(tls *libc.TLS, pParse uintptr, zUsage uintptr) { bp := tls.Alloc(16) defer tls.Free(16) _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+19147, libc.VaList(bp+8, zUsage)) } /* ** Assign expression b to lvalue a. A second, no-op, version of this macro ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code ** in sqlite3Select() to assign values to structure member variables that ** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the ** code with #ifndef directives. */ // C documentation // // /* // ** If the inner loop was generated using a non-null pOrderBy argument, // ** then the results were placed in a sorter. After the loop is terminated // ** we need to run the sorter and output the results. The following // ** routine generates the code needed to do that. // */ func _generateSortTail(tls *libc.TLS, pParse uintptr, p uintptr, pSort uintptr, nColumn int32, pDest uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var aOutEx, pOrderBy, v, v1, v3, v5 uintptr var addr, addrBreak, addrContinue, addrOnce, bSeq, eDest, i, i2, iCol, iParm, iRead, iSortTab, iTab, nKey, nRefKey, r1, regRow, regRowid, regSortOut, v2, v4, v8 int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aOutEx, addr, addrBreak, addrContinue, addrOnce, bSeq, eDest, i, i2, iCol, iParm, iRead, iSortTab, iTab, nKey, nRefKey, pOrderBy, r1, regRow, regRowid, regSortOut, v, v1, v2, v3, v4, v5, v8 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* The prepared statement */ addrBreak = (*TSortCtx)(unsafe.Pointer(pSort)).FlabelDone /* Jump here to exit loop */ addrContinue = _sqlite3VdbeMakeLabel(tls, pParse) /* Top of output loop. Jump for Next. */ addrOnce = 0 pOrderBy = (*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy eDest = int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) iParm = (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm /* True if sorter record includes seq. no. */ nRefKey = 0 aOutEx = (*TSelect)(unsafe.Pointer(p)).FpEList + 8 if (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat > 0 { v1 = __ccgo_ts + 19170 } else { v1 = __ccgo_ts + 1648 } _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+19185, libc.VaList(bp+8, v1)) _ = libc.Int32FromInt32(0) if (*TSortCtx)(unsafe.Pointer(pSort)).FlabelBkOut != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TSortCtx)(unsafe.Pointer(pSort)).FregReturn, (*TSortCtx)(unsafe.Pointer(pSort)).FlabelBkOut) _sqlite3VdbeGoto(tls, v, addrBreak) _sqlite3VdbeResolveLabel(tls, v, (*TSortCtx)(unsafe.Pointer(pSort)).FlabelBkOut) } iTab = (*TSortCtx)(unsafe.Pointer(pSort)).FiECursor if eDest == int32(SRT_Output) || eDest == int32(SRT_Coroutine) || eDest == int32(SRT_Mem) { if eDest == int32(SRT_Mem) && (*TSelect)(unsafe.Pointer(p)).FiOffset != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst) } regRowid = 0 regRow = (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst } else { regRowid = _sqlite3GetTempReg(tls, pParse) if eDest == int32(SRT_EphemTab) || eDest == int32(SRT_Table) { regRow = _sqlite3GetTempReg(tls, pParse) nColumn = 0 } else { regRow = _sqlite3GetTempRange(tls, pParse, nColumn) } } nKey = (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr - (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat if int32((*TSortCtx)(unsafe.Pointer(pSort)).FsortFlags)&int32(SORTFLAG_UseSorter) != 0 { v3 = pParse + 44 *(*int32)(unsafe.Pointer(v3))++ v2 = *(*int32)(unsafe.Pointer(v3)) regSortOut = v2 v5 = pParse + 40 v4 = *(*int32)(unsafe.Pointer(v5)) *(*int32)(unsafe.Pointer(v5))++ iSortTab = v4 if (*TSortCtx)(unsafe.Pointer(pSort)).FlabelBkOut != 0 { addrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once)) } _sqlite3VdbeAddOp3(tls, v, int32(OP_OpenPseudo), iSortTab, regSortOut, nKey+int32(1)+nColumn+nRefKey) if addrOnce != 0 { _sqlite3VdbeJumpHere(tls, v, addrOnce) } addr = int32(1) + _sqlite3VdbeAddOp2(tls, v, int32(OP_SorterSort), iTab, addrBreak) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, int32(OP_SorterData), iTab, regSortOut, iSortTab) bSeq = 0 } else { addr = int32(1) + _sqlite3VdbeAddOp2(tls, v, int32(OP_Sort), iTab, addrBreak) _codeOffset(tls, v, (*TSelect)(unsafe.Pointer(p)).FiOffset, addrContinue) iSortTab = iTab bSeq = int32(1) if (*TSelect)(unsafe.Pointer(p)).FiOffset > 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), (*TSelect)(unsafe.Pointer(p)).FiLimit, -int32(1)) } } i = 0 iCol = nKey + bSeq - libc.Int32FromInt32(1) for { if !(i < nColumn) { break } if int32(*(*Tu16)(unsafe.Pointer(aOutEx + uintptr(i)*20 + 16))) == 0 { iCol++ } goto _6 _6: ; i++ } i = nColumn - int32(1) for { if !(i >= 0) { break } if *(*Tu16)(unsafe.Pointer(aOutEx + uintptr(i)*20 + 16)) != 0 { iRead = int32(*(*Tu16)(unsafe.Pointer(aOutEx + uintptr(i)*20 + 16))) - int32(1) } else { v8 = iCol iCol-- iRead = v8 } _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iSortTab, iRead, regRow+i) goto _7 _7: ; i-- } switch eDest { case int32(SRT_Table): fallthrough case int32(SRT_EphemTab): _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iSortTab, nKey+bSeq, regRow) _sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), iParm, regRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iParm, regRow, regRowid) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_APPEND)) case int32(SRT_Set): _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), regRow, nColumn, regRowid, (*TSelectDest)(unsafe.Pointer(pDest)).FzAffSdst, nColumn) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm, regRowid, regRow, nColumn) case int32(SRT_Mem): /* The LIMIT clause will terminate the loop for us */ case int32(SRT_Upfrom): i2 = (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm2 r1 = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regRow+libc.BoolInt32(i2 < 0), nColumn-libc.BoolInt32(i2 < 0), r1) if i2 < 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iParm, r1, regRow) } else { _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm, r1, regRow, i2) } default: _ = libc.Int32FromInt32(0) if eDest == int32(SRT_Output) { _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst, nColumn) } else { _sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm) } break } if regRowid != 0 { if eDest == int32(SRT_Set) { _sqlite3ReleaseTempRange(tls, pParse, regRow, nColumn) } else { _sqlite3ReleaseTempReg(tls, pParse, regRow) } _sqlite3ReleaseTempReg(tls, pParse, regRowid) } /* The bottom of the loop */ _sqlite3VdbeResolveLabel(tls, v, addrContinue) if int32((*TSortCtx)(unsafe.Pointer(pSort)).FsortFlags)&int32(SORTFLAG_UseSorter) != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_SorterNext), iTab, addr) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iTab, addr) } if (*TSortCtx)(unsafe.Pointer(pSort)).FregReturn != 0 { _sqlite3VdbeAddOp1(tls, v, int32(OP_Return), (*TSortCtx)(unsafe.Pointer(pSort)).FregReturn) } _sqlite3VdbeResolveLabel(tls, v, addrBreak) } // C documentation // // /* // ** Return a pointer to a string containing the 'declaration type' of the // ** expression pExpr. The string may be treated as static by the caller. // ** // ** The declaration type is the exact datatype definition extracted from the // ** original CREATE TABLE statement if the expression is a column. The // ** declaration type for a ROWID field is INTEGER. Exactly when an expression // ** is considered a column can be complex in the presence of subqueries. The // ** result-set expression in all of the following SELECT statements is // ** considered a column by this function. // ** // ** SELECT col FROM tbl; // ** SELECT (SELECT col FROM tbl; // ** SELECT (SELECT col FROM tbl); // ** SELECT abc FROM (SELECT col AS abc FROM tbl); // ** // ** The declaration type for any expression other than a column is NULL. // ** // ** This routine has either 3 or 6 parameters depending on whether or not // ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used. // */ func _columnTypeImpl(tls *libc.TLS, pNC uintptr, pExpr uintptr, pzOrigDb uintptr, pzOrigTab uintptr, pzOrigCol uintptr) (r uintptr) { bp := tls.Alloc(96) defer tls.Free(96) var iCol, iDb, j int32 var p, p1, pS, pS1, pTab, pTabList, zType uintptr var _ /* sNC at bp+12 */ TNameContext var _ /* sNC at bp+48 */ TNameContext var _ /* zOrigCol at bp+8 */ uintptr var _ /* zOrigDb at bp+0 */ uintptr var _ /* zOrigTab at bp+4 */ uintptr _, _, _, _, _, _, _, _, _, _ = iCol, iDb, j, p, p1, pS, pS1, pTab, pTabList, zType zType = uintptr(0) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) { case int32(TK_COLUMN): /* The expression is a column. Locate the table the column is being ** extracted from in NameContext.pSrcList. This table may be real ** database table or a subquery. */ pTab = uintptr(0) /* Table structure column is extracted from */ pS = uintptr(0) /* Select the column is extracted from */ iCol = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) /* Index of column in pTab */ for pNC != 0 && !(pTab != 0) { pTabList = (*TNameContext)(unsafe.Pointer(pNC)).FpSrcList j = 0 for { if !(j < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc && (*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(j)*72))).FiCursor != (*TExpr)(unsafe.Pointer(pExpr)).FiTable) { break } goto _1 _1: ; j++ } if j < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc { pTab = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(j)*72))).FpTab pS = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(j)*72))).FpSelect } else { pNC = (*TNameContext)(unsafe.Pointer(pNC)).FpNext } } if pTab == uintptr(0) { /* At one time, code such as "SELECT new.x" within a trigger would ** cause this condition to run. Since then, we have restructured how ** trigger code is generated and so this condition is no longer ** possible. However, it can still be true for statements like ** the following: ** ** CREATE TABLE t1(col INTEGER); ** SELECT (SELECT t1.col) FROM FROM t1; ** ** when columnType() is called on the expression "t1.col" in the ** sub-select. In this case, set the column type to NULL, even ** though it should really be "INTEGER". ** ** This is not a problem, as the column type of "t1.col" is never ** used. When columnType() is called on the expression ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT ** branch below. */ break } _ = libc.Int32FromInt32(0) if pS != 0 { /* The "table" is actually a sub-select or a view in the FROM clause ** of the SELECT statement. Return the declaration type and origin ** data for the result-set column of the sub-select. */ if iCol < (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pS)).FpEList)).FnExpr && libc.Bool(libc.Bool(!(libc.Int32FromInt32(ViewCanHaveRowid) != 0)) || iCol >= 0) { p = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pS)).FpEList + 8 + uintptr(iCol)*20))).FpExpr (*(*TNameContext)(unsafe.Pointer(bp + 12))).FpSrcList = (*TSelect)(unsafe.Pointer(pS)).FpSrc (*(*TNameContext)(unsafe.Pointer(bp + 12))).FpNext = pNC (*(*TNameContext)(unsafe.Pointer(bp + 12))).FpParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse zType = _columnTypeImpl(tls, bp+12, p, bp, bp+4, bp+8) } } else { /* A real table or a CTE table */ _ = libc.Int32FromInt32(0) if iCol < 0 { iCol = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) } _ = libc.Int32FromInt32(0) if iCol < 0 { zType = __ccgo_ts + 1136 *(*uintptr)(unsafe.Pointer(bp + 8)) = __ccgo_ts + 16694 } else { *(*uintptr)(unsafe.Pointer(bp + 8)) = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName zType = _sqlite3ColumnType(tls, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(iCol)*12, uintptr(0)) } *(*uintptr)(unsafe.Pointer(bp + 4)) = (*TTable)(unsafe.Pointer(pTab)).FzName if (*TNameContext)(unsafe.Pointer(pNC)).FpParse != 0 && (*TTable)(unsafe.Pointer(pTab)).FpSchema != 0 { iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer((*TNameContext)(unsafe.Pointer(pNC)).FpParse)).Fdb, (*TTable)(unsafe.Pointer(pTab)).FpSchema) *(*uintptr)(unsafe.Pointer(bp)) = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer((*TNameContext)(unsafe.Pointer(pNC)).FpParse)).Fdb)).FaDb + uintptr(iDb)*16))).FzDbSName } } case int32(TK_SELECT): _ = libc.Int32FromInt32(0) pS1 = *(*uintptr)(unsafe.Pointer(pExpr + 20)) p1 = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pS1)).FpEList + 8))).FpExpr (*(*TNameContext)(unsafe.Pointer(bp + 48))).FpSrcList = (*TSelect)(unsafe.Pointer(pS1)).FpSrc (*(*TNameContext)(unsafe.Pointer(bp + 48))).FpNext = pNC (*(*TNameContext)(unsafe.Pointer(bp + 48))).FpParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse zType = _columnTypeImpl(tls, bp+48, p1, bp, bp+4, bp+8) break } if pzOrigDb != 0 { _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pzOrigDb)) = *(*uintptr)(unsafe.Pointer(bp)) *(*uintptr)(unsafe.Pointer(pzOrigTab)) = *(*uintptr)(unsafe.Pointer(bp + 4)) *(*uintptr)(unsafe.Pointer(pzOrigCol)) = *(*uintptr)(unsafe.Pointer(bp + 8)) } return zType } // C documentation // // /* // ** Generate code that will tell the VDBE the declaration types of columns // ** in the result set. // */ func _generateColumnTypes(tls *libc.TLS, pParse uintptr, pTabList uintptr, pEList uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var i int32 var p, v, zType uintptr var _ /* sNC at bp+0 */ TNameContext var _ /* zOrigCol at bp+44 */ uintptr var _ /* zOrigDb at bp+36 */ uintptr var _ /* zOrigTab at bp+40 */ uintptr _, _, _, _ = i, p, v, zType v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe (*(*TNameContext)(unsafe.Pointer(bp))).FpSrcList = pTabList (*(*TNameContext)(unsafe.Pointer(bp))).FpParse = pParse (*(*TNameContext)(unsafe.Pointer(bp))).FpNext = uintptr(0) i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } p = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FpExpr *(*uintptr)(unsafe.Pointer(bp + 36)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 40)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 44)) = uintptr(0) zType = _columnTypeImpl(tls, bp, p, bp+36, bp+40, bp+44) /* The vdbe must make its own copy of the column-type and other ** column specific strings, in case the schema is reset before this ** virtual machine is deleted. */ _sqlite3VdbeSetColName(tls, v, i, int32(COLNAME_DATABASE), *(*uintptr)(unsafe.Pointer(bp + 36)), uintptr(-libc.Int32FromInt32(1))) _sqlite3VdbeSetColName(tls, v, i, int32(COLNAME_TABLE), *(*uintptr)(unsafe.Pointer(bp + 40)), uintptr(-libc.Int32FromInt32(1))) _sqlite3VdbeSetColName(tls, v, i, int32(COLNAME_COLUMN), *(*uintptr)(unsafe.Pointer(bp + 44)), uintptr(-libc.Int32FromInt32(1))) _sqlite3VdbeSetColName(tls, v, i, int32(COLNAME_DECLTYPE), zType, uintptr(-libc.Int32FromInt32(1))) goto _1 _1: ; i++ } } // C documentation // // /* // ** Compute the column names for a SELECT statement. // ** // ** The only guarantee that SQLite makes about column names is that if the // ** column has an AS clause assigning it a name, that will be the name used. // ** That is the only documented guarantee. However, countless applications // ** developed over the years have made baseless assumptions about column names // ** and will break if those assumptions changes. Hence, use extreme caution // ** when modifying this routine to avoid breaking legacy. // ** // ** See Also: sqlite3ColumnsFromExprList() // ** // ** The PRAGMA short_column_names and PRAGMA full_column_names settings are // ** deprecated. The default setting is short=ON, full=OFF. 99.9% of all // ** applications should operate this way. Nevertheless, we need to support the // ** other modes for legacy: // ** // ** short=OFF, full=OFF: Column name is the text of the expression has it // ** originally appears in the SELECT statement. In // ** other words, the zSpan of the result expression. // ** // ** short=ON, full=OFF: (This is the default setting). If the result // ** refers directly to a table column, then the // ** result column name is just the table column // ** name: COLUMN. Otherwise use zSpan. // ** // ** full=ON, short=ANY: If the result refers directly to a table column, // ** then the result column name with the table name // ** prefix, ex: TABLE.COLUMN. Otherwise use zSpan. // */ func _sqlite3GenerateColumnNames(tls *libc.TLS, pParse uintptr, pSelect uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var db, p, pEList, pTab, pTabList, v, z, zCol, zName, zName1, v2 uintptr var fullName, i, iCol, srcName int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, fullName, i, iCol, p, pEList, pTab, pTabList, srcName, v, z, zCol, zName, zName1, v2 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* COLUMN or TABLE.COLUMN if no AS clause and is direct */ if (*TParse)(unsafe.Pointer(pParse)).FcolNamesSet != 0 { return } /* Column names are determined by the left-most term of a compound select */ for (*TSelect)(unsafe.Pointer(pSelect)).FpPrior != 0 { pSelect = (*TSelect)(unsafe.Pointer(pSelect)).FpPrior } pTabList = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).FcolNamesSet = uint8(1) fullName = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_FullColNames) != uint64(0)) srcName = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ShortColNames) != uint64(0) || fullName != 0) _sqlite3VdbeSetNumCols(tls, v, (*TExprList)(unsafe.Pointer(pEList)).FnExpr) i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } p = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FpExpr _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Agg processing has not run yet */ _ = libc.Int32FromInt32(0) /* Covering idx not yet coded */ if (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FzEName != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(i)*20 + 8 + 4))&0x3>>0)) == ENAME_NAME { /* An AS clause always takes first priority */ zName = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FzEName _sqlite3VdbeSetColName(tls, v, i, COLNAME_NAME, zName, uintptr(-libc.Int32FromInt32(1))) } else { if srcName != 0 && int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_COLUMN) { iCol = int32((*TExpr)(unsafe.Pointer(p)).FiColumn) pTab = (*TExpr)(unsafe.Pointer(p)).Fy.FpTab _ = libc.Int32FromInt32(0) if iCol < 0 { iCol = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) } _ = libc.Int32FromInt32(0) if iCol < 0 { zCol = __ccgo_ts + 16694 } else { zCol = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName } if fullName != 0 { zName1 = uintptr(0) zName1 = _sqlite3MPrintf(tls, db, __ccgo_ts+12381, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName, zCol)) _sqlite3VdbeSetColName(tls, v, i, COLNAME_NAME, zName1, __ccgo_fp(_sqlite3OomClear)) } else { _sqlite3VdbeSetColName(tls, v, i, COLNAME_NAME, zCol, uintptr(-libc.Int32FromInt32(1))) } } else { z = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FzEName if z == uintptr(0) { v2 = _sqlite3MPrintf(tls, db, __ccgo_ts+19216, libc.VaList(bp+8, i+int32(1))) } else { v2 = _sqlite3DbStrDup(tls, db, z) } z = v2 _sqlite3VdbeSetColName(tls, v, i, COLNAME_NAME, z, __ccgo_fp(_sqlite3OomClear)) } } goto _1 _1: ; i++ } _generateColumnTypes(tls, pParse, pTabList, pEList) } // C documentation // // /* // ** Given an expression list (which is really the list of expressions // ** that form the result set of a SELECT statement) compute appropriate // ** column names for a table that would hold the expression list. // ** // ** All column names will be unique. // ** // ** Only the column names are computed. Column.zType, Column.zColl, // ** and other fields of Column are zeroed. // ** // ** Return SQLITE_OK on success. If a memory allocation error occurs, // ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM. // ** // ** The only guarantee that SQLite makes about column names is that if the // ** column has an AS clause assigning it a name, that will be the name used. // ** That is the only documented guarantee. However, countless applications // ** developed over the years have made baseless assumptions about column names // ** and will break if those assumptions changes. Hence, use extreme caution // ** when modifying this routine to avoid breaking legacy. // ** // ** See Also: sqlite3GenerateColumnNames() // */ func _sqlite3ColumnsFromExprList(tls *libc.TLS, pParse uintptr, pEList uintptr, pnCol uintptr, paCol uintptr) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var aCol, db, pCol, pColExpr, pCollide, pTab, pX, zName, v2, v3, v4, p6, p9 uintptr var i, iCol, j, nCol, nName int32 var v5 bool var v8 Tu32 var _ /* cnt at bp+0 */ Tu32 var _ /* ht at bp+4 */ THash _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aCol, db, i, iCol, j, nCol, nName, pCol, pColExpr, pCollide, pTab, pX, zName, v2, v3, v4, v5, v8, p6, p9 db = (*TParse)(unsafe.Pointer(pParse)).Fdb _sqlite3HashInit(tls, bp+4) if pEList != 0 { nCol = (*TExprList)(unsafe.Pointer(pEList)).FnExpr aCol = _sqlite3DbMallocZero(tls, db, uint64(uint32(12)*uint32(nCol))) if nCol > int32(32767) { nCol = int32(32767) } } else { nCol = 0 aCol = uintptr(0) } _ = libc.Int32FromInt32(0) *(*Ti16)(unsafe.Pointer(pnCol)) = int16(nCol) *(*uintptr)(unsafe.Pointer(paCol)) = aCol i = 0 pCol = aCol for { if !(i < nCol && !((*TParse)(unsafe.Pointer(pParse)).FnErr != 0)) { break } pX = pEList + 8 + uintptr(i)*20 /* Get an appropriate name for the column */ v2 = (*TExprList_item)(unsafe.Pointer(pX)).FzEName zName = v2 if v2 != uintptr(0) && int32(uint32(*(*uint16)(unsafe.Pointer(pX + 8 + 4))&0x3>>0)) == ENAME_NAME { /* If the column contains an "AS " phrase, use as the name */ } else { pColExpr = _sqlite3ExprSkipCollateAndLikely(tls, (*TExprList_item)(unsafe.Pointer(pX)).FpExpr) for pColExpr != uintptr(0) && int32((*TExpr)(unsafe.Pointer(pColExpr)).Fop) == int32(TK_DOT) { pColExpr = (*TExpr)(unsafe.Pointer(pColExpr)).FpRight _ = libc.Int32FromInt32(0) } if int32((*TExpr)(unsafe.Pointer(pColExpr)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pColExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)|libc.Int32FromInt32(EP_Subrtn)) == uint32(0) && (*TExpr)(unsafe.Pointer(pColExpr)).Fy.FpTab != uintptr(0) { /* For columns use the column name name */ iCol = int32((*TExpr)(unsafe.Pointer(pColExpr)).FiColumn) pTab = (*TExpr)(unsafe.Pointer(pColExpr)).Fy.FpTab if iCol < 0 { iCol = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) } if iCol >= 0 { v3 = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName } else { v3 = __ccgo_ts + 16694 } zName = v3 } else { if int32((*TExpr)(unsafe.Pointer(pColExpr)).Fop) == int32(TK_ID) { _ = libc.Int32FromInt32(0) zName = *(*uintptr)(unsafe.Pointer(pColExpr + 8)) } else { /* Use the original text of the column expression as its name */ _ = libc.Int32FromInt32(0) /* pointer comparison intended */ } } } if zName != 0 && !(_sqlite3IsTrueOrFalse(tls, zName) != 0) { zName = _sqlite3DbStrDup(tls, db, zName) } else { zName = _sqlite3MPrintf(tls, db, __ccgo_ts+19216, libc.VaList(bp+32, i+int32(1))) } /* Make sure the column name is unique. If the name is not unique, ** append an integer to the name so that it becomes unique. */ *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) for { if v5 = zName != 0; v5 { v4 = _sqlite3HashFind(tls, bp+4, zName) pCollide = v4 } if !(v5 && v4 != uintptr(0)) { break } if int32(uint32(*(*uint16)(unsafe.Pointer(pCollide + 8 + 4))&0x80>>7)) != 0 { p6 = pCol + 10 *(*Tu16)(unsafe.Pointer(p6)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p6))) | libc.Int32FromInt32(COLFLAG_NOEXPAND)) } nName = _sqlite3Strlen30(tls, zName) if nName > 0 { j = nName - int32(1) for { if !(j > 0 && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zName + uintptr(j)))])&int32(0x04) != 0) { break } goto _7 _7: ; j-- } if int32(*(*uint8)(unsafe.Pointer(zName + uintptr(j)))) == int32(':') { nName = j } } *(*Tu32)(unsafe.Pointer(bp))++ v8 = *(*Tu32)(unsafe.Pointer(bp)) zName = _sqlite3MPrintf(tls, db, __ccgo_ts+19225, libc.VaList(bp+32, nName, zName, v8)) _sqlite3ProgressCheck(tls, pParse) if *(*Tu32)(unsafe.Pointer(bp)) > uint32(3) { Xsqlite3_randomness(tls, int32(4), bp) } } (*TColumn)(unsafe.Pointer(pCol)).FzCnName = zName (*TColumn)(unsafe.Pointer(pCol)).FhName = _sqlite3StrIHash(tls, zName) if int32(uint32(*(*uint16)(unsafe.Pointer(pX + 8 + 4))&0x100>>8)) != 0 { p9 = pCol + 10 *(*Tu16)(unsafe.Pointer(p9)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p9))) | libc.Int32FromInt32(COLFLAG_NOEXPAND)) } if zName != 0 && _sqlite3HashInsert(tls, bp+4, zName, pX) == pX { _sqlite3OomFault(tls, db) } goto _1 _1: ; i++ pCol += 12 } _sqlite3HashClear(tls, bp+4) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { j = 0 for { if !(j < i) { break } _sqlite3DbFree(tls, db, (*(*TColumn)(unsafe.Pointer(aCol + uintptr(j)*12))).FzCnName) goto _10 _10: ; j++ } _sqlite3DbFree(tls, db, aCol) *(*uintptr)(unsafe.Pointer(paCol)) = uintptr(0) *(*Ti16)(unsafe.Pointer(pnCol)) = 0 return (*TParse)(unsafe.Pointer(pParse)).Frc } return SQLITE_OK } // C documentation // // /* // ** pTab is a transient Table object that represents a subquery of some // ** kind (maybe a parenthesized subquery in the FROM clause of a larger // ** query, or a VIEW, or a CTE). This routine computes type information // ** for that Table object based on the Select object that implements the // ** subquery. For the purposes of this routine, "type information" means: // ** // ** * The datatype name, as it might appear in a CREATE TABLE statement // ** * Which collating sequence to use for the column // ** * The affinity of the column // */ func _sqlite3SubqueryColumnTypes(tls *libc.TLS, pParse uintptr, pTab uintptr, pSelect uintptr, aff uint8) { bp := tls.Alloc(48) defer tls.Free(48) var a, db, p, pCol, pColl, pS2, zType, p4, p5 uintptr var i, j, m int32 var m1, n Ti64 var _ /* sNC at bp+0 */ TNameContext _, _, _, _, _, _, _, _, _, _, _, _, _, _ = a, db, i, j, m, m1, n, p, pCol, pColl, pS2, zType, p4, p5 db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 || int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { return } for (*TSelect)(unsafe.Pointer(pSelect)).FpPrior != 0 { pSelect = (*TSelect)(unsafe.Pointer(pSelect)).FpPrior } a = (*TSelect)(unsafe.Pointer(pSelect)).FpEList + 8 libc.Xmemset(tls, bp, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp))).FpSrcList = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc i = 0 pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } *(*Tu32)(unsafe.Pointer(pTab + 28)) |= uint32(int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags) & libc.Int32FromInt32(COLFLAG_NOINSERT)) p = (*(*TExprList_item)(unsafe.Pointer(a + uintptr(i)*20))).FpExpr /* pCol->szEst = ... // Column size est for SELECT tables never used */ (*TColumn)(unsafe.Pointer(pCol)).Faffinity = _sqlite3ExprAffinity(tls, p) if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) <= int32(SQLITE_AFF_NONE) { (*TColumn)(unsafe.Pointer(pCol)).Faffinity = aff } if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) >= int32(SQLITE_AFF_TEXT) && (*TSelect)(unsafe.Pointer(pSelect)).FpNext != 0 { m = 0 m = 0 pS2 = (*TSelect)(unsafe.Pointer(pSelect)).FpNext for { if !(pS2 != 0) { break } m |= _sqlite3ExprDataType(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pS2)).FpEList + 8 + uintptr(i)*20))).FpExpr) goto _2 _2: ; pS2 = (*TSelect)(unsafe.Pointer(pS2)).FpNext } if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) == int32(SQLITE_AFF_TEXT) && m&int32(0x01) != 0 { (*TColumn)(unsafe.Pointer(pCol)).Faffinity = uint8(SQLITE_AFF_BLOB) } else { if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) >= int32(SQLITE_AFF_NUMERIC) && m&int32(0x02) != 0 { (*TColumn)(unsafe.Pointer(pCol)).Faffinity = uint8(SQLITE_AFF_BLOB) } } if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) >= int32(SQLITE_AFF_NUMERIC) && int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_CAST) { (*TColumn)(unsafe.Pointer(pCol)).Faffinity = uint8(SQLITE_AFF_FLEXNUM) } } zType = _columnTypeImpl(tls, bp, p, uintptr(0), uintptr(0), uintptr(0)) if zType == uintptr(0) || int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) != int32(_sqlite3AffinityType(tls, zType, uintptr(0))) { if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) == int32(SQLITE_AFF_NUMERIC) || int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) == int32(SQLITE_AFF_FLEXNUM) { zType = __ccgo_ts + 19233 } else { zType = uintptr(0) j = int32(1) for { if !(j < int32(SQLITE_N_STDTYPE)) { break } if int32(_sqlite3StdTypeAffinity[j]) == int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) { zType = _sqlite3StdType[j] break } goto _3 _3: ; j++ } } } if zType != 0 { m1 = int64(_sqlite3Strlen30(tls, zType)) n = int64(_sqlite3Strlen30(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)) (*TColumn)(unsafe.Pointer(pCol)).FzCnName = _sqlite3DbReallocOrFree(tls, db, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, uint64(n+m1+int64(2))) p4 = pCol + 10 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) & ^(libc.Int32FromInt32(COLFLAG_HASTYPE) | libc.Int32FromInt32(COLFLAG_HASCOLL))) if (*TColumn)(unsafe.Pointer(pCol)).FzCnName != 0 { libc.Xmemcpy(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName+uintptr(n+int64(1)), zType, uint32(m1+int64(1))) p5 = pCol + 10 *(*Tu16)(unsafe.Pointer(p5)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p5))) | libc.Int32FromInt32(COLFLAG_HASTYPE)) } } pColl = _sqlite3ExprCollSeq(tls, pParse, p) if pColl != 0 { _ = libc.Int32FromInt32(0) _sqlite3ColumnSetColl(tls, db, pCol, (*TCollSeq)(unsafe.Pointer(pColl)).FzName) } goto _1 _1: ; i++ pCol += 12 } (*TTable)(unsafe.Pointer(pTab)).FszTabRow = int16(1) /* Any non-zero value works */ } // C documentation // // /* // ** Given a SELECT statement, generate a Table structure that describes // ** the result set of that SELECT. // */ func _sqlite3ResultSetOfSelect(tls *libc.TLS, pParse uintptr, pSelect uintptr, aff uint8) (r uintptr) { var db, pTab uintptr var savedFlags Tu64 _, _, _ = db, pTab, savedFlags db = (*TParse)(unsafe.Pointer(pParse)).Fdb savedFlags = (*Tsqlite3)(unsafe.Pointer(db)).Fflags *(*Tu64)(unsafe.Pointer(db + 32)) &= ^libc.Uint64FromInt32(SQLITE_FullColNames) *(*Tu64)(unsafe.Pointer(db + 32)) |= uint64(SQLITE_ShortColNames) _sqlite3SelectPrep(tls, pParse, pSelect, uintptr(0)) (*Tsqlite3)(unsafe.Pointer(db)).Fflags = savedFlags if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return uintptr(0) } for (*TSelect)(unsafe.Pointer(pSelect)).FpPrior != 0 { pSelect = (*TSelect)(unsafe.Pointer(pSelect)).FpPrior } pTab = _sqlite3DbMallocZero(tls, db, uint64(64)) if pTab == uintptr(0) { return uintptr(0) } (*TTable)(unsafe.Pointer(pTab)).FnTabRef = uint32(1) (*TTable)(unsafe.Pointer(pTab)).FzName = uintptr(0) (*TTable)(unsafe.Pointer(pTab)).FnRowLogEst = int16(200) _ = libc.Int32FromInt32(0) _sqlite3ColumnsFromExprList(tls, pParse, (*TSelect)(unsafe.Pointer(pSelect)).FpEList, pTab+34, pTab+4) _sqlite3SubqueryColumnTypes(tls, pParse, pTab, pSelect, aff) (*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(-int32(1)) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _sqlite3DeleteTable(tls, db, pTab) return uintptr(0) } return pTab } // C documentation // // /* // ** Get a VDBE for the given parser context. Create a new one if necessary. // ** If an error occurs, return NULL and leave a message in pParse. // */ func _sqlite3GetVdbe(tls *libc.TLS, pParse uintptr) (r uintptr) { if (*TParse)(unsafe.Pointer(pParse)).FpVdbe != 0 { return (*TParse)(unsafe.Pointer(pParse)).FpVdbe } if (*TParse)(unsafe.Pointer(pParse)).FpToplevel == uintptr(0) && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_FactorOutConst)) == uint32(0) { (*TParse)(unsafe.Pointer(pParse)).FokConstFactor = uint8(1) } return _sqlite3VdbeCreate(tls, pParse) } // C documentation // // /* // ** Compute the iLimit and iOffset fields of the SELECT based on the // ** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions // ** that appear in the original SQL statement after the LIMIT and OFFSET // ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset // ** are the integer memory register numbers for counters used to compute // ** the limit and offset. If there is no limit and/or offset, then // ** iLimit and iOffset are negative. // ** // ** This routine changes the values of iLimit and iOffset only if // ** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit // ** and iOffset should have been preset to appropriate default values (zero) // ** prior to calling this routine. // ** // ** The iOffset register (if it exists) is initialized to the value // ** of the OFFSET. The iLimit register is initialized to LIMIT. Register // ** iOffset+1 is initialized to LIMIT+OFFSET. // ** // ** Only if pLimit->pLeft!=0 do the limit registers get // ** redefined. The UNION ALL operator uses this property to force // ** the reuse of the same limit and offset registers across multiple // ** SELECT statements. // */ func _computeLimitRegisters(tls *libc.TLS, pParse uintptr, p uintptr, iBreak int32) { bp := tls.Alloc(16) defer tls.Free(16) var iLimit, iOffset, v1, v2, v4, v5 int32 var pLimit, v, v3, v6 uintptr var _ /* n at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _ = iLimit, iOffset, pLimit, v, v1, v2, v3, v4, v5, v6 v = uintptr(0) iLimit = 0 pLimit = (*TSelect)(unsafe.Pointer(p)).FpLimit if (*TSelect)(unsafe.Pointer(p)).FiLimit != 0 { return } /* ** "LIMIT -1" always shows all rows. There is some ** controversy about what the correct behavior should be. ** The current implementation interprets "LIMIT 0" to mean ** no rows. */ if pLimit != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) v3 = pParse + 44 *(*int32)(unsafe.Pointer(v3))++ v2 = *(*int32)(unsafe.Pointer(v3)) v1 = v2 iLimit = v1 (*TSelect)(unsafe.Pointer(p)).FiLimit = v1 v = _sqlite3GetVdbe(tls, pParse) _ = libc.Int32FromInt32(0) if _sqlite3ExprIsInteger(tls, (*TExpr)(unsafe.Pointer(pLimit)).FpLeft, bp) != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), *(*int32)(unsafe.Pointer(bp)), iLimit) if *(*int32)(unsafe.Pointer(bp)) == 0 { _sqlite3VdbeGoto(tls, v, iBreak) } else { if *(*int32)(unsafe.Pointer(bp)) >= 0 && int32((*TSelect)(unsafe.Pointer(p)).FnSelectRow) > int32(_sqlite3LogEst(tls, uint64(*(*int32)(unsafe.Pointer(bp))))) { (*TSelect)(unsafe.Pointer(p)).FnSelectRow = _sqlite3LogEst(tls, uint64(*(*int32)(unsafe.Pointer(bp)))) *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(SF_FixedLimit) } } } else { _sqlite3ExprCode(tls, pParse, (*TExpr)(unsafe.Pointer(pLimit)).FpLeft, iLimit) _sqlite3VdbeAddOp1(tls, v, int32(OP_MustBeInt), iLimit) _sqlite3VdbeAddOp2(tls, v, int32(OP_IfNot), iLimit, iBreak) } if (*TExpr)(unsafe.Pointer(pLimit)).FpRight != 0 { v6 = pParse + 44 *(*int32)(unsafe.Pointer(v6))++ v5 = *(*int32)(unsafe.Pointer(v6)) v4 = v5 iOffset = v4 (*TSelect)(unsafe.Pointer(p)).FiOffset = v4 (*TParse)(unsafe.Pointer(pParse)).FnMem++ /* Allocate an extra register for limit+offset */ _sqlite3ExprCode(tls, pParse, (*TExpr)(unsafe.Pointer(pLimit)).FpRight, iOffset) _sqlite3VdbeAddOp1(tls, v, int32(OP_MustBeInt), iOffset) _sqlite3VdbeAddOp3(tls, v, int32(OP_OffsetLimit), iLimit, iOffset+int32(1), iOffset) } } } // C documentation // // /* // ** Return the appropriate collating sequence for the iCol-th column of // ** the result set for the compound-select statement "p". Return NULL if // ** the column has no default collating sequence. // ** // ** The collating sequence for the compound select is taken from the // ** left-most term of the select that has a collating sequence. // */ func _multiSelectCollSeq(tls *libc.TLS, pParse uintptr, p uintptr, iCol int32) (r uintptr) { var pRet uintptr _ = pRet if (*TSelect)(unsafe.Pointer(p)).FpPrior != 0 { pRet = _multiSelectCollSeq(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpPrior, iCol) } else { pRet = uintptr(0) } _ = libc.Int32FromInt32(0) /* iCol must be less than p->pEList->nExpr. Otherwise an error would ** have been thrown during name resolution and we would not have gotten ** this far */ if pRet == uintptr(0) && iCol < (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr { pRet = _sqlite3ExprCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList + 8 + uintptr(iCol)*20))).FpExpr) } return pRet } // C documentation // // /* // ** The select statement passed as the second parameter is a compound SELECT // ** with an ORDER BY clause. This function allocates and returns a KeyInfo // ** structure suitable for implementing the ORDER BY. // ** // ** Space to hold the KeyInfo structure is obtained from malloc. The calling // ** function is responsible for ensuring that this structure is eventually // ** freed. // */ func _multiSelectOrderByKeyInfo(tls *libc.TLS, pParse uintptr, p uintptr, nExtra int32) (r uintptr) { var db, pColl, pItem, pOrderBy, pRet, pTerm uintptr var i, nOrderBy, v1 int32 _, _, _, _, _, _, _, _, _ = db, i, nOrderBy, pColl, pItem, pOrderBy, pRet, pTerm, v1 pOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy if pOrderBy != uintptr(0) { v1 = (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr } else { v1 = 0 } nOrderBy = v1 db = (*TParse)(unsafe.Pointer(pParse)).Fdb pRet = _sqlite3KeyInfoAlloc(tls, db, nOrderBy+nExtra, int32(1)) if pRet != 0 { i = 0 for { if !(i < nOrderBy) { break } pItem = pOrderBy + 8 + uintptr(i)*20 pTerm = (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr if (*TExpr)(unsafe.Pointer(pTerm)).Fflags&uint32(EP_Collate) != 0 { pColl = _sqlite3ExprCollSeq(tls, pParse, pTerm) } else { pColl = _multiSelectCollSeq(tls, pParse, p, int32((*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem + 16))).FiOrderByCol)-int32(1)) if pColl == uintptr(0) { pColl = (*Tsqlite3)(unsafe.Pointer(db)).FpDfltColl } (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).FpExpr = _sqlite3ExprAddCollateString(tls, pParse, pTerm, (*TCollSeq)(unsafe.Pointer(pColl)).FzName) } _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pRet + 20 + uintptr(i)*4)) = pColl *(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pRet)).FaSortFlags + uintptr(i))) = (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).Ffg.FsortFlags goto _2 _2: ; i++ } } return pRet } // C documentation // // /* // ** This routine generates VDBE code to compute the content of a WITH RECURSIVE // ** query of the form: // ** // ** AS ( UNION [ALL] ) // ** \___________/ \_______________/ // ** p->pPrior p // ** // ** // ** There is exactly one reference to the recursive-table in the FROM clause // ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag. // ** // ** The setup-query runs once to generate an initial set of rows that go // ** into a Queue table. Rows are extracted from the Queue table one by // ** one. Each row extracted from Queue is output to pDest. Then the single // ** extracted row (now in the iCurrent table) becomes the content of the // ** recursive-table for a recursive-query run. The output of the recursive-query // ** is added back into the Queue table. Then another row is extracted from Queue // ** and the iteration continues until the Queue table is empty. // ** // ** If the compound query operator is UNION then no duplicate rows are ever // ** inserted into the Queue table. The iDistinct table keeps a copy of all rows // ** that have ever been inserted into Queue and causes duplicates to be // ** discarded. If the operator is UNION ALL, then duplicates are allowed. // ** // ** If the query has an ORDER BY, then entries in the Queue table are kept in // ** ORDER BY order and the first entry is extracted for each cycle. Without // ** an ORDER BY, the Queue table is just a FIFO. // ** // ** If a LIMIT clause is provided, then the iteration stops after LIMIT rows // ** have been output to pDest. A LIMIT of zero means to output no rows and a // ** negative LIMIT means to output all rows. If there is also an OFFSET clause // ** with a positive value, then the first OFFSET outputs are discarded rather // ** than being sent to pDest. The LIMIT count does not begin until after OFFSET // ** rows have been skipped. // */ func _generateWithRecursiveQuery(tls *libc.TLS, pParse uintptr, p uintptr, pDest uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var addrBreak, addrCont, addrTop, eDest, i, iCurrent, iDistinct, iQueue, nCol, rc, regCurrent, regLimit, regOffset, v1, v3, v5, v6, v8, v9 int32 var pFirstRec, pKeyInfo, pLimit, pOrderBy, pSetup, pSrc, v, v10, v4, v7 uintptr var _ /* destQueue at bp+0 */ TSelectDest _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrBreak, addrCont, addrTop, eDest, i, iCurrent, iDistinct, iQueue, nCol, pFirstRec, pKeyInfo, pLimit, pOrderBy, pSetup, pSrc, rc, regCurrent, regLimit, regOffset, v, v1, v10, v3, v4, v5, v6, v7, v8, v9 pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc /* The FROM clause of the recursive query */ nCol = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr /* Number of columns in the recursive table */ v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* CONTINUE and BREAK addresses */ iCurrent = 0 /* The Queue table */ iDistinct = 0 /* To ensure unique results if UNION */ eDest = int32(SRT_Fifo) /* Registers used by LIMIT and OFFSET */ if (*TSelect)(unsafe.Pointer(p)).FpWin != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19237, 0) return } /* Obtain authorization to do a recursive query */ if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_RECURSIVE), uintptr(0), uintptr(0), uintptr(0)) != 0 { return } /* Process the LIMIT and OFFSET clauses, if they exist */ addrBreak = _sqlite3VdbeMakeLabel(tls, pParse) (*TSelect)(unsafe.Pointer(p)).FnSelectRow = int16(320) /* 4 billion rows */ _computeLimitRegisters(tls, pParse, p, addrBreak) pLimit = (*TSelect)(unsafe.Pointer(p)).FpLimit regLimit = (*TSelect)(unsafe.Pointer(p)).FiLimit regOffset = (*TSelect)(unsafe.Pointer(p)).FiOffset (*TSelect)(unsafe.Pointer(p)).FpLimit = uintptr(0) v1 = libc.Int32FromInt32(0) (*TSelect)(unsafe.Pointer(p)).FiOffset = v1 (*TSelect)(unsafe.Pointer(p)).FiLimit = v1 pOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy /* Locate the cursor number of the Current table */ i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) { break } if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72 + 36 + 4))&0x40>>6)) != 0 { iCurrent = (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72))).FiCursor break } goto _2 _2: ; i++ } /* Allocate cursors numbers for Queue and Distinct. The cursor number for ** the Distinct table must be exactly one greater than Queue in order ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */ v4 = pParse + 40 v3 = *(*int32)(unsafe.Pointer(v4)) *(*int32)(unsafe.Pointer(v4))++ iQueue = v3 if int32((*TSelect)(unsafe.Pointer(p)).Fop) == int32(TK_UNION) { if pOrderBy != 0 { v5 = int32(SRT_DistQueue) } else { v5 = int32(SRT_DistFifo) } eDest = v5 v7 = pParse + 40 v6 = *(*int32)(unsafe.Pointer(v7)) *(*int32)(unsafe.Pointer(v7))++ iDistinct = v6 } else { if pOrderBy != 0 { v8 = int32(SRT_Queue) } else { v8 = int32(SRT_Fifo) } eDest = v8 } _sqlite3SelectDestInit(tls, bp, eDest, iQueue) /* Allocate cursors for Current, Queue, and Distinct. */ v10 = pParse + 44 *(*int32)(unsafe.Pointer(v10))++ v9 = *(*int32)(unsafe.Pointer(v10)) regCurrent = v9 _sqlite3VdbeAddOp3(tls, v, int32(OP_OpenPseudo), iCurrent, regCurrent, nCol) if pOrderBy != 0 { pKeyInfo = _multiSelectOrderByKeyInfo(tls, pParse, p, int32(1)) _sqlite3VdbeAddOp4(tls, v, int32(OP_OpenEphemeral), iQueue, (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr+int32(2), 0, pKeyInfo, -int32(8)) (*(*TSelectDest)(unsafe.Pointer(bp))).FpOrderBy = pOrderBy } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), iQueue, nCol) } if iDistinct != 0 { *(*int32)(unsafe.Pointer(p + 20)) = _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), iDistinct, 0) *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(SF_UsesEphemeral) } /* Detach the ORDER BY clause from the compound SELECT */ (*TSelect)(unsafe.Pointer(p)).FpOrderBy = uintptr(0) /* Figure out how many elements of the compound SELECT are part of the ** recursive query. Make sure no recursive elements use aggregate ** functions. Mark the recursive elements as UNION ALL even if they ** are really UNION because the distinctness will be enforced by the ** iDistinct table. pFirstRec is left pointing to the left-most ** recursive term of the CTE. */ pFirstRec = p for { if !(pFirstRec != uintptr(0)) { break } if (*TSelect)(unsafe.Pointer(pFirstRec)).FselFlags&uint32(SF_Aggregate) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19286, 0) goto end_of_recursive_query } (*TSelect)(unsafe.Pointer(pFirstRec)).Fop = uint8(TK_ALL) if (*TSelect)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pFirstRec)).FpPrior)).FselFlags&uint32(SF_Recursive) == uint32(0) { break } goto _11 _11: ; pFirstRec = (*TSelect)(unsafe.Pointer(pFirstRec)).FpPrior } /* Store the results of the setup-query in Queue. */ pSetup = (*TSelect)(unsafe.Pointer(pFirstRec)).FpPrior (*TSelect)(unsafe.Pointer(pSetup)).FpNext = uintptr(0) _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+19328, 0) rc = _sqlite3Select(tls, pParse, pSetup, bp) (*TSelect)(unsafe.Pointer(pSetup)).FpNext = p if rc != 0 { goto end_of_recursive_query } /* Find the next row in the Queue and output that row */ addrTop = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), iQueue, addrBreak) /* Transfer the next row in Queue over to Current */ _sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), iCurrent) /* To reset column cache */ if pOrderBy != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iQueue, (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr+int32(1), regCurrent) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_RowData), iQueue, regCurrent) } _sqlite3VdbeAddOp1(tls, v, int32(OP_Delete), iQueue) /* Output the single row in Current */ addrCont = _sqlite3VdbeMakeLabel(tls, pParse) _codeOffset(tls, v, regOffset, addrCont) _selectInnerLoop(tls, pParse, p, iCurrent, uintptr(0), uintptr(0), pDest, addrCont, addrBreak) if regLimit != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_DecrJumpZero), regLimit, addrBreak) } _sqlite3VdbeResolveLabel(tls, v, addrCont) /* Execute the recursive SELECT taking the single row in Current as ** the value for the recursive-table. Store the results in the Queue. */ (*TSelect)(unsafe.Pointer(pFirstRec)).FpPrior = uintptr(0) _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+19334, 0) _sqlite3Select(tls, pParse, p, bp) _ = libc.Int32FromInt32(0) (*TSelect)(unsafe.Pointer(pFirstRec)).FpPrior = pSetup /* Keep running the loop until the Queue is empty */ _sqlite3VdbeGoto(tls, v, addrTop) _sqlite3VdbeResolveLabel(tls, v, addrBreak) goto end_of_recursive_query end_of_recursive_query: ; _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TSelect)(unsafe.Pointer(p)).FpOrderBy) (*TSelect)(unsafe.Pointer(p)).FpOrderBy = pOrderBy (*TSelect)(unsafe.Pointer(p)).FpLimit = pLimit return } // C documentation // // /* // ** Handle the special case of a compound-select that originates from a // ** VALUES clause. By handling this as a special case, we avoid deep // ** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT // ** on a VALUES clause. // ** // ** Because the Select object originates from a VALUES clause: // ** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1 // ** (2) All terms are UNION ALL // ** (3) There is no ORDER BY clause // ** // ** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES // ** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))"). // ** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case. // ** Since the limit is exactly 1, we only need to evaluate the left-most VALUES. // */ func _multiSelectValues(tls *libc.TLS, pParse uintptr, p uintptr, pDest uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var bShowAll, nRow, rc int32 var v1 uintptr _, _, _, _ = bShowAll, nRow, rc, v1 nRow = int32(1) rc = 0 bShowAll = libc.BoolInt32((*TSelect)(unsafe.Pointer(p)).FpLimit == uintptr(0)) _ = libc.Int32FromInt32(0) for cond := true; cond; cond = int32(1) != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TSelect)(unsafe.Pointer(p)).FpWin != 0 { return -int32(1) } if (*TSelect)(unsafe.Pointer(p)).FpPrior == uintptr(0) { break } _ = libc.Int32FromInt32(0) p = (*TSelect)(unsafe.Pointer(p)).FpPrior nRow += bShowAll } if nRow == int32(1) { v1 = __ccgo_ts + 1648 } else { v1 = __ccgo_ts + 19349 } _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+19351, libc.VaList(bp+8, nRow, v1)) for p != 0 { _selectInnerLoop(tls, pParse, p, -int32(1), uintptr(0), uintptr(0), pDest, int32(1), int32(1)) if !(bShowAll != 0) { break } (*TSelect)(unsafe.Pointer(p)).FnSelectRow = int16(nRow) p = (*TSelect)(unsafe.Pointer(p)).FpNext } return rc } // C documentation // // /* // ** Return true if the SELECT statement which is known to be the recursive // ** part of a recursive CTE still has its anchor terms attached. If the // ** anchor terms have already been removed, then return false. // */ func _hasAnchor(tls *libc.TLS, p uintptr) (r int32) { for p != 0 && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Recursive) != uint32(0) { p = (*TSelect)(unsafe.Pointer(p)).FpPrior } return libc.BoolInt32(p != uintptr(0)) } // C documentation // // /* // ** This routine is called to process a compound query form from // ** two or more separate queries using UNION, UNION ALL, EXCEPT, or // ** INTERSECT // ** // ** "p" points to the right-most of the two queries. the query on the // ** left is p->pPrior. The left query could also be a compound query // ** in which case this routine will be called recursively. // ** // ** The results of the total query are to be written into a destination // ** of type eDest with parameter iParm. // ** // ** Example 1: Consider a three-way compound SQL statement. // ** // ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3 // ** // ** This statement is parsed up as follows: // ** // ** SELECT c FROM t3 // ** | // ** `-----> SELECT b FROM t2 // ** | // ** `------> SELECT a FROM t1 // ** // ** The arrows in the diagram above represent the Select.pPrior pointer. // ** So if this routine is called with p equal to the t3 query, then // ** pPrior will be the t2 query. p->op will be TK_UNION in this case. // ** // ** Notice that because of the way SQLite parses compound SELECTs, the // ** individual selects always group from left to right. // */ func _multiSelect(tls *libc.TLS, pParse uintptr, p uintptr, pDest uintptr) (r int32) { bp := tls.Alloc(112) defer tls.Free(112) var addr, addr1, addr2, addr3, i, iBreak, iBreak1, iCont, iCont1, iStart, iStart1, nCol, priorOp, r1, rc, tab1, tab2, unionTab, v1, v3, v5 int32 var apColl, db, pDelete, pKeyInfo, pLimit, pLimit1, pLoop, pPrior, v, v2, v4, v6 uintptr var op Tu8 var _ /* dest at bp+0 */ TSelectDest var _ /* intersectdest at bp+60 */ TSelectDest var _ /* nLimit at bp+28 */ int32 var _ /* uniondest at bp+32 */ TSelectDest _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, addr1, addr2, addr3, apColl, db, i, iBreak, iBreak1, iCont, iCont1, iStart, iStart1, nCol, op, pDelete, pKeyInfo, pLimit, pLimit1, pLoop, pPrior, priorOp, r1, rc, tab1, tab2, unionTab, v, v1, v2, v3, v4, v5, v6 rc = SQLITE_OK /* Alternative data destination */ pDelete = uintptr(0) /* Database connection */ /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT. */ _ = libc.Int32FromInt32(0) /* Calling function guarantees this much */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) db = (*TParse)(unsafe.Pointer(pParse)).Fdb pPrior = (*TSelect)(unsafe.Pointer(p)).FpPrior *(*TSelectDest)(unsafe.Pointer(bp)) = *(*TSelectDest)(unsafe.Pointer(pDest)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) v = _sqlite3GetVdbe(tls, pParse) _ = libc.Int32FromInt32(0) /* The VDBE already created by calling function */ /* Create the destination temporary table if necessary */ if int32((*(*TSelectDest)(unsafe.Pointer(bp))).FeDest) == int32(SRT_EphemTab) { _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (*(*TSelectDest)(unsafe.Pointer(bp))).FiSDParm, (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr) (*(*TSelectDest)(unsafe.Pointer(bp))).FeDest = uint8(SRT_Table) } /* Special handling for a compound-select that originates as a VALUES clause. */ if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_MultiValue) != 0 { rc = _multiSelectValues(tls, pParse, p, bp) if rc >= 0 { goto multi_select_end } rc = SQLITE_OK } /* Make sure all SELECTs in the statement have the same number of elements ** in their result sets. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Recursive) != uint32(0) && _hasAnchor(tls, p) != 0 { _generateWithRecursiveQuery(tls, pParse, p, bp) } else { /* Compound SELECTs that have an ORDER BY clause are handled separately. */ if (*TSelect)(unsafe.Pointer(p)).FpOrderBy != 0 { return _multiSelectOrderBy(tls, pParse, p, pDest) } else { if (*TSelect)(unsafe.Pointer(pPrior)).FpPrior == uintptr(0) { _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+19374, 0) _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+19389, 0) } /* Generate code for the left and right SELECT statements. */ switch int32((*TSelect)(unsafe.Pointer(p)).Fop) { case int32(TK_ALL): addr = 0 *(*int32)(unsafe.Pointer(bp + 28)) = 0 /* Initialize to suppress harmless compiler warning */ _ = libc.Int32FromInt32(0) (*TSelect)(unsafe.Pointer(pPrior)).FiLimit = (*TSelect)(unsafe.Pointer(p)).FiLimit (*TSelect)(unsafe.Pointer(pPrior)).FiOffset = (*TSelect)(unsafe.Pointer(p)).FiOffset (*TSelect)(unsafe.Pointer(pPrior)).FpLimit = (*TSelect)(unsafe.Pointer(p)).FpLimit rc = _sqlite3Select(tls, pParse, pPrior, bp) (*TSelect)(unsafe.Pointer(pPrior)).FpLimit = uintptr(0) if rc != 0 { goto multi_select_end } (*TSelect)(unsafe.Pointer(p)).FpPrior = uintptr(0) (*TSelect)(unsafe.Pointer(p)).FiLimit = (*TSelect)(unsafe.Pointer(pPrior)).FiLimit (*TSelect)(unsafe.Pointer(p)).FiOffset = (*TSelect)(unsafe.Pointer(pPrior)).FiOffset if (*TSelect)(unsafe.Pointer(p)).FiLimit != 0 { addr = _sqlite3VdbeAddOp1(tls, v, int32(OP_IfNot), (*TSelect)(unsafe.Pointer(p)).FiLimit) if (*TSelect)(unsafe.Pointer(p)).FiOffset != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_OffsetLimit), (*TSelect)(unsafe.Pointer(p)).FiLimit, (*TSelect)(unsafe.Pointer(p)).FiOffset+int32(1), (*TSelect)(unsafe.Pointer(p)).FiOffset) } } _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+19114, 0) rc = _sqlite3Select(tls, pParse, p, bp) pDelete = (*TSelect)(unsafe.Pointer(p)).FpPrior (*TSelect)(unsafe.Pointer(p)).FpPrior = pPrior (*TSelect)(unsafe.Pointer(p)).FnSelectRow = _sqlite3LogEstAdd(tls, (*TSelect)(unsafe.Pointer(p)).FnSelectRow, (*TSelect)(unsafe.Pointer(pPrior)).FnSelectRow) if (*TSelect)(unsafe.Pointer(p)).FpLimit != 0 && _sqlite3ExprIsInteger(tls, (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpLimit)).FpLeft, bp+28) != 0 && *(*int32)(unsafe.Pointer(bp + 28)) > 0 && int32((*TSelect)(unsafe.Pointer(p)).FnSelectRow) > int32(_sqlite3LogEst(tls, uint64(*(*int32)(unsafe.Pointer(bp + 28))))) { (*TSelect)(unsafe.Pointer(p)).FnSelectRow = _sqlite3LogEst(tls, uint64(*(*int32)(unsafe.Pointer(bp + 28)))) } if addr != 0 { _sqlite3VdbeJumpHere(tls, v, addr) } case int32(TK_EXCEPT): fallthrough case int32(TK_UNION): /* Cursor number of the temp table holding result */ op = uint8(0) priorOp = int32(SRT_Union) if int32((*(*TSelectDest)(unsafe.Pointer(bp))).FeDest) == priorOp { /* We can reuse a temporary table generated by a SELECT to our ** right. */ _ = libc.Int32FromInt32(0) /* Not allowed on leftward elements */ unionTab = (*(*TSelectDest)(unsafe.Pointer(bp))).FiSDParm } else { /* We will need to create our own temporary table to hold the ** intermediate results. */ v2 = pParse + 40 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ unionTab = v1 _ = libc.Int32FromInt32(0) addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), unionTab, 0) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(p + 20)) = addr1 *(*Tu32)(unsafe.Pointer(_findRightmost(tls, p) + 4)) |= uint32(SF_UsesEphemeral) _ = libc.Int32FromInt32(0) } /* Code the SELECT statements to our left */ _ = libc.Int32FromInt32(0) _sqlite3SelectDestInit(tls, bp+32, priorOp, unionTab) rc = _sqlite3Select(tls, pParse, pPrior, bp+32) if rc != 0 { goto multi_select_end } /* Code the current SELECT statement */ if int32((*TSelect)(unsafe.Pointer(p)).Fop) == int32(TK_EXCEPT) { op = uint8(SRT_Except) } else { _ = libc.Int32FromInt32(0) op = uint8(SRT_Union) } (*TSelect)(unsafe.Pointer(p)).FpPrior = uintptr(0) pLimit = (*TSelect)(unsafe.Pointer(p)).FpLimit (*TSelect)(unsafe.Pointer(p)).FpLimit = uintptr(0) (*(*TSelectDest)(unsafe.Pointer(bp + 32))).FeDest = op _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+19408, libc.VaList(bp+96, _sqlite3SelectOpName(tls, int32((*TSelect)(unsafe.Pointer(p)).Fop)))) rc = _sqlite3Select(tls, pParse, p, bp+32) _ = libc.Int32FromInt32(0) pDelete = (*TSelect)(unsafe.Pointer(p)).FpPrior (*TSelect)(unsafe.Pointer(p)).FpPrior = pPrior (*TSelect)(unsafe.Pointer(p)).FpOrderBy = uintptr(0) if int32((*TSelect)(unsafe.Pointer(p)).Fop) == int32(TK_UNION) { (*TSelect)(unsafe.Pointer(p)).FnSelectRow = _sqlite3LogEstAdd(tls, (*TSelect)(unsafe.Pointer(p)).FnSelectRow, (*TSelect)(unsafe.Pointer(pPrior)).FnSelectRow) } _sqlite3ExprDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpLimit) (*TSelect)(unsafe.Pointer(p)).FpLimit = pLimit (*TSelect)(unsafe.Pointer(p)).FiLimit = 0 (*TSelect)(unsafe.Pointer(p)).FiOffset = 0 /* Convert the data in the temporary table into whatever form ** it is that we currently need. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*(*TSelectDest)(unsafe.Pointer(bp))).FeDest) != priorOp && int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 { iBreak = _sqlite3VdbeMakeLabel(tls, pParse) iCont = _sqlite3VdbeMakeLabel(tls, pParse) _computeLimitRegisters(tls, pParse, p, iBreak) _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), unionTab, iBreak) iStart = _sqlite3VdbeCurrentAddr(tls, v) _selectInnerLoop(tls, pParse, p, unionTab, uintptr(0), uintptr(0), bp, iCont, iBreak) _sqlite3VdbeResolveLabel(tls, v, iCont) _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), unionTab, iStart) _sqlite3VdbeResolveLabel(tls, v, iBreak) _sqlite3VdbeAddOp2(tls, v, int32(OP_Close), unionTab, 0) } default: _ = libc.Int32FromInt32(0) /* INTERSECT is different from the others since it requires ** two temporary tables. Hence it has its own case. Begin ** by allocating the tables we will need. */ v4 = pParse + 40 v3 = *(*int32)(unsafe.Pointer(v4)) *(*int32)(unsafe.Pointer(v4))++ tab1 = v3 v6 = pParse + 40 v5 = *(*int32)(unsafe.Pointer(v6)) *(*int32)(unsafe.Pointer(v6))++ tab2 = v5 _ = libc.Int32FromInt32(0) addr2 = _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), tab1, 0) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(p + 20)) = addr2 *(*Tu32)(unsafe.Pointer(_findRightmost(tls, p) + 4)) |= uint32(SF_UsesEphemeral) _ = libc.Int32FromInt32(0) /* Code the SELECTs to our left into temporary table "tab1". */ _sqlite3SelectDestInit(tls, bp+60, int32(SRT_Union), tab1) rc = _sqlite3Select(tls, pParse, pPrior, bp+60) if rc != 0 { goto multi_select_end } /* Code the current SELECT into temporary table "tab2" */ addr2 = _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), tab2, 0) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(p + 20 + 1*4)) = addr2 (*TSelect)(unsafe.Pointer(p)).FpPrior = uintptr(0) pLimit1 = (*TSelect)(unsafe.Pointer(p)).FpLimit (*TSelect)(unsafe.Pointer(p)).FpLimit = uintptr(0) (*(*TSelectDest)(unsafe.Pointer(bp + 60))).FiSDParm = tab2 _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+19408, libc.VaList(bp+96, _sqlite3SelectOpName(tls, int32((*TSelect)(unsafe.Pointer(p)).Fop)))) rc = _sqlite3Select(tls, pParse, p, bp+60) pDelete = (*TSelect)(unsafe.Pointer(p)).FpPrior (*TSelect)(unsafe.Pointer(p)).FpPrior = pPrior if int32((*TSelect)(unsafe.Pointer(p)).FnSelectRow) > int32((*TSelect)(unsafe.Pointer(pPrior)).FnSelectRow) { (*TSelect)(unsafe.Pointer(p)).FnSelectRow = (*TSelect)(unsafe.Pointer(pPrior)).FnSelectRow } _sqlite3ExprDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpLimit) (*TSelect)(unsafe.Pointer(p)).FpLimit = pLimit1 /* Generate code to take the intersection of the two temporary ** tables. */ if rc != 0 { break } _ = libc.Int32FromInt32(0) iBreak1 = _sqlite3VdbeMakeLabel(tls, pParse) iCont1 = _sqlite3VdbeMakeLabel(tls, pParse) _computeLimitRegisters(tls, pParse, p, iBreak1) _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), tab1, iBreak1) r1 = _sqlite3GetTempReg(tls, pParse) iStart1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_RowData), tab1, r1) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), tab2, iCont1, r1, 0) _sqlite3ReleaseTempReg(tls, pParse, r1) _selectInnerLoop(tls, pParse, p, tab1, uintptr(0), uintptr(0), bp, iCont1, iBreak1) _sqlite3VdbeResolveLabel(tls, v, iCont1) _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), tab1, iStart1) _sqlite3VdbeResolveLabel(tls, v, iBreak1) _sqlite3VdbeAddOp2(tls, v, int32(OP_Close), tab2, 0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Close), tab1, 0) break } if (*TSelect)(unsafe.Pointer(p)).FpNext == uintptr(0) { _sqlite3VdbeExplainPop(tls, pParse) } } } if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto multi_select_end } /* Compute collating sequences used by ** temporary tables needed to implement the compound select. ** Attach the KeyInfo structure to all temporary tables. ** ** This section is run by the right-most SELECT statement only. ** SELECT statements to the left always skip this part. The right-most ** SELECT might also skip this part if it has no ORDER BY clause and ** no temp tables are required. */ if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_UsesEphemeral) != 0 { /* Number of columns in result set */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) nCol = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr pKeyInfo = _sqlite3KeyInfoAlloc(tls, db, nCol, int32(1)) if !(pKeyInfo != 0) { rc = int32(SQLITE_NOMEM) goto multi_select_end } i = 0 apColl = pKeyInfo + 20 for { if !(i < nCol) { break } *(*uintptr)(unsafe.Pointer(apColl)) = _multiSelectCollSeq(tls, pParse, p, i) if uintptr(0) == *(*uintptr)(unsafe.Pointer(apColl)) { *(*uintptr)(unsafe.Pointer(apColl)) = (*Tsqlite3)(unsafe.Pointer(db)).FpDfltColl } goto _7 _7: ; i++ apColl += 4 } pLoop = p for { if !(pLoop != 0) { break } i = 0 for { if !(i < int32(2)) { break } addr3 = *(*int32)(unsafe.Pointer(pLoop + 20 + uintptr(i)*4)) if addr3 < 0 { /* If [0] is unused then [1] is also unused. So we can ** always safely abort as soon as the first unused slot is found */ _ = libc.Int32FromInt32(0) break } _sqlite3VdbeChangeP2(tls, v, addr3, nCol) _sqlite3VdbeChangeP4(tls, v, addr3, _sqlite3KeyInfoRef(tls, pKeyInfo), -int32(8)) *(*int32)(unsafe.Pointer(pLoop + 20 + uintptr(i)*4)) = -int32(1) goto _9 _9: ; i++ } goto _8 _8: ; pLoop = (*TSelect)(unsafe.Pointer(pLoop)).FpPrior } _sqlite3KeyInfoUnref(tls, pKeyInfo) } goto multi_select_end multi_select_end: ; (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst = (*(*TSelectDest)(unsafe.Pointer(bp))).FiSdst (*TSelectDest)(unsafe.Pointer(pDest)).FnSdst = (*(*TSelectDest)(unsafe.Pointer(bp))).FnSdst if pDelete != 0 { _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3SelectDeleteGeneric), pDelete) } return rc } // C documentation // // /* // ** Error message for when two or more terms of a compound select have different // ** size result sets. // */ func _sqlite3SelectWrongNumTermsError(tls *libc.TLS, pParse uintptr, p uintptr) { bp := tls.Alloc(16) defer tls.Free(16) if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Values) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19429, 0) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19475, libc.VaList(bp+8, _sqlite3SelectOpName(tls, int32((*TSelect)(unsafe.Pointer(p)).Fop)))) } } // C documentation // // /* // ** Code an output subroutine for a coroutine implementation of a // ** SELECT statement. // ** // ** The data to be output is contained in pIn->iSdst. There are // ** pIn->nSdst columns to be output. pDest is where the output should // ** be sent. // ** // ** regReturn is the number of the register holding the subroutine // ** return address. // ** // ** If regPrev>0 then it is the first register in a vector that // ** records the previous output. mem[regPrev] is a flag that is false // ** if there has been no previous output. If regPrev>0 then code is // ** generated to suppress duplicates. pKeyInfo is used for comparing // ** keys. // ** // ** If the LIMIT found in p->iLimit is reached, jump immediately to // ** iBreak. // */ func _generateOutputSubroutine(tls *libc.TLS, pParse uintptr, p uintptr, pIn uintptr, pDest uintptr, regReturn int32, regPrev int32, pKeyInfo uintptr, iBreak int32) (r int32) { var addr, addr1, addr2, iContinue, r1, r11, r2 int32 var v uintptr _, _, _, _, _, _, _, _ = addr, addr1, addr2, iContinue, r1, r11, r2, v v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe addr = _sqlite3VdbeCurrentAddr(tls, v) iContinue = _sqlite3VdbeMakeLabel(tls, pParse) /* Suppress duplicates for UNION, EXCEPT, and INTERSECT */ if regPrev != 0 { addr1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_IfNot), regPrev) addr2 = _sqlite3VdbeAddOp4(tls, v, int32(OP_Compare), (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, regPrev+int32(1), (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst, _sqlite3KeyInfoRef(tls, pKeyInfo), -int32(8)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Jump), addr2+int32(2), iContinue, addr2+int32(2)) _sqlite3VdbeJumpHere(tls, v, addr1) _sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, regPrev+int32(1), (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst-int32(1)) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), regPrev) } if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 { return 0 } /* Suppress the first OFFSET entries if there is an OFFSET clause */ _codeOffset(tls, v, (*TSelect)(unsafe.Pointer(p)).FiOffset, iContinue) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) switch int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) { /* Store the result as data using a unique key. */ case int32(SRT_EphemTab): r1 = _sqlite3GetTempReg(tls, pParse) r2 = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst, r1) _sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm, r2) _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm, r1, r2) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_APPEND)) _sqlite3ReleaseTempReg(tls, pParse, r2) _sqlite3ReleaseTempReg(tls, pParse, r1) break /* If we are creating a set for an "expr IN (SELECT ...)". */ fallthrough case int32(SRT_Set): r11 = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst, r11, (*TSelectDest)(unsafe.Pointer(pDest)).FzAffSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm, r11, (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst) _sqlite3ReleaseTempReg(tls, pParse, r11) break /* If this is a scalar select that is part of an expression, then ** store the results in the appropriate memory cell and break out ** of the scan loop. Note that the select might return multiple columns ** if it is the RHS of a row-value IN operator. */ fallthrough case int32(SRT_Mem): _sqlite3ExprCodeMove(tls, pParse, (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst) /* The LIMIT clause will jump out of the loop for us */ break /* The results are stored in a sequence of registers ** starting at pDest->iSdst. Then the co-routine yields. */ fallthrough case int32(SRT_Coroutine): if (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst == 0 { (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst = _sqlite3GetTempRange(tls, pParse, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst) (*TSelectDest)(unsafe.Pointer(pDest)).FnSdst = (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst } _sqlite3ExprCodeMove(tls, pParse, (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst) _sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm) break /* If none of the above, then the result destination must be ** SRT_Output. This routine is never called with any other ** destination other than the ones handled above or SRT_Output. ** ** For SRT_Output, results are stored in a sequence of registers. ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to ** return the next row of result. */ fallthrough default: _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst) break } /* Jump to the end of the loop if the LIMIT is reached. */ if (*TSelect)(unsafe.Pointer(p)).FiLimit != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_DecrJumpZero), (*TSelect)(unsafe.Pointer(p)).FiLimit, iBreak) } /* Generate the subroutine return */ _sqlite3VdbeResolveLabel(tls, v, iContinue) _sqlite3VdbeAddOp1(tls, v, int32(OP_Return), regReturn) return addr } // C documentation // // /* // ** Alternative compound select code generator for cases when there // ** is an ORDER BY clause. // ** // ** We assume a query of the following form: // ** // ** ORDER BY // ** // ** is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea // ** is to code both and with the ORDER BY clause as // ** co-routines. Then run the co-routines in parallel and merge the results // ** into the output. In addition to the two coroutines (called selectA and // ** selectB) there are 7 subroutines: // ** // ** outA: Move the output of the selectA coroutine into the output // ** of the compound query. // ** // ** outB: Move the output of the selectB coroutine into the output // ** of the compound query. (Only generated for UNION and // ** UNION ALL. EXCEPT and INSERTSECT never output a row that // ** appears only in B.) // ** // ** AltB: Called when there is data from both coroutines and AB. // ** // ** EofA: Called when data is exhausted from selectA. // ** // ** EofB: Called when data is exhausted from selectB. // ** // ** The implementation of the latter five subroutines depend on which // ** is used: // ** // ** // ** UNION ALL UNION EXCEPT INTERSECT // ** ------------- ----------------- -------------- ----------------- // ** AltB: outA, nextA outA, nextA outA, nextA nextA // ** // ** AeqB: outA, nextA nextA nextA outA, nextA // ** // ** AgtB: outB, nextB outB, nextB nextB nextB // ** // ** EofA: outB, nextB outB, nextB halt halt // ** // ** EofB: outA, nextA outA, nextA outA, nextA halt // ** // ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA // ** causes an immediate jump to EofA and an EOF on B following nextB causes // ** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or // ** following nextX causes a jump to the end of the select processing. // ** // ** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled // ** within the output subroutine. The regPrev register set holds the previously // ** output value. A comparison is made against this value and the output // ** is skipped if the next results would be the same as the previous. // ** // ** The implementation plan is to implement the two coroutines and seven // ** subroutines first, then put the control logic at the bottom. Like this: // ** // ** goto Init // ** coA: coroutine for left query (A) // ** coB: coroutine for right query (B) // ** outA: output one row of A // ** outB: output one row of B (UNION and UNION ALL only) // ** EofA: ... // ** EofB: ... // ** AltB: ... // ** AeqB: ... // ** AgtB: ... // ** Init: initialize coroutine registers // ** yield coA // ** if eof(A) goto EofA // ** yield coB // ** if eof(B) goto EofB // ** Cmpr: Compare A, B // ** Jump AltB, AeqB, AgtB // ** End: ... // ** // ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not // ** actually called using Gosub and they do not Return. EofA and EofB loop // ** until all data is exhausted then jump to the "end" label. AltB, AeqB, // ** and AgtB jump to either L2 or to one of EofA or EofB. // */ func _multiSelectOrderBy(tls *libc.TLS, pParse uintptr, p uintptr, pDest uintptr) (r int32) { bp := tls.Alloc(80) defer tls.Free(80) var aPermute, db, pItem, pItem1, pKeyDup, pKeyMerge, pNew, pOrderBy, pPrior, pSplit, v, v10, v12, v16, v18, v20, v22, v3 uintptr var addr1, addrAeqB, addrAgtB, addrAltB, addrEofA, addrEofA_noB, addrEofB, addrOutA, addrOutB, addrSelectA, addrSelectB, i, j, labelCmpr, labelEnd, nExpr, nOrderBy, nSelect, op, regAddrA, regAddrB, regLimitA, regLimitB, regOutA, regOutB, regPrev, savedLimit, savedOffset, v11, v13, v14, v15, v17, v19, v21, v23, v4, v9 int32 var _ /* destA at bp+0 */ TSelectDest var _ /* destB at bp+28 */ TSelectDest _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aPermute, addr1, addrAeqB, addrAgtB, addrAltB, addrEofA, addrEofA_noB, addrEofB, addrOutA, addrOutB, addrSelectA, addrSelectB, db, i, j, labelCmpr, labelEnd, nExpr, nOrderBy, nSelect, op, pItem, pItem1, pKeyDup, pKeyMerge, pNew, pOrderBy, pPrior, pSplit, regAddrA, regAddrB, regLimitA, regLimitB, regOutA, regOutB, regPrev, savedLimit, savedOffset, v, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v3, v4, v9 /* Address of the output-A subroutine */ addrOutB = 0 /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */ pKeyDup = uintptr(0) /* Mapping from ORDER BY terms to result set columns */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* "Managed" code needs this. Ticket #3382. */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) /* Already thrown the error if VDBE alloc failed */ labelEnd = _sqlite3VdbeMakeLabel(tls, pParse) labelCmpr = _sqlite3VdbeMakeLabel(tls, pParse) /* Patch up the ORDER BY clause */ op = int32((*TSelect)(unsafe.Pointer(p)).Fop) _ = libc.Int32FromInt32(0) pOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy _ = libc.Int32FromInt32(0) nOrderBy = (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr /* For operators other than UNION ALL we have to make sure that ** the ORDER BY clause covers every term of the result set. Add ** terms to the ORDER BY clause as necessary. */ if op != int32(TK_ALL) { i = int32(1) for { if !(int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 && i <= (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr) { break } j = 0 pItem = pOrderBy + 8 for { if !(j < nOrderBy) { break } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem + 16))).FiOrderByCol) == i { break } goto _2 _2: ; j++ pItem += 20 } if j == nOrderBy { pNew = _sqlite3Expr(tls, db, int32(TK_INTEGER), uintptr(0)) if pNew == uintptr(0) { return int32(SQLITE_NOMEM) } *(*Tu32)(unsafe.Pointer(pNew + 4)) |= uint32(EP_IntValue) *(*int32)(unsafe.Pointer(pNew + 8)) = i v3 = _sqlite3ExprListAppend(tls, pParse, pOrderBy, pNew) pOrderBy = v3 (*TSelect)(unsafe.Pointer(p)).FpOrderBy = v3 if pOrderBy != 0 { v4 = nOrderBy nOrderBy++ *(*Tu16)(unsafe.Pointer(pOrderBy + 8 + uintptr(v4)*20 + 16)) = uint16(i) } } goto _1 _1: ; i++ } } /* Compute the comparison permutation and keyinfo that is used with ** the permutation used to determine if the next ** row of results comes from selectA or selectB. Also add explicit ** collations to the ORDER BY clause terms so that when the subqueries ** to the right and the left are evaluated, they use the correct ** collation. */ aPermute = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(4)*uint32(nOrderBy+libc.Int32FromInt32(1)))) if aPermute != 0 { *(*Tu32)(unsafe.Pointer(aPermute)) = uint32(nOrderBy) i = int32(1) pItem1 = pOrderBy + 8 for { if !(i <= nOrderBy) { break } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(aPermute + uintptr(i)*4)) = uint32(int32((*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem1 + 16))).FiOrderByCol) - int32(1)) goto _5 _5: ; i++ pItem1 += 20 } pKeyMerge = _multiSelectOrderByKeyInfo(tls, pParse, p, int32(1)) } else { pKeyMerge = uintptr(0) } /* Allocate a range of temporary registers and the KeyInfo needed ** for the logic that removes duplicate result rows when the ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL). */ if op == int32(TK_ALL) { regPrev = 0 } else { nExpr = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr _ = libc.Int32FromInt32(0) regPrev = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nExpr + int32(1) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regPrev) pKeyDup = _sqlite3KeyInfoAlloc(tls, db, nExpr, int32(1)) if pKeyDup != 0 { _ = libc.Int32FromInt32(0) i = 0 for { if !(i < nExpr) { break } *(*uintptr)(unsafe.Pointer(pKeyDup + 20 + uintptr(i)*4)) = _multiSelectCollSeq(tls, pParse, p, i) *(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pKeyDup)).FaSortFlags + uintptr(i))) = uint8(0) goto _6 _6: ; i++ } } } /* Separate the left and the right query from one another */ nSelect = int32(1) if (op == int32(TK_ALL) || op == int32(TK_UNION)) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_BalancedMerge)) == uint32(0) { pSplit = p for { if !((*TSelect)(unsafe.Pointer(pSplit)).FpPrior != uintptr(0) && int32((*TSelect)(unsafe.Pointer(pSplit)).Fop) == op) { break } nSelect++ _ = libc.Int32FromInt32(0) goto _7 _7: ; pSplit = (*TSelect)(unsafe.Pointer(pSplit)).FpPrior } } if nSelect <= int32(3) { pSplit = p } else { pSplit = p i = int32(2) for { if !(i < nSelect) { break } pSplit = (*TSelect)(unsafe.Pointer(pSplit)).FpPrior goto _8 _8: ; i += int32(2) } } pPrior = (*TSelect)(unsafe.Pointer(pSplit)).FpPrior _ = libc.Int32FromInt32(0) (*TSelect)(unsafe.Pointer(pSplit)).FpPrior = uintptr(0) (*TSelect)(unsafe.Pointer(pPrior)).FpNext = uintptr(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TSelect)(unsafe.Pointer(pPrior)).FpOrderBy = _sqlite3ExprListDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pOrderBy, 0) _sqlite3ResolveOrderGroupBy(tls, pParse, p, (*TSelect)(unsafe.Pointer(p)).FpOrderBy, __ccgo_ts+7367) _sqlite3ResolveOrderGroupBy(tls, pParse, pPrior, (*TSelect)(unsafe.Pointer(pPrior)).FpOrderBy, __ccgo_ts+7367) /* Compute the limit registers */ _computeLimitRegisters(tls, pParse, p, labelEnd) if (*TSelect)(unsafe.Pointer(p)).FiLimit != 0 && op == int32(TK_ALL) { v10 = pParse + 44 *(*int32)(unsafe.Pointer(v10))++ v9 = *(*int32)(unsafe.Pointer(v10)) regLimitA = v9 v12 = pParse + 44 *(*int32)(unsafe.Pointer(v12))++ v11 = *(*int32)(unsafe.Pointer(v12)) regLimitB = v11 if (*TSelect)(unsafe.Pointer(p)).FiOffset != 0 { v13 = (*TSelect)(unsafe.Pointer(p)).FiOffset + int32(1) } else { v13 = (*TSelect)(unsafe.Pointer(p)).FiLimit } _sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), v13, regLimitA) _sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), regLimitA, regLimitB) } else { v14 = libc.Int32FromInt32(0) regLimitB = v14 regLimitA = v14 } _sqlite3ExprDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpLimit) (*TSelect)(unsafe.Pointer(p)).FpLimit = uintptr(0) v16 = pParse + 44 *(*int32)(unsafe.Pointer(v16))++ v15 = *(*int32)(unsafe.Pointer(v16)) regAddrA = v15 v18 = pParse + 44 *(*int32)(unsafe.Pointer(v18))++ v17 = *(*int32)(unsafe.Pointer(v18)) regAddrB = v17 v20 = pParse + 44 *(*int32)(unsafe.Pointer(v20))++ v19 = *(*int32)(unsafe.Pointer(v20)) regOutA = v19 v22 = pParse + 44 *(*int32)(unsafe.Pointer(v22))++ v21 = *(*int32)(unsafe.Pointer(v22)) regOutB = v21 _sqlite3SelectDestInit(tls, bp, int32(SRT_Coroutine), regAddrA) _sqlite3SelectDestInit(tls, bp+28, int32(SRT_Coroutine), regAddrB) _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+19557, libc.VaList(bp+64, _sqlite3SelectOpName(tls, int32((*TSelect)(unsafe.Pointer(p)).Fop)))) /* Generate a coroutine to evaluate the SELECT statement to the ** left of the compound operator - the "A" select. */ addrSelectA = _sqlite3VdbeCurrentAddr(tls, v) + int32(1) addr1 = _sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), regAddrA, 0, addrSelectA) (*TSelect)(unsafe.Pointer(pPrior)).FiLimit = regLimitA _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+19568, 0) _sqlite3Select(tls, pParse, pPrior, bp) _sqlite3VdbeEndCoroutine(tls, v, regAddrA) _sqlite3VdbeJumpHere(tls, v, addr1) /* Generate a coroutine to evaluate the SELECT statement on ** the right - the "B" select */ addrSelectB = _sqlite3VdbeCurrentAddr(tls, v) + int32(1) addr1 = _sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), regAddrB, 0, addrSelectB) savedLimit = (*TSelect)(unsafe.Pointer(p)).FiLimit savedOffset = (*TSelect)(unsafe.Pointer(p)).FiOffset (*TSelect)(unsafe.Pointer(p)).FiLimit = regLimitB (*TSelect)(unsafe.Pointer(p)).FiOffset = 0 _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+19573, 0) _sqlite3Select(tls, pParse, p, bp+28) (*TSelect)(unsafe.Pointer(p)).FiLimit = savedLimit (*TSelect)(unsafe.Pointer(p)).FiOffset = savedOffset _sqlite3VdbeEndCoroutine(tls, v, regAddrB) /* Generate a subroutine that outputs the current row of the A ** select as the next output row of the compound select. */ addrOutA = _generateOutputSubroutine(tls, pParse, p, bp, pDest, regOutA, regPrev, pKeyDup, labelEnd) /* Generate a subroutine that outputs the current row of the B ** select as the next output row of the compound select. */ if op == int32(TK_ALL) || op == int32(TK_UNION) { addrOutB = _generateOutputSubroutine(tls, pParse, p, bp+28, pDest, regOutB, regPrev, pKeyDup, labelEnd) } _sqlite3KeyInfoUnref(tls, pKeyDup) /* Generate a subroutine to run when the results from select A ** are exhausted and only data in select B remains. */ if op == int32(TK_EXCEPT) || op == int32(TK_INTERSECT) { v23 = labelEnd addrEofA = v23 addrEofA_noB = v23 } else { addrEofA = _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regOutB, addrOutB) addrEofA_noB = _sqlite3VdbeAddOp2(tls, v, int32(OP_Yield), regAddrB, labelEnd) _sqlite3VdbeGoto(tls, v, addrEofA) (*TSelect)(unsafe.Pointer(p)).FnSelectRow = _sqlite3LogEstAdd(tls, (*TSelect)(unsafe.Pointer(p)).FnSelectRow, (*TSelect)(unsafe.Pointer(pPrior)).FnSelectRow) } /* Generate a subroutine to run when the results from select B ** are exhausted and only data in select A remains. */ if op == int32(TK_INTERSECT) { addrEofB = addrEofA if int32((*TSelect)(unsafe.Pointer(p)).FnSelectRow) > int32((*TSelect)(unsafe.Pointer(pPrior)).FnSelectRow) { (*TSelect)(unsafe.Pointer(p)).FnSelectRow = (*TSelect)(unsafe.Pointer(pPrior)).FnSelectRow } } else { addrEofB = _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regOutA, addrOutA) _sqlite3VdbeAddOp2(tls, v, int32(OP_Yield), regAddrA, labelEnd) _sqlite3VdbeGoto(tls, v, addrEofB) } /* Generate code to handle the case of AB */ addrAgtB = _sqlite3VdbeCurrentAddr(tls, v) if op == int32(TK_ALL) || op == int32(TK_UNION) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regOutB, addrOutB) } _sqlite3VdbeAddOp2(tls, v, int32(OP_Yield), regAddrB, addrEofB) _sqlite3VdbeGoto(tls, v, labelCmpr) /* This code runs once to initialize everything. */ _sqlite3VdbeJumpHere(tls, v, addr1) _sqlite3VdbeAddOp2(tls, v, int32(OP_Yield), regAddrA, addrEofA_noB) _sqlite3VdbeAddOp2(tls, v, int32(OP_Yield), regAddrB, addrEofB) /* Implement the main merge loop */ _sqlite3VdbeResolveLabel(tls, v, labelCmpr) _sqlite3VdbeAddOp4(tls, v, int32(OP_Permutation), 0, 0, 0, aPermute, -int32(14)) _sqlite3VdbeAddOp4(tls, v, int32(OP_Compare), (*(*TSelectDest)(unsafe.Pointer(bp))).FiSdst, (*(*TSelectDest)(unsafe.Pointer(bp + 28))).FiSdst, nOrderBy, pKeyMerge, -int32(8)) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_PERMUTE)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Jump), addrAltB, addrAeqB, addrAgtB) /* Jump to the this point in order to terminate the query. */ _sqlite3VdbeResolveLabel(tls, v, labelEnd) /* Make arrangements to free the 2nd and subsequent arms of the compound ** after the parse has finished */ if (*TSelect)(unsafe.Pointer(pSplit)).FpPrior != 0 { _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3SelectDeleteGeneric), (*TSelect)(unsafe.Pointer(pSplit)).FpPrior) } (*TSelect)(unsafe.Pointer(pSplit)).FpPrior = pPrior (*TSelect)(unsafe.Pointer(pPrior)).FpNext = pSplit _sqlite3ExprListDelete(tls, db, (*TSelect)(unsafe.Pointer(pPrior)).FpOrderBy) (*TSelect)(unsafe.Pointer(pPrior)).FpOrderBy = uintptr(0) /*** TBD: Insert subroutine calls to close cursors on incomplete **** subqueries ****/ _sqlite3VdbeExplainPop(tls, pParse) return libc.BoolInt32((*TParse)(unsafe.Pointer(pParse)).FnErr != 0) } // C documentation // // /* An instance of the SubstContext object describes an substitution edit // ** to be performed on a parse tree. // ** // ** All references to columns in table iTable are to be replaced by corresponding // ** expressions in pEList. // ** // ** ## About "isOuterJoin": // ** // ** The isOuterJoin column indicates that the replacement will occur into a // ** position in the parent that NULL-able due to an OUTER JOIN. Either the // ** target slot in the parent is the right operand of a LEFT JOIN, or one of // ** the left operands of a RIGHT JOIN. In either case, we need to potentially // ** bypass the substituted expression with OP_IfNullRow. // ** // ** Suppose the original expression is an integer constant. Even though the table // ** has the nullRow flag set, because the expression is an integer constant, // ** it will not be NULLed out. So instead, we insert an OP_IfNullRow opcode // ** that checks to see if the nullRow flag is set on the table. If the nullRow // ** flag is set, then the value in the register is set to NULL and the original // ** expression is bypassed. If the nullRow flag is not set, then the original // ** expression runs to populate the register. // ** // ** Example where this is needed: // ** // ** CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT); // ** CREATE TABLE t2(x INT UNIQUE); // ** // ** SELECT a,b,m,x FROM t1 LEFT JOIN (SELECT 59 AS m,x FROM t2) ON b=x; // ** // ** When the subquery on the right side of the LEFT JOIN is flattened, we // ** have to add OP_IfNullRow in front of the OP_Integer that implements the // ** "m" value of the subquery so that a NULL will be loaded instead of 59 // ** when processing a non-matched row of the left. // */ type TSubstContext = struct { FpParse uintptr FiTable int32 FiNewTable int32 FisOuterJoin int32 FpEList uintptr FpCList uintptr } type SubstContext = TSubstContext // C documentation // // /* // ** Scan through the expression pExpr. Replace every reference to // ** a column in table number iTable with a copy of the iColumn-th // ** entry in pEList. (But leave references to the ROWID column // ** unchanged.) // ** // ** This routine is part of the flattening procedure. A subquery // ** whose result set is defined by pEList appears as entry in the // ** FROM clause of a SELECT such that the VDBE cursor assigned to that // ** FORM clause entry is iTable. This routine makes the necessary // ** changes to pExpr so that it refers directly to the source table // ** of the subquery rather the result set of the subquery. // */ func _substExpr(tls *libc.TLS, pSubst uintptr, pExpr uintptr) (r uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var db, pColl, pCopy, pNat, pNew, pWin, v1 uintptr var iColumn int32 var _ /* ifNullRow at bp+0 */ TExpr _, _, _, _, _, _, _, _ = db, iColumn, pColl, pCopy, pNat, pNew, pWin, v1 if pExpr == uintptr(0) { return uintptr(0) } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != uint32(0) && *(*int32)(unsafe.Pointer(pExpr + 36)) == (*TSubstContext)(unsafe.Pointer(pSubst)).FiTable { *(*int32)(unsafe.Pointer(pExpr + 36)) = (*TSubstContext)(unsafe.Pointer(pSubst)).FiNewTable } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*TSubstContext)(unsafe.Pointer(pSubst)).FiTable && !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_FixedCol)) != libc.Uint32FromInt32(0)) { iColumn = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pCopy = (*(*TExprList_item)(unsafe.Pointer((*TSubstContext)(unsafe.Pointer(pSubst)).FpEList + 8 + uintptr(iColumn)*20))).FpExpr if _sqlite3ExprIsVector(tls, pCopy) != 0 { _sqlite3VectorErrorMsg(tls, (*TSubstContext)(unsafe.Pointer(pSubst)).FpParse, pCopy) } else { db = (*TParse)(unsafe.Pointer((*TSubstContext)(unsafe.Pointer(pSubst)).FpParse)).Fdb if (*TSubstContext)(unsafe.Pointer(pSubst)).FisOuterJoin != 0 && (int32((*TExpr)(unsafe.Pointer(pCopy)).Fop) != int32(TK_COLUMN) || (*TExpr)(unsafe.Pointer(pCopy)).FiTable != (*TSubstContext)(unsafe.Pointer(pSubst)).FiNewTable) { libc.Xmemset(tls, bp, 0, uint32(52)) (*(*TExpr)(unsafe.Pointer(bp))).Fop = uint8(TK_IF_NULL_ROW) (*(*TExpr)(unsafe.Pointer(bp))).FpLeft = pCopy (*(*TExpr)(unsafe.Pointer(bp))).FiTable = (*TSubstContext)(unsafe.Pointer(pSubst)).FiNewTable (*(*TExpr)(unsafe.Pointer(bp))).FiColumn = int16(-int32(99)) (*(*TExpr)(unsafe.Pointer(bp))).Fflags = uint32(EP_IfNullRow) pCopy = bp } pNew = _sqlite3ExprDup(tls, db, pCopy, 0) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _sqlite3ExprDelete(tls, db, pNew) return pExpr } if (*TSubstContext)(unsafe.Pointer(pSubst)).FisOuterJoin != 0 { *(*Tu32)(unsafe.Pointer(pNew + 4)) |= uint32(libc.Int32FromInt32(EP_CanBeNull)) } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != uint32(0) { _sqlite3SetJoinExpr(tls, pNew, *(*int32)(unsafe.Pointer(pExpr + 36)), (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON))) } _sqlite3ExprDelete(tls, db, pExpr) pExpr = pNew if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_TRUEFALSE) { *(*int32)(unsafe.Pointer(pExpr + 8)) = _sqlite3ExprTruthValue(tls, pExpr) (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_INTEGER) *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_IntValue)) } /* Ensure that the expression now has an implicit collation sequence, ** just as it did when it was a column of a view or sub-query. */ pNat = _sqlite3ExprCollSeq(tls, (*TSubstContext)(unsafe.Pointer(pSubst)).FpParse, pExpr) pColl = _sqlite3ExprCollSeq(tls, (*TSubstContext)(unsafe.Pointer(pSubst)).FpParse, (*(*TExprList_item)(unsafe.Pointer((*TSubstContext)(unsafe.Pointer(pSubst)).FpCList + 8 + uintptr(iColumn)*20))).FpExpr) if pNat != pColl || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLLATE) { if pColl != 0 { v1 = (*TCollSeq)(unsafe.Pointer(pColl)).FzName } else { v1 = __ccgo_ts + 5086 } pExpr = _sqlite3ExprAddCollateString(tls, (*TSubstContext)(unsafe.Pointer(pSubst)).FpParse, pExpr, v1) } *(*Tu32)(unsafe.Pointer(pExpr + 4)) &= uint32(^libc.Int32FromInt32(EP_Collate)) } } else { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_IF_NULL_ROW) && (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*TSubstContext)(unsafe.Pointer(pSubst)).FiTable { (*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TSubstContext)(unsafe.Pointer(pSubst)).FiNewTable } (*TExpr)(unsafe.Pointer(pExpr)).FpLeft = _substExpr(tls, pSubst, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) (*TExpr)(unsafe.Pointer(pExpr)).FpRight = _substExpr(tls, pSubst, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) { _substSelect(tls, pSubst, *(*uintptr)(unsafe.Pointer(pExpr + 20)), int32(1)) } else { _substExprList(tls, pSubst, *(*uintptr)(unsafe.Pointer(pExpr + 20))) } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { pWin = *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)) (*TWindow)(unsafe.Pointer(pWin)).FpFilter = _substExpr(tls, pSubst, (*TWindow)(unsafe.Pointer(pWin)).FpFilter) _substExprList(tls, pSubst, (*TWindow)(unsafe.Pointer(pWin)).FpPartition) _substExprList(tls, pSubst, (*TWindow)(unsafe.Pointer(pWin)).FpOrderBy) } } return pExpr } func _substExprList(tls *libc.TLS, pSubst uintptr, pList uintptr) { var i int32 _ = i if pList == uintptr(0) { return } i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr = _substExpr(tls, pSubst, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr) goto _1 _1: ; i++ } } func _substSelect(tls *libc.TLS, pSubst uintptr, p uintptr, doPrior int32) { var i int32 var pItem, pSrc, v1 uintptr var v2 bool _, _, _, _, _ = i, pItem, pSrc, v1, v2 if !(p != 0) { return } for { _substExprList(tls, pSubst, (*TSelect)(unsafe.Pointer(p)).FpEList) _substExprList(tls, pSubst, (*TSelect)(unsafe.Pointer(p)).FpGroupBy) _substExprList(tls, pSubst, (*TSelect)(unsafe.Pointer(p)).FpOrderBy) (*TSelect)(unsafe.Pointer(p)).FpHaving = _substExpr(tls, pSubst, (*TSelect)(unsafe.Pointer(p)).FpHaving) (*TSelect)(unsafe.Pointer(p)).FpWhere = _substExpr(tls, pSubst, (*TSelect)(unsafe.Pointer(p)).FpWhere) pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc _ = libc.Int32FromInt32(0) i = (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc pItem = pSrc + 8 for { if !(i > 0) { break } _substSelect(tls, pSubst, (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect, int32(1)) if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x4>>2)) != 0 { _substExprList(tls, pSubst, *(*uintptr)(unsafe.Pointer(pItem + 64))) } goto _4 _4: ; i-- pItem += 72 } goto _3 _3: ; if v2 = doPrior != 0; v2 { v1 = (*TSelect)(unsafe.Pointer(p)).FpPrior p = v1 } if !(v2 && v1 != uintptr(0)) { break } } } // C documentation // // /* // ** pSelect is a SELECT statement and pSrcItem is one item in the FROM // ** clause of that SELECT. // ** // ** This routine scans the entire SELECT statement and recomputes the // ** pSrcItem->colUsed mask. // */ func _recomputeColumnsUsedExpr(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var pItem uintptr _ = pItem if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLUMN) { return WRC_Continue } pItem = *(*uintptr)(unsafe.Pointer(pWalker + 24)) if (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor != (*TExpr)(unsafe.Pointer(pExpr)).FiTable { return WRC_Continue } if int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) < 0 { return WRC_Continue } *(*TBitmask)(unsafe.Pointer(pItem + 56)) |= _sqlite3ExprColUsed(tls, pExpr) return WRC_Continue } func _recomputeColumnsUsed(tls *libc.TLS, pSelect uintptr, pSrcItem uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* w at bp+0 */ TWalker if (*TSrcItem)(unsafe.Pointer(pSrcItem)).FpTab == uintptr(0) { return } libc.Xmemset(tls, bp, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_recomputeColumnsUsedExpr) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop) *(*uintptr)(unsafe.Pointer(bp + 24)) = pSrcItem (*TSrcItem)(unsafe.Pointer(pSrcItem)).FcolUsed = uint64(0) _sqlite3WalkSelect(tls, bp, pSelect) } // C documentation // // /* // ** Assign new cursor numbers to each of the items in pSrc. For each // ** new cursor number assigned, set an entry in the aCsrMap[] array // ** to map the old cursor number to the new: // ** // ** aCsrMap[iOld+1] = iNew; // ** // ** The array is guaranteed by the caller to be large enough for all // ** existing cursor numbers in pSrc. aCsrMap[0] is the array size. // ** // ** If pSrc contains any sub-selects, call this routine recursively // ** on the FROM clause of each such sub-select, with iExcept set to -1. // */ func _srclistRenumberCursors(tls *libc.TLS, pParse uintptr, aCsrMap uintptr, pSrc uintptr, iExcept int32) { var i, v2 int32 var p, pItem, v3 uintptr _, _, _, _, _ = i, p, pItem, v2, v3 i = 0 pItem = pSrc + 8 for { if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) { break } if i != iExcept { _ = libc.Int32FromInt32(0) if !(int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x40>>6)) != 0) || *(*int32)(unsafe.Pointer(aCsrMap + uintptr((*TSrcItem)(unsafe.Pointer(pItem)).FiCursor+int32(1))*4)) == 0 { v3 = pParse + 40 v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ *(*int32)(unsafe.Pointer(aCsrMap + uintptr((*TSrcItem)(unsafe.Pointer(pItem)).FiCursor+int32(1))*4)) = v2 } (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor = *(*int32)(unsafe.Pointer(aCsrMap + uintptr((*TSrcItem)(unsafe.Pointer(pItem)).FiCursor+int32(1))*4)) p = (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect for { if !(p != 0) { break } _srclistRenumberCursors(tls, pParse, aCsrMap, (*TSelect)(unsafe.Pointer(p)).FpSrc, -int32(1)) goto _4 _4: ; p = (*TSelect)(unsafe.Pointer(p)).FpPrior } } goto _1 _1: ; i++ pItem += 72 } } // C documentation // // /* // ** *piCursor is a cursor number. Change it if it needs to be mapped. // */ func _renumberCursorDoMapping(tls *libc.TLS, pWalker uintptr, piCursor uintptr) { var aCsrMap uintptr var iCsr int32 _, _ = aCsrMap, iCsr aCsrMap = *(*uintptr)(unsafe.Pointer(pWalker + 24)) iCsr = *(*int32)(unsafe.Pointer(piCursor)) if iCsr < *(*int32)(unsafe.Pointer(aCsrMap)) && *(*int32)(unsafe.Pointer(aCsrMap + uintptr(iCsr+int32(1))*4)) > 0 { *(*int32)(unsafe.Pointer(piCursor)) = *(*int32)(unsafe.Pointer(aCsrMap + uintptr(iCsr+int32(1))*4)) } } // C documentation // // /* // ** Expression walker callback used by renumberCursors() to update // ** Expr objects to match newly assigned cursor numbers. // */ func _renumberCursorsCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var op int32 _ = op op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) if op == int32(TK_COLUMN) || op == int32(TK_IF_NULL_ROW) { _renumberCursorDoMapping(tls, pWalker, pExpr+28) } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) { _renumberCursorDoMapping(tls, pWalker, pExpr+36) } return WRC_Continue } // C documentation // // /* // ** Assign a new cursor number to each cursor in the FROM clause (Select.pSrc) // ** of the SELECT statement passed as the second argument, and to each // ** cursor in the FROM clause of any FROM clause sub-selects, recursively. // ** Except, do not assign a new cursor number to the iExcept'th element in // ** the FROM clause of (*p). Update all expressions and other references // ** to refer to the new cursor numbers. // ** // ** Argument aCsrMap is an array that may be used for temporary working // ** space. Two guarantees are made by the caller: // ** // ** * the array is larger than the largest cursor number used within the // ** select statement passed as an argument, and // ** // ** * the array entries for all cursor numbers that do *not* appear in // ** FROM clauses of the select statement as described above are // ** initialized to zero. // */ func _renumberCursors(tls *libc.TLS, pParse uintptr, p uintptr, iExcept int32, aCsrMap uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* w at bp+0 */ TWalker _srclistRenumberCursors(tls, pParse, aCsrMap, (*TSelect)(unsafe.Pointer(p)).FpSrc, iExcept) libc.Xmemset(tls, bp, 0, uint32(28)) *(*uintptr)(unsafe.Pointer(bp + 24)) = aCsrMap (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_renumberCursorsCb) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop) _sqlite3WalkSelect(tls, bp, p) } // C documentation // // /* // ** If pSel is not part of a compound SELECT, return a pointer to its // ** expression list. Otherwise, return a pointer to the expression list // ** of the leftmost SELECT in the compound. // */ func _findLeftmostExprlist(tls *libc.TLS, pSel uintptr) (r uintptr) { for (*TSelect)(unsafe.Pointer(pSel)).FpPrior != 0 { pSel = (*TSelect)(unsafe.Pointer(pSel)).FpPrior } return (*TSelect)(unsafe.Pointer(pSel)).FpEList } // C documentation // // /* // ** Return true if any of the result-set columns in the compound query // ** have incompatible affinities on one or more arms of the compound. // */ func _compoundHasDifferentAffinities(tls *libc.TLS, p uintptr) (r int32) { var aff uint8 var ii int32 var pList, pSub1 uintptr _, _, _, _ = aff, ii, pList, pSub1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pList = (*TSelect)(unsafe.Pointer(p)).FpEList ii = 0 for { if !(ii < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } _ = libc.Int32FromInt32(0) aff = _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*20))).FpExpr) pSub1 = (*TSelect)(unsafe.Pointer(p)).FpPrior for { if !(pSub1 != 0) { break } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(_sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub1)).FpEList + 8 + uintptr(ii)*20))).FpExpr)) != int32(aff) { return int32(1) } goto _2 _2: ; pSub1 = (*TSelect)(unsafe.Pointer(pSub1)).FpPrior } goto _1 _1: ; ii++ } return 0 } // C documentation // // /* // ** This routine attempts to flatten subqueries as a performance optimization. // ** This routine returns 1 if it makes changes and 0 if no flattening occurs. // ** // ** To understand the concept of flattening, consider the following // ** query: // ** // ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 // ** // ** The default way of implementing this query is to execute the // ** subquery first and store the results in a temporary table, then // ** run the outer query on that temporary table. This requires two // ** passes over the data. Furthermore, because the temporary table // ** has no indices, the WHERE clause on the outer query cannot be // ** optimized. // ** // ** This routine attempts to rewrite queries such as the above into // ** a single flat select, like this: // ** // ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 // ** // ** The code generated for this simplification gives the same result // ** but only has to scan the data once. And because indices might // ** exist on the table t1, a complete scan of the data might be // ** avoided. // ** // ** Flattening is subject to the following constraints: // ** // ** (**) We no longer attempt to flatten aggregate subqueries. Was: // ** The subquery and the outer query cannot both be aggregates. // ** // ** (**) We no longer attempt to flatten aggregate subqueries. Was: // ** (2) If the subquery is an aggregate then // ** (2a) the outer query must not be a join and // ** (2b) the outer query must not use subqueries // ** other than the one FROM-clause subquery that is a candidate // ** for flattening. (This is due to ticket [2f7170d73bf9abf80] // ** from 2015-02-09.) // ** // ** (3) If the subquery is the right operand of a LEFT JOIN then // ** (3a) the subquery may not be a join and // ** (3b) the FROM clause of the subquery may not contain a virtual // ** table and // ** (**) Was: "The outer query may not have a GROUP BY." This case // ** is now managed correctly // ** (3d) the outer query may not be DISTINCT. // ** See also (26) for restrictions on RIGHT JOIN. // ** // ** (4) The subquery can not be DISTINCT. // ** // ** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT // ** sub-queries that were excluded from this optimization. Restriction // ** (4) has since been expanded to exclude all DISTINCT subqueries. // ** // ** (**) We no longer attempt to flatten aggregate subqueries. Was: // ** If the subquery is aggregate, the outer query may not be DISTINCT. // ** // ** (7) The subquery must have a FROM clause. TODO: For subqueries without // ** A FROM clause, consider adding a FROM clause with the special // ** table sqlite_once that consists of a single row containing a // ** single NULL. // ** // ** (8) If the subquery uses LIMIT then the outer query may not be a join. // ** // ** (9) If the subquery uses LIMIT then the outer query may not be aggregate. // ** // ** (**) Restriction (10) was removed from the code on 2005-02-05 but we // ** accidentally carried the comment forward until 2014-09-15. Original // ** constraint: "If the subquery is aggregate then the outer query // ** may not use LIMIT." // ** // ** (11) The subquery and the outer query may not both have ORDER BY clauses. // ** // ** (**) Not implemented. Subsumed into restriction (3). Was previously // ** a separate restriction deriving from ticket #350. // ** // ** (13) The subquery and outer query may not both use LIMIT. // ** // ** (14) The subquery may not use OFFSET. // ** // ** (15) If the outer query is part of a compound select, then the // ** subquery may not use LIMIT. // ** (See ticket #2339 and ticket [02a8e81d44]). // ** // ** (16) If the outer query is aggregate, then the subquery may not // ** use ORDER BY. (Ticket #2942) This used to not matter // ** until we introduced the group_concat() function. // ** // ** (17) If the subquery is a compound select, then // ** (17a) all compound operators must be a UNION ALL, and // ** (17b) no terms within the subquery compound may be aggregate // ** or DISTINCT, and // ** (17c) every term within the subquery compound must have a FROM clause // ** (17d) the outer query may not be // ** (17d1) aggregate, or // ** (17d2) DISTINCT // ** (17e) the subquery may not contain window functions, and // ** (17f) the subquery must not be the RHS of a LEFT JOIN. // ** (17g) either the subquery is the first element of the outer // ** query or there are no RIGHT or FULL JOINs in any arm // ** of the subquery. (This is a duplicate of condition (27b).) // ** (17h) The corresponding result set expressions in all arms of the // ** compound must have the same affinity. // ** // ** The parent and sub-query may contain WHERE clauses. Subject to // ** rules (11), (13) and (14), they may also contain ORDER BY, // ** LIMIT and OFFSET clauses. The subquery cannot use any compound // ** operator other than UNION ALL because all the other compound // ** operators have an implied DISTINCT which is disallowed by // ** restriction (4). // ** // ** Also, each component of the sub-query must return the same number // ** of result columns. This is actually a requirement for any compound // ** SELECT statement, but all the code here does is make sure that no // ** such (illegal) sub-query is flattened. The caller will detect the // ** syntax error and return a detailed message. // ** // ** (18) If the sub-query is a compound select, then all terms of the // ** ORDER BY clause of the parent must be copies of a term returned // ** by the parent query. // ** // ** (19) If the subquery uses LIMIT then the outer query may not // ** have a WHERE clause. // ** // ** (20) If the sub-query is a compound select, then it must not use // ** an ORDER BY clause. Ticket #3773. We could relax this constraint // ** somewhat by saying that the terms of the ORDER BY clause must // ** appear as unmodified result columns in the outer query. But we // ** have other optimizations in mind to deal with that case. // ** // ** (21) If the subquery uses LIMIT then the outer query may not be // ** DISTINCT. (See ticket [752e1646fc]). // ** // ** (22) The subquery may not be a recursive CTE. // ** // ** (23) If the outer query is a recursive CTE, then the sub-query may not be // ** a compound query. This restriction is because transforming the // ** parent to a compound query confuses the code that handles // ** recursive queries in multiSelect(). // ** // ** (**) We no longer attempt to flatten aggregate subqueries. Was: // ** The subquery may not be an aggregate that uses the built-in min() or // ** or max() functions. (Without this restriction, a query like: // ** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily // ** return the value X for which Y was maximal.) // ** // ** (25) If either the subquery or the parent query contains a window // ** function in the select list or ORDER BY clause, flattening // ** is not attempted. // ** // ** (26) The subquery may not be the right operand of a RIGHT JOIN. // ** See also (3) for restrictions on LEFT JOIN. // ** // ** (27) The subquery may not contain a FULL or RIGHT JOIN unless it // ** is the first element of the parent query. Two subcases: // ** (27a) the subquery is not a compound query. // ** (27b) the subquery is a compound query and the RIGHT JOIN occurs // ** in any arm of the compound query. (See also (17g).) // ** // ** (28) The subquery is not a MATERIALIZED CTE. (This is handled // ** in the caller before ever reaching this routine.) // ** // ** // ** In this routine, the "p" parameter is a pointer to the outer query. // ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query // ** uses aggregates. // ** // ** If flattening is not attempted, this routine is a no-op and returns 0. // ** If flattening is attempted this routine returns 1. // ** // ** All of the expression analysis must occur on both the outer query and // ** the subquery before this routine runs. // */ func _flattenSubquery(tls *libc.TLS, pParse uintptr, p uintptr, iFrom int32, isAgg int32) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var aCsrMap, db, pItem, pItemTab, pLimit, pNew, pOrderBy, pOrderBy1, pParent, pPrior, pSrc, pSub, pSub1, pSubSrc, pSubitem, pTabToDel, pToplevel, pWhere, zSavedAuthContext, v5, v6, p10, p11, p9 uintptr var i, iNewParent, iParent, ii, isOuterJoin, nSubSrc, v4 int32 var jointype, ltorj Tu8 var _ /* w at bp+0 */ TWalker var _ /* x at bp+28 */ TSubstContext _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aCsrMap, db, i, iNewParent, iParent, ii, isOuterJoin, jointype, ltorj, nSubSrc, pItem, pItemTab, pLimit, pNew, pOrderBy, pOrderBy1, pParent, pPrior, pSrc, pSub, pSub1, pSubSrc, pSubitem, pTabToDel, pToplevel, pWhere, zSavedAuthContext, v4, v5, v6, p10, p11, p9 zSavedAuthContext = (*TParse)(unsafe.Pointer(pParse)).FzAuthContext /* VDBE cursor number of the pSub result set temp table */ iNewParent = -int32(1) /* Replacement table for iParent */ isOuterJoin = 0 /* The subquery */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Walker to persist agginfo data */ aCsrMap = uintptr(0) /* Check to see if flattening is permitted. Return 0 if not. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_QueryFlattener)) != uint32(0) { return 0 } pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc _ = libc.Int32FromInt32(0) pSubitem = pSrc + 8 + uintptr(iFrom)*72 iParent = (*TSrcItem)(unsafe.Pointer(pSubitem)).FiCursor pSub = (*TSrcItem)(unsafe.Pointer(pSubitem)).FpSelect _ = libc.Int32FromInt32(0) if (*TSelect)(unsafe.Pointer(p)).FpWin != 0 || (*TSelect)(unsafe.Pointer(pSub)).FpWin != 0 { return 0 } /* Restriction (25) */ pSubSrc = (*TSelect)(unsafe.Pointer(pSub)).FpSrc _ = libc.Int32FromInt32(0) /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants, ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET ** because they could be computed at compile-time. But when LIMIT and OFFSET ** became arbitrary expressions, we were forced to add restrictions (13) ** and (14). */ if (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 && (*TSelect)(unsafe.Pointer(p)).FpLimit != 0 { return 0 } /* Restriction (13) */ if (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 && (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub)).FpLimit)).FpRight != 0 { return 0 } /* Restriction (14) */ if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Compound) != uint32(0) && (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 { return 0 /* Restriction (15) */ } if (*TSrcList)(unsafe.Pointer(pSubSrc)).FnSrc == 0 { return 0 } /* Restriction (7) */ if (*TSelect)(unsafe.Pointer(pSub)).FselFlags&uint32(SF_Distinct) != 0 { return 0 } /* Restriction (4) */ if (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 && ((*TSrcList)(unsafe.Pointer(pSrc)).FnSrc > int32(1) || isAgg != 0) { return 0 /* Restrictions (8)(9) */ } if (*TSelect)(unsafe.Pointer(p)).FpOrderBy != 0 && (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy != 0 { return 0 /* Restriction (11) */ } if isAgg != 0 && (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy != 0 { return 0 } /* Restriction (16) */ if (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 && (*TSelect)(unsafe.Pointer(p)).FpWhere != 0 { return 0 } /* Restriction (19) */ if (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Distinct) != uint32(0) { return 0 /* Restriction (21) */ } if (*TSelect)(unsafe.Pointer(pSub)).FselFlags&uint32(libc.Int32FromInt32(SF_Recursive)) != 0 { return 0 /* Restrictions (22) */ } /* ** If the subquery is the right operand of a LEFT JOIN, then the ** subquery may not be a join itself (3a). Example of why this is not ** allowed: ** ** t1 LEFT OUTER JOIN (t2 JOIN t3) ** ** If we flatten the above, we would get ** ** (t1 LEFT OUTER JOIN t2) JOIN t3 ** ** which is not at all the same thing. ** ** See also tickets #306, #350, and #3300. */ if int32((*TSrcItem)(unsafe.Pointer(pSubitem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_OUTER)|libc.Int32FromInt32(JT_LTORJ)) != 0 { if (*TSrcList)(unsafe.Pointer(pSubSrc)).FnSrc > int32(1) || int32((*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pSubSrc + 8))).FpTab)).FeTabType) == int32(TABTYP_VTAB) || (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Distinct) != uint32(0) || int32((*TSrcItem)(unsafe.Pointer(pSubitem)).Ffg.Fjointype)&int32(JT_RIGHT) != 0 { return 0 } isOuterJoin = int32(1) } _ = libc.Int32FromInt32(0) /* True by restriction (7) */ if iFrom > 0 && int32((*(*TSrcItem)(unsafe.Pointer(pSubSrc + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 { return 0 /* Restriction (27a) */ } /* Condition (28) is blocked by the caller */ _ = libc.Int32FromInt32(0) /* Restriction (17): If the sub-query is a compound SELECT, then it must ** use only the UNION ALL operator. And none of the simple select queries ** that make up the compound SELECT are allowed to be aggregate or distinct ** queries. */ if (*TSelect)(unsafe.Pointer(pSub)).FpPrior != 0 { if (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy != 0 { return 0 /* Restriction (20) */ } if isAgg != 0 || (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Distinct) != uint32(0) || isOuterJoin > 0 { return 0 /* (17d1), (17d2), or (17f) */ } pSub1 = pSub for { if !(pSub1 != 0) { break } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TSelect)(unsafe.Pointer(pSub1)).FselFlags&uint32(libc.Int32FromInt32(SF_Distinct)|libc.Int32FromInt32(SF_Aggregate)) != uint32(0) || (*TSelect)(unsafe.Pointer(pSub1)).FpPrior != 0 && int32((*TSelect)(unsafe.Pointer(pSub1)).Fop) != int32(TK_ALL) || (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub1)).FpSrc)).FnSrc < int32(1) || (*TSelect)(unsafe.Pointer(pSub1)).FpWin != 0 { return 0 } if iFrom > 0 && int32((*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub1)).FpSrc + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 { /* Without this restriction, the JT_LTORJ flag would end up being ** omitted on left-hand tables of the right join that is being ** flattened. */ return 0 /* Restrictions (17g), (27b) */ } goto _1 _1: ; pSub1 = (*TSelect)(unsafe.Pointer(pSub1)).FpPrior } /* Restriction (18). */ if (*TSelect)(unsafe.Pointer(p)).FpOrderBy != 0 { ii = 0 for { if !(ii < (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpOrderBy)).FnExpr) { break } if int32(*(*Tu16)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpOrderBy + 8 + uintptr(ii)*20 + 16))) == 0 { return 0 } goto _2 _2: ; ii++ } } /* Restriction (23) */ if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Recursive) != 0 { return 0 } /* Restriction (17h) */ if _compoundHasDifferentAffinities(tls, pSub) != 0 { return 0 } if (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc > int32(1) { if (*TParse)(unsafe.Pointer(pParse)).FnSelect > int32(500) { return 0 } if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_FlttnUnionAll)) != uint32(0) { return 0 } aCsrMap = _sqlite3DbMallocZero(tls, db, uint64((int64((*TParse)(unsafe.Pointer(pParse)).FnTab)+int64(1))*int64(4))) if aCsrMap != 0 { *(*int32)(unsafe.Pointer(aCsrMap)) = (*TParse)(unsafe.Pointer(pParse)).FnTab } } } /***** If we reach this point, flattening is permitted. *****/ /* Authorize the subquery */ (*TParse)(unsafe.Pointer(pParse)).FzAuthContext = (*TSrcItem)(unsafe.Pointer(pSubitem)).FzName _sqlite3AuthCheck(tls, pParse, int32(SQLITE_SELECT), uintptr(0), uintptr(0), uintptr(0)) (*TParse)(unsafe.Pointer(pParse)).FzAuthContext = zSavedAuthContext /* Delete the transient structures associated with the subquery */ pSub1 = (*TSrcItem)(unsafe.Pointer(pSubitem)).FpSelect _sqlite3DbFree(tls, db, (*TSrcItem)(unsafe.Pointer(pSubitem)).FzDatabase) _sqlite3DbFree(tls, db, (*TSrcItem)(unsafe.Pointer(pSubitem)).FzName) _sqlite3DbFree(tls, db, (*TSrcItem)(unsafe.Pointer(pSubitem)).FzAlias) (*TSrcItem)(unsafe.Pointer(pSubitem)).FzDatabase = uintptr(0) (*TSrcItem)(unsafe.Pointer(pSubitem)).FzName = uintptr(0) (*TSrcItem)(unsafe.Pointer(pSubitem)).FzAlias = uintptr(0) (*TSrcItem)(unsafe.Pointer(pSubitem)).FpSelect = uintptr(0) _ = libc.Int32FromInt32(0) /* If the sub-query is a compound SELECT statement, then (by restrictions ** 17 and 18 above) it must be a UNION ALL and the parent query must ** be of the form: ** ** SELECT FROM () ** ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or ** OFFSET clauses and joins them to the left-hand-side of the original ** using UNION ALL operators. In this case N is the number of simple ** select statements in the compound sub-query. ** ** Example: ** ** SELECT a+1 FROM ( ** SELECT x FROM tab ** UNION ALL ** SELECT y FROM tab ** UNION ALL ** SELECT abs(z*2) FROM tab2 ** ) WHERE a!=5 ORDER BY 1 ** ** Transformed into: ** ** SELECT x+1 FROM tab WHERE x+1!=5 ** UNION ALL ** SELECT y+1 FROM tab WHERE y+1!=5 ** UNION ALL ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5 ** ORDER BY 1 ** ** We call this the "compound-subquery flattening". */ pSub = (*TSelect)(unsafe.Pointer(pSub)).FpPrior for { if !(pSub != 0) { break } pOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy pLimit = (*TSelect)(unsafe.Pointer(p)).FpLimit pPrior = (*TSelect)(unsafe.Pointer(p)).FpPrior pItemTab = (*TSrcItem)(unsafe.Pointer(pSubitem)).FpTab (*TSrcItem)(unsafe.Pointer(pSubitem)).FpTab = uintptr(0) (*TSelect)(unsafe.Pointer(p)).FpOrderBy = uintptr(0) (*TSelect)(unsafe.Pointer(p)).FpPrior = uintptr(0) (*TSelect)(unsafe.Pointer(p)).FpLimit = uintptr(0) pNew = _sqlite3SelectDup(tls, db, p, 0) (*TSelect)(unsafe.Pointer(p)).FpLimit = pLimit (*TSelect)(unsafe.Pointer(p)).FpOrderBy = pOrderBy (*TSelect)(unsafe.Pointer(p)).Fop = uint8(TK_ALL) (*TSrcItem)(unsafe.Pointer(pSubitem)).FpTab = pItemTab if pNew == uintptr(0) { (*TSelect)(unsafe.Pointer(p)).FpPrior = pPrior } else { v5 = pParse + 108 *(*int32)(unsafe.Pointer(v5))++ v4 = *(*int32)(unsafe.Pointer(v5)) (*TSelect)(unsafe.Pointer(pNew)).FselId = uint32(v4) if aCsrMap != 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 { _renumberCursors(tls, pParse, pNew, iFrom, aCsrMap) } (*TSelect)(unsafe.Pointer(pNew)).FpPrior = pPrior if pPrior != 0 { (*TSelect)(unsafe.Pointer(pPrior)).FpNext = pNew } (*TSelect)(unsafe.Pointer(pNew)).FpNext = p (*TSelect)(unsafe.Pointer(p)).FpPrior = pNew } _ = libc.Int32FromInt32(0) goto _3 _3: ; pSub = (*TSelect)(unsafe.Pointer(pSub)).FpPrior } _sqlite3DbFree(tls, db, aCsrMap) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { (*TSrcItem)(unsafe.Pointer(pSubitem)).FpSelect = pSub1 return int32(1) } /* Defer deleting the Table object associated with the ** subquery until code generation is ** complete, since there may still exist Expr.pTab entries that ** refer to the subquery even after flattening. Ticket #3346. ** ** pSubitem->pTab is always non-NULL by test restrictions and tests above. */ if (*TSrcItem)(unsafe.Pointer(pSubitem)).FpTab != uintptr(0) { pTabToDel = (*TSrcItem)(unsafe.Pointer(pSubitem)).FpTab if (*TTable)(unsafe.Pointer(pTabToDel)).FnTabRef == uint32(1) { if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 { v6 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel } else { v6 = pParse } pToplevel = v6 _sqlite3ParserAddCleanup(tls, pToplevel, __ccgo_fp(_sqlite3DeleteTableGeneric), pTabToDel) } else { (*TTable)(unsafe.Pointer(pTabToDel)).FnTabRef-- } (*TSrcItem)(unsafe.Pointer(pSubitem)).FpTab = uintptr(0) } /* The following loop runs once for each term in a compound-subquery ** flattening (as described above). If we are doing a different kind ** of flattening - a flattening other than a compound-subquery flattening - ** then this loop only runs once. ** ** This loop moves all of the FROM elements of the subquery into the ** the FROM clause of the outer query. Before doing this, remember ** the cursor number for the original outer query FROM element in ** iParent. The iParent cursor will never be used. Subsequent code ** will scan expressions looking for iParent references and replace ** those references with expressions that resolve to the subquery FROM ** elements we are now copying in. */ pSub = pSub1 pParent = p for { if !(pParent != 0) { break } jointype = uint8(0) ltorj = uint8(int32((*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(iFrom)*72))).Ffg.Fjointype) & int32(JT_LTORJ)) _ = libc.Int32FromInt32(0) pSubSrc = (*TSelect)(unsafe.Pointer(pSub)).FpSrc /* FROM clause of subquery */ nSubSrc = (*TSrcList)(unsafe.Pointer(pSubSrc)).FnSrc /* Number of terms in subquery FROM clause */ pSrc = (*TSelect)(unsafe.Pointer(pParent)).FpSrc /* FROM clause of the outer query */ if pParent == p { jointype = (*TSrcItem)(unsafe.Pointer(pSubitem)).Ffg.Fjointype /* First time through the loop */ } /* The subquery uses a single slot of the FROM clause of the outer ** query. If the subquery has more than one element in its FROM clause, ** then expand the outer query to make space for it to hold all elements ** of the subquery. ** ** Example: ** ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB; ** ** The outer query has 3 slots in its FROM clause. One slot of the ** outer query (the middle slot) is used by the subquery. The next ** block of code will expand the outer query FROM clause to 4 slots. ** The middle slot is expanded to two slots in order to make space ** for the two elements in the FROM clause of the subquery. */ if nSubSrc > int32(1) { pSrc = _sqlite3SrcListEnlarge(tls, pParse, pSrc, nSubSrc-int32(1), iFrom+int32(1)) if pSrc == uintptr(0) { break } (*TSelect)(unsafe.Pointer(pParent)).FpSrc = pSrc } /* Transfer the FROM clause terms from the subquery into the ** outer query. */ i = 0 for { if !(i < nSubSrc) { break } pItem = pSrc + 8 + uintptr(i+iFrom)*72 if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x400>>10)) != 0 { _sqlite3IdListDelete(tls, db, *(*uintptr)(unsafe.Pointer(pItem + 48))) } _ = libc.Int32FromInt32(0) *(*TSrcItem)(unsafe.Pointer(pItem)) = *(*TSrcItem)(unsafe.Pointer(pSubSrc + 8 + uintptr(i)*72)) p9 = pItem + 36 *(*Tu8)(unsafe.Pointer(p9)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p9))) | int32(ltorj)) iNewParent = (*(*TSrcItem)(unsafe.Pointer(pSubSrc + 8 + uintptr(i)*72))).FiCursor libc.Xmemset(tls, pSubSrc+8+uintptr(i)*72, 0, uint32(72)) goto _8 _8: ; i++ } p10 = pSrc + 8 + uintptr(iFrom)*72 + 36 *(*Tu8)(unsafe.Pointer(p10)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p10))) & libc.Int32FromInt32(JT_LTORJ)) p11 = pSrc + 8 + uintptr(iFrom)*72 + 36 *(*Tu8)(unsafe.Pointer(p11)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p11))) | (int32(jointype) | int32(ltorj))) /* Now begin substituting subquery result set expressions for ** references to the iParent in the outer query. ** ** Example: ** ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b; ** \ \_____________ subquery __________/ / ** \_____________________ outer query ______________________________/ ** ** We look at every expression in the outer query and every place we see ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". */ if (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy != 0 && (*TSelect)(unsafe.Pointer(pParent)).FselFlags&uint32(SF_NoopOrderBy) == uint32(0) { /* At this point, any non-zero iOrderByCol values indicate that the ** ORDER BY column expression is identical to the iOrderByCol'th ** expression returned by SELECT statement pSub. Since these values ** do not necessarily correspond to columns in SELECT statement pParent, ** zero them before transferring the ORDER BY clause. ** ** Not doing this may cause an error if a subsequent call to this ** function attempts to flatten a compound sub-query into pParent ** (the only way this can happen is if the compound sub-query is ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */ pOrderBy1 = (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pOrderBy1)).FnExpr) { break } *(*Tu16)(unsafe.Pointer(pOrderBy1 + 8 + uintptr(i)*20 + 16)) = uint16(0) goto _12 _12: ; i++ } _ = libc.Int32FromInt32(0) (*TSelect)(unsafe.Pointer(pParent)).FpOrderBy = pOrderBy1 (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy = uintptr(0) } pWhere = (*TSelect)(unsafe.Pointer(pSub)).FpWhere (*TSelect)(unsafe.Pointer(pSub)).FpWhere = uintptr(0) if isOuterJoin > 0 { _sqlite3SetJoinExpr(tls, pWhere, iNewParent, uint32(EP_OuterON)) } if pWhere != 0 { if (*TSelect)(unsafe.Pointer(pParent)).FpWhere != 0 { (*TSelect)(unsafe.Pointer(pParent)).FpWhere = _sqlite3PExpr(tls, pParse, int32(TK_AND), pWhere, (*TSelect)(unsafe.Pointer(pParent)).FpWhere) } else { (*TSelect)(unsafe.Pointer(pParent)).FpWhere = pWhere } } if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 { (*(*TSubstContext)(unsafe.Pointer(bp + 28))).FpParse = pParse (*(*TSubstContext)(unsafe.Pointer(bp + 28))).FiTable = iParent (*(*TSubstContext)(unsafe.Pointer(bp + 28))).FiNewTable = iNewParent (*(*TSubstContext)(unsafe.Pointer(bp + 28))).FisOuterJoin = isOuterJoin (*(*TSubstContext)(unsafe.Pointer(bp + 28))).FpEList = (*TSelect)(unsafe.Pointer(pSub)).FpEList (*(*TSubstContext)(unsafe.Pointer(bp + 28))).FpCList = _findLeftmostExprlist(tls, pSub) _substSelect(tls, bp+28, pParent, 0) } /* The flattened query is a compound if either the inner or the ** outer query is a compound. */ *(*Tu32)(unsafe.Pointer(pParent + 4)) |= (*TSelect)(unsafe.Pointer(pSub)).FselFlags & uint32(SF_Compound) _ = libc.Int32FromInt32(0) /* restriction (17b) */ /* ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y; ** ** One is tempted to try to add a and b to combine the limits. But this ** does not work if either limit is negative. */ if (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 { (*TSelect)(unsafe.Pointer(pParent)).FpLimit = (*TSelect)(unsafe.Pointer(pSub)).FpLimit (*TSelect)(unsafe.Pointer(pSub)).FpLimit = uintptr(0) } /* Recompute the SrcItem.colUsed masks for the flattened ** tables. */ i = 0 for { if !(i < nSubSrc) { break } _recomputeColumnsUsed(tls, pParent, pSrc+8+uintptr(i+iFrom)*72) goto _13 _13: ; i++ } goto _7 _7: ; pParent = (*TSelect)(unsafe.Pointer(pParent)).FpPrior pSub = (*TSelect)(unsafe.Pointer(pSub)).FpPrior } /* Finally, delete what is left of the subquery and return success. */ _sqlite3AggInfoPersistWalkerInit(tls, bp, pParse) _sqlite3WalkSelect(tls, bp, pSub1) _sqlite3SelectDelete(tls, db, pSub1) return int32(1) } // C documentation // // /* // ** A structure to keep track of all of the column values that are fixed to // ** a known value due to WHERE clause constraints of the form COLUMN=VALUE. // */ type TWhereConst = struct { FpParse uintptr FpOomFault uintptr FnConst int32 FnChng int32 FbHasAffBlob int32 FmExcludeOn Tu32 FapExpr uintptr } type WhereConst = TWhereConst type TWhereConst1 = struct { FpParse uintptr FpOomFault uintptr FnConst int32 FnChng int32 FbHasAffBlob int32 FmExcludeOn Tu32 FapExpr uintptr } type WhereConst1 = TWhereConst1 // C documentation // // /* // ** Add a new entry to the pConst object. Except, do not add duplicate // ** pColumn entries. Also, do not add if doing so would not be appropriate. // ** // ** The caller guarantees the pColumn is a column and pValue is a constant. // ** This routine has to do some additional checks before completing the // ** insert. // */ func _constInsert(tls *libc.TLS, pConst uintptr, pColumn uintptr, pValue uintptr, pExpr uintptr) { var i int32 var pE2 uintptr _, _ = i, pE2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(pColumn)).Fflags&uint32(libc.Int32FromInt32(EP_FixedCol)) != uint32(0) { return } if int32(_sqlite3ExprAffinity(tls, pValue)) != 0 { return } if !(_sqlite3IsBinary(tls, _sqlite3ExprCompareCollSeq(tls, (*TWhereConst)(unsafe.Pointer(pConst)).FpParse, pExpr)) != 0) { return } /* 2018-10-25 ticket [cf5ed20f] ** Make sure the same pColumn is not inserted more than once */ i = 0 for { if !(i < (*TWhereConst)(unsafe.Pointer(pConst)).FnConst) { break } pE2 = *(*uintptr)(unsafe.Pointer((*TWhereConst)(unsafe.Pointer(pConst)).FapExpr + uintptr(i*int32(2))*4)) _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(pE2)).FiTable == (*TExpr)(unsafe.Pointer(pColumn)).FiTable && int32((*TExpr)(unsafe.Pointer(pE2)).FiColumn) == int32((*TExpr)(unsafe.Pointer(pColumn)).FiColumn) { return /* Already present. Return without doing anything. */ } goto _1 _1: ; i++ } if int32(_sqlite3ExprAffinity(tls, pColumn)) == int32(SQLITE_AFF_BLOB) { (*TWhereConst)(unsafe.Pointer(pConst)).FbHasAffBlob = int32(1) } (*TWhereConst)(unsafe.Pointer(pConst)).FnConst++ (*TWhereConst)(unsafe.Pointer(pConst)).FapExpr = _sqlite3DbReallocOrFree(tls, (*TParse)(unsafe.Pointer((*TWhereConst)(unsafe.Pointer(pConst)).FpParse)).Fdb, (*TWhereConst)(unsafe.Pointer(pConst)).FapExpr, uint64(uint32((*TWhereConst)(unsafe.Pointer(pConst)).FnConst*int32(2))*uint32(4))) if (*TWhereConst)(unsafe.Pointer(pConst)).FapExpr == uintptr(0) { (*TWhereConst)(unsafe.Pointer(pConst)).FnConst = 0 } else { *(*uintptr)(unsafe.Pointer((*TWhereConst)(unsafe.Pointer(pConst)).FapExpr + uintptr((*TWhereConst)(unsafe.Pointer(pConst)).FnConst*int32(2)-int32(2))*4)) = pColumn *(*uintptr)(unsafe.Pointer((*TWhereConst)(unsafe.Pointer(pConst)).FapExpr + uintptr((*TWhereConst)(unsafe.Pointer(pConst)).FnConst*int32(2)-int32(1))*4)) = pValue } } // C documentation // // /* // ** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE // ** is a constant expression and where the term must be true because it // ** is part of the AND-connected terms of the expression. For each term // ** found, add it to the pConst structure. // */ func _findConstInWhere(tls *libc.TLS, pConst uintptr, pExpr uintptr) { var pLeft, pRight uintptr _, _ = pLeft, pRight if pExpr == uintptr(0) { return } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&(*TWhereConst)(unsafe.Pointer(pConst)).FmExcludeOn != uint32(0) { return } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AND) { _findConstInWhere(tls, pConst, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) _findConstInWhere(tls, pConst, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) return } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_EQ) { return } pRight = (*TExpr)(unsafe.Pointer(pExpr)).FpRight pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pRight)).Fop) == int32(TK_COLUMN) && _sqlite3ExprIsConstant(tls, pLeft) != 0 { _constInsert(tls, pConst, pRight, pLeft, pExpr) } if int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) == int32(TK_COLUMN) && _sqlite3ExprIsConstant(tls, pRight) != 0 { _constInsert(tls, pConst, pLeft, pRight, pExpr) } } // C documentation // // /* // ** This is a helper function for Walker callback propagateConstantExprRewrite(). // ** // ** Argument pExpr is a candidate expression to be replaced by a value. If // ** pExpr is equivalent to one of the columns named in pWalker->u.pConst, // ** then overwrite it with the corresponding value. Except, do not do so // ** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr // ** is SQLITE_AFF_BLOB. // */ func _propagateConstantExprRewriteOne(tls *libc.TLS, pConst uintptr, pExpr uintptr, bIgnoreAffBlob int32) (r int32) { var i int32 var pColumn uintptr _, _ = i, pColumn if *(*Tu8)(unsafe.Pointer((*TWhereConst)(unsafe.Pointer(pConst)).FpOomFault)) != 0 { return int32(WRC_Prune) } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLUMN) { return WRC_Continue } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&(uint32(EP_FixedCol)|(*TWhereConst)(unsafe.Pointer(pConst)).FmExcludeOn) != uint32(0) { return WRC_Continue } i = 0 for { if !(i < (*TWhereConst)(unsafe.Pointer(pConst)).FnConst) { break } pColumn = *(*uintptr)(unsafe.Pointer((*TWhereConst)(unsafe.Pointer(pConst)).FapExpr + uintptr(i*int32(2))*4)) if pColumn == pExpr { goto _1 } if (*TExpr)(unsafe.Pointer(pColumn)).FiTable != (*TExpr)(unsafe.Pointer(pExpr)).FiTable { goto _1 } if int32((*TExpr)(unsafe.Pointer(pColumn)).FiColumn) != int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) { goto _1 } if bIgnoreAffBlob != 0 && int32(_sqlite3ExprAffinity(tls, pColumn)) == int32(SQLITE_AFF_BLOB) { break } /* A match is found. Add the EP_FixedCol property */ (*TWhereConst)(unsafe.Pointer(pConst)).FnChng++ *(*Tu32)(unsafe.Pointer(pExpr + 4)) &= uint32(^libc.Int32FromInt32(EP_Leaf)) *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_FixedCol)) _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer(pExpr)).FpLeft = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer((*TWhereConst)(unsafe.Pointer(pConst)).FpParse)).Fdb, *(*uintptr)(unsafe.Pointer((*TWhereConst)(unsafe.Pointer(pConst)).FapExpr + uintptr(i*int32(2)+int32(1))*4)), 0) if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer((*TWhereConst)(unsafe.Pointer(pConst)).FpParse)).Fdb)).FmallocFailed != 0 { return int32(WRC_Prune) } break goto _1 _1: ; i++ } return int32(WRC_Prune) } // C documentation // // /* // ** This is a Walker expression callback. pExpr is a node from the WHERE // ** clause of a SELECT statement. This function examines pExpr to see if // ** any substitutions based on the contents of pWalker->u.pConst should // ** be made to pExpr or its immediate children. // ** // ** A substitution is made if: // ** // ** + pExpr is a column with an affinity other than BLOB that matches // ** one of the columns in pWalker->u.pConst, or // ** // ** + pExpr is a binary comparison operator (=, <=, >=, <, >) that // ** uses an affinity other than TEXT and one of its immediate // ** children is a column that matches one of the columns in // ** pWalker->u.pConst. // */ func _propagateConstantExprRewrite(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var pConst uintptr _ = pConst pConst = *(*uintptr)(unsafe.Pointer(pWalker + 24)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TWhereConst)(unsafe.Pointer(pConst)).FbHasAffBlob != 0 { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) >= int32(TK_EQ) && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) <= int32(TK_GE) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_IS) { _propagateConstantExprRewriteOne(tls, pConst, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, 0) if *(*Tu8)(unsafe.Pointer((*TWhereConst)(unsafe.Pointer(pConst)).FpOomFault)) != 0 { return int32(WRC_Prune) } if int32(_sqlite3ExprAffinity(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)) != int32(SQLITE_AFF_TEXT) { _propagateConstantExprRewriteOne(tls, pConst, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, 0) } } } return _propagateConstantExprRewriteOne(tls, pConst, pExpr, (*TWhereConst)(unsafe.Pointer(pConst)).FbHasAffBlob) } // C documentation // // /* // ** The WHERE-clause constant propagation optimization. // ** // ** If the WHERE clause contains terms of the form COLUMN=CONSTANT or // ** CONSTANT=COLUMN that are top-level AND-connected terms that are not // ** part of a ON clause from a LEFT JOIN, then throughout the query // ** replace all other occurrences of COLUMN with CONSTANT. // ** // ** For example, the query: // ** // ** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b // ** // ** Is transformed into // ** // ** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39 // ** // ** Return true if any transformations where made and false if not. // ** // ** Implementation note: Constant propagation is tricky due to affinity // ** and collating sequence interactions. Consider this example: // ** // ** CREATE TABLE t1(a INT,b TEXT); // ** INSERT INTO t1 VALUES(123,'0123'); // ** SELECT * FROM t1 WHERE a=123 AND b=a; // ** SELECT * FROM t1 WHERE a=123 AND b=123; // ** // ** The two SELECT statements above should return different answers. b=a // ** is always true because the comparison uses numeric affinity, but b=123 // ** is false because it uses text affinity and '0123' is not the same as '123'. // ** To work around this, the expression tree is not actually changed from // ** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol // ** and the "123" value is hung off of the pLeft pointer. Code generator // ** routines know to generate the constant "123" instead of looking up the // ** column value. Also, to avoid collation problems, this optimization is // ** only attempted if the "a=123" term uses the default BINARY collation. // ** // ** 2021-05-25 forum post 6a06202608: Another troublesome case is... // ** // ** CREATE TABLE t1(x); // ** INSERT INTO t1 VALUES(10.0); // ** SELECT 1 FROM t1 WHERE x=10 AND x LIKE 10; // ** // ** The query should return no rows, because the t1.x value is '10.0' not '10' // ** and '10.0' is not LIKE '10'. But if we are not careful, the first WHERE // ** term "x=10" will cause the second WHERE term to become "10 LIKE 10", // ** resulting in a false positive. To avoid this, constant propagation for // ** columns with BLOB affinity is only allowed if the constant is used with // ** operators ==, <=, <, >=, >, or IS in a way that will cause the correct // ** type conversions to occur. See logic associated with the bHasAffBlob flag // ** for details. // */ func _propagateConstants(tls *libc.TLS, pParse uintptr, p uintptr) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var nChng int32 var _ /* w at bp+28 */ TWalker var _ /* x at bp+0 */ TWhereConst _ = nChng nChng = 0 (*(*TWhereConst)(unsafe.Pointer(bp))).FpParse = pParse (*(*TWhereConst)(unsafe.Pointer(bp))).FpOomFault = (*TParse)(unsafe.Pointer(pParse)).Fdb + 87 for cond := true; cond; cond = (*(*TWhereConst)(unsafe.Pointer(bp))).FnChng != 0 { (*(*TWhereConst)(unsafe.Pointer(bp))).FnConst = 0 (*(*TWhereConst)(unsafe.Pointer(bp))).FnChng = 0 (*(*TWhereConst)(unsafe.Pointer(bp))).FapExpr = uintptr(0) (*(*TWhereConst)(unsafe.Pointer(bp))).FbHasAffBlob = 0 if (*TSelect)(unsafe.Pointer(p)).FpSrc != uintptr(0) && (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc)).FnSrc > 0 && int32((*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 { /* Do not propagate constants on any ON clause if there is a ** RIGHT JOIN anywhere in the query */ (*(*TWhereConst)(unsafe.Pointer(bp))).FmExcludeOn = uint32(libc.Int32FromInt32(EP_InnerON) | libc.Int32FromInt32(EP_OuterON)) } else { /* Do not propagate constants through the ON clause of a LEFT JOIN */ (*(*TWhereConst)(unsafe.Pointer(bp))).FmExcludeOn = uint32(EP_OuterON) } _findConstInWhere(tls, bp, (*TSelect)(unsafe.Pointer(p)).FpWhere) if (*(*TWhereConst)(unsafe.Pointer(bp))).FnConst != 0 { libc.Xmemset(tls, bp+28, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp + 28))).FpParse = pParse (*(*TWalker)(unsafe.Pointer(bp + 28))).FxExprCallback = __ccgo_fp(_propagateConstantExprRewrite) (*(*TWalker)(unsafe.Pointer(bp + 28))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop) (*(*TWalker)(unsafe.Pointer(bp + 28))).FxSelectCallback2 = uintptr(0) (*(*TWalker)(unsafe.Pointer(bp + 28))).FwalkerDepth = 0 *(*uintptr)(unsafe.Pointer(bp + 28 + 24)) = bp _sqlite3WalkExpr(tls, bp+28, (*TSelect)(unsafe.Pointer(p)).FpWhere) _sqlite3DbFree(tls, (*TParse)(unsafe.Pointer((*(*TWhereConst)(unsafe.Pointer(bp))).FpParse)).Fdb, (*(*TWhereConst)(unsafe.Pointer(bp))).FapExpr) nChng += (*(*TWhereConst)(unsafe.Pointer(bp))).FnChng } } return nChng } // C documentation // // /* // ** This function is called to determine whether or not it is safe to // ** push WHERE clause expression pExpr down to FROM clause sub-query // ** pSubq, which contains at least one window function. Return 1 // ** if it is safe and the expression should be pushed down, or 0 // ** otherwise. // ** // ** It is only safe to push the expression down if it consists only // ** of constants and copies of expressions that appear in the PARTITION // ** BY clause of all window function used by the sub-query. It is safe // ** to filter out entire partitions, but not rows within partitions, as // ** this may change the results of the window functions. // ** // ** At the time this function is called it is guaranteed that // ** // ** * the sub-query uses only one distinct window frame, and // ** * that the window frame has a PARTITION BY clause. // */ func _pushDownWindowCheck(tls *libc.TLS, pParse uintptr, pSubq uintptr, pExpr uintptr) (r int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return _sqlite3ExprIsConstantOrGroupBy(tls, pParse, pExpr, (*TWindow)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSubq)).FpWin)).FpPartition) } // C documentation // // /* // ** Make copies of relevant WHERE clause terms of the outer query into // ** the WHERE clause of subquery. Example: // ** // ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10; // ** // ** Transformed into: // ** // ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10) // ** WHERE x=5 AND y=10; // ** // ** The hope is that the terms added to the inner query will make it more // ** efficient. // ** // ** Do not attempt this optimization if: // ** // ** (1) (** This restriction was removed on 2017-09-29. We used to // ** disallow this optimization for aggregate subqueries, but now // ** it is allowed by putting the extra terms on the HAVING clause. // ** The added HAVING clause is pointless if the subquery lacks // ** a GROUP BY clause. But such a HAVING clause is also harmless // ** so there does not appear to be any reason to add extra logic // ** to suppress it. **) // ** // ** (2) The inner query is the recursive part of a common table expression. // ** // ** (3) The inner query has a LIMIT clause (since the changes to the WHERE // ** clause would change the meaning of the LIMIT). // ** // ** (4) The inner query is the right operand of a LEFT JOIN and the // ** expression to be pushed down does not come from the ON clause // ** on that LEFT JOIN. // ** // ** (5) The WHERE clause expression originates in the ON or USING clause // ** of a LEFT JOIN where iCursor is not the right-hand table of that // ** left join. An example: // ** // ** SELECT * // ** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa // ** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2) // ** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2); // ** // ** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9). // ** But if the (b2=2) term were to be pushed down into the bb subquery, // ** then the (1,1,NULL) row would be suppressed. // ** // ** (6) Window functions make things tricky as changes to the WHERE clause // ** of the inner query could change the window over which window // ** functions are calculated. Therefore, do not attempt the optimization // ** if: // ** // ** (6a) The inner query uses multiple incompatible window partitions. // ** // ** (6b) The inner query is a compound and uses window-functions. // ** // ** (6c) The WHERE clause does not consist entirely of constants and // ** copies of expressions found in the PARTITION BY clause of // ** all window-functions used by the sub-query. It is safe to // ** filter out entire partitions, as this does not change the // ** window over which any window-function is calculated. // ** // ** (7) The inner query is a Common Table Expression (CTE) that should // ** be materialized. (This restriction is implemented in the calling // ** routine.) // ** // ** (8) If the subquery is a compound that uses UNION, INTERSECT, // ** or EXCEPT, then all of the result set columns for all arms of // ** the compound must use the BINARY collating sequence. // ** // ** (9) All three of the following are true: // ** // ** (9a) The WHERE clause expression originates in the ON or USING clause // ** of a join (either an INNER or an OUTER join), and // ** // ** (9b) The subquery is to the right of the ON/USING clause // ** // ** (9c) There is a RIGHT JOIN (or FULL JOIN) in between the ON/USING // ** clause and the subquery. // ** // ** Without this restriction, the push-down optimization might move // ** the ON/USING filter expression from the left side of a RIGHT JOIN // ** over to the right side, which leads to incorrect answers. See // ** also restriction (6) in sqlite3ExprIsSingleTableConstraint(). // ** // ** (10) The inner query is not the right-hand table of a RIGHT JOIN. // ** // ** (11) The subquery is not a VALUES clause // ** // ** (12) The WHERE clause is not "rowid ISNULL" or the equivalent. This // ** case only comes up if SQLite is compiled using // ** SQLITE_ALLOW_ROWID_IN_VIEW. // ** // ** Return 0 if no changes are made and non-zero if one or more WHERE clause // ** terms are duplicated into the subquery. // */ func _pushDownWhereTerms(tls *libc.TLS, pParse uintptr, pSubq uintptr, pWhere uintptr, pSrcList uintptr, iSrc int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var ii, nChng, notUnionAll int32 var op Tu8 var pColl, pList, pNew, pSel, pSrc uintptr var _ /* x at bp+0 */ TSubstContext _, _, _, _, _, _, _, _, _ = ii, nChng, notUnionAll, op, pColl, pList, pNew, pSel, pSrc /* The subquery FROM term into which WHERE is pushed */ nChng = 0 pSrc = pSrcList + 8 + uintptr(iSrc)*72 if pWhere == uintptr(0) { return 0 } if (*TSelect)(unsafe.Pointer(pSubq)).FselFlags&uint32(libc.Int32FromInt32(SF_Recursive)|libc.Int32FromInt32(SF_MultiPart)) != 0 { return 0 /* restrictions (2) and (11) */ } if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0 { return 0 /* restrictions (10) */ } if (*TSelect)(unsafe.Pointer(pSubq)).FpPrior != 0 { notUnionAll = 0 pSel = pSubq for { if !(pSel != 0) { break } op = (*TSelect)(unsafe.Pointer(pSel)).Fop _ = libc.Int32FromInt32(0) if int32(op) != int32(TK_ALL) && int32(op) != int32(TK_SELECT) { notUnionAll = int32(1) } if (*TSelect)(unsafe.Pointer(pSel)).FpWin != 0 { return 0 } /* restriction (6b) */ goto _1 _1: ; pSel = (*TSelect)(unsafe.Pointer(pSel)).FpPrior } if notUnionAll != 0 { /* If any of the compound arms are connected using UNION, INTERSECT, ** or EXCEPT, then we must ensure that none of the columns use a ** non-BINARY collating sequence. */ pSel = pSubq for { if !(pSel != 0) { break } pList = (*TSelect)(unsafe.Pointer(pSel)).FpEList _ = libc.Int32FromInt32(0) ii = 0 for { if !(ii < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } pColl = _sqlite3ExprCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*20))).FpExpr) if !(_sqlite3IsBinary(tls, pColl) != 0) { return 0 /* Restriction (8) */ } goto _3 _3: ; ii++ } goto _2 _2: ; pSel = (*TSelect)(unsafe.Pointer(pSel)).FpPrior } } } else { if (*TSelect)(unsafe.Pointer(pSubq)).FpWin != 0 && (*TWindow)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSubq)).FpWin)).FpPartition == uintptr(0) { return 0 } } if (*TSelect)(unsafe.Pointer(pSubq)).FpLimit != uintptr(0) { return 0 /* restriction (3) */ } for int32((*TExpr)(unsafe.Pointer(pWhere)).Fop) == int32(TK_AND) { nChng += _pushDownWhereTerms(tls, pParse, pSubq, (*TExpr)(unsafe.Pointer(pWhere)).FpRight, pSrcList, iSrc) pWhere = (*TExpr)(unsafe.Pointer(pWhere)).FpLeft } if _sqlite3ExprIsSingleTableConstraint(tls, pWhere, pSrcList, iSrc) != 0 { nChng++ *(*Tu32)(unsafe.Pointer(pSubq + 4)) |= uint32(SF_PushDown) for pSubq != 0 { pNew = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pWhere, 0) _unsetJoinExpr(tls, pNew, -int32(1), int32(1)) (*(*TSubstContext)(unsafe.Pointer(bp))).FpParse = pParse (*(*TSubstContext)(unsafe.Pointer(bp))).FiTable = (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor (*(*TSubstContext)(unsafe.Pointer(bp))).FiNewTable = (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor (*(*TSubstContext)(unsafe.Pointer(bp))).FisOuterJoin = 0 (*(*TSubstContext)(unsafe.Pointer(bp))).FpEList = (*TSelect)(unsafe.Pointer(pSubq)).FpEList (*(*TSubstContext)(unsafe.Pointer(bp))).FpCList = _findLeftmostExprlist(tls, pSubq) pNew = _substExpr(tls, bp, pNew) if (*TSelect)(unsafe.Pointer(pSubq)).FpWin != 0 && 0 == _pushDownWindowCheck(tls, pParse, pSubq, pNew) { /* Restriction 6c has prevented push-down in this case */ _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pNew) nChng-- break } if (*TSelect)(unsafe.Pointer(pSubq)).FselFlags&uint32(SF_Aggregate) != 0 { (*TSelect)(unsafe.Pointer(pSubq)).FpHaving = _sqlite3ExprAnd(tls, pParse, (*TSelect)(unsafe.Pointer(pSubq)).FpHaving, pNew) } else { (*TSelect)(unsafe.Pointer(pSubq)).FpWhere = _sqlite3ExprAnd(tls, pParse, (*TSelect)(unsafe.Pointer(pSubq)).FpWhere, pNew) } pSubq = (*TSelect)(unsafe.Pointer(pSubq)).FpPrior } } return nChng } // C documentation // // /* // ** Check to see if a subquery contains result-set columns that are // ** never used. If it does, change the value of those result-set columns // ** to NULL so that they do not cause unnecessary work to compute. // ** // ** Return the number of column that were changed to NULL. // */ func _disableUnusedSubqueryResultColumns(tls *libc.TLS, pItem uintptr) (r int32) { var colUsed, m TBitmask var iCol Tu16 var j, nChng, nCol, v3 int32 var pList, pSub, pTab, pX, pY uintptr var v5 uint64 _, _, _, _, _, _, _, _, _, _, _, _, _ = colUsed, iCol, j, m, nChng, nCol, pList, pSub, pTab, pX, pY, v3, v5 /* Column number */ nChng = 0 /* Columns that may not be NULLed out */ _ = libc.Int32FromInt32(0) if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x8>>3)) != 0 || int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x100>>8)) != 0 { return 0 } _ = libc.Int32FromInt32(0) pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab _ = libc.Int32FromInt32(0) pSub = (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect _ = libc.Int32FromInt32(0) pX = pSub for { if !(pX != 0) { break } if (*TSelect)(unsafe.Pointer(pX)).FselFlags&uint32(libc.Int32FromInt32(SF_Distinct)|libc.Int32FromInt32(SF_Aggregate)) != uint32(0) { return 0 } if (*TSelect)(unsafe.Pointer(pX)).FpPrior != 0 && int32((*TSelect)(unsafe.Pointer(pX)).Fop) != int32(TK_ALL) { /* This optimization does not work for compound subqueries that ** use UNION, INTERSECT, or EXCEPT. Only UNION ALL is allowed. */ return 0 } if (*TSelect)(unsafe.Pointer(pX)).FpWin != 0 { /* This optimization does not work for subqueries that use window ** functions. */ return 0 } goto _1 _1: ; pX = (*TSelect)(unsafe.Pointer(pX)).FpPrior } colUsed = (*TSrcItem)(unsafe.Pointer(pItem)).FcolUsed if (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy != 0 { pList = (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy j = 0 for { if !(j < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } iCol = *(*Tu16)(unsafe.Pointer(pList + 8 + uintptr(j)*20 + 16)) if int32(iCol) > 0 { iCol-- if int32(iCol) >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) { v3 = int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1) } else { v3 = int32(iCol) } colUsed |= libc.Uint64FromInt32(1) << v3 } goto _2 _2: ; j++ } } nCol = int32((*TTable)(unsafe.Pointer(pTab)).FnCol) j = 0 for { if !(j < nCol) { break } if j < int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) { v5 = libc.Uint64FromInt32(1) << j } else { v5 = libc.Uint64FromInt32(1) << (int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1)) } m = v5 if m&colUsed != uint64(0) { goto _4 } pX = pSub for { if !(pX != 0) { break } pY = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pX)).FpEList + 8 + uintptr(j)*20))).FpExpr if int32((*TExpr)(unsafe.Pointer(pY)).Fop) == int32(TK_NULL) { goto _6 } (*TExpr)(unsafe.Pointer(pY)).Fop = uint8(TK_NULL) *(*Tu32)(unsafe.Pointer(pY + 4)) &= uint32(^(libc.Int32FromInt32(EP_Skip) | libc.Int32FromInt32(EP_Unlikely))) *(*Tu32)(unsafe.Pointer(pX + 4)) |= uint32(SF_PushDown) nChng++ goto _6 _6: ; pX = (*TSelect)(unsafe.Pointer(pX)).FpPrior } goto _4 _4: ; j++ } return nChng } // C documentation // // /* // ** The pFunc is the only aggregate function in the query. Check to see // ** if the query is a candidate for the min/max optimization. // ** // ** If the query is a candidate for the min/max optimization, then set // ** *ppMinMax to be an ORDER BY clause to be used for the optimization // ** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on // ** whether pFunc is a min() or max() function. // ** // ** If the query is not a candidate for the min/max optimization, return // ** WHERE_ORDERBY_NORMAL (which must be zero). // ** // ** This routine must be called after aggregate functions have been // ** located but before their arguments have been subjected to aggregate // ** analysis. // */ func _minMaxQuery(tls *libc.TLS, db uintptr, pFunc uintptr, ppMinMax uintptr) (r Tu8) { var eRet int32 var pEList, pOrderBy, zFunc, v1 uintptr var sortFlags Tu8 _, _, _, _, _, _ = eRet, pEList, pOrderBy, sortFlags, zFunc, v1 eRet = WHERE_ORDERBY_NORMAL sortFlags = uint8(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pEList = *(*uintptr)(unsafe.Pointer(pFunc + 20)) if pEList == uintptr(0) || (*TExprList)(unsafe.Pointer(pEList)).FnExpr != int32(1) || (*TExpr)(unsafe.Pointer(pFunc)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) || (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_MinMaxOpt)) != uint32(0) { return uint8(eRet) } _ = libc.Int32FromInt32(0) zFunc = *(*uintptr)(unsafe.Pointer(pFunc + 8)) if _sqlite3StrICmp(tls, zFunc, __ccgo_ts+15550) == 0 { eRet = int32(WHERE_ORDERBY_MIN) if _sqlite3ExprCanBeNull(tls, (*(*TExprList_item)(unsafe.Pointer(pEList + 8))).FpExpr) != 0 { sortFlags = uint8(KEYINFO_ORDER_BIGNULL) } } else { if _sqlite3StrICmp(tls, zFunc, __ccgo_ts+15554) == 0 { eRet = int32(WHERE_ORDERBY_MAX) sortFlags = uint8(KEYINFO_ORDER_DESC) } else { return uint8(eRet) } } v1 = _sqlite3ExprListDup(tls, db, pEList, 0) pOrderBy = v1 *(*uintptr)(unsafe.Pointer(ppMinMax)) = v1 _ = libc.Int32FromInt32(0) if pOrderBy != 0 { (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8))).Ffg.FsortFlags = sortFlags } return uint8(eRet) } // C documentation // // /* // ** The select statement passed as the first argument is an aggregate query. // ** The second argument is the associated aggregate-info object. This // ** function tests if the SELECT is of the form: // ** // ** SELECT count(*) FROM // ** // ** where table is a database table, not a sub-select or view. If the query // ** does match this pattern, then a pointer to the Table object representing // ** is returned. Otherwise, NULL is returned. // ** // ** This routine checks to see if it is safe to use the count optimization. // ** A correct answer is still obtained (though perhaps more slowly) if // ** this routine returns NULL when it could have returned a table pointer. // ** But returning the pointer when NULL should have been returned can // ** result in incorrect answers and/or crashes. So, when in doubt, return NULL. // */ func _isSimpleCount(tls *libc.TLS, p uintptr, pAggInfo uintptr) (r uintptr) { var pExpr, pTab uintptr _, _ = pExpr, pTab _ = libc.Int32FromInt32(0) if (*TSelect)(unsafe.Pointer(p)).FpWhere != 0 || (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr != int32(1) || (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc)).FnSrc != int32(1) || (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpSelect != 0 || (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc != int32(1) || (*TSelect)(unsafe.Pointer(p)).FpHaving != 0 { return uintptr(0) } pTab = (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpTab _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) { return uintptr(0) } pExpr = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList + 8))).FpExpr _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_AGG_FUNCTION) { return uintptr(0) } if (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo != pAggInfo { return uintptr(0) } if (*TFuncDef)(unsafe.Pointer((*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFunc)).FfuncFlags&uint32(SQLITE_FUNC_COUNT) == uint32(0) { return uintptr(0) } _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Distinct)|libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { return uintptr(0) } return pTab } // C documentation // // /* // ** If the source-list item passed as an argument was augmented with an // ** INDEXED BY clause, then try to locate the specified index. If there // ** was such a clause and the named index cannot be found, return // ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate // ** pFrom->pIndex and return SQLITE_OK. // */ func _sqlite3IndexedByLookup(tls *libc.TLS, pParse uintptr, pFrom uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var pIdx, pTab, zIndexedBy uintptr _, _, _ = pIdx, pTab, zIndexedBy pTab = (*TSrcItem)(unsafe.Pointer(pFrom)).FpTab zIndexedBy = *(*uintptr)(unsafe.Pointer(pFrom + 64)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0 && _sqlite3StrICmp(tls, (*TIndex)(unsafe.Pointer(pIdx)).FzName, zIndexedBy) != 0) { break } goto _1 _1: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } if !(pIdx != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19579, libc.VaList(bp+8, zIndexedBy, 0)) (*TParse)(unsafe.Pointer(pParse)).FcheckSchema = uint8(1) return int32(SQLITE_ERROR) } _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pFrom + 68)) = pIdx return SQLITE_OK } // C documentation // // /* // ** Detect compound SELECT statements that use an ORDER BY clause with // ** an alternative collating sequence. // ** // ** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ... // ** // ** These are rewritten as a subquery: // ** // ** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2) // ** ORDER BY ... COLLATE ... // ** // ** This transformation is necessary because the multiSelectOrderBy() routine // ** above that generates the code for a compound SELECT with an ORDER BY clause // ** uses a merge algorithm that requires the same collating sequence on the // ** result columns as on the ORDER BY clause. See ticket // ** http://www.sqlite.org/src/info/6709574d2a // ** // ** This transformation is only needed for EXCEPT, INTERSECT, and UNION. // ** The UNION ALL operator works fine with multiSelectOrderBy() even when // ** there are COLLATE terms in the ORDER BY. // */ func _convertCompoundSelectToSubquery(tls *libc.TLS, pWalker uintptr, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var a, db, pNew, pNewSrc, pParse, pX uintptr var i int32 var _ /* dummy at bp+0 */ TToken _, _, _, _, _, _, _ = a, db, i, pNew, pNewSrc, pParse, pX if (*TSelect)(unsafe.Pointer(p)).FpPrior == uintptr(0) { return WRC_Continue } if (*TSelect)(unsafe.Pointer(p)).FpOrderBy == uintptr(0) { return WRC_Continue } pX = p for { if !(pX != 0 && (int32((*TSelect)(unsafe.Pointer(pX)).Fop) == int32(TK_ALL) || int32((*TSelect)(unsafe.Pointer(pX)).Fop) == int32(TK_SELECT))) { break } goto _1 _1: ; pX = (*TSelect)(unsafe.Pointer(pX)).FpPrior } if pX == uintptr(0) { return WRC_Continue } a = (*TSelect)(unsafe.Pointer(p)).FpOrderBy + 8 /* If iOrderByCol is already non-zero, then it has already been matched ** to a result column of the SELECT statement. This occurs when the ** SELECT is rewritten for window-functions processing and then passed ** to sqlite3SelectPrep() and similar a second time. The rewriting done ** by this function is not required in this case. */ if *(*Tu16)(unsafe.Pointer(a + 16)) != 0 { return WRC_Continue } i = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpOrderBy)).FnExpr - int32(1) for { if !(i >= 0) { break } if (*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(a + uintptr(i)*20))).FpExpr)).Fflags&uint32(EP_Collate) != 0 { break } goto _2 _2: ; i-- } if i < 0 { return WRC_Continue } /* If we reach this point, that means the transformation is required. */ pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse db = (*TParse)(unsafe.Pointer(pParse)).Fdb pNew = _sqlite3DbMallocZero(tls, db, uint64(76)) if pNew == uintptr(0) { return int32(WRC_Abort) } libc.Xmemset(tls, bp, 0, uint32(8)) pNewSrc = _sqlite3SrcListAppendFromTerm(tls, pParse, uintptr(0), uintptr(0), uintptr(0), bp, pNew, uintptr(0)) if pNewSrc == uintptr(0) { return int32(WRC_Abort) } *(*TSelect)(unsafe.Pointer(pNew)) = *(*TSelect)(unsafe.Pointer(p)) (*TSelect)(unsafe.Pointer(p)).FpSrc = pNewSrc (*TSelect)(unsafe.Pointer(p)).FpEList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), _sqlite3Expr(tls, db, int32(TK_ASTERISK), uintptr(0))) (*TSelect)(unsafe.Pointer(p)).Fop = uint8(TK_SELECT) (*TSelect)(unsafe.Pointer(p)).FpWhere = uintptr(0) (*TSelect)(unsafe.Pointer(pNew)).FpGroupBy = uintptr(0) (*TSelect)(unsafe.Pointer(pNew)).FpHaving = uintptr(0) (*TSelect)(unsafe.Pointer(pNew)).FpOrderBy = uintptr(0) (*TSelect)(unsafe.Pointer(p)).FpPrior = uintptr(0) (*TSelect)(unsafe.Pointer(p)).FpNext = uintptr(0) (*TSelect)(unsafe.Pointer(p)).FpWith = uintptr(0) (*TSelect)(unsafe.Pointer(p)).FpWinDefn = uintptr(0) *(*Tu32)(unsafe.Pointer(p + 4)) &= uint32(^libc.Int32FromInt32(SF_Compound)) _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(SF_Converted) _ = libc.Int32FromInt32(0) (*TSelect)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pNew)).FpPrior)).FpNext = pNew (*TSelect)(unsafe.Pointer(pNew)).FpLimit = uintptr(0) return WRC_Continue } // C documentation // // /* // ** Check to see if the FROM clause term pFrom has table-valued function // ** arguments. If it does, leave an error message in pParse and return // ** non-zero, since pFrom is not allowed to be a table-valued function. // */ func _cannotBeFunction(tls *libc.TLS, pParse uintptr, pFrom uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) if int32(uint32(*(*uint16)(unsafe.Pointer(pFrom + 36 + 4))&0x4>>2)) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19597, libc.VaList(bp+8, (*TSrcItem)(unsafe.Pointer(pFrom)).FzName)) return int32(1) } return 0 } // C documentation // // /* // ** Argument pWith (which may be NULL) points to a linked list of nested // ** WITH contexts, from inner to outermost. If the table identified by // ** FROM clause element pItem is really a common-table-expression (CTE) // ** then return a pointer to the CTE definition for that table. Otherwise // ** return NULL. // ** // ** If a non-NULL value is returned, set *ppContext to point to the With // ** object that the returned CTE belongs to. // */ func _searchWith(tls *libc.TLS, pWith uintptr, pItem uintptr, ppContext uintptr) (r uintptr) { var i int32 var p, zName uintptr _, _, _ = i, p, zName zName = (*TSrcItem)(unsafe.Pointer(pItem)).FzName _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) p = pWith for { if !(p != 0) { break } i = 0 for { if !(i < (*TWith)(unsafe.Pointer(p)).FnCte) { break } if _sqlite3StrICmp(tls, zName, (*(*TCte)(unsafe.Pointer(p + 12 + uintptr(i)*24))).FzName) == 0 { *(*uintptr)(unsafe.Pointer(ppContext)) = p return p + 12 + uintptr(i)*24 } goto _2 _2: ; i++ } if (*TWith)(unsafe.Pointer(p)).FbView != 0 { break } goto _1 _1: ; p = (*TWith)(unsafe.Pointer(p)).FpOuter } return uintptr(0) } // C documentation // // /* The code generator maintains a stack of active WITH clauses // ** with the inner-most WITH clause being at the top of the stack. // ** // ** This routine pushes the WITH clause passed as the second argument // ** onto the top of the stack. If argument bFree is true, then this // ** WITH clause will never be popped from the stack but should instead // ** be freed along with the Parse object. In other cases, when // ** bFree==0, the With object will be freed along with the SELECT // ** statement with which it is associated. // ** // ** This routine returns a copy of pWith. Or, if bFree is true and // ** the pWith object is destroyed immediately due to an OOM condition, // ** then this routine return NULL. // ** // ** If bFree is true, do not continue to use the pWith pointer after // ** calling this routine, Instead, use only the return value. // */ func _sqlite3WithPush(tls *libc.TLS, pParse uintptr, pWith uintptr, bFree Tu8) (r uintptr) { if pWith != 0 { if bFree != 0 { pWith = _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3WithDeleteGeneric), pWith) if pWith == uintptr(0) { return uintptr(0) } } if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 { _ = libc.Int32FromInt32(0) (*TWith)(unsafe.Pointer(pWith)).FpOuter = (*TParse)(unsafe.Pointer(pParse)).FpWith (*TParse)(unsafe.Pointer(pParse)).FpWith = pWith } } return pWith } // C documentation // // /* // ** This function checks if argument pFrom refers to a CTE declared by // ** a WITH clause on the stack currently maintained by the parser (on the // ** pParse->pWith linked list). And if currently processing a CTE // ** CTE expression, through routine checks to see if the reference is // ** a recursive reference to the CTE. // ** // ** If pFrom matches a CTE according to either of these two above, pFrom->pTab // ** and other fields are populated accordingly. // ** // ** Return 0 if no match is found. // ** Return 1 if a match is found. // ** Return 2 if an error condition is detected. // */ func _resolveFromTermToCte(tls *libc.TLS, pParse uintptr, pWalker uintptr, pFrom uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var bMayRecursive, i, iRecTab, rc, v4 int32 var db, pCte, pCteUse, pEList, pItem, pLeft, pRecTerm, pSavedWith, pSel, pSrc, pTab, v1, v2, v5 uintptr var _ /* pWith at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bMayRecursive, db, i, iRecTab, pCte, pCteUse, pEList, pItem, pLeft, pRecTerm, pSavedWith, pSel, pSrc, pTab, rc, v1, v2, v4, v5 /* The matching WITH */ _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FpWith == uintptr(0) { /* There are no WITH clauses in the stack. No match is possible */ return 0 } if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { /* Prior errors might have left pParse->pWith in a goofy state, so ** go no further. */ return 0 } if (*TSrcItem)(unsafe.Pointer(pFrom)).FzDatabase != uintptr(0) { /* The FROM term contains a schema qualifier (ex: main.t1) and so ** it cannot possibly be a CTE reference. */ return 0 } if int32(uint32(*(*uint16)(unsafe.Pointer(pFrom + 36 + 4))&0x200>>9)) != 0 { /* The FROM term is specifically excluded from matching a CTE. ** (1) It is part of a trigger that used to have zDatabase but had ** zDatabase removed by sqlite3FixTriggerStep(). ** (2) This is the first term in the FROM clause of an UPDATE. */ return 0 } pCte = _searchWith(tls, (*TParse)(unsafe.Pointer(pParse)).FpWith, pFrom, bp) if pCte != 0 { db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Initial value of pParse->pWith */ iRecTab = -int32(1) /* If pCte->zCteErr is non-NULL at this point, then this is an illegal ** recursive reference to CTE pCte. Leave an error in pParse and return ** early. If pCte->zCteErr is NULL, then this is not a recursive reference. ** In this case, proceed. */ if (*TCte)(unsafe.Pointer(pCte)).FzCteErr != 0 { _sqlite3ErrorMsg(tls, pParse, (*TCte)(unsafe.Pointer(pCte)).FzCteErr, libc.VaList(bp+16, (*TCte)(unsafe.Pointer(pCte)).FzName)) return int32(2) } if _cannotBeFunction(tls, pParse, pFrom) != 0 { return int32(2) } _ = libc.Int32FromInt32(0) pTab = _sqlite3DbMallocZero(tls, db, uint64(64)) if pTab == uintptr(0) { return int32(2) } pCteUse = (*TCte)(unsafe.Pointer(pCte)).FpUse if pCteUse == uintptr(0) { v1 = _sqlite3DbMallocZero(tls, db, uint64(20)) pCteUse = v1 (*TCte)(unsafe.Pointer(pCte)).FpUse = v1 if pCteUse == uintptr(0) || _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3DbFree), pCteUse) == uintptr(0) { _sqlite3DbFree(tls, db, pTab) return int32(2) } (*TCteUse)(unsafe.Pointer(pCteUse)).FeM10d = (*TCte)(unsafe.Pointer(pCte)).FeM10d } (*TSrcItem)(unsafe.Pointer(pFrom)).FpTab = pTab (*TTable)(unsafe.Pointer(pTab)).FnTabRef = uint32(1) (*TTable)(unsafe.Pointer(pTab)).FzName = _sqlite3DbStrDup(tls, db, (*TCte)(unsafe.Pointer(pCte)).FzName) (*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(-int32(1)) (*TTable)(unsafe.Pointer(pTab)).FnRowLogEst = int16(200) _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(pTab + 28)) |= uint32(libc.Int32FromInt32(TF_Ephemeral) | libc.Int32FromInt32(TF_NoVisibleRowid)) (*TSrcItem)(unsafe.Pointer(pFrom)).FpSelect = _sqlite3SelectDup(tls, db, (*TCte)(unsafe.Pointer(pCte)).FpSelect, 0) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return int32(2) } *(*Tu32)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pFrom)).FpSelect + 4)) |= uint32(SF_CopyCte) _ = libc.Int32FromInt32(0) if int32(uint32(*(*uint16)(unsafe.Pointer(pFrom + 36 + 4))&0x2>>1)) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19620, libc.VaList(bp+16, *(*uintptr)(unsafe.Pointer(pFrom + 64)))) return int32(2) } libc.SetBitFieldPtr16Uint32(pFrom+36+4, libc.Uint32FromInt32(1), 8, 0x100) *(*uintptr)(unsafe.Pointer(pFrom + 68)) = pCteUse (*TCteUse)(unsafe.Pointer(pCteUse)).FnUse++ /* Check if this is a recursive CTE. */ v2 = (*TSrcItem)(unsafe.Pointer(pFrom)).FpSelect pSel = v2 pRecTerm = v2 bMayRecursive = libc.BoolInt32(int32((*TSelect)(unsafe.Pointer(pSel)).Fop) == int32(TK_ALL) || int32((*TSelect)(unsafe.Pointer(pSel)).Fop) == int32(TK_UNION)) for bMayRecursive != 0 && int32((*TSelect)(unsafe.Pointer(pRecTerm)).Fop) == int32((*TSelect)(unsafe.Pointer(pSel)).Fop) { pSrc = (*TSelect)(unsafe.Pointer(pRecTerm)).FpSrc _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) { break } pItem = pSrc + 8 + uintptr(i)*72 if (*TSrcItem)(unsafe.Pointer(pItem)).FzDatabase == uintptr(0) && (*TSrcItem)(unsafe.Pointer(pItem)).FzName != uintptr(0) && 0 == _sqlite3StrICmp(tls, (*TSrcItem)(unsafe.Pointer(pItem)).FzName, (*TCte)(unsafe.Pointer(pCte)).FzName) { (*TSrcItem)(unsafe.Pointer(pItem)).FpTab = pTab (*TTable)(unsafe.Pointer(pTab)).FnTabRef++ libc.SetBitFieldPtr16Uint32(pItem+36+4, libc.Uint32FromInt32(1), 6, 0x40) if (*TSelect)(unsafe.Pointer(pRecTerm)).FselFlags&uint32(SF_Recursive) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19640, libc.VaList(bp+16, (*TCte)(unsafe.Pointer(pCte)).FzName)) return int32(2) } *(*Tu32)(unsafe.Pointer(pRecTerm + 4)) |= uint32(SF_Recursive) if iRecTab < 0 { v5 = pParse + 40 v4 = *(*int32)(unsafe.Pointer(v5)) *(*int32)(unsafe.Pointer(v5))++ iRecTab = v4 } (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor = iRecTab } goto _3 _3: ; i++ } if (*TSelect)(unsafe.Pointer(pRecTerm)).FselFlags&uint32(SF_Recursive) == uint32(0) { break } pRecTerm = (*TSelect)(unsafe.Pointer(pRecTerm)).FpPrior } (*TCte)(unsafe.Pointer(pCte)).FzCteErr = __ccgo_ts + 19683 pSavedWith = (*TParse)(unsafe.Pointer(pParse)).FpWith (*TParse)(unsafe.Pointer(pParse)).FpWith = *(*uintptr)(unsafe.Pointer(bp)) if (*TSelect)(unsafe.Pointer(pSel)).FselFlags&uint32(SF_Recursive) != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TSelect)(unsafe.Pointer(pRecTerm)).FpWith = (*TSelect)(unsafe.Pointer(pSel)).FpWith rc = _sqlite3WalkSelect(tls, pWalker, pRecTerm) (*TSelect)(unsafe.Pointer(pRecTerm)).FpWith = uintptr(0) if rc != 0 { (*TParse)(unsafe.Pointer(pParse)).FpWith = pSavedWith return int32(2) } } else { if _sqlite3WalkSelect(tls, pWalker, pSel) != 0 { (*TParse)(unsafe.Pointer(pParse)).FpWith = pSavedWith return int32(2) } } (*TParse)(unsafe.Pointer(pParse)).FpWith = *(*uintptr)(unsafe.Pointer(bp)) pLeft = pSel for { if !((*TSelect)(unsafe.Pointer(pLeft)).FpPrior != 0) { break } goto _6 _6: ; pLeft = (*TSelect)(unsafe.Pointer(pLeft)).FpPrior } pEList = (*TSelect)(unsafe.Pointer(pLeft)).FpEList if (*TCte)(unsafe.Pointer(pCte)).FpCols != 0 { if pEList != 0 && (*TExprList)(unsafe.Pointer(pEList)).FnExpr != (*TExprList)(unsafe.Pointer((*TCte)(unsafe.Pointer(pCte)).FpCols)).FnExpr { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19706, libc.VaList(bp+16, (*TCte)(unsafe.Pointer(pCte)).FzName, (*TExprList)(unsafe.Pointer(pEList)).FnExpr, (*TExprList)(unsafe.Pointer((*TCte)(unsafe.Pointer(pCte)).FpCols)).FnExpr)) (*TParse)(unsafe.Pointer(pParse)).FpWith = pSavedWith return int32(2) } pEList = (*TCte)(unsafe.Pointer(pCte)).FpCols } _sqlite3ColumnsFromExprList(tls, pParse, pEList, pTab+34, pTab+4) if bMayRecursive != 0 { if (*TSelect)(unsafe.Pointer(pSel)).FselFlags&uint32(SF_Recursive) != 0 { (*TCte)(unsafe.Pointer(pCte)).FzCteErr = __ccgo_ts + 19744 } else { (*TCte)(unsafe.Pointer(pCte)).FzCteErr = __ccgo_ts + 19778 } _sqlite3WalkSelect(tls, pWalker, pSel) } (*TCte)(unsafe.Pointer(pCte)).FzCteErr = uintptr(0) (*TParse)(unsafe.Pointer(pParse)).FpWith = pSavedWith return int32(1) /* Success */ } return 0 /* No match */ } // C documentation // // /* // ** If the SELECT passed as the second argument has an associated WITH // ** clause, pop it from the stack stored as part of the Parse object. // ** // ** This function is used as the xSelectCallback2() callback by // ** sqlite3SelectExpand() when walking a SELECT tree to resolve table // ** names and other FROM clause elements. // */ func _sqlite3SelectPopWith(tls *libc.TLS, pWalker uintptr, p uintptr) { var pParse, pWith uintptr _, _ = pParse, pWith pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse if (*TParse)(unsafe.Pointer(pParse)).FpWith != 0 && (*TSelect)(unsafe.Pointer(p)).FpPrior == uintptr(0) { pWith = (*TSelect)(unsafe.Pointer(_findRightmost(tls, p))).FpWith if pWith != uintptr(0) { _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).FpWith = (*TWith)(unsafe.Pointer(pWith)).FpOuter } } } // C documentation // // /* // ** The SrcItem structure passed as the second argument represents a // ** sub-query in the FROM clause of a SELECT statement. This function // ** allocates and populates the SrcItem.pTab object. If successful, // ** SQLITE_OK is returned. Otherwise, if an OOM error is encountered, // ** SQLITE_NOMEM. // */ func _sqlite3ExpandSubquery(tls *libc.TLS, pParse uintptr, pFrom uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pSel, pTab, v1 uintptr var v2 int32 _, _, _, _ = pSel, pTab, v1, v2 pSel = (*TSrcItem)(unsafe.Pointer(pFrom)).FpSelect _ = libc.Int32FromInt32(0) v1 = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(64)) pTab = v1 (*TSrcItem)(unsafe.Pointer(pFrom)).FpTab = v1 if pTab == uintptr(0) { return int32(SQLITE_NOMEM) } (*TTable)(unsafe.Pointer(pTab)).FnTabRef = uint32(1) if (*TSrcItem)(unsafe.Pointer(pFrom)).FzAlias != 0 { (*TTable)(unsafe.Pointer(pTab)).FzName = _sqlite3DbStrDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TSrcItem)(unsafe.Pointer(pFrom)).FzAlias) } else { (*TTable)(unsafe.Pointer(pTab)).FzName = _sqlite3MPrintf(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, __ccgo_ts+19816, libc.VaList(bp+8, pFrom)) } for (*TSelect)(unsafe.Pointer(pSel)).FpPrior != 0 { pSel = (*TSelect)(unsafe.Pointer(pSel)).FpPrior } _sqlite3ColumnsFromExprList(tls, pParse, (*TSelect)(unsafe.Pointer(pSel)).FpEList, pTab+34, pTab+4) (*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(-int32(1)) (*TTable)(unsafe.Pointer(pTab)).FeTabType = uint8(TABTYP_VIEW) (*TTable)(unsafe.Pointer(pTab)).FnRowLogEst = int16(200) _ = libc.Int32FromInt32(0) /* The usual case - do not allow ROWID on a subquery */ *(*Tu32)(unsafe.Pointer(pTab + 28)) |= uint32(libc.Int32FromInt32(TF_Ephemeral) | libc.Int32FromInt32(TF_NoVisibleRowid)) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { v2 = int32(SQLITE_ERROR) } else { v2 = SQLITE_OK } return v2 } // C documentation // // /* // ** Check the N SrcItem objects to the right of pBase. (N might be zero!) // ** If any of those SrcItem objects have a USING clause containing zName // ** then return true. // ** // ** If N is zero, or none of the N SrcItem objects to the right of pBase // ** contains a USING clause, or if none of the USING clauses contain zName, // ** then return false. // */ func _inAnyUsingClause(tls *libc.TLS, zName uintptr, pBase uintptr, N int32) (r int32) { for N > 0 { N-- pBase += 72 if int32(uint32(*(*uint16)(unsafe.Pointer(pBase + 36 + 4))&0x400>>10)) == 0 { continue } if *(*uintptr)(unsafe.Pointer(pBase + 48)) == uintptr(0) { continue } if _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pBase + 48)), zName) >= 0 { return int32(1) } } return 0 } // C documentation // // /* // ** This routine is a Walker callback for "expanding" a SELECT statement. // ** "Expanding" means to do the following: // ** // ** (1) Make sure VDBE cursor numbers have been assigned to every // ** element of the FROM clause. // ** // ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that // ** defines FROM clause. When views appear in the FROM clause, // ** fill pTabList->a[].pSelect with a copy of the SELECT statement // ** that implements the view. A copy is made of the view's SELECT // ** statement so that we can freely modify or delete that statement // ** without worrying about messing up the persistent representation // ** of the view. // ** // ** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword // ** on joins and the ON and USING clause of joins. // ** // ** (4) Scan the list of columns in the result set (pEList) looking // ** for instances of the "*" operator or the TABLE.* operator. // ** If found, expand each "*" to be every column in every table // ** and TABLE.* to be every column in TABLE. // ** // */ func _selectExpander(tls *libc.TLS, pWalker uintptr, p uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var a, db, pE, pEList, pExpr, pFrom, pLeft, pNestedFrom, pNew, pParse, pRight, pSel, pTab, pTab1, pTabList, pUsing, pX, pX1, zName, zSchemaName, zTName, zTabName, zUName, v10, v2, v5, v9 uintptr var eCodeOrig Tu8 var elistFlags Tu32 var flags, i, iDb, iErrOfst, ii, j, k, longNames, nAdd, rc, tableSeen, v1, v13, v4 int32 var nCol Ti16 var selFlags Tu16 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = a, db, eCodeOrig, elistFlags, flags, i, iDb, iErrOfst, ii, j, k, longNames, nAdd, nCol, pE, pEList, pExpr, pFrom, pLeft, pNestedFrom, pNew, pParse, pRight, pSel, pTab, pTab1, pTabList, pUsing, pX, pX1, rc, selFlags, tableSeen, zName, zSchemaName, zTName, zTabName, zUName, v1, v10, v13, v2, v4, v5, v9 pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse db = (*TParse)(unsafe.Pointer(pParse)).Fdb selFlags = uint16((*TSelect)(unsafe.Pointer(p)).FselFlags) elistFlags = uint32(0) *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(SF_Expanded) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return int32(WRC_Abort) } _ = libc.Int32FromInt32(0) if int32(selFlags)&int32(SF_Expanded) != 0 { return int32(WRC_Prune) } if (*TWalker)(unsafe.Pointer(pWalker)).FeCode != 0 { /* Renumber selId because it has been copied from a view */ v2 = pParse + 108 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) (*TSelect)(unsafe.Pointer(p)).FselId = uint32(v1) } pTabList = (*TSelect)(unsafe.Pointer(p)).FpSrc pEList = (*TSelect)(unsafe.Pointer(p)).FpEList if (*TParse)(unsafe.Pointer(pParse)).FpWith != 0 && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_View) != 0 { if (*TSelect)(unsafe.Pointer(p)).FpWith == uintptr(0) { (*TSelect)(unsafe.Pointer(p)).FpWith = _sqlite3DbMallocZero(tls, db, uint64(36)) if (*TSelect)(unsafe.Pointer(p)).FpWith == uintptr(0) { return int32(WRC_Abort) } } (*TWith)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpWith)).FbView = int32(1) } _sqlite3WithPush(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpWith, uint8(0)) /* Make sure cursor numbers have been assigned to all entries in ** the FROM clause of the SELECT statement. */ _sqlite3SrcListAssignCursors(tls, pParse, pTabList) /* Look up every table named in the FROM clause of the select. If ** an entry of the FROM clause is a subquery instead of a table or view, ** then create a transient table structure to describe the subquery. */ i = 0 pFrom = pTabList + 8 for { if !(i < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) { break } _ = libc.Int32FromInt32(0) if (*TSrcItem)(unsafe.Pointer(pFrom)).FpTab != 0 { goto _3 } _ = libc.Int32FromInt32(0) if (*TSrcItem)(unsafe.Pointer(pFrom)).FzName == uintptr(0) { pSel = (*TSrcItem)(unsafe.Pointer(pFrom)).FpSelect /* A sub-query in the FROM clause of a SELECT */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if _sqlite3WalkSelect(tls, pWalker, pSel) != 0 { return int32(WRC_Abort) } if _sqlite3ExpandSubquery(tls, pParse, pFrom) != 0 { return int32(WRC_Abort) } } else { v4 = _resolveFromTermToCte(tls, pParse, pWalker, pFrom) rc = v4 if v4 != 0 { if rc > int32(1) { return int32(WRC_Abort) } pTab = (*TSrcItem)(unsafe.Pointer(pFrom)).FpTab _ = libc.Int32FromInt32(0) } else { /* An ordinary table or view name in the FROM clause */ _ = libc.Int32FromInt32(0) v5 = _sqlite3LocateTableItem(tls, pParse, uint32(0), pFrom) pTab = v5 (*TSrcItem)(unsafe.Pointer(pFrom)).FpTab = v5 if pTab == uintptr(0) { return int32(WRC_Abort) } if (*TTable)(unsafe.Pointer(pTab)).FnTabRef >= uint32(0xffff) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19820, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) (*TSrcItem)(unsafe.Pointer(pFrom)).FpTab = uintptr(0) return int32(WRC_Abort) } (*TTable)(unsafe.Pointer(pTab)).FnTabRef++ if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) && _cannotBeFunction(tls, pParse, pFrom) != 0 { return int32(WRC_Abort) } if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) { eCodeOrig = uint8((*TWalker)(unsafe.Pointer(pWalker)).FeCode) if _sqlite3ViewGetColumnNames(tls, pParse, pTab) != 0 { return int32(WRC_Abort) } _ = libc.Int32FromInt32(0) if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_EnableView) == uint64(0) && (*TTable)(unsafe.Pointer(pTab)).FpSchema != (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19859, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) } (*TSrcItem)(unsafe.Pointer(pFrom)).FpSelect = _sqlite3SelectDup(tls, db, (*(*struct { FpSelect uintptr })(unsafe.Pointer(&(*TTable)(unsafe.Pointer(pTab)).Fu))).FpSelect, 0) } else { if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) && int32(uint32(*(*uint16)(unsafe.Pointer(pFrom + 36 + 4))&0x80>>7)) != 0 && (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).Fp != uintptr(0) && int32((*TVTable)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).Fp)).FeVtabRisk) > libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_TrustedSchema) != uint64(0)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15109, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) } } _ = libc.Int32FromInt32(0) nCol = (*TTable)(unsafe.Pointer(pTab)).FnCol (*TTable)(unsafe.Pointer(pTab)).FnCol = int16(-int32(1)) (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(1) /* Turn on Select.selId renumbering */ _sqlite3WalkSelect(tls, pWalker, (*TSrcItem)(unsafe.Pointer(pFrom)).FpSelect) (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(eCodeOrig) (*TTable)(unsafe.Pointer(pTab)).FnCol = nCol } } } /* Locate the index named by the INDEXED BY clause, if any. */ if int32(uint32(*(*uint16)(unsafe.Pointer(pFrom + 36 + 4))&0x2>>1)) != 0 && _sqlite3IndexedByLookup(tls, pParse, pFrom) != 0 { return int32(WRC_Abort) } goto _3 _3: ; i++ pFrom += 72 } /* Process NATURAL keywords, and ON and USING clauses of joins. */ _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 || _sqlite3ProcessJoin(tls, pParse, p) != 0 { return int32(WRC_Abort) } /* For every "*" that occurs in the column list, insert the names of ** all columns in all tables. And for every TABLE.* insert the names ** of all columns in TABLE. The parser inserted a special expression ** with the TK_ASTERISK operator for each "*" that it found in the column ** list. The following code just has to locate the TK_ASTERISK ** expressions and expand each one to the list of all columns in ** all tables. ** ** The first loop just checks to see if there are any "*" operators ** that need expanding. */ k = 0 for { if !(k < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } pE = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(k)*20))).FpExpr if int32((*TExpr)(unsafe.Pointer(pE)).Fop) == int32(TK_ASTERISK) { break } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pE)).Fop) == int32(TK_DOT) && int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pE)).FpRight)).Fop) == int32(TK_ASTERISK) { break } elistFlags |= (*TExpr)(unsafe.Pointer(pE)).Fflags goto _6 _6: ; k++ } if k < (*TExprList)(unsafe.Pointer(pEList)).FnExpr { /* ** If we get here it means the result set contains one or more "*" ** operators that need to be expanded. Loop through each expression ** in the result set and expand them one by one. */ a = pEList + 8 pNew = uintptr(0) flags = int32((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags) longNames = libc.BoolInt32(flags&int32(SQLITE_FullColNames) != 0 && flags&int32(SQLITE_ShortColNames) == 0) k = 0 for { if !(k < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } pE = (*(*TExprList_item)(unsafe.Pointer(a + uintptr(k)*20))).FpExpr elistFlags |= (*TExpr)(unsafe.Pointer(pE)).Fflags pRight = (*TExpr)(unsafe.Pointer(pE)).FpRight _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pE)).Fop) != int32(TK_ASTERISK) && (int32((*TExpr)(unsafe.Pointer(pE)).Fop) != int32(TK_DOT) || int32((*TExpr)(unsafe.Pointer(pRight)).Fop) != int32(TK_ASTERISK)) { /* This particular expression does not need to be expanded. */ pNew = _sqlite3ExprListAppend(tls, pParse, pNew, (*(*TExprList_item)(unsafe.Pointer(a + uintptr(k)*20))).FpExpr) if pNew != 0 { (*(*TExprList_item)(unsafe.Pointer(pNew + 8 + uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*20))).FzEName = (*(*TExprList_item)(unsafe.Pointer(a + uintptr(k)*20))).FzEName libc.SetBitFieldPtr16Uint32(pNew+8+uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*20+8+4, uint32(int32(uint32(*(*uint16)(unsafe.Pointer(a + uintptr(k)*20 + 8 + 4))&0x3>>0))), 0, 0x3) (*(*TExprList_item)(unsafe.Pointer(a + uintptr(k)*20))).FzEName = uintptr(0) } (*(*TExprList_item)(unsafe.Pointer(a + uintptr(k)*20))).FpExpr = uintptr(0) } else { /* This expression is a "*" or a "TABLE.*" and needs to be ** expanded. */ tableSeen = 0 /* Set to 1 when TABLE matches */ zTName = uintptr(0) if int32((*TExpr)(unsafe.Pointer(pE)).Fop) == int32(TK_DOT) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) zTName = *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pE)).FpLeft + 8)) _ = libc.Int32FromInt32(0) iErrOfst = *(*int32)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pE)).FpRight + 36)) } else { _ = libc.Int32FromInt32(0) iErrOfst = *(*int32)(unsafe.Pointer(pE + 36)) } i = 0 pFrom = pTabList + 8 for { if !(i < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) { break } /* Number of cols including rowid */ pTab1 = (*TSrcItem)(unsafe.Pointer(pFrom)).FpTab /* AS name for this data source */ zSchemaName = uintptr(0) /* USING clause for pFrom[1] */ v9 = (*TSrcItem)(unsafe.Pointer(pFrom)).FzAlias zTabName = v9 if v9 == uintptr(0) { zTabName = (*TTable)(unsafe.Pointer(pTab1)).FzName } if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { break } _ = libc.Int32FromInt32(0) if int32(uint32(*(*uint16)(unsafe.Pointer(pFrom + 36 + 4))&0x2000>>13)) != 0 { _ = libc.Int32FromInt32(0) pNestedFrom = (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pFrom)).FpSelect)).FpEList _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } else { if zTName != 0 && _sqlite3StrICmp(tls, zTName, zTabName) != 0 { goto _8 } pNestedFrom = uintptr(0) iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab1)).FpSchema) if iDb >= 0 { v10 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName } else { v10 = __ccgo_ts + 6590 } zSchemaName = v10 } if i+int32(1) < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc && int32(uint32(*(*uint16)(unsafe.Pointer(pFrom + 1*72 + 36 + 4))&0x400>>10)) != 0 && int32(selFlags)&int32(SF_NestedFrom) != 0 { pUsing = *(*uintptr)(unsafe.Pointer(pFrom + 1*72 + 48)) ii = 0 for { if !(ii < (*TIdList)(unsafe.Pointer(pUsing)).FnId) { break } zUName = (*(*TIdList_item)(unsafe.Pointer(pUsing + 8 + uintptr(ii)*8))).FzName pRight = _sqlite3Expr(tls, db, int32(TK_ID), zUName) _sqlite3ExprSetErrorOffset(tls, pRight, iErrOfst) pNew = _sqlite3ExprListAppend(tls, pParse, pNew, pRight) if pNew != 0 { pX = pNew + 8 + uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*20 _ = libc.Int32FromInt32(0) (*TExprList_item)(unsafe.Pointer(pX)).FzEName = _sqlite3MPrintf(tls, db, __ccgo_ts+19890, libc.VaList(bp+8, zUName)) libc.SetBitFieldPtr16Uint32(pX+8+4, libc.Uint32FromInt32(ENAME_TAB), 0, 0x3) libc.SetBitFieldPtr16Uint32(pX+8+4, libc.Uint32FromInt32(1), 7, 0x80) } goto _11 _11: ; ii++ } } else { pUsing = uintptr(0) } nAdd = int32((*TTable)(unsafe.Pointer(pTab1)).FnCol) if (*TTable)(unsafe.Pointer(pTab1)).FtabFlags&uint32(TF_NoVisibleRowid) == uint32(0) && int32(selFlags)&int32(SF_NestedFrom) != 0 { nAdd++ } j = 0 for { if !(j < nAdd) { break } /* Newly added ExprList term */ if j == int32((*TTable)(unsafe.Pointer(pTab1)).FnCol) { zName = _sqlite3RowidAlias(tls, pTab1) if zName == uintptr(0) { goto _12 } } else { zName = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab1)).FaCol + uintptr(j)*12))).FzCnName /* If pTab is actually an SF_NestedFrom sub-select, do not ** expand any ENAME_ROWID columns. */ if pNestedFrom != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pNestedFrom + 8 + uintptr(j)*20 + 8 + 4))&0x3>>0)) == int32(ENAME_ROWID) { goto _12 } if zTName != 0 && pNestedFrom != 0 && _sqlite3MatchEName(tls, pNestedFrom+8+uintptr(j)*20, uintptr(0), zTName, uintptr(0), uintptr(0)) == 0 { goto _12 } /* If a column is marked as 'hidden', omit it from the expanded ** result-set list unless the SELECT has the SF_IncludeHidden ** bit set. */ if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_IncludeHidden) == uint32(0) && int32((*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab1)).FaCol+uintptr(j)*12)).FcolFlags)&int32(COLFLAG_HIDDEN) != 0 { goto _12 } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab1)).FaCol + uintptr(j)*12))).FcolFlags)&int32(COLFLAG_NOEXPAND) != 0 && zTName == uintptr(0) && int32(selFlags)&int32(SF_NestedFrom) == 0 { goto _12 } } _ = libc.Int32FromInt32(0) tableSeen = int32(1) if i > 0 && zTName == uintptr(0) && int32(selFlags)&int32(SF_NestedFrom) == 0 { if int32(uint32(*(*uint16)(unsafe.Pointer(pFrom + 36 + 4))&0x400>>10)) != 0 && _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pFrom + 48)), zName) >= 0 { /* In a join with a USING clause, omit columns in the ** using clause from the table on the right. */ goto _12 } } pRight = _sqlite3Expr(tls, db, int32(TK_ID), zName) if (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc > int32(1) && (int32((*TSrcItem)(unsafe.Pointer(pFrom)).Ffg.Fjointype)&int32(JT_LTORJ) == 0 || int32(selFlags)&int32(SF_NestedFrom) != 0 || !(_inAnyUsingClause(tls, zName, pFrom, (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc-i-int32(1)) != 0)) || int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { pLeft = _sqlite3Expr(tls, db, int32(TK_ID), zTabName) pExpr = _sqlite3PExpr(tls, pParse, int32(TK_DOT), pLeft, pRight) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && (*TExpr)(unsafe.Pointer(pE)).FpLeft != 0 { _sqlite3RenameTokenRemap(tls, pParse, pLeft, (*TExpr)(unsafe.Pointer(pE)).FpLeft) } if zSchemaName != 0 { pLeft = _sqlite3Expr(tls, db, int32(TK_ID), zSchemaName) pExpr = _sqlite3PExpr(tls, pParse, int32(TK_DOT), pLeft, pExpr) } } else { pExpr = pRight } _sqlite3ExprSetErrorOffset(tls, pExpr, iErrOfst) pNew = _sqlite3ExprListAppend(tls, pParse, pNew, pExpr) if pNew == uintptr(0) { break /* OOM */ } pX1 = pNew + 8 + uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*20 _ = libc.Int32FromInt32(0) if int32(selFlags)&int32(SF_NestedFrom) != 0 && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) { if pNestedFrom != 0 && libc.Bool(libc.Bool(!(libc.Int32FromInt32(ViewCanHaveRowid) != 0)) || j < (*TExprList)(unsafe.Pointer(pNestedFrom)).FnExpr) { _ = libc.Int32FromInt32(0) (*TExprList_item)(unsafe.Pointer(pX1)).FzEName = _sqlite3DbStrDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pNestedFrom + 8 + uintptr(j)*20))).FzEName) } else { (*TExprList_item)(unsafe.Pointer(pX1)).FzEName = _sqlite3MPrintf(tls, db, __ccgo_ts+19895, libc.VaList(bp+8, zSchemaName, zTabName, zName)) } if j == int32((*TTable)(unsafe.Pointer(pTab1)).FnCol) { v13 = int32(ENAME_ROWID) } else { v13 = int32(ENAME_TAB) } libc.SetBitFieldPtr16Uint32(pX1+8+4, uint32(v13), 0, 0x3) if int32(uint32(*(*uint16)(unsafe.Pointer(pFrom + 36 + 4))&0x400>>10)) != 0 && _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pFrom + 48)), zName) >= 0 || pUsing != 0 && _sqlite3IdListIndex(tls, pUsing, zName) >= 0 || j < int32((*TTable)(unsafe.Pointer(pTab1)).FnCol) && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab1)).FaCol + uintptr(j)*12))).FcolFlags)&int32(COLFLAG_NOEXPAND) != 0 { libc.SetBitFieldPtr16Uint32(pX1+8+4, libc.Uint32FromInt32(1), 8, 0x100) } } else { if longNames != 0 { (*TExprList_item)(unsafe.Pointer(pX1)).FzEName = _sqlite3MPrintf(tls, db, __ccgo_ts+12381, libc.VaList(bp+8, zTabName, zName)) libc.SetBitFieldPtr16Uint32(pX1+8+4, libc.Uint32FromInt32(ENAME_NAME), 0, 0x3) } else { (*TExprList_item)(unsafe.Pointer(pX1)).FzEName = _sqlite3DbStrDup(tls, db, zName) libc.SetBitFieldPtr16Uint32(pX1+8+4, libc.Uint32FromInt32(ENAME_NAME), 0, 0x3) } } goto _12 _12: ; j++ } goto _8 _8: ; i++ pFrom += 72 } if !(tableSeen != 0) { if zTName != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19904, libc.VaList(bp+8, zTName)) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19922, 0) } } } goto _7 _7: ; k++ } _sqlite3ExprListDelete(tls, db, pEList) (*TSelect)(unsafe.Pointer(p)).FpEList = pNew } if (*TSelect)(unsafe.Pointer(p)).FpEList != 0 { if (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr > *(*int32)(unsafe.Pointer(db + 120 + 2*4)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19942, 0) return int32(WRC_Abort) } if elistFlags&uint32(libc.Int32FromInt32(EP_HasFunc)|libc.Int32FromInt32(EP_Subquery)) != uint32(0) { *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(SF_ComplexResult) } } return WRC_Continue } // C documentation // // /* // ** This routine "expands" a SELECT statement and all of its subqueries. // ** For additional information on what it means to "expand" a SELECT // ** statement, see the comment on the selectExpand worker callback above. // ** // ** Expanding a SELECT statement is the first step in processing a // ** SELECT statement. The SELECT statement must be expanded before // ** name resolution is performed. // ** // ** If anything goes wrong, an error message is written into pParse. // ** The calling function can detect the problem by looking at pParse->nErr // ** and/or pParse->db->mallocFailed. // */ func _sqlite3SelectExpand(tls *libc.TLS, pParse uintptr, pSelect uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* w at bp+0 */ TWalker (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_sqlite3ExprWalkNoop) (*(*TWalker)(unsafe.Pointer(bp))).FpParse = pParse if (*TParse)(unsafe.Pointer(pParse)).FhasCompound != 0 { (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_convertCompoundSelectToSubquery) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback2 = uintptr(0) _sqlite3WalkSelect(tls, bp, pSelect) } (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_selectExpander) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback2 = __ccgo_fp(_sqlite3SelectPopWith) (*(*TWalker)(unsafe.Pointer(bp))).FeCode = uint16(0) _sqlite3WalkSelect(tls, bp, pSelect) } // C documentation // // /* // ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo() // ** interface. // ** // ** For each FROM-clause subquery, add Column.zType, Column.zColl, and // ** Column.affinity information to the Table structure that represents // ** the result set of that subquery. // ** // ** The Table structure that represents the result set was constructed // ** by selectExpander() but the type and collation and affinity information // ** was omitted at that point because identifiers had not yet been resolved. // ** This routine is called after identifier resolution. // */ func _selectAddSubqueryTypeInfo(tls *libc.TLS, pWalker uintptr, p uintptr) { var i int32 var pFrom, pParse, pSel, pTab, pTabList uintptr _, _, _, _, _, _ = i, pFrom, pParse, pSel, pTab, pTabList if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_HasTypeInfo) != 0 { return } *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(SF_HasTypeInfo) pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse _ = libc.Int32FromInt32(0) pTabList = (*TSelect)(unsafe.Pointer(p)).FpSrc i = 0 pFrom = pTabList + 8 for { if !(i < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) { break } pTab = (*TSrcItem)(unsafe.Pointer(pFrom)).FpTab _ = libc.Int32FromInt32(0) if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Ephemeral) != uint32(0) { /* A sub-query in the FROM clause of a SELECT */ pSel = (*TSrcItem)(unsafe.Pointer(pFrom)).FpSelect if pSel != 0 { _sqlite3SubqueryColumnTypes(tls, pParse, pTab, pSel, uint8(SQLITE_AFF_NONE)) } } goto _1 _1: ; i++ pFrom += 72 } } // C documentation // // /* // ** This routine adds datatype and collating sequence information to // ** the Table structures of all FROM-clause subqueries in a // ** SELECT statement. // ** // ** Use this routine after name resolution. // */ func _sqlite3SelectAddTypeInfo(tls *libc.TLS, pParse uintptr, pSelect uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* w at bp+0 */ TWalker (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback2 = __ccgo_fp(_selectAddSubqueryTypeInfo) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_sqlite3ExprWalkNoop) (*(*TWalker)(unsafe.Pointer(bp))).FpParse = pParse _sqlite3WalkSelect(tls, bp, pSelect) } // C documentation // // /* // ** This routine sets up a SELECT statement for processing. The // ** following is accomplished: // ** // ** * VDBE Cursor numbers are assigned to all FROM-clause terms. // ** * Ephemeral Table objects are created for all FROM-clause subqueries. // ** * ON and USING clauses are shifted into WHERE statements // ** * Wildcards "*" and "TABLE.*" in result sets are expanded. // ** * Identifiers in expression are matched to tables. // ** // ** This routine acts recursively on all subqueries within the SELECT. // */ func _sqlite3SelectPrep(tls *libc.TLS, pParse uintptr, p uintptr, pOuterNC uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 { return } if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_HasTypeInfo) != 0 { return } _sqlite3SelectExpand(tls, pParse, p) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return } _sqlite3ResolveSelectNames(tls, pParse, p, pOuterNC) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return } _sqlite3SelectAddTypeInfo(tls, pParse, p) } // C documentation // // /* // ** Analyze the arguments to aggregate functions. Create new pAggInfo->aCol[] // ** entries for columns that are arguments to aggregate functions but which // ** are not otherwise used. // ** // ** The aCol[] entries in AggInfo prior to nAccumulator are columns that // ** are referenced outside of aggregate functions. These might be columns // ** that are part of the GROUP by clause, for example. Other database engines // ** would throw an error if there is a column reference that is not in the // ** GROUP BY clause and that is not part of an aggregate function argument. // ** But SQLite allows this. // ** // ** The aCol[] entries beginning with the aCol[nAccumulator] and following // ** are column references that are used exclusively as arguments to // ** aggregate functions. This routine is responsible for computing // ** (or recomputing) those aCol[] entries. // */ func _analyzeAggFuncArgs(tls *libc.TLS, pAggInfo uintptr, pNC uintptr) { var i int32 var pExpr uintptr _, _ = i, pExpr _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pNC + 24)) |= int32(NC_InAggFunc) i = 0 for { if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc) { break } pExpr = (*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(i)*24))).FpFExpr _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3ExprAnalyzeAggList(tls, pNC, *(*uintptr)(unsafe.Pointer(pExpr + 20))) if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3ExprAnalyzeAggList(tls, pNC, *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft + 20))) } _ = libc.Int32FromInt32(0) if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { _sqlite3ExprAnalyzeAggregates(tls, pNC, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)))).FpFilter) } goto _1 _1: ; i++ } *(*int32)(unsafe.Pointer(pNC + 24)) &= ^libc.Int32FromInt32(NC_InAggFunc) } // C documentation // // /* // ** An index on expressions is being used in the inner loop of an // ** aggregate query with a GROUP BY clause. This routine attempts // ** to adjust the AggInfo object to take advantage of index and to // ** perhaps use the index as a covering index. // ** // */ func _optimizeAggregateUseOfIndexedExpr(tls *libc.TLS, pParse uintptr, pSelect uintptr, pAggInfo uintptr, pNC uintptr) { var j, k, mx int32 _, _, _ = j, k, mx _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator if int32((*TAggInfo)(unsafe.Pointer(pAggInfo)).FnSortingColumn) > 0 { mx = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpGroupBy)).FnExpr - int32(1) j = 0 for { if !(j < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn) { break } k = int32((*(*TAggInfo_col)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr(j)*16))).FiSorterColumn) if k > mx { mx = k } goto _1 _1: ; j++ } (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnSortingColumn = uint16(mx + int32(1)) } _analyzeAggFuncArgs(tls, pAggInfo, pNC) _ = pSelect _ = pParse } // C documentation // // /* // ** Walker callback for aggregateConvertIndexedExprRefToColumn(). // */ func _aggregateIdxEprRefToColCallback(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var pAggInfo, pCol uintptr _, _ = pAggInfo, pCol _ = pWalker if (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo == uintptr(0) { return WRC_Continue } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_COLUMN) { return WRC_Continue } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_FUNCTION) { return WRC_Continue } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_IF_NULL_ROW) { return WRC_Continue } pAggInfo = (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo if int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg) >= (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn { return WRC_Continue } _ = libc.Int32FromInt32(0) pCol = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiAgg)*16 (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_AGG_COLUMN) (*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TAggInfo_col)(unsafe.Pointer(pCol)).FiTable (*TExpr)(unsafe.Pointer(pExpr)).FiColumn = (*TAggInfo_col)(unsafe.Pointer(pCol)).FiColumn *(*Tu32)(unsafe.Pointer(pExpr + 4)) &= uint32(^(libc.Int32FromInt32(EP_Skip) | libc.Int32FromInt32(EP_Collate) | libc.Int32FromInt32(EP_Unlikely))) return int32(WRC_Prune) } // C documentation // // /* // ** Convert every pAggInfo->aFunc[].pExpr such that any node within // ** those expressions that has pAppInfo set is changed into a TK_AGG_COLUMN // ** opcode. // */ func _aggregateConvertIndexedExprRefToColumn(tls *libc.TLS, pAggInfo uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var i int32 var _ /* w at bp+0 */ TWalker _ = i libc.Xmemset(tls, bp, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_aggregateIdxEprRefToColCallback) i = 0 for { if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc) { break } _sqlite3WalkExpr(tls, bp, (*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(i)*24))).FpFExpr) goto _1 _1: ; i++ } } // C documentation // // /* // ** Allocate a block of registers so that there is one register for each // ** pAggInfo->aCol[] and pAggInfo->aFunc[] entry in pAggInfo. The first // ** register in this block is stored in pAggInfo->iFirstReg. // ** // ** This routine may only be called once for each AggInfo object. Prior // ** to calling this routine: // ** // ** * The aCol[] and aFunc[] arrays may be modified // ** * The AggInfoColumnReg() and AggInfoFuncReg() macros may not be used // ** // ** After calling this routine: // ** // ** * The aCol[] and aFunc[] arrays are fixed // ** * The AggInfoColumnReg() and AggInfoFuncReg() macros may be used // ** // */ func _assignAggregateRegisters(tls *libc.TLS, pParse uintptr, pAggInfo uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn + (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc } // C documentation // // /* // ** Reset the aggregate accumulator. // ** // ** The aggregate accumulator is a set of memory cells that hold // ** intermediate results while calculating an aggregate. This // ** routine generates code that stores NULLs in all of those memory // ** cells. // */ func _resetAccumulator(tls *libc.TLS, pParse uintptr, pAggInfo uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var i, nExtra, nReg int32 var pE, pFunc, pKeyInfo, pKeyInfo1, pOBList, v uintptr _, _, _, _, _, _, _, _, _ = i, nExtra, nReg, pE, pFunc, pKeyInfo, pKeyInfo1, pOBList, v v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe nReg = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc + (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if nReg == 0 { return } if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return } _sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg+nReg-int32(1)) pFunc = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc i = libc.Int32FromInt32(0) for { if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc) { break } if (*TAggInfo_func)(unsafe.Pointer(pFunc)).FiDistinct >= 0 { pE = (*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFExpr _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(pE + 20)) == uintptr(0) || (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pE + 20)))).FnExpr != int32(1) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19973, 0) (*TAggInfo_func)(unsafe.Pointer(pFunc)).FiDistinct = -int32(1) } else { pKeyInfo = _sqlite3KeyInfoFromExprList(tls, pParse, *(*uintptr)(unsafe.Pointer(pE + 20)), 0, 0) (*TAggInfo_func)(unsafe.Pointer(pFunc)).FiDistAddr = _sqlite3VdbeAddOp4(tls, v, int32(OP_OpenEphemeral), (*TAggInfo_func)(unsafe.Pointer(pFunc)).FiDistinct, 0, 0, pKeyInfo, -int32(8)) _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+20024, libc.VaList(bp+8, (*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFunc)).FzName)) } } if (*TAggInfo_func)(unsafe.Pointer(pFunc)).FiOBTab >= 0 { nExtra = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pOBList = *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFExpr)).FpLeft + 20)) if !((*TAggInfo_func)(unsafe.Pointer(pFunc)).FbOBUnique != 0) { nExtra++ /* One extra column for the OP_Sequence */ } if (*TAggInfo_func)(unsafe.Pointer(pFunc)).FbOBPayload != 0 { /* extra columns for the function arguments */ _ = libc.Int32FromInt32(0) nExtra += (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFExpr + 20)))).FnExpr } if (*TAggInfo_func)(unsafe.Pointer(pFunc)).FbUseSubtype != 0 { nExtra += (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFExpr + 20)))).FnExpr } pKeyInfo1 = _sqlite3KeyInfoFromExprList(tls, pParse, pOBList, 0, nExtra) if !((*TAggInfo_func)(unsafe.Pointer(pFunc)).FbOBUnique != 0) && (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 { (*TKeyInfo)(unsafe.Pointer(pKeyInfo1)).FnKeyField++ } _sqlite3VdbeAddOp4(tls, v, int32(OP_OpenEphemeral), (*TAggInfo_func)(unsafe.Pointer(pFunc)).FiOBTab, (*TExprList)(unsafe.Pointer(pOBList)).FnExpr+nExtra, 0, pKeyInfo1, -int32(8)) _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+20057, libc.VaList(bp+8, (*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFunc)).FzName)) } goto _1 _1: ; i++ pFunc += 24 } } // C documentation // // /* // ** Invoke the OP_AggFinalize opcode for every aggregate function // ** in the AggInfo structure. // */ func _finalizeAggFunctions(tls *libc.TLS, pParse uintptr, pAggInfo uintptr) { var i, iBaseCol, iTop, j, nArg, nKey, regAgg, regSubtype, v4 int32 var pF, pList, v uintptr _, _, _, _, _, _, _, _, _, _, _, _ = i, iBaseCol, iTop, j, nArg, nKey, pF, pList, regAgg, regSubtype, v, v4 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe i = 0 pF = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc for { if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc) { break } _ = libc.Int32FromInt32(0) pList = *(*uintptr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr + 20)) if (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab >= 0 { /* Loop counter */ _ = libc.Int32FromInt32(0) nArg = (*TExprList)(unsafe.Pointer(pList)).FnExpr regAgg = _sqlite3GetTempRange(tls, pParse, nArg) if int32((*TAggInfo_func)(unsafe.Pointer(pF)).FbOBPayload) == 0 { nKey = 0 } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) nKey = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr)).FpLeft + 20)))).FnExpr if !((*TAggInfo_func)(unsafe.Pointer(pF)).FbOBUnique != 0) { nKey++ } } iTop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab) j = nArg - int32(1) for { if !(j >= 0) { break } _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab, nKey+j, regAgg+j) goto _2 _2: ; j-- } if (*TAggInfo_func)(unsafe.Pointer(pF)).FbUseSubtype != 0 { regSubtype = _sqlite3GetTempReg(tls, pParse) iBaseCol = nKey + nArg + libc.BoolInt32(int32((*TAggInfo_func)(unsafe.Pointer(pF)).FbOBPayload) == 0 && int32((*TAggInfo_func)(unsafe.Pointer(pF)).FbOBUnique) == 0) j = nArg - int32(1) for { if !(j >= 0) { break } _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab, iBaseCol+j, regSubtype) _sqlite3VdbeAddOp2(tls, v, int32(OP_SetSubtype), regSubtype, regAgg+j) goto _3 _3: ; j-- } _sqlite3ReleaseTempReg(tls, pParse, regSubtype) } _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, int32(OP_AggStep), 0, regAgg, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg+(*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn+i) _sqlite3VdbeAppendP4(tls, v, (*TAggInfo_func)(unsafe.Pointer(pF)).FpFunc, -int32(7)) _sqlite3VdbeChangeP5(tls, v, uint16(uint8(nArg))) _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab, iTop+int32(1)) _sqlite3VdbeJumpHere(tls, v, iTop) _sqlite3ReleaseTempRange(tls, pParse, regAgg, nArg) } _ = libc.Int32FromInt32(0) if pList != 0 { v4 = (*TExprList)(unsafe.Pointer(pList)).FnExpr } else { v4 = 0 } _sqlite3VdbeAddOp2(tls, v, int32(OP_AggFinal), (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg+(*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn+i, v4) _sqlite3VdbeAppendP4(tls, v, (*TAggInfo_func)(unsafe.Pointer(pF)).FpFunc, -int32(7)) goto _1 _1: ; i++ pF += 24 } } // C documentation // // /* // ** Generate code that will update the accumulator memory cells for an // ** aggregate based on the current cursor position. // ** // ** If regAcc is non-zero and there are no min() or max() aggregates // ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator // ** registers if register regAcc contains 0. The caller will take care // ** of setting and clearing regAcc. // ** // ** For an ORDER BY aggregate, the actual accumulator memory cell update // ** is deferred until after all input rows have been received, so that they // ** can be run in the requested order. In that case, instead of invoking // ** OP_AggStep to update the accumulator, just add the arguments that would // ** have been passed into OP_AggStep into the sorting ephemeral table // ** (along with the appropriate sort key). // */ func _updateAccumulator(tls *libc.TLS, pParse uintptr, regAcc int32, pAggInfo uintptr, eDistinctType int32) { var addrHitTest, addrNext, i, j, jj, kk, nArg, regAgg, regAggSz, regBase, regDistinct, regHit, v2, v4, v7 int32 var pC, pColl, pF, pFilter, pItem, pList, pOBList, v, v3, v8 uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrHitTest, addrNext, i, j, jj, kk, nArg, pC, pColl, pF, pFilter, pItem, pList, pOBList, regAgg, regAggSz, regBase, regDistinct, regHit, v, v2, v3, v4, v7, v8 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe regHit = 0 addrHitTest = 0 _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return } (*TAggInfo)(unsafe.Pointer(pAggInfo)).FdirectMode = uint8(1) i = 0 pF = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc for { if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc) { break } addrNext = 0 regAggSz = 0 regDistinct = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pList = *(*uintptr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr + 20)) if (*TExpr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { pFilter = (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr)).Fy)))).FpFilter if (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator != 0 && (*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFunc)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) != 0 && regAcc != 0 { /* If regAcc==0, there there exists some min() or max() function ** without a FILTER clause that will ensure the magnet registers ** are populated. */ if regHit == 0 { v3 = pParse + 44 *(*int32)(unsafe.Pointer(v3))++ v2 = *(*int32)(unsafe.Pointer(v3)) regHit = v2 } /* If this is the first row of the group (regAcc contains 0), clear the ** "magnet" register regHit so that the accumulator registers ** are populated if the FILTER clause jumps over the the ** invocation of min() or max() altogether. Or, if this is not ** the first row (regAcc contains 1), set the magnet register so that ** the accumulators are not populated unless the min()/max() is invoked ** and indicates that they should be. */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), regAcc, regHit) } addrNext = _sqlite3VdbeMakeLabel(tls, pParse) _sqlite3ExprIfFalse(tls, pParse, pFilter, addrNext, int32(SQLITE_JUMPIFNULL)) } if (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab >= 0 { /* The ORDER BY clause */ _ = libc.Int32FromInt32(0) nArg = (*TExprList)(unsafe.Pointer(pList)).FnExpr _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pOBList = *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr)).FpLeft + 20)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) regAggSz = (*TExprList)(unsafe.Pointer(pOBList)).FnExpr if !((*TAggInfo_func)(unsafe.Pointer(pF)).FbOBUnique != 0) { regAggSz++ /* One register for OP_Sequence */ } if (*TAggInfo_func)(unsafe.Pointer(pF)).FbOBPayload != 0 { regAggSz += nArg } if (*TAggInfo_func)(unsafe.Pointer(pF)).FbUseSubtype != 0 { regAggSz += nArg } regAggSz++ /* One extra register to hold result of MakeRecord */ regAgg = _sqlite3GetTempRange(tls, pParse, regAggSz) regDistinct = regAgg _sqlite3ExprCodeExprList(tls, pParse, pOBList, regAgg, 0, uint8(SQLITE_ECEL_DUP)) jj = (*TExprList)(unsafe.Pointer(pOBList)).FnExpr if !((*TAggInfo_func)(unsafe.Pointer(pF)).FbOBUnique != 0) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Sequence), (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab, regAgg+jj) jj++ } if (*TAggInfo_func)(unsafe.Pointer(pF)).FbOBPayload != 0 { regDistinct = regAgg + jj _sqlite3ExprCodeExprList(tls, pParse, pList, regDistinct, 0, uint8(SQLITE_ECEL_DUP)) jj += nArg } if (*TAggInfo_func)(unsafe.Pointer(pF)).FbUseSubtype != 0 { if (*TAggInfo_func)(unsafe.Pointer(pF)).FbOBPayload != 0 { v4 = regDistinct } else { v4 = regAgg } regBase = v4 kk = 0 for { if !(kk < nArg) { break } _sqlite3VdbeAddOp2(tls, v, int32(OP_GetSubtype), regBase+kk, regAgg+jj) goto _5 _5: ; kk++ jj++ } } } else { if pList != 0 { nArg = (*TExprList)(unsafe.Pointer(pList)).FnExpr regAgg = _sqlite3GetTempRange(tls, pParse, nArg) regDistinct = regAgg _sqlite3ExprCodeExprList(tls, pParse, pList, regAgg, 0, uint8(SQLITE_ECEL_DUP)) } else { nArg = 0 regAgg = 0 } } if (*TAggInfo_func)(unsafe.Pointer(pF)).FiDistinct >= 0 && pList != 0 { if addrNext == 0 { addrNext = _sqlite3VdbeMakeLabel(tls, pParse) } (*TAggInfo_func)(unsafe.Pointer(pF)).FiDistinct = _codeDistinct(tls, pParse, eDistinctType, (*TAggInfo_func)(unsafe.Pointer(pF)).FiDistinct, addrNext, pList, regDistinct) } if (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab >= 0 { /* Insert a new record into the ORDER BY table */ _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regAgg, regAggSz-int32(1), regAgg+regAggSz-int32(1)) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab, regAgg+regAggSz-int32(1), regAgg, regAggSz-int32(1)) _sqlite3ReleaseTempRange(tls, pParse, regAgg, regAggSz) } else { /* Invoke the AggStep function */ if (*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFunc)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) != 0 { pColl = uintptr(0) _ = libc.Int32FromInt32(0) /* pList!=0 if pF->pFunc has NEEDCOLL */ j = 0 pItem = pList + 8 for { if !(!(pColl != 0) && j < nArg) { break } pColl = _sqlite3ExprCollSeq(tls, pParse, (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr) goto _6 _6: ; j++ pItem += 20 } if !(pColl != 0) { pColl = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FpDfltColl } if regHit == 0 && (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator != 0 { v8 = pParse + 44 *(*int32)(unsafe.Pointer(v8))++ v7 = *(*int32)(unsafe.Pointer(v8)) regHit = v7 } _sqlite3VdbeAddOp4(tls, v, int32(OP_CollSeq), regHit, 0, 0, pColl, -int32(2)) } _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, int32(OP_AggStep), 0, regAgg, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg+(*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn+i) _sqlite3VdbeAppendP4(tls, v, (*TAggInfo_func)(unsafe.Pointer(pF)).FpFunc, -int32(7)) _sqlite3VdbeChangeP5(tls, v, uint16(uint8(nArg))) _sqlite3ReleaseTempRange(tls, pParse, regAgg, nArg) } if addrNext != 0 { _sqlite3VdbeResolveLabel(tls, v, addrNext) } goto _1 _1: ; i++ pF += 24 } if regHit == 0 && (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator != 0 { regHit = regAcc } if regHit != 0 { addrHitTest = _sqlite3VdbeAddOp1(tls, v, int32(OP_If), regHit) } i = 0 pC = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol for { if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator) { break } _ = libc.Int32FromInt32(0) _sqlite3ExprCode(tls, pParse, (*TAggInfo_col)(unsafe.Pointer(pC)).FpCExpr, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg+i) goto _9 _9: ; i++ pC += 16 } (*TAggInfo)(unsafe.Pointer(pAggInfo)).FdirectMode = uint8(0) if addrHitTest != 0 { _sqlite3VdbeJumpHereOrPopInst(tls, v, addrHitTest) } } // C documentation // // /* // ** Add a single OP_Explain instruction to the VDBE to explain a simple // ** count(*) query ("SELECT count(*) FROM pTab"). // */ func _explainSimpleCount(tls *libc.TLS, pParse uintptr, pTab uintptr, pIdx uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var bCover int32 var v1, v2 uintptr _, _, _ = bCover, v1, v2 if int32((*TParse)(unsafe.Pointer(pParse)).Fexplain) == int32(2) { bCover = libc.BoolInt32(pIdx != uintptr(0) && ((*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) || !(int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == libc.Int32FromInt32(SQLITE_IDXTYPE_PRIMARYKEY)))) if bCover != 0 { v1 = __ccgo_ts + 20090 } else { v1 = __ccgo_ts + 1648 } if bCover != 0 { v2 = (*TIndex)(unsafe.Pointer(pIdx)).FzName } else { v2 = __ccgo_ts + 1648 } _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+20113, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName, v1, v2)) } } // C documentation // // /* // ** sqlite3WalkExpr() callback used by havingToWhere(). // ** // ** If the node passed to the callback is a TK_AND node, return // ** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes. // ** // ** Otherwise, return WRC_Prune. In this case, also check if the // ** sub-expression matches the criteria for being moved to the WHERE // ** clause. If so, add it to the WHERE clause and replace the sub-expression // ** within the HAVING expression with a constant "1". // */ func _havingToWhereExprCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var db, pNew, pS, pWhere uintptr var t TExpr _, _, _, _, _ = db, pNew, pS, pWhere, t if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_AND) { pS = *(*uintptr)(unsafe.Pointer(pWalker + 24)) /* This routine is called before the HAVING clause of the current ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set ** here, it indicates that the expression is a correlated reference to a ** column from an outer aggregate query, or an aggregate function that ** belongs to an outer query. Do not move the expression to the WHERE ** clause in this obscure case, as doing so may corrupt the outer Select ** statements AggInfo structure. */ if _sqlite3ExprIsConstantOrGroupBy(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, pExpr, (*TSelect)(unsafe.Pointer(pS)).FpGroupBy) != 0 && libc.BoolInt32((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_IsFalse)) == uint32(EP_IsFalse)) == 0 && (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo == uintptr(0) { db = (*TParse)(unsafe.Pointer((*TWalker)(unsafe.Pointer(pWalker)).FpParse)).Fdb pNew = _sqlite3Expr(tls, db, int32(TK_INTEGER), __ccgo_ts+8130) if pNew != 0 { pWhere = (*TSelect)(unsafe.Pointer(pS)).FpWhere t = *(*TExpr)(unsafe.Pointer(pNew)) *(*TExpr)(unsafe.Pointer(pNew)) = *(*TExpr)(unsafe.Pointer(pExpr)) *(*TExpr)(unsafe.Pointer(pExpr)) = t pNew = _sqlite3ExprAnd(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, pWhere, pNew) (*TSelect)(unsafe.Pointer(pS)).FpWhere = pNew (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(1) } } return int32(WRC_Prune) } return WRC_Continue } // C documentation // // /* // ** Transfer eligible terms from the HAVING clause of a query, which is // ** processed after grouping, to the WHERE clause, which is processed before // ** grouping. For example, the query: // ** // ** SELECT * FROM WHERE a=? GROUP BY b HAVING b=? AND c=? // ** // ** can be rewritten as: // ** // ** SELECT * FROM WHERE a=? AND b=? GROUP BY b HAVING c=? // ** // ** A term of the HAVING expression is eligible for transfer if it consists // ** entirely of constants and expressions that are also GROUP BY terms that // ** use the "BINARY" collation sequence. // */ func _havingToWhere(tls *libc.TLS, pParse uintptr, p uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* sWalker at bp+0 */ TWalker libc.Xmemset(tls, bp, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp))).FpParse = pParse (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_havingToWhereExprCb) *(*uintptr)(unsafe.Pointer(bp + 24)) = p _sqlite3WalkExpr(tls, bp, (*TSelect)(unsafe.Pointer(p)).FpHaving) } // C documentation // // /* // ** Check to see if the pThis entry of pTabList is a self-join of another view. // ** Search FROM-clause entries in the range of iFirst..iEnd, including iFirst // ** but stopping before iEnd. // ** // ** If pThis is a self-join, then return the SrcItem for the first other // ** instance of that view found. If pThis is not a self-join then return 0. // */ func _isSelfJoinView(tls *libc.TLS, pTabList uintptr, pThis uintptr, iFirst int32, iEnd int32) (r uintptr) { var pItem, pS1 uintptr var v1 int32 _, _, _ = pItem, pS1, v1 _ = libc.Int32FromInt32(0) if (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pThis)).FpSelect)).FselFlags&uint32(SF_PushDown) != 0 { return uintptr(0) } for iFirst < iEnd { v1 = iFirst iFirst++ pItem = pTabList + 8 + uintptr(v1)*72 if (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect == uintptr(0) { continue } if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x20>>5)) != 0 { continue } if (*TSrcItem)(unsafe.Pointer(pItem)).FzName == uintptr(0) { continue } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpTab)).FpSchema != (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pThis)).FpTab)).FpSchema { continue } if Xsqlite3_stricmp(tls, (*TSrcItem)(unsafe.Pointer(pItem)).FzName, (*TSrcItem)(unsafe.Pointer(pThis)).FzName) != 0 { continue } pS1 = (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect if (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpTab)).FpSchema == uintptr(0) && (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pThis)).FpSelect)).FselId != (*TSelect)(unsafe.Pointer(pS1)).FselId { /* The query flattener left two different CTE tables with identical ** names in the same FROM clause. */ continue } if (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpSelect)).FselFlags&uint32(SF_PushDown) != 0 { /* The view was modified by some other optimization such as ** pushDownWhereTerms() */ continue } return pItem } return uintptr(0) } // C documentation // // /* // ** Deallocate a single AggInfo object // */ func _agginfoFree(tls *libc.TLS, db uintptr, pArg uintptr) { var p uintptr _ = p p = pArg _sqlite3DbFree(tls, db, (*TAggInfo)(unsafe.Pointer(p)).FaCol) _sqlite3DbFree(tls, db, (*TAggInfo)(unsafe.Pointer(p)).FaFunc) _sqlite3DbFreeNN(tls, db, p) } // C documentation // // /* // ** Attempt to transform a query of the form // ** // ** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2) // ** // ** Into this: // ** // ** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2) // ** // ** The transformation only works if all of the following are true: // ** // ** * The subquery is a UNION ALL of two or more terms // ** * The subquery does not have a LIMIT clause // ** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries // ** * The outer query is a simple count(*) with no WHERE clause or other // ** extraneous syntax. // ** // ** Return TRUE if the optimization is undertaken. // */ func _countOfViewOptimization(tls *libc.TLS, pParse uintptr, p uintptr) (r int32) { var db, pCount, pExpr, pPrior, pSub, pTerm, v1 uintptr _, _, _, _, _, _, _ = db, pCount, pExpr, pPrior, pSub, pTerm, v1 if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Aggregate) == uint32(0) { return 0 } /* This is an aggregate */ if (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr != int32(1) { return 0 } /* Single result column */ if (*TSelect)(unsafe.Pointer(p)).FpWhere != 0 { return 0 } if (*TSelect)(unsafe.Pointer(p)).FpHaving != 0 { return 0 } if (*TSelect)(unsafe.Pointer(p)).FpGroupBy != 0 { return 0 } if (*TSelect)(unsafe.Pointer(p)).FpOrderBy != 0 { return 0 } pExpr = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList + 8))).FpExpr if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_AGG_FUNCTION) { return 0 } /* Result is an aggregate */ _ = libc.Int32FromInt32(0) if Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), __ccgo_ts+15844) != 0 { return 0 } /* Is count() */ _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(pExpr + 20)) != uintptr(0) { return 0 } /* Must be count(*) */ if (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc)).FnSrc != int32(1) { return 0 } /* One table in FROM */ if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { return 0 } /* Not a window function */ pSub = (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpSelect if pSub == uintptr(0) { return 0 } /* The FROM is a subquery */ if (*TSelect)(unsafe.Pointer(pSub)).FpPrior == uintptr(0) { return 0 } /* Must be a compound */ if (*TSelect)(unsafe.Pointer(pSub)).FselFlags&uint32(SF_CopyCte) != 0 { return 0 } /* Not a CTE */ for cond := true; cond; cond = pSub != 0 { if int32((*TSelect)(unsafe.Pointer(pSub)).Fop) != int32(TK_ALL) && (*TSelect)(unsafe.Pointer(pSub)).FpPrior != 0 { return 0 } /* Must be UNION ALL */ if (*TSelect)(unsafe.Pointer(pSub)).FpWhere != 0 { return 0 } /* No WHERE clause */ if (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 { return 0 } /* No LIMIT clause */ if (*TSelect)(unsafe.Pointer(pSub)).FselFlags&uint32(SF_Aggregate) != 0 { return 0 } /* Not an aggregate */ _ = libc.Int32FromInt32(0) /* Due to the previous */ pSub = (*TSelect)(unsafe.Pointer(pSub)).FpPrior /* Repeat over compound */ } /* If we reach this point then it is OK to perform the transformation */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb pCount = pExpr pExpr = uintptr(0) pSub = (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpSelect (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpSelect = uintptr(0) _sqlite3SrcListDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpSrc) (*TSelect)(unsafe.Pointer(p)).FpSrc = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(80)) for pSub != 0 { pPrior = (*TSelect)(unsafe.Pointer(pSub)).FpPrior (*TSelect)(unsafe.Pointer(pSub)).FpPrior = uintptr(0) (*TSelect)(unsafe.Pointer(pSub)).FpNext = uintptr(0) *(*Tu32)(unsafe.Pointer(pSub + 4)) |= uint32(SF_Aggregate) *(*Tu32)(unsafe.Pointer(pSub + 4)) &= uint32(^libc.Int32FromInt32(SF_Compound)) (*TSelect)(unsafe.Pointer(pSub)).FnSelectRow = 0 _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3ExprListDeleteGeneric), (*TSelect)(unsafe.Pointer(pSub)).FpEList) if pPrior != 0 { v1 = _sqlite3ExprDup(tls, db, pCount, 0) } else { v1 = pCount } pTerm = v1 (*TSelect)(unsafe.Pointer(pSub)).FpEList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), pTerm) pTerm = _sqlite3PExpr(tls, pParse, int32(TK_SELECT), uintptr(0), uintptr(0)) _sqlite3PExprAddSelect(tls, pParse, pTerm, pSub) if pExpr == uintptr(0) { pExpr = pTerm } else { pExpr = _sqlite3PExpr(tls, pParse, int32(TK_PLUS), pTerm, pExpr) } pSub = pPrior } (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList + 8))).FpExpr = pExpr *(*Tu32)(unsafe.Pointer(p + 4)) &= uint32(^libc.Int32FromInt32(SF_Aggregate)) return int32(1) } // C documentation // // /* // ** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same // ** as pSrcItem but has the same alias as p0, then return true. // ** Otherwise return false. // */ func _sameSrcAlias(tls *libc.TLS, p0 uintptr, pSrc uintptr) (r int32) { var i int32 var p1 uintptr _, _ = i, p1 i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) { break } p1 = pSrc + 8 + uintptr(i)*72 if p1 == p0 { goto _1 } if (*TSrcItem)(unsafe.Pointer(p0)).FpTab == (*TSrcItem)(unsafe.Pointer(p1)).FpTab && 0 == Xsqlite3_stricmp(tls, (*TSrcItem)(unsafe.Pointer(p0)).FzAlias, (*TSrcItem)(unsafe.Pointer(p1)).FzAlias) { return int32(1) } if (*TSrcItem)(unsafe.Pointer(p1)).FpSelect != 0 && (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(p1)).FpSelect)).FselFlags&uint32(SF_NestedFrom) != uint32(0) && _sameSrcAlias(tls, p0, (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(p1)).FpSelect)).FpSrc) != 0 { return int32(1) } goto _1 _1: ; i++ } return 0 } // C documentation // // /* // ** Return TRUE (non-zero) if the i-th entry in the pTabList SrcList can // ** be implemented as a co-routine. The i-th entry is guaranteed to be // ** a subquery. // ** // ** The subquery is implemented as a co-routine if all of the following are // ** true: // ** // ** (1) The subquery will likely be implemented in the outer loop of // ** the query. This will be the case if any one of the following // ** conditions hold: // ** (a) The subquery is the only term in the FROM clause // ** (b) The subquery is the left-most term and a CROSS JOIN or similar // ** requires it to be the outer loop // ** (c) All of the following are true: // ** (i) The subquery is the left-most subquery in the FROM clause // ** (ii) There is nothing that would prevent the subquery from // ** being used as the outer loop if the sqlite3WhereBegin() // ** routine nominates it to that position. // ** (iii) The query is not a UPDATE ... FROM // ** (2) The subquery is not a CTE that should be materialized because // ** (a) the AS MATERIALIZED keyword is used, or // ** (b) the CTE is used multiple times and does not have the // ** NOT MATERIALIZED keyword // ** (3) The subquery is not part of a left operand for a RIGHT JOIN // ** (4) The SQLITE_Coroutine optimization disable flag is not set // ** (5) The subquery is not self-joined // */ func _fromClauseTermCanBeCoroutine(tls *libc.TLS, pParse uintptr, pTabList uintptr, i int32, selFlags int32) (r int32) { var pCteUse, pItem uintptr _, _ = pCteUse, pItem pItem = pTabList + 8 + uintptr(i)*72 if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x100>>8)) != 0 { pCteUse = *(*uintptr)(unsafe.Pointer(pItem + 68)) if int32((*TCteUse)(unsafe.Pointer(pCteUse)).FeM10d) == M10d_Yes { return 0 } /* (2a) */ if (*TCteUse)(unsafe.Pointer(pCteUse)).FnUse >= int32(2) && int32((*TCteUse)(unsafe.Pointer(pCteUse)).FeM10d) != int32(M10d_No) { return 0 } /* (2b) */ } if int32((*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 { return 0 } /* (3) */ if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Coroutines)) != uint32(0) { return 0 } /* (4) */ if _isSelfJoinView(tls, pTabList, pItem, i+int32(1), (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) != uintptr(0) { return 0 /* (5) */ } if i == 0 { if (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc == int32(1) { return int32(1) } /* (1a) */ if int32((*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + 1*72))).Ffg.Fjointype)&int32(JT_CROSS) != 0 { return int32(1) } /* (1b) */ if selFlags&int32(SF_UpdateFrom) != 0 { return 0 } /* (1c-iii) */ return int32(1) } if selFlags&int32(SF_UpdateFrom) != 0 { return 0 } /* (1c-iii) */ for int32(1) != 0 { if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_OUTER)|libc.Int32FromInt32(JT_CROSS)) != 0 { return 0 } /* (1c-ii) */ if i == 0 { break } i-- pItem -= 72 if (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect != uintptr(0) { return 0 } /* (1c-i) */ } return int32(1) } // C documentation // // /* // ** Generate code for the SELECT statement given in the p argument. // ** // ** The results are returned according to the SelectDest structure. // ** See comments in sqliteInt.h for further information. // ** // ** This routine returns the number of errors. If any errors are // ** encountered, then an appropriate error message is left in // ** pParse->zErrMsg. // ** // ** This routine does NOT free the Select structure passed in. The // ** calling function needs to do that. // */ func _sqlite3Select(tls *libc.TLS, pParse uintptr, p uintptr, pDest uintptr) (r int32) { bp := tls.Alloc(160) defer tls.Free(160) var addr1, addrEnd, addrGosub, addrOutputRow, addrReset, addrSetAbort, addrSortingIdx, addrTop, addrTopOfLoop, eDist, eDist1, groupBySort, i, iAMem, iAbortFlag, iBMem, iBreak, iCont, iCsr, iDb, iEnd, iUseFlag, ii, ii1, isAgg, j, k, nCol, nGroupBy, onceAddr, orderByGrp, rc, regAcc, regBase, regGosub, regOutputRow, regRecord, regReset, sortOut, sortPTab, topAddr, v11, v14, v17, v22, v24, v25, v30, v31, v32, v34, v36, v38, v40, v42, v43, v47, v48, v52, v56, v58 int32 var db, p0, pAggInfo, pBest, pCol, pCteUse, pCteUse1, pDistinct, pDistinct1, pEList, pExpr, pF, pF1, pGroupBy, pHaving, pI2, pIdx, pItem, pItem1, pItem2, pKeyInfo, pKeyInfo1, pKeyInfo2, pPrior, pSub, pSub1, pTab, pTab1, pTabList, pWInfo, pWhere, pWin, v, zSavedAuthContext, v1, v12, v13, v15, v16, v18, v23, v26, v33, v35, v37, v39, v41, v44, v49, v51, v53, v57, p21, p3, p4, p6, p7, p9 uintptr var distFlag, distFlag1, wctrlFlags Tu16 var iRoot TPgno var minMaxFlag, sortFlags Tu8 var _ /* dest at bp+56 */ TSelectDest var _ /* pMinMaxOrderBy at bp+52 */ uintptr var _ /* sDistinct at bp+0 */ TDistinctCtx var _ /* sNC at bp+84 */ TNameContext var _ /* sSort at bp+12 */ TSortCtx _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr1, addrEnd, addrGosub, addrOutputRow, addrReset, addrSetAbort, addrSortingIdx, addrTop, addrTopOfLoop, db, distFlag, distFlag1, eDist, eDist1, groupBySort, i, iAMem, iAbortFlag, iBMem, iBreak, iCont, iCsr, iDb, iEnd, iRoot, iUseFlag, ii, ii1, isAgg, j, k, minMaxFlag, nCol, nGroupBy, onceAddr, orderByGrp, p0, pAggInfo, pBest, pCol, pCteUse, pCteUse1, pDistinct, pDistinct1, pEList, pExpr, pF, pF1, pGroupBy, pHaving, pI2, pIdx, pItem, pItem1, pItem2, pKeyInfo, pKeyInfo1, pKeyInfo2, pPrior, pSub, pSub1, pTab, pTab1, pTabList, pWInfo, pWhere, pWin, rc, regAcc, regBase, regGosub, regOutputRow, regRecord, regReset, sortFlags, sortOut, sortPTab, topAddr, v, wctrlFlags, zSavedAuthContext, v1, v11, v12, v13, v14, v15, v16, v17, v18, v22, v23, v24, v25, v26, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v47, v48, v49, v51, v52, v53, v56, v57, v58, p21, p3, p4, p6, p7, p9 /* True for select lists like "count(*)" */ pEList = uintptr(0) /* The HAVING clause. May be NULL */ pAggInfo = uintptr(0) /* Aggregate information */ rc = int32(1) /* The database connection */ *(*uintptr)(unsafe.Pointer(bp + 52)) = uintptr(0) /* Flag for min/max queries */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) v = _sqlite3GetVdbe(tls, pParse) if p == uintptr(0) || (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return int32(1) } _ = libc.Int32FromInt32(0) if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_SELECT), uintptr(0), uintptr(0), uintptr(0)) != 0 { return int32(1) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) <= int32(SRT_DistQueue) { _ = libc.Int32FromInt32(0) /* All of these destinations are also able to ignore the ORDER BY clause */ if (*TSelect)(unsafe.Pointer(p)).FpOrderBy != 0 { _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3ExprListDeleteGeneric), (*TSelect)(unsafe.Pointer(p)).FpOrderBy) (*TSelect)(unsafe.Pointer(p)).FpOrderBy = uintptr(0) } *(*Tu32)(unsafe.Pointer(p + 4)) &= uint32(^libc.Int32FromInt32(SF_Distinct)) *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(SF_NoopOrderBy) } _sqlite3SelectPrep(tls, pParse, p, uintptr(0)) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto select_end } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If the SF_UFSrcCheck flag is set, then this function is being called ** as part of populating the temp table for an UPDATE...FROM statement. ** In this case, it is an error if the target object (pSrc->a[0]) name ** or alias is duplicated within FROM clause (pSrc->a[1..n]). ** ** Postgres disallows this case too. The reason is that some other ** systems handle this case differently, and not all the same way, ** which is just confusing. To avoid this, we follow PG's lead and ** disallow it altogether. */ if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_UFSrcCheck) != 0 { p0 = (*TSelect)(unsafe.Pointer(p)).FpSrc + 8 if _sameSrcAlias(tls, p0, (*TSelect)(unsafe.Pointer(p)).FpSrc) != 0 { if (*TSrcItem)(unsafe.Pointer(p0)).FzAlias != 0 { v1 = (*TSrcItem)(unsafe.Pointer(p0)).FzAlias } else { v1 = (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(p0)).FpTab)).FzName } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20125, libc.VaList(bp+128, v1)) goto select_end } /* Clear the SF_UFSrcCheck flag. The check has already been performed, ** and leaving this flag set can cause errors if a compound sub-query ** in p->pSrc is flattened into this query and this function called ** again as part of compound SELECT processing. */ *(*Tu32)(unsafe.Pointer(p + 4)) &= uint32(^libc.Int32FromInt32(SF_UFSrcCheck)) } if int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) == int32(SRT_Output) { _sqlite3GenerateColumnNames(tls, pParse, p) } if _sqlite3WindowRewrite(tls, pParse, p) != 0 { _ = libc.Int32FromInt32(0) goto select_end } pTabList = (*TSelect)(unsafe.Pointer(p)).FpSrc isAgg = libc.BoolInt32((*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Aggregate) != uint32(0)) libc.Xmemset(tls, bp+12, 0, uint32(40)) (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy /* Try to do various optimizations (flattening subqueries, and strength ** reduction of join operators) in the FROM clause up into the main query */ i = 0 for { if !(!((*TSelect)(unsafe.Pointer(p)).FpPrior != 0) && i < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) { break } pItem = pTabList + 8 + uintptr(i)*72 pSub = (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab /* The expander should have already created transient Table objects ** even for FROM clause elements such as subqueries that do not correspond ** to a real table */ _ = libc.Int32FromInt32(0) /* Try to simplify joins: ** ** LEFT JOIN -> JOIN ** RIGHT JOIN -> JOIN ** FULL JOIN -> RIGHT JOIN ** ** If terms of the i-th table are used in the WHERE clause in such a ** way that the i-th table cannot be the NULL row of a join, then ** perform the appropriate simplification. This is called ** "OUTER JOIN strength reduction" in the SQLite documentation. */ if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)) != 0 && _sqlite3ExprImpliesNonNullRow(tls, (*TSelect)(unsafe.Pointer(p)).FpWhere, (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor, int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_LTORJ)) != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_SimplifyJoin)) == uint32(0) { if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_LEFT) != 0 { if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_RIGHT) != 0 { p3 = pItem + 36 *(*Tu8)(unsafe.Pointer(p3)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p3))) & ^libc.Int32FromInt32(JT_LEFT)) } else { p4 = pItem + 36 *(*Tu8)(unsafe.Pointer(p4)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p4))) & ^(libc.Int32FromInt32(JT_LEFT) | libc.Int32FromInt32(JT_OUTER))) _unsetJoinExpr(tls, (*TSelect)(unsafe.Pointer(p)).FpWhere, (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor, 0) } } if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_LTORJ) != 0 { j = i + int32(1) for { if !(j < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) { break } pI2 = pTabList + 8 + uintptr(j)*72 if int32((*TSrcItem)(unsafe.Pointer(pI2)).Ffg.Fjointype)&int32(JT_RIGHT) != 0 { if int32((*TSrcItem)(unsafe.Pointer(pI2)).Ffg.Fjointype)&int32(JT_LEFT) != 0 { p6 = pI2 + 36 *(*Tu8)(unsafe.Pointer(p6)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p6))) & ^libc.Int32FromInt32(JT_RIGHT)) } else { p7 = pI2 + 36 *(*Tu8)(unsafe.Pointer(p7)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p7))) & ^(libc.Int32FromInt32(JT_RIGHT) | libc.Int32FromInt32(JT_OUTER))) _unsetJoinExpr(tls, (*TSelect)(unsafe.Pointer(p)).FpWhere, (*TSrcItem)(unsafe.Pointer(pI2)).FiCursor, int32(1)) } } goto _5 _5: ; j++ } j = (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc - int32(1) for { if !(j >= 0) { break } p9 = pTabList + 8 + uintptr(j)*72 + 36 *(*Tu8)(unsafe.Pointer(p9)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p9))) & ^libc.Int32FromInt32(JT_LTORJ)) if int32((*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(j)*72))).Ffg.Fjointype)&int32(JT_RIGHT) != 0 { break } goto _8 _8: ; j-- } } } /* No further action if this term of the FROM clause is not a subquery */ if pSub == uintptr(0) { goto _2 } /* Catch mismatch in the declared columns of a view and the number of ** columns in the SELECT on the RHS */ if int32((*TTable)(unsafe.Pointer(pTab)).FnCol) != (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub)).FpEList)).FnExpr { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20179, libc.VaList(bp+128, int32((*TTable)(unsafe.Pointer(pTab)).FnCol), (*TTable)(unsafe.Pointer(pTab)).FzName, (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub)).FpEList)).FnExpr)) goto select_end } /* Do not attempt the usual optimizations (flattening and ORDER BY ** elimination) on a MATERIALIZED common table expression because ** a MATERIALIZED common table expression is an optimization fence. */ if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x100>>8)) != 0 && int32((*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem + 68)))).FeM10d) == M10d_Yes { goto _2 } /* Do not try to flatten an aggregate subquery. ** ** Flattening an aggregate subquery is only possible if the outer query ** is not a join. But if the outer query is not a join, then the subquery ** will be implemented as a co-routine and there is no advantage to ** flattening in that case. */ if (*TSelect)(unsafe.Pointer(pSub)).FselFlags&uint32(SF_Aggregate) != uint32(0) { goto _2 } _ = libc.Int32FromInt32(0) /* If a FROM-clause subquery has an ORDER BY clause that is not ** really doing anything, then delete it now so that it does not ** interfere with query flattening. See the discussion at ** https://sqlite.org/forum/forumpost/2d76f2bcf65d256a ** ** Beware of these cases where the ORDER BY clause may not be safely ** omitted: ** ** (1) There is also a LIMIT clause ** (2) The subquery was added to help with window-function ** processing ** (3) The subquery is in the FROM clause of an UPDATE ** (4) The outer query uses an aggregate function other than ** the built-in count(), min(), or max(). ** (5) The ORDER BY isn't going to accomplish anything because ** one of: ** (a) The outer query has a different ORDER BY clause ** (b) The subquery is part of a join ** See forum post 062d576715d277c8 ** ** Also retain the ORDER BY if the OmitOrderBy optimization is disabled. */ if (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy != uintptr(0) && ((*TSelect)(unsafe.Pointer(p)).FpOrderBy != uintptr(0) || (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc > int32(1)) && (*TSelect)(unsafe.Pointer(pSub)).FpLimit == uintptr(0) && (*TSelect)(unsafe.Pointer(pSub)).FselFlags&uint32(SF_OrderByReqd) == uint32(0) && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_OrderByReqd) == uint32(0) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_OmitOrderBy)) == uint32(0) { _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3ExprListDeleteGeneric), (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy) (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy = uintptr(0) } /* If the outer query contains a "complex" result set (that is, ** if the result set of the outer query uses functions or subqueries) ** and if the subquery contains an ORDER BY clause and if ** it will be implemented as a co-routine, then do not flatten. This ** restriction allows SQL constructs like this: ** ** SELECT expensive_function(x) ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10); ** ** The expensive_function() is only computed on the 10 rows that ** are output, rather than every row of the table. ** ** The requirement that the outer query have a complex result set ** means that flattening does occur on simpler SQL constraints without ** the expensive_function() like: ** ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10); */ if (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy != uintptr(0) && i == 0 && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_ComplexResult) != uint32(0) && ((*TSrcList)(unsafe.Pointer(pTabList)).FnSrc == int32(1) || int32((*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + 1*72))).Ffg.Fjointype)&(libc.Int32FromInt32(JT_OUTER)|libc.Int32FromInt32(JT_CROSS)) != 0) { goto _2 } if _flattenSubquery(tls, pParse, p, i, isAgg) != 0 { if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto select_end } /* This subquery can be absorbed into its parent. */ i = -int32(1) } pTabList = (*TSelect)(unsafe.Pointer(p)).FpSrc if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto select_end } if !(int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) <= libc.Int32FromInt32(SRT_Fifo)) { (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy } goto _2 _2: ; i++ } /* Handle compound SELECT statements using the separate multiSelect() ** procedure. */ if (*TSelect)(unsafe.Pointer(p)).FpPrior != 0 { rc = _multiSelect(tls, pParse, p, pDest) if (*TSelect)(unsafe.Pointer(p)).FpNext == uintptr(0) { _sqlite3VdbeExplainPop(tls, pParse) } return rc } /* Do the WHERE-clause constant propagation optimization if this is ** a join. No need to speed time on this operation for non-join queries ** as the equivalent optimization will be handled by query planner in ** sqlite3WhereBegin(). */ if (*TSelect)(unsafe.Pointer(p)).FpWhere != uintptr(0) && int32((*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpWhere)).Fop) == int32(TK_AND) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_PropagateConst)) == uint32(0) && _propagateConstants(tls, pParse, p) != 0 { } else { } if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_QueryFlattener)|libc.Int32FromInt32(SQLITE_CountOfView)) == uint32(0) && _countOfViewOptimization(tls, pParse, p) != 0 { if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto select_end } pTabList = (*TSelect)(unsafe.Pointer(p)).FpSrc } /* For each term in the FROM clause, do two things: ** (1) Authorized unreferenced tables ** (2) Generate code for all sub-queries */ i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) { break } pItem1 = pTabList + 8 + uintptr(i)*72 /* Issue SQLITE_READ authorizations with a fake column name for any ** tables that are referenced but from which no values are extracted. ** Examples of where these kinds of null SQLITE_READ authorizations ** would occur: ** ** SELECT count(*) FROM t1; -- SQLITE_READ t1."" ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2."" ** ** The fake column name is an empty string. It is possible for a table to ** have a column named by the empty string, in which case there is no way to ** distinguish between an unreferenced table and an actual reference to the ** "" column. The original design was for the fake column name to be a NULL, ** which would be unambiguous. But legacy authorization callbacks might ** assume the column name is non-NULL and segfault. The use of an empty ** string for the fake column name seems safer. */ if (*TSrcItem)(unsafe.Pointer(pItem1)).FcolUsed == uint64(0) && (*TSrcItem)(unsafe.Pointer(pItem1)).FzName != uintptr(0) { _sqlite3AuthCheck(tls, pParse, int32(SQLITE_READ), (*TSrcItem)(unsafe.Pointer(pItem1)).FzName, __ccgo_ts+1648, (*TSrcItem)(unsafe.Pointer(pItem1)).FzDatabase) } /* Generate code for all sub-queries in the FROM clause */ pSub1 = (*TSrcItem)(unsafe.Pointer(pItem1)).FpSelect if pSub1 == uintptr(0) { goto _10 } /* The code for a subquery should only be generated once. */ _ = libc.Int32FromInt32(0) /* Increment Parse.nHeight by the height of the largest expression ** tree referred to by this, the parent select. The child select ** may contain expression trees of at most ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit ** more conservative than necessary, but much easier than enforcing ** an exact limit. */ *(*int32)(unsafe.Pointer(pParse + 228)) += _sqlite3SelectExprHeight(tls, p) /* Make copies of constant WHERE-clause terms in the outer query down ** inside the subquery. This can help the subquery to run more efficiently. */ if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_PushDown)) == uint32(0) && (int32(uint32(*(*uint16)(unsafe.Pointer(pItem1 + 36 + 4))&0x100>>8)) == 0 || int32((*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem1 + 68)))).FeM10d) != M10d_Yes && (*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem1 + 68)))).FnUse < int32(2)) && _pushDownWhereTerms(tls, pParse, pSub1, (*TSelect)(unsafe.Pointer(p)).FpWhere, pTabList, i) != 0 { _ = libc.Int32FromInt32(0) } else { } /* Convert unused result columns of the subquery into simple NULL ** expressions, to avoid unneeded searching and computation. */ if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_NullUnusedCols)) == uint32(0) && _disableUnusedSubqueryResultColumns(tls, pItem1) != 0 { } zSavedAuthContext = (*TParse)(unsafe.Pointer(pParse)).FzAuthContext (*TParse)(unsafe.Pointer(pParse)).FzAuthContext = (*TSrcItem)(unsafe.Pointer(pItem1)).FzName /* Generate code to implement the subquery */ if _fromClauseTermCanBeCoroutine(tls, pParse, pTabList, i, int32((*TSelect)(unsafe.Pointer(p)).FselFlags)) != 0 { /* Implement a co-routine that will return a single row of the result ** set on each invocation. */ addrTop = _sqlite3VdbeCurrentAddr(tls, v) + int32(1) v12 = pParse + 44 *(*int32)(unsafe.Pointer(v12))++ v11 = *(*int32)(unsafe.Pointer(v12)) (*TSrcItem)(unsafe.Pointer(pItem1)).FregReturn = v11 _sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), (*TSrcItem)(unsafe.Pointer(pItem1)).FregReturn, 0, addrTop) (*TSrcItem)(unsafe.Pointer(pItem1)).FaddrFillSub = addrTop _sqlite3SelectDestInit(tls, bp+56, int32(SRT_Coroutine), (*TSrcItem)(unsafe.Pointer(pItem1)).FregReturn) _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+20219, libc.VaList(bp+128, pItem1)) _sqlite3Select(tls, pParse, pSub1, bp+56) (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem1)).FpTab)).FnRowLogEst = (*TSelect)(unsafe.Pointer(pSub1)).FnSelectRow libc.SetBitFieldPtr16Uint32(pItem1+36+4, libc.Uint32FromInt32(1), 5, 0x20) (*TSrcItem)(unsafe.Pointer(pItem1)).FregResult = (*(*TSelectDest)(unsafe.Pointer(bp + 56))).FiSdst _sqlite3VdbeEndCoroutine(tls, v, (*TSrcItem)(unsafe.Pointer(pItem1)).FregReturn) _sqlite3VdbeJumpHere(tls, v, addrTop-int32(1)) _sqlite3ClearTempRegCache(tls, pParse) } else { if int32(uint32(*(*uint16)(unsafe.Pointer(pItem1 + 36 + 4))&0x100>>8)) != 0 && (*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem1 + 68)))).FaddrM9e > 0 { /* This is a CTE for which materialization code has already been ** generated. Invoke the subroutine to compute the materialization, ** the make the pItem->iCursor be a copy of the ephemeral table that ** holds the result of the materialization. */ pCteUse = *(*uintptr)(unsafe.Pointer(pItem1 + 68)) _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TCteUse)(unsafe.Pointer(pCteUse)).FregRtn, (*TCteUse)(unsafe.Pointer(pCteUse)).FaddrM9e) if (*TSrcItem)(unsafe.Pointer(pItem1)).FiCursor != (*TCteUse)(unsafe.Pointer(pCteUse)).FiCur { _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TSrcItem)(unsafe.Pointer(pItem1)).FiCursor, (*TCteUse)(unsafe.Pointer(pCteUse)).FiCur) } (*TSelect)(unsafe.Pointer(pSub1)).FnSelectRow = (*TCteUse)(unsafe.Pointer(pCteUse)).FnRowEst } else { v13 = _isSelfJoinView(tls, pTabList, pItem1, 0, i) pPrior = v13 if v13 != uintptr(0) { /* This view has already been materialized by a prior entry in ** this same FROM clause. Reuse it. */ if (*TSrcItem)(unsafe.Pointer(pPrior)).FaddrFillSub != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TSrcItem)(unsafe.Pointer(pPrior)).FregReturn, (*TSrcItem)(unsafe.Pointer(pPrior)).FaddrFillSub) } _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TSrcItem)(unsafe.Pointer(pItem1)).FiCursor, (*TSrcItem)(unsafe.Pointer(pPrior)).FiCursor) (*TSelect)(unsafe.Pointer(pSub1)).FnSelectRow = (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pPrior)).FpSelect)).FnSelectRow } else { onceAddr = 0 v15 = pParse + 44 *(*int32)(unsafe.Pointer(v15))++ v14 = *(*int32)(unsafe.Pointer(v15)) (*TSrcItem)(unsafe.Pointer(pItem1)).FregReturn = v14 topAddr = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto)) (*TSrcItem)(unsafe.Pointer(pItem1)).FaddrFillSub = topAddr + int32(1) libc.SetBitFieldPtr16Uint32(pItem1+36+4, libc.Uint32FromInt32(1), 4, 0x10) if int32(uint32(*(*uint16)(unsafe.Pointer(pItem1 + 36 + 4))&0x8>>3)) == 0 { /* If the subquery is not correlated and if we are not inside of ** a trigger, then we only need to compute the value of the subquery ** once. */ onceAddr = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once)) } else { } _sqlite3SelectDestInit(tls, bp+56, int32(SRT_EphemTab), (*TSrcItem)(unsafe.Pointer(pItem1)).FiCursor) _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+20234, libc.VaList(bp+128, pItem1)) _sqlite3Select(tls, pParse, pSub1, bp+56) (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem1)).FpTab)).FnRowLogEst = (*TSelect)(unsafe.Pointer(pSub1)).FnSelectRow if onceAddr != 0 { _sqlite3VdbeJumpHere(tls, v, onceAddr) } _sqlite3VdbeAddOp2(tls, v, int32(OP_Return), (*TSrcItem)(unsafe.Pointer(pItem1)).FregReturn, topAddr+int32(1)) _sqlite3VdbeJumpHere(tls, v, topAddr) _sqlite3ClearTempRegCache(tls, pParse) if int32(uint32(*(*uint16)(unsafe.Pointer(pItem1 + 36 + 4))&0x100>>8)) != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pItem1 + 36 + 4))&0x8>>3)) == 0 { pCteUse1 = *(*uintptr)(unsafe.Pointer(pItem1 + 68)) (*TCteUse)(unsafe.Pointer(pCteUse1)).FaddrM9e = (*TSrcItem)(unsafe.Pointer(pItem1)).FaddrFillSub (*TCteUse)(unsafe.Pointer(pCteUse1)).FregRtn = (*TSrcItem)(unsafe.Pointer(pItem1)).FregReturn (*TCteUse)(unsafe.Pointer(pCteUse1)).FiCur = (*TSrcItem)(unsafe.Pointer(pItem1)).FiCursor (*TCteUse)(unsafe.Pointer(pCteUse1)).FnRowEst = (*TSelect)(unsafe.Pointer(pSub1)).FnSelectRow } } } } if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto select_end } *(*int32)(unsafe.Pointer(pParse + 228)) -= _sqlite3SelectExprHeight(tls, p) (*TParse)(unsafe.Pointer(pParse)).FzAuthContext = zSavedAuthContext goto _10 _10: ; i++ } /* Various elements of the SELECT copied into local variables for ** convenience */ pEList = (*TSelect)(unsafe.Pointer(p)).FpEList pWhere = (*TSelect)(unsafe.Pointer(p)).FpWhere pGroupBy = (*TSelect)(unsafe.Pointer(p)).FpGroupBy pHaving = (*TSelect)(unsafe.Pointer(p)).FpHaving (*(*TDistinctCtx)(unsafe.Pointer(bp))).FisTnct = libc.BoolUint8((*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Distinct) != uint32(0)) /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and ** if the select-list is the same as the ORDER BY list, then this query ** can be rewritten as a GROUP BY. In other words, this: ** ** SELECT DISTINCT xyz FROM ... ORDER BY xyz ** ** is transformed to: ** ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz ** ** The second form is preferred as a single index (or temp-table) may be ** used for both the ORDER BY and DISTINCT processing. As originally ** written the query must use a temp-table for at least one of the ORDER ** BY and DISTINCT, and an index or separate temp-table for the other. */ if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(libc.Int32FromInt32(SF_Distinct)|libc.Int32FromInt32(SF_Aggregate)) == uint32(SF_Distinct) && _sqlite3ExprListCompare(tls, (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy, pEList, -int32(1)) == 0 && (*TSelect)(unsafe.Pointer(p)).FpWin == uintptr(0) { *(*Tu32)(unsafe.Pointer(p + 4)) &= uint32(^libc.Int32FromInt32(SF_Distinct)) v16 = _sqlite3ExprListDup(tls, db, pEList, 0) (*TSelect)(unsafe.Pointer(p)).FpGroupBy = v16 pGroupBy = v16 *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(SF_Aggregate) /* Notice that even thought SF_Distinct has been cleared from p->selFlags, ** the sDistinct.isTnct is still set. Hence, isTnct represents the ** original setting of the SF_Distinct flag, not the current setting */ _ = libc.Int32FromInt32(0) (*(*TDistinctCtx)(unsafe.Pointer(bp))).FisTnct = uint8(2) } /* If there is an ORDER BY clause, then create an ephemeral index to ** do the sorting. But this sorting ephemeral index might end up ** being unused if the data can be extracted in pre-sorted order. ** If that is the case, then the OP_OpenEphemeral instruction will be ** changed to an OP_Noop once we figure out that the sorting index is ** not needed. The sSort.addrSortIndex variable is used to facilitate ** that change. */ if (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy != 0 { pKeyInfo = _sqlite3KeyInfoFromExprList(tls, pParse, (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy, 0, (*TExprList)(unsafe.Pointer(pEList)).FnExpr) v18 = pParse + 40 v17 = *(*int32)(unsafe.Pointer(v18)) *(*int32)(unsafe.Pointer(v18))++ (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FiECursor = v17 (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FaddrSortIndex = _sqlite3VdbeAddOp4(tls, v, int32(OP_OpenEphemeral), (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FiECursor, (*TExprList)(unsafe.Pointer((*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy)).FnExpr+int32(1)+(*TExprList)(unsafe.Pointer(pEList)).FnExpr, 0, pKeyInfo, -int32(8)) } else { (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FaddrSortIndex = -int32(1) } /* If the output is destined for a temporary table, open that table. */ if int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) == int32(SRT_EphemTab) { _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm, (*TExprList)(unsafe.Pointer(pEList)).FnExpr) if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_NestedFrom) != 0 { ii = (*TExprList)(unsafe.Pointer(pEList)).FnExpr - int32(1) for { if !(ii > 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(ii)*20 + 8 + 4))&0x40>>6)) == 0) { break } _sqlite3ExprDelete(tls, db, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(ii)*20))).FpExpr) _sqlite3DbFree(tls, db, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(ii)*20))).FzEName) (*TExprList)(unsafe.Pointer(pEList)).FnExpr-- goto _19 _19: ; ii-- } ii = 0 for { if !(ii < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } if int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(ii)*20 + 8 + 4))&0x40>>6)) == 0 { (*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(ii)*20))).FpExpr)).Fop = uint8(TK_NULL) } goto _20 _20: ; ii++ } } } /* Set the limiter. */ iEnd = _sqlite3VdbeMakeLabel(tls, pParse) if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_FixedLimit) == uint32(0) { (*TSelect)(unsafe.Pointer(p)).FnSelectRow = int16(320) /* 4 billion rows */ } if (*TSelect)(unsafe.Pointer(p)).FpLimit != 0 { _computeLimitRegisters(tls, pParse, p, iEnd) } if (*TSelect)(unsafe.Pointer(p)).FiLimit == 0 && (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FaddrSortIndex >= 0 { _sqlite3VdbeChangeOpcode(tls, v, (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FaddrSortIndex, uint8(OP_SorterOpen)) p21 = bp + 12 + 32 *(*Tu8)(unsafe.Pointer(p21)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p21))) | libc.Int32FromInt32(SORTFLAG_UseSorter)) } /* Open an ephemeral index to use for the distinct set. */ if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Distinct) != 0 { v23 = pParse + 40 v22 = *(*int32)(unsafe.Pointer(v23)) *(*int32)(unsafe.Pointer(v23))++ (*(*TDistinctCtx)(unsafe.Pointer(bp))).FtabTnct = v22 (*(*TDistinctCtx)(unsafe.Pointer(bp))).FaddrTnct = _sqlite3VdbeAddOp4(tls, v, int32(OP_OpenEphemeral), (*(*TDistinctCtx)(unsafe.Pointer(bp))).FtabTnct, 0, 0, _sqlite3KeyInfoFromExprList(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpEList, 0, 0), -int32(8)) _sqlite3VdbeChangeP5(tls, v, uint16(BTREE_UNORDERED)) (*(*TDistinctCtx)(unsafe.Pointer(bp))).FeTnctType = uint8(WHERE_DISTINCT_UNORDERED) } else { (*(*TDistinctCtx)(unsafe.Pointer(bp))).FeTnctType = uint8(WHERE_DISTINCT_NOOP) } if !(isAgg != 0) && pGroupBy == uintptr(0) { if (*(*TDistinctCtx)(unsafe.Pointer(bp))).FisTnct != 0 { v24 = int32(WHERE_WANT_DISTINCT) } else { v24 = 0 } /* No aggregate functions and no GROUP BY clause */ wctrlFlags = uint16(uint32(v24) | (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_FixedLimit)) pWin = (*TSelect)(unsafe.Pointer(p)).FpWin /* Main window object (or NULL) */ if pWin != 0 { _sqlite3WindowCodeInit(tls, pParse, p) } _ = libc.Int32FromInt32(0) /* Begin the database scan. */ pWInfo = _sqlite3WhereBegin(tls, pParse, pTabList, pWhere, (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy, (*TSelect)(unsafe.Pointer(p)).FpEList, p, wctrlFlags, int32((*TSelect)(unsafe.Pointer(p)).FnSelectRow)) if pWInfo == uintptr(0) { goto select_end } if int32(_sqlite3WhereOutputRowCount(tls, pWInfo)) < int32((*TSelect)(unsafe.Pointer(p)).FnSelectRow) { (*TSelect)(unsafe.Pointer(p)).FnSelectRow = _sqlite3WhereOutputRowCount(tls, pWInfo) } if (*(*TDistinctCtx)(unsafe.Pointer(bp))).FisTnct != 0 && _sqlite3WhereIsDistinct(tls, pWInfo) != 0 { (*(*TDistinctCtx)(unsafe.Pointer(bp))).FeTnctType = uint8(_sqlite3WhereIsDistinct(tls, pWInfo)) } if (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy != 0 { (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FnOBSat = _sqlite3WhereIsOrdered(tls, pWInfo) (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FlabelOBLopt = _sqlite3WhereOrderByLimitOptLabel(tls, pWInfo) if (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FnOBSat == (*TExprList)(unsafe.Pointer((*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy)).FnExpr { (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy = uintptr(0) } } /* If sorting index that was created by a prior OP_OpenEphemeral ** instruction ended up not being needed, then change the OP_OpenEphemeral ** into an OP_Noop. */ if (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FaddrSortIndex >= 0 && (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy == uintptr(0) { _sqlite3VdbeChangeToNoop(tls, v, (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FaddrSortIndex) } _ = libc.Int32FromInt32(0) if pWin != 0 { addrGosub = _sqlite3VdbeMakeLabel(tls, pParse) iCont = _sqlite3VdbeMakeLabel(tls, pParse) iBreak = _sqlite3VdbeMakeLabel(tls, pParse) v26 = pParse + 44 *(*int32)(unsafe.Pointer(v26))++ v25 = *(*int32)(unsafe.Pointer(v26)) regGosub = v25 _sqlite3WindowCodeStep(tls, pParse, p, pWInfo, regGosub, addrGosub) _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, iBreak) _sqlite3VdbeResolveLabel(tls, v, addrGosub) (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FlabelOBLopt = 0 _selectInnerLoop(tls, pParse, p, -int32(1), bp+12, bp, pDest, iCont, iBreak) _sqlite3VdbeResolveLabel(tls, v, iCont) _sqlite3VdbeAddOp1(tls, v, int32(OP_Return), regGosub) _sqlite3VdbeResolveLabel(tls, v, iBreak) } else { /* Use the standard inner loop. */ _selectInnerLoop(tls, pParse, p, -int32(1), bp+12, bp, pDest, _sqlite3WhereContinueLabel(tls, pWInfo), _sqlite3WhereBreakLabel(tls, pWInfo)) /* End the database scan loop. */ _sqlite3WhereEnd(tls, pWInfo) } } else { /* End of processing for this SELECT */ sortPTab = 0 /* Pseudotable used to decode sorting results */ sortOut = 0 /* Output register from the sorter */ orderByGrp = 0 /* True if the GROUP BY and ORDER BY are the same */ /* Remove any and all aliases between the result set and the ** GROUP BY clause. */ if pGroupBy != 0 { /* For looping over expression in a list */ k = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr pItem2 = (*TSelect)(unsafe.Pointer(p)).FpEList + 8 for { if !(k > 0) { break } (*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem2 + 16))).FiAlias = uint16(0) goto _27 _27: ; k-- pItem2 += 20 } k = (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr pItem2 = pGroupBy + 8 for { if !(k > 0) { break } (*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem2 + 16))).FiAlias = uint16(0) goto _28 _28: ; k-- pItem2 += 20 } _ = libc.Int32FromInt32(0) if int32((*TSelect)(unsafe.Pointer(p)).FnSelectRow) > int32(66) { (*TSelect)(unsafe.Pointer(p)).FnSelectRow = int16(66) } /* If there is both a GROUP BY and an ORDER BY clause and they are ** identical, then it may be possible to disable the ORDER BY clause ** on the grounds that the GROUP BY will cause elements to come out ** in the correct order. It also may not - the GROUP BY might use a ** database index that causes rows to be grouped together as required ** but not actually sorted. Either way, record the fact that the ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp ** variable. */ if (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy != 0 && (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr == (*TExprList)(unsafe.Pointer((*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy)).FnExpr { /* The GROUP BY processing doesn't care whether rows are delivered in ** ASC or DESC order - only that each group is returned contiguously. ** So set the ASC/DESC flags in the GROUP BY to match those in the ** ORDER BY to maximize the chances of rows being delivered in an ** order that makes the ORDER BY redundant. */ ii1 = 0 for { if !(ii1 < (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr) { break } sortFlags = uint8(int32((*(*TExprList_item)(unsafe.Pointer((*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy + 8 + uintptr(ii1)*20))).Ffg.FsortFlags) & int32(KEYINFO_ORDER_DESC)) (*(*TExprList_item)(unsafe.Pointer(pGroupBy + 8 + uintptr(ii1)*20))).Ffg.FsortFlags = sortFlags goto _29 _29: ; ii1++ } if _sqlite3ExprListCompare(tls, pGroupBy, (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy, -int32(1)) == 0 { orderByGrp = int32(1) } } } else { _ = libc.Int32FromInt32(0) (*TSelect)(unsafe.Pointer(p)).FnSelectRow = 0 } /* Create a label to jump to when we want to abort the query */ addrEnd = _sqlite3VdbeMakeLabel(tls, pParse) /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the ** SELECT statement. */ pAggInfo = _sqlite3DbMallocZero(tls, db, uint64(44)) if pAggInfo != 0 { _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_agginfoFree), pAggInfo) } if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto select_end } (*TAggInfo)(unsafe.Pointer(pAggInfo)).FselId = (*TSelect)(unsafe.Pointer(p)).FselId libc.Xmemset(tls, bp+84, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp + 84))).FpParse = pParse (*(*TNameContext)(unsafe.Pointer(bp + 84))).FpSrcList = pTabList *(*uintptr)(unsafe.Pointer(bp + 84 + 8)) = pAggInfo if pGroupBy != 0 { v30 = (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr } else { v30 = 0 } (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnSortingColumn = uint16(v30) (*TAggInfo)(unsafe.Pointer(pAggInfo)).FpGroupBy = pGroupBy _sqlite3ExprAnalyzeAggList(tls, bp+84, pEList) _sqlite3ExprAnalyzeAggList(tls, bp+84, (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy) if pHaving != 0 { if pGroupBy != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _havingToWhere(tls, pParse, p) pWhere = (*TSelect)(unsafe.Pointer(p)).FpWhere } _sqlite3ExprAnalyzeAggregates(tls, bp+84, pHaving) } (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn if (*TSelect)(unsafe.Pointer(p)).FpGroupBy == uintptr(0) && (*TSelect)(unsafe.Pointer(p)).FpHaving == uintptr(0) && (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc == int32(1) { minMaxFlag = _minMaxQuery(tls, db, (*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFExpr, bp+52) } else { minMaxFlag = uint8(WHERE_ORDERBY_NORMAL) } _analyzeAggFuncArgs(tls, pAggInfo, bp+84) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto select_end } /* Processing for aggregates with GROUP BY is very different and ** much more complex than aggregates without a GROUP BY. */ if pGroupBy != 0 { /* Return address register for reset subroutine */ pDistinct = uintptr(0) distFlag = uint16(0) eDist = WHERE_DISTINCT_NOOP if (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc == int32(1) && (*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FiDistinct >= 0 && (*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFExpr != uintptr(0) && (*TExpr)(unsafe.Pointer((*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFExpr)).Fflags&uint32(EP_xIsSelect) == uint32(0) && *(*uintptr)(unsafe.Pointer((*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFExpr + 20)) != uintptr(0) { pExpr = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFExpr + 20)) + 8))).FpExpr pExpr = _sqlite3ExprDup(tls, db, pExpr, 0) pDistinct = _sqlite3ExprListDup(tls, db, pGroupBy, 0) pDistinct = _sqlite3ExprListAppend(tls, pParse, pDistinct, pExpr) if pDistinct != 0 { v31 = libc.Int32FromInt32(WHERE_WANT_DISTINCT) | libc.Int32FromInt32(WHERE_AGG_DISTINCT) } else { v31 = 0 } distFlag = uint16(v31) } /* If there is a GROUP BY clause we might need a sorting index to ** implement it. Allocate that sorting index now. If it turns out ** that we do not need it after all, the OP_SorterOpen instruction ** will be converted into a Noop. */ v33 = pParse + 40 v32 = *(*int32)(unsafe.Pointer(v33)) *(*int32)(unsafe.Pointer(v33))++ (*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdx = v32 pKeyInfo1 = _sqlite3KeyInfoFromExprList(tls, pParse, pGroupBy, 0, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn) addrSortingIdx = _sqlite3VdbeAddOp4(tls, v, int32(OP_SorterOpen), (*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdx, int32((*TAggInfo)(unsafe.Pointer(pAggInfo)).FnSortingColumn), 0, pKeyInfo1, -int32(8)) /* Initialize memory locations used by GROUP BY aggregate processing */ v35 = pParse + 44 *(*int32)(unsafe.Pointer(v35))++ v34 = *(*int32)(unsafe.Pointer(v35)) iUseFlag = v34 v37 = pParse + 44 *(*int32)(unsafe.Pointer(v37))++ v36 = *(*int32)(unsafe.Pointer(v37)) iAbortFlag = v36 v39 = pParse + 44 *(*int32)(unsafe.Pointer(v39))++ v38 = *(*int32)(unsafe.Pointer(v39)) regOutputRow = v38 addrOutputRow = _sqlite3VdbeMakeLabel(tls, pParse) v41 = pParse + 44 *(*int32)(unsafe.Pointer(v41))++ v40 = *(*int32)(unsafe.Pointer(v41)) regReset = v40 addrReset = _sqlite3VdbeMakeLabel(tls, pParse) iAMem = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr iBMem = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, iAbortFlag) _sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, iAMem, iAMem+(*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr-int32(1)) /* Begin a loop that will extract all source rows in GROUP BY order. ** This might involve two separate loops with an OP_Sort in between, or ** it might be a single loop that uses an index to extract information ** in the right order to begin with. */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regReset, addrReset) if int32((*(*TDistinctCtx)(unsafe.Pointer(bp))).FisTnct) == int32(2) { v42 = int32(WHERE_DISTINCTBY) } else { v42 = int32(WHERE_GROUPBY) } if orderByGrp != 0 { v43 = int32(WHERE_SORTBYGROUP) } else { v43 = 0 } pWInfo = _sqlite3WhereBegin(tls, pParse, pTabList, pWhere, pGroupBy, pDistinct, p, uint16(v42|v43|int32(distFlag)), 0) if pWInfo == uintptr(0) { _sqlite3ExprListDelete(tls, db, pDistinct) goto select_end } if (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr != 0 { _optimizeAggregateUseOfIndexedExpr(tls, pParse, p, pAggInfo, bp+84) } _assignAggregateRegisters(tls, pParse, pAggInfo) eDist = _sqlite3WhereIsDistinct(tls, pWInfo) if _sqlite3WhereIsOrdered(tls, pWInfo) == (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr { /* The optimizer is able to deliver rows in group by order so ** we do not have to sort. The OP_OpenEphemeral table will be ** cancelled later because we still need to use the pKeyInfo */ groupBySort = 0 } else { if (*(*TDistinctCtx)(unsafe.Pointer(bp))).FisTnct != 0 && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Distinct) == uint32(0) { v44 = __ccgo_ts + 20250 } else { v44 = __ccgo_ts + 20259 } _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+19147, libc.VaList(bp+128, v44)) groupBySort = int32(1) nGroupBy = (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr nCol = nGroupBy j = nGroupBy i = 0 for { if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn) { break } if int32((*(*TAggInfo_col)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr(i)*16))).FiSorterColumn) >= j { nCol++ j++ } goto _45 _45: ; i++ } regBase = _sqlite3GetTempRange(tls, pParse, nCol) _sqlite3ExprCodeExprList(tls, pParse, pGroupBy, regBase, 0, uint8(0)) j = nGroupBy (*TAggInfo)(unsafe.Pointer(pAggInfo)).FdirectMode = uint8(1) i = 0 for { if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn) { break } pCol = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr(i)*16 if int32((*TAggInfo_col)(unsafe.Pointer(pCol)).FiSorterColumn) >= j { _sqlite3ExprCode(tls, pParse, (*TAggInfo_col)(unsafe.Pointer(pCol)).FpCExpr, j+regBase) j++ } goto _46 _46: ; i++ } (*TAggInfo)(unsafe.Pointer(pAggInfo)).FdirectMode = uint8(0) regRecord = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regBase, nCol, regRecord) _sqlite3VdbeAddOp2(tls, v, int32(OP_SorterInsert), (*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdx, regRecord) _sqlite3ReleaseTempReg(tls, pParse, regRecord) _sqlite3ReleaseTempRange(tls, pParse, regBase, nCol) _sqlite3WhereEnd(tls, pWInfo) v49 = pParse + 40 v48 = *(*int32)(unsafe.Pointer(v49)) *(*int32)(unsafe.Pointer(v49))++ v47 = v48 sortPTab = v47 (*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdxPTab = v47 sortOut = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp3(tls, v, int32(OP_OpenPseudo), sortPTab, sortOut, nCol) _sqlite3VdbeAddOp2(tls, v, int32(OP_SorterSort), (*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdx, addrEnd) (*TAggInfo)(unsafe.Pointer(pAggInfo)).FuseSortingIdx = uint8(1) } /* If there are entries in pAgggInfo->aFunc[] that contain subexpressions ** that are indexed (and that were previously identified and tagged ** in optimizeAggregateUseOfIndexedExpr()) then those subexpressions ** must now be converted into a TK_AGG_COLUMN node so that the value ** is correctly pulled from the index rather than being recomputed. */ if (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr != 0 { _aggregateConvertIndexedExprRefToColumn(tls, pAggInfo) } /* If the index or temporary table used by the GROUP BY sort ** will naturally deliver rows in the order required by the ORDER BY ** clause, cancel the ephemeral table open coded earlier. ** ** This is an optimization - the correct answer should result regardless. ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to ** disable this optimization for testing purposes. */ if orderByGrp != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_GroupByOrder)) == uint32(0) && (groupBySort != 0 || _sqlite3WhereIsSorted(tls, pWInfo) != 0) { (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy = uintptr(0) _sqlite3VdbeChangeToNoop(tls, v, (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FaddrSortIndex) } /* Evaluate the current GROUP BY terms and store in b0, b1, b2... ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth) ** Then compare the current GROUP BY terms against the GROUP BY terms ** from the previous row currently stored in a0, a1, a2... */ addrTopOfLoop = _sqlite3VdbeCurrentAddr(tls, v) if groupBySort != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_SorterData), (*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdx, sortOut, sortPTab) } j = 0 for { if !(j < (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr) { break } if groupBySort != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), sortPTab, j, iBMem+j) } else { (*TAggInfo)(unsafe.Pointer(pAggInfo)).FdirectMode = uint8(1) _sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pGroupBy + 8 + uintptr(j)*20))).FpExpr, iBMem+j) } goto _50 _50: ; j++ } _sqlite3VdbeAddOp4(tls, v, int32(OP_Compare), iAMem, iBMem, (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr, _sqlite3KeyInfoRef(tls, pKeyInfo1), -int32(8)) addr1 = _sqlite3VdbeCurrentAddr(tls, v) _sqlite3VdbeAddOp3(tls, v, int32(OP_Jump), addr1+int32(1), 0, addr1+int32(1)) /* Generate code that runs whenever the GROUP BY changes. ** Changes in the GROUP BY are detected by the previous code ** block. If there were no changes, this block is skipped. ** ** This code copies current group by terms in b0,b1,b2,... ** over to a0,a1,a2. It then calls the output subroutine ** and resets the aggregate accumulator registers in preparation ** for the next GROUP BY batch. */ _sqlite3ExprCodeMove(tls, pParse, iBMem, iAMem, (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr) _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regOutputRow, addrOutputRow) _sqlite3VdbeAddOp2(tls, v, int32(OP_IfPos), iAbortFlag, addrEnd) _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regReset, addrReset) /* Update the aggregate accumulators based on the content of ** the current row */ _sqlite3VdbeJumpHere(tls, v, addr1) _updateAccumulator(tls, pParse, iUseFlag, pAggInfo, eDist) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), iUseFlag) /* End of the loop */ if groupBySort != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_SorterNext), (*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdx, addrTopOfLoop) } else { _sqlite3WhereEnd(tls, pWInfo) _sqlite3VdbeChangeToNoop(tls, v, addrSortingIdx) } _sqlite3ExprListDelete(tls, db, pDistinct) /* Output the final row of result */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regOutputRow, addrOutputRow) /* Jump over the subroutines */ _sqlite3VdbeGoto(tls, v, addrEnd) /* Generate a subroutine that outputs a single row of the result ** set. This subroutine first looks at the iUseFlag. If iUseFlag ** is less than or equal to zero, the subroutine is a no-op. If ** the processing calls for the query to abort, this subroutine ** increments the iAbortFlag memory location before returning in ** order to signal the caller to abort. */ addrSetAbort = _sqlite3VdbeCurrentAddr(tls, v) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), iAbortFlag) _sqlite3VdbeAddOp1(tls, v, int32(OP_Return), regOutputRow) _sqlite3VdbeResolveLabel(tls, v, addrOutputRow) addrOutputRow = _sqlite3VdbeCurrentAddr(tls, v) _sqlite3VdbeAddOp2(tls, v, int32(OP_IfPos), iUseFlag, addrOutputRow+int32(2)) _sqlite3VdbeAddOp1(tls, v, int32(OP_Return), regOutputRow) _finalizeAggFunctions(tls, pParse, pAggInfo) _sqlite3ExprIfFalse(tls, pParse, pHaving, addrOutputRow+int32(1), int32(SQLITE_JUMPIFNULL)) _selectInnerLoop(tls, pParse, p, -int32(1), bp+12, bp, pDest, addrOutputRow+int32(1), addrSetAbort) _sqlite3VdbeAddOp1(tls, v, int32(OP_Return), regOutputRow) /* Generate a subroutine that will reset the group-by accumulator */ _sqlite3VdbeResolveLabel(tls, v, addrReset) _resetAccumulator(tls, pParse, pAggInfo) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, iUseFlag) _sqlite3VdbeAddOp1(tls, v, int32(OP_Return), regReset) if int32(distFlag) != 0 && eDist != WHERE_DISTINCT_NOOP { pF = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc _fixDistinctOpenEph(tls, pParse, eDist, (*TAggInfo_func)(unsafe.Pointer(pF)).FiDistinct, (*TAggInfo_func)(unsafe.Pointer(pF)).FiDistAddr) } } else { v51 = _isSimpleCount(tls, p, pAggInfo) pTab1 = v51 if v51 != uintptr(0) { /* If isSimpleCount() returns a pointer to a Table structure, then ** the SQL statement is of the form: ** ** SELECT count(*) FROM ** ** where the Table structure returned represents table . ** ** This statement is so common that it is optimized specially. The ** OP_Count instruction is executed either on the intkey table that ** contains the data for table or on one of its indexes. It ** is better to execute the op on an index, as indexes are almost ** always spread across less pages than their corresponding tables. */ iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTable)(unsafe.Pointer(pTab1)).FpSchema) v53 = pParse + 40 v52 = *(*int32)(unsafe.Pointer(v53)) *(*int32)(unsafe.Pointer(v53))++ iCsr = v52 /* Iterator variable */ pKeyInfo2 = uintptr(0) /* Keyinfo for scanned index */ pBest = uintptr(0) /* Best index found so far */ iRoot = (*TTable)(unsafe.Pointer(pTab1)).Ftnum /* Root page of scanned b-tree */ _sqlite3CodeVerifySchema(tls, pParse, iDb) _sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab1)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pTab1)).FzName) /* Search for the index that has the lowest scan cost. ** ** (2011-04-15) Do not do a full scan of an unordered index. ** ** (2013-10-03) Do not count the entries in a partial index. ** ** In practice the KeyInfo structure will not be used. It is only ** passed to keep OP_OpenRead happy. */ if !((*TTable)(unsafe.Pointer(pTab1)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { pBest = _sqlite3PrimaryKeyIndex(tls, pTab1) } if !(int32(uint32(*(*uint16)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8 + 36 + 4))&0x1>>0)) != 0) { pIdx = (*TTable)(unsafe.Pointer(pTab1)).FpIndex for { if !(pIdx != 0) { break } if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x4>>2)) == 0 && int32((*TIndex)(unsafe.Pointer(pIdx)).FszIdxRow) < int32((*TTable)(unsafe.Pointer(pTab1)).FszTabRow) && (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere == uintptr(0) && (!(pBest != 0) || int32((*TIndex)(unsafe.Pointer(pIdx)).FszIdxRow) < int32((*TIndex)(unsafe.Pointer(pBest)).FszIdxRow)) { pBest = pIdx } goto _54 _54: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } } if pBest != 0 { iRoot = (*TIndex)(unsafe.Pointer(pBest)).Ftnum pKeyInfo2 = _sqlite3KeyInfoOfIndex(tls, pParse, pBest) } /* Open a read-only cursor, execute the OP_Count, close the cursor. */ _sqlite3VdbeAddOp4Int(tls, v, int32(OP_OpenRead), iCsr, int32(iRoot), iDb, int32(1)) if pKeyInfo2 != 0 { _sqlite3VdbeChangeP4(tls, v, -int32(1), pKeyInfo2, -int32(8)) } _assignAggregateRegisters(tls, pParse, pAggInfo) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Count), iCsr, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg+(*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn+libc.Int32FromInt32(0)) _sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iCsr) _explainSimpleCount(tls, pParse, pTab1, pBest) } else { regAcc = 0 /* "populate accumulators" flag */ pDistinct1 = uintptr(0) distFlag1 = uint16(0) /* If there are accumulator registers but no min() or max() functions ** without FILTER clauses, allocate register regAcc. Register regAcc ** will contain 0 the first time the inner loop runs, and 1 thereafter. ** The code generated by updateAccumulator() uses this to ensure ** that the accumulator registers are (a) updated only once if ** there are no min() or max functions or (b) always updated for the ** first row visited by the aggregate, so that they are updated at ** least once even if the FILTER clause means the min() or max() ** function visits zero rows. */ if (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator != 0 { i = 0 for { if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc) { break } if (*TExpr)(unsafe.Pointer((*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(i)*24))).FpFExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { goto _55 } if (*TFuncDef)(unsafe.Pointer((*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(i)*24))).FpFunc)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) != 0 { break } goto _55 _55: ; i++ } if i == (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc { v57 = pParse + 44 *(*int32)(unsafe.Pointer(v57))++ v56 = *(*int32)(unsafe.Pointer(v57)) regAcc = v56 _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regAcc) } } else { if (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc == int32(1) && (*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FiDistinct >= 0 { _ = libc.Int32FromInt32(0) pDistinct1 = *(*uintptr)(unsafe.Pointer((*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFExpr + 20)) if pDistinct1 != 0 { v58 = libc.Int32FromInt32(WHERE_WANT_DISTINCT) | libc.Int32FromInt32(WHERE_AGG_DISTINCT) } else { v58 = 0 } distFlag1 = uint16(v58) } } _assignAggregateRegisters(tls, pParse, pAggInfo) /* This case runs if the aggregate has no GROUP BY clause. The ** processing is much simpler since there is only a single row ** of output. */ _ = libc.Int32FromInt32(0) _resetAccumulator(tls, pParse, pAggInfo) /* If this query is a candidate for the min/max optimization, then ** minMaxFlag will have been previously set to either ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will ** be an appropriate ORDER BY expression for the optimization. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pWInfo = _sqlite3WhereBegin(tls, pParse, pTabList, pWhere, *(*uintptr)(unsafe.Pointer(bp + 52)), pDistinct1, p, uint16(int32(minMaxFlag)|int32(distFlag1)), 0) if pWInfo == uintptr(0) { goto select_end } eDist1 = _sqlite3WhereIsDistinct(tls, pWInfo) _updateAccumulator(tls, pParse, regAcc, pAggInfo, eDist1) if eDist1 != WHERE_DISTINCT_NOOP { pF1 = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc if pF1 != 0 { _fixDistinctOpenEph(tls, pParse, eDist1, (*TAggInfo_func)(unsafe.Pointer(pF1)).FiDistinct, (*TAggInfo_func)(unsafe.Pointer(pF1)).FiDistAddr) } } if regAcc != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), regAcc) } if minMaxFlag != 0 { _sqlite3WhereMinMaxOptEarlyOut(tls, v, pWInfo) } _sqlite3WhereEnd(tls, pWInfo) _finalizeAggFunctions(tls, pParse, pAggInfo) } (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy = uintptr(0) _sqlite3ExprIfFalse(tls, pParse, pHaving, addrEnd, int32(SQLITE_JUMPIFNULL)) _selectInnerLoop(tls, pParse, p, -int32(1), uintptr(0), uintptr(0), pDest, addrEnd, addrEnd) } _sqlite3VdbeResolveLabel(tls, v, addrEnd) } /* endif aggregate query */ if int32((*(*TDistinctCtx)(unsafe.Pointer(bp))).FeTnctType) == int32(WHERE_DISTINCT_UNORDERED) { _explainTempTable(tls, pParse, __ccgo_ts+20250) } /* If there is an ORDER BY clause, then we need to sort the results ** and send them to the callback one by one. */ if (*(*TSortCtx)(unsafe.Pointer(bp + 12))).FpOrderBy != 0 { _ = libc.Int32FromInt32(0) _generateSortTail(tls, pParse, p, bp+12, (*TExprList)(unsafe.Pointer(pEList)).FnExpr, pDest) } /* Jump here to skip this query */ _sqlite3VdbeResolveLabel(tls, v, iEnd) /* The SELECT has been coded. If there is an error in the Parse structure, ** set the return code to 1. Otherwise 0. */ rc = libc.BoolInt32((*TParse)(unsafe.Pointer(pParse)).FnErr > 0) /* Control jumps to here if an error is encountered above, or upon ** successful coding of the SELECT. */ goto select_end select_end: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3ExprListDelete(tls, db, *(*uintptr)(unsafe.Pointer(bp + 52))) _sqlite3VdbeExplainPop(tls, pParse) return rc } /************** End of select.c **********************************************/ /************** Begin file table.c *******************************************/ /* ** 2001 September 15 ** ** 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 contains the sqlite3_get_table() and sqlite3_free_table() ** interface routines. These are just wrappers around the main ** interface routine of sqlite3_exec(). ** ** These routines are in a separate files so that they will not be linked ** if they are not used. */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** This structure is used to pass data from sqlite3_get_table() through // ** to the callback function is uses to build the result. // */ type TTabResult = struct { FazResult uintptr FzErrMsg uintptr FnAlloc Tu32 FnRow Tu32 FnColumn Tu32 FnData Tu32 Frc int32 } type TabResult = TTabResult // C documentation // // /* // ** This routine is called once for each row in the result table. Its job // ** is to fill in the TabResult structure appropriately, allocating new // ** memory as necessary. // */ func _sqlite3_get_table_cb(tls *libc.TLS, pArg uintptr, nCol int32, argv uintptr, colv uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var azNew, p, z, v3, v6 uintptr var i, n, need int32 var v2, v5 Tu32 _, _, _, _, _, _, _, _, _, _ = azNew, i, n, need, p, z, v2, v3, v5, v6 p = pArg /* A single column of result */ /* Make sure there is enough space in p->azResult to hold everything ** we need to remember from this invocation of the callback. */ if (*TTabResult)(unsafe.Pointer(p)).FnRow == uint32(0) && argv != uintptr(0) { need = nCol * int32(2) } else { need = nCol } if (*TTabResult)(unsafe.Pointer(p)).FnData+uint32(need) > (*TTabResult)(unsafe.Pointer(p)).FnAlloc { (*TTabResult)(unsafe.Pointer(p)).FnAlloc = (*TTabResult)(unsafe.Pointer(p)).FnAlloc*uint32(2) + uint32(need) azNew = _sqlite3Realloc(tls, (*TTabResult)(unsafe.Pointer(p)).FazResult, uint64(uint32(4)*(*TTabResult)(unsafe.Pointer(p)).FnAlloc)) if azNew == uintptr(0) { goto malloc_failed } (*TTabResult)(unsafe.Pointer(p)).FazResult = azNew } /* If this is the first row, then generate an extra row containing ** the names of all columns. */ if (*TTabResult)(unsafe.Pointer(p)).FnRow == uint32(0) { (*TTabResult)(unsafe.Pointer(p)).FnColumn = uint32(nCol) i = 0 for { if !(i < nCol) { break } z = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+8, *(*uintptr)(unsafe.Pointer(colv + uintptr(i)*4)))) if z == uintptr(0) { goto malloc_failed } v3 = p + 20 v2 = *(*Tu32)(unsafe.Pointer(v3)) *(*Tu32)(unsafe.Pointer(v3))++ *(*uintptr)(unsafe.Pointer((*TTabResult)(unsafe.Pointer(p)).FazResult + uintptr(v2)*4)) = z goto _1 _1: ; i++ } } else { if int32((*TTabResult)(unsafe.Pointer(p)).FnColumn) != nCol { Xsqlite3_free(tls, (*TTabResult)(unsafe.Pointer(p)).FzErrMsg) (*TTabResult)(unsafe.Pointer(p)).FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+20268, 0) (*TTabResult)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR) return int32(1) } } /* Copy over the row data */ if argv != uintptr(0) { i = 0 for { if !(i < nCol) { break } if *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4)) == uintptr(0) { z = uintptr(0) } else { n = _sqlite3Strlen30(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) + int32(1) z = Xsqlite3_malloc64(tls, uint64(n)) if z == uintptr(0) { goto malloc_failed } libc.Xmemcpy(tls, z, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4)), uint32(n)) } v6 = p + 20 v5 = *(*Tu32)(unsafe.Pointer(v6)) *(*Tu32)(unsafe.Pointer(v6))++ *(*uintptr)(unsafe.Pointer((*TTabResult)(unsafe.Pointer(p)).FazResult + uintptr(v5)*4)) = z goto _4 _4: ; i++ } (*TTabResult)(unsafe.Pointer(p)).FnRow++ } return 0 goto malloc_failed malloc_failed: ; (*TTabResult)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) return int32(1) } // C documentation // // /* // ** Query the database. But instead of invoking a callback for each row, // ** malloc() for space to hold the result and return the entire results // ** at the conclusion of the call. // ** // ** The result that is written to ***pazResult is held in memory obtained // ** from malloc(). But the caller cannot free this memory directly. // ** Instead, the entire table should be passed to sqlite3_free_table() when // ** the calling procedure is finished using it. // */ func Xsqlite3_get_table(tls *libc.TLS, db uintptr, zSql uintptr, pazResult uintptr, pnRow uintptr, pnColumn uintptr, pzErrMsg uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var azNew uintptr var rc int32 var _ /* res at bp+0 */ TTabResult _, _ = azNew, rc *(*uintptr)(unsafe.Pointer(pazResult)) = uintptr(0) if pnColumn != 0 { *(*int32)(unsafe.Pointer(pnColumn)) = 0 } if pnRow != 0 { *(*int32)(unsafe.Pointer(pnRow)) = 0 } if pzErrMsg != 0 { *(*uintptr)(unsafe.Pointer(pzErrMsg)) = uintptr(0) } (*(*TTabResult)(unsafe.Pointer(bp))).FzErrMsg = uintptr(0) (*(*TTabResult)(unsafe.Pointer(bp))).FnRow = uint32(0) (*(*TTabResult)(unsafe.Pointer(bp))).FnColumn = uint32(0) (*(*TTabResult)(unsafe.Pointer(bp))).FnData = uint32(1) (*(*TTabResult)(unsafe.Pointer(bp))).FnAlloc = uint32(20) (*(*TTabResult)(unsafe.Pointer(bp))).Frc = SQLITE_OK (*(*TTabResult)(unsafe.Pointer(bp))).FazResult = Xsqlite3_malloc64(tls, uint64(uint32(4)*(*(*TTabResult)(unsafe.Pointer(bp))).FnAlloc)) if (*(*TTabResult)(unsafe.Pointer(bp))).FazResult == uintptr(0) { (*Tsqlite3)(unsafe.Pointer(db)).FerrCode = int32(SQLITE_NOMEM) return int32(SQLITE_NOMEM) } *(*uintptr)(unsafe.Pointer((*(*TTabResult)(unsafe.Pointer(bp))).FazResult)) = uintptr(0) rc = Xsqlite3_exec(tls, db, zSql, __ccgo_fp(_sqlite3_get_table_cb), bp, pzErrMsg) _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer((*(*TTabResult)(unsafe.Pointer(bp))).FazResult)) = uintptr(int32((*(*TTabResult)(unsafe.Pointer(bp))).FnData)) if rc&int32(0xff) == int32(SQLITE_ABORT) { Xsqlite3_free_table(tls, (*(*TTabResult)(unsafe.Pointer(bp))).FazResult+1*4) if (*(*TTabResult)(unsafe.Pointer(bp))).FzErrMsg != 0 { if pzErrMsg != 0 { Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(pzErrMsg))) *(*uintptr)(unsafe.Pointer(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+40, (*(*TTabResult)(unsafe.Pointer(bp))).FzErrMsg)) } Xsqlite3_free(tls, (*(*TTabResult)(unsafe.Pointer(bp))).FzErrMsg) } (*Tsqlite3)(unsafe.Pointer(db)).FerrCode = (*(*TTabResult)(unsafe.Pointer(bp))).Frc /* Assume 32-bit assignment is atomic */ return (*(*TTabResult)(unsafe.Pointer(bp))).Frc } Xsqlite3_free(tls, (*(*TTabResult)(unsafe.Pointer(bp))).FzErrMsg) if rc != SQLITE_OK { Xsqlite3_free_table(tls, (*(*TTabResult)(unsafe.Pointer(bp))).FazResult+1*4) return rc } if (*(*TTabResult)(unsafe.Pointer(bp))).FnAlloc > (*(*TTabResult)(unsafe.Pointer(bp))).FnData { azNew = _sqlite3Realloc(tls, (*(*TTabResult)(unsafe.Pointer(bp))).FazResult, uint64(uint32(4)*(*(*TTabResult)(unsafe.Pointer(bp))).FnData)) if azNew == uintptr(0) { Xsqlite3_free_table(tls, (*(*TTabResult)(unsafe.Pointer(bp))).FazResult+1*4) (*Tsqlite3)(unsafe.Pointer(db)).FerrCode = int32(SQLITE_NOMEM) return int32(SQLITE_NOMEM) } (*(*TTabResult)(unsafe.Pointer(bp))).FazResult = azNew } *(*uintptr)(unsafe.Pointer(pazResult)) = (*(*TTabResult)(unsafe.Pointer(bp))).FazResult + 1*4 if pnColumn != 0 { *(*int32)(unsafe.Pointer(pnColumn)) = int32((*(*TTabResult)(unsafe.Pointer(bp))).FnColumn) } if pnRow != 0 { *(*int32)(unsafe.Pointer(pnRow)) = int32((*(*TTabResult)(unsafe.Pointer(bp))).FnRow) } return rc } // C documentation // // /* // ** This routine frees the space the sqlite3_get_table() malloced. // */ func Xsqlite3_free_table(tls *libc.TLS, azResult uintptr) { var i, n int32 _, _ = i, n if azResult != 0 { azResult -= 4 _ = libc.Int32FromInt32(0) n = int32(*(*uintptr)(unsafe.Pointer(azResult))) i = int32(1) for { if !(i < n) { break } if *(*uintptr)(unsafe.Pointer(azResult + uintptr(i)*4)) != 0 { Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(azResult + uintptr(i)*4))) } goto _1 _1: ; i++ } Xsqlite3_free(tls, azResult) } } /************** End of table.c ***********************************************/ /************** Begin file trigger.c *****************************************/ /* ** ** 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 contains the implementation for TRIGGERs */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** Delete a linked list of TriggerStep structures. // */ func _sqlite3DeleteTriggerStep(tls *libc.TLS, db uintptr, pTriggerStep uintptr) { var pTmp uintptr _ = pTmp for pTriggerStep != 0 { pTmp = pTriggerStep pTriggerStep = (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpNext _sqlite3ExprDelete(tls, db, (*TTriggerStep)(unsafe.Pointer(pTmp)).FpWhere) _sqlite3ExprListDelete(tls, db, (*TTriggerStep)(unsafe.Pointer(pTmp)).FpExprList) _sqlite3SelectDelete(tls, db, (*TTriggerStep)(unsafe.Pointer(pTmp)).FpSelect) _sqlite3IdListDelete(tls, db, (*TTriggerStep)(unsafe.Pointer(pTmp)).FpIdList) _sqlite3UpsertDelete(tls, db, (*TTriggerStep)(unsafe.Pointer(pTmp)).FpUpsert) _sqlite3SrcListDelete(tls, db, (*TTriggerStep)(unsafe.Pointer(pTmp)).FpFrom) _sqlite3DbFree(tls, db, (*TTriggerStep)(unsafe.Pointer(pTmp)).FzSpan) _sqlite3DbFree(tls, db, pTmp) } } // C documentation // // /* // ** Given table pTab, return a list of all the triggers attached to // ** the table. The list is connected by Trigger.pNext pointers. // ** // ** All of the triggers on pTab that are in the same database as pTab // ** are already attached to pTab->pTrigger. But there might be additional // ** triggers on pTab in the TEMP schema. This routine prepends all // ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list // ** and returns the combined list. // ** // ** To state it another way: This routine returns a list of all triggers // ** that fire off of pTab. The list will include any TEMP triggers on // ** pTab as well as the triggers lised in pTab->pTrigger. // */ func _sqlite3TriggerList(tls *libc.TLS, pParse uintptr, pTab uintptr) (r uintptr) { var p, pList, pTmpSchema, pTrig uintptr _, _, _, _ = p, pList, pTmpSchema, pTrig /* Loop variable for TEMP triggers */ _ = libc.Int32FromInt32(0) pTmpSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + 1*16))).FpSchema p = (*THash)(unsafe.Pointer(pTmpSchema + 40)).Ffirst pList = (*TTable)(unsafe.Pointer(pTab)).FpTrigger for p != 0 { pTrig = (*THashElem)(unsafe.Pointer(p)).Fdata if (*TTrigger)(unsafe.Pointer(pTrig)).FpTabSchema == (*TTable)(unsafe.Pointer(pTab)).FpSchema && (*TTrigger)(unsafe.Pointer(pTrig)).Ftable != 0 && 0 == _sqlite3StrICmp(tls, (*TTrigger)(unsafe.Pointer(pTrig)).Ftable, (*TTable)(unsafe.Pointer(pTab)).FzName) && ((*TTrigger)(unsafe.Pointer(pTrig)).FpTabSchema != pTmpSchema || (*TTrigger)(unsafe.Pointer(pTrig)).FbReturning != 0) { (*TTrigger)(unsafe.Pointer(pTrig)).FpNext = pList pList = pTrig } else { if int32((*TTrigger)(unsafe.Pointer(pTrig)).Fop) == int32(TK_RETURNING) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TTrigger)(unsafe.Pointer(pTrig)).Ftable = (*TTable)(unsafe.Pointer(pTab)).FzName (*TTrigger)(unsafe.Pointer(pTrig)).FpTabSchema = (*TTable)(unsafe.Pointer(pTab)).FpSchema (*TTrigger)(unsafe.Pointer(pTrig)).FpNext = pList pList = pTrig } } p = (*THashElem)(unsafe.Pointer(p)).Fnext } return pList } // C documentation // // /* // ** This is called by the parser when it sees a CREATE TRIGGER statement // ** up to the point of the BEGIN before the trigger actions. A Trigger // ** structure is generated based on the information available and stored // ** in pParse->pNewTrigger. After the trigger actions have been parsed, the // ** sqlite3FinishTrigger() function is called to complete the trigger // ** construction process. // */ func _sqlite3BeginTrigger(tls *libc.TLS, pParse uintptr, pName1 uintptr, pName2 uintptr, tr_tm int32, op int32, pColumns uintptr, pTableName uintptr, pWhen uintptr, isTemp int32, noErr int32) { bp := tls.Alloc(80) defer tls.Free(80) var code, iDb, iTabDb, v4 int32 var db, pTab, pTrigger, zDb, zDbTrig, zName, v1, v2, v3 uintptr var _ /* pName at bp+0 */ uintptr var _ /* sFix at bp+4 */ TDbFixer _, _, _, _, _, _, _, _, _, _, _, _, _ = code, db, iDb, iTabDb, pTab, pTrigger, zDb, zDbTrig, zName, v1, v2, v3, v4 pTrigger = uintptr(0) /* Table that the trigger fires off of */ zName = uintptr(0) /* Name of the trigger */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* State vector for the DB fixer */ _ = libc.Int32FromInt32(0) /* pName1->z might be NULL, but not pName1 itself */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if isTemp != 0 { /* If TEMP was specified, then the trigger name may not be qualified. */ if (*TToken)(unsafe.Pointer(pName2)).Fn > uint32(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20333, 0) goto trigger_cleanup } iDb = int32(1) *(*uintptr)(unsafe.Pointer(bp)) = pName1 } else { /* Figure out the db that the trigger will be created in */ iDb = _sqlite3TwoPartName(tls, pParse, pName1, pName2, bp) if iDb < 0 { goto trigger_cleanup } } if !(pTableName != 0) || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto trigger_cleanup } /* A long-standing parser bug is that this syntax was allowed: ** ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab .... ** ^^^^^^^^ ** ** To maintain backwards compatibility, ignore the database ** name on pTableName if we are reparsing out of the schema table */ if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 && iDb != int32(1) { _sqlite3DbFree(tls, db, (*(*TSrcItem)(unsafe.Pointer(pTableName + 8))).FzDatabase) (*(*TSrcItem)(unsafe.Pointer(pTableName + 8))).FzDatabase = uintptr(0) } /* If the trigger name was unqualified, and the table is a temp table, ** then set iDb to 1 to create the trigger in the temporary database. ** If sqlite3SrcListLookup() returns 0, indicating the table does not ** exist, the error is caught by the block below. */ pTab = _sqlite3SrcListLookup(tls, pParse, pTableName) if int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) == 0 && (*TToken)(unsafe.Pointer(pName2)).Fn == uint32(0) && pTab != 0 && (*TTable)(unsafe.Pointer(pTab)).FpSchema == (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema { iDb = int32(1) } /* Ensure the table name matches database name and that the table exists */ if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto trigger_cleanup } _ = libc.Int32FromInt32(0) _sqlite3FixInit(tls, bp+4, pParse, iDb, __ccgo_ts+20379, *(*uintptr)(unsafe.Pointer(bp))) if _sqlite3FixSrcList(tls, bp+4, pTableName) != 0 { goto trigger_cleanup } pTab = _sqlite3SrcListLookup(tls, pParse, pTableName) if !(pTab != 0) { /* The table does not exist. */ goto trigger_orphan_error } if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20387, 0) goto trigger_orphan_error } if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Shadow) != uint32(0) && _sqlite3ReadOnlyShadowTables(tls, db) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20428, 0) goto trigger_orphan_error } /* Check that the trigger name is not reserved and that no trigger of the ** specified name exists */ zName = _sqlite3NameFromToken(tls, db, *(*uintptr)(unsafe.Pointer(bp))) if zName == uintptr(0) { _ = libc.Int32FromInt32(0) goto trigger_cleanup } if _sqlite3CheckObjectName(tls, pParse, zName, __ccgo_ts+20379, (*TTable)(unsafe.Pointer(pTab)).FzName) != 0 { goto trigger_cleanup } _ = libc.Int32FromInt32(0) if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) { if _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema+40, zName) != 0 { if !(noErr != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20468, libc.VaList(bp+64, *(*uintptr)(unsafe.Pointer(bp)))) } else { _ = libc.Int32FromInt32(0) _sqlite3CodeVerifySchema(tls, pParse, iDb) } goto trigger_cleanup } } /* Do not create a trigger on a system table */ if Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName, __ccgo_ts+6525, int32(7)) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20494, 0) goto trigger_cleanup } /* INSTEAD of triggers are only for views and views only support INSTEAD ** of triggers. */ if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) && tr_tm != int32(TK_INSTEAD) { if tr_tm == int32(TK_BEFORE) { v1 = __ccgo_ts + 20532 } else { v1 = __ccgo_ts + 20539 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20545, libc.VaList(bp+64, v1, pTableName+8)) goto trigger_orphan_error } if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VIEW)) && tr_tm == int32(TK_INSTEAD) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20582, libc.VaList(bp+64, pTableName+8)) goto trigger_orphan_error } if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) { iTabDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) code = int32(SQLITE_CREATE_TRIGGER) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iTabDb)*16))).FzDbSName if isTemp != 0 { v2 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FzDbSName } else { v2 = zDb } zDbTrig = v2 if iTabDb == int32(1) || isTemp != 0 { code = int32(SQLITE_CREATE_TEMP_TRIGGER) } if _sqlite3AuthCheck(tls, pParse, code, zName, (*TTable)(unsafe.Pointer(pTab)).FzName, zDbTrig) != 0 { goto trigger_cleanup } if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iTabDb == int32(1) { v3 = __ccgo_ts + 6533 } else { v3 = __ccgo_ts + 6066 } if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_INSERT), v3, uintptr(0), zDb) != 0 { goto trigger_cleanup } } /* INSTEAD OF triggers can only appear on views and BEFORE triggers ** cannot appear on views. So we might as well translate every ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code ** elsewhere. */ if tr_tm == int32(TK_INSTEAD) { tr_tm = int32(TK_BEFORE) } /* Build the Trigger object */ pTrigger = _sqlite3DbMallocZero(tls, db, uint64(36)) if pTrigger == uintptr(0) { goto trigger_cleanup } (*TTrigger)(unsafe.Pointer(pTrigger)).FzName = zName zName = uintptr(0) (*TTrigger)(unsafe.Pointer(pTrigger)).Ftable = _sqlite3DbStrDup(tls, db, (*(*TSrcItem)(unsafe.Pointer(pTableName + 8))).FzName) (*TTrigger)(unsafe.Pointer(pTrigger)).FpSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema (*TTrigger)(unsafe.Pointer(pTrigger)).FpTabSchema = (*TTable)(unsafe.Pointer(pTab)).FpSchema (*TTrigger)(unsafe.Pointer(pTrigger)).Fop = uint8(op) if tr_tm == int32(TK_BEFORE) { v4 = int32(TRIGGER_BEFORE) } else { v4 = int32(TRIGGER_AFTER) } (*TTrigger)(unsafe.Pointer(pTrigger)).Ftr_tm = uint8(v4) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenRemap(tls, pParse, (*TTrigger)(unsafe.Pointer(pTrigger)).Ftable, (*(*TSrcItem)(unsafe.Pointer(pTableName + 8))).FzName) (*TTrigger)(unsafe.Pointer(pTrigger)).FpWhen = pWhen pWhen = uintptr(0) } else { (*TTrigger)(unsafe.Pointer(pTrigger)).FpWhen = _sqlite3ExprDup(tls, db, pWhen, int32(EXPRDUP_REDUCE)) } (*TTrigger)(unsafe.Pointer(pTrigger)).FpColumns = pColumns pColumns = uintptr(0) _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger = pTrigger goto trigger_cleanup trigger_cleanup: ; _sqlite3DbFree(tls, db, zName) _sqlite3SrcListDelete(tls, db, pTableName) _sqlite3IdListDelete(tls, db, pColumns) _sqlite3ExprDelete(tls, db, pWhen) if !((*TParse)(unsafe.Pointer(pParse)).FpNewTrigger != 0) { _sqlite3DeleteTrigger(tls, db, pTrigger) } else { _ = libc.Int32FromInt32(0) } return goto trigger_orphan_error trigger_orphan_error: ; if int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb) == int32(1) { /* Ticket #3810. ** Normally, whenever a table is dropped, all associated triggers are ** dropped too. But if a TEMP trigger is created on a non-TEMP table ** and the table is dropped by a different database connection, the ** trigger is not visible to the database connection that does the ** drop so the trigger cannot be dropped. This results in an ** "orphaned trigger" - a trigger whose associated table is missing. ** ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df */ libc.SetBitFieldPtr8Uint32(db+172+8, libc.Uint32FromInt32(1), 0, 0x1) } goto trigger_cleanup } // C documentation // // /* // ** This routine is called after all of the trigger actions have been parsed // ** in order to complete the process of building the trigger. // */ func _sqlite3FinishTrigger(tls *libc.TLS, pParse uintptr, pStepList uintptr, pAll uintptr) { bp := tls.Alloc(112) defer tls.Free(112) var db, pHash, pLink, pStep, pTab, pTrig, v, z, zName uintptr var iDb int32 var _ /* nameToken at bp+56 */ TToken var _ /* sFix at bp+0 */ TDbFixer _, _, _, _, _, _, _, _, _, _ = db, iDb, pHash, pLink, pStep, pTab, pTrig, v, z, zName pTrig = (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger /* Name of trigger */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Trigger name for error reporting */ (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger = uintptr(0) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 || !(pTrig != 0) { goto triggerfinish_cleanup } zName = (*TTrigger)(unsafe.Pointer(pTrig)).FzName iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTrigger)(unsafe.Pointer(pTrig)).FpSchema) (*TTrigger)(unsafe.Pointer(pTrig)).Fstep_list = pStepList for pStepList != 0 { (*TTriggerStep)(unsafe.Pointer(pStepList)).FpTrig = pTrig pStepList = (*TTriggerStep)(unsafe.Pointer(pStepList)).FpNext } _sqlite3TokenInit(tls, bp+56, (*TTrigger)(unsafe.Pointer(pTrig)).FzName) _sqlite3FixInit(tls, bp, pParse, iDb, __ccgo_ts+20379, bp+56) if _sqlite3FixTriggerStep(tls, bp, (*TTrigger)(unsafe.Pointer(pTrig)).Fstep_list) != 0 || _sqlite3FixExpr(tls, bp, (*TTrigger)(unsafe.Pointer(pTrig)).FpWhen) != 0 { goto triggerfinish_cleanup } if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger = pTrig pTrig = uintptr(0) } else { /* if we are not initializing, ** build the sqlite_schema entry */ if !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) { /* If this is a new CREATE TABLE statement, and if shadow tables ** are read-only, and the trigger makes a change to a shadow table, ** then raise an error - do not allow the trigger to be created. */ if _sqlite3ReadOnlyShadowTables(tls, db) != 0 { pStep = (*TTrigger)(unsafe.Pointer(pTrig)).Fstep_list for { if !(pStep != 0) { break } if (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget != uintptr(0) && _sqlite3ShadowTableName(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20628, libc.VaList(bp+72, (*TTrigger)(unsafe.Pointer(pTrig)).FzName, (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget)) goto triggerfinish_cleanup } goto _1 _1: ; pStep = (*TTriggerStep)(unsafe.Pointer(pStep)).FpNext } } /* Make an entry in the sqlite_schema table */ v = _sqlite3GetVdbe(tls, pParse) if v == uintptr(0) { goto triggerfinish_cleanup } _sqlite3BeginWriteOperation(tls, pParse, 0, iDb) z = _sqlite3DbStrNDup(tls, db, (*TToken)(unsafe.Pointer(pAll)).Fz, uint64((*TToken)(unsafe.Pointer(pAll)).Fn)) _sqlite3NestedParse(tls, pParse, __ccgo_ts+20676, libc.VaList(bp+72, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, zName, (*TTrigger)(unsafe.Pointer(pTrig)).Ftable, z)) _sqlite3DbFree(tls, db, z) _sqlite3ChangeCookie(tls, pParse, iDb) _sqlite3VdbeAddParseSchemaOp(tls, v, iDb, _sqlite3MPrintf(tls, db, __ccgo_ts+20751, libc.VaList(bp+72, zName)), uint16(0)) } } if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 { pLink = pTrig pHash = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema + 40 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pTrig = _sqlite3HashInsert(tls, pHash, zName, pTrig) if pTrig != 0 { _sqlite3OomFault(tls, db) } else { if (*TTrigger)(unsafe.Pointer(pLink)).FpSchema == (*TTrigger)(unsafe.Pointer(pLink)).FpTabSchema { pTab = _sqlite3HashFind(tls, (*TTrigger)(unsafe.Pointer(pLink)).FpTabSchema+8, (*TTrigger)(unsafe.Pointer(pLink)).Ftable) _ = libc.Int32FromInt32(0) (*TTrigger)(unsafe.Pointer(pLink)).FpNext = (*TTable)(unsafe.Pointer(pTab)).FpTrigger (*TTable)(unsafe.Pointer(pTab)).FpTrigger = pLink } } } goto triggerfinish_cleanup triggerfinish_cleanup: ; _sqlite3DeleteTrigger(tls, db, pTrig) _ = libc.Int32FromInt32(0) _sqlite3DeleteTriggerStep(tls, db, pStepList) } // C documentation // // /* // ** Duplicate a range of text from an SQL statement, then convert all // ** whitespace characters into ordinary space characters. // */ func _triggerSpanDup(tls *libc.TLS, db uintptr, zStart uintptr, zEnd uintptr) (r uintptr) { var i int32 var z uintptr _, _ = i, z z = _sqlite3DbSpanDup(tls, db, zStart, zEnd) if z != 0 { i = 0 for { if !(*(*uint8)(unsafe.Pointer(z + uintptr(i))) != 0) { break } if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x01) != 0 { *(*uint8)(unsafe.Pointer(z + uintptr(i))) = uint8(' ') } goto _1 _1: ; i++ } } return z } // C documentation // // /* // ** Turn a SELECT statement (that the pSelect parameter points to) into // ** a trigger step. Return a pointer to a TriggerStep structure. // ** // ** The parser calls this routine when it finds a SELECT statement in // ** body of a TRIGGER. // */ func _sqlite3TriggerSelectStep(tls *libc.TLS, db uintptr, pSelect uintptr, zStart uintptr, zEnd uintptr) (r uintptr) { var pTriggerStep uintptr _ = pTriggerStep pTriggerStep = _sqlite3DbMallocZero(tls, db, uint64(48)) if pTriggerStep == uintptr(0) { _sqlite3SelectDelete(tls, db, pSelect) return uintptr(0) } (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).Fop = uint8(TK_SELECT) (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpSelect = pSelect (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).Forconf = uint8(OE_Default) (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FzSpan = _triggerSpanDup(tls, db, zStart, zEnd) return pTriggerStep } // C documentation // // /* // ** Allocate space to hold a new trigger step. The allocated space // ** holds both the TriggerStep object and the TriggerStep.target.z string. // ** // ** If an OOM error occurs, NULL is returned and db->mallocFailed is set. // */ func _triggerStepAllocate(tls *libc.TLS, pParse uintptr, op Tu8, pName uintptr, zStart uintptr, zEnd uintptr) (r uintptr) { var db, pTriggerStep, z uintptr _, _, _ = db, pTriggerStep, z db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return uintptr(0) } pTriggerStep = _sqlite3DbMallocZero(tls, db, uint64(uint32(48)+(*TToken)(unsafe.Pointer(pName)).Fn+uint32(1))) if pTriggerStep != 0 { z = pTriggerStep + 1*48 libc.Xmemcpy(tls, z, (*TToken)(unsafe.Pointer(pName)).Fz, (*TToken)(unsafe.Pointer(pName)).Fn) _sqlite3Dequote(tls, z) (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FzTarget = z (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).Fop = op (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FzSpan = _triggerSpanDup(tls, db, zStart, zEnd) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenMap(tls, pParse, (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FzTarget, pName) } } return pTriggerStep } // C documentation // // /* // ** Build a trigger step out of an INSERT statement. Return a pointer // ** to the new trigger step. // ** // ** The parser calls this routine when it sees an INSERT inside the // ** body of a trigger. // */ func _sqlite3TriggerInsertStep(tls *libc.TLS, pParse uintptr, pTableName uintptr, pColumn uintptr, pSelect uintptr, orconf Tu8, pUpsert uintptr, zStart uintptr, zEnd uintptr) (r uintptr) { var db, pTriggerStep uintptr _, _ = db, pTriggerStep db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) pTriggerStep = _triggerStepAllocate(tls, pParse, uint8(TK_INSERT), pTableName, zStart, zEnd) if pTriggerStep != 0 { if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpSelect = pSelect pSelect = uintptr(0) } else { (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpSelect = _sqlite3SelectDup(tls, db, pSelect, int32(EXPRDUP_REDUCE)) } (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpIdList = pColumn (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpUpsert = pUpsert (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).Forconf = orconf if pUpsert != 0 { _sqlite3HasExplicitNulls(tls, pParse, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTarget) } } else { _sqlite3IdListDelete(tls, db, pColumn) _sqlite3UpsertDelete(tls, db, pUpsert) } _sqlite3SelectDelete(tls, db, pSelect) return pTriggerStep } // C documentation // // /* // ** Construct a trigger step that implements an UPDATE statement and return // ** a pointer to that trigger step. The parser calls this routine when it // ** sees an UPDATE statement inside the body of a CREATE TRIGGER. // */ func _sqlite3TriggerUpdateStep(tls *libc.TLS, pParse uintptr, pTableName uintptr, pFrom uintptr, pEList uintptr, pWhere uintptr, orconf Tu8, zStart uintptr, zEnd uintptr) (r uintptr) { var db, pTriggerStep uintptr _, _ = db, pTriggerStep db = (*TParse)(unsafe.Pointer(pParse)).Fdb pTriggerStep = _triggerStepAllocate(tls, pParse, uint8(TK_UPDATE), pTableName, zStart, zEnd) if pTriggerStep != 0 { if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpExprList = pEList (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpWhere = pWhere (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpFrom = pFrom pEList = uintptr(0) pWhere = uintptr(0) pFrom = uintptr(0) } else { (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpExprList = _sqlite3ExprListDup(tls, db, pEList, int32(EXPRDUP_REDUCE)) (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpWhere = _sqlite3ExprDup(tls, db, pWhere, int32(EXPRDUP_REDUCE)) (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpFrom = _sqlite3SrcListDup(tls, db, pFrom, int32(EXPRDUP_REDUCE)) } (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).Forconf = orconf } _sqlite3ExprListDelete(tls, db, pEList) _sqlite3ExprDelete(tls, db, pWhere) _sqlite3SrcListDelete(tls, db, pFrom) return pTriggerStep } // C documentation // // /* // ** Construct a trigger step that implements a DELETE statement and return // ** a pointer to that trigger step. The parser calls this routine when it // ** sees a DELETE statement inside the body of a CREATE TRIGGER. // */ func _sqlite3TriggerDeleteStep(tls *libc.TLS, pParse uintptr, pTableName uintptr, pWhere uintptr, zStart uintptr, zEnd uintptr) (r uintptr) { var db, pTriggerStep uintptr _, _ = db, pTriggerStep db = (*TParse)(unsafe.Pointer(pParse)).Fdb pTriggerStep = _triggerStepAllocate(tls, pParse, uint8(TK_DELETE), pTableName, zStart, zEnd) if pTriggerStep != 0 { if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpWhere = pWhere pWhere = uintptr(0) } else { (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpWhere = _sqlite3ExprDup(tls, db, pWhere, int32(EXPRDUP_REDUCE)) } (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).Forconf = uint8(OE_Default) } _sqlite3ExprDelete(tls, db, pWhere) return pTriggerStep } // C documentation // // /* // ** Recursively delete a Trigger structure // */ func _sqlite3DeleteTrigger(tls *libc.TLS, db uintptr, pTrigger uintptr) { if pTrigger == uintptr(0) || (*TTrigger)(unsafe.Pointer(pTrigger)).FbReturning != 0 { return } _sqlite3DeleteTriggerStep(tls, db, (*TTrigger)(unsafe.Pointer(pTrigger)).Fstep_list) _sqlite3DbFree(tls, db, (*TTrigger)(unsafe.Pointer(pTrigger)).FzName) _sqlite3DbFree(tls, db, (*TTrigger)(unsafe.Pointer(pTrigger)).Ftable) _sqlite3ExprDelete(tls, db, (*TTrigger)(unsafe.Pointer(pTrigger)).FpWhen) _sqlite3IdListDelete(tls, db, (*TTrigger)(unsafe.Pointer(pTrigger)).FpColumns) _sqlite3DbFree(tls, db, pTrigger) } // C documentation // // /* // ** This function is called to drop a trigger from the database schema. // ** // ** This may be called directly from the parser and therefore identifies // ** the trigger by name. The sqlite3DropTriggerPtr() routine does the // ** same job as this routine except it takes a pointer to the trigger // ** instead of the trigger name. // **/ func _sqlite3DropTrigger(tls *libc.TLS, pParse uintptr, pName uintptr, noErr int32) { bp := tls.Alloc(16) defer tls.Free(16) var db, pTrigger, zDb, zName uintptr var i, j, v2 int32 _, _, _, _, _, _, _ = db, i, j, pTrigger, zDb, zName, v2 pTrigger = uintptr(0) db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto drop_trigger_cleanup } if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) { goto drop_trigger_cleanup } _ = libc.Int32FromInt32(0) zDb = (*(*TSrcItem)(unsafe.Pointer(pName + 8))).FzDatabase zName = (*(*TSrcItem)(unsafe.Pointer(pName + 8))).FzName _ = libc.Int32FromInt32(0) i = OMIT_TEMPDB for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } if i < int32(2) { v2 = i ^ int32(1) } else { v2 = i } j = v2 /* Search TEMP before MAIN */ if zDb != 0 && _sqlite3DbIsNamed(tls, db, j, zDb) == 0 { goto _1 } _ = libc.Int32FromInt32(0) pTrigger = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(j)*16))).FpSchema+40, zName) if pTrigger != 0 { break } goto _1 _1: ; i++ } if !(pTrigger != 0) { if !(noErr != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20780, libc.VaList(bp+8, pName+8)) } else { _sqlite3CodeVerifyNamedSchema(tls, pParse, zDb) } (*TParse)(unsafe.Pointer(pParse)).FcheckSchema = uint8(1) goto drop_trigger_cleanup } _sqlite3DropTriggerPtr(tls, pParse, pTrigger) goto drop_trigger_cleanup drop_trigger_cleanup: ; _sqlite3SrcListDelete(tls, db, pName) } // C documentation // // /* // ** Return a pointer to the Table structure for the table that a trigger // ** is set on. // */ func _tableOfTrigger(tls *libc.TLS, pTrigger uintptr) (r uintptr) { return _sqlite3HashFind(tls, (*TTrigger)(unsafe.Pointer(pTrigger)).FpTabSchema+8, (*TTrigger)(unsafe.Pointer(pTrigger)).Ftable) } // C documentation // // /* // ** Drop a trigger given a pointer to that trigger. // */ func _sqlite3DropTriggerPtr(tls *libc.TLS, pParse uintptr, pTrigger uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var code, iDb int32 var db, pTable, v, zDb, zTab, v1, v2 uintptr _, _, _, _, _, _, _, _, _ = code, db, iDb, pTable, v, zDb, zTab, v1, v2 db = (*TParse)(unsafe.Pointer(pParse)).Fdb iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTrigger)(unsafe.Pointer(pTrigger)).FpSchema) _ = libc.Int32FromInt32(0) pTable = _tableOfTrigger(tls, pTrigger) _ = libc.Int32FromInt32(0) if pTable != 0 { code = int32(SQLITE_DROP_TRIGGER) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) { v1 = __ccgo_ts + 6533 } else { v1 = __ccgo_ts + 6066 } zTab = v1 if iDb == int32(1) { code = int32(SQLITE_DROP_TEMP_TRIGGER) } if _sqlite3AuthCheck(tls, pParse, code, (*TTrigger)(unsafe.Pointer(pTrigger)).FzName, (*TTable)(unsafe.Pointer(pTable)).FzName, zDb) != 0 || _sqlite3AuthCheck(tls, pParse, int32(SQLITE_DELETE), zTab, uintptr(0), zDb) != 0 { return } } /* Generate code to destroy the database record of the trigger. */ v2 = _sqlite3GetVdbe(tls, pParse) v = v2 if v2 != uintptr(0) { _sqlite3NestedParse(tls, pParse, __ccgo_ts+20800, libc.VaList(bp+8, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, (*TTrigger)(unsafe.Pointer(pTrigger)).FzName)) _sqlite3ChangeCookie(tls, pParse, iDb) _sqlite3VdbeAddOp4(tls, v, int32(OP_DropTrigger), iDb, 0, 0, (*TTrigger)(unsafe.Pointer(pTrigger)).FzName, 0) } } // C documentation // // /* // ** Remove a trigger from the hash tables of the sqlite* pointer. // */ func _sqlite3UnlinkAndDeleteTrigger(tls *libc.TLS, db uintptr, iDb int32, zName uintptr) { var pHash, pTab, pTrigger, pp uintptr _, _, _, _ = pHash, pTab, pTrigger, pp _ = libc.Int32FromInt32(0) pHash = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema + 40 pTrigger = _sqlite3HashInsert(tls, pHash, zName, uintptr(0)) if pTrigger != 0 { if (*TTrigger)(unsafe.Pointer(pTrigger)).FpSchema == (*TTrigger)(unsafe.Pointer(pTrigger)).FpTabSchema { pTab = _tableOfTrigger(tls, pTrigger) if pTab != 0 { pp = pTab + 56 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != 0) { break } if *(*uintptr)(unsafe.Pointer(pp)) == pTrigger { *(*uintptr)(unsafe.Pointer(pp)) = (*TTrigger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FpNext break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 32 } } } _sqlite3DeleteTrigger(tls, db, pTrigger) *(*Tu32)(unsafe.Pointer(db + 24)) |= uint32(DBFLAG_SchemaChange) } } // C documentation // // /* // ** pEList is the SET clause of an UPDATE statement. Each entry // ** in pEList is of the format =. If any of the entries // ** in pEList have an which matches an identifier in pIdList, // ** then return TRUE. If pIdList==NULL, then it is considered a // ** wildcard that matches anything. Likewise if pEList==NULL then // ** it matches anything so always return true. Return false only // ** if there is no match. // */ func _checkColumnOverlap(tls *libc.TLS, pIdList uintptr, pEList uintptr) (r int32) { var e int32 _ = e if pIdList == uintptr(0) || pEList == uintptr(0) { return int32(1) } e = 0 for { if !(e < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } if _sqlite3IdListIndex(tls, pIdList, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(e)*20))).FzEName) >= 0 { return int32(1) } goto _1 _1: ; e++ } return 0 } // C documentation // // /* // ** Return true if any TEMP triggers exist // */ func _tempTriggersExist(tls *libc.TLS, db uintptr) (r int32) { if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema == uintptr(0) { return 0 } if (*THash)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema+40)).Ffirst == uintptr(0) { return 0 } return int32(1) } // C documentation // // /* // ** Return a list of all triggers on table pTab if there exists at least // ** one trigger that must be fired when an operation of type 'op' is // ** performed on the table, and, if that operation is an UPDATE, if at // ** least one of the columns in pChanges is being modified. // */ func _triggersReallyExist(tls *libc.TLS, pParse uintptr, pTab uintptr, op int32, pChanges uintptr, pMask uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var mask int32 var p, pList, v1, v2 uintptr _, _, _, _, _ = mask, p, pList, v1, v2 mask = 0 pList = uintptr(0) pList = _sqlite3TriggerList(tls, pParse, pTab) _ = libc.Int32FromInt32(0) if pList != uintptr(0) { p = pList if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&uint64(SQLITE_EnableTrigger) == uint64(0) && (*TTable)(unsafe.Pointer(pTab)).FpTrigger != uintptr(0) { /* The SQLITE_DBCONFIG_ENABLE_TRIGGER setting is off. That means that ** only TEMP triggers are allowed. Truncate the pList so that it ** includes only TEMP triggers */ if pList == (*TTable)(unsafe.Pointer(pTab)).FpTrigger { pList = uintptr(0) goto exit_triggers_exist } for (*TTrigger)(unsafe.Pointer(p)).FpNext != 0 && (*TTrigger)(unsafe.Pointer(p)).FpNext != (*TTable)(unsafe.Pointer(pTab)).FpTrigger { p = (*TTrigger)(unsafe.Pointer(p)).FpNext } (*TTrigger)(unsafe.Pointer(p)).FpNext = uintptr(0) p = pList } for cond := true; cond; cond = p != 0 { if int32((*TTrigger)(unsafe.Pointer(p)).Fop) == op && _checkColumnOverlap(tls, (*TTrigger)(unsafe.Pointer(p)).FpColumns, pChanges) != 0 { mask |= int32((*TTrigger)(unsafe.Pointer(p)).Ftr_tm) } else { if int32((*TTrigger)(unsafe.Pointer(p)).Fop) == int32(TK_RETURNING) { /* The first time a RETURNING trigger is seen, the "op" value tells ** us what time of trigger it should be. */ _ = libc.Int32FromInt32(0) (*TTrigger)(unsafe.Pointer(p)).Fop = uint8(op) if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { if op != int32(TK_INSERT) { if op == int32(TK_DELETE) { v1 = __ccgo_ts + 20862 } else { v1 = __ccgo_ts + 20869 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20876, libc.VaList(bp+8, v1)) } (*TTrigger)(unsafe.Pointer(p)).Ftr_tm = uint8(TRIGGER_BEFORE) } else { (*TTrigger)(unsafe.Pointer(p)).Ftr_tm = uint8(TRIGGER_AFTER) } mask |= int32((*TTrigger)(unsafe.Pointer(p)).Ftr_tm) } else { if (*TTrigger)(unsafe.Pointer(p)).FbReturning != 0 && int32((*TTrigger)(unsafe.Pointer(p)).Fop) == int32(TK_INSERT) && op == int32(TK_UPDATE) && (*TParse)(unsafe.Pointer(pParse)).FpToplevel == uintptr(0) { /* Also fire a RETURNING trigger for an UPSERT */ mask |= int32((*TTrigger)(unsafe.Pointer(p)).Ftr_tm) } } } p = (*TTrigger)(unsafe.Pointer(p)).FpNext } } goto exit_triggers_exist exit_triggers_exist: ; if pMask != 0 { *(*int32)(unsafe.Pointer(pMask)) = mask } if mask != 0 { v2 = pList } else { v2 = uintptr(0) } return v2 } func _sqlite3TriggersExist(tls *libc.TLS, pParse uintptr, pTab uintptr, op int32, pChanges uintptr, pMask uintptr) (r uintptr) { _ = libc.Int32FromInt32(0) if (*TTable)(unsafe.Pointer(pTab)).FpTrigger == uintptr(0) && !(_tempTriggersExist(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb) != 0) || (*TParse)(unsafe.Pointer(pParse)).FdisableTriggers != 0 { if pMask != 0 { *(*int32)(unsafe.Pointer(pMask)) = 0 } return uintptr(0) } return _triggersReallyExist(tls, pParse, pTab, op, pChanges, pMask) } // C documentation // // /* // ** Convert the pStep->zTarget string into a SrcList and return a pointer // ** to that SrcList. // ** // ** This routine adds a specific database name, if needed, to the target when // ** forming the SrcList. This prevents a trigger in one database from // ** referring to a target in another database. An exception is when the // ** trigger is in TEMP in which case it can refer to any other database it // ** wants. // */ func _sqlite3TriggerStepSrc(tls *libc.TLS, pParse uintptr, pStep uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, pDup, pSchema, pSrc, pSubquery, zName uintptr var _ /* as at bp+0 */ TToken _, _, _, _, _, _ = db, pDup, pSchema, pSrc, pSubquery, zName db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* SrcList to be returned */ zName = _sqlite3DbStrDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget) pSrc = _sqlite3SrcListAppend(tls, pParse, uintptr(0), uintptr(0), uintptr(0)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pSrc != 0 { pSchema = (*TTrigger)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(pStep)).FpTrig)).FpSchema (*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FzName = zName if pSchema != (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema { (*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FpSchema = pSchema } if (*TTriggerStep)(unsafe.Pointer(pStep)).FpFrom != 0 { pDup = _sqlite3SrcListDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpFrom, 0) if pDup != 0 && (*TSrcList)(unsafe.Pointer(pDup)).FnSrc > int32(1) && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) { pSubquery = _sqlite3SelectNew(tls, pParse, uintptr(0), pDup, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(SF_NestedFrom), uintptr(0)) (*(*TToken)(unsafe.Pointer(bp))).Fn = uint32(0) (*(*TToken)(unsafe.Pointer(bp))).Fz = uintptr(0) pDup = _sqlite3SrcListAppendFromTerm(tls, pParse, uintptr(0), uintptr(0), uintptr(0), bp, pSubquery, uintptr(0)) } pSrc = _sqlite3SrcListAppendList(tls, pParse, pSrc, pDup) } } else { _sqlite3DbFree(tls, db, zName) } return pSrc } // C documentation // // /* // ** Return true if the pExpr term from the RETURNING clause argument // ** list is of the form "*". Raise an error if the terms if of the // ** form "table.*". // */ func _isAsteriskTerm(tls *libc.TLS, pParse uintptr, pTerm uintptr) (r int32) { _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pTerm)).Fop) == int32(TK_ASTERISK) { return int32(1) } if int32((*TExpr)(unsafe.Pointer(pTerm)).Fop) != int32(TK_DOT) { return 0 } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pTerm)).FpRight)).Fop) != int32(TK_ASTERISK) { return 0 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20924, 0) return int32(1) } // C documentation // // /* The input list pList is the list of result set terms from a RETURNING // ** clause. The table that we are returning from is pTab. // ** // ** This routine makes a copy of the pList, and at the same time expands // ** any "*" wildcards to be the complete set of columns from pTab. // */ func _sqlite3ExpandReturning(tls *libc.TLS, pParse uintptr, pList uintptr, pTab uintptr) (r uintptr) { var db, pItem, pItem1, pNew, pNewExpr, pNewExpr1, pOldExpr uintptr var i, jj int32 _, _, _, _, _, _, _, _, _ = db, i, jj, pItem, pItem1, pNew, pNewExpr, pNewExpr1, pOldExpr pNew = uintptr(0) db = (*TParse)(unsafe.Pointer(pParse)).Fdb i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } pOldExpr = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr if pOldExpr == uintptr(0) { goto _1 } if _isAsteriskTerm(tls, pParse, pOldExpr) != 0 { jj = 0 for { if !(jj < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if int32((*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(jj)*12)).FcolFlags)&int32(COLFLAG_HIDDEN) != 0 { goto _2 } pNewExpr = _sqlite3Expr(tls, db, int32(TK_ID), (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(jj)*12))).FzCnName) pNew = _sqlite3ExprListAppend(tls, pParse, pNew, pNewExpr) if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { pItem = pNew + 8 + uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*20 (*TExprList_item)(unsafe.Pointer(pItem)).FzEName = _sqlite3DbStrDup(tls, db, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(jj)*12))).FzCnName) libc.SetBitFieldPtr16Uint32(pItem+8+4, libc.Uint32FromInt32(ENAME_NAME), 0, 0x3) } goto _2 _2: ; jj++ } } else { pNewExpr1 = _sqlite3ExprDup(tls, db, pOldExpr, 0) pNew = _sqlite3ExprListAppend(tls, pParse, pNew, pNewExpr1) if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) && (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FzEName != uintptr(0) { pItem1 = pNew + 8 + uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*20 (*TExprList_item)(unsafe.Pointer(pItem1)).FzEName = _sqlite3DbStrDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FzEName) libc.SetBitFieldPtr16Uint32(pItem1+8+4, uint32(int32(uint32(*(*uint16)(unsafe.Pointer(pList + 8 + uintptr(i)*20 + 8 + 4))&0x3>>0))), 0, 0x3) } } goto _1 _1: ; i++ } return pNew } // C documentation // // /* // ** Generate code for the RETURNING trigger. Unlike other triggers // ** that invoke a subprogram in the bytecode, the code for RETURNING // ** is generated in-line. // */ func _codeReturningTrigger(tls *libc.TLS, pParse uintptr, pTrigger uintptr, pTab uintptr, regIn int32) { bp := tls.Alloc(208) defer tls.Free(208) var db, pCol, pNew, pReturning, v, v2 uintptr var i, nCol, reg, v1 int32 var _ /* sFrom at bp+80 */ TSrcList var _ /* sNC at bp+160 */ TNameContext var _ /* sSelect at bp+0 */ TSelect _, _, _, _, _, _, _, _, _, _ = db, i, nCol, pCol, pNew, pReturning, reg, v, v1, v2 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) if !((*TParse)(unsafe.Pointer(pParse)).FbReturning != 0) { /* This RETURNING trigger must be for a different statement as ** this statement lacks a RETURNING clause. */ return } _ = libc.Int32FromInt32(0) pReturning = *(*uintptr)(unsafe.Pointer(pParse + 144)) if pTrigger != pReturning+8 { /* This RETURNING trigger is for a different statement */ return } libc.Xmemset(tls, bp, 0, uint32(76)) libc.Xmemset(tls, bp+80, 0, uint32(80)) (*(*TSelect)(unsafe.Pointer(bp))).FpEList = _sqlite3ExprListDup(tls, db, (*TReturning)(unsafe.Pointer(pReturning)).FpReturnEL, 0) (*(*TSelect)(unsafe.Pointer(bp))).FpSrc = bp + 80 (*(*TSrcList)(unsafe.Pointer(bp + 80))).FnSrc = int32(1) (*(*TSrcItem)(unsafe.Pointer(bp + 80 + 8))).FpTab = pTab (*(*TSrcItem)(unsafe.Pointer(bp + 80 + 8))).FiCursor = -int32(1) _sqlite3SelectPrep(tls, pParse, bp, uintptr(0)) if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 { _ = libc.Int32FromInt32(0) _sqlite3GenerateColumnNames(tls, pParse, bp) } _sqlite3ExprListDelete(tls, db, (*(*TSelect)(unsafe.Pointer(bp))).FpEList) pNew = _sqlite3ExpandReturning(tls, pParse, (*TReturning)(unsafe.Pointer(pReturning)).FpReturnEL, pTab) if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 { libc.Xmemset(tls, bp+160, 0, uint32(36)) if (*TReturning)(unsafe.Pointer(pReturning)).FnRetCol == 0 { (*TReturning)(unsafe.Pointer(pReturning)).FnRetCol = (*TExprList)(unsafe.Pointer(pNew)).FnExpr v2 = pParse + 40 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ (*TReturning)(unsafe.Pointer(pReturning)).FiRetCur = v1 } (*(*TNameContext)(unsafe.Pointer(bp + 160))).FpParse = pParse *(*int32)(unsafe.Pointer(bp + 160 + 8)) = regIn (*(*TNameContext)(unsafe.Pointer(bp + 160))).FncFlags = int32(NC_UBaseReg) (*TParse)(unsafe.Pointer(pParse)).FeTriggerOp = (*TTrigger)(unsafe.Pointer(pTrigger)).Fop (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab = pTab if _sqlite3ResolveExprListNames(tls, bp+160, pNew) == SQLITE_OK && !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { nCol = (*TExprList)(unsafe.Pointer(pNew)).FnExpr reg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nCol + int32(2) (*TReturning)(unsafe.Pointer(pReturning)).FiRetReg = reg i = 0 for { if !(i < nCol) { break } pCol = (*(*TExprList_item)(unsafe.Pointer(pNew + 8 + uintptr(i)*20))).FpExpr _ = libc.Int32FromInt32(0) /* Due to !db->mallocFailed ~9 lines above */ _sqlite3ExprCodeFactorable(tls, pParse, pCol, reg+i) if int32(_sqlite3ExprAffinity(tls, pCol)) == int32(SQLITE_AFF_REAL) { _sqlite3VdbeAddOp1(tls, v, int32(OP_RealAffinity), reg+i) } goto _3 _3: ; i++ } _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), reg, i, reg+i) _sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), (*TReturning)(unsafe.Pointer(pReturning)).FiRetCur, reg+i+int32(1)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), (*TReturning)(unsafe.Pointer(pReturning)).FiRetCur, reg+i, reg+i+int32(1)) } } _sqlite3ExprListDelete(tls, db, pNew) (*TParse)(unsafe.Pointer(pParse)).FeTriggerOp = uint8(0) (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab = uintptr(0) } // C documentation // // /* // ** Generate VDBE code for the statements inside the body of a single // ** trigger. // */ func _codeTriggerProgram(tls *libc.TLS, pParse uintptr, pStepList uintptr, orconf int32) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var db, pSelect, pStep, v uintptr var v2 int32 var _ /* sDest at bp+0 */ TSelectDest _, _, _, _, _ = db, pSelect, pStep, v, v2 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pStep = pStepList for { if !(pStep != 0) { break } /* Figure out the ON CONFLICT policy that will be used for this step ** of the trigger program. If the statement that caused this trigger ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use ** the ON CONFLICT policy that was specified as part of the trigger ** step statement. Example: ** ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN; ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b); ** END; ** ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy */ if orconf == int32(OE_Default) { v2 = int32((*TTriggerStep)(unsafe.Pointer(pStep)).Forconf) } else { v2 = int32(uint8(orconf)) } (*TParse)(unsafe.Pointer(pParse)).FeOrconf = uint8(v2) _ = libc.Int32FromInt32(0) if (*TTriggerStep)(unsafe.Pointer(pStep)).FzSpan != 0 { _sqlite3VdbeAddOp4(tls, v, int32(OP_Trace), int32(0x7fffffff), int32(1), 0, _sqlite3MPrintf(tls, db, __ccgo_ts+6263, libc.VaList(bp+40, (*TTriggerStep)(unsafe.Pointer(pStep)).FzSpan)), -int32(6)) } switch int32((*TTriggerStep)(unsafe.Pointer(pStep)).Fop) { case int32(TK_UPDATE): _sqlite3Update(tls, pParse, _sqlite3TriggerStepSrc(tls, pParse, pStep), _sqlite3ExprListDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList, 0), _sqlite3ExprDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpWhere, 0), int32((*TParse)(unsafe.Pointer(pParse)).FeOrconf), uintptr(0), uintptr(0), uintptr(0)) _sqlite3VdbeAddOp0(tls, v, int32(OP_ResetCount)) case int32(TK_INSERT): _sqlite3Insert(tls, pParse, _sqlite3TriggerStepSrc(tls, pParse, pStep), _sqlite3SelectDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpSelect, 0), _sqlite3IdListDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpIdList), int32((*TParse)(unsafe.Pointer(pParse)).FeOrconf), _sqlite3UpsertDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpUpsert)) _sqlite3VdbeAddOp0(tls, v, int32(OP_ResetCount)) case int32(TK_DELETE): _sqlite3DeleteFrom(tls, pParse, _sqlite3TriggerStepSrc(tls, pParse, pStep), _sqlite3ExprDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpWhere, 0), uintptr(0), uintptr(0)) _sqlite3VdbeAddOp0(tls, v, int32(OP_ResetCount)) default: _ = libc.Int32FromInt32(0) pSelect = _sqlite3SelectDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpSelect, 0) _sqlite3SelectDestInit(tls, bp, int32(SRT_Discard), 0) _sqlite3Select(tls, pParse, pSelect, bp) _sqlite3SelectDelete(tls, db, pSelect) break } goto _1 _1: ; pStep = (*TTriggerStep)(unsafe.Pointer(pStep)).FpNext } return 0 } // C documentation // // /* // ** Parse context structure pFrom has just been used to create a sub-vdbe // ** (trigger program). If an error has occurred, transfer error information // ** from pFrom to pTo. // */ func _transferParseError(tls *libc.TLS, pTo uintptr, pFrom uintptr) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pTo)).FnErr == 0 { (*TParse)(unsafe.Pointer(pTo)).FzErrMsg = (*TParse)(unsafe.Pointer(pFrom)).FzErrMsg (*TParse)(unsafe.Pointer(pTo)).FnErr = (*TParse)(unsafe.Pointer(pFrom)).FnErr (*TParse)(unsafe.Pointer(pTo)).Frc = (*TParse)(unsafe.Pointer(pFrom)).Frc } else { _sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pFrom)).Fdb, (*TParse)(unsafe.Pointer(pFrom)).FzErrMsg) } } // C documentation // // /* // ** Create and populate a new TriggerPrg object with a sub-program // ** implementing trigger pTrigger with ON CONFLICT policy orconf. // */ func _codeRowTrigger(tls *libc.TLS, pParse uintptr, pTrigger uintptr, pTab uintptr, orconf int32) (r uintptr) { bp := tls.Alloc(336) defer tls.Free(336) var db, pPrg, pProgram, pTop, pWhen, v, v1, v2 uintptr var iEndTrigger int32 var _ /* sNC at bp+0 */ TNameContext var _ /* sSubParse at bp+36 */ TParse _, _, _, _, _, _, _, _, _ = db, iEndTrigger, pPrg, pProgram, pTop, pWhen, v, v1, v2 if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 { v1 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel } else { v1 = pParse } pTop = v1 db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Value to return */ pWhen = uintptr(0) /* Name context for sub-vdbe */ pProgram = uintptr(0) /* Sub-vdbe for trigger program */ iEndTrigger = 0 /* Parse context for sub-vdbe */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Allocate the TriggerPrg and SubProgram objects. To ensure that they ** are freed if an error occurs, link them into the Parse.pTriggerPrg ** list of the top-level Parse object sooner rather than later. */ pPrg = _sqlite3DbMallocZero(tls, db, uint64(24)) if !(pPrg != 0) { return uintptr(0) } (*TTriggerPrg)(unsafe.Pointer(pPrg)).FpNext = (*TParse)(unsafe.Pointer(pTop)).FpTriggerPrg (*TParse)(unsafe.Pointer(pTop)).FpTriggerPrg = pPrg v2 = _sqlite3DbMallocZero(tls, db, uint64(28)) pProgram = v2 (*TTriggerPrg)(unsafe.Pointer(pPrg)).FpProgram = v2 if !(pProgram != 0) { return uintptr(0) } _sqlite3VdbeLinkSubProgram(tls, (*TParse)(unsafe.Pointer(pTop)).FpVdbe, pProgram) (*TTriggerPrg)(unsafe.Pointer(pPrg)).FpTrigger = pTrigger (*TTriggerPrg)(unsafe.Pointer(pPrg)).Forconf = orconf *(*Tu32)(unsafe.Pointer(pPrg + 16)) = uint32(0xffffffff) *(*Tu32)(unsafe.Pointer(pPrg + 16 + 1*4)) = uint32(0xffffffff) /* Allocate and populate a new Parse context to use for coding the ** trigger sub-program. */ _sqlite3ParseObjectInit(tls, bp+36, db) libc.Xmemset(tls, bp, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp))).FpParse = bp + 36 (*(*TParse)(unsafe.Pointer(bp + 36))).FpTriggerTab = pTab (*(*TParse)(unsafe.Pointer(bp + 36))).FpToplevel = pTop (*(*TParse)(unsafe.Pointer(bp + 36))).FzAuthContext = (*TTrigger)(unsafe.Pointer(pTrigger)).FzName (*(*TParse)(unsafe.Pointer(bp + 36))).FeTriggerOp = (*TTrigger)(unsafe.Pointer(pTrigger)).Fop (*(*TParse)(unsafe.Pointer(bp + 36))).FnQueryLoop = (*TParse)(unsafe.Pointer(pParse)).FnQueryLoop (*(*TParse)(unsafe.Pointer(bp + 36))).FprepFlags = (*TParse)(unsafe.Pointer(pParse)).FprepFlags v = _sqlite3GetVdbe(tls, bp+36) if v != 0 { if (*TTrigger)(unsafe.Pointer(pTrigger)).FzName != 0 { _sqlite3VdbeChangeP4(tls, v, -int32(1), _sqlite3MPrintf(tls, db, __ccgo_ts+20966, libc.VaList(bp+328, (*TTrigger)(unsafe.Pointer(pTrigger)).FzName)), -int32(6)) } /* If one was specified, code the WHEN clause. If it evaluates to false ** (or NULL) the sub-vdbe is immediately halted by jumping to the ** OP_Halt inserted at the end of the program. */ if (*TTrigger)(unsafe.Pointer(pTrigger)).FpWhen != 0 { pWhen = _sqlite3ExprDup(tls, db, (*TTrigger)(unsafe.Pointer(pTrigger)).FpWhen, 0) if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 && SQLITE_OK == _sqlite3ResolveExprNames(tls, bp, pWhen) { iEndTrigger = _sqlite3VdbeMakeLabel(tls, bp+36) _sqlite3ExprIfFalse(tls, bp+36, pWhen, iEndTrigger, int32(SQLITE_JUMPIFNULL)) } _sqlite3ExprDelete(tls, db, pWhen) } /* Code the trigger program into the sub-vdbe. */ _codeTriggerProgram(tls, bp+36, (*TTrigger)(unsafe.Pointer(pTrigger)).Fstep_list, orconf) /* Insert an OP_Halt at the end of the sub-program. */ if iEndTrigger != 0 { _sqlite3VdbeResolveLabel(tls, v, iEndTrigger) } _sqlite3VdbeAddOp0(tls, v, int32(OP_Halt)) _transferParseError(tls, pParse, bp+36) if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 { _ = libc.Int32FromInt32(0) (*TSubProgram)(unsafe.Pointer(pProgram)).FaOp = _sqlite3VdbeTakeOpArray(tls, v, pProgram+4, pTop+104) } (*TSubProgram)(unsafe.Pointer(pProgram)).FnMem = (*(*TParse)(unsafe.Pointer(bp + 36))).FnMem (*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr = (*(*TParse)(unsafe.Pointer(bp + 36))).FnTab (*TSubProgram)(unsafe.Pointer(pProgram)).Ftoken = pTrigger *(*Tu32)(unsafe.Pointer(pPrg + 16)) = (*(*TParse)(unsafe.Pointer(bp + 36))).Foldmask *(*Tu32)(unsafe.Pointer(pPrg + 16 + 1*4)) = (*(*TParse)(unsafe.Pointer(bp + 36))).Fnewmask _sqlite3VdbeDelete(tls, v) } else { _transferParseError(tls, pParse, bp+36) } _ = libc.Int32FromInt32(0) _sqlite3ParseObjectReset(tls, bp+36) return pPrg } // C documentation // // /* // ** Return a pointer to a TriggerPrg object containing the sub-program for // ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such // ** TriggerPrg object exists, a new object is allocated and populated before // ** being returned. // */ func _getRowTrigger(tls *libc.TLS, pParse uintptr, pTrigger uintptr, pTab uintptr, orconf int32) (r uintptr) { var pPrg, pRoot, v1 uintptr _, _, _ = pPrg, pRoot, v1 if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 { v1 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel } else { v1 = pParse } pRoot = v1 _ = libc.Int32FromInt32(0) /* It may be that this trigger has already been coded (or is in the ** process of being coded). If this is the case, then an entry with ** a matching TriggerPrg.pTrigger field will be present somewhere ** in the Parse.pTriggerPrg list. Search for such an entry. */ pPrg = (*TParse)(unsafe.Pointer(pRoot)).FpTriggerPrg for { if !(pPrg != 0 && ((*TTriggerPrg)(unsafe.Pointer(pPrg)).FpTrigger != pTrigger || (*TTriggerPrg)(unsafe.Pointer(pPrg)).Forconf != orconf)) { break } goto _2 _2: ; pPrg = (*TTriggerPrg)(unsafe.Pointer(pPrg)).FpNext } /* If an existing TriggerPrg could not be located, create a new one. */ if !(pPrg != 0) { pPrg = _codeRowTrigger(tls, pParse, pTrigger, pTab, orconf) (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FerrByteOffset = -int32(1) } return pPrg } // C documentation // // /* // ** Generate code for the trigger program associated with trigger p on // ** table pTab. The reg, orconf and ignoreJump parameters passed to this // ** function are the same as those described in the header function for // ** sqlite3CodeRowTrigger() // */ func _sqlite3CodeRowTriggerDirect(tls *libc.TLS, pParse uintptr, p uintptr, pTab uintptr, reg int32, orconf int32, ignoreJump int32) { var bRecursive, v1 int32 var pPrg, v, v2 uintptr _, _, _, _, _ = bRecursive, pPrg, v, v1, v2 v = _sqlite3GetVdbe(tls, pParse) pPrg = _getRowTrigger(tls, pParse, p, pTab, orconf) _ = libc.Int32FromInt32(0) /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program ** is a pointer to the sub-vdbe containing the trigger program. */ if pPrg != 0 { bRecursive = libc.BoolInt32((*TTrigger)(unsafe.Pointer(p)).FzName != 0 && uint64(0) == (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&uint64(SQLITE_RecTriggers)) v2 = pParse + 44 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) _sqlite3VdbeAddOp4(tls, v, int32(OP_Program), reg, ignoreJump, v1, (*TTriggerPrg)(unsafe.Pointer(pPrg)).FpProgram, -int32(4)) /* Set the P5 operand of the OP_Program instruction to non-zero if ** recursive invocation of this trigger program is disallowed. Recursive ** invocation is disallowed if (a) the sub-program is really a trigger, ** not a foreign key action, and (b) the flag to enable recursive triggers ** is clear. */ _sqlite3VdbeChangeP5(tls, v, uint16(uint8(bRecursive))) } } // C documentation // // /* // ** This is called to code the required FOR EACH ROW triggers for an operation // ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE) // ** is given by the op parameter. The tr_tm parameter determines whether the // ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then // ** parameter pChanges is passed the list of columns being modified. // ** // ** If there are no triggers that fire at the specified time for the specified // ** operation on pTab, this function is a no-op. // ** // ** The reg argument is the address of the first in an array of registers // ** that contain the values substituted for the new.* and old.* references // ** in the trigger program. If N is the number of columns in table pTab // ** (a copy of pTab->nCol), then registers are populated as follows: // ** // ** Register Contains // ** ------------------------------------------------------ // ** reg+0 OLD.rowid // ** reg+1 OLD.* value of left-most column of pTab // ** ... ... // ** reg+N OLD.* value of right-most column of pTab // ** reg+N+1 NEW.rowid // ** reg+N+2 NEW.* value of left-most column of pTab // ** ... ... // ** reg+N+N+1 NEW.* value of right-most column of pTab // ** // ** For ON DELETE triggers, the registers containing the NEW.* values will // ** never be accessed by the trigger program, so they are not allocated or // ** populated by the caller (there is no data to populate them with anyway). // ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers // ** are never accessed, and so are not allocated by the caller. So, for an // ** ON INSERT trigger, the value passed to this function as parameter reg // ** is not a readable register, although registers (reg+N) through // ** (reg+N+N+1) are. // ** // ** Parameter orconf is the default conflict resolution algorithm for the // ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump // ** is the instruction that control should jump to if a trigger program // ** raises an IGNORE exception. // */ func _sqlite3CodeRowTrigger(tls *libc.TLS, pParse uintptr, pTrigger uintptr, op int32, pChanges uintptr, tr_tm int32, pTab uintptr, reg int32, orconf int32, ignoreJump int32) { var p uintptr _ = p /* Used to iterate through pTrigger list */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) p = pTrigger for { if !(p != 0) { break } /* Sanity checking: The schema for the trigger and for the table are ** always defined. The trigger must be in the same schema as the table ** or else it must be a TEMP trigger. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Determine whether we should code this trigger. One of two choices: ** 1. The trigger is an exact match to the current DML statement ** 2. This is a RETURNING trigger for INSERT but we are currently ** doing the UPDATE part of an UPSERT. */ if (int32((*TTrigger)(unsafe.Pointer(p)).Fop) == op || (*TTrigger)(unsafe.Pointer(p)).FbReturning != 0 && int32((*TTrigger)(unsafe.Pointer(p)).Fop) == int32(TK_INSERT) && op == int32(TK_UPDATE)) && int32((*TTrigger)(unsafe.Pointer(p)).Ftr_tm) == tr_tm && _checkColumnOverlap(tls, (*TTrigger)(unsafe.Pointer(p)).FpColumns, pChanges) != 0 { if !((*TTrigger)(unsafe.Pointer(p)).FbReturning != 0) { _sqlite3CodeRowTriggerDirect(tls, pParse, p, pTab, reg, orconf, ignoreJump) } else { if (*TParse)(unsafe.Pointer(pParse)).FpToplevel == uintptr(0) { _codeReturningTrigger(tls, pParse, p, pTab, reg) } } } goto _1 _1: ; p = (*TTrigger)(unsafe.Pointer(p)).FpNext } } // C documentation // // /* // ** Triggers may access values stored in the old.* or new.* pseudo-table. // ** This function returns a 32-bit bitmask indicating which columns of the // ** old.* or new.* tables actually are used by triggers. This information // ** may be used by the caller, for example, to avoid having to load the entire // ** old.* record into memory when executing an UPDATE or DELETE command. // ** // ** Bit 0 of the returned mask is set if the left-most column of the // ** table may be accessed using an [old|new].reference. Bit 1 is set if // ** the second leftmost column value is required, and so on. If there // ** are more than 32 columns in the table, and at least one of the columns // ** with an index greater than 32 may be accessed, 0xffffffff is returned. // ** // ** It is not possible to determine if the old.rowid or new.rowid column is // ** accessed by triggers. The caller must always assume that it is. // ** // ** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned // ** applies to the old.* table. If 1, the new.* table. // ** // ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE // ** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only // ** included in the returned mask if the TRIGGER_BEFORE bit is set in the // ** tr_tm parameter. Similarly, values accessed by AFTER triggers are only // ** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm. // */ func _sqlite3TriggerColmask(tls *libc.TLS, pParse uintptr, pTrigger uintptr, pChanges uintptr, isNew int32, tr_tm int32, pTab uintptr, orconf int32) (r Tu32) { var mask Tu32 var op, v1 int32 var p, pPrg uintptr _, _, _, _, _ = mask, op, p, pPrg, v1 if pChanges != 0 { v1 = int32(TK_UPDATE) } else { v1 = int32(TK_DELETE) } op = v1 mask = uint32(0) _ = libc.Int32FromInt32(0) if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { return uint32(0xffffffff) } p = pTrigger for { if !(p != 0) { break } if int32((*TTrigger)(unsafe.Pointer(p)).Fop) == op && tr_tm&int32((*TTrigger)(unsafe.Pointer(p)).Ftr_tm) != 0 && _checkColumnOverlap(tls, (*TTrigger)(unsafe.Pointer(p)).FpColumns, pChanges) != 0 { if (*TTrigger)(unsafe.Pointer(p)).FbReturning != 0 { mask = uint32(0xffffffff) } else { pPrg = _getRowTrigger(tls, pParse, p, pTab, orconf) if pPrg != 0 { mask |= *(*Tu32)(unsafe.Pointer(pPrg + 16 + uintptr(isNew)*4)) } } } goto _2 _2: ; p = (*TTrigger)(unsafe.Pointer(p)).FpNext } return mask } // C documentation // // /* // ** The most recently coded instruction was an OP_Column to retrieve the // ** i-th column of table pTab. This routine sets the P4 parameter of the // ** OP_Column to the default value, if any. // ** // ** The default value of a column is specified by a DEFAULT clause in the // ** column definition. This was either supplied by the user when the table // ** was created, or added later to the table definition by an ALTER TABLE // ** command. If the latter, then the row-records in the table btree on disk // ** may not contain a value for the column and the default value, taken // ** from the P4 parameter of the OP_Column instruction, is returned instead. // ** If the former, then all row-records are guaranteed to include a value // ** for the column and the P4 value is not required. // ** // ** Column definitions created by an ALTER TABLE command may only have // ** literal default values specified: a number, null or a string. (If a more // ** complicated default expression value was provided, it is evaluated // ** when the ALTER TABLE is executed and one of the literal values written // ** into the sqlite_schema table.) // ** // ** Therefore, the P4 parameter is only required if the default value for // ** the column is a literal number, string or null. The sqlite3ValueFromExpr() // ** function is capable of transforming these types of expressions into // ** sqlite3_value objects. // ** // ** If column as REAL affinity and the table is an ordinary b-tree table // ** (not a virtual table) then the value might have been stored as an // ** integer. In that case, add an OP_RealAffinity opcode to make sure // ** it has been converted into REAL. // */ func _sqlite3ColumnDefault(tls *libc.TLS, v uintptr, pTab uintptr, i int32, iReg int32) { bp := tls.Alloc(16) defer tls.Free(16) var enc Tu8 var pCol uintptr var _ /* pValue at bp+0 */ uintptr _, _ = enc, pCol _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12 if (*TColumn)(unsafe.Pointer(pCol)).FiDflt != 0 { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) enc = (*Tsqlite3)(unsafe.Pointer(_sqlite3VdbeDb(tls, v))).Fenc _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3ValueFromExpr(tls, _sqlite3VdbeDb(tls, v), _sqlite3ColumnExpr(tls, pTab, pCol), enc, (*TColumn)(unsafe.Pointer(pCol)).Faffinity, bp) if *(*uintptr)(unsafe.Pointer(bp)) != 0 { _sqlite3VdbeAppendP4(tls, v, *(*uintptr)(unsafe.Pointer(bp)), -int32(10)) } } if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) == int32(SQLITE_AFF_REAL) && !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) { _sqlite3VdbeAddOp1(tls, v, int32(OP_RealAffinity), iReg) } } // C documentation // // /* // ** Check to see if column iCol of index pIdx references any of the // ** columns defined by aXRef and chngRowid. Return true if it does // ** and false if not. This is an optimization. False-positives are a // ** performance degradation, but false-negatives can result in a corrupt // ** index and incorrect answers. // ** // ** aXRef[j] will be non-negative if column j of the original table is // ** being updated. chngRowid will be true if the rowid of the table is // ** being updated. // */ func _indexColumnIsBeingUpdated(tls *libc.TLS, pIdx uintptr, iCol int32, aXRef uintptr, chngRowid int32) (r int32) { var iIdxCol Ti16 _ = iIdxCol iIdxCol = *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(iCol)*2)) _ = libc.Int32FromInt32(0) /* Cannot index rowid */ if int32(iIdxCol) >= 0 { return libc.BoolInt32(*(*int32)(unsafe.Pointer(aXRef + uintptr(iIdxCol)*4)) >= 0) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return _sqlite3ExprReferencesUpdatedColumn(tls, (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(iCol)*20))).FpExpr, aXRef, chngRowid) } // C documentation // // /* // ** Check to see if index pIdx is a partial index whose conditional // ** expression might change values due to an UPDATE. Return true if // ** the index is subject to change and false if the index is guaranteed // ** to be unchanged. This is an optimization. False-positives are a // ** performance degradation, but false-negatives can result in a corrupt // ** index and incorrect answers. // ** // ** aXRef[j] will be non-negative if column j of the original table is // ** being updated. chngRowid will be true if the rowid of the table is // ** being updated. // */ func _indexWhereClauseMightChange(tls *libc.TLS, pIdx uintptr, aXRef uintptr, chngRowid int32) (r int32) { if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere == uintptr(0) { return 0 } return _sqlite3ExprReferencesUpdatedColumn(tls, (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere, aXRef, chngRowid) } // C documentation // // /* // ** Allocate and return a pointer to an expression of type TK_ROW with // ** Expr.iColumn set to value (iCol+1). The resolver will modify the // ** expression to be a TK_COLUMN reading column iCol of the first // ** table in the source-list (pSrc->a[0]). // */ func _exprRowColumn(tls *libc.TLS, pParse uintptr, iCol int32) (r uintptr) { var pRet uintptr _ = pRet pRet = _sqlite3PExpr(tls, pParse, int32(TK_ROW), uintptr(0), uintptr(0)) if pRet != 0 { (*TExpr)(unsafe.Pointer(pRet)).FiColumn = int16(iCol + int32(1)) } return pRet } // C documentation // // /* // ** Assuming both the pLimit and pOrderBy parameters are NULL, this function // ** generates VM code to run the query: // ** // ** SELECT , pChanges FROM pTabList WHERE pWhere // ** // ** and write the results to the ephemeral table already opened as cursor // ** iEph. None of pChanges, pTabList or pWhere are modified or consumed by // ** this function, they must be deleted by the caller. // ** // ** Or, if pLimit and pOrderBy are not NULL, and pTab is not a view: // ** // ** SELECT , pChanges FROM pTabList // ** WHERE pWhere // ** GROUP BY // ** ORDER BY pOrderBy LIMIT pLimit // ** // ** If pTab is a view, the GROUP BY clause is omitted. // ** // ** Exactly how results are written to table iEph, and exactly what // ** the in the query above are is determined by the type // ** of table pTabList->a[0].pTab. // ** // ** If the table is a WITHOUT ROWID table, then argument pPk must be its // ** PRIMARY KEY. In this case are the primary key columns // ** of the table, in order. The results of the query are written to ephemeral // ** table iEph as index keys, using OP_IdxInsert. // ** // ** If the table is actually a view, then are all columns of // ** the view. The results are written to the ephemeral table iEph as records // ** with automatically assigned integer keys. // ** // ** If the table is a virtual or ordinary intkey table, then // ** is its rowid. For a virtual table, the results are written to iEph as // ** records with automatically assigned integer keys For intkey tables, the // ** rowid value in is used as the integer key, and the // ** remaining fields make up the table record. // */ func _updateFromSelect(tls *libc.TLS, pParse uintptr, iEph int32, pPk uintptr, pChanges uintptr, pTabList uintptr, pWhere uintptr, pOrderBy uintptr, pLimit uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var db, pGrp, pLimit2, pList, pNew, pOrderBy2, pSelect, pSrc, pTab, pWhere2 uintptr var eDest, i, v2, v4, v6 int32 var _ /* dest at bp+0 */ TSelectDest _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, eDest, i, pGrp, pLimit2, pList, pNew, pOrderBy2, pSelect, pSrc, pTab, pWhere2, v2, v4, v6 pSelect = uintptr(0) pList = uintptr(0) pGrp = uintptr(0) pLimit2 = uintptr(0) pOrderBy2 = uintptr(0) db = (*TParse)(unsafe.Pointer(pParse)).Fdb pTab = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FpTab _ = pOrderBy _ = pLimit pSrc = _sqlite3SrcListDup(tls, db, pTabList, 0) pWhere2 = _sqlite3ExprDup(tls, db, pWhere, 0) _ = libc.Int32FromInt32(0) if pSrc != 0 { _ = libc.Int32FromInt32(0) (*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FiCursor = -int32(1) (*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FpTab)).FnTabRef-- (*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FpTab = uintptr(0) } if pPk != 0 { i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) { break } pNew = _exprRowColumn(tls, pParse, int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2)))) pList = _sqlite3ExprListAppend(tls, pParse, pList, pNew) goto _1 _1: ; i++ } if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { v2 = int32(SRT_Table) } else { v2 = int32(SRT_Upfrom) } eDest = v2 } else { if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } pList = _sqlite3ExprListAppend(tls, pParse, pList, _exprRowColumn(tls, pParse, i)) goto _3 _3: ; i++ } eDest = int32(SRT_Table) } else { if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { v4 = int32(SRT_Table) } else { v4 = int32(SRT_Upfrom) } eDest = v4 pList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), _sqlite3PExpr(tls, pParse, int32(TK_ROW), uintptr(0), uintptr(0))) } } _ = libc.Int32FromInt32(0) if pChanges != 0 { i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pChanges)).FnExpr) { break } pList = _sqlite3ExprListAppend(tls, pParse, pList, _sqlite3ExprDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FpExpr, 0)) goto _5 _5: ; i++ } } pSelect = _sqlite3SelectNew(tls, pParse, pList, pSrc, pWhere2, pGrp, uintptr(0), pOrderBy2, uint32(libc.Int32FromInt32(SF_UFSrcCheck)|libc.Int32FromInt32(SF_IncludeHidden)|libc.Int32FromInt32(SF_UpdateFrom)), pLimit2) if pSelect != 0 { *(*Tu32)(unsafe.Pointer(pSelect + 4)) |= uint32(SF_OrderByReqd) } _sqlite3SelectDestInit(tls, bp, eDest, iEph) if pPk != 0 { v6 = int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol) } else { v6 = -int32(1) } (*(*TSelectDest)(unsafe.Pointer(bp))).FiSDParm2 = v6 _sqlite3Select(tls, pParse, pSelect, bp) _sqlite3SelectDelete(tls, db, pSelect) } // C documentation // // /* // ** Process an UPDATE statement. // ** // ** UPDATE OR IGNORE tbl SET a=b, c=d FROM tbl2... WHERE e<5 AND f NOT NULL; // ** \_______/ \_/ \______/ \_____/ \________________/ // ** onError | pChanges | pWhere // ** \_______________________/ // ** pTabList // */ func _sqlite3Update(tls *libc.TLS, pParse uintptr, pTabList uintptr, pChanges uintptr, pWhere uintptr, onError int32, pOrderBy uintptr, pLimit uintptr, pUpsert uintptr) { bp := tls.Alloc(96) defer tls.Free(96) var aRegIdx, aToOpen, aXRef, db, pIdx, pKeyInfo, pPk, pRowidExpr, pTab, pTrigger, pWInfo, v, v11, v16, v19, v21, v24, v26, v29, v31, v34, v37, v39, v4, v5 uintptr var addrOnce, addrOpen, addrTop, bFinishSeek, bProgress, eOnePass, flags, hasFK, i, iBaseCur, iCur, iDataCur, iDb, iEph, iIdxCur, iPk, iRowidExpr, isView, j, k, labelBreak, labelContinue, nAllIdx, nChangeFrom, nEphCol, nIdx, nKey, nOff, newmask, rc, reg, regKey, regNew, regNewRowid, regOld, regOldRowid, regRowCount, regRowSet, v1, v15, v18, v2, v20, v22, v23, v25, v27, v28, v3, v30, v32, v33, v35, v36, v38, v41, v46, v48, v49 int32 var chngKey, chngPk, chngRowid, hCol, v8 Tu8 var colFlags, oldmask Tu32 var nPk Ti16 var v13 uint64 var v43 uint32 var _ /* aiCurOnePass at bp+48 */ [2]int32 var _ /* bReplace at bp+56 */ int32 var _ /* iNotUsed1 at bp+60 */ int32 var _ /* iNotUsed2 at bp+64 */ int32 var _ /* sContext at bp+0 */ TAuthContext var _ /* sNC at bp+8 */ TNameContext var _ /* tmask at bp+44 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aRegIdx, aToOpen, aXRef, addrOnce, addrOpen, addrTop, bFinishSeek, bProgress, chngKey, chngPk, chngRowid, colFlags, db, eOnePass, flags, hCol, hasFK, i, iBaseCur, iCur, iDataCur, iDb, iEph, iIdxCur, iPk, iRowidExpr, isView, j, k, labelBreak, labelContinue, nAllIdx, nChangeFrom, nEphCol, nIdx, nKey, nOff, nPk, newmask, oldmask, pIdx, pKeyInfo, pPk, pRowidExpr, pTab, pTrigger, pWInfo, rc, reg, regKey, regNew, regNewRowid, regOld, regOldRowid, regRowCount, regRowSet, v, v1, v11, v13, v15, v16, v18, v19, v2, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v3, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v4, v41, v43, v46, v48, v49, v5, v8 /* The table to be updated */ addrTop = 0 /* VDBE instruction address of the start of the loop */ pWInfo = uintptr(0) /* The database structure */ aRegIdx = uintptr(0) /* Registers for to each index and the main table */ aXRef = uintptr(0) /* Either chngPk or chngRowid */ pRowidExpr = uintptr(0) /* Expression defining the new record number */ iRowidExpr = -int32(1) /* Mask of NEW.* columns accessed by BEFORE triggers */ iEph = 0 /* Ephemeral table holding all primary key values */ nKey = 0 /* The write cursors opened by WHERE_ONEPASS */ addrOpen = 0 /* Address of OP_OpenEphemeral */ iPk = 0 /* First of nPk cells holding PRIMARY KEY value */ nPk = 0 /* Number of components of the PRIMARY KEY */ *(*int32)(unsafe.Pointer(bp + 56)) = 0 /* True if REPLACE conflict resolution might happen */ bFinishSeek = int32(1) /* The OP_FinishSeek opcode is needed */ nChangeFrom = 0 /* If there is a FROM, pChanges->nExpr, else 0 */ /* Register Allocations */ regRowCount = 0 /* A count of rows changed */ regOldRowid = 0 /* The old rowid */ regNewRowid = 0 /* The new rowid */ regNew = 0 /* Content of the NEW.* table in triggers */ regOld = 0 /* Content of OLD.* table in triggers */ regRowSet = 0 /* Rowset of rows to be updated */ regKey = 0 /* composite PRIMARY KEY value */ libc.Xmemset(tls, bp, 0, uint32(8)) db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto update_cleanup } _ = libc.Int32FromInt32(0) /* Locate the table which we want to update. */ pTab = _sqlite3SrcListLookup(tls, pParse, pTabList) if pTab == uintptr(0) { goto update_cleanup } iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTable)(unsafe.Pointer(pTab)).FpSchema) /* Figure out if we have any triggers and if the table being ** updated is a view. */ pTrigger = _sqlite3TriggersExist(tls, pParse, pTab, int32(TK_UPDATE), pChanges, bp+44) isView = libc.BoolInt32(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW)) _ = libc.Int32FromInt32(0) /* If there was a FROM clause, set nChangeFrom to the number of expressions ** in the change-list. Otherwise, set it to 0. There cannot be a FROM ** clause if this function is being called to generate code for part of ** an UPSERT statement. */ if (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc > int32(1) { v1 = (*TExprList)(unsafe.Pointer(pChanges)).FnExpr } else { v1 = 0 } nChangeFrom = v1 _ = libc.Int32FromInt32(0) if _sqlite3ViewGetColumnNames(tls, pParse, pTab) != 0 { goto update_cleanup } if _sqlite3IsReadOnly(tls, pParse, pTab, pTrigger) != 0 { goto update_cleanup } /* Allocate a cursors for the main database table and for all indices. ** The index cursors might not be used, but if they are used they ** need to occur right after the database cursor. So go ahead and ** allocate enough space, just in case. */ v4 = pParse + 40 v3 = *(*int32)(unsafe.Pointer(v4)) *(*int32)(unsafe.Pointer(v4))++ v2 = v3 iDataCur = v2 iBaseCur = v2 iIdxCur = iDataCur + int32(1) if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { v5 = uintptr(0) } else { v5 = _sqlite3PrimaryKeyIndex(tls, pTab) } pPk = v5 nIdx = 0 pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0) { break } if pPk == pIdx { iDataCur = (*TParse)(unsafe.Pointer(pParse)).FnTab } (*TParse)(unsafe.Pointer(pParse)).FnTab++ goto _6 _6: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext nIdx++ } if pUpsert != 0 { /* On an UPSERT, reuse the same cursors already opened by INSERT */ iDataCur = (*TUpsert)(unsafe.Pointer(pUpsert)).FiDataCur iIdxCur = (*TUpsert)(unsafe.Pointer(pUpsert)).FiIdxCur (*TParse)(unsafe.Pointer(pParse)).FnTab = iBaseCur } (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FiCursor = iDataCur /* Allocate space for aXRef[], aRegIdx[], and aToOpen[]. ** Initialize aXRef[] and aToOpen[] to their default values. */ aXRef = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(4)*uint32(int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+nIdx+libc.Int32FromInt32(1))+uint32(nIdx)+uint32(2))) if aXRef == uintptr(0) { goto update_cleanup } aRegIdx = aXRef + uintptr((*TTable)(unsafe.Pointer(pTab)).FnCol)*4 aToOpen = aRegIdx + uintptr(nIdx)*4 + libc.UintptrFromInt32(1)*4 libc.Xmemset(tls, aToOpen, int32(1), uint32(nIdx+int32(1))) *(*Tu8)(unsafe.Pointer(aToOpen + uintptr(nIdx+int32(1)))) = uint8(0) i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } *(*int32)(unsafe.Pointer(aXRef + uintptr(i)*4)) = -int32(1) goto _7 _7: ; i++ } /* Initialize the name-context */ libc.Xmemset(tls, bp+8, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp + 8))).FpParse = pParse (*(*TNameContext)(unsafe.Pointer(bp + 8))).FpSrcList = pTabList *(*uintptr)(unsafe.Pointer(bp + 8 + 8)) = pUpsert (*(*TNameContext)(unsafe.Pointer(bp + 8))).FncFlags = int32(NC_UUpsert) /* Begin generating code. */ v = _sqlite3GetVdbe(tls, pParse) if v == uintptr(0) { goto update_cleanup } /* Resolve the column names in all the expressions of the ** of the UPDATE statement. Also find the column index ** for each column to be updated in the pChanges array. For each ** column to be updated, make sure we have authorization to change ** that column. */ v8 = libc.Uint8FromInt32(0) chngPk = v8 chngRowid = v8 i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pChanges)).FnExpr) { break } hCol = _sqlite3StrIHash(tls, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FzEName) /* If this is an UPDATE with a FROM clause, do not resolve expressions ** here. The call to sqlite3Select() below will do that. */ if nChangeFrom == 0 && _sqlite3ResolveExprNames(tls, bp+8, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FpExpr) != 0 { goto update_cleanup } j = 0 for { if !(j < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FhName) == int32(hCol) && _sqlite3StrICmp(tls, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FzCnName, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FzEName) == 0 { if j == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { chngRowid = uint8(1) pRowidExpr = (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FpExpr iRowidExpr = i } else { if pPk != 0 && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 { chngPk = uint8(1) } else { if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20980, libc.VaList(bp+80, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FzCnName)) goto update_cleanup } } } *(*int32)(unsafe.Pointer(aXRef + uintptr(j)*4)) = i break } goto _10 _10: ; j++ } if j >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { if pPk == uintptr(0) && _sqlite3IsRowid(tls, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FzEName) != 0 { j = -int32(1) chngRowid = uint8(1) pRowidExpr = (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FpExpr iRowidExpr = i } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21016, libc.VaList(bp+80, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FzEName)) (*TParse)(unsafe.Pointer(pParse)).FcheckSchema = uint8(1) goto update_cleanup } } if j < 0 { v11 = __ccgo_ts + 7905 } else { v11 = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FzCnName } rc = _sqlite3AuthCheck(tls, pParse, int32(SQLITE_UPDATE), (*TTable)(unsafe.Pointer(pTab)).FzName, v11, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName) if rc == int32(SQLITE_DENY) { goto update_cleanup } else { if rc == int32(SQLITE_IGNORE) { *(*int32)(unsafe.Pointer(aXRef + uintptr(j)*4)) = -int32(1) } } goto _9 _9: ; i++ } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) chngKey = uint8(int32(chngRowid) + int32(chngPk)) /* Mark generated columns as changing if their generator expressions ** reference any changing column. The actual aXRef[] value for ** generated expressions is not used, other than to check to see that it ** is non-negative, so the value of aXRef[] for generated columns can be ** set to any non-negative number. We use 99999 so that the value is ** obvious when looking at aXRef[] in a symbolic debugger. */ if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != 0 { for cond := true; cond; cond = bProgress != 0 { bProgress = 0 i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if *(*int32)(unsafe.Pointer(aXRef + uintptr(i)*4)) >= 0 { goto _12 } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_GENERATED) == 0 { goto _12 } if _sqlite3ExprReferencesUpdatedColumn(tls, _sqlite3ColumnExpr(tls, pTab, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(i)*12), aXRef, int32(chngRowid)) != 0 { *(*int32)(unsafe.Pointer(aXRef + uintptr(i)*4)) = int32(99999) bProgress = int32(1) } goto _12 _12: ; i++ } } } /* The SET expressions are not actually used inside the WHERE loop. ** So reset the colUsed mask. Unless this is a virtual table. In that ** case, set all bits of the colUsed mask (to ensure that the virtual ** table implementation makes all columns available). */ if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { v13 = uint64(-libc.Int32FromInt32(1)) } else { v13 = uint64(0) } (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FcolUsed = v13 hasFK = _sqlite3FkRequired(tls, pParse, pTab, aXRef, int32(chngKey)) /* There is one entry in the aRegIdx[] array for each index on the table ** being updated. Fill in aRegIdx[] with a register number that will hold ** the key for accessing each index. */ if onError == int32(OE_Replace) { *(*int32)(unsafe.Pointer(bp + 56)) = int32(1) } nAllIdx = 0 pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0) { break } if chngKey != 0 || hasFK > int32(1) || pIdx == pPk || _indexWhereClauseMightChange(tls, pIdx, aXRef, int32(chngRowid)) != 0 { v16 = pParse + 44 *(*int32)(unsafe.Pointer(v16))++ v15 = *(*int32)(unsafe.Pointer(v16)) reg = v15 *(*int32)(unsafe.Pointer(pParse + 44)) += int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) } else { reg = 0 i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) { break } if _indexColumnIsBeingUpdated(tls, pIdx, i, aXRef, int32(chngRowid)) != 0 { v19 = pParse + 44 *(*int32)(unsafe.Pointer(v19))++ v18 = *(*int32)(unsafe.Pointer(v19)) reg = v18 *(*int32)(unsafe.Pointer(pParse + 44)) += int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) if onError == int32(OE_Default) && int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) == int32(OE_Replace) { *(*int32)(unsafe.Pointer(bp + 56)) = int32(1) } break } goto _17 _17: ; i++ } } if reg == 0 { *(*Tu8)(unsafe.Pointer(aToOpen + uintptr(nAllIdx+int32(1)))) = uint8(0) } *(*int32)(unsafe.Pointer(aRegIdx + uintptr(nAllIdx)*4)) = reg goto _14 _14: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext nAllIdx++ } v21 = pParse + 44 *(*int32)(unsafe.Pointer(v21))++ v20 = *(*int32)(unsafe.Pointer(v21)) *(*int32)(unsafe.Pointer(aRegIdx + uintptr(nAllIdx)*4)) = v20 /* Register storing the table record */ if *(*int32)(unsafe.Pointer(bp + 56)) != 0 { /* If REPLACE conflict resolution might be invoked, open cursors on all ** indexes in case they are needed to delete records. */ libc.Xmemset(tls, aToOpen, int32(1), uint32(nIdx+int32(1))) } if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 { _sqlite3VdbeCountChanges(tls, v) } _sqlite3BeginWriteOperation(tls, pParse, libc.BoolInt32(pTrigger != 0 || hasFK != 0), iDb) /* Allocate required registers. */ if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) { /* For now, regRowSet and aRegIdx[nAllIdx] share the same register. ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be ** reallocated. aRegIdx[nAllIdx] is the register in which the main ** table record is written. regRowSet holds the RowSet for the ** two-pass update algorithm. */ _ = libc.Int32FromInt32(0) regRowSet = *(*int32)(unsafe.Pointer(aRegIdx + uintptr(nAllIdx)*4)) v24 = pParse + 44 *(*int32)(unsafe.Pointer(v24))++ v23 = *(*int32)(unsafe.Pointer(v24)) v22 = v23 regNewRowid = v22 regOldRowid = v22 if chngPk != 0 || pTrigger != 0 || hasFK != 0 { regOld = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += int32((*TTable)(unsafe.Pointer(pTab)).FnCol) } if chngKey != 0 || pTrigger != 0 || hasFK != 0 { v26 = pParse + 44 *(*int32)(unsafe.Pointer(v26))++ v25 = *(*int32)(unsafe.Pointer(v26)) regNewRowid = v25 } regNew = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += int32((*TTable)(unsafe.Pointer(pTab)).FnCol) } /* Start the view context. */ if isView != 0 { _sqlite3AuthContextPush(tls, pParse, bp, (*TTable)(unsafe.Pointer(pTab)).FzName) } /* If we are trying to update a view, realize that view into ** an ephemeral table. */ if nChangeFrom == 0 && isView != 0 { _sqlite3MaterializeView(tls, pParse, pTab, pWhere, pOrderBy, pLimit, iDataCur) pOrderBy = uintptr(0) pLimit = uintptr(0) } /* Resolve the column names in all the expressions in the ** WHERE clause. */ if nChangeFrom == 0 && _sqlite3ResolveExprNames(tls, bp+8, pWhere) != 0 { goto update_cleanup } /* Virtual tables must be handled separately */ if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { _updateVirtualTable(tls, pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef, pWhere, onError) goto update_cleanup } /* Jump to labelBreak to abandon further processing of this UPDATE */ v27 = _sqlite3VdbeMakeLabel(tls, pParse) labelBreak = v27 labelContinue = v27 /* Not an UPSERT. Normal processing. Begin by ** initialize the count of updated rows */ if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00001))<? ** ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI ** strategy that uses an index for which one or more columns are being ** updated. */ eOnePass = _sqlite3WhereOkOnePass(tls, pWInfo, bp+48) bFinishSeek = _sqlite3WhereUsesDeferredSeek(tls, pWInfo) if eOnePass != int32(ONEPASS_SINGLE) { _sqlite3MultiWrite(tls, pParse) if eOnePass == int32(ONEPASS_MULTI) { iCur = (*(*[2]int32)(unsafe.Pointer(bp + 48)))[int32(1)] if iCur >= 0 && iCur != iDataCur && *(*Tu8)(unsafe.Pointer(aToOpen + uintptr(iCur-iBaseCur))) != 0 { eOnePass = ONEPASS_OFF } _ = libc.Int32FromInt32(0) } } } if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF ** mode, write the rowid into the FIFO. In either of the one-pass modes, ** leave it in register regOldRowid. */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iDataCur, regOldRowid) if eOnePass == ONEPASS_OFF { v39 = pParse + 44 *(*int32)(unsafe.Pointer(v39))++ v38 = *(*int32)(unsafe.Pointer(v39)) *(*int32)(unsafe.Pointer(aRegIdx + uintptr(nAllIdx)*4)) = v38 _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iEph, regRowSet, regOldRowid) } else { if addrOpen != 0 { _sqlite3VdbeChangeToNoop(tls, v, addrOpen) } } } else { /* Read the PK of the current row into an array of registers. In ** ONEPASS_OFF mode, serialize the array into a record and store it in ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table ** is not required) and leave the PK fields in the array of registers. */ i = 0 for { if !(i < int32(nPk)) { break } _ = libc.Int32FromInt32(0) _sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iDataCur, int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2))), iPk+i) goto _40 _40: ; i++ } if eOnePass != 0 { if addrOpen != 0 { _sqlite3VdbeChangeToNoop(tls, v, addrOpen) } nKey = int32(nPk) regKey = iPk } else { _sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), iPk, int32(nPk), regKey, _sqlite3IndexAffinityStr(tls, db, pPk), int32(nPk)) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iEph, regKey, iPk, int32(nPk)) } } } if pUpsert == uintptr(0) { if nChangeFrom == 0 && eOnePass != int32(ONEPASS_MULTI) { _sqlite3WhereEnd(tls, pWInfo) } if !(isView != 0) { addrOnce = 0 *(*int32)(unsafe.Pointer(bp + 60)) = 0 *(*int32)(unsafe.Pointer(bp + 64)) = 0 /* Open every index that needs updating. */ if eOnePass != ONEPASS_OFF { if (*(*[2]int32)(unsafe.Pointer(bp + 48)))[0] >= 0 { *(*Tu8)(unsafe.Pointer(aToOpen + uintptr((*(*[2]int32)(unsafe.Pointer(bp + 48)))[0]-iBaseCur))) = uint8(0) } if (*(*[2]int32)(unsafe.Pointer(bp + 48)))[int32(1)] >= 0 { *(*Tu8)(unsafe.Pointer(aToOpen + uintptr((*(*[2]int32)(unsafe.Pointer(bp + 48)))[int32(1)]-iBaseCur))) = uint8(0) } } if eOnePass == int32(ONEPASS_MULTI) && nIdx-libc.BoolInt32((*(*[2]int32)(unsafe.Pointer(bp + 48)))[int32(1)] >= 0) > 0 { addrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once)) } _sqlite3OpenTableAndIndices(tls, pParse, pTab, int32(OP_OpenWrite), uint8(0), iBaseCur, aToOpen, bp+60, bp+64) if addrOnce != 0 { _sqlite3VdbeJumpHereOrPopInst(tls, v, addrOnce) } } /* Top of the update loop */ if eOnePass != ONEPASS_OFF { if (*(*[2]int32)(unsafe.Pointer(bp + 48)))[0] != iDataCur && (*(*[2]int32)(unsafe.Pointer(bp + 48)))[int32(1)] != iDataCur { _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), iDataCur, labelBreak, regKey, nKey) } if eOnePass != int32(ONEPASS_SINGLE) { labelContinue = _sqlite3VdbeMakeLabel(tls, pParse) } if pPk != 0 { v41 = regKey } else { v41 = regOldRowid } _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), v41, labelBreak) } else { if pPk != 0 || nChangeFrom != 0 { labelContinue = _sqlite3VdbeMakeLabel(tls, pParse) _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), iEph, labelBreak) addrTop = _sqlite3VdbeCurrentAddr(tls, v) if nChangeFrom != 0 { if !(isView != 0) { if pPk != 0 { i = 0 for { if !(i < int32(nPk)) { break } _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iEph, i, iPk+i) goto _42 _42: ; i++ } _sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), iDataCur, labelContinue, iPk, int32(nPk)) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iEph, regOldRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_NotExists), iDataCur, labelContinue, regOldRowid) } } } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_RowData), iEph, regKey) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), iDataCur, labelContinue, regKey, 0) } } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), iEph, labelBreak) labelContinue = _sqlite3VdbeMakeLabel(tls, pParse) addrTop = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iEph, regOldRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_NotExists), iDataCur, labelContinue, regOldRowid) } } } /* If the rowid value will change, set register regNewRowid to ** contain the new value. If the rowid is not being modified, ** then regNewRowid is the same register as regOldRowid, which is ** already populated. */ _ = libc.Int32FromInt32(0) if chngRowid != 0 { _ = libc.Int32FromInt32(0) if nChangeFrom == 0 { _sqlite3ExprCode(tls, pParse, pRowidExpr, regNewRowid) } else { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iEph, iRowidExpr, regNewRowid) } _sqlite3VdbeAddOp1(tls, v, int32(OP_MustBeInt), regNewRowid) } /* Compute the old pre-UPDATE content of the row being changed, if that ** information is needed */ if chngPk != 0 || hasFK != 0 || pTrigger != 0 { if hasFK != 0 { v43 = _sqlite3FkOldmask(tls, pParse, pTab) } else { v43 = uint32(0) } oldmask = v43 oldmask |= _sqlite3TriggerColmask(tls, pParse, pTrigger, pChanges, 0, libc.Int32FromInt32(TRIGGER_BEFORE)|libc.Int32FromInt32(TRIGGER_AFTER), pTab, onError) i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } colFlags = uint32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags) k = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(i))) + regOld if oldmask == uint32(0xffffffff) || i < int32(32) && oldmask&(libc.Uint32FromInt32(1)<= 0 { if nChangeFrom != 0 { if isView != 0 { v46 = int32((*TTable)(unsafe.Pointer(pTab)).FnCol) } else { v46 = int32(nPk) } nOff = v46 _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iEph, nOff+j, k) } else { _sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(j)*20))).FpExpr, k) } } else { if 0 == *(*int32)(unsafe.Pointer(bp + 44))&int32(TRIGGER_BEFORE) || i > int32(31) || uint32(newmask)&(libc.Uint32FromInt32(1)< int32(1) || chngKey != 0 { v48 = 0 } else { v48 = int32(OPFLAG_ISNOOP) } _sqlite3VdbeAddOp3(tls, v, int32(OP_Delete), iDataCur, int32(OPFLAG_ISUPDATE)|v48, regNewRowid) if eOnePass == int32(ONEPASS_MULTI) { _ = libc.Int32FromInt32(0) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_SAVEPOSITION)) } if !((*TParse)(unsafe.Pointer(pParse)).Fnested != 0) { _sqlite3VdbeAppendP4(tls, v, pTab, -int32(5)) } if hasFK != 0 { _sqlite3FkCheck(tls, pParse, pTab, 0, regNewRowid, aXRef, int32(chngKey)) } /* Insert the new index entries and the new record. */ if eOnePass == int32(ONEPASS_MULTI) { v49 = int32(OPFLAG_SAVEPOSITION) } else { v49 = 0 } _sqlite3CompleteInsertion(tls, pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx, int32(OPFLAG_ISUPDATE)|v49, 0, 0) /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to ** handle rows (possibly in other tables) that refer via a foreign key ** to the row just updated. */ if hasFK != 0 { _sqlite3FkActions(tls, pParse, pTab, pChanges, regOldRowid, aXRef, int32(chngKey)) } } /* Increment the row counter */ if regRowCount != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), regRowCount, int32(1)) } if pTrigger != 0 { _sqlite3CodeRowTrigger(tls, pParse, pTrigger, int32(TK_UPDATE), pChanges, int32(TRIGGER_AFTER), pTab, regOldRowid, onError, labelContinue) } /* Repeat the above with the next record to be updated, until ** all record selected by the WHERE clause have been updated. */ if eOnePass == int32(ONEPASS_SINGLE) { /* Nothing to do at end-of-loop for a single-pass */ } else { if eOnePass == int32(ONEPASS_MULTI) { _sqlite3VdbeResolveLabel(tls, v, labelContinue) _sqlite3WhereEnd(tls, pWInfo) } else { _sqlite3VdbeResolveLabel(tls, v, labelContinue) _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iEph, addrTop) } } _sqlite3VdbeResolveLabel(tls, v, labelBreak) /* Update the sqlite_sequence table by storing the content of the ** maximum rowid counter values recorded while inserting into ** autoincrement tables. */ if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 && (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab == uintptr(0) && pUpsert == uintptr(0) { _sqlite3AutoincrementEnd(tls, pParse) } /* ** Return the number of rows that were changed, if we are tracking ** that information. */ if regRowCount != 0 { _sqlite3CodeChangeCount(tls, v, regRowCount, __ccgo_ts+21035) } goto update_cleanup update_cleanup: ; _sqlite3AuthContextPop(tls, bp) _sqlite3DbFree(tls, db, aXRef) /* Also frees aRegIdx[] and aToOpen[] */ _sqlite3SrcListDelete(tls, db, pTabList) _sqlite3ExprListDelete(tls, db, pChanges) _sqlite3ExprDelete(tls, db, pWhere) return } /* Make sure "isView" and other macros defined above are undefined. Otherwise ** they may interfere with compilation of other functions in this file ** (or in another file, if this file becomes part of the amalgamation). */ // C documentation // // /* // ** Generate code for an UPDATE of a virtual table. // ** // ** There are two possible strategies - the default and the special // ** "onepass" strategy. Onepass is only used if the virtual table // ** implementation indicates that pWhere may match at most one row. // ** // ** The default strategy is to create an ephemeral table that contains // ** for each row to be changed: // ** // ** (A) The original rowid of that row. // ** (B) The revised rowid for the row. // ** (C) The content of every column in the row. // ** // ** Then loop through the contents of this ephemeral table executing a // ** VUpdate for each row. When finished, drop the ephemeral table. // ** // ** The "onepass" strategy does not use an ephemeral table. Instead, it // ** stores the same values (A, B and C above) in a register array and // ** makes a single invocation of VUpdate. // */ func _updateVirtualTable(tls *libc.TLS, pParse uintptr, pSrc uintptr, pTab uintptr, pChanges uintptr, pRowid uintptr, aXRef uintptr, pWhere uintptr, onError int32) { bp := tls.Alloc(16) defer tls.Free(16) var addr, eOnePass, ephemTab, i, iCsr, nArg, regArg, regRec, regRowid, v1, v10, v4, v6 int32 var db, pList, pPk, pPk1, pRow, pRowExpr, pVTab, pWInfo, v, v2, v5, v7 uintptr var iPk, iPk1 Ti16 var _ /* aDummy at bp+0 */ [2]int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, db, eOnePass, ephemTab, i, iCsr, iPk, iPk1, nArg, pList, pPk, pPk1, pRow, pRowExpr, pVTab, pWInfo, regArg, regRec, regRowid, v, v1, v10, v2, v4, v5, v6, v7 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* Loop counter */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Database connection */ pVTab = _sqlite3GetVTable(tls, db, pTab) pWInfo = uintptr(0) nArg = int32(2) + int32((*TTable)(unsafe.Pointer(pTab)).FnCol) /* Register for ephemeral table rowid */ iCsr = (*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FiCursor /* Address of OP_OpenEphemeral */ /* Allocate nArg registers in which to gather the arguments for VUpdate. Then ** create and open the ephemeral table in which the records created from ** these arguments will be temporarily stored. */ _ = libc.Int32FromInt32(0) v2 = pParse + 40 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ ephemTab = v1 addr = _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), ephemTab, nArg) regArg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nArg if (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc > int32(1) { pPk = uintptr(0) if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { if pRowid != 0 { pRow = _sqlite3ExprDup(tls, db, pRowid, 0) } else { pRow = _sqlite3PExpr(tls, pParse, int32(TK_ROW), uintptr(0), uintptr(0)) } } else { /* PRIMARY KEY column */ pPk = _sqlite3PrimaryKeyIndex(tls, pTab) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) iPk = *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn)) if *(*int32)(unsafe.Pointer(aXRef + uintptr(iPk)*4)) >= 0 { pRow = _sqlite3ExprDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(*(*int32)(unsafe.Pointer(aXRef + uintptr(iPk)*4)))*20))).FpExpr, 0) } else { pRow = _exprRowColumn(tls, pParse, int32(iPk)) } } pList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), pRow) i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if *(*int32)(unsafe.Pointer(aXRef + uintptr(i)*4)) >= 0 { pList = _sqlite3ExprListAppend(tls, pParse, pList, _sqlite3ExprDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(*(*int32)(unsafe.Pointer(aXRef + uintptr(i)*4)))*20))).FpExpr, 0)) } else { pRowExpr = _exprRowColumn(tls, pParse, i) if pRowExpr != 0 { (*TExpr)(unsafe.Pointer(pRowExpr)).Fop2 = uint8(OPFLAG_NOCHNG) } pList = _sqlite3ExprListAppend(tls, pParse, pList, pRowExpr) } goto _3 _3: ; i++ } _updateFromSelect(tls, pParse, ephemTab, pPk, pList, pSrc, pWhere, uintptr(0), uintptr(0)) _sqlite3ExprListDelete(tls, db, pList) eOnePass = ONEPASS_OFF } else { v5 = pParse + 44 *(*int32)(unsafe.Pointer(v5))++ v4 = *(*int32)(unsafe.Pointer(v5)) regRec = v4 v7 = pParse + 44 *(*int32)(unsafe.Pointer(v7))++ v6 = *(*int32)(unsafe.Pointer(v7)) regRowid = v6 /* Start scanning the virtual table */ pWInfo = _sqlite3WhereBegin(tls, pParse, pSrc, pWhere, uintptr(0), uintptr(0), uintptr(0), uint16(WHERE_ONEPASS_DESIRED), 0) if pWInfo == uintptr(0) { return } /* Populate the argument registers. */ i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(aXRef + uintptr(i)*4)) >= 0 { _sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(*(*int32)(unsafe.Pointer(aXRef + uintptr(i)*4)))*20))).FpExpr, regArg+int32(2)+i) } else { _sqlite3VdbeAddOp3(tls, v, int32(OP_VColumn), iCsr, i, regArg+int32(2)+i) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_NOCHNG)) /* For sqlite3_vtab_nochange() */ } goto _8 _8: ; i++ } if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iCsr, regArg) if pRowid != 0 { _sqlite3ExprCode(tls, pParse, pRowid, regArg+int32(1)) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iCsr, regArg+int32(1)) } } else { /* PRIMARY KEY column */ pPk1 = _sqlite3PrimaryKeyIndex(tls, pTab) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) iPk1 = *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk1)).FaiColumn)) _sqlite3VdbeAddOp3(tls, v, int32(OP_VColumn), iCsr, int32(iPk1), regArg) _sqlite3VdbeAddOp2(tls, v, int32(OP_SCopy), regArg+int32(2)+int32(iPk1), regArg+int32(1)) } eOnePass = _sqlite3WhereOkOnePass(tls, pWInfo, bp) /* There is no ONEPASS_MULTI on virtual tables */ _ = libc.Int32FromInt32(0) if eOnePass != 0 { /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded ** above. */ _sqlite3VdbeChangeToNoop(tls, v, addr) _sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iCsr) } else { /* Create a record from the argument register contents and insert it into ** the ephemeral table. */ _sqlite3MultiWrite(tls, pParse) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regArg, nArg, regRec) _sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), ephemTab, regRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), ephemTab, regRec, regRowid) } } if eOnePass == ONEPASS_OFF { /* End the virtual table scan */ if (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc == int32(1) { _sqlite3WhereEnd(tls, pWInfo) } /* Begin scanning through the ephemeral table. */ addr = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), ephemTab) /* Extract arguments from the current row of the ephemeral table and ** invoke the VUpdate method. */ i = 0 for { if !(i < nArg) { break } _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), ephemTab, i, regArg+i) goto _9 _9: ; i++ } } _sqlite3VtabMakeWritable(tls, pParse, pTab) _sqlite3VdbeAddOp4(tls, v, int32(OP_VUpdate), 0, nArg, regArg, pVTab, -int32(11)) if onError == int32(OE_Default) { v10 = int32(OE_Abort) } else { v10 = onError } _sqlite3VdbeChangeP5(tls, v, uint16(v10)) _sqlite3MayAbort(tls, pParse) /* End of the ephemeral table scan. Or, if using the onepass strategy, ** jump to here if the scan visited zero rows. */ if eOnePass == ONEPASS_OFF { _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), ephemTab, addr+int32(1)) _sqlite3VdbeJumpHere(tls, v, addr) _sqlite3VdbeAddOp2(tls, v, int32(OP_Close), ephemTab, 0) } else { _sqlite3WhereEnd(tls, pWInfo) } } /************** End of update.c **********************************************/ /************** Begin file upsert.c ******************************************/ /* ** 2018-04-12 ** ** 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 contains code to implement various aspects of UPSERT ** processing and handling of the Upsert object. */ /* #include "sqliteInt.h" */ // C documentation // // /* // ** Free a list of Upsert objects // */ func _upsertDelete(tls *libc.TLS, db uintptr, p uintptr) { var pNext uintptr _ = pNext for cond := true; cond; cond = p != 0 { pNext = (*TUpsert)(unsafe.Pointer(p)).FpNextUpsert _sqlite3ExprListDelete(tls, db, (*TUpsert)(unsafe.Pointer(p)).FpUpsertTarget) _sqlite3ExprDelete(tls, db, (*TUpsert)(unsafe.Pointer(p)).FpUpsertTargetWhere) _sqlite3ExprListDelete(tls, db, (*TUpsert)(unsafe.Pointer(p)).FpUpsertSet) _sqlite3ExprDelete(tls, db, (*TUpsert)(unsafe.Pointer(p)).FpUpsertWhere) _sqlite3DbFree(tls, db, (*TUpsert)(unsafe.Pointer(p)).FpToFree) _sqlite3DbFree(tls, db, p) p = pNext } } func _sqlite3UpsertDelete(tls *libc.TLS, db uintptr, p uintptr) { if p != 0 { _upsertDelete(tls, db, p) } } // C documentation // // /* // ** Duplicate an Upsert object. // */ func _sqlite3UpsertDup(tls *libc.TLS, db uintptr, p uintptr) (r uintptr) { if p == uintptr(0) { return uintptr(0) } return _sqlite3UpsertNew(tls, db, _sqlite3ExprListDup(tls, db, (*TUpsert)(unsafe.Pointer(p)).FpUpsertTarget, 0), _sqlite3ExprDup(tls, db, (*TUpsert)(unsafe.Pointer(p)).FpUpsertTargetWhere, 0), _sqlite3ExprListDup(tls, db, (*TUpsert)(unsafe.Pointer(p)).FpUpsertSet, 0), _sqlite3ExprDup(tls, db, (*TUpsert)(unsafe.Pointer(p)).FpUpsertWhere, 0), _sqlite3UpsertDup(tls, db, (*TUpsert)(unsafe.Pointer(p)).FpNextUpsert)) } // C documentation // // /* // ** Create a new Upsert object. // */ func _sqlite3UpsertNew(tls *libc.TLS, db uintptr, pTarget uintptr, pTargetWhere uintptr, pSet uintptr, pWhere uintptr, pNext uintptr) (r uintptr) { var pNew uintptr _ = pNew pNew = _sqlite3DbMallocZero(tls, db, uint64(48)) if pNew == uintptr(0) { _sqlite3ExprListDelete(tls, db, pTarget) _sqlite3ExprDelete(tls, db, pTargetWhere) _sqlite3ExprListDelete(tls, db, pSet) _sqlite3ExprDelete(tls, db, pWhere) _sqlite3UpsertDelete(tls, db, pNext) return uintptr(0) } else { (*TUpsert)(unsafe.Pointer(pNew)).FpUpsertTarget = pTarget (*TUpsert)(unsafe.Pointer(pNew)).FpUpsertTargetWhere = pTargetWhere (*TUpsert)(unsafe.Pointer(pNew)).FpUpsertSet = pSet (*TUpsert)(unsafe.Pointer(pNew)).FpUpsertWhere = pWhere (*TUpsert)(unsafe.Pointer(pNew)).FisDoUpdate = libc.BoolUint8(pSet != uintptr(0)) (*TUpsert)(unsafe.Pointer(pNew)).FpNextUpsert = pNext } return pNew } // C documentation // // /* // ** Analyze the ON CONFLICT clause described by pUpsert. Resolve all // ** symbols in the conflict-target. // ** // ** Return SQLITE_OK if everything works, or an error code is something // ** is wrong. // */ func _sqlite3UpsertAnalyzeTarget(tls *libc.TLS, pParse uintptr, pTabList uintptr, pUpsert uintptr, pAll uintptr) (r int32) { bp := tls.Alloc(176) defer tls.Free(176) var iCursor, ii, jj, nClause, nn, rc int32 var pExpr, pIdx, pTab, pTarget, pTerm, v2 uintptr var v3 bool var _ /* sCol at bp+36 */ [2]TExpr var _ /* sNC at bp+0 */ TNameContext var _ /* zWhich at bp+140 */ [16]uint8 _, _, _, _, _, _, _, _, _, _, _, _, _ = iCursor, ii, jj, nClause, nn, pExpr, pIdx, pTab, pTarget, pTerm, rc, v2, v3 /* Index column converted into an Expr */ nClause = 0 /* Counter of ON CONFLICT clauses */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Resolve all symbolic names in the conflict-target clause, which ** includes both the list of columns and the optional partial-index ** WHERE clause. */ libc.Xmemset(tls, bp, 0, uint32(36)) (*(*TNameContext)(unsafe.Pointer(bp))).FpParse = pParse (*(*TNameContext)(unsafe.Pointer(bp))).FpSrcList = pTabList for { if !(pUpsert != 0 && (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTarget != 0) { break } rc = _sqlite3ResolveExprListNames(tls, bp, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTarget) if rc != 0 { return rc } rc = _sqlite3ResolveExprNames(tls, bp, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTargetWhere) if rc != 0 { return rc } /* Check to see if the conflict target matches the rowid. */ pTab = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FpTab pTarget = (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTarget iCursor = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FiCursor if v3 = (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) && (*TExprList)(unsafe.Pointer(pTarget)).FnExpr == int32(1); v3 { v2 = (*(*TExprList_item)(unsafe.Pointer(pTarget + 8))).FpExpr pTerm = v2 } if v3 && int32((*TExpr)(unsafe.Pointer(v2)).Fop) == int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(pTerm)).FiColumn) == -int32(1) { /* The conflict-target is the rowid of the primary table */ _ = libc.Int32FromInt32(0) goto _1 } /* Initialize sCol[0..1] to be an expression parse tree for a ** single column of an index. The sCol[0] node will be the TK_COLLATE ** operator and sCol[1] will be the TK_COLUMN operator. Code below ** will populate the specific collation and column number values ** prior to comparing against the conflict-target expression. */ libc.Xmemset(tls, bp+36, 0, uint32(104)) (*(*[2]TExpr)(unsafe.Pointer(bp + 36)))[0].Fop = uint8(TK_COLLATE) (*(*[2]TExpr)(unsafe.Pointer(bp + 36)))[0].FpLeft = bp + 36 + 1*52 (*(*[2]TExpr)(unsafe.Pointer(bp + 36)))[int32(1)].Fop = uint8(TK_COLUMN) (*(*[2]TExpr)(unsafe.Pointer(bp + 36)))[int32(1)].FiTable = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FiCursor /* Check for matches against other indexes */ pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0) { break } if !(int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != libc.Int32FromInt32(OE_None)) { goto _4 } if (*TExprList)(unsafe.Pointer(pTarget)).FnExpr != int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) { goto _4 } if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 { if (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTargetWhere == uintptr(0) { goto _4 } if _sqlite3ExprCompare(tls, pParse, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTargetWhere, (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere, iCursor) != 0 { goto _4 } } nn = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) ii = 0 for { if !(ii < nn) { break } *(*uintptr)(unsafe.Pointer(bp + 36 + 8)) = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(ii)*4)) if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(ii)*2))) == -int32(2) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pExpr = (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(ii)*20))).FpExpr if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLLATE) { (*(*[2]TExpr)(unsafe.Pointer(bp + 36)))[0].FpLeft = pExpr pExpr = bp + 36 } } else { (*(*[2]TExpr)(unsafe.Pointer(bp + 36)))[0].FpLeft = bp + 36 + 1*52 (*(*[2]TExpr)(unsafe.Pointer(bp + 36)))[int32(1)].FiColumn = *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(ii)*2)) pExpr = bp + 36 } jj = 0 for { if !(jj < nn) { break } if _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pTarget + 8 + uintptr(jj)*20))).FpExpr, pExpr, iCursor) < int32(2) { break /* Column ii of the index matches column jj of target */ } goto _6 _6: ; jj++ } if jj >= nn { /* The target contains no match for column jj of the index */ break } goto _5 _5: ; ii++ } if ii < nn { /* Column ii of the index did not match any term of the conflict target. ** Continue the search with the next index. */ goto _4 } (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertIdx = pIdx if _sqlite3UpsertOfIndex(tls, pAll, pIdx) != pUpsert { /* Really this should be an error. The isDup ON CONFLICT clause will ** never fire. But this problem was not discovered until three years ** after multi-CONFLICT upsert was added, and so we silently ignore ** the problem to prevent breaking applications that might actually ** have redundant ON CONFLICT clauses. */ (*TUpsert)(unsafe.Pointer(pUpsert)).FisDup = uint8(1) } break goto _4 _4: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } if (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertIdx == uintptr(0) { if nClause == 0 && (*TUpsert)(unsafe.Pointer(pUpsert)).FpNextUpsert == uintptr(0) { (*(*[16]uint8)(unsafe.Pointer(bp + 140)))[0] = uint8(0) } else { Xsqlite3_snprintf(tls, int32(16), bp+140, __ccgo_ts+21048, libc.VaList(bp+168, nClause+int32(1))) } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21052, libc.VaList(bp+168, bp+140)) return int32(SQLITE_ERROR) } goto _1 _1: ; pUpsert = (*TUpsert)(unsafe.Pointer(pUpsert)).FpNextUpsert nClause++ } return SQLITE_OK } // C documentation // // /* // ** Return true if pUpsert is the last ON CONFLICT clause with a // ** conflict target, or if pUpsert is followed by another ON CONFLICT // ** clause that targets the INTEGER PRIMARY KEY. // */ func _sqlite3UpsertNextIsIPK(tls *libc.TLS, pUpsert uintptr) (r int32) { var pNext uintptr _ = pNext if pUpsert == uintptr(0) { return 0 } pNext = (*TUpsert)(unsafe.Pointer(pUpsert)).FpNextUpsert for int32(1) != 0 { if pNext == uintptr(0) { return int32(1) } if (*TUpsert)(unsafe.Pointer(pNext)).FpUpsertTarget == uintptr(0) { return int32(1) } if (*TUpsert)(unsafe.Pointer(pNext)).FpUpsertIdx == uintptr(0) { return int32(1) } if !((*TUpsert)(unsafe.Pointer(pNext)).FisDup != 0) { return 0 } pNext = (*TUpsert)(unsafe.Pointer(pNext)).FpNextUpsert } return 0 } // C documentation // // /* // ** Given the list of ON CONFLICT clauses described by pUpsert, and // ** a particular index pIdx, return a pointer to the particular ON CONFLICT // ** clause that applies to the index. Or, if the index is not subject to // ** any ON CONFLICT clause, return NULL. // */ func _sqlite3UpsertOfIndex(tls *libc.TLS, pUpsert uintptr, pIdx uintptr) (r uintptr) { for pUpsert != 0 && (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTarget != uintptr(0) && (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertIdx != pIdx { pUpsert = (*TUpsert)(unsafe.Pointer(pUpsert)).FpNextUpsert } return pUpsert } // C documentation // // /* // ** Generate bytecode that does an UPDATE as part of an upsert. // ** // ** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK. // ** In this case parameter iCur is a cursor open on the table b-tree that // ** currently points to the conflicting table row. Otherwise, if pIdx // ** is not NULL, then pIdx is the constraint that failed and iCur is a // ** cursor points to the conflicting row. // */ func _sqlite3UpsertDoUpdate(tls *libc.TLS, pParse uintptr, pUpsert uintptr, pTab uintptr, pIdx uintptr, iCur int32) { var db, pPk, pSrc, pTop, v uintptr var i, iDataCur, iPk, k, nPk, regRowid int32 _, _, _, _, _, _, _, _, _, _, _ = db, i, iDataCur, iPk, k, nPk, pPk, pSrc, pTop, regRowid, v v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe db = (*TParse)(unsafe.Pointer(pParse)).Fdb pTop = pUpsert _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) iDataCur = (*TUpsert)(unsafe.Pointer(pUpsert)).FiDataCur pUpsert = _sqlite3UpsertOfIndex(tls, pTop, pIdx) if pIdx != 0 && iCur != iDataCur { if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { regRowid = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp2(tls, v, int32(OP_IdxRowid), iCur, regRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_SeekRowid), iDataCur, 0, regRowid) _sqlite3ReleaseTempReg(tls, pParse, regRowid) } else { pPk = _sqlite3PrimaryKeyIndex(tls, pTab) nPk = int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol) iPk = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nPk i = 0 for { if !(i < nPk) { break } _ = libc.Int32FromInt32(0) k = int32(_sqlite3TableColumnToIndex(tls, pIdx, *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2)))) _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iCur, k, iPk+i) goto _1 _1: ; i++ } i = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), iDataCur, 0, iPk, nPk) _sqlite3VdbeAddOp4(tls, v, int32(OP_Halt), int32(SQLITE_CORRUPT), int32(OE_Abort), 0, __ccgo_ts+12470, -int32(1)) _sqlite3MayAbort(tls, pParse) _sqlite3VdbeJumpHere(tls, v, i) } } /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does. ** So we have to make a copy before passing it down into sqlite3Update() */ pSrc = _sqlite3SrcListDup(tls, db, (*TUpsert)(unsafe.Pointer(pTop)).FpUpsertSrc, 0) /* excluded.* columns of type REAL need to be converted to a hard real */ i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).Faffinity) == int32(SQLITE_AFF_REAL) { _sqlite3VdbeAddOp1(tls, v, int32(OP_RealAffinity), (*TUpsert)(unsafe.Pointer(pTop)).FregData+i) } goto _2 _2: ; i++ } _sqlite3Update(tls, pParse, pSrc, _sqlite3ExprListDup(tls, db, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertSet, 0), _sqlite3ExprDup(tls, db, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertWhere, 0), int32(OE_Abort), uintptr(0), uintptr(0), pUpsert) } /************** End of upsert.c **********************************************/ /************** Begin file vacuum.c ******************************************/ /* ** 2003 April 6 ** ** 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 contains code used to implement the VACUUM command. ** ** Most of the code in this file may be omitted by defining the ** SQLITE_OMIT_VACUUM macro. */ /* #include "sqliteInt.h" */ /* #include "vdbeInt.h" */ // C documentation // // /* // ** Execute zSql on database db. // ** // ** If zSql returns rows, then each row will have exactly one // ** column. (This will only happen if zSql begins with "SELECT".) // ** Take each row of result and call execSql() again recursively. // ** // ** The execSqlF() routine does the same thing, except it accepts // ** a format string as its third argument // */ func _execSql(tls *libc.TLS, db uintptr, pzErrMsg uintptr, zSql uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc, v1 int32 var zSubSql uintptr var _ /* pStmt at bp+0 */ uintptr _, _, _ = rc, zSubSql, v1 /* printf("SQL: [%s]\n", zSql); fflush(stdout); */ rc = Xsqlite3_prepare_v2(tls, db, zSql, -int32(1), bp, uintptr(0)) if rc != SQLITE_OK { return rc } for { v1 = Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) rc = v1 if !(int32(SQLITE_ROW) == v1) { break } zSubSql = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) _ = libc.Int32FromInt32(0) /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX, ** or INSERT. Historically there have been attacks that first ** corrupt the sqlite_schema.sql field with other kinds of statements ** then run VACUUM to get those statements to execute at inappropriate ** times. */ if zSubSql != 0 && (libc.Xstrncmp(tls, zSubSql, __ccgo_ts+21125, uint32(3)) == 0 || libc.Xstrncmp(tls, zSubSql, __ccgo_ts+21129, uint32(3)) == 0) { rc = _execSql(tls, db, pzErrMsg, zSubSql) if rc != SQLITE_OK { break } } } _ = libc.Int32FromInt32(0) if rc == int32(SQLITE_DONE) { rc = SQLITE_OK } if rc != 0 { _sqlite3SetString(tls, pzErrMsg, db, Xsqlite3_errmsg(tls, db)) } Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) return rc } func _execSqlF(tls *libc.TLS, db uintptr, pzErrMsg uintptr, zSql uintptr, va uintptr) (r int32) { var ap Tva_list var rc int32 var z uintptr _, _, _ = ap, rc, z ap = va z = _sqlite3VMPrintf(tls, db, zSql, ap) _ = ap if z == uintptr(0) { return int32(SQLITE_NOMEM) } rc = _execSql(tls, db, pzErrMsg, z) _sqlite3DbFree(tls, db, z) return rc } // C documentation // // /* // ** The VACUUM command is used to clean up the database, // ** collapse free space, etc. It is modelled after the VACUUM command // ** in PostgreSQL. The VACUUM command works as follows: // ** // ** (1) Create a new transient database file // ** (2) Copy all content from the database being vacuumed into // ** the new transient database file // ** (3) Copy content from the transient database back into the // ** original database. // ** // ** The transient database requires temporary disk space approximately // ** equal to the size of the original database. The copy operation of // ** step (3) requires additional temporary disk space approximately equal // ** to the size of the original database for the rollback journal. // ** Hence, temporary disk space that is approximately 2x the size of the // ** original database is required. Every page of the database is written // ** approximately 3 times: Once for step (2) and twice for step (3). // ** Two writes per page are required in step (3) because the original // ** database content must be written into the rollback journal prior to // ** overwriting the database with the vacuumed content. // ** // ** Only 1x temporary space and only 1x writes would be required if // ** the copy of step (3) were replaced by deleting the original database // ** and renaming the transient database as the original. But that will // ** not work if other processes are attached to the original database. // ** And a power loss in between deleting the original and renaming the // ** transient would cause the database file to appear to be deleted // ** following reboot. // */ func _sqlite3Vacuum(tls *libc.TLS, pParse uintptr, _pNm uintptr, pInto uintptr) { bp := tls.Alloc(16) defer tls.Free(16) *(*uintptr)(unsafe.Pointer(bp)) = _pNm var iDb, iIntoReg, v1 int32 var v, v2 uintptr _, _, _, _, _ = iDb, iIntoReg, v, v1, v2 v = _sqlite3GetVdbe(tls, pParse) iDb = 0 if v == uintptr(0) { goto build_vacuum_end } if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto build_vacuum_end } if *(*uintptr)(unsafe.Pointer(bp)) != 0 { /* Default behavior: Report an error if the argument to VACUUM is ** not recognized */ iDb = _sqlite3TwoPartName(tls, pParse, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp)), bp) if iDb < 0 { goto build_vacuum_end } } if iDb != int32(1) { iIntoReg = 0 if pInto != 0 && _sqlite3ResolveSelfReference(tls, pParse, uintptr(0), 0, pInto, uintptr(0)) == 0 { v2 = pParse + 44 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) iIntoReg = v1 _sqlite3ExprCode(tls, pParse, pInto, iIntoReg) } _sqlite3VdbeAddOp2(tls, v, int32(OP_Vacuum), iDb, iIntoReg) _sqlite3VdbeUsesBtree(tls, v, iDb) } goto build_vacuum_end build_vacuum_end: ; _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pInto) return } // C documentation // // /* // ** This routine implements the OP_Vacuum opcode of the VDBE. // */ func _sqlite3RunVacuum(tls *libc.TLS, pzErrMsg uintptr, db uintptr, iDb int32, pOut uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var i, isMemDb, nDb, nRes, rc, v1, v2 int32 var id, pDb, pMain, pTemp, zDbMain, zOut uintptr var pgflags, saved_mDbFlags, saved_openFlags Tu32 var saved_flags Tu64 var saved_mTrace Tu8 var saved_nChange, saved_nTotalChange Ti64 var _ /* meta at bp+8 */ Tu32 var _ /* sz at bp+0 */ Ti64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, id, isMemDb, nDb, nRes, pDb, pMain, pTemp, pgflags, rc, saved_flags, saved_mDbFlags, saved_mTrace, saved_nChange, saved_nTotalChange, saved_openFlags, zDbMain, zOut, v1, v2 rc = SQLITE_OK /* Saved trace settings */ pDb = uintptr(0) /* Name of output file */ pgflags = uint32(PAGER_SYNCHRONOUS_OFF) /* sync flags for output db */ if !((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0) { _sqlite3SetString(tls, pzErrMsg, db, __ccgo_ts+21133) return int32(SQLITE_ERROR) /* IMP: R-12218-18073 */ } if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive > int32(1) { _sqlite3SetString(tls, pzErrMsg, db, __ccgo_ts+21173) return int32(SQLITE_ERROR) /* IMP: R-15610-35227 */ } saved_openFlags = (*Tsqlite3)(unsafe.Pointer(db)).FopenFlags if pOut != 0 { if Xsqlite3_value_type(tls, pOut) != int32(SQLITE_TEXT) { _sqlite3SetString(tls, pzErrMsg, db, __ccgo_ts+21216) return int32(SQLITE_ERROR) } zOut = Xsqlite3_value_text(tls, pOut) *(*uint32)(unsafe.Pointer(db + 60)) &= uint32(^libc.Int32FromInt32(SQLITE_OPEN_READONLY)) *(*uint32)(unsafe.Pointer(db + 60)) |= uint32(libc.Int32FromInt32(SQLITE_OPEN_CREATE) | libc.Int32FromInt32(SQLITE_OPEN_READWRITE)) } else { zOut = __ccgo_ts + 1648 } /* Save the current value of the database flags so that it can be ** restored before returning. Then set the writable-schema flag, and ** disable CHECK and foreign key constraints. */ saved_flags = (*Tsqlite3)(unsafe.Pointer(db)).Fflags saved_mDbFlags = (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags saved_nChange = (*Tsqlite3)(unsafe.Pointer(db)).FnChange saved_nTotalChange = (*Tsqlite3)(unsafe.Pointer(db)).FnTotalChange saved_mTrace = (*Tsqlite3)(unsafe.Pointer(db)).FmTrace *(*Tu64)(unsafe.Pointer(db + 32)) |= uint64(libc.Int32FromInt32(SQLITE_WriteSchema) | libc.Int32FromInt32(SQLITE_IgnoreChecks)) *(*Tu32)(unsafe.Pointer(db + 24)) |= uint32(libc.Int32FromInt32(DBFLAG_PreferBuiltin) | libc.Int32FromInt32(DBFLAG_Vacuum)) *(*Tu64)(unsafe.Pointer(db + 32)) &= ^(uint64(libc.Int32FromInt32(SQLITE_ForeignKeys)|libc.Int32FromInt32(SQLITE_ReverseOrder)|libc.Int32FromInt32(SQLITE_Defensive)) | uint64(libc.Int32FromInt32(0x00001))< 0) { rc = int32(SQLITE_ERROR) _sqlite3SetString(tls, pzErrMsg, db, __ccgo_ts+21257) goto end_of_vacuum } *(*Tu32)(unsafe.Pointer(db + 24)) |= uint32(DBFLAG_VacuumInto) /* For a VACUUM INTO, the pager-flags are set to the same values as ** they are for the database being vacuumed, except that PAGER_CACHESPILL ** is always set. */ pgflags = uint32(uint64((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).Fsafety_level) | (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(PAGER_FLAGS_MASK)) } nRes = _sqlite3BtreeGetRequestedReserve(tls, pMain) _sqlite3BtreeSetCacheSize(tls, pTemp, (*TSchema)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema)).Fcache_size) _sqlite3BtreeSetSpillSize(tls, pTemp, _sqlite3BtreeSetSpillSize(tls, pMain, 0)) _sqlite3BtreeSetPagerFlags(tls, pTemp, pgflags|uint32(PAGER_CACHESPILL)) /* Begin a transaction and take an exclusive lock on the main database ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below, ** to ensure that we do not try to change the page-size on a WAL database. */ rc = _execSql(tls, db, pzErrMsg, __ccgo_ts+14879) if rc != SQLITE_OK { goto end_of_vacuum } if pOut == uintptr(0) { v1 = int32(2) } else { v1 = 0 } rc = _sqlite3BtreeBeginTrans(tls, pMain, v1, uintptr(0)) if rc != SQLITE_OK { goto end_of_vacuum } /* Do not attempt to change the page size for a WAL database */ if _sqlite3PagerGetJournalMode(tls, _sqlite3BtreePager(tls, pMain)) == int32(PAGER_JOURNALMODE_WAL) && pOut == uintptr(0) { (*Tsqlite3)(unsafe.Pointer(db)).FnextPagesize = 0 } if _sqlite3BtreeSetPageSize(tls, pTemp, _sqlite3BtreeGetPageSize(tls, pMain), nRes, 0) != 0 || !(isMemDb != 0) && _sqlite3BtreeSetPageSize(tls, pTemp, (*Tsqlite3)(unsafe.Pointer(db)).FnextPagesize, nRes, 0) != 0 || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { rc = int32(SQLITE_NOMEM) goto end_of_vacuum } if int32((*Tsqlite3)(unsafe.Pointer(db)).FnextAutovac) >= 0 { v2 = int32((*Tsqlite3)(unsafe.Pointer(db)).FnextAutovac) } else { v2 = _sqlite3BtreeGetAutoVacuum(tls, pMain) } _sqlite3BtreeSetAutoVacuum(tls, pTemp, v2) /* Query the schema of the main database. Create a mirror schema ** in the temporary database. */ (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(nDb) /* force new CREATE statements into vacuum_db */ rc = _execSqlF(tls, db, pzErrMsg, __ccgo_ts+21284, libc.VaList(bp+24, zDbMain)) if rc != SQLITE_OK { goto end_of_vacuum } rc = _execSqlF(tls, db, pzErrMsg, __ccgo_ts+21392, libc.VaList(bp+24, zDbMain)) if rc != SQLITE_OK { goto end_of_vacuum } (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(0) /* Loop through the tables in the main database. For each, do ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy ** the contents to the temporary database. */ rc = _execSqlF(tls, db, pzErrMsg, __ccgo_ts+21446, libc.VaList(bp+24, zDbMain)) _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(db + 24)) &= uint32(^libc.Int32FromInt32(DBFLAG_Vacuum)) if rc != SQLITE_OK { goto end_of_vacuum } /* Copy the triggers, views, and virtual tables from the main database ** over to the temporary database. None of these objects has any ** associated storage, so all we have to do is copy their entries ** from the schema table. */ rc = _execSqlF(tls, db, pzErrMsg, __ccgo_ts+21597, libc.VaList(bp+24, zDbMain)) if rc != 0 { goto end_of_vacuum } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Copy Btree meta values */ i = 0 for { if !(i < int32(libc.Uint32FromInt64(10)/libc.Uint32FromInt64(1))) { break } /* GetMeta() and UpdateMeta() cannot fail in this context because ** we already have page 1 loaded into cache and marked dirty. */ _sqlite3BtreeGetMeta(tls, pMain, int32(_aCopy[i]), bp+8) rc = _sqlite3BtreeUpdateMeta(tls, pTemp, int32(_aCopy[i]), *(*Tu32)(unsafe.Pointer(bp + 8))+uint32(_aCopy[i+int32(1)])) if rc != SQLITE_OK { goto end_of_vacuum } goto _3 _3: ; i += int32(2) } if pOut == uintptr(0) { rc = _sqlite3BtreeCopyFile(tls, pMain, pTemp) } if rc != SQLITE_OK { goto end_of_vacuum } rc = _sqlite3BtreeCommit(tls, pTemp) if rc != SQLITE_OK { goto end_of_vacuum } if pOut == uintptr(0) { _sqlite3BtreeSetAutoVacuum(tls, pMain, _sqlite3BtreeGetAutoVacuum(tls, pTemp)) } _ = libc.Int32FromInt32(0) if pOut == uintptr(0) { nRes = _sqlite3BtreeGetRequestedReserve(tls, pTemp) rc = _sqlite3BtreeSetPageSize(tls, pMain, _sqlite3BtreeGetPageSize(tls, pTemp), nRes, int32(1)) } goto end_of_vacuum end_of_vacuum: ; /* Restore the original value of db->flags */ (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(0) (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags = saved_mDbFlags (*Tsqlite3)(unsafe.Pointer(db)).Fflags = saved_flags (*Tsqlite3)(unsafe.Pointer(db)).FnChange = saved_nChange (*Tsqlite3)(unsafe.Pointer(db)).FnTotalChange = saved_nTotalChange (*Tsqlite3)(unsafe.Pointer(db)).FmTrace = saved_mTrace _sqlite3BtreeSetPageSize(tls, pMain, -int32(1), 0, int32(1)) /* Currently there is an SQL level transaction open on the vacuum ** database. No locks are held on any other files (since the main file ** was committed at the btree level). So it safe to end the transaction ** by manually setting the autoCommit flag to true and detaching the ** vacuum database. The vacuum_db journal file is deleted when the pager ** is closed by the DETACH. */ (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(1) if pDb != 0 { _sqlite3BtreeClose(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) (*TDb)(unsafe.Pointer(pDb)).FpBt = uintptr(0) (*TDb)(unsafe.Pointer(pDb)).FpSchema = uintptr(0) } /* This both clears the schemas and reduces the size of the db->aDb[] ** array. */ _sqlite3ResetAllSchemasOfConnection(tls, db) return rc } /* At this point, there is a write transaction open on both the ** vacuum database and the main database. Assuming no error occurs, ** both transactions are closed by this block - the main database ** transaction by sqlite3BtreeCopyFile() and the other by an explicit ** call to sqlite3BtreeCommit(). */ /* This array determines which meta meta values are preserved in the ** vacuum. Even entries are the meta value number and odd entries ** are an increment to apply to the meta value after the vacuum. ** The increment is used to increase the schema cookie so that other ** connections to the same database will know to reread the schema. */ var _aCopy = [10]uint8{ 0: uint8(BTREE_SCHEMA_VERSION), 1: uint8(1), 2: uint8(BTREE_DEFAULT_CACHE_SIZE), 4: uint8(BTREE_TEXT_ENCODING), 6: uint8(BTREE_USER_VERSION), 8: uint8(BTREE_APPLICATION_ID), } /************** End of vacuum.c **********************************************/ /************** Begin file vtab.c ********************************************/ /* ** 2006 June 10 ** ** 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 contains code used to help implement virtual tables. */ /* #include "sqliteInt.h" */ /* ** Before a virtual table xCreate() or xConnect() method is invoked, the ** sqlite3.pVtabCtx member variable is set to point to an instance of ** this struct allocated on the stack. It is used by the implementation of ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which ** are invoked only from within xCreate and xConnect methods. */ type TVtabCtx1 = struct { FpVTable uintptr FpTab uintptr FpPrior uintptr FbDeclared int32 } type VtabCtx1 = TVtabCtx1 // C documentation // // /* // ** Construct and install a Module object for a virtual table. When this // ** routine is called, it is guaranteed that all appropriate locks are held // ** and the module is not already part of the connection. // ** // ** If there already exists a module with zName, replace it with the new one. // ** If pModule==0, then delete the module zName if it exists. // */ func _sqlite3VtabCreateModule(tls *libc.TLS, db uintptr, zName uintptr, pModule uintptr, pAux uintptr, xDestroy uintptr) (r uintptr) { var nName int32 var pDel, pMod, zCopy uintptr _, _, _, _ = nName, pDel, pMod, zCopy if pModule == uintptr(0) { zCopy = zName pMod = uintptr(0) } else { nName = _sqlite3Strlen30(tls, zName) pMod = _sqlite3Malloc(tls, uint64(uint32(24)+uint32(nName)+uint32(1))) if pMod == uintptr(0) { _sqlite3OomFault(tls, db) return uintptr(0) } zCopy = pMod + 1*24 libc.Xmemcpy(tls, zCopy, zName, uint32(nName+int32(1))) (*TModule)(unsafe.Pointer(pMod)).FzName = zCopy (*TModule)(unsafe.Pointer(pMod)).FpModule = pModule (*TModule)(unsafe.Pointer(pMod)).FpAux = pAux (*TModule)(unsafe.Pointer(pMod)).FxDestroy = xDestroy (*TModule)(unsafe.Pointer(pMod)).FpEpoTab = uintptr(0) (*TModule)(unsafe.Pointer(pMod)).FnRefModule = int32(1) } pDel = _sqlite3HashInsert(tls, db+404, zCopy, pMod) if pDel != 0 { if pDel == pMod { _sqlite3OomFault(tls, db) _sqlite3DbFree(tls, db, pDel) pMod = uintptr(0) } else { _sqlite3VtabEponymousTableClear(tls, db, pDel) _sqlite3VtabModuleUnref(tls, db, pDel) } } return pMod } // C documentation // // /* // ** The actual function that does the work of creating a new module. // ** This function implements the sqlite3_create_module() and // ** sqlite3_create_module_v2() interfaces. // */ func _createModule(tls *libc.TLS, db uintptr, zName uintptr, pModule uintptr, pAux uintptr, xDestroy uintptr) (r int32) { var rc int32 _ = rc rc = SQLITE_OK Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) _sqlite3VtabCreateModule(tls, db, zName, pModule, pAux, xDestroy) rc = _sqlite3ApiExit(tls, db, rc) if rc != SQLITE_OK && xDestroy != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{xDestroy})))(tls, pAux) } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** External API function used to create a new virtual-table module. // */ func Xsqlite3_create_module(tls *libc.TLS, db uintptr, zName uintptr, pModule uintptr, pAux uintptr) (r int32) { return _createModule(tls, db, zName, pModule, pAux, uintptr(0)) } // C documentation // // /* // ** External API function used to create a new virtual-table module. // */ func Xsqlite3_create_module_v2(tls *libc.TLS, db uintptr, zName uintptr, pModule uintptr, pAux uintptr, xDestroy uintptr) (r int32) { return _createModule(tls, db, zName, pModule, pAux, xDestroy) } // C documentation // // /* // ** External API to drop all virtual-table modules, except those named // ** on the azNames list. // */ func Xsqlite3_drop_modules(tls *libc.TLS, db uintptr, azNames uintptr) (r int32) { var ii int32 var pMod, pNext, pThis uintptr _, _, _, _ = ii, pMod, pNext, pThis pThis = (*THash)(unsafe.Pointer(db + 404)).Ffirst for { if !(pThis != 0) { break } pMod = (*THashElem)(unsafe.Pointer(pThis)).Fdata pNext = (*THashElem)(unsafe.Pointer(pThis)).Fnext if azNames != 0 { ii = 0 for { if !(*(*uintptr)(unsafe.Pointer(azNames + uintptr(ii)*4)) != uintptr(0) && libc.Xstrcmp(tls, *(*uintptr)(unsafe.Pointer(azNames + uintptr(ii)*4)), (*TModule)(unsafe.Pointer(pMod)).FzName) != 0) { break } goto _2 _2: ; ii++ } if *(*uintptr)(unsafe.Pointer(azNames + uintptr(ii)*4)) != uintptr(0) { goto _1 } } _createModule(tls, db, (*TModule)(unsafe.Pointer(pMod)).FzName, uintptr(0), uintptr(0), uintptr(0)) goto _1 _1: ; pThis = pNext } return SQLITE_OK } // C documentation // // /* // ** Decrement the reference count on a Module object. Destroy the // ** module when the reference count reaches zero. // */ func _sqlite3VtabModuleUnref(tls *libc.TLS, db uintptr, pMod uintptr) { _ = libc.Int32FromInt32(0) (*TModule)(unsafe.Pointer(pMod)).FnRefModule-- if (*TModule)(unsafe.Pointer(pMod)).FnRefModule == 0 { if (*TModule)(unsafe.Pointer(pMod)).FxDestroy != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TModule)(unsafe.Pointer(pMod)).FxDestroy})))(tls, (*TModule)(unsafe.Pointer(pMod)).FpAux) } _ = libc.Int32FromInt32(0) _sqlite3DbFree(tls, db, pMod) } } // C documentation // // /* // ** Lock the virtual table so that it cannot be disconnected. // ** Locks nest. Every lock should have a corresponding unlock. // ** If an unlock is omitted, resources leaks will occur. // ** // ** If a disconnect is attempted while a virtual table is locked, // ** the disconnect is deferred until all locks have been removed. // */ func _sqlite3VtabLock(tls *libc.TLS, pVTab uintptr) { (*TVTable)(unsafe.Pointer(pVTab)).FnRef++ } // C documentation // // /* // ** pTab is a pointer to a Table structure representing a virtual-table. // ** Return a pointer to the VTable object used by connection db to access // ** this virtual-table, if one has been created, or NULL otherwise. // */ func _sqlite3GetVTable(tls *libc.TLS, db uintptr, pTab uintptr) (r uintptr) { var pVtab uintptr _ = pVtab _ = libc.Int32FromInt32(0) pVtab = (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).Fp for { if !(pVtab != 0 && (*TVTable)(unsafe.Pointer(pVtab)).Fdb != db) { break } goto _1 _1: ; pVtab = (*TVTable)(unsafe.Pointer(pVtab)).FpNext } return pVtab } // C documentation // // /* // ** Decrement the ref-count on a virtual table object. When the ref-count // ** reaches zero, call the xDisconnect() method to delete the object. // */ func _sqlite3VtabUnlock(tls *libc.TLS, pVTab uintptr) { var db, p uintptr _, _ = db, p db = (*TVTable)(unsafe.Pointer(pVTab)).Fdb _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TVTable)(unsafe.Pointer(pVTab)).FnRef-- if (*TVTable)(unsafe.Pointer(pVTab)).FnRef == 0 { p = (*TVTable)(unsafe.Pointer(pVTab)).FpVtab if p != 0 { (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer(p)).FpModule)).FxDisconnect})))(tls, p) } _sqlite3VtabModuleUnref(tls, (*TVTable)(unsafe.Pointer(pVTab)).Fdb, (*TVTable)(unsafe.Pointer(pVTab)).FpMod) _sqlite3DbFree(tls, db, pVTab) } } // C documentation // // /* // ** Table p is a virtual table. This function moves all elements in the // ** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated // ** database connections to be disconnected at the next opportunity. // ** Except, if argument db is not NULL, then the entry associated with // ** connection db is left in the p->u.vtab.p list. // */ func _vtabDisconnectAll(tls *libc.TLS, db uintptr, p uintptr) (r uintptr) { var db2, pNext, pRet, pVTable uintptr _, _, _, _ = db2, pNext, pRet, pVTable pRet = uintptr(0) _ = libc.Int32FromInt32(0) pVTable = (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(p + 44))).Fp (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(p + 44))).Fp = uintptr(0) /* Assert that the mutex (if any) associated with the BtShared database ** that contains table p is held by the caller. See header comments ** above function sqlite3VtabUnlockList() for an explanation of why ** this makes it safe to access the sqlite3.pDisconnect list of any ** database connection that may have an entry in the p->u.vtab.p list. */ _ = libc.Int32FromInt32(0) for pVTable != 0 { db2 = (*TVTable)(unsafe.Pointer(pVTable)).Fdb pNext = (*TVTable)(unsafe.Pointer(pVTable)).FpNext _ = libc.Int32FromInt32(0) if db2 == db { pRet = pVTable (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(p + 44))).Fp = pRet (*TVTable)(unsafe.Pointer(pRet)).FpNext = uintptr(0) } else { (*TVTable)(unsafe.Pointer(pVTable)).FpNext = (*Tsqlite3)(unsafe.Pointer(db2)).FpDisconnect (*Tsqlite3)(unsafe.Pointer(db2)).FpDisconnect = pVTable } pVTable = pNext } _ = libc.Int32FromInt32(0) return pRet } // C documentation // // /* // ** Table *p is a virtual table. This function removes the VTable object // ** for table *p associated with database connection db from the linked // ** list in p->pVTab. It also decrements the VTable ref count. This is // ** used when closing database connection db to free all of its VTable // ** objects without disturbing the rest of the Schema object (which may // ** be being used by other shared-cache connections). // */ func _sqlite3VtabDisconnect(tls *libc.TLS, db uintptr, p uintptr) { var pVTab, ppVTab uintptr _, _ = pVTab, ppVTab _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) ppVTab = p + 44 + 8 for { if !(*(*uintptr)(unsafe.Pointer(ppVTab)) != 0) { break } if (*TVTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(ppVTab)))).Fdb == db { pVTab = *(*uintptr)(unsafe.Pointer(ppVTab)) *(*uintptr)(unsafe.Pointer(ppVTab)) = (*TVTable)(unsafe.Pointer(pVTab)).FpNext _sqlite3VtabUnlock(tls, pVTab) break } goto _1 _1: ; ppVTab = *(*uintptr)(unsafe.Pointer(ppVTab)) + 24 } } // C documentation // // /* // ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list. // ** // ** This function may only be called when the mutexes associated with all // ** shared b-tree databases opened using connection db are held by the // ** caller. This is done to protect the sqlite3.pDisconnect list. The // ** sqlite3.pDisconnect list is accessed only as follows: // ** // ** 1) By this function. In this case, all BtShared mutexes and the mutex // ** associated with the database handle itself must be held. // ** // ** 2) By function vtabDisconnectAll(), when it adds a VTable entry to // ** the sqlite3.pDisconnect list. In this case either the BtShared mutex // ** associated with the database the virtual table is stored in is held // ** or, if the virtual table is stored in a non-sharable database, then // ** the database handle mutex is held. // ** // ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously // ** by multiple threads. It is thread-safe. // */ func _sqlite3VtabUnlockList(tls *libc.TLS, db uintptr) { var p, pNext uintptr _, _ = p, pNext p = (*Tsqlite3)(unsafe.Pointer(db)).FpDisconnect _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if p != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FpDisconnect = uintptr(0) for cond := true; cond; cond = p != 0 { pNext = (*TVTable)(unsafe.Pointer(p)).FpNext _sqlite3VtabUnlock(tls, p) p = pNext } } } // C documentation // // /* // ** Clear any and all virtual-table information from the Table record. // ** This routine is called, for example, just before deleting the Table // ** record. // ** // ** Since it is a virtual-table, the Table structure contains a pointer // ** to the head of a linked list of VTable structures. Each VTable // ** structure is associated with a single sqlite3* user of the schema. // ** The reference count of the VTable structure associated with database // ** connection db is decremented immediately (which may lead to the // ** structure being xDisconnected and free). Any other VTable structures // ** in the list are moved to the sqlite3.pDisconnect list of the associated // ** database connection. // */ func _sqlite3VtabClear(tls *libc.TLS, db uintptr, p uintptr) { var i int32 _ = i _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0) { _vtabDisconnectAll(tls, uintptr(0), p) } if (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(p + 44))).FazArg != 0 { i = 0 for { if !(i < (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(p + 44))).FnArg) { break } if i != int32(1) { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(p + 44))).FazArg + uintptr(i)*4))) } goto _1 _1: ; i++ } _sqlite3DbFree(tls, db, (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(p + 44))).FazArg) } } // C documentation // // /* // ** Add a new module argument to pTable->u.vtab.azArg[]. // ** The string is not copied - the pointer is stored. The // ** string will be freed automatically when the table is // ** deleted. // */ func _addModuleArgument(tls *libc.TLS, pParse uintptr, pTable uintptr, zArg uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var azModuleArg, db, v2 uintptr var i, v1 int32 var nBytes Tsqlite3_int64 _, _, _, _, _, _ = azModuleArg, db, i, nBytes, v1, v2 db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) nBytes = int64(uint32(4) * uint32(libc.Int32FromInt32(2)+(*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTable + 44))).FnArg)) if (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTable + 44))).FnArg+int32(3) >= *(*int32)(unsafe.Pointer(db + 120 + 2*4)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12700, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTable)).FzName)) } azModuleArg = _sqlite3DbRealloc(tls, db, (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTable + 44))).FazArg, uint64(nBytes)) if azModuleArg == uintptr(0) { _sqlite3DbFree(tls, db, zArg) } else { v2 = pTable + 44 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ i = v1 *(*uintptr)(unsafe.Pointer(azModuleArg + uintptr(i)*4)) = zArg *(*uintptr)(unsafe.Pointer(azModuleArg + uintptr(i+int32(1))*4)) = uintptr(0) (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTable + 44))).FazArg = azModuleArg } } // C documentation // // /* // ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE // ** statement. The module name has been parsed, but the optional list // ** of parameters that follow the module name are still pending. // */ func _sqlite3VtabBeginParse(tls *libc.TLS, pParse uintptr, pName1 uintptr, pName2 uintptr, pModuleName uintptr, ifNotExists int32) { var db, pTable uintptr var iDb int32 _, _, _ = db, iDb, pTable /* Database connection */ _sqlite3StartTable(tls, pParse, pName1, pName2, 0, 0, int32(1), ifNotExists) pTable = (*TParse)(unsafe.Pointer(pParse)).FpNewTable if pTable == uintptr(0) { return } _ = libc.Int32FromInt32(0) (*TTable)(unsafe.Pointer(pTable)).FeTabType = uint8(TABTYP_VTAB) db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) _addModuleArgument(tls, pParse, pTable, _sqlite3NameFromToken(tls, db, pModuleName)) _addModuleArgument(tls, pParse, pTable, uintptr(0)) _addModuleArgument(tls, pParse, pTable, _sqlite3DbStrDup(tls, db, (*TTable)(unsafe.Pointer(pTable)).FzName)) _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fn = uint32(t__predefined_ptrdiff_t((*TToken)(unsafe.Pointer(pModuleName)).Fz+uintptr((*TToken)(unsafe.Pointer(pModuleName)).Fn)) - int32((*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fz)) /* Creating a virtual table invokes the authorization callback twice. ** The first invocation, to obtain permission to INSERT a row into the ** sqlite_schema table, has already been made by sqlite3StartTable(). ** The second call, to obtain permission to create the table, is made now. */ if (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTable + 44))).FazArg != 0 { iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTable)).FpSchema) _ = libc.Int32FromInt32(0) /* The database the table is being created in */ _sqlite3AuthCheck(tls, pParse, int32(SQLITE_CREATE_VTABLE), (*TTable)(unsafe.Pointer(pTable)).FzName, *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTable + 44))).FazArg)), (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + uintptr(iDb)*16))).FzDbSName) } } // C documentation // // /* // ** This routine takes the module argument that has been accumulating // ** in pParse->zArg[] and appends it to the list of arguments on the // ** virtual table currently under construction in pParse->pTable. // */ func _addArgumentToVtab(tls *libc.TLS, pParse uintptr) { var db, z uintptr var n int32 _, _, _ = db, n, z if (*TParse)(unsafe.Pointer(pParse)).FsArg.Fz != 0 && (*TParse)(unsafe.Pointer(pParse)).FpNewTable != 0 { z = (*TParse)(unsafe.Pointer(pParse)).FsArg.Fz n = int32((*TParse)(unsafe.Pointer(pParse)).FsArg.Fn) db = (*TParse)(unsafe.Pointer(pParse)).Fdb _addModuleArgument(tls, pParse, (*TParse)(unsafe.Pointer(pParse)).FpNewTable, _sqlite3DbStrNDup(tls, db, z, uint64(n))) } } // C documentation // // /* // ** The parser calls this routine after the CREATE VIRTUAL TABLE statement // ** has been completely parsed. // */ func _sqlite3VtabFinishParse(tls *libc.TLS, pParse uintptr, pEnd uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var db, pOld, pSchema, pTab, v, zName, zStmt, zWhere, v2 uintptr var iDb, iReg, v1 int32 _, _, _, _, _, _, _, _, _, _, _, _ = db, iDb, iReg, pOld, pSchema, pTab, v, zName, zStmt, zWhere, v1, v2 pTab = (*TParse)(unsafe.Pointer(pParse)).FpNewTable /* The table being constructed */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* The database connection */ if pTab == uintptr(0) { return } _ = libc.Int32FromInt32(0) _addArgumentToVtab(tls, pParse) (*TParse)(unsafe.Pointer(pParse)).FsArg.Fz = uintptr(0) if (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).FnArg < int32(1) { return } /* If the CREATE VIRTUAL TABLE statement is being entered for the ** first time (in other words if the virtual table is actually being ** created now instead of just being read out of sqlite_schema) then ** do additional initialization work and store the statement text ** in the sqlite_schema table. */ if !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) { _sqlite3MayAbort(tls, pParse) /* Compute the complete text of the CREATE VIRTUAL TABLE statement */ if pEnd != 0 { (*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fn = uint32(int32((*TToken)(unsafe.Pointer(pEnd)).Fz)-int32((*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fz)) + (*TToken)(unsafe.Pointer(pEnd)).Fn } zStmt = _sqlite3MPrintf(tls, db, __ccgo_ts+21727, libc.VaList(bp+8, pParse+200)) /* A slot for the record has already been allocated in the ** schema table. We just need to update that slot with all ** the information we've collected. ** ** The VM register number pParse->regRowid holds the rowid of an ** entry in the sqlite_schema table that was created for this vtab ** by sqlite3StartTable(). */ iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) _sqlite3NestedParse(tls, pParse, __ccgo_ts+21751, libc.VaList(bp+8, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, (*TTable)(unsafe.Pointer(pTab)).FzName, (*TTable)(unsafe.Pointer(pTab)).FzName, zStmt, (*TParse)(unsafe.Pointer(pParse)).FregRowid)) v = _sqlite3GetVdbe(tls, pParse) _sqlite3ChangeCookie(tls, pParse, iDb) _sqlite3VdbeAddOp0(tls, v, int32(OP_Expire)) zWhere = _sqlite3MPrintf(tls, db, __ccgo_ts+21850, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName, zStmt)) _sqlite3VdbeAddParseSchemaOp(tls, v, iDb, zWhere, uint16(0)) _sqlite3DbFree(tls, db, zStmt) v2 = pParse + 44 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) iReg = v1 _sqlite3VdbeLoadString(tls, v, iReg, (*TTable)(unsafe.Pointer(pTab)).FzName) _sqlite3VdbeAddOp2(tls, v, int32(OP_VCreate), iDb, iReg) } else { pSchema = (*TTable)(unsafe.Pointer(pTab)).FpSchema zName = (*TTable)(unsafe.Pointer(pTab)).FzName _ = libc.Int32FromInt32(0) _sqlite3MarkAllShadowTablesOf(tls, db, pTab) pOld = _sqlite3HashInsert(tls, pSchema+8, zName, pTab) if pOld != 0 { _sqlite3OomFault(tls, db) _ = libc.Int32FromInt32(0) /* Malloc must have failed inside HashInsert() */ return } (*TParse)(unsafe.Pointer(pParse)).FpNewTable = uintptr(0) } } // C documentation // // /* // ** The parser calls this routine when it sees the first token // ** of an argument to the module name in a CREATE VIRTUAL TABLE statement. // */ func _sqlite3VtabArgInit(tls *libc.TLS, pParse uintptr) { _addArgumentToVtab(tls, pParse) (*TParse)(unsafe.Pointer(pParse)).FsArg.Fz = uintptr(0) (*TParse)(unsafe.Pointer(pParse)).FsArg.Fn = uint32(0) } // C documentation // // /* // ** The parser calls this routine for each token after the first token // ** in an argument to the module name in a CREATE VIRTUAL TABLE statement. // */ func _sqlite3VtabArgExtend(tls *libc.TLS, pParse uintptr, p uintptr) { var pArg uintptr _ = pArg pArg = pParse + 264 if (*TToken)(unsafe.Pointer(pArg)).Fz == uintptr(0) { (*TToken)(unsafe.Pointer(pArg)).Fz = (*TToken)(unsafe.Pointer(p)).Fz (*TToken)(unsafe.Pointer(pArg)).Fn = (*TToken)(unsafe.Pointer(p)).Fn } else { _ = libc.Int32FromInt32(0) (*TToken)(unsafe.Pointer(pArg)).Fn = uint32(t__predefined_ptrdiff_t((*TToken)(unsafe.Pointer(p)).Fz+uintptr((*TToken)(unsafe.Pointer(p)).Fn)) - int32((*TToken)(unsafe.Pointer(pArg)).Fz)) } } // C documentation // // /* // ** Invoke a virtual table constructor (either xCreate or xConnect). The // ** pointer to the function to invoke is passed as the fourth parameter // ** to this procedure. // */ func _vtabCallConstructor(tls *libc.TLS, db uintptr, pTab uintptr, pMod uintptr, xConstruct uintptr, pzErr uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var azArg, pCtx, pVTable, zFormat, zModuleName, zType, p6 uintptr var i, iCol, iDb, j, nArg, nDel, nType, rc, v4 int32 var oooHidden Tu16 var _ /* sCtx at bp+0 */ TVtabCtx var _ /* zErr at bp+16 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = azArg, i, iCol, iDb, j, nArg, nDel, nType, oooHidden, pCtx, pVTable, rc, zFormat, zModuleName, zType, v4, p6 nArg = (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).FnArg *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) _ = libc.Int32FromInt32(0) azArg = (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).FazArg /* Check that the virtual-table is not already being initialized */ pCtx = (*Tsqlite3)(unsafe.Pointer(db)).FpVtabCtx for { if !(pCtx != 0) { break } if (*TVtabCtx)(unsafe.Pointer(pCtx)).FpTab == pTab { *(*uintptr)(unsafe.Pointer(pzErr)) = _sqlite3MPrintf(tls, db, __ccgo_ts+21869, libc.VaList(bp+32, (*TTable)(unsafe.Pointer(pTab)).FzName)) return int32(SQLITE_LOCKED) } goto _1 _1: ; pCtx = (*TVtabCtx)(unsafe.Pointer(pCtx)).FpPrior } zModuleName = _sqlite3DbStrDup(tls, db, (*TTable)(unsafe.Pointer(pTab)).FzName) if !(zModuleName != 0) { return int32(SQLITE_NOMEM) } pVTable = _sqlite3MallocZero(tls, uint64(28)) if !(pVTable != 0) { _sqlite3OomFault(tls, db) _sqlite3DbFree(tls, db, zModuleName) return int32(SQLITE_NOMEM) } (*TVTable)(unsafe.Pointer(pVTable)).Fdb = db (*TVTable)(unsafe.Pointer(pVTable)).FpMod = pMod (*TVTable)(unsafe.Pointer(pVTable)).FeVtabRisk = uint8(SQLITE_VTABRISK_Normal) iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).FazArg + 1*4)) = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName /* Invoke the virtual table constructor */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*(*TVtabCtx)(unsafe.Pointer(bp))).FpTab = pTab (*(*TVtabCtx)(unsafe.Pointer(bp))).FpVTable = pVTable (*(*TVtabCtx)(unsafe.Pointer(bp))).FpPrior = (*Tsqlite3)(unsafe.Pointer(db)).FpVtabCtx (*(*TVtabCtx)(unsafe.Pointer(bp))).FbDeclared = 0 (*Tsqlite3)(unsafe.Pointer(db)).FpVtabCtx = bp (*TTable)(unsafe.Pointer(pTab)).FnTabRef++ rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xConstruct})))(tls, db, (*TModule)(unsafe.Pointer(pMod)).FpAux, nArg, azArg, pVTable+8, bp+16) _sqlite3DeleteTable(tls, db, pTab) (*Tsqlite3)(unsafe.Pointer(db)).FpVtabCtx = (*(*TVtabCtx)(unsafe.Pointer(bp))).FpPrior if rc == int32(SQLITE_NOMEM) { _sqlite3OomFault(tls, db) } _ = libc.Int32FromInt32(0) if SQLITE_OK != rc { if *(*uintptr)(unsafe.Pointer(bp + 16)) == uintptr(0) { *(*uintptr)(unsafe.Pointer(pzErr)) = _sqlite3MPrintf(tls, db, __ccgo_ts+21911, libc.VaList(bp+32, zModuleName)) } else { *(*uintptr)(unsafe.Pointer(pzErr)) = _sqlite3MPrintf(tls, db, __ccgo_ts+3795, libc.VaList(bp+32, *(*uintptr)(unsafe.Pointer(bp + 16)))) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) } _sqlite3DbFree(tls, db, pVTable) } else { if (*TVTable)(unsafe.Pointer(pVTable)).FpVtab != 0 { /* Justification of ALWAYS(): A correct vtab constructor must allocate ** the sqlite3_vtab object if successful. */ libc.Xmemset(tls, (*TVTable)(unsafe.Pointer(pVTable)).FpVtab, 0, uint32(12)) (*Tsqlite3_vtab)(unsafe.Pointer((*TVTable)(unsafe.Pointer(pVTable)).FpVtab)).FpModule = (*TModule)(unsafe.Pointer(pMod)).FpModule (*TModule)(unsafe.Pointer(pMod)).FnRefModule++ (*TVTable)(unsafe.Pointer(pVTable)).FnRef = int32(1) if (*(*TVtabCtx)(unsafe.Pointer(bp))).FbDeclared == 0 { zFormat = __ccgo_ts + 21941 *(*uintptr)(unsafe.Pointer(pzErr)) = _sqlite3MPrintf(tls, db, zFormat, libc.VaList(bp+32, (*TTable)(unsafe.Pointer(pTab)).FzName)) _sqlite3VtabUnlock(tls, pVTable) rc = int32(SQLITE_ERROR) } else { oooHidden = uint16(0) /* If everything went according to plan, link the new VTable structure ** into the linked list headed by pTab->u.vtab.p. Then loop through the ** columns of the table to see if any of them contain the token "hidden". ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from ** the type string. */ (*TVTable)(unsafe.Pointer(pVTable)).FpNext = (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).Fp (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).Fp = pVTable iCol = 0 for { if !(iCol < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } zType = _sqlite3ColumnType(tls, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(iCol)*12, __ccgo_ts+1648) i = 0 nType = _sqlite3Strlen30(tls, zType) i = 0 for { if !(i < nType) { break } if 0 == Xsqlite3_strnicmp(tls, __ccgo_ts+16585, zType+uintptr(i), int32(6)) && (i == 0 || int32(*(*uint8)(unsafe.Pointer(zType + uintptr(i-int32(1))))) == int32(' ')) && (int32(*(*uint8)(unsafe.Pointer(zType + uintptr(i+int32(6))))) == int32('\000') || int32(*(*uint8)(unsafe.Pointer(zType + uintptr(i+int32(6))))) == int32(' ')) { break } goto _3 _3: ; i++ } if i < nType { if *(*uint8)(unsafe.Pointer(zType + uintptr(i+int32(6)))) != 0 { v4 = int32(1) } else { v4 = 0 } nDel = int32(6) + v4 j = i for { if !(j+nDel <= nType) { break } *(*uint8)(unsafe.Pointer(zType + uintptr(j))) = *(*uint8)(unsafe.Pointer(zType + uintptr(j+nDel))) goto _5 _5: ; j++ } if int32(*(*uint8)(unsafe.Pointer(zType + uintptr(i)))) == int32('\000') && i > 0 { _ = libc.Int32FromInt32(0) *(*uint8)(unsafe.Pointer(zType + uintptr(i-int32(1)))) = uint8('\000') } p6 = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12 + 10 *(*Tu16)(unsafe.Pointer(p6)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p6))) | libc.Int32FromInt32(COLFLAG_HIDDEN)) *(*Tu32)(unsafe.Pointer(pTab + 28)) |= uint32(TF_HasHidden) oooHidden = uint16(TF_OOOHidden) } else { *(*Tu32)(unsafe.Pointer(pTab + 28)) |= uint32(oooHidden) } goto _2 _2: ; iCol++ } } } } _sqlite3DbFree(tls, db, zModuleName) return rc } // C documentation // // /* // ** This function is invoked by the parser to call the xConnect() method // ** of the virtual table pTab. If an error occurs, an error code is returned // ** and an error left in pParse. // ** // ** This call is a no-op if table pTab is not a virtual table. // */ func _sqlite3VtabCallConnect(tls *libc.TLS, pParse uintptr, pTab uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var db, pMod, zMod, zModule uintptr var rc int32 var _ /* zErr at bp+0 */ uintptr _, _, _, _, _ = db, pMod, rc, zMod, zModule db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if _sqlite3GetVTable(tls, db, pTab) != 0 { return SQLITE_OK } /* Locate the required virtual table module */ zMod = *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).FazArg)) pMod = _sqlite3HashFind(tls, db+404, zMod) if !(pMod != 0) { zModule = *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).FazArg)) _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21987, libc.VaList(bp+16, zModule)) rc = int32(SQLITE_ERROR) } else { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) rc = _vtabCallConstructor(tls, db, pTab, pMod, (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxConnect, bp) if rc != SQLITE_OK { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+3795, libc.VaList(bp+16, *(*uintptr)(unsafe.Pointer(bp)))) (*TParse)(unsafe.Pointer(pParse)).Frc = rc } _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp))) } return rc } // C documentation // // /* // ** Grow the db->aVTrans[] array so that there is room for at least one // ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise. // */ func _growVTrans(tls *libc.TLS, db uintptr) (r int32) { var ARRAY_INCR int32 var aVTrans uintptr var nBytes Tsqlite3_int64 _, _, _ = ARRAY_INCR, aVTrans, nBytes ARRAY_INCR = int32(5) /* Grow the sqlite3.aVTrans array if required */ if (*Tsqlite3)(unsafe.Pointer(db)).FnVTrans%ARRAY_INCR == 0 { nBytes = int64(4) * (int64((*Tsqlite3)(unsafe.Pointer(db)).FnVTrans) + int64(ARRAY_INCR)) aVTrans = _sqlite3DbRealloc(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).FaVTrans, uint64(nBytes)) if !(aVTrans != 0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, aVTrans+uintptr((*Tsqlite3)(unsafe.Pointer(db)).FnVTrans)*4, 0, uint32(4)*uint32(ARRAY_INCR)) (*Tsqlite3)(unsafe.Pointer(db)).FaVTrans = aVTrans } return SQLITE_OK } // C documentation // // /* // ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should // ** have already been reserved using growVTrans(). // */ func _addToVTrans(tls *libc.TLS, db uintptr, pVTab uintptr) { var v1 int32 var v2 uintptr _, _ = v1, v2 /* Add pVtab to the end of sqlite3.aVTrans */ v2 = db + 400 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ *(*uintptr)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaVTrans + uintptr(v1)*4)) = pVTab _sqlite3VtabLock(tls, pVTab) } // C documentation // // /* // ** This function is invoked by the vdbe to call the xCreate method // ** of the virtual table named zTab in database iDb. // ** // ** If an error occurs, *pzErr is set to point to an English language // ** description of the error and an SQLITE_XXX error code is returned. // ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr. // */ func _sqlite3VtabCallCreate(tls *libc.TLS, db uintptr, iDb int32, zTab uintptr, pzErr uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pMod, pTab, zMod uintptr var rc int32 _, _, _, _ = pMod, pTab, rc, zMod rc = SQLITE_OK pTab = _sqlite3FindTable(tls, db, zTab, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName) _ = libc.Int32FromInt32(0) /* Locate the required virtual table module */ zMod = *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).FazArg)) pMod = _sqlite3HashFind(tls, db+404, zMod) /* If the module has been registered and includes a Create method, ** invoke it now. If the module has not been registered, return an ** error. Otherwise, do nothing. */ if pMod == uintptr(0) || (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxCreate == uintptr(0) || (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxDestroy == uintptr(0) { *(*uintptr)(unsafe.Pointer(pzErr)) = _sqlite3MPrintf(tls, db, __ccgo_ts+21987, libc.VaList(bp+8, zMod)) rc = int32(SQLITE_ERROR) } else { rc = _vtabCallConstructor(tls, db, pTab, pMod, (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxCreate, pzErr) } /* Justification of ALWAYS(): The xConstructor method is required to ** create a valid sqlite3_vtab if it returns SQLITE_OK. */ if rc == SQLITE_OK && _sqlite3GetVTable(tls, db, pTab) != 0 { rc = _growVTrans(tls, db) if rc == SQLITE_OK { _addToVTrans(tls, db, _sqlite3GetVTable(tls, db, pTab)) } } return rc } // C documentation // // /* // ** This function is used to set the schema of a virtual table. It is only // ** valid to call this function from within the xCreate() or xConnect() of a // ** virtual table module. // */ func Xsqlite3_declare_vtab(tls *libc.TLS, db uintptr, zCreateTable uintptr) (r int32) { bp := tls.Alloc(304) defer tls.Free(304) var initBusy, rc int32 var pCtx, pIdx, pNew, pTab, v2 uintptr var v1 Ti16 var _ /* sParse at bp+0 */ TParse _, _, _, _, _, _, _, _ = initBusy, pCtx, pIdx, pNew, pTab, rc, v1, v2 rc = SQLITE_OK Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) pCtx = (*Tsqlite3)(unsafe.Pointer(db)).FpVtabCtx if !(pCtx != 0) || (*TVtabCtx)(unsafe.Pointer(pCtx)).FbDeclared != 0 { _sqlite3Error(tls, db, _sqlite3MisuseError(tls, int32(155155))) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return _sqlite3MisuseError(tls, int32(155157)) } pTab = (*TVtabCtx)(unsafe.Pointer(pCtx)).FpTab _ = libc.Int32FromInt32(0) _sqlite3ParseObjectInit(tls, bp, db) (*(*TParse)(unsafe.Pointer(bp))).FeParseMode = uint8(PARSE_MODE_DECLARE_VTAB) (*(*TParse)(unsafe.Pointer(bp))).FdisableTriggers = uint8(1) /* We should never be able to reach this point while loading the ** schema. Nevertheless, defend against that (turn off db->init.busy) ** in case a bug arises. */ _ = libc.Int32FromInt32(0) initBusy = int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy = uint8(0) (*(*TParse)(unsafe.Pointer(bp))).FnQueryLoop = int16(1) if SQLITE_OK == _sqlite3RunParser(tls, bp, zCreateTable) && (*(*TParse)(unsafe.Pointer(bp))).FpNewTable != uintptr(0) && !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) && int32((*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewTable)).FeTabType) == TABTYP_NORM { _ = libc.Int32FromInt32(0) if !((*TTable)(unsafe.Pointer(pTab)).FaCol != 0) { pNew = (*(*TParse)(unsafe.Pointer(bp))).FpNewTable (*TTable)(unsafe.Pointer(pTab)).FaCol = (*TTable)(unsafe.Pointer(pNew)).FaCol _sqlite3ExprListDelete(tls, db, (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pNew + 44))).FpDfltList) v1 = (*TTable)(unsafe.Pointer(pNew)).FnCol (*TTable)(unsafe.Pointer(pTab)).FnCol = v1 (*TTable)(unsafe.Pointer(pTab)).FnNVCol = v1 *(*Tu32)(unsafe.Pointer(pTab + 28)) |= (*TTable)(unsafe.Pointer(pNew)).FtabFlags & uint32(libc.Int32FromInt32(TF_WithoutRowid)|libc.Int32FromInt32(TF_NoVisibleRowid)) (*TTable)(unsafe.Pointer(pNew)).FnCol = 0 (*TTable)(unsafe.Pointer(pNew)).FaCol = uintptr(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if !((*TTable)(unsafe.Pointer(pNew)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer((*TVTable)(unsafe.Pointer((*TVtabCtx)(unsafe.Pointer(pCtx)).FpVTable)).FpMod)).FpModule)).FxUpdate != uintptr(0) && int32((*TIndex)(unsafe.Pointer(_sqlite3PrimaryKeyIndex(tls, pNew))).FnKeyCol) != int32(1) { /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0) ** or else must have a single-column PRIMARY KEY */ rc = int32(SQLITE_ERROR) } pIdx = (*TTable)(unsafe.Pointer(pNew)).FpIndex if pIdx != 0 { _ = libc.Int32FromInt32(0) (*TTable)(unsafe.Pointer(pTab)).FpIndex = pIdx (*TTable)(unsafe.Pointer(pNew)).FpIndex = uintptr(0) (*TIndex)(unsafe.Pointer(pIdx)).FpTable = pTab } } (*TVtabCtx)(unsafe.Pointer(pCtx)).FbDeclared = int32(1) } else { if (*(*TParse)(unsafe.Pointer(bp))).FzErrMsg != 0 { v2 = __ccgo_ts + 3795 } else { v2 = uintptr(0) } _sqlite3ErrorWithMsg(tls, db, int32(SQLITE_ERROR), v2, libc.VaList(bp+296, (*(*TParse)(unsafe.Pointer(bp))).FzErrMsg)) _sqlite3DbFree(tls, db, (*(*TParse)(unsafe.Pointer(bp))).FzErrMsg) rc = int32(SQLITE_ERROR) } (*(*TParse)(unsafe.Pointer(bp))).FeParseMode = uint8(PARSE_MODE_NORMAL) if (*(*TParse)(unsafe.Pointer(bp))).FpVdbe != 0 { _sqlite3VdbeFinalize(tls, (*(*TParse)(unsafe.Pointer(bp))).FpVdbe) } _sqlite3DeleteTable(tls, db, (*(*TParse)(unsafe.Pointer(bp))).FpNewTable) _sqlite3ParseObjectReset(tls, bp) (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy = uint8(initBusy) _ = libc.Int32FromInt32(0) rc = _sqlite3ApiExit(tls, db, rc) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** This function is invoked by the vdbe to call the xDestroy method // ** of the virtual table named zTab in database iDb. This occurs // ** when a DROP TABLE is mentioned. // ** // ** This call is a no-op if zTab is not a virtual table. // */ func _sqlite3VtabCallDestroy(tls *libc.TLS, db uintptr, iDb int32, zTab uintptr) (r int32) { var p, pTab, xDestroy uintptr var rc int32 _, _, _, _ = p, pTab, rc, xDestroy rc = SQLITE_OK pTab = _sqlite3FindTable(tls, db, zTab, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName) if pTab != uintptr(0) && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) && (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).Fp != uintptr(0) { p = (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).Fp for { if !(p != 0) { break } _ = libc.Int32FromInt32(0) if (*Tsqlite3_vtab)(unsafe.Pointer((*TVTable)(unsafe.Pointer(p)).FpVtab)).FnRef > 0 { return int32(SQLITE_LOCKED) } goto _1 _1: ; p = (*TVTable)(unsafe.Pointer(p)).FpNext } p = _vtabDisconnectAll(tls, db, pTab) xDestroy = (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer((*TVTable)(unsafe.Pointer(p)).FpMod)).FpModule)).FxDestroy if xDestroy == uintptr(0) { xDestroy = (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer((*TVTable)(unsafe.Pointer(p)).FpMod)).FpModule)).FxDisconnect } _ = libc.Int32FromInt32(0) (*TTable)(unsafe.Pointer(pTab)).FnTabRef++ rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xDestroy})))(tls, (*TVTable)(unsafe.Pointer(p)).FpVtab) /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */ if rc == SQLITE_OK { _ = libc.Int32FromInt32(0) (*TVTable)(unsafe.Pointer(p)).FpVtab = uintptr(0) (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).Fp = uintptr(0) _sqlite3VtabUnlock(tls, p) } _sqlite3DeleteTable(tls, db, pTab) } return rc } // C documentation // // /* // ** This function invokes either the xRollback or xCommit method // ** of each of the virtual tables in the sqlite3.aVTrans array. The method // ** called is identified by the second argument, "offset", which is // ** the offset of the method to call in the sqlite3_module structure. // ** // ** The array is cleared after invoking the callbacks. // */ func _callFinaliser(tls *libc.TLS, db uintptr, offset int32) { var aVTrans, p, pVTab, x uintptr var i int32 _, _, _, _, _ = aVTrans, i, p, pVTab, x if (*Tsqlite3)(unsafe.Pointer(db)).FaVTrans != 0 { aVTrans = (*Tsqlite3)(unsafe.Pointer(db)).FaVTrans (*Tsqlite3)(unsafe.Pointer(db)).FaVTrans = uintptr(0) i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnVTrans) { break } pVTab = *(*uintptr)(unsafe.Pointer(aVTrans + uintptr(i)*4)) p = (*TVTable)(unsafe.Pointer(pVTab)).FpVtab if p != 0 { x = *(*uintptr)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer(p)).FpModule + uintptr(offset))) if x != 0 { (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{x})))(tls, p) } } (*TVTable)(unsafe.Pointer(pVTab)).FiSavepoint = 0 _sqlite3VtabUnlock(tls, pVTab) goto _1 _1: ; i++ } _sqlite3DbFree(tls, db, aVTrans) (*Tsqlite3)(unsafe.Pointer(db)).FnVTrans = 0 } } // C documentation // // /* // ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans // ** array. Return the error code for the first error that occurs, or // ** SQLITE_OK if all xSync operations are successful. // ** // ** If an error message is available, leave it in p->zErrMsg. // */ func _sqlite3VtabSync(tls *libc.TLS, db uintptr, p uintptr) (r int32) { var aVTrans, pVtab, x, v2 uintptr var i, rc int32 var v3 bool _, _, _, _, _, _, _ = aVTrans, i, pVtab, rc, x, v2, v3 rc = SQLITE_OK aVTrans = (*Tsqlite3)(unsafe.Pointer(db)).FaVTrans (*Tsqlite3)(unsafe.Pointer(db)).FaVTrans = uintptr(0) i = 0 for { if !(rc == SQLITE_OK && i < (*Tsqlite3)(unsafe.Pointer(db)).FnVTrans) { break } pVtab = (*TVTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aVTrans + uintptr(i)*4)))).FpVtab if v3 = pVtab != 0; v3 { v2 = (*Tsqlite3_module)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FpModule)).FxSync x = v2 } if v3 && v2 != uintptr(0) { rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{x})))(tls, pVtab) _sqlite3VtabImportErrmsg(tls, p, pVtab) } goto _1 _1: ; i++ } (*Tsqlite3)(unsafe.Pointer(db)).FaVTrans = aVTrans return rc } // C documentation // // /* // ** Invoke the xRollback method of all virtual tables in the // ** sqlite3.aVTrans array. Then clear the array itself. // */ func _sqlite3VtabRollback(tls *libc.TLS, db uintptr) (r int32) { _callFinaliser(tls, db, int32(uint32(libc.UintptrFromInt32(0)+68))) return SQLITE_OK } // C documentation // // /* // ** Invoke the xCommit method of all virtual tables in the // ** sqlite3.aVTrans array. Then clear the array itself. // */ func _sqlite3VtabCommit(tls *libc.TLS, db uintptr) (r int32) { _callFinaliser(tls, db, int32(uint32(libc.UintptrFromInt32(0)+64))) return SQLITE_OK } // C documentation // // /* // ** If the virtual table pVtab supports the transaction interface // ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is // ** not currently open, invoke the xBegin method now. // ** // ** If the xBegin call is successful, place the sqlite3_vtab pointer // ** in the sqlite3.aVTrans array. // */ func _sqlite3VtabBegin(tls *libc.TLS, db uintptr, pVTab uintptr) (r int32) { var i, iSvpt, rc int32 var pModule uintptr _, _, _, _ = i, iSvpt, pModule, rc rc = SQLITE_OK /* Special case: If db->aVTrans is NULL and db->nVTrans is greater ** than zero, then this function is being called from within a ** virtual module xSync() callback. It is illegal to write to ** virtual module tables in this case, so return SQLITE_LOCKED. */ if (*Tsqlite3)(unsafe.Pointer(db)).FnVTrans > 0 && (*Tsqlite3)(unsafe.Pointer(db)).FaVTrans == uintptr(0) { return int32(SQLITE_LOCKED) } if !(pVTab != 0) { return SQLITE_OK } pModule = (*Tsqlite3_vtab)(unsafe.Pointer((*TVTable)(unsafe.Pointer(pVTab)).FpVtab)).FpModule if (*Tsqlite3_module)(unsafe.Pointer(pModule)).FxBegin != 0 { /* If pVtab is already in the aVTrans array, return early */ i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnVTrans) { break } if *(*uintptr)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaVTrans + uintptr(i)*4)) == pVTab { return SQLITE_OK } goto _1 _1: ; i++ } /* Invoke the xBegin method. If successful, add the vtab to the ** sqlite3.aVTrans[] array. */ rc = _growVTrans(tls, db) if rc == SQLITE_OK { rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule)).FxBegin})))(tls, (*TVTable)(unsafe.Pointer(pVTab)).FpVtab) if rc == SQLITE_OK { iSvpt = (*Tsqlite3)(unsafe.Pointer(db)).FnStatement + (*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint _addToVTrans(tls, db, pVTab) if iSvpt != 0 && (*Tsqlite3_module)(unsafe.Pointer(pModule)).FxSavepoint != 0 { (*TVTable)(unsafe.Pointer(pVTab)).FiSavepoint = iSvpt rc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule)).FxSavepoint})))(tls, (*TVTable)(unsafe.Pointer(pVTab)).FpVtab, iSvpt-int32(1)) } } } } return rc } // C documentation // // /* // ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all // ** virtual tables that currently have an open transaction. Pass iSavepoint // ** as the second argument to the virtual table method invoked. // ** // ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is // ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is // ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with // ** an open transaction is invoked. // ** // ** If any virtual table method returns an error code other than SQLITE_OK, // ** processing is abandoned and the error returned to the caller of this // ** function immediately. If all calls to virtual table methods are successful, // ** SQLITE_OK is returned. // */ func _sqlite3VtabSavepoint(tls *libc.TLS, db uintptr, op int32, iSavepoint int32) (r int32) { var i, rc int32 var pMod, pVTab, xMethod uintptr var savedFlags Tu64 _, _, _, _, _, _ = i, pMod, pVTab, rc, savedFlags, xMethod rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FaVTrans != 0 { i = 0 for { if !(rc == SQLITE_OK && i < (*Tsqlite3)(unsafe.Pointer(db)).FnVTrans) { break } pVTab = *(*uintptr)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaVTrans + uintptr(i)*4)) pMod = (*TModule)(unsafe.Pointer((*TVTable)(unsafe.Pointer(pVTab)).FpMod)).FpModule if (*TVTable)(unsafe.Pointer(pVTab)).FpVtab != 0 && (*Tsqlite3_module)(unsafe.Pointer(pMod)).FiVersion >= int32(2) { _sqlite3VtabLock(tls, pVTab) switch op { case SAVEPOINT_BEGIN: xMethod = (*Tsqlite3_module)(unsafe.Pointer(pMod)).FxSavepoint (*TVTable)(unsafe.Pointer(pVTab)).FiSavepoint = iSavepoint + int32(1) case int32(SAVEPOINT_ROLLBACK): xMethod = (*Tsqlite3_module)(unsafe.Pointer(pMod)).FxRollbackTo default: xMethod = (*Tsqlite3_module)(unsafe.Pointer(pMod)).FxRelease break } if xMethod != 0 && (*TVTable)(unsafe.Pointer(pVTab)).FiSavepoint > iSavepoint { savedFlags = (*Tsqlite3)(unsafe.Pointer(db)).Fflags & libc.Uint64FromInt32(SQLITE_Defensive) *(*Tu64)(unsafe.Pointer(db + 32)) &= ^libc.Uint64FromInt32(SQLITE_Defensive) rc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{xMethod})))(tls, (*TVTable)(unsafe.Pointer(pVTab)).FpVtab, iSavepoint) *(*Tu64)(unsafe.Pointer(db + 32)) |= savedFlags } _sqlite3VtabUnlock(tls, pVTab) } goto _1 _1: ; i++ } } return rc } // C documentation // // /* // ** The first parameter (pDef) is a function implementation. The // ** second parameter (pExpr) is the first argument to this function. // ** If pExpr is a column in a virtual table, then let the virtual // ** table implementation have an opportunity to overload the function. // ** // ** This routine is used to allow virtual table implementations to // ** overload MATCH, LIKE, GLOB, and REGEXP operators. // ** // ** Return either the pDef argument (indicating no change) or a // ** new FuncDef structure that is marked as ephemeral using the // ** SQLITE_FUNC_EPHEM flag. // */ func _sqlite3VtabOverloadFunction(tls *libc.TLS, db uintptr, pDef uintptr, nArg int32, pExpr uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var pMod, pNew, pTab, pVtab uintptr var rc int32 var _ /* pArg at bp+4 */ uintptr var _ /* xSFunc at bp+0 */ uintptr _, _, _, _, _ = pMod, pNew, pTab, pVtab, rc *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) rc = 0 /* Check to see the left operand is a column in a virtual table */ if pExpr == uintptr(0) { return pDef } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLUMN) { return pDef } _ = libc.Int32FromInt32(0) pTab = (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab if pTab == uintptr(0) { return pDef } if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) { return pDef } pVtab = (*TVTable)(unsafe.Pointer(_sqlite3GetVTable(tls, db, pTab))).FpVtab _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pMod = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FpModule if (*Tsqlite3_module)(unsafe.Pointer(pMod)).FxFindFunction == uintptr(0) { return pDef } /* Call the xFindFunction method on the virtual table implementation ** to see if the implementation wants to overload this function. ** ** Though undocumented, we have historically always invoked xFindFunction ** with an all lower-case function name. Continue in this tradition to ** avoid any chance of an incompatibility. */ rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pMod)).FxFindFunction})))(tls, pVtab, nArg, (*TFuncDef)(unsafe.Pointer(pDef)).FzName, bp, bp+4) if rc == 0 { return pDef } /* Create a new ephemeral function definition for the overloaded ** function */ pNew = _sqlite3DbMallocZero(tls, db, uint64(uint32(40)+uint32(_sqlite3Strlen30(tls, (*TFuncDef)(unsafe.Pointer(pDef)).FzName))+uint32(1))) if pNew == uintptr(0) { return pDef } *(*TFuncDef)(unsafe.Pointer(pNew)) = *(*TFuncDef)(unsafe.Pointer(pDef)) (*TFuncDef)(unsafe.Pointer(pNew)).FzName = pNew + 1*40 libc.Xmemcpy(tls, pNew+1*40, (*TFuncDef)(unsafe.Pointer(pDef)).FzName, uint32(_sqlite3Strlen30(tls, (*TFuncDef)(unsafe.Pointer(pDef)).FzName)+int32(1))) (*TFuncDef)(unsafe.Pointer(pNew)).FxSFunc = *(*uintptr)(unsafe.Pointer(bp)) (*TFuncDef)(unsafe.Pointer(pNew)).FpUserData = *(*uintptr)(unsafe.Pointer(bp + 4)) *(*Tu32)(unsafe.Pointer(pNew + 4)) |= uint32(SQLITE_FUNC_EPHEM) return pNew } // C documentation // // /* // ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[] // ** array so that an OP_VBegin will get generated for it. Add pTab to the // ** array if it is missing. If pTab is already in the array, this routine // ** is a no-op. // */ func _sqlite3VtabMakeWritable(tls *libc.TLS, pParse uintptr, pTab uintptr) { var apVtabLock, pToplevel, v1, v4 uintptr var i, n, v3 int32 _, _, _, _, _, _, _ = apVtabLock, i, n, pToplevel, v1, v3, v4 if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 { v1 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel } else { v1 = pParse } pToplevel = v1 _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*TParse)(unsafe.Pointer(pToplevel)).FnVtabLock) { break } if pTab == *(*uintptr)(unsafe.Pointer((*TParse)(unsafe.Pointer(pToplevel)).FapVtabLock + uintptr(i)*4)) { return } goto _2 _2: ; i++ } n = int32(uint32((*TParse)(unsafe.Pointer(pToplevel)).FnVtabLock+libc.Int32FromInt32(1)) * uint32(4)) apVtabLock = _sqlite3Realloc(tls, (*TParse)(unsafe.Pointer(pToplevel)).FapVtabLock, uint64(n)) if apVtabLock != 0 { (*TParse)(unsafe.Pointer(pToplevel)).FapVtabLock = apVtabLock v4 = pToplevel + 224 v3 = *(*int32)(unsafe.Pointer(v4)) *(*int32)(unsafe.Pointer(v4))++ *(*uintptr)(unsafe.Pointer((*TParse)(unsafe.Pointer(pToplevel)).FapVtabLock + uintptr(v3)*4)) = pTab } else { _sqlite3OomFault(tls, (*TParse)(unsafe.Pointer(pToplevel)).Fdb) } } // C documentation // // /* // ** Check to see if virtual table module pMod can be have an eponymous // ** virtual table instance. If it can, create one if one does not already // ** exist. Return non-zero if either the eponymous virtual table instance // ** exists when this routine returns or if an attempt to create it failed // ** and an error message was left in pParse. // ** // ** An eponymous virtual table instance is one that is named after its // ** module, and more importantly, does not require a CREATE VIRTUAL TABLE // ** statement in order to come into existence. Eponymous virtual table // ** instances always exist. They cannot be DROP-ed. // ** // ** Any virtual table module for which xConnect and xCreate are the same // ** method can have an eponymous virtual table instance. // */ func _sqlite3VtabEponymousTableInit(tls *libc.TLS, pParse uintptr, pMod uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var db, pModule, pTab uintptr var rc int32 var _ /* zErr at bp+0 */ uintptr _, _, _, _ = db, pModule, pTab, rc pModule = (*TModule)(unsafe.Pointer(pMod)).FpModule *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*TModule)(unsafe.Pointer(pMod)).FpEpoTab != 0 { return int32(1) } if (*Tsqlite3_module)(unsafe.Pointer(pModule)).FxCreate != uintptr(0) && (*Tsqlite3_module)(unsafe.Pointer(pModule)).FxCreate != (*Tsqlite3_module)(unsafe.Pointer(pModule)).FxConnect { return 0 } pTab = _sqlite3DbMallocZero(tls, db, uint64(64)) if pTab == uintptr(0) { return 0 } (*TTable)(unsafe.Pointer(pTab)).FzName = _sqlite3DbStrDup(tls, db, (*TModule)(unsafe.Pointer(pMod)).FzName) if (*TTable)(unsafe.Pointer(pTab)).FzName == uintptr(0) { _sqlite3DbFree(tls, db, pTab) return 0 } (*TModule)(unsafe.Pointer(pMod)).FpEpoTab = pTab (*TTable)(unsafe.Pointer(pTab)).FnTabRef = uint32(1) (*TTable)(unsafe.Pointer(pTab)).FeTabType = uint8(TABTYP_VTAB) (*TTable)(unsafe.Pointer(pTab)).FpSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema _ = libc.Int32FromInt32(0) (*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(-int32(1)) *(*Tu32)(unsafe.Pointer(pTab + 28)) |= uint32(TF_Eponymous) _addModuleArgument(tls, pParse, pTab, _sqlite3DbStrDup(tls, db, (*TTable)(unsafe.Pointer(pTab)).FzName)) _addModuleArgument(tls, pParse, pTab, uintptr(0)) _addModuleArgument(tls, pParse, pTab, _sqlite3DbStrDup(tls, db, (*TTable)(unsafe.Pointer(pTab)).FzName)) rc = _vtabCallConstructor(tls, db, pTab, pMod, (*Tsqlite3_module)(unsafe.Pointer(pModule)).FxConnect, bp) if rc != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+3795, libc.VaList(bp+16, *(*uintptr)(unsafe.Pointer(bp)))) _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp))) _sqlite3VtabEponymousTableClear(tls, db, pMod) } return int32(1) } // C documentation // // /* // ** Erase the eponymous virtual table instance associated with // ** virtual table module pMod, if it exists. // */ func _sqlite3VtabEponymousTableClear(tls *libc.TLS, db uintptr, pMod uintptr) { var pTab uintptr _ = pTab pTab = (*TModule)(unsafe.Pointer(pMod)).FpEpoTab if pTab != uintptr(0) { /* Mark the table as Ephemeral prior to deleting it, so that the ** sqlite3DeleteTable() routine will know that it is not stored in ** the schema. */ *(*Tu32)(unsafe.Pointer(pTab + 28)) |= uint32(TF_Ephemeral) _sqlite3DeleteTable(tls, db, pTab) (*TModule)(unsafe.Pointer(pMod)).FpEpoTab = uintptr(0) } } // C documentation // // /* // ** Return the ON CONFLICT resolution mode in effect for the virtual // ** table update operation currently in progress. // ** // ** The results of this routine are undefined unless it is called from // ** within an xUpdate method. // */ func Xsqlite3_vtab_on_conflict(tls *libc.TLS, db uintptr) (r int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return int32(_aMap[int32((*Tsqlite3)(unsafe.Pointer(db)).FvtabOnConflict)-int32(1)]) } var _aMap = [5]uint8{ 0: uint8(SQLITE_ROLLBACK), 1: uint8(SQLITE_ABORT), 2: uint8(SQLITE_FAIL), 3: uint8(SQLITE_IGNORE), 4: uint8(SQLITE_REPLACE), } // C documentation // // /* // ** Call from within the xCreate() or xConnect() methods to provide // ** the SQLite core with additional information about the behavior // ** of the virtual table being implemented. // */ func Xsqlite3_vtab_config(tls *libc.TLS, db uintptr, op int32, va uintptr) (r int32) { var ap Tva_list var p uintptr var rc int32 _, _, _ = ap, p, rc rc = SQLITE_OK Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) p = (*Tsqlite3)(unsafe.Pointer(db)).FpVtabCtx if !(p != 0) { rc = _sqlite3MisuseError(tls, int32(155651)) } else { _ = libc.Int32FromInt32(0) ap = va switch op { case int32(SQLITE_VTAB_CONSTRAINT_SUPPORT): (*TVTable)(unsafe.Pointer((*TVtabCtx)(unsafe.Pointer(p)).FpVTable)).FbConstraint = uint8(libc.VaInt32(&ap)) case int32(SQLITE_VTAB_INNOCUOUS): (*TVTable)(unsafe.Pointer((*TVtabCtx)(unsafe.Pointer(p)).FpVTable)).FeVtabRisk = uint8(SQLITE_VTABRISK_Low) case int32(SQLITE_VTAB_DIRECTONLY): (*TVTable)(unsafe.Pointer((*TVtabCtx)(unsafe.Pointer(p)).FpVTable)).FeVtabRisk = uint8(SQLITE_VTABRISK_High) case int32(SQLITE_VTAB_USES_ALL_SCHEMAS): (*TVTable)(unsafe.Pointer((*TVtabCtx)(unsafe.Pointer(p)).FpVTable)).FbAllSchemas = uint8(1) default: rc = _sqlite3MisuseError(tls, int32(155673)) break } _ = ap } if rc != SQLITE_OK { _sqlite3Error(tls, db, rc) } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } /************** End of vtab.c ************************************************/ /************** Begin file wherecode.c ***************************************/ /* ** 2015-06-06 ** ** 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 module contains C code that generates VDBE code used to process ** the WHERE clause of SQL statements. ** ** This file was split off from where.c on 2015-06-06 in order to reduce the ** size of where.c and make it easier to edit. This file contains the routines ** that actually generate the bulk of the WHERE loop code. The original where.c ** file retains the code that does query planning and analysis. */ /* #include "sqliteInt.h" */ /************** Include whereInt.h in the middle of wherecode.c **************/ /************** Begin file whereInt.h ****************************************/ /* ** 2013-11-12 ** ** 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 contains structure and macro definitions for the query ** planner logic in "where.c". These definitions are broken out into ** a separate source file for easier editing. */ // C documentation // // /* Forward references // */ type TWhereClause = struct { F__ccgo_align [0]uint32 FpWInfo uintptr FpOuter uintptr Fop Tu8 FhasOr Tu8 FnTerm int32 FnSlot int32 FnBase int32 Fa uintptr F__ccgo_align8 [4]byte FaStatic [8]TWhereTerm } type WhereClause = TWhereClause type TWhereMaskSet = struct { FbVarSelect int32 Fn int32 Fix [64]int32 } type WhereMaskSet = TWhereMaskSet type TWhereOrInfo = struct { F__ccgo_align [0]uint32 Fwc TWhereClause Findexable TBitmask } type WhereOrInfo = TWhereOrInfo type TWhereAndInfo = struct { F__ccgo_align [0]uint32 Fwc TWhereClause } type WhereAndInfo = TWhereAndInfo type TWhereLevel = struct { F__ccgo_align [0]uint32 FiLeftJoin int32 FiTabCur int32 FiIdxCur int32 FaddrBrk int32 FaddrNxt int32 FaddrSkip int32 FaddrCont int32 FaddrFirst int32 FaddrBody int32 FregBignull int32 FaddrBignull int32 FregFilter int32 FpRJ uintptr FiFrom Tu8 Fop Tu8 Fp3 Tu8 Fp5 Tu8 Fp1 int32 Fp2 int32 Fu struct { FpCoveringIdx [0]uintptr Fin struct { FnIn int32 FaInLoop uintptr } } FpWLoop uintptr F__ccgo_align21 [4]byte FnotReady TBitmask } type WhereLevel = TWhereLevel type TWhereLoop = struct { F__ccgo_align [0]uint32 Fprereq TBitmask FmaskSelf TBitmask FiTab Tu8 FiSortIdx Tu8 FrSetup TLogEst FrRun TLogEst FnOut TLogEst Fu struct { Fvtab [0]struct { FidxNum int32 F__ccgo4 uint8 FisOrdered Ti8 FomitMask Tu16 FidxStr uintptr FmHandleIn Tu32 } Fbtree struct { FnEq Tu16 FnBtm Tu16 FnTop Tu16 FnDistinctCol Tu16 FpIndex uintptr } F__ccgo_pad2 [4]byte } FwsFlags Tu32 FnLTerm Tu16 FnSkip Tu16 FnLSlot Tu16 FaLTerm uintptr FpNextLoop uintptr FaLTermSpace [3]uintptr } type WhereLoop = TWhereLoop type TWherePath = struct { F__ccgo_align [0]uint32 FmaskLoop TBitmask FrevLoop TBitmask FnRow TLogEst FrCost TLogEst FrUnsorted TLogEst FisOrdered Ti8 FaLoop uintptr F__ccgo_pad7 [4]byte } type WherePath = TWherePath type TWhereTerm = struct { F__ccgo_align [0]uint32 FpExpr uintptr FpWC uintptr FtruthProb TLogEst FwtFlags Tu16 FeOperator Tu16 FnChild Tu8 FeMatchOp Tu8 FiParent int32 FleftCursor int32 Fu struct { FpOrInfo [0]uintptr FpAndInfo [0]uintptr Fx struct { FleftColumn int32 FiField int32 } } FprereqRight TBitmask FprereqAll TBitmask } type WhereTerm = TWhereTerm type TWhereLoopBuilder = struct { FpWInfo uintptr FpWC uintptr FpNew uintptr FpOrSet uintptr FpRec uintptr FnRecValid int32 FbldFlags1 uint8 FbldFlags2 uint8 FiPlanLimit uint32 } type WhereLoopBuilder = TWhereLoopBuilder type TWhereScan = struct { FpOrigWC uintptr FpWC uintptr FzCollName uintptr FpIdxExpr uintptr Fk int32 FopMask Tu32 Fidxaff uint8 FiEquiv uint8 FnEquiv uint8 FaiCur [11]int32 FaiColumn [11]Ti16 } type WhereScan = TWhereScan type TWhereOrCost = struct { F__ccgo_align [0]uint32 Fprereq TBitmask FrRun TLogEst FnOut TLogEst F__ccgo_pad3 [4]byte } type WhereOrCost = TWhereOrCost type TWhereOrSet = struct { F__ccgo_align [0]uint32 Fn Tu16 F__ccgo_align1 [6]byte Fa [3]TWhereOrCost } type WhereOrSet = TWhereOrSet type TWhereMemBlock = struct { F__ccgo_align [0]uint32 FpNext uintptr F__ccgo_align1 [4]byte Fsz Tu64 } type WhereMemBlock = TWhereMemBlock type TWhereRightJoin = struct { FiMatch int32 FregBloom int32 FregReturn int32 FaddrSubrtn int32 FendSubrtn int32 } type WhereRightJoin = TWhereRightJoin /* ** This object is a header on a block of allocated memory that will be ** automatically freed when its WInfo object is destructed. */ type TWhereMemBlock1 = struct { F__ccgo_align [0]uint32 FpNext uintptr F__ccgo_align1 [4]byte Fsz Tu64 } type WhereMemBlock1 = TWhereMemBlock1 /* ** Extra information attached to a WhereLevel that is a RIGHT JOIN. */ type TWhereRightJoin1 = struct { FiMatch int32 FregBloom int32 FregReturn int32 FaddrSubrtn int32 FendSubrtn int32 } type WhereRightJoin1 = TWhereRightJoin1 /* ** This object contains information needed to implement a single nested ** loop in WHERE clause. ** ** Contrast this object with WhereLoop. This object describes the ** implementation of the loop. WhereLoop describes the algorithm. ** This object contains a pointer to the WhereLoop algorithm as one of ** its elements. ** ** The WhereInfo object contains a single instance of this object for ** each term in the FROM clause (which is to say, for each of the ** nested loops as implemented). The order of WhereLevel objects determines ** the loop nested order, with WhereInfo.a[0] being the outer loop and ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop. */ type TWhereLevel1 = struct { F__ccgo_align [0]uint32 FiLeftJoin int32 FiTabCur int32 FiIdxCur int32 FaddrBrk int32 FaddrNxt int32 FaddrSkip int32 FaddrCont int32 FaddrFirst int32 FaddrBody int32 FregBignull int32 FaddrBignull int32 FregFilter int32 FpRJ uintptr FiFrom Tu8 Fop Tu8 Fp3 Tu8 Fp5 Tu8 Fp1 int32 Fp2 int32 Fu struct { FpCoveringIdx [0]uintptr Fin struct { FnIn int32 FaInLoop uintptr } } FpWLoop uintptr F__ccgo_align21 [4]byte FnotReady TBitmask } type WhereLevel1 = TWhereLevel1 /* ** Each instance of this object represents an algorithm for evaluating one ** term of a join. Every term of the FROM clause will have at least ** one corresponding WhereLoop object (unless INDEXED BY constraints ** prevent a query solution - which is an error) and many terms of the ** FROM clause will have multiple WhereLoop objects, each describing a ** potential way of implementing that FROM-clause term, together with ** dependencies and cost estimates for using the chosen algorithm. ** ** Query planning consists of building up a collection of these WhereLoop ** objects, then computing a particular sequence of WhereLoop objects, with ** one WhereLoop object per FROM clause term, that satisfy all dependencies ** and that minimize the overall cost. */ type TWhereLoop1 = struct { F__ccgo_align [0]uint32 Fprereq TBitmask FmaskSelf TBitmask FiTab Tu8 FiSortIdx Tu8 FrSetup TLogEst FrRun TLogEst FnOut TLogEst Fu struct { Fvtab [0]struct { FidxNum int32 F__ccgo4 uint8 FisOrdered Ti8 FomitMask Tu16 FidxStr uintptr FmHandleIn Tu32 } Fbtree struct { FnEq Tu16 FnBtm Tu16 FnTop Tu16 FnDistinctCol Tu16 FpIndex uintptr } F__ccgo_pad2 [4]byte } FwsFlags Tu32 FnLTerm Tu16 FnSkip Tu16 FnLSlot Tu16 FaLTerm uintptr FpNextLoop uintptr FaLTermSpace [3]uintptr } type WhereLoop1 = TWhereLoop1 /* This object holds the prerequisites and the cost of running a ** subquery on one operand of an OR operator in the WHERE clause. ** See WhereOrSet for additional information */ type TWhereOrCost1 = struct { F__ccgo_align [0]uint32 Fprereq TBitmask FrRun TLogEst FnOut TLogEst F__ccgo_pad3 [4]byte } type WhereOrCost1 = TWhereOrCost1 /* The WhereOrSet object holds a set of possible WhereOrCosts that ** correspond to the subquery(s) of OR-clause processing. Only the ** best N_OR_COST elements are retained. */ type TWhereOrSet1 = struct { F__ccgo_align [0]uint32 Fn Tu16 F__ccgo_align1 [6]byte Fa [3]TWhereOrCost } type WhereOrSet1 = TWhereOrSet1 /* ** Each instance of this object holds a sequence of WhereLoop objects ** that implement some or all of a query plan. ** ** Think of each WhereLoop object as a node in a graph with arcs ** showing dependencies and costs for travelling between nodes. (That is ** not a completely accurate description because WhereLoop costs are a ** vector, not a scalar, and because dependencies are many-to-one, not ** one-to-one as are graph nodes. But it is a useful visualization aid.) ** Then a WherePath object is a path through the graph that visits some ** or all of the WhereLoop objects once. ** ** The "solver" works by creating the N best WherePath objects of length ** 1. Then using those as a basis to compute the N best WherePath objects ** of length 2. And so forth until the length of WherePaths equals the ** number of nodes in the FROM clause. The best (lowest cost) WherePath ** at the end is the chosen query plan. */ type TWherePath1 = struct { F__ccgo_align [0]uint32 FmaskLoop TBitmask FrevLoop TBitmask FnRow TLogEst FrCost TLogEst FrUnsorted TLogEst FisOrdered Ti8 FaLoop uintptr F__ccgo_pad7 [4]byte } type WherePath1 = TWherePath1 /* ** The query generator uses an array of instances of this structure to ** help it analyze the subexpressions of the WHERE clause. Each WHERE ** clause subexpression is separated from the others by AND operators, ** usually, or sometimes subexpressions separated by OR. ** ** All WhereTerms are collected into a single WhereClause structure. ** The following identity holds: ** ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm ** ** When a term is of the form: ** ** X ** ** where X is a column name and is one of certain operators, ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the ** cursor number and column number for X. WhereTerm.eOperator records ** the using a bitmask encoding defined by WO_xxx below. The ** use of a bitmask encoding for the operator allows us to search ** quickly for terms that match any of several different operators. ** ** A WhereTerm might also be two or more subterms connected by OR: ** ** (t1.X ) OR (t1.Y ) OR .... ** ** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR ** and the WhereTerm.u.pOrInfo field points to auxiliary information that ** is collected about the OR clause. ** ** If a term in the WHERE clause does not match either of the two previous ** categories, then eOperator==0. The WhereTerm.pExpr field is still set ** to the original subexpression content and wtFlags is set up appropriately ** but no other fields in the WhereTerm object are meaningful. ** ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers, ** but they do so indirectly. A single WhereMaskSet structure translates ** cursor number into bits and the translated bit is stored in the prereq ** fields. The translation is used in order to maximize the number of ** bits that will fit in a Bitmask. The VDBE cursor numbers might be ** spread out over the non-negative integers. For example, the cursor ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet ** translates these sparse cursor numbers into consecutive integers ** beginning with 0 in order to make the best possible use of the available ** bits in the Bitmask. So, in the example above, the cursor numbers ** would be mapped into integers 0 through 7. ** ** The number of terms in a join is limited by the number of bits ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite ** is only able to process joins with 64 or fewer tables. */ type TWhereTerm1 = struct { F__ccgo_align [0]uint32 FpExpr uintptr FpWC uintptr FtruthProb TLogEst FwtFlags Tu16 FeOperator Tu16 FnChild Tu8 FeMatchOp Tu8 FiParent int32 FleftCursor int32 Fu struct { FpOrInfo [0]uintptr FpAndInfo [0]uintptr Fx struct { FleftColumn int32 FiField int32 } } FprereqRight TBitmask FprereqAll TBitmask } type WhereTerm1 = TWhereTerm1 /* ** Allowed values of WhereTerm.wtFlags */ /* ** An instance of the WhereScan object is used as an iterator for locating ** terms in the WHERE clause that are useful to the query planner. */ type TWhereScan1 = struct { FpOrigWC uintptr FpWC uintptr FzCollName uintptr FpIdxExpr uintptr Fk int32 FopMask Tu32 Fidxaff uint8 FiEquiv uint8 FnEquiv uint8 FaiCur [11]int32 FaiColumn [11]Ti16 } type WhereScan1 = TWhereScan1 /* ** An instance of the following structure holds all information about a ** WHERE clause. Mostly this is a container for one or more WhereTerms. ** ** Explanation of pOuter: For a WHERE clause of the form ** ** a AND ((b AND c) OR (d AND e)) AND f ** ** There are separate WhereClause objects for the whole clause and for ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the ** subclauses points to the WhereClause object for the whole clause. */ type TWhereClause1 = struct { F__ccgo_align [0]uint32 FpWInfo uintptr FpOuter uintptr Fop Tu8 FhasOr Tu8 FnTerm int32 FnSlot int32 FnBase int32 Fa uintptr F__ccgo_align8 [4]byte FaStatic [8]TWhereTerm } type WhereClause1 = TWhereClause1 /* ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to ** a dynamically allocated instance of the following structure. */ type TWhereOrInfo1 = struct { F__ccgo_align [0]uint32 Fwc TWhereClause Findexable TBitmask } type WhereOrInfo1 = TWhereOrInfo1 /* ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to ** a dynamically allocated instance of the following structure. */ type TWhereAndInfo1 = struct { F__ccgo_align [0]uint32 Fwc TWhereClause } type WhereAndInfo1 = TWhereAndInfo1 /* ** An instance of the following structure keeps track of a mapping ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. ** ** The VDBE cursor numbers are small integers contained in ** SrcItem.iCursor and Expr.iTable fields. For any given WHERE ** clause, the cursor numbers might not begin with 0 and they might ** contain gaps in the numbering sequence. But we want to make maximum ** use of the bits in our bitmasks. This structure provides a mapping ** from the sparse cursor numbers into consecutive integers beginning ** with 0. ** ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<3, 5->1, 8->2, 29->0, ** 57->5, 73->4. Or one of 719 other combinations might be used. It ** does not really matter. What is important is that sparse cursor ** numbers all get mapped into bit numbers that begin with 0 and contain ** no gaps. */ type TWhereMaskSet1 = struct { FbVarSelect int32 Fn int32 Fix [64]int32 } type WhereMaskSet1 = TWhereMaskSet1 /* ** This object is a convenience wrapper holding all information needed ** to construct WhereLoop objects for a particular query. */ type TWhereLoopBuilder1 = struct { FpWInfo uintptr FpWC uintptr FpNew uintptr FpOrSet uintptr FpRec uintptr FnRecValid int32 FbldFlags1 uint8 FbldFlags2 uint8 FiPlanLimit uint32 } type WhereLoopBuilder1 = TWhereLoopBuilder1 /* Allowed values for WhereLoopBuider.bldFlags */ /* The WhereLoopBuilder.iPlanLimit is used to limit the number of ** index+constraint combinations the query planner will consider for a ** particular query. If this parameter is unlimited, then certain ** pathological queries can spend excess time in the sqlite3WhereBegin() ** routine. The limit is high enough that is should not impact real-world ** queries. ** ** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit. The limit is ** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM ** clause is processed, so that every table in a join is guaranteed to be ** able to propose a some index+constraint combinations even if the initial ** baseline limit was exhausted by prior tables of the join. */ /* ** The WHERE clause processing routine has two halves. The ** first part does the start of the WHERE loop and the second ** half does the tail of the WHERE loop. An instance of ** this structure is returned by the first half and passed ** into the second half to give some continuity. ** ** An instance of this object holds the complete state of the query ** planner. */ type TWhereInfo1 = struct { F__ccgo_align [0]uint32 FpParse uintptr FpTabList uintptr FpOrderBy uintptr FpResultSet uintptr FpSelect uintptr FaiCurOnePass [2]int32 FiContinue int32 FiBreak int32 FsavedNQueryLoop int32 FwctrlFlags Tu16 FiLimit TLogEst FnLevel Tu8 FnOBSat Ti8 FeOnePass Tu8 FeDistinct Tu8 F__ccgo48 uint8 FnRowOut TLogEst FiTop int32 FiEndWhere int32 FpLoops uintptr FpMemToFree uintptr F__ccgo_align24 [4]byte FrevMask TBitmask FsWC TWhereClause FsMaskSet TWhereMaskSet Fa [1]TWhereLevel } type WhereInfo1 = TWhereInfo1 /* ** Bitmasks for the operators on WhereTerm objects. These are all ** operators that are of interest to the query planner. An ** OR-ed combination of these values can be used when searching for ** particular WhereTerms within a WhereClause. ** ** Value constraints: ** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ ** WO_LT == SQLITE_INDEX_CONSTRAINT_LT ** WO_LE == SQLITE_INDEX_CONSTRAINT_LE ** WO_GT == SQLITE_INDEX_CONSTRAINT_GT ** WO_GE == SQLITE_INDEX_CONSTRAINT_GE */ /* ** These are definitions of bits in the WhereLoop.wsFlags field. ** The particular combination of bits in each WhereLoop help to ** determine the algorithm that WhereLoop represents. */ /* 0x02000000 -- available for reuse */ /************** End of whereInt.h ********************************************/ /************** Continuing where we left off in wherecode.c ******************/ // C documentation // // /* // ** Return the name of the i-th column of the pIdx index. // */ func _explainIndexColumnName(tls *libc.TLS, pIdx uintptr, i int32) (r uintptr) { i = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))) if i == -int32(2) { return __ccgo_ts + 22006 } if i == -int32(1) { return __ccgo_ts + 16694 } return (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol + uintptr(i)*12))).FzCnName } // C documentation // // /* // ** This routine is a helper for explainIndexRange() below // ** // ** pStr holds the text of an expression that we are building up one term // ** at a time. This routine adds a new term to the end of the expression. // ** Terms are separated by AND so add the "AND" text for second and subsequent // ** terms only. // */ func _explainAppendTerm(tls *libc.TLS, pStr uintptr, pIdx uintptr, nTerm int32, iTerm int32, bAnd int32, zOp uintptr) { var i int32 _ = i _ = libc.Int32FromInt32(0) if bAnd != 0 { Xsqlite3_str_append(tls, pStr, __ccgo_ts+22013, int32(5)) } if nTerm > int32(1) { Xsqlite3_str_append(tls, pStr, __ccgo_ts+22019, int32(1)) } i = 0 for { if !(i < nTerm) { break } if i != 0 { Xsqlite3_str_append(tls, pStr, __ccgo_ts+13090, int32(1)) } Xsqlite3_str_appendall(tls, pStr, _explainIndexColumnName(tls, pIdx, iTerm+i)) goto _1 _1: ; i++ } if nTerm > int32(1) { Xsqlite3_str_append(tls, pStr, __ccgo_ts+5106, int32(1)) } Xsqlite3_str_append(tls, pStr, zOp, int32(1)) if nTerm > int32(1) { Xsqlite3_str_append(tls, pStr, __ccgo_ts+22019, int32(1)) } i = 0 for { if !(i < nTerm) { break } if i != 0 { Xsqlite3_str_append(tls, pStr, __ccgo_ts+13090, int32(1)) } Xsqlite3_str_append(tls, pStr, __ccgo_ts+5108, int32(1)) goto _2 _2: ; i++ } if nTerm > int32(1) { Xsqlite3_str_append(tls, pStr, __ccgo_ts+5106, int32(1)) } } // C documentation // // /* // ** Argument pLevel describes a strategy for scanning table pTab. This // ** function appends text to pStr that describes the subset of table // ** rows scanned by the strategy in the form of an SQL expression. // ** // ** For example, if the query: // ** // ** SELECT * FROM t1 WHERE a=1 AND b>2; // ** // ** is run and there is an index on (a, b), then this function returns a // ** string similar to: // ** // ** "a=? AND b>?" // */ func _explainIndexRange(tls *libc.TLS, pStr uintptr, pLoop uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var i, j int32 var nEq, nSkip Tu16 var pIndex, z, v2 uintptr _, _, _, _, _, _, _ = i, j, nEq, nSkip, pIndex, z, v2 pIndex = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex nEq = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq nSkip = (*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip if int32(nEq) == 0 && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_BTM_LIMIT)|libc.Int32FromInt32(WHERE_TOP_LIMIT)) == uint32(0) { return } Xsqlite3_str_append(tls, pStr, __ccgo_ts+22021, int32(2)) i = 0 for { if !(i < int32(nEq)) { break } z = _explainIndexColumnName(tls, pIndex, i) if i != 0 { Xsqlite3_str_append(tls, pStr, __ccgo_ts+22013, int32(5)) } if i >= int32(nSkip) { v2 = __ccgo_ts + 22024 } else { v2 = __ccgo_ts + 22029 } Xsqlite3_str_appendf(tls, pStr, v2, libc.VaList(bp+8, z)) goto _1 _1: ; i++ } j = i if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_BTM_LIMIT) != 0 { _explainAppendTerm(tls, pStr, pIndex, int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnBtm), j, i, __ccgo_ts+22037) i = int32(1) } if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_TOP_LIMIT) != 0 { _explainAppendTerm(tls, pStr, pIndex, int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnTop), j, i, __ccgo_ts+22039) } Xsqlite3_str_append(tls, pStr, __ccgo_ts+5106, int32(1)) } // C documentation // // /* // ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN // ** command, or if stmt_scanstatus_v2() stats are enabled, or if SQLITE_DEBUG // ** was defined at compile-time. If it is not a no-op, a single OP_Explain // ** opcode is added to the output to describe the table scan strategy in pLevel. // ** // ** If an OP_Explain opcode is added to the VM, its address is returned. // ** Otherwise, if no OP_Explain is coded, zero is returned. // */ func _sqlite3WhereExplainOneScan(tls *libc.TLS, pParse uintptr, pTabList uintptr, pLevel uintptr, wctrlFlags Tu16) (r int32) { bp := tls.Alloc(160) defer tls.Free(160) var cRangeOp uint8 var db, pIdx, pItem, pLoop, v, zFmt, zMsg, zRowid, v1, v2 uintptr var flags Tu32 var isSearch, ret int32 var _ /* str at bp+0 */ TStrAccum var _ /* zBuf at bp+24 */ [100]uint8 _, _, _, _, _, _, _, _, _, _, _, _, _, _ = cRangeOp, db, flags, isSearch, pIdx, pItem, pLoop, ret, v, zFmt, zMsg, zRowid, v1, v2 ret = 0 if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 { v1 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel } else { v1 = pParse } if int32((*TParse)(unsafe.Pointer(v1)).Fexplain) == int32(2) || libc.Bool(0 != 0) { pItem = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*72 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* VM being constructed */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Initial space for EQP output string */ pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop flags = (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags if flags&uint32(WHERE_MULTI_OR) != 0 || int32(wctrlFlags)&int32(WHERE_OR_SUBCLAUSE) != 0 { return 0 } isSearch = libc.BoolInt32(flags&uint32(libc.Int32FromInt32(WHERE_BTM_LIMIT)|libc.Int32FromInt32(WHERE_TOP_LIMIT)) != uint32(0) || flags&uint32(WHERE_VIRTUALTABLE) == uint32(0) && int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq) > 0 || int32(wctrlFlags)&(libc.Int32FromInt32(WHERE_ORDERBY_MIN)|libc.Int32FromInt32(WHERE_ORDERBY_MAX)) != 0) _sqlite3StrAccumInit(tls, bp, db, bp+24, int32(100), int32(SQLITE_MAX_LENGTH)) (*(*TStrAccum)(unsafe.Pointer(bp))).FprintfFlags = uint8(SQLITE_PRINTF_INTERNAL) if isSearch != 0 { v2 = __ccgo_ts + 22041 } else { v2 = __ccgo_ts + 22048 } Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22053, libc.VaList(bp+136, v2, pItem)) if flags&uint32(libc.Int32FromInt32(WHERE_IPK)|libc.Int32FromInt32(WHERE_VIRTUALTABLE)) == uint32(0) { zFmt = uintptr(0) _ = libc.Int32FromInt32(0) pIdx = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex _ = libc.Int32FromInt32(0) if !((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) { if isSearch != 0 { zFmt = __ccgo_ts + 11253 } } else { if flags&uint32(WHERE_PARTIALIDX) != 0 { zFmt = __ccgo_ts + 22059 } else { if flags&uint32(WHERE_AUTO_INDEX) != 0 { zFmt = __ccgo_ts + 22092 } else { if flags&uint32(WHERE_IDX_ONLY) != 0 { zFmt = __ccgo_ts + 22117 } else { zFmt = __ccgo_ts + 22135 } } } } if zFmt != 0 { Xsqlite3_str_append(tls, bp, __ccgo_ts+22144, int32(7)) Xsqlite3_str_appendf(tls, bp, zFmt, libc.VaList(bp+136, (*TIndex)(unsafe.Pointer(pIdx)).FzName)) _explainIndexRange(tls, bp, pLoop) } } else { if flags&uint32(WHERE_IPK) != uint32(0) && flags&uint32(WHERE_CONSTRAINT) != uint32(0) { zRowid = __ccgo_ts + 16694 Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22152, libc.VaList(bp+136, zRowid)) if flags&uint32(libc.Int32FromInt32(WHERE_COLUMN_EQ)|libc.Int32FromInt32(WHERE_COLUMN_IN)) != 0 { cRangeOp = uint8('=') } else { if flags&uint32(WHERE_BOTH_LIMIT) == uint32(WHERE_BOTH_LIMIT) { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22183, libc.VaList(bp+136, zRowid)) cRangeOp = uint8('<') } else { if flags&uint32(WHERE_BTM_LIMIT) != 0 { cRangeOp = uint8('>') } else { _ = libc.Int32FromInt32(0) cRangeOp = uint8('<') } } } Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22193, libc.VaList(bp+136, int32(cRangeOp))) } else { if flags&uint32(WHERE_VIRTUALTABLE) != uint32(0) { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22198, libc.VaList(bp+136, (*(*struct { FidxNum int32 F__ccgo4 uint8 FisOrdered Ti8 FomitMask Tu16 FidxStr uintptr FmHandleIn Tu32 })(unsafe.Pointer(pLoop + 24))).FidxNum, (*(*struct { FidxNum int32 F__ccgo4 uint8 FisOrdered Ti8 FomitMask Tu16 FidxStr uintptr FmHandleIn Tu32 })(unsafe.Pointer(pLoop + 24))).FidxStr)) } } } if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_LEFT) != 0 { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22225, 0) } zMsg = _sqlite3StrAccumFinish(tls, bp) ret = _sqlite3VdbeAddOp4(tls, v, int32(OP_Explain), _sqlite3VdbeCurrentAddr(tls, v), (*TParse)(unsafe.Pointer(pParse)).FaddrExplain, 0, zMsg, -int32(6)) } return ret } // C documentation // // /* // ** Add a single OP_Explain opcode that describes a Bloom filter. // ** // ** Or if not processing EXPLAIN QUERY PLAN and not in a SQLITE_DEBUG and/or // ** SQLITE_ENABLE_STMT_SCANSTATUS build, then OP_Explain opcodes are not // ** required and this routine is a no-op. // ** // ** If an OP_Explain opcode is added to the VM, its address is returned. // ** Otherwise, if no OP_Explain is coded, zero is returned. // */ func _sqlite3WhereExplainBloomFilter(tls *libc.TLS, pParse uintptr, pWInfo uintptr, pLevel uintptr) (r int32) { bp := tls.Alloc(144) defer tls.Free(144) var db, pItem, pLoop, pTab, v, z, zMsg uintptr var i, ret int32 var _ /* str at bp+0 */ TStrAccum var _ /* zBuf at bp+24 */ [100]uint8 _, _, _, _, _, _, _, _, _ = db, i, pItem, pLoop, pTab, ret, v, z, zMsg ret = 0 pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*72 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* VM being constructed */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Initial space for EQP output string */ _sqlite3StrAccumInit(tls, bp, db, bp+24, int32(100), int32(SQLITE_MAX_LENGTH)) (*(*TStrAccum)(unsafe.Pointer(bp))).FprintfFlags = uint8(SQLITE_PRINTF_INTERNAL) Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22236, libc.VaList(bp+136, pItem)) pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IPK) != 0 { pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab if int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) >= 0 { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22024, libc.VaList(bp+136, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr((*TTable)(unsafe.Pointer(pTab)).FiPKey)*12))).FzCnName)) } else { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22257, 0) } } else { i = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip) for { if !(i < int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq)) { break } z = _explainIndexColumnName(tls, (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex, i) if i > int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip) { Xsqlite3_str_append(tls, bp, __ccgo_ts+22013, int32(5)) } Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22024, libc.VaList(bp+136, z)) goto _1 _1: ; i++ } } Xsqlite3_str_append(tls, bp, __ccgo_ts+5106, int32(1)) zMsg = _sqlite3StrAccumFinish(tls, bp) ret = _sqlite3VdbeAddOp4(tls, v, int32(OP_Explain), _sqlite3VdbeCurrentAddr(tls, v), (*TParse)(unsafe.Pointer(pParse)).FaddrExplain, 0, zMsg, -int32(6)) return ret } // C documentation // // /* // ** Disable a term in the WHERE clause. Except, do not disable the term // ** if it controls a LEFT OUTER JOIN and it did not originate in the ON // ** or USING clause of that join. // ** // ** Consider the term t2.z='ok' in the following queries: // ** // ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' // ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' // ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' // ** // ** The t2.z='ok' is disabled in the in (2) because it originates // ** in the ON clause. The term is disabled in (3) because it is not part // ** of a LEFT OUTER JOIN. In (1), the term is not disabled. // ** // ** Disabling a term causes that term to not be tested in the inner loop // ** of the join. Disabling is an optimization. When terms are satisfied // ** by indices, we disable them to prevent redundant tests in the inner // ** loop. We would get the correct results if nothing were ever disabled, // ** but joins might run a little slower. The trick is to disable as much // ** as we can without disabling too much. If we disabled in (1), we'd get // ** the wrong answer. See ticket #813. // ** // ** If all the children of a term are disabled, then that term is also // ** automatically disabled. In this way, terms get disabled if derived // ** virtual terms are tested first. For example: // ** // ** x GLOB 'abc*' AND x>='abc' AND x<'acd' // ** \___________/ \______/ \_____/ // ** parent child1 child2 // ** // ** Only the parent term was in the original WHERE clause. The child1 // ** and child2 terms were added by the LIKE optimization. If both of // ** the virtual child terms are valid, then testing of the parent can be // ** skipped. // ** // ** Usually the parent term is marked as TERM_CODED. But if the parent // ** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead. // ** The TERM_LIKECOND marking indicates that the term should be coded inside // ** a conditional such that is only evaluated on the second pass of a // ** LIKE-optimization loop, when scanning BLOBs instead of strings. // */ func _disableTerm(tls *libc.TLS, pLevel uintptr, pTerm uintptr) { var nLoop int32 var p1, p2 uintptr _, _, _ = nLoop, p1, p2 nLoop = 0 _ = libc.Int32FromInt32(0) for int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_CODED) == 0 && ((*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin == 0 || (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0)) && (*TWhereLevel)(unsafe.Pointer(pLevel)).FnotReady&(*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll == uint64(0) { if nLoop != 0 && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_LIKE) != 0 { p1 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(TERM_LIKECOND)) } else { p2 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(TERM_CODED)) } if (*TWhereTerm)(unsafe.Pointer(pTerm)).FiParent < 0 { break } pTerm = (*TWhereClause)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpWC)).Fa + uintptr((*TWhereTerm)(unsafe.Pointer(pTerm)).FiParent)*48 _ = libc.Int32FromInt32(0) (*TWhereTerm)(unsafe.Pointer(pTerm)).FnChild-- if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FnChild) != 0 { break } nLoop++ } } // C documentation // // /* // ** Code an OP_Affinity opcode to apply the column affinity string zAff // ** to the n registers starting at base. // ** // ** As an optimization, SQLITE_AFF_BLOB and SQLITE_AFF_NONE entries (which // ** are no-ops) at the beginning and end of zAff are ignored. If all entries // ** in zAff are SQLITE_AFF_BLOB or SQLITE_AFF_NONE, then no code gets generated. // ** // ** This routine makes its own copy of zAff so that the caller is free // ** to modify zAff after this routine returns. // */ func _codeApplyAffinity(tls *libc.TLS, pParse uintptr, base int32, n int32, zAff uintptr) { var v uintptr _ = v v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe if zAff == uintptr(0) { _ = libc.Int32FromInt32(0) return } _ = libc.Int32FromInt32(0) /* Adjust base and n to skip over SQLITE_AFF_BLOB and SQLITE_AFF_NONE ** entries at the beginning and end of the affinity string. */ _ = libc.Int32FromInt32(0) for n > 0 && int32(*(*uint8)(unsafe.Pointer(zAff))) <= int32(SQLITE_AFF_BLOB) { n-- base++ zAff++ } for n > int32(1) && int32(*(*uint8)(unsafe.Pointer(zAff + uintptr(n-int32(1))))) <= int32(SQLITE_AFF_BLOB) { n-- } /* Code the OP_Affinity opcode if there is anything left to do. */ if n > 0 { _sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), base, n, 0, zAff, n) } } // C documentation // // /* // ** Expression pRight, which is the RHS of a comparison operation, is // ** either a vector of n elements or, if n==1, a scalar expression. // ** Before the comparison operation, affinity zAff is to be applied // ** to the pRight values. This function modifies characters within the // ** affinity string to SQLITE_AFF_BLOB if either: // ** // ** * the comparison will be performed with no affinity, or // ** * the affinity change in zAff is guaranteed not to change the value. // */ func _updateRangeAffinityStr(tls *libc.TLS, pRight uintptr, n int32, zAff uintptr) { var i int32 var p uintptr _, _ = i, p i = 0 for { if !(i < n) { break } p = _sqlite3VectorFieldSubexpr(tls, pRight, i) if int32(_sqlite3CompareAffinity(tls, p, *(*uint8)(unsafe.Pointer(zAff + uintptr(i))))) == int32(SQLITE_AFF_BLOB) || _sqlite3ExprNeedsNoAffinityChange(tls, p, *(*uint8)(unsafe.Pointer(zAff + uintptr(i)))) != 0 { *(*uint8)(unsafe.Pointer(zAff + uintptr(i))) = uint8(SQLITE_AFF_BLOB) } goto _1 _1: ; i++ } } // C documentation // // /* // ** pX is an expression of the form: (vector) IN (SELECT ...) // ** In other words, it is a vector IN operator with a SELECT clause on the // ** LHS. But not all terms in the vector are indexable and the terms might // ** not be in the correct order for indexing. // ** // ** This routine makes a copy of the input pX expression and then adjusts // ** the vector on the LHS with corresponding changes to the SELECT so that // ** the vector contains only index terms and those terms are in the correct // ** order. The modified IN expression is returned. The caller is responsible // ** for deleting the returned expression. // ** // ** Example: // ** // ** CREATE TABLE t1(a,b,c,d,e,f); // ** CREATE INDEX t1x1 ON t1(e,c); // ** SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2) // ** \_______________________________________/ // ** The pX expression // ** // ** Since only columns e and c can be used with the index, in that order, // ** the modified IN expression that is returned will be: // ** // ** (e,c) IN (SELECT z,x FROM t2) // ** // ** The reduced pX is different from the original (obviously) and thus is // ** only used for indexing, to improve performance. The original unaltered // ** IN expression must also be run on each output row for correctness. // */ func _removeUnindexableInClauseTerms(tls *libc.TLS, pParse uintptr, iEq int32, pLoop uintptr, pX uintptr) (r uintptr) { var db, p, pLhs, pNew, pOrderBy, pOrigLhs, pOrigRhs, pRhs, pSelect uintptr var i, iField int32 _, _, _, _, _, _, _, _, _, _, _ = db, i, iField, p, pLhs, pNew, pOrderBy, pOrigLhs, pOrigRhs, pRhs, pSelect db = (*TParse)(unsafe.Pointer(pParse)).Fdb pNew = _sqlite3ExprDup(tls, db, pX, 0) if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 { pSelect = *(*uintptr)(unsafe.Pointer(pNew + 20)) for { if !(pSelect != 0) { break } /* Original unmodified RHS */ pOrigLhs = uintptr(0) /* Original unmodified LHS */ pRhs = uintptr(0) /* New RHS after modifications */ pLhs = uintptr(0) /* Loop counter */ _ = libc.Int32FromInt32(0) pOrigRhs = (*TSelect)(unsafe.Pointer(pSelect)).FpEList _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pSelect == *(*uintptr)(unsafe.Pointer(pNew + 20)) { pOrigLhs = *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pNew)).FpLeft + 20)) } i = iEq for { if !(i < int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm)) { break } if (*TWhereTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*4)))).FpExpr == pX { _ = libc.Int32FromInt32(0) iField = (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*4)) + 24))).FiField - int32(1) if (*(*TExprList_item)(unsafe.Pointer(pOrigRhs + 8 + uintptr(iField)*20))).FpExpr == uintptr(0) { goto _2 } /* Duplicate PK column */ pRhs = _sqlite3ExprListAppend(tls, pParse, pRhs, (*(*TExprList_item)(unsafe.Pointer(pOrigRhs + 8 + uintptr(iField)*20))).FpExpr) (*(*TExprList_item)(unsafe.Pointer(pOrigRhs + 8 + uintptr(iField)*20))).FpExpr = uintptr(0) if pOrigLhs != 0 { _ = libc.Int32FromInt32(0) pLhs = _sqlite3ExprListAppend(tls, pParse, pLhs, (*(*TExprList_item)(unsafe.Pointer(pOrigLhs + 8 + uintptr(iField)*20))).FpExpr) (*(*TExprList_item)(unsafe.Pointer(pOrigLhs + 8 + uintptr(iField)*20))).FpExpr = uintptr(0) } } goto _2 _2: ; i++ } _sqlite3ExprListDelete(tls, db, pOrigRhs) if pOrigLhs != 0 { _sqlite3ExprListDelete(tls, db, pOrigLhs) *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pNew)).FpLeft + 20)) = pLhs } (*TSelect)(unsafe.Pointer(pSelect)).FpEList = pRhs if pLhs != 0 && (*TExprList)(unsafe.Pointer(pLhs)).FnExpr == int32(1) { /* Take care here not to generate a TK_VECTOR containing only a ** single value. Since the parser never creates such a vector, some ** of the subroutines do not handle this case. */ p = (*(*TExprList_item)(unsafe.Pointer(pLhs + 8))).FpExpr (*(*TExprList_item)(unsafe.Pointer(pLhs + 8))).FpExpr = uintptr(0) _sqlite3ExprDelete(tls, db, (*TExpr)(unsafe.Pointer(pNew)).FpLeft) (*TExpr)(unsafe.Pointer(pNew)).FpLeft = p } if (*TSelect)(unsafe.Pointer(pSelect)).FpOrderBy != 0 { /* If the SELECT statement has an ORDER BY clause, zero the ** iOrderByCol variables. These are set to non-zero when an ** ORDER BY term exactly matches one of the terms of the ** result-set. Since the result-set of the SELECT statement may ** have been modified or reordered, these variables are no longer ** set correctly. Since setting them is just an optimization, ** it's easiest just to zero them here. */ pOrderBy = (*TSelect)(unsafe.Pointer(pSelect)).FpOrderBy i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) { break } *(*Tu16)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20 + 16)) = uint16(0) goto _3 _3: ; i++ } } goto _1 _1: ; pSelect = (*TSelect)(unsafe.Pointer(pSelect)).FpPrior } } return pNew } // C documentation // // /* // ** Generate code for a single equality term of the WHERE clause. An equality // ** term can be either X=expr or X IN (...). pTerm is the term to be // ** coded. // ** // ** The current value for the constraint is left in a register, the index // ** of which is returned. An attempt is made store the result in iTarget but // ** this is only guaranteed for TK_ISNULL and TK_IN constraints. If the // ** constraint is a TK_EQ or TK_IS, then the current value might be left in // ** some other register and it is the caller's responsibility to compensate. // ** // ** For a constraint of the form X=expr, the expression is evaluated in // ** straight-line code. For constraints of the form X IN (...) // ** this routine sets up a loop that will iterate over all values of X. // */ func _codeEqualityTerm(tls *libc.TLS, pParse uintptr, pTerm uintptr, pLevel uintptr, iEq int32, bRev int32, iTarget int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var aiMap, db, pExpr, pIn, pLoop, pX, v uintptr var eType, i, iCol, iMap, iOut, iReg, n, nEq, v3, v4, v6, v7, v8 int32 var _ /* iTab at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aiMap, db, eType, i, iCol, iMap, iOut, iReg, n, nEq, pExpr, pIn, pLoop, pX, v, v3, v4, v6, v7, v8 pX = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* Register holding results */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pX)).Fop) == int32(TK_EQ) || int32((*TExpr)(unsafe.Pointer(pX)).Fop) == int32(TK_IS) { iReg = _sqlite3ExprCodeTarget(tls, pParse, (*TExpr)(unsafe.Pointer(pX)).FpRight, iTarget) } else { if int32((*TExpr)(unsafe.Pointer(pX)).Fop) == int32(TK_ISNULL) { iReg = iTarget _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, iReg) } else { eType = int32(IN_INDEX_NOOP) pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop nEq = 0 aiMap = uintptr(0) if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_VIRTUALTABLE) == uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex != uintptr(0) && *(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex)).FaSortOrder + uintptr(iEq))) != 0 { bRev = libc.BoolInt32(!(bRev != 0)) } _ = libc.Int32FromInt32(0) iReg = iTarget i = 0 for { if !(i < iEq) { break } if *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*4)) != 0 && (*TWhereTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*4)))).FpExpr == pX { _disableTerm(tls, pLevel, pTerm) return iTarget } goto _1 _1: ; i++ } i = iEq for { if !(i < int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm)) { break } _ = libc.Int32FromInt32(0) if (*TWhereTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*4)))).FpExpr == pX { nEq++ } goto _2 _2: ; i++ } *(*int32)(unsafe.Pointer(bp)) = 0 if !((*TExpr)(unsafe.Pointer(pX)).Fflags&libc.Uint32FromInt32(EP_xIsSelect) != libc.Uint32FromInt32(0)) || (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pX + 20)))).FpEList)).FnExpr == int32(1) { eType = _sqlite3FindInIndex(tls, pParse, pX, uint32(IN_INDEX_LOOP), uintptr(0), uintptr(0), bp) } else { pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr if (*TExpr)(unsafe.Pointer(pExpr)).FiTable == 0 || !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Subrtn)) != libc.Uint32FromInt32(0)) { db = (*TParse)(unsafe.Pointer(pParse)).Fdb pX = _removeUnindexableInClauseTerms(tls, pParse, iEq, pLoop, pX) if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { aiMap = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(4)*uint32(nEq))) eType = _sqlite3FindInIndex(tls, pParse, pX, uint32(IN_INDEX_LOOP), uintptr(0), aiMap, bp) (*TExpr)(unsafe.Pointer(pExpr)).FiTable = *(*int32)(unsafe.Pointer(bp)) } _sqlite3ExprDelete(tls, db, pX) } else { n = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pX)).FpLeft) if nEq > n { v3 = nEq } else { v3 = n } aiMap = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(4)*uint32(v3))) eType = _sqlite3FindInIndex(tls, pParse, pX, uint32(IN_INDEX_LOOP), uintptr(0), aiMap, bp) } pX = pExpr } if eType == int32(IN_INDEX_INDEX_DESC) { bRev = libc.BoolInt32(!(bRev != 0)) } if bRev != 0 { v4 = int32(OP_Last) } else { v4 = int32(OP_Rewind) } _sqlite3VdbeAddOp2(tls, v, v4, *(*int32)(unsafe.Pointer(bp)), 0) _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(pLoop + 40)) |= uint32(WHERE_IN_ABLE) if (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 64))).FnIn == 0 { (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrNxt = _sqlite3VdbeMakeLabel(tls, pParse) } if iEq > 0 && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IN_SEEKSCAN) == uint32(0) { *(*Tu32)(unsafe.Pointer(pLoop + 40)) |= uint32(WHERE_IN_EARLYOUT) } i = (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 64))).FnIn (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 64))).FnIn += nEq (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 64))).FaInLoop = _sqlite3WhereRealloc(tls, (*TWhereClause)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpWC)).FpWInfo, (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 64))).FaInLoop, uint64(uint32(20)*uint32((*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 64))).FnIn))) pIn = (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 64))).FaInLoop if pIn != 0 { iMap = 0 /* Index in aiMap[] */ pIn += uintptr(i) * 20 i = iEq for { if !(i < int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm)) { break } if (*TWhereTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*4)))).FpExpr == pX { iOut = iReg + i - iEq if eType == int32(IN_INDEX_ROWID) { (*TInLoop)(unsafe.Pointer(pIn)).FaddrInTop = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), *(*int32)(unsafe.Pointer(bp)), iOut) } else { if aiMap != 0 { v7 = iMap iMap++ v6 = *(*int32)(unsafe.Pointer(aiMap + uintptr(v7)*4)) } else { v6 = 0 } iCol = v6 (*TInLoop)(unsafe.Pointer(pIn)).FaddrInTop = _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), *(*int32)(unsafe.Pointer(bp)), iCol, iOut) } _sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), iOut) if i == iEq { (*TInLoop)(unsafe.Pointer(pIn)).FiCur = *(*int32)(unsafe.Pointer(bp)) if bRev != 0 { v8 = int32(OP_Prev) } else { v8 = int32(OP_Next) } (*TInLoop)(unsafe.Pointer(pIn)).FeEndLoopOp = uint8(v8) if iEq > 0 { (*TInLoop)(unsafe.Pointer(pIn)).FiBase = iReg - i (*TInLoop)(unsafe.Pointer(pIn)).FnPrefix = i } else { (*TInLoop)(unsafe.Pointer(pIn)).FnPrefix = 0 } } else { (*TInLoop)(unsafe.Pointer(pIn)).FeEndLoopOp = uint8(OP_Noop) } pIn += 20 } goto _5 _5: ; i++ } if iEq > 0 && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_IN_SEEKSCAN)|libc.Int32FromInt32(WHERE_VIRTUALTABLE)) == uint32(0) { _sqlite3VdbeAddOp3(tls, v, int32(OP_SeekHit), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur, 0, iEq) } } else { (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 64))).FnIn = 0 } _sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, aiMap) } } /* As an optimization, try to disable the WHERE clause term that is ** driving the index as it will always be true. The correct answer is ** obtained regardless, but we might get the answer with fewer CPU cycles ** by omitting the term. ** ** But do not disable the term unless we are certain that the term is ** not a transitive constraint. For an example of where that does not ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04) */ if (*TWhereLoop1)(unsafe.Pointer((*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop)).FwsFlags&uint32(WHERE_TRANSCONS) == uint32(0) || int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_EQUIV) == 0 { _disableTerm(tls, pLevel, pTerm) } return iReg } // C documentation // // /* // ** Generate code that will evaluate all == and IN constraints for an // ** index scan. // ** // ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). // ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 // ** The index has as many as three equality constraints, but in this // ** example, the third "c" value is an inequality. So only two // ** constraints are coded. This routine will generate code to evaluate // ** a==5 and b IN (1,2,3). The current values for a and b will be stored // ** in consecutive registers and the index of the first register is returned. // ** // ** In the example above nEq==2. But this subroutine works for any value // ** of nEq including 0. If nEq==0, this routine is nearly a no-op. // ** The only thing it does is allocate the pLevel->iMem memory cell and // ** compute the affinity string. // ** // ** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints // ** are == or IN and are covered by the nEq. nExtraReg is 1 if there is // ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that // ** occurs after the nEq quality constraints. // ** // ** This routine allocates a range of nEq+nExtraReg memory cells and returns // ** the index of the first memory cell in that range. The code that // ** calls this routine will use that memory range to store keys for // ** start and termination conditions of the loop. // ** key value of the loop. If one or more IN operators appear, then // ** this routine allocates an additional nEq memory cells for internal // ** use. // ** // ** Before returning, *pzAff is set to point to a buffer containing a // ** copy of the column affinity string of the index allocated using // ** sqlite3DbMalloc(). Except, entries in the copy of the string associated // ** with equality constraints that use BLOB or NONE affinity are set to // ** SQLITE_AFF_BLOB. This is to deal with SQL such as the following: // ** // ** CREATE TABLE t1(a TEXT PRIMARY KEY, b); // ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; // ** // ** In the example above, the index on t1(a) has TEXT affinity. But since // ** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity, // ** no conversion should be attempted before using a t2.b value as part of // ** a key to search the index. Hence the first byte in the returned affinity // ** string in this example would be set to SQLITE_AFF_BLOB. // */ func _codeAllEqualityTerms(tls *libc.TLS, pParse uintptr, pLevel uintptr, bRev int32, nExtraReg int32, pzAff uintptr) (r int32) { var iIdxCur, j, nReg, r1, regBase, v1, v2 int32 var nEq, nSkip Tu16 var pIdx, pLoop, pRight, pTerm, v, zAff uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = iIdxCur, j, nEq, nReg, nSkip, pIdx, pLoop, pRight, pTerm, r1, regBase, v, zAff, v1, v2 /* Number of left-most columns to skip */ v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* Affinity string to return */ /* This module is only called on query plans that use an index. */ pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop _ = libc.Int32FromInt32(0) nEq = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq nSkip = (*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip pIdx = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex _ = libc.Int32FromInt32(0) /* Figure out how many memory cells we will need then allocate them. */ regBase = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) nReg = int32(nEq) + nExtraReg *(*int32)(unsafe.Pointer(pParse + 44)) += nReg zAff = _sqlite3DbStrDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, _sqlite3IndexAffinityStr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pIdx)) _ = libc.Int32FromInt32(0) if nSkip != 0 { iIdxCur = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur _sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, regBase, regBase+int32(nSkip)-int32(1)) if bRev != 0 { v1 = int32(OP_Last) } else { v1 = int32(OP_Rewind) } _sqlite3VdbeAddOp1(tls, v, v1, iIdxCur) j = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto)) _ = libc.Int32FromInt32(0) if bRev != 0 { v2 = int32(OP_SeekLT) } else { v2 = int32(OP_SeekGT) } (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrSkip = _sqlite3VdbeAddOp4Int(tls, v, v2, iIdxCur, 0, regBase, int32(nSkip)) _sqlite3VdbeJumpHere(tls, v, j) j = 0 for { if !(j < int32(nSkip)) { break } _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iIdxCur, j, regBase+j) goto _3 _3: ; j++ } } /* Evaluate the equality constraints */ _ = libc.Int32FromInt32(0) j = int32(nSkip) for { if !(j < int32(nEq)) { break } pTerm = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4)) _ = libc.Int32FromInt32(0) /* The following testcase is true for indices with redundant columns. ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ r1 = _codeEqualityTerm(tls, pParse, pTerm, pLevel, j, bRev, regBase+j) if r1 != regBase+j { if nReg == int32(1) { _sqlite3ReleaseTempReg(tls, pParse, regBase) regBase = r1 } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), r1, regBase+j) } } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_IN) != 0 { if (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(EP_xIsSelect) != 0 { /* No affinity ever needs to be (or should be) applied to a value ** from the RHS of an "? IN (SELECT ...)" expression. The ** sqlite3FindInIndex() routine has already ensured that the ** affinity of the comparison has been applied to the value. */ if zAff != 0 { *(*uint8)(unsafe.Pointer(zAff + uintptr(j))) = uint8(SQLITE_AFF_BLOB) } } } else { if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_ISNULL) == 0 { pRight = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpRight if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_IS) == 0 && _sqlite3ExprCanBeNull(tls, pRight) != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regBase+j, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk) } if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 { _ = libc.Int32FromInt32(0) if int32(_sqlite3CompareAffinity(tls, pRight, *(*uint8)(unsafe.Pointer(zAff + uintptr(j))))) == int32(SQLITE_AFF_BLOB) { *(*uint8)(unsafe.Pointer(zAff + uintptr(j))) = uint8(SQLITE_AFF_BLOB) } if _sqlite3ExprNeedsNoAffinityChange(tls, pRight, *(*uint8)(unsafe.Pointer(zAff + uintptr(j)))) != 0 { *(*uint8)(unsafe.Pointer(zAff + uintptr(j))) = uint8(SQLITE_AFF_BLOB) } } } } goto _4 _4: ; j++ } *(*uintptr)(unsafe.Pointer(pzAff)) = zAff return regBase } // C documentation // // /* // ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains // ** a rowid value just read from cursor iIdxCur, open on index pIdx. This // ** function generates code to do a deferred seek of cursor iCur to the // ** rowid stored in register iRowid. // ** // ** Normally, this is just: // ** // ** OP_DeferredSeek $iCur $iRowid // ** // ** Which causes a seek on $iCur to the row with rowid $iRowid. // ** // ** However, if the scan currently being coded is a branch of an OR-loop and // ** the statement currently being coded is a SELECT, then additional information // ** is added that might allow OP_Column to omit the seek and instead do its // ** lookup on the index, thus avoiding an expensive seek operation. To // ** enable this optimization, the P3 of OP_DeferredSeek is set to iIdxCur // ** and P4 is set to an array of integers containing one entry for each column // ** in the table. For each table column, if the column is the i'th // ** column of the index, then the corresponding array entry is set to (i+1). // ** If the column does not appear in the index at all, the array entry is set // ** to 0. The OP_Column opcode can check this array to see if the column it // ** wants is in the index and if it is, it will substitute the index cursor // ** and column number and continue with those new values, rather than seeking // ** the table cursor. // */ func _codeDeferredSeek(tls *libc.TLS, pWInfo uintptr, pIdx uintptr, iCur int32, iIdxCur int32) { var ai, pParse, pTab, v, v1 uintptr var i, x1, x2 int32 var v2 bool _, _, _, _, _, _, _, _, _ = ai, i, pParse, pTab, v, x1, x2, v1, v2 pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse /* Parse context */ v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* Vdbe to generate code within */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(1), 0, 0x1) _sqlite3VdbeAddOp3(tls, v, int32(OP_DeferredSeek), iIdxCur, 0, iCur) if v2 = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&(libc.Int32FromInt32(WHERE_OR_SUBCLAUSE)|libc.Int32FromInt32(WHERE_RIGHT_JOIN)) != 0; v2 { if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 { v1 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel } else { v1 = pParse } } if v2 && (*TParse)(unsafe.Pointer(v1)).FwriteMask == uint32(0) { pTab = (*TIndex)(unsafe.Pointer(pIdx)).FpTable ai = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(4)*uint32(int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+libc.Int32FromInt32(1)))) if ai != 0 { *(*Tu32)(unsafe.Pointer(ai)) = uint32((*TTable)(unsafe.Pointer(pTab)).FnCol) i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)-int32(1)) { break } _ = libc.Int32FromInt32(0) x1 = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))) x2 = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(x1))) if x1 >= 0 { *(*Tu32)(unsafe.Pointer(ai + uintptr(x2+int32(1))*4)) = uint32(i + int32(1)) } goto _3 _3: ; i++ } _sqlite3VdbeChangeP4(tls, v, -int32(1), ai, -int32(14)) } } } // C documentation // // /* // ** If the expression passed as the second argument is a vector, generate // ** code to write the first nReg elements of the vector into an array // ** of registers starting with iReg. // ** // ** If the expression is not a vector, then nReg must be passed 1. In // ** this case, generate code to evaluate the expression and leave the // ** result in register iReg. // */ func _codeExprOrVector(tls *libc.TLS, pParse uintptr, p uintptr, iReg int32, nReg int32) { var i, iSelect int32 var pList, v uintptr _, _, _, _ = i, iSelect, pList, v _ = libc.Int32FromInt32(0) if p != 0 && _sqlite3ExprIsVector(tls, p) != 0 { if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_xIsSelect) != uint32(0) { v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) iSelect = _sqlite3CodeSubselect(tls, pParse, p) _sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), iSelect, iReg, nReg-int32(1)) } else { _ = libc.Int32FromInt32(0) pList = *(*uintptr)(unsafe.Pointer(p + 20)) _ = libc.Int32FromInt32(0) i = 0 for { if !(i < nReg) { break } _sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr, iReg+i) goto _1 _1: ; i++ } } } else { _ = libc.Int32FromInt32(0) _sqlite3ExprCode(tls, pParse, p, iReg) } } // C documentation // // /* // ** The pTruth expression is always true because it is the WHERE clause // ** a partial index that is driving a query loop. Look through all of the // ** WHERE clause terms on the query, and if any of those terms must be // ** true because pTruth is true, then mark those WHERE clause terms as // ** coded. // */ func _whereApplyPartialIndexConstraints(tls *libc.TLS, pTruth uintptr, iTabCur int32, pWC uintptr) { var i int32 var pExpr, pTerm, p2 uintptr _, _, _, _ = i, pExpr, pTerm, p2 for int32((*TExpr)(unsafe.Pointer(pTruth)).Fop) == int32(TK_AND) { _whereApplyPartialIndexConstraints(tls, (*TExpr)(unsafe.Pointer(pTruth)).FpLeft, iTabCur, pWC) pTruth = (*TExpr)(unsafe.Pointer(pTruth)).FpRight } i = 0 pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa for { if !(i < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) { break } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_CODED) != 0 { goto _1 } pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr if _sqlite3ExprCompare(tls, uintptr(0), pExpr, pTruth, iTabCur) == 0 { p2 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(TERM_CODED)) } goto _1 _1: ; i++ pTerm += 48 } } // C documentation // // /* // ** This routine is called right after An OP_Filter has been generated and // ** before the corresponding index search has been performed. This routine // ** checks to see if there are additional Bloom filters in inner loops that // ** can be checked prior to doing the index lookup. If there are available // ** inner-loop Bloom filters, then evaluate those filters now, before the // ** index lookup. The idea is that a Bloom filter check is way faster than // ** an index lookup, and the Bloom filter might return false, meaning that // ** the index lookup can be skipped. // ** // ** We know that an inner loop uses a Bloom filter because it has the // ** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked, // ** then clear the WhereLevel.regFilter value to prevent the Bloom filter // ** from being checked a second time when the inner loop is evaluated. // */ func _filterPullDown(tls *libc.TLS, pParse uintptr, pWInfo uintptr, iLevel int32, addrNxt int32, notReady TBitmask) { bp := tls.Alloc(16) defer tls.Free(16) var nEq Tu16 var pLevel, pLoop, pTerm uintptr var r1, regRowid, v1 int32 var _ /* zStartAff at bp+0 */ uintptr _, _, _, _, _, _, _ = nEq, pLevel, pLoop, pTerm, r1, regRowid, v1 for { iLevel++ v1 = iLevel if !(v1 < int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)) { break } pLevel = pWInfo + 760 + uintptr(iLevel)*88 pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop if (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter == 0 { continue } if (*TWhereLoop1)(unsafe.Pointer((*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop)).FnSkip != 0 { continue } /* ,--- Because sqlite3ConstructBloomFilter() has will not have set ** vvvvv--' pLevel->regFilter if this were true. */ if (*TWhereLoop)(unsafe.Pointer(pLoop)).Fprereq¬Ready != 0 { continue } _ = libc.Int32FromInt32(0) (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk = addrNxt if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IPK) != 0 { pTerm = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) regRowid = _sqlite3GetTempReg(tls, pParse) regRowid = _codeEqualityTerm(tls, pParse, pTerm, pLevel, 0, 0, regRowid) _sqlite3VdbeAddOp2(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(OP_MustBeInt), regRowid, addrNxt) _sqlite3VdbeAddOp4Int(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(OP_Filter), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter, addrNxt, regRowid, int32(1)) } else { nEq = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) r1 = _codeAllEqualityTerms(tls, pParse, pLevel, 0, 0, bp) _codeApplyAffinity(tls, pParse, r1, int32(nEq), *(*uintptr)(unsafe.Pointer(bp))) _sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(bp))) _sqlite3VdbeAddOp4Int(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(OP_Filter), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter, addrNxt, r1, int32(nEq)) } (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter = 0 (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk = 0 } } // C documentation // // /* // ** Generate code for the start of the iLevel-th loop in the WHERE clause // ** implementation described by pWInfo. // */ func _sqlite3WhereCodeOneLoopStart(tls *libc.TLS, pParse uintptr, v uintptr, pWInfo uintptr, iLevel int32, pLevel uintptr, notReady TBitmask) (r2 TBitmask) { bp := tls.Alloc(80) defer tls.Free(80) var aMoveOp [4]Tu8 var addrBrk, addrCont, addrExplain, addrHalt, addrNotFound, addrNxt, addrSeekScan, bRev, endEq, iCache, iCol, iCol1, iCovCur, iCur, iFld, iIdxCur, iIn, iLoop, iLoopBody, iNext, iPk, iPk1, iReg, iReleaseReg, iRetInit, iRowidReg, iSet, iTab, iTarget, iTerm, ii, j, jmp1, jmp11, k, memEndValue, nConstraint, nConstraint1, nExtraReg, nNotReady, nPk, nPk1, omitTable, op, op1, r, r1, r11, regBase, regBignull, regReturn, regRowid, regRowset, regYield, skipLikeAddr, start, startEq, start_constraints, testOp, untestedTerms, v1, v11, v13, v14, v2, v20, v23, v24, v25, v26, v27, v29, v3, v30, v31, v32, v34, v35, v36, v37, v38, v39, v4, v41, v43, v44, v46, v49, v51, v53, v58, v60, v9 int32 var bSeekPastNull, bStopAtNull, t1, t2 Tu8 var db, origSrc, pAlt, pAndExpr, pCompare, pCov, pDelete, pE, pE1, pEnd, pExpr, pIdx, pLeft, pLoop, pOp, pOrExpr, pOrTab, pOrTerm, pOrWc, pPk, pPk1, pPk2, pPk3, pRJ, pRJ1, pRangeEnd, pRangeStart, pRight, pRight1, pRight2, pRight3, pStart, pSubLoop, pSubWInfo, pTab, pTab1, pTabItem, pTerm, pWC, pX, pX1, t, zEndAff, v10, v12, v19, v21, v22, v28, v33, v40, v45, v47, v5, v50, v52, v54, v57, p62, p64, p68 uintptr var m TBitmask var nBtm, nEq, nTop Tu16 var v16, v8 uint32 var v17 bool var _ /* rTemp at bp+0 */ int32 var _ /* sEAlt at bp+8 */ TExpr var _ /* zStartAff at bp+4 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aMoveOp, addrBrk, addrCont, addrExplain, addrHalt, addrNotFound, addrNxt, addrSeekScan, bRev, bSeekPastNull, bStopAtNull, db, endEq, iCache, iCol, iCol1, iCovCur, iCur, iFld, iIdxCur, iIn, iLoop, iLoopBody, iNext, iPk, iPk1, iReg, iReleaseReg, iRetInit, iRowidReg, iSet, iTab, iTarget, iTerm, ii, j, jmp1, jmp11, k, m, memEndValue, nBtm, nConstraint, nConstraint1, nEq, nExtraReg, nNotReady, nPk, nPk1, nTop, omitTable, op, op1, origSrc, pAlt, pAndExpr, pCompare, pCov, pDelete, pE, pE1, pEnd, pExpr, pIdx, pLeft, pLoop, pOp, pOrExpr, pOrTab, pOrTerm, pOrWc, pPk, pPk1, pPk2, pPk3, pRJ, pRJ1, pRangeEnd, pRangeStart, pRight, pRight1, pRight2, pRight3, pStart, pSubLoop, pSubWInfo, pTab, pTab1, pTabItem, pTerm, pWC, pX, pX1, r, r1, r11, regBase, regBignull, regReturn, regRowid, regRowset, regYield, skipLikeAddr, start, startEq, start_constraints, t, t1, t2, testOp, untestedTerms, zEndAff, v1, v10, v11, v12, v13, v14, v16, v17, v19, v2, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v3, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v4, v40, v41, v43, v44, v45, v46, v47, v49, v5, v50, v51, v52, v53, v54, v57, v58, v60, v8, v9, p62, p64, p68 /* Jump here to continue with next cycle */ iRowidReg = 0 /* Rowid is stored in this register, if not zero */ iReleaseReg = 0 /* Temp register to free before returning */ pIdx = uintptr(0) /* Iteration of constraint generator loop */ pWC = pWInfo + 80 db = (*TParse)(unsafe.Pointer(pParse)).Fdb pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop pTabItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*72 iCur = (*TSrcItem)(unsafe.Pointer(pTabItem)).FiCursor (*TWhereLevel)(unsafe.Pointer(pLevel)).FnotReady = notReady & ^_sqlite3WhereGetMask(tls, pWInfo+496, iCur) bRev = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FrevMask >> iLevel & uint64(1)) /* Create labels for the "break" and "continue" instructions ** for the current loop. Jump to addrBrk to break out of a loop. ** Jump to cont to go immediately to the next iteration of the ** loop. ** ** When there is an IN operator, we also have a "addrNxt" label that ** means to continue with the next IN value combination. When ** there are no IN operators in the constraints, the "addrNxt" label ** is the same as "addrBrk". */ v2 = _sqlite3VdbeMakeLabel(tls, pParse) (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrNxt = v2 v1 = v2 (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk = v1 addrBrk = v1 v3 = _sqlite3VdbeMakeLabel(tls, pParse) (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont = v3 addrCont = v3 /* If this is the right table of a LEFT OUTER JOIN, allocate and ** initialize a memory cell that records if this table matches any ** row of the left table of the join. */ _ = libc.Int32FromInt32(0) if int32((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom) > 0 && int32((*(*TSrcItem)(unsafe.Pointer(pTabItem))).Ffg.Fjointype)&int32(JT_LEFT) != 0 { v5 = pParse + 44 *(*int32)(unsafe.Pointer(v5))++ v4 = *(*int32)(unsafe.Pointer(v5)) (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin = v4 _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin) } /* Compute a safe address to jump to if we discover that the table for ** this loop is empty and can never contribute content. */ j = iLevel for { if !(j > 0) { break } if (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(j)*88))).FiLeftJoin != 0 { break } if (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(j)*88))).FpRJ != 0 { break } goto _6 _6: ; j-- } addrHalt = (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(j)*88))).FaddrBrk /* Special case of a FROM clause subquery implemented as a co-routine */ if int32(uint32(*(*uint16)(unsafe.Pointer(pTabItem + 36 + 4))&0x20>>5)) != 0 { regYield = (*TSrcItem)(unsafe.Pointer(pTabItem)).FregReturn _sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), regYield, 0, (*TSrcItem)(unsafe.Pointer(pTabItem)).FaddrFillSub) (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2 = _sqlite3VdbeAddOp2(tls, v, int32(OP_Yield), regYield, addrBrk) (*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(OP_Goto) } else { if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_VIRTUALTABLE) != uint32(0) { nConstraint = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm) iReg = _sqlite3GetTempRange(tls, pParse, nConstraint+int32(2)) addrNotFound = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk j = 0 for { if !(j < nConstraint) { break } iTarget = iReg + j + int32(2) pTerm = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4)) if pTerm == uintptr(0) { goto _7 } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_IN) != 0 { if j <= int32(31) { v8 = libc.Uint32FromInt32(1) << j } else { v8 = uint32(0) } if v8&(*(*struct { FidxNum int32 F__ccgo4 uint8 FisOrdered Ti8 FomitMask Tu16 FidxStr uintptr FmHandleIn Tu32 })(unsafe.Pointer(pLoop + 24))).FmHandleIn != 0 { v10 = pParse + 40 v9 = *(*int32)(unsafe.Pointer(v10)) *(*int32)(unsafe.Pointer(v10))++ iTab = v9 v12 = pParse + 44 *(*int32)(unsafe.Pointer(v12))++ v11 = *(*int32)(unsafe.Pointer(v12)) iCache = v11 _sqlite3CodeRhsOfIN(tls, pParse, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr, iTab) _sqlite3VdbeAddOp3(tls, v, int32(OP_VInitIn), iTab, iTarget, iCache) } else { _codeEqualityTerm(tls, pParse, pTerm, pLevel, j, bRev, iTarget) addrNotFound = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrNxt } } else { pRight = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpRight _codeExprOrVector(tls, pParse, pRight, iTarget, int32(1)) if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeMatchOp) == int32(SQLITE_INDEX_CONSTRAINT_OFFSET) && int32(Tu32(*(*uint8)(unsafe.Pointer(pLoop + 24 + 4))&0x2>>1)) != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (*TSelect)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect)).FiOffset) } } goto _7 _7: ; j++ } _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), (*(*struct { FidxNum int32 F__ccgo4 uint8 FisOrdered Ti8 FomitMask Tu16 FidxStr uintptr FmHandleIn Tu32 })(unsafe.Pointer(pLoop + 24))).FidxNum, iReg) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), nConstraint, iReg+int32(1)) if int32(Tu32(*(*uint8)(unsafe.Pointer(pLoop + 24 + 4))&0x1>>0)) != 0 { v13 = -int32(6) } else { v13 = -int32(1) } _sqlite3VdbeAddOp4(tls, v, int32(OP_VFilter), iCur, addrNotFound, iReg, (*(*struct { FidxNum int32 F__ccgo4 uint8 FisOrdered Ti8 FomitMask Tu16 FidxStr uintptr FmHandleIn Tu32 })(unsafe.Pointer(pLoop + 24))).FidxStr, v13) libc.SetBitFieldPtr8Uint32(pLoop+24+4, libc.Uint32FromInt32(0), 0, 0x1) /* An OOM inside of AddOp4(OP_VFilter) instruction above might have freed ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */ if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { (*(*struct { FidxNum int32 F__ccgo4 uint8 FisOrdered Ti8 FomitMask Tu16 FidxStr uintptr FmHandleIn Tu32 })(unsafe.Pointer(pLoop + 24))).FidxStr = uintptr(0) } (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp1 = iCur if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass != 0 { v14 = int32(OP_Noop) } else { v14 = int32(OP_VNext) } (*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(v14) (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2 = _sqlite3VdbeCurrentAddr(tls, v) _ = libc.Int32FromInt32(0) j = 0 for { if !(j < nConstraint) { break } pTerm = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4)) if j < int32(16) && int32((*(*struct { FidxNum int32 F__ccgo4 uint8 FisOrdered Ti8 FomitMask Tu16 FidxStr uintptr FmHandleIn Tu32 })(unsafe.Pointer(pLoop + 24))).FomitMask)>>j&int32(1) != 0 { _disableTerm(tls, pLevel, pTerm) goto _15 } if v17 = int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_IN) != 0; v17 { if j <= int32(31) { v16 = libc.Uint32FromInt32(1) << j } else { v16 = uint32(0) } } if v17 && v16&(*(*struct { FidxNum int32 F__ccgo4 uint8 FisOrdered Ti8 FomitMask Tu16 FidxStr uintptr FmHandleIn Tu32 })(unsafe.Pointer(pLoop + 24))).FmHandleIn == uint32(0) && !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { /* IN loop corresponding to the j-th constraint */ /* Reload the constraint value into reg[iReg+j+2]. The same value ** was loaded into the same register prior to the OP_VFilter, but ** the xFilter implementation might have changed the datatype or ** encoding of the value in the register, so it *must* be reloaded. */ iIn = 0 for { if !(iIn < (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 64))).FnIn) { break } pOp = _sqlite3VdbeGetOp(tls, v, (*(*TInLoop)(unsafe.Pointer((*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 64))).FaInLoop + uintptr(iIn)*20))).FaddrInTop) if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Column) && (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 == iReg+j+int32(2) || int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Rowid) && (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 == iReg+j+int32(2) { _sqlite3VdbeAddOp3(tls, v, int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode), (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1, (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2, (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3) break } goto _18 _18: ; iIn++ } /* Generate code that will continue to the next row if ** the IN constraint is not satisfied */ pCompare = _sqlite3PExpr(tls, pParse, int32(TK_EQ), uintptr(0), uintptr(0)) if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { iFld = (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 24))).FiField pLeft = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpLeft _ = libc.Int32FromInt32(0) if iFld > 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer(pCompare)).FpLeft = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pLeft + 20)) + 8 + uintptr(iFld-int32(1))*20))).FpExpr } else { (*TExpr)(unsafe.Pointer(pCompare)).FpLeft = pLeft } v19 = _sqlite3Expr(tls, db, int32(TK_REGISTER), uintptr(0)) pRight1 = v19 (*TExpr)(unsafe.Pointer(pCompare)).FpRight = v19 if pRight1 != 0 { (*TExpr)(unsafe.Pointer(pRight1)).FiTable = iReg + j + int32(2) _sqlite3ExprIfFalse(tls, pParse, pCompare, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont, int32(SQLITE_JUMPIFNULL)) } (*TExpr)(unsafe.Pointer(pCompare)).FpLeft = uintptr(0) } _sqlite3ExprDelete(tls, db, pCompare) } goto _15 _15: ; j++ } /* These registers need to be preserved in case there is an IN operator ** loop. So we could deallocate the registers here (and potentially ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems ** simpler and safer to simply not reuse the registers. ** ** sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); */ } else { if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IPK) != uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_COLUMN_IN)|libc.Int32FromInt32(WHERE_COLUMN_EQ)) != uint32(0) { /* Case 2: We can directly reference a single row using an ** equality comparison against the ROWID field. Or ** we reference multiple rows using a "rowid IN (...)" ** construct. */ _ = libc.Int32FromInt32(0) pTerm = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) v21 = pParse + 44 *(*int32)(unsafe.Pointer(v21))++ v20 = *(*int32)(unsafe.Pointer(v21)) iReleaseReg = v20 iRowidReg = _codeEqualityTerm(tls, pParse, pTerm, pLevel, 0, bRev, iReleaseReg) if iRowidReg != iReleaseReg { _sqlite3ReleaseTempReg(tls, pParse, iReleaseReg) } addrNxt = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrNxt if (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_MustBeInt), iRowidReg, addrNxt) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Filter), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter, addrNxt, iRowidReg, int32(1)) _filterPullDown(tls, pParse, pWInfo, iLevel, addrNxt, notReady) } _sqlite3VdbeAddOp3(tls, v, int32(OP_SeekRowid), iCur, addrNxt, iRowidReg) (*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(OP_Noop) } else { if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IPK) != uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_COLUMN_RANGE) != uint32(0) { /* Case 3: We have an inequality comparison against the ROWID field. */ testOp = int32(OP_Noop) memEndValue = 0 j = 0 v22 = libc.UintptrFromInt32(0) pEnd = v22 pStart = v22 if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_BTM_LIMIT) != 0 { v23 = j j++ pStart = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(v23)*4)) } if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_TOP_LIMIT) != 0 { v24 = j j++ pEnd = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(v24)*4)) } _ = libc.Int32FromInt32(0) if bRev != 0 { pTerm = pStart pStart = pEnd pEnd = pTerm } if pStart != 0 { /* Cursor seek operation */ /* The following constant maps TK_xx codes into corresponding ** seek opcodes. It depends on a particular ordering of TK_xx */ aMoveOp = [4]Tu8{ 0: uint8(OP_SeekGT), 1: uint8(OP_SeekLE), 2: uint8(OP_SeekLT), 3: uint8(OP_SeekGE), } _ = libc.Int32FromInt32(0) /* Make sure the ordering.. */ _ = libc.Int32FromInt32(0) /* ... of the TK_xx values... */ _ = libc.Int32FromInt32(0) /* ... is correct. */ _ = libc.Int32FromInt32(0) pX = (*TWhereTerm)(unsafe.Pointer(pStart)).FpExpr _ = libc.Int32FromInt32(0) /* transitive constraints */ if _sqlite3ExprIsVector(tls, (*TExpr)(unsafe.Pointer(pX)).FpRight) != 0 { v25 = _sqlite3GetTempReg(tls, pParse) *(*int32)(unsafe.Pointer(bp)) = v25 r11 = v25 _codeExprOrVector(tls, pParse, (*TExpr)(unsafe.Pointer(pX)).FpRight, r11, int32(1)) op = int32(aMoveOp[(int32((*TExpr)(unsafe.Pointer(pX)).Fop)-int32(TK_GT)-int32(1))&int32(0x3)|int32(0x1)]) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } else { r11 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pX)).FpRight, bp) _disableTerm(tls, pLevel, pStart) op = int32(aMoveOp[int32((*TExpr)(unsafe.Pointer(pX)).Fop)-int32(TK_GT)]) } _sqlite3VdbeAddOp3(tls, v, op, iCur, addrBrk, r11) _sqlite3ReleaseTempReg(tls, pParse, *(*int32)(unsafe.Pointer(bp))) } else { if bRev != 0 { v26 = int32(OP_Last) } else { v26 = int32(OP_Rewind) } _sqlite3VdbeAddOp2(tls, v, v26, iCur, addrHalt) } if pEnd != 0 { pX1 = (*TWhereTerm)(unsafe.Pointer(pEnd)).FpExpr _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Transitive constraints */ v28 = pParse + 44 *(*int32)(unsafe.Pointer(v28))++ v27 = *(*int32)(unsafe.Pointer(v28)) memEndValue = v27 _codeExprOrVector(tls, pParse, (*TExpr)(unsafe.Pointer(pX1)).FpRight, memEndValue, int32(1)) if 0 == _sqlite3ExprIsVector(tls, (*TExpr)(unsafe.Pointer(pX1)).FpRight) && (int32((*TExpr)(unsafe.Pointer(pX1)).Fop) == int32(TK_LT) || int32((*TExpr)(unsafe.Pointer(pX1)).Fop) == int32(TK_GT)) { if bRev != 0 { v29 = int32(OP_Le) } else { v29 = int32(OP_Ge) } testOp = v29 } else { if bRev != 0 { v30 = int32(OP_Lt) } else { v30 = int32(OP_Gt) } testOp = v30 } if 0 == _sqlite3ExprIsVector(tls, (*TExpr)(unsafe.Pointer(pX1)).FpRight) { _disableTerm(tls, pLevel, pEnd) } } start = _sqlite3VdbeCurrentAddr(tls, v) if bRev != 0 { v31 = int32(OP_Prev) } else { v31 = int32(OP_Next) } (*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(v31) (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp1 = iCur (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2 = start _ = libc.Int32FromInt32(0) if testOp != int32(OP_Noop) { v33 = pParse + 44 *(*int32)(unsafe.Pointer(v33))++ v32 = *(*int32)(unsafe.Pointer(v33)) iRowidReg = v32 _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iCur, iRowidReg) _sqlite3VdbeAddOp3(tls, v, testOp, memEndValue, addrBrk, iRowidReg) _sqlite3VdbeChangeP5(tls, v, uint16(libc.Int32FromInt32(SQLITE_AFF_NUMERIC)|libc.Int32FromInt32(SQLITE_JUMPIFNULL))) } } else { if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_INDEXED) != 0 { nEq = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq /* Number of == or IN terms */ nBtm = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnBtm /* Length of BTM vector */ nTop = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnTop /* Base register holding constraint values */ pRangeStart = uintptr(0) /* Inequality constraint at range start */ pRangeEnd = uintptr(0) /* The VDBE cursor for the index */ nExtraReg = 0 /* Affinity for start of range constraint */ zEndAff = uintptr(0) /* Affinity for end of range constraint */ bSeekPastNull = uint8(0) /* True to seek past initial nulls */ bStopAtNull = uint8(0) /* True if we use the index only */ regBignull = 0 /* big-null flag register */ addrSeekScan = 0 /* Opcode of the OP_SeekScan, if any */ pIdx = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex iIdxCur = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur _ = libc.Int32FromInt32(0) /* Find any inequality constraint terms for the start and end ** of the range. */ j = int32(nEq) if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_BTM_LIMIT) != 0 { v34 = j j++ pRangeStart = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(v34)*4)) if nExtraReg > int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnBtm) { v35 = nExtraReg } else { v35 = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnBtm) } nExtraReg = v35 /* Like optimization range constraints always occur in pairs */ _ = libc.Int32FromInt32(0) } if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_TOP_LIMIT) != 0 { v36 = j j++ pRangeEnd = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(v36)*4)) if nExtraReg > int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnTop) { v37 = nExtraReg } else { v37 = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnTop) } nExtraReg = v37 if pRangeStart == uintptr(0) { j = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(nEq)*2))) if j >= 0 && int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol + uintptr(j)*12 + 4))&0xf>>0)) == 0 || j == -int32(2) { bSeekPastNull = uint8(1) } } } _ = libc.Int32FromInt32(0) /* If the WHERE_BIGNULL_SORT flag is set, then index column nEq uses ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS ** FIRST). In both cases separate ordered scans are made of those ** index entries for which the column is null and for those for which ** it is not. For an ASC sort, the non-NULL entries are scanned first. ** For DESC, NULL entries are scanned first. */ if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_TOP_LIMIT)|libc.Int32FromInt32(WHERE_BTM_LIMIT)) == uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_BIGNULL_SORT) != uint32(0) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) nExtraReg = int32(1) bSeekPastNull = uint8(1) v40 = pParse + 44 *(*int32)(unsafe.Pointer(v40))++ v39 = *(*int32)(unsafe.Pointer(v40)) v38 = v39 regBignull = v38 (*TWhereLevel)(unsafe.Pointer(pLevel)).FregBignull = v38 if (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regBignull) } (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBignull = _sqlite3VdbeMakeLabel(tls, pParse) } /* If we are doing a reverse order scan on an ascending index, or ** a forward order scan on a descending index, interchange the ** start and end terms (pRangeStart and pRangeEnd). */ if int32(nEq) < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) && bRev == libc.BoolInt32(int32(*(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder + uintptr(nEq)))) == SQLITE_SO_ASC) { t = pRangeEnd pRangeEnd = pRangeStart pRangeStart = t t1 = bSeekPastNull bSeekPastNull = bStopAtNull bStopAtNull = t1 t2 = uint8(nBtm) nBtm = nTop nTop = uint16(t2) } if iLevel > 0 && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IN_SEEKSCAN) != uint32(0) { /* In case OP_SeekScan is used, ensure that the index cursor does not ** point to a valid row for the first iteration of this loop. */ _sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), iIdxCur) } /* Generate code to evaluate all constraint terms using == or IN ** and store the values of those terms in an array of registers ** starting at regBase. */ regBase = _codeAllEqualityTerms(tls, pParse, pLevel, bRev, nExtraReg, bp+4) _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(bp + 4)) != 0 && nTop != 0 { zEndAff = _sqlite3DbStrDup(tls, db, *(*uintptr)(unsafe.Pointer(bp + 4))+uintptr(nEq)) } if regBignull != 0 { v41 = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBignull } else { v41 = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrNxt } addrNxt = v41 startEq = libc.BoolInt32(!(pRangeStart != 0) || int32((*TWhereTerm)(unsafe.Pointer(pRangeStart)).FeOperator)&(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ))) != 0) endEq = libc.BoolInt32(!(pRangeEnd != 0) || int32((*TWhereTerm)(unsafe.Pointer(pRangeEnd)).FeOperator)&(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ))) != 0) start_constraints = libc.BoolInt32(pRangeStart != 0 || int32(nEq) > 0) /* Seek the index cursor to the start of the range. */ nConstraint1 = int32(nEq) if pRangeStart != 0 { pRight2 = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pRangeStart)).FpExpr)).FpRight _codeExprOrVector(tls, pParse, pRight2, regBase+int32(nEq), int32(nBtm)) if int32((*TWhereTerm)(unsafe.Pointer(pRangeStart)).FwtFlags)&int32(TERM_VNULL) == 0 && _sqlite3ExprCanBeNull(tls, pRight2) != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regBase+int32(nEq), addrNxt) } if *(*uintptr)(unsafe.Pointer(bp + 4)) != 0 { _updateRangeAffinityStr(tls, pRight2, int32(nBtm), *(*uintptr)(unsafe.Pointer(bp + 4))+uintptr(nEq)) } nConstraint1 += int32(nBtm) if _sqlite3ExprIsVector(tls, pRight2) == 0 { _disableTerm(tls, pLevel, pRangeStart) } else { startEq = int32(1) } bSeekPastNull = uint8(0) } else { if bSeekPastNull != 0 { startEq = 0 _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regBase+int32(nEq)) start_constraints = int32(1) nConstraint1++ } else { if regBignull != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regBase+int32(nEq)) start_constraints = int32(1) nConstraint1++ } } } _codeApplyAffinity(tls, pParse, regBase, nConstraint1-int32(bSeekPastNull), *(*uintptr)(unsafe.Pointer(bp + 4))) if int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip) > 0 && nConstraint1 == int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip) { /* The skip-scan logic inside the call to codeAllEqualityConstraints() ** above has already left the cursor sitting on the correct row, ** so no further seeking is needed */ } else { if regBignull != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), regBignull) } if (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter != 0 { _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Filter), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter, addrNxt, regBase, int32(nEq)) _filterPullDown(tls, pParse, pWInfo, iLevel, addrNxt, notReady) } op1 = int32(_aStartOp[start_constraints< int32(1))*int32(4)+int32(2)+bRev]) _sqlite3VdbeAddOp4Int(tls, v, op1, iIdxCur, addrNxt, regBase, nConstraint1-startEq) _ = libc.Int32FromInt32(0) } } /* Load the value for the inequality constraint at the end of the ** range (if any). */ nConstraint1 = int32(nEq) _ = libc.Int32FromInt32(0) if pRangeEnd != 0 { pRight3 = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pRangeEnd)).FpExpr)).FpRight _ = libc.Int32FromInt32(0) _codeExprOrVector(tls, pParse, pRight3, regBase+int32(nEq), int32(nTop)) if int32((*TWhereTerm)(unsafe.Pointer(pRangeEnd)).FwtFlags)&int32(TERM_VNULL) == 0 && _sqlite3ExprCanBeNull(tls, pRight3) != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regBase+int32(nEq), addrNxt) } if zEndAff != 0 { _updateRangeAffinityStr(tls, pRight3, int32(nTop), zEndAff) _codeApplyAffinity(tls, pParse, regBase+int32(nEq), int32(nTop), zEndAff) } else { _ = libc.Int32FromInt32(0) } nConstraint1 += int32(nTop) if _sqlite3ExprIsVector(tls, pRight3) == 0 { _disableTerm(tls, pLevel, pRangeEnd) } else { endEq = int32(1) } } else { if bStopAtNull != 0 { if regBignull == 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regBase+int32(nEq)) endEq = 0 } nConstraint1++ } } if *(*uintptr)(unsafe.Pointer(bp + 4)) != 0 { _sqlite3DbNNFreeNN(tls, db, *(*uintptr)(unsafe.Pointer(bp + 4))) } if zEndAff != 0 { _sqlite3DbNNFreeNN(tls, db, zEndAff) } /* Top of the loop body */ (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2 = _sqlite3VdbeCurrentAddr(tls, v) /* Check if the index cursor is past the end of the range. */ if nConstraint1 != 0 { if regBignull != 0 { /* Except, skip the end-of-range check while doing the NULL-scan */ _sqlite3VdbeAddOp2(tls, v, int32(OP_IfNot), regBignull, _sqlite3VdbeCurrentAddr(tls, v)+int32(3)) } op1 = int32(_aEndOp[bRev*int32(2)+endEq]) _sqlite3VdbeAddOp4Int(tls, v, op1, iIdxCur, addrNxt, regBase, nConstraint1) if addrSeekScan != 0 { _sqlite3VdbeJumpHere(tls, v, addrSeekScan) } } if regBignull != 0 { /* During a NULL-scan, check to see if we have reached the end of ** the NULLs */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_If), regBignull, _sqlite3VdbeCurrentAddr(tls, v)+int32(2)) op1 = int32(_aEndOp[bRev*int32(2)+int32(bSeekPastNull)]) _sqlite3VdbeAddOp4Int(tls, v, op1, iIdxCur, addrNxt, regBase, nConstraint1+int32(bSeekPastNull)) } if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IN_EARLYOUT) != uint32(0) { _sqlite3VdbeAddOp3(tls, v, int32(OP_SeekHit), iIdxCur, int32(nEq), int32(nEq)) } /* Seek the table cursor, if required */ omitTable = libc.BoolInt32((*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IDX_ONLY) != uint32(0) && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&(libc.Int32FromInt32(WHERE_OR_SUBCLAUSE)|libc.Int32FromInt32(WHERE_RIGHT_JOIN)) == 0) if omitTable != 0 { /* pIdx is a covering index. No need to access the main table. */ } else { if (*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { _codeDeferredSeek(tls, pWInfo, pIdx, iCur, iIdxCur) } else { if iCur != iIdxCur { pPk = _sqlite3PrimaryKeyIndex(tls, (*TIndex)(unsafe.Pointer(pIdx)).FpTable) iRowidReg = _sqlite3GetTempRange(tls, pParse, int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) j = 0 for { if !(j < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) { break } k = int32(_sqlite3TableColumnToIndex(tls, pIdx, *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(j)*2)))) _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iIdxCur, k, iRowidReg+j) goto _42 _42: ; j++ } _sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), iCur, addrCont, iRowidReg, int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) } } } if (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin == 0 { /* If a partial index is driving the loop, try to eliminate WHERE clause ** terms from the query that must be true due to the WHERE clause of ** the partial index. ** ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work ** for a LEFT JOIN. */ if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 { _whereApplyPartialIndexConstraints(tls, (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere, iCur, pWC) } } else { /* The following assert() is not a requirement, merely an observation: ** The OR-optimization doesn't work for the right hand table of ** a LEFT JOIN: */ _ = libc.Int32FromInt32(0) } /* Record the instruction used to terminate the loop. */ if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_ONEROW) != 0 { (*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(OP_Noop) } else { if bRev != 0 { (*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(OP_Prev) } else { (*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(OP_Next) } } (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp1 = iIdxCur if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_UNQ_WANTED) != uint32(0) { v43 = int32(1) } else { v43 = 0 } (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp3 = uint8(v43) if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_CONSTRAINT) == uint32(0) { (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp5 = uint8(SQLITE_STMTSTATUS_FULLSCAN_STEP) } else { _ = libc.Int32FromInt32(0) } if omitTable != 0 { pIdx = uintptr(0) } } else { if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_MULTI_OR) != 0 { /* Shortened table list or OR-clause generation */ pCov = uintptr(0) v45 = pParse + 40 v44 = *(*int32)(unsafe.Pointer(v45)) *(*int32)(unsafe.Pointer(v45))++ /* Potential covering index (or NULL) */ iCovCur = v44 v47 = pParse + 44 *(*int32)(unsafe.Pointer(v47))++ v46 = *(*int32)(unsafe.Pointer(v47)) /* Cursor used for index scans (if any) */ regReturn = v46 /* Register used with OP_Gosub */ regRowset = 0 /* Register for RowSet object */ regRowid = 0 /* Register holding rowid */ iLoopBody = _sqlite3VdbeMakeLabel(tls, pParse) /* Address of regReturn init */ untestedTerms = 0 /* Loop counter */ pAndExpr = uintptr(0) /* An ".. AND (...)" expression */ pTab = (*TSrcItem)(unsafe.Pointer(pTabItem)).FpTab pTerm = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pOrWc = *(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pTerm)).Fu)) (*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(OP_Return) (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp1 = regReturn /* Set up a new SrcList in pOrTab containing the table being scanned ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). */ if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) > int32(1) { /* Original list of tables */ nNotReady = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) - iLevel - int32(1) pOrTab = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(80)+uint32(nNotReady)*uint32(72))) if pOrTab == uintptr(0) { return notReady } (*TSrcList)(unsafe.Pointer(pOrTab)).FnAlloc = uint32(uint8(nNotReady + libc.Int32FromInt32(1))) (*TSrcList)(unsafe.Pointer(pOrTab)).FnSrc = int32((*TSrcList)(unsafe.Pointer(pOrTab)).FnAlloc) libc.Xmemcpy(tls, pOrTab+8, pTabItem, uint32(72)) origSrc = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 k = int32(1) for { if !(k <= nNotReady) { break } libc.Xmemcpy(tls, pOrTab+8+uintptr(k)*72, origSrc+uintptr((*(*TWhereLevel)(unsafe.Pointer(pLevel + uintptr(k)*88))).FiFrom)*72, uint32(72)) goto _48 _48: ; k++ } } else { pOrTab = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList } /* Initialize the rowset register to contain NULL. An SQL NULL is ** equivalent to an empty rowset. Or, create an ephemeral index ** capable of holding primary keys in the case of a WITHOUT ROWID. ** ** Also initialize regReturn to contain the address of the instruction ** immediately following the OP_Return at the bottom of the loop. This ** is required in a few obscure LEFT JOIN cases where control jumps ** over the top of the loop into the body of it. In this case the ** correct response for the end-of-loop code (the OP_Return) is to ** fall through to the next instruction, just as an OP_Next does if ** called on an uninitialized cursor. */ if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_DUPLICATES_OK) == 0 { if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { v50 = pParse + 44 *(*int32)(unsafe.Pointer(v50))++ v49 = *(*int32)(unsafe.Pointer(v50)) regRowset = v49 _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regRowset) } else { pPk1 = _sqlite3PrimaryKeyIndex(tls, pTab) v52 = pParse + 40 v51 = *(*int32)(unsafe.Pointer(v52)) *(*int32)(unsafe.Pointer(v52))++ regRowset = v51 _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), regRowset, int32((*TIndex)(unsafe.Pointer(pPk1)).FnKeyCol)) _sqlite3VdbeSetP4KeyInfo(tls, pParse, pPk1) } v54 = pParse + 44 *(*int32)(unsafe.Pointer(v54))++ v53 = *(*int32)(unsafe.Pointer(v54)) regRowid = v53 } iRetInit = _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regReturn) /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y ** Then for every term xN, evaluate as the subexpression: xN AND y ** That way, terms in y that are factored into the disjunction will ** be picked up by the recursive calls to sqlite3WhereBegin() below. ** ** Actually, each subexpression is converted to "xN AND w" where w is ** the "interesting" terms of z - terms that did not originate in the ** ON or USING clause of a LEFT JOIN, and terms that are usable as ** indices. ** ** This optimization also only applies if the (x1 OR x2 OR ...) term ** is not contained in the ON clause of a LEFT JOIN. ** See ticket http://www.sqlite.org/src/info/f2369304e4 ** ** 2022-02-04: Do not push down slices of a row-value comparison. ** In other words, "w" or "y" may not be a slice of a vector. Otherwise, ** the initialization of the right-hand operand of the vector comparison ** might not occur, or might occur only in an OR branch that is not ** taken. dbsqlfuzz 80a9fade844b4fb43564efc972bcb2c68270f5d1. ** ** 2022-03-03: Do not push down expressions that involve subqueries. ** The subquery might get coded as a subroutine. Any table-references ** in the subquery might be resolved to index-references for the index on ** the OR branch in which the subroutine is coded. But if the subroutine ** is invoked from a different OR branch that uses a different index, such ** index-references will not work. tag-20220303a ** https://sqlite.org/forum/forumpost/36937b197273d403 */ if (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm > int32(1) { iTerm = 0 for { if !(iTerm < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) { break } pExpr = (*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(iTerm)*48))).FpExpr if (*TWhereClause)(unsafe.Pointer(pWC)).Fa+uintptr(iTerm)*48 == pTerm { goto _55 } if int32((*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(iTerm)*48))).FwtFlags)&(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_CODED)|libc.Int32FromInt32(TERM_SLICE)) != 0 { goto _55 } if int32((*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(iTerm)*48))).FeOperator)&int32(WO_ALL) == 0 { goto _55 } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Subquery)) != uint32(0) { goto _55 } /* tag-20220303a */ pExpr = _sqlite3ExprDup(tls, db, pExpr, 0) pAndExpr = _sqlite3ExprAnd(tls, pParse, pAndExpr, pExpr) goto _55 _55: ; iTerm++ } if pAndExpr != 0 { /* The extra 0x10000 bit on the opcode is masked off and does not ** become part of the new Expr.op. However, it does make the ** op==TK_AND comparison inside of sqlite3PExpr() false, and this ** prevents sqlite3PExpr() from applying the AND short-circuit ** optimization, which we do not want here. */ pAndExpr = _sqlite3PExpr(tls, pParse, libc.Int32FromInt32(TK_AND)|libc.Int32FromInt32(0x10000), uintptr(0), pAndExpr) } } /* Run a separate WHERE clause for each term of the OR clause. After ** eliminating duplicates from other WHERE clauses, the action for each ** sub-WHERE clause is to to invoke the main loop body as a subroutine. */ _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+22265, 0) ii = 0 for { if !(ii < (*TWhereClause)(unsafe.Pointer(pOrWc)).FnTerm) { break } pOrTerm = (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa + uintptr(ii)*48 if (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor == iCur || int32((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FeOperator)&int32(WO_AND) != 0 { /* Info for single OR-term scan */ pOrExpr = (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr /* Local copy of OR clause term */ jmp1 = 0 /* Address of jump operation */ /* See TH3 vtab25.400 and ticket 614b25314c766238 */ v57 = _sqlite3ExprDup(tls, db, pOrExpr, 0) pOrExpr = v57 pDelete = v57 if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _sqlite3ExprDelete(tls, db, pDelete) goto _56 } if pAndExpr != 0 { (*TExpr)(unsafe.Pointer(pAndExpr)).FpLeft = pOrExpr pOrExpr = pAndExpr } /* Loop through table entries that match term pOrTerm. */ _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+22280, libc.VaList(bp+72, ii+int32(1))) pSubWInfo = _sqlite3WhereBegin(tls, pParse, pOrTab, pOrExpr, uintptr(0), uintptr(0), uintptr(0), uint16(WHERE_OR_SUBCLAUSE), iCovCur) _ = libc.Int32FromInt32(0) if pSubWInfo != 0 { addrExplain = _sqlite3WhereExplainOneScan(tls, pParse, pOrTab, pSubWInfo+760, uint16(0)) _ = addrExplain /* This is the sub-WHERE clause body. First skip over ** duplicate rows from prior sub-WHERE clauses, and record the ** rowid (or PRIMARY KEY) for the current row so that the same ** row will be skipped in subsequent sub-WHERE clauses. */ if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_DUPLICATES_OK) == 0 { if ii == (*TWhereClause)(unsafe.Pointer(pOrWc)).FnTerm-int32(1) { v58 = -int32(1) } else { v58 = ii } iSet = v58 if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { _sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iCur, -int32(1), regRowid) jmp1 = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_RowSetTest), regRowset, 0, regRowid, iSet) } else { pPk2 = _sqlite3PrimaryKeyIndex(tls, pTab) nPk = int32((*TIndex)(unsafe.Pointer(pPk2)).FnKeyCol) /* Read the PK into an array of temp registers. */ r = _sqlite3GetTempRange(tls, pParse, nPk) iPk = 0 for { if !(iPk < nPk) { break } iCol = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk2)).FaiColumn + uintptr(iPk)*2))) _sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iCur, iCol, r+iPk) goto _59 _59: ; iPk++ } /* Check if the temp table already contains this key. If so, ** the row has already been included in the result set and ** can be ignored (by jumping past the Gosub below). Otherwise, ** insert the key into the temp table and proceed with processing ** the row. ** ** Use some of the same optimizations as OP_RowSetTest: If iSet ** is zero, assume that the key cannot already be present in ** the temp table. And if iSet is -1, assume that there is no ** need to insert the key into the temp table, as it will never ** be tested for. */ if iSet != 0 { jmp1 = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), regRowset, 0, r, nPk) } if iSet >= 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), r, nPk, regRowid) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), regRowset, regRowid, r, nPk) if iSet != 0 { _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_USESEEKRESULT)) } } /* Release the array of temp registers */ _sqlite3ReleaseTempRange(tls, pParse, r, nPk) } } /* Invoke the main loop body as a subroutine */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regReturn, iLoopBody) /* Jump here (skipping the main loop body subroutine) if the ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */ if jmp1 != 0 { _sqlite3VdbeJumpHere(tls, v, jmp1) } /* The pSubWInfo->untestedTerms flag means that this OR term ** contained one or more AND term from a notReady table. The ** terms from the notReady table could not be tested and will ** need to be tested later. */ if int32(uint32(*(*uint8)(unsafe.Pointer(pSubWInfo + 48))&0x2>>1)) != 0 { untestedTerms = int32(1) } /* If all of the OR-connected terms are optimized using the same ** index, and the index is opened using the same cursor number ** by each call to sqlite3WhereBegin() made by this loop, it may ** be possible to use that index as a covering index. ** ** If the call to sqlite3WhereBegin() above resulted in a scan that ** uses an index, and this is either the first OR-connected term ** processed or the index is the same as that used by all previous ** terms, set pCov to the candidate covering index. Otherwise, set ** pCov to NULL to indicate that no candidate covering index will ** be available. */ pSubLoop = (*(*TWhereLevel)(unsafe.Pointer(pSubWInfo + 760))).FpWLoop _ = libc.Int32FromInt32(0) if (*TWhereLoop)(unsafe.Pointer(pSubLoop)).FwsFlags&uint32(WHERE_INDEXED) != uint32(0) && (ii == 0 || (*TWhereLoop)(unsafe.Pointer(pSubLoop)).Fu.Fbtree.FpIndex == pCov) && ((*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) || !(int32(uint32(*(*uint16)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pSubLoop)).Fu.Fbtree.FpIndex + 56))&0x3>>0)) == libc.Int32FromInt32(SQLITE_IDXTYPE_PRIMARYKEY))) { _ = libc.Int32FromInt32(0) pCov = (*TWhereLoop)(unsafe.Pointer(pSubLoop)).Fu.Fbtree.FpIndex } else { pCov = uintptr(0) } if _sqlite3WhereUsesDeferredSeek(tls, pSubWInfo) != 0 { libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(1), 0, 0x1) } /* Finish the loop through table entries that match term pOrTerm. */ _sqlite3WhereEnd(tls, pSubWInfo) _sqlite3VdbeExplainPop(tls, pParse) } _sqlite3ExprDelete(tls, db, pDelete) } goto _56 _56: ; ii++ } _sqlite3VdbeExplainPop(tls, pParse) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(&(*TWhereLevel)(unsafe.Pointer(pLevel)).Fu)) = pCov if pCov != 0 { (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur = iCovCur } if pAndExpr != 0 { (*TExpr)(unsafe.Pointer(pAndExpr)).FpLeft = uintptr(0) _sqlite3ExprDelete(tls, db, pAndExpr) } _sqlite3VdbeChangeP1(tls, v, iRetInit, _sqlite3VdbeCurrentAddr(tls, v)) _sqlite3VdbeGoto(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk) _sqlite3VdbeResolveLabel(tls, v, iLoopBody) /* Set the P2 operand of the OP_Return opcode that will end the current ** loop to point to this spot, which is the top of the next containing ** loop. The byte-code formatter will use that P2 value as a hint to ** indent everything in between the this point and the final OP_Return. ** See tag-20220407a in vdbe.c and shell.c */ _ = libc.Int32FromInt32(0) (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2 = _sqlite3VdbeCurrentAddr(tls, v) if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) > int32(1) { _sqlite3DbFreeNN(tls, db, pOrTab) } if !(untestedTerms != 0) { _disableTerm(tls, pLevel, pTerm) } } else { _ = libc.Int32FromInt32(0) if int32(uint32(*(*uint16)(unsafe.Pointer(pTabItem + 36 + 4))&0x40>>6)) != 0 { /* Tables marked isRecursive have only a single row that is stored in ** a pseudo-cursor. No need to Rewind or Next such cursors. */ (*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(OP_Noop) } else { (*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = _aStep[bRev] (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp1 = iCur (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2 = int32(1) + _sqlite3VdbeAddOp2(tls, v, int32(_aStart[bRev]), iCur, addrHalt) (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp5 = uint8(SQLITE_STMTSTATUS_FULLSCAN_STEP) } } } } } } } /* Insert code to test every subexpression that can be completely ** computed using the current set of tables. ** ** This loop may run between one and three times, depending on the ** constraints to be generated. The value of stack variable iLoop ** determines the constraints coded by each iteration, as follows: ** ** iLoop==1: Code only expressions that are entirely covered by pIdx. ** iLoop==2: Code remaining expressions that do not contain correlated ** sub-queries. ** iLoop==3: Code all remaining expressions. ** ** An effort is made to skip unnecessary iterations of the loop. */ if pIdx != 0 { v60 = int32(1) } else { v60 = int32(2) } iLoop = v60 for cond := true; cond; cond = iLoop > 0 { iNext = 0 /* Next value for iLoop */ pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa j = (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm for { if !(j > 0) { break } skipLikeAddr = 0 if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_CODED)) != 0 { goto _61 } if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll&(*TWhereLevel)(unsafe.Pointer(pLevel)).FnotReady != uint64(0) { libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(1), 1, 0x2) goto _61 } pE = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr _ = libc.Int32FromInt32(0) if int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0 { if !((*TExpr)(unsafe.Pointer(pE)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != libc.Uint32FromInt32(0)) { /* Defer processing WHERE clause constraints until after outer ** join processing. tag-20220513a */ goto _61 } else { if int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&int32(JT_LEFT) == int32(JT_LEFT) && !((*TExpr)(unsafe.Pointer(pE)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != libc.Uint32FromInt32(0)) { goto _61 } else { m = _sqlite3WhereGetMask(tls, pWInfo+496, *(*int32)(unsafe.Pointer(pE + 36))) if m&(*TWhereLevel)(unsafe.Pointer(pLevel)).FnotReady != 0 { /* An ON clause that is not ripe */ goto _61 } } } } if iLoop == int32(1) && !(_sqlite3ExprCoveredByIndex(tls, pE, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, pIdx) != 0) { iNext = int32(2) goto _61 } if iLoop < int32(3) && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VARSELECT) != 0 { if iNext == 0 { iNext = int32(3) } goto _61 } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_LIKECOND) != 0 { /* If the TERM_LIKECOND flag is set, that means that the range search ** is sufficient to guarantee that the LIKE operator is true, so we ** can skip the call to the like(A,B) function. But this only works ** for strings. So do not skip the call to the function on the pass ** that compares BLOBs. */ goto _61 } _sqlite3ExprIfFalse(tls, pParse, pE, addrCont, int32(SQLITE_JUMPIFNULL)) if skipLikeAddr != 0 { _sqlite3VdbeJumpHere(tls, v, skipLikeAddr) } p62 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p62)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p62))) | libc.Int32FromInt32(TERM_CODED)) goto _61 _61: ; j-- pTerm += 48 } iLoop = iNext } /* Insert code to test for implied constraints based on transitivity ** of the "==" operator. ** ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" ** and we are coding the t1 loop and the t2 loop has not yet coded, ** then we cannot use the "t1.a=t2.b" constraint, but we can code ** the implied "t1.a=123" constraint. */ pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa j = (*TWhereClause)(unsafe.Pointer(pWC)).FnBase for { if !(j > 0) { break } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_CODED)) != 0 { goto _63 } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) == 0 { goto _63 } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_EQUIV) == 0 { goto _63 } if (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor != iCur { goto _63 } if int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0 { goto _63 } pE1 = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pAlt = _sqlite3WhereFindTerm(tls, pWC, iCur, (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 24))).FleftColumn, notReady, uint32(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IN)|libc.Int32FromInt32(WO_IS)), uintptr(0)) if pAlt == uintptr(0) { goto _63 } if int32((*TWhereTerm)(unsafe.Pointer(pAlt)).FwtFlags)&int32(TERM_CODED) != 0 { goto _63 } if int32((*TWhereTerm)(unsafe.Pointer(pAlt)).FeOperator)&int32(WO_IN) != 0 && (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pAlt)).FpExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) && (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pAlt)).FpExpr + 20)))).FpEList)).FnExpr > int32(1) { goto _63 } *(*TExpr)(unsafe.Pointer(bp + 8)) = *(*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pAlt)).FpExpr)) (*(*TExpr)(unsafe.Pointer(bp + 8))).FpLeft = (*TExpr)(unsafe.Pointer(pE1)).FpLeft _sqlite3ExprIfFalse(tls, pParse, bp+8, addrCont, int32(SQLITE_JUMPIFNULL)) p64 = pAlt + 10 *(*Tu16)(unsafe.Pointer(p64)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p64))) | libc.Int32FromInt32(TERM_CODED)) goto _63 _63: ; j-- pTerm += 48 } /* For a RIGHT OUTER JOIN, record the fact that the current row has ** been matched at least once. */ if (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ != 0 { jmp11 = 0 pRJ = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ /* pTab is the right-hand table of the RIGHT JOIN. Generate code that ** will record that the current row of that table has been matched at ** least once. This is accomplished by storing the PK for the row in ** both the iMatch index and the regBloom Bloom filter. */ pTab1 = (*(*TSrcItem)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*72))).FpTab if (*TTable)(unsafe.Pointer(pTab1)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { r1 = _sqlite3GetTempRange(tls, pParse, int32(2)) _sqlite3ExprCodeGetColumnOfTable(tls, v, pTab1, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, -int32(1), r1+int32(1)) nPk1 = int32(1) } else { pPk3 = _sqlite3PrimaryKeyIndex(tls, pTab1) nPk1 = int32((*TIndex)(unsafe.Pointer(pPk3)).FnKeyCol) r1 = _sqlite3GetTempRange(tls, pParse, nPk1+int32(1)) iPk1 = 0 for { if !(iPk1 < nPk1) { break } iCol1 = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk3)).FaiColumn + uintptr(iPk1)*2))) _sqlite3ExprCodeGetColumnOfTable(tls, v, pTab1, iCur, iCol1, r1+int32(1)+iPk1) goto _65 _65: ; iPk1++ } } jmp11 = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FiMatch, 0, r1+int32(1), nPk1) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), r1+int32(1), nPk1, r1) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FiMatch, r1, r1+int32(1), nPk1) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_FilterAdd), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregBloom, 0, r1+int32(1), nPk1) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_USESEEKRESULT)) _sqlite3VdbeJumpHere(tls, v, jmp11) _sqlite3ReleaseTempRange(tls, pParse, r1, nPk1+int32(1)) } /* For a LEFT OUTER JOIN, generate code that will record the fact that ** at least one row of the right table has matched the left table. */ if (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin != 0 { (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrFirst = _sqlite3VdbeCurrentAddr(tls, v) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin) if (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ == uintptr(0) { goto code_outer_join_constraints /* WHERE clause constraints */ } } if !((*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ != 0) { goto _66 } /* Create a subroutine used to process all interior loops and code ** of the RIGHT JOIN. During normal operation, the subroutine will ** be in-line with the rest of the code. But at the end, a separate ** loop will run that invokes this subroutine for unmatched rows ** of pTab, with all tables to left begin set to NULL. */ pRJ1 = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ _sqlite3VdbeAddOp2(tls, v, int32(OP_BeginSubrtn), 0, (*TWhereRightJoin)(unsafe.Pointer(pRJ1)).FregReturn) (*TWhereRightJoin)(unsafe.Pointer(pRJ1)).FaddrSubrtn = _sqlite3VdbeCurrentAddr(tls, v) _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).FwithinRJSubrtn++ /* WHERE clause constraints must be deferred until after outer join ** row elimination has completed, since WHERE clause constraints apply ** to the results of the OUTER JOIN. The following loop generates the ** appropriate WHERE clause constraint checks. tag-20220513a. */ goto code_outer_join_constraints code_outer_join_constraints: ; pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa j = libc.Int32FromInt32(0) for { if !(j < (*TWhereClause)(unsafe.Pointer(pWC)).FnBase) { break } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_CODED)) != 0 { goto _67 } if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll&(*TWhereLevel)(unsafe.Pointer(pLevel)).FnotReady != uint64(0) { _ = libc.Int32FromInt32(0) goto _67 } if int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&int32(JT_LTORJ) != 0 { goto _67 } _ = libc.Int32FromInt32(0) _sqlite3ExprIfFalse(tls, pParse, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr, addrCont, int32(SQLITE_JUMPIFNULL)) p68 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p68)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p68))) | libc.Int32FromInt32(TERM_CODED)) goto _67 _67: ; j++ pTerm += 48 } _66: ; return (*TWhereLevel)(unsafe.Pointer(pLevel)).FnotReady } /* Case 4: A scan using an index. ** ** The WHERE clause may contain zero or more equality ** terms ("==" or "IN" operators) that refer to the N ** left-most columns of the index. It may also contain ** inequality constraints (>, <, >= or <=) on the indexed ** column that immediately follows the N equalities. Only ** the right-most column can be an inequality - the rest must ** use the "==" and "IN" operators. For example, if the ** index is on (x,y,z), then the following clauses are all ** optimized: ** ** x=5 ** x=5 AND y=10 ** x=5 AND y<10 ** x=5 AND y>5 AND y<10 ** x=5 AND y=5 AND z<=10 ** ** The z<10 term of the following cannot be used, only ** the x=5 term: ** ** x=5 AND z<10 ** ** N may be zero if there are inequality constraints. ** If there are no inequality constraints, then N is at ** least one. ** ** This case is also used when there are no WHERE clause ** constraints but an index is selected anyway, in order ** to force the output order to conform to an ORDER BY. */ var _aStartOp = [8]Tu8{ 2: uint8(OP_Rewind), 3: uint8(OP_Last), 4: uint8(OP_SeekGT), 5: uint8(OP_SeekLT), 6: uint8(OP_SeekGE), 7: uint8(OP_SeekLE), } var _aEndOp = [4]Tu8{ 0: uint8(OP_IdxGE), 1: uint8(OP_IdxGT), 2: uint8(OP_IdxLE), 3: uint8(OP_IdxLT), } /* Case 6: There is no usable index. We must do a complete ** scan of the entire table. */ var _aStep = [2]Tu8{ 0: uint8(OP_Next), 1: uint8(OP_Prev), } var _aStart = [2]Tu8{ 0: uint8(OP_Rewind), 1: uint8(OP_Last), } // C documentation // // /* // ** Generate the code for the loop that finds all non-matched terms // ** for a RIGHT JOIN. // */ func _sqlite3WhereRightJoinLoop(tls *libc.TLS, pWInfo uintptr, iLevel int32, pLevel uintptr) { bp := tls.Alloc(96) defer tls.Free(96) var addrCont, iCol, iCur, iIdxCur, iPk, jmp, k, nPk, r, v3 int32 var mAll TBitmask var pLoop, pParse, pPk, pRJ, pSubWInfo, pSubWhere, pTab, pTabItem, pTerm, pWC, v, v4 uintptr var _ /* sFrom at bp+0 */ TSrcList _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrCont, iCol, iCur, iIdxCur, iPk, jmp, k, mAll, nPk, pLoop, pParse, pPk, pRJ, pSubWInfo, pSubWhere, pTab, pTabItem, pTerm, pWC, r, v, v3, v4 pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe pRJ = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ pSubWhere = uintptr(0) pWC = pWInfo + 80 pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop pTabItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*72 mAll = uint64(0) _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+22289, libc.VaList(bp+88, (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pTabItem)).FpTab)).FzName)) k = 0 for { if !(k < iLevel) { break } mAll |= (*TWhereLoop1)(unsafe.Pointer((*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(k)*88))).FpWLoop)).FmaskSelf _sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(k)*88))).FiTabCur) iIdxCur = (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(k)*88))).FiIdxCur if iIdxCur != 0 { _sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), iIdxCur) } goto _1 _1: ; k++ } if int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&int32(JT_LTORJ) == 0 { mAll |= (*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf k = 0 for { if !(k < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) { break } pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(k)*48 if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_SLICE)) != 0 && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator) != int32(WO_ROWVAL) { break } if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll & ^mAll != 0 { goto _2 } if (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != uint32(0) { goto _2 } pSubWhere = _sqlite3ExprAnd(tls, pParse, pSubWhere, _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr, 0)) goto _2 _2: ; k++ } } (*(*TSrcList)(unsafe.Pointer(bp))).FnSrc = int32(1) (*(*TSrcList)(unsafe.Pointer(bp))).FnAlloc = uint32(1) libc.Xmemcpy(tls, bp+8, pTabItem, uint32(72)) (*(*TSrcItem)(unsafe.Pointer(bp + 8))).Ffg.Fjointype = uint8(0) _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).FwithinRJSubrtn++ pSubWInfo = _sqlite3WhereBegin(tls, pParse, bp, pSubWhere, uintptr(0), uintptr(0), uintptr(0), uint16(WHERE_RIGHT_JOIN), 0) if pSubWInfo != 0 { iCur = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur v4 = pParse + 44 *(*int32)(unsafe.Pointer(v4))++ v3 = *(*int32)(unsafe.Pointer(v4)) r = v3 addrCont = _sqlite3WhereContinueLabel(tls, pSubWInfo) pTab = (*TSrcItem)(unsafe.Pointer(pTabItem)).FpTab if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { _sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iCur, -int32(1), r) nPk = int32(1) } else { pPk = _sqlite3PrimaryKeyIndex(tls, pTab) nPk = int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol) *(*int32)(unsafe.Pointer(pParse + 44)) += nPk - int32(1) iPk = 0 for { if !(iPk < nPk) { break } iCol = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(iPk)*2))) _sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iCur, iCol, r+iPk) goto _5 _5: ; iPk++ } } jmp = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Filter), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregBloom, 0, r, nPk) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FiMatch, addrCont, r, nPk) _sqlite3VdbeJumpHere(tls, v, jmp) _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregReturn, (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FaddrSubrtn) _sqlite3WhereEnd(tls, pSubWInfo) } _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSubWhere) _sqlite3VdbeExplainPop(tls, pParse) _ = libc.Int32FromInt32(0) (*TParse)(unsafe.Pointer(pParse)).FwithinRJSubrtn-- } // C documentation // // /* // ** Deallocate all memory associated with a WhereOrInfo object. // */ func _whereOrInfoDelete(tls *libc.TLS, db uintptr, p uintptr) { _sqlite3WhereClauseClear(tls, p) _sqlite3DbFree(tls, db, p) } // C documentation // // /* // ** Deallocate all memory associated with a WhereAndInfo object. // */ func _whereAndInfoDelete(tls *libc.TLS, db uintptr, p uintptr) { _sqlite3WhereClauseClear(tls, p) _sqlite3DbFree(tls, db, p) } // C documentation // // /* // ** Add a single new WhereTerm entry to the WhereClause object pWC. // ** The new WhereTerm object is constructed from Expr p and with wtFlags. // ** The index in pWC->a[] of the new WhereTerm is returned on success. // ** 0 is returned if the new WhereTerm could not be added due to a memory // ** allocation error. The memory allocation failure will be recorded in // ** the db->mallocFailed flag so that higher-level functions can detect it. // ** // ** This routine will increase the size of the pWC->a[] array as necessary. // ** // ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility // ** for freeing the expression p is assumed by the WhereClause object pWC. // ** This is true even if this routine fails to allocate a new WhereTerm. // ** // ** WARNING: This routine might reallocate the space used to store // ** WhereTerms. All pointers to WhereTerms should be invalidated after // ** calling this routine. Such pointers may be reinitialized by referencing // ** the pWC->a[] array. // */ func _whereClauseInsert(tls *libc.TLS, pWC uintptr, p uintptr, wtFlags Tu16) (r int32) { var db, pOld, pTerm, v3 uintptr var idx, v1, v2 int32 _, _, _, _, _, _, _ = db, idx, pOld, pTerm, v1, v2, v3 if (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm >= (*TWhereClause)(unsafe.Pointer(pWC)).FnSlot { pOld = (*TWhereClause)(unsafe.Pointer(pWC)).Fa db = (*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpParse)).Fdb (*TWhereClause)(unsafe.Pointer(pWC)).Fa = _sqlite3WhereMalloc(tls, (*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo, uint64(uint32(48)*uint32((*TWhereClause)(unsafe.Pointer(pWC)).FnSlot)*uint32(2))) if (*TWhereClause)(unsafe.Pointer(pWC)).Fa == uintptr(0) { if int32(wtFlags)&int32(TERM_DYNAMIC) != 0 { _sqlite3ExprDelete(tls, db, p) } (*TWhereClause)(unsafe.Pointer(pWC)).Fa = pOld return 0 } libc.Xmemcpy(tls, (*TWhereClause)(unsafe.Pointer(pWC)).Fa, pOld, uint32(48)*uint32((*TWhereClause)(unsafe.Pointer(pWC)).FnTerm)) (*TWhereClause)(unsafe.Pointer(pWC)).FnSlot = (*TWhereClause)(unsafe.Pointer(pWC)).FnSlot * int32(2) } v3 = pWC + 12 v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ v1 = v2 idx = v1 pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(v1)*48 if int32(wtFlags)&int32(TERM_VIRTUAL) == 0 { (*TWhereClause)(unsafe.Pointer(pWC)).FnBase = (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm } if p != 0 && (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_Unlikely)) != uint32(0) { (*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb = int16(int32(_sqlite3LogEst(tls, uint64((*TExpr)(unsafe.Pointer(p)).FiTable))) - int32(270)) } else { (*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb = int16(1) } (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr = _sqlite3ExprSkipCollateAndLikely(tls, p) (*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags = wtFlags (*TWhereTerm)(unsafe.Pointer(pTerm)).FpWC = pWC (*TWhereTerm)(unsafe.Pointer(pTerm)).FiParent = -int32(1) libc.Xmemset(tls, pTerm+12, 0, libc.Uint32FromInt64(48)-uint32(libc.UintptrFromInt32(0)+12)) return idx } // C documentation // // /* // ** Return TRUE if the given operator is one of the operators that is // ** allowed for an indexable WHERE clause term. The allowed operators are // ** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL" // */ func _allowedOp(tls *libc.TLS, op int32) (r int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return libc.BoolInt32(op == int32(TK_IN) || op >= int32(TK_EQ) && op <= int32(TK_GE) || op == int32(TK_ISNULL) || op == int32(TK_IS)) } // C documentation // // /* // ** Commute a comparison operator. Expressions of the form "X op Y" // ** are converted into "Y op X". // */ func _exprCommute(tls *libc.TLS, pParse uintptr, pExpr uintptr) (r Tu16) { var t uintptr _ = t if int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft)).Fop) == int32(TK_VECTOR) || int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpRight)).Fop) == int32(TK_VECTOR) || _sqlite3BinaryCompareCollSeq(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) != _sqlite3BinaryCompareCollSeq(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) { *(*Tu32)(unsafe.Pointer(pExpr + 4)) ^= uint32(EP_Commuted) } t = (*TExpr)(unsafe.Pointer(pExpr)).FpRight (*TExpr)(unsafe.Pointer(pExpr)).FpRight = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft (*TExpr)(unsafe.Pointer(pExpr)).FpLeft = t if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) >= int32(TK_GT) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) - int32(TK_GT) ^ int32(2) + int32(TK_GT)) } return uint16(0) } // C documentation // // /* // ** Translate from TK_xx operator to WO_xx bitmask. // */ func _operatorMask(tls *libc.TLS, op int32) (r Tu16) { var c Tu16 _ = c _ = libc.Int32FromInt32(0) if op == int32(TK_IN) { c = uint16(WO_IN) } else { if op == int32(TK_ISNULL) { c = uint16(WO_ISNULL) } else { if op == int32(TK_IS) { c = uint16(WO_IS) } else { _ = libc.Int32FromInt32(0) c = uint16(libc.Int32FromInt32(WO_EQ) << (op - libc.Int32FromInt32(TK_EQ))) } } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return c } // C documentation // // /* // ** Check to see if the given expression is a LIKE or GLOB operator that // ** can be optimized using inequality constraints. Return TRUE if it is // ** so and false if not. // ** // ** In order for the operator to be optimizible, the RHS must be a string // ** literal that does not begin with a wildcard. The LHS must be a column // ** that may only be NULL, a string, or a BLOB, never a number. (This means // ** that virtual tables cannot participate in the LIKE optimization.) The // ** collating sequence for the column on the LHS must be appropriate for // ** the operator. // */ func _isLikeOrGlob(tls *libc.TLS, pParse uintptr, pExpr uintptr, ppPrefix uintptr, pisComplete uintptr, pnoCase uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var c, v1 Tu8 var cnt, iCol, iFrom, iTo, isNum, op, r1, rc, v3, v4 int32 var db, pLeft, pList, pPrefix, pReprepare, pRight, pVal, v, z, zNew uintptr var _ /* rDummy at bp+8 */ float64 var _ /* wc at bp+0 */ [4]Tu8 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, cnt, db, iCol, iFrom, iTo, isNum, op, pLeft, pList, pPrefix, pReprepare, pRight, pVal, r1, rc, v, z, zNew, v1, v3, v4 z = uintptr(0) /* Wildcard characters */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Database connection */ pVal = uintptr(0) /* Result code to return */ if !(_sqlite3IsLikeFunction(tls, db, pExpr, pnoCase, bp) != 0) { return 0 } _ = libc.Int32FromInt32(0) pList = *(*uintptr)(unsafe.Pointer(pExpr + 20)) pLeft = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + 1*20))).FpExpr pRight = _sqlite3ExprSkipCollate(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8))).FpExpr) op = int32((*TExpr)(unsafe.Pointer(pRight)).Fop) if op == int32(TK_VARIABLE) && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_EnableQPSG) == uint64(0) { pReprepare = (*TParse)(unsafe.Pointer(pParse)).FpReprepare iCol = int32((*TExpr)(unsafe.Pointer(pRight)).FiColumn) pVal = _sqlite3VdbeGetBoundValue(tls, pReprepare, iCol, uint8(SQLITE_AFF_BLOB)) if pVal != 0 && Xsqlite3_value_type(tls, pVal) == int32(SQLITE_TEXT) { z = Xsqlite3_value_text(tls, pVal) } _sqlite3VdbeSetVarmask(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, iCol) _ = libc.Int32FromInt32(0) } else { if op == int32(TK_STRING) { _ = libc.Int32FromInt32(0) z = *(*uintptr)(unsafe.Pointer(pRight + 8)) } } if z != 0 { /* Count the number of prefix characters prior to the first wildcard */ cnt = 0 for { v1 = *(*Tu8)(unsafe.Pointer(z + uintptr(cnt))) c = v1 if !(int32(v1) != 0 && int32(c) != int32((*(*[4]Tu8)(unsafe.Pointer(bp)))[0]) && int32(c) != int32((*(*[4]Tu8)(unsafe.Pointer(bp)))[int32(1)]) && int32(c) != int32((*(*[4]Tu8)(unsafe.Pointer(bp)))[int32(2)])) { break } cnt++ if int32(c) == int32((*(*[4]Tu8)(unsafe.Pointer(bp)))[int32(3)]) && int32(*(*Tu8)(unsafe.Pointer(z + uintptr(cnt)))) != 0 { cnt++ } } /* The optimization is possible only if (1) the pattern does not begin ** with a wildcard and if (2) the non-wildcard prefix does not end with ** an (illegal 0xff) character, or (3) the pattern does not consist of ** a single escape character. The second condition is necessary so ** that we can increment the prefix key to find an upper bound for the ** range search. The third is because the caller assumes that the pattern ** consists of at least one character after all escapes have been ** removed. */ if (cnt > int32(1) || cnt > 0 && int32(*(*Tu8)(unsafe.Pointer(z))) != int32((*(*[4]Tu8)(unsafe.Pointer(bp)))[int32(3)])) && int32(255) != int32(*(*Tu8)(unsafe.Pointer(z + uintptr(cnt-int32(1))))) { /* A "complete" match if the pattern ends with "*" or "%" */ *(*int32)(unsafe.Pointer(pisComplete)) = libc.BoolInt32(int32(c) == int32((*(*[4]Tu8)(unsafe.Pointer(bp)))[0]) && int32(*(*Tu8)(unsafe.Pointer(z + uintptr(cnt+int32(1))))) == 0) /* Get the pattern prefix. Remove all escapes from the prefix. */ pPrefix = _sqlite3Expr(tls, db, int32(TK_STRING), z) if pPrefix != 0 { _ = libc.Int32FromInt32(0) zNew = *(*uintptr)(unsafe.Pointer(pPrefix + 8)) *(*uint8)(unsafe.Pointer(zNew + uintptr(cnt))) = uint8(0) v3 = libc.Int32FromInt32(0) iTo = v3 iFrom = v3 for { if !(iFrom < cnt) { break } if int32(*(*uint8)(unsafe.Pointer(zNew + uintptr(iFrom)))) == int32((*(*[4]Tu8)(unsafe.Pointer(bp)))[int32(3)]) { iFrom++ } v4 = iTo iTo++ *(*uint8)(unsafe.Pointer(zNew + uintptr(v4))) = *(*uint8)(unsafe.Pointer(zNew + uintptr(iFrom))) goto _2 _2: ; iFrom++ } *(*uint8)(unsafe.Pointer(zNew + uintptr(iTo))) = uint8(0) _ = libc.Int32FromInt32(0) /* If the LHS is not an ordinary column with TEXT affinity, then the ** pattern prefix boundaries (both the start and end boundaries) must ** not look like a number. Otherwise the pattern might be treated as ** a number, which will invalidate the LIKE optimization. ** ** Getting this right has been a persistent source of bugs in the ** LIKE optimization. See, for example: ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a */ if int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) != int32(TK_COLUMN) || int32(_sqlite3ExprAffinity(tls, pLeft)) != int32(SQLITE_AFF_TEXT) || (*TExpr)(unsafe.Pointer(pLeft)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)|libc.Int32FromInt32(EP_Subrtn)) == uint32(0) && (*TExpr)(unsafe.Pointer(pLeft)).Fy.FpTab != 0 && int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pLeft)).Fy.FpTab)).FeTabType) == int32(TABTYP_VTAB) { isNum = _sqlite3AtoF(tls, zNew, bp+8, iTo, uint8(SQLITE_UTF8)) if isNum <= 0 { if iTo == int32(1) && int32(*(*uint8)(unsafe.Pointer(zNew))) == int32('-') { isNum = +libc.Int32FromInt32(1) } else { *(*uint8)(unsafe.Pointer(zNew + uintptr(iTo-int32(1))))++ isNum = _sqlite3AtoF(tls, zNew, bp+8, iTo, uint8(SQLITE_UTF8)) *(*uint8)(unsafe.Pointer(zNew + uintptr(iTo-int32(1))))-- } } if isNum > 0 { _sqlite3ExprDelete(tls, db, pPrefix) _sqlite3ValueFree(tls, pVal) return 0 } } } *(*uintptr)(unsafe.Pointer(ppPrefix)) = pPrefix /* If the RHS pattern is a bound parameter, make arrangements to ** reprepare the statement when that parameter is rebound */ if op == int32(TK_VARIABLE) { v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _sqlite3VdbeSetVarmask(tls, v, int32((*TExpr)(unsafe.Pointer(pRight)).FiColumn)) _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(pisComplete)) != 0 && *(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pRight + 8)) + 1)) != 0 { /* If the rhs of the LIKE expression is a variable, and the current ** value of the variable means there is no need to invoke the LIKE ** function, then no OP_Variable will be added to the program. ** This causes problems for the sqlite3_bind_parameter_name() ** API. To work around them, add a dummy OP_Variable here. */ r1 = _sqlite3GetTempReg(tls, pParse) _sqlite3ExprCodeTarget(tls, pParse, pRight, r1) _sqlite3VdbeChangeP3(tls, v, _sqlite3VdbeCurrentAddr(tls, v)-int32(1), 0) _sqlite3ReleaseTempReg(tls, pParse, r1) } } } else { z = uintptr(0) } } rc = libc.BoolInt32(z != uintptr(0)) _sqlite3ValueFree(tls, pVal) return rc } // C documentation // // /* // ** Check to see if the pExpr expression is a form that needs to be passed // ** to the xBestIndex method of virtual tables. Forms of interest include: // ** // ** Expression Virtual Table Operator // ** ----------------------- --------------------------------- // ** 1. column MATCH expr SQLITE_INDEX_CONSTRAINT_MATCH // ** 2. column GLOB expr SQLITE_INDEX_CONSTRAINT_GLOB // ** 3. column LIKE expr SQLITE_INDEX_CONSTRAINT_LIKE // ** 4. column REGEXP expr SQLITE_INDEX_CONSTRAINT_REGEXP // ** 5. column != expr SQLITE_INDEX_CONSTRAINT_NE // ** 6. expr != column SQLITE_INDEX_CONSTRAINT_NE // ** 7. column IS NOT expr SQLITE_INDEX_CONSTRAINT_ISNOT // ** 8. expr IS NOT column SQLITE_INDEX_CONSTRAINT_ISNOT // ** 9. column IS NOT NULL SQLITE_INDEX_CONSTRAINT_ISNOTNULL // ** // ** In every case, "column" must be a column of a virtual table. If there // ** is a match, set *ppLeft to the "column" expression, set *ppRight to the // ** "expr" expression (even though in forms (6) and (8) the column is on the // ** right and the expression is on the left). Also set *peOp2 to the // ** appropriate virtual table operator. The return value is 1 or 2 if there // ** is a match. The usual return is 1, but if the RHS is also a column // ** of virtual table in forms (5) or (7) then return 2. // ** // ** If the expression matches none of the patterns above, return 0. // */ func _isAuxiliaryVtabOperator(tls *libc.TLS, db uintptr, pExpr uintptr, peOp2 uintptr, ppLeft uintptr, ppRight uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, res int32 var pCol, pLeft, pList, pMod, pRight, pVtab, t uintptr var _ /* pNotUsed at bp+4 */ uintptr var _ /* xNotUsed at bp+0 */ uintptr _, _, _, _, _, _, _, _, _ = i, pCol, pLeft, pList, pMod, pRight, pVtab, res, t if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_FUNCTION) { _ = libc.Int32FromInt32(0) pList = *(*uintptr)(unsafe.Pointer(pExpr + 20)) if pList == uintptr(0) || (*TExprList)(unsafe.Pointer(pList)).FnExpr != int32(2) { return 0 } /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a ** virtual table on their second argument, which is the same as ** the left-hand side operand in their in-fix form. ** ** vtab_column MATCH expression ** MATCH(expression,vtab_column) */ pCol = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + 1*20))).FpExpr _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pCol)).Fop) == int32(TK_COLUMN) && int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pCol)).Fy.FpTab)).FeTabType) == int32(TABTYP_VTAB) { i = 0 for { if !(i < int32(libc.Uint32FromInt64(32)/libc.Uint32FromInt64(8))) { break } _ = libc.Int32FromInt32(0) if _sqlite3StrICmp(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), _aOp[i].FzOp) == 0 { *(*uint8)(unsafe.Pointer(peOp2)) = _aOp[i].FeOp2 *(*uintptr)(unsafe.Pointer(ppRight)) = (*(*TExprList_item)(unsafe.Pointer(pList + 8))).FpExpr *(*uintptr)(unsafe.Pointer(ppLeft)) = pCol return int32(1) } goto _1 _1: ; i++ } } /* We can also match against the first column of overloaded ** functions where xFindFunction returns a value of at least ** SQLITE_INDEX_CONSTRAINT_FUNCTION. ** ** OVERLOADED(vtab_column,expression) ** ** Historically, xFindFunction expected to see lower-case function ** names. But for this use case, xFindFunction is expected to deal ** with function names in an arbitrary case. */ pCol = (*(*TExprList_item)(unsafe.Pointer(pList + 8))).FpExpr _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pCol)).Fop) == int32(TK_COLUMN) && int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pCol)).Fy.FpTab)).FeTabType) == int32(TABTYP_VTAB) { pVtab = (*TVTable)(unsafe.Pointer(_sqlite3GetVTable(tls, db, (*TExpr)(unsafe.Pointer(pCol)).Fy.FpTab))).FpVtab _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pMod = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FpModule if (*Tsqlite3_module)(unsafe.Pointer(pMod)).FxFindFunction != uintptr(0) { i = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pMod)).FxFindFunction})))(tls, pVtab, int32(2), *(*uintptr)(unsafe.Pointer(pExpr + 8)), bp, bp+4) if i >= int32(SQLITE_INDEX_CONSTRAINT_FUNCTION) { *(*uint8)(unsafe.Pointer(peOp2)) = uint8(i) *(*uintptr)(unsafe.Pointer(ppRight)) = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + 1*20))).FpExpr *(*uintptr)(unsafe.Pointer(ppLeft)) = pCol return int32(1) } } } } else { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_NE) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_ISNOT) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_NOTNULL) { res = 0 pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft pRight = (*TExpr)(unsafe.Pointer(pExpr)).FpRight _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) == int32(TK_COLUMN) && int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pLeft)).Fy.FpTab)).FeTabType) == int32(TABTYP_VTAB) { res++ } _ = libc.Int32FromInt32(0) if pRight != 0 && (int32((*TExpr)(unsafe.Pointer(pRight)).Fop) == int32(TK_COLUMN) && int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pRight)).Fy.FpTab)).FeTabType) == int32(TABTYP_VTAB)) { res++ t = pLeft pLeft = pRight pRight = t } *(*uintptr)(unsafe.Pointer(ppLeft)) = pLeft *(*uintptr)(unsafe.Pointer(ppRight)) = pRight if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_NE) { *(*uint8)(unsafe.Pointer(peOp2)) = uint8(SQLITE_INDEX_CONSTRAINT_NE) } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_ISNOT) { *(*uint8)(unsafe.Pointer(peOp2)) = uint8(SQLITE_INDEX_CONSTRAINT_ISNOT) } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_NOTNULL) { *(*uint8)(unsafe.Pointer(peOp2)) = uint8(SQLITE_INDEX_CONSTRAINT_ISNOTNULL) } return res } } return 0 } var _aOp = [4]struct { FzOp uintptr FeOp2 uint8 }{ 0: { FzOp: __ccgo_ts + 16543, FeOp2: uint8(SQLITE_INDEX_CONSTRAINT_MATCH), }, 1: { FzOp: __ccgo_ts + 15874, FeOp2: uint8(SQLITE_INDEX_CONSTRAINT_GLOB), }, 2: { FzOp: __ccgo_ts + 15353, FeOp2: uint8(SQLITE_INDEX_CONSTRAINT_LIKE), }, 3: { FzOp: __ccgo_ts + 22303, FeOp2: uint8(SQLITE_INDEX_CONSTRAINT_REGEXP), }, } // C documentation // // /* // ** If the pBase expression originated in the ON or USING clause of // ** a join, then transfer the appropriate markings over to derived. // */ func _transferJoinMarkings(tls *libc.TLS, pDerived uintptr, pBase uintptr) { if pDerived != 0 && (*TExpr)(unsafe.Pointer(pBase)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != uint32(0) { *(*Tu32)(unsafe.Pointer(pDerived + 4)) |= (*TExpr)(unsafe.Pointer(pBase)).Fflags & uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) *(*int32)(unsafe.Pointer(pDerived + 36)) = *(*int32)(unsafe.Pointer(pBase + 36)) } } // C documentation // // /* // ** Mark term iChild as being a child of term iParent // */ func _markTermAsChild(tls *libc.TLS, pWC uintptr, iChild int32, iParent int32) { (*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(iChild)*48))).FiParent = iParent (*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(iChild)*48))).FtruthProb = (*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(iParent)*48))).FtruthProb (*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(iParent)*48))).FnChild++ } // C documentation // // /* // ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not // ** a conjunction, then return just pTerm when N==0. If N is exceeds // ** the number of available subterms, return NULL. // */ func _whereNthSubterm(tls *libc.TLS, pTerm uintptr, N int32) (r uintptr) { var v1 uintptr _ = v1 if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator) != int32(WO_AND) { if N == 0 { v1 = pTerm } else { v1 = uintptr(0) } return v1 } if N < (*TWhereAndInfo)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pTerm)).Fu)))).Fwc.FnTerm { return (*TWhereAndInfo)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pTerm)).Fu)))).Fwc.Fa + uintptr(N)*48 } return uintptr(0) } // C documentation // // /* // ** Subterms pOne and pTwo are contained within WHERE clause pWC. The // ** two subterms are in disjunction - they are OR-ed together. // ** // ** If these two terms are both of the form: "A op B" with the same // ** A and B values but different operators and if the operators are // ** compatible (if one is = and the other is <, for example) then // ** add a new virtual AND term to pWC that is the combination of the // ** two. // ** // ** Some examples: // ** // ** x x<=y // ** x=y OR x=y --> x=y // ** x<=y OR x x<=y // ** // ** The following is NOT generated: // ** // ** xy --> x!=y // */ func _whereCombineDisjuncts(tls *libc.TLS, pSrc uintptr, pWC uintptr, pOne uintptr, pTwo uintptr) { var db, pNew uintptr var eOp Tu16 var idxNew, op int32 _, _, _, _, _ = db, eOp, idxNew, op, pNew eOp = uint16(int32((*TWhereTerm)(unsafe.Pointer(pOne)).FeOperator) | int32((*TWhereTerm)(unsafe.Pointer(pTwo)).FeOperator)) /* Index in pWC of the next virtual term */ if (int32((*TWhereTerm)(unsafe.Pointer(pOne)).FwtFlags)|int32((*TWhereTerm)(unsafe.Pointer(pTwo)).FwtFlags))&int32(TERM_VNULL) != 0 { return } if int32((*TWhereTerm)(unsafe.Pointer(pOne)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ))) == 0 { return } if int32((*TWhereTerm)(unsafe.Pointer(pTwo)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ))) == 0 { return } if int32(eOp)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ))) != int32(eOp) && int32(eOp)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ))) != int32(eOp) { return } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if _sqlite3ExprCompare(tls, uintptr(0), (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOne)).FpExpr)).FpLeft, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTwo)).FpExpr)).FpLeft, -int32(1)) != 0 { return } if _sqlite3ExprCompare(tls, uintptr(0), (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOne)).FpExpr)).FpRight, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTwo)).FpExpr)).FpRight, -int32(1)) != 0 { return } /* If we reach this point, it means the two subterms can be combined */ if int32(eOp)&(int32(eOp)-int32(1)) != 0 { if int32(eOp)&(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ))) != 0 { eOp = uint16(libc.Int32FromInt32(WO_EQ) << (libc.Int32FromInt32(TK_LE) - libc.Int32FromInt32(TK_EQ))) } else { _ = libc.Int32FromInt32(0) eOp = uint16(libc.Int32FromInt32(WO_EQ) << (libc.Int32FromInt32(TK_GE) - libc.Int32FromInt32(TK_EQ))) } } db = (*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpParse)).Fdb pNew = _sqlite3ExprDup(tls, db, (*TWhereTerm)(unsafe.Pointer(pOne)).FpExpr, 0) if pNew == uintptr(0) { return } op = int32(TK_EQ) for { if !(int32(eOp) != int32(WO_EQ)<<(op-int32(TK_EQ))) { break } _ = libc.Int32FromInt32(0) goto _1 _1: ; op++ } (*TExpr)(unsafe.Pointer(pNew)).Fop = uint8(op) idxNew = _whereClauseInsert(tls, pWC, pNew, uint16(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_DYNAMIC))) _exprAnalyze(tls, pSrc, pWC, idxNew) } // C documentation // // /* // ** Analyze a term that consists of two or more OR-connected // ** subterms. So in: // ** // ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) // ** ^^^^^^^^^^^^^^^^^^^^ // ** // ** This routine analyzes terms such as the middle term in the above example. // ** A WhereOrTerm object is computed and attached to the term under // ** analysis, regardless of the outcome of the analysis. Hence: // ** // ** WhereTerm.wtFlags |= TERM_ORINFO // ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object // ** // ** The term being analyzed must have two or more of OR-connected subterms. // ** A single subterm might be a set of AND-connected sub-subterms. // ** Examples of terms under analysis: // ** // ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 // ** (B) x=expr1 OR expr2=x OR x=expr3 // ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) // ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') // ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6) // ** (F) x>A OR (x=A AND y>=B) // ** // ** CASE 1: // ** // ** If all subterms are of the form T.C=expr for some single column of C and // ** a single table T (as shown in example B above) then create a new virtual // ** term that is an equivalent IN expression. In other words, if the term // ** being analyzed is: // ** // ** x = expr1 OR expr2 = x OR x = expr3 // ** // ** then create a new virtual term like this: // ** // ** x IN (expr1,expr2,expr3) // ** // ** CASE 2: // ** // ** If there are exactly two disjuncts and one side has x>A and the other side // ** has x=A (for the same x and A) then add a new virtual conjunct term to the // ** WHERE clause of the form "x>=A". Example: // ** // ** x>A OR (x=A AND y>B) adds: x>=A // ** // ** The added conjunct can sometimes be helpful in query planning. // ** // ** CASE 3: // ** // ** If all subterms are indexable by a single table T, then set // ** // ** WhereTerm.eOperator = WO_OR // ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T // ** // ** A subterm is "indexable" if it is of the form // ** "T.C " where C is any column of table T and // ** is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". // ** A subterm is also indexable if it is an AND of two or more // ** subsubterms at least one of which is indexable. Indexable AND // ** subterms have their eOperator set to WO_AND and they have // ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. // ** // ** From another point of view, "indexable" means that the subterm could // ** potentially be used with an index if an appropriate index exists. // ** This analysis does not consider whether or not the index exists; that // ** is decided elsewhere. This analysis only looks at whether subterms // ** appropriate for indexing exist. // ** // ** All examples A through E above satisfy case 3. But if a term // ** also satisfies case 1 (such as B) we know that the optimizer will // ** always prefer case 1, so in that case we pretend that case 3 is not // ** satisfied. // ** // ** It might be the case that multiple tables are indexable. For example, // ** (E) above is indexable on tables P, Q, and R. // ** // ** Terms that satisfy case 3 are candidates for lookup by using // ** separate indices to find rowids for each subterm and composing // ** the union of all rowids using a RowSet object. This is similar // ** to "bitmap indices" in other database engines. // ** // ** OTHERWISE: // ** // ** If none of cases 1, 2, or 3 apply, then leave the eOperator set to // ** zero. This term is not useful for search. // */ func _exprAnalyzeOrTerm(tls *libc.TLS, pSrc uintptr, pWC uintptr, idxTerm int32) { var affLeft, affRight, i, iColumn, iCursor, iOne, iTwo, idxNew, j, j1, okToChngToIN, v7, v9 int32 var b, b1, chngToIN, indexable TBitmask var db, pAndInfo, pAndTerm, pAndWC, pDup, pExpr, pLeft, pLeft1, pList, pNew, pOne, pOrInfo, pOrTerm, pOrWc, pOther, pParse, pTerm, pTwo, pWInfo, v1, v6, v8, p12, p14, p15, p2, p4 uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = affLeft, affRight, b, b1, chngToIN, db, i, iColumn, iCursor, iOne, iTwo, idxNew, indexable, j, j1, okToChngToIN, pAndInfo, pAndTerm, pAndWC, pDup, pExpr, pLeft, pLeft1, pList, pNew, pOne, pOrInfo, pOrTerm, pOrWc, pOther, pParse, pTerm, pTwo, pWInfo, v1, v6, v7, v8, v9, p12, p14, p15, p2, p4 pWInfo = (*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo /* WHERE clause processing context */ pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse /* Parser context */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Database connection */ pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48 /* The term to be analyzed */ pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr /* Tables that are indexable, satisfying case 2 */ /* ** Break the OR clause into its separate subterms. The subterms are ** stored in a WhereClause structure containing within the WhereOrInfo ** object that is attached to the original OR clause term. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) v1 = _sqlite3DbMallocZero(tls, db, uint64(424)) pOrInfo = v1 *(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pTerm)).Fu)) = v1 if pOrInfo == uintptr(0) { return } p2 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(TERM_ORINFO)) pOrWc = pOrInfo libc.Xmemset(tls, pOrWc+32, 0, uint32(384)) _sqlite3WhereClauseInit(tls, pOrWc, pWInfo) _sqlite3WhereSplit(tls, pOrWc, pExpr, uint8(TK_OR)) _sqlite3WhereExprAnalyze(tls, pSrc, pOrWc) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return } _ = libc.Int32FromInt32(0) /* ** Compute the set of tables that might satisfy cases 1 or 3. */ indexable = ^libc.Uint64FromInt32(0) chngToIN = ^libc.Uint64FromInt32(0) i = (*TWhereClause)(unsafe.Pointer(pOrWc)).FnTerm - int32(1) pOrTerm = (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa for { if !(i >= 0 && indexable != 0) { break } if int32((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FeOperator)&int32(WO_SINGLE) == 0 { _ = libc.Int32FromInt32(0) chngToIN = uint64(0) pAndInfo = _sqlite3DbMallocRawNN(tls, db, uint64(416)) if pAndInfo != 0 { b = uint64(0) *(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pOrTerm)).Fu)) = pAndInfo p4 = pOrTerm + 10 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | libc.Int32FromInt32(TERM_ANDINFO)) (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FeOperator = uint16(WO_AND) (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor = -int32(1) pAndWC = pAndInfo libc.Xmemset(tls, pAndWC+32, 0, uint32(384)) _sqlite3WhereClauseInit(tls, pAndWC, (*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo) _sqlite3WhereSplit(tls, pAndWC, (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr, uint8(TK_AND)) _sqlite3WhereExprAnalyze(tls, pSrc, pAndWC) (*TWhereClause)(unsafe.Pointer(pAndWC)).FpOuter = pWC if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { j = 0 pAndTerm = (*TWhereClause)(unsafe.Pointer(pAndWC)).Fa for { if !(j < (*TWhereClause)(unsafe.Pointer(pAndWC)).FnTerm) { break } _ = libc.Int32FromInt32(0) if _allowedOp(tls, int32((*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pAndTerm)).FpExpr)).Fop)) != 0 || int32((*TWhereTerm)(unsafe.Pointer(pAndTerm)).FeOperator) == int32(WO_AUX) { b |= _sqlite3WhereGetMask(tls, pWInfo+496, (*TWhereTerm)(unsafe.Pointer(pAndTerm)).FleftCursor) } goto _5 _5: ; j++ pAndTerm += 48 } } indexable &= b } } else { if int32((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FwtFlags)&int32(TERM_COPIED) != 0 { /* Skip this term for now. We revisit it when we process the ** corresponding TERM_VIRTUAL term */ } else { b1 = _sqlite3WhereGetMask(tls, pWInfo+496, (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor) if int32((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FwtFlags)&int32(TERM_VIRTUAL) != 0 { pOther = (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa + uintptr((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FiParent)*48 b1 |= _sqlite3WhereGetMask(tls, pWInfo+496, (*TWhereTerm)(unsafe.Pointer(pOther)).FleftCursor) } indexable &= b1 if int32((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FeOperator)&int32(WO_EQ) == 0 { chngToIN = uint64(0) } else { chngToIN &= b1 } } } goto _3 _3: ; i-- pOrTerm += 48 } /* ** Record the set of tables that satisfy case 3. The set might be ** empty. */ (*TWhereOrInfo)(unsafe.Pointer(pOrInfo)).Findexable = indexable (*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator = uint16(WO_OR) (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor = -int32(1) if indexable != 0 { (*TWhereClause)(unsafe.Pointer(pWC)).FhasOr = uint8(1) } /* For a two-way OR, attempt to implementation case 2. */ if indexable != 0 && (*TWhereClause)(unsafe.Pointer(pOrWc)).FnTerm == int32(2) { iOne = 0 for { v7 = iOne iOne++ v6 = _whereNthSubterm(tls, (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa, v7) pOne = v6 if !(v6 != uintptr(0)) { break } iTwo = 0 for { v9 = iTwo iTwo++ v8 = _whereNthSubterm(tls, (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa+1*48, v9) pTwo = v8 if !(v8 != uintptr(0)) { break } _whereCombineDisjuncts(tls, pSrc, pWC, pOne, pTwo) } } } /* ** chngToIN holds a set of tables that *might* satisfy case 1. But ** we have to do some additional checking to see if case 1 really ** is satisfied. ** ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means ** that there is no possibility of transforming the OR clause into an ** IN operator because one or more terms in the OR clause contain ** something other than == on a column in the single table. The 1-bit ** case means that every term of the OR clause is of the form ** "table.column=expr" for some single table. The one bit that is set ** will correspond to the common table. We still need to check to make ** sure the same column is used on all terms. The 2-bit case is when ** the all terms are of the form "table1.column=table2.column". It ** might be possible to form an IN operator with either table1.column ** or table2.column as the LHS if either is common to every term of ** the OR clause. ** ** Note that terms of the form "table.column1=table.column2" (the ** same table on both sizes of the ==) cannot be optimized. */ if chngToIN != 0 { okToChngToIN = 0 /* True if the conversion to IN is valid */ iColumn = -int32(1) /* Column index on lhs of IN operator */ iCursor = -int32(1) /* Table cursor common to all terms */ j1 = 0 /* Loop counter */ /* Search for a table and column that appears on one side or the ** other of the == operator in every subterm. That table and column ** will be recorded in iCursor and iColumn. There might not be any ** such table and column. Set okToChngToIN if an appropriate table ** and column is found but leave okToChngToIN false if not found. */ j1 = 0 for { if !(j1 < int32(2) && !(okToChngToIN != 0)) { break } pLeft = uintptr(0) pOrTerm = (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa i = (*TWhereClause)(unsafe.Pointer(pOrWc)).FnTerm - int32(1) for { if !(i >= 0) { break } _ = libc.Int32FromInt32(0) p12 = pOrTerm + 10 *(*Tu16)(unsafe.Pointer(p12)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p12))) & ^libc.Int32FromInt32(TERM_OK)) if (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor == iCursor { /* This is the 2-bit case and we are on the second iteration and ** current term is from the first iteration. So skip this term. */ _ = libc.Int32FromInt32(0) goto _11 } if chngToIN&_sqlite3WhereGetMask(tls, pWInfo+496, (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor) == uint64(0) { /* This term must be of the form t1.a==t2.b where t2 is in the ** chngToIN set but t1 is not. This term will be either preceded ** or followed by an inverted copy (t2.b==t1.a). Skip this term ** and use its inversion. */ _ = libc.Int32FromInt32(0) goto _11 } _ = libc.Int32FromInt32(0) iColumn = (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pOrTerm + 24))).FleftColumn iCursor = (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor pLeft = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr)).FpLeft break goto _11 _11: ; i-- pOrTerm += 48 } if i < 0 { /* No candidate table+column was found. This can only occur ** on the second iteration */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) break } /* We have found a candidate table and column. Check to see if that ** table and column is common to every term in the OR clause */ okToChngToIN = int32(1) for { if !(i >= 0 && okToChngToIN != 0) { break } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor != iCursor { p14 = pOrTerm + 10 *(*Tu16)(unsafe.Pointer(p14)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p14))) & ^libc.Int32FromInt32(TERM_OK)) } else { if (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pOrTerm + 24))).FleftColumn != iColumn || iColumn == -int32(2) && _sqlite3ExprCompare(tls, pParse, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr)).FpLeft, pLeft, -int32(1)) != 0 { okToChngToIN = 0 } else { /* If the right-hand side is also a column, then the affinities ** of both right and left sides must be such that no type ** conversions are required on the right. (Ticket #2249) */ affRight = int32(_sqlite3ExprAffinity(tls, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr)).FpRight)) affLeft = int32(_sqlite3ExprAffinity(tls, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr)).FpLeft)) if affRight != 0 && affRight != affLeft { okToChngToIN = 0 } else { p15 = pOrTerm + 10 *(*Tu16)(unsafe.Pointer(p15)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p15))) | libc.Int32FromInt32(TERM_OK)) } } } goto _13 _13: ; i-- pOrTerm += 48 } goto _10 _10: ; j1++ } /* At this point, okToChngToIN is true if original pTerm satisfies ** case 1. In that case, construct a new virtual term that is ** pTerm converted into an IN operator. */ if okToChngToIN != 0 { /* A transient duplicate expression */ pList = uintptr(0) /* The RHS of the IN operator */ pLeft1 = uintptr(0) /* The complete IN operator */ i = (*TWhereClause)(unsafe.Pointer(pOrWc)).FnTerm - int32(1) pOrTerm = (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa for { if !(i >= 0) { break } if int32((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FwtFlags)&int32(TERM_OK) == 0 { goto _16 } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pDup = _sqlite3ExprDup(tls, db, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr)).FpRight, 0) pList = _sqlite3ExprListAppend(tls, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse, pList, pDup) pLeft1 = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr)).FpLeft goto _16 _16: ; i-- pOrTerm += 48 } _ = libc.Int32FromInt32(0) pDup = _sqlite3ExprDup(tls, db, pLeft1, 0) pNew = _sqlite3PExpr(tls, pParse, int32(TK_IN), pDup, uintptr(0)) if pNew != 0 { _transferJoinMarkings(tls, pNew, pExpr) _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pNew + 20)) = pList idxNew = _whereClauseInsert(tls, pWC, pNew, uint16(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_DYNAMIC))) _exprAnalyze(tls, pSrc, pWC, idxNew) /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */ _markTermAsChild(tls, pWC, idxNew, idxTerm) } else { _sqlite3ExprListDelete(tls, db, pList) } } } } // C documentation // // /* // ** We already know that pExpr is a binary operator where both operands are // ** column references. This routine checks to see if pExpr is an equivalence // ** relation: // ** 1. The SQLITE_Transitive optimization must be enabled // ** 2. Must be either an == or an IS operator // ** 3. Not originating in the ON clause of an OUTER JOIN // ** 4. The affinities of A and B must be compatible // ** 5a. Both operands use the same collating sequence OR // ** 5b. The overall collating sequence is BINARY // ** If this routine returns TRUE, that means that the RHS can be substituted // ** for the LHS anyplace else in the WHERE clause where the LHS column occurs. // ** This is an optimization. No harm comes from returning 0. But if 1 is // ** returned when it should not be, then incorrect answers might result. // */ func _termIsEquivalence(tls *libc.TLS, pParse uintptr, pExpr uintptr) (r int32) { var aff1, aff2 uint8 var pColl uintptr _, _, _ = aff1, aff2, pColl if !((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Transitive)) == libc.Uint32FromInt32(0)) { return 0 } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_EQ) && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_IS) { return 0 } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) { return 0 } aff1 = _sqlite3ExprAffinity(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) aff2 = _sqlite3ExprAffinity(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) if int32(aff1) != int32(aff2) && (!(int32(aff1) >= libc.Int32FromInt32(SQLITE_AFF_NUMERIC)) || !(int32(aff2) >= libc.Int32FromInt32(SQLITE_AFF_NUMERIC))) { return 0 } pColl = _sqlite3ExprCompareCollSeq(tls, pParse, pExpr) if _sqlite3IsBinary(tls, pColl) != 0 { return int32(1) } return _sqlite3ExprCollSeqMatch(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) } // C documentation // // /* // ** Recursively walk the expressions of a SELECT statement and generate // ** a bitmask indicating which tables are used in that expression // ** tree. // */ func _exprSelectUsage(tls *libc.TLS, pMaskSet uintptr, pS uintptr) (r TBitmask) { var i int32 var mask TBitmask var pSrc uintptr _, _, _ = i, mask, pSrc mask = uint64(0) for pS != 0 { pSrc = (*TSelect)(unsafe.Pointer(pS)).FpSrc mask |= _sqlite3WhereExprListUsage(tls, pMaskSet, (*TSelect)(unsafe.Pointer(pS)).FpEList) mask |= _sqlite3WhereExprListUsage(tls, pMaskSet, (*TSelect)(unsafe.Pointer(pS)).FpGroupBy) mask |= _sqlite3WhereExprListUsage(tls, pMaskSet, (*TSelect)(unsafe.Pointer(pS)).FpOrderBy) mask |= _sqlite3WhereExprUsage(tls, pMaskSet, (*TSelect)(unsafe.Pointer(pS)).FpWhere) mask |= _sqlite3WhereExprUsage(tls, pMaskSet, (*TSelect)(unsafe.Pointer(pS)).FpHaving) if pSrc != uintptr(0) { i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) { break } mask |= _exprSelectUsage(tls, pMaskSet, (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72))).FpSelect) if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72 + 36 + 4))&0x400>>10)) == 0 { mask |= _sqlite3WhereExprUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72 + 48))) } if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72 + 36 + 4))&0x4>>2)) != 0 { mask |= _sqlite3WhereExprListUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(pSrc + 8 + uintptr(i)*72 + 64))) } goto _1 _1: ; i++ } } pS = (*TSelect)(unsafe.Pointer(pS)).FpPrior } return mask } // C documentation // // /* // ** Expression pExpr is one operand of a comparison operator that might // ** be useful for indexing. This routine checks to see if pExpr appears // ** in any index. Return TRUE (1) if pExpr is an indexed term and return // ** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor // ** number of the table that is indexed and aiCurCol[1] to the column number // ** of the column that is indexed, or XN_EXPR (-2) if an expression is being // ** indexed. // ** // ** If pExpr is a TK_COLUMN column reference, then this routine always returns // ** true even if that particular column is not indexed, because the column // ** might be added to an automatic index later. // */ func _exprMightBeIndexed2(tls *libc.TLS, pFrom uintptr, aiCurCol uintptr, pExpr uintptr, j int32) (r int32) { var i, iCur, v1 int32 var pIdx uintptr _, _, _, _ = i, iCur, pIdx, v1 for { iCur = (*(*TSrcItem)(unsafe.Pointer(pFrom + 8 + uintptr(j)*72))).FiCursor pIdx = (*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pFrom + 8 + uintptr(j)*72))).FpTab)).FpIndex for { if !(pIdx != 0) { break } if (*TIndex)(unsafe.Pointer(pIdx)).FaColExpr == uintptr(0) { goto _3 } i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) { break } if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))) != -int32(2) { goto _4 } _ = libc.Int32FromInt32(0) if _sqlite3ExprCompareSkip(tls, pExpr, (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(i)*20))).FpExpr, iCur) == 0 && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_STRING) { *(*int32)(unsafe.Pointer(aiCurCol)) = iCur *(*int32)(unsafe.Pointer(aiCurCol + 1*4)) = -int32(2) return int32(1) } goto _4 _4: ; i++ } goto _3 _3: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } goto _2 _2: ; j++ v1 = j if !(v1 < (*TSrcList)(unsafe.Pointer(pFrom)).FnSrc) { break } } return 0 } func _exprMightBeIndexed(tls *libc.TLS, pFrom uintptr, aiCurCol uintptr, pExpr uintptr, op int32) (r int32) { var i int32 var pIdx uintptr _, _ = i, pIdx /* If this expression is a vector to the left or right of a ** inequality constraint (>, <, >= or <=), perform the processing ** on the first element of the vector. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_VECTOR) && (op >= int32(TK_GT) && op <= int32(TK_GE)) { _ = libc.Int32FromInt32(0) pExpr = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8))).FpExpr } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) { *(*int32)(unsafe.Pointer(aiCurCol)) = (*TExpr)(unsafe.Pointer(pExpr)).FiTable *(*int32)(unsafe.Pointer(aiCurCol + 1*4)) = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) return int32(1) } i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(pFrom)).FnSrc) { break } pIdx = (*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pFrom + 8 + uintptr(i)*72))).FpTab)).FpIndex for { if !(pIdx != 0) { break } if (*TIndex)(unsafe.Pointer(pIdx)).FaColExpr != 0 { return _exprMightBeIndexed2(tls, pFrom, aiCurCol, pExpr, i) } goto _2 _2: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } goto _1 _1: ; i++ } return 0 } // C documentation // // /* // ** The input to this routine is an WhereTerm structure with only the // ** "pExpr" field filled in. The job of this routine is to analyze the // ** subexpression and populate all the other fields of the WhereTerm // ** structure. // ** // ** If the expression is of the form " X" it gets commuted // ** to the standard form of "X ". // ** // ** If the expression is of the form "X Y" where both X and Y are // ** columns, then the original expression is unchanged and a new virtual // ** term of the form "Y X" is added to the WHERE clause and // ** analyzed separately. The original term is marked with TERM_COPIED // ** and the new term is marked with TERM_DYNAMIC (because it's pExpr // ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it // ** is a commuted copy of a prior term.) The original term has nChild=1 // ** and the copy has idxParent set to the index of the original term. // */ func _exprAnalyze(tls *libc.TLS, pSrc uintptr, pWC uintptr, idxTerm int32) { bp := tls.Alloc(32) defer tls.Free(32) var c, v12 uint8 var c1 Tu8 var db, pC, pDup, pExpr, pLeft, pLeft1, pLeft2, pLeft3, pList, pMaskSet, pNew, pNew1, pNewExpr, pNewExpr1, pNewExpr11, pNewExpr2, pNewExpr21, pNewTerm, pNewTerm1, pParse, pRight, pRight1, pStr2, pTerm, pWInfo, t, zCollSeqName, v13, p1, p10, p17, p20, p3, p4, p5, p6, p7, p9 uintptr var eExtraOp, opMask, wtFlags Tu16 var extraRight, prereqAll, prereqColumn, prereqExpr, prereqLeft, x TBitmask var i, i1, i2, i3, idxNew, idxNew1, idxNew11, idxNew2, idxNew21, idxNew3, idxNew4, idxNew5, nLeft, op, res, v14, v19, v2 int32 var v15 bool var _ /* aiCurCol at bp+16 */ [2]int32 var _ /* eOp2 at bp+12 */ uint8 var _ /* isComplete at bp+4 */ int32 var _ /* noCase at bp+8 */ int32 var _ /* pLeft at bp+28 */ uintptr var _ /* pRight at bp+24 */ uintptr var _ /* pStr1 at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, c1, db, eExtraOp, extraRight, i, i1, i2, i3, idxNew, idxNew1, idxNew11, idxNew2, idxNew21, idxNew3, idxNew4, idxNew5, nLeft, op, opMask, pC, pDup, pExpr, pLeft, pLeft1, pLeft2, pLeft3, pList, pMaskSet, pNew, pNew1, pNewExpr, pNewExpr1, pNewExpr11, pNewExpr2, pNewExpr21, pNewTerm, pNewTerm1, pParse, pRight, pRight1, pStr2, pTerm, pWInfo, prereqAll, prereqColumn, prereqExpr, prereqLeft, res, t, wtFlags, x, zCollSeqName, v12, v13, v14, v15, v19, v2, p1, p10, p17, p20, p3, p4, p5, p6, p7, p9 pWInfo = (*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo /* Prerequisites of pExpr */ extraRight = uint64(0) /* Extra dependencies on LEFT JOIN */ *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* RHS of LIKE/GLOB operator */ *(*int32)(unsafe.Pointer(bp + 4)) = 0 /* RHS of LIKE/GLOB ends with wildcard */ *(*int32)(unsafe.Pointer(bp + 8)) = 0 /* Top-level operator. pExpr->op */ pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse /* Parsing context */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Database connection */ *(*uint8)(unsafe.Pointer(bp + 12)) = uint8(0) /* Number of elements on left side vector */ if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return } _ = libc.Int32FromInt32(0) pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48 pMaskSet = pWInfo + 496 pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr _ = libc.Int32FromInt32(0) /* Because malloc() has not failed */ _ = libc.Int32FromInt32(0) (*TWhereMaskSet)(unsafe.Pointer(pMaskSet)).FbVarSelect = 0 prereqLeft = _sqlite3WhereExprUsage(tls, pMaskSet, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) if op == int32(TK_IN) { _ = libc.Int32FromInt32(0) if _sqlite3ExprCheckIN(tls, pParse, pExpr) != 0 { return } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) { (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight = _exprSelectUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(pExpr + 20))) } else { (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight = _sqlite3WhereExprListUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(pExpr + 20))) } prereqAll = prereqLeft | (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight } else { (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight = _sqlite3WhereExprUsage(tls, pMaskSet, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft == uintptr(0) || (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_xIsSelect)|libc.Int32FromInt32(EP_IfNullRow)) != uint32(0) || *(*uintptr)(unsafe.Pointer(pExpr + 20)) != uintptr(0) { prereqAll = _sqlite3WhereExprUsageNN(tls, pMaskSet, pExpr) } else { prereqAll = prereqLeft | (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight } } if (*TWhereMaskSet)(unsafe.Pointer(pMaskSet)).FbVarSelect != 0 { p1 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(TERM_VARSELECT)) } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != uint32(0) { x = _sqlite3WhereGetMask(tls, pMaskSet, *(*int32)(unsafe.Pointer(pExpr + 36))) if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) { prereqAll |= x extraRight = x - uint64(1) /* ON clause terms may not be used with an index ** on left table of a LEFT JOIN. Ticket #3015 */ if prereqAll>>libc.Int32FromInt32(1) >= x { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22310, 0) return } } else { if prereqAll>>libc.Int32FromInt32(1) >= x { /* The ON clause of an INNER JOIN references a table to its right. ** Most other SQL database engines raise an error. But SQLite versions ** 3.0 through 3.38 just put the ON clause constraint into the WHERE ** clause and carried on. Beginning with 3.39, raise an error only ** if there is a RIGHT or FULL JOIN in the query. This makes SQLite ** more like other systems, and also preserves legacy. */ if (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc > 0 && int32((*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22310, 0) return } *(*Tu32)(unsafe.Pointer(pExpr + 4)) &= uint32(^libc.Int32FromInt32(EP_InnerON)) } } } (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll = prereqAll (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor = -int32(1) (*TWhereTerm)(unsafe.Pointer(pTerm)).FiParent = -int32(1) (*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator = uint16(0) if _allowedOp(tls, op) != 0 { pLeft = _sqlite3ExprSkipCollate(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) pRight = _sqlite3ExprSkipCollate(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight&prereqLeft == uint64(0) { v2 = int32(WO_ALL) } else { v2 = int32(WO_EQUIV) } opMask = uint16(v2) if (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 24))).FiField > 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pLeft = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pLeft + 20)) + 8 + uintptr((*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 24))).FiField-int32(1))*20))).FpExpr } if _exprMightBeIndexed(tls, pSrc, bp+16, pLeft, op) != 0 { (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor = (*(*[2]int32)(unsafe.Pointer(bp + 16)))[0] _ = libc.Int32FromInt32(0) (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 24))).FleftColumn = (*(*[2]int32)(unsafe.Pointer(bp + 16)))[int32(1)] (*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator = uint16(int32(_operatorMask(tls, op)) & int32(opMask)) } if op == int32(TK_IS) { p3 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) | libc.Int32FromInt32(TERM_IS)) } if pRight != 0 && _exprMightBeIndexed(tls, pSrc, bp+16, pRight, op) != 0 && !((*TExpr)(unsafe.Pointer(pRight)).Fflags&uint32(libc.Int32FromInt32(EP_FixedCol)) != libc.Uint32FromInt32(0)) { eExtraOp = uint16(0) /* Extra bits for pNew->eOperator */ _ = libc.Int32FromInt32(0) if (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor >= 0 { pDup = _sqlite3ExprDup(tls, db, pExpr, 0) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _sqlite3ExprDelete(tls, db, pDup) return } idxNew = _whereClauseInsert(tls, pWC, pDup, uint16(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_DYNAMIC))) if idxNew == 0 { return } pNew = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxNew)*48 _markTermAsChild(tls, pWC, idxNew, idxTerm) if op == int32(TK_IS) { p4 = pNew + 10 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | libc.Int32FromInt32(TERM_IS)) } pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48 p5 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p5)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p5))) | libc.Int32FromInt32(TERM_COPIED)) if _termIsEquivalence(tls, pParse, pDup) != 0 { p6 = pTerm + 12 *(*Tu16)(unsafe.Pointer(p6)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p6))) | libc.Int32FromInt32(WO_EQUIV)) eExtraOp = uint16(WO_EQUIV) } } else { pDup = pExpr pNew = pTerm } p7 = pNew + 10 *(*Tu16)(unsafe.Pointer(p7)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p7))) | int32(_exprCommute(tls, pParse, pDup))) (*TWhereTerm)(unsafe.Pointer(pNew)).FleftCursor = (*(*[2]int32)(unsafe.Pointer(bp + 16)))[0] _ = libc.Int32FromInt32(0) (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pNew + 24))).FleftColumn = (*(*[2]int32)(unsafe.Pointer(bp + 16)))[int32(1)] (*TWhereTerm)(unsafe.Pointer(pNew)).FprereqRight = prereqLeft | extraRight (*TWhereTerm)(unsafe.Pointer(pNew)).FprereqAll = prereqAll (*TWhereTerm)(unsafe.Pointer(pNew)).FeOperator = uint16((int32(_operatorMask(tls, int32((*TExpr)(unsafe.Pointer(pDup)).Fop))) + int32(eExtraOp)) & int32(opMask)) } else { if op == int32(TK_ISNULL) && !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != libc.Uint32FromInt32(0)) && 0 == _sqlite3ExprCanBeNull(tls, pLeft) { _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_TRUEFALSE) /* See tag-20230504-1 */ *(*uintptr)(unsafe.Pointer(pExpr + 8)) = __ccgo_ts + 7891 *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_IsFalse)) (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll = uint64(0) (*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator = uint16(0) } } } else { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_BETWEEN) && int32((*TWhereClause)(unsafe.Pointer(pWC)).Fop) == int32(TK_AND) { _ = libc.Int32FromInt32(0) pList = *(*uintptr)(unsafe.Pointer(pExpr + 20)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) i = 0 for { if !(i < int32(2)) { break } pNewExpr = _sqlite3PExpr(tls, pParse, int32(_ops[i]), _sqlite3ExprDup(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, 0), _sqlite3ExprDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr, 0)) _transferJoinMarkings(tls, pNewExpr, pExpr) idxNew1 = _whereClauseInsert(tls, pWC, pNewExpr, uint16(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_DYNAMIC))) _exprAnalyze(tls, pSrc, pWC, idxNew1) pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48 _markTermAsChild(tls, pWC, idxNew1, idxTerm) goto _8 _8: ; i++ } } else { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_OR) { _ = libc.Int32FromInt32(0) _exprAnalyzeOrTerm(tls, pSrc, pWC, idxTerm) pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48 } else { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_NOTNULL) { if int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft)).Fop) == int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft)).FiColumn) >= 0 && !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != libc.Uint32FromInt32(0)) { pLeft1 = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft pNewExpr1 = _sqlite3PExpr(tls, pParse, int32(TK_GT), _sqlite3ExprDup(tls, db, pLeft1, 0), _sqlite3ExprAlloc(tls, db, int32(TK_NULL), uintptr(0), 0)) idxNew2 = _whereClauseInsert(tls, pWC, pNewExpr1, uint16(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_DYNAMIC)|libc.Int32FromInt32(TERM_VNULL))) if idxNew2 != 0 { pNewTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxNew2)*48 (*TWhereTerm)(unsafe.Pointer(pNewTerm)).FprereqRight = uint64(0) (*TWhereTerm)(unsafe.Pointer(pNewTerm)).FleftCursor = (*TExpr)(unsafe.Pointer(pLeft1)).FiTable (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pNewTerm + 24))).FleftColumn = int32((*TExpr)(unsafe.Pointer(pLeft1)).FiColumn) (*TWhereTerm)(unsafe.Pointer(pNewTerm)).FeOperator = uint16(libc.Int32FromInt32(WO_EQ) << (libc.Int32FromInt32(TK_GT) - libc.Int32FromInt32(TK_EQ))) _markTermAsChild(tls, pWC, idxNew2, idxTerm) pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48 p9 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p9)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p9))) | libc.Int32FromInt32(TERM_COPIED)) (*TWhereTerm)(unsafe.Pointer(pNewTerm)).FprereqAll = (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll } } } else { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_FUNCTION) && int32((*TWhereClause)(unsafe.Pointer(pWC)).Fop) == int32(TK_AND) && _isLikeOrGlob(tls, pParse, pExpr, bp, bp+4, bp+8) != 0 { /* Name of collating sequence */ wtFlags = uint16(libc.Int32FromInt32(TERM_LIKEOPT) | libc.Int32FromInt32(TERM_VIRTUAL) | libc.Int32FromInt32(TERM_DYNAMIC)) _ = libc.Int32FromInt32(0) pLeft2 = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8 + 1*20))).FpExpr pStr2 = _sqlite3ExprDup(tls, db, *(*uintptr)(unsafe.Pointer(bp)), 0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Convert the lower bound to upper-case and the upper bound to ** lower-case (upper-case is less than lower-case in ASCII) so that ** the range constraints also work for BLOBs */ if *(*int32)(unsafe.Pointer(bp + 8)) != 0 && !((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0) { p10 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p10)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p10))) | libc.Int32FromInt32(TERM_LIKE)) i1 = 0 for { v12 = *(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)) + 8)) + uintptr(i1))) c = v12 if !(int32(v12) != 0) { break } *(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)) + 8)) + uintptr(i1))) = uint8(int32(c) & ^(int32(_sqlite3CtypeMap[c]) & libc.Int32FromInt32(0x20))) *(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pStr2 + 8)) + uintptr(i1))) = _sqlite3UpperToLower[c] goto _11 _11: ; i1++ } } if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { /* Last character before the first wildcard */ pC = *(*uintptr)(unsafe.Pointer(pStr2 + 8)) + uintptr(_sqlite3Strlen30(tls, *(*uintptr)(unsafe.Pointer(pStr2 + 8)))-int32(1)) c1 = *(*Tu8)(unsafe.Pointer(pC)) if *(*int32)(unsafe.Pointer(bp + 8)) != 0 { /* The point is to increment the last character before the first ** wildcard. But if we increment '@', that will push it into the ** alphabetic range where case conversions will mess up the ** inequality. To avoid this, make sure to also run the full ** LIKE on all candidate expressions by clearing the isComplete flag */ if int32(c1) == libc.Int32FromUint8('A')-libc.Int32FromInt32(1) { *(*int32)(unsafe.Pointer(bp + 4)) = 0 } c1 = _sqlite3UpperToLower[c1] } *(*Tu8)(unsafe.Pointer(pC)) = uint8(int32(c1) + int32(1)) } if *(*int32)(unsafe.Pointer(bp + 8)) != 0 { v13 = __ccgo_ts + 22351 } else { v13 = uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) } zCollSeqName = v13 pNewExpr11 = _sqlite3ExprDup(tls, db, pLeft2, 0) pNewExpr11 = _sqlite3PExpr(tls, pParse, int32(TK_GE), _sqlite3ExprAddCollateString(tls, pParse, pNewExpr11, zCollSeqName), *(*uintptr)(unsafe.Pointer(bp))) _transferJoinMarkings(tls, pNewExpr11, pExpr) idxNew11 = _whereClauseInsert(tls, pWC, pNewExpr11, wtFlags) pNewExpr21 = _sqlite3ExprDup(tls, db, pLeft2, 0) pNewExpr21 = _sqlite3PExpr(tls, pParse, int32(TK_LT), _sqlite3ExprAddCollateString(tls, pParse, pNewExpr21, zCollSeqName), pStr2) _transferJoinMarkings(tls, pNewExpr21, pExpr) idxNew21 = _whereClauseInsert(tls, pWC, pNewExpr21, wtFlags) _exprAnalyze(tls, pSrc, pWC, idxNew11) _exprAnalyze(tls, pSrc, pWC, idxNew21) pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48 if *(*int32)(unsafe.Pointer(bp + 4)) != 0 { _markTermAsChild(tls, pWC, idxNew11, idxTerm) _markTermAsChild(tls, pWC, idxNew21, idxTerm) } } } } } } /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create ** new terms for each component comparison - "a = ?" and "b = ?". The ** new terms completely replace the original vector comparison, which is ** no longer used. ** ** This is only required if at least one side of the comparison operation ** is not a sub-select. ** ** tag-20220128a */ if v15 = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_EQ) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_IS); v15 { v14 = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) nLeft = v14 } if v15 && v14 > int32(1) && _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) == nLeft && ((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft)).Fflags&uint32(EP_xIsSelect) == uint32(0) || (*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpRight)).Fflags&uint32(EP_xIsSelect) == uint32(0)) && int32((*TWhereClause)(unsafe.Pointer(pWC)).Fop) == int32(TK_AND) { i2 = 0 for { if !(i2 < nLeft) { break } pLeft3 = _sqlite3ExprForVectorField(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, i2, nLeft) pRight1 = _sqlite3ExprForVectorField(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, i2, nLeft) pNew1 = _sqlite3PExpr(tls, pParse, int32((*TExpr)(unsafe.Pointer(pExpr)).Fop), pLeft3, pRight1) _transferJoinMarkings(tls, pNew1, pExpr) idxNew3 = _whereClauseInsert(tls, pWC, pNew1, uint16(libc.Int32FromInt32(TERM_DYNAMIC)|libc.Int32FromInt32(TERM_SLICE))) _exprAnalyze(tls, pSrc, pWC, idxNew3) goto _16 _16: ; i2++ } pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48 p17 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p17)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p17))) | (libc.Int32FromInt32(TERM_CODED) | libc.Int32FromInt32(TERM_VIRTUAL))) /* Disable the original */ (*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator = uint16(WO_ROWVAL) } else { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_IN) && (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 24))).FiField == 0 && int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft)).Fop) == int32(TK_VECTOR) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) && ((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpPrior == uintptr(0) || (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FselFlags&uint32(SF_Values) != 0) && (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpWin == uintptr(0) && int32((*TWhereClause)(unsafe.Pointer(pWC)).Fop) == int32(TK_AND) { i3 = 0 for { if !(i3 < _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)) { break } idxNew4 = _whereClauseInsert(tls, pWC, pExpr, uint16(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_SLICE))) *(*int32)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxNew4)*48 + 24 + 4)) = i3 + int32(1) _exprAnalyze(tls, pSrc, pWC, idxNew4) _markTermAsChild(tls, pWC, idxNew4, idxTerm) goto _18 _18: ; i3++ } } else { if int32((*TWhereClause)(unsafe.Pointer(pWC)).Fop) == int32(TK_AND) { *(*uintptr)(unsafe.Pointer(bp + 24)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 28)) = uintptr(0) res = _isAuxiliaryVtabOperator(tls, db, pExpr, bp+12, bp+28, bp+24) for { v19 = res res-- if !(v19 > 0) { break } prereqExpr = _sqlite3WhereExprUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(bp + 24))) prereqColumn = _sqlite3WhereExprUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(bp + 28))) if prereqExpr&prereqColumn == uint64(0) { pNewExpr2 = _sqlite3PExpr(tls, pParse, int32(TK_MATCH), uintptr(0), _sqlite3ExprDup(tls, db, *(*uintptr)(unsafe.Pointer(bp + 24)), 0)) if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) && pNewExpr2 != 0 { *(*Tu32)(unsafe.Pointer(pNewExpr2 + 4)) |= uint32(libc.Int32FromInt32(EP_OuterON)) *(*int32)(unsafe.Pointer(pNewExpr2 + 36)) = *(*int32)(unsafe.Pointer(pExpr + 36)) } idxNew5 = _whereClauseInsert(tls, pWC, pNewExpr2, uint16(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_DYNAMIC))) pNewTerm1 = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxNew5)*48 (*TWhereTerm)(unsafe.Pointer(pNewTerm1)).FprereqRight = prereqExpr (*TWhereTerm)(unsafe.Pointer(pNewTerm1)).FleftCursor = (*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 28)))).FiTable (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pNewTerm1 + 24))).FleftColumn = int32((*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 28)))).FiColumn) (*TWhereTerm)(unsafe.Pointer(pNewTerm1)).FeOperator = uint16(WO_AUX) (*TWhereTerm)(unsafe.Pointer(pNewTerm1)).FeMatchOp = *(*uint8)(unsafe.Pointer(bp + 12)) _markTermAsChild(tls, pWC, idxNew5, idxTerm) pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48 p20 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p20)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p20))) | libc.Int32FromInt32(TERM_COPIED)) (*TWhereTerm)(unsafe.Pointer(pNewTerm1)).FprereqAll = (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll } t = *(*uintptr)(unsafe.Pointer(bp + 28)) *(*uintptr)(unsafe.Pointer(bp + 28)) = *(*uintptr)(unsafe.Pointer(bp + 24)) *(*uintptr)(unsafe.Pointer(bp + 24)) = t } } } } /* Prevent ON clause terms of a LEFT JOIN from being used to drive ** an index for tables to the left of the join. */ pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48 *(*TBitmask)(unsafe.Pointer(pTerm + 32)) |= extraRight } var _ops = [2]Tu8{ 0: uint8(TK_GE), 1: uint8(TK_LE), } /*************************************************************************** ** Routines with file scope above. Interface to the rest of the where.c ** subsystem follows. ***************************************************************************/ // C documentation // // /* // ** This routine identifies subexpressions in the WHERE clause where // ** each subexpression is separated by the AND operator or some other // ** operator specified in the op parameter. The WhereClause structure // ** is filled with pointers to subexpressions. For example: // ** // ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) // ** \________/ \_______________/ \________________/ // ** slot[0] slot[1] slot[2] // ** // ** The original WHERE clause in pExpr is unaltered. All this routine // ** does is make slot[] entries point to substructure within pExpr. // ** // ** In the previous sentence and in the diagram, "slot[]" refers to // ** the WhereClause.a[] array. The slot[] array grows as needed to contain // ** all terms of the WHERE clause. // */ func _sqlite3WhereSplit(tls *libc.TLS, pWC uintptr, pExpr uintptr, op Tu8) { var pE2 uintptr _ = pE2 pE2 = _sqlite3ExprSkipCollateAndLikely(tls, pExpr) (*TWhereClause)(unsafe.Pointer(pWC)).Fop = op _ = libc.Int32FromInt32(0) if pE2 == uintptr(0) { return } if int32((*TExpr)(unsafe.Pointer(pE2)).Fop) != int32(op) { _whereClauseInsert(tls, pWC, pExpr, uint16(0)) } else { _sqlite3WhereSplit(tls, pWC, (*TExpr)(unsafe.Pointer(pE2)).FpLeft, op) _sqlite3WhereSplit(tls, pWC, (*TExpr)(unsafe.Pointer(pE2)).FpRight, op) } } // C documentation // // /* // ** Add either a LIMIT (if eMatchOp==SQLITE_INDEX_CONSTRAINT_LIMIT) or // ** OFFSET (if eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET) term to the // ** where-clause passed as the first argument. The value for the term // ** is found in register iReg. // ** // ** In the common case where the value is a simple integer // ** (example: "LIMIT 5 OFFSET 10") then the expression codes as a // ** TK_INTEGER so that it will be available to sqlite3_vtab_rhs_value(). // ** If not, then it codes as a TK_REGISTER expression. // */ func _whereAddLimitExpr(tls *libc.TLS, pWC uintptr, iReg int32, pExpr uintptr, iCsr int32, eMatchOp int32) { bp := tls.Alloc(16) defer tls.Free(16) var db, pNew, pParse, pTerm, pVal, pVal1 uintptr var idx int32 var _ /* iVal at bp+0 */ int32 _, _, _, _, _, _, _ = db, idx, pNew, pParse, pTerm, pVal, pVal1 pParse = (*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpParse db = (*TParse)(unsafe.Pointer(pParse)).Fdb *(*int32)(unsafe.Pointer(bp)) = 0 if _sqlite3ExprIsInteger(tls, pExpr, bp) != 0 && *(*int32)(unsafe.Pointer(bp)) >= 0 { pVal = _sqlite3Expr(tls, db, int32(TK_INTEGER), uintptr(0)) if pVal == uintptr(0) { return } *(*Tu32)(unsafe.Pointer(pVal + 4)) |= uint32(libc.Int32FromInt32(EP_IntValue)) *(*int32)(unsafe.Pointer(pVal + 8)) = *(*int32)(unsafe.Pointer(bp)) pNew = _sqlite3PExpr(tls, pParse, int32(TK_MATCH), uintptr(0), pVal) } else { pVal1 = _sqlite3Expr(tls, db, int32(TK_REGISTER), uintptr(0)) if pVal1 == uintptr(0) { return } (*TExpr)(unsafe.Pointer(pVal1)).FiTable = iReg pNew = _sqlite3PExpr(tls, pParse, int32(TK_MATCH), uintptr(0), pVal1) } if pNew != 0 { idx = _whereClauseInsert(tls, pWC, pNew, uint16(libc.Int32FromInt32(TERM_DYNAMIC)|libc.Int32FromInt32(TERM_VIRTUAL))) pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idx)*48 (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor = iCsr (*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator = uint16(WO_AUX) (*TWhereTerm)(unsafe.Pointer(pTerm)).FeMatchOp = uint8(eMatchOp) } } // C documentation // // /* // ** Possibly add terms corresponding to the LIMIT and OFFSET clauses of the // ** SELECT statement passed as the second argument. These terms are only // ** added if: // ** // ** 1. The SELECT statement has a LIMIT clause, and // ** 2. The SELECT statement is not an aggregate or DISTINCT query, and // ** 3. The SELECT statement has exactly one object in its from clause, and // ** that object is a virtual table, and // ** 4. There are no terms in the WHERE clause that will not be passed // ** to the virtual table xBestIndex method. // ** 5. The ORDER BY clause, if any, will be made available to the xBestIndex // ** method. // ** // ** LIMIT and OFFSET terms are ignored by most of the planner code. They // ** exist only so that they may be passed to the xBestIndex method of the // ** single virtual table in the FROM clause of the SELECT. // */ func _sqlite3WhereAddLimit(tls *libc.TLS, pWC uintptr, p uintptr) { var iCsr, ii int32 var pExpr, pOrderBy uintptr _, _, _, _ = iCsr, ii, pExpr, pOrderBy _ = libc.Int32FromInt32(0) /* 1 -- checked by caller */ if (*TSelect)(unsafe.Pointer(p)).FpGroupBy == uintptr(0) && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(libc.Int32FromInt32(SF_Distinct)|libc.Int32FromInt32(SF_Aggregate)) == uint32(0) && ((*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc)).FnSrc == int32(1) && int32((*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpTab)).FeTabType) == int32(TABTYP_VTAB)) { pOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy iCsr = (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FiCursor /* Check condition (4). Return early if it is not met. */ ii = 0 for { if !(ii < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) { break } if int32((*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(ii)*48))).FwtFlags)&int32(TERM_CODED) != 0 { /* This term is a vector operation that has been decomposed into ** other, subsequent terms. It can be ignored. See tag-20220128a */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) goto _1 } if (*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(ii)*48))).FnChild != 0 { /* If this term has child terms, then they are also part of the ** pWC->a[] array. So this term can be ignored, as a LIMIT clause ** will only be added if each of the child terms passes the ** (leftCursor==iCsr) test below. */ goto _1 } if (*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(ii)*48))).FleftCursor != iCsr { return } goto _1 _1: ; ii++ } /* Check condition (5). Return early if it is not met. */ if pOrderBy != 0 { ii = 0 for { if !(ii < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) { break } pExpr = (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(ii)*20))).FpExpr if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLUMN) { return } if (*TExpr)(unsafe.Pointer(pExpr)).FiTable != iCsr { return } if int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(ii)*20))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_BIGNULL) != 0 { return } goto _2 _2: ; ii++ } } /* All conditions are met. Add the terms to the where-clause object. */ _ = libc.Int32FromInt32(0) _whereAddLimitExpr(tls, pWC, (*TSelect)(unsafe.Pointer(p)).FiLimit, (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpLimit)).FpLeft, iCsr, int32(SQLITE_INDEX_CONSTRAINT_LIMIT)) if (*TSelect)(unsafe.Pointer(p)).FiOffset > 0 { _whereAddLimitExpr(tls, pWC, (*TSelect)(unsafe.Pointer(p)).FiOffset, (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpLimit)).FpRight, iCsr, int32(SQLITE_INDEX_CONSTRAINT_OFFSET)) } } } // C documentation // // /* // ** Initialize a preallocated WhereClause structure. // */ func _sqlite3WhereClauseInit(tls *libc.TLS, pWC uintptr, pWInfo uintptr) { (*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo = pWInfo (*TWhereClause)(unsafe.Pointer(pWC)).FhasOr = uint8(0) (*TWhereClause)(unsafe.Pointer(pWC)).FpOuter = uintptr(0) (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm = 0 (*TWhereClause)(unsafe.Pointer(pWC)).FnBase = 0 (*TWhereClause)(unsafe.Pointer(pWC)).FnSlot = int32(libc.Uint32FromInt64(384) / libc.Uint32FromInt64(48)) (*TWhereClause)(unsafe.Pointer(pWC)).Fa = pWC + 32 } // C documentation // // /* // ** Deallocate a WhereClause structure. The WhereClause structure // ** itself is not freed. This routine is the inverse of // ** sqlite3WhereClauseInit(). // */ func _sqlite3WhereClauseClear(tls *libc.TLS, pWC uintptr) { var a, aLast, db uintptr _, _, _ = a, aLast, db db = (*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpParse)).Fdb _ = libc.Int32FromInt32(0) if (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm > 0 { a = (*TWhereClause)(unsafe.Pointer(pWC)).Fa aLast = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr((*TWhereClause)(unsafe.Pointer(pWC)).FnTerm-int32(1))*48 for int32(1) != 0 { _ = libc.Int32FromInt32(0) if int32((*TWhereTerm)(unsafe.Pointer(a)).FwtFlags)&int32(TERM_DYNAMIC) != 0 { _sqlite3ExprDelete(tls, db, (*TWhereTerm)(unsafe.Pointer(a)).FpExpr) } if int32((*TWhereTerm)(unsafe.Pointer(a)).FwtFlags)&(libc.Int32FromInt32(TERM_ORINFO)|libc.Int32FromInt32(TERM_ANDINFO)) != 0 { if int32((*TWhereTerm)(unsafe.Pointer(a)).FwtFlags)&int32(TERM_ORINFO) != 0 { _ = libc.Int32FromInt32(0) _whereOrInfoDelete(tls, db, *(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(a)).Fu))) } else { _ = libc.Int32FromInt32(0) _whereAndInfoDelete(tls, db, *(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(a)).Fu))) } } if a == aLast { break } a += 48 } } } // C documentation // // /* // ** These routines walk (recursively) an expression tree and generate // ** a bitmask indicating which tables are used in that expression // ** tree. // ** // ** sqlite3WhereExprUsage(MaskSet, Expr) -> // ** // ** Return a Bitmask of all tables referenced by Expr. Expr can be // ** be NULL, in which case 0 is returned. // ** // ** sqlite3WhereExprUsageNN(MaskSet, Expr) -> // ** // ** Same as sqlite3WhereExprUsage() except that Expr must not be // ** NULL. The "NN" suffix on the name stands for "Not Null". // ** // ** sqlite3WhereExprListUsage(MaskSet, ExprList) -> // ** // ** Return a Bitmask of all tables referenced by every expression // ** in the expression list ExprList. ExprList can be NULL, in which // ** case 0 is returned. // ** // ** sqlite3WhereExprUsageFull(MaskSet, ExprList) -> // ** // ** Internal use only. Called only by sqlite3WhereExprUsageNN() for // ** complex expressions that require pushing register values onto // ** the stack. Many calls to sqlite3WhereExprUsageNN() do not need // ** the more complex analysis done by this routine. Hence, the // ** computations done by this routine are broken out into a separate // ** "no-inline" function to avoid the stack push overhead in the // ** common case where it is not needed. // */ func _sqlite3WhereExprUsageFull(tls *libc.TLS, pMaskSet uintptr, p uintptr) (r TBitmask) { var mask TBitmask var v1 uint64 _, _ = mask, v1 if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_IF_NULL_ROW) { v1 = _sqlite3WhereGetMask(tls, pMaskSet, (*TExpr)(unsafe.Pointer(p)).FiTable) } else { v1 = uint64(0) } mask = v1 if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 { mask |= _sqlite3WhereExprUsageNN(tls, pMaskSet, (*TExpr)(unsafe.Pointer(p)).FpLeft) } if (*TExpr)(unsafe.Pointer(p)).FpRight != 0 { mask |= _sqlite3WhereExprUsageNN(tls, pMaskSet, (*TExpr)(unsafe.Pointer(p)).FpRight) _ = libc.Int32FromInt32(0) } else { if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_xIsSelect) != uint32(0) { if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_VarSelect)) != uint32(0) { (*TWhereMaskSet)(unsafe.Pointer(pMaskSet)).FbVarSelect = int32(1) } mask |= _exprSelectUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(p + 20))) } else { if *(*uintptr)(unsafe.Pointer(p + 20)) != 0 { mask |= _sqlite3WhereExprListUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(p + 20))) } } } if (int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_FUNCTION) || int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_AGG_FUNCTION)) && (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_WinFunc) != uint32(0) { _ = libc.Int32FromInt32(0) mask |= _sqlite3WhereExprListUsage(tls, pMaskSet, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(p)).Fy)))).FpPartition) mask |= _sqlite3WhereExprListUsage(tls, pMaskSet, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(p)).Fy)))).FpOrderBy) mask |= _sqlite3WhereExprUsage(tls, pMaskSet, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(p)).Fy)))).FpFilter) } return mask } func _sqlite3WhereExprUsageNN(tls *libc.TLS, pMaskSet uintptr, p uintptr) (r TBitmask) { if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_COLUMN) && !((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_FixedCol)) != libc.Uint32FromInt32(0)) { return _sqlite3WhereGetMask(tls, pMaskSet, (*TExpr)(unsafe.Pointer(p)).FiTable) } else { if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_TokenOnly)|libc.Int32FromInt32(EP_Leaf)) != uint32(0) { _ = libc.Int32FromInt32(0) return uint64(0) } } return _sqlite3WhereExprUsageFull(tls, pMaskSet, p) } func _sqlite3WhereExprUsage(tls *libc.TLS, pMaskSet uintptr, p uintptr) (r TBitmask) { var v1 uint64 _ = v1 if p != 0 { v1 = _sqlite3WhereExprUsageNN(tls, pMaskSet, p) } else { v1 = uint64(0) } return v1 } func _sqlite3WhereExprListUsage(tls *libc.TLS, pMaskSet uintptr, pList uintptr) (r TBitmask) { var i int32 var mask TBitmask _, _ = i, mask mask = uint64(0) if pList != 0 { i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } mask |= _sqlite3WhereExprUsage(tls, pMaskSet, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr) goto _1 _1: ; i++ } } return mask } // C documentation // // /* // ** Call exprAnalyze on all terms in a WHERE clause. // ** // ** Note that exprAnalyze() might add new virtual terms onto the // ** end of the WHERE clause. We do not want to analyze these new // ** virtual terms, so start analyzing at the end and work forward // ** so that the added virtual terms are never processed. // */ func _sqlite3WhereExprAnalyze(tls *libc.TLS, pTabList uintptr, pWC uintptr) { var i int32 _ = i i = (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm - int32(1) for { if !(i >= 0) { break } _exprAnalyze(tls, pTabList, pWC, i) goto _1 _1: ; i-- } } // C documentation // // /* // ** For table-valued-functions, transform the function arguments into // ** new WHERE clause terms. // ** // ** Each function argument translates into an equality constraint against // ** a HIDDEN column in the table. // */ func _sqlite3WhereTabFuncArgs(tls *libc.TLS, pParse uintptr, pItem uintptr, pWC uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var j, k, v2, v3 int32 var joinType Tu32 var pArgs, pColRef, pRhs, pTab, pTerm uintptr _, _, _, _, _, _, _, _, _, _ = j, joinType, k, pArgs, pColRef, pRhs, pTab, pTerm, v2, v3 if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x4>>2)) == 0 { return } pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab _ = libc.Int32FromInt32(0) pArgs = *(*uintptr)(unsafe.Pointer(pItem + 64)) if pArgs == uintptr(0) { return } v2 = libc.Int32FromInt32(0) k = v2 j = v2 for { if !(j < (*TExprList)(unsafe.Pointer(pArgs)).FnExpr) { break } for k < int32((*TTable)(unsafe.Pointer(pTab)).FnCol) && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(k)*12))).FcolFlags)&int32(COLFLAG_HIDDEN) == 0 { k++ } if k >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22358, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName, j)) return } pColRef = _sqlite3ExprAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_COLUMN), uintptr(0), 0) if pColRef == uintptr(0) { return } (*TExpr)(unsafe.Pointer(pColRef)).FiTable = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor v3 = k k++ (*TExpr)(unsafe.Pointer(pColRef)).FiColumn = int16(v3) _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer(pColRef)).Fy.FpTab = pTab *(*TBitmask)(unsafe.Pointer(pItem + 56)) |= _sqlite3ExprColUsed(tls, pColRef) pRhs = _sqlite3PExpr(tls, pParse, int32(TK_UPLUS), _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*(*TExprList_item)(unsafe.Pointer(pArgs + 8 + uintptr(j)*20))).FpExpr, 0), uintptr(0)) pTerm = _sqlite3PExpr(tls, pParse, int32(TK_EQ), pColRef, pRhs) if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_RIGHT)) != 0 { /* testtag-20230227a */ /* testtag-20230227b */ joinType = uint32(EP_OuterON) } else { /* testtag-20230227c */ joinType = uint32(EP_InnerON) } _sqlite3SetJoinExpr(tls, pTerm, (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor, joinType) _whereClauseInsert(tls, pWC, pTerm, uint16(TERM_DYNAMIC)) goto _1 _1: ; j++ } } /************** End of whereexpr.c *******************************************/ /************** Begin file where.c *******************************************/ /* ** 2001 September 15 ** ** 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 module contains C code that generates VDBE code used to process ** the WHERE clause of SQL statements. This module is responsible for ** generating the code that loops through a table looking for applicable ** rows. Indices are selected and used to speed the search when doing ** so is applicable. Because this module is responsible for selecting ** indices, you might also think of this module as the "query optimizer". */ /* #include "sqliteInt.h" */ /* #include "whereInt.h" */ // C documentation // // /* // ** Extra information appended to the end of sqlite3_index_info but not // ** visible to the xBestIndex function, at least not directly. The // ** sqlite3_vtab_collation() interface knows how to reach it, however. // ** // ** This object is not an API and can be changed from one release to the // ** next. As long as allocateIndexInfo() and sqlite3_vtab_collation() // ** agree on the structure, all will be well. // */ type THiddenIndexInfo = struct { FpWC uintptr FpParse uintptr FeDistinct int32 FmIn Tu32 FmHandleIn Tu32 FaRhs [1]uintptr } type HiddenIndexInfo = THiddenIndexInfo type THiddenIndexInfo1 = struct { FpWC uintptr FpParse uintptr FeDistinct int32 FmIn Tu32 FmHandleIn Tu32 FaRhs [1]uintptr } type HiddenIndexInfo1 = THiddenIndexInfo1 // C documentation // // /* // ** Return the estimated number of output rows from a WHERE clause // */ func _sqlite3WhereOutputRowCount(tls *libc.TLS, pWInfo uintptr) (r TLogEst) { return (*TWhereInfo)(unsafe.Pointer(pWInfo)).FnRowOut } // C documentation // // /* // ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this // ** WHERE clause returns outputs for DISTINCT processing. // */ func _sqlite3WhereIsDistinct(tls *libc.TLS, pWInfo uintptr) (r int32) { return int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct) } // C documentation // // /* // ** Return the number of ORDER BY terms that are satisfied by the // ** WHERE clause. A return of 0 means that the output must be // ** completely sorted. A return equal to the number of ORDER BY // ** terms means that no sorting is needed at all. A return that // ** is positive but less than the number of ORDER BY terms means that // ** block sorting is required. // */ func _sqlite3WhereIsOrdered(tls *libc.TLS, pWInfo uintptr) (r int32) { var v1 int32 _ = v1 if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat) < 0 { v1 = 0 } else { v1 = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat) } return v1 } // C documentation // // /* // ** In the ORDER BY LIMIT optimization, if the inner-most loop is known // ** to emit rows in increasing order, and if the last row emitted by the // ** inner-most loop did not fit within the sorter, then we can skip all // ** subsequent rows for the current iteration of the inner loop (because they // ** will not fit in the sorter either) and continue with the second inner // ** loop - the loop immediately outside the inner-most. // ** // ** When a row does not fit in the sorter (because the sorter already // ** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the // ** label returned by this function. // ** // ** If the ORDER BY LIMIT optimization applies, the jump destination should // ** be the continuation for the second-inner-most loop. If the ORDER BY // ** LIMIT optimization does not apply, then the jump destination should // ** be the continuation for the inner-most loop. // ** // ** It is always safe for this routine to return the continuation of the // ** inner-most loop, in the sense that a correct answer will result. // ** Returning the continuation the second inner loop is an optimization // ** that might make the code run a little faster, but should not change // ** the final answer. // */ func _sqlite3WhereOrderByLimitOptLabel(tls *libc.TLS, pWInfo uintptr) (r int32) { var pInner uintptr var v1 int32 _, _ = pInner, v1 if !(int32(uint32(*(*uint8)(unsafe.Pointer(pWInfo + 48))&0x4>>2)) != 0) { /* The ORDER BY LIMIT optimization does not apply. Jump to the ** continuation of the inner-most loop. */ return (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiContinue } pInner = pWInfo + 760 + uintptr(int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)-int32(1))*88 _ = libc.Int32FromInt32(0) if (*TWhereLevel)(unsafe.Pointer(pInner)).FpRJ != 0 { v1 = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiContinue } else { v1 = (*TWhereLevel)(unsafe.Pointer(pInner)).FaddrNxt } return v1 } // C documentation // // /* // ** While generating code for the min/max optimization, after handling // ** the aggregate-step call to min() or max(), check to see if any // ** additional looping is required. If the output order is such that // ** we are certain that the correct answer has already been found, then // ** code an OP_Goto to by pass subsequent processing. // ** // ** Any extra OP_Goto that is coded here is an optimization. The // ** correct answer should be obtained regardless. This OP_Goto just // ** makes the answer appear faster. // */ func _sqlite3WhereMinMaxOptEarlyOut(tls *libc.TLS, v uintptr, pWInfo uintptr) { var i int32 var pInner uintptr _, _ = i, pInner if !(int32(uint32(*(*uint8)(unsafe.Pointer(pWInfo + 48))&0x4>>2)) != 0) { return } if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat) == 0 { return } i = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) - int32(1) for { if !(i >= 0) { break } pInner = pWInfo + 760 + uintptr(i)*88 if (*TWhereLoop1)(unsafe.Pointer((*TWhereLevel)(unsafe.Pointer(pInner)).FpWLoop)).FwsFlags&uint32(WHERE_COLUMN_IN) != uint32(0) { _sqlite3VdbeGoto(tls, v, (*TWhereLevel)(unsafe.Pointer(pInner)).FaddrNxt) return } goto _1 _1: ; i-- } _sqlite3VdbeGoto(tls, v, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiBreak) } // C documentation // // /* // ** Return the VDBE address or label to jump to in order to continue // ** immediately with the next row of a WHERE clause. // */ func _sqlite3WhereContinueLabel(tls *libc.TLS, pWInfo uintptr) (r int32) { _ = libc.Int32FromInt32(0) return (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiContinue } // C documentation // // /* // ** Return the VDBE address or label to jump to in order to break // ** out of a WHERE loop. // */ func _sqlite3WhereBreakLabel(tls *libc.TLS, pWInfo uintptr) (r int32) { return (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiBreak } // C documentation // // /* // ** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to // ** operate directly on the rowids returned by a WHERE clause. Return // ** ONEPASS_SINGLE (1) if the statement can operation directly because only // ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass // ** optimization can be used on multiple // ** // ** If the ONEPASS optimization is used (if this routine returns true) // ** then also write the indices of open cursors used by ONEPASS // ** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data // ** table and iaCur[1] gets the cursor used by an auxiliary index. // ** Either value may be -1, indicating that cursor is not used. // ** Any cursors returned will have been opened for writing. // ** // ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is // ** unable to use the ONEPASS optimization. // */ func _sqlite3WhereOkOnePass(tls *libc.TLS, pWInfo uintptr, aiCur uintptr) (r int32) { libc.Xmemcpy(tls, aiCur, pWInfo+20, libc.Uint32FromInt64(4)*libc.Uint32FromInt32(2)) return int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass) } // C documentation // // /* // ** Return TRUE if the WHERE loop uses the OP_DeferredSeek opcode to move // ** the data cursor to the row selected by the index cursor. // */ func _sqlite3WhereUsesDeferredSeek(tls *libc.TLS, pWInfo uintptr) (r int32) { return int32(uint32(*(*uint8)(unsafe.Pointer(pWInfo + 48)) & 0x1 >> 0)) } // C documentation // // /* // ** Move the content of pSrc into pDest // */ func _whereOrMove(tls *libc.TLS, pDest uintptr, pSrc uintptr) { (*TWhereOrSet)(unsafe.Pointer(pDest)).Fn = (*TWhereOrSet)(unsafe.Pointer(pSrc)).Fn libc.Xmemcpy(tls, pDest+8, pSrc+8, uint32((*TWhereOrSet)(unsafe.Pointer(pDest)).Fn)*uint32(16)) } // C documentation // // /* // ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet. // ** // ** The new entry might overwrite an existing entry, or it might be // ** appended, or it might be discarded. Do whatever is the right thing // ** so that pSet keeps the N_OR_COST best entries seen so far. // */ func _whereOrInsert(tls *libc.TLS, pSet uintptr, prereq TBitmask, rRun TLogEst, nOut TLogEst) (r int32) { var i, v2 Tu16 var p, v3 uintptr _, _, _, _ = i, p, v2, v3 i = (*TWhereOrSet)(unsafe.Pointer(pSet)).Fn p = pSet + 8 for { if !(int32(i) > 0) { break } if int32(rRun) <= int32((*TWhereOrCost)(unsafe.Pointer(p)).FrRun) && prereq&(*TWhereOrCost)(unsafe.Pointer(p)).Fprereq == prereq { goto whereOrInsert_done } if int32((*TWhereOrCost)(unsafe.Pointer(p)).FrRun) <= int32(rRun) && (*TWhereOrCost)(unsafe.Pointer(p)).Fprereq&prereq == (*TWhereOrCost)(unsafe.Pointer(p)).Fprereq { return 0 } goto _1 _1: ; i-- p += 16 } if int32((*TWhereOrSet)(unsafe.Pointer(pSet)).Fn) < int32(N_OR_COST) { v3 = pSet v2 = *(*Tu16)(unsafe.Pointer(v3)) *(*Tu16)(unsafe.Pointer(v3))++ p = pSet + 8 + uintptr(v2)*16 (*TWhereOrCost)(unsafe.Pointer(p)).FnOut = nOut } else { p = pSet + 8 i = uint16(1) for { if !(int32(i) < int32((*TWhereOrSet)(unsafe.Pointer(pSet)).Fn)) { break } if int32((*TWhereOrCost)(unsafe.Pointer(p)).FrRun) > int32((*(*TWhereOrCost)(unsafe.Pointer(pSet + 8 + uintptr(i)*16))).FrRun) { p = pSet + 8 + uintptr(i)*16 } goto _4 _4: ; i++ } if int32((*TWhereOrCost)(unsafe.Pointer(p)).FrRun) <= int32(rRun) { return 0 } } goto whereOrInsert_done whereOrInsert_done: ; (*TWhereOrCost)(unsafe.Pointer(p)).Fprereq = prereq (*TWhereOrCost)(unsafe.Pointer(p)).FrRun = rRun if int32((*TWhereOrCost)(unsafe.Pointer(p)).FnOut) > int32(nOut) { (*TWhereOrCost)(unsafe.Pointer(p)).FnOut = nOut } return int32(1) } // C documentation // // /* // ** Return the bitmask for the given cursor number. Return 0 if // ** iCursor is not in the set. // */ func _sqlite3WhereGetMask(tls *libc.TLS, pMaskSet uintptr, iCursor int32) (r TBitmask) { var i int32 _ = i _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(pMaskSet + 8)) == iCursor { return uint64(1) } i = int32(1) for { if !(i < (*TWhereMaskSet)(unsafe.Pointer(pMaskSet)).Fn) { break } if *(*int32)(unsafe.Pointer(pMaskSet + 8 + uintptr(i)*4)) == iCursor { return libc.Uint64FromInt32(1) << i } goto _1 _1: ; i++ } return uint64(0) } // C documentation // // /* Allocate memory that is automatically freed when pWInfo is freed. // */ func _sqlite3WhereMalloc(tls *libc.TLS, pWInfo uintptr, nByte Tu64) (r uintptr) { var pBlock uintptr _ = pBlock pBlock = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse)).Fdb, nByte+uint64(16)) if pBlock != 0 { (*TWhereMemBlock)(unsafe.Pointer(pBlock)).FpNext = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpMemToFree (*TWhereMemBlock)(unsafe.Pointer(pBlock)).Fsz = nByte (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpMemToFree = pBlock pBlock += 16 } return pBlock } func _sqlite3WhereRealloc(tls *libc.TLS, pWInfo uintptr, pOld uintptr, nByte Tu64) (r uintptr) { var pNew, pOldBlk uintptr _, _ = pNew, pOldBlk pNew = _sqlite3WhereMalloc(tls, pWInfo, nByte) if pNew != 0 && pOld != 0 { pOldBlk = pOld pOldBlk -= 16 _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, pNew, pOld, uint32((*TWhereMemBlock)(unsafe.Pointer(pOldBlk)).Fsz)) } return pNew } // C documentation // // /* // ** Create a new mask for cursor iCursor. // ** // ** There is one cursor per table in the FROM clause. The number of // ** tables in the FROM clause is limited by a test early in the // ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] // ** array will never overflow. // */ func _createMask(tls *libc.TLS, pMaskSet uintptr, iCursor int32) { var v1 int32 var v2 uintptr _, _ = v1, v2 _ = libc.Int32FromInt32(0) v2 = pMaskSet + 4 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ *(*int32)(unsafe.Pointer(pMaskSet + 8 + uintptr(v1)*4)) = iCursor } // C documentation // // /* // ** If the right-hand branch of the expression is a TK_COLUMN, then return // ** a pointer to the right-hand branch. Otherwise, return NULL. // */ func _whereRightSubexprIsColumn(tls *libc.TLS, p uintptr) (r uintptr) { p = _sqlite3ExprSkipCollateAndLikely(tls, (*TExpr)(unsafe.Pointer(p)).FpRight) if p != uintptr(0) && int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_COLUMN) && !((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_FixedCol)) != libc.Uint32FromInt32(0)) { return p } return uintptr(0) } // C documentation // // /* // ** Advance to the next WhereTerm that matches according to the criteria // ** established when the pScan object was initialized by whereScanInit(). // ** Return NULL if there are no more matching WhereTerms. // */ func _whereScanNext(tls *libc.TLS, pScan uintptr) (r uintptr) { var iColumn Ti16 var iCur, j, k int32 var pColl, pParse, pTerm, pWC, pX, v2 uintptr var v3, v5 bool _, _, _, _, _, _, _, _, _, _, _, _ = iColumn, iCur, j, k, pColl, pParse, pTerm, pWC, pX, v2, v3, v5 /* The term being tested */ k = (*TWhereScan)(unsafe.Pointer(pScan)).Fk /* Where to start scanning */ _ = libc.Int32FromInt32(0) pWC = (*TWhereScan)(unsafe.Pointer(pScan)).FpWC for int32(1) != 0 { iColumn = *(*Ti16)(unsafe.Pointer(pScan + 72 + uintptr(int32((*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv)-int32(1))*2)) iCur = *(*int32)(unsafe.Pointer(pScan + 28 + uintptr(int32((*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv)-int32(1))*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) for cond := true; cond; cond = pWC != uintptr(0) { pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(k)*48 for { if !(k < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) { break } _ = libc.Int32FromInt32(0) if (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor == iCur && (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 24))).FleftColumn == int32(iColumn) && (int32(iColumn) != -int32(2) || _sqlite3ExprCompareSkip(tls, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpLeft, (*TWhereScan)(unsafe.Pointer(pScan)).FpIdxExpr, iCur) == 0) && (int32((*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv) <= int32(1) || !((*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != libc.Uint32FromInt32(0))) { if v3 = int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_EQUIV) != 0 && int32((*TWhereScan)(unsafe.Pointer(pScan)).FnEquiv) < int32(libc.Uint32FromInt64(44)/libc.Uint32FromInt64(4)); v3 { v2 = _whereRightSubexprIsColumn(tls, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr) pX = v2 } if v3 && v2 != uintptr(0) { j = 0 for { if !(j < int32((*TWhereScan)(unsafe.Pointer(pScan)).FnEquiv)) { break } if *(*int32)(unsafe.Pointer(pScan + 28 + uintptr(j)*4)) == (*TExpr)(unsafe.Pointer(pX)).FiTable && int32(*(*Ti16)(unsafe.Pointer(pScan + 72 + uintptr(j)*2))) == int32((*TExpr)(unsafe.Pointer(pX)).FiColumn) { break } goto _4 _4: ; j++ } if j == int32((*TWhereScan)(unsafe.Pointer(pScan)).FnEquiv) { *(*int32)(unsafe.Pointer(pScan + 28 + uintptr(j)*4)) = (*TExpr)(unsafe.Pointer(pX)).FiTable *(*Ti16)(unsafe.Pointer(pScan + 72 + uintptr(j)*2)) = (*TExpr)(unsafe.Pointer(pX)).FiColumn (*TWhereScan)(unsafe.Pointer(pScan)).FnEquiv++ } } if uint32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&(*TWhereScan)(unsafe.Pointer(pScan)).FopMask != uint32(0) { /* Verify the affinity and collating sequence match */ if (*TWhereScan)(unsafe.Pointer(pScan)).FzCollName != 0 && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_ISNULL) == 0 { pParse = (*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpParse pX = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr if !(_sqlite3IndexAffinityOk(tls, pX, (*TWhereScan)(unsafe.Pointer(pScan)).Fidxaff) != 0) { goto _1 } _ = libc.Int32FromInt32(0) pColl = _sqlite3ExprCompareCollSeq(tls, pParse, pX) if pColl == uintptr(0) { pColl = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FpDfltColl } if _sqlite3StrICmp(tls, (*TCollSeq)(unsafe.Pointer(pColl)).FzName, (*TWhereScan)(unsafe.Pointer(pScan)).FzCollName) != 0 { goto _1 } } if v5 = int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) != 0; v5 { pX = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpRight } if v5 && pX != libc.UintptrFromInt32(0) && int32((*TExpr)(unsafe.Pointer(pX)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pX)).FiTable == *(*int32)(unsafe.Pointer(pScan + 28)) && int32((*TExpr)(unsafe.Pointer(pX)).FiColumn) == int32(*(*Ti16)(unsafe.Pointer(pScan + 72))) { goto _1 } (*TWhereScan)(unsafe.Pointer(pScan)).FpWC = pWC (*TWhereScan)(unsafe.Pointer(pScan)).Fk = k + int32(1) return pTerm } } goto _1 _1: ; k++ pTerm += 48 } pWC = (*TWhereClause)(unsafe.Pointer(pWC)).FpOuter k = 0 } if int32((*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv) >= int32((*TWhereScan)(unsafe.Pointer(pScan)).FnEquiv) { break } pWC = (*TWhereScan)(unsafe.Pointer(pScan)).FpOrigWC k = 0 (*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv++ } return uintptr(0) } // C documentation // // /* // ** This is whereScanInit() for the case of an index on an expression. // ** It is factored out into a separate tail-recursion subroutine so that // ** the normal whereScanInit() routine, which is a high-runner, does not // ** need to push registers onto the stack as part of its prologue. // */ func _whereScanInitIndexExpr(tls *libc.TLS, pScan uintptr) (r uintptr) { (*TWhereScan)(unsafe.Pointer(pScan)).Fidxaff = _sqlite3ExprAffinity(tls, (*TWhereScan)(unsafe.Pointer(pScan)).FpIdxExpr) return _whereScanNext(tls, pScan) } // C documentation // // /* // ** Initialize a WHERE clause scanner object. Return a pointer to the // ** first match. Return NULL if there are no matches. // ** // ** The scanner will be searching the WHERE clause pWC. It will look // ** for terms of the form "X " where X is column iColumn of table // ** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx // ** must be one of the indexes of table iCur. // ** // ** The must be one of the operators described by opMask. // ** // ** If the search is for X and the WHERE clause contains terms of the // ** form X=Y then this routine might also return terms of the form // ** "Y ". The number of levels of transitivity is limited, // ** but is enough to handle most commonly occurring SQL statements. // ** // ** If X is not the INTEGER PRIMARY KEY then X must be compatible with // ** index pIdx. // */ func _whereScanInit(tls *libc.TLS, pScan uintptr, pWC uintptr, iCur int32, iColumn int32, opMask Tu32, pIdx uintptr) (r uintptr) { var j int32 _ = j (*TWhereScan)(unsafe.Pointer(pScan)).FpOrigWC = pWC (*TWhereScan)(unsafe.Pointer(pScan)).FpWC = pWC (*TWhereScan)(unsafe.Pointer(pScan)).FpIdxExpr = uintptr(0) (*TWhereScan)(unsafe.Pointer(pScan)).Fidxaff = uint8(0) (*TWhereScan)(unsafe.Pointer(pScan)).FzCollName = uintptr(0) (*TWhereScan)(unsafe.Pointer(pScan)).FopMask = opMask (*TWhereScan)(unsafe.Pointer(pScan)).Fk = 0 *(*int32)(unsafe.Pointer(pScan + 28)) = iCur (*TWhereScan)(unsafe.Pointer(pScan)).FnEquiv = uint8(1) (*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv = uint8(1) if pIdx != 0 { j = iColumn iColumn = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j)*2))) if iColumn == int32((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FiPKey) { iColumn = -int32(1) } else { if iColumn >= 0 { (*TWhereScan)(unsafe.Pointer(pScan)).Fidxaff = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol + uintptr(iColumn)*12))).Faffinity (*TWhereScan)(unsafe.Pointer(pScan)).FzCollName = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(j)*4)) } else { if iColumn == -int32(2) { (*TWhereScan)(unsafe.Pointer(pScan)).FpIdxExpr = (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(j)*20))).FpExpr (*TWhereScan)(unsafe.Pointer(pScan)).FzCollName = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(j)*4)) *(*Ti16)(unsafe.Pointer(pScan + 72)) = int16(-libc.Int32FromInt32(2)) return _whereScanInitIndexExpr(tls, pScan) } } } } else { if iColumn == -int32(2) { return uintptr(0) } } *(*Ti16)(unsafe.Pointer(pScan + 72)) = int16(iColumn) return _whereScanNext(tls, pScan) } // C documentation // // /* // ** Search for a term in the WHERE clause that is of the form "X " // ** where X is a reference to the iColumn of table iCur or of index pIdx // ** if pIdx!=0 and is one of the WO_xx operator codes specified by // ** the op parameter. Return a pointer to the term. Return 0 if not found. // ** // ** If pIdx!=0 then it must be one of the indexes of table iCur. // ** Search for terms matching the iColumn-th column of pIdx // ** rather than the iColumn-th column of table iCur. // ** // ** The term returned might by Y= if there is another constraint in // ** the WHERE clause that specifies that X=Y. Any such constraints will be // ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The // ** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11 // ** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10 // ** other equivalent values. Hence a search for X will return if X=A1 // ** and A1=A2 and A2=A3 and ... and A9=A10 and A10=. // ** // ** If there are multiple terms in the WHERE clause of the form "X " // ** then try for the one with no dependencies on - in other words where // ** is a constant expression of some kind. Only return entries of // ** the form "X Y" where Y is a column in another table if no terms of // ** the form "X " exist. If no terms with a constant RHS // ** exist, try to return a term that does not use WO_EQUIV. // */ func _sqlite3WhereFindTerm(tls *libc.TLS, pWC uintptr, iCur int32, iColumn int32, notReady TBitmask, op Tu32, pIdx uintptr) (r uintptr) { bp := tls.Alloc(96) defer tls.Free(96) var p, pResult uintptr var _ /* scan at bp+0 */ TWhereScan _, _ = p, pResult pResult = uintptr(0) p = _whereScanInit(tls, bp, pWC, iCur, iColumn, op, pIdx) op &= uint32(libc.Int32FromInt32(WO_EQ) | libc.Int32FromInt32(WO_IS)) for p != 0 { if (*TWhereTerm)(unsafe.Pointer(p)).FprereqRight¬Ready == uint64(0) { if (*TWhereTerm)(unsafe.Pointer(p)).FprereqRight == uint64(0) && uint32((*TWhereTerm)(unsafe.Pointer(p)).FeOperator)&op != uint32(0) { return p } if pResult == uintptr(0) { pResult = p } } p = _whereScanNext(tls, bp) } return pResult } // C documentation // // /* // ** This function searches pList for an entry that matches the iCol-th column // ** of index pIdx. // ** // ** If such an expression is found, its index in pList->a[] is returned. If // ** no expression is found, -1 is returned. // */ func _findIndexCol(tls *libc.TLS, pParse uintptr, pList uintptr, iBase int32, pIdx uintptr, iCol int32) (r int32) { var i int32 var p, pColl, zColl uintptr _, _, _, _ = i, p, pColl, zColl zColl = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(iCol)*4)) i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } p = _sqlite3ExprSkipCollateAndLikely(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr) if p != uintptr(0) && (int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_COLUMN) || int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_AGG_COLUMN)) && int32((*TExpr)(unsafe.Pointer(p)).FiColumn) == int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(iCol)*2))) && (*TExpr)(unsafe.Pointer(p)).FiTable == iBase { pColl = _sqlite3ExprNNCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr) if 0 == _sqlite3StrICmp(tls, (*TCollSeq)(unsafe.Pointer(pColl)).FzName, zColl) { return i } } goto _1 _1: ; i++ } return -int32(1) } // C documentation // // /* // ** Return TRUE if the iCol-th column of index pIdx is NOT NULL // */ func _indexColumnNotNull(tls *libc.TLS, pIdx uintptr, iCol int32) (r int32) { var j int32 _ = j _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) j = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(iCol)*2))) if j >= 0 { return int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol + uintptr(j)*12 + 4)) & 0xf >> 0)) } else { if j == -int32(1) { return int32(1) } else { _ = libc.Int32FromInt32(0) return 0 /* Assume an indexed expression can always yield a NULL */ } } return r } // C documentation // // /* // ** Return true if the DISTINCT expression-list passed as the third argument // ** is redundant. // ** // ** A DISTINCT list is redundant if any subset of the columns in the // ** DISTINCT list are collectively unique and individually non-null. // */ func _isDistinctRedundant(tls *libc.TLS, pParse uintptr, pTabList uintptr, pWC uintptr, pDistinct uintptr) (r int32) { var i, iBase int32 var p, pIdx, pTab uintptr _, _, _, _, _ = i, iBase, p, pIdx, pTab /* If there is more than one table or sub-select in the FROM clause of ** this query, then it will not be possible to show that the DISTINCT ** clause is redundant. */ if (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc != int32(1) { return 0 } iBase = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FiCursor pTab = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FpTab /* If any of the expressions is an IPK column on table iBase, then return ** true. Note: The (p->iTable==iBase) part of this test may be false if the ** current SELECT is a correlated sub-query. */ i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pDistinct)).FnExpr) { break } p = _sqlite3ExprSkipCollateAndLikely(tls, (*(*TExprList_item)(unsafe.Pointer(pDistinct + 8 + uintptr(i)*20))).FpExpr) if p == uintptr(0) { goto _1 } if int32((*TExpr)(unsafe.Pointer(p)).Fop) != int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(p)).Fop) != int32(TK_AGG_COLUMN) { goto _1 } if (*TExpr)(unsafe.Pointer(p)).FiTable == iBase && int32((*TExpr)(unsafe.Pointer(p)).FiColumn) < 0 { return int32(1) } goto _1 _1: ; i++ } /* Loop through all indices on the table, checking each to see if it makes ** the DISTINCT qualifier redundant. It does so if: ** ** 1. The index is itself UNIQUE, and ** ** 2. All of the columns in the index are either part of the pDistinct ** list, or else the WHERE clause contains a term of the form "col=X", ** where X is a constant value. The collation sequences of the ** comparison and select-list expressions must match those of the index. ** ** 3. All of those index columns for which the WHERE clause does not ** contain a "col=X" term are subject to a NOT NULL constraint. */ pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0) { break } if !(int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != libc.Int32FromInt32(OE_None)) { goto _2 } if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 { goto _2 } i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) { break } if uintptr(0) == _sqlite3WhereFindTerm(tls, pWC, iBase, i, ^libc.Uint64FromInt32(0), uint32(WO_EQ), pIdx) { if _findIndexCol(tls, pParse, pDistinct, iBase, pIdx, i) < 0 { break } if _indexColumnNotNull(tls, pIdx, i) == 0 { break } } goto _3 _3: ; i++ } if i == int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) { /* This index implies that the DISTINCT qualifier is redundant. */ return int32(1) } goto _2 _2: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } return 0 } // C documentation // // /* // ** Estimate the logarithm of the input value to base 2. // */ func _estLog(tls *libc.TLS, N TLogEst) (r TLogEst) { var v1 int32 _ = v1 if int32(N) <= int32(10) { v1 = 0 } else { v1 = int32(_sqlite3LogEst(tls, uint64(N))) - int32(33) } return int16(v1) } // C documentation // // /* // ** Convert OP_Column opcodes to OP_Copy in previously generated code. // ** // ** This routine runs over generated VDBE code and translates OP_Column // ** opcodes into OP_Copy when the table is being accessed via co-routine // ** instead of via table lookup. // ** // ** If the iAutoidxCur is not zero, then any OP_Rowid instructions on // ** cursor iTabCur are transformed into OP_Sequence opcode for the // ** iAutoidxCur cursor, in order to generate unique rowids for the // ** automatic index being generated. // */ func _translateColumnToCopy(tls *libc.TLS, pParse uintptr, iStart int32, iTabCur int32, iRegister int32, iAutoidxCur int32) { var iEnd int32 var pOp, v uintptr _, _, _ = iEnd, pOp, v v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe pOp = _sqlite3VdbeGetOp(tls, v, iStart) iEnd = _sqlite3VdbeCurrentAddr(tls, v) if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 { return } for { if !(iStart < iEnd) { break } if (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 != iTabCur { goto _1 } if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Column) { (*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode = uint8(OP_Copy) (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 + iRegister (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 = (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 = 0 (*TVdbeOp)(unsafe.Pointer(pOp)).Fp5 = uint16(2) /* Cause the MEM_Subtype flag to be cleared */ } else { if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Rowid) { (*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode = uint8(OP_Sequence) (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = iAutoidxCur } } goto _1 _1: ; iStart++ pOp += 20 } } /* ** Two routines for printing the content of an sqlite3_index_info ** structure. Used for testing and debugging only. If neither ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines ** are no-ops. */ // C documentation // // /* // ** We know that pSrc is an operand of an outer join. Return true if // ** pTerm is a constraint that is compatible with that join. // ** // ** pTerm must be EP_OuterON if pSrc is the right operand of an // ** outer join. pTerm can be either EP_OuterON or EP_InnerON if pSrc // ** is the left operand of a RIGHT join. // ** // ** See https://sqlite.org/forum/forumpost/206d99a16dd9212f // ** for an example of a WHERE clause constraints that may not be used on // ** the right table of a RIGHT JOIN because the constraint implies a // ** not-NULL condition on the left table of the RIGHT JOIN. // */ func _constraintCompatibleWithOuterJoin(tls *libc.TLS, pTerm uintptr, pSrc uintptr) (r int32) { _ = libc.Int32FromInt32(0) /* By caller */ if !((*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != libc.Uint32FromInt32(0)) || *(*int32)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr + 36)) != (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor { return 0 } if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_RIGHT)) != 0 && (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_InnerON)) != uint32(0) { return 0 } return int32(1) } // C documentation // // /* // ** Return TRUE if the WHERE clause term pTerm is of a form where it // ** could be used with an index to access pSrc, assuming an appropriate // ** index existed. // */ func _termCanDriveIndex(tls *libc.TLS, pTerm uintptr, pSrc uintptr, notReady TBitmask) (r int32) { var aff uint8 _ = aff if (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor != (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor { return 0 } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) == 0 { return 0 } _ = libc.Int32FromInt32(0) if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0 && !(_constraintCompatibleWithOuterJoin(tls, pTerm, pSrc) != 0) { return 0 /* See https://sqlite.org/forum/forumpost/51e6959f61 */ } if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight¬Ready != uint64(0) { return 0 } _ = libc.Int32FromInt32(0) if (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 24))).FleftColumn < 0 { return 0 } aff = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pSrc)).FpTab)).FaCol + uintptr((*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 24))).FleftColumn)*12))).Faffinity if !(_sqlite3IndexAffinityOk(tls, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr, aff) != 0) { return 0 } return int32(1) } // C documentation // // /* // ** Generate code to construct the Index object for an automatic index // ** and to set up the WhereLevel object pLevel so that the code generator // ** makes use of the automatic index. // */ func _constructAutomaticIndex(tls *libc.TLS, pParse uintptr, pWC uintptr, notReady TBitmask, pLevel uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var addrCounter, addrInit, addrTop, i, iCol, iCol1, iContinue, mxBitCol, n, nKeyCol, regBase, regRecord, regYield, v12, v14, v3, v5 int32 var cMask, cMask1, extraCols, idxCols TBitmask var pColl, pExpr, pIdx, pLoop, pPartial, pSrc, pTabList, pTable, pTerm, pWCEnd, pX, v, v13, v15, v9 uintptr var sentWarning, useBloomFilter Tu8 var v2, v8 uint64 var v4 Tu16 var _ /* zNotUsed at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrCounter, addrInit, addrTop, cMask, cMask1, extraCols, i, iCol, iCol1, iContinue, idxCols, mxBitCol, n, nKeyCol, pColl, pExpr, pIdx, pLoop, pPartial, pSrc, pTabList, pTable, pTerm, pWCEnd, pX, regBase, regRecord, regYield, sentWarning, useBloomFilter, v, v12, v13, v14, v15, v2, v3, v4, v5, v8, v9 /* Bitmap of additional columns */ sentWarning = uint8(0) /* True if a warning has been issued */ useBloomFilter = uint8(0) /* True to also add a Bloom filter */ pPartial = uintptr(0) /* Partial Index Expression */ iContinue = 0 /* The FROM clause term to get the next index */ addrCounter = 0 /* Array of registers where record is assembled */ /* Generate code to skip over the creation and initialization of the ** transient index on 2nd and subsequent iterations of the loop. */ v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe _ = libc.Int32FromInt32(0) addrInit = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once)) /* Count the number of columns that will be added to the index ** and used to match WHERE clause constraints */ nKeyCol = 0 pTabList = (*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpTabList pSrc = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*72 pTable = (*TSrcItem)(unsafe.Pointer(pSrc)).FpTab pWCEnd = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr((*TWhereClause)(unsafe.Pointer(pWC)).FnTerm)*48 pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop idxCols = uint64(0) pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa for { if !(pTerm < pWCEnd) { break } pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr /* Make the automatic index a partial index if there are terms in the ** WHERE clause (or the ON clause of a LEFT join) that constrain which ** rows of the target table (pSrc) that can be used. */ if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VIRTUAL) == 0 && _sqlite3ExprIsSingleTableConstraint(tls, pExpr, pTabList, int32((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)) != 0 { pPartial = _sqlite3ExprAnd(tls, pParse, pPartial, _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr, 0)) } if _termCanDriveIndex(tls, pTerm, pSrc, notReady) != 0 { _ = libc.Int32FromInt32(0) iCol = (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 24))).FleftColumn if iCol >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) { v2 = libc.Uint64FromInt32(1) << (int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1)) } else { v2 = libc.Uint64FromInt32(1) << iCol } cMask = v2 if !(sentWarning != 0) { Xsqlite3_log(tls, libc.Int32FromInt32(SQLITE_WARNING)|libc.Int32FromInt32(1)<= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) { v8 = libc.Uint64FromInt32(1) << (int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1)) } else { v8 = libc.Uint64FromInt32(1) << iCol1 } cMask1 = v8 if idxCols&cMask1 == uint64(0) { pX = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr idxCols |= cMask1 *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(n)*2)) = int16((*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 24))).FleftColumn) pColl = _sqlite3ExprCompareCollSeq(tls, pParse, pX) _ = libc.Int32FromInt32(0) /* TH3 collate01.800 */ if pColl != 0 { v9 = (*TCollSeq)(unsafe.Pointer(pColl)).FzName } else { v9 = uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) } *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(n)*4)) = v9 n++ if (*TExpr)(unsafe.Pointer(pX)).FpLeft != uintptr(0) && int32(_sqlite3ExprAffinity(tls, (*TExpr)(unsafe.Pointer(pX)).FpLeft)) != int32(SQLITE_AFF_TEXT) { /* TUNING: only use a Bloom filter on an automatic index ** if one or more key columns has the ability to hold numeric ** values, since strings all have the same hash in the Bloom ** filter implementation and hence a Bloom filter on a text column ** is not usually helpful. */ useBloomFilter = uint8(1) } } } goto _7 _7: ; pTerm += 48 } _ = libc.Int32FromInt32(0) /* Add additional columns needed to make the automatic index into ** a covering index */ i = 0 for { if !(i < mxBitCol) { break } if extraCols&(libc.Uint64FromInt32(1)<>5)) != 0 { regYield = (*TSrcItem)(unsafe.Pointer(pSrc)).FregReturn addrCounter = _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, 0) _sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), regYield, 0, (*TSrcItem)(unsafe.Pointer(pSrc)).FaddrFillSub) addrTop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), regYield) } else { addrTop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur) } if pPartial != 0 { iContinue = _sqlite3VdbeMakeLabel(tls, pParse) _sqlite3ExprIfFalse(tls, pParse, pPartial, iContinue, int32(SQLITE_JUMPIFNULL)) *(*Tu32)(unsafe.Pointer(pLoop + 40)) |= uint32(WHERE_PARTIALIDX) } regRecord = _sqlite3GetTempReg(tls, pParse) regBase = _sqlite3GenerateIndexKey(tls, pParse, pIdx, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, regRecord, 0, uintptr(0), uintptr(0), 0) if (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter != 0 { _sqlite3VdbeAddOp4Int(tls, v, int32(OP_FilterAdd), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter, 0, regBase, int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq)) } _sqlite3VdbeAddOp2(tls, v, int32(OP_IdxInsert), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur, regRecord) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_USESEEKRESULT)) if pPartial != 0 { _sqlite3VdbeResolveLabel(tls, v, iContinue) } if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 36 + 4))&0x20>>5)) != 0 { _sqlite3VdbeChangeP2(tls, v, addrCounter, regBase+n) _ = libc.Int32FromInt32(0) _translateColumnToCopy(tls, pParse, addrTop, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, (*TSrcItem)(unsafe.Pointer(pSrc)).FregResult, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur) _sqlite3VdbeGoto(tls, v, addrTop) libc.SetBitFieldPtr16Uint32(pSrc+36+4, libc.Uint32FromInt32(0), 5, 0x20) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, addrTop+int32(1)) _sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_STMTSTATUS_AUTOINDEX)) } _sqlite3VdbeJumpHere(tls, v, addrTop) _sqlite3ReleaseTempReg(tls, pParse, regRecord) /* Jump here when skipping the initialization */ _sqlite3VdbeJumpHere(tls, v, addrInit) goto end_auto_index_create end_auto_index_create: ; _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pPartial) } // C documentation // // /* // ** Generate bytecode that will initialize a Bloom filter that is appropriate // ** for pLevel. // ** // ** If there are inner loops within pLevel that have the WHERE_BLOOMFILTER // ** flag set, initialize a Bloomfilter for them as well. Except don't do // ** this recursive initialization if the SQLITE_BloomPulldown optimization has // ** been turned off. // ** // ** When the Bloom filter is initialized, the WHERE_BLOOMFILTER flag is cleared // ** from the loop, but the regFilter value is set to a register that implements // ** the Bloom filter. When regFilter is positive, the // ** sqlite3WhereCodeOneLoopStart() will generate code to test the Bloom filter // ** and skip the subsequence B-Tree seek if the Bloom filter indicates that // ** no matching rows exist. // ** // ** This routine may only be called if it has previously been determined that // ** the loop would benefit from a Bloom filter, and the WHERE_BLOOMFILTER bit // ** is set. // */ func _sqlite3ConstructBloomFilter(tls *libc.TLS, pWInfo uintptr, iLevel int32, pLevel uintptr, notReady TBitmask) { var addrCont, addrOnce, addrTop, iCur, iSrc, jj, n, r1, r11, v1, v5 int32 var pExpr, pIdx, pItem, pLoop, pParse, pTab, pTabItem, pTabList, pTerm, pWCEnd, saved_pIdxEpr, saved_pIdxPartExpr, v, v2 uintptr var sz Tu64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrCont, addrOnce, addrTop, iCur, iSrc, jj, n, pExpr, pIdx, pItem, pLoop, pParse, pTab, pTabItem, pTabList, pTerm, pWCEnd, r1, r11, saved_pIdxEpr, saved_pIdxPartExpr, sz, v, v1, v2, v5 /* Last WHERE clause term */ pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse /* Parsing context */ v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* VDBE under construction */ pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop /* saved copy of Parse.pIdxPartExpr */ saved_pIdxEpr = (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr saved_pIdxPartExpr = (*TParse)(unsafe.Pointer(pParse)).FpIdxPartExpr (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr = uintptr(0) (*TParse)(unsafe.Pointer(pParse)).FpIdxPartExpr = uintptr(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) addrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once)) for cond := true; cond; cond = iLevel < int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) { _sqlite3WhereExplainBloomFilter(tls, pParse, pWInfo, pLevel) addrCont = _sqlite3VdbeMakeLabel(tls, pParse) iCur = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur v2 = pParse + 44 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter = v1 /* The Bloom filter is a Blob held in a register. Initialize it ** to zero-filled blob of at least 80K bits, but maybe more if the ** estimated size of the table is larger. We could actually ** measure the size of the table at run-time using OP_Count with ** P3==1 and use that value to initialize the blob. But that makes ** testing complicated. By basing the blob size on the value in the ** sqlite_stat1 table, testing is much easier. */ pTabList = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList iSrc = int32((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom) pItem = pTabList + 8 + uintptr(iSrc)*72 _ = libc.Int32FromInt32(0) pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab _ = libc.Int32FromInt32(0) sz = _sqlite3LogEstToInt(tls, (*TTable)(unsafe.Pointer(pTab)).FnRowLogEst) if sz < uint64(10000) { sz = uint64(10000) } else { if sz > uint64(10000000) { sz = uint64(10000000) } } _sqlite3VdbeAddOp2(tls, v, int32(OP_Blob), int32(sz), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter) addrTop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), iCur) pWCEnd = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FsWC.Fa + uintptr((*TWhereInfo)(unsafe.Pointer(pWInfo)).FsWC.FnTerm)*48 pTerm = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FsWC.Fa for { if !(pTerm < pWCEnd) { break } pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VIRTUAL) == 0 && _sqlite3ExprIsSingleTableConstraint(tls, pExpr, pTabList, iSrc) != 0 { _sqlite3ExprIfFalse(tls, pParse, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr, addrCont, int32(SQLITE_JUMPIFNULL)) } goto _3 _3: ; pTerm += 48 } if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IPK) != 0 { r1 = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iCur, r1) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_FilterAdd), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter, 0, r1, int32(1)) _sqlite3ReleaseTempReg(tls, pParse, r1) } else { pIdx = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex n = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq) r11 = _sqlite3GetTempRange(tls, pParse, n) jj = 0 for { if !(jj < n) { break } _ = libc.Int32FromInt32(0) _sqlite3ExprCodeLoadIndexColumn(tls, pParse, pIdx, iCur, jj, r11+jj) goto _4 _4: ; jj++ } _sqlite3VdbeAddOp4Int(tls, v, int32(OP_FilterAdd), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter, 0, r11, n) _sqlite3ReleaseTempRange(tls, pParse, r11, n) } _sqlite3VdbeResolveLabel(tls, v, addrCont) _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, addrTop+int32(1)) _sqlite3VdbeJumpHere(tls, v, addrTop) *(*Tu32)(unsafe.Pointer(pLoop + 40)) &= uint32(^libc.Int32FromInt32(WHERE_BLOOMFILTER)) if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_BloomPulldown)) != uint32(0) { break } for { iLevel++ v5 = iLevel if !(v5 < int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)) { break } pLevel = pWInfo + 760 + uintptr(iLevel)*88 pTabItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*72 if int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)) != 0 { continue } pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop if pLoop == uintptr(0) { continue } if (*TWhereLoop)(unsafe.Pointer(pLoop)).Fprereq¬Ready != 0 { continue } if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_BLOOMFILTER)|libc.Int32FromInt32(WHERE_COLUMN_IN)) == uint32(WHERE_BLOOMFILTER) { /* This is a candidate for bloom-filter pull-down (early evaluation). ** The test that WHERE_COLUMN_IN is omitted is important, as we are ** not able to do early evaluation of bloom filters that make use of ** the IN operator */ break } } } _sqlite3VdbeJumpHere(tls, v, addrOnce) (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr = saved_pIdxEpr (*TParse)(unsafe.Pointer(pParse)).FpIdxPartExpr = saved_pIdxPartExpr } // C documentation // // /* // ** Allocate and populate an sqlite3_index_info structure. It is the // ** responsibility of the caller to eventually release the structure // ** by passing the pointer returned by this function to freeIndexInfo(). // */ func _allocateIndexInfo(tls *libc.TLS, pWInfo uintptr, pWC uintptr, mUnusable TBitmask, pSrc uintptr, pmNoOmit uintptr) (r uintptr) { var eDistinct, i, j, n, nOrderBy, nTerm, v12, v2, v9 int32 var mNoOmit, op Tu16 var pE2, pExpr, pExpr1, pHidden, pIdxCons, pIdxInfo, pIdxOrderBy, pOrderBy, pParse, pTab, pTerm, pUsage, zColl, v6, p3, p4 uintptr var v10 uint32 var v7 bool _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = eDistinct, i, j, mNoOmit, n, nOrderBy, nTerm, op, pE2, pExpr, pExpr1, pHidden, pIdxCons, pIdxInfo, pIdxOrderBy, pOrderBy, pParse, pTab, pTerm, pUsage, zColl, v10, v12, v2, v6, v7, v9, p3, p4 pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse mNoOmit = uint16(0) eDistinct = 0 pOrderBy = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy _ = libc.Int32FromInt32(0) pTab = (*TSrcItem)(unsafe.Pointer(pSrc)).FpTab _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Find all WHERE clause constraints referring to this virtual table. ** Mark each term with the TERM_OK flag. Set nTerm to the number of ** terms found. */ v2 = libc.Int32FromInt32(0) nTerm = v2 i = v2 pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa for { if !(i < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) { break } p3 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) & ^libc.Int32FromInt32(TERM_OK)) if (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor != (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor { goto _1 } if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight&mUnusable != 0 { goto _1 } _ = libc.Int32FromInt32(0) if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator) & ^libc.Int32FromInt32(WO_EQUIV) == 0 { goto _1 } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VNULL) != 0 { goto _1 } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0 && !(_constraintCompatibleWithOuterJoin(tls, pTerm, pSrc) != 0) { goto _1 } nTerm++ p4 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | libc.Int32FromInt32(TERM_OK)) goto _1 _1: ; i++ pTerm += 48 } /* If the ORDER BY clause contains only columns in the current ** virtual table then allocate space for the aOrderBy part of ** the sqlite3_index_info structure. */ nOrderBy = 0 if pOrderBy != 0 { n = (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr i = 0 for { if !(i < n) { break } pExpr = (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).FpExpr /* Skip over constant terms in the ORDER BY clause */ if _sqlite3ExprIsConstant(tls, pExpr) != 0 { goto _5 } /* Virtual tables are unable to deal with NULLS FIRST */ if int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_BIGNULL) != 0 { break } /* First case - a direct column references without a COLLATE operator */ if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor { _ = libc.Int32FromInt32(0) goto _5 } /* 2nd case - a column reference with a COLLATE operator. Only match ** of the COLLATE operator matches the collation of the column. */ if v7 = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLLATE); v7 { v6 = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft pE2 = v6 } if v7 && int32((*TExpr)(unsafe.Pointer(v6)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pE2)).FiTable == (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor { /* The collating sequence name */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TExpr)(unsafe.Pointer(pExpr)).FiColumn = (*TExpr)(unsafe.Pointer(pE2)).FiColumn if int32((*TExpr)(unsafe.Pointer(pE2)).FiColumn) < 0 { goto _5 } /* Collseq does not matter for rowid */ zColl = _sqlite3ColumnColl(tls, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr((*TExpr)(unsafe.Pointer(pE2)).FiColumn)*12) if zColl == uintptr(0) { zColl = uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) } if Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), zColl) == 0 { goto _5 } } /* No matches cause a break out of the loop */ break goto _5 _5: ; i++ } if i == n { nOrderBy = n if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_DISTINCTBY) != 0 { eDistinct = int32(2) + libc.BoolInt32(int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_SORTBYGROUP) != 0) } else { if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_GROUPBY) != 0 { eDistinct = int32(1) } } } } /* Allocate the sqlite3_index_info structure */ pIdxInfo = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(72)+(libc.Uint32FromInt64(12)+libc.Uint32FromInt64(8))*uint32(nTerm)+uint32(8)*uint32(nOrderBy)+uint32(24)+uint32(4)*uint32(nTerm))) if pIdxInfo == uintptr(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+1618, 0) return uintptr(0) } pHidden = pIdxInfo + 1*72 pIdxCons = pHidden + 20 + uintptr(nTerm)*4 pIdxOrderBy = pIdxCons + uintptr(nTerm)*12 pUsage = pIdxOrderBy + uintptr(nOrderBy)*8 (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint = pIdxCons (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy = pIdxOrderBy (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage = pUsage (*THiddenIndexInfo1)(unsafe.Pointer(pHidden)).FpWC = pWC (*THiddenIndexInfo1)(unsafe.Pointer(pHidden)).FpParse = pParse (*THiddenIndexInfo1)(unsafe.Pointer(pHidden)).FeDistinct = eDistinct (*THiddenIndexInfo1)(unsafe.Pointer(pHidden)).FmIn = uint32(0) v9 = libc.Int32FromInt32(0) j = v9 i = v9 pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa for { if !(i < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) { break } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_OK) == 0 { goto _8 } (*(*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons + uintptr(j)*12))).FiColumn = (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 24))).FleftColumn (*(*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons + uintptr(j)*12))).FiTermOffset = i op = uint16(int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator) & int32(WO_ALL)) if int32(op) == int32(WO_IN) { if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_SLICE) == 0 { if j <= int32(31) { v10 = libc.Uint32FromInt32(1) << j } else { v10 = uint32(0) } *(*Tu32)(unsafe.Pointer(pHidden + 12)) |= v10 } op = uint16(WO_EQ) } if int32(op) == int32(WO_AUX) { (*(*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons + uintptr(j)*12))).Fop = (*TWhereTerm)(unsafe.Pointer(pTerm)).FeMatchOp } else { if int32(op)&(libc.Int32FromInt32(WO_ISNULL)|libc.Int32FromInt32(WO_IS)) != 0 { if int32(op) == int32(WO_ISNULL) { (*(*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons + uintptr(j)*12))).Fop = uint8(SQLITE_INDEX_CONSTRAINT_ISNULL) } else { (*(*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons + uintptr(j)*12))).Fop = uint8(SQLITE_INDEX_CONSTRAINT_IS) } } else { (*(*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons + uintptr(j)*12))).Fop = uint8(op) /* The direct assignment in the previous line is possible only because ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The ** following asserts verify this fact. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(op)&(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ))) != 0 && _sqlite3ExprIsVector(tls, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpRight) != 0 { if j < int32(16) { mNoOmit = Tu16(int32(mNoOmit) | libc.Int32FromInt32(1)<idxStr if p->needToFreeIdxStr indicates // ** that this is required. // */ func _vtabBestIndex(tls *libc.TLS, pParse uintptr, pTab uintptr, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pVtab uintptr var rc int32 _, _ = pVtab, rc pVtab = (*TVTable)(unsafe.Pointer(_sqlite3GetVTable(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pTab))).FpVtab (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FnSchemaLock++ rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FpModule)).FxBestIndex})))(tls, pVtab, p) (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FnSchemaLock-- if rc != SQLITE_OK && rc != int32(SQLITE_CONSTRAINT) { if rc == int32(SQLITE_NOMEM) { _sqlite3OomFault(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb) } else { if !((*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+3795, libc.VaList(bp+8, _sqlite3ErrStr(tls, rc))) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+3795, libc.VaList(bp+8, (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg)) } } } if (*TVTable)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 44))).Fp)).FbAllSchemas != 0 { _sqlite3VtabUsesAllSchemas(tls, pParse) } Xsqlite3_free(tls, (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg) (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg = uintptr(0) return rc } // C documentation // // /* // ** Estimate the location of a particular key among all keys in an // ** index. Store the results in aStat as follows: // ** // ** aStat[0] Est. number of rows less than pRec // ** aStat[1] Est. number of rows equal to pRec // ** // ** Return the index of the sample that is the smallest sample that // ** is greater than or equal to pRec. Note that this index is not an index // ** into the aSample[] array - it is an index into a virtual set of samples // ** based on the contents of aSample[] and the number of fields in record // ** pRec. // */ func _whereKeyStats(tls *libc.TLS, pParse uintptr, pIdx uintptr, pRec uintptr, roundUp int32, aStat uintptr) (r int32) { var aSample uintptr var i, iCol, iMin, iSamp, iSample, iTest, n, nField, res, v1 int32 var iGap, iLower, iUpper TtRowcnt _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aSample, i, iCol, iGap, iLower, iMin, iSamp, iSample, iTest, iUpper, n, nField, res, v1 aSample = (*TIndex)(unsafe.Pointer(pIdx)).FaSample /* Smallest sample larger than or equal to pRec */ iMin = 0 /* Number of fields in pRec */ iLower = uint64(0) /* anLt[] + anEq[] of largest sample pRec is > */ _ = pParse _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Do a binary search to find the first sample greater than or equal ** to pRec. If pRec contains a single field, the set of samples to search ** is simply the aSample[] array. If the samples in aSample[] contain more ** than one fields, all fields following the first are ignored. ** ** If pRec contains N fields, where N is more than one, then as well as the ** samples in aSample[] (truncated to N fields), the search also has to ** consider prefixes of those samples. For example, if the set of samples ** in aSample is: ** ** aSample[0] = (a, 5) ** aSample[1] = (a, 10) ** aSample[2] = (b, 5) ** aSample[3] = (c, 100) ** aSample[4] = (c, 105) ** ** Then the search space should ideally be the samples above and the ** unique prefixes [a], [b] and [c]. But since that is hard to organize, ** the code actually searches this set: ** ** 0: (a) ** 1: (a, 5) ** 2: (a, 10) ** 3: (a, 10) ** 4: (b) ** 5: (b, 5) ** 6: (c) ** 7: (c, 100) ** 8: (c, 105) ** 9: (c, 105) ** ** For each sample in the aSample[] array, N samples are present in the ** effective sample array. In the above, samples 0 and 1 are based on ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc. ** ** Often, sample i of each block of N effective samples has (i+1) fields. ** Except, each sample may be extended to ensure that it is greater than or ** equal to the previous sample in the array. For example, in the above, ** sample 2 is the first sample of a block of N samples, so at first it ** appears that it should be 1 field in size. However, that would make it ** smaller than sample 1, so the binary search would not work. As a result, ** it is extended to two fields. The duplicates that this creates do not ** cause any problems. */ if !((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) { nField = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) } else { nField = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) } if int32((*TUnpackedRecord)(unsafe.Pointer(pRec)).FnField) < nField { v1 = int32((*TUnpackedRecord)(unsafe.Pointer(pRec)).FnField) } else { v1 = nField } nField = v1 iCol = 0 iSample = (*TIndex)(unsafe.Pointer(pIdx)).FnSample * nField for cond := true; cond; cond = res != 0 && iMin < iSample { /* Number of fields in test sample */ iTest = (iMin + iSample) / int32(2) iSamp = iTest / nField if iSamp > 0 { /* The proposed effective sample is a prefix of sample aSample[iSamp]. ** Specifically, the shortest prefix of at least (1 + iTest%nField) ** fields that is greater than the previous effective sample. */ n = iTest%nField + int32(1) for { if !(n < nField) { break } if *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(iSamp-int32(1))*20))).FanLt + uintptr(n-int32(1))*8)) != *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(iSamp)*20))).FanLt + uintptr(n-int32(1))*8)) { break } goto _2 _2: ; n++ } } else { n = iTest + int32(1) } (*TUnpackedRecord)(unsafe.Pointer(pRec)).FnField = uint16(n) res = _sqlite3VdbeRecordCompare(tls, (*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(iSamp)*20))).Fn, (*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(iSamp)*20))).Fp, pRec) if res < 0 { iLower = *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(iSamp)*20))).FanLt + uintptr(n-int32(1))*8)) + *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(iSamp)*20))).FanEq + uintptr(n-int32(1))*8)) iMin = iTest + int32(1) } else { if res == 0 && n < nField { iLower = *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(iSamp)*20))).FanLt + uintptr(n-int32(1))*8)) iMin = iTest + int32(1) res = -int32(1) } else { iSample = iTest iCol = n - int32(1) } } } i = iSample / nField if res == 0 { /* Record pRec is equal to sample i */ _ = libc.Int32FromInt32(0) *(*TtRowcnt)(unsafe.Pointer(aStat)) = *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(i)*20))).FanLt + uintptr(iCol)*8)) *(*TtRowcnt)(unsafe.Pointer(aStat + 1*8)) = *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(i)*20))).FanEq + uintptr(iCol)*8)) } else { if i >= (*TIndex)(unsafe.Pointer(pIdx)).FnSample { iUpper = (*TIndex)(unsafe.Pointer(pIdx)).FnRowEst0 } else { iUpper = *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(i)*20))).FanLt + uintptr(iCol)*8)) } if iLower >= iUpper { iGap = uint64(0) } else { iGap = iUpper - iLower } if roundUp != 0 { iGap = iGap * uint64(2) / uint64(3) } else { iGap = iGap / uint64(3) } *(*TtRowcnt)(unsafe.Pointer(aStat)) = iLower + iGap *(*TtRowcnt)(unsafe.Pointer(aStat + 1*8)) = *(*TtRowcnt)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaAvgEq + uintptr(nField-int32(1))*8)) } /* Restore the pRec->nField value before returning. */ (*TUnpackedRecord)(unsafe.Pointer(pRec)).FnField = uint16(nField) return i } // C documentation // // /* // ** If it is not NULL, pTerm is a term that provides an upper or lower // ** bound on a range scan. Without considering pTerm, it is estimated // ** that the scan will visit nNew rows. This function returns the number // ** estimated to be visited after taking pTerm into account. // ** // ** If the user explicitly specified a likelihood() value for this term, // ** then the return value is the likelihood multiplied by the number of // ** input rows. Otherwise, this function assumes that an "IS NOT NULL" term // ** has a likelihood of 0.50, and any other term a likelihood of 0.25. // */ func _whereRangeAdjust(tls *libc.TLS, pTerm uintptr, nNew TLogEst) (r TLogEst) { var nRet TLogEst _ = nRet nRet = nNew if pTerm != 0 { if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb) <= 0 { nRet = TLogEst(int32(nRet) + int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb)) } else { if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VNULL) == 0 { nRet = TLogEst(int32(nRet) - libc.Int32FromInt32(20)) _ = libc.Int32FromInt32(0) } } } return nRet } // C documentation // // /* // ** Return the affinity for a single column of an index. // */ func _sqlite3IndexColumnAffinity(tls *libc.TLS, db uintptr, pIdx uintptr, iCol int32) (r uint8) { _ = libc.Int32FromInt32(0) if !((*TIndex)(unsafe.Pointer(pIdx)).FzColAff != 0) { if _sqlite3IndexAffinityStr(tls, db, pIdx) == uintptr(0) { return uint8(SQLITE_AFF_BLOB) } } _ = libc.Int32FromInt32(0) return *(*uint8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FzColAff + uintptr(iCol))) } // C documentation // // /* // ** This function is called to estimate the number of rows visited by a // ** range-scan on a skip-scan index. For example: // ** // ** CREATE INDEX i1 ON t1(a, b, c); // ** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?; // ** // ** Value pLoop->nOut is currently set to the estimated number of rows // ** visited for scanning (a=? AND b=?). This function reduces that estimate // ** by some factor to account for the (c BETWEEN ? AND ?) expression based // ** on the stat4 data for the index. this scan will be performed multiple // ** times (once for each (a,b) combination that matches a=?) is dealt with // ** by the caller. // ** // ** It does this by scanning through all stat4 samples, comparing values // ** extracted from pLower and pUpper with the corresponding column in each // ** sample. If L and U are the number of samples found to be less than or // ** equal to the values extracted from pLower and pUpper respectively, and // ** N is the total number of samples, the pLoop->nOut value is adjusted // ** as follows: // ** // ** nOut = nOut * ( min(U - L, 1) / N ) // ** // ** If pLower is NULL, or a value cannot be extracted from the term, L is // ** set to zero. If pUpper is NULL, or a value cannot be extracted from it, // ** U is set to N. // ** // ** Normally, this function sets *pbDone to 1 before returning. However, // ** if no value can be extracted from either pLower or pUpper (and so the // ** estimate of the number of rows delivered remains unchanged), *pbDone // ** is left as is. // ** // ** If an error occurs, an SQLite error code is returned. Otherwise, // ** SQLITE_OK. // */ func _whereRangeSkipScanEst(tls *libc.TLS, pParse uintptr, pLower uintptr, pUpper uintptr, pLoop uintptr, pbDone uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var aff Tu8 var db, p, pColl, p3 uintptr var i, nAdjust, nDiff, nEq, nLower, nUpper, rc, res, res1, v1 int32 var _ /* p1 at bp+0 */ uintptr var _ /* p2 at bp+4 */ uintptr var _ /* pVal at bp+8 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aff, db, i, nAdjust, nDiff, nEq, nLower, nUpper, p, pColl, rc, res, res1, v1, p3 p = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex nEq = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq) db = (*TParse)(unsafe.Pointer(pParse)).Fdb nLower = -int32(1) nUpper = (*TIndex)(unsafe.Pointer(p)).FnSample + int32(1) rc = SQLITE_OK aff = _sqlite3IndexColumnAffinity(tls, db, p, nEq) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Value extracted from pLower */ *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) /* Value extracted from pUpper */ *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) /* Value extracted from record */ pColl = _sqlite3LocateCollSeq(tls, pParse, *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(p)).FazColl + uintptr(nEq)*4))) if pLower != 0 { rc = _sqlite3Stat4ValueFromExpr(tls, pParse, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pLower)).FpExpr)).FpRight, aff, bp) nLower = 0 } if pUpper != 0 && rc == SQLITE_OK { rc = _sqlite3Stat4ValueFromExpr(tls, pParse, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pUpper)).FpExpr)).FpRight, aff, bp+4) if *(*uintptr)(unsafe.Pointer(bp + 4)) != 0 { v1 = 0 } else { v1 = (*TIndex)(unsafe.Pointer(p)).FnSample } nUpper = v1 } if *(*uintptr)(unsafe.Pointer(bp)) != 0 || *(*uintptr)(unsafe.Pointer(bp + 4)) != 0 { i = 0 for { if !(rc == SQLITE_OK && i < (*TIndex)(unsafe.Pointer(p)).FnSample) { break } rc = _sqlite3Stat4Column(tls, db, (*(*TIndexSample)(unsafe.Pointer((*TIndex)(unsafe.Pointer(p)).FaSample + uintptr(i)*20))).Fp, (*(*TIndexSample)(unsafe.Pointer((*TIndex)(unsafe.Pointer(p)).FaSample + uintptr(i)*20))).Fn, nEq, bp+8) if rc == SQLITE_OK && *(*uintptr)(unsafe.Pointer(bp)) != 0 { res = _sqlite3MemCompare(tls, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 8)), pColl) if res >= 0 { nLower++ } } if rc == SQLITE_OK && *(*uintptr)(unsafe.Pointer(bp + 4)) != 0 { res1 = _sqlite3MemCompare(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), *(*uintptr)(unsafe.Pointer(bp + 8)), pColl) if res1 >= 0 { nUpper++ } } goto _2 _2: ; i++ } nDiff = nUpper - nLower if nDiff <= 0 { nDiff = int32(1) } /* If there is both an upper and lower bound specified, and the ** comparisons indicate that they are close together, use the fallback ** method (assume that the scan visits 1/64 of the rows) for estimating ** the number of rows visited. Otherwise, estimate the number of rows ** using the method described in the header comment for this function. */ if nDiff != int32(1) || pUpper == uintptr(0) || pLower == uintptr(0) { nAdjust = int32(_sqlite3LogEst(tls, uint64((*TIndex)(unsafe.Pointer(p)).FnSample))) - int32(_sqlite3LogEst(tls, uint64(nDiff))) p3 = pLoop + 22 *(*TLogEst)(unsafe.Pointer(p3)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p3))) - nAdjust) *(*int32)(unsafe.Pointer(pbDone)) = int32(1) } } else { _ = libc.Int32FromInt32(0) } _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer(bp))) _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) return rc } // C documentation // // /* // ** This function is used to estimate the number of rows that will be visited // ** by scanning an index for a range of values. The range may have an upper // ** bound, a lower bound, or both. The WHERE clause terms that set the upper // ** and lower bounds are represented by pLower and pUpper respectively. For // ** example, assuming that index p is on t1(a): // ** // ** ... FROM t1 WHERE a > ? AND a < ? ... // ** |_____| |_____| // ** | | // ** pLower pUpper // ** // ** If either of the upper or lower bound is not present, then NULL is passed in // ** place of the corresponding WhereTerm. // ** // ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index // ** column subject to the range constraint. Or, equivalently, the number of // ** equality constraints optimized by the proposed index scan. For example, // ** assuming index p is on t1(a, b), and the SQL query is: // ** // ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... // ** // ** then nEq is set to 1 (as the range restricted column, b, is the second // ** left-most column of the index). Or, if the query is: // ** // ** ... FROM t1 WHERE a > ? AND a < ? ... // ** // ** then nEq is set to 0. // ** // ** When this function is called, *pnOut is set to the sqlite3LogEst() of the // ** number of rows that the index scan is expected to visit without // ** considering the range constraints. If nEq is 0, then *pnOut is the number of // ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced) // ** to account for the range constraints pLower and pUpper. // ** // ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be // ** used, a single range inequality reduces the search space by a factor of 4. // ** and a pair of constraints (x>? AND x 0 && nEq < (*TIndex)(unsafe.Pointer(p)).FnSampleCol && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) == uint32(0) { if nEq == (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FnRecValid { *(*uintptr)(unsafe.Pointer(bp)) = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpRec nBtm = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnBtm) nTop = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnTop) /* Rows less than the upper bound */ iLwrIdx = -int32(2) /* aSample[] for the lower bound */ iUprIdx = -int32(1) /* aSample[] for the upper bound */ if *(*uintptr)(unsafe.Pointer(bp)) != 0 { (*TUnpackedRecord)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnField = uint16((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FnRecValid) } /* Determine iLower and iUpper using ($P) only. */ if nEq == 0 { iLower = uint64(0) iUpper = (*TIndex)(unsafe.Pointer(p)).FnRowEst0 } else { /* Note: this call could be optimized away - since the same values must ** have been requested when testing key $P in whereEqualScanEst(). */ _whereKeyStats(tls, pParse, p, *(*uintptr)(unsafe.Pointer(bp)), 0, bp+8) iLower = (*(*[2]TtRowcnt)(unsafe.Pointer(bp + 8)))[0] iUpper = (*(*[2]TtRowcnt)(unsafe.Pointer(bp + 8)))[0] + (*(*[2]TtRowcnt)(unsafe.Pointer(bp + 8)))[int32(1)] } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if *(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(p)).FaSortOrder + uintptr(nEq))) != 0 { /* The roles of pLower and pUpper are swapped for a DESC index */ t = pLower pLower = pUpper pUpper = t t1 = nBtm nBtm = nTop nTop = t1 } /* If possible, improve on the iLower estimate using ($P:$L). */ if pLower != 0 { /* Values extracted from pExpr */ pExpr = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pLower)).FpExpr)).FpRight rc = _sqlite3Stat4ProbeSetValue(tls, pParse, p, bp, pExpr, nBtm, nEq, bp+24) if rc == SQLITE_OK && *(*int32)(unsafe.Pointer(bp + 24)) != 0 { mask = uint16(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ))) if _sqlite3ExprVectorSize(tls, pExpr) > *(*int32)(unsafe.Pointer(bp + 24)) { mask = uint16(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ))) } iLwrIdx = _whereKeyStats(tls, pParse, p, *(*uintptr)(unsafe.Pointer(bp)), 0, bp+8) if int32((*TWhereTerm)(unsafe.Pointer(pLower)).FeOperator)&int32(mask) != 0 { v1 = (*(*[2]TtRowcnt)(unsafe.Pointer(bp + 8)))[int32(1)] } else { v1 = uint64(0) } iNew = (*(*[2]TtRowcnt)(unsafe.Pointer(bp + 8)))[0] + v1 if iNew > iLower { iLower = iNew } nOut-- pLower = uintptr(0) } } /* If possible, improve on the iUpper estimate using ($P:$U). */ if pUpper != 0 { /* Values extracted from pExpr */ pExpr1 = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pUpper)).FpExpr)).FpRight rc = _sqlite3Stat4ProbeSetValue(tls, pParse, p, bp, pExpr1, nTop, nEq, bp+28) if rc == SQLITE_OK && *(*int32)(unsafe.Pointer(bp + 28)) != 0 { mask1 = uint16(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ))) if _sqlite3ExprVectorSize(tls, pExpr1) > *(*int32)(unsafe.Pointer(bp + 28)) { mask1 = uint16(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ))) } iUprIdx = _whereKeyStats(tls, pParse, p, *(*uintptr)(unsafe.Pointer(bp)), int32(1), bp+8) if int32((*TWhereTerm)(unsafe.Pointer(pUpper)).FeOperator)&int32(mask1) != 0 { v2 = (*(*[2]TtRowcnt)(unsafe.Pointer(bp + 8)))[int32(1)] } else { v2 = uint64(0) } iNew1 = (*(*[2]TtRowcnt)(unsafe.Pointer(bp + 8)))[0] + v2 if iNew1 < iUpper { iUpper = iNew1 } nOut-- pUpper = uintptr(0) } } (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpRec = *(*uintptr)(unsafe.Pointer(bp)) if rc == SQLITE_OK { if iUpper > iLower { nNew = _sqlite3LogEst(tls, iUpper-iLower) /* TUNING: If both iUpper and iLower are derived from the same ** sample, then assume they are 4x more selective. This brings ** the estimated selectivity more in line with what it would be ** if estimated without the use of STAT4 tables. */ if iLwrIdx == iUprIdx { nNew = TLogEst(int32(nNew) - libc.Int32FromInt32(20)) } _ = libc.Int32FromInt32(0) } else { nNew = int16(10) _ = libc.Int32FromInt32(0) } if int32(nNew) < nOut { nOut = int32(nNew) } } } else { *(*int32)(unsafe.Pointer(bp + 32)) = 0 rc = _whereRangeSkipScanEst(tls, pParse, pLower, pUpper, pLoop, bp+32) if *(*int32)(unsafe.Pointer(bp + 32)) != 0 { return rc } } } _ = libc.Int32FromInt32(0) nNew = _whereRangeAdjust(tls, pLower, int16(nOut)) nNew = _whereRangeAdjust(tls, pUpper, nNew) /* TUNING: If there is both an upper and lower limit and neither limit ** has an application-defined likelihood(), assume the range is ** reduced by an additional 75%. This means that, by default, an open-ended ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to ** match 1/64 of the index. */ if pLower != 0 && int32((*TWhereTerm)(unsafe.Pointer(pLower)).FtruthProb) > 0 && pUpper != 0 && int32((*TWhereTerm)(unsafe.Pointer(pUpper)).FtruthProb) > 0 { nNew = TLogEst(int32(nNew) - libc.Int32FromInt32(20)) } nOut -= libc.BoolInt32(pLower != uintptr(0)) + libc.BoolInt32(pUpper != uintptr(0)) if int32(nNew) < int32(10) { nNew = int16(10) } if int32(nNew) < nOut { nOut = int32(nNew) } (*TWhereLoop)(unsafe.Pointer(pLoop)).FnOut = int16(nOut) return rc } // C documentation // // /* // ** Estimate the number of rows that will be returned based on // ** an equality constraint x=VALUE and where that VALUE occurs in // ** the histogram data. This only works when x is the left-most // ** column of an index and sqlite_stat4 histogram data is available // ** for that index. When pExpr==NULL that means the constraint is // ** "x IS NULL" instead of "x=VALUE". // ** // ** Write the estimated row count into *pnRow and return SQLITE_OK. // ** If unable to make an estimate, leave *pnRow unchanged and return // ** non-zero. // ** // ** This routine can fail if it is unable to load a collating sequence // ** required for string comparison, or if unable to allocate memory // ** for a UTF conversion required for comparison. The error is stored // ** in the pParse structure. // */ func _whereEqualScanEst(tls *libc.TLS, pParse uintptr, pBuilder uintptr, pExpr uintptr, pnRow uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var nEq, rc int32 var p uintptr var _ /* a at bp+8 */ [2]TtRowcnt var _ /* bOk at bp+24 */ int32 var _ /* pRec at bp+0 */ uintptr _, _, _ = nEq, p, rc p = (*TWhereLoop)(unsafe.Pointer((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew)).Fu.Fbtree.FpIndex nEq = int32((*TWhereLoop)(unsafe.Pointer((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew)).Fu.Fbtree.FnEq) *(*uintptr)(unsafe.Pointer(bp)) = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpRec _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If values are not available for all fields of the index to the left ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */ if (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FnRecValid < nEq-int32(1) { return int32(SQLITE_NOTFOUND) } /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue() ** below would return the same value. */ if nEq >= int32((*TIndex)(unsafe.Pointer(p)).FnColumn) { *(*TtRowcnt)(unsafe.Pointer(pnRow)) = uint64(1) return SQLITE_OK } rc = _sqlite3Stat4ProbeSetValue(tls, pParse, p, bp, pExpr, int32(1), nEq-int32(1), bp+24) (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpRec = *(*uintptr)(unsafe.Pointer(bp)) if rc != SQLITE_OK { return rc } if *(*int32)(unsafe.Pointer(bp + 24)) == 0 { return int32(SQLITE_NOTFOUND) } (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FnRecValid = nEq _whereKeyStats(tls, pParse, p, *(*uintptr)(unsafe.Pointer(bp)), 0, bp+8) *(*TtRowcnt)(unsafe.Pointer(pnRow)) = (*(*[2]TtRowcnt)(unsafe.Pointer(bp + 8)))[int32(1)] return rc } // C documentation // // /* // ** Estimate the number of rows that will be returned based on // ** an IN constraint where the right-hand side of the IN operator // ** is a list of values. Example: // ** // ** WHERE x IN (1,2,3,4) // ** // ** Write the estimated row count into *pnRow and return SQLITE_OK. // ** If unable to make an estimate, leave *pnRow unchanged and return // ** non-zero. // ** // ** This routine can fail if it is unable to load a collating sequence // ** required for string comparison, or if unable to allocate memory // ** for a UTF conversion required for comparison. The error is stored // ** in the pParse structure. // */ func _whereInScanEst(tls *libc.TLS, pParse uintptr, pBuilder uintptr, pList uintptr, pnRow uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, nRecValid, rc int32 var nRow0 Ti64 var nRowEst TtRowcnt var p uintptr var _ /* nEst at bp+0 */ TtRowcnt _, _, _, _, _, _ = i, nRecValid, nRow0, nRowEst, p, rc p = (*TWhereLoop)(unsafe.Pointer((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew)).Fu.Fbtree.FpIndex nRow0 = int64(_sqlite3LogEstToInt(tls, *(*TLogEst)(unsafe.Pointer((*TIndex)(unsafe.Pointer(p)).FaiRowLogEst)))) nRecValid = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FnRecValid rc = SQLITE_OK /* Number of rows for a single term */ nRowEst = uint64(0) /* Loop counter */ _ = libc.Int32FromInt32(0) i = 0 for { if !(rc == SQLITE_OK && i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } *(*TtRowcnt)(unsafe.Pointer(bp)) = uint64(nRow0) rc = _whereEqualScanEst(tls, pParse, pBuilder, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr, bp) nRowEst += *(*TtRowcnt)(unsafe.Pointer(bp)) (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FnRecValid = nRecValid goto _1 _1: ; i++ } if rc == SQLITE_OK { if nRowEst > uint64(nRow0) { nRowEst = uint64(nRow0) } *(*TtRowcnt)(unsafe.Pointer(pnRow)) = nRowEst } _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** Convert bulk memory into a valid WhereLoop that can be passed // ** to whereLoopClear harmlessly. // */ func _whereLoopInit(tls *libc.TLS, p uintptr) { (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm = p + 60 (*TWhereLoop)(unsafe.Pointer(p)).FnLTerm = uint16(0) (*TWhereLoop)(unsafe.Pointer(p)).FnLSlot = uint16(int32(libc.Uint32FromInt64(12) / libc.Uint32FromInt64(4))) (*TWhereLoop)(unsafe.Pointer(p)).FwsFlags = uint32(0) } // C documentation // // /* // ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. // */ func _whereLoopClearUnion(tls *libc.TLS, db uintptr, p uintptr) { if (*TWhereLoop)(unsafe.Pointer(p)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_VIRTUALTABLE)|libc.Int32FromInt32(WHERE_AUTO_INDEX)) != 0 { if (*TWhereLoop)(unsafe.Pointer(p)).FwsFlags&uint32(WHERE_VIRTUALTABLE) != uint32(0) && int32(Tu32(*(*uint8)(unsafe.Pointer(p + 24 + 4))&0x1>>0)) != 0 { Xsqlite3_free(tls, (*(*struct { FidxNum int32 F__ccgo4 uint8 FisOrdered Ti8 FomitMask Tu16 FidxStr uintptr FmHandleIn Tu32 })(unsafe.Pointer(p + 24))).FidxStr) libc.SetBitFieldPtr8Uint32(p+24+4, libc.Uint32FromInt32(0), 0, 0x1) (*(*struct { FidxNum int32 F__ccgo4 uint8 FisOrdered Ti8 FomitMask Tu16 FidxStr uintptr FmHandleIn Tu32 })(unsafe.Pointer(p + 24))).FidxStr = uintptr(0) } else { if (*TWhereLoop)(unsafe.Pointer(p)).FwsFlags&uint32(WHERE_AUTO_INDEX) != uint32(0) && (*TWhereLoop)(unsafe.Pointer(p)).Fu.Fbtree.FpIndex != uintptr(0) { _sqlite3DbFree(tls, db, (*TIndex)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(p)).Fu.Fbtree.FpIndex)).FzColAff) _sqlite3DbFreeNN(tls, db, (*TWhereLoop)(unsafe.Pointer(p)).Fu.Fbtree.FpIndex) (*TWhereLoop)(unsafe.Pointer(p)).Fu.Fbtree.FpIndex = uintptr(0) } } } } // C documentation // // /* // ** Deallocate internal memory used by a WhereLoop object. Leave the // ** object in an initialized state, as if it had been newly allocated. // */ func _whereLoopClear(tls *libc.TLS, db uintptr, p uintptr) { if (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm != p+60 { _sqlite3DbFreeNN(tls, db, (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm) (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm = p + 60 (*TWhereLoop)(unsafe.Pointer(p)).FnLSlot = uint16(int32(libc.Uint32FromInt64(12) / libc.Uint32FromInt64(4))) } _whereLoopClearUnion(tls, db, p) (*TWhereLoop)(unsafe.Pointer(p)).FnLTerm = uint16(0) (*TWhereLoop)(unsafe.Pointer(p)).FwsFlags = uint32(0) } // C documentation // // /* // ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. // */ func _whereLoopResize(tls *libc.TLS, db uintptr, p uintptr, n int32) (r int32) { var paNew uintptr _ = paNew if int32((*TWhereLoop)(unsafe.Pointer(p)).FnLSlot) >= n { return SQLITE_OK } n = (n + int32(7)) & ^libc.Int32FromInt32(7) paNew = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(4)*uint32(n))) if paNew == uintptr(0) { return int32(SQLITE_NOMEM) } libc.Xmemcpy(tls, paNew, (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm, uint32(4)*uint32((*TWhereLoop)(unsafe.Pointer(p)).FnLSlot)) if (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm != p+60 { _sqlite3DbFreeNN(tls, db, (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm) } (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm = paNew (*TWhereLoop)(unsafe.Pointer(p)).FnLSlot = uint16(n) return SQLITE_OK } // C documentation // // /* // ** Transfer content from the second pLoop into the first. // */ func _whereLoopXfer(tls *libc.TLS, db uintptr, pTo uintptr, pFrom uintptr) (r int32) { _whereLoopClearUnion(tls, db, pTo) if int32((*TWhereLoop)(unsafe.Pointer(pFrom)).FnLTerm) > int32((*TWhereLoop)(unsafe.Pointer(pTo)).FnLSlot) && _whereLoopResize(tls, db, pTo, int32((*TWhereLoop)(unsafe.Pointer(pFrom)).FnLTerm)) != 0 { libc.Xmemset(tls, pTo, 0, uint32(libc.UintptrFromInt32(0)+48)) return int32(SQLITE_NOMEM) } libc.Xmemcpy(tls, pTo, pFrom, uint32(libc.UintptrFromInt32(0)+48)) libc.Xmemcpy(tls, (*TWhereLoop)(unsafe.Pointer(pTo)).FaLTerm, (*TWhereLoop)(unsafe.Pointer(pFrom)).FaLTerm, uint32((*TWhereLoop)(unsafe.Pointer(pTo)).FnLTerm)*uint32(4)) if (*TWhereLoop)(unsafe.Pointer(pFrom)).FwsFlags&uint32(WHERE_VIRTUALTABLE) != 0 { libc.SetBitFieldPtr8Uint32(pFrom+24+4, libc.Uint32FromInt32(0), 0, 0x1) } else { if (*TWhereLoop)(unsafe.Pointer(pFrom)).FwsFlags&uint32(WHERE_AUTO_INDEX) != uint32(0) { (*TWhereLoop)(unsafe.Pointer(pFrom)).Fu.Fbtree.FpIndex = uintptr(0) } } return SQLITE_OK } // C documentation // // /* // ** Delete a WhereLoop object // */ func _whereLoopDelete(tls *libc.TLS, db uintptr, p uintptr) { _ = libc.Int32FromInt32(0) _whereLoopClear(tls, db, p) _sqlite3DbNNFreeNN(tls, db, p) } // C documentation // // /* // ** Free a WhereInfo structure // */ func _whereInfoFree(tls *libc.TLS, db uintptr, pWInfo uintptr) { var p, pNext uintptr _, _ = p, pNext _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3WhereClauseClear(tls, pWInfo+80) for (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops != 0 { p = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops = (*TWhereLoop)(unsafe.Pointer(p)).FpNextLoop _whereLoopDelete(tls, db, p) } for (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpMemToFree != 0 { pNext = (*TWhereMemBlock)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpMemToFree)).FpNext _sqlite3DbNNFreeNN(tls, db, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpMemToFree) (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpMemToFree = pNext } _sqlite3DbNNFreeNN(tls, db, pWInfo) } // C documentation // // /* // ** Return TRUE if X is a proper subset of Y but is of equal or less cost. // ** In other words, return true if all constraints of X are also part of Y // ** and Y has additional constraints that might speed the search that X lacks // ** but the cost of running X is not more than the cost of running Y. // ** // ** In other words, return true if the cost relationwship between X and Y // ** is inverted and needs to be adjusted. // ** // ** Case 1: // ** // ** (1a) X and Y use the same index. // ** (1b) X has fewer == terms than Y // ** (1c) Neither X nor Y use skip-scan // ** (1d) X does not have a a greater cost than Y // ** // ** Case 2: // ** // ** (2a) X has the same or lower cost, or returns the same or fewer rows, // ** than Y. // ** (2b) X uses fewer WHERE clause terms than Y // ** (2c) Every WHERE clause term used by X is also used by Y // ** (2d) X skips at least as many columns as Y // ** (2e) If X is a covering index, than Y is too // */ func _whereLoopCheaperProperSubset(tls *libc.TLS, pX uintptr, pY uintptr) (r int32) { var i, j int32 _, _ = i, j if int32((*TWhereLoop)(unsafe.Pointer(pX)).FrRun) > int32((*TWhereLoop)(unsafe.Pointer(pY)).FrRun) && int32((*TWhereLoop)(unsafe.Pointer(pX)).FnOut) > int32((*TWhereLoop)(unsafe.Pointer(pY)).FnOut) { return 0 } /* (1d) and (2a) */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TWhereLoop)(unsafe.Pointer(pX)).Fu.Fbtree.FnEq) < int32((*TWhereLoop)(unsafe.Pointer(pY)).Fu.Fbtree.FnEq) && (*TWhereLoop)(unsafe.Pointer(pX)).Fu.Fbtree.FpIndex == (*TWhereLoop)(unsafe.Pointer(pY)).Fu.Fbtree.FpIndex && int32((*TWhereLoop)(unsafe.Pointer(pX)).FnSkip) == 0 && int32((*TWhereLoop)(unsafe.Pointer(pY)).FnSkip) == 0 { return int32(1) /* Case 1 is true */ } if int32((*TWhereLoop)(unsafe.Pointer(pX)).FnLTerm)-int32((*TWhereLoop)(unsafe.Pointer(pX)).FnSkip) >= int32((*TWhereLoop)(unsafe.Pointer(pY)).FnLTerm)-int32((*TWhereLoop)(unsafe.Pointer(pY)).FnSkip) { return 0 /* (2b) */ } if int32((*TWhereLoop)(unsafe.Pointer(pY)).FnSkip) > int32((*TWhereLoop)(unsafe.Pointer(pX)).FnSkip) { return 0 } /* (2d) */ i = int32((*TWhereLoop)(unsafe.Pointer(pX)).FnLTerm) - int32(1) for { if !(i >= 0) { break } if *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pX)).FaLTerm + uintptr(i)*4)) == uintptr(0) { goto _1 } j = int32((*TWhereLoop)(unsafe.Pointer(pY)).FnLTerm) - int32(1) for { if !(j >= 0) { break } if *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pY)).FaLTerm + uintptr(j)*4)) == *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pX)).FaLTerm + uintptr(i)*4)) { break } goto _2 _2: ; j-- } if j < 0 { return 0 } /* (2c) */ goto _1 _1: ; i-- } if (*TWhereLoop)(unsafe.Pointer(pX)).FwsFlags&uint32(WHERE_IDX_ONLY) != uint32(0) && (*TWhereLoop)(unsafe.Pointer(pY)).FwsFlags&uint32(WHERE_IDX_ONLY) == uint32(0) { return 0 /* (2e) */ } return int32(1) /* Case 2 is true */ } // C documentation // // /* // ** Try to adjust the cost and number of output rows of WhereLoop pTemplate // ** upwards or downwards so that: // ** // ** (1) pTemplate costs less than any other WhereLoops that are a proper // ** subset of pTemplate // ** // ** (2) pTemplate costs more than any other WhereLoops for which pTemplate // ** is a proper subset. // ** // ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer // ** WHERE clause terms than Y and that every WHERE clause term used by X is // ** also used by Y. // */ func _whereLoopAdjustCost(tls *libc.TLS, p uintptr, pTemplate uintptr) { var v2, v3, v4, v5 int32 _, _, _, _ = v2, v3, v4, v5 if (*TWhereLoop)(unsafe.Pointer(pTemplate)).FwsFlags&uint32(WHERE_INDEXED) == uint32(0) { return } for { if !(p != 0) { break } if int32((*TWhereLoop)(unsafe.Pointer(p)).FiTab) != int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FiTab) { goto _1 } if (*TWhereLoop)(unsafe.Pointer(p)).FwsFlags&uint32(WHERE_INDEXED) == uint32(0) { goto _1 } if _whereLoopCheaperProperSubset(tls, p, pTemplate) != 0 { /* Adjust pTemplate cost downward so that it is cheaper than its ** subset p. */ if int32((*TWhereLoop)(unsafe.Pointer(p)).FrRun) < int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FrRun) { v2 = int32((*TWhereLoop)(unsafe.Pointer(p)).FrRun) } else { v2 = int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FrRun) } (*TWhereLoop)(unsafe.Pointer(pTemplate)).FrRun = int16(v2) if int32((*TWhereLoop)(unsafe.Pointer(p)).FnOut)-int32(1) < int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FnOut) { v3 = int32((*TWhereLoop)(unsafe.Pointer(p)).FnOut) - int32(1) } else { v3 = int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FnOut) } (*TWhereLoop)(unsafe.Pointer(pTemplate)).FnOut = int16(v3) } else { if _whereLoopCheaperProperSubset(tls, pTemplate, p) != 0 { /* Adjust pTemplate cost upward so that it is costlier than p since ** pTemplate is a proper subset of p */ if int32((*TWhereLoop)(unsafe.Pointer(p)).FrRun) > int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FrRun) { v4 = int32((*TWhereLoop)(unsafe.Pointer(p)).FrRun) } else { v4 = int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FrRun) } (*TWhereLoop)(unsafe.Pointer(pTemplate)).FrRun = int16(v4) if int32((*TWhereLoop)(unsafe.Pointer(p)).FnOut)+int32(1) > int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FnOut) { v5 = int32((*TWhereLoop)(unsafe.Pointer(p)).FnOut) + int32(1) } else { v5 = int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FnOut) } (*TWhereLoop)(unsafe.Pointer(pTemplate)).FnOut = int16(v5) } } goto _1 _1: ; p = (*TWhereLoop)(unsafe.Pointer(p)).FpNextLoop } } // C documentation // // /* // ** Search the list of WhereLoops in *ppPrev looking for one that can be // ** replaced by pTemplate. // ** // ** Return NULL if pTemplate does not belong on the WhereLoop list. // ** In other words if pTemplate ought to be dropped from further consideration. // ** // ** If pX is a WhereLoop that pTemplate can replace, then return the // ** link that points to pX. // ** // ** If pTemplate cannot replace any existing element of the list but needs // ** to be added to the list as a new entry, then return a pointer to the // ** tail of the list. // */ func _whereLoopFindLesser(tls *libc.TLS, ppPrev uintptr, pTemplate uintptr) (r uintptr) { var p uintptr _ = p p = *(*uintptr)(unsafe.Pointer(ppPrev)) for { if !(p != 0) { break } if int32((*TWhereLoop)(unsafe.Pointer(p)).FiTab) != int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FiTab) || int32((*TWhereLoop)(unsafe.Pointer(p)).FiSortIdx) != int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FiSortIdx) { /* If either the iTab or iSortIdx values for two WhereLoop are different ** then those WhereLoops need to be considered separately. Neither is ** a candidate to replace the other. */ goto _1 } /* In the current implementation, the rSetup value is either zero ** or the cost of building an automatic index (NlogN) and the NlogN ** is the same for compatible WhereLoops. */ _ = libc.Int32FromInt32(0) /* whereLoopAddBtree() always generates and inserts the automatic index ** case first. Hence compatible candidate WhereLoops never have a larger ** rSetup. Call this SETUP-INVARIANT */ _ = libc.Int32FromInt32(0) /* Any loop using an application-defined index (or PRIMARY KEY or ** UNIQUE constraint) with one or more == constraints is better ** than an automatic index. Unless it is a skip-scan. */ if (*TWhereLoop)(unsafe.Pointer(p)).FwsFlags&uint32(WHERE_AUTO_INDEX) != uint32(0) && int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FnSkip) == 0 && (*TWhereLoop)(unsafe.Pointer(pTemplate)).FwsFlags&uint32(WHERE_INDEXED) != uint32(0) && (*TWhereLoop)(unsafe.Pointer(pTemplate)).FwsFlags&uint32(WHERE_COLUMN_EQ) != uint32(0) && (*TWhereLoop)(unsafe.Pointer(p)).Fprereq&(*TWhereLoop)(unsafe.Pointer(pTemplate)).Fprereq == (*TWhereLoop)(unsafe.Pointer(pTemplate)).Fprereq { break } /* If existing WhereLoop p is better than pTemplate, pTemplate can be ** discarded. WhereLoop p is better if: ** (1) p has no more dependencies than pTemplate, and ** (2) p has an equal or lower cost than pTemplate */ if (*TWhereLoop)(unsafe.Pointer(p)).Fprereq&(*TWhereLoop)(unsafe.Pointer(pTemplate)).Fprereq == (*TWhereLoop)(unsafe.Pointer(p)).Fprereq && int32((*TWhereLoop)(unsafe.Pointer(p)).FrSetup) <= int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FrSetup) && int32((*TWhereLoop)(unsafe.Pointer(p)).FrRun) <= int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FrRun) && int32((*TWhereLoop)(unsafe.Pointer(p)).FnOut) <= int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FnOut) { return uintptr(0) /* Discard pTemplate */ } /* If pTemplate is always better than p, then cause p to be overwritten ** with pTemplate. pTemplate is better than p if: ** (1) pTemplate has no more dependencies than p, and ** (2) pTemplate has an equal or lower cost than p. */ if (*TWhereLoop)(unsafe.Pointer(p)).Fprereq&(*TWhereLoop)(unsafe.Pointer(pTemplate)).Fprereq == (*TWhereLoop)(unsafe.Pointer(pTemplate)).Fprereq && int32((*TWhereLoop)(unsafe.Pointer(p)).FrRun) >= int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FrRun) && int32((*TWhereLoop)(unsafe.Pointer(p)).FnOut) >= int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FnOut) { _ = libc.Int32FromInt32(0) /* SETUP-INVARIANT above */ break /* Cause p to be overwritten by pTemplate */ } goto _1 _1: ; ppPrev = p + 56 p = *(*uintptr)(unsafe.Pointer(ppPrev)) } return ppPrev } // C documentation // // /* // ** Insert or replace a WhereLoop entry using the template supplied. // ** // ** An existing WhereLoop entry might be overwritten if the new template // ** is better and has fewer dependencies. Or the template will be ignored // ** and no insert will occur if an existing WhereLoop is faster and has // ** fewer dependencies than the template. Otherwise a new WhereLoop is // ** added based on the template. // ** // ** If pBuilder->pOrSet is not NULL then we care about only the // ** prerequisites and rRun and nOut costs of the N best loops. That // ** information is gathered in the pBuilder->pOrSet object. This special // ** processing mode is used only for OR clause processing. // ** // ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we // ** still might overwrite similar loops with the new template if the // ** new template is better. Loops may be overwritten if the following // ** conditions are met: // ** // ** (1) They have the same iTab. // ** (2) They have the same iSortIdx. // ** (3) The template has same or fewer dependencies than the current loop // ** (4) The template has the same or lower cost than the current loop // */ func _whereLoopInsert(tls *libc.TLS, pBuilder uintptr, pTemplate uintptr) (r int32) { var db, p, pIndex, pToDel, pWInfo, ppPrev, ppTail, v1 uintptr var rc int32 _, _, _, _, _, _, _, _, _ = db, p, pIndex, pToDel, pWInfo, ppPrev, ppTail, rc, v1 pWInfo = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo db = (*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse)).Fdb /* Stop the search once we hit the query planner search limit */ if (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FiPlanLimit == uint32(0) { if (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpOrSet != 0 { (*TWhereOrSet)(unsafe.Pointer((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpOrSet)).Fn = uint16(0) } return int32(SQLITE_DONE) } (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FiPlanLimit-- _whereLoopAdjustCost(tls, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops, pTemplate) /* If pBuilder->pOrSet is defined, then only keep track of the costs ** and prereqs. */ if (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpOrSet != uintptr(0) { if (*TWhereLoop)(unsafe.Pointer(pTemplate)).FnLTerm != 0 { _whereOrInsert(tls, (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpOrSet, (*TWhereLoop)(unsafe.Pointer(pTemplate)).Fprereq, (*TWhereLoop)(unsafe.Pointer(pTemplate)).FrRun, (*TWhereLoop)(unsafe.Pointer(pTemplate)).FnOut) } return SQLITE_OK } /* Look for an existing WhereLoop to replace with pTemplate */ ppPrev = _whereLoopFindLesser(tls, pWInfo+60, pTemplate) if ppPrev == uintptr(0) { /* There already exists a WhereLoop on the list that is better ** than pTemplate, so just ignore pTemplate */ return SQLITE_OK } else { p = *(*uintptr)(unsafe.Pointer(ppPrev)) } /* If we reach this point it means that either p[] should be overwritten ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new ** WhereLoop and insert it. */ if p == uintptr(0) { /* Allocate a new WhereLoop to add to the end of the list */ v1 = _sqlite3DbMallocRawNN(tls, db, uint64(72)) p = v1 *(*uintptr)(unsafe.Pointer(ppPrev)) = v1 if p == uintptr(0) { return int32(SQLITE_NOMEM) } _whereLoopInit(tls, p) (*TWhereLoop)(unsafe.Pointer(p)).FpNextLoop = uintptr(0) } else { /* We will be overwriting WhereLoop p[]. But before we do, first ** go through the rest of the list and delete any other entries besides ** p[] that are also supplanted by pTemplate */ ppTail = p + 56 for *(*uintptr)(unsafe.Pointer(ppTail)) != 0 { ppTail = _whereLoopFindLesser(tls, ppTail, pTemplate) if ppTail == uintptr(0) { break } pToDel = *(*uintptr)(unsafe.Pointer(ppTail)) if pToDel == uintptr(0) { break } *(*uintptr)(unsafe.Pointer(ppTail)) = (*TWhereLoop)(unsafe.Pointer(pToDel)).FpNextLoop _whereLoopDelete(tls, db, pToDel) } } rc = _whereLoopXfer(tls, db, p, pTemplate) if (*TWhereLoop)(unsafe.Pointer(p)).FwsFlags&uint32(WHERE_VIRTUALTABLE) == uint32(0) { pIndex = (*TWhereLoop)(unsafe.Pointer(p)).Fu.Fbtree.FpIndex if pIndex != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pIndex + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_IPK) { (*TWhereLoop)(unsafe.Pointer(p)).Fu.Fbtree.FpIndex = uintptr(0) } } return rc } // C documentation // // /* // ** Adjust the WhereLoop.nOut value downward to account for terms of the // ** WHERE clause that reference the loop but which are not used by an // ** index. // * // ** For every WHERE clause term that is not used by the index // ** and which has a truth probability assigned by one of the likelihood(), // ** likely(), or unlikely() SQL functions, reduce the estimated number // ** of output rows by the probability specified. // ** // ** TUNING: For every WHERE clause term that is not used by the index // ** and which does not have an assigned truth probability, heuristics // ** described below are used to try to estimate the truth probability. // ** TODO --> Perhaps this is something that could be improved by better // ** table statistics. // ** // ** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75% // ** value corresponds to -1 in LogEst notation, so this means decrement // ** the WhereLoop.nOut field for every such WHERE clause term. // ** // ** Heuristic 2: If there exists one or more WHERE clause terms of the // ** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the // ** final output row estimate is no greater than 1/4 of the total number // ** of rows in the table. In other words, assume that x==EXPR will filter // ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the // ** "x" column is boolean or else -1 or 0 or 1 is a common default value // ** on the "x" column and so in that case only cap the output row estimate // ** at 1/2 instead of 1/4. // */ func _whereLoopOutputAdjust(tls *libc.TLS, pWC uintptr, pLoop uintptr, nRow TLogEst) { bp := tls.Alloc(16) defer tls.Free(16) var i, j int32 var iReduce TLogEst var notAllowed TBitmask var pRight, pTerm, pX, p3, p4 uintptr var _ /* k at bp+0 */ int32 _, _, _, _, _, _, _, _, _ = i, iReduce, j, notAllowed, pRight, pTerm, pX, p3, p4 notAllowed = ^((*TWhereLoop)(unsafe.Pointer(pLoop)).Fprereq | (*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf) iReduce = 0 /* pLoop->nOut should not exceed nRow-iReduce */ _ = libc.Int32FromInt32(0) i = (*TWhereClause)(unsafe.Pointer(pWC)).FnBase pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa for { if !(i > 0) { break } _ = libc.Int32FromInt32(0) if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll¬Allowed != uint64(0) { goto _1 } if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll&(*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf == uint64(0) { goto _1 } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VIRTUAL) != 0 { goto _1 } j = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm) - int32(1) for { if !(j >= 0) { break } pX = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4)) if pX == uintptr(0) { goto _2 } if pX == pTerm { break } if (*TWhereTerm)(unsafe.Pointer(pX)).FiParent >= 0 && (*TWhereClause)(unsafe.Pointer(pWC)).Fa+uintptr((*TWhereTerm)(unsafe.Pointer(pX)).FiParent)*48 == pTerm { break } goto _2 _2: ; j-- } if j < 0 { _sqlite3ProgressCheck(tls, (*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpParse) if (*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf == (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll { /* If there are extra terms in the WHERE clause not used by an index ** that depend only on the table being scanned, and that will tend to ** cause many rows to be omitted, then mark that table as ** "self-culling". ** ** 2022-03-24: Self-culling only applies if either the extra terms ** are straight comparison operators that are non-true with NULL ** operand, or if the loop is not an OUTER JOIN. */ if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(0x3f) != 0 || int32((*(*TSrcItem)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pLoop)).FiTab)*72))).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)) == 0 { *(*Tu32)(unsafe.Pointer(pLoop + 40)) |= uint32(WHERE_SELFCULL) } } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb) <= 0 { /* If a truth probability is specified using the likelihood() hints, ** then use the probability provided by the application. */ p3 = pLoop + 22 *(*TLogEst)(unsafe.Pointer(p3)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p3))) + int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb)) } else { /* In the absence of explicit truth probabilities, use heuristics to ** guess a reasonable truth probability. */ (*TWhereLoop)(unsafe.Pointer(pLoop)).FnOut-- if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) != 0 && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_HIGHTRUTH) == 0 { pRight = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpRight *(*int32)(unsafe.Pointer(bp)) = 0 if _sqlite3ExprIsInteger(tls, pRight, bp) != 0 && *(*int32)(unsafe.Pointer(bp)) >= -int32(1) && *(*int32)(unsafe.Pointer(bp)) <= int32(1) { *(*int32)(unsafe.Pointer(bp)) = int32(10) } else { *(*int32)(unsafe.Pointer(bp)) = int32(20) } if int32(iReduce) < *(*int32)(unsafe.Pointer(bp)) { p4 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | libc.Int32FromInt32(TERM_HEURTRUTH)) iReduce = int16(*(*int32)(unsafe.Pointer(bp))) } } } } goto _1 _1: ; i-- pTerm += 48 } if int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnOut) > int32(nRow)-int32(iReduce) { (*TWhereLoop)(unsafe.Pointer(pLoop)).FnOut = int16(int32(nRow) - int32(iReduce)) } } // C documentation // // /* // ** Term pTerm is a vector range comparison operation. The first comparison // ** in the vector can be optimized using column nEq of the index. This // ** function returns the total number of vector elements that can be used // ** as part of the range comparison. // ** // ** For example, if the query is: // ** // ** WHERE a = ? AND (b, c, d) > (?, ?, ?) // ** // ** and the index: // ** // ** CREATE INDEX ... ON (a, b, c, d, e) // ** // ** then this function would be invoked with nEq=1. The value returned in // ** this case is 3. // */ func _whereRangeVectorLen(tls *libc.TLS, pParse uintptr, iCur int32, pIdx uintptr, nEq int32, pTerm uintptr) (r int32) { var aff, idxaff uint8 var i, nCmp, v1 int32 var pColl, pLhs, pRhs uintptr _, _, _, _, _, _, _, _ = aff, i, idxaff, nCmp, pColl, pLhs, pRhs, v1 nCmp = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpLeft) if nCmp < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)-nEq { v1 = nCmp } else { v1 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) - nEq } nCmp = v1 i = int32(1) for { if !(i < nCmp) { break } /* Comparison affinity */ idxaff = uint8(0) _ = libc.Int32FromInt32(0) pLhs = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpLeft + 20)) + 8 + uintptr(i)*20))).FpExpr pRhs = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpRight if (*TExpr)(unsafe.Pointer(pRhs)).Fflags&uint32(EP_xIsSelect) != uint32(0) { pRhs = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pRhs + 20)))).FpEList + 8 + uintptr(i)*20))).FpExpr } else { pRhs = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pRhs + 20)) + 8 + uintptr(i)*20))).FpExpr } /* Check that the LHS of the comparison is a column reference to ** the right column of the right source table. And that the sort ** order of the index column is the same as the sort order of the ** leftmost index column. */ if int32((*TExpr)(unsafe.Pointer(pLhs)).Fop) != int32(TK_COLUMN) || (*TExpr)(unsafe.Pointer(pLhs)).FiTable != iCur || int32((*TExpr)(unsafe.Pointer(pLhs)).FiColumn) != int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i+nEq)*2))) || int32(*(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder + uintptr(i+nEq)))) != int32(*(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder + uintptr(nEq)))) { break } aff = _sqlite3CompareAffinity(tls, pRhs, _sqlite3ExprAffinity(tls, pLhs)) idxaff = _sqlite3TableColumnAffinity(tls, (*TIndex)(unsafe.Pointer(pIdx)).FpTable, int32((*TExpr)(unsafe.Pointer(pLhs)).FiColumn)) if int32(aff) != int32(idxaff) { break } pColl = _sqlite3BinaryCompareCollSeq(tls, pParse, pLhs, pRhs) if pColl == uintptr(0) { break } if _sqlite3StrICmp(tls, (*TCollSeq)(unsafe.Pointer(pColl)).FzName, *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(i+nEq)*4))) != 0 { break } goto _2 _2: ; i++ } return i } /* ** Adjust the cost C by the costMult factor T. This only occurs if ** compiled with -DSQLITE_ENABLE_COSTMULT */ // C documentation // // /* // ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the // ** index pIndex. Try to match one more. // ** // ** When this function is called, pBuilder->pNew->nOut contains the // ** number of rows expected to be visited by filtering using the nEq // ** terms only. If it is modified, this value is restored before this // ** function returns. // ** // ** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is // ** a fake index used for the INTEGER PRIMARY KEY. // */ func _whereLoopAddBtreeIndex(tls *libc.TLS, pBuilder uintptr, pSrc uintptr, pProbe uintptr, nInMul TLogEst) (r int32) { bp := tls.Alloc(112) defer tls.Free(112) var M, logK, nIter, nOutUnadjusted, rCostIdx, rLogSize, rSize, saved_nOut, x TLogEst var db, pBtm, pExpr, pExpr1, pNew, pParse, pTerm, pTop, pWInfo, v11, v24, v5, v8, v9, p12, p13, p14, p15, p16, p17, p18, p19, p2, p20, p25, p3 uintptr var eOp, saved_nBtm, saved_nEq, saved_nLTerm, saved_nSkip, saved_nTop, v10, v23, v4, v7 Tu16 var i, iCol, nEq, nIn, nRecValid, nVecLen, opMask, rc, v21 int32 var saved_prereq TBitmask var saved_wsFlags Tu32 var v22 bool var _ /* nOut at bp+96 */ TtRowcnt var _ /* scan at bp+0 */ TWhereScan _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = M, db, eOp, i, iCol, logK, nEq, nIn, nIter, nOutUnadjusted, nRecValid, nVecLen, opMask, pBtm, pExpr, pExpr1, pNew, pParse, pTerm, pTop, pWInfo, rCostIdx, rLogSize, rSize, rc, saved_nBtm, saved_nEq, saved_nLTerm, saved_nOut, saved_nSkip, saved_nTop, saved_prereq, saved_wsFlags, x, v10, v11, v21, v22, v23, v24, v4, v5, v7, v8, v9, p12, p13, p14, p15, p16, p17, p18, p19, p2, p20, p25, p3 pWInfo = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo /* WHERE analyze context */ pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse /* Parsing context */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Original value of pNew->nOut */ rc = SQLITE_OK /* Logarithm of table size */ pTop = uintptr(0) pBtm = uintptr(0) /* Top and bottom range constraints */ pNew = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return (*TParse)(unsafe.Pointer(pParse)).Frc } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags&uint32(WHERE_BTM_LIMIT) != 0 { opMask = libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ)) } else { _ = libc.Int32FromInt32(0) opMask = libc.Int32FromInt32(WO_EQ) | libc.Int32FromInt32(WO_IN) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_ISNULL) | libc.Int32FromInt32(WO_IS) } if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x4>>2)) != 0 || int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x100>>8)) != 0 { if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x4>>2)) != 0 { opMask &= ^(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ))) } if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x100>>8)) != 0 { opMask &= ^(libc.Int32FromInt32(WO_EQ) | libc.Int32FromInt32(WO_IN) | libc.Int32FromInt32(WO_IS)) } } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) saved_nEq = (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnEq saved_nBtm = (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnBtm saved_nTop = (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnTop saved_nSkip = (*TWhereLoop)(unsafe.Pointer(pNew)).FnSkip saved_nLTerm = (*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm saved_wsFlags = (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags saved_prereq = (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq saved_nOut = (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut pTerm = _whereScanInit(tls, bp, (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC, (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor, int32(saved_nEq), uint32(opMask), pProbe) (*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup = 0 rSize = *(*TLogEst)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst)) rLogSize = _estLog(tls, rSize) for { if !(rc == SQLITE_OK && pTerm != uintptr(0)) { break } eOp = (*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator /* nOut before IN() and WHERE adjustments */ nIn = 0 nRecValid = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FnRecValid if (int32(eOp) == int32(WO_ISNULL) || int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VNULL) != 0) && _indexColumnNotNull(tls, pProbe, int32(saved_nEq)) != 0 { goto _1 /* ignore IS [NOT] NULL constraints on NOT NULL columns */ } if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight&(*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf != 0 { goto _1 } /* Do not allow the upper bound of a LIKE optimization range constraint ** to mix with a lower range bound from some other source */ if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_LIKEOPT) != 0 && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator) == libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ)) { goto _1 } if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0 && !(_constraintCompatibleWithOuterJoin(tls, pTerm, pSrc) != 0) { goto _1 } if int32((*TIndex)(unsafe.Pointer(pProbe)).FonError) != OE_None && int32(saved_nEq) == int32((*TIndex)(unsafe.Pointer(pProbe)).FnKeyCol)-int32(1) { p2 = pBuilder + 24 *(*uint8)(unsafe.Pointer(p2)) = uint8(int32(*(*uint8)(unsafe.Pointer(p2))) | libc.Int32FromInt32(SQLITE_BLDF1_UNIQUE)) } else { p3 = pBuilder + 24 *(*uint8)(unsafe.Pointer(p3)) = uint8(int32(*(*uint8)(unsafe.Pointer(p3))) | libc.Int32FromInt32(SQLITE_BLDF1_INDEXED)) } (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = saved_wsFlags (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnEq = saved_nEq (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnBtm = saved_nBtm (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnTop = saved_nTop (*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm = saved_nLTerm if int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm) >= int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLSlot) && _whereLoopResize(tls, db, pNew, int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm)+int32(1)) != 0 { break /* OOM while trying to enlarge the pNew->aLTerm array */ } v5 = pNew + 44 v4 = *(*Tu16)(unsafe.Pointer(v5)) *(*Tu16)(unsafe.Pointer(v5))++ *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(v4)*4)) = pTerm (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq = (saved_prereq | (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight) & ^(*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf _ = libc.Int32FromInt32(0) if int32(eOp)&int32(WO_IN) != 0 { pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) { nIn = int32(46) _ = libc.Int32FromInt32(0) /* The expression may actually be of the form (x, y) IN (SELECT...). ** In this case there is a separate term for each of (x) and (y). ** However, the nIn multiplier should only be applied once, not once ** for each such term. The following loop checks that pTerm is the ** first such term in use, and sets nIn back to 0 if it is not. */ i = 0 for { if !(i < int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm)-int32(1)) { break } if *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(i)*4)) != 0 && (*TWhereTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(i)*4)))).FpExpr == pExpr { nIn = 0 } goto _6 _6: ; i++ } } else { if *(*uintptr)(unsafe.Pointer(pExpr + 20)) != 0 && (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr != 0 { /* "x IN (value, value, ...)" */ nIn = int32(_sqlite3LogEst(tls, uint64((*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr))) } } if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x80>>7)) != 0 && int32(rLogSize) >= int32(10) { /* Let: ** N = the total number of rows in the table ** K = the number of entries on the RHS of the IN operator ** M = the number of rows in the table that match terms to the ** to the left in the same index. If the IN operator is on ** the left-most index column, M==N. ** ** Given the definitions above, it is better to omit the IN operator ** from the index lookup and instead do a scan of the M elements, ** testing each scanned row against the IN operator separately, if: ** ** M*log(K) < K*log(N) ** ** Our estimates for M, K, and N might be inaccurate, so we build in ** a safety margin of 2 (LogEst: 10) that favors using the IN operator ** with the index, as using an index has better worst-case behavior. ** If we do not have real sqlite_stat1 data, always prefer to use ** the index. Do not bother with this optimization on very small ** tables (less than 2 rows) as it is pointless in that case. */ M = *(*TLogEst)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst + uintptr(saved_nEq)*2)) logK = _estLog(tls, int16(nIn)) /* TUNING v----- 10 to bias toward indexed IN */ x = int16(int32(M) + int32(logK) + int32(10) - (nIn + int32(rLogSize))) if int32(x) >= 0 { } else { if int32(nInMul) < int32(2) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_SeekScan)) == uint32(0) { *(*Tu32)(unsafe.Pointer(pNew + 40)) |= uint32(WHERE_IN_SEEKSCAN) } else { goto _1 } } } *(*Tu32)(unsafe.Pointer(pNew + 40)) |= uint32(WHERE_COLUMN_IN) } else { if int32(eOp)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) != 0 { iCol = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pProbe)).FaiColumn + uintptr(saved_nEq)*2))) *(*Tu32)(unsafe.Pointer(pNew + 40)) |= uint32(WHERE_COLUMN_EQ) _ = libc.Int32FromInt32(0) if iCol == -int32(1) || iCol >= 0 && int32(nInMul) == 0 && int32(saved_nEq) == int32((*TIndex)(unsafe.Pointer(pProbe)).FnKeyCol)-int32(1) { if iCol == -int32(1) || int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x8>>3)) != 0 || int32((*TIndex)(unsafe.Pointer(pProbe)).FnKeyCol) == int32(1) && (*TIndex)(unsafe.Pointer(pProbe)).FonError != 0 && int32(eOp) == int32(WO_EQ) { *(*Tu32)(unsafe.Pointer(pNew + 40)) |= uint32(WHERE_ONEROW) } else { *(*Tu32)(unsafe.Pointer(pNew + 40)) |= uint32(WHERE_UNQ_WANTED) } } if int32((*(*TWhereScan)(unsafe.Pointer(bp))).FiEquiv) > int32(1) { *(*Tu32)(unsafe.Pointer(pNew + 40)) |= uint32(WHERE_TRANSCONS) } } else { if int32(eOp)&int32(WO_ISNULL) != 0 { *(*Tu32)(unsafe.Pointer(pNew + 40)) |= uint32(WHERE_COLUMN_NULL) } else { nVecLen = _whereRangeVectorLen(tls, pParse, (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor, pProbe, int32(saved_nEq), pTerm) if int32(eOp)&(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ))) != 0 { *(*Tu32)(unsafe.Pointer(pNew + 40)) |= uint32(libc.Int32FromInt32(WHERE_COLUMN_RANGE) | libc.Int32FromInt32(WHERE_BTM_LIMIT)) (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnBtm = uint16(nVecLen) pBtm = pTerm pTop = uintptr(0) if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_LIKEOPT) != 0 { /* Range constraints that come from the LIKE optimization are ** always used in pairs. */ pTop = pTerm + 1*48 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if _whereLoopResize(tls, db, pNew, int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm)+int32(1)) != 0 { break } /* OOM */ v8 = pNew + 44 v7 = *(*Tu16)(unsafe.Pointer(v8)) *(*Tu16)(unsafe.Pointer(v8))++ *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(v7)*4)) = pTop *(*Tu32)(unsafe.Pointer(pNew + 40)) |= uint32(WHERE_TOP_LIMIT) (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnTop = uint16(1) } } else { _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(pNew + 40)) |= uint32(libc.Int32FromInt32(WHERE_COLUMN_RANGE) | libc.Int32FromInt32(WHERE_TOP_LIMIT)) (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnTop = uint16(nVecLen) pTop = pTerm if (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags&uint32(WHERE_BTM_LIMIT) != uint32(0) { v9 = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm)-int32(2))*4)) } else { v9 = uintptr(0) } pBtm = v9 } } } } /* At this point pNew->nOut is set to the number of rows expected to ** be visited by the index scan before considering term pTerm, or the ** values of nIn and nInMul. In other words, assuming that all ** "x IN(...)" terms are replaced with "x = ?". This block updates ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */ _ = libc.Int32FromInt32(0) if (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags&uint32(WHERE_COLUMN_RANGE) != 0 { /* Adjust nOut using stat4 data. Or, if there is no stat4 ** data, using some other estimate. */ _whereRangeScanEst(tls, pParse, pBuilder, pBtm, pTop, pNew) } else { v11 = pNew + 24 *(*Tu16)(unsafe.Pointer(v11))++ v10 = *(*Tu16)(unsafe.Pointer(v11)) nEq = int32(v10) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb) <= 0 && int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pProbe)).FaiColumn + uintptr(saved_nEq)*2))) >= 0 { _ = libc.Int32FromInt32(0) p12 = pNew + 22 *(*TLogEst)(unsafe.Pointer(p12)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p12))) + int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb)) p13 = pNew + 22 *(*TLogEst)(unsafe.Pointer(p13)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p13))) - nIn) } else { *(*TtRowcnt)(unsafe.Pointer(bp + 96)) = uint64(0) if int32(nInMul) == 0 && (*TIndex)(unsafe.Pointer(pProbe)).FnSample != 0 && int32((*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnEq) <= (*TIndex)(unsafe.Pointer(pProbe)).FnSampleCol && (int32(eOp)&int32(WO_IN) == 0 || (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(EP_xIsSelect) == uint32(0)) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) == uint32(0) { pExpr1 = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr if int32(eOp)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_ISNULL)|libc.Int32FromInt32(WO_IS)) != 0 { rc = _whereEqualScanEst(tls, pParse, pBuilder, (*TExpr)(unsafe.Pointer(pExpr1)).FpRight, bp+96) } else { rc = _whereInScanEst(tls, pParse, pBuilder, *(*uintptr)(unsafe.Pointer(pExpr1 + 20)), bp+96) } if rc == int32(SQLITE_NOTFOUND) { rc = SQLITE_OK } if rc != SQLITE_OK { break } /* Jump out of the pTerm loop */ if *(*TtRowcnt)(unsafe.Pointer(bp + 96)) != 0 { (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = _sqlite3LogEst(tls, *(*TtRowcnt)(unsafe.Pointer(bp + 96))) if nEq == int32(1) && int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnOut)+int32(10) > int32(*(*TLogEst)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst))) { p14 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p14)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p14))) | libc.Int32FromInt32(TERM_HIGHTRUTH)) if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_HEURTRUTH) != 0 { /* If the term has previously been used with an assumption of ** higher selectivity, then set the flag to rerun the ** loop computations. */ p15 = pBuilder + 25 *(*uint8)(unsafe.Pointer(p15)) = uint8(int32(*(*uint8)(unsafe.Pointer(p15))) | libc.Int32FromInt32(SQLITE_BLDF2_2NDPASS)) } } if int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnOut) > int32(saved_nOut) { (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = saved_nOut } p16 = pNew + 22 *(*TLogEst)(unsafe.Pointer(p16)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p16))) - nIn) } } if *(*TtRowcnt)(unsafe.Pointer(bp + 96)) == uint64(0) { p17 = pNew + 22 *(*TLogEst)(unsafe.Pointer(p17)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p17))) + (int32(*(*TLogEst)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst + uintptr(nEq)*2))) - int32(*(*TLogEst)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst + uintptr(nEq-int32(1))*2))))) if int32(eOp)&int32(WO_ISNULL) != 0 { /* TUNING: If there is no likelihood() value, assume that a ** "col IS NULL" expression matches twice as many rows ** as (col=?). */ p18 = pNew + 22 *(*TLogEst)(unsafe.Pointer(p18)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p18))) + libc.Int32FromInt32(10)) } } } } /* Set rCostIdx to the cost of visiting selected rows in index. Add ** it to pNew->rRun, which is currently set to the cost of the index ** seek only. Then, if this is a non-covering index, add the cost of ** visiting the rows in the main table. */ _ = libc.Int32FromInt32(0) if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_IPK) { /* The pProbe->szIdxRow is low for an IPK table since the interior ** pages are small. Thus szIdxRow gives a good estimate of seek cost. ** But the leaf pages are full-size, so pProbe->szIdxRow would badly ** under-estimate the scanning cost. */ rCostIdx = int16(int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnOut) + int32(16)) } else { rCostIdx = int16(int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnOut) + int32(1) + int32(15)*int32((*TIndex)(unsafe.Pointer(pProbe)).FszIdxRow)/int32((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pSrc)).FpTab)).FszTabRow)) } (*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = _sqlite3LogEstAdd(tls, rLogSize, rCostIdx) if (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_IDX_ONLY)|libc.Int32FromInt32(WHERE_IPK)|libc.Int32FromInt32(WHERE_EXPRIDX)) == uint32(0) { (*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = _sqlite3LogEstAdd(tls, (*TWhereLoop)(unsafe.Pointer(pNew)).FrRun, int16(int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnOut)+int32(16))) } nOutUnadjusted = (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut p19 = pNew + 20 *(*TLogEst)(unsafe.Pointer(p19)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p19))) + (int32(nInMul) + nIn)) p20 = pNew + 22 *(*TLogEst)(unsafe.Pointer(p20)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p20))) + (int32(nInMul) + nIn)) _whereLoopOutputAdjust(tls, (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC, pNew, rSize) rc = _whereLoopInsert(tls, pBuilder, pNew) if (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags&uint32(WHERE_COLUMN_RANGE) != 0 { (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = saved_nOut } else { (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = nOutUnadjusted } if (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags&uint32(WHERE_TOP_LIMIT) == uint32(0) && int32((*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnEq) < int32((*TIndex)(unsafe.Pointer(pProbe)).FnColumn) && (int32((*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnEq) < int32((*TIndex)(unsafe.Pointer(pProbe)).FnKeyCol) || int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x3>>0)) != int32(SQLITE_IDXTYPE_PRIMARYKEY)) { if int32((*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnEq) > int32(3) { _sqlite3ProgressCheck(tls, pParse) } _whereLoopAddBtreeIndex(tls, pBuilder, pSrc, pProbe, int16(int32(nInMul)+nIn)) } (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = saved_nOut (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FnRecValid = nRecValid goto _1 _1: ; pTerm = _whereScanNext(tls, bp) } (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq = saved_prereq (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnEq = saved_nEq (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnBtm = saved_nBtm (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnTop = saved_nTop (*TWhereLoop)(unsafe.Pointer(pNew)).FnSkip = saved_nSkip (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = saved_wsFlags (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = saved_nOut (*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm = saved_nLTerm /* Consider using a skip-scan if there are no WHERE clause constraints ** available for the left-most terms of the index, and if the average ** number of repeats in the left-most terms is at least 18. ** ** The magic number 18 is selected on the basis that scanning 17 rows ** is almost always quicker than an index seek (even though if the index ** contains fewer than 2^17 rows we assume otherwise in other parts of ** the code). And, even if it is not, it should not be too much slower. ** On the other hand, the extra seeks could end up being significantly ** more expensive. */ _ = libc.Int32FromInt32(0) if v22 = int32(saved_nEq) == int32(saved_nSkip) && int32(saved_nEq)+int32(1) < int32((*TIndex)(unsafe.Pointer(pProbe)).FnKeyCol) && int32(saved_nEq) == int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm) && int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x40>>6)) == 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x80>>7)) != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_SkipScan)) == uint32(0) && int32(*(*TLogEst)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst + uintptr(int32(saved_nEq)+int32(1))*2))) >= int32(42); v22 { v21 = _whereLoopResize(tls, db, pNew, int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm)+int32(1)) rc = v21 } if v22 && v21 == SQLITE_OK { (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnEq++ (*TWhereLoop)(unsafe.Pointer(pNew)).FnSkip++ v24 = pNew + 44 v23 = *(*Tu16)(unsafe.Pointer(v24)) *(*Tu16)(unsafe.Pointer(v24))++ *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(v23)*4)) = uintptr(0) *(*Tu32)(unsafe.Pointer(pNew + 40)) |= uint32(WHERE_SKIPSCAN) nIter = int16(int32(*(*TLogEst)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst + uintptr(saved_nEq)*2))) - int32(*(*TLogEst)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst + uintptr(int32(saved_nEq)+int32(1))*2)))) p25 = pNew + 22 *(*TLogEst)(unsafe.Pointer(p25)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p25))) - int32(nIter)) /* TUNING: Because uncertainties in the estimates for skip-scan queries, ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ nIter = TLogEst(int32(nIter) + libc.Int32FromInt32(5)) _whereLoopAddBtreeIndex(tls, pBuilder, pSrc, pProbe, int16(int32(nIter)+int32(nInMul))) (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = saved_nOut (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnEq = saved_nEq (*TWhereLoop)(unsafe.Pointer(pNew)).FnSkip = saved_nSkip (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = saved_wsFlags } return rc } // C documentation // // /* // ** Return True if it is possible that pIndex might be useful in // ** implementing the ORDER BY clause in pBuilder. // ** // ** Return False if pBuilder does not contain an ORDER BY clause or // ** if there is no way for pIndex to be useful in implementing that // ** ORDER BY clause. // */ func _indexMightHelpWithOrderBy(tls *libc.TLS, pBuilder uintptr, pIndex uintptr, iCursor int32) (r int32) { var aColExpr, pExpr, pOB, v1, v4 uintptr var ii, jj int32 _, _, _, _, _, _, _ = aColExpr, ii, jj, pExpr, pOB, v1, v4 if int32(uint32(*(*uint16)(unsafe.Pointer(pIndex + 56))&0x4>>2)) != 0 { return 0 } v1 = (*TWhereInfo)(unsafe.Pointer((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo)).FpOrderBy pOB = v1 if v1 == uintptr(0) { return 0 } ii = 0 for { if !(ii < (*TExprList)(unsafe.Pointer(pOB)).FnExpr) { break } pExpr = _sqlite3ExprSkipCollateAndLikely(tls, (*(*TExprList_item)(unsafe.Pointer(pOB + 8 + uintptr(ii)*20))).FpExpr) if pExpr == uintptr(0) { goto _2 } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pExpr)).FiTable == iCursor { if int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) < 0 { return int32(1) } jj = 0 for { if !(jj < int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol)) { break } if int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) == int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(jj)*2))) { return int32(1) } goto _3 _3: ; jj++ } } else { v4 = (*TIndex)(unsafe.Pointer(pIndex)).FaColExpr aColExpr = v4 if v4 != uintptr(0) { jj = 0 for { if !(jj < int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol)) { break } if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(jj)*2))) != -int32(2) { goto _5 } if _sqlite3ExprCompareSkip(tls, pExpr, (*(*TExprList_item)(unsafe.Pointer(aColExpr + 8 + uintptr(jj)*20))).FpExpr, iCursor) == 0 { return int32(1) } goto _5 _5: ; jj++ } } } goto _2 _2: ; ii++ } return 0 } // C documentation // // /* Check to see if a partial index with pPartIndexWhere can be used // ** in the current query. Return true if it can be and false if not. // */ func _whereUsablePartialIndex(tls *libc.TLS, iTab int32, jointype Tu8, pWC uintptr, pWhere uintptr) (r int32) { var i int32 var pExpr, pParse, pTerm uintptr _, _, _, _ = i, pExpr, pParse, pTerm if int32(jointype)&int32(JT_LTORJ) != 0 { return 0 } pParse = (*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpParse for int32((*TExpr)(unsafe.Pointer(pWhere)).Fop) == int32(TK_AND) { if !(_whereUsablePartialIndex(tls, iTab, jointype, pWC, (*TExpr)(unsafe.Pointer(pWhere)).FpLeft) != 0) { return 0 } pWhere = (*TExpr)(unsafe.Pointer(pWhere)).FpRight } if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&uint64(SQLITE_EnableQPSG) != 0 { pParse = uintptr(0) } i = 0 pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa for { if !(i < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) { break } pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr if (!((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != libc.Uint32FromInt32(0)) || *(*int32)(unsafe.Pointer(pExpr + 36)) == iTab) && (int32(jointype)&int32(JT_OUTER) == 0 || (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0)) && _sqlite3ExprImpliesExpr(tls, pParse, pExpr, pWhere, iTab) != 0 && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VNULL) == 0 { return int32(1) } goto _1 _1: ; i++ pTerm += 48 } return 0 } // C documentation // // /* // ** pIdx is an index containing expressions. Check it see if any of the // ** expressions in the index match the pExpr expression. // */ func _exprIsCoveredByIndex(tls *libc.TLS, pExpr uintptr, pIdx uintptr, iTabCur int32) (r int32) { var i int32 _ = i i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) { break } if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))) == -int32(2) && _sqlite3ExprCompare(tls, uintptr(0), pExpr, (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(i)*20))).FpExpr, iTabCur) == 0 { return int32(1) } goto _1 _1: ; i++ } return 0 } // C documentation // // /* // ** Structure passed to the whereIsCoveringIndex Walker callback. // */ type TCoveringIndexCheck = struct { FpIdx uintptr FiTabCur int32 FbExpr Tu8 FbUnidx Tu8 } type CoveringIndexCheck = TCoveringIndexCheck type TCoveringIndexCheck1 = struct { FpIdx uintptr FiTabCur int32 FbExpr Tu8 FbUnidx Tu8 } type CoveringIndexCheck1 = TCoveringIndexCheck1 // C documentation // // /* // ** Information passed in is pWalk->u.pCovIdxCk. Call it pCk. // ** // ** If the Expr node references the table with cursor pCk->iTabCur, then // ** make sure that column is covered by the index pCk->pIdx. We know that // ** all columns less than 63 (really BMS-1) are covered, so we don't need // ** to check them. But we do need to check any column at 63 or greater. // ** // ** If the index does not cover the column, then set pWalk->eCode to // ** non-zero and return WRC_Abort to stop the search. // ** // ** If this node does not disprove that the index can be a covering index, // ** then just return WRC_Continue, to continue the search. // ** // ** If pCk->pIdx contains indexed expressions and one of those expressions // ** matches pExpr, then prune the search. // */ func _whereIsCoveringIndexWalkCallback(tls *libc.TLS, pWalk uintptr, pExpr uintptr) (r int32) { var aiColumn, pCk, pIdx uintptr var i int32 var nColumn Tu16 _, _, _, _, _ = aiColumn, i, nColumn, pCk, pIdx /* Info about this search */ pCk = *(*uintptr)(unsafe.Pointer(pWalk + 24)) pIdx = (*TCoveringIndexCheck)(unsafe.Pointer(pCk)).FpIdx if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_COLUMN) { /* if( pExpr->iColumn<(BMS-1) && pIdx->bHasExpr==0 ) return WRC_Continue;*/ if (*TExpr)(unsafe.Pointer(pExpr)).FiTable != (*TCoveringIndexCheck)(unsafe.Pointer(pCk)).FiTabCur { return WRC_Continue } pIdx = (*TCoveringIndexCheck1)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalk + 24)))).FpIdx aiColumn = (*TIndex)(unsafe.Pointer(pIdx)).FaiColumn nColumn = (*TIndex)(unsafe.Pointer(pIdx)).FnColumn i = 0 for { if !(i < int32(nColumn)) { break } if int32(*(*Ti16)(unsafe.Pointer(aiColumn + uintptr(i)*2))) == int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) { return WRC_Continue } goto _1 _1: ; i++ } (*TCoveringIndexCheck)(unsafe.Pointer(pCk)).FbUnidx = uint8(1) return int32(WRC_Abort) } else { if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x1000>>12)) != 0 && _exprIsCoveredByIndex(tls, pExpr, pIdx, (*TCoveringIndexCheck1)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalk + 24)))).FiTabCur) != 0 { (*TCoveringIndexCheck)(unsafe.Pointer(pCk)).FbExpr = uint8(1) return int32(WRC_Prune) } } return WRC_Continue } // C documentation // // /* // ** pIdx is an index that covers all of the low-number columns used by // ** pWInfo->pSelect (columns from 0 through 62) or an index that has // ** expressions terms. Hence, we cannot determine whether or not it is // ** a covering index by using the colUsed bitmasks. We have to do a search // ** to see if the index is covering. This routine does that search. // ** // ** The return value is one of these: // ** // ** 0 The index is definitely not a covering index // ** // ** WHERE_IDX_ONLY The index is definitely a covering index // ** // ** WHERE_EXPRIDX The index is likely a covering index, but it is // ** difficult to determine precisely because of the // ** expressions that are indexed. Score it as a // ** covering index, but still keep the main table open // ** just in case we need it. // ** // ** This routine is an optimization. It is always safe to return zero. // ** But returning one of the other two values when zero should have been // ** returned can lead to incorrect bytecode and assertion faults. // */ func _whereIsCoveringIndex(tls *libc.TLS, pWInfo uintptr, pIdx uintptr, iTabCur int32) (r Tu32) { bp := tls.Alloc(48) defer tls.Free(48) var i, rc int32 var _ /* ck at bp+0 */ TCoveringIndexCheck1 var _ /* w at bp+12 */ TWalker _, _ = i, rc if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect == uintptr(0) { /* We don't have access to the full query, so we cannot check to see ** if pIdx is covering. Assume it is not. */ return uint32(0) } if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x1000>>12)) == 0 { i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) { break } if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))) >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) { break } goto _1 _1: ; i++ } if i >= int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) { /* pIdx does not index any columns greater than 62, but we know from ** colMask that columns greater than 62 are used, so this is not a ** covering index */ return uint32(0) } } (*(*TCoveringIndexCheck1)(unsafe.Pointer(bp))).FpIdx = pIdx (*(*TCoveringIndexCheck1)(unsafe.Pointer(bp))).FiTabCur = iTabCur (*(*TCoveringIndexCheck1)(unsafe.Pointer(bp))).FbExpr = uint8(0) (*(*TCoveringIndexCheck1)(unsafe.Pointer(bp))).FbUnidx = uint8(0) libc.Xmemset(tls, bp+12, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp + 12))).FxExprCallback = __ccgo_fp(_whereIsCoveringIndexWalkCallback) (*(*TWalker)(unsafe.Pointer(bp + 12))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop) *(*uintptr)(unsafe.Pointer(bp + 12 + 24)) = bp _sqlite3WalkSelect(tls, bp+12, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect) if (*(*TCoveringIndexCheck1)(unsafe.Pointer(bp))).FbUnidx != 0 { rc = 0 } else { if (*(*TCoveringIndexCheck1)(unsafe.Pointer(bp))).FbExpr != 0 { rc = int32(WHERE_EXPRIDX) } else { rc = int32(WHERE_IDX_ONLY) } } return uint32(rc) } // C documentation // // /* // ** This is an sqlite3ParserAddCleanup() callback that is invoked to // ** free the Parse->pIdxEpr list when the Parse object is destroyed. // */ func _whereIndexedExprCleanup(tls *libc.TLS, db uintptr, pObject uintptr) { var p, pp uintptr _, _ = p, pp pp = pObject for *(*uintptr)(unsafe.Pointer(pp)) != uintptr(0) { p = *(*uintptr)(unsafe.Pointer(pp)) *(*uintptr)(unsafe.Pointer(pp)) = (*TIndexedExpr)(unsafe.Pointer(p)).FpIENext _sqlite3ExprDelete(tls, db, (*TIndexedExpr)(unsafe.Pointer(p)).FpExpr) _sqlite3DbFreeNN(tls, db, p) } } // C documentation // // /* // ** This function is called for a partial index - one with a WHERE clause - in // ** two scenarios. In both cases, it determines whether or not the WHERE // ** clause on the index implies that a column of the table may be safely // ** replaced by a constant expression. For example, in the following // ** SELECT: // ** // ** CREATE INDEX i1 ON t1(b, c) WHERE a=; // ** SELECT a, b, c FROM t1 WHERE a= AND b=?; // ** // ** The "a" in the select-list may be replaced by , iff: // ** // ** (a) is a constant expression, and // ** (b) The (a=) comparison uses the BINARY collation sequence, and // ** (c) Column "a" has an affinity other than NONE or BLOB. // ** // ** If argument pItem is NULL, then pMask must not be NULL. In this case this // ** function is being called as part of determining whether or not pIdx // ** is a covering index. This function clears any bits in (*pMask) // ** corresponding to columns that may be replaced by constants as described // ** above. // ** // ** Otherwise, if pItem is not NULL, then this function is being called // ** as part of coding a loop that uses index pIdx. In this case, add entries // ** to the Parse.pIdxPartExpr list for each column that can be replaced // ** by a constant. // */ func _wherePartIdxExpr(tls *libc.TLS, pParse uintptr, pIdx uintptr, pPart uintptr, pMask uintptr, iIdxCur int32, pItem uintptr) { var aff Tu8 var bNullRow int32 var db, p, pArg, pLeft, pRight uintptr _, _, _, _, _, _, _ = aff, bNullRow, db, p, pArg, pLeft, pRight _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TExpr)(unsafe.Pointer(pPart)).Fop) == int32(TK_AND) { _wherePartIdxExpr(tls, pParse, pIdx, (*TExpr)(unsafe.Pointer(pPart)).FpRight, pMask, iIdxCur, pItem) pPart = (*TExpr)(unsafe.Pointer(pPart)).FpLeft } if int32((*TExpr)(unsafe.Pointer(pPart)).Fop) == int32(TK_EQ) || int32((*TExpr)(unsafe.Pointer(pPart)).Fop) == int32(TK_IS) { pLeft = (*TExpr)(unsafe.Pointer(pPart)).FpLeft pRight = (*TExpr)(unsafe.Pointer(pPart)).FpRight if int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) != int32(TK_COLUMN) { return } if !(_sqlite3ExprIsConstant(tls, pRight) != 0) { return } if !(_sqlite3IsBinary(tls, _sqlite3ExprCompareCollSeq(tls, pParse, pPart)) != 0) { return } if int32((*TExpr)(unsafe.Pointer(pLeft)).FiColumn) < 0 { return } aff = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol + uintptr((*TExpr)(unsafe.Pointer(pLeft)).FiColumn)*12))).Faffinity if int32(aff) >= int32(SQLITE_AFF_TEXT) { if pItem != 0 { db = (*TParse)(unsafe.Pointer(pParse)).Fdb p = _sqlite3DbMallocRaw(tls, db, uint64(24)) if p != 0 { bNullRow = libc.BoolInt32(int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)) != 0) (*TIndexedExpr)(unsafe.Pointer(p)).FpExpr = _sqlite3ExprDup(tls, db, pRight, 0) (*TIndexedExpr)(unsafe.Pointer(p)).FiDataCur = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor (*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCur = iIdxCur (*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCol = int32((*TExpr)(unsafe.Pointer(pLeft)).FiColumn) (*TIndexedExpr)(unsafe.Pointer(p)).FbMaybeNullRow = uint8(bNullRow) (*TIndexedExpr)(unsafe.Pointer(p)).FpIENext = (*TParse)(unsafe.Pointer(pParse)).FpIdxPartExpr (*TIndexedExpr)(unsafe.Pointer(p)).Faff = aff (*TParse)(unsafe.Pointer(pParse)).FpIdxPartExpr = p if (*TIndexedExpr)(unsafe.Pointer(p)).FpIENext == uintptr(0) { pArg = pParse + 76 _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_whereIndexedExprCleanup), pArg) } } } else { if int32((*TExpr)(unsafe.Pointer(pLeft)).FiColumn) < int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) { *(*TBitmask)(unsafe.Pointer(pMask)) &= ^(libc.Uint64FromInt32(1) << (*TExpr)(unsafe.Pointer(pLeft)).FiColumn) } } } } } // C documentation // // /* // ** Add all WhereLoop objects for a single table of the join where the table // ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be // ** a b-tree table, not a virtual table. // ** // ** The costs (WhereLoop.rRun) of the b-tree loops added by this function // ** are calculated as follows: // ** // ** For a full scan, assuming the table (or index) contains nRow rows: // ** // ** cost = nRow * 3.0 // full-table scan // ** cost = nRow * K // scan of covering index // ** cost = nRow * (K+3.0) // scan of non-covering index // ** // ** where K is a value between 1.1 and 3.0 set based on the relative // ** estimated average size of the index and table records. // ** // ** For an index scan, where nVisit is the number of index rows visited // ** by the scan, and nSeek is the number of seek operations required on // ** the index b-tree: // ** // ** cost = nSeek * (log(nRow) + K * nVisit) // covering index // ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index // ** // ** Normally, nSeek is 1. nSeek values greater than 1 come about if the // ** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when // ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans. // ** // ** The estimated values (nRow, nVisit, nSeek) often contain a large amount // ** of uncertainty. For this reason, scoring is designed to pick plans that // ** "do the least harm" if the estimates are inaccurate. For example, a // ** log(nRow) factor is omitted from a non-covering index scan in order to // ** bias the scoring in favor of using an index, since the worst-case // ** performance of using an index is far better than the worst-case performance // ** of a full table scan. // */ func _whereLoopAddBtree(tls *libc.TLS, pBuilder uintptr, mPrereq TBitmask) (r int32) { bp := tls.Alloc(128) defer tls.Free(128) var b, iCur, iSortIdx, ii, rc, v5, v6 int32 var isCov Tu32 var nLookup, rLogSize, rSize TLogEst var pFirst, pNew, pProbe, pSrc, pTab, pTabList, pTerm, pTerm1, pWC, pWC2, pWCEnd, pWInfo, v8, p2, p3 uintptr var _ /* aiColumnPk at bp+108 */ Ti16 var _ /* aiRowEstPk at bp+104 */ [2]TLogEst var _ /* m at bp+112 */ TBitmask var _ /* sPk at bp+0 */ TIndex _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = b, iCur, iSortIdx, ii, isCov, nLookup, pFirst, pNew, pProbe, pSrc, pTab, pTabList, pTerm, pTerm1, pWC, pWC2, pWCEnd, pWInfo, rLogSize, rSize, rc, v5, v6, v8, p2, p3 /* The aiRowLogEst[] value for the sPk index */ *(*Ti16)(unsafe.Pointer(bp + 108)) = int16(-int32(1)) /* Template WhereLoop object */ rc = SQLITE_OK /* Return code */ iSortIdx = int32(1) /* Table being queried */ pNew = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew pWInfo = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo pTabList = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList pSrc = pTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pNew)).FiTab)*72 pTab = (*TSrcItem)(unsafe.Pointer(pSrc)).FpTab pWC = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC _ = libc.Int32FromInt32(0) if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 36 + 4))&0x2>>1)) != 0 { _ = libc.Int32FromInt32(0) /* An INDEXED BY clause specifies a particular index to use */ pProbe = *(*uintptr)(unsafe.Pointer(pSrc + 68)) } else { if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { pProbe = (*TTable)(unsafe.Pointer(pTab)).FpIndex } else { /* First of real indices on the table */ libc.Xmemset(tls, bp, 0, uint32(104)) (*(*TIndex)(unsafe.Pointer(bp))).FnKeyCol = uint16(1) (*(*TIndex)(unsafe.Pointer(bp))).FnColumn = uint16(1) (*(*TIndex)(unsafe.Pointer(bp))).FaiColumn = bp + 108 (*(*TIndex)(unsafe.Pointer(bp))).FaiRowLogEst = bp + 104 (*(*TIndex)(unsafe.Pointer(bp))).FonError = uint8(OE_Replace) (*(*TIndex)(unsafe.Pointer(bp))).FpTable = pTab (*(*TIndex)(unsafe.Pointer(bp))).FszIdxRow = int16(3) /* TUNING: Interior rows of IPK table are very small */ libc.SetBitFieldPtr16Uint32(bp+56, libc.Uint32FromInt32(SQLITE_IDXTYPE_IPK), 0, 0x3) (*(*[2]TLogEst)(unsafe.Pointer(bp + 104)))[0] = (*TTable)(unsafe.Pointer(pTab)).FnRowLogEst (*(*[2]TLogEst)(unsafe.Pointer(bp + 104)))[int32(1)] = 0 pFirst = (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pSrc)).FpTab)).FpIndex if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 36 + 4))&0x1>>0)) == 0 { /* The real indices of the table are only considered if the ** NOT INDEXED qualifier is omitted from the FROM clause */ (*(*TIndex)(unsafe.Pointer(bp))).FpNext = pFirst } pProbe = bp } } rSize = (*TTable)(unsafe.Pointer(pTab)).FnRowLogEst /* Automatic indexes */ if !((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpOrSet != 0) && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&(libc.Int32FromInt32(WHERE_RIGHT_JOIN)|libc.Int32FromInt32(WHERE_OR_SUBCLAUSE)) == 0 && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse)).Fdb)).Fflags&uint64(SQLITE_AutoIndex) != uint64(0) && !(int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 36 + 4))&0x2>>1)) != 0) && !(int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 36 + 4))&0x1>>0)) != 0) && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) && !(int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 36 + 4))&0x8>>3)) != 0) && !(int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 36 + 4))&0x40>>6)) != 0) && int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&int32(JT_RIGHT) == 0 { pWCEnd = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr((*TWhereClause)(unsafe.Pointer(pWC)).FnTerm)*48 rLogSize = _estLog(tls, rSize) pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa for { if !(rc == SQLITE_OK && pTerm < pWCEnd) { break } if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight&(*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf != 0 { goto _1 } if _termCanDriveIndex(tls, pTerm, pSrc, uint64(0)) != 0 { (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnEq = uint16(1) (*TWhereLoop)(unsafe.Pointer(pNew)).FnSkip = uint16(0) (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FpIndex = uintptr(0) (*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm = uint16(1) *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm)) = pTerm /* TUNING: One-time cost for computing the automatic index is ** estimated to be X*N*log2(N) where N is the number of rows in ** the table being indexed and where X is 7 (LogEst=28) for normal ** tables or 0.5 (LogEst=-10) for views and subqueries. The value ** of X is smaller for views and subqueries so that the query planner ** will be more aggressive about generating automatic indexes for ** those objects, since there is no opportunity to add schema ** indexes on subqueries and views. */ (*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup = int16(int32(rLogSize) + int32(rSize)) if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VIEW)) && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Ephemeral) == uint32(0) { p2 = pNew + 18 *(*TLogEst)(unsafe.Pointer(p2)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p2))) + libc.Int32FromInt32(28)) } else { p3 = pNew + 18 *(*TLogEst)(unsafe.Pointer(p3)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p3))) - libc.Int32FromInt32(25)) /* Greatly reduced setup cost for auto indexes ** on ephemeral materializations of views */ } if int32((*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup) < 0 { (*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup = 0 } /* TUNING: Each index lookup yields 20 rows in the table. This ** is more than the usual guess of 10 rows, since we have no way ** of knowing how selective the index will ultimately be. It would ** not be unreasonable to make this value much larger. */ (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = int16(43) _ = libc.Int32FromInt32(0) (*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = _sqlite3LogEstAdd(tls, rLogSize, (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut) (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(WHERE_AUTO_INDEX) (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq = mPrereq | (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight rc = _whereLoopInsert(tls, pBuilder, pNew) } goto _1 _1: ; pTerm += 48 } } /* Loop over all indices. If there was an INDEXED BY clause, then only ** consider index pProbe. */ for { if !(rc == SQLITE_OK && pProbe != 0) { break } if (*TIndex)(unsafe.Pointer(pProbe)).FpPartIdxWhere != uintptr(0) && !(_whereUsablePartialIndex(tls, (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor, (*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype, pWC, (*TIndex)(unsafe.Pointer(pProbe)).FpPartIdxWhere) != 0) { /* See ticket [98d973b8f5] */ goto _4 /* Partial index inappropriate for this query */ } if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x200>>9)) != 0 { goto _4 } rSize = *(*TLogEst)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst)) (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnEq = uint16(0) (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnBtm = uint16(0) (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnTop = uint16(0) (*TWhereLoop)(unsafe.Pointer(pNew)).FnSkip = uint16(0) (*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm = uint16(0) (*TWhereLoop)(unsafe.Pointer(pNew)).FiSortIdx = uint8(0) (*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup = 0 (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq = mPrereq (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = rSize (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FpIndex = pProbe b = _indexMightHelpWithOrderBy(tls, pBuilder, pProbe, (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor) /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ _ = libc.Int32FromInt32(0) if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_IPK) { /* Integer primary key index */ (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(WHERE_IPK) /* Full table scan */ if b != 0 { v5 = iSortIdx } else { v5 = 0 } (*TWhereLoop)(unsafe.Pointer(pNew)).FiSortIdx = uint8(v5) /* TUNING: Cost of full table scan is 3.0*N. The 3.0 factor is an ** extra cost designed to discourage the use of full table scans, ** since index lookups have better worst-case performance if our ** stat guesses are wrong. Reduce the 3.0 penalty slightly ** (to 2.75) if we have valid STAT4 information for the table. ** At 2.75, a full table scan is preferred over using an index on ** a column with just two distinct values where each value has about ** an equal number of appearances. Without STAT4 data, we still want ** to use an index in that case, since the constraint might be for ** the scarcer of the two values, and in that case an index lookup is ** better. */ (*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = int16(int32(rSize) + int32(16) - int32(2)*libc.BoolInt32((*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasStat4) != uint32(0))) _whereLoopOutputAdjust(tls, pWC, pNew, rSize) rc = _whereLoopInsert(tls, pBuilder, pNew) (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = rSize if rc != 0 { break } } else { if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x20>>5)) != 0 { *(*TBitmask)(unsafe.Pointer(bp + 112)) = uint64(0) (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(libc.Int32FromInt32(WHERE_IDX_ONLY) | libc.Int32FromInt32(WHERE_INDEXED)) } else { *(*TBitmask)(unsafe.Pointer(bp + 112)) = (*TSrcItem)(unsafe.Pointer(pSrc)).FcolUsed & (*TIndex)(unsafe.Pointer(pProbe)).FcolNotIdxed if (*TIndex)(unsafe.Pointer(pProbe)).FpPartIdxWhere != 0 { _wherePartIdxExpr(tls, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse, pProbe, (*TIndex)(unsafe.Pointer(pProbe)).FpPartIdxWhere, bp+112, 0, uintptr(0)) } (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(WHERE_INDEXED) if *(*TBitmask)(unsafe.Pointer(bp + 112)) == libc.Uint64FromInt32(1)<<(int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1)) || int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x1000>>12)) != 0 && !(int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x800>>11)) != 0) && *(*TBitmask)(unsafe.Pointer(bp + 112)) != uint64(0) { isCov = _whereIsCoveringIndex(tls, pWInfo, pProbe, (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor) if isCov == uint32(0) { _ = libc.Int32FromInt32(0) } else { *(*TBitmask)(unsafe.Pointer(bp + 112)) = uint64(0) *(*Tu32)(unsafe.Pointer(pNew + 40)) |= isCov if isCov&uint32(WHERE_IDX_ONLY) != 0 { } else { _ = libc.Int32FromInt32(0) } } } else { if *(*TBitmask)(unsafe.Pointer(bp + 112)) == uint64(0) { (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(libc.Int32FromInt32(WHERE_IDX_ONLY) | libc.Int32FromInt32(WHERE_INDEXED)) } } } /* Full scan via index */ if b != 0 || !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) || (*TIndex)(unsafe.Pointer(pProbe)).FpPartIdxWhere != uintptr(0) || int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 36 + 4))&0x2>>1)) != 0 || *(*TBitmask)(unsafe.Pointer(bp + 112)) == uint64(0) && int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x4>>2)) == 0 && int32((*TIndex)(unsafe.Pointer(pProbe)).FszIdxRow) < int32((*TTable)(unsafe.Pointer(pTab)).FszTabRow) && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_ONEPASS_DESIRED) == 0 && _sqlite3Config.FbUseCis != 0 && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_CoverIdxScan)) == uint32(0) { if b != 0 { v6 = iSortIdx } else { v6 = 0 } (*TWhereLoop)(unsafe.Pointer(pNew)).FiSortIdx = uint8(v6) /* The cost of visiting the index rows is N*K, where K is ** between 1.1 and 3.0, depending on the relative sizes of the ** index and table rows. */ (*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = int16(int32(rSize) + int32(1) + int32(15)*int32((*TIndex)(unsafe.Pointer(pProbe)).FszIdxRow)/int32((*TTable)(unsafe.Pointer(pTab)).FszTabRow)) if *(*TBitmask)(unsafe.Pointer(bp + 112)) != uint64(0) { /* If this is a non-covering index scan, add in the cost of ** doing table lookups. The cost will be 3x the number of ** lookups. Take into account WHERE clause terms that can be ** satisfied using just the index, and that do not require a ** table lookup. */ nLookup = int16(int32(rSize) + int32(16)) iCur = (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor pWC2 = pWInfo + 80 ii = 0 for { if !(ii < (*TWhereClause)(unsafe.Pointer(pWC2)).FnTerm) { break } pTerm1 = (*TWhereClause)(unsafe.Pointer(pWC2)).Fa + uintptr(ii)*48 if !(_sqlite3ExprCoveredByIndex(tls, (*TWhereTerm)(unsafe.Pointer(pTerm1)).FpExpr, iCur, pProbe) != 0) { break } /* pTerm can be evaluated using just the index. So reduce ** the expected number of table lookups accordingly */ if int32((*TWhereTerm)(unsafe.Pointer(pTerm1)).FtruthProb) <= 0 { nLookup = TLogEst(int32(nLookup) + int32((*TWhereTerm)(unsafe.Pointer(pTerm1)).FtruthProb)) } else { nLookup-- if int32((*TWhereTerm)(unsafe.Pointer(pTerm1)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) != 0 { nLookup = TLogEst(int32(nLookup) - libc.Int32FromInt32(19)) } } goto _7 _7: ; ii++ } (*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = _sqlite3LogEstAdd(tls, (*TWhereLoop)(unsafe.Pointer(pNew)).FrRun, nLookup) } _whereLoopOutputAdjust(tls, pWC, pNew, rSize) if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&int32(JT_RIGHT) != 0 && (*TIndex)(unsafe.Pointer(pProbe)).FaColExpr != 0 { /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN ** because the cursor used to access the index might not be ** positioned to the correct row during the right-join no-match ** loop. */ } else { rc = _whereLoopInsert(tls, pBuilder, pNew) } (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = rSize if rc != 0 { break } } } (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FbldFlags1 = uint8(0) rc = _whereLoopAddBtreeIndex(tls, pBuilder, pSrc, pProbe, 0) if int32((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FbldFlags1) == int32(SQLITE_BLDF1_INDEXED) { /* If a non-unique index is used, or if a prefix of the key for ** unique index is used (making the index functionally non-unique) ** then the sqlite_stat1 data becomes important for scoring the ** plan */ *(*Tu32)(unsafe.Pointer(pTab + 28)) |= uint32(TF_StatsUsed) } _sqlite3Stat4ProbeFree(tls, (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpRec) (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FnRecValid = 0 (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpRec = uintptr(0) goto _4 _4: ; if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 36 + 4))&0x2>>1)) != 0 { v8 = uintptr(0) } else { v8 = (*TIndex)(unsafe.Pointer(pProbe)).FpNext } pProbe = v8 iSortIdx++ } return rc } // C documentation // // /* // ** Return true if pTerm is a virtual table LIMIT or OFFSET term. // */ func _isLimitTerm(tls *libc.TLS, pTerm uintptr) (r int32) { _ = libc.Int32FromInt32(0) return libc.BoolInt32(int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeMatchOp) >= int32(SQLITE_INDEX_CONSTRAINT_LIMIT) && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeMatchOp) <= int32(SQLITE_INDEX_CONSTRAINT_OFFSET)) } // C documentation // // /* // ** Argument pIdxInfo is already populated with all constraints that may // ** be used by the virtual table identified by pBuilder->pNew->iTab. This // ** function marks a subset of those constraints usable, invokes the // ** xBestIndex method and adds the returned plan to pBuilder. // ** // ** A constraint is marked usable if: // ** // ** * Argument mUsable indicates that its prerequisites are available, and // ** // ** * It is not one of the operators specified in the mExclude mask passed // ** as the fourth argument (which in practice is either WO_IN or 0). // ** // ** Argument mPrereq is a mask of tables that must be scanned before the // ** virtual table in question. These are added to the plans prerequisites // ** before it is added to pBuilder. // ** // ** Output parameter *pbIn is set to true if the plan added to pBuilder // ** uses one or more WO_IN terms, or false otherwise. // */ func _whereLoopAddVirtualOne(tls *libc.TLS, pBuilder uintptr, mPrereq TBitmask, mUsable TBitmask, mExclude Tu16, pIdxInfo uintptr, mNoOmit Tu16, pbIn uintptr, pbRetryLimit uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, iTerm, j, mxTerm, nConstraint, rc, v3, v7 int32 var pHidden, pIdxCons, pNew, pParse, pSrc, pTerm, pTerm1, pUsage, pWC, p4 uintptr var v5 uint32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, iTerm, j, mxTerm, nConstraint, pHidden, pIdxCons, pNew, pParse, pSrc, pTerm, pTerm1, pUsage, pWC, rc, v3, v5, v7, p4 pWC = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC pHidden = pIdxInfo + 1*72 pUsage = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage rc = SQLITE_OK pNew = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew pParse = (*TWhereInfo)(unsafe.Pointer((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo)).FpParse pSrc = (*TWhereInfo)(unsafe.Pointer((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pNew)).FiTab)*72 nConstraint = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pbIn)) = 0 (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq = mPrereq /* Set the usable flag on the subset of constraints identified by ** arguments mUsable and mExclude. */ pIdxCons = *(*uintptr)(unsafe.Pointer(pIdxInfo + 4)) i = 0 for { if !(i < nConstraint) { break } pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr((*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons)).FiTermOffset)*48 (*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons)).Fusable = uint8(0) if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight&mUsable == (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(mExclude) == 0 && (pbRetryLimit != 0 || !(_isLimitTerm(tls, pTerm) != 0)) { (*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons)).Fusable = uint8(1) } goto _1 _1: ; i++ pIdxCons += 12 } /* Initialize the output fields of the sqlite3_index_info structure */ libc.Xmemset(tls, pUsage, 0, uint32(8)*uint32(nConstraint)) _ = libc.Int32FromInt32(0) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxStr = uintptr(0) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxNum = 0 (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).ForderByConsumed = 0 (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = float64(1e+99) / libc.Float64FromInt32(2) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = int64(25) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxFlags = 0 (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FcolUsed = uint64(int64((*TSrcItem)(unsafe.Pointer(pSrc)).FcolUsed)) (*THiddenIndexInfo)(unsafe.Pointer(pHidden)).FmHandleIn = uint32(0) /* Invoke the virtual table xBestIndex() method */ rc = _vtabBestIndex(tls, pParse, (*TSrcItem)(unsafe.Pointer(pSrc)).FpTab, pIdxInfo) if rc != 0 { if rc == int32(SQLITE_CONSTRAINT) { /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means ** that the particular combination of parameters provided is unusable. ** Make no entries in the loop table. */ return SQLITE_OK } return rc } mxTerm = -int32(1) _ = libc.Int32FromInt32(0) libc.Xmemset(tls, (*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm, 0, uint32(4)*uint32(nConstraint)) libc.Xmemset(tls, pNew+24, 0, uint32(16)) pIdxCons = *(*uintptr)(unsafe.Pointer(pIdxInfo + 4)) i = 0 for { if !(i < nConstraint) { break } v3 = (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer(pUsage + uintptr(i)*8))).FargvIndex - libc.Int32FromInt32(1) iTerm = v3 if v3 >= 0 { j = (*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons)).FiTermOffset if iTerm >= nConstraint || j < 0 || j >= (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm || *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(iTerm)*4)) != uintptr(0) || int32((*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons)).Fusable) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22431, libc.VaList(bp+8, (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pSrc)).FpTab)).FzName)) return int32(SQLITE_ERROR) } pTerm1 = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(j)*48 *(*TBitmask)(unsafe.Pointer(pNew)) |= (*TWhereTerm)(unsafe.Pointer(pTerm1)).FprereqRight _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(iTerm)*4)) = pTerm1 if iTerm > mxTerm { mxTerm = iTerm } if (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer(pUsage + uintptr(i)*8))).Fomit != 0 { if i < int32(16) && int32(1)<>0)) != 0 { Xsqlite3_free(tls, (*(*struct { FidxNum int32 F__ccgo4 uint8 FisOrdered Ti8 FomitMask Tu16 FidxStr uintptr FmHandleIn Tu32 })(unsafe.Pointer(pNew + 24))).FidxStr) libc.SetBitFieldPtr8Uint32(pNew+24+4, libc.Uint32FromInt32(0), 0, 0x1) } return rc } // C documentation // // /* // ** Return the collating sequence for a constraint passed into xBestIndex. // ** // ** pIdxInfo must be an sqlite3_index_info structure passed into xBestIndex. // ** This routine depends on there being a HiddenIndexInfo structure immediately // ** following the sqlite3_index_info structure. // ** // ** Return a pointer to the collation name: // ** // ** 1. If there is an explicit COLLATE operator on the constraint, return it. // ** // ** 2. Else, if the column has an alternative collation, return that. // ** // ** 3. Otherwise, return "BINARY". // */ func Xsqlite3_vtab_collation(tls *libc.TLS, pIdxInfo uintptr, iCons int32) (r uintptr) { var iTerm int32 var pC, pHidden, pX, zRet, v1 uintptr _, _, _, _, _, _ = iTerm, pC, pHidden, pX, zRet, v1 pHidden = pIdxInfo + 1*72 zRet = uintptr(0) if iCons >= 0 && iCons < (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint { pC = uintptr(0) iTerm = (*(*Tsqlite3_index_constraint)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(iCons)*12))).FiTermOffset pX = (*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer((*THiddenIndexInfo)(unsafe.Pointer(pHidden)).FpWC)).Fa + uintptr(iTerm)*48))).FpExpr if (*TExpr)(unsafe.Pointer(pX)).FpLeft != 0 { pC = _sqlite3ExprCompareCollSeq(tls, (*THiddenIndexInfo)(unsafe.Pointer(pHidden)).FpParse, pX) } if pC != 0 { v1 = (*TCollSeq)(unsafe.Pointer(pC)).FzName } else { v1 = uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) } zRet = v1 } return zRet } // C documentation // // /* // ** Return true if constraint iCons is really an IN(...) constraint, or // ** false otherwise. If iCons is an IN(...) constraint, set (if bHandle!=0) // ** or clear (if bHandle==0) the flag to handle it using an iterator. // */ func Xsqlite3_vtab_in(tls *libc.TLS, pIdxInfo uintptr, iCons int32, bHandle int32) (r int32) { var m Tu32 var pHidden uintptr var v1 uint32 _, _, _ = m, pHidden, v1 pHidden = pIdxInfo + 1*72 if iCons <= int32(31) { v1 = libc.Uint32FromInt32(1) << iCons } else { v1 = uint32(0) } m = v1 if m&(*THiddenIndexInfo)(unsafe.Pointer(pHidden)).FmIn != 0 { if bHandle == 0 { *(*Tu32)(unsafe.Pointer(pHidden + 16)) &= ^m } else { if bHandle > 0 { *(*Tu32)(unsafe.Pointer(pHidden + 16)) |= m } } return int32(1) } return 0 } // C documentation // // /* // ** This interface is callable from within the xBestIndex callback only. // ** // ** If possible, set (*ppVal) to point to an object containing the value // ** on the right-hand-side of constraint iCons. // */ func Xsqlite3_vtab_rhs_value(tls *libc.TLS, pIdxInfo uintptr, iCons int32, ppVal uintptr) (r int32) { var pH, pTerm, pVal uintptr var rc int32 _, _, _, _ = pH, pTerm, pVal, rc pH = pIdxInfo + 1*72 pVal = uintptr(0) rc = SQLITE_OK if iCons < 0 || iCons >= (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint { rc = _sqlite3MisuseError(tls, int32(165251)) /* EV: R-30545-25046 */ } else { if *(*uintptr)(unsafe.Pointer(pH + 20 + uintptr(iCons)*4)) == uintptr(0) { pTerm = (*TWhereClause)(unsafe.Pointer((*THiddenIndexInfo)(unsafe.Pointer(pH)).FpWC)).Fa + uintptr((*(*Tsqlite3_index_constraint)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(iCons)*12))).FiTermOffset)*48 rc = _sqlite3ValueFromExpr(tls, (*TParse)(unsafe.Pointer((*THiddenIndexInfo)(unsafe.Pointer(pH)).FpParse)).Fdb, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpRight, (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer((*THiddenIndexInfo)(unsafe.Pointer(pH)).FpParse)).Fdb)).Fenc, uint8(SQLITE_AFF_BLOB), pH+20+uintptr(iCons)*4) } pVal = *(*uintptr)(unsafe.Pointer(pH + 20 + uintptr(iCons)*4)) } *(*uintptr)(unsafe.Pointer(ppVal)) = pVal if rc == SQLITE_OK && pVal == uintptr(0) { /* IMP: R-19933-32160 */ rc = int32(SQLITE_NOTFOUND) /* IMP: R-36424-56542 */ } return rc } // C documentation // // /* // ** Return true if ORDER BY clause may be handled as DISTINCT. // */ func Xsqlite3_vtab_distinct(tls *libc.TLS, pIdxInfo uintptr) (r int32) { var pHidden uintptr _ = pHidden pHidden = pIdxInfo + 1*72 _ = libc.Int32FromInt32(0) return (*THiddenIndexInfo)(unsafe.Pointer(pHidden)).FeDistinct } // C documentation // // /* // ** Cause the prepared statement that is associated with a call to // ** xBestIndex to potentially use all schemas. If the statement being // ** prepared is read-only, then just start read transactions on all // ** schemas. But if this is a write operation, start writes on all // ** schemas. // ** // ** This is used by the (built-in) sqlite_dbpage virtual table. // */ func _sqlite3VtabUsesAllSchemas(tls *libc.TLS, pParse uintptr) { var i, nDb int32 _, _ = i, nDb nDb = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FnDb i = 0 for { if !(i < nDb) { break } _sqlite3CodeVerifySchema(tls, pParse, i) goto _1 _1: ; i++ } if (*TParse)(unsafe.Pointer(pParse)).FwriteMask != uint32(0) { i = 0 for { if !(i < nDb) { break } _sqlite3BeginWriteOperation(tls, pParse, 0, i) goto _2 _2: ; i++ } } } // C documentation // // /* // ** Add all WhereLoop objects for a table of the join identified by // ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. // ** // ** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and // ** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause // ** entries that occur before the virtual table in the FROM clause and are // ** separated from it by at least one LEFT or CROSS JOIN. Similarly, the // ** mUnusable mask contains all FROM clause entries that occur after the // ** virtual table and are separated from it by at least one LEFT or // ** CROSS JOIN. // ** // ** For example, if the query were: // ** // ** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6; // ** // ** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6). // ** // ** All the tables in mPrereq must be scanned before the current virtual // ** table. So any terms for which all prerequisites are satisfied by // ** mPrereq may be specified as "usable" in all calls to xBestIndex. // ** Conversely, all tables in mUnusable must be scanned after the current // ** virtual table, so any terms for which the prerequisites overlap with // ** mUnusable should always be configured as "not-usable" for xBestIndex. // */ func _whereLoopAddVirtual(tls *libc.TLS, pBuilder uintptr, mPrereq TBitmask, mUnusable TBitmask) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, nConstraint, rc, seenZero, seenZeroNoIN int32 var mBest, mBestNoIn, mNext, mPrev, mThis, v1 TBitmask var p, pNew, pParse, pSrc, pWC, pWInfo uintptr var v2 bool var _ /* bIn at bp+0 */ int32 var _ /* bRetry at bp+8 */ int32 var _ /* mNoOmit at bp+4 */ Tu16 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, mBest, mBestNoIn, mNext, mPrev, mThis, nConstraint, p, pNew, pParse, pSrc, pWC, pWInfo, rc, seenZero, seenZeroNoIN, v1, v2 rc = SQLITE_OK *(*int32)(unsafe.Pointer(bp + 8)) = 0 /* True to retry with LIMIT/OFFSET disabled */ _ = libc.Int32FromInt32(0) pWInfo = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse pWC = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC pNew = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew pSrc = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pNew)).FiTab)*72 _ = libc.Int32FromInt32(0) p = _allocateIndexInfo(tls, pWInfo, pWC, mUnusable, pSrc, bp+4) if p == uintptr(0) { return int32(SQLITE_NOMEM) } (*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup = 0 (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(WHERE_VIRTUALTABLE) (*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm = uint16(0) libc.SetBitFieldPtr8Uint32(pNew+24+4, libc.Uint32FromInt32(0), 0, 0x1) nConstraint = (*Tsqlite3_index_info)(unsafe.Pointer(p)).FnConstraint if _whereLoopResize(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pNew, nConstraint) != 0 { _freeIndexInfo(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, p) return int32(SQLITE_NOMEM) } /* First call xBestIndex() with all constraints usable. */ rc = _whereLoopAddVirtualOne(tls, pBuilder, mPrereq, uint64(-libc.Int32FromInt32(1)), uint16(0), p, *(*Tu16)(unsafe.Pointer(bp + 4)), bp, bp+8) if *(*int32)(unsafe.Pointer(bp + 8)) != 0 { _ = libc.Int32FromInt32(0) rc = _whereLoopAddVirtualOne(tls, pBuilder, mPrereq, uint64(-libc.Int32FromInt32(1)), uint16(0), p, *(*Tu16)(unsafe.Pointer(bp + 4)), bp, uintptr(0)) } /* If the call to xBestIndex() with all terms enabled produced a plan ** that does not require any source tables (IOW: a plan with mBest==0) ** and does not use an IN(...) operator, then there is no point in making ** any further calls to xBestIndex() since they will all return the same ** result (if the xBestIndex() implementation is sane). */ if v2 = rc == SQLITE_OK; v2 { v1 = (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq & ^mPrereq mBest = v1 } if v2 && (v1 != uint64(0) || *(*int32)(unsafe.Pointer(bp)) != 0) { seenZero = 0 /* True if a plan with no prereqs seen */ seenZeroNoIN = 0 /* Plan with no prereqs and no IN(...) seen */ mPrev = uint64(0) mBestNoIn = uint64(0) /* If the plan produced by the earlier call uses an IN(...) term, call ** xBestIndex again, this time with IN(...) terms disabled. */ if *(*int32)(unsafe.Pointer(bp)) != 0 { rc = _whereLoopAddVirtualOne(tls, pBuilder, mPrereq, uint64(-libc.Int32FromInt32(1)), uint16(WO_IN), p, *(*Tu16)(unsafe.Pointer(bp + 4)), bp, uintptr(0)) _ = libc.Int32FromInt32(0) mBestNoIn = (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq & ^mPrereq if mBestNoIn == uint64(0) { seenZero = int32(1) seenZeroNoIN = int32(1) } } /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq) ** in the set of terms that apply to the current virtual table. */ for rc == SQLITE_OK { mNext = uint64(-libc.Int32FromInt32(1)) _ = libc.Int32FromInt32(0) i = 0 for { if !(i < nConstraint) { break } mThis = (*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr((*(*Tsqlite3_index_constraint)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(p)).FaConstraint + uintptr(i)*12))).FiTermOffset)*48))).FprereqRight & ^mPrereq if mThis > mPrev && mThis < mNext { mNext = mThis } goto _3 _3: ; i++ } mPrev = mNext if mNext == uint64(-libc.Int32FromInt32(1)) { break } if mNext == mBest || mNext == mBestNoIn { continue } rc = _whereLoopAddVirtualOne(tls, pBuilder, mPrereq, mNext|mPrereq, uint16(0), p, *(*Tu16)(unsafe.Pointer(bp + 4)), bp, uintptr(0)) if (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq == mPrereq { seenZero = int32(1) if *(*int32)(unsafe.Pointer(bp)) == 0 { seenZeroNoIN = int32(1) } } } /* If the calls to xBestIndex() in the above loop did not find a plan ** that requires no source tables at all (i.e. one guaranteed to be ** usable), make a call here with all source tables disabled */ if rc == SQLITE_OK && seenZero == 0 { rc = _whereLoopAddVirtualOne(tls, pBuilder, mPrereq, mPrereq, uint16(0), p, *(*Tu16)(unsafe.Pointer(bp + 4)), bp, uintptr(0)) if *(*int32)(unsafe.Pointer(bp)) == 0 { seenZeroNoIN = int32(1) } } /* If the calls to xBestIndex() have so far failed to find a plan ** that requires no source tables at all and does not use an IN(...) ** operator, make a final call to obtain one here. */ if rc == SQLITE_OK && seenZeroNoIN == 0 { rc = _whereLoopAddVirtualOne(tls, pBuilder, mPrereq, mPrereq, uint16(WO_IN), p, *(*Tu16)(unsafe.Pointer(bp + 4)), bp, uintptr(0)) } } if (*Tsqlite3_index_info)(unsafe.Pointer(p)).FneedToFreeIdxStr != 0 { Xsqlite3_free(tls, (*Tsqlite3_index_info)(unsafe.Pointer(p)).FidxStr) } _freeIndexInfo(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, p) return rc } // C documentation // // /* // ** Add WhereLoop entries to handle OR terms. This works for either // ** btrees or virtual tables. // */ func _whereLoopAddOr(tls *libc.TLS, pBuilder uintptr, mPrereq TBitmask, mUnusable TBitmask) (r int32) { bp := tls.Alloc(624) defer tls.Free(624) var i, iCur, j, once, rc int32 var pItem, pNew, pOrTerm, pOrWC, pOrWCEnd, pTerm, pWC, pWCEnd, pWInfo uintptr var _ /* sCur at bp+504 */ TWhereOrSet var _ /* sPrev at bp+560 */ TWhereOrSet var _ /* sSubBuild at bp+416 */ TWhereLoopBuilder var _ /* sSum at bp+448 */ TWhereOrSet var _ /* tempWC at bp+0 */ TWhereClause _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, iCur, j, once, pItem, pNew, pOrTerm, pOrWC, pOrWCEnd, pTerm, pWC, pWCEnd, pWInfo, rc pWInfo = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo rc = SQLITE_OK pWC = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC pWCEnd = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr((*TWhereClause)(unsafe.Pointer(pWC)).FnTerm)*48 pNew = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew libc.Xmemset(tls, bp+448, 0, uint32(56)) pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pNew)).FiTab)*72 iCur = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor /* The multi-index OR optimization does not work for RIGHT and FULL JOIN */ if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_RIGHT) != 0 { return SQLITE_OK } pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa for { if !(pTerm < pWCEnd && rc == SQLITE_OK) { break } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_OR) != 0 && (*TWhereOrInfo)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pTerm)).Fu)))).Findexable&(*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf != uint64(0) { pOrWC = *(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pTerm)).Fu)) pOrWCEnd = (*TWhereClause)(unsafe.Pointer(pOrWC)).Fa + uintptr((*TWhereClause)(unsafe.Pointer(pOrWC)).FnTerm)*48 once = int32(1) *(*TWhereLoopBuilder)(unsafe.Pointer(bp + 416)) = *(*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)) (*(*TWhereLoopBuilder)(unsafe.Pointer(bp + 416))).FpOrSet = bp + 504 pOrTerm = (*TWhereClause)(unsafe.Pointer(pOrWC)).Fa for { if !(pOrTerm < pOrWCEnd) { break } if int32((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FeOperator)&int32(WO_AND) != 0 { (*(*TWhereLoopBuilder)(unsafe.Pointer(bp + 416))).FpWC = *(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pOrTerm)).Fu)) } else { if (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor == iCur { (*(*TWhereClause)(unsafe.Pointer(bp))).FpWInfo = (*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo (*(*TWhereClause)(unsafe.Pointer(bp))).FpOuter = pWC (*(*TWhereClause)(unsafe.Pointer(bp))).Fop = uint8(TK_AND) (*(*TWhereClause)(unsafe.Pointer(bp))).FnTerm = int32(1) (*(*TWhereClause)(unsafe.Pointer(bp))).FnBase = int32(1) (*(*TWhereClause)(unsafe.Pointer(bp))).Fa = pOrTerm (*(*TWhereLoopBuilder)(unsafe.Pointer(bp + 416))).FpWC = bp } else { goto _2 } } (*(*TWhereOrSet)(unsafe.Pointer(bp + 504))).Fn = uint16(0) if int32((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpTab)).FeTabType) == int32(TABTYP_VTAB) { rc = _whereLoopAddVirtual(tls, bp+416, mPrereq, mUnusable) } else { rc = _whereLoopAddBtree(tls, bp+416, mPrereq) } if rc == SQLITE_OK { rc = _whereLoopAddOr(tls, bp+416, mPrereq, mUnusable) } if int32((*(*TWhereOrSet)(unsafe.Pointer(bp + 504))).Fn) == 0 { (*(*TWhereOrSet)(unsafe.Pointer(bp + 448))).Fn = uint16(0) break } else { if once != 0 { _whereOrMove(tls, bp+448, bp+504) once = 0 } else { _whereOrMove(tls, bp+560, bp+448) (*(*TWhereOrSet)(unsafe.Pointer(bp + 448))).Fn = uint16(0) i = 0 for { if !(i < int32((*(*TWhereOrSet)(unsafe.Pointer(bp + 560))).Fn)) { break } j = 0 for { if !(j < int32((*(*TWhereOrSet)(unsafe.Pointer(bp + 504))).Fn)) { break } _whereOrInsert(tls, bp+448, (*(*TWhereOrCost)(unsafe.Pointer(bp + 560 + 8 + uintptr(i)*16))).Fprereq|(*(*TWhereOrCost)(unsafe.Pointer(bp + 504 + 8 + uintptr(j)*16))).Fprereq, _sqlite3LogEstAdd(tls, (*(*TWhereOrCost)(unsafe.Pointer(bp + 560 + 8 + uintptr(i)*16))).FrRun, (*(*TWhereOrCost)(unsafe.Pointer(bp + 504 + 8 + uintptr(j)*16))).FrRun), _sqlite3LogEstAdd(tls, (*(*TWhereOrCost)(unsafe.Pointer(bp + 560 + 8 + uintptr(i)*16))).FnOut, (*(*TWhereOrCost)(unsafe.Pointer(bp + 504 + 8 + uintptr(j)*16))).FnOut)) goto _4 _4: ; j++ } goto _3 _3: ; i++ } } } goto _2 _2: ; pOrTerm += 48 } (*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm = uint16(1) *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm)) = pTerm (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(WHERE_MULTI_OR) (*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup = 0 (*TWhereLoop)(unsafe.Pointer(pNew)).FiSortIdx = uint8(0) libc.Xmemset(tls, pNew+24, 0, uint32(16)) i = 0 for { if !(rc == SQLITE_OK && i < int32((*(*TWhereOrSet)(unsafe.Pointer(bp + 448))).Fn)) { break } /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs ** of all sub-scans required by the OR-scan. However, due to rounding ** errors, it may be that the cost of the OR-scan is equal to its ** most expensive sub-scan. Add the smallest possible penalty ** (equivalent to multiplying the cost by 1.07) to ensure that ** this does not happen. Otherwise, for WHERE clauses such as the ** following where there is an index on "y": ** ** WHERE likelihood(x=?, 0.99) OR y=? ** ** the planner may elect to "OR" together a full-table scan and an ** index lookup. And other similarly odd results. */ (*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = int16(int32((*(*TWhereOrCost)(unsafe.Pointer(bp + 448 + 8 + uintptr(i)*16))).FrRun) + int32(1)) (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = (*(*TWhereOrCost)(unsafe.Pointer(bp + 448 + 8 + uintptr(i)*16))).FnOut (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq = (*(*TWhereOrCost)(unsafe.Pointer(bp + 448 + 8 + uintptr(i)*16))).Fprereq rc = _whereLoopInsert(tls, pBuilder, pNew) goto _5 _5: ; i++ } } goto _1 _1: ; pTerm += 48 } return rc } // C documentation // // /* // ** Add all WhereLoop objects for all tables // */ func _whereLoopAddAll(tls *libc.TLS, pBuilder uintptr) (r int32) { var bFirstPastRJ, hasRightJoin, iTab, rc int32 var db, p, pEnd, pItem, pNew, pTabList, pWInfo uintptr var mPrereq, mPrior, mUnusable TBitmask _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bFirstPastRJ, db, hasRightJoin, iTab, mPrereq, mPrior, mUnusable, p, pEnd, pItem, pNew, pTabList, pWInfo, rc pWInfo = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo mPrereq = uint64(0) mPrior = uint64(0) pTabList = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList pEnd = pTabList + 8 + uintptr((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)*72 db = (*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse)).Fdb rc = SQLITE_OK bFirstPastRJ = 0 hasRightJoin = 0 /* Loop over the tables in the join, from left to right */ pNew = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew /* Verify that pNew has already been initialized */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FiPlanLimit = uint32(SQLITE_QUERY_PLANNER_LIMIT) iTab = 0 pItem = pTabList + 8 for { if !(pItem < pEnd) { break } mUnusable = uint64(0) (*TWhereLoop)(unsafe.Pointer(pNew)).FiTab = uint8(iTab) *(*uint32)(unsafe.Pointer(pBuilder + 28)) += uint32(SQLITE_QUERY_PLANNER_LIMIT_INCR) (*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf = _sqlite3WhereGetMask(tls, pWInfo+496, (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor) if bFirstPastRJ != 0 || int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_OUTER)|libc.Int32FromInt32(JT_CROSS)|libc.Int32FromInt32(JT_LTORJ)) != 0 { /* Add prerequisites to prevent reordering of FROM clause terms ** across CROSS joins and outer joins. The bFirstPastRJ boolean ** prevents the right operand of a RIGHT JOIN from being swapped with ** other elements even further to the right. ** ** The JT_LTORJ case and the hasRightJoin flag work together to ** prevent FROM-clause terms from moving from the right side of ** a LEFT JOIN over to the left side of that join if the LEFT JOIN ** is itself on the left side of a RIGHT JOIN. */ if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_LTORJ) != 0 { hasRightJoin = int32(1) } mPrereq |= mPrior bFirstPastRJ = libc.BoolInt32(int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_RIGHT) != 0) } else { if !(hasRightJoin != 0) { mPrereq = uint64(0) } } if int32((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpTab)).FeTabType) == int32(TABTYP_VTAB) { p = pItem + 1*72 for { if !(p < pEnd) { break } if mUnusable != 0 || int32((*TSrcItem)(unsafe.Pointer(p)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_OUTER)|libc.Int32FromInt32(JT_CROSS)) != 0 { mUnusable |= _sqlite3WhereGetMask(tls, pWInfo+496, (*TSrcItem)(unsafe.Pointer(p)).FiCursor) } goto _2 _2: ; p += 72 } rc = _whereLoopAddVirtual(tls, pBuilder, mPrereq, mUnusable) } else { rc = _whereLoopAddBtree(tls, pBuilder, mPrereq) } if rc == SQLITE_OK && (*TWhereClause)(unsafe.Pointer((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC)).FhasOr != 0 { rc = _whereLoopAddOr(tls, pBuilder, mPrereq, mUnusable) } mPrior |= (*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf if rc != 0 || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { if rc == int32(SQLITE_DONE) { /* We hit the query planner search limit set by iPlanLimit */ Xsqlite3_log(tls, int32(SQLITE_WARNING), __ccgo_ts+22457, 0) rc = SQLITE_OK } else { break } } goto _1 _1: ; iTab++ pItem += 72 } _whereLoopClear(tls, db, pNew) return rc } // C documentation // // /* // ** Examine a WherePath (with the addition of the extra WhereLoop of the 6th // ** parameters) to see if it outputs rows in the requested ORDER BY // ** (or GROUP BY) without requiring a separate sort operation. Return N: // ** // ** N>0: N terms of the ORDER BY clause are satisfied // ** N==0: No terms of the ORDER BY clause are satisfied // ** N<0: Unknown yet how many terms of ORDER BY might be satisfied. // ** // ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as // ** strict. With GROUP BY and DISTINCT the only requirement is that // ** equivalent rows appear immediately adjacent to one another. GROUP BY // ** and DISTINCT do not require rows to appear in any particular order as long // ** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT // ** the pOrderBy terms can be matched in any order. With ORDER BY, the // ** pOrderBy terms must be matched in strict left-to-right order. // */ func _wherePathSatisfiesOrderBy(tls *libc.TLS, pWInfo uintptr, pOrderBy uintptr, pPath uintptr, wctrlFlags Tu16, nLoop Tu16, pLast uintptr, pRevMask uintptr) (r Ti8) { var bOnce, distinctColumns, isMatch, isOrderDistinct, rev, revIdx, revSet, v5 Tu8 var db, p, pColl, pColl1, pColl2, pIndex, pIxExpr, pLoop, pOBExpr, pParse, pTerm, pX, v4 uintptr var eOp, eqOpMask, nColumn, nKeyCol, nOrderBy Tu16 var i, iColumn, iCur, iLoop, j int32 var m, mTerm, obDone, obSat, orderDistinctMask, ready TBitmask var v11 uint64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bOnce, db, distinctColumns, eOp, eqOpMask, i, iColumn, iCur, iLoop, isMatch, isOrderDistinct, j, m, mTerm, nColumn, nKeyCol, nOrderBy, obDone, obSat, orderDistinctMask, p, pColl, pColl1, pColl2, pIndex, pIxExpr, pLoop, pOBExpr, pParse, pTerm, pX, ready, rev, revIdx, revSet, v11, v4, v5 /* A column number within table iCur */ pLoop = uintptr(0) /* The index associated with pLoop */ db = (*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse)).Fdb /* Database connection */ obSat = uint64(0) /* Mask of inner loops */ /* ** We say the WhereLoop is "one-row" if it generates no more than one ** row of output. A WhereLoop is one-row if all of the following are true: ** (a) All index columns match with WHERE_COLUMN_EQ. ** (b) The index is unique ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. ** ** We say the WhereLoop is "order-distinct" if the set of columns from ** that WhereLoop that are in the ORDER BY clause are different for every ** row of the WhereLoop. Every one-row WhereLoop is automatically ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause ** is not order-distinct. To be order-distinct is not quite the same as being ** UNIQUE since a UNIQUE column or index can have multiple rows that ** are NULL and NULL values are equivalent for the purpose of order-distinct. ** To be order-distinct, the columns must be UNIQUE and NOT NULL. ** ** The rowid for a table is always UNIQUE and NOT NULL so whenever the ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is ** automatically order-distinct. */ _ = libc.Int32FromInt32(0) if nLoop != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_OrderByIdxJoin)) != uint32(0) { return 0 } nOrderBy = uint16((*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) if int32(nOrderBy) > int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) { return 0 } /* Cannot optimize overly large ORDER BYs */ isOrderDistinct = uint8(1) obDone = libc.Uint64FromInt32(1)< 0 { ready |= (*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf } if iLoop < int32(nLoop) { pLoop = *(*uintptr)(unsafe.Pointer((*TWherePath)(unsafe.Pointer(pPath)).FaLoop + uintptr(iLoop)*4)) if int32(wctrlFlags)&int32(WHERE_ORDERBY_LIMIT) != 0 { goto _1 } } else { pLoop = pLast } if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_VIRTUALTABLE) != 0 { if (*(*struct { FidxNum int32 F__ccgo4 uint8 FisOrdered Ti8 FomitMask Tu16 FidxStr uintptr FmHandleIn Tu32 })(unsafe.Pointer(pLoop + 24))).FisOrdered != 0 && int32(wctrlFlags)&(libc.Int32FromInt32(WHERE_DISTINCTBY)|libc.Int32FromInt32(WHERE_SORTBYGROUP)) != int32(WHERE_DISTINCTBY) { obSat = obDone } break } else { if int32(wctrlFlags)&int32(WHERE_DISTINCTBY) != 0 { (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnDistinctCol = uint16(0) } } iCur = (*(*TSrcItem)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pLoop)).FiTab)*72))).FiCursor /* Mark off any ORDER BY term X that is a column in the table of ** the current loop for which there is term in the WHERE ** clause of the form X IS NULL or X=? that reference only outer ** loops. */ i = 0 for { if !(i < int32(nOrderBy)) { break } if libc.Uint64FromInt32(1)<= int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm) { goto _2 } } if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) != 0 && int32((*TExpr)(unsafe.Pointer(pOBExpr)).FiColumn) >= 0 { pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse pColl1 = _sqlite3ExprNNCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).FpExpr) pColl2 = _sqlite3ExprCompareCollSeq(tls, pParse, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr) _ = libc.Int32FromInt32(0) if pColl2 == uintptr(0) || _sqlite3StrICmp(tls, (*TCollSeq)(unsafe.Pointer(pColl1)).FzName, (*TCollSeq)(unsafe.Pointer(pColl2)).FzName) != 0 { goto _2 } } obSat |= libc.Uint64FromInt32(1) << i goto _2 _2: ; i++ } if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_ONEROW) == uint32(0) { if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IPK) != 0 { pIndex = uintptr(0) nKeyCol = uint16(0) nColumn = uint16(1) } else { v4 = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex pIndex = v4 if v4 == uintptr(0) || int32(uint32(*(*uint16)(unsafe.Pointer(pIndex + 56))&0x4>>2)) != 0 { return 0 } else { nKeyCol = (*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol nColumn = (*TIndex)(unsafe.Pointer(pIndex)).FnColumn _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* All relevant terms of the index must also be non-NULL in order ** for isOrderDistinct to be true. So the isOrderDistint value ** computed here might be a false positive. Corrections will be ** made at tag-20210426-1 below */ isOrderDistinct = libc.BoolUint8(int32((*TIndex)(unsafe.Pointer(pIndex)).FonError) != OE_None && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_SKIPSCAN) == uint32(0)) } } /* Loop through all columns of the index and deal with the ones ** that are not constrained by == or IN. */ v5 = libc.Uint8FromInt32(0) revSet = v5 rev = v5 distinctColumns = uint8(0) j = 0 for { if !(j < int32(nColumn)) { break } bOnce = uint8(1) /* True to run the ORDER BY search loop */ _ = libc.Int32FromInt32(0) if j < int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq) && j >= int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip) { eOp = (*TWhereTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4)))).FeOperator /* Skip over == and IS and ISNULL terms. (Also skip IN terms when ** doing WHERE_ORDERBY_LIMIT processing). Except, IS and ISNULL ** terms imply that the index is not UNIQUE NOT NULL in which case ** the loop need to be marked as not order-distinct because it can ** have repeated NULL rows. ** ** If the current term is a column of an ((?,?) IN (SELECT...)) ** expression for which the SELECT returns more than one column, ** check that it is the only column used by this loop. Otherwise, ** if it is one of two or more, none of the columns can be ** considered to match an ORDER BY term. */ if int32(eOp)&int32(eqOpMask) != 0 { if int32(eOp)&(libc.Int32FromInt32(WO_ISNULL)|libc.Int32FromInt32(WO_IS)) != 0 { isOrderDistinct = uint8(0) } goto _6 } else { if int32(eOp)&int32(WO_IN) != 0 { /* ALWAYS() justification: eOp is an equality operator due to the ** ju.btree.nEq constraint above. Any equality other ** than WO_IN is captured by the previous "if". So this one ** always has to be WO_IN. */ pX = (*TWhereTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4)))).FpExpr i = j + int32(1) for { if !(i < int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq)) { break } if (*TWhereTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*4)))).FpExpr == pX { _ = libc.Int32FromInt32(0) bOnce = uint8(0) break } goto _7 _7: ; i++ } } } } /* Get the column number in the table (iColumn) and sort order ** (revIdx) for the j-th column of the index. */ if pIndex != 0 { iColumn = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(j)*2))) revIdx = uint8(int32(*(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FaSortOrder + uintptr(j)))) & int32(KEYINFO_ORDER_DESC)) if iColumn == int32((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FpTable)).FiPKey) { iColumn = -int32(1) } } else { iColumn = -int32(1) revIdx = uint8(0) } /* An unconstrained column that might be NULL means that this ** WhereLoop is not well-ordered. tag-20210426-1 */ if isOrderDistinct != 0 { if iColumn >= 0 && j >= int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq) && int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FpTable)).FaCol + uintptr(iColumn)*12 + 4))&0xf>>0)) == 0 { isOrderDistinct = uint8(0) } if iColumn == -int32(2) { isOrderDistinct = uint8(0) } } /* Find the ORDER BY term that corresponds to the j-th column ** of the index and mark that ORDER BY term off */ isMatch = uint8(0) i = 0 for { if !(bOnce != 0 && i < int32(nOrderBy)) { break } if libc.Uint64FromInt32(1)<= -int32(1) { if int32((*TExpr)(unsafe.Pointer(pOBExpr)).Fop) != int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(pOBExpr)).Fop) != int32(TK_AGG_COLUMN) { goto _8 } if (*TExpr)(unsafe.Pointer(pOBExpr)).FiTable != iCur { goto _8 } if int32((*TExpr)(unsafe.Pointer(pOBExpr)).FiColumn) != iColumn { goto _8 } } else { pIxExpr = (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FaColExpr + 8 + uintptr(j)*20))).FpExpr if _sqlite3ExprCompareSkip(tls, pOBExpr, pIxExpr, iCur) != 0 { goto _8 } } if iColumn != -int32(1) { pColl = _sqlite3ExprNNCollSeq(tls, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse, (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).FpExpr) if _sqlite3StrICmp(tls, (*TCollSeq)(unsafe.Pointer(pColl)).FzName, *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FazColl + uintptr(j)*4))) != 0 { goto _8 } } if int32(wctrlFlags)&int32(WHERE_DISTINCTBY) != 0 { (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnDistinctCol = uint16(j + int32(1)) } isMatch = uint8(1) break goto _8 _8: ; i++ } if isMatch != 0 && int32(wctrlFlags)&int32(WHERE_GROUPBY) == 0 { /* Make sure the sort order is compatible in an ORDER BY clause. ** Sort order is irrelevant for a GROUP BY clause. */ if revSet != 0 { if int32(rev)^int32(revIdx) != int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_DESC) { isMatch = uint8(0) } } else { rev = uint8(int32(revIdx) ^ int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_DESC)) if rev != 0 { *(*TBitmask)(unsafe.Pointer(pRevMask)) |= libc.Uint64FromInt32(1) << iLoop } revSet = uint8(1) } } if isMatch != 0 && int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_BIGNULL) != 0 { if j == int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq) { *(*Tu32)(unsafe.Pointer(pLoop + 40)) |= uint32(WHERE_BIGNULL_SORT) } else { isMatch = uint8(0) } } if isMatch != 0 { if iColumn == -int32(1) { distinctColumns = uint8(1) } obSat |= libc.Uint64FromInt32(1) << i } else { /* No match found */ if j == 0 || j < int32(nKeyCol) { isOrderDistinct = uint8(0) } break } goto _6 _6: ; j++ } /* end Loop over all index columns */ if distinctColumns != 0 { isOrderDistinct = uint8(1) } } /* end-if not one-row */ /* Mark off any other ORDER BY terms that reference pLoop */ if isOrderDistinct != 0 { orderDistinctMask |= (*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf i = 0 for { if !(i < int32(nOrderBy)) { break } if libc.Uint64FromInt32(1)< 0) { break } if i < int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) { v11 = libc.Uint64FromInt32(1)<> 3)) } // C documentation // // /* // ** Return the cost of sorting nRow rows, assuming that the keys have // ** nOrderby columns and that the first nSorted columns are already in // ** order. // */ func _whereSortingCost(tls *libc.TLS, pWInfo uintptr, nRow TLogEst, nOrderBy int32, nSorted int32) (r TLogEst) { var nCol, rSortCost TLogEst _, _ = nCol, rSortCost _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* TUNING: sorting cost proportional to the number of output columns: */ nCol = _sqlite3LogEst(tls, uint64(((*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect)).FpEList)).FnExpr+int32(59))/int32(30))) rSortCost = int16(int32(nRow) + int32(nCol)) if nSorted > 0 { /* Scale the result by (Y/X) */ rSortCost = TLogEst(int32(rSortCost) + (int32(_sqlite3LogEst(tls, uint64((nOrderBy-nSorted)*int32(100)/nOrderBy))) - libc.Int32FromInt32(66))) } /* Multiple by log(M) where M is the number of output rows. ** Use the LIMIT for M if it is smaller. Or if this sort is for ** a DISTINCT operator, M will be the number of distinct output ** rows, so fudge it downwards a bit. */ if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_USE_LIMIT) != 0 { rSortCost = TLogEst(int32(rSortCost) + libc.Int32FromInt32(10)) /* TUNING: Extra 2.0x if using LIMIT */ if nSorted != 0 { rSortCost = TLogEst(int32(rSortCost) + libc.Int32FromInt32(6)) /* TUNING: Extra 1.5x if also using partial sort */ } if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FiLimit) < int32(nRow) { nRow = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiLimit } } else { if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_WANT_DISTINCT) != 0 { /* TUNING: In the sort for a DISTINCT operator, assume that the DISTINCT ** reduces the number of output rows by a factor of 2 */ if int32(nRow) > int32(10) { nRow = TLogEst(int32(nRow) - libc.Int32FromInt32(10)) _ = libc.Int32FromInt32(0) } } } rSortCost = TLogEst(int32(rSortCost) + int32(_estLog(tls, nRow))) return rSortCost } // C documentation // // /* // ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine // ** attempts to find the lowest cost path that visits each WhereLoop // ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. // ** // ** Assume that the total number of output rows that will need to be sorted // ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting // ** costs if nRowEst==0. // ** // ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation // ** error occurs. // */ func _wherePathSolver(tls *libc.TLS, pWInfo uintptr, nRowEst TLogEst) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var aFrom, aSortCost, aTo, pFrom, pLevel, pParse, pSpace, pTo, pWLoop, pX, v14 uintptr var iLoop, ii, jj, mxChoice, mxI, nFrom, nLoop, nOrder, nOrderBy, nSpace, nTo, rc, rc1, v1, v10, v2, v4, v5 int32 var isOrdered Ti8 var maskNew TBitmask var mxCost, mxUnsorted, nOut, rCost, rUnsorted TLogEst var wsFlags Tu32 var _ /* m at bp+16 */ TBitmask var _ /* notUsed at bp+8 */ TBitmask var _ /* revMask at bp+0 */ TBitmask var _ /* revMask at bp+24 */ TBitmask _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aFrom, aSortCost, aTo, iLoop, ii, isOrdered, jj, maskNew, mxChoice, mxCost, mxI, mxUnsorted, nFrom, nLoop, nOrder, nOrderBy, nOut, nSpace, nTo, pFrom, pLevel, pParse, pSpace, pTo, pWLoop, pX, rCost, rUnsorted, rc, rc1, wsFlags, v1, v10, v14, v2, v4, v5 /* Loop counters */ mxI = 0 /* Number of ORDER BY clause terms */ mxCost = 0 /* Maximum cost of a set of paths */ mxUnsorted = 0 /* Used to divy up the pSpace memory */ aSortCost = uintptr(0) /* Bytes of space allocated at pSpace */ pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse nLoop = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) /* TUNING: For simple queries, only the best path is tracked. ** For 2-way joins, the 5 best paths are followed. ** For joins of 3 or more tables, track the 10 best paths */ if nLoop <= int32(1) { v1 = int32(1) } else { if nLoop == int32(2) { v2 = int32(5) } else { v2 = int32(10) } v1 = v2 } mxChoice = v1 _ = libc.Int32FromInt32(0) /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this ** case the purpose of this call is to estimate the number of rows returned ** by the overall query. Once this estimate has been obtained, the caller ** will invoke this function a second time, passing the estimate as the ** nRowEst parameter. */ if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy == uintptr(0) || int32(nRowEst) == 0 { nOrderBy = 0 } else { nOrderBy = (*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)).FnExpr } /* Allocate and initialize space for aTo, aFrom and aSortCost[] */ nSpace = int32((uint32(32) + uint32(4)*uint32(nLoop)) * uint32(mxChoice) * uint32(2)) nSpace = int32(uint32(nSpace) + libc.Uint32FromInt64(2)*uint32(nOrderBy)) pSpace = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(nSpace)) if pSpace == uintptr(0) { return int32(SQLITE_NOMEM) } aTo = pSpace aFrom = aTo + uintptr(mxChoice)*32 libc.Xmemset(tls, aFrom, 0, uint32(32)) pX = aFrom + uintptr(mxChoice)*32 ii = mxChoice * int32(2) pFrom = aTo for { if !(ii > 0) { break } (*TWherePath)(unsafe.Pointer(pFrom)).FaLoop = pX goto _3 _3: ; ii-- pFrom += 32 pX += uintptr(nLoop) * 4 } if nOrderBy != 0 { /* If there is an ORDER BY clause and it is not being ignored, set up ** space for the aSortCost[] array. Each element of the aSortCost array ** is either zero - meaning it has not yet been initialized - or the ** cost of sorting nRowEst rows of data where the first X terms of ** the ORDER BY clause are already in order, where X is the array ** index. */ aSortCost = pX libc.Xmemset(tls, aSortCost, 0, uint32(2)*uint32(nOrderBy)) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Seed the search with a single WherePath containing zero WhereLoops. ** ** TUNING: Do not let the number of iterations go above 28. If the cost ** of computing an automatic index is not paid back within the first 28 ** rows, then do not use the automatic index. */ if int32((*TParse)(unsafe.Pointer(pParse)).FnQueryLoop) < int32(48) { v4 = int32((*TParse)(unsafe.Pointer(pParse)).FnQueryLoop) } else { v4 = int32(48) } (*(*TWherePath)(unsafe.Pointer(aFrom))).FnRow = int16(v4) _ = libc.Int32FromInt32(0) nFrom = int32(1) _ = libc.Int32FromInt32(0) if nOrderBy != 0 { /* If nLoop is zero, then there are no FROM terms in the query. Since ** in this case the query may return a maximum of one row, the results ** are already in the requested order. Set isOrdered to nOrderBy to ** indicate this. Or, if nLoop is greater than zero, set isOrdered to ** -1, indicating that the result set may or may not be ordered, ** depending on the loops added to the current plan. */ if nLoop > 0 { v5 = -int32(1) } else { v5 = nOrderBy } (*(*TWherePath)(unsafe.Pointer(aFrom))).FisOrdered = int8(v5) } /* Compute successively longer WherePaths using the previous generation ** of WherePaths as the basis for the next. Keep track of the mxChoice ** best paths at each generation */ iLoop = 0 for { if !(iLoop < nLoop) { break } nTo = 0 ii = 0 pFrom = aFrom for { if !(ii < nFrom) { break } pWLoop = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops for { if !(pWLoop != 0) { break } /* Mask of rev-order loops for (..) */ if (*TWhereLoop)(unsafe.Pointer(pWLoop)).Fprereq & ^(*TWherePath)(unsafe.Pointer(pFrom)).FmaskLoop != uint64(0) { goto _8 } if (*TWhereLoop)(unsafe.Pointer(pWLoop)).FmaskSelf&(*TWherePath)(unsafe.Pointer(pFrom)).FmaskLoop != uint64(0) { goto _8 } if (*TWhereLoop)(unsafe.Pointer(pWLoop)).FwsFlags&uint32(WHERE_AUTO_INDEX) != uint32(0) && int32((*TWherePath)(unsafe.Pointer(pFrom)).FnRow) < int32(3) { /* Do not use an automatic index if the this loop is expected ** to run less than 1.25 times. It is tempting to also exclude ** automatic index usage on an outer loop, but sometimes an automatic ** index is useful in the outer loop of a correlated subquery. */ _ = libc.Int32FromInt32(0) goto _8 } /* At this point, pWLoop is a candidate to be the next loop. ** Compute its cost */ rUnsorted = _sqlite3LogEstAdd(tls, (*TWhereLoop)(unsafe.Pointer(pWLoop)).FrSetup, int16(int32((*TWhereLoop)(unsafe.Pointer(pWLoop)).FrRun)+int32((*TWherePath)(unsafe.Pointer(pFrom)).FnRow))) rUnsorted = _sqlite3LogEstAdd(tls, rUnsorted, (*TWherePath)(unsafe.Pointer(pFrom)).FrUnsorted) nOut = int16(int32((*TWherePath)(unsafe.Pointer(pFrom)).FnRow) + int32((*TWhereLoop)(unsafe.Pointer(pWLoop)).FnOut)) maskNew = (*TWherePath)(unsafe.Pointer(pFrom)).FmaskLoop | (*TWhereLoop)(unsafe.Pointer(pWLoop)).FmaskSelf isOrdered = (*TWherePath)(unsafe.Pointer(pFrom)).FisOrdered if int32(isOrdered) < 0 { *(*TBitmask)(unsafe.Pointer(bp)) = uint64(0) isOrdered = _wherePathSatisfiesOrderBy(tls, pWInfo, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy, pFrom, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags, uint16(iLoop), pWLoop, bp) } else { *(*TBitmask)(unsafe.Pointer(bp)) = (*TWherePath)(unsafe.Pointer(pFrom)).FrevLoop } if int32(isOrdered) >= 0 && int32(isOrdered) < nOrderBy { if int32(*(*TLogEst)(unsafe.Pointer(aSortCost + uintptr(isOrdered)*2))) == 0 { *(*TLogEst)(unsafe.Pointer(aSortCost + uintptr(isOrdered)*2)) = _whereSortingCost(tls, pWInfo, nRowEst, nOrderBy, int32(isOrdered)) } /* TUNING: Add a small extra penalty (3) to sorting as an ** extra encouragement to the query planner to select a plan ** where the rows emerge in the correct order without any sorting ** required. */ rCost = int16(int32(_sqlite3LogEstAdd(tls, rUnsorted, *(*TLogEst)(unsafe.Pointer(aSortCost + uintptr(isOrdered)*2)))) + int32(3)) } else { rCost = rUnsorted rUnsorted = TLogEst(int32(rUnsorted) - libc.Int32FromInt32(2)) /* TUNING: Slight bias in favor of no-sort plans */ } /* Check to see if pWLoop should be added to the set of ** mxChoice best-so-far paths. ** ** First look for an existing path among best-so-far paths ** that covers the same set of loops and has the same isOrdered ** setting as the current path candidate. ** ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range ** of legal values for isOrdered, -1..64. */ jj = 0 pTo = aTo for { if !(jj < nTo) { break } if (*TWherePath)(unsafe.Pointer(pTo)).FmaskLoop == maskNew && (int32((*TWherePath)(unsafe.Pointer(pTo)).FisOrdered)^int32(isOrdered))&int32(0x80) == 0 { break } goto _9 _9: ; jj++ pTo += 32 } if jj >= nTo { /* None of the existing best-so-far paths match the candidate. */ if nTo >= mxChoice && (int32(rCost) > int32(mxCost) || int32(rCost) == int32(mxCost) && int32(rUnsorted) >= int32(mxUnsorted)) { /* The current candidate is no better than any of the mxChoice ** paths currently in the best-so-far buffer. So discard ** this candidate as not viable. */ goto _8 } /* If we reach this points it means that the new candidate path ** needs to be added to the set of best-so-far paths. */ if nTo < mxChoice { /* Increase the size of the aTo set by one */ v10 = nTo nTo++ jj = v10 } else { /* New path replaces the prior worst to keep count below mxChoice */ jj = mxI } pTo = aTo + uintptr(jj)*32 } else { /* Control reaches here if best-so-far path pTo=aTo[jj] covers the ** same set of loops and has the same isOrdered setting as the ** candidate path. Check to see if the candidate should replace ** pTo or if the candidate should be skipped. ** ** The conditional is an expanded vector comparison equivalent to: ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted) */ if int32((*TWherePath)(unsafe.Pointer(pTo)).FrCost) < int32(rCost) || int32((*TWherePath)(unsafe.Pointer(pTo)).FrCost) == int32(rCost) && (int32((*TWherePath)(unsafe.Pointer(pTo)).FnRow) < int32(nOut) || int32((*TWherePath)(unsafe.Pointer(pTo)).FnRow) == int32(nOut) && int32((*TWherePath)(unsafe.Pointer(pTo)).FrUnsorted) <= int32(rUnsorted)) { /* Discard the candidate path from further consideration */ goto _8 } /* Control reaches here if the candidate path is better than the ** pTo path. Replace pTo with the candidate. */ } /* pWLoop is a winner. Add it to the set of best so far */ (*TWherePath)(unsafe.Pointer(pTo)).FmaskLoop = (*TWherePath)(unsafe.Pointer(pFrom)).FmaskLoop | (*TWhereLoop)(unsafe.Pointer(pWLoop)).FmaskSelf (*TWherePath)(unsafe.Pointer(pTo)).FrevLoop = *(*TBitmask)(unsafe.Pointer(bp)) (*TWherePath)(unsafe.Pointer(pTo)).FnRow = nOut (*TWherePath)(unsafe.Pointer(pTo)).FrCost = rCost (*TWherePath)(unsafe.Pointer(pTo)).FrUnsorted = rUnsorted (*TWherePath)(unsafe.Pointer(pTo)).FisOrdered = isOrdered libc.Xmemcpy(tls, (*TWherePath)(unsafe.Pointer(pTo)).FaLoop, (*TWherePath)(unsafe.Pointer(pFrom)).FaLoop, uint32(4)*uint32(iLoop)) *(*uintptr)(unsafe.Pointer((*TWherePath)(unsafe.Pointer(pTo)).FaLoop + uintptr(iLoop)*4)) = pWLoop if nTo >= mxChoice { mxI = 0 mxCost = (*(*TWherePath)(unsafe.Pointer(aTo))).FrCost mxUnsorted = (*(*TWherePath)(unsafe.Pointer(aTo))).FnRow jj = int32(1) pTo = aTo + 1*32 for { if !(jj < mxChoice) { break } if int32((*TWherePath)(unsafe.Pointer(pTo)).FrCost) > int32(mxCost) || int32((*TWherePath)(unsafe.Pointer(pTo)).FrCost) == int32(mxCost) && int32((*TWherePath)(unsafe.Pointer(pTo)).FrUnsorted) > int32(mxUnsorted) { mxCost = (*TWherePath)(unsafe.Pointer(pTo)).FrCost mxUnsorted = (*TWherePath)(unsafe.Pointer(pTo)).FrUnsorted mxI = jj } goto _11 _11: ; jj++ pTo += 32 } } goto _8 _8: ; pWLoop = (*TWhereLoop)(unsafe.Pointer(pWLoop)).FpNextLoop } goto _7 _7: ; ii++ pFrom += 32 } /* Swap the roles of aFrom and aTo for the next generation */ pFrom = aTo aTo = aFrom aFrom = pFrom nFrom = nTo goto _6 _6: ; iLoop++ } if nFrom == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22492, 0) _sqlite3DbFreeNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSpace) return int32(SQLITE_ERROR) } /* Find the lowest cost path. pFrom will be left pointing to that path */ pFrom = aFrom ii = int32(1) for { if !(ii < nFrom) { break } if int32((*TWherePath)(unsafe.Pointer(pFrom)).FrCost) > int32((*(*TWherePath)(unsafe.Pointer(aFrom + uintptr(ii)*32))).FrCost) { pFrom = aFrom + uintptr(ii)*32 } goto _12 _12: ; ii++ } _ = libc.Int32FromInt32(0) /* Load the lowest cost path into pWInfo */ iLoop = 0 for { if !(iLoop < nLoop) { break } pLevel = pWInfo + 760 + uintptr(iLoop)*88 v14 = *(*uintptr)(unsafe.Pointer((*TWherePath)(unsafe.Pointer(pFrom)).FaLoop + uintptr(iLoop)*4)) pWLoop = v14 (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop = v14 (*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom = (*TWhereLoop)(unsafe.Pointer(pWLoop)).FiTab (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur = (*(*TSrcItem)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*72))).FiCursor goto _13 _13: ; iLoop++ } if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_WANT_DISTINCT) != 0 && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_DISTINCTBY) == 0 && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct) == WHERE_DISTINCT_NOOP && nRowEst != 0 { rc = int32(_wherePathSatisfiesOrderBy(tls, pWInfo, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpResultSet, pFrom, uint16(WHERE_DISTINCTBY), uint16(nLoop-int32(1)), *(*uintptr)(unsafe.Pointer((*TWherePath)(unsafe.Pointer(pFrom)).FaLoop + uintptr(nLoop-int32(1))*4)), bp+8)) if rc == (*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpResultSet)).FnExpr { (*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct = uint8(WHERE_DISTINCT_ORDERED) } } libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(0), 2, 0x4) if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy != 0 { (*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat = (*TWherePath)(unsafe.Pointer(pFrom)).FisOrdered if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_DISTINCTBY) != 0 { if int32((*TWherePath)(unsafe.Pointer(pFrom)).FisOrdered) == (*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)).FnExpr { (*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct = uint8(WHERE_DISTINCT_ORDERED) } if (*TSelect)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect)).FpOrderBy != 0 && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat) > (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect)).FpOrderBy)).FnExpr { (*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat = int8((*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect)).FpOrderBy)).FnExpr) } } else { (*TWhereInfo)(unsafe.Pointer(pWInfo)).FrevMask = (*TWherePath)(unsafe.Pointer(pFrom)).FrevLoop if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat) <= 0 { (*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat = 0 if nLoop > 0 { wsFlags = (*TWhereLoop)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWherePath)(unsafe.Pointer(pFrom)).FaLoop + uintptr(nLoop-int32(1))*4)))).FwsFlags if wsFlags&uint32(WHERE_ONEROW) == uint32(0) && wsFlags&uint32(libc.Int32FromInt32(WHERE_IPK)|libc.Int32FromInt32(WHERE_COLUMN_IN)) != uint32(libc.Int32FromInt32(WHERE_IPK)|libc.Int32FromInt32(WHERE_COLUMN_IN)) { *(*TBitmask)(unsafe.Pointer(bp + 16)) = uint64(0) rc1 = int32(_wherePathSatisfiesOrderBy(tls, pWInfo, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy, pFrom, uint16(WHERE_ORDERBY_LIMIT), uint16(nLoop-int32(1)), *(*uintptr)(unsafe.Pointer((*TWherePath)(unsafe.Pointer(pFrom)).FaLoop + uintptr(nLoop-int32(1))*4)), bp+16)) if rc1 == (*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)).FnExpr { libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(1), 2, 0x4) (*TWhereInfo)(unsafe.Pointer(pWInfo)).FrevMask = *(*TBitmask)(unsafe.Pointer(bp + 16)) } } } } else { if nLoop != 0 && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat) == int32(1) && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&(libc.Int32FromInt32(WHERE_ORDERBY_MIN)|libc.Int32FromInt32(WHERE_ORDERBY_MAX)) != 0 { libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(1), 2, 0x4) } } } if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_SORTBYGROUP) != 0 && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat) == (*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)).FnExpr && nLoop > 0 { *(*TBitmask)(unsafe.Pointer(bp + 24)) = uint64(0) nOrder = int32(_wherePathSatisfiesOrderBy(tls, pWInfo, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy, pFrom, uint16(0), uint16(nLoop-int32(1)), *(*uintptr)(unsafe.Pointer((*TWherePath)(unsafe.Pointer(pFrom)).FaLoop + uintptr(nLoop-int32(1))*4)), bp+24)) _ = libc.Int32FromInt32(0) if nOrder == (*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)).FnExpr { libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(1), 3, 0x8) (*TWhereInfo)(unsafe.Pointer(pWInfo)).FrevMask = *(*TBitmask)(unsafe.Pointer(bp + 24)) } } } (*TWhereInfo)(unsafe.Pointer(pWInfo)).FnRowOut = (*TWherePath)(unsafe.Pointer(pFrom)).FnRow /* Free temporary memory and return success */ _sqlite3DbFreeNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSpace) return SQLITE_OK } // C documentation // // /* // ** Most queries use only a single table (they are not joins) and have // ** simple == constraints against indexed fields. This routine attempts // ** to plan those simple cases using much less ceremony than the // ** general-purpose query planner, and thereby yield faster sqlite3_prepare() // ** times for the common case. // ** // ** Return non-zero on success, if this query can be handled by this // ** no-frills query planner. Return zero if this query needs the // ** general-purpose query planner. // */ func _whereShortCut(tls *libc.TLS, pBuilder uintptr) (r int32) { bp := tls.Alloc(96) defer tls.Free(96) var iCur, j, opMask, v2 int32 var pIdx, pItem, pLoop, pTab, pTerm, pWC, pWInfo uintptr var _ /* scan at bp+0 */ TWhereScan _, _, _, _, _, _, _, _, _, _, _ = iCur, j, opMask, pIdx, pItem, pLoop, pTab, pTerm, pWC, pWInfo, v2 pWInfo = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_OR_SUBCLAUSE) != 0 { return 0 } _ = libc.Int32FromInt32(0) pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { return 0 } if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x2>>1)) != 0 || int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x1>>0)) != 0 { return 0 } iCur = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor pWC = pWInfo + 80 pLoop = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags = uint32(0) (*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip = uint16(0) pTerm = _whereScanInit(tls, bp, pWC, iCur, -int32(1), uint32(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)), uintptr(0)) for pTerm != 0 && (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight != 0 { pTerm = _whereScanNext(tls, bp) } if pTerm != 0 { (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags = uint32(libc.Int32FromInt32(WHERE_COLUMN_EQ) | libc.Int32FromInt32(WHERE_IPK) | libc.Int32FromInt32(WHERE_ONEROW)) *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm)) = pTerm (*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm = uint16(1) (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq = uint16(1) /* TUNING: Cost of a rowid lookup is 10 */ (*TWhereLoop)(unsafe.Pointer(pLoop)).FrRun = int16(33) /* 33==sqlite3LogEst(10) */ } else { pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex for { if !(pIdx != 0) { break } _ = libc.Int32FromInt32(0) if !(int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != libc.Int32FromInt32(OE_None)) || (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != uintptr(0) || int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) > int32(libc.Uint32FromInt64(12)/libc.Uint32FromInt64(4)) { goto _1 } if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x8>>3)) != 0 { v2 = libc.Int32FromInt32(WO_EQ) | libc.Int32FromInt32(WO_IS) } else { v2 = int32(WO_EQ) } opMask = v2 j = 0 for { if !(j < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) { break } pTerm = _whereScanInit(tls, bp, pWC, iCur, j, uint32(opMask), pIdx) for pTerm != 0 && (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight != 0 { pTerm = _whereScanNext(tls, bp) } if pTerm == uintptr(0) { break } *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4)) = pTerm goto _3 _3: ; j++ } if j != int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) { goto _1 } (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags = uint32(libc.Int32FromInt32(WHERE_COLUMN_EQ) | libc.Int32FromInt32(WHERE_ONEROW) | libc.Int32FromInt32(WHERE_INDEXED)) if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x20>>5)) != 0 || (*TSrcItem)(unsafe.Pointer(pItem)).FcolUsed&(*TIndex)(unsafe.Pointer(pIdx)).FcolNotIdxed == uint64(0) { *(*Tu32)(unsafe.Pointer(pLoop + 40)) |= uint32(WHERE_IDX_ONLY) } (*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm = uint16(j) (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq = uint16(j) (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex = pIdx /* TUNING: Cost of a unique index lookup is 15 */ (*TWhereLoop)(unsafe.Pointer(pLoop)).FrRun = int16(39) /* 39==sqlite3LogEst(15) */ break goto _1 _1: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } } if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags != 0 { (*TWhereLoop)(unsafe.Pointer(pLoop)).FnOut = libc.Int16FromInt32(1) (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760))).FpWLoop = pLoop _ = libc.Int32FromInt32(0) (*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf = uint64(1) /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */ (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760))).FiTabCur = iCur (*TWhereInfo)(unsafe.Pointer(pWInfo)).FnRowOut = int16(1) if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy != 0 { (*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat = int8((*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)).FnExpr) } if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_WANT_DISTINCT) != 0 { (*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct = uint8(WHERE_DISTINCT_UNIQUE) } if int32((*(*TWhereScan)(unsafe.Pointer(bp))).FiEquiv) > int32(1) { *(*Tu32)(unsafe.Pointer(pLoop + 40)) |= uint32(WHERE_TRANSCONS) } return int32(1) } return 0 } // C documentation // // /* // ** Helper function for exprIsDeterministic(). // */ func _exprNodeIsDeterministic(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_FUNCTION) && libc.BoolInt32((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_ConstFunc)) != uint32(0)) == 0 { (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0) return int32(WRC_Abort) } return WRC_Continue } // C documentation // // /* // ** Return true if the expression contains no non-deterministic SQL // ** functions. Do not consider non-deterministic SQL functions that are // ** part of sub-select statements. // */ func _exprIsDeterministic(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* w at bp+0 */ TWalker libc.Xmemset(tls, bp, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp))).FeCode = uint16(1) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_exprNodeIsDeterministic) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkFail) _sqlite3WalkExpr(tls, bp, p) return int32((*(*TWalker)(unsafe.Pointer(bp))).FeCode) } // C documentation // // /* Attempt to omit tables from a join that do not affect the result. // ** For a table to not affect the result, the following must be true: // ** // ** 1) The query must not be an aggregate. // ** 2) The table must be the RHS of a LEFT JOIN. // ** 3) Either the query must be DISTINCT, or else the ON or USING clause // ** must contain a constraint that limits the scan of the table to // ** at most a single row. // ** 4) The table must not be referenced by any part of the query apart // ** from its own USING or ON clause. // ** 5) The table must not have an inner-join ON or USING clause if there is // ** a RIGHT JOIN anywhere in the query. Otherwise the ON/USING clause // ** might move from the right side to the left side of the RIGHT JOIN. // ** Note: Due to (2), this condition can only arise if the table is // ** the right-most table of a subquery that was flattened into the // ** main query and that subquery was the right-hand operand of an // ** inner join that held an ON or USING clause. // ** // ** For example, given: // ** // ** CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1); // ** CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2); // ** CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3); // ** // ** then table t2 can be omitted from the following: // ** // ** SELECT v1, v3 FROM t1 // ** LEFT JOIN t2 ON (t1.ipk=t2.ipk) // ** LEFT JOIN t3 ON (t1.ipk=t3.ipk) // ** // ** or from: // ** // ** SELECT DISTINCT v1, v3 FROM t1 // ** LEFT JOIN t2 // ** LEFT JOIN t3 ON (t1.ipk=t3.ipk) // */ func _whereOmitNoopJoin(tls *libc.TLS, pWInfo uintptr, notReady TBitmask) (r TBitmask) { var hasRightJoin, i, nByte int32 var pEnd, pItem, pLoop, pTerm, p4 uintptr var tabUsed TBitmask _, _, _, _, _, _, _, _, _ = hasRightJoin, i, nByte, pEnd, pItem, pLoop, pTerm, tabUsed, p4 /* Preconditions checked by the caller */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* These two preconditions checked by the caller combine to guarantee ** condition (1) of the header comment */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) tabUsed = _sqlite3WhereExprListUsage(tls, pWInfo+496, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpResultSet) if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy != 0 { tabUsed |= _sqlite3WhereExprListUsage(tls, pWInfo+496, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy) } hasRightJoin = libc.BoolInt32(int32((*(*TSrcItem)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0) i = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) - int32(1) for { if !(i >= int32(1)) { break } pLoop = (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(i)*88))).FpWLoop pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pLoop)).FiTab)*72 if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_RIGHT)) != int32(JT_LEFT) { goto _1 } if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_WANT_DISTINCT) == 0 && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_ONEROW) == uint32(0) { goto _1 } if tabUsed&(*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf != uint64(0) { goto _1 } pEnd = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FsWC.Fa + uintptr((*TWhereInfo)(unsafe.Pointer(pWInfo)).FsWC.FnTerm)*48 pTerm = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FsWC.Fa for { if !(pTerm < pEnd) { break } if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll&(*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf != uint64(0) { if !((*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != libc.Uint32FromInt32(0)) || *(*int32)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr + 36)) != (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor { break } } if hasRightJoin != 0 && (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_InnerON)) != uint32(0) && *(*int32)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr + 36)) == (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor { break /* restriction (5) */ } goto _2 _2: ; pTerm += 48 } if pTerm < pEnd { goto _1 } notReady &= ^(*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf pTerm = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FsWC.Fa for { if !(pTerm < pEnd) { break } if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll&(*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf != uint64(0) { p4 = pTerm + 10 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | libc.Int32FromInt32(TERM_CODED)) } goto _3 _3: ; pTerm += 48 } if i != int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)-int32(1) { nByte = int32(uint32(int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)-libc.Int32FromInt32(1)-i) * uint32(88)) libc.Xmemmove(tls, pWInfo+760+uintptr(i)*88, pWInfo+760+uintptr(i+int32(1))*88, uint32(nByte)) } (*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel-- _ = libc.Int32FromInt32(0) goto _1 _1: ; i-- } return notReady } // C documentation // // /* // ** Check to see if there are any SEARCH loops that might benefit from // ** using a Bloom filter. Consider a Bloom filter if: // ** // ** (1) The SEARCH happens more than N times where N is the number // ** of rows in the table that is being considered for the Bloom // ** filter. // ** (2) Some searches are expected to find zero rows. (This is determined // ** by the WHERE_SELFCULL flag on the term.) // ** (3) Bloom-filter processing is not disabled. (Checked by the // ** caller.) // ** (4) The size of the table being searched is known by ANALYZE. // ** // ** This block of code merely checks to see if a Bloom filter would be // ** appropriate, and if so sets the WHERE_BLOOMFILTER flag on the // ** WhereLoop. The implementation of the Bloom filter comes further // ** down where the code for each WhereLoop is generated. // */ func _whereCheckIfBloomFilterIsUseful(tls *libc.TLS, pWInfo uintptr) { var i int32 var nSearch TLogEst var pItem, pLoop, pTab uintptr var reqFlags uint32 _, _, _, _, _, _ = i, nSearch, pItem, pLoop, pTab, reqFlags nSearch = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) i = 0 for { if !(i < int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)) { break } pLoop = (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(i)*88))).FpWLoop reqFlags = uint32(libc.Int32FromInt32(WHERE_SELFCULL) | libc.Int32FromInt32(WHERE_COLUMN_EQ)) pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pLoop)).FiTab)*72 pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasStat1) == uint32(0) { break } *(*Tu32)(unsafe.Pointer(pTab + 28)) |= uint32(TF_StatsUsed) if i >= int32(1) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&reqFlags == reqFlags && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_IPK)|libc.Int32FromInt32(WHERE_INDEXED)) != uint32(0) { if int32(nSearch) > int32((*TTable)(unsafe.Pointer(pTab)).FnRowLogEst) { *(*Tu32)(unsafe.Pointer(pLoop + 40)) |= uint32(WHERE_BLOOMFILTER) *(*Tu32)(unsafe.Pointer(pLoop + 40)) &= uint32(^libc.Int32FromInt32(WHERE_IDX_ONLY)) } } nSearch = TLogEst(int32(nSearch) + int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnOut)) goto _1 _1: ; i++ } } // C documentation // // /* // ** The index pIdx is used by a query and contains one or more expressions. // ** In other words pIdx is an index on an expression. iIdxCur is the cursor // ** number for the index and iDataCur is the cursor number for the corresponding // ** table. // ** // ** This routine adds IndexedExpr entries to the Parse->pIdxEpr field for // ** each of the expressions in the index so that the expression code generator // ** will know to replace occurrences of the indexed expression with // ** references to the corresponding column of the index. // */ func _whereAddIndexedExpr(tls *libc.TLS, pParse uintptr, pIdx uintptr, iIdxCur int32, pTabItem uintptr) { var db, p, pArg, pDef, pExpr, pTab uintptr var i, j, n, v2 int32 _, _, _, _, _, _, _, _, _, _ = db, i, j, n, p, pArg, pDef, pExpr, pTab, v2 _ = libc.Int32FromInt32(0) pTab = (*TIndex)(unsafe.Pointer(pIdx)).FpTable i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) { break } j = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))) if j == -int32(2) { pExpr = (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(i)*20))).FpExpr } else { if j >= 0 && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 { pExpr = _sqlite3ColumnExpr(tls, pTab, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(j)*12) } else { goto _1 } } if _sqlite3ExprIsConstant(tls, pExpr) != 0 { goto _1 } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_FUNCTION) { db = (*TParse)(unsafe.Pointer(pParse)).Fdb _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(pExpr + 20)) != 0 { v2 = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr } else { v2 = 0 } n = v2 pDef = _sqlite3FindFunction(tls, db, *(*uintptr)(unsafe.Pointer(pExpr + 8)), n, (*Tsqlite3)(unsafe.Pointer(db)).Fenc, uint8(0)) if pDef == uintptr(0) || (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_RESULT_SUBTYPE) != uint32(0) { goto _1 } } p = _sqlite3DbMallocRaw(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(24)) if p == uintptr(0) { break } (*TIndexedExpr)(unsafe.Pointer(p)).FpIENext = (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr (*TIndexedExpr)(unsafe.Pointer(p)).FpExpr = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr, 0) (*TIndexedExpr)(unsafe.Pointer(p)).FiDataCur = (*TSrcItem)(unsafe.Pointer(pTabItem)).FiCursor (*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCur = iIdxCur (*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCol = i (*TIndexedExpr)(unsafe.Pointer(p)).FbMaybeNullRow = libc.BoolUint8(int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0) if _sqlite3IndexAffinityStr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pIdx) != 0 { (*TIndexedExpr)(unsafe.Pointer(p)).Faff = *(*uint8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FzColAff + uintptr(i))) } (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr = p if (*TIndexedExpr)(unsafe.Pointer(p)).FpIENext == uintptr(0) { pArg = pParse + 72 _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_whereIndexedExprCleanup), pArg) } goto _1 _1: ; i++ } } // C documentation // // /* // ** Set the reverse-scan order mask to one for all tables in the query // ** with the exception of MATERIALIZED common table expressions that have // ** their own internal ORDER BY clauses. // ** // ** This implements the PRAGMA reverse_unordered_selects=ON setting. // ** (Also SQLITE_DBCONFIG_REVERSE_SCANORDER). // */ func _whereReverseScanOrder(tls *libc.TLS, pWInfo uintptr) { var ii int32 var pItem uintptr _, _ = ii, pItem ii = 0 for { if !(ii < (*TSrcList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList)).FnSrc) { break } pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr(ii)*72 if !(int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 36 + 4))&0x100>>8)) != 0) || int32((*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem + 68)))).FeM10d) != M10d_Yes || (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect == uintptr(0) || (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpSelect)).FpOrderBy == uintptr(0) { *(*TBitmask)(unsafe.Pointer(pWInfo + 72)) |= libc.Uint64FromInt32(1) << ii } goto _1 _1: ; ii++ } } // C documentation // // /* // ** Generate the beginning of the loop used for WHERE clause processing. // ** The return value is a pointer to an opaque structure that contains // ** information needed to terminate the loop. Later, the calling routine // ** should invoke sqlite3WhereEnd() with the return value of this function // ** in order to complete the WHERE clause processing. // ** // ** If an error occurs, this routine returns NULL. // ** // ** The basic idea is to do a nested loop, one loop for each table in // ** the FROM clause of a select. (INSERT and UPDATE statements are the // ** same as a SELECT with only a single table in the FROM clause.) For // ** example, if the SQL is this: // ** // ** SELECT * FROM t1, t2, t3 WHERE ...; // ** // ** Then the code generated is conceptually like the following: // ** // ** foreach row1 in t1 do \ Code generated // ** foreach row2 in t2 do |-- by sqlite3WhereBegin() // ** foreach row3 in t3 do / // ** ... // ** end \ Code generated // ** end |-- by sqlite3WhereEnd() // ** end / // ** // ** Note that the loops might not be nested in the order in which they // ** appear in the FROM clause if a different order is better able to make // ** use of indices. Note also that when the IN operator appears in // ** the WHERE clause, it might result in additional nested loops for // ** scanning through all values on the right-hand side of the IN. // ** // ** There are Btree cursors associated with each table. t1 uses cursor // ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. // ** And so forth. This routine generates code to open those VDBE cursors // ** and sqlite3WhereEnd() generates the code to close them. // ** // ** The code that sqlite3WhereBegin() generates leaves the cursors named // ** in pTabList pointing at their appropriate entries. The [...] code // ** can use OP_Column and OP_Rowid opcodes on these cursors to extract // ** data from the various tables of the loop. // ** // ** If the WHERE clause is empty, the foreach loops must each scan their // ** entire tables. Thus a three-way join is an O(N^3) operation. But if // ** the tables have indices and there are terms in the WHERE clause that // ** refer to those indices, a complete table scan can be avoided and the // ** code will run much faster. Most of the work of this routine is checking // ** to see if there are indices that can be used to speed up the loop. // ** // ** Terms of the WHERE clause are also used to limit which rows actually // ** make it to the "..." in the middle of the loop. After each "foreach", // ** terms of the WHERE clause that use only terms in that loop and outer // ** loops are evaluated and if false a jump is made around all subsequent // ** inner loops (or around the "..." if the test occurs within the inner- // ** most loop) // ** // ** OUTER JOINS // ** // ** An outer join of tables t1 and t2 is conceptually coded as follows: // ** // ** foreach row1 in t1 do // ** flag = 0 // ** foreach row2 in t2 do // ** start: // ** ... // ** flag = 1 // ** end // ** if flag==0 then // ** move the row2 cursor to a null row // ** goto start // ** fi // ** end // ** // ** ORDER BY CLAUSE PROCESSING // ** // ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause // ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement // ** if there is one. If there is no ORDER BY clause or if this routine // ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. // ** // ** The iIdxCur parameter is the cursor number of an index. If // ** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index // ** to use for OR clause processing. The WHERE clause should use this // ** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is // ** the first cursor in an array of cursors for all indices. iIdxCur should // ** be used to compute the appropriate cursor depending on which index is // ** used. // */ func _sqlite3WhereBegin(tls *libc.TLS, pParse uintptr, pTabList uintptr, pWhere uintptr, pOrderBy uintptr, pResultSet uintptr, pSelect uintptr, wctrlFlags Tu16, iAuxArg int32) (r uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var addrExplain, bOnerow, iCur, iDb, iIndexCur, iOnce, ii, n, nByteWInfo, nTabList, op, op1, rc, wsFlags, wsFlags1, v1, v12, v15, v19, v2, v21, v23, v3, v4 int32 var b, notReady TBitmask var bFordelete Tu8 var db, p, pInfo, pIx, pJ, pLevel, pLoop, pMaskSet, pPk, pRJ, pSrc, pT, pTab, pTabItem, pVTab, pWInfo, pX, v, v16, v17, v20, v22, v24, p10, p11, p7, p8, p9 uintptr var v18 bool var _ /* sWLB at bp+0 */ TWhereLoopBuilder _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrExplain, b, bFordelete, bOnerow, db, iCur, iDb, iIndexCur, iOnce, ii, n, nByteWInfo, nTabList, notReady, op, op1, p, pInfo, pIx, pJ, pLevel, pLoop, pMaskSet, pPk, pRJ, pSrc, pT, pTab, pTabItem, pVTab, pWInfo, pX, rc, v, wsFlags, wsFlags1, v1, v12, v15, v16, v17, v18, v19, v2, v20, v21, v22, v23, v24, v3, v4, p10, p11, p7, p8, p9 /* Will become the return value of this function */ v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* Return code */ bFordelete = uint8(0) /* OPFLAG_FORDELETE or zero, as appropriate */ _ = libc.Int32FromInt32(0) /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */ _ = libc.Int32FromInt32(0) /* Variable initialization */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb libc.Xmemset(tls, bp, 0, uint32(32)) /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ if pOrderBy != 0 && (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) { pOrderBy = uintptr(0) wctrlFlags = Tu16(int32(wctrlFlags) & ^libc.Int32FromInt32(WHERE_WANT_DISTINCT)) } /* The number of tables in the FROM clause is limited by the number of ** bits in a Bitmask */ if (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc > int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22510, libc.VaList(bp+40, int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)))) return uintptr(0) } /* This function normally generates a nested loop for all tables in ** pTabList. But if the WHERE_OR_SUBCLAUSE flag is set, then we should ** only generate code for the first table in pTabList and assume that ** any cursors associated with subsequent tables are uninitialized. */ if int32(wctrlFlags)&int32(WHERE_OR_SUBCLAUSE) != 0 { v1 = int32(1) } else { v1 = (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc } nTabList = v1 /* Allocate and initialize the WhereInfo structure that will become the ** return value. A single allocation is used to store the WhereInfo ** struct, the contents of WhereInfo.a[], the WhereClause structure ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte ** field (type Bitmask) it must be aligned on an 8-byte boundary on ** some architectures. Hence the ROUND8() below. */ nByteWInfo = int32((libc.Uint32FromInt64(848) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7))) if nTabList > int32(1) { nByteWInfo = int32((uint32(nByteWInfo) + uint32(nTabList-libc.Int32FromInt32(1))*libc.Uint32FromInt64(88) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7))) } pWInfo = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(nByteWInfo)+uint32(72))) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _sqlite3DbFree(tls, db, pWInfo) pWInfo = uintptr(0) goto whereBeginError } (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse = pParse (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList = pTabList (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy = pOrderBy (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpResultSet = pResultSet v2 = -libc.Int32FromInt32(1) *(*int32)(unsafe.Pointer(pWInfo + 20 + 1*4)) = v2 *(*int32)(unsafe.Pointer(pWInfo + 20)) = v2 (*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel = uint8(nTabList) v3 = _sqlite3VdbeMakeLabel(tls, pParse) (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiContinue = v3 (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiBreak = v3 (*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags = wctrlFlags (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiLimit = int16(iAuxArg) (*TWhereInfo)(unsafe.Pointer(pWInfo)).FsavedNQueryLoop = int32((*TParse)(unsafe.Pointer(pParse)).FnQueryLoop) (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect = pSelect libc.Xmemset(tls, pWInfo+45, 0, uint32(libc.UintptrFromInt32(0)+80)-uint32(libc.UintptrFromInt32(0)+45)) libc.Xmemset(tls, pWInfo+760, 0, uint32(72)+uint32(nTabList)*uint32(88)) _ = libc.Int32FromInt32(0) /* ONEPASS defaults to OFF */ pMaskSet = pWInfo + 496 (*TWhereMaskSet)(unsafe.Pointer(pMaskSet)).Fn = 0 *(*int32)(unsafe.Pointer(pMaskSet + 8)) = -int32(99) /* Initialize ix[0] to a value that can never be ** a valid cursor number, to avoid an initial ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */ (*(*TWhereLoopBuilder)(unsafe.Pointer(bp))).FpWInfo = pWInfo (*(*TWhereLoopBuilder)(unsafe.Pointer(bp))).FpWC = pWInfo + 80 (*(*TWhereLoopBuilder)(unsafe.Pointer(bp))).FpNew = pWInfo + uintptr(nByteWInfo) _ = libc.Int32FromInt32(0) _whereLoopInit(tls, (*(*TWhereLoopBuilder)(unsafe.Pointer(bp))).FpNew) /* Split the WHERE clause into separate subexpressions where each ** subexpression is separated by an AND operator. */ _sqlite3WhereClauseInit(tls, pWInfo+80, pWInfo) _sqlite3WhereSplit(tls, pWInfo+80, pWhere, uint8(TK_AND)) /* Special case: No FROM clause */ if nTabList == 0 { if pOrderBy != 0 { (*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat = int8((*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) } if int32(wctrlFlags)&int32(WHERE_WANT_DISTINCT) != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_DistinctOpt)) == uint32(0) { (*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct = uint8(WHERE_DISTINCT_UNIQUE) } _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+22538, 0) } else { /* Assign a bit from the bitmask to every term in the FROM clause. ** ** The N-th term of the FROM clause is assigned a bitmask of 1<nSrc tables in ** pTabList, not just the first nTabList tables. nTabList is normally ** equal to pTabList->nSrc but might be shortened to 1 if the ** WHERE_OR_SUBCLAUSE flag is set. */ ii = 0 for { _createMask(tls, pMaskSet, (*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(ii)*72))).FiCursor) _sqlite3WhereTabFuncArgs(tls, pParse, pTabList+8+uintptr(ii)*72, pWInfo+80) goto _5 _5: ; ii++ v4 = ii if !(v4 < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) { break } } } /* Analyze all of the subexpressions. */ _sqlite3WhereExprAnalyze(tls, pTabList, pWInfo+80) if pSelect != 0 && (*TSelect)(unsafe.Pointer(pSelect)).FpLimit != 0 { _sqlite3WhereAddLimit(tls, pWInfo+80, pSelect) } if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto whereBeginError } /* The False-WHERE-Term-Bypass optimization: ** ** If there are WHERE terms that are false, then no rows will be output, ** so skip over all of the code generated here. ** ** Conditions: ** ** (1) The WHERE term must not refer to any tables in the join. ** (2) The term must not come from an ON clause on the ** right-hand side of a LEFT or FULL JOIN. ** (3) The term must not come from an ON clause, or there must be ** no RIGHT or FULL OUTER joins in pTabList. ** (4) If the expression contains non-deterministic functions ** that are not within a sub-select. This is not required ** for correctness but rather to preserves SQLite's legacy ** behaviour in the following two cases: ** ** WHERE random()>0; -- eval random() once per row ** WHERE (SELECT random())>0; -- eval random() just once overall ** ** Note that the Where term need not be a constant in order for this ** optimization to apply, though it does need to be constant relative to ** the current subquery (condition 1). The term might include variables ** from outer queries so that the value of the term changes from one ** invocation of the current subquery to the next. */ ii = 0 for { if !(ii < (*TWhereClause)(unsafe.Pointer((*(*TWhereLoopBuilder)(unsafe.Pointer(bp))).FpWC)).FnBase) { break } pT = (*TWhereClause)(unsafe.Pointer((*(*TWhereLoopBuilder)(unsafe.Pointer(bp))).FpWC)).Fa + uintptr(ii)*48 /* The expression of pT */ if int32((*TWhereTerm)(unsafe.Pointer(pT)).FwtFlags)&int32(TERM_VIRTUAL) != 0 { goto _6 } pX = (*TWhereTerm)(unsafe.Pointer(pT)).FpExpr _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TWhereTerm)(unsafe.Pointer(pT)).FprereqAll == uint64(0) && (nTabList == 0 || _exprIsDeterministic(tls, pX) != 0) && !((*TExpr)(unsafe.Pointer(pX)).Fflags&uint32(libc.Int32FromInt32(EP_InnerON)) != uint32(0) && int32((*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0) { _sqlite3ExprIfFalse(tls, pParse, pX, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiBreak, int32(SQLITE_JUMPIFNULL)) p7 = pT + 10 *(*Tu16)(unsafe.Pointer(p7)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p7))) | libc.Int32FromInt32(TERM_CODED)) } goto _6 _6: ; ii++ } if int32(wctrlFlags)&int32(WHERE_WANT_DISTINCT) != 0 { if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_DistinctOpt)) != uint32(0) { /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ wctrlFlags = Tu16(int32(wctrlFlags) & ^libc.Int32FromInt32(WHERE_WANT_DISTINCT)) p8 = pWInfo + 40 *(*Tu16)(unsafe.Pointer(p8)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p8))) & ^libc.Int32FromInt32(WHERE_WANT_DISTINCT)) } else { if _isDistinctRedundant(tls, pParse, pTabList, pWInfo+80, pResultSet) != 0 { /* The DISTINCT marking is pointless. Ignore it. */ (*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct = uint8(WHERE_DISTINCT_UNIQUE) } else { if pOrderBy == uintptr(0) { /* Try to ORDER BY the result set to make distinct processing easier */ p9 = pWInfo + 40 *(*Tu16)(unsafe.Pointer(p9)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p9))) | libc.Int32FromInt32(WHERE_DISTINCTBY)) (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy = pResultSet } } } } /* Construct the WhereLoop objects */ if nTabList != int32(1) || _whereShortCut(tls, bp) == 0 { rc = _whereLoopAddAll(tls, bp) if rc != 0 { goto whereBeginError } /* If one or more WhereTerm.truthProb values were used in estimating ** loop parameters, but then those truthProb values were subsequently ** changed based on STAT4 information while computing subsequent loops, ** then we need to rerun the whole loop building process so that all ** loops will be built using the revised truthProb values. */ if int32((*(*TWhereLoopBuilder)(unsafe.Pointer(bp))).FbldFlags2)&int32(SQLITE_BLDF2_2NDPASS) != 0 { for (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops != 0 { p = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops = (*TWhereLoop)(unsafe.Pointer(p)).FpNextLoop _whereLoopDelete(tls, db, p) } rc = _whereLoopAddAll(tls, bp) if rc != 0 { goto whereBeginError } } _wherePathSolver(tls, pWInfo, 0) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto whereBeginError } if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy != 0 { _wherePathSolver(tls, pWInfo, int16(int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnRowOut)+int32(1))) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto whereBeginError } } /* TUNING: Assume that a DISTINCT clause on a subquery reduces ** the output size by a factor of 8 (LogEst -30). */ if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_WANT_DISTINCT) != 0 { p10 = pWInfo + 50 *(*TLogEst)(unsafe.Pointer(p10)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p10))) - libc.Int32FromInt32(30)) } } _ = libc.Int32FromInt32(0) if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy == uintptr(0) && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ReverseOrder) != uint64(0) { _whereReverseScanOrder(tls, pWInfo) } if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto whereBeginError } _ = libc.Int32FromInt32(0) /* Attempt to omit tables from a join that do not affect the result. ** See the comment on whereOmitNoopJoin() for further information. ** ** This query optimization is factored out into a separate "no-inline" ** procedure to keep the sqlite3WhereBegin() procedure from becoming ** too large. If sqlite3WhereBegin() becomes too large, that prevents ** some C-compiler optimizers from in-lining the ** sqlite3WhereCodeOneLoopStart() procedure, and it is important to ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons. */ notReady = ^libc.Uint64FromInt32(0) if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) >= int32(2) && pResultSet != uintptr(0) && 0 == int32(wctrlFlags)&int32(WHERE_AGG_DISTINCT) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_OmitNoopJoin)) == uint32(0) { notReady = _whereOmitNoopJoin(tls, pWInfo, notReady) nTabList = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) _ = libc.Int32FromInt32(0) } /* Check to see if there are any SEARCH loops that might benefit from ** using a Bloom filter. */ if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) >= int32(2) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_BloomFilter)) == uint32(0) { _whereCheckIfBloomFilterIsUseful(tls, pWInfo) } p11 = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse + 156 *(*TLogEst)(unsafe.Pointer(p11)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p11))) + int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnRowOut)) /* If the caller is an UPDATE or DELETE statement that is requesting ** to use a one-pass algorithm, determine if this is appropriate. ** ** A one-pass approach can be used if the caller has requested one ** and either (a) the scan visits at most one row or (b) each ** of the following are true: ** ** * the caller has indicated that a one-pass approach can be used ** with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and ** * the table is not a virtual table, and ** * either the scan does not use the OR optimization or the caller ** is a DELETE operation (WHERE_DUPLICATES_OK is only specified ** for DELETE). ** ** The last qualification is because an UPDATE statement uses ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can ** use a one-pass approach, and this is not set accurately for scans ** that use the OR optimization. */ _ = libc.Int32FromInt32(0) if int32(wctrlFlags)&int32(WHERE_ONEPASS_DESIRED) != 0 { wsFlags = int32((*TWhereLoop1)(unsafe.Pointer((*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760))).FpWLoop)).FwsFlags) bOnerow = libc.BoolInt32(wsFlags&int32(WHERE_ONEROW) != 0) _ = libc.Int32FromInt32(0) if bOnerow != 0 || 0 != int32(wctrlFlags)&int32(WHERE_ONEPASS_MULTIROW) && !(int32((*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FpTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) && (0 == wsFlags&int32(WHERE_MULTI_OR) || int32(wctrlFlags)&int32(WHERE_DUPLICATES_OK) != 0) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_OnePass)) == uint32(0) { if bOnerow != 0 { v12 = int32(ONEPASS_SINGLE) } else { v12 = int32(ONEPASS_MULTI) } (*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass = uint8(v12) if (*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FpTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) && wsFlags&int32(WHERE_IDX_ONLY) != 0 { if int32(wctrlFlags)&int32(WHERE_ONEPASS_MULTIROW) != 0 { bFordelete = uint8(OPFLAG_FORDELETE) } (*TWhereLoop1)(unsafe.Pointer((*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760))).FpWLoop)).FwsFlags = uint32(wsFlags & ^libc.Int32FromInt32(WHERE_IDX_ONLY)) } } } /* Open all tables in the pTabList and any indices selected for ** searching those tables. */ ii = 0 pLevel = pWInfo + 760 for { if !(ii < nTabList) { break } pTabItem = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*72 pTab = (*TSrcItem)(unsafe.Pointer(pTabItem)).FpTab iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Ephemeral) != uint32(0) || int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { /* Do nothing */ } else { if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_VIRTUALTABLE) != uint32(0) { pVTab = _sqlite3GetVTable(tls, db, pTab) iCur = (*TSrcItem)(unsafe.Pointer(pTabItem)).FiCursor _sqlite3VdbeAddOp4(tls, v, int32(OP_VOpen), iCur, 0, 0, pVTab, -int32(11)) } else { if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { /* noop */ } else { if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IDX_ONLY) == uint32(0) && int32(wctrlFlags)&int32(WHERE_OR_SUBCLAUSE) == 0 || int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0 { op = int32(OP_OpenRead) if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass) != ONEPASS_OFF { op = int32(OP_OpenWrite) *(*int32)(unsafe.Pointer(pWInfo + 20)) = (*TSrcItem)(unsafe.Pointer(pTabItem)).FiCursor } _sqlite3OpenTable(tls, pParse, (*TSrcItem)(unsafe.Pointer(pTabItem)).FiCursor, iDb, pTab, op) _ = libc.Int32FromInt32(0) if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass) == ONEPASS_OFF && int32((*TTable)(unsafe.Pointer(pTab)).FnCol) < int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(libc.Int32FromInt32(TF_HasGenerated)|libc.Int32FromInt32(TF_WithoutRowid)) == uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_AUTO_INDEX)|libc.Int32FromInt32(WHERE_BLOOMFILTER)) == uint32(0) { /* If we know that only a prefix of the record will be used, ** it is advantageous to reduce the "column count" field in ** the P4 operand of the OP_OpenRead/Write opcode. */ b = (*TSrcItem)(unsafe.Pointer(pTabItem)).FcolUsed n = 0 for { if !(b != 0) { break } goto _14 _14: ; b = b >> int32(1) n++ } _sqlite3VdbeChangeP4(tls, v, -int32(1), uintptr(n), -int32(3)) _ = libc.Int32FromInt32(0) } _sqlite3VdbeChangeP5(tls, v, uint16(bFordelete)) } else { _sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pTab)).FzName) } } } } if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_INDEXED) != 0 { pIx = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex op1 = int32(OP_OpenRead) /* iAuxArg is always set to a positive value if ONEPASS is possible */ _ = libc.Int32FromInt32(0) if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pIx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) && int32(wctrlFlags)&int32(WHERE_OR_SUBCLAUSE) != 0 { /* This is one term of an OR-optimization using the PRIMARY KEY of a ** WITHOUT ROWID table. No need for a separate index */ iIndexCur = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur op1 = 0 } else { if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass) != ONEPASS_OFF { pJ = (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pTabItem)).FpTab)).FpIndex iIndexCur = iAuxArg _ = libc.Int32FromInt32(0) for pJ != 0 && pJ != pIx { iIndexCur++ pJ = (*TIndex)(unsafe.Pointer(pJ)).FpNext } op1 = int32(OP_OpenWrite) *(*int32)(unsafe.Pointer(pWInfo + 20 + 1*4)) = iIndexCur } else { if iAuxArg != 0 && int32(wctrlFlags)&int32(WHERE_OR_SUBCLAUSE) != 0 { iIndexCur = iAuxArg op1 = int32(OP_ReopenIdx) } else { v16 = pParse + 40 v15 = *(*int32)(unsafe.Pointer(v16)) *(*int32)(unsafe.Pointer(v16))++ iIndexCur = v15 if int32(uint32(*(*uint16)(unsafe.Pointer(pIx + 56))&0x1000>>12)) != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_IndexedExpr)) == uint32(0) { _whereAddIndexedExpr(tls, pParse, pIx, iIndexCur, pTabItem) } if (*TIndex)(unsafe.Pointer(pIx)).FpPartIdxWhere != 0 && int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&int32(JT_RIGHT) == 0 { _wherePartIdxExpr(tls, pParse, pIx, (*TIndex)(unsafe.Pointer(pIx)).FpPartIdxWhere, uintptr(0), iIndexCur, pTabItem) } } } } (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur = iIndexCur _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if op1 != 0 { _sqlite3VdbeAddOp3(tls, v, op1, iIndexCur, int32((*TIndex)(unsafe.Pointer(pIx)).Ftnum), iDb) _sqlite3VdbeSetP4KeyInfo(tls, pParse, pIx) if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_CONSTRAINT) != uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_COLUMN_RANGE)|libc.Int32FromInt32(WHERE_SKIPSCAN)) == uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_BIGNULL_SORT) == uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IN_SEEKSCAN) == uint32(0) && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_ORDERBY_MIN) == 0 && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct) != int32(WHERE_DISTINCT_ORDERED) { _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_SEEKEQ)) } } } if iDb >= 0 { _sqlite3CodeVerifySchema(tls, pParse, iDb) } if v18 = int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&int32(JT_RIGHT) != 0; v18 { v17 = _sqlite3WhereMalloc(tls, pWInfo, uint64(20)) (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ = v17 } if v18 && v17 != uintptr(0) { pRJ = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ v20 = pParse + 40 v19 = *(*int32)(unsafe.Pointer(v20)) *(*int32)(unsafe.Pointer(v20))++ (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FiMatch = v19 v22 = pParse + 44 *(*int32)(unsafe.Pointer(v22))++ v21 = *(*int32)(unsafe.Pointer(v22)) (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregBloom = v21 _sqlite3VdbeAddOp2(tls, v, int32(OP_Blob), int32(65536), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregBloom) v24 = pParse + 44 *(*int32)(unsafe.Pointer(v24))++ v23 = *(*int32)(unsafe.Pointer(v24)) (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregReturn = v23 _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregReturn) _ = libc.Int32FromInt32(0) if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FiMatch, int32(1)) pInfo = _sqlite3KeyInfoAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(1), 0) if pInfo != 0 { *(*uintptr)(unsafe.Pointer(pInfo + 20)) = uintptr(0) *(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pInfo)).FaSortFlags)) = uint8(0) _sqlite3VdbeAppendP4(tls, v, pInfo, -int32(8)) } } else { pPk = _sqlite3PrimaryKeyIndex(tls, pTab) _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FiMatch, int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) _sqlite3VdbeSetP4KeyInfo(tls, pParse, pPk) } *(*Tu32)(unsafe.Pointer(pLoop + 40)) &= uint32(^libc.Int32FromInt32(WHERE_IDX_ONLY)) /* The nature of RIGHT JOIN processing is such that it messes up ** the output order. So omit any ORDER BY/GROUP BY elimination ** optimizations. We need to do an actual sort for RIGHT JOIN. */ (*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat = 0 (*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct = uint8(WHERE_DISTINCT_UNORDERED) } goto _13 _13: ; ii++ pLevel += 88 } (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiTop = _sqlite3VdbeCurrentAddr(tls, v) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto whereBeginError } /* Generate the code to do the search. Each iteration of the for ** loop below generates code for a single nested loop of the VM ** program. */ ii = 0 for { if !(ii < nTabList) { break } if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto whereBeginError } pLevel = pWInfo + 760 + uintptr(ii)*88 wsFlags1 = int32((*TWhereLoop1)(unsafe.Pointer((*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop)).FwsFlags) pSrc = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*72 if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 36 + 4))&0x10>>4)) != 0 { if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 36 + 4))&0x8>>3)) != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TSrcItem)(unsafe.Pointer(pSrc)).FregReturn, (*TSrcItem)(unsafe.Pointer(pSrc)).FaddrFillSub) } else { iOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once)) _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TSrcItem)(unsafe.Pointer(pSrc)).FregReturn, (*TSrcItem)(unsafe.Pointer(pSrc)).FaddrFillSub) _sqlite3VdbeJumpHere(tls, v, iOnce) } } _ = libc.Int32FromInt32(0) if wsFlags1&(libc.Int32FromInt32(WHERE_AUTO_INDEX)|libc.Int32FromInt32(WHERE_BLOOMFILTER)) != 0 { if wsFlags1&int32(WHERE_AUTO_INDEX) != 0 { _constructAutomaticIndex(tls, pParse, pWInfo+80, notReady, pLevel) } else { _sqlite3ConstructBloomFilter(tls, pWInfo, ii, pLevel, notReady) } if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto whereBeginError } } addrExplain = _sqlite3WhereExplainOneScan(tls, pParse, pTabList, pLevel, wctrlFlags) (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBody = _sqlite3VdbeCurrentAddr(tls, v) notReady = _sqlite3WhereCodeOneLoopStart(tls, pParse, v, pWInfo, ii, pLevel, notReady) (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiContinue = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont if wsFlags1&int32(WHERE_MULTI_OR) == 0 && int32(wctrlFlags)&int32(WHERE_OR_SUBCLAUSE) == 0 { _ = addrExplain } goto _25 _25: ; ii++ } /* Done. */ (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiEndWhere = _sqlite3VdbeCurrentAddr(tls, v) return pWInfo /* Jump here if malloc fails */ goto whereBeginError whereBeginError: ; if pWInfo != 0 { (*TParse)(unsafe.Pointer(pParse)).FnQueryLoop = int16((*TWhereInfo)(unsafe.Pointer(pWInfo)).FsavedNQueryLoop) _whereInfoFree(tls, db, pWInfo) } return uintptr(0) } /* ** Part of sqlite3WhereEnd() will rewrite opcodes to reference the ** index rather than the main table. In SQLITE_DEBUG mode, we want ** to trace those changes if PRAGMA vdbe_addoptrace=on. This routine ** does that. */ // C documentation // // /* // ** Generate the end of the WHERE loop. See comments on // ** sqlite3WhereBegin() for additional information. // */ func _sqlite3WhereEnd(tls *libc.TLS, pWInfo uintptr) { var addr, addrSeek, bEarlyOut, i, iDb, iEnd, j, j1, k, last, n, nRJ, op, r1, ws, x, v4, v7 int32 var db, p, pIdx, pIdx1, pIn, pIx, pLastOp, pLevel, pLoop, pOp, pParse, pPk, pRJ, pTab, pTabItem, pTabList, v, v10, v2, p12 uintptr var v3, v5 bool _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, addrSeek, bEarlyOut, db, i, iDb, iEnd, j, j1, k, last, n, nRJ, op, p, pIdx, pIdx1, pIn, pIx, pLastOp, pLevel, pLoop, pOp, pParse, pPk, pRJ, pTab, pTabItem, pTabList, r1, v, ws, x, v10, v2, v3, v4, v5, v7, p12 pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe pTabList = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList db = (*TParse)(unsafe.Pointer(pParse)).Fdb iEnd = _sqlite3VdbeCurrentAddr(tls, v) nRJ = 0 /* Generate loop termination code. */ i = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) - int32(1) for { if !(i >= 0) { break } pLevel = pWInfo + 760 + uintptr(i)*88 if (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ != 0 { /* Terminate the subroutine that forms the interior of the loop of ** the RIGHT JOIN table */ pRJ = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ _sqlite3VdbeResolveLabel(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont) (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont = 0 (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FendSubrtn = _sqlite3VdbeCurrentAddr(tls, v) _sqlite3VdbeAddOp3(tls, v, int32(OP_Return), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregReturn, (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FaddrSubrtn, int32(1)) nRJ++ } pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop if int32((*TWhereLevel)(unsafe.Pointer(pLevel)).Fop) != int32(OP_Noop) { addrSeek = 0 if v3 = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct) == int32(WHERE_DISTINCT_ORDERED) && i == int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)-int32(1) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_INDEXED) != uint32(0); v3 { v2 = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex pIdx = v2 } if v5 = v3 && int32(uint32(*(*uint16)(unsafe.Pointer(v2 + 56))&0x80>>7)) != 0; v5 { v4 = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnDistinctCol) n = v4 } if v5 && v4 > 0 && int32(*(*TLogEst)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiRowLogEst + uintptr(n)*2))) >= int32(36) { r1 = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) j = 0 for { if !(j < n) { break } _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur, j, r1+j) goto _6 _6: ; j++ } *(*int32)(unsafe.Pointer(pParse + 44)) += n + int32(1) if int32((*TWhereLevel)(unsafe.Pointer(pLevel)).Fop) == int32(OP_Prev) { v7 = int32(OP_SeekLT) } else { v7 = int32(OP_SeekGT) } op = v7 addrSeek = _sqlite3VdbeAddOp4Int(tls, v, op, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur, 0, r1, n) _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), int32(1), (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2) } /* The common case: Advance to the next row */ if (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont != 0 { _sqlite3VdbeResolveLabel(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont) } _sqlite3VdbeAddOp3(tls, v, int32((*TWhereLevel)(unsafe.Pointer(pLevel)).Fop), (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp1, (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2, int32((*TWhereLevel)(unsafe.Pointer(pLevel)).Fp3)) _sqlite3VdbeChangeP5(tls, v, uint16((*TWhereLevel)(unsafe.Pointer(pLevel)).Fp5)) if (*TWhereLevel)(unsafe.Pointer(pLevel)).FregBignull != 0 { _sqlite3VdbeResolveLabel(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBignull) _sqlite3VdbeAddOp2(tls, v, int32(OP_DecrJumpZero), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregBignull, (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2-int32(1)) } if addrSeek != 0 { _sqlite3VdbeJumpHere(tls, v, addrSeek) } } else { if (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont != 0 { _sqlite3VdbeResolveLabel(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont) } } if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IN_ABLE) != uint32(0) && (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 64))).FnIn > 0 { _sqlite3VdbeResolveLabel(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrNxt) j1 = (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 64))).FnIn pIn = (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 64))).FaInLoop + uintptr(j1-int32(1))*20 for { if !(j1 > 0) { break } _ = libc.Int32FromInt32(0) _sqlite3VdbeJumpHere(tls, v, (*TInLoop)(unsafe.Pointer(pIn)).FaddrInTop+int32(1)) if int32((*TInLoop)(unsafe.Pointer(pIn)).FeEndLoopOp) != int32(OP_Noop) { if (*TInLoop)(unsafe.Pointer(pIn)).FnPrefix != 0 { bEarlyOut = libc.BoolInt32((*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_VIRTUALTABLE) == uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IN_EARLYOUT) != uint32(0)) if (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin != 0 { /* For LEFT JOIN queries, cursor pIn->iCur may not have been ** opened yet. This occurs for WHERE clauses such as ** "a = ? AND b IN (...)", where the index is on (a, b). If ** the RHS of the (a=?) is NULL, then the "b IN (...)" may ** never have been coded, but the body of the loop run to ** return the null-row. So, if the cursor is not open yet, ** jump over the OP_Next or OP_Prev instruction about to ** be coded. */ _sqlite3VdbeAddOp2(tls, v, int32(OP_IfNotOpen), (*TInLoop)(unsafe.Pointer(pIn)).FiCur, _sqlite3VdbeCurrentAddr(tls, v)+int32(2)+bEarlyOut) } if bEarlyOut != 0 { _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IfNoHope), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur, _sqlite3VdbeCurrentAddr(tls, v)+int32(2), (*TInLoop)(unsafe.Pointer(pIn)).FiBase, (*TInLoop)(unsafe.Pointer(pIn)).FnPrefix) /* Retarget the OP_IsNull against the left operand of IN so ** it jumps past the OP_IfNoHope. This is because the ** OP_IsNull also bypasses the OP_Affinity opcode that is ** required by OP_IfNoHope. */ _sqlite3VdbeJumpHere(tls, v, (*TInLoop)(unsafe.Pointer(pIn)).FaddrInTop+int32(1)) } } _sqlite3VdbeAddOp2(tls, v, int32((*TInLoop)(unsafe.Pointer(pIn)).FeEndLoopOp), (*TInLoop)(unsafe.Pointer(pIn)).FiCur, (*TInLoop)(unsafe.Pointer(pIn)).FaddrInTop) } _sqlite3VdbeJumpHere(tls, v, (*TInLoop)(unsafe.Pointer(pIn)).FaddrInTop-int32(1)) goto _8 _8: ; j1-- pIn -= 20 } } _sqlite3VdbeResolveLabel(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk) if (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Return), (*TWhereRightJoin)(unsafe.Pointer((*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ)).FregReturn, 0, int32(1)) } if (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrSkip != 0 { _sqlite3VdbeGoto(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrSkip) _sqlite3VdbeJumpHere(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrSkip) _sqlite3VdbeJumpHere(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrSkip-int32(2)) } if (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin != 0 { ws = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags) addr = _sqlite3VdbeAddOp1(tls, v, int32(OP_IfPos), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin) _ = libc.Int32FromInt32(0) if ws&int32(WHERE_IDX_ONLY) == 0 { _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur) } if ws&int32(WHERE_INDEXED) != 0 || ws&int32(WHERE_MULTI_OR) != 0 && *(*uintptr)(unsafe.Pointer(&(*TWhereLevel)(unsafe.Pointer(pLevel)).Fu)) != 0 { if ws&int32(WHERE_MULTI_OR) != 0 { pIx = *(*uintptr)(unsafe.Pointer(&(*TWhereLevel)(unsafe.Pointer(pLevel)).Fu)) iDb = _sqlite3SchemaToIndex(tls, db, (*TIndex)(unsafe.Pointer(pIx)).FpSchema) _sqlite3VdbeAddOp3(tls, v, int32(OP_ReopenIdx), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur, int32((*TIndex)(unsafe.Pointer(pIx)).Ftnum), iDb) _sqlite3VdbeSetP4KeyInfo(tls, pParse, pIx) } _sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur) } if int32((*TWhereLevel)(unsafe.Pointer(pLevel)).Fop) == int32(OP_Return) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp1, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrFirst) } else { _sqlite3VdbeGoto(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrFirst) } _sqlite3VdbeJumpHere(tls, v, addr) } goto _1 _1: ; i-- } _ = libc.Int32FromInt32(0) i = 0 pLevel = pWInfo + 760 for { if !(i < int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)) { break } pIdx1 = uintptr(0) pTabItem = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*72 pTab = (*TSrcItem)(unsafe.Pointer(pTabItem)).FpTab _ = libc.Int32FromInt32(0) pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop /* Do RIGHT JOIN processing. Generate code that will output the ** unmatched rows of the right operand of the RIGHT JOIN with ** all of the columns of the left operand set to NULL. */ if (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ != 0 { _sqlite3WhereRightJoinLoop(tls, pWInfo, i, pLevel) goto _9 } /* For a co-routine, change all OP_Column references to the table of ** the co-routine into OP_Copy of result contained in a register. ** OP_Rowid becomes OP_Null. */ if int32(uint32(*(*uint16)(unsafe.Pointer(pTabItem + 36 + 4))&0x20>>5)) != 0 { _translateColumnToCopy(tls, pParse, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBody, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, (*TSrcItem)(unsafe.Pointer(pTabItem)).FregResult, 0) goto _9 } /* If this scan uses an index, make VDBE code substitutions to read data ** from the index instead of from the table where possible. In some cases ** this optimization prevents the table from ever being read, which can ** yield a significant performance boost. ** ** Calls to the code generator in between sqlite3WhereBegin and ** sqlite3WhereEnd will have created code that references the table ** directly. This loop scans all that code looking for opcodes ** that reference the table and converts them into opcodes that ** reference the index. */ if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_INDEXED)|libc.Int32FromInt32(WHERE_IDX_ONLY)) != 0 { pIdx1 = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex } else { if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_MULTI_OR) != 0 { pIdx1 = *(*uintptr)(unsafe.Pointer(&(*TWhereLevel)(unsafe.Pointer(pLevel)).Fu)) } } if pIdx1 != 0 && !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass) == ONEPASS_OFF || !((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx1)).FpTable)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { last = iEnd } else { last = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiEndWhere } if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx1 + 56))&0x1000>>12)) != 0 { p = (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr for p != 0 { if (*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCur == (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur { (*TIndexedExpr)(unsafe.Pointer(p)).FiDataCur = -int32(1) (*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCur = -int32(1) } p = (*TIndexedExpr)(unsafe.Pointer(p)).FpIENext } } k = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBody + int32(1) pOp = _sqlite3VdbeGetOp(tls, v, k) pLastOp = pOp + uintptr(last-k)*20 _ = libc.Int32FromInt32(0) for { if (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 != (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur { /* no-op */ } else { if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Column) || int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Offset) { x = (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 _ = libc.Int32FromInt32(0) if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Offset) { /* Do not need to translate the column number */ } else { if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { pPk = _sqlite3PrimaryKeyIndex(tls, pTab) x = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(x)*2))) _ = libc.Int32FromInt32(0) } else { x = int32(_sqlite3StorageColumnToTable(tls, pTab, int16(x))) } } x = int32(_sqlite3TableColumnToIndex(tls, pIdx1, int16(x))) if x >= 0 { (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 = x (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur } else { /* Unable to translate the table reference into an index ** reference. Verify that this is harmless - that the ** table being referenced really is open. */ _ = libc.Int32FromInt32(0) } } else { if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Rowid) { (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur (*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode = uint8(OP_IdxRowid) } else { if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_IfNullRow) { (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur } } } } goto _11 _11: ; pOp += 20 v10 = pOp if !(v10 < pLastOp) { break } } } goto _9 _9: ; i++ pLevel += 88 } /* The "break" point is here, just past the end of the outer loop. ** Set it. */ _sqlite3VdbeResolveLabel(tls, v, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiBreak) /* Final cleanup */ (*TParse)(unsafe.Pointer(pParse)).FnQueryLoop = int16((*TWhereInfo)(unsafe.Pointer(pWInfo)).FsavedNQueryLoop) _whereInfoFree(tls, db, pWInfo) p12 = pParse + 26 *(*Tu8)(unsafe.Pointer(p12)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p12))) - nRJ) return } /************** End of where.c ***********************************************/ /************** Begin file window.c ******************************************/ /* ** 2018 May 08 ** ** 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. ** ************************************************************************* */ /* #include "sqliteInt.h" */ /* ** SELECT REWRITING ** ** Any SELECT statement that contains one or more window functions in ** either the select list or ORDER BY clause (the only two places window ** functions may be used) is transformed by function sqlite3WindowRewrite() ** in order to support window function processing. For example, with the ** schema: ** ** CREATE TABLE t1(a, b, c, d, e, f, g); ** ** the statement: ** ** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e; ** ** is transformed to: ** ** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM ( ** SELECT a, e, c, d, b FROM t1 ORDER BY c, d ** ) ORDER BY e; ** ** The flattening optimization is disabled when processing this transformed ** SELECT statement. This allows the implementation of the window function ** (in this case max()) to process rows sorted in order of (c, d), which ** makes things easier for obvious reasons. More generally: ** ** * FROM, WHERE, GROUP BY and HAVING clauses are all moved to ** the sub-query. ** ** * ORDER BY, LIMIT and OFFSET remain part of the parent query. ** ** * Terminals from each of the expression trees that make up the ** select-list and ORDER BY expressions in the parent query are ** selected by the sub-query. For the purposes of the transformation, ** terminals are column references and aggregate functions. ** ** If there is more than one window function in the SELECT that uses ** the same window declaration (the OVER bit), then a single scan may ** be used to process more than one window function. For example: ** ** SELECT max(b) OVER (PARTITION BY c ORDER BY d), ** min(e) OVER (PARTITION BY c ORDER BY d) ** FROM t1; ** ** is transformed in the same way as the example above. However: ** ** SELECT max(b) OVER (PARTITION BY c ORDER BY d), ** min(e) OVER (PARTITION BY a ORDER BY b) ** FROM t1; ** ** Must be transformed to: ** ** SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM ( ** SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM ** SELECT a, e, c, d, b FROM t1 ORDER BY a, b ** ) ORDER BY c, d ** ) ORDER BY e; ** ** so that both min() and max() may process rows in the order defined by ** their respective window declarations. ** ** INTERFACE WITH SELECT.C ** ** When processing the rewritten SELECT statement, code in select.c calls ** sqlite3WhereBegin() to begin iterating through the results of the ** sub-query, which is always implemented as a co-routine. It then calls ** sqlite3WindowCodeStep() to process rows and finish the scan by calling ** sqlite3WhereEnd(). ** ** sqlite3WindowCodeStep() generates VM code so that, for each row returned ** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked. ** When the sub-routine is invoked: ** ** * The results of all window-functions for the row are stored ** in the associated Window.regResult registers. ** ** * The required terminal values are stored in the current row of ** temp table Window.iEphCsr. ** ** In some cases, depending on the window frame and the specific window ** functions invoked, sqlite3WindowCodeStep() caches each entire partition ** in a temp table before returning any rows. In other cases it does not. ** This detail is encapsulated within this file, the code generated by ** select.c is the same in either case. ** ** BUILT-IN WINDOW FUNCTIONS ** ** This implementation features the following built-in window functions: ** ** row_number() ** rank() ** dense_rank() ** percent_rank() ** cume_dist() ** ntile(N) ** lead(expr [, offset [, default]]) ** lag(expr [, offset [, default]]) ** first_value(expr) ** last_value(expr) ** nth_value(expr, N) ** ** These are the same built-in window functions supported by Postgres. ** Although the behaviour of aggregate window functions (functions that ** can be used as either aggregates or window functions) allows them to ** be implemented using an API, built-in window functions are much more ** esoteric. Additionally, some window functions (e.g. nth_value()) ** may only be implemented by caching the entire partition in memory. ** As such, some built-in window functions use the same API as aggregate ** window functions and some are implemented directly using VDBE ** instructions. Additionally, for those functions that use the API, the ** window frame is sometimes modified before the SELECT statement is ** rewritten. For example, regardless of the specified window frame, the ** row_number() function always uses: ** ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ** ** See sqlite3WindowUpdate() for details. ** ** As well as some of the built-in window functions, aggregate window ** functions min() and max() are implemented using VDBE instructions if ** the start of the window frame is declared as anything other than ** UNBOUNDED PRECEDING. */ // C documentation // // /* // ** Implementation of built-in window function row_number(). Assumes that the // ** window frame has been coerced to: // ** // ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW // */ func _row_numberStepFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) { var p uintptr _ = p p = Xsqlite3_aggregate_context(tls, pCtx, int32(8)) if p != 0 { *(*Ti64)(unsafe.Pointer(p))++ } _ = nArg _ = apArg } func _row_numberValueFunc(tls *libc.TLS, pCtx uintptr) { var p uintptr var v1 int64 _, _ = p, v1 p = Xsqlite3_aggregate_context(tls, pCtx, int32(8)) if p != 0 { v1 = *(*Ti64)(unsafe.Pointer(p)) } else { v1 = 0 } Xsqlite3_result_int64(tls, pCtx, v1) } /* ** Context object type used by rank(), dense_rank(), percent_rank() and ** cume_dist(). */ type TCallCount = struct { F__ccgo_align [0]uint32 FnValue Ti64 FnStep Ti64 FnTotal Ti64 } type CallCount = TCallCount // C documentation // // /* // ** Implementation of built-in window function dense_rank(). Assumes that // ** the window frame has been set to: // ** // ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW // */ func _dense_rankStepFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) { var p uintptr _ = p p = Xsqlite3_aggregate_context(tls, pCtx, int32(24)) if p != 0 { (*TCallCount)(unsafe.Pointer(p)).FnStep = int64(1) } _ = nArg _ = apArg } func _dense_rankValueFunc(tls *libc.TLS, pCtx uintptr) { var p uintptr _ = p p = Xsqlite3_aggregate_context(tls, pCtx, int32(24)) if p != 0 { if (*TCallCount)(unsafe.Pointer(p)).FnStep != 0 { (*TCallCount)(unsafe.Pointer(p)).FnValue++ (*TCallCount)(unsafe.Pointer(p)).FnStep = 0 } Xsqlite3_result_int64(tls, pCtx, (*TCallCount)(unsafe.Pointer(p)).FnValue) } } /* ** Implementation of built-in window function nth_value(). This ** implementation is used in "slow mode" only - when the EXCLUDE clause ** is not set to the default value "NO OTHERS". */ type TNthValueCtx = struct { F__ccgo_align [0]uint32 FnStep Ti64 FpValue uintptr F__ccgo_pad2 [4]byte } type NthValueCtx = TNthValueCtx func _nth_valueStepFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) { var fVal float64 var iVal Ti64 var p uintptr _, _, _ = fVal, iVal, p p = Xsqlite3_aggregate_context(tls, pCtx, int32(16)) if p != 0 { switch Xsqlite3_value_numeric_type(tls, *(*uintptr)(unsafe.Pointer(apArg + 1*4))) { case int32(SQLITE_INTEGER): iVal = Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(apArg + 1*4))) case int32(SQLITE_FLOAT): fVal = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(apArg + 1*4))) if float64(int64(fVal)) != fVal { goto error_out } iVal = int64(fVal) default: goto error_out } if iVal <= 0 { goto error_out } (*TNthValueCtx)(unsafe.Pointer(p)).FnStep++ if iVal == (*TNthValueCtx)(unsafe.Pointer(p)).FnStep { (*TNthValueCtx)(unsafe.Pointer(p)).FpValue = Xsqlite3_value_dup(tls, *(*uintptr)(unsafe.Pointer(apArg))) if !((*TNthValueCtx)(unsafe.Pointer(p)).FpValue != 0) { Xsqlite3_result_error_nomem(tls, pCtx) } } } _ = nArg _ = apArg return goto error_out error_out: ; Xsqlite3_result_error(tls, pCtx, __ccgo_ts+22556, -int32(1)) } func _nth_valueFinalizeFunc(tls *libc.TLS, pCtx uintptr) { var p uintptr _ = p p = Xsqlite3_aggregate_context(tls, pCtx, 0) if p != 0 && (*TNthValueCtx)(unsafe.Pointer(p)).FpValue != 0 { Xsqlite3_result_value(tls, pCtx, (*TNthValueCtx)(unsafe.Pointer(p)).FpValue) Xsqlite3_value_free(tls, (*TNthValueCtx)(unsafe.Pointer(p)).FpValue) (*TNthValueCtx)(unsafe.Pointer(p)).FpValue = uintptr(0) } } func _first_valueStepFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) { var p uintptr _ = p p = Xsqlite3_aggregate_context(tls, pCtx, int32(16)) if p != 0 && (*TNthValueCtx)(unsafe.Pointer(p)).FpValue == uintptr(0) { (*TNthValueCtx)(unsafe.Pointer(p)).FpValue = Xsqlite3_value_dup(tls, *(*uintptr)(unsafe.Pointer(apArg))) if !((*TNthValueCtx)(unsafe.Pointer(p)).FpValue != 0) { Xsqlite3_result_error_nomem(tls, pCtx) } } _ = nArg _ = apArg } func _first_valueFinalizeFunc(tls *libc.TLS, pCtx uintptr) { var p uintptr _ = p p = Xsqlite3_aggregate_context(tls, pCtx, int32(16)) if p != 0 && (*TNthValueCtx)(unsafe.Pointer(p)).FpValue != 0 { Xsqlite3_result_value(tls, pCtx, (*TNthValueCtx)(unsafe.Pointer(p)).FpValue) Xsqlite3_value_free(tls, (*TNthValueCtx)(unsafe.Pointer(p)).FpValue) (*TNthValueCtx)(unsafe.Pointer(p)).FpValue = uintptr(0) } } // C documentation // // /* // ** Implementation of built-in window function rank(). Assumes that // ** the window frame has been set to: // ** // ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW // */ func _rankStepFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) { var p uintptr _ = p p = Xsqlite3_aggregate_context(tls, pCtx, int32(24)) if p != 0 { (*TCallCount)(unsafe.Pointer(p)).FnStep++ if (*TCallCount)(unsafe.Pointer(p)).FnValue == 0 { (*TCallCount)(unsafe.Pointer(p)).FnValue = (*TCallCount)(unsafe.Pointer(p)).FnStep } } _ = nArg _ = apArg } func _rankValueFunc(tls *libc.TLS, pCtx uintptr) { var p uintptr _ = p p = Xsqlite3_aggregate_context(tls, pCtx, int32(24)) if p != 0 { Xsqlite3_result_int64(tls, pCtx, (*TCallCount)(unsafe.Pointer(p)).FnValue) (*TCallCount)(unsafe.Pointer(p)).FnValue = 0 } } // C documentation // // /* // ** Implementation of built-in window function percent_rank(). Assumes that // ** the window frame has been set to: // ** // ** GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING // */ func _percent_rankStepFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) { var p uintptr _ = p _ = nArg _ = libc.Int32FromInt32(0) _ = apArg p = Xsqlite3_aggregate_context(tls, pCtx, int32(24)) if p != 0 { (*TCallCount)(unsafe.Pointer(p)).FnTotal++ } } func _percent_rankInvFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) { var p uintptr _ = p _ = nArg _ = libc.Int32FromInt32(0) _ = apArg p = Xsqlite3_aggregate_context(tls, pCtx, int32(24)) (*TCallCount)(unsafe.Pointer(p)).FnStep++ } func _percent_rankValueFunc(tls *libc.TLS, pCtx uintptr) { var p uintptr var r float64 _, _ = p, r p = Xsqlite3_aggregate_context(tls, pCtx, int32(24)) if p != 0 { (*TCallCount)(unsafe.Pointer(p)).FnValue = (*TCallCount)(unsafe.Pointer(p)).FnStep if (*TCallCount)(unsafe.Pointer(p)).FnTotal > int64(1) { r = float64((*TCallCount)(unsafe.Pointer(p)).FnValue) / float64((*TCallCount)(unsafe.Pointer(p)).FnTotal-libc.Int64FromInt32(1)) Xsqlite3_result_double(tls, pCtx, r) } else { Xsqlite3_result_double(tls, pCtx, float64(0)) } } } // C documentation // // /* // ** Implementation of built-in window function cume_dist(). Assumes that // ** the window frame has been set to: // ** // ** GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING // */ func _cume_distStepFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) { var p uintptr _ = p _ = nArg _ = libc.Int32FromInt32(0) _ = apArg p = Xsqlite3_aggregate_context(tls, pCtx, int32(24)) if p != 0 { (*TCallCount)(unsafe.Pointer(p)).FnTotal++ } } func _cume_distInvFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) { var p uintptr _ = p _ = nArg _ = libc.Int32FromInt32(0) _ = apArg p = Xsqlite3_aggregate_context(tls, pCtx, int32(24)) (*TCallCount)(unsafe.Pointer(p)).FnStep++ } func _cume_distValueFunc(tls *libc.TLS, pCtx uintptr) { var p uintptr var r float64 _, _ = p, r p = Xsqlite3_aggregate_context(tls, pCtx, 0) if p != 0 { r = float64((*TCallCount)(unsafe.Pointer(p)).FnStep) / float64((*TCallCount)(unsafe.Pointer(p)).FnTotal) Xsqlite3_result_double(tls, pCtx, r) } } /* ** Context object for ntile() window function. */ type TNtileCtx = struct { F__ccgo_align [0]uint32 FnTotal Ti64 FnParam Ti64 FiRow Ti64 } type NtileCtx = TNtileCtx // C documentation // // /* // ** Implementation of ntile(). This assumes that the window frame has // ** been coerced to: // ** // ** ROWS CURRENT ROW AND UNBOUNDED FOLLOWING // */ func _ntileStepFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) { var p uintptr _ = p _ = libc.Int32FromInt32(0) _ = nArg p = Xsqlite3_aggregate_context(tls, pCtx, int32(24)) if p != 0 { if (*TNtileCtx)(unsafe.Pointer(p)).FnTotal == 0 { (*TNtileCtx)(unsafe.Pointer(p)).FnParam = Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(apArg))) if (*TNtileCtx)(unsafe.Pointer(p)).FnParam <= 0 { Xsqlite3_result_error(tls, pCtx, __ccgo_ts+22612, -int32(1)) } } (*TNtileCtx)(unsafe.Pointer(p)).FnTotal++ } } func _ntileInvFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) { var p uintptr _ = p _ = libc.Int32FromInt32(0) _ = nArg _ = apArg p = Xsqlite3_aggregate_context(tls, pCtx, int32(24)) (*TNtileCtx)(unsafe.Pointer(p)).FiRow++ } func _ntileValueFunc(tls *libc.TLS, pCtx uintptr) { var iRow, iSmall, nLarge Ti64 var nSize int32 var p uintptr _, _, _, _, _ = iRow, iSmall, nLarge, nSize, p p = Xsqlite3_aggregate_context(tls, pCtx, int32(24)) if p != 0 && (*TNtileCtx)(unsafe.Pointer(p)).FnParam > 0 { nSize = int32((*TNtileCtx)(unsafe.Pointer(p)).FnTotal / (*TNtileCtx)(unsafe.Pointer(p)).FnParam) if nSize == 0 { Xsqlite3_result_int64(tls, pCtx, (*TNtileCtx)(unsafe.Pointer(p)).FiRow+int64(1)) } else { nLarge = (*TNtileCtx)(unsafe.Pointer(p)).FnTotal - (*TNtileCtx)(unsafe.Pointer(p)).FnParam*int64(nSize) iSmall = nLarge * int64(nSize+libc.Int32FromInt32(1)) iRow = (*TNtileCtx)(unsafe.Pointer(p)).FiRow _ = libc.Int32FromInt32(0) if iRow < iSmall { Xsqlite3_result_int64(tls, pCtx, int64(1)+iRow/int64(nSize+libc.Int32FromInt32(1))) } else { Xsqlite3_result_int64(tls, pCtx, int64(1)+nLarge+(iRow-iSmall)/int64(nSize)) } } } } /* ** Context object for last_value() window function. */ type TLastValueCtx = struct { FpVal uintptr FnVal int32 } type LastValueCtx = TLastValueCtx // C documentation // // /* // ** Implementation of last_value(). // */ func _last_valueStepFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) { var p uintptr _ = p _ = nArg p = Xsqlite3_aggregate_context(tls, pCtx, int32(8)) if p != 0 { Xsqlite3_value_free(tls, (*TLastValueCtx)(unsafe.Pointer(p)).FpVal) (*TLastValueCtx)(unsafe.Pointer(p)).FpVal = Xsqlite3_value_dup(tls, *(*uintptr)(unsafe.Pointer(apArg))) if (*TLastValueCtx)(unsafe.Pointer(p)).FpVal == uintptr(0) { Xsqlite3_result_error_nomem(tls, pCtx) } else { (*TLastValueCtx)(unsafe.Pointer(p)).FnVal++ } } } func _last_valueInvFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) { var p uintptr _ = p _ = nArg _ = apArg p = Xsqlite3_aggregate_context(tls, pCtx, int32(8)) if p != 0 { (*TLastValueCtx)(unsafe.Pointer(p)).FnVal-- if (*TLastValueCtx)(unsafe.Pointer(p)).FnVal == 0 { Xsqlite3_value_free(tls, (*TLastValueCtx)(unsafe.Pointer(p)).FpVal) (*TLastValueCtx)(unsafe.Pointer(p)).FpVal = uintptr(0) } } } func _last_valueValueFunc(tls *libc.TLS, pCtx uintptr) { var p uintptr _ = p p = Xsqlite3_aggregate_context(tls, pCtx, 0) if p != 0 && (*TLastValueCtx)(unsafe.Pointer(p)).FpVal != 0 { Xsqlite3_result_value(tls, pCtx, (*TLastValueCtx)(unsafe.Pointer(p)).FpVal) } } func _last_valueFinalizeFunc(tls *libc.TLS, pCtx uintptr) { var p uintptr _ = p p = Xsqlite3_aggregate_context(tls, pCtx, int32(8)) if p != 0 && (*TLastValueCtx)(unsafe.Pointer(p)).FpVal != 0 { Xsqlite3_result_value(tls, pCtx, (*TLastValueCtx)(unsafe.Pointer(p)).FpVal) Xsqlite3_value_free(tls, (*TLastValueCtx)(unsafe.Pointer(p)).FpVal) (*TLastValueCtx)(unsafe.Pointer(p)).FpVal = uintptr(0) } } // C documentation // // /* // ** Static names for the built-in window function names. These static // ** names are used, rather than string literals, so that FuncDef objects // ** can be associated with a particular window function by direct // ** comparison of the zName pointer. Example: // ** // ** if( pFuncDef->zName==row_valueName ){ ... } // */ var _row_numberName = [11]uint8{'r', 'o', 'w', '_', 'n', 'u', 'm', 'b', 'e', 'r'} var _dense_rankName = [11]uint8{'d', 'e', 'n', 's', 'e', '_', 'r', 'a', 'n', 'k'} var _rankName = [5]uint8{'r', 'a', 'n', 'k'} var _percent_rankName = [13]uint8{'p', 'e', 'r', 'c', 'e', 'n', 't', '_', 'r', 'a', 'n', 'k'} var _cume_distName = [10]uint8{'c', 'u', 'm', 'e', '_', 'd', 'i', 's', 't'} var _ntileName = [6]uint8{'n', 't', 'i', 'l', 'e'} var _last_valueName = [11]uint8{'l', 'a', 's', 't', '_', 'v', 'a', 'l', 'u', 'e'} var _nth_valueName = [10]uint8{'n', 't', 'h', '_', 'v', 'a', 'l', 'u', 'e'} var _first_valueName = [12]uint8{'f', 'i', 'r', 's', 't', '_', 'v', 'a', 'l', 'u', 'e'} var _leadName = [5]uint8{'l', 'e', 'a', 'd'} var _lagName = [4]uint8{'l', 'a', 'g'} // C documentation // // /* // ** No-op implementations of xStep() and xFinalize(). Used as place-holders // ** for built-in window functions that never call those interfaces. // ** // ** The noopValueFunc() is called but is expected to do nothing. The // ** noopStepFunc() is never called, and so it is marked with NO_TEST to // ** let the test coverage routine know not to expect this function to be // ** invoked. // */ func _noopStepFunc(tls *libc.TLS, p uintptr, n int32, a uintptr) { /*NO_TEST*/ _ = p /*NO_TEST*/ _ = n /*NO_TEST*/ _ = a /*NO_TEST*/ _ = libc.Int32FromInt32(0) /*NO_TEST*/ } /*NO_TEST*/ func _noopValueFunc(tls *libc.TLS, p uintptr) { _ = p /*no-op*/ } /* Window functions that use all window interfaces: xStep, xFinal, ** xValue, and xInverse */ /* Window functions that are implemented using bytecode and thus have ** no-op routines for their methods */ /* Window functions that use all window interfaces: xStep, the ** same routine for xFinalize and xValue and which never call ** xInverse. */ // C documentation // // /* // ** Register those built-in window functions that are not also aggregates. // */ func _sqlite3WindowFunctions(tls *libc.TLS) { _sqlite3InsertBuiltinFuncs(tls, uintptr(unsafe.Pointer(&_aWindowFuncs)), int32(libc.Uint32FromInt64(600)/libc.Uint32FromInt64(40))) } var _aWindowFuncs = [15]TFuncDef{ 0: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_row_numberName)), }, 1: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_dense_rankName)), }, 2: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_rankName)), }, 3: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_percent_rankName)), }, 4: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_cume_distName)), }, 5: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_ntileName)), }, 6: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_last_valueName)), }, 7: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_nth_valueName)), }, 8: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_first_valueName)), }, 9: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_leadName)), }, 10: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_leadName)), }, 11: { FnArg: int8(3), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_leadName)), }, 12: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_lagName)), }, 13: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_lagName)), }, 14: { FnArg: int8(3), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_WINDOW) | libc.Int32FromInt32(0)), FzName: uintptr(unsafe.Pointer(&_lagName)), }, } func init() { p := unsafe.Pointer(&_aWindowFuncs) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_row_numberStepFunc) *(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_row_numberValueFunc) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_row_numberValueFunc) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_dense_rankStepFunc) *(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_dense_rankValueFunc) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_dense_rankValueFunc) *(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_rankStepFunc) *(*uintptr)(unsafe.Add(p, 100)) = __ccgo_fp(_rankValueFunc) *(*uintptr)(unsafe.Add(p, 104)) = __ccgo_fp(_rankValueFunc) *(*uintptr)(unsafe.Add(p, 108)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_percent_rankStepFunc) *(*uintptr)(unsafe.Add(p, 140)) = __ccgo_fp(_percent_rankValueFunc) *(*uintptr)(unsafe.Add(p, 144)) = __ccgo_fp(_percent_rankValueFunc) *(*uintptr)(unsafe.Add(p, 148)) = __ccgo_fp(_percent_rankInvFunc) *(*uintptr)(unsafe.Add(p, 176)) = __ccgo_fp(_cume_distStepFunc) *(*uintptr)(unsafe.Add(p, 180)) = __ccgo_fp(_cume_distValueFunc) *(*uintptr)(unsafe.Add(p, 184)) = __ccgo_fp(_cume_distValueFunc) *(*uintptr)(unsafe.Add(p, 188)) = __ccgo_fp(_cume_distInvFunc) *(*uintptr)(unsafe.Add(p, 216)) = __ccgo_fp(_ntileStepFunc) *(*uintptr)(unsafe.Add(p, 220)) = __ccgo_fp(_ntileValueFunc) *(*uintptr)(unsafe.Add(p, 224)) = __ccgo_fp(_ntileValueFunc) *(*uintptr)(unsafe.Add(p, 228)) = __ccgo_fp(_ntileInvFunc) *(*uintptr)(unsafe.Add(p, 256)) = __ccgo_fp(_last_valueStepFunc) *(*uintptr)(unsafe.Add(p, 260)) = __ccgo_fp(_last_valueFinalizeFunc) *(*uintptr)(unsafe.Add(p, 264)) = __ccgo_fp(_last_valueValueFunc) *(*uintptr)(unsafe.Add(p, 268)) = __ccgo_fp(_last_valueInvFunc) *(*uintptr)(unsafe.Add(p, 296)) = __ccgo_fp(_nth_valueStepFunc) *(*uintptr)(unsafe.Add(p, 300)) = __ccgo_fp(_nth_valueFinalizeFunc) *(*uintptr)(unsafe.Add(p, 304)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 308)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 336)) = __ccgo_fp(_first_valueStepFunc) *(*uintptr)(unsafe.Add(p, 340)) = __ccgo_fp(_first_valueFinalizeFunc) *(*uintptr)(unsafe.Add(p, 344)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 348)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 376)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 380)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 384)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 388)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 416)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 420)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 424)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 428)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 456)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 460)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 464)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 468)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 496)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 500)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 504)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 508)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 536)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 540)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 544)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 548)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 576)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 580)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 584)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 588)) = __ccgo_fp(_noopStepFunc) } func _windowFind(tls *libc.TLS, pParse uintptr, pList uintptr, zName uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var p uintptr _ = p p = pList for { if !(p != 0) { break } if _sqlite3StrICmp(tls, (*TWindow)(unsafe.Pointer(p)).FzName, zName) == 0 { break } goto _1 _1: ; p = (*TWindow)(unsafe.Pointer(p)).FpNextWin } if p == uintptr(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22657, libc.VaList(bp+8, zName)) } return p } // C documentation // // /* // ** This function is called immediately after resolving the function name // ** for a window function within a SELECT statement. Argument pList is a // ** linked list of WINDOW definitions for the current SELECT statement. // ** Argument pFunc is the function definition just resolved and pWin // ** is the Window object representing the associated OVER clause. This // ** function updates the contents of pWin as follows: // ** // ** * If the OVER clause referred to a named window (as in "max(x) OVER win"), // ** search list pList for a matching WINDOW definition, and update pWin // ** accordingly. If no such WINDOW clause can be found, leave an error // ** in pParse. // ** // ** * If the function is a built-in window function that requires the // ** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top // ** of this file), pWin is updated here. // */ func _sqlite3WindowUpdate(tls *libc.TLS, pParse uintptr, pList uintptr, pWin uintptr, pFunc uintptr) { var aUp [8]struct { FzFunc uintptr FeFrmType int32 FeStart int32 FeEnd int32 } var db, p, v2 uintptr var i int32 _, _, _, _, _ = aUp, db, i, p, v2 if (*TWindow)(unsafe.Pointer(pWin)).FzName != 0 && int32((*TWindow)(unsafe.Pointer(pWin)).FeFrmType) == 0 { p = _windowFind(tls, pParse, pList, (*TWindow)(unsafe.Pointer(pWin)).FzName) if p == uintptr(0) { return } (*TWindow)(unsafe.Pointer(pWin)).FpPartition = _sqlite3ExprListDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TWindow)(unsafe.Pointer(p)).FpPartition, 0) (*TWindow)(unsafe.Pointer(pWin)).FpOrderBy = _sqlite3ExprListDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TWindow)(unsafe.Pointer(p)).FpOrderBy, 0) (*TWindow)(unsafe.Pointer(pWin)).FpStart = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TWindow)(unsafe.Pointer(p)).FpStart, 0) (*TWindow)(unsafe.Pointer(pWin)).FpEnd = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TWindow)(unsafe.Pointer(p)).FpEnd, 0) (*TWindow)(unsafe.Pointer(pWin)).FeStart = (*TWindow)(unsafe.Pointer(p)).FeStart (*TWindow)(unsafe.Pointer(pWin)).FeEnd = (*TWindow)(unsafe.Pointer(p)).FeEnd (*TWindow)(unsafe.Pointer(pWin)).FeFrmType = (*TWindow)(unsafe.Pointer(p)).FeFrmType (*TWindow)(unsafe.Pointer(pWin)).FeExclude = (*TWindow)(unsafe.Pointer(p)).FeExclude } else { _sqlite3WindowChain(tls, pParse, pWin, pList) } if int32((*TWindow)(unsafe.Pointer(pWin)).FeFrmType) == int32(TK_RANGE) && ((*TWindow)(unsafe.Pointer(pWin)).FpStart != 0 || (*TWindow)(unsafe.Pointer(pWin)).FpEnd != 0) && ((*TWindow)(unsafe.Pointer(pWin)).FpOrderBy == uintptr(0) || (*TExprList)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOrderBy)).FnExpr != int32(1)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22676, 0) } else { if (*TFuncDef)(unsafe.Pointer(pFunc)).FfuncFlags&uint32(SQLITE_FUNC_WINDOW) != 0 { db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*TWindow)(unsafe.Pointer(pWin)).FpFilter != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22747, 0) } else { aUp = [8]struct { FzFunc uintptr FeFrmType int32 FeStart int32 FeEnd int32 }{ 0: { FzFunc: uintptr(unsafe.Pointer(&_row_numberName)), FeFrmType: int32(TK_ROWS), FeStart: int32(TK_UNBOUNDED), FeEnd: int32(TK_CURRENT), }, 1: { FzFunc: uintptr(unsafe.Pointer(&_dense_rankName)), FeFrmType: int32(TK_RANGE), FeStart: int32(TK_UNBOUNDED), FeEnd: int32(TK_CURRENT), }, 2: { FzFunc: uintptr(unsafe.Pointer(&_rankName)), FeFrmType: int32(TK_RANGE), FeStart: int32(TK_UNBOUNDED), FeEnd: int32(TK_CURRENT), }, 3: { FzFunc: uintptr(unsafe.Pointer(&_percent_rankName)), FeFrmType: int32(TK_GROUPS), FeStart: int32(TK_CURRENT), FeEnd: int32(TK_UNBOUNDED), }, 4: { FzFunc: uintptr(unsafe.Pointer(&_cume_distName)), FeFrmType: int32(TK_GROUPS), FeStart: int32(TK_FOLLOWING), FeEnd: int32(TK_UNBOUNDED), }, 5: { FzFunc: uintptr(unsafe.Pointer(&_ntileName)), FeFrmType: int32(TK_ROWS), FeStart: int32(TK_CURRENT), FeEnd: int32(TK_UNBOUNDED), }, 6: { FzFunc: uintptr(unsafe.Pointer(&_leadName)), FeFrmType: int32(TK_ROWS), FeStart: int32(TK_UNBOUNDED), FeEnd: int32(TK_UNBOUNDED), }, 7: { FzFunc: uintptr(unsafe.Pointer(&_lagName)), FeFrmType: int32(TK_ROWS), FeStart: int32(TK_UNBOUNDED), FeEnd: int32(TK_CURRENT), }, } i = 0 for { if !(i < int32(libc.Uint32FromInt64(128)/libc.Uint32FromInt64(16))) { break } if (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == aUp[i].FzFunc { _sqlite3ExprDelete(tls, db, (*TWindow)(unsafe.Pointer(pWin)).FpStart) _sqlite3ExprDelete(tls, db, (*TWindow)(unsafe.Pointer(pWin)).FpEnd) v2 = libc.UintptrFromInt32(0) (*TWindow)(unsafe.Pointer(pWin)).FpStart = v2 (*TWindow)(unsafe.Pointer(pWin)).FpEnd = v2 (*TWindow)(unsafe.Pointer(pWin)).FeFrmType = uint8(aUp[i].FeFrmType) (*TWindow)(unsafe.Pointer(pWin)).FeStart = uint8(aUp[i].FeStart) (*TWindow)(unsafe.Pointer(pWin)).FeEnd = uint8(aUp[i].FeEnd) (*TWindow)(unsafe.Pointer(pWin)).FeExclude = uint8(0) if int32((*TWindow)(unsafe.Pointer(pWin)).FeStart) == int32(TK_FOLLOWING) { (*TWindow)(unsafe.Pointer(pWin)).FpStart = _sqlite3Expr(tls, db, int32(TK_INTEGER), __ccgo_ts+8130) } break } goto _1 _1: ; i++ } } } } (*TWindow)(unsafe.Pointer(pWin)).FpWFunc = pFunc } // C documentation // // /* // ** Context object passed through sqlite3WalkExprList() to // ** selectWindowRewriteExprCb() by selectWindowRewriteEList(). // */ type TWindowRewrite = struct { FpWin uintptr FpSrc uintptr FpSub uintptr FpTab uintptr FpSubSelect uintptr } type WindowRewrite = TWindowRewrite type TWindowRewrite1 = struct { FpWin uintptr FpSrc uintptr FpSub uintptr FpTab uintptr FpSubSelect uintptr } type WindowRewrite1 = TWindowRewrite1 // C documentation // // /* // ** Callback function used by selectWindowRewriteEList(). If necessary, // ** this function appends to the output expression-list and updates // ** expression (*ppExpr) in place. // */ func _selectWindowRewriteExprCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var f, i, i1, iCol, nSrc, v4 int32 var p, pDup, pParse, pWin uintptr _, _, _, _, _, _, _, _, _, _ = f, i, i1, iCol, nSrc, p, pDup, pParse, pWin, v4 p = *(*uintptr)(unsafe.Pointer(pWalker + 24)) pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If this function is being called from within a scalar sub-select ** that used by the SELECT statement being processed, only process ** TK_COLUMN expressions that refer to it (the outer SELECT). Do ** not process aggregates or window functions at all, as they belong ** to the scalar sub-select. */ if (*TWindowRewrite1)(unsafe.Pointer(p)).FpSubSelect != 0 { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLUMN) { return WRC_Continue } else { nSrc = (*TSrcList)(unsafe.Pointer((*TWindowRewrite1)(unsafe.Pointer(p)).FpSrc)).FnSrc i = 0 for { if !(i < nSrc) { break } if (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*(*TSrcItem)(unsafe.Pointer((*TWindowRewrite1)(unsafe.Pointer(p)).FpSrc + 8 + uintptr(i)*72))).FiCursor { break } goto _1 _1: ; i++ } if i == nSrc { return WRC_Continue } } } switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) { case int32(TK_FUNCTION): if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != libc.Uint32FromInt32(0)) { break } else { pWin = (*TWindowRewrite1)(unsafe.Pointer(p)).FpWin for { if !(pWin != 0) { break } if *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)) == pWin { _ = libc.Int32FromInt32(0) return int32(WRC_Prune) } goto _2 _2: ; pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin } } fallthrough case int32(TK_IF_NULL_ROW): fallthrough case int32(TK_AGG_FUNCTION): fallthrough case int32(TK_COLUMN): iCol = -int32(1) if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 { return int32(WRC_Abort) } if (*TWindowRewrite1)(unsafe.Pointer(p)).FpSub != 0 { i1 = 0 for { if !(i1 < (*TExprList)(unsafe.Pointer((*TWindowRewrite1)(unsafe.Pointer(p)).FpSub)).FnExpr) { break } if 0 == _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer((*TWindowRewrite1)(unsafe.Pointer(p)).FpSub + 8 + uintptr(i1)*20))).FpExpr, pExpr, -int32(1)) { iCol = i1 break } goto _3 _3: ; i1++ } } if iCol < 0 { pDup = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr, 0) if pDup != 0 && int32((*TExpr)(unsafe.Pointer(pDup)).Fop) == int32(TK_AGG_FUNCTION) { (*TExpr)(unsafe.Pointer(pDup)).Fop = uint8(TK_FUNCTION) } (*TWindowRewrite1)(unsafe.Pointer(p)).FpSub = _sqlite3ExprListAppend(tls, pParse, (*TWindowRewrite1)(unsafe.Pointer(p)).FpSub, pDup) } if (*TWindowRewrite1)(unsafe.Pointer(p)).FpSub != 0 { f = int32((*TExpr)(unsafe.Pointer(pExpr)).Fflags & uint32(EP_Collate)) _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_Static)) _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr) *(*Tu32)(unsafe.Pointer(pExpr + 4)) &= uint32(^libc.Int32FromInt32(EP_Static)) libc.Xmemset(tls, pExpr, 0, uint32(52)) (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_COLUMN) if iCol < 0 { v4 = (*TExprList)(unsafe.Pointer((*TWindowRewrite1)(unsafe.Pointer(p)).FpSub)).FnExpr - int32(1) } else { v4 = iCol } (*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(v4) (*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TWindow)(unsafe.Pointer((*TWindowRewrite1)(unsafe.Pointer(p)).FpWin)).FiEphCsr (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab = (*TWindowRewrite1)(unsafe.Pointer(p)).FpTab (*TExpr)(unsafe.Pointer(pExpr)).Fflags = uint32(f) } if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 { return int32(WRC_Abort) } default: /* no-op */ break } return WRC_Continue } func _selectWindowRewriteSelectCb(tls *libc.TLS, pWalker uintptr, pSelect uintptr) (r int32) { var p, pSave uintptr _, _ = p, pSave p = *(*uintptr)(unsafe.Pointer(pWalker + 24)) pSave = (*TWindowRewrite1)(unsafe.Pointer(p)).FpSubSelect if pSave == pSelect { return WRC_Continue } else { (*TWindowRewrite1)(unsafe.Pointer(p)).FpSubSelect = pSelect _sqlite3WalkSelect(tls, pWalker, pSelect) (*TWindowRewrite1)(unsafe.Pointer(p)).FpSubSelect = pSave } return int32(WRC_Prune) } // C documentation // // /* // ** Iterate through each expression in expression-list pEList. For each: // ** // ** * TK_COLUMN, // ** * aggregate function, or // ** * window function with a Window object that is not a member of the // ** Window list passed as the second argument (pWin). // ** // ** Append the node to output expression-list (*ppSub). And replace it // ** with a TK_COLUMN that reads the (N-1)th element of table // ** pWin->iEphCsr, where N is the number of elements in (*ppSub) after // ** appending the new one. // */ func _selectWindowRewriteEList(tls *libc.TLS, pParse uintptr, pWin uintptr, pSrc uintptr, pEList uintptr, pTab uintptr, ppSub uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* sRewrite at bp+28 */ TWindowRewrite var _ /* sWalker at bp+0 */ TWalker _ = libc.Int32FromInt32(0) libc.Xmemset(tls, bp, 0, uint32(28)) libc.Xmemset(tls, bp+28, 0, uint32(20)) (*(*TWindowRewrite)(unsafe.Pointer(bp + 28))).FpSub = *(*uintptr)(unsafe.Pointer(ppSub)) (*(*TWindowRewrite)(unsafe.Pointer(bp + 28))).FpWin = pWin (*(*TWindowRewrite)(unsafe.Pointer(bp + 28))).FpSrc = pSrc (*(*TWindowRewrite)(unsafe.Pointer(bp + 28))).FpTab = pTab (*(*TWalker)(unsafe.Pointer(bp))).FpParse = pParse (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_selectWindowRewriteExprCb) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_selectWindowRewriteSelectCb) *(*uintptr)(unsafe.Pointer(bp + 24)) = bp + 28 _sqlite3WalkExprList(tls, bp, pEList) *(*uintptr)(unsafe.Pointer(ppSub)) = (*(*TWindowRewrite)(unsafe.Pointer(bp + 28))).FpSub } // C documentation // // /* // ** Append a copy of each expression in expression-list pAppend to // ** expression list pList. Return a pointer to the result list. // */ func _exprListAppendList(tls *libc.TLS, pParse uintptr, pList uintptr, pAppend uintptr, bIntToNull int32) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, pDup, pSub uintptr var i, nInit, v1 int32 var _ /* iDummy at bp+0 */ int32 _, _, _, _, _, _ = db, i, nInit, pDup, pSub, v1 if pAppend != 0 { if pList != 0 { v1 = (*TExprList)(unsafe.Pointer(pList)).FnExpr } else { v1 = 0 } nInit = v1 i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pAppend)).FnExpr) { break } db = (*TParse)(unsafe.Pointer(pParse)).Fdb pDup = _sqlite3ExprDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pAppend + 8 + uintptr(i)*20))).FpExpr, 0) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _sqlite3ExprDelete(tls, db, pDup) break } if bIntToNull != 0 { pSub = _sqlite3ExprSkipCollateAndLikely(tls, pDup) if _sqlite3ExprIsInteger(tls, pSub, bp) != 0 { (*TExpr)(unsafe.Pointer(pSub)).Fop = uint8(TK_NULL) *(*Tu32)(unsafe.Pointer(pSub + 4)) &= uint32(^(libc.Int32FromInt32(EP_IntValue) | libc.Int32FromInt32(EP_IsTrue) | libc.Int32FromInt32(EP_IsFalse))) *(*uintptr)(unsafe.Pointer(pSub + 8)) = uintptr(0) } } pList = _sqlite3ExprListAppend(tls, pParse, pList, pDup) if pList != 0 { (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(nInit+i)*20))).Ffg.FsortFlags = (*(*TExprList_item)(unsafe.Pointer(pAppend + 8 + uintptr(i)*20))).Ffg.FsortFlags } goto _2 _2: ; i++ } } return pList } // C documentation // // /* // ** When rewriting a query, if the new subquery in the FROM clause // ** contains TK_AGG_FUNCTION nodes that refer to an outer query, // ** then we have to increase the Expr->op2 values of those nodes // ** due to the extra subquery layer that was added. // ** // ** See also the incrAggDepth() routine in resolve.c // */ func _sqlite3WindowExtraAggFuncDepth(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_FUNCTION) && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop2) >= (*TWalker)(unsafe.Pointer(pWalker)).FwalkerDepth { (*TExpr)(unsafe.Pointer(pExpr)).Fop2++ } return WRC_Continue } func _disallowAggregatesInOrderByCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_FUNCTION) && (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo == uintptr(0) { _ = libc.Int32FromInt32(0) _sqlite3ErrorMsg(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, __ccgo_ts+22810, libc.VaList(bp+8, *(*uintptr)(unsafe.Pointer(pExpr + 8)))) } return WRC_Continue } // C documentation // // /* // ** If the SELECT statement passed as the second argument does not invoke // ** any SQL window functions, this function is a no-op. Otherwise, it // ** rewrites the SELECT statement so that window function xStep functions // ** are invoked in the correct order as described under "SELECT REWRITING" // ** at the top of this file. // */ func _sqlite3WindowRewrite(tls *libc.TLS, pParse uintptr, p uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var db, pArgs, pFilter, pGroupBy, pHaving, pMWin, pSort, pSrc, pSub, pTab, pTab2, pWhere, pWin, v, v10, v2, v8 uintptr var nSave, rc, v1, v3, v5, v6, v7, v9 int32 var selFlags Tu32 var _ /* pSublist at bp+0 */ uintptr var _ /* w at bp+4 */ TWalker _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, nSave, pArgs, pFilter, pGroupBy, pHaving, pMWin, pSort, pSrc, pSub, pTab, pTab2, pWhere, pWin, rc, selFlags, v, v1, v10, v2, v3, v5, v6, v7, v8, v9 rc = SQLITE_OK if (*TSelect)(unsafe.Pointer(p)).FpWin != 0 && (*TSelect)(unsafe.Pointer(p)).FpPrior == uintptr(0) && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_WinRewrite) == uint32(0) && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) { v = _sqlite3GetVdbe(tls, pParse) db = (*TParse)(unsafe.Pointer(pParse)).Fdb pSub = uintptr(0) /* The subquery */ pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc pWhere = (*TSelect)(unsafe.Pointer(p)).FpWhere pGroupBy = (*TSelect)(unsafe.Pointer(p)).FpGroupBy pHaving = (*TSelect)(unsafe.Pointer(p)).FpHaving pSort = uintptr(0) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Expression list for sub-query */ pMWin = (*TSelect)(unsafe.Pointer(p)).FpWin selFlags = (*TSelect)(unsafe.Pointer(p)).FselFlags pTab = _sqlite3DbMallocZero(tls, db, uint64(64)) if pTab == uintptr(0) { return _sqlite3ErrorToParser(tls, db, int32(SQLITE_NOMEM)) } _sqlite3AggInfoPersistWalkerInit(tls, bp+4, pParse) _sqlite3WalkSelect(tls, bp+4, p) if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Aggregate) == uint32(0) { (*(*TWalker)(unsafe.Pointer(bp + 4))).FxExprCallback = __ccgo_fp(_disallowAggregatesInOrderByCb) (*(*TWalker)(unsafe.Pointer(bp + 4))).FxSelectCallback = uintptr(0) _sqlite3WalkExprList(tls, bp+4, (*TSelect)(unsafe.Pointer(p)).FpOrderBy) } (*TSelect)(unsafe.Pointer(p)).FpSrc = uintptr(0) (*TSelect)(unsafe.Pointer(p)).FpWhere = uintptr(0) (*TSelect)(unsafe.Pointer(p)).FpGroupBy = uintptr(0) (*TSelect)(unsafe.Pointer(p)).FpHaving = uintptr(0) *(*Tu32)(unsafe.Pointer(p + 4)) &= uint32(^libc.Int32FromInt32(SF_Aggregate)) *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(SF_WinRewrite) /* Create the ORDER BY clause for the sub-select. This is the concatenation ** of the window PARTITION and ORDER BY clauses. Then, if this makes it ** redundant, remove the ORDER BY from the parent SELECT. */ pSort = _exprListAppendList(tls, pParse, uintptr(0), (*TWindow)(unsafe.Pointer(pMWin)).FpPartition, int32(1)) pSort = _exprListAppendList(tls, pParse, pSort, (*TWindow)(unsafe.Pointer(pMWin)).FpOrderBy, int32(1)) if pSort != 0 && (*TSelect)(unsafe.Pointer(p)).FpOrderBy != 0 && (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpOrderBy)).FnExpr <= (*TExprList)(unsafe.Pointer(pSort)).FnExpr { nSave = (*TExprList)(unsafe.Pointer(pSort)).FnExpr (*TExprList)(unsafe.Pointer(pSort)).FnExpr = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpOrderBy)).FnExpr if _sqlite3ExprListCompare(tls, pSort, (*TSelect)(unsafe.Pointer(p)).FpOrderBy, -int32(1)) == 0 { _sqlite3ExprListDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpOrderBy) (*TSelect)(unsafe.Pointer(p)).FpOrderBy = uintptr(0) } (*TExprList)(unsafe.Pointer(pSort)).FnExpr = nSave } /* Assign a cursor number for the ephemeral table used to buffer rows. ** The OpenEphemeral instruction is coded later, after it is known how ** many columns the table will have. */ v2 = pParse + 40 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr = v1 *(*int32)(unsafe.Pointer(pParse + 40)) += int32(3) _selectWindowRewriteEList(tls, pParse, pMWin, pSrc, (*TSelect)(unsafe.Pointer(p)).FpEList, pTab, bp) _selectWindowRewriteEList(tls, pParse, pMWin, pSrc, (*TSelect)(unsafe.Pointer(p)).FpOrderBy, pTab, bp) if *(*uintptr)(unsafe.Pointer(bp)) != 0 { v3 = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnExpr } else { v3 = 0 } (*TWindow)(unsafe.Pointer(pMWin)).FnBufferCol = v3 /* Append the PARTITION BY and ORDER BY expressions to the to the ** sub-select expression list. They are required to figure out where ** boundaries for partitions and sets of peer rows lie. */ *(*uintptr)(unsafe.Pointer(bp)) = _exprListAppendList(tls, pParse, *(*uintptr)(unsafe.Pointer(bp)), (*TWindow)(unsafe.Pointer(pMWin)).FpPartition, 0) *(*uintptr)(unsafe.Pointer(bp)) = _exprListAppendList(tls, pParse, *(*uintptr)(unsafe.Pointer(bp)), (*TWindow)(unsafe.Pointer(pMWin)).FpOrderBy, 0) /* Append the arguments passed to each window function to the ** sub-select expression list. Also allocate two registers for each ** window function - one for the accumulator, another for interim ** results. */ pWin = pMWin for { if !(pWin != 0) { break } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pArgs = *(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 20)) if (*TFuncDef)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpWFunc)).FfuncFlags&uint32(SQLITE_SUBTYPE) != 0 { _selectWindowRewriteEList(tls, pParse, pMWin, pSrc, pArgs, pTab, bp) if *(*uintptr)(unsafe.Pointer(bp)) != 0 { v5 = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnExpr } else { v5 = 0 } (*TWindow)(unsafe.Pointer(pWin)).FiArgCol = v5 (*TWindow)(unsafe.Pointer(pWin)).FbExprArgs = uint8(1) } else { if *(*uintptr)(unsafe.Pointer(bp)) != 0 { v6 = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnExpr } else { v6 = 0 } (*TWindow)(unsafe.Pointer(pWin)).FiArgCol = v6 *(*uintptr)(unsafe.Pointer(bp)) = _exprListAppendList(tls, pParse, *(*uintptr)(unsafe.Pointer(bp)), pArgs, 0) } if (*TWindow)(unsafe.Pointer(pWin)).FpFilter != 0 { pFilter = _sqlite3ExprDup(tls, db, (*TWindow)(unsafe.Pointer(pWin)).FpFilter, 0) *(*uintptr)(unsafe.Pointer(bp)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(bp)), pFilter) } v8 = pParse + 44 *(*int32)(unsafe.Pointer(v8))++ v7 = *(*int32)(unsafe.Pointer(v8)) (*TWindow)(unsafe.Pointer(pWin)).FregAccum = v7 v10 = pParse + 44 *(*int32)(unsafe.Pointer(v10))++ v9 = *(*int32)(unsafe.Pointer(v10)) (*TWindow)(unsafe.Pointer(pWin)).FregResult = v9 _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, (*TWindow)(unsafe.Pointer(pWin)).FregAccum) goto _4 _4: ; pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin } /* If there is no ORDER BY or PARTITION BY clause, and the window ** function accepts zero arguments, and there are no other columns ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible ** that pSublist is still NULL here. Add a constant expression here to ** keep everything legal in this case. */ if *(*uintptr)(unsafe.Pointer(bp)) == uintptr(0) { *(*uintptr)(unsafe.Pointer(bp)) = _sqlite3ExprListAppend(tls, pParse, uintptr(0), _sqlite3Expr(tls, db, int32(TK_INTEGER), __ccgo_ts+1722)) } pSub = _sqlite3SelectNew(tls, pParse, *(*uintptr)(unsafe.Pointer(bp)), pSrc, pWhere, pGroupBy, pHaving, pSort, uint32(0), uintptr(0)) (*TSelect)(unsafe.Pointer(p)).FpSrc = _sqlite3SrcListAppend(tls, pParse, uintptr(0), uintptr(0), uintptr(0)) _ = libc.Int32FromInt32(0) /* Due to db->mallocFailed test inside ** of sqlite3DbMallocRawNN() called from ** sqlite3SrcListAppend() */ if (*TSelect)(unsafe.Pointer(p)).FpSrc != 0 { (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpSelect = pSub libc.SetBitFieldPtr16Uint32((*TSelect)(unsafe.Pointer(p)).FpSrc+8+36+4, libc.Uint32FromInt32(1), 3, 0x8) _sqlite3SrcListAssignCursors(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpSrc) *(*Tu32)(unsafe.Pointer(pSub + 4)) |= uint32(libc.Int32FromInt32(SF_Expanded) | libc.Int32FromInt32(SF_OrderByReqd)) pTab2 = _sqlite3ResultSetOfSelect(tls, pParse, pSub, uint8(SQLITE_AFF_NONE)) *(*Tu32)(unsafe.Pointer(pSub + 4)) |= selFlags & uint32(SF_Aggregate) if pTab2 == uintptr(0) { /* Might actually be some other kind of error, but in that case ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get ** the correct error message regardless. */ rc = int32(SQLITE_NOMEM) } else { libc.Xmemcpy(tls, pTab, pTab2, uint32(64)) *(*Tu32)(unsafe.Pointer(pTab + 28)) |= uint32(TF_Ephemeral) (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpTab = pTab pTab = pTab2 libc.Xmemset(tls, bp+4, 0, uint32(28)) (*(*TWalker)(unsafe.Pointer(bp + 4))).FxExprCallback = __ccgo_fp(_sqlite3WindowExtraAggFuncDepth) (*(*TWalker)(unsafe.Pointer(bp + 4))).FxSelectCallback = __ccgo_fp(_sqlite3WalkerDepthIncrease) (*(*TWalker)(unsafe.Pointer(bp + 4))).FxSelectCallback2 = __ccgo_fp(_sqlite3WalkerDepthDecrease) _sqlite3WalkSelect(tls, bp+4, pSub) } } else { _sqlite3SelectDelete(tls, db, pSub) } if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { rc = int32(SQLITE_NOMEM) } /* Defer deleting the temporary table pTab because if an error occurred, ** there could still be references to that table embedded in the ** result-set or ORDER BY clause of the SELECT statement p. */ _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3DbFree), pTab) } _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** Unlink the Window object from the Select to which it is attached, // ** if it is attached. // */ func _sqlite3WindowUnlinkFromSelect(tls *libc.TLS, p uintptr) { if (*TWindow)(unsafe.Pointer(p)).FppThis != 0 { *(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(p)).FppThis)) = (*TWindow)(unsafe.Pointer(p)).FpNextWin if (*TWindow)(unsafe.Pointer(p)).FpNextWin != 0 { (*TWindow)(unsafe.Pointer((*TWindow)(unsafe.Pointer(p)).FpNextWin)).FppThis = (*TWindow)(unsafe.Pointer(p)).FppThis } (*TWindow)(unsafe.Pointer(p)).FppThis = uintptr(0) } } // C documentation // // /* // ** Free the Window object passed as the second argument. // */ func _sqlite3WindowDelete(tls *libc.TLS, db uintptr, p uintptr) { if p != 0 { _sqlite3WindowUnlinkFromSelect(tls, p) _sqlite3ExprDelete(tls, db, (*TWindow)(unsafe.Pointer(p)).FpFilter) _sqlite3ExprListDelete(tls, db, (*TWindow)(unsafe.Pointer(p)).FpPartition) _sqlite3ExprListDelete(tls, db, (*TWindow)(unsafe.Pointer(p)).FpOrderBy) _sqlite3ExprDelete(tls, db, (*TWindow)(unsafe.Pointer(p)).FpEnd) _sqlite3ExprDelete(tls, db, (*TWindow)(unsafe.Pointer(p)).FpStart) _sqlite3DbFree(tls, db, (*TWindow)(unsafe.Pointer(p)).FzName) _sqlite3DbFree(tls, db, (*TWindow)(unsafe.Pointer(p)).FzBase) _sqlite3DbFree(tls, db, p) } } // C documentation // // /* // ** Free the linked list of Window objects starting at the second argument. // */ func _sqlite3WindowListDelete(tls *libc.TLS, db uintptr, p uintptr) { var pNext uintptr _ = pNext for p != 0 { pNext = (*TWindow)(unsafe.Pointer(p)).FpNextWin _sqlite3WindowDelete(tls, db, p) p = pNext } } // C documentation // // /* // ** The argument expression is an PRECEDING or FOLLOWING offset. The // ** value should be a non-negative integer. If the value is not a // ** constant, change it to NULL. The fact that it is then a non-negative // ** integer will be caught later. But it is important not to leave // ** variable values in the expression tree. // */ func _sqlite3WindowOffsetExpr(tls *libc.TLS, pParse uintptr, pExpr uintptr) (r uintptr) { if 0 == _sqlite3ExprIsConstant(tls, pExpr) { if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameExprUnmap(tls, pParse, pExpr) } _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr) pExpr = _sqlite3ExprAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_NULL), uintptr(0), 0) } return pExpr } // C documentation // // /* // ** Allocate and return a new Window object describing a Window Definition. // */ func _sqlite3WindowAlloc(tls *libc.TLS, pParse uintptr, eType int32, eStart int32, pStart uintptr, eEnd int32, pEnd uintptr, eExclude Tu8) (r uintptr) { var bImplicitFrame int32 var pWin uintptr _, _ = bImplicitFrame, pWin pWin = uintptr(0) bImplicitFrame = 0 /* Parser assures the following: */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if eType == 0 { bImplicitFrame = int32(1) eType = int32(TK_RANGE) } /* Additionally, the ** starting boundary type may not occur earlier in the following list than ** the ending boundary type: ** ** UNBOUNDED PRECEDING ** PRECEDING ** CURRENT ROW ** FOLLOWING ** UNBOUNDED FOLLOWING ** ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting ** frame boundary. */ if eStart == int32(TK_CURRENT) && eEnd == int32(TK_PRECEDING) || eStart == int32(TK_FOLLOWING) && (eEnd == int32(TK_PRECEDING) || eEnd == int32(TK_CURRENT)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22836, 0) goto windowAllocErr } pWin = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(100)) if pWin == uintptr(0) { goto windowAllocErr } (*TWindow)(unsafe.Pointer(pWin)).FeFrmType = uint8(eType) (*TWindow)(unsafe.Pointer(pWin)).FeStart = uint8(eStart) (*TWindow)(unsafe.Pointer(pWin)).FeEnd = uint8(eEnd) if int32(eExclude) == 0 && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_WindowFunc)) != uint32(0) { eExclude = uint8(TK_NO) } (*TWindow)(unsafe.Pointer(pWin)).FeExclude = eExclude (*TWindow)(unsafe.Pointer(pWin)).FbImplicitFrame = uint8(bImplicitFrame) (*TWindow)(unsafe.Pointer(pWin)).FpEnd = _sqlite3WindowOffsetExpr(tls, pParse, pEnd) (*TWindow)(unsafe.Pointer(pWin)).FpStart = _sqlite3WindowOffsetExpr(tls, pParse, pStart) return pWin goto windowAllocErr windowAllocErr: ; _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pEnd) _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pStart) return uintptr(0) } // C documentation // // /* // ** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window // ** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the // ** equivalent nul-terminated string. // */ func _sqlite3WindowAssemble(tls *libc.TLS, pParse uintptr, pWin uintptr, pPartition uintptr, pOrderBy uintptr, pBase uintptr) (r uintptr) { if pWin != 0 { (*TWindow)(unsafe.Pointer(pWin)).FpPartition = pPartition (*TWindow)(unsafe.Pointer(pWin)).FpOrderBy = pOrderBy if pBase != 0 { (*TWindow)(unsafe.Pointer(pWin)).FzBase = _sqlite3DbStrNDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TToken)(unsafe.Pointer(pBase)).Fz, uint64((*TToken)(unsafe.Pointer(pBase)).Fn)) } } else { _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pPartition) _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pOrderBy) } return pWin } // C documentation // // /* // ** Window *pWin has just been created from a WINDOW clause. Token pBase // ** is the base window. Earlier windows from the same WINDOW clause are // ** stored in the linked list starting at pWin->pNextWin. This function // ** either updates *pWin according to the base specification, or else // ** leaves an error in pParse. // */ func _sqlite3WindowChain(tls *libc.TLS, pParse uintptr, pWin uintptr, pList uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var db, pExist, zErr uintptr _, _, _ = db, pExist, zErr if (*TWindow)(unsafe.Pointer(pWin)).FzBase != 0 { db = (*TParse)(unsafe.Pointer(pParse)).Fdb pExist = _windowFind(tls, pParse, pList, (*TWindow)(unsafe.Pointer(pWin)).FzBase) if pExist != 0 { zErr = uintptr(0) /* Check for errors */ if (*TWindow)(unsafe.Pointer(pWin)).FpPartition != 0 { zErr = __ccgo_ts + 22868 } else { if (*TWindow)(unsafe.Pointer(pExist)).FpOrderBy != 0 && (*TWindow)(unsafe.Pointer(pWin)).FpOrderBy != 0 { zErr = __ccgo_ts + 22885 } else { if int32((*TWindow)(unsafe.Pointer(pExist)).FbImplicitFrame) == 0 { zErr = __ccgo_ts + 22901 } } } if zErr != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22921, libc.VaList(bp+8, zErr, (*TWindow)(unsafe.Pointer(pWin)).FzBase)) } else { (*TWindow)(unsafe.Pointer(pWin)).FpPartition = _sqlite3ExprListDup(tls, db, (*TWindow)(unsafe.Pointer(pExist)).FpPartition, 0) if (*TWindow)(unsafe.Pointer(pExist)).FpOrderBy != 0 { _ = libc.Int32FromInt32(0) (*TWindow)(unsafe.Pointer(pWin)).FpOrderBy = _sqlite3ExprListDup(tls, db, (*TWindow)(unsafe.Pointer(pExist)).FpOrderBy, 0) } _sqlite3DbFree(tls, db, (*TWindow)(unsafe.Pointer(pWin)).FzBase) (*TWindow)(unsafe.Pointer(pWin)).FzBase = uintptr(0) } } } } // C documentation // // /* // ** Attach window object pWin to expression p. // */ func _sqlite3WindowAttach(tls *libc.TLS, pParse uintptr, p uintptr, pWin uintptr) { if p != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(p)).Fy)) = pWin *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(libc.Int32FromInt32(EP_WinFunc) | libc.Int32FromInt32(EP_FullSize)) (*TWindow)(unsafe.Pointer(pWin)).FpOwner = p if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_Distinct) != 0 && int32((*TWindow)(unsafe.Pointer(pWin)).FeFrmType) != int32(TK_FILTER) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22954, 0) } } else { _sqlite3WindowDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pWin) } } // C documentation // // /* // ** Possibly link window pWin into the list at pSel->pWin (window functions // ** to be processed as part of SELECT statement pSel). The window is linked // ** in if either (a) there are no other windows already linked to this // ** SELECT, or (b) the windows already linked use a compatible window frame. // */ func _sqlite3WindowLink(tls *libc.TLS, pSel uintptr, pWin uintptr) { if pSel != 0 { if uintptr(0) == (*TSelect)(unsafe.Pointer(pSel)).FpWin || 0 == _sqlite3WindowCompare(tls, uintptr(0), (*TSelect)(unsafe.Pointer(pSel)).FpWin, pWin, 0) { (*TWindow)(unsafe.Pointer(pWin)).FpNextWin = (*TSelect)(unsafe.Pointer(pSel)).FpWin if (*TSelect)(unsafe.Pointer(pSel)).FpWin != 0 { (*TWindow)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSel)).FpWin)).FppThis = pWin + 36 } (*TSelect)(unsafe.Pointer(pSel)).FpWin = pWin (*TWindow)(unsafe.Pointer(pWin)).FppThis = pSel + 68 } else { if _sqlite3ExprListCompare(tls, (*TWindow)(unsafe.Pointer(pWin)).FpPartition, (*TWindow)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSel)).FpWin)).FpPartition, -int32(1)) != 0 { *(*Tu32)(unsafe.Pointer(pSel + 4)) |= uint32(SF_MultiPart) } } } } // C documentation // // /* // ** Return 0 if the two window objects are identical, 1 if they are // ** different, or 2 if it cannot be determined if the objects are identical // ** or not. Identical window objects can be processed in a single scan. // */ func _sqlite3WindowCompare(tls *libc.TLS, pParse uintptr, p1 uintptr, p2 uintptr, bFilter int32) (r int32) { var res, v1, v2, v3 int32 _, _, _, _ = res, v1, v2, v3 if p1 == uintptr(0) || p2 == uintptr(0) { return int32(1) } if int32((*TWindow)(unsafe.Pointer(p1)).FeFrmType) != int32((*TWindow)(unsafe.Pointer(p2)).FeFrmType) { return int32(1) } if int32((*TWindow)(unsafe.Pointer(p1)).FeStart) != int32((*TWindow)(unsafe.Pointer(p2)).FeStart) { return int32(1) } if int32((*TWindow)(unsafe.Pointer(p1)).FeEnd) != int32((*TWindow)(unsafe.Pointer(p2)).FeEnd) { return int32(1) } if int32((*TWindow)(unsafe.Pointer(p1)).FeExclude) != int32((*TWindow)(unsafe.Pointer(p2)).FeExclude) { return int32(1) } if _sqlite3ExprCompare(tls, pParse, (*TWindow)(unsafe.Pointer(p1)).FpStart, (*TWindow)(unsafe.Pointer(p2)).FpStart, -int32(1)) != 0 { return int32(1) } if _sqlite3ExprCompare(tls, pParse, (*TWindow)(unsafe.Pointer(p1)).FpEnd, (*TWindow)(unsafe.Pointer(p2)).FpEnd, -int32(1)) != 0 { return int32(1) } v1 = _sqlite3ExprListCompare(tls, (*TWindow)(unsafe.Pointer(p1)).FpPartition, (*TWindow)(unsafe.Pointer(p2)).FpPartition, -int32(1)) res = v1 if v1 != 0 { return res } v2 = _sqlite3ExprListCompare(tls, (*TWindow)(unsafe.Pointer(p1)).FpOrderBy, (*TWindow)(unsafe.Pointer(p2)).FpOrderBy, -int32(1)) res = v2 if v2 != 0 { return res } if bFilter != 0 { v3 = _sqlite3ExprCompare(tls, pParse, (*TWindow)(unsafe.Pointer(p1)).FpFilter, (*TWindow)(unsafe.Pointer(p2)).FpFilter, -int32(1)) res = v3 if v3 != 0 { return res } } return 0 } // C documentation // // /* // ** This is called by code in select.c before it calls sqlite3WhereBegin() // ** to begin iterating through the sub-query results. It is used to allocate // ** and initialize registers and cursors used by sqlite3WindowCodeStep(). // */ func _sqlite3WindowCodeInit(tls *libc.TLS, pParse uintptr, pSelect uintptr) { var nEphExpr, nExpr, v1, v10, v12, v14, v3, v5, v7 int32 var p, pKeyInfo, pList, pMWin, pWin, v, v11, v13, v15, v2, v4, v6, v8 uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = nEphExpr, nExpr, p, pKeyInfo, pList, pMWin, pWin, v, v1, v10, v11, v12, v13, v14, v15, v2, v3, v4, v5, v6, v7, v8 nEphExpr = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpSrc + 8))).FpSelect)).FpEList)).FnExpr pMWin = (*TSelect)(unsafe.Pointer(pSelect)).FpWin v = _sqlite3GetVdbe(tls, pParse) _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr, nEphExpr) _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr+int32(1), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr) _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr+int32(2), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr) _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr+int32(3), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr) /* Allocate registers to use for PARTITION BY values, if any. Initialize ** said registers to NULL. */ if (*TWindow)(unsafe.Pointer(pMWin)).FpPartition != 0 { nExpr = (*TExprList)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pMWin)).FpPartition)).FnExpr (*TWindow)(unsafe.Pointer(pMWin)).FregPart = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nExpr _sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, (*TWindow)(unsafe.Pointer(pMWin)).FregPart, (*TWindow)(unsafe.Pointer(pMWin)).FregPart+nExpr-int32(1)) } v2 = pParse + 44 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) (*TWindow)(unsafe.Pointer(pMWin)).FregOne = v1 _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), (*TWindow)(unsafe.Pointer(pMWin)).FregOne) if (*TWindow)(unsafe.Pointer(pMWin)).FeExclude != 0 { v4 = pParse + 44 *(*int32)(unsafe.Pointer(v4))++ v3 = *(*int32)(unsafe.Pointer(v4)) (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid = v3 v6 = pParse + 44 *(*int32)(unsafe.Pointer(v6))++ v5 = *(*int32)(unsafe.Pointer(v6)) (*TWindow)(unsafe.Pointer(pMWin)).FregEndRowid = v5 v8 = pParse + 40 v7 = *(*int32)(unsafe.Pointer(v8)) *(*int32)(unsafe.Pointer(v8))++ (*TWindow)(unsafe.Pointer(pMWin)).FcsrApp = v7 _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (*TWindow)(unsafe.Pointer(pMWin)).FregEndRowid) _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TWindow)(unsafe.Pointer(pMWin)).FcsrApp, (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr) return } pWin = pMWin for { if !(pWin != 0) { break } p = (*TWindow)(unsafe.Pointer(pWin)).FpWFunc if (*TFuncDef)(unsafe.Pointer(p)).FfuncFlags&uint32(SQLITE_FUNC_MINMAX) != 0 && int32((*TWindow)(unsafe.Pointer(pWin)).FeStart) != int32(TK_UNBOUNDED) { _ = libc.Int32FromInt32(0) pList = *(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 20)) pKeyInfo = _sqlite3KeyInfoFromExprList(tls, pParse, pList, 0, 0) v11 = pParse + 40 v10 = *(*int32)(unsafe.Pointer(v11)) *(*int32)(unsafe.Pointer(v11))++ (*TWindow)(unsafe.Pointer(pWin)).FcsrApp = v10 (*TWindow)(unsafe.Pointer(pWin)).FregApp = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += int32(3) if pKeyInfo != 0 && int32(*(*uint8)(unsafe.Pointer((*TFuncDef)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpWFunc)).FzName + 1))) == int32('i') { _ = libc.Int32FromInt32(0) *(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags)) = uint8(KEYINFO_ORDER_DESC) } _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp, int32(2)) _sqlite3VdbeAppendP4(tls, v, pKeyInfo, -int32(8)) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (*TWindow)(unsafe.Pointer(pWin)).FregApp+int32(1)) } else { if (*TFuncDef)(unsafe.Pointer(p)).FzName == uintptr(unsafe.Pointer(&_nth_valueName)) || (*TFuncDef)(unsafe.Pointer(p)).FzName == uintptr(unsafe.Pointer(&_first_valueName)) { /* Allocate two registers at pWin->regApp. These will be used to ** store the start and end index of the current frame. */ (*TWindow)(unsafe.Pointer(pWin)).FregApp = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) v13 = pParse + 40 v12 = *(*int32)(unsafe.Pointer(v13)) *(*int32)(unsafe.Pointer(v13))++ (*TWindow)(unsafe.Pointer(pWin)).FcsrApp = v12 *(*int32)(unsafe.Pointer(pParse + 44)) += int32(2) _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp, (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr) } else { if (*TFuncDef)(unsafe.Pointer(p)).FzName == uintptr(unsafe.Pointer(&_leadName)) || (*TFuncDef)(unsafe.Pointer(p)).FzName == uintptr(unsafe.Pointer(&_lagName)) { v15 = pParse + 40 v14 = *(*int32)(unsafe.Pointer(v15)) *(*int32)(unsafe.Pointer(v15))++ (*TWindow)(unsafe.Pointer(pWin)).FcsrApp = v14 _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp, (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr) } } } goto _9 _9: ; pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin } } // C documentation // // /* // ** A "PRECEDING " (eCond==0) or "FOLLOWING " (eCond==1) or the // ** value of the second argument to nth_value() (eCond==2) has just been // ** evaluated and the result left in register reg. This function generates VM // ** code to check that the value is a non-negative integer and throws an // ** exception if it is not. // */ func _windowCheckValue(tls *libc.TLS, pParse uintptr, reg int32, eCond int32) { var regString, regZero int32 var v uintptr _, _, _ = regString, regZero, v v = _sqlite3GetVdbe(tls, pParse) regZero = _sqlite3GetTempReg(tls, pParse) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regZero) if eCond >= int32(WINDOW_STARTING_NUM) { regString = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, regString, 0, __ccgo_ts+1648, -int32(1)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Ge), regString, _sqlite3VdbeCurrentAddr(tls, v)+int32(2), reg) _sqlite3VdbeChangeP5(tls, v, uint16(libc.Int32FromInt32(SQLITE_AFF_NUMERIC)|libc.Int32FromInt32(SQLITE_JUMPIFNULL))) _ = libc.Int32FromInt32(0) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_MustBeInt), reg, _sqlite3VdbeCurrentAddr(tls, v)+int32(2)) _ = libc.Int32FromInt32(0) } _sqlite3VdbeAddOp3(tls, v, _aOp1[eCond], regZero, _sqlite3VdbeCurrentAddr(tls, v)+int32(2), reg) _sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_AFF_NUMERIC)) /* NULL case captured by */ /* the OP_MustBeInt */ /* NULL case caught by */ /* the OP_Ge */ _sqlite3MayAbort(tls, pParse) _sqlite3VdbeAddOp2(tls, v, int32(OP_Halt), int32(SQLITE_ERROR), int32(OE_Abort)) _sqlite3VdbeAppendP4(tls, v, _azErr[eCond], -int32(1)) _sqlite3ReleaseTempReg(tls, pParse, regZero) } var _azErr = [5]uintptr{ 0: __ccgo_ts + 23001, 1: __ccgo_ts + 23054, 2: __ccgo_ts + 22556, 3: __ccgo_ts + 23105, 4: __ccgo_ts + 23157, } var _aOp1 = [5]int32{ 0: int32(OP_Ge), 1: int32(OP_Ge), 2: int32(OP_Gt), 3: int32(OP_Ge), 4: int32(OP_Ge), } // C documentation // // /* // ** Return the number of arguments passed to the window-function associated // ** with the object passed as the only argument to this function. // */ func _windowArgCount(tls *libc.TLS, pWin uintptr) (r int32) { var pList uintptr var v1 int32 _, _ = pList, v1 _ = libc.Int32FromInt32(0) pList = *(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 20)) if pList != 0 { v1 = (*TExprList)(unsafe.Pointer(pList)).FnExpr } else { v1 = 0 } return v1 } type TWindowCodeArg = struct { FpParse uintptr FpMWin uintptr FpVdbe uintptr FaddrGosub int32 FregGosub int32 FregArg int32 FeDelete int32 FregRowid int32 Fstart TWindowCsrAndReg Fcurrent TWindowCsrAndReg Fend TWindowCsrAndReg } type WindowCodeArg = TWindowCodeArg type TWindowCsrAndReg = struct { Fcsr int32 Freg int32 } type WindowCsrAndReg = TWindowCsrAndReg /* ** See comments above struct WindowCodeArg. */ type TWindowCsrAndReg1 = struct { Fcsr int32 Freg int32 } type WindowCsrAndReg1 = TWindowCsrAndReg1 /* ** A single instance of this structure is allocated on the stack by ** sqlite3WindowCodeStep() and a pointer to it passed to the various helper ** routines. This is to reduce the number of arguments required by each ** helper function. ** ** regArg: ** Each window function requires an accumulator register (just as an ** ordinary aggregate function does). This variable is set to the first ** in an array of accumulator registers - one for each window function ** in the WindowCodeArg.pMWin list. ** ** eDelete: ** The window functions implementation sometimes caches the input rows ** that it processes in a temporary table. If it is not zero, this ** variable indicates when rows may be removed from the temp table (in ** order to reduce memory requirements - it would always be safe just ** to leave them there). Possible values for eDelete are: ** ** WINDOW_RETURN_ROW: ** An input row can be discarded after it is returned to the caller. ** ** WINDOW_AGGINVERSE: ** An input row can be discarded after the window functions xInverse() ** callbacks have been invoked in it. ** ** WINDOW_AGGSTEP: ** An input row can be discarded after the window functions xStep() ** callbacks have been invoked in it. ** ** start,current,end ** Consider a window-frame similar to the following: ** ** (ORDER BY a, b GROUPS BETWEEN 2 PRECEDING AND 2 FOLLOWING) ** ** The windows functions implementation caches the input rows in a temp ** table, sorted by "a, b" (it actually populates the cache lazily, and ** aggressively removes rows once they are no longer required, but that's ** a mere detail). It keeps three cursors open on the temp table. One ** (current) that points to the next row to return to the query engine ** once its window function values have been calculated. Another (end) ** points to the next row to call the xStep() method of each window function ** on (so that it is 2 groups ahead of current). And a third (start) that ** points to the next row to call the xInverse() method of each window ** function on. ** ** Each cursor (start, current and end) consists of a VDBE cursor ** (WindowCsrAndReg.csr) and an array of registers (starting at ** WindowCodeArg.reg) that always contains a copy of the peer values ** read from the corresponding cursor. ** ** Depending on the window-frame in question, all three cursors may not ** be required. In this case both WindowCodeArg.csr and reg are set to ** 0. */ type TWindowCodeArg1 = struct { FpParse uintptr FpMWin uintptr FpVdbe uintptr FaddrGosub int32 FregGosub int32 FregArg int32 FeDelete int32 FregRowid int32 Fstart TWindowCsrAndReg Fcurrent TWindowCsrAndReg Fend TWindowCsrAndReg } type WindowCodeArg1 = TWindowCodeArg1 // C documentation // // /* // ** Generate VM code to read the window frames peer values from cursor csr into // ** an array of registers starting at reg. // */ func _windowReadPeerValues(tls *libc.TLS, p uintptr, csr int32, reg int32) { var i, iColOff, v1 int32 var pMWin, pOrderBy, pPart, v uintptr _, _, _, _, _, _, _ = i, iColOff, pMWin, pOrderBy, pPart, v, v1 pMWin = (*TWindowCodeArg)(unsafe.Pointer(p)).FpMWin pOrderBy = (*TWindow)(unsafe.Pointer(pMWin)).FpOrderBy if pOrderBy != 0 { v = _sqlite3GetVdbe(tls, (*TWindowCodeArg)(unsafe.Pointer(p)).FpParse) pPart = (*TWindow)(unsafe.Pointer(pMWin)).FpPartition if pPart != 0 { v1 = (*TExprList)(unsafe.Pointer(pPart)).FnExpr } else { v1 = 0 } iColOff = (*TWindow)(unsafe.Pointer(pMWin)).FnBufferCol + v1 i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) { break } _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), csr, iColOff+i, reg+i) goto _2 _2: ; i++ } } } // C documentation // // /* // ** Generate VM code to invoke either xStep() (if bInverse is 0) or // ** xInverse (if bInverse is non-zero) for each window function in the // ** linked list starting at pMWin. Or, for built-in window functions // ** that do not use the standard function API, generate the required // ** inline VM code. // ** // ** If argument csr is greater than or equal to 0, then argument reg is // ** the first register in an array of registers guaranteed to be large // ** enough to hold the array of arguments for each function. In this case // ** the arguments are extracted from the current row of csr into the // ** array of registers before invoking OP_AggStep or OP_AggInverse // ** // ** Or, if csr is less than zero, then the array of registers at reg is // ** already populated with all columns from the current row of the sub-query. // ** // ** If argument regPartSize is non-zero, then it is a register containing the // ** number of rows in the current partition. // */ func _windowAggStep(tls *libc.TLS, p uintptr, pMWin uintptr, csr int32, bInverse int32, reg int32) { var addrIf, addrIsNull, i, iEnd, iOp, nArg, regArg, regTmp, v2, v5 int32 var pColl, pFunc, pOp, pParse, pWin, v uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrIf, addrIsNull, i, iEnd, iOp, nArg, pColl, pFunc, pOp, pParse, pWin, regArg, regTmp, v, v2, v5 pParse = (*TWindowCodeArg)(unsafe.Pointer(p)).FpParse v = _sqlite3GetVdbe(tls, pParse) pWin = pMWin for { if !(pWin != 0) { break } pFunc = (*TWindow)(unsafe.Pointer(pWin)).FpWFunc if (*TWindow)(unsafe.Pointer(pWin)).FbExprArgs != 0 { v2 = 0 } else { v2 = _windowArgCount(tls, pWin) } nArg = v2 _ = libc.Int32FromInt32(0) /* All OVER clauses in the same window function aggregate step must ** be the same. */ _ = libc.Int32FromInt32(0) i = 0 for { if !(i < nArg) { break } if i != int32(1) || (*TFuncDef)(unsafe.Pointer(pFunc)).FzName != uintptr(unsafe.Pointer(&_nth_valueName)) { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), csr, (*TWindow)(unsafe.Pointer(pWin)).FiArgCol+i, reg+i) } else { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr, (*TWindow)(unsafe.Pointer(pWin)).FiArgCol+i, reg+i) } goto _3 _3: ; i++ } regArg = reg if (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid == 0 && (*TFuncDef)(unsafe.Pointer(pFunc)).FfuncFlags&uint32(SQLITE_FUNC_MINMAX) != 0 && int32((*TWindow)(unsafe.Pointer(pWin)).FeStart) != int32(TK_UNBOUNDED) { addrIsNull = _sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), regArg) if bInverse == 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), (*TWindow)(unsafe.Pointer(pWin)).FregApp+int32(1), int32(1)) _sqlite3VdbeAddOp2(tls, v, int32(OP_SCopy), regArg, (*TWindow)(unsafe.Pointer(pWin)).FregApp) _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), (*TWindow)(unsafe.Pointer(pWin)).FregApp, int32(2), (*TWindow)(unsafe.Pointer(pWin)).FregApp+int32(2)) _sqlite3VdbeAddOp2(tls, v, int32(OP_IdxInsert), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp, (*TWindow)(unsafe.Pointer(pWin)).FregApp+int32(2)) } else { _sqlite3VdbeAddOp4Int(tls, v, int32(OP_SeekGE), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp, 0, regArg, int32(1)) _sqlite3VdbeAddOp1(tls, v, int32(OP_Delete), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp) _sqlite3VdbeJumpHere(tls, v, _sqlite3VdbeCurrentAddr(tls, v)-int32(2)) } _sqlite3VdbeJumpHere(tls, v, addrIsNull) } else { if (*TWindow)(unsafe.Pointer(pWin)).FregApp != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), (*TWindow)(unsafe.Pointer(pWin)).FregApp+int32(1)-bInverse, int32(1)) } else { if (*TFuncDef)(unsafe.Pointer(pFunc)).FxSFunc != __ccgo_fp(_noopStepFunc) { addrIf = 0 if (*TWindow)(unsafe.Pointer(pWin)).FpFilter != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) regTmp = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), csr, (*TWindow)(unsafe.Pointer(pWin)).FiArgCol+nArg, regTmp) addrIf = _sqlite3VdbeAddOp3(tls, v, int32(OP_IfNot), regTmp, 0, int32(1)) _sqlite3ReleaseTempReg(tls, pParse, regTmp) } if (*TWindow)(unsafe.Pointer(pWin)).FbExprArgs != 0 { iOp = _sqlite3VdbeCurrentAddr(tls, v) _ = libc.Int32FromInt32(0) nArg = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 20)))).FnExpr regArg = _sqlite3GetTempRange(tls, pParse, nArg) _sqlite3ExprCodeExprList(tls, pParse, *(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 20)), regArg, 0, uint8(0)) iEnd = _sqlite3VdbeCurrentAddr(tls, v) for { if !(iOp < iEnd) { break } pOp = _sqlite3VdbeGetOp(tls, v, iOp) if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Column) && (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 == (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr { (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = csr } goto _4 _4: ; iOp++ } } if (*TFuncDef)(unsafe.Pointer(pFunc)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pColl = _sqlite3ExprNNCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 20)) + 8))).FpExpr) _sqlite3VdbeAddOp4(tls, v, int32(OP_CollSeq), 0, 0, 0, pColl, -int32(2)) } if bInverse != 0 { v5 = int32(OP_AggInverse) } else { v5 = int32(OP_AggStep) } _sqlite3VdbeAddOp3(tls, v, v5, bInverse, regArg, (*TWindow)(unsafe.Pointer(pWin)).FregAccum) _sqlite3VdbeAppendP4(tls, v, pFunc, -int32(7)) _sqlite3VdbeChangeP5(tls, v, uint16(uint8(nArg))) if (*TWindow)(unsafe.Pointer(pWin)).FbExprArgs != 0 { _sqlite3ReleaseTempRange(tls, pParse, regArg, nArg) } if addrIf != 0 { _sqlite3VdbeJumpHere(tls, v, addrIf) } } } } goto _1 _1: ; pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin } } /* ** Values that may be passed as the second argument to windowCodeOp(). */ // C documentation // // /* // ** Generate VM code to invoke either xValue() (bFin==0) or xFinalize() // ** (bFin==1) for each window function in the linked list starting at // ** pMWin. Or, for built-in window-functions that do not use the standard // ** API, generate the equivalent VM code. // */ func _windowAggFinal(tls *libc.TLS, p uintptr, bFin int32) { var nArg int32 var pMWin, pParse, pWin, v uintptr _, _, _, _, _ = nArg, pMWin, pParse, pWin, v pParse = (*TWindowCodeArg)(unsafe.Pointer(p)).FpParse pMWin = (*TWindowCodeArg)(unsafe.Pointer(p)).FpMWin v = _sqlite3GetVdbe(tls, pParse) pWin = pMWin for { if !(pWin != 0) { break } if (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid == 0 && (*TFuncDef)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpWFunc)).FfuncFlags&uint32(SQLITE_FUNC_MINMAX) != 0 && int32((*TWindow)(unsafe.Pointer(pWin)).FeStart) != int32(TK_UNBOUNDED) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, (*TWindow)(unsafe.Pointer(pWin)).FregResult) _sqlite3VdbeAddOp1(tls, v, int32(OP_Last), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp) _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp, 0, (*TWindow)(unsafe.Pointer(pWin)).FregResult) _sqlite3VdbeJumpHere(tls, v, _sqlite3VdbeCurrentAddr(tls, v)-int32(2)) } else { if (*TWindow)(unsafe.Pointer(pWin)).FregApp != 0 { _ = libc.Int32FromInt32(0) } else { nArg = _windowArgCount(tls, pWin) if bFin != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_AggFinal), (*TWindow)(unsafe.Pointer(pWin)).FregAccum, nArg) _sqlite3VdbeAppendP4(tls, v, (*TWindow)(unsafe.Pointer(pWin)).FpWFunc, -int32(7)) _sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), (*TWindow)(unsafe.Pointer(pWin)).FregAccum, (*TWindow)(unsafe.Pointer(pWin)).FregResult) _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, (*TWindow)(unsafe.Pointer(pWin)).FregAccum) } else { _sqlite3VdbeAddOp3(tls, v, int32(OP_AggValue), (*TWindow)(unsafe.Pointer(pWin)).FregAccum, nArg, (*TWindow)(unsafe.Pointer(pWin)).FregResult) _sqlite3VdbeAppendP4(tls, v, (*TWindow)(unsafe.Pointer(pWin)).FpWFunc, -int32(7)) } } } goto _1 _1: ; pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin } } // C documentation // // /* // ** Generate code to calculate the current values of all window functions in the // ** p->pMWin list by doing a full scan of the current window frame. Store the // ** results in the Window.regResult registers, ready to return the upper // ** layer. // */ func _windowFullScan(tls *libc.TLS, p uintptr) { var addr, addrEq, addrNext, csr, lblBrk, lblNext, nPeer, regCPeer, regCRowid, regPeer, regRowid, v1 int32 var pKeyInfo, pMWin, pParse, pWin, v uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, addrEq, addrNext, csr, lblBrk, lblNext, nPeer, pKeyInfo, pMWin, pParse, pWin, regCPeer, regCRowid, regPeer, regRowid, v, v1 pParse = (*TWindowCodeArg)(unsafe.Pointer(p)).FpParse pMWin = (*TWindowCodeArg)(unsafe.Pointer(p)).FpMWin v = (*TWindowCodeArg)(unsafe.Pointer(p)).FpVdbe regCRowid = 0 /* Current rowid value */ regCPeer = 0 /* Current peer values */ regRowid = 0 /* AggStep rowid value */ regPeer = 0 _ = libc.Int32FromInt32(0) csr = (*TWindow)(unsafe.Pointer(pMWin)).FcsrApp if (*TWindow)(unsafe.Pointer(pMWin)).FpOrderBy != 0 { v1 = (*TExprList)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pMWin)).FpOrderBy)).FnExpr } else { v1 = 0 } nPeer = v1 lblNext = _sqlite3VdbeMakeLabel(tls, pParse) lblBrk = _sqlite3VdbeMakeLabel(tls, pParse) regCRowid = _sqlite3GetTempReg(tls, pParse) regRowid = _sqlite3GetTempReg(tls, pParse) if nPeer != 0 { regCPeer = _sqlite3GetTempRange(tls, pParse, nPeer) regPeer = _sqlite3GetTempRange(tls, pParse, nPeer) } _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr, regCRowid) _windowReadPeerValues(tls, p, (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr, regCPeer) pWin = pMWin for { if !(pWin != 0) { break } _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, (*TWindow)(unsafe.Pointer(pWin)).FregAccum) goto _2 _2: ; pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin } _sqlite3VdbeAddOp3(tls, v, int32(OP_SeekGE), csr, lblBrk, (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid) addrNext = _sqlite3VdbeCurrentAddr(tls, v) _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), csr, regRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_Gt), (*TWindow)(unsafe.Pointer(pMWin)).FregEndRowid, lblBrk, regRowid) if int32((*TWindow)(unsafe.Pointer(pMWin)).FeExclude) == int32(TK_CURRENT) { _sqlite3VdbeAddOp3(tls, v, int32(OP_Eq), regCRowid, lblNext, regRowid) } else { if int32((*TWindow)(unsafe.Pointer(pMWin)).FeExclude) != int32(TK_NO) { addrEq = 0 pKeyInfo = uintptr(0) if (*TWindow)(unsafe.Pointer(pMWin)).FpOrderBy != 0 { pKeyInfo = _sqlite3KeyInfoFromExprList(tls, pParse, (*TWindow)(unsafe.Pointer(pMWin)).FpOrderBy, 0, 0) } if int32((*TWindow)(unsafe.Pointer(pMWin)).FeExclude) == int32(TK_TIES) { addrEq = _sqlite3VdbeAddOp3(tls, v, int32(OP_Eq), regCRowid, 0, regRowid) } if pKeyInfo != 0 { _windowReadPeerValues(tls, p, csr, regPeer) _sqlite3VdbeAddOp3(tls, v, int32(OP_Compare), regPeer, regCPeer, nPeer) _sqlite3VdbeAppendP4(tls, v, pKeyInfo, -int32(8)) addr = _sqlite3VdbeCurrentAddr(tls, v) + int32(1) _sqlite3VdbeAddOp3(tls, v, int32(OP_Jump), addr, lblNext, addr) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, lblNext) } if addrEq != 0 { _sqlite3VdbeJumpHere(tls, v, addrEq) } } } _windowAggStep(tls, p, pMWin, csr, 0, (*TWindowCodeArg)(unsafe.Pointer(p)).FregArg) _sqlite3VdbeResolveLabel(tls, v, lblNext) _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), csr, addrNext) _sqlite3VdbeJumpHere(tls, v, addrNext-int32(1)) _sqlite3VdbeJumpHere(tls, v, addrNext+int32(1)) _sqlite3ReleaseTempReg(tls, pParse, regRowid) _sqlite3ReleaseTempReg(tls, pParse, regCRowid) if nPeer != 0 { _sqlite3ReleaseTempRange(tls, pParse, regPeer, nPeer) _sqlite3ReleaseTempRange(tls, pParse, regCPeer, nPeer) } _windowAggFinal(tls, p, int32(1)) } // C documentation // // /* // ** Invoke the sub-routine at regGosub (generated by code in select.c) to // ** return the current row of Window.iEphCsr. If all window functions are // ** aggregate window functions that use the standard API, a single // ** OP_Gosub instruction is all that this routine generates. Extra VM code // ** for per-row processing is only generated for the following built-in window // ** functions: // ** // ** nth_value() // ** first_value() // ** lag() // ** lead() // */ func _windowReturnOneRow(tls *libc.TLS, p uintptr) { var csr, csr1, iEph, lbl, lbl1, nArg, op, tmpReg, tmpReg1, tmpReg2, val, v2, v3 int32 var pFunc, pMWin, pParse, pWin, v uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = csr, csr1, iEph, lbl, lbl1, nArg, op, pFunc, pMWin, pParse, pWin, tmpReg, tmpReg1, tmpReg2, v, val, v2, v3 pMWin = (*TWindowCodeArg)(unsafe.Pointer(p)).FpMWin v = (*TWindowCodeArg)(unsafe.Pointer(p)).FpVdbe if (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid != 0 { _windowFullScan(tls, p) } else { pParse = (*TWindowCodeArg)(unsafe.Pointer(p)).FpParse pWin = pMWin for { if !(pWin != 0) { break } pFunc = (*TWindow)(unsafe.Pointer(pWin)).FpWFunc _ = libc.Int32FromInt32(0) if (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == uintptr(unsafe.Pointer(&_nth_valueName)) || (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == uintptr(unsafe.Pointer(&_first_valueName)) { csr = (*TWindow)(unsafe.Pointer(pWin)).FcsrApp lbl = _sqlite3VdbeMakeLabel(tls, pParse) tmpReg = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, (*TWindow)(unsafe.Pointer(pWin)).FregResult) if (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == uintptr(unsafe.Pointer(&_nth_valueName)) { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr, (*TWindow)(unsafe.Pointer(pWin)).FiArgCol+int32(1), tmpReg) _windowCheckValue(tls, pParse, tmpReg, int32(2)) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), tmpReg) } _sqlite3VdbeAddOp3(tls, v, int32(OP_Add), tmpReg, (*TWindow)(unsafe.Pointer(pWin)).FregApp, tmpReg) _sqlite3VdbeAddOp3(tls, v, int32(OP_Gt), (*TWindow)(unsafe.Pointer(pWin)).FregApp+int32(1), lbl, tmpReg) _sqlite3VdbeAddOp3(tls, v, int32(OP_SeekRowid), csr, 0, tmpReg) _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), csr, (*TWindow)(unsafe.Pointer(pWin)).FiArgCol, (*TWindow)(unsafe.Pointer(pWin)).FregResult) _sqlite3VdbeResolveLabel(tls, v, lbl) _sqlite3ReleaseTempReg(tls, pParse, tmpReg) } else { if (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == uintptr(unsafe.Pointer(&_leadName)) || (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == uintptr(unsafe.Pointer(&_lagName)) { nArg = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 20)))).FnExpr csr1 = (*TWindow)(unsafe.Pointer(pWin)).FcsrApp lbl1 = _sqlite3VdbeMakeLabel(tls, pParse) tmpReg1 = _sqlite3GetTempReg(tls, pParse) iEph = (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr if nArg < int32(3) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, (*TWindow)(unsafe.Pointer(pWin)).FregResult) } else { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iEph, (*TWindow)(unsafe.Pointer(pWin)).FiArgCol+int32(2), (*TWindow)(unsafe.Pointer(pWin)).FregResult) } _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iEph, tmpReg1) if nArg < int32(2) { if (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == uintptr(unsafe.Pointer(&_leadName)) { v2 = int32(1) } else { v2 = -int32(1) } val = v2 _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), tmpReg1, val) } else { if (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == uintptr(unsafe.Pointer(&_leadName)) { v3 = int32(OP_Add) } else { v3 = int32(OP_Subtract) } op = v3 tmpReg2 = _sqlite3GetTempReg(tls, pParse) _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iEph, (*TWindow)(unsafe.Pointer(pWin)).FiArgCol+int32(1), tmpReg2) _sqlite3VdbeAddOp3(tls, v, op, tmpReg2, tmpReg1, tmpReg1) _sqlite3ReleaseTempReg(tls, pParse, tmpReg2) } _sqlite3VdbeAddOp3(tls, v, int32(OP_SeekRowid), csr1, lbl1, tmpReg1) _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), csr1, (*TWindow)(unsafe.Pointer(pWin)).FiArgCol, (*TWindow)(unsafe.Pointer(pWin)).FregResult) _sqlite3VdbeResolveLabel(tls, v, lbl1) _sqlite3ReleaseTempReg(tls, pParse, tmpReg1) } } goto _1 _1: ; pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin } } _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TWindowCodeArg)(unsafe.Pointer(p)).FregGosub, (*TWindowCodeArg)(unsafe.Pointer(p)).FaddrGosub) } // C documentation // // /* // ** Generate code to set the accumulator register for each window function // ** in the linked list passed as the second argument to NULL. And perform // ** any equivalent initialization required by any built-in window functions // ** in the list. // */ func _windowInitAccum(tls *libc.TLS, pParse uintptr, pMWin uintptr) (r int32) { var nArg, regArg, v2 int32 var pFunc, pWin, v uintptr _, _, _, _, _, _ = nArg, pFunc, pWin, regArg, v, v2 v = _sqlite3GetVdbe(tls, pParse) nArg = 0 pWin = pMWin for { if !(pWin != 0) { break } pFunc = (*TWindow)(unsafe.Pointer(pWin)).FpWFunc _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, (*TWindow)(unsafe.Pointer(pWin)).FregAccum) if nArg > _windowArgCount(tls, pWin) { v2 = nArg } else { v2 = _windowArgCount(tls, pWin) } nArg = v2 if (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid == 0 { if (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == uintptr(unsafe.Pointer(&_nth_valueName)) || (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == uintptr(unsafe.Pointer(&_first_valueName)) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (*TWindow)(unsafe.Pointer(pWin)).FregApp) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (*TWindow)(unsafe.Pointer(pWin)).FregApp+int32(1)) } if (*TFuncDef)(unsafe.Pointer(pFunc)).FfuncFlags&uint32(SQLITE_FUNC_MINMAX) != 0 && (*TWindow)(unsafe.Pointer(pWin)).FcsrApp != 0 { _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp1(tls, v, int32(OP_ResetSorter), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (*TWindow)(unsafe.Pointer(pWin)).FregApp+int32(1)) } } goto _1 _1: ; pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin } regArg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nArg return regArg } // C documentation // // /* // ** Return true if the current frame should be cached in the ephemeral table, // ** even if there are no xInverse() calls required. // */ func _windowCacheFrame(tls *libc.TLS, pMWin uintptr) (r int32) { var pFunc, pWin uintptr _, _ = pFunc, pWin if (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid != 0 { return int32(1) } pWin = pMWin for { if !(pWin != 0) { break } pFunc = (*TWindow)(unsafe.Pointer(pWin)).FpWFunc if (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == uintptr(unsafe.Pointer(&_nth_valueName)) || (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == uintptr(unsafe.Pointer(&_first_valueName)) || (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == uintptr(unsafe.Pointer(&_leadName)) || (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == uintptr(unsafe.Pointer(&_lagName)) { return int32(1) } goto _1 _1: ; pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin } return 0 } // C documentation // // /* // ** regOld and regNew are each the first register in an array of size // ** pOrderBy->nExpr. This function generates code to compare the two // ** arrays of registers using the collation sequences and other comparison // ** parameters specified by pOrderBy. // ** // ** If the two arrays are not equal, the contents of regNew is copied to // ** regOld and control falls through. Otherwise, if the contents of the arrays // ** are equal, an OP_Goto is executed. The address of the OP_Goto is returned. // */ func _windowIfNewPeer(tls *libc.TLS, pParse uintptr, pOrderBy uintptr, regNew int32, regOld int32, addr int32) { var nVal int32 var pKeyInfo, v uintptr _, _, _ = nVal, pKeyInfo, v v = _sqlite3GetVdbe(tls, pParse) if pOrderBy != 0 { nVal = (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr pKeyInfo = _sqlite3KeyInfoFromExprList(tls, pParse, pOrderBy, 0, 0) _sqlite3VdbeAddOp3(tls, v, int32(OP_Compare), regOld, regNew, nVal) _sqlite3VdbeAppendP4(tls, v, pKeyInfo, -int32(8)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Jump), _sqlite3VdbeCurrentAddr(tls, v)+int32(1), addr, _sqlite3VdbeCurrentAddr(tls, v)+int32(1)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regNew, regOld, nVal-int32(1)) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addr) } } // C documentation // // /* // ** This function is called as part of generating VM programs for RANGE // ** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for // ** the ORDER BY term in the window, and that argument op is OP_Ge, it generates // ** code equivalent to: // ** // ** if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl; // ** // ** The value of parameter op may also be OP_Gt or OP_Le. In these cases the // ** operator in the above pseudo-code is replaced with ">" or "<=", respectively. // ** // ** If the sort-order for the ORDER BY term in the window is DESC, then the // ** comparison is reversed. Instead of adding regVal to csr1.peerVal, it is // ** subtracted. And the comparison operator is inverted to - ">=" becomes "<=", // ** ">" becomes "<", and so on. So, with DESC sort order, if the argument op // ** is OP_Ge, the generated code is equivalent to: // ** // ** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl; // ** // ** A special type of arithmetic is used such that if csr1.peerVal is not // ** a numeric type (real or integer), then the result of the addition // ** or subtraction is a a copy of csr1.peerVal. // */ func _windowCodeRangeTest(tls *libc.TLS, p uintptr, op int32, csr1 int32, regVal int32, csr2 int32, lbl int32) { var addr, addrDone, addrGe, arith, reg1, reg2, regString, v1, v3 int32 var pColl, pOrderBy, pParse, v, v2 uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, addrDone, addrGe, arith, pColl, pOrderBy, pParse, reg1, reg2, regString, v, v1, v2, v3 pParse = (*TWindowCodeArg)(unsafe.Pointer(p)).FpParse v = _sqlite3GetVdbe(tls, pParse) pOrderBy = (*TWindow)(unsafe.Pointer((*TWindowCodeArg)(unsafe.Pointer(p)).FpMWin)).FpOrderBy /* ORDER BY clause for window */ reg1 = _sqlite3GetTempReg(tls, pParse) /* Reg. for csr1.peerVal+regVal */ reg2 = _sqlite3GetTempReg(tls, pParse) v2 = pParse + 44 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) /* Reg. for csr2.peerVal */ regString = v1 /* Reg. for constant value '' */ arith = int32(OP_Add) /* Jump destination */ addrDone = _sqlite3VdbeMakeLabel(tls, pParse) /* Read the peer-value from each cursor into a register */ _windowReadPeerValues(tls, p, csr1, reg1) _windowReadPeerValues(tls, p, csr2, reg2) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_DESC) != 0 { switch op { case int32(OP_Ge): op = int32(OP_Le) case int32(OP_Gt): op = int32(OP_Lt) default: _ = libc.Int32FromInt32(0) op = int32(OP_Ge) break } arith = int32(OP_Subtract) } /* If the BIGNULL flag is set for the ORDER BY, then it is required to ** consider NULL values to be larger than all other values, instead of ** the usual smaller. The VDBE opcodes OP_Ge and so on do not handle this ** (and adding that capability causes a performance regression), so ** instead if the BIGNULL flag is set then cases where either reg1 or ** reg2 are NULL are handled separately in the following block. The code ** generated is equivalent to: ** ** if( reg1 IS NULL ){ ** if( op==OP_Ge ) goto lbl; ** if( op==OP_Gt && reg2 IS NOT NULL ) goto lbl; ** if( op==OP_Le && reg2 IS NULL ) goto lbl; ** }else if( reg2 IS NULL ){ ** if( op==OP_Le ) goto lbl; ** } ** ** Additionally, if either reg1 or reg2 are NULL but the jump to lbl is ** not taken, control jumps over the comparison operator coded below this ** block. */ if int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_BIGNULL) != 0 { /* This block runs if reg1 contains a NULL. */ addr = _sqlite3VdbeAddOp1(tls, v, int32(OP_NotNull), reg1) switch op { case int32(OP_Ge): _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, lbl) case int32(OP_Gt): _sqlite3VdbeAddOp2(tls, v, int32(OP_NotNull), reg2, lbl) case int32(OP_Le): _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), reg2, lbl) default: _ = libc.Int32FromInt32(0) /* no-op */ break } _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addrDone) /* This block runs if reg1 is not NULL, but reg2 is. */ _sqlite3VdbeJumpHere(tls, v, addr) if op == int32(OP_Gt) || op == int32(OP_Ge) { v3 = addrDone } else { v3 = lbl } _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), reg2, v3) } /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1). ** This block adds (or subtracts for DESC) the numeric value in regVal ** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob), ** then leave reg1 as it is. In pseudo-code, this is implemented as: ** ** if( reg1>='' ) goto addrGe; ** reg1 = reg1 +/- regVal ** addrGe: ** ** Since all strings and blobs are greater-than-or-equal-to an empty string, ** the add/subtract is skipped for these, as required. If reg1 is a NULL, ** then the arithmetic is performed, but since adding or subtracting from ** NULL is always NULL anyway, this case is handled as required too. */ _sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, regString, 0, __ccgo_ts+1648, -int32(1)) addrGe = _sqlite3VdbeAddOp3(tls, v, int32(OP_Ge), regString, 0, reg1) if op == int32(OP_Ge) && arith == int32(OP_Add) || op == int32(OP_Le) && arith == int32(OP_Subtract) { _sqlite3VdbeAddOp3(tls, v, op, reg2, lbl, reg1) } _sqlite3VdbeAddOp3(tls, v, arith, regVal, reg1, reg1) _sqlite3VdbeJumpHere(tls, v, addrGe) /* Compare registers reg2 and reg1, taking the jump if required. Note that ** control skips over this test if the BIGNULL flag is set and either ** reg1 or reg2 contain a NULL value. */ _sqlite3VdbeAddOp3(tls, v, op, reg2, lbl, reg1) pColl = _sqlite3ExprNNCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8))).FpExpr) _sqlite3VdbeAppendP4(tls, v, pColl, -int32(2)) _sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_NULLEQ)) _sqlite3VdbeResolveLabel(tls, v, addrDone) _ = libc.Int32FromInt32(0) _sqlite3ReleaseTempReg(tls, pParse, reg1) _sqlite3ReleaseTempReg(tls, pParse, reg2) } // C documentation // // /* // ** Helper function for sqlite3WindowCodeStep(). Each call to this function // ** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE // ** operation. Refer to the header comment for sqlite3WindowCodeStep() for // ** details. // */ func _windowCodeOp(tls *libc.TLS, p uintptr, op int32, regCountdown int32, jumpOnEof int32) (r int32) { var addrContinue, addrNextRange, bPeer, csr, lblDone, nReg, reg, regRowid1, regRowid2, regTmp, ret, v1, v2 int32 var pMWin, pParse, v uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrContinue, addrNextRange, bPeer, csr, lblDone, nReg, pMWin, pParse, reg, regRowid1, regRowid2, regTmp, ret, v, v1, v2 pParse = (*TWindowCodeArg)(unsafe.Pointer(p)).FpParse pMWin = (*TWindowCodeArg)(unsafe.Pointer(p)).FpMWin ret = 0 v = (*TWindowCodeArg)(unsafe.Pointer(p)).FpVdbe addrContinue = 0 bPeer = libc.BoolInt32(int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) != int32(TK_ROWS)) lblDone = _sqlite3VdbeMakeLabel(tls, pParse) addrNextRange = 0 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame ** starts with UNBOUNDED PRECEDING. */ if op == int32(WINDOW_AGGINVERSE) && int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_UNBOUNDED) { _ = libc.Int32FromInt32(0) return 0 } if regCountdown > 0 { if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE) { addrNextRange = _sqlite3VdbeCurrentAddr(tls, v) _ = libc.Int32FromInt32(0) if op == int32(WINDOW_AGGINVERSE) { if int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_FOLLOWING) { _windowCodeRangeTest(tls, p, int32(OP_Le), (*TWindowCodeArg)(unsafe.Pointer(p)).Fcurrent.Fcsr, regCountdown, (*TWindowCodeArg)(unsafe.Pointer(p)).Fstart.Fcsr, lblDone) } else { _windowCodeRangeTest(tls, p, int32(OP_Ge), (*TWindowCodeArg)(unsafe.Pointer(p)).Fstart.Fcsr, regCountdown, (*TWindowCodeArg)(unsafe.Pointer(p)).Fcurrent.Fcsr, lblDone) } } else { _windowCodeRangeTest(tls, p, int32(OP_Gt), (*TWindowCodeArg)(unsafe.Pointer(p)).Fend.Fcsr, regCountdown, (*TWindowCodeArg)(unsafe.Pointer(p)).Fcurrent.Fcsr, lblDone) } } else { _sqlite3VdbeAddOp3(tls, v, int32(OP_IfPos), regCountdown, lblDone, int32(1)) } } if op == int32(WINDOW_RETURN_ROW) && (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid == 0 { _windowAggFinal(tls, p, 0) } addrContinue = _sqlite3VdbeCurrentAddr(tls, v) /* If this is a (RANGE BETWEEN a FOLLOWING AND b FOLLOWING) or ** (RANGE BETWEEN b PRECEDING AND a PRECEDING) frame, ensure the ** start cursor does not advance past the end cursor within the ** temporary table. It otherwise might, if (a>b). Also ensure that, ** if the input cursor is still finding new rows, that the end ** cursor does not go past it to EOF. */ if int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) && regCountdown != 0 && int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE) { regRowid1 = _sqlite3GetTempReg(tls, pParse) regRowid2 = _sqlite3GetTempReg(tls, pParse) if op == int32(WINDOW_AGGINVERSE) { _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), (*TWindowCodeArg)(unsafe.Pointer(p)).Fstart.Fcsr, regRowid1) _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), (*TWindowCodeArg)(unsafe.Pointer(p)).Fend.Fcsr, regRowid2) _sqlite3VdbeAddOp3(tls, v, int32(OP_Ge), regRowid2, lblDone, regRowid1) } else { if (*TWindowCodeArg)(unsafe.Pointer(p)).FregRowid != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), (*TWindowCodeArg)(unsafe.Pointer(p)).Fend.Fcsr, regRowid1) _sqlite3VdbeAddOp3(tls, v, int32(OP_Ge), (*TWindowCodeArg)(unsafe.Pointer(p)).FregRowid, lblDone, regRowid1) } } _sqlite3ReleaseTempReg(tls, pParse, regRowid1) _sqlite3ReleaseTempReg(tls, pParse, regRowid2) _ = libc.Int32FromInt32(0) } switch op { case int32(WINDOW_RETURN_ROW): csr = (*TWindowCodeArg)(unsafe.Pointer(p)).Fcurrent.Fcsr reg = (*TWindowCodeArg)(unsafe.Pointer(p)).Fcurrent.Freg _windowReturnOneRow(tls, p) case int32(WINDOW_AGGINVERSE): csr = (*TWindowCodeArg)(unsafe.Pointer(p)).Fstart.Fcsr reg = (*TWindowCodeArg)(unsafe.Pointer(p)).Fstart.Freg if (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid != 0 { _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid, int32(1)) } else { _windowAggStep(tls, p, pMWin, csr, int32(1), (*TWindowCodeArg)(unsafe.Pointer(p)).FregArg) } default: _ = libc.Int32FromInt32(0) csr = (*TWindowCodeArg)(unsafe.Pointer(p)).Fend.Fcsr reg = (*TWindowCodeArg)(unsafe.Pointer(p)).Fend.Freg if (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid != 0 { _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), (*TWindow)(unsafe.Pointer(pMWin)).FregEndRowid, int32(1)) } else { _windowAggStep(tls, p, pMWin, csr, 0, (*TWindowCodeArg)(unsafe.Pointer(p)).FregArg) } break } if op == (*TWindowCodeArg)(unsafe.Pointer(p)).FeDelete { _sqlite3VdbeAddOp1(tls, v, int32(OP_Delete), csr) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_SAVEPOSITION)) } if jumpOnEof != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), csr, _sqlite3VdbeCurrentAddr(tls, v)+int32(2)) ret = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto)) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), csr, _sqlite3VdbeCurrentAddr(tls, v)+int32(1)+bPeer) if bPeer != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, lblDone) } } if bPeer != 0 { if (*TWindow)(unsafe.Pointer(pMWin)).FpOrderBy != 0 { v1 = (*TExprList)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pMWin)).FpOrderBy)).FnExpr } else { v1 = 0 } nReg = v1 if nReg != 0 { v2 = _sqlite3GetTempRange(tls, pParse, nReg) } else { v2 = 0 } regTmp = v2 _windowReadPeerValues(tls, p, csr, regTmp) _windowIfNewPeer(tls, pParse, (*TWindow)(unsafe.Pointer(pMWin)).FpOrderBy, regTmp, reg, addrContinue) _sqlite3ReleaseTempRange(tls, pParse, regTmp, nReg) } if addrNextRange != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addrNextRange) } _sqlite3VdbeResolveLabel(tls, v, lblDone) return ret } // C documentation // // /* // ** Allocate and return a duplicate of the Window object indicated by the // ** third argument. Set the Window.pOwner field of the new object to // ** pOwner. // */ func _sqlite3WindowDup(tls *libc.TLS, db uintptr, pOwner uintptr, p uintptr) (r uintptr) { var pNew uintptr _ = pNew pNew = uintptr(0) if p != 0 { pNew = _sqlite3DbMallocZero(tls, db, uint64(100)) if pNew != 0 { (*TWindow)(unsafe.Pointer(pNew)).FzName = _sqlite3DbStrDup(tls, db, (*TWindow)(unsafe.Pointer(p)).FzName) (*TWindow)(unsafe.Pointer(pNew)).FzBase = _sqlite3DbStrDup(tls, db, (*TWindow)(unsafe.Pointer(p)).FzBase) (*TWindow)(unsafe.Pointer(pNew)).FpFilter = _sqlite3ExprDup(tls, db, (*TWindow)(unsafe.Pointer(p)).FpFilter, 0) (*TWindow)(unsafe.Pointer(pNew)).FpWFunc = (*TWindow)(unsafe.Pointer(p)).FpWFunc (*TWindow)(unsafe.Pointer(pNew)).FpPartition = _sqlite3ExprListDup(tls, db, (*TWindow)(unsafe.Pointer(p)).FpPartition, 0) (*TWindow)(unsafe.Pointer(pNew)).FpOrderBy = _sqlite3ExprListDup(tls, db, (*TWindow)(unsafe.Pointer(p)).FpOrderBy, 0) (*TWindow)(unsafe.Pointer(pNew)).FeFrmType = (*TWindow)(unsafe.Pointer(p)).FeFrmType (*TWindow)(unsafe.Pointer(pNew)).FeEnd = (*TWindow)(unsafe.Pointer(p)).FeEnd (*TWindow)(unsafe.Pointer(pNew)).FeStart = (*TWindow)(unsafe.Pointer(p)).FeStart (*TWindow)(unsafe.Pointer(pNew)).FeExclude = (*TWindow)(unsafe.Pointer(p)).FeExclude (*TWindow)(unsafe.Pointer(pNew)).FregResult = (*TWindow)(unsafe.Pointer(p)).FregResult (*TWindow)(unsafe.Pointer(pNew)).FregAccum = (*TWindow)(unsafe.Pointer(p)).FregAccum (*TWindow)(unsafe.Pointer(pNew)).FiArgCol = (*TWindow)(unsafe.Pointer(p)).FiArgCol (*TWindow)(unsafe.Pointer(pNew)).FiEphCsr = (*TWindow)(unsafe.Pointer(p)).FiEphCsr (*TWindow)(unsafe.Pointer(pNew)).FbExprArgs = (*TWindow)(unsafe.Pointer(p)).FbExprArgs (*TWindow)(unsafe.Pointer(pNew)).FpStart = _sqlite3ExprDup(tls, db, (*TWindow)(unsafe.Pointer(p)).FpStart, 0) (*TWindow)(unsafe.Pointer(pNew)).FpEnd = _sqlite3ExprDup(tls, db, (*TWindow)(unsafe.Pointer(p)).FpEnd, 0) (*TWindow)(unsafe.Pointer(pNew)).FpOwner = pOwner (*TWindow)(unsafe.Pointer(pNew)).FbImplicitFrame = (*TWindow)(unsafe.Pointer(p)).FbImplicitFrame } } return pNew } // C documentation // // /* // ** Return a copy of the linked list of Window objects passed as the // ** second argument. // */ func _sqlite3WindowListDup(tls *libc.TLS, db uintptr, p uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var pWin, pp uintptr var _ /* pRet at bp+0 */ uintptr _, _ = pWin, pp *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) pp = bp pWin = p for { if !(pWin != 0) { break } *(*uintptr)(unsafe.Pointer(pp)) = _sqlite3WindowDup(tls, db, uintptr(0), pWin) if *(*uintptr)(unsafe.Pointer(pp)) == uintptr(0) { break } pp = *(*uintptr)(unsafe.Pointer(pp)) + 36 goto _1 _1: ; pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin } return *(*uintptr)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Return true if it can be determined at compile time that expression // ** pExpr evaluates to a value that, when cast to an integer, is greater // ** than zero. False otherwise. // ** // ** If an OOM error occurs, this function sets the Parse.db.mallocFailed // ** flag and returns zero. // */ func _windowExprGtZero(tls *libc.TLS, pParse uintptr, pExpr uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var db uintptr var ret int32 var _ /* pVal at bp+0 */ uintptr _, _ = db, ret ret = 0 db = (*TParse)(unsafe.Pointer(pParse)).Fdb *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) _sqlite3ValueFromExpr(tls, db, pExpr, (*Tsqlite3)(unsafe.Pointer(db)).Fenc, uint8(SQLITE_AFF_NUMERIC), bp) if *(*uintptr)(unsafe.Pointer(bp)) != 0 && Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(bp))) > 0 { ret = int32(1) } _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer(bp))) return ret } // C documentation // // /* // ** sqlite3WhereBegin() has already been called for the SELECT statement // ** passed as the second argument when this function is invoked. It generates // ** code to populate the Window.regResult register for each window function // ** and invoke the sub-routine at instruction addrGosub once for each row. // ** sqlite3WhereEnd() is always called before returning. // ** // ** This function handles several different types of window frames, which // ** require slightly different processing. The following pseudo code is // ** used to implement window frames of the form: // ** // ** ROWS BETWEEN PRECEDING AND FOLLOWING // ** // ** Other window frame types use variants of the following: // ** // ** ... loop started by sqlite3WhereBegin() ... // ** if( new partition ){ // ** Gosub flush // ** } // ** Insert new row into eph table. // ** // ** if( first row of partition ){ // ** // Rewind three cursors, all open on the eph table. // ** Rewind(csrEnd); // ** Rewind(csrStart); // ** Rewind(csrCurrent); // ** // ** regEnd = // FOLLOWING expression // ** regStart = // PRECEDING expression // ** }else{ // ** // First time this branch is taken, the eph table contains two // ** // rows. The first row in the partition, which all three cursors // ** // currently point to, and the following row. // ** AGGSTEP // ** if( (regEnd--)<=0 ){ // ** RETURN_ROW // ** if( (regStart--)<=0 ){ // ** AGGINVERSE // ** } // ** } // ** } // ** } // ** flush: // ** AGGSTEP // ** while( 1 ){ // ** RETURN ROW // ** if( csrCurrent is EOF ) break; // ** if( (regStart--)<=0 ){ // ** AggInverse(csrStart) // ** Next(csrStart) // ** } // ** } // ** // ** The pseudo-code above uses the following shorthand: // ** // ** AGGSTEP: invoke the aggregate xStep() function for each window function // ** with arguments read from the current row of cursor csrEnd, then // ** step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()). // ** // ** RETURN_ROW: return a row to the caller based on the contents of the // ** current row of csrCurrent and the current state of all // ** aggregates. Then step cursor csrCurrent forward one row. // ** // ** AGGINVERSE: invoke the aggregate xInverse() function for each window // ** functions with arguments read from the current row of cursor // ** csrStart. Then step csrStart forward one row. // ** // ** There are two other ROWS window frames that are handled significantly // ** differently from the above - "BETWEEN PRECEDING AND PRECEDING" // ** and "BETWEEN FOLLOWING AND FOLLOWING". These are special // ** cases because they change the order in which the three cursors (csrStart, // ** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that // ** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these // ** three. // ** // ** ROWS BETWEEN PRECEDING AND PRECEDING // ** // ** ... loop started by sqlite3WhereBegin() ... // ** if( new partition ){ // ** Gosub flush // ** } // ** Insert new row into eph table. // ** if( first row of partition ){ // ** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) // ** regEnd = // ** regStart = // ** }else{ // ** if( (regEnd--)<=0 ){ // ** AGGSTEP // ** } // ** RETURN_ROW // ** if( (regStart--)<=0 ){ // ** AGGINVERSE // ** } // ** } // ** } // ** flush: // ** if( (regEnd--)<=0 ){ // ** AGGSTEP // ** } // ** RETURN_ROW // ** // ** // ** ROWS BETWEEN FOLLOWING AND FOLLOWING // ** // ** ... loop started by sqlite3WhereBegin() ... // ** if( new partition ){ // ** Gosub flush // ** } // ** Insert new row into eph table. // ** if( first row of partition ){ // ** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) // ** regEnd = // ** regStart = regEnd - // ** }else{ // ** AGGSTEP // ** if( (regEnd--)<=0 ){ // ** RETURN_ROW // ** } // ** if( (regStart--)<=0 ){ // ** AGGINVERSE // ** } // ** } // ** } // ** flush: // ** AGGSTEP // ** while( 1 ){ // ** if( (regEnd--)<=0 ){ // ** RETURN_ROW // ** if( eof ) break; // ** } // ** if( (regStart--)<=0 ){ // ** AGGINVERSE // ** if( eof ) break // ** } // ** } // ** while( !eof csrCurrent ){ // ** RETURN_ROW // ** } // ** // ** For the most part, the patterns above are adapted to support UNBOUNDED by // ** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and // ** CURRENT ROW by assuming that it is equivalent to "0 PRECEDING/FOLLOWING". // ** This is optimized of course - branches that will never be taken and // ** conditions that are always true are omitted from the VM code. The only // ** exceptional case is: // ** // ** ROWS BETWEEN FOLLOWING AND UNBOUNDED FOLLOWING // ** // ** ... loop started by sqlite3WhereBegin() ... // ** if( new partition ){ // ** Gosub flush // ** } // ** Insert new row into eph table. // ** if( first row of partition ){ // ** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) // ** regStart = // ** }else{ // ** AGGSTEP // ** } // ** } // ** flush: // ** AGGSTEP // ** while( 1 ){ // ** if( (regStart--)<=0 ){ // ** AGGINVERSE // ** if( eof ) break // ** } // ** RETURN_ROW // ** } // ** while( !eof csrCurrent ){ // ** RETURN_ROW // ** } // ** // ** Also requiring special handling are the cases: // ** // ** ROWS BETWEEN PRECEDING AND PRECEDING // ** ROWS BETWEEN FOLLOWING AND FOLLOWING // ** // ** when (expr1 < expr2). This is detected at runtime, not by this function. // ** To handle this case, the pseudo-code programs depicted above are modified // ** slightly to be: // ** // ** ... loop started by sqlite3WhereBegin() ... // ** if( new partition ){ // ** Gosub flush // ** } // ** Insert new row into eph table. // ** if( first row of partition ){ // ** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) // ** regEnd = // ** regStart = // ** if( regEnd < regStart ){ // ** RETURN_ROW // ** delete eph table contents // ** continue // ** } // ** ... // ** // ** The new "continue" statement in the above jumps to the next iteration // ** of the outer loop - the one started by sqlite3WhereBegin(). // ** // ** The various GROUPS cases are implemented using the same patterns as // ** ROWS. The VM code is modified slightly so that: // ** // ** 1. The else branch in the main loop is only taken if the row just // ** added to the ephemeral table is the start of a new group. In // ** other words, it becomes: // ** // ** ... loop started by sqlite3WhereBegin() ... // ** if( new partition ){ // ** Gosub flush // ** } // ** Insert new row into eph table. // ** if( first row of partition ){ // ** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) // ** regEnd = // ** regStart = // ** }else if( new group ){ // ** ... // ** } // ** } // ** // ** 2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or // ** AGGINVERSE step processes the current row of the relevant cursor and // ** all subsequent rows belonging to the same group. // ** // ** RANGE window frames are a little different again. As for GROUPS, the // ** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE // ** deal in groups instead of rows. As for ROWS and GROUPS, there are three // ** basic cases: // ** // ** RANGE BETWEEN PRECEDING AND FOLLOWING // ** // ** ... loop started by sqlite3WhereBegin() ... // ** if( new partition ){ // ** Gosub flush // ** } // ** Insert new row into eph table. // ** if( first row of partition ){ // ** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) // ** regEnd = // ** regStart = // ** }else{ // ** AGGSTEP // ** while( (csrCurrent.key + regEnd) < csrEnd.key ){ // ** RETURN_ROW // ** while( csrStart.key + regStart) < csrCurrent.key ){ // ** AGGINVERSE // ** } // ** } // ** } // ** } // ** flush: // ** AGGSTEP // ** while( 1 ){ // ** RETURN ROW // ** if( csrCurrent is EOF ) break; // ** while( csrStart.key + regStart) < csrCurrent.key ){ // ** AGGINVERSE // ** } // ** } // ** } // ** // ** In the above notation, "csr.key" means the current value of the ORDER BY // ** expression (there is only ever 1 for a RANGE that uses an FOLLOWING // ** or PRECEDING AND PRECEDING // ** // ** ... loop started by sqlite3WhereBegin() ... // ** if( new partition ){ // ** Gosub flush // ** } // ** Insert new row into eph table. // ** if( first row of partition ){ // ** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) // ** regEnd = // ** regStart = // ** }else{ // ** while( (csrEnd.key + regEnd) <= csrCurrent.key ){ // ** AGGSTEP // ** } // ** while( (csrStart.key + regStart) < csrCurrent.key ){ // ** AGGINVERSE // ** } // ** RETURN_ROW // ** } // ** } // ** flush: // ** while( (csrEnd.key + regEnd) <= csrCurrent.key ){ // ** AGGSTEP // ** } // ** while( (csrStart.key + regStart) < csrCurrent.key ){ // ** AGGINVERSE // ** } // ** RETURN_ROW // ** // ** RANGE BETWEEN FOLLOWING AND FOLLOWING // ** // ** ... loop started by sqlite3WhereBegin() ... // ** if( new partition ){ // ** Gosub flush // ** } // ** Insert new row into eph table. // ** if( first row of partition ){ // ** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) // ** regEnd = // ** regStart = // ** }else{ // ** AGGSTEP // ** while( (csrCurrent.key + regEnd) < csrEnd.key ){ // ** while( (csrCurrent.key + regStart) > csrStart.key ){ // ** AGGINVERSE // ** } // ** RETURN_ROW // ** } // ** } // ** } // ** flush: // ** AGGSTEP // ** while( 1 ){ // ** while( (csrCurrent.key + regStart) > csrStart.key ){ // ** AGGINVERSE // ** if( eof ) break "while( 1 )" loop. // ** } // ** RETURN_ROW // ** } // ** while( !eof csrCurrent ){ // ** RETURN_ROW // ** } // ** // ** The text above leaves out many details. Refer to the code and comments // ** below for a more complete picture. // */ func _sqlite3WindowCodeStep(tls *libc.TLS, pParse uintptr, p uintptr, pWInfo uintptr, regGosub int32, addrGosub int32) { bp := tls.Alloc(64) defer tls.Free(64) var addr, addr1, addrBreak, addrBreak1, addrBreak2, addrBreak3, addrEmpty, addrGe, addrGosubFlush, addrInteger, addrNe, addrNext, addrStart, addrStart1, bRPS, bRPS1, csrInput, csrWrite, iInput, lbl, lbl1, lblWhereEnd, nInput, nPart, nPeer, op, regEnd, regFlushPart, regNew, regNewPart, regNewPeer, regPeer, regRecord, regStart, v1, v11, v13, v14, v15, v3, v5, v7, v9 int32 var pKeyInfo, pMWin, pOrderBy, pPart, v, v12, v2, v4, v6, v8 uintptr var _ /* s at bp+0 */ TWindowCodeArg _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, addr1, addrBreak, addrBreak1, addrBreak2, addrBreak3, addrEmpty, addrGe, addrGosubFlush, addrInteger, addrNe, addrNext, addrStart, addrStart1, bRPS, bRPS1, csrInput, csrWrite, iInput, lbl, lbl1, lblWhereEnd, nInput, nPart, nPeer, op, pKeyInfo, pMWin, pOrderBy, pPart, regEnd, regFlushPart, regNew, regNewPart, regNewPeer, regPeer, regRecord, regStart, v, v1, v11, v12, v13, v14, v15, v2, v3, v4, v5, v6, v7, v8, v9 pMWin = (*TSelect)(unsafe.Pointer(p)).FpWin pOrderBy = (*TWindow)(unsafe.Pointer(pMWin)).FpOrderBy v = _sqlite3GetVdbe(tls, pParse) /* Cursor used to write to eph. table */ csrInput = (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FiCursor /* Cursor of sub-select */ nInput = int32((*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpTab)).FnCol) /* Address of OP_Ne */ addrGosubFlush = 0 /* Address of OP_Gosub to flush: */ addrInteger = 0 /* regNew array in record form */ regNewPeer = 0 /* Peer values for new row (part of regNew) */ regPeer = 0 /* Peer values for current row */ regFlushPart = 0 /* Label just before sqlite3WhereEnd() code */ regStart = 0 /* Value of PRECEDING */ regEnd = 0 /* Value of FOLLOWING */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) lblWhereEnd = _sqlite3VdbeMakeLabel(tls, pParse) /* Fill in the context object */ libc.Xmemset(tls, bp, 0, uint32(56)) (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FpParse = pParse (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FpMWin = pMWin (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FpVdbe = v (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FregGosub = regGosub (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FaddrGosub = addrGosub (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fcurrent.Fcsr = (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr csrWrite = (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fcurrent.Fcsr + int32(1) (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fstart.Fcsr = (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fcurrent.Fcsr + int32(2) (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fend.Fcsr = (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fcurrent.Fcsr + int32(3) /* Figure out when rows may be deleted from the ephemeral table. There ** are four options - they may never be deleted (eDelete==0), they may ** be deleted as soon as they are no longer part of the window frame ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row ** has been returned to the caller (WINDOW_RETURN_ROW), or they may ** be deleted after they enter the frame (WINDOW_AGGSTEP). */ switch int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) { case int32(TK_FOLLOWING): if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) != int32(TK_RANGE) && _windowExprGtZero(tls, pParse, (*TWindow)(unsafe.Pointer(pMWin)).FpStart) != 0 { (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FeDelete = int32(WINDOW_RETURN_ROW) } case int32(TK_UNBOUNDED): if _windowCacheFrame(tls, pMWin) == 0 { if int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) == int32(TK_PRECEDING) { if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) != int32(TK_RANGE) && _windowExprGtZero(tls, pParse, (*TWindow)(unsafe.Pointer(pMWin)).FpEnd) != 0 { (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FeDelete = int32(WINDOW_AGGSTEP) } } else { (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FeDelete = int32(WINDOW_RETURN_ROW) } } default: (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FeDelete = int32(WINDOW_AGGINVERSE) break } /* Allocate registers for the array of values from the sub-query, the ** same values in record form, and the rowid used to insert said record ** into the ephemeral table. */ regNew = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nInput v2 = pParse + 44 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) regRecord = v1 v4 = pParse + 44 *(*int32)(unsafe.Pointer(v4))++ v3 = *(*int32)(unsafe.Pointer(v4)) (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FregRowid = v3 /* If the window frame contains an " PRECEDING" or " FOLLOWING" ** clause, allocate registers to store the results of evaluating each ** . */ if int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_PRECEDING) || int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_FOLLOWING) { v6 = pParse + 44 *(*int32)(unsafe.Pointer(v6))++ v5 = *(*int32)(unsafe.Pointer(v6)) regStart = v5 } if int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) == int32(TK_PRECEDING) || int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) == int32(TK_FOLLOWING) { v8 = pParse + 44 *(*int32)(unsafe.Pointer(v8))++ v7 = *(*int32)(unsafe.Pointer(v8)) regEnd = v7 } /* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of ** registers to store copies of the ORDER BY expressions (peer values) ** for the main loop, and for each cursor (start, current and end). */ if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) != int32(TK_ROWS) { if pOrderBy != 0 { v9 = (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr } else { v9 = 0 } nPeer = v9 regNewPeer = regNew + (*TWindow)(unsafe.Pointer(pMWin)).FnBufferCol if (*TWindow)(unsafe.Pointer(pMWin)).FpPartition != 0 { regNewPeer += (*TExprList)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pMWin)).FpPartition)).FnExpr } regPeer = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nPeer (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fstart.Freg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nPeer (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fcurrent.Freg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nPeer (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fend.Freg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 44)) += nPeer } /* Load the column values for the row returned by the sub-select ** into an array of registers starting at regNew. Assemble them into ** a record in register regRecord. */ iInput = 0 for { if !(iInput < nInput) { break } _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), csrInput, iInput, regNew+iInput) goto _10 _10: ; iInput++ } _sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regNew, nInput, regRecord) /* An input row has just been read into an array of registers starting ** at regNew. If the window has a PARTITION clause, this block generates ** VM code to check if the input row is the start of a new partition. ** If so, it does an OP_Gosub to an address to be filled in later. The ** address of the OP_Gosub is stored in local variable addrGosubFlush. */ if (*TWindow)(unsafe.Pointer(pMWin)).FpPartition != 0 { pPart = (*TWindow)(unsafe.Pointer(pMWin)).FpPartition nPart = (*TExprList)(unsafe.Pointer(pPart)).FnExpr regNewPart = regNew + (*TWindow)(unsafe.Pointer(pMWin)).FnBufferCol pKeyInfo = _sqlite3KeyInfoFromExprList(tls, pParse, pPart, 0, 0) v12 = pParse + 44 *(*int32)(unsafe.Pointer(v12))++ v11 = *(*int32)(unsafe.Pointer(v12)) regFlushPart = v11 addr = _sqlite3VdbeAddOp3(tls, v, int32(OP_Compare), regNewPart, (*TWindow)(unsafe.Pointer(pMWin)).FregPart, nPart) _sqlite3VdbeAppendP4(tls, v, pKeyInfo, -int32(8)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Jump), addr+int32(2), addr+int32(4), addr+int32(2)) addrGosubFlush = _sqlite3VdbeAddOp1(tls, v, int32(OP_Gosub), regFlushPart) _sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regNewPart, (*TWindow)(unsafe.Pointer(pMWin)).FregPart, nPart-int32(1)) } /* Insert the new row into the ephemeral table */ _sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), csrWrite, (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FregRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), csrWrite, regRecord, (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FregRowid) addrNe = _sqlite3VdbeAddOp3(tls, v, int32(OP_Ne), (*TWindow)(unsafe.Pointer(pMWin)).FregOne, 0, (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FregRowid) /* This block is run for the first row of each partition */ (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FregArg = _windowInitAccum(tls, pParse, pMWin) if regStart != 0 { _sqlite3ExprCode(tls, pParse, (*TWindow)(unsafe.Pointer(pMWin)).FpStart, regStart) if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE) { v13 = int32(3) } else { v13 = 0 } _windowCheckValue(tls, pParse, regStart, 0+v13) } if regEnd != 0 { _sqlite3ExprCode(tls, pParse, (*TWindow)(unsafe.Pointer(pMWin)).FpEnd, regEnd) if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE) { v14 = int32(3) } else { v14 = 0 } _windowCheckValue(tls, pParse, regEnd, int32(1)+v14) } if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) != int32(TK_RANGE) && int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) && regStart != 0 { if int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_FOLLOWING) { v15 = int32(OP_Ge) } else { v15 = int32(OP_Le) } op = v15 addrGe = _sqlite3VdbeAddOp3(tls, v, op, regStart, 0, regEnd) /* NeverNull because bound */ /* values previously checked */ _windowAggFinal(tls, bp, 0) _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fcurrent.Fcsr) _windowReturnOneRow(tls, bp) _sqlite3VdbeAddOp1(tls, v, int32(OP_ResetSorter), (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fcurrent.Fcsr) _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, lblWhereEnd) _sqlite3VdbeJumpHere(tls, v, addrGe) } if int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_FOLLOWING) && int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) != int32(TK_RANGE) && regEnd != 0 { _ = libc.Int32FromInt32(0) _sqlite3VdbeAddOp3(tls, v, int32(OP_Subtract), regStart, regEnd, regStart) } if int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) != int32(TK_UNBOUNDED) { _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fstart.Fcsr) } _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fcurrent.Fcsr) _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fend.Fcsr) if regPeer != 0 && pOrderBy != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regNewPeer, regPeer, (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr-int32(1)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regPeer, (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fstart.Freg, (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr-int32(1)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regPeer, (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fcurrent.Freg, (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr-int32(1)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regPeer, (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fend.Freg, (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr-int32(1)) } _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, lblWhereEnd) _sqlite3VdbeJumpHere(tls, v, addrNe) /* Beginning of the block executed for the second and subsequent rows. */ if regPeer != 0 { _windowIfNewPeer(tls, pParse, pOrderBy, regNewPeer, regPeer, lblWhereEnd) } if int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_FOLLOWING) { _windowCodeOp(tls, bp, int32(WINDOW_AGGSTEP), 0, 0) if int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) != int32(TK_UNBOUNDED) { if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE) { lbl = _sqlite3VdbeMakeLabel(tls, pParse) addrNext = _sqlite3VdbeCurrentAddr(tls, v) _windowCodeRangeTest(tls, bp, int32(OP_Ge), (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fcurrent.Fcsr, regEnd, (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fend.Fcsr, lbl) _windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0) _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, 0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addrNext) _sqlite3VdbeResolveLabel(tls, v, lbl) } else { _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), regEnd, 0) _windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0) } } } else { if int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) == int32(TK_PRECEDING) { bRPS = libc.BoolInt32(int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_PRECEDING) && int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE)) _windowCodeOp(tls, bp, int32(WINDOW_AGGSTEP), regEnd, 0) if bRPS != 0 { _windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0) } _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, 0) if !(bRPS != 0) { _windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0) } } else { addr1 = 0 _windowCodeOp(tls, bp, int32(WINDOW_AGGSTEP), 0, 0) if int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) != int32(TK_UNBOUNDED) { if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE) { lbl1 = 0 addr1 = _sqlite3VdbeCurrentAddr(tls, v) if regEnd != 0 { lbl1 = _sqlite3VdbeMakeLabel(tls, pParse) _windowCodeRangeTest(tls, bp, int32(OP_Ge), (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fcurrent.Fcsr, regEnd, (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fend.Fcsr, lbl1) } _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, 0) _windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0) if regEnd != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addr1) _sqlite3VdbeResolveLabel(tls, v, lbl1) } } else { if regEnd != 0 { addr1 = _sqlite3VdbeAddOp3(tls, v, int32(OP_IfPos), regEnd, 0, int32(1)) } _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, 0) _windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0) if regEnd != 0 { _sqlite3VdbeJumpHere(tls, v, addr1) } } } } } /* End of the main input loop */ _sqlite3VdbeResolveLabel(tls, v, lblWhereEnd) _sqlite3WhereEnd(tls, pWInfo) /* Fall through */ if (*TWindow)(unsafe.Pointer(pMWin)).FpPartition != 0 { addrInteger = _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regFlushPart) _sqlite3VdbeJumpHere(tls, v, addrGosubFlush) } (*(*TWindowCodeArg)(unsafe.Pointer(bp))).FregRowid = 0 addrEmpty = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), csrWrite) if int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) == int32(TK_PRECEDING) { bRPS1 = libc.BoolInt32(int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_PRECEDING) && int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE)) _windowCodeOp(tls, bp, int32(WINDOW_AGGSTEP), regEnd, 0) if bRPS1 != 0 { _windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0) } _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, 0) } else { if int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_FOLLOWING) { _windowCodeOp(tls, bp, int32(WINDOW_AGGSTEP), 0, 0) if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE) { addrStart = _sqlite3VdbeCurrentAddr(tls, v) addrBreak2 = _windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, int32(1)) addrBreak1 = _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, int32(1)) } else { if int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) == int32(TK_UNBOUNDED) { addrStart = _sqlite3VdbeCurrentAddr(tls, v) addrBreak1 = _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), regStart, int32(1)) addrBreak2 = _windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), 0, int32(1)) } else { _ = libc.Int32FromInt32(0) addrStart = _sqlite3VdbeCurrentAddr(tls, v) addrBreak1 = _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), regEnd, int32(1)) addrBreak2 = _windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, int32(1)) } } _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addrStart) _sqlite3VdbeJumpHere(tls, v, addrBreak2) addrStart = _sqlite3VdbeCurrentAddr(tls, v) addrBreak3 = _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, int32(1)) _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addrStart) _sqlite3VdbeJumpHere(tls, v, addrBreak1) _sqlite3VdbeJumpHere(tls, v, addrBreak3) } else { _windowCodeOp(tls, bp, int32(WINDOW_AGGSTEP), 0, 0) addrStart1 = _sqlite3VdbeCurrentAddr(tls, v) addrBreak = _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, int32(1)) _windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0) _sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addrStart1) _sqlite3VdbeJumpHere(tls, v, addrBreak) } } _sqlite3VdbeJumpHere(tls, v, addrEmpty) _sqlite3VdbeAddOp1(tls, v, int32(OP_ResetSorter), (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fcurrent.Fcsr) if (*TWindow)(unsafe.Pointer(pMWin)).FpPartition != 0 { if (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid != 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (*TWindow)(unsafe.Pointer(pMWin)).FregEndRowid) } _sqlite3VdbeChangeP1(tls, v, addrInteger, _sqlite3VdbeCurrentAddr(tls, v)) _sqlite3VdbeAddOp1(tls, v, int32(OP_Return), regFlushPart) } } /************** End of window.c **********************************************/ /************** Begin file parse.c *******************************************/ /* This file is automatically generated by Lemon from input grammar ** source file "parse.y". */ /* ** 2001-09-15 ** ** 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 contains SQLite's SQL parser. ** ** The canonical source code to this file ("parse.y") is a Lemon grammar ** file that specifies the input grammar and actions to take while parsing. ** That input file is processed by Lemon to generate a C-language ** implementation of a parser for the given grammar. You might be reading ** this comment as part of the translated C-code. Edits should be made ** to the original parse.y sources. */ /* #include "sqliteInt.h" */ /* ** Disable all error recovery processing in the parser push-down ** automaton. */ /* ** Make yytestcase() the same as testcase() */ /* ** Indicate that sqlite3ParserFree() will never be called with a null ** pointer. */ /* ** In the amalgamation, the parse.c file generated by lemon and the ** tokenize.c file are concatenated. In that case, sqlite3RunParser() ** has access to the the size of the yyParser object and so the parser ** engine can be allocated from stack. In that case, only the ** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked ** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be ** omitted. */ /* ** Alternative datatype for the argument to the malloc() routine passed ** into sqlite3ParserAlloc(). The default is size_t. */ /* ** An instance of the following structure describes the event of a ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT, ** TK_DELETE, or TK_INSTEAD. If the event is of the form ** ** UPDATE ON (a,b,c) ** ** Then the "b" IdList records the list "a,b,c". */ type TTrigEvent = struct { Fa int32 Fb uintptr } type TrigEvent = TTrigEvent type TFrameBound = struct { FeType int32 FpExpr uintptr } type FrameBound = TFrameBound // C documentation // // /* // ** Disable lookaside memory allocation for objects that might be // ** shared across database connections. // */ func _disableLookaside(tls *libc.TLS, pParse uintptr) { var db uintptr _ = db db = (*TParse)(unsafe.Pointer(pParse)).Fdb (*TParse)(unsafe.Pointer(pParse)).FdisableLookaside++ (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable++ (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0) } // C documentation // // /* // ** For a compound SELECT statement, make sure p->pPrior->pNext==p for // ** all elements in the list. And make sure list length does not exceed // ** SQLITE_LIMIT_COMPOUND_SELECT. // */ func _parserDoubleLinkSelect(tls *libc.TLS, pParse uintptr, p uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var cnt, mxSelect, v2 int32 var pLoop, pNext, v1 uintptr var v3 bool _, _, _, _, _, _, _ = cnt, mxSelect, pLoop, pNext, v1, v2, v3 _ = libc.Int32FromInt32(0) if (*TSelect)(unsafe.Pointer(p)).FpPrior != 0 { pNext = uintptr(0) pLoop = p cnt = int32(1) for int32(1) != 0 { (*TSelect)(unsafe.Pointer(pLoop)).FpNext = pNext *(*Tu32)(unsafe.Pointer(pLoop + 4)) |= uint32(SF_Compound) pNext = pLoop pLoop = (*TSelect)(unsafe.Pointer(pLoop)).FpPrior if pLoop == uintptr(0) { break } cnt++ if (*TSelect)(unsafe.Pointer(pLoop)).FpOrderBy != 0 || (*TSelect)(unsafe.Pointer(pLoop)).FpLimit != 0 { if (*TSelect)(unsafe.Pointer(pLoop)).FpOrderBy != uintptr(0) { v1 = __ccgo_ts + 23207 } else { v1 = __ccgo_ts + 23216 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23222, libc.VaList(bp+8, v1, _sqlite3SelectOpName(tls, int32((*TSelect)(unsafe.Pointer(pNext)).Fop)))) break } } if v3 = (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_MultiValue) == uint32(0); v3 { v2 = *(*int32)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb + 120 + 4*4)) mxSelect = v2 } if v3 && v2 > 0 && cnt > mxSelect { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23264, 0) } } } // C documentation // // /* Attach a With object describing the WITH clause to a Select // ** object describing the query for which the WITH clause is a prefix. // */ func _attachWithToSelect(tls *libc.TLS, pParse uintptr, pSelect uintptr, pWith uintptr) (r uintptr) { if pSelect != 0 { (*TSelect)(unsafe.Pointer(pSelect)).FpWith = pWith _parserDoubleLinkSelect(tls, pParse, pSelect) } else { _sqlite3WithDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pWith) } return pSelect } // C documentation // // /* Construct a new Expr object from a single token */ func _tokenExpr(tls *libc.TLS, pParse uintptr, op int32, _t TToken) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) *(*TToken)(unsafe.Pointer(bp)) = _t var p, v1 uintptr _, _ = p, v1 p = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(52)+(*(*TToken)(unsafe.Pointer(bp))).Fn+uint32(1))) if p != 0 { /* memset(p, 0, sizeof(Expr)); */ (*TExpr)(unsafe.Pointer(p)).Fop = uint8(op) (*TExpr)(unsafe.Pointer(p)).FaffExpr = uint8(0) (*TExpr)(unsafe.Pointer(p)).Fflags = uint32(EP_Leaf) /* p->iAgg = -1; // Not required */ v1 = libc.UintptrFromInt32(0) (*TExpr)(unsafe.Pointer(p)).FpRight = v1 (*TExpr)(unsafe.Pointer(p)).FpLeft = v1 (*TExpr)(unsafe.Pointer(p)).FpAggInfo = uintptr(0) libc.Xmemset(tls, p+20, 0, uint32(4)) libc.Xmemset(tls, p+44, 0, uint32(8)) (*TExpr)(unsafe.Pointer(p)).Fop2 = uint8(0) (*TExpr)(unsafe.Pointer(p)).FiTable = 0 (*TExpr)(unsafe.Pointer(p)).FiColumn = 0 *(*uintptr)(unsafe.Pointer(p + 8)) = p + 1*52 libc.Xmemcpy(tls, *(*uintptr)(unsafe.Pointer(p + 8)), (*(*TToken)(unsafe.Pointer(bp))).Fz, (*(*TToken)(unsafe.Pointer(bp))).Fn) *(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 8)) + uintptr((*(*TToken)(unsafe.Pointer(bp))).Fn))) = uint8(0) *(*int32)(unsafe.Pointer(p + 36)) = int32((*(*TToken)(unsafe.Pointer(bp))).Fz) - int32((*TParse)(unsafe.Pointer(pParse)).FzTail) if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 8))))])&int32(0x80) != 0 { _sqlite3DequoteExpr(tls, p) } (*TExpr)(unsafe.Pointer(p)).FnHeight = int32(1) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { return _sqlite3RenameTokenMap(tls, pParse, p, bp) } } return p } // C documentation // // /* A routine to convert a binary TK_IS or TK_ISNOT expression into a // ** unary TK_ISNULL or TK_NOTNULL expression. */ func _binaryToUnaryIfNull(tls *libc.TLS, pParse uintptr, pY uintptr, pA uintptr, op int32) { var db uintptr _ = db db = (*TParse)(unsafe.Pointer(pParse)).Fdb if pA != 0 && pY != 0 && int32((*TExpr)(unsafe.Pointer(pY)).Fop) == int32(TK_NULL) && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) { (*TExpr)(unsafe.Pointer(pA)).Fop = uint8(op) _sqlite3ExprDelete(tls, db, (*TExpr)(unsafe.Pointer(pA)).FpRight) (*TExpr)(unsafe.Pointer(pA)).FpRight = uintptr(0) } } // C documentation // // /* Add a single new term to an ExprList that is used to store a // ** list of identifiers. Report an error if the ID list contains // ** a COLLATE clause or an ASC or DESC keyword, except ignore the // ** error while parsing a legacy schema. // */ func _parserAddExprIdListTerm(tls *libc.TLS, pParse uintptr, pPrior uintptr, pIdToken uintptr, hasCollate int32, sortOrder int32) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var p uintptr _ = p p = _sqlite3ExprListAppend(tls, pParse, pPrior, uintptr(0)) if (hasCollate != 0 || sortOrder != -int32(1)) && int32((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Finit1.Fbusy) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23298, libc.VaList(bp+8, (*TToken)(unsafe.Pointer(pIdToken)).Fn, (*TToken)(unsafe.Pointer(pIdToken)).Fz)) } _sqlite3ExprListSetName(tls, pParse, p, pIdToken, int32(1)) return p } /**************** End of %include directives **********************************/ /* These constants specify the various numeric values for terminal symbols. ***************** Begin token definitions *************************************/ /**************** End token definitions ***************************************/ // C documentation // // /* The next sections is a series of control #defines. // ** various aspects of the generated parser. // ** YYCODETYPE is the data type used to store the integer codes // ** that represent terminal and non-terminal symbols. // ** "unsigned char" is used if there are fewer than // ** 256 symbols. Larger types otherwise. // ** YYNOCODE is a number of type YYCODETYPE that is not used for // ** any terminal or nonterminal symbol. // ** YYFALLBACK If defined, this indicates that one or more tokens // ** (also known as: "terminal symbols") have fall-back // ** values which should be used if the original symbol // ** would not parse. This permits keywords to sometimes // ** be used as identifiers, for example. // ** YYACTIONTYPE is the data type used for "action codes" - numbers // ** that indicate what to do in response to the next // ** token. // ** sqlite3ParserTOKENTYPE is the data type used for minor type for terminal // ** symbols. Background: A "minor type" is a semantic // ** value associated with a terminal or non-terminal // ** symbols. For example, for an "ID" terminal symbol, // ** the minor type might be the name of the identifier. // ** Each non-terminal can have a different minor type. // ** Terminal symbols all have the same minor type, though. // ** This macros defines the minor type for terminal // ** symbols. // ** YYMINORTYPE is the data type used for all minor types. // ** This is typically a union of many types, one of // ** which is sqlite3ParserTOKENTYPE. The entry in the union // ** for terminal symbols is called "yy0". // ** YYSTACKDEPTH is the maximum depth of the parser's stack. If // ** zero the stack is dynamically sized using realloc() // ** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument // ** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument // ** sqlite3ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter // ** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser // ** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser // ** sqlite3ParserCTX_* As sqlite3ParserARG_ except for %extra_context // ** YYERRORSYMBOL is the code number of the error symbol. If not // ** defined, then do no error processing. // ** YYNSTATE the combined number of states. // ** YYNRULE the number of rules in the grammar // ** YYNTOKEN Number of terminal symbols // ** YY_MAX_SHIFT Maximum value for shift actions // ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions // ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions // ** YY_ERROR_ACTION The yy_action[] code for syntax error // ** YY_ACCEPT_ACTION The yy_action[] code for accept // ** YY_NO_ACTION The yy_action[] code for no-op // ** YY_MIN_REDUCE Minimum value for reduce actions // ** YY_MAX_REDUCE Maximum value for reduce actions // */ // /************* Begin control #defines *****************************************/ type TYYMINORTYPE = struct { Fyy0 [0]TToken Fyy33 [0]uintptr Fyy41 [0]uintptr Fyy47 [0]uintptr Fyy131 [0]uintptr Fyy180 [0]TTrigEvent Fyy231 [0]struct { Fvalue int32 Fmask int32 } Fyy254 [0]uintptr Fyy285 [0]Tu32 Fyy322 [0]uintptr Fyy385 [0]uintptr Fyy394 [0]int32 Fyy444 [0]uintptr Fyy516 [0]Tu8 Fyy521 [0]uintptr Fyy522 [0]uintptr Fyy528 [0]uintptr Fyy561 [0]TOnOrUsing Fyy595 [0]TFrameBound Fyyinit int32 F__ccgo_pad20 [4]byte } type YYMINORTYPE = TYYMINORTYPE /************* End control #defines *******************************************/ /* Define the yytestcase() macro to be a no-op if is not already defined ** otherwise. ** ** Applications can choose to define yytestcase() in the %include section ** to a macro that can assist in verifying code coverage. For production ** code the yytestcase() macro should be turned off. But it is useful ** for testing. */ // C documentation // // /* Next are the tables used to determine what action to take based on the // ** current state and lookahead token. These tables are used to implement // ** functions that take a state number and lookahead value and return an // ** action integer. // ** // ** Suppose the action integer is N. Then the action is determined as // ** follows // ** // ** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead // ** token onto the stack and goto state N. // ** // ** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then // ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE. // ** // ** N == YY_ERROR_ACTION A syntax error has occurred. // ** // ** N == YY_ACCEPT_ACTION The parser accepts its input. // ** // ** N == YY_NO_ACTION No such action. Denotes unused // ** slots in the yy_action[] table. // ** // ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE // ** and YY_MAX_REDUCE // ** // ** The action table is constructed as a single large table named yy_action[]. // ** Given state S and lookahead X, the action is computed as either: // ** // ** (A) N = yy_action[ yy_shift_ofst[S] + X ] // ** (B) N = yy_default[S] // ** // ** The (A) formula is preferred. The B formula is used instead if // ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X. // ** // ** The formulas above are for computing the action when the lookahead is // ** a terminal symbol. If the lookahead is a non-terminal (as occurs after // ** a reduce action) then the yy_reduce_ofst[] array is used in place of // ** the yy_shift_ofst[] array. // ** // ** The following are the tables generated in this section: // ** // ** yy_action[] A single table containing all actions. // ** yy_lookahead[] A table containing the lookahead for each entry in // ** yy_action. Used to detect hash collisions. // ** yy_shift_ofst[] For each state, the offset into yy_action for // ** shifting terminals. // ** yy_reduce_ofst[] For each state, the offset into yy_action for // ** shifting non-terminals after a reduce. // ** yy_default[] Default action for each state. // ** // *********** Begin parsing tables **********************************************/ var _yy_action = [2100]uint16{ 0: uint16(572), 1: uint16(210), 2: uint16(572), 3: uint16(119), 4: uint16(116), 5: uint16(231), 6: uint16(572), 7: uint16(119), 8: uint16(116), 9: uint16(231), 10: uint16(572), 11: uint16(1317), 12: uint16(379), 13: uint16(1296), 14: uint16(410), 15: uint16(566), 16: uint16(566), 17: uint16(566), 18: uint16(572), 19: uint16(411), 20: uint16(380), 21: uint16(1317), 22: uint16(1279), 23: uint16(42), 24: uint16(42), 25: uint16(42), 26: uint16(42), 27: uint16(210), 28: uint16(1529), 29: uint16(72), 30: uint16(72), 31: uint16(974), 32: uint16(421), 33: uint16(42), 34: uint16(42), 35: uint16(495), 36: uint16(305), 37: uint16(281), 38: uint16(305), 39: uint16(975), 40: uint16(399), 41: uint16(72), 42: uint16(72), 43: uint16(126), 44: uint16(127), 45: uint16(81), 46: uint16(1217), 47: uint16(1217), 48: uint16(1054), 49: uint16(1057), 50: uint16(1044), 51: uint16(1044), 52: uint16(124), 53: uint16(124), 54: uint16(125), 55: uint16(125), 56: uint16(125), 57: uint16(125), 58: uint16(480), 59: uint16(411), 60: uint16(1244), 61: uint16(1), 62: uint16(1), 63: uint16(578), 64: uint16(2), 65: uint16(1248), 66: uint16(554), 67: uint16(119), 68: uint16(116), 69: uint16(231), 70: uint16(319), 71: uint16(484), 72: uint16(147), 73: uint16(484), 74: uint16(528), 75: uint16(119), 76: uint16(116), 77: uint16(231), 78: uint16(533), 79: uint16(1330), 80: uint16(419), 81: uint16(527), 82: uint16(143), 83: uint16(126), 84: uint16(127), 85: uint16(81), 86: uint16(1217), 87: uint16(1217), 88: uint16(1054), 89: uint16(1057), 90: uint16(1044), 91: uint16(1044), 92: uint16(124), 93: uint16(124), 94: uint16(125), 95: uint16(125), 96: uint16(125), 97: uint16(125), 98: uint16(119), 99: uint16(116), 100: uint16(231), 101: uint16(329), 102: uint16(123), 103: uint16(123), 104: uint16(123), 105: uint16(123), 106: uint16(122), 107: uint16(122), 108: uint16(121), 109: uint16(121), 110: uint16(121), 111: uint16(120), 112: uint16(117), 113: uint16(448), 114: uint16(286), 115: uint16(286), 116: uint16(286), 117: uint16(286), 118: uint16(446), 119: uint16(446), 120: uint16(446), 121: uint16(1568), 122: uint16(378), 123: uint16(1570), 124: uint16(1193), 125: uint16(377), 126: uint16(1164), 127: uint16(569), 128: uint16(1164), 129: uint16(569), 130: uint16(411), 131: uint16(1568), 132: uint16(541), 133: uint16(261), 134: uint16(228), 135: uint16(448), 136: uint16(102), 137: uint16(146), 138: uint16(453), 139: uint16(318), 140: uint16(563), 141: uint16(242), 142: uint16(123), 143: uint16(123), 144: uint16(123), 145: uint16(123), 146: uint16(122), 147: uint16(122), 148: uint16(121), 149: uint16(121), 150: uint16(121), 151: uint16(120), 152: uint16(117), 153: uint16(448), 154: uint16(126), 155: uint16(127), 156: uint16(81), 157: uint16(1217), 158: uint16(1217), 159: uint16(1054), 160: uint16(1057), 161: uint16(1044), 162: uint16(1044), 163: uint16(124), 164: uint16(124), 165: uint16(125), 166: uint16(125), 167: uint16(125), 168: uint16(125), 169: uint16(143), 170: uint16(296), 171: uint16(1193), 172: uint16(341), 173: uint16(452), 174: uint16(121), 175: uint16(121), 176: uint16(121), 177: uint16(120), 178: uint16(117), 179: uint16(448), 180: uint16(128), 181: uint16(1193), 182: uint16(1194), 183: uint16(1193), 184: uint16(149), 185: uint16(445), 186: uint16(444), 187: uint16(572), 188: uint16(120), 189: uint16(117), 190: uint16(448), 191: uint16(125), 192: uint16(125), 193: uint16(125), 194: uint16(125), 195: uint16(118), 196: uint16(123), 197: uint16(123), 198: uint16(123), 199: uint16(123), 200: uint16(122), 201: uint16(122), 202: uint16(121), 203: uint16(121), 204: uint16(121), 205: uint16(120), 206: uint16(117), 207: uint16(448), 208: uint16(458), 209: uint16(114), 210: uint16(13), 211: uint16(13), 212: uint16(550), 213: uint16(123), 214: uint16(123), 215: uint16(123), 216: uint16(123), 217: uint16(122), 218: uint16(122), 219: uint16(121), 220: uint16(121), 221: uint16(121), 222: uint16(120), 223: uint16(117), 224: uint16(448), 225: uint16(424), 226: uint16(318), 227: uint16(563), 228: uint16(1193), 229: uint16(1194), 230: uint16(1193), 231: uint16(150), 232: uint16(1225), 233: uint16(411), 234: uint16(1225), 235: uint16(125), 236: uint16(125), 237: uint16(125), 238: uint16(125), 239: uint16(123), 240: uint16(123), 241: uint16(123), 242: uint16(123), 243: uint16(122), 244: uint16(122), 245: uint16(121), 246: uint16(121), 247: uint16(121), 248: uint16(120), 249: uint16(117), 250: uint16(448), 251: uint16(469), 252: uint16(344), 253: uint16(1041), 254: uint16(1041), 255: uint16(1055), 256: uint16(1058), 257: uint16(126), 258: uint16(127), 259: uint16(81), 260: uint16(1217), 261: uint16(1217), 262: uint16(1054), 263: uint16(1057), 264: uint16(1044), 265: uint16(1044), 266: uint16(124), 267: uint16(124), 268: uint16(125), 269: uint16(125), 270: uint16(125), 271: uint16(125), 272: uint16(1282), 273: uint16(526), 274: uint16(224), 275: uint16(1193), 276: uint16(572), 277: uint16(411), 278: uint16(226), 279: uint16(519), 280: uint16(177), 281: uint16(83), 282: uint16(84), 283: uint16(123), 284: uint16(123), 285: uint16(123), 286: uint16(123), 287: uint16(122), 288: uint16(122), 289: uint16(121), 290: uint16(121), 291: uint16(121), 292: uint16(120), 293: uint16(117), 294: uint16(448), 295: uint16(1010), 296: uint16(16), 297: uint16(16), 298: uint16(1193), 299: uint16(134), 300: uint16(134), 301: uint16(126), 302: uint16(127), 303: uint16(81), 304: uint16(1217), 305: uint16(1217), 306: uint16(1054), 307: uint16(1057), 308: uint16(1044), 309: uint16(1044), 310: uint16(124), 311: uint16(124), 312: uint16(125), 313: uint16(125), 314: uint16(125), 315: uint16(125), 316: uint16(123), 317: uint16(123), 318: uint16(123), 319: uint16(123), 320: uint16(122), 321: uint16(122), 322: uint16(121), 323: uint16(121), 324: uint16(121), 325: uint16(120), 326: uint16(117), 327: uint16(448), 328: uint16(1045), 329: uint16(550), 330: uint16(1193), 331: uint16(375), 332: uint16(1193), 333: uint16(1194), 334: uint16(1193), 335: uint16(254), 336: uint16(1438), 337: uint16(401), 338: uint16(508), 339: uint16(505), 340: uint16(504), 341: uint16(112), 342: uint16(564), 343: uint16(570), 344: uint16(4), 345: uint16(929), 346: uint16(929), 347: uint16(435), 348: uint16(503), 349: uint16(342), 350: uint16(464), 351: uint16(330), 352: uint16(362), 353: uint16(396), 354: uint16(1238), 355: uint16(1193), 356: uint16(1194), 357: uint16(1193), 358: uint16(567), 359: uint16(572), 360: uint16(123), 361: uint16(123), 362: uint16(123), 363: uint16(123), 364: uint16(122), 365: uint16(122), 366: uint16(121), 367: uint16(121), 368: uint16(121), 369: uint16(120), 370: uint16(117), 371: uint16(448), 372: uint16(286), 373: uint16(286), 374: uint16(371), 375: uint16(1581), 376: uint16(1607), 377: uint16(445), 378: uint16(444), 379: uint16(155), 380: uint16(411), 381: uint16(449), 382: uint16(72), 383: uint16(72), 384: uint16(1289), 385: uint16(569), 386: uint16(1222), 387: uint16(1193), 388: uint16(1194), 389: uint16(1193), 390: uint16(86), 391: uint16(1224), 392: uint16(273), 393: uint16(561), 394: uint16(547), 395: uint16(520), 396: uint16(520), 397: uint16(572), 398: uint16(99), 399: uint16(1223), 400: uint16(6), 401: uint16(1281), 402: uint16(476), 403: uint16(143), 404: uint16(126), 405: uint16(127), 406: uint16(81), 407: uint16(1217), 408: uint16(1217), 409: uint16(1054), 410: uint16(1057), 411: uint16(1044), 412: uint16(1044), 413: uint16(124), 414: uint16(124), 415: uint16(125), 416: uint16(125), 417: uint16(125), 418: uint16(125), 419: uint16(554), 420: uint16(13), 421: uint16(13), 422: uint16(1031), 423: uint16(511), 424: uint16(1225), 425: uint16(1193), 426: uint16(1225), 427: uint16(553), 428: uint16(110), 429: uint16(110), 430: uint16(224), 431: uint16(572), 432: uint16(1239), 433: uint16(177), 434: uint16(572), 435: uint16(429), 436: uint16(111), 437: uint16(199), 438: uint16(449), 439: uint16(573), 440: uint16(449), 441: uint16(432), 442: uint16(1555), 443: uint16(1019), 444: uint16(327), 445: uint16(555), 446: uint16(1193), 447: uint16(272), 448: uint16(289), 449: uint16(370), 450: uint16(514), 451: uint16(365), 452: uint16(513), 453: uint16(259), 454: uint16(72), 455: uint16(72), 456: uint16(547), 457: uint16(72), 458: uint16(72), 459: uint16(361), 460: uint16(318), 461: uint16(563), 462: uint16(1613), 463: uint16(123), 464: uint16(123), 465: uint16(123), 466: uint16(123), 467: uint16(122), 468: uint16(122), 469: uint16(121), 470: uint16(121), 471: uint16(121), 472: uint16(120), 473: uint16(117), 474: uint16(448), 475: uint16(1019), 476: uint16(1019), 477: uint16(1021), 478: uint16(1022), 479: uint16(28), 480: uint16(286), 481: uint16(286), 482: uint16(1193), 483: uint16(1194), 484: uint16(1193), 485: uint16(1159), 486: uint16(572), 487: uint16(1612), 488: uint16(411), 489: uint16(904), 490: uint16(192), 491: uint16(554), 492: uint16(358), 493: uint16(569), 494: uint16(554), 495: uint16(940), 496: uint16(537), 497: uint16(521), 498: uint16(1159), 499: uint16(437), 500: uint16(415), 501: uint16(1159), 502: uint16(556), 503: uint16(1193), 504: uint16(1194), 505: uint16(1193), 506: uint16(572), 507: uint16(548), 508: uint16(548), 509: uint16(52), 510: uint16(52), 511: uint16(216), 512: uint16(126), 513: uint16(127), 514: uint16(81), 515: uint16(1217), 516: uint16(1217), 517: uint16(1054), 518: uint16(1057), 519: uint16(1044), 520: uint16(1044), 521: uint16(124), 522: uint16(124), 523: uint16(125), 524: uint16(125), 525: uint16(125), 526: uint16(125), 527: uint16(1193), 528: uint16(478), 529: uint16(136), 530: uint16(136), 531: uint16(411), 532: uint16(286), 533: uint16(286), 534: uint16(1493), 535: uint16(509), 536: uint16(122), 537: uint16(122), 538: uint16(121), 539: uint16(121), 540: uint16(121), 541: uint16(120), 542: uint16(117), 543: uint16(448), 544: uint16(1010), 545: uint16(569), 546: uint16(522), 547: uint16(219), 548: uint16(545), 549: uint16(545), 550: uint16(318), 551: uint16(563), 552: uint16(143), 553: uint16(6), 554: uint16(536), 555: uint16(126), 556: uint16(127), 557: uint16(81), 558: uint16(1217), 559: uint16(1217), 560: uint16(1054), 561: uint16(1057), 562: uint16(1044), 563: uint16(1044), 564: uint16(124), 565: uint16(124), 566: uint16(125), 567: uint16(125), 568: uint16(125), 569: uint16(125), 570: uint16(1557), 571: uint16(123), 572: uint16(123), 573: uint16(123), 574: uint16(123), 575: uint16(122), 576: uint16(122), 577: uint16(121), 578: uint16(121), 579: uint16(121), 580: uint16(120), 581: uint16(117), 582: uint16(448), 583: uint16(489), 584: uint16(1193), 585: uint16(1194), 586: uint16(1193), 587: uint16(486), 588: uint16(283), 589: uint16(1270), 590: uint16(960), 591: uint16(254), 592: uint16(1193), 593: uint16(375), 594: uint16(508), 595: uint16(505), 596: uint16(504), 597: uint16(1193), 598: uint16(342), 599: uint16(574), 600: uint16(1193), 601: uint16(574), 602: uint16(411), 603: uint16(294), 604: uint16(503), 605: uint16(960), 606: uint16(879), 607: uint16(193), 608: uint16(484), 609: uint16(318), 610: uint16(563), 611: uint16(386), 612: uint16(292), 613: uint16(382), 614: uint16(123), 615: uint16(123), 616: uint16(123), 617: uint16(123), 618: uint16(122), 619: uint16(122), 620: uint16(121), 621: uint16(121), 622: uint16(121), 623: uint16(120), 624: uint16(117), 625: uint16(448), 626: uint16(126), 627: uint16(127), 628: uint16(81), 629: uint16(1217), 630: uint16(1217), 631: uint16(1054), 632: uint16(1057), 633: uint16(1044), 634: uint16(1044), 635: uint16(124), 636: uint16(124), 637: uint16(125), 638: uint16(125), 639: uint16(125), 640: uint16(125), 641: uint16(411), 642: uint16(396), 643: uint16(1139), 644: uint16(1193), 645: uint16(872), 646: uint16(101), 647: uint16(286), 648: uint16(286), 649: uint16(1193), 650: uint16(1194), 651: uint16(1193), 652: uint16(375), 653: uint16(1096), 654: uint16(1193), 655: uint16(1194), 656: uint16(1193), 657: uint16(1193), 658: uint16(1194), 659: uint16(1193), 660: uint16(569), 661: uint16(459), 662: uint16(33), 663: uint16(375), 664: uint16(235), 665: uint16(126), 666: uint16(127), 667: uint16(81), 668: uint16(1217), 669: uint16(1217), 670: uint16(1054), 671: uint16(1057), 672: uint16(1044), 673: uint16(1044), 674: uint16(124), 675: uint16(124), 676: uint16(125), 677: uint16(125), 678: uint16(125), 679: uint16(125), 680: uint16(1437), 681: uint16(962), 682: uint16(572), 683: uint16(230), 684: uint16(961), 685: uint16(123), 686: uint16(123), 687: uint16(123), 688: uint16(123), 689: uint16(122), 690: uint16(122), 691: uint16(121), 692: uint16(121), 693: uint16(121), 694: uint16(120), 695: uint16(117), 696: uint16(448), 697: uint16(1159), 698: uint16(230), 699: uint16(1193), 700: uint16(158), 701: uint16(1193), 702: uint16(1194), 703: uint16(1193), 704: uint16(1556), 705: uint16(13), 706: uint16(13), 707: uint16(303), 708: uint16(960), 709: uint16(1233), 710: uint16(1159), 711: uint16(154), 712: uint16(411), 713: uint16(1159), 714: uint16(375), 715: uint16(1584), 716: uint16(1177), 717: uint16(5), 718: uint16(371), 719: uint16(1581), 720: uint16(431), 721: uint16(1239), 722: uint16(3), 723: uint16(960), 724: uint16(123), 725: uint16(123), 726: uint16(123), 727: uint16(123), 728: uint16(122), 729: uint16(122), 730: uint16(121), 731: uint16(121), 732: uint16(121), 733: uint16(120), 734: uint16(117), 735: uint16(448), 736: uint16(126), 737: uint16(127), 738: uint16(81), 739: uint16(1217), 740: uint16(1217), 741: uint16(1054), 742: uint16(1057), 743: uint16(1044), 744: uint16(1044), 745: uint16(124), 746: uint16(124), 747: uint16(125), 748: uint16(125), 749: uint16(125), 750: uint16(125), 751: uint16(411), 752: uint16(210), 753: uint16(571), 754: uint16(1193), 755: uint16(1032), 756: uint16(1193), 757: uint16(1194), 758: uint16(1193), 759: uint16(1193), 760: uint16(390), 761: uint16(855), 762: uint16(156), 763: uint16(1555), 764: uint16(376), 765: uint16(404), 766: uint16(1101), 767: uint16(1101), 768: uint16(492), 769: uint16(572), 770: uint16(469), 771: uint16(344), 772: uint16(1322), 773: uint16(1322), 774: uint16(1555), 775: uint16(126), 776: uint16(127), 777: uint16(81), 778: uint16(1217), 779: uint16(1217), 780: uint16(1054), 781: uint16(1057), 782: uint16(1044), 783: uint16(1044), 784: uint16(124), 785: uint16(124), 786: uint16(125), 787: uint16(125), 788: uint16(125), 789: uint16(125), 790: uint16(130), 791: uint16(572), 792: uint16(13), 793: uint16(13), 794: uint16(532), 795: uint16(123), 796: uint16(123), 797: uint16(123), 798: uint16(123), 799: uint16(122), 800: uint16(122), 801: uint16(121), 802: uint16(121), 803: uint16(121), 804: uint16(120), 805: uint16(117), 806: uint16(448), 807: uint16(304), 808: uint16(572), 809: uint16(457), 810: uint16(229), 811: uint16(1193), 812: uint16(1194), 813: uint16(1193), 814: uint16(13), 815: uint16(13), 816: uint16(1193), 817: uint16(1194), 818: uint16(1193), 819: uint16(1300), 820: uint16(467), 821: uint16(1270), 822: uint16(411), 823: uint16(1320), 824: uint16(1320), 825: uint16(1555), 826: uint16(1015), 827: uint16(457), 828: uint16(456), 829: uint16(436), 830: uint16(301), 831: uint16(72), 832: uint16(72), 833: uint16(1268), 834: uint16(123), 835: uint16(123), 836: uint16(123), 837: uint16(123), 838: uint16(122), 839: uint16(122), 840: uint16(121), 841: uint16(121), 842: uint16(121), 843: uint16(120), 844: uint16(117), 845: uint16(448), 846: uint16(126), 847: uint16(127), 848: uint16(81), 849: uint16(1217), 850: uint16(1217), 851: uint16(1054), 852: uint16(1057), 853: uint16(1044), 854: uint16(1044), 855: uint16(124), 856: uint16(124), 857: uint16(125), 858: uint16(125), 859: uint16(125), 860: uint16(125), 861: uint16(411), 862: uint16(384), 863: uint16(1076), 864: uint16(1159), 865: uint16(286), 866: uint16(286), 867: uint16(421), 868: uint16(314), 869: uint16(280), 870: uint16(280), 871: uint16(287), 872: uint16(287), 873: uint16(461), 874: uint16(408), 875: uint16(407), 876: uint16(1539), 877: uint16(1159), 878: uint16(569), 879: uint16(572), 880: uint16(1159), 881: uint16(1196), 882: uint16(569), 883: uint16(409), 884: uint16(569), 885: uint16(126), 886: uint16(127), 887: uint16(81), 888: uint16(1217), 889: uint16(1217), 890: uint16(1054), 891: uint16(1057), 892: uint16(1044), 893: uint16(1044), 894: uint16(124), 895: uint16(124), 896: uint16(125), 897: uint16(125), 898: uint16(125), 899: uint16(125), 900: uint16(457), 901: uint16(1485), 902: uint16(13), 903: uint16(13), 904: uint16(1541), 905: uint16(123), 906: uint16(123), 907: uint16(123), 908: uint16(123), 909: uint16(122), 910: uint16(122), 911: uint16(121), 912: uint16(121), 913: uint16(121), 914: uint16(120), 915: uint16(117), 916: uint16(448), 917: uint16(202), 918: uint16(572), 919: uint16(462), 920: uint16(1587), 921: uint16(578), 922: uint16(2), 923: uint16(1248), 924: uint16(843), 925: uint16(844), 926: uint16(845), 927: uint16(1563), 928: uint16(319), 929: uint16(409), 930: uint16(147), 931: uint16(6), 932: uint16(411), 933: uint16(257), 934: uint16(256), 935: uint16(255), 936: uint16(208), 937: uint16(1330), 938: uint16(9), 939: uint16(1196), 940: uint16(264), 941: uint16(72), 942: uint16(72), 943: uint16(1436), 944: uint16(123), 945: uint16(123), 946: uint16(123), 947: uint16(123), 948: uint16(122), 949: uint16(122), 950: uint16(121), 951: uint16(121), 952: uint16(121), 953: uint16(120), 954: uint16(117), 955: uint16(448), 956: uint16(126), 957: uint16(127), 958: uint16(81), 959: uint16(1217), 960: uint16(1217), 961: uint16(1054), 962: uint16(1057), 963: uint16(1044), 964: uint16(1044), 965: uint16(124), 966: uint16(124), 967: uint16(125), 968: uint16(125), 969: uint16(125), 970: uint16(125), 971: uint16(572), 972: uint16(286), 973: uint16(286), 974: uint16(572), 975: uint16(1213), 976: uint16(411), 977: uint16(577), 978: uint16(315), 979: uint16(1248), 980: uint16(421), 981: uint16(371), 982: uint16(1581), 983: uint16(356), 984: uint16(319), 985: uint16(569), 986: uint16(147), 987: uint16(495), 988: uint16(529), 989: uint16(1644), 990: uint16(397), 991: uint16(935), 992: uint16(495), 993: uint16(1330), 994: uint16(71), 995: uint16(71), 996: uint16(934), 997: uint16(72), 998: uint16(72), 999: uint16(242), 1000: uint16(1328), 1001: uint16(105), 1002: uint16(81), 1003: uint16(1217), 1004: uint16(1217), 1005: uint16(1054), 1006: uint16(1057), 1007: uint16(1044), 1008: uint16(1044), 1009: uint16(124), 1010: uint16(124), 1011: uint16(125), 1012: uint16(125), 1013: uint16(125), 1014: uint16(125), 1015: uint16(123), 1016: uint16(123), 1017: uint16(123), 1018: uint16(123), 1019: uint16(122), 1020: uint16(122), 1021: uint16(121), 1022: uint16(121), 1023: uint16(121), 1024: uint16(120), 1025: uint16(117), 1026: uint16(448), 1027: uint16(1117), 1028: uint16(286), 1029: uint16(286), 1030: uint16(1422), 1031: uint16(452), 1032: uint16(1528), 1033: uint16(1213), 1034: uint16(443), 1035: uint16(286), 1036: uint16(286), 1037: uint16(1492), 1038: uint16(1355), 1039: uint16(313), 1040: uint16(478), 1041: uint16(569), 1042: uint16(1118), 1043: uint16(454), 1044: uint16(351), 1045: uint16(495), 1046: uint16(354), 1047: uint16(1266), 1048: uint16(569), 1049: uint16(209), 1050: uint16(572), 1051: uint16(418), 1052: uint16(179), 1053: uint16(572), 1054: uint16(1031), 1055: uint16(242), 1056: uint16(385), 1057: uint16(1119), 1058: uint16(523), 1059: uint16(123), 1060: uint16(123), 1061: uint16(123), 1062: uint16(123), 1063: uint16(122), 1064: uint16(122), 1065: uint16(121), 1066: uint16(121), 1067: uint16(121), 1068: uint16(120), 1069: uint16(117), 1070: uint16(448), 1071: uint16(1020), 1072: uint16(108), 1073: uint16(72), 1074: uint16(72), 1075: uint16(1019), 1076: uint16(13), 1077: uint16(13), 1078: uint16(915), 1079: uint16(572), 1080: uint16(1498), 1081: uint16(572), 1082: uint16(286), 1083: uint16(286), 1084: uint16(98), 1085: uint16(530), 1086: uint16(1537), 1087: uint16(452), 1088: uint16(916), 1089: uint16(1334), 1090: uint16(1329), 1091: uint16(203), 1092: uint16(411), 1093: uint16(286), 1094: uint16(286), 1095: uint16(569), 1096: uint16(152), 1097: uint16(211), 1098: uint16(1498), 1099: uint16(1500), 1100: uint16(426), 1101: uint16(569), 1102: uint16(56), 1103: uint16(56), 1104: uint16(57), 1105: uint16(57), 1106: uint16(569), 1107: uint16(1019), 1108: uint16(1019), 1109: uint16(1021), 1110: uint16(447), 1111: uint16(572), 1112: uint16(411), 1113: uint16(531), 1114: uint16(12), 1115: uint16(297), 1116: uint16(126), 1117: uint16(127), 1118: uint16(81), 1119: uint16(1217), 1120: uint16(1217), 1121: uint16(1054), 1122: uint16(1057), 1123: uint16(1044), 1124: uint16(1044), 1125: uint16(124), 1126: uint16(124), 1127: uint16(125), 1128: uint16(125), 1129: uint16(125), 1130: uint16(125), 1131: uint16(572), 1132: uint16(411), 1133: uint16(867), 1134: uint16(15), 1135: uint16(15), 1136: uint16(126), 1137: uint16(127), 1138: uint16(81), 1139: uint16(1217), 1140: uint16(1217), 1141: uint16(1054), 1142: uint16(1057), 1143: uint16(1044), 1144: uint16(1044), 1145: uint16(124), 1146: uint16(124), 1147: uint16(125), 1148: uint16(125), 1149: uint16(125), 1150: uint16(125), 1151: uint16(373), 1152: uint16(529), 1153: uint16(264), 1154: uint16(44), 1155: uint16(44), 1156: uint16(126), 1157: uint16(115), 1158: uint16(81), 1159: uint16(1217), 1160: uint16(1217), 1161: uint16(1054), 1162: uint16(1057), 1163: uint16(1044), 1164: uint16(1044), 1165: uint16(124), 1166: uint16(124), 1167: uint16(125), 1168: uint16(125), 1169: uint16(125), 1170: uint16(125), 1171: uint16(1498), 1172: uint16(478), 1173: uint16(1271), 1174: uint16(417), 1175: uint16(123), 1176: uint16(123), 1177: uint16(123), 1178: uint16(123), 1179: uint16(122), 1180: uint16(122), 1181: uint16(121), 1182: uint16(121), 1183: uint16(121), 1184: uint16(120), 1185: uint16(117), 1186: uint16(448), 1187: uint16(205), 1188: uint16(1213), 1189: uint16(495), 1190: uint16(430), 1191: uint16(867), 1192: uint16(468), 1193: uint16(322), 1194: uint16(495), 1195: uint16(123), 1196: uint16(123), 1197: uint16(123), 1198: uint16(123), 1199: uint16(122), 1200: uint16(122), 1201: uint16(121), 1202: uint16(121), 1203: uint16(121), 1204: uint16(120), 1205: uint16(117), 1206: uint16(448), 1207: uint16(572), 1208: uint16(557), 1209: uint16(1140), 1210: uint16(1642), 1211: uint16(1422), 1212: uint16(1642), 1213: uint16(543), 1214: uint16(572), 1215: uint16(123), 1216: uint16(123), 1217: uint16(123), 1218: uint16(123), 1219: uint16(122), 1220: uint16(122), 1221: uint16(121), 1222: uint16(121), 1223: uint16(121), 1224: uint16(120), 1225: uint16(117), 1226: uint16(448), 1227: uint16(572), 1228: uint16(1422), 1229: uint16(572), 1230: uint16(13), 1231: uint16(13), 1232: uint16(542), 1233: uint16(323), 1234: uint16(1325), 1235: uint16(411), 1236: uint16(334), 1237: uint16(58), 1238: uint16(58), 1239: uint16(349), 1240: uint16(1422), 1241: uint16(1170), 1242: uint16(326), 1243: uint16(286), 1244: uint16(286), 1245: uint16(549), 1246: uint16(1213), 1247: uint16(300), 1248: uint16(895), 1249: uint16(530), 1250: uint16(45), 1251: uint16(45), 1252: uint16(59), 1253: uint16(59), 1254: uint16(1140), 1255: uint16(1643), 1256: uint16(569), 1257: uint16(1643), 1258: uint16(565), 1259: uint16(417), 1260: uint16(127), 1261: uint16(81), 1262: uint16(1217), 1263: uint16(1217), 1264: uint16(1054), 1265: uint16(1057), 1266: uint16(1044), 1267: uint16(1044), 1268: uint16(124), 1269: uint16(124), 1270: uint16(125), 1271: uint16(125), 1272: uint16(125), 1273: uint16(125), 1274: uint16(1367), 1275: uint16(373), 1276: uint16(500), 1277: uint16(290), 1278: uint16(1193), 1279: uint16(512), 1280: uint16(1366), 1281: uint16(427), 1282: uint16(394), 1283: uint16(394), 1284: uint16(393), 1285: uint16(275), 1286: uint16(391), 1287: uint16(896), 1288: uint16(1138), 1289: uint16(852), 1290: uint16(478), 1291: uint16(258), 1292: uint16(1422), 1293: uint16(1170), 1294: uint16(463), 1295: uint16(1159), 1296: uint16(12), 1297: uint16(331), 1298: uint16(428), 1299: uint16(333), 1300: uint16(1117), 1301: uint16(460), 1302: uint16(236), 1303: uint16(258), 1304: uint16(325), 1305: uint16(460), 1306: uint16(544), 1307: uint16(1544), 1308: uint16(1159), 1309: uint16(1098), 1310: uint16(491), 1311: uint16(1159), 1312: uint16(324), 1313: uint16(1098), 1314: uint16(440), 1315: uint16(1118), 1316: uint16(335), 1317: uint16(516), 1318: uint16(123), 1319: uint16(123), 1320: uint16(123), 1321: uint16(123), 1322: uint16(122), 1323: uint16(122), 1324: uint16(121), 1325: uint16(121), 1326: uint16(121), 1327: uint16(120), 1328: uint16(117), 1329: uint16(448), 1330: uint16(1119), 1331: uint16(318), 1332: uint16(563), 1333: uint16(1138), 1334: uint16(572), 1335: uint16(1193), 1336: uint16(1194), 1337: uint16(1193), 1338: uint16(112), 1339: uint16(564), 1340: uint16(201), 1341: uint16(4), 1342: uint16(238), 1343: uint16(433), 1344: uint16(935), 1345: uint16(490), 1346: uint16(285), 1347: uint16(228), 1348: uint16(1517), 1349: uint16(934), 1350: uint16(170), 1351: uint16(560), 1352: uint16(572), 1353: uint16(142), 1354: uint16(1516), 1355: uint16(567), 1356: uint16(572), 1357: uint16(60), 1358: uint16(60), 1359: uint16(572), 1360: uint16(416), 1361: uint16(572), 1362: uint16(441), 1363: uint16(572), 1364: uint16(535), 1365: uint16(302), 1366: uint16(875), 1367: uint16(8), 1368: uint16(487), 1369: uint16(572), 1370: uint16(237), 1371: uint16(572), 1372: uint16(416), 1373: uint16(572), 1374: uint16(485), 1375: uint16(61), 1376: uint16(61), 1377: uint16(572), 1378: uint16(449), 1379: uint16(62), 1380: uint16(62), 1381: uint16(332), 1382: uint16(63), 1383: uint16(63), 1384: uint16(46), 1385: uint16(46), 1386: uint16(47), 1387: uint16(47), 1388: uint16(361), 1389: uint16(572), 1390: uint16(561), 1391: uint16(572), 1392: uint16(48), 1393: uint16(48), 1394: uint16(50), 1395: uint16(50), 1396: uint16(51), 1397: uint16(51), 1398: uint16(572), 1399: uint16(295), 1400: uint16(64), 1401: uint16(64), 1402: uint16(482), 1403: uint16(295), 1404: uint16(539), 1405: uint16(412), 1406: uint16(471), 1407: uint16(1031), 1408: uint16(572), 1409: uint16(538), 1410: uint16(318), 1411: uint16(563), 1412: uint16(65), 1413: uint16(65), 1414: uint16(66), 1415: uint16(66), 1416: uint16(409), 1417: uint16(475), 1418: uint16(572), 1419: uint16(1031), 1420: uint16(572), 1421: uint16(14), 1422: uint16(14), 1423: uint16(875), 1424: uint16(1020), 1425: uint16(110), 1426: uint16(110), 1427: uint16(409), 1428: uint16(1019), 1429: uint16(572), 1430: uint16(474), 1431: uint16(67), 1432: uint16(67), 1433: uint16(111), 1434: uint16(455), 1435: uint16(449), 1436: uint16(573), 1437: uint16(449), 1438: uint16(98), 1439: uint16(317), 1440: uint16(1019), 1441: uint16(132), 1442: uint16(132), 1443: uint16(133), 1444: uint16(133), 1445: uint16(572), 1446: uint16(1561), 1447: uint16(572), 1448: uint16(974), 1449: uint16(409), 1450: uint16(6), 1451: uint16(1562), 1452: uint16(68), 1453: uint16(68), 1454: uint16(1560), 1455: uint16(6), 1456: uint16(975), 1457: uint16(572), 1458: uint16(6), 1459: uint16(1559), 1460: uint16(1019), 1461: uint16(1019), 1462: uint16(1021), 1463: uint16(6), 1464: uint16(346), 1465: uint16(218), 1466: uint16(101), 1467: uint16(531), 1468: uint16(53), 1469: uint16(53), 1470: uint16(69), 1471: uint16(69), 1472: uint16(1019), 1473: uint16(1019), 1474: uint16(1021), 1475: uint16(1022), 1476: uint16(28), 1477: uint16(1586), 1478: uint16(1181), 1479: uint16(451), 1480: uint16(70), 1481: uint16(70), 1482: uint16(290), 1483: uint16(87), 1484: uint16(215), 1485: uint16(31), 1486: uint16(1363), 1487: uint16(394), 1488: uint16(394), 1489: uint16(393), 1490: uint16(275), 1491: uint16(391), 1492: uint16(350), 1493: uint16(109), 1494: uint16(852), 1495: uint16(107), 1496: uint16(572), 1497: uint16(112), 1498: uint16(564), 1499: uint16(483), 1500: uint16(4), 1501: uint16(1212), 1502: uint16(572), 1503: uint16(239), 1504: uint16(153), 1505: uint16(572), 1506: uint16(39), 1507: uint16(236), 1508: uint16(1299), 1509: uint16(325), 1510: uint16(112), 1511: uint16(564), 1512: uint16(1298), 1513: uint16(4), 1514: uint16(567), 1515: uint16(572), 1516: uint16(32), 1517: uint16(324), 1518: uint16(572), 1519: uint16(54), 1520: uint16(54), 1521: uint16(572), 1522: uint16(1135), 1523: uint16(353), 1524: uint16(398), 1525: uint16(165), 1526: uint16(165), 1527: uint16(567), 1528: uint16(166), 1529: uint16(166), 1530: uint16(572), 1531: uint16(291), 1532: uint16(355), 1533: uint16(572), 1534: uint16(17), 1535: uint16(357), 1536: uint16(572), 1537: uint16(449), 1538: uint16(77), 1539: uint16(77), 1540: uint16(1313), 1541: uint16(55), 1542: uint16(55), 1543: uint16(1297), 1544: uint16(73), 1545: uint16(73), 1546: uint16(572), 1547: uint16(238), 1548: uint16(470), 1549: uint16(561), 1550: uint16(449), 1551: uint16(472), 1552: uint16(364), 1553: uint16(135), 1554: uint16(135), 1555: uint16(170), 1556: uint16(74), 1557: uint16(74), 1558: uint16(142), 1559: uint16(163), 1560: uint16(163), 1561: uint16(374), 1562: uint16(561), 1563: uint16(539), 1564: uint16(572), 1565: uint16(321), 1566: uint16(572), 1567: uint16(886), 1568: uint16(540), 1569: uint16(137), 1570: uint16(137), 1571: uint16(339), 1572: uint16(1353), 1573: uint16(422), 1574: uint16(298), 1575: uint16(237), 1576: uint16(539), 1577: uint16(572), 1578: uint16(1031), 1579: uint16(572), 1580: uint16(340), 1581: uint16(538), 1582: uint16(101), 1583: uint16(369), 1584: uint16(110), 1585: uint16(110), 1586: uint16(162), 1587: uint16(131), 1588: uint16(131), 1589: uint16(164), 1590: uint16(164), 1591: uint16(1031), 1592: uint16(111), 1593: uint16(368), 1594: uint16(449), 1595: uint16(573), 1596: uint16(449), 1597: uint16(110), 1598: uint16(110), 1599: uint16(1019), 1600: uint16(157), 1601: uint16(157), 1602: uint16(141), 1603: uint16(141), 1604: uint16(572), 1605: uint16(111), 1606: uint16(572), 1607: uint16(449), 1608: uint16(573), 1609: uint16(449), 1610: uint16(412), 1611: uint16(288), 1612: uint16(1019), 1613: uint16(572), 1614: uint16(882), 1615: uint16(318), 1616: uint16(563), 1617: uint16(572), 1618: uint16(219), 1619: uint16(572), 1620: uint16(241), 1621: uint16(1012), 1622: uint16(477), 1623: uint16(263), 1624: uint16(263), 1625: uint16(894), 1626: uint16(893), 1627: uint16(140), 1628: uint16(140), 1629: uint16(138), 1630: uint16(138), 1631: uint16(1019), 1632: uint16(1019), 1633: uint16(1021), 1634: uint16(1022), 1635: uint16(28), 1636: uint16(139), 1637: uint16(139), 1638: uint16(525), 1639: uint16(455), 1640: uint16(76), 1641: uint16(76), 1642: uint16(78), 1643: uint16(78), 1644: uint16(1019), 1645: uint16(1019), 1646: uint16(1021), 1647: uint16(1022), 1648: uint16(28), 1649: uint16(1181), 1650: uint16(451), 1651: uint16(572), 1652: uint16(1083), 1653: uint16(290), 1654: uint16(112), 1655: uint16(564), 1656: uint16(1575), 1657: uint16(4), 1658: uint16(394), 1659: uint16(394), 1660: uint16(393), 1661: uint16(275), 1662: uint16(391), 1663: uint16(572), 1664: uint16(1023), 1665: uint16(852), 1666: uint16(572), 1667: uint16(479), 1668: uint16(345), 1669: uint16(263), 1670: uint16(101), 1671: uint16(567), 1672: uint16(882), 1673: uint16(1376), 1674: uint16(75), 1675: uint16(75), 1676: uint16(1421), 1677: uint16(501), 1678: uint16(236), 1679: uint16(260), 1680: uint16(325), 1681: uint16(112), 1682: uint16(564), 1683: uint16(359), 1684: uint16(4), 1685: uint16(101), 1686: uint16(43), 1687: uint16(43), 1688: uint16(324), 1689: uint16(49), 1690: uint16(49), 1691: uint16(901), 1692: uint16(902), 1693: uint16(161), 1694: uint16(449), 1695: uint16(101), 1696: uint16(977), 1697: uint16(978), 1698: uint16(567), 1699: uint16(1079), 1700: uint16(1349), 1701: uint16(260), 1702: uint16(965), 1703: uint16(932), 1704: uint16(263), 1705: uint16(114), 1706: uint16(561), 1707: uint16(1095), 1708: uint16(517), 1709: uint16(1095), 1710: uint16(1083), 1711: uint16(1094), 1712: uint16(865), 1713: uint16(1094), 1714: uint16(151), 1715: uint16(933), 1716: uint16(1144), 1717: uint16(114), 1718: uint16(238), 1719: uint16(1361), 1720: uint16(558), 1721: uint16(449), 1722: uint16(1023), 1723: uint16(559), 1724: uint16(1426), 1725: uint16(1278), 1726: uint16(170), 1727: uint16(1269), 1728: uint16(1257), 1729: uint16(142), 1730: uint16(1601), 1731: uint16(1256), 1732: uint16(1258), 1733: uint16(561), 1734: uint16(1594), 1735: uint16(1031), 1736: uint16(496), 1737: uint16(278), 1738: uint16(213), 1739: uint16(1346), 1740: uint16(310), 1741: uint16(110), 1742: uint16(110), 1743: uint16(939), 1744: uint16(311), 1745: uint16(312), 1746: uint16(237), 1747: uint16(11), 1748: uint16(234), 1749: uint16(111), 1750: uint16(221), 1751: uint16(449), 1752: uint16(573), 1753: uint16(449), 1754: uint16(293), 1755: uint16(395), 1756: uint16(1019), 1757: uint16(1408), 1758: uint16(337), 1759: uint16(1403), 1760: uint16(1396), 1761: uint16(338), 1762: uint16(1031), 1763: uint16(299), 1764: uint16(343), 1765: uint16(1413), 1766: uint16(1412), 1767: uint16(481), 1768: uint16(110), 1769: uint16(110), 1770: uint16(506), 1771: uint16(402), 1772: uint16(225), 1773: uint16(1296), 1774: uint16(206), 1775: uint16(367), 1776: uint16(111), 1777: uint16(1358), 1778: uint16(449), 1779: uint16(573), 1780: uint16(449), 1781: uint16(412), 1782: uint16(1359), 1783: uint16(1019), 1784: uint16(1489), 1785: uint16(1488), 1786: uint16(318), 1787: uint16(563), 1788: uint16(1019), 1789: uint16(1019), 1790: uint16(1021), 1791: uint16(1022), 1792: uint16(28), 1793: uint16(562), 1794: uint16(207), 1795: uint16(220), 1796: uint16(80), 1797: uint16(564), 1798: uint16(389), 1799: uint16(4), 1800: uint16(1597), 1801: uint16(1357), 1802: uint16(552), 1803: uint16(1356), 1804: uint16(1233), 1805: uint16(181), 1806: uint16(267), 1807: uint16(232), 1808: uint16(1536), 1809: uint16(1534), 1810: uint16(455), 1811: uint16(1230), 1812: uint16(420), 1813: uint16(567), 1814: uint16(82), 1815: uint16(1019), 1816: uint16(1019), 1817: uint16(1021), 1818: uint16(1022), 1819: uint16(28), 1820: uint16(86), 1821: uint16(217), 1822: uint16(85), 1823: uint16(1494), 1824: uint16(190), 1825: uint16(175), 1826: uint16(183), 1827: uint16(465), 1828: uint16(185), 1829: uint16(466), 1830: uint16(36), 1831: uint16(1409), 1832: uint16(186), 1833: uint16(187), 1834: uint16(188), 1835: uint16(499), 1836: uint16(449), 1837: uint16(244), 1838: uint16(37), 1839: uint16(99), 1840: uint16(400), 1841: uint16(1415), 1842: uint16(1414), 1843: uint16(488), 1844: uint16(1417), 1845: uint16(194), 1846: uint16(473), 1847: uint16(403), 1848: uint16(561), 1849: uint16(1483), 1850: uint16(248), 1851: uint16(92), 1852: uint16(1505), 1853: uint16(494), 1854: uint16(198), 1855: uint16(279), 1856: uint16(112), 1857: uint16(564), 1858: uint16(250), 1859: uint16(4), 1860: uint16(348), 1861: uint16(497), 1862: uint16(405), 1863: uint16(352), 1864: uint16(1259), 1865: uint16(251), 1866: uint16(252), 1867: uint16(515), 1868: uint16(1316), 1869: uint16(434), 1870: uint16(1315), 1871: uint16(1314), 1872: uint16(94), 1873: uint16(567), 1874: uint16(1307), 1875: uint16(886), 1876: uint16(1306), 1877: uint16(1031), 1878: uint16(226), 1879: uint16(406), 1880: uint16(1611), 1881: uint16(1610), 1882: uint16(438), 1883: uint16(110), 1884: uint16(110), 1885: uint16(1580), 1886: uint16(1286), 1887: uint16(524), 1888: uint16(439), 1889: uint16(308), 1890: uint16(266), 1891: uint16(111), 1892: uint16(1285), 1893: uint16(449), 1894: uint16(573), 1895: uint16(449), 1896: uint16(449), 1897: uint16(309), 1898: uint16(1019), 1899: uint16(366), 1900: uint16(1284), 1901: uint16(1609), 1902: uint16(265), 1903: uint16(1566), 1904: uint16(1565), 1905: uint16(442), 1906: uint16(372), 1907: uint16(1381), 1908: uint16(561), 1909: uint16(129), 1910: uint16(550), 1911: uint16(1380), 1912: uint16(10), 1913: uint16(1470), 1914: uint16(383), 1915: uint16(106), 1916: uint16(316), 1917: uint16(551), 1918: uint16(100), 1919: uint16(35), 1920: uint16(534), 1921: uint16(575), 1922: uint16(212), 1923: uint16(1339), 1924: uint16(381), 1925: uint16(387), 1926: uint16(1187), 1927: uint16(1338), 1928: uint16(274), 1929: uint16(276), 1930: uint16(1019), 1931: uint16(1019), 1932: uint16(1021), 1933: uint16(1022), 1934: uint16(28), 1935: uint16(277), 1936: uint16(413), 1937: uint16(1031), 1938: uint16(576), 1939: uint16(1254), 1940: uint16(388), 1941: uint16(1521), 1942: uint16(1249), 1943: uint16(110), 1944: uint16(110), 1945: uint16(167), 1946: uint16(1522), 1947: uint16(168), 1948: uint16(148), 1949: uint16(1520), 1950: uint16(1519), 1951: uint16(111), 1952: uint16(306), 1953: uint16(449), 1954: uint16(573), 1955: uint16(449), 1956: uint16(222), 1957: uint16(223), 1958: uint16(1019), 1959: uint16(839), 1960: uint16(169), 1961: uint16(79), 1962: uint16(450), 1963: uint16(214), 1964: uint16(414), 1965: uint16(233), 1966: uint16(320), 1967: uint16(145), 1968: uint16(1093), 1969: uint16(1091), 1970: uint16(328), 1971: uint16(182), 1972: uint16(171), 1973: uint16(1212), 1974: uint16(918), 1975: uint16(184), 1976: uint16(240), 1977: uint16(336), 1978: uint16(243), 1979: uint16(1107), 1980: uint16(189), 1981: uint16(172), 1982: uint16(173), 1983: uint16(423), 1984: uint16(425), 1985: uint16(88), 1986: uint16(180), 1987: uint16(191), 1988: uint16(89), 1989: uint16(90), 1990: uint16(1019), 1991: uint16(1019), 1992: uint16(1021), 1993: uint16(1022), 1994: uint16(28), 1995: uint16(91), 1996: uint16(174), 1997: uint16(1110), 1998: uint16(245), 1999: uint16(1106), 2000: uint16(246), 2001: uint16(159), 2002: uint16(18), 2003: uint16(247), 2004: uint16(347), 2005: uint16(1099), 2006: uint16(263), 2007: uint16(195), 2008: uint16(1227), 2009: uint16(493), 2010: uint16(249), 2011: uint16(196), 2012: uint16(38), 2013: uint16(854), 2014: uint16(498), 2015: uint16(368), 2016: uint16(253), 2017: uint16(360), 2018: uint16(897), 2019: uint16(197), 2020: uint16(502), 2021: uint16(93), 2022: uint16(19), 2023: uint16(20), 2024: uint16(507), 2025: uint16(884), 2026: uint16(363), 2027: uint16(510), 2028: uint16(95), 2029: uint16(307), 2030: uint16(160), 2031: uint16(96), 2032: uint16(518), 2033: uint16(97), 2034: uint16(1175), 2035: uint16(1060), 2036: uint16(1146), 2037: uint16(40), 2038: uint16(21), 2039: uint16(227), 2040: uint16(176), 2041: uint16(1145), 2042: uint16(282), 2043: uint16(284), 2044: uint16(969), 2045: uint16(200), 2046: uint16(963), 2047: uint16(114), 2048: uint16(262), 2049: uint16(1165), 2050: uint16(22), 2051: uint16(23), 2052: uint16(24), 2053: uint16(1161), 2054: uint16(1169), 2055: uint16(25), 2056: uint16(1163), 2057: uint16(1150), 2058: uint16(34), 2059: uint16(26), 2060: uint16(1168), 2061: uint16(546), 2062: uint16(27), 2063: uint16(204), 2064: uint16(101), 2065: uint16(103), 2066: uint16(104), 2067: uint16(1074), 2068: uint16(7), 2069: uint16(1061), 2070: uint16(1059), 2071: uint16(1063), 2072: uint16(1116), 2073: uint16(1064), 2074: uint16(1115), 2075: uint16(268), 2076: uint16(269), 2077: uint16(29), 2078: uint16(41), 2079: uint16(270), 2080: uint16(1024), 2081: uint16(866), 2082: uint16(113), 2083: uint16(30), 2084: uint16(568), 2085: uint16(392), 2086: uint16(1183), 2087: uint16(144), 2088: uint16(178), 2089: uint16(1182), 2090: uint16(271), 2091: uint16(928), 2092: uint16(1245), 2093: uint16(1245), 2094: uint16(1245), 2095: uint16(1245), 2096: uint16(1245), 2097: uint16(1245), 2098: uint16(1245), 2099: uint16(1602), } var _yy_lookahead = [2285]uint16{ 0: uint16(193), 1: uint16(193), 2: uint16(193), 3: uint16(274), 4: uint16(275), 5: uint16(276), 6: uint16(193), 7: uint16(274), 8: uint16(275), 9: uint16(276), 10: uint16(193), 11: uint16(223), 12: uint16(219), 13: uint16(225), 14: uint16(206), 15: uint16(210), 16: uint16(211), 17: uint16(212), 18: uint16(193), 19: uint16(19), 20: uint16(219), 21: uint16(233), 22: uint16(216), 23: uint16(216), 24: uint16(217), 25: uint16(216), 26: uint16(217), 27: uint16(193), 28: uint16(295), 29: uint16(216), 30: uint16(217), 31: uint16(31), 32: uint16(193), 33: uint16(216), 34: uint16(217), 35: uint16(193), 36: uint16(228), 37: uint16(213), 38: uint16(230), 39: uint16(39), 40: uint16(206), 41: uint16(216), 42: uint16(217), 43: uint16(43), 44: uint16(44), 45: uint16(45), 46: uint16(46), 47: uint16(47), 48: uint16(48), 49: uint16(49), 50: uint16(50), 51: uint16(51), 52: uint16(52), 53: uint16(53), 54: uint16(54), 55: uint16(55), 56: uint16(56), 57: uint16(57), 58: uint16(193), 59: uint16(19), 60: uint16(185), 61: uint16(186), 62: uint16(187), 63: uint16(188), 64: uint16(189), 65: uint16(190), 66: uint16(253), 67: uint16(274), 68: uint16(275), 69: uint16(276), 70: uint16(195), 71: uint16(193), 72: uint16(197), 73: uint16(193), 74: uint16(261), 75: uint16(274), 76: uint16(275), 77: uint16(276), 78: uint16(253), 79: uint16(204), 80: uint16(238), 81: uint16(204), 82: uint16(81), 83: uint16(43), 84: uint16(44), 85: uint16(45), 86: uint16(46), 87: uint16(47), 88: uint16(48), 89: uint16(49), 90: uint16(50), 91: uint16(51), 92: uint16(52), 93: uint16(53), 94: uint16(54), 95: uint16(55), 96: uint16(56), 97: uint16(57), 98: uint16(274), 99: uint16(275), 100: uint16(276), 101: uint16(262), 102: uint16(102), 103: uint16(103), 104: uint16(104), 105: uint16(105), 106: uint16(106), 107: uint16(107), 108: uint16(108), 109: uint16(109), 110: uint16(110), 111: uint16(111), 112: uint16(112), 113: uint16(113), 114: uint16(239), 115: uint16(240), 116: uint16(239), 117: uint16(240), 118: uint16(210), 119: uint16(211), 120: uint16(212), 121: uint16(314), 122: uint16(315), 123: uint16(314), 124: uint16(59), 125: uint16(316), 126: uint16(86), 127: uint16(252), 128: uint16(88), 129: uint16(252), 130: uint16(19), 131: uint16(314), 132: uint16(315), 133: uint16(256), 134: uint16(257), 135: uint16(113), 136: uint16(25), 137: uint16(72), 138: uint16(296), 139: uint16(138), 140: uint16(139), 141: uint16(266), 142: uint16(102), 143: uint16(103), 144: uint16(104), 145: uint16(105), 146: uint16(106), 147: uint16(107), 148: uint16(108), 149: uint16(109), 150: uint16(110), 151: uint16(111), 152: uint16(112), 153: uint16(113), 154: uint16(43), 155: uint16(44), 156: uint16(45), 157: uint16(46), 158: uint16(47), 159: uint16(48), 160: uint16(49), 161: uint16(50), 162: uint16(51), 163: uint16(52), 164: uint16(53), 165: uint16(54), 166: uint16(55), 167: uint16(56), 168: uint16(57), 169: uint16(81), 170: uint16(292), 171: uint16(59), 172: uint16(292), 173: uint16(298), 174: uint16(108), 175: uint16(109), 176: uint16(110), 177: uint16(111), 178: uint16(112), 179: uint16(113), 180: uint16(69), 181: uint16(116), 182: uint16(117), 183: uint16(118), 184: uint16(72), 185: uint16(106), 186: uint16(107), 187: uint16(193), 188: uint16(111), 189: uint16(112), 190: uint16(113), 191: uint16(54), 192: uint16(55), 193: uint16(56), 194: uint16(57), 195: uint16(58), 196: uint16(102), 197: uint16(103), 198: uint16(104), 199: uint16(105), 200: uint16(106), 201: uint16(107), 202: uint16(108), 203: uint16(109), 204: uint16(110), 205: uint16(111), 206: uint16(112), 207: uint16(113), 208: uint16(120), 209: uint16(25), 210: uint16(216), 211: uint16(217), 212: uint16(145), 213: uint16(102), 214: uint16(103), 215: uint16(104), 216: uint16(105), 217: uint16(106), 218: uint16(107), 219: uint16(108), 220: uint16(109), 221: uint16(110), 222: uint16(111), 223: uint16(112), 224: uint16(113), 225: uint16(231), 226: uint16(138), 227: uint16(139), 228: uint16(116), 229: uint16(117), 230: uint16(118), 231: uint16(164), 232: uint16(153), 233: uint16(19), 234: uint16(155), 235: uint16(54), 236: uint16(55), 237: uint16(56), 238: uint16(57), 239: uint16(102), 240: uint16(103), 241: uint16(104), 242: uint16(105), 243: uint16(106), 244: uint16(107), 245: uint16(108), 246: uint16(109), 247: uint16(110), 248: uint16(111), 249: uint16(112), 250: uint16(113), 251: uint16(128), 252: uint16(129), 253: uint16(46), 254: uint16(47), 255: uint16(48), 256: uint16(49), 257: uint16(43), 258: uint16(44), 259: uint16(45), 260: uint16(46), 261: uint16(47), 262: uint16(48), 263: uint16(49), 264: uint16(50), 265: uint16(51), 266: uint16(52), 267: uint16(53), 268: uint16(54), 269: uint16(55), 270: uint16(56), 271: uint16(57), 272: uint16(216), 273: uint16(193), 274: uint16(25), 275: uint16(59), 276: uint16(193), 277: uint16(19), 278: uint16(165), 279: uint16(166), 280: uint16(193), 281: uint16(67), 282: uint16(24), 283: uint16(102), 284: uint16(103), 285: uint16(104), 286: uint16(105), 287: uint16(106), 288: uint16(107), 289: uint16(108), 290: uint16(109), 291: uint16(110), 292: uint16(111), 293: uint16(112), 294: uint16(113), 295: uint16(73), 296: uint16(216), 297: uint16(217), 298: uint16(59), 299: uint16(216), 300: uint16(217), 301: uint16(43), 302: uint16(44), 303: uint16(45), 304: uint16(46), 305: uint16(47), 306: uint16(48), 307: uint16(49), 308: uint16(50), 309: uint16(51), 310: uint16(52), 311: uint16(53), 312: uint16(54), 313: uint16(55), 314: uint16(56), 315: uint16(57), 316: uint16(102), 317: uint16(103), 318: uint16(104), 319: uint16(105), 320: uint16(106), 321: uint16(107), 322: uint16(108), 323: uint16(109), 324: uint16(110), 325: uint16(111), 326: uint16(112), 327: uint16(113), 328: uint16(121), 329: uint16(145), 330: uint16(59), 331: uint16(193), 332: uint16(116), 333: uint16(117), 334: uint16(118), 335: uint16(119), 336: uint16(273), 337: uint16(204), 338: uint16(122), 339: uint16(123), 340: uint16(124), 341: uint16(19), 342: uint16(20), 343: uint16(134), 344: uint16(22), 345: uint16(136), 346: uint16(137), 347: uint16(19), 348: uint16(132), 349: uint16(127), 350: uint16(128), 351: uint16(129), 352: uint16(24), 353: uint16(22), 354: uint16(23), 355: uint16(116), 356: uint16(117), 357: uint16(118), 358: uint16(36), 359: uint16(193), 360: uint16(102), 361: uint16(103), 362: uint16(104), 363: uint16(105), 364: uint16(106), 365: uint16(107), 366: uint16(108), 367: uint16(109), 368: uint16(110), 369: uint16(111), 370: uint16(112), 371: uint16(113), 372: uint16(239), 373: uint16(240), 374: uint16(311), 375: uint16(312), 376: uint16(215), 377: uint16(106), 378: uint16(107), 379: uint16(241), 380: uint16(19), 381: uint16(59), 382: uint16(216), 383: uint16(217), 384: uint16(223), 385: uint16(252), 386: uint16(115), 387: uint16(116), 388: uint16(117), 389: uint16(118), 390: uint16(151), 391: uint16(120), 392: uint16(26), 393: uint16(71), 394: uint16(193), 395: uint16(308), 396: uint16(309), 397: uint16(193), 398: uint16(149), 399: uint16(128), 400: uint16(313), 401: uint16(216), 402: uint16(269), 403: uint16(81), 404: uint16(43), 405: uint16(44), 406: uint16(45), 407: uint16(46), 408: uint16(47), 409: uint16(48), 410: uint16(49), 411: uint16(50), 412: uint16(51), 413: uint16(52), 414: uint16(53), 415: uint16(54), 416: uint16(55), 417: uint16(56), 418: uint16(57), 419: uint16(253), 420: uint16(216), 421: uint16(217), 422: uint16(100), 423: uint16(95), 424: uint16(153), 425: uint16(59), 426: uint16(155), 427: uint16(261), 428: uint16(106), 429: uint16(107), 430: uint16(25), 431: uint16(193), 432: uint16(101), 433: uint16(193), 434: uint16(193), 435: uint16(231), 436: uint16(114), 437: uint16(25), 438: uint16(116), 439: uint16(117), 440: uint16(118), 441: uint16(113), 442: uint16(304), 443: uint16(121), 444: uint16(193), 445: uint16(204), 446: uint16(59), 447: uint16(119), 448: uint16(120), 449: uint16(121), 450: uint16(122), 451: uint16(123), 452: uint16(124), 453: uint16(125), 454: uint16(216), 455: uint16(217), 456: uint16(193), 457: uint16(216), 458: uint16(217), 459: uint16(131), 460: uint16(138), 461: uint16(139), 462: uint16(230), 463: uint16(102), 464: uint16(103), 465: uint16(104), 466: uint16(105), 467: uint16(106), 468: uint16(107), 469: uint16(108), 470: uint16(109), 471: uint16(110), 472: uint16(111), 473: uint16(112), 474: uint16(113), 475: uint16(153), 476: uint16(154), 477: uint16(155), 478: uint16(156), 479: uint16(157), 480: uint16(239), 481: uint16(240), 482: uint16(116), 483: uint16(117), 484: uint16(118), 485: uint16(76), 486: uint16(193), 487: uint16(23), 488: uint16(19), 489: uint16(25), 490: uint16(22), 491: uint16(253), 492: uint16(23), 493: uint16(252), 494: uint16(253), 495: uint16(108), 496: uint16(87), 497: uint16(204), 498: uint16(89), 499: uint16(261), 500: uint16(198), 501: uint16(92), 502: uint16(261), 503: uint16(116), 504: uint16(117), 505: uint16(118), 506: uint16(193), 507: uint16(306), 508: uint16(307), 509: uint16(216), 510: uint16(217), 511: uint16(150), 512: uint16(43), 513: uint16(44), 514: uint16(45), 515: uint16(46), 516: uint16(47), 517: uint16(48), 518: uint16(49), 519: uint16(50), 520: uint16(51), 521: uint16(52), 522: uint16(53), 523: uint16(54), 524: uint16(55), 525: uint16(56), 526: uint16(57), 527: uint16(59), 528: uint16(193), 529: uint16(216), 530: uint16(217), 531: uint16(19), 532: uint16(239), 533: uint16(240), 534: uint16(283), 535: uint16(23), 536: uint16(106), 537: uint16(107), 538: uint16(108), 539: uint16(109), 540: uint16(110), 541: uint16(111), 542: uint16(112), 543: uint16(113), 544: uint16(73), 545: uint16(252), 546: uint16(253), 547: uint16(142), 548: uint16(308), 549: uint16(309), 550: uint16(138), 551: uint16(139), 552: uint16(81), 553: uint16(313), 554: uint16(145), 555: uint16(43), 556: uint16(44), 557: uint16(45), 558: uint16(46), 559: uint16(47), 560: uint16(48), 561: uint16(49), 562: uint16(50), 563: uint16(51), 564: uint16(52), 565: uint16(53), 566: uint16(54), 567: uint16(55), 568: uint16(56), 569: uint16(57), 570: uint16(307), 571: uint16(102), 572: uint16(103), 573: uint16(104), 574: uint16(105), 575: uint16(106), 576: uint16(107), 577: uint16(108), 578: uint16(109), 579: uint16(110), 580: uint16(111), 581: uint16(112), 582: uint16(113), 583: uint16(281), 584: uint16(116), 585: uint16(117), 586: uint16(118), 587: uint16(285), 588: uint16(23), 589: uint16(193), 590: uint16(25), 591: uint16(119), 592: uint16(59), 593: uint16(193), 594: uint16(122), 595: uint16(123), 596: uint16(124), 597: uint16(59), 598: uint16(127), 599: uint16(203), 600: uint16(59), 601: uint16(205), 602: uint16(19), 603: uint16(268), 604: uint16(132), 605: uint16(25), 606: uint16(23), 607: uint16(22), 608: uint16(193), 609: uint16(138), 610: uint16(139), 611: uint16(249), 612: uint16(204), 613: uint16(251), 614: uint16(102), 615: uint16(103), 616: uint16(104), 617: uint16(105), 618: uint16(106), 619: uint16(107), 620: uint16(108), 621: uint16(109), 622: uint16(110), 623: uint16(111), 624: uint16(112), 625: uint16(113), 626: uint16(43), 627: uint16(44), 628: uint16(45), 629: uint16(46), 630: uint16(47), 631: uint16(48), 632: uint16(49), 633: uint16(50), 634: uint16(51), 635: uint16(52), 636: uint16(53), 637: uint16(54), 638: uint16(55), 639: uint16(56), 640: uint16(57), 641: uint16(19), 642: uint16(22), 643: uint16(23), 644: uint16(59), 645: uint16(23), 646: uint16(25), 647: uint16(239), 648: uint16(240), 649: uint16(116), 650: uint16(117), 651: uint16(118), 652: uint16(193), 653: uint16(11), 654: uint16(116), 655: uint16(117), 656: uint16(118), 657: uint16(116), 658: uint16(117), 659: uint16(118), 660: uint16(252), 661: uint16(269), 662: uint16(22), 663: uint16(193), 664: uint16(15), 665: uint16(43), 666: uint16(44), 667: uint16(45), 668: uint16(46), 669: uint16(47), 670: uint16(48), 671: uint16(49), 672: uint16(50), 673: uint16(51), 674: uint16(52), 675: uint16(53), 676: uint16(54), 677: uint16(55), 678: uint16(56), 679: uint16(57), 680: uint16(273), 681: uint16(143), 682: uint16(193), 683: uint16(118), 684: uint16(143), 685: uint16(102), 686: uint16(103), 687: uint16(104), 688: uint16(105), 689: uint16(106), 690: uint16(107), 691: uint16(108), 692: uint16(109), 693: uint16(110), 694: uint16(111), 695: uint16(112), 696: uint16(113), 697: uint16(76), 698: uint16(118), 699: uint16(59), 700: uint16(241), 701: uint16(116), 702: uint16(117), 703: uint16(118), 704: uint16(304), 705: uint16(216), 706: uint16(217), 707: uint16(292), 708: uint16(143), 709: uint16(60), 710: uint16(89), 711: uint16(241), 712: uint16(19), 713: uint16(92), 714: uint16(193), 715: uint16(193), 716: uint16(23), 717: uint16(22), 718: uint16(311), 719: uint16(312), 720: uint16(231), 721: uint16(101), 722: uint16(22), 723: uint16(143), 724: uint16(102), 725: uint16(103), 726: uint16(104), 727: uint16(105), 728: uint16(106), 729: uint16(107), 730: uint16(108), 731: uint16(109), 732: uint16(110), 733: uint16(111), 734: uint16(112), 735: uint16(113), 736: uint16(43), 737: uint16(44), 738: uint16(45), 739: uint16(46), 740: uint16(47), 741: uint16(48), 742: uint16(49), 743: uint16(50), 744: uint16(51), 745: uint16(52), 746: uint16(53), 747: uint16(54), 748: uint16(55), 749: uint16(56), 750: uint16(57), 751: uint16(19), 752: uint16(193), 753: uint16(193), 754: uint16(59), 755: uint16(23), 756: uint16(116), 757: uint16(117), 758: uint16(118), 759: uint16(59), 760: uint16(201), 761: uint16(21), 762: uint16(241), 763: uint16(304), 764: uint16(193), 765: uint16(206), 766: uint16(127), 767: uint16(128), 768: uint16(129), 769: uint16(193), 770: uint16(128), 771: uint16(129), 772: uint16(235), 773: uint16(236), 774: uint16(304), 775: uint16(43), 776: uint16(44), 777: uint16(45), 778: uint16(46), 779: uint16(47), 780: uint16(48), 781: uint16(49), 782: uint16(50), 783: uint16(51), 784: uint16(52), 785: uint16(53), 786: uint16(54), 787: uint16(55), 788: uint16(56), 789: uint16(57), 790: uint16(22), 791: uint16(193), 792: uint16(216), 793: uint16(217), 794: uint16(193), 795: uint16(102), 796: uint16(103), 797: uint16(104), 798: uint16(105), 799: uint16(106), 800: uint16(107), 801: uint16(108), 802: uint16(109), 803: uint16(110), 804: uint16(111), 805: uint16(112), 806: uint16(113), 807: uint16(231), 808: uint16(193), 809: uint16(193), 810: uint16(193), 811: uint16(116), 812: uint16(117), 813: uint16(118), 814: uint16(216), 815: uint16(217), 816: uint16(116), 817: uint16(117), 818: uint16(118), 819: uint16(226), 820: uint16(80), 821: uint16(193), 822: uint16(19), 823: uint16(235), 824: uint16(236), 825: uint16(304), 826: uint16(23), 827: uint16(211), 828: uint16(212), 829: uint16(231), 830: uint16(204), 831: uint16(216), 832: uint16(217), 833: uint16(205), 834: uint16(102), 835: uint16(103), 836: uint16(104), 837: uint16(105), 838: uint16(106), 839: uint16(107), 840: uint16(108), 841: uint16(109), 842: uint16(110), 843: uint16(111), 844: uint16(112), 845: uint16(113), 846: uint16(43), 847: uint16(44), 848: uint16(45), 849: uint16(46), 850: uint16(47), 851: uint16(48), 852: uint16(49), 853: uint16(50), 854: uint16(51), 855: uint16(52), 856: uint16(53), 857: uint16(54), 858: uint16(55), 859: uint16(56), 860: uint16(57), 861: uint16(19), 862: uint16(193), 863: uint16(123), 864: uint16(76), 865: uint16(239), 866: uint16(240), 867: uint16(193), 868: uint16(253), 869: uint16(239), 870: uint16(240), 871: uint16(239), 872: uint16(240), 873: uint16(244), 874: uint16(106), 875: uint16(107), 876: uint16(193), 877: uint16(89), 878: uint16(252), 879: uint16(193), 880: uint16(92), 881: uint16(59), 882: uint16(252), 883: uint16(254), 884: uint16(252), 885: uint16(43), 886: uint16(44), 887: uint16(45), 888: uint16(46), 889: uint16(47), 890: uint16(48), 891: uint16(49), 892: uint16(50), 893: uint16(51), 894: uint16(52), 895: uint16(53), 896: uint16(54), 897: uint16(55), 898: uint16(56), 899: uint16(57), 900: uint16(284), 901: uint16(161), 902: uint16(216), 903: uint16(217), 904: uint16(193), 905: uint16(102), 906: uint16(103), 907: uint16(104), 908: uint16(105), 909: uint16(106), 910: uint16(107), 911: uint16(108), 912: uint16(109), 913: uint16(110), 914: uint16(111), 915: uint16(112), 916: uint16(113), 917: uint16(231), 918: uint16(193), 919: uint16(244), 920: uint16(187), 921: uint16(188), 922: uint16(189), 923: uint16(190), 924: uint16(7), 925: uint16(8), 926: uint16(9), 927: uint16(309), 928: uint16(195), 929: uint16(254), 930: uint16(197), 931: uint16(313), 932: uint16(19), 933: uint16(127), 934: uint16(128), 935: uint16(129), 936: uint16(262), 937: uint16(204), 938: uint16(22), 939: uint16(117), 940: uint16(24), 941: uint16(216), 942: uint16(217), 943: uint16(273), 944: uint16(102), 945: uint16(103), 946: uint16(104), 947: uint16(105), 948: uint16(106), 949: uint16(107), 950: uint16(108), 951: uint16(109), 952: uint16(110), 953: uint16(111), 954: uint16(112), 955: uint16(113), 956: uint16(43), 957: uint16(44), 958: uint16(45), 959: uint16(46), 960: uint16(47), 961: uint16(48), 962: uint16(49), 963: uint16(50), 964: uint16(51), 965: uint16(52), 966: uint16(53), 967: uint16(54), 968: uint16(55), 969: uint16(56), 970: uint16(57), 971: uint16(193), 972: uint16(239), 973: uint16(240), 974: uint16(193), 975: uint16(59), 976: uint16(19), 977: uint16(188), 978: uint16(253), 979: uint16(190), 980: uint16(193), 981: uint16(311), 982: uint16(312), 983: uint16(16), 984: uint16(195), 985: uint16(252), 986: uint16(197), 987: uint16(193), 988: uint16(19), 989: uint16(301), 990: uint16(302), 991: uint16(135), 992: uint16(193), 993: uint16(204), 994: uint16(216), 995: uint16(217), 996: uint16(140), 997: uint16(216), 998: uint16(217), 999: uint16(266), 1000: uint16(204), 1001: uint16(159), 1002: uint16(45), 1003: uint16(46), 1004: uint16(47), 1005: uint16(48), 1006: uint16(49), 1007: uint16(50), 1008: uint16(51), 1009: uint16(52), 1010: uint16(53), 1011: uint16(54), 1012: uint16(55), 1013: uint16(56), 1014: uint16(57), 1015: uint16(102), 1016: uint16(103), 1017: uint16(104), 1018: uint16(105), 1019: uint16(106), 1020: uint16(107), 1021: uint16(108), 1022: uint16(109), 1023: uint16(110), 1024: uint16(111), 1025: uint16(112), 1026: uint16(113), 1027: uint16(12), 1028: uint16(239), 1029: uint16(240), 1030: uint16(193), 1031: uint16(298), 1032: uint16(238), 1033: uint16(117), 1034: uint16(253), 1035: uint16(239), 1036: uint16(240), 1037: uint16(238), 1038: uint16(259), 1039: uint16(260), 1040: uint16(193), 1041: uint16(252), 1042: uint16(27), 1043: uint16(193), 1044: uint16(77), 1045: uint16(193), 1046: uint16(79), 1047: uint16(204), 1048: uint16(252), 1049: uint16(262), 1050: uint16(193), 1051: uint16(299), 1052: uint16(300), 1053: uint16(193), 1054: uint16(100), 1055: uint16(266), 1056: uint16(278), 1057: uint16(42), 1058: uint16(204), 1059: uint16(102), 1060: uint16(103), 1061: uint16(104), 1062: uint16(105), 1063: uint16(106), 1064: uint16(107), 1065: uint16(108), 1066: uint16(109), 1067: uint16(110), 1068: uint16(111), 1069: uint16(112), 1070: uint16(113), 1071: uint16(117), 1072: uint16(159), 1073: uint16(216), 1074: uint16(217), 1075: uint16(121), 1076: uint16(216), 1077: uint16(217), 1078: uint16(63), 1079: uint16(193), 1080: uint16(193), 1081: uint16(193), 1082: uint16(239), 1083: uint16(240), 1084: uint16(115), 1085: uint16(116), 1086: uint16(193), 1087: uint16(298), 1088: uint16(73), 1089: uint16(240), 1090: uint16(238), 1091: uint16(231), 1092: uint16(19), 1093: uint16(239), 1094: uint16(240), 1095: uint16(252), 1096: uint16(22), 1097: uint16(24), 1098: uint16(211), 1099: uint16(212), 1100: uint16(263), 1101: uint16(252), 1102: uint16(216), 1103: uint16(217), 1104: uint16(216), 1105: uint16(217), 1106: uint16(252), 1107: uint16(153), 1108: uint16(154), 1109: uint16(155), 1110: uint16(253), 1111: uint16(193), 1112: uint16(19), 1113: uint16(144), 1114: uint16(213), 1115: uint16(268), 1116: uint16(43), 1117: uint16(44), 1118: uint16(45), 1119: uint16(46), 1120: uint16(47), 1121: uint16(48), 1122: uint16(49), 1123: uint16(50), 1124: uint16(51), 1125: uint16(52), 1126: uint16(53), 1127: uint16(54), 1128: uint16(55), 1129: uint16(56), 1130: uint16(57), 1131: uint16(193), 1132: uint16(19), 1133: uint16(59), 1134: uint16(216), 1135: uint16(217), 1136: uint16(43), 1137: uint16(44), 1138: uint16(45), 1139: uint16(46), 1140: uint16(47), 1141: uint16(48), 1142: uint16(49), 1143: uint16(50), 1144: uint16(51), 1145: uint16(52), 1146: uint16(53), 1147: uint16(54), 1148: uint16(55), 1149: uint16(56), 1150: uint16(57), 1151: uint16(193), 1152: uint16(19), 1153: uint16(24), 1154: uint16(216), 1155: uint16(217), 1156: uint16(43), 1157: uint16(44), 1158: uint16(45), 1159: uint16(46), 1160: uint16(47), 1161: uint16(48), 1162: uint16(49), 1163: uint16(50), 1164: uint16(51), 1165: uint16(52), 1166: uint16(53), 1167: uint16(54), 1168: uint16(55), 1169: uint16(56), 1170: uint16(57), 1171: uint16(284), 1172: uint16(193), 1173: uint16(208), 1174: uint16(209), 1175: uint16(102), 1176: uint16(103), 1177: uint16(104), 1178: uint16(105), 1179: uint16(106), 1180: uint16(107), 1181: uint16(108), 1182: uint16(109), 1183: uint16(110), 1184: uint16(111), 1185: uint16(112), 1186: uint16(113), 1187: uint16(286), 1188: uint16(59), 1189: uint16(193), 1190: uint16(232), 1191: uint16(117), 1192: uint16(291), 1193: uint16(193), 1194: uint16(193), 1195: uint16(102), 1196: uint16(103), 1197: uint16(104), 1198: uint16(105), 1199: uint16(106), 1200: uint16(107), 1201: uint16(108), 1202: uint16(109), 1203: uint16(110), 1204: uint16(111), 1205: uint16(112), 1206: uint16(113), 1207: uint16(193), 1208: uint16(204), 1209: uint16(22), 1210: uint16(23), 1211: uint16(193), 1212: uint16(25), 1213: uint16(66), 1214: uint16(193), 1215: uint16(102), 1216: uint16(103), 1217: uint16(104), 1218: uint16(105), 1219: uint16(106), 1220: uint16(107), 1221: uint16(108), 1222: uint16(109), 1223: uint16(110), 1224: uint16(111), 1225: uint16(112), 1226: uint16(113), 1227: uint16(193), 1228: uint16(193), 1229: uint16(193), 1230: uint16(216), 1231: uint16(217), 1232: uint16(85), 1233: uint16(193), 1234: uint16(238), 1235: uint16(19), 1236: uint16(16), 1237: uint16(216), 1238: uint16(217), 1239: uint16(238), 1240: uint16(193), 1241: uint16(94), 1242: uint16(193), 1243: uint16(239), 1244: uint16(240), 1245: uint16(231), 1246: uint16(117), 1247: uint16(268), 1248: uint16(35), 1249: uint16(116), 1250: uint16(216), 1251: uint16(217), 1252: uint16(216), 1253: uint16(217), 1254: uint16(22), 1255: uint16(23), 1256: uint16(252), 1257: uint16(25), 1258: uint16(208), 1259: uint16(209), 1260: uint16(44), 1261: uint16(45), 1262: uint16(46), 1263: uint16(47), 1264: uint16(48), 1265: uint16(49), 1266: uint16(50), 1267: uint16(51), 1268: uint16(52), 1269: uint16(53), 1270: uint16(54), 1271: uint16(55), 1272: uint16(56), 1273: uint16(57), 1274: uint16(193), 1275: uint16(193), 1276: uint16(19), 1277: uint16(5), 1278: uint16(59), 1279: uint16(66), 1280: uint16(193), 1281: uint16(263), 1282: uint16(10), 1283: uint16(11), 1284: uint16(12), 1285: uint16(13), 1286: uint16(14), 1287: uint16(74), 1288: uint16(101), 1289: uint16(17), 1290: uint16(193), 1291: uint16(46), 1292: uint16(193), 1293: uint16(146), 1294: uint16(193), 1295: uint16(76), 1296: uint16(213), 1297: uint16(77), 1298: uint16(263), 1299: uint16(79), 1300: uint16(12), 1301: uint16(260), 1302: uint16(30), 1303: uint16(46), 1304: uint16(32), 1305: uint16(264), 1306: uint16(87), 1307: uint16(193), 1308: uint16(89), 1309: uint16(29), 1310: uint16(263), 1311: uint16(92), 1312: uint16(40), 1313: uint16(33), 1314: uint16(232), 1315: uint16(27), 1316: uint16(193), 1317: uint16(108), 1318: uint16(102), 1319: uint16(103), 1320: uint16(104), 1321: uint16(105), 1322: uint16(106), 1323: uint16(107), 1324: uint16(108), 1325: uint16(109), 1326: uint16(110), 1327: uint16(111), 1328: uint16(112), 1329: uint16(113), 1330: uint16(42), 1331: uint16(138), 1332: uint16(139), 1333: uint16(101), 1334: uint16(193), 1335: uint16(116), 1336: uint16(117), 1337: uint16(118), 1338: uint16(19), 1339: uint16(20), 1340: uint16(255), 1341: uint16(22), 1342: uint16(70), 1343: uint16(130), 1344: uint16(135), 1345: uint16(65), 1346: uint16(256), 1347: uint16(257), 1348: uint16(193), 1349: uint16(140), 1350: uint16(78), 1351: uint16(63), 1352: uint16(193), 1353: uint16(81), 1354: uint16(193), 1355: uint16(36), 1356: uint16(193), 1357: uint16(216), 1358: uint16(217), 1359: uint16(193), 1360: uint16(115), 1361: uint16(193), 1362: uint16(263), 1363: uint16(193), 1364: uint16(145), 1365: uint16(268), 1366: uint16(59), 1367: uint16(48), 1368: uint16(193), 1369: uint16(193), 1370: uint16(98), 1371: uint16(193), 1372: uint16(115), 1373: uint16(193), 1374: uint16(291), 1375: uint16(216), 1376: uint16(217), 1377: uint16(193), 1378: uint16(59), 1379: uint16(216), 1380: uint16(217), 1381: uint16(161), 1382: uint16(216), 1383: uint16(217), 1384: uint16(216), 1385: uint16(217), 1386: uint16(216), 1387: uint16(217), 1388: uint16(131), 1389: uint16(193), 1390: uint16(71), 1391: uint16(193), 1392: uint16(216), 1393: uint16(217), 1394: uint16(216), 1395: uint16(217), 1396: uint16(216), 1397: uint16(217), 1398: uint16(193), 1399: uint16(260), 1400: uint16(216), 1401: uint16(217), 1402: uint16(19), 1403: uint16(264), 1404: uint16(85), 1405: uint16(133), 1406: uint16(244), 1407: uint16(100), 1408: uint16(193), 1409: uint16(90), 1410: uint16(138), 1411: uint16(139), 1412: uint16(216), 1413: uint16(217), 1414: uint16(216), 1415: uint16(217), 1416: uint16(254), 1417: uint16(244), 1418: uint16(193), 1419: uint16(100), 1420: uint16(193), 1421: uint16(216), 1422: uint16(217), 1423: uint16(116), 1424: uint16(117), 1425: uint16(106), 1426: uint16(107), 1427: uint16(254), 1428: uint16(121), 1429: uint16(193), 1430: uint16(115), 1431: uint16(216), 1432: uint16(217), 1433: uint16(114), 1434: uint16(162), 1435: uint16(116), 1436: uint16(117), 1437: uint16(118), 1438: uint16(115), 1439: uint16(244), 1440: uint16(121), 1441: uint16(216), 1442: uint16(217), 1443: uint16(216), 1444: uint16(217), 1445: uint16(193), 1446: uint16(309), 1447: uint16(193), 1448: uint16(31), 1449: uint16(254), 1450: uint16(313), 1451: uint16(309), 1452: uint16(216), 1453: uint16(217), 1454: uint16(309), 1455: uint16(313), 1456: uint16(39), 1457: uint16(193), 1458: uint16(313), 1459: uint16(309), 1460: uint16(153), 1461: uint16(154), 1462: uint16(155), 1463: uint16(313), 1464: uint16(193), 1465: uint16(150), 1466: uint16(25), 1467: uint16(144), 1468: uint16(216), 1469: uint16(217), 1470: uint16(216), 1471: uint16(217), 1472: uint16(153), 1473: uint16(154), 1474: uint16(155), 1475: uint16(156), 1476: uint16(157), 1478: uint16(1), 1479: uint16(2), 1480: uint16(216), 1481: uint16(217), 1482: uint16(5), 1483: uint16(149), 1484: uint16(150), 1485: uint16(22), 1486: uint16(193), 1487: uint16(10), 1488: uint16(11), 1489: uint16(12), 1490: uint16(13), 1491: uint16(14), 1492: uint16(193), 1493: uint16(158), 1494: uint16(17), 1495: uint16(160), 1496: uint16(193), 1497: uint16(19), 1498: uint16(20), 1499: uint16(116), 1500: uint16(22), 1501: uint16(25), 1502: uint16(193), 1503: uint16(24), 1504: uint16(22), 1505: uint16(193), 1506: uint16(24), 1507: uint16(30), 1508: uint16(226), 1509: uint16(32), 1510: uint16(19), 1511: uint16(20), 1512: uint16(226), 1513: uint16(22), 1514: uint16(36), 1515: uint16(193), 1516: uint16(53), 1517: uint16(40), 1518: uint16(193), 1519: uint16(216), 1520: uint16(217), 1521: uint16(193), 1522: uint16(23), 1523: uint16(193), 1524: uint16(25), 1525: uint16(216), 1526: uint16(217), 1527: uint16(36), 1528: uint16(216), 1529: uint16(217), 1530: uint16(193), 1531: uint16(99), 1532: uint16(193), 1533: uint16(193), 1534: uint16(22), 1535: uint16(193), 1536: uint16(193), 1537: uint16(59), 1538: uint16(216), 1539: uint16(217), 1540: uint16(193), 1541: uint16(216), 1542: uint16(217), 1543: uint16(193), 1544: uint16(216), 1545: uint16(217), 1546: uint16(193), 1547: uint16(70), 1548: uint16(129), 1549: uint16(71), 1550: uint16(59), 1551: uint16(129), 1552: uint16(193), 1553: uint16(216), 1554: uint16(217), 1555: uint16(78), 1556: uint16(216), 1557: uint16(217), 1558: uint16(81), 1559: uint16(216), 1560: uint16(217), 1561: uint16(193), 1562: uint16(71), 1563: uint16(85), 1564: uint16(193), 1565: uint16(133), 1566: uint16(193), 1567: uint16(126), 1568: uint16(90), 1569: uint16(216), 1570: uint16(217), 1571: uint16(152), 1572: uint16(258), 1573: uint16(61), 1574: uint16(152), 1575: uint16(98), 1576: uint16(85), 1577: uint16(193), 1578: uint16(100), 1579: uint16(193), 1580: uint16(23), 1581: uint16(90), 1582: uint16(25), 1583: uint16(121), 1584: uint16(106), 1585: uint16(107), 1586: uint16(23), 1587: uint16(216), 1588: uint16(217), 1589: uint16(216), 1590: uint16(217), 1591: uint16(100), 1592: uint16(114), 1593: uint16(131), 1594: uint16(116), 1595: uint16(117), 1596: uint16(118), 1597: uint16(106), 1598: uint16(107), 1599: uint16(121), 1600: uint16(216), 1601: uint16(217), 1602: uint16(216), 1603: uint16(217), 1604: uint16(193), 1605: uint16(114), 1606: uint16(193), 1607: uint16(116), 1608: uint16(117), 1609: uint16(118), 1610: uint16(133), 1611: uint16(22), 1612: uint16(121), 1613: uint16(193), 1614: uint16(59), 1615: uint16(138), 1616: uint16(139), 1617: uint16(193), 1618: uint16(142), 1619: uint16(193), 1620: uint16(141), 1621: uint16(23), 1622: uint16(23), 1623: uint16(25), 1624: uint16(25), 1625: uint16(120), 1626: uint16(121), 1627: uint16(216), 1628: uint16(217), 1629: uint16(216), 1630: uint16(217), 1631: uint16(153), 1632: uint16(154), 1633: uint16(155), 1634: uint16(156), 1635: uint16(157), 1636: uint16(216), 1637: uint16(217), 1638: uint16(19), 1639: uint16(162), 1640: uint16(216), 1641: uint16(217), 1642: uint16(216), 1643: uint16(217), 1644: uint16(153), 1645: uint16(154), 1646: uint16(155), 1647: uint16(156), 1648: uint16(157), 1649: uint16(1), 1650: uint16(2), 1651: uint16(193), 1652: uint16(59), 1653: uint16(5), 1654: uint16(19), 1655: uint16(20), 1656: uint16(318), 1657: uint16(22), 1658: uint16(10), 1659: uint16(11), 1660: uint16(12), 1661: uint16(13), 1662: uint16(14), 1663: uint16(193), 1664: uint16(59), 1665: uint16(17), 1666: uint16(193), 1667: uint16(23), 1668: uint16(23), 1669: uint16(25), 1670: uint16(25), 1671: uint16(36), 1672: uint16(117), 1673: uint16(193), 1674: uint16(216), 1675: uint16(217), 1676: uint16(193), 1677: uint16(23), 1678: uint16(30), 1679: uint16(25), 1680: uint16(32), 1681: uint16(19), 1682: uint16(20), 1683: uint16(23), 1684: uint16(22), 1685: uint16(25), 1686: uint16(216), 1687: uint16(217), 1688: uint16(40), 1689: uint16(216), 1690: uint16(217), 1691: uint16(7), 1692: uint16(8), 1693: uint16(23), 1694: uint16(59), 1695: uint16(25), 1696: uint16(83), 1697: uint16(84), 1698: uint16(36), 1699: uint16(23), 1700: uint16(193), 1701: uint16(25), 1702: uint16(23), 1703: uint16(23), 1704: uint16(25), 1705: uint16(25), 1706: uint16(71), 1707: uint16(153), 1708: uint16(145), 1709: uint16(155), 1710: uint16(117), 1711: uint16(153), 1712: uint16(23), 1713: uint16(155), 1714: uint16(25), 1715: uint16(23), 1716: uint16(97), 1717: uint16(25), 1718: uint16(70), 1719: uint16(193), 1720: uint16(193), 1721: uint16(59), 1722: uint16(117), 1723: uint16(236), 1724: uint16(193), 1725: uint16(193), 1726: uint16(78), 1727: uint16(193), 1728: uint16(193), 1729: uint16(81), 1730: uint16(141), 1731: uint16(193), 1732: uint16(193), 1733: uint16(71), 1734: uint16(193), 1735: uint16(100), 1736: uint16(288), 1737: uint16(287), 1738: uint16(242), 1739: uint16(255), 1740: uint16(255), 1741: uint16(106), 1742: uint16(107), 1743: uint16(108), 1744: uint16(255), 1745: uint16(255), 1746: uint16(98), 1747: uint16(243), 1748: uint16(297), 1749: uint16(114), 1750: uint16(214), 1751: uint16(116), 1752: uint16(117), 1753: uint16(118), 1754: uint16(245), 1755: uint16(191), 1756: uint16(121), 1757: uint16(271), 1758: uint16(293), 1759: uint16(267), 1760: uint16(267), 1761: uint16(246), 1762: uint16(100), 1763: uint16(246), 1764: uint16(245), 1765: uint16(271), 1766: uint16(271), 1767: uint16(293), 1768: uint16(106), 1769: uint16(107), 1770: uint16(220), 1771: uint16(271), 1772: uint16(229), 1773: uint16(225), 1774: uint16(249), 1775: uint16(219), 1776: uint16(114), 1777: uint16(259), 1778: uint16(116), 1779: uint16(117), 1780: uint16(118), 1781: uint16(133), 1782: uint16(259), 1783: uint16(121), 1784: uint16(219), 1785: uint16(219), 1786: uint16(138), 1787: uint16(139), 1788: uint16(153), 1789: uint16(154), 1790: uint16(155), 1791: uint16(156), 1792: uint16(157), 1793: uint16(280), 1794: uint16(249), 1795: uint16(243), 1796: uint16(19), 1797: uint16(20), 1798: uint16(245), 1799: uint16(22), 1800: uint16(196), 1801: uint16(259), 1802: uint16(140), 1803: uint16(259), 1804: uint16(60), 1805: uint16(297), 1806: uint16(141), 1807: uint16(297), 1808: uint16(200), 1809: uint16(200), 1810: uint16(162), 1811: uint16(38), 1812: uint16(200), 1813: uint16(36), 1814: uint16(294), 1815: uint16(153), 1816: uint16(154), 1817: uint16(155), 1818: uint16(156), 1819: uint16(157), 1820: uint16(151), 1821: uint16(150), 1822: uint16(294), 1823: uint16(283), 1824: uint16(22), 1825: uint16(43), 1826: uint16(234), 1827: uint16(18), 1828: uint16(237), 1829: uint16(200), 1830: uint16(270), 1831: uint16(272), 1832: uint16(237), 1833: uint16(237), 1834: uint16(237), 1835: uint16(18), 1836: uint16(59), 1837: uint16(199), 1838: uint16(270), 1839: uint16(149), 1840: uint16(246), 1841: uint16(272), 1842: uint16(272), 1843: uint16(200), 1844: uint16(234), 1845: uint16(234), 1846: uint16(246), 1847: uint16(246), 1848: uint16(71), 1849: uint16(246), 1850: uint16(199), 1851: uint16(158), 1852: uint16(290), 1853: uint16(62), 1854: uint16(22), 1855: uint16(200), 1856: uint16(19), 1857: uint16(20), 1858: uint16(199), 1859: uint16(22), 1860: uint16(289), 1861: uint16(221), 1862: uint16(221), 1863: uint16(200), 1864: uint16(200), 1865: uint16(199), 1866: uint16(199), 1867: uint16(115), 1868: uint16(218), 1869: uint16(64), 1870: uint16(218), 1871: uint16(218), 1872: uint16(22), 1873: uint16(36), 1874: uint16(227), 1875: uint16(126), 1876: uint16(227), 1877: uint16(100), 1878: uint16(165), 1879: uint16(221), 1880: uint16(224), 1881: uint16(224), 1882: uint16(24), 1883: uint16(106), 1884: uint16(107), 1885: uint16(312), 1886: uint16(218), 1887: uint16(305), 1888: uint16(113), 1889: uint16(282), 1890: uint16(91), 1891: uint16(114), 1892: uint16(220), 1893: uint16(116), 1894: uint16(117), 1895: uint16(118), 1896: uint16(59), 1897: uint16(282), 1898: uint16(121), 1899: uint16(218), 1900: uint16(218), 1901: uint16(218), 1902: uint16(200), 1903: uint16(317), 1904: uint16(317), 1905: uint16(82), 1906: uint16(221), 1907: uint16(265), 1908: uint16(71), 1909: uint16(148), 1910: uint16(145), 1911: uint16(265), 1912: uint16(22), 1913: uint16(277), 1914: uint16(200), 1915: uint16(158), 1916: uint16(279), 1917: uint16(140), 1918: uint16(147), 1919: uint16(25), 1920: uint16(146), 1921: uint16(202), 1922: uint16(248), 1923: uint16(250), 1924: uint16(249), 1925: uint16(247), 1926: uint16(13), 1927: uint16(250), 1928: uint16(194), 1929: uint16(194), 1930: uint16(153), 1931: uint16(154), 1932: uint16(155), 1933: uint16(156), 1934: uint16(157), 1935: uint16(6), 1936: uint16(303), 1937: uint16(100), 1938: uint16(192), 1939: uint16(192), 1940: uint16(246), 1941: uint16(213), 1942: uint16(192), 1943: uint16(106), 1944: uint16(107), 1945: uint16(207), 1946: uint16(213), 1947: uint16(207), 1948: uint16(222), 1949: uint16(213), 1950: uint16(213), 1951: uint16(114), 1952: uint16(222), 1953: uint16(116), 1954: uint16(117), 1955: uint16(118), 1956: uint16(214), 1957: uint16(214), 1958: uint16(121), 1959: uint16(4), 1960: uint16(207), 1961: uint16(213), 1962: uint16(3), 1963: uint16(22), 1964: uint16(303), 1965: uint16(15), 1966: uint16(163), 1967: uint16(16), 1968: uint16(23), 1969: uint16(23), 1970: uint16(139), 1971: uint16(151), 1972: uint16(130), 1973: uint16(25), 1974: uint16(20), 1975: uint16(142), 1976: uint16(24), 1977: uint16(16), 1978: uint16(144), 1979: uint16(1), 1980: uint16(142), 1981: uint16(130), 1982: uint16(130), 1983: uint16(61), 1984: uint16(37), 1985: uint16(53), 1986: uint16(300), 1987: uint16(151), 1988: uint16(53), 1989: uint16(53), 1990: uint16(153), 1991: uint16(154), 1992: uint16(155), 1993: uint16(156), 1994: uint16(157), 1995: uint16(53), 1996: uint16(130), 1997: uint16(116), 1998: uint16(34), 1999: uint16(1), 2000: uint16(141), 2001: uint16(5), 2002: uint16(22), 2003: uint16(115), 2004: uint16(161), 2005: uint16(68), 2006: uint16(25), 2007: uint16(68), 2008: uint16(75), 2009: uint16(41), 2010: uint16(141), 2011: uint16(115), 2012: uint16(24), 2013: uint16(20), 2014: uint16(19), 2015: uint16(131), 2016: uint16(125), 2017: uint16(23), 2018: uint16(28), 2019: uint16(22), 2020: uint16(67), 2021: uint16(22), 2022: uint16(22), 2023: uint16(22), 2024: uint16(67), 2025: uint16(59), 2026: uint16(24), 2027: uint16(96), 2028: uint16(22), 2029: uint16(67), 2030: uint16(23), 2031: uint16(149), 2032: uint16(22), 2033: uint16(25), 2034: uint16(23), 2035: uint16(23), 2036: uint16(23), 2037: uint16(22), 2038: uint16(34), 2039: uint16(141), 2040: uint16(37), 2041: uint16(97), 2042: uint16(23), 2043: uint16(23), 2044: uint16(116), 2045: uint16(22), 2046: uint16(143), 2047: uint16(25), 2048: uint16(34), 2049: uint16(75), 2050: uint16(34), 2051: uint16(34), 2052: uint16(34), 2053: uint16(88), 2054: uint16(75), 2055: uint16(34), 2056: uint16(86), 2057: uint16(23), 2058: uint16(22), 2059: uint16(34), 2060: uint16(93), 2061: uint16(24), 2062: uint16(34), 2063: uint16(25), 2064: uint16(25), 2065: uint16(142), 2066: uint16(142), 2067: uint16(23), 2068: uint16(44), 2069: uint16(23), 2070: uint16(23), 2071: uint16(23), 2072: uint16(23), 2073: uint16(11), 2074: uint16(23), 2075: uint16(25), 2076: uint16(22), 2077: uint16(22), 2078: uint16(22), 2079: uint16(141), 2080: uint16(23), 2081: uint16(23), 2082: uint16(22), 2083: uint16(22), 2084: uint16(25), 2085: uint16(15), 2086: uint16(1), 2087: uint16(23), 2088: uint16(25), 2089: uint16(1), 2090: uint16(141), 2091: uint16(135), 2092: uint16(319), 2093: uint16(319), 2094: uint16(319), 2095: uint16(319), 2096: uint16(319), 2097: uint16(319), 2098: uint16(319), 2099: uint16(141), 2100: uint16(319), 2101: uint16(319), 2102: uint16(319), 2103: uint16(319), 2104: uint16(319), 2105: uint16(319), 2106: uint16(319), 2107: uint16(319), 2108: uint16(319), 2109: uint16(319), 2110: uint16(319), 2111: uint16(319), 2112: uint16(319), 2113: uint16(319), 2114: uint16(319), 2115: uint16(319), 2116: uint16(319), 2117: uint16(319), 2118: uint16(319), 2119: uint16(319), 2120: uint16(319), 2121: uint16(319), 2122: uint16(319), 2123: uint16(319), 2124: uint16(319), 2125: uint16(319), 2126: uint16(319), 2127: uint16(319), 2128: uint16(319), 2129: uint16(319), 2130: uint16(319), 2131: uint16(319), 2132: uint16(319), 2133: uint16(319), 2134: uint16(319), 2135: uint16(319), 2136: uint16(319), 2137: uint16(319), 2138: uint16(319), 2139: uint16(319), 2140: uint16(319), 2141: uint16(319), 2142: uint16(319), 2143: uint16(319), 2144: uint16(319), 2145: uint16(319), 2146: uint16(319), 2147: uint16(319), 2148: uint16(319), 2149: uint16(319), 2150: uint16(319), 2151: uint16(319), 2152: uint16(319), 2153: uint16(319), 2154: uint16(319), 2155: uint16(319), 2156: uint16(319), 2157: uint16(319), 2158: uint16(319), 2159: uint16(319), 2160: uint16(319), 2161: uint16(319), 2162: uint16(319), 2163: uint16(319), 2164: uint16(319), 2165: uint16(319), 2166: uint16(319), 2167: uint16(319), 2168: uint16(319), 2169: uint16(319), 2170: uint16(319), 2171: uint16(319), 2172: uint16(319), 2173: uint16(319), 2174: uint16(319), 2175: uint16(319), 2176: uint16(319), 2177: uint16(319), 2178: uint16(319), 2179: uint16(319), 2180: uint16(319), 2181: uint16(319), 2182: uint16(319), 2183: uint16(319), 2184: uint16(319), 2185: uint16(319), 2186: uint16(319), 2187: uint16(319), 2188: uint16(319), 2189: uint16(319), 2190: uint16(319), 2191: uint16(319), 2192: uint16(319), 2193: uint16(319), 2194: uint16(319), 2195: uint16(319), 2196: uint16(319), 2197: uint16(319), 2198: uint16(319), 2199: uint16(319), 2200: uint16(319), 2201: uint16(319), 2202: uint16(319), 2203: uint16(319), 2204: uint16(319), 2205: uint16(319), 2206: uint16(319), 2207: uint16(319), 2208: uint16(319), 2209: uint16(319), 2210: uint16(319), 2211: uint16(319), 2212: uint16(319), 2213: uint16(319), 2214: uint16(319), 2215: uint16(319), 2216: uint16(319), 2217: uint16(319), 2218: uint16(319), 2219: uint16(319), 2220: uint16(319), 2221: uint16(319), 2222: uint16(319), 2223: uint16(319), 2224: uint16(319), 2225: uint16(319), 2226: uint16(319), 2227: uint16(319), 2228: uint16(319), 2229: uint16(319), 2230: uint16(319), 2231: uint16(319), 2232: uint16(319), 2233: uint16(319), 2234: uint16(319), 2235: uint16(319), 2236: uint16(319), 2237: uint16(319), 2238: uint16(319), 2239: uint16(319), 2240: uint16(319), 2241: uint16(319), 2242: uint16(319), 2243: uint16(319), 2244: uint16(319), 2245: uint16(319), 2246: uint16(319), 2247: uint16(319), 2248: uint16(319), 2249: uint16(319), 2250: uint16(319), 2251: uint16(319), 2252: uint16(319), 2253: uint16(319), 2254: uint16(319), 2255: uint16(319), 2256: uint16(319), 2257: uint16(319), 2258: uint16(319), 2259: uint16(319), 2260: uint16(319), 2261: uint16(319), 2262: uint16(319), 2263: uint16(319), 2264: uint16(319), 2265: uint16(319), 2266: uint16(319), 2267: uint16(319), 2268: uint16(319), 2269: uint16(319), 2270: uint16(319), 2271: uint16(319), 2272: uint16(319), 2273: uint16(319), 2274: uint16(319), 2275: uint16(319), 2276: uint16(319), 2277: uint16(319), 2278: uint16(319), 2279: uint16(319), 2280: uint16(319), 2281: uint16(319), 2282: uint16(319), 2283: uint16(319), 2284: uint16(319), } var _yy_shift_ofst = [579]uint16{ 0: uint16(1648), 1: uint16(1477), 2: uint16(1272), 3: uint16(322), 4: uint16(322), 5: uint16(1), 6: uint16(1319), 7: uint16(1478), 8: uint16(1491), 9: uint16(1837), 10: uint16(1837), 11: uint16(1837), 12: uint16(471), 15: uint16(214), 16: uint16(1093), 17: uint16(1837), 18: uint16(1837), 19: uint16(1837), 20: uint16(1837), 21: uint16(1837), 22: uint16(1837), 23: uint16(1837), 24: uint16(1837), 25: uint16(1837), 26: uint16(1837), 27: uint16(1837), 28: uint16(1837), 29: uint16(1837), 30: uint16(1837), 31: uint16(271), 32: uint16(271), 33: uint16(1219), 34: uint16(1219), 35: uint16(216), 36: uint16(88), 37: uint16(1), 38: uint16(1), 39: uint16(1), 40: uint16(1), 41: uint16(1), 42: uint16(40), 43: uint16(111), 44: uint16(258), 45: uint16(361), 46: uint16(469), 47: uint16(512), 48: uint16(583), 49: uint16(622), 50: uint16(693), 51: uint16(732), 52: uint16(803), 53: uint16(842), 54: uint16(913), 55: uint16(1073), 56: uint16(1093), 57: uint16(1093), 58: uint16(1093), 59: uint16(1093), 60: uint16(1093), 61: uint16(1093), 62: uint16(1093), 63: uint16(1093), 64: uint16(1093), 65: uint16(1093), 66: uint16(1093), 67: uint16(1093), 68: uint16(1093), 69: uint16(1093), 70: uint16(1093), 71: uint16(1093), 72: uint16(1093), 73: uint16(1093), 74: uint16(1113), 75: uint16(1093), 76: uint16(1216), 77: uint16(957), 78: uint16(957), 79: uint16(1635), 80: uint16(1662), 81: uint16(1777), 82: uint16(1837), 83: uint16(1837), 84: uint16(1837), 85: uint16(1837), 86: uint16(1837), 87: uint16(1837), 88: uint16(1837), 89: uint16(1837), 90: uint16(1837), 91: uint16(1837), 92: uint16(1837), 93: uint16(1837), 94: uint16(1837), 95: uint16(1837), 96: uint16(1837), 97: uint16(1837), 98: uint16(1837), 99: uint16(1837), 100: uint16(1837), 101: uint16(1837), 102: uint16(1837), 103: uint16(1837), 104: uint16(1837), 105: uint16(1837), 106: uint16(1837), 107: uint16(1837), 108: uint16(1837), 109: uint16(1837), 110: uint16(1837), 111: uint16(1837), 112: uint16(1837), 113: uint16(1837), 114: uint16(1837), 115: uint16(1837), 116: uint16(1837), 117: uint16(1837), 118: uint16(1837), 119: uint16(1837), 120: uint16(1837), 121: uint16(1837), 122: uint16(1837), 123: uint16(1837), 124: uint16(1837), 125: uint16(1837), 126: uint16(1837), 127: uint16(1837), 128: uint16(1837), 129: uint16(1837), 130: uint16(1837), 131: uint16(137), 132: uint16(181), 133: uint16(181), 134: uint16(181), 135: uint16(181), 136: uint16(181), 137: uint16(181), 138: uint16(181), 139: uint16(94), 140: uint16(430), 141: uint16(66), 142: uint16(65), 143: uint16(112), 144: uint16(366), 145: uint16(533), 146: uint16(533), 147: uint16(740), 148: uint16(1257), 149: uint16(533), 150: uint16(533), 151: uint16(79), 152: uint16(79), 153: uint16(533), 154: uint16(412), 155: uint16(412), 156: uint16(412), 157: uint16(77), 158: uint16(412), 159: uint16(123), 160: uint16(113), 161: uint16(113), 162: uint16(113), 163: uint16(22), 164: uint16(22), 165: uint16(2100), 166: uint16(2100), 167: uint16(328), 168: uint16(328), 169: uint16(328), 170: uint16(239), 171: uint16(468), 172: uint16(468), 173: uint16(468), 174: uint16(468), 175: uint16(1015), 176: uint16(1015), 177: uint16(409), 178: uint16(366), 179: uint16(1187), 180: uint16(1232), 181: uint16(533), 182: uint16(533), 183: uint16(533), 184: uint16(533), 185: uint16(533), 186: uint16(533), 187: uint16(533), 188: uint16(533), 189: uint16(533), 190: uint16(533), 191: uint16(533), 192: uint16(533), 193: uint16(533), 194: uint16(533), 195: uint16(533), 196: uint16(533), 197: uint16(533), 198: uint16(533), 199: uint16(533), 200: uint16(533), 201: uint16(969), 202: uint16(621), 203: uint16(621), 204: uint16(533), 205: uint16(642), 206: uint16(788), 207: uint16(788), 208: uint16(1133), 209: uint16(1133), 210: uint16(822), 211: uint16(822), 212: uint16(67), 213: uint16(1193), 214: uint16(2100), 215: uint16(2100), 216: uint16(2100), 217: uint16(2100), 218: uint16(2100), 219: uint16(2100), 220: uint16(2100), 221: uint16(1307), 222: uint16(954), 223: uint16(954), 224: uint16(585), 225: uint16(472), 226: uint16(640), 227: uint16(387), 228: uint16(695), 229: uint16(538), 230: uint16(541), 231: uint16(700), 232: uint16(533), 233: uint16(533), 234: uint16(533), 235: uint16(533), 236: uint16(533), 237: uint16(533), 238: uint16(533), 239: uint16(533), 240: uint16(533), 241: uint16(533), 242: uint16(222), 243: uint16(533), 244: uint16(533), 245: uint16(533), 246: uint16(533), 247: uint16(533), 248: uint16(533), 249: uint16(533), 250: uint16(533), 251: uint16(533), 252: uint16(533), 253: uint16(533), 254: uint16(533), 255: uint16(1213), 256: uint16(1213), 257: uint16(1213), 258: uint16(533), 259: uint16(533), 260: uint16(533), 261: uint16(565), 262: uint16(533), 263: uint16(533), 264: uint16(533), 265: uint16(916), 266: uint16(1147), 267: uint16(533), 268: uint16(533), 269: uint16(1288), 270: uint16(533), 271: uint16(533), 272: uint16(533), 273: uint16(533), 274: uint16(533), 275: uint16(533), 276: uint16(533), 277: uint16(533), 278: uint16(639), 279: uint16(1280), 280: uint16(209), 281: uint16(1129), 282: uint16(1129), 283: uint16(1129), 284: uint16(1129), 285: uint16(580), 286: uint16(209), 287: uint16(209), 288: uint16(1209), 289: uint16(768), 290: uint16(917), 291: uint16(649), 292: uint16(1315), 293: uint16(1334), 294: uint16(405), 295: uint16(1334), 296: uint16(1383), 297: uint16(249), 298: uint16(1315), 299: uint16(1315), 300: uint16(249), 301: uint16(1315), 302: uint16(405), 303: uint16(1383), 304: uint16(1441), 305: uint16(464), 306: uint16(1245), 307: uint16(1417), 308: uint16(1417), 309: uint16(1417), 310: uint16(1323), 311: uint16(1323), 312: uint16(1323), 313: uint16(1323), 314: uint16(184), 315: uint16(184), 316: uint16(1335), 317: uint16(1476), 318: uint16(856), 319: uint16(1482), 320: uint16(1744), 321: uint16(1744), 322: uint16(1665), 323: uint16(1665), 324: uint16(1773), 325: uint16(1773), 326: uint16(1665), 327: uint16(1669), 328: uint16(1671), 329: uint16(1802), 330: uint16(1782), 331: uint16(1809), 332: uint16(1809), 333: uint16(1809), 334: uint16(1809), 335: uint16(1665), 336: uint16(1817), 337: uint16(1690), 338: uint16(1671), 339: uint16(1671), 340: uint16(1690), 341: uint16(1802), 342: uint16(1782), 343: uint16(1690), 344: uint16(1782), 345: uint16(1690), 346: uint16(1665), 347: uint16(1817), 348: uint16(1693), 349: uint16(1791), 350: uint16(1665), 351: uint16(1817), 352: uint16(1832), 353: uint16(1665), 354: uint16(1817), 355: uint16(1665), 356: uint16(1817), 357: uint16(1832), 358: uint16(1752), 359: uint16(1752), 360: uint16(1752), 361: uint16(1805), 362: uint16(1850), 363: uint16(1850), 364: uint16(1832), 365: uint16(1752), 366: uint16(1749), 367: uint16(1752), 368: uint16(1805), 369: uint16(1752), 370: uint16(1752), 371: uint16(1713), 372: uint16(1858), 373: uint16(1775), 374: uint16(1775), 375: uint16(1832), 376: uint16(1665), 377: uint16(1799), 378: uint16(1799), 379: uint16(1823), 380: uint16(1823), 381: uint16(1761), 382: uint16(1765), 383: uint16(1890), 384: uint16(1665), 385: uint16(1757), 386: uint16(1761), 387: uint16(1771), 388: uint16(1774), 389: uint16(1690), 390: uint16(1894), 391: uint16(1913), 392: uint16(1913), 393: uint16(1929), 394: uint16(1929), 395: uint16(1929), 396: uint16(2100), 397: uint16(2100), 398: uint16(2100), 399: uint16(2100), 400: uint16(2100), 401: uint16(2100), 402: uint16(2100), 403: uint16(2100), 404: uint16(2100), 405: uint16(2100), 406: uint16(2100), 407: uint16(2100), 408: uint16(2100), 409: uint16(2100), 410: uint16(2100), 411: uint16(207), 412: uint16(1220), 413: uint16(331), 414: uint16(620), 415: uint16(967), 416: uint16(806), 417: uint16(1074), 418: uint16(1499), 419: uint16(1432), 420: uint16(1463), 421: uint16(1479), 422: uint16(1419), 423: uint16(1422), 424: uint16(1557), 425: uint16(1512), 426: uint16(1598), 427: uint16(1599), 428: uint16(1644), 429: uint16(1645), 430: uint16(1654), 431: uint16(1660), 432: uint16(1555), 433: uint16(1505), 434: uint16(1684), 435: uint16(1462), 436: uint16(1670), 437: uint16(1563), 438: uint16(1619), 439: uint16(1593), 440: uint16(1676), 441: uint16(1679), 442: uint16(1613), 443: uint16(1680), 444: uint16(1554), 445: uint16(1558), 446: uint16(1689), 447: uint16(1692), 448: uint16(1605), 449: uint16(1589), 450: uint16(1955), 451: uint16(1959), 452: uint16(1941), 453: uint16(1803), 454: uint16(1950), 455: uint16(1951), 456: uint16(1945), 457: uint16(1946), 458: uint16(1831), 459: uint16(1820), 460: uint16(1842), 461: uint16(1948), 462: uint16(1948), 463: uint16(1952), 464: uint16(1833), 465: uint16(1954), 466: uint16(1834), 467: uint16(1961), 468: uint16(1978), 469: uint16(1838), 470: uint16(1851), 471: uint16(1948), 472: uint16(1852), 473: uint16(1922), 474: uint16(1947), 475: uint16(1948), 476: uint16(1836), 477: uint16(1932), 478: uint16(1935), 479: uint16(1936), 480: uint16(1942), 481: uint16(1866), 482: uint16(1881), 483: uint16(1964), 484: uint16(1859), 485: uint16(1998), 486: uint16(1996), 487: uint16(1980), 488: uint16(1888), 489: uint16(1843), 490: uint16(1937), 491: uint16(1981), 492: uint16(1939), 493: uint16(1933), 494: uint16(1968), 495: uint16(1869), 496: uint16(1896), 497: uint16(1988), 498: uint16(1993), 499: uint16(1995), 500: uint16(1884), 501: uint16(1891), 502: uint16(1997), 503: uint16(1953), 504: uint16(1999), 505: uint16(2000), 506: uint16(1994), 507: uint16(2001), 508: uint16(1957), 509: uint16(1966), 510: uint16(2002), 511: uint16(1931), 512: uint16(1990), 513: uint16(2006), 514: uint16(1962), 515: uint16(2003), 516: uint16(2007), 517: uint16(2004), 518: uint16(1882), 519: uint16(2010), 520: uint16(2011), 521: uint16(2012), 522: uint16(2008), 523: uint16(2013), 524: uint16(2015), 525: uint16(1944), 526: uint16(1898), 527: uint16(2019), 528: uint16(2020), 529: uint16(1928), 530: uint16(2014), 531: uint16(2023), 532: uint16(1903), 533: uint16(2022), 534: uint16(2016), 535: uint16(2017), 536: uint16(2018), 537: uint16(2021), 538: uint16(1965), 539: uint16(1974), 540: uint16(1970), 541: uint16(2024), 542: uint16(1979), 543: uint16(1967), 544: uint16(2025), 545: uint16(2034), 546: uint16(2036), 547: uint16(2037), 548: uint16(2038), 549: uint16(2039), 550: uint16(2028), 551: uint16(1923), 552: uint16(1924), 553: uint16(2044), 554: uint16(2022), 555: uint16(2046), 556: uint16(2047), 557: uint16(2048), 558: uint16(2049), 559: uint16(2050), 560: uint16(2051), 561: uint16(2054), 562: uint16(2062), 563: uint16(2055), 564: uint16(2056), 565: uint16(2057), 566: uint16(2058), 567: uint16(2060), 568: uint16(2061), 569: uint16(2059), 570: uint16(1956), 571: uint16(1938), 572: uint16(1949), 573: uint16(1958), 574: uint16(2063), 575: uint16(2064), 576: uint16(2070), 577: uint16(2085), 578: uint16(2088), } var _yy_reduce_ofst = [411]int16{ 0: int16(-int32(125)), 1: int16(733), 2: int16(789), 3: int16(241), 4: int16(293), 5: int16(-int32(123)), 6: int16(-int32(193)), 7: int16(-int32(191)), 8: int16(-int32(183)), 9: int16(-int32(187)), 10: int16(166), 11: int16(238), 12: int16(133), 13: int16(-int32(207)), 14: int16(-int32(199)), 15: int16(-int32(267)), 16: int16(-int32(176)), 17: int16(-int32(6)), 18: int16(204), 19: int16(489), 20: int16(576), 21: int16(598), 22: int16(-int32(175)), 23: int16(686), 24: int16(860), 25: int16(615), 26: int16(725), 27: int16(1014), 28: int16(778), 29: int16(781), 30: int16(857), 31: int16(616), 32: int16(887), 33: int16(87), 34: int16(240), 35: int16(-int32(192)), 36: int16(408), 37: int16(626), 38: int16(796), 39: int16(843), 40: int16(854), 41: int16(1004), 42: int16(-int32(271)), 43: int16(-int32(271)), 44: int16(-int32(271)), 45: int16(-int32(271)), 46: int16(-int32(271)), 47: int16(-int32(271)), 48: int16(-int32(271)), 49: int16(-int32(271)), 50: int16(-int32(271)), 51: int16(-int32(271)), 52: int16(-int32(271)), 53: int16(-int32(271)), 54: int16(-int32(271)), 55: int16(-int32(271)), 56: int16(-int32(271)), 57: int16(-int32(271)), 58: int16(-int32(271)), 59: int16(-int32(271)), 60: int16(-int32(271)), 61: int16(-int32(271)), 62: int16(-int32(271)), 63: int16(-int32(271)), 64: int16(-int32(271)), 65: int16(-int32(271)), 66: int16(-int32(271)), 67: int16(-int32(271)), 68: int16(-int32(271)), 69: int16(-int32(271)), 70: int16(-int32(271)), 71: int16(-int32(271)), 72: int16(-int32(271)), 73: int16(-int32(271)), 74: int16(-int32(271)), 75: int16(-int32(271)), 76: int16(-int32(271)), 77: int16(-int32(271)), 78: int16(-int32(271)), 79: int16(80), 80: int16(83), 81: int16(313), 82: int16(886), 83: int16(888), 84: int16(918), 85: int16(938), 86: int16(1021), 87: int16(1034), 88: int16(1036), 89: int16(1141), 90: int16(1159), 91: int16(1163), 92: int16(1166), 93: int16(1168), 94: int16(1170), 95: int16(1176), 96: int16(1178), 97: int16(1180), 98: int16(1184), 99: int16(1196), 100: int16(1198), 101: int16(1205), 102: int16(1215), 103: int16(1225), 104: int16(1227), 105: int16(1236), 106: int16(1252), 107: int16(1254), 108: int16(1264), 109: int16(1303), 110: int16(1309), 111: int16(1312), 112: int16(1322), 113: int16(1325), 114: int16(1328), 115: int16(1337), 116: int16(1340), 117: int16(1343), 118: int16(1353), 119: int16(1371), 120: int16(1373), 121: int16(1384), 122: int16(1386), 123: int16(1411), 124: int16(1413), 125: int16(1420), 126: int16(1424), 127: int16(1426), 128: int16(1458), 129: int16(1470), 130: int16(1473), 131: int16(-int32(271)), 132: int16(-int32(271)), 133: int16(-int32(271)), 134: int16(-int32(271)), 135: int16(-int32(271)), 136: int16(-int32(271)), 137: int16(-int32(271)), 138: int16(-int32(271)), 139: int16(-int32(271)), 140: int16(-int32(271)), 141: int16(-int32(271)), 142: int16(138), 143: int16(459), 144: int16(396), 145: int16(-int32(158)), 146: int16(470), 147: int16(302), 148: int16(-int32(212)), 149: int16(521), 150: int16(201), 151: int16(-int32(195)), 152: int16(-int32(92)), 153: int16(559), 154: int16(630), 155: int16(632), 156: int16(630), 157: int16(-int32(271)), 158: int16(632), 159: int16(901), 160: int16(63), 161: int16(407), 162: int16(670), 163: int16(-int32(271)), 164: int16(-int32(271)), 165: int16(-int32(271)), 166: int16(-int32(271)), 167: int16(161), 168: int16(161), 169: int16(161), 170: int16(251), 171: int16(335), 172: int16(847), 173: int16(979), 174: int16(1097), 175: int16(537), 176: int16(588), 177: int16(618), 178: int16(628), 179: int16(688), 180: int16(688), 181: int16(-int32(166)), 182: int16(-int32(161)), 183: int16(674), 184: int16(787), 185: int16(794), 186: int16(799), 187: int16(852), 188: int16(996), 189: int16(-int32(122)), 190: int16(837), 191: int16(-int32(120)), 192: int16(1018), 193: int16(1035), 194: int16(415), 195: int16(1047), 196: int16(1001), 197: int16(958), 198: int16(1082), 199: int16(400), 200: int16(1099), 201: int16(779), 202: int16(1137), 203: int16(1142), 204: int16(263), 205: int16(1083), 206: int16(1145), 207: int16(1150), 208: int16(1041), 209: int16(1139), 210: int16(965), 211: int16(1050), 212: int16(362), 213: int16(849), 214: int16(752), 215: int16(629), 216: int16(675), 217: int16(1162), 218: int16(1173), 219: int16(1090), 220: int16(1195), 221: int16(-int32(194)), 222: int16(56), 223: int16(185), 224: int16(-int32(135)), 225: int16(232), 226: int16(522), 227: int16(560), 228: int16(571), 229: int16(601), 230: int16(617), 231: int16(669), 232: int16(683), 233: int16(711), 234: int16(850), 235: int16(893), 236: int16(1000), 237: int16(1040), 238: int16(1049), 239: int16(1081), 240: int16(1087), 241: int16(1101), 242: int16(392), 243: int16(1114), 244: int16(1123), 245: int16(1155), 246: int16(1161), 247: int16(1175), 248: int16(1271), 249: int16(1293), 250: int16(1299), 251: int16(1330), 252: int16(1339), 253: int16(1342), 254: int16(1347), 255: int16(593), 256: int16(1282), 257: int16(1286), 258: int16(1350), 259: int16(1359), 260: int16(1368), 261: int16(1314), 262: int16(1480), 263: int16(1483), 264: int16(1507), 265: int16(1085), 266: int16(1338), 267: int16(1526), 268: int16(1527), 269: int16(1487), 270: int16(1531), 271: int16(560), 272: int16(1532), 273: int16(1534), 274: int16(1535), 275: int16(1538), 276: int16(1539), 277: int16(1541), 278: int16(1448), 279: int16(1450), 280: int16(1496), 281: int16(1484), 282: int16(1485), 283: int16(1489), 284: int16(1490), 285: int16(1314), 286: int16(1496), 287: int16(1496), 288: int16(1504), 289: int16(1536), 290: int16(1564), 291: int16(1451), 292: int16(1486), 293: int16(1492), 294: int16(1509), 295: int16(1493), 296: int16(1465), 297: int16(1515), 298: int16(1494), 299: int16(1495), 300: int16(1517), 301: int16(1500), 302: int16(1519), 303: int16(1474), 304: int16(1550), 305: int16(1543), 306: int16(1548), 307: int16(1556), 308: int16(1565), 309: int16(1566), 310: int16(1518), 311: int16(1523), 312: int16(1542), 313: int16(1544), 314: int16(1525), 315: int16(1545), 316: int16(1513), 317: int16(1553), 318: int16(1552), 319: int16(1604), 320: int16(1508), 321: int16(1510), 322: int16(1608), 323: int16(1609), 324: int16(1520), 325: int16(1528), 326: int16(1612), 327: int16(1540), 328: int16(1559), 329: int16(1560), 330: int16(1592), 331: int16(1591), 332: int16(1595), 333: int16(1596), 334: int16(1597), 335: int16(1629), 336: int16(1638), 337: int16(1594), 338: int16(1569), 339: int16(1570), 340: int16(1600), 341: int16(1568), 342: int16(1610), 343: int16(1601), 344: int16(1611), 345: int16(1603), 346: int16(1643), 347: int16(1651), 348: int16(1562), 349: int16(1571), 350: int16(1655), 351: int16(1659), 352: int16(1640), 353: int16(1663), 354: int16(1666), 355: int16(1664), 356: int16(1667), 357: int16(1641), 358: int16(1650), 359: int16(1652), 360: int16(1653), 361: int16(1647), 362: int16(1656), 363: int16(1657), 364: int16(1658), 365: int16(1668), 366: int16(1672), 367: int16(1681), 368: int16(1649), 369: int16(1682), 370: int16(1683), 371: int16(1573), 372: int16(1582), 373: int16(1607), 374: int16(1615), 375: int16(1685), 376: int16(1702), 377: int16(1586), 378: int16(1587), 379: int16(1642), 380: int16(1646), 381: int16(1673), 382: int16(1675), 383: int16(1636), 384: int16(1714), 385: int16(1637), 386: int16(1677), 387: int16(1674), 388: int16(1678), 389: int16(1694), 390: int16(1719), 391: int16(1734), 392: int16(1735), 393: int16(1746), 394: int16(1747), 395: int16(1750), 396: int16(1633), 397: int16(1661), 398: int16(1686), 399: int16(1738), 400: int16(1728), 401: int16(1733), 402: int16(1736), 403: int16(1737), 404: int16(1740), 405: int16(1726), 406: int16(1730), 407: int16(1742), 408: int16(1743), 409: int16(1748), 410: int16(1753), } var _yy_default = [579]uint16{ 0: uint16(1648), 1: uint16(1648), 2: uint16(1648), 3: uint16(1478), 4: uint16(1243), 5: uint16(1354), 6: uint16(1243), 7: uint16(1243), 8: uint16(1243), 9: uint16(1478), 10: uint16(1478), 11: uint16(1478), 12: uint16(1243), 13: uint16(1384), 14: uint16(1384), 15: uint16(1531), 16: uint16(1276), 17: uint16(1243), 18: uint16(1243), 19: uint16(1243), 20: uint16(1243), 21: uint16(1243), 22: uint16(1243), 23: uint16(1243), 24: uint16(1243), 25: uint16(1243), 26: uint16(1243), 27: uint16(1243), 28: uint16(1477), 29: uint16(1243), 30: uint16(1243), 31: uint16(1243), 32: uint16(1243), 33: uint16(1564), 34: uint16(1564), 35: uint16(1243), 36: uint16(1243), 37: uint16(1243), 38: uint16(1243), 39: uint16(1243), 40: uint16(1243), 41: uint16(1243), 42: uint16(1243), 43: uint16(1393), 44: uint16(1243), 45: uint16(1400), 46: uint16(1243), 47: uint16(1243), 48: uint16(1243), 49: uint16(1243), 50: uint16(1243), 51: uint16(1479), 52: uint16(1480), 53: uint16(1243), 54: uint16(1243), 55: uint16(1243), 56: uint16(1530), 57: uint16(1532), 58: uint16(1495), 59: uint16(1407), 60: uint16(1406), 61: uint16(1405), 62: uint16(1404), 63: uint16(1513), 64: uint16(1372), 65: uint16(1398), 66: uint16(1391), 67: uint16(1395), 68: uint16(1474), 69: uint16(1475), 70: uint16(1473), 71: uint16(1626), 72: uint16(1480), 73: uint16(1479), 74: uint16(1243), 75: uint16(1394), 76: uint16(1442), 77: uint16(1458), 78: uint16(1441), 79: uint16(1243), 80: uint16(1243), 81: uint16(1243), 82: uint16(1243), 83: uint16(1243), 84: uint16(1243), 85: uint16(1243), 86: uint16(1243), 87: uint16(1243), 88: uint16(1243), 89: uint16(1243), 90: uint16(1243), 91: uint16(1243), 92: uint16(1243), 93: uint16(1243), 94: uint16(1243), 95: uint16(1243), 96: uint16(1243), 97: uint16(1243), 98: uint16(1243), 99: uint16(1243), 100: uint16(1243), 101: uint16(1243), 102: uint16(1243), 103: uint16(1243), 104: uint16(1243), 105: uint16(1243), 106: uint16(1243), 107: uint16(1243), 108: uint16(1243), 109: uint16(1243), 110: uint16(1243), 111: uint16(1243), 112: uint16(1243), 113: uint16(1243), 114: uint16(1243), 115: uint16(1243), 116: uint16(1243), 117: uint16(1243), 118: uint16(1243), 119: uint16(1243), 120: uint16(1243), 121: uint16(1243), 122: uint16(1243), 123: uint16(1243), 124: uint16(1243), 125: uint16(1243), 126: uint16(1243), 127: uint16(1243), 128: uint16(1243), 129: uint16(1243), 130: uint16(1243), 131: uint16(1450), 132: uint16(1457), 133: uint16(1456), 134: uint16(1455), 135: uint16(1464), 136: uint16(1454), 137: uint16(1451), 138: uint16(1444), 139: uint16(1443), 140: uint16(1445), 141: uint16(1446), 142: uint16(1243), 143: uint16(1243), 144: uint16(1267), 145: uint16(1243), 146: uint16(1243), 147: uint16(1264), 148: uint16(1318), 149: uint16(1243), 150: uint16(1243), 151: uint16(1243), 152: uint16(1243), 153: uint16(1243), 154: uint16(1550), 155: uint16(1549), 156: uint16(1243), 157: uint16(1447), 158: uint16(1243), 159: uint16(1276), 160: uint16(1435), 161: uint16(1434), 162: uint16(1433), 163: uint16(1461), 164: uint16(1448), 165: uint16(1460), 166: uint16(1459), 167: uint16(1538), 168: uint16(1600), 169: uint16(1599), 170: uint16(1496), 171: uint16(1243), 172: uint16(1243), 173: uint16(1243), 174: uint16(1243), 175: uint16(1243), 176: uint16(1243), 177: uint16(1564), 178: uint16(1243), 179: uint16(1243), 180: uint16(1243), 181: uint16(1243), 182: uint16(1243), 183: uint16(1243), 184: uint16(1243), 185: uint16(1243), 186: uint16(1243), 187: uint16(1243), 188: uint16(1243), 189: uint16(1243), 190: uint16(1243), 191: uint16(1243), 192: uint16(1243), 193: uint16(1243), 194: uint16(1243), 195: uint16(1243), 196: uint16(1243), 197: uint16(1243), 198: uint16(1243), 199: uint16(1243), 200: uint16(1243), 201: uint16(1374), 202: uint16(1564), 203: uint16(1564), 204: uint16(1243), 205: uint16(1276), 206: uint16(1564), 207: uint16(1564), 208: uint16(1375), 209: uint16(1375), 210: uint16(1272), 211: uint16(1272), 212: uint16(1378), 213: uint16(1243), 214: uint16(1545), 215: uint16(1345), 216: uint16(1345), 217: uint16(1345), 218: uint16(1345), 219: uint16(1354), 220: uint16(1345), 221: uint16(1243), 222: uint16(1243), 223: uint16(1243), 224: uint16(1243), 225: uint16(1243), 226: uint16(1243), 227: uint16(1243), 228: uint16(1243), 229: uint16(1243), 230: uint16(1243), 231: uint16(1243), 232: uint16(1243), 233: uint16(1243), 234: uint16(1243), 235: uint16(1243), 236: uint16(1535), 237: uint16(1533), 238: uint16(1243), 239: uint16(1243), 240: uint16(1243), 241: uint16(1243), 242: uint16(1243), 243: uint16(1243), 244: uint16(1243), 245: uint16(1243), 246: uint16(1243), 247: uint16(1243), 248: uint16(1243), 249: uint16(1243), 250: uint16(1243), 251: uint16(1243), 252: uint16(1243), 253: uint16(1243), 254: uint16(1243), 255: uint16(1243), 256: uint16(1243), 257: uint16(1243), 258: uint16(1243), 259: uint16(1243), 260: uint16(1243), 261: uint16(1243), 262: uint16(1243), 263: uint16(1243), 264: uint16(1243), 265: uint16(1350), 266: uint16(1243), 267: uint16(1243), 268: uint16(1243), 269: uint16(1243), 270: uint16(1243), 271: uint16(1243), 272: uint16(1243), 273: uint16(1243), 274: uint16(1243), 275: uint16(1243), 276: uint16(1243), 277: uint16(1593), 278: uint16(1243), 279: uint16(1508), 280: uint16(1332), 281: uint16(1350), 282: uint16(1350), 283: uint16(1350), 284: uint16(1350), 285: uint16(1352), 286: uint16(1333), 287: uint16(1331), 288: uint16(1344), 289: uint16(1277), 290: uint16(1250), 291: uint16(1640), 292: uint16(1410), 293: uint16(1399), 294: uint16(1351), 295: uint16(1399), 296: uint16(1637), 297: uint16(1397), 298: uint16(1410), 299: uint16(1410), 300: uint16(1397), 301: uint16(1410), 302: uint16(1351), 303: uint16(1637), 304: uint16(1293), 305: uint16(1615), 306: uint16(1288), 307: uint16(1384), 308: uint16(1384), 309: uint16(1384), 310: uint16(1374), 311: uint16(1374), 312: uint16(1374), 313: uint16(1374), 314: uint16(1378), 315: uint16(1378), 316: uint16(1476), 317: uint16(1351), 318: uint16(1344), 319: uint16(1243), 320: uint16(1640), 321: uint16(1640), 322: uint16(1360), 323: uint16(1360), 324: uint16(1639), 325: uint16(1639), 326: uint16(1360), 327: uint16(1496), 328: uint16(1623), 329: uint16(1419), 330: uint16(1321), 331: uint16(1327), 332: uint16(1327), 333: uint16(1327), 334: uint16(1327), 335: uint16(1360), 336: uint16(1261), 337: uint16(1397), 338: uint16(1623), 339: uint16(1623), 340: uint16(1397), 341: uint16(1419), 342: uint16(1321), 343: uint16(1397), 344: uint16(1321), 345: uint16(1397), 346: uint16(1360), 347: uint16(1261), 348: uint16(1512), 349: uint16(1634), 350: uint16(1360), 351: uint16(1261), 352: uint16(1486), 353: uint16(1360), 354: uint16(1261), 355: uint16(1360), 356: uint16(1261), 357: uint16(1486), 358: uint16(1319), 359: uint16(1319), 360: uint16(1319), 361: uint16(1308), 362: uint16(1243), 363: uint16(1243), 364: uint16(1486), 365: uint16(1319), 366: uint16(1293), 367: uint16(1319), 368: uint16(1308), 369: uint16(1319), 370: uint16(1319), 371: uint16(1582), 372: uint16(1243), 373: uint16(1490), 374: uint16(1490), 375: uint16(1486), 376: uint16(1360), 377: uint16(1574), 378: uint16(1574), 379: uint16(1387), 380: uint16(1387), 381: uint16(1392), 382: uint16(1378), 383: uint16(1481), 384: uint16(1360), 385: uint16(1243), 386: uint16(1392), 387: uint16(1390), 388: uint16(1388), 389: uint16(1397), 390: uint16(1311), 391: uint16(1596), 392: uint16(1596), 393: uint16(1592), 394: uint16(1592), 395: uint16(1592), 396: uint16(1645), 397: uint16(1645), 398: uint16(1545), 399: uint16(1608), 400: uint16(1276), 401: uint16(1276), 402: uint16(1276), 403: uint16(1276), 404: uint16(1608), 405: uint16(1295), 406: uint16(1295), 407: uint16(1277), 408: uint16(1277), 409: uint16(1276), 410: uint16(1608), 411: uint16(1243), 412: uint16(1243), 413: uint16(1243), 414: uint16(1243), 415: uint16(1243), 416: uint16(1243), 417: uint16(1603), 418: uint16(1243), 419: uint16(1540), 420: uint16(1497), 421: uint16(1364), 422: uint16(1243), 423: uint16(1243), 424: uint16(1243), 425: uint16(1243), 426: uint16(1243), 427: uint16(1243), 428: uint16(1243), 429: uint16(1243), 430: uint16(1243), 431: uint16(1243), 432: uint16(1243), 433: uint16(1243), 434: uint16(1243), 435: uint16(1243), 436: uint16(1243), 437: uint16(1243), 438: uint16(1551), 439: uint16(1243), 440: uint16(1243), 441: uint16(1243), 442: uint16(1243), 443: uint16(1243), 444: uint16(1243), 445: uint16(1243), 446: uint16(1243), 447: uint16(1243), 448: uint16(1243), 449: uint16(1424), 450: uint16(1243), 451: uint16(1246), 452: uint16(1542), 453: uint16(1243), 454: uint16(1243), 455: uint16(1243), 456: uint16(1243), 457: uint16(1243), 458: uint16(1243), 459: uint16(1243), 460: uint16(1243), 461: uint16(1401), 462: uint16(1402), 463: uint16(1365), 464: uint16(1243), 465: uint16(1243), 466: uint16(1243), 467: uint16(1243), 468: uint16(1243), 469: uint16(1243), 470: uint16(1243), 471: uint16(1416), 472: uint16(1243), 473: uint16(1243), 474: uint16(1243), 475: uint16(1411), 476: uint16(1243), 477: uint16(1243), 478: uint16(1243), 479: uint16(1243), 480: uint16(1243), 481: uint16(1243), 482: uint16(1243), 483: uint16(1243), 484: uint16(1636), 485: uint16(1243), 486: uint16(1243), 487: uint16(1243), 488: uint16(1243), 489: uint16(1243), 490: uint16(1243), 491: uint16(1511), 492: uint16(1510), 493: uint16(1243), 494: uint16(1243), 495: uint16(1362), 496: uint16(1243), 497: uint16(1243), 498: uint16(1243), 499: uint16(1243), 500: uint16(1243), 501: uint16(1243), 502: uint16(1243), 503: uint16(1243), 504: uint16(1243), 505: uint16(1243), 506: uint16(1243), 507: uint16(1243), 508: uint16(1243), 509: uint16(1291), 510: uint16(1243), 511: uint16(1243), 512: uint16(1243), 513: uint16(1243), 514: uint16(1243), 515: uint16(1243), 516: uint16(1243), 517: uint16(1243), 518: uint16(1243), 519: uint16(1243), 520: uint16(1243), 521: uint16(1243), 522: uint16(1243), 523: uint16(1243), 524: uint16(1243), 525: uint16(1243), 526: uint16(1243), 527: uint16(1243), 528: uint16(1243), 529: uint16(1243), 530: uint16(1243), 531: uint16(1243), 532: uint16(1243), 533: uint16(1389), 534: uint16(1243), 535: uint16(1243), 536: uint16(1243), 537: uint16(1243), 538: uint16(1243), 539: uint16(1243), 540: uint16(1243), 541: uint16(1243), 542: uint16(1243), 543: uint16(1243), 544: uint16(1243), 545: uint16(1243), 546: uint16(1243), 547: uint16(1243), 548: uint16(1579), 549: uint16(1379), 550: uint16(1243), 551: uint16(1243), 552: uint16(1243), 553: uint16(1243), 554: uint16(1627), 555: uint16(1243), 556: uint16(1243), 557: uint16(1243), 558: uint16(1243), 559: uint16(1243), 560: uint16(1243), 561: uint16(1243), 562: uint16(1243), 563: uint16(1243), 564: uint16(1243), 565: uint16(1243), 566: uint16(1243), 567: uint16(1243), 568: uint16(1243), 569: uint16(1619), 570: uint16(1335), 571: uint16(1425), 572: uint16(1243), 573: uint16(1428), 574: uint16(1265), 575: uint16(1243), 576: uint16(1255), 577: uint16(1243), 578: uint16(1243), } /********** End of lemon-generated parsing tables *****************************/ // C documentation // // /* The next table maps tokens (terminal symbols) into fallback tokens. // ** If a construct like the following: // ** // ** %fallback ID X Y Z. // ** // ** appears in the grammar, then ID becomes a fallback token for X, Y, // ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser // ** but it does not parse, the type of the token is changed to ID and // ** the parse is retried before an error is thrown. // ** // ** This feature can be used, for example, to cause some keywords in a language // ** to revert to identifiers if they keyword does not apply in the context where // ** it appears. // */ var _yyFallback = [185]uint16{ 2: uint16(59), 3: uint16(59), 4: uint16(59), 5: uint16(59), 7: uint16(59), 8: uint16(59), 9: uint16(59), 11: uint16(59), 12: uint16(59), 13: uint16(59), 14: uint16(59), 18: uint16(59), 21: uint16(59), 26: uint16(59), 27: uint16(59), 28: uint16(59), 29: uint16(59), 30: uint16(59), 31: uint16(59), 32: uint16(59), 33: uint16(59), 34: uint16(59), 35: uint16(59), 36: uint16(59), 37: uint16(59), 38: uint16(59), 39: uint16(59), 40: uint16(59), 41: uint16(59), 42: uint16(59), 46: uint16(59), 47: uint16(59), 60: uint16(59), 61: uint16(59), 62: uint16(59), 63: uint16(59), 64: uint16(59), 65: uint16(59), 66: uint16(59), 67: uint16(59), 68: uint16(59), 69: uint16(59), 70: uint16(59), 71: uint16(59), 72: uint16(59), 73: uint16(59), 74: uint16(59), 75: uint16(59), 76: uint16(59), 77: uint16(59), 78: uint16(59), 79: uint16(59), 80: uint16(59), 81: uint16(59), 82: uint16(59), 83: uint16(59), 84: uint16(59), 85: uint16(59), 86: uint16(59), 87: uint16(59), 88: uint16(59), 89: uint16(59), 90: uint16(59), 91: uint16(59), 92: uint16(59), 93: uint16(59), 94: uint16(59), 95: uint16(59), 96: uint16(59), 97: uint16(59), 98: uint16(59), 99: uint16(59), 100: uint16(59), } /* The following structure represents a single element of the ** parser's stack. Information stored includes: ** ** + The state number for the parser at this level of the stack. ** ** + The value of the token stored at this level of the stack. ** (In other words, the "major" token.) ** ** + The semantic value stored at this level of the stack. This is ** the information used by the action routines in the grammar. ** It is sometimes called the "minor" token. ** ** After the "shift" half of a SHIFTREDUCE action, the stateno field ** actually contains the reduce action for the second half of the ** SHIFTREDUCE. */ type TyyStackEntry1 = struct { Fstateno uint16 Fmajor uint16 Fminor TYYMINORTYPE } type yyStackEntry1 = TyyStackEntry1 type TyyStackEntry = struct { Fstateno uint16 Fmajor uint16 Fminor TYYMINORTYPE } type yyStackEntry = TyyStackEntry /* The state of the parser is completely contained in an instance of ** the following structure */ type TyyParser1 = struct { Fyytos uintptr FpParse uintptr Fyystack [100]TyyStackEntry FyystackEnd uintptr } type yyParser1 = TyyParser1 type TyyParser = struct { Fyytos uintptr FpParse uintptr Fyystack [100]TyyStackEntry FyystackEnd uintptr } type yyParser = TyyParser /* #include */ /* Datatype of the argument to the memory allocated passed as the ** second argument to sqlite3ParserAlloc() below. This can be changed by ** putting an appropriate #define in the %include section of the input ** grammar. */ // C documentation // // /* Initialize a new parser that has already been allocated. // */ func _sqlite3ParserInit(tls *libc.TLS, yypRawParser uintptr, pParse uintptr) { var yypParser uintptr _ = yypParser yypParser = yypRawParser (*TyyParser)(unsafe.Pointer(yypParser)).FpParse = pParse (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos = yypParser + 8 (*(*TyyStackEntry)(unsafe.Pointer(yypParser + 8))).Fstateno = uint16(0) (*(*TyyStackEntry)(unsafe.Pointer(yypParser + 8))).Fmajor = uint16(0) (*TyyParser)(unsafe.Pointer(yypParser)).FyystackEnd = yypParser + 8 + uintptr(libc.Int32FromInt32(YYSTACKDEPTH)-libc.Int32FromInt32(1))*12 } // C documentation // // /* The following function deletes the "minor type" or semantic value // ** associated with a symbol. The symbol can be either a terminal // ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is // ** a pointer to the value to be deleted. The code used to do the // ** deletions is derived from the %destructor and/or %token_destructor // ** directives of the input grammar. // */ func _yy_destructor(tls *libc.TLS, yypParser uintptr, yymajor uint16, yypminor uintptr) { var pParse uintptr _ = pParse pParse = (*TyyParser)(unsafe.Pointer(yypParser)).FpParse switch int32(yymajor) { /* Here is inserted the actions which take place when a ** terminal or non-terminal is destroyed. This can happen ** when the symbol is popped from the stack during a ** reduce or during error processing or when a parser is ** being destroyed before it is finished parsing. ** ** Note: during a reduce, the only symbols destroyed are those ** which appear on the RHS of the rule, but which are *not* used ** inside the C code. */ /********* Begin destructor definitions ***************************************/ case int32(204): /* select */ fallthrough case int32(239): /* selectnowith */ fallthrough case int32(240): /* oneselect */ fallthrough case int32(252): /* values */ _sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(216): /* term */ fallthrough case int32(217): /* expr */ fallthrough case int32(246): /* where_opt */ fallthrough case int32(248): /* having_opt */ fallthrough case int32(267): /* where_opt_ret */ fallthrough case int32(278): /* case_operand */ fallthrough case int32(280): /* case_else */ fallthrough case int32(283): /* vinto */ fallthrough case int32(290): /* when_clause */ fallthrough case int32(295): /* key_opt */ fallthrough case int32(311): /* filter_clause */ _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(221): /* eidlist_opt */ fallthrough case int32(231): /* sortlist */ fallthrough case int32(232): /* eidlist */ fallthrough case int32(244): /* selcollist */ fallthrough case int32(247): /* groupby_opt */ fallthrough case int32(249): /* orderby_opt */ fallthrough case int32(253): /* nexprlist */ fallthrough case int32(254): /* sclp */ fallthrough case int32(261): /* exprlist */ fallthrough case int32(268): /* setlist */ fallthrough case int32(277): /* paren_exprlist */ fallthrough case int32(279): /* case_exprlist */ fallthrough case int32(310): /* part_opt */ _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(238): /* fullname */ fallthrough case int32(245): /* from */ fallthrough case int32(256): /* seltablist */ fallthrough case int32(257): /* stl_prefix */ fallthrough case int32(262): /* xfullname */ _sqlite3SrcListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(241): /* wqlist */ _sqlite3WithDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(251): /* window_clause */ fallthrough case int32(306): /* windowdefn_list */ _sqlite3WindowListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(263): /* idlist */ fallthrough case int32(270): /* idlist_opt */ _sqlite3IdListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(273): /* filter_over */ fallthrough case int32(307): /* windowdefn */ fallthrough case int32(308): /* window */ fallthrough case int32(309): /* frame_opt */ fallthrough case int32(312): /* over_clause */ _sqlite3WindowDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(286): /* trigger_cmd_list */ fallthrough case int32(291): /* trigger_cmd */ _sqlite3DeleteTriggerStep(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(288): /* trigger_event */ _sqlite3IdListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*(*TTrigEvent)(unsafe.Pointer(yypminor))).Fb) case int32(314): /* frame_bound */ fallthrough case int32(315): /* frame_bound_s */ fallthrough case int32(316): /* frame_bound_e */ _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*(*TFrameBound)(unsafe.Pointer(yypminor))).FpExpr) break /********* End destructor definitions *****************************************/ fallthrough default: break /* If no destructor action specified: do nothing */ } } // C documentation // // /* // ** Pop the parser's stack once. // ** // ** If there is a destructor routine associated with the token which // ** is popped from the stack, then call it. // */ func _yy_pop_parser_stack(tls *libc.TLS, pParser uintptr) { var yytos, v1, v2 uintptr _, _, _ = yytos, v1, v2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) v2 = pParser v1 = *(*uintptr)(unsafe.Pointer(v2)) *(*uintptr)(unsafe.Pointer(v2)) -= 12 yytos = v1 _yy_destructor(tls, pParser, (*TyyStackEntry)(unsafe.Pointer(yytos)).Fmajor, yytos+4) } // C documentation // // /* // ** Clear all secondary memory allocations from the parser // */ func _sqlite3ParserFinalize(tls *libc.TLS, p uintptr) { var pParser uintptr _ = pParser pParser = p for (*TyyParser)(unsafe.Pointer(pParser)).Fyytos > pParser+8 { _yy_pop_parser_stack(tls, pParser) } } /* ** Return the peak depth of the stack for a parser. */ /* This array of booleans keeps track of the parser statement ** coverage. The element yycoverage[X][Y] is set when the parser ** is in state X and has a lookahead token Y. In a well-tested ** systems, every element of this matrix should end up being set. */ /* ** Write into out a description of every state/lookahead combination that ** ** (1) has not been used by the parser, and ** (2) is not a syntax error. ** ** Return the number of missed state/lookahead combinations. */ // C documentation // // /* // ** Find the appropriate action for a parser given the terminal // ** look-ahead token iLookAhead. // */ func _yy_find_shift_action(tls *libc.TLS, iLookAhead uint16, stateno uint16) (r uint16) { var i, j int32 var iFallback uint16 _, _, _ = i, iFallback, j if int32(stateno) > int32(YY_MAX_SHIFT) { return stateno } _ = libc.Int32FromInt32(0) for cond := true; cond; cond = int32(1) != 0 { i = int32(_yy_shift_ofst[stateno]) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) i += int32(iLookAhead) _ = libc.Int32FromInt32(0) if int32(_yy_lookahead[i]) != int32(iLookAhead) { /* Fallback token */ _ = libc.Int32FromInt32(0) iFallback = _yyFallback[iLookAhead] if int32(iFallback) != 0 { _ = libc.Int32FromInt32(0) /* Fallback loop must terminate */ iLookAhead = iFallback continue } j = i - int32(iLookAhead) + int32(YYWILDCARD) _ = libc.Int32FromInt32(0) if int32(_yy_lookahead[j]) == int32(YYWILDCARD) && int32(iLookAhead) > 0 { return _yy_action[j] } return _yy_default[stateno] } else { _ = libc.Int32FromInt32(0) return _yy_action[i] } } return r } // C documentation // // /* // ** Find the appropriate action for a parser given the non-terminal // ** look-ahead token iLookAhead. // */ func _yy_find_reduce_action(tls *libc.TLS, stateno uint16, iLookAhead uint16) (r uint16) { var i int32 _ = i _ = libc.Int32FromInt32(0) i = int32(_yy_reduce_ofst[stateno]) _ = libc.Int32FromInt32(0) i += int32(iLookAhead) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return _yy_action[i] } // C documentation // // /* // ** The following routine is called if the stack overflows. // */ func _yyStackOverflow(tls *libc.TLS, yypParser uintptr) { var pParse uintptr _ = pParse pParse = (*TyyParser)(unsafe.Pointer(yypParser)).FpParse for (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos > yypParser+8 { _yy_pop_parser_stack(tls, yypParser) } /* Here code is inserted which will execute if the parser ** stack every overflows */ /******** Begin %stack_overflow code ******************************************/ _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23336, 0) /******** End %stack_overflow code ********************************************/ /* Suppress warning about unused %extra_argument var */ (*TyyParser)(unsafe.Pointer(yypParser)).FpParse = pParse } /* ** Print tracing information for a SHIFT action */ // C documentation // // /* // ** Perform a shift action. // */ func _yy_shift(tls *libc.TLS, yypParser uintptr, yyNewState uint16, yyMajor uint16, yyMinor TToken) { var yytos uintptr _ = yytos (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos += 12 if (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos > (*TyyParser)(unsafe.Pointer(yypParser)).FyystackEnd { (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos -= 12 _yyStackOverflow(tls, yypParser) return } if int32(yyNewState) > int32(YY_MAX_SHIFT) { yyNewState = uint16(int32(yyNewState) + (libc.Int32FromInt32(YY_MIN_REDUCE) - libc.Int32FromInt32(YY_MIN_SHIFTREDUCE))) } yytos = (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos (*TyyStackEntry)(unsafe.Pointer(yytos)).Fstateno = yyNewState (*TyyStackEntry)(unsafe.Pointer(yytos)).Fmajor = yyMajor *(*TToken)(unsafe.Pointer(yytos + 4)) = yyMinor } // C documentation // // /* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side // ** of that rule */ var _yyRuleInfoLhs = [405]uint16{ 0: uint16(189), 1: uint16(189), 2: uint16(188), 3: uint16(190), 4: uint16(191), 5: uint16(191), 6: uint16(191), 7: uint16(191), 8: uint16(190), 9: uint16(190), 10: uint16(190), 11: uint16(190), 12: uint16(190), 13: uint16(195), 14: uint16(197), 15: uint16(199), 16: uint16(199), 17: uint16(198), 18: uint16(198), 19: uint16(196), 20: uint16(196), 21: uint16(203), 22: uint16(203), 23: uint16(205), 24: uint16(205), 25: uint16(206), 26: uint16(208), 27: uint16(208), 28: uint16(208), 29: uint16(209), 30: uint16(213), 31: uint16(214), 32: uint16(215), 33: uint16(215), 34: uint16(215), 35: uint16(215), 36: uint16(215), 37: uint16(215), 38: uint16(215), 39: uint16(215), 40: uint16(215), 41: uint16(215), 42: uint16(215), 43: uint16(215), 44: uint16(215), 45: uint16(224), 46: uint16(224), 47: uint16(220), 48: uint16(220), 49: uint16(222), 50: uint16(222), 51: uint16(225), 52: uint16(225), 53: uint16(225), 54: uint16(225), 55: uint16(226), 56: uint16(226), 57: uint16(226), 58: uint16(226), 59: uint16(226), 60: uint16(223), 61: uint16(223), 62: uint16(227), 63: uint16(227), 64: uint16(227), 65: uint16(202), 66: uint16(229), 67: uint16(230), 68: uint16(230), 69: uint16(230), 70: uint16(230), 71: uint16(230), 72: uint16(233), 73: uint16(218), 74: uint16(218), 75: uint16(234), 76: uint16(234), 77: uint16(235), 78: uint16(235), 79: uint16(190), 80: uint16(237), 81: uint16(237), 82: uint16(190), 83: uint16(190), 84: uint16(190), 85: uint16(204), 86: uint16(204), 87: uint16(204), 88: uint16(239), 89: uint16(242), 90: uint16(242), 91: uint16(242), 92: uint16(240), 93: uint16(240), 94: uint16(252), 95: uint16(252), 96: uint16(243), 97: uint16(243), 98: uint16(243), 99: uint16(254), 100: uint16(244), 101: uint16(244), 102: uint16(244), 103: uint16(255), 104: uint16(255), 105: uint16(245), 106: uint16(245), 107: uint16(257), 108: uint16(257), 109: uint16(256), 110: uint16(256), 111: uint16(256), 112: uint16(256), 113: uint16(256), 114: uint16(200), 115: uint16(200), 116: uint16(238), 117: uint16(238), 118: uint16(262), 119: uint16(262), 120: uint16(262), 121: uint16(262), 122: uint16(258), 123: uint16(258), 124: uint16(258), 125: uint16(258), 126: uint16(259), 127: uint16(259), 128: uint16(259), 129: uint16(264), 130: uint16(260), 131: uint16(260), 132: uint16(249), 133: uint16(249), 134: uint16(231), 135: uint16(231), 136: uint16(219), 137: uint16(219), 138: uint16(219), 139: uint16(265), 140: uint16(265), 141: uint16(265), 142: uint16(247), 143: uint16(247), 144: uint16(248), 145: uint16(248), 146: uint16(250), 147: uint16(250), 148: uint16(250), 149: uint16(250), 150: uint16(190), 151: uint16(246), 152: uint16(246), 153: uint16(267), 154: uint16(267), 155: uint16(267), 156: uint16(267), 157: uint16(190), 158: uint16(268), 159: uint16(268), 160: uint16(268), 161: uint16(268), 162: uint16(190), 163: uint16(190), 164: uint16(271), 165: uint16(271), 166: uint16(271), 167: uint16(271), 168: uint16(271), 169: uint16(271), 170: uint16(272), 171: uint16(269), 172: uint16(269), 173: uint16(270), 174: uint16(270), 175: uint16(263), 176: uint16(263), 177: uint16(217), 178: uint16(217), 179: uint16(217), 180: uint16(217), 181: uint16(216), 182: uint16(216), 183: uint16(216), 184: uint16(217), 185: uint16(217), 186: uint16(217), 187: uint16(217), 188: uint16(217), 189: uint16(217), 190: uint16(217), 191: uint16(217), 192: uint16(217), 193: uint16(216), 194: uint16(217), 195: uint16(217), 196: uint16(217), 197: uint16(217), 198: uint16(217), 199: uint16(217), 200: uint16(217), 201: uint16(217), 202: uint16(217), 203: uint16(274), 204: uint16(217), 205: uint16(217), 206: uint16(217), 207: uint16(217), 208: uint16(217), 209: uint16(217), 210: uint16(217), 211: uint16(217), 212: uint16(217), 213: uint16(217), 214: uint16(217), 215: uint16(217), 216: uint16(275), 217: uint16(275), 218: uint16(217), 219: uint16(276), 220: uint16(276), 221: uint16(217), 222: uint16(217), 223: uint16(217), 224: uint16(217), 225: uint16(217), 226: uint16(217), 227: uint16(279), 228: uint16(279), 229: uint16(280), 230: uint16(280), 231: uint16(278), 232: uint16(261), 233: uint16(253), 234: uint16(253), 235: uint16(277), 236: uint16(277), 237: uint16(190), 238: uint16(281), 239: uint16(281), 240: uint16(221), 241: uint16(221), 242: uint16(232), 243: uint16(232), 244: uint16(282), 245: uint16(282), 246: uint16(190), 247: uint16(190), 248: uint16(190), 249: uint16(283), 250: uint16(283), 251: uint16(190), 252: uint16(190), 253: uint16(190), 254: uint16(190), 255: uint16(190), 256: uint16(211), 257: uint16(212), 258: uint16(190), 259: uint16(285), 260: uint16(287), 261: uint16(287), 262: uint16(287), 263: uint16(288), 264: uint16(288), 265: uint16(288), 266: uint16(290), 267: uint16(290), 268: uint16(286), 269: uint16(286), 270: uint16(292), 271: uint16(293), 272: uint16(293), 273: uint16(291), 274: uint16(291), 275: uint16(291), 276: uint16(291), 277: uint16(217), 278: uint16(217), 279: uint16(236), 280: uint16(236), 281: uint16(236), 282: uint16(190), 283: uint16(190), 284: uint16(190), 285: uint16(295), 286: uint16(295), 287: uint16(190), 288: uint16(190), 289: uint16(190), 290: uint16(190), 291: uint16(190), 292: uint16(190), 293: uint16(190), 294: uint16(296), 295: uint16(190), 296: uint16(190), 297: uint16(190), 298: uint16(298), 299: uint16(300), 300: uint16(301), 301: uint16(301), 302: uint16(302), 303: uint16(266), 304: uint16(266), 305: uint16(305), 306: uint16(305), 307: uint16(305), 308: uint16(304), 309: uint16(241), 310: uint16(241), 311: uint16(306), 312: uint16(307), 313: uint16(308), 314: uint16(308), 315: uint16(308), 316: uint16(308), 317: uint16(308), 318: uint16(309), 319: uint16(309), 320: uint16(309), 321: uint16(313), 322: uint16(315), 323: uint16(315), 324: uint16(316), 325: uint16(316), 326: uint16(314), 327: uint16(314), 328: uint16(317), 329: uint16(317), 330: uint16(318), 331: uint16(318), 332: uint16(318), 333: uint16(251), 334: uint16(273), 335: uint16(273), 336: uint16(273), 337: uint16(312), 338: uint16(312), 339: uint16(311), 340: uint16(185), 341: uint16(186), 342: uint16(186), 343: uint16(187), 344: uint16(187), 345: uint16(187), 346: uint16(192), 347: uint16(192), 348: uint16(192), 349: uint16(194), 350: uint16(194), 351: uint16(190), 352: uint16(203), 353: uint16(201), 354: uint16(201), 355: uint16(193), 356: uint16(193), 357: uint16(208), 358: uint16(209), 359: uint16(210), 360: uint16(210), 361: uint16(207), 362: uint16(207), 363: uint16(215), 364: uint16(215), 365: uint16(215), 366: uint16(202), 367: uint16(228), 368: uint16(228), 369: uint16(229), 370: uint16(233), 371: uint16(235), 372: uint16(239), 373: uint16(240), 374: uint16(254), 375: uint16(255), 376: uint16(264), 377: uint16(272), 378: uint16(217), 379: uint16(274), 380: uint16(278), 381: uint16(261), 382: uint16(284), 383: uint16(284), 384: uint16(284), 385: uint16(284), 386: uint16(284), 387: uint16(211), 388: uint16(289), 389: uint16(289), 390: uint16(292), 391: uint16(293), 392: uint16(294), 393: uint16(294), 394: uint16(297), 395: uint16(297), 396: uint16(299), 397: uint16(299), 398: uint16(300), 399: uint16(303), 400: uint16(303), 401: uint16(303), 402: uint16(266), 403: uint16(306), 404: uint16(308), } // C documentation // // /* For rule J, yyRuleInfoNRhs[J] contains the negative of the number // ** of symbols on the right-hand side of that rule. */ var _yyRuleInfoNRhs = [405]int8{ 0: int8(-int32(1)), 1: int8(-int32(3)), 2: int8(-int32(1)), 3: int8(-int32(3)), 5: int8(-int32(1)), 6: int8(-int32(1)), 7: int8(-int32(1)), 8: int8(-int32(2)), 9: int8(-int32(2)), 10: int8(-int32(2)), 11: int8(-int32(3)), 12: int8(-int32(5)), 13: int8(-int32(6)), 14: int8(-int32(1)), 16: int8(-int32(3)), 17: int8(-int32(1)), 19: int8(-int32(5)), 20: int8(-int32(2)), 22: int8(-int32(3)), 23: int8(-int32(2)), 24: int8(-int32(1)), 25: int8(-int32(2)), 27: int8(-int32(4)), 28: int8(-int32(6)), 29: int8(-int32(2)), 32: int8(-int32(2)), 33: int8(-int32(3)), 34: int8(-int32(4)), 35: int8(-int32(4)), 36: int8(-int32(4)), 37: int8(-int32(3)), 38: int8(-int32(3)), 39: int8(-int32(5)), 40: int8(-int32(2)), 41: int8(-int32(4)), 42: int8(-int32(4)), 43: int8(-int32(1)), 44: int8(-int32(2)), 45: int8(-int32(3)), 46: int8(-int32(4)), 48: int8(-int32(1)), 50: int8(-int32(2)), 51: int8(-int32(2)), 52: int8(-int32(3)), 53: int8(-int32(3)), 54: int8(-int32(3)), 55: int8(-int32(2)), 56: int8(-int32(2)), 57: int8(-int32(1)), 58: int8(-int32(1)), 59: int8(-int32(2)), 60: int8(-int32(3)), 61: int8(-int32(2)), 63: int8(-int32(2)), 64: int8(-int32(2)), 66: int8(-int32(1)), 67: int8(-int32(2)), 68: int8(-int32(7)), 69: int8(-int32(5)), 70: int8(-int32(5)), 71: int8(-int32(10)), 74: int8(-int32(3)), 76: int8(-int32(2)), 77: int8(-int32(1)), 78: int8(-int32(1)), 79: int8(-int32(4)), 80: int8(-int32(2)), 82: int8(-int32(9)), 83: int8(-int32(4)), 84: int8(-int32(1)), 85: int8(-int32(3)), 86: int8(-int32(4)), 87: int8(-int32(1)), 88: int8(-int32(3)), 89: int8(-int32(1)), 90: int8(-int32(2)), 91: int8(-int32(1)), 92: int8(-int32(9)), 93: int8(-int32(10)), 94: int8(-int32(4)), 95: int8(-int32(5)), 96: int8(-int32(1)), 97: int8(-int32(1)), 100: int8(-int32(5)), 101: int8(-int32(3)), 102: int8(-int32(5)), 103: int8(-int32(2)), 106: int8(-int32(2)), 107: int8(-int32(2)), 109: int8(-int32(5)), 110: int8(-int32(6)), 111: int8(-int32(8)), 112: int8(-int32(6)), 113: int8(-int32(6)), 115: int8(-int32(2)), 116: int8(-int32(1)), 117: int8(-int32(3)), 118: int8(-int32(1)), 119: int8(-int32(3)), 120: int8(-int32(5)), 121: int8(-int32(3)), 122: int8(-int32(1)), 123: int8(-int32(2)), 124: int8(-int32(3)), 125: int8(-int32(4)), 126: int8(-int32(2)), 127: int8(-int32(4)), 130: int8(-int32(3)), 131: int8(-int32(2)), 133: int8(-int32(3)), 134: int8(-int32(5)), 135: int8(-int32(3)), 136: int8(-int32(1)), 137: int8(-int32(1)), 139: int8(-int32(2)), 140: int8(-int32(2)), 143: int8(-int32(3)), 145: int8(-int32(2)), 147: int8(-int32(2)), 148: int8(-int32(4)), 149: int8(-int32(4)), 150: int8(-int32(6)), 152: int8(-int32(2)), 154: int8(-int32(2)), 155: int8(-int32(2)), 156: int8(-int32(4)), 157: int8(-int32(9)), 158: int8(-int32(5)), 159: int8(-int32(7)), 160: int8(-int32(3)), 161: int8(-int32(5)), 162: int8(-int32(7)), 163: int8(-int32(8)), 165: int8(-int32(2)), 166: int8(-int32(12)), 167: int8(-int32(9)), 168: int8(-int32(5)), 169: int8(-int32(8)), 170: int8(-int32(2)), 171: int8(-int32(2)), 172: int8(-int32(1)), 174: int8(-int32(3)), 175: int8(-int32(3)), 176: int8(-int32(1)), 177: int8(-int32(3)), 178: int8(-int32(1)), 179: int8(-int32(3)), 180: int8(-int32(5)), 181: int8(-int32(1)), 182: int8(-int32(1)), 183: int8(-int32(1)), 184: int8(-int32(1)), 185: int8(-int32(3)), 186: int8(-int32(6)), 187: int8(-int32(5)), 188: int8(-int32(8)), 189: int8(-int32(4)), 190: int8(-int32(6)), 191: int8(-int32(9)), 192: int8(-int32(5)), 193: int8(-int32(1)), 194: int8(-int32(5)), 195: int8(-int32(3)), 196: int8(-int32(3)), 197: int8(-int32(3)), 198: int8(-int32(3)), 199: int8(-int32(3)), 200: int8(-int32(3)), 201: int8(-int32(3)), 202: int8(-int32(3)), 203: int8(-int32(2)), 204: int8(-int32(3)), 205: int8(-int32(5)), 206: int8(-int32(2)), 207: int8(-int32(3)), 208: int8(-int32(3)), 209: int8(-int32(4)), 210: int8(-int32(6)), 211: int8(-int32(5)), 212: int8(-int32(2)), 213: int8(-int32(2)), 214: int8(-int32(2)), 215: int8(-int32(3)), 216: int8(-int32(1)), 217: int8(-int32(2)), 218: int8(-int32(5)), 219: int8(-int32(1)), 220: int8(-int32(2)), 221: int8(-int32(5)), 222: int8(-int32(3)), 223: int8(-int32(5)), 224: int8(-int32(5)), 225: int8(-int32(4)), 226: int8(-int32(5)), 227: int8(-int32(5)), 228: int8(-int32(4)), 229: int8(-int32(2)), 233: int8(-int32(3)), 234: int8(-int32(1)), 236: int8(-int32(3)), 237: int8(-int32(12)), 238: int8(-int32(1)), 241: int8(-int32(3)), 242: int8(-int32(5)), 243: int8(-int32(3)), 245: int8(-int32(2)), 246: int8(-int32(4)), 247: int8(-int32(2)), 248: int8(-int32(3)), 249: int8(-int32(2)), 251: int8(-int32(3)), 252: int8(-int32(5)), 253: int8(-int32(6)), 254: int8(-int32(5)), 255: int8(-int32(6)), 256: int8(-int32(2)), 257: int8(-int32(2)), 258: int8(-int32(5)), 259: int8(-int32(11)), 260: int8(-int32(1)), 261: int8(-int32(2)), 263: int8(-int32(1)), 264: int8(-int32(1)), 265: int8(-int32(3)), 267: int8(-int32(2)), 268: int8(-int32(3)), 269: int8(-int32(2)), 270: int8(-int32(3)), 271: int8(-int32(3)), 272: int8(-int32(2)), 273: int8(-int32(9)), 274: int8(-int32(8)), 275: int8(-int32(6)), 276: int8(-int32(3)), 277: int8(-int32(4)), 278: int8(-int32(6)), 279: int8(-int32(1)), 280: int8(-int32(1)), 281: int8(-int32(1)), 282: int8(-int32(4)), 283: int8(-int32(6)), 284: int8(-int32(3)), 286: int8(-int32(2)), 287: int8(-int32(1)), 288: int8(-int32(3)), 289: int8(-int32(1)), 290: int8(-int32(3)), 291: int8(-int32(6)), 292: int8(-int32(7)), 293: int8(-int32(6)), 294: int8(-int32(1)), 295: int8(-int32(8)), 296: int8(-int32(1)), 297: int8(-int32(4)), 298: int8(-int32(8)), 300: int8(-int32(1)), 301: int8(-int32(3)), 302: int8(-int32(1)), 303: int8(-int32(2)), 304: int8(-int32(3)), 305: int8(-int32(1)), 306: int8(-int32(2)), 307: int8(-int32(3)), 308: int8(-int32(6)), 309: int8(-int32(1)), 310: int8(-int32(3)), 311: int8(-int32(3)), 312: int8(-int32(5)), 313: int8(-int32(5)), 314: int8(-int32(6)), 315: int8(-int32(4)), 316: int8(-int32(5)), 317: int8(-int32(2)), 319: int8(-int32(3)), 320: int8(-int32(6)), 321: int8(-int32(1)), 322: int8(-int32(1)), 323: int8(-int32(2)), 324: int8(-int32(1)), 325: int8(-int32(2)), 326: int8(-int32(2)), 327: int8(-int32(2)), 329: int8(-int32(2)), 330: int8(-int32(2)), 331: int8(-int32(2)), 332: int8(-int32(1)), 333: int8(-int32(2)), 334: int8(-int32(2)), 335: int8(-int32(1)), 336: int8(-int32(1)), 337: int8(-int32(4)), 338: int8(-int32(2)), 339: int8(-int32(5)), 340: int8(-int32(1)), 341: int8(-int32(2)), 342: int8(-int32(1)), 343: int8(-int32(1)), 344: int8(-int32(2)), 345: int8(-int32(3)), 347: int8(-int32(1)), 348: int8(-int32(2)), 349: int8(-int32(1)), 351: int8(-int32(2)), 352: int8(-int32(1)), 353: int8(-int32(4)), 354: int8(-int32(2)), 355: int8(-int32(1)), 356: int8(-int32(1)), 357: int8(-int32(1)), 358: int8(-int32(1)), 359: int8(-int32(1)), 360: int8(-int32(1)), 361: int8(-int32(2)), 363: int8(-int32(2)), 364: int8(-int32(4)), 365: int8(-int32(2)), 366: int8(-int32(2)), 367: int8(-int32(3)), 368: int8(-int32(1)), 370: int8(-int32(1)), 371: int8(-int32(1)), 372: int8(-int32(1)), 373: int8(-int32(1)), 374: int8(-int32(2)), 375: int8(-int32(1)), 376: int8(-int32(1)), 378: int8(-int32(1)), 379: int8(-int32(1)), 380: int8(-int32(1)), 381: int8(-int32(1)), 382: int8(-int32(1)), 383: int8(-int32(1)), 384: int8(-int32(1)), 385: int8(-int32(1)), 386: int8(-int32(1)), 387: int8(-int32(1)), 389: int8(-int32(3)), 390: int8(-int32(1)), 392: int8(-int32(1)), 395: int8(-int32(1)), 396: int8(-int32(1)), 397: int8(-int32(3)), 398: int8(-int32(2)), 400: int8(-int32(4)), 401: int8(-int32(2)), 403: int8(-int32(1)), 404: int8(-int32(1)), } // C documentation // // /* // ** Perform a reduce action and the shift that must immediately // ** follow the reduce. // ** // ** The yyLookahead and yyLookaheadToken parameters provide reduce actions // ** access to the lookahead token (if any). The yyLookahead will be YYNOCODE // ** if the lookahead token has already been consumed. As this procedure is // ** only called from one place, optimizing compilers will in-line it, which // ** means that the extra parameters have no performance impact. // */ func _yy_reduce(tls *libc.TLS, yypParser uintptr, yyruleno uint32, yyLookahead int32, yyLookaheadToken TToken, pParse uintptr) (r uint16) { bp := tls.Alloc(96) defer tls.Free(96) var bNot, bNot1, nExpr, yygoto, yysize, v344 int32 var n Tu32 var p, p1, p2, p3, p4, pDot, pFrom, pFromClause, pLeft, pLeft1, pLhs, pList, pList1, pList2, pList3, pList4, pNew, pOld, pRHS, pRhs, pRight, pRight1, pSelect, pSelectRHS, pSrc, pSubquery, pSubquery1, temp1, temp11, temp2, temp21, temp3, temp4, yymsp, v343, v345, v346, v347, v348 uintptr var yyact uint16 var v349 TToken var _ /* all at bp+64 */ TToken var _ /* as at bp+48 */ TToken var _ /* dest at bp+8 */ TSelectDest var _ /* t at bp+56 */ TToken var _ /* x at bp+40 */ TToken var _ /* yylhsminor at bp+0 */ TYYMINORTYPE _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bNot, bNot1, n, nExpr, p, p1, p2, p3, p4, pDot, pFrom, pFromClause, pLeft, pLeft1, pLhs, pList, pList1, pList2, pList3, pList4, pNew, pOld, pRHS, pRhs, pRight, pRight1, pSelect, pSelectRHS, pSrc, pSubquery, pSubquery1, temp1, temp11, temp2, temp21, temp3, temp4, yyact, yygoto, yymsp, yysize, v343, v344, v345, v346, v347, v348, v349 /* Amount to pop the stack */ _ = yyLookahead _ = yyLookaheadToken yymsp = (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos switch yyruleno { case uint32(0): goto _1 case uint32(1): goto _2 case uint32(2): goto _3 case uint32(3): goto _4 case uint32(4): goto _5 case uint32(6): goto _6 case uint32(5): goto _7 case uint32(7): goto _8 case uint32(321): goto _9 case uint32(9): goto _10 case uint32(8): goto _11 case uint32(10): goto _12 case uint32(11): goto _13 case uint32(12): goto _14 case uint32(13): goto _15 case uint32(14): goto _16 case uint32(18): goto _17 case uint32(15): goto _18 case uint32(47): goto _19 case uint32(62): goto _20 case uint32(72): goto _21 case uint32(81): goto _22 case uint32(98): goto _23 case uint32(244): goto _24 case uint32(16): goto _25 case uint32(17): goto _26 case uint32(19): goto _27 case uint32(20): goto _28 case uint32(21): goto _29 case uint32(22): goto _30 case uint32(23): goto _31 case uint32(24): goto _32 case uint32(25): goto _33 case uint32(65): goto _34 case uint32(26): goto _35 case uint32(104): goto _36 case uint32(27): goto _37 case uint32(28): goto _38 case uint32(29): goto _39 case uint32(30): goto _40 case uint32(31): goto _41 case uint32(67): goto _42 case uint32(32): goto _43 case uint32(33): goto _44 case uint32(34): goto _45 case uint32(35): goto _46 case uint32(36): goto _47 case uint32(37): goto _48 case uint32(38): goto _49 case uint32(39): goto _50 case uint32(40): goto _51 case uint32(41): goto _52 case uint32(42): goto _53 case uint32(43): goto _54 case uint32(44): goto _55 case uint32(45): goto _56 case uint32(46): goto _57 case uint32(48): goto _58 case uint32(49): goto _59 case uint32(50): goto _60 case uint32(51): goto _61 case uint32(52): goto _62 case uint32(53): goto _63 case uint32(54): goto _64 case uint32(55): goto _65 case uint32(56): goto _66 case uint32(57): goto _67 case uint32(58): goto _68 case uint32(59): goto _69 case uint32(60): goto _70 case uint32(76): goto _71 case uint32(61): goto _72 case uint32(171): goto _73 case uint32(80): goto _74 case uint32(63): goto _75 case uint32(217): goto _76 case uint32(220): goto _77 case uint32(245): goto _78 case uint32(64): goto _79 case uint32(66): goto _80 case uint32(68): goto _81 case uint32(69): goto _82 case uint32(70): goto _83 case uint32(71): goto _84 case uint32(75): goto _85 case uint32(73): goto _86 case uint32(74): goto _87 case uint32(77): goto _88 case uint32(172): goto _89 case uint32(78): goto _90 case uint32(79): goto _91 case uint32(82): goto _92 case uint32(83): goto _93 case uint32(84): goto _94 case uint32(85): goto _95 case uint32(86): goto _96 case uint32(87): goto _97 case uint32(88): goto _98 case uint32(91): goto _99 case uint32(89): goto _100 case uint32(90): goto _101 case uint32(92): goto _102 case uint32(93): goto _103 case uint32(94): goto _104 case uint32(95): goto _105 case uint32(96): goto _106 case uint32(97): goto _107 case uint32(132): goto _108 case uint32(99): goto _109 case uint32(142): goto _110 case uint32(232): goto _111 case uint32(235): goto _112 case uint32(240): goto _113 case uint32(100): goto _114 case uint32(101): goto _115 case uint32(102): goto _116 case uint32(115): goto _117 case uint32(103): goto _118 case uint32(256): goto _119 case uint32(257): goto _120 case uint32(108): goto _121 case uint32(105): goto _122 case uint32(106): goto _123 case uint32(107): goto _124 case uint32(109): goto _125 case uint32(110): goto _126 case uint32(111): goto _127 case uint32(112): goto _128 case uint32(113): goto _129 case uint32(129): goto _130 case uint32(114): goto _131 case uint32(116): goto _132 case uint32(117): goto _133 case uint32(118): goto _134 case uint32(119): goto _135 case uint32(120): goto _136 case uint32(121): goto _137 case uint32(122): goto _138 case uint32(123): goto _139 case uint32(124): goto _140 case uint32(125): goto _141 case uint32(126): goto _142 case uint32(127): goto _143 case uint32(128): goto _144 case uint32(130): goto _145 case uint32(131): goto _146 case uint32(143): goto _147 case uint32(133): goto _148 case uint32(134): goto _149 case uint32(135): goto _150 case uint32(136): goto _151 case uint32(137): goto _152 case uint32(141): goto _153 case uint32(138): goto _154 case uint32(139): goto _155 case uint32(140): goto _156 case uint32(146): goto _157 case uint32(144): goto _158 case uint32(151): goto _159 case uint32(153): goto _160 case uint32(230): goto _161 case uint32(231): goto _162 case uint32(250): goto _163 case uint32(152): goto _164 case uint32(145): goto _165 case uint32(154): goto _166 case uint32(229): goto _167 case uint32(249): goto _168 case uint32(147): goto _169 case uint32(148): goto _170 case uint32(149): goto _171 case uint32(150): goto _172 case uint32(155): goto _173 case uint32(156): goto _174 case uint32(157): goto _175 case uint32(158): goto _176 case uint32(159): goto _177 case uint32(160): goto _178 case uint32(161): goto _179 case uint32(162): goto _180 case uint32(163): goto _181 case uint32(164): goto _182 case uint32(165): goto _183 case uint32(166): goto _184 case uint32(167): goto _185 case uint32(168): goto _186 case uint32(169): goto _187 case uint32(170): goto _188 case uint32(173): goto _189 case uint32(174): goto _190 case uint32(175): goto _191 case uint32(176): goto _192 case uint32(177): goto _193 case uint32(178): goto _194 case uint32(179): goto _195 case uint32(180): goto _196 case uint32(182): goto _197 case uint32(181): goto _198 case uint32(183): goto _199 case uint32(184): goto _200 case uint32(185): goto _201 case uint32(186): goto _202 case uint32(187): goto _203 case uint32(188): goto _204 case uint32(189): goto _205 case uint32(190): goto _206 case uint32(191): goto _207 case uint32(192): goto _208 case uint32(193): goto _209 case uint32(194): goto _210 case uint32(195): goto _211 case uint32(197): goto _212 case uint32(196): goto _213 case uint32(198): goto _214 case uint32(199): goto _215 case uint32(200): goto _216 case uint32(201): goto _217 case uint32(202): goto _218 case uint32(203): goto _219 case uint32(204): goto _220 case uint32(205): goto _221 case uint32(206): goto _222 case uint32(207): goto _223 case uint32(208): goto _224 case uint32(209): goto _225 case uint32(210): goto _226 case uint32(211): goto _227 case uint32(213): goto _228 case uint32(212): goto _229 case uint32(214): goto _230 case uint32(215): goto _231 case uint32(219): goto _232 case uint32(216): goto _233 case uint32(218): goto _234 case uint32(221): goto _235 case uint32(222): goto _236 case uint32(223): goto _237 case uint32(224): goto _238 case uint32(225): goto _239 case uint32(226): goto _240 case uint32(227): goto _241 case uint32(228): goto _242 case uint32(233): goto _243 case uint32(234): goto _244 case uint32(241): goto _245 case uint32(236): goto _246 case uint32(237): goto _247 case uint32(280): goto _248 case uint32(238): goto _249 case uint32(239): goto _250 case uint32(242): goto _251 case uint32(243): goto _252 case uint32(246): goto _253 case uint32(247): goto _254 case uint32(248): goto _255 case uint32(251): goto _256 case uint32(252): goto _257 case uint32(253): goto _258 case uint32(254): goto _259 case uint32(255): goto _260 case uint32(258): goto _261 case uint32(259): goto _262 case uint32(260): goto _263 case uint32(261): goto _264 case uint32(262): goto _265 case uint32(264): goto _266 case uint32(263): goto _267 case uint32(265): goto _268 case uint32(285): goto _269 case uint32(266): goto _270 case uint32(286): goto _271 case uint32(267): goto _272 case uint32(268): goto _273 case uint32(269): goto _274 case uint32(270): goto _275 case uint32(271): goto _276 case uint32(272): goto _277 case uint32(273): goto _278 case uint32(274): goto _279 case uint32(275): goto _280 case uint32(276): goto _281 case uint32(277): goto _282 case uint32(278): goto _283 case uint32(279): goto _284 case uint32(281): goto _285 case uint32(282): goto _286 case uint32(283): goto _287 case uint32(284): goto _288 case uint32(287): goto _289 case uint32(288): goto _290 case uint32(289): goto _291 case uint32(290): goto _292 case uint32(291): goto _293 case uint32(292): goto _294 case uint32(293): goto _295 case uint32(294): goto _296 case uint32(295): goto _297 case uint32(296): goto _298 case uint32(297): goto _299 case uint32(298): goto _300 case uint32(299): goto _301 case uint32(301): goto _302 case uint32(300): goto _303 case uint32(302): goto _304 case uint32(304): goto _305 case uint32(303): goto _306 case uint32(305): goto _307 case uint32(306): goto _308 case uint32(307): goto _309 case uint32(308): goto _310 case uint32(309): goto _311 case uint32(310): goto _312 case uint32(311): goto _313 case uint32(312): goto _314 case uint32(313): goto _315 case uint32(314): goto _316 case uint32(315): goto _317 case uint32(316): goto _318 case uint32(317): goto _319 case uint32(318): goto _320 case uint32(319): goto _321 case uint32(320): goto _322 case uint32(324): goto _323 case uint32(322): goto _324 case uint32(325): goto _325 case uint32(323): goto _326 case uint32(327): goto _327 case uint32(326): goto _328 case uint32(328): goto _329 case uint32(329): goto _330 case uint32(331): goto _331 case uint32(330): goto _332 case uint32(332): goto _333 case uint32(333): goto _334 case uint32(334): goto _335 case uint32(335): goto _336 case uint32(336): goto _337 case uint32(337): goto _338 case uint32(338): goto _339 case uint32(339): goto _340 default: goto _341 } goto _342 _1: ; /* explain ::= EXPLAIN */ if (*TParse)(unsafe.Pointer(pParse)).FpReprepare == uintptr(0) { (*TParse)(unsafe.Pointer(pParse)).Fexplain = uint8(1) } goto _342 _2: ; /* explain ::= EXPLAIN QUERY PLAN */ if (*TParse)(unsafe.Pointer(pParse)).FpReprepare == uintptr(0) { (*TParse)(unsafe.Pointer(pParse)).Fexplain = uint8(2) } goto _342 _3: ; /* cmdx ::= cmd */ _sqlite3FinishCoding(tls, pParse) goto _342 _4: ; /* cmd ::= BEGIN transtype trans_opt */ _sqlite3BeginTransaction(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) goto _342 _5: ; /* transtype ::= */ *(*int32)(unsafe.Pointer(yymsp + 1*12 + 4)) = int32(TK_DEFERRED) goto _342 _7: ; /* transtype ::= DEFERRED */ _6: ; _8: ; _9: ; *(*int32)(unsafe.Pointer(yymsp + 4)) = int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor) /*A-overwrites-X*/ goto _342 _11: ; /* cmd ::= COMMIT|END trans_opt */ _10: ; _sqlite3EndTransaction(tls, pParse, int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12))).Fmajor)) goto _342 _12: ; /* cmd ::= SAVEPOINT nm */ _sqlite3Savepoint(tls, pParse, SAVEPOINT_BEGIN, yymsp+4) goto _342 _13: ; /* cmd ::= RELEASE savepoint_opt nm */ _sqlite3Savepoint(tls, pParse, int32(SAVEPOINT_RELEASE), yymsp+4) goto _342 _14: ; /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */ _sqlite3Savepoint(tls, pParse, int32(SAVEPOINT_ROLLBACK), yymsp+4) goto _342 _15: ; /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */ _sqlite3StartTable(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, yymsp+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), 0, 0, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) goto _342 _16: ; /* createkw ::= CREATE */ _disableLookaside(tls, pParse) goto _342 _18: ; /* ifnotexists ::= */ _17: ; _19: ; _20: ; _21: ; _22: ; _23: ; _24: ; *(*int32)(unsafe.Pointer(yymsp + 1*12 + 4)) = 0 goto _342 _25: ; /* ifnotexists ::= IF NOT EXISTS */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = int32(1) goto _342 _26: ; /* temp ::= TEMP */ *(*int32)(unsafe.Pointer(yymsp + 4)) = libc.BoolInt32(int32((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Finit1.Fbusy) == 0) goto _342 _27: ; /* create_table_args ::= LP columnlist conslist_opt RP table_option_set */ _sqlite3EndTable(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, *(*Tu32)(unsafe.Pointer(yymsp + 4)), uintptr(0)) goto _342 _28: ; /* create_table_args ::= AS select */ _sqlite3EndTable(tls, pParse, uintptr(0), uintptr(0), uint32(0), *(*uintptr)(unsafe.Pointer(yymsp + 4))) _sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _29: ; /* table_option_set ::= */ *(*Tu32)(unsafe.Pointer(yymsp + 1*12 + 4)) = uint32(0) goto _342 _30: ; /* table_option_set ::= table_option_set COMMA table_option */ *(*Tu32)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = *(*Tu32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) | *(*Tu32)(unsafe.Pointer(yymsp + 4)) *(*Tu32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*Tu32)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _31: ; /* table_option ::= WITHOUT nm */ if *(*uint32)(unsafe.Pointer(yymsp + 4 + 4)) == uint32(5) && Xsqlite3_strnicmp(tls, *(*uintptr)(unsafe.Pointer(yymsp + 4)), __ccgo_ts+16694, int32(5)) == 0 { *(*Tu32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = uint32(libc.Int32FromInt32(TF_WithoutRowid) | libc.Int32FromInt32(TF_NoVisibleRowid)) } else { *(*Tu32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = uint32(0) _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23358, libc.VaList(bp+80, *(*uint32)(unsafe.Pointer(yymsp + 4 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))) } goto _342 _32: ; /* table_option ::= nm */ if *(*uint32)(unsafe.Pointer(yymsp + 4 + 4)) == uint32(6) && Xsqlite3_strnicmp(tls, *(*uintptr)(unsafe.Pointer(yymsp + 4)), __ccgo_ts+16607, int32(6)) == 0 { *(*Tu32)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = uint32(TF_Strict) } else { *(*Tu32)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = uint32(0) _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23358, libc.VaList(bp+80, *(*uint32)(unsafe.Pointer(yymsp + 4 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))) } *(*Tu32)(unsafe.Pointer(yymsp + 4)) = *(*Tu32)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _33: ; /* columnname ::= nm typetoken */ _sqlite3AddColumn(tls, pParse, *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*TToken)(unsafe.Pointer(yymsp + 4))) goto _342 _35: ; /* typetoken ::= */ _34: ; _36: ; *(*uint32)(unsafe.Pointer(yymsp + 1*12 + 4 + 4)) = uint32(0) *(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0) goto _342 _37: ; /* typetoken ::= typename LP signed RP */ *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4 + 4)) = uint32(t__predefined_ptrdiff_t(*(*uintptr)(unsafe.Pointer(yymsp + 4))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + 4 + 4)))) - int32(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)))) goto _342 _38: ; /* typetoken ::= typename LP signed COMMA signed RP */ *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4 + 4)) = uint32(t__predefined_ptrdiff_t(*(*uintptr)(unsafe.Pointer(yymsp + 4))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + 4 + 4)))) - int32(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)))) goto _342 _39: ; /* typename ::= typename ID|STRING */ *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) = *(*uint32)(unsafe.Pointer(yymsp + 4 + 4)) + uint32(int32(*(*uintptr)(unsafe.Pointer(yymsp + 4)))-int32(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))) goto _342 _40: ; /* scanpt ::= */ _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = yyLookaheadToken.Fz goto _342 _41: ; /* scantok ::= */ _ = libc.Int32FromInt32(0) *(*TToken)(unsafe.Pointer(yymsp + 1*12 + 4)) = yyLookaheadToken goto _342 _43: ; /* ccons ::= CONSTRAINT nm */ _42: ; (*TParse)(unsafe.Pointer(pParse)).FconstraintName = *(*TToken)(unsafe.Pointer(yymsp + 4)) goto _342 _44: ; /* ccons ::= DEFAULT scantok term */ _sqlite3AddDefaultValue(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)))) goto _342 _45: ; /* ccons ::= DEFAULT LP expr RP */ _sqlite3AddDefaultValue(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))+uintptr(1), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _46: ; /* ccons ::= DEFAULT PLUS scantok term */ _sqlite3AddDefaultValue(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)))) goto _342 _47: ; /* ccons ::= DEFAULT MINUS scantok term */ p = _sqlite3PExpr(tls, pParse, int32(TK_UMINUS), *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0)) _sqlite3AddDefaultValue(tls, pParse, p, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)))) goto _342 _48: ; /* ccons ::= DEFAULT scantok ID|INDEXED */ p1 = _tokenExpr(tls, pParse, int32(TK_STRING), *(*TToken)(unsafe.Pointer(yymsp + 4))) if p1 != 0 { _sqlite3ExprIdToTrueFalse(tls, p1) } _sqlite3AddDefaultValue(tls, pParse, p1, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + 4 + 4)))) goto _342 _49: ; /* ccons ::= NOT NULL onconf */ _sqlite3AddNotNull(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + 4))) goto _342 _50: ; /* ccons ::= PRIMARY KEY sortorder onconf autoinc */ _sqlite3AddPrimaryKey(tls, pParse, uintptr(0), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) goto _342 _51: ; /* ccons ::= UNIQUE onconf */ _sqlite3CreateIndex(tls, pParse, uintptr(0), uintptr(0), uintptr(0), uintptr(0), *(*int32)(unsafe.Pointer(yymsp + 4)), uintptr(0), uintptr(0), 0, 0, uint8(SQLITE_IDXTYPE_UNIQUE)) goto _342 _52: ; /* ccons ::= CHECK LP expr RP */ _sqlite3AddCheckConstraint(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _53: ; /* ccons ::= REFERENCES nm eidlist_opt refargs */ _sqlite3CreateForeignKey(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4))) goto _342 _54: ; /* ccons ::= defer_subclause */ _sqlite3DeferForeignKey(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + 4))) goto _342 _55: ; /* ccons ::= COLLATE ID|STRING */ _sqlite3AddCollateType(tls, pParse, yymsp+4) goto _342 _56: ; /* generated ::= LP expr RP */ _sqlite3AddGenerated(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uintptr(0)) goto _342 _57: ; /* generated ::= LP expr RP ID */ _sqlite3AddGenerated(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), yymsp+4) goto _342 _58: ; /* autoinc ::= AUTOINCR */ *(*int32)(unsafe.Pointer(yymsp + 4)) = int32(1) goto _342 _59: ; /* refargs ::= */ *(*int32)(unsafe.Pointer(yymsp + 1*12 + 4)) = libc.Int32FromInt32(OE_None) * libc.Int32FromInt32(0x0101) /* EV: R-19803-45884 */ goto _342 _60: ; /* refargs ::= refargs refarg */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) & ^*(*int32)(unsafe.Pointer(yymsp + 4 + 4)) | *(*int32)(unsafe.Pointer(yymsp + 4)) goto _342 _61: ; /* refarg ::= MATCH nm */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = 0 *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) = 0x000000 goto _342 _62: ; /* refarg ::= ON INSERT refact */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = 0 *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4 + 4)) = 0x000000 goto _342 _63: ; /* refarg ::= ON DELETE refact */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*int32)(unsafe.Pointer(yymsp + 4)) *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4 + 4)) = int32(0x0000ff) goto _342 _64: ; /* refarg ::= ON UPDATE refact */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*int32)(unsafe.Pointer(yymsp + 4)) << int32(8) *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4 + 4)) = int32(0x00ff00) goto _342 _65: ; /* refact ::= SET NULL */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = int32(OE_SetNull) /* EV: R-33326-45252 */ goto _342 _66: ; /* refact ::= SET DEFAULT */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = int32(OE_SetDflt) /* EV: R-33326-45252 */ goto _342 _67: ; /* refact ::= CASCADE */ *(*int32)(unsafe.Pointer(yymsp + 4)) = int32(OE_Cascade) /* EV: R-33326-45252 */ goto _342 _68: ; /* refact ::= RESTRICT */ *(*int32)(unsafe.Pointer(yymsp + 4)) = int32(OE_Restrict) /* EV: R-33326-45252 */ goto _342 _69: ; /* refact ::= NO ACTION */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = OE_None /* EV: R-33326-45252 */ goto _342 _70: ; /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = 0 goto _342 _72: ; /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */ _71: ; _73: ; *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*int32)(unsafe.Pointer(yymsp + 4)) goto _342 _75: ; /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ _74: ; _76: ; _77: ; _78: ; *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = int32(1) goto _342 _79: ; /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = 0 goto _342 _80: ; /* tconscomma ::= COMMA */ (*TParse)(unsafe.Pointer(pParse)).FconstraintName.Fn = uint32(0) goto _342 _81: ; /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */ _sqlite3AddPrimaryKey(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), 0) goto _342 _82: ; /* tcons ::= UNIQUE LP sortlist RP onconf */ _sqlite3CreateIndex(tls, pParse, uintptr(0), uintptr(0), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4)), uintptr(0), uintptr(0), 0, 0, uint8(SQLITE_IDXTYPE_UNIQUE)) goto _342 _83: ; /* tcons ::= CHECK LP expr RP onconf */ _sqlite3AddCheckConstraint(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) goto _342 _84: ; /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */ _sqlite3CreateForeignKey(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) _sqlite3DeferForeignKey(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + 4))) goto _342 _86: ; /* onconf ::= */ _85: ; *(*int32)(unsafe.Pointer(yymsp + 1*12 + 4)) = int32(OE_Default) goto _342 _87: ; /* onconf ::= ON CONFLICT resolvetype */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*int32)(unsafe.Pointer(yymsp + 4)) goto _342 _88: ; /* resolvetype ::= IGNORE */ *(*int32)(unsafe.Pointer(yymsp + 4)) = int32(OE_Ignore) goto _342 _90: ; /* resolvetype ::= REPLACE */ _89: ; *(*int32)(unsafe.Pointer(yymsp + 4)) = int32(OE_Replace) goto _342 _91: ; /* cmd ::= DROP TABLE ifexists fullname */ _sqlite3DropTable(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), 0, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) goto _342 _92: ; /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */ _sqlite3CreateView(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(8))*12+4, yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4))) goto _342 _93: ; /* cmd ::= DROP VIEW ifexists fullname */ _sqlite3DropTable(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), int32(1), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) goto _342 _94: ; /* cmd ::= select */ *(*TSelectDest)(unsafe.Pointer(bp + 8)) = TSelectDest{ FeDest: uint8(SRT_Output), } _sqlite3Select(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), bp+8) _sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _95: ; /* select ::= WITH wqlist selectnowith */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _attachWithToSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) goto _342 _96: ; /* select ::= WITH RECURSIVE wqlist selectnowith */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _attachWithToSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) goto _342 _97: ; /* select ::= selectnowith */ p2 = *(*uintptr)(unsafe.Pointer(yymsp + 4)) if p2 != 0 { _parserDoubleLinkSelect(tls, pParse, p2) } goto _342 _98: ; /* selectnowith ::= selectnowith multiselect_op oneselect */ pRhs = *(*uintptr)(unsafe.Pointer(yymsp + 4)) pLhs = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) if pRhs != 0 && (*TSelect)(unsafe.Pointer(pRhs)).FpPrior != 0 { (*(*TToken)(unsafe.Pointer(bp + 40))).Fn = uint32(0) _parserDoubleLinkSelect(tls, pParse, pRhs) pFrom = _sqlite3SrcListAppendFromTerm(tls, pParse, uintptr(0), uintptr(0), uintptr(0), bp+40, pRhs, uintptr(0)) pRhs = _sqlite3SelectNew(tls, pParse, uintptr(0), pFrom, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(0), uintptr(0)) } if pRhs != 0 { (*TSelect)(unsafe.Pointer(pRhs)).Fop = uint8(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) (*TSelect)(unsafe.Pointer(pRhs)).FpPrior = pLhs if pLhs != 0 { *(*Tu32)(unsafe.Pointer(pLhs + 4)) &= uint32(^libc.Int32FromInt32(SF_MultiValue)) } *(*Tu32)(unsafe.Pointer(pRhs + 4)) &= uint32(^libc.Int32FromInt32(SF_MultiValue)) if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) != int32(TK_ALL) { (*TParse)(unsafe.Pointer(pParse)).FhasCompound = uint8(1) } } else { _sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pLhs) } *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = pRhs goto _342 _100: ; /* multiselect_op ::= UNION */ _99: ; *(*int32)(unsafe.Pointer(yymsp + 4)) = int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor) /*A-overwrites-OP*/ goto _342 _101: ; /* multiselect_op ::= UNION ALL */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = int32(TK_ALL) goto _342 _102: ; /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)) = _sqlite3SelectNew(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uint32(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4))), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _103: ; /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(9))*12 + 4)) = _sqlite3SelectNew(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uint32(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4))), *(*uintptr)(unsafe.Pointer(yymsp + 4))) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(9))*12 + 4)) != 0 { (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(9))*12 + 4)))).FpWinDefn = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) } else { _sqlite3WindowListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) } goto _342 _104: ; /* values ::= VALUES LP nexprlist RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3SelectNew(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(SF_Values), uintptr(0)) goto _342 _105: ; /* values ::= values COMMA LP nexprlist RP */ pLeft = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) pRight = _sqlite3SelectNew(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(libc.Int32FromInt32(SF_Values)|libc.Int32FromInt32(SF_MultiValue)), uintptr(0)) if pLeft != 0 { *(*Tu32)(unsafe.Pointer(pLeft + 4)) &= uint32(^libc.Int32FromInt32(SF_MultiValue)) } if pRight != 0 { (*TSelect)(unsafe.Pointer(pRight)).Fop = uint8(TK_ALL) (*TSelect)(unsafe.Pointer(pRight)).FpPrior = pLeft *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = pRight } else { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = pLeft } goto _342 _106: ; /* distinct ::= DISTINCT */ *(*int32)(unsafe.Pointer(yymsp + 4)) = int32(SF_Distinct) goto _342 _107: ; /* distinct ::= ALL */ *(*int32)(unsafe.Pointer(yymsp + 4)) = int32(SF_All) goto _342 _109: ; /* sclp ::= */ _108: ; _110: ; _111: ; _112: ; _113: ; *(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0) goto _342 _114: ; /* selcollist ::= sclp scanpt expr scanpt as */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) if *(*uint32)(unsafe.Pointer(yymsp + 4 + 4)) > uint32(0) { _sqlite3ExprListSetName(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), yymsp+4, int32(1)) } _sqlite3ExprListSetSpan(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) goto _342 _115: ; /* selcollist ::= sclp scanpt STAR */ p3 = _sqlite3Expr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_ASTERISK), uintptr(0)) _sqlite3ExprSetErrorOffset(tls, p3, int32(*(*uintptr)(unsafe.Pointer(yymsp + 4)))-int32((*TParse)(unsafe.Pointer(pParse)).FzTail)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), p3) goto _342 _116: ; /* selcollist ::= sclp scanpt nm DOT STAR */ pRight1 = _sqlite3PExpr(tls, pParse, int32(TK_ASTERISK), uintptr(0), uintptr(0)) _sqlite3ExprSetErrorOffset(tls, pRight1, int32(*(*uintptr)(unsafe.Pointer(yymsp + 4)))-int32((*TParse)(unsafe.Pointer(pParse)).FzTail)) pLeft1 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) pDot = _sqlite3PExpr(tls, pParse, int32(TK_DOT), pLeft1, pRight1) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), pDot) goto _342 _118: ; /* as ::= AS nm */ _117: ; _119: ; _120: ; *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*TToken)(unsafe.Pointer(yymsp + 4)) goto _342 _122: ; /* from ::= */ _121: ; *(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0) goto _342 _123: ; /* from ::= FROM seltablist */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + 4)) _sqlite3SrcListShiftJoinType(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) goto _342 _124: ; /* stl_prefix ::= seltablist joinop */ if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) != 0 && (*TSrcList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))).FnSrc > 0 { (*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) + 8 + uintptr((*TSrcList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))).FnSrc-int32(1))*72))).Ffg.Fjointype = uint8(*(*int32)(unsafe.Pointer(yymsp + 4))) } goto _342 _125: ; /* seltablist ::= stl_prefix nm dbnm as on_using */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, uintptr(0), yymsp+4) goto _342 _126: ; /* seltablist ::= stl_prefix nm dbnm as indexed_by on_using */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, uintptr(0), yymsp+4) _sqlite3SrcListIndexedBy(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4) goto _342 _127: ; /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(6))*12+4, yymsp+uintptr(-libc.Int32FromInt32(5))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, uintptr(0), yymsp+4) _sqlite3SrcListFuncArgs(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4))) goto _342 _128: ; /* seltablist ::= stl_prefix LP select RP as on_using */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), uintptr(0), uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), yymsp+4) goto _342 _129: ; /* seltablist ::= stl_prefix LP seltablist RP as on_using */ if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) == uintptr(0) && *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) == uint32(0) && *(*uintptr)(unsafe.Pointer(yymsp + 4)) == uintptr(0) && *(*uintptr)(unsafe.Pointer(yymsp + 4 + 4)) == uintptr(0) { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) } else { if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != uintptr(0) && (*TSrcList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)))).FnSrc == int32(1) { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), uintptr(0), uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, uintptr(0), yymsp+4) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) != 0 { pNew = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) + 8 + uintptr((*TSrcList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)))).FnSrc-int32(1))*72 pOld = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) + 8 (*TSrcItem)(unsafe.Pointer(pNew)).FzName = (*TSrcItem)(unsafe.Pointer(pOld)).FzName (*TSrcItem)(unsafe.Pointer(pNew)).FzDatabase = (*TSrcItem)(unsafe.Pointer(pOld)).FzDatabase (*TSrcItem)(unsafe.Pointer(pNew)).FpSelect = (*TSrcItem)(unsafe.Pointer(pOld)).FpSelect if (*TSrcItem)(unsafe.Pointer(pNew)).FpSelect != 0 && (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pNew)).FpSelect)).FselFlags&uint32(SF_NestedFrom) != uint32(0) { libc.SetBitFieldPtr16Uint32(pNew+36+4, libc.Uint32FromInt32(1), 13, 0x2000) } if int32(uint32(*(*uint16)(unsafe.Pointer(pOld + 36 + 4))&0x4>>2)) != 0 { *(*uintptr)(unsafe.Pointer(pNew + 64)) = *(*uintptr)(unsafe.Pointer(pOld + 64)) *(*uintptr)(unsafe.Pointer(pOld + 64)) = uintptr(0) libc.SetBitFieldPtr16Uint32(pOld+36+4, libc.Uint32FromInt32(0), 2, 0x4) libc.SetBitFieldPtr16Uint32(pNew+36+4, libc.Uint32FromInt32(1), 2, 0x4) } v343 = libc.UintptrFromInt32(0) (*TSrcItem)(unsafe.Pointer(pOld)).FzDatabase = v343 (*TSrcItem)(unsafe.Pointer(pOld)).FzName = v343 (*TSrcItem)(unsafe.Pointer(pOld)).FpSelect = uintptr(0) } _sqlite3SrcListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4))) } else { _sqlite3SrcListShiftJoinType(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4))) pSubquery = _sqlite3SelectNew(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(SF_NestedFrom), uintptr(0)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), uintptr(0), uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, pSubquery, yymsp+4) } } goto _342 _131: ; /* dbnm ::= */ _130: ; *(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0) *(*uint32)(unsafe.Pointer(yymsp + 1*12 + 4 + 4)) = uint32(0) goto _342 _132: ; /* fullname ::= nm */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+4, uintptr(0)) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) != 0 { _sqlite3RenameTokenMap(tls, pParse, (*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) + 8))).FzName, yymsp+4) } *(*uintptr)(unsafe.Pointer(yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _133: ; /* fullname ::= nm DOT nm */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+4) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) != 0 { _sqlite3RenameTokenMap(tls, pParse, (*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) + 8))).FzName, yymsp+4) } *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _134: ; /* xfullname ::= nm */ *(*uintptr)(unsafe.Pointer(yymsp + 4)) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+4, uintptr(0)) /*A-overwrites-X*/ goto _342 _135: ; /* xfullname ::= nm DOT nm */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+4) /*A-overwrites-X*/ goto _342 _136: ; /* xfullname ::= nm DOT nm AS nm */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4) /*A-overwrites-X*/ if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) != 0 { (*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) + 8))).FzAlias = _sqlite3NameFromToken(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, yymsp+4) } goto _342 _137: ; /* xfullname ::= nm AS nm */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, uintptr(0)) /*A-overwrites-X*/ if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) != 0 { (*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) + 8))).FzAlias = _sqlite3NameFromToken(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, yymsp+4) } goto _342 _138: ; /* joinop ::= COMMA|JOIN */ *(*int32)(unsafe.Pointer(yymsp + 4)) = int32(JT_INNER) goto _342 _139: ; /* joinop ::= JOIN_KW JOIN */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3JoinType(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, uintptr(0), uintptr(0)) /*X-overwrites-A*/ goto _342 _140: ; /* joinop ::= JOIN_KW nm JOIN */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3JoinType(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, uintptr(0)) /*X-overwrites-A*/ goto _342 _141: ; /* joinop ::= JOIN_KW nm nm JOIN */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3JoinType(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4) /*X-overwrites-A*/ goto _342 _142: ; /* on_using ::= ON expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + 4)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) = uintptr(0) goto _342 _143: ; /* on_using ::= USING LP idlist RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = uintptr(0) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) goto _342 _144: ; /* on_using ::= */ *(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0) *(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4 + 4)) = uintptr(0) goto _342 _145: ; /* indexed_by ::= INDEXED BY nm */ *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*TToken)(unsafe.Pointer(yymsp + 4)) goto _342 _146: ; /* indexed_by ::= NOT INDEXED */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = uintptr(0) *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) = uint32(1) goto _342 _148: ; /* orderby_opt ::= ORDER BY sortlist */ _147: ; *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + 4)) goto _342 _149: ; /* sortlist ::= sortlist COMMA expr sortorder nulls */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) _sqlite3ExprListSetSortOrder(tls, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4))) goto _342 _150: ; /* sortlist ::= expr sortorder nulls */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) /*A-overwrites-Y*/ _sqlite3ExprListSetSortOrder(tls, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4))) goto _342 _151: ; /* sortorder ::= ASC */ *(*int32)(unsafe.Pointer(yymsp + 4)) = SQLITE_SO_ASC goto _342 _152: ; /* sortorder ::= DESC */ *(*int32)(unsafe.Pointer(yymsp + 4)) = int32(SQLITE_SO_DESC) goto _342 _154: ; /* sortorder ::= */ _153: ; *(*int32)(unsafe.Pointer(yymsp + 1*12 + 4)) = -int32(1) goto _342 _155: ; /* nulls ::= NULLS FIRST */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = SQLITE_SO_ASC goto _342 _156: ; /* nulls ::= NULLS LAST */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = int32(SQLITE_SO_DESC) goto _342 _158: ; /* having_opt ::= */ _157: ; _159: ; _160: ; _161: ; _162: ; _163: ; *(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0) goto _342 _165: ; /* having_opt ::= HAVING expr */ _164: ; _166: ; _167: ; _168: ; *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + 4)) goto _342 _169: ; /* limit_opt ::= LIMIT expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_LIMIT), *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0)) goto _342 _170: ; /* limit_opt ::= LIMIT expr OFFSET expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_LIMIT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _171: ; /* limit_opt ::= LIMIT expr COMMA expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_LIMIT), *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) goto _342 _172: ; /* cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */ _sqlite3SrcListIndexedBy(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4) _sqlite3DeleteFrom(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0), uintptr(0)) goto _342 _173: ; /* where_opt_ret ::= RETURNING selcollist */ _sqlite3AddReturning(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = uintptr(0) goto _342 _174: ; /* where_opt_ret ::= WHERE expr RETURNING selcollist */ _sqlite3AddReturning(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) goto _342 _175: ; /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */ _sqlite3SrcListIndexedBy(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4) _sqlite3ExprListCheckLength(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), __ccgo_ts+23385) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) != 0 { pFromClause = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) if (*TSrcList)(unsafe.Pointer(pFromClause)).FnSrc > int32(1) { pSubquery1 = _sqlite3SelectNew(tls, pParse, uintptr(0), pFromClause, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(SF_NestedFrom), uintptr(0)) (*(*TToken)(unsafe.Pointer(bp + 48))).Fn = uint32(0) (*(*TToken)(unsafe.Pointer(bp + 48))).Fz = uintptr(0) pFromClause = _sqlite3SrcListAppendFromTerm(tls, pParse, uintptr(0), uintptr(0), uintptr(0), bp+48, pSubquery1, uintptr(0)) } *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3SrcListAppendList(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), pFromClause) } _sqlite3Update(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), uintptr(0), uintptr(0), uintptr(0)) goto _342 _176: ; /* setlist ::= setlist COMMA nm EQ expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) _sqlite3ExprListSetName(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, int32(1)) goto _342 _177: ; /* setlist ::= setlist COMMA LP idlist RP EQ expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)) = _sqlite3ExprListAppendVector(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _178: ; /* setlist ::= nm EQ expr */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 4))) _sqlite3ExprListSetName(tls, pParse, *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, int32(1)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _179: ; /* setlist ::= LP idlist RP EQ expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprListAppendVector(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _180: ; /* cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */ _sqlite3Insert(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _181: ; /* cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */ _sqlite3Insert(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), uintptr(0)) goto _342 _182: ; /* upsert ::= */ *(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0) goto _342 _183: ; /* upsert ::= RETURNING selcollist */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = uintptr(0) _sqlite3AddReturning(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _184: ; /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(11))*12 + 4)) = _sqlite3UpsertNew(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _185: ; /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)) = _sqlite3UpsertNew(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), uintptr(0), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _186: ; /* upsert ::= ON CONFLICT DO NOTHING returning */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3UpsertNew(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0)) goto _342 _187: ; /* upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)) = _sqlite3UpsertNew(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uintptr(0), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uintptr(0)) goto _342 _188: ; /* returning ::= RETURNING selcollist */ _sqlite3AddReturning(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _189: ; /* idlist_opt ::= */ *(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0) goto _342 _190: ; /* idlist_opt ::= LP idlist RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) goto _342 _191: ; /* idlist ::= idlist COMMA nm */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3IdListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), yymsp+4) goto _342 _192: ; /* idlist ::= nm */ *(*uintptr)(unsafe.Pointer(yymsp + 4)) = _sqlite3IdListAppend(tls, pParse, uintptr(0), yymsp+4) /*A-overwrites-Y*/ goto _342 _193: ; /* expr ::= LP expr RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) goto _342 _194: ; /* expr ::= ID|INDEXED|JOIN_KW */ *(*uintptr)(unsafe.Pointer(yymsp + 4)) = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + 4))) /*A-overwrites-X*/ goto _342 _195: ; /* expr ::= nm DOT nm */ temp1 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) temp2 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + 4))) *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3PExpr(tls, pParse, int32(TK_DOT), temp1, temp2) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _196: ; /* expr ::= nm DOT nm DOT nm */ temp11 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4))) temp21 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) temp3 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + 4))) temp4 = _sqlite3PExpr(tls, pParse, int32(TK_DOT), temp21, temp3) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenRemap(tls, pParse, uintptr(0), temp11) } *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3PExpr(tls, pParse, int32(TK_DOT), temp11, temp4) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _198: ; /* term ::= NULL|FLOAT|BLOB */ _197: ; *(*uintptr)(unsafe.Pointer(yymsp + 4)) = _tokenExpr(tls, pParse, int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor), *(*TToken)(unsafe.Pointer(yymsp + 4))) /*A-overwrites-X*/ goto _342 _199: ; /* term ::= INTEGER */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3ExprAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_INTEGER), yymsp+4, int32(1)) if *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) != 0 { *(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) + 36)) = int32(*(*uintptr)(unsafe.Pointer(yymsp + 4))) - int32((*TParse)(unsafe.Pointer(pParse)).FzTail) } *(*uintptr)(unsafe.Pointer(yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _200: ; /* expr ::= VARIABLE */ if !(int32(*(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + 4))))) == int32('#') && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + 4)) + 1))])&int32(0x04) != 0) { n = *(*uint32)(unsafe.Pointer(yymsp + 4 + 4)) *(*uintptr)(unsafe.Pointer(yymsp + 4)) = _tokenExpr(tls, pParse, int32(TK_VARIABLE), *(*TToken)(unsafe.Pointer(yymsp + 4))) _sqlite3ExprAssignVarNumber(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), n) } else { /* When doing a nested parse, one can include terms in an expression ** that look like this: #1 #2 ... These terms refer to registers ** in the virtual machine. #N is the N-th register. */ *(*TToken)(unsafe.Pointer(bp + 56)) = *(*TToken)(unsafe.Pointer(yymsp + 4)) /*A-overwrites-X*/ _ = libc.Int32FromInt32(0) if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23394, libc.VaList(bp+80, bp+56)) *(*uintptr)(unsafe.Pointer(yymsp + 4)) = uintptr(0) } else { *(*uintptr)(unsafe.Pointer(yymsp + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_REGISTER), uintptr(0), uintptr(0)) if *(*uintptr)(unsafe.Pointer(yymsp + 4)) != 0 { _sqlite3GetInt32(tls, (*(*TToken)(unsafe.Pointer(bp + 56))).Fz+1, *(*uintptr)(unsafe.Pointer(yymsp + 4))+28) } } } goto _342 _201: ; /* expr ::= expr COLLATE ID|STRING */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3ExprAddCollateToken(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), yymsp+4, int32(1)) goto _342 _202: ; /* expr ::= CAST LP expr AS typetoken RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3ExprAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_CAST), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, int32(1)) _sqlite3ExprAttachSubtrees(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), uintptr(0)) goto _342 _203: ; /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3ExprFunction(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _204: ; /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3ExprFunction(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(7))*12+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4))) _sqlite3ExprAddFunctionOrderBy(tls, pParse, *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _205: ; /* expr ::= ID|INDEXED|JOIN_KW LP STAR RP */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3ExprFunction(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, 0) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _206: ; /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3ExprFunction(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(5))*12+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4))) _sqlite3WindowAttach(tls, pParse, *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _207: ; /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP filter_over */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3ExprFunction(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(8))*12+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4))) _sqlite3WindowAttach(tls, pParse, *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + 4))) _sqlite3ExprAddFunctionOrderBy(tls, pParse, *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _208: ; /* expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3ExprFunction(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, 0) _sqlite3WindowAttach(tls, pParse, *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _209: ; /* term ::= CTIME_KW */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3ExprFunction(tls, pParse, uintptr(0), yymsp+4, 0) *(*uintptr)(unsafe.Pointer(yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _210: ; /* expr ::= LP nexprlist COMMA expr RP */ pList = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_VECTOR), uintptr(0), uintptr(0)) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) != 0 { *(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) + 20)) = pList if (*TExprList)(unsafe.Pointer(pList)).FnExpr != 0 { *(*Tu32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) + 4)) |= (*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pList + 8))).FpExpr)).Fflags & uint32(libc.Int32FromInt32(EP_Collate)|libc.Int32FromInt32(EP_Subquery)|libc.Int32FromInt32(EP_HasFunc)) } } else { _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pList) } goto _342 _211: ; /* expr ::= expr AND expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3ExprAnd(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _213: ; /* expr ::= expr OR expr */ _212: ; _214: ; _215: ; _216: ; _217: ; _218: ; *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12))).Fmajor), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _219: ; /* likeop ::= NOT LIKE_KW|MATCH */ *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*TToken)(unsafe.Pointer(yymsp + 4)) *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) |= uint32(0x80000000) /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/ goto _342 _220: ; /* expr ::= expr likeop expr */ bNot = int32(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) & uint32(0x80000000)) *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) &= uint32(0x7fffffff) pList1 = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 4))) pList1 = _sqlite3ExprListAppend(tls, pParse, pList1, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3ExprFunction(tls, pParse, pList1, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, 0) if bNot != 0 { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), uintptr(0)) } if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) != 0 { *(*Tu32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) + 4)) |= uint32(EP_InfixFunc) } goto _342 _221: ; /* expr ::= expr likeop expr ESCAPE expr */ bNot1 = int32(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4 + 4)) & uint32(0x80000000)) *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4 + 4)) &= uint32(0x7fffffff) pList2 = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) pList2 = _sqlite3ExprListAppend(tls, pParse, pList2, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4))) pList2 = _sqlite3ExprListAppend(tls, pParse, pList2, *(*uintptr)(unsafe.Pointer(yymsp + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprFunction(tls, pParse, pList2, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, 0) if bNot1 != 0 { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0)) } if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) != 0 { *(*Tu32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) + 4)) |= uint32(EP_InfixFunc) } goto _342 _222: ; /* expr ::= expr ISNULL|NOTNULL */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uintptr(0)) goto _342 _223: ; /* expr ::= expr NOT NULL */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_NOTNULL), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), uintptr(0)) goto _342 _224: ; /* expr ::= expr IS expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_IS), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) _binaryToUnaryIfNull(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), int32(TK_ISNULL)) goto _342 _225: ; /* expr ::= expr IS NOT expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_ISNOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) _binaryToUnaryIfNull(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), int32(TK_NOTNULL)) goto _342 _226: ; /* expr ::= expr IS NOT DISTINCT FROM expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_IS), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) _binaryToUnaryIfNull(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), int32(TK_ISNULL)) goto _342 _227: ; /* expr ::= expr IS DISTINCT FROM expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_ISNOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) _binaryToUnaryIfNull(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), int32(TK_NOTNULL)) goto _342 _229: ; /* expr ::= NOT expr */ _228: ; *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12))).Fmajor), *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0)) /*A-overwrites-B*/ goto _342 _230: ; /* expr ::= PLUS|MINUS expr */ if int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12))).Fmajor) == int32(TK_PLUS) { v344 = int32(TK_UPLUS) } else { v344 = int32(TK_UMINUS) } *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3PExpr(tls, pParse, v344, *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0)) /*A-overwrites-B*/ goto _342 _231: ; /* expr ::= expr PTR expr */ pList3 = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) pList3 = _sqlite3ExprListAppend(tls, pParse, pList3, *(*uintptr)(unsafe.Pointer(yymsp + 4))) *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3ExprFunction(tls, pParse, pList3, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, 0) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _233: ; /* between_op ::= BETWEEN */ _232: ; *(*int32)(unsafe.Pointer(yymsp + 4)) = 0 goto _342 _234: ; /* expr ::= expr between_op expr AND expr */ pList4 = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) pList4 = _sqlite3ExprListAppend(tls, pParse, pList4, *(*uintptr)(unsafe.Pointer(yymsp + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_BETWEEN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0)) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) != 0 { *(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) + 20)) = pList4 } else { _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pList4) } if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != 0 { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0)) } goto _342 _235: ; /* expr ::= expr in_op LP exprlist RP */ if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) == uintptr(0) { /* Expressions of the form ** ** expr1 IN () ** expr1 NOT IN () ** ** simplify to constants 0 (false) and 1 (true), respectively, ** regardless of the value of expr1. */ _sqlite3ExprUnmapAndDelete(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4))) if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != 0 { v345 = __ccgo_ts + 7886 } else { v345 = __ccgo_ts + 7891 } *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3Expr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_STRING), v345) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) != 0 { _sqlite3ExprIdToTrueFalse(tls, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4))) } } else { pRHS = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) + 8))).FpExpr if (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))).FnExpr == int32(1) && _sqlite3ExprIsConstant(tls, pRHS) != 0 && int32((*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))).Fop) != int32(TK_VECTOR) { (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) + 8))).FpExpr = uintptr(0) _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) pRHS = _sqlite3PExpr(tls, pParse, int32(TK_UPLUS), pRHS, uintptr(0)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_EQ), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), pRHS) } else { if (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))).FnExpr == int32(1) && int32((*TExpr)(unsafe.Pointer(pRHS)).Fop) == int32(TK_SELECT) { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_IN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0)) _sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(pRHS + 20))) *(*uintptr)(unsafe.Pointer(pRHS + 20)) = uintptr(0) _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) } else { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_IN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0)) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) == uintptr(0) { _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) } else { if int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))).FpLeft)).Fop) == int32(TK_VECTOR) { nExpr = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))).FpLeft + 20)))).FnExpr pSelectRHS = _sqlite3ExprListToValues(tls, pParse, nExpr, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) if pSelectRHS != 0 { _parserDoubleLinkSelect(tls, pParse, pSelectRHS) _sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), pSelectRHS) } } else { *(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) + 20)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) _sqlite3ExprSetHeightAndFlags(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4))) } } } } if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != 0 { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0)) } } goto _342 _236: ; /* expr ::= LP select RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_SELECT), uintptr(0), uintptr(0)) _sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) goto _342 _237: ; /* expr ::= expr in_op LP select RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_IN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0)) _sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != 0 { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0)) } goto _342 _238: ; /* expr ::= expr in_op nm dbnm paren_exprlist */ pSrc = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4) pSelect = _sqlite3SelectNew(tls, pParse, uintptr(0), pSrc, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(0), uintptr(0)) if *(*uintptr)(unsafe.Pointer(yymsp + 4)) != 0 { if pSelect != 0 { v346 = pSrc } else { v346 = uintptr(0) } _sqlite3SrcListFuncArgs(tls, pParse, v346, *(*uintptr)(unsafe.Pointer(yymsp + 4))) } *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_IN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0)) _sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), pSelect) if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != 0 { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0)) } goto _342 _239: ; /* expr ::= EXISTS LP select RP */ v347 = _sqlite3PExpr(tls, pParse, int32(TK_EXISTS), uintptr(0), uintptr(0)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = v347 p4 = v347 _sqlite3PExprAddSelect(tls, pParse, p4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) goto _342 _240: ; /* expr ::= CASE case_operand case_exprlist case_else END */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_CASE), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), uintptr(0)) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) != 0 { if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) != 0 { v348 = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) } else { v348 = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) } *(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) + 20)) = v348 _sqlite3ExprSetHeightAndFlags(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4))) } else { _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) } goto _342 _241: ; /* case_exprlist ::= case_exprlist WHEN expr THEN expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _242: ; /* case_exprlist ::= WHEN expr THEN expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _243: ; /* nexprlist ::= nexprlist COMMA expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _244: ; /* nexprlist ::= expr */ *(*uintptr)(unsafe.Pointer(yymsp + 4)) = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 4))) /*A-overwrites-Y*/ goto _342 _246: ; /* paren_exprlist ::= LP exprlist RP */ _245: ; *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) goto _342 _247: ; /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */ _sqlite3CreateIndex(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(7))*12+4, yymsp+uintptr(-libc.Int32FromInt32(6))*12+4, _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, uintptr(0)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(10))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(11))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + 4)), SQLITE_SO_ASC, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)), uint8(SQLITE_IDXTYPE_APPDEF)) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && (*TParse)(unsafe.Pointer(pParse)).FpNewIndex != 0 { _sqlite3RenameTokenMap(tls, pParse, (*TIndex)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).FpNewIndex)).FzName, yymsp+uintptr(-libc.Int32FromInt32(4))*12+4) } goto _342 _249: ; /* uniqueflag ::= UNIQUE */ _248: ; *(*int32)(unsafe.Pointer(yymsp + 4)) = int32(OE_Abort) goto _342 _250: ; /* uniqueflag ::= */ *(*int32)(unsafe.Pointer(yymsp + 1*12 + 4)) = OE_None goto _342 _251: ; /* eidlist ::= eidlist COMMA nm collate sortorder */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _parserAddExprIdListTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4))) goto _342 _252: ; /* eidlist ::= nm collate sortorder */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _parserAddExprIdListTerm(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4))) /*A-overwrites-Y*/ goto _342 _253: ; /* cmd ::= DROP INDEX ifexists fullname */ _sqlite3DropIndex(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) goto _342 _254: ; /* cmd ::= VACUUM vinto */ _sqlite3Vacuum(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _255: ; /* cmd ::= VACUUM nm vinto */ _sqlite3Vacuum(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _256: ; /* cmd ::= PRAGMA nm dbnm */ _sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, yymsp+4, uintptr(0), 0) goto _342 _257: ; /* cmd ::= PRAGMA nm dbnm EQ nmnum */ _sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+4, 0) goto _342 _258: ; /* cmd ::= PRAGMA nm dbnm LP nmnum RP */ _sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, 0) goto _342 _259: ; /* cmd ::= PRAGMA nm dbnm EQ minus_num */ _sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+4, int32(1)) goto _342 _260: ; /* cmd ::= PRAGMA nm dbnm LP minus_num RP */ _sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, int32(1)) goto _342 _261: ; /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */ (*(*TToken)(unsafe.Pointer(bp + 64))).Fz = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) (*(*TToken)(unsafe.Pointer(bp + 64))).Fn = uint32(int32(*(*uintptr)(unsafe.Pointer(yymsp + 4)))-int32(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)))) + *(*uint32)(unsafe.Pointer(yymsp + 4 + 4)) _sqlite3FinishTrigger(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), bp+64) goto _342 _262: ; /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */ _sqlite3BeginTrigger(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(7))*12+4, yymsp+uintptr(-libc.Int32FromInt32(6))*12+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(10))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4))) if *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4 + 4)) == uint32(0) { v349 = *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)) } else { v349 = *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)) } *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(10))*12 + 4)) = v349 /*A-overwrites-T*/ goto _342 _263: ; /* trigger_time ::= BEFORE|AFTER */ *(*int32)(unsafe.Pointer(yymsp + 4)) = int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor) /*A-overwrites-X*/ goto _342 _264: ; /* trigger_time ::= INSTEAD OF */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = int32(TK_INSTEAD) goto _342 _265: ; /* trigger_time ::= */ *(*int32)(unsafe.Pointer(yymsp + 1*12 + 4)) = int32(TK_BEFORE) goto _342 _267: ; /* trigger_event ::= DELETE|INSERT */ _266: ; *(*int32)(unsafe.Pointer(yymsp + 4)) = int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor) /*A-overwrites-X*/ *(*uintptr)(unsafe.Pointer(yymsp + 4 + 4)) = uintptr(0) goto _342 _268: ; /* trigger_event ::= UPDATE OF idlist */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = int32(TK_UPDATE) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + 4)) goto _342 _270: ; /* when_clause ::= */ _269: ; *(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0) goto _342 _272: ; /* when_clause ::= WHEN expr */ _271: ; *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + 4)) goto _342 _273: ; /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */ _ = libc.Int32FromInt32(0) (*TTriggerStep)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))).FpLast)).FpNext = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) (*TTriggerStep)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))).FpLast = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) goto _342 _274: ; /* trigger_cmd_list ::= trigger_cmd SEMI */ _ = libc.Int32FromInt32(0) (*TTriggerStep)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))).FpLast = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) goto _342 _275: ; /* trnm ::= nm DOT nm */ *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*TToken)(unsafe.Pointer(yymsp + 4)) _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23418, 0) goto _342 _276: ; /* tridxby ::= INDEXED BY nm */ _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23513, 0) goto _342 _277: ; /* tridxby ::= NOT INDEXED */ _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23597, 0) goto _342 _278: ; /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3TriggerUpdateStep(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(6))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uint8(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4))), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _279: ; /* trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3TriggerInsertStep(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), uint8(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4))), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) /*yylhsminor.yy33-overwrites-yymsp[-6].minor.yy394*/ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _280: ; /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3TriggerDeleteStep(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _281: ; /* trigger_cmd ::= scanpt select scanpt */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3TriggerSelectStep(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) /*yylhsminor.yy33-overwrites-yymsp[-1].minor.yy47*/ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _282: ; /* expr ::= RAISE LP IGNORE RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_RAISE), uintptr(0), uintptr(0)) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != 0 { (*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)))).FaffExpr = uint8(OE_Ignore) } goto _342 _283: ; /* expr ::= RAISE LP raisetype COMMA nm RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3ExprAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_RAISE), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, int32(1)) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) != 0 { (*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)))).FaffExpr = uint8(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4))) } goto _342 _284: ; /* raisetype ::= ROLLBACK */ *(*int32)(unsafe.Pointer(yymsp + 4)) = int32(OE_Rollback) goto _342 _285: ; /* raisetype ::= FAIL */ *(*int32)(unsafe.Pointer(yymsp + 4)) = int32(OE_Fail) goto _342 _286: ; /* cmd ::= DROP TRIGGER ifexists fullname */ _sqlite3DropTrigger(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) goto _342 _287: ; /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */ _sqlite3Attach(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _288: ; /* cmd ::= DETACH database_kw_opt expr */ _sqlite3Detach(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _289: ; /* cmd ::= REINDEX */ _sqlite3Reindex(tls, pParse, uintptr(0), uintptr(0)) goto _342 _290: ; /* cmd ::= REINDEX nm dbnm */ _sqlite3Reindex(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, yymsp+4) goto _342 _291: ; /* cmd ::= ANALYZE */ _sqlite3Analyze(tls, pParse, uintptr(0), uintptr(0)) goto _342 _292: ; /* cmd ::= ANALYZE nm dbnm */ _sqlite3Analyze(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, yymsp+4) goto _342 _293: ; /* cmd ::= ALTER TABLE fullname RENAME TO nm */ _sqlite3AlterRenameTable(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), yymsp+4) goto _342 _294: ; /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */ *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) = uint32(int32((*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fz)-int32(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))) + (*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fn _sqlite3AlterFinishAddColumn(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4) goto _342 _295: ; /* cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */ _sqlite3AlterDropColumn(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), yymsp+4) goto _342 _296: ; /* add_column_fullname ::= fullname */ _disableLookaside(tls, pParse) _sqlite3AlterBeginAddColumn(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _297: ; /* cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */ _sqlite3AlterRenameColumn(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+4) goto _342 _298: ; /* cmd ::= create_vtab */ _sqlite3VtabFinishParse(tls, pParse, uintptr(0)) goto _342 _299: ; /* cmd ::= create_vtab LP vtabarglist RP */ _sqlite3VtabFinishParse(tls, pParse, yymsp+4) goto _342 _300: ; /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */ _sqlite3VtabBeginParse(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4))) goto _342 _301: ; /* vtabarg ::= */ _sqlite3VtabArgInit(tls, pParse) goto _342 _303: ; /* vtabargtoken ::= ANY */ _302: ; _304: ; _sqlite3VtabArgExtend(tls, pParse, yymsp+4) goto _342 _306: ; /* with ::= WITH wqlist */ _305: ; _sqlite3WithPush(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), uint8(1)) goto _342 _307: ; /* wqas ::= AS */ *(*Tu8)(unsafe.Pointer(yymsp + 4)) = uint8(M10d_Any) goto _342 _308: ; /* wqas ::= AS MATERIALIZED */ *(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = uint8(M10d_Yes) goto _342 _309: ; /* wqas ::= AS NOT MATERIALIZED */ *(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = uint8(M10d_No) goto _342 _310: ; /* wqitem ::= nm eidlist_opt wqas LP select RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3CteNew(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(5))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4))) /*A-overwrites-X*/ goto _342 _311: ; /* wqlist ::= wqitem */ *(*uintptr)(unsafe.Pointer(yymsp + 4)) = _sqlite3WithAdd(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 4))) /*A-overwrites-X*/ goto _342 _312: ; /* wqlist ::= wqlist COMMA wqitem */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3WithAdd(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) goto _342 _313: ; /* windowdefn_list ::= windowdefn_list COMMA windowdefn */ _ = libc.Int32FromInt32(0) _sqlite3WindowChain(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + 4)))).FpNextWin = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = *(*uintptr)(unsafe.Pointer(yymsp + 4)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _314: ; /* windowdefn ::= nm AS LP window RP */ if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) != 0 { (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))).FzName = _sqlite3DbStrNDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uint64(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4 + 4)))) } *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _315: ; /* window ::= PARTITION BY nexprlist orderby_opt frame_opt */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uintptr(0)) goto _342 _316: ; /* window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(5))*12+4) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _317: ; /* window ::= ORDER BY sortlist frame_opt */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uintptr(0)) goto _342 _318: ; /* window ::= nm ORDER BY sortlist frame_opt */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _319: ; /* window ::= nm frame_opt */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0), uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _320: ; /* frame_opt ::= */ *(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = _sqlite3WindowAlloc(tls, pParse, 0, int32(TK_UNBOUNDED), uintptr(0), int32(TK_CURRENT), uintptr(0), uint8(0)) goto _342 _321: ; /* frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3WindowAlloc(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)), int32(TK_CURRENT), uintptr(0), *(*Tu8)(unsafe.Pointer(yymsp + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _322: ; /* frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3WindowAlloc(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)), *(*Tu8)(unsafe.Pointer(yymsp + 4))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _324: ; /* frame_bound_s ::= frame_bound */ _323: ; *(*TFrameBound)(unsafe.Pointer(bp)) = *(*TFrameBound)(unsafe.Pointer(yymsp + 4)) *(*TFrameBound)(unsafe.Pointer(yymsp + 4)) = *(*TFrameBound)(unsafe.Pointer(bp)) goto _342 _326: ; /* frame_bound_s ::= UNBOUNDED PRECEDING */ _325: ; _327: ; (*(*TFrameBound)(unsafe.Pointer(bp))).FeType = int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12))).Fmajor) (*(*TFrameBound)(unsafe.Pointer(bp))).FpExpr = uintptr(0) *(*TFrameBound)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*TFrameBound)(unsafe.Pointer(bp)) goto _342 _328: ; /* frame_bound ::= expr PRECEDING|FOLLOWING */ (*(*TFrameBound)(unsafe.Pointer(bp))).FeType = int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor) (*(*TFrameBound)(unsafe.Pointer(bp))).FpExpr = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) *(*TFrameBound)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*TFrameBound)(unsafe.Pointer(bp)) goto _342 _329: ; /* frame_exclude_opt ::= */ *(*Tu8)(unsafe.Pointer(yymsp + 1*12 + 4)) = uint8(0) goto _342 _330: ; /* frame_exclude_opt ::= EXCLUDE frame_exclude */ *(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*Tu8)(unsafe.Pointer(yymsp + 4)) goto _342 _332: ; /* frame_exclude ::= NO OTHERS */ _331: ; *(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = uint8((*(*TyyStackEntry)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12))).Fmajor) /*A-overwrites-X*/ goto _342 _333: ; /* frame_exclude ::= GROUP|TIES */ *(*Tu8)(unsafe.Pointer(yymsp + 4)) = uint8((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor) /*A-overwrites-X*/ goto _342 _334: ; /* window_clause ::= WINDOW windowdefn_list */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + 4)) goto _342 _335: ; /* filter_over ::= filter_clause over_clause */ if *(*uintptr)(unsafe.Pointer(yymsp + 4)) != 0 { (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + 4)))).FpFilter = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) } else { _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) } *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = *(*uintptr)(unsafe.Pointer(yymsp + 4)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _336: ; /* filter_over ::= over_clause */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = *(*uintptr)(unsafe.Pointer(yymsp + 4)) *(*uintptr)(unsafe.Pointer(yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _337: ; /* filter_over ::= filter_clause */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(100)) if *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) != 0 { (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))))).FeFrmType = uint8(TK_FILTER) (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))))).FpFilter = *(*uintptr)(unsafe.Pointer(yymsp + 4)) } else { _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + 4))) } *(*uintptr)(unsafe.Pointer(yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _342 _338: ; /* over_clause ::= OVER LP window RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) _ = libc.Int32FromInt32(0) goto _342 _339: ; /* over_clause ::= OVER nm */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(100)) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) != 0 { (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))).FzName = _sqlite3DbStrNDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + 4)), uint64(*(*uint32)(unsafe.Pointer(yymsp + 4 + 4)))) } goto _342 _340: ; /* filter_clause ::= FILTER LP WHERE expr RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) goto _342 _341: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) goto _342 /********** End reduce actions ************************************************/ _342: ; _ = libc.Int32FromInt32(0) yygoto = int32(_yyRuleInfoLhs[yyruleno]) yysize = int32(_yyRuleInfoNRhs[yyruleno]) yyact = _yy_find_reduce_action(tls, (*(*TyyStackEntry)(unsafe.Pointer(yymsp + uintptr(yysize)*12))).Fstateno, uint16(yygoto)) /* There are no SHIFTREDUCE actions on nonterminals because the table ** generator has simplified them to pure REDUCE actions. */ _ = libc.Int32FromInt32(0) /* It is not possible for a REDUCE to be followed by an error */ _ = libc.Int32FromInt32(0) yymsp += uintptr(yysize+int32(1)) * 12 (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos = yymsp (*TyyStackEntry)(unsafe.Pointer(yymsp)).Fstateno = yyact (*TyyStackEntry)(unsafe.Pointer(yymsp)).Fmajor = uint16(yygoto) return yyact } /* ** The following code executes when the parse fails */ // C documentation // // /* // ** The following code executes when a syntax error first occurs. // */ func _yy_syntax_error(tls *libc.TLS, yypParser uintptr, yymajor int32, _yyminor TToken) { bp := tls.Alloc(32) defer tls.Free(32) *(*TToken)(unsafe.Pointer(bp)) = _yyminor var pParse uintptr _ = pParse pParse = (*TyyParser)(unsafe.Pointer(yypParser)).FpParse /************ Begin %syntax_error code ****************************************/ _ = yymajor /* Silence some compiler warnings */ if *(*uint8)(unsafe.Pointer((*(*TToken)(unsafe.Pointer(bp))).Fz)) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23394, libc.VaList(bp+16, bp)) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23682, 0) } /************ End %syntax_error code ******************************************/ /* Suppress warning about unused %extra_argument variable */ (*TyyParser)(unsafe.Pointer(yypParser)).FpParse = pParse } // C documentation // // /* // ** The following is executed when the parser accepts // */ func _yy_accept(tls *libc.TLS, yypParser uintptr) { var pParse uintptr _ = pParse pParse = (*TyyParser)(unsafe.Pointer(yypParser)).FpParse _ = libc.Int32FromInt32(0) /* Here code is inserted which will be executed whenever the ** parser accepts */ /*********** Begin %parse_accept code *****************************************/ /*********** End %parse_accept code *******************************************/ /* Suppress warning about unused %extra_argument variable */ (*TyyParser)(unsafe.Pointer(yypParser)).FpParse = pParse } // C documentation // // /* The main parser program. // ** The first argument is a pointer to a structure obtained from // ** "sqlite3ParserAlloc" which describes the current state of the parser. // ** The second argument is the major token number. The third is // ** the minor token. The fourth optional argument is whatever the // ** user wants (and specified in the grammar) and is available for // ** use by the action routines. // ** // ** Inputs: // **
      // **
    • A pointer to the parser (an opaque structure.) // **
    • The major token number. // **
    • The minor token number. // **
    • An option argument of a grammar-specified type. // **
    // ** // ** Outputs: // ** None. // */ func _sqlite3Parser(tls *libc.TLS, yyp uintptr, yymajor int32, yyminor TToken) { bp := tls.Alloc(16) defer tls.Free(16) var pParse, yypParser uintptr var yyact uint16 var yyruleno uint32 var _ /* yyminorunion at bp+0 */ TYYMINORTYPE _, _, _, _ = pParse, yyact, yypParser, yyruleno /* The parser action. */ yypParser = yyp /* The parser */ pParse = (*TyyParser)(unsafe.Pointer(yypParser)).FpParse _ = libc.Int32FromInt32(0) yyact = (*TyyStackEntry)(unsafe.Pointer((*TyyParser)(unsafe.Pointer(yypParser)).Fyytos)).Fstateno for int32(1) != 0 { /* Exit by "break" */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) yyact = _yy_find_shift_action(tls, uint16(yymajor), yyact) if int32(yyact) >= int32(YY_MIN_REDUCE) { yyruleno = uint32(int32(yyact) - int32(YY_MIN_REDUCE)) /* Reduce by this rule */ /* Check that the stack is large enough to grow by a single entry ** if the RHS of the rule is empty. This ensures that there is room ** enough on the stack to push the LHS value */ if int32(_yyRuleInfoNRhs[yyruleno]) == 0 { if (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos >= (*TyyParser)(unsafe.Pointer(yypParser)).FyystackEnd { _yyStackOverflow(tls, yypParser) break } } yyact = _yy_reduce(tls, yypParser, yyruleno, yymajor, yyminor, pParse) } else { if int32(yyact) <= int32(YY_MAX_SHIFTREDUCE) { _yy_shift(tls, yypParser, yyact, uint16(yymajor), yyminor) break } else { if int32(yyact) == int32(YY_ACCEPT_ACTION) { (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos -= 12 _yy_accept(tls, yypParser) return } else { _ = libc.Int32FromInt32(0) *(*TToken)(unsafe.Pointer(bp)) = yyminor /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to ** do any kind of error recovery. Instead, simply invoke the syntax ** error routine and continue going as if nothing had happened. ** ** Applications can set this macro (for example inside %include) if ** they intend to abandon the parse upon the first syntax error seen. */ _yy_syntax_error(tls, yypParser, yymajor, yyminor) _yy_destructor(tls, yypParser, uint16(yymajor), bp) break } } } } return } // C documentation // // /* // ** Return the fallback token corresponding to canonical token iToken, or // ** 0 if iToken has no fallback. // */ func _sqlite3ParserFallback(tls *libc.TLS, iToken int32) (r int32) { _ = libc.Int32FromInt32(0) return int32(_yyFallback[iToken]) } /************** End of parse.c ***********************************************/ /************** Begin file tokenize.c ****************************************/ /* ** 2001 September 15 ** ** 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. ** ************************************************************************* ** An tokenizer for SQL ** ** This file contains C code that splits an SQL input string up into ** individual tokens and sends those tokens one-by-one over to the ** parser for analysis. */ /* #include "sqliteInt.h" */ /* #include */ /* Character classes for tokenizing ** ** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented ** using a lookup table, whereas a switch() directly on c uses a binary search. ** The lookup table is much faster. To maximize speed, and to ensure that ** a lookup table is used, all of the classes need to be small integers and ** all of them need to be used within the switch. */ var _aiClass = [256]uint8{ 0: uint8(29), 1: uint8(28), 2: uint8(28), 3: uint8(28), 4: uint8(28), 5: uint8(28), 6: uint8(28), 7: uint8(28), 8: uint8(28), 9: uint8(7), 10: uint8(7), 11: uint8(28), 12: uint8(7), 13: uint8(7), 14: uint8(28), 15: uint8(28), 16: uint8(28), 17: uint8(28), 18: uint8(28), 19: uint8(28), 20: uint8(28), 21: uint8(28), 22: uint8(28), 23: uint8(28), 24: uint8(28), 25: uint8(28), 26: uint8(28), 27: uint8(28), 28: uint8(28), 29: uint8(28), 30: uint8(28), 31: uint8(28), 32: uint8(7), 33: uint8(15), 34: uint8(8), 35: uint8(5), 36: uint8(4), 37: uint8(22), 38: uint8(24), 39: uint8(8), 40: uint8(17), 41: uint8(18), 42: uint8(21), 43: uint8(20), 44: uint8(23), 45: uint8(11), 46: uint8(26), 47: uint8(16), 48: uint8(3), 49: uint8(3), 50: uint8(3), 51: uint8(3), 52: uint8(3), 53: uint8(3), 54: uint8(3), 55: uint8(3), 56: uint8(3), 57: uint8(3), 58: uint8(5), 59: uint8(19), 60: uint8(12), 61: uint8(14), 62: uint8(13), 63: uint8(6), 64: uint8(5), 65: uint8(1), 66: uint8(1), 67: uint8(1), 68: uint8(1), 69: uint8(1), 70: uint8(1), 71: uint8(1), 72: uint8(1), 73: uint8(1), 74: uint8(1), 75: uint8(1), 76: uint8(1), 77: uint8(1), 78: uint8(1), 79: uint8(1), 80: uint8(1), 81: uint8(1), 82: uint8(1), 83: uint8(1), 84: uint8(1), 85: uint8(1), 86: uint8(1), 87: uint8(1), 89: uint8(2), 90: uint8(2), 91: uint8(9), 92: uint8(28), 93: uint8(28), 94: uint8(28), 95: uint8(2), 96: uint8(8), 97: uint8(1), 98: uint8(1), 99: uint8(1), 100: uint8(1), 101: uint8(1), 102: uint8(1), 103: uint8(1), 104: uint8(1), 105: uint8(1), 106: uint8(1), 107: uint8(1), 108: uint8(1), 109: uint8(1), 110: uint8(1), 111: uint8(1), 112: uint8(1), 113: uint8(1), 114: uint8(1), 115: uint8(1), 116: uint8(1), 117: uint8(1), 118: uint8(1), 119: uint8(1), 121: uint8(2), 122: uint8(2), 123: uint8(28), 124: uint8(10), 125: uint8(28), 126: uint8(25), 127: uint8(28), 128: uint8(27), 129: uint8(27), 130: uint8(27), 131: uint8(27), 132: uint8(27), 133: uint8(27), 134: uint8(27), 135: uint8(27), 136: uint8(27), 137: uint8(27), 138: uint8(27), 139: uint8(27), 140: uint8(27), 141: uint8(27), 142: uint8(27), 143: uint8(27), 144: uint8(27), 145: uint8(27), 146: uint8(27), 147: uint8(27), 148: uint8(27), 149: uint8(27), 150: uint8(27), 151: uint8(27), 152: uint8(27), 153: uint8(27), 154: uint8(27), 155: uint8(27), 156: uint8(27), 157: uint8(27), 158: uint8(27), 159: uint8(27), 160: uint8(27), 161: uint8(27), 162: uint8(27), 163: uint8(27), 164: uint8(27), 165: uint8(27), 166: uint8(27), 167: uint8(27), 168: uint8(27), 169: uint8(27), 170: uint8(27), 171: uint8(27), 172: uint8(27), 173: uint8(27), 174: uint8(27), 175: uint8(27), 176: uint8(27), 177: uint8(27), 178: uint8(27), 179: uint8(27), 180: uint8(27), 181: uint8(27), 182: uint8(27), 183: uint8(27), 184: uint8(27), 185: uint8(27), 186: uint8(27), 187: uint8(27), 188: uint8(27), 189: uint8(27), 190: uint8(27), 191: uint8(27), 192: uint8(27), 193: uint8(27), 194: uint8(27), 195: uint8(27), 196: uint8(27), 197: uint8(27), 198: uint8(27), 199: uint8(27), 200: uint8(27), 201: uint8(27), 202: uint8(27), 203: uint8(27), 204: uint8(27), 205: uint8(27), 206: uint8(27), 207: uint8(27), 208: uint8(27), 209: uint8(27), 210: uint8(27), 211: uint8(27), 212: uint8(27), 213: uint8(27), 214: uint8(27), 215: uint8(27), 216: uint8(27), 217: uint8(27), 218: uint8(27), 219: uint8(27), 220: uint8(27), 221: uint8(27), 222: uint8(27), 223: uint8(27), 224: uint8(27), 225: uint8(27), 226: uint8(27), 227: uint8(27), 228: uint8(27), 229: uint8(27), 230: uint8(27), 231: uint8(27), 232: uint8(27), 233: uint8(27), 234: uint8(27), 235: uint8(27), 236: uint8(27), 237: uint8(27), 238: uint8(27), 239: uint8(30), 240: uint8(27), 241: uint8(27), 242: uint8(27), 243: uint8(27), 244: uint8(27), 245: uint8(27), 246: uint8(27), 247: uint8(27), 248: uint8(27), 249: uint8(27), 250: uint8(27), 251: uint8(27), 252: uint8(27), 253: uint8(27), 254: uint8(27), 255: uint8(27), } /* ** The charMap() macro maps alphabetic characters (only) into their ** lower-case ASCII equivalent. On ASCII machines, this is just ** an upper-to-lower case map. On EBCDIC machines we also need ** to adjust the encoding. The mapping is only valid for alphabetics ** which are the only characters for which this feature is used. ** ** Used by keywordhash.h */ // C documentation // // /* // ** The sqlite3KeywordCode function looks up an identifier to determine if // ** it is a keyword. If it is a keyword, the token code of that keyword is // ** returned. If the input is not a keyword, TK_ID is returned. // ** // ** The implementation of this routine was generated by a program, // ** mkkeywordhash.c, located in the tool subdirectory of the distribution. // ** The output of the mkkeywordhash.c program is written into a file // ** named keywordhash.h and then included into this source file by // ** the #include below. // */ // /************** Include keywordhash.h in the middle of tokenize.c ************/ // /************** Begin file keywordhash.h *************************************/ // /***** This file contains automatically generated code ****** // ** // ** The code in this file has been automatically generated by // ** // ** sqlite/tool/mkkeywordhash.c // ** // ** The code in this file implements a function that determines whether // ** or not a given identifier is really an SQL keyword. The same thing // ** might be implemented more directly using a hand-written hash table. // ** But by using this automatically generated code, the size of the code // ** is substantially reduced. This is important for embedded applications // ** on platforms with limited memory. // */ // /* Hash score: 231 */ // /* zKWText[] encodes 1007 bytes of keyword text in 667 bytes */ // /* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */ // /* ABLEFTHENDEFERRABLELSEXCLUDELETEMPORARYISNULLSAVEPOINTERSECT */ // /* IESNOTNULLIKEXCEPTRANSACTIONATURALTERAISEXCLUSIVEXISTS */ // /* CONSTRAINTOFFSETRIGGERANGENERATEDETACHAVINGLOBEGINNEREFERENCES */ // /* UNIQUERYWITHOUTERELEASEATTACHBETWEENOTHINGROUPSCASCADEFAULT */ // /* CASECOLLATECREATECURRENT_DATEIMMEDIATEJOINSERTMATCHPLANALYZE */ // /* PRAGMATERIALIZEDEFERREDISTINCTUPDATEVALUESVIRTUALWAYSWHENWHERE */ // /* CURSIVEABORTAFTERENAMEANDROPARTITIONAUTOINCREMENTCASTCOLUMN */ // /* COMMITCONFLICTCROSSCURRENT_TIMESTAMPRECEDINGFAILASTFILTER */ // /* EPLACEFIRSTFOLLOWINGFROMFULLIMITIFORDERESTRICTOTHERSOVER */ // /* ETURNINGRIGHTROLLBACKROWSUNBOUNDEDUNIONUSINGVACUUMVIEWINDOWBY */ // /* INITIALLYPRIMARY */ var _zKWText = [666]uint8{ 0: uint8('R'), 1: uint8('E'), 2: uint8('I'), 3: uint8('N'), 4: uint8('D'), 5: uint8('E'), 6: uint8('X'), 7: uint8('E'), 8: uint8('D'), 9: uint8('E'), 10: uint8('S'), 11: uint8('C'), 12: uint8('A'), 13: uint8('P'), 14: uint8('E'), 15: uint8('A'), 16: uint8('C'), 17: uint8('H'), 18: uint8('E'), 19: uint8('C'), 20: uint8('K'), 21: uint8('E'), 22: uint8('Y'), 23: uint8('B'), 24: uint8('E'), 25: uint8('F'), 26: uint8('O'), 27: uint8('R'), 28: uint8('E'), 29: uint8('I'), 30: uint8('G'), 31: uint8('N'), 32: uint8('O'), 33: uint8('R'), 34: uint8('E'), 35: uint8('G'), 36: uint8('E'), 37: uint8('X'), 38: uint8('P'), 39: uint8('L'), 40: uint8('A'), 41: uint8('I'), 42: uint8('N'), 43: uint8('S'), 44: uint8('T'), 45: uint8('E'), 46: uint8('A'), 47: uint8('D'), 48: uint8('D'), 49: uint8('A'), 50: uint8('T'), 51: uint8('A'), 52: uint8('B'), 53: uint8('A'), 54: uint8('S'), 55: uint8('E'), 56: uint8('L'), 57: uint8('E'), 58: uint8('C'), 59: uint8('T'), 60: uint8('A'), 61: uint8('B'), 62: uint8('L'), 63: uint8('E'), 64: uint8('F'), 65: uint8('T'), 66: uint8('H'), 67: uint8('E'), 68: uint8('N'), 69: uint8('D'), 70: uint8('E'), 71: uint8('F'), 72: uint8('E'), 73: uint8('R'), 74: uint8('R'), 75: uint8('A'), 76: uint8('B'), 77: uint8('L'), 78: uint8('E'), 79: uint8('L'), 80: uint8('S'), 81: uint8('E'), 82: uint8('X'), 83: uint8('C'), 84: uint8('L'), 85: uint8('U'), 86: uint8('D'), 87: uint8('E'), 88: uint8('L'), 89: uint8('E'), 90: uint8('T'), 91: uint8('E'), 92: uint8('M'), 93: uint8('P'), 94: uint8('O'), 95: uint8('R'), 96: uint8('A'), 97: uint8('R'), 98: uint8('Y'), 99: uint8('I'), 100: uint8('S'), 101: uint8('N'), 102: uint8('U'), 103: uint8('L'), 104: uint8('L'), 105: uint8('S'), 106: uint8('A'), 107: uint8('V'), 108: uint8('E'), 109: uint8('P'), 110: uint8('O'), 111: uint8('I'), 112: uint8('N'), 113: uint8('T'), 114: uint8('E'), 115: uint8('R'), 116: uint8('S'), 117: uint8('E'), 118: uint8('C'), 119: uint8('T'), 120: uint8('I'), 121: uint8('E'), 122: uint8('S'), 123: uint8('N'), 124: uint8('O'), 125: uint8('T'), 126: uint8('N'), 127: uint8('U'), 128: uint8('L'), 129: uint8('L'), 130: uint8('I'), 131: uint8('K'), 132: uint8('E'), 133: uint8('X'), 134: uint8('C'), 135: uint8('E'), 136: uint8('P'), 137: uint8('T'), 138: uint8('R'), 139: uint8('A'), 140: uint8('N'), 141: uint8('S'), 142: uint8('A'), 143: uint8('C'), 144: uint8('T'), 145: uint8('I'), 146: uint8('O'), 147: uint8('N'), 148: uint8('A'), 149: uint8('T'), 150: uint8('U'), 151: uint8('R'), 152: uint8('A'), 153: uint8('L'), 154: uint8('T'), 155: uint8('E'), 156: uint8('R'), 157: uint8('A'), 158: uint8('I'), 159: uint8('S'), 160: uint8('E'), 161: uint8('X'), 162: uint8('C'), 163: uint8('L'), 164: uint8('U'), 165: uint8('S'), 166: uint8('I'), 167: uint8('V'), 168: uint8('E'), 169: uint8('X'), 170: uint8('I'), 171: uint8('S'), 172: uint8('T'), 173: uint8('S'), 174: uint8('C'), 175: uint8('O'), 176: uint8('N'), 177: uint8('S'), 178: uint8('T'), 179: uint8('R'), 180: uint8('A'), 181: uint8('I'), 182: uint8('N'), 183: uint8('T'), 184: uint8('O'), 185: uint8('F'), 186: uint8('F'), 187: uint8('S'), 188: uint8('E'), 189: uint8('T'), 190: uint8('R'), 191: uint8('I'), 192: uint8('G'), 193: uint8('G'), 194: uint8('E'), 195: uint8('R'), 196: uint8('A'), 197: uint8('N'), 198: uint8('G'), 199: uint8('E'), 200: uint8('N'), 201: uint8('E'), 202: uint8('R'), 203: uint8('A'), 204: uint8('T'), 205: uint8('E'), 206: uint8('D'), 207: uint8('E'), 208: uint8('T'), 209: uint8('A'), 210: uint8('C'), 211: uint8('H'), 212: uint8('A'), 213: uint8('V'), 214: uint8('I'), 215: uint8('N'), 216: uint8('G'), 217: uint8('L'), 218: uint8('O'), 219: uint8('B'), 220: uint8('E'), 221: uint8('G'), 222: uint8('I'), 223: uint8('N'), 224: uint8('N'), 225: uint8('E'), 226: uint8('R'), 227: uint8('E'), 228: uint8('F'), 229: uint8('E'), 230: uint8('R'), 231: uint8('E'), 232: uint8('N'), 233: uint8('C'), 234: uint8('E'), 235: uint8('S'), 236: uint8('U'), 237: uint8('N'), 238: uint8('I'), 239: uint8('Q'), 240: uint8('U'), 241: uint8('E'), 242: uint8('R'), 243: uint8('Y'), 244: uint8('W'), 245: uint8('I'), 246: uint8('T'), 247: uint8('H'), 248: uint8('O'), 249: uint8('U'), 250: uint8('T'), 251: uint8('E'), 252: uint8('R'), 253: uint8('E'), 254: uint8('L'), 255: uint8('E'), 256: uint8('A'), 257: uint8('S'), 258: uint8('E'), 259: uint8('A'), 260: uint8('T'), 261: uint8('T'), 262: uint8('A'), 263: uint8('C'), 264: uint8('H'), 265: uint8('B'), 266: uint8('E'), 267: uint8('T'), 268: uint8('W'), 269: uint8('E'), 270: uint8('E'), 271: uint8('N'), 272: uint8('O'), 273: uint8('T'), 274: uint8('H'), 275: uint8('I'), 276: uint8('N'), 277: uint8('G'), 278: uint8('R'), 279: uint8('O'), 280: uint8('U'), 281: uint8('P'), 282: uint8('S'), 283: uint8('C'), 284: uint8('A'), 285: uint8('S'), 286: uint8('C'), 287: uint8('A'), 288: uint8('D'), 289: uint8('E'), 290: uint8('F'), 291: uint8('A'), 292: uint8('U'), 293: uint8('L'), 294: uint8('T'), 295: uint8('C'), 296: uint8('A'), 297: uint8('S'), 298: uint8('E'), 299: uint8('C'), 300: uint8('O'), 301: uint8('L'), 302: uint8('L'), 303: uint8('A'), 304: uint8('T'), 305: uint8('E'), 306: uint8('C'), 307: uint8('R'), 308: uint8('E'), 309: uint8('A'), 310: uint8('T'), 311: uint8('E'), 312: uint8('C'), 313: uint8('U'), 314: uint8('R'), 315: uint8('R'), 316: uint8('E'), 317: uint8('N'), 318: uint8('T'), 319: uint8('_'), 320: uint8('D'), 321: uint8('A'), 322: uint8('T'), 323: uint8('E'), 324: uint8('I'), 325: uint8('M'), 326: uint8('M'), 327: uint8('E'), 328: uint8('D'), 329: uint8('I'), 330: uint8('A'), 331: uint8('T'), 332: uint8('E'), 333: uint8('J'), 334: uint8('O'), 335: uint8('I'), 336: uint8('N'), 337: uint8('S'), 338: uint8('E'), 339: uint8('R'), 340: uint8('T'), 341: uint8('M'), 342: uint8('A'), 343: uint8('T'), 344: uint8('C'), 345: uint8('H'), 346: uint8('P'), 347: uint8('L'), 348: uint8('A'), 349: uint8('N'), 350: uint8('A'), 351: uint8('L'), 352: uint8('Y'), 353: uint8('Z'), 354: uint8('E'), 355: uint8('P'), 356: uint8('R'), 357: uint8('A'), 358: uint8('G'), 359: uint8('M'), 360: uint8('A'), 361: uint8('T'), 362: uint8('E'), 363: uint8('R'), 364: uint8('I'), 365: uint8('A'), 366: uint8('L'), 367: uint8('I'), 368: uint8('Z'), 369: uint8('E'), 370: uint8('D'), 371: uint8('E'), 372: uint8('F'), 373: uint8('E'), 374: uint8('R'), 375: uint8('R'), 376: uint8('E'), 377: uint8('D'), 378: uint8('I'), 379: uint8('S'), 380: uint8('T'), 381: uint8('I'), 382: uint8('N'), 383: uint8('C'), 384: uint8('T'), 385: uint8('U'), 386: uint8('P'), 387: uint8('D'), 388: uint8('A'), 389: uint8('T'), 390: uint8('E'), 391: uint8('V'), 392: uint8('A'), 393: uint8('L'), 394: uint8('U'), 395: uint8('E'), 396: uint8('S'), 397: uint8('V'), 398: uint8('I'), 399: uint8('R'), 400: uint8('T'), 401: uint8('U'), 402: uint8('A'), 403: uint8('L'), 404: uint8('W'), 405: uint8('A'), 406: uint8('Y'), 407: uint8('S'), 408: uint8('W'), 409: uint8('H'), 410: uint8('E'), 411: uint8('N'), 412: uint8('W'), 413: uint8('H'), 414: uint8('E'), 415: uint8('R'), 416: uint8('E'), 417: uint8('C'), 418: uint8('U'), 419: uint8('R'), 420: uint8('S'), 421: uint8('I'), 422: uint8('V'), 423: uint8('E'), 424: uint8('A'), 425: uint8('B'), 426: uint8('O'), 427: uint8('R'), 428: uint8('T'), 429: uint8('A'), 430: uint8('F'), 431: uint8('T'), 432: uint8('E'), 433: uint8('R'), 434: uint8('E'), 435: uint8('N'), 436: uint8('A'), 437: uint8('M'), 438: uint8('E'), 439: uint8('A'), 440: uint8('N'), 441: uint8('D'), 442: uint8('R'), 443: uint8('O'), 444: uint8('P'), 445: uint8('A'), 446: uint8('R'), 447: uint8('T'), 448: uint8('I'), 449: uint8('T'), 450: uint8('I'), 451: uint8('O'), 452: uint8('N'), 453: uint8('A'), 454: uint8('U'), 455: uint8('T'), 456: uint8('O'), 457: uint8('I'), 458: uint8('N'), 459: uint8('C'), 460: uint8('R'), 461: uint8('E'), 462: uint8('M'), 463: uint8('E'), 464: uint8('N'), 465: uint8('T'), 466: uint8('C'), 467: uint8('A'), 468: uint8('S'), 469: uint8('T'), 470: uint8('C'), 471: uint8('O'), 472: uint8('L'), 473: uint8('U'), 474: uint8('M'), 475: uint8('N'), 476: uint8('C'), 477: uint8('O'), 478: uint8('M'), 479: uint8('M'), 480: uint8('I'), 481: uint8('T'), 482: uint8('C'), 483: uint8('O'), 484: uint8('N'), 485: uint8('F'), 486: uint8('L'), 487: uint8('I'), 488: uint8('C'), 489: uint8('T'), 490: uint8('C'), 491: uint8('R'), 492: uint8('O'), 493: uint8('S'), 494: uint8('S'), 495: uint8('C'), 496: uint8('U'), 497: uint8('R'), 498: uint8('R'), 499: uint8('E'), 500: uint8('N'), 501: uint8('T'), 502: uint8('_'), 503: uint8('T'), 504: uint8('I'), 505: uint8('M'), 506: uint8('E'), 507: uint8('S'), 508: uint8('T'), 509: uint8('A'), 510: uint8('M'), 511: uint8('P'), 512: uint8('R'), 513: uint8('E'), 514: uint8('C'), 515: uint8('E'), 516: uint8('D'), 517: uint8('I'), 518: uint8('N'), 519: uint8('G'), 520: uint8('F'), 521: uint8('A'), 522: uint8('I'), 523: uint8('L'), 524: uint8('A'), 525: uint8('S'), 526: uint8('T'), 527: uint8('F'), 528: uint8('I'), 529: uint8('L'), 530: uint8('T'), 531: uint8('E'), 532: uint8('R'), 533: uint8('E'), 534: uint8('P'), 535: uint8('L'), 536: uint8('A'), 537: uint8('C'), 538: uint8('E'), 539: uint8('F'), 540: uint8('I'), 541: uint8('R'), 542: uint8('S'), 543: uint8('T'), 544: uint8('F'), 545: uint8('O'), 546: uint8('L'), 547: uint8('L'), 548: uint8('O'), 549: uint8('W'), 550: uint8('I'), 551: uint8('N'), 552: uint8('G'), 553: uint8('F'), 554: uint8('R'), 555: uint8('O'), 556: uint8('M'), 557: uint8('F'), 558: uint8('U'), 559: uint8('L'), 560: uint8('L'), 561: uint8('I'), 562: uint8('M'), 563: uint8('I'), 564: uint8('T'), 565: uint8('I'), 566: uint8('F'), 567: uint8('O'), 568: uint8('R'), 569: uint8('D'), 570: uint8('E'), 571: uint8('R'), 572: uint8('E'), 573: uint8('S'), 574: uint8('T'), 575: uint8('R'), 576: uint8('I'), 577: uint8('C'), 578: uint8('T'), 579: uint8('O'), 580: uint8('T'), 581: uint8('H'), 582: uint8('E'), 583: uint8('R'), 584: uint8('S'), 585: uint8('O'), 586: uint8('V'), 587: uint8('E'), 588: uint8('R'), 589: uint8('E'), 590: uint8('T'), 591: uint8('U'), 592: uint8('R'), 593: uint8('N'), 594: uint8('I'), 595: uint8('N'), 596: uint8('G'), 597: uint8('R'), 598: uint8('I'), 599: uint8('G'), 600: uint8('H'), 601: uint8('T'), 602: uint8('R'), 603: uint8('O'), 604: uint8('L'), 605: uint8('L'), 606: uint8('B'), 607: uint8('A'), 608: uint8('C'), 609: uint8('K'), 610: uint8('R'), 611: uint8('O'), 612: uint8('W'), 613: uint8('S'), 614: uint8('U'), 615: uint8('N'), 616: uint8('B'), 617: uint8('O'), 618: uint8('U'), 619: uint8('N'), 620: uint8('D'), 621: uint8('E'), 622: uint8('D'), 623: uint8('U'), 624: uint8('N'), 625: uint8('I'), 626: uint8('O'), 627: uint8('N'), 628: uint8('U'), 629: uint8('S'), 630: uint8('I'), 631: uint8('N'), 632: uint8('G'), 633: uint8('V'), 634: uint8('A'), 635: uint8('C'), 636: uint8('U'), 637: uint8('U'), 638: uint8('M'), 639: uint8('V'), 640: uint8('I'), 641: uint8('E'), 642: uint8('W'), 643: uint8('I'), 644: uint8('N'), 645: uint8('D'), 646: uint8('O'), 647: uint8('W'), 648: uint8('B'), 649: uint8('Y'), 650: uint8('I'), 651: uint8('N'), 652: uint8('I'), 653: uint8('T'), 654: uint8('I'), 655: uint8('A'), 656: uint8('L'), 657: uint8('L'), 658: uint8('Y'), 659: uint8('P'), 660: uint8('R'), 661: uint8('I'), 662: uint8('M'), 663: uint8('A'), 664: uint8('R'), 665: uint8('Y'), } // C documentation // // /* aKWHash[i] is the hash value for the i-th keyword */ var _aKWHash = [127]uint8{ 0: uint8(84), 1: uint8(92), 2: uint8(134), 3: uint8(82), 4: uint8(105), 5: uint8(29), 8: uint8(94), 10: uint8(85), 11: uint8(72), 13: uint8(53), 14: uint8(35), 15: uint8(86), 16: uint8(15), 18: uint8(42), 19: uint8(97), 20: uint8(54), 21: uint8(89), 22: uint8(135), 23: uint8(19), 26: uint8(140), 28: uint8(40), 29: uint8(129), 31: uint8(22), 32: uint8(107), 34: uint8(9), 37: uint8(123), 38: uint8(80), 40: uint8(78), 41: uint8(6), 43: uint8(65), 44: uint8(103), 45: uint8(147), 47: uint8(136), 48: uint8(115), 51: uint8(48), 53: uint8(90), 54: uint8(24), 56: uint8(17), 58: uint8(27), 59: uint8(70), 60: uint8(23), 61: uint8(26), 62: uint8(5), 63: uint8(60), 64: uint8(142), 65: uint8(110), 66: uint8(122), 68: uint8(73), 69: uint8(91), 70: uint8(71), 71: uint8(145), 72: uint8(61), 73: uint8(120), 74: uint8(74), 76: uint8(49), 78: uint8(11), 79: uint8(41), 81: uint8(113), 85: uint8(109), 86: uint8(10), 87: uint8(111), 88: uint8(116), 89: uint8(125), 90: uint8(14), 91: uint8(50), 92: uint8(124), 94: uint8(100), 96: uint8(18), 97: uint8(121), 98: uint8(144), 99: uint8(56), 100: uint8(130), 101: uint8(139), 102: uint8(88), 103: uint8(83), 104: uint8(37), 105: uint8(30), 106: uint8(126), 109: uint8(108), 110: uint8(51), 111: uint8(131), 112: uint8(128), 114: uint8(34), 117: uint8(132), 119: uint8(98), 120: uint8(38), 121: uint8(39), 123: uint8(20), 124: uint8(45), 125: uint8(117), 126: uint8(93), } // C documentation // // /* aKWNext[] forms the hash collision chain. If aKWHash[i]==0 // ** then the i-th keyword has no more hash collisions. Otherwise, // ** the next keyword with the same hash is aKWHash[i]-1. */ var _aKWNext = [148]uint8{ 5: uint8(4), 7: uint8(43), 10: uint8(106), 11: uint8(114), 15: uint8(2), 18: uint8(143), 22: uint8(13), 27: uint8(141), 30: uint8(119), 31: uint8(52), 34: uint8(137), 35: uint8(12), 38: uint8(62), 40: uint8(138), 42: uint8(133), 45: uint8(36), 48: uint8(28), 49: uint8(77), 54: uint8(59), 56: uint8(47), 67: uint8(69), 73: uint8(146), 74: uint8(3), 76: uint8(58), 78: uint8(1), 79: uint8(75), 83: uint8(31), 89: uint8(127), 91: uint8(104), 93: uint8(64), 94: uint8(66), 95: uint8(63), 101: uint8(46), 103: uint8(16), 104: uint8(8), 115: uint8(81), 116: uint8(101), 118: uint8(112), 119: uint8(21), 120: uint8(7), 121: uint8(67), 123: uint8(79), 124: uint8(96), 125: uint8(118), 128: uint8(68), 131: uint8(99), 132: uint8(44), 134: uint8(55), 136: uint8(76), 138: uint8(95), 139: uint8(32), 140: uint8(33), 141: uint8(57), 142: uint8(25), 144: uint8(102), 147: uint8(87), } // C documentation // // /* aKWLen[i] is the length (in bytes) of the i-th keyword */ var _aKWLen = [148]uint8{ 1: uint8(7), 2: uint8(7), 3: uint8(5), 4: uint8(4), 5: uint8(6), 6: uint8(4), 7: uint8(5), 8: uint8(3), 9: uint8(6), 10: uint8(7), 11: uint8(3), 12: uint8(6), 13: uint8(6), 14: uint8(7), 15: uint8(7), 16: uint8(3), 17: uint8(8), 18: uint8(2), 19: uint8(6), 20: uint8(5), 21: uint8(4), 22: uint8(4), 23: uint8(3), 24: uint8(10), 25: uint8(4), 26: uint8(7), 27: uint8(6), 28: uint8(9), 29: uint8(4), 30: uint8(2), 31: uint8(6), 32: uint8(5), 33: uint8(9), 34: uint8(9), 35: uint8(4), 36: uint8(7), 37: uint8(3), 38: uint8(2), 39: uint8(4), 40: uint8(4), 41: uint8(6), 42: uint8(11), 43: uint8(6), 44: uint8(2), 45: uint8(7), 46: uint8(5), 47: uint8(5), 48: uint8(9), 49: uint8(6), 50: uint8(10), 51: uint8(4), 52: uint8(6), 53: uint8(2), 54: uint8(3), 55: uint8(7), 56: uint8(5), 57: uint8(9), 58: uint8(6), 59: uint8(6), 60: uint8(4), 61: uint8(5), 62: uint8(5), 63: uint8(10), 64: uint8(6), 65: uint8(5), 66: uint8(7), 67: uint8(4), 68: uint8(5), 69: uint8(7), 70: uint8(6), 71: uint8(7), 72: uint8(7), 73: uint8(6), 74: uint8(5), 75: uint8(7), 76: uint8(3), 77: uint8(7), 78: uint8(4), 79: uint8(7), 80: uint8(6), 81: uint8(12), 82: uint8(9), 83: uint8(4), 84: uint8(6), 85: uint8(5), 86: uint8(4), 87: uint8(7), 88: uint8(6), 89: uint8(12), 90: uint8(8), 91: uint8(8), 92: uint8(2), 93: uint8(6), 94: uint8(6), 95: uint8(7), 96: uint8(6), 97: uint8(4), 98: uint8(5), 99: uint8(9), 100: uint8(5), 101: uint8(5), 102: uint8(6), 103: uint8(3), 104: uint8(4), 105: uint8(9), 106: uint8(13), 107: uint8(2), 108: uint8(2), 109: uint8(4), 110: uint8(6), 111: uint8(6), 112: uint8(8), 113: uint8(5), 114: uint8(17), 115: uint8(12), 116: uint8(7), 117: uint8(9), 118: uint8(4), 119: uint8(4), 120: uint8(6), 121: uint8(7), 122: uint8(5), 123: uint8(9), 124: uint8(4), 125: uint8(4), 126: uint8(5), 127: uint8(2), 128: uint8(5), 129: uint8(8), 130: uint8(6), 131: uint8(4), 132: uint8(9), 133: uint8(5), 134: uint8(8), 135: uint8(4), 136: uint8(3), 137: uint8(9), 138: uint8(5), 139: uint8(5), 140: uint8(6), 141: uint8(4), 142: uint8(6), 143: uint8(2), 144: uint8(2), 145: uint8(9), 146: uint8(3), 147: uint8(7), } // C documentation // // /* aKWOffset[i] is the index into zKWText[] of the start of // ** the text for the i-th keyword. */ var _aKWOffset = [148]uint16{ 2: uint16(2), 3: uint16(2), 4: uint16(8), 5: uint16(9), 6: uint16(14), 7: uint16(16), 8: uint16(20), 9: uint16(23), 10: uint16(25), 11: uint16(25), 12: uint16(29), 13: uint16(33), 14: uint16(36), 15: uint16(41), 16: uint16(46), 17: uint16(48), 18: uint16(53), 19: uint16(54), 20: uint16(59), 21: uint16(62), 22: uint16(65), 23: uint16(67), 24: uint16(69), 25: uint16(78), 26: uint16(81), 27: uint16(86), 28: uint16(90), 29: uint16(90), 30: uint16(94), 31: uint16(99), 32: uint16(101), 33: uint16(105), 34: uint16(111), 35: uint16(119), 36: uint16(123), 37: uint16(123), 38: uint16(123), 39: uint16(126), 40: uint16(129), 41: uint16(132), 42: uint16(137), 43: uint16(142), 44: uint16(146), 45: uint16(147), 46: uint16(152), 47: uint16(156), 48: uint16(160), 49: uint16(168), 50: uint16(174), 51: uint16(181), 52: uint16(184), 53: uint16(184), 54: uint16(187), 55: uint16(189), 56: uint16(195), 57: uint16(198), 58: uint16(206), 59: uint16(211), 60: uint16(216), 61: uint16(219), 62: uint16(222), 63: uint16(226), 64: uint16(236), 65: uint16(239), 66: uint16(244), 67: uint16(244), 68: uint16(248), 69: uint16(252), 70: uint16(259), 71: uint16(265), 72: uint16(271), 73: uint16(277), 74: uint16(277), 75: uint16(283), 76: uint16(284), 77: uint16(288), 78: uint16(295), 79: uint16(299), 80: uint16(306), 81: uint16(312), 82: uint16(324), 83: uint16(333), 84: uint16(335), 85: uint16(341), 86: uint16(346), 87: uint16(348), 88: uint16(355), 89: uint16(359), 90: uint16(370), 91: uint16(377), 92: uint16(378), 93: uint16(385), 94: uint16(391), 95: uint16(397), 96: uint16(402), 97: uint16(408), 98: uint16(412), 99: uint16(415), 100: uint16(424), 101: uint16(429), 102: uint16(433), 103: uint16(439), 104: uint16(441), 105: uint16(444), 106: uint16(453), 107: uint16(455), 108: uint16(457), 109: uint16(466), 110: uint16(470), 111: uint16(476), 112: uint16(482), 113: uint16(490), 114: uint16(495), 115: uint16(495), 116: uint16(495), 117: uint16(511), 118: uint16(520), 119: uint16(523), 120: uint16(527), 121: uint16(532), 122: uint16(539), 123: uint16(544), 124: uint16(553), 125: uint16(557), 126: uint16(560), 127: uint16(565), 128: uint16(567), 129: uint16(571), 130: uint16(579), 131: uint16(585), 132: uint16(588), 133: uint16(597), 134: uint16(602), 135: uint16(610), 136: uint16(610), 137: uint16(614), 138: uint16(623), 139: uint16(628), 140: uint16(633), 141: uint16(639), 142: uint16(642), 143: uint16(645), 144: uint16(648), 145: uint16(650), 146: uint16(655), 147: uint16(659), } // C documentation // // /* aKWCode[i] is the parser symbol code for the i-th keyword */ var _aKWCode = [148]uint8{ 1: uint8(TK_REINDEX), 2: uint8(TK_INDEXED), 3: uint8(TK_INDEX), 4: uint8(TK_DESC), 5: uint8(TK_ESCAPE), 6: uint8(TK_EACH), 7: uint8(TK_CHECK), 8: uint8(TK_KEY), 9: uint8(TK_BEFORE), 10: uint8(TK_FOREIGN), 11: uint8(TK_FOR), 12: uint8(TK_IGNORE), 13: uint8(TK_LIKE_KW), 14: uint8(TK_EXPLAIN), 15: uint8(TK_INSTEAD), 16: uint8(TK_ADD), 17: uint8(TK_DATABASE), 18: uint8(TK_AS), 19: uint8(TK_SELECT), 20: uint8(TK_TABLE), 21: uint8(TK_JOIN_KW), 22: uint8(TK_THEN), 23: uint8(TK_END), 24: uint8(TK_DEFERRABLE), 25: uint8(TK_ELSE), 26: uint8(TK_EXCLUDE), 27: uint8(TK_DELETE), 28: uint8(TK_TEMP), 29: uint8(TK_TEMP), 30: uint8(TK_OR), 31: uint8(TK_ISNULL), 32: uint8(TK_NULLS), 33: uint8(TK_SAVEPOINT), 34: uint8(TK_INTERSECT), 35: uint8(TK_TIES), 36: uint8(TK_NOTNULL), 37: uint8(TK_NOT), 38: uint8(TK_NO), 39: uint8(TK_NULL), 40: uint8(TK_LIKE_KW), 41: uint8(TK_EXCEPT), 42: uint8(TK_TRANSACTION), 43: uint8(TK_ACTION), 44: uint8(TK_ON), 45: uint8(TK_JOIN_KW), 46: uint8(TK_ALTER), 47: uint8(TK_RAISE), 48: uint8(TK_EXCLUSIVE), 49: uint8(TK_EXISTS), 50: uint8(TK_CONSTRAINT), 51: uint8(TK_INTO), 52: uint8(TK_OFFSET), 53: uint8(TK_OF), 54: uint8(TK_SET), 55: uint8(TK_TRIGGER), 56: uint8(TK_RANGE), 57: uint8(TK_GENERATED), 58: uint8(TK_DETACH), 59: uint8(TK_HAVING), 60: uint8(TK_LIKE_KW), 61: uint8(TK_BEGIN), 62: uint8(TK_JOIN_KW), 63: uint8(TK_REFERENCES), 64: uint8(TK_UNIQUE), 65: uint8(TK_QUERY), 66: uint8(TK_WITHOUT), 67: uint8(TK_WITH), 68: uint8(TK_JOIN_KW), 69: uint8(TK_RELEASE), 70: uint8(TK_ATTACH), 71: uint8(TK_BETWEEN), 72: uint8(TK_NOTHING), 73: uint8(TK_GROUPS), 74: uint8(TK_GROUP), 75: uint8(TK_CASCADE), 76: uint8(TK_ASC), 77: uint8(TK_DEFAULT), 78: uint8(TK_CASE), 79: uint8(TK_COLLATE), 80: uint8(TK_CREATE), 81: uint8(TK_CTIME_KW), 82: uint8(TK_IMMEDIATE), 83: uint8(TK_JOIN), 84: uint8(TK_INSERT), 85: uint8(TK_MATCH), 86: uint8(TK_PLAN), 87: uint8(TK_ANALYZE), 88: uint8(TK_PRAGMA), 89: uint8(TK_MATERIALIZED), 90: uint8(TK_DEFERRED), 91: uint8(TK_DISTINCT), 92: uint8(TK_IS), 93: uint8(TK_UPDATE), 94: uint8(TK_VALUES), 95: uint8(TK_VIRTUAL), 96: uint8(TK_ALWAYS), 97: uint8(TK_WHEN), 98: uint8(TK_WHERE), 99: uint8(TK_RECURSIVE), 100: uint8(TK_ABORT), 101: uint8(TK_AFTER), 102: uint8(TK_RENAME), 103: uint8(TK_AND), 104: uint8(TK_DROP), 105: uint8(TK_PARTITION), 106: uint8(TK_AUTOINCR), 107: uint8(TK_TO), 108: uint8(TK_IN), 109: uint8(TK_CAST), 110: uint8(TK_COLUMNKW), 111: uint8(TK_COMMIT), 112: uint8(TK_CONFLICT), 113: uint8(TK_JOIN_KW), 114: uint8(TK_CTIME_KW), 115: uint8(TK_CTIME_KW), 116: uint8(TK_CURRENT), 117: uint8(TK_PRECEDING), 118: uint8(TK_FAIL), 119: uint8(TK_LAST), 120: uint8(TK_FILTER), 121: uint8(TK_REPLACE), 122: uint8(TK_FIRST), 123: uint8(TK_FOLLOWING), 124: uint8(TK_FROM), 125: uint8(TK_JOIN_KW), 126: uint8(TK_LIMIT), 127: uint8(TK_IF), 128: uint8(TK_ORDER), 129: uint8(TK_RESTRICT), 130: uint8(TK_OTHERS), 131: uint8(TK_OVER), 132: uint8(TK_RETURNING), 133: uint8(TK_JOIN_KW), 134: uint8(TK_ROLLBACK), 135: uint8(TK_ROWS), 136: uint8(TK_ROW), 137: uint8(TK_UNBOUNDED), 138: uint8(TK_UNION), 139: uint8(TK_USING), 140: uint8(TK_VACUUM), 141: uint8(TK_VIEW), 142: uint8(TK_WINDOW), 143: uint8(TK_DO), 144: uint8(TK_BY), 145: uint8(TK_INITIALLY), 146: uint8(TK_ALL), 147: uint8(TK_PRIMARY), } // C documentation // // /* Hash table decoded: // ** 0: INSERT // ** 1: IS // ** 2: ROLLBACK TRIGGER // ** 3: IMMEDIATE // ** 4: PARTITION // ** 5: TEMP // ** 6: // ** 7: // ** 8: VALUES WITHOUT // ** 9: // ** 10: MATCH // ** 11: NOTHING // ** 12: // ** 13: OF // ** 14: TIES IGNORE // ** 15: PLAN // ** 16: INSTEAD INDEXED // ** 17: // ** 18: TRANSACTION RIGHT // ** 19: WHEN // ** 20: SET HAVING // ** 21: MATERIALIZED IF // ** 22: ROWS // ** 23: SELECT // ** 24: // ** 25: // ** 26: VACUUM SAVEPOINT // ** 27: // ** 28: LIKE UNION VIRTUAL REFERENCES // ** 29: RESTRICT // ** 30: // ** 31: THEN REGEXP // ** 32: TO // ** 33: // ** 34: BEFORE // ** 35: // ** 36: // ** 37: FOLLOWING COLLATE CASCADE // ** 38: CREATE // ** 39: // ** 40: CASE REINDEX // ** 41: EACH // ** 42: // ** 43: QUERY // ** 44: AND ADD // ** 45: PRIMARY ANALYZE // ** 46: // ** 47: ROW ASC DETACH // ** 48: CURRENT_TIME CURRENT_DATE // ** 49: // ** 50: // ** 51: EXCLUSIVE TEMPORARY // ** 52: // ** 53: DEFERRED // ** 54: DEFERRABLE // ** 55: // ** 56: DATABASE // ** 57: // ** 58: DELETE VIEW GENERATED // ** 59: ATTACH // ** 60: END // ** 61: EXCLUDE // ** 62: ESCAPE DESC // ** 63: GLOB // ** 64: WINDOW ELSE // ** 65: COLUMN // ** 66: FIRST // ** 67: // ** 68: GROUPS ALL // ** 69: DISTINCT DROP KEY // ** 70: BETWEEN // ** 71: INITIALLY // ** 72: BEGIN // ** 73: FILTER CHECK ACTION // ** 74: GROUP INDEX // ** 75: // ** 76: EXISTS DEFAULT // ** 77: // ** 78: FOR CURRENT_TIMESTAMP // ** 79: EXCEPT // ** 80: // ** 81: CROSS // ** 82: // ** 83: // ** 84: // ** 85: CAST // ** 86: FOREIGN AUTOINCREMENT // ** 87: COMMIT // ** 88: CURRENT AFTER ALTER // ** 89: FULL FAIL CONFLICT // ** 90: EXPLAIN // ** 91: CONSTRAINT // ** 92: FROM ALWAYS // ** 93: // ** 94: ABORT // ** 95: // ** 96: AS DO // ** 97: REPLACE WITH RELEASE // ** 98: BY RENAME // ** 99: RANGE RAISE // ** 100: OTHERS // ** 101: USING NULLS // ** 102: PRAGMA // ** 103: JOIN ISNULL OFFSET // ** 104: NOT // ** 105: OR LAST LEFT // ** 106: LIMIT // ** 107: // ** 108: // ** 109: IN // ** 110: INTO // ** 111: OVER RECURSIVE // ** 112: ORDER OUTER // ** 113: // ** 114: INTERSECT UNBOUNDED // ** 115: // ** 116: // ** 117: RETURNING ON // ** 118: // ** 119: WHERE // ** 120: NO INNER // ** 121: NULL // ** 122: // ** 123: TABLE // ** 124: NATURAL NOTNULL // ** 125: PRECEDING // ** 126: UPDATE UNIQUE // */ // /* Check to see if z[0..n-1] is a keyword. If it is, write the // ** parser symbol code for that keyword into *pType. Always // ** return the integer n (the length of the token). */ func _keywordCode(tls *libc.TLS, z uintptr, n int32, pType uintptr) (r int32) { var i, j int32 var zKW uintptr _, _, _ = i, j, zKW _ = libc.Int32FromInt32(0) i = (int32(_sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(z))])*int32(4) ^ int32(_sqlite3UpperToLower[*(*uint8)(unsafe.Pointer(z + uintptr(n-int32(1))))])*int32(3) ^ n*int32(1)) % int32(127) i = int32(_aKWHash[i]) for { if !(i > 0) { break } if int32(_aKWLen[i]) != n { goto _1 } zKW = uintptr(unsafe.Pointer(&_zKWText)) + uintptr(_aKWOffset[i]) if int32(*(*uint8)(unsafe.Pointer(z))) & ^libc.Int32FromInt32(0x20) != int32(*(*uint8)(unsafe.Pointer(zKW))) { goto _1 } if int32(*(*uint8)(unsafe.Pointer(z + 1))) & ^libc.Int32FromInt32(0x20) != int32(*(*uint8)(unsafe.Pointer(zKW + 1))) { goto _1 } j = int32(2) for j < n && int32(*(*uint8)(unsafe.Pointer(z + uintptr(j)))) & ^libc.Int32FromInt32(0x20) == int32(*(*uint8)(unsafe.Pointer(zKW + uintptr(j)))) { j++ } if j < n { goto _1 } /* REINDEX */ /* INDEXED */ /* INDEX */ /* DESC */ /* ESCAPE */ /* EACH */ /* CHECK */ /* KEY */ /* BEFORE */ /* FOREIGN */ /* FOR */ /* IGNORE */ /* REGEXP */ /* EXPLAIN */ /* INSTEAD */ /* ADD */ /* DATABASE */ /* AS */ /* SELECT */ /* TABLE */ /* LEFT */ /* THEN */ /* END */ /* DEFERRABLE */ /* ELSE */ /* EXCLUDE */ /* DELETE */ /* TEMPORARY */ /* TEMP */ /* OR */ /* ISNULL */ /* NULLS */ /* SAVEPOINT */ /* INTERSECT */ /* TIES */ /* NOTNULL */ /* NOT */ /* NO */ /* NULL */ /* LIKE */ /* EXCEPT */ /* TRANSACTION */ /* ACTION */ /* ON */ /* NATURAL */ /* ALTER */ /* RAISE */ /* EXCLUSIVE */ /* EXISTS */ /* CONSTRAINT */ /* INTO */ /* OFFSET */ /* OF */ /* SET */ /* TRIGGER */ /* RANGE */ /* GENERATED */ /* DETACH */ /* HAVING */ /* GLOB */ /* BEGIN */ /* INNER */ /* REFERENCES */ /* UNIQUE */ /* QUERY */ /* WITHOUT */ /* WITH */ /* OUTER */ /* RELEASE */ /* ATTACH */ /* BETWEEN */ /* NOTHING */ /* GROUPS */ /* GROUP */ /* CASCADE */ /* ASC */ /* DEFAULT */ /* CASE */ /* COLLATE */ /* CREATE */ /* CURRENT_DATE */ /* IMMEDIATE */ /* JOIN */ /* INSERT */ /* MATCH */ /* PLAN */ /* ANALYZE */ /* PRAGMA */ /* MATERIALIZED */ /* DEFERRED */ /* DISTINCT */ /* IS */ /* UPDATE */ /* VALUES */ /* VIRTUAL */ /* ALWAYS */ /* WHEN */ /* WHERE */ /* RECURSIVE */ /* ABORT */ /* AFTER */ /* RENAME */ /* AND */ /* DROP */ /* PARTITION */ /* AUTOINCREMENT */ /* TO */ /* IN */ /* CAST */ /* COLUMN */ /* COMMIT */ /* CONFLICT */ /* CROSS */ /* CURRENT_TIMESTAMP */ /* CURRENT_TIME */ /* CURRENT */ /* PRECEDING */ /* FAIL */ /* LAST */ /* FILTER */ /* REPLACE */ /* FIRST */ /* FOLLOWING */ /* FROM */ /* FULL */ /* LIMIT */ /* IF */ /* ORDER */ /* RESTRICT */ /* OTHERS */ /* OVER */ /* RETURNING */ /* RIGHT */ /* ROLLBACK */ /* ROWS */ /* ROW */ /* UNBOUNDED */ /* UNION */ /* USING */ /* VACUUM */ /* VIEW */ /* WINDOW */ /* DO */ /* BY */ /* INITIALLY */ /* ALL */ /* PRIMARY */ *(*int32)(unsafe.Pointer(pType)) = int32(_aKWCode[i]) break goto _1 _1: ; i = int32(_aKWNext[i]) } return n } func _sqlite3KeywordCode(tls *libc.TLS, z uintptr, n int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* id at bp+0 */ int32 *(*int32)(unsafe.Pointer(bp)) = int32(TK_ID) if n >= int32(2) { _keywordCode(tls, z, n, bp) } return *(*int32)(unsafe.Pointer(bp)) } func Xsqlite3_keyword_name(tls *libc.TLS, i int32, pzName uintptr, pnName uintptr) (r int32) { if i < 0 || i >= int32(SQLITE_N_KEYWORD) { return int32(SQLITE_ERROR) } i++ *(*uintptr)(unsafe.Pointer(pzName)) = uintptr(unsafe.Pointer(&_zKWText)) + uintptr(_aKWOffset[i]) *(*int32)(unsafe.Pointer(pnName)) = int32(_aKWLen[i]) return SQLITE_OK } func Xsqlite3_keyword_count(tls *libc.TLS) (r int32) { return int32(SQLITE_N_KEYWORD) } func Xsqlite3_keyword_check(tls *libc.TLS, zName uintptr, nName int32) (r int32) { return libc.BoolInt32(int32(TK_ID) != _sqlite3KeywordCode(tls, zName, nName)) } /************** End of keywordhash.h *****************************************/ /************** Continuing where we left off in tokenize.c *******************/ /* ** If X is a character that can be used in an identifier then ** IdChar(X) will be true. Otherwise it is false. ** ** For ASCII, any character with the high-order bit set is ** allowed in an identifier. For 7-bit characters, ** sqlite3IsIdChar[X] must be 1. ** ** For EBCDIC, the rules are more complex but have the same ** end result. ** ** Ticket #1066. the SQL standard does not allow '$' in the ** middle of identifiers. But many SQL implementations do. ** SQLite will allow '$' in identifiers for compatibility. ** But the feature is undocumented. */ // C documentation // // /* Make the IdChar function accessible from ctime.c and alter.c */ func _sqlite3IsIdChar(tls *libc.TLS, c Tu8) (r int32) { return libc.BoolInt32(int32(_sqlite3CtypeMap[c])&int32(0x46) != 0) } // C documentation // // /* // ** Return the id of the next token in string (*pz). Before returning, set // ** (*pz) to point to the byte following the parsed token. // */ func _getToken(tls *libc.TLS, pz uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var z uintptr var _ /* t at bp+0 */ int32 _ = z z = *(*uintptr)(unsafe.Pointer(pz)) /* Token type to return */ for cond := true; cond; cond = *(*int32)(unsafe.Pointer(bp)) == int32(TK_SPACE) { z += uintptr(_sqlite3GetToken(tls, z, bp)) } if *(*int32)(unsafe.Pointer(bp)) == int32(TK_ID) || *(*int32)(unsafe.Pointer(bp)) == int32(TK_STRING) || *(*int32)(unsafe.Pointer(bp)) == int32(TK_JOIN_KW) || *(*int32)(unsafe.Pointer(bp)) == int32(TK_WINDOW) || *(*int32)(unsafe.Pointer(bp)) == int32(TK_OVER) || _sqlite3ParserFallback(tls, *(*int32)(unsafe.Pointer(bp))) == int32(TK_ID) { *(*int32)(unsafe.Pointer(bp)) = int32(TK_ID) } *(*uintptr)(unsafe.Pointer(pz)) = z return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** The following three functions are called immediately after the tokenizer // ** reads the keywords WINDOW, OVER and FILTER, respectively, to determine // ** whether the token should be treated as a keyword or an SQL identifier. // ** This cannot be handled by the usual lemon %fallback method, due to // ** the ambiguity in some constructions. e.g. // ** // ** SELECT sum(x) OVER ... // ** // ** In the above, "OVER" might be a keyword, or it might be an alias for the // ** sum(x) expression. If a "%fallback ID OVER" directive were added to // ** grammar, then SQLite would always treat "OVER" as an alias, making it // ** impossible to call a window-function without a FILTER clause. // ** // ** WINDOW is treated as a keyword if: // ** // ** * the following token is an identifier, or a keyword that can fallback // ** to being an identifier, and // ** * the token after than one is TK_AS. // ** // ** OVER is a keyword if: // ** // ** * the previous token was TK_RP, and // ** * the next token is either TK_LP or an identifier. // ** // ** FILTER is a keyword if: // ** // ** * the previous token was TK_RP, and // ** * the next token is TK_LP. // */ func _analyzeWindowKeyword(tls *libc.TLS, _z uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) *(*uintptr)(unsafe.Pointer(bp)) = _z var t int32 _ = t t = _getToken(tls, bp) if t != int32(TK_ID) { return int32(TK_ID) } t = _getToken(tls, bp) if t != int32(TK_AS) { return int32(TK_ID) } return int32(TK_WINDOW) } func _analyzeOverKeyword(tls *libc.TLS, _z uintptr, lastToken int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) *(*uintptr)(unsafe.Pointer(bp)) = _z var t int32 _ = t if lastToken == int32(TK_RP) { t = _getToken(tls, bp) if t == int32(TK_LP) || t == int32(TK_ID) { return int32(TK_OVER) } } return int32(TK_ID) } func _analyzeFilterKeyword(tls *libc.TLS, _z uintptr, lastToken int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) *(*uintptr)(unsafe.Pointer(bp)) = _z if lastToken == int32(TK_RP) && _getToken(tls, bp) == int32(TK_LP) { return int32(TK_FILTER) } return int32(TK_ID) } // C documentation // // /* // ** Return the length (in bytes) of the token that begins at z[0]. // ** Store the token type in *tokenType before returning. // */ func _sqlite3GetToken(tls *libc.TLS, z uintptr, tokenType uintptr) (r int32) { var c, delim, i, n, v10, v14, v16, v19, v20, v3, v5, v7, v8 int32 var v15, v6 bool _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, delim, i, n, v10, v14, v15, v16, v19, v20, v3, v5, v6, v7, v8 switch int32(_aiClass[*(*uint8)(unsafe.Pointer(z))]) { /* Switch on the character-class of the first byte ** of the token. See the comment on the CC_ defines ** above. */ case int32(CC_SPACE): i = int32(1) for { if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x01) != 0) { break } goto _1 _1: ; i++ } *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_SPACE) return i case int32(CC_MINUS): if int32(*(*uint8)(unsafe.Pointer(z + 1))) == int32('-') { i = int32(2) for { v3 = int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) c = v3 if !(v3 != 0 && c != int32('\n')) { break } goto _2 _2: ; i++ } *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_SPACE) /* IMP: R-22934-25134 */ return i } else { if int32(*(*uint8)(unsafe.Pointer(z + 1))) == int32('>') { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_PTR) return int32(2) + libc.BoolInt32(int32(*(*uint8)(unsafe.Pointer(z + 2))) == int32('>')) } } *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_MINUS) return int32(1) case int32(CC_LP): *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_LP) return int32(1) case int32(CC_RP): *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_RP) return int32(1) case int32(CC_SEMI): *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_SEMI) return int32(1) case int32(CC_PLUS): *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_PLUS) return int32(1) case int32(CC_STAR): *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_STAR) return int32(1) case int32(CC_SLASH): if int32(*(*uint8)(unsafe.Pointer(z + 1))) != int32('*') || int32(*(*uint8)(unsafe.Pointer(z + 2))) == 0 { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_SLASH) return int32(1) } i = int32(3) c = int32(*(*uint8)(unsafe.Pointer(z + 2))) for { if v6 = c != int32('*') || int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) != int32('/'); v6 { v5 = int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) c = v5 } if !(v6 && v5 != 0) { break } goto _4 _4: ; i++ } if c != 0 { i++ } *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_SPACE) /* IMP: R-22934-25134 */ return i case int32(CC_PERCENT): *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_REM) return int32(1) case int32(CC_EQ): *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_EQ) return int32(1) + libc.BoolInt32(int32(*(*uint8)(unsafe.Pointer(z + 1))) == int32('=')) case int32(CC_LT): v7 = int32(*(*uint8)(unsafe.Pointer(z + 1))) c = v7 if v7 == int32('=') { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_LE) return int32(2) } else { if c == int32('>') { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_NE) return int32(2) } else { if c == int32('<') { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_LSHIFT) return int32(2) } else { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_LT) return int32(1) } } } fallthrough case int32(CC_GT): v8 = int32(*(*uint8)(unsafe.Pointer(z + 1))) c = v8 if v8 == int32('=') { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_GE) return int32(2) } else { if c == int32('>') { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_RSHIFT) return int32(2) } else { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_GT) return int32(1) } } fallthrough case int32(CC_BANG): if int32(*(*uint8)(unsafe.Pointer(z + 1))) != int32('=') { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_ILLEGAL) return int32(1) } else { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_NE) return int32(2) } fallthrough case int32(CC_PIPE): if int32(*(*uint8)(unsafe.Pointer(z + 1))) != int32('|') { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_BITOR) return int32(1) } else { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_CONCAT) return int32(2) } fallthrough case int32(CC_COMMA): *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_COMMA) return int32(1) case int32(CC_AND): *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_BITAND) return int32(1) case int32(CC_TILDA): *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_BITNOT) return int32(1) case int32(CC_QUOTE): delim = int32(*(*uint8)(unsafe.Pointer(z))) i = int32(1) for { v10 = int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) c = v10 if !(v10 != 0) { break } if c == delim { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+int32(1))))) == delim { i++ } else { break } } goto _9 _9: ; i++ } if c == int32('\'') { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_STRING) return i + int32(1) } else { if c != 0 { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_ID) return i + int32(1) } else { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_ILLEGAL) return i } } fallthrough case int32(CC_DOT): if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + 1))])&libc.Int32FromInt32(0x04) != 0) { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_DOT) return int32(1) } fallthrough case int32(CC_DIGIT): *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_INTEGER) if int32(*(*uint8)(unsafe.Pointer(z))) == int32('0') && (int32(*(*uint8)(unsafe.Pointer(z + 1))) == int32('x') || int32(*(*uint8)(unsafe.Pointer(z + 1))) == int32('X')) && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + 2))])&int32(0x08) != 0 { i = int32(3) for { if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x08) != 0) { break } goto _11 _11: ; i++ } return i } i = 0 for { if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x04) != 0) { break } goto _12 _12: ; i++ } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) == int32('.') { i++ for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x04) != 0 { i++ } *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_FLOAT) } if (int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) == int32('e') || int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) == int32('E')) && (int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i+int32(1))))])&int32(0x04) != 0 || (int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+int32(1))))) == int32('+') || int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+int32(1))))) == int32('-')) && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i+int32(2))))])&int32(0x04) != 0) { i += int32(2) for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x04) != 0 { i++ } *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_FLOAT) } for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x46) != 0 { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_ILLEGAL) i++ } return i case int32(CC_QUOTE2): i = int32(1) c = int32(*(*uint8)(unsafe.Pointer(z))) for { if v15 = c != int32(']'); v15 { v14 = int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) c = v14 } if !(v15 && v14 != 0) { break } goto _13 _13: ; i++ } if c == int32(']') { v16 = int32(TK_ID) } else { v16 = int32(TK_ILLEGAL) } *(*int32)(unsafe.Pointer(tokenType)) = v16 return i case int32(CC_VARNUM): *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_VARIABLE) i = int32(1) for { if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x04) != 0) { break } goto _17 _17: ; i++ } return i case int32(CC_DOLLAR): fallthrough case int32(CC_VARALPHA): n = 0 *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_VARIABLE) i = int32(1) for { v19 = int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) c = v19 if !(v19 != 0) { break } if int32(_sqlite3CtypeMap[uint8(c)])&int32(0x46) != 0 { n++ } else { if c == int32('(') && n > 0 { for { i++ goto _21 _21: ; v20 = int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) c = v20 if !(v20 != 0 && !(int32(_sqlite3CtypeMap[uint8(c)])&libc.Int32FromInt32(0x01) != 0) && c != int32(')')) { break } } if c == int32(')') { i++ } else { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_ILLEGAL) } break } else { if c == int32(':') && int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+int32(1))))) == int32(':') { i++ } else { break } } } goto _18 _18: ; i++ } if n == 0 { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_ILLEGAL) } return i case int32(CC_KYWD0): if int32(_aiClass[*(*uint8)(unsafe.Pointer(z + 1))]) > int32(CC_KYWD) { i = int32(1) break } i = int32(2) for { if !(int32(_aiClass[*(*uint8)(unsafe.Pointer(z + uintptr(i)))]) <= int32(CC_KYWD)) { break } goto _22 _22: ; i++ } if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x46) != 0 { /* This token started out using characters that can appear in keywords, ** but z[i] is a character not allowed within keywords, so this must ** be an identifier instead */ i++ break } *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_ID) return _keywordCode(tls, z, i, tokenType) case CC_X: if int32(*(*uint8)(unsafe.Pointer(z + 1))) == int32('\'') { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_BLOB) i = int32(2) for { if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x08) != 0) { break } goto _23 _23: ; i++ } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) != int32('\'') || i%int32(2) != 0 { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_ILLEGAL) for *(*uint8)(unsafe.Pointer(z + uintptr(i))) != 0 && int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) != int32('\'') { i++ } } if *(*uint8)(unsafe.Pointer(z + uintptr(i))) != 0 { i++ } return i } fallthrough case int32(CC_KYWD): fallthrough case int32(CC_ID): i = int32(1) case int32(CC_BOM): if int32(*(*uint8)(unsafe.Pointer(z + 1))) == int32(0xbb) && int32(*(*uint8)(unsafe.Pointer(z + 2))) == int32(0xbf) { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_SPACE) return int32(3) } i = int32(1) case int32(CC_NUL): *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_ILLEGAL) return 0 default: *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_ILLEGAL) return int32(1) } for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x46) != 0 { i++ } *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_ID) return i } // C documentation // // /* // ** Run the parser on the given SQL string. // */ func _sqlite3RunParser(tls *libc.TLS, pParse uintptr, zSql uintptr) (r int32) { bp := tls.Alloc(1248) defer tls.Free(1248) var db, pEngine, pParentParse uintptr var lastTokenParsed, mxSqlLen, n, nErr int32 var _ /* sEngine at bp+4 */ TyyParser var _ /* tokenType at bp+0 */ int32 var _ /* x at bp+1216 */ TToken _, _, _, _, _, _, _ = db, lastTokenParsed, mxSqlLen, n, nErr, pEngine, pParentParse nErr = 0 /* The LEMON-generated LALR(1) parser */ n = 0 /* type of the next token */ lastTokenParsed = -int32(1) /* type of the previous token */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Max length of an SQL string */ pParentParse = uintptr(0) /* Space to hold the Lemon-generated Parser object */ _ = libc.Int32FromInt32(0) mxSqlLen = *(*int32)(unsafe.Pointer(db + 120 + 1*4)) if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive == 0 { libc.AtomicStoreNInt32(db+312, libc.Int32FromInt32(0), libc.Int32FromInt32(__ATOMIC_RELAXED)) } (*TParse)(unsafe.Pointer(pParse)).Frc = SQLITE_OK (*TParse)(unsafe.Pointer(pParse)).FzTail = zSql pEngine = bp + 4 _sqlite3ParserInit(tls, pEngine, pParse) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pParentParse = (*Tsqlite3)(unsafe.Pointer(db)).FpParse (*Tsqlite3)(unsafe.Pointer(db)).FpParse = pParse for int32(1) != 0 { n = _sqlite3GetToken(tls, zSql, bp) mxSqlLen -= n if mxSqlLen < 0 { (*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_TOOBIG) (*TParse)(unsafe.Pointer(pParse)).FnErr++ break } if *(*int32)(unsafe.Pointer(bp)) >= int32(TK_WINDOW) { _ = libc.Int32FromInt32(0) if libc.AtomicLoadNInt32(db+312, libc.Int32FromInt32(__ATOMIC_RELAXED)) != 0 { (*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_INTERRUPT) (*TParse)(unsafe.Pointer(pParse)).FnErr++ break } if *(*int32)(unsafe.Pointer(bp)) == int32(TK_SPACE) { zSql += uintptr(n) continue } if int32(*(*uint8)(unsafe.Pointer(zSql))) == 0 { /* Upon reaching the end of input, call the parser two more times ** with tokens TK_SEMI and 0, in that order. */ if lastTokenParsed == int32(TK_SEMI) { *(*int32)(unsafe.Pointer(bp)) = 0 } else { if lastTokenParsed == 0 { break } else { *(*int32)(unsafe.Pointer(bp)) = int32(TK_SEMI) } } n = 0 } else { if *(*int32)(unsafe.Pointer(bp)) == int32(TK_WINDOW) { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp)) = _analyzeWindowKeyword(tls, zSql+6) } else { if *(*int32)(unsafe.Pointer(bp)) == int32(TK_OVER) { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp)) = _analyzeOverKeyword(tls, zSql+4, lastTokenParsed) } else { if *(*int32)(unsafe.Pointer(bp)) == int32(TK_FILTER) { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp)) = _analyzeFilterKeyword(tls, zSql+6, lastTokenParsed) } else { (*(*TToken)(unsafe.Pointer(bp + 1216))).Fz = zSql (*(*TToken)(unsafe.Pointer(bp + 1216))).Fn = uint32(n) _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23699, libc.VaList(bp+1232, bp+1216)) break } } } } } (*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fz = zSql (*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fn = uint32(n) _sqlite3Parser(tls, pEngine, *(*int32)(unsafe.Pointer(bp)), (*TParse)(unsafe.Pointer(pParse)).FsLastToken) lastTokenParsed = *(*int32)(unsafe.Pointer(bp)) zSql += uintptr(n) _ = libc.Int32FromInt32(0) if (*TParse)(unsafe.Pointer(pParse)).Frc != SQLITE_OK { break } } _ = libc.Int32FromInt32(0) _sqlite3ParserFinalize(tls, pEngine) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { (*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM) } if (*TParse)(unsafe.Pointer(pParse)).FzErrMsg != 0 || (*TParse)(unsafe.Pointer(pParse)).Frc != SQLITE_OK && (*TParse)(unsafe.Pointer(pParse)).Frc != int32(SQLITE_DONE) { if (*TParse)(unsafe.Pointer(pParse)).FzErrMsg == uintptr(0) { (*TParse)(unsafe.Pointer(pParse)).FzErrMsg = _sqlite3MPrintf(tls, db, __ccgo_ts+3795, libc.VaList(bp+1232, _sqlite3ErrStr(tls, (*TParse)(unsafe.Pointer(pParse)).Frc))) } Xsqlite3_log(tls, (*TParse)(unsafe.Pointer(pParse)).Frc, __ccgo_ts+23724, libc.VaList(bp+1232, (*TParse)(unsafe.Pointer(pParse)).FzErrMsg, (*TParse)(unsafe.Pointer(pParse)).FzTail)) nErr++ } (*TParse)(unsafe.Pointer(pParse)).FzTail = zSql Xsqlite3_free(tls, (*TParse)(unsafe.Pointer(pParse)).FapVtabLock) if (*TParse)(unsafe.Pointer(pParse)).FpNewTable != 0 && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != libc.Int32FromInt32(PARSE_MODE_NORMAL)) { /* If the pParse->declareVtab flag is set, do not delete any table ** structure built up in pParse->pNewTable. The calling code (see vtab.c) ** will take responsibility for freeing the Table structure. */ _sqlite3DeleteTable(tls, db, (*TParse)(unsafe.Pointer(pParse)).FpNewTable) } if (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger != 0 && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) { _sqlite3DeleteTrigger(tls, db, (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger) } if (*TParse)(unsafe.Pointer(pParse)).FpVList != 0 { _sqlite3DbNNFreeNN(tls, db, (*TParse)(unsafe.Pointer(pParse)).FpVList) } (*Tsqlite3)(unsafe.Pointer(db)).FpParse = pParentParse _ = libc.Int32FromInt32(0) return nErr } /************** End of tokenize.c ********************************************/ /************** Begin file complete.c ****************************************/ /* ** 2001 September 15 ** ** 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. ** ************************************************************************* ** An tokenizer for SQL ** ** This file contains C code that implements the sqlite3_complete() API. ** This code used to be part of the tokenizer.c source file. But by ** separating it out, the code will be automatically omitted from ** static links that do not use it. */ /* #include "sqliteInt.h" */ /* ** This is defined in tokenize.c. We just have to import the definition. */ /* ** Token types used by the sqlite3_complete() routine. See the header ** comments on that procedure for additional information. */ // C documentation // // /* // ** Return TRUE if the given SQL string ends in a semicolon. // ** // ** Special handling is require for CREATE TRIGGER statements. // ** Whenever the CREATE TRIGGER keywords are seen, the statement // ** must end with ";END;". // ** // ** This implementation uses a state machine with 8 states: // ** // ** (0) INVALID We have not yet seen a non-whitespace character. // ** // ** (1) START At the beginning or end of an SQL statement. This routine // ** returns 1 if it ends in the START state and 0 if it ends // ** in any other state. // ** // ** (2) NORMAL We are in the middle of statement which ends with a single // ** semicolon. // ** // ** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of // ** a statement. // ** // ** (4) CREATE The keyword CREATE has been seen at the beginning of a // ** statement, possibly preceded by EXPLAIN and/or followed by // ** TEMP or TEMPORARY // ** // ** (5) TRIGGER We are in the middle of a trigger definition that must be // ** ended by a semicolon, the keyword END, and another semicolon. // ** // ** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at // ** the end of a trigger definition. // ** // ** (7) END We've seen the ";END" of the ";END;" that occurs at the end // ** of a trigger definition. // ** // ** Transitions between states above are determined by tokens extracted // ** from the input. The following tokens are significant: // ** // ** (0) tkSEMI A semicolon. // ** (1) tkWS Whitespace. // ** (2) tkOTHER Any other SQL token. // ** (3) tkEXPLAIN The "explain" keyword. // ** (4) tkCREATE The "create" keyword. // ** (5) tkTEMP The "temp" or "temporary" keyword. // ** (6) tkTRIGGER The "trigger" keyword. // ** (7) tkEND The "end" keyword. // ** // ** Whitespace never causes a state transition and is always ignored. // ** This means that a SQL string of all whitespace is invalid. // ** // ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed // ** to recognize the end of a trigger can be omitted. All we have to do // ** is look for a semicolon that is not part of an string or comment. // */ func Xsqlite3_complete(tls *libc.TLS, zSql uintptr) (r int32) { var c, nId int32 var state, token Tu8 _, _, _, _ = c, nId, state, token state = uint8(0) /* Value of the next token */ for *(*uint8)(unsafe.Pointer(zSql)) != 0 { switch int32(*(*uint8)(unsafe.Pointer(zSql))) { case int32(';'): /* A semicolon */ token = uint8(tkSEMI) case int32(' '): fallthrough case int32('\r'): fallthrough case int32('\t'): fallthrough case int32('\n'): fallthrough case int32('\f'): /* White space is ignored */ token = uint8(tkWS) case int32('/'): /* C-style comments */ if int32(*(*uint8)(unsafe.Pointer(zSql + 1))) != int32('*') { token = uint8(tkOTHER) break } zSql += uintptr(2) for *(*uint8)(unsafe.Pointer(zSql)) != 0 && (int32(*(*uint8)(unsafe.Pointer(zSql))) != int32('*') || int32(*(*uint8)(unsafe.Pointer(zSql + 1))) != int32('/')) { zSql++ } if int32(*(*uint8)(unsafe.Pointer(zSql))) == 0 { return 0 } zSql++ token = uint8(tkWS) case int32('-'): /* SQL-style comments from "--" to end of line */ if int32(*(*uint8)(unsafe.Pointer(zSql + 1))) != int32('-') { token = uint8(tkOTHER) break } for *(*uint8)(unsafe.Pointer(zSql)) != 0 && int32(*(*uint8)(unsafe.Pointer(zSql))) != int32('\n') { zSql++ } if int32(*(*uint8)(unsafe.Pointer(zSql))) == 0 { return libc.BoolInt32(int32(state) == int32(1)) } token = uint8(tkWS) case int32('['): /* Microsoft-style identifiers in [...] */ zSql++ for *(*uint8)(unsafe.Pointer(zSql)) != 0 && int32(*(*uint8)(unsafe.Pointer(zSql))) != int32(']') { zSql++ } if int32(*(*uint8)(unsafe.Pointer(zSql))) == 0 { return 0 } token = uint8(tkOTHER) case int32('`'): /* Grave-accent quoted symbols used by MySQL */ fallthrough case int32('"'): /* single- and double-quoted strings */ fallthrough case int32('\''): c = int32(*(*uint8)(unsafe.Pointer(zSql))) zSql++ for *(*uint8)(unsafe.Pointer(zSql)) != 0 && int32(*(*uint8)(unsafe.Pointer(zSql))) != c { zSql++ } if int32(*(*uint8)(unsafe.Pointer(zSql))) == 0 { return 0 } token = uint8(tkOTHER) default: if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zSql))])&int32(0x46) != 0 { nId = int32(1) for { if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zSql + uintptr(nId)))])&int32(0x46) != 0) { break } goto _1 _1: ; nId++ } switch int32(*(*uint8)(unsafe.Pointer(zSql))) { case int32('c'): fallthrough case int32('C'): if nId == int32(6) && Xsqlite3_strnicmp(tls, zSql, __ccgo_ts+23735, int32(6)) == 0 { token = uint8(tkCREATE) } else { token = uint8(tkOTHER) } case int32('t'): fallthrough case int32('T'): if nId == int32(7) && Xsqlite3_strnicmp(tls, zSql, __ccgo_ts+20379, int32(7)) == 0 { token = uint8(tkTRIGGER) } else { if nId == int32(4) && Xsqlite3_strnicmp(tls, zSql, __ccgo_ts+23742, int32(4)) == 0 { token = uint8(tkTEMP) } else { if nId == int32(9) && Xsqlite3_strnicmp(tls, zSql, __ccgo_ts+23747, int32(9)) == 0 { token = uint8(tkTEMP) } else { token = uint8(tkOTHER) } } } case int32('e'): fallthrough case int32('E'): if nId == int32(3) && Xsqlite3_strnicmp(tls, zSql, __ccgo_ts+23757, int32(3)) == 0 { token = uint8(tkEND) } else { if nId == int32(7) && Xsqlite3_strnicmp(tls, zSql, __ccgo_ts+23761, int32(7)) == 0 { token = uint8(tkEXPLAIN) } else { token = uint8(tkOTHER) } } default: token = uint8(tkOTHER) break } zSql += uintptr(nId - int32(1)) } else { /* Operators and special symbols */ token = uint8(tkOTHER) } break } state = *(*Tu8)(unsafe.Pointer(uintptr(unsafe.Pointer(&_trans)) + uintptr(state)*8 + uintptr(token))) zSql++ } return libc.BoolInt32(int32(state) == int32(1)) } /* A complex statement machine used to detect the end of a CREATE TRIGGER ** statement. This is the normal case. */ var _trans = [8][8]Tu8{ 0: { 0: uint8(1), 2: uint8(2), 3: uint8(3), 4: uint8(4), 5: uint8(2), 6: uint8(2), 7: uint8(2), }, 1: { 0: uint8(1), 1: uint8(1), 2: uint8(2), 3: uint8(3), 4: uint8(4), 5: uint8(2), 6: uint8(2), 7: uint8(2), }, 2: { 0: uint8(1), 1: uint8(2), 2: uint8(2), 3: uint8(2), 4: uint8(2), 5: uint8(2), 6: uint8(2), 7: uint8(2), }, 3: { 0: uint8(1), 1: uint8(3), 2: uint8(3), 3: uint8(2), 4: uint8(4), 5: uint8(2), 6: uint8(2), 7: uint8(2), }, 4: { 0: uint8(1), 1: uint8(4), 2: uint8(2), 3: uint8(2), 4: uint8(2), 5: uint8(4), 6: uint8(5), 7: uint8(2), }, 5: { 0: uint8(6), 1: uint8(5), 2: uint8(5), 3: uint8(5), 4: uint8(5), 5: uint8(5), 6: uint8(5), 7: uint8(5), }, 6: { 0: uint8(6), 1: uint8(6), 2: uint8(5), 3: uint8(5), 4: uint8(5), 5: uint8(5), 6: uint8(5), 7: uint8(7), }, 7: { 0: uint8(1), 1: uint8(7), 2: uint8(5), 3: uint8(5), 4: uint8(5), 5: uint8(5), 6: uint8(5), 7: uint8(5), }, } // C documentation // // /* // ** This routine is the same as the sqlite3_complete() routine described // ** above, except that the parameter is required to be UTF-16 encoded, not // ** UTF-8. // */ func Xsqlite3_complete16(tls *libc.TLS, zSql uintptr) (r int32) { var pVal, zSql8 uintptr var rc int32 _, _, _ = pVal, rc, zSql8 rc = Xsqlite3_initialize(tls) if rc != 0 { return rc } pVal = _sqlite3ValueNew(tls, uintptr(0)) _sqlite3ValueSetStr(tls, pVal, -int32(1), zSql, uint8(SQLITE_UTF16LE), libc.UintptrFromInt32(0)) zSql8 = _sqlite3ValueText(tls, pVal, uint8(SQLITE_UTF8)) if zSql8 != 0 { rc = Xsqlite3_complete(tls, zSql8) } else { rc = int32(SQLITE_NOMEM) } _sqlite3ValueFree(tls, pVal) return rc & int32(0xff) } /************** End of rtree.h ***********************************************/ /************** Continuing where we left off in main.c ***********************/ // C documentation // // /* // ** This is an extension initializer that is a no-op and always // ** succeeds, except that it fails if the fault-simulation is set // ** to 500. // */ func _sqlite3TestExtInit(tls *libc.TLS, db uintptr) (r int32) { _ = db return _sqlite3FaultSim(tls, int32(500)) } // C documentation // // /* // ** An array of pointers to extension initializer functions for // ** built-in extensions. // */ var _sqlite3BuiltinExtensions = [5]uintptr{} func init() { p := unsafe.Pointer(&_sqlite3BuiltinExtensions) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_sqlite3Fts5Init) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_sqlite3RtreeInit) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_sqlite3DbstatRegister) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_sqlite3TestExtInit) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_sqlite3JsonTableFunctions) } // C documentation // // /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns // ** a pointer to the to the sqlite3_version[] string constant. // */ func Xsqlite3_libversion(tls *libc.TLS) (r uintptr) { return uintptr(unsafe.Pointer(&Xsqlite3_version)) } /* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a ** pointer to a string constant whose value is the same as the ** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using ** an edited copy of the amalgamation, then the last four characters of ** the hash might be different from SQLITE_SOURCE_ID. */ /* SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } */ // C documentation // // /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function // ** returns an integer equal to SQLITE_VERSION_NUMBER. // */ func Xsqlite3_libversion_number(tls *libc.TLS) (r int32) { return int32(SQLITE_VERSION_NUMBER) } // C documentation // // /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns // ** zero if and only if SQLite was compiled with mutexing code omitted due to // ** the SQLITE_THREADSAFE compile-time option being set to 0. // */ func Xsqlite3_threadsafe(tls *libc.TLS) (r int32) { return int32(SQLITE_THREADSAFE) } // C documentation // // /* // ** Determine whether or not high-precision (long double) floating point // ** math works correctly on CPU currently running. // */ func _hasHighPrecisionDouble(tls *libc.TLS, rc int32) (r int32) { var a, b, c float64 _, _, _ = a, b, c if uint32(8) <= uint32(8) { /* If the size of "long double" is not more than 8, then ** high-precision math is not possible. */ return 0 } else { rc++ a = float64(1) + float64(rc)*float64(0.1) b = float64(1e+18) + float64(rc)*float64(25) c = a + b return libc.BoolInt32(b != c) } return r } // C documentation // // /* // ** Initialize SQLite. // ** // ** This routine must be called to initialize the memory allocation, // ** VFS, and mutex subsystems prior to doing any serious work with // ** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT // ** this routine will be called automatically by key routines such as // ** sqlite3_open(). // ** // ** This routine is a no-op except on its very first call for the process, // ** or for the first call after a call to sqlite3_shutdown. // ** // ** The first thread to call this routine runs the initialization to // ** completion. If subsequent threads call this routine before the first // ** thread has finished the initialization process, then the subsequent // ** threads must block until the first thread finishes with the initialization. // ** // ** The first thread might call this routine recursively. Recursive // ** calls to this routine should not block, of course. Otherwise the // ** initialization process would never complete. // ** // ** Let X be the first thread to enter this routine. Let Y be some other // ** thread. Then while the initial invocation of this routine by X is // ** incomplete, it is required that: // ** // ** * Calls to this routine from Y must block until the outer-most // ** call by X completes. // ** // ** * Recursive calls to this routine from thread X return immediately // ** without blocking. // */ func Xsqlite3_initialize(tls *libc.TLS) (r int32) { var pMainMtx uintptr var rc int32 _, _ = pMainMtx, rc /* Result code */ /* If the following assert() fails on some obscure processor/compiler ** combination, the work-around is to set the correct pointer ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */ _ = libc.Int32FromInt32(0) /* If SQLite is already completely initialized, then this call ** to sqlite3_initialize() should be a no-op. But the initialization ** must be complete. So isInit must not be set until the very end ** of this routine. */ if libc.AtomicLoadPInt32(uintptr(unsafe.Pointer(&_sqlite3Config))+208) != 0 { return SQLITE_OK } /* Make sure the mutex subsystem is initialized. If unable to ** initialize the mutex subsystem, return early with the error. ** If the system is so sick that we are unable to allocate a mutex, ** there is not much SQLite is going to be able to do. ** ** The mutex subsystem must take care of serializing its own ** initialization. */ rc = _sqlite3MutexInit(tls) if rc != 0 { return rc } /* Initialize the malloc() system and the recursive pInitMutex mutex. ** This operation is protected by the STATIC_MAIN mutex. Note that ** MutexAlloc() is called for a static mutex prior to initializing the ** malloc subsystem - this implies that the allocation of a static ** mutex must not require support from the malloc subsystem. */ pMainMtx = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN)) Xsqlite3_mutex_enter(tls, pMainMtx) _sqlite3Config.FisMutexInit = int32(1) if !(_sqlite3Config.FisMallocInit != 0) { rc = _sqlite3MallocInit(tls) } if rc == SQLITE_OK { _sqlite3Config.FisMallocInit = int32(1) if !(_sqlite3Config.FpInitMutex != 0) { _sqlite3Config.FpInitMutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_RECURSIVE)) if _sqlite3Config.FbCoreMutex != 0 && !(_sqlite3Config.FpInitMutex != 0) { rc = int32(SQLITE_NOMEM) } } } if rc == SQLITE_OK { _sqlite3Config.FnRefInitMutex++ } Xsqlite3_mutex_leave(tls, pMainMtx) /* If rc is not SQLITE_OK at this point, then either the malloc ** subsystem could not be initialized or the system failed to allocate ** the pInitMutex mutex. Return an error in either case. */ if rc != SQLITE_OK { return rc } /* Do the rest of the initialization under the recursive mutex so ** that we will be able to handle recursive calls into ** sqlite3_initialize(). The recursive calls normally come through ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other ** recursive calls might also be possible. ** ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls ** to the xInit method, so the xInit method need not be threadsafe. ** ** The following mutex is what serializes access to the appdef pcache xInit ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the ** call to sqlite3PcacheInitialize(). */ Xsqlite3_mutex_enter(tls, _sqlite3Config.FpInitMutex) if libc.AtomicLoadPInt32(uintptr(unsafe.Pointer(&_sqlite3Config))+208) == 0 && _sqlite3Config.FinProgress == 0 { _sqlite3Config.FinProgress = int32(1) libc.Xmemset(tls, uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)), 0, uint32(92)) _sqlite3RegisterBuiltinFunctions(tls) if _sqlite3Config.FisPCacheInit == 0 { rc = _sqlite3PcacheInitialize(tls) } if rc == SQLITE_OK { _sqlite3Config.FisPCacheInit = int32(1) rc = _sqlite3OsInit(tls) } if rc == SQLITE_OK { rc = _sqlite3MemdbInit(tls) } if rc == SQLITE_OK { _sqlite3PCacheBufferSetup(tls, _sqlite3Config.FpPage, _sqlite3Config.FszPage, _sqlite3Config.FnPage) libc.AtomicStorePInt32(uintptr(unsafe.Pointer(&_sqlite3Config))+208, int32(1)) } _sqlite3Config.FinProgress = 0 } Xsqlite3_mutex_leave(tls, _sqlite3Config.FpInitMutex) /* Go back under the static mutex and clean up the recursive ** mutex to prevent a resource leak. */ Xsqlite3_mutex_enter(tls, pMainMtx) _sqlite3Config.FnRefInitMutex-- if _sqlite3Config.FnRefInitMutex <= 0 { _ = libc.Int32FromInt32(0) Xsqlite3_mutex_free(tls, _sqlite3Config.FpInitMutex) _sqlite3Config.FpInitMutex = uintptr(0) } Xsqlite3_mutex_leave(tls, pMainMtx) /* The following is just a sanity check to make sure SQLite has ** been compiled correctly. It is important to run this code, but ** we don't want to run it too often and soak up CPU cycles for no ** reason. So we run it once during initialization. */ /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT ** compile-time option. */ /* Experimentally determine if high-precision floating point is ** available. */ // disabled return rc } // C documentation // // /* // ** Undo the effects of sqlite3_initialize(). Must not be called while // ** there are outstanding database connections or memory allocations or // ** while any part of SQLite is otherwise in use in any thread. This // ** routine is not threadsafe. But it is safe to invoke this routine // ** on when SQLite is already shut down. If SQLite is already shut down // ** when this routine is invoked, then this routine is a harmless no-op. // */ func Xsqlite3_shutdown(tls *libc.TLS) (r int32) { if libc.AtomicLoadPInt32(uintptr(unsafe.Pointer(&_sqlite3Config))+208) != 0 { Xsqlite3_os_end(tls) Xsqlite3_reset_auto_extension(tls) libc.AtomicStorePInt32(uintptr(unsafe.Pointer(&_sqlite3Config))+208, 0) } if _sqlite3Config.FisPCacheInit != 0 { _sqlite3PcacheShutdown(tls) _sqlite3Config.FisPCacheInit = 0 } if _sqlite3Config.FisMallocInit != 0 { _sqlite3MallocEnd(tls) _sqlite3Config.FisMallocInit = 0 /* The heap subsystem has now been shutdown and these values are supposed ** to be NULL or point to memory that was obtained from sqlite3_malloc(), ** which would rely on that heap subsystem; therefore, make sure these ** values cannot refer to heap memory that was just invalidated when the ** heap subsystem was shutdown. This is only done if the current call to ** this function resulted in the heap subsystem actually being shutdown. */ Xsqlite3_data_directory = uintptr(0) Xsqlite3_temp_directory = uintptr(0) } if _sqlite3Config.FisMutexInit != 0 { _sqlite3MutexEnd(tls) _sqlite3Config.FisMutexInit = 0 } return SQLITE_OK } // C documentation // // /* // ** This API allows applications to modify the global configuration of // ** the SQLite library at run-time. // ** // ** This routine should only be called when there are no outstanding // ** database connections or memory allocations. This routine is not // ** threadsafe. Failure to heed these warnings can lead to unpredictable // ** behavior. // */ func Xsqlite3_config(tls *libc.TLS, op int32, va uintptr) (r int32) { var ap Tva_list var bOpenUri, rc int32 var mxMmap, szMmap Tsqlite3_int64 var pLogArg, pVal, xLog uintptr _, _, _, _, _, _, _, _ = ap, bOpenUri, mxMmap, pLogArg, pVal, rc, szMmap, xLog rc = SQLITE_OK /* sqlite3_config() normally returns SQLITE_MISUSE if it is invoked while ** the SQLite library is in use. Except, a few selected opcodes ** are allowed. */ if libc.AtomicLoadPInt32(uintptr(unsafe.Pointer(&_sqlite3Config))+208) != 0 { if op < 0 || op > int32(63) || libc.Uint64FromInt32(1)< int64(SQLITE_MAX_MMAP_SIZE) { mxMmap = int64(SQLITE_MAX_MMAP_SIZE) } if szMmap < 0 { szMmap = SQLITE_DEFAULT_MMAP_SIZE } if szMmap > mxMmap { szMmap = mxMmap } _sqlite3Config.FmxMmap = mxMmap _sqlite3Config.FszMmap = szMmap case int32(SQLITE_CONFIG_PMASZ): _sqlite3Config.FszPma = libc.VaUint32(&ap) case int32(SQLITE_CONFIG_STMTJRNL_SPILL): _sqlite3Config.FnStmtSpill = libc.VaInt32(&ap) case int32(SQLITE_CONFIG_MEMDB_MAXSIZE): _sqlite3Config.FmxMemdbSize = libc.VaInt64(&ap) case int32(SQLITE_CONFIG_ROWID_IN_VIEW): pVal = libc.VaUintptr(&ap) *(*int32)(unsafe.Pointer(pVal)) = 0 default: rc = int32(SQLITE_ERROR) break } _ = ap return rc } var _mAnytimeConfigOption = libc.Uint64FromInt32(0) | libc.Uint64FromInt32(1)< 0 { return int32(SQLITE_BUSY) } /* Free any existing lookaside buffer for this handle before ** allocating a new one so we don't have to have space for ** both at the same time. */ if (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbMalloced != 0 { Xsqlite3_free(tls, (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart) } /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger ** than a pointer to be useful. */ sz = sz & ^libc.Int32FromInt32(7) /* IMP: R-33038-09382 */ if sz <= libc.Int32FromInt64(4) { sz = 0 } if cnt < 0 { cnt = 0 } if sz == 0 || cnt == 0 { sz = 0 pStart = uintptr(0) } else { if pBuf == uintptr(0) { _sqlite3BeginBenignMalloc(tls) pStart = _sqlite3Malloc(tls, uint64(szAlloc)) /* IMP: R-61949-35727 */ _sqlite3EndBenignMalloc(tls) if pStart != 0 { szAlloc = int64(_sqlite3MallocSize(tls, pStart)) } } else { pStart = pBuf } } if sz >= libc.Int32FromInt32(LOOKASIDE_SMALL)*libc.Int32FromInt32(3) { nBig = int32(szAlloc / int64(libc.Int32FromInt32(3)*libc.Int32FromInt32(LOOKASIDE_SMALL)+sz)) nSm = int32((szAlloc - int64(sz*nBig)) / int64(LOOKASIDE_SMALL)) } else { if sz >= libc.Int32FromInt32(LOOKASIDE_SMALL)*libc.Int32FromInt32(2) { nBig = int32(szAlloc / int64(libc.Int32FromInt32(LOOKASIDE_SMALL)+sz)) nSm = int32((szAlloc - int64(sz*nBig)) / int64(LOOKASIDE_SMALL)) } else { if sz > 0 { nBig = int32(szAlloc / int64(sz)) nSm = 0 } else { v1 = libc.Int32FromInt32(0) nSm = v1 nBig = v1 } } } (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart = pStart (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpInit = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(sz) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue = uint16(sz) if pStart != 0 { _ = libc.Int32FromInt32(0) p = pStart i = 0 for { if !(i < nBig) { break } (*TLookasideSlot)(unsafe.Pointer(p)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpInit (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpInit = p p = p + uintptr(sz) goto _2 _2: ; i++ } (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpMiddle = p i = 0 for { if !(i < nSm) { break } (*TLookasideSlot)(unsafe.Pointer(p)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit = p p = p + 128 goto _3 _3: ; i++ } _ = libc.Int32FromInt32(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = p (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = uint32(0) if pBuf == uintptr(0) { v4 = int32(1) } else { v4 = 0 } (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbMalloced = uint8(v4) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FnSlot = uint32(nBig + nSm) } else { (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpMiddle = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = uint32(1) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbMalloced = uint8(0) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FnSlot = uint32(0) } (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpTrueEnd = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd _ = libc.Int32FromInt32(0) return SQLITE_OK } // C documentation // // /* // ** Return the mutex associated with a database connection. // */ func Xsqlite3_db_mutex(tls *libc.TLS, db uintptr) (r uintptr) { return (*Tsqlite3)(unsafe.Pointer(db)).Fmutex } // C documentation // // /* // ** Free up as much memory as we can from the given database // ** connection. // */ func Xsqlite3_db_release_memory(tls *libc.TLS, db uintptr) (r int32) { var i int32 var pBt, pPager uintptr _, _, _ = i, pBt, pPager Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) _sqlite3BtreeEnterAll(tls, db) i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt if pBt != 0 { pPager = _sqlite3BtreePager(tls, pBt) _sqlite3PagerShrink(tls, pPager) } goto _1 _1: ; i++ } _sqlite3BtreeLeaveAll(tls, db) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return SQLITE_OK } // C documentation // // /* // ** Flush any dirty pages in the pager-cache for any attached database // ** to disk. // */ func Xsqlite3_db_cacheflush(tls *libc.TLS, db uintptr) (r int32) { var bSeenBusy, i, rc, v2 int32 var pBt, pPager uintptr _, _, _, _, _, _ = bSeenBusy, i, pBt, pPager, rc, v2 rc = SQLITE_OK bSeenBusy = 0 Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) _sqlite3BtreeEnterAll(tls, db) i = 0 for { if !(rc == SQLITE_OK && i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt if pBt != 0 && _sqlite3BtreeTxnState(tls, pBt) == int32(SQLITE_TXN_WRITE) { pPager = _sqlite3BtreePager(tls, pBt) rc = _sqlite3PagerFlush(tls, pPager) if rc == int32(SQLITE_BUSY) { bSeenBusy = int32(1) rc = SQLITE_OK } } goto _1 _1: ; i++ } _sqlite3BtreeLeaveAll(tls, db) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if rc == SQLITE_OK && bSeenBusy != 0 { v2 = int32(SQLITE_BUSY) } else { v2 = rc } return v2 } // C documentation // // /* // ** Configuration settings for an individual database connection // */ func Xsqlite3_db_config(tls *libc.TLS, db uintptr, op int32, va uintptr) (r int32) { var ap Tva_list var cnt, onoff, rc, sz int32 var i uint32 var oldFlags Tu64 var pBuf, pRes uintptr _, _, _, _, _, _, _, _, _ = ap, cnt, i, oldFlags, onoff, pBuf, pRes, rc, sz Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) ap = va switch op { case int32(SQLITE_DBCONFIG_MAINDBNAME): /* IMP: R-06824-28531 */ /* IMP: R-36257-52125 */ (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FzDbSName = libc.VaUintptr(&ap) rc = SQLITE_OK case int32(SQLITE_DBCONFIG_LOOKASIDE): pBuf = libc.VaUintptr(&ap) /* IMP: R-26835-10964 */ sz = libc.VaInt32(&ap) /* IMP: R-47871-25994 */ cnt = libc.VaInt32(&ap) /* IMP: R-04460-53386 */ rc = _setupLookaside(tls, db, pBuf, sz, cnt) default: rc = int32(SQLITE_ERROR) /* IMP: R-42790-23372 */ i = uint32(0) for { if !(i < uint32(int32(libc.Uint32FromInt64(144)/libc.Uint32FromInt64(8)))) { break } if _aFlagOp[i].Fop == op { onoff = libc.VaInt32(&ap) pRes = libc.VaUintptr(&ap) oldFlags = (*Tsqlite3)(unsafe.Pointer(db)).Fflags if onoff > 0 { *(*Tu64)(unsafe.Pointer(db + 32)) |= uint64(_aFlagOp[i].Fmask) } else { if onoff == 0 { *(*Tu64)(unsafe.Pointer(db + 32)) &= ^uint64(_aFlagOp[i].Fmask) } } if oldFlags != (*Tsqlite3)(unsafe.Pointer(db)).Fflags { _sqlite3ExpirePreparedStatements(tls, db, 0) } if pRes != 0 { *(*int32)(unsafe.Pointer(pRes)) = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(_aFlagOp[i].Fmask) != uint64(0)) } rc = SQLITE_OK break } goto _1 _1: ; i++ } break } _ = ap Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } var _aFlagOp = [18]struct { Fop int32 Fmask Tu32 }{ 0: { Fop: int32(SQLITE_DBCONFIG_ENABLE_FKEY), Fmask: uint32(SQLITE_ForeignKeys), }, 1: { Fop: int32(SQLITE_DBCONFIG_ENABLE_TRIGGER), Fmask: uint32(SQLITE_EnableTrigger), }, 2: { Fop: int32(SQLITE_DBCONFIG_ENABLE_VIEW), Fmask: uint32(SQLITE_EnableView), }, 3: { Fop: int32(SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER), Fmask: uint32(SQLITE_Fts3Tokenizer), }, 4: { Fop: int32(SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION), Fmask: uint32(SQLITE_LoadExtension), }, 5: { Fop: int32(SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE), Fmask: uint32(SQLITE_NoCkptOnClose), }, 6: { Fop: int32(SQLITE_DBCONFIG_ENABLE_QPSG), Fmask: uint32(SQLITE_EnableQPSG), }, 7: { Fop: int32(SQLITE_DBCONFIG_TRIGGER_EQP), Fmask: uint32(SQLITE_TriggerEQP), }, 8: { Fop: int32(SQLITE_DBCONFIG_RESET_DATABASE), Fmask: uint32(SQLITE_ResetDatabase), }, 9: { Fop: int32(SQLITE_DBCONFIG_DEFENSIVE), Fmask: uint32(SQLITE_Defensive), }, 10: { Fop: int32(SQLITE_DBCONFIG_WRITABLE_SCHEMA), Fmask: uint32(libc.Int32FromInt32(SQLITE_WriteSchema) | libc.Int32FromInt32(SQLITE_NoSchemaError)), }, 11: { Fop: int32(SQLITE_DBCONFIG_LEGACY_ALTER_TABLE), Fmask: uint32(SQLITE_LegacyAlter), }, 12: { Fop: int32(SQLITE_DBCONFIG_DQS_DDL), Fmask: uint32(SQLITE_DqsDDL), }, 13: { Fop: int32(SQLITE_DBCONFIG_DQS_DML), Fmask: uint32(SQLITE_DqsDML), }, 14: { Fop: int32(SQLITE_DBCONFIG_LEGACY_FILE_FORMAT), Fmask: uint32(SQLITE_LegacyFileFmt), }, 15: { Fop: int32(SQLITE_DBCONFIG_TRUSTED_SCHEMA), Fmask: uint32(SQLITE_TrustedSchema), }, 16: { Fop: int32(SQLITE_DBCONFIG_STMT_SCANSTATUS), Fmask: uint32(SQLITE_StmtScanStatus), }, 17: { Fop: int32(SQLITE_DBCONFIG_REVERSE_SCANORDER), Fmask: uint32(SQLITE_ReverseOrder), }, } // C documentation // // /* // ** This is the default collating function named "BINARY" which is always // ** available. // */ func _binCollFunc(tls *libc.TLS, NotUsed uintptr, nKey1 int32, pKey1 uintptr, nKey2 int32, pKey2 uintptr) (r int32) { var n, rc, v1 int32 _, _, _ = n, rc, v1 _ = NotUsed if nKey1 < nKey2 { v1 = nKey1 } else { v1 = nKey2 } n = v1 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares ** strings byte by byte using the memcmp() function from the standard C ** library. */ _ = libc.Int32FromInt32(0) rc = libc.Xmemcmp(tls, pKey1, pKey2, uint32(n)) if rc == 0 { rc = nKey1 - nKey2 } return rc } // C documentation // // /* // ** This is the collating function named "RTRIM" which is always // ** available. Ignore trailing spaces. // */ func _rtrimCollFunc(tls *libc.TLS, pUser uintptr, nKey1 int32, pKey1 uintptr, nKey2 int32, pKey2 uintptr) (r int32) { var pK1, pK2 uintptr _, _ = pK1, pK2 pK1 = pKey1 pK2 = pKey2 for nKey1 != 0 && int32(*(*Tu8)(unsafe.Pointer(pK1 + uintptr(nKey1-int32(1))))) == int32(' ') { nKey1-- } for nKey2 != 0 && int32(*(*Tu8)(unsafe.Pointer(pK2 + uintptr(nKey2-int32(1))))) == int32(' ') { nKey2-- } return _binCollFunc(tls, pUser, nKey1, pKey1, nKey2, pKey2) } // C documentation // // /* // ** Return true if CollSeq is the default built-in BINARY. // */ func _sqlite3IsBinary(tls *libc.TLS, p uintptr) (r int32) { _ = libc.Int32FromInt32(0) return libc.BoolInt32(p == uintptr(0) || (*TCollSeq)(unsafe.Pointer(p)).FxCmp == __ccgo_fp(_binCollFunc)) } // C documentation // // /* // ** Another built-in collating sequence: NOCASE. // ** // ** This collating sequence is intended to be used for "case independent // ** comparison". SQLite's knowledge of upper and lower case equivalents // ** extends only to the 26 characters used in the English language. // ** // ** At the moment there is only a UTF-8 implementation. // */ func _nocaseCollatingFunc(tls *libc.TLS, NotUsed uintptr, nKey1 int32, pKey1 uintptr, nKey2 int32, pKey2 uintptr) (r1 int32) { var r, v1 int32 _, _ = r, v1 if nKey1 < nKey2 { v1 = nKey1 } else { v1 = nKey2 } r = Xsqlite3_strnicmp(tls, pKey1, pKey2, v1) _ = NotUsed if 0 == r { r = nKey1 - nKey2 } return r } // C documentation // // /* // ** Return the ROWID of the most recent insert // */ func Xsqlite3_last_insert_rowid(tls *libc.TLS, db uintptr) (r Tsqlite_int64) { return (*Tsqlite3)(unsafe.Pointer(db)).FlastRowid } // C documentation // // /* // ** Set the value returned by the sqlite3_last_insert_rowid() API function. // */ func Xsqlite3_set_last_insert_rowid(tls *libc.TLS, db uintptr, iRowid Tsqlite3_int64) { Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) (*Tsqlite3)(unsafe.Pointer(db)).FlastRowid = iRowid Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) } // C documentation // // /* // ** Return the number of changes in the most recent call to sqlite3_exec(). // */ func Xsqlite3_changes64(tls *libc.TLS, db uintptr) (r Tsqlite3_int64) { return (*Tsqlite3)(unsafe.Pointer(db)).FnChange } func Xsqlite3_changes(tls *libc.TLS, db uintptr) (r int32) { return int32(Xsqlite3_changes64(tls, db)) } // C documentation // // /* // ** Return the number of changes since the database handle was opened. // */ func Xsqlite3_total_changes64(tls *libc.TLS, db uintptr) (r Tsqlite3_int64) { return (*Tsqlite3)(unsafe.Pointer(db)).FnTotalChange } func Xsqlite3_total_changes(tls *libc.TLS, db uintptr) (r int32) { return int32(Xsqlite3_total_changes64(tls, db)) } // C documentation // // /* // ** Close all open savepoints. This function only manipulates fields of the // ** database handle object, it does not close any savepoints that may be open // ** at the b-tree/pager level. // */ func _sqlite3CloseSavepoints(tls *libc.TLS, db uintptr) { var pTmp uintptr _ = pTmp for (*Tsqlite3)(unsafe.Pointer(db)).FpSavepoint != 0 { pTmp = (*Tsqlite3)(unsafe.Pointer(db)).FpSavepoint (*Tsqlite3)(unsafe.Pointer(db)).FpSavepoint = (*TSavepoint)(unsafe.Pointer(pTmp)).FpNext _sqlite3DbFree(tls, db, pTmp) } (*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint = 0 (*Tsqlite3)(unsafe.Pointer(db)).FnStatement = 0 (*Tsqlite3)(unsafe.Pointer(db)).FisTransactionSavepoint = uint8(0) } // C documentation // // /* // ** Invoke the destructor function associated with FuncDef p, if any. Except, // ** if this is not the last copy of the function, do not invoke it. Multiple // ** copies of a single function are created when create_function() is called // ** with SQLITE_ANY as the encoding. // */ func _functionDestroy(tls *libc.TLS, db uintptr, p uintptr) { var pDestructor uintptr _ = pDestructor _ = libc.Int32FromInt32(0) pDestructor = *(*uintptr)(unsafe.Pointer(p + 36)) if pDestructor != 0 { (*TFuncDestructor)(unsafe.Pointer(pDestructor)).FnRef-- if (*TFuncDestructor)(unsafe.Pointer(pDestructor)).FnRef == 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFuncDestructor)(unsafe.Pointer(pDestructor)).FxDestroy})))(tls, (*TFuncDestructor)(unsafe.Pointer(pDestructor)).FpUserData) _sqlite3DbFree(tls, db, pDestructor) } } } // C documentation // // /* // ** Disconnect all sqlite3_vtab objects that belong to database connection // ** db. This is called when db is being closed. // */ func _disconnectAllVtab(tls *libc.TLS, db uintptr) { var i int32 var p, pMod, pSchema, pTab uintptr _, _, _, _, _ = i, p, pMod, pSchema, pTab _sqlite3BtreeEnterAll(tls, db) i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema if pSchema != 0 { p = (*THash)(unsafe.Pointer(pSchema + 8)).Ffirst for { if !(p != 0) { break } pTab = (*THashElem)(unsafe.Pointer(p)).Fdata if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { _sqlite3VtabDisconnect(tls, db, pTab) } goto _2 _2: ; p = (*THashElem)(unsafe.Pointer(p)).Fnext } } goto _1 _1: ; i++ } p = (*THash)(unsafe.Pointer(db + 404)).Ffirst for { if !(p != 0) { break } pMod = (*THashElem)(unsafe.Pointer(p)).Fdata if (*TModule)(unsafe.Pointer(pMod)).FpEpoTab != 0 { _sqlite3VtabDisconnect(tls, db, (*TModule)(unsafe.Pointer(pMod)).FpEpoTab) } goto _3 _3: ; p = (*THashElem)(unsafe.Pointer(p)).Fnext } _sqlite3VtabUnlockList(tls, db) _sqlite3BtreeLeaveAll(tls, db) } // C documentation // // /* // ** Return TRUE if database connection db has unfinalized prepared // ** statements or unfinished sqlite3_backup objects. // */ func _connectionIsBusy(tls *libc.TLS, db uintptr) (r int32) { var j int32 var pBt uintptr _, _ = j, pBt _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).FpVdbe != 0 { return int32(1) } j = 0 for { if !(j < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(j)*16))).FpBt if pBt != 0 && _sqlite3BtreeIsInBackup(tls, pBt) != 0 { return int32(1) } goto _1 _1: ; j++ } return 0 } // C documentation // // /* // ** Close an existing SQLite database // */ func _sqlite3Close(tls *libc.TLS, db uintptr, forceZombie int32) (r int32) { var p uintptr _ = p if !(db != 0) { /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */ return SQLITE_OK } if !(_sqlite3SafetyCheckSickOrOk(tls, db) != 0) { return _sqlite3MisuseError(tls, int32(179589)) } Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if int32((*Tsqlite3)(unsafe.Pointer(db)).FmTrace)&int32(SQLITE_TRACE_CLOSE) != 0 { (*(*func(*libc.TLS, Tu32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{*(*uintptr)(unsafe.Pointer(&(*Tsqlite3)(unsafe.Pointer(db)).Ftrace))})))(tls, uint32(SQLITE_TRACE_CLOSE), (*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg, db, uintptr(0)) } /* Force xDisconnect calls on all virtual tables */ _disconnectAllVtab(tls, db) /* If a transaction is open, the disconnectAllVtab() call above ** will not have called the xDisconnect() method on any virtual ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback() ** call will do so. We need to do this before the check for active ** SQL statements below, as the v-table implementation may be storing ** some prepared statements internally. */ _sqlite3VtabRollback(tls, db) /* Legacy behavior (sqlite3_close() behavior) is to return ** SQLITE_BUSY if the connection can not be closed immediately. */ if !(forceZombie != 0) && _connectionIsBusy(tls, db) != 0 { _sqlite3ErrorWithMsg(tls, db, int32(SQLITE_BUSY), __ccgo_ts+23769, 0) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return int32(SQLITE_BUSY) } for (*Tsqlite3)(unsafe.Pointer(db)).FpDbData != 0 { p = (*Tsqlite3)(unsafe.Pointer(db)).FpDbData (*Tsqlite3)(unsafe.Pointer(db)).FpDbData = (*TDbClientData)(unsafe.Pointer(p)).FpNext _ = libc.Int32FromInt32(0) if (*TDbClientData)(unsafe.Pointer(p)).FxDestructor != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TDbClientData)(unsafe.Pointer(p)).FxDestructor})))(tls, (*TDbClientData)(unsafe.Pointer(p)).FpData) } Xsqlite3_free(tls, p) } /* Convert the connection into a zombie and then close it. */ (*Tsqlite3)(unsafe.Pointer(db)).FeOpenState = uint8(SQLITE_STATE_ZOMBIE) _sqlite3LeaveMutexAndCloseZombie(tls, db) return SQLITE_OK } // C documentation // // /* // ** Return the transaction state for a single databse, or the maximum // ** transaction state over all attached databases if zSchema is null. // */ func Xsqlite3_txn_state(tls *libc.TLS, db uintptr, zSchema uintptr) (r int32) { var iDb, iTxn, nDb, x, v1, v3 int32 var pBt uintptr _, _, _, _, _, _, _ = iDb, iTxn, nDb, pBt, x, v1, v3 iTxn = -int32(1) Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if zSchema != 0 { v1 = _sqlite3FindDbName(tls, db, zSchema) iDb = v1 nDb = v1 if iDb < 0 { nDb-- } } else { iDb = 0 nDb = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1) } for { if !(iDb <= nDb) { break } pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt if pBt != uintptr(0) { v3 = _sqlite3BtreeTxnState(tls, pBt) } else { v3 = SQLITE_TXN_NONE } x = v3 if x > iTxn { iTxn = x } goto _2 _2: ; iDb++ } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return iTxn } // C documentation // // /* // ** Two variations on the public interface for closing a database // ** connection. The sqlite3_close() version returns SQLITE_BUSY and // ** leaves the connection open if there are unfinalized prepared // ** statements or unfinished sqlite3_backups. The sqlite3_close_v2() // ** version forces the connection to become a zombie if there are // ** unclosed resources, and arranges for deallocation when the last // ** prepare statement or sqlite3_backup closes. // */ func Xsqlite3_close(tls *libc.TLS, db uintptr) (r int32) { return _sqlite3Close(tls, db, 0) } func Xsqlite3_close_v2(tls *libc.TLS, db uintptr) (r int32) { return _sqlite3Close(tls, db, int32(1)) } // C documentation // // /* // ** Close the mutex on database connection db. // ** // ** Furthermore, if database connection db is a zombie (meaning that there // ** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and // ** every sqlite3_stmt has now been finalized and every sqlite3_backup has // ** finished, then free all resources. // */ func _sqlite3LeaveMutexAndCloseZombie(tls *libc.TLS, db uintptr) { var i, p, pColl, pDb, pMod, pNext uintptr var j int32 _, _, _, _, _, _, _ = i, j, p, pColl, pDb, pMod, pNext /* If there are outstanding sqlite3_stmt or sqlite3_backup objects ** or if the connection has not yet been closed by sqlite3_close_v2(), ** then just leave the mutex and return. */ if int32((*Tsqlite3)(unsafe.Pointer(db)).FeOpenState) != int32(SQLITE_STATE_ZOMBIE) || _connectionIsBusy(tls, db) != 0 { Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return } /* If we reach this point, it means that the database connection has ** closed all sqlite3_stmt and sqlite3_backup objects and has been ** passed to sqlite3_close (meaning that it is a zombie). Therefore, ** go ahead and free all resources. */ /* If a transaction is open, roll it back. This also ensures that if ** any database schemas have been modified by an uncommitted transaction ** they are reset. And that the required b-tree mutex is held to make ** the pager rollback and schema reset an atomic operation. */ _sqlite3RollbackAll(tls, db, SQLITE_OK) /* Free any outstanding Savepoint structures. */ _sqlite3CloseSavepoints(tls, db) /* Close all database connections */ j = 0 for { if !(j < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(j)*16 if (*TDb1)(unsafe.Pointer(pDb)).FpBt != 0 { _sqlite3BtreeClose(tls, (*TDb1)(unsafe.Pointer(pDb)).FpBt) (*TDb1)(unsafe.Pointer(pDb)).FpBt = uintptr(0) if j != int32(1) { (*TDb1)(unsafe.Pointer(pDb)).FpSchema = uintptr(0) } } goto _1 _1: ; j++ } /* Clear the TEMP schema separately and last */ if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema != 0 { _sqlite3SchemaClear(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema) } _sqlite3VtabUnlockList(tls, db) /* Free up the array of auxiliary databases */ _sqlite3CollapseDatabaseArray(tls, db) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Tell the code in notify.c that the connection no longer holds any ** locks and does not require any further unlock-notify callbacks. */ _sqlite3ConnectionClosed(tls, db) i = (*THash)(unsafe.Pointer(db + 432)).Ffirst for { if !(i != 0) { break } p = (*THashElem)(unsafe.Pointer(i)).Fdata for cond := true; cond; cond = p != 0 { _functionDestroy(tls, db, p) pNext = (*TFuncDef)(unsafe.Pointer(p)).FpNext _sqlite3DbFree(tls, db, p) p = pNext } goto _2 _2: ; i = (*THashElem)(unsafe.Pointer(i)).Fnext } _sqlite3HashClear(tls, db+432) i = (*THash)(unsafe.Pointer(db + 448)).Ffirst for { if !(i != 0) { break } pColl = (*THashElem)(unsafe.Pointer(i)).Fdata /* Invoke any destructors registered for collation sequence user data. */ j = 0 for { if !(j < int32(3)) { break } if (*(*TCollSeq)(unsafe.Pointer(pColl + uintptr(j)*20))).FxDel != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*(*TCollSeq)(unsafe.Pointer(pColl + uintptr(j)*20))).FxDel})))(tls, (*(*TCollSeq)(unsafe.Pointer(pColl + uintptr(j)*20))).FpUser) } goto _4 _4: ; j++ } _sqlite3DbFree(tls, db, pColl) goto _3 _3: ; i = (*THashElem)(unsafe.Pointer(i)).Fnext } _sqlite3HashClear(tls, db+448) i = (*THash)(unsafe.Pointer(db + 404)).Ffirst for { if !(i != 0) { break } pMod = (*THashElem)(unsafe.Pointer(i)).Fdata _sqlite3VtabEponymousTableClear(tls, db, pMod) _sqlite3VtabModuleUnref(tls, db, pMod) goto _5 _5: ; i = (*THashElem)(unsafe.Pointer(i)).Fnext } _sqlite3HashClear(tls, db+404) _sqlite3Error(tls, db, SQLITE_OK) /* Deallocates any cached error strings. */ _sqlite3ValueFree(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpErr) _sqlite3CloseExtensions(tls, db) (*Tsqlite3)(unsafe.Pointer(db)).FeOpenState = uint8(SQLITE_STATE_ERROR) /* The temp-database schema is allocated differently from the other schema ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()). ** So it needs to be freed here. Todo: Why not roll the temp schema into ** the same sqliteMalloc() as the one that allocates the database ** structure? */ _sqlite3DbFree(tls, db, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema) if (*Tsqlite3)(unsafe.Pointer(db)).FxAutovacDestr != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxAutovacDestr})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpAutovacPagesArg) } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) (*Tsqlite3)(unsafe.Pointer(db)).FeOpenState = uint8(SQLITE_STATE_CLOSED) Xsqlite3_mutex_free(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbMalloced != 0 { Xsqlite3_free(tls, (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart) } Xsqlite3_free(tls, db) } // C documentation // // /* // ** Rollback all database files. If tripCode is not SQLITE_OK, then // ** any write cursors are invalidated ("tripped" - as in "tripping a circuit // ** breaker") and made to return tripCode if there are any further // ** attempts to use that cursor. Read cursors remain open and valid // ** but are "saved" in case the table pages are moved around. // */ func _sqlite3RollbackAll(tls *libc.TLS, db uintptr, tripCode int32) { var i, inTrans, schemaChange int32 var p uintptr _, _, _, _ = i, inTrans, p, schemaChange inTrans = 0 _ = libc.Int32FromInt32(0) _sqlite3BeginBenignMalloc(tls) /* Obtain all b-tree mutexes before making any calls to BtreeRollback(). ** This is important in case the transaction being rolled back has ** modified the database schema. If the b-tree mutexes are not taken ** here, then another shared-cache connection might sneak in between ** the database rollback and schema reset, which can cause false ** corruption reports in some cases. */ _sqlite3BtreeEnterAll(tls, db) schemaChange = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_SchemaChange) != uint32(0) && int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) == 0) i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } p = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt if p != 0 { if _sqlite3BtreeTxnState(tls, p) == int32(SQLITE_TXN_WRITE) { inTrans = int32(1) } _sqlite3BtreeRollback(tls, p, tripCode, libc.BoolInt32(!(schemaChange != 0))) } goto _1 _1: ; i++ } _sqlite3VtabRollback(tls, db) _sqlite3EndBenignMalloc(tls) if schemaChange != 0 { _sqlite3ExpirePreparedStatements(tls, db, 0) _sqlite3ResetAllSchemasOfConnection(tls, db) } _sqlite3BtreeLeaveAll(tls, db) /* Any deferred constraint violations have now been resolved. */ (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons = 0 (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons = 0 *(*Tu64)(unsafe.Pointer(db + 32)) &= ^(libc.Uint64FromInt32(SQLITE_DeferFKs) | uint64(libc.Int32FromInt32(0x00002))<= 0 && rc < int32(libc.Uint32FromInt64(116)/libc.Uint32FromInt64(4)) && _aMsg[rc] != uintptr(0) { zErr = _aMsg[rc] } break } return zErr } var _aMsg = [29]uintptr{ 0: __ccgo_ts + 23837, 1: __ccgo_ts + 23850, 3: __ccgo_ts + 23866, 4: __ccgo_ts + 23891, 5: __ccgo_ts + 23905, 6: __ccgo_ts + 23924, 7: __ccgo_ts + 1618, 8: __ccgo_ts + 23949, 9: __ccgo_ts + 23986, 10: __ccgo_ts + 23998, 11: __ccgo_ts + 24013, 12: __ccgo_ts + 24046, 13: __ccgo_ts + 24064, 14: __ccgo_ts + 24089, 15: __ccgo_ts + 24118, 17: __ccgo_ts + 6021, 18: __ccgo_ts + 5423, 19: __ccgo_ts + 24135, 20: __ccgo_ts + 24153, 21: __ccgo_ts + 24171, 23: __ccgo_ts + 24205, 25: __ccgo_ts + 24226, 26: __ccgo_ts + 24252, 27: __ccgo_ts + 24275, 28: __ccgo_ts + 24296, } // C documentation // // /* // ** This routine implements a busy callback that sleeps and tries // ** again until a timeout value is reached. The timeout value is // ** an integer number of milliseconds passed in as the first // ** argument. // ** // ** Return non-zero to retry the lock. Return zero to stop trying // ** and cause SQLite to return SQLITE_BUSY. // */ func _sqliteDefaultBusyCallback(tls *libc.TLS, ptr uintptr, count int32) (r int32) { var db uintptr var delay, prior, tmout int32 _, _, _, _ = db, delay, prior, tmout db = ptr tmout = (*Tsqlite3)(unsafe.Pointer(db)).FbusyTimeout _ = libc.Int32FromInt32(0) if count < int32(libc.Uint32FromInt64(12)/libc.Uint32FromInt64(1)) { delay = int32(_delays[count]) prior = int32(_totals[count]) } else { delay = int32(_delays[int32(libc.Uint32FromInt64(12)/libc.Uint32FromInt64(1))-libc.Int32FromInt32(1)]) prior = int32(_totals[int32(libc.Uint32FromInt64(12)/libc.Uint32FromInt64(1))-libc.Int32FromInt32(1)]) + delay*(count-(int32(libc.Uint32FromInt64(12)/libc.Uint32FromInt64(1))-libc.Int32FromInt32(1))) } if prior+delay > tmout { delay = tmout - prior if delay <= 0 { return 0 } } _sqlite3OsSleep(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, delay*int32(1000)) return int32(1) } /* This case is for systems that have support for sleeping for fractions of ** a second. Examples: All windows systems, unix systems with nanosleep() */ var _delays = [12]Tu8{ 0: uint8(1), 1: uint8(2), 2: uint8(5), 3: uint8(10), 4: uint8(15), 5: uint8(20), 6: uint8(25), 7: uint8(25), 8: uint8(25), 9: uint8(50), 10: uint8(50), 11: uint8(100), } var _totals = [12]Tu8{ 1: uint8(1), 2: uint8(3), 3: uint8(8), 4: uint8(18), 5: uint8(33), 6: uint8(53), 7: uint8(78), 8: uint8(103), 9: uint8(128), 10: uint8(178), 11: uint8(228), } // C documentation // // /* // ** Invoke the given busy handler. // ** // ** This routine is called when an operation failed to acquire a // ** lock on VFS file pFile. // ** // ** If this routine returns non-zero, the lock is retried. If it // ** returns 0, the operation aborts with an SQLITE_BUSY error. // */ func _sqlite3InvokeBusyHandler(tls *libc.TLS, p uintptr) (r int32) { var rc int32 _ = rc if (*TBusyHandler)(unsafe.Pointer(p)).FxBusyHandler == uintptr(0) || (*TBusyHandler)(unsafe.Pointer(p)).FnBusy < 0 { return 0 } rc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*TBusyHandler)(unsafe.Pointer(p)).FxBusyHandler})))(tls, (*TBusyHandler)(unsafe.Pointer(p)).FpBusyArg, (*TBusyHandler)(unsafe.Pointer(p)).FnBusy) if rc == 0 { (*TBusyHandler)(unsafe.Pointer(p)).FnBusy = -int32(1) } else { (*TBusyHandler)(unsafe.Pointer(p)).FnBusy++ } return rc } // C documentation // // /* // ** This routine sets the busy callback for an Sqlite database to the // ** given callback function with the given argument. // */ func Xsqlite3_busy_handler(tls *libc.TLS, db uintptr, xBusy uintptr, pArg uintptr) (r int32) { Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) (*Tsqlite3)(unsafe.Pointer(db)).FbusyHandler.FxBusyHandler = xBusy (*Tsqlite3)(unsafe.Pointer(db)).FbusyHandler.FpBusyArg = pArg (*Tsqlite3)(unsafe.Pointer(db)).FbusyHandler.FnBusy = 0 (*Tsqlite3)(unsafe.Pointer(db)).FbusyTimeout = 0 Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return SQLITE_OK } // C documentation // // /* // ** This routine sets the progress callback for an Sqlite database to the // ** given callback function with the given argument. The progress callback will // ** be invoked every nOps opcodes. // */ func Xsqlite3_progress_handler(tls *libc.TLS, db uintptr, nOps int32, xProgress uintptr, pArg uintptr) { Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if nOps > 0 { (*Tsqlite3)(unsafe.Pointer(db)).FxProgress = xProgress (*Tsqlite3)(unsafe.Pointer(db)).FnProgressOps = uint32(nOps) (*Tsqlite3)(unsafe.Pointer(db)).FpProgressArg = pArg } else { (*Tsqlite3)(unsafe.Pointer(db)).FxProgress = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).FnProgressOps = uint32(0) (*Tsqlite3)(unsafe.Pointer(db)).FpProgressArg = uintptr(0) } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) } // C documentation // // /* // ** This routine installs a default busy handler that waits for the // ** specified number of milliseconds before returning 0. // */ func Xsqlite3_busy_timeout(tls *libc.TLS, db uintptr, ms int32) (r int32) { if ms > 0 { Xsqlite3_busy_handler(tls, db, __ccgo_fp(_sqliteDefaultBusyCallback), db) (*Tsqlite3)(unsafe.Pointer(db)).FbusyTimeout = ms } else { Xsqlite3_busy_handler(tls, db, uintptr(0), uintptr(0)) } return SQLITE_OK } // C documentation // // /* // ** Cause any pending operation to stop at its earliest opportunity. // */ func Xsqlite3_interrupt(tls *libc.TLS, db uintptr) { libc.AtomicStoreNInt32(db+312, libc.Int32FromInt32(1), libc.Int32FromInt32(__ATOMIC_RELAXED)) } // C documentation // // /* // ** Return true or false depending on whether or not an interrupt is // ** pending on connection db. // */ func Xsqlite3_is_interrupted(tls *libc.TLS, db uintptr) (r int32) { return libc.BoolInt32(libc.AtomicLoadNInt32(db+312, libc.Int32FromInt32(__ATOMIC_RELAXED)) != 0) } // C documentation // // /* // ** This function is exactly the same as sqlite3_create_function(), except // ** that it is designed to be called by internal code. The difference is // ** that if a malloc() fails in sqlite3_create_function(), an error code // ** is returned and the mallocFailed flag cleared. // */ func _sqlite3CreateFunc(tls *libc.TLS, db uintptr, zFunctionName uintptr, nArg int32, enc int32, pUserData uintptr, xSFunc uintptr, xStep uintptr, xFinal uintptr, xValue uintptr, xInverse uintptr, pDestructor uintptr) (r int32) { var extraFlags, rc int32 var p, v1 uintptr _, _, _, _ = extraFlags, p, rc, v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if zFunctionName == uintptr(0) || xSFunc != uintptr(0) && xFinal != uintptr(0) || libc.BoolInt32(xFinal == uintptr(0)) != libc.BoolInt32(xStep == uintptr(0)) || libc.BoolInt32(xValue == uintptr(0)) != libc.BoolInt32(xInverse == uintptr(0)) || (nArg < -int32(1) || nArg > int32(SQLITE_MAX_FUNCTION_ARG)) || int32(255) < _sqlite3Strlen30(tls, zFunctionName) { return _sqlite3MisuseError(tls, int32(180244)) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) extraFlags = enc & (libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_DIRECTONLY) | libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(SQLITE_INNOCUOUS) | libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)) enc &= libc.Int32FromInt32(SQLITE_FUNC_ENCMASK) | libc.Int32FromInt32(SQLITE_ANY) /* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE. But ** the meaning is inverted. So flip the bit. */ _ = libc.Int32FromInt32(0) extraFlags ^= int32(SQLITE_FUNC_UNSAFE) /* tag-20230109-1 */ /* If SQLITE_UTF16 is specified as the encoding type, transform this ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. ** ** If SQLITE_ANY is specified, add three versions of the function ** to the hash table. */ switch enc { case int32(SQLITE_UTF16): enc = int32(SQLITE_UTF16LE) case int32(SQLITE_ANY): rc = _sqlite3CreateFunc(tls, db, zFunctionName, nArg, int32(SQLITE_UTF8)|extraFlags^int32(SQLITE_FUNC_UNSAFE), pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor) if rc == SQLITE_OK { rc = _sqlite3CreateFunc(tls, db, zFunctionName, nArg, int32(SQLITE_UTF16LE)|extraFlags^int32(SQLITE_FUNC_UNSAFE), pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor) } if rc != SQLITE_OK { return rc } enc = int32(SQLITE_UTF16BE) case int32(SQLITE_UTF8): fallthrough case int32(SQLITE_UTF16LE): fallthrough case int32(SQLITE_UTF16BE): default: enc = int32(SQLITE_UTF8) break } /* Check if an existing function is being overridden or deleted. If so, ** and there are active VMs, then return SQLITE_BUSY. If a function ** is being overridden/deleted but there are no active VMs, allow the ** operation to continue but invalidate all precompiled statements. */ p = _sqlite3FindFunction(tls, db, zFunctionName, nArg, uint8(enc), uint8(0)) if p != 0 && (*TFuncDef)(unsafe.Pointer(p)).FfuncFlags&uint32(SQLITE_FUNC_ENCMASK) == uint32(enc) && int32((*TFuncDef)(unsafe.Pointer(p)).FnArg) == nArg { if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive != 0 { _sqlite3ErrorWithMsg(tls, db, int32(SQLITE_BUSY), __ccgo_ts+24393, 0) _ = libc.Int32FromInt32(0) return int32(SQLITE_BUSY) } else { _sqlite3ExpirePreparedStatements(tls, db, 0) } } else { if xSFunc == uintptr(0) && xFinal == uintptr(0) { /* Trying to delete a function that does not exist. This is a no-op. ** https://sqlite.org/forum/forumpost/726219164b */ return SQLITE_OK } } p = _sqlite3FindFunction(tls, db, zFunctionName, nArg, uint8(enc), uint8(1)) _ = libc.Int32FromInt32(0) if !(p != 0) { return int32(SQLITE_NOMEM) } /* If an older version of the function with a configured destructor is ** being replaced invoke the destructor function here. */ _functionDestroy(tls, db, p) if pDestructor != 0 { (*TFuncDestructor)(unsafe.Pointer(pDestructor)).FnRef++ } *(*uintptr)(unsafe.Pointer(p + 36)) = pDestructor (*TFuncDef)(unsafe.Pointer(p)).FfuncFlags = (*TFuncDef)(unsafe.Pointer(p)).FfuncFlags&uint32(SQLITE_FUNC_ENCMASK) | uint32(extraFlags) if xSFunc != 0 { v1 = xSFunc } else { v1 = xStep } (*TFuncDef)(unsafe.Pointer(p)).FxSFunc = v1 (*TFuncDef)(unsafe.Pointer(p)).FxFinalize = xFinal (*TFuncDef)(unsafe.Pointer(p)).FxValue = xValue (*TFuncDef)(unsafe.Pointer(p)).FxInverse = xInverse (*TFuncDef)(unsafe.Pointer(p)).FpUserData = pUserData (*TFuncDef)(unsafe.Pointer(p)).FnArg = int8(uint16(nArg)) return SQLITE_OK } // C documentation // // /* // ** Worker function used by utf-8 APIs that create new functions: // ** // ** sqlite3_create_function() // ** sqlite3_create_function_v2() // ** sqlite3_create_window_function() // */ func _createFunctionApi(tls *libc.TLS, db uintptr, zFunc uintptr, nArg int32, enc int32, p uintptr, xSFunc uintptr, xStep uintptr, xFinal uintptr, xValue uintptr, xInverse uintptr, xDestroy uintptr) (r int32) { var pArg uintptr var rc int32 _, _ = pArg, rc rc = int32(SQLITE_ERROR) pArg = uintptr(0) Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if xDestroy != 0 { pArg = _sqlite3Malloc(tls, uint64(12)) if !(pArg != 0) { _sqlite3OomFault(tls, db) (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{xDestroy})))(tls, p) goto out } (*TFuncDestructor)(unsafe.Pointer(pArg)).FnRef = 0 (*TFuncDestructor)(unsafe.Pointer(pArg)).FxDestroy = xDestroy (*TFuncDestructor)(unsafe.Pointer(pArg)).FpUserData = p } rc = _sqlite3CreateFunc(tls, db, zFunc, nArg, enc, p, xSFunc, xStep, xFinal, xValue, xInverse, pArg) if pArg != 0 && (*TFuncDestructor)(unsafe.Pointer(pArg)).FnRef == 0 { _ = libc.Int32FromInt32(0) (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{xDestroy})))(tls, p) Xsqlite3_free(tls, pArg) } goto out out: ; rc = _sqlite3ApiExit(tls, db, rc) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** Create new user functions. // */ func Xsqlite3_create_function(tls *libc.TLS, db uintptr, zFunc uintptr, nArg int32, enc int32, p uintptr, xSFunc uintptr, xStep uintptr, xFinal uintptr) (r int32) { return _createFunctionApi(tls, db, zFunc, nArg, enc, p, xSFunc, xStep, xFinal, uintptr(0), uintptr(0), uintptr(0)) } func Xsqlite3_create_function_v2(tls *libc.TLS, db uintptr, zFunc uintptr, nArg int32, enc int32, p uintptr, xSFunc uintptr, xStep uintptr, xFinal uintptr, xDestroy uintptr) (r int32) { return _createFunctionApi(tls, db, zFunc, nArg, enc, p, xSFunc, xStep, xFinal, uintptr(0), uintptr(0), xDestroy) } func Xsqlite3_create_window_function(tls *libc.TLS, db uintptr, zFunc uintptr, nArg int32, enc int32, p uintptr, xStep uintptr, xFinal uintptr, xValue uintptr, xInverse uintptr, xDestroy uintptr) (r int32) { return _createFunctionApi(tls, db, zFunc, nArg, enc, p, uintptr(0), xStep, xFinal, xValue, xInverse, xDestroy) } func Xsqlite3_create_function16(tls *libc.TLS, db uintptr, zFunctionName uintptr, nArg int32, eTextRep int32, p uintptr, xSFunc uintptr, xStep uintptr, xFinal uintptr) (r int32) { var rc int32 var zFunc8 uintptr _, _ = rc, zFunc8 Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) _ = libc.Int32FromInt32(0) zFunc8 = _sqlite3Utf16to8(tls, db, zFunctionName, -int32(1), uint8(SQLITE_UTF16LE)) rc = _sqlite3CreateFunc(tls, db, zFunc8, nArg, eTextRep, p, xSFunc, xStep, xFinal, uintptr(0), uintptr(0), uintptr(0)) _sqlite3DbFree(tls, db, zFunc8) rc = _sqlite3ApiExit(tls, db, rc) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** The following is the implementation of an SQL function that always // ** fails with an error message stating that the function is used in the // ** wrong context. The sqlite3_overload_function() API might construct // ** SQL function that use this routine so that the functions will exist // ** for name resolution but are actually overloaded by the xFindFunction // ** method of virtual tables. // */ func _sqlite3InvalidFunction(tls *libc.TLS, context uintptr, NotUsed int32, NotUsed2 uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var zErr, zName uintptr _, _ = zErr, zName zName = Xsqlite3_user_data(tls, context) _ = NotUsed _ = NotUsed2 zErr = Xsqlite3_mprintf(tls, __ccgo_ts+24456, libc.VaList(bp+8, zName)) Xsqlite3_result_error(tls, context, zErr, -int32(1)) Xsqlite3_free(tls, zErr) } // C documentation // // /* // ** Declare that a function has been overloaded by a virtual table. // ** // ** If the function already exists as a regular global function, then // ** this routine is a no-op. If the function does not exist, then create // ** a new one that always throws a run-time error. // ** // ** When virtual tables intend to provide an overloaded function, they // ** should call this routine to make sure the global function exists. // ** A global function must exist in order for name resolution to work // ** properly. // */ func Xsqlite3_overload_function(tls *libc.TLS, db uintptr, zName uintptr, nArg int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var zCopy uintptr _, _ = rc, zCopy Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) rc = libc.BoolInt32(_sqlite3FindFunction(tls, db, zName, nArg, uint8(SQLITE_UTF8), uint8(0)) != uintptr(0)) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if rc != 0 { return SQLITE_OK } zCopy = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+8, zName)) if zCopy == uintptr(0) { return int32(SQLITE_NOMEM) } return Xsqlite3_create_function_v2(tls, db, zName, nArg, int32(SQLITE_UTF8), zCopy, __ccgo_fp(_sqlite3InvalidFunction), uintptr(0), uintptr(0), __ccgo_fp(Xsqlite3_free)) } // C documentation // // /* // ** Register a trace function. The pArg from the previously registered trace // ** is returned. // ** // ** A NULL trace function means that no tracing is executes. A non-NULL // ** trace is a pointer to a function that is invoked at the start of each // ** SQL statement. // */ func Xsqlite3_trace(tls *libc.TLS, db uintptr, xTrace uintptr, pArg uintptr) (r uintptr) { var pOld uintptr var v1 int32 _, _ = pOld, v1 Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) pOld = (*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg if xTrace != 0 { v1 = int32(SQLITE_TRACE_LEGACY) } else { v1 = 0 } (*Tsqlite3)(unsafe.Pointer(db)).FmTrace = uint8(v1) *(*uintptr)(unsafe.Pointer(db + 216)) = xTrace (*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg = pArg Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return pOld } // C documentation // // /* Register a trace callback using the version-2 interface. // */ func Xsqlite3_trace_v2(tls *libc.TLS, db uintptr, mTrace uint32, xTrace uintptr, pArg uintptr) (r int32) { Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if mTrace == uint32(0) { xTrace = uintptr(0) } if xTrace == uintptr(0) { mTrace = uint32(0) } (*Tsqlite3)(unsafe.Pointer(db)).FmTrace = uint8(mTrace) *(*uintptr)(unsafe.Pointer(db + 216)) = xTrace (*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg = pArg Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return SQLITE_OK } // C documentation // // /* // ** Register a profile function. The pArg from the previously registered // ** profile function is returned. // ** // ** A NULL profile function means that no profiling is executes. A non-NULL // ** profile is a pointer to a function that is invoked at the conclusion of // ** each SQL statement that is run. // */ func Xsqlite3_profile(tls *libc.TLS, db uintptr, xProfile uintptr, pArg uintptr) (r uintptr) { var pOld, p1, p2 uintptr _, _, _ = pOld, p1, p2 Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) pOld = (*Tsqlite3)(unsafe.Pointer(db)).FpProfileArg (*Tsqlite3)(unsafe.Pointer(db)).FxProfile = xProfile (*Tsqlite3)(unsafe.Pointer(db)).FpProfileArg = pArg p1 = db + 94 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) & libc.Int32FromInt32(SQLITE_TRACE_NONLEGACY_MASK)) if (*Tsqlite3)(unsafe.Pointer(db)).FxProfile != 0 { p2 = db + 94 *(*Tu8)(unsafe.Pointer(p2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p2))) | libc.Int32FromInt32(SQLITE_TRACE_XPROFILE)) } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return pOld } // C documentation // // /* // ** Register a function to be invoked when a transaction commits. // ** If the invoked function returns non-zero, then the commit becomes a // ** rollback. // */ func Xsqlite3_commit_hook(tls *libc.TLS, db uintptr, xCallback uintptr, pArg uintptr) (r uintptr) { var pOld uintptr _ = pOld Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) pOld = (*Tsqlite3)(unsafe.Pointer(db)).FpCommitArg (*Tsqlite3)(unsafe.Pointer(db)).FxCommitCallback = xCallback (*Tsqlite3)(unsafe.Pointer(db)).FpCommitArg = pArg Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return pOld } // C documentation // // /* // ** Register a callback to be invoked each time a row is updated, // ** inserted or deleted using this database connection. // */ func Xsqlite3_update_hook(tls *libc.TLS, db uintptr, xCallback uintptr, pArg uintptr) (r uintptr) { var pRet uintptr _ = pRet Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) pRet = (*Tsqlite3)(unsafe.Pointer(db)).FpUpdateArg (*Tsqlite3)(unsafe.Pointer(db)).FxUpdateCallback = xCallback (*Tsqlite3)(unsafe.Pointer(db)).FpUpdateArg = pArg Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return pRet } // C documentation // // /* // ** Register a callback to be invoked each time a transaction is rolled // ** back by this database connection. // */ func Xsqlite3_rollback_hook(tls *libc.TLS, db uintptr, xCallback uintptr, pArg uintptr) (r uintptr) { var pRet uintptr _ = pRet Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) pRet = (*Tsqlite3)(unsafe.Pointer(db)).FpRollbackArg (*Tsqlite3)(unsafe.Pointer(db)).FxRollbackCallback = xCallback (*Tsqlite3)(unsafe.Pointer(db)).FpRollbackArg = pArg Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return pRet } // C documentation // // /* // ** Register a callback to be invoked each time a row is updated, // ** inserted or deleted using this database connection. // */ func Xsqlite3_preupdate_hook(tls *libc.TLS, db uintptr, xCallback uintptr, pArg uintptr) (r uintptr) { var pRet uintptr _ = pRet Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) pRet = (*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdateArg (*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback = xCallback (*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdateArg = pArg Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return pRet } // C documentation // // /* // ** Register a function to be invoked prior to each autovacuum that // ** determines the number of pages to vacuum. // */ func Xsqlite3_autovacuum_pages(tls *libc.TLS, db uintptr, xCallback uintptr, pArg uintptr, xDestructor uintptr) (r int32) { Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if (*Tsqlite3)(unsafe.Pointer(db)).FxAutovacDestr != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxAutovacDestr})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpAutovacPagesArg) } (*Tsqlite3)(unsafe.Pointer(db)).FxAutovacPages = xCallback (*Tsqlite3)(unsafe.Pointer(db)).FpAutovacPagesArg = pArg (*Tsqlite3)(unsafe.Pointer(db)).FxAutovacDestr = xDestructor Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return SQLITE_OK } // C documentation // // /* // ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint(). // ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file // ** is greater than sqlite3.pWalArg cast to an integer (the value configured by // ** wal_autocheckpoint()). // */ func _sqlite3WalDefaultHook(tls *libc.TLS, pClientData uintptr, db uintptr, zDb uintptr, nFrame int32) (r int32) { if nFrame >= int32(pClientData) { _sqlite3BeginBenignMalloc(tls) Xsqlite3_wal_checkpoint(tls, db, zDb) _sqlite3EndBenignMalloc(tls) } return SQLITE_OK } // C documentation // // /* // ** Configure an sqlite3_wal_hook() callback to automatically checkpoint // ** a database after committing a transaction if there are nFrame or // ** more frames in the log file. Passing zero or a negative value as the // ** nFrame parameter disables automatic checkpoints entirely. // ** // ** The callback registered by this function replaces any existing callback // ** registered using sqlite3_wal_hook(). Likewise, registering a callback // ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism // ** configured by this function. // */ func Xsqlite3_wal_autocheckpoint(tls *libc.TLS, db uintptr, nFrame int32) (r int32) { if nFrame > 0 { Xsqlite3_wal_hook(tls, db, __ccgo_fp(_sqlite3WalDefaultHook), uintptr(nFrame)) } else { Xsqlite3_wal_hook(tls, db, uintptr(0), uintptr(0)) } return SQLITE_OK } // C documentation // // /* // ** Register a callback to be invoked each time a transaction is written // ** into the write-ahead-log by this database connection. // */ func Xsqlite3_wal_hook(tls *libc.TLS, db uintptr, xCallback uintptr, pArg uintptr) (r uintptr) { var pRet uintptr _ = pRet Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) pRet = (*Tsqlite3)(unsafe.Pointer(db)).FpWalArg (*Tsqlite3)(unsafe.Pointer(db)).FxWalCallback = xCallback (*Tsqlite3)(unsafe.Pointer(db)).FpWalArg = pArg Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return pRet } // C documentation // // /* // ** Checkpoint database zDb. // */ func Xsqlite3_wal_checkpoint_v2(tls *libc.TLS, db uintptr, zDb uintptr, eMode int32, pnLog uintptr, pnCkpt uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iDb, rc int32 _, _ = iDb, rc /* Schema to checkpoint */ /* Initialize the output variables to -1 in case an error occurs. */ if pnLog != 0 { *(*int32)(unsafe.Pointer(pnLog)) = -int32(1) } if pnCkpt != 0 { *(*int32)(unsafe.Pointer(pnCkpt)) = -int32(1) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if eMode < SQLITE_CHECKPOINT_PASSIVE || eMode > int32(SQLITE_CHECKPOINT_TRUNCATE) { /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint ** mode: */ return _sqlite3MisuseError(tls, int32(180859)) } Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if zDb != 0 && *(*uint8)(unsafe.Pointer(zDb)) != 0 { iDb = _sqlite3FindDbName(tls, db, zDb) } else { iDb = libc.Int32FromInt32(SQLITE_MAX_ATTACHED) + libc.Int32FromInt32(2) /* This means process all schemas */ } if iDb < 0 { rc = int32(SQLITE_ERROR) _sqlite3ErrorWithMsg(tls, db, int32(SQLITE_ERROR), __ccgo_ts+24507, libc.VaList(bp+8, zDb)) } else { (*Tsqlite3)(unsafe.Pointer(db)).FbusyHandler.FnBusy = 0 rc = _sqlite3Checkpoint(tls, db, iDb, eMode, pnLog, pnCkpt) _sqlite3Error(tls, db, rc) } rc = _sqlite3ApiExit(tls, db, rc) /* If there are no active statements, clear the interrupt flag at this ** point. */ if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive == 0 { libc.AtomicStoreNInt32(db+312, libc.Int32FromInt32(0), libc.Int32FromInt32(__ATOMIC_RELAXED)) } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points // ** to contains a zero-length string, all attached databases are // ** checkpointed. // */ func Xsqlite3_wal_checkpoint(tls *libc.TLS, db uintptr, zDb uintptr) (r int32) { /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */ return Xsqlite3_wal_checkpoint_v2(tls, db, zDb, SQLITE_CHECKPOINT_PASSIVE, uintptr(0), uintptr(0)) } // C documentation // // /* // ** Run a checkpoint on database iDb. This is a no-op if database iDb is // ** not currently open in WAL mode. // ** // ** If a transaction is open on the database being checkpointed, this // ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If // ** an error occurs while running the checkpoint, an SQLite error code is // ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK. // ** // ** The mutex on database handle db should be held by the caller. The mutex // ** associated with the specific b-tree being checkpointed is taken by // ** this function while the checkpoint is running. // ** // ** If iDb is passed SQLITE_MAX_DB then all attached databases are // ** checkpointed. If an error is encountered it is returned immediately - // ** no attempt is made to checkpoint any remaining databases. // ** // ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART // ** or TRUNCATE. // */ func _sqlite3Checkpoint(tls *libc.TLS, db uintptr, iDb int32, eMode int32, pnLog uintptr, pnCkpt uintptr) (r int32) { var bBusy, i, rc, v2 int32 _, _, _, _ = bBusy, i, rc, v2 rc = SQLITE_OK /* Used to iterate through attached dbs */ bBusy = 0 /* True if SQLITE_BUSY has been encountered */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* See forum post a006d86f72 */ i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb && rc == SQLITE_OK) { break } if i == iDb || iDb == libc.Int32FromInt32(SQLITE_MAX_ATTACHED)+libc.Int32FromInt32(2) { rc = _sqlite3BtreeCheckpoint(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt, eMode, pnLog, pnCkpt) pnLog = uintptr(0) pnCkpt = uintptr(0) if rc == int32(SQLITE_BUSY) { bBusy = int32(1) rc = SQLITE_OK } } goto _1 _1: ; i++ } if rc == SQLITE_OK && bBusy != 0 { v2 = int32(SQLITE_BUSY) } else { v2 = rc } return v2 } // C documentation // // /* // ** This function returns true if main-memory should be used instead of // ** a temporary file for transient pager files and statement journals. // ** The value returned depends on the value of db->temp_store (runtime // ** parameter) and the compile time value of SQLITE_TEMP_STORE. The // ** following table describes the relationship between these two values // ** and this functions return value. // ** // ** SQLITE_TEMP_STORE db->temp_store Location of temporary database // ** ----------------- -------------- ------------------------------ // ** 0 any file (return 0) // ** 1 1 file (return 0) // ** 1 2 memory (return 1) // ** 1 0 file (return 0) // ** 2 1 file (return 0) // ** 2 2 memory (return 1) // ** 2 0 memory (return 1) // ** 3 any memory (return 1) // */ func _sqlite3TempInMemory(tls *libc.TLS, db uintptr) (r int32) { return libc.BoolInt32(int32((*Tsqlite3)(unsafe.Pointer(db)).Ftemp_store) == int32(2)) } // C documentation // // /* // ** Return UTF-8 encoded English language explanation of the most recent // ** error. // */ func Xsqlite3_errmsg(tls *libc.TLS, db uintptr) (r uintptr) { var z, v1 uintptr _, _ = z, v1 if !(db != 0) { return _sqlite3ErrStr(tls, int32(SQLITE_NOMEM)) } if !(_sqlite3SafetyCheckSickOrOk(tls, db) != 0) { return _sqlite3ErrStr(tls, _sqlite3MisuseError(tls, int32(180995))) } Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { z = _sqlite3ErrStr(tls, int32(SQLITE_NOMEM)) } else { if (*Tsqlite3)(unsafe.Pointer(db)).FerrCode != 0 { v1 = Xsqlite3_value_text(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpErr) } else { v1 = uintptr(0) } z = v1 _ = libc.Int32FromInt32(0) if z == uintptr(0) { z = _sqlite3ErrStr(tls, (*Tsqlite3)(unsafe.Pointer(db)).FerrCode) } } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return z } // C documentation // // /* // ** Return the byte offset of the most recent error // */ func Xsqlite3_error_offset(tls *libc.TLS, db uintptr) (r int32) { var iOffset int32 _ = iOffset iOffset = -int32(1) if db != 0 && _sqlite3SafetyCheckSickOrOk(tls, db) != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FerrCode != 0 { Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) iOffset = (*Tsqlite3)(unsafe.Pointer(db)).FerrByteOffset Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) } return iOffset } // C documentation // // /* // ** Return UTF-16 encoded English language explanation of the most recent // ** error. // */ func Xsqlite3_errmsg16(tls *libc.TLS, db uintptr) (r uintptr) { var z uintptr _ = z if !(db != 0) { return uintptr(unsafe.Pointer(&_outOfMem)) } if !(_sqlite3SafetyCheckSickOrOk(tls, db) != 0) { return uintptr(unsafe.Pointer(&_misuse)) } Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { z = uintptr(unsafe.Pointer(&_outOfMem)) } else { z = Xsqlite3_value_text16(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpErr) if z == uintptr(0) { _sqlite3ErrorWithMsg(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).FerrCode, _sqlite3ErrStr(tls, (*Tsqlite3)(unsafe.Pointer(db)).FerrCode), 0) z = Xsqlite3_value_text16(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpErr) } /* A malloc() may have failed within the call to sqlite3_value_text16() ** above. If this is the case, then the db->mallocFailed flag needs to ** be cleared before returning. Do this directly, instead of via ** sqlite3ApiExit(), to avoid setting the database handle error message. */ _sqlite3OomClear(tls, db) } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return z } var _outOfMem = [14]Tu16{ 0: uint16('o'), 1: uint16('u'), 2: uint16('t'), 3: uint16(' '), 4: uint16('o'), 5: uint16('f'), 6: uint16(' '), 7: uint16('m'), 8: uint16('e'), 9: uint16('m'), 10: uint16('o'), 11: uint16('r'), 12: uint16('y'), } var _misuse = [34]Tu16{ 0: uint16('b'), 1: uint16('a'), 2: uint16('d'), 3: uint16(' '), 4: uint16('p'), 5: uint16('a'), 6: uint16('r'), 7: uint16('a'), 8: uint16('m'), 9: uint16('e'), 10: uint16('t'), 11: uint16('e'), 12: uint16('r'), 13: uint16(' '), 14: uint16('o'), 15: uint16('r'), 16: uint16(' '), 17: uint16('o'), 18: uint16('t'), 19: uint16('h'), 20: uint16('e'), 21: uint16('r'), 22: uint16(' '), 23: uint16('A'), 24: uint16('P'), 25: uint16('I'), 26: uint16(' '), 27: uint16('m'), 28: uint16('i'), 29: uint16('s'), 30: uint16('u'), 31: uint16('s'), 32: uint16('e'), } // C documentation // // /* // ** Return the most recent error code generated by an SQLite routine. If NULL is // ** passed to this function, we assume a malloc() failed during sqlite3_open(). // */ func Xsqlite3_errcode(tls *libc.TLS, db uintptr) (r int32) { if db != 0 && !(_sqlite3SafetyCheckSickOrOk(tls, db) != 0) { return _sqlite3MisuseError(tls, int32(181074)) } if !(db != 0) || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return int32(SQLITE_NOMEM) } return (*Tsqlite3)(unsafe.Pointer(db)).FerrCode & (*Tsqlite3)(unsafe.Pointer(db)).FerrMask } func Xsqlite3_extended_errcode(tls *libc.TLS, db uintptr) (r int32) { if db != 0 && !(_sqlite3SafetyCheckSickOrOk(tls, db) != 0) { return _sqlite3MisuseError(tls, int32(181083)) } if !(db != 0) || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return int32(SQLITE_NOMEM) } return (*Tsqlite3)(unsafe.Pointer(db)).FerrCode } func Xsqlite3_system_errno(tls *libc.TLS, db uintptr) (r int32) { var v1 int32 _ = v1 if db != 0 { v1 = (*Tsqlite3)(unsafe.Pointer(db)).FiSysErrno } else { v1 = 0 } return v1 } // C documentation // // /* // ** Return a string that describes the kind of error specified in the // ** argument. For now, this simply calls the internal sqlite3ErrStr() // ** function. // */ func Xsqlite3_errstr(tls *libc.TLS, rc int32) (r uintptr) { return _sqlite3ErrStr(tls, rc) } // C documentation // // /* // ** Create a new collating function for database "db". The name is zName // ** and the encoding is enc. // */ func _createCollation(tls *libc.TLS, db uintptr, zName uintptr, enc Tu8, pCtx uintptr, xCompare uintptr, xDel uintptr) (r int32) { var aColl, p, pColl uintptr var enc2, j int32 _, _, _, _, _ = aColl, enc2, j, p, pColl _ = libc.Int32FromInt32(0) /* If SQLITE_UTF16 is specified as the encoding type, transform this ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. */ enc2 = int32(enc) if enc2 == int32(SQLITE_UTF16) || enc2 == int32(SQLITE_UTF16_ALIGNED) { enc2 = int32(SQLITE_UTF16LE) } if enc2 < int32(SQLITE_UTF8) || enc2 > int32(SQLITE_UTF16BE) { return _sqlite3MisuseError(tls, int32(181131)) } /* Check if this call is removing or replacing an existing collation ** sequence. If so, and there are active VMs, return busy. If there ** are no active VMs, invalidate any pre-compiled statements. */ pColl = _sqlite3FindCollSeq(tls, db, uint8(enc2), zName, 0) if pColl != 0 && (*TCollSeq)(unsafe.Pointer(pColl)).FxCmp != 0 { if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive != 0 { _sqlite3ErrorWithMsg(tls, db, int32(SQLITE_BUSY), __ccgo_ts+24528, 0) return int32(SQLITE_BUSY) } _sqlite3ExpirePreparedStatements(tls, db, 0) /* If collation sequence pColl was created directly by a call to ** sqlite3_create_collation, and not generated by synthCollSeq(), ** then any copies made by synthCollSeq() need to be invalidated. ** Also, collation destructor - CollSeq.xDel() - function may need ** to be called. */ if int32((*TCollSeq)(unsafe.Pointer(pColl)).Fenc) & ^libc.Int32FromInt32(SQLITE_UTF16_ALIGNED) == enc2 { aColl = _sqlite3HashFind(tls, db+448, zName) j = 0 for { if !(j < int32(3)) { break } p = aColl + uintptr(j)*20 if int32((*TCollSeq)(unsafe.Pointer(p)).Fenc) == int32((*TCollSeq)(unsafe.Pointer(pColl)).Fenc) { if (*TCollSeq)(unsafe.Pointer(p)).FxDel != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TCollSeq)(unsafe.Pointer(p)).FxDel})))(tls, (*TCollSeq)(unsafe.Pointer(p)).FpUser) } (*TCollSeq)(unsafe.Pointer(p)).FxCmp = uintptr(0) } goto _1 _1: ; j++ } } } pColl = _sqlite3FindCollSeq(tls, db, uint8(enc2), zName, int32(1)) if pColl == uintptr(0) { return int32(SQLITE_NOMEM) } (*TCollSeq)(unsafe.Pointer(pColl)).FxCmp = xCompare (*TCollSeq)(unsafe.Pointer(pColl)).FpUser = pCtx (*TCollSeq)(unsafe.Pointer(pColl)).FxDel = xDel (*TCollSeq)(unsafe.Pointer(pColl)).Fenc = uint8(enc2 | int32(enc)&libc.Int32FromInt32(SQLITE_UTF16_ALIGNED)) _sqlite3Error(tls, db, SQLITE_OK) return SQLITE_OK } // C documentation // // /* // ** This array defines hard upper bounds on limit values. The // ** initializer must be kept in sync with the SQLITE_LIMIT_* // ** #defines in sqlite3.h. // */ var _aHardLimit = [12]int32{ 0: int32(SQLITE_MAX_LENGTH), 1: int32(SQLITE_MAX_SQL_LENGTH), 2: int32(SQLITE_MAX_COLUMN), 3: int32(SQLITE_MAX_EXPR_DEPTH), 4: int32(SQLITE_MAX_COMPOUND_SELECT), 5: int32(SQLITE_MAX_VDBE_OP), 6: int32(SQLITE_MAX_FUNCTION_ARG), 7: int32(SQLITE_MAX_ATTACHED), 8: int32(SQLITE_MAX_LIKE_PATTERN_LENGTH), 9: int32(SQLITE_MAX_VARIABLE_NUMBER), 10: int32(SQLITE_MAX_TRIGGER_DEPTH), 11: int32(SQLITE_MAX_WORKER_THREADS), } /* ** Make sure the hard limits are set to reasonable values */ // C documentation // // /* // ** Change the value of a limit. Report the old value. // ** If an invalid limit index is supplied, report -1. // ** Make no changes but still report the old value if the // ** new limit is negative. // ** // ** A new lower limit does not shrink existing constructs. // ** It merely prevents new constructs that exceed the limit // ** from forming. // */ func Xsqlite3_limit(tls *libc.TLS, db uintptr, limitId int32, newLimit int32) (r int32) { var oldLimit int32 _ = oldLimit /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME ** there is a hard upper bound set at compile-time by a C preprocessor ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to ** "_MAX_".) */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if limitId < 0 || limitId >= libc.Int32FromInt32(SQLITE_LIMIT_WORKER_THREADS)+libc.Int32FromInt32(1) { return -int32(1) } oldLimit = *(*int32)(unsafe.Pointer(db + 120 + uintptr(limitId)*4)) if newLimit >= 0 { /* IMP: R-52476-28732 */ if newLimit > _aHardLimit[limitId] { newLimit = _aHardLimit[limitId] /* IMP: R-51463-25634 */ } else { if newLimit < int32(1) && limitId == SQLITE_LIMIT_LENGTH { newLimit = int32(1) } } *(*int32)(unsafe.Pointer(db + 120 + uintptr(limitId)*4)) = newLimit } return oldLimit /* IMP: R-53341-35419 */ } // C documentation // // /* // ** This function is used to parse both URIs and non-URI filenames passed by the // ** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database // ** URIs specified as part of ATTACH statements. // ** // ** The first argument to this function is the name of the VFS to use (or // ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx" // ** query parameter. The second argument contains the URI (or non-URI filename) // ** itself. When this function is called the *pFlags variable should contain // ** the default flags to open the database handle with. The value stored in // ** *pFlags may be updated before returning if the URI filename contains // ** "cache=xxx" or "mode=xxx" query parameters. // ** // ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to // ** the VFS that should be used to open the database file. *pzFile is set to // ** point to a buffer containing the name of the file to open. The value // ** stored in *pzFile is a database name acceptable to sqlite3_uri_parameter() // ** and is in the same format as names created using sqlite3_create_filename(). // ** The caller must invoke sqlite3_free_filename() (not sqlite3_free()!) on // ** the value returned in *pzFile to avoid a memory leak. // ** // ** If an error occurs, then an SQLite error code is returned and *pzErrMsg // ** may be set to point to a buffer containing an English language error // ** message. It is the responsibility of the caller to eventually release // ** this buffer by calling sqlite3_free(). // */ func _sqlite3ParseUri(tls *libc.TLS, zDefaultVfs uintptr, zUri uintptr, pFlags uintptr, ppVfs uintptr, pzFile uintptr, pzErrMsg uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var aMode, z, zFile, zModeType, zOpt, zVal, zVfs uintptr var c, v2, v5 uint8 var eState, i, iIn, iOut, limit, mask, mode, nOpt, nUri, nVal, octet, rc, v3, v4, v6, v7, v8 int32 var flags uint32 var nByte Tu64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aMode, c, eState, flags, i, iIn, iOut, limit, mask, mode, nByte, nOpt, nUri, nVal, octet, rc, z, zFile, zModeType, zOpt, zVal, zVfs, v2, v3, v4, v5, v6, v7, v8 rc = SQLITE_OK flags = *(*uint32)(unsafe.Pointer(pFlags)) zVfs = zDefaultVfs nUri = _sqlite3Strlen30(tls, zUri) _ = libc.Int32FromInt32(0) if (flags&uint32(SQLITE_OPEN_URI) != 0 || libc.AtomicLoadNUint8(uintptr(unsafe.Pointer(&_sqlite3Config))+6, libc.Int32FromInt32(__ATOMIC_RELAXED)) != 0) && nUri >= int32(5) && libc.Xmemcmp(tls, zUri, __ccgo_ts+24596, uint32(5)) == 0 { /* Input character index */ iOut = 0 /* Output character index */ nByte = uint64(nUri + int32(8)) /* Bytes of space to allocate */ /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen ** method that there may be extra parameters following the file-name. */ flags |= uint32(SQLITE_OPEN_URI) iIn = 0 for { if !(iIn < nUri) { break } nByte += libc.BoolUint64(int32(*(*uint8)(unsafe.Pointer(zUri + uintptr(iIn)))) == libc.Int32FromUint8('&')) goto _1 _1: ; iIn++ } zFile = Xsqlite3_malloc64(tls, nByte) if !(zFile != 0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, zFile, 0, uint32(4)) /* 4-byte of 0x00 is the start of DB name marker */ zFile += uintptr(4) iIn = int32(5) /* Discard the scheme and authority segments of the URI. */ if int32(*(*uint8)(unsafe.Pointer(zUri + 5))) == int32('/') && int32(*(*uint8)(unsafe.Pointer(zUri + 6))) == int32('/') { iIn = int32(7) for *(*uint8)(unsafe.Pointer(zUri + uintptr(iIn))) != 0 && int32(*(*uint8)(unsafe.Pointer(zUri + uintptr(iIn)))) != int32('/') { iIn++ } if iIn != int32(7) && (iIn != int32(16) || libc.Xmemcmp(tls, __ccgo_ts+24602, zUri+7, uint32(9)) != 0) { *(*uintptr)(unsafe.Pointer(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+24612, libc.VaList(bp+8, iIn-int32(7), zUri+7)) rc = int32(SQLITE_ERROR) goto parse_uri_out } } /* Copy the filename and any query parameters into the zFile buffer. ** Decode %HH escape codes along the way. ** ** Within this loop, variable eState may be set to 0, 1 or 2, depending ** on the parsing context. As follows: ** ** 0: Parsing file-name. ** 1: Parsing name section of a name=value query parameter. ** 2: Parsing value section of a name=value query parameter. */ eState = 0 for { v2 = *(*uint8)(unsafe.Pointer(zUri + uintptr(iIn))) c = v2 if !(int32(v2) != 0 && int32(c) != int32('#')) { break } iIn++ if int32(c) == int32('%') && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zUri + uintptr(iIn)))])&int32(0x08) != 0 && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zUri + uintptr(iIn+int32(1))))])&int32(0x08) != 0 { v3 = iIn iIn++ octet = int32(_sqlite3HexToInt(tls, int32(*(*uint8)(unsafe.Pointer(zUri + uintptr(v3)))))) << int32(4) v4 = iIn iIn++ octet += int32(_sqlite3HexToInt(tls, int32(*(*uint8)(unsafe.Pointer(zUri + uintptr(v4)))))) _ = libc.Int32FromInt32(0) if octet == 0 { /* This branch is taken when "%00" appears within the URI. In this ** case we ignore all text in the remainder of the path, name or ** value currently being parsed. So ignore the current character ** and skip to the next "?", "=" or "&", as appropriate. */ for { v5 = *(*uint8)(unsafe.Pointer(zUri + uintptr(iIn))) c = v5 if !(int32(v5) != 0 && int32(c) != int32('#') && (eState != 0 || int32(c) != int32('?')) && (eState != int32(1) || int32(c) != int32('=') && int32(c) != int32('&')) && (eState != int32(2) || int32(c) != int32('&'))) { break } iIn++ } continue } c = uint8(octet) } else { if eState == int32(1) && (int32(c) == int32('&') || int32(c) == int32('=')) { if int32(*(*uint8)(unsafe.Pointer(zFile + uintptr(iOut-int32(1))))) == 0 { /* An empty option name. Ignore this option altogether. */ for *(*uint8)(unsafe.Pointer(zUri + uintptr(iIn))) != 0 && int32(*(*uint8)(unsafe.Pointer(zUri + uintptr(iIn)))) != int32('#') && int32(*(*uint8)(unsafe.Pointer(zUri + uintptr(iIn-int32(1))))) != int32('&') { iIn++ } continue } if int32(c) == int32('&') { v6 = iOut iOut++ *(*uint8)(unsafe.Pointer(zFile + uintptr(v6))) = uint8('\000') } else { eState = int32(2) } c = uint8(0) } else { if eState == 0 && int32(c) == int32('?') || eState == int32(2) && int32(c) == int32('&') { c = uint8(0) eState = int32(1) } } } v7 = iOut iOut++ *(*uint8)(unsafe.Pointer(zFile + uintptr(v7))) = c } if eState == int32(1) { v8 = iOut iOut++ *(*uint8)(unsafe.Pointer(zFile + uintptr(v8))) = uint8('\000') } libc.Xmemset(tls, zFile+uintptr(iOut), 0, uint32(4)) /* end-of-options + empty journal filenames */ /* Check if there were any options specified that should be interpreted ** here. Options that are interpreted here include "vfs" and those that ** correspond to flags that may be passed to the sqlite3_open_v2() ** method. */ zOpt = zFile + uintptr(_sqlite3Strlen30(tls, zFile)+int32(1)) for *(*uint8)(unsafe.Pointer(zOpt)) != 0 { nOpt = _sqlite3Strlen30(tls, zOpt) zVal = zOpt + uintptr(nOpt+int32(1)) nVal = _sqlite3Strlen30(tls, zVal) if nOpt == int32(3) && libc.Xmemcmp(tls, __ccgo_ts+24640, zOpt, uint32(3)) == 0 { zVfs = zVal } else { aMode = uintptr(0) zModeType = uintptr(0) mask = 0 limit = 0 if nOpt == int32(5) && libc.Xmemcmp(tls, __ccgo_ts+24644, zOpt, uint32(5)) == 0 { mask = libc.Int32FromInt32(SQLITE_OPEN_SHAREDCACHE) | libc.Int32FromInt32(SQLITE_OPEN_PRIVATECACHE) aMode = uintptr(unsafe.Pointer(&_aCacheMode)) limit = mask zModeType = __ccgo_ts + 24644 } if nOpt == int32(4) && libc.Xmemcmp(tls, __ccgo_ts+24665, zOpt, uint32(4)) == 0 { mask = libc.Int32FromInt32(SQLITE_OPEN_READONLY) | libc.Int32FromInt32(SQLITE_OPEN_READWRITE) | libc.Int32FromInt32(SQLITE_OPEN_CREATE) | libc.Int32FromInt32(SQLITE_OPEN_MEMORY) aMode = uintptr(unsafe.Pointer(&_aOpenMode)) limit = int32(uint32(mask) & flags) zModeType = __ccgo_ts + 3404 } if aMode != 0 { mode = 0 i = 0 for { if !((*(*struct { Fz uintptr Fmode int32 })(unsafe.Pointer(aMode + uintptr(i)*8))).Fz != 0) { break } z = (*(*struct { Fz uintptr Fmode int32 })(unsafe.Pointer(aMode + uintptr(i)*8))).Fz if nVal == _sqlite3Strlen30(tls, z) && 0 == libc.Xmemcmp(tls, zVal, z, uint32(nVal)) { mode = (*(*struct { Fz uintptr Fmode int32 })(unsafe.Pointer(aMode + uintptr(i)*8))).Fmode break } goto _9 _9: ; i++ } if mode == 0 { *(*uintptr)(unsafe.Pointer(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+24680, libc.VaList(bp+8, zModeType, zVal)) rc = int32(SQLITE_ERROR) goto parse_uri_out } if mode & ^libc.Int32FromInt32(SQLITE_OPEN_MEMORY) > limit { *(*uintptr)(unsafe.Pointer(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+24700, libc.VaList(bp+8, zModeType, zVal)) rc = int32(SQLITE_PERM) goto parse_uri_out } flags = flags&uint32(^mask) | uint32(mode) } } zOpt = zVal + uintptr(nVal+int32(1)) } } else { zFile = Xsqlite3_malloc64(tls, uint64(nUri+int32(8))) if !(zFile != 0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, zFile, 0, uint32(4)) zFile += uintptr(4) if nUri != 0 { libc.Xmemcpy(tls, zFile, zUri, uint32(nUri)) } libc.Xmemset(tls, zFile+uintptr(nUri), 0, uint32(4)) flags &= uint32(^libc.Int32FromInt32(SQLITE_OPEN_URI)) } *(*uintptr)(unsafe.Pointer(ppVfs)) = Xsqlite3_vfs_find(tls, zVfs) if *(*uintptr)(unsafe.Pointer(ppVfs)) == uintptr(0) { *(*uintptr)(unsafe.Pointer(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+24724, libc.VaList(bp+8, zVfs)) rc = int32(SQLITE_ERROR) } goto parse_uri_out parse_uri_out: ; if rc != SQLITE_OK { Xsqlite3_free_filename(tls, zFile) zFile = uintptr(0) } *(*uint32)(unsafe.Pointer(pFlags)) = flags *(*uintptr)(unsafe.Pointer(pzFile)) = zFile return rc } var _aCacheMode = [3]struct { Fz uintptr Fmode int32 }{ 0: { Fz: __ccgo_ts + 24650, Fmode: int32(SQLITE_OPEN_SHAREDCACHE), }, 1: { Fz: __ccgo_ts + 24657, Fmode: int32(SQLITE_OPEN_PRIVATECACHE), }, 2: {}, } var _aOpenMode = [5]struct { Fz uintptr Fmode int32 }{ 0: { Fz: __ccgo_ts + 24670, Fmode: int32(SQLITE_OPEN_READONLY), }, 1: { Fz: __ccgo_ts + 24673, Fmode: int32(SQLITE_OPEN_READWRITE), }, 2: { Fz: __ccgo_ts + 24676, Fmode: libc.Int32FromInt32(SQLITE_OPEN_READWRITE) | libc.Int32FromInt32(SQLITE_OPEN_CREATE), }, 3: { Fz: __ccgo_ts + 17764, Fmode: int32(SQLITE_OPEN_MEMORY), }, 4: {}, } // C documentation // // /* // ** This routine does the core work of extracting URI parameters from a // ** database filename for the sqlite3_uri_parameter() interface. // */ func _uriParameter(tls *libc.TLS, zFilename uintptr, zParam uintptr) (r uintptr) { var x int32 _ = x zFilename += uintptr(_sqlite3Strlen30(tls, zFilename) + int32(1)) for zFilename != uintptr(0) && *(*uint8)(unsafe.Pointer(zFilename)) != 0 { x = libc.Xstrcmp(tls, zFilename, zParam) zFilename += uintptr(_sqlite3Strlen30(tls, zFilename) + int32(1)) if x == 0 { return zFilename } zFilename += uintptr(_sqlite3Strlen30(tls, zFilename) + int32(1)) } return uintptr(0) } // C documentation // // /* // ** This routine does the work of opening a database on behalf of // ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" // ** is UTF-8 encoded. // */ func _openDatabase(tls *libc.TLS, zFilename uintptr, ppDb uintptr, _flags uint32, zVfs uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) *(*uint32)(unsafe.Pointer(bp)) = _flags var db, v2 uintptr var i, isThreadsafe, rc int32 var v1 uint32 var _ /* zErrMsg at bp+8 */ uintptr var _ /* zOpen at bp+4 */ uintptr _, _, _, _, _, _ = db, i, isThreadsafe, rc, v1, v2 /* True for threadsafe connections */ *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) /* Filename argument to pass to BtreeOpen() */ *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) /* Loop counter */ *(*uintptr)(unsafe.Pointer(ppDb)) = uintptr(0) rc = Xsqlite3_initialize(tls) if rc != 0 { return rc } if int32(_sqlite3Config.FbCoreMutex) == 0 { isThreadsafe = 0 } else { if *(*uint32)(unsafe.Pointer(bp))&uint32(SQLITE_OPEN_NOMUTEX) != 0 { isThreadsafe = 0 } else { if *(*uint32)(unsafe.Pointer(bp))&uint32(SQLITE_OPEN_FULLMUTEX) != 0 { isThreadsafe = int32(1) } else { isThreadsafe = int32(_sqlite3Config.FbFullMutex) } } } if *(*uint32)(unsafe.Pointer(bp))&uint32(SQLITE_OPEN_PRIVATECACHE) != 0 { *(*uint32)(unsafe.Pointer(bp)) &= uint32(^libc.Int32FromInt32(SQLITE_OPEN_SHAREDCACHE)) } else { if _sqlite3Config.FsharedCacheEnabled != 0 { *(*uint32)(unsafe.Pointer(bp)) |= uint32(SQLITE_OPEN_SHAREDCACHE) } } /* Remove harmful bits from the flags parameter ** ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were ** dealt with in the previous code block. Besides these, the only ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY, ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE, ** SQLITE_OPEN_PRIVATECACHE, SQLITE_OPEN_EXRESCODE, and some reserved ** bits. Silently mask off all other flags. */ *(*uint32)(unsafe.Pointer(bp)) &= uint32(^(libc.Int32FromInt32(SQLITE_OPEN_DELETEONCLOSE) | libc.Int32FromInt32(SQLITE_OPEN_EXCLUSIVE) | libc.Int32FromInt32(SQLITE_OPEN_MAIN_DB) | libc.Int32FromInt32(SQLITE_OPEN_TEMP_DB) | libc.Int32FromInt32(SQLITE_OPEN_TRANSIENT_DB) | libc.Int32FromInt32(SQLITE_OPEN_MAIN_JOURNAL) | libc.Int32FromInt32(SQLITE_OPEN_TEMP_JOURNAL) | libc.Int32FromInt32(SQLITE_OPEN_SUBJOURNAL) | libc.Int32FromInt32(SQLITE_OPEN_SUPER_JOURNAL) | libc.Int32FromInt32(SQLITE_OPEN_NOMUTEX) | libc.Int32FromInt32(SQLITE_OPEN_FULLMUTEX) | libc.Int32FromInt32(SQLITE_OPEN_WAL))) /* Allocate the sqlite data structure */ db = _sqlite3MallocZero(tls, uint64(576)) if db == uintptr(0) { goto opendb_out } if isThreadsafe != 0 { (*Tsqlite3)(unsafe.Pointer(db)).Fmutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_RECURSIVE)) if (*Tsqlite3)(unsafe.Pointer(db)).Fmutex == uintptr(0) { Xsqlite3_free(tls, db) db = uintptr(0) goto opendb_out } if isThreadsafe == 0 { } } Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if *(*uint32)(unsafe.Pointer(bp))&uint32(SQLITE_OPEN_EXRESCODE) != uint32(0) { v1 = uint32(0xffffffff) } else { v1 = uint32(0xff) } (*Tsqlite3)(unsafe.Pointer(db)).FerrMask = int32(v1) (*Tsqlite3)(unsafe.Pointer(db)).FnDb = int32(2) (*Tsqlite3)(unsafe.Pointer(db)).FeOpenState = uint8(SQLITE_STATE_BUSY) (*Tsqlite3)(unsafe.Pointer(db)).FaDb = db + 476 (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = uint32(1) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0) _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, db+120, uintptr(unsafe.Pointer(&_aHardLimit)), uint32(48)) *(*int32)(unsafe.Pointer(db + 120 + 11*4)) = SQLITE_DEFAULT_WORKER_THREADS (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(1) (*Tsqlite3)(unsafe.Pointer(db)).FnextAutovac = int8(-int32(1)) (*Tsqlite3)(unsafe.Pointer(db)).FszMmap = _sqlite3Config.FszMmap (*Tsqlite3)(unsafe.Pointer(db)).FnextPagesize = 0 (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FazInit = uintptr(unsafe.Pointer(&_sqlite3StdType)) /* Any array of string ptrs will do */ *(*Tu64)(unsafe.Pointer(db + 32)) |= uint64(uint32(libc.Int32FromInt32(SQLITE_ShortColNames)|libc.Int32FromInt32(SQLITE_EnableTrigger)) | libc.Uint32FromUint32(SQLITE_EnableView) | libc.Uint32FromInt32(SQLITE_CacheSpill) | libc.Uint32FromInt32(SQLITE_TrustedSchema) | libc.Uint32FromInt32(SQLITE_DqsDML) | libc.Uint32FromInt32(SQLITE_DqsDDL) | libc.Uint32FromInt32(SQLITE_AutoIndex)) _sqlite3HashInit(tls, db+448) _sqlite3HashInit(tls, db+404) /* Add the default collation sequence BINARY. BINARY works for both UTF-8 ** and UTF-16, so add a version for each to avoid any unnecessary ** conversions. The only error that can occur here is a malloc() failure. ** ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating ** functions: */ _createCollation(tls, db, uintptr(unsafe.Pointer(&_sqlite3StrBINARY)), uint8(SQLITE_UTF8), uintptr(0), __ccgo_fp(_binCollFunc), uintptr(0)) _createCollation(tls, db, uintptr(unsafe.Pointer(&_sqlite3StrBINARY)), uint8(SQLITE_UTF16BE), uintptr(0), __ccgo_fp(_binCollFunc), uintptr(0)) _createCollation(tls, db, uintptr(unsafe.Pointer(&_sqlite3StrBINARY)), uint8(SQLITE_UTF16LE), uintptr(0), __ccgo_fp(_binCollFunc), uintptr(0)) _createCollation(tls, db, __ccgo_ts+22351, uint8(SQLITE_UTF8), uintptr(0), __ccgo_fp(_nocaseCollatingFunc), uintptr(0)) _createCollation(tls, db, __ccgo_ts+24740, uint8(SQLITE_UTF8), uintptr(0), __ccgo_fp(_rtrimCollFunc), uintptr(0)) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto opendb_out } /* Parse the filename/URI argument ** ** Only allow sensible combinations of bits in the flags argument. ** Throw an error if any non-sense combination is used. If we ** do not block illegal combinations here, it could trigger ** assert() statements in deeper layers. Sensible combinations ** are: ** ** 1: SQLITE_OPEN_READONLY ** 2: SQLITE_OPEN_READWRITE ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE */ (*Tsqlite3)(unsafe.Pointer(db)).FopenFlags = *(*uint32)(unsafe.Pointer(bp)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* READONLY */ /* READWRITE */ /* READWRITE | CREATE */ if int32(1)<<(*(*uint32)(unsafe.Pointer(bp))&uint32(7))&int32(0x46) == 0 { rc = _sqlite3MisuseError(tls, int32(181803)) /* IMP: R-18321-05872 */ } else { rc = _sqlite3ParseUri(tls, zVfs, zFilename, bp, db, bp+4, bp+8) } if rc != SQLITE_OK { if rc == int32(SQLITE_NOMEM) { _sqlite3OomFault(tls, db) } if *(*uintptr)(unsafe.Pointer(bp + 8)) != 0 { v2 = __ccgo_ts + 3795 } else { v2 = uintptr(0) } _sqlite3ErrorWithMsg(tls, db, rc, v2, libc.VaList(bp+24, *(*uintptr)(unsafe.Pointer(bp + 8)))) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) goto opendb_out } _ = libc.Int32FromInt32(0) /* Open the backend database driver */ rc = _sqlite3BtreeOpen(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, *(*uintptr)(unsafe.Pointer(bp + 4)), db, (*Tsqlite3)(unsafe.Pointer(db)).FaDb+4, 0, int32(*(*uint32)(unsafe.Pointer(bp))|uint32(SQLITE_OPEN_MAIN_DB))) if rc != SQLITE_OK { if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<= 0 { v2 = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12 } else { v2 = uintptr(0) } pCol = v2 } else { pTab = uintptr(0) goto error_out } } } /* The following block stores the meta information that will be returned ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey ** and autoinc. At this point there are two possibilities: ** ** 1. The specified column name was rowid", "oid" or "_rowid_" ** and there is no explicitly declared IPK column. ** ** 2. The table is not a view and the column name identified an ** explicitly declared column. Copy meta information from *pCol. */ if pCol != 0 { zDataType = _sqlite3ColumnType(tls, pCol, uintptr(0)) zCollSeq = _sqlite3ColumnColl(tls, pCol) notnull = libc.BoolInt32(int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf>>0)) != 0) primarykey = libc.BoolInt32(int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0) autoinc = libc.BoolInt32(int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) == iCol && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Autoincrement) != uint32(0)) } else { zDataType = __ccgo_ts + 1136 primarykey = int32(1) } if !(zCollSeq != 0) { zCollSeq = uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) } goto error_out error_out: ; _sqlite3BtreeLeaveAll(tls, db) /* Whether the function call succeeded or failed, set the output parameters ** to whatever their local counterparts contain. If an error did occur, ** this has the effect of zeroing all output parameters. */ if pzDataType != 0 { *(*uintptr)(unsafe.Pointer(pzDataType)) = zDataType } if pzCollSeq != 0 { *(*uintptr)(unsafe.Pointer(pzCollSeq)) = zCollSeq } if pNotNull != 0 { *(*int32)(unsafe.Pointer(pNotNull)) = notnull } if pPrimaryKey != 0 { *(*int32)(unsafe.Pointer(pPrimaryKey)) = primarykey } if pAutoinc != 0 { *(*int32)(unsafe.Pointer(pAutoinc)) = autoinc } if SQLITE_OK == rc && !(pTab != 0) { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = _sqlite3MPrintf(tls, db, __ccgo_ts+24818, libc.VaList(bp+16, zTableName, zColumnName)) rc = int32(SQLITE_ERROR) } if *(*uintptr)(unsafe.Pointer(bp)) != 0 { v3 = __ccgo_ts + 3795 } else { v3 = uintptr(0) } _sqlite3ErrorWithMsg(tls, db, rc, v3, libc.VaList(bp+16, *(*uintptr)(unsafe.Pointer(bp)))) _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp))) rc = _sqlite3ApiExit(tls, db, rc) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** Sleep for a little while. Return the amount of time slept. // */ func Xsqlite3_sleep(tls *libc.TLS, ms int32) (r int32) { var pVfs uintptr var rc, v1 int32 _, _, _ = pVfs, rc, v1 pVfs = Xsqlite3_vfs_find(tls, uintptr(0)) if pVfs == uintptr(0) { return 0 } /* This function works in milliseconds, but the underlying OsSleep() ** API uses microseconds. Hence the 1000's. */ if ms < 0 { v1 = 0 } else { v1 = int32(1000) * ms } rc = _sqlite3OsSleep(tls, pVfs, v1) / int32(1000) return rc } // C documentation // // /* // ** Enable or disable the extended result codes. // */ func Xsqlite3_extended_result_codes(tls *libc.TLS, db uintptr, onoff int32) (r int32) { var v1 uint32 _ = v1 Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if onoff != 0 { v1 = uint32(0xffffffff) } else { v1 = uint32(0xff) } (*Tsqlite3)(unsafe.Pointer(db)).FerrMask = int32(v1) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return SQLITE_OK } // C documentation // // /* // ** Invoke the xFileControl method on a particular database. // */ func Xsqlite3_file_control(tls *libc.TLS, db uintptr, zDbName uintptr, op int32, pArg uintptr) (r int32) { var fd, pBtree, pPager uintptr var iNew, nSave, rc int32 _, _, _, _, _, _ = fd, iNew, nSave, pBtree, pPager, rc rc = int32(SQLITE_ERROR) Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) pBtree = _sqlite3DbNameToBtree(tls, db, zDbName) if pBtree != 0 { _sqlite3BtreeEnter(tls, pBtree) pPager = _sqlite3BtreePager(tls, pBtree) _ = libc.Int32FromInt32(0) fd = _sqlite3PagerFile(tls, pPager) _ = libc.Int32FromInt32(0) if op == int32(SQLITE_FCNTL_FILE_POINTER) { *(*uintptr)(unsafe.Pointer(pArg)) = fd rc = SQLITE_OK } else { if op == int32(SQLITE_FCNTL_VFS_POINTER) { *(*uintptr)(unsafe.Pointer(pArg)) = _sqlite3PagerVfs(tls, pPager) rc = SQLITE_OK } else { if op == int32(SQLITE_FCNTL_JOURNAL_POINTER) { *(*uintptr)(unsafe.Pointer(pArg)) = _sqlite3PagerJrnlFile(tls, pPager) rc = SQLITE_OK } else { if op == int32(SQLITE_FCNTL_DATA_VERSION) { *(*uint32)(unsafe.Pointer(pArg)) = _sqlite3PagerDataVersion(tls, pPager) rc = SQLITE_OK } else { if op == int32(SQLITE_FCNTL_RESERVE_BYTES) { iNew = *(*int32)(unsafe.Pointer(pArg)) *(*int32)(unsafe.Pointer(pArg)) = _sqlite3BtreeGetRequestedReserve(tls, pBtree) if iNew >= 0 && iNew <= int32(255) { _sqlite3BtreeSetPageSize(tls, pBtree, 0, iNew, 0) } rc = SQLITE_OK } else { if op == int32(SQLITE_FCNTL_RESET_CACHE) { _sqlite3BtreeClearCache(tls, pBtree) rc = SQLITE_OK } else { nSave = (*Tsqlite3)(unsafe.Pointer(db)).FbusyHandler.FnBusy rc = _sqlite3OsFileControl(tls, fd, op, pArg) (*Tsqlite3)(unsafe.Pointer(db)).FbusyHandler.FnBusy = nSave } } } } } } _sqlite3BtreeLeave(tls, pBtree) } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** Interface to the testing logic. // */ func Xsqlite3_test_control(tls *libc.TLS, op int32, va uintptr) (r int32) { var aProg, db, db1, db2, db3, db4, db5, db6, pCtx, pI1, pI2, pU64, pn, ptr, xBenignBegin, xBenignEnd uintptr var ap Tva_list var b, b1, iDb, opTrace, rc, sz, x, x1, x2, y, v1, v3 int32 var newVal uint32 var rIn float64 var rLogEst TLogEst var v2 bool _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aProg, ap, b, b1, db, db1, db2, db3, db4, db5, db6, iDb, newVal, opTrace, pCtx, pI1, pI2, pU64, pn, ptr, rIn, rLogEst, rc, sz, x, x1, x2, xBenignBegin, xBenignEnd, y, v1, v2, v3 rc = 0 ap = va switch op { /* ** Save the current state of the PRNG. */ case int32(SQLITE_TESTCTRL_PRNG_SAVE): _sqlite3PrngSaveState(tls) break /* ** Restore the state of the PRNG to the last state saved using ** PRNG_SAVE. If PRNG_SAVE has never before been called, then ** this verb acts like PRNG_RESET. */ fallthrough case int32(SQLITE_TESTCTRL_PRNG_RESTORE): _sqlite3PrngRestoreState(tls) break /* sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, int x, sqlite3 *db); ** ** Control the seed for the pseudo-random number generator (PRNG) that ** is built into SQLite. Cases: ** ** x!=0 && db!=0 Seed the PRNG to the current value of the ** schema cookie in the main database for db, or ** x if the schema cookie is zero. This case ** is convenient to use with database fuzzers ** as it allows the fuzzer some control over the ** the PRNG seed. ** ** x!=0 && db==0 Seed the PRNG to the value of x. ** ** x==0 && db==0 Revert to default behavior of using the ** xRandomness method on the primary VFS. ** ** This test-control also resets the PRNG so that the new seed will ** be used for the next call to sqlite3_randomness(). */ fallthrough case int32(SQLITE_TESTCTRL_PRNG_SEED): x = libc.VaInt32(&ap) db = libc.VaUintptr(&ap) _ = libc.Int32FromInt32(0) if v2 = db != 0; v2 { v1 = (*TSchema)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema)).Fschema_cookie y = v1 } if v2 && v1 != 0 { x = y } _sqlite3Config.FiPrngSeed = uint32(x) Xsqlite3_randomness(tls, 0, uintptr(0)) break /* sqlite3_test_control(SQLITE_TESTCTRL_FK_NO_ACTION, sqlite3 *db, int b); ** ** If b is true, then activate the SQLITE_FkNoAction setting. If b is ** false then clearn that setting. If the SQLITE_FkNoAction setting is ** abled, all foreign key ON DELETE and ON UPDATE actions behave as if ** they were NO ACTION, regardless of how they are defined. ** ** NB: One must usually run "PRAGMA writable_schema=RESET" after ** using this test-control, before it will take full effect. failing ** to reset the schema can result in some unexpected behavior. */ fallthrough case int32(SQLITE_TESTCTRL_FK_NO_ACTION): db1 = libc.VaUintptr(&ap) b = libc.VaInt32(&ap) if b != 0 { *(*Tu64)(unsafe.Pointer(db1 + 32)) |= uint64(libc.Int32FromInt32(0x00008)) << libc.Int32FromInt32(32) } else { *(*Tu64)(unsafe.Pointer(db1 + 32)) &= ^(uint64(libc.Int32FromInt32(0x00008)) << libc.Int32FromInt32(32)) } break /* ** sqlite3_test_control(BITVEC_TEST, size, program) ** ** Run a test against a Bitvec object of size. The program argument ** is an array of integers that defines the test. Return -1 on a ** memory allocation error, 0 on success, or non-zero for an error. ** See the sqlite3BitvecBuiltinTest() for additional information. */ fallthrough case int32(SQLITE_TESTCTRL_BITVEC_TEST): sz = libc.VaInt32(&ap) aProg = libc.VaUintptr(&ap) rc = _sqlite3BitvecBuiltinTest(tls, sz, aProg) break /* ** sqlite3_test_control(FAULT_INSTALL, xCallback) ** ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called, ** if xCallback is not NULL. ** ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0) ** is called immediately after installing the new callback and the return ** value from sqlite3FaultSim(0) becomes the return from ** sqlite3_test_control(). */ fallthrough case int32(SQLITE_TESTCTRL_FAULT_INSTALL): _sqlite3Config.FxTestCallback = libc.VaUintptr(&ap) rc = _sqlite3FaultSim(tls, 0) break /* ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd) ** ** Register hooks to call to indicate which malloc() failures ** are benign. */ fallthrough case int32(SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS): xBenignBegin = libc.VaUintptr(&ap) xBenignEnd = libc.VaUintptr(&ap) _sqlite3BenignMallocHooks(tls, xBenignBegin, xBenignEnd) break /* ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X) ** ** Set the PENDING byte to the value in the argument, if X>0. ** Make no changes if X==0. Return the value of the pending byte ** as it existing before this routine was called. ** ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in ** an incompatible database file format. Changing the PENDING byte ** while any database connection is open results in undefined and ** deleterious behavior. */ fallthrough case int32(SQLITE_TESTCTRL_PENDING_BYTE): rc = _sqlite3PendingByte newVal = libc.VaUint32(&ap) if newVal != 0 { _sqlite3PendingByte = int32(newVal) } break /* ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X) ** ** This action provides a run-time test to see whether or not ** assert() was enabled at compile-time. If X is true and assert() ** is enabled, then the return value is true. If X is true and ** assert() is disabled, then the return value is zero. If X is ** false and assert() is enabled, then the assertion fires and the ** process aborts. If X is false and assert() is disabled, then the ** return value is zero. */ fallthrough case int32(SQLITE_TESTCTRL_ASSERT): x1 = 0 _ = libc.Int32FromInt32(0) rc = x1 break /* ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X) ** ** This action provides a run-time test to see how the ALWAYS and ** NEVER macros were defined at compile-time. ** ** The return value is ALWAYS(X) if X is true, or 0 if X is false. ** ** The recommended test is X==2. If the return value is 2, that means ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the ** default setting. If the return value is 1, then ALWAYS() is either ** hard-coded to true or else it asserts if its argument is false. ** The first behavior (hard-coded to true) is the case if ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second ** behavior (assert if the argument to ALWAYS() is false) is the case if ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled. ** ** The run-time test procedure might look something like this: ** ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){ ** // ALWAYS() and NEVER() are no-op pass-through macros ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){ ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false. ** }else{ ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0. ** } */ fallthrough case int32(SQLITE_TESTCTRL_ALWAYS): x2 = libc.VaInt32(&ap) if x2 != 0 { v3 = x2 } else { v3 = 0 } rc = v3 break /* ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER); ** ** The integer returned reveals the byte-order of the computer on which ** SQLite is running: ** ** 1 big-endian, determined at run-time ** 10 little-endian, determined at run-time ** 432101 big-endian, determined at compile-time ** 123410 little-endian, determined at compile-time */ fallthrough case int32(SQLITE_TESTCTRL_BYTEORDER): rc = libc.Int32FromInt32(SQLITE_BYTEORDER)*libc.Int32FromInt32(100) + libc.Int32FromInt32(SQLITE_LITTLEENDIAN)*libc.Int32FromInt32(10) + libc.Int32FromInt32(SQLITE_BIGENDIAN) break /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N) ** ** Enable or disable various optimizations for testing purposes. The ** argument N is a bitmask of optimizations to be disabled. For normal ** operation N should be 0. The idea is that a test program (like the ** SQL Logic Test or SLT test module) can run the same SQL multiple times ** with various optimizations disabled to verify that the same answer ** is obtained in every case. */ fallthrough case int32(SQLITE_TESTCTRL_OPTIMIZATIONS): db2 = libc.VaUintptr(&ap) (*Tsqlite3)(unsafe.Pointer(db2)).FdbOptFlags = libc.VaUint32(&ap) break /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, onoff, xAlt); ** ** If parameter onoff is 1, subsequent calls to localtime() fail. ** If 2, then invoke xAlt() instead of localtime(). If 0, normal ** processing. ** ** xAlt arguments are void pointers, but they really want to be: ** ** int xAlt(const time_t*, struct tm*); ** ** xAlt should write results in to struct tm object of its 2nd argument ** and return zero on success, or return non-zero on failure. */ fallthrough case int32(SQLITE_TESTCTRL_LOCALTIME_FAULT): _sqlite3Config.FbLocaltimeFault = libc.VaInt32(&ap) if _sqlite3Config.FbLocaltimeFault == int32(2) { _sqlite3Config.FxAltLocaltime = libc.VaUintptr(&ap) } else { _sqlite3Config.FxAltLocaltime = uintptr(0) } break /* sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, sqlite3*); ** ** Toggle the ability to use internal functions on or off for ** the database connection given in the argument. */ fallthrough case int32(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS): db3 = libc.VaUintptr(&ap) *(*Tu32)(unsafe.Pointer(db3 + 24)) ^= uint32(DBFLAG_InternalFunc) break /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int); ** ** Set or clear a flag that indicates that the database file is always well- ** formed and never corrupt. This flag is clear by default, indicating that ** database files might have arbitrary corruption. Setting the flag during ** testing causes certain assert() statements in the code to be activated ** that demonstrate invariants on well-formed database files. */ fallthrough case int32(SQLITE_TESTCTRL_NEVER_CORRUPT): _sqlite3Config.FneverCorrupt = libc.VaInt32(&ap) break /* sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, int); ** ** Set or clear a flag that causes SQLite to verify that type, name, ** and tbl_name fields of the sqlite_schema table. This is normally ** on, but it is sometimes useful to turn it off for testing. ** ** 2020-07-22: Disabling EXTRA_SCHEMA_CHECKS also disables the ** verification of rootpage numbers when parsing the schema. This ** is useful to make it easier to reach strange internal error states ** during testing. The EXTRA_SCHEMA_CHECKS setting is always enabled ** in production. */ fallthrough case int32(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS): _sqlite3Config.FbExtraSchemaChecks = uint8(libc.VaInt32(&ap)) break /* Set the threshold at which OP_Once counters reset back to zero. ** By default this is 0x7ffffffe (over 2 billion), but that value is ** too big to test in a reasonable amount of time, so this control is ** provided to set a small and easily reachable reset value. */ fallthrough case int32(SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD): _sqlite3Config.FiOnceResetThreshold = libc.VaInt32(&ap) break /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr); ** ** Set the VDBE coverage callback function to xCallback with context ** pointer ptr. */ fallthrough case int32(SQLITE_TESTCTRL_VDBE_COVERAGE): break /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */ fallthrough case int32(SQLITE_TESTCTRL_SORTER_MMAP): db4 = libc.VaUintptr(&ap) (*Tsqlite3)(unsafe.Pointer(db4)).FnMaxSorterMmap = libc.VaInt32(&ap) break /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT); ** ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if ** not. */ fallthrough case int32(SQLITE_TESTCTRL_ISINIT): if libc.AtomicLoadPInt32(uintptr(unsafe.Pointer(&_sqlite3Config))+208) == 0 { rc = int32(SQLITE_ERROR) } break /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum); ** ** This test control is used to create imposter tables. "db" is a pointer ** to the database connection. dbName is the database name (ex: "main" or ** "temp") which will receive the imposter. "onOff" turns imposter mode on ** or off. "tnum" is the root page of the b-tree to which the imposter ** table should connect. ** ** Enable imposter mode only when the schema has already been parsed. Then ** run a single CREATE TABLE statement to construct the imposter table in ** the parsed schema. Then turn imposter mode back off again. ** ** If onOff==0 and tnum>0 then reset the schema for all databases, causing ** the schema to be reparsed the next time it is needed. This has the ** effect of erasing all imposter tables. */ fallthrough case int32(SQLITE_TESTCTRL_IMPOSTER): db5 = libc.VaUintptr(&ap) Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db5)).Fmutex) iDb = _sqlite3FindDbName(tls, db5, libc.VaUintptr(&ap)) if iDb >= 0 { (*Tsqlite3)(unsafe.Pointer(db5)).Finit1.FiDb = uint8(iDb) (*Tsqlite3)(unsafe.Pointer(db5)).Finit1.Fbusy = uint8(libc.AssignBitFieldPtr8Uint32(db5+172+8, uint32(libc.VaInt32(&ap)), 1, 1, 0x2)) (*Tsqlite3)(unsafe.Pointer(db5)).Finit1.FnewTnum = uint32(libc.VaInt32(&ap)) if int32((*Tsqlite3)(unsafe.Pointer(db5)).Finit1.Fbusy) == 0 && (*Tsqlite3)(unsafe.Pointer(db5)).Finit1.FnewTnum > uint32(0) { _sqlite3ResetAllSchemasOfConnection(tls, db5) } } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db5)).Fmutex) break /* sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*); ** ** This test-control causes the most recent sqlite3_result_int64() value ** to be interpreted as a MEM_IntReal instead of as an MEM_Int. Normally, ** MEM_IntReal values only arise during an INSERT operation of integer ** values into a REAL column, so they can be challenging to test. This ** test-control enables us to write an intreal() SQL function that can ** inject an intreal() value at arbitrary places in an SQL statement, ** for testing purposes. */ fallthrough case int32(SQLITE_TESTCTRL_RESULT_INTREAL): pCtx = libc.VaUintptr(&ap) _sqlite3ResultIntReal(tls, pCtx) break /* sqlite3_test_control(SQLITE_TESTCTRL_SEEK_COUNT, ** sqlite3 *db, // Database connection ** u64 *pnSeek // Write seek count here ** ); ** ** This test-control queries the seek-counter on the "main" database ** file. The seek-counter is written into *pnSeek and is then reset. ** The seek-count is only available if compiled with SQLITE_DEBUG. */ fallthrough case int32(SQLITE_TESTCTRL_SEEK_COUNT): db6 = libc.VaUintptr(&ap) pn = libc.VaUintptr(&ap) *(*Tu64)(unsafe.Pointer(pn)) = uint64(0) _ = db6 /* Silence harmless unused variable warning */ break /* sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, op, ptr) ** ** "ptr" is a pointer to a u32. ** ** op==0 Store the current sqlite3TreeTrace in *ptr ** op==1 Set sqlite3TreeTrace to the value *ptr ** op==2 Store the current sqlite3WhereTrace in *ptr ** op==3 Set sqlite3WhereTrace to the value *ptr */ fallthrough case int32(SQLITE_TESTCTRL_TRACEFLAGS): opTrace = libc.VaInt32(&ap) ptr = libc.VaUintptr(&ap) switch opTrace { case 0: *(*Tu32)(unsafe.Pointer(ptr)) = _sqlite3TreeTrace case int32(1): _sqlite3TreeTrace = *(*Tu32)(unsafe.Pointer(ptr)) case int32(2): *(*Tu32)(unsafe.Pointer(ptr)) = _sqlite3WhereTrace case int32(3): _sqlite3WhereTrace = *(*Tu32)(unsafe.Pointer(ptr)) break } break /* sqlite3_test_control(SQLITE_TESTCTRL_LOGEST, ** double fIn, // Input value ** int *pLogEst, // sqlite3LogEstFromDouble(fIn) ** u64 *pInt, // sqlite3LogEstToInt(*pLogEst) ** int *pLogEst2 // sqlite3LogEst(*pInt) ** ); ** ** Test access for the LogEst conversion routines. */ fallthrough case int32(SQLITE_TESTCTRL_LOGEST): rIn = libc.VaFloat64(&ap) rLogEst = _sqlite3LogEstFromDouble(tls, rIn) pI1 = libc.VaUintptr(&ap) pU64 = libc.VaUintptr(&ap) pI2 = libc.VaUintptr(&ap) *(*int32)(unsafe.Pointer(pI1)) = int32(rLogEst) *(*Tu64)(unsafe.Pointer(pU64)) = _sqlite3LogEstToInt(tls, rLogEst) *(*int32)(unsafe.Pointer(pI2)) = int32(_sqlite3LogEst(tls, *(*Tu64)(unsafe.Pointer(pU64)))) break /* sqlite3_test_control(SQLITE_TESTCTRL_USELONGDOUBLE, int X); ** ** X<0 Make no changes to the bUseLongDouble. Just report value. ** X==0 Disable bUseLongDouble ** X==1 Enable bUseLongDouble ** X>=2 Set bUseLongDouble to its default value for this platform */ fallthrough case int32(SQLITE_TESTCTRL_USELONGDOUBLE): b1 = libc.VaInt32(&ap) if b1 >= int32(2) { b1 = _hasHighPrecisionDouble(tls, b1) } if b1 >= 0 { _sqlite3Config.FbUseLongDouble = libc.BoolUint8(b1 > 0) } rc = libc.BoolInt32(int32(_sqlite3Config.FbUseLongDouble) != 0) break /* sqlite3_test_control(SQLITE_TESTCTRL_JSON_SELFCHECK, &onOff); ** ** Activate or deactivate validation of JSONB that is generated from ** text. Off by default, as the validation is slow. Validation is ** only available if compiled using SQLITE_DEBUG. ** ** If onOff is initially 1, then turn it on. If onOff is initially ** off, turn it off. If onOff is initially -1, then change onOff ** to be the current setting. */ fallthrough case int32(SQLITE_TESTCTRL_JSON_SELFCHECK): break } _ = ap return rc } // C documentation // // /* // ** The Pager stores the Database filename, Journal filename, and WAL filename // ** consecutively in memory, in that order. The database filename is prefixed // ** by four zero bytes. Locate the start of the database filename by searching // ** backwards for the first byte following four consecutive zero bytes. // ** // ** This only works if the filename passed in was obtained from the Pager. // */ func _databaseName(tls *libc.TLS, zName uintptr) (r uintptr) { for int32(*(*uint8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(1))))) != 0 || int32(*(*uint8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(2))))) != 0 || int32(*(*uint8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(3))))) != 0 || int32(*(*uint8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(4))))) != 0 { zName-- } return zName } // C documentation // // /* // ** Append text z[] to the end of p[]. Return a pointer to the first // ** character after then zero terminator on the new text in p[]. // */ func _appendText(tls *libc.TLS, p uintptr, z uintptr) (r uintptr) { var n Tsize_t _ = n n = libc.Xstrlen(tls, z) libc.Xmemcpy(tls, p, z, n+uint32(1)) return p + uintptr(n) + uintptr(1) } // C documentation // // /* // ** Allocate memory to hold names for a database, journal file, WAL file, // ** and query parameters. The pointer returned is valid for use by // ** sqlite3_filename_database() and sqlite3_uri_parameter() and related // ** functions. // ** // ** Memory layout must be compatible with that generated by the pager // ** and expected by sqlite3_uri_parameter() and databaseName(). // */ func Xsqlite3_create_filename(tls *libc.TLS, zDatabase uintptr, zJournal uintptr, zWal uintptr, nParam int32, azParam uintptr) (r uintptr) { var i int32 var nByte Tsqlite3_int64 var p, pResult, v2, v4, v5, v6 uintptr _, _, _, _, _, _, _, _ = i, nByte, p, pResult, v2, v4, v5, v6 nByte = int64(libc.Xstrlen(tls, zDatabase) + libc.Xstrlen(tls, zJournal) + libc.Xstrlen(tls, zWal) + uint32(10)) i = 0 for { if !(i < nParam*int32(2)) { break } nByte += int64(libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(azParam + uintptr(i)*4))) + uint32(1)) goto _1 _1: ; i++ } v2 = Xsqlite3_malloc64(tls, uint64(nByte)) p = v2 pResult = v2 if p == uintptr(0) { return uintptr(0) } libc.Xmemset(tls, p, 0, uint32(4)) p += uintptr(4) p = _appendText(tls, p, zDatabase) i = 0 for { if !(i < nParam*int32(2)) { break } p = _appendText(tls, p, *(*uintptr)(unsafe.Pointer(azParam + uintptr(i)*4))) goto _3 _3: ; i++ } v4 = p p++ *(*uint8)(unsafe.Pointer(v4)) = uint8(0) p = _appendText(tls, p, zJournal) p = _appendText(tls, p, zWal) v5 = p p++ *(*uint8)(unsafe.Pointer(v5)) = uint8(0) v6 = p p++ *(*uint8)(unsafe.Pointer(v6)) = uint8(0) _ = libc.Int32FromInt32(0) return pResult + uintptr(4) } // C documentation // // /* // ** Free memory obtained from sqlite3_create_filename(). It is a severe // ** error to call this routine with any parameter other than a pointer // ** previously obtained from sqlite3_create_filename() or a NULL pointer. // */ func Xsqlite3_free_filename(tls *libc.TLS, p uintptr) { if p == uintptr(0) { return } p = _databaseName(tls, p) Xsqlite3_free(tls, p-uintptr(4)) } // C documentation // // /* // ** This is a utility routine, useful to VFS implementations, that checks // ** to see if a database file was a URI that contained a specific query // ** parameter, and if so obtains the value of the query parameter. // ** // ** The zFilename argument is the filename pointer passed into the xOpen() // ** method of a VFS implementation. The zParam argument is the name of the // ** query parameter we seek. This routine returns the value of the zParam // ** parameter if it exists. If the parameter does not exist, this routine // ** returns a NULL pointer. // */ func Xsqlite3_uri_parameter(tls *libc.TLS, zFilename uintptr, zParam uintptr) (r uintptr) { if zFilename == uintptr(0) || zParam == uintptr(0) { return uintptr(0) } zFilename = _databaseName(tls, zFilename) return _uriParameter(tls, zFilename, zParam) } // C documentation // // /* // ** Return a pointer to the name of Nth query parameter of the filename. // */ func Xsqlite3_uri_key(tls *libc.TLS, zFilename uintptr, N int32) (r uintptr) { var v1 int32 var v2 bool var v3 uintptr _, _, _ = v1, v2, v3 if zFilename == uintptr(0) || N < 0 { return uintptr(0) } zFilename = _databaseName(tls, zFilename) zFilename += uintptr(_sqlite3Strlen30(tls, zFilename) + int32(1)) for { if v2 = zFilename != 0 && *(*uint8)(unsafe.Pointer(zFilename)) != 0; v2 { v1 = N N-- } if !(v2 && v1 > 0) { break } zFilename += uintptr(_sqlite3Strlen30(tls, zFilename) + int32(1)) zFilename += uintptr(_sqlite3Strlen30(tls, zFilename) + int32(1)) } if *(*uint8)(unsafe.Pointer(zFilename)) != 0 { v3 = zFilename } else { v3 = uintptr(0) } return v3 } // C documentation // // /* // ** Return a boolean value for a query parameter. // */ func Xsqlite3_uri_boolean(tls *libc.TLS, zFilename uintptr, zParam uintptr, bDflt int32) (r int32) { var z uintptr var v1 int32 _, _ = z, v1 z = Xsqlite3_uri_parameter(tls, zFilename, zParam) bDflt = libc.BoolInt32(bDflt != 0) if z != 0 { v1 = int32(_sqlite3GetBoolean(tls, z, uint8(bDflt))) } else { v1 = bDflt } return v1 } // C documentation // // /* // ** Return a 64-bit integer value for a query parameter. // */ func Xsqlite3_uri_int64(tls *libc.TLS, zFilename uintptr, zParam uintptr, bDflt Tsqlite3_int64) (r Tsqlite3_int64) { bp := tls.Alloc(16) defer tls.Free(16) var z uintptr var _ /* v at bp+0 */ Tsqlite3_int64 _ = z z = Xsqlite3_uri_parameter(tls, zFilename, zParam) if z != 0 && _sqlite3DecOrHexToI64(tls, z, bp) == 0 { bDflt = *(*Tsqlite3_int64)(unsafe.Pointer(bp)) } return bDflt } // C documentation // // /* // ** Translate a filename that was handed to a VFS routine into the corresponding // ** database, journal, or WAL file. // ** // ** It is an error to pass this routine a filename string that was not // ** passed into the VFS from the SQLite core. Doing so is similar to // ** passing free() a pointer that was not obtained from malloc() - it is // ** an error that we cannot easily detect but that will likely cause memory // ** corruption. // */ func Xsqlite3_filename_database(tls *libc.TLS, zFilename uintptr) (r uintptr) { if zFilename == uintptr(0) { return uintptr(0) } return _databaseName(tls, zFilename) } func Xsqlite3_filename_journal(tls *libc.TLS, zFilename uintptr) (r uintptr) { if zFilename == uintptr(0) { return uintptr(0) } zFilename = _databaseName(tls, zFilename) zFilename += uintptr(_sqlite3Strlen30(tls, zFilename) + int32(1)) for zFilename != 0 && *(*uint8)(unsafe.Pointer(zFilename)) != 0 { zFilename += uintptr(_sqlite3Strlen30(tls, zFilename) + int32(1)) zFilename += uintptr(_sqlite3Strlen30(tls, zFilename) + int32(1)) } return zFilename + uintptr(1) } func Xsqlite3_filename_wal(tls *libc.TLS, zFilename uintptr) (r uintptr) { zFilename = Xsqlite3_filename_journal(tls, zFilename) if zFilename != 0 { zFilename += uintptr(_sqlite3Strlen30(tls, zFilename) + int32(1)) } return zFilename } // C documentation // // /* // ** Return the Btree pointer identified by zDbName. Return NULL if not found. // */ func _sqlite3DbNameToBtree(tls *libc.TLS, db uintptr, zDbName uintptr) (r uintptr) { var iDb, v1 int32 var v2 uintptr _, _, _ = iDb, v1, v2 if zDbName != 0 { v1 = _sqlite3FindDbName(tls, db, zDbName) } else { v1 = 0 } iDb = v1 if iDb < 0 { v2 = uintptr(0) } else { v2 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt } return v2 } // C documentation // // /* // ** Return the name of the N-th database schema. Return NULL if N is out // ** of range. // */ func Xsqlite3_db_name(tls *libc.TLS, db uintptr, N int32) (r uintptr) { if N < 0 || N >= (*Tsqlite3)(unsafe.Pointer(db)).FnDb { return uintptr(0) } else { return (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(N)*16))).FzDbSName } return r } // C documentation // // /* // ** Return the filename of the database associated with a database // ** connection. // */ func Xsqlite3_db_filename(tls *libc.TLS, db uintptr, zDbName uintptr) (r uintptr) { var pBt, v1 uintptr _, _ = pBt, v1 pBt = _sqlite3DbNameToBtree(tls, db, zDbName) if pBt != 0 { v1 = _sqlite3BtreeGetFilename(tls, pBt) } else { v1 = uintptr(0) } return v1 } // C documentation // // /* // ** Return 1 if database is read-only or 0 if read/write. Return -1 if // ** no such database exists. // */ func Xsqlite3_db_readonly(tls *libc.TLS, db uintptr, zDbName uintptr) (r int32) { var pBt uintptr var v1 int32 _, _ = pBt, v1 pBt = _sqlite3DbNameToBtree(tls, db, zDbName) if pBt != 0 { v1 = _sqlite3BtreeIsReadonly(tls, pBt) } else { v1 = -int32(1) } return v1 } // C documentation // // /* // ** Obtain a snapshot handle for the snapshot of database zDb currently // ** being read by handle db. // */ func Xsqlite3_snapshot_get(tls *libc.TLS, db uintptr, zDb uintptr, ppSnapshot uintptr) (r int32) { var iDb, rc int32 var pBt uintptr _, _, _ = iDb, pBt, rc rc = int32(SQLITE_ERROR) Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if int32((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit) == 0 { iDb = _sqlite3FindDbName(tls, db, zDb) if iDb == 0 || iDb > int32(1) { pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt if int32(SQLITE_TXN_WRITE) != _sqlite3BtreeTxnState(tls, pBt) { rc = _sqlite3BtreeBeginTrans(tls, pBt, 0, uintptr(0)) if rc == SQLITE_OK { rc = _sqlite3PagerSnapshotGet(tls, _sqlite3BtreePager(tls, pBt), ppSnapshot) } } } } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** Open a read-transaction on the snapshot identified by pSnapshot. // */ func Xsqlite3_snapshot_open(tls *libc.TLS, db uintptr, zDb uintptr, pSnapshot uintptr) (r int32) { var bUnlock, iDb, rc int32 var pBt, pPager uintptr _, _, _, _, _ = bUnlock, iDb, pBt, pPager, rc rc = int32(SQLITE_ERROR) Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if int32((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit) == 0 { iDb = _sqlite3FindDbName(tls, db, zDb) if iDb == 0 || iDb > int32(1) { pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt if _sqlite3BtreeTxnState(tls, pBt) != int32(SQLITE_TXN_WRITE) { pPager = _sqlite3BtreePager(tls, pBt) bUnlock = 0 if _sqlite3BtreeTxnState(tls, pBt) != SQLITE_TXN_NONE { if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive == 0 { rc = _sqlite3PagerSnapshotCheck(tls, pPager, pSnapshot) if rc == SQLITE_OK { bUnlock = int32(1) rc = _sqlite3BtreeCommit(tls, pBt) } } } else { rc = SQLITE_OK } if rc == SQLITE_OK { rc = _sqlite3PagerSnapshotOpen(tls, pPager, pSnapshot) } if rc == SQLITE_OK { rc = _sqlite3BtreeBeginTrans(tls, pBt, 0, uintptr(0)) _sqlite3PagerSnapshotOpen(tls, pPager, uintptr(0)) } if bUnlock != 0 { _sqlite3PagerSnapshotUnlock(tls, pPager) } } } } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** Recover as many snapshots as possible from the wal file associated with // ** schema zDb of database db. // */ func Xsqlite3_snapshot_recover(tls *libc.TLS, db uintptr, zDb uintptr) (r int32) { var iDb, rc int32 var pBt uintptr _, _, _ = iDb, pBt, rc rc = int32(SQLITE_ERROR) Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) iDb = _sqlite3FindDbName(tls, db, zDb) if iDb == 0 || iDb > int32(1) { pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt if SQLITE_TXN_NONE == _sqlite3BtreeTxnState(tls, pBt) { rc = _sqlite3BtreeBeginTrans(tls, pBt, 0, uintptr(0)) if rc == SQLITE_OK { rc = _sqlite3PagerSnapshotRecover(tls, _sqlite3BtreePager(tls, pBt)) _sqlite3BtreeCommit(tls, pBt) } } } Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** Free a snapshot handle obtained from sqlite3_snapshot_get(). // */ func Xsqlite3_snapshot_free(tls *libc.TLS, pSnapshot uintptr) { Xsqlite3_free(tls, pSnapshot) } // C documentation // // /* // ** Given the name of a compile-time option, return true if that option // ** was used and false if not. // ** // ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix // ** is not required for a match. // */ func Xsqlite3_compileoption_used(tls *libc.TLS, zOptName uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var azCompileOpt uintptr var i, n int32 var _ /* nOpt at bp+0 */ int32 _, _, _ = azCompileOpt, i, n azCompileOpt = _sqlite3CompileOptions(tls, bp) if Xsqlite3_strnicmp(tls, zOptName, __ccgo_ts+24846, int32(7)) == 0 { zOptName += uintptr(7) } n = _sqlite3Strlen30(tls, zOptName) /* Since nOpt is normally in single digits, a linear search is ** adequate. No need for a binary search. */ i = 0 for { if !(i < *(*int32)(unsafe.Pointer(bp))) { break } if Xsqlite3_strnicmp(tls, zOptName, *(*uintptr)(unsafe.Pointer(azCompileOpt + uintptr(i)*4)), n) == 0 && _sqlite3IsIdChar(tls, *(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(azCompileOpt + uintptr(i)*4)) + uintptr(n)))) == 0 { return int32(1) } goto _1 _1: ; i++ } return 0 } // C documentation // // /* // ** Return the N-th compile-time option string. If N is out of range, // ** return a NULL pointer. // */ func Xsqlite3_compileoption_get(tls *libc.TLS, N int32) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var azCompileOpt uintptr var _ /* nOpt at bp+0 */ int32 _ = azCompileOpt azCompileOpt = _sqlite3CompileOptions(tls, bp) if N >= 0 && N < *(*int32)(unsafe.Pointer(bp)) { return *(*uintptr)(unsafe.Pointer(azCompileOpt + uintptr(N)*4)) } return uintptr(0) } /************** End of main.c ************************************************/ /************** Begin file notify.c ******************************************/ /* ** 2009 March 3 ** ** 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 contains the implementation of the sqlite3_unlock_notify() ** API method and its associated functionality. */ /* #include "sqliteInt.h" */ /* #include "btreeInt.h" */ /* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */ /* ** Public interfaces: ** ** sqlite3ConnectionBlocked() ** sqlite3ConnectionUnlocked() ** sqlite3ConnectionClosed() ** sqlite3_unlock_notify() */ // C documentation // // /* // ** Head of a linked list of all sqlite3 objects created by this process // ** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection // ** is not NULL. This variable may only accessed while the STATIC_MAIN // ** mutex is held. // */ var _sqlite3BlockedList = uintptr(0) // C documentation // // /* // ** Remove connection db from the blocked connections list. If connection // ** db is not currently a part of the list, this function is a no-op. // */ func _removeFromBlockedList(tls *libc.TLS, db uintptr) { var pp uintptr _ = pp _ = libc.Int32FromInt32(0) pp = uintptr(unsafe.Pointer(&_sqlite3BlockedList)) for { if !(*(*uintptr)(unsafe.Pointer(pp)) != 0) { break } if *(*uintptr)(unsafe.Pointer(pp)) == db { *(*uintptr)(unsafe.Pointer(pp)) = (*Tsqlite3)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FpNextBlocked break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 568 } } // C documentation // // /* // ** Add connection db to the blocked connections list. It is assumed // ** that it is not already a part of the list. // */ func _addToBlockedList(tls *libc.TLS, db uintptr) { var pp uintptr _ = pp _ = libc.Int32FromInt32(0) pp = uintptr(unsafe.Pointer(&_sqlite3BlockedList)) for { if !(*(*uintptr)(unsafe.Pointer(pp)) != 0 && (*Tsqlite3)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FxUnlockNotify != (*Tsqlite3)(unsafe.Pointer(db)).FxUnlockNotify) { break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 568 } (*Tsqlite3)(unsafe.Pointer(db)).FpNextBlocked = *(*uintptr)(unsafe.Pointer(pp)) *(*uintptr)(unsafe.Pointer(pp)) = db } // C documentation // // /* // ** Obtain the STATIC_MAIN mutex. // */ func _enterMutex(tls *libc.TLS) { Xsqlite3_mutex_enter(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN))) } // C documentation // // /* // ** Release the STATIC_MAIN mutex. // */ func _leaveMutex(tls *libc.TLS) { _ = libc.Int32FromInt32(0) Xsqlite3_mutex_leave(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN))) } // C documentation // // /* // ** Register an unlock-notify callback. // ** // ** This is called after connection "db" has attempted some operation // ** but has received an SQLITE_LOCKED error because another connection // ** (call it pOther) in the same process was busy using the same shared // ** cache. pOther is found by looking at db->pBlockingConnection. // ** // ** If there is no blocking connection, the callback is invoked immediately, // ** before this routine returns. // ** // ** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate // ** a deadlock. // ** // ** Otherwise, make arrangements to invoke xNotify when pOther drops // ** its locks. // ** // ** Each call to this routine overrides any prior callbacks registered // ** on the same "db". If xNotify==0 then any prior callbacks are immediately // ** cancelled. // */ func Xsqlite3_unlock_notify(tls *libc.TLS, db uintptr, xNotify uintptr, _pArg uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) *(*uintptr)(unsafe.Pointer(bp)) = _pArg var p, v2 uintptr var rc int32 _, _, _ = p, rc, v2 rc = SQLITE_OK Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) _enterMutex(tls) if xNotify == uintptr(0) { _removeFromBlockedList(tls, db) (*Tsqlite3)(unsafe.Pointer(db)).FpBlockingConnection = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).FpUnlockConnection = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).FxUnlockNotify = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).FpUnlockArg = uintptr(0) } else { if uintptr(0) == (*Tsqlite3)(unsafe.Pointer(db)).FpBlockingConnection { /* The blocking transaction has been concluded. Or there never was a ** blocking transaction. In either case, invoke the notify callback ** immediately. */ (*(*func(*libc.TLS, uintptr, int32))(unsafe.Pointer(&struct{ uintptr }{xNotify})))(tls, bp, int32(1)) } else { p = (*Tsqlite3)(unsafe.Pointer(db)).FpBlockingConnection for { if !(p != 0 && p != db) { break } goto _1 _1: ; p = (*Tsqlite3)(unsafe.Pointer(p)).FpUnlockConnection } if p != 0 { rc = int32(SQLITE_LOCKED) /* Deadlock detected. */ } else { (*Tsqlite3)(unsafe.Pointer(db)).FpUnlockConnection = (*Tsqlite3)(unsafe.Pointer(db)).FpBlockingConnection (*Tsqlite3)(unsafe.Pointer(db)).FxUnlockNotify = xNotify (*Tsqlite3)(unsafe.Pointer(db)).FpUnlockArg = *(*uintptr)(unsafe.Pointer(bp)) _removeFromBlockedList(tls, db) _addToBlockedList(tls, db) } } } _leaveMutex(tls) _ = libc.Int32FromInt32(0) if rc != 0 { v2 = __ccgo_ts + 24854 } else { v2 = uintptr(0) } _sqlite3ErrorWithMsg(tls, db, rc, v2, 0) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } // C documentation // // /* // ** This function is called while stepping or preparing a statement // ** associated with connection db. The operation will return SQLITE_LOCKED // ** to the user because it requires a lock that will not be available // ** until connection pBlocker concludes its current transaction. // */ func _sqlite3ConnectionBlocked(tls *libc.TLS, db uintptr, pBlocker uintptr) { _enterMutex(tls) if (*Tsqlite3)(unsafe.Pointer(db)).FpBlockingConnection == uintptr(0) && (*Tsqlite3)(unsafe.Pointer(db)).FpUnlockConnection == uintptr(0) { _addToBlockedList(tls, db) } (*Tsqlite3)(unsafe.Pointer(db)).FpBlockingConnection = pBlocker _leaveMutex(tls) } // C documentation // // /* // ** This function is called when // ** the transaction opened by database db has just finished. Locks held // ** by database connection db have been released. // ** // ** This function loops through each entry in the blocked connections // ** list and does the following: // ** // ** 1) If the sqlite3.pBlockingConnection member of a list entry is // ** set to db, then set pBlockingConnection=0. // ** // ** 2) If the sqlite3.pUnlockConnection member of a list entry is // ** set to db, then invoke the configured unlock-notify callback and // ** set pUnlockConnection=0. // ** // ** 3) If the two steps above mean that pBlockingConnection==0 and // ** pUnlockConnection==0, remove the entry from the blocked connections // ** list. // */ func _sqlite3ConnectionUnlocked(tls *libc.TLS, db uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var aArg, aDyn, p, pNew, pp, xUnlockNotify, v2 uintptr var nArg, v3 int32 var _ /* aStatic at bp+0 */ [16]uintptr _, _, _, _, _, _, _, _, _ = aArg, aDyn, nArg, p, pNew, pp, xUnlockNotify, v2, v3 xUnlockNotify = uintptr(0) /* Unlock-notify cb to invoke */ nArg = 0 /* Arguments to the unlock callback */ aDyn = uintptr(0) /* Starter space for aArg[]. No malloc required */ aArg = bp _enterMutex(tls) /* Enter STATIC_MAIN mutex */ /* This loop runs once for each entry in the blocked-connections list. */ pp = uintptr(unsafe.Pointer(&_sqlite3BlockedList)) for { if !(*(*uintptr)(unsafe.Pointer(pp)) != 0) { break } p = *(*uintptr)(unsafe.Pointer(pp)) /* Step 1. */ if (*Tsqlite3)(unsafe.Pointer(p)).FpBlockingConnection == db { (*Tsqlite3)(unsafe.Pointer(p)).FpBlockingConnection = uintptr(0) } /* Step 2. */ if (*Tsqlite3)(unsafe.Pointer(p)).FpUnlockConnection == db { _ = libc.Int32FromInt32(0) if (*Tsqlite3)(unsafe.Pointer(p)).FxUnlockNotify != xUnlockNotify && nArg != 0 { (*(*func(*libc.TLS, uintptr, int32))(unsafe.Pointer(&struct{ uintptr }{xUnlockNotify})))(tls, aArg, nArg) nArg = 0 } _sqlite3BeginBenignMalloc(tls) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if !(aDyn != 0) && nArg == int32(libc.Uint32FromInt64(64)/libc.Uint32FromInt64(4)) || aDyn != 0 && nArg == int32(uint32(_sqlite3MallocSize(tls, aDyn))/libc.Uint32FromInt64(4)) { /* The aArg[] array needs to grow. */ pNew = _sqlite3Malloc(tls, uint64(uint32(nArg)*uint32(4)*uint32(2))) if pNew != 0 { libc.Xmemcpy(tls, pNew, aArg, uint32(nArg)*uint32(4)) Xsqlite3_free(tls, aDyn) v2 = pNew aArg = v2 aDyn = v2 } else { /* This occurs when the array of context pointers that need to ** be passed to the unlock-notify callback is larger than the ** aStatic[] array allocated on the stack and the attempt to ** allocate a larger array from the heap has failed. ** ** This is a difficult situation to handle. Returning an error ** code to the caller is insufficient, as even if an error code ** is returned the transaction on connection db will still be ** closed and the unlock-notify callbacks on blocked connections ** will go unissued. This might cause the application to wait ** indefinitely for an unlock-notify callback that will never ** arrive. ** ** Instead, invoke the unlock-notify callback with the context ** array already accumulated. We can then clear the array and ** begin accumulating any further context pointers without ** requiring any dynamic allocation. This is sub-optimal because ** it means that instead of one callback with a large array of ** context pointers the application will receive two or more ** callbacks with smaller arrays of context pointers, which will ** reduce the applications ability to prioritize multiple ** connections. But it is the best that can be done under the ** circumstances. */ (*(*func(*libc.TLS, uintptr, int32))(unsafe.Pointer(&struct{ uintptr }{xUnlockNotify})))(tls, aArg, nArg) nArg = 0 } } _sqlite3EndBenignMalloc(tls) v3 = nArg nArg++ *(*uintptr)(unsafe.Pointer(aArg + uintptr(v3)*4)) = (*Tsqlite3)(unsafe.Pointer(p)).FpUnlockArg xUnlockNotify = (*Tsqlite3)(unsafe.Pointer(p)).FxUnlockNotify (*Tsqlite3)(unsafe.Pointer(p)).FpUnlockConnection = uintptr(0) (*Tsqlite3)(unsafe.Pointer(p)).FxUnlockNotify = uintptr(0) (*Tsqlite3)(unsafe.Pointer(p)).FpUnlockArg = uintptr(0) } /* Step 3. */ if (*Tsqlite3)(unsafe.Pointer(p)).FpBlockingConnection == uintptr(0) && (*Tsqlite3)(unsafe.Pointer(p)).FpUnlockConnection == uintptr(0) { /* Remove connection p from the blocked connections list. */ *(*uintptr)(unsafe.Pointer(pp)) = (*Tsqlite3)(unsafe.Pointer(p)).FpNextBlocked (*Tsqlite3)(unsafe.Pointer(p)).FpNextBlocked = uintptr(0) } else { pp = p + 568 } goto _1 _1: } if nArg != 0 { (*(*func(*libc.TLS, uintptr, int32))(unsafe.Pointer(&struct{ uintptr }{xUnlockNotify})))(tls, aArg, nArg) } Xsqlite3_free(tls, aDyn) _leaveMutex(tls) /* Leave STATIC_MAIN mutex */ } // C documentation // // /* // ** This is called when the database connection passed as an argument is // ** being closed. The connection is removed from the blocked list. // */ func _sqlite3ConnectionClosed(tls *libc.TLS, db uintptr) { _sqlite3ConnectionUnlocked(tls, db) _enterMutex(tls) _removeFromBlockedList(tls, db) _leaveMutex(tls) } /************** End of notify.c **********************************************/ /************** Begin file fts3.c ********************************************/ /* ** 2006 Oct 10 ** ** 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 is an SQLite module implementing full-text search. */ /* ** The code in this file is only compiled if: ** ** * The FTS3 module is being built as an extension ** (in which case SQLITE_CORE is not defined), or ** ** * The FTS3 module is being built into the core of ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). */ /* The full-text index is stored in a series of b+tree (-like) ** structures called segments which map terms to doclists. The ** structures are like b+trees in layout, but are constructed from the ** bottom up in optimal fashion and are not updatable. Since trees ** are built from the bottom up, things will be described from the ** bottom up. ** ** **** Varints **** ** The basic unit of encoding is a variable-length integer called a ** varint. We encode variable-length integers in little-endian order ** using seven bits * per byte as follows: ** ** KEY: ** A = 0xxxxxxx 7 bits of data and one flag bit ** B = 1xxxxxxx 7 bits of data and one flag bit ** ** 7 bits - A ** 14 bits - BA ** 21 bits - BBA ** and so on. ** ** This is similar in concept to how sqlite encodes "varints" but ** the encoding is not the same. SQLite varints are big-endian ** are are limited to 9 bytes in length whereas FTS3 varints are ** little-endian and can be up to 10 bytes in length (in theory). ** ** Example encodings: ** ** 1: 0x01 ** 127: 0x7f ** 128: 0x81 0x00 ** ** **** Document lists **** ** A doclist (document list) holds a docid-sorted list of hits for a ** given term. Doclists hold docids and associated token positions. ** A docid is the unique integer identifier for a single document. ** A position is the index of a word within the document. The first ** word of the document has a position of 0. ** ** FTS3 used to optionally store character offsets using a compile-time ** option. But that functionality is no longer supported. ** ** A doclist is stored like this: ** ** array { ** varint docid; (delta from previous doclist) ** array { (position list for column 0) ** varint position; (2 more than the delta from previous position) ** } ** array { ** varint POS_COLUMN; (marks start of position list for new column) ** varint column; (index of new column) ** array { ** varint position; (2 more than the delta from previous position) ** } ** } ** varint POS_END; (marks end of positions for this document. ** } ** ** Here, array { X } means zero or more occurrences of X, adjacent in ** memory. A "position" is an index of a token in the token stream ** generated by the tokenizer. Note that POS_END and POS_COLUMN occur ** in the same logical place as the position element, and act as sentinals ** ending a position list array. POS_END is 0. POS_COLUMN is 1. ** The positions numbers are not stored literally but rather as two more ** than the difference from the prior position, or the just the position plus ** 2 for the first position. Example: ** ** label: A B C D E F G H I J K ** value: 123 5 9 1 1 14 35 0 234 72 0 ** ** The 123 value is the first docid. For column zero in this document ** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1 ** at D signals the start of a new column; the 1 at E indicates that the ** new column is column number 1. There are two positions at 12 and 45 ** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The ** 234 at I is the delta to next docid (357). It has one position 70 ** (72-2) and then terminates with the 0 at K. ** ** A "position-list" is the list of positions for multiple columns for ** a single docid. A "column-list" is the set of positions for a single ** column. Hence, a position-list consists of one or more column-lists, ** a document record consists of a docid followed by a position-list and ** a doclist consists of one or more document records. ** ** A bare doclist omits the position information, becoming an ** array of varint-encoded docids. ** **** Segment leaf nodes **** ** Segment leaf nodes store terms and doclists, ordered by term. Leaf ** nodes are written using LeafWriter, and read using LeafReader (to ** iterate through a single leaf node's data) and LeavesReader (to ** iterate through a segment's entire leaf layer). Leaf nodes have ** the format: ** ** varint iHeight; (height from leaf level, always 0) ** varint nTerm; (length of first term) ** char pTerm[nTerm]; (content of first term) ** varint nDoclist; (length of term's associated doclist) ** char pDoclist[nDoclist]; (content of doclist) ** array { ** (further terms are delta-encoded) ** varint nPrefix; (length of prefix shared with previous term) ** varint nSuffix; (length of unshared suffix) ** char pTermSuffix[nSuffix];(unshared suffix of next term) ** varint nDoclist; (length of term's associated doclist) ** char pDoclist[nDoclist]; (content of doclist) ** } ** ** Here, array { X } means zero or more occurrences of X, adjacent in ** memory. ** ** Leaf nodes are broken into blocks which are stored contiguously in ** the %_segments table in sorted order. This means that when the end ** of a node is reached, the next term is in the node with the next ** greater node id. ** ** New data is spilled to a new leaf node when the current node ** exceeds LEAF_MAX bytes (default 2048). New data which itself is ** larger than STANDALONE_MIN (default 1024) is placed in a standalone ** node (a leaf node with a single term and doclist). The goal of ** these settings is to pack together groups of small doclists while ** making it efficient to directly access large doclists. The ** assumption is that large doclists represent terms which are more ** likely to be query targets. ** ** TODO(shess) It may be useful for blocking decisions to be more ** dynamic. For instance, it may make more sense to have a 2.5k leaf ** node rather than splitting into 2k and .5k nodes. My intuition is ** that this might extend through 2x or 4x the pagesize. ** ** **** Segment interior nodes **** ** Segment interior nodes store blockids for subtree nodes and terms ** to describe what data is stored by the each subtree. Interior ** nodes are written using InteriorWriter, and read using ** InteriorReader. InteriorWriters are created as needed when ** SegmentWriter creates new leaf nodes, or when an interior node ** itself grows too big and must be split. The format of interior ** nodes: ** ** varint iHeight; (height from leaf level, always >0) ** varint iBlockid; (block id of node's leftmost subtree) ** optional { ** varint nTerm; (length of first term) ** char pTerm[nTerm]; (content of first term) ** array { ** (further terms are delta-encoded) ** varint nPrefix; (length of shared prefix with previous term) ** varint nSuffix; (length of unshared suffix) ** char pTermSuffix[nSuffix]; (unshared suffix of next term) ** } ** } ** ** Here, optional { X } means an optional element, while array { X } ** means zero or more occurrences of X, adjacent in memory. ** ** An interior node encodes n terms separating n+1 subtrees. The ** subtree blocks are contiguous, so only the first subtree's blockid ** is encoded. The subtree at iBlockid will contain all terms less ** than the first term encoded (or all terms if no term is encoded). ** Otherwise, for terms greater than or equal to pTerm[i] but less ** than pTerm[i+1], the subtree for that term will be rooted at ** iBlockid+i. Interior nodes only store enough term data to ** distinguish adjacent children (if the rightmost term of the left ** child is "something", and the leftmost term of the right child is ** "wicked", only "w" is stored). ** ** New data is spilled to a new interior node at the same height when ** the current node exceeds INTERIOR_MAX bytes (default 2048). ** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing ** interior nodes and making the tree too skinny. The interior nodes ** at a given height are naturally tracked by interior nodes at ** height+1, and so on. ** ** **** Segment directory **** ** The segment directory in table %_segdir stores meta-information for ** merging and deleting segments, and also the root node of the ** segment's tree. ** ** The root node is the top node of the segment's tree after encoding ** the entire segment, restricted to ROOT_MAX bytes (default 1024). ** This could be either a leaf node or an interior node. If the top ** node requires more than ROOT_MAX bytes, it is flushed to %_segments ** and a new root interior node is generated (which should always fit ** within ROOT_MAX because it only needs space for 2 varints, the ** height and the blockid of the previous root). ** ** The meta-information in the segment directory is: ** level - segment level (see below) ** idx - index within level ** - (level,idx uniquely identify a segment) ** start_block - first leaf node ** leaves_end_block - last leaf node ** end_block - last block (including interior nodes) ** root - contents of root node ** ** If the root node is a leaf node, then start_block, ** leaves_end_block, and end_block are all 0. ** ** **** Segment merging **** ** To amortize update costs, segments are grouped into levels and ** merged in batches. Each increase in level represents exponentially ** more documents. ** ** New documents (actually, document updates) are tokenized and ** written individually (using LeafWriter) to a level 0 segment, with ** incrementing idx. When idx reaches MERGE_COUNT (default 16), all ** level 0 segments are merged into a single level 1 segment. Level 1 ** is populated like level 0, and eventually MERGE_COUNT level 1 ** segments are merged to a single level 2 segment (representing ** MERGE_COUNT^2 updates), and so on. ** ** A segment merge traverses all segments at a given level in ** parallel, performing a straightforward sorted merge. Since segment ** leaf nodes are written in to the %_segments table in order, this ** merge traverses the underlying sqlite disk structures efficiently. ** After the merge, all segment blocks from the merged level are ** deleted. ** ** MERGE_COUNT controls how often we merge segments. 16 seems to be ** somewhat of a sweet spot for insertion performance. 32 and 64 show ** very similar performance numbers to 16 on insertion, though they're ** a tiny bit slower (perhaps due to more overhead in merge-time ** sorting). 8 is about 20% slower than 16, 4 about 50% slower than ** 16, 2 about 66% slower than 16. ** ** At query time, high MERGE_COUNT increases the number of segments ** which need to be scanned and merged. For instance, with 100k docs ** inserted: ** ** MERGE_COUNT segments ** 16 25 ** 8 12 ** 4 10 ** 2 6 ** ** This appears to have only a moderate impact on queries for very ** frequent terms (which are somewhat dominated by segment merge ** costs), and infrequent and non-existent terms still seem to be fast ** even with many segments. ** ** TODO(shess) That said, it would be nice to have a better query-side ** argument for MERGE_COUNT of 16. Also, it is possible/likely that ** optimizations to things like doclist merging will swing the sweet ** spot around. ** ** ** **** Handling of deletions and updates **** ** Since we're using a segmented structure, with no docid-oriented ** index into the term index, we clearly cannot simply update the term ** index when a document is deleted or updated. For deletions, we ** write an empty doclist (varint(docid) varint(POS_END)), for updates ** we simply write the new doclist. Segment merges overwrite older ** data for a particular docid with newer data, so deletes or updates ** will eventually overtake the earlier data and knock it out. The ** query logic likewise merges doclists so that newer data knocks out ** older data. */ /************** Include fts3Int.h in the middle of fts3.c ********************/ /************** Begin file fts3Int.h *****************************************/ /* ** 2009 Nov 12 ** ** 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. ** ****************************************************************************** ** */ /* FTS3/FTS4 require virtual tables */ /* ** FTS4 is really an extension for FTS3. It is enabled using the ** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3. */ /************** End of fts3Int.h *********************************************/ /************** Continuing where we left off in fts3.c ***********************/ /************** End of fts3.c ************************************************/ /************** Begin file fts3_aux.c ****************************************/ /* ** 2011 Jan 27 ** ** 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. ** ****************************************************************************** ** */ /* #include "fts3Int.h" */ /************** End of fts3_aux.c ********************************************/ /************** Begin file fts3_expr.c ***************************************/ /* ** 2008 Nov 28 ** ** 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 module contains code that implements a parser for fts3 query strings ** (the right-hand argument to the MATCH operator). Because the supported ** syntax is relatively simple, the whole tokenizer/parser system is ** hand-coded. */ /* #include "fts3Int.h" */ /************** End of fts3_expr.c *******************************************/ /************** Begin file fts3_hash.c ***************************************/ /* ** 2001 September 22 ** ** 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 is the implementation of generic hash-tables used in SQLite. ** We've modified it slightly to serve as a standalone hash table ** implementation for the full-text indexing module. */ /* ** The code in this file is only compiled if: ** ** * The FTS3 module is being built as an extension ** (in which case SQLITE_CORE is not defined), or ** ** * The FTS3 module is being built into the core of ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). */ /* #include "fts3Int.h" */ /************** End of fts3_hash.c *******************************************/ /************** Begin file fts3_porter.c *************************************/ /* ** 2006 September 30 ** ** 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. ** ************************************************************************* ** Implementation of the full-text-search tokenizer that implements ** a Porter stemmer. */ /* ** The code in this file is only compiled if: ** ** * The FTS3 module is being built as an extension ** (in which case SQLITE_CORE is not defined), or ** ** * The FTS3 module is being built into the core of ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). */ /* #include "fts3Int.h" */ /************** End of fts3_porter.c *****************************************/ /************** Begin file fts3_tokenizer.c **********************************/ /* ** 2007 June 22 ** ** 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 is part of an SQLite module implementing full-text search. ** This particular file implements the generic tokenizer interface. */ /* ** The code in this file is only compiled if: ** ** * The FTS3 module is being built as an extension ** (in which case SQLITE_CORE is not defined), or ** ** * The FTS3 module is being built into the core of ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). */ /* #include "fts3Int.h" */ /************** End of fts3_tokenizer.c **************************************/ /************** Begin file fts3_tokenizer1.c *********************************/ /* ** 2006 Oct 10 ** ** 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. ** ****************************************************************************** ** ** Implementation of the "simple" full-text-search tokenizer. */ /* ** The code in this file is only compiled if: ** ** * The FTS3 module is being built as an extension ** (in which case SQLITE_CORE is not defined), or ** ** * The FTS3 module is being built into the core of ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). */ /* #include "fts3Int.h" */ /************** End of fts3_tokenizer1.c *************************************/ /************** Begin file fts3_tokenize_vtab.c ******************************/ /* ** 2013 Apr 22 ** ** 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 contains code for the "fts3tokenize" virtual table module. ** An fts3tokenize virtual table is created as follows: ** ** CREATE VIRTUAL TABLE USING fts3tokenize( ** , , ... ** ); ** ** The table created has the following schema: ** ** CREATE TABLE (input, token, start, end, position) ** ** When queried, the query must include a WHERE clause of type: ** ** input = ** ** The virtual table module tokenizes this , using the FTS3 ** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE ** statement and returns one row for each token in the result. With ** fields set as follows: ** ** input: Always set to a copy of ** token: A token from the input. ** start: Byte offset of the token within the input . ** end: Byte offset of the byte immediately following the end of the ** token within the input string. ** pos: Token offset of token within input. ** */ /* #include "fts3Int.h" */ /************** End of fts3_tokenize_vtab.c **********************************/ /************** Begin file fts3_write.c **************************************/ /* ** 2009 Oct 23 ** ** 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 is part of the SQLite FTS3 extension module. Specifically, ** this file contains code to insert, update and delete rows from FTS3 ** tables. It also contains code to merge FTS3 b-tree segments. Some ** of the sub-routines used to merge segments are also used by the query ** code in fts3.c. */ /* #include "fts3Int.h" */ /************** End of fts3_write.c ******************************************/ /************** Begin file fts3_snippet.c ************************************/ /* ** 2009 Oct 23 ** ** 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. ** ****************************************************************************** */ /* #include "fts3Int.h" */ /************** End of fts3_snippet.c ****************************************/ /************** Begin file fts3_unicode.c ************************************/ /* ** 2012 May 24 ** ** 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. ** ****************************************************************************** ** ** Implementation of the "unicode" full-text-search tokenizer. */ /* #include "fts3Int.h" */ /************** End of fts3_unicode.c ****************************************/ /************** Begin file fts3_unicode2.c ***********************************/ /* ** 2012-05-25 ** ** 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. ** ****************************************************************************** */ /* ** DO NOT EDIT THIS MACHINE GENERATED FILE. */ /************** End of fts3_unicode2.c ***************************************/ /************** Begin file json.c ********************************************/ /* ** 2015-08-12 ** ** 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. ** ****************************************************************************** ** ** SQLite JSON functions. ** ** This file began as an extension in ext/misc/json1.c in 2015. That ** extension proved so useful that it has now been moved into the core. ** ** The original design stored all JSON as pure text, canonical RFC-8259. ** Support for JSON-5 extensions was added with version 3.42.0 (2023-05-16). ** All generated JSON text still conforms strictly to RFC-8259, but text ** with JSON-5 extensions is accepted as input. ** ** Beginning with version 3.45.0 (circa 2024-01-01), these routines also ** accept BLOB values that have JSON encoded using a binary representation ** called "JSONB". The name JSONB comes from PostgreSQL, however the on-disk ** format SQLite JSONB is completely different and incompatible with ** PostgreSQL JSONB. ** ** Decoding and interpreting JSONB is still O(N) where N is the size of ** the input, the same as text JSON. However, the constant of proportionality ** for JSONB is much smaller due to faster parsing. The size of each ** element in JSONB is encoded in its header, so there is no need to search ** for delimiters using persnickety syntax rules. JSONB seems to be about ** 3x faster than text JSON as a result. JSONB is also tends to be slightly ** smaller than text JSON, by 5% or 10%, but there are corner cases where ** JSONB can be slightly larger. So you are not far mistaken to say that ** a JSONB blob is the same size as the equivalent RFC-8259 text. ** ** ** THE JSONB ENCODING: ** ** Every JSON element is encoded in JSONB as a header and a payload. ** The header is between 1 and 9 bytes in size. The payload is zero ** or more bytes. ** ** The lower 4 bits of the first byte of the header determines the ** element type: ** ** 0: NULL ** 1: TRUE ** 2: FALSE ** 3: INT -- RFC-8259 integer literal ** 4: INT5 -- JSON5 integer literal ** 5: FLOAT -- RFC-8259 floating point literal ** 6: FLOAT5 -- JSON5 floating point literal ** 7: TEXT -- Text literal acceptable to both SQL and JSON ** 8: TEXTJ -- Text containing RFC-8259 escapes ** 9: TEXT5 -- Text containing JSON5 and/or RFC-8259 escapes ** 10: TEXTRAW -- Text containing unescaped syntax characters ** 11: ARRAY ** 12: OBJECT ** ** The other three possible values (13-15) are reserved for future ** enhancements. ** ** The upper 4 bits of the first byte determine the size of the header ** and sometimes also the size of the payload. If X is the first byte ** of the element and if X>>4 is between 0 and 11, then the payload ** will be that many bytes in size and the header is exactly one byte ** in size. Other four values for X>>4 (12-15) indicate that the header ** is more than one byte in size and that the payload size is determined ** by the remainder of the header, interpreted as a unsigned big-endian ** integer. ** ** Value of X>>4 Size integer Total header size ** ------------- -------------------- ----------------- ** 12 1 byte (0-255) 2 ** 13 2 byte (0-65535) 3 ** 14 4 byte (0-4294967295) 5 ** 15 8 byte (0-1.8e19) 9 ** ** The payload size need not be expressed in its minimal form. For example, ** if the payload size is 10, the size can be expressed in any of 5 different ** ways: (1) (X>>4)==10, (2) (X>>4)==12 following by on 0x0a byte, ** (3) (X>>4)==13 followed by 0x00 and 0x0a, (4) (X>>4)==14 followed by ** 0x00 0x00 0x00 0x0a, or (5) (X>>4)==15 followed by 7 bytes of 0x00 and ** a single byte of 0x0a. The shorter forms are preferred, of course, but ** sometimes when generating JSONB, the payload size is not known in advance ** and it is convenient to reserve sufficient header space to cover the ** largest possible payload size and then come back later and patch up ** the size when it becomes known, resulting in a non-minimal encoding. ** ** The value (X>>4)==15 is not actually used in the current implementation ** (as SQLite is currently unable handle BLOBs larger than about 2GB) ** but is included in the design to allow for future enhancements. ** ** The payload follows the header. NULL, TRUE, and FALSE have no payload and ** their payload size must always be zero. The payload for INT, INT5, ** FLOAT, FLOAT5, TEXT, TEXTJ, TEXT5, and TEXTROW is text. Note that the ** "..." or '...' delimiters are omitted from the various text encodings. ** The payload for ARRAY and OBJECT is a list of additional elements that ** are the content for the array or object. The payload for an OBJECT ** must be an even number of elements. The first element of each pair is ** the label and must be of type TEXT, TEXTJ, TEXT5, or TEXTRAW. ** ** A valid JSONB blob consists of a single element, as described above. ** Usually this will be an ARRAY or OBJECT element which has many more ** elements as its content. But the overall blob is just a single element. ** ** Input validation for JSONB blobs simply checks that the element type ** code is between 0 and 12 and that the total size of the element ** (header plus payload) is the same as the size of the BLOB. If those ** checks are true, the BLOB is assumed to be JSONB and processing continues. ** Errors are only raised if some other miscoding is discovered during ** processing. ** ** Additional information can be found in the doc/jsonb.md file of the ** canonical SQLite source tree. */ /* #include "sqliteInt.h" */ /* JSONB element types */ // C documentation // // /* Human-readable names for the JSONB values. The index for each // ** string must correspond to the JSONB_* integer above. // */ var _jsonbType = [17]uintptr{ 0: __ccgo_ts + 1634, 1: __ccgo_ts + 7886, 2: __ccgo_ts + 7891, 3: __ccgo_ts + 6306, 4: __ccgo_ts + 6306, 5: __ccgo_ts + 6301, 6: __ccgo_ts + 6301, 7: __ccgo_ts + 8197, 8: __ccgo_ts + 8197, 9: __ccgo_ts + 8197, 10: __ccgo_ts + 8197, 11: __ccgo_ts + 24877, 12: __ccgo_ts + 24883, 13: __ccgo_ts + 1648, 14: __ccgo_ts + 1648, 15: __ccgo_ts + 1648, 16: __ccgo_ts + 1648, } // C documentation // // /* // ** Growing our own isspace() routine this way is twice as fast as // ** the library isspace() function, resulting in a 7% overall performance // ** increase for the text-JSON parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os). // */ var _jsonIsSpace = [256]uint8{ 9: uint8(1), 10: uint8(1), 13: uint8(1), 32: uint8(1), } // C documentation // // /* // ** The set of all space characters recognized by jsonIsspace(). // ** Useful as the second argument to strspn(). // */ var _jsonSpaces = [5]uint8{9, 10, 13, ' '} // C documentation // // /* // ** Characters that are special to JSON. Control characters, // ** '"' and '\\' and '\''. Actually, '\'' is not special to // ** canonical JSON, but it is special in JSON-5, so we include // ** it in the set of special characters. // */ var _jsonIsOk = [256]uint8{ 32: uint8(1), 33: uint8(1), 35: uint8(1), 36: uint8(1), 37: uint8(1), 38: uint8(1), 40: uint8(1), 41: uint8(1), 42: uint8(1), 43: uint8(1), 44: uint8(1), 45: uint8(1), 46: uint8(1), 47: uint8(1), 48: uint8(1), 49: uint8(1), 50: uint8(1), 51: uint8(1), 52: uint8(1), 53: uint8(1), 54: uint8(1), 55: uint8(1), 56: uint8(1), 57: uint8(1), 58: uint8(1), 59: uint8(1), 60: uint8(1), 61: uint8(1), 62: uint8(1), 63: uint8(1), 64: uint8(1), 65: uint8(1), 66: uint8(1), 67: uint8(1), 68: uint8(1), 69: uint8(1), 70: uint8(1), 71: uint8(1), 72: uint8(1), 73: uint8(1), 74: uint8(1), 75: uint8(1), 76: uint8(1), 77: uint8(1), 78: uint8(1), 79: uint8(1), 80: uint8(1), 81: uint8(1), 82: uint8(1), 83: uint8(1), 84: uint8(1), 85: uint8(1), 86: uint8(1), 87: uint8(1), 88: uint8(1), 89: uint8(1), 90: uint8(1), 91: uint8(1), 93: uint8(1), 94: uint8(1), 95: uint8(1), 96: uint8(1), 97: uint8(1), 98: uint8(1), 99: uint8(1), 100: uint8(1), 101: uint8(1), 102: uint8(1), 103: uint8(1), 104: uint8(1), 105: uint8(1), 106: uint8(1), 107: uint8(1), 108: uint8(1), 109: uint8(1), 110: uint8(1), 111: uint8(1), 112: uint8(1), 113: uint8(1), 114: uint8(1), 115: uint8(1), 116: uint8(1), 117: uint8(1), 118: uint8(1), 119: uint8(1), 120: uint8(1), 121: uint8(1), 122: uint8(1), 123: uint8(1), 124: uint8(1), 125: uint8(1), 126: uint8(1), 127: uint8(1), 128: uint8(1), 129: uint8(1), 130: uint8(1), 131: uint8(1), 132: uint8(1), 133: uint8(1), 134: uint8(1), 135: uint8(1), 136: uint8(1), 137: uint8(1), 138: uint8(1), 139: uint8(1), 140: uint8(1), 141: uint8(1), 142: uint8(1), 143: uint8(1), 144: uint8(1), 145: uint8(1), 146: uint8(1), 147: uint8(1), 148: uint8(1), 149: uint8(1), 150: uint8(1), 151: uint8(1), 152: uint8(1), 153: uint8(1), 154: uint8(1), 155: uint8(1), 156: uint8(1), 157: uint8(1), 158: uint8(1), 159: uint8(1), 160: uint8(1), 161: uint8(1), 162: uint8(1), 163: uint8(1), 164: uint8(1), 165: uint8(1), 166: uint8(1), 167: uint8(1), 168: uint8(1), 169: uint8(1), 170: uint8(1), 171: uint8(1), 172: uint8(1), 173: uint8(1), 174: uint8(1), 175: uint8(1), 176: uint8(1), 177: uint8(1), 178: uint8(1), 179: uint8(1), 180: uint8(1), 181: uint8(1), 182: uint8(1), 183: uint8(1), 184: uint8(1), 185: uint8(1), 186: uint8(1), 187: uint8(1), 188: uint8(1), 189: uint8(1), 190: uint8(1), 191: uint8(1), 192: uint8(1), 193: uint8(1), 194: uint8(1), 195: uint8(1), 196: uint8(1), 197: uint8(1), 198: uint8(1), 199: uint8(1), 200: uint8(1), 201: uint8(1), 202: uint8(1), 203: uint8(1), 204: uint8(1), 205: uint8(1), 206: uint8(1), 207: uint8(1), 208: uint8(1), 209: uint8(1), 210: uint8(1), 211: uint8(1), 212: uint8(1), 213: uint8(1), 214: uint8(1), 215: uint8(1), 216: uint8(1), 217: uint8(1), 218: uint8(1), 219: uint8(1), 220: uint8(1), 221: uint8(1), 222: uint8(1), 223: uint8(1), 224: uint8(1), 225: uint8(1), 226: uint8(1), 227: uint8(1), 228: uint8(1), 229: uint8(1), 230: uint8(1), 231: uint8(1), 232: uint8(1), 233: uint8(1), 234: uint8(1), 235: uint8(1), 236: uint8(1), 237: uint8(1), 238: uint8(1), 239: uint8(1), 240: uint8(1), 241: uint8(1), 242: uint8(1), 243: uint8(1), 244: uint8(1), 245: uint8(1), 246: uint8(1), 247: uint8(1), 248: uint8(1), 249: uint8(1), 250: uint8(1), 251: uint8(1), 252: uint8(1), 253: uint8(1), 254: uint8(1), 255: uint8(1), } // C documentation // // /* Objects */ type TJsonCache = struct { Fdb uintptr FnUsed int32 Fa [4]uintptr } type JsonCache = TJsonCache type TJsonString = struct { F__ccgo_align [0]uint32 FpCtx uintptr FzBuf uintptr FnAlloc Tu64 FnUsed Tu64 FbStatic Tu8 FeErr Tu8 FzSpace [100]uint8 F__ccgo_pad7 [2]byte } type JsonString = TJsonString type TJsonParse = struct { FaBlob uintptr FnBlob Tu32 FnBlobAlloc Tu32 FzJson uintptr Fdb uintptr FnJson int32 FnJPRef Tu32 FiErr Tu32 FiDepth Tu16 FnErr Tu8 Foom Tu8 FbJsonIsRCStr Tu8 FhasNonstd Tu8 FbReadOnly Tu8 FeEdit Tu8 Fdelta int32 FnIns Tu32 FiLabel Tu32 FaIns uintptr } type JsonParse = TJsonParse /* ** Magic number used for the JSON parse cache in sqlite3_get_auxdata() */ /* ** jsonUnescapeOneChar() returns this invalid code point if it encounters ** a syntax error. */ /* A cache mapping JSON text into JSONB blobs. ** ** Each cache entry is a JsonParse object with the following restrictions: ** ** * The bReadOnly flag must be set ** ** * The aBlob[] array must be owned by the JsonParse object. In other ** words, nBlobAlloc must be non-zero. ** ** * eEdit and delta must be zero. ** ** * zJson must be an RCStr. In other words bJsonIsRCStr must be true. */ type TJsonCache1 = struct { Fdb uintptr FnUsed int32 Fa [4]uintptr } type JsonCache1 = TJsonCache1 /* An instance of this object represents a JSON string ** under construction. Really, this is a generic string accumulator ** that can be and is used to create strings other than JSON. ** ** If the generated string is longer than will fit into the zSpace[] buffer, ** then it will be an RCStr string. This aids with caching of large ** JSON strings. */ type TJsonString1 = struct { F__ccgo_align [0]uint32 FpCtx uintptr FzBuf uintptr FnAlloc Tu64 FnUsed Tu64 FbStatic Tu8 FeErr Tu8 FzSpace [100]uint8 F__ccgo_pad7 [2]byte } type JsonString1 = TJsonString1 /* Allowed values for JsonString.eErr */ /* The "subtype" set for text JSON values passed through using ** sqlite3_result_subtype() and sqlite3_value_subtype(). */ /* ** Bit values for the flags passed into various SQL function implementations ** via the sqlite3_user_data() value. */ /* A parsed JSON value. Lifecycle: ** ** 1. JSON comes in and is parsed into a JSONB value in aBlob. The ** original text is stored in zJson. This step is skipped if the ** input is JSONB instead of text JSON. ** ** 2. The aBlob[] array is searched using the JSON path notation, if needed. ** ** 3. Zero or more changes are made to aBlob[] (via json_remove() or ** json_replace() or json_patch() or similar). ** ** 4. New JSON text is generated from the aBlob[] for output. This step ** is skipped if the function is one of the jsonb_* functions that ** returns JSONB instead of text JSON. */ type TJsonParse1 = struct { FaBlob uintptr FnBlob Tu32 FnBlobAlloc Tu32 FzJson uintptr Fdb uintptr FnJson int32 FnJPRef Tu32 FiErr Tu32 FiDepth Tu16 FnErr Tu8 Foom Tu8 FbJsonIsRCStr Tu8 FhasNonstd Tu8 FbReadOnly Tu8 FeEdit Tu8 Fdelta int32 FnIns Tu32 FiLabel Tu32 FaIns uintptr } type JsonParse1 = TJsonParse1 /************************************************************************** ** Utility routines for dealing with JsonCache objects **************************************************************************/ // C documentation // // /* // ** Free a JsonCache object. // */ func _jsonCacheDelete(tls *libc.TLS, p uintptr) { var i int32 _ = i i = 0 for { if !(i < (*TJsonCache)(unsafe.Pointer(p)).FnUsed) { break } _jsonParseFree(tls, *(*uintptr)(unsafe.Pointer(p + 8 + uintptr(i)*4))) goto _1 _1: ; i++ } _sqlite3DbFree(tls, (*TJsonCache)(unsafe.Pointer(p)).Fdb, p) } func _jsonCacheDeleteGeneric(tls *libc.TLS, p uintptr) { _jsonCacheDelete(tls, p) } // C documentation // // /* // ** Insert a new entry into the cache. If the cache is full, expel // ** the least recently used entry. Return SQLITE_OK on success or a // ** result code otherwise. // ** // ** Cache entries are stored in age order, oldest first. // */ func _jsonCacheInsert(tls *libc.TLS, ctx uintptr, pParse uintptr) (r int32) { var db, p uintptr _, _ = db, p _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) p = Xsqlite3_get_auxdata(tls, ctx, -int32(429938)) if p == uintptr(0) { db = Xsqlite3_context_db_handle(tls, ctx) p = _sqlite3DbMallocZero(tls, db, uint64(24)) if p == uintptr(0) { return int32(SQLITE_NOMEM) } (*TJsonCache)(unsafe.Pointer(p)).Fdb = db Xsqlite3_set_auxdata(tls, ctx, -int32(429938), p, __ccgo_fp(_jsonCacheDeleteGeneric)) p = Xsqlite3_get_auxdata(tls, ctx, -int32(429938)) if p == uintptr(0) { return int32(SQLITE_NOMEM) } } if (*TJsonCache)(unsafe.Pointer(p)).FnUsed >= int32(JSON_CACHE_SIZE) { _jsonParseFree(tls, *(*uintptr)(unsafe.Pointer(p + 8))) libc.Xmemmove(tls, p+8, p+8+1*4, uint32(libc.Int32FromInt32(JSON_CACHE_SIZE)-libc.Int32FromInt32(1))*libc.Uint32FromInt64(4)) (*TJsonCache)(unsafe.Pointer(p)).FnUsed = libc.Int32FromInt32(JSON_CACHE_SIZE) - libc.Int32FromInt32(1) } _ = libc.Int32FromInt32(0) (*TJsonParse)(unsafe.Pointer(pParse)).FeEdit = uint8(0) (*TJsonParse)(unsafe.Pointer(pParse)).FnJPRef++ (*TJsonParse)(unsafe.Pointer(pParse)).FbReadOnly = uint8(1) *(*uintptr)(unsafe.Pointer(p + 8 + uintptr((*TJsonCache)(unsafe.Pointer(p)).FnUsed)*4)) = pParse (*TJsonCache)(unsafe.Pointer(p)).FnUsed++ return SQLITE_OK } // C documentation // // /* // ** Search for a cached translation the json text supplied by pArg. Return // ** the JsonParse object if found. Return NULL if not found. // ** // ** When a match if found, the matching entry is moved to become the // ** most-recently used entry if it isn't so already. // ** // ** The JsonParse object returned still belongs to the Cache and might // ** be deleted at any moment. If the caller whants the JsonParse to // ** linger, it needs to increment the nPJRef reference counter. // */ func _jsonCacheSearch(tls *libc.TLS, ctx uintptr, pArg uintptr) (r uintptr) { var i, nJson int32 var p, tmp, zJson uintptr _, _, _, _, _ = i, nJson, p, tmp, zJson if Xsqlite3_value_type(tls, pArg) != int32(SQLITE_TEXT) { return uintptr(0) } zJson = Xsqlite3_value_text(tls, pArg) if zJson == uintptr(0) { return uintptr(0) } nJson = Xsqlite3_value_bytes(tls, pArg) p = Xsqlite3_get_auxdata(tls, ctx, -int32(429938)) if p == uintptr(0) { return uintptr(0) } i = 0 for { if !(i < (*TJsonCache)(unsafe.Pointer(p)).FnUsed) { break } if (*TJsonParse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 8 + uintptr(i)*4)))).FzJson == zJson { break } goto _1 _1: ; i++ } if i >= (*TJsonCache)(unsafe.Pointer(p)).FnUsed { i = 0 for { if !(i < (*TJsonCache)(unsafe.Pointer(p)).FnUsed) { break } if (*TJsonParse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 8 + uintptr(i)*4)))).FnJson != nJson { goto _2 } if libc.Xmemcmp(tls, (*TJsonParse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 8 + uintptr(i)*4)))).FzJson, zJson, uint32(nJson)) == 0 { break } goto _2 _2: ; i++ } } if i < (*TJsonCache)(unsafe.Pointer(p)).FnUsed { if i < (*TJsonCache)(unsafe.Pointer(p)).FnUsed-int32(1) { /* Make the matching entry the most recently used entry */ tmp = *(*uintptr)(unsafe.Pointer(p + 8 + uintptr(i)*4)) libc.Xmemmove(tls, p+8+uintptr(i)*4, p+8+uintptr(i+int32(1))*4, uint32((*TJsonCache)(unsafe.Pointer(p)).FnUsed-i-libc.Int32FromInt32(1))*uint32(4)) *(*uintptr)(unsafe.Pointer(p + 8 + uintptr((*TJsonCache)(unsafe.Pointer(p)).FnUsed-int32(1))*4)) = tmp i = (*TJsonCache)(unsafe.Pointer(p)).FnUsed - int32(1) } _ = libc.Int32FromInt32(0) return *(*uintptr)(unsafe.Pointer(p + 8 + uintptr(i)*4)) } else { return uintptr(0) } return r } /************************************************************************** ** Utility routines for dealing with JsonString objects **************************************************************************/ // C documentation // // /* Turn uninitialized bulk memory into a valid JsonString object // ** holding a zero-length string. // */ func _jsonStringZero(tls *libc.TLS, p uintptr) { (*TJsonString)(unsafe.Pointer(p)).FzBuf = p + 26 (*TJsonString)(unsafe.Pointer(p)).FnAlloc = uint64(100) (*TJsonString)(unsafe.Pointer(p)).FnUsed = uint64(0) (*TJsonString)(unsafe.Pointer(p)).FbStatic = uint8(1) } // C documentation // // /* Initialize the JsonString object // */ func _jsonStringInit(tls *libc.TLS, p uintptr, pCtx uintptr) { (*TJsonString)(unsafe.Pointer(p)).FpCtx = pCtx (*TJsonString)(unsafe.Pointer(p)).FeErr = uint8(0) _jsonStringZero(tls, p) } // C documentation // // /* Free all allocated memory and reset the JsonString object back to its // ** initial state. // */ func _jsonStringReset(tls *libc.TLS, p uintptr) { if !((*TJsonString)(unsafe.Pointer(p)).FbStatic != 0) { _sqlite3RCStrUnref(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf) } _jsonStringZero(tls, p) } // C documentation // // /* Report an out-of-memory (OOM) condition // */ func _jsonStringOom(tls *libc.TLS, p uintptr) { var p1 uintptr _ = p1 p1 = p + 25 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | libc.Int32FromInt32(JSTRING_OOM)) if (*TJsonString)(unsafe.Pointer(p)).FpCtx != 0 { Xsqlite3_result_error_nomem(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx) } _jsonStringReset(tls, p) } // C documentation // // /* Enlarge pJson->zBuf so that it can hold at least N more bytes. // ** Return zero on success. Return non-zero on an OOM error // */ func _jsonStringGrow(tls *libc.TLS, p uintptr, N Tu32) (r int32) { var nTotal Tu64 var zNew, p2 uintptr var v1 uint64 _, _, _, _ = nTotal, zNew, v1, p2 if uint64(N) < (*TJsonString)(unsafe.Pointer(p)).FnAlloc { v1 = (*TJsonString)(unsafe.Pointer(p)).FnAlloc * uint64(2) } else { v1 = (*TJsonString)(unsafe.Pointer(p)).FnAlloc + uint64(N) + uint64(10) } nTotal = v1 if (*TJsonString)(unsafe.Pointer(p)).FbStatic != 0 { if (*TJsonString)(unsafe.Pointer(p)).FeErr != 0 { return int32(1) } zNew = _sqlite3RCStrNew(tls, nTotal) if zNew == uintptr(0) { _jsonStringOom(tls, p) return int32(SQLITE_NOMEM) } libc.Xmemcpy(tls, zNew, (*TJsonString)(unsafe.Pointer(p)).FzBuf, uint32((*TJsonString)(unsafe.Pointer(p)).FnUsed)) (*TJsonString)(unsafe.Pointer(p)).FzBuf = zNew (*TJsonString)(unsafe.Pointer(p)).FbStatic = uint8(0) } else { (*TJsonString)(unsafe.Pointer(p)).FzBuf = _sqlite3RCStrResize(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf, nTotal) if (*TJsonString)(unsafe.Pointer(p)).FzBuf == uintptr(0) { p2 = p + 25 *(*Tu8)(unsafe.Pointer(p2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p2))) | libc.Int32FromInt32(JSTRING_OOM)) _jsonStringZero(tls, p) return int32(SQLITE_NOMEM) } } (*TJsonString)(unsafe.Pointer(p)).FnAlloc = nTotal return SQLITE_OK } // C documentation // // /* Append N bytes from zIn onto the end of the JsonString string. // */ func _jsonStringExpandAndAppend(tls *libc.TLS, p uintptr, zIn uintptr, N Tu32) { _ = libc.Int32FromInt32(0) if _jsonStringGrow(tls, p, N) != 0 { return } libc.Xmemcpy(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), zIn, N) *(*Tu64)(unsafe.Pointer(p + 16)) += uint64(N) } func _jsonAppendRaw(tls *libc.TLS, p uintptr, zIn uintptr, N Tu32) { if N == uint32(0) { return } if uint64(N)+(*TJsonString)(unsafe.Pointer(p)).FnUsed >= (*TJsonString)(unsafe.Pointer(p)).FnAlloc { _jsonStringExpandAndAppend(tls, p, zIn, N) } else { libc.Xmemcpy(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), zIn, N) *(*Tu64)(unsafe.Pointer(p + 16)) += uint64(N) } } func _jsonAppendRawNZ(tls *libc.TLS, p uintptr, zIn uintptr, N Tu32) { _ = libc.Int32FromInt32(0) if uint64(N)+(*TJsonString)(unsafe.Pointer(p)).FnUsed >= (*TJsonString)(unsafe.Pointer(p)).FnAlloc { _jsonStringExpandAndAppend(tls, p, zIn, N) } else { libc.Xmemcpy(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), zIn, N) *(*Tu64)(unsafe.Pointer(p + 16)) += uint64(N) } } // C documentation // // /* Append formatted text (not to exceed N bytes) to the JsonString. // */ func _jsonPrintf(tls *libc.TLS, N int32, p uintptr, zFormat uintptr, va uintptr) { var ap Tva_list _ = ap if (*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(N) >= (*TJsonString)(unsafe.Pointer(p)).FnAlloc && _jsonStringGrow(tls, p, uint32(N)) != 0 { return } ap = va Xsqlite3_vsnprintf(tls, N, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), zFormat, ap) _ = ap *(*Tu64)(unsafe.Pointer(p + 16)) += uint64(int32(libc.Xstrlen(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed)))) } // C documentation // // /* Append a single character // */ func _jsonAppendCharExpand(tls *libc.TLS, p uintptr, c uint8) { var v1 Tu64 var v2 uintptr _, _ = v1, v2 if _jsonStringGrow(tls, p, uint32(1)) != 0 { return } v2 = p + 16 v1 = *(*Tu64)(unsafe.Pointer(v2)) *(*Tu64)(unsafe.Pointer(v2))++ *(*uint8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v1))) = c } func _jsonAppendChar(tls *libc.TLS, p uintptr, c uint8) { var v1 Tu64 var v2 uintptr _, _ = v1, v2 if (*TJsonString)(unsafe.Pointer(p)).FnUsed >= (*TJsonString)(unsafe.Pointer(p)).FnAlloc { _jsonAppendCharExpand(tls, p, c) } else { v2 = p + 16 v1 = *(*Tu64)(unsafe.Pointer(v2)) *(*Tu64)(unsafe.Pointer(v2))++ *(*uint8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v1))) = c } } // C documentation // // /* Remove a single character from the end of the string // */ func _jsonStringTrimOneChar(tls *libc.TLS, p uintptr) { if int32((*TJsonString)(unsafe.Pointer(p)).FeErr) == 0 { _ = libc.Int32FromInt32(0) (*TJsonString)(unsafe.Pointer(p)).FnUsed-- } } // C documentation // // /* Make sure there is a zero terminator on p->zBuf[] // ** // ** Return true on success. Return false if an OOM prevents this // ** from happening. // */ func _jsonStringTerminate(tls *libc.TLS, p uintptr) (r int32) { _jsonAppendChar(tls, p, uint8(0)) _jsonStringTrimOneChar(tls, p) return libc.BoolInt32(int32((*TJsonString)(unsafe.Pointer(p)).FeErr) == 0) } // C documentation // // /* Append a comma separator to the output buffer, if the previous // ** character is not '[' or '{'. // */ func _jsonAppendSeparator(tls *libc.TLS, p uintptr) { var c uint8 _ = c if (*TJsonString)(unsafe.Pointer(p)).FnUsed == uint64(0) { return } c = *(*uint8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed-uint64(1)))) if int32(c) == int32('[') || int32(c) == int32('{') { return } _jsonAppendChar(tls, p, uint8(',')) } // C documentation // // /* Append the N-byte string in zIn to the end of the JsonString string // ** under construction. Enclose the string in double-quotes ("...") and // ** escape any double-quotes or backslash characters contained within the // ** string. // ** // ** This routine is a high-runner. There is a measurable performance // ** increase associated with unwinding the jsonIsOk[] loop. // */ func _jsonAppendString(tls *libc.TLS, p uintptr, zIn uintptr, N Tu32) { var c Tu8 var k Tu32 var z, v10, v12, v14, v16, v18, v2, v20, v22, v24, v6, v8 uintptr var v1, v11, v13, v15, v17, v19, v21, v23, v5, v7, v9 Tu64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, k, z, v1, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v2, v20, v21, v22, v23, v24, v5, v6, v7, v8, v9 z = zIn if z == uintptr(0) { return } if uint64(N)+(*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(2) >= (*TJsonString)(unsafe.Pointer(p)).FnAlloc && _jsonStringGrow(tls, p, N+uint32(2)) != 0 { return } v2 = p + 16 v1 = *(*Tu64)(unsafe.Pointer(v2)) *(*Tu64)(unsafe.Pointer(v2))++ *(*uint8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v1))) = uint8('"') for int32(1) != 0 { k = uint32(0) /* The following while() is the 4-way unwound equivalent of ** ** while( k= N { for k < N && _jsonIsOk[*(*Tu8)(unsafe.Pointer(z + uintptr(k)))] != 0 { k++ } break } if !(_jsonIsOk[*(*Tu8)(unsafe.Pointer(z + uintptr(k)))] != 0) { break } if !(_jsonIsOk[*(*Tu8)(unsafe.Pointer(z + uintptr(k+uint32(1))))] != 0) { k += uint32(1) break } if !(_jsonIsOk[*(*Tu8)(unsafe.Pointer(z + uintptr(k+uint32(2))))] != 0) { k += uint32(2) break } if !(_jsonIsOk[*(*Tu8)(unsafe.Pointer(z + uintptr(k+uint32(3))))] != 0) { k += uint32(3) break } else { k += uint32(4) } } if k >= N { if k > uint32(0) { libc.Xmemcpy(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), z, k) *(*Tu64)(unsafe.Pointer(p + 16)) += uint64(k) } break } if k > uint32(0) { libc.Xmemcpy(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), z, k) *(*Tu64)(unsafe.Pointer(p + 16)) += uint64(k) z += uintptr(k) N -= k } c = *(*Tu8)(unsafe.Pointer(z)) if !(int32(c) == int32('"') || int32(c) == int32('\\')) { goto _3 } goto json_simple_escape json_simple_escape: ; if (*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(N)+uint64(3) > (*TJsonString)(unsafe.Pointer(p)).FnAlloc && _jsonStringGrow(tls, p, N+uint32(3)) != 0 { return } v6 = p + 16 v5 = *(*Tu64)(unsafe.Pointer(v6)) *(*Tu64)(unsafe.Pointer(v6))++ *(*uint8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v5))) = uint8('\\') v8 = p + 16 v7 = *(*Tu64)(unsafe.Pointer(v8)) *(*Tu64)(unsafe.Pointer(v8))++ *(*uint8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v7))) = c goto _4 _3: ; if int32(c) == int32('\'') { v10 = p + 16 v9 = *(*Tu64)(unsafe.Pointer(v10)) *(*Tu64)(unsafe.Pointer(v10))++ *(*uint8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v9))) = c } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if _aSpecial[c] != 0 { c = _aSpecial[c] goto json_simple_escape } if (*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(N)+uint64(7) > (*TJsonString)(unsafe.Pointer(p)).FnAlloc && _jsonStringGrow(tls, p, N+uint32(7)) != 0 { return } v12 = p + 16 v11 = *(*Tu64)(unsafe.Pointer(v12)) *(*Tu64)(unsafe.Pointer(v12))++ *(*uint8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v11))) = uint8('\\') v14 = p + 16 v13 = *(*Tu64)(unsafe.Pointer(v14)) *(*Tu64)(unsafe.Pointer(v14))++ *(*uint8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v13))) = uint8('u') v16 = p + 16 v15 = *(*Tu64)(unsafe.Pointer(v16)) *(*Tu64)(unsafe.Pointer(v16))++ *(*uint8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v15))) = uint8('0') v18 = p + 16 v17 = *(*Tu64)(unsafe.Pointer(v18)) *(*Tu64)(unsafe.Pointer(v18))++ *(*uint8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v17))) = uint8('0') v20 = p + 16 v19 = *(*Tu64)(unsafe.Pointer(v20)) *(*Tu64)(unsafe.Pointer(v20))++ *(*uint8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v19))) = *(*uint8)(unsafe.Pointer(__ccgo_ts + 24890 + uintptr(int32(c)>>int32(4)))) v22 = p + 16 v21 = *(*Tu64)(unsafe.Pointer(v22)) *(*Tu64)(unsafe.Pointer(v22))++ *(*uint8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v21))) = *(*uint8)(unsafe.Pointer(__ccgo_ts + 24890 + uintptr(int32(c)&int32(0xf)))) } _4: ; z++ N-- } v24 = p + 16 v23 = *(*Tu64)(unsafe.Pointer(v24)) *(*Tu64)(unsafe.Pointer(v24))++ *(*uint8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v23))) = uint8('"') _ = libc.Int32FromInt32(0) } var _aSpecial = [32]uint8{ 8: uint8('b'), 9: uint8('t'), 10: uint8('n'), 12: uint8('f'), 13: uint8('r'), } // C documentation // // /* // ** Append an sqlite3_value (such as a function parameter) to the JSON // ** string under construction in p. // */ func _jsonAppendSqlValue(tls *libc.TLS, p uintptr, pValue uintptr) { bp := tls.Alloc(80) defer tls.Free(80) var n, n1 Tu32 var z, z1 uintptr var _ /* px at bp+0 */ TJsonParse _, _, _, _ = n, n1, z, z1 switch Xsqlite3_value_type(tls, pValue) { case int32(SQLITE_NULL): _jsonAppendRawNZ(tls, p, __ccgo_ts+1634, uint32(4)) case int32(SQLITE_FLOAT): _jsonPrintf(tls, int32(100), p, __ccgo_ts+15323, libc.VaList(bp+64, Xsqlite3_value_double(tls, pValue))) case int32(SQLITE_INTEGER): z = Xsqlite3_value_text(tls, pValue) n = uint32(Xsqlite3_value_bytes(tls, pValue)) _jsonAppendRaw(tls, p, z, n) case int32(SQLITE_TEXT): z1 = Xsqlite3_value_text(tls, pValue) n1 = uint32(Xsqlite3_value_bytes(tls, pValue)) if Xsqlite3_value_subtype(tls, pValue) == uint32(JSON_SUBTYPE) { _jsonAppendRaw(tls, p, z1, n1) } else { _jsonAppendString(tls, p, z1, n1) } default: if _jsonFuncArgMightBeBinary(tls, pValue) != 0 { libc.Xmemset(tls, bp, 0, uint32(56)) (*(*TJsonParse)(unsafe.Pointer(bp))).FaBlob = Xsqlite3_value_blob(tls, pValue) (*(*TJsonParse)(unsafe.Pointer(bp))).FnBlob = uint32(Xsqlite3_value_bytes(tls, pValue)) _jsonTranslateBlobToText(tls, bp, uint32(0), p) } else { if int32((*TJsonString)(unsafe.Pointer(p)).FeErr) == 0 { Xsqlite3_result_error(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx, __ccgo_ts+24907, -int32(1)) (*TJsonString)(unsafe.Pointer(p)).FeErr = uint8(JSTRING_ERR) _jsonStringReset(tls, p) } } break } } // C documentation // // /* Make the text in p (which is probably a generated JSON text string) // ** the result of the SQL function. // ** // ** The JsonString is reset. // ** // ** If pParse and ctx are both non-NULL, then the SQL string in p is // ** loaded into the zJson field of the pParse object as a RCStr and the // ** pParse is added to the cache. // */ func _jsonReturnString(tls *libc.TLS, p uintptr, pParse uintptr, ctx uintptr) { var flags, rc int32 _, _ = flags, rc _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TJsonString)(unsafe.Pointer(p)).FeErr) == 0 { flags = int32(Xsqlite3_user_data(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx)) if flags&int32(JSON_BLOB) != 0 { _jsonReturnStringAsBlob(tls, p) } else { if (*TJsonString)(unsafe.Pointer(p)).FbStatic != 0 { Xsqlite3_result_text64(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx, (*TJsonString)(unsafe.Pointer(p)).FzBuf, (*TJsonString)(unsafe.Pointer(p)).FnUsed, uintptr(-libc.Int32FromInt32(1)), uint8(SQLITE_UTF8)) } else { if _jsonStringTerminate(tls, p) != 0 { if pParse != 0 && int32((*TJsonParse)(unsafe.Pointer(pParse)).FbJsonIsRCStr) == 0 && (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc > uint32(0) { (*TJsonParse)(unsafe.Pointer(pParse)).FzJson = _sqlite3RCStrRef(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf) (*TJsonParse)(unsafe.Pointer(pParse)).FnJson = int32((*TJsonString)(unsafe.Pointer(p)).FnUsed) (*TJsonParse)(unsafe.Pointer(pParse)).FbJsonIsRCStr = uint8(1) rc = _jsonCacheInsert(tls, ctx, pParse) if rc == int32(SQLITE_NOMEM) { Xsqlite3_result_error_nomem(tls, ctx) _jsonStringReset(tls, p) return } } Xsqlite3_result_text64(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx, _sqlite3RCStrRef(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf), (*TJsonString)(unsafe.Pointer(p)).FnUsed, __ccgo_fp(_sqlite3RCStrUnref), uint8(SQLITE_UTF8)) } else { Xsqlite3_result_error_nomem(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx) } } } } else { if int32((*TJsonString)(unsafe.Pointer(p)).FeErr)&int32(JSTRING_OOM) != 0 { Xsqlite3_result_error_nomem(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx) } else { if int32((*TJsonString)(unsafe.Pointer(p)).FeErr)&int32(JSTRING_MALFORMED) != 0 { Xsqlite3_result_error(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx, __ccgo_ts+24936, -int32(1)) } } } _jsonStringReset(tls, p) } /************************************************************************** ** Utility routines for dealing with JsonParse objects **************************************************************************/ // C documentation // // /* // ** Reclaim all memory allocated by a JsonParse object. But do not // ** delete the JsonParse object itself. // */ func _jsonParseReset(tls *libc.TLS, pParse uintptr) { _ = libc.Int32FromInt32(0) if (*TJsonParse)(unsafe.Pointer(pParse)).FbJsonIsRCStr != 0 { _sqlite3RCStrUnref(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FzJson) (*TJsonParse)(unsafe.Pointer(pParse)).FzJson = uintptr(0) (*TJsonParse)(unsafe.Pointer(pParse)).FnJson = 0 (*TJsonParse)(unsafe.Pointer(pParse)).FbJsonIsRCStr = uint8(0) } if (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc != 0 { _sqlite3DbFree(tls, (*TJsonParse)(unsafe.Pointer(pParse)).Fdb, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob) (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob = uintptr(0) (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob = uint32(0) (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc = uint32(0) } } // C documentation // // /* // ** Decrement the reference count on the JsonParse object. When the // ** count reaches zero, free the object. // */ func _jsonParseFree(tls *libc.TLS, pParse uintptr) { if pParse != 0 { if (*TJsonParse)(unsafe.Pointer(pParse)).FnJPRef > uint32(1) { (*TJsonParse)(unsafe.Pointer(pParse)).FnJPRef-- } else { _jsonParseReset(tls, pParse) _sqlite3DbFree(tls, (*TJsonParse)(unsafe.Pointer(pParse)).Fdb, pParse) } } } /************************************************************************** ** Utility routines for the JSON text parser **************************************************************************/ // C documentation // // /* // ** Translate a single byte of Hex into an integer. // ** This routine only gives a correct answer if h really is a valid hexadecimal // ** character: 0..9a..fA..F. But unlike sqlite3HexToInt(), it does not // ** assert() if the digit is not hex. // */ func _jsonHexToInt(tls *libc.TLS, h int32) (r Tu8) { h += int32(9) * (int32(1) & (h >> int32(6))) return uint8(h & libc.Int32FromInt32(0xf)) } // C documentation // // /* // ** Convert a 4-byte hex string into an integer // */ func _jsonHexToInt4(tls *libc.TLS, z uintptr) (r Tu32) { var v Tu32 _ = v v = uint32(int32(_jsonHexToInt(tls, int32(*(*uint8)(unsafe.Pointer(z)))))<aBlob so that it holds at least N bytes. // ** // ** Return the number of errors. // */ func _jsonBlobExpand(tls *libc.TLS, pParse uintptr, N Tu32) (r int32) { var aNew uintptr var t Tu32 _, _ = aNew, t _ = libc.Int32FromInt32(0) if (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc == uint32(0) { t = uint32(100) } else { t = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc * uint32(2) } if t < N { t = N + uint32(100) } aNew = _sqlite3DbRealloc(tls, (*TJsonParse)(unsafe.Pointer(pParse)).Fdb, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob, uint64(t)) if aNew == uintptr(0) { (*TJsonParse)(unsafe.Pointer(pParse)).Foom = uint8(1) return int32(1) } (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob = aNew (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc = t return 0 } // C documentation // // /* // ** If pParse->aBlob is not previously editable (because it is taken // ** from sqlite3_value_blob(), as indicated by the fact that // ** pParse->nBlobAlloc==0 and pParse->nBlob>0) then make it editable // ** by making a copy into space obtained from malloc. // ** // ** Return true on success. Return false on OOM. // */ func _jsonBlobMakeEditable(tls *libc.TLS, pParse uintptr, nExtra Tu32) (r int32) { var aOld uintptr var nSize Tu32 _, _ = aOld, nSize _ = libc.Int32FromInt32(0) if (*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0 { return 0 } if (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc > uint32(0) { return int32(1) } aOld = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob nSize = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob + nExtra (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob = uintptr(0) if _jsonBlobExpand(tls, pParse, nSize) != 0 { return 0 } _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob, aOld, (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob) return int32(1) } // C documentation // // /* Expand pParse->aBlob and append one bytes. // */ func _jsonBlobExpandAndAppendOneByte(tls *libc.TLS, pParse uintptr, c Tu8) { var v1 Tu32 var v2 uintptr _, _ = v1, v2 _jsonBlobExpand(tls, pParse, (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob+uint32(1)) if int32((*TJsonParse)(unsafe.Pointer(pParse)).Foom) == 0 { _ = libc.Int32FromInt32(0) v2 = pParse + 4 v1 = *(*Tu32)(unsafe.Pointer(v2)) *(*Tu32)(unsafe.Pointer(v2))++ *(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(v1))) = c } } // C documentation // // /* Append a single character. // */ func _jsonBlobAppendOneByte(tls *libc.TLS, pParse uintptr, c Tu8) { var v1 Tu32 var v2 uintptr _, _ = v1, v2 if (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob >= (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc { _jsonBlobExpandAndAppendOneByte(tls, pParse, c) } else { v2 = pParse + 4 v1 = *(*Tu32)(unsafe.Pointer(v2)) *(*Tu32)(unsafe.Pointer(v2))++ *(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(v1))) = c } } func _jsonBlobExpandAndAppendNode(tls *libc.TLS, pParse uintptr, eType Tu8, szPayload Tu32, aPayload uintptr) { if _jsonBlobExpand(tls, pParse, (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob+szPayload+uint32(9)) != 0 { return } _jsonBlobAppendNode(tls, pParse, eType, szPayload, aPayload) } // C documentation // // /* Append an node type byte together with the payload size and // ** possibly also the payload. // ** // ** If aPayload is not NULL, then it is a pointer to the payload which // ** is also appended. If aPayload is NULL, the pParse->aBlob[] array // ** is resized (if necessary) so that it is big enough to hold the // ** payload, but the payload is not appended and pParse->nBlob is left // ** pointing to where the first byte of payload will eventually be. // */ func _jsonBlobAppendNode(tls *libc.TLS, pParse uintptr, eType Tu8, szPayload Tu32, aPayload uintptr) { var a uintptr _ = a if (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob+szPayload+uint32(9) > (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc { _jsonBlobExpandAndAppendNode(tls, pParse, eType, szPayload, aPayload) return } _ = libc.Int32FromInt32(0) a = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob) if szPayload <= uint32(11) { *(*Tu8)(unsafe.Pointer(a)) = uint8(uint32(eType) | szPayload<> libc.Int32FromInt32(8) & uint32(0xff)) *(*Tu8)(unsafe.Pointer(a + 2)) = uint8(szPayload & uint32(0xff)) *(*Tu32)(unsafe.Pointer(pParse + 4)) += uint32(3) } else { *(*Tu8)(unsafe.Pointer(a)) = uint8(int32(eType) | int32(0xe0)) *(*Tu8)(unsafe.Pointer(a + 1)) = uint8(szPayload >> libc.Int32FromInt32(24) & uint32(0xff)) *(*Tu8)(unsafe.Pointer(a + 2)) = uint8(szPayload >> libc.Int32FromInt32(16) & uint32(0xff)) *(*Tu8)(unsafe.Pointer(a + 3)) = uint8(szPayload >> libc.Int32FromInt32(8) & uint32(0xff)) *(*Tu8)(unsafe.Pointer(a + 4)) = uint8(szPayload & uint32(0xff)) *(*Tu32)(unsafe.Pointer(pParse + 4)) += uint32(5) } } } if aPayload != 0 { *(*Tu32)(unsafe.Pointer(pParse + 4)) += szPayload libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-szPayload), aPayload, szPayload) } } // C documentation // // /* Change the payload size for the node at index i to be szPayload. // */ func _jsonBlobChangePayloadSize(tls *libc.TLS, pParse uintptr, i Tu32, szPayload Tu32) (r int32) { var a uintptr var delta int32 var nExtra, nNeeded, szType Tu8 var newSize Tu32 _, _, _, _, _, _ = a, delta, nExtra, nNeeded, newSize, szType if (*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0 { return 0 } a = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i) szType = uint8(int32(*(*Tu8)(unsafe.Pointer(a))) >> int32(4)) if int32(szType) <= int32(11) { nExtra = uint8(0) } else { if int32(szType) == int32(12) { nExtra = uint8(1) } else { if int32(szType) == int32(13) { nExtra = uint8(2) } else { nExtra = uint8(4) } } } if szPayload <= uint32(11) { nNeeded = uint8(0) } else { if szPayload <= uint32(0xff) { nNeeded = uint8(1) } else { if szPayload <= uint32(0xffff) { nNeeded = uint8(2) } else { nNeeded = uint8(4) } } } delta = int32(nNeeded) - int32(nExtra) if delta != 0 { newSize = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob + uint32(delta) if delta > 0 { if newSize > (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc && _jsonBlobExpand(tls, pParse, newSize) != 0 { return 0 /* OOM error. Error state recorded in pParse->oom. */ } a = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i) libc.Xmemmove(tls, a+uintptr(int32(1)+delta), a+1, (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-(i+uint32(1))) } else { libc.Xmemmove(tls, a+1, a+uintptr(int32(1)-delta), (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-(i+uint32(1)-uint32(delta))) } (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob = newSize } if int32(nNeeded) == 0 { *(*Tu8)(unsafe.Pointer(a)) = uint8(uint32(int32(*(*Tu8)(unsafe.Pointer(a)))&libc.Int32FromInt32(0x0f)) | szPayload<> libc.Int32FromInt32(8) & uint32(0xff)) *(*Tu8)(unsafe.Pointer(a + 2)) = uint8(szPayload & uint32(0xff)) } else { *(*Tu8)(unsafe.Pointer(a)) = uint8(int32(*(*Tu8)(unsafe.Pointer(a)))&int32(0x0f) | int32(0xe0)) *(*Tu8)(unsafe.Pointer(a + 1)) = uint8(szPayload >> libc.Int32FromInt32(24) & uint32(0xff)) *(*Tu8)(unsafe.Pointer(a + 2)) = uint8(szPayload >> libc.Int32FromInt32(16) & uint32(0xff)) *(*Tu8)(unsafe.Pointer(a + 3)) = uint8(szPayload >> libc.Int32FromInt32(8) & uint32(0xff)) *(*Tu8)(unsafe.Pointer(a + 4)) = uint8(szPayload & uint32(0xff)) } } } return delta } // C documentation // // /* // ** If z[0] is 'u' and is followed by exactly 4 hexadecimal character, // ** then set *pOp to JSONB_TEXTJ and return true. If not, do not make // ** any changes to *pOp and return false. // */ func _jsonIs4HexB(tls *libc.TLS, z uintptr, pOp uintptr) (r int32) { if int32(*(*uint8)(unsafe.Pointer(z))) != int32('u') { return 0 } if !(_jsonIs4Hex(tls, z+1) != 0) { return 0 } *(*int32)(unsafe.Pointer(pOp)) = int32(JSONB_TEXTJ) return int32(1) } // C documentation // // /* // ** Check a single element of the JSONB in pParse for validity. // ** // ** The element to be checked starts at offset i and must end at on the // ** last byte before iEnd. // ** // ** Return 0 if everything is correct. Return the 1-based byte offset of the // ** error if a problem is detected. (In other words, if the error is at offset // ** 0, return 1). // */ func _jsonbValidityCheck(tls *libc.TLS, pParse uintptr, i Tu32, iEnd Tu32, iDepth Tu32) (r Tu32) { bp := tls.Alloc(16) defer tls.Free(16) var cnt, j, k, n, sub, sub1, szC Tu32 var seen, x Tu8 var z uintptr var v1 uint32 var _ /* c at bp+4 */ Tu32 var _ /* sz at bp+0 */ Tu32 _, _, _, _, _, _, _, _, _, _, _ = cnt, j, k, n, seen, sub, sub1, szC, x, z, v1 if iDepth > uint32(JSON_MAX_DEPTH) { return i + uint32(1) } *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) n = _jsonbPayloadSize(tls, pParse, i, bp) if n == uint32(0) { return i + uint32(1) } /* Checked by caller */ if i+n+*(*Tu32)(unsafe.Pointer(bp)) != iEnd { return i + uint32(1) } /* Checked by caller */ z = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob x = uint8(int32(*(*Tu8)(unsafe.Pointer(z + uintptr(i)))) & int32(0x0f)) switch int32(x) { case JSONB_NULL: fallthrough case int32(JSONB_TRUE): fallthrough case int32(JSONB_FALSE): if n+*(*Tu32)(unsafe.Pointer(bp)) == uint32(1) { v1 = uint32(0) } else { v1 = i + uint32(1) } return v1 case int32(JSONB_INT): if *(*Tu32)(unsafe.Pointer(bp)) < uint32(1) { return i + uint32(1) } j = i + n if int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j)))) == int32('-') { j++ if *(*Tu32)(unsafe.Pointer(bp)) < uint32(2) { return i + uint32(1) } } k = i + n + *(*Tu32)(unsafe.Pointer(bp)) for j < k { if int32(_sqlite3CtypeMap[*(*Tu8)(unsafe.Pointer(z + uintptr(j)))])&int32(0x04) != 0 { j++ } else { return j + uint32(1) } } return uint32(0) case int32(JSONB_INT5): if *(*Tu32)(unsafe.Pointer(bp)) < uint32(3) { return i + uint32(1) } j = i + n if int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j)))) == int32('-') { if *(*Tu32)(unsafe.Pointer(bp)) < uint32(4) { return i + uint32(1) } j++ } if int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j)))) != int32('0') { return i + uint32(1) } if int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j+uint32(1))))) != int32('x') && int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j+uint32(1))))) != int32('X') { return j + uint32(2) } j += uint32(2) k = i + n + *(*Tu32)(unsafe.Pointer(bp)) for j < k { if int32(_sqlite3CtypeMap[*(*Tu8)(unsafe.Pointer(z + uintptr(j)))])&int32(0x08) != 0 { j++ } else { return j + uint32(1) } } return uint32(0) case int32(JSONB_FLOAT): fallthrough case int32(JSONB_FLOAT5): seen = uint8(0) /* 0: initial. 1: '.' seen 2: 'e' seen */ if *(*Tu32)(unsafe.Pointer(bp)) < uint32(2) { return i + uint32(1) } j = i + n k = j + *(*Tu32)(unsafe.Pointer(bp)) if int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j)))) == int32('-') { j++ if *(*Tu32)(unsafe.Pointer(bp)) < uint32(3) { return i + uint32(1) } } if int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j)))) == int32('.') { if int32(x) == int32(JSONB_FLOAT) { return j + uint32(1) } if !(int32(_sqlite3CtypeMap[*(*Tu8)(unsafe.Pointer(z + uintptr(j+uint32(1))))])&libc.Int32FromInt32(0x04) != 0) { return j + uint32(1) } j += uint32(2) seen = uint8(1) } else { if int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j)))) == int32('0') && int32(x) == int32(JSONB_FLOAT) { if j+uint32(3) > k { return j + uint32(1) } if int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j+uint32(1))))) != int32('.') && int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j+uint32(1))))) != int32('e') && int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j+uint32(1))))) != int32('E') { return j + uint32(1) } j++ } } for { if !(j < k) { break } if int32(_sqlite3CtypeMap[*(*Tu8)(unsafe.Pointer(z + uintptr(j)))])&int32(0x04) != 0 { goto _2 } if int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j)))) == int32('.') { if int32(seen) > 0 { return j + uint32(1) } if int32(x) == int32(JSONB_FLOAT) && (j == k-uint32(1) || !(int32(_sqlite3CtypeMap[*(*Tu8)(unsafe.Pointer(z + uintptr(j+uint32(1))))])&libc.Int32FromInt32(0x04) != 0)) { return j + uint32(1) } seen = uint8(1) goto _2 } if int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j)))) == int32('e') || int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j)))) == int32('E') { if int32(seen) == int32(2) { return j + uint32(1) } if j == k-uint32(1) { return j + uint32(1) } if int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j+uint32(1))))) == int32('+') || int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j+uint32(1))))) == int32('-') { j++ if j == k-uint32(1) { return j + uint32(1) } } seen = uint8(2) goto _2 } return j + uint32(1) goto _2 _2: ; j++ } if int32(seen) == 0 { return i + uint32(1) } return uint32(0) case int32(JSONB_TEXT): j = i + n k = j + *(*Tu32)(unsafe.Pointer(bp)) for j < k { if !(_jsonIsOk[*(*Tu8)(unsafe.Pointer(z + uintptr(j)))] != 0) && int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j)))) != int32('\'') { return j + uint32(1) } j++ } return uint32(0) case int32(JSONB_TEXTJ): fallthrough case int32(JSONB_TEXT5): j = i + n k = j + *(*Tu32)(unsafe.Pointer(bp)) for j < k { if !(_jsonIsOk[*(*Tu8)(unsafe.Pointer(z + uintptr(j)))] != 0) && int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j)))) != int32('\'') { if int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j)))) == int32('"') { if int32(x) == int32(JSONB_TEXTJ) { return j + uint32(1) } } else { if int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j)))) != int32('\\') || j+uint32(1) >= k { return j + uint32(1) } else { if libc.Xstrchr(tls, __ccgo_ts+25025, int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j+uint32(1)))))) != uintptr(0) { j++ } else { if int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j+uint32(1))))) == int32('u') { if j+uint32(5) >= k { return j + uint32(1) } if !(_jsonIs4Hex(tls, z+uintptr(j+uint32(2))) != 0) { return j + uint32(1) } j++ } else { if int32(x) != int32(JSONB_TEXT5) { return j + uint32(1) } else { *(*Tu32)(unsafe.Pointer(bp + 4)) = uint32(0) szC = _jsonUnescapeOneChar(tls, z+uintptr(j), k-j, bp+4) if *(*Tu32)(unsafe.Pointer(bp + 4)) == uint32(JSON_INVALID_CHAR) { return j + uint32(1) } j += szC - uint32(1) } } } } } } j++ } return uint32(0) case int32(JSONB_TEXTRAW): return uint32(0) case int32(JSONB_ARRAY): j = i + n k = j + *(*Tu32)(unsafe.Pointer(bp)) for j < k { *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) n = _jsonbPayloadSize(tls, pParse, j, bp) if n == uint32(0) { return j + uint32(1) } if j+n+*(*Tu32)(unsafe.Pointer(bp)) > k { return j + uint32(1) } sub = _jsonbValidityCheck(tls, pParse, j, j+n+*(*Tu32)(unsafe.Pointer(bp)), iDepth+uint32(1)) if sub != 0 { return sub } j += n + *(*Tu32)(unsafe.Pointer(bp)) } _ = libc.Int32FromInt32(0) return uint32(0) case int32(JSONB_OBJECT): cnt = uint32(0) j = i + n k = j + *(*Tu32)(unsafe.Pointer(bp)) for j < k { *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) n = _jsonbPayloadSize(tls, pParse, j, bp) if n == uint32(0) { return j + uint32(1) } if j+n+*(*Tu32)(unsafe.Pointer(bp)) > k { return j + uint32(1) } if cnt&uint32(1) == uint32(0) { x = uint8(int32(*(*Tu8)(unsafe.Pointer(z + uintptr(j)))) & int32(0x0f)) if int32(x) < int32(JSONB_TEXT) || int32(x) > int32(JSONB_TEXTRAW) { return j + uint32(1) } } sub1 = _jsonbValidityCheck(tls, pParse, j, j+n+*(*Tu32)(unsafe.Pointer(bp)), iDepth+uint32(1)) if sub1 != 0 { return sub1 } cnt++ j += n + *(*Tu32)(unsafe.Pointer(bp)) } _ = libc.Int32FromInt32(0) if cnt&uint32(1) != uint32(0) { return j + uint32(1) } return uint32(0) default: return i + uint32(1) } return r } // C documentation // // /* // ** Translate a single element of JSON text at pParse->zJson[i] into // ** its equivalent binary JSONB representation. Append the translation into // ** pParse->aBlob[] beginning at pParse->nBlob. The size of // ** pParse->aBlob[] is increased as necessary. // ** // ** Return the index of the first character past the end of the element parsed, // ** or one of the following special result codes: // ** // ** 0 End of input // ** -1 Syntax error or OOM // ** -2 '}' seen ** -3 ']' seen \___ For these returns, pParse->iErr is set to // ** -4 ',' seen / the index in zJson[] of the seen character // ** -5 ':' seen / // */ func _jsonTranslateTextToBlob(tls *libc.TLS, pParse uintptr, i Tu32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var c, cDelim uint8 var iBlob, iStart, iThis, j, k1, v46 Tu32 var k, nn, x, v48 int32 var opcode, seenE, t Tu8 var z, v41, v44 uintptr var v40, v43 Tu16 var _ /* op at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, cDelim, iBlob, iStart, iThis, j, k, k1, nn, opcode, seenE, t, x, z, v40, v41, v43, v44, v46, v48 z = (*TJsonParse)(unsafe.Pointer(pParse)).FzJson goto json_parse_restart json_parse_restart: ; switch int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) { case int32('{'): goto _1 case int32('['): goto _2 case int32('"'): goto _3 case int32('\''): goto _4 case int32('t'): goto _5 case int32('f'): goto _6 case int32('.'): goto _7 case int32('9'): goto _8 case int32('8'): goto _9 case int32('7'): goto _10 case int32('6'): goto _11 case int32('5'): goto _12 case int32('4'): goto _13 case int32('3'): goto _14 case int32('2'): goto _15 case int32('1'): goto _16 case int32('0'): goto _17 case int32('-'): goto _18 case int32('+'): goto _19 case int32('}'): goto _20 case int32(']'): goto _21 case int32(','): goto _22 case int32(':'): goto _23 case 0: goto _24 case int32(0x20): goto _25 case int32(0x0d): goto _26 case int32(0x0a): goto _27 case int32(0x09): goto _28 case int32(0xef): goto _29 case int32(0xe3): goto _30 case int32(0xe2): goto _31 case int32(0xe1): goto _32 case int32(0xc2): goto _33 case int32('/'): goto _34 case int32(0x0c): goto _35 case int32(0x0b): goto _36 case int32('n'): goto _37 default: goto _38 } goto _39 _1: ; /* Parse object */ iThis = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob _jsonBlobAppendNode(tls, pParse, uint8(JSONB_OBJECT), uint32((*TJsonParse)(unsafe.Pointer(pParse)).FnJson)-i, uintptr(0)) v41 = pParse + 32 *(*Tu16)(unsafe.Pointer(v41))++ v40 = *(*Tu16)(unsafe.Pointer(v41)) if int32(v40) > int32(JSON_MAX_DEPTH) { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i return -int32(1) } iStart = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob j = i + uint32(1) for { iBlob = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob x = _jsonTranslateTextToBlob(tls, pParse, j) if x <= 0 { if x == -int32(2) { j = (*TJsonParse)(unsafe.Pointer(pParse)).FiErr if (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob != iStart { (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) } break } j += uint32(_json5Whitespace(tls, z+uintptr(j))) *(*int32)(unsafe.Pointer(bp)) = int32(JSONB_TEXT) if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(j)))])&int32(0x42) != 0 || int32(*(*uint8)(unsafe.Pointer(z + uintptr(j)))) == int32('\\') && _jsonIs4HexB(tls, z+uintptr(j+uint32(1)), bp) != 0 { k = int32(j + uint32(1)) for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(k)))])&int32(0x46) != 0 && _json5Whitespace(tls, z+uintptr(k)) == 0 || int32(*(*uint8)(unsafe.Pointer(z + uintptr(k)))) == int32('\\') && _jsonIs4HexB(tls, z+uintptr(k+int32(1)), bp) != 0 { k++ } _ = libc.Int32FromInt32(0) _jsonBlobAppendNode(tls, pParse, uint8(*(*int32)(unsafe.Pointer(bp))), uint32(k)-j, z+uintptr(j)) (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) x = k } else { if x != -int32(1) { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j } return -int32(1) } } if (*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0 { return -int32(1) } t = uint8(int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(iBlob)))) & int32(0x0f)) if int32(t) < int32(JSONB_TEXT) || int32(t) > int32(JSONB_TEXTRAW) { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j return -int32(1) } j = uint32(x) if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j)))) == int32(':') { j++ } else { if _jsonIsSpace[*(*uint8)(unsafe.Pointer(z + uintptr(j)))] != 0 { /* strspn() is not helpful here */ for cond := true; cond; cond = _jsonIsSpace[*(*uint8)(unsafe.Pointer(z + uintptr(j)))] != 0 { j++ } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j)))) == int32(':') { j++ goto parse_object_value } } x = _jsonTranslateTextToBlob(tls, pParse, j) if x != -int32(5) { if x != -int32(1) { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j } return -int32(1) } j = (*TJsonParse)(unsafe.Pointer(pParse)).FiErr + uint32(1) } goto parse_object_value parse_object_value: ; x = _jsonTranslateTextToBlob(tls, pParse, j) if x <= 0 { if x != -int32(1) { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j } return -int32(1) } j = uint32(x) if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j)))) == int32(',') { goto _42 } else { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j)))) == int32('}') { break } else { if _jsonIsSpace[*(*uint8)(unsafe.Pointer(z + uintptr(j)))] != 0 { j += uint32(1) + libc.Xstrspn(tls, z+uintptr(j+uint32(1)), uintptr(unsafe.Pointer(&_jsonSpaces))) if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j)))) == int32(',') { goto _42 } else { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j)))) == int32('}') { break } } } x = _jsonTranslateTextToBlob(tls, pParse, j) if x == -int32(4) { j = (*TJsonParse)(unsafe.Pointer(pParse)).FiErr goto _42 } if x == -int32(2) { j = (*TJsonParse)(unsafe.Pointer(pParse)).FiErr break } } } (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j return -int32(1) goto _42 _42: ; j++ } _jsonBlobChangePayloadSize(tls, pParse, iThis, (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-iStart) (*TJsonParse)(unsafe.Pointer(pParse)).FiDepth-- return int32(j + uint32(1)) _2: ; /* Parse array */ iThis = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob _ = libc.Int32FromInt32(0) _jsonBlobAppendNode(tls, pParse, uint8(JSONB_ARRAY), uint32((*TJsonParse)(unsafe.Pointer(pParse)).FnJson)-i, uintptr(0)) iStart = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob if (*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0 { return -int32(1) } v44 = pParse + 32 *(*Tu16)(unsafe.Pointer(v44))++ v43 = *(*Tu16)(unsafe.Pointer(v44)) if int32(v43) > int32(JSON_MAX_DEPTH) { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i return -int32(1) } j = i + uint32(1) for { x = _jsonTranslateTextToBlob(tls, pParse, j) if x <= 0 { if x == -int32(3) { j = (*TJsonParse)(unsafe.Pointer(pParse)).FiErr if (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob != iStart { (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) } break } if x != -int32(1) { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j } return -int32(1) } j = uint32(x) if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j)))) == int32(',') { goto _45 } else { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j)))) == int32(']') { break } else { if _jsonIsSpace[*(*uint8)(unsafe.Pointer(z + uintptr(j)))] != 0 { j += uint32(1) + libc.Xstrspn(tls, z+uintptr(j+uint32(1)), uintptr(unsafe.Pointer(&_jsonSpaces))) if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j)))) == int32(',') { goto _45 } else { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j)))) == int32(']') { break } } } x = _jsonTranslateTextToBlob(tls, pParse, j) if x == -int32(4) { j = (*TJsonParse)(unsafe.Pointer(pParse)).FiErr goto _45 } if x == -int32(3) { j = (*TJsonParse)(unsafe.Pointer(pParse)).FiErr break } } } (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j return -int32(1) goto _45 _45: ; j++ } _jsonBlobChangePayloadSize(tls, pParse, iThis, (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-iStart) (*TJsonParse)(unsafe.Pointer(pParse)).FiDepth-- return int32(j + uint32(1)) _4: ; (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) opcode = uint8(JSONB_TEXT) goto parse_string _3: ; /* Parse string */ opcode = uint8(JSONB_TEXT) goto parse_string parse_string: ; cDelim = *(*uint8)(unsafe.Pointer(z + uintptr(i))) j = i + uint32(1) for int32(1) != 0 { if _jsonIsOk[*(*uint8)(unsafe.Pointer(z + uintptr(j)))] != 0 { if !(_jsonIsOk[*(*uint8)(unsafe.Pointer(z + uintptr(j+uint32(1))))] != 0) { j += uint32(1) } else { if !(_jsonIsOk[*(*uint8)(unsafe.Pointer(z + uintptr(j+uint32(2))))] != 0) { j += uint32(2) } else { j += uint32(3) continue } } } c = *(*uint8)(unsafe.Pointer(z + uintptr(j))) if int32(c) == int32(cDelim) { break } else { if int32(c) == int32('\\') { j++ v46 = j c = *(*uint8)(unsafe.Pointer(z + uintptr(v46))) if int32(c) == int32('"') || int32(c) == int32('\\') || int32(c) == int32('/') || int32(c) == int32('b') || int32(c) == int32('f') || int32(c) == int32('n') || int32(c) == int32('r') || int32(c) == int32('t') || int32(c) == int32('u') && _jsonIs4Hex(tls, z+uintptr(j+uint32(1))) != 0 { if int32(opcode) == int32(JSONB_TEXT) { opcode = uint8(JSONB_TEXTJ) } } else { if int32(c) == int32('\'') || int32(c) == int32('0') || int32(c) == int32('v') || int32(c) == int32('\n') || int32(0xe2) == int32(c) && int32(0x80) == int32(*(*uint8)(unsafe.Pointer(z + uintptr(j+uint32(1))))) && (int32(0xa8) == int32(*(*uint8)(unsafe.Pointer(z + uintptr(j+uint32(2))))) || int32(0xa9) == int32(*(*uint8)(unsafe.Pointer(z + uintptr(j+uint32(2)))))) || int32(c) == int32('x') && _jsonIs2Hex(tls, z+uintptr(j+uint32(1))) != 0 { opcode = uint8(JSONB_TEXT5) (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) } else { if int32(c) == int32('\r') { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j+uint32(1))))) == int32('\n') { j++ } opcode = uint8(JSONB_TEXT5) (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) } else { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j return -int32(1) } } } } else { if int32(c) <= int32(0x1f) { /* Control characters are not allowed in strings */ (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j return -int32(1) } else { if int32(c) == int32('"') { opcode = uint8(JSONB_TEXT5) } } } } j++ } _jsonBlobAppendNode(tls, pParse, opcode, j-uint32(1)-i, z+uintptr(i+uint32(1))) return int32(j + uint32(1)) _5: ; if libc.Xstrncmp(tls, z+uintptr(i), __ccgo_ts+7886, uint32(4)) == 0 && !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(4))))])&libc.Int32FromInt32(0x06) != 0) { _jsonBlobAppendOneByte(tls, pParse, uint8(JSONB_TRUE)) return int32(i + uint32(4)) } (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i return -int32(1) _6: ; if libc.Xstrncmp(tls, z+uintptr(i), __ccgo_ts+7891, uint32(5)) == 0 && !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(5))))])&libc.Int32FromInt32(0x06) != 0) { _jsonBlobAppendOneByte(tls, pParse, uint8(JSONB_FALSE)) return int32(i + uint32(5)) } (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i return -int32(1) _19: ; (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) t = uint8(0x00) /* Bit 0x01: JSON5. Bit 0x02: FLOAT */ goto parse_number _7: ; if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(1))))])&int32(0x04) != 0 { (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) t = uint8(0x03) /* Bit 0x01: JSON5. Bit 0x02: FLOAT */ seenE = uint8(0) goto parse_number_2 } (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i return -int32(1) _18: ; _17: ; _16: ; _15: ; _14: ; _13: ; _12: ; _11: ; _10: ; _9: ; _8: ; /* Parse number */ t = uint8(0x00) /* Bit 0x01: JSON5. Bit 0x02: FLOAT */ goto parse_number parse_number: ; seenE = uint8(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) c = *(*uint8)(unsafe.Pointer(z + uintptr(i))) if int32(c) <= int32('0') { if int32(c) == int32('0') { if (int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('x') || int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('X')) && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(2))))])&int32(0x08) != 0 { _ = libc.Int32FromInt32(0) (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) t = uint8(0x01) j = i + uint32(3) for { if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(j)))])&int32(0x08) != 0) { break } goto _47 _47: ; j++ } goto parse_number_finish } else { if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(1))))])&int32(0x04) != 0 { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i + uint32(1) return -int32(1) } } } else { if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(1))))])&libc.Int32FromInt32(0x04) != 0) { /* JSON5 allows for "+Infinity" and "-Infinity" using exactly ** that case. SQLite also allows these in any case and it allows ** "+inf" and "-inf". */ if (int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('I') || int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('i')) && Xsqlite3_strnicmp(tls, z+uintptr(i+uint32(1)), __ccgo_ts+24951, int32(3)) == 0 { (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) == int32('-') { _jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint32(6), __ccgo_ts+25034) } else { _jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint32(5), __ccgo_ts+25041) } if Xsqlite3_strnicmp(tls, z+uintptr(i+uint32(4)), __ccgo_ts+25047, int32(5)) == 0 { v48 = int32(9) } else { v48 = int32(4) } return int32(i + uint32(v48)) } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('.') { (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) t = Tu8(int32(t) | libc.Int32FromInt32(0x01)) goto parse_number_2 } (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i return -int32(1) } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('0') { if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(2))))])&int32(0x04) != 0 { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i + uint32(1) return -int32(1) } else { if (int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(2))))) == int32('x') || int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(2))))) == int32('X')) && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(3))))])&int32(0x08) != 0 { (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) t = Tu8(int32(t) | libc.Int32FromInt32(0x01)) j = i + uint32(4) for { if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(j)))])&int32(0x08) != 0) { break } goto _49 _49: ; j++ } goto parse_number_finish } } } } } goto parse_number_2 parse_number_2: ; j = i + uint32(1) for { c = *(*uint8)(unsafe.Pointer(z + uintptr(j))) if int32(_sqlite3CtypeMap[c])&int32(0x04) != 0 { goto _50 } if int32(c) == int32('.') { if int32(t)&int32(0x02) != 0 { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j return -int32(1) } t = Tu8(int32(t) | libc.Int32FromInt32(0x02)) goto _50 } if int32(c) == int32('e') || int32(c) == int32('E') { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j-uint32(1))))) < int32('0') { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j-uint32(1))))) == int32('.') && j-uint32(2) >= i && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(j-uint32(2))))])&int32(0x04) != 0 { (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) t = Tu8(int32(t) | libc.Int32FromInt32(0x01)) } else { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j return -int32(1) } } if seenE != 0 { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j return -int32(1) } t = Tu8(int32(t) | libc.Int32FromInt32(0x02)) seenE = uint8(1) c = *(*uint8)(unsafe.Pointer(z + uintptr(j+uint32(1)))) if int32(c) == int32('+') || int32(c) == int32('-') { j++ c = *(*uint8)(unsafe.Pointer(z + uintptr(j+uint32(1)))) } if int32(c) < int32('0') || int32(c) > int32('9') { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j return -int32(1) } goto _50 } break goto _50 _50: ; j++ } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j-uint32(1))))) < int32('0') { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j-uint32(1))))) == int32('.') && j-uint32(2) >= i && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(j-uint32(2))))])&int32(0x04) != 0 { (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) t = Tu8(int32(t) | libc.Int32FromInt32(0x01)) } else { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j return -int32(1) } } goto parse_number_finish parse_number_finish: ; _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) == int32('+') { i++ } _jsonBlobAppendNode(tls, pParse, uint8(int32(JSONB_INT)+int32(t)), j-i, z+uintptr(i)) return int32(j) _20: ; (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i return -int32(2) /* End of {...} */ _21: ; (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i return -int32(3) /* End of [...] */ _22: ; (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i return -int32(4) /* List separator */ _23: ; (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i return -int32(5) /* Object label/value separator */ _24: ; return 0 /* End of file */ _28: ; _27: ; _26: ; _25: ; i += uint32(1) + libc.Xstrspn(tls, z+uintptr(i+uint32(1)), uintptr(unsafe.Pointer(&_jsonSpaces))) goto json_parse_restart _36: ; _35: ; _34: ; _33: ; _32: ; _31: ; _30: ; _29: ; j = uint32(_json5Whitespace(tls, z+uintptr(i))) if j > uint32(0) { i += j (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) goto json_parse_restart } (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i return -int32(1) _37: ; if libc.Xstrncmp(tls, z+uintptr(i), __ccgo_ts+1634, uint32(4)) == 0 && !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(4))))])&libc.Int32FromInt32(0x06) != 0) { _jsonBlobAppendOneByte(tls, pParse, uint8(JSONB_NULL)) return int32(i + uint32(4)) } /* fall-through into the default case that checks for NaN */ _38: ; c = *(*uint8)(unsafe.Pointer(z + uintptr(i))) k1 = uint32(0) for { if !(k1 < libc.Uint32FromInt64(80)/libc.Uint32FromInt64(16)) { break } if int32(c) != int32(_aNanInfName[k1].Fc1) && int32(c) != int32(_aNanInfName[k1].Fc2) { goto _51 } nn = int32(_aNanInfName[k1].Fn) if Xsqlite3_strnicmp(tls, z+uintptr(i), _aNanInfName[k1].FzMatch, nn) != 0 { goto _51 } if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(nn))))])&int32(0x06) != 0 { goto _51 } if int32(_aNanInfName[k1].FeType) == int32(JSONB_FLOAT) { _jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint32(5), __ccgo_ts+25041) } else { _jsonBlobAppendOneByte(tls, pParse, uint8(JSONB_NULL)) } (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) return int32(i + uint32(nn)) goto _51 _51: ; k1++ } (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i return -int32(1) /* Syntax error */ _39: ; /* End switch(z[i]) */ return r } // C documentation // // /* // ** Parse a complete JSON string. Return 0 on success or non-zero if there // ** are any errors. If an error occurs, free all memory held by pParse, // ** but not pParse itself. // ** // ** pParse must be initialized to an empty parse object prior to calling // ** this routine. // */ func _jsonConvertTextToBlob(tls *libc.TLS, pParse uintptr, pCtx uintptr) (r int32) { var i int32 var zJson uintptr _, _ = i, zJson zJson = (*TJsonParse)(unsafe.Pointer(pParse)).FzJson i = _jsonTranslateTextToBlob(tls, pParse, uint32(0)) if (*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0 { i = -int32(1) } if i > 0 { for _jsonIsSpace[*(*uint8)(unsafe.Pointer(zJson + uintptr(i)))] != 0 { i++ } if *(*uint8)(unsafe.Pointer(zJson + uintptr(i))) != 0 { i += _json5Whitespace(tls, zJson+uintptr(i)) if *(*uint8)(unsafe.Pointer(zJson + uintptr(i))) != 0 { if pCtx != 0 { Xsqlite3_result_error(tls, pCtx, __ccgo_ts+24936, -int32(1)) } _jsonParseReset(tls, pParse) return int32(1) } (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) } } if i <= 0 { if pCtx != uintptr(0) { if (*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0 { Xsqlite3_result_error_nomem(tls, pCtx) } else { Xsqlite3_result_error(tls, pCtx, __ccgo_ts+24936, -int32(1)) } } _jsonParseReset(tls, pParse) return int32(1) } return 0 } // C documentation // // /* // ** The input string pStr is a well-formed JSON text string. Convert // ** this into the JSONB format and make it the return value of the // ** SQL function. // */ func _jsonReturnStringAsBlob(tls *libc.TLS, pStr uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var _ /* px at bp+0 */ TJsonParse libc.Xmemset(tls, bp, 0, uint32(56)) _jsonStringTerminate(tls, pStr) if (*TJsonString)(unsafe.Pointer(pStr)).FeErr != 0 { Xsqlite3_result_error_nomem(tls, (*TJsonString)(unsafe.Pointer(pStr)).FpCtx) return } (*(*TJsonParse)(unsafe.Pointer(bp))).FzJson = (*TJsonString)(unsafe.Pointer(pStr)).FzBuf (*(*TJsonParse)(unsafe.Pointer(bp))).FnJson = int32((*TJsonString)(unsafe.Pointer(pStr)).FnUsed) (*(*TJsonParse)(unsafe.Pointer(bp))).Fdb = Xsqlite3_context_db_handle(tls, (*TJsonString)(unsafe.Pointer(pStr)).FpCtx) _jsonTranslateTextToBlob(tls, bp, uint32(0)) if (*(*TJsonParse)(unsafe.Pointer(bp))).Foom != 0 { _sqlite3DbFree(tls, (*(*TJsonParse)(unsafe.Pointer(bp))).Fdb, (*(*TJsonParse)(unsafe.Pointer(bp))).FaBlob) Xsqlite3_result_error_nomem(tls, (*TJsonString)(unsafe.Pointer(pStr)).FpCtx) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) Xsqlite3_result_blob(tls, (*TJsonString)(unsafe.Pointer(pStr)).FpCtx, (*(*TJsonParse)(unsafe.Pointer(bp))).FaBlob, int32((*(*TJsonParse)(unsafe.Pointer(bp))).FnBlob), __ccgo_fp(_sqlite3OomClear)) } } // C documentation // // /* The byte at index i is a node type-code. This routine // ** determines the payload size for that node and writes that // ** payload size in to *pSz. It returns the offset from i to the // ** beginning of the payload. Return 0 on error. // */ func _jsonbPayloadSize(tls *libc.TLS, pParse uintptr, i Tu32, pSz uintptr) (r Tu32) { var n, sz Tu32 var x Tu8 _, _, _ = n, sz, x if i > (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob { *(*Tu32)(unsafe.Pointer(pSz)) = uint32(0) return uint32(0) } x = uint8(int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i)))) >> int32(4)) if int32(x) <= int32(11) { sz = uint32(x) n = uint32(1) } else { if int32(x) == int32(12) { if i+uint32(1) >= (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob { *(*Tu32)(unsafe.Pointer(pSz)) = uint32(0) return uint32(0) } sz = uint32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+uint32(1))))) n = uint32(2) } else { if int32(x) == int32(13) { if i+uint32(2) >= (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob { *(*Tu32)(unsafe.Pointer(pSz)) = uint32(0) return uint32(0) } sz = uint32(int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+uint32(1)))))<= (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob { *(*Tu32)(unsafe.Pointer(pSz)) = uint32(0) return uint32(0) } sz = uint32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+uint32(1)))))<= (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob || int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+uint32(1))))) != 0 || int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+uint32(2))))) != 0 || int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+uint32(3))))) != 0 || int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+uint32(4))))) != 0 { *(*Tu32)(unsafe.Pointer(pSz)) = uint32(0) return uint32(0) } sz = uint32(int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+uint32(5)))))< int64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob) && int64(i)+int64(sz)+int64(n) > int64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-uint32((*TJsonParse)(unsafe.Pointer(pParse)).Fdelta)) { sz = uint32(0) n = uint32(0) } *(*Tu32)(unsafe.Pointer(pSz)) = sz return n } // C documentation // // /* // ** Translate the binary JSONB representation of JSON beginning at // ** pParse->aBlob[i] into a JSON text string. Append the JSON // ** text onto the end of pOut. Return the index in pParse->aBlob[] // ** of the first byte past the end of the element that is translated. // ** // ** If an error is detected in the BLOB input, the pOut->eErr flag // ** might get set to JSTRING_MALFORMED. But not all BLOB input errors // ** are detected. So a malformed JSONB input might either result // ** in an error, or in incorrect JSON. // ** // ** The pOut->eErr JSTRING_OOM flag is set on a OOM. // */ func _jsonTranslateBlobToText(tls *libc.TLS, pParse uintptr, i Tu32, pOut uintptr) (r Tu32) { bp := tls.Alloc(32) defer tls.Free(32) var bOverflow, x, v26, v27 int32 var iEnd, j, k, k1, k2, n, sz2 Tu32 var u Tsqlite3_uint64 var zIn, zIn1, zIn2, v19, p1, p18, p22, p23, p24, p25, p28, p29 uintptr var _ /* sz at bp+0 */ Tu32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bOverflow, iEnd, j, k, k1, k2, n, sz2, u, x, zIn, zIn1, zIn2, v19, v26, v27, p1, p18, p22, p23, p24, p25, p28, p29 n = _jsonbPayloadSize(tls, pParse, i, bp) if n == uint32(0) { p1 = pOut + 25 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | libc.Int32FromInt32(JSTRING_MALFORMED)) return (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob + uint32(1) } switch int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i)))) & libc.Int32FromInt32(0x0f) { case JSONB_NULL: goto _2 case int32(JSONB_TRUE): goto _3 case int32(JSONB_FALSE): goto _4 case int32(JSONB_FLOAT): goto _5 case int32(JSONB_INT): goto _6 case int32(JSONB_INT5): goto _7 case int32(JSONB_FLOAT5): goto _8 case int32(JSONB_TEXTJ): goto _9 case int32(JSONB_TEXT): goto _10 case int32(JSONB_TEXT5): goto _11 case int32(JSONB_TEXTRAW): goto _12 case int32(JSONB_ARRAY): goto _13 case int32(JSONB_OBJECT): goto _14 default: goto _15 } goto _16 _2: ; _jsonAppendRawNZ(tls, pOut, __ccgo_ts+1634, uint32(4)) return i + uint32(1) _3: ; _jsonAppendRawNZ(tls, pOut, __ccgo_ts+7886, uint32(4)) return i + uint32(1) _4: ; _jsonAppendRawNZ(tls, pOut, __ccgo_ts+7891, uint32(5)) return i + uint32(1) _6: ; _5: ; if *(*Tu32)(unsafe.Pointer(bp)) == uint32(0) { goto malformed_jsonb } _jsonAppendRaw(tls, pOut, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i+n), *(*Tu32)(unsafe.Pointer(bp))) goto _16 _7: ; /* Integer literal in hexadecimal notation */ k = uint32(2) u = uint64(0) zIn = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+n) bOverflow = 0 if *(*Tu32)(unsafe.Pointer(bp)) == uint32(0) { goto malformed_jsonb } if int32(*(*uint8)(unsafe.Pointer(zIn))) == int32('-') { _jsonAppendChar(tls, pOut, uint8('-')) k++ } else { if int32(*(*uint8)(unsafe.Pointer(zIn))) == int32('+') { k++ } } for { if !(k < *(*Tu32)(unsafe.Pointer(bp))) { break } if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zIn + uintptr(k)))])&libc.Int32FromInt32(0x08) != 0) { p18 = pOut + 25 *(*Tu8)(unsafe.Pointer(p18)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p18))) | libc.Int32FromInt32(JSTRING_MALFORMED)) break } else { if u>>libc.Int32FromInt32(60) != uint64(0) { bOverflow = int32(1) } else { u = u*uint64(16) + uint64(_sqlite3HexToInt(tls, int32(*(*uint8)(unsafe.Pointer(zIn + uintptr(k)))))) } } goto _17 _17: ; k++ } if bOverflow != 0 { v19 = __ccgo_ts + 24955 } else { v19 = __ccgo_ts + 11753 } _jsonPrintf(tls, int32(100), pOut, v19, libc.VaList(bp+16, u)) goto _16 _8: ; /* Float literal missing digits beside "." */ k1 = uint32(0) zIn1 = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+n) if *(*Tu32)(unsafe.Pointer(bp)) == uint32(0) { goto malformed_jsonb } if int32(*(*uint8)(unsafe.Pointer(zIn1))) == int32('-') { _jsonAppendChar(tls, pOut, uint8('-')) k1++ } if int32(*(*uint8)(unsafe.Pointer(zIn1 + uintptr(k1)))) == int32('.') { _jsonAppendChar(tls, pOut, uint8('0')) } for { if !(k1 < *(*Tu32)(unsafe.Pointer(bp))) { break } _jsonAppendChar(tls, pOut, *(*uint8)(unsafe.Pointer(zIn1 + uintptr(k1)))) if int32(*(*uint8)(unsafe.Pointer(zIn1 + uintptr(k1)))) == int32('.') && (k1+uint32(1) == *(*Tu32)(unsafe.Pointer(bp)) || !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zIn1 + uintptr(k1+uint32(1))))])&libc.Int32FromInt32(0x04) != 0)) { _jsonAppendChar(tls, pOut, uint8('0')) } goto _20 _20: ; k1++ } goto _16 _10: ; _9: ; _jsonAppendChar(tls, pOut, uint8('"')) _jsonAppendRaw(tls, pOut, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i+n), *(*Tu32)(unsafe.Pointer(bp))) _jsonAppendChar(tls, pOut, uint8('"')) goto _16 _11: ; sz2 = *(*Tu32)(unsafe.Pointer(bp)) zIn2 = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+n) _jsonAppendChar(tls, pOut, uint8('"')) for sz2 > uint32(0) { k2 = uint32(0) for { if !(k2 < sz2 && int32(*(*uint8)(unsafe.Pointer(zIn2 + uintptr(k2)))) != int32('\\') && int32(*(*uint8)(unsafe.Pointer(zIn2 + uintptr(k2)))) != int32('"')) { break } goto _21 _21: ; k2++ } if k2 > uint32(0) { _jsonAppendRawNZ(tls, pOut, zIn2, k2) if k2 >= sz2 { break } zIn2 += uintptr(k2) sz2 -= k2 } if int32(*(*uint8)(unsafe.Pointer(zIn2))) == int32('"') { _jsonAppendRawNZ(tls, pOut, __ccgo_ts+25053, uint32(2)) zIn2++ sz2-- continue } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if sz2 < uint32(2) { p22 = pOut + 25 *(*Tu8)(unsafe.Pointer(p22)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p22))) | libc.Int32FromInt32(JSTRING_MALFORMED)) break } switch int32(*(*uint8)(unsafe.Pointer(zIn2 + 1))) { case int32('\''): _jsonAppendChar(tls, pOut, uint8('\'')) case int32('v'): _jsonAppendRawNZ(tls, pOut, __ccgo_ts+25056, uint32(6)) case int32('x'): if sz2 < uint32(4) { p23 = pOut + 25 *(*Tu8)(unsafe.Pointer(p23)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p23))) | libc.Int32FromInt32(JSTRING_MALFORMED)) sz2 = uint32(2) break } _jsonAppendRawNZ(tls, pOut, __ccgo_ts+25063, uint32(4)) _jsonAppendRawNZ(tls, pOut, zIn2+2, uint32(2)) zIn2 += uintptr(2) sz2 -= uint32(2) case int32('0'): _jsonAppendRawNZ(tls, pOut, __ccgo_ts+25068, uint32(6)) case int32('\r'): if sz2 > uint32(2) && int32(*(*uint8)(unsafe.Pointer(zIn2 + 2))) == int32('\n') { zIn2++ sz2-- } case int32('\n'): case int32(0xe2): /* '\' followed by either U+2028 or U+2029 is ignored as ** whitespace. Not that in UTF8, U+2028 is 0xe2 0x80 0x29. ** U+2029 is the same except for the last byte */ if sz2 < uint32(4) || int32(0x80) != int32(*(*uint8)(unsafe.Pointer(zIn2 + 2))) || int32(0xa8) != int32(*(*uint8)(unsafe.Pointer(zIn2 + 3))) && int32(0xa9) != int32(*(*uint8)(unsafe.Pointer(zIn2 + 3))) { p24 = pOut + 25 *(*Tu8)(unsafe.Pointer(p24)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p24))) | libc.Int32FromInt32(JSTRING_MALFORMED)) sz2 = uint32(2) break } zIn2 += uintptr(2) sz2 -= uint32(2) default: _jsonAppendRawNZ(tls, pOut, zIn2, uint32(2)) break } _ = libc.Int32FromInt32(0) zIn2 += uintptr(2) sz2 -= uint32(2) } _jsonAppendChar(tls, pOut, uint8('"')) goto _16 _12: ; _jsonAppendString(tls, pOut, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i+n), *(*Tu32)(unsafe.Pointer(bp))) goto _16 _13: ; _jsonAppendChar(tls, pOut, uint8('[')) j = i + n iEnd = j + *(*Tu32)(unsafe.Pointer(bp)) for j < iEnd && int32((*TJsonString)(unsafe.Pointer(pOut)).FeErr) == 0 { j = _jsonTranslateBlobToText(tls, pParse, j, pOut) _jsonAppendChar(tls, pOut, uint8(',')) } if j > iEnd { p25 = pOut + 25 *(*Tu8)(unsafe.Pointer(p25)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p25))) | libc.Int32FromInt32(JSTRING_MALFORMED)) } if *(*Tu32)(unsafe.Pointer(bp)) > uint32(0) { _jsonStringTrimOneChar(tls, pOut) } _jsonAppendChar(tls, pOut, uint8(']')) goto _16 _14: ; x = 0 _jsonAppendChar(tls, pOut, uint8('{')) j = i + n iEnd = j + *(*Tu32)(unsafe.Pointer(bp)) for j < iEnd && int32((*TJsonString)(unsafe.Pointer(pOut)).FeErr) == 0 { j = _jsonTranslateBlobToText(tls, pParse, j, pOut) v27 = x x++ if v27&int32(1) != 0 { v26 = int32(',') } else { v26 = int32(':') } _jsonAppendChar(tls, pOut, uint8(v26)) } if x&int32(1) != 0 || j > iEnd { p28 = pOut + 25 *(*Tu8)(unsafe.Pointer(p28)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p28))) | libc.Int32FromInt32(JSTRING_MALFORMED)) } if *(*Tu32)(unsafe.Pointer(bp)) > uint32(0) { _jsonStringTrimOneChar(tls, pOut) } _jsonAppendChar(tls, pOut, uint8('}')) goto _16 _15: ; goto malformed_jsonb malformed_jsonb: ; p29 = pOut + 25 *(*Tu8)(unsafe.Pointer(p29)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p29))) | libc.Int32FromInt32(JSTRING_MALFORMED)) goto _16 _16: ; return i + n + *(*Tu32)(unsafe.Pointer(bp)) } // C documentation // // /* Return true if the input pJson // ** // ** For performance reasons, this routine does not do a detailed check of the // ** input BLOB to ensure that it is well-formed. Hence, false positives are // ** possible. False negatives should never occur, however. // */ func _jsonFuncArgMightBeBinary(tls *libc.TLS, pJson uintptr) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var aBlob uintptr var n Tu32 var nBlob int32 var _ /* s at bp+4 */ TJsonParse var _ /* sz at bp+0 */ Tu32 _, _, _ = aBlob, n, nBlob if Xsqlite3_value_type(tls, pJson) != int32(SQLITE_BLOB) { return 0 } aBlob = Xsqlite3_value_blob(tls, pJson) nBlob = Xsqlite3_value_bytes(tls, pJson) if nBlob < int32(1) { return 0 } if aBlob == uintptr(0) || int32(*(*Tu8)(unsafe.Pointer(aBlob)))&int32(0x0f) > int32(JSONB_OBJECT) { return 0 } libc.Xmemset(tls, bp+4, 0, uint32(56)) (*(*TJsonParse)(unsafe.Pointer(bp + 4))).FaBlob = aBlob (*(*TJsonParse)(unsafe.Pointer(bp + 4))).FnBlob = uint32(nBlob) n = _jsonbPayloadSize(tls, bp+4, uint32(0), bp) if n == uint32(0) { return 0 } if *(*Tu32)(unsafe.Pointer(bp))+n != uint32(nBlob) { return 0 } if int32(*(*Tu8)(unsafe.Pointer(aBlob)))&int32(0x0f) <= int32(JSONB_FALSE) && *(*Tu32)(unsafe.Pointer(bp)) > uint32(0) { return 0 } return libc.BoolInt32(*(*Tu32)(unsafe.Pointer(bp))+n == uint32(nBlob)) } // C documentation // // /* // ** Given that a JSONB_ARRAY object starts at offset i, return // ** the number of entries in that array. // */ func _jsonbArrayCount(tls *libc.TLS, pParse uintptr, iRoot Tu32) (r Tu32) { bp := tls.Alloc(16) defer tls.Free(16) var i, iEnd, k, n Tu32 var _ /* sz at bp+0 */ Tu32 _, _, _, _ = i, iEnd, k, n k = uint32(0) n = _jsonbPayloadSize(tls, pParse, iRoot, bp) iEnd = iRoot + n + *(*Tu32)(unsafe.Pointer(bp)) i = iRoot + n for { if !(n > uint32(0) && i < iEnd) { break } n = _jsonbPayloadSize(tls, pParse, i, bp) goto _1 _1: ; i += *(*Tu32)(unsafe.Pointer(bp)) + n k++ } return k } // C documentation // // /* // ** Edit the payload size of the element at iRoot by the amount in // ** pParse->delta. // */ func _jsonAfterEditSizeAdjust(tls *libc.TLS, pParse uintptr, iRoot Tu32) { bp := tls.Alloc(16) defer tls.Free(16) var nBlob Tu32 var _ /* sz at bp+0 */ Tu32 _ = nBlob *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) nBlob = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc _jsonbPayloadSize(tls, pParse, iRoot, bp) (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob = nBlob *(*Tu32)(unsafe.Pointer(bp)) += uint32((*TJsonParse)(unsafe.Pointer(pParse)).Fdelta) *(*int32)(unsafe.Pointer(pParse + 40)) += _jsonBlobChangePayloadSize(tls, pParse, iRoot, *(*Tu32)(unsafe.Pointer(bp))) } // C documentation // // /* // ** Modify the JSONB blob at pParse->aBlob by removing nDel bytes of // ** content beginning at iDel, and replacing them with nIns bytes of // ** content given by aIns. // ** // ** nDel may be zero, in which case no bytes are removed. But iDel is // ** still important as new bytes will be insert beginning at iDel. // ** // ** aIns may be zero, in which case space is created to hold nIns bytes // ** beginning at iDel, but that space is uninitialized. // ** // ** Set pParse->oom if an OOM occurs. // */ func _jsonBlobEdit(tls *libc.TLS, pParse uintptr, iDel Tu32, nDel Tu32, aIns uintptr, nIns Tu32) { var d Ti64 var p1, p2 uintptr _, _, _ = d, p1, p2 d = int64(nIns) - int64(nDel) if d != 0 { if int64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob)+d > int64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc) { _jsonBlobExpand(tls, pParse, uint32(int64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob)+d)) if (*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0 { return } } libc.Xmemmove(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(iDel+nIns), (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(iDel+nDel), (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-(iDel+nDel)) p1 = pParse + 4 *(*Tu32)(unsafe.Pointer(p1)) = Tu32(int64(*(*Tu32)(unsafe.Pointer(p1))) + d) p2 = pParse + 40 *(*int32)(unsafe.Pointer(p2)) = int32(int64(*(*int32)(unsafe.Pointer(p2))) + d) } if nIns != 0 && aIns != 0 { libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(iDel), aIns, nIns) } } // C documentation // // /* // ** Return the number of escaped newlines to be ignored. // ** An escaped newline is a one of the following byte sequences: // ** // ** 0x5c 0x0a // ** 0x5c 0x0d // ** 0x5c 0x0d 0x0a // ** 0x5c 0xe2 0x80 0xa8 // ** 0x5c 0xe2 0x80 0xa9 // */ func _jsonBytesToBypass(tls *libc.TLS, z uintptr, n Tu32) (r Tu32) { var i Tu32 _ = i i = uint32(0) for i+uint32(1) < n { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) != int32('\\') { return i } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('\n') { i += uint32(2) continue } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('\r') { if i+uint32(2) < n && int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(2))))) == int32('\n') { i += uint32(3) } else { i += uint32(2) } continue } if int32(0xe2) == int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) && i+uint32(3) < n && int32(0x80) == int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(2))))) && (int32(0xa8) == int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(3))))) || int32(0xa9) == int32(*(*uint8)(unsafe.Pointer(z + uintptr(i+uint32(3)))))) { i += uint32(4) continue } break } return i } // C documentation // // /* // ** Input z[0..n] defines JSON escape sequence including the leading '\\'. // ** Decode that escape sequence into a single character. Write that // ** character into *piOut. Return the number of bytes in the escape sequence. // ** // ** If there is a syntax error of some kind (for example too few characters // ** after the '\\' to complete the encoding) then *piOut is set to // ** JSON_INVALID_CHAR. // */ func _jsonUnescapeOneChar(tls *libc.TLS, z uintptr, n Tu32, piOut uintptr) (r Tu32) { var nSkip, v, vlo, v1 Tu32 var sz int32 var v2 bool _, _, _, _, _, _ = nSkip, sz, v, vlo, v1, v2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if n < uint32(2) { *(*Tu32)(unsafe.Pointer(piOut)) = uint32(JSON_INVALID_CHAR) return n } switch int32(*(*uint8)(unsafe.Pointer(z + 1))) { case int32('u'): if n < uint32(6) { *(*Tu32)(unsafe.Pointer(piOut)) = uint32(JSON_INVALID_CHAR) return n } v = _jsonHexToInt4(tls, z+2) if v2 = v&uint32(0xfc00) == uint32(0xd800) && n >= uint32(12) && int32(*(*uint8)(unsafe.Pointer(z + 6))) == int32('\\') && int32(*(*uint8)(unsafe.Pointer(z + 7))) == int32('u'); v2 { v1 = _jsonHexToInt4(tls, z+8) vlo = v1 } if v2 && v1&uint32(0xfc00) == uint32(0xdc00) { *(*Tu32)(unsafe.Pointer(piOut)) = v&uint32(0x3ff)<= uint32(0xc0) { sz = _sqlite3Utf8ReadLimited(tls, zLeft, int32(nLeft), bp) zLeft += uintptr(sz) nLeft -= uint32(sz) } else { zLeft++ nLeft-- } } else { n = _jsonUnescapeOneChar(tls, zLeft, nLeft, bp) zLeft += uintptr(n) _ = libc.Int32FromInt32(0) nLeft -= n } } if nRight == uint32(0) { *(*Tu32)(unsafe.Pointer(bp + 4)) = uint32(0) } else { if rawRight != 0 || int32(*(*uint8)(unsafe.Pointer(zRight))) != int32('\\') { *(*Tu32)(unsafe.Pointer(bp + 4)) = uint32(*(*Tu8)(unsafe.Pointer(zRight))) if *(*Tu32)(unsafe.Pointer(bp + 4)) >= uint32(0xc0) { sz1 = _sqlite3Utf8ReadLimited(tls, zRight, int32(nRight), bp+4) zRight += uintptr(sz1) nRight -= uint32(sz1) } else { zRight++ nRight-- } } else { n1 = _jsonUnescapeOneChar(tls, zRight, nRight, bp+4) zRight += uintptr(n1) _ = libc.Int32FromInt32(0) nRight -= n1 } } if *(*Tu32)(unsafe.Pointer(bp)) != *(*Tu32)(unsafe.Pointer(bp + 4)) { return 0 } if *(*Tu32)(unsafe.Pointer(bp)) == uint32(0) { return int32(1) } } return r } // C documentation // // /* // ** Compare two object labels. Return 1 if they are equal and // ** 0 if they differ. Return -1 if an OOM occurs. // */ func _jsonLabelCompare(tls *libc.TLS, zLeft uintptr, nLeft Tu32, rawLeft int32, zRight uintptr, nRight Tu32, rawRight int32) (r int32) { if rawLeft != 0 && rawRight != 0 { /* Simpliest case: Neither label contains escapes. A simple ** memcmp() is sufficient. */ if nLeft != nRight { return 0 } return libc.BoolInt32(libc.Xmemcmp(tls, zLeft, zRight, nLeft) == 0) } else { return _jsonLabelCompareEscaped(tls, zLeft, nLeft, rawLeft, zRight, nRight, rawRight) } return r } // C documentation // // /* This helper routine for jsonLookupStep() populates pIns with // ** binary data that is to be inserted into pParse. // ** // ** In the common case, pIns just points to pParse->aIns and pParse->nIns. // ** But if the zPath of the original edit operation includes path elements // ** that go deeper, additional substructure must be created. // ** // ** For example: // ** // ** json_insert('{}', '$.a.b.c', 123); // ** // ** The search stops at '$.a' But additional substructure must be // ** created for the ".b.c" part of the patch so that the final result // ** is: {"a":{"b":{"c"::123}}}. This routine populates pIns with // ** the binary equivalent of {"b":{"c":123}} so that it can be inserted. // ** // ** The caller is responsible for resetting pIns when it has finished // ** using the substructure. // */ func _jsonCreateEditSubstructure(tls *libc.TLS, pParse uintptr, pIns uintptr, zTail uintptr) (r Tu32) { var rc int32 var p1 uintptr _, _ = rc, p1 libc.Xmemset(tls, pIns, 0, uint32(56)) (*TJsonParse)(unsafe.Pointer(pIns)).Fdb = (*TJsonParse)(unsafe.Pointer(pParse)).Fdb if int32(*(*uint8)(unsafe.Pointer(zTail))) == 0 { /* No substructure. Just insert what is given in pParse. */ (*TJsonParse)(unsafe.Pointer(pIns)).FaBlob = (*TJsonParse)(unsafe.Pointer(pParse)).FaIns (*TJsonParse)(unsafe.Pointer(pIns)).FnBlob = (*TJsonParse)(unsafe.Pointer(pParse)).FnIns rc = 0 } else { /* Construct the binary substructure */ (*TJsonParse)(unsafe.Pointer(pIns)).FnBlob = uint32(1) (*TJsonParse)(unsafe.Pointer(pIns)).FaBlob = uintptr(unsafe.Pointer(&_emptyObject)) + libc.BoolUintptr(int32(*(*uint8)(unsafe.Pointer(zTail))) == int32('.')) (*TJsonParse)(unsafe.Pointer(pIns)).FeEdit = (*TJsonParse)(unsafe.Pointer(pParse)).FeEdit (*TJsonParse)(unsafe.Pointer(pIns)).FnIns = (*TJsonParse)(unsafe.Pointer(pParse)).FnIns (*TJsonParse)(unsafe.Pointer(pIns)).FaIns = (*TJsonParse)(unsafe.Pointer(pParse)).FaIns rc = int32(_jsonLookupStep(tls, pIns, uint32(0), zTail, uint32(0))) p1 = pParse + 35 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | int32((*TJsonParse)(unsafe.Pointer(pIns)).Foom)) } return uint32(rc) /* Error code only */ } var _emptyObject = [2]Tu8{ 0: uint8(JSONB_ARRAY), 1: uint8(JSONB_OBJECT), } // C documentation // // /* // ** Search along zPath to find the Json element specified. Return an // ** index into pParse->aBlob[] for the start of that element's value. // ** // ** If the value found by this routine is the value half of label/value pair // ** within an object, then set pPath->iLabel to the start of the corresponding // ** label, before returning. // ** // ** Return one of the JSON_LOOKUP error codes if problems are seen. // ** // ** This routine will also modify the blob. If pParse->eEdit is one of // ** JEDIT_DEL, JEDIT_REPL, JEDIT_INS, or JEDIT_SET, then changes might be // ** made to the selected value. If an edit is performed, then the return // ** value does not necessarily point to the select element. If an edit // ** is performed, the return value is only useful for detecting error // ** conditions. // */ func _jsonLookupStep(tls *libc.TLS, pParse uintptr, iRoot Tu32, zPath uintptr, iLabel Tu32) (r Tu32) { bp := tls.Alloc(176) defer tls.Free(176) var i, iEnd, j, k, n, nIns, nKey, rc, v Tu32 var nn uint32 var rawKey, rawLabel, v3 int32 var x Tu8 var zKey, zLabel, p4 uintptr var _ /* ix at bp+60 */ TJsonParse var _ /* sz at bp+0 */ Tu32 var _ /* v at bp+116 */ TJsonParse var _ /* v at bp+4 */ TJsonParse _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, iEnd, j, k, n, nIns, nKey, nn, rawKey, rawLabel, rc, v, x, zKey, zLabel, v3, p4 if int32(*(*uint8)(unsafe.Pointer(zPath))) == 0 { if (*TJsonParse)(unsafe.Pointer(pParse)).FeEdit != 0 && _jsonBlobMakeEditable(tls, pParse, (*TJsonParse)(unsafe.Pointer(pParse)).FnIns) != 0 { n = _jsonbPayloadSize(tls, pParse, iRoot, bp) *(*Tu32)(unsafe.Pointer(bp)) += n if int32((*TJsonParse)(unsafe.Pointer(pParse)).FeEdit) == int32(JEDIT_DEL) { if iLabel > uint32(0) { *(*Tu32)(unsafe.Pointer(bp)) += iRoot - iLabel iRoot = iLabel } _jsonBlobEdit(tls, pParse, iRoot, *(*Tu32)(unsafe.Pointer(bp)), uintptr(0), uint32(0)) } else { if int32((*TJsonParse)(unsafe.Pointer(pParse)).FeEdit) == int32(JEDIT_INS) { /* Already exists, so json_insert() is a no-op */ } else { /* json_set() or json_replace() */ _jsonBlobEdit(tls, pParse, iRoot, *(*Tu32)(unsafe.Pointer(bp)), (*TJsonParse)(unsafe.Pointer(pParse)).FaIns, (*TJsonParse)(unsafe.Pointer(pParse)).FnIns) } } } (*TJsonParse)(unsafe.Pointer(pParse)).FiLabel = iLabel return iRoot } if int32(*(*uint8)(unsafe.Pointer(zPath))) == int32('.') { rawKey = int32(1) x = *(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(iRoot))) zPath++ if int32(*(*uint8)(unsafe.Pointer(zPath))) == int32('"') { zKey = zPath + uintptr(1) i = uint32(1) for { if !(*(*uint8)(unsafe.Pointer(zPath + uintptr(i))) != 0 && int32(*(*uint8)(unsafe.Pointer(zPath + uintptr(i)))) != int32('"')) { break } goto _1 _1: ; i++ } nKey = i - uint32(1) if *(*uint8)(unsafe.Pointer(zPath + uintptr(i))) != 0 { i++ } else { return uint32(JSON_LOOKUP_PATHERROR) } rawKey = libc.BoolInt32(libc.Xmemchr(tls, zKey, int32('\\'), nKey) == uintptr(0)) } else { zKey = zPath i = uint32(0) for { if !(*(*uint8)(unsafe.Pointer(zPath + uintptr(i))) != 0 && int32(*(*uint8)(unsafe.Pointer(zPath + uintptr(i)))) != int32('.') && int32(*(*uint8)(unsafe.Pointer(zPath + uintptr(i)))) != int32('[')) { break } goto _2 _2: ; i++ } nKey = i if nKey == uint32(0) { return uint32(JSON_LOOKUP_PATHERROR) } } if int32(x)&int32(0x0f) != int32(JSONB_OBJECT) { return uint32(JSON_LOOKUP_NOTFOUND) } n = _jsonbPayloadSize(tls, pParse, iRoot, bp) j = iRoot + n /* j is the index of a label */ iEnd = j + *(*Tu32)(unsafe.Pointer(bp)) for j < iEnd { x = uint8(int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(j)))) & int32(0x0f)) if int32(x) < int32(JSONB_TEXT) || int32(x) > int32(JSONB_TEXTRAW) { return uint32(JSON_LOOKUP_ERROR) } n = _jsonbPayloadSize(tls, pParse, j, bp) if n == uint32(0) { return uint32(JSON_LOOKUP_ERROR) } k = j + n /* k is the index of the label text */ if k+*(*Tu32)(unsafe.Pointer(bp)) >= iEnd { return uint32(JSON_LOOKUP_ERROR) } zLabel = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(k) rawLabel = libc.BoolInt32(int32(x) == int32(JSONB_TEXT) || int32(x) == int32(JSONB_TEXTRAW)) if _jsonLabelCompare(tls, zKey, nKey, rawKey, zLabel, *(*Tu32)(unsafe.Pointer(bp)), rawLabel) != 0 { v = k + *(*Tu32)(unsafe.Pointer(bp)) /* v is the index of the value */ if int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(v))))&int32(0x0f) > int32(JSONB_OBJECT) { return uint32(JSON_LOOKUP_ERROR) } n = _jsonbPayloadSize(tls, pParse, v, bp) if n == uint32(0) || v+n+*(*Tu32)(unsafe.Pointer(bp)) > iEnd { return uint32(JSON_LOOKUP_ERROR) } _ = libc.Int32FromInt32(0) rc = _jsonLookupStep(tls, pParse, v, zPath+uintptr(i), j) if (*TJsonParse)(unsafe.Pointer(pParse)).Fdelta != 0 { _jsonAfterEditSizeAdjust(tls, pParse, iRoot) } return rc } j = k + *(*Tu32)(unsafe.Pointer(bp)) if int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(j))))&int32(0x0f) > int32(JSONB_OBJECT) { return uint32(JSON_LOOKUP_ERROR) } n = _jsonbPayloadSize(tls, pParse, j, bp) if n == uint32(0) { return uint32(JSON_LOOKUP_ERROR) } j += n + *(*Tu32)(unsafe.Pointer(bp)) } if j > iEnd { return uint32(JSON_LOOKUP_ERROR) } if int32((*TJsonParse)(unsafe.Pointer(pParse)).FeEdit) >= int32(JEDIT_INS) { /* Header of the label to be inserted */ libc.Xmemset(tls, bp+60, 0, uint32(56)) (*(*TJsonParse)(unsafe.Pointer(bp + 60))).Fdb = (*TJsonParse)(unsafe.Pointer(pParse)).Fdb if rawKey != 0 { v3 = int32(JSONB_TEXTRAW) } else { v3 = int32(JSONB_TEXT5) } _jsonBlobAppendNode(tls, bp+60, uint8(v3), nKey, uintptr(0)) p4 = pParse + 35 *(*Tu8)(unsafe.Pointer(p4)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p4))) | int32((*(*TJsonParse)(unsafe.Pointer(bp + 60))).Foom)) rc = _jsonCreateEditSubstructure(tls, pParse, bp+4, zPath+uintptr(i)) if !(rc >= libc.Uint32FromUint32(JSON_LOOKUP_PATHERROR)) && _jsonBlobMakeEditable(tls, pParse, (*(*TJsonParse)(unsafe.Pointer(bp + 60))).FnBlob+nKey+(*(*TJsonParse)(unsafe.Pointer(bp + 4))).FnBlob) != 0 { _ = libc.Int32FromInt32(0) nIns = (*(*TJsonParse)(unsafe.Pointer(bp + 60))).FnBlob + nKey + (*(*TJsonParse)(unsafe.Pointer(bp + 4))).FnBlob _jsonBlobEdit(tls, pParse, j, uint32(0), uintptr(0), nIns) if !((*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0) { _ = libc.Int32FromInt32(0) /* Because pParse->oom!=0 */ _ = libc.Int32FromInt32(0) /* Because pPasre->oom!=0 */ libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(j), (*(*TJsonParse)(unsafe.Pointer(bp + 60))).FaBlob, (*(*TJsonParse)(unsafe.Pointer(bp + 60))).FnBlob) k = j + (*(*TJsonParse)(unsafe.Pointer(bp + 60))).FnBlob libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(k), zKey, nKey) k += nKey libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(k), (*(*TJsonParse)(unsafe.Pointer(bp + 4))).FaBlob, (*(*TJsonParse)(unsafe.Pointer(bp + 4))).FnBlob) if (*TJsonParse)(unsafe.Pointer(pParse)).Fdelta != 0 { _jsonAfterEditSizeAdjust(tls, pParse, iRoot) } } } _jsonParseReset(tls, bp+4) _jsonParseReset(tls, bp+60) return rc } } else { if int32(*(*uint8)(unsafe.Pointer(zPath))) == int32('[') { x = uint8(int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(iRoot)))) & int32(0x0f)) if int32(x) != int32(JSONB_ARRAY) { return uint32(JSON_LOOKUP_NOTFOUND) } n = _jsonbPayloadSize(tls, pParse, iRoot, bp) k = uint32(0) i = uint32(1) for int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zPath + uintptr(i)))])&int32(0x04) != 0 { k = k*uint32(10) + uint32(*(*uint8)(unsafe.Pointer(zPath + uintptr(i)))) - uint32('0') i++ } if i < uint32(2) || int32(*(*uint8)(unsafe.Pointer(zPath + uintptr(i)))) != int32(']') { if int32(*(*uint8)(unsafe.Pointer(zPath + 1))) == int32('#') { k = _jsonbArrayCount(tls, pParse, iRoot) i = uint32(2) if int32(*(*uint8)(unsafe.Pointer(zPath + 2))) == int32('-') && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zPath + 3))])&int32(0x04) != 0 { nn = uint32(0) i = uint32(3) for cond := true; cond; cond = int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(zPath + uintptr(i)))])&int32(0x04) != 0 { nn = nn*uint32(10) + uint32(*(*uint8)(unsafe.Pointer(zPath + uintptr(i)))) - uint32('0') i++ } if nn > k { return uint32(JSON_LOOKUP_NOTFOUND) } k -= nn } if int32(*(*uint8)(unsafe.Pointer(zPath + uintptr(i)))) != int32(']') { return uint32(JSON_LOOKUP_PATHERROR) } } else { return uint32(JSON_LOOKUP_PATHERROR) } } j = iRoot + n iEnd = j + *(*Tu32)(unsafe.Pointer(bp)) for j < iEnd { if k == uint32(0) { rc = _jsonLookupStep(tls, pParse, j, zPath+uintptr(i+uint32(1)), uint32(0)) if (*TJsonParse)(unsafe.Pointer(pParse)).Fdelta != 0 { _jsonAfterEditSizeAdjust(tls, pParse, iRoot) } return rc } k-- n = _jsonbPayloadSize(tls, pParse, j, bp) if n == uint32(0) { return uint32(JSON_LOOKUP_ERROR) } j += n + *(*Tu32)(unsafe.Pointer(bp)) } if j > iEnd { return uint32(JSON_LOOKUP_ERROR) } if k > uint32(0) { return uint32(JSON_LOOKUP_NOTFOUND) } if int32((*TJsonParse)(unsafe.Pointer(pParse)).FeEdit) >= int32(JEDIT_INS) { rc = _jsonCreateEditSubstructure(tls, pParse, bp+116, zPath+uintptr(i+uint32(1))) if !(rc >= libc.Uint32FromUint32(JSON_LOOKUP_PATHERROR)) && _jsonBlobMakeEditable(tls, pParse, (*(*TJsonParse)(unsafe.Pointer(bp + 116))).FnBlob) != 0 { _ = libc.Int32FromInt32(0) _jsonBlobEdit(tls, pParse, j, uint32(0), (*(*TJsonParse)(unsafe.Pointer(bp + 116))).FaBlob, (*(*TJsonParse)(unsafe.Pointer(bp + 116))).FnBlob) } _jsonParseReset(tls, bp+116) if (*TJsonParse)(unsafe.Pointer(pParse)).Fdelta != 0 { _jsonAfterEditSizeAdjust(tls, pParse, iRoot) } return rc } } else { return uint32(JSON_LOOKUP_PATHERROR) } } return uint32(JSON_LOOKUP_NOTFOUND) } // C documentation // // /* // ** Convert a JSON BLOB into text and make that text the return value // ** of an SQL function. // */ func _jsonReturnTextJsonFromBlob(tls *libc.TLS, ctx uintptr, aBlob uintptr, nBlob Tu32) { bp := tls.Alloc(192) defer tls.Free(192) var _ /* s at bp+56 */ TJsonString var _ /* x at bp+0 */ TJsonParse if aBlob == uintptr(0) { return } libc.Xmemset(tls, bp, 0, uint32(56)) (*(*TJsonParse)(unsafe.Pointer(bp))).FaBlob = aBlob (*(*TJsonParse)(unsafe.Pointer(bp))).FnBlob = nBlob _jsonStringInit(tls, bp+56, ctx) _jsonTranslateBlobToText(tls, bp, uint32(0), bp+56) _jsonReturnString(tls, bp+56, uintptr(0), uintptr(0)) } // C documentation // // /* // ** Return the value of the BLOB node at index i. // ** // ** If the value is a primitive, return it as an SQL value. // ** If the value is an array or object, return it as either // ** JSON text or the BLOB encoding, depending on the JSON_B flag // ** on the userdata. // */ func _jsonReturnFromBlob(tls *libc.TLS, pParse uintptr, i Tu32, pCtx uintptr, textOnly int32) { bp := tls.Alloc(32) defer tls.Free(32) var bNeg, flags, rc, v30 int32 var c, x uint8 var db, z, z1, z2, zOut uintptr var iIn, iOut, n, nOut, szEscape, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29 Tu32 var v16 int64 var _ /* iRes at bp+8 */ Tsqlite3_int64 var _ /* r at bp+16 */ float64 var _ /* sz at bp+0 */ Tu32 var _ /* v at bp+24 */ Tu32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bNeg, c, db, flags, iIn, iOut, n, nOut, rc, szEscape, x, z, z1, z2, zOut, v16, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30 db = Xsqlite3_context_db_handle(tls, pCtx) n = _jsonbPayloadSize(tls, pParse, i, bp) if n == uint32(0) { Xsqlite3_result_error(tls, pCtx, __ccgo_ts+24936, -int32(1)) return } switch int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i)))) & libc.Int32FromInt32(0x0f) { case JSONB_NULL: goto _1 case int32(JSONB_TRUE): goto _2 case int32(JSONB_FALSE): goto _3 case int32(JSONB_INT): goto _4 case int32(JSONB_INT5): goto _5 case int32(JSONB_FLOAT): goto _6 case int32(JSONB_FLOAT5): goto _7 case int32(JSONB_TEXT): goto _8 case int32(JSONB_TEXTRAW): goto _9 case int32(JSONB_TEXTJ): goto _10 case int32(JSONB_TEXT5): goto _11 case int32(JSONB_OBJECT): goto _12 case int32(JSONB_ARRAY): goto _13 default: goto _14 } goto _15 _1: ; if *(*Tu32)(unsafe.Pointer(bp)) != 0 { goto returnfromblob_malformed } Xsqlite3_result_null(tls, pCtx) goto _15 _2: ; if *(*Tu32)(unsafe.Pointer(bp)) != 0 { goto returnfromblob_malformed } Xsqlite3_result_int(tls, pCtx, int32(1)) goto _15 _3: ; if *(*Tu32)(unsafe.Pointer(bp)) != 0 { goto returnfromblob_malformed } Xsqlite3_result_int(tls, pCtx, 0) goto _15 _5: ; _4: ; *(*Tsqlite3_int64)(unsafe.Pointer(bp + 8)) = 0 bNeg = 0 if *(*Tu32)(unsafe.Pointer(bp)) == uint32(0) { goto returnfromblob_malformed } x = *(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+n))) if int32(x) == int32('-') { if *(*Tu32)(unsafe.Pointer(bp)) < uint32(2) { goto returnfromblob_malformed } n++ *(*Tu32)(unsafe.Pointer(bp))-- bNeg = int32(1) } z = _sqlite3DbStrNDup(tls, db, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i+n), uint64(int32(*(*Tu32)(unsafe.Pointer(bp))))) if z == uintptr(0) { goto returnfromblob_oom } rc = _sqlite3DecOrHexToI64(tls, z, bp+8) _sqlite3DbFree(tls, db, z) if rc == 0 { if bNeg != 0 { v16 = -*(*Tsqlite3_int64)(unsafe.Pointer(bp + 8)) } else { v16 = *(*Tsqlite3_int64)(unsafe.Pointer(bp + 8)) } Xsqlite3_result_int64(tls, pCtx, v16) } else { if rc == int32(3) && bNeg != 0 { Xsqlite3_result_int64(tls, pCtx, int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<>libc.Int32FromInt32(6)) v21 = iOut iOut++ *(*uint8)(unsafe.Pointer(zOut + uintptr(v21))) = uint8(uint32(0x80) | *(*Tu32)(unsafe.Pointer(bp + 24))&uint32(0x3f)) } else { if *(*Tu32)(unsafe.Pointer(bp + 24)) < uint32(0x10000) { _ = libc.Int32FromInt32(0) v22 = iOut iOut++ *(*uint8)(unsafe.Pointer(zOut + uintptr(v22))) = uint8(uint32(0xe0) | *(*Tu32)(unsafe.Pointer(bp + 24))>>libc.Int32FromInt32(12)) v23 = iOut iOut++ *(*uint8)(unsafe.Pointer(zOut + uintptr(v23))) = uint8(uint32(0x80) | *(*Tu32)(unsafe.Pointer(bp + 24))>>libc.Int32FromInt32(6)&uint32(0x3f)) v24 = iOut iOut++ *(*uint8)(unsafe.Pointer(zOut + uintptr(v24))) = uint8(uint32(0x80) | *(*Tu32)(unsafe.Pointer(bp + 24))&uint32(0x3f)) } else { if *(*Tu32)(unsafe.Pointer(bp + 24)) == uint32(JSON_INVALID_CHAR) { /* Silently ignore illegal unicode */ } else { _ = libc.Int32FromInt32(0) v25 = iOut iOut++ *(*uint8)(unsafe.Pointer(zOut + uintptr(v25))) = uint8(uint32(0xf0) | *(*Tu32)(unsafe.Pointer(bp + 24))>>libc.Int32FromInt32(18)) v26 = iOut iOut++ *(*uint8)(unsafe.Pointer(zOut + uintptr(v26))) = uint8(uint32(0x80) | *(*Tu32)(unsafe.Pointer(bp + 24))>>libc.Int32FromInt32(12)&uint32(0x3f)) v27 = iOut iOut++ *(*uint8)(unsafe.Pointer(zOut + uintptr(v27))) = uint8(uint32(0x80) | *(*Tu32)(unsafe.Pointer(bp + 24))>>libc.Int32FromInt32(6)&uint32(0x3f)) v28 = iOut iOut++ *(*uint8)(unsafe.Pointer(zOut + uintptr(v28))) = uint8(uint32(0x80) | *(*Tu32)(unsafe.Pointer(bp + 24))&uint32(0x3f)) } } } } iIn += szEscape - uint32(1) } else { v29 = iOut iOut++ *(*uint8)(unsafe.Pointer(zOut + uintptr(v29))) = c } goto _17 _17: ; iIn++ } /* end for() */ _ = libc.Int32FromInt32(0) *(*uint8)(unsafe.Pointer(zOut + uintptr(iOut))) = uint8(0) Xsqlite3_result_text(tls, pCtx, zOut, int32(iOut), __ccgo_fp(_sqlite3OomClear)) goto _15 _13: ; _12: ; if textOnly != 0 { v30 = 0 } else { v30 = int32(Xsqlite3_user_data(tls, pCtx)) } flags = v30 if flags&int32(JSON_BLOB) != 0 { Xsqlite3_result_blob(tls, pCtx, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i), int32(*(*Tu32)(unsafe.Pointer(bp))+n), uintptr(-libc.Int32FromInt32(1))) } else { _jsonReturnTextJsonFromBlob(tls, pCtx, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i), *(*Tu32)(unsafe.Pointer(bp))+n) } goto _15 _14: ; goto returnfromblob_malformed _15: ; return goto returnfromblob_oom returnfromblob_oom: ; Xsqlite3_result_error_nomem(tls, pCtx) return goto returnfromblob_malformed returnfromblob_malformed: ; Xsqlite3_result_error(tls, pCtx, __ccgo_ts+24936, -int32(1)) return } // C documentation // // /* // ** pArg is a function argument that might be an SQL value or a JSON // ** value. Figure out what it is and encode it as a JSONB blob. // ** Return the results in pParse. // ** // ** pParse is uninitialized upon entry. This routine will handle the // ** initialization of pParse. The result will be contained in // ** pParse->aBlob and pParse->nBlob. pParse->aBlob might be dynamically // ** allocated (if pParse->nBlobAlloc is greater than zero) in which case // ** the caller is responsible for freeing the space allocated to pParse->aBlob // ** when it has finished with it. Or pParse->aBlob might be a static string // ** or a value obtained from sqlite3_value_blob(pArg). // ** // ** If the argument is a BLOB that is clearly not a JSONB, then this // ** function might set an error message in ctx and return non-zero. // ** It might also set an error message and return non-zero on an OOM error. // */ func _jsonFunctionArgToBlob(tls *libc.TLS, ctx uintptr, pArg uintptr, pParse uintptr) (r1 int32) { var eType, n, n1, nJson int32 var r float64 var z, z1, zJson uintptr _, _, _, _, _, _, _, _ = eType, n, n1, nJson, r, z, z1, zJson eType = Xsqlite3_value_type(tls, pArg) libc.Xmemset(tls, pParse, 0, uint32(56)) (*TJsonParse)(unsafe.Pointer(pParse)).Fdb = Xsqlite3_context_db_handle(tls, ctx) switch eType { default: (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob = uintptr(unsafe.Pointer(&_aNull)) (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob = uint32(1) return 0 case int32(SQLITE_BLOB): if _jsonFuncArgMightBeBinary(tls, pArg) != 0 { (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob = Xsqlite3_value_blob(tls, pArg) (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob = uint32(Xsqlite3_value_bytes(tls, pArg)) } else { Xsqlite3_result_error(tls, ctx, __ccgo_ts+24907, -int32(1)) return int32(1) } case int32(SQLITE_TEXT): zJson = Xsqlite3_value_text(tls, pArg) nJson = Xsqlite3_value_bytes(tls, pArg) if zJson == uintptr(0) { return int32(1) } if Xsqlite3_value_subtype(tls, pArg) == uint32(JSON_SUBTYPE) { (*TJsonParse)(unsafe.Pointer(pParse)).FzJson = zJson (*TJsonParse)(unsafe.Pointer(pParse)).FnJson = nJson if _jsonConvertTextToBlob(tls, pParse, ctx) != 0 { Xsqlite3_result_error(tls, ctx, __ccgo_ts+24936, -int32(1)) _sqlite3DbFree(tls, (*TJsonParse)(unsafe.Pointer(pParse)).Fdb, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob) libc.Xmemset(tls, pParse, 0, uint32(56)) return int32(1) } } else { _jsonBlobAppendNode(tls, pParse, uint8(JSONB_TEXTRAW), uint32(nJson), zJson) } case int32(SQLITE_FLOAT): r = Xsqlite3_value_double(tls, pArg) if _sqlite3IsNaN(tls, r) != 0 { _jsonBlobAppendNode(tls, pParse, uint8(JSONB_NULL), uint32(0), uintptr(0)) } else { n = Xsqlite3_value_bytes(tls, pArg) z = Xsqlite3_value_text(tls, pArg) if z == uintptr(0) { return int32(1) } if int32(*(*uint8)(unsafe.Pointer(z))) == int32('I') { _jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint32(5), __ccgo_ts+25041) } else { if int32(*(*uint8)(unsafe.Pointer(z))) == int32('-') && int32(*(*uint8)(unsafe.Pointer(z + 1))) == int32('I') { _jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint32(6), __ccgo_ts+25034) } else { _jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint32(n), z) } } } case int32(SQLITE_INTEGER): n1 = Xsqlite3_value_bytes(tls, pArg) z1 = Xsqlite3_value_text(tls, pArg) if z1 == uintptr(0) { return int32(1) } _jsonBlobAppendNode(tls, pParse, uint8(JSONB_INT), uint32(n1), z1) break } if (*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0 { Xsqlite3_result_error_nomem(tls, ctx) return int32(1) } else { return 0 } return r1 } var _aNull = [1]Tu8{} // C documentation // // /* // ** Generate a bad path error. // ** // ** If ctx is not NULL then push the error message into ctx and return NULL. // ** If ctx is NULL, then return the text of the error message. // */ func _jsonBadPathError(tls *libc.TLS, ctx uintptr, zPath uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var zMsg uintptr _ = zMsg zMsg = Xsqlite3_mprintf(tls, __ccgo_ts+25075, libc.VaList(bp+8, zPath)) if ctx == uintptr(0) { return zMsg } if zMsg != 0 { Xsqlite3_result_error(tls, ctx, zMsg, -int32(1)) Xsqlite3_free(tls, zMsg) } else { Xsqlite3_result_error_nomem(tls, ctx) } return uintptr(0) } // C documentation // // /* argv[0] is a BLOB that seems likely to be a JSONB. Subsequent // ** arguments come in parse where each pair contains a JSON path and // ** content to insert or set at that patch. Do the updates // ** and return the result. // ** // ** The specific operation is determined by eEdit, which can be one // ** of JEDIT_INS, JEDIT_REPL, or JEDIT_SET. // */ func _jsonInsertIntoBlob(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr, eEdit int32) { bp := tls.Alloc(64) defer tls.Free(64) var flgs, i, v1 int32 var p, zPath uintptr var rc Tu32 var _ /* ax at bp+0 */ TJsonParse _, _, _, _, _, _ = flgs, i, p, rc, zPath, v1 rc = uint32(0) zPath = uintptr(0) _ = libc.Int32FromInt32(0) if argc == int32(1) { v1 = 0 } else { v1 = int32(JSON_EDITABLE) } flgs = v1 p = _jsonParseFuncArg(tls, ctx, *(*uintptr)(unsafe.Pointer(argv)), uint32(flgs)) if p == uintptr(0) { return } i = int32(1) for { if !(i < argc-int32(1)) { break } if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) == int32(SQLITE_NULL) { goto _2 } zPath = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) if zPath == uintptr(0) { Xsqlite3_result_error_nomem(tls, ctx) _jsonParseFree(tls, p) return } if int32(*(*uint8)(unsafe.Pointer(zPath))) != int32('$') { goto jsonInsertIntoBlob_patherror } if _jsonFunctionArgToBlob(tls, ctx, *(*uintptr)(unsafe.Pointer(argv + uintptr(i+int32(1))*4)), bp) != 0 { _jsonParseReset(tls, bp) _jsonParseFree(tls, p) return } if int32(*(*uint8)(unsafe.Pointer(zPath + 1))) == 0 { if eEdit == int32(JEDIT_REPL) || eEdit == int32(JEDIT_SET) { _jsonBlobEdit(tls, p, uint32(0), (*TJsonParse)(unsafe.Pointer(p)).FnBlob, (*(*TJsonParse)(unsafe.Pointer(bp))).FaBlob, (*(*TJsonParse)(unsafe.Pointer(bp))).FnBlob) } rc = uint32(0) } else { (*TJsonParse)(unsafe.Pointer(p)).FeEdit = uint8(eEdit) (*TJsonParse)(unsafe.Pointer(p)).FnIns = (*(*TJsonParse)(unsafe.Pointer(bp))).FnBlob (*TJsonParse)(unsafe.Pointer(p)).FaIns = (*(*TJsonParse)(unsafe.Pointer(bp))).FaBlob (*TJsonParse)(unsafe.Pointer(p)).Fdelta = 0 rc = _jsonLookupStep(tls, p, uint32(0), zPath+uintptr(1), uint32(0)) } _jsonParseReset(tls, bp) if rc == uint32(JSON_LOOKUP_NOTFOUND) { goto _2 } if rc >= uint32(JSON_LOOKUP_PATHERROR) { goto jsonInsertIntoBlob_patherror } goto _2 _2: ; i += int32(2) } _jsonReturnParse(tls, ctx, p) _jsonParseFree(tls, p) return goto jsonInsertIntoBlob_patherror jsonInsertIntoBlob_patherror: ; _jsonParseFree(tls, p) if rc == uint32(JSON_LOOKUP_ERROR) { Xsqlite3_result_error(tls, ctx, __ccgo_ts+24936, -int32(1)) } else { _jsonBadPathError(tls, ctx, zPath) } return } // C documentation // // /* // ** If pArg is a blob that seems like a JSONB blob, then initialize // ** p to point to that JSONB and return TRUE. If pArg does not seem like // ** a JSONB blob, then return FALSE; // ** // ** This routine is only called if it is already known that pArg is a // ** blob. The only open question is whether or not the blob appears // ** to be a JSONB blob. // */ func _jsonArgIsJsonb(tls *libc.TLS, pArg uintptr, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var n, v1 Tu32 var v2 bool var _ /* sz at bp+0 */ Tu32 _, _, _ = n, v1, v2 *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) (*TJsonParse)(unsafe.Pointer(p)).FaBlob = Xsqlite3_value_blob(tls, pArg) (*TJsonParse)(unsafe.Pointer(p)).FnBlob = uint32(Xsqlite3_value_bytes(tls, pArg)) if (*TJsonParse)(unsafe.Pointer(p)).FnBlob == uint32(0) { (*TJsonParse)(unsafe.Pointer(p)).FaBlob = uintptr(0) return 0 } if (*TJsonParse)(unsafe.Pointer(p)).FaBlob == uintptr(0) { return 0 } if v2 = int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(p)).FaBlob)))&int32(0x0f) <= int32(JSONB_OBJECT); v2 { v1 = _jsonbPayloadSize(tls, p, uint32(0), bp) n = v1 } if v2 && v1 > uint32(0) && *(*Tu32)(unsafe.Pointer(bp))+n == (*TJsonParse)(unsafe.Pointer(p)).FnBlob && (int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(p)).FaBlob)))&int32(0x0f) > int32(JSONB_FALSE) || *(*Tu32)(unsafe.Pointer(bp)) == uint32(0)) { return int32(1) } (*TJsonParse)(unsafe.Pointer(p)).FaBlob = uintptr(0) (*TJsonParse)(unsafe.Pointer(p)).FnBlob = uint32(0) return 0 } // C documentation // // /* // ** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob, // ** from the SQL function argument pArg. Return a pointer to the new // ** JsonParse object. // ** // ** Ownership of the new JsonParse object is passed to the caller. The // ** caller should invoke jsonParseFree() on the return value when it // ** has finished using it. // ** // ** If any errors are detected, an appropriate error messages is set // ** using sqlite3_result_error() or the equivalent and this routine // ** returns NULL. This routine also returns NULL if the pArg argument // ** is an SQL NULL value, but no error message is set in that case. This // ** is so that SQL functions that are given NULL arguments will return // ** a NULL value. // */ func _jsonParseFuncArg(tls *libc.TLS, ctx uintptr, pArg uintptr, flgs Tu32) (r uintptr) { var db, p, pFromCache, zNew, v2 uintptr var eType, isRCStr, rc int32 var nBlob, v1 Tu32 _, _, _, _, _, _, _, _, _, _ = db, eType, isRCStr, nBlob, p, pFromCache, rc, zNew, v1, v2 /* Datatype of pArg */ p = uintptr(0) /* Value to be returned */ pFromCache = uintptr(0) /* The database connection */ _ = libc.Int32FromInt32(0) eType = Xsqlite3_value_type(tls, pArg) if eType == int32(SQLITE_NULL) { return uintptr(0) } pFromCache = _jsonCacheSearch(tls, ctx, pArg) if pFromCache != 0 { (*TJsonParse)(unsafe.Pointer(pFromCache)).FnJPRef++ if flgs&uint32(JSON_EDITABLE) == uint32(0) { return pFromCache } } db = Xsqlite3_context_db_handle(tls, ctx) goto rebuild_from_cache rebuild_from_cache: ; p = _sqlite3DbMallocZero(tls, db, uint64(56)) if p == uintptr(0) { goto json_pfa_oom } libc.Xmemset(tls, p, 0, uint32(56)) (*TJsonParse)(unsafe.Pointer(p)).Fdb = db (*TJsonParse)(unsafe.Pointer(p)).FnJPRef = uint32(1) if pFromCache != uintptr(0) { nBlob = (*TJsonParse)(unsafe.Pointer(pFromCache)).FnBlob (*TJsonParse)(unsafe.Pointer(p)).FaBlob = _sqlite3DbMallocRaw(tls, db, uint64(nBlob)) if (*TJsonParse)(unsafe.Pointer(p)).FaBlob == uintptr(0) { goto json_pfa_oom } libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(p)).FaBlob, (*TJsonParse)(unsafe.Pointer(pFromCache)).FaBlob, nBlob) v1 = nBlob (*TJsonParse)(unsafe.Pointer(p)).FnBlob = v1 (*TJsonParse)(unsafe.Pointer(p)).FnBlobAlloc = v1 (*TJsonParse)(unsafe.Pointer(p)).FhasNonstd = (*TJsonParse)(unsafe.Pointer(pFromCache)).FhasNonstd _jsonParseFree(tls, pFromCache) return p } if eType == int32(SQLITE_BLOB) { if _jsonArgIsJsonb(tls, pArg, p) != 0 { if flgs&uint32(JSON_EDITABLE) != uint32(0) && _jsonBlobMakeEditable(tls, p, uint32(0)) == 0 { goto json_pfa_oom } return p } /* If the blob is not valid JSONB, fall through into trying to cast ** the blob into text which is then interpreted as JSON. (tag-20240123-a) ** ** This goes against all historical documentation about how the SQLite ** JSON functions were suppose to work. From the beginning, blob was ** reserved for expansion and a blob value should have raised an error. ** But it did not, due to a bug. And many applications came to depend ** upon this buggy behavior, espeically when using the CLI and reading ** JSON text using readfile(), which returns a blob. For this reason ** we will continue to support the bug moving forward. ** See for example https://sqlite.org/forum/forumpost/012136abd5292b8d */ } (*TJsonParse)(unsafe.Pointer(p)).FzJson = Xsqlite3_value_text(tls, pArg) (*TJsonParse)(unsafe.Pointer(p)).FnJson = Xsqlite3_value_bytes(tls, pArg) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto json_pfa_oom } if (*TJsonParse)(unsafe.Pointer(p)).FnJson == 0 { goto json_pfa_malformed } _ = libc.Int32FromInt32(0) if flgs&uint32(JSON_KEEPERROR) != 0 { v2 = uintptr(0) } else { v2 = ctx } if _jsonConvertTextToBlob(tls, p, v2) != 0 { if flgs&uint32(JSON_KEEPERROR) != 0 { (*TJsonParse)(unsafe.Pointer(p)).FnErr = uint8(1) return p } else { _jsonParseFree(tls, p) return uintptr(0) } } else { isRCStr = _sqlite3ValueIsOfClass(tls, pArg, __ccgo_fp(_sqlite3RCStrUnref)) if !(isRCStr != 0) { zNew = _sqlite3RCStrNew(tls, uint64((*TJsonParse)(unsafe.Pointer(p)).FnJson)) if zNew == uintptr(0) { goto json_pfa_oom } libc.Xmemcpy(tls, zNew, (*TJsonParse)(unsafe.Pointer(p)).FzJson, uint32((*TJsonParse)(unsafe.Pointer(p)).FnJson)) (*TJsonParse)(unsafe.Pointer(p)).FzJson = zNew *(*uint8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(p)).FzJson + uintptr((*TJsonParse)(unsafe.Pointer(p)).FnJson))) = uint8(0) } else { _sqlite3RCStrRef(tls, (*TJsonParse)(unsafe.Pointer(p)).FzJson) } (*TJsonParse)(unsafe.Pointer(p)).FbJsonIsRCStr = uint8(1) rc = _jsonCacheInsert(tls, ctx, p) if rc == int32(SQLITE_NOMEM) { goto json_pfa_oom } if flgs&uint32(JSON_EDITABLE) != 0 { pFromCache = p p = uintptr(0) goto rebuild_from_cache } } return p goto json_pfa_malformed json_pfa_malformed: ; if flgs&uint32(JSON_KEEPERROR) != 0 { (*TJsonParse)(unsafe.Pointer(p)).FnErr = uint8(1) return p } else { _jsonParseFree(tls, p) Xsqlite3_result_error(tls, ctx, __ccgo_ts+24936, -int32(1)) return uintptr(0) } goto json_pfa_oom json_pfa_oom: ; _jsonParseFree(tls, pFromCache) _jsonParseFree(tls, p) Xsqlite3_result_error_nomem(tls, ctx) return uintptr(0) } // C documentation // // /* // ** Make the return value of a JSON function either the raw JSONB blob // ** or make it JSON text, depending on whether the JSON_BLOB flag is // ** set on the function. // */ func _jsonReturnParse(tls *libc.TLS, ctx uintptr, p uintptr) { bp := tls.Alloc(128) defer tls.Free(128) var flgs int32 var _ /* s at bp+0 */ TJsonString _ = flgs if (*TJsonParse)(unsafe.Pointer(p)).Foom != 0 { Xsqlite3_result_error_nomem(tls, ctx) return } flgs = int32(Xsqlite3_user_data(tls, ctx)) if flgs&int32(JSON_BLOB) != 0 { if (*TJsonParse)(unsafe.Pointer(p)).FnBlobAlloc > uint32(0) && !((*TJsonParse)(unsafe.Pointer(p)).FbReadOnly != 0) { Xsqlite3_result_blob(tls, ctx, (*TJsonParse)(unsafe.Pointer(p)).FaBlob, int32((*TJsonParse)(unsafe.Pointer(p)).FnBlob), __ccgo_fp(_sqlite3OomClear)) (*TJsonParse)(unsafe.Pointer(p)).FnBlobAlloc = uint32(0) } else { Xsqlite3_result_blob(tls, ctx, (*TJsonParse)(unsafe.Pointer(p)).FaBlob, int32((*TJsonParse)(unsafe.Pointer(p)).FnBlob), uintptr(-libc.Int32FromInt32(1))) } } else { _jsonStringInit(tls, bp, ctx) (*TJsonParse)(unsafe.Pointer(p)).Fdelta = 0 _jsonTranslateBlobToText(tls, p, uint32(0), bp) _jsonReturnString(tls, bp, p, ctx) Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE)) } } /**************************************************************************** ** SQL functions used for testing and debugging ****************************************************************************/ /**************************************************************************** ** Scalar SQL function implementations ****************************************************************************/ // C documentation // // /* // ** Implementation of the json_quote(VALUE) function. Return a JSON value // ** corresponding to the SQL value input. Mostly this means putting // ** double-quotes around strings and returning the unquoted string "null" // ** when given a NULL input. // */ func _jsonQuoteFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { bp := tls.Alloc(128) defer tls.Free(128) var _ /* jx at bp+0 */ TJsonString _ = argc _jsonStringInit(tls, bp, ctx) _jsonAppendSqlValue(tls, bp, *(*uintptr)(unsafe.Pointer(argv))) _jsonReturnString(tls, bp, uintptr(0), uintptr(0)) Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE)) } // C documentation // // /* // ** Implementation of the json_array(VALUE,...) function. Return a JSON // ** array that contains all values given in arguments. Or if any argument // ** is a BLOB, throw an error. // */ func _jsonArrayFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { bp := tls.Alloc(128) defer tls.Free(128) var i int32 var _ /* jx at bp+0 */ TJsonString _ = i _jsonStringInit(tls, bp, ctx) _jsonAppendChar(tls, bp, uint8('[')) i = 0 for { if !(i < argc) { break } _jsonAppendSeparator(tls, bp) _jsonAppendSqlValue(tls, bp, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) goto _1 _1: ; i++ } _jsonAppendChar(tls, bp, uint8(']')) _jsonReturnString(tls, bp, uintptr(0), uintptr(0)) Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE)) } // C documentation // // /* // ** json_array_length(JSON) // ** json_array_length(JSON, PATH) // ** // ** Return the number of elements in the top-level JSON array. // ** Return 0 if the input is not a well-formed JSON array. // */ func _jsonArrayLengthFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { var cnt Tsqlite3_int64 var eErr Tu8 var i Tu32 var p, zPath, v1 uintptr _, _, _, _, _, _ = cnt, eErr, i, p, zPath, v1 /* The parse */ cnt = 0 eErr = uint8(0) p = _jsonParseFuncArg(tls, ctx, *(*uintptr)(unsafe.Pointer(argv)), uint32(0)) if p == uintptr(0) { return } if argc == int32(2) { zPath = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) if zPath == uintptr(0) { _jsonParseFree(tls, p) return } if int32(*(*uint8)(unsafe.Pointer(zPath))) == int32('$') { v1 = zPath + uintptr(1) } else { v1 = __ccgo_ts + 25093 } i = _jsonLookupStep(tls, p, uint32(0), v1, uint32(0)) if i >= uint32(JSON_LOOKUP_PATHERROR) { if i == uint32(JSON_LOOKUP_NOTFOUND) { /* no-op */ } else { if i == uint32(JSON_LOOKUP_PATHERROR) { _jsonBadPathError(tls, ctx, zPath) } else { Xsqlite3_result_error(tls, ctx, __ccgo_ts+24936, -int32(1)) } } eErr = uint8(1) i = uint32(0) } } else { i = uint32(0) } if int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(p)).FaBlob + uintptr(i))))&int32(0x0f) == int32(JSONB_ARRAY) { cnt = int64(_jsonbArrayCount(tls, p, i)) } if !(eErr != 0) { Xsqlite3_result_int64(tls, ctx, cnt) } _jsonParseFree(tls, p) } // C documentation // // /* True if the string is all digits */ func _jsonAllDigits(tls *libc.TLS, z uintptr, n int32) (r int32) { var i int32 _ = i i = 0 for { if !(i < n && int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x04) != 0) { break } goto _1 _1: ; i++ } return libc.BoolInt32(i == n) } // C documentation // // /* True if the string is all alphanumerics and underscores */ func _jsonAllAlphanum(tls *libc.TLS, z uintptr, n int32) (r int32) { var i int32 _ = i i = 0 for { if !(i < n && (int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x06) != 0 || int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) == int32('_'))) { break } goto _1 _1: ; i++ } return libc.BoolInt32(i == n) } // C documentation // // /* // ** json_extract(JSON, PATH, ...) // ** "->"(JSON,PATH) // ** "->>"(JSON,PATH) // ** // ** Return the element described by PATH. Return NULL if that PATH element // ** is not found. // ** // ** If JSON_JSON is set or if more that one PATH argument is supplied then // ** always return a JSON representation of the result. If JSON_SQL is set, // ** then always return an SQL representation of the result. If neither flag // ** is present and argc==2, then return JSON for objects and arrays and SQL // ** for all other values. // ** // ** When multiple PATH arguments are supplied, the result is a JSON array // ** containing the result of each PATH. // ** // ** Abbreviated JSON path expressions are allows if JSON_ABPATH, for // ** compatibility with PG. // */ func _jsonExtractFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { bp := tls.Alloc(128) defer tls.Free(128) var flags, i, nPath int32 var j Tu32 var p, zPath uintptr var _ /* jx at bp+0 */ TJsonString _, _, _, _, _, _ = flags, i, j, nPath, p, zPath p = uintptr(0) /* String for array result */ if argc < int32(2) { return } p = _jsonParseFuncArg(tls, ctx, *(*uintptr)(unsafe.Pointer(argv)), uint32(0)) if p == uintptr(0) { return } flags = int32(Xsqlite3_user_data(tls, ctx)) _jsonStringInit(tls, bp, ctx) if argc > int32(2) { _jsonAppendChar(tls, bp, uint8('[')) } i = int32(1) for { if !(i < argc) { break } /* With a single PATH argument */ zPath = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) if zPath == uintptr(0) { goto json_extract_error } nPath = _sqlite3Strlen30(tls, zPath) if int32(*(*uint8)(unsafe.Pointer(zPath))) == int32('$') { j = _jsonLookupStep(tls, p, uint32(0), zPath+uintptr(1), uint32(0)) } else { if flags&int32(JSON_ABPATH) != 0 { /* The -> and ->> operators accept abbreviated PATH arguments. This ** is mostly for compatibility with PostgreSQL, but also for ** convenience. ** ** NUMBER ==> $[NUMBER] // PG compatible ** LABEL ==> $.LABEL // PG compatible ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience */ _jsonStringInit(tls, bp, ctx) if _jsonAllDigits(tls, zPath, nPath) != 0 { _jsonAppendRawNZ(tls, bp, __ccgo_ts+25095, uint32(1)) _jsonAppendRaw(tls, bp, zPath, uint32(nPath)) _jsonAppendRawNZ(tls, bp, __ccgo_ts+5161, uint32(2)) } else { if _jsonAllAlphanum(tls, zPath, nPath) != 0 { _jsonAppendRawNZ(tls, bp, __ccgo_ts+1661, uint32(1)) _jsonAppendRaw(tls, bp, zPath, uint32(nPath)) } else { if int32(*(*uint8)(unsafe.Pointer(zPath))) == int32('[') && nPath >= int32(3) && int32(*(*uint8)(unsafe.Pointer(zPath + uintptr(nPath-int32(1))))) == int32(']') { _jsonAppendRaw(tls, bp, zPath, uint32(nPath)) } else { _jsonAppendRawNZ(tls, bp, __ccgo_ts+25097, uint32(2)) _jsonAppendRaw(tls, bp, zPath, uint32(nPath)) _jsonAppendRawNZ(tls, bp, __ccgo_ts+25100, uint32(1)) } } } _jsonStringTerminate(tls, bp) j = _jsonLookupStep(tls, p, uint32(0), (*(*TJsonString)(unsafe.Pointer(bp))).FzBuf, uint32(0)) _jsonStringReset(tls, bp) } else { _jsonBadPathError(tls, ctx, zPath) goto json_extract_error } } if j < (*TJsonParse)(unsafe.Pointer(p)).FnBlob { if argc == int32(2) { if flags&int32(JSON_JSON) != 0 { _jsonStringInit(tls, bp, ctx) _jsonTranslateBlobToText(tls, p, j, bp) _jsonReturnString(tls, bp, uintptr(0), uintptr(0)) _jsonStringReset(tls, bp) _ = libc.Int32FromInt32(0) Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE)) } else { _jsonReturnFromBlob(tls, p, j, ctx, 0) if flags&(libc.Int32FromInt32(JSON_SQL)|libc.Int32FromInt32(JSON_BLOB)) == 0 && int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(p)).FaBlob + uintptr(j))))&int32(0x0f) >= int32(JSONB_ARRAY) { Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE)) } } } else { _jsonAppendSeparator(tls, bp) _jsonTranslateBlobToText(tls, p, j, bp) } } else { if j == uint32(JSON_LOOKUP_NOTFOUND) { if argc == int32(2) { goto json_extract_error /* Return NULL if not found */ } else { _jsonAppendSeparator(tls, bp) _jsonAppendRawNZ(tls, bp, __ccgo_ts+1634, uint32(4)) } } else { if j == uint32(JSON_LOOKUP_ERROR) { Xsqlite3_result_error(tls, ctx, __ccgo_ts+24936, -int32(1)) goto json_extract_error } else { _jsonBadPathError(tls, ctx, zPath) goto json_extract_error } } } goto _1 _1: ; i++ } if argc > int32(2) { _jsonAppendChar(tls, bp, uint8(']')) _jsonReturnString(tls, bp, uintptr(0), uintptr(0)) if flags&int32(JSON_BLOB) == 0 { Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE)) } } goto json_extract_error json_extract_error: ; _jsonStringReset(tls, bp) _jsonParseFree(tls, p) return } /* ** Return codes for jsonMergePatch() */ // C documentation // // /* // ** RFC-7396 MergePatch for two JSONB blobs. // ** // ** pTarget is the target. pPatch is the patch. The target is updated // ** in place. The patch is read-only. // ** // ** The original RFC-7396 algorithm is this: // ** // ** define MergePatch(Target, Patch): // ** if Patch is an Object: // ** if Target is not an Object: // ** Target = {} # Ignore the contents and set it to an empty Object // ** for each Name/Value pair in Patch: // ** if Value is null: // ** if Name exists in Target: // ** remove the Name/Value pair from Target // ** else: // ** Target[Name] = MergePatch(Target[Name], Value) // ** return Target // ** else: // ** return Patch // ** // ** Here is an equivalent algorithm restructured to show the actual // ** implementation: // ** // ** 01 define MergePatch(Target, Patch): // ** 02 if Patch is not an Object: // ** 03 return Patch // ** 04 else: // if Patch is an Object // ** 05 if Target is not an Object: // ** 06 Target = {} // ** 07 for each Name/Value pair in Patch: // ** 08 if Name exists in Target: // ** 09 if Value is null: // ** 10 remove the Name/Value pair from Target // ** 11 else // ** 12 Target[name] = MergePatch(Target[Name], Value) // ** 13 else if Value is not NULL: // ** 14 if Value is not an Object: // ** 15 Target[name] = Value // ** 16 else: // ** 17 Target[name] = MergePatch('{}',value) // ** 18 return Target // ** | // ** ^---- Line numbers referenced in comments in the implementation // */ func _jsonMergePatch(tls *libc.TLS, pTarget uintptr, iTarget Tu32, pPatch uintptr, iPatch Tu32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var ePLabel, eTLabel, x Tu8 var iPCursor, iPEnd, iPLabel, iPValue, iTCursor, iTEnd, iTEndBE, iTLabel, iTStart, iTValue, n, nPLabel, nPValue, nTLabel, nTValue, szNew, szPatch, szTarget Tu32 var isEqual, rc, rc1, savedDelta, savedDelta1, v1, v2 int32 var _ /* sz at bp+0 */ Tu32 var _ /* szPLabel at bp+12 */ Tu32 var _ /* szPValue at bp+16 */ Tu32 var _ /* szTLabel at bp+4 */ Tu32 var _ /* szTValue at bp+8 */ Tu32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = ePLabel, eTLabel, iPCursor, iPEnd, iPLabel, iPValue, iTCursor, iTEnd, iTEndBE, iTLabel, iTStart, iTValue, isEqual, n, nPLabel, nPValue, nTLabel, nTValue, rc, rc1, savedDelta, savedDelta1, szNew, szPatch, szTarget, x, v1, v2 *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) /* Node type of the target label */ iTLabel = uint32(0) /* Index of the label */ nTLabel = uint32(0) /* Header size in bytes for the target label */ *(*Tu32)(unsafe.Pointer(bp + 4)) = uint32(0) /* Size of the target label payload */ iTValue = uint32(0) /* Index of the target value */ nTValue = uint32(0) /* Header size of the target value */ *(*Tu32)(unsafe.Pointer(bp + 8)) = uint32(0) /* Payload size of the patch value */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) x = uint8(int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob + uintptr(iPatch)))) & int32(0x0f)) if int32(x) != int32(JSONB_OBJECT) { /* Total size of the target, header+payload */ n = _jsonbPayloadSize(tls, pPatch, iPatch, bp) szPatch = n + *(*Tu32)(unsafe.Pointer(bp)) *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) n = _jsonbPayloadSize(tls, pTarget, iTarget, bp) szTarget = n + *(*Tu32)(unsafe.Pointer(bp)) _jsonBlobEdit(tls, pTarget, iTarget, szTarget, (*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob+uintptr(iPatch), szPatch) if (*TJsonParse)(unsafe.Pointer(pTarget)).Foom != 0 { v1 = int32(JSON_MERGE_OOM) } else { v1 = JSON_MERGE_OK } return v1 /* Line 03 */ } x = uint8(int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob + uintptr(iTarget)))) & int32(0x0f)) if int32(x) != int32(JSONB_OBJECT) { /* Algorithm line 05 */ n = _jsonbPayloadSize(tls, pTarget, iTarget, bp) _jsonBlobEdit(tls, pTarget, iTarget+n, *(*Tu32)(unsafe.Pointer(bp)), uintptr(0), uint32(0)) x = *(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob + uintptr(iTarget))) *(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob + uintptr(iTarget))) = uint8(int32(x)&int32(0xf0) | int32(JSONB_OBJECT)) } n = _jsonbPayloadSize(tls, pPatch, iPatch, bp) if n == uint32(0) { return int32(JSON_MERGE_BADPATCH) } iPCursor = iPatch + n iPEnd = iPCursor + *(*Tu32)(unsafe.Pointer(bp)) n = _jsonbPayloadSize(tls, pTarget, iTarget, bp) if n == uint32(0) { return int32(JSON_MERGE_BADTARGET) } iTStart = iTarget + n iTEndBE = iTStart + *(*Tu32)(unsafe.Pointer(bp)) for iPCursor < iPEnd { /* Algorithm line 07 */ iPLabel = iPCursor ePLabel = uint8(int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob + uintptr(iPCursor)))) & int32(0x0f)) if int32(ePLabel) < int32(JSONB_TEXT) || int32(ePLabel) > int32(JSONB_TEXTRAW) { return int32(JSON_MERGE_BADPATCH) } nPLabel = _jsonbPayloadSize(tls, pPatch, iPCursor, bp+12) if nPLabel == uint32(0) { return int32(JSON_MERGE_BADPATCH) } iPValue = iPCursor + nPLabel + *(*Tu32)(unsafe.Pointer(bp + 12)) if iPValue >= iPEnd { return int32(JSON_MERGE_BADPATCH) } nPValue = _jsonbPayloadSize(tls, pPatch, iPValue, bp+16) if nPValue == uint32(0) { return int32(JSON_MERGE_BADPATCH) } iPCursor = iPValue + nPValue + *(*Tu32)(unsafe.Pointer(bp + 16)) if iPCursor > iPEnd { return int32(JSON_MERGE_BADPATCH) } iTCursor = iTStart iTEnd = iTEndBE + uint32((*TJsonParse)(unsafe.Pointer(pTarget)).Fdelta) for iTCursor < iTEnd { /* true if the patch and target labels match */ iTLabel = iTCursor eTLabel = uint8(int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob + uintptr(iTCursor)))) & int32(0x0f)) if int32(eTLabel) < int32(JSONB_TEXT) || int32(eTLabel) > int32(JSONB_TEXTRAW) { return int32(JSON_MERGE_BADTARGET) } nTLabel = _jsonbPayloadSize(tls, pTarget, iTCursor, bp+4) if nTLabel == uint32(0) { return int32(JSON_MERGE_BADTARGET) } iTValue = iTLabel + nTLabel + *(*Tu32)(unsafe.Pointer(bp + 4)) if iTValue >= iTEnd { return int32(JSON_MERGE_BADTARGET) } nTValue = _jsonbPayloadSize(tls, pTarget, iTValue, bp+8) if nTValue == uint32(0) { return int32(JSON_MERGE_BADTARGET) } if iTValue+nTValue+*(*Tu32)(unsafe.Pointer(bp + 8)) > iTEnd { return int32(JSON_MERGE_BADTARGET) } isEqual = _jsonLabelCompare(tls, (*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob+uintptr(iPLabel+nPLabel), *(*Tu32)(unsafe.Pointer(bp + 12)), libc.BoolInt32(int32(ePLabel) == int32(JSONB_TEXT) || int32(ePLabel) == int32(JSONB_TEXTRAW)), (*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob+uintptr(iTLabel+nTLabel), *(*Tu32)(unsafe.Pointer(bp + 4)), libc.BoolInt32(int32(eTLabel) == int32(JSONB_TEXT) || int32(eTLabel) == int32(JSONB_TEXTRAW))) if isEqual != 0 { break } iTCursor = iTValue + nTValue + *(*Tu32)(unsafe.Pointer(bp + 8)) } x = uint8(int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob + uintptr(iPValue)))) & int32(0x0f)) if iTCursor < iTEnd { /* A match was found. Algorithm line 08 */ if int32(x) == 0 { /* Patch value is NULL. Algorithm line 09 */ _jsonBlobEdit(tls, pTarget, iTLabel, nTLabel+*(*Tu32)(unsafe.Pointer(bp + 4))+nTValue+*(*Tu32)(unsafe.Pointer(bp + 8)), uintptr(0), uint32(0)) /* vvvvvv----- No OOM on a delete-only edit */ if (*TJsonParse)(unsafe.Pointer(pTarget)).Foom != 0 { return int32(JSON_MERGE_OOM) } } else { savedDelta = (*TJsonParse)(unsafe.Pointer(pTarget)).Fdelta (*TJsonParse)(unsafe.Pointer(pTarget)).Fdelta = 0 rc = _jsonMergePatch(tls, pTarget, iTValue, pPatch, iPValue) if rc != 0 { return rc } *(*int32)(unsafe.Pointer(pTarget + 40)) += savedDelta } } else { if int32(x) > 0 { /* Algorithm line 13 */ /* No match and patch value is not NULL */ szNew = *(*Tu32)(unsafe.Pointer(bp + 12)) + nPLabel if int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob + uintptr(iPValue))))&int32(0x0f) != int32(JSONB_OBJECT) { /* Line 14 */ _jsonBlobEdit(tls, pTarget, iTEnd, uint32(0), uintptr(0), *(*Tu32)(unsafe.Pointer(bp + 16))+nPValue+szNew) if (*TJsonParse)(unsafe.Pointer(pTarget)).Foom != 0 { return int32(JSON_MERGE_OOM) } libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob+uintptr(iTEnd), (*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob+uintptr(iPLabel), szNew) libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob+uintptr(iTEnd+szNew), (*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob+uintptr(iPValue), *(*Tu32)(unsafe.Pointer(bp + 16))+nPValue) } else { _jsonBlobEdit(tls, pTarget, iTEnd, uint32(0), uintptr(0), szNew+uint32(1)) if (*TJsonParse)(unsafe.Pointer(pTarget)).Foom != 0 { return int32(JSON_MERGE_OOM) } libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob+uintptr(iTEnd), (*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob+uintptr(iPLabel), szNew) *(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob + uintptr(iTEnd+szNew))) = uint8(0x00) savedDelta1 = (*TJsonParse)(unsafe.Pointer(pTarget)).Fdelta (*TJsonParse)(unsafe.Pointer(pTarget)).Fdelta = 0 rc1 = _jsonMergePatch(tls, pTarget, iTEnd+szNew, pPatch, iPValue) if rc1 != 0 { return rc1 } *(*int32)(unsafe.Pointer(pTarget + 40)) += savedDelta1 } } } } if (*TJsonParse)(unsafe.Pointer(pTarget)).Fdelta != 0 { _jsonAfterEditSizeAdjust(tls, pTarget, iTarget) } if (*TJsonParse)(unsafe.Pointer(pTarget)).Foom != 0 { v2 = int32(JSON_MERGE_OOM) } else { v2 = JSON_MERGE_OK } return v2 } // C documentation // // /* // ** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON // ** object that is the result of running the RFC 7396 MergePatch() algorithm // ** on the two arguments. // */ func _jsonPatchFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { var pPatch, pTarget uintptr var rc int32 _, _, _ = pPatch, pTarget, rc /* Result code */ _ = argc _ = libc.Int32FromInt32(0) pTarget = _jsonParseFuncArg(tls, ctx, *(*uintptr)(unsafe.Pointer(argv)), uint32(JSON_EDITABLE)) if pTarget == uintptr(0) { return } pPatch = _jsonParseFuncArg(tls, ctx, *(*uintptr)(unsafe.Pointer(argv + 1*4)), uint32(0)) if pPatch != 0 { rc = _jsonMergePatch(tls, pTarget, uint32(0), pPatch, uint32(0)) if rc == JSON_MERGE_OK { _jsonReturnParse(tls, ctx, pTarget) } else { if rc == int32(JSON_MERGE_OOM) { Xsqlite3_result_error_nomem(tls, ctx) } else { Xsqlite3_result_error(tls, ctx, __ccgo_ts+24936, -int32(1)) } } _jsonParseFree(tls, pPatch) } _jsonParseFree(tls, pTarget) } // C documentation // // /* // ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON // ** object that contains all name/value given in arguments. Or if any name // ** is not a string or if any value is a BLOB, throw an error. // */ func _jsonObjectFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { bp := tls.Alloc(128) defer tls.Free(128) var i int32 var n Tu32 var z uintptr var _ /* jx at bp+0 */ TJsonString _, _, _ = i, n, z if argc&int32(1) != 0 { Xsqlite3_result_error(tls, ctx, __ccgo_ts+25102, -int32(1)) return } _jsonStringInit(tls, bp, ctx) _jsonAppendChar(tls, bp, uint8('{')) i = 0 for { if !(i < argc) { break } if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) != int32(SQLITE_TEXT) { Xsqlite3_result_error(tls, ctx, __ccgo_ts+25153, -int32(1)) _jsonStringReset(tls, bp) return } _jsonAppendSeparator(tls, bp) z = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) n = uint32(Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4)))) _jsonAppendString(tls, bp, z, n) _jsonAppendChar(tls, bp, uint8(':')) _jsonAppendSqlValue(tls, bp, *(*uintptr)(unsafe.Pointer(argv + uintptr(i+int32(1))*4))) goto _1 _1: ; i += int32(2) } _jsonAppendChar(tls, bp, uint8('}')) _jsonReturnString(tls, bp, uintptr(0), uintptr(0)) Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE)) } // C documentation // // /* // ** json_remove(JSON, PATH, ...) // ** // ** Remove the named elements from JSON and return the result. malformed // ** JSON or PATH arguments result in an error. // */ func _jsonRemoveFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { var i, v1 int32 var p, zPath uintptr var rc Tu32 _, _, _, _, _ = i, p, rc, zPath, v1 /* The parse */ zPath = uintptr(0) /* Subroutine return code */ if argc < int32(1) { return } if argc > int32(1) { v1 = int32(JSON_EDITABLE) } else { v1 = 0 } p = _jsonParseFuncArg(tls, ctx, *(*uintptr)(unsafe.Pointer(argv)), uint32(v1)) if p == uintptr(0) { return } i = int32(1) for { if !(i < argc) { break } zPath = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) if zPath == uintptr(0) { goto json_remove_done } if int32(*(*uint8)(unsafe.Pointer(zPath))) != int32('$') { goto json_remove_patherror } if int32(*(*uint8)(unsafe.Pointer(zPath + 1))) == 0 { /* json_remove(j,'$') returns NULL */ goto json_remove_done } (*TJsonParse)(unsafe.Pointer(p)).FeEdit = uint8(JEDIT_DEL) (*TJsonParse)(unsafe.Pointer(p)).Fdelta = 0 rc = _jsonLookupStep(tls, p, uint32(0), zPath+uintptr(1), uint32(0)) if rc >= uint32(JSON_LOOKUP_PATHERROR) { if rc == uint32(JSON_LOOKUP_NOTFOUND) { goto _2 /* No-op */ } else { if rc == uint32(JSON_LOOKUP_PATHERROR) { _jsonBadPathError(tls, ctx, zPath) } else { Xsqlite3_result_error(tls, ctx, __ccgo_ts+24936, -int32(1)) } } goto json_remove_done } goto _2 _2: ; i++ } _jsonReturnParse(tls, ctx, p) _jsonParseFree(tls, p) return goto json_remove_patherror json_remove_patherror: ; _jsonBadPathError(tls, ctx, zPath) goto json_remove_done json_remove_done: ; _jsonParseFree(tls, p) return } // C documentation // // /* // ** json_replace(JSON, PATH, VALUE, ...) // ** // ** Replace the value at PATH with VALUE. If PATH does not already exist, // ** this routine is a no-op. If JSON or PATH is malformed, throw an error. // */ func _jsonReplaceFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { if argc < int32(1) { return } if argc&int32(1) == 0 { _jsonWrongNumArgs(tls, ctx, __ccgo_ts+15796) return } _jsonInsertIntoBlob(tls, ctx, argc, argv, int32(JEDIT_REPL)) } // C documentation // // /* // ** json_set(JSON, PATH, VALUE, ...) // ** // ** Set the value at PATH to VALUE. Create the PATH if it does not already // ** exist. Overwrite existing values that do exist. // ** If JSON or PATH is malformed, throw an error. // ** // ** json_insert(JSON, PATH, VALUE, ...) // ** // ** Create PATH and initialize it to VALUE. If PATH already exists, this // ** routine is a no-op. If JSON or PATH is malformed, throw an error. // */ func _jsonSetFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { var bIsSet, flags, v2 int32 var v1 uintptr _, _, _, _ = bIsSet, flags, v1, v2 flags = int32(Xsqlite3_user_data(tls, ctx)) bIsSet = libc.BoolInt32(flags&int32(JSON_ISSET) != 0) if argc < int32(1) { return } if argc&int32(1) == 0 { if bIsSet != 0 { v1 = __ccgo_ts + 25187 } else { v1 = __ccgo_ts + 25191 } _jsonWrongNumArgs(tls, ctx, v1) return } if bIsSet != 0 { v2 = int32(JEDIT_SET) } else { v2 = int32(JEDIT_INS) } _jsonInsertIntoBlob(tls, ctx, argc, argv, v2) } // C documentation // // /* // ** json_type(JSON) // ** json_type(JSON, PATH) // ** // ** Return the top-level "type" of a JSON string. json_type() raises an // ** error if either the JSON or PATH inputs are not well-formed. // */ func _jsonTypeFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { var i Tu32 var p, zPath uintptr _, _, _ = i, p, zPath /* The parse */ zPath = uintptr(0) p = _jsonParseFuncArg(tls, ctx, *(*uintptr)(unsafe.Pointer(argv)), uint32(0)) if p == uintptr(0) { return } if argc == int32(2) { zPath = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) if zPath == uintptr(0) { goto json_type_done } if int32(*(*uint8)(unsafe.Pointer(zPath))) != int32('$') { _jsonBadPathError(tls, ctx, zPath) goto json_type_done } i = _jsonLookupStep(tls, p, uint32(0), zPath+uintptr(1), uint32(0)) if i >= uint32(JSON_LOOKUP_PATHERROR) { if i == uint32(JSON_LOOKUP_NOTFOUND) { /* no-op */ } else { if i == uint32(JSON_LOOKUP_PATHERROR) { _jsonBadPathError(tls, ctx, zPath) } else { Xsqlite3_result_error(tls, ctx, __ccgo_ts+24936, -int32(1)) } } goto json_type_done } } else { i = uint32(0) } Xsqlite3_result_text(tls, ctx, _jsonbType[int32(*(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(p)).FaBlob + uintptr(i))))&int32(0x0f)], -int32(1), libc.UintptrFromInt32(0)) goto json_type_done json_type_done: ; _jsonParseFree(tls, p) } // C documentation // // /* // ** json_valid(JSON) // ** json_valid(JSON, FLAGS) // ** // ** Check the JSON argument to see if it is well-formed. The FLAGS argument // ** encodes the various constraints on what is meant by "well-formed": // ** // ** 0x01 Canonical RFC-8259 JSON text // ** 0x02 JSON text with optional JSON-5 extensions // ** 0x04 Superficially appears to be JSONB // ** 0x08 Strictly well-formed JSONB // ** // ** If the FLAGS argument is omitted, it defaults to 1. Useful values for // ** FLAGS include: // ** // ** 1 Strict canonical JSON text // ** 2 JSON text perhaps with JSON-5 extensions // ** 4 Superficially appears to be JSONB // ** 5 Canonical JSON text or superficial JSONB // ** 6 JSON-5 text or superficial JSONB // ** 8 Strict JSONB // ** 9 Canonical JSON text or strict JSONB // ** 10 JSON-5 text or strict JSONB // ** // ** Other flag combinations are redundant. For example, every canonical // ** JSON text is also well-formed JSON-5 text, so FLAG values 2 and 3 // ** are the same. Similarly, any input that passes a strict JSONB validation // ** will also pass the superficial validation so 12 through 15 are the same // ** as 8 through 11 respectively. // ** // ** This routine runs in linear time to validate text and when doing strict // ** JSONB validation. Superficial JSONB validation is constant time, // ** assuming the BLOB is already in memory. The performance advantage // ** of superficial JSONB validation is why that option is provided. // ** Application developers can choose to do fast superficial validation or // ** slower strict validation, according to their specific needs. // ** // ** Only the lower four bits of the FLAGS argument are currently used. // ** Higher bits are reserved for future expansion. To facilitate // ** compatibility, the current implementation raises an error if any bit // ** in FLAGS is set other than the lower four bits. // ** // ** The original circa 2015 implementation of the JSON routines in // ** SQLite only supported canonical RFC-8259 JSON text and the json_valid() // ** function only accepted one argument. That is why the default value // ** for the FLAGS argument is 1, since FLAGS=1 causes this routine to only // ** recognize canonical RFC-8259 JSON text as valid. The extra FLAGS // ** argument was added when the JSON routines were extended to support // ** JSON5-like extensions and binary JSONB stored in BLOBs. // ** // ** Return Values: // ** // ** * Raise an error if FLAGS is outside the range of 1 to 15. // ** * Return NULL if the input is NULL // ** * Return 1 if the input is well-formed. // ** * Return 0 if the input is not well-formed. // */ func _jsonValidFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { bp := tls.Alloc(112) defer tls.Free(112) var f Ti64 var flags, res Tu8 var iErr Tu32 var p uintptr var _ /* px at bp+0 */ TJsonParse var _ /* px at bp+56 */ TJsonParse _, _, _, _, _ = f, flags, iErr, p, res /* The parse */ flags = uint8(1) res = uint8(0) if argc == int32(2) { f = Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) if f < int64(1) || f > int64(15) { Xsqlite3_result_error(tls, ctx, __ccgo_ts+25198, -int32(1)) return } flags = uint8(f & int64(0x0f)) } switch Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) { case int32(SQLITE_NULL): return case int32(SQLITE_BLOB): if _jsonFuncArgMightBeBinary(tls, *(*uintptr)(unsafe.Pointer(argv))) != 0 { if int32(flags)&int32(0x04) != 0 { /* Superficial checking only - accomplished by the ** jsonFuncArgMightBeBinary() call above. */ res = uint8(1) } else { if int32(flags)&int32(0x08) != 0 { libc.Xmemset(tls, bp, 0, uint32(56)) (*(*TJsonParse)(unsafe.Pointer(bp))).FaBlob = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv))) (*(*TJsonParse)(unsafe.Pointer(bp))).FnBlob = uint32(Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv)))) iErr = _jsonbValidityCheck(tls, bp, uint32(0), (*(*TJsonParse)(unsafe.Pointer(bp))).FnBlob, uint32(1)) res = libc.BoolUint8(iErr == uint32(0)) } } break } fallthrough default: if int32(flags)&int32(0x3) == 0 { break } libc.Xmemset(tls, bp+56, 0, uint32(56)) p = _jsonParseFuncArg(tls, ctx, *(*uintptr)(unsafe.Pointer(argv)), uint32(JSON_KEEPERROR)) if p != 0 { if (*TJsonParse)(unsafe.Pointer(p)).Foom != 0 { Xsqlite3_result_error_nomem(tls, ctx) } else { if (*TJsonParse)(unsafe.Pointer(p)).FnErr != 0 { /* no-op */ } else { if int32(flags)&int32(0x02) != 0 || int32((*TJsonParse)(unsafe.Pointer(p)).FhasNonstd) == 0 { res = uint8(1) } } } _jsonParseFree(tls, p) } else { Xsqlite3_result_error_nomem(tls, ctx) } break } Xsqlite3_result_int(tls, ctx, int32(res)) } // C documentation // // /* // ** json_error_position(JSON) // ** // ** If the argument is NULL, return NULL // ** // ** If the argument is BLOB, do a full validity check and return non-zero // ** if the check fails. The return value is the approximate 1-based offset // ** to the byte of the element that contains the first error. // ** // ** Otherwise interpret the argument is TEXT (even if it is numeric) and // ** return the 1-based character position for where the parser first recognized // ** that the input was not valid JSON, or return 0 if the input text looks // ** ok. JSON-5 extensions are accepted. // */ func _jsonErrorFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var iErrPos Ti64 var k Tu32 var _ /* s at bp+0 */ TJsonParse _, _ = iErrPos, k iErrPos = 0 _ = libc.Int32FromInt32(0) _ = argc libc.Xmemset(tls, bp, 0, uint32(56)) (*(*TJsonParse)(unsafe.Pointer(bp))).Fdb = Xsqlite3_context_db_handle(tls, ctx) if _jsonFuncArgMightBeBinary(tls, *(*uintptr)(unsafe.Pointer(argv))) != 0 { (*(*TJsonParse)(unsafe.Pointer(bp))).FaBlob = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv))) (*(*TJsonParse)(unsafe.Pointer(bp))).FnBlob = uint32(Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv)))) iErrPos = int64(_jsonbValidityCheck(tls, bp, uint32(0), (*(*TJsonParse)(unsafe.Pointer(bp))).FnBlob, uint32(1))) } else { (*(*TJsonParse)(unsafe.Pointer(bp))).FzJson = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) if (*(*TJsonParse)(unsafe.Pointer(bp))).FzJson == uintptr(0) { return } /* NULL input or OOM */ (*(*TJsonParse)(unsafe.Pointer(bp))).FnJson = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv))) if _jsonConvertTextToBlob(tls, bp, uintptr(0)) != 0 { if (*(*TJsonParse)(unsafe.Pointer(bp))).Foom != 0 { iErrPos = int64(-int32(1)) } else { _ = libc.Int32FromInt32(0) /* Because s.oom is false */ k = uint32(0) for { if !(k < (*(*TJsonParse)(unsafe.Pointer(bp))).FiErr && *(*uint8)(unsafe.Pointer((*(*TJsonParse)(unsafe.Pointer(bp))).FzJson + uintptr(k))) != 0) { break } if int32(*(*uint8)(unsafe.Pointer((*(*TJsonParse)(unsafe.Pointer(bp))).FzJson + uintptr(k))))&int32(0xc0) != int32(0x80) { iErrPos++ } goto _1 _1: ; k++ } iErrPos++ } } } _jsonParseReset(tls, bp) if iErrPos < 0 { Xsqlite3_result_error_nomem(tls, ctx) } else { Xsqlite3_result_int64(tls, ctx, iErrPos) } } // C documentation // // /**************************************************************************** // ** Aggregate SQL function implementations // ****************************************************************************/ // /* // ** json_group_array(VALUE) // ** // ** Return a JSON array composed of all values in the aggregate. // */ func _jsonArrayStep(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { var pStr uintptr _ = pStr _ = argc pStr = Xsqlite3_aggregate_context(tls, ctx, int32(128)) if pStr != 0 { if (*TJsonString)(unsafe.Pointer(pStr)).FzBuf == uintptr(0) { _jsonStringInit(tls, pStr, ctx) _jsonAppendChar(tls, pStr, uint8('[')) } else { if (*TJsonString)(unsafe.Pointer(pStr)).FnUsed > uint64(1) { _jsonAppendChar(tls, pStr, uint8(',')) } } (*TJsonString)(unsafe.Pointer(pStr)).FpCtx = ctx _jsonAppendSqlValue(tls, pStr, *(*uintptr)(unsafe.Pointer(argv))) } } func _jsonArrayCompute(tls *libc.TLS, ctx uintptr, isFinal int32) { var flags int32 var pStr uintptr var v1 Tsqlite3_destructor_type _, _, _ = flags, pStr, v1 pStr = Xsqlite3_aggregate_context(tls, ctx, 0) if pStr != 0 { (*TJsonString)(unsafe.Pointer(pStr)).FpCtx = ctx _jsonAppendChar(tls, pStr, uint8(']')) flags = int32(Xsqlite3_user_data(tls, ctx)) if (*TJsonString)(unsafe.Pointer(pStr)).FeErr != 0 { _jsonReturnString(tls, pStr, uintptr(0), uintptr(0)) return } else { if flags&int32(JSON_BLOB) != 0 { _jsonReturnStringAsBlob(tls, pStr) if isFinal != 0 { if !((*TJsonString)(unsafe.Pointer(pStr)).FbStatic != 0) { _sqlite3RCStrUnref(tls, (*TJsonString)(unsafe.Pointer(pStr)).FzBuf) } } else { _jsonStringTrimOneChar(tls, pStr) } return } else { if isFinal != 0 { if (*TJsonString)(unsafe.Pointer(pStr)).FbStatic != 0 { v1 = uintptr(-libc.Int32FromInt32(1)) } else { v1 = __ccgo_fp(_sqlite3RCStrUnref) } Xsqlite3_result_text(tls, ctx, (*TJsonString)(unsafe.Pointer(pStr)).FzBuf, int32((*TJsonString)(unsafe.Pointer(pStr)).FnUsed), v1) (*TJsonString)(unsafe.Pointer(pStr)).FbStatic = uint8(1) } else { Xsqlite3_result_text(tls, ctx, (*TJsonString)(unsafe.Pointer(pStr)).FzBuf, int32((*TJsonString)(unsafe.Pointer(pStr)).FnUsed), uintptr(-libc.Int32FromInt32(1))) _jsonStringTrimOneChar(tls, pStr) } } } } else { Xsqlite3_result_text(tls, ctx, __ccgo_ts+25255, int32(2), libc.UintptrFromInt32(0)) } Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE)) } func _jsonArrayValue(tls *libc.TLS, ctx uintptr) { _jsonArrayCompute(tls, ctx, 0) } func _jsonArrayFinal(tls *libc.TLS, ctx uintptr) { _jsonArrayCompute(tls, ctx, int32(1)) } // C documentation // // /* // ** This method works for both json_group_array() and json_group_object(). // ** It works by removing the first element of the group by searching forward // ** to the first comma (",") that is not within a string and deleting all // ** text through that comma. // */ func _jsonGroupInverse(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { var c, v2 uint8 var i uint32 var inStr, nNest int32 var pStr, z uintptr var v3 bool _, _, _, _, _, _, _, _ = c, i, inStr, nNest, pStr, z, v2, v3 inStr = 0 nNest = 0 _ = argc _ = argv pStr = Xsqlite3_aggregate_context(tls, ctx, 0) /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will ** always have been called to initialize it */ if !(pStr != 0) { return } z = (*TJsonString)(unsafe.Pointer(pStr)).FzBuf i = uint32(1) for { if v3 = uint64(i) < (*TJsonString)(unsafe.Pointer(pStr)).FnUsed; v3 { v2 = *(*uint8)(unsafe.Pointer(z + uintptr(i))) c = v2 } if !(v3 && (int32(v2) != int32(',') || inStr != 0 || nNest != 0)) { break } if int32(c) == int32('"') { inStr = libc.BoolInt32(!(inStr != 0)) } else { if int32(c) == int32('\\') { i++ } else { if !(inStr != 0) { if int32(c) == int32('{') || int32(c) == int32('[') { nNest++ } if int32(c) == int32('}') || int32(c) == int32(']') { nNest-- } } } } goto _1 _1: ; i++ } if uint64(i) < (*TJsonString)(unsafe.Pointer(pStr)).FnUsed { *(*Tu64)(unsafe.Pointer(pStr + 16)) -= uint64(i) libc.Xmemmove(tls, z+1, z+uintptr(i+uint32(1)), uint32((*TJsonString)(unsafe.Pointer(pStr)).FnUsed)-uint32(1)) *(*uint8)(unsafe.Pointer(z + uintptr((*TJsonString)(unsafe.Pointer(pStr)).FnUsed))) = uint8(0) } else { (*TJsonString)(unsafe.Pointer(pStr)).FnUsed = uint64(1) } } // C documentation // // /* // ** json_group_obj(NAME,VALUE) // ** // ** Return a JSON object composed of all names and values in the aggregate. // */ func _jsonObjectStep(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { var n Tu32 var pStr, z uintptr _, _, _ = n, pStr, z _ = argc pStr = Xsqlite3_aggregate_context(tls, ctx, int32(128)) if pStr != 0 { if (*TJsonString)(unsafe.Pointer(pStr)).FzBuf == uintptr(0) { _jsonStringInit(tls, pStr, ctx) _jsonAppendChar(tls, pStr, uint8('{')) } else { if (*TJsonString)(unsafe.Pointer(pStr)).FnUsed > uint64(1) { _jsonAppendChar(tls, pStr, uint8(',')) } } (*TJsonString)(unsafe.Pointer(pStr)).FpCtx = ctx z = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) n = uint32(_sqlite3Strlen30(tls, z)) _jsonAppendString(tls, pStr, z, n) _jsonAppendChar(tls, pStr, uint8(':')) _jsonAppendSqlValue(tls, pStr, *(*uintptr)(unsafe.Pointer(argv + 1*4))) } } func _jsonObjectCompute(tls *libc.TLS, ctx uintptr, isFinal int32) { var flags int32 var pStr uintptr var v1 Tsqlite3_destructor_type _, _, _ = flags, pStr, v1 pStr = Xsqlite3_aggregate_context(tls, ctx, 0) if pStr != 0 { _jsonAppendChar(tls, pStr, uint8('}')) (*TJsonString)(unsafe.Pointer(pStr)).FpCtx = ctx flags = int32(Xsqlite3_user_data(tls, ctx)) if (*TJsonString)(unsafe.Pointer(pStr)).FeErr != 0 { _jsonReturnString(tls, pStr, uintptr(0), uintptr(0)) return } else { if flags&int32(JSON_BLOB) != 0 { _jsonReturnStringAsBlob(tls, pStr) if isFinal != 0 { if !((*TJsonString)(unsafe.Pointer(pStr)).FbStatic != 0) { _sqlite3RCStrUnref(tls, (*TJsonString)(unsafe.Pointer(pStr)).FzBuf) } } else { _jsonStringTrimOneChar(tls, pStr) } return } else { if isFinal != 0 { if (*TJsonString)(unsafe.Pointer(pStr)).FbStatic != 0 { v1 = uintptr(-libc.Int32FromInt32(1)) } else { v1 = __ccgo_fp(_sqlite3RCStrUnref) } Xsqlite3_result_text(tls, ctx, (*TJsonString)(unsafe.Pointer(pStr)).FzBuf, int32((*TJsonString)(unsafe.Pointer(pStr)).FnUsed), v1) (*TJsonString)(unsafe.Pointer(pStr)).FbStatic = uint8(1) } else { Xsqlite3_result_text(tls, ctx, (*TJsonString)(unsafe.Pointer(pStr)).FzBuf, int32((*TJsonString)(unsafe.Pointer(pStr)).FnUsed), uintptr(-libc.Int32FromInt32(1))) _jsonStringTrimOneChar(tls, pStr) } } } } else { Xsqlite3_result_text(tls, ctx, __ccgo_ts+25258, int32(2), libc.UintptrFromInt32(0)) } Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE)) } func _jsonObjectValue(tls *libc.TLS, ctx uintptr) { _jsonObjectCompute(tls, ctx, 0) } func _jsonObjectFinal(tls *libc.TLS, ctx uintptr) { _jsonObjectCompute(tls, ctx, int32(1)) } // C documentation // // /**************************************************************************** // ** The json_each virtual table // ****************************************************************************/ type TJsonParent = struct { F__ccgo_align [0]uint32 FiHead Tu32 FiValue Tu32 FiEnd Tu32 FnPath Tu32 FiKey Ti64 } type JsonParent = TJsonParent type TJsonParent1 = struct { F__ccgo_align [0]uint32 FiHead Tu32 FiValue Tu32 FiEnd Tu32 FnPath Tu32 FiKey Ti64 } type JsonParent1 = TJsonParent1 type TJsonEachCursor = struct { F__ccgo_align [0]uint32 Fbase Tsqlite3_vtab_cursor FiRowid Tu32 Fi Tu32 FiEnd Tu32 FnRoot Tu32 FeType Tu8 FbRecursive Tu8 FnParent Tu32 FnParentAlloc Tu32 FaParent uintptr Fdb uintptr Fpath TJsonString FsParse TJsonParse } type JsonEachCursor = TJsonEachCursor type TJsonEachCursor1 = struct { F__ccgo_align [0]uint32 Fbase Tsqlite3_vtab_cursor FiRowid Tu32 Fi Tu32 FiEnd Tu32 FnRoot Tu32 FeType Tu8 FbRecursive Tu8 FnParent Tu32 FnParentAlloc Tu32 FaParent uintptr Fdb uintptr Fpath TJsonString FsParse TJsonParse } type JsonEachCursor1 = TJsonEachCursor1 type TJsonEachConnection = struct { Fbase Tsqlite3_vtab Fdb uintptr } type JsonEachConnection = TJsonEachConnection type TJsonEachConnection1 = struct { Fbase Tsqlite3_vtab Fdb uintptr } type JsonEachConnection1 = TJsonEachConnection1 // C documentation // // /* Constructor for the json_each virtual table */ func _jsonEachConnect(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) { var pNew uintptr var rc int32 _, _ = pNew, rc /* Column numbers */ /* The xBestIndex method assumes that the JSON and ROOT columns are ** the last two columns in the table. Should this ever changes, be ** sure to update the xBestIndex method. */ _ = pzErr _ = argv _ = argc _ = pAux rc = Xsqlite3_declare_vtab(tls, db, __ccgo_ts+25261) if rc == SQLITE_OK { pNew = _sqlite3DbMallocZero(tls, db, uint64(16)) *(*uintptr)(unsafe.Pointer(ppVtab)) = pNew if pNew == uintptr(0) { return int32(SQLITE_NOMEM) } Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_INNOCUOUS), 0) (*TJsonEachConnection)(unsafe.Pointer(pNew)).Fdb = db } return rc } // C documentation // // /* destructor for json_each virtual table */ func _jsonEachDisconnect(tls *libc.TLS, pVtab uintptr) (r int32) { var p uintptr _ = p p = pVtab _sqlite3DbFree(tls, (*TJsonEachConnection)(unsafe.Pointer(p)).Fdb, pVtab) return SQLITE_OK } // C documentation // // /* constructor for a JsonEachCursor object for json_each(). */ func _jsonEachOpenEach(tls *libc.TLS, p uintptr, ppCursor uintptr) (r int32) { var pCur, pVtab uintptr _, _ = pCur, pVtab pVtab = p _ = p pCur = _sqlite3DbMallocZero(tls, (*TJsonEachConnection)(unsafe.Pointer(pVtab)).Fdb, uint64(224)) if pCur == uintptr(0) { return int32(SQLITE_NOMEM) } (*TJsonEachCursor)(unsafe.Pointer(pCur)).Fdb = (*TJsonEachConnection)(unsafe.Pointer(pVtab)).Fdb _jsonStringZero(tls, pCur+40) *(*uintptr)(unsafe.Pointer(ppCursor)) = pCur return SQLITE_OK } // C documentation // // /* constructor for a JsonEachCursor object for json_tree(). */ func _jsonEachOpenTree(tls *libc.TLS, p uintptr, ppCursor uintptr) (r int32) { var pCur uintptr var rc int32 _, _ = pCur, rc rc = _jsonEachOpenEach(tls, p, ppCursor) if rc == SQLITE_OK { pCur = *(*uintptr)(unsafe.Pointer(ppCursor)) (*TJsonEachCursor)(unsafe.Pointer(pCur)).FbRecursive = uint8(1) } return rc } // C documentation // // /* Reset a JsonEachCursor back to its original state. Free any memory // ** held. */ func _jsonEachCursorReset(tls *libc.TLS, p uintptr) { _jsonParseReset(tls, p+168) _jsonStringReset(tls, p+40) _sqlite3DbFree(tls, (*TJsonEachCursor)(unsafe.Pointer(p)).Fdb, (*TJsonEachCursor)(unsafe.Pointer(p)).FaParent) (*TJsonEachCursor)(unsafe.Pointer(p)).FiRowid = uint32(0) (*TJsonEachCursor)(unsafe.Pointer(p)).Fi = uint32(0) (*TJsonEachCursor)(unsafe.Pointer(p)).FaParent = uintptr(0) (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent = uint32(0) (*TJsonEachCursor)(unsafe.Pointer(p)).FnParentAlloc = uint32(0) (*TJsonEachCursor)(unsafe.Pointer(p)).FiEnd = uint32(0) (*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(0) } // C documentation // // /* Destructor for a jsonEachCursor object */ func _jsonEachClose(tls *libc.TLS, cur uintptr) (r int32) { var p uintptr _ = p p = cur _jsonEachCursorReset(tls, p) _sqlite3DbFree(tls, (*TJsonEachCursor)(unsafe.Pointer(p)).Fdb, cur) return SQLITE_OK } // C documentation // // /* Return TRUE if the jsonEachCursor object has been advanced off the end // ** of the JSON object */ func _jsonEachEof(tls *libc.TLS, cur uintptr) (r int32) { var p uintptr _ = p p = cur return libc.BoolInt32((*TJsonEachCursor)(unsafe.Pointer(p)).Fi >= (*TJsonEachCursor)(unsafe.Pointer(p)).FiEnd) } // C documentation // // /* // ** If the cursor is currently pointing at the label of a object entry, // ** then return the index of the value. For all other cases, return the // ** current pointer position, which is the value. // */ func _jsonSkipLabel(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var n Tu32 var _ /* sz at bp+0 */ Tu32 _ = n if int32((*TJsonEachCursor)(unsafe.Pointer(p)).FeType) == int32(JSONB_OBJECT) { *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) n = _jsonbPayloadSize(tls, p+168, (*TJsonEachCursor)(unsafe.Pointer(p)).Fi, bp) return int32((*TJsonEachCursor)(unsafe.Pointer(p)).Fi + n + *(*Tu32)(unsafe.Pointer(bp))) } else { return int32((*TJsonEachCursor)(unsafe.Pointer(p)).Fi) } return r } // C documentation // // /* // ** Append the path name for the current element. // */ func _jsonAppendPathName(tls *libc.TLS, p uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var i, k, n Tu32 var needQuote int32 var z uintptr var _ /* sz at bp+0 */ Tu32 _, _, _, _, _ = i, k, n, needQuote, z _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if int32((*TJsonEachCursor)(unsafe.Pointer(p)).FeType) == int32(JSONB_ARRAY) { _jsonPrintf(tls, int32(30), p+40, __ccgo_ts+25344, libc.VaList(bp+16, (*(*TJsonParent)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent-uint32(1))*24))).FiKey)) } else { *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) needQuote = 0 n = _jsonbPayloadSize(tls, p+168, (*TJsonEachCursor)(unsafe.Pointer(p)).Fi, bp) k = (*TJsonEachCursor)(unsafe.Pointer(p)).Fi + n z = (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(k) if *(*Tu32)(unsafe.Pointer(bp)) == uint32(0) || !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z))])&libc.Int32FromInt32(0x02) != 0) { needQuote = int32(1) } else { i = uint32(0) for { if !(i < *(*Tu32)(unsafe.Pointer(bp))) { break } if !(int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&libc.Int32FromInt32(0x06) != 0) { needQuote = int32(1) break } goto _1 _1: ; i++ } } if needQuote != 0 { _jsonPrintf(tls, int32(*(*Tu32)(unsafe.Pointer(bp))+uint32(4)), p+40, __ccgo_ts+25351, libc.VaList(bp+16, *(*Tu32)(unsafe.Pointer(bp)), z)) } else { _jsonPrintf(tls, int32(*(*Tu32)(unsafe.Pointer(bp))+uint32(2)), p+40, __ccgo_ts+25359, libc.VaList(bp+16, *(*Tu32)(unsafe.Pointer(bp)), z)) } } } // C documentation // // /* Advance the cursor to the next element for json_tree() */ func _jsonEachNext(tls *libc.TLS, cur uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, i1, iVal, n, n1 Tu32 var levelChange, x Tu8 var nNew Tu64 var p, pNew, pParent, pParent1 uintptr var rc int32 var _ /* sz at bp+0 */ Tu32 var _ /* sz at bp+4 */ Tu32 _, _, _, _, _, _, _, _, _, _, _, _, _ = i, i1, iVal, levelChange, n, n1, nNew, p, pNew, pParent, pParent1, rc, x p = cur rc = SQLITE_OK if (*TJsonEachCursor)(unsafe.Pointer(p)).FbRecursive != 0 { levelChange = uint8(0) *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) i = uint32(_jsonSkipLabel(tls, p)) x = uint8(int32(*(*Tu8)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(i)))) & int32(0x0f)) n = _jsonbPayloadSize(tls, p+168, i, bp) if int32(x) == int32(JSONB_OBJECT) || int32(x) == int32(JSONB_ARRAY) { if (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent >= (*TJsonEachCursor)(unsafe.Pointer(p)).FnParentAlloc { nNew = uint64((*TJsonEachCursor)(unsafe.Pointer(p)).FnParentAlloc*uint32(2) + uint32(3)) pNew = _sqlite3DbRealloc(tls, (*TJsonEachCursor)(unsafe.Pointer(p)).Fdb, (*TJsonEachCursor)(unsafe.Pointer(p)).FaParent, uint64(24)*nNew) if pNew == uintptr(0) { return int32(SQLITE_NOMEM) } (*TJsonEachCursor)(unsafe.Pointer(p)).FnParentAlloc = uint32(nNew) (*TJsonEachCursor)(unsafe.Pointer(p)).FaParent = pNew } levelChange = uint8(1) pParent = (*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent)*24 (*TJsonParent)(unsafe.Pointer(pParent)).FiHead = (*TJsonEachCursor)(unsafe.Pointer(p)).Fi (*TJsonParent)(unsafe.Pointer(pParent)).FiValue = i (*TJsonParent)(unsafe.Pointer(pParent)).FiEnd = i + n + *(*Tu32)(unsafe.Pointer(bp)) (*TJsonParent)(unsafe.Pointer(pParent)).FiKey = int64(-int32(1)) (*TJsonParent)(unsafe.Pointer(pParent)).FnPath = uint32((*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FnUsed) if (*TJsonEachCursor)(unsafe.Pointer(p)).FeType != 0 && (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent != 0 { _jsonAppendPathName(tls, p) if (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FeErr != 0 { rc = int32(SQLITE_NOMEM) } } (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent++ (*TJsonEachCursor)(unsafe.Pointer(p)).Fi = i + n } else { (*TJsonEachCursor)(unsafe.Pointer(p)).Fi = i + n + *(*Tu32)(unsafe.Pointer(bp)) } for (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent > uint32(0) && (*TJsonEachCursor)(unsafe.Pointer(p)).Fi >= (*(*TJsonParent)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent-uint32(1))*24))).FiEnd { (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent-- (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FnUsed = uint64((*(*TJsonParent)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent)*24))).FnPath) levelChange = uint8(1) } if levelChange != 0 { if (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent > uint32(0) { pParent1 = (*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent-uint32(1))*24 iVal = (*TJsonParent)(unsafe.Pointer(pParent1)).FiValue (*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(int32(*(*Tu8)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(iVal)))) & int32(0x0f)) } else { (*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(0) } } } else { *(*Tu32)(unsafe.Pointer(bp + 4)) = uint32(0) i1 = uint32(_jsonSkipLabel(tls, p)) n1 = _jsonbPayloadSize(tls, p+168, i1, bp+4) (*TJsonEachCursor)(unsafe.Pointer(p)).Fi = i1 + n1 + *(*Tu32)(unsafe.Pointer(bp + 4)) } if int32((*TJsonEachCursor)(unsafe.Pointer(p)).FeType) == int32(JSONB_ARRAY) && (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent != 0 { (*(*TJsonParent)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent-uint32(1))*24))).FiKey++ } (*TJsonEachCursor)(unsafe.Pointer(p)).FiRowid++ return rc } // C documentation // // /* Length of the path for rowid==0 in bRecursive mode. // */ func _jsonEachPathLength(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var cSaved uint8 var n, x Tu32 var z uintptr var _ /* sz at bp+0 */ Tu32 _, _, _, _ = cSaved, n, x, z n = uint32((*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FnUsed) z = (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf if (*TJsonEachCursor)(unsafe.Pointer(p)).FiRowid == uint32(0) && (*TJsonEachCursor)(unsafe.Pointer(p)).FbRecursive != 0 && n >= uint32(2) { for n > uint32(1) { n-- if int32(*(*uint8)(unsafe.Pointer(z + uintptr(n)))) == int32('[') || int32(*(*uint8)(unsafe.Pointer(z + uintptr(n)))) == int32('.') { *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) cSaved = *(*uint8)(unsafe.Pointer(z + uintptr(n))) *(*uint8)(unsafe.Pointer(z + uintptr(n))) = uint8(0) _ = libc.Int32FromInt32(0) x = _jsonLookupStep(tls, p+168, uint32(0), z+uintptr(1), uint32(0)) *(*uint8)(unsafe.Pointer(z + uintptr(n))) = cSaved if x >= uint32(JSON_LOOKUP_PATHERROR) { continue } if x+_jsonbPayloadSize(tls, p+168, x, bp) == (*TJsonEachCursor)(unsafe.Pointer(p)).Fi { break } } } } return int32(n) } // C documentation // // /* Return the value of a column */ func _jsonEachColumn(tls *libc.TLS, cur uintptr, ctx uintptr, iColumn int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var eType Tu8 var i, i1, i2, j, n, n1 Tu32 var nBase Tu64 var p uintptr var _ /* x at bp+0 */ Ti64 _, _, _, _, _, _, _, _, _ = eType, i, i1, i2, j, n, n1, nBase, p p = cur switch iColumn { case JEACH_KEY: if (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent == uint32(0) { if (*TJsonEachCursor)(unsafe.Pointer(p)).FnRoot == uint32(1) { break } j = uint32(_jsonEachPathLength(tls, p)) n = (*TJsonEachCursor)(unsafe.Pointer(p)).FnRoot - j if n == uint32(0) { break } else { if int32(*(*uint8)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf + uintptr(j)))) == int32('[') { _sqlite3Atoi64(tls, (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf+uintptr(j+uint32(1)), bp, int32(n-uint32(1)), uint8(SQLITE_UTF8)) Xsqlite3_result_int64(tls, ctx, *(*Ti64)(unsafe.Pointer(bp))) } else { if int32(*(*uint8)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf + uintptr(j+uint32(1))))) == int32('"') { Xsqlite3_result_text(tls, ctx, (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf+uintptr(j+uint32(2)), int32(n-uint32(3)), uintptr(-libc.Int32FromInt32(1))) } else { Xsqlite3_result_text(tls, ctx, (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf+uintptr(j+uint32(1)), int32(n-uint32(1)), uintptr(-libc.Int32FromInt32(1))) } } } break } if int32((*TJsonEachCursor)(unsafe.Pointer(p)).FeType) == int32(JSONB_OBJECT) { _jsonReturnFromBlob(tls, p+168, (*TJsonEachCursor)(unsafe.Pointer(p)).Fi, ctx, int32(1)) } else { _ = libc.Int32FromInt32(0) Xsqlite3_result_int64(tls, ctx, (*(*TJsonParent)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent-uint32(1))*24))).FiKey) } case int32(JEACH_VALUE): i = uint32(_jsonSkipLabel(tls, p)) _jsonReturnFromBlob(tls, p+168, i, ctx, int32(1)) if int32(*(*Tu8)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(i))))&int32(0x0f) >= int32(JSONB_ARRAY) { Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE)) } case int32(JEACH_TYPE): i1 = uint32(_jsonSkipLabel(tls, p)) eType = uint8(int32(*(*Tu8)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(i1)))) & int32(0x0f)) Xsqlite3_result_text(tls, ctx, _jsonbType[eType], -int32(1), libc.UintptrFromInt32(0)) case int32(JEACH_ATOM): i2 = uint32(_jsonSkipLabel(tls, p)) if int32(*(*Tu8)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(i2))))&int32(0x0f) < int32(JSONB_ARRAY) { _jsonReturnFromBlob(tls, p+168, i2, ctx, int32(1)) } case int32(JEACH_ID): Xsqlite3_result_int64(tls, ctx, int64((*TJsonEachCursor)(unsafe.Pointer(p)).Fi)) case int32(JEACH_PARENT): if (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent > uint32(0) && (*TJsonEachCursor)(unsafe.Pointer(p)).FbRecursive != 0 { Xsqlite3_result_int64(tls, ctx, int64((*(*TJsonParent)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent-uint32(1))*24))).FiHead)) } case int32(JEACH_FULLKEY): nBase = (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FnUsed if (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent != 0 { _jsonAppendPathName(tls, p) } Xsqlite3_result_text64(tls, ctx, (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf, (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FnUsed, uintptr(-libc.Int32FromInt32(1)), uint8(SQLITE_UTF8)) (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FnUsed = nBase case int32(JEACH_PATH): n1 = uint32(_jsonEachPathLength(tls, p)) Xsqlite3_result_text64(tls, ctx, (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf, uint64(n1), uintptr(-libc.Int32FromInt32(1)), uint8(SQLITE_UTF8)) default: Xsqlite3_result_text(tls, ctx, (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf, int32((*TJsonEachCursor)(unsafe.Pointer(p)).FnRoot), libc.UintptrFromInt32(0)) case int32(JEACH_JSON): if (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FzJson == uintptr(0) { Xsqlite3_result_blob(tls, ctx, (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob, int32((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FnBlob), uintptr(-libc.Int32FromInt32(1))) } else { Xsqlite3_result_text(tls, ctx, (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FzJson, -int32(1), uintptr(-libc.Int32FromInt32(1))) } break } return SQLITE_OK } // C documentation // // /* Return the current rowid value */ func _jsonEachRowid(tls *libc.TLS, cur uintptr, pRowid uintptr) (r int32) { var p uintptr _ = p p = cur *(*Tsqlite_int64)(unsafe.Pointer(pRowid)) = int64((*TJsonEachCursor)(unsafe.Pointer(p)).FiRowid) return SQLITE_OK } // C documentation // // /* The query strategy is to look for an equality constraint on the json // ** column. Without such a constraint, the table cannot operate. idxNum is // ** 1 if the constraint is found, 3 if the constraint and zRoot are found, // ** and 0 otherwise. // */ func _jsonEachBestIndex(tls *libc.TLS, tab uintptr, pIdxInfo uintptr) (r int32) { var aIdx [2]int32 var i, iCol, iMask, idxMask, unusableMask, v1 int32 var pConstraint uintptr _, _, _, _, _, _, _, _ = aIdx, i, iCol, iMask, idxMask, pConstraint, unusableMask, v1 /* Index of constraints for JSON and ROOT */ unusableMask = 0 /* Mask of unusable JSON and ROOT constraints */ idxMask = 0 /* This implementation assumes that JSON and ROOT are the last two ** columns in the table */ _ = libc.Int32FromInt32(0) _ = tab v1 = -libc.Int32FromInt32(1) aIdx[int32(1)] = v1 aIdx[0] = v1 pConstraint = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint i = 0 for { if !(i < (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint) { break } if (*Tsqlite3_index_constraint)(unsafe.Pointer(pConstraint)).FiColumn < int32(JEACH_JSON) { goto _2 } iCol = (*Tsqlite3_index_constraint)(unsafe.Pointer(pConstraint)).FiColumn - int32(JEACH_JSON) _ = libc.Int32FromInt32(0) iMask = int32(1) << iCol if int32((*Tsqlite3_index_constraint)(unsafe.Pointer(pConstraint)).Fusable) == 0 { unusableMask |= iMask } else { if int32((*Tsqlite3_index_constraint)(unsafe.Pointer(pConstraint)).Fop) == int32(SQLITE_INDEX_CONSTRAINT_EQ) { aIdx[iCol] = i idxMask |= iMask } } goto _2 _2: ; i++ pConstraint += 12 } if (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnOrderBy > 0 && (*(*Tsqlite3_index_orderby)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy))).FiColumn < 0 && int32((*(*Tsqlite3_index_orderby)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy))).Fdesc) == 0 { (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).ForderByConsumed = int32(1) } if unusableMask & ^idxMask != 0 { /* If there are any unusable constraints on JSON or ROOT, then reject ** this entire plan */ return int32(SQLITE_CONSTRAINT) } if aIdx[0] < 0 { /* No JSON input. Leave estimatedCost at the huge value that it was ** initialized to to discourage the query planner from selecting this ** plan. */ (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxNum = 0 } else { (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = float64(1) i = aIdx[0] (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(i)*8))).FargvIndex = int32(1) (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(i)*8))).Fomit = uint8(1) if aIdx[int32(1)] < 0 { (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxNum = int32(1) /* Only JSON supplied. Plan 1 */ } else { i = aIdx[int32(1)] (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(i)*8))).FargvIndex = int32(2) (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(i)*8))).Fomit = uint8(1) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxNum = int32(3) /* Both JSON and ROOT are supplied. Plan 3 */ } } return SQLITE_OK } // C documentation // // /* Start a search on a new JSON string */ func _jsonEachFilter(tls *libc.TLS, cur uintptr, idxNum int32, idxStr uintptr, argc int32, argv uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, n, v1, v3, v5 Tu32 var p, zRoot uintptr var v2, v4, v6 int32 var _ /* sz at bp+0 */ Tu32 _, _, _, _, _, _, _, _, _, _ = i, n, p, zRoot, v1, v2, v3, v4, v5, v6 p = cur zRoot = uintptr(0) _ = idxStr _ = argc _jsonEachCursorReset(tls, p) if idxNum == 0 { return SQLITE_OK } libc.Xmemset(tls, p+168, 0, uint32(56)) (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FnJPRef = uint32(1) (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.Fdb = (*TJsonEachCursor)(unsafe.Pointer(p)).Fdb if _jsonFuncArgMightBeBinary(tls, *(*uintptr)(unsafe.Pointer(argv))) != 0 { (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FnBlob = uint32(Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv)))) (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv))) } else { (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FzJson = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FnJson = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv))) if (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FzJson == uintptr(0) { v1 = libc.Uint32FromInt32(0) (*TJsonEachCursor)(unsafe.Pointer(p)).FiEnd = v1 (*TJsonEachCursor)(unsafe.Pointer(p)).Fi = v1 return SQLITE_OK } if _jsonConvertTextToBlob(tls, p+168, uintptr(0)) != 0 { if (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.Foom != 0 { return int32(SQLITE_NOMEM) } goto json_each_malformed_input } } if idxNum == int32(3) { zRoot = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) if zRoot == uintptr(0) { return SQLITE_OK } if int32(*(*uint8)(unsafe.Pointer(zRoot))) != int32('$') { Xsqlite3_free(tls, (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg) (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg = _jsonBadPathError(tls, uintptr(0), zRoot) _jsonEachCursorReset(tls, p) if (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg != 0 { v2 = int32(SQLITE_ERROR) } else { v2 = int32(SQLITE_NOMEM) } return v2 } (*TJsonEachCursor)(unsafe.Pointer(p)).FnRoot = uint32(_sqlite3Strlen30(tls, zRoot)) if int32(*(*uint8)(unsafe.Pointer(zRoot + 1))) == 0 { v3 = libc.Uint32FromInt32(0) (*TJsonEachCursor)(unsafe.Pointer(p)).Fi = v3 i = v3 (*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(0) } else { i = _jsonLookupStep(tls, p+168, uint32(0), zRoot+uintptr(1), uint32(0)) if i >= uint32(JSON_LOOKUP_PATHERROR) { if i == uint32(JSON_LOOKUP_NOTFOUND) { (*TJsonEachCursor)(unsafe.Pointer(p)).Fi = uint32(0) (*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(0) (*TJsonEachCursor)(unsafe.Pointer(p)).FiEnd = uint32(0) return SQLITE_OK } Xsqlite3_free(tls, (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg) (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg = _jsonBadPathError(tls, uintptr(0), zRoot) _jsonEachCursorReset(tls, p) if (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg != 0 { v4 = int32(SQLITE_ERROR) } else { v4 = int32(SQLITE_NOMEM) } return v4 } if (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FiLabel != 0 { (*TJsonEachCursor)(unsafe.Pointer(p)).Fi = (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FiLabel (*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(JSONB_OBJECT) } else { (*TJsonEachCursor)(unsafe.Pointer(p)).Fi = i (*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(JSONB_ARRAY) } } _jsonAppendRaw(tls, p+40, zRoot, (*TJsonEachCursor)(unsafe.Pointer(p)).FnRoot) } else { v5 = libc.Uint32FromInt32(0) (*TJsonEachCursor)(unsafe.Pointer(p)).Fi = v5 i = v5 (*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(0) (*TJsonEachCursor)(unsafe.Pointer(p)).FnRoot = uint32(1) _jsonAppendRaw(tls, p+40, __ccgo_ts+25365, uint32(1)) } (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent = uint32(0) n = _jsonbPayloadSize(tls, p+168, i, bp) (*TJsonEachCursor)(unsafe.Pointer(p)).FiEnd = i + n + *(*Tu32)(unsafe.Pointer(bp)) if int32(*(*Tu8)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(i))))&int32(0x0f) >= int32(JSONB_ARRAY) && !((*TJsonEachCursor)(unsafe.Pointer(p)).FbRecursive != 0) { (*TJsonEachCursor)(unsafe.Pointer(p)).Fi = i + n (*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(int32(*(*Tu8)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(i)))) & int32(0x0f)) (*TJsonEachCursor)(unsafe.Pointer(p)).FaParent = _sqlite3DbMallocZero(tls, (*TJsonEachCursor)(unsafe.Pointer(p)).Fdb, uint64(24)) if (*TJsonEachCursor)(unsafe.Pointer(p)).FaParent == uintptr(0) { return int32(SQLITE_NOMEM) } (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent = uint32(1) (*TJsonEachCursor)(unsafe.Pointer(p)).FnParentAlloc = uint32(1) (*(*TJsonParent)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent))).FiKey = 0 (*(*TJsonParent)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent))).FiEnd = (*TJsonEachCursor)(unsafe.Pointer(p)).FiEnd (*(*TJsonParent)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent))).FiHead = (*TJsonEachCursor)(unsafe.Pointer(p)).Fi (*(*TJsonParent)(unsafe.Pointer((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent))).FiValue = i } return SQLITE_OK goto json_each_malformed_input json_each_malformed_input: ; Xsqlite3_free(tls, (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg) (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+24936, 0) _jsonEachCursorReset(tls, p) if (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg != 0 { v6 = int32(SQLITE_ERROR) } else { v6 = int32(SQLITE_NOMEM) } return v6 } // C documentation // // /* The methods of the json_each virtual table */ var _jsonEachModule = Tsqlite3_module{} func init() { p := unsafe.Pointer(&_jsonEachModule) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_jsonEachConnect) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_jsonEachBestIndex) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_jsonEachDisconnect) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_jsonEachOpenEach) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_jsonEachClose) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_jsonEachFilter) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_jsonEachNext) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_jsonEachEof) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_jsonEachColumn) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_jsonEachRowid) } // C documentation // // /* The methods of the json_tree virtual table. */ var _jsonTreeModule = Tsqlite3_module{} func init() { p := unsafe.Pointer(&_jsonTreeModule) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_jsonEachConnect) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_jsonEachBestIndex) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_jsonEachDisconnect) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_jsonEachOpenTree) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_jsonEachClose) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_jsonEachFilter) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_jsonEachNext) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_jsonEachEof) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_jsonEachColumn) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_jsonEachRowid) } // C documentation // // /* // ** Register JSON functions. // */ func _sqlite3RegisterJsonFunctions(tls *libc.TLS) { _sqlite3InsertBuiltinFuncs(tls, uintptr(unsafe.Pointer(&_aJsonFunc)), int32(libc.Uint32FromInt64(1280)/libc.Uint32FromInt64(40))) } var _aJsonFunc = [32]TFuncDef{ 0: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25367, }, 1: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)), FzName: __ccgo_ts + 25372, }, 2: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25378, }, 3: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)), FzName: __ccgo_ts + 25389, }, 4: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25401, }, 5: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25401, }, 6: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25419, }, 7: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25439, }, 8: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)), FzName: __ccgo_ts + 25452, }, 9: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FpUserData: uintptr(libc.Int32FromInt32(JSON_JSON) | libc.Int32FromInt32(0)*libc.Int32FromInt32(JSON_BLOB)), FzName: __ccgo_ts + 25466, }, 10: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FpUserData: uintptr(libc.Int32FromInt32(JSON_SQL) | libc.Int32FromInt32(0)*libc.Int32FromInt32(JSON_BLOB)), FzName: __ccgo_ts + 25469, }, 11: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25473, }, 12: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)), FzName: __ccgo_ts + 25485, }, 13: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25498, }, 14: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)), FzName: __ccgo_ts + 25510, }, 15: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25523, }, 16: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)), FzName: __ccgo_ts + 25534, }, 17: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25546, }, 18: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25557, }, 19: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)), FzName: __ccgo_ts + 25569, }, 20: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25582, }, 21: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)), FzName: __ccgo_ts + 25595, }, 22: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FpUserData: uintptr(libc.Int32FromInt32(JSON_ISSET) | libc.Int32FromInt32(0)*libc.Int32FromInt32(JSON_BLOB)), FzName: __ccgo_ts + 25609, }, 23: { FnArg: int8(-int32(1)), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FpUserData: uintptr(libc.Int32FromInt32(JSON_ISSET) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)), FzName: __ccgo_ts + 25618, }, 24: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25628, }, 25: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25628, }, 26: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25638, }, 27: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 25638, }, 28: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DETERMINISTIC)), FzName: __ccgo_ts + 25649, }, 29: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DETERMINISTIC)), FpUserData: uintptr(libc.Int32FromInt32(JSON_BLOB)), FzName: __ccgo_ts + 25666, }, 30: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DETERMINISTIC)), FzName: __ccgo_ts + 25684, }, 31: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DETERMINISTIC)), FpUserData: uintptr(libc.Int32FromInt32(JSON_BLOB)), FzName: __ccgo_ts + 25702, }, } func init() { p := unsafe.Pointer(&_aJsonFunc) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_jsonRemoveFunc) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_jsonRemoveFunc) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_jsonArrayFunc) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_jsonArrayFunc) *(*uintptr)(unsafe.Add(p, 176)) = __ccgo_fp(_jsonArrayLengthFunc) *(*uintptr)(unsafe.Add(p, 216)) = __ccgo_fp(_jsonArrayLengthFunc) *(*uintptr)(unsafe.Add(p, 256)) = __ccgo_fp(_jsonErrorFunc) *(*uintptr)(unsafe.Add(p, 296)) = __ccgo_fp(_jsonExtractFunc) *(*uintptr)(unsafe.Add(p, 336)) = __ccgo_fp(_jsonExtractFunc) *(*uintptr)(unsafe.Add(p, 376)) = __ccgo_fp(_jsonExtractFunc) *(*uintptr)(unsafe.Add(p, 416)) = __ccgo_fp(_jsonExtractFunc) *(*uintptr)(unsafe.Add(p, 456)) = __ccgo_fp(_jsonSetFunc) *(*uintptr)(unsafe.Add(p, 496)) = __ccgo_fp(_jsonSetFunc) *(*uintptr)(unsafe.Add(p, 536)) = __ccgo_fp(_jsonObjectFunc) *(*uintptr)(unsafe.Add(p, 576)) = __ccgo_fp(_jsonObjectFunc) *(*uintptr)(unsafe.Add(p, 616)) = __ccgo_fp(_jsonPatchFunc) *(*uintptr)(unsafe.Add(p, 656)) = __ccgo_fp(_jsonPatchFunc) *(*uintptr)(unsafe.Add(p, 696)) = __ccgo_fp(_jsonQuoteFunc) *(*uintptr)(unsafe.Add(p, 736)) = __ccgo_fp(_jsonRemoveFunc) *(*uintptr)(unsafe.Add(p, 776)) = __ccgo_fp(_jsonRemoveFunc) *(*uintptr)(unsafe.Add(p, 816)) = __ccgo_fp(_jsonReplaceFunc) *(*uintptr)(unsafe.Add(p, 856)) = __ccgo_fp(_jsonReplaceFunc) *(*uintptr)(unsafe.Add(p, 896)) = __ccgo_fp(_jsonSetFunc) *(*uintptr)(unsafe.Add(p, 936)) = __ccgo_fp(_jsonSetFunc) *(*uintptr)(unsafe.Add(p, 976)) = __ccgo_fp(_jsonTypeFunc) *(*uintptr)(unsafe.Add(p, 1016)) = __ccgo_fp(_jsonTypeFunc) *(*uintptr)(unsafe.Add(p, 1056)) = __ccgo_fp(_jsonValidFunc) *(*uintptr)(unsafe.Add(p, 1096)) = __ccgo_fp(_jsonValidFunc) *(*uintptr)(unsafe.Add(p, 1136)) = __ccgo_fp(_jsonArrayStep) *(*uintptr)(unsafe.Add(p, 1140)) = __ccgo_fp(_jsonArrayFinal) *(*uintptr)(unsafe.Add(p, 1144)) = __ccgo_fp(_jsonArrayValue) *(*uintptr)(unsafe.Add(p, 1148)) = __ccgo_fp(_jsonGroupInverse) *(*uintptr)(unsafe.Add(p, 1176)) = __ccgo_fp(_jsonArrayStep) *(*uintptr)(unsafe.Add(p, 1180)) = __ccgo_fp(_jsonArrayFinal) *(*uintptr)(unsafe.Add(p, 1184)) = __ccgo_fp(_jsonArrayValue) *(*uintptr)(unsafe.Add(p, 1188)) = __ccgo_fp(_jsonGroupInverse) *(*uintptr)(unsafe.Add(p, 1216)) = __ccgo_fp(_jsonObjectStep) *(*uintptr)(unsafe.Add(p, 1220)) = __ccgo_fp(_jsonObjectFinal) *(*uintptr)(unsafe.Add(p, 1224)) = __ccgo_fp(_jsonObjectValue) *(*uintptr)(unsafe.Add(p, 1228)) = __ccgo_fp(_jsonGroupInverse) *(*uintptr)(unsafe.Add(p, 1256)) = __ccgo_fp(_jsonObjectStep) *(*uintptr)(unsafe.Add(p, 1260)) = __ccgo_fp(_jsonObjectFinal) *(*uintptr)(unsafe.Add(p, 1264)) = __ccgo_fp(_jsonObjectValue) *(*uintptr)(unsafe.Add(p, 1268)) = __ccgo_fp(_jsonGroupInverse) } // C documentation // // /* // ** Register the JSON table-valued functions // */ func _sqlite3JsonTableFunctions(tls *libc.TLS, db uintptr) (r int32) { var i uint32 var rc int32 _, _ = i, rc rc = SQLITE_OK i = uint32(0) for { if !(i < libc.Uint32FromInt64(16)/libc.Uint32FromInt64(8) && rc == SQLITE_OK) { break } rc = Xsqlite3_create_module(tls, db, _aMod[i].FzName, _aMod[i].FpModule, uintptr(0)) goto _1 _1: ; i++ } return rc } var _aMod = [2]struct { FzName uintptr FpModule uintptr }{ 0: { FzName: __ccgo_ts + 25721, FpModule: uintptr(unsafe.Pointer(&_jsonEachModule)), }, 1: { FzName: __ccgo_ts + 25731, FpModule: uintptr(unsafe.Pointer(&_jsonTreeModule)), }, } /* In the SQLite core */ /* ** If building separately, we will need some setup that is normally ** found in sqliteInt.h */ /* Macro to check for 4-byte alignment. Only used inside of assert() */ /* #include */ /* #include */ /* #include */ /* #include */ /* The following macro is used to suppress compiler warnings. */ type TRtree = struct { F__ccgo_align [0]uint32 Fbase Tsqlite3_vtab Fdb uintptr FiNodeSize int32 FnDim Tu8 FnDim2 Tu8 FeCoordType Tu8 FnBytesPerCell Tu8 FinWrTrans Tu8 FnAux Tu8 FnAuxNotNull Tu8 FiDepth int32 FzDb uintptr FzName uintptr FzNodeName uintptr FnBusy Tu32 FnRowEst Ti64 FnCursor Tu32 FnNodeRef Tu32 FzReadAuxSql uintptr FpDeleted uintptr FpNodeBlob uintptr FpWriteNode uintptr FpDeleteNode uintptr FpReadRowid uintptr FpWriteRowid uintptr FpDeleteRowid uintptr FpReadParent uintptr FpWriteParent uintptr FpDeleteParent uintptr FpWriteAux uintptr FaHash [97]uintptr F__ccgo_pad31 [4]byte } type Rtree = TRtree type TRtreeCursor = struct { F__ccgo_align [0]uint32 Fbase Tsqlite3_vtab_cursor FatEOF Tu8 FbPoint Tu8 FbAuxValid Tu8 FiStrategy int32 FnConstraint int32 FaConstraint uintptr FnPointAlloc int32 FnPoint int32 FmxLevel int32 FaPoint uintptr FpReadAux uintptr FsPoint TRtreeSearchPoint FaNode [5]uintptr FanQueue [41]Tu32 } type RtreeCursor = TRtreeCursor type TRtreeNode = struct { F__ccgo_align [0]uint32 FpParent uintptr F__ccgo_align1 [4]byte FiNode Ti64 FnRef int32 FisDirty int32 FzData uintptr FpNext uintptr } type RtreeNode = TRtreeNode type TRtreeCell = struct { F__ccgo_align [0]uint32 FiRowid Ti64 FaCoord [10]TRtreeCoord } type RtreeCell = TRtreeCell type TRtreeConstraint = struct { F__ccgo_align [0]uint32 FiCoord int32 Fop int32 Fu struct { F__ccgo_align [0]uint32 FxGeom [0]uintptr FxQueryFunc [0]uintptr FrValue TRtreeDValue } FpInfo uintptr F__ccgo_pad4 [4]byte } type RtreeConstraint = TRtreeConstraint type TRtreeMatchArg = struct { F__ccgo_align [0]uint32 FiSize Tu32 Fcb TRtreeGeomCallback FnParam int32 FapSqlParam uintptr F__ccgo_align4 [4]byte FaParam [1]TRtreeDValue } type RtreeMatchArg = TRtreeMatchArg type TRtreeGeomCallback = struct { FxGeom uintptr FxQueryFunc uintptr FxDestructor uintptr FpContext uintptr } type RtreeGeomCallback = TRtreeGeomCallback type TRtreeCoord = struct { Fi [0]int32 Fu [0]Tu32 Ff TRtreeValue } type RtreeCoord = TRtreeCoord type TRtreeSearchPoint = struct { F__ccgo_align [0]uint32 FrScore TRtreeDValue Fid Tsqlite3_int64 FiLevel Tu8 FeWithin Tu8 FiCell Tu8 F__ccgo_pad5 [5]byte } type RtreeSearchPoint = TRtreeSearchPoint /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */ /* Maximum number of auxiliary columns */ /* Size of hash table Rtree.aHash. This hash table is not expected to ** ever contain very many entries, so a fixed number of buckets is ** used. */ /* The xBestIndex method of this virtual table requires an estimate of ** the number of rows in the virtual table to calculate the costs of ** various strategies. If possible, this estimate is loaded from the ** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum). ** Otherwise, if no sqlite_stat1 entry is available, use ** RTREE_DEFAULT_ROWEST. */ /* ** An rtree virtual-table object. */ type TRtree1 = struct { F__ccgo_align [0]uint32 Fbase Tsqlite3_vtab Fdb uintptr FiNodeSize int32 FnDim Tu8 FnDim2 Tu8 FeCoordType Tu8 FnBytesPerCell Tu8 FinWrTrans Tu8 FnAux Tu8 FnAuxNotNull Tu8 FiDepth int32 FzDb uintptr FzName uintptr FzNodeName uintptr FnBusy Tu32 FnRowEst Ti64 FnCursor Tu32 FnNodeRef Tu32 FzReadAuxSql uintptr FpDeleted uintptr FpNodeBlob uintptr FpWriteNode uintptr FpDeleteNode uintptr FpReadRowid uintptr FpWriteRowid uintptr FpDeleteRowid uintptr FpReadParent uintptr FpWriteParent uintptr FpDeleteParent uintptr FpWriteAux uintptr FaHash [97]uintptr F__ccgo_pad31 [4]byte } type Rtree1 = TRtree1 /* Possible values for Rtree.eCoordType: */ // C documentation // // /* // ** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will // ** only deal with integer coordinates. No floating point operations // ** will be done. // */ type TRtreeDValue = float64 type RtreeDValue = TRtreeDValue /* High accuracy coordinate */ type TRtreeValue = float32 type RtreeValue = TRtreeValue /* Low accuracy coordinate */ /* ** Set the Rtree.bCorrupt flag */ /* ** When doing a search of an r-tree, instances of the following structure ** record intermediate results from the tree walk. ** ** The id is always a node-id. For iLevel>=1 the id is the node-id of ** the node that the RtreeSearchPoint represents. When iLevel==0, however, ** the id is of the parent node and the cell that RtreeSearchPoint ** represents is the iCell-th entry in the parent node. */ type TRtreeSearchPoint1 = struct { F__ccgo_align [0]uint32 FrScore TRtreeDValue Fid Tsqlite3_int64 FiLevel Tu8 FeWithin Tu8 FiCell Tu8 F__ccgo_pad5 [5]byte } type RtreeSearchPoint1 = TRtreeSearchPoint1 /* ** The minimum number of cells allowed for a node is a third of the ** maximum. In Gutman's notation: ** ** m = M/3 ** ** If an R*-tree "Reinsert" operation is required, the same number of ** cells are removed from the overfull node and reinserted into the tree. */ /* ** The smallest possible node-size is (512-64)==448 bytes. And the largest ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates). ** Therefore all non-root nodes must contain at least 3 entries. Since ** 3^40 is greater than 2^64, an r-tree structure always has a depth of ** 40 or less. */ /* ** Number of entries in the cursor RtreeNode cache. The first entry is ** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining ** entries cache the RtreeNode for the first elements of the priority queue. */ /* ** An rtree cursor object. */ type TRtreeCursor1 = struct { F__ccgo_align [0]uint32 Fbase Tsqlite3_vtab_cursor FatEOF Tu8 FbPoint Tu8 FbAuxValid Tu8 FiStrategy int32 FnConstraint int32 FaConstraint uintptr FnPointAlloc int32 FnPoint int32 FmxLevel int32 FaPoint uintptr FpReadAux uintptr FsPoint TRtreeSearchPoint FaNode [5]uintptr FanQueue [41]Tu32 } type RtreeCursor1 = TRtreeCursor1 /* Return the Rtree of a RtreeCursor */ /* ** A coordinate can be either a floating point number or a integer. All ** coordinates within a single R-Tree are always of the same time. */ type TRtreeCoord1 = struct { Fi [0]int32 Fu [0]Tu32 Ff TRtreeValue } type RtreeCoord1 = TRtreeCoord1 /* ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord ** formatted as a RtreeDValue (double or int64). This macro assumes that local ** variable pRtree points to the Rtree structure associated with the ** RtreeCoord. */ /* ** A search constraint. */ type TRtreeConstraint1 = struct { F__ccgo_align [0]uint32 FiCoord int32 Fop int32 Fu struct { F__ccgo_align [0]uint32 FxGeom [0]uintptr FxQueryFunc [0]uintptr FrValue TRtreeDValue } FpInfo uintptr F__ccgo_pad4 [4]byte } type RtreeConstraint1 = TRtreeConstraint1 /* Possible values for RtreeConstraint.op */ /* Special operators available only on cursors. Needs to be consecutive ** with the normal values above, but must be less than RTREE_MATCH. These ** are used in the cursor for contraints such as x=NULL (RTREE_FALSE) or ** x<'xyz' (RTREE_TRUE) */ /* ** An rtree structure node. */ type TRtreeNode1 = struct { F__ccgo_align [0]uint32 FpParent uintptr F__ccgo_align1 [4]byte FiNode Ti64 FnRef int32 FisDirty int32 FzData uintptr FpNext uintptr } type RtreeNode1 = TRtreeNode1 /* Return the number of cells in a node */ /* ** A single cell from a node, deserialized */ type TRtreeCell1 = struct { F__ccgo_align [0]uint32 FiRowid Ti64 FaCoord [10]TRtreeCoord } type RtreeCell1 = TRtreeCell1 /* ** This object becomes the sqlite3_user_data() for the SQL functions ** that are created by sqlite3_rtree_geometry_callback() and ** sqlite3_rtree_query_callback() and which appear on the right of MATCH ** operators in order to constrain a search. ** ** xGeom and xQueryFunc are the callback functions. Exactly one of ** xGeom and xQueryFunc fields is non-NULL, depending on whether the ** SQL function was created using sqlite3_rtree_geometry_callback() or ** sqlite3_rtree_query_callback(). ** ** This object is deleted automatically by the destructor mechanism in ** sqlite3_create_function_v2(). */ type TRtreeGeomCallback1 = struct { FxGeom uintptr FxQueryFunc uintptr FxDestructor uintptr FpContext uintptr } type RtreeGeomCallback1 = TRtreeGeomCallback1 /* ** An instance of this structure (in the form of a BLOB) is returned by ** the SQL functions that sqlite3_rtree_geometry_callback() and ** sqlite3_rtree_query_callback() create, and is read as the right-hand ** operand to the MATCH operator of an R-Tree. */ type TRtreeMatchArg1 = struct { F__ccgo_align [0]uint32 FiSize Tu32 Fcb TRtreeGeomCallback FnParam int32 FapSqlParam uintptr F__ccgo_align4 [4]byte FaParam [1]TRtreeDValue } type RtreeMatchArg1 = TRtreeMatchArg1 /* What version of GCC is being used. 0 means GCC is not being used . ** Note that the GCC_VERSION macro will also be set correctly when using ** clang, since clang works hard to be gcc compatible. So the gcc ** optimizations will also work when compiling with clang. */ /* The testcase() macro should already be defined in the amalgamation. If ** it is not, make it a no-op. */ /* ** Make sure that the compiler intrinsics we desire are enabled when ** compiling with an appropriate version of MSVC unless prevented by ** the SQLITE_DISABLE_INTRINSIC define. */ /* ** Macros to determine whether the machine is big or little endian, ** and whether or not that determination is run-time or compile-time. ** ** For best performance, an attempt is made to guess at the byte-order ** using C-preprocessor macros. If that is unsuccessful, or if ** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined ** at run-time. */ /* What version of MSVC is being used. 0 means MSVC is not being used */ // C documentation // // /* // ** Functions to deserialize a 16 bit integer, 32 bit real number and // ** 64 bit integer. The deserialized value is returned. // */ func _readInt16(tls *libc.TLS, p uintptr) (r int32) { return int32(*(*Tu8)(unsafe.Pointer(p)))<> int32(8) & int32(0xFF)) *(*Tu8)(unsafe.Pointer(p + 1)) = uint8(i >> 0 & int32(0xFF)) } func _writeCoord(tls *libc.TLS, p uintptr, pCoord uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* i at bp+0 */ Tu32 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*Tu32)(unsafe.Pointer(bp)) = libc.X__builtin_bswap32(tls, *(*Tu32)(unsafe.Pointer(pCoord))) libc.Xmemcpy(tls, p, bp, uint32(4)) return int32(4) } func _writeInt64(tls *libc.TLS, p uintptr, _i Ti64) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) *(*Ti64)(unsafe.Pointer(bp)) = _i *(*Ti64)(unsafe.Pointer(bp)) = int64(libc.X__builtin_bswap64(tls, uint64(*(*Ti64)(unsafe.Pointer(bp))))) libc.Xmemcpy(tls, p, bp, uint32(8)) return int32(8) } // C documentation // // /* // ** Increment the reference count of node p. // */ func _nodeReference(tls *libc.TLS, p uintptr) { if p != 0 { _ = libc.Int32FromInt32(0) (*TRtreeNode)(unsafe.Pointer(p)).FnRef++ } } // C documentation // // /* // ** Clear the content of node p (set all bytes to 0x00). // */ func _nodeZero(tls *libc.TLS, pRtree uintptr, p uintptr) { libc.Xmemset(tls, (*TRtreeNode)(unsafe.Pointer(p)).FzData+2, 0, uint32((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize-int32(2))) (*TRtreeNode)(unsafe.Pointer(p)).FisDirty = int32(1) } // C documentation // // /* // ** Given a node number iNode, return the corresponding key to use // ** in the Rtree.aHash table. // */ func _nodeHash(tls *libc.TLS, iNode Ti64) (r uint32) { return uint32(iNode) % uint32(HASHSIZE) } // C documentation // // /* // ** Search the node hash table for node iNode. If found, return a pointer // ** to it. Otherwise, return 0. // */ func _nodeHashLookup(tls *libc.TLS, pRtree uintptr, iNode Ti64) (r uintptr) { var p uintptr _ = p p = *(*uintptr)(unsafe.Pointer(pRtree + 112 + uintptr(_nodeHash(tls, iNode))*4)) for { if !(p != 0 && (*TRtreeNode)(unsafe.Pointer(p)).FiNode != iNode) { break } goto _1 _1: ; p = (*TRtreeNode)(unsafe.Pointer(p)).FpNext } return p } // C documentation // // /* // ** Add node pNode to the node hash table. // */ func _nodeHashInsert(tls *libc.TLS, pRtree uintptr, pNode uintptr) { var iHash int32 _ = iHash _ = libc.Int32FromInt32(0) iHash = int32(_nodeHash(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FiNode)) (*TRtreeNode)(unsafe.Pointer(pNode)).FpNext = *(*uintptr)(unsafe.Pointer(pRtree + 112 + uintptr(iHash)*4)) *(*uintptr)(unsafe.Pointer(pRtree + 112 + uintptr(iHash)*4)) = pNode } // C documentation // // /* // ** Remove node pNode from the node hash table. // */ func _nodeHashDelete(tls *libc.TLS, pRtree uintptr, pNode uintptr) { var pp uintptr _ = pp if (*TRtreeNode)(unsafe.Pointer(pNode)).FiNode != 0 { pp = pRtree + 112 + uintptr(_nodeHash(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FiNode))*4 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != pNode) { break } _ = libc.Int32FromInt32(0) goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 28 } *(*uintptr)(unsafe.Pointer(pp)) = (*TRtreeNode)(unsafe.Pointer(pNode)).FpNext (*TRtreeNode)(unsafe.Pointer(pNode)).FpNext = uintptr(0) } } // C documentation // // /* // ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0), // ** indicating that node has not yet been assigned a node number. It is // ** assigned a node number when nodeWrite() is called to write the // ** node contents out to the database. // */ func _nodeNew(tls *libc.TLS, pRtree uintptr, pParent uintptr) (r uintptr) { var pNode uintptr _ = pNode pNode = Xsqlite3_malloc64(tls, uint64(uint32(32)+uint32((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize))) if pNode != 0 { libc.Xmemset(tls, pNode, 0, uint32(32)+uint32((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize)) (*TRtreeNode)(unsafe.Pointer(pNode)).FzData = pNode + 1*32 (*TRtreeNode)(unsafe.Pointer(pNode)).FnRef = int32(1) (*TRtree)(unsafe.Pointer(pRtree)).FnNodeRef++ (*TRtreeNode)(unsafe.Pointer(pNode)).FpParent = pParent (*TRtreeNode)(unsafe.Pointer(pNode)).FisDirty = int32(1) _nodeReference(tls, pParent) } return pNode } // C documentation // // /* // ** Clear the Rtree.pNodeBlob object // */ func _nodeBlobReset(tls *libc.TLS, pRtree uintptr) { var pBlob uintptr _ = pBlob pBlob = (*TRtree)(unsafe.Pointer(pRtree)).FpNodeBlob (*TRtree)(unsafe.Pointer(pRtree)).FpNodeBlob = uintptr(0) Xsqlite3_blob_close(tls, pBlob) } // C documentation // // /* // ** Obtain a reference to an r-tree node. // */ func _nodeAcquire(tls *libc.TLS, pRtree uintptr, iNode Ti64, pParent uintptr, ppNode uintptr) (r int32) { var pBlob, pNode, v1 uintptr var rc int32 _, _, _, _ = pBlob, pNode, rc, v1 rc = SQLITE_OK pNode = uintptr(0) /* Check if the requested node is already in the hash table. If so, ** increase its reference count and return it. */ v1 = _nodeHashLookup(tls, pRtree, iNode) pNode = v1 if v1 != uintptr(0) { if pParent != 0 && pParent != (*TRtreeNode)(unsafe.Pointer(pNode)).FpParent { return libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<iDepth to the height ** of the r-tree structure. A height of zero means all data is stored on ** the root node. A height of one means the children of the root node ** are the leaves, and so on. If the depth as specified on the root node ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt. */ if rc == SQLITE_OK && pNode != 0 && iNode == int64(1) { (*TRtree)(unsafe.Pointer(pRtree)).FiDepth = _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData) if (*TRtree)(unsafe.Pointer(pRtree)).FiDepth > int32(RTREE_MAX_DEPTH) { rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< ((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize-int32(4))/int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell) { rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<iCoord might point to either a lower or upper bound coordinate ** in a coordinate pair. But make pCellData point to the lower bound. */ pCellData += uintptr(int32(8) + int32(4)*((*TRtreeConstraint)(unsafe.Pointer(p)).FiCoord&int32(0xfe))) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) switch (*TRtreeConstraint)(unsafe.Pointer(p)).Fop { case int32(RTREE_TRUE): return /* Always satisfied */ case int32(RTREE_FALSE): case int32(RTREE_EQ): /* Coordinate decoded */ *(*Tu32)(unsafe.Pointer(bp)) = libc.X__builtin_bswap32(tls, *(*Tu32)(unsafe.Pointer(pCellData))) if eInt != 0 { v1 = float64(*(*int32)(unsafe.Pointer(bp))) } else { v1 = float64(*(*TRtreeValue)(unsafe.Pointer(bp))) } val = v1 /* val now holds the lower bound of the coordinate pair */ if *(*TRtreeDValue)(unsafe.Pointer(p + 8)) >= val { pCellData += uintptr(4) /* Coordinate decoded */ *(*Tu32)(unsafe.Pointer(bp + 4)) = libc.X__builtin_bswap32(tls, *(*Tu32)(unsafe.Pointer(pCellData))) if eInt != 0 { v2 = float64(*(*int32)(unsafe.Pointer(bp + 4))) } else { v2 = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 4))) } val = v2 /* val now holds the upper bound of the coordinate pair */ if *(*TRtreeDValue)(unsafe.Pointer(p + 8)) <= val { return } } case int32(RTREE_LE): fallthrough case int32(RTREE_LT): /* Coordinate decoded */ *(*Tu32)(unsafe.Pointer(bp + 8)) = libc.X__builtin_bswap32(tls, *(*Tu32)(unsafe.Pointer(pCellData))) if eInt != 0 { v3 = float64(*(*int32)(unsafe.Pointer(bp + 8))) } else { v3 = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 8))) } val = v3 /* val now holds the lower bound of the coordinate pair */ if *(*TRtreeDValue)(unsafe.Pointer(p + 8)) >= val { return } default: pCellData += uintptr(4) /* Coordinate decoded */ *(*Tu32)(unsafe.Pointer(bp + 12)) = libc.X__builtin_bswap32(tls, *(*Tu32)(unsafe.Pointer(pCellData))) if eInt != 0 { v4 = float64(*(*int32)(unsafe.Pointer(bp + 12))) } else { v4 = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 12))) } val = v4 /* val now holds the upper bound of the coordinate pair */ if *(*TRtreeDValue)(unsafe.Pointer(p + 8)) <= val { return } break } *(*int32)(unsafe.Pointer(peWithin)) = NOT_WITHIN } // C documentation // // /* // ** Check the leaf RTree cell given by pCellData against constraint p. // ** If this constraint is not satisfied, set *peWithin to NOT_WITHIN. // ** If the constraint is satisfied, leave *peWithin unchanged. // ** // ** The constraint is of the form: xN op $val // ** // ** The op is given by p->op. The xN is p->iCoord-th coordinate in // ** pCellData. $val is given by p->u.rValue. // */ func _rtreeLeafConstraint(tls *libc.TLS, p uintptr, eInt int32, pCellData uintptr, peWithin uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var xN TRtreeDValue var v1 Tsqlite3_rtree_dbl var _ /* c at bp+0 */ TRtreeCoord _, _ = xN, v1 /* Coordinate value converted to a double */ _ = libc.Int32FromInt32(0) pCellData += uintptr(int32(8) + (*TRtreeConstraint)(unsafe.Pointer(p)).FiCoord*int32(4)) _ = libc.Int32FromInt32(0) /* Coordinate decoded */ *(*Tu32)(unsafe.Pointer(bp)) = libc.X__builtin_bswap32(tls, *(*Tu32)(unsafe.Pointer(pCellData))) if eInt != 0 { v1 = float64(*(*int32)(unsafe.Pointer(bp))) } else { v1 = float64(*(*TRtreeValue)(unsafe.Pointer(bp))) } xN = v1 switch (*TRtreeConstraint)(unsafe.Pointer(p)).Fop { case int32(RTREE_TRUE): return /* Always satisfied */ case int32(RTREE_FALSE): case int32(RTREE_LE): if xN <= *(*TRtreeDValue)(unsafe.Pointer(p + 8)) { return } case int32(RTREE_LT): if xN < *(*TRtreeDValue)(unsafe.Pointer(p + 8)) { return } case int32(RTREE_GE): if xN >= *(*TRtreeDValue)(unsafe.Pointer(p + 8)) { return } case int32(RTREE_GT): if xN > *(*TRtreeDValue)(unsafe.Pointer(p + 8)) { return } default: if xN == *(*TRtreeDValue)(unsafe.Pointer(p + 8)) { return } break } *(*int32)(unsafe.Pointer(peWithin)) = NOT_WITHIN } // C documentation // // /* // ** One of the cells in node pNode is guaranteed to have a 64-bit // ** integer value equal to iRowid. Return the index of this cell. // */ func _nodeRowidIndex(tls *libc.TLS, pRtree uintptr, pNode uintptr, iRowid Ti64, piIndex uintptr) (r int32) { var ii, nCell int32 _, _ = ii, nCell nCell = _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData+2) _ = libc.Int32FromInt32(0) ii = 0 for { if !(ii < nCell) { break } if _nodeGetRowid(tls, pRtree, pNode, ii) == iRowid { *(*int32)(unsafe.Pointer(piIndex)) = ii return SQLITE_OK } goto _1 _1: ; ii++ } return libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< (*TRtreeSearchPoint)(unsafe.Pointer(pB)).FrScore { return +libc.Int32FromInt32(1) } if int32((*TRtreeSearchPoint)(unsafe.Pointer(pA)).FiLevel) < int32((*TRtreeSearchPoint)(unsafe.Pointer(pB)).FiLevel) { return -int32(1) } if int32((*TRtreeSearchPoint)(unsafe.Pointer(pA)).FiLevel) > int32((*TRtreeSearchPoint)(unsafe.Pointer(pB)).FiLevel) { return +libc.Int32FromInt32(1) } return 0 } // C documentation // // /* // ** Interchange two search points in a cursor. // */ func _rtreeSearchPointSwap(tls *libc.TLS, p uintptr, i int32, j int32) { var pTemp uintptr var t TRtreeSearchPoint _, _ = pTemp, t t = *(*TRtreeSearchPoint)(unsafe.Pointer((*TRtreeCursor)(unsafe.Pointer(p)).FaPoint + uintptr(i)*24)) _ = libc.Int32FromInt32(0) *(*TRtreeSearchPoint)(unsafe.Pointer((*TRtreeCursor)(unsafe.Pointer(p)).FaPoint + uintptr(i)*24)) = *(*TRtreeSearchPoint)(unsafe.Pointer((*TRtreeCursor)(unsafe.Pointer(p)).FaPoint + uintptr(j)*24)) *(*TRtreeSearchPoint)(unsafe.Pointer((*TRtreeCursor)(unsafe.Pointer(p)).FaPoint + uintptr(j)*24)) = t i++ j++ if i < int32(RTREE_CACHE_SZ) { if j >= int32(RTREE_CACHE_SZ) { _nodeRelease(tls, (*TRtreeCursor)(unsafe.Pointer(p)).Fbase.FpVtab, *(*uintptr)(unsafe.Pointer(p + 64 + uintptr(i)*4))) *(*uintptr)(unsafe.Pointer(p + 64 + uintptr(i)*4)) = uintptr(0) } else { pTemp = *(*uintptr)(unsafe.Pointer(p + 64 + uintptr(i)*4)) *(*uintptr)(unsafe.Pointer(p + 64 + uintptr(i)*4)) = *(*uintptr)(unsafe.Pointer(p + 64 + uintptr(j)*4)) *(*uintptr)(unsafe.Pointer(p + 64 + uintptr(j)*4)) = pTemp } } } // C documentation // // /* // ** Return the search point with the lowest current score. // */ func _rtreeSearchPointFirst(tls *libc.TLS, pCur uintptr) (r uintptr) { var v1, v2 uintptr _, _ = v1, v2 if (*TRtreeCursor)(unsafe.Pointer(pCur)).FbPoint != 0 { v1 = pCur + 40 } else { if (*TRtreeCursor)(unsafe.Pointer(pCur)).FnPoint != 0 { v2 = (*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint } else { v2 = uintptr(0) } v1 = v2 } return v1 } // C documentation // // /* // ** Get the RtreeNode for the search point with the lowest score. // */ func _rtreeNodeOfFirstSearchPoint(tls *libc.TLS, pCur uintptr, pRC uintptr) (r uintptr) { var id Tsqlite3_int64 var ii int32 var v1 int64 _, _, _ = id, ii, v1 ii = int32(1) - int32((*TRtreeCursor)(unsafe.Pointer(pCur)).FbPoint) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(pCur + 64 + uintptr(ii)*4)) == uintptr(0) { _ = libc.Int32FromInt32(0) if ii != 0 { v1 = (*(*TRtreeSearchPoint)(unsafe.Pointer((*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint))).Fid } else { v1 = (*TRtreeCursor)(unsafe.Pointer(pCur)).FsPoint.Fid } id = v1 *(*int32)(unsafe.Pointer(pRC)) = _nodeAcquire(tls, (*TRtreeCursor)(unsafe.Pointer(pCur)).Fbase.FpVtab, id, uintptr(0), pCur+64+uintptr(ii)*4) } return *(*uintptr)(unsafe.Pointer(pCur + 64 + uintptr(ii)*4)) } // C documentation // // /* // ** Push a new element onto the priority queue // */ func _rtreeEnqueue(tls *libc.TLS, pCur uintptr, rScore TRtreeDValue, iLevel Tu8) (r uintptr) { var i, j, nNew, v1 int32 var pNew, pParent, v2 uintptr _, _, _, _, _, _, _ = i, j, nNew, pNew, pParent, v1, v2 if (*TRtreeCursor)(unsafe.Pointer(pCur)).FnPoint >= (*TRtreeCursor)(unsafe.Pointer(pCur)).FnPointAlloc { nNew = (*TRtreeCursor)(unsafe.Pointer(pCur)).FnPointAlloc*int32(2) + int32(8) pNew = Xsqlite3_realloc64(tls, (*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint, uint64(uint32(nNew)*uint32(24))) if pNew == uintptr(0) { return uintptr(0) } (*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint = pNew (*TRtreeCursor)(unsafe.Pointer(pCur)).FnPointAlloc = nNew } v2 = pCur + 24 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ i = v1 pNew = (*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint + uintptr(i)*24 (*TRtreeSearchPoint)(unsafe.Pointer(pNew)).FrScore = rScore (*TRtreeSearchPoint)(unsafe.Pointer(pNew)).FiLevel = iLevel _ = libc.Int32FromInt32(0) for i > 0 { j = (i - int32(1)) / int32(2) pParent = (*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint + uintptr(j)*24 if _rtreeSearchPointCompare(tls, pNew, pParent) >= 0 { break } _rtreeSearchPointSwap(tls, pCur, j, i) i = j pNew = pParent } return pNew } // C documentation // // /* // ** Allocate a new RtreeSearchPoint and return a pointer to it. Return // ** NULL if malloc fails. // */ func _rtreeSearchPointNew(tls *libc.TLS, pCur uintptr, rScore TRtreeDValue, iLevel Tu8) (r uintptr) { var ii int32 var pFirst, pNew uintptr _, _, _ = ii, pFirst, pNew pFirst = _rtreeSearchPointFirst(tls, pCur) *(*Tu32)(unsafe.Pointer(pCur + 84 + uintptr(iLevel)*4))++ if pFirst == uintptr(0) || (*TRtreeSearchPoint)(unsafe.Pointer(pFirst)).FrScore > rScore || (*TRtreeSearchPoint)(unsafe.Pointer(pFirst)).FrScore == rScore && int32((*TRtreeSearchPoint)(unsafe.Pointer(pFirst)).FiLevel) > int32(iLevel) { if (*TRtreeCursor)(unsafe.Pointer(pCur)).FbPoint != 0 { pNew = _rtreeEnqueue(tls, pCur, rScore, iLevel) if pNew == uintptr(0) { return uintptr(0) } ii = (int32(pNew)-int32((*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint))/24 + int32(1) _ = libc.Int32FromInt32(0) if ii < int32(RTREE_CACHE_SZ) { _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pCur + 64 + uintptr(ii)*4)) = *(*uintptr)(unsafe.Pointer(pCur + 64)) } else { _nodeRelease(tls, (*TRtreeCursor)(unsafe.Pointer(pCur)).Fbase.FpVtab, *(*uintptr)(unsafe.Pointer(pCur + 64))) } *(*uintptr)(unsafe.Pointer(pCur + 64)) = uintptr(0) *(*TRtreeSearchPoint)(unsafe.Pointer(pNew)) = (*TRtreeCursor)(unsafe.Pointer(pCur)).FsPoint } (*TRtreeCursor)(unsafe.Pointer(pCur)).FsPoint.FrScore = rScore (*TRtreeCursor)(unsafe.Pointer(pCur)).FsPoint.FiLevel = iLevel (*TRtreeCursor)(unsafe.Pointer(pCur)).FbPoint = uint8(1) return pCur + 40 } else { return _rtreeEnqueue(tls, pCur, rScore, iLevel) } return r } // C documentation // // /* Remove the search point with the lowest current score. // */ func _rtreeSearchPointPop(tls *libc.TLS, p uintptr) { var i, j, k, n, v1, v3 int32 var v2 uintptr _, _, _, _, _, _, _ = i, j, k, n, v1, v2, v3 i = int32(1) - int32((*TRtreeCursor)(unsafe.Pointer(p)).FbPoint) _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(p + 64 + uintptr(i)*4)) != 0 { _nodeRelease(tls, (*TRtreeCursor)(unsafe.Pointer(p)).Fbase.FpVtab, *(*uintptr)(unsafe.Pointer(p + 64 + uintptr(i)*4))) *(*uintptr)(unsafe.Pointer(p + 64 + uintptr(i)*4)) = uintptr(0) } if (*TRtreeCursor)(unsafe.Pointer(p)).FbPoint != 0 { *(*Tu32)(unsafe.Pointer(p + 84 + uintptr((*TRtreeCursor)(unsafe.Pointer(p)).FsPoint.FiLevel)*4))-- (*TRtreeCursor)(unsafe.Pointer(p)).FbPoint = uint8(0) } else { if (*TRtreeCursor)(unsafe.Pointer(p)).FnPoint != 0 { *(*Tu32)(unsafe.Pointer(p + 84 + uintptr((*(*TRtreeSearchPoint)(unsafe.Pointer((*TRtreeCursor)(unsafe.Pointer(p)).FaPoint))).FiLevel)*4))-- v2 = p + 24 *(*int32)(unsafe.Pointer(v2))-- v1 = *(*int32)(unsafe.Pointer(v2)) n = v1 *(*TRtreeSearchPoint)(unsafe.Pointer((*TRtreeCursor)(unsafe.Pointer(p)).FaPoint)) = *(*TRtreeSearchPoint)(unsafe.Pointer((*TRtreeCursor)(unsafe.Pointer(p)).FaPoint + uintptr(n)*24)) if n < libc.Int32FromInt32(RTREE_CACHE_SZ)-libc.Int32FromInt32(1) { *(*uintptr)(unsafe.Pointer(p + 64 + 1*4)) = *(*uintptr)(unsafe.Pointer(p + 64 + uintptr(n+int32(1))*4)) *(*uintptr)(unsafe.Pointer(p + 64 + uintptr(n+int32(1))*4)) = uintptr(0) } i = 0 for { v3 = i*libc.Int32FromInt32(2) + libc.Int32FromInt32(1) j = v3 if !(v3 < n) { break } k = j + int32(1) if k < n && _rtreeSearchPointCompare(tls, (*TRtreeCursor)(unsafe.Pointer(p)).FaPoint+uintptr(k)*24, (*TRtreeCursor)(unsafe.Pointer(p)).FaPoint+uintptr(j)*24) < 0 { if _rtreeSearchPointCompare(tls, (*TRtreeCursor)(unsafe.Pointer(p)).FaPoint+uintptr(k)*24, (*TRtreeCursor)(unsafe.Pointer(p)).FaPoint+uintptr(i)*24) < 0 { _rtreeSearchPointSwap(tls, p, i, k) i = k } else { break } } else { if _rtreeSearchPointCompare(tls, (*TRtreeCursor)(unsafe.Pointer(p)).FaPoint+uintptr(j)*24, (*TRtreeCursor)(unsafe.Pointer(p)).FaPoint+uintptr(i)*24) < 0 { _rtreeSearchPointSwap(tls, p, i, j) i = j } else { break } } } } } } // C documentation // // /* // ** Continue the search on cursor pCur until the front of the queue // ** contains an entry suitable for returning as a result-set row, // ** or until the RtreeSearchPoint queue is empty, indicating that the // ** query has completed. // */ func _rtreeStepToLeaf(tls *libc.TLS, pCur uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var eInt, ii, nCell, nConstraint int32 var p, pCellData, pConstraint, pNode, pRtree, v1 uintptr var x TRtreeSearchPoint var _ /* eWithin at bp+0 */ int32 var _ /* rScore at bp+8 */ Tsqlite3_rtree_dbl var _ /* rc at bp+4 */ int32 _, _, _, _, _, _, _, _, _, _, _ = eInt, ii, nCell, nConstraint, p, pCellData, pConstraint, pNode, pRtree, x, v1 pRtree = (*TRtreeCursor)(unsafe.Pointer(pCur)).Fbase.FpVtab *(*int32)(unsafe.Pointer(bp + 4)) = SQLITE_OK nConstraint = (*TRtreeCursor)(unsafe.Pointer(pCur)).FnConstraint eInt = libc.BoolInt32(int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == int32(RTREE_COORD_INT32)) for { v1 = _rtreeSearchPointFirst(tls, pCur) p = v1 if !(v1 != uintptr(0) && int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiLevel) > 0) { break } pNode = _rtreeNodeOfFirstSearchPoint(tls, pCur, bp+4) if *(*int32)(unsafe.Pointer(bp + 4)) != 0 { return *(*int32)(unsafe.Pointer(bp + 4)) } nCell = _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData+2) _ = libc.Int32FromInt32(0) pCellData = (*TRtreeNode)(unsafe.Pointer(pNode)).FzData + uintptr(libc.Int32FromInt32(4)+int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell)*int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell)) for int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell) < nCell { *(*Tsqlite3_rtree_dbl)(unsafe.Pointer(bp + 8)) = float64(-libc.Int32FromInt32(1)) *(*int32)(unsafe.Pointer(bp)) = int32(FULLY_WITHIN) ii = 0 for { if !(ii < nConstraint) { break } pConstraint = (*TRtreeCursor)(unsafe.Pointer(pCur)).FaConstraint + uintptr(ii)*24 if (*TRtreeConstraint)(unsafe.Pointer(pConstraint)).Fop >= int32(RTREE_MATCH) { *(*int32)(unsafe.Pointer(bp + 4)) = _rtreeCallbackConstraint(tls, pConstraint, eInt, pCellData, p, bp+8, bp) if *(*int32)(unsafe.Pointer(bp + 4)) != 0 { return *(*int32)(unsafe.Pointer(bp + 4)) } } else { if int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiLevel) == int32(1) { _rtreeLeafConstraint(tls, pConstraint, eInt, pCellData, bp) } else { _rtreeNonleafConstraint(tls, pConstraint, eInt, pCellData, bp) } } if *(*int32)(unsafe.Pointer(bp)) == NOT_WITHIN { (*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell++ pCellData += uintptr((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell) break } goto _2 _2: ; ii++ } if *(*int32)(unsafe.Pointer(bp)) == NOT_WITHIN { continue } (*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell++ x.FiLevel = uint8(int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiLevel) - int32(1)) if x.FiLevel != 0 { x.Fid = _readInt64(tls, pCellData) ii = 0 for { if !(ii < (*TRtreeCursor)(unsafe.Pointer(pCur)).FnPoint) { break } if (*(*TRtreeSearchPoint)(unsafe.Pointer((*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint + uintptr(ii)*24))).Fid == x.Fid { return libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<= nCell { _rtreeSearchPointPop(tls, pCur) } if *(*Tsqlite3_rtree_dbl)(unsafe.Pointer(bp + 8)) < float64(0) { *(*Tsqlite3_rtree_dbl)(unsafe.Pointer(bp + 8)) = float64(0) } p = _rtreeSearchPointNew(tls, pCur, *(*Tsqlite3_rtree_dbl)(unsafe.Pointer(bp + 8)), x.FiLevel) if p == uintptr(0) { return int32(SQLITE_NOMEM) } (*TRtreeSearchPoint)(unsafe.Pointer(p)).FeWithin = uint8(*(*int32)(unsafe.Pointer(bp))) (*TRtreeSearchPoint)(unsafe.Pointer(p)).Fid = x.Fid (*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell = x.FiCell break } if int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell) >= nCell { _rtreeSearchPointPop(tls, pCur) } } (*TRtreeCursor)(unsafe.Pointer(pCur)).FatEOF = libc.BoolUint8(p == uintptr(0)) return SQLITE_OK } // C documentation // // /* // ** Rtree virtual table module xNext method. // */ func _rtreeNext(tls *libc.TLS, pVtabCursor uintptr) (r int32) { var pCsr uintptr var rc int32 _, _ = pCsr, rc pCsr = pVtabCursor rc = SQLITE_OK /* Move to the next entry that matches the configured constraints. */ if (*TRtreeCursor)(unsafe.Pointer(pCsr)).FbAuxValid != 0 { (*TRtreeCursor)(unsafe.Pointer(pCsr)).FbAuxValid = uint8(0) Xsqlite3_reset(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux) } _rtreeSearchPointPop(tls, pCsr) rc = _rtreeStepToLeaf(tls, pCsr) return rc } // C documentation // // /* // ** Rtree virtual table module xRowid method. // */ func _rtreeRowid(tls *libc.TLS, pVtabCursor uintptr, pRowid uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var p, pCsr, pNode uintptr var _ /* rc at bp+0 */ int32 _, _, _ = p, pCsr, pNode pCsr = pVtabCursor p = _rtreeSearchPointFirst(tls, pCsr) *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK pNode = _rtreeNodeOfFirstSearchPoint(tls, pCsr, bp) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && p != 0 { if int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell) >= _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData+2) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ABORT) } else { *(*Tsqlite_int64)(unsafe.Pointer(pRowid)) = _nodeGetRowid(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab, pNode, int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell)) } } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Rtree virtual table module xColumn method. // */ func _rtreeColumn(tls *libc.TLS, cur uintptr, ctx uintptr, i int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var p, pCsr, pNode, pRtree uintptr var _ /* c at bp+0 */ TRtreeCoord var _ /* rc at bp+4 */ int32 _, _, _, _ = p, pCsr, pNode, pRtree pRtree = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab pCsr = cur p = _rtreeSearchPointFirst(tls, pCsr) *(*int32)(unsafe.Pointer(bp + 4)) = SQLITE_OK pNode = _rtreeNodeOfFirstSearchPoint(tls, pCsr, bp+4) if *(*int32)(unsafe.Pointer(bp + 4)) != 0 { return *(*int32)(unsafe.Pointer(bp + 4)) } if p == uintptr(0) { return SQLITE_OK } if int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell) >= _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData+2) { return int32(SQLITE_ABORT) } if i == 0 { Xsqlite3_result_int64(tls, ctx, _nodeGetRowid(tls, pRtree, pNode, int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell))) } else { if i <= int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) { _nodeGetCoord(tls, pRtree, pNode, int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell), i-int32(1), bp) if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { Xsqlite3_result_double(tls, ctx, float64(*(*TRtreeValue)(unsafe.Pointer(bp)))) } else { _ = libc.Int32FromInt32(0) Xsqlite3_result_int(tls, ctx, *(*int32)(unsafe.Pointer(bp))) } } else { if !((*TRtreeCursor)(unsafe.Pointer(pCsr)).FbAuxValid != 0) { if (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux == uintptr(0) { *(*int32)(unsafe.Pointer(bp + 4)) = Xsqlite3_prepare_v3(tls, (*TRtree)(unsafe.Pointer(pRtree)).Fdb, (*TRtree)(unsafe.Pointer(pRtree)).FzReadAuxSql, -int32(1), uint32(0), pCsr+36, uintptr(0)) if *(*int32)(unsafe.Pointer(bp + 4)) != 0 { return *(*int32)(unsafe.Pointer(bp + 4)) } } Xsqlite3_bind_int64(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux, int32(1), _nodeGetRowid(tls, pRtree, pNode, int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell))) *(*int32)(unsafe.Pointer(bp + 4)) = Xsqlite3_step(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux) if *(*int32)(unsafe.Pointer(bp + 4)) == int32(SQLITE_ROW) { (*TRtreeCursor)(unsafe.Pointer(pCsr)).FbAuxValid = uint8(1) } else { Xsqlite3_reset(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux) if *(*int32)(unsafe.Pointer(bp + 4)) == int32(SQLITE_DONE) { *(*int32)(unsafe.Pointer(bp + 4)) = SQLITE_OK } return *(*int32)(unsafe.Pointer(bp + 4)) } } Xsqlite3_result_value(tls, ctx, Xsqlite3_column_value(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux, i-int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)+int32(1))) } } return SQLITE_OK } // C documentation // // /* // ** Use nodeAcquire() to obtain the leaf node containing the record with // ** rowid iRowid. If successful, set *ppLeaf to point to the node and // ** return SQLITE_OK. If there is no such record in the table, set // ** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf // ** to zero and return an SQLite error code. // */ func _findLeafNode(tls *libc.TLS, pRtree uintptr, iRowid Ti64, ppLeaf uintptr, piNode uintptr) (r int32) { var iNode Ti64 var rc int32 _, _ = iNode, rc *(*uintptr)(unsafe.Pointer(ppLeaf)) = uintptr(0) Xsqlite3_bind_int64(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid, int32(1), iRowid) if Xsqlite3_step(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid) == int32(SQLITE_ROW) { iNode = Xsqlite3_column_int64(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid, 0) if piNode != 0 { *(*Tsqlite3_int64)(unsafe.Pointer(piNode)) = iNode } rc = _nodeAcquire(tls, pRtree, iNode, uintptr(0), ppLeaf) Xsqlite3_reset(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid) } else { rc = Xsqlite3_reset(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid) } return rc } // C documentation // // /* // ** This function is called to configure the RtreeConstraint object passed // ** as the second argument for a MATCH constraint. The value passed as the // ** first argument to this function is the right-hand operand to the MATCH // ** operator. // */ func _deserializeGeometry(tls *libc.TLS, pValue uintptr, pCons uintptr) (r int32) { var pBlob, pInfo, pSrc uintptr _, _, _ = pBlob, pInfo, pSrc /* Callback information */ pSrc = Xsqlite3_value_pointer(tls, pValue, __ccgo_ts+25828) if pSrc == uintptr(0) { return int32(SQLITE_ERROR) } pInfo = Xsqlite3_malloc64(tls, uint64(uint32(80)+(*TRtreeMatchArg)(unsafe.Pointer(pSrc)).FiSize)) if !(pInfo != 0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, pInfo, 0, uint32(80)) pBlob = pInfo + 1*80 libc.Xmemcpy(tls, pBlob, pSrc, (*TRtreeMatchArg)(unsafe.Pointer(pSrc)).FiSize) (*Tsqlite3_rtree_query_info)(unsafe.Pointer(pInfo)).FpContext = (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).Fcb.FpContext (*Tsqlite3_rtree_query_info)(unsafe.Pointer(pInfo)).FnParam = (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FnParam (*Tsqlite3_rtree_query_info)(unsafe.Pointer(pInfo)).FaParam = pBlob + 32 (*Tsqlite3_rtree_query_info)(unsafe.Pointer(pInfo)).FapSqlParam = (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FapSqlParam if (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).Fcb.FxGeom != 0 { *(*uintptr)(unsafe.Pointer(&(*TRtreeConstraint)(unsafe.Pointer(pCons)).Fu)) = (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).Fcb.FxGeom } else { (*TRtreeConstraint)(unsafe.Pointer(pCons)).Fop = int32(RTREE_QUERY) *(*uintptr)(unsafe.Pointer(&(*TRtreeConstraint)(unsafe.Pointer(pCons)).Fu)) = (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).Fcb.FxQueryFunc } (*TRtreeConstraint)(unsafe.Pointer(pCons)).FpInfo = pInfo return SQLITE_OK } // C documentation // // /* // ** Rtree virtual table module xFilter method. // */ func _rtreeFilter(tls *libc.TLS, pVtabCursor uintptr, idxNum int32, idxStr uintptr, argc int32, argv uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var eType, eType1, ii, rc int32 var iRowid Ti64 var iVal Tsqlite3_int64 var p, p1, pCsr, pNew, pRtree uintptr var _ /* iCell at bp+4 */ int32 var _ /* iNode at bp+16 */ Ti64 var _ /* pLeaf at bp+8 */ uintptr var _ /* pRoot at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _ = eType, eType1, iRowid, iVal, ii, p, p1, pCsr, pNew, pRtree, rc pRtree = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVtabCursor)).FpVtab pCsr = pVtabCursor *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) rc = SQLITE_OK *(*int32)(unsafe.Pointer(bp + 4)) = 0 _rtreeReference(tls, pRtree) /* Reset the cursor to the same state as rtreeOpen() leaves it in. */ _resetCursor(tls, pCsr) (*TRtreeCursor)(unsafe.Pointer(pCsr)).FiStrategy = idxNum if idxNum == int32(1) { /* Search point for the leaf */ iRowid = Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv))) *(*Ti64)(unsafe.Pointer(bp + 16)) = 0 eType = Xsqlite3_value_numeric_type(tls, *(*uintptr)(unsafe.Pointer(argv))) if eType == int32(SQLITE_INTEGER) || eType == int32(SQLITE_FLOAT) && Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv))) == float64(iRowid) { rc = _findLeafNode(tls, pRtree, iRowid, bp+8, bp+16) } else { rc = SQLITE_OK *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) } if rc == SQLITE_OK && *(*uintptr)(unsafe.Pointer(bp + 8)) != uintptr(0) { p = _rtreeSearchPointNew(tls, pCsr, float64(0), uint8(0)) _ = libc.Int32FromInt32(0) /* Always returns pCsr->sPoint */ *(*uintptr)(unsafe.Pointer(pCsr + 64)) = *(*uintptr)(unsafe.Pointer(bp + 8)) (*TRtreeSearchPoint)(unsafe.Pointer(p)).Fid = *(*Ti64)(unsafe.Pointer(bp + 16)) (*TRtreeSearchPoint)(unsafe.Pointer(p)).FeWithin = uint8(PARTLY_WITHIN) rc = _nodeRowidIndex(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 8)), iRowid, bp+4) (*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell = uint8(*(*int32)(unsafe.Pointer(bp + 4))) } else { (*TRtreeCursor)(unsafe.Pointer(pCsr)).FatEOF = uint8(1) } } else { /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array ** with the configured constraints. */ rc = _nodeAcquire(tls, pRtree, int64(1), uintptr(0), bp) if rc == SQLITE_OK && argc > 0 { (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint = Xsqlite3_malloc64(tls, uint64(uint32(24)*uint32(argc))) (*TRtreeCursor)(unsafe.Pointer(pCsr)).FnConstraint = argc if !((*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint != 0) { rc = int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint, 0, uint32(24)*uint32(argc)) libc.Xmemset(tls, pCsr+84, 0, uint32(4)*uint32((*TRtree)(unsafe.Pointer(pRtree)).FiDepth+libc.Int32FromInt32(1))) _ = libc.Int32FromInt32(0) ii = 0 for { if !(ii < argc) { break } p1 = (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint + uintptr(ii)*24 eType1 = Xsqlite3_value_numeric_type(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(ii)*4))) (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32(*(*uint8)(unsafe.Pointer(idxStr + uintptr(ii*int32(2))))) (*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(*(*uint8)(unsafe.Pointer(idxStr + uintptr(ii*int32(2)+int32(1))))) - int32('0') if (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop >= int32(RTREE_MATCH) { /* A MATCH operator. The right-hand-side must be a blob that ** can be cast into an RtreeMatchArg object. One created using ** an sqlite3_rtree_geometry_callback() SQL user function. */ rc = _deserializeGeometry(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(ii)*4)), p1) if rc != SQLITE_OK { break } (*Tsqlite3_rtree_query_info)(unsafe.Pointer((*TRtreeConstraint)(unsafe.Pointer(p1)).FpInfo)).FnCoord = int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) (*Tsqlite3_rtree_query_info)(unsafe.Pointer((*TRtreeConstraint)(unsafe.Pointer(p1)).FpInfo)).FanQueue = pCsr + 84 (*Tsqlite3_rtree_query_info)(unsafe.Pointer((*TRtreeConstraint)(unsafe.Pointer(p1)).FpInfo)).FmxLevel = (*TRtree)(unsafe.Pointer(pRtree)).FiDepth + int32(1) } else { if eType1 == int32(SQLITE_INTEGER) { iVal = Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(ii)*4))) *(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(iVal) if iVal >= libc.Int64FromInt32(1)<bPoint was FALSE */ return int32(SQLITE_NOMEM) } (*TRtreeSearchPoint)(unsafe.Pointer(pNew)).Fid = int64(1) (*TRtreeSearchPoint)(unsafe.Pointer(pNew)).FiCell = uint8(0) (*TRtreeSearchPoint)(unsafe.Pointer(pNew)).FeWithin = uint8(PARTLY_WITHIN) _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pCsr + 64)) = *(*uintptr)(unsafe.Pointer(bp)) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) rc = _rtreeStepToLeaf(tls, pCsr) } } _nodeRelease(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp))) _rtreeRelease(tls, pRtree) return rc } // C documentation // // /* // ** Rtree virtual table module xBestIndex method. There are three // ** table scan strategies to choose from (in order from most to // ** least desirable): // ** // ** idxNum idxStr Strategy // ** ------------------------------------------------ // ** 1 Unused Direct lookup by rowid. // ** 2 See below R-tree query or full-table scan. // ** ------------------------------------------------ // ** // ** If strategy 1 is used, then idxStr is not meaningful. If strategy // ** 2 is used, idxStr is formatted to contain 2 bytes for each // ** constraint used. The first two bytes of idxStr correspond to // ** the constraint in sqlite3_index_info.aConstraintUsage[] with // ** (argvIndex==1) etc. // ** // ** The first of each pair of bytes in idxStr identifies the constraint // ** operator as follows: // ** // ** Operator Byte Value // ** ---------------------- // ** = 0x41 ('A') // ** <= 0x42 ('B') // ** < 0x43 ('C') // ** >= 0x44 ('D') // ** > 0x45 ('E') // ** MATCH 0x46 ('F') // ** ---------------------- // ** // ** The second of each pair of bytes identifies the coordinate column // ** to which the constraint applies. The leftmost coordinate column // ** is 'a', the second from the left 'b' etc. // */ func _rtreeBestIndex(tls *libc.TLS, tab uintptr, pIdxInfo uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var bMatch, iIdx, ii, jj, rc, v4, v5 int32 var doOmit, op Tu8 var nRow Ti64 var p, pRtree uintptr var _ /* zIdxStr at bp+0 */ [41]uint8 _, _, _, _, _, _, _, _, _, _, _, _ = bMatch, doOmit, iIdx, ii, jj, nRow, op, p, pRtree, rc, v4, v5 pRtree = tab rc = SQLITE_OK bMatch = 0 /* Estimated rows returned by this scan */ iIdx = 0 libc.Xmemset(tls, bp, 0, uint32(41)) /* Check if there exists a MATCH constraint - even an unusable one. If there ** is, do not consider the lookup-by-rowid plan as using such a plan would ** require the VDBE to evaluate the MATCH constraint, which is not currently ** possible. */ ii = 0 for { if !(ii < (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint) { break } if int32((*(*Tsqlite3_index_constraint)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(ii)*12))).Fop) == int32(SQLITE_INDEX_CONSTRAINT_MATCH) { bMatch = int32(1) } goto _1 _1: ; ii++ } _ = libc.Int32FromInt32(0) ii = 0 for { if !(ii < (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint && iIdx < int32(libc.Uint32FromInt64(41)-libc.Uint32FromInt32(1))) { break } p = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(ii)*12 if bMatch == 0 && (*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fusable != 0 && (*Tsqlite3_index_constraint)(unsafe.Pointer(p)).FiColumn <= 0 && int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INDEX_CONSTRAINT_EQ) { jj = 0 for { if !(jj < ii) { break } (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(jj)*8))).FargvIndex = 0 (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(jj)*8))).Fomit = uint8(0) goto _3 _3: ; jj++ } (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxNum = int32(1) (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(ii)*8))).FargvIndex = int32(1) (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(jj)*8))).Fomit = uint8(1) /* This strategy involves a two rowid lookups on an B-Tree structures ** and then a linear search of an R-Tree node. This should be ** considered almost as quick as a direct rowid lookup (for which ** sqlite uses an internal cost of 0.0). It is expected to return ** a single row. */ (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = float64(30) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = int64(1) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxFlags = int32(SQLITE_INDEX_SCAN_UNIQUE) return SQLITE_OK } if (*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fusable != 0 && ((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).FiColumn > 0 && (*Tsqlite3_index_constraint)(unsafe.Pointer(p)).FiColumn <= int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) || int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INDEX_CONSTRAINT_MATCH)) { doOmit = uint8(1) switch int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) { case int32(SQLITE_INDEX_CONSTRAINT_EQ): op = uint8(RTREE_EQ) doOmit = uint8(0) case int32(SQLITE_INDEX_CONSTRAINT_GT): op = uint8(RTREE_GT) doOmit = uint8(0) case int32(SQLITE_INDEX_CONSTRAINT_LE): op = uint8(RTREE_LE) case int32(SQLITE_INDEX_CONSTRAINT_LT): op = uint8(RTREE_LT) doOmit = uint8(0) case int32(SQLITE_INDEX_CONSTRAINT_GE): op = uint8(RTREE_GE) case int32(SQLITE_INDEX_CONSTRAINT_MATCH): op = uint8(RTREE_MATCH) default: op = uint8(0) break } if op != 0 { v4 = iIdx iIdx++ (*(*[41]uint8)(unsafe.Pointer(bp)))[v4] = op v5 = iIdx iIdx++ (*(*[41]uint8)(unsafe.Pointer(bp)))[v5] = uint8((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).FiColumn - libc.Int32FromInt32(1) + libc.Int32FromUint8('0')) (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(ii)*8))).FargvIndex = iIdx / int32(2) (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(ii)*8))).Fomit = doOmit } } goto _2 _2: ; ii++ } (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxNum = int32(2) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FneedToFreeIdxStr = int32(1) if iIdx > 0 { (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxStr = Xsqlite3_malloc(tls, iIdx+int32(1)) if (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxStr == uintptr(0) { return int32(SQLITE_NOMEM) } libc.Xmemcpy(tls, (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxStr, bp, uint32(iIdx+int32(1))) } nRow = (*TRtree)(unsafe.Pointer(pRtree)).FnRowEst >> (iIdx / int32(2)) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = libc.Float64FromFloat64(6) * float64(nRow) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = nRow return rc } // C documentation // // /* // ** Return the N-dimensional volumn of the cell stored in *p. // */ func _cellArea(tls *libc.TLS, pRtree uintptr, p uintptr) (r TRtreeDValue) { var area TRtreeDValue _ = area area = libc.Float64FromInt32(1) _ = libc.Int32FromInt32(0) if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { switch int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim) { case int32(5): area = float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + 9*4)) - *(*TRtreeValue)(unsafe.Pointer(p + 8 + 8*4))) fallthrough case int32(4): area *= float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + 7*4)) - *(*TRtreeValue)(unsafe.Pointer(p + 8 + 6*4))) fallthrough case int32(3): area *= float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + 5*4)) - *(*TRtreeValue)(unsafe.Pointer(p + 8 + 4*4))) fallthrough case int32(2): area *= float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + 3*4)) - *(*TRtreeValue)(unsafe.Pointer(p + 8 + 2*4))) fallthrough default: area *= float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + 1*4)) - *(*TRtreeValue)(unsafe.Pointer(p + 8))) } } else { switch int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim) { case int32(5): area = float64(int64(*(*int32)(unsafe.Pointer(p + 8 + 9*4))) - int64(*(*int32)(unsafe.Pointer(p + 8 + 8*4)))) fallthrough case int32(4): area *= float64(int64(*(*int32)(unsafe.Pointer(p + 8 + 7*4))) - int64(*(*int32)(unsafe.Pointer(p + 8 + 6*4)))) fallthrough case int32(3): area *= float64(int64(*(*int32)(unsafe.Pointer(p + 8 + 5*4))) - int64(*(*int32)(unsafe.Pointer(p + 8 + 4*4)))) fallthrough case int32(2): area *= float64(int64(*(*int32)(unsafe.Pointer(p + 8 + 3*4))) - int64(*(*int32)(unsafe.Pointer(p + 8 + 2*4)))) fallthrough default: area *= float64(int64(*(*int32)(unsafe.Pointer(p + 8 + 1*4))) - int64(*(*int32)(unsafe.Pointer(p + 8)))) } } return area } // C documentation // // /* // ** Return the margin length of cell p. The margin length is the sum // ** of the objects size in each dimension. // */ func _cellMargin(tls *libc.TLS, pRtree uintptr, p uintptr) (r TRtreeDValue) { var ii int32 var margin TRtreeDValue var v1, v2 float64 _, _, _, _ = ii, margin, v1, v2 margin = libc.Float64FromInt32(0) ii = int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) - int32(2) for cond := true; cond; cond = ii >= 0 { if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { v1 = float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + uintptr(ii+int32(1))*4))) } else { v1 = float64(*(*int32)(unsafe.Pointer(p + 8 + uintptr(ii+int32(1))*4))) } if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { v2 = float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + uintptr(ii)*4))) } else { v2 = float64(*(*int32)(unsafe.Pointer(p + 8 + uintptr(ii)*4))) } margin += v1 - v2 ii -= int32(2) } return margin } // C documentation // // /* // ** Store the union of cells p1 and p2 in p1. // */ func _cellUnion(tls *libc.TLS, pRtree uintptr, p1 uintptr, p2 uintptr) { var ii, v3, v4 int32 var v1, v2 TRtreeValue _, _, _, _, _ = ii, v1, v2, v3, v4 ii = 0 if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { for cond := true; cond; cond = ii < int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) { if *(*TRtreeValue)(unsafe.Pointer(p1 + 8 + uintptr(ii)*4)) < *(*TRtreeValue)(unsafe.Pointer(p2 + 8 + uintptr(ii)*4)) { v1 = *(*TRtreeValue)(unsafe.Pointer(p1 + 8 + uintptr(ii)*4)) } else { v1 = *(*TRtreeValue)(unsafe.Pointer(p2 + 8 + uintptr(ii)*4)) } *(*TRtreeValue)(unsafe.Pointer(p1 + 8 + uintptr(ii)*4)) = v1 if *(*TRtreeValue)(unsafe.Pointer(p1 + 8 + uintptr(ii+int32(1))*4)) > *(*TRtreeValue)(unsafe.Pointer(p2 + 8 + uintptr(ii+int32(1))*4)) { v2 = *(*TRtreeValue)(unsafe.Pointer(p1 + 8 + uintptr(ii+int32(1))*4)) } else { v2 = *(*TRtreeValue)(unsafe.Pointer(p2 + 8 + uintptr(ii+int32(1))*4)) } *(*TRtreeValue)(unsafe.Pointer(p1 + 8 + uintptr(ii+int32(1))*4)) = v2 ii += int32(2) } } else { for cond := true; cond; cond = ii < int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) { if *(*int32)(unsafe.Pointer(p1 + 8 + uintptr(ii)*4)) < *(*int32)(unsafe.Pointer(p2 + 8 + uintptr(ii)*4)) { v3 = *(*int32)(unsafe.Pointer(p1 + 8 + uintptr(ii)*4)) } else { v3 = *(*int32)(unsafe.Pointer(p2 + 8 + uintptr(ii)*4)) } *(*int32)(unsafe.Pointer(p1 + 8 + uintptr(ii)*4)) = v3 if *(*int32)(unsafe.Pointer(p1 + 8 + uintptr(ii+int32(1))*4)) > *(*int32)(unsafe.Pointer(p2 + 8 + uintptr(ii+int32(1))*4)) { v4 = *(*int32)(unsafe.Pointer(p1 + 8 + uintptr(ii+int32(1))*4)) } else { v4 = *(*int32)(unsafe.Pointer(p2 + 8 + uintptr(ii+int32(1))*4)) } *(*int32)(unsafe.Pointer(p1 + 8 + uintptr(ii+int32(1))*4)) = v4 ii += int32(2) } } } // C documentation // // /* // ** Return true if the area covered by p2 is a subset of the area covered // ** by p1. False otherwise. // */ func _cellContains(tls *libc.TLS, pRtree uintptr, p1 uintptr, p2 uintptr) (r int32) { var a1, a11, a2, a21 uintptr var ii int32 _, _, _, _, _ = a1, a11, a2, a21, ii if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == int32(RTREE_COORD_INT32) { ii = 0 for { if !(ii < int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)) { break } a1 = p1 + 8 + uintptr(ii)*4 a2 = p2 + 8 + uintptr(ii)*4 if *(*int32)(unsafe.Pointer(a2)) < *(*int32)(unsafe.Pointer(a1)) || *(*int32)(unsafe.Pointer(a2 + 1*4)) > *(*int32)(unsafe.Pointer(a1 + 1*4)) { return 0 } goto _1 _1: ; ii += int32(2) } } else { ii = 0 for { if !(ii < int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)) { break } a11 = p1 + 8 + uintptr(ii)*4 a21 = p2 + 8 + uintptr(ii)*4 if *(*TRtreeValue)(unsafe.Pointer(a21)) < *(*TRtreeValue)(unsafe.Pointer(a11)) || *(*TRtreeValue)(unsafe.Pointer(a21 + 1*4)) > *(*TRtreeValue)(unsafe.Pointer(a11 + 1*4)) { return 0 } goto _2 _2: ; ii += int32(2) } } return int32(1) } func _cellOverlap(tls *libc.TLS, pRtree uintptr, p uintptr, aCell uintptr, nCell int32) (r TRtreeDValue) { var ii, jj int32 var o, overlap, x1, x2 TRtreeDValue var v10, v11, v12, v3, v4, v5, v6, v7, v8, v9 float64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = ii, jj, o, overlap, x1, x2, v10, v11, v12, v3, v4, v5, v6, v7, v8, v9 overlap = float64(0) ii = 0 for { if !(ii < nCell) { break } o = libc.Float64FromInt32(1) jj = 0 for { if !(jj < int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)) { break } if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { v4 = float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + uintptr(jj)*4))) } else { v4 = float64(*(*int32)(unsafe.Pointer(p + 8 + uintptr(jj)*4))) } if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { v5 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj)*4))) } else { v5 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj)*4))) } if v4 > v5 { if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { v6 = float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + uintptr(jj)*4))) } else { v6 = float64(*(*int32)(unsafe.Pointer(p + 8 + uintptr(jj)*4))) } v3 = v6 } else { if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { v7 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj)*4))) } else { v7 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj)*4))) } v3 = v7 } x1 = v3 if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { v9 = float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + uintptr(jj+int32(1))*4))) } else { v9 = float64(*(*int32)(unsafe.Pointer(p + 8 + uintptr(jj+int32(1))*4))) } if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { v10 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj+int32(1))*4))) } else { v10 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj+int32(1))*4))) } if v9 < v10 { if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { v11 = float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + uintptr(jj+int32(1))*4))) } else { v11 = float64(*(*int32)(unsafe.Pointer(p + 8 + uintptr(jj+int32(1))*4))) } v8 = v11 } else { if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { v12 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj+int32(1))*4))) } else { v12 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj+int32(1))*4))) } v8 = v12 } x2 = v8 if x2 < x1 { o = libc.Float64FromInt32(0) break } else { o = o * (x2 - x1) } goto _2 _2: ; jj += int32(2) } overlap += o goto _1 _1: ; ii++ } return overlap } // C documentation // // /* // ** This function implements the ChooseLeaf algorithm from Gutman[84]. // ** ChooseSubTree in r*tree terminology. // */ func _ChooseLeaf(tls *libc.TLS, pRtree uintptr, pCell uintptr, iHeight int32, ppLeaf uintptr) (r int32) { bp := tls.Alloc(112) defer tls.Free(112) var area, area1, fMinArea, fMinGrowth, growth TRtreeDValue var bFound, iCell, ii, nCell, rc int32 var iBest Tsqlite3_int64 var _ /* cell at bp+56 */ TRtreeCell var _ /* cell at bp+8 */ TRtreeCell var _ /* pChild at bp+4 */ uintptr var _ /* pNode at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _ = area, area1, bFound, fMinArea, fMinGrowth, growth, iBest, iCell, ii, nCell, rc *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) rc = _nodeAcquire(tls, pRtree, int64(1), uintptr(0), bp) ii = 0 for { if !(rc == SQLITE_OK && ii < (*TRtree)(unsafe.Pointer(pRtree)).FiDepth-iHeight) { break } iBest = 0 bFound = 0 fMinGrowth = float64(0) fMinArea = float64(0) nCell = _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FzData+2) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) /* First check to see if there is are any cells in pNode that completely ** contains pCell. If two or more cells in pNode completely contain pCell ** then pick the smallest. */ iCell = 0 for { if !(iCell < nCell) { break } _nodeGetCell(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp)), iCell, bp+8) if _cellContains(tls, pRtree, bp+8, pCell) != 0 { area = _cellArea(tls, pRtree, bp+8) if bFound == 0 || area < fMinArea { iBest = (*(*TRtreeCell)(unsafe.Pointer(bp + 8))).FiRowid fMinArea = area bFound = int32(1) } } goto _2 _2: ; iCell++ } if !(bFound != 0) { /* No cells of pNode will completely contain pCell. So pick the ** cell of pNode that grows by the least amount when pCell is added. ** Break ties by selecting the smaller cell. */ iCell = 0 for { if !(iCell < nCell) { break } _nodeGetCell(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp)), iCell, bp+56) area1 = _cellArea(tls, pRtree, bp+56) _cellUnion(tls, pRtree, bp+56, pCell) growth = _cellArea(tls, pRtree, bp+56) - area1 if iCell == 0 || growth < fMinGrowth || growth == fMinGrowth && area1 < fMinArea { fMinGrowth = growth fMinArea = area1 iBest = (*(*TRtreeCell)(unsafe.Pointer(bp + 56))).FiRowid } goto _3 _3: ; iCell++ } } rc = _nodeAcquire(tls, pRtree, iBest, *(*uintptr)(unsafe.Pointer(bp)), bp+4) _nodeRelease(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = *(*uintptr)(unsafe.Pointer(bp + 4)) goto _1 _1: ; ii++ } *(*uintptr)(unsafe.Pointer(ppLeaf)) = *(*uintptr)(unsafe.Pointer(bp)) return rc } // C documentation // // /* // ** A cell with the same content as pCell has just been inserted into // ** the node pNode. This function updates the bounding box cells in // ** all ancestor elements. // */ func _AdjustTree(tls *libc.TLS, pRtree uintptr, pNode uintptr, pCell uintptr) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var cnt, rc int32 var p, pParent uintptr var _ /* cell at bp+0 */ TRtreeCell var _ /* iCell at bp+48 */ int32 _, _, _, _ = cnt, p, pParent, rc p = pNode cnt = 0 for (*TRtreeNode)(unsafe.Pointer(p)).FpParent != 0 { pParent = (*TRtreeNode)(unsafe.Pointer(p)).FpParent cnt++ if cnt > int32(100) { return libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<iNode) to the _rowid table. // */ func _rowidWrite(tls *libc.TLS, pRtree uintptr, iRowid Tsqlite3_int64, iNode Tsqlite3_int64) (r int32) { Xsqlite3_bind_int64(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpWriteRowid, int32(1), iRowid) Xsqlite3_bind_int64(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpWriteRowid, int32(2), iNode) Xsqlite3_step(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpWriteRowid) return Xsqlite3_reset(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpWriteRowid) } // C documentation // // /* // ** Write mapping (iNode->iPar) to the _parent table. // */ func _parentWrite(tls *libc.TLS, pRtree uintptr, iNode Tsqlite3_int64, iPar Tsqlite3_int64) (r int32) { Xsqlite3_bind_int64(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpWriteParent, int32(1), iNode) Xsqlite3_bind_int64(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpWriteParent, int32(2), iPar) Xsqlite3_step(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpWriteParent) return Xsqlite3_reset(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpWriteParent) } // C documentation // // /* // ** Arguments aIdx, aCell and aSpare all point to arrays of size // ** nIdx. The aIdx array contains the set of integers from 0 to // ** (nIdx-1) in no particular order. This function sorts the values // ** in aIdx according to dimension iDim of the cells in aCell. The // ** minimum value of dimension iDim is considered first, the // ** maximum used to break ties. // ** // ** The aSpare array is used as temporary working space by the // ** sorting algorithm. // */ func _SortByDimension(tls *libc.TLS, pRtree uintptr, aIdx uintptr, nIdx int32, iDim int32, aCell uintptr, aSpare uintptr) { var aLeft, aRight uintptr var iLeft, iRight, nLeft, nRight int32 var xleft1, xleft2, xright1, xright2 TRtreeDValue var v1, v2, v3, v4 float64 _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aLeft, aRight, iLeft, iRight, nLeft, nRight, xleft1, xleft2, xright1, xright2, v1, v2, v3, v4 if nIdx > int32(1) { iLeft = 0 iRight = 0 nLeft = nIdx / int32(2) nRight = nIdx - nLeft aLeft = aIdx aRight = aIdx + uintptr(nLeft)*4 _SortByDimension(tls, pRtree, aLeft, nLeft, iDim, aCell, aSpare) _SortByDimension(tls, pRtree, aRight, nRight, iDim, aCell, aSpare) libc.Xmemcpy(tls, aSpare, aLeft, uint32(4)*uint32(nLeft)) aLeft = aSpare for iLeft < nLeft || iRight < nRight { if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { v1 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(*(*int32)(unsafe.Pointer(aLeft + uintptr(iLeft)*4)))*48 + 8 + uintptr(iDim*int32(2))*4))) } else { v1 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(*(*int32)(unsafe.Pointer(aLeft + uintptr(iLeft)*4)))*48 + 8 + uintptr(iDim*int32(2))*4))) } xleft1 = v1 if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { v2 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(*(*int32)(unsafe.Pointer(aLeft + uintptr(iLeft)*4)))*48 + 8 + uintptr(iDim*int32(2)+int32(1))*4))) } else { v2 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(*(*int32)(unsafe.Pointer(aLeft + uintptr(iLeft)*4)))*48 + 8 + uintptr(iDim*int32(2)+int32(1))*4))) } xleft2 = v2 if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { v3 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(*(*int32)(unsafe.Pointer(aRight + uintptr(iRight)*4)))*48 + 8 + uintptr(iDim*int32(2))*4))) } else { v3 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(*(*int32)(unsafe.Pointer(aRight + uintptr(iRight)*4)))*48 + 8 + uintptr(iDim*int32(2))*4))) } xright1 = v3 if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { v4 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(*(*int32)(unsafe.Pointer(aRight + uintptr(iRight)*4)))*48 + 8 + uintptr(iDim*int32(2)+int32(1))*4))) } else { v4 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(*(*int32)(unsafe.Pointer(aRight + uintptr(iRight)*4)))*48 + 8 + uintptr(iDim*int32(2)+int32(1))*4))) } xright2 = v4 if iLeft != nLeft && (iRight == nRight || xleft1 < xright1 || xleft1 == xright1 && xleft2 < xright2) { *(*int32)(unsafe.Pointer(aIdx + uintptr(iLeft+iRight)*4)) = *(*int32)(unsafe.Pointer(aLeft + uintptr(iLeft)*4)) iLeft++ } else { *(*int32)(unsafe.Pointer(aIdx + uintptr(iLeft+iRight)*4)) = *(*int32)(unsafe.Pointer(aRight + uintptr(iRight)*4)) iRight++ } } } } // C documentation // // /* // ** Implementation of the R*-tree variant of SplitNode from Beckman[1990]. // */ func _splitNodeStartree(tls *libc.TLS, pRtree uintptr, aCell uintptr, nCell int32, pLeft uintptr, pRight uintptr, pBboxLeft uintptr, pBboxRight uintptr) (r int32) { bp := tls.Alloc(96) defer tls.Free(96) var aSpare, aaSorted, pBbox, pCell, pTarget, v7, v8 uintptr var area, fBestArea, fBestMargin, fBestOverlap, margin, overlap TRtreeDValue var iBestDim, iBestLeft, iBestSplit, ii, jj, kk, nLeft int32 var nByte Tsqlite3_int64 var _ /* left at bp+0 */ TRtreeCell var _ /* right at bp+48 */ TRtreeCell _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aSpare, aaSorted, area, fBestArea, fBestMargin, fBestOverlap, iBestDim, iBestLeft, iBestSplit, ii, jj, kk, margin, nByte, nLeft, overlap, pBbox, pCell, pTarget, v7, v8 iBestDim = 0 iBestSplit = 0 fBestMargin = float64(0) nByte = int64(uint32(int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim)+libc.Int32FromInt32(1)) * (uint32(4) + uint32(nCell)*uint32(4))) aaSorted = Xsqlite3_malloc64(tls, uint64(nByte)) if !(aaSorted != 0) { return int32(SQLITE_NOMEM) } aSpare = aaSorted + uintptr((*TRtree)(unsafe.Pointer(pRtree)).FnDim)*4 + uintptr(int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim)*nCell)*4 libc.Xmemset(tls, aaSorted, 0, uint32(nByte)) ii = 0 for { if !(ii < int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim)) { break } *(*uintptr)(unsafe.Pointer(aaSorted + uintptr(ii)*4)) = aaSorted + uintptr((*TRtree)(unsafe.Pointer(pRtree)).FnDim)*4 + uintptr(ii*nCell)*4 jj = 0 for { if !(jj < nCell) { break } *(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aaSorted + uintptr(ii)*4)) + uintptr(jj)*4)) = jj goto _2 _2: ; jj++ } _SortByDimension(tls, pRtree, *(*uintptr)(unsafe.Pointer(aaSorted + uintptr(ii)*4)), nCell, ii, aCell, aSpare) goto _1 _1: ; ii++ } ii = 0 for { if !(ii < int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim)) { break } margin = float64(0) fBestOverlap = float64(0) fBestArea = float64(0) iBestLeft = 0 nLeft = ((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize - int32(4)) / int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell) / int32(3) for { if !(nLeft <= nCell-((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize-int32(4))/int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell)/int32(3)) { break } libc.Xmemcpy(tls, bp, aCell+uintptr(*(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aaSorted + uintptr(ii)*4)))))*48, uint32(48)) libc.Xmemcpy(tls, bp+48, aCell+uintptr(*(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aaSorted + uintptr(ii)*4)) + uintptr(nCell-int32(1))*4)))*48, uint32(48)) kk = int32(1) for { if !(kk < nCell-int32(1)) { break } if kk < nLeft { _cellUnion(tls, pRtree, bp, aCell+uintptr(*(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aaSorted + uintptr(ii)*4)) + uintptr(kk)*4)))*48) } else { _cellUnion(tls, pRtree, bp+48, aCell+uintptr(*(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aaSorted + uintptr(ii)*4)) + uintptr(kk)*4)))*48) } goto _5 _5: ; kk++ } margin += _cellMargin(tls, pRtree, bp) margin += _cellMargin(tls, pRtree, bp+48) overlap = _cellOverlap(tls, pRtree, bp, bp+48, int32(1)) area = _cellArea(tls, pRtree, bp) + _cellArea(tls, pRtree, bp+48) if nLeft == ((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize-int32(4))/int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell)/int32(3) || overlap < fBestOverlap || overlap == fBestOverlap && area < fBestArea { iBestLeft = nLeft fBestOverlap = overlap fBestArea = area } goto _4 _4: ; nLeft++ } if ii == 0 || margin < fBestMargin { iBestDim = ii fBestMargin = margin iBestSplit = iBestLeft } goto _3 _3: ; ii++ } libc.Xmemcpy(tls, pBboxLeft, aCell+uintptr(*(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aaSorted + uintptr(iBestDim)*4)))))*48, uint32(48)) libc.Xmemcpy(tls, pBboxRight, aCell+uintptr(*(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aaSorted + uintptr(iBestDim)*4)) + uintptr(iBestSplit)*4)))*48, uint32(48)) ii = 0 for { if !(ii < nCell) { break } if ii < iBestSplit { v7 = pLeft } else { v7 = pRight } pTarget = v7 if ii < iBestSplit { v8 = pBboxLeft } else { v8 = pBboxRight } pBbox = v8 pCell = aCell + uintptr(*(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aaSorted + uintptr(iBestDim)*4)) + uintptr(ii)*4)))*48 _nodeInsertCell(tls, pRtree, pTarget, pCell) _cellUnion(tls, pRtree, pBbox, pCell) goto _6 _6: ; ii++ } Xsqlite3_free(tls, aaSorted) return SQLITE_OK } func _updateMapping(tls *libc.TLS, pRtree uintptr, iRowid Ti64, pNode uintptr, iHeight int32) (r int32) { var p, pChild, xSetMapping, v1 uintptr _, _, _, _ = p, pChild, xSetMapping, v1 if iHeight == 0 { v1 = __ccgo_fp(_rowidWrite) } else { v1 = __ccgo_fp(_parentWrite) } xSetMapping = v1 if iHeight > 0 { pChild = _nodeHashLookup(tls, pRtree, iRowid) p = pNode for { if !(p != 0) { break } if p == pChild { return libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<pParent chain all the way up to the root node. // ** // ** This operation is required when a row is deleted (or updated - an update // ** is implemented as a delete followed by an insert). SQLite provides the // ** rowid of the row to delete, which can be used to find the leaf on which // ** the entry resides (argument pLeaf). Once the leaf is located, this // ** function is called to determine its ancestry. // */ func _fixLeafParent(tls *libc.TLS, pRtree uintptr, pLeaf uintptr) (r int32) { var iNode Ti64 var pChild, pTest uintptr var rc, rc2 int32 _, _, _, _, _ = iNode, pChild, pTest, rc, rc2 rc = SQLITE_OK pChild = pLeaf for rc == SQLITE_OK && (*TRtreeNode)(unsafe.Pointer(pChild)).FiNode != int64(1) && (*TRtreeNode)(unsafe.Pointer(pChild)).FpParent == uintptr(0) { rc2 = SQLITE_OK /* sqlite3_reset() return code */ Xsqlite3_bind_int64(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadParent, int32(1), (*TRtreeNode)(unsafe.Pointer(pChild)).FiNode) rc = Xsqlite3_step(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadParent) if rc == int32(SQLITE_ROW) { /* Node number of parent node */ /* Before setting pChild->pParent, test that we are not creating a ** loop of references (as we would if, say, pChild==pParent). We don't ** want to do this as it leads to a memory leak when trying to delete ** the referenced counted node structures. */ iNode = Xsqlite3_column_int64(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadParent, 0) pTest = pLeaf for { if !(pTest != 0 && (*TRtreeNode)(unsafe.Pointer(pTest)).FiNode != iNode) { break } goto _1 _1: ; pTest = (*TRtreeNode)(unsafe.Pointer(pTest)).FpParent } if pTest == uintptr(0) { rc2 = _nodeAcquire(tls, pRtree, iNode, uintptr(0), pChild) } } rc = Xsqlite3_reset(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadParent) if rc == SQLITE_OK { rc = rc2 } if rc == SQLITE_OK && !((*TRtreeNode)(unsafe.Pointer(pChild)).FpParent != 0) { rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< 0 { pChild = _nodeHashLookup(tls, pRtree, (*TRtreeCell)(unsafe.Pointer(pCell)).FiRowid) if pChild != 0 { _nodeRelease(tls, pRtree, (*TRtreeNode)(unsafe.Pointer(pChild)).FpParent) _nodeReference(tls, pNode) (*TRtreeNode)(unsafe.Pointer(pChild)).FpParent = pNode } } if _nodeInsertCell(tls, pRtree, pNode, pCell) != 0 { rc = _SplitNode(tls, pRtree, pNode, pCell, iHeight) } else { rc = _AdjustTree(tls, pRtree, pNode, pCell) if rc == SQLITE_OK { if iHeight == 0 { rc = _rowidWrite(tls, pRtree, (*TRtreeCell)(unsafe.Pointer(pCell)).FiRowid, (*TRtreeNode)(unsafe.Pointer(pNode)).FiNode) } else { rc = _parentWrite(tls, pRtree, (*TRtreeCell)(unsafe.Pointer(pCell)).FiRowid, (*TRtreeNode)(unsafe.Pointer(pNode)).FiNode) } } } return rc } func _reinsertNodeContent(tls *libc.TLS, pRtree uintptr, pNode uintptr) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var ii, nCell, rc, rc2 int32 var _ /* cell at bp+8 */ TRtreeCell var _ /* pInsert at bp+0 */ uintptr _, _, _, _ = ii, nCell, rc, rc2 rc = SQLITE_OK nCell = _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData+2) ii = 0 for { if !(rc == SQLITE_OK && ii < nCell) { break } _nodeGetCell(tls, pRtree, pNode, ii, bp+8) /* Find a node to store this cell in. pNode->iNode currently contains ** the height of the sub-tree headed by the cell. */ rc = _ChooseLeaf(tls, pRtree, bp+8, int32((*TRtreeNode)(unsafe.Pointer(pNode)).FiNode), bp) if rc == SQLITE_OK { rc = _rtreeInsertCell(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp)), bp+8, int32((*TRtreeNode)(unsafe.Pointer(pNode)).FiNode)) rc2 = _nodeRelease(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp))) if rc == SQLITE_OK { rc = rc2 } } goto _1 _1: ; ii++ } return rc } // C documentation // // /* // ** Select a currently unused rowid for a new r-tree record. // */ func _rtreeNewRowid(tls *libc.TLS, pRtree uintptr, piRowid uintptr) (r int32) { var rc int32 _ = rc Xsqlite3_bind_null(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpWriteRowid, int32(1)) Xsqlite3_bind_null(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpWriteRowid, int32(2)) Xsqlite3_step(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpWriteRowid) rc = Xsqlite3_reset(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpWriteRowid) *(*Ti64)(unsafe.Pointer(piRowid)) = Xsqlite3_last_insert_rowid(tls, (*TRtree)(unsafe.Pointer(pRtree)).Fdb) return rc } // C documentation // // /* // ** Remove the entry with rowid=iDelete from the r-tree structure. // */ func _rtreeDeleteRowid(tls *libc.TLS, pRtree uintptr, iDelete Tsqlite3_int64) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iChild Ti64 var rc, rc2, rc21 int32 var _ /* iCell at bp+4 */ int32 var _ /* pChild at bp+12 */ uintptr var _ /* pLeaf at bp+0 */ uintptr var _ /* pRoot at bp+8 */ uintptr _, _, _, _ = iChild, rc, rc2, rc21 /* Return code */ *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Index of iDelete cell in pLeaf */ *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) /* Root node of rtree structure */ /* Obtain a reference to the root node to initialize Rtree.iDepth */ rc = _nodeAcquire(tls, pRtree, int64(1), uintptr(0), bp+8) /* Obtain a reference to the leaf node that contains the entry ** about to be deleted. */ if rc == SQLITE_OK { rc = _findLeafNode(tls, pRtree, iDelete, bp, uintptr(0)) } _ = libc.Int32FromInt32(0) /* Delete the cell in question from the leaf node. */ if rc == SQLITE_OK && *(*uintptr)(unsafe.Pointer(bp)) != 0 { rc = _nodeRowidIndex(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp)), iDelete, bp+4) if rc == SQLITE_OK { rc = _deleteCell(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp)), *(*int32)(unsafe.Pointer(bp + 4)), 0) } rc2 = _nodeRelease(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp))) if rc == SQLITE_OK { rc = rc2 } } /* Delete the corresponding entry in the _rowid table. */ if rc == SQLITE_OK { Xsqlite3_bind_int64(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpDeleteRowid, int32(1), iDelete) Xsqlite3_step(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpDeleteRowid) rc = Xsqlite3_reset(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpDeleteRowid) } /* Check if the root node now has exactly one child. If so, remove ** it, schedule the contents of the child for reinsertion and ** reduce the tree height by one. ** ** This is equivalent to copying the contents of the child into ** the root node (the operation that Gutman's paper says to perform ** in this scenario). */ if rc == SQLITE_OK && (*TRtree)(unsafe.Pointer(pRtree)).FiDepth > 0 && _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FzData+2) == int32(1) { *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) iChild = _nodeGetRowid(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 8)), 0) rc = _nodeAcquire(tls, pRtree, iChild, *(*uintptr)(unsafe.Pointer(bp + 8)), bp+12) /* tag-20210916a */ if rc == SQLITE_OK { rc = _removeNode(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 12)), (*TRtree)(unsafe.Pointer(pRtree)).FiDepth-int32(1)) } rc21 = _nodeRelease(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 12))) if rc == SQLITE_OK { rc = rc21 } if rc == SQLITE_OK { (*TRtree)(unsafe.Pointer(pRtree)).FiDepth-- _writeInt16(tls, (*TRtreeNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FzData, (*TRtree)(unsafe.Pointer(pRtree)).FiDepth) (*TRtreeNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FisDirty = int32(1) } } /* Re-insert the contents of any underfull nodes removed from the tree. */ *(*uintptr)(unsafe.Pointer(bp)) = (*TRtree)(unsafe.Pointer(pRtree)).FpDeleted for { if !(*(*uintptr)(unsafe.Pointer(bp)) != 0) { break } if rc == SQLITE_OK { rc = _reinsertNodeContent(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp))) } (*TRtree)(unsafe.Pointer(pRtree)).FpDeleted = (*TRtreeNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpNext (*TRtree)(unsafe.Pointer(pRtree)).FnNodeRef-- Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp))) goto _1 _1: ; *(*uintptr)(unsafe.Pointer(bp)) = (*TRtree)(unsafe.Pointer(pRtree)).FpDeleted } /* Release the reference to the root node. */ if rc == SQLITE_OK { rc = _nodeRelease(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 8))) } else { _nodeRelease(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 8))) } return rc } /* ** Rounding constants for float->double conversion. */ // C documentation // // /* // ** Convert an sqlite3_value into an RtreeValue (presumably a float) // ** while taking care to round toward negative or positive, respectively. // */ func _rtreeValueDown(tls *libc.TLS, v uintptr) (r TRtreeValue) { var d, v1 float64 var f float32 _, _, _ = d, f, v1 d = Xsqlite3_value_double(tls, v) f = float32(d) if float64(f) > d { if d < libc.Float64FromInt32(0) { v1 = float64(1) + libc.Float64FromFloat64(1)/libc.Float64FromFloat64(8.388608e+06) } else { v1 = float64(1) - libc.Float64FromFloat64(1)/libc.Float64FromFloat64(8.388608e+06) } f = float32(d * v1) } return f } func _rtreeValueUp(tls *libc.TLS, v uintptr) (r TRtreeValue) { var d, v1 float64 var f float32 _, _, _ = d, f, v1 d = Xsqlite3_value_double(tls, v) f = float32(d) if float64(f) < d { if d < libc.Float64FromInt32(0) { v1 = float64(1) - libc.Float64FromFloat64(1)/libc.Float64FromFloat64(8.388608e+06) } else { v1 = float64(1) + libc.Float64FromFloat64(1)/libc.Float64FromFloat64(8.388608e+06) } f = float32(d * v1) } return f } // C documentation // // /* // ** A constraint has failed while inserting a row into an rtree table. // ** Assuming no OOM error occurs, this function sets the error message // ** (at pRtree->base.zErrMsg) to an appropriate value and returns // ** SQLITE_CONSTRAINT. // ** // ** Parameter iCol is the index of the leftmost column involved in the // ** constraint failure. If it is 0, then the constraint that failed is // ** the unique constraint on the id column. Otherwise, it is the rtree // ** (c1<=c2) constraint on columns iCol and iCol+1 that has failed. // ** // ** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT. // */ func _rtreeConstraintError(tls *libc.TLS, pRtree uintptr, iCol int32) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var rc, v1 int32 var zCol, zCol1, zCol2, zSql uintptr var _ /* pStmt at bp+0 */ uintptr _, _, _, _, _, _ = rc, zCol, zCol1, zCol2, zSql, v1 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) _ = libc.Int32FromInt32(0) zSql = Xsqlite3_mprintf(tls, __ccgo_ts+25842, libc.VaList(bp+16, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, (*TRtree)(unsafe.Pointer(pRtree)).FzName)) if zSql != 0 { rc = Xsqlite3_prepare_v2(tls, (*TRtree)(unsafe.Pointer(pRtree)).Fdb, zSql, -int32(1), bp, uintptr(0)) } else { rc = int32(SQLITE_NOMEM) } Xsqlite3_free(tls, zSql) if rc == SQLITE_OK { if iCol == 0 { zCol = Xsqlite3_column_name(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) (*TRtree)(unsafe.Pointer(pRtree)).Fbase.FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+25862, libc.VaList(bp+16, (*TRtree)(unsafe.Pointer(pRtree)).FzName, zCol)) } else { zCol1 = Xsqlite3_column_name(tls, *(*uintptr)(unsafe.Pointer(bp)), iCol) zCol2 = Xsqlite3_column_name(tls, *(*uintptr)(unsafe.Pointer(bp)), iCol+int32(1)) (*TRtree)(unsafe.Pointer(pRtree)).Fbase.FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+25894, libc.VaList(bp+16, (*TRtree)(unsafe.Pointer(pRtree)).FzName, zCol1, zCol2)) } } Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) if rc == SQLITE_OK { v1 = int32(SQLITE_CONSTRAINT) } else { v1 = rc } return v1 } // C documentation // // /* // ** The xUpdate method for rtree module virtual tables. // */ func _rtreeUpdate(tls *libc.TLS, pVtab uintptr, nData int32, aData uintptr, pRowid uintptr) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var bHaveRowid, ii, jj, nn, rc, rc2, steprc int32 var pRtree, pUp uintptr var _ /* cell at bp+0 */ TRtreeCell var _ /* pLeaf at bp+48 */ uintptr _, _, _, _, _, _, _, _, _ = bHaveRowid, ii, jj, nn, pRtree, pUp, rc, rc2, steprc pRtree = pVtab rc = SQLITE_OK /* New cell to insert if nData>1 */ bHaveRowid = 0 /* Set to 1 after new rowid is determined */ if (*TRtree)(unsafe.Pointer(pRtree)).FnNodeRef != 0 { /* Unable to write to the btree while another cursor is reading from it, ** since the write might do a rebalance which would disrupt the read ** cursor. */ return libc.Int32FromInt32(SQLITE_LOCKED) | libc.Int32FromInt32(2)<=x1" constraint. ** ** In the first case, if the conflict-handling mode is REPLACE, then ** the conflicting row can be removed before proceeding. In the second ** case, SQLITE_CONSTRAINT must be returned regardless of the ** conflict-handling mode specified by the user. */ if nData > int32(1) { nn = nData - int32(4) if nn > int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) { nn = int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) } /* Populate the cell.aCoord[] array. The first coordinate is aData[3]. ** ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared ** with "column" that are interpreted as table constraints. ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5)); ** This problem was discovered after years of use, so we silently ignore ** these kinds of misdeclared tables to avoid breaking any legacy. */ if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 { ii = 0 for { if !(ii < nn) { break } *(*TRtreeValue)(unsafe.Pointer(bp + 8 + uintptr(ii)*4)) = _rtreeValueDown(tls, *(*uintptr)(unsafe.Pointer(aData + uintptr(ii+int32(3))*4))) *(*TRtreeValue)(unsafe.Pointer(bp + 8 + uintptr(ii+int32(1))*4)) = _rtreeValueUp(tls, *(*uintptr)(unsafe.Pointer(aData + uintptr(ii+int32(4))*4))) if *(*TRtreeValue)(unsafe.Pointer(bp + 8 + uintptr(ii)*4)) > *(*TRtreeValue)(unsafe.Pointer(bp + 8 + uintptr(ii+int32(1))*4)) { rc = _rtreeConstraintError(tls, pRtree, ii+int32(1)) goto constraint } goto _1 _1: ; ii += int32(2) } } else { ii = 0 for { if !(ii < nn) { break } *(*int32)(unsafe.Pointer(bp + 8 + uintptr(ii)*4)) = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(aData + uintptr(ii+int32(3))*4))) *(*int32)(unsafe.Pointer(bp + 8 + uintptr(ii+int32(1))*4)) = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(aData + uintptr(ii+int32(4))*4))) if *(*int32)(unsafe.Pointer(bp + 8 + uintptr(ii)*4)) > *(*int32)(unsafe.Pointer(bp + 8 + uintptr(ii+int32(1))*4)) { rc = _rtreeConstraintError(tls, pRtree, ii+int32(1)) goto constraint } goto _2 _2: ; ii += int32(2) } } /* If a rowid value was supplied, check if it is already present in ** the table. If so, the constraint has failed. */ if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(aData + 2*4))) != int32(SQLITE_NULL) { (*(*TRtreeCell)(unsafe.Pointer(bp))).FiRowid = Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(aData + 2*4))) if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(aData))) == int32(SQLITE_NULL) || Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(aData))) != (*(*TRtreeCell)(unsafe.Pointer(bp))).FiRowid { Xsqlite3_bind_int64(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid, int32(1), (*(*TRtreeCell)(unsafe.Pointer(bp))).FiRowid) steprc = Xsqlite3_step(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid) rc = Xsqlite3_reset(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid) if int32(SQLITE_ROW) == steprc { if Xsqlite3_vtab_on_conflict(tls, (*TRtree)(unsafe.Pointer(pRtree)).Fdb) == int32(SQLITE_REPLACE) { rc = _rtreeDeleteRowid(tls, pRtree, (*(*TRtreeCell)(unsafe.Pointer(bp))).FiRowid) } else { rc = _rtreeConstraintError(tls, pRtree, 0) goto constraint } } } bHaveRowid = int32(1) } } /* If aData[0] is not an SQL NULL value, it is the rowid of a ** record to delete from the r-tree table. The following block does ** just that. */ if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(aData))) != int32(SQLITE_NULL) { rc = _rtreeDeleteRowid(tls, pRtree, Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(aData)))) } /* If the aData[] array contains more than one element, elements ** (aData[2]..aData[argc-1]) contain a new record to insert into ** the r-tree structure. */ if rc == SQLITE_OK && nData > int32(1) { /* Insert the new record into the r-tree */ *(*uintptr)(unsafe.Pointer(bp + 48)) = uintptr(0) /* Figure out the rowid of the new row. */ if bHaveRowid == 0 { rc = _rtreeNewRowid(tls, pRtree, bp) } *(*Tsqlite_int64)(unsafe.Pointer(pRowid)) = (*(*TRtreeCell)(unsafe.Pointer(bp))).FiRowid if rc == SQLITE_OK { rc = _ChooseLeaf(tls, pRtree, bp, 0, bp+48) } if rc == SQLITE_OK { rc = _rtreeInsertCell(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 48)), bp, 0) rc2 = _nodeRelease(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 48))) if rc == SQLITE_OK { rc = rc2 } } if rc == SQLITE_OK && (*TRtree)(unsafe.Pointer(pRtree)).FnAux != 0 { pUp = (*TRtree)(unsafe.Pointer(pRtree)).FpWriteAux Xsqlite3_bind_int64(tls, pUp, int32(1), *(*Tsqlite_int64)(unsafe.Pointer(pRowid))) jj = 0 for { if !(jj < int32((*TRtree)(unsafe.Pointer(pRtree)).FnAux)) { break } Xsqlite3_bind_value(tls, pUp, jj+int32(2), *(*uintptr)(unsafe.Pointer(aData + uintptr(int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)+int32(3)+jj)*4))) goto _3 _3: ; jj++ } Xsqlite3_step(tls, pUp) rc = Xsqlite3_reset(tls, pUp) } } goto constraint constraint: ; _rtreeRelease(tls, pRtree) return rc } // C documentation // // /* // ** Called when a transaction starts. // */ func _rtreeBeginTransaction(tls *libc.TLS, pVtab uintptr) (r int32) { var pRtree uintptr _ = pRtree pRtree = pVtab (*TRtree)(unsafe.Pointer(pRtree)).FinWrTrans = uint8(1) return SQLITE_OK } // C documentation // // /* // ** Called when a transaction completes (either by COMMIT or ROLLBACK). // ** The sqlite3_blob object should be released at this point. // */ func _rtreeEndTransaction(tls *libc.TLS, pVtab uintptr) (r int32) { var pRtree uintptr _ = pRtree pRtree = pVtab (*TRtree)(unsafe.Pointer(pRtree)).FinWrTrans = uint8(0) _nodeBlobReset(tls, pRtree) return SQLITE_OK } func _rtreeRollback(tls *libc.TLS, pVtab uintptr) (r int32) { return _rtreeEndTransaction(tls, pVtab) } // C documentation // // /* // ** The xRename method for rtree module virtual tables. // */ func _rtreeRename(tls *libc.TLS, pVtab uintptr, zNewName uintptr) (r int32) { bp := tls.Alloc(80) defer tls.Free(80) var pRtree, zSql uintptr var rc int32 _, _, _ = pRtree, rc, zSql pRtree = pVtab rc = int32(SQLITE_NOMEM) zSql = Xsqlite3_mprintf(tls, __ccgo_ts+25931, libc.VaList(bp+8, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, (*TRtree)(unsafe.Pointer(pRtree)).FzName, zNewName, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, (*TRtree)(unsafe.Pointer(pRtree)).FzName, zNewName, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, (*TRtree)(unsafe.Pointer(pRtree)).FzName, zNewName)) if zSql != 0 { _nodeBlobReset(tls, pRtree) rc = Xsqlite3_exec(tls, (*TRtree)(unsafe.Pointer(pRtree)).Fdb, zSql, uintptr(0), uintptr(0), uintptr(0)) Xsqlite3_free(tls, zSql) } return rc } // C documentation // // /* // ** The xSavepoint method. // ** // ** This module does not need to do anything to support savepoints. However, // ** it uses this hook to close any open blob handle. This is done because a // ** DROP TABLE command - which fortunately always opens a savepoint - cannot // ** succeed if there are any open blob handles. i.e. if the blob handle were // ** not closed here, the following would fail: // ** // ** BEGIN; // ** INSERT INTO rtree... // ** DROP TABLE ; -- Would fail with SQLITE_LOCKED // ** COMMIT; // */ func _rtreeSavepoint(tls *libc.TLS, pVtab uintptr, iSavepoint int32) (r int32) { var iwt Tu8 var pRtree uintptr _, _ = iwt, pRtree pRtree = pVtab iwt = (*TRtree)(unsafe.Pointer(pRtree)).FinWrTrans _ = iSavepoint (*TRtree)(unsafe.Pointer(pRtree)).FinWrTrans = uint8(0) _nodeBlobReset(tls, pRtree) (*TRtree)(unsafe.Pointer(pRtree)).FinWrTrans = iwt return SQLITE_OK } // C documentation // // /* // ** This function populates the pRtree->nRowEst variable with an estimate // ** of the number of rows in the virtual table. If possible, this is based // ** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST. // */ func _rtreeQueryStat1(tls *libc.TLS, db uintptr, pRtree uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var nRow Ti64 var rc, v1 int32 var zFmt, zSql uintptr var v2 int64 var _ /* p at bp+0 */ uintptr _, _, _, _, _, _ = nRow, rc, zFmt, zSql, v1, v2 zFmt = __ccgo_ts + 26076 nRow = int64(RTREE_MIN_ROWEST) rc = Xsqlite3_table_column_metadata(tls, db, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, __ccgo_ts+11582, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0)) if rc != SQLITE_OK { (*TRtree)(unsafe.Pointer(pRtree)).FnRowEst = int64(RTREE_DEFAULT_ROWEST) if rc == int32(SQLITE_ERROR) { v1 = SQLITE_OK } else { v1 = rc } return v1 } zSql = Xsqlite3_mprintf(tls, zFmt, libc.VaList(bp+16, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, (*TRtree)(unsafe.Pointer(pRtree)).FzName)) if zSql == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { rc = Xsqlite3_prepare_v2(tls, db, zSql, -int32(1), bp, uintptr(0)) if rc == SQLITE_OK { if Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) == int32(SQLITE_ROW) { nRow = Xsqlite3_column_int64(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) } rc = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) } Xsqlite3_free(tls, zSql) } if nRow > int64(libc.Int32FromInt32(RTREE_MIN_ROWEST)) { v2 = nRow } else { v2 = int64(libc.Int32FromInt32(RTREE_MIN_ROWEST)) } (*TRtree)(unsafe.Pointer(pRtree)).FnRowEst = v2 return rc } // C documentation // // /* // ** Return true if zName is the extension on one of the shadow tables used // ** by this module. // */ func _rtreeShadowName(tls *libc.TLS, zName uintptr) (r int32) { var i uint32 _ = i i = uint32(0) for { if !(i < libc.Uint32FromInt64(12)/libc.Uint32FromInt64(4)) { break } if Xsqlite3_stricmp(tls, zName, _azName1[i]) == 0 { return int32(1) } goto _1 _1: ; i++ } return 0 } var _azName1 = [3]uintptr{ 0: __ccgo_ts + 26132, 1: __ccgo_ts + 5484, 2: __ccgo_ts + 16694, } var _rtreeModule = Tsqlite3_module{ FiVersion: int32(4), } func init() { p := unsafe.Pointer(&_rtreeModule) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_rtreeCreate) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_rtreeConnect) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_rtreeBestIndex) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_rtreeDisconnect) *(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_rtreeDestroy) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_rtreeOpen) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_rtreeClose) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_rtreeFilter) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_rtreeNext) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_rtreeEof) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_rtreeColumn) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_rtreeRowid) *(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(_rtreeUpdate) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_rtreeBeginTransaction) *(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_rtreeEndTransaction) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_rtreeEndTransaction) *(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_rtreeRollback) *(*uintptr)(unsafe.Add(p, 76)) = __ccgo_fp(_rtreeRename) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_rtreeSavepoint) *(*uintptr)(unsafe.Add(p, 92)) = __ccgo_fp(_rtreeShadowName) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_rtreeIntegrity) } func _rtreeSqlInit(tls *libc.TLS, pRtree uintptr, db uintptr, zDb uintptr, zPrefix uintptr, isCreate int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var appStmt [8]uintptr var f, i, ii, ii1, rc int32 var p, p1, zCreate, zFormat, zSql, zSql1 uintptr _, _, _, _, _, _, _, _, _, _, _, _ = appStmt, f, i, ii, ii1, p, p1, rc, zCreate, zFormat, zSql, zSql1 rc = SQLITE_OK f = libc.Int32FromInt32(SQLITE_PREPARE_PERSISTENT) | libc.Int32FromInt32(SQLITE_PREPARE_NO_VTAB) (*TRtree)(unsafe.Pointer(pRtree)).Fdb = db if isCreate != 0 { p = Xsqlite3_str_new(tls, db) Xsqlite3_str_appendf(tls, p, __ccgo_ts+26546, libc.VaList(bp+8, zDb, zPrefix)) ii = 0 for { if !(ii < int32((*TRtree)(unsafe.Pointer(pRtree)).FnAux)) { break } Xsqlite3_str_appendf(tls, p, __ccgo_ts+26608, libc.VaList(bp+8, ii)) goto _1 _1: ; ii++ } Xsqlite3_str_appendf(tls, p, __ccgo_ts+26613, libc.VaList(bp+8, zDb, zPrefix)) Xsqlite3_str_appendf(tls, p, __ccgo_ts+26677, libc.VaList(bp+8, zDb, zPrefix)) Xsqlite3_str_appendf(tls, p, __ccgo_ts+26747, libc.VaList(bp+8, zDb, zPrefix, (*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize)) zCreate = Xsqlite3_str_finish(tls, p) if !(zCreate != 0) { return int32(SQLITE_NOMEM) } rc = Xsqlite3_exec(tls, db, zCreate, uintptr(0), uintptr(0), uintptr(0)) Xsqlite3_free(tls, zCreate) if rc != SQLITE_OK { return rc } } appStmt[0] = pRtree + 76 appStmt[int32(1)] = pRtree + 80 appStmt[int32(2)] = pRtree + 84 appStmt[int32(3)] = pRtree + 88 appStmt[int32(4)] = pRtree + 92 appStmt[int32(5)] = pRtree + 96 appStmt[int32(6)] = pRtree + 100 appStmt[int32(7)] = pRtree + 104 rc = _rtreeQueryStat1(tls, db, pRtree) i = 0 for { if !(i < int32(N_STATEMENT) && rc == SQLITE_OK) { break } if i != int32(3) || int32((*TRtree)(unsafe.Pointer(pRtree)).FnAux) == 0 { zFormat = _azSql[i] } else { /* An UPSERT is very slightly slower than REPLACE, but it is needed ** if there are auxiliary columns */ zFormat = __ccgo_ts + 26796 } zSql = Xsqlite3_mprintf(tls, zFormat, libc.VaList(bp+8, zDb, zPrefix)) if zSql != 0 { rc = Xsqlite3_prepare_v3(tls, db, zSql, -int32(1), uint32(f), appStmt[i], uintptr(0)) } else { rc = int32(SQLITE_NOMEM) } Xsqlite3_free(tls, zSql) goto _2 _2: ; i++ } if (*TRtree)(unsafe.Pointer(pRtree)).FnAux != 0 && rc != int32(SQLITE_NOMEM) { (*TRtree)(unsafe.Pointer(pRtree)).FzReadAuxSql = Xsqlite3_mprintf(tls, __ccgo_ts+26904, libc.VaList(bp+8, zDb, zPrefix)) if (*TRtree)(unsafe.Pointer(pRtree)).FzReadAuxSql == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { p1 = Xsqlite3_str_new(tls, db) Xsqlite3_str_appendf(tls, p1, __ccgo_ts+26949, libc.VaList(bp+8, zDb, zPrefix)) ii1 = 0 for { if !(ii1 < int32((*TRtree)(unsafe.Pointer(pRtree)).FnAux)) { break } if ii1 != 0 { Xsqlite3_str_append(tls, p1, __ccgo_ts+13090, int32(1)) } if ii1 < int32((*TRtree)(unsafe.Pointer(pRtree)).FnAuxNotNull) { Xsqlite3_str_appendf(tls, p1, __ccgo_ts+26976, libc.VaList(bp+8, ii1, ii1+int32(2), ii1)) } else { Xsqlite3_str_appendf(tls, p1, __ccgo_ts+26998, libc.VaList(bp+8, ii1, ii1+int32(2))) } goto _3 _3: ; ii1++ } Xsqlite3_str_appendf(tls, p1, __ccgo_ts+27006, 0) zSql1 = Xsqlite3_str_finish(tls, p1) if zSql1 == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { rc = Xsqlite3_prepare_v3(tls, db, zSql1, -int32(1), uint32(f), pRtree+108, uintptr(0)) Xsqlite3_free(tls, zSql1) } } } return rc } var _azSql = [8]uintptr{ 0: __ccgo_ts + 26137, 1: __ccgo_ts + 26190, 2: __ccgo_ts + 26235, 3: __ccgo_ts + 26287, 4: __ccgo_ts + 26341, 5: __ccgo_ts + 26386, 6: __ccgo_ts + 26444, 7: __ccgo_ts + 26499, } // C documentation // // /* // ** The second argument to this function contains the text of an SQL statement // ** that returns a single integer value. The statement is compiled and executed // ** using database connection db. If successful, the integer value returned // ** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error // ** code is returned and the value of *piVal after returning is not defined. // */ func _getIntFromStmt(tls *libc.TLS, db uintptr, zSql uintptr, piVal uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* pStmt at bp+0 */ uintptr _ = rc rc = int32(SQLITE_NOMEM) if zSql != 0 { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) rc = Xsqlite3_prepare_v2(tls, db, zSql, -int32(1), bp, uintptr(0)) if rc == SQLITE_OK { if int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { *(*int32)(unsafe.Pointer(piVal)) = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) } rc = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) } } return rc } // C documentation // // /* // ** This function is called from within the xConnect() or xCreate() method to // ** determine the node-size used by the rtree table being created or connected // ** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned. // ** Otherwise, an SQLite error code is returned. // ** // ** If this function is being called as part of an xConnect(), then the rtree // ** table already exists. In this case the node-size is determined by inspecting // ** the root node of the tree. // ** // ** Otherwise, for an xCreate(), use 64 bytes less than the database page-size. // ** This ensures that each node is stored on a single database page. If the // ** database page-size is so large that more than RTREE_MAXCELLS entries // ** would fit in a single node, use a smaller node-size. // */ func _getNodeSize(tls *libc.TLS, db uintptr, pRtree uintptr, isCreate int32, pzErr uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var rc int32 var zSql uintptr var _ /* iPageSize at bp+0 */ int32 _, _ = rc, zSql if isCreate != 0 { *(*int32)(unsafe.Pointer(bp)) = 0 zSql = Xsqlite3_mprintf(tls, __ccgo_ts+27022, libc.VaList(bp+16, (*TRtree)(unsafe.Pointer(pRtree)).FzDb)) rc = _getIntFromStmt(tls, db, zSql, bp) if rc == SQLITE_OK { (*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize = *(*int32)(unsafe.Pointer(bp)) - int32(64) if int32(4)+int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell)*int32(RTREE_MAXCELLS) < (*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize { (*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize = int32(4) + int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell)*int32(RTREE_MAXCELLS) } } else { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+16, Xsqlite3_errmsg(tls, db))) } } else { zSql = Xsqlite3_mprintf(tls, __ccgo_ts+27042, libc.VaList(bp+16, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, (*TRtree)(unsafe.Pointer(pRtree)).FzName)) rc = _getIntFromStmt(tls, db, zSql, pRtree+16) if rc != SQLITE_OK { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+16, Xsqlite3_errmsg(tls, db))) } else { if (*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize < libc.Int32FromInt32(512)-libc.Int32FromInt32(64) { rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< module name // ** argv[1] -> database name // ** argv[2] -> table name // ** argv[...] -> column names... // */ func _rtreeInit(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr, isCreate int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var aErrMsg [5]uintptr var eCoordType, iErr, ii, nDb, nName, rc, v1, v3 int32 var pRtree, pSql, zArg, zSql uintptr _, _, _, _, _, _, _, _, _, _, _, _, _ = aErrMsg, eCoordType, iErr, ii, nDb, nName, pRtree, pSql, rc, zArg, zSql, v1, v3 rc = SQLITE_OK if pAux != 0 { v1 = int32(RTREE_COORD_INT32) } else { v1 = RTREE_COORD_REAL32 } /* Length of string argv[2] */ eCoordType = v1 ii = int32(4) aErrMsg = [5]uintptr{ 1: __ccgo_ts + 27134, 2: __ccgo_ts + 27177, 3: __ccgo_ts + 27212, 4: __ccgo_ts + 27248, } _ = libc.Int32FromInt32(0) /* Aux columns counted by a u8 */ if argc < int32(6) || argc > libc.Int32FromInt32(RTREE_MAX_AUX_COLUMN)+libc.Int32FromInt32(3) { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+8, aErrMsg[int32(2)+libc.BoolInt32(argc >= int32(6))])) return int32(SQLITE_ERROR) } Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_CONSTRAINT_SUPPORT), libc.VaList(bp+8, int32(1))) Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_INNOCUOUS), 0) /* Allocate the sqlite3_vtab structure */ nDb = int32(libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4)))) nName = int32(libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4)))) pRtree = Xsqlite3_malloc64(tls, uint64(uint32(504)+uint32(nDb)+uint32(nName*int32(2))+uint32(8))) if !(pRtree != 0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, pRtree, 0, uint32(504)+uint32(nDb)+uint32(nName*int32(2))+uint32(8)) (*TRtree)(unsafe.Pointer(pRtree)).FnBusy = uint32(1) (*TRtree)(unsafe.Pointer(pRtree)).Fbase.FpModule = uintptr(unsafe.Pointer(&_rtreeModule)) (*TRtree)(unsafe.Pointer(pRtree)).FzDb = pRtree + 1*504 (*TRtree)(unsafe.Pointer(pRtree)).FzName = (*TRtree)(unsafe.Pointer(pRtree)).FzDb + uintptr(nDb+int32(1)) (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName = (*TRtree)(unsafe.Pointer(pRtree)).FzName + uintptr(nName+int32(1)) (*TRtree)(unsafe.Pointer(pRtree)).FeCoordType = uint8(eCoordType) libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, *(*uintptr)(unsafe.Pointer(argv + 1*4)), uint32(nDb)) libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzName, *(*uintptr)(unsafe.Pointer(argv + 2*4)), uint32(nName)) libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName, *(*uintptr)(unsafe.Pointer(argv + 2*4)), uint32(nName)) libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName+uintptr(nName), __ccgo_ts+27285, uint32(6)) /* Create/Connect to the underlying relational database schema. If ** that is successful, call sqlite3_declare_vtab() to configure ** the r-tree table schema. */ pSql = Xsqlite3_str_new(tls, db) Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+27291, libc.VaList(bp+8, _rtreeTokenLength(tls, *(*uintptr)(unsafe.Pointer(argv + 3*4))), *(*uintptr)(unsafe.Pointer(argv + 3*4)))) ii = int32(4) for { if !(ii < argc) { break } zArg = *(*uintptr)(unsafe.Pointer(argv + uintptr(ii)*4)) if int32(*(*uint8)(unsafe.Pointer(zArg))) == int32('+') { (*TRtree)(unsafe.Pointer(pRtree)).FnAux++ Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+27315, libc.VaList(bp+8, _rtreeTokenLength(tls, zArg+uintptr(1)), zArg+uintptr(1))) } else { if int32((*TRtree)(unsafe.Pointer(pRtree)).FnAux) > 0 { break } else { (*TRtree)(unsafe.Pointer(pRtree)).FnDim2++ Xsqlite3_str_appendf(tls, pSql, _azFormat[eCoordType], libc.VaList(bp+8, _rtreeTokenLength(tls, zArg), zArg)) } } goto _2 _2: ; ii++ } Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+27342, 0) zSql = Xsqlite3_str_finish(tls, pSql) if !(zSql != 0) { rc = int32(SQLITE_NOMEM) } else { if ii < argc { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+8, aErrMsg[int32(4)])) rc = int32(SQLITE_ERROR) } else { v3 = Xsqlite3_declare_vtab(tls, db, zSql) rc = v3 if SQLITE_OK != v3 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+8, Xsqlite3_errmsg(tls, db))) } } } Xsqlite3_free(tls, zSql) if rc != 0 { goto rtreeInit_fail } (*TRtree)(unsafe.Pointer(pRtree)).FnDim = uint8(int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) / int32(2)) if int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim) < int32(1) { iErr = int32(2) } else { if int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) > libc.Int32FromInt32(RTREE_MAX_DIMENSIONS)*libc.Int32FromInt32(2) { iErr = int32(3) } else { if int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)%int32(2) != 0 { iErr = int32(1) } else { iErr = 0 } } } if iErr != 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+8, aErrMsg[iErr])) goto rtreeInit_fail } (*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell = uint8(int32(8) + int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)*int32(4)) /* Figure out the node size to use. */ rc = _getNodeSize(tls, db, pRtree, isCreate, pzErr) if rc != 0 { goto rtreeInit_fail } rc = _rtreeSqlInit(tls, pRtree, db, *(*uintptr)(unsafe.Pointer(argv + 1*4)), *(*uintptr)(unsafe.Pointer(argv + 2*4)), isCreate) if rc != 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+8, Xsqlite3_errmsg(tls, db))) goto rtreeInit_fail } *(*uintptr)(unsafe.Pointer(ppVtab)) = pRtree return SQLITE_OK goto rtreeInit_fail rtreeInit_fail: ; if rc == SQLITE_OK { rc = int32(SQLITE_ERROR) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _rtreeRelease(tls, pRtree) return rc } var _azFormat = [2]uintptr{ 0: __ccgo_ts + 27321, 1: __ccgo_ts + 27332, } // C documentation // // /* // ** Implementation of a scalar function that decodes r-tree nodes to // ** human readable strings. This can be used for debugging and analysis. // ** // ** The scalar function takes two arguments: (1) the number of dimensions // ** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing // ** an r-tree node. For a two-dimensional r-tree structure called "rt", to // ** deserialize all nodes, a statement like: // ** // ** SELECT rtreenode(2, data) FROM rt_node; // ** // ** The human readable string takes the form of a Tcl list with one // ** entry for each cell in the r-tree node. Each entry is itself a // ** list, containing the 8-byte rowid/pageno followed by the // ** *2 coordinates. // */ func _rtreenode(tls *libc.TLS, ctx uintptr, nArg int32, apArg uintptr) { bp := tls.Alloc(608) defer tls.Free(608) var errCode, ii, jj, nData int32 var pOut uintptr var _ /* cell at bp+536 */ TRtreeCell var _ /* node at bp+0 */ TRtreeNode var _ /* tree at bp+32 */ TRtree _, _, _, _, _ = errCode, ii, jj, nData, pOut _ = nArg libc.Xmemset(tls, bp, 0, uint32(32)) libc.Xmemset(tls, bp+32, 0, uint32(504)) (*(*TRtree)(unsafe.Pointer(bp + 32))).FnDim = uint8(Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(apArg)))) if int32((*(*TRtree)(unsafe.Pointer(bp + 32))).FnDim) < int32(1) || int32((*(*TRtree)(unsafe.Pointer(bp + 32))).FnDim) > int32(5) { return } (*(*TRtree)(unsafe.Pointer(bp + 32))).FnDim2 = uint8(int32((*(*TRtree)(unsafe.Pointer(bp + 32))).FnDim) * int32(2)) (*(*TRtree)(unsafe.Pointer(bp + 32))).FnBytesPerCell = uint8(int32(8) + int32(8)*int32((*(*TRtree)(unsafe.Pointer(bp + 32))).FnDim)) (*(*TRtreeNode)(unsafe.Pointer(bp))).FzData = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(apArg + 1*4))) if (*(*TRtreeNode)(unsafe.Pointer(bp))).FzData == uintptr(0) { return } nData = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(apArg + 1*4))) if nData < int32(4) { return } if nData < _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(bp)).FzData+2)*int32((*(*TRtree)(unsafe.Pointer(bp + 32))).FnBytesPerCell) { return } pOut = Xsqlite3_str_new(tls, uintptr(0)) ii = 0 for { if !(ii < _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(bp)).FzData+2)) { break } _nodeGetCell(tls, bp+32, bp, ii, bp+536) if ii > 0 { Xsqlite3_str_append(tls, pOut, __ccgo_ts+11202, int32(1)) } Xsqlite3_str_appendf(tls, pOut, __ccgo_ts+27345, libc.VaList(bp+592, (*(*TRtreeCell)(unsafe.Pointer(bp + 536))).FiRowid)) jj = 0 for { if !(jj < int32((*(*TRtree)(unsafe.Pointer(bp + 32))).FnDim2)) { break } Xsqlite3_str_appendf(tls, pOut, __ccgo_ts+27351, libc.VaList(bp+592, float64(*(*TRtreeValue)(unsafe.Pointer(bp + 536 + 8 + uintptr(jj)*4))))) goto _2 _2: ; jj++ } Xsqlite3_str_append(tls, pOut, __ccgo_ts+27355, int32(1)) goto _1 _1: ; ii++ } errCode = Xsqlite3_str_errcode(tls, pOut) Xsqlite3_result_text(tls, ctx, Xsqlite3_str_finish(tls, pOut), -int32(1), __ccgo_fp(Xsqlite3_free)) Xsqlite3_result_error_code(tls, ctx, errCode) } // C documentation // // /* This routine implements an SQL function that returns the "depth" parameter // ** from the front of a blob that is an r-tree node. For example: // ** // ** SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1; // ** // ** The depth value is 0 for all nodes other than the root node, and the root // ** node always has nodeno=1, so the example above is the primary use for this // ** routine. This routine is intended for testing and analysis only. // */ func _rtreedepth(tls *libc.TLS, ctx uintptr, nArg int32, apArg uintptr) { var zBlob uintptr _ = zBlob _ = nArg if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(apArg))) != int32(SQLITE_BLOB) || Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(apArg))) < int32(2) { Xsqlite3_result_error(tls, ctx, __ccgo_ts+27357, -int32(1)) } else { zBlob = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(apArg))) if zBlob != 0 { Xsqlite3_result_int(tls, ctx, _readInt16(tls, zBlob)) } else { Xsqlite3_result_error_nomem(tls, ctx) } } } // C documentation // // /* // ** Context object passed between the various routines that make up the // ** implementation of integrity-check function rtreecheck(). // */ type TRtreeCheck = struct { Fdb uintptr FzDb uintptr FzTab uintptr FbInt int32 FnDim int32 FpGetNode uintptr FaCheckMapping [2]uintptr FnLeaf int32 FnNonLeaf int32 Frc int32 FzReport uintptr FnErr int32 } type RtreeCheck = TRtreeCheck type TRtreeCheck1 = struct { Fdb uintptr FzDb uintptr FzTab uintptr FbInt int32 FnDim int32 FpGetNode uintptr FaCheckMapping [2]uintptr FnLeaf int32 FnNonLeaf int32 Frc int32 FzReport uintptr FnErr int32 } type RtreeCheck1 = TRtreeCheck1 // C documentation // // /* // ** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error, // ** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code. // */ func _rtreeCheckReset(tls *libc.TLS, pCheck uintptr, pStmt uintptr) { var rc int32 _ = rc rc = Xsqlite3_reset(tls, pStmt) if (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc == SQLITE_OK { (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc = rc } } // C documentation // // /* // ** The second and subsequent arguments to this function are a format string // ** and printf style arguments. This function formats the string and attempts // ** to compile it as an SQL statement. // ** // ** If successful, a pointer to the new SQL statement is returned. Otherwise, // ** NULL is returned and an error code left in RtreeCheck.rc. // */ func _rtreeCheckPrepare(tls *libc.TLS, pCheck uintptr, zFmt uintptr, va uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var ap Tva_list var z uintptr var _ /* pRet at bp+0 */ uintptr _, _ = ap, z *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) ap = va z = Xsqlite3_vmprintf(tls, zFmt, ap) if (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc == SQLITE_OK { if z == uintptr(0) { (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc = int32(SQLITE_NOMEM) } else { (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc = Xsqlite3_prepare_v2(tls, (*TRtreeCheck)(unsafe.Pointer(pCheck)).Fdb, z, -int32(1), bp, uintptr(0)) } } Xsqlite3_free(tls, z) _ = ap return *(*uintptr)(unsafe.Pointer(bp)) } // C documentation // // /* // ** The second and subsequent arguments to this function are a printf() // ** style format string and arguments. This function formats the string and // ** appends it to the report being accumuated in pCheck. // */ func _rtreeCheckAppendMsg(tls *libc.TLS, pCheck uintptr, zFmt uintptr, va uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var ap Tva_list var z, v1 uintptr _, _, _ = ap, z, v1 ap = va if (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc == SQLITE_OK && (*TRtreeCheck)(unsafe.Pointer(pCheck)).FnErr < int32(RTREE_CHECK_MAX_ERROR) { z = Xsqlite3_vmprintf(tls, zFmt, ap) if z == uintptr(0) { (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc = int32(SQLITE_NOMEM) } else { if (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzReport != 0 { v1 = __ccgo_ts + 4203 } else { v1 = __ccgo_ts + 1648 } (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzReport = Xsqlite3_mprintf(tls, __ccgo_ts+27390, libc.VaList(bp+8, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzReport, v1, z)) if (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzReport == uintptr(0) { (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc = int32(SQLITE_NOMEM) } } (*TRtreeCheck)(unsafe.Pointer(pCheck)).FnErr++ } _ = ap } // C documentation // // /* // ** This function is a no-op if there is already an error code stored // ** in the RtreeCheck object indicated by the first argument. NULL is // ** returned in this case. // ** // ** Otherwise, the contents of rtree table node iNode are loaded from // ** the database and copied into a buffer obtained from sqlite3_malloc(). // ** If no error occurs, a pointer to the buffer is returned and (*pnNode) // ** is set to the size of the buffer in bytes. // ** // ** Or, if an error does occur, NULL is returned and an error code left // ** in the RtreeCheck object. The final value of *pnNode is undefined in // ** this case. // */ func _rtreeCheckGetNode(tls *libc.TLS, pCheck uintptr, iNode Ti64, pnNode uintptr) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var nNode int32 var pNode, pRet uintptr _, _, _ = nNode, pNode, pRet pRet = uintptr(0) /* Return value */ if (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc == SQLITE_OK && (*TRtreeCheck)(unsafe.Pointer(pCheck)).FpGetNode == uintptr(0) { (*TRtreeCheck)(unsafe.Pointer(pCheck)).FpGetNode = _rtreeCheckPrepare(tls, pCheck, __ccgo_ts+27397, libc.VaList(bp+8, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzDb, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzTab)) } if (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc == SQLITE_OK { Xsqlite3_bind_int64(tls, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FpGetNode, int32(1), iNode) if Xsqlite3_step(tls, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FpGetNode) == int32(SQLITE_ROW) { nNode = Xsqlite3_column_bytes(tls, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FpGetNode, 0) pNode = Xsqlite3_column_blob(tls, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FpGetNode, 0) pRet = Xsqlite3_malloc64(tls, uint64(nNode)) if pRet == uintptr(0) { (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc = int32(SQLITE_NOMEM) } else { libc.Xmemcpy(tls, pRet, pNode, uint32(nNode)) *(*int32)(unsafe.Pointer(pnNode)) = nNode } } _rtreeCheckReset(tls, pCheck, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FpGetNode) if (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc == SQLITE_OK && pRet == uintptr(0) { _rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+27442, libc.VaList(bp+8, iNode)) } } return pRet } // C documentation // // /* // ** This function is used to check that the %_parent (if bLeaf==0) or %_rowid // ** (if bLeaf==1) table contains a specified entry. The schemas of the // ** two tables are: // ** // ** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER) // ** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...) // ** // ** In both cases, this function checks that there exists an entry with // ** IPK value iKey and the second column set to iVal. // ** // */ func _rtreeCheckMapping(tls *libc.TLS, pCheck uintptr, bLeaf int32, iKey Ti64, iVal Ti64) { bp := tls.Alloc(48) defer tls.Free(48) var azSql [2]uintptr var ii Ti64 var pStmt, v1, v2 uintptr var rc int32 _, _, _, _, _, _ = azSql, ii, pStmt, rc, v1, v2 azSql = [2]uintptr{ 0: __ccgo_ts + 27474, 1: __ccgo_ts + 27528, } _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(pCheck + 24 + uintptr(bLeaf)*4)) == uintptr(0) { *(*uintptr)(unsafe.Pointer(pCheck + 24 + uintptr(bLeaf)*4)) = _rtreeCheckPrepare(tls, pCheck, azSql[bLeaf], libc.VaList(bp+8, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzDb, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzTab)) } if (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc != SQLITE_OK { return } pStmt = *(*uintptr)(unsafe.Pointer(pCheck + 24 + uintptr(bLeaf)*4)) Xsqlite3_bind_int64(tls, pStmt, int32(1), iKey) rc = Xsqlite3_step(tls, pStmt) if rc == int32(SQLITE_DONE) { if bLeaf != 0 { v1 = __ccgo_ts + 27576 } else { v1 = __ccgo_ts + 27584 } _rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+27593, libc.VaList(bp+8, iKey, iVal, v1)) } else { if rc == int32(SQLITE_ROW) { ii = Xsqlite3_column_int64(tls, pStmt, 0) if ii != iVal { if bLeaf != 0 { v2 = __ccgo_ts + 27576 } else { v2 = __ccgo_ts + 27584 } _rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+27638, libc.VaList(bp+8, iKey, ii, v2, iKey, iVal)) } } } _rtreeCheckReset(tls, pCheck, pStmt) } // C documentation // // /* // ** Argument pCell points to an array of coordinates stored on an rtree page. // ** This function checks that the coordinates are internally consistent (no // ** x1>x2 conditions) and adds an error message to the RtreeCheck object // ** if they are not. // ** // ** Additionally, if pParent is not NULL, then it is assumed to point to // ** the array of coordinates on the parent page that bound the page // ** containing pCell. In this case it is also verified that the two // ** sets of coordinates are mutually consistent and an error message added // ** to the RtreeCheck object if they are not. // */ func _rtreeCheckCellCoord(tls *libc.TLS, pCheck uintptr, iNode Ti64, iCell int32, pCell uintptr, pParent uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var i, v2, v3, v4 int32 var v5 bool var _ /* c1 at bp+0 */ TRtreeCoord var _ /* c2 at bp+4 */ TRtreeCoord var _ /* p1 at bp+8 */ TRtreeCoord var _ /* p2 at bp+12 */ TRtreeCoord _, _, _, _, _ = i, v2, v3, v4, v5 i = 0 for { if !(i < (*TRtreeCheck)(unsafe.Pointer(pCheck)).FnDim) { break } _readCoord(tls, pCell+uintptr(libc.Int32FromInt32(4)*libc.Int32FromInt32(2)*i), bp) _readCoord(tls, pCell+uintptr(int32(4)*(int32(2)*i+int32(1))), bp+4) /* printf("%e, %e\n", c1.u.f, c2.u.f); */ if (*TRtreeCheck)(unsafe.Pointer(pCheck)).FbInt != 0 { v2 = libc.BoolInt32(*(*int32)(unsafe.Pointer(bp)) > *(*int32)(unsafe.Pointer(bp + 4))) } else { v2 = libc.BoolInt32(*(*TRtreeValue)(unsafe.Pointer(bp)) > *(*TRtreeValue)(unsafe.Pointer(bp + 4))) } if v2 != 0 { _rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+27696, libc.VaList(bp+24, i, iCell, iNode)) } if pParent != 0 { _readCoord(tls, pParent+uintptr(libc.Int32FromInt32(4)*libc.Int32FromInt32(2)*i), bp+8) _readCoord(tls, pParent+uintptr(int32(4)*(int32(2)*i+int32(1))), bp+12) if (*TRtreeCheck)(unsafe.Pointer(pCheck)).FbInt != 0 { v3 = libc.BoolInt32(*(*int32)(unsafe.Pointer(bp)) < *(*int32)(unsafe.Pointer(bp + 8))) } else { v3 = libc.BoolInt32(*(*TRtreeValue)(unsafe.Pointer(bp)) < *(*TRtreeValue)(unsafe.Pointer(bp + 8))) } if v5 = v3 != 0; !v5 { if (*TRtreeCheck)(unsafe.Pointer(pCheck)).FbInt != 0 { v4 = libc.BoolInt32(*(*int32)(unsafe.Pointer(bp + 4)) > *(*int32)(unsafe.Pointer(bp + 12))) } else { v4 = libc.BoolInt32(*(*TRtreeValue)(unsafe.Pointer(bp + 4)) > *(*TRtreeValue)(unsafe.Pointer(bp + 12))) } } if v5 || v4 != 0 { _rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+27744, libc.VaList(bp+24, i, iCell, iNode)) } } goto _1 _1: ; i++ } } // C documentation // // /* // ** Run rtreecheck() checks on node iNode, which is at depth iDepth within // ** the r-tree structure. Argument aParent points to the array of coordinates // ** that bound node iNode on the parent node. // ** // ** If any problems are discovered, an error message is appended to the // ** report accumulated in the RtreeCheck object. // */ func _rtreeCheckNode(tls *libc.TLS, pCheck uintptr, iDepth int32, aParent uintptr, iNode Ti64) { bp := tls.Alloc(48) defer tls.Free(48) var aNode, pCell uintptr var i, nCell int32 var iVal Ti64 var _ /* nNode at bp+0 */ int32 _, _, _, _, _ = aNode, i, iVal, nCell, pCell aNode = uintptr(0) *(*int32)(unsafe.Pointer(bp)) = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) aNode = _rtreeCheckGetNode(tls, pCheck, iNode, bp) if aNode != 0 { if *(*int32)(unsafe.Pointer(bp)) < int32(4) { _rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+27811, libc.VaList(bp+16, iNode, *(*int32)(unsafe.Pointer(bp)))) } else { /* Used to iterate through cells */ if aParent == uintptr(0) { iDepth = _readInt16(tls, aNode) if iDepth > int32(RTREE_MAX_DEPTH) { _rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+27845, libc.VaList(bp+16, iDepth)) Xsqlite3_free(tls, aNode) return } } nCell = _readInt16(tls, aNode+2) if int32(4)+nCell*(int32(8)+(*TRtreeCheck)(unsafe.Pointer(pCheck)).FnDim*int32(2)*int32(4)) > *(*int32)(unsafe.Pointer(bp)) { _rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+27875, libc.VaList(bp+16, iNode, nCell, *(*int32)(unsafe.Pointer(bp)))) } else { i = 0 for { if !(i < nCell) { break } pCell = aNode + uintptr(int32(4)+i*(int32(8)+(*TRtreeCheck)(unsafe.Pointer(pCheck)).FnDim*int32(2)*int32(4))) iVal = _readInt64(tls, pCell) _rtreeCheckCellCoord(tls, pCheck, iNode, i, pCell+8, aParent) if iDepth > 0 { _rtreeCheckMapping(tls, pCheck, 0, iVal, iNode) _rtreeCheckNode(tls, pCheck, iDepth-int32(1), pCell+8, iVal) (*TRtreeCheck)(unsafe.Pointer(pCheck)).FnNonLeaf++ } else { _rtreeCheckMapping(tls, pCheck, int32(1), iVal, iNode) (*TRtreeCheck)(unsafe.Pointer(pCheck)).FnLeaf++ } goto _1 _1: ; i++ } } } Xsqlite3_free(tls, aNode) } } // C documentation // // /* // ** The second argument to this function must be either "_rowid" or // ** "_parent". This function checks that the number of entries in the // ** %_rowid or %_parent table is exactly nExpect. If not, it adds // ** an error message to the report in the RtreeCheck object indicated // ** by the first argument. // */ func _rtreeCheckCount(tls *libc.TLS, pCheck uintptr, zTbl uintptr, nExpect Ti64) { bp := tls.Alloc(32) defer tls.Free(32) var nActual Ti64 var pCount uintptr _, _ = nActual, pCount if (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc == SQLITE_OK { pCount = _rtreeCheckPrepare(tls, pCheck, __ccgo_ts+27930, libc.VaList(bp+8, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzDb, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzTab, zTbl)) if pCount != 0 { if Xsqlite3_step(tls, pCount) == int32(SQLITE_ROW) { nActual = Xsqlite3_column_int64(tls, pCount, 0) if nActual != nExpect { _rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+27961, libc.VaList(bp+8, zTbl, nExpect, nActual)) } } (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc = Xsqlite3_finalize(tls, pCount) } } } // C documentation // // /* // ** This function does the bulk of the work for the rtree integrity-check. // ** It is called by rtreecheck(), which is the SQL function implementation. // */ func _rtreeCheckTable(tls *libc.TLS, db uintptr, zDb uintptr, zTab uintptr, pzReport uintptr) (r int32) { bp := tls.Alloc(80) defer tls.Free(80) var nAux, rc int32 var pStmt uintptr var _ /* check at bp+0 */ TRtreeCheck _, _, _ = nAux, pStmt, rc /* Common context for various routines */ pStmt = uintptr(0) /* Used to find column count of rtree table */ nAux = 0 /* Number of extra columns. */ /* Initialize the context object */ libc.Xmemset(tls, bp, 0, uint32(52)) (*(*TRtreeCheck)(unsafe.Pointer(bp))).Fdb = db (*(*TRtreeCheck)(unsafe.Pointer(bp))).FzDb = zDb (*(*TRtreeCheck)(unsafe.Pointer(bp))).FzTab = zTab /* Find the number of auxiliary columns */ pStmt = _rtreeCheckPrepare(tls, bp, __ccgo_ts+28028, libc.VaList(bp+64, zDb, zTab)) if pStmt != 0 { nAux = Xsqlite3_column_count(tls, pStmt) - int32(2) Xsqlite3_finalize(tls, pStmt) } else { if (*(*TRtreeCheck)(unsafe.Pointer(bp))).Frc != int32(SQLITE_NOMEM) { (*(*TRtreeCheck)(unsafe.Pointer(bp))).Frc = SQLITE_OK } } /* Find number of dimensions in the rtree table. */ pStmt = _rtreeCheckPrepare(tls, bp, __ccgo_ts+25842, libc.VaList(bp+64, zDb, zTab)) if pStmt != 0 { (*(*TRtreeCheck)(unsafe.Pointer(bp))).FnDim = (Xsqlite3_column_count(tls, pStmt) - int32(1) - nAux) / int32(2) if (*(*TRtreeCheck)(unsafe.Pointer(bp))).FnDim < int32(1) { _rtreeCheckAppendMsg(tls, bp, __ccgo_ts+28056, 0) } else { if int32(SQLITE_ROW) == Xsqlite3_step(tls, pStmt) { (*(*TRtreeCheck)(unsafe.Pointer(bp))).FbInt = libc.BoolInt32(Xsqlite3_column_type(tls, pStmt, int32(1)) == int32(SQLITE_INTEGER)) } } rc = Xsqlite3_finalize(tls, pStmt) if rc != int32(SQLITE_CORRUPT) { (*(*TRtreeCheck)(unsafe.Pointer(bp))).Frc = rc } } /* Do the actual integrity-check */ if (*(*TRtreeCheck)(unsafe.Pointer(bp))).FnDim >= int32(1) { if (*(*TRtreeCheck)(unsafe.Pointer(bp))).Frc == SQLITE_OK { _rtreeCheckNode(tls, bp, 0, uintptr(0), int64(1)) } _rtreeCheckCount(tls, bp, __ccgo_ts+28087, int64((*(*TRtreeCheck)(unsafe.Pointer(bp))).FnLeaf)) _rtreeCheckCount(tls, bp, __ccgo_ts+28094, int64((*(*TRtreeCheck)(unsafe.Pointer(bp))).FnNonLeaf)) } /* Finalize SQL statements used by the integrity-check */ Xsqlite3_finalize(tls, (*(*TRtreeCheck)(unsafe.Pointer(bp))).FpGetNode) Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp + 24))) Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp + 24 + 1*4))) *(*uintptr)(unsafe.Pointer(pzReport)) = (*(*TRtreeCheck)(unsafe.Pointer(bp))).FzReport return (*(*TRtreeCheck)(unsafe.Pointer(bp))).Frc } // C documentation // // /* // ** Implementation of the xIntegrity method for Rtree. // */ func _rtreeIntegrity(tls *libc.TLS, pVtab uintptr, zSchema uintptr, zName uintptr, isQuick int32, pzErr uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var pRtree uintptr var rc int32 _, _ = pRtree, rc pRtree = pVtab _ = libc.Int32FromInt32(0) _ = zSchema _ = zName _ = isQuick rc = _rtreeCheckTable(tls, (*TRtree)(unsafe.Pointer(pRtree)).Fdb, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, (*TRtree)(unsafe.Pointer(pRtree)).FzName, pzErr) if rc == SQLITE_OK && *(*uintptr)(unsafe.Pointer(pzErr)) != 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+28102, libc.VaList(bp+8, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, (*TRtree)(unsafe.Pointer(pRtree)).FzName, *(*uintptr)(unsafe.Pointer(pzErr)))) if *(*uintptr)(unsafe.Pointer(pzErr)) == uintptr(0) { rc = int32(SQLITE_NOMEM) } } return rc } // C documentation // // /* // ** Usage: // ** // ** rtreecheck(); // ** rtreecheck(, ); // ** // ** Invoking this SQL function runs an integrity-check on the named rtree // ** table. The integrity-check verifies the following: // ** // ** 1. For each cell in the r-tree structure (%_node table), that: // ** // ** a) for each dimension, (coord1 <= coord2). // ** // ** b) unless the cell is on the root node, that the cell is bounded // ** by the parent cell on the parent node. // ** // ** c) for leaf nodes, that there is an entry in the %_rowid // ** table corresponding to the cell's rowid value that // ** points to the correct node. // ** // ** d) for cells on non-leaf nodes, that there is an entry in the // ** %_parent table mapping from the cell's child node to the // ** node that it resides on. // ** // ** 2. That there are the same number of entries in the %_rowid table // ** as there are leaf cells in the r-tree structure, and that there // ** is a leaf cell that corresponds to each entry in the %_rowid table. // ** // ** 3. That there are the same number of entries in the %_parent table // ** as there are non-leaf cells in the r-tree structure, and that // ** there is a non-leaf cell that corresponds to each entry in the // ** %_parent table. // */ func _rtreecheck(tls *libc.TLS, ctx uintptr, nArg int32, apArg uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var zDb, zTab, v1 uintptr var _ /* zReport at bp+0 */ uintptr _, _, _, _ = rc, zDb, zTab, v1 if nArg != int32(1) && nArg != int32(2) { Xsqlite3_result_error(tls, ctx, __ccgo_ts+28121, -int32(1)) } else { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) zDb = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(apArg))) if nArg == int32(1) { zTab = zDb zDb = __ccgo_ts + 6585 } else { zTab = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(apArg + 1*4))) } rc = _rtreeCheckTable(tls, Xsqlite3_context_db_handle(tls, ctx), zDb, zTab, bp) if rc == SQLITE_OK { if *(*uintptr)(unsafe.Pointer(bp)) != 0 { v1 = *(*uintptr)(unsafe.Pointer(bp)) } else { v1 = __ccgo_ts + 18461 } Xsqlite3_result_text(tls, ctx, v1, -int32(1), uintptr(-libc.Int32FromInt32(1))) } else { Xsqlite3_result_error_code(tls, ctx, rc) } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp))) } } /* Conditionally include the geopoly code */ /************** Include geopoly.c in the middle of rtree.c *******************/ /************** Begin file geopoly.c *****************************************/ /* ** 2018-05-25 ** ** 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 implements an alternative R-Tree virtual table that ** uses polygons to express the boundaries of 2-dimensional objects. ** ** This file is #include-ed onto the end of "rtree.c" so that it has ** access to all of the R-Tree internals. */ /* #include */ /* Enable -DGEOPOLY_ENABLE_DEBUG for debugging facilities */ /* Character class routines */ /* Use the SQLite core versions if this routine is part of the ** SQLite amalgamation */ // C documentation // // /* // ** Growing our own isspace() routine this way is twice as fast as // ** the library isspace() function. // */ var _geopolyIsSpace = [256]uint8{ 9: uint8(1), 10: uint8(1), 13: uint8(1), 32: uint8(1), } /* Compiler and version */ // C documentation // // /* Datatype for coordinates // */ type TGeoCoord = float32 type GeoCoord = TGeoCoord // C documentation // // /* // ** Internal representation of a polygon. // ** // ** The polygon consists of a sequence of vertexes. There is a line // ** segment between each pair of vertexes, and one final segment from // ** the last vertex back to the first. (This differs from the GeoJSON // ** standard in which the final vertex is a repeat of the first.) // ** // ** The polygon follows the right-hand rule. The area to the right of // ** each segment is "outside" and the area to the left is "inside". // ** // ** The on-disk representation consists of a 4-byte header followed by // ** the values. The 4-byte header is: // ** // ** encoding (1 byte) 0=big-endian, 1=little-endian // ** nvertex (3 bytes) Number of vertexes as a big-endian integer // ** // ** Enough space is allocated for 4 coordinates, to work around over-zealous // ** warnings coming from some compiler (notably, clang). In reality, the size // ** of each GeoPoly memory allocate is adjusted as necessary so that the // ** GeoPoly.a[] array at the end is the appropriate size. // */ type TGeoPoly = struct { FnVertex int32 Fhdr [4]uint8 Fa [8]TGeoCoord } type GeoPoly = TGeoPoly type TGeoPoly1 = struct { FnVertex int32 Fhdr [4]uint8 Fa [8]TGeoCoord } type GeoPoly1 = TGeoPoly1 /* The size of a memory allocation needed for a GeoPoly object sufficient ** to hold N coordinate pairs. */ /* Macros to access coordinates of a GeoPoly. ** We have to use these macros, rather than just say p->a[i] in order ** to silence (incorrect) UBSAN warnings if the array index is too large. */ // C documentation // // /* // ** State of a parse of a GeoJSON input. // */ type TGeoParse = struct { Fz uintptr FnVertex int32 FnAlloc int32 FnErr int32 Fa uintptr } type GeoParse = TGeoParse type TGeoParse1 = struct { Fz uintptr FnVertex int32 FnAlloc int32 FnErr int32 Fa uintptr } type GeoParse1 = TGeoParse1 // C documentation // // /* Do a 4-byte byte swap */ func _geopolySwab32(tls *libc.TLS, a uintptr) { var t uint8 _ = t t = *(*uint8)(unsafe.Pointer(a)) *(*uint8)(unsafe.Pointer(a)) = *(*uint8)(unsafe.Pointer(a + 3)) *(*uint8)(unsafe.Pointer(a + 3)) = t t = *(*uint8)(unsafe.Pointer(a + 1)) *(*uint8)(unsafe.Pointer(a + 1)) = *(*uint8)(unsafe.Pointer(a + 2)) *(*uint8)(unsafe.Pointer(a + 2)) = t } // C documentation // // /* Skip whitespace. Return the next non-whitespace character. */ func _geopolySkipSpace(tls *libc.TLS, p uintptr) (r uint8) { for _geopolyIsSpace[*(*uint8)(unsafe.Pointer((*TGeoParse)(unsafe.Pointer(p)).Fz))] != 0 { (*TGeoParse)(unsafe.Pointer(p)).Fz++ } return *(*uint8)(unsafe.Pointer((*TGeoParse)(unsafe.Pointer(p)).Fz)) } // C documentation // // /* Parse out a number. Write the value into *pVal if pVal!=0. // ** return non-zero on success and zero if the next token is not a number. // */ func _geopolyParseNumber(tls *libc.TLS, p uintptr, pVal uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var c uint8 var j, seenDP, seenE, v2 int32 var z uintptr var _ /* r at bp+0 */ float64 _, _, _, _, _, _ = c, j, seenDP, seenE, z, v2 c = _geopolySkipSpace(tls, p) z = (*TGeoParse)(unsafe.Pointer(p)).Fz j = 0 seenDP = 0 seenE = 0 if int32(c) == int32('-') { j = int32(1) c = *(*uint8)(unsafe.Pointer(z + uintptr(j))) } if int32(c) == int32('0') && int32(*(*uint8)(unsafe.Pointer(z + uintptr(j+int32(1))))) >= int32('0') && int32(*(*uint8)(unsafe.Pointer(z + uintptr(j+int32(1))))) <= int32('9') { return 0 } for { c = *(*uint8)(unsafe.Pointer(z + uintptr(j))) if int32(_sqlite3CtypeMap[c])&int32(0x04) != 0 { goto _1 } if int32(c) == int32('.') { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j-int32(1))))) == int32('-') { return 0 } if seenDP != 0 { return 0 } seenDP = int32(1) goto _1 } if int32(c) == int32('e') || int32(c) == int32('E') { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j-int32(1))))) < int32('0') { return 0 } if seenE != 0 { return -int32(1) } v2 = libc.Int32FromInt32(1) seenE = v2 seenDP = v2 c = *(*uint8)(unsafe.Pointer(z + uintptr(j+int32(1)))) if int32(c) == int32('+') || int32(c) == int32('-') { j++ c = *(*uint8)(unsafe.Pointer(z + uintptr(j+int32(1)))) } if int32(c) < int32('0') || int32(c) > int32('9') { return 0 } goto _1 } break goto _1 _1: ; j++ } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(j-int32(1))))) < int32('0') { return 0 } if pVal != 0 { _sqlite3AtoF(tls, (*TGeoParse)(unsafe.Pointer(p)).Fz, bp, j, uint8(SQLITE_UTF8)) *(*TGeoCoord)(unsafe.Pointer(pVal)) = float32(*(*float64)(unsafe.Pointer(bp))) } *(*uintptr)(unsafe.Pointer(p)) += uintptr(j) return int32(1) } // C documentation // // /* // ** If the input is a well-formed JSON array of coordinates with at least // ** four coordinates and where each coordinate is itself a two-value array, // ** then convert the JSON into a GeoPoly object and return a pointer to // ** that object. // ** // ** If any error occurs, return NULL. // */ func _geopolyParseJson(tls *libc.TLS, z uintptr, pRc uintptr) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var aNew, pOut, v1 uintptr var c uint8 var ii, rc int32 var v2 bool var _ /* s at bp+0 */ TGeoParse var _ /* x at bp+20 */ int32 _, _, _, _, _, _, _ = aNew, c, ii, pOut, rc, v1, v2 rc = SQLITE_OK libc.Xmemset(tls, bp, 0, uint32(20)) (*(*TGeoParse)(unsafe.Pointer(bp))).Fz = z if int32(_geopolySkipSpace(tls, bp)) == int32('[') { (*(*TGeoParse)(unsafe.Pointer(bp))).Fz++ for int32(_geopolySkipSpace(tls, bp)) == int32('[') { ii = 0 (*(*TGeoParse)(unsafe.Pointer(bp))).Fz++ if (*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex >= (*(*TGeoParse)(unsafe.Pointer(bp))).FnAlloc { (*(*TGeoParse)(unsafe.Pointer(bp))).FnAlloc = (*(*TGeoParse)(unsafe.Pointer(bp))).FnAlloc*int32(2) + int32(16) aNew = Xsqlite3_realloc64(tls, (*(*TGeoParse)(unsafe.Pointer(bp))).Fa, uint64(uint32((*(*TGeoParse)(unsafe.Pointer(bp))).FnAlloc)*uint32(4)*uint32(2))) if aNew == uintptr(0) { rc = int32(SQLITE_NOMEM) (*(*TGeoParse)(unsafe.Pointer(bp))).FnErr++ break } (*(*TGeoParse)(unsafe.Pointer(bp))).Fa = aNew } for { if ii <= int32(1) { v1 = (*(*TGeoParse)(unsafe.Pointer(bp))).Fa + uintptr((*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex*int32(2)+ii)*4 } else { v1 = uintptr(0) } if !(_geopolyParseNumber(tls, bp, v1) != 0) { break } ii++ if ii == int32(2) { (*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex++ } c = _geopolySkipSpace(tls, bp) (*(*TGeoParse)(unsafe.Pointer(bp))).Fz++ if int32(c) == int32(',') { continue } if int32(c) == int32(']') && ii >= int32(2) { break } (*(*TGeoParse)(unsafe.Pointer(bp))).FnErr++ rc = int32(SQLITE_ERROR) goto parse_json_err } if int32(_geopolySkipSpace(tls, bp)) == int32(',') { (*(*TGeoParse)(unsafe.Pointer(bp))).Fz++ continue } break } if v2 = int32(_geopolySkipSpace(tls, bp)) == int32(']') && (*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex >= int32(4) && *(*TGeoCoord)(unsafe.Pointer((*(*TGeoParse)(unsafe.Pointer(bp))).Fa)) == *(*TGeoCoord)(unsafe.Pointer((*(*TGeoParse)(unsafe.Pointer(bp))).Fa + uintptr((*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex*int32(2)-int32(2))*4)) && *(*TGeoCoord)(unsafe.Pointer((*(*TGeoParse)(unsafe.Pointer(bp))).Fa + 1*4)) == *(*TGeoCoord)(unsafe.Pointer((*(*TGeoParse)(unsafe.Pointer(bp))).Fa + uintptr((*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex*int32(2)-int32(1))*4)); v2 { (*(*TGeoParse)(unsafe.Pointer(bp))).Fz++ } if v2 && int32(_geopolySkipSpace(tls, bp)) == libc.Int32FromInt32(0) { *(*int32)(unsafe.Pointer(bp + 20)) = int32(1) (*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex-- /* Remove the redundant vertex at the end */ pOut = Xsqlite3_malloc64(tls, uint64(libc.Int64FromInt64(40)+int64(libc.Uint32FromInt64(4)*libc.Uint32FromInt32(2))*(int64((*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex)-libc.Int64FromInt32(4)))) *(*int32)(unsafe.Pointer(bp + 20)) = int32(1) if pOut == uintptr(0) { goto parse_json_err } (*TGeoPoly)(unsafe.Pointer(pOut)).FnVertex = (*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex libc.Xmemcpy(tls, pOut+8, (*(*TGeoParse)(unsafe.Pointer(bp))).Fa, uint32((*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex*int32(2))*uint32(4)) *(*uint8)(unsafe.Pointer(pOut + 4)) = *(*uint8)(unsafe.Pointer(bp + 20)) *(*uint8)(unsafe.Pointer(pOut + 4 + 1)) = uint8((*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex >> int32(16) & int32(0xff)) *(*uint8)(unsafe.Pointer(pOut + 4 + 2)) = uint8((*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex >> int32(8) & int32(0xff)) *(*uint8)(unsafe.Pointer(pOut + 4 + 3)) = uint8((*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex & int32(0xff)) Xsqlite3_free(tls, (*(*TGeoParse)(unsafe.Pointer(bp))).Fa) if pRc != 0 { *(*int32)(unsafe.Pointer(pRc)) = SQLITE_OK } return pOut } else { (*(*TGeoParse)(unsafe.Pointer(bp))).FnErr++ rc = int32(SQLITE_ERROR) } } goto parse_json_err parse_json_err: ; if pRc != 0 { *(*int32)(unsafe.Pointer(pRc)) = rc } Xsqlite3_free(tls, (*(*TGeoParse)(unsafe.Pointer(bp))).Fa) return uintptr(0) } // C documentation // // /* // ** Given a function parameter, try to interpret it as a polygon, either // ** in the binary format or JSON text. Compute a GeoPoly object and // ** return a pointer to that object. Or if the input is not a well-formed // ** polygon, put an error message in sqlite3_context and return NULL. // */ func _geopolyFuncParam(tls *libc.TLS, pCtx uintptr, pVal uintptr, pRc uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var a, p, zJson, p4 uintptr var ii, nByte, nVertex, v1 int32 var v2 bool var _ /* x at bp+0 */ int32 _, _, _, _, _, _, _, _, _ = a, ii, nByte, nVertex, p, zJson, v1, v2, p4 p = uintptr(0) if v2 = Xsqlite3_value_type(tls, pVal) == int32(SQLITE_BLOB); v2 { v1 = Xsqlite3_value_bytes(tls, pVal) nByte = v1 } if v2 && v1 >= int32(libc.Uint32FromInt32(4)+libc.Uint32FromInt32(6)*libc.Uint32FromInt64(4)) { a = Xsqlite3_value_blob(tls, pVal) if a == uintptr(0) { if pCtx != 0 { Xsqlite3_result_error_nomem(tls, pCtx) } return uintptr(0) } nVertex = int32(*(*uint8)(unsafe.Pointer(a + 1)))<. // ** Additional arguments are added as attributes to the . // */ func _geopolySvgFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var cSep uint8 var db, p, x, z uintptr var i int32 _, _, _, _, _, _ = cSep, db, i, p, x, z if argc < int32(1) { return } p = _geopolyFuncParam(tls, context, *(*uintptr)(unsafe.Pointer(argv)), uintptr(0)) if p != 0 { db = Xsqlite3_context_db_handle(tls, context) x = Xsqlite3_str_new(tls, db) cSep = uint8('\'') Xsqlite3_str_appendf(tls, x, __ccgo_ts+28194, 0) i = 0 for { if !(i < (*TGeoPoly)(unsafe.Pointer(p)).FnVertex) { break } Xsqlite3_str_appendf(tls, x, __ccgo_ts+28212, libc.VaList(bp+8, int32(cSep), float64(*(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(i*int32(2))*4))), float64(*(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(i*int32(2)+int32(1))*4))))) cSep = uint8(' ') goto _1 _1: ; i++ } Xsqlite3_str_appendf(tls, x, __ccgo_ts+28220, libc.VaList(bp+8, float64(*(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2))*4))), float64(*(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2)+libc.Int32FromInt32(1))*4))))) i = int32(1) for { if !(i < argc) { break } z = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*4))) if z != 0 && *(*uint8)(unsafe.Pointer(z)) != 0 { Xsqlite3_str_appendf(tls, x, __ccgo_ts+28228, libc.VaList(bp+8, z)) } goto _2 _2: ; i++ } Xsqlite3_str_appendf(tls, x, __ccgo_ts+28232, 0) Xsqlite3_result_text(tls, context, Xsqlite3_str_finish(tls, x), -int32(1), __ccgo_fp(Xsqlite3_free)) Xsqlite3_free(tls, p) } } // C documentation // // /* // ** SQL Function: geopoly_xform(poly, A, B, C, D, E, F) // ** // ** Transform and/or translate a polygon as follows: // ** // ** x1 = A*x0 + B*y0 + E // ** y1 = C*x0 + D*y0 + F // ** // ** For a translation: // ** // ** geopoly_xform(poly, 1, 0, 0, 1, x-offset, y-offset) // ** // ** Rotate by R around the point (0,0): // ** // ** geopoly_xform(poly, cos(R), sin(R), -sin(R), cos(R), 0, 0) // */ func _geopolyXformFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var A, B, C, D, E, F float64 var ii int32 var p uintptr var x0, x1, y0, y1 TGeoCoord _, _, _, _, _, _, _, _, _, _, _, _ = A, B, C, D, E, F, ii, p, x0, x1, y0, y1 p = _geopolyFuncParam(tls, context, *(*uintptr)(unsafe.Pointer(argv)), uintptr(0)) A = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) B = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4))) C = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 3*4))) D = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 4*4))) E = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 5*4))) F = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 6*4))) _ = argc if p != 0 { ii = 0 for { if !(ii < (*TGeoPoly)(unsafe.Pointer(p)).FnVertex) { break } x0 = *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(ii*int32(2))*4)) y0 = *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(ii*int32(2)+int32(1))*4)) x1 = float32(A*float64(x0) + B*float64(y0) + E) y1 = float32(C*float64(x0) + D*float64(y0) + F) *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(ii*int32(2))*4)) = x1 *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(ii*int32(2)+int32(1))*4)) = y1 goto _1 _1: ; ii++ } Xsqlite3_result_blob(tls, context, p+4, int32(4)+int32(8)*(*TGeoPoly)(unsafe.Pointer(p)).FnVertex, uintptr(-libc.Int32FromInt32(1))) Xsqlite3_free(tls, p) } } // C documentation // // /* // ** Compute the area enclosed by the polygon. // ** // ** This routine can also be used to detect polygons that rotate in // ** the wrong direction. Polygons are suppose to be counter-clockwise (CCW). // ** This routine returns a negative value for clockwise (CW) polygons. // */ func _geopolyArea(tls *libc.TLS, p uintptr) (r float64) { var ii int32 var rArea float64 _, _ = ii, rArea rArea = float64(0) ii = 0 for { if !(ii < (*TGeoPoly)(unsafe.Pointer(p)).FnVertex-int32(1)) { break } rArea += float64((*(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(ii*int32(2))*4))-*(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr((ii+int32(1))*int32(2))*4)))*(*(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(ii*int32(2)+int32(1))*4))+*(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr((ii+int32(1))*int32(2)+int32(1))*4)))) * float64(0.5) goto _1 _1: ; ii++ } rArea += float64((*(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(ii*int32(2))*4))-*(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2))*4)))*(*(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(ii*int32(2)+int32(1))*4))+*(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2)+libc.Int32FromInt32(1))*4)))) * float64(0.5) return rArea } // C documentation // // /* // ** Implementation of the geopoly_area(X) function. // ** // ** If the input is a well-formed Geopoly BLOB then return the area // ** enclosed by the polygon. If the polygon circulates clockwise instead // ** of counterclockwise (as it should) then return the negative of the // ** enclosed area. Otherwise return NULL. // */ func _geopolyAreaFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var p uintptr _ = p p = _geopolyFuncParam(tls, context, *(*uintptr)(unsafe.Pointer(argv)), uintptr(0)) _ = argc if p != 0 { Xsqlite3_result_double(tls, context, _geopolyArea(tls, p)) Xsqlite3_free(tls, p) } } // C documentation // // /* // ** Implementation of the geopoly_ccw(X) function. // ** // ** If the rotation of polygon X is clockwise (incorrect) instead of // ** counter-clockwise (the correct winding order according to RFC7946) // ** then reverse the order of the vertexes in polygon X. // ** // ** In other words, this routine returns a CCW polygon regardless of the // ** winding order of its input. // ** // ** Use this routine to sanitize historical inputs that that sometimes // ** contain polygons that wind in the wrong direction. // */ func _geopolyCcwFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var ii, jj int32 var p uintptr var t TGeoCoord _, _, _, _ = ii, jj, p, t p = _geopolyFuncParam(tls, context, *(*uintptr)(unsafe.Pointer(argv)), uintptr(0)) _ = argc if p != 0 { if _geopolyArea(tls, p) < float64(0) { ii = int32(1) jj = (*TGeoPoly)(unsafe.Pointer(p)).FnVertex - libc.Int32FromInt32(1) for { if !(ii < jj) { break } t = *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(ii*int32(2))*4)) *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(ii*int32(2))*4)) = *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(jj*int32(2))*4)) *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(jj*int32(2))*4)) = t t = *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(ii*int32(2)+int32(1))*4)) *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(ii*int32(2)+int32(1))*4)) = *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(jj*int32(2)+int32(1))*4)) *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(jj*int32(2)+int32(1))*4)) = t goto _1 _1: ; ii++ jj-- } } Xsqlite3_result_blob(tls, context, p+4, int32(4)+int32(8)*(*TGeoPoly)(unsafe.Pointer(p)).FnVertex, uintptr(-libc.Int32FromInt32(1))) Xsqlite3_free(tls, p) } } // C documentation // // /* Fast approximation for sine(X) for X between -0.5*pi and 2*pi // */ func _geopolySine(tls *libc.TLS, r float64) (r1 float64) { var r2, r3, r5 float64 _, _, _ = r2, r3, r5 _ = libc.Int32FromInt32(0) if r >= libc.Float64FromFloat64(1.5)*libc.Float64FromFloat64(3.141592653589793) { r -= libc.Float64FromFloat64(2) * libc.Float64FromFloat64(3.141592653589793) } if r >= libc.Float64FromFloat64(0.5)*libc.Float64FromFloat64(3.141592653589793) { return -_geopolySine(tls, r-float64(3.141592653589793)) } else { r2 = r * r r3 = r2 * r r5 = r3 * r2 return float64(0.9996949)*r - float64(0.16567)*r3 + float64(0.0075134)*r5 } return r1 } // C documentation // // /* // ** Function: geopoly_regular(X,Y,R,N) // ** // ** Construct a simple, convex, regular polygon centered at X, Y // ** with circumradius R and with N sides. // */ func _geopolyRegularFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var n int32 var p uintptr var r, rAngle, x, y float64 var _ /* i at bp+0 */ int32 _, _, _, _, _, _ = n, p, r, rAngle, x, y x = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv))) y = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) r = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4))) n = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 3*4))) _ = argc if n < int32(3) || r <= float64(0) { return } if n > int32(1000) { n = int32(1000) } p = Xsqlite3_malloc64(tls, uint64(uint32(40)+uint32((n-int32(1))*int32(2))*uint32(4))) if p == uintptr(0) { Xsqlite3_result_error_nomem(tls, context) return } *(*int32)(unsafe.Pointer(bp)) = int32(1) *(*uint8)(unsafe.Pointer(p + 4)) = *(*uint8)(unsafe.Pointer(bp)) *(*uint8)(unsafe.Pointer(p + 4 + 1)) = uint8(0) *(*uint8)(unsafe.Pointer(p + 4 + 2)) = uint8(n >> int32(8) & int32(0xff)) *(*uint8)(unsafe.Pointer(p + 4 + 3)) = uint8(n & int32(0xff)) *(*int32)(unsafe.Pointer(bp)) = 0 for { if !(*(*int32)(unsafe.Pointer(bp)) < n) { break } rAngle = libc.Float64FromFloat64(2) * libc.Float64FromFloat64(3.141592653589793) * float64(*(*int32)(unsafe.Pointer(bp))) / float64(n) *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(*(*int32)(unsafe.Pointer(bp))*int32(2))*4)) = float32(x - r*_geopolySine(tls, rAngle-libc.Float64FromFloat64(0.5)*libc.Float64FromFloat64(3.141592653589793))) *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(*(*int32)(unsafe.Pointer(bp))*int32(2)+int32(1))*4)) = float32(y + r*_geopolySine(tls, rAngle)) goto _1 _1: ; *(*int32)(unsafe.Pointer(bp))++ } Xsqlite3_result_blob(tls, context, p+4, int32(4)+int32(8)*n, uintptr(-libc.Int32FromInt32(1))) Xsqlite3_free(tls, p) } // C documentation // // /* // ** If pPoly is a polygon, compute its bounding box. Then: // ** // ** (1) if aCoord!=0 store the bounding box in aCoord, returning NULL // ** (2) otherwise, compute a GeoPoly for the bounding box and return the // ** new GeoPoly // ** // ** If pPoly is NULL but aCoord is not NULL, then compute a new GeoPoly from // ** the bounding box in aCoord and return a pointer to that GeoPoly. // */ func _geopolyBBox(tls *libc.TLS, context uintptr, pPoly uintptr, aCoord uintptr, pRc uintptr) (r1 uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var mnX, mnY, mxX, mxY, v3, v4 float32 var p, pOut uintptr var r float64 var _ /* ii at bp+0 */ int32 _, _, _, _, _, _, _, _, _ = mnX, mnY, mxX, mxY, p, pOut, r, v3, v4 pOut = uintptr(0) if pPoly == uintptr(0) && aCoord != uintptr(0) { p = uintptr(0) mnX = *(*TRtreeValue)(unsafe.Pointer(aCoord)) mxX = *(*TRtreeValue)(unsafe.Pointer(aCoord + 1*4)) mnY = *(*TRtreeValue)(unsafe.Pointer(aCoord + 2*4)) mxY = *(*TRtreeValue)(unsafe.Pointer(aCoord + 3*4)) goto geopolyBboxFill } else { p = _geopolyFuncParam(tls, context, pPoly, pRc) } if !(p != 0) { goto _1 } v3 = *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2))*4)) mxX = v3 mnX = v3 v4 = *(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2)+libc.Int32FromInt32(1))*4)) mxY = v4 mnY = v4 *(*int32)(unsafe.Pointer(bp)) = int32(1) for { if !(*(*int32)(unsafe.Pointer(bp)) < (*TGeoPoly)(unsafe.Pointer(p)).FnVertex) { break } r = float64(*(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(*(*int32)(unsafe.Pointer(bp))*int32(2))*4))) if r < float64(mnX) { mnX = float32(r) } else { if r > float64(mxX) { mxX = float32(r) } } r = float64(*(*TGeoCoord)(unsafe.Pointer(p + 8 + uintptr(*(*int32)(unsafe.Pointer(bp))*int32(2)+int32(1))*4))) if r < float64(mnY) { mnY = float32(r) } else { if r > float64(mxY) { mxY = float32(r) } } goto _5 _5: ; *(*int32)(unsafe.Pointer(bp))++ } if pRc != 0 { *(*int32)(unsafe.Pointer(pRc)) = SQLITE_OK } if !(aCoord == uintptr(0)) { goto _6 } goto geopolyBboxFill geopolyBboxFill: ; pOut = Xsqlite3_realloc64(tls, p, uint64(libc.Uint32FromInt64(40)+libc.Uint32FromInt64(4)*libc.Uint32FromInt32(2)*uint32(libc.Int32FromInt32(4)-libc.Int32FromInt32(4)))) if pOut == uintptr(0) { Xsqlite3_free(tls, p) if context != 0 { Xsqlite3_result_error_nomem(tls, context) } if pRc != 0 { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) } return uintptr(0) } (*TGeoPoly)(unsafe.Pointer(pOut)).FnVertex = int32(4) *(*int32)(unsafe.Pointer(bp)) = int32(1) *(*uint8)(unsafe.Pointer(pOut + 4)) = *(*uint8)(unsafe.Pointer(bp)) *(*uint8)(unsafe.Pointer(pOut + 4 + 1)) = uint8(0) *(*uint8)(unsafe.Pointer(pOut + 4 + 2)) = uint8(0) *(*uint8)(unsafe.Pointer(pOut + 4 + 3)) = uint8(4) *(*TGeoCoord)(unsafe.Pointer(pOut + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2))*4)) = mnX *(*TGeoCoord)(unsafe.Pointer(pOut + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2)+libc.Int32FromInt32(1))*4)) = mnY *(*TGeoCoord)(unsafe.Pointer(pOut + 8 + uintptr(libc.Int32FromInt32(1)*libc.Int32FromInt32(2))*4)) = mxX *(*TGeoCoord)(unsafe.Pointer(pOut + 8 + uintptr(libc.Int32FromInt32(1)*libc.Int32FromInt32(2)+libc.Int32FromInt32(1))*4)) = mnY *(*TGeoCoord)(unsafe.Pointer(pOut + 8 + uintptr(libc.Int32FromInt32(2)*libc.Int32FromInt32(2))*4)) = mxX *(*TGeoCoord)(unsafe.Pointer(pOut + 8 + uintptr(libc.Int32FromInt32(2)*libc.Int32FromInt32(2)+libc.Int32FromInt32(1))*4)) = mxY *(*TGeoCoord)(unsafe.Pointer(pOut + 8 + uintptr(libc.Int32FromInt32(3)*libc.Int32FromInt32(2))*4)) = mnX *(*TGeoCoord)(unsafe.Pointer(pOut + 8 + uintptr(libc.Int32FromInt32(3)*libc.Int32FromInt32(2)+libc.Int32FromInt32(1))*4)) = mxY goto _7 _6: ; Xsqlite3_free(tls, p) *(*TRtreeValue)(unsafe.Pointer(aCoord)) = mnX *(*TRtreeValue)(unsafe.Pointer(aCoord + 1*4)) = mxX *(*TRtreeValue)(unsafe.Pointer(aCoord + 2*4)) = mnY *(*TRtreeValue)(unsafe.Pointer(aCoord + 3*4)) = mxY _7: ; goto _2 _1: ; if aCoord != 0 { libc.Xmemset(tls, aCoord, 0, libc.Uint32FromInt64(4)*libc.Uint32FromInt32(4)) } _2: ; return pOut } // C documentation // // /* // ** Implementation of the geopoly_bbox(X) SQL function. // */ func _geopolyBBoxFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var p uintptr _ = p p = _geopolyBBox(tls, context, *(*uintptr)(unsafe.Pointer(argv)), uintptr(0), uintptr(0)) _ = argc if p != 0 { Xsqlite3_result_blob(tls, context, p+4, int32(4)+int32(8)*(*TGeoPoly)(unsafe.Pointer(p)).FnVertex, uintptr(-libc.Int32FromInt32(1))) Xsqlite3_free(tls, p) } } // C documentation // // /* // ** State vector for the geopoly_group_bbox() aggregate function. // */ type TGeoBBox = struct { FisInit int32 Fa [4]TRtreeCoord } type GeoBBox = TGeoBBox type TGeoBBox1 = struct { FisInit int32 Fa [4]TRtreeCoord } type GeoBBox1 = TGeoBBox1 // C documentation // // /* // ** Implementation of the geopoly_group_bbox(X) aggregate SQL function. // */ func _geopolyBBoxStep(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var pBBox uintptr var _ /* a at bp+0 */ [4]TRtreeCoord var _ /* rc at bp+16 */ int32 _ = pBBox *(*int32)(unsafe.Pointer(bp + 16)) = SQLITE_OK _ = argc _geopolyBBox(tls, context, *(*uintptr)(unsafe.Pointer(argv)), bp, bp+16) if *(*int32)(unsafe.Pointer(bp + 16)) == SQLITE_OK { pBBox = Xsqlite3_aggregate_context(tls, context, int32(20)) if pBBox == uintptr(0) { return } if libc.AtomicLoadPInt32(pBBox) == 0 { libc.AtomicStorePInt32(pBBox, int32(1)) libc.Xmemcpy(tls, pBBox+4, bp, libc.Uint32FromInt64(4)*libc.Uint32FromInt32(4)) } else { if *(*TRtreeValue)(unsafe.Pointer(bp)) < *(*TRtreeValue)(unsafe.Pointer(pBBox + 4)) { *(*TRtreeCoord)(unsafe.Pointer(pBBox + 4)) = (*(*[4]TRtreeCoord)(unsafe.Pointer(bp)))[0] } if *(*TRtreeValue)(unsafe.Pointer(bp + 1*4)) > *(*TRtreeValue)(unsafe.Pointer(pBBox + 4 + 1*4)) { *(*TRtreeCoord)(unsafe.Pointer(pBBox + 4 + 1*4)) = (*(*[4]TRtreeCoord)(unsafe.Pointer(bp)))[int32(1)] } if *(*TRtreeValue)(unsafe.Pointer(bp + 2*4)) < *(*TRtreeValue)(unsafe.Pointer(pBBox + 4 + 2*4)) { *(*TRtreeCoord)(unsafe.Pointer(pBBox + 4 + 2*4)) = (*(*[4]TRtreeCoord)(unsafe.Pointer(bp)))[int32(2)] } if *(*TRtreeValue)(unsafe.Pointer(bp + 3*4)) > *(*TRtreeValue)(unsafe.Pointer(pBBox + 4 + 3*4)) { *(*TRtreeCoord)(unsafe.Pointer(pBBox + 4 + 3*4)) = (*(*[4]TRtreeCoord)(unsafe.Pointer(bp)))[int32(3)] } } } } func _geopolyBBoxFinal(tls *libc.TLS, context uintptr) { var p, pBBox uintptr _, _ = p, pBBox pBBox = Xsqlite3_aggregate_context(tls, context, 0) if pBBox == uintptr(0) { return } p = _geopolyBBox(tls, context, uintptr(0), pBBox+4, uintptr(0)) if p != 0 { Xsqlite3_result_blob(tls, context, p+4, int32(4)+int32(8)*(*TGeoPoly)(unsafe.Pointer(p)).FnVertex, uintptr(-libc.Int32FromInt32(1))) Xsqlite3_free(tls, p) } } // C documentation // // /* // ** Determine if point (x0,y0) is beneath line segment (x1,y1)->(x2,y2). // ** Returns: // ** // ** +2 x0,y0 is on the line segement // ** // ** +1 x0,y0 is beneath line segment // ** // ** 0 x0,y0 is not on or beneath the line segment or the line segment // ** is vertical and x0,y0 is not on the line segment // ** // ** The left-most coordinate min(x1,x2) is not considered to be part of // ** the line segment for the purposes of this analysis. // */ func _pointBeneathLine(tls *libc.TLS, x0 float64, y0 float64, x1 float64, y1 float64, x2 float64, y2 float64) (r int32) { var y float64 _ = y if x0 == x1 && y0 == y1 { return int32(2) } if x1 < x2 { if x0 <= x1 || x0 > x2 { return 0 } } else { if x1 > x2 { if x0 <= x2 || x0 > x1 { return 0 } } else { /* Vertical line segment */ if x0 != x1 { return 0 } if y0 < y1 && y0 < y2 { return 0 } if y0 > y1 && y0 > y2 { return 0 } return int32(2) } } y = y1 + (y2-y1)*(x0-x1)/(x2-x1) if y0 == y { return int32(2) } if y0 < y { return int32(1) } return 0 } // C documentation // // /* // ** SQL function: geopoly_contains_point(P,X,Y) // ** // ** Return +2 if point X,Y is within polygon P. // ** Return +1 if point X,Y is on the polygon boundary. // ** Return 0 if point X,Y is outside the polygon // */ func _geopolyContainsPointFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var cnt, ii, v int32 var p1 uintptr var x0, y0 float64 _, _, _, _, _, _ = cnt, ii, p1, v, x0, y0 p1 = _geopolyFuncParam(tls, context, *(*uintptr)(unsafe.Pointer(argv)), uintptr(0)) x0 = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) y0 = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4))) v = 0 cnt = 0 _ = argc if p1 == uintptr(0) { return } ii = 0 for { if !(ii < (*TGeoPoly)(unsafe.Pointer(p1)).FnVertex-int32(1)) { break } v = _pointBeneathLine(tls, x0, y0, float64(*(*TGeoCoord)(unsafe.Pointer(p1 + 8 + uintptr(ii*int32(2))*4))), float64(*(*TGeoCoord)(unsafe.Pointer(p1 + 8 + uintptr(ii*int32(2)+int32(1))*4))), float64(*(*TGeoCoord)(unsafe.Pointer(p1 + 8 + uintptr((ii+int32(1))*int32(2))*4))), float64(*(*TGeoCoord)(unsafe.Pointer(p1 + 8 + uintptr((ii+int32(1))*int32(2)+int32(1))*4)))) if v == int32(2) { break } cnt += v goto _1 _1: ; ii++ } if v != int32(2) { v = _pointBeneathLine(tls, x0, y0, float64(*(*TGeoCoord)(unsafe.Pointer(p1 + 8 + uintptr(ii*int32(2))*4))), float64(*(*TGeoCoord)(unsafe.Pointer(p1 + 8 + uintptr(ii*int32(2)+int32(1))*4))), float64(*(*TGeoCoord)(unsafe.Pointer(p1 + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2))*4))), float64(*(*TGeoCoord)(unsafe.Pointer(p1 + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2)+libc.Int32FromInt32(1))*4)))) } if v == int32(2) { Xsqlite3_result_int(tls, context, int32(1)) } else { if (v+cnt)&int32(1) == 0 { Xsqlite3_result_int(tls, context, 0) } else { Xsqlite3_result_int(tls, context, int32(2)) } } Xsqlite3_free(tls, p1) } // C documentation // // /* // ** SQL function: geopoly_within(P1,P2) // ** // ** Return +2 if P1 and P2 are the same polygon // ** Return +1 if P2 is contained within P1 // ** Return 0 if any part of P2 is on the outside of P1 // ** // */ func _geopolyWithinFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var p1, p2 uintptr var x, v1, v2 int32 _, _, _, _, _ = p1, p2, x, v1, v2 p1 = _geopolyFuncParam(tls, context, *(*uintptr)(unsafe.Pointer(argv)), uintptr(0)) p2 = _geopolyFuncParam(tls, context, *(*uintptr)(unsafe.Pointer(argv + 1*4)), uintptr(0)) _ = argc if p1 != 0 && p2 != 0 { x = _geopolyOverlap(tls, p1, p2) if x < 0 { Xsqlite3_result_error_nomem(tls, context) } else { if x == int32(2) { v1 = int32(1) } else { if x == int32(4) { v2 = int32(2) } else { v2 = 0 } v1 = v2 } Xsqlite3_result_int(tls, context, v1) } } Xsqlite3_free(tls, p1) Xsqlite3_free(tls, p2) } // C documentation // // /* Objects used by the overlap algorihm. */ type TGeoEvent = struct { F__ccgo_align [0]uint32 Fx float64 FeType int32 FpSeg uintptr FpNext uintptr F__ccgo_pad4 [4]byte } type GeoEvent = TGeoEvent type TGeoSegment = struct { F__ccgo_align [0]uint32 FC float64 FB float64 Fy float64 Fy0 float32 Fside uint8 Fidx uint32 FpNext uintptr } type GeoSegment = TGeoSegment type TGeoOverlap = struct { FaEvent uintptr FaSegment uintptr FnEvent int32 FnSegment int32 } type GeoOverlap = TGeoOverlap type TGeoEvent1 = struct { F__ccgo_align [0]uint32 Fx float64 FeType int32 FpSeg uintptr FpNext uintptr F__ccgo_pad4 [4]byte } type GeoEvent1 = TGeoEvent1 type TGeoSegment1 = struct { F__ccgo_align [0]uint32 FC float64 FB float64 Fy float64 Fy0 float32 Fside uint8 Fidx uint32 FpNext uintptr } type GeoSegment1 = TGeoSegment1 type TGeoOverlap1 = struct { FaEvent uintptr FaSegment uintptr FnEvent int32 FnSegment int32 } type GeoOverlap1 = TGeoOverlap1 // C documentation // // /* // ** Add a single segment and its associated events. // */ func _geopolyAddOneSegment(tls *libc.TLS, p uintptr, x0 TGeoCoord, y0 TGeoCoord, x1 TGeoCoord, y1 TGeoCoord, side uint8, idx uint32) { var pEvent, pSeg uintptr var t TGeoCoord _, _, _ = pEvent, pSeg, t if x0 == x1 { return } /* Ignore vertical segments */ if x0 > x1 { t = x0 x0 = x1 x1 = t t = y0 y0 = y1 y1 = t } pSeg = (*TGeoOverlap)(unsafe.Pointer(p)).FaSegment + uintptr((*TGeoOverlap)(unsafe.Pointer(p)).FnSegment)*40 (*TGeoOverlap)(unsafe.Pointer(p)).FnSegment++ (*TGeoSegment)(unsafe.Pointer(pSeg)).FC = float64((y1 - y0) / (x1 - x0)) (*TGeoSegment)(unsafe.Pointer(pSeg)).FB = float64(y1) - float64(x1)*(*TGeoSegment)(unsafe.Pointer(pSeg)).FC (*TGeoSegment)(unsafe.Pointer(pSeg)).Fy0 = y0 (*TGeoSegment)(unsafe.Pointer(pSeg)).Fside = side (*TGeoSegment)(unsafe.Pointer(pSeg)).Fidx = idx pEvent = (*TGeoOverlap)(unsafe.Pointer(p)).FaEvent + uintptr((*TGeoOverlap)(unsafe.Pointer(p)).FnEvent)*24 (*TGeoOverlap)(unsafe.Pointer(p)).FnEvent++ (*TGeoEvent)(unsafe.Pointer(pEvent)).Fx = float64(x0) (*TGeoEvent)(unsafe.Pointer(pEvent)).FeType = 0 (*TGeoEvent)(unsafe.Pointer(pEvent)).FpSeg = pSeg pEvent = (*TGeoOverlap)(unsafe.Pointer(p)).FaEvent + uintptr((*TGeoOverlap)(unsafe.Pointer(p)).FnEvent)*24 (*TGeoOverlap)(unsafe.Pointer(p)).FnEvent++ (*TGeoEvent)(unsafe.Pointer(pEvent)).Fx = float64(x1) (*TGeoEvent)(unsafe.Pointer(pEvent)).FeType = int32(1) (*TGeoEvent)(unsafe.Pointer(pEvent)).FpSeg = pSeg } // C documentation // // /* // ** Insert all segments and events for polygon pPoly. // */ func _geopolyAddSegments(tls *libc.TLS, p uintptr, pPoly uintptr, side uint8) { var i uint32 var x uintptr _, _ = i, x i = uint32(0) for { if !(i < uint32((*TGeoPoly)(unsafe.Pointer(pPoly)).FnVertex)-uint32(1)) { break } x = pPoly + 8 + uintptr(i*uint32(2))*4 _geopolyAddOneSegment(tls, p, *(*TGeoCoord)(unsafe.Pointer(x)), *(*TGeoCoord)(unsafe.Pointer(x + 1*4)), *(*TGeoCoord)(unsafe.Pointer(x + 2*4)), *(*TGeoCoord)(unsafe.Pointer(x + 3*4)), side, i) goto _1 _1: ; i++ } x = pPoly + 8 + uintptr(i*uint32(2))*4 _geopolyAddOneSegment(tls, p, *(*TGeoCoord)(unsafe.Pointer(x)), *(*TGeoCoord)(unsafe.Pointer(x + 1*4)), *(*TGeoCoord)(unsafe.Pointer(pPoly + 8)), *(*TGeoCoord)(unsafe.Pointer(pPoly + 8 + 1*4)), side, i) } // C documentation // // /* // ** Merge two lists of sorted events by X coordinate // */ func _geopolyEventMerge(tls *libc.TLS, pLeft uintptr, pRight uintptr) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var pLast, v1 uintptr var _ /* head at bp+0 */ TGeoEvent _, _ = pLast, v1 (*(*TGeoEvent)(unsafe.Pointer(bp))).FpNext = uintptr(0) pLast = bp for pRight != 0 && pLeft != 0 { if (*TGeoEvent)(unsafe.Pointer(pRight)).Fx <= (*TGeoEvent)(unsafe.Pointer(pLeft)).Fx { (*TGeoEvent)(unsafe.Pointer(pLast)).FpNext = pRight pLast = pRight pRight = (*TGeoEvent)(unsafe.Pointer(pRight)).FpNext } else { (*TGeoEvent)(unsafe.Pointer(pLast)).FpNext = pLeft pLast = pLeft pLeft = (*TGeoEvent)(unsafe.Pointer(pLeft)).FpNext } } if pRight != 0 { v1 = pRight } else { v1 = pLeft } (*TGeoEvent)(unsafe.Pointer(pLast)).FpNext = v1 return (*(*TGeoEvent)(unsafe.Pointer(bp))).FpNext } // C documentation // // /* // ** Sort an array of nEvent event objects into a list. // */ func _geopolySortEventsByX(tls *libc.TLS, aEvent uintptr, nEvent int32) (r uintptr) { var a [50]uintptr var i, j, mx int32 var p uintptr _, _, _, _, _ = a, i, j, mx, p mx = 0 i = 0 for { if !(i < nEvent) { break } p = aEvent + uintptr(i)*24 (*TGeoEvent)(unsafe.Pointer(p)).FpNext = uintptr(0) j = 0 for { if !(j < mx && a[j] != 0) { break } p = _geopolyEventMerge(tls, a[j], p) a[j] = uintptr(0) goto _2 _2: ; j++ } a[j] = p if j >= mx { mx = j + int32(1) } goto _1 _1: ; i++ } p = uintptr(0) i = 0 for { if !(i < mx) { break } p = _geopolyEventMerge(tls, a[i], p) goto _3 _3: ; i++ } return p } // C documentation // // /* // ** Merge two lists of sorted segments by Y, and then by C. // */ func _geopolySegmentMerge(tls *libc.TLS, pLeft uintptr, pRight uintptr) (r1 uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var pLast, v1 uintptr var r float64 var _ /* head at bp+0 */ TGeoSegment _, _, _ = pLast, r, v1 (*(*TGeoSegment)(unsafe.Pointer(bp))).FpNext = uintptr(0) pLast = bp for pRight != 0 && pLeft != 0 { r = (*TGeoSegment)(unsafe.Pointer(pRight)).Fy - (*TGeoSegment)(unsafe.Pointer(pLeft)).Fy if r == float64(0) { r = (*TGeoSegment)(unsafe.Pointer(pRight)).FC - (*TGeoSegment)(unsafe.Pointer(pLeft)).FC } if r < float64(0) { (*TGeoSegment)(unsafe.Pointer(pLast)).FpNext = pRight pLast = pRight pRight = (*TGeoSegment)(unsafe.Pointer(pRight)).FpNext } else { (*TGeoSegment)(unsafe.Pointer(pLast)).FpNext = pLeft pLast = pLeft pLeft = (*TGeoSegment)(unsafe.Pointer(pLeft)).FpNext } } if pRight != 0 { v1 = pRight } else { v1 = pLeft } (*TGeoSegment)(unsafe.Pointer(pLast)).FpNext = v1 return (*(*TGeoSegment)(unsafe.Pointer(bp))).FpNext } // C documentation // // /* // ** Sort a list of GeoSegments in order of increasing Y and in the event of // ** a tie, increasing C (slope). // */ func _geopolySortSegmentsByYAndC(tls *libc.TLS, pList uintptr) (r uintptr) { var a [50]uintptr var i, mx int32 var p uintptr _, _, _, _ = a, i, mx, p mx = 0 for pList != 0 { p = pList pList = (*TGeoSegment)(unsafe.Pointer(pList)).FpNext (*TGeoSegment)(unsafe.Pointer(p)).FpNext = uintptr(0) i = 0 for { if !(i < mx && a[i] != 0) { break } p = _geopolySegmentMerge(tls, a[i], p) a[i] = uintptr(0) goto _1 _1: ; i++ } a[i] = p if i >= mx { mx = i + int32(1) } } p = uintptr(0) i = 0 for { if !(i < mx) { break } p = _geopolySegmentMerge(tls, a[i], p) goto _2 _2: ; i++ } return p } // C documentation // // /* // ** Determine the overlap between two polygons // */ func _geopolyOverlap(tls *libc.TLS, p1 uintptr, p2 uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iMask, needSort, rc, v1 int32 var nByte, nVertex Tsqlite3_int64 var p, pActive, pPrev, pSeg, pThisEvent, v5, v7 uintptr var rX, y, v2 float64 var _ /* aOverlap at bp+0 */ [4]uint8 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = iMask, nByte, nVertex, needSort, p, pActive, pPrev, pSeg, pThisEvent, rX, rc, y, v1, v2, v5, v7 nVertex = int64((*TGeoPoly)(unsafe.Pointer(p1)).FnVertex + (*TGeoPoly)(unsafe.Pointer(p2)).FnVertex + int32(2)) rc = 0 needSort = 0 pActive = uintptr(0) nByte = int64(24)*nVertex*int64(2) + int64(40)*nVertex + int64(16) p = Xsqlite3_malloc64(tls, uint64(nByte)) if p == uintptr(0) { return -int32(1) } (*TGeoOverlap)(unsafe.Pointer(p)).FaEvent = p + 1*16 (*TGeoOverlap)(unsafe.Pointer(p)).FaSegment = (*TGeoOverlap)(unsafe.Pointer(p)).FaEvent + uintptr(nVertex*int64(2))*24 v1 = libc.Int32FromInt32(0) (*TGeoOverlap)(unsafe.Pointer(p)).FnSegment = v1 (*TGeoOverlap)(unsafe.Pointer(p)).FnEvent = v1 _geopolyAddSegments(tls, p, p1, uint8(1)) _geopolyAddSegments(tls, p, p2, uint8(2)) pThisEvent = _geopolySortEventsByX(tls, (*TGeoOverlap)(unsafe.Pointer(p)).FaEvent, (*TGeoOverlap)(unsafe.Pointer(p)).FnEvent) if pThisEvent != 0 && (*TGeoEvent)(unsafe.Pointer(pThisEvent)).Fx == float64(0) { v2 = -libc.Float64FromFloat64(1) } else { v2 = float64(0) } rX = v2 libc.Xmemset(tls, bp, 0, uint32(4)) for pThisEvent != 0 { if (*TGeoEvent)(unsafe.Pointer(pThisEvent)).Fx != rX { pPrev = uintptr(0) iMask = 0 rX = (*TGeoEvent)(unsafe.Pointer(pThisEvent)).Fx if needSort != 0 { pActive = _geopolySortSegmentsByYAndC(tls, pActive) needSort = 0 } pSeg = pActive for { if !(pSeg != 0) { break } if pPrev != 0 { if (*TGeoSegment)(unsafe.Pointer(pPrev)).Fy != (*TGeoSegment)(unsafe.Pointer(pSeg)).Fy { (*(*[4]uint8)(unsafe.Pointer(bp)))[iMask] = uint8(1) } } iMask ^= int32((*TGeoSegment)(unsafe.Pointer(pSeg)).Fside) pPrev = pSeg goto _3 _3: ; pSeg = (*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext } pPrev = uintptr(0) pSeg = pActive for { if !(pSeg != 0) { break } y = (*TGeoSegment)(unsafe.Pointer(pSeg)).FC*rX + (*TGeoSegment)(unsafe.Pointer(pSeg)).FB (*TGeoSegment)(unsafe.Pointer(pSeg)).Fy = y if pPrev != 0 { if (*TGeoSegment)(unsafe.Pointer(pPrev)).Fy > (*TGeoSegment)(unsafe.Pointer(pSeg)).Fy && int32((*TGeoSegment)(unsafe.Pointer(pPrev)).Fside) != int32((*TGeoSegment)(unsafe.Pointer(pSeg)).Fside) { rc = int32(1) goto geopolyOverlapDone } else { if (*TGeoSegment)(unsafe.Pointer(pPrev)).Fy != (*TGeoSegment)(unsafe.Pointer(pSeg)).Fy { (*(*[4]uint8)(unsafe.Pointer(bp)))[iMask] = uint8(1) } } } iMask ^= int32((*TGeoSegment)(unsafe.Pointer(pSeg)).Fside) pPrev = pSeg goto _4 _4: ; pSeg = (*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext } } if (*TGeoEvent)(unsafe.Pointer(pThisEvent)).FeType == 0 { /* Add a segment */ pSeg = (*TGeoEvent)(unsafe.Pointer(pThisEvent)).FpSeg (*TGeoSegment)(unsafe.Pointer(pSeg)).Fy = float64((*TGeoSegment)(unsafe.Pointer(pSeg)).Fy0) (*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext = pActive pActive = pSeg needSort = int32(1) } else { /* Remove a segment */ if pActive == (*TGeoEvent)(unsafe.Pointer(pThisEvent)).FpSeg { if pActive != 0 { v5 = (*TGeoSegment)(unsafe.Pointer(pActive)).FpNext } else { v5 = uintptr(0) } pActive = v5 } else { pSeg = pActive for { if !(pSeg != 0) { break } if (*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext == (*TGeoEvent)(unsafe.Pointer(pThisEvent)).FpSeg { if (*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext != 0 { v7 = (*TGeoSegment)(unsafe.Pointer((*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext)).FpNext } else { v7 = uintptr(0) } (*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext = v7 break } goto _6 _6: ; pSeg = (*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext } } } pThisEvent = (*TGeoEvent)(unsafe.Pointer(pThisEvent)).FpNext } if int32((*(*[4]uint8)(unsafe.Pointer(bp)))[int32(3)]) == 0 { rc = 0 } else { if int32((*(*[4]uint8)(unsafe.Pointer(bp)))[int32(1)]) != 0 && int32((*(*[4]uint8)(unsafe.Pointer(bp)))[int32(2)]) == 0 { rc = int32(3) } else { if int32((*(*[4]uint8)(unsafe.Pointer(bp)))[int32(1)]) == 0 && int32((*(*[4]uint8)(unsafe.Pointer(bp)))[int32(2)]) != 0 { rc = int32(2) } else { if int32((*(*[4]uint8)(unsafe.Pointer(bp)))[int32(1)]) == 0 && int32((*(*[4]uint8)(unsafe.Pointer(bp)))[int32(2)]) == 0 { rc = int32(4) } else { rc = int32(1) } } } } goto geopolyOverlapDone geopolyOverlapDone: ; Xsqlite3_free(tls, p) return rc } // C documentation // // /* // ** SQL function: geopoly_overlap(P1,P2) // ** // ** Determine whether or not P1 and P2 overlap. Return value: // ** // ** 0 The two polygons are disjoint // ** 1 They overlap // ** 2 P1 is completely contained within P2 // ** 3 P2 is completely contained within P1 // ** 4 P1 and P2 are the same polygon // ** NULL Either P1 or P2 or both are not valid polygons // */ func _geopolyOverlapFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var p1, p2 uintptr var x int32 _, _, _ = p1, p2, x p1 = _geopolyFuncParam(tls, context, *(*uintptr)(unsafe.Pointer(argv)), uintptr(0)) p2 = _geopolyFuncParam(tls, context, *(*uintptr)(unsafe.Pointer(argv + 1*4)), uintptr(0)) _ = argc if p1 != 0 && p2 != 0 { x = _geopolyOverlap(tls, p1, p2) if x < 0 { Xsqlite3_result_error_nomem(tls, context) } else { Xsqlite3_result_int(tls, context, x) } } Xsqlite3_free(tls, p1) Xsqlite3_free(tls, p2) } // C documentation // // /* // ** Enable or disable debugging output // */ func _geopolyDebugFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { _ = context _ = argc _ = argv } // C documentation // // /* // ** This function is the implementation of both the xConnect and xCreate // ** methods of the geopoly virtual table. // ** // ** argv[0] -> module name // ** argv[1] -> database name // ** argv[2] -> table name // ** argv[...] -> column names... // */ func _geopolyInit(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr, isCreate int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var ii, rc, v2 int32 var nDb, nName Tsqlite3_int64 var pRtree, pSql, zSql uintptr _, _, _, _, _, _, _, _ = ii, nDb, nName, pRtree, pSql, rc, zSql, v2 rc = SQLITE_OK _ = pAux Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_CONSTRAINT_SUPPORT), libc.VaList(bp+8, int32(1))) Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_INNOCUOUS), 0) /* Allocate the sqlite3_vtab structure */ nDb = int64(libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4)))) nName = int64(libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(argv + 2*4)))) pRtree = Xsqlite3_malloc64(tls, uint64(int64(504)+nDb+nName*int64(2)+int64(8))) if !(pRtree != 0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, pRtree, 0, uint32(int64(504)+nDb+nName*int64(2)+int64(8))) (*TRtree)(unsafe.Pointer(pRtree)).FnBusy = uint32(1) (*TRtree)(unsafe.Pointer(pRtree)).Fbase.FpModule = uintptr(unsafe.Pointer(&_rtreeModule)) (*TRtree)(unsafe.Pointer(pRtree)).FzDb = pRtree + 1*504 (*TRtree)(unsafe.Pointer(pRtree)).FzName = (*TRtree)(unsafe.Pointer(pRtree)).FzDb + uintptr(nDb+int64(1)) (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName = (*TRtree)(unsafe.Pointer(pRtree)).FzName + uintptr(nName+int64(1)) (*TRtree)(unsafe.Pointer(pRtree)).FeCoordType = uint8(RTREE_COORD_REAL32) (*TRtree)(unsafe.Pointer(pRtree)).FnDim = uint8(2) (*TRtree)(unsafe.Pointer(pRtree)).FnDim2 = uint8(4) libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, *(*uintptr)(unsafe.Pointer(argv + 1*4)), uint32(nDb)) libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzName, *(*uintptr)(unsafe.Pointer(argv + 2*4)), uint32(nName)) libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName, *(*uintptr)(unsafe.Pointer(argv + 2*4)), uint32(nName)) libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName+uintptr(nName), __ccgo_ts+27285, uint32(6)) /* Create/Connect to the underlying relational database schema. If ** that is successful, call sqlite3_declare_vtab() to configure ** the r-tree table schema. */ pSql = Xsqlite3_str_new(tls, db) Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+28245, 0) (*TRtree)(unsafe.Pointer(pRtree)).FnAux = uint8(1) /* Add one for _shape */ (*TRtree)(unsafe.Pointer(pRtree)).FnAuxNotNull = uint8(1) /* The _shape column is always not-null */ ii = int32(3) for { if !(ii < argc) { break } (*TRtree)(unsafe.Pointer(pRtree)).FnAux++ Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+28267, libc.VaList(bp+8, *(*uintptr)(unsafe.Pointer(argv + uintptr(ii)*4)))) goto _1 _1: ; ii++ } Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+27342, 0) zSql = Xsqlite3_str_finish(tls, pSql) if !(zSql != 0) { rc = int32(SQLITE_NOMEM) } else { v2 = Xsqlite3_declare_vtab(tls, db, zSql) rc = v2 if SQLITE_OK != v2 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+8, Xsqlite3_errmsg(tls, db))) } } Xsqlite3_free(tls, zSql) if rc != 0 { goto geopolyInit_fail } (*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell = uint8(int32(8) + int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)*int32(4)) /* Figure out the node size to use. */ rc = _getNodeSize(tls, db, pRtree, isCreate, pzErr) if rc != 0 { goto geopolyInit_fail } rc = _rtreeSqlInit(tls, pRtree, db, *(*uintptr)(unsafe.Pointer(argv + 1*4)), *(*uintptr)(unsafe.Pointer(argv + 2*4)), isCreate) if rc != 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+8, Xsqlite3_errmsg(tls, db))) goto geopolyInit_fail } *(*uintptr)(unsafe.Pointer(ppVtab)) = pRtree return SQLITE_OK goto geopolyInit_fail geopolyInit_fail: ; if rc == SQLITE_OK { rc = int32(SQLITE_ERROR) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _rtreeRelease(tls, pRtree) return rc } // C documentation // // /* // ** GEOPOLY virtual table module xCreate method. // */ func _geopolyCreate(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) { return _geopolyInit(tls, db, pAux, argc, argv, ppVtab, pzErr, int32(1)) } // C documentation // // /* // ** GEOPOLY virtual table module xConnect method. // */ func _geopolyConnect(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) { return _geopolyInit(tls, db, pAux, argc, argv, ppVtab, pzErr, 0) } // C documentation // // /* // ** GEOPOLY virtual table module xFilter method. // ** // ** Query plans: // ** // ** 1 rowid lookup // ** 2 search for objects overlapping the same bounding box // ** that contains polygon argv[0] // ** 3 search for objects overlapping the same bounding box // ** that contains polygon argv[0] // ** 4 full table scan // */ func _geopolyFilter(tls *libc.TLS, pVtabCursor uintptr, idxNum int32, idxStr uintptr, argc int32, argv uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var iRowid Ti64 var p, p1, pCsr, pNew, pRtree, v1 uintptr var _ /* bbox at bp+24 */ [4]TRtreeCoord var _ /* iCell at bp+8 */ int32 var _ /* iNode at bp+16 */ Ti64 var _ /* pLeaf at bp+12 */ uintptr var _ /* pRoot at bp+0 */ uintptr var _ /* rc at bp+4 */ int32 _, _, _, _, _, _, _ = iRowid, p, p1, pCsr, pNew, pRtree, v1 pRtree = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVtabCursor)).FpVtab pCsr = pVtabCursor *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 4)) = SQLITE_OK *(*int32)(unsafe.Pointer(bp + 8)) = 0 _ = idxStr _rtreeReference(tls, pRtree) /* Reset the cursor to the same state as rtreeOpen() leaves it in. */ _resetCursor(tls, pCsr) (*TRtreeCursor)(unsafe.Pointer(pCsr)).FiStrategy = idxNum if idxNum == int32(1) { /* Search point for the leaf */ iRowid = Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv))) *(*Ti64)(unsafe.Pointer(bp + 16)) = 0 *(*int32)(unsafe.Pointer(bp + 4)) = _findLeafNode(tls, pRtree, iRowid, bp+12, bp+16) if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK && *(*uintptr)(unsafe.Pointer(bp + 12)) != uintptr(0) { p = _rtreeSearchPointNew(tls, pCsr, float64(0), uint8(0)) _ = libc.Int32FromInt32(0) /* Always returns pCsr->sPoint */ *(*uintptr)(unsafe.Pointer(pCsr + 64)) = *(*uintptr)(unsafe.Pointer(bp + 12)) (*TRtreeSearchPoint)(unsafe.Pointer(p)).Fid = *(*Ti64)(unsafe.Pointer(bp + 16)) (*TRtreeSearchPoint)(unsafe.Pointer(p)).FeWithin = uint8(PARTLY_WITHIN) *(*int32)(unsafe.Pointer(bp + 4)) = _nodeRowidIndex(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 12)), iRowid, bp+8) (*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell = uint8(*(*int32)(unsafe.Pointer(bp + 8))) } else { (*TRtreeCursor)(unsafe.Pointer(pCsr)).FatEOF = uint8(1) } } else { /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array ** with the configured constraints. */ *(*int32)(unsafe.Pointer(bp + 4)) = _nodeAcquire(tls, pRtree, int64(1), uintptr(0), bp) if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK && idxNum <= int32(3) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _geopolyBBox(tls, uintptr(0), *(*uintptr)(unsafe.Pointer(argv)), bp+24, bp+4) if *(*int32)(unsafe.Pointer(bp + 4)) != 0 { goto geopoly_filter_end } v1 = Xsqlite3_malloc(tls, int32(libc.Uint32FromInt64(24)*libc.Uint32FromInt32(4))) p1 = v1 (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint = v1 (*TRtreeCursor)(unsafe.Pointer(pCsr)).FnConstraint = int32(4) if p1 == uintptr(0) { *(*int32)(unsafe.Pointer(bp + 4)) = int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint, 0, libc.Uint32FromInt64(24)*libc.Uint32FromInt32(4)) libc.Xmemset(tls, pCsr+84, 0, uint32(4)*uint32((*TRtree)(unsafe.Pointer(pRtree)).FiDepth+libc.Int32FromInt32(1))) if idxNum == int32(2) { /* Overlap query */ (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('B') (*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = 0 *(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24 + 1*4))) p1 += 24 (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('D') (*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(1) *(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24))) p1 += 24 (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('B') (*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(2) *(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24 + 3*4))) p1 += 24 (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('D') (*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(3) *(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24 + 2*4))) } else { /* Within query */ (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('D') (*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = 0 *(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24))) p1 += 24 (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('B') (*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(1) *(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24 + 1*4))) p1 += 24 (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('D') (*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(2) *(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24 + 2*4))) p1 += 24 (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('B') (*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(3) *(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24 + 3*4))) } } } if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK { pNew = _rtreeSearchPointNew(tls, pCsr, float64(0), uint8((*TRtree)(unsafe.Pointer(pRtree)).FiDepth+libc.Int32FromInt32(1))) if pNew == uintptr(0) { *(*int32)(unsafe.Pointer(bp + 4)) = int32(SQLITE_NOMEM) goto geopoly_filter_end } (*TRtreeSearchPoint)(unsafe.Pointer(pNew)).Fid = int64(1) (*TRtreeSearchPoint)(unsafe.Pointer(pNew)).FiCell = uint8(0) (*TRtreeSearchPoint)(unsafe.Pointer(pNew)).FeWithin = uint8(PARTLY_WITHIN) _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pCsr + 64)) = *(*uintptr)(unsafe.Pointer(bp)) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 4)) = _rtreeStepToLeaf(tls, pCsr) } } goto geopoly_filter_end geopoly_filter_end: ; _nodeRelease(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp))) _rtreeRelease(tls, pRtree) return *(*int32)(unsafe.Pointer(bp + 4)) } // C documentation // // /* // ** Rtree virtual table module xBestIndex method. There are three // ** table scan strategies to choose from (in order from most to // ** least desirable): // ** // ** idxNum idxStr Strategy // ** ------------------------------------------------ // ** 1 "rowid" Direct lookup by rowid. // ** 2 "rtree" R-tree overlap query using geopoly_overlap() // ** 3 "rtree" R-tree within query using geopoly_within() // ** 4 "fullscan" full-table scan. // ** ------------------------------------------------ // */ func _geopolyBestIndex(tls *libc.TLS, tab uintptr, pIdxInfo uintptr) (r int32) { var iFuncTerm, iRowidTerm, idxNum, ii int32 var p uintptr _, _, _, _, _ = iFuncTerm, iRowidTerm, idxNum, ii, p iRowidTerm = -int32(1) iFuncTerm = -int32(1) idxNum = 0 _ = tab ii = 0 for { if !(ii < (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint) { break } p = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(ii)*12 if !((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fusable != 0) { goto _1 } if (*Tsqlite3_index_constraint)(unsafe.Pointer(p)).FiColumn < 0 && int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INDEX_CONSTRAINT_EQ) { iRowidTerm = ii break } if (*Tsqlite3_index_constraint)(unsafe.Pointer(p)).FiColumn == 0 && int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) >= int32(SQLITE_INDEX_CONSTRAINT_FUNCTION) { /* p->op==SQLITE_INDEX_CONSTRAINT_FUNCTION for geopoly_overlap() ** p->op==(SQLITE_INDEX_CONTRAINT_FUNCTION+1) for geopoly_within(). ** See geopolyFindFunction() */ iFuncTerm = ii idxNum = int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) - int32(SQLITE_INDEX_CONSTRAINT_FUNCTION) + int32(2) } goto _1 _1: ; ii++ } if iRowidTerm >= 0 { (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxNum = int32(1) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxStr = __ccgo_ts + 16694 (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(iRowidTerm)*8))).FargvIndex = int32(1) (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(iRowidTerm)*8))).Fomit = uint8(1) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = float64(30) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = int64(1) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxFlags = int32(SQLITE_INDEX_SCAN_UNIQUE) return SQLITE_OK } if iFuncTerm >= 0 { (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxNum = idxNum (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxStr = __ccgo_ts + 28271 (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(iFuncTerm)*8))).FargvIndex = int32(1) (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(iFuncTerm)*8))).Fomit = uint8(0) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = float64(300) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = int64(10) return SQLITE_OK } (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxNum = int32(4) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxStr = __ccgo_ts + 28277 (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = float64(3e+06) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = int64(100000) return SQLITE_OK } // C documentation // // /* // ** GEOPOLY virtual table module xColumn method. // */ func _geopolyColumn(tls *libc.TLS, cur uintptr, ctx uintptr, i int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var p, pCsr, pNode, pRtree uintptr var _ /* rc at bp+0 */ int32 _, _, _, _ = p, pCsr, pNode, pRtree pRtree = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab pCsr = cur p = _rtreeSearchPointFirst(tls, pCsr) *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK pNode = _rtreeNodeOfFirstSearchPoint(tls, pCsr, bp) if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } if p == uintptr(0) { return SQLITE_OK } if i == 0 && Xsqlite3_vtab_nochange(tls, ctx) != 0 { return SQLITE_OK } if i <= int32((*TRtree)(unsafe.Pointer(pRtree)).FnAux) { if !((*TRtreeCursor)(unsafe.Pointer(pCsr)).FbAuxValid != 0) { if (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_prepare_v3(tls, (*TRtree)(unsafe.Pointer(pRtree)).Fdb, (*TRtree)(unsafe.Pointer(pRtree)).FzReadAuxSql, -int32(1), uint32(0), pCsr+36, uintptr(0)) if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } } Xsqlite3_bind_int64(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux, int32(1), _nodeGetRowid(tls, pRtree, pNode, int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell))) *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_step(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux) if *(*int32)(unsafe.Pointer(bp)) == int32(SQLITE_ROW) { (*TRtreeCursor)(unsafe.Pointer(pCsr)).FbAuxValid = uint8(1) } else { Xsqlite3_reset(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux) if *(*int32)(unsafe.Pointer(bp)) == int32(SQLITE_DONE) { *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK } return *(*int32)(unsafe.Pointer(bp)) } } Xsqlite3_result_value(tls, ctx, Xsqlite3_column_value(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux, i+int32(2))) } return SQLITE_OK } // C documentation // // /* // ** The xUpdate method for GEOPOLY module virtual tables. // ** // ** For DELETE: // ** // ** argv[0] = the rowid to be deleted // ** // ** For INSERT: // ** // ** argv[0] = SQL NULL // ** argv[1] = rowid to insert, or an SQL NULL to select automatically // ** argv[2] = _shape column // ** argv[3] = first application-defined column.... // ** // ** For UPDATE: // ** // ** argv[0] = rowid to modify. Never NULL // ** argv[1] = rowid after the change. Never NULL // ** argv[2] = new value for _shape // ** argv[3] = new value for first application-defined column.... // */ func _geopolyUpdate(tls *libc.TLS, pVtab uintptr, nData int32, aData uintptr, pRowid uintptr) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var coordChange, jj, nChange, newRowidValid, oldRowidValid, rc2, steprc int32 var newRowid, oldRowid Ti64 var p, pRtree, pUp, v3 uintptr var v1, v2 int64 var v4 bool var _ /* cell at bp+8 */ TRtreeCell var _ /* pLeaf at bp+56 */ uintptr var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = coordChange, jj, nChange, newRowid, newRowidValid, oldRowid, oldRowidValid, p, pRtree, pUp, rc2, steprc, v1, v2, v3, v4 pRtree = pVtab *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK /* True if newRowid is valid */ coordChange = 0 /* Change in coordinates */ if (*TRtree)(unsafe.Pointer(pRtree)).FnNodeRef != 0 { /* Unable to write to the btree while another cursor is reading from it, ** since the write might do a rebalance which would disrupt the read ** cursor. */ return libc.Int32FromInt32(SQLITE_LOCKED) | libc.Int32FromInt32(2)< int32(1) && Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(aData + 1*4))) != int32(SQLITE_NULL)) if newRowidValid != 0 { v2 = Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(aData + 1*4))) } else { v2 = 0 } newRowid = v2 (*(*TRtreeCell)(unsafe.Pointer(bp + 8))).FiRowid = newRowid if nData > int32(1) && (!(oldRowidValid != 0) || !(Xsqlite3_value_nochange(tls, *(*uintptr)(unsafe.Pointer(aData + 2*4))) != 0) || oldRowid != newRowid) { _ = libc.Int32FromInt32(0) _geopolyBBox(tls, uintptr(0), *(*uintptr)(unsafe.Pointer(aData + 2*4)), bp+8+8, bp) if *(*int32)(unsafe.Pointer(bp)) != 0 { if *(*int32)(unsafe.Pointer(bp)) == int32(SQLITE_ERROR) { (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+28286, 0) } goto geopoly_update_end } coordChange = int32(1) /* If a rowid value was supplied, check if it is already present in ** the table. If so, the constraint has failed. */ if newRowidValid != 0 && (!(oldRowidValid != 0) || oldRowid != newRowid) { Xsqlite3_bind_int64(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid, int32(1), (*(*TRtreeCell)(unsafe.Pointer(bp + 8))).FiRowid) steprc = Xsqlite3_step(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid) *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_reset(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid) if int32(SQLITE_ROW) == steprc { if Xsqlite3_vtab_on_conflict(tls, (*TRtree)(unsafe.Pointer(pRtree)).Fdb) == int32(SQLITE_REPLACE) { *(*int32)(unsafe.Pointer(bp)) = _rtreeDeleteRowid(tls, pRtree, (*(*TRtreeCell)(unsafe.Pointer(bp + 8))).FiRowid) } else { *(*int32)(unsafe.Pointer(bp)) = _rtreeConstraintError(tls, pRtree, 0) } } } } /* If aData[0] is not an SQL NULL value, it is the rowid of a ** record to delete from the r-tree table. The following block does ** just that. */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && (nData == int32(1) || coordChange != 0 && oldRowidValid != 0) { *(*int32)(unsafe.Pointer(bp)) = _rtreeDeleteRowid(tls, pRtree, oldRowid) } /* If the aData[] array contains more than one element, elements ** (aData[2]..aData[argc-1]) contain a new record to insert into ** the r-tree structure. */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && nData > int32(1) && coordChange != 0 { /* Insert the new record into the r-tree */ *(*uintptr)(unsafe.Pointer(bp + 56)) = uintptr(0) if !(newRowidValid != 0) { *(*int32)(unsafe.Pointer(bp)) = _rtreeNewRowid(tls, pRtree, bp+8) } *(*Tsqlite_int64)(unsafe.Pointer(pRowid)) = (*(*TRtreeCell)(unsafe.Pointer(bp + 8))).FiRowid if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _ChooseLeaf(tls, pRtree, bp+8, 0, bp+56) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _rtreeInsertCell(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 56)), bp+8, 0) rc2 = _nodeRelease(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 56))) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = rc2 } } } /* Change the data */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && nData > int32(1) { pUp = (*TRtree)(unsafe.Pointer(pRtree)).FpWriteAux nChange = 0 Xsqlite3_bind_int64(tls, pUp, int32(1), (*(*TRtreeCell)(unsafe.Pointer(bp + 8))).FiRowid) _ = libc.Int32FromInt32(0) if Xsqlite3_value_nochange(tls, *(*uintptr)(unsafe.Pointer(aData + 2*4))) != 0 { Xsqlite3_bind_null(tls, pUp, int32(2)) } else { p = uintptr(0) if v4 = Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(aData + 2*4))) == int32(SQLITE_TEXT); v4 { v3 = _geopolyFuncParam(tls, uintptr(0), *(*uintptr)(unsafe.Pointer(aData + 2*4)), bp) p = v3 } if v4 && v3 != uintptr(0) && *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { Xsqlite3_bind_blob(tls, pUp, int32(2), p+4, int32(4)+int32(8)*(*TGeoPoly)(unsafe.Pointer(p)).FnVertex, uintptr(-libc.Int32FromInt32(1))) } else { Xsqlite3_bind_value(tls, pUp, int32(2), *(*uintptr)(unsafe.Pointer(aData + 2*4))) } Xsqlite3_free(tls, p) nChange = int32(1) } jj = int32(1) for { if !(jj < nData-int32(2)) { break } nChange++ Xsqlite3_bind_value(tls, pUp, jj+int32(2), *(*uintptr)(unsafe.Pointer(aData + uintptr(jj+int32(2))*4))) goto _5 _5: ; jj++ } if nChange != 0 { Xsqlite3_step(tls, pUp) *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_reset(tls, pUp) } } goto geopoly_update_end geopoly_update_end: ; _rtreeRelease(tls, pRtree) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Report that geopoly_overlap() is an overloaded function suitable // ** for use in xBestIndex. // */ func _geopolyFindFunction(tls *libc.TLS, pVtab uintptr, nArg int32, zName uintptr, pxFunc uintptr, ppArg uintptr) (r int32) { _ = pVtab _ = nArg if Xsqlite3_stricmp(tls, zName, __ccgo_ts+28326) == 0 { *(*uintptr)(unsafe.Pointer(pxFunc)) = __ccgo_fp(_geopolyOverlapFunc) *(*uintptr)(unsafe.Pointer(ppArg)) = uintptr(0) return int32(SQLITE_INDEX_CONSTRAINT_FUNCTION) } if Xsqlite3_stricmp(tls, zName, __ccgo_ts+28342) == 0 { *(*uintptr)(unsafe.Pointer(pxFunc)) = __ccgo_fp(_geopolyWithinFunc) *(*uintptr)(unsafe.Pointer(ppArg)) = uintptr(0) return libc.Int32FromInt32(SQLITE_INDEX_CONSTRAINT_FUNCTION) + libc.Int32FromInt32(1) } return 0 } var _geopolyModule = Tsqlite3_module{ FiVersion: int32(3), } func init() { p := unsafe.Pointer(&_geopolyModule) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_geopolyCreate) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_geopolyConnect) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_geopolyBestIndex) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_rtreeDisconnect) *(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_rtreeDestroy) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_rtreeOpen) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_rtreeClose) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_geopolyFilter) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_rtreeNext) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_rtreeEof) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_geopolyColumn) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_rtreeRowid) *(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(_geopolyUpdate) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_rtreeBeginTransaction) *(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_rtreeEndTransaction) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_rtreeEndTransaction) *(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_rtreeEndTransaction) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_geopolyFindFunction) *(*uintptr)(unsafe.Add(p, 76)) = __ccgo_fp(_rtreeRename) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_rtreeSavepoint) *(*uintptr)(unsafe.Add(p, 92)) = __ccgo_fp(_rtreeShadowName) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_rtreeIntegrity) } func _sqlite3_geopoly_init(tls *libc.TLS, db uintptr) (r int32) { var enc, rc int32 var i uint32 _, _, _ = enc, i, rc rc = SQLITE_OK i = uint32(0) for { if !(i < libc.Uint32FromInt64(144)/libc.Uint32FromInt64(12) && rc == SQLITE_OK) { break } if _aFunc[i].FbPure != 0 { enc = libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_INNOCUOUS) } else { enc = libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DIRECTONLY) } rc = Xsqlite3_create_function(tls, db, _aFunc[i].FzName, int32(_aFunc[i].FnArg), enc, uintptr(0), _aFunc[i].FxFunc, uintptr(0), uintptr(0)) goto _1 _1: ; i++ } i = uint32(0) for { if !(i < libc.Uint32FromInt64(12)/libc.Uint32FromInt64(12) && rc == SQLITE_OK) { break } rc = Xsqlite3_create_function(tls, db, _aAgg[i].FzName, int32(1), libc.Int32FromInt32(SQLITE_UTF8)|libc.Int32FromInt32(SQLITE_DETERMINISTIC)|libc.Int32FromInt32(SQLITE_INNOCUOUS), uintptr(0), uintptr(0), _aAgg[i].FxStep, _aAgg[i].FxFinal) goto _2 _2: ; i++ } if rc == SQLITE_OK { rc = Xsqlite3_create_module_v2(tls, db, __ccgo_ts+28519, uintptr(unsafe.Pointer(&_geopolyModule)), uintptr(0), uintptr(0)) } return rc } var _aFunc = [12]struct { FxFunc uintptr FnArg int8 FbPure uint8 FzName uintptr }{ 0: { FnArg: int8(1), FbPure: uint8(1), FzName: __ccgo_ts + 28357, }, 1: { FnArg: int8(1), FbPure: uint8(1), FzName: __ccgo_ts + 28370, }, 2: { FnArg: int8(1), FbPure: uint8(1), FzName: __ccgo_ts + 28383, }, 3: { FnArg: int8(-int32(1)), FbPure: uint8(1), FzName: __ccgo_ts + 28396, }, 4: { FnArg: int8(2), FbPure: uint8(1), FzName: __ccgo_ts + 28342, }, 5: { FnArg: int8(3), FbPure: uint8(1), FzName: __ccgo_ts + 28408, }, 6: { FnArg: int8(2), FbPure: uint8(1), FzName: __ccgo_ts + 28326, }, 7: { FnArg: int8(1), FzName: __ccgo_ts + 28431, }, 8: { FnArg: int8(1), FbPure: uint8(1), FzName: __ccgo_ts + 28445, }, 9: { FnArg: int8(7), FbPure: uint8(1), FzName: __ccgo_ts + 28458, }, 10: { FnArg: int8(4), FbPure: uint8(1), FzName: __ccgo_ts + 28472, }, 11: { FnArg: int8(1), FbPure: uint8(1), FzName: __ccgo_ts + 28488, }, } func init() { p := unsafe.Pointer(&_aFunc) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_geopolyAreaFunc) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_geopolyBlobFunc) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_geopolyJsonFunc) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_geopolySvgFunc) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_geopolyWithinFunc) *(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_geopolyContainsPointFunc) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_geopolyOverlapFunc) *(*uintptr)(unsafe.Add(p, 84)) = __ccgo_fp(_geopolyDebugFunc) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_geopolyBBoxFunc) *(*uintptr)(unsafe.Add(p, 108)) = __ccgo_fp(_geopolyXformFunc) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_geopolyRegularFunc) *(*uintptr)(unsafe.Add(p, 132)) = __ccgo_fp(_geopolyCcwFunc) } var _aAgg = [1]struct { FxStep uintptr FxFinal uintptr FzName uintptr }{ 0: { FzName: __ccgo_ts + 28500, }, } func init() { p := unsafe.Pointer(&_aAgg) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_geopolyBBoxStep) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_geopolyBBoxFinal) } /************** End of geopoly.c *********************************************/ /************** Continuing where we left off in rtree.c **********************/ // C documentation // // /* // ** Register the r-tree module with database handle db. This creates the // ** virtual table module "rtree" and the debugging/analysis scalar // ** function "rtreenode". // */ func _sqlite3RtreeInit(tls *libc.TLS, db uintptr) (r int32) { var c, c1 uintptr var rc, utf8 int32 _, _, _, _ = c, c1, rc, utf8 utf8 = int32(SQLITE_UTF8) rc = Xsqlite3_create_function(tls, db, __ccgo_ts+28527, int32(2), utf8, uintptr(0), __ccgo_fp(_rtreenode), uintptr(0), uintptr(0)) if rc == SQLITE_OK { rc = Xsqlite3_create_function(tls, db, __ccgo_ts+28537, int32(1), utf8, uintptr(0), __ccgo_fp(_rtreedepth), uintptr(0), uintptr(0)) } if rc == SQLITE_OK { rc = Xsqlite3_create_function(tls, db, __ccgo_ts+28548, -int32(1), utf8, uintptr(0), __ccgo_fp(_rtreecheck), uintptr(0), uintptr(0)) } if rc == SQLITE_OK { c = libc.UintptrFromInt32(RTREE_COORD_REAL32) rc = Xsqlite3_create_module_v2(tls, db, __ccgo_ts+28271, uintptr(unsafe.Pointer(&_rtreeModule)), c, uintptr(0)) } if rc == SQLITE_OK { c1 = libc.UintptrFromInt32(RTREE_COORD_INT32) rc = Xsqlite3_create_module_v2(tls, db, __ccgo_ts+28559, uintptr(unsafe.Pointer(&_rtreeModule)), c1, uintptr(0)) } if rc == SQLITE_OK { rc = _sqlite3_geopoly_init(tls, db) } return rc } // C documentation // // /* // ** This routine deletes the RtreeGeomCallback object that was attached // ** one of the SQL functions create by sqlite3_rtree_geometry_callback() // ** or sqlite3_rtree_query_callback(). In other words, this routine is the // ** destructor for an RtreeGeomCallback objecct. This routine is called when // ** the corresponding SQL function is deleted. // */ func _rtreeFreeCallback(tls *libc.TLS, p uintptr) { var pInfo uintptr _ = pInfo pInfo = p if (*TRtreeGeomCallback)(unsafe.Pointer(pInfo)).FxDestructor != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TRtreeGeomCallback)(unsafe.Pointer(pInfo)).FxDestructor})))(tls, (*TRtreeGeomCallback)(unsafe.Pointer(pInfo)).FpContext) } Xsqlite3_free(tls, p) } // C documentation // // /* // ** This routine frees the BLOB that is returned by geomCallback(). // */ func _rtreeMatchArgFree(tls *libc.TLS, pArg uintptr) { var i int32 var p uintptr _, _ = i, p p = pArg i = 0 for { if !(i < (*TRtreeMatchArg)(unsafe.Pointer(p)).FnParam) { break } Xsqlite3_value_free(tls, *(*uintptr)(unsafe.Pointer((*TRtreeMatchArg)(unsafe.Pointer(p)).FapSqlParam + uintptr(i)*4))) goto _1 _1: ; i++ } Xsqlite3_free(tls, p) } // C documentation // // /* // ** Each call to sqlite3_rtree_geometry_callback() or // ** sqlite3_rtree_query_callback() creates an ordinary SQLite // ** scalar function that is implemented by this routine. // ** // ** All this function does is construct an RtreeMatchArg object that // ** contains the geometry-checking callback routines and a list of // ** parameters to this function, then return that RtreeMatchArg object // ** as a BLOB. // ** // ** The R-Tree MATCH operator will read the returned BLOB, deserialize // ** the RtreeMatchArg object, and use the RtreeMatchArg object to figure // ** out which elements of the R-Tree should be returned by the query. // */ func _geomCallback(tls *libc.TLS, ctx uintptr, nArg int32, aArg uintptr) { var i, memErr int32 var nBlob Tsqlite3_int64 var pBlob, pGeomCtx uintptr _, _, _, _, _ = i, memErr, nBlob, pBlob, pGeomCtx pGeomCtx = Xsqlite3_user_data(tls, ctx) memErr = 0 nBlob = int64(uint32(40) + uint32(nArg-libc.Int32FromInt32(1))*uint32(8) + uint32(nArg)*uint32(4)) pBlob = Xsqlite3_malloc64(tls, uint64(nBlob)) if !(pBlob != 0) { Xsqlite3_result_error_nomem(tls, ctx) } else { (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FiSize = uint32(nBlob) (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).Fcb = *(*TRtreeGeomCallback)(unsafe.Pointer(pGeomCtx)) (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FapSqlParam = pBlob + 32 + uintptr(nArg)*8 (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FnParam = nArg i = 0 for { if !(i < nArg) { break } *(*uintptr)(unsafe.Pointer((*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FapSqlParam + uintptr(i)*4)) = Xsqlite3_value_dup(tls, *(*uintptr)(unsafe.Pointer(aArg + uintptr(i)*4))) if *(*uintptr)(unsafe.Pointer((*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FapSqlParam + uintptr(i)*4)) == uintptr(0) { memErr = int32(1) } *(*TRtreeDValue)(unsafe.Pointer(pBlob + 32 + uintptr(i)*8)) = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(aArg + uintptr(i)*4))) goto _1 _1: ; i++ } if memErr != 0 { Xsqlite3_result_error_nomem(tls, ctx) _rtreeMatchArgFree(tls, pBlob) } else { Xsqlite3_result_pointer(tls, ctx, pBlob, __ccgo_ts+25828, __ccgo_fp(_rtreeMatchArgFree)) } } } // C documentation // // /* // ** Register a new geometry function for use with the r-tree MATCH operator. // */ func Xsqlite3_rtree_geometry_callback(tls *libc.TLS, db uintptr, zGeom uintptr, xGeom uintptr, pContext uintptr) (r int32) { var pGeomCtx uintptr _ = pGeomCtx /* Context object for new user-function */ /* Allocate and populate the context object. */ pGeomCtx = Xsqlite3_malloc(tls, int32(16)) if !(pGeomCtx != 0) { return int32(SQLITE_NOMEM) } (*TRtreeGeomCallback)(unsafe.Pointer(pGeomCtx)).FxGeom = xGeom (*TRtreeGeomCallback)(unsafe.Pointer(pGeomCtx)).FxQueryFunc = uintptr(0) (*TRtreeGeomCallback)(unsafe.Pointer(pGeomCtx)).FxDestructor = uintptr(0) (*TRtreeGeomCallback)(unsafe.Pointer(pGeomCtx)).FpContext = pContext return Xsqlite3_create_function_v2(tls, db, zGeom, -int32(1), int32(SQLITE_ANY), pGeomCtx, __ccgo_fp(_geomCallback), uintptr(0), uintptr(0), __ccgo_fp(_rtreeFreeCallback)) } // C documentation // // /* // ** Register a new 2nd-generation geometry function for use with the // ** r-tree MATCH operator. // */ func Xsqlite3_rtree_query_callback(tls *libc.TLS, db uintptr, zQueryFunc uintptr, xQueryFunc uintptr, pContext uintptr, xDestructor uintptr) (r int32) { var pGeomCtx uintptr _ = pGeomCtx /* Context object for new user-function */ /* Allocate and populate the context object. */ pGeomCtx = Xsqlite3_malloc(tls, int32(16)) if !(pGeomCtx != 0) { if xDestructor != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{xDestructor})))(tls, pContext) } return int32(SQLITE_NOMEM) } (*TRtreeGeomCallback)(unsafe.Pointer(pGeomCtx)).FxGeom = uintptr(0) (*TRtreeGeomCallback)(unsafe.Pointer(pGeomCtx)).FxQueryFunc = xQueryFunc (*TRtreeGeomCallback)(unsafe.Pointer(pGeomCtx)).FxDestructor = xDestructor (*TRtreeGeomCallback)(unsafe.Pointer(pGeomCtx)).FpContext = pContext return Xsqlite3_create_function_v2(tls, db, zQueryFunc, -int32(1), int32(SQLITE_ANY), pGeomCtx, __ccgo_fp(_geomCallback), uintptr(0), uintptr(0), __ccgo_fp(_rtreeFreeCallback)) } /************** End of rtree.c ***********************************************/ /************** Begin file icu.c *********************************************/ /* ** 2007 May 6 ** ** 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. ** ************************************************************************* ** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $ ** ** This file implements an integration between the ICU library ** ("International Components for Unicode", an open-source library ** for handling unicode data) and SQLite. The integration uses ** ICU to provide the following to SQLite: ** ** * An implementation of the SQL regexp() function (and hence REGEXP ** operator) using the ICU uregex_XX() APIs. ** ** * Implementations of the SQL scalar upper() and lower() functions ** for case mapping. ** ** * Integration of ICU and SQLite collation sequences. ** ** * An implementation of the LIKE operator that uses ICU to ** provide case-independent matching. */ /************** End of icu.c *************************************************/ /************** Begin file fts3_icu.c ****************************************/ /* ** 2007 June 22 ** ** 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 implements a tokenizer for fts3 based on the ICU library. */ /* #include "fts3Int.h" */ /************** End of fts3_icu.c ********************************************/ /************** Begin file sqlite3rbu.c **************************************/ /* ** 2014 August 30 ** ** 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. ** ************************************************************************* ** ** ** OVERVIEW ** ** The RBU extension requires that the RBU update be packaged as an ** SQLite database. The tables it expects to find are described in ** sqlite3rbu.h. Essentially, for each table xyz in the target database ** that the user wishes to write to, a corresponding data_xyz table is ** created in the RBU database and populated with one row for each row to ** update, insert or delete from the target table. ** ** The update proceeds in three stages: ** ** 1) The database is updated. The modified database pages are written ** to a *-oal file. A *-oal file is just like a *-wal file, except ** that it is named "-oal" instead of "-wal". ** Because regular SQLite clients do not look for file named ** "-oal", they go on using the original database in ** rollback mode while the *-oal file is being generated. ** ** During this stage RBU does not update the database by writing ** directly to the target tables. Instead it creates "imposter" ** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses ** to update each b-tree individually. All updates required by each ** b-tree are completed before moving on to the next, and all ** updates are done in sorted key order. ** ** 2) The "-oal" file is moved to the equivalent "-wal" ** location using a call to rename(2). Before doing this the RBU ** module takes an EXCLUSIVE lock on the database file, ensuring ** that there are no other active readers. ** ** Once the EXCLUSIVE lock is released, any other database readers ** detect the new *-wal file and read the database in wal mode. At ** this point they see the new version of the database - including ** the updates made as part of the RBU update. ** ** 3) The new *-wal file is checkpointed. This proceeds in the same way ** as a regular database checkpoint, except that a single frame is ** checkpointed each time sqlite3rbu_step() is called. If the RBU ** handle is closed before the entire *-wal file is checkpointed, ** the checkpoint progress is saved in the RBU database and the ** checkpoint can be resumed by another RBU client at some point in ** the future. ** ** POTENTIAL PROBLEMS ** ** The rename() call might not be portable. And RBU is not currently ** syncing the directory after renaming the file. ** ** When state is saved, any commit to the *-oal file and the commit to ** the RBU update database are not atomic. So if the power fails at the ** wrong moment they might get out of sync. As the main database will be ** committed before the RBU update database this will likely either just ** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE ** constraint violations). ** ** If some client does modify the target database mid RBU update, or some ** other error occurs, the RBU extension will keep throwing errors. It's ** not really clear how to get out of this state. The system could just ** by delete the RBU update database and *-oal file and have the device ** download the update again and start over. ** ** At present, for an UPDATE, both the new.* and old.* records are ** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all ** fields are collected. This means we're probably writing a lot more ** data to disk when saving the state of an ongoing update to the RBU ** update database than is strictly necessary. ** */ /* #include */ /* #include */ /* #include */ /* #include "sqlite3.h" */ /************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/ /************** Begin file sqlite3rbu.h **************************************/ /* ** 2014 August 30 ** ** 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 contains the public interface for the RBU extension. */ /* ** SUMMARY ** ** Writing a transaction containing a large number of operations on ** b-tree indexes that are collectively larger than the available cache ** memory can be very inefficient. ** ** The problem is that in order to update a b-tree, the leaf page (at least) ** containing the entry being inserted or deleted must be modified. If the ** working set of leaves is larger than the available cache memory, then a ** single leaf that is modified more than once as part of the transaction ** may be loaded from or written to the persistent media multiple times. ** Additionally, because the index updates are likely to be applied in ** random order, access to pages within the database is also likely to be in ** random order, which is itself quite inefficient. ** ** One way to improve the situation is to sort the operations on each index ** by index key before applying them to the b-tree. This leads to an IO ** pattern that resembles a single linear scan through the index b-tree, ** and all but guarantees each modified leaf page is loaded and stored ** exactly once. SQLite uses this trick to improve the performance of ** CREATE INDEX commands. This extension allows it to be used to improve ** the performance of large transactions on existing databases. ** ** Additionally, this extension allows the work involved in writing the ** large transaction to be broken down into sub-transactions performed ** sequentially by separate processes. This is useful if the system cannot ** guarantee that a single update process will run for long enough to apply ** the entire update, for example because the update is being applied on a ** mobile device that is frequently rebooted. Even after the writer process ** has committed one or more sub-transactions, other database clients continue ** to read from the original database snapshot. In other words, partially ** applied transactions are not visible to other clients. ** ** "RBU" stands for "Resumable Bulk Update". As in a large database update ** transmitted via a wireless network to a mobile device. A transaction ** applied using this extension is hence refered to as an "RBU update". ** ** ** LIMITATIONS ** ** An "RBU update" transaction is subject to the following limitations: ** ** * The transaction must consist of INSERT, UPDATE and DELETE operations ** only. ** ** * INSERT statements may not use any default values. ** ** * UPDATE and DELETE statements must identify their target rows by ** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY ** KEY fields may not be updated or deleted. If the table being written ** has no PRIMARY KEY, affected rows must be identified by rowid. ** ** * UPDATE statements may not modify PRIMARY KEY columns. ** ** * No triggers will be fired. ** ** * No foreign key violations are detected or reported. ** ** * CHECK constraints are not enforced. ** ** * No constraint handling mode except for "OR ROLLBACK" is supported. ** ** ** PREPARATION ** ** An "RBU update" is stored as a separate SQLite database. A database ** containing an RBU update is an "RBU database". For each table in the ** target database to be updated, the RBU database should contain a table ** named "data_" containing the same set of columns as the ** target table, and one more - "rbu_control". The data_% table should ** have no PRIMARY KEY or UNIQUE constraints, but each column should have ** the same type as the corresponding column in the target database. ** The "rbu_control" column should have no type at all. For example, if ** the target database contains: ** ** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE); ** ** Then the RBU database should contain: ** ** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control); ** ** The order of the columns in the data_% table does not matter. ** ** Instead of a regular table, the RBU database may also contain virtual ** tables or views named using the data_ naming scheme. ** ** Instead of the plain data_ naming scheme, RBU database tables ** may also be named data_, where is any sequence ** of zero or more numeric characters (0-9). This can be significant because ** tables within the RBU database are always processed in order sorted by ** name. By judicious selection of the portion of the names ** of the RBU tables the user can therefore control the order in which they ** are processed. This can be useful, for example, to ensure that "external ** content" FTS4 tables are updated before their underlying content tables. ** ** If the target database table is a virtual table or a table that has no ** PRIMARY KEY declaration, the data_% table must also contain a column ** named "rbu_rowid". This column is mapped to the table's implicit primary ** key column - "rowid". Virtual tables for which the "rowid" column does ** not function like a primary key value cannot be updated using RBU. For ** example, if the target db contains either of the following: ** ** CREATE VIRTUAL TABLE x1 USING fts3(a, b); ** CREATE TABLE x1(a, b) ** ** then the RBU database should contain: ** ** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control); ** ** All non-hidden columns (i.e. all columns matched by "SELECT *") of the ** target table must be present in the input table. For virtual tables, ** hidden columns are optional - they are updated by RBU if present in ** the input table, or not otherwise. For example, to write to an fts4 ** table with a hidden languageid column such as: ** ** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid'); ** ** Either of the following input table schemas may be used: ** ** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control); ** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control); ** ** For each row to INSERT into the target database as part of the RBU ** update, the corresponding data_% table should contain a single record ** with the "rbu_control" column set to contain integer value 0. The ** other columns should be set to the values that make up the new record ** to insert. ** ** If the target database table has an INTEGER PRIMARY KEY, it is not ** possible to insert a NULL value into the IPK column. Attempting to ** do so results in an SQLITE_MISMATCH error. ** ** For each row to DELETE from the target database as part of the RBU ** update, the corresponding data_% table should contain a single record ** with the "rbu_control" column set to contain integer value 1. The ** real primary key values of the row to delete should be stored in the ** corresponding columns of the data_% table. The values stored in the ** other columns are not used. ** ** For each row to UPDATE from the target database as part of the RBU ** update, the corresponding data_% table should contain a single record ** with the "rbu_control" column set to contain a value of type text. ** The real primary key values identifying the row to update should be ** stored in the corresponding columns of the data_% table row, as should ** the new values of all columns being update. The text value in the ** "rbu_control" column must contain the same number of characters as ** there are columns in the target database table, and must consist entirely ** of 'x' and '.' characters (or in some special cases 'd' - see below). For ** each column that is being updated, the corresponding character is set to ** 'x'. For those that remain as they are, the corresponding character of the ** rbu_control value should be set to '.'. For example, given the tables ** above, the update statement: ** ** UPDATE t1 SET c = 'usa' WHERE a = 4; ** ** is represented by the data_t1 row created by: ** ** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x'); ** ** Instead of an 'x' character, characters of the rbu_control value specified ** for UPDATEs may also be set to 'd'. In this case, instead of updating the ** target table with the value stored in the corresponding data_% column, the ** user-defined SQL function "rbu_delta()" is invoked and the result stored in ** the target table column. rbu_delta() is invoked with two arguments - the ** original value currently stored in the target table column and the ** value specified in the data_xxx table. ** ** For example, this row: ** ** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d'); ** ** is similar to an UPDATE statement such as: ** ** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4; ** ** Finally, if an 'f' character appears in place of a 'd' or 's' in an ** ota_control string, the contents of the data_xxx table column is assumed ** to be a "fossil delta" - a patch to be applied to a blob value in the ** format used by the fossil source-code management system. In this case ** the existing value within the target database table must be of type BLOB. ** It is replaced by the result of applying the specified fossil delta to ** itself. ** ** If the target database table is a virtual table or a table with no PRIMARY ** KEY, the rbu_control value should not include a character corresponding ** to the rbu_rowid value. For example, this: ** ** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control) ** VALUES(NULL, 'usa', 12, '.x'); ** ** causes a result similar to: ** ** UPDATE ft1 SET b = 'usa' WHERE rowid = 12; ** ** The data_xxx tables themselves should have no PRIMARY KEY declarations. ** However, RBU is more efficient if reading the rows in from each data_xxx ** table in "rowid" order is roughly the same as reading them sorted by ** the PRIMARY KEY of the corresponding target database table. In other ** words, rows should be sorted using the destination table PRIMARY KEY ** fields before they are inserted into the data_xxx tables. ** ** USAGE ** ** The API declared below allows an application to apply an RBU update ** stored on disk to an existing target database. Essentially, the ** application: ** ** 1) Opens an RBU handle using the sqlite3rbu_open() function. ** ** 2) Registers any required virtual table modules with the database ** handle returned by sqlite3rbu_db(). Also, if required, register ** the rbu_delta() implementation. ** ** 3) Calls the sqlite3rbu_step() function one or more times on ** the new handle. Each call to sqlite3rbu_step() performs a single ** b-tree operation, so thousands of calls may be required to apply ** a complete update. ** ** 4) Calls sqlite3rbu_close() to close the RBU update handle. If ** sqlite3rbu_step() has been called enough times to completely ** apply the update to the target database, then the RBU database ** is marked as fully applied. Otherwise, the state of the RBU ** update application is saved in the RBU database for later ** resumption. ** ** See comments below for more detail on APIs. ** ** If an update is only partially applied to the target database by the ** time sqlite3rbu_close() is called, various state information is saved ** within the RBU database. This allows subsequent processes to automatically ** resume the RBU update from where it left off. ** ** To remove all RBU extension state information, returning an RBU database ** to its original contents, it is sufficient to drop all tables that begin ** with the prefix "rbu_" ** ** DATABASE LOCKING ** ** An RBU update may not be applied to a database in WAL mode. Attempting ** to do so is an error (SQLITE_ERROR). ** ** While an RBU handle is open, a SHARED lock may be held on the target ** database file. This means it is possible for other clients to read the ** database, but not to write it. ** ** If an RBU update is started and then suspended before it is completed, ** then an external client writes to the database, then attempting to resume ** the suspended RBU update is also an error (SQLITE_BUSY). */ /* #include "sqlite3.h" ** Required for error code definitions ** */ type Tsqlite3rbu = struct { F__ccgo_align [0]uint32 FeStage int32 FdbMain uintptr FdbRbu uintptr FzTarget uintptr FzRbu uintptr FzState uintptr FzStateDb [5]uint8 Frc int32 FzErrmsg uintptr FnStep int32 FnProgress int32 Fobjiter TRbuObjIter FzVfsName uintptr FpTargetFd uintptr FnPagePerSector int32 FiOalSz Ti64 FnPhaseOneStep Ti64 FpRenameArg uintptr FxRename uintptr FiMaxFrame Tu32 FmLock Tu32 FnFrame int32 FnFrameAlloc int32 FaFrame uintptr Fpgsz int32 FaBuf uintptr F__ccgo_align26 [4]byte FiWalCksum Ti64 FszTemp Ti64 FszTempLimit Ti64 FnRbu int32 FpRbuFd uintptr } type sqlite3rbu = Tsqlite3rbu /************** End of sqlite3rbu.h ******************************************/ /************** Continuing where we left off in sqlite3rbu.c *****************/ /* Maximum number of prepared UPDATE statements held by this module */ /* Delta checksums disabled by default. Compile with -DRBU_ENABLE_DELTA_CKSUM ** to enable checksum verification. */ /* ** Swap two objects of type TYPE. */ /* ** Name of the URI option that causes RBU to take an exclusive lock as ** part of the incremental checkpoint operation. */ /* ** The rbu_state table is used to save the state of a partially applied ** update so that it can be resumed later. The table consists of integer ** keys mapped to values as follows: ** ** RBU_STATE_STAGE: ** May be set to integer values 1, 2, 4 or 5. As follows: ** 1: the *-rbu file is currently under construction. ** 2: the *-rbu file has been constructed, but not yet moved ** to the *-wal path. ** 4: the checkpoint is underway. ** 5: the rbu update has been checkpointed. ** ** RBU_STATE_TBL: ** Only valid if STAGE==1. The target database name of the table ** currently being written. ** ** RBU_STATE_IDX: ** Only valid if STAGE==1. The target database name of the index ** currently being written, or NULL if the main table is currently being ** updated. ** ** RBU_STATE_ROW: ** Only valid if STAGE==1. Number of rows already processed for the current ** table/index. ** ** RBU_STATE_PROGRESS: ** Trbul number of sqlite3rbu_step() calls made so far as part of this ** rbu update. ** ** RBU_STATE_CKPT: ** Valid if STAGE==4. The 64-bit checksum associated with the wal-index ** header created by recovering the *-wal file. This is used to detect ** cases when another client appends frames to the *-wal file in the ** middle of an incremental checkpoint (an incremental checkpoint cannot ** be continued if this happens). ** ** RBU_STATE_COOKIE: ** Valid if STAGE==1. The current change-counter cookie value in the ** target db file. ** ** RBU_STATE_OALSZ: ** Valid if STAGE==1. The size in bytes of the *-oal file. ** ** RBU_STATE_DATATBL: ** Only valid if STAGE==1. The RBU database name of the table ** currently being read. */ type TRbuFrame = struct { FiDbPage Tu32 FiWalFrame Tu32 } type RbuFrame = TRbuFrame type TRbuObjIter = struct { FpTblIter uintptr FpIdxIter uintptr FnTblCol int32 FazTblCol uintptr FazTblType uintptr FaiSrcOrder uintptr FabTblPk uintptr FabNotNull uintptr FabIndexed uintptr FeType int32 FbCleanup int32 FzTbl uintptr FzDataTbl uintptr FzIdx uintptr FiTnum int32 FiPkTnum int32 FbUnique int32 FnIndex int32 FnCol int32 FpSelect uintptr FpInsert uintptr FpDelete uintptr FpTmpInsert uintptr FnIdxCol int32 FaIdxCol uintptr FzIdxSql uintptr FpRbuUpdate uintptr } type RbuObjIter = TRbuObjIter type TRbuState = struct { F__ccgo_align [0]uint32 FeStage int32 FzTbl uintptr FzDataTbl uintptr FzIdx uintptr FiWalCksum Ti64 FnRow int32 F__ccgo_align6 [4]byte FnProgress Ti64 FiCookie Tu32 F__ccgo_align8 [4]byte FiOalSz Ti64 FnPhaseOneStep Ti64 } type RbuState = TRbuState type TRbuSpan = struct { FzSpan uintptr FnSpan int32 } type RbuSpan = TRbuSpan type Trbu_vfs = struct { Fbase Tsqlite3_vfs FpRealVfs uintptr Fmutex uintptr FpRbu uintptr FpMain uintptr FpMainRbu uintptr } type rbu_vfs = Trbu_vfs type Trbu_file = struct { F__ccgo_align [0]uint32 Fbase Tsqlite3_file FpReal uintptr FpRbuVfs uintptr FpRbu uintptr Fsz Ti64 FopenFlags int32 FiCookie Tu32 FiWriteVer Tu8 FbNolock Tu8 FnShm int32 FapShm uintptr FzDel uintptr FzWal uintptr FpWalFd uintptr FpMainNext uintptr FpMainRbuNext uintptr } type rbu_file = Trbu_file type TRbuUpdateStmt = struct { FzMask uintptr FpUpdate uintptr FpNext uintptr } type RbuUpdateStmt = TRbuUpdateStmt /* ** These values must match the values defined in wal.c for the equivalent ** locks. These are not magic numbers as they are part of the SQLite file ** format. */ /* ** A structure to store values read from the rbu_state table in memory. */ type TRbuState1 = struct { F__ccgo_align [0]uint32 FeStage int32 FzTbl uintptr FzDataTbl uintptr FzIdx uintptr FiWalCksum Ti64 FnRow int32 F__ccgo_align6 [4]byte FnProgress Ti64 FiCookie Tu32 F__ccgo_align8 [4]byte FiOalSz Ti64 FnPhaseOneStep Ti64 } type RbuState1 = TRbuState1 type TRbuUpdateStmt1 = struct { FzMask uintptr FpUpdate uintptr FpNext uintptr } type RbuUpdateStmt1 = TRbuUpdateStmt1 type TRbuSpan1 = struct { FzSpan uintptr FnSpan int32 } type RbuSpan1 = TRbuSpan1 /* ** An iterator of this type is used to iterate through all objects in ** the target database that require updating. For each such table, the ** iterator visits, in order: ** ** * the table itself, ** * each index of the table (zero or more points to visit), and ** * a special "cleanup table" state. ** ** abIndexed: ** If the table has no indexes on it, abIndexed is set to NULL. Otherwise, ** it points to an array of flags nTblCol elements in size. The flag is ** set for each column that is either a part of the PK or a part of an ** index. Or clear otherwise. ** ** If there are one or more partial indexes on the table, all fields of ** this array set set to 1. This is because in that case, the module has ** no way to tell which fields will be required to add and remove entries ** from the partial indexes. ** */ type TRbuObjIter1 = struct { FpTblIter uintptr FpIdxIter uintptr FnTblCol int32 FazTblCol uintptr FazTblType uintptr FaiSrcOrder uintptr FabTblPk uintptr FabNotNull uintptr FabIndexed uintptr FeType int32 FbCleanup int32 FzTbl uintptr FzDataTbl uintptr FzIdx uintptr FiTnum int32 FiPkTnum int32 FbUnique int32 FnIndex int32 FnCol int32 FpSelect uintptr FpInsert uintptr FpDelete uintptr FpTmpInsert uintptr FnIdxCol int32 FaIdxCol uintptr FzIdxSql uintptr FpRbuUpdate uintptr } type RbuObjIter1 = TRbuObjIter1 /* ** Values for RbuObjIter.eType ** ** 0: Table does not exist (error) ** 1: Table has an implicit rowid. ** 2: Table has an explicit IPK column. ** 3: Table has an external PK index. ** 4: Table is WITHOUT ROWID. ** 5: Table is a virtual table. */ /* ** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs ** one of the following operations. */ /* ** A single step of an incremental checkpoint - frame iWalFrame of the wal ** file should be copied to page iDbPage of the database file. */ type TRbuFrame1 = struct { FiDbPage Tu32 FiWalFrame Tu32 } type RbuFrame1 = TRbuFrame1 /* ** RBU handle. ** ** nPhaseOneStep: ** If the RBU database contains an rbu_count table, this value is set to ** a running estimate of the number of b-tree operations required to ** finish populating the *-oal file. This allows the sqlite3_bp_progress() ** API to calculate the permyriadage progress of populating the *-oal file ** using the formula: ** ** permyriadage = (10000 * nProgress) / nPhaseOneStep ** ** nPhaseOneStep is initialized to the sum of: ** ** nRow * (nIndex + 1) ** ** for all source tables in the RBU database, where nRow is the number ** of rows in the source table and nIndex the number of indexes on the ** corresponding target database table. ** ** This estimate is accurate if the RBU update consists entirely of ** INSERT operations. However, it is inaccurate if: ** ** * the RBU update contains any UPDATE operations. If the PK specified ** for an UPDATE operation does not exist in the target table, then ** no b-tree operations are required on index b-trees. Or if the ** specified PK does exist, then (nIndex*2) such operations are ** required (one delete and one insert on each index b-tree). ** ** * the RBU update contains any DELETE operations for which the specified ** PK does not exist. In this case no operations are required on index ** b-trees. ** ** * the RBU update contains REPLACE operations. These are similar to ** UPDATE operations. ** ** nPhaseOneStep is updated to account for the conditions above during the ** first pass of each source table. The updated nPhaseOneStep value is ** stored in the rbu_state table if the RBU update is suspended. */ type Tsqlite3rbu1 = struct { F__ccgo_align [0]uint32 FeStage int32 FdbMain uintptr FdbRbu uintptr FzTarget uintptr FzRbu uintptr FzState uintptr FzStateDb [5]uint8 Frc int32 FzErrmsg uintptr FnStep int32 FnProgress int32 Fobjiter TRbuObjIter FzVfsName uintptr FpTargetFd uintptr FnPagePerSector int32 FiOalSz Ti64 FnPhaseOneStep Ti64 FpRenameArg uintptr FxRename uintptr FiMaxFrame Tu32 FmLock Tu32 FnFrame int32 FnFrameAlloc int32 FaFrame uintptr Fpgsz int32 FaBuf uintptr F__ccgo_align26 [4]byte FiWalCksum Ti64 FszTemp Ti64 FszTempLimit Ti64 FnRbu int32 FpRbuFd uintptr } type sqlite3rbu1 = Tsqlite3rbu1 /* ** An rbu VFS is implemented using an instance of this structure. ** ** Variable pRbu is only non-NULL for automatically created RBU VFS objects. ** It is NULL for RBU VFS objects created explicitly using ** sqlite3rbu_create_vfs(). It is used to track the total amount of temp ** space used by the RBU handle. */ type Trbu_vfs1 = struct { Fbase Tsqlite3_vfs FpRealVfs uintptr Fmutex uintptr FpRbu uintptr FpMain uintptr FpMainRbu uintptr } type rbu_vfs1 = Trbu_vfs1 /* ** Each file opened by an rbu VFS is represented by an instance of ** the following structure. ** ** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable ** "sz" is set to the current size of the database file. */ type Trbu_file1 = struct { F__ccgo_align [0]uint32 Fbase Tsqlite3_file FpReal uintptr FpRbuVfs uintptr FpRbu uintptr Fsz Ti64 FopenFlags int32 FiCookie Tu32 FiWriteVer Tu8 FbNolock Tu8 FnShm int32 FapShm uintptr FzDel uintptr FzWal uintptr FpWalFd uintptr FpMainNext uintptr FpMainRbuNext uintptr } type rbu_file1 = Trbu_file1 /* ** True for an RBU vacuum handle, or false otherwise. */ /************************************************************************* ** The following three functions, found below: ** ** rbuDeltaGetInt() ** rbuDeltaChecksum() ** rbuDeltaApply() ** ** are lifted from the fossil source code (http://fossil-scm.org). They ** are used to implement the scalar SQL function rbu_fossil_delta(). */ // C documentation // // /* // ** Read bytes from *pz and convert them into a positive integer. When // ** finished, leave *pz pointing to the first character past the end of // ** the integer. The *pLen parameter holds the length of the string // ** in *pz and is decremented once for each character in the integer. // */ func _rbuDeltaGetInt(tls *libc.TLS, pz uintptr, pLen uintptr) (r uint32) { var c, v1 int32 var v uint32 var z, zStart, v2 uintptr _, _, _, _, _, _ = c, v, z, zStart, v1, v2 v = uint32(0) z = *(*uintptr)(unsafe.Pointer(pz)) zStart = z for { v2 = z z++ v1 = int32(_zValue[int32(0x7f)&int32(*(*uint8)(unsafe.Pointer(v2)))]) c = v1 if !(v1 >= 0) { break } v = v< 0 { cnt = _rbuDeltaGetInt(tls, bp, bp+4) switch int32(*(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp))))) { case int32('@'): *(*uintptr)(unsafe.Pointer(bp))++ *(*int32)(unsafe.Pointer(bp + 4))-- ofst = _rbuDeltaGetInt(tls, bp, bp+4) if *(*int32)(unsafe.Pointer(bp + 4)) > 0 && int32(*(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp))))) != int32(',') { /* ERROR: copy command not terminated by ',' */ return -int32(1) } *(*uintptr)(unsafe.Pointer(bp))++ *(*int32)(unsafe.Pointer(bp + 4))-- total += cnt if total > limit { /* ERROR: copy exceeds output file size */ return -int32(1) } if int32(ofst+cnt) > lenSrc { /* ERROR: copy extends past end of input */ return -int32(1) } libc.Xmemcpy(tls, zOut, zSrc+uintptr(ofst), cnt) zOut += uintptr(cnt) case int32(':'): *(*uintptr)(unsafe.Pointer(bp))++ *(*int32)(unsafe.Pointer(bp + 4))-- total += cnt if total > limit { /* ERROR: insert command gives an output larger than predicted */ return -int32(1) } if int32(cnt) > *(*int32)(unsafe.Pointer(bp + 4)) { /* ERROR: insert count exceeds size of delta */ return -int32(1) } libc.Xmemcpy(tls, zOut, *(*uintptr)(unsafe.Pointer(bp)), cnt) zOut += uintptr(cnt) *(*uintptr)(unsafe.Pointer(bp)) += uintptr(cnt) *(*int32)(unsafe.Pointer(bp + 4)) = int32(uint32(*(*int32)(unsafe.Pointer(bp + 4))) - cnt) case int32(';'): *(*uintptr)(unsafe.Pointer(bp))++ *(*int32)(unsafe.Pointer(bp + 4))-- *(*uint8)(unsafe.Pointer(zOut)) = uint8(0) if total != limit { /* ERROR: generated size does not match predicted size */ return -int32(1) } return int32(total) default: /* ERROR: unknown delta operator */ return -int32(1) } } /* ERROR: unterminated delta */ return -int32(1) } func _rbuDeltaOutputSize(tls *libc.TLS, _zDelta uintptr, _lenDelta int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) *(*uintptr)(unsafe.Pointer(bp)) = _zDelta *(*int32)(unsafe.Pointer(bp + 4)) = _lenDelta var size int32 _ = size size = int32(_rbuDeltaGetInt(tls, bp, bp+4)) if int32(*(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp))))) != int32('\n') { /* ERROR: size integer not terminated by "\n" */ return -int32(1) } return size } /* ** End of code taken from fossil. *************************************************************************/ // C documentation // // /* // ** Implementation of SQL scalar function rbu_fossil_delta(). // ** // ** This function applies a fossil delta patch to a blob. Exactly two // ** arguments must be passed to this function. The first is the blob to // ** patch and the second the patch to apply. If no error occurs, this // ** function returns the patched blob. // */ func _rbuFossilDeltaFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { var aDelta, aOrig, aOut uintptr var nDelta, nOrig, nOut, nOut2 int32 _, _, _, _, _, _, _ = aDelta, aOrig, aOut, nDelta, nOrig, nOut, nOut2 _ = libc.Int32FromInt32(0) nOrig = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv))) aOrig = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv))) nDelta = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) aDelta = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) /* Figure out the size of the output */ nOut = _rbuDeltaOutputSize(tls, aDelta, nDelta) if nOut < 0 { Xsqlite3_result_error(tls, context, __ccgo_ts+28569, -int32(1)) return } aOut = Xsqlite3_malloc(tls, nOut+int32(1)) if aOut == uintptr(0) { Xsqlite3_result_error_nomem(tls, context) } else { nOut2 = _rbuDeltaApply(tls, aOrig, nOrig, aDelta, nDelta, aOut) if nOut2 != nOut { Xsqlite3_free(tls, aOut) Xsqlite3_result_error(tls, context, __ccgo_ts+28569, -int32(1)) } else { Xsqlite3_result_blob(tls, context, aOut, nOut, __ccgo_fp(Xsqlite3_free)) } } } // C documentation // // /* // ** Prepare the SQL statement in buffer zSql against database handle db. // ** If successful, set *ppStmt to point to the new statement and return // ** SQLITE_OK. // ** // ** Otherwise, if an error does occur, set *ppStmt to NULL and return // ** an SQLite error code. Additionally, set output variable *pzErrmsg to // ** point to a buffer containing an error message. It is the responsibility // ** of the caller to (eventually) free this buffer using sqlite3_free(). // */ func _prepareAndCollectError(tls *libc.TLS, db uintptr, ppStmt uintptr, pzErrmsg uintptr, zSql uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 _ = rc rc = Xsqlite3_prepare_v2(tls, db, zSql, -int32(1), ppStmt, uintptr(0)) if rc != SQLITE_OK { *(*uintptr)(unsafe.Pointer(pzErrmsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+8, Xsqlite3_errmsg(tls, db))) *(*uintptr)(unsafe.Pointer(ppStmt)) = uintptr(0) } return rc } // C documentation // // /* // ** Reset the SQL statement passed as the first argument. Return a copy // ** of the value returned by sqlite3_reset(). // ** // ** If an error has occurred, then set *pzErrmsg to point to a buffer // ** containing an error message. It is the responsibility of the caller // ** to eventually free this buffer using sqlite3_free(). // */ func _resetAndCollectError(tls *libc.TLS, pStmt uintptr, pzErrmsg uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 _ = rc rc = Xsqlite3_reset(tls, pStmt) if rc != SQLITE_OK { *(*uintptr)(unsafe.Pointer(pzErrmsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+8, Xsqlite3_errmsg(tls, Xsqlite3_db_handle(tls, pStmt)))) } return rc } // C documentation // // /* // ** Unless it is NULL, argument zSql points to a buffer allocated using // ** sqlite3_malloc containing an SQL statement. This function prepares the SQL // ** statement against database db and frees the buffer. If statement // ** compilation is successful, *ppStmt is set to point to the new statement // ** handle and SQLITE_OK is returned. // ** // ** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code // ** returned. In this case, *pzErrmsg may also be set to point to an error // ** message. It is the responsibility of the caller to free this error message // ** buffer using sqlite3_free(). // ** // ** If argument zSql is NULL, this function assumes that an OOM has occurred. // ** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL. // */ func _prepareFreeAndCollectError(tls *libc.TLS, db uintptr, ppStmt uintptr, pzErrmsg uintptr, zSql uintptr) (r int32) { var rc int32 _ = rc _ = libc.Int32FromInt32(0) if zSql == uintptr(0) { rc = int32(SQLITE_NOMEM) *(*uintptr)(unsafe.Pointer(ppStmt)) = uintptr(0) } else { rc = _prepareAndCollectError(tls, db, ppStmt, pzErrmsg, zSql) Xsqlite3_free(tls, zSql) } return rc } // C documentation // // /* // ** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated // ** by an earlier call to rbuObjIterCacheTableInfo(). // */ func _rbuObjIterFreeCols(tls *libc.TLS, pIter uintptr) { var i int32 _ = i i = 0 for { if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) { break } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4))) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(i)*4))) goto _1 _1: ; i++ } Xsqlite3_free(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol) (*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol = uintptr(0) (*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType = uintptr(0) (*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder = uintptr(0) (*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk = uintptr(0) (*TRbuObjIter)(unsafe.Pointer(pIter)).FabNotNull = uintptr(0) (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol = 0 (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType = 0 /* Invalid value */ } // C documentation // // /* // ** Finalize all statements and free all allocations that are specific to // ** the current object (table/index pair). // */ func _rbuObjIterClearStatements(tls *libc.TLS, pIter uintptr) { var pTmp, pUp uintptr _, _ = pTmp, pUp Xsqlite3_finalize(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect) Xsqlite3_finalize(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpInsert) Xsqlite3_finalize(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpDelete) Xsqlite3_finalize(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpTmpInsert) pUp = (*TRbuObjIter)(unsafe.Pointer(pIter)).FpRbuUpdate for pUp != 0 { pTmp = (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpNext Xsqlite3_finalize(tls, (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpUpdate) Xsqlite3_free(tls, pUp) pUp = pTmp } Xsqlite3_free(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol) Xsqlite3_free(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdxSql) (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect = uintptr(0) (*TRbuObjIter)(unsafe.Pointer(pIter)).FpInsert = uintptr(0) (*TRbuObjIter)(unsafe.Pointer(pIter)).FpDelete = uintptr(0) (*TRbuObjIter)(unsafe.Pointer(pIter)).FpRbuUpdate = uintptr(0) (*TRbuObjIter)(unsafe.Pointer(pIter)).FpTmpInsert = uintptr(0) (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol = 0 (*TRbuObjIter)(unsafe.Pointer(pIter)).FnIdxCol = 0 (*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol = uintptr(0) (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdxSql = uintptr(0) } // C documentation // // /* // ** Clean up any resources allocated as part of the iterator object passed // ** as the only argument. // */ func _rbuObjIterFinalize(tls *libc.TLS, pIter uintptr) { _rbuObjIterClearStatements(tls, pIter) Xsqlite3_finalize(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpTblIter) Xsqlite3_finalize(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpIdxIter) _rbuObjIterFreeCols(tls, pIter) libc.Xmemset(tls, pIter, 0, uint32(108)) } // C documentation // // /* // ** Advance the iterator to the next position. // ** // ** If no error occurs, SQLITE_OK is returned and the iterator is left // ** pointing to the next entry. Otherwise, an error code and message is // ** left in the RBU handle passed as the first argument. A copy of the // ** error code is returned. // */ func _rbuObjIterNext(tls *libc.TLS, p uintptr, pIter uintptr) (r int32) { var pIdx uintptr var rc, v1, v2 int32 _, _, _, _ = pIdx, rc, v1, v2 rc = (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc if rc == SQLITE_OK { /* Free any SQLite statements used while processing the previous object */ _rbuObjIterClearStatements(tls, pIter) if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx == uintptr(0) { rc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+28590, uintptr(0), uintptr(0), p+36) } if rc == SQLITE_OK { if (*TRbuObjIter)(unsafe.Pointer(pIter)).FbCleanup != 0 { _rbuObjIterFreeCols(tls, pIter) (*TRbuObjIter)(unsafe.Pointer(pIter)).FbCleanup = 0 rc = Xsqlite3_step(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpTblIter) if rc != int32(SQLITE_ROW) { rc = _resetAndCollectError(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpTblIter, p+36) (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl = uintptr(0) } else { (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl = Xsqlite3_column_text(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpTblIter, 0) (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl = Xsqlite3_column_text(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpTblIter, int32(1)) if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl != 0 && (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl != 0 { v1 = SQLITE_OK } else { v1 = int32(SQLITE_NOMEM) } rc = v1 } } else { if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx == uintptr(0) { pIdx = (*TRbuObjIter)(unsafe.Pointer(pIter)).FpIdxIter rc = Xsqlite3_bind_text(tls, pIdx, int32(1), (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, -int32(1), libc.UintptrFromInt32(0)) } if rc == SQLITE_OK { rc = Xsqlite3_step(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpIdxIter) if rc != int32(SQLITE_ROW) { rc = _resetAndCollectError(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpIdxIter, p+36) (*TRbuObjIter)(unsafe.Pointer(pIter)).FbCleanup = int32(1) (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx = uintptr(0) } else { (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx = Xsqlite3_column_text(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpIdxIter, 0) (*TRbuObjIter)(unsafe.Pointer(pIter)).FiTnum = Xsqlite3_column_int(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpIdxIter, int32(1)) (*TRbuObjIter)(unsafe.Pointer(pIter)).FbUnique = Xsqlite3_column_int(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpIdxIter, int32(2)) if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx != 0 { v2 = SQLITE_OK } else { v2 = int32(SQLITE_NOMEM) } rc = v2 } } } } } if rc != SQLITE_OK { _rbuObjIterFinalize(tls, pIter) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc } return rc } // C documentation // // /* // ** The implementation of the rbu_target_name() SQL function. This function // ** accepts one or two arguments. The first argument is the name of a table - // ** the name of a table in the RBU database. The second, if it is present, is 1 // ** for a view or 0 for a table. // ** // ** For a non-vacuum RBU handle, if the table name matches the pattern: // ** // ** data[0-9]_ // ** // ** where is any sequence of 1 or more characters, is returned. // ** Otherwise, if the only argument does not match the above pattern, an SQL // ** NULL is returned. // ** // ** "data_t1" -> "t1" // ** "data0123_t2" -> "t2" // ** "dataAB_t3" -> NULL // ** // ** For an rbu vacuum handle, a copy of the first argument is returned if // ** the second argument is either missing or 0 (not a view). // */ func _rbuTargetNameFunc(tls *libc.TLS, pCtx uintptr, argc int32, argv uintptr) { var i int32 var p, zIn uintptr _, _, _ = i, p, zIn p = Xsqlite3_user_data(tls, pCtx) _ = libc.Int32FromInt32(0) zIn = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) if zIn != 0 { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { _ = libc.Int32FromInt32(0) if argc == int32(1) || 0 == Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 1*4))) { Xsqlite3_result_text(tls, pCtx, zIn, -int32(1), libc.UintptrFromInt32(0)) } } else { if libc.Xstrlen(tls, zIn) > uint32(4) && libc.Xmemcmp(tls, __ccgo_ts+25741, zIn, uint32(4)) == 0 { i = int32(4) for { if !(int32(*(*uint8)(unsafe.Pointer(zIn + uintptr(i)))) >= int32('0') && int32(*(*uint8)(unsafe.Pointer(zIn + uintptr(i)))) <= int32('9')) { break } goto _1 _1: ; i++ } if int32(*(*uint8)(unsafe.Pointer(zIn + uintptr(i)))) == int32('_') && *(*uint8)(unsafe.Pointer(zIn + uintptr(i+int32(1)))) != 0 { Xsqlite3_result_text(tls, pCtx, zIn+uintptr(i+int32(1)), -int32(1), libc.UintptrFromInt32(0)) } } } } } // C documentation // // /* // ** Initialize the iterator structure passed as the second argument. // ** // ** If no error occurs, SQLITE_OK is returned and the iterator is left // ** pointing to the first entry. Otherwise, an error code and message is // ** left in the RBU handle passed as the first argument. A copy of the // ** error code is returned. // */ func _rbuObjIterFirst(tls *libc.TLS, p uintptr, pIter uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var v1 uintptr _, _ = rc, v1 libc.Xmemset(tls, pIter, 0, uint32(108)) if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { v1 = __ccgo_ts + 28761 } else { v1 = __ccgo_ts + 1648 } rc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, pIter, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+28802, libc.VaList(bp+8, v1))) if rc == SQLITE_OK { rc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pIter+4, p+36, __ccgo_ts+28952) } (*TRbuObjIter)(unsafe.Pointer(pIter)).FbCleanup = int32(1) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc return _rbuObjIterNext(tls, p, pIter) } // C documentation // // /* // ** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs, // ** an error code is stored in the RBU handle passed as the first argument. // ** // ** If an error has already occurred (p->rc is already set to something other // ** than SQLITE_OK), then this function returns NULL without modifying the // ** stored error code. In this case it still calls sqlite3_free() on any // ** printf() parameters associated with %z conversions. // */ func _rbuMPrintf(tls *libc.TLS, p uintptr, zFmt uintptr, va uintptr) (r uintptr) { var ap Tva_list var zSql uintptr _, _ = ap, zSql zSql = uintptr(0) ap = va zSql = Xsqlite3_vmprintf(tls, zFmt, ap) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if zSql == uintptr(0) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) } } else { Xsqlite3_free(tls, zSql) zSql = uintptr(0) } _ = ap return zSql } // C documentation // // /* // ** Argument zFmt is a sqlite3_mprintf() style format string. The trailing // ** arguments are the usual subsitution values. This function performs // ** the printf() style substitutions and executes the result as an SQL // ** statement on the RBU handles database. // ** // ** If an error occurs, an error code and error message is stored in the // ** RBU handle. If an error has already occurred when this function is // ** called, it is a no-op. // */ func _rbuMPrintfExec(tls *libc.TLS, p uintptr, db uintptr, zFmt uintptr, va uintptr) (r int32) { var ap Tva_list var zSql uintptr _, _ = ap, zSql ap = va zSql = Xsqlite3_vmprintf(tls, zFmt, ap) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if zSql == uintptr(0) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) } else { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, db, zSql, uintptr(0), uintptr(0), p+36) } } Xsqlite3_free(tls, zSql) _ = ap return (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc } // C documentation // // /* // ** Attempt to allocate and return a pointer to a zeroed block of nByte // ** bytes. // ** // ** If an error (i.e. an OOM condition) occurs, return NULL and leave an // ** error code in the rbu handle passed as the first argument. Or, if an // ** error has already occurred when this function is called, return NULL // ** immediately without attempting the allocation or modifying the stored // ** error code. // */ func _rbuMalloc(tls *libc.TLS, p uintptr, nByte Tsqlite3_int64) (r uintptr) { var pRet uintptr _ = pRet pRet = uintptr(0) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { _ = libc.Int32FromInt32(0) pRet = Xsqlite3_malloc64(tls, uint64(nByte)) if pRet == uintptr(0) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, pRet, 0, uint32(nByte)) } } return pRet } // C documentation // // /* // ** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that // ** there is room for at least nCol elements. If an OOM occurs, store an // ** error code in the RBU handle passed as the first argument. // */ func _rbuAllocateIterArrays(tls *libc.TLS, p uintptr, pIter uintptr, nCol int32) { var azNew uintptr var nByte Tsqlite3_int64 _, _ = azNew, nByte nByte = int64((libc.Uint32FromInt32(2)*libc.Uint32FromInt64(4) + libc.Uint32FromInt64(4) + libc.Uint32FromInt32(3)*libc.Uint32FromInt64(1)) * uint32(nCol)) azNew = _rbuMalloc(tls, p, nByte) if azNew != 0 { (*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol = azNew (*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType = azNew + uintptr(nCol)*4 (*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder = (*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(nCol)*4 (*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk = (*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(nCol)*4 (*TRbuObjIter)(unsafe.Pointer(pIter)).FabNotNull = (*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(nCol) (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed = (*TRbuObjIter)(unsafe.Pointer(pIter)).FabNotNull + uintptr(nCol) } } // C documentation // // /* // ** The first argument must be a nul-terminated string. This function // ** returns a copy of the string in memory obtained from sqlite3_malloc(). // ** It is the responsibility of the caller to eventually free this memory // ** using sqlite3_free(). // ** // ** If an OOM condition is encountered when attempting to allocate memory, // ** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise, // ** if the allocation succeeds, (*pRc) is left unchanged. // */ func _rbuStrndup(tls *libc.TLS, zStr uintptr, pRc uintptr) (r uintptr) { var nCopy Tsize_t var zRet uintptr _, _ = nCopy, zRet zRet = uintptr(0) if *(*int32)(unsafe.Pointer(pRc)) == SQLITE_OK { if zStr != 0 { nCopy = libc.Xstrlen(tls, zStr) + uint32(1) zRet = Xsqlite3_malloc64(tls, uint64(nCopy)) if zRet != 0 { libc.Xmemcpy(tls, zRet, zStr, nCopy) } else { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) } } } return zRet } // C documentation // // /* // ** Finalize the statement passed as the second argument. // ** // ** If the sqlite3_finalize() call indicates that an error occurs, and the // ** rbu handle error code is not already set, set the error code and error // ** message accordingly. // */ func _rbuFinalize(tls *libc.TLS, p uintptr, pStmt uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db uintptr var rc int32 _, _ = db, rc db = Xsqlite3_db_handle(tls, pStmt) rc = Xsqlite3_finalize(tls, pStmt) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && rc != SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+8, Xsqlite3_errmsg(tls, db))) } } // C documentation // // /* Determine the type of a table. // ** // ** peType is of type (int*), a pointer to an output parameter of type // ** (int). This call sets the output parameter as follows, depending // ** on the type of the table specified by parameters dbName and zTbl. // ** // ** RBU_PK_NOTABLE: No such table. // ** RBU_PK_NONE: Table has an implicit rowid. // ** RBU_PK_IPK: Table has an explicit IPK column. // ** RBU_PK_EXTERNAL: Table has an external PK index. // ** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID. // ** RBU_PK_VTAB: Table is a virtual table. // ** // ** Argument *piPk is also of type (int*), and also points to an output // ** parameter. Unless the table has an external primary key index // ** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or, // ** if the table does have an external primary key index, then *piPk // ** is set to the root page number of the primary key index before // ** returning. // ** // ** ALGORITHM: // ** // ** if( no entry exists in sqlite_schema ){ // ** return RBU_PK_NOTABLE // ** }else if( sql for the entry starts with "CREATE VIRTUAL" ){ // ** return RBU_PK_VTAB // ** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){ // ** if( the index that is the pk exists in sqlite_schema ){ // ** *piPK = rootpage of that index. // ** return RBU_PK_EXTERNAL // ** }else{ // ** return RBU_PK_WITHOUT_ROWID // ** } // ** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){ // ** return RBU_PK_IPK // ** }else{ // ** return RBU_PK_NONE // ** } // */ func _rbuTableType(tls *libc.TLS, p uintptr, zTab uintptr, peType uintptr, piTnum uintptr, piPk uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var i uint32 var zIdx, zOrig uintptr var _ /* aStmt at bp+0 */ [4]uintptr _, _, _ = i, zIdx, zOrig /* ** 0) SELECT count(*) FROM sqlite_schema where name=%Q AND IsVirtual(%Q) ** 1) PRAGMA index_list = ? ** 2) SELECT count(*) FROM sqlite_schema where name=%Q ** 3) PRAGMA table_info = ? */ *(*[4]uintptr)(unsafe.Pointer(bp)) = [4]uintptr{} *(*int32)(unsafe.Pointer(peType)) = RBU_PK_NOTABLE *(*int32)(unsafe.Pointer(piPk)) = 0 _ = libc.Int32FromInt32(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29077, libc.VaList(bp+24, zTab))) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != SQLITE_OK || Xsqlite3_step(tls, (*(*[4]uintptr)(unsafe.Pointer(bp)))[0]) != int32(SQLITE_ROW) { /* Either an error, or no such table. */ goto rbuTableType_end } if Xsqlite3_column_int(tls, (*(*[4]uintptr)(unsafe.Pointer(bp)))[0], 0) != 0 { *(*int32)(unsafe.Pointer(peType)) = int32(RBU_PK_VTAB) /* virtual table */ goto rbuTableType_end } *(*int32)(unsafe.Pointer(piTnum)) = Xsqlite3_column_int(tls, (*(*[4]uintptr)(unsafe.Pointer(bp)))[0], int32(1)) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+1*4, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29196, libc.VaList(bp+24, zTab))) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != 0 { goto rbuTableType_end } for Xsqlite3_step(tls, (*(*[4]uintptr)(unsafe.Pointer(bp)))[int32(1)]) == int32(SQLITE_ROW) { zOrig = Xsqlite3_column_text(tls, (*(*[4]uintptr)(unsafe.Pointer(bp)))[int32(1)], int32(3)) zIdx = Xsqlite3_column_text(tls, (*(*[4]uintptr)(unsafe.Pointer(bp)))[int32(1)], int32(1)) if zOrig != 0 && zIdx != 0 && int32(*(*Tu8)(unsafe.Pointer(zOrig))) == int32('p') { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+2*4, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29217, libc.VaList(bp+24, zIdx))) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if Xsqlite3_step(tls, (*(*[4]uintptr)(unsafe.Pointer(bp)))[int32(2)]) == int32(SQLITE_ROW) { *(*int32)(unsafe.Pointer(piPk)) = Xsqlite3_column_int(tls, (*(*[4]uintptr)(unsafe.Pointer(bp)))[int32(2)], 0) *(*int32)(unsafe.Pointer(peType)) = int32(RBU_PK_EXTERNAL) } else { *(*int32)(unsafe.Pointer(peType)) = int32(RBU_PK_WITHOUT_ROWID) } } goto rbuTableType_end } } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+3*4, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29268, libc.VaList(bp+24, zTab))) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { for Xsqlite3_step(tls, (*(*[4]uintptr)(unsafe.Pointer(bp)))[int32(3)]) == int32(SQLITE_ROW) { if Xsqlite3_column_int(tls, (*(*[4]uintptr)(unsafe.Pointer(bp)))[int32(3)], int32(5)) > 0 { *(*int32)(unsafe.Pointer(peType)) = int32(RBU_PK_IPK) /* explicit IPK column */ goto rbuTableType_end } } *(*int32)(unsafe.Pointer(peType)) = int32(RBU_PK_NONE) } goto rbuTableType_end rbuTableType_end: ; i = uint32(0) for { if !(i < libc.Uint32FromInt64(16)/libc.Uint32FromInt64(4)) { break } _rbuFinalize(tls, p, (*(*[4]uintptr)(unsafe.Pointer(bp)))[i]) goto _1 _1: ; i++ } } // C documentation // // /* // ** This is a helper function for rbuObjIterCacheTableInfo(). It populates // ** the pIter->abIndexed[] array. // */ func _rbuObjIterCacheIndexedCols(tls *libc.TLS, p uintptr, pIter uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var bIndex, bPartial, iCid int32 var zIdx uintptr var _ /* pList at bp+0 */ uintptr var _ /* pXInfo at bp+4 */ uintptr _, _, _, _ = bIndex, bPartial, iCid, zIdx *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) bIndex = 0 if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { libc.Xmemcpy(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk, uint32(1)*uint32((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol)) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29289, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl))) } (*TRbuObjIter)(unsafe.Pointer(pIter)).FnIndex = 0 for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { zIdx = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) bPartial = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(4)) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) if zIdx == uintptr(0) { break } if bPartial != 0 { libc.Xmemset(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed, int32(0x01), uint32(1)*uint32((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol)) } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+4, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29317, libc.VaList(bp+16, zIdx))) for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) { iCid = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), int32(1)) if iCid >= 0 { *(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed + uintptr(iCid))) = uint8(1) } if iCid == -int32(2) { libc.Xmemset(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed, int32(0x01), uint32(1)*uint32((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol)) } } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp + 4))) bIndex = int32(1) (*TRbuObjIter)(unsafe.Pointer(pIter)).FnIndex++ } if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_WITHOUT_ROWID) { /* "PRAGMA index_list" includes the main PK b-tree */ (*TRbuObjIter)(unsafe.Pointer(pIter)).FnIndex-- } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp))) if bIndex == 0 { (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed = uintptr(0) } } // C documentation // // /* // ** If they are not already populated, populate the pIter->azTblCol[], // ** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to // ** the table (not index) that the iterator currently points to. // ** // ** Return SQLITE_OK if successful, or an SQLite error code otherwise. If // ** an error does occur, an error code and error message are also left in // ** the RBU handle. // */ func _rbuObjIterCacheTableInfo(tls *libc.TLS, p uintptr, pIter uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var bNotNull, bRbuRowid, i, iOrder, iPk, nCol, t, v2 int32 var t1, zCopy, zName, zName1, zType, v3, v4 uintptr var _ /* iTnum at bp+4 */ int32 var _ /* pStmt at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bNotNull, bRbuRowid, i, iOrder, iPk, nCol, t, t1, zCopy, zName, zName1, zType, v2, v3, v4 if (*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol == uintptr(0) { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) nCol = 0 /* for() loop iterator variable */ bRbuRowid = 0 /* If input table has column "rbu_rowid" */ iOrder = 0 *(*int32)(unsafe.Pointer(bp + 4)) = 0 /* Figure out the type of table this step will deal with. */ _ = libc.Int32FromInt32(0) _rbuTableType(tls, p, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, pIter+36, bp+4, pIter+60) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == RBU_PK_NOTABLE { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR) (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+19904, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl)) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != 0 { return (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc } if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx == uintptr(0) { (*TRbuObjIter)(unsafe.Pointer(pIter)).FiTnum = *(*int32)(unsafe.Pointer(bp + 4)) } _ = libc.Int32FromInt32(0) /* Populate the azTblCol[] and nTblCol variables based on the columns ** of the input table. Ignore any input table columns that begin with ** "rbu_". */ (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29346, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl))) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { nCol = Xsqlite3_column_count(tls, *(*uintptr)(unsafe.Pointer(bp))) _rbuAllocateIterArrays(tls, p, pIter, nCol) } i = 0 for { if !((*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && i < nCol) { break } zName = Xsqlite3_column_name(tls, *(*uintptr)(unsafe.Pointer(bp)), i) if Xsqlite3_strnicmp(tls, __ccgo_ts+29365, zName, int32(4)) != 0 { zCopy = _rbuStrndup(tls, zName, p+32) *(*int32)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol)*4)) = (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol v3 = pIter + 8 v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(v2)*4)) = zCopy } else { if 0 == Xsqlite3_stricmp(tls, __ccgo_ts+29370, zName) { bRbuRowid = int32(1) } } goto _1 _1: ; i++ } Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0)) == 0 && bRbuRowid != libc.BoolInt32((*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_VTAB) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE)) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR) if bRbuRowid != 0 { v4 = __ccgo_ts + 29380 } else { v4 = __ccgo_ts + 29393 } (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+29402, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, v4)) } /* Check that all non-HIDDEN columns in the destination table are also ** present in the input table. Populate the abTblPk[], azTblType[] and ** aiTblOrder[] arrays at the same time. */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29431, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl))) } for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { zName1 = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) if zName1 == uintptr(0) { break } /* An OOM - finalize() below returns S_NOMEM */ i = iOrder for { if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) { break } if 0 == libc.Xstrcmp(tls, zName1, *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4))) { break } goto _5 _5: ; i++ } if i == (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR) (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+29453, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, zName1)) } else { iPk = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(5)) bNotNull = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(3)) zType = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(2)) if i != iOrder { t = *(*int32)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(i)*4)) *(*int32)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(i)*4)) = *(*int32)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(iOrder)*4)) *(*int32)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(iOrder)*4)) = t t1 = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)) *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)) = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iOrder)*4)) *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iOrder)*4)) = t1 } *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(iOrder)*4)) = _rbuStrndup(tls, zType, p+32) _ = libc.Int32FromInt32(0) *(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(iOrder))) = uint8(iPk) *(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabNotNull + uintptr(iOrder))) = libc.BoolUint8(uint8(bNotNull) != 0 || iPk != 0) iOrder++ } } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp))) _rbuObjIterCacheIndexedCols(tls, p, pIter) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } return (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc } // C documentation // // /* // ** This function constructs and returns a pointer to a nul-terminated // ** string containing some SQL clause or list based on one or more of the // ** column names currently stored in the pIter->azTblCol[] array. // */ func _rbuObjIterGetCollist(tls *libc.TLS, p uintptr, pIter uintptr) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var i int32 var z, zList, zSep uintptr _, _, _, _ = i, z, zList, zSep zList = uintptr(0) zSep = __ccgo_ts + 1648 i = 0 for { if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) { break } z = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)) zList = _rbuMPrintf(tls, p, __ccgo_ts+29480, libc.VaList(bp+8, zList, zSep, z)) zSep = __ccgo_ts + 14990 goto _1 _1: ; i++ } return zList } // C documentation // // /* // ** Return a comma separated list of the quoted PRIMARY KEY column names, // ** in order, for the current table. Before each column name, add the text // ** zPre. After each column name, add the zPost text. Use zSeparator as // ** the separator text (usually ", "). // */ func _rbuObjIterGetPkList(tls *libc.TLS, p uintptr, pIter uintptr, zPre uintptr, zSeparator uintptr, zPost uintptr) (r uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var i, iPk int32 var zCol, zRet, zSep uintptr _, _, _, _, _ = i, iPk, zCol, zRet, zSep iPk = int32(1) zRet = uintptr(0) zSep = __ccgo_ts + 1648 for int32(1) != 0 { i = 0 for { if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) { break } if int32(*(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i)))) == iPk { zCol = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)) zRet = _rbuMPrintf(tls, p, __ccgo_ts+29489, libc.VaList(bp+8, zRet, zSep, zPre, zCol, zPost)) zSep = zSeparator break } goto _1 _1: ; i++ } if i == (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol { break } iPk++ } return zRet } // C documentation // // /* // ** This function is called as part of restarting an RBU vacuum within // ** stage 1 of the process (while the *-oal file is being built) while // ** updating a table (not an index). The table may be a rowid table or // ** a WITHOUT ROWID table. It queries the target database to find the // ** largest key that has already been written to the target table and // ** constructs a WHERE clause that can be used to extract the remaining // ** rows from the source table. For a rowid table, the WHERE clause // ** is of the form: // ** // ** "WHERE _rowid_ > ?" // ** // ** and for WITHOUT ROWID tables: // ** // ** "WHERE (key1, key2) > (?, ?)" // ** // ** Instead of "?" placeholders, the actual WHERE clauses created by // ** this function contain literal SQL values. // */ func _rbuVacuumTableStart(tls *libc.TLS, p uintptr, pIter uintptr, bRowid int32, zWrite uintptr) (r uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var iMax Tsqlite3_int64 var zList, zOrder, zRet, zSelect, zVal uintptr var _ /* pMax at bp+0 */ uintptr _, _, _, _, _, _ = iMax, zList, zOrder, zRet, zSelect, zVal *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) zRet = uintptr(0) if bRowid != 0 { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29502, libc.VaList(bp+16, zWrite, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl))) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { iMax = Xsqlite3_column_int64(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) zRet = _rbuMPrintf(tls, p, __ccgo_ts+29534, libc.VaList(bp+16, iMax)) } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp))) } else { zOrder = _rbuObjIterGetPkList(tls, p, pIter, __ccgo_ts+1648, __ccgo_ts+14990, __ccgo_ts+29557) zSelect = _rbuObjIterGetPkList(tls, p, pIter, __ccgo_ts+29563, __ccgo_ts+29570, __ccgo_ts+5106) zList = _rbuObjIterGetPkList(tls, p, pIter, __ccgo_ts+1648, __ccgo_ts+14990, __ccgo_ts+1648) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29578, libc.VaList(bp+16, zSelect, zWrite, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, zOrder))) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { zVal = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) zRet = _rbuMPrintf(tls, p, __ccgo_ts+29620, libc.VaList(bp+16, zList, zVal)) } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp))) } Xsqlite3_free(tls, zOrder) Xsqlite3_free(tls, zSelect) Xsqlite3_free(tls, zList) } return zRet } // C documentation // // /* // ** This function is called as part of restating an RBU vacuum when the // ** current operation is writing content to an index. If possible, it // ** queries the target index b-tree for the largest key already written to // ** it, then composes and returns an expression that can be used in a WHERE // ** clause to select the remaining required rows from the source table. // ** It is only possible to return such an expression if: // ** // ** * The index contains no DESC columns, and // ** * The last key written to the index before the operation was // ** suspended does not contain any NULL values. // ** // ** The expression is of the form: // ** // ** (index-field1, index-field2, ...) > (?, ?, ...) // ** // ** except that the "?" placeholders are replaced with literal values. // ** // ** If the expression cannot be created, NULL is returned. In this case, // ** the caller has to use an OFFSET clause to extract only the required // ** rows from the sourct table, just as it does for an RBU update operation. // */ func _rbuVacuumIndexStart(tls *libc.TLS, p uintptr, pIter uintptr) (r uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var bFailed, i, iCid, iCol int32 var zCol, zCollate, zLhs, zOrder, zQuoted, zRet, zSelect, zSep, zVector uintptr var _ /* pSel at bp+4 */ uintptr var _ /* pXInfo at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _ = bFailed, i, iCid, iCol, zCol, zCollate, zLhs, zOrder, zQuoted, zRet, zSelect, zSep, zVector zOrder = uintptr(0) zLhs = uintptr(0) zSelect = uintptr(0) zVector = uintptr(0) zRet = uintptr(0) bFailed = 0 zSep = __ccgo_ts + 1648 iCol = 0 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29317, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx))) for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { iCid = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) zCollate = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(4)) if Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(3)) != 0 { bFailed = int32(1) break } if iCid < 0 { if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_IPK) { i = 0 for { if !(int32(*(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i)))) == 0) { break } goto _1 _1: ; i++ } _ = libc.Int32FromInt32(0) zCol = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)) } else { zCol = __ccgo_ts + 29640 } } else { zCol = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iCid)*4)) } zLhs = _rbuMPrintf(tls, p, __ccgo_ts+29648, libc.VaList(bp+16, zLhs, zSep, zCol, zCollate)) zOrder = _rbuMPrintf(tls, p, __ccgo_ts+29669, libc.VaList(bp+16, zOrder, zSep, iCol, zCol, zCollate)) zSelect = _rbuMPrintf(tls, p, __ccgo_ts+29705, libc.VaList(bp+16, zSelect, zSep, iCol, zCol)) zSep = __ccgo_ts + 14990 iCol++ } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp))) if bFailed != 0 { goto index_start_out } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+4, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29732, libc.VaList(bp+16, zSelect, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, zOrder))) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) { zSep = __ccgo_ts + 1648 iCol = 0 for { if !(iCol < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol) { break } zQuoted = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), iCol) if zQuoted == uintptr(0) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) } else { if int32(*(*uint8)(unsafe.Pointer(zQuoted))) == int32('N') { bFailed = int32(1) break } } zVector = _rbuMPrintf(tls, p, __ccgo_ts+29780, libc.VaList(bp+16, zVector, zSep, zQuoted)) zSep = __ccgo_ts + 14990 goto _2 _2: ; iCol++ } if !(bFailed != 0) { zRet = _rbuMPrintf(tls, p, __ccgo_ts+29787, libc.VaList(bp+16, zLhs, zVector)) } } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp + 4))) } goto index_start_out index_start_out: ; Xsqlite3_free(tls, zOrder) Xsqlite3_free(tls, zSelect) Xsqlite3_free(tls, zVector) Xsqlite3_free(tls, zLhs) return zRet } // C documentation // // /* // ** This function is used to create a SELECT list (the list of SQL // ** expressions that follows a SELECT keyword) for a SELECT statement // ** used to read from an data_xxx or rbu_tmp_xxx table while updating the // ** index object currently indicated by the iterator object passed as the // ** second argument. A "PRAGMA index_xinfo = " statement is used // ** to obtain the required information. // ** // ** If the index is of the following form: // ** // ** CREATE INDEX i1 ON t1(c, b COLLATE nocase); // ** // ** and "t1" is a table with an explicit INTEGER PRIMARY KEY column // ** "ipk", the returned string is: // ** // ** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'" // ** // ** As well as the returned string, three other malloc'd strings are // ** returned via output parameters. As follows: // ** // ** pzImposterCols: ... // ** pzImposterPk: ... // ** pzWhere: ... // */ func _rbuObjIterGetIndexCols(tls *libc.TLS, p uintptr, pIter uintptr, pzImposterCols uintptr, pzImposterPk uintptr, pzWhere uintptr, pnBind uintptr) (r uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var bDesc, i, iCid, iSeq, nBind, rc, rc2 int32 var zAnd, zCol, zCollate, zCom, zImpCols, zImpPK, zOrder, zRet, zType, zWhere, v2 uintptr var _ /* pXInfo at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bDesc, i, iCid, iSeq, nBind, rc, rc2, zAnd, zCol, zCollate, zCom, zImpCols, zImpPK, zOrder, zRet, zType, zWhere, v2 rc = (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc /* sqlite3_finalize() return code */ zRet = uintptr(0) /* String to return */ zImpCols = uintptr(0) /* String to return via *pzImposterCols */ zImpPK = uintptr(0) /* String to return via *pzImposterPK */ zWhere = uintptr(0) /* String to return via *pzWhere */ nBind = 0 /* Value to return via *pnBind */ zCom = __ccgo_ts + 1648 /* Set to ", " later on */ zAnd = __ccgo_ts + 1648 /* Set to " AND " later on */ *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* PRAGMA index_xinfo = ? */ if rc == SQLITE_OK { _ = libc.Int32FromInt32(0) rc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29317, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx))) } for rc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { iCid = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) bDesc = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(3)) zCollate = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(4)) zCol = uintptr(0) if iCid == -int32(2) { iSeq = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) zRet = Xsqlite3_mprintf(tls, __ccgo_ts+29799, libc.VaList(bp+16, zRet, zCom, (*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iSeq)*8))).FnSpan, (*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iSeq)*8))).FzSpan, zCollate)) zType = __ccgo_ts + 1648 } else { if iCid < 0 { /* An integer primary key. If the table has an explicit IPK, use ** its name. Otherwise, use "rbu_rowid". */ if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_IPK) { i = 0 for { if !(int32(*(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i)))) == 0) { break } goto _1 _1: ; i++ } _ = libc.Int32FromInt32(0) zCol = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)) } else { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { zCol = __ccgo_ts + 29640 } else { zCol = __ccgo_ts + 29370 } } zType = __ccgo_ts + 1136 } else { zCol = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iCid)*4)) zType = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(iCid)*4)) } zRet = Xsqlite3_mprintf(tls, __ccgo_ts+29821, libc.VaList(bp+16, zRet, zCom, zCol, zCollate)) } if (*TRbuObjIter)(unsafe.Pointer(pIter)).FbUnique == 0 || Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(5)) != 0 { if bDesc != 0 { v2 = __ccgo_ts + 29557 } else { v2 = __ccgo_ts + 1648 } zOrder = v2 zImpPK = Xsqlite3_mprintf(tls, __ccgo_ts+29841, libc.VaList(bp+16, zImpPK, zCom, nBind, zCol, zOrder)) } zImpCols = Xsqlite3_mprintf(tls, __ccgo_ts+29862, libc.VaList(bp+16, zImpCols, zCom, nBind, zCol, zType, zCollate)) zWhere = Xsqlite3_mprintf(tls, __ccgo_ts+29895, libc.VaList(bp+16, zWhere, zAnd, nBind, zCol)) if zRet == uintptr(0) || zImpPK == uintptr(0) || zImpCols == uintptr(0) || zWhere == uintptr(0) { rc = int32(SQLITE_NOMEM) } zCom = __ccgo_ts + 14990 zAnd = __ccgo_ts + 22013 nBind++ } rc2 = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) if rc == SQLITE_OK { rc = rc2 } if rc != SQLITE_OK { Xsqlite3_free(tls, zRet) Xsqlite3_free(tls, zImpCols) Xsqlite3_free(tls, zImpPK) Xsqlite3_free(tls, zWhere) zRet = uintptr(0) zImpCols = uintptr(0) zImpPK = uintptr(0) zWhere = uintptr(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc } *(*uintptr)(unsafe.Pointer(pzImposterCols)) = zImpCols *(*uintptr)(unsafe.Pointer(pzImposterPk)) = zImpPK *(*uintptr)(unsafe.Pointer(pzWhere)) = zWhere *(*int32)(unsafe.Pointer(pnBind)) = nBind return zRet } // C documentation // // /* // ** Assuming the current table columns are "a", "b" and "c", and the zObj // ** paramter is passed "old", return a string of the form: // ** // ** "old.a, old.b, old.b" // ** // ** With the column names escaped. // ** // ** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append // ** the text ", old._rowid_" to the returned value. // */ func _rbuObjIterGetOldlist(tls *libc.TLS, p uintptr, pIter uintptr, zObj uintptr) (r uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var i int32 var zCol, zList, zS uintptr _, _, _, _ = i, zCol, zList, zS zList = uintptr(0) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed != 0 { zS = __ccgo_ts + 1648 i = 0 for { if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) { break } if *(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed + uintptr(i))) != 0 { zCol = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)) zList = Xsqlite3_mprintf(tls, __ccgo_ts+29919, libc.VaList(bp+8, zList, zS, zObj, zCol)) } else { zList = Xsqlite3_mprintf(tls, __ccgo_ts+29931, libc.VaList(bp+8, zList, zS)) } zS = __ccgo_ts + 14990 if zList == uintptr(0) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) break } goto _1 _1: ; i++ } /* For a table with implicit rowids, append "old._rowid_" to the list. */ if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) { zList = _rbuMPrintf(tls, p, __ccgo_ts+29940, libc.VaList(bp+8, zList, zObj)) } } return zList } // C documentation // // /* // ** Return an expression that can be used in a WHERE clause to match the // ** primary key of the current table. For example, if the table is: // ** // ** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c)); // ** // ** Return the string: // ** // ** "b = ?1 AND c = ?2" // */ func _rbuObjIterGetWhere(tls *libc.TLS, p uintptr, pIter uintptr) (r uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var i, i1 int32 var zCol, zList, zSep, zSep1 uintptr _, _, _, _, _, _ = i, i1, zCol, zList, zSep, zSep1 zList = uintptr(0) if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_VTAB) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) { zList = _rbuMPrintf(tls, p, __ccgo_ts+29955, libc.VaList(bp+8, (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol+int32(1))) } else { if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) { zSep = __ccgo_ts + 1648 i = 0 for { if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) { break } if *(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i))) != 0 { zList = _rbuMPrintf(tls, p, __ccgo_ts+29969, libc.VaList(bp+8, zList, zSep, i, i+int32(1))) zSep = __ccgo_ts + 22013 } goto _1 _1: ; i++ } zList = _rbuMPrintf(tls, p, __ccgo_ts+29981, libc.VaList(bp+8, zList)) } else { zSep1 = __ccgo_ts + 1648 i1 = 0 for { if !(i1 < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) { break } if *(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i1))) != 0 { zCol = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i1)*4)) zList = _rbuMPrintf(tls, p, __ccgo_ts+30031, libc.VaList(bp+8, zList, zSep1, zCol, i1+int32(1))) zSep1 = __ccgo_ts + 22013 } goto _2 _2: ; i1++ } } } return zList } // C documentation // // /* // ** The SELECT statement iterating through the keys for the current object // ** (p->objiter.pSelect) currently points to a valid row. However, there // ** is something wrong with the rbu_control value in the rbu_control value // ** stored in the (p->nCol+1)'th column. Set the error code and error message // ** of the RBU handle to something reflecting this. // */ func _rbuBadControlError(tls *libc.TLS, p uintptr) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR) (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+30044, 0) } // C documentation // // /* // ** Return a nul-terminated string containing the comma separated list of // ** assignments that should be included following the "SET" keyword of // ** an UPDATE statement used to update the table object that the iterator // ** passed as the second argument currently points to if the rbu_control // ** column of the data_xxx table entry is set to zMask. // ** // ** The memory for the returned string is obtained from sqlite3_malloc(). // ** It is the responsibility of the caller to eventually free it using // ** sqlite3_free(). // ** // ** If an OOM error is encountered when allocating space for the new // ** string, an error code is left in the rbu handle passed as the first // ** argument and NULL is returned. Or, if an error has already occurred // ** when this function is called, NULL is returned immediately, without // ** attempting the allocation or modifying the stored error code. // */ func _rbuObjIterGetSetlist(tls *libc.TLS, p uintptr, pIter uintptr, zMask uintptr) (r uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var c uint8 var i int32 var zList, zSep uintptr _, _, _, _ = c, i, zList, zSep zList = uintptr(0) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if int32(libc.Xstrlen(tls, zMask)) != (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol { _rbuBadControlError(tls, p) } else { zSep = __ccgo_ts + 1648 i = 0 for { if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) { break } c = *(*uint8)(unsafe.Pointer(zMask + uintptr(*(*int32)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(i)*4))))) if int32(c) == int32('x') { zList = _rbuMPrintf(tls, p, __ccgo_ts+30031, libc.VaList(bp+8, zList, zSep, *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)), i+int32(1))) zSep = __ccgo_ts + 14990 } else { if int32(c) == int32('d') { zList = _rbuMPrintf(tls, p, __ccgo_ts+30070, libc.VaList(bp+8, zList, zSep, *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)), *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)), i+int32(1))) zSep = __ccgo_ts + 14990 } else { if int32(c) == int32('f') { zList = _rbuMPrintf(tls, p, __ccgo_ts+30100, libc.VaList(bp+8, zList, zSep, *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)), *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)), i+int32(1))) zSep = __ccgo_ts + 14990 } } } goto _1 _1: ; i++ } } } return zList } // C documentation // // /* // ** Return a nul-terminated string consisting of nByte comma separated // ** "?" expressions. For example, if nByte is 3, return a pointer to // ** a buffer containing the string "?,?,?". // ** // ** The memory for the returned string is obtained from sqlite3_malloc(). // ** It is the responsibility of the caller to eventually free it using // ** sqlite3_free(). // ** // ** If an OOM error is encountered when allocating space for the new // ** string, an error code is left in the rbu handle passed as the first // ** argument and NULL is returned. Or, if an error has already occurred // ** when this function is called, NULL is returned immediately, without // ** attempting the allocation or modifying the stored error code. // */ func _rbuObjIterGetBindlist(tls *libc.TLS, p uintptr, nBind int32) (r uintptr) { var i, v2 int32 var nByte Tsqlite3_int64 var zRet uintptr _, _, _, _ = i, nByte, zRet, v2 zRet = uintptr(0) nByte = int64(2)*int64(nBind) + int64(1) zRet = _rbuMalloc(tls, p, nByte) if zRet != 0 { i = 0 for { if !(i < nBind) { break } *(*uint8)(unsafe.Pointer(zRet + uintptr(i*int32(2)))) = uint8('?') if i+int32(1) == nBind { v2 = int32('\000') } else { v2 = int32(',') } *(*uint8)(unsafe.Pointer(zRet + uintptr(i*int32(2)+int32(1)))) = uint8(v2) goto _1 _1: ; i++ } } return zRet } // C documentation // // /* // ** The iterator currently points to a table (not index) of type // ** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY // ** declaration for the corresponding imposter table. For example, // ** if the iterator points to a table created as: // ** // ** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID // ** // ** this function returns: // ** // ** PRIMARY KEY("b", "a" DESC) // */ func _rbuWithoutRowidPK(tls *libc.TLS, p uintptr, pIter uintptr) (r uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var z, zCol, zDesc, zIdx, zOrig, zSep, v1 uintptr var _ /* pXInfo at bp+4 */ uintptr var _ /* pXList at bp+0 */ uintptr _, _, _, _, _, _, _ = z, zCol, zDesc, zIdx, zOrig, zSep, v1 z = uintptr(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { zSep = __ccgo_ts + 30137 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* PRAGMA index_list = (pIter->zTbl) */ *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) /* PRAGMA index_xinfo = */ (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29289, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl))) for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { zOrig = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(3)) if zOrig != 0 && libc.Xstrcmp(tls, zOrig, __ccgo_ts+16582) == 0 { zIdx = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) if zIdx != 0 { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+4, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29317, libc.VaList(bp+16, zIdx))) } break } } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp))) for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) { if Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), int32(5)) != 0 { /* int iCid = sqlite3_column_int(pXInfo, 0); */ zCol = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), int32(2)) if Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), int32(3)) != 0 { v1 = __ccgo_ts + 29557 } else { v1 = __ccgo_ts + 1648 } zDesc = v1 z = _rbuMPrintf(tls, p, __ccgo_ts+30150, libc.VaList(bp+16, z, zSep, zCol, zDesc)) zSep = __ccgo_ts + 14990 } } z = _rbuMPrintf(tls, p, __ccgo_ts+30161, libc.VaList(bp+16, z)) _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp + 4))) } return z } // C documentation // // /* // ** This function creates the second imposter table used when writing to // ** a table b-tree where the table has an external primary key. If the // ** iterator passed as the second argument does not currently point to // ** a table (not index) with an external primary key, this function is a // ** no-op. // ** // ** Assuming the iterator does point to a table with an external PK, this // ** function creates a WITHOUT ROWID imposter table named "rbu_imposter2" // ** used to access that PK index. For example, if the target table is // ** declared as follows: // ** // ** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c)); // ** // ** then the imposter table schema is: // ** // ** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID; // ** // */ func _rbuCreateImposterTable2(tls *libc.TLS, p uintptr, pIter uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var bDesc, bKey, iCid, tnum int32 var zCollate, zCols, zComma, zIdx, zPk, v1 uintptr var _ /* pQuery at bp+0 */ uintptr var _ /* pXInfo at bp+4 */ uintptr _, _, _, _, _, _, _, _, _, _ = bDesc, bKey, iCid, tnum, zCollate, zCols, zComma, zIdx, zPk, v1 if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) { tnum = (*TRbuObjIter)(unsafe.Pointer(pIter)).FiPkTnum /* Root page of PK index */ *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* SELECT name ... WHERE rootpage = $tnum */ zIdx = uintptr(0) /* Name of PK index */ *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) /* PRAGMA main.index_xinfo = $zIdx */ zComma = __ccgo_ts + 1648 zCols = uintptr(0) /* Used to build up list of table cols */ zPk = uintptr(0) /* Used to build up table PK declaration */ /* Figure out the name of the primary key index for the current table. ** This is needed for the argument to "PRAGMA index_xinfo". Set ** zIdx to point to a nul-terminated string containing this name. */ (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, __ccgo_ts+30165) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { Xsqlite3_bind_int(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1), tnum) if int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { zIdx = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) } } if zIdx != 0 { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+4, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+29317, libc.VaList(bp+16, zIdx))) } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp))) for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) { bKey = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), int32(5)) if bKey != 0 { iCid = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), int32(1)) bDesc = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), int32(3)) zCollate = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), int32(4)) zCols = _rbuMPrintf(tls, p, __ccgo_ts+30215, libc.VaList(bp+16, zCols, zComma, iCid, *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(iCid)*4)), zCollate)) if bDesc != 0 { v1 = __ccgo_ts + 29557 } else { v1 = __ccgo_ts + 1648 } zPk = _rbuMPrintf(tls, p, __ccgo_ts+30237, libc.VaList(bp+16, zPk, zComma, iCid, v1)) zComma = __ccgo_ts + 14990 } } zCols = _rbuMPrintf(tls, p, __ccgo_ts+30247, libc.VaList(bp+16, zCols)) _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp + 4))) Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+16, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6585, int32(1), tnum)) _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+30262, libc.VaList(bp+16, zCols, zPk)) Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+16, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6585, 0, 0)) } } // C documentation // // /* // ** If an error has already occurred when this function is called, it // ** immediately returns zero (without doing any work). Or, if an error // ** occurs during the execution of this function, it sets the error code // ** in the sqlite3rbu object indicated by the first argument and returns // ** zero. // ** // ** The iterator passed as the second argument is guaranteed to point to // ** a table (not an index) when this function is called. This function // ** attempts to create any imposter table required to write to the main // ** table b-tree of the table before returning. Non-zero is returned if // ** an imposter table are created, or zero otherwise. // ** // ** An imposter table is required in all cases except RBU_PK_VTAB. Only // ** virtual tables are written to directly. The imposter table has the // ** same schema as the actual target table (less any UNIQUE constraints). // ** More precisely, the "same schema" means the same columns, types, // ** collation sequences. For tables that do not have an external PRIMARY // ** KEY, it also means the same PRIMARY KEY declaration. // */ func _rbuCreateImposterTable(tls *libc.TLS, p uintptr, pIter uintptr) { bp := tls.Alloc(80) defer tls.Free(80) var iCol, tnum int32 var zCol, zComma, zPk, zPk1, zSql, v2, v3 uintptr var _ /* zColl at bp+0 */ uintptr _, _, _, _, _, _, _, _, _ = iCol, tnum, zCol, zComma, zPk, zPk1, zSql, v2, v3 if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType != int32(RBU_PK_VTAB) { tnum = (*TRbuObjIter)(unsafe.Pointer(pIter)).FiTnum zComma = __ccgo_ts + 1648 zSql = uintptr(0) Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+16, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6585, 0, int32(1))) iCol = 0 for { if !((*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && iCol < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) { break } zPk = __ccgo_ts + 1648 zCol = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iCol)*4)) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_table_column_metadata(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6585, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, zCol, uintptr(0), bp, uintptr(0), uintptr(0), uintptr(0)) if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_IPK) && *(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(iCol))) != 0 { /* If the target table column is an "INTEGER PRIMARY KEY", add ** "PRIMARY KEY" to the imposter table column declaration. */ zPk = __ccgo_ts + 30324 } if *(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabNotNull + uintptr(iCol))) != 0 { v2 = __ccgo_ts + 30337 } else { v2 = __ccgo_ts + 1648 } zSql = _rbuMPrintf(tls, p, __ccgo_ts+30347, libc.VaList(bp+16, zSql, zComma, zCol, *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(iCol)*4)), zPk, *(*uintptr)(unsafe.Pointer(bp)), v2)) zComma = __ccgo_ts + 14990 goto _1 _1: ; iCol++ } if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_WITHOUT_ROWID) { zPk1 = _rbuWithoutRowidPK(tls, p, pIter) if zPk1 != 0 { zSql = _rbuMPrintf(tls, p, __ccgo_ts+30374, libc.VaList(bp+16, zSql, zPk1)) } } Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+16, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6585, int32(1), tnum)) if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_WITHOUT_ROWID) { v3 = __ccgo_ts + 30381 } else { v3 = __ccgo_ts + 1648 } _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+30396, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, zSql, v3)) Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+16, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6585, 0, 0)) } } // C documentation // // /* // ** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table. // ** Specifically a statement of the form: // ** // ** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...); // ** // ** The number of bound variables is equal to the number of columns in // ** the target table, plus one (for the rbu_control column), plus one more // ** (for the rbu_rowid column) if the target table is an implicit IPK or // ** virtual table. // */ func _rbuObjIterPrepareTmpInsert(tls *libc.TLS, p uintptr, pIter uintptr, zCollist uintptr, zRbuRowid uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var bRbuRowid int32 var zBind uintptr _, _ = bRbuRowid, zBind bRbuRowid = libc.BoolInt32((*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE)) zBind = _rbuObjIterGetBindlist(tls, p, (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol+int32(1)+bRbuRowid) if zBind != 0 { _ = libc.Int32FromInt32(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, pIter+88, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+30428, libc.VaList(bp+8, p+24, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, zCollist, zRbuRowid, zBind))) } } func _rbuTmpInsertFunc(tls *libc.TLS, pCtx uintptr, nVal int32, apVal uintptr) { var i, rc int32 var p uintptr _, _, _ = i, p, rc p = Xsqlite3_user_data(tls, pCtx) rc = SQLITE_OK _ = libc.Int32FromInt32(0) if Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(apVal))) != 0 { *(*Ti64)(unsafe.Pointer(p + 176)) += int64((*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FnIndex) } i = 0 for { if !(rc == SQLITE_OK && i < nVal) { break } rc = Xsqlite3_bind_value(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FpTmpInsert, i+int32(1), *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*4))) goto _1 _1: ; i++ } if rc == SQLITE_OK { Xsqlite3_step(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FpTmpInsert) rc = Xsqlite3_reset(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FpTmpInsert) } if rc != SQLITE_OK { Xsqlite3_result_error_code(tls, pCtx, rc) } } func _rbuObjIterGetIndexWhere(tls *libc.TLS, p uintptr, pIter uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var aIdxCol, zRet, zSql, v1 uintptr var c uint8 var i, iIdxCol, nIdxAlloc, nParen, nSpan, nSpan1, rc2, v3, v4 int32 var _ /* pStmt at bp+0 */ uintptr var _ /* rc at bp+4 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aIdxCol, c, i, iIdxCol, nIdxAlloc, nParen, nSpan, nSpan1, rc2, zRet, zSql, v1, v3, v4 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 4)) = (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc zRet = uintptr(0) _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 4)) = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, __ccgo_ts+30485) } if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 4)) = Xsqlite3_bind_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1), (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx, -int32(1), libc.UintptrFromInt32(0)) if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { zSql = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) if zSql != 0 { v1 = _rbuStrndup(tls, zSql, bp+4) zSql = v1 (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdxSql = v1 } if zSql != 0 { nParen = 0 iIdxCol = 0 nIdxAlloc = 0 i = 0 for { if !(*(*uint8)(unsafe.Pointer(zSql + uintptr(i))) != 0) { break } c = *(*uint8)(unsafe.Pointer(zSql + uintptr(i))) /* If necessary, grow the pIter->aIdxCol[] array */ if iIdxCol == nIdxAlloc { aIdxCol = Xsqlite3_realloc(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol, int32(uint32(nIdxAlloc+libc.Int32FromInt32(16))*uint32(8))) if aIdxCol == uintptr(0) { *(*int32)(unsafe.Pointer(bp + 4)) = int32(SQLITE_NOMEM) break } (*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol = aIdxCol nIdxAlloc += int32(16) } if int32(c) == int32('(') { if nParen == 0 { _ = libc.Int32FromInt32(0) (*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol))).FzSpan = zSql + uintptr(i+int32(1)) } nParen++ } else { if int32(c) == int32(')') { nParen-- if nParen == 0 { nSpan = t__predefined_ptrdiff_t(zSql+uintptr(i)) - int32((*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iIdxCol)*8))).FzSpan) v3 = iIdxCol iIdxCol++ (*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(v3)*8))).FnSpan = nSpan i++ break } } else { if int32(c) == int32(',') && nParen == int32(1) { nSpan1 = t__predefined_ptrdiff_t(zSql+uintptr(i)) - int32((*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iIdxCol)*8))).FzSpan) v4 = iIdxCol iIdxCol++ (*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(v4)*8))).FnSpan = nSpan1 (*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iIdxCol)*8))).FzSpan = zSql + uintptr(i+int32(1)) } else { if int32(c) == int32('"') || int32(c) == int32('\'') || int32(c) == int32('`') { i++ for { if !(int32(1) != 0) { break } if int32(*(*uint8)(unsafe.Pointer(zSql + uintptr(i)))) == int32(c) { if int32(*(*uint8)(unsafe.Pointer(zSql + uintptr(i+int32(1))))) != int32(c) { break } i++ } goto _5 _5: ; i++ } } else { if int32(c) == int32('[') { i++ for { if !(int32(1) != 0) { break } if int32(*(*uint8)(unsafe.Pointer(zSql + uintptr(i)))) == int32(']') { break } goto _6 _6: ; i++ } } else { if int32(c) == int32('-') && int32(*(*uint8)(unsafe.Pointer(zSql + uintptr(i+int32(1))))) == int32('-') { i = i + int32(2) for { if !(*(*uint8)(unsafe.Pointer(zSql + uintptr(i))) != 0 && int32(*(*uint8)(unsafe.Pointer(zSql + uintptr(i)))) != int32('\n')) { break } goto _7 _7: ; i++ } if int32(*(*uint8)(unsafe.Pointer(zSql + uintptr(i)))) == int32('\000') { break } } else { if int32(c) == int32('/') && int32(*(*uint8)(unsafe.Pointer(zSql + uintptr(i+int32(1))))) == int32('*') { i = i + int32(2) for { if !(*(*uint8)(unsafe.Pointer(zSql + uintptr(i))) != 0 && (int32(*(*uint8)(unsafe.Pointer(zSql + uintptr(i)))) != int32('*') || int32(*(*uint8)(unsafe.Pointer(zSql + uintptr(i+int32(1))))) != int32('/'))) { break } goto _8 _8: ; i++ } if int32(*(*uint8)(unsafe.Pointer(zSql + uintptr(i)))) == int32('\000') { break } i++ } } } } } } } goto _2 _2: ; i++ } if *(*uint8)(unsafe.Pointer(zSql + uintptr(i))) != 0 { zRet = _rbuStrndup(tls, zSql+uintptr(i), bp+4) } (*TRbuObjIter)(unsafe.Pointer(pIter)).FnIdxCol = iIdxCol } } rc2 = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 4)) = rc2 } } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = *(*int32)(unsafe.Pointer(bp + 4)) return zRet } // C documentation // // /* // ** Ensure that the SQLite statement handles required to update the // ** target database object currently indicated by the iterator passed // ** as the second argument are available. // */ func _rbuObjIterPrepareAll(tls *libc.TLS, p uintptr, pIter uintptr, nOffset int32) (r int32) { bp := tls.Alloc(112) defer tls.Free(112) var bRbuRowid, tnum int32 var pz, zBind, zBindings, zCollist, zIdx, zLimit, zNewlist, zOldlist, zOrder, zPart, zRbuRowid, zRbuRowid1, zSql, zStart, zStart1, zTbl, zTbl1, zWhere1, zWrite, v1, v10, v2, v3, v4, v5, v6, v7, v8, v9 uintptr var _ /* nBind at bp+12 */ int32 var _ /* zImposterCols at bp+0 */ uintptr var _ /* zImposterPK at bp+4 */ uintptr var _ /* zWhere at bp+8 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bRbuRowid, pz, tnum, zBind, zBindings, zCollist, zIdx, zLimit, zNewlist, zOldlist, zOrder, zPart, zRbuRowid, zRbuRowid1, zSql, zStart, zStart1, zTbl, zTbl1, zWhere1, zWrite, v1, v10, v2, v3, v4, v5, v6, v7, v8, v9 _ = libc.Int32FromInt32(0) if (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect == uintptr(0) && _rbuObjIterCacheTableInfo(tls, p, pIter) == SQLITE_OK { tnum = (*TRbuObjIter)(unsafe.Pointer(pIter)).FiTnum zCollist = uintptr(0) /* List of indexed columns */ pz = p + 36 zIdx = (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx zLimit = uintptr(0) if nOffset != 0 { zLimit = Xsqlite3_mprintf(tls, __ccgo_ts+30551, libc.VaList(bp+24, nOffset)) if !(zLimit != 0) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) } } if zIdx != 0 { zTbl = (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Columns for imposter table */ *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) /* Primary key declaration for imposter */ *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) /* WHERE clause on PK columns */ zBind = uintptr(0) zPart = uintptr(0) *(*int32)(unsafe.Pointer(bp + 12)) = 0 _ = libc.Int32FromInt32(0) zPart = _rbuObjIterGetIndexWhere(tls, p, pIter) zCollist = _rbuObjIterGetIndexCols(tls, p, pIter, bp, bp+4, bp+8, bp+12) zBind = _rbuObjIterGetBindlist(tls, p, *(*int32)(unsafe.Pointer(bp + 12))) /* Create the imposter table used to write to this index. */ Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+24, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6585, 0, int32(1))) Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+24, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6585, int32(1), tnum)) _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+30571, libc.VaList(bp+24, zTbl, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 4)))) Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+24, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6585, 0, 0)) /* Create the statement to insert index entries */ (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol = *(*int32)(unsafe.Pointer(bp + 12)) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pIter+80, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+30636, libc.VaList(bp+24, zTbl, zBind))) } /* And to delete index entries */ if libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0)) == 0 && (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pIter+84, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+30672, libc.VaList(bp+24, zTbl, *(*uintptr)(unsafe.Pointer(bp + 8))))) } /* Create the SELECT statement to read keys in sorted order */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { zStart = uintptr(0) if nOffset != 0 { zStart = _rbuVacuumIndexStart(tls, p, pIter) if zStart != 0 { Xsqlite3_free(tls, zLimit) zLimit = uintptr(0) } } if zStart != 0 { if zPart != 0 { v2 = __ccgo_ts + 30706 } else { v2 = __ccgo_ts + 30710 } v1 = v2 } else { v1 = __ccgo_ts + 1648 } zSql = Xsqlite3_mprintf(tls, __ccgo_ts+30716, libc.VaList(bp+24, zCollist, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, zPart, v1, zStart, zCollist, zLimit)) Xsqlite3_free(tls, zStart) } else { if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) { zSql = Xsqlite3_mprintf(tls, __ccgo_ts+30777, libc.VaList(bp+24, zCollist, p+24, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, zPart, zCollist, zLimit)) } else { if zPart != 0 { v3 = __ccgo_ts + 30706 } else { v3 = __ccgo_ts + 30710 } zSql = Xsqlite3_mprintf(tls, __ccgo_ts+30838, libc.VaList(bp+24, zCollist, p+24, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, zPart, zCollist, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, zPart, v3, zCollist, zLimit)) } } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, pIter+76, pz, zSql) } else { Xsqlite3_free(tls, zSql) } } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp))) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) Xsqlite3_free(tls, zBind) Xsqlite3_free(tls, zPart) } else { bRbuRowid = libc.BoolInt32((*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_VTAB) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) && (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0)) zTbl1 = (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl /* Imposter table name */ zBindings = _rbuObjIterGetBindlist(tls, p, (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol+bRbuRowid) zWhere1 = _rbuObjIterGetWhere(tls, p, pIter) zOldlist = _rbuObjIterGetOldlist(tls, p, pIter, __ccgo_ts+6596) zNewlist = _rbuObjIterGetOldlist(tls, p, pIter, __ccgo_ts+6592) zCollist = _rbuObjIterGetCollist(tls, p, pIter) (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol = (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol /* Create the imposter table or tables (if required). */ _rbuCreateImposterTable(tls, p, pIter) _rbuCreateImposterTable2(tls, p, pIter) if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_VTAB) { v4 = __ccgo_ts + 1648 } else { v4 = __ccgo_ts + 30997 } zWrite = v4 /* Create the INSERT statement to write to the target PK b-tree */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if bRbuRowid != 0 { v5 = __ccgo_ts + 31006 } else { v5 = __ccgo_ts + 1648 } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pIter+80, pz, Xsqlite3_mprintf(tls, __ccgo_ts+31016, libc.VaList(bp+24, zWrite, zTbl1, zCollist, v5, zBindings))) } /* Create the DELETE statement to write to the target PK b-tree. ** Because it only performs INSERT operations, this is not required for ** an rbu vacuum handle. */ if libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0)) == 0 && (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pIter+84, pz, Xsqlite3_mprintf(tls, __ccgo_ts+31052, libc.VaList(bp+24, zWrite, zTbl1, zWhere1))) } if libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0)) == 0 && (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed != 0 { zRbuRowid = __ccgo_ts + 1648 if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) { zRbuRowid = __ccgo_ts + 31080 } /* Create the rbu_tmp_xxx table and the triggers to populate it. */ if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) { v6 = __ccgo_ts + 31092 } else { v6 = __ccgo_ts + 1648 } _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+31109, libc.VaList(bp+24, p+24, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, v6, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl)) _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+31185, libc.VaList(bp+24, zWrite, zTbl1, zOldlist, zWrite, zTbl1, zOldlist, zWrite, zTbl1, zNewlist)) if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) { _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+31484, libc.VaList(bp+24, zWrite, zTbl1, zNewlist)) } _rbuObjIterPrepareTmpInsert(tls, p, pIter, zCollist, zRbuRowid) } /* Create the SELECT statement to read keys from data_xxx */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { zRbuRowid1 = __ccgo_ts + 1648 zStart1 = uintptr(0) zOrder = uintptr(0) if bRbuRowid != 0 { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { v7 = __ccgo_ts + 31583 } else { v7 = __ccgo_ts + 31593 } zRbuRowid1 = v7 } if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { if nOffset != 0 { zStart1 = _rbuVacuumTableStart(tls, p, pIter, bRbuRowid, zWrite) if zStart1 != 0 { Xsqlite3_free(tls, zLimit) zLimit = uintptr(0) } } if bRbuRowid != 0 { zOrder = _rbuMPrintf(tls, p, __ccgo_ts+29640, 0) } else { zOrder = _rbuObjIterGetPkList(tls, p, pIter, __ccgo_ts+1648, __ccgo_ts+14990, __ccgo_ts+1648) } } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { v8 = __ccgo_ts + 31604 } else { v8 = __ccgo_ts + 1648 } if zStart1 != 0 { v9 = zStart1 } else { v9 = __ccgo_ts + 1648 } if zOrder != 0 { v10 = __ccgo_ts + 23207 } else { v10 = __ccgo_ts + 1648 } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, pIter+76, pz, Xsqlite3_mprintf(tls, __ccgo_ts+31610, libc.VaList(bp+24, zCollist, v8, zRbuRowid1, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, v9, v10, zOrder, zLimit))) } Xsqlite3_free(tls, zStart1) Xsqlite3_free(tls, zOrder) } Xsqlite3_free(tls, zWhere1) Xsqlite3_free(tls, zOldlist) Xsqlite3_free(tls, zNewlist) Xsqlite3_free(tls, zBindings) } Xsqlite3_free(tls, zCollist) Xsqlite3_free(tls, zLimit) } return (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc } // C documentation // // /* // ** Set output variable *ppStmt to point to an UPDATE statement that may // ** be used to update the imposter table for the main table b-tree of the // ** table object that pIter currently points to, assuming that the // ** rbu_control column of the data_xyz table contains zMask. // ** // ** If the zMask string does not specify any columns to update, then this // ** is not an error. Output variable *ppStmt is set to NULL in this case. // */ func _rbuGetUpdateStmt(tls *libc.TLS, p uintptr, pIter uintptr, zMask uintptr, ppStmt uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var nUp int32 var pUp, pp, zPrefix, zSet, zUpdate, zWhere uintptr _, _, _, _, _, _, _ = nUp, pUp, pp, zPrefix, zSet, zUpdate, zWhere pUp = uintptr(0) nUp = 0 /* In case an error occurs */ *(*uintptr)(unsafe.Pointer(ppStmt)) = uintptr(0) /* Search for an existing statement. If one is found, shift it to the front ** of the LRU queue and return immediately. Otherwise, leave nUp pointing ** to the number of statements currently in the cache and pUp to the ** last object in the list. */ pp = pIter + 104 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != 0) { break } pUp = *(*uintptr)(unsafe.Pointer(pp)) if libc.Xstrcmp(tls, (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FzMask, zMask) == 0 { *(*uintptr)(unsafe.Pointer(pp)) = (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpNext (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpNext = (*TRbuObjIter)(unsafe.Pointer(pIter)).FpRbuUpdate (*TRbuObjIter)(unsafe.Pointer(pIter)).FpRbuUpdate = pUp *(*uintptr)(unsafe.Pointer(ppStmt)) = (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpUpdate return SQLITE_OK } nUp++ goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 8 } _ = libc.Int32FromInt32(0) if nUp >= int32(SQLITE_RBU_UPDATE_CACHESIZE) { pp = pIter + 104 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != pUp) { break } goto _2 _2: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 8 } *(*uintptr)(unsafe.Pointer(pp)) = uintptr(0) Xsqlite3_finalize(tls, (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpUpdate) (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpUpdate = uintptr(0) } else { pUp = _rbuMalloc(tls, p, int64(uint32(12)+uint32((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol)+uint32(1))) } if pUp != 0 { zWhere = _rbuObjIterGetWhere(tls, p, pIter) zSet = _rbuObjIterGetSetlist(tls, p, pIter, zMask) zUpdate = uintptr(0) (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FzMask = pUp + 1*12 libc.Xmemcpy(tls, (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FzMask, zMask, uint32((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol)) (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpNext = (*TRbuObjIter)(unsafe.Pointer(pIter)).FpRbuUpdate (*TRbuObjIter)(unsafe.Pointer(pIter)).FpRbuUpdate = pUp if zSet != 0 { zPrefix = __ccgo_ts + 1648 if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType != int32(RBU_PK_VTAB) { zPrefix = __ccgo_ts + 30997 } zUpdate = Xsqlite3_mprintf(tls, __ccgo_ts+31658, libc.VaList(bp+8, zPrefix, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, zSet, zWhere)) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pUp+4, p+36, zUpdate) *(*uintptr)(unsafe.Pointer(ppStmt)) = (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpUpdate } Xsqlite3_free(tls, zWhere) Xsqlite3_free(tls, zSet) } return (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc } func _rbuOpenDbhandle(tls *libc.TLS, p uintptr, zName uintptr, bUseVfs int32) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var flags int32 var v1 uintptr var _ /* db at bp+0 */ uintptr _, _ = flags, v1 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { flags = libc.Int32FromInt32(SQLITE_OPEN_READWRITE) | libc.Int32FromInt32(SQLITE_OPEN_CREATE) | libc.Int32FromInt32(SQLITE_OPEN_URI) if bUseVfs != 0 { v1 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FzVfsName } else { v1 = uintptr(0) } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_open_v2(tls, zName, bp, flags, v1) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != 0 { (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+16, Xsqlite3_errmsg(tls, *(*uintptr)(unsafe.Pointer(bp))))) Xsqlite3_close(tls, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } } return *(*uintptr)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Free an RbuState object allocated by rbuLoadState(). // */ func _rbuFreeState(tls *libc.TLS, p uintptr) { if p != 0 { Xsqlite3_free(tls, (*TRbuState)(unsafe.Pointer(p)).FzTbl) Xsqlite3_free(tls, (*TRbuState)(unsafe.Pointer(p)).FzDataTbl) Xsqlite3_free(tls, (*TRbuState)(unsafe.Pointer(p)).FzIdx) Xsqlite3_free(tls, p) } } // C documentation // // /* // ** Allocate an RbuState object and load the contents of the rbu_state // ** table into it. Return a pointer to the new object. It is the // ** responsibility of the caller to eventually free the object using // ** sqlite3_free(). // ** // ** If an error occurs, leave an error code and message in the rbu handle // ** and return NULL. // */ func _rbuLoadState(tls *libc.TLS, p uintptr) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var pRet uintptr var rc2 int32 var _ /* pStmt at bp+0 */ uintptr var _ /* rc at bp+4 */ int32 _, _ = pRet, rc2 pRet = uintptr(0) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) pRet = _rbuMalloc(tls, p, int64(64)) if pRet == uintptr(0) { return uintptr(0) } *(*int32)(unsafe.Pointer(bp + 4)) = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+31688, libc.VaList(bp+16, p+24))) for *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { switch Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) { case int32(RBU_STATE_STAGE): (*TRbuState)(unsafe.Pointer(pRet)).FeStage = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) if (*TRbuState)(unsafe.Pointer(pRet)).FeStage != int32(RBU_STAGE_OAL) && (*TRbuState)(unsafe.Pointer(pRet)).FeStage != int32(RBU_STAGE_MOVE) && (*TRbuState)(unsafe.Pointer(pRet)).FeStage != int32(RBU_STAGE_CKPT) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_CORRUPT) } case int32(RBU_STATE_TBL): (*TRbuState)(unsafe.Pointer(pRet)).FzTbl = _rbuStrndup(tls, Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)), bp+4) case int32(RBU_STATE_IDX): (*TRbuState)(unsafe.Pointer(pRet)).FzIdx = _rbuStrndup(tls, Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)), bp+4) case int32(RBU_STATE_ROW): (*TRbuState)(unsafe.Pointer(pRet)).FnRow = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) case int32(RBU_STATE_PROGRESS): (*TRbuState)(unsafe.Pointer(pRet)).FnProgress = Xsqlite3_column_int64(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) case int32(RBU_STATE_CKPT): (*TRbuState)(unsafe.Pointer(pRet)).FiWalCksum = Xsqlite3_column_int64(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) case int32(RBU_STATE_COOKIE): (*TRbuState)(unsafe.Pointer(pRet)).FiCookie = uint32(Xsqlite3_column_int64(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1))) case int32(RBU_STATE_OALSZ): (*TRbuState)(unsafe.Pointer(pRet)).FiOalSz = Xsqlite3_column_int64(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) case int32(RBU_STATE_PHASEONESTEP): (*TRbuState)(unsafe.Pointer(pRet)).FnPhaseOneStep = Xsqlite3_column_int64(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) case int32(RBU_STATE_DATATBL): (*TRbuState)(unsafe.Pointer(pRet)).FzDataTbl = _rbuStrndup(tls, Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)), bp+4) default: *(*int32)(unsafe.Pointer(bp + 4)) = int32(SQLITE_CORRUPT) break } } rc2 = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 4)) = rc2 } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = *(*int32)(unsafe.Pointer(bp + 4)) return pRet } // C documentation // // /* // ** Open the database handle and attach the RBU database as "rbu". If an // ** error occurs, leave an error code and message in the RBU handle. // ** // ** If argument dbMain is not NULL, then it is a database handle already // ** open on the target database. Use this handle instead of opening a new // ** one. // */ func _rbuOpenDatabase(tls *libc.TLS, p uintptr, dbMain uintptr, pbRetry uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var bOpen, rc int32 var pState, zExtra, zFile, zTarget, v1, v2, v3 uintptr _, _, _, _, _, _, _, _, _ = bOpen, pState, rc, zExtra, zFile, zTarget, v1, v2, v3 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Open the RBU database */ (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu = _rbuOpenDbhandle(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu, int32(1)) (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain = dbMain if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { Xsqlite3_file_control(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+6585, int32(SQLITE_FCNTL_RBUCNT), p) if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzState == uintptr(0) { zFile = Xsqlite3_db_filename(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+6585) (*Tsqlite3rbu)(unsafe.Pointer(p)).FzState = _rbuMPrintf(tls, p, __ccgo_ts+31718, libc.VaList(bp+8, zFile, zFile)) } } /* If using separate RBU and state databases, attach the state database to ** the RBU db handle now. */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzState != 0 { _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+31746, libc.VaList(bp+8, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzState)) libc.Xmemcpy(tls, p+24, __ccgo_ts+3418, uint32(4)) } else { libc.Xmemcpy(tls, p+24, __ccgo_ts+6585, uint32(4)) } /* If it has not already been created, create the rbu_state table */ _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+31764, libc.VaList(bp+8, p+24)) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { bOpen = 0 (*Tsqlite3rbu)(unsafe.Pointer(p)).FnRbu = 0 (*Tsqlite3rbu)(unsafe.Pointer(p)).FpRbuFd = uintptr(0) rc = Xsqlite3_file_control(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+6585, int32(SQLITE_FCNTL_RBUCNT), p) if rc != int32(SQLITE_NOTFOUND) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc } if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage >= int32(RBU_STAGE_MOVE) { bOpen = int32(1) } else { pState = _rbuLoadState(tls, p) if pState != 0 { bOpen = libc.BoolInt32((*TRbuState)(unsafe.Pointer(pState)).FeStage >= int32(RBU_STAGE_MOVE)) _rbuFreeState(tls, pState) } } if bOpen != 0 { (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain = _rbuOpenDbhandle(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu, libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FnRbu <= int32(1))) } } (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = 0 if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain == uintptr(0) { if !((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == libc.UintptrFromInt32(0)) { (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain = _rbuOpenDbhandle(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget, int32(1)) } else { if (*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FpRbuFd)).FpWalFd != 0 { if pbRetry != 0 { (*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FpRbuFd)).FbNolock = uint8(0) Xsqlite3_close(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu) Xsqlite3_close(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain) (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain = uintptr(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu = uintptr(0) *(*int32)(unsafe.Pointer(pbRetry)) = int32(1) return } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR) (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+31830, 0) } else { zExtra = uintptr(0) if libc.Xstrlen(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu) >= uint32(5) && 0 == libc.Xmemcmp(tls, __ccgo_ts+24596, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu, uint32(5)) { zExtra = (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu + 5 for *(*uint8)(unsafe.Pointer(zExtra)) != 0 { v1 = zExtra zExtra++ if int32(*(*uint8)(unsafe.Pointer(v1))) == int32('?') { break } } if int32(*(*uint8)(unsafe.Pointer(zExtra))) == int32('\000') { zExtra = uintptr(0) } } if zExtra == uintptr(0) { v2 = __ccgo_ts + 1648 } else { v2 = __ccgo_ts + 31862 } if zExtra == uintptr(0) { v3 = __ccgo_ts + 1648 } else { v3 = zExtra } zTarget = Xsqlite3_mprintf(tls, __ccgo_ts+31864, libc.VaList(bp+8, Xsqlite3_db_filename(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+6585), v2, v3)) if zTarget == uintptr(0) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) return } (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain = _rbuOpenDbhandle(tls, p, zTarget, libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FnRbu <= int32(1))) Xsqlite3_free(tls, zTarget) } } } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_create_function(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+31896, -int32(1), int32(SQLITE_UTF8), p, __ccgo_fp(_rbuTmpInsertFunc), uintptr(0), uintptr(0)) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_create_function(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+31911, int32(2), int32(SQLITE_UTF8), uintptr(0), __ccgo_fp(_rbuFossilDeltaFunc), uintptr(0), uintptr(0)) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_create_function(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+31928, -int32(1), int32(SQLITE_UTF8), p, __ccgo_fp(_rbuTargetNameFunc), uintptr(0), uintptr(0)) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_file_control(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6585, int32(SQLITE_FCNTL_RBU), p) } _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+31944, 0) /* Mark the database file just opened as an RBU target database. If ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use. ** This is an error. */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_file_control(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6585, int32(SQLITE_FCNTL_RBU), p) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == int32(SQLITE_NOTFOUND) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR) (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+31972, 0) } } // C documentation // // /* // ** This routine is a copy of the sqlite3FileSuffix3() routine from the core. // ** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined. // ** // ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database // ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and // ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than // ** three characters, then shorten the suffix on z[] to be the last three // ** characters of the original suffix. // ** // ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always // ** do the suffix shortening regardless of URI parameter. // ** // ** Examples: // ** // ** test.db-journal => test.nal // ** test.db-wal => test.wal // ** test.db-shm => test.shm // ** test.db-mj7f3319fa => test.9fa // */ func _rbuFileSuffix3(tls *libc.TLS, zBase uintptr, z uintptr) { } // C documentation // // /* // ** Return the current wal-index header checksum for the target database // ** as a 64-bit integer. // ** // ** The checksum is store in the first page of xShmMap memory as an 8-byte // ** blob starting at byte offset 40. // */ func _rbuShmChecksum(tls *libc.TLS, p uintptr) (r Ti64) { bp := tls.Alloc(16) defer tls.Free(16) var iRet Ti64 var pDb uintptr var _ /* ptr at bp+0 */ uintptr _, _ = iRet, pDb iRet = 0 if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { pDb = (*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FpTargetFd)).FpReal (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = (*(*func(*libc.TLS, uintptr, int32, int32, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(pDb)).FpMethods)).FxShmMap})))(tls, pDb, 0, libc.Int32FromInt32(32)*libc.Int32FromInt32(1024), 0, bp) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { iRet = int64(libc.AtomicLoadPUint32(*(*uintptr)(unsafe.Pointer(bp))+libc.UintptrFromInt32(10)*4))< db page) copy operations required to checkpoint the // ** current wal file, and obtains the set of shm locks required to safely // ** perform the copy operations directly on the file-system. // ** // ** If argument pState is not NULL, then the incremental checkpoint is // ** being resumed. In this case, if the checksum of the wal-index-header // ** following recovery is not the same as the checksum saved in the RbuState // ** object, then the rbu handle is set to DONE state. This occurs if some // ** other client appends a transaction to the wal file in the middle of // ** an incremental checkpoint. // */ func _rbuSetupCheckpoint(tls *libc.TLS, p uintptr, pState uintptr) { var nSectorSize, rc2, v1 int32 var pDb, pWal uintptr _, _, _, _, _ = nSectorSize, pDb, pWal, rc2, v1 /* If pState is NULL, then the wal file may not have been opened and ** recovered. Running a read-statement here to ensure that doing so ** does not interfere with the "capture" process below. */ if pState == uintptr(0) { (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = 0 if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+31944, uintptr(0), uintptr(0), uintptr(0)) } } /* Assuming no error has occurred, run a "restart" checkpoint with the ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following ** special behaviour in the rbu VFS: ** ** * If the exclusive shm WRITER or READ0 lock cannot be obtained, ** the checkpoint fails with SQLITE_BUSY (normally SQLite would ** proceed with running a passive checkpoint instead of failing). ** ** * Attempts to read from the *-wal file or write to the database file ** do not perform any IO. Instead, the frame/page combinations that ** would be read/written are recorded in the sqlite3rbu.aFrame[] ** array. ** ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER, ** READ0 and CHECKPOINT locks taken as part of the checkpoint are ** no-ops. These locks will not be released until the connection ** is closed. ** ** * Attempting to xSync() the database file causes an SQLITE_NOTICE ** error. ** ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the ** checkpoint below fails with SQLITE_NOTICE, and leaves the aFrame[] ** array populated with a set of (frame -> page) mappings. Because the ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy ** data from the wal file into the database file according to the ** contents of aFrame[]. */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_CAPTURE) rc2 = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+31990, uintptr(0), uintptr(0), uintptr(0)) if rc2 != int32(SQLITE_NOTICE) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc2 } } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FnFrame > 0 { (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_CKPT) if pState != 0 { v1 = (*TRbuState)(unsafe.Pointer(pState)).FnRow } else { v1 = 0 } (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep = v1 (*Tsqlite3rbu)(unsafe.Pointer(p)).FaBuf = _rbuMalloc(tls, p, int64((*Tsqlite3rbu)(unsafe.Pointer(p)).Fpgsz)) (*Tsqlite3rbu)(unsafe.Pointer(p)).FiWalCksum = _rbuShmChecksum(tls, p) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FnFrame == 0 || pState != 0 && (*TRbuState)(unsafe.Pointer(pState)).FiWalCksum != (*Tsqlite3rbu)(unsafe.Pointer(p)).FiWalCksum { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_DONE) (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_DONE) } else { pDb = (*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FpTargetFd)).FpReal pWal = (*Trbu_file)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FpTargetFd)).FpWalFd)).FpReal _ = libc.Int32FromInt32(0) nSectorSize = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(pDb)).FpMethods)).FxSectorSize})))(tls, pDb) if nSectorSize > (*Tsqlite3rbu)(unsafe.Pointer(p)).Fpgsz { (*Tsqlite3rbu)(unsafe.Pointer(p)).FnPagePerSector = nSectorSize / (*Tsqlite3rbu)(unsafe.Pointer(p)).Fpgsz } else { (*Tsqlite3rbu)(unsafe.Pointer(p)).FnPagePerSector = int32(1) } /* Call xSync() on the wal file. This causes SQLite to sync the ** directory in which the target database and the wal file reside, in ** case it has not been synced since the rename() call in ** rbuMoveOalFile(). */ (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(pWal)).FpMethods)).FxSync})))(tls, pWal, int32(SQLITE_SYNC_NORMAL)) } } } // C documentation // // /* // ** Called when iAmt bytes are read from offset iOff of the wal file while // ** the rbu object is in capture mode. Record the frame number of the frame // ** being read in the aFrame[] array. // */ func _rbuCaptureWalRead(tls *libc.TLS, pRbu uintptr, iOff Ti64, iAmt int32) (r int32) { var aNew uintptr var iFrame, mReq Tu32 var nNew, v1 int32 _, _, _, _, _ = aNew, iFrame, mReq, nNew, v1 mReq = uint32(libc.Int32FromInt32(1)<db is ** holding a SHARED lock on the target database file (because it is ** in WAL mode). So no other connection may be writing the db. ** ** In order to ensure that there are no database readers, an EXCLUSIVE ** lock is obtained here before the *-oal is moved to *-wal. */ dbMain = uintptr(0) _rbuFileSuffix3(tls, zBase, zWal) _rbuFileSuffix3(tls, zBase, zOal) /* Re-open the databases. */ _rbuObjIterFinalize(tls, p+48) Xsqlite3_close(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu) Xsqlite3_close(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain) (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain = uintptr(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu = uintptr(0) dbMain = _rbuOpenDbhandle(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget, int32(1)) if dbMain != 0 { _ = libc.Int32FromInt32(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _rbuLockDatabase(tls, dbMain) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = (*(*func(*libc.TLS, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3rbu)(unsafe.Pointer(p)).FxRename})))(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FpRenameArg, zOal, zWal) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != SQLITE_OK || (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) || _rbuExclusiveCheckpoint(tls, dbMain) == 0 { Xsqlite3_close(tls, dbMain) dbMain = uintptr(0) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { _rbuOpenDatabase(tls, p, dbMain, uintptr(0)) _rbuSetupCheckpoint(tls, p, uintptr(0)) } } Xsqlite3_free(tls, zWal) Xsqlite3_free(tls, zOal) } // C documentation // // /* // ** The SELECT statement iterating through the keys for the current object // ** (p->objiter.pSelect) currently points to a valid row. This function // ** determines the type of operation requested by this row and returns // ** one of the following values to indicate the result: // ** // ** * RBU_INSERT // ** * RBU_DELETE // ** * RBU_IDX_DELETE // ** * RBU_UPDATE // ** // ** If RBU_UPDATE is returned, then output variable *pzMask is set to // ** point to the text value indicating the columns to update. // ** // ** If the rbu_control field contains an invalid value, an error code and // ** message are left in the RBU handle and zero returned. // */ func _rbuStepType(tls *libc.TLS, p uintptr, pzMask uintptr) (r int32) { var iCol, iVal, res int32 var z uintptr _, _, _, _ = iCol, iVal, res, z iCol = (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FnCol /* Index of rbu_control column */ res = 0 /* Return value */ switch Xsqlite3_column_type(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FpSelect, iCol) { case int32(SQLITE_INTEGER): iVal = Xsqlite3_column_int(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FpSelect, iCol) switch iVal { case 0: res = int32(RBU_INSERT) case int32(1): res = int32(RBU_DELETE) case int32(2): res = int32(RBU_REPLACE) case int32(3): res = int32(RBU_IDX_DELETE) case int32(4): res = int32(RBU_IDX_INSERT) break } case int32(SQLITE_TEXT): z = Xsqlite3_column_text(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FpSelect, iCol) if z == uintptr(0) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) } else { *(*uintptr)(unsafe.Pointer(pzMask)) = z } res = int32(RBU_UPDATE) default: break } if res == 0 { _rbuBadControlError(tls, p) } return res } // C documentation // // /* // ** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or // ** RBU_IDX_DELETE. This function performs the work of a single // ** sqlite3rbu_step() call for the type of operation specified by eType. // */ func _rbuStepOneOp(tls *libc.TLS, p uintptr, eType int32) { var i int32 var pIter, pVal, pWriter uintptr _, _, _, _ = i, pIter, pVal, pWriter pIter = p + 48 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE ** statement below does actually delete a row, nPhaseOneStep will be ** incremented by the same amount when SQL function rbu_tmp_insert() ** is invoked by the trigger. */ if eType == int32(RBU_DELETE) { *(*Ti64)(unsafe.Pointer(p + 176)) -= int64((*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FnIndex) } if eType == int32(RBU_IDX_DELETE) || eType == int32(RBU_DELETE) { pWriter = (*TRbuObjIter)(unsafe.Pointer(pIter)).FpDelete } else { pWriter = (*TRbuObjIter)(unsafe.Pointer(pIter)).FpInsert } i = 0 for { if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol) { break } /* If this is an INSERT into a table b-tree and the table has an ** explicit INTEGER PRIMARY KEY, check that this is not an attempt ** to write a NULL into the IPK column. That is not permitted. */ if eType == int32(RBU_INSERT) && (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx == uintptr(0) && (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_IPK) && *(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i))) != 0 && Xsqlite3_column_type(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect, i) == int32(SQLITE_NULL) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_MISMATCH) (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+24153, 0) return } if eType == int32(RBU_DELETE) && int32(*(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i)))) == 0 { goto _1 } pVal = Xsqlite3_column_value(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect, i) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_bind_value(tls, pWriter, i+int32(1), pVal) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != 0 { return } goto _1 _1: ; i++ } if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx == uintptr(0) { if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_VTAB) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) && (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { /* For a virtual table, or a table with no primary key, the ** SELECT statement is: ** ** SELECT , rbu_control, rbu_rowid FROM .... ** ** Hence column_value(pIter->nCol+1). */ pVal = Xsqlite3_column_value(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect, (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol+int32(1)) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_bind_value(tls, pWriter, (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol+int32(1), pVal) } } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { Xsqlite3_step(tls, pWriter) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _resetAndCollectError(tls, pWriter, p+36) } } // C documentation // // /* // ** This function does the work for an sqlite3rbu_step() call. // ** // ** The object-iterator (p->objiter) currently points to a valid object, // ** and the input cursor (p->objiter.pSelect) currently points to a valid // ** input row. Perform whatever processing is required and return. // ** // ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code // ** and message is left in the RBU handle and a copy of the error code // ** returned. // */ func _rbuStep(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var c uint8 var eType, i int32 var pIter, pVal uintptr var _ /* pUpdate at bp+4 */ uintptr var _ /* zMask at bp+0 */ uintptr _, _, _, _, _ = c, eType, i, pIter, pVal pIter = p + 48 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) eType = _rbuStepType(tls, p, bp) if eType != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx == uintptr(0) && (eType == int32(RBU_IDX_DELETE) || eType == int32(RBU_IDX_INSERT)) { _rbuBadControlError(tls, p) } else { if eType == int32(RBU_REPLACE) { if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx == uintptr(0) { *(*Ti64)(unsafe.Pointer(p + 176)) += int64((*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FnIndex) _rbuStepOneOp(tls, p, int32(RBU_DELETE)) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { _rbuStepOneOp(tls, p, int32(RBU_INSERT)) } } else { if eType != int32(RBU_UPDATE) { _rbuStepOneOp(tls, p, eType) } else { *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) _ = libc.Int32FromInt32(0) *(*Ti64)(unsafe.Pointer(p + 176)) -= int64((*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FnIndex) _rbuGetUpdateStmt(tls, p, pIter, *(*uintptr)(unsafe.Pointer(bp)), bp+4) if *(*uintptr)(unsafe.Pointer(bp + 4)) != 0 { i = 0 for { if !((*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol) { break } c = *(*uint8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)) + uintptr(*(*int32)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(i)*4))))) pVal = Xsqlite3_column_value(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect, i) if *(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i))) != 0 || int32(c) != int32('.') { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_bind_value(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), i+int32(1), pVal) } goto _1 _1: ; i++ } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && ((*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_VTAB) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE)) { /* Bind the rbu_rowid value to column _rowid_ */ pVal = Xsqlite3_column_value(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect, (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol+int32(1)) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_bind_value(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol+int32(1), pVal) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _resetAndCollectError(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), p+36) } } } } } } return (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc } // C documentation // // /* // ** Increment the schema cookie of the main database opened by p->dbMain. // ** // ** Or, if this is an RBU vacuum, set the schema cookie of the main db // ** opened by p->dbMain to one more than the schema cookie of the main // ** db opened by p->dbRbu. // */ func _rbuIncrSchemaCookie(tls *libc.TLS, p uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var dbread, v1 uintptr var iCookie int32 var _ /* pStmt at bp+0 */ uintptr _, _, _ = dbread, iCookie, v1 if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { v1 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu } else { v1 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain } dbread = v1 iCookie = int32(1000000) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, dbread, bp, p+36, __ccgo_ts+32064) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { /* Coverage: it may be that this sqlite3_step() cannot fail. There ** is already a transaction open, so the prepared statement cannot ** throw an SQLITE_SCHEMA exception. The only database page the ** statement reads is page 1, which is guaranteed to be in the cache. ** And no memory allocations are required. */ if int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { iCookie = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp))) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+32086, libc.VaList(bp+16, iCookie+int32(1))) } } } // C documentation // // /* // ** Update the contents of the rbu_state table within the rbu database. The // ** value stored in the RBU_STATE_STAGE column is eStage. All other values // ** are determined by inspecting the rbu handle passed as the first argument. // */ func _rbuSaveState(tls *libc.TLS, p uintptr, eStage int32) { bp := tls.Alloc(192) defer tls.Free(192) var pFd, v1 uintptr var rc int32 var _ /* pInsert at bp+0 */ uintptr _, _, _ = pFd, rc, v1 if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK || (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == int32(SQLITE_DONE) { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { v1 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FpRbuFd } else { v1 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FpTargetFd } pFd = v1 _ = libc.Int32FromInt32(0) rc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32113, libc.VaList(bp+16, p+24, int32(RBU_STATE_STAGE), eStage, int32(RBU_STATE_TBL), (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FzTbl, int32(RBU_STATE_IDX), (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FzIdx, int32(RBU_STATE_ROW), (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep, int32(RBU_STATE_PROGRESS), (*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress, int32(RBU_STATE_CKPT), (*Tsqlite3rbu)(unsafe.Pointer(p)).FiWalCksum, int32(RBU_STATE_COOKIE), int64((*Trbu_file)(unsafe.Pointer(pFd)).FiCookie), int32(RBU_STATE_OALSZ), (*Tsqlite3rbu)(unsafe.Pointer(p)).FiOalSz, int32(RBU_STATE_PHASEONESTEP), (*Tsqlite3rbu)(unsafe.Pointer(p)).FnPhaseOneStep, int32(RBU_STATE_DATATBL), (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FzDataTbl))) _ = libc.Int32FromInt32(0) if rc == SQLITE_OK { Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) rc = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) } if rc != SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc } } } // C documentation // // /* // ** The second argument passed to this function is the name of a PRAGMA // ** setting - "page_size", "auto_vacuum", "user_version" or "application_id". // ** This function executes the following on sqlite3rbu.dbRbu: // ** // ** "PRAGMA main.$zPragma" // ** // ** where $zPragma is the string passed as the second argument, then // ** on sqlite3rbu.dbMain: // ** // ** "PRAGMA main.$zPragma = $val" // ** // ** where $val is the value returned by the first PRAGMA invocation. // ** // ** In short, it copies the value of the specified PRAGMA setting from // ** dbRbu to dbMain. // */ func _rbuCopyPragma(tls *libc.TLS, p uintptr, zPragma uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var _ /* pPragma at bp+0 */ uintptr if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32271, libc.VaList(bp+16, zPragma))) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+32286, libc.VaList(bp+16, zPragma, Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), 0))) } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp))) } } // C documentation // // /* // ** The RBU handle passed as the only argument has just been opened and // ** the state database is empty. If this RBU handle was opened for an // ** RBU vacuum operation, create the schema in the target db. // */ func _rbuCreateTargetSchema(tls *libc.TLS, p uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var i int32 var zSql uintptr var _ /* pInsert at bp+4 */ uintptr var _ /* pSql at bp+0 */ uintptr _, _ = i, zSql *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) _ = libc.Int32FromInt32(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+32306, uintptr(0), uintptr(0), p+36) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, __ccgo_ts+32331) } for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) == int32(SQLITE_ROW) { zSql = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, zSql, uintptr(0), uintptr(0), p+36) } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp))) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != SQLITE_OK { return } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, __ccgo_ts+32439) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+4, p+36, __ccgo_ts+32504) } for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) == int32(SQLITE_ROW) { i = 0 for { if !(i < int32(5)) { break } Xsqlite3_bind_value(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), i+int32(1), Xsqlite3_column_value(tls, *(*uintptr)(unsafe.Pointer(bp)), i)) goto _1 _1: ; i++ } Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+32548, uintptr(0), uintptr(0), p+36) } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp))) _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp + 4))) } // C documentation // // /* // ** Step the RBU object. // */ func Xsqlite3rbu_step(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var iSector Tu32 var pDb, pFrame, pIter uintptr var rc int32 var _ /* ptr at bp+0 */ uintptr _, _, _, _, _ = iSector, pDb, pFrame, pIter, rc if p != 0 { switch (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage { case int32(RBU_STAGE_OAL): pIter = p + 48 /* If this is an RBU vacuum operation and the state table was empty ** when this handle was opened, create the target database schema. */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) && (*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress == 0 && (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { _rbuCreateTargetSchema(tls, p) _rbuCopyPragma(tls, p, __ccgo_ts+17667) _rbuCopyPragma(tls, p, __ccgo_ts+16778) } for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl != 0 { if (*TRbuObjIter)(unsafe.Pointer(pIter)).FbCleanup != 0 { /* Clean up the rbu_tmp_xxx table for the previous table. It ** cannot be dropped as there are currently active SQL statements. ** But the contents can be deleted. */ if libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0)) == 0 && (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed != 0 { _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+32573, libc.VaList(bp+16, p+24, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl)) } } else { _rbuObjIterPrepareAll(tls, p, pIter, 0) /* Advance to the next row to process. */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { rc = Xsqlite3_step(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect) if rc == int32(SQLITE_ROW) { (*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress++ (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep++ return _rbuStep(tls, p) } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect) (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep = 0 } } _rbuObjIterNext(tls, p, pIter) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { _ = libc.Int32FromInt32(0) _rbuSaveState(tls, p, int32(RBU_STAGE_MOVE)) _rbuIncrSchemaCookie(tls, p) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+14894, uintptr(0), uintptr(0), p+36) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+14894, uintptr(0), uintptr(0), p+36) } (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_MOVE) } case int32(RBU_STAGE_MOVE): if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { _rbuMoveOalFile(tls, p) (*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress++ } case int32(RBU_STAGE_CKPT): if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep >= (*Tsqlite3rbu)(unsafe.Pointer(p)).FnFrame { pDb = (*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FpTargetFd)).FpReal /* Sync the db file */ (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(pDb)).FpMethods)).FxSync})))(tls, pDb, int32(SQLITE_SYNC_NORMAL)) /* Update nBackfill */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = (*(*func(*libc.TLS, uintptr, int32, int32, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(pDb)).FpMethods)).FxShmMap})))(tls, pDb, 0, libc.Int32FromInt32(32)*libc.Int32FromInt32(1024), 0, bp) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { *(*Tu32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)) + 24*4)) = (*Tsqlite3rbu)(unsafe.Pointer(p)).FiMaxFrame } } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_DONE) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_DONE) } } else { for cond := true; cond; cond = (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep < (*Tsqlite3rbu)(unsafe.Pointer(p)).FnFrame && iSector == ((*(*TRbuFrame)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FaFrame + uintptr((*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep)*8))).FiDbPage-uint32(1))/uint32((*Tsqlite3rbu)(unsafe.Pointer(p)).FnPagePerSector) && (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { pFrame = (*Tsqlite3rbu)(unsafe.Pointer(p)).FaFrame + uintptr((*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep)*8 iSector = ((*TRbuFrame)(unsafe.Pointer(pFrame)).FiDbPage - uint32(1)) / uint32((*Tsqlite3rbu)(unsafe.Pointer(p)).FnPagePerSector) _rbuCheckpointFrame(tls, p, pFrame) (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep++ } } (*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress++ } default: break } return (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc } else { return int32(SQLITE_NOMEM) } return r } // C documentation // // /* // ** Compare strings z1 and z2, returning 0 if they are identical, or non-zero // ** otherwise. Either or both argument may be NULL. Two NULL values are // ** considered equal, and NULL is considered distinct from all other values. // */ func _rbuStrCompare(tls *libc.TLS, z1 uintptr, z2 uintptr) (r int32) { if z1 == uintptr(0) && z2 == uintptr(0) { return 0 } if z1 == uintptr(0) || z2 == uintptr(0) { return int32(1) } return libc.BoolInt32(Xsqlite3_stricmp(tls, z1, z2) != 0) } // C documentation // // /* // ** This function is called as part of sqlite3rbu_open() when initializing // ** an rbu handle in OAL stage. If the rbu update has not started (i.e. // ** the rbu_state table was empty) it is a no-op. Otherwise, it arranges // ** things so that the next call to sqlite3rbu_step() continues on from // ** where the previous rbu handle left off. // ** // ** If an error occurs, an error code and error message are left in the // ** rbu handle passed as the first argument. // */ func _rbuSetupOal(tls *libc.TLS, p uintptr, pState uintptr) { var pIter uintptr var rc int32 _, _ = pIter, rc _ = libc.Int32FromInt32(0) if (*TRbuState)(unsafe.Pointer(pState)).FzTbl != 0 { pIter = p + 48 rc = SQLITE_OK for rc == SQLITE_OK && (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl != 0 && ((*TRbuObjIter)(unsafe.Pointer(pIter)).FbCleanup != 0 || _rbuStrCompare(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx, (*TRbuState)(unsafe.Pointer(pState)).FzIdx) != 0 || (*TRbuState)(unsafe.Pointer(pState)).FzDataTbl == uintptr(0) && _rbuStrCompare(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, (*TRbuState)(unsafe.Pointer(pState)).FzTbl) != 0 || (*TRbuState)(unsafe.Pointer(pState)).FzDataTbl != 0 && _rbuStrCompare(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, (*TRbuState)(unsafe.Pointer(pState)).FzDataTbl) != 0) { rc = _rbuObjIterNext(tls, p, pIter) } if rc == SQLITE_OK && !((*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl != 0) { rc = int32(SQLITE_ERROR) (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+32601, 0) } if rc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep = (*TRbuState)(unsafe.Pointer(pState)).FnRow rc = _rbuObjIterPrepareAll(tls, p, p+48, (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep) } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc } } // C documentation // // /* // ** If there is a "*-oal" file in the file-system corresponding to the // ** target database in the file-system, delete it. If an error occurs, // ** leave an error code and error message in the rbu handle. // */ func _rbuDeleteOalFile(tls *libc.TLS, p uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var zOal uintptr var _ /* pVfs at bp+0 */ uintptr _ = zOal zOal = _rbuMPrintf(tls, p, __ccgo_ts+32050, libc.VaList(bp+16, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget)) if zOal != 0 { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) Xsqlite3_file_control(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6585, int32(SQLITE_FCNTL_VFS_POINTER), bp) _ = libc.Int32FromInt32(0) (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FxDelete})))(tls, *(*uintptr)(unsafe.Pointer(bp)), zOal, 0) Xsqlite3_free(tls, zOal) } } // C documentation // // /* // ** Allocate a private rbu VFS for the rbu handle passed as the only // ** argument. This VFS will be used unless the call to sqlite3rbu_open() // ** specified a URI with a vfs=? option in place of a target database // ** file name. // */ func _rbuCreateVfs(tls *libc.TLS, p uintptr) { bp := tls.Alloc(96) defer tls.Free(96) var pVfs uintptr var _ /* rnd at bp+0 */ int32 var _ /* zRnd at bp+4 */ [64]uint8 _ = pVfs _ = libc.Int32FromInt32(0) Xsqlite3_randomness(tls, int32(4), bp) Xsqlite3_snprintf(tls, int32(64), bp+4, __ccgo_ts+32626, libc.VaList(bp+80, *(*int32)(unsafe.Pointer(bp)))) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3rbu_create_vfs(tls, bp+4, uintptr(0)) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { pVfs = Xsqlite3_vfs_find(tls, bp+4) _ = libc.Int32FromInt32(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).FzVfsName = (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FzName (*Trbu_vfs)(unsafe.Pointer(pVfs)).FpRbu = p } } // C documentation // // /* // ** Destroy the private VFS created for the rbu handle passed as the only // ** argument by an earlier call to rbuCreateVfs(). // */ func _rbuDeleteVfs(tls *libc.TLS, p uintptr) { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzVfsName != 0 { Xsqlite3rbu_destroy_vfs(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzVfsName) (*Tsqlite3rbu)(unsafe.Pointer(p)).FzVfsName = uintptr(0) } } // C documentation // // /* // ** This user-defined SQL function is invoked with a single argument - the // ** name of a table expected to appear in the target database. It returns // ** the number of auxilliary indexes on the table. // */ func _rbuIndexCntFunc(tls *libc.TLS, pCtx uintptr, nVal int32, apVal uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var db, p, v1 uintptr var nIndex, rc int32 var _ /* pStmt at bp+0 */ uintptr var _ /* zErrmsg at bp+4 */ uintptr _, _, _, _, _ = db, nIndex, p, rc, v1 p = Xsqlite3_user_data(tls, pCtx) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { v1 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu } else { v1 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain } db = v1 _ = libc.Int32FromInt32(0) rc = _prepareFreeAndCollectError(tls, db, bp, bp+4, Xsqlite3_mprintf(tls, __ccgo_ts+32637, libc.VaList(bp+16, Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(apVal)))))) if rc != SQLITE_OK { Xsqlite3_result_error(tls, pCtx, *(*uintptr)(unsafe.Pointer(bp + 4)), -int32(1)) } else { nIndex = 0 if int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { nIndex = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) } rc = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) if rc == SQLITE_OK { Xsqlite3_result_int(tls, pCtx, nIndex) } else { Xsqlite3_result_error(tls, pCtx, Xsqlite3_errmsg(tls, db), -int32(1)) } } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) } // C documentation // // /* // ** If the RBU database contains the rbu_count table, use it to initialize // ** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table // ** is assumed to contain the same columns as: // ** // ** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID; // ** // ** There should be one row in the table for each data_xxx table in the // ** database. The 'tbl' column should contain the name of a data_xxx table, // ** and the cnt column the number of rows it contains. // ** // ** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt // ** for all rows in the rbu_count table, where nIndex is the number of // ** indexes on the corresponding target database table. // */ func _rbuInitPhaseOneSteps(tls *libc.TLS, p uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var bExists int32 var _ /* pStmt at bp+0 */ uintptr _ = bExists if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) bExists = 0 /* True if rbu_count exists */ (*Tsqlite3rbu)(unsafe.Pointer(p)).FnPhaseOneStep = int64(-int32(1)) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_create_function(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+32709, int32(1), int32(SQLITE_UTF8), p, __ccgo_fp(_rbuIndexCntFunc), uintptr(0), uintptr(0)) /* Check for the rbu_count table. If it does not exist, or if an error ** occurs, nPhaseOneStep will be left set to -1. */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, __ccgo_ts+32723) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { bExists = int32(1) } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && bExists != 0 { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, __ccgo_ts+32780) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { (*Tsqlite3rbu)(unsafe.Pointer(p)).FnPhaseOneStep = Xsqlite3_column_int64(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) } } } } func _openRbuHandle(tls *libc.TLS, zTarget uintptr, zRbu uintptr, zState uintptr) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var db, p, pCsr, pFd, pState, v2, v3 uintptr var frc int32 var nByte, nRbu, nTarget Tsize_t var v1 uint32 var _ /* bRetry at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _, _, _ = db, frc, nByte, nRbu, nTarget, p, pCsr, pFd, pState, v1, v2, v3 if zTarget != 0 { v1 = libc.Xstrlen(tls, zTarget) } else { v1 = uint32(0) } nTarget = v1 nRbu = libc.Xstrlen(tls, zRbu) nByte = uint32(256) + nTarget + uint32(1) + nRbu + uint32(1) p = Xsqlite3_malloc64(tls, uint64(nByte)) if p != 0 { pState = uintptr(0) /* Create the custom VFS. */ libc.Xmemset(tls, p, 0, uint32(256)) Xsqlite3rbu_rename_handler(tls, p, uintptr(0), uintptr(0)) _rbuCreateVfs(tls, p) /* Open the target, RBU and state databases */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { pCsr = p + 1*256 *(*int32)(unsafe.Pointer(bp)) = 0 if zTarget != 0 { (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget = pCsr libc.Xmemcpy(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget, zTarget, nTarget+uint32(1)) pCsr += uintptr(nTarget + uint32(1)) } (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu = pCsr libc.Xmemcpy(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu, zRbu, nRbu+uint32(1)) pCsr += uintptr(nRbu + uint32(1)) if zState != 0 { (*Tsqlite3rbu)(unsafe.Pointer(p)).FzState = _rbuMPrintf(tls, p, __ccgo_ts+3795, libc.VaList(bp+16, zState)) } /* If the first attempt to open the database file fails and the bRetry ** flag it set, this means that the db was not opened because it seemed ** to be a wal-mode db. But, this may have happened due to an earlier ** RBU vacuum operation leaving an old wal file in the directory. ** If this is the case, it will have been checkpointed and deleted ** when the handle was closed and a second attempt to open the ** database may succeed. */ _rbuOpenDatabase(tls, p, uintptr(0), bp) if *(*int32)(unsafe.Pointer(bp)) != 0 { _rbuOpenDatabase(tls, p, uintptr(0), uintptr(0)) } } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { pState = _rbuLoadState(tls, p) _ = libc.Int32FromInt32(0) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if (*TRbuState)(unsafe.Pointer(pState)).FeStage == 0 { _rbuDeleteOalFile(tls, p) _rbuInitPhaseOneSteps(tls, p) (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_OAL) } else { (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = (*TRbuState)(unsafe.Pointer(pState)).FeStage (*Tsqlite3rbu)(unsafe.Pointer(p)).FnPhaseOneStep = (*TRbuState)(unsafe.Pointer(pState)).FnPhaseOneStep } (*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress = int32((*TRbuState)(unsafe.Pointer(pState)).FnProgress) (*Tsqlite3rbu)(unsafe.Pointer(p)).FiOalSz = (*TRbuState)(unsafe.Pointer(pState)).FiOalSz } } _ = libc.Int32FromInt32(0) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FpTargetFd)).FpWalFd != 0 { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_OAL) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR) (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+32854, 0) } else { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_MOVE) { (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_CKPT) (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep = 0 } } } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && ((*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_OAL) || (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_MOVE)) && (*TRbuState)(unsafe.Pointer(pState)).FeStage != 0 { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { v2 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FpRbuFd } else { v2 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FpTargetFd } pFd = v2 if (*Trbu_file)(unsafe.Pointer(pFd)).FiCookie != (*TRbuState)(unsafe.Pointer(pState)).FiCookie { /* At this point (pTargetFd->iCookie) contains the value of the ** change-counter cookie (the thing that gets incremented when a ** transaction is committed in rollback mode) currently stored on ** page 1 of the database file. */ (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_BUSY) if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { v3 = __ccgo_ts + 32886 } else { v3 = __ccgo_ts + 32893 } (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+32900, libc.VaList(bp+16, v3)) } } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_OAL) { db = (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+14879, uintptr(0), uintptr(0), p+36) /* Point the object iterator at the first object */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _rbuObjIterFirst(tls, p, p+48) } /* If the RBU database contains no data_xxx tables, declare the RBU ** update finished. */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FzTbl == uintptr(0) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_DONE) (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_DONE) } else { if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TRbuState)(unsafe.Pointer(pState)).FeStage == 0 && (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { _rbuCopyPragma(tls, p, __ccgo_ts+17381) _rbuCopyPragma(tls, p, __ccgo_ts+16793) } /* Open transactions both databases. The *-oal file is opened or ** created at this point. */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, db, __ccgo_ts+32932, uintptr(0), uintptr(0), p+36) } /* Check if the main database is a zipvfs db. If it is, set the upper ** level pager to use "journal_mode=off". This prevents it from ** generating a large journal using a temp file. */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { frc = Xsqlite3_file_control(tls, db, __ccgo_ts+6585, int32(SQLITE_FCNTL_ZIPVFS), uintptr(0)) if frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, db, __ccgo_ts+32948, uintptr(0), uintptr(0), p+36) } } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { _rbuSetupOal(tls, p, pState) } } } else { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_MOVE) { /* no-op */ } else { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_CKPT) { if !((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == libc.UintptrFromInt32(0)) && _rbuExclusiveCheckpoint(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain) != 0 { /* If the rbu_exclusive_checkpoint=1 URI parameter was specified ** and an incremental checkpoint is being resumed, attempt an ** exclusive lock on the db file. If this fails, so be it. */ (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_DONE) _rbuLockDatabase(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain) (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_CKPT) } _rbuSetupCheckpoint(tls, p, pState) } else { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_DONE) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_DONE) } else { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_CORRUPT) } } } } } _rbuFreeState(tls, pState) } return p } // C documentation // // /* // ** Allocate and return an RBU handle with all fields zeroed except for the // ** error code, which is set to SQLITE_MISUSE. // */ func _rbuMisuseError(tls *libc.TLS) (r uintptr) { var pRet uintptr _ = pRet pRet = Xsqlite3_malloc64(tls, uint64(256)) if pRet != 0 { libc.Xmemset(tls, pRet, 0, uint32(256)) (*Tsqlite3rbu)(unsafe.Pointer(pRet)).Frc = int32(SQLITE_MISUSE) } return pRet } // C documentation // // /* // ** Open and return a new RBU handle. // */ func Xsqlite3rbu_open(tls *libc.TLS, zTarget uintptr, zRbu uintptr, zState uintptr) (r uintptr) { if zTarget == uintptr(0) || zRbu == uintptr(0) { return _rbuMisuseError(tls) } return _openRbuHandle(tls, zTarget, zRbu, zState) } // C documentation // // /* // ** Open a handle to begin or resume an RBU VACUUM operation. // */ func Xsqlite3rbu_vacuum(tls *libc.TLS, zTarget uintptr, zState uintptr) (r uintptr) { var n int32 _ = n if zTarget == uintptr(0) { return _rbuMisuseError(tls) } if zState != 0 { n = int32(libc.Xstrlen(tls, zState)) if n >= int32(7) && 0 == libc.Xmemcmp(tls, __ccgo_ts+32972, zState+uintptr(n-int32(7)), uint32(7)) { return _rbuMisuseError(tls) } } /* TODO: Check that both arguments are non-NULL */ return _openRbuHandle(tls, uintptr(0), zTarget, zState) } // C documentation // // /* // ** Return the database handle used by pRbu. // */ func Xsqlite3rbu_db(tls *libc.TLS, pRbu uintptr, bRbu int32) (r uintptr) { var db, v1 uintptr _, _ = db, v1 db = uintptr(0) if pRbu != 0 { if bRbu != 0 { v1 = (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FdbRbu } else { v1 = (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FdbMain } db = v1 } return db } // C documentation // // /* // ** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT, // ** then edit any error message string so as to remove all occurrences of // ** the pattern "rbu_imp_[0-9]*". // */ func _rbuEditErrmsg(tls *libc.TLS, p uintptr) { var i uint32 var nDel int32 var nErrmsg Tsize_t _, _, _ = i, nDel, nErrmsg if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == int32(SQLITE_CONSTRAINT) && (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg != 0 { nErrmsg = libc.Xstrlen(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg) i = uint32(0) for { if !(i < nErrmsg-uint32(8)) { break } if libc.Xmemcmp(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg+uintptr(i), __ccgo_ts+30997, uint32(8)) == 0 { nDel = int32(8) for int32(*(*uint8)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg + uintptr(i+uint32(nDel))))) >= int32('0') && int32(*(*uint8)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg + uintptr(i+uint32(nDel))))) <= int32('9') { nDel++ } libc.Xmemmove(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg+uintptr(i), (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg+uintptr(i+uint32(nDel)), nErrmsg+uint32(1)-i-uint32(nDel)) nErrmsg -= uint32(nDel) } goto _1 _1: ; i++ } } } // C documentation // // /* // ** Close the RBU handle. // */ func Xsqlite3rbu_close(tls *libc.TLS, p uintptr, pzErrmsg uintptr) (r int32) { var pDb uintptr var rc, rc2 int32 _, _, _ = pDb, rc, rc2 if p != 0 { /* Commit the transaction to the *-oal file. */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_OAL) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+14894, uintptr(0), uintptr(0), p+36) } /* Sync the db file if currently doing an incremental checkpoint */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_CKPT) { pDb = (*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FpTargetFd)).FpReal (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(pDb)).FpMethods)).FxSync})))(tls, pDb, int32(SQLITE_SYNC_NORMAL)) } _rbuSaveState(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_OAL) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+14894, uintptr(0), uintptr(0), p+36) } /* Close any open statement handles. */ _rbuObjIterFinalize(tls, p+48) /* If this is an RBU vacuum handle and the vacuum has either finished ** successfully or encountered an error, delete the contents of the ** state table. This causes the next call to sqlite3rbu_vacuum() ** specifying the current target and state databases to start a new ** vacuum from scratch. */ if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) && (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu != 0 { rc2 = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+32980, uintptr(0), uintptr(0), uintptr(0)) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == int32(SQLITE_DONE) && rc2 != SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc2 } } /* Close the open database handle and VFS object. */ Xsqlite3_close(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu) Xsqlite3_close(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain) _ = libc.Int32FromInt32(0) _rbuDeleteVfs(tls, p) Xsqlite3_free(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FaBuf) Xsqlite3_free(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FaFrame) _rbuEditErrmsg(tls, p) rc = (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc if pzErrmsg != 0 { *(*uintptr)(unsafe.Pointer(pzErrmsg)) = (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg } else { Xsqlite3_free(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg) } Xsqlite3_free(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzState) Xsqlite3_free(tls, p) } else { rc = int32(SQLITE_NOMEM) *(*uintptr)(unsafe.Pointer(pzErrmsg)) = uintptr(0) } return rc } // C documentation // // /* // ** Return the total number of key-value operations (inserts, deletes or // ** updates) that have been performed on the target database since the // ** current RBU update was started. // */ func Xsqlite3rbu_progress(tls *libc.TLS, pRbu uintptr) (r Tsqlite3_int64) { return int64((*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FnProgress) } // C documentation // // /* // ** Return permyriadage progress indications for the two main stages of // ** an RBU update. // */ func Xsqlite3rbu_bp_progress(tls *libc.TLS, p uintptr, pnOne uintptr, pnTwo uintptr) { var MAX_PROGRESS int32 _ = MAX_PROGRESS MAX_PROGRESS = int32(10000) switch (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage { case int32(RBU_STAGE_OAL): if (*Tsqlite3rbu)(unsafe.Pointer(p)).FnPhaseOneStep > 0 { *(*int32)(unsafe.Pointer(pnOne)) = int32(int64(MAX_PROGRESS) * int64((*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress) / (*Tsqlite3rbu)(unsafe.Pointer(p)).FnPhaseOneStep) } else { *(*int32)(unsafe.Pointer(pnOne)) = -int32(1) } *(*int32)(unsafe.Pointer(pnTwo)) = 0 case int32(RBU_STAGE_MOVE): *(*int32)(unsafe.Pointer(pnOne)) = MAX_PROGRESS *(*int32)(unsafe.Pointer(pnTwo)) = 0 case int32(RBU_STAGE_CKPT): *(*int32)(unsafe.Pointer(pnOne)) = MAX_PROGRESS *(*int32)(unsafe.Pointer(pnTwo)) = int32(int64(MAX_PROGRESS) * int64((*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep) / int64((*Tsqlite3rbu)(unsafe.Pointer(p)).FnFrame)) case int32(RBU_STAGE_DONE): *(*int32)(unsafe.Pointer(pnOne)) = MAX_PROGRESS *(*int32)(unsafe.Pointer(pnTwo)) = MAX_PROGRESS default: _ = libc.Int32FromInt32(0) } } // C documentation // // /* // ** Return the current state of the RBU vacuum or update operation. // */ func Xsqlite3rbu_state(tls *libc.TLS, p uintptr) (r int32) { var aRes [6]int32 _ = aRes aRes = [6]int32{ 1: int32(SQLITE_RBU_STATE_OAL), 2: int32(SQLITE_RBU_STATE_MOVE), 4: int32(SQLITE_RBU_STATE_CHECKPOINT), 5: int32(SQLITE_RBU_STATE_DONE), } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != int32(SQLITE_DONE) { return int32(SQLITE_RBU_STATE_ERROR) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return aRes[(*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage] } return r } func Xsqlite3rbu_savestate(tls *libc.TLS, p uintptr) (r int32) { var pDb, zBegin, v1 uintptr var rc int32 _, _, _, _ = pDb, rc, zBegin, v1 rc = (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc if rc == int32(SQLITE_DONE) { return SQLITE_OK } _ = libc.Int32FromInt32(0) if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_OAL) { _ = libc.Int32FromInt32(0) if rc == SQLITE_OK { rc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+14894, uintptr(0), uintptr(0), uintptr(0)) } } /* Sync the db file */ if rc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_CKPT) { pDb = (*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FpTargetFd)).FpReal rc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(pDb)).FpMethods)).FxSync})))(tls, pDb, int32(SQLITE_SYNC_NORMAL)) } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc _rbuSaveState(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage) rc = (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_OAL) { _ = libc.Int32FromInt32(0) if rc == SQLITE_OK { rc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+14894, uintptr(0), uintptr(0), uintptr(0)) } if rc == SQLITE_OK { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { v1 = __ccgo_ts + 14879 } else { v1 = __ccgo_ts + 32932 } zBegin = v1 rc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, zBegin, uintptr(0), uintptr(0), uintptr(0)) } if rc == SQLITE_OK { rc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+32932, uintptr(0), uintptr(0), uintptr(0)) } } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc return rc } // C documentation // // /* // ** Default xRename callback for RBU. // */ func _xDefaultRename(tls *libc.TLS, pArg uintptr, zOld uintptr, zNew uintptr) (r int32) { var rc, v1 int32 _, _ = rc, v1 rc = SQLITE_OK if libc.Xrename(tls, zOld, zNew) != 0 { v1 = int32(SQLITE_IOERR) } else { v1 = SQLITE_OK } rc = v1 return rc } func Xsqlite3rbu_rename_handler(tls *libc.TLS, pRbu uintptr, pArg uintptr, xRename uintptr) { if xRename != 0 { (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FxRename = xRename (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FpRenameArg = pArg } else { (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FxRename = __ccgo_fp(_xDefaultRename) (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FpRenameArg = uintptr(0) } } /************************************************************************** ** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour ** of a standard VFS in the following ways: ** ** 1. Whenever the first page of a main database file is read or ** written, the value of the change-counter cookie is stored in ** rbu_file.iCookie. Similarly, the value of the "write-version" ** database header field is stored in rbu_file.iWriteVer. This ensures ** that the values are always trustworthy within an open transaction. ** ** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd) ** member variable of the associated database file descriptor is set ** to point to the new file. A mutex protected linked list of all main ** db fds opened using a particular RBU VFS is maintained at ** rbu_vfs.pMain to facilitate this. ** ** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file ** object can be marked as the target database of an RBU update. This ** turns on the following extra special behaviour: ** ** 3a. If xAccess() is called to check if there exists a *-wal file ** associated with an RBU target database currently in RBU_STAGE_OAL ** stage (preparing the *-oal file), the following special handling ** applies: ** ** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU ** target database may not be in wal mode already. ** ** * if the *-wal file does not exist, set the output parameter to ** non-zero (to tell SQLite that it does exist) anyway. ** ** Then, when xOpen() is called to open the *-wal file associated with ** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal ** file, the rbu vfs opens the corresponding *-oal file instead. ** ** 3b. The *-shm pages returned by xShmMap() for a target db file in ** RBU_STAGE_OAL mode are actually stored in heap memory. This is to ** avoid creating a *-shm file on disk. Additionally, xShmLock() calls ** are no-ops on target database files in RBU_STAGE_OAL mode. This is ** because assert() statements in some VFS implementations fail if ** xShmLock() is called before xShmMap(). ** ** 3c. If an EXCLUSIVE lock is attempted on a target database file in any ** mode except RBU_STAGE_DONE (all work completed and checkpointed), it ** fails with an SQLITE_BUSY error. This is to stop RBU connections ** from automatically checkpointing a *-wal (or *-oal) file from within ** sqlite3_close(). ** ** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and ** all xWrite() calls on the target database file perform no IO. ** Instead the frame and page numbers that would be read and written ** are recorded. Additionally, successful attempts to obtain exclusive ** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target ** database file are recorded. xShmLock() calls to unlock the same ** locks are no-ops (so that once obtained, these locks are never ** relinquished). Finally, calls to xSync() on the target database ** file fail with SQLITE_NOTICE errors. */ func _rbuUnlockShm(tls *libc.TLS, p uintptr) { var i int32 var xShmLock uintptr _, _ = i, xShmLock _ = libc.Int32FromInt32(0) if (*Trbu_file)(unsafe.Pointer(p)).FpRbu != 0 { xShmLock = (*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(p)).FpReal)).FpMethods)).FxShmLock i = 0 for { if !(i < int32(SQLITE_SHM_NLOCK)) { break } if uint32(libc.Int32FromInt32(1)< (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FszTempLimit { return int32(SQLITE_FULL) } return SQLITE_OK } // C documentation // // /* // ** Add an item to the main-db lists, if it is not already present. // ** // ** There are two main-db lists. One for all file descriptors, and one // ** for all file descriptors with rbu_file.pDb!=0. If the argument has // ** rbu_file.pDb!=0, then it is assumed to already be present on the // ** main list and is only added to the pDb!=0 list. // */ func _rbuMainlistAdd(tls *libc.TLS, p uintptr) { var pIter, pRbuVfs uintptr _, _ = pIter, pRbuVfs pRbuVfs = (*Trbu_file)(unsafe.Pointer(p)).FpRbuVfs _ = libc.Int32FromInt32(0) Xsqlite3_mutex_enter(tls, (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).Fmutex) if (*Trbu_file)(unsafe.Pointer(p)).FpRbu == uintptr(0) { pIter = (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).FpMain for { if !(pIter != 0) { break } goto _1 _1: ; pIter = (*Trbu_file)(unsafe.Pointer(pIter)).FpMainNext } (*Trbu_file)(unsafe.Pointer(p)).FpMainNext = (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).FpMain (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).FpMain = p } else { pIter = (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).FpMainRbu for { if !(pIter != 0 && pIter != p) { break } goto _2 _2: ; pIter = (*Trbu_file)(unsafe.Pointer(pIter)).FpMainRbuNext } if pIter == uintptr(0) { (*Trbu_file)(unsafe.Pointer(p)).FpMainRbuNext = (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).FpMainRbu (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).FpMainRbu = p } } Xsqlite3_mutex_leave(tls, (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).Fmutex) } // C documentation // // /* // ** Remove an item from the main-db lists. // */ func _rbuMainlistRemove(tls *libc.TLS, p uintptr) { var pp uintptr _ = pp Xsqlite3_mutex_enter(tls, (*Trbu_vfs)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(p)).FpRbuVfs)).Fmutex) pp = (*Trbu_file)(unsafe.Pointer(p)).FpRbuVfs + 100 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != 0 && *(*uintptr)(unsafe.Pointer(pp)) != p) { break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 56 } if *(*uintptr)(unsafe.Pointer(pp)) != 0 { *(*uintptr)(unsafe.Pointer(pp)) = (*Trbu_file)(unsafe.Pointer(p)).FpMainNext } (*Trbu_file)(unsafe.Pointer(p)).FpMainNext = uintptr(0) pp = (*Trbu_file)(unsafe.Pointer(p)).FpRbuVfs + 104 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != 0 && *(*uintptr)(unsafe.Pointer(pp)) != p) { break } goto _2 _2: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 60 } if *(*uintptr)(unsafe.Pointer(pp)) != 0 { *(*uintptr)(unsafe.Pointer(pp)) = (*Trbu_file)(unsafe.Pointer(p)).FpMainRbuNext } (*Trbu_file)(unsafe.Pointer(p)).FpMainRbuNext = uintptr(0) Xsqlite3_mutex_leave(tls, (*Trbu_vfs)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(p)).FpRbuVfs)).Fmutex) } // C documentation // // /* // ** Given that zWal points to a buffer containing a wal file name passed to // ** either the xOpen() or xAccess() VFS method, search the main-db list for // ** a file-handle opened by the same database connection on the corresponding // ** database file. // ** // ** If parameter bRbu is true, only search for file-descriptors with // ** rbu_file.pDb!=0. // */ func _rbuFindMaindb(tls *libc.TLS, pRbuVfs uintptr, zWal uintptr, bRbu int32) (r uintptr) { var pDb uintptr _ = pDb Xsqlite3_mutex_enter(tls, (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).Fmutex) if bRbu != 0 { pDb = (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).FpMainRbu for { if !(pDb != 0 && (*Trbu_file)(unsafe.Pointer(pDb)).FzWal != zWal) { break } goto _1 _1: ; pDb = (*Trbu_file)(unsafe.Pointer(pDb)).FpMainRbuNext } } else { pDb = (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).FpMain for { if !(pDb != 0 && (*Trbu_file)(unsafe.Pointer(pDb)).FzWal != zWal) { break } goto _2 _2: ; pDb = (*Trbu_file)(unsafe.Pointer(pDb)).FpMainNext } } Xsqlite3_mutex_leave(tls, (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).Fmutex) return pDb } // C documentation // // /* // ** Close an rbu file. // */ func _rbuVfsClose(tls *libc.TLS, pFile uintptr) (r int32) { var i, rc int32 var p, pMeth uintptr _, _, _, _ = i, p, pMeth, rc p = pFile /* Free the contents of the apShm[] array. And the array itself. */ i = 0 for { if !(i < (*Trbu_file)(unsafe.Pointer(p)).FnShm) { break } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(p)).FapShm + uintptr(i)*4))) goto _1 _1: ; i++ } Xsqlite3_free(tls, (*Trbu_file)(unsafe.Pointer(p)).FapShm) (*Trbu_file)(unsafe.Pointer(p)).FapShm = uintptr(0) Xsqlite3_free(tls, (*Trbu_file)(unsafe.Pointer(p)).FzDel) if (*Trbu_file)(unsafe.Pointer(p)).FopenFlags&int32(SQLITE_OPEN_MAIN_DB) != 0 { pMeth = (*Tsqlite3_file)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(p)).FpReal)).FpMethods _rbuMainlistRemove(tls, p) _rbuUnlockShm(tls, p) if (*Tsqlite3_io_methods)(unsafe.Pointer(pMeth)).FiVersion > int32(1) && (*Tsqlite3_io_methods)(unsafe.Pointer(pMeth)).FxShmUnmap != 0 { (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer(pMeth)).FxShmUnmap})))(tls, (*Trbu_file)(unsafe.Pointer(p)).FpReal, 0) } } else { if (*Trbu_file)(unsafe.Pointer(p)).FopenFlags&int32(SQLITE_OPEN_DELETEONCLOSE) != 0 && (*Trbu_file)(unsafe.Pointer(p)).FpRbu != 0 { _rbuUpdateTempSize(tls, p, 0) } } _ = libc.Int32FromInt32(0) /* Close the underlying file handle */ rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(p)).FpReal)).FpMethods)).FxClose})))(tls, (*Trbu_file)(unsafe.Pointer(p)).FpReal) return rc } // C documentation // // /* // ** Read and return an unsigned 32-bit big-endian integer from the buffer // ** passed as the only argument. // */ func _rbuGetU32(tls *libc.TLS, aBuf uintptr) (r Tu32) { return uint32(*(*Tu8)(unsafe.Pointer(aBuf)))<> libc.Int32FromInt32(24) & uint32(0xFF)) *(*Tu8)(unsafe.Pointer(aBuf + 1)) = uint8(iVal >> libc.Int32FromInt32(16) & uint32(0xFF)) *(*Tu8)(unsafe.Pointer(aBuf + 2)) = uint8(iVal >> libc.Int32FromInt32(8) & uint32(0xFF)) *(*Tu8)(unsafe.Pointer(aBuf + 3)) = uint8(iVal >> libc.Int32FromInt32(0) & uint32(0xFF)) } func _rbuPutU16(tls *libc.TLS, aBuf uintptr, iVal Tu16) { *(*Tu8)(unsafe.Pointer(aBuf)) = uint8(int32(iVal) >> int32(8) & int32(0xFF)) *(*Tu8)(unsafe.Pointer(aBuf + 1)) = uint8(int32(iVal) >> 0 & int32(0xFF)) } // C documentation // // /* // ** Read data from an rbuVfs-file. // */ func _rbuVfsRead(tls *libc.TLS, pFile uintptr, zBuf uintptr, iAmt int32, iOfst Tsqlite_int64) (r int32) { var aBuf, p, pBuf, pFd, pRbu uintptr var iRoot Tu32 var rc, v1 int32 _, _, _, _, _, _, _, _ = aBuf, iRoot, p, pBuf, pFd, pRbu, rc, v1 p = pFile pRbu = (*Trbu_file)(unsafe.Pointer(p)).FpRbu if pRbu != 0 && (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FeStage == int32(RBU_STAGE_CAPTURE) { _ = libc.Int32FromInt32(0) rc = _rbuCaptureWalRead(tls, (*Trbu_file)(unsafe.Pointer(p)).FpRbu, iOfst, iAmt) } else { if pRbu != 0 && (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FeStage == int32(RBU_STAGE_OAL) && (*Trbu_file)(unsafe.Pointer(p)).FopenFlags&int32(SQLITE_OPEN_WAL) != 0 && iOfst >= (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FiOalSz { rc = SQLITE_OK libc.Xmemset(tls, zBuf, 0, uint32(iAmt)) } else { rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, Tsqlite3_int64) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(p)).FpReal)).FpMethods)).FxRead})))(tls, (*Trbu_file)(unsafe.Pointer(p)).FpReal, zBuf, iAmt, iOfst) /* If this is being called to read the first page of the target ** database as part of an rbu vacuum operation, synthesize the ** contents of the first page if it does not yet exist. Otherwise, ** SQLite will not check for a *-wal file. */ if pRbu != 0 && (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FzTarget == uintptr(0) && rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(2)< int32(100) { libc.Xmemset(tls, aBuf+100, 0, uint32(iAmt-int32(100))) _rbuPutU16(tls, aBuf+105, uint16(iAmt&int32(0xFFFF))) *(*Tu8)(unsafe.Pointer(aBuf + 100)) = uint8(0x0D) } } } } if rc == SQLITE_OK && iOfst == 0 && (*Trbu_file)(unsafe.Pointer(p)).FopenFlags&int32(SQLITE_OPEN_MAIN_DB) != 0 { /* These look like magic numbers. But they are stable, as they are part ** of the definition of the SQLite file format, which may not change. */ pBuf = zBuf (*Trbu_file)(unsafe.Pointer(p)).FiCookie = _rbuGetU32(tls, pBuf+24) (*Trbu_file)(unsafe.Pointer(p)).FiWriteVer = *(*Tu8)(unsafe.Pointer(pBuf + 19)) } } return rc } // C documentation // // /* // ** Write data to an rbuVfs-file. // */ func _rbuVfsWrite(tls *libc.TLS, pFile uintptr, zBuf uintptr, iAmt int32, iOfst Tsqlite_int64) (r int32) { var p, pBuf, pRbu uintptr var rc int32 var szNew Ti64 _, _, _, _, _ = p, pBuf, pRbu, rc, szNew p = pFile pRbu = (*Trbu_file)(unsafe.Pointer(p)).FpRbu if pRbu != 0 && (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FeStage == int32(RBU_STAGE_CAPTURE) { _ = libc.Int32FromInt32(0) rc = _rbuCaptureDbWrite(tls, (*Trbu_file)(unsafe.Pointer(p)).FpRbu, iOfst) } else { if pRbu != 0 { if (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FeStage == int32(RBU_STAGE_OAL) && (*Trbu_file)(unsafe.Pointer(p)).FopenFlags&int32(SQLITE_OPEN_WAL) != 0 && iOfst >= (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FiOalSz { (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FiOalSz = int64(iAmt) + iOfst } else { if (*Trbu_file)(unsafe.Pointer(p)).FopenFlags&int32(SQLITE_OPEN_DELETEONCLOSE) != 0 { szNew = int64(iAmt) + iOfst if szNew > (*Trbu_file)(unsafe.Pointer(p)).Fsz { rc = _rbuUpdateTempSize(tls, p, szNew) if rc != SQLITE_OK { return rc } } } } } rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, Tsqlite3_int64) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(p)).FpReal)).FpMethods)).FxWrite})))(tls, (*Trbu_file)(unsafe.Pointer(p)).FpReal, zBuf, iAmt, iOfst) if rc == SQLITE_OK && iOfst == 0 && (*Trbu_file)(unsafe.Pointer(p)).FopenFlags&int32(SQLITE_OPEN_MAIN_DB) != 0 { /* These look like magic numbers. But they are stable, as they are part ** of the definition of the SQLite file format, which may not change. */ pBuf = zBuf (*Trbu_file)(unsafe.Pointer(p)).FiCookie = _rbuGetU32(tls, pBuf+24) (*Trbu_file)(unsafe.Pointer(p)).FiWriteVer = *(*Tu8)(unsafe.Pointer(pBuf + 19)) } } return rc } // C documentation // // /* // ** Truncate an rbuVfs-file. // */ func _rbuVfsTruncate(tls *libc.TLS, pFile uintptr, size Tsqlite_int64) (r int32) { var p uintptr var rc int32 _, _ = p, rc p = pFile if (*Trbu_file)(unsafe.Pointer(p)).FopenFlags&int32(SQLITE_OPEN_DELETEONCLOSE) != 0 && (*Trbu_file)(unsafe.Pointer(p)).FpRbu != 0 { rc = _rbuUpdateTempSize(tls, p, size) if rc != SQLITE_OK { return rc } } return (*(*func(*libc.TLS, uintptr, Tsqlite3_int64) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods1)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(p)).FpReal)).FpMethods)).FxTruncate})))(tls, (*Trbu_file)(unsafe.Pointer(p)).FpReal, size) } // C documentation // // /* // ** Sync an rbuVfs-file. // */ func _rbuVfsSync(tls *libc.TLS, pFile uintptr, flags int32) (r int32) { var p uintptr _ = p p = pFile if (*Trbu_file)(unsafe.Pointer(p)).FpRbu != 0 && (*Tsqlite3rbu)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(p)).FpRbu)).FeStage == int32(RBU_STAGE_CAPTURE) { if (*Trbu_file)(unsafe.Pointer(p)).FopenFlags&int32(SQLITE_OPEN_MAIN_DB) != 0 { return libc.Int32FromInt32(SQLITE_NOTICE) | libc.Int32FromInt32(3)<zWal) to point to a buffer owned by SQLite that contains ** the name of the *-wal file this db connection will use. SQLite ** happens to pass a pointer to this buffer when using xAccess() ** or xOpen() to operate on the *-wal file. */ (*Trbu_file)(unsafe.Pointer(pFd)).FzWal = Xsqlite3_filename_wal(tls, zName) } else { if flags&int32(SQLITE_OPEN_WAL) != 0 { pDb = _rbuFindMaindb(tls, pRbuVfs, zName, 0) if pDb != 0 { if (*Trbu_file)(unsafe.Pointer(pDb)).FpRbu != 0 && (*Tsqlite3rbu)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(pDb)).FpRbu)).FeStage == int32(RBU_STAGE_OAL) { if (*Tsqlite3rbu)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(pDb)).FpRbu)).FzTarget == uintptr(0) { zOpen = Xsqlite3_db_filename(tls, (*Tsqlite3rbu)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(pDb)).FpRbu)).FdbRbu, __ccgo_ts+6585) zOpen = Xsqlite3_filename_wal(tls, zOpen) } nOpen = libc.Xstrlen(tls, zOpen) *(*uint8)(unsafe.Pointer(zOpen + uintptr(nOpen-uint32(3)))) = uint8('o') (*Trbu_file)(unsafe.Pointer(pFd)).FpRbu = (*Trbu_file)(unsafe.Pointer(pDb)).FpRbu } (*Trbu_file)(unsafe.Pointer(pDb)).FpWalFd = pFd } } } } else { (*Trbu_file)(unsafe.Pointer(pFd)).FpRbu = (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).FpRbu } if oflags&int32(SQLITE_OPEN_MAIN_DB) != 0 && Xsqlite3_uri_boolean(tls, zName, __ccgo_ts+33041, 0) != 0 { _ = libc.Int32FromInt32(0) oflags = libc.Int32FromInt32(SQLITE_OPEN_TEMP_DB) | libc.Int32FromInt32(SQLITE_OPEN_READWRITE) | libc.Int32FromInt32(SQLITE_OPEN_CREATE) | libc.Int32FromInt32(SQLITE_OPEN_EXCLUSIVE) | libc.Int32FromInt32(SQLITE_OPEN_DELETEONCLOSE) zOpen = uintptr(0) } if rc == SQLITE_OK { rc = (*(*func(*libc.TLS, uintptr, Tsqlite3_filename, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pRealVfs)).FxOpen})))(tls, pRealVfs, zOpen, (*Trbu_file)(unsafe.Pointer(pFd)).FpReal, oflags, pOutFlags) } if (*Tsqlite3_file)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(pFd)).FpReal)).FpMethods != 0 { pMeth = (*Tsqlite3_file)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(pFd)).FpReal)).FpMethods /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods ** pointer and, if the file is a main database file, link it into the ** mutex protected linked list of all such files. */ if (*Tsqlite3_io_methods)(unsafe.Pointer(pMeth)).FiVersion < int32(2) || (*Tsqlite3_io_methods)(unsafe.Pointer(pMeth)).FxShmLock == uintptr(0) { (*Tsqlite3_file)(unsafe.Pointer(pFile)).FpMethods = uintptr(unsafe.Pointer(&_rbuvfs_io_methods1)) } else { (*Tsqlite3_file)(unsafe.Pointer(pFile)).FpMethods = uintptr(unsafe.Pointer(&_rbuvfs_io_methods)) } if flags&int32(SQLITE_OPEN_MAIN_DB) != 0 { _rbuMainlistAdd(tls, pFd) } } else { Xsqlite3_free(tls, (*Trbu_file)(unsafe.Pointer(pFd)).FzDel) } return rc } var _rbuvfs_io_methods = Tsqlite3_io_methods{ FiVersion: int32(2), } func init() { p := unsafe.Pointer(&_rbuvfs_io_methods) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_rbuVfsClose) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_rbuVfsRead) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_rbuVfsWrite) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_rbuVfsTruncate) *(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_rbuVfsSync) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_rbuVfsFileSize) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_rbuVfsLock) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_rbuVfsUnlock) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_rbuVfsCheckReservedLock) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_rbuVfsFileControl) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_rbuVfsSectorSize) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_rbuVfsDeviceCharacteristics) *(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(_rbuVfsShmMap) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_rbuVfsShmLock) *(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_rbuVfsShmBarrier) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_rbuVfsShmUnmap) } var _rbuvfs_io_methods1 = Tsqlite3_io_methods{ FiVersion: int32(1), } func init() { p := unsafe.Pointer(&_rbuvfs_io_methods1) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_rbuVfsClose) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_rbuVfsRead) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_rbuVfsWrite) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_rbuVfsTruncate) *(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_rbuVfsSync) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_rbuVfsFileSize) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_rbuVfsLock) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_rbuVfsUnlock) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_rbuVfsCheckReservedLock) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_rbuVfsFileControl) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_rbuVfsSectorSize) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_rbuVfsDeviceCharacteristics) } // C documentation // // /* // ** Delete the file located at zPath. // */ func _rbuVfsDelete(tls *libc.TLS, pVfs uintptr, zPath uintptr, dirSync int32) (r int32) { var pRealVfs uintptr _ = pRealVfs pRealVfs = (*Trbu_vfs)(unsafe.Pointer(pVfs)).FpRealVfs return (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pRealVfs)).FxDelete})))(tls, pRealVfs, zPath, dirSync) } // C documentation // // /* // ** Test for access permissions. Return true if the requested permission // ** is available, or false otherwise. // */ func _rbuVfsAccess(tls *libc.TLS, pVfs uintptr, zPath uintptr, flags int32, pResOut uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var pDb, pRbuVfs, pRealVfs uintptr var rc int32 var _ /* sz at bp+0 */ Tsqlite3_int64 _, _, _, _ = pDb, pRbuVfs, pRealVfs, rc pRbuVfs = pVfs pRealVfs = (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).FpRealVfs rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pRealVfs)).FxAccess})))(tls, pRealVfs, zPath, flags, pResOut) /* If this call is to check if a *-wal file associated with an RBU target ** database connection exists, and the RBU update is in RBU_STAGE_OAL, ** the following special handling is activated: ** ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This ** ensures that the RBU extension never tries to update a database ** in wal mode, even if the first page of the database file has ** been damaged. ** ** b) if the *-wal file does not exist, claim that it does anyway, ** causing SQLite to call xOpen() to open it. This call will also ** be intercepted (see the rbuVfsOpen() function) and the *-oal ** file opened instead. */ if rc == SQLITE_OK && flags == SQLITE_ACCESS_EXISTS { pDb = _rbuFindMaindb(tls, pRbuVfs, zPath, int32(1)) if pDb != 0 && (*Tsqlite3rbu)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(pDb)).FpRbu)).FeStage == int32(RBU_STAGE_OAL) { _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(pResOut)) != 0 { rc = int32(SQLITE_CANTOPEN) } else { *(*Tsqlite3_int64)(unsafe.Pointer(bp)) = 0 rc = _rbuVfsFileSize(tls, pDb, bp) *(*int32)(unsafe.Pointer(pResOut)) = libc.BoolInt32(*(*Tsqlite3_int64)(unsafe.Pointer(bp)) > 0) } } } return rc } // C documentation // // /* // ** Populate buffer zOut with the full canonical pathname corresponding // ** to the pathname in zPath. zOut is guaranteed to point to a buffer // ** of at least (DEVSYM_MAX_PATHNAME+1) bytes. // */ func _rbuVfsFullPathname(tls *libc.TLS, pVfs uintptr, zPath uintptr, nOut int32, zOut uintptr) (r int32) { var pRealVfs uintptr _ = pRealVfs pRealVfs = (*Trbu_vfs)(unsafe.Pointer(pVfs)).FpRealVfs return (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pRealVfs)).FxFullPathname})))(tls, pRealVfs, zPath, nOut, zOut) } // C documentation // // /* // ** Open the dynamic library located at zPath and return a handle. // */ func _rbuVfsDlOpen(tls *libc.TLS, pVfs uintptr, zPath uintptr) (r uintptr) { var pRealVfs uintptr _ = pRealVfs pRealVfs = (*Trbu_vfs)(unsafe.Pointer(pVfs)).FpRealVfs return (*(*func(*libc.TLS, uintptr, uintptr) uintptr)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pRealVfs)).FxDlOpen})))(tls, pRealVfs, zPath) } // C documentation // // /* // ** Populate the buffer zErrMsg (size nByte bytes) with a human readable // ** utf-8 string describing the most recent error encountered associated // ** with dynamic libraries. // */ func _rbuVfsDlError(tls *libc.TLS, pVfs uintptr, nByte int32, zErrMsg uintptr) { var pRealVfs uintptr _ = pRealVfs pRealVfs = (*Trbu_vfs)(unsafe.Pointer(pVfs)).FpRealVfs (*(*func(*libc.TLS, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pRealVfs)).FxDlError})))(tls, pRealVfs, nByte, zErrMsg) } // C documentation // // /* // ** Return a pointer to the symbol zSymbol in the dynamic library pHandle. // */ func _rbuVfsDlSym(tls *libc.TLS, pVfs uintptr, pArg uintptr, zSym uintptr) (r uintptr) { var pRealVfs uintptr _ = pRealVfs pRealVfs = (*Trbu_vfs)(unsafe.Pointer(pVfs)).FpRealVfs return (*(*func(*libc.TLS, uintptr, uintptr, uintptr) uintptr)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pRealVfs)).FxDlSym})))(tls, pRealVfs, pArg, zSym) } // C documentation // // /* // ** Close the dynamic library handle pHandle. // */ func _rbuVfsDlClose(tls *libc.TLS, pVfs uintptr, pHandle uintptr) { var pRealVfs uintptr _ = pRealVfs pRealVfs = (*Trbu_vfs)(unsafe.Pointer(pVfs)).FpRealVfs (*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pRealVfs)).FxDlClose})))(tls, pRealVfs, pHandle) } // C documentation // // /* // ** Populate the buffer pointed to by zBufOut with nByte bytes of // ** random data. // */ func _rbuVfsRandomness(tls *libc.TLS, pVfs uintptr, nByte int32, zBufOut uintptr) (r int32) { var pRealVfs uintptr _ = pRealVfs pRealVfs = (*Trbu_vfs)(unsafe.Pointer(pVfs)).FpRealVfs return (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pRealVfs)).FxRandomness})))(tls, pRealVfs, nByte, zBufOut) } // C documentation // // /* // ** Sleep for nMicro microseconds. Return the number of microseconds // ** actually slept. // */ func _rbuVfsSleep(tls *libc.TLS, pVfs uintptr, nMicro int32) (r int32) { var pRealVfs uintptr _ = pRealVfs pRealVfs = (*Trbu_vfs)(unsafe.Pointer(pVfs)).FpRealVfs return (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pRealVfs)).FxSleep})))(tls, pRealVfs, nMicro) } // C documentation // // /* // ** Return the current time as a Julian Day number in *pTimeOut. // */ func _rbuVfsCurrentTime(tls *libc.TLS, pVfs uintptr, pTimeOut uintptr) (r int32) { var pRealVfs uintptr _ = pRealVfs pRealVfs = (*Trbu_vfs)(unsafe.Pointer(pVfs)).FpRealVfs return (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pRealVfs)).FxCurrentTime})))(tls, pRealVfs, pTimeOut) } // C documentation // // /* // ** No-op. // */ func _rbuVfsGetLastError(tls *libc.TLS, pVfs uintptr, a int32, b uintptr) (r int32) { return 0 } // C documentation // // /* // ** Deregister and destroy an RBU vfs created by an earlier call to // ** sqlite3rbu_create_vfs(). // */ func Xsqlite3rbu_destroy_vfs(tls *libc.TLS, zName uintptr) { var pVfs uintptr _ = pVfs pVfs = Xsqlite3_vfs_find(tls, zName) if pVfs != 0 && (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxOpen == __ccgo_fp(_rbuVfsOpen) { Xsqlite3_mutex_free(tls, (*Trbu_vfs)(unsafe.Pointer(pVfs)).Fmutex) Xsqlite3_vfs_unregister(tls, pVfs) Xsqlite3_free(tls, pVfs) } } // C documentation // // /* // ** Create an RBU VFS named zName that accesses the underlying file-system // ** via existing VFS zParent. The new object is registered as a non-default // ** VFS with SQLite before returning. // */ func Xsqlite3rbu_create_vfs(tls *libc.TLS, zName uintptr, zParent uintptr) (r int32) { var nByte, nName Tsize_t var pNew, pParent, zSpace, v1 uintptr var rc int32 _, _, _, _, _, _, _ = nByte, nName, pNew, pParent, rc, zSpace, v1 pNew = uintptr(0) /* Newly allocated VFS */ rc = SQLITE_OK nName = libc.Xstrlen(tls, zName) nByte = uint32(108) + nName + uint32(1) pNew = Xsqlite3_malloc64(tls, uint64(nByte)) if pNew == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { /* Parent VFS */ libc.Xmemset(tls, pNew, 0, nByte) pParent = Xsqlite3_vfs_find(tls, zParent) if pParent == uintptr(0) { rc = int32(SQLITE_NOTFOUND) } else { libc.Xmemcpy(tls, pNew, uintptr(unsafe.Pointer(&_vfs_template)), uint32(88)) (*Trbu_vfs)(unsafe.Pointer(pNew)).Fbase.FmxPathname = (*Tsqlite3_vfs)(unsafe.Pointer(pParent)).FmxPathname (*Trbu_vfs)(unsafe.Pointer(pNew)).Fbase.FszOsFile = int32(uint32(64) + uint32((*Tsqlite3_vfs)(unsafe.Pointer(pParent)).FszOsFile)) (*Trbu_vfs)(unsafe.Pointer(pNew)).FpRealVfs = pParent v1 = pNew + 1*108 zSpace = v1 (*Trbu_vfs)(unsafe.Pointer(pNew)).Fbase.FzName = v1 libc.Xmemcpy(tls, zSpace, zName, nName) /* Allocate the mutex and register the new VFS (not as the default) */ (*Trbu_vfs)(unsafe.Pointer(pNew)).Fmutex = Xsqlite3_mutex_alloc(tls, int32(SQLITE_MUTEX_RECURSIVE)) if (*Trbu_vfs)(unsafe.Pointer(pNew)).Fmutex == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { rc = Xsqlite3_vfs_register(tls, pNew, 0) } } if rc != SQLITE_OK { Xsqlite3_mutex_free(tls, (*Trbu_vfs)(unsafe.Pointer(pNew)).Fmutex) Xsqlite3_free(tls, pNew) } } return rc } /* Template for VFS */ var _vfs_template = Tsqlite3_vfs{ FiVersion: int32(1), } func init() { p := unsafe.Pointer(&_vfs_template) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_rbuVfsOpen) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_rbuVfsDelete) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_rbuVfsAccess) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_rbuVfsFullPathname) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_rbuVfsDlOpen) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_rbuVfsDlError) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_rbuVfsDlSym) *(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(_rbuVfsDlClose) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_rbuVfsRandomness) *(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_rbuVfsSleep) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_rbuVfsCurrentTime) *(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_rbuVfsGetLastError) } // C documentation // // /* // ** Configure the aggregate temp file size limit for this RBU handle. // */ func Xsqlite3rbu_temp_size_limit(tls *libc.TLS, pRbu uintptr, n Tsqlite3_int64) (r Tsqlite3_int64) { if n >= 0 { (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FszTempLimit = n } return (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FszTempLimit } func Xsqlite3rbu_temp_size(tls *libc.TLS, pRbu uintptr) (r Tsqlite3_int64) { return (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FszTemp } /**************************************************************************/ /************** End of sqlite3rbu.c ******************************************/ /************** Begin file dbstat.c ******************************************/ /* ** 2010 July 12 ** ** 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 contains an implementation of the "dbstat" virtual table. ** ** The dbstat virtual table is used to extract low-level storage ** information from an SQLite database in order to implement the ** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script ** for an example implementation. ** ** Additional information is available on the "dbstat.html" page of the ** official SQLite documentation. */ /* #include "sqliteInt.h" ** Requires access to internal data structures ** */ /* ** The pager and btree modules arrange objects in memory so that there are ** always approximately 200 bytes of addressable memory following each page ** buffer. This way small buffer overreads caused by corrupt database pages ** do not cause undefined behaviour. This module pads each page buffer ** by the following number of bytes for the same purpose. */ // C documentation // // /* // ** Page paths: // ** // ** The value of the 'path' column describes the path taken from the // ** root-node of the b-tree structure to each page. The value of the // ** root-node path is '/'. // ** // ** The value of the path for the left-most child page of the root of // ** a b-tree is '/000/'. (Btrees store content ordered from left to right // ** so the pages to the left have smaller keys than the pages to the right.) // ** The next to left-most child of the root page is // ** '/001', and so on, each sibling page identified by a 3-digit hex // ** value. The children of the 451st left-most sibling have paths such // ** as '/1c2/000/, '/1c2/001/' etc. // ** // ** Overflow pages are specified by appending a '+' character and a // ** six-digit hexadecimal value to the path to the cell they are linked // ** from. For example, the three overflow pages in a chain linked from // ** the left-most cell of the 450th child of the root page are identified // ** by the paths: // ** // ** '/1c2/000+000000' // First page in overflow chain // ** '/1c2/000+000001' // Second page in overflow chain // ** '/1c2/000+000002' // Third page in overflow chain // ** // ** If the paths are sorted using the BINARY collation sequence, then // ** the overflow pages associated with a cell will appear earlier in the // ** sort-order than its child page: // ** // ** '/1c2/000/' // Left-most child of 451st child of root // */ var _zDbstatSchema = [258]uint8{'C', 'R', 'E', 'A', 'T', 'E', ' ', 'T', 'A', 'B', 'L', 'E', ' ', 'x', '(', ' ', 'n', 'a', 'm', 'e', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'T', 'E', 'X', 'T', ',', ' ', 'p', 'a', 't', 'h', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'T', 'E', 'X', 'T', ',', ' ', 'p', 'a', 'g', 'e', 'n', 'o', ' ', ' ', ' ', ' ', ' ', 'I', 'N', 'T', 'E', 'G', 'E', 'R', ',', ' ', 'p', 'a', 'g', 'e', 't', 'y', 'p', 'e', ' ', ' ', ' ', 'T', 'E', 'X', 'T', ',', ' ', 'n', 'c', 'e', 'l', 'l', ' ', ' ', ' ', ' ', ' ', ' ', 'I', 'N', 'T', 'E', 'G', 'E', 'R', ',', ' ', 'p', 'a', 'y', 'l', 'o', 'a', 'd', ' ', ' ', ' ', ' ', 'I', 'N', 'T', 'E', 'G', 'E', 'R', ',', ' ', 'u', 'n', 'u', 's', 'e', 'd', ' ', ' ', ' ', ' ', ' ', 'I', 'N', 'T', 'E', 'G', 'E', 'R', ',', ' ', 'm', 'x', '_', 'p', 'a', 'y', 'l', 'o', 'a', 'd', ' ', 'I', 'N', 'T', 'E', 'G', 'E', 'R', ',', ' ', 'p', 'g', 'o', 'f', 'f', 's', 'e', 't', ' ', ' ', ' ', 'I', 'N', 'T', 'E', 'G', 'E', 'R', ',', ' ', 'p', 'g', 's', 'i', 'z', 'e', ' ', ' ', ' ', ' ', ' ', 'I', 'N', 'T', 'E', 'G', 'E', 'R', ',', ' ', 's', 'c', 'h', 'e', 'm', 'a', ' ', ' ', ' ', ' ', ' ', 'T', 'E', 'X', 'T', ' ', 'H', 'I', 'D', 'D', 'E', 'N', ',', ' ', 'a', 'g', 'g', 'r', 'e', 'g', 'a', 't', 'e', ' ', ' ', 'B', 'O', 'O', 'L', 'E', 'A', 'N', ' ', 'H', 'I', 'D', 'D', 'E', 'N', ')'} // C documentation // // /* Forward reference to data structured used in this module */ type TStatTable = struct { Fbase Tsqlite3_vtab Fdb uintptr FiDb int32 } type StatTable = TStatTable type TStatCursor = struct { F__ccgo_align [0]uint32 Fbase Tsqlite3_vtab_cursor FpStmt uintptr FisEof Tu8 FisAgg Tu8 FiDb int32 FaPage [32]TStatPage FiPage int32 FiPageno Tu32 FzName uintptr FzPath uintptr FzPagetype uintptr FnPage int32 FnCell int32 FnMxPayload int32 FnUnused Ti64 FnPayload Ti64 FiOffset Ti64 FszPage Ti64 } type StatCursor = TStatCursor type TStatPage = struct { FiPgno Tu32 FaPg uintptr FiCell int32 FzPath uintptr Fflags Tu8 FnCell int32 FnUnused int32 FaCell uintptr FiRightChildPg Tu32 FnMxPayload int32 } type StatPage = TStatPage type TStatCell = struct { FnLocal int32 FiChildPg Tu32 FnOvfl int32 FaOvfl uintptr FnLastOvfl int32 FiOvfl int32 } type StatCell = TStatCell /* Size information for a single cell within a btree page */ type TStatCell1 = struct { FnLocal int32 FiChildPg Tu32 FnOvfl int32 FaOvfl uintptr FnLastOvfl int32 FiOvfl int32 } type StatCell1 = TStatCell1 /* Size information for a single btree page */ type TStatPage1 = struct { FiPgno Tu32 FaPg uintptr FiCell int32 FzPath uintptr Fflags Tu8 FnCell int32 FnUnused int32 FaCell uintptr FiRightChildPg Tu32 FnMxPayload int32 } type StatPage1 = TStatPage1 /* The cursor for scanning the dbstat virtual table */ type TStatCursor1 = struct { F__ccgo_align [0]uint32 Fbase Tsqlite3_vtab_cursor FpStmt uintptr FisEof Tu8 FisAgg Tu8 FiDb int32 FaPage [32]TStatPage FiPage int32 FiPageno Tu32 FzName uintptr FzPath uintptr FzPagetype uintptr FnPage int32 FnCell int32 FnMxPayload int32 FnUnused Ti64 FnPayload Ti64 FiOffset Ti64 FszPage Ti64 } type StatCursor1 = TStatCursor1 /* An instance of the DBSTAT virtual table */ type TStatTable1 = struct { Fbase Tsqlite3_vtab Fdb uintptr FiDb int32 } type StatTable1 = TStatTable1 // C documentation // // /* // ** Connect to or create a new DBSTAT virtual table. // */ func _statConnect(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var iDb, rc int32 var pTab uintptr var _ /* nm at bp+0 */ TToken _, _, _ = iDb, pTab, rc pTab = uintptr(0) rc = SQLITE_OK _ = pAux if argc >= int32(4) { _sqlite3TokenInit(tls, bp, *(*uintptr)(unsafe.Pointer(argv + 3*4))) iDb = _sqlite3FindDb(tls, db, bp) if iDb < 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+12191, libc.VaList(bp+16, *(*uintptr)(unsafe.Pointer(argv + 3*4)))) return int32(SQLITE_ERROR) } } else { iDb = 0 } Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_DIRECTONLY), 0) rc = Xsqlite3_declare_vtab(tls, db, uintptr(unsafe.Pointer(&_zDbstatSchema))) if rc == SQLITE_OK { pTab = Xsqlite3_malloc64(tls, uint64(20)) if pTab == uintptr(0) { rc = int32(SQLITE_NOMEM) } } _ = libc.Int32FromInt32(0) if rc == SQLITE_OK { libc.Xmemset(tls, pTab, 0, uint32(20)) (*TStatTable)(unsafe.Pointer(pTab)).Fdb = db (*TStatTable)(unsafe.Pointer(pTab)).FiDb = iDb } *(*uintptr)(unsafe.Pointer(ppVtab)) = pTab return rc } // C documentation // // /* // ** Disconnect from or destroy the DBSTAT virtual table. // */ func _statDisconnect(tls *libc.TLS, pVtab uintptr) (r int32) { Xsqlite3_free(tls, pVtab) return SQLITE_OK } // C documentation // // /* // ** Compute the best query strategy and return the result in idxNum. // ** // ** idxNum-Bit Meaning // ** ---------- ---------------------------------------------- // ** 0x01 There is a schema=? term in the WHERE clause // ** 0x02 There is a name=? term in the WHERE clause // ** 0x04 There is an aggregate=? term in the WHERE clause // ** 0x08 Output should be ordered by name and path // */ func _statBestIndex(tls *libc.TLS, tab uintptr, pIdxInfo uintptr) (r int32) { var i, iAgg, iName, iSchema, v2, v3, v4 int32 _, _, _, _, _, _, _ = i, iAgg, iName, iSchema, v2, v3, v4 iSchema = -int32(1) iName = -int32(1) iAgg = -int32(1) _ = tab /* Look for a valid schema=? constraint. If found, change the idxNum to ** 1 and request the value of that constraint be sent to xFilter. And ** lower the cost estimate to encourage the constrained version to be ** used. */ i = 0 for { if !(i < (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint) { break } if int32((*(*Tsqlite3_index_constraint)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(i)*12))).Fop) != int32(SQLITE_INDEX_CONSTRAINT_EQ) { goto _1 } if int32((*(*Tsqlite3_index_constraint)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(i)*12))).Fusable) == 0 { /* Force DBSTAT table should always be the right-most table in a join */ return int32(SQLITE_CONSTRAINT) } switch (*(*Tsqlite3_index_constraint)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(i)*12))).FiColumn { case 0: /* name */ iName = i case int32(10): /* schema */ iSchema = i case int32(11): /* aggregate */ iAgg = i break } goto _1 _1: ; i++ } i = 0 if iSchema >= 0 { i++ v2 = i (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(iSchema)*8))).FargvIndex = v2 (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(iSchema)*8))).Fomit = uint8(1) *(*int32)(unsafe.Pointer(pIdxInfo + 20)) |= int32(0x01) } if iName >= 0 { i++ v3 = i (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(iName)*8))).FargvIndex = v3 *(*int32)(unsafe.Pointer(pIdxInfo + 20)) |= int32(0x02) } if iAgg >= 0 { i++ v4 = i (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(iAgg)*8))).FargvIndex = v4 *(*int32)(unsafe.Pointer(pIdxInfo + 20)) |= int32(0x04) } (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = float64(1) /* Records are always returned in ascending order of (name, path). ** If this will satisfy the client, set the orderByConsumed flag so that ** SQLite does not do an external sort. */ if (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnOrderBy == int32(1) && (*(*Tsqlite3_index_orderby)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy))).FiColumn == 0 && int32((*(*Tsqlite3_index_orderby)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy))).Fdesc) == 0 || (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnOrderBy == int32(2) && (*(*Tsqlite3_index_orderby)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy))).FiColumn == 0 && int32((*(*Tsqlite3_index_orderby)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy))).Fdesc) == 0 && (*(*Tsqlite3_index_orderby)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy + 1*8))).FiColumn == int32(1) && int32((*(*Tsqlite3_index_orderby)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy + 1*8))).Fdesc) == 0 { (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).ForderByConsumed = int32(1) *(*int32)(unsafe.Pointer(pIdxInfo + 20)) |= int32(0x08) } return SQLITE_OK } // C documentation // // /* // ** Open a new DBSTAT cursor. // */ func _statOpen(tls *libc.TLS, pVTab uintptr, ppCursor uintptr) (r int32) { var pCsr, pTab uintptr _, _ = pCsr, pTab pTab = pVTab pCsr = Xsqlite3_malloc64(tls, uint64(1360)) if pCsr == uintptr(0) { return int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, pCsr, 0, uint32(1360)) (*TStatCursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab = pVTab (*TStatCursor)(unsafe.Pointer(pCsr)).FiDb = (*TStatTable)(unsafe.Pointer(pTab)).FiDb } *(*uintptr)(unsafe.Pointer(ppCursor)) = pCsr return SQLITE_OK } func _statClearCells(tls *libc.TLS, p uintptr) { var i int32 _ = i if (*TStatPage)(unsafe.Pointer(p)).FaCell != 0 { i = 0 for { if !(i < (*TStatPage)(unsafe.Pointer(p)).FnCell) { break } Xsqlite3_free(tls, (*(*TStatCell)(unsafe.Pointer((*TStatPage)(unsafe.Pointer(p)).FaCell + uintptr(i)*24))).FaOvfl) goto _1 _1: ; i++ } Xsqlite3_free(tls, (*TStatPage)(unsafe.Pointer(p)).FaCell) } (*TStatPage)(unsafe.Pointer(p)).FnCell = 0 (*TStatPage)(unsafe.Pointer(p)).FaCell = uintptr(0) } func _statClearPage(tls *libc.TLS, p uintptr) { var aPg uintptr _ = aPg aPg = (*TStatPage)(unsafe.Pointer(p)).FaPg _statClearCells(tls, p) Xsqlite3_free(tls, (*TStatPage)(unsafe.Pointer(p)).FzPath) libc.Xmemset(tls, p, 0, uint32(40)) (*TStatPage)(unsafe.Pointer(p)).FaPg = aPg } func _statResetCsr(tls *libc.TLS, pCsr uintptr) { var i int32 _ = i /* In some circumstances, specifically if an OOM has occurred, the call ** to sqlite3_reset() may cause the pager to be reset (emptied). It is ** important that statClearPage() is called to free any page refs before ** this happens. dbsqlfuzz 9ed3e4e3816219d3509d711636c38542bf3f40b1. */ i = 0 for { if !(i < int32(libc.Uint32FromInt64(1280)/libc.Uint32FromInt64(40))) { break } _statClearPage(tls, pCsr+16+uintptr(i)*40) Xsqlite3_free(tls, (*(*TStatPage)(unsafe.Pointer(pCsr + 16 + uintptr(i)*40))).FaPg) (*(*TStatPage)(unsafe.Pointer(pCsr + 16 + uintptr(i)*40))).FaPg = uintptr(0) goto _1 _1: ; i++ } Xsqlite3_reset(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt) (*TStatCursor)(unsafe.Pointer(pCsr)).FiPage = 0 Xsqlite3_free(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FzPath) (*TStatCursor)(unsafe.Pointer(pCsr)).FzPath = uintptr(0) (*TStatCursor)(unsafe.Pointer(pCsr)).FisEof = uint8(0) } // C documentation // // /* Resize the space-used counters inside of the cursor */ func _statResetCounts(tls *libc.TLS, pCsr uintptr) { (*TStatCursor)(unsafe.Pointer(pCsr)).FnCell = 0 (*TStatCursor)(unsafe.Pointer(pCsr)).FnMxPayload = 0 (*TStatCursor)(unsafe.Pointer(pCsr)).FnUnused = 0 (*TStatCursor)(unsafe.Pointer(pCsr)).FnPayload = 0 (*TStatCursor)(unsafe.Pointer(pCsr)).FszPage = 0 (*TStatCursor)(unsafe.Pointer(pCsr)).FnPage = 0 } // C documentation // // /* // ** Close a DBSTAT cursor. // */ func _statClose(tls *libc.TLS, pCursor uintptr) (r int32) { var pCsr uintptr _ = pCsr pCsr = pCursor _statResetCsr(tls, pCsr) Xsqlite3_finalize(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt) Xsqlite3_free(tls, pCsr) return SQLITE_OK } // C documentation // // /* // ** For a single cell on a btree page, compute the number of bytes of // ** content (payload) stored on that page. That is to say, compute the // ** number of bytes of content not found on overflow pages. // */ func _getLocalPayload(tls *libc.TLS, nUsable int32, flags Tu8, nTotal int32) (r int32) { var nLocal, nMaxLocal, nMinLocal int32 _, _, _ = nLocal, nMaxLocal, nMinLocal if int32(flags) == int32(0x0D) { /* Table leaf node */ nMinLocal = (nUsable-int32(12))*int32(32)/int32(255) - int32(23) nMaxLocal = nUsable - int32(35) } else { /* Index interior and leaf nodes */ nMinLocal = (nUsable-int32(12))*int32(32)/int32(255) - int32(23) nMaxLocal = (nUsable-int32(12))*int32(64)/int32(255) - int32(23) } nLocal = nMinLocal + (nTotal-nMinLocal)%(nUsable-int32(4)) if nLocal > nMaxLocal { nLocal = nMinLocal } return nLocal } // C documentation // // /* Populate the StatPage object with information about the all // ** cells found on the page currently under analysis. // */ func _statDecodePage(tls *libc.TLS, pBt uintptr, p uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var aData, aHdr, pCell uintptr var i, iNext, iOff, isLeaf, j, nHdr, nLocal, nOvfl, nUnused, nUsable, rc, szPage, v1, v4 int32 var iPrev Tu32 var v2 uint32 var _ /* dummy at bp+8 */ Tu64 var _ /* nPayload at bp+0 */ Tu32 var _ /* pPg at bp+16 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aData, aHdr, i, iNext, iOff, iPrev, isLeaf, j, nHdr, nLocal, nOvfl, nUnused, nUsable, pCell, rc, szPage, v1, v2, v4 aData = (*TStatPage)(unsafe.Pointer(p)).FaPg if (*TStatPage)(unsafe.Pointer(p)).FiPgno == uint32(1) { v1 = int32(100) } else { v1 = 0 } aHdr = aData + uintptr(v1) (*TStatPage)(unsafe.Pointer(p)).Fflags = *(*Tu8)(unsafe.Pointer(aHdr)) if int32((*TStatPage)(unsafe.Pointer(p)).Fflags) == int32(0x0A) || int32((*TStatPage)(unsafe.Pointer(p)).Fflags) == int32(0x0D) { isLeaf = int32(1) nHdr = int32(8) } else { if int32((*TStatPage)(unsafe.Pointer(p)).Fflags) == int32(0x05) || int32((*TStatPage)(unsafe.Pointer(p)).Fflags) == int32(0x02) { isLeaf = 0 nHdr = int32(12) } else { goto statPageIsCorrupt } } if (*TStatPage)(unsafe.Pointer(p)).FiPgno == uint32(1) { nHdr += int32(100) } (*TStatPage)(unsafe.Pointer(p)).FnCell = int32(*(*Tu8)(unsafe.Pointer(aHdr + 3)))<= szPage { goto statPageIsCorrupt } nUnused += int32(*(*Tu8)(unsafe.Pointer(aData + uintptr(iOff+int32(2)))))< 0 { goto statPageIsCorrupt } iOff = iNext } (*TStatPage)(unsafe.Pointer(p)).FnUnused = nUnused if isLeaf != 0 { v2 = uint32(0) } else { v2 = _sqlite3Get4byte(tls, aHdr+8) } (*TStatPage)(unsafe.Pointer(p)).FiRightChildPg = v2 if (*TStatPage)(unsafe.Pointer(p)).FnCell != 0 { /* Usable bytes per page */ _sqlite3BtreeEnter(tls, pBt) nUsable = szPage - _sqlite3BtreeGetReserveNoMutex(tls, pBt) _sqlite3BtreeLeave(tls, pBt) (*TStatPage)(unsafe.Pointer(p)).FaCell = Xsqlite3_malloc64(tls, uint64(uint32((*TStatPage)(unsafe.Pointer(p)).FnCell+libc.Int32FromInt32(1))*uint32(24))) if (*TStatPage)(unsafe.Pointer(p)).FaCell == uintptr(0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, (*TStatPage)(unsafe.Pointer(p)).FaCell, 0, uint32((*TStatPage)(unsafe.Pointer(p)).FnCell+libc.Int32FromInt32(1))*uint32(24)) i = 0 for { if !(i < (*TStatPage)(unsafe.Pointer(p)).FnCell) { break } pCell = (*TStatPage)(unsafe.Pointer(p)).FaCell + uintptr(i)*24 iOff = int32(*(*Tu8)(unsafe.Pointer(aData + uintptr(nHdr+i*int32(2)))))<= szPage { goto statPageIsCorrupt } if !(isLeaf != 0) { (*TStatCell)(unsafe.Pointer(pCell)).FiChildPg = _sqlite3Get4byte(tls, aData+uintptr(iOff)) iOff += int32(4) } if int32((*TStatPage)(unsafe.Pointer(p)).Fflags) == int32(0x05) { /* A table interior node. nPayload==0. */ } else { /* Bytes of payload stored locally */ if int32(*(*Tu8)(unsafe.Pointer(aData + uintptr(iOff)))) < int32(libc.Uint8FromInt32(0x80)) { *(*Tu32)(unsafe.Pointer(bp)) = uint32(*(*Tu8)(unsafe.Pointer(aData + uintptr(iOff)))) v4 = libc.Int32FromInt32(1) } else { v4 = int32(_sqlite3GetVarint32(tls, aData+uintptr(iOff), bp)) } iOff += int32(uint8(v4)) if int32((*TStatPage)(unsafe.Pointer(p)).Fflags) == int32(0x0D) { iOff += int32(_sqlite3GetVarint(tls, aData+uintptr(iOff), bp+8)) } if *(*Tu32)(unsafe.Pointer(bp)) > uint32((*TStatPage)(unsafe.Pointer(p)).FnMxPayload) { (*TStatPage)(unsafe.Pointer(p)).FnMxPayload = int32(*(*Tu32)(unsafe.Pointer(bp))) } nLocal = _getLocalPayload(tls, nUsable, (*TStatPage)(unsafe.Pointer(p)).Fflags, int32(*(*Tu32)(unsafe.Pointer(bp)))) if nLocal < 0 { goto statPageIsCorrupt } (*TStatCell)(unsafe.Pointer(pCell)).FnLocal = nLocal _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if *(*Tu32)(unsafe.Pointer(bp)) > uint32(nLocal) { nOvfl = int32((*(*Tu32)(unsafe.Pointer(bp)) - uint32(nLocal) + uint32(nUsable) - uint32(4) - uint32(1)) / uint32(nUsable-libc.Int32FromInt32(4))) if iOff+nLocal+int32(4) > nUsable || *(*Tu32)(unsafe.Pointer(bp)) > uint32(0x7fffffff) { goto statPageIsCorrupt } (*TStatCell)(unsafe.Pointer(pCell)).FnLastOvfl = int32(*(*Tu32)(unsafe.Pointer(bp)) - uint32(nLocal) - uint32((nOvfl-int32(1))*(nUsable-int32(4)))) (*TStatCell)(unsafe.Pointer(pCell)).FnOvfl = nOvfl (*TStatCell)(unsafe.Pointer(pCell)).FaOvfl = Xsqlite3_malloc64(tls, uint64(uint32(4)*uint32(nOvfl))) if (*TStatCell)(unsafe.Pointer(pCell)).FaOvfl == uintptr(0) { return int32(SQLITE_NOMEM) } *(*Tu32)(unsafe.Pointer((*TStatCell)(unsafe.Pointer(pCell)).FaOvfl)) = _sqlite3Get4byte(tls, aData+uintptr(iOff+nLocal)) j = int32(1) for { if !(j < nOvfl) { break } iPrev = *(*Tu32)(unsafe.Pointer((*TStatCell)(unsafe.Pointer(pCell)).FaOvfl + uintptr(j-int32(1))*4)) *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) rc = _sqlite3PagerGet(tls, _sqlite3BtreePager(tls, pBt), iPrev, bp+16, 0) if rc != SQLITE_OK { _ = libc.Int32FromInt32(0) return rc } *(*Tu32)(unsafe.Pointer((*TStatCell)(unsafe.Pointer(pCell)).FaOvfl + uintptr(j)*4)) = _sqlite3Get4byte(tls, _sqlite3PagerGetData(tls, *(*uintptr)(unsafe.Pointer(bp + 16)))) _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) goto _5 _5: ; j++ } } } goto _3 _3: ; i++ } } return SQLITE_OK goto statPageIsCorrupt statPageIsCorrupt: ; (*TStatPage)(unsafe.Pointer(p)).Fflags = uint8(0) _statClearCells(tls, p) return SQLITE_OK } // C documentation // // /* // ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on // ** the current value of pCsr->iPageno. // */ func _statSizeAndOffset(tls *libc.TLS, pCsr uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var fd, pBt, pPager, pTab uintptr var _ /* x at bp+0 */ [2]Tsqlite3_int64 _, _, _, _ = fd, pBt, pPager, pTab pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCsr)).FpVtab pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TStatTable)(unsafe.Pointer(pTab)).Fdb)).FaDb + uintptr((*TStatTable)(unsafe.Pointer(pTab)).FiDb)*16))).FpBt pPager = _sqlite3BtreePager(tls, pBt) /* If connected to a ZIPVFS backend, find the page size and ** offset from ZIPVFS. */ fd = _sqlite3PagerFile(tls, pPager) (*(*[2]Tsqlite3_int64)(unsafe.Pointer(bp)))[0] = int64((*TStatCursor)(unsafe.Pointer(pCsr)).FiPageno) if _sqlite3OsFileControl(tls, fd, int32(230440), bp) == SQLITE_OK { (*TStatCursor)(unsafe.Pointer(pCsr)).FiOffset = (*(*[2]Tsqlite3_int64)(unsafe.Pointer(bp)))[0] *(*Ti64)(unsafe.Pointer(pCsr + 1352)) += (*(*[2]Tsqlite3_int64)(unsafe.Pointer(bp)))[int32(1)] } else { /* Not ZIPVFS: The default page size and offset */ *(*Ti64)(unsafe.Pointer(pCsr + 1352)) += int64(_sqlite3BtreeGetPageSize(tls, pBt)) (*TStatCursor)(unsafe.Pointer(pCsr)).FiOffset = (*TStatCursor)(unsafe.Pointer(pCsr)).FszPage * int64((*TStatCursor)(unsafe.Pointer(pCsr)).FiPageno-libc.Uint32FromInt32(1)) } } // C documentation // // /* // ** Load a copy of the page data for page iPg into the buffer belonging // ** to page object pPg. Allocate the buffer if necessary. Return SQLITE_OK // ** if successful, or an SQLite error code otherwise. // */ func _statGetPage(tls *libc.TLS, pBt uintptr, iPg Tu32, pPg uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var a uintptr var pgsz, rc int32 var _ /* pDbPage at bp+0 */ uintptr _, _, _ = a, pgsz, rc pgsz = _sqlite3BtreeGetPageSize(tls, pBt) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) if (*TStatPage)(unsafe.Pointer(pPg)).FaPg == uintptr(0) { (*TStatPage)(unsafe.Pointer(pPg)).FaPg = Xsqlite3_malloc(tls, pgsz+int32(DBSTAT_PAGE_PADDING_BYTES)) if (*TStatPage)(unsafe.Pointer(pPg)).FaPg == uintptr(0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, (*TStatPage)(unsafe.Pointer(pPg)).FaPg+uintptr(pgsz), 0, uint32(DBSTAT_PAGE_PADDING_BYTES)) } rc = _sqlite3PagerGet(tls, _sqlite3BtreePager(tls, pBt), iPg, bp, 0) if rc == SQLITE_OK { a = _sqlite3PagerGetData(tls, *(*uintptr)(unsafe.Pointer(bp))) libc.Xmemcpy(tls, (*TStatPage)(unsafe.Pointer(pPg)).FaPg, a, uint32(pgsz)) _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp))) } return rc } // C documentation // // /* // ** Move a DBSTAT cursor to the next entry. Normally, the next // ** entry will be the next page, but in aggregated mode (pCsr->isAgg!=0), // ** the next entry is the next btree. // */ func _statNext(tls *libc.TLS, pCursor uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var i, iOvfl, nPayload, nUsable, rc, v3 int32 var iRoot Tu32 var p, p1, pBt, pCell, pCsr, pPager, pTab, z, v1, v2, v4, v5 uintptr var _ /* nPage at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, iOvfl, iRoot, nPayload, nUsable, p, p1, pBt, pCell, pCsr, pPager, pTab, rc, z, v1, v2, v3, v4, v5 pCsr = pCursor pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TStatTable)(unsafe.Pointer(pTab)).Fdb)).FaDb + uintptr((*TStatCursor)(unsafe.Pointer(pCsr)).FiDb)*16))).FpBt pPager = _sqlite3BtreePager(tls, pBt) Xsqlite3_free(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FzPath) (*TStatCursor)(unsafe.Pointer(pCsr)).FzPath = uintptr(0) goto statNextRestart statNextRestart: ; if (*TStatCursor)(unsafe.Pointer(pCsr)).FiPage < 0 { /* Start measuring space on the next btree */ _statResetCounts(tls, pCsr) rc = Xsqlite3_step(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt) if rc == int32(SQLITE_ROW) { iRoot = uint32(Xsqlite3_column_int64(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt, int32(1))) _sqlite3PagerPagecount(tls, pPager, bp) if *(*int32)(unsafe.Pointer(bp)) == 0 { (*TStatCursor)(unsafe.Pointer(pCsr)).FisEof = uint8(1) return Xsqlite3_reset(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt) } rc = _statGetPage(tls, pBt, iRoot, pCsr+16) (*(*TStatPage)(unsafe.Pointer(pCsr + 16))).FiPgno = iRoot (*(*TStatPage)(unsafe.Pointer(pCsr + 16))).FiCell = 0 if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) { v1 = Xsqlite3_mprintf(tls, __ccgo_ts+33052, 0) z = v1 (*(*TStatPage)(unsafe.Pointer(pCsr + 16))).FzPath = v1 if z == uintptr(0) { rc = int32(SQLITE_NOMEM) } } (*TStatCursor)(unsafe.Pointer(pCsr)).FiPage = 0 (*TStatCursor)(unsafe.Pointer(pCsr)).FnPage = int32(1) } else { (*TStatCursor)(unsafe.Pointer(pCsr)).FisEof = uint8(1) return Xsqlite3_reset(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt) } } else { /* Continue analyzing the btree previously started */ p = pCsr + 16 + uintptr((*TStatCursor)(unsafe.Pointer(pCsr)).FiPage)*40 if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) { _statResetCounts(tls, pCsr) } for (*TStatPage)(unsafe.Pointer(p)).FiCell < (*TStatPage)(unsafe.Pointer(p)).FnCell { pCell = (*TStatPage)(unsafe.Pointer(p)).FaCell + uintptr((*TStatPage)(unsafe.Pointer(p)).FiCell)*24 for (*TStatCell)(unsafe.Pointer(pCell)).FiOvfl < (*TStatCell)(unsafe.Pointer(pCell)).FnOvfl { _sqlite3BtreeEnter(tls, pBt) nUsable = _sqlite3BtreeGetPageSize(tls, pBt) - _sqlite3BtreeGetReserveNoMutex(tls, pBt) _sqlite3BtreeLeave(tls, pBt) (*TStatCursor)(unsafe.Pointer(pCsr)).FnPage++ _statSizeAndOffset(tls, pCsr) if (*TStatCell)(unsafe.Pointer(pCell)).FiOvfl < (*TStatCell)(unsafe.Pointer(pCell)).FnOvfl-int32(1) { *(*Ti64)(unsafe.Pointer(pCsr + 1336)) += int64(nUsable - int32(4)) } else { *(*Ti64)(unsafe.Pointer(pCsr + 1336)) += int64((*TStatCell)(unsafe.Pointer(pCell)).FnLastOvfl) *(*Ti64)(unsafe.Pointer(pCsr + 1328)) += int64(nUsable - int32(4) - (*TStatCell)(unsafe.Pointer(pCell)).FnLastOvfl) } iOvfl = (*TStatCell)(unsafe.Pointer(pCell)).FiOvfl (*TStatCell)(unsafe.Pointer(pCell)).FiOvfl++ if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) { (*TStatCursor)(unsafe.Pointer(pCsr)).FzName = Xsqlite3_column_text(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt, 0) (*TStatCursor)(unsafe.Pointer(pCsr)).FiPageno = *(*Tu32)(unsafe.Pointer((*TStatCell)(unsafe.Pointer(pCell)).FaOvfl + uintptr(iOvfl)*4)) (*TStatCursor)(unsafe.Pointer(pCsr)).FzPagetype = __ccgo_ts + 33054 v2 = Xsqlite3_mprintf(tls, __ccgo_ts+33063, libc.VaList(bp+16, (*TStatPage)(unsafe.Pointer(p)).FzPath, (*TStatPage)(unsafe.Pointer(p)).FiCell, iOvfl)) z = v2 (*TStatCursor)(unsafe.Pointer(pCsr)).FzPath = v2 if z == uintptr(0) { v3 = int32(SQLITE_NOMEM) } else { v3 = SQLITE_OK } return v3 } } if (*TStatPage)(unsafe.Pointer(p)).FiRightChildPg != 0 { break } (*TStatPage)(unsafe.Pointer(p)).FiCell++ } if !((*TStatPage)(unsafe.Pointer(p)).FiRightChildPg != 0) || (*TStatPage)(unsafe.Pointer(p)).FiCell > (*TStatPage)(unsafe.Pointer(p)).FnCell { _statClearPage(tls, p) (*TStatCursor)(unsafe.Pointer(pCsr)).FiPage-- if (*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0 && (*TStatCursor)(unsafe.Pointer(pCsr)).FiPage < 0 { /* label-statNext-done: When computing aggregate space usage over ** an entire btree, this is the exit point from this function */ return SQLITE_OK } goto statNextRestart /* Tail recursion */ } (*TStatCursor)(unsafe.Pointer(pCsr)).FiPage++ if (*TStatCursor)(unsafe.Pointer(pCsr)).FiPage >= int32(libc.Uint32FromInt64(1280)/libc.Uint32FromInt64(40)) { _statResetCsr(tls, pCsr) return _sqlite3CorruptError(tls, int32(222551)) } _ = libc.Int32FromInt32(0) if (*TStatPage)(unsafe.Pointer(p)).FiCell == (*TStatPage)(unsafe.Pointer(p)).FnCell { (*(*TStatPage)(unsafe.Pointer(p + 1*40))).FiPgno = (*TStatPage)(unsafe.Pointer(p)).FiRightChildPg } else { (*(*TStatPage)(unsafe.Pointer(p + 1*40))).FiPgno = (*(*TStatCell)(unsafe.Pointer((*TStatPage)(unsafe.Pointer(p)).FaCell + uintptr((*TStatPage)(unsafe.Pointer(p)).FiCell)*24))).FiChildPg } rc = _statGetPage(tls, pBt, (*(*TStatPage)(unsafe.Pointer(p + 1*40))).FiPgno, p+1*40) (*TStatCursor)(unsafe.Pointer(pCsr)).FnPage++ (*(*TStatPage)(unsafe.Pointer(p + 1*40))).FiCell = 0 if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) { v4 = Xsqlite3_mprintf(tls, __ccgo_ts+33075, libc.VaList(bp+16, (*TStatPage)(unsafe.Pointer(p)).FzPath, (*TStatPage)(unsafe.Pointer(p)).FiCell)) z = v4 (*(*TStatPage)(unsafe.Pointer(p + 1*40))).FzPath = v4 if z == uintptr(0) { rc = int32(SQLITE_NOMEM) } } (*TStatPage)(unsafe.Pointer(p)).FiCell++ } /* Populate the StatCursor fields with the values to be returned ** by the xColumn() and xRowid() methods. */ if rc == SQLITE_OK { p1 = pCsr + 16 + uintptr((*TStatCursor)(unsafe.Pointer(pCsr)).FiPage)*40 (*TStatCursor)(unsafe.Pointer(pCsr)).FzName = Xsqlite3_column_text(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt, 0) (*TStatCursor)(unsafe.Pointer(pCsr)).FiPageno = (*TStatPage)(unsafe.Pointer(p1)).FiPgno rc = _statDecodePage(tls, pBt, p1) if rc == SQLITE_OK { _statSizeAndOffset(tls, pCsr) switch int32((*TStatPage)(unsafe.Pointer(p1)).Fflags) { case int32(0x05): /* table internal */ fallthrough case int32(0x02): /* index internal */ (*TStatCursor)(unsafe.Pointer(pCsr)).FzPagetype = __ccgo_ts + 33083 case int32(0x0D): /* table leaf */ fallthrough case int32(0x0A): /* index leaf */ (*TStatCursor)(unsafe.Pointer(pCsr)).FzPagetype = __ccgo_ts + 33092 default: (*TStatCursor)(unsafe.Pointer(pCsr)).FzPagetype = __ccgo_ts + 33097 break } *(*int32)(unsafe.Pointer(pCsr + 1320)) += (*TStatPage)(unsafe.Pointer(p1)).FnCell *(*Ti64)(unsafe.Pointer(pCsr + 1328)) += int64((*TStatPage)(unsafe.Pointer(p1)).FnUnused) if (*TStatPage)(unsafe.Pointer(p1)).FnMxPayload > (*TStatCursor)(unsafe.Pointer(pCsr)).FnMxPayload { (*TStatCursor)(unsafe.Pointer(pCsr)).FnMxPayload = (*TStatPage)(unsafe.Pointer(p1)).FnMxPayload } if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) { v5 = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+16, (*TStatPage)(unsafe.Pointer(p1)).FzPath)) z = v5 (*TStatCursor)(unsafe.Pointer(pCsr)).FzPath = v5 if z == uintptr(0) { rc = int32(SQLITE_NOMEM) } } nPayload = 0 i = 0 for { if !(i < (*TStatPage)(unsafe.Pointer(p1)).FnCell) { break } nPayload += (*(*TStatCell)(unsafe.Pointer((*TStatPage)(unsafe.Pointer(p1)).FaCell + uintptr(i)*24))).FnLocal goto _6 _6: ; i++ } *(*Ti64)(unsafe.Pointer(pCsr + 1336)) += int64(nPayload) /* If computing aggregate space usage by btree, continue with the ** next page. The loop will exit via the return at label-statNext-done */ if (*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0 { goto statNextRestart } } } return rc } func _statEof(tls *libc.TLS, pCursor uintptr) (r int32) { var pCsr uintptr _ = pCsr pCsr = pCursor return int32((*TStatCursor)(unsafe.Pointer(pCsr)).FisEof) } // C documentation // // /* Initialize a cursor according to the query plan idxNum using the // ** arguments in argv[0]. See statBestIndex() for a description of the // ** meaning of the bits in idxNum. // */ func _statFilter(tls *libc.TLS, pCursor uintptr, idxNum int32, idxStr uintptr, argc int32, argv uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iArg, rc, v1, v2, v3 int32 var pCsr, pSql, pTab, zDbase, zName, zSql uintptr _, _, _, _, _, _, _, _, _, _, _ = iArg, pCsr, pSql, pTab, rc, zDbase, zName, zSql, v1, v2, v3 pCsr = pCursor pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab /* String value of pSql */ iArg = 0 /* Count of argv[] parameters used so far */ rc = SQLITE_OK /* Result of this operation */ zName = uintptr(0) /* Only provide analysis of this table */ _ = argc _ = idxStr _statResetCsr(tls, pCsr) Xsqlite3_finalize(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt) (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt = uintptr(0) if idxNum&int32(0x01) != 0 { v1 = iArg iArg++ /* schema=? constraint is present. Get its value */ zDbase = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(v1)*4))) (*TStatCursor)(unsafe.Pointer(pCsr)).FiDb = _sqlite3FindDbName(tls, (*TStatTable)(unsafe.Pointer(pTab)).Fdb, zDbase) if (*TStatCursor)(unsafe.Pointer(pCsr)).FiDb < 0 { (*TStatCursor)(unsafe.Pointer(pCsr)).FiDb = 0 (*TStatCursor)(unsafe.Pointer(pCsr)).FisEof = uint8(1) return SQLITE_OK } } else { (*TStatCursor)(unsafe.Pointer(pCsr)).FiDb = (*TStatTable)(unsafe.Pointer(pTab)).FiDb } if idxNum&int32(0x02) != 0 { /* name=? constraint is present */ v2 = iArg iArg++ zName = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(v2)*4))) } if idxNum&int32(0x04) != 0 { /* aggregate=? constraint is present */ v3 = iArg iArg++ (*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg = libc.BoolUint8(Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(v3)*4))) != float64(0)) } else { (*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg = uint8(0) } pSql = Xsqlite3_str_new(tls, (*TStatTable)(unsafe.Pointer(pTab)).Fdb) Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+33107, libc.VaList(bp+8, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TStatTable)(unsafe.Pointer(pTab)).Fdb)).FaDb + uintptr((*TStatCursor)(unsafe.Pointer(pCsr)).FiDb)*16))).FzDbSName)) if zName != 0 { Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+33262, libc.VaList(bp+8, zName)) } if idxNum&int32(0x08) != 0 { Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+33276, 0) } zSql = Xsqlite3_str_finish(tls, pSql) if zSql == uintptr(0) { return int32(SQLITE_NOMEM) } else { rc = Xsqlite3_prepare_v2(tls, (*TStatTable)(unsafe.Pointer(pTab)).Fdb, zSql, -int32(1), pCsr+4, uintptr(0)) Xsqlite3_free(tls, zSql) } if rc == SQLITE_OK { (*TStatCursor)(unsafe.Pointer(pCsr)).FiPage = -int32(1) rc = _statNext(tls, pCursor) } return rc } func _statColumn(tls *libc.TLS, pCursor uintptr, ctx uintptr, i int32) (r int32) { var db, pCsr uintptr var iDb int32 _, _, _ = db, iDb, pCsr pCsr = pCursor switch i { case 0: /* name */ Xsqlite3_result_text(tls, ctx, (*TStatCursor)(unsafe.Pointer(pCsr)).FzName, -int32(1), uintptr(-libc.Int32FromInt32(1))) case int32(1): /* path */ if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) { Xsqlite3_result_text(tls, ctx, (*TStatCursor)(unsafe.Pointer(pCsr)).FzPath, -int32(1), uintptr(-libc.Int32FromInt32(1))) } case int32(2): /* pageno */ if (*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0 { Xsqlite3_result_int64(tls, ctx, int64((*TStatCursor)(unsafe.Pointer(pCsr)).FnPage)) } else { Xsqlite3_result_int64(tls, ctx, int64((*TStatCursor)(unsafe.Pointer(pCsr)).FiPageno)) } case int32(3): /* pagetype */ if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) { Xsqlite3_result_text(tls, ctx, (*TStatCursor)(unsafe.Pointer(pCsr)).FzPagetype, -int32(1), libc.UintptrFromInt32(0)) } case int32(4): /* ncell */ Xsqlite3_result_int64(tls, ctx, int64((*TStatCursor)(unsafe.Pointer(pCsr)).FnCell)) case int32(5): /* payload */ Xsqlite3_result_int64(tls, ctx, (*TStatCursor)(unsafe.Pointer(pCsr)).FnPayload) case int32(6): /* unused */ Xsqlite3_result_int64(tls, ctx, (*TStatCursor)(unsafe.Pointer(pCsr)).FnUnused) case int32(7): /* mx_payload */ Xsqlite3_result_int64(tls, ctx, int64((*TStatCursor)(unsafe.Pointer(pCsr)).FnMxPayload)) case int32(8): /* pgoffset */ if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) { Xsqlite3_result_int64(tls, ctx, (*TStatCursor)(unsafe.Pointer(pCsr)).FiOffset) } case int32(9): /* pgsize */ Xsqlite3_result_int64(tls, ctx, (*TStatCursor)(unsafe.Pointer(pCsr)).FszPage) case int32(10): /* schema */ db = Xsqlite3_context_db_handle(tls, ctx) iDb = (*TStatCursor)(unsafe.Pointer(pCsr)).FiDb Xsqlite3_result_text(tls, ctx, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, -int32(1), libc.UintptrFromInt32(0)) default: /* aggregate */ Xsqlite3_result_int(tls, ctx, int32((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg)) break } return SQLITE_OK } func _statRowid(tls *libc.TLS, pCursor uintptr, pRowid uintptr) (r int32) { var pCsr uintptr _ = pCsr pCsr = pCursor *(*Tsqlite_int64)(unsafe.Pointer(pRowid)) = int64((*TStatCursor)(unsafe.Pointer(pCsr)).FiPageno) return SQLITE_OK } // C documentation // // /* // ** Invoke this routine to register the "dbstat" virtual table module // */ func _sqlite3DbstatRegister(tls *libc.TLS, db uintptr) (r int32) { return Xsqlite3_create_module(tls, db, __ccgo_ts+33291, uintptr(unsafe.Pointer(&_dbstat_module)), uintptr(0)) } var _dbstat_module = Tsqlite3_module{} func init() { p := unsafe.Pointer(&_dbstat_module) *(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_statConnect) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_statConnect) *(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_statBestIndex) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_statDisconnect) *(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_statDisconnect) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_statOpen) *(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_statClose) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_statFilter) *(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_statNext) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_statEof) *(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_statColumn) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_statRowid) } /************** End of dbstat.c **********************************************/ /************** Begin file dbpage.c ******************************************/ /* ** 2017-10-11 ** ** 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 contains an implementation of the "sqlite_dbpage" virtual table. ** ** The sqlite_dbpage virtual table is used to read or write whole raw ** pages of the database file. The pager interface is used so that ** uncommitted changes and changes recorded in the WAL file are correctly ** retrieved. ** ** Usage example: ** ** SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123; ** ** This is an eponymous virtual table so it does not need to be created before ** use. The optional argument to the sqlite_dbpage() table name is the ** schema for the database file that is to be read. The default schema is ** "main". ** ** The data field of sqlite_dbpage table can be updated. The new ** value must be a BLOB which is the correct page size, otherwise the ** update fails. Rows may not be deleted or inserted. */ /* #include "sqliteInt.h" ** Requires access to internal data structures ** */ /************** End of dbpage.c **********************************************/ /************** Begin file sqlite3session.c **********************************/ /* #include "sqlite3session.h" */ /* #include */ /* #include */ type TSessionTable = struct { FpNext uintptr FzName uintptr FnCol int32 FbStat1 int32 FbRowid int32 FazCol uintptr FazDflt uintptr FabPK uintptr FnEntry int32 FnChange int32 FapChange uintptr FpDfltStmt uintptr } type SessionTable = TSessionTable type TSessionChange = struct { Fop Tu8 FbIndirect Tu8 FnRecordField Tu16 FnMaxSize int32 FnRecord int32 FaRecord uintptr FpNext uintptr } type SessionChange = TSessionChange type TSessionBuffer = struct { FaBuf uintptr FnBuf int32 FnAlloc int32 } type SessionBuffer = TSessionBuffer type TSessionInput = struct { FbNoDiscard int32 FiCurrent int32 FiNext int32 FaData uintptr FnData int32 Fbuf TSessionBuffer FxInput uintptr FpIn uintptr FbEof int32 } type SessionInput = TSessionInput /* ** Minimum chunk size used by streaming versions of functions. */ var _sessions_strm_chunk_size = int32(SESSIONS_STRM_CHUNK_SIZE) type TSessionHook = struct { FpCtx uintptr FxOld uintptr FxNew uintptr FxCount uintptr FxDepth uintptr } type SessionHook = TSessionHook type TSessionHook1 = struct { FpCtx uintptr FxOld uintptr FxNew uintptr FxCount uintptr FxDepth uintptr } type SessionHook1 = TSessionHook1 /* ** Session handle structure. */ type Tsqlite3_session1 = struct { F__ccgo_align [0]uint32 Fdb uintptr FzDb uintptr FbEnableSize int32 FbEnable int32 FbIndirect int32 FbAutoAttach int32 FbImplicitPK int32 Frc int32 FpFilterCtx uintptr FxTableFilter uintptr FnMalloc Ti64 FnMaxChangesetSize Ti64 FpZeroBlob uintptr FpNext uintptr FpTable uintptr Fhook TSessionHook } type sqlite3_session1 = Tsqlite3_session1 /* ** Instances of this structure are used to build strings or binary records. */ type TSessionBuffer1 = struct { FaBuf uintptr FnBuf int32 FnAlloc int32 } type SessionBuffer1 = TSessionBuffer1 /* ** An object of this type is used internally as an abstraction for ** input data. Input data may be supplied either as a single large buffer ** (e.g. sqlite3changeset_start()) or using a stream function (e.g. ** sqlite3changeset_start_strm()). */ type TSessionInput1 = struct { FbNoDiscard int32 FiCurrent int32 FiNext int32 FaData uintptr FnData int32 Fbuf TSessionBuffer FxInput uintptr FpIn uintptr FbEof int32 } type SessionInput1 = TSessionInput1 /* ** Structure for changeset iterators. */ type Tsqlite3_changeset_iter1 = struct { Fin TSessionInput Ftblhdr TSessionBuffer FbPatchset int32 FbInvert int32 FbSkipEmpty int32 Frc int32 FpConflict uintptr FzTab uintptr FnCol int32 Fop int32 FbIndirect int32 FabPK uintptr FapValue uintptr } type sqlite3_changeset_iter1 = Tsqlite3_changeset_iter1 /* ** Each session object maintains a set of the following structures, one ** for each table the session object is monitoring. The structures are ** stored in a linked list starting at sqlite3_session.pTable. ** ** The keys of the SessionTable.aChange[] hash table are all rows that have ** been modified in any way since the session object was attached to the ** table. ** ** The data associated with each hash-table entry is a structure containing ** a subset of the initial values that the modified row contained at the ** start of the session. Or no initial values if the row was inserted. ** ** pDfltStmt: ** This is only used by the sqlite3changegroup_xxx() APIs, not by ** regular sqlite3_session objects. It is a SELECT statement that ** selects the default value for each table column. For example, ** if the table is ** ** CREATE TABLE xx(a DEFAULT 1, b, c DEFAULT 'abc') ** ** then this variable is the compiled version of: ** ** SELECT 1, NULL, 'abc' */ type TSessionTable1 = struct { FpNext uintptr FzName uintptr FnCol int32 FbStat1 int32 FbRowid int32 FazCol uintptr FazDflt uintptr FabPK uintptr FnEntry int32 FnChange int32 FapChange uintptr FpDfltStmt uintptr } type SessionTable1 = TSessionTable1 /* ** RECORD FORMAT: ** ** The following record format is similar to (but not compatible with) that ** used in SQLite database files. This format is used as part of the ** change-set binary format, and so must be architecture independent. ** ** Unlike the SQLite database record format, each field is self-contained - ** there is no separation of header and data. Each field begins with a ** single byte describing its type, as follows: ** ** 0x00: Undefined value. ** 0x01: Integer value. ** 0x02: Real value. ** 0x03: Text value. ** 0x04: Blob value. ** 0x05: SQL NULL value. ** ** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT ** and so on in sqlite3.h. For undefined and NULL values, the field consists ** only of the single type byte. For other types of values, the type byte ** is followed by: ** ** Text values: ** A varint containing the number of bytes in the value (encoded using ** UTF-8). Followed by a buffer containing the UTF-8 representation ** of the text value. There is no nul terminator. ** ** Blob values: ** A varint containing the number of bytes in the value, followed by ** a buffer containing the value itself. ** ** Integer values: ** An 8-byte big-endian integer value. ** ** Real values: ** An 8-byte big-endian IEEE 754-2008 real value. ** ** Varint values are encoded in the same way as varints in the SQLite ** record format. ** ** CHANGESET FORMAT: ** ** A changeset is a collection of DELETE, UPDATE and INSERT operations on ** one or more tables. Operations on a single table are grouped together, ** but may occur in any order (i.e. deletes, updates and inserts are all ** mixed together). ** ** Each group of changes begins with a table header: ** ** 1 byte: Constant 0x54 (capital 'T') ** Varint: Number of columns in the table. ** nCol bytes: 0x01 for PK columns, 0x00 otherwise. ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated. ** ** Followed by one or more changes to the table. ** ** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09). ** 1 byte: The "indirect-change" flag. ** old.* record: (delete and update only) ** new.* record: (insert and update only) ** ** The "old.*" and "new.*" records, if present, are N field records in the ** format described above under "RECORD FORMAT", where N is the number of ** columns in the table. The i'th field of each record is associated with ** the i'th column of the table, counting from left to right in the order ** in which columns were declared in the CREATE TABLE statement. ** ** The new.* record that is part of each INSERT change contains the values ** that make up the new row. Similarly, the old.* record that is part of each ** DELETE change contains the values that made up the row that was deleted ** from the database. In the changeset format, the records that are part ** of INSERT or DELETE changes never contain any undefined (type byte 0x00) ** fields. ** ** Within the old.* record associated with an UPDATE change, all fields ** associated with table columns that are not PRIMARY KEY columns and are ** not modified by the UPDATE change are set to "undefined". Other fields ** are set to the values that made up the row before the UPDATE that the ** change records took place. Within the new.* record, fields associated ** with table columns modified by the UPDATE change contain the new ** values. Fields associated with table columns that are not modified ** are set to "undefined". ** ** PATCHSET FORMAT: ** ** A patchset is also a collection of changes. It is similar to a changeset, ** but leaves undefined those fields that are not useful if no conflict ** resolution is required when applying the changeset. ** ** Each group of changes begins with a table header: ** ** 1 byte: Constant 0x50 (capital 'P') ** Varint: Number of columns in the table. ** nCol bytes: 0x01 for PK columns, 0x00 otherwise. ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated. ** ** Followed by one or more changes to the table. ** ** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09). ** 1 byte: The "indirect-change" flag. ** single record: (PK fields for DELETE, PK and modified fields for UPDATE, ** full record for INSERT). ** ** As in the changeset format, each field of the single record that is part ** of a patchset change is associated with the correspondingly positioned ** table column, counting from left to right within the CREATE TABLE ** statement. ** ** For a DELETE change, all fields within the record except those associated ** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the ** values identifying the row to delete. ** ** For an UPDATE change, all fields except those associated with PRIMARY KEY ** columns and columns that are modified by the UPDATE are set to "undefined". ** PRIMARY KEY fields contain the values identifying the table row to update, ** and fields associated with modified columns contain the new column values. ** ** The records associated with INSERT changes are in the same format as for ** changesets. It is not possible for a record associated with an INSERT ** change to contain a field set to "undefined". ** ** REBASE BLOB FORMAT: ** ** A rebase blob may be output by sqlite3changeset_apply_v2() and its ** streaming equivalent for use with the sqlite3_rebaser APIs to rebase ** existing changesets. A rebase blob contains one entry for each conflict ** resolved using either the OMIT or REPLACE strategies within the apply_v2() ** call. ** ** The format used for a rebase blob is very similar to that used for ** changesets. All entries related to a single table are grouped together. ** ** Each group of entries begins with a table header in changeset format: ** ** 1 byte: Constant 0x54 (capital 'T') ** Varint: Number of columns in the table. ** nCol bytes: 0x01 for PK columns, 0x00 otherwise. ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated. ** ** Followed by one or more entries associated with the table. ** ** 1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09). ** 1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT. ** record: (in the record format defined above). ** ** In a rebase blob, the first field is set to SQLITE_INSERT if the change ** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if ** it was a DELETE. The second field is set to 0x01 if the conflict ** resolution strategy was REPLACE, or 0x00 if it was OMIT. ** ** If the change that caused the conflict was a DELETE, then the single ** record is a copy of the old.* record from the original changeset. If it ** was an INSERT, then the single record is a copy of the new.* record. If ** the conflicting change was an UPDATE, then the single record is a copy ** of the new.* record with the PK fields filled in based on the original ** old.* record. */ /* ** For each row modified during a session, there exists a single instance of ** this structure stored in a SessionTable.aChange[] hash table. */ type TSessionChange1 = struct { Fop Tu8 FbIndirect Tu8 FnRecordField Tu16 FnMaxSize int32 FnRecord int32 FaRecord uintptr FpNext uintptr } type SessionChange1 = TSessionChange1 // C documentation // // /* // ** Write a varint with value iVal into the buffer at aBuf. Return the // ** number of bytes written. // */ func _sessionVarintPut(tls *libc.TLS, aBuf uintptr, iVal int32) (r int32) { var v1 int32 _ = v1 if uint32(iVal) < libc.Uint32FromInt32(0x80) { *(*Tu8)(unsafe.Pointer(aBuf)) = uint8(iVal) v1 = libc.Int32FromInt32(1) } else { v1 = _sqlite3PutVarint(tls, aBuf, uint64(iVal)) } return int32(uint8(v1)) } // C documentation // // /* // ** Return the number of bytes required to store value iVal as a varint. // */ func _sessionVarintLen(tls *libc.TLS, iVal int32) (r int32) { return _sqlite3VarintLen(tls, uint64(iVal)) } // C documentation // // /* // ** Read a varint value from aBuf[] into *piVal. Return the number of // ** bytes read. // */ func _sessionVarintGet(tls *libc.TLS, aBuf uintptr, piVal uintptr) (r int32) { var v1 int32 _ = v1 if int32(*(*Tu8)(unsafe.Pointer(aBuf))) < int32(libc.Uint8FromInt32(0x80)) { *(*int32)(unsafe.Pointer(piVal)) = int32(uint32(*(*Tu8)(unsafe.Pointer(aBuf)))) v1 = libc.Int32FromInt32(1) } else { v1 = int32(_sqlite3GetVarint32(tls, aBuf, piVal)) } return int32(uint8(v1)) } /* Load an unaligned and unsigned 32-bit integer */ // C documentation // // /* // ** Read a 64-bit big-endian integer value from buffer aRec[]. Return // ** the value read. // */ func _sessionGetI64(tls *libc.TLS, aRec uintptr) (r Tsqlite3_int64) { var x Tu64 var y Tu32 _, _ = x, y x = uint64(uint32(*(*Tu8)(unsafe.Pointer(aRec)))<> libc.Int32FromInt32(56) & int64(0xFF)) *(*Tu8)(unsafe.Pointer(aBuf + 1)) = uint8(i >> libc.Int32FromInt32(48) & int64(0xFF)) *(*Tu8)(unsafe.Pointer(aBuf + 2)) = uint8(i >> libc.Int32FromInt32(40) & int64(0xFF)) *(*Tu8)(unsafe.Pointer(aBuf + 3)) = uint8(i >> libc.Int32FromInt32(32) & int64(0xFF)) *(*Tu8)(unsafe.Pointer(aBuf + 4)) = uint8(i >> libc.Int32FromInt32(24) & int64(0xFF)) *(*Tu8)(unsafe.Pointer(aBuf + 5)) = uint8(i >> libc.Int32FromInt32(16) & int64(0xFF)) *(*Tu8)(unsafe.Pointer(aBuf + 6)) = uint8(i >> libc.Int32FromInt32(8) & int64(0xFF)) *(*Tu8)(unsafe.Pointer(aBuf + 7)) = uint8(i >> libc.Int32FromInt32(0) & int64(0xFF)) } // C documentation // // /* // ** This function is used to serialize the contents of value pValue (see // ** comment titled "RECORD FORMAT" above). // ** // ** If it is non-NULL, the serialized form of the value is written to // ** buffer aBuf. *pnWrite is set to the number of bytes written before // ** returning. Or, if aBuf is NULL, the only thing this function does is // ** set *pnWrite. // ** // ** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs // ** within a call to sqlite3_value_text() (may fail if the db is utf-16)) // ** SQLITE_NOMEM is returned. // */ func _sessionSerializeValue(tls *libc.TLS, aBuf uintptr, pValue uintptr, pnWrite uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var eType, n, nByte, nVarint int32 var z uintptr var _ /* i at bp+0 */ Tu64 var _ /* r at bp+8 */ float64 _, _, _, _, _ = eType, n, nByte, nVarint, z /* Size of serialized value in bytes */ if pValue != 0 { /* Value type (SQLITE_NULL, TEXT etc.) */ eType = Xsqlite3_value_type(tls, pValue) if aBuf != 0 { *(*Tu8)(unsafe.Pointer(aBuf)) = uint8(eType) } switch eType { case int32(SQLITE_NULL): nByte = int32(1) case int32(SQLITE_INTEGER): fallthrough case int32(SQLITE_FLOAT): if aBuf != 0 { if eType == int32(SQLITE_INTEGER) { *(*Tu64)(unsafe.Pointer(bp)) = uint64(Xsqlite3_value_int64(tls, pValue)) } else { _ = libc.Int32FromInt32(0) *(*float64)(unsafe.Pointer(bp + 8)) = Xsqlite3_value_double(tls, pValue) libc.Xmemcpy(tls, bp, bp+8, uint32(8)) } _sessionPutI64(tls, aBuf+1, int64(*(*Tu64)(unsafe.Pointer(bp)))) } nByte = int32(9) default: _ = libc.Int32FromInt32(0) if eType == int32(SQLITE_TEXT) { z = Xsqlite3_value_text(tls, pValue) } else { z = Xsqlite3_value_blob(tls, pValue) } n = Xsqlite3_value_bytes(tls, pValue) if z == uintptr(0) && (eType != int32(SQLITE_BLOB) || n > 0) { return int32(SQLITE_NOMEM) } nVarint = _sessionVarintLen(tls, n) if aBuf != 0 { _sessionVarintPut(tls, aBuf+1, n) if n > 0 { libc.Xmemcpy(tls, aBuf+uintptr(nVarint+int32(1)), z, uint32(n)) } } nByte = int32(1) + nVarint + n break } } else { nByte = int32(1) if aBuf != 0 { *(*Tu8)(unsafe.Pointer(aBuf)) = uint8('\000') } } if pnWrite != 0 { *(*Tsqlite3_int64)(unsafe.Pointer(pnWrite)) += int64(nByte) } return SQLITE_OK } // C documentation // // /* // ** Allocate and return a pointer to a buffer nByte bytes in size. If // ** pSession is not NULL, increase the sqlite3_session.nMalloc variable // ** by the number of bytes allocated. // */ func _sessionMalloc64(tls *libc.TLS, pSession uintptr, nByte Ti64) (r uintptr) { var pRet, p1 uintptr _, _ = pRet, p1 pRet = Xsqlite3_malloc64(tls, uint64(nByte)) if pSession != 0 { p1 = pSession + 40 *(*Ti64)(unsafe.Pointer(p1)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(p1))) + Xsqlite3_msize(tls, pRet)) } return pRet } // C documentation // // /* // ** Free buffer pFree, which must have been allocated by an earlier // ** call to sessionMalloc64(). If pSession is not NULL, decrease the // ** sqlite3_session.nMalloc counter by the number of bytes freed. // */ func _sessionFree(tls *libc.TLS, pSession uintptr, pFree uintptr) { var p1 uintptr _ = p1 if pSession != 0 { p1 = pSession + 40 *(*Ti64)(unsafe.Pointer(p1)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(p1))) - Xsqlite3_msize(tls, pFree)) } Xsqlite3_free(tls, pFree) } /* ** This macro is used to calculate hash key values for data structures. In ** order to use this macro, the entire data structure must be represented ** as a series of unsigned integers. In order to calculate a hash-key value ** for a data structure represented as three such integers, the macro may ** then be used as follows: ** ** int hash_key_value; ** hash_key_value = HASH_APPEND(0, ); ** hash_key_value = HASH_APPEND(hash_key_value, ); ** hash_key_value = HASH_APPEND(hash_key_value, ); ** ** In practice, the data structures this macro is used for are the primary ** key values of modified rows. */ // C documentation // // /* // ** Append the hash of the 64-bit integer passed as the second argument to the // ** hash-key value passed as the first. Return the new hash-key value. // */ func _sessionHashAppendI64(tls *libc.TLS, h uint32, i Ti64) (r uint32) { h = h<>libc.Int32FromInt32(32)&libc.Int64FromUint32(0xFFFFFFFF)) } // C documentation // // /* // ** Append the hash of the blob passed via the second and third arguments to // ** the hash-key value passed as the first. Return the new hash-key value. // */ func _sessionHashAppendBlob(tls *libc.TLS, h uint32, n int32, z uintptr) (r uint32) { var i int32 _ = i i = 0 for { if !(i < n) { break } h = h< 0) { return int32(SQLITE_NOMEM) } h = _sessionHashAppendBlob(tls, h, n, z) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pbNullPK)) = int32(1) } } } goto _1 _1: ; i++ } } *(*int32)(unsafe.Pointer(piHash)) = int32(h % uint32((*TSessionTable)(unsafe.Pointer(pTab)).FnChange)) return SQLITE_OK } // C documentation // // /* // ** The buffer that the argument points to contains a serialized SQL value. // ** Return the number of bytes of space occupied by the value (including // ** the type byte). // */ func _sessionSerialLen(tls *libc.TLS, a uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var e int32 var _ /* n at bp+0 */ int32 _ = e _ = libc.Int32FromInt32(0) e = int32(*(*Tu8)(unsafe.Pointer(a))) if e == 0 || e == int32(0xFF) { return int32(1) } if e == int32(SQLITE_NULL) { return int32(1) } if e == int32(SQLITE_INTEGER) || e == int32(SQLITE_FLOAT) { return int32(9) } return _sessionVarintGet(tls, a+1, bp) + int32(1) + *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Based on the primary key values stored in change aRecord, calculate a // ** hash key. Assume the has table has nBucket buckets. The hash keys // ** calculated by this function are compatible with those calculated by // ** sessionPreupdateHash(). // ** // ** The bPkOnly argument is non-zero if the record at aRecord[] is from // ** a patchset DELETE. In this case the non-PK fields are omitted entirely. // */ func _sessionChangeHash(tls *libc.TLS, pTab uintptr, bPkOnly int32, aRecord uintptr, nBucket int32) (r uint32) { bp := tls.Alloc(16) defer tls.Free(16) var a uintptr var eType, i, isPK int32 var h uint32 var _ /* n at bp+0 */ int32 _, _, _, _, _ = a, eType, h, i, isPK h = uint32(0) /* Used to iterate through columns */ a = aRecord /* Used to iterate through change record */ i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } eType = int32(*(*Tu8)(unsafe.Pointer(a))) isPK = int32(*(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i)))) if bPkOnly != 0 && isPK == 0 { goto _1 } /* It is not possible for eType to be SQLITE_NULL here. The session ** module does not record changes for rows with NULL values stored in ** primary key columns. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if isPK != 0 { a++ h = _sessionHashAppendType(tls, h, eType) if eType == int32(SQLITE_INTEGER) || eType == int32(SQLITE_FLOAT) { h = _sessionHashAppendI64(tls, h, _sessionGetI64(tls, a)) a += uintptr(8) } else { a += uintptr(_sessionVarintGet(tls, a, bp)) h = _sessionHashAppendBlob(tls, h, *(*int32)(unsafe.Pointer(bp)), a) a += uintptr(*(*int32)(unsafe.Pointer(bp))) } } else { a += uintptr(_sessionSerialLen(tls, a)) } goto _1 _1: ; i++ } return h % uint32(nBucket) } // C documentation // // /* // ** Arguments aLeft and aRight are pointers to change records for table pTab. // ** This function returns true if the two records apply to the same row (i.e. // ** have the same values stored in the primary key columns), or false // ** otherwise. // */ func _sessionChangeEqual(tls *libc.TLS, pTab uintptr, bLeftPkOnly int32, aLeft uintptr, bRightPkOnly int32, aRight uintptr) (r int32) { var a1, a2 uintptr var iCol, n1, n2 int32 _, _, _, _, _ = a1, a2, iCol, n1, n2 a1 = aLeft /* Cursor to iterate through aLeft */ a2 = aRight /* Used to iterate through table columns */ iCol = 0 for { if !(iCol < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } if *(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(iCol))) != 0 { n1 = _sessionSerialLen(tls, a1) n2 = _sessionSerialLen(tls, a2) if n1 != n2 || libc.Xmemcmp(tls, a1, a2, uint32(n1)) != 0 { return 0 } a1 += uintptr(n1) a2 += uintptr(n2) } else { if bLeftPkOnly == 0 { a1 += uintptr(_sessionSerialLen(tls, a1)) } if bRightPkOnly == 0 { a2 += uintptr(_sessionSerialLen(tls, a2)) } } goto _1 _1: ; iCol++ } return int32(1) } // C documentation // // /* // ** Arguments aLeft and aRight both point to buffers containing change // ** records with nCol columns. This function "merges" the two records into // ** a single records which is written to the buffer at *paOut. *paOut is // ** then set to point to one byte after the last byte written before // ** returning. // ** // ** The merging of records is done as follows: For each column, if the // ** aRight record contains a value for the column, copy the value from // ** their. Otherwise, if aLeft contains a value, copy it. If neither // ** record contains a value for a given column, then neither does the // ** output record. // */ func _sessionMergeRecord(tls *libc.TLS, paOut uintptr, nCol int32, aLeft uintptr, aRight uintptr) { var a1, a2, aOut uintptr var iCol, n1, n2 int32 _, _, _, _, _, _ = a1, a2, aOut, iCol, n1, n2 a1 = aLeft /* Cursor used to iterate through aLeft */ a2 = aRight /* Cursor used to iterate through aRight */ aOut = *(*uintptr)(unsafe.Pointer(paOut)) /* Used to iterate from 0 to nCol */ iCol = 0 for { if !(iCol < nCol) { break } n1 = _sessionSerialLen(tls, a1) n2 = _sessionSerialLen(tls, a2) if *(*Tu8)(unsafe.Pointer(a2)) != 0 { libc.Xmemcpy(tls, aOut, a2, uint32(n2)) aOut += uintptr(n2) } else { libc.Xmemcpy(tls, aOut, a1, uint32(n1)) aOut += uintptr(n1) } a1 += uintptr(n1) a2 += uintptr(n2) goto _1 _1: ; iCol++ } *(*uintptr)(unsafe.Pointer(paOut)) = aOut } // C documentation // // /* // ** This is a helper function used by sessionMergeUpdate(). // ** // ** When this function is called, both *paOne and *paTwo point to a value // ** within a change record. Before it returns, both have been advanced so // ** as to point to the next value in the record. // ** // ** If, when this function is called, *paTwo points to a valid value (i.e. // ** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo // ** pointer is returned and *pnVal is set to the number of bytes in the // ** serialized value. Otherwise, a copy of *paOne is returned and *pnVal // ** set to the number of bytes in the value at *paOne. If *paOne points // ** to the "no value" placeholder, *pnVal is set to 1. In other words: // ** // ** if( *paTwo is valid ) return *paTwo; // ** return *paOne; // ** // */ func _sessionMergeValue(tls *libc.TLS, paOne uintptr, paTwo uintptr, pnVal uintptr) (r uintptr) { var a1, a2, pRet uintptr var n1, n2 int32 _, _, _, _, _ = a1, a2, n1, n2, pRet a1 = *(*uintptr)(unsafe.Pointer(paOne)) a2 = *(*uintptr)(unsafe.Pointer(paTwo)) pRet = uintptr(0) _ = libc.Int32FromInt32(0) if a2 != 0 { n2 = _sessionSerialLen(tls, a2) if *(*Tu8)(unsafe.Pointer(a2)) != 0 { *(*int32)(unsafe.Pointer(pnVal)) = n2 pRet = a2 } *(*uintptr)(unsafe.Pointer(paTwo)) = a2 + uintptr(n2) } n1 = _sessionSerialLen(tls, a1) if pRet == uintptr(0) { *(*int32)(unsafe.Pointer(pnVal)) = n1 pRet = a1 } *(*uintptr)(unsafe.Pointer(paOne)) = a1 + uintptr(n1) return pRet } // C documentation // // /* // ** This function is used by changeset_concat() to merge two UPDATE changes // ** on the same row. // */ func _sessionMergeUpdate(tls *libc.TLS, paOut uintptr, pTab uintptr, bPatchset int32, aOldRecord1 uintptr, aOldRecord2 uintptr, aNewRecord1 uintptr, aNewRecord2 uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var aNew, aNew1, aOld, aOld1, aOut, v2, v4 uintptr var bRequired, i int32 var _ /* aNew1 at bp+8 */ uintptr var _ /* aNew2 at bp+12 */ uintptr var _ /* aOld1 at bp+0 */ uintptr var _ /* aOld2 at bp+4 */ uintptr var _ /* nNew at bp+20 */ int32 var _ /* nNew at bp+28 */ int32 var _ /* nOld at bp+16 */ int32 var _ /* nOld at bp+24 */ int32 _, _, _, _, _, _, _, _, _ = aNew, aNew1, aOld, aOld1, aOut, bRequired, i, v2, v4 *(*uintptr)(unsafe.Pointer(bp)) = aOldRecord1 *(*uintptr)(unsafe.Pointer(bp + 4)) = aOldRecord2 *(*uintptr)(unsafe.Pointer(bp + 8)) = aNewRecord1 *(*uintptr)(unsafe.Pointer(bp + 12)) = aNewRecord2 aOut = *(*uintptr)(unsafe.Pointer(paOut)) if bPatchset == 0 { bRequired = 0 _ = libc.Int32FromInt32(0) /* Write the old.* vector first. */ i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } aOld = _sessionMergeValue(tls, bp, bp+4, bp+16) aNew = _sessionMergeValue(tls, bp+8, bp+12, bp+20) if *(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i))) != 0 || *(*int32)(unsafe.Pointer(bp + 16)) != *(*int32)(unsafe.Pointer(bp + 20)) || libc.Xmemcmp(tls, aOld, aNew, uint32(*(*int32)(unsafe.Pointer(bp + 20)))) != 0 { if int32(*(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i)))) == 0 { bRequired = int32(1) } libc.Xmemcpy(tls, aOut, aOld, uint32(*(*int32)(unsafe.Pointer(bp + 16)))) aOut += uintptr(*(*int32)(unsafe.Pointer(bp + 16))) } else { v2 = aOut aOut++ *(*Tu8)(unsafe.Pointer(v2)) = uint8('\000') } goto _1 _1: ; i++ } if !(bRequired != 0) { return 0 } } /* Write the new.* vector */ *(*uintptr)(unsafe.Pointer(bp)) = aOldRecord1 *(*uintptr)(unsafe.Pointer(bp + 4)) = aOldRecord2 *(*uintptr)(unsafe.Pointer(bp + 8)) = aNewRecord1 *(*uintptr)(unsafe.Pointer(bp + 12)) = aNewRecord2 i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } aOld1 = _sessionMergeValue(tls, bp, bp+4, bp+24) aNew1 = _sessionMergeValue(tls, bp+8, bp+12, bp+28) if bPatchset == 0 && (*(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i))) != 0 || *(*int32)(unsafe.Pointer(bp + 24)) == *(*int32)(unsafe.Pointer(bp + 28)) && 0 == libc.Xmemcmp(tls, aOld1, aNew1, uint32(*(*int32)(unsafe.Pointer(bp + 28))))) { v4 = aOut aOut++ *(*Tu8)(unsafe.Pointer(v4)) = uint8('\000') } else { libc.Xmemcpy(tls, aOut, aNew1, uint32(*(*int32)(unsafe.Pointer(bp + 28)))) aOut += uintptr(*(*int32)(unsafe.Pointer(bp + 28))) } goto _3 _3: ; i++ } *(*uintptr)(unsafe.Pointer(paOut)) = aOut return int32(1) } // C documentation // // /* // ** This function is only called from within a pre-update-hook callback. // ** It determines if the current pre-update-hook change affects the same row // ** as the change stored in argument pChange. If so, it returns true. Otherwise // ** if the pre-update-hook does not affect the same row as pChange, it returns // ** false. // */ func _sessionPreupdateEqual(tls *libc.TLS, pSession uintptr, iRowid Ti64, pTab uintptr, pChange uintptr, op int32) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var a, z, v2 uintptr var eType, iCol, rc int32 var _ /* iVal at bp+8 */ Ti64 var _ /* n at bp+24 */ int32 var _ /* pVal at bp+0 */ uintptr var _ /* rVal at bp+16 */ float64 _, _, _, _, _, _ = a, eType, iCol, rc, z, v2 /* Used to iterate through columns */ a = (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord /* Cursor used to scan change record */ if (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != 0 { if int32(*(*Tu8)(unsafe.Pointer(a))) != int32(SQLITE_INTEGER) { return 0 } return libc.BoolInt32(_sessionGetI64(tls, a+1) == iRowid) } _ = libc.Int32FromInt32(0) iCol = 0 for { if !(iCol < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } if !(*(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(iCol))) != 0) { a += uintptr(_sessionSerialLen(tls, a)) } else { v2 = a a++ /* Error code from preupdate_new/old */ eType = int32(*(*Tu8)(unsafe.Pointer(v2))) /* Type of value from change record */ /* The following calls to preupdate_new() and preupdate_old() can not ** fail. This is because they cache their return values, and by the ** time control flows to here they have already been called once from ** within sessionPreupdateHash(). The first two asserts below verify ** this (that the method has already been called). */ if op == int32(SQLITE_INSERT) { /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */ rc = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, iCol, bp) } else { /* assert( db->pPreUpdate->pUnpacked ); */ rc = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxOld})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, iCol, bp) } _ = libc.Int32FromInt32(0) _ = rc /* Suppress warning about unused variable */ if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(bp))) != eType { return 0 } /* A SessionChange object never has a NULL value in a PK column */ _ = libc.Int32FromInt32(0) if eType == int32(SQLITE_INTEGER) || eType == int32(SQLITE_FLOAT) { *(*Ti64)(unsafe.Pointer(bp + 8)) = _sessionGetI64(tls, a) a += uintptr(8) if eType == int32(SQLITE_INTEGER) { if Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(bp))) != *(*Ti64)(unsafe.Pointer(bp + 8)) { return 0 } } else { _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, bp+16, bp+8, uint32(8)) if Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(bp))) != *(*float64)(unsafe.Pointer(bp + 16)) { return 0 } } } else { a += uintptr(_sessionVarintGet(tls, a, bp+24)) if Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(bp))) != *(*int32)(unsafe.Pointer(bp + 24)) { return 0 } if eType == int32(SQLITE_TEXT) { z = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(bp))) } else { z = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(bp))) } if *(*int32)(unsafe.Pointer(bp + 24)) > 0 && libc.Xmemcmp(tls, a, z, uint32(*(*int32)(unsafe.Pointer(bp + 24)))) != 0 { return 0 } a += uintptr(*(*int32)(unsafe.Pointer(bp + 24))) } } goto _1 _1: ; iCol++ } return int32(1) } // C documentation // // /* // ** If required, grow the hash table used to store changes on table pTab // ** (part of the session pSession). If a fatal OOM error occurs, set the // ** session object to failed and return SQLITE_ERROR. Otherwise, return // ** SQLITE_OK. // ** // ** It is possible that a non-fatal OOM error occurs in this function. In // ** that case the hash-table does not grow, but SQLITE_OK is returned anyway. // ** Growing the hash table in this case is a performance optimization only, // ** it is not required for correct operation. // */ func _sessionGrowHash(tls *libc.TLS, pSession uintptr, bPatchset int32, pTab uintptr) (r int32) { var apNew, p, pNext uintptr var bPkOnly, i, iHash, v1 int32 var nNew Tsqlite3_int64 _, _, _, _, _, _, _, _ = apNew, bPkOnly, i, iHash, nNew, p, pNext, v1 if (*TSessionTable)(unsafe.Pointer(pTab)).FnChange == 0 || (*TSessionTable)(unsafe.Pointer(pTab)).FnEntry >= (*TSessionTable)(unsafe.Pointer(pTab)).FnChange/int32(2) { if (*TSessionTable)(unsafe.Pointer(pTab)).FnChange != 0 { v1 = (*TSessionTable)(unsafe.Pointer(pTab)).FnChange } else { v1 = int32(128) } nNew = int64(2) * int64(v1) apNew = _sessionMalloc64(tls, pSession, int64(4)*nNew) if apNew == uintptr(0) { if (*TSessionTable)(unsafe.Pointer(pTab)).FnChange == 0 { return int32(SQLITE_ERROR) } return SQLITE_OK } libc.Xmemset(tls, apNew, 0, uint32(int64(4)*nNew)) i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnChange) { break } p = *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(i)*4)) for { if !(p != 0) { break } bPkOnly = libc.BoolInt32(int32((*TSessionChange)(unsafe.Pointer(p)).Fop) == int32(SQLITE_DELETE) && bPatchset != 0) iHash = int32(_sessionChangeHash(tls, pTab, bPkOnly, (*TSessionChange)(unsafe.Pointer(p)).FaRecord, int32(nNew))) pNext = (*TSessionChange)(unsafe.Pointer(p)).FpNext (*TSessionChange)(unsafe.Pointer(p)).FpNext = *(*uintptr)(unsafe.Pointer(apNew + uintptr(iHash)*4)) *(*uintptr)(unsafe.Pointer(apNew + uintptr(iHash)*4)) = p goto _3 _3: ; p = pNext } goto _2 _2: ; i++ } _sessionFree(tls, pSession, (*TSessionTable)(unsafe.Pointer(pTab)).FapChange) (*TSessionTable)(unsafe.Pointer(pTab)).FnChange = int32(nNew) (*TSessionTable)(unsafe.Pointer(pTab)).FapChange = apNew } return SQLITE_OK } // C documentation // // /* // ** This function queries the database for the names of the columns of table // ** zThis, in schema zDb. // ** // ** Otherwise, if they are not NULL, variable *pnCol is set to the number // ** of columns in the database table and variable *pzTab is set to point to a // ** nul-terminated copy of the table name. *pazCol (if not NULL) is set to // ** point to an array of pointers to column names. And *pabPK (again, if not // ** NULL) is set to point to an array of booleans - true if the corresponding // ** column is part of the primary key. // ** // ** For example, if the table is declared as: // ** // ** CREATE TABLE tbl1(w, x DEFAULT 'abc', y, z, PRIMARY KEY(w, z)); // ** // ** Then the five output variables are populated as follows: // ** // ** *pnCol = 4 // ** *pzTab = "tbl1" // ** *pazCol = {"w", "x", "y", "z"} // ** *pazDflt = {NULL, 'abc', NULL, NULL} // ** *pabPK = {1, 0, 0, 1} // ** // ** All returned buffers are part of the same single allocation, which must // ** be freed using sqlite3_free() by the caller // */ func _sessionTableInfo(tls *libc.TLS, pSession uintptr, db uintptr, zDb uintptr, zThis uintptr, pnCol uintptr, pzTab uintptr, pazCol uintptr, pazDflt uintptr, pabPK uintptr, pbRowid uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var abPK, azCol, azDflt, pAlloc, zDflt, zName, zPragma uintptr var bRowid, i, nDbCol, nDflt, nName1, nThis, rc int32 var nByte Tsqlite3_int64 var nName Tsize_t var _ /* pStmt at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = abPK, azCol, azDflt, bRowid, i, nByte, nDbCol, nDflt, nName, nName1, nThis, pAlloc, rc, zDflt, zName, zPragma nDbCol = 0 pAlloc = uintptr(0) azCol = uintptr(0) azDflt = uintptr(0) abPK = uintptr(0) bRowid = 0 /* Set to true to use rowid as PK */ _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(pazCol)) = uintptr(0) *(*uintptr)(unsafe.Pointer(pabPK)) = uintptr(0) *(*int32)(unsafe.Pointer(pnCol)) = 0 if pzTab != 0 { *(*uintptr)(unsafe.Pointer(pzTab)) = uintptr(0) } if pazDflt != 0 { *(*uintptr)(unsafe.Pointer(pazDflt)) = uintptr(0) } nThis = _sqlite3Strlen30(tls, zThis) if nThis == int32(12) && 0 == Xsqlite3_stricmp(tls, __ccgo_ts+11582, zThis) { rc = Xsqlite3_table_column_metadata(tls, db, zDb, zThis, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0)) if rc == SQLITE_OK { /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */ zPragma = Xsqlite3_mprintf(tls, __ccgo_ts+33298, 0) } else { if rc == int32(SQLITE_ERROR) { zPragma = Xsqlite3_mprintf(tls, __ccgo_ts+1648, 0) } else { return rc } } } else { zPragma = Xsqlite3_mprintf(tls, __ccgo_ts+33419, libc.VaList(bp+16, zDb, zThis)) } if !(zPragma != 0) { return int32(SQLITE_NOMEM) } rc = Xsqlite3_prepare_v2(tls, db, zPragma, -int32(1), bp, uintptr(0)) Xsqlite3_free(tls, zPragma) if rc != SQLITE_OK { return rc } nByte = int64(nThis + int32(1)) bRowid = libc.BoolInt32(pbRowid != uintptr(0)) for int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { nByte += int64(Xsqlite3_column_bytes(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1))) /* name */ nByte += int64(Xsqlite3_column_bytes(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(4))) /* dflt_value */ nDbCol++ if Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(5)) != 0 { bRowid = 0 } /* pk */ } if nDbCol == 0 { bRowid = 0 } nDbCol += bRowid nByte += int64(libc.Xstrlen(tls, __ccgo_ts+29640)) rc = Xsqlite3_reset(tls, *(*uintptr)(unsafe.Pointer(bp))) if rc == SQLITE_OK { nByte += int64(uint32(nDbCol) * (libc.Uint32FromInt64(4)*libc.Uint32FromInt32(2) + libc.Uint32FromInt64(1) + libc.Uint32FromInt32(1) + libc.Uint32FromInt32(1))) pAlloc = _sessionMalloc64(tls, pSession, nByte) if pAlloc == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, pAlloc, 0, uint32(nByte)) } } if rc == SQLITE_OK { azCol = pAlloc azDflt = azCol + uintptr(nDbCol)*4 pAlloc = azDflt + uintptr(nDbCol)*4 abPK = pAlloc pAlloc = abPK + uintptr(nDbCol) if pzTab != 0 { libc.Xmemcpy(tls, pAlloc, zThis, uint32(nThis+int32(1))) *(*uintptr)(unsafe.Pointer(pzTab)) = pAlloc pAlloc += uintptr(nThis + int32(1)) } i = 0 if bRowid != 0 { nName = libc.Xstrlen(tls, __ccgo_ts+29640) libc.Xmemcpy(tls, pAlloc, __ccgo_ts+29640, nName+uint32(1)) *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*4)) = pAlloc pAlloc += uintptr(nName + uint32(1)) *(*Tu8)(unsafe.Pointer(abPK + uintptr(i))) = uint8(1) i++ } for int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { nName1 = Xsqlite3_column_bytes(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) nDflt = Xsqlite3_column_bytes(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(4)) zName = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) zDflt = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(4)) if zName == uintptr(0) { break } libc.Xmemcpy(tls, pAlloc, zName, uint32(nName1+int32(1))) *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*4)) = pAlloc pAlloc += uintptr(nName1 + int32(1)) if zDflt != 0 { libc.Xmemcpy(tls, pAlloc, zDflt, uint32(nDflt+int32(1))) *(*uintptr)(unsafe.Pointer(azDflt + uintptr(i)*4)) = pAlloc pAlloc += uintptr(nDflt + int32(1)) } else { *(*uintptr)(unsafe.Pointer(azDflt + uintptr(i)*4)) = uintptr(0) } *(*Tu8)(unsafe.Pointer(abPK + uintptr(i))) = uint8(Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(5))) i++ } rc = Xsqlite3_reset(tls, *(*uintptr)(unsafe.Pointer(bp))) } /* If successful, populate the output variables. Otherwise, zero them and ** free any allocation made. An error code will be returned in this case. */ if rc == SQLITE_OK { *(*uintptr)(unsafe.Pointer(pazCol)) = azCol if pazDflt != 0 { *(*uintptr)(unsafe.Pointer(pazDflt)) = azDflt } *(*uintptr)(unsafe.Pointer(pabPK)) = abPK *(*int32)(unsafe.Pointer(pnCol)) = nDbCol } else { _sessionFree(tls, pSession, azCol) } if pbRowid != 0 { *(*int32)(unsafe.Pointer(pbRowid)) = bRowid } Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) return rc } // C documentation // // /* // ** This function is called to initialize the SessionTable.nCol, azCol[] // ** abPK[] and azDflt[] members of SessionTable object pTab. If these // ** fields are already initilialized, this function is a no-op. // ** // ** If an error occurs, an error code is stored in sqlite3_session.rc and // ** non-zero returned. Or, if no error occurs but the table has no primary // ** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to // ** indicate that updates on this table should be ignored. SessionTable.abPK // ** is set to NULL in this case. // */ func _sessionInitTable(tls *libc.TLS, pSession uintptr, pTab uintptr, db uintptr, zDb uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, rc int32 var v1 uintptr var _ /* abPK at bp+0 */ uintptr _, _, _ = i, rc, v1 rc = SQLITE_OK if (*TSessionTable)(unsafe.Pointer(pTab)).FnCol == 0 { _ = libc.Int32FromInt32(0) if pSession == uintptr(0) || (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbImplicitPK != 0 { v1 = pTab + 16 } else { v1 = uintptr(0) } rc = _sessionTableInfo(tls, pSession, db, zDb, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, pTab+8, uintptr(0), pTab+20, pTab+24, bp, v1) if rc == SQLITE_OK { i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } if *(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)) + uintptr(i))) != 0 { (*TSessionTable)(unsafe.Pointer(pTab)).FabPK = *(*uintptr)(unsafe.Pointer(bp)) break } goto _2 _2: ; i++ } if 0 == Xsqlite3_stricmp(tls, __ccgo_ts+11582, (*TSessionTable)(unsafe.Pointer(pTab)).FzName) { (*TSessionTable)(unsafe.Pointer(pTab)).FbStat1 = int32(1) } if pSession != 0 && (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbEnableSize != 0 { *(*Ti64)(unsafe.Pointer(pSession + 48)) += int64(uint32(libc.Int32FromInt32(1)+_sessionVarintLen(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol)+(*TSessionTable)(unsafe.Pointer(pTab)).FnCol) + libc.Xstrlen(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName) + libc.Uint32FromInt32(1)) } } } if pSession != 0 { (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = rc return libc.BoolInt32(rc != 0 || (*TSessionTable)(unsafe.Pointer(pTab)).FabPK == uintptr(0)) } return rc } // C documentation // // /* // ** Re-initialize table object pTab. // */ func _sessionReinitTable(tls *libc.TLS, pSession uintptr, pTab uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var a, v1 uintptr var ii, nOldCol int32 var _ /* abPK at bp+12 */ uintptr var _ /* azCol at bp+4 */ uintptr var _ /* azDflt at bp+8 */ uintptr var _ /* bRowid at bp+16 */ int32 var _ /* nCol at bp+0 */ int32 _, _, _, _ = a, ii, nOldCol, v1 *(*int32)(unsafe.Pointer(bp)) = 0 *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 16)) = 0 _ = libc.Int32FromInt32(0) if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbImplicitPK != 0 { v1 = bp + 16 } else { v1 = uintptr(0) } (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = _sessionTableInfo(tls, pSession, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, bp, uintptr(0), bp+4, bp+8, bp+12, v1) if (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc == SQLITE_OK { if (*TSessionTable)(unsafe.Pointer(pTab)).FnCol > *(*int32)(unsafe.Pointer(bp)) || (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != *(*int32)(unsafe.Pointer(bp + 16)) { (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = int32(SQLITE_SCHEMA) } else { nOldCol = (*TSessionTable)(unsafe.Pointer(pTab)).FnCol ii = 0 for { if !(ii < *(*int32)(unsafe.Pointer(bp))) { break } if ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol { if int32(*(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(ii)))) != int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)) + uintptr(ii)))) { (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = int32(SQLITE_SCHEMA) } } else { if *(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)) + uintptr(ii))) != 0 { (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = int32(SQLITE_SCHEMA) } } goto _2 _2: ; ii++ } if (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc == SQLITE_OK { a = (*TSessionTable)(unsafe.Pointer(pTab)).FazCol (*TSessionTable)(unsafe.Pointer(pTab)).FazCol = *(*uintptr)(unsafe.Pointer(bp + 4)) (*TSessionTable)(unsafe.Pointer(pTab)).FnCol = *(*int32)(unsafe.Pointer(bp)) (*TSessionTable)(unsafe.Pointer(pTab)).FazDflt = *(*uintptr)(unsafe.Pointer(bp + 8)) (*TSessionTable)(unsafe.Pointer(pTab)).FabPK = *(*uintptr)(unsafe.Pointer(bp + 12)) *(*uintptr)(unsafe.Pointer(bp + 4)) = a } if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbEnableSize != 0 { *(*Ti64)(unsafe.Pointer(pSession + 48)) += int64(*(*int32)(unsafe.Pointer(bp)) - nOldCol) *(*Ti64)(unsafe.Pointer(pSession + 48)) += int64(_sessionVarintLen(tls, *(*int32)(unsafe.Pointer(bp)))) *(*Ti64)(unsafe.Pointer(pSession + 48)) -= int64(_sessionVarintLen(tls, nOldCol)) } } } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) return (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc } // C documentation // // /* // ** Session-change object (*pp) contains an old.* record with fewer than // ** nCol fields. This function updates it with the default values for // ** the missing fields. // */ func _sessionUpdateOneChange(tls *libc.TLS, pSession uintptr, pRc uintptr, pp uintptr, nCol int32, pDflt uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var eType, iField, n, n1, n2, nByte, nIncr, v1 int32 var iVal Ti64 var pNew, pOld, z, z1, v2, v3 uintptr var _ /* iVal at bp+8 */ Ti64 var _ /* rVal at bp+0 */ float64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = eType, iField, iVal, n, n1, n2, nByte, nIncr, pNew, pOld, z, z1, v1, v2, v3 pOld = *(*uintptr)(unsafe.Pointer(pp)) for int32((*TSessionChange)(unsafe.Pointer(pOld)).FnRecordField) < nCol { pNew = uintptr(0) nByte = 0 nIncr = 0 iField = int32((*TSessionChange)(unsafe.Pointer(pOld)).FnRecordField) eType = Xsqlite3_column_type(tls, pDflt, iField) switch eType { case int32(SQLITE_NULL): nIncr = int32(1) case int32(SQLITE_INTEGER): fallthrough case int32(SQLITE_FLOAT): nIncr = int32(9) default: n = Xsqlite3_column_bytes(tls, pDflt, iField) nIncr = int32(1) + _sessionVarintLen(tls, n) + n _ = libc.Int32FromInt32(0) break } nByte = int32(uint32(nIncr) + (uint32(20) + uint32((*TSessionChange)(unsafe.Pointer(pOld)).FnRecord))) pNew = _sessionMalloc64(tls, pSession, int64(nByte)) if pNew == uintptr(0) { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) return } else { libc.Xmemcpy(tls, pNew, pOld, uint32(20)) (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord = pNew + 1*20 libc.Xmemcpy(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord, (*TSessionChange)(unsafe.Pointer(pOld)).FaRecord, uint32((*TSessionChange)(unsafe.Pointer(pOld)).FnRecord)) v2 = pNew + 8 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ *(*Tu8)(unsafe.Pointer((*TSessionChange)(unsafe.Pointer(pNew)).FaRecord + uintptr(v1))) = uint8(eType) switch eType { case int32(SQLITE_INTEGER): iVal = Xsqlite3_column_int64(tls, pDflt, iField) _sessionPutI64(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), iVal) *(*int32)(unsafe.Pointer(pNew + 8)) += int32(8) case int32(SQLITE_FLOAT): *(*float64)(unsafe.Pointer(bp)) = Xsqlite3_column_double(tls, pDflt, iField) *(*Ti64)(unsafe.Pointer(bp + 8)) = 0 libc.Xmemcpy(tls, bp+8, bp, uint32(8)) _sessionPutI64(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), *(*Ti64)(unsafe.Pointer(bp + 8))) *(*int32)(unsafe.Pointer(pNew + 8)) += int32(8) case int32(SQLITE_TEXT): n1 = Xsqlite3_column_bytes(tls, pDflt, iField) z = Xsqlite3_column_text(tls, pDflt, iField) *(*int32)(unsafe.Pointer(pNew + 8)) += _sessionVarintPut(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), n1) libc.Xmemcpy(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), z, uint32(n1)) *(*int32)(unsafe.Pointer(pNew + 8)) += n1 case int32(SQLITE_BLOB): n2 = Xsqlite3_column_bytes(tls, pDflt, iField) z1 = Xsqlite3_column_blob(tls, pDflt, iField) *(*int32)(unsafe.Pointer(pNew + 8)) += _sessionVarintPut(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), n2) libc.Xmemcpy(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), z1, uint32(n2)) *(*int32)(unsafe.Pointer(pNew + 8)) += n2 default: _ = libc.Int32FromInt32(0) break } _sessionFree(tls, pSession, pOld) v3 = pNew pOld = v3 *(*uintptr)(unsafe.Pointer(pp)) = v3 (*TSessionChange)(unsafe.Pointer(pNew)).FnRecordField++ *(*int32)(unsafe.Pointer(pNew + 4)) += nIncr if pSession != 0 { *(*Ti64)(unsafe.Pointer(pSession + 48)) += int64(nIncr) } } } } // C documentation // // /* // ** Ensure that there is room in the buffer to append nByte bytes of data. // ** If not, use sqlite3_realloc() to grow the buffer so that there is. // ** // ** If successful, return zero. Otherwise, if an OOM condition is encountered, // ** set *pRc to SQLITE_NOMEM and return non-zero. // */ func _sessionBufferGrow(tls *libc.TLS, p uintptr, nByte Ti64, pRc uintptr) (r int32) { var aNew uintptr var nNew, nReq Ti64 var v1 int32 _, _, _, _ = aNew, nNew, nReq, v1 nReq = int64((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf) + nByte if *(*int32)(unsafe.Pointer(pRc)) == SQLITE_OK && nReq > int64((*TSessionBuffer)(unsafe.Pointer(p)).FnAlloc) { if (*TSessionBuffer)(unsafe.Pointer(p)).FnAlloc != 0 { v1 = (*TSessionBuffer)(unsafe.Pointer(p)).FnAlloc } else { v1 = int32(128) } nNew = int64(v1) for cond := true; cond; cond = nNew < nReq { nNew = nNew * int64(2) } /* The value of SESSION_MAX_BUFFER_SZ is copied from the implementation ** of sqlite3_realloc64(). Allocations greater than this size in bytes ** always fail. It is used here to ensure that this routine can always ** allocate up to this limit - instead of up to the largest power of ** two smaller than the limit. */ if nNew > int64(libc.Int32FromInt32(0x7FFFFF00)-libc.Int32FromInt32(1)) { nNew = int64(libc.Int32FromInt32(0x7FFFFF00) - libc.Int32FromInt32(1)) if nNew < nReq { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) return int32(1) } } aNew = Xsqlite3_realloc64(tls, (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf, uint64(nNew)) if uintptr(0) == aNew { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) } else { (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf = aNew (*TSessionBuffer)(unsafe.Pointer(p)).FnAlloc = int32(nNew) } } return libc.BoolInt32(*(*int32)(unsafe.Pointer(pRc)) != SQLITE_OK) } // C documentation // // /* // ** This function is a no-op if *pRc is other than SQLITE_OK when it is // ** called. Otherwise, append a string to the buffer. All bytes in the string // ** up to (but not including) the nul-terminator are written to the buffer. // ** // ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before // ** returning. // */ func _sessionAppendStr(tls *libc.TLS, p uintptr, zStr uintptr, pRc uintptr) { var nStr int32 _ = nStr nStr = _sqlite3Strlen30(tls, zStr) if 0 == _sessionBufferGrow(tls, p, int64(nStr+int32(1)), pRc) { libc.Xmemcpy(tls, (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf), zStr, uint32(nStr)) *(*int32)(unsafe.Pointer(p + 4)) += nStr *(*Tu8)(unsafe.Pointer((*TSessionBuffer)(unsafe.Pointer(p)).FaBuf + uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf))) = uint8(0x00) } } // C documentation // // /* // ** Format a string using printf() style formatting and then append it to the // ** buffer using sessionAppendString(). // */ func _sessionAppendPrintf(tls *libc.TLS, p uintptr, pRc uintptr, zFmt uintptr, va uintptr) { var ap Tva_list var zApp uintptr _, _ = ap, zApp if *(*int32)(unsafe.Pointer(pRc)) == SQLITE_OK { zApp = uintptr(0) ap = va zApp = Xsqlite3_vmprintf(tls, zFmt, ap) if zApp == uintptr(0) { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) } else { _sessionAppendStr(tls, p, zApp, pRc) } _ = ap Xsqlite3_free(tls, zApp) } } // C documentation // // /* // ** Prepare a statement against database handle db that SELECTs a single // ** row containing the default values for each column in table pTab. For // ** example, if pTab is declared as: // ** // ** CREATE TABLE pTab(a PRIMARY KEY, b DEFAULT 123, c DEFAULT 'abcd'); // ** // ** Then this function prepares and returns the SQL statement: // ** // ** SELECT NULL, 123, 'abcd'; // */ func _sessionPrepareDfltStmt(tls *libc.TLS, db uintptr, pTab uintptr, ppStmt uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var ii int32 var zDflt, zSep, v2 uintptr var _ /* rc at bp+12 */ int32 var _ /* sql at bp+0 */ TSessionBuffer _, _, _, _ = ii, zDflt, zSep, v2 *(*TSessionBuffer)(unsafe.Pointer(bp)) = TSessionBuffer{} *(*int32)(unsafe.Pointer(bp + 12)) = SQLITE_OK zSep = __ccgo_ts + 11202 ii = 0 *(*uintptr)(unsafe.Pointer(ppStmt)) = uintptr(0) _sessionAppendPrintf(tls, bp, bp+12, __ccgo_ts+33448, 0) ii = 0 for { if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } if *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FazDflt + uintptr(ii)*4)) != 0 { v2 = *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FazDflt + uintptr(ii)*4)) } else { v2 = __ccgo_ts + 1649 } zDflt = v2 _sessionAppendPrintf(tls, bp, bp+12, __ccgo_ts+5076, libc.VaList(bp+24, zSep, zDflt)) zSep = __ccgo_ts + 14990 goto _1 _1: ; ii++ } if *(*int32)(unsafe.Pointer(bp + 12)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 12)) = Xsqlite3_prepare_v2(tls, db, (*(*TSessionBuffer)(unsafe.Pointer(bp))).FaBuf, -int32(1), ppStmt, uintptr(0)) } Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp))).FaBuf) return *(*int32)(unsafe.Pointer(bp + 12)) } // C documentation // // /* // ** Table pTab has one or more existing change-records with old.* records // ** with fewer than pTab->nCol columns. This function updates all such // ** change-records with the default values for the missing columns. // */ func _sessionUpdateChanges(tls *libc.TLS, pSession uintptr, pTab uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var ii int32 var pp uintptr var _ /* pStmt at bp+0 */ uintptr var _ /* rc at bp+4 */ int32 _, _ = ii, pp *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 4)) = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc *(*int32)(unsafe.Pointer(bp + 4)) = _sessionPrepareDfltStmt(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb, pTab, bp) if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { ii = 0 pp = uintptr(0) ii = 0 for { if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnChange) { break } pp = (*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(ii)*4 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != 0) { break } if int32((*TSessionChange)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FnRecordField) != (*TSessionTable)(unsafe.Pointer(pTab)).FnCol { _sessionUpdateOneChange(tls, pSession, bp+4, pp, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, *(*uintptr)(unsafe.Pointer(bp))) } goto _2 _2: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 16 } goto _1 _1: ; ii++ } } (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = *(*int32)(unsafe.Pointer(bp + 4)) *(*int32)(unsafe.Pointer(bp + 4)) = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) if (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc == SQLITE_OK { (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = *(*int32)(unsafe.Pointer(bp + 4)) } return (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc } // C documentation // // /* // ** Versions of the four methods in object SessionHook for use with the // ** sqlite_stat1 table. The purpose of this is to substitute a zero-length // ** blob each time a NULL value is read from the "idx" column of the // ** sqlite_stat1 table. // */ type TSessionStat1Ctx = struct { Fhook TSessionHook FpSession uintptr } type SessionStat1Ctx = TSessionStat1Ctx type TSessionStat1Ctx1 = struct { Fhook TSessionHook FpSession uintptr } type SessionStat1Ctx1 = TSessionStat1Ctx1 func _sessionStat1Old(tls *libc.TLS, pCtx uintptr, iCol int32, ppVal uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var p uintptr var rc int32 var _ /* pVal at bp+0 */ uintptr _, _ = p, rc p = pCtx *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) rc = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TSessionStat1Ctx)(unsafe.Pointer(p)).Fhook.FxOld})))(tls, (*TSessionStat1Ctx)(unsafe.Pointer(p)).Fhook.FpCtx, iCol, bp) if rc == SQLITE_OK && iCol == int32(1) && Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(bp))) == int32(SQLITE_NULL) { *(*uintptr)(unsafe.Pointer(bp)) = (*Tsqlite3_session)(unsafe.Pointer((*TSessionStat1Ctx)(unsafe.Pointer(p)).FpSession)).FpZeroBlob } *(*uintptr)(unsafe.Pointer(ppVal)) = *(*uintptr)(unsafe.Pointer(bp)) return rc } func _sessionStat1New(tls *libc.TLS, pCtx uintptr, iCol int32, ppVal uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var p uintptr var rc int32 var _ /* pVal at bp+0 */ uintptr _, _ = p, rc p = pCtx *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) rc = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TSessionStat1Ctx)(unsafe.Pointer(p)).Fhook.FxNew})))(tls, (*TSessionStat1Ctx)(unsafe.Pointer(p)).Fhook.FpCtx, iCol, bp) if rc == SQLITE_OK && iCol == int32(1) && Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(bp))) == int32(SQLITE_NULL) { *(*uintptr)(unsafe.Pointer(bp)) = (*Tsqlite3_session)(unsafe.Pointer((*TSessionStat1Ctx)(unsafe.Pointer(p)).FpSession)).FpZeroBlob } *(*uintptr)(unsafe.Pointer(ppVal)) = *(*uintptr)(unsafe.Pointer(bp)) return rc } func _sessionStat1Count(tls *libc.TLS, pCtx uintptr) (r int32) { var p uintptr _ = p p = pCtx return (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TSessionStat1Ctx)(unsafe.Pointer(p)).Fhook.FxCount})))(tls, (*TSessionStat1Ctx)(unsafe.Pointer(p)).Fhook.FpCtx) } func _sessionStat1Depth(tls *libc.TLS, pCtx uintptr) (r int32) { var p uintptr _ = p p = pCtx return (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TSessionStat1Ctx)(unsafe.Pointer(p)).Fhook.FxDepth})))(tls, (*TSessionStat1Ctx)(unsafe.Pointer(p)).Fhook.FpCtx) } func _sessionUpdateMaxSize(tls *libc.TLS, op int32, pSession uintptr, pTab uintptr, pC uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var bChanged, eType, ii, ii1, nIncr, nOld int32 var pCsr, v3 uintptr var _ /* dVal at bp+24 */ float64 var _ /* iVal at bp+16 */ Tsqlite3_int64 var _ /* nByte at bp+32 */ int32 var _ /* nNew at bp+0 */ Ti64 var _ /* p at bp+12 */ uintptr var _ /* p at bp+8 */ uintptr _, _, _, _, _, _, _, _ = bChanged, eType, ii, ii1, nIncr, nOld, pCsr, v3 *(*Ti64)(unsafe.Pointer(bp)) = int64(2) if int32((*TSessionChange)(unsafe.Pointer(pC)).Fop) == int32(SQLITE_INSERT) { if (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != 0 { *(*Ti64)(unsafe.Pointer(bp)) += int64(9) } if op != int32(SQLITE_DELETE) { ii = 0 for { if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, ii, bp+8) _sessionSerializeValue(tls, uintptr(0), *(*uintptr)(unsafe.Pointer(bp + 8)), bp) goto _1 _1: ; ii++ } } } else { if op == int32(SQLITE_DELETE) { *(*Ti64)(unsafe.Pointer(bp)) += int64((*TSessionChange)(unsafe.Pointer(pC)).FnRecord) if Xsqlite3_preupdate_blobwrite(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb) >= 0 { *(*Ti64)(unsafe.Pointer(bp)) += int64((*TSessionChange)(unsafe.Pointer(pC)).FnRecord) } } else { pCsr = (*TSessionChange)(unsafe.Pointer(pC)).FaRecord if (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != 0 { *(*Ti64)(unsafe.Pointer(bp)) += int64(libc.Int32FromInt32(9) + libc.Int32FromInt32(1)) pCsr += uintptr(9) } ii1 = (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid for { if !(ii1 < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } bChanged = int32(1) nOld = 0 *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, ii1-(*TSessionTable)(unsafe.Pointer(pTab)).FbRowid, bp+12) if *(*uintptr)(unsafe.Pointer(bp + 12)) == uintptr(0) { return int32(SQLITE_NOMEM) } v3 = pCsr pCsr++ eType = int32(*(*Tu8)(unsafe.Pointer(v3))) switch eType { case int32(SQLITE_NULL): bChanged = libc.BoolInt32(Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) != int32(SQLITE_NULL)) case int32(SQLITE_FLOAT): fallthrough case int32(SQLITE_INTEGER): if eType == Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) { *(*Tsqlite3_int64)(unsafe.Pointer(bp + 16)) = _sessionGetI64(tls, pCsr) if eType == int32(SQLITE_INTEGER) { bChanged = libc.BoolInt32(*(*Tsqlite3_int64)(unsafe.Pointer(bp + 16)) != Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(bp + 12)))) } else { libc.Xmemcpy(tls, bp+24, bp+16, uint32(8)) bChanged = libc.BoolInt32(*(*float64)(unsafe.Pointer(bp + 24)) != Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(bp + 12)))) } } nOld = int32(8) pCsr += uintptr(8) default: nOld = _sessionVarintGet(tls, pCsr, bp+32) pCsr += uintptr(nOld) nOld += *(*int32)(unsafe.Pointer(bp + 32)) _ = libc.Int32FromInt32(0) if eType == Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) && *(*int32)(unsafe.Pointer(bp + 32)) == Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) && (*(*int32)(unsafe.Pointer(bp + 32)) == 0 || 0 == libc.Xmemcmp(tls, pCsr, Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(bp + 12))), uint32(*(*int32)(unsafe.Pointer(bp + 32))))) { bChanged = 0 } pCsr += uintptr(*(*int32)(unsafe.Pointer(bp + 32))) break } if bChanged != 0 && *(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(ii1))) != 0 { *(*Ti64)(unsafe.Pointer(bp)) = int64((*TSessionChange)(unsafe.Pointer(pC)).FnRecord + int32(2)) break } if bChanged != 0 { *(*Ti64)(unsafe.Pointer(bp)) += int64(int32(1) + nOld) _sessionSerializeValue(tls, uintptr(0), *(*uintptr)(unsafe.Pointer(bp + 12)), bp) } else { if *(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(ii1))) != 0 { *(*Ti64)(unsafe.Pointer(bp)) += int64(int32(2) + nOld) } else { *(*Ti64)(unsafe.Pointer(bp)) += int64(2) } } goto _2 _2: ; ii1++ } } } if *(*Ti64)(unsafe.Pointer(bp)) > int64((*TSessionChange)(unsafe.Pointer(pC)).FnMaxSize) { nIncr = int32(*(*Ti64)(unsafe.Pointer(bp)) - int64((*TSessionChange)(unsafe.Pointer(pC)).FnMaxSize)) (*TSessionChange)(unsafe.Pointer(pC)).FnMaxSize = int32(*(*Ti64)(unsafe.Pointer(bp))) *(*Ti64)(unsafe.Pointer(pSession + 48)) += int64(nIncr) } return SQLITE_OK } // C documentation // // /* // ** This function is only called from with a pre-update-hook reporting a // ** change on table pTab (attached to session pSession). The type of change // ** (UPDATE, INSERT, DELETE) is specified by the first argument. // ** // ** Unless one is already present or an error occurs, an entry is added // ** to the changed-rows hash table associated with table pTab. // */ func _sessionPreupdateOneChange(tls *libc.TLS, op int32, iRowid Ti64, pSession uintptr, pTab uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var i, nExpect, rc int32 var p, pC uintptr var _ /* bNull at bp+4 */ int32 var _ /* iHash at bp+0 */ int32 var _ /* nByte at bp+32 */ Tsqlite3_int64 var _ /* p at bp+40 */ uintptr var _ /* p at bp+44 */ uintptr var _ /* stat1 at bp+8 */ TSessionStat1Ctx _, _, _, _, _ = i, nExpect, p, pC, rc *(*int32)(unsafe.Pointer(bp + 4)) = 0 rc = SQLITE_OK nExpect = 0 *(*TSessionStat1Ctx)(unsafe.Pointer(bp + 8)) = TSessionStat1Ctx{} if (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc != 0 { return } /* Load table details if required */ if _sessionInitTable(tls, pSession, pTab, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb) != 0 { return } /* Check the number of columns in this xPreUpdate call matches the ** number of columns in the table. */ nExpect = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxCount})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx) if (*TSessionTable)(unsafe.Pointer(pTab)).FnCol-(*TSessionTable)(unsafe.Pointer(pTab)).FbRowid < nExpect { if _sessionReinitTable(tls, pSession, pTab) != 0 { return } if _sessionUpdateChanges(tls, pSession, pTab) != 0 { return } } if (*TSessionTable)(unsafe.Pointer(pTab)).FnCol-(*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != nExpect { (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = int32(SQLITE_SCHEMA) return } /* Grow the hash table if required */ if _sessionGrowHash(tls, pSession, 0, pTab) != 0 { (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = int32(SQLITE_NOMEM) return } if (*TSessionTable)(unsafe.Pointer(pTab)).FbStat1 != 0 { (*(*TSessionStat1Ctx)(unsafe.Pointer(bp + 8))).Fhook = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook (*(*TSessionStat1Ctx)(unsafe.Pointer(bp + 8))).FpSession = pSession (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx = bp + 8 (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew = __ccgo_fp(_sessionStat1New) (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxOld = __ccgo_fp(_sessionStat1Old) (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxCount = __ccgo_fp(_sessionStat1Count) (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxDepth = __ccgo_fp(_sessionStat1Depth) if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpZeroBlob == uintptr(0) { p = _sqlite3ValueNew(tls, uintptr(0)) if p == uintptr(0) { rc = int32(SQLITE_NOMEM) goto error_out } _sqlite3ValueSetStr(tls, p, 0, __ccgo_ts+1648, uint8(0), libc.UintptrFromInt32(0)) (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpZeroBlob = p } } /* Calculate the hash-key for this change. If the primary key of the row ** includes a NULL value, exit early. Such changes are ignored by the ** session module. */ rc = _sessionPreupdateHash(tls, pSession, iRowid, pTab, libc.BoolInt32(op == int32(SQLITE_INSERT)), bp, bp+4) if rc != SQLITE_OK { goto error_out } if *(*int32)(unsafe.Pointer(bp + 4)) == 0 { pC = *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(*(*int32)(unsafe.Pointer(bp)))*4)) for { if !(pC != 0) { break } if _sessionPreupdateEqual(tls, pSession, iRowid, pTab, pC, op) != 0 { break } goto _1 _1: ; pC = (*TSessionChange)(unsafe.Pointer(pC)).FpNext } if pC == uintptr(0) { /* Used to iterate through columns */ _ = libc.Int32FromInt32(0) (*TSessionTable)(unsafe.Pointer(pTab)).FnEntry++ /* Figure out how large an allocation is required */ *(*Tsqlite3_int64)(unsafe.Pointer(bp + 32)) = int64(20) i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol-(*TSessionTable)(unsafe.Pointer(pTab)).FbRowid) { break } *(*uintptr)(unsafe.Pointer(bp + 40)) = uintptr(0) if op != int32(SQLITE_INSERT) { (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxOld})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, i, bp+40) _ = libc.Int32FromInt32(0) } else { if *(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i))) != 0 { (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, i, bp+40) _ = libc.Int32FromInt32(0) } } /* This may fail if SQLite value p contains a utf-16 string that must ** be converted to utf-8 and an OOM error occurs while doing so. */ rc = _sessionSerializeValue(tls, uintptr(0), *(*uintptr)(unsafe.Pointer(bp + 40)), bp+32) if rc != SQLITE_OK { goto error_out } goto _2 _2: ; i++ } if (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != 0 { *(*Tsqlite3_int64)(unsafe.Pointer(bp + 32)) += int64(9) /* Size of rowid field - an integer */ } /* Allocate the change object */ pC = _sessionMalloc64(tls, pSession, *(*Tsqlite3_int64)(unsafe.Pointer(bp + 32))) if !(pC != 0) { rc = int32(SQLITE_NOMEM) goto error_out } else { libc.Xmemset(tls, pC, 0, uint32(20)) (*TSessionChange)(unsafe.Pointer(pC)).FaRecord = pC + 1*20 } /* Populate the change object. None of the preupdate_old(), ** preupdate_new() or SerializeValue() calls below may fail as all ** required values and encodings have already been cached in memory. ** It is not possible for an OOM to occur in this block. */ *(*Tsqlite3_int64)(unsafe.Pointer(bp + 32)) = 0 if (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != 0 { *(*Tu8)(unsafe.Pointer((*TSessionChange)(unsafe.Pointer(pC)).FaRecord)) = uint8(SQLITE_INTEGER) _sessionPutI64(tls, (*TSessionChange)(unsafe.Pointer(pC)).FaRecord+1, iRowid) *(*Tsqlite3_int64)(unsafe.Pointer(bp + 32)) = int64(9) } i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol-(*TSessionTable)(unsafe.Pointer(pTab)).FbRowid) { break } *(*uintptr)(unsafe.Pointer(bp + 44)) = uintptr(0) if op != int32(SQLITE_INSERT) { (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxOld})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, i, bp+44) } else { if *(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i))) != 0 { (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, i, bp+44) } } _sessionSerializeValue(tls, (*TSessionChange)(unsafe.Pointer(pC)).FaRecord+uintptr(*(*Tsqlite3_int64)(unsafe.Pointer(bp + 32))), *(*uintptr)(unsafe.Pointer(bp + 44)), bp+32) goto _3 _3: ; i++ } /* Add the change to the hash-table */ if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbIndirect != 0 || (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxDepth})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx) != 0 { (*TSessionChange)(unsafe.Pointer(pC)).FbIndirect = uint8(1) } (*TSessionChange)(unsafe.Pointer(pC)).FnRecordField = uint16((*TSessionTable)(unsafe.Pointer(pTab)).FnCol) (*TSessionChange)(unsafe.Pointer(pC)).FnRecord = int32(*(*Tsqlite3_int64)(unsafe.Pointer(bp + 32))) (*TSessionChange)(unsafe.Pointer(pC)).Fop = uint8(op) (*TSessionChange)(unsafe.Pointer(pC)).FpNext = *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(*(*int32)(unsafe.Pointer(bp)))*4)) *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(*(*int32)(unsafe.Pointer(bp)))*4)) = pC } else { if (*TSessionChange)(unsafe.Pointer(pC)).FbIndirect != 0 { /* If the existing change is considered "indirect", but this current ** change is "direct", mark the change object as direct. */ if (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxDepth})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx) == 0 && (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbIndirect == 0 { (*TSessionChange)(unsafe.Pointer(pC)).FbIndirect = uint8(0) } } } _ = libc.Int32FromInt32(0) if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbEnableSize != 0 { rc = _sessionUpdateMaxSize(tls, op, pSession, pTab, pC) } } /* If an error has occurred, mark the session object as failed. */ goto error_out error_out: ; if (*TSessionTable)(unsafe.Pointer(pTab)).FbStat1 != 0 { (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook = (*(*TSessionStat1Ctx)(unsafe.Pointer(bp + 8))).Fhook } if rc != SQLITE_OK { (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = rc } } func _sessionFindTable(tls *libc.TLS, pSession uintptr, zName uintptr, ppTab uintptr) (r int32) { var nName, rc int32 var pRet uintptr _, _, _ = nName, pRet, rc rc = SQLITE_OK nName = _sqlite3Strlen30(tls, zName) /* Search for an existing table */ pRet = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpTable for { if !(pRet != 0) { break } if 0 == Xsqlite3_strnicmp(tls, (*TSessionTable)(unsafe.Pointer(pRet)).FzName, zName, nName+int32(1)) { break } goto _1 _1: ; pRet = (*TSessionTable)(unsafe.Pointer(pRet)).FpNext } if pRet == uintptr(0) && (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbAutoAttach != 0 { /* If there is a table-filter configured, invoke it. If it returns 0, ** do not automatically add the new table. */ if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FxTableFilter == uintptr(0) || (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).FxTableFilter})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpFilterCtx, zName) != 0 { rc = Xsqlite3session_attach(tls, pSession, zName) if rc == SQLITE_OK { pRet = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpTable for pRet != 0 && (*TSessionTable)(unsafe.Pointer(pRet)).FpNext != 0 { pRet = (*TSessionTable)(unsafe.Pointer(pRet)).FpNext } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } } } _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer(ppTab)) = pRet return rc } // C documentation // // /* // ** The 'pre-update' hook registered by this module with SQLite databases. // */ func _xPreUpdate(tls *libc.TLS, pCtx uintptr, db uintptr, op int32, zDb uintptr, zName uintptr, iKey1 Tsqlite3_int64, iKey2 Tsqlite3_int64) { bp := tls.Alloc(16) defer tls.Free(16) var nDb int32 var pSession uintptr var _ /* pTab at bp+0 */ uintptr _, _ = nDb, pSession nDb = _sqlite3Strlen30(tls, zDb) _ = libc.Int32FromInt32(0) _ = iKey1 _ = iKey2 pSession = pCtx for { if !(pSession != 0) { break } /* If this session is attached to a different database ("main", "temp" ** etc.), or if it is not currently enabled, there is nothing to do. Skip ** to the next session object attached to this database. */ if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbEnable == 0 { goto _1 } if (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc != 0 { goto _1 } if Xsqlite3_strnicmp(tls, zDb, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb, nDb+int32(1)) != 0 { goto _1 } (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = _sessionFindTable(tls, pSession, zName, bp) if *(*uintptr)(unsafe.Pointer(bp)) != 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _sessionPreupdateOneChange(tls, op, iKey1, pSession, *(*uintptr)(unsafe.Pointer(bp))) if op == int32(SQLITE_UPDATE) { _sessionPreupdateOneChange(tls, int32(SQLITE_INSERT), iKey2, pSession, *(*uintptr)(unsafe.Pointer(bp))) } } goto _1 _1: ; pSession = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpNext } } // C documentation // // /* // ** The pre-update hook implementations. // */ func _sessionPreupdateOld(tls *libc.TLS, pCtx uintptr, iVal int32, ppVal uintptr) (r int32) { return Xsqlite3_preupdate_old(tls, pCtx, iVal, ppVal) } func _sessionPreupdateNew(tls *libc.TLS, pCtx uintptr, iVal int32, ppVal uintptr) (r int32) { return Xsqlite3_preupdate_new(tls, pCtx, iVal, ppVal) } func _sessionPreupdateCount(tls *libc.TLS, pCtx uintptr) (r int32) { return Xsqlite3_preupdate_count(tls, pCtx) } func _sessionPreupdateDepth(tls *libc.TLS, pCtx uintptr) (r int32) { return Xsqlite3_preupdate_depth(tls, pCtx) } // C documentation // // /* // ** Install the pre-update hooks on the session object passed as the only // ** argument. // */ func _sessionPreupdateHooks(tls *libc.TLS, pSession uintptr) { (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxOld = __ccgo_fp(_sessionPreupdateOld) (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew = __ccgo_fp(_sessionPreupdateNew) (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxCount = __ccgo_fp(_sessionPreupdateCount) (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxDepth = __ccgo_fp(_sessionPreupdateDepth) } type TSessionDiffCtx = struct { FpStmt uintptr FbRowid int32 FnOldOff int32 } type SessionDiffCtx = TSessionDiffCtx type TSessionDiffCtx1 = struct { FpStmt uintptr FbRowid int32 FnOldOff int32 } type SessionDiffCtx1 = TSessionDiffCtx1 // C documentation // // /* // ** The diff hook implementations. // */ func _sessionDiffOld(tls *libc.TLS, pCtx uintptr, iVal int32, ppVal uintptr) (r int32) { var p uintptr _ = p p = pCtx *(*uintptr)(unsafe.Pointer(ppVal)) = Xsqlite3_column_value(tls, (*TSessionDiffCtx)(unsafe.Pointer(p)).FpStmt, iVal+(*TSessionDiffCtx)(unsafe.Pointer(p)).FnOldOff+(*TSessionDiffCtx)(unsafe.Pointer(p)).FbRowid) return SQLITE_OK } func _sessionDiffNew(tls *libc.TLS, pCtx uintptr, iVal int32, ppVal uintptr) (r int32) { var p uintptr _ = p p = pCtx *(*uintptr)(unsafe.Pointer(ppVal)) = Xsqlite3_column_value(tls, (*TSessionDiffCtx)(unsafe.Pointer(p)).FpStmt, iVal+(*TSessionDiffCtx)(unsafe.Pointer(p)).FbRowid) return SQLITE_OK } func _sessionDiffCount(tls *libc.TLS, pCtx uintptr) (r int32) { var p uintptr var v1 int32 _, _ = p, v1 p = pCtx if (*TSessionDiffCtx)(unsafe.Pointer(p)).FnOldOff != 0 { v1 = (*TSessionDiffCtx)(unsafe.Pointer(p)).FnOldOff } else { v1 = Xsqlite3_column_count(tls, (*TSessionDiffCtx)(unsafe.Pointer(p)).FpStmt) } return v1 - (*TSessionDiffCtx)(unsafe.Pointer(p)).FbRowid } func _sessionDiffDepth(tls *libc.TLS, pCtx uintptr) (r int32) { _ = pCtx return 0 } // C documentation // // /* // ** Install the diff hooks on the session object passed as the only // ** argument. // */ func _sessionDiffHooks(tls *libc.TLS, pSession uintptr, pDiffCtx uintptr) { (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx = pDiffCtx (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxOld = __ccgo_fp(_sessionDiffOld) (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew = __ccgo_fp(_sessionDiffNew) (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxCount = __ccgo_fp(_sessionDiffCount) (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxDepth = __ccgo_fp(_sessionDiffDepth) } func _sessionExprComparePK(tls *libc.TLS, nCol int32, zDb1 uintptr, zDb2 uintptr, zTab uintptr, azCol uintptr, abPK uintptr) (r uintptr) { bp := tls.Alloc(80) defer tls.Free(80) var i int32 var zRet, zSep uintptr _, _, _ = i, zRet, zSep zSep = __ccgo_ts + 1648 zRet = uintptr(0) i = 0 for { if !(i < nCol) { break } if *(*Tu8)(unsafe.Pointer(abPK + uintptr(i))) != 0 { zRet = Xsqlite3_mprintf(tls, __ccgo_ts+33455, libc.VaList(bp+8, zRet, zSep, zDb1, zTab, *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*4)), zDb2, zTab, *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*4)))) zSep = __ccgo_ts + 22013 if zRet == uintptr(0) { break } } goto _1 _1: ; i++ } return zRet } func _sessionExprCompareOther(tls *libc.TLS, nCol int32, zDb1 uintptr, zDb2 uintptr, zTab uintptr, azCol uintptr, abPK uintptr) (r uintptr) { bp := tls.Alloc(80) defer tls.Free(80) var bHave, i int32 var zRet, zSep uintptr _, _, _, _ = bHave, i, zRet, zSep zSep = __ccgo_ts + 1648 zRet = uintptr(0) bHave = 0 i = 0 for { if !(i < nCol) { break } if int32(*(*Tu8)(unsafe.Pointer(abPK + uintptr(i)))) == 0 { bHave = int32(1) zRet = Xsqlite3_mprintf(tls, __ccgo_ts+33489, libc.VaList(bp+8, zRet, zSep, zDb1, zTab, *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*4)), zDb2, zTab, *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*4)))) zSep = __ccgo_ts + 33530 if zRet == uintptr(0) { break } } goto _1 _1: ; i++ } if bHave == 0 { _ = libc.Int32FromInt32(0) zRet = Xsqlite3_mprintf(tls, __ccgo_ts+1722, 0) } return zRet } func _sessionSelectFindNew(tls *libc.TLS, zDb1 uintptr, zDb2 uintptr, bRowid int32, zTbl uintptr, zExpr uintptr) (r uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var zRet, zSel, v1 uintptr _, _, _ = zRet, zSel, v1 if bRowid != 0 { v1 = __ccgo_ts + 33535 } else { v1 = __ccgo_ts + 6590 } zSel = v1 zRet = Xsqlite3_mprintf(tls, __ccgo_ts+33546, libc.VaList(bp+8, zSel, zDb1, zTbl, zDb2, zTbl, zExpr)) return zRet } func _sessionDiffFindNew(tls *libc.TLS, op int32, pSession uintptr, pTab uintptr, zDb1 uintptr, zDb2 uintptr, zExpr uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iRowid Ti64 var pDiffCtx, zStmt uintptr var rc int32 var v1 int64 var _ /* pStmt at bp+0 */ uintptr _, _, _, _, _ = iRowid, pDiffCtx, rc, zStmt, v1 rc = SQLITE_OK zStmt = _sessionSelectFindNew(tls, zDb1, zDb2, (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, zExpr) if zStmt == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { rc = Xsqlite3_prepare(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb, zStmt, -int32(1), bp, uintptr(0)) if rc == SQLITE_OK { pDiffCtx = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx (*TSessionDiffCtx)(unsafe.Pointer(pDiffCtx)).FpStmt = *(*uintptr)(unsafe.Pointer(bp)) (*TSessionDiffCtx)(unsafe.Pointer(pDiffCtx)).FnOldOff = 0 (*TSessionDiffCtx)(unsafe.Pointer(pDiffCtx)).FbRowid = (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid for int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { if (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != 0 { v1 = Xsqlite3_column_int64(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) } else { v1 = 0 } iRowid = v1 _sessionPreupdateOneChange(tls, op, iRowid, pSession, pTab) } rc = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) } Xsqlite3_free(tls, zStmt) } return rc } // C documentation // // /* // ** Return a comma-separated list of the fully-qualified (with both database // ** and table name) column names from table pTab. e.g. // ** // ** "main"."t1"."a", "main"."t1"."b", "main"."t1"."c" // */ func _sessionAllCols(tls *libc.TLS, zDb uintptr, pTab uintptr) (r uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var ii int32 var zRet, v2 uintptr _, _, _ = ii, zRet, v2 zRet = uintptr(0) ii = 0 for { if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } if zRet != 0 { v2 = __ccgo_ts + 14990 } else { v2 = __ccgo_ts + 1648 } zRet = Xsqlite3_mprintf(tls, __ccgo_ts+33625, libc.VaList(bp+8, zRet, v2, zDb, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FazCol + uintptr(ii)*4)))) if !(zRet != 0) { break } goto _1 _1: ; ii++ } return zRet } func _sessionDiffFindModified(tls *libc.TLS, pSession uintptr, pTab uintptr, zFrom uintptr, zExpr uintptr) (r int32) { bp := tls.Alloc(80) defer tls.Free(80) var iRowid Ti64 var pDiffCtx, z1, z2, zExpr2, zStmt uintptr var rc int32 var v1 int64 var _ /* pStmt at bp+0 */ uintptr _, _, _, _, _, _, _, _ = iRowid, pDiffCtx, rc, z1, z2, zExpr2, zStmt, v1 rc = SQLITE_OK zExpr2 = _sessionExprCompareOther(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb, zFrom, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, (*TSessionTable)(unsafe.Pointer(pTab)).FazCol, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK) if zExpr2 == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { z1 = _sessionAllCols(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb, pTab) z2 = _sessionAllCols(tls, zFrom, pTab) zStmt = Xsqlite3_mprintf(tls, __ccgo_ts+33644, libc.VaList(bp+16, z1, z2, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, zFrom, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, zExpr, zExpr2)) if zStmt == uintptr(0) || z1 == uintptr(0) || z2 == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { rc = Xsqlite3_prepare(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb, zStmt, -int32(1), bp, uintptr(0)) if rc == SQLITE_OK { pDiffCtx = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx (*TSessionDiffCtx)(unsafe.Pointer(pDiffCtx)).FpStmt = *(*uintptr)(unsafe.Pointer(bp)) (*TSessionDiffCtx)(unsafe.Pointer(pDiffCtx)).FnOldOff = (*TSessionTable)(unsafe.Pointer(pTab)).FnCol for int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { if (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != 0 { v1 = Xsqlite3_column_int64(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) } else { v1 = 0 } iRowid = v1 _sessionPreupdateOneChange(tls, int32(SQLITE_UPDATE), iRowid, pSession, pTab) } rc = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) } } Xsqlite3_free(tls, zStmt) Xsqlite3_free(tls, z1) Xsqlite3_free(tls, z2) } return rc } func Xsqlite3session_diff(tls *libc.TLS, pSession uintptr, zFrom uintptr, zTbl uintptr, pzErrMsg uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var bHasPk, bMismatch, i, rc int32 var db, zDb, zExpr, v1 uintptr var _ /* abPK at bp+24 */ uintptr var _ /* azCol at bp+28 */ uintptr var _ /* bRowid at bp+20 */ int32 var _ /* d at bp+0 */ TSessionDiffCtx var _ /* nCol at bp+16 */ int32 var _ /* pTo at bp+12 */ uintptr _, _, _, _, _, _, _, _ = bHasPk, bMismatch, db, i, rc, zDb, zExpr, v1 zDb = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb rc = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc libc.Xmemset(tls, bp, 0, uint32(12)) _sessionDiffHooks(tls, pSession, bp) Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb)) if pzErrMsg != 0 { *(*uintptr)(unsafe.Pointer(pzErrMsg)) = uintptr(0) } if rc == SQLITE_OK { zExpr = uintptr(0) db = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb /* Table zTbl */ /* Locate and if necessary initialize the target table object */ rc = _sessionFindTable(tls, pSession, zTbl, bp+12) if *(*uintptr)(unsafe.Pointer(bp + 12)) == uintptr(0) { goto diff_out } if _sessionInitTable(tls, pSession, *(*uintptr)(unsafe.Pointer(bp + 12)), (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb) != 0 { rc = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc goto diff_out } /* Check the table schemas match */ if rc == SQLITE_OK { bHasPk = 0 bMismatch = 0 /* Columns in zFrom.zTbl */ *(*int32)(unsafe.Pointer(bp + 20)) = 0 *(*uintptr)(unsafe.Pointer(bp + 28)) = uintptr(0) if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbImplicitPK != 0 { v1 = bp + 20 } else { v1 = uintptr(0) } rc = _sessionTableInfo(tls, uintptr(0), db, zFrom, zTbl, bp+16, uintptr(0), bp+28, uintptr(0), bp+24, v1) if rc == SQLITE_OK { if (*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)))).FnCol != *(*int32)(unsafe.Pointer(bp + 16)) { bMismatch = int32(1) } else { i = 0 for { if !(i < *(*int32)(unsafe.Pointer(bp + 16))) { break } if int32(*(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)))).FabPK + uintptr(i)))) != int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)) + uintptr(i)))) { bMismatch = int32(1) } if Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 28)) + uintptr(i)*4)), *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)))).FazCol + uintptr(i)*4))) != 0 { bMismatch = int32(1) } if *(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)) + uintptr(i))) != 0 { bHasPk = int32(1) } goto _2 _2: ; i++ } } } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 28))) if bMismatch != 0 { if pzErrMsg != 0 { *(*uintptr)(unsafe.Pointer(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+33701, 0) } rc = int32(SQLITE_SCHEMA) } if bHasPk == 0 { /* Ignore tables with no primary keys */ goto diff_out } } if rc == SQLITE_OK { zExpr = _sessionExprComparePK(tls, (*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)))).FnCol, zDb, zFrom, (*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)))).FzName, (*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)))).FazCol, (*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)))).FabPK) } /* Find new rows */ if rc == SQLITE_OK { rc = _sessionDiffFindNew(tls, int32(SQLITE_INSERT), pSession, *(*uintptr)(unsafe.Pointer(bp + 12)), zDb, zFrom, zExpr) } /* Find old rows */ if rc == SQLITE_OK { rc = _sessionDiffFindNew(tls, int32(SQLITE_DELETE), pSession, *(*uintptr)(unsafe.Pointer(bp + 12)), zFrom, zDb, zExpr) } /* Find modified rows */ if rc == SQLITE_OK { rc = _sessionDiffFindModified(tls, pSession, *(*uintptr)(unsafe.Pointer(bp + 12)), zFrom, zExpr) } Xsqlite3_free(tls, zExpr) } goto diff_out diff_out: ; _sessionPreupdateHooks(tls, pSession) Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb)) return rc } // C documentation // // /* // ** Create a session object. This session object will record changes to // ** database zDb attached to connection db. // */ func Xsqlite3session_create(tls *libc.TLS, db uintptr, zDb uintptr, ppSession uintptr) (r int32) { var nDb int32 var pNew, pOld uintptr _, _, _ = nDb, pNew, pOld /* Session object already attached to db */ nDb = _sqlite3Strlen30(tls, zDb) /* Length of zDb in bytes */ /* Zero the output value in case an error occurs. */ *(*uintptr)(unsafe.Pointer(ppSession)) = uintptr(0) /* Allocate and populate the new session object. */ pNew = Xsqlite3_malloc64(tls, uint64(uint32(88)+uint32(nDb)+uint32(1))) if !(pNew != 0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, pNew, 0, uint32(88)) (*Tsqlite3_session)(unsafe.Pointer(pNew)).Fdb = db (*Tsqlite3_session)(unsafe.Pointer(pNew)).FzDb = pNew + 1*88 (*Tsqlite3_session)(unsafe.Pointer(pNew)).FbEnable = int32(1) libc.Xmemcpy(tls, (*Tsqlite3_session)(unsafe.Pointer(pNew)).FzDb, zDb, uint32(nDb+int32(1))) _sessionPreupdateHooks(tls, pNew) /* Add the new session object to the linked list of session objects ** attached to database handle $db. Do this under the cover of the db ** handle mutex. */ Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, db)) pOld = Xsqlite3_preupdate_hook(tls, db, __ccgo_fp(_xPreUpdate), pNew) (*Tsqlite3_session)(unsafe.Pointer(pNew)).FpNext = pOld Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, db)) *(*uintptr)(unsafe.Pointer(ppSession)) = pNew return SQLITE_OK } // C documentation // // /* // ** Free the list of table objects passed as the first argument. The contents // ** of the changed-rows hash tables are also deleted. // */ func _sessionDeleteTable(tls *libc.TLS, pSession uintptr, pList uintptr) { var i int32 var p, pNext, pNextChange, pTab uintptr _, _, _, _, _ = i, p, pNext, pNextChange, pTab pTab = pList for { if !(pTab != 0) { break } pNext = (*TSessionTable)(unsafe.Pointer(pTab)).FpNext i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnChange) { break } p = *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(i)*4)) for { if !(p != 0) { break } pNextChange = (*TSessionChange)(unsafe.Pointer(p)).FpNext _sessionFree(tls, pSession, p) goto _3 _3: ; p = pNextChange } goto _2 _2: ; i++ } Xsqlite3_finalize(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt) _sessionFree(tls, pSession, (*TSessionTable)(unsafe.Pointer(pTab)).FazCol) /* cast works around VC++ bug */ _sessionFree(tls, pSession, (*TSessionTable)(unsafe.Pointer(pTab)).FapChange) _sessionFree(tls, pSession, pTab) goto _1 _1: ; pTab = pNext } } // C documentation // // /* // ** Delete a session object previously allocated using sqlite3session_create(). // */ func Xsqlite3session_delete(tls *libc.TLS, pSession uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var db, pp uintptr var _ /* pHead at bp+0 */ uintptr _, _ = db, pp db = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb /* Unlink the session from the linked list of sessions attached to the ** database handle. Hold the db mutex while doing so. */ Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, db)) *(*uintptr)(unsafe.Pointer(bp)) = Xsqlite3_preupdate_hook(tls, db, uintptr(0), uintptr(0)) pp = bp for { if !(*(*uintptr)(unsafe.Pointer(pp)) != uintptr(0)) { break } if *(*uintptr)(unsafe.Pointer(pp)) == pSession { *(*uintptr)(unsafe.Pointer(pp)) = (*Tsqlite3_session)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FpNext if *(*uintptr)(unsafe.Pointer(bp)) != 0 { Xsqlite3_preupdate_hook(tls, db, __ccgo_fp(_xPreUpdate), *(*uintptr)(unsafe.Pointer(bp))) } break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 60 } Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, db)) _sqlite3ValueFree(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpZeroBlob) /* Delete all attached table objects. And the contents of their ** associated hash-tables. */ _sessionDeleteTable(tls, pSession, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpTable) /* Free the session object. */ Xsqlite3_free(tls, pSession) } // C documentation // // /* // ** Set a table filter on a Session Object. // */ func Xsqlite3session_table_filter(tls *libc.TLS, pSession uintptr, xFilter uintptr, pCtx uintptr) { (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbAutoAttach = int32(1) (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpFilterCtx = pCtx (*Tsqlite3_session)(unsafe.Pointer(pSession)).FxTableFilter = xFilter } // C documentation // // /* // ** Attach a table to a session. All subsequent changes made to the table // ** while the session object is enabled will be recorded. // ** // ** Only tables that have a PRIMARY KEY defined may be attached. It does // ** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) // ** or not. // */ func Xsqlite3session_attach(tls *libc.TLS, pSession uintptr, zName uintptr) (r int32) { var nByte, nName, rc int32 var pTab, ppTab uintptr _, _, _, _, _ = nByte, nName, pTab, ppTab, rc rc = SQLITE_OK Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb)) if !(zName != 0) { (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbAutoAttach = int32(1) } else { /* Number of bytes in string zName */ /* First search for an existing entry. If one is found, this call is ** a no-op. Return early. */ nName = _sqlite3Strlen30(tls, zName) pTab = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpTable for { if !(pTab != 0) { break } if 0 == Xsqlite3_strnicmp(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, zName, nName+int32(1)) { break } goto _1 _1: ; pTab = (*TSessionTable)(unsafe.Pointer(pTab)).FpNext } if !(pTab != 0) { /* Allocate new SessionTable object. */ nByte = int32(uint32(48) + uint32(nName) + uint32(1)) pTab = _sessionMalloc64(tls, pSession, int64(nByte)) if !(pTab != 0) { rc = int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, pTab, 0, uint32(48)) (*TSessionTable)(unsafe.Pointer(pTab)).FzName = pTab + 1*48 libc.Xmemcpy(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, zName, uint32(nName+int32(1))) ppTab = pSession + 64 for { if !(*(*uintptr)(unsafe.Pointer(ppTab)) != 0) { break } goto _2 _2: ; ppTab = *(*uintptr)(unsafe.Pointer(ppTab)) } *(*uintptr)(unsafe.Pointer(ppTab)) = pTab } } } Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb)) return rc } // C documentation // // /* // ** Append the value passed as the second argument to the buffer passed // ** as the first. // ** // ** This function is a no-op if *pRc is non-zero when it is called. // ** Otherwise, if an error occurs, *pRc is set to an SQLite error code // ** before returning. // */ func _sessionAppendValue(tls *libc.TLS, p uintptr, pVal uintptr, pRc uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var p1 uintptr var _ /* nByte at bp+8 */ Tsqlite3_int64 var _ /* rc at bp+0 */ int32 _ = p1 *(*int32)(unsafe.Pointer(bp)) = *(*int32)(unsafe.Pointer(pRc)) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*Tsqlite3_int64)(unsafe.Pointer(bp + 8)) = 0 *(*int32)(unsafe.Pointer(bp)) = _sessionSerializeValue(tls, uintptr(0), pVal, bp+8) _sessionBufferGrow(tls, p, *(*Tsqlite3_int64)(unsafe.Pointer(bp + 8)), bp) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sessionSerializeValue(tls, (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf), pVal, uintptr(0)) p1 = p + 4 *(*int32)(unsafe.Pointer(p1)) = int32(int64(*(*int32)(unsafe.Pointer(p1))) + *(*Tsqlite3_int64)(unsafe.Pointer(bp + 8))) } else { *(*int32)(unsafe.Pointer(pRc)) = *(*int32)(unsafe.Pointer(bp)) } } } // C documentation // // /* // ** This function is a no-op if *pRc is other than SQLITE_OK when it is // ** called. Otherwise, append a single byte to the buffer. // ** // ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before // ** returning. // */ func _sessionAppendByte(tls *libc.TLS, p uintptr, v Tu8, pRc uintptr) { var v1 int32 var v2 uintptr _, _ = v1, v2 if 0 == _sessionBufferGrow(tls, p, int64(1), pRc) { v2 = p + 4 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ *(*Tu8)(unsafe.Pointer((*TSessionBuffer)(unsafe.Pointer(p)).FaBuf + uintptr(v1))) = v } } // C documentation // // /* // ** This function is a no-op if *pRc is other than SQLITE_OK when it is // ** called. Otherwise, append a single varint to the buffer. // ** // ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before // ** returning. // */ func _sessionAppendVarint(tls *libc.TLS, p uintptr, v int32, pRc uintptr) { if 0 == _sessionBufferGrow(tls, p, int64(9), pRc) { *(*int32)(unsafe.Pointer(p + 4)) += _sessionVarintPut(tls, (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf), v) } } // C documentation // // /* // ** This function is a no-op if *pRc is other than SQLITE_OK when it is // ** called. Otherwise, append a blob of data to the buffer. // ** // ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before // ** returning. // */ func _sessionAppendBlob(tls *libc.TLS, p uintptr, aBlob uintptr, nBlob int32, pRc uintptr) { if nBlob > 0 && 0 == _sessionBufferGrow(tls, p, int64(nBlob), pRc) { libc.Xmemcpy(tls, (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf), aBlob, uint32(nBlob)) *(*int32)(unsafe.Pointer(p + 4)) += nBlob } } // C documentation // // /* // ** This function is a no-op if *pRc is other than SQLITE_OK when it is // ** called. Otherwise, append the string representation of integer iVal // ** to the buffer. No nul-terminator is written. // ** // ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before // ** returning. // */ func _sessionAppendInteger(tls *libc.TLS, p uintptr, iVal int32, pRc uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* aBuf at bp+0 */ [24]uint8 Xsqlite3_snprintf(tls, int32(libc.Uint32FromInt64(24)-libc.Uint32FromInt32(1)), bp, __ccgo_ts+5138, libc.VaList(bp+32, iVal)) _sessionAppendStr(tls, p, bp, pRc) } // C documentation // // /* // ** This function is a no-op if *pRc is other than SQLITE_OK when it is // ** called. Otherwise, append the string zStr enclosed in quotes (") and // ** with any embedded quote characters escaped to the buffer. No // ** nul-terminator byte is written. // ** // ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before // ** returning. // */ func _sessionAppendIdent(tls *libc.TLS, p uintptr, zStr uintptr, pRc uintptr) { var nStr int32 var zIn, zOut, v1, v2, v3, v4, v5 uintptr _, _, _, _, _, _, _, _ = nStr, zIn, zOut, v1, v2, v3, v4, v5 nStr = _sqlite3Strlen30(tls, zStr)*int32(2) + int32(2) + int32(2) if 0 == _sessionBufferGrow(tls, p, int64(nStr), pRc) { zOut = (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf + uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf) zIn = zStr v1 = zOut zOut++ *(*uint8)(unsafe.Pointer(v1)) = uint8('"') for *(*uint8)(unsafe.Pointer(zIn)) != 0 { if int32(*(*uint8)(unsafe.Pointer(zIn))) == int32('"') { v2 = zOut zOut++ *(*uint8)(unsafe.Pointer(v2)) = uint8('"') } v3 = zOut zOut++ v4 = zIn zIn++ *(*uint8)(unsafe.Pointer(v3)) = *(*uint8)(unsafe.Pointer(v4)) } v5 = zOut zOut++ *(*uint8)(unsafe.Pointer(v5)) = uint8('"') (*TSessionBuffer)(unsafe.Pointer(p)).FnBuf = int32(zOut) - int32((*TSessionBuffer)(unsafe.Pointer(p)).FaBuf) *(*Tu8)(unsafe.Pointer((*TSessionBuffer)(unsafe.Pointer(p)).FaBuf + uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf))) = uint8(0x00) } } // C documentation // // /* // ** This function is a no-op if *pRc is other than SQLITE_OK when it is // ** called. Otherwse, it appends the serialized version of the value stored // ** in column iCol of the row that SQL statement pStmt currently points // ** to to the buffer. // */ func _sessionAppendCol(tls *libc.TLS, p uintptr, pStmt uintptr, iCol int32, pRc uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var eType, nByte int32 var z uintptr var _ /* aBuf at bp+8 */ [8]Tu8 var _ /* i at bp+0 */ Tsqlite3_int64 var _ /* r at bp+16 */ float64 _, _, _ = eType, nByte, z if *(*int32)(unsafe.Pointer(pRc)) == SQLITE_OK { eType = Xsqlite3_column_type(tls, pStmt, iCol) _sessionAppendByte(tls, p, uint8(eType), pRc) if eType == int32(SQLITE_INTEGER) || eType == int32(SQLITE_FLOAT) { if eType == int32(SQLITE_INTEGER) { *(*Tsqlite3_int64)(unsafe.Pointer(bp)) = Xsqlite3_column_int64(tls, pStmt, iCol) } else { *(*float64)(unsafe.Pointer(bp + 16)) = Xsqlite3_column_double(tls, pStmt, iCol) libc.Xmemcpy(tls, bp, bp+16, uint32(8)) } _sessionPutI64(tls, bp+8, *(*Tsqlite3_int64)(unsafe.Pointer(bp))) _sessionAppendBlob(tls, p, bp+8, int32(8), pRc) } if eType == int32(SQLITE_BLOB) || eType == int32(SQLITE_TEXT) { if eType == int32(SQLITE_BLOB) { z = Xsqlite3_column_blob(tls, pStmt, iCol) } else { z = Xsqlite3_column_text(tls, pStmt, iCol) } nByte = Xsqlite3_column_bytes(tls, pStmt, iCol) if z != 0 || eType == int32(SQLITE_BLOB) && nByte == 0 { _sessionAppendVarint(tls, p, nByte, pRc) _sessionAppendBlob(tls, p, z, nByte, pRc) } else { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) } } } } // C documentation // // /* // ** // ** This function appends an update change to the buffer (see the comments // ** under "CHANGESET FORMAT" at the top of the file). An update change // ** consists of: // ** // ** 1 byte: SQLITE_UPDATE (0x17) // ** n bytes: old.* record (see RECORD FORMAT) // ** m bytes: new.* record (see RECORD FORMAT) // ** // ** The SessionChange object passed as the third argument contains the // ** values that were stored in the row when the session began (the old.* // ** values). The statement handle passed as the second argument points // ** at the current version of the row (the new.* values). // ** // ** If all of the old.* values are equal to their corresponding new.* value // ** (i.e. nothing has changed), then no data at all is appended to the buffer. // ** // ** Otherwise, the old.* record contains all primary key values and the // ** original values of any fields that have been modified. The new.* record // ** contains the new values of only those fields that have been modified. // */ func _sessionAppendUpdate(tls *libc.TLS, pBuf uintptr, bPatchset int32, pStmt uintptr, p uintptr, abPK uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var bChanged, bNoop, eType, i, nAdvance, nHdr, nRewind int32 var pCsr uintptr var _ /* buf2 at bp+4 */ TSessionBuffer var _ /* dVal at bp+24 */ float64 var _ /* iVal at bp+16 */ Tsqlite3_int64 var _ /* n at bp+32 */ int32 var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _, _ = bChanged, bNoop, eType, i, nAdvance, nHdr, nRewind, pCsr *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*TSessionBuffer)(unsafe.Pointer(bp + 4)) = TSessionBuffer{} /* Buffer to accumulate new.* record in */ bNoop = int32(1) /* Set to zero if any values are modified */ nRewind = (*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf /* Used to iterate through columns */ pCsr = (*TSessionChange)(unsafe.Pointer(p)).FaRecord /* Used to iterate through old.* values */ _ = libc.Int32FromInt32(0) _sessionAppendByte(tls, pBuf, uint8(SQLITE_UPDATE), bp) _sessionAppendByte(tls, pBuf, (*TSessionChange)(unsafe.Pointer(p)).FbIndirect, bp) i = 0 for { if !(i < Xsqlite3_column_count(tls, pStmt)) { break } bChanged = 0 eType = int32(*(*Tu8)(unsafe.Pointer(pCsr))) switch eType { case int32(SQLITE_NULL): nAdvance = int32(1) if Xsqlite3_column_type(tls, pStmt, i) != int32(SQLITE_NULL) { bChanged = int32(1) } case int32(SQLITE_FLOAT): fallthrough case int32(SQLITE_INTEGER): nAdvance = int32(9) if eType == Xsqlite3_column_type(tls, pStmt, i) { *(*Tsqlite3_int64)(unsafe.Pointer(bp + 16)) = _sessionGetI64(tls, pCsr+1) if eType == int32(SQLITE_INTEGER) { if *(*Tsqlite3_int64)(unsafe.Pointer(bp + 16)) == Xsqlite3_column_int64(tls, pStmt, i) { break } } else { libc.Xmemcpy(tls, bp+24, bp+16, uint32(8)) if *(*float64)(unsafe.Pointer(bp + 24)) == Xsqlite3_column_double(tls, pStmt, i) { break } } } bChanged = int32(1) default: nHdr = int32(1) + _sessionVarintGet(tls, pCsr+1, bp+32) _ = libc.Int32FromInt32(0) nAdvance = nHdr + *(*int32)(unsafe.Pointer(bp + 32)) if eType == Xsqlite3_column_type(tls, pStmt, i) && *(*int32)(unsafe.Pointer(bp + 32)) == Xsqlite3_column_bytes(tls, pStmt, i) && (*(*int32)(unsafe.Pointer(bp + 32)) == 0 || 0 == libc.Xmemcmp(tls, pCsr+uintptr(nHdr), Xsqlite3_column_blob(tls, pStmt, i), uint32(*(*int32)(unsafe.Pointer(bp + 32))))) { break } bChanged = int32(1) } /* If at least one field has been modified, this is not a no-op. */ if bChanged != 0 { bNoop = 0 } /* Add a field to the old.* record. This is omitted if this module is ** currently generating a patchset. */ if bPatchset == 0 { if bChanged != 0 || *(*Tu8)(unsafe.Pointer(abPK + uintptr(i))) != 0 { _sessionAppendBlob(tls, pBuf, pCsr, nAdvance, bp) } else { _sessionAppendByte(tls, pBuf, uint8(0), bp) } } /* Add a field to the new.* record. Or the only record if currently ** generating a patchset. */ if bChanged != 0 || bPatchset != 0 && *(*Tu8)(unsafe.Pointer(abPK + uintptr(i))) != 0 { _sessionAppendCol(tls, bp+4, pStmt, i, bp) } else { _sessionAppendByte(tls, bp+4, uint8(0), bp) } pCsr += uintptr(nAdvance) goto _1 _1: ; i++ } if bNoop != 0 { (*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf = nRewind } else { _sessionAppendBlob(tls, pBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf, bp) } Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Append a DELETE change to the buffer passed as the first argument. Use // ** the changeset format if argument bPatchset is zero, or the patchset // ** format otherwise. // */ func _sessionAppendDelete(tls *libc.TLS, pBuf uintptr, bPatchset int32, p uintptr, nCol int32, abPK uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var a, pStart, v2 uintptr var eType, i int32 var _ /* n at bp+4 */ int32 var _ /* rc at bp+0 */ int32 _, _, _, _, _ = a, eType, i, pStart, v2 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK _sessionAppendByte(tls, pBuf, uint8(SQLITE_DELETE), bp) _sessionAppendByte(tls, pBuf, (*TSessionChange)(unsafe.Pointer(p)).FbIndirect, bp) if bPatchset == 0 { _sessionAppendBlob(tls, pBuf, (*TSessionChange)(unsafe.Pointer(p)).FaRecord, (*TSessionChange)(unsafe.Pointer(p)).FnRecord, bp) } else { a = (*TSessionChange)(unsafe.Pointer(p)).FaRecord i = 0 for { if !(i < nCol) { break } pStart = a v2 = a a++ eType = int32(*(*Tu8)(unsafe.Pointer(v2))) switch eType { case 0: fallthrough case int32(SQLITE_NULL): _ = libc.Int32FromInt32(0) case int32(SQLITE_FLOAT): fallthrough case int32(SQLITE_INTEGER): a += uintptr(8) default: a += uintptr(_sessionVarintGet(tls, a, bp+4)) a += uintptr(*(*int32)(unsafe.Pointer(bp + 4))) break } if *(*Tu8)(unsafe.Pointer(abPK + uintptr(i))) != 0 { _sessionAppendBlob(tls, pBuf, pStart, int32(a)-int32(pStart), bp) } goto _1 _1: ; i++ } _ = libc.Int32FromInt32(0) } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Formulate and prepare a SELECT statement to retrieve a row from table // ** zTab in database zDb based on its primary key. i.e. // ** // ** SELECT *, FROM zDb.zTab WHERE (pk1, pk2,...) IS (?1, ?2,...) // ** // ** where is: // ** // ** 1 AND (?A OR ?1 IS ) AND ... // ** // ** for each non-pk . // */ func _sessionSelectStmt(tls *libc.TLS, db uintptr, bIgnoreNoop int32, zDb uintptr, zTab uintptr, bRowid int32, nCol int32, azCol uintptr, abPK uintptr, ppStmt uintptr) (r int32) { bp := tls.Alloc(96) defer tls.Free(96) var i, nSql int32 var zCols, zSep, zSql, v1, v3 uintptr var _ /* nooptest at bp+4 */ TSessionBuffer var _ /* pkfield at bp+16 */ TSessionBuffer var _ /* pkvar at bp+28 */ TSessionBuffer var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _ = i, nSql, zCols, zSep, zSql, v1, v3 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK zSql = uintptr(0) zSep = __ccgo_ts + 1648 if bRowid != 0 { v1 = __ccgo_ts + 33535 } else { v1 = __ccgo_ts + 6590 } zCols = v1 nSql = -int32(1) *(*TSessionBuffer)(unsafe.Pointer(bp + 4)) = TSessionBuffer{} *(*TSessionBuffer)(unsafe.Pointer(bp + 16)) = TSessionBuffer{} *(*TSessionBuffer)(unsafe.Pointer(bp + 28)) = TSessionBuffer{} _sessionAppendStr(tls, bp+4, __ccgo_ts+33728, bp) if 0 == Xsqlite3_stricmp(tls, __ccgo_ts+11582, zTab) { _sessionAppendStr(tls, bp+4, __ccgo_ts+33732, bp) _sessionAppendStr(tls, bp+16, __ccgo_ts+33756, bp) _sessionAppendStr(tls, bp+28, __ccgo_ts+33765, bp) zCols = __ccgo_ts + 33810 } else { i = 0 for { if !(i < nCol) { break } if *(*Tu8)(unsafe.Pointer(abPK + uintptr(i))) != 0 { _sessionAppendStr(tls, bp+16, zSep, bp) _sessionAppendStr(tls, bp+28, zSep, bp) zSep = __ccgo_ts + 14990 _sessionAppendIdent(tls, bp+16, *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*4)), bp) _sessionAppendPrintf(tls, bp+28, bp, __ccgo_ts+33824, libc.VaList(bp+48, i+int32(1))) } else { _sessionAppendPrintf(tls, bp+4, bp, __ccgo_ts+33828, libc.VaList(bp+48, i+int32(1)+nCol, i+int32(1), zTab, *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*4)))) } goto _2 _2: ; i++ } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { if bIgnoreNoop != 0 { v3 = (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf } else { v3 = __ccgo_ts + 1648 } zSql = Xsqlite3_mprintf(tls, __ccgo_ts+33855, libc.VaList(bp+48, zCols, v3, zDb, zTab, (*(*TSessionBuffer)(unsafe.Pointer(bp + 16))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 28))).FaBuf)) if zSql == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_NOMEM) } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_prepare_v2(tls, db, zSql, nSql, ppStmt, uintptr(0)) } Xsqlite3_free(tls, zSql) Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf) Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 16))).FaBuf) Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 28))).FaBuf) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Bind the PRIMARY KEY values from the change passed in argument pChange // ** to the SELECT statement passed as the first argument. The SELECT statement // ** is as prepared by function sessionSelectStmt(). // ** // ** Return SQLITE_OK if all PK values are successfully bound, or an SQLite // ** error code (e.g. SQLITE_NOMEM) otherwise. // */ func _sessionSelectBind(tls *libc.TLS, pSelect uintptr, nCol int32, abPK uintptr, pChange uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var a, v2 uintptr var eType, i, rc int32 var iVal Ti64 var _ /* iVal at bp+8 */ Ti64 var _ /* n at bp+16 */ int32 var _ /* n at bp+20 */ int32 var _ /* rVal at bp+0 */ float64 _, _, _, _, _, _ = a, eType, i, iVal, rc, v2 rc = SQLITE_OK a = (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord i = 0 for { if !(i < nCol && rc == SQLITE_OK) { break } v2 = a a++ eType = int32(*(*Tu8)(unsafe.Pointer(v2))) switch eType { case 0: fallthrough case int32(SQLITE_NULL): _ = libc.Int32FromInt32(0) case int32(SQLITE_INTEGER): if *(*Tu8)(unsafe.Pointer(abPK + uintptr(i))) != 0 { iVal = _sessionGetI64(tls, a) rc = Xsqlite3_bind_int64(tls, pSelect, i+int32(1), iVal) } a += uintptr(8) case int32(SQLITE_FLOAT): if *(*Tu8)(unsafe.Pointer(abPK + uintptr(i))) != 0 { *(*Ti64)(unsafe.Pointer(bp + 8)) = _sessionGetI64(tls, a) libc.Xmemcpy(tls, bp, bp+8, uint32(8)) rc = Xsqlite3_bind_double(tls, pSelect, i+int32(1), *(*float64)(unsafe.Pointer(bp))) } a += uintptr(8) case int32(SQLITE_TEXT): a += uintptr(_sessionVarintGet(tls, a, bp+16)) if *(*Tu8)(unsafe.Pointer(abPK + uintptr(i))) != 0 { rc = Xsqlite3_bind_text(tls, pSelect, i+int32(1), a, *(*int32)(unsafe.Pointer(bp + 16)), uintptr(-libc.Int32FromInt32(1))) } a += uintptr(*(*int32)(unsafe.Pointer(bp + 16))) default: _ = libc.Int32FromInt32(0) a += uintptr(_sessionVarintGet(tls, a, bp+20)) if *(*Tu8)(unsafe.Pointer(abPK + uintptr(i))) != 0 { rc = Xsqlite3_bind_blob(tls, pSelect, i+int32(1), a, *(*int32)(unsafe.Pointer(bp + 20)), uintptr(-libc.Int32FromInt32(1))) } a += uintptr(*(*int32)(unsafe.Pointer(bp + 20))) break } goto _1 _1: ; i++ } return rc } // C documentation // // /* // ** This function is a no-op if *pRc is set to other than SQLITE_OK when it // ** is called. Otherwise, append a serialized table header (part of the binary // ** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an // ** SQLite error code before returning. // */ func _sessionAppendTableHdr(tls *libc.TLS, pBuf uintptr, bPatchset int32, pTab uintptr, pRc uintptr) { var v1 int32 _ = v1 /* Write a table header */ if bPatchset != 0 { v1 = int32('P') } else { v1 = int32('T') } _sessionAppendByte(tls, pBuf, uint8(v1), pRc) _sessionAppendVarint(tls, pBuf, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, pRc) _sessionAppendBlob(tls, pBuf, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, pRc) _sessionAppendBlob(tls, pBuf, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, int32(libc.Xstrlen(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName))+int32(1), pRc) } // C documentation // // /* // ** Generate either a changeset (if argument bPatchset is zero) or a patchset // ** (if it is non-zero) based on the current contents of the session object // ** passed as the first argument. // ** // ** If no error occurs, SQLITE_OK is returned and the new changeset/patchset // ** stored in output variables *pnChangeset and *ppChangeset. Or, if an error // ** occurs, an SQLite error code is returned and both output variables set // ** to 0. // */ func _sessionGenerateChangeset(tls *libc.TLS, pSession uintptr, bPatchset int32, xOutput uintptr, pOut uintptr, pnChangeset uintptr, ppChangeset uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var db, p, pTab, zName uintptr var i, iCol, nNoop, nOldCol, nRewind int32 var _ /* buf at bp+0 */ TSessionBuffer var _ /* pSel at bp+16 */ uintptr var _ /* rc at bp+12 */ int32 _, _, _, _, _, _, _, _, _ = db, i, iCol, nNoop, nOldCol, nRewind, p, pTab, zName db = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb /* Used to iterate through attached tables */ *(*TSessionBuffer)(unsafe.Pointer(bp)) = TSessionBuffer{} /* Return code */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Zero the output variables in case an error occurs. If this session ** object is already in the error state (sqlite3_session.rc != SQLITE_OK), ** this call will be a no-op. */ if xOutput == uintptr(0) { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pnChangeset)) = 0 *(*uintptr)(unsafe.Pointer(ppChangeset)) = uintptr(0) } if (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc != 0 { return (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc } *(*int32)(unsafe.Pointer(bp + 12)) = Xsqlite3_exec(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb, __ccgo_ts+33897, uintptr(0), uintptr(0), uintptr(0)) if *(*int32)(unsafe.Pointer(bp + 12)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 12)) } Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, db)) pTab = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpTable for { if !(*(*int32)(unsafe.Pointer(bp + 12)) == SQLITE_OK && pTab != 0) { break } if (*TSessionTable)(unsafe.Pointer(pTab)).FnEntry != 0 { zName = (*TSessionTable)(unsafe.Pointer(pTab)).FzName /* Used to iterate through hash buckets */ *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) /* SELECT statement to query table pTab */ nRewind = (*(*TSessionBuffer)(unsafe.Pointer(bp))).FnBuf /* Size of buffer after writing tbl header */ nOldCol = (*TSessionTable)(unsafe.Pointer(pTab)).FnCol /* Check the table schema is still Ok. */ *(*int32)(unsafe.Pointer(bp + 12)) = _sessionReinitTable(tls, pSession, pTab) if *(*int32)(unsafe.Pointer(bp + 12)) == SQLITE_OK && (*TSessionTable)(unsafe.Pointer(pTab)).FnCol != nOldCol { *(*int32)(unsafe.Pointer(bp + 12)) = _sessionUpdateChanges(tls, pSession, pTab) } /* Write a table header */ _sessionAppendTableHdr(tls, bp, bPatchset, pTab, bp+12) /* Build and compile a statement to execute: */ if *(*int32)(unsafe.Pointer(bp + 12)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 12)) = _sessionSelectStmt(tls, db, 0, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb, zName, (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, (*TSessionTable)(unsafe.Pointer(pTab)).FazCol, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK, bp+16) } nNoop = (*(*TSessionBuffer)(unsafe.Pointer(bp))).FnBuf i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnChange && *(*int32)(unsafe.Pointer(bp + 12)) == SQLITE_OK) { break } /* Used to iterate through changes */ p = *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(i)*4)) for { if !(*(*int32)(unsafe.Pointer(bp + 12)) == SQLITE_OK && p != 0) { break } *(*int32)(unsafe.Pointer(bp + 12)) = _sessionSelectBind(tls, *(*uintptr)(unsafe.Pointer(bp + 16)), (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK, p) if *(*int32)(unsafe.Pointer(bp + 12)) != SQLITE_OK { goto _3 } if Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) == int32(SQLITE_ROW) { if int32((*TSessionChange)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INSERT) { _sessionAppendByte(tls, bp, uint8(SQLITE_INSERT), bp+12) _sessionAppendByte(tls, bp, (*TSessionChange)(unsafe.Pointer(p)).FbIndirect, bp+12) iCol = 0 for { if !(iCol < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } _sessionAppendCol(tls, bp, *(*uintptr)(unsafe.Pointer(bp + 16)), iCol, bp+12) goto _4 _4: ; iCol++ } } else { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp + 12)) = _sessionAppendUpdate(tls, bp, bPatchset, *(*uintptr)(unsafe.Pointer(bp + 16)), p, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK) } } else { if int32((*TSessionChange)(unsafe.Pointer(p)).Fop) != int32(SQLITE_INSERT) { *(*int32)(unsafe.Pointer(bp + 12)) = _sessionAppendDelete(tls, bp, bPatchset, p, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK) } } if *(*int32)(unsafe.Pointer(bp + 12)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 12)) = Xsqlite3_reset(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) } /* If the buffer is now larger than sessions_strm_chunk_size, pass ** its contents to the xOutput() callback. */ if xOutput != 0 && *(*int32)(unsafe.Pointer(bp + 12)) == SQLITE_OK && (*(*TSessionBuffer)(unsafe.Pointer(bp))).FnBuf > nNoop && (*(*TSessionBuffer)(unsafe.Pointer(bp))).FnBuf > _sessions_strm_chunk_size { *(*int32)(unsafe.Pointer(bp + 12)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{xOutput})))(tls, pOut, (*(*TSessionBuffer)(unsafe.Pointer(bp))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp))).FnBuf) nNoop = -int32(1) (*(*TSessionBuffer)(unsafe.Pointer(bp))).FnBuf = 0 } goto _3 _3: ; p = (*TSessionChange)(unsafe.Pointer(p)).FpNext } goto _2 _2: ; i++ } Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) if (*(*TSessionBuffer)(unsafe.Pointer(bp))).FnBuf == nNoop { (*(*TSessionBuffer)(unsafe.Pointer(bp))).FnBuf = nRewind } } goto _1 _1: ; pTab = (*TSessionTable)(unsafe.Pointer(pTab)).FpNext } if *(*int32)(unsafe.Pointer(bp + 12)) == SQLITE_OK { if xOutput == uintptr(0) { *(*int32)(unsafe.Pointer(pnChangeset)) = (*(*TSessionBuffer)(unsafe.Pointer(bp))).FnBuf *(*uintptr)(unsafe.Pointer(ppChangeset)) = (*(*TSessionBuffer)(unsafe.Pointer(bp))).FaBuf (*(*TSessionBuffer)(unsafe.Pointer(bp))).FaBuf = uintptr(0) } else { if (*(*TSessionBuffer)(unsafe.Pointer(bp))).FnBuf > 0 { *(*int32)(unsafe.Pointer(bp + 12)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{xOutput})))(tls, pOut, (*(*TSessionBuffer)(unsafe.Pointer(bp))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp))).FnBuf) } } } Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp))).FaBuf) Xsqlite3_exec(tls, db, __ccgo_ts+33917, uintptr(0), uintptr(0), uintptr(0)) Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, db)) return *(*int32)(unsafe.Pointer(bp + 12)) } // C documentation // // /* // ** Obtain a changeset object containing all changes recorded by the // ** session object passed as the first argument. // ** // ** It is the responsibility of the caller to eventually free the buffer // ** using sqlite3_free(). // */ func Xsqlite3session_changeset(tls *libc.TLS, pSession uintptr, pnChangeset uintptr, ppChangeset uintptr) (r int32) { var rc int32 _ = rc if pnChangeset == uintptr(0) || ppChangeset == uintptr(0) { return int32(SQLITE_MISUSE) } rc = _sessionGenerateChangeset(tls, pSession, 0, uintptr(0), uintptr(0), pnChangeset, ppChangeset) _ = libc.Int32FromInt32(0) return rc } // C documentation // // /* // ** Streaming version of sqlite3session_changeset(). // */ func Xsqlite3session_changeset_strm(tls *libc.TLS, pSession uintptr, xOutput uintptr, pOut uintptr) (r int32) { if xOutput == uintptr(0) { return int32(SQLITE_MISUSE) } return _sessionGenerateChangeset(tls, pSession, 0, xOutput, pOut, uintptr(0), uintptr(0)) } // C documentation // // /* // ** Streaming version of sqlite3session_patchset(). // */ func Xsqlite3session_patchset_strm(tls *libc.TLS, pSession uintptr, xOutput uintptr, pOut uintptr) (r int32) { if xOutput == uintptr(0) { return int32(SQLITE_MISUSE) } return _sessionGenerateChangeset(tls, pSession, int32(1), xOutput, pOut, uintptr(0), uintptr(0)) } // C documentation // // /* // ** Obtain a patchset object containing all changes recorded by the // ** session object passed as the first argument. // ** // ** It is the responsibility of the caller to eventually free the buffer // ** using sqlite3_free(). // */ func Xsqlite3session_patchset(tls *libc.TLS, pSession uintptr, pnPatchset uintptr, ppPatchset uintptr) (r int32) { if pnPatchset == uintptr(0) || ppPatchset == uintptr(0) { return int32(SQLITE_MISUSE) } return _sessionGenerateChangeset(tls, pSession, int32(1), uintptr(0), uintptr(0), pnPatchset, ppPatchset) } // C documentation // // /* // ** Enable or disable the session object passed as the first argument. // */ func Xsqlite3session_enable(tls *libc.TLS, pSession uintptr, bEnable int32) (r int32) { var ret int32 _ = ret Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb)) if bEnable >= 0 { (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbEnable = bEnable } ret = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbEnable Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb)) return ret } // C documentation // // /* // ** Enable or disable the session object passed as the first argument. // */ func Xsqlite3session_indirect(tls *libc.TLS, pSession uintptr, bIndirect int32) (r int32) { var ret int32 _ = ret Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb)) if bIndirect >= 0 { (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbIndirect = bIndirect } ret = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbIndirect Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb)) return ret } // C documentation // // /* // ** Return true if there have been no changes to monitored tables recorded // ** by the session object passed as the only argument. // */ func Xsqlite3session_isempty(tls *libc.TLS, pSession uintptr) (r int32) { var pTab uintptr var ret int32 _, _ = pTab, ret ret = 0 Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb)) pTab = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpTable for { if !(pTab != 0 && ret == 0) { break } ret = libc.BoolInt32((*TSessionTable)(unsafe.Pointer(pTab)).FnEntry > 0) goto _1 _1: ; pTab = (*TSessionTable)(unsafe.Pointer(pTab)).FpNext } Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb)) return libc.BoolInt32(ret == 0) } // C documentation // // /* // ** Return the amount of heap memory in use. // */ func Xsqlite3session_memory_used(tls *libc.TLS, pSession uintptr) (r Tsqlite3_int64) { return (*Tsqlite3_session)(unsafe.Pointer(pSession)).FnMalloc } // C documentation // // /* // ** Configure the session object passed as the first argument. // */ func Xsqlite3session_object_config(tls *libc.TLS, pSession uintptr, op int32, pArg uintptr) (r int32) { var iArg, iArg1, rc int32 _, _, _ = iArg, iArg1, rc rc = SQLITE_OK switch op { case int32(SQLITE_SESSION_OBJCONFIG_SIZE): iArg = *(*int32)(unsafe.Pointer(pArg)) if iArg >= 0 { if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpTable != 0 { rc = int32(SQLITE_MISUSE) } else { (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbEnableSize = libc.BoolInt32(iArg != 0) } } *(*int32)(unsafe.Pointer(pArg)) = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbEnableSize case int32(SQLITE_SESSION_OBJCONFIG_ROWID): iArg1 = *(*int32)(unsafe.Pointer(pArg)) if iArg1 >= 0 { if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpTable != 0 { rc = int32(SQLITE_MISUSE) } else { (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbImplicitPK = libc.BoolInt32(iArg1 != 0) } } *(*int32)(unsafe.Pointer(pArg)) = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbImplicitPK default: rc = int32(SQLITE_MISUSE) } return rc } // C documentation // // /* // ** Return the maximum size of sqlite3session_changeset() output. // */ func Xsqlite3session_changeset_size(tls *libc.TLS, pSession uintptr) (r Tsqlite3_int64) { return (*Tsqlite3_session)(unsafe.Pointer(pSession)).FnMaxChangesetSize } // C documentation // // /* // ** Do the work for either sqlite3changeset_start() or start_strm(). // */ func _sessionChangesetStart(tls *libc.TLS, pp uintptr, xInput uintptr, pIn uintptr, nChangeset int32, pChangeset uintptr, bInvert int32, bSkipEmpty int32) (r int32) { var nByte, v1 int32 var pRet uintptr _, _, _ = nByte, pRet, v1 /* Number of bytes to allocate for iterator */ _ = libc.Int32FromInt32(0) /* Zero the output variable in case an error occurs. */ *(*uintptr)(unsafe.Pointer(pp)) = uintptr(0) /* Allocate and initialize the iterator structure. */ nByte = int32(100) pRet = Xsqlite3_malloc(tls, nByte) if !(pRet != 0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, pRet, 0, uint32(100)) (*Tsqlite3_changeset_iter)(unsafe.Pointer(pRet)).Fin.FaData = pChangeset (*Tsqlite3_changeset_iter)(unsafe.Pointer(pRet)).Fin.FnData = nChangeset (*Tsqlite3_changeset_iter)(unsafe.Pointer(pRet)).Fin.FxInput = xInput (*Tsqlite3_changeset_iter)(unsafe.Pointer(pRet)).Fin.FpIn = pIn if xInput != 0 { v1 = 0 } else { v1 = int32(1) } (*Tsqlite3_changeset_iter)(unsafe.Pointer(pRet)).Fin.FbEof = v1 (*Tsqlite3_changeset_iter)(unsafe.Pointer(pRet)).FbInvert = bInvert (*Tsqlite3_changeset_iter)(unsafe.Pointer(pRet)).FbSkipEmpty = bSkipEmpty /* Populate the output variable and return success. */ *(*uintptr)(unsafe.Pointer(pp)) = pRet return SQLITE_OK } // C documentation // // /* // ** Create an iterator used to iterate through the contents of a changeset. // */ func Xsqlite3changeset_start(tls *libc.TLS, pp uintptr, nChangeset int32, pChangeset uintptr) (r int32) { return _sessionChangesetStart(tls, pp, uintptr(0), uintptr(0), nChangeset, pChangeset, 0, 0) } func Xsqlite3changeset_start_v2(tls *libc.TLS, pp uintptr, nChangeset int32, pChangeset uintptr, flags int32) (r int32) { var bInvert int32 _ = bInvert bInvert = libc.BoolInt32(!!(flags&libc.Int32FromInt32(SQLITE_CHANGESETSTART_INVERT) != 0)) return _sessionChangesetStart(tls, pp, uintptr(0), uintptr(0), nChangeset, pChangeset, bInvert, 0) } // C documentation // // /* // ** Streaming version of sqlite3changeset_start(). // */ func Xsqlite3changeset_start_strm(tls *libc.TLS, pp uintptr, xInput uintptr, pIn uintptr) (r int32) { return _sessionChangesetStart(tls, pp, xInput, pIn, 0, uintptr(0), 0, 0) } func Xsqlite3changeset_start_v2_strm(tls *libc.TLS, pp uintptr, xInput uintptr, pIn uintptr, flags int32) (r int32) { var bInvert int32 _ = bInvert bInvert = libc.BoolInt32(!!(flags&libc.Int32FromInt32(SQLITE_CHANGESETSTART_INVERT) != 0)) return _sessionChangesetStart(tls, pp, xInput, pIn, 0, uintptr(0), bInvert, 0) } // C documentation // // /* // ** If the SessionInput object passed as the only argument is a streaming // ** object and the buffer is full, discard some data to free up space. // */ func _sessionDiscardData(tls *libc.TLS, pIn uintptr) { var nMove int32 _ = nMove if (*TSessionInput)(unsafe.Pointer(pIn)).FxInput != 0 && (*TSessionInput)(unsafe.Pointer(pIn)).FiNext >= _sessions_strm_chunk_size { nMove = (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FnBuf - (*TSessionInput)(unsafe.Pointer(pIn)).FiNext _ = libc.Int32FromInt32(0) if nMove > 0 { libc.Xmemmove(tls, (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FaBuf, (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FaBuf+uintptr((*TSessionInput)(unsafe.Pointer(pIn)).FiNext), uint32(nMove)) } (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FnBuf -= (*TSessionInput)(unsafe.Pointer(pIn)).FiNext (*TSessionInput)(unsafe.Pointer(pIn)).FiNext = 0 (*TSessionInput)(unsafe.Pointer(pIn)).FnData = (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FnBuf } } // C documentation // // /* // ** Ensure that there are at least nByte bytes available in the buffer. Or, // ** if there are not nByte bytes remaining in the input, that all available // ** data is in the buffer. // ** // ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise. // */ func _sessionInputBuffer(tls *libc.TLS, pIn uintptr, nByte int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* nNew at bp+4 */ int32 var _ /* rc at bp+0 */ int32 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK if (*TSessionInput)(unsafe.Pointer(pIn)).FxInput != 0 { for !((*TSessionInput)(unsafe.Pointer(pIn)).FbEof != 0) && (*TSessionInput)(unsafe.Pointer(pIn)).FiNext+nByte >= (*TSessionInput)(unsafe.Pointer(pIn)).FnData && *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 4)) = _sessions_strm_chunk_size if (*TSessionInput)(unsafe.Pointer(pIn)).FbNoDiscard == 0 { _sessionDiscardData(tls, pIn) } if SQLITE_OK == _sessionBufferGrow(tls, pIn+20, int64(*(*int32)(unsafe.Pointer(bp + 4))), bp) { *(*int32)(unsafe.Pointer(bp)) = (*(*func(*libc.TLS, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TSessionInput)(unsafe.Pointer(pIn)).FxInput})))(tls, (*TSessionInput)(unsafe.Pointer(pIn)).FpIn, (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FaBuf+uintptr((*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FnBuf), bp+4) if *(*int32)(unsafe.Pointer(bp + 4)) == 0 { (*TSessionInput)(unsafe.Pointer(pIn)).FbEof = int32(1) } else { (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FnBuf += *(*int32)(unsafe.Pointer(bp + 4)) } } (*TSessionInput)(unsafe.Pointer(pIn)).FaData = (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FaBuf (*TSessionInput)(unsafe.Pointer(pIn)).FnData = (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FnBuf } } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** When this function is called, *ppRec points to the start of a record // ** that contains nCol values. This function advances the pointer *ppRec // ** until it points to the byte immediately following that record. // */ func _sessionSkipRecord(tls *libc.TLS, ppRec uintptr, nCol int32) { bp := tls.Alloc(16) defer tls.Free(16) var aRec, v2 uintptr var eType, i int32 var _ /* nByte at bp+0 */ int32 _, _, _, _ = aRec, eType, i, v2 aRec = *(*uintptr)(unsafe.Pointer(ppRec)) i = 0 for { if !(i < nCol) { break } v2 = aRec aRec++ eType = int32(*(*Tu8)(unsafe.Pointer(v2))) if eType == int32(SQLITE_TEXT) || eType == int32(SQLITE_BLOB) { aRec += uintptr(_sessionVarintGet(tls, aRec, bp)) aRec += uintptr(*(*int32)(unsafe.Pointer(bp))) } else { if eType == int32(SQLITE_INTEGER) || eType == int32(SQLITE_FLOAT) { aRec += uintptr(8) } } goto _1 _1: ; i++ } *(*uintptr)(unsafe.Pointer(ppRec)) = aRec } // C documentation // // /* // ** This function sets the value of the sqlite3_value object passed as the // ** first argument to a copy of the string or blob held in the aData[] // ** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM // ** error occurs. // */ func _sessionValueSetStr(tls *libc.TLS, pVal uintptr, aData uintptr, nData int32, enc Tu8) (r int32) { var aCopy uintptr _ = aCopy /* In theory this code could just pass SQLITE_TRANSIENT as the final ** argument to sqlite3ValueSetStr() and have the copy created ** automatically. But doing so makes it difficult to detect any OOM ** error. Hence the code to create the copy externally. */ aCopy = Xsqlite3_malloc64(tls, uint64(int64(nData)+int64(1))) if aCopy == uintptr(0) { return int32(SQLITE_NOMEM) } libc.Xmemcpy(tls, aCopy, aData, uint32(nData)) _sqlite3ValueSetStr(tls, pVal, nData, aCopy, enc, __ccgo_fp(Xsqlite3_free)) return SQLITE_OK } // C documentation // // /* // ** Deserialize a single record from a buffer in memory. See "RECORD FORMAT" // ** for details. // ** // ** When this function is called, *paChange points to the start of the record // ** to deserialize. Assuming no error occurs, *paChange is set to point to // ** one byte after the end of the same record before this function returns. // ** If the argument abPK is NULL, then the record contains nCol values. Or, // ** if abPK is other than NULL, then the record contains only the PK fields // ** (in other words, it is a patchset DELETE record). // ** // ** If successful, each element of the apOut[] array (allocated by the caller) // ** is set to point to an sqlite3_value object containing the value read // ** from the corresponding position in the record. If that value is not // ** included in the record (i.e. because the record is part of an UPDATE change // ** and the field was not modified), the corresponding element of apOut[] is // ** set to NULL. // ** // ** It is the responsibility of the caller to free all sqlite_value structures // ** using sqlite3_free(). // ** // ** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned. // ** The apOut[] array may have been partially populated in this case. // */ func _sessionReadRecord(tls *libc.TLS, pIn uintptr, nCol int32, abPK uintptr, apOut uintptr, pbEmpty uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var aVal, v3 uintptr var eType, i, rc, v2, v4 int32 var enc Tu8 var _ /* d at bp+16 */ float64 var _ /* nByte at bp+0 */ int32 var _ /* v at bp+8 */ Tsqlite3_int64 _, _, _, _, _, _, _, _ = aVal, eType, enc, i, rc, v2, v3, v4 /* Used to iterate through columns */ rc = SQLITE_OK _ = libc.Int32FromInt32(0) if pbEmpty != 0 { *(*int32)(unsafe.Pointer(pbEmpty)) = int32(1) } i = 0 for { if !(i < nCol && rc == SQLITE_OK) { break } eType = 0 /* Type of value (SQLITE_NULL, TEXT etc.) */ if abPK != 0 && int32(*(*Tu8)(unsafe.Pointer(abPK + uintptr(i)))) == 0 { goto _1 } rc = _sessionInputBuffer(tls, pIn, int32(9)) if rc == SQLITE_OK { if (*TSessionInput)(unsafe.Pointer(pIn)).FiNext >= (*TSessionInput)(unsafe.Pointer(pIn)).FnData { rc = _sqlite3CorruptError(tls, int32(226700)) } else { v3 = pIn + 8 v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ eType = int32(*(*Tu8)(unsafe.Pointer((*TSessionInput)(unsafe.Pointer(pIn)).FaData + uintptr(v2)))) _ = libc.Int32FromInt32(0) if eType != 0 { if pbEmpty != 0 { *(*int32)(unsafe.Pointer(pbEmpty)) = 0 } *(*uintptr)(unsafe.Pointer(apOut + uintptr(i)*4)) = _sqlite3ValueNew(tls, uintptr(0)) if !(*(*uintptr)(unsafe.Pointer(apOut + uintptr(i)*4)) != 0) { rc = int32(SQLITE_NOMEM) } } } } if rc == SQLITE_OK { aVal = (*TSessionInput)(unsafe.Pointer(pIn)).FaData + uintptr((*TSessionInput)(unsafe.Pointer(pIn)).FiNext) if eType == int32(SQLITE_TEXT) || eType == int32(SQLITE_BLOB) { *(*int32)(unsafe.Pointer(pIn + 8)) += _sessionVarintGet(tls, aVal, bp) rc = _sessionInputBuffer(tls, pIn, *(*int32)(unsafe.Pointer(bp))) if rc == SQLITE_OK { if *(*int32)(unsafe.Pointer(bp)) < 0 || *(*int32)(unsafe.Pointer(bp)) > (*TSessionInput)(unsafe.Pointer(pIn)).FnData-(*TSessionInput)(unsafe.Pointer(pIn)).FiNext { rc = _sqlite3CorruptError(tls, int32(226720)) } else { if eType == int32(SQLITE_TEXT) { v4 = int32(SQLITE_UTF8) } else { v4 = 0 } enc = uint8(v4) rc = _sessionValueSetStr(tls, *(*uintptr)(unsafe.Pointer(apOut + uintptr(i)*4)), (*TSessionInput)(unsafe.Pointer(pIn)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pIn)).FiNext), *(*int32)(unsafe.Pointer(bp)), enc) *(*int32)(unsafe.Pointer(pIn + 8)) += *(*int32)(unsafe.Pointer(bp)) } } } if eType == int32(SQLITE_INTEGER) || eType == int32(SQLITE_FLOAT) { if (*TSessionInput)(unsafe.Pointer(pIn)).FnData-(*TSessionInput)(unsafe.Pointer(pIn)).FiNext < int32(8) { rc = _sqlite3CorruptError(tls, int32(226730)) } else { *(*Tsqlite3_int64)(unsafe.Pointer(bp + 8)) = _sessionGetI64(tls, aVal) if eType == int32(SQLITE_INTEGER) { _sqlite3VdbeMemSetInt64(tls, *(*uintptr)(unsafe.Pointer(apOut + uintptr(i)*4)), *(*Tsqlite3_int64)(unsafe.Pointer(bp + 8))) } else { libc.Xmemcpy(tls, bp+16, bp+8, uint32(8)) _sqlite3VdbeMemSetDouble(tls, *(*uintptr)(unsafe.Pointer(apOut + uintptr(i)*4)), *(*float64)(unsafe.Pointer(bp + 16))) } *(*int32)(unsafe.Pointer(pIn + 8)) += int32(8) } } } goto _1 _1: ; i++ } return rc } // C documentation // // /* // ** The input pointer currently points to the second byte of a table-header. // ** Specifically, to the following: // ** // ** + number of columns in table (varint) // ** + array of PK flags (1 byte per column), // ** + table name (nul terminated). // ** // ** This function ensures that all of the above is present in the input // ** buffer (i.e. that it can be accessed without any calls to xInput()). // ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code. // ** The input pointer is not moved. // */ func _sessionChangesetBufferTblhdr(tls *libc.TLS, pIn uintptr, pnByte uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var nRead, rc int32 var _ /* nCol at bp+0 */ int32 _, _ = nRead, rc rc = SQLITE_OK *(*int32)(unsafe.Pointer(bp)) = 0 nRead = 0 rc = _sessionInputBuffer(tls, pIn, int32(9)) if rc == SQLITE_OK { nRead += _sessionVarintGet(tls, (*TSessionInput)(unsafe.Pointer(pIn)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pIn)).FiNext+nRead), bp) /* The hard upper limit for the number of columns in an SQLite ** database table is, according to sqliteLimit.h, 32676. So ** consider any table-header that purports to have more than 65536 ** columns to be corrupt. This is convenient because otherwise, ** if the (nCol>65536) condition below were omitted, a sufficiently ** large value for nCol may cause nRead to wrap around and become ** negative. Leading to a crash. */ if *(*int32)(unsafe.Pointer(bp)) < 0 || *(*int32)(unsafe.Pointer(bp)) > int32(65536) { rc = _sqlite3CorruptError(tls, int32(226778)) } else { rc = _sessionInputBuffer(tls, pIn, nRead+*(*int32)(unsafe.Pointer(bp))+int32(100)) nRead += *(*int32)(unsafe.Pointer(bp)) } } for rc == SQLITE_OK { for (*TSessionInput)(unsafe.Pointer(pIn)).FiNext+nRead < (*TSessionInput)(unsafe.Pointer(pIn)).FnData && *(*Tu8)(unsafe.Pointer((*TSessionInput)(unsafe.Pointer(pIn)).FaData + uintptr((*TSessionInput)(unsafe.Pointer(pIn)).FiNext+nRead))) != 0 { nRead++ } if (*TSessionInput)(unsafe.Pointer(pIn)).FiNext+nRead < (*TSessionInput)(unsafe.Pointer(pIn)).FnData { break } rc = _sessionInputBuffer(tls, pIn, nRead+int32(100)) } *(*int32)(unsafe.Pointer(pnByte)) = nRead + int32(1) return rc } // C documentation // // /* // ** The input pointer currently points to the first byte of the first field // ** of a record consisting of nCol columns. This function ensures the entire // ** record is buffered. It does not move the input pointer. // ** // ** If successful, SQLITE_OK is returned and *pnByte is set to the size of // ** the record in bytes. Otherwise, an SQLite error code is returned. The // ** final value of *pnByte is undefined in this case. // */ func _sessionChangesetBufferRecord(tls *libc.TLS, pIn uintptr, nCol int32, pnByte uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var eType, i, nByte, rc, v2 int32 var _ /* n at bp+0 */ int32 _, _, _, _, _ = eType, i, nByte, rc, v2 rc = SQLITE_OK nByte = 0 i = 0 for { if !(rc == SQLITE_OK && i < nCol) { break } rc = _sessionInputBuffer(tls, pIn, nByte+int32(10)) if rc == SQLITE_OK { v2 = nByte nByte++ eType = int32(*(*Tu8)(unsafe.Pointer((*TSessionInput)(unsafe.Pointer(pIn)).FaData + uintptr((*TSessionInput)(unsafe.Pointer(pIn)).FiNext+v2)))) if eType == int32(SQLITE_TEXT) || eType == int32(SQLITE_BLOB) { nByte += _sessionVarintGet(tls, (*TSessionInput)(unsafe.Pointer(pIn)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pIn)).FiNext+nByte), bp) nByte += *(*int32)(unsafe.Pointer(bp)) rc = _sessionInputBuffer(tls, pIn, nByte) } else { if eType == int32(SQLITE_INTEGER) || eType == int32(SQLITE_FLOAT) { nByte += int32(8) } } } goto _1 _1: ; i++ } *(*int32)(unsafe.Pointer(pnByte)) = nByte return rc } // C documentation // // /* // ** The input pointer currently points to the second byte of a table-header. // ** Specifically, to the following: // ** // ** + number of columns in table (varint) // ** + array of PK flags (1 byte per column), // ** + table name (nul terminated). // ** // ** This function decodes the table-header and populates the p->nCol, // ** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is // ** also allocated or resized according to the new value of p->nCol. The // ** input pointer is left pointing to the byte following the table header. // ** // ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code // ** is returned and the final values of the various fields enumerated above // ** are undefined. // */ func _sessionChangesetReadTblhdr(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iPK Tsize_t var nByte, nVarint, v2 int32 var v1 uintptr var _ /* nCopy at bp+4 */ int32 var _ /* rc at bp+0 */ int32 _, _, _, _, _ = iPK, nByte, nVarint, v1, v2 _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp)) = _sessionChangesetBufferTblhdr(tls, p, bp+4) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { nVarint = _sessionVarintGet(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FaData+uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext), p+80) if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol > 0 { *(*int32)(unsafe.Pointer(bp + 4)) -= nVarint (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext += nVarint nByte = int32(uint32((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*uint32(4)*uint32(2) + uint32(*(*int32)(unsafe.Pointer(bp + 4)))) (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Ftblhdr.FnBuf = 0 _sessionBufferGrow(tls, p+44, int64(nByte), bp) } else { *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(226866)) } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { iPK = uint32(4) * uint32((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol) * uint32(2) libc.Xmemset(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Ftblhdr.FaBuf, 0, iPK) libc.Xmemcpy(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Ftblhdr.FaBuf+uintptr(iPK), (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FaData+uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext), uint32(*(*int32)(unsafe.Pointer(bp + 4)))) (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext += *(*int32)(unsafe.Pointer(bp + 4)) } (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Ftblhdr.FaBuf if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue == uintptr(0) { (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK = uintptr(0) (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FzTab = uintptr(0) } else { (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol*int32(2))*4 if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK != 0 { v1 = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol) } else { v1 = uintptr(0) } (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FzTab = v1 } v2 = *(*int32)(unsafe.Pointer(bp)) (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = v2 return v2 } // C documentation // // /* // ** Advance the changeset iterator to the next change. The differences between // ** this function and sessionChangesetNext() are that // ** // ** * If pbEmpty is not NULL and the change is a no-op UPDATE (an UPDATE // ** that modifies no columns), this function sets (*pbEmpty) to 1. // ** // ** * If the iterator is configured to skip no-op UPDATEs, // ** sessionChangesetNext() does that. This function does not. // */ func _sessionChangesetNextOne(tls *libc.TLS, p uintptr, paRec uintptr, pnRec uintptr, pbNew uintptr, pbEmpty uintptr) (r int32) { var abPK, apNew, apOld, v12, v13, v14, v3, v6, v9 uintptr var i, nVal, v10, v16, v2, v4, v5, v7, v8 int32 var op Tu8 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = abPK, apNew, apOld, i, nVal, op, v10, v12, v13, v14, v16, v2, v3, v4, v5, v6, v7, v8, v9 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If the iterator is in the error-state, return immediately. */ if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc != SQLITE_OK { return (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc } /* Free the current contents of p->apValue[], if any. */ if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue != 0 { i = 0 for { if !(i < (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol*int32(2)) { break } _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i)*4))) goto _1 _1: ; i++ } libc.Xmemset(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue, 0, uint32(4)*uint32((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*uint32(2)) } /* Make sure the buffer contains at least 10 bytes of input data, or all ** remaining data if there are less than 10 bytes available. This is ** sufficient either for the 'T' or 'P' byte and the varint that follows ** it, or for the two single byte values otherwise. */ (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = _sessionInputBuffer(tls, p, int32(2)) if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc != SQLITE_OK { return (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc } /* If the iterator is already at the end of the changeset, return DONE. */ if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext >= (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FnData { return int32(SQLITE_DONE) } _sessionDiscardData(tls, p) (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiCurrent = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext v3 = p + 8 v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ op = *(*Tu8)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FaData + uintptr(v2))) for int32(op) == int32('T') || int32(op) == int32('P') { if pbNew != 0 { *(*int32)(unsafe.Pointer(pbNew)) = int32(1) } (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset = libc.BoolInt32(int32(op) == int32('P')) if _sessionChangesetReadTblhdr(tls, p) != 0 { return (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc } v4 = _sessionInputBuffer(tls, p, int32(2)) (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = v4 if v4 != 0 { return (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc } (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiCurrent = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext >= (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FnData { return int32(SQLITE_DONE) } v6 = p + 8 v5 = *(*int32)(unsafe.Pointer(v6)) *(*int32)(unsafe.Pointer(v6))++ op = *(*Tu8)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FaData + uintptr(v5))) } if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FzTab == uintptr(0) || (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset != 0 && (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbInvert != 0 { /* The first record in the changeset is not a table header. Must be a ** corrupt changeset. */ _ = libc.Int32FromInt32(0) v7 = _sqlite3CorruptError(tls, int32(226952)) (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = v7 return v7 } (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop = int32(op) v9 = p + 8 v8 = *(*int32)(unsafe.Pointer(v9)) *(*int32)(unsafe.Pointer(v9))++ (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbIndirect = int32(*(*Tu8)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FaData + uintptr(v8)))) if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop != int32(SQLITE_UPDATE) && (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop != int32(SQLITE_DELETE) && (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop != int32(SQLITE_INSERT) { v10 = _sqlite3CorruptError(tls, int32(226958)) (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = v10 return v10 } if paRec != 0 { /* Number of values to buffer */ if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset == 0 && int32(op) == int32(SQLITE_UPDATE) { nVal = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol * int32(2) } else { if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset != 0 && int32(op) == int32(SQLITE_DELETE) { nVal = 0 i = 0 for { if !(i < (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol) { break } if *(*Tu8)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK + uintptr(i))) != 0 { nVal++ } goto _11 _11: ; i++ } } else { nVal = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol } } (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = _sessionChangesetBufferRecord(tls, p, nVal, pnRec) if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc != SQLITE_OK { return (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc } *(*uintptr)(unsafe.Pointer(paRec)) = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FaData + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext) (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext += *(*int32)(unsafe.Pointer(pnRec)) } else { if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbInvert != 0 { v12 = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*4 } else { v12 = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue } apOld = v12 if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbInvert != 0 { v13 = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue } else { v13 = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*4 } apNew = v13 /* If this is an UPDATE or DELETE, read the old.* record. */ if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop != int32(SQLITE_INSERT) && ((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset == 0 || (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop == int32(SQLITE_DELETE)) { if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset != 0 { v14 = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK } else { v14 = uintptr(0) } abPK = v14 (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = _sessionReadRecord(tls, p, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol, abPK, apOld, uintptr(0)) if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc != SQLITE_OK { return (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc } } /* If this is an INSERT or UPDATE, read the new.* record. */ if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop != int32(SQLITE_DELETE) { (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = _sessionReadRecord(tls, p, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol, uintptr(0), apNew, pbEmpty) if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc != SQLITE_OK { return (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc } } if ((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset != 0 || (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbInvert != 0) && (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop == int32(SQLITE_UPDATE) { /* If this is an UPDATE that is part of a patchset, then all PK and ** modified fields are present in the new.* record. The old.* record ** is currently completely empty. This block shifts the PK fields from ** new.* to old.*, to accommodate the code that reads these arrays. */ i = 0 for { if !(i < (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol) { break } _ = libc.Int32FromInt32(0) if *(*Tu8)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK + uintptr(i))) != 0 { _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i)*4)) = *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i+(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*4)) if *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i)*4)) == uintptr(0) { v16 = _sqlite3CorruptError(tls, int32(227002)) (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = v16 return v16 } *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i+(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*4)) = uintptr(0) } goto _15 _15: ; i++ } } else { if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbInvert != 0 { if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop == int32(SQLITE_INSERT) { (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop = int32(SQLITE_DELETE) } else { if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop == int32(SQLITE_DELETE) { (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop = int32(SQLITE_INSERT) } } } } /* If this is an UPDATE that is part of a changeset, then check that ** there are no fields in the old.* record that are not (a) PK fields, ** or (b) also present in the new.* record. ** ** Such records are technically corrupt, but the rebaser was at one ** point generating them. Under most circumstances this is benign, but ** can cause spurious SQLITE_RANGE errors when applying the changeset. */ if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset == 0 && (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop == int32(SQLITE_UPDATE) { i = 0 for { if !(i < (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol) { break } if int32(*(*Tu8)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK + uintptr(i)))) == 0 && *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i+(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*4)) == uintptr(0) { _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i)*4))) *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i)*4)) = uintptr(0) } goto _17 _17: ; i++ } } } return int32(SQLITE_ROW) } // C documentation // // /* // ** Advance the changeset iterator to the next change. // ** // ** If both paRec and pnRec are NULL, then this function works like the public // ** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the // ** sqlite3changeset_new() and old() APIs may be used to query for values. // ** // ** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change // ** record is written to *paRec before returning and the number of bytes in // ** the record to *pnRec. // ** // ** Either way, this function returns SQLITE_ROW if the iterator is // ** successfully advanced to the next change in the changeset, an SQLite // ** error code if an error occurs, or SQLITE_DONE if there are no further // ** changes in the changeset. // */ func _sessionChangesetNext(tls *libc.TLS, p uintptr, paRec uintptr, pnRec uintptr, pbNew uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* bEmpty at bp+0 */ int32 _ = rc for cond := true; cond; cond = rc == int32(SQLITE_ROW) && (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbSkipEmpty != 0 && *(*int32)(unsafe.Pointer(bp)) != 0 { *(*int32)(unsafe.Pointer(bp)) = 0 rc = _sessionChangesetNextOne(tls, p, paRec, pnRec, pbNew, bp) } return rc } // C documentation // // /* // ** Advance an iterator created by sqlite3changeset_start() to the next // ** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE // ** or SQLITE_CORRUPT. // ** // ** This function may not be called on iterators passed to a conflict handler // ** callback by changeset_apply(). // */ func Xsqlite3changeset_next(tls *libc.TLS, p uintptr) (r int32) { return _sessionChangesetNext(tls, p, uintptr(0), uintptr(0), uintptr(0)) } // C documentation // // /* // ** The following function extracts information on the current change // ** from a changeset iterator. It may only be called after changeset_next() // ** has returned SQLITE_ROW. // */ func Xsqlite3changeset_op(tls *libc.TLS, pIter uintptr, pzTab uintptr, pnCol uintptr, pOp uintptr, pbIndirect uintptr) (r int32) { *(*int32)(unsafe.Pointer(pOp)) = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fop *(*int32)(unsafe.Pointer(pnCol)) = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol *(*uintptr)(unsafe.Pointer(pzTab)) = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab if pbIndirect != 0 { *(*int32)(unsafe.Pointer(pbIndirect)) = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect } return SQLITE_OK } // C documentation // // /* // ** Return information regarding the PRIMARY KEY and number of columns in // ** the database table affected by the change that pIter currently points // ** to. This function may only be called after changeset_next() returns // ** SQLITE_ROW. // */ func Xsqlite3changeset_pk(tls *libc.TLS, pIter uintptr, pabPK uintptr, pnCol uintptr) (r int32) { *(*uintptr)(unsafe.Pointer(pabPK)) = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FabPK if pnCol != 0 { *(*int32)(unsafe.Pointer(pnCol)) = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol } return SQLITE_OK } // C documentation // // /* // ** This function may only be called while the iterator is pointing to an // ** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()). // ** Otherwise, SQLITE_MISUSE is returned. // ** // ** It sets *ppValue to point to an sqlite3_value structure containing the // ** iVal'th value in the old.* record. Or, if that particular value is not // ** included in the record (because the change is an UPDATE and the field // ** was not modified and is not a PK column), set *ppValue to NULL. // ** // ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is // ** not modified. Otherwise, SQLITE_OK. // */ func Xsqlite3changeset_old(tls *libc.TLS, pIter uintptr, iVal int32, ppValue uintptr) (r int32) { if (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fop != int32(SQLITE_UPDATE) && (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fop != int32(SQLITE_DELETE) { return int32(SQLITE_MISUSE) } if iVal < 0 || iVal >= (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol { return int32(SQLITE_RANGE) } *(*uintptr)(unsafe.Pointer(ppValue)) = *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FapValue + uintptr(iVal)*4)) return SQLITE_OK } // C documentation // // /* // ** This function may only be called while the iterator is pointing to an // ** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()). // ** Otherwise, SQLITE_MISUSE is returned. // ** // ** It sets *ppValue to point to an sqlite3_value structure containing the // ** iVal'th value in the new.* record. Or, if that particular value is not // ** included in the record (because the change is an UPDATE and the field // ** was not modified), set *ppValue to NULL. // ** // ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is // ** not modified. Otherwise, SQLITE_OK. // */ func Xsqlite3changeset_new(tls *libc.TLS, pIter uintptr, iVal int32, ppValue uintptr) (r int32) { if (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fop != int32(SQLITE_UPDATE) && (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fop != int32(SQLITE_INSERT) { return int32(SQLITE_MISUSE) } if iVal < 0 || iVal >= (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol { return int32(SQLITE_RANGE) } *(*uintptr)(unsafe.Pointer(ppValue)) = *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FapValue + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol+iVal)*4)) return SQLITE_OK } /* ** The following two macros are used internally. They are similar to the ** sqlite3changeset_new() and sqlite3changeset_old() functions, except that ** they omit all error checking and return a pointer to the requested value. */ // C documentation // // /* // ** This function may only be called with a changeset iterator that has been // ** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT // ** conflict-handler function. Otherwise, SQLITE_MISUSE is returned. // ** // ** If successful, *ppValue is set to point to an sqlite3_value structure // ** containing the iVal'th value of the conflicting record. // ** // ** If value iVal is out-of-range or some other error occurs, an SQLite error // ** code is returned. Otherwise, SQLITE_OK. // */ func Xsqlite3changeset_conflict(tls *libc.TLS, pIter uintptr, iVal int32, ppValue uintptr) (r int32) { if !((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FpConflict != 0) { return int32(SQLITE_MISUSE) } if iVal < 0 || iVal >= (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol { return int32(SQLITE_RANGE) } *(*uintptr)(unsafe.Pointer(ppValue)) = Xsqlite3_column_value(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FpConflict, iVal) return SQLITE_OK } // C documentation // // /* // ** This function may only be called with an iterator passed to an // ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case // ** it sets the output variable to the total number of known foreign key // ** violations in the destination database and returns SQLITE_OK. // ** // ** In all other cases this function returns SQLITE_MISUSE. // */ func Xsqlite3changeset_fk_conflicts(tls *libc.TLS, pIter uintptr, pnOut uintptr) (r int32) { if (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FpConflict != 0 || (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FapValue != 0 { return int32(SQLITE_MISUSE) } *(*int32)(unsafe.Pointer(pnOut)) = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol return SQLITE_OK } // C documentation // // /* // ** Finalize an iterator allocated with sqlite3changeset_start(). // ** // ** This function may not be called on iterators passed to a conflict handler // ** callback by changeset_apply(). // */ func Xsqlite3changeset_finalize(tls *libc.TLS, p uintptr) (r int32) { var i, rc int32 _, _ = i, rc rc = SQLITE_OK if p != 0 { /* Used to iterate through p->apValue[] */ rc = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue != 0 { i = 0 for { if !(i < (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol*int32(2)) { break } _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i)*4))) goto _1 _1: ; i++ } } Xsqlite3_free(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Ftblhdr.FaBuf) Xsqlite3_free(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.Fbuf.FaBuf) Xsqlite3_free(tls, p) } return rc } func _sessionChangesetInvert(tls *libc.TLS, pInput uintptr, xOutput uintptr, pOut uintptr, pnInverted uintptr, ppInverted uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var abPK, apVal, pVal, pVal1, v7 uintptr var bIndirect, eType2, iCol, nVar, v1, v2, v3, v5 int32 var eType Tu8 var _ /* nByte at bp+32 */ int32 var _ /* nByte at bp+36 */ int32 var _ /* nCol at bp+16 */ int32 var _ /* rc at bp+0 */ int32 var _ /* sOut at bp+4 */ TSessionBuffer var _ /* sPK at bp+20 */ TSessionBuffer _, _, _, _, _, _, _, _, _, _, _, _, _, _ = abPK, apVal, bIndirect, eType, eType2, iCol, nVar, pVal, pVal1, v1, v2, v3, v5, v7 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK /* Output buffer */ *(*int32)(unsafe.Pointer(bp + 16)) = 0 /* Number of cols in current table */ abPK = uintptr(0) /* PK array for current table */ apVal = uintptr(0) /* Space for values for UPDATE inversion */ *(*TSessionBuffer)(unsafe.Pointer(bp + 20)) = TSessionBuffer{} /* PK array for current table */ /* Initialize the output buffer */ libc.Xmemset(tls, bp+4, 0, uint32(12)) /* Zero the output variables in case an error occurs. */ if ppInverted != 0 { *(*uintptr)(unsafe.Pointer(ppInverted)) = uintptr(0) *(*int32)(unsafe.Pointer(pnInverted)) = 0 } for int32(1) != 0 { /* Test for EOF. */ v1 = _sessionInputBuffer(tls, pInput, int32(2)) *(*int32)(unsafe.Pointer(bp)) = v1 if v1 != 0 { goto finished_invert } if (*TSessionInput)(unsafe.Pointer(pInput)).FiNext >= (*TSessionInput)(unsafe.Pointer(pInput)).FnData { break } eType = *(*Tu8)(unsafe.Pointer((*TSessionInput)(unsafe.Pointer(pInput)).FaData + uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext))) switch int32(eType) { case int32('T'): (*TSessionInput)(unsafe.Pointer(pInput)).FiNext++ v2 = _sessionChangesetBufferTblhdr(tls, pInput, bp+32) *(*int32)(unsafe.Pointer(bp)) = v2 if v2 != 0 { goto finished_invert } nVar = _sessionVarintGet(tls, (*TSessionInput)(unsafe.Pointer(pInput)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext), bp+16) (*(*TSessionBuffer)(unsafe.Pointer(bp + 20))).FnBuf = 0 _sessionAppendBlob(tls, bp+20, (*TSessionInput)(unsafe.Pointer(pInput)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext+nVar), *(*int32)(unsafe.Pointer(bp + 16)), bp) _sessionAppendByte(tls, bp+4, eType, bp) _sessionAppendBlob(tls, bp+4, (*TSessionInput)(unsafe.Pointer(pInput)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext), *(*int32)(unsafe.Pointer(bp + 32)), bp) if *(*int32)(unsafe.Pointer(bp)) != 0 { goto finished_invert } *(*int32)(unsafe.Pointer(pInput + 8)) += *(*int32)(unsafe.Pointer(bp + 32)) Xsqlite3_free(tls, apVal) apVal = uintptr(0) abPK = (*(*TSessionBuffer)(unsafe.Pointer(bp + 20))).FaBuf case int32(SQLITE_INSERT): fallthrough case int32(SQLITE_DELETE): bIndirect = int32(*(*Tu8)(unsafe.Pointer((*TSessionInput)(unsafe.Pointer(pInput)).FaData + uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext+int32(1))))) if int32(eType) == int32(SQLITE_DELETE) { v3 = int32(SQLITE_INSERT) } else { v3 = int32(SQLITE_DELETE) } eType2 = v3 *(*int32)(unsafe.Pointer(pInput + 8)) += int32(2) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp)) = _sessionChangesetBufferRecord(tls, pInput, *(*int32)(unsafe.Pointer(bp + 16)), bp+36) _sessionAppendByte(tls, bp+4, uint8(eType2), bp) _sessionAppendByte(tls, bp+4, uint8(bIndirect), bp) _sessionAppendBlob(tls, bp+4, (*TSessionInput)(unsafe.Pointer(pInput)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext), *(*int32)(unsafe.Pointer(bp + 36)), bp) *(*int32)(unsafe.Pointer(pInput + 8)) += *(*int32)(unsafe.Pointer(bp + 36)) if *(*int32)(unsafe.Pointer(bp)) != 0 { goto finished_invert } case int32(SQLITE_UPDATE): if uintptr(0) == apVal { apVal = Xsqlite3_malloc64(tls, uint64(uint32(4)*uint32(*(*int32)(unsafe.Pointer(bp + 16)))*uint32(2))) if uintptr(0) == apVal { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_NOMEM) goto finished_invert } libc.Xmemset(tls, apVal, 0, uint32(4)*uint32(*(*int32)(unsafe.Pointer(bp + 16)))*uint32(2)) } /* Write the header for the new UPDATE change. Same as the original. */ _sessionAppendByte(tls, bp+4, eType, bp) _sessionAppendByte(tls, bp+4, *(*Tu8)(unsafe.Pointer((*TSessionInput)(unsafe.Pointer(pInput)).FaData + uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext+int32(1)))), bp) /* Read the old.* and new.* records for the update change. */ *(*int32)(unsafe.Pointer(pInput + 8)) += int32(2) *(*int32)(unsafe.Pointer(bp)) = _sessionReadRecord(tls, pInput, *(*int32)(unsafe.Pointer(bp + 16)), uintptr(0), apVal, uintptr(0)) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sessionReadRecord(tls, pInput, *(*int32)(unsafe.Pointer(bp + 16)), uintptr(0), apVal+uintptr(*(*int32)(unsafe.Pointer(bp + 16)))*4, uintptr(0)) } /* Write the new old.* record. Consists of the PK columns from the ** original old.* record, and the other values from the original ** new.* record. */ iCol = 0 for { if !(iCol < *(*int32)(unsafe.Pointer(bp + 16))) { break } if *(*Tu8)(unsafe.Pointer(abPK + uintptr(iCol))) != 0 { v5 = 0 } else { v5 = *(*int32)(unsafe.Pointer(bp + 16)) } pVal = *(*uintptr)(unsafe.Pointer(apVal + uintptr(iCol+v5)*4)) _sessionAppendValue(tls, bp+4, pVal, bp) goto _4 _4: ; iCol++ } /* Write the new new.* record. Consists of a copy of all values ** from the original old.* record, except for the PK columns, which ** are set to "undefined". */ iCol = 0 for { if !(iCol < *(*int32)(unsafe.Pointer(bp + 16))) { break } if *(*Tu8)(unsafe.Pointer(abPK + uintptr(iCol))) != 0 { v7 = uintptr(0) } else { v7 = *(*uintptr)(unsafe.Pointer(apVal + uintptr(iCol)*4)) } pVal1 = v7 _sessionAppendValue(tls, bp+4, pVal1, bp) goto _6 _6: ; iCol++ } iCol = 0 for { if !(iCol < *(*int32)(unsafe.Pointer(bp + 16))*int32(2)) { break } _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer(apVal + uintptr(iCol)*4))) goto _8 _8: ; iCol++ } libc.Xmemset(tls, apVal, 0, uint32(4)*uint32(*(*int32)(unsafe.Pointer(bp + 16)))*uint32(2)) if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { goto finished_invert } default: *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(227367)) goto finished_invert } _ = libc.Int32FromInt32(0) if xOutput != 0 && (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf >= _sessions_strm_chunk_size { *(*int32)(unsafe.Pointer(bp)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{xOutput})))(tls, pOut, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf) (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf = 0 if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { goto finished_invert } } } _ = libc.Int32FromInt32(0) if pnInverted != 0 && ppInverted != 0 { *(*int32)(unsafe.Pointer(pnInverted)) = (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf *(*uintptr)(unsafe.Pointer(ppInverted)) = (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf = uintptr(0) } else { if (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf > 0 && xOutput != uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{xOutput})))(tls, pOut, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf) } } goto finished_invert finished_invert: ; Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf) Xsqlite3_free(tls, apVal) Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 20))).FaBuf) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Invert a changeset object. // */ func Xsqlite3changeset_invert(tls *libc.TLS, nChangeset int32, pChangeset uintptr, pnInverted uintptr, ppInverted uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* sInput at bp+0 */ TSessionInput /* Set up the input stream */ libc.Xmemset(tls, bp, 0, uint32(44)) (*(*TSessionInput)(unsafe.Pointer(bp))).FnData = nChangeset (*(*TSessionInput)(unsafe.Pointer(bp))).FaData = pChangeset return _sessionChangesetInvert(tls, bp, uintptr(0), uintptr(0), pnInverted, ppInverted) } // C documentation // // /* // ** Streaming version of sqlite3changeset_invert(). // */ func Xsqlite3changeset_invert_strm(tls *libc.TLS, xInput uintptr, pIn uintptr, xOutput uintptr, pOut uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var rc int32 var _ /* sInput at bp+0 */ TSessionInput _ = rc /* Set up the input stream */ libc.Xmemset(tls, bp, 0, uint32(44)) (*(*TSessionInput)(unsafe.Pointer(bp))).FxInput = xInput (*(*TSessionInput)(unsafe.Pointer(bp))).FpIn = pIn rc = _sessionChangesetInvert(tls, bp, xOutput, pOut, uintptr(0), uintptr(0)) Xsqlite3_free(tls, (*(*TSessionInput)(unsafe.Pointer(bp))).Fbuf.FaBuf) return rc } type TSessionUpdate = struct { FpStmt uintptr FaMask uintptr FpNext uintptr } type SessionUpdate = TSessionUpdate type TSessionUpdate1 = struct { FpStmt uintptr FaMask uintptr FpNext uintptr } type SessionUpdate1 = TSessionUpdate1 type TSessionApplyCtx = struct { Fdb uintptr FpDelete uintptr FpInsert uintptr FpSelect uintptr FnCol int32 FazCol uintptr FabPK uintptr FaUpdateMask uintptr FpUp uintptr FbStat1 int32 FbDeferConstraints int32 FbInvertConstraints int32 Fconstraints TSessionBuffer Frebase TSessionBuffer FbRebaseStarted Tu8 FbRebase Tu8 FbIgnoreNoop Tu8 FbRowid int32 } type SessionApplyCtx = TSessionApplyCtx type TSessionApplyCtx1 = struct { Fdb uintptr FpDelete uintptr FpInsert uintptr FpSelect uintptr FnCol int32 FazCol uintptr FabPK uintptr FaUpdateMask uintptr FpUp uintptr FbStat1 int32 FbDeferConstraints int32 FbInvertConstraints int32 Fconstraints TSessionBuffer Frebase TSessionBuffer FbRebaseStarted Tu8 FbRebase Tu8 FbIgnoreNoop Tu8 FbRowid int32 } type SessionApplyCtx1 = TSessionApplyCtx1 /* Number of prepared UPDATE statements to cache. */ // C documentation // // /* // ** Find a prepared UPDATE statement suitable for the UPDATE step currently // ** being visited by the iterator. The UPDATE is of the form: // ** // ** UPDATE tbl SET col = ?, col2 = ? WHERE pk1 IS ? AND pk2 IS ? // */ func _sessionUpdateFind(tls *libc.TLS, pIter uintptr, p uintptr, bPatchset int32, ppStmt uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bStat1, ii, nByte, nCol, nU32, nUp int32 var pUp, pp, zSep, zSql uintptr var _ /* buf at bp+4 */ TSessionBuffer var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _ = bStat1, ii, nByte, nCol, nU32, nUp, pUp, pp, zSep, zSql *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK pUp = uintptr(0) nCol = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol nU32 = ((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol + int32(33)) / int32(32) if (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask == uintptr(0) { (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask = Xsqlite3_malloc(tls, int32(uint32(nU32)*uint32(4))) if (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_NOMEM) } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { libc.Xmemset(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask, 0, uint32(nU32)*uint32(4)) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_CORRUPT) ii = 0 for { if !(ii < (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol) { break } if *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FapValue + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol+ii)*4)) != 0 { *(*Tu32)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask + uintptr(ii/int32(32))*4)) |= uint32(libc.Int32FromInt32(1) << (ii % libc.Int32FromInt32(32))) *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK } goto _1 _1: ; ii++ } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { if bPatchset != 0 { *(*Tu32)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask + uintptr(nCol/int32(32))*4)) |= uint32(libc.Int32FromInt32(1) << (nCol % libc.Int32FromInt32(32))) } if (*TSessionApplyCtx)(unsafe.Pointer(p)).FpUp != 0 { nUp = 0 pp = p + 32 for int32(1) != 0 { nUp++ if 0 == libc.Xmemcmp(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask, (*TSessionUpdate)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FaMask, uint32(nU32)*uint32(4)) { pUp = *(*uintptr)(unsafe.Pointer(pp)) *(*uintptr)(unsafe.Pointer(pp)) = (*TSessionUpdate)(unsafe.Pointer(pUp)).FpNext (*TSessionUpdate)(unsafe.Pointer(pUp)).FpNext = (*TSessionApplyCtx)(unsafe.Pointer(p)).FpUp (*TSessionApplyCtx)(unsafe.Pointer(p)).FpUp = pUp break } if (*TSessionUpdate)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FpNext != 0 { pp = *(*uintptr)(unsafe.Pointer(pp)) + 8 } else { if nUp >= int32(SESSION_UPDATE_CACHE_SZ) { Xsqlite3_finalize(tls, (*TSessionUpdate)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FpStmt) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(pp))) *(*uintptr)(unsafe.Pointer(pp)) = uintptr(0) } break } } } if pUp == uintptr(0) { nByte = int32(uint32(12) * uint32(nU32) * uint32(4)) bStat1 = libc.BoolInt32(Xsqlite3_stricmp(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab, __ccgo_ts+11582) == 0) pUp = Xsqlite3_malloc(tls, nByte) if pUp == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_NOMEM) } else { zSep = __ccgo_ts + 1648 libc.Xmemset(tls, bp+4, 0, uint32(12)) (*TSessionUpdate)(unsafe.Pointer(pUp)).FaMask = pUp + 1*12 libc.Xmemcpy(tls, (*TSessionUpdate)(unsafe.Pointer(pUp)).FaMask, (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask, uint32(nU32)*uint32(4)) _sessionAppendStr(tls, bp+4, __ccgo_ts+33935, bp) _sessionAppendIdent(tls, bp+4, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab, bp) _sessionAppendStr(tls, bp+4, __ccgo_ts+33948, bp) /* Create the assignments part of the UPDATE */ ii = 0 for { if !(ii < (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol) { break } if int32(*(*Tu8)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK + uintptr(ii)))) == 0 && *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FapValue + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol+ii)*4)) != 0 { _sessionAppendStr(tls, bp+4, zSep, bp) _sessionAppendIdent(tls, bp+4, *(*uintptr)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(ii)*4)), bp) _sessionAppendStr(tls, bp+4, __ccgo_ts+33954, bp) _sessionAppendInteger(tls, bp+4, ii*int32(2)+int32(1), bp) zSep = __ccgo_ts + 14990 } goto _2 _2: ; ii++ } /* Create the WHERE clause part of the UPDATE */ zSep = __ccgo_ts + 1648 _sessionAppendStr(tls, bp+4, __ccgo_ts+33959, bp) ii = 0 for { if !(ii < (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol) { break } if *(*Tu8)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK + uintptr(ii))) != 0 || bPatchset == 0 && *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FapValue + uintptr(ii)*4)) != 0 { _sessionAppendStr(tls, bp+4, zSep, bp) if bStat1 != 0 && ii == int32(1) { _ = libc.Int32FromInt32(0) _sessionAppendStr(tls, bp+4, __ccgo_ts+33967, bp) } else { _sessionAppendIdent(tls, bp+4, *(*uintptr)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(ii)*4)), bp) _sessionAppendStr(tls, bp+4, __ccgo_ts+34042, bp) _sessionAppendInteger(tls, bp+4, ii*int32(2)+int32(2), bp) } zSep = __ccgo_ts + 22013 } goto _3 _3: ; ii++ } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { zSql = (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_prepare_v2(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).Fdb, zSql, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf, pUp, uintptr(0)) } if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { Xsqlite3_free(tls, pUp) pUp = uintptr(0) } else { (*TSessionUpdate)(unsafe.Pointer(pUp)).FpNext = (*TSessionApplyCtx)(unsafe.Pointer(p)).FpUp (*TSessionApplyCtx)(unsafe.Pointer(p)).FpUp = pUp } Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf) } } } _ = libc.Int32FromInt32(0) if pUp != 0 { *(*uintptr)(unsafe.Pointer(ppStmt)) = (*TSessionUpdate)(unsafe.Pointer(pUp)).FpStmt } else { *(*uintptr)(unsafe.Pointer(ppStmt)) = uintptr(0) } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Free all cached UPDATE statements. // */ func _sessionUpdateFree(tls *libc.TLS, p uintptr) { var pNext, pUp uintptr _, _ = pNext, pUp pUp = (*TSessionApplyCtx)(unsafe.Pointer(p)).FpUp for { if !(pUp != 0) { break } pNext = (*TSessionUpdate)(unsafe.Pointer(pUp)).FpNext Xsqlite3_finalize(tls, (*TSessionUpdate)(unsafe.Pointer(pUp)).FpStmt) Xsqlite3_free(tls, pUp) goto _1 _1: ; pUp = pNext } (*TSessionApplyCtx)(unsafe.Pointer(p)).FpUp = uintptr(0) Xsqlite3_free(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask) (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask = uintptr(0) } // C documentation // // /* // ** Formulate a statement to DELETE a row from database db. Assuming a table // ** structure like this: // ** // ** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c)); // ** // ** The DELETE statement looks like this: // ** // ** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4) // ** // ** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require // ** matching b and d values, or 1 otherwise. The second case comes up if the // ** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE. // ** // ** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left // ** pointing to the prepared version of the SQL statement. // */ func _sessionDeleteRow(tls *libc.TLS, db uintptr, zTab uintptr, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, nPk int32 var zSep uintptr var _ /* buf at bp+4 */ TSessionBuffer var _ /* rc at bp+0 */ int32 _, _, _ = i, nPk, zSep zSep = __ccgo_ts + 1648 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*TSessionBuffer)(unsafe.Pointer(bp + 4)) = TSessionBuffer{} nPk = 0 _sessionAppendStr(tls, bp+4, __ccgo_ts+34048, bp) _sessionAppendIdent(tls, bp+4, zTab, bp) _sessionAppendStr(tls, bp+4, __ccgo_ts+33959, bp) i = 0 for { if !(i < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol) { break } if *(*Tu8)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK + uintptr(i))) != 0 { nPk++ _sessionAppendStr(tls, bp+4, zSep, bp) _sessionAppendIdent(tls, bp+4, *(*uintptr)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(i)*4)), bp) _sessionAppendStr(tls, bp+4, __ccgo_ts+33954, bp) _sessionAppendInteger(tls, bp+4, i+int32(1), bp) zSep = __ccgo_ts + 22013 } goto _1 _1: ; i++ } if nPk < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol { _sessionAppendStr(tls, bp+4, __ccgo_ts+34066, bp) _sessionAppendInteger(tls, bp+4, (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol+int32(1), bp) _sessionAppendStr(tls, bp+4, __ccgo_ts+33530, bp) zSep = __ccgo_ts + 1648 i = 0 for { if !(i < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol) { break } if !(*(*Tu8)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK + uintptr(i))) != 0) { _sessionAppendStr(tls, bp+4, zSep, bp) _sessionAppendIdent(tls, bp+4, *(*uintptr)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(i)*4)), bp) _sessionAppendStr(tls, bp+4, __ccgo_ts+34042, bp) _sessionAppendInteger(tls, bp+4, i+int32(1), bp) zSep = __ccgo_ts + 34074 } goto _2 _2: ; i++ } _sessionAppendStr(tls, bp+4, __ccgo_ts+5106, bp) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_prepare_v2(tls, db, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf, p+4, uintptr(0)) } Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Formulate and prepare an SQL statement to query table zTab by primary // ** key. Assuming the following table structure: // ** // ** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c)); // ** // ** The SELECT statement looks like this: // ** // ** SELECT * FROM x WHERE a = ?1 AND c = ?3 // ** // ** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left // ** pointing to the prepared version of the SQL statement. // */ func _sessionSelectRow(tls *libc.TLS, db uintptr, zTab uintptr, p uintptr) (r int32) { /* TODO */ return _sessionSelectStmt(tls, db, int32((*TSessionApplyCtx)(unsafe.Pointer(p)).FbIgnoreNoop), __ccgo_ts+6585, zTab, (*TSessionApplyCtx)(unsafe.Pointer(p)).FbRowid, (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol, (*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol, (*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK, p+12) } // C documentation // // /* // ** Formulate and prepare an INSERT statement to add a record to table zTab. // ** For example: // ** // ** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...); // ** // ** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left // ** pointing to the prepared version of the SQL statement. // */ func _sessionInsertRow(tls *libc.TLS, db uintptr, zTab uintptr, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i int32 var _ /* buf at bp+4 */ TSessionBuffer var _ /* rc at bp+0 */ int32 _ = i *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*TSessionBuffer)(unsafe.Pointer(bp + 4)) = TSessionBuffer{} _sessionAppendStr(tls, bp+4, __ccgo_ts+34079, bp) _sessionAppendIdent(tls, bp+4, zTab, bp) _sessionAppendStr(tls, bp+4, __ccgo_ts+22019, bp) i = 0 for { if !(i < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol) { break } if i != 0 { _sessionAppendStr(tls, bp+4, __ccgo_ts+14990, bp) } _sessionAppendIdent(tls, bp+4, *(*uintptr)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(i)*4)), bp) goto _1 _1: ; i++ } _sessionAppendStr(tls, bp+4, __ccgo_ts+34097, bp) i = int32(1) for { if !(i < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol) { break } _sessionAppendStr(tls, bp+4, __ccgo_ts+34108, bp) goto _2 _2: ; i++ } _sessionAppendStr(tls, bp+4, __ccgo_ts+5106, bp) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_prepare_v2(tls, db, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf, p+8, uintptr(0)) } Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf) return *(*int32)(unsafe.Pointer(bp)) } func _sessionPrepare(tls *libc.TLS, db uintptr, pp uintptr, zSql uintptr) (r int32) { return Xsqlite3_prepare_v2(tls, db, zSql, -int32(1), pp, uintptr(0)) } // C documentation // // /* // ** Prepare statements for applying changes to the sqlite_stat1 table. // ** These are similar to those created by sessionSelectRow(), // ** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for // ** other tables. // */ func _sessionStat1Sql(tls *libc.TLS, db uintptr, p uintptr) (r int32) { var rc int32 _ = rc rc = _sessionSelectRow(tls, db, __ccgo_ts+11582, p) if rc == SQLITE_OK { rc = _sessionPrepare(tls, db, p+8, __ccgo_ts+34112) } if rc == SQLITE_OK { rc = _sessionPrepare(tls, db, p+4, __ccgo_ts+34225) } return rc } // C documentation // // /* // ** A wrapper around sqlite3_bind_value() that detects an extra problem. // ** See comments in the body of this function for details. // */ func _sessionBindValue(tls *libc.TLS, pStmt uintptr, i int32, pVal uintptr) (r int32) { var eType int32 _ = eType eType = Xsqlite3_value_type(tls, pVal) /* COVERAGE: The (pVal->z==0) branch is never true using current versions ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either ** the (pVal->z) variable remains as it was or the type of the value is ** set to SQLITE_NULL. */ if (eType == int32(SQLITE_TEXT) || eType == int32(SQLITE_BLOB)) && (*Tsqlite3_value)(unsafe.Pointer(pVal)).Fz == uintptr(0) { /* This condition occurs when an earlier OOM in a call to ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */ return int32(SQLITE_NOMEM) } return Xsqlite3_bind_value(tls, pStmt, i, pVal) } // C documentation // // /* // ** Iterator pIter must point to an SQLITE_INSERT entry. This function // ** transfers new.* values from the current iterator entry to statement // ** pStmt. The table being inserted into has nCol columns. // ** // ** New.* value $i from the iterator is bound to variable ($i+1) of // ** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1) // ** are transfered to the statement. Otherwise, if abPK is not NULL, it points // ** to an array nCol elements in size. In this case only those values for // ** which abPK[$i] is true are read from the iterator and bound to the // ** statement. // ** // ** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK. // */ func _sessionBindRow(tls *libc.TLS, pIter uintptr, xValue uintptr, nCol int32, abPK uintptr, pStmt uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, rc int32 var _ /* pVal at bp+0 */ uintptr _, _ = i, rc rc = SQLITE_OK /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the ** argument iterator points to a suitable entry. Make sure that xValue ** is one of these to guarantee that it is safe to ignore the return ** in the code below. */ _ = libc.Int32FromInt32(0) i = 0 for { if !(rc == SQLITE_OK && i < nCol) { break } if !(abPK != 0) || *(*Tu8)(unsafe.Pointer(abPK + uintptr(i))) != 0 { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xValue})))(tls, pIter, i, bp) if *(*uintptr)(unsafe.Pointer(bp)) == uintptr(0) { /* The value in the changeset was "undefined". This indicates a ** corrupt changeset blob. */ rc = _sqlite3CorruptError(tls, int32(227849)) } else { rc = _sessionBindValue(tls, pStmt, i+int32(1), *(*uintptr)(unsafe.Pointer(bp))) } } goto _1 _1: ; i++ } return rc } // C documentation // // /* // ** SQL statement pSelect is as generated by the sessionSelectRow() function. // ** This function binds the primary key values from the change that changeset // ** iterator pIter points to to the SELECT and attempts to seek to the table // ** entry. If a row is found, the SELECT statement left pointing at the row // ** and SQLITE_ROW is returned. Otherwise, if no row is found and no error // ** has occured, the statement is reset and SQLITE_OK is returned. If an // ** error occurs, the statement is reset and an SQLite error code is returned. // ** // ** If this function returns SQLITE_ROW, the caller must eventually reset() // ** statement pSelect. If any other value is returned, the statement does // ** not require a reset(). // ** // ** If the iterator currently points to an INSERT record, bind values from the // ** new.* record to the SELECT statement. Or, if it points to a DELETE or // ** UPDATE, bind values from the old.* record. // */ func _sessionSeekToRow(tls *libc.TLS, pIter uintptr, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var ii, rc int32 var pSelect, v1 uintptr var _ /* nCol at bp+0 */ int32 var _ /* op at bp+4 */ int32 var _ /* pVal at bp+12 */ uintptr var _ /* zDummy at bp+8 */ uintptr _, _, _, _ = ii, pSelect, rc, v1 pSelect = (*TSessionApplyCtx)(unsafe.Pointer(p)).FpSelect /* Unused */ Xsqlite3_clear_bindings(tls, pSelect) Xsqlite3changeset_op(tls, pIter, bp+8, bp, bp+4, uintptr(0)) if *(*int32)(unsafe.Pointer(bp + 4)) == int32(SQLITE_INSERT) { v1 = __ccgo_fp(Xsqlite3changeset_new) } else { v1 = __ccgo_fp(Xsqlite3changeset_old) } rc = _sessionBindRow(tls, pIter, v1, *(*int32)(unsafe.Pointer(bp)), (*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK, pSelect) if *(*int32)(unsafe.Pointer(bp + 4)) != int32(SQLITE_DELETE) && (*TSessionApplyCtx)(unsafe.Pointer(p)).FbIgnoreNoop != 0 { ii = 0 for { if !(rc == SQLITE_OK && ii < *(*int32)(unsafe.Pointer(bp))) { break } if int32(*(*Tu8)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK + uintptr(ii)))) == 0 { *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) Xsqlite3changeset_new(tls, pIter, ii, bp+12) Xsqlite3_bind_int(tls, pSelect, ii+int32(1)+*(*int32)(unsafe.Pointer(bp)), libc.BoolInt32(*(*uintptr)(unsafe.Pointer(bp + 12)) == uintptr(0))) if *(*uintptr)(unsafe.Pointer(bp + 12)) != 0 { rc = _sessionBindValue(tls, pSelect, ii+int32(1), *(*uintptr)(unsafe.Pointer(bp + 12))) } } goto _2 _2: ; ii++ } } if rc == SQLITE_OK { rc = Xsqlite3_step(tls, pSelect) if rc != int32(SQLITE_ROW) { rc = Xsqlite3_reset(tls, pSelect) } } return rc } // C documentation // // /* // ** This function is called from within sqlite3changeset_apply_v2() when // ** a conflict is encountered and resolved using conflict resolution // ** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE).. // ** It adds a conflict resolution record to the buffer in // ** SessionApplyCtx.rebase, which will eventually be returned to the caller // ** of apply_v2() as the "rebase" buffer. // ** // ** Return SQLITE_OK if successful, or an SQLite error code otherwise. // */ func _sessionRebaseAdd(tls *libc.TLS, p uintptr, eType int32, pIter uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var eOp, i, v1 int32 var zTab uintptr var _ /* pVal at bp+4 */ uintptr var _ /* rc at bp+0 */ int32 _, _, _, _ = eOp, i, zTab, v1 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK if (*TSessionApplyCtx)(unsafe.Pointer(p)).FbRebase != 0 { eOp = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fop if int32((*TSessionApplyCtx)(unsafe.Pointer(p)).FbRebaseStarted) == 0 { /* Append a table-header to the rebase buffer */ zTab = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab _sessionAppendByte(tls, p+60, uint8('T'), bp) _sessionAppendVarint(tls, p+60, (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol, bp) _sessionAppendBlob(tls, p+60, (*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK, (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol, bp) _sessionAppendBlob(tls, p+60, zTab, int32(libc.Xstrlen(tls, zTab))+int32(1), bp) (*TSessionApplyCtx)(unsafe.Pointer(p)).FbRebaseStarted = uint8(1) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if eOp == int32(SQLITE_DELETE) { v1 = int32(SQLITE_DELETE) } else { v1 = int32(SQLITE_INSERT) } _sessionAppendByte(tls, p+60, uint8(v1), bp) _sessionAppendByte(tls, p+60, libc.BoolUint8(eType == libc.Int32FromInt32(SQLITE_CHANGESET_REPLACE)), bp) i = 0 for { if !(i < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol) { break } *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) if eOp == int32(SQLITE_DELETE) || eOp == int32(SQLITE_UPDATE) && *(*Tu8)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK + uintptr(i))) != 0 { Xsqlite3changeset_old(tls, pIter, i, bp+4) } else { Xsqlite3changeset_new(tls, pIter, i, bp+4) } _sessionAppendValue(tls, p+60, *(*uintptr)(unsafe.Pointer(bp + 4)), bp) goto _2 _2: ; i++ } } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Invoke the conflict handler for the change that the changeset iterator // ** currently points to. // ** // ** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT. // ** If argument pbReplace is NULL, then the type of conflict handler invoked // ** depends solely on eType, as follows: // ** // ** eType value Value passed to xConflict // ** ------------------------------------------------- // ** CHANGESET_DATA CHANGESET_NOTFOUND // ** CHANGESET_CONFLICT CHANGESET_CONSTRAINT // ** // ** Or, if pbReplace is not NULL, then an attempt is made to find an existing // ** record with the same primary key as the record about to be deleted, updated // ** or inserted. If such a record can be found, it is available to the conflict // ** handler as the "conflicting" record. In this case the type of conflict // ** handler invoked is as follows: // ** // ** eType value PK Record found? Value passed to xConflict // ** ---------------------------------------------------------------- // ** CHANGESET_DATA Yes CHANGESET_DATA // ** CHANGESET_DATA No CHANGESET_NOTFOUND // ** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT // ** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT // ** // ** If pbReplace is not NULL, and a record with a matching PK is found, and // ** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace // ** is set to non-zero before returning SQLITE_OK. // ** // ** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is // ** returned. Or, if the conflict handler returns an invalid value, // ** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT, // ** this function returns SQLITE_OK. // */ func _sessionConflictHandler(tls *libc.TLS, eType int32, p uintptr, pIter uintptr, xConflict uintptr, pCtx uintptr, pbReplace uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var aBlob uintptr var nBlob, res int32 var _ /* nCol at bp+4 */ int32 var _ /* op at bp+8 */ int32 var _ /* rc at bp+0 */ int32 var _ /* zDummy at bp+12 */ uintptr _, _, _ = aBlob, nBlob, res res = 0 Xsqlite3changeset_op(tls, pIter, bp+12, bp+4, bp+8, uintptr(0)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Bind the new.* PRIMARY KEY values to the SELECT statement. */ if pbReplace != 0 { *(*int32)(unsafe.Pointer(bp)) = _sessionSeekToRow(tls, pIter, p) } else { *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK } if *(*int32)(unsafe.Pointer(bp)) == int32(SQLITE_ROW) { /* There exists another row with the new.* primary key. */ if (*TSessionApplyCtx)(unsafe.Pointer(p)).FbIgnoreNoop != 0 && Xsqlite3_column_int(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpSelect, Xsqlite3_column_count(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpSelect)-int32(1)) != 0 { res = SQLITE_CHANGESET_OMIT } else { (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FpConflict = (*TSessionApplyCtx)(unsafe.Pointer(p)).FpSelect res = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xConflict})))(tls, pCtx, eType, pIter) (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FpConflict = uintptr(0) } *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_reset(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpSelect) } else { if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { if (*TSessionApplyCtx)(unsafe.Pointer(p)).FbDeferConstraints != 0 && eType == int32(SQLITE_CHANGESET_CONFLICT) { /* Instead of invoking the conflict handler, append the change blob ** to the SessionApplyCtx.constraints buffer. */ aBlob = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FaData + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FiCurrent) nBlob = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FiNext - (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FiCurrent _sessionAppendBlob(tls, p+48, aBlob, nBlob, bp) return SQLITE_OK } else { /* No other row with the new.* primary key. */ res = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xConflict})))(tls, pCtx, eType+int32(1), pIter) if res == int32(SQLITE_CHANGESET_REPLACE) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_MISUSE) } } } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { switch res { case int32(SQLITE_CHANGESET_REPLACE): _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pbReplace)) = int32(1) case SQLITE_CHANGESET_OMIT: case int32(SQLITE_CHANGESET_ABORT): *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ABORT) default: *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_MISUSE) break } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sessionRebaseAdd(tls, p, res, pIter) } } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Attempt to apply the change that the iterator passed as the first argument // ** currently points to to the database. If a conflict is encountered, invoke // ** the conflict handler callback. // ** // ** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If // ** one is encountered, update or delete the row with the matching primary key // ** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs, // ** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry // ** to true before returning. In this case the caller will invoke this function // ** again, this time with pbRetry set to NULL. // ** // ** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is // ** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead. // ** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such // ** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true // ** before retrying. In this case the caller attempts to remove the conflicting // ** row before invoking this function again, this time with pbReplace set // ** to NULL. // ** // ** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function // ** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is // ** returned. // */ func _sessionApplyOneOp(tls *libc.TLS, pIter uintptr, p uintptr, xConflict uintptr, pCtx uintptr, pbReplace uintptr, pbRetry uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var abPK, pNew, pOld, v1 uintptr var bPatchset, i, rc int32 var _ /* nCol at bp+8 */ int32 var _ /* op at bp+4 */ int32 var _ /* pUp at bp+12 */ uintptr var _ /* zDummy at bp+0 */ uintptr _, _, _, _, _, _, _ = abPK, bPatchset, i, pNew, pOld, rc, v1 rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) Xsqlite3changeset_op(tls, pIter, bp, bp+8, bp+4, uintptr(0)) if *(*int32)(unsafe.Pointer(bp + 4)) == int32(SQLITE_DELETE) { if (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset != 0 { v1 = (*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK } else { v1 = uintptr(0) } /* Bind values to the DELETE statement. If conflict handling is required, ** bind values for all columns and set bound variable (nCol+1) to true. ** Or, if conflict handling is not required, bind just the PK column ** values and, if it exists, set (nCol+1) to false. Conflict handling ** is not required if: ** ** * this is a patchset, or ** * (pbRetry==0), or ** * all columns of the table are PK columns (in this case there is ** no (nCol+1) variable to bind to). */ abPK = v1 rc = _sessionBindRow(tls, pIter, __ccgo_fp(Xsqlite3changeset_old), *(*int32)(unsafe.Pointer(bp + 8)), abPK, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpDelete) if rc == SQLITE_OK && Xsqlite3_bind_parameter_count(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpDelete) > *(*int32)(unsafe.Pointer(bp + 8)) { rc = Xsqlite3_bind_int(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpDelete, *(*int32)(unsafe.Pointer(bp + 8))+int32(1), libc.BoolInt32(pbRetry == uintptr(0) || abPK != 0)) } if rc != SQLITE_OK { return rc } Xsqlite3_step(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpDelete) rc = Xsqlite3_reset(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpDelete) if rc == SQLITE_OK && Xsqlite3_changes(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).Fdb) == 0 && int32((*TSessionApplyCtx)(unsafe.Pointer(p)).FbIgnoreNoop) == 0 { rc = _sessionConflictHandler(tls, int32(SQLITE_CHANGESET_DATA), p, pIter, xConflict, pCtx, pbRetry) } else { if rc&int32(0xff) == int32(SQLITE_CONSTRAINT) { rc = _sessionConflictHandler(tls, int32(SQLITE_CHANGESET_CONFLICT), p, pIter, xConflict, pCtx, uintptr(0)) } } } else { if *(*int32)(unsafe.Pointer(bp + 4)) == int32(SQLITE_UPDATE) { *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) bPatchset = libc.BoolInt32(pbRetry == uintptr(0) || (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset != 0) rc = _sessionUpdateFind(tls, pIter, p, bPatchset, bp+12) /* Bind values to the UPDATE statement. */ i = 0 for { if !(rc == SQLITE_OK && i < *(*int32)(unsafe.Pointer(bp + 8))) { break } pOld = *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FapValue + uintptr(i)*4)) pNew = *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FapValue + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol+i)*4)) if *(*Tu8)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK + uintptr(i))) != 0 || bPatchset == 0 && pOld != 0 { rc = _sessionBindValue(tls, *(*uintptr)(unsafe.Pointer(bp + 12)), i*int32(2)+int32(2), pOld) } if rc == SQLITE_OK && pNew != 0 { rc = _sessionBindValue(tls, *(*uintptr)(unsafe.Pointer(bp + 12)), i*int32(2)+int32(1), pNew) } goto _2 _2: ; i++ } if rc != SQLITE_OK { return rc } /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict, ** the result will be SQLITE_OK with 0 rows modified. */ Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) rc = Xsqlite3_reset(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) if rc == SQLITE_OK && Xsqlite3_changes(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).Fdb) == 0 { /* A NOTFOUND or DATA error. Search the table to see if it contains ** a row with a matching primary key. If so, this is a DATA conflict. ** Otherwise, if there is no primary key match, it is a NOTFOUND. */ rc = _sessionConflictHandler(tls, int32(SQLITE_CHANGESET_DATA), p, pIter, xConflict, pCtx, pbRetry) } else { if rc&int32(0xff) == int32(SQLITE_CONSTRAINT) { /* This is always a CONSTRAINT conflict. */ rc = _sessionConflictHandler(tls, int32(SQLITE_CHANGESET_CONFLICT), p, pIter, xConflict, pCtx, uintptr(0)) } } } else { _ = libc.Int32FromInt32(0) if (*TSessionApplyCtx)(unsafe.Pointer(p)).FbStat1 != 0 { /* Check if there is a conflicting row. For sqlite_stat1, this needs ** to be done using a SELECT, as there is no PRIMARY KEY in the ** database schema to throw an exception if a duplicate is inserted. */ rc = _sessionSeekToRow(tls, pIter, p) if rc == int32(SQLITE_ROW) { rc = int32(SQLITE_CONSTRAINT) Xsqlite3_reset(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpSelect) } } if rc == SQLITE_OK { rc = _sessionBindRow(tls, pIter, __ccgo_fp(Xsqlite3changeset_new), *(*int32)(unsafe.Pointer(bp + 8)), uintptr(0), (*TSessionApplyCtx)(unsafe.Pointer(p)).FpInsert) if rc != SQLITE_OK { return rc } Xsqlite3_step(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpInsert) rc = Xsqlite3_reset(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpInsert) } if rc&int32(0xff) == int32(SQLITE_CONSTRAINT) { rc = _sessionConflictHandler(tls, int32(SQLITE_CHANGESET_CONFLICT), p, pIter, xConflict, pCtx, pbReplace) } } } return rc } // C documentation // // /* // ** Attempt to apply the change that the iterator passed as the first argument // ** currently points to to the database. If a conflict is encountered, invoke // ** the conflict handler callback. // ** // ** The difference between this function and sessionApplyOne() is that this // ** function handles the case where the conflict-handler is invoked and // ** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be // ** retried in some manner. // */ func _sessionApplyOneWithRetry(tls *libc.TLS, db uintptr, pIter uintptr, pApply uintptr, xConflict uintptr, pCtx uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* bReplace at bp+0 */ int32 var _ /* bRetry at bp+4 */ int32 _ = rc *(*int32)(unsafe.Pointer(bp)) = 0 *(*int32)(unsafe.Pointer(bp + 4)) = 0 rc = _sessionApplyOneOp(tls, pIter, pApply, xConflict, pCtx, bp, bp+4) if rc == SQLITE_OK { /* If the bRetry flag is set, the change has not been applied due to an ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and ** a row with the correct PK is present in the db, but one or more other ** fields do not contain the expected values) and the conflict handler ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation, ** but pass NULL as the final argument so that sessionApplyOneOp() ignores ** the SQLITE_CHANGESET_DATA problem. */ if *(*int32)(unsafe.Pointer(bp + 4)) != 0 { _ = libc.Int32FromInt32(0) rc = _sessionApplyOneOp(tls, pIter, pApply, xConflict, pCtx, uintptr(0), uintptr(0)) } else { if *(*int32)(unsafe.Pointer(bp)) != 0 { _ = libc.Int32FromInt32(0) rc = Xsqlite3_exec(tls, db, __ccgo_ts+34369, uintptr(0), uintptr(0), uintptr(0)) if rc == SQLITE_OK { rc = _sessionBindRow(tls, pIter, __ccgo_fp(Xsqlite3changeset_new), (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FnCol, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FabPK, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FpDelete) Xsqlite3_bind_int(tls, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FpDelete, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FnCol+int32(1), int32(1)) } if rc == SQLITE_OK { Xsqlite3_step(tls, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FpDelete) rc = Xsqlite3_reset(tls, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FpDelete) } if rc == SQLITE_OK { rc = _sessionApplyOneOp(tls, pIter, pApply, xConflict, pCtx, uintptr(0), uintptr(0)) } if rc == SQLITE_OK { rc = Xsqlite3_exec(tls, db, __ccgo_ts+34390, uintptr(0), uintptr(0), uintptr(0)) } } } } return rc } // C documentation // // /* // ** Retry the changes accumulated in the pApply->constraints buffer. // */ func _sessionRetryConstraints(tls *libc.TLS, db uintptr, bPatchset int32, zTab uintptr, pApply uintptr, xConflict uintptr, pCtx uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var cons TSessionBuffer var nByte Tsize_t var rc2 int32 var _ /* pIter2 at bp+4 */ uintptr var _ /* rc at bp+0 */ int32 _, _, _ = cons, nByte, rc2 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK for (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints.FnBuf != 0 { *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) cons = (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints libc.Xmemset(tls, pApply+48, 0, uint32(12)) *(*int32)(unsafe.Pointer(bp)) = _sessionChangesetStart(tls, bp+4, uintptr(0), uintptr(0), cons.FnBuf, cons.FaBuf, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FbInvertConstraints, int32(1)) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { nByte = uint32(int32(2)*(*TSessionApplyCtx)(unsafe.Pointer(pApply)).FnCol) * uint32(4) (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FbPatchset = bPatchset (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FzTab = zTab (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FnCol = (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FnCol (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FabPK = (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FabPK _sessionBufferGrow(tls, *(*uintptr)(unsafe.Pointer(bp + 4))+44, int64(nByte), bp) (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FapValue = (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).Ftblhdr.FaBuf if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { libc.Xmemset(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FapValue, 0, nByte) } for *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3changeset_next(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) { *(*int32)(unsafe.Pointer(bp)) = _sessionApplyOneWithRetry(tls, db, *(*uintptr)(unsafe.Pointer(bp + 4)), pApply, xConflict, pCtx) } rc2 = Xsqlite3changeset_finalize(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = rc2 } } _ = libc.Int32FromInt32(0) Xsqlite3_free(tls, cons.FaBuf) if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { break } if (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints.FnBuf >= cons.FnBuf { /* No progress was made on the last round. */ (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FbDeferConstraints = 0 } } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Argument pIter is a changeset iterator that has been initialized, but // ** not yet passed to sqlite3changeset_next(). This function applies the // ** changeset to the main database attached to handle "db". The supplied // ** conflict handler callback is invoked to resolve any conflicts encountered // ** while applying the change. // */ func _sessionChangesetApply(tls *libc.TLS, db uintptr, pIter uintptr, xFilter uintptr, xConflict uintptr, pCtx uintptr, ppRebase uintptr, pnRebase uintptr, flags int32) (r int32) { bp := tls.Alloc(240) defer tls.Free(240) var bPatchset, i, nMinCol, nTab, rc, res, schemaMismatch, v2, v3, v4, v6 int32 var v5, v7 bool var _ /* abPK at bp+96 */ uintptr var _ /* nCol at bp+84 */ int32 var _ /* nFk at bp+100 */ int32 var _ /* notUsed at bp+104 */ int32 var _ /* op at bp+88 */ int32 var _ /* sApply at bp+4 */ TSessionApplyCtx var _ /* sIter at bp+108 */ Tsqlite3_changeset_iter var _ /* zNew at bp+92 */ uintptr var _ /* zTab at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _ = bPatchset, i, nMinCol, nTab, rc, res, schemaMismatch, v2, v3, v4, v5, v6, v7 schemaMismatch = 0 rc = SQLITE_OK /* Return code */ *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Name of current table */ nTab = 0 _ = libc.Int32FromInt32(0) (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FbNoDiscard = int32(1) libc.Xmemset(tls, bp+4, 0, uint32(80)) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FbRebase = libc.BoolUint8(ppRebase != 0 && pnRebase != 0) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FbInvertConstraints = libc.BoolInt32(!!(flags&libc.Int32FromInt32(SQLITE_CHANGESETAPPLY_INVERT) != 0)) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FbIgnoreNoop = libc.BoolUint8(!!(flags&libc.Int32FromInt32(SQLITE_CHANGESETAPPLY_IGNORENOOP) != 0)) Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, db)) if flags&int32(SQLITE_CHANGESETAPPLY_NOSAVEPOINT) == 0 { rc = Xsqlite3_exec(tls, db, __ccgo_ts+34409, uintptr(0), uintptr(0), uintptr(0)) } if rc == SQLITE_OK { rc = Xsqlite3_exec(tls, db, __ccgo_ts+34435, uintptr(0), uintptr(0), uintptr(0)) } for rc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3changeset_next(tls, pIter) { Xsqlite3changeset_op(tls, pIter, bp+92, bp+84, bp+88, uintptr(0)) if *(*uintptr)(unsafe.Pointer(bp)) == uintptr(0) || Xsqlite3_strnicmp(tls, *(*uintptr)(unsafe.Pointer(bp + 92)), *(*uintptr)(unsafe.Pointer(bp)), nTab+int32(1)) != 0 { rc = _sessionRetryConstraints(tls, db, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset, *(*uintptr)(unsafe.Pointer(bp)), bp+4, xConflict, pCtx) if rc != SQLITE_OK { break } _sessionUpdateFree(tls, bp+4) Xsqlite3_free(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FazCol) /* cast works around VC++ bug */ Xsqlite3_finalize(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FpDelete) Xsqlite3_finalize(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FpInsert) Xsqlite3_finalize(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FpSelect) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).Fdb = db (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FpDelete = uintptr(0) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FpInsert = uintptr(0) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FpSelect = uintptr(0) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FnCol = 0 (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FazCol = uintptr(0) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FabPK = uintptr(0) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FbStat1 = 0 (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FbDeferConstraints = int32(1) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FbRebaseStarted = uint8(0) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FbRowid = 0 libc.Xmemset(tls, bp+4+48, 0, uint32(12)) /* If an xFilter() callback was specified, invoke it now. If the ** xFilter callback returns zero, skip this table. If it returns ** non-zero, proceed. */ schemaMismatch = libc.BoolInt32(xFilter != 0 && 0 == (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xFilter})))(tls, pCtx, *(*uintptr)(unsafe.Pointer(bp + 92)))) if schemaMismatch != 0 { *(*uintptr)(unsafe.Pointer(bp)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+216, *(*uintptr)(unsafe.Pointer(bp + 92)))) if *(*uintptr)(unsafe.Pointer(bp)) == uintptr(0) { rc = int32(SQLITE_NOMEM) break } nTab = int32(libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(bp)))) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FazCol = *(*uintptr)(unsafe.Pointer(bp)) } else { nMinCol = 0 Xsqlite3changeset_pk(tls, pIter, bp+96, uintptr(0)) rc = _sessionTableInfo(tls, uintptr(0), db, __ccgo_ts+6585, *(*uintptr)(unsafe.Pointer(bp + 92)), bp+4+16, bp, bp+4+20, uintptr(0), bp+4+24, bp+4+76) if rc != SQLITE_OK { break } i = 0 for { if !(i < (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FnCol) { break } if *(*Tu8)(unsafe.Pointer((*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FabPK + uintptr(i))) != 0 { nMinCol = i + int32(1) } goto _1 _1: ; i++ } if (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FnCol == 0 { schemaMismatch = int32(1) Xsqlite3_log(tls, int32(SQLITE_SCHEMA), __ccgo_ts+34465, libc.VaList(bp+216, *(*uintptr)(unsafe.Pointer(bp)))) } else { if (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FnCol < *(*int32)(unsafe.Pointer(bp + 84)) { schemaMismatch = int32(1) Xsqlite3_log(tls, int32(SQLITE_SCHEMA), __ccgo_ts+34509, libc.VaList(bp+216, *(*uintptr)(unsafe.Pointer(bp)), (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FnCol, *(*int32)(unsafe.Pointer(bp + 84)))) } else { if *(*int32)(unsafe.Pointer(bp + 84)) < nMinCol || libc.Xmemcmp(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FabPK, *(*uintptr)(unsafe.Pointer(bp + 96)), uint32(*(*int32)(unsafe.Pointer(bp + 84)))) != 0 { schemaMismatch = int32(1) Xsqlite3_log(tls, int32(SQLITE_SCHEMA), __ccgo_ts+34580, libc.VaList(bp+216, *(*uintptr)(unsafe.Pointer(bp)))) } else { (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FnCol = *(*int32)(unsafe.Pointer(bp + 84)) if 0 == Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer(bp)), __ccgo_ts+11582) { v2 = _sessionStat1Sql(tls, db, bp+4) rc = v2 if v2 != 0 { break } (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FbStat1 = int32(1) } else { v3 = _sessionSelectRow(tls, db, *(*uintptr)(unsafe.Pointer(bp)), bp+4) rc = v3 if v5 = v3 != 0; !v5 { v4 = _sessionDeleteRow(tls, db, *(*uintptr)(unsafe.Pointer(bp)), bp+4) rc = v4 } if v7 = v5 || v4 != 0; !v7 { v6 = _sessionInsertRow(tls, db, *(*uintptr)(unsafe.Pointer(bp)), bp+4) rc = v6 } if v7 || v6 != 0 { break } (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FbStat1 = 0 } } } } nTab = _sqlite3Strlen30(tls, *(*uintptr)(unsafe.Pointer(bp))) } } /* If there is a schema mismatch on the current table, proceed to the ** next change. A log message has already been issued. */ if schemaMismatch != 0 { continue } rc = _sessionApplyOneWithRetry(tls, db, pIter, bp+4, xConflict, pCtx) } bPatchset = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset if rc == SQLITE_OK { rc = Xsqlite3changeset_finalize(tls, pIter) } else { Xsqlite3changeset_finalize(tls, pIter) } if rc == SQLITE_OK { rc = _sessionRetryConstraints(tls, db, bPatchset, *(*uintptr)(unsafe.Pointer(bp)), bp+4, xConflict, pCtx) } if rc == SQLITE_OK { Xsqlite3_db_status(tls, db, int32(SQLITE_DBSTATUS_DEFERRED_FKS), bp+100, bp+104, 0) if *(*int32)(unsafe.Pointer(bp + 100)) != 0 { res = int32(SQLITE_CHANGESET_ABORT) libc.Xmemset(tls, bp+108, 0, uint32(100)) (*(*Tsqlite3_changeset_iter)(unsafe.Pointer(bp + 108))).FnCol = *(*int32)(unsafe.Pointer(bp + 100)) res = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xConflict})))(tls, pCtx, int32(SQLITE_CHANGESET_FOREIGN_KEY), bp+108) if res != SQLITE_CHANGESET_OMIT { rc = int32(SQLITE_CONSTRAINT) } } } Xsqlite3_exec(tls, db, __ccgo_ts+34640, uintptr(0), uintptr(0), uintptr(0)) if flags&int32(SQLITE_CHANGESETAPPLY_NOSAVEPOINT) == 0 { if rc == SQLITE_OK { rc = Xsqlite3_exec(tls, db, __ccgo_ts+34670, uintptr(0), uintptr(0), uintptr(0)) } else { Xsqlite3_exec(tls, db, __ccgo_ts+34694, uintptr(0), uintptr(0), uintptr(0)) Xsqlite3_exec(tls, db, __ccgo_ts+34670, uintptr(0), uintptr(0), uintptr(0)) } } _ = libc.Int32FromInt32(0) if rc == SQLITE_OK && bPatchset == 0 && (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FbRebase != 0 { *(*uintptr)(unsafe.Pointer(ppRebase)) = (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).Frebase.FaBuf *(*int32)(unsafe.Pointer(pnRebase)) = (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).Frebase.FnBuf (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).Frebase.FaBuf = uintptr(0) } _sessionUpdateFree(tls, bp+4) Xsqlite3_finalize(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FpInsert) Xsqlite3_finalize(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FpDelete) Xsqlite3_finalize(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FpSelect) Xsqlite3_free(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).FazCol) /* cast works around VC++ bug */ Xsqlite3_free(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).Fconstraints.FaBuf) Xsqlite3_free(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 4))).Frebase.FaBuf) Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, db)) return rc } // C documentation // // /* // ** Apply the changeset passed via pChangeset/nChangeset to the main // ** database attached to handle "db". // */ func Xsqlite3changeset_apply_v2(tls *libc.TLS, db uintptr, nChangeset int32, pChangeset uintptr, xFilter uintptr, xConflict uintptr, pCtx uintptr, ppRebase uintptr, pnRebase uintptr, flags int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bInv, rc int32 var savedFlag Tu64 var _ /* pIter at bp+0 */ uintptr _, _, _ = bInv, rc, savedFlag /* Iterator to skip through changeset */ bInv = libc.BoolInt32(!!(flags&libc.Int32FromInt32(SQLITE_CHANGESETAPPLY_INVERT) != 0)) rc = _sessionChangesetStart(tls, bp, uintptr(0), uintptr(0), nChangeset, pChangeset, bInv, int32(1)) savedFlag = (*Tsqlite3)(unsafe.Pointer(db)).Fflags & (uint64(libc.Int32FromInt32(0x00008)) << libc.Int32FromInt32(32)) if flags&int32(SQLITE_CHANGESETAPPLY_FKNOACTION) != 0 { *(*Tu64)(unsafe.Pointer(db + 32)) |= uint64(libc.Int32FromInt32(0x00008)) << libc.Int32FromInt32(32) *(*int32)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema)) -= int32(32) } if rc == SQLITE_OK { rc = _sessionChangesetApply(tls, db, *(*uintptr)(unsafe.Pointer(bp)), xFilter, xConflict, pCtx, ppRebase, pnRebase, flags) } if flags&int32(SQLITE_CHANGESETAPPLY_FKNOACTION) != 0 && savedFlag == uint64(0) { _ = libc.Int32FromInt32(0) *(*Tu64)(unsafe.Pointer(db + 32)) &= ^(uint64(libc.Int32FromInt32(0x00008)) << libc.Int32FromInt32(32)) *(*int32)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema)) -= int32(32) } return rc } // C documentation // // /* // ** Apply the changeset passed via pChangeset/nChangeset to the main database // ** attached to handle "db". Invoke the supplied conflict handler callback // ** to resolve any conflicts encountered while applying the change. // */ func Xsqlite3changeset_apply(tls *libc.TLS, db uintptr, nChangeset int32, pChangeset uintptr, xFilter uintptr, xConflict uintptr, pCtx uintptr) (r int32) { return Xsqlite3changeset_apply_v2(tls, db, nChangeset, pChangeset, xFilter, xConflict, pCtx, uintptr(0), uintptr(0), 0) } // C documentation // // /* // ** Apply the changeset passed via xInput/pIn to the main database // ** attached to handle "db". Invoke the supplied conflict handler callback // ** to resolve any conflicts encountered while applying the change. // */ func Xsqlite3changeset_apply_v2_strm(tls *libc.TLS, db uintptr, xInput uintptr, pIn uintptr, xFilter uintptr, xConflict uintptr, pCtx uintptr, ppRebase uintptr, pnRebase uintptr, flags int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bInverse, rc int32 var _ /* pIter at bp+0 */ uintptr _, _ = bInverse, rc /* Iterator to skip through changeset */ bInverse = libc.BoolInt32(!!(flags&libc.Int32FromInt32(SQLITE_CHANGESETAPPLY_INVERT) != 0)) rc = _sessionChangesetStart(tls, bp, xInput, pIn, 0, uintptr(0), bInverse, int32(1)) if rc == SQLITE_OK { rc = _sessionChangesetApply(tls, db, *(*uintptr)(unsafe.Pointer(bp)), xFilter, xConflict, pCtx, ppRebase, pnRebase, flags) } return rc } func Xsqlite3changeset_apply_strm(tls *libc.TLS, db uintptr, xInput uintptr, pIn uintptr, xFilter uintptr, xConflict uintptr, pCtx uintptr) (r int32) { return Xsqlite3changeset_apply_v2_strm(tls, db, xInput, pIn, xFilter, xConflict, pCtx, uintptr(0), uintptr(0), 0) } /* ** sqlite3_changegroup handle. */ type Tsqlite3_changegroup1 = struct { Frc int32 FbPatch int32 FpList uintptr Fdb uintptr FzDb uintptr } type sqlite3_changegroup1 = Tsqlite3_changegroup1 // C documentation // // /* // ** This function is called to merge two changes to the same row together as // ** part of an sqlite3changeset_concat() operation. A new change object is // ** allocated and a pointer to it stored in *ppNew. // */ func _sessionChangeMerge(tls *libc.TLS, pTab uintptr, bRebase int32, bPatchset int32, pExist uintptr, op2 int32, bIndirect int32, aRec uintptr, nRec int32, ppNew uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var a1, a2, aExist, pIn, pNew, pOut, pOut1, v2, v3, v4, v6, v7 uintptr var i, i1, n1, n2, nIn, op1, rc int32 var nByte, nByte1 Tsqlite3_int64 var _ /* a1 at bp+4 */ uintptr var _ /* a1 at bp+8 */ uintptr var _ /* a2 at bp+12 */ uintptr var _ /* aCsr at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = a1, a2, aExist, i, i1, n1, n2, nByte, nByte1, nIn, op1, pIn, pNew, pOut, pOut1, rc, v2, v3, v4, v6, v7 pNew = uintptr(0) rc = SQLITE_OK _ = libc.Int32FromInt32(0) if !(pExist != 0) { pNew = Xsqlite3_malloc64(tls, uint64(uint32(20)+uint32(nRec))) if !(pNew != 0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, pNew, 0, uint32(20)) (*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(op2) (*TSessionChange)(unsafe.Pointer(pNew)).FbIndirect = uint8(bIndirect) (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord = pNew + 1*20 if bIndirect == 0 || bRebase == 0 { (*TSessionChange)(unsafe.Pointer(pNew)).FnRecord = nRec libc.Xmemcpy(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord, aRec, uint32(nRec)) } else { pIn = aRec pOut = (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } nIn = _sessionSerialLen(tls, pIn) if int32(*(*Tu8)(unsafe.Pointer(pIn))) == 0 { v2 = pOut pOut++ *(*Tu8)(unsafe.Pointer(v2)) = uint8(0) } else { if int32(*(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i)))) == 0 { v3 = pOut pOut++ *(*Tu8)(unsafe.Pointer(v3)) = uint8(0xFF) } else { libc.Xmemcpy(tls, pOut, pIn, uint32(nIn)) pOut += uintptr(nIn) } } pIn += uintptr(nIn) goto _1 _1: ; i++ } (*TSessionChange)(unsafe.Pointer(pNew)).FnRecord = int32(pOut) - int32((*TSessionChange)(unsafe.Pointer(pNew)).FaRecord) } } else { if bRebase != 0 { if int32((*TSessionChange)(unsafe.Pointer(pExist)).Fop) == int32(SQLITE_DELETE) && (*TSessionChange)(unsafe.Pointer(pExist)).FbIndirect != 0 { *(*uintptr)(unsafe.Pointer(ppNew)) = pExist } else { nByte = int64(uint32(nRec+(*TSessionChange)(unsafe.Pointer(pExist)).FnRecord) + uint32(20)) pNew = Xsqlite3_malloc64(tls, uint64(nByte)) if pNew == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { a1 = (*TSessionChange)(unsafe.Pointer(pExist)).FaRecord a2 = aRec libc.Xmemset(tls, pNew, 0, uint32(nByte)) (*TSessionChange)(unsafe.Pointer(pNew)).FbIndirect = libc.BoolUint8(bIndirect != 0 || (*TSessionChange)(unsafe.Pointer(pExist)).FbIndirect != 0) (*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(op2) v4 = pNew + 1*20 (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord = v4 pOut1 = v4 i1 = 0 for { if !(i1 < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } n1 = _sessionSerialLen(tls, a1) n2 = _sessionSerialLen(tls, a2) if int32(*(*Tu8)(unsafe.Pointer(a1))) == int32(0xFF) || int32(*(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i1)))) == 0 && bIndirect != 0 { v6 = pOut1 pOut1++ *(*Tu8)(unsafe.Pointer(v6)) = uint8(0xFF) } else { if int32(*(*Tu8)(unsafe.Pointer(a2))) == 0 { libc.Xmemcpy(tls, pOut1, a1, uint32(n1)) pOut1 += uintptr(n1) } else { libc.Xmemcpy(tls, pOut1, a2, uint32(n2)) pOut1 += uintptr(n2) } } a1 += uintptr(n1) a2 += uintptr(n2) goto _5 _5: ; i1++ } (*TSessionChange)(unsafe.Pointer(pNew)).FnRecord = int32(pOut1) - int32((*TSessionChange)(unsafe.Pointer(pNew)).FaRecord) } Xsqlite3_free(tls, pExist) } } else { op1 = int32((*TSessionChange)(unsafe.Pointer(pExist)).Fop) /* ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2. ** op1=INSERT, op2=UPDATE -> INSERT. ** op1=INSERT, op2=DELETE -> (none) ** ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2. ** op1=UPDATE, op2=UPDATE -> UPDATE. ** op1=UPDATE, op2=DELETE -> DELETE. ** ** op1=DELETE, op2=INSERT -> UPDATE. ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2. ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2. */ if op1 == int32(SQLITE_INSERT) && op2 == int32(SQLITE_INSERT) || op1 == int32(SQLITE_UPDATE) && op2 == int32(SQLITE_INSERT) || op1 == int32(SQLITE_DELETE) && op2 == int32(SQLITE_UPDATE) || op1 == int32(SQLITE_DELETE) && op2 == int32(SQLITE_DELETE) { pNew = pExist } else { if op1 == int32(SQLITE_INSERT) && op2 == int32(SQLITE_DELETE) { Xsqlite3_free(tls, pExist) _ = libc.Int32FromInt32(0) } else { aExist = (*TSessionChange)(unsafe.Pointer(pExist)).FaRecord /* Allocate a new SessionChange object. Ensure that the aRecord[] ** buffer of the new object is large enough to hold any record that ** may be generated by combining the input records. */ nByte1 = int64(uint32(20) + uint32((*TSessionChange)(unsafe.Pointer(pExist)).FnRecord) + uint32(nRec)) pNew = Xsqlite3_malloc64(tls, uint64(nByte1)) if !(pNew != 0) { Xsqlite3_free(tls, pExist) return int32(SQLITE_NOMEM) } libc.Xmemset(tls, pNew, 0, uint32(20)) (*TSessionChange)(unsafe.Pointer(pNew)).FbIndirect = libc.BoolUint8(bIndirect != 0 && (*TSessionChange)(unsafe.Pointer(pExist)).FbIndirect != 0) v7 = pNew + 1*20 (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord = v7 *(*uintptr)(unsafe.Pointer(bp)) = v7 if op1 == int32(SQLITE_INSERT) { /* INSERT + UPDATE */ *(*uintptr)(unsafe.Pointer(bp + 4)) = aRec _ = libc.Int32FromInt32(0) (*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(SQLITE_INSERT) if bPatchset == 0 { _sessionSkipRecord(tls, bp+4, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) } _sessionMergeRecord(tls, bp, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, aExist, *(*uintptr)(unsafe.Pointer(bp + 4))) } else { if op1 == int32(SQLITE_DELETE) { /* DELETE + INSERT */ _ = libc.Int32FromInt32(0) (*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(SQLITE_UPDATE) if bPatchset != 0 { libc.Xmemcpy(tls, *(*uintptr)(unsafe.Pointer(bp)), aRec, uint32(nRec)) *(*uintptr)(unsafe.Pointer(bp)) += uintptr(nRec) } else { if 0 == _sessionMergeUpdate(tls, bp, pTab, bPatchset, aExist, uintptr(0), aRec, uintptr(0)) { Xsqlite3_free(tls, pNew) pNew = uintptr(0) } } } else { if op2 == int32(SQLITE_UPDATE) { /* UPDATE + UPDATE */ *(*uintptr)(unsafe.Pointer(bp + 8)) = aExist *(*uintptr)(unsafe.Pointer(bp + 12)) = aRec _ = libc.Int32FromInt32(0) if bPatchset == 0 { _sessionSkipRecord(tls, bp+8, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) _sessionSkipRecord(tls, bp+12, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) } (*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(SQLITE_UPDATE) if 0 == _sessionMergeUpdate(tls, bp, pTab, bPatchset, aRec, aExist, *(*uintptr)(unsafe.Pointer(bp + 8)), *(*uintptr)(unsafe.Pointer(bp + 12))) { Xsqlite3_free(tls, pNew) pNew = uintptr(0) } } else { /* UPDATE + DELETE */ _ = libc.Int32FromInt32(0) (*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(SQLITE_DELETE) if bPatchset != 0 { libc.Xmemcpy(tls, *(*uintptr)(unsafe.Pointer(bp)), aRec, uint32(nRec)) *(*uintptr)(unsafe.Pointer(bp)) += uintptr(nRec) } else { _sessionMergeRecord(tls, bp, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, aRec, aExist) } } } } if pNew != 0 { (*TSessionChange)(unsafe.Pointer(pNew)).FnRecord = int32(*(*uintptr)(unsafe.Pointer(bp))) - int32((*TSessionChange)(unsafe.Pointer(pNew)).FaRecord) } Xsqlite3_free(tls, pExist) } } } } *(*uintptr)(unsafe.Pointer(ppNew)) = pNew return rc } // C documentation // // /* // ** Check if a changeset entry with nCol columns and the PK array passed // ** as the final argument to this function is compatible with SessionTable // ** pTab. If so, return 1. Otherwise, if they are incompatible in some way, // ** return 0. // */ func _sessionChangesetCheckCompat(tls *libc.TLS, pTab uintptr, nCol int32, abPK uintptr) (r int32) { var bPK Tu8 var ii, v2 int32 _, _, _ = bPK, ii, v2 if (*TSessionTable)(unsafe.Pointer(pTab)).FazCol != 0 && nCol < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol { ii = 0 for { if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } if ii < nCol { v2 = int32(*(*Tu8)(unsafe.Pointer(abPK + uintptr(ii)))) } else { v2 = 0 } bPK = uint8(v2) if int32(*(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(ii)))) != int32(bPK) { return 0 } goto _1 _1: ; ii++ } return int32(1) } return libc.BoolInt32((*TSessionTable)(unsafe.Pointer(pTab)).FnCol == nCol && 0 == libc.Xmemcmp(tls, abPK, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK, uint32(nCol))) } func _sessionChangesetExtendRecord(tls *libc.TLS, pGrp uintptr, pTab uintptr, nCol int32, op int32, aRec uintptr, nRec int32, pOut uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var eType, iOff, ii, n int32 var z, z1 uintptr var _ /* iVal at bp+8 */ Ti64 var _ /* rVal at bp+16 */ float64 var _ /* rc at bp+0 */ int32 _, _, _, _, _, _ = eType, iOff, ii, n, z, z1 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK ii = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TSessionBuffer)(unsafe.Pointer(pOut)).FnBuf = 0 if op == int32(SQLITE_INSERT) || op == int32(SQLITE_DELETE) && (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch == 0 { /* Append the missing default column values to the record. */ _sessionAppendBlob(tls, pOut, aRec, nRec, bp) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = _sessionPrepareDfltStmt(tls, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fdb, pTab, pTab+44) } ii = nCol for { if !(*(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } eType = Xsqlite3_column_type(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt, ii) _sessionAppendByte(tls, pOut, uint8(eType), bp) switch eType { case int32(SQLITE_FLOAT): fallthrough case int32(SQLITE_INTEGER): if eType == int32(SQLITE_INTEGER) { *(*Ti64)(unsafe.Pointer(bp + 8)) = Xsqlite3_column_int64(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt, ii) } else { *(*float64)(unsafe.Pointer(bp + 16)) = float64(Xsqlite3_column_int64(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt, ii)) libc.Xmemcpy(tls, bp+8, bp+16, uint32(8)) } if SQLITE_OK == _sessionBufferGrow(tls, pOut, int64(8), bp) { _sessionPutI64(tls, (*TSessionBuffer)(unsafe.Pointer(pOut)).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(pOut)).FnBuf), *(*Ti64)(unsafe.Pointer(bp + 8))) } case int32(SQLITE_BLOB): fallthrough case int32(SQLITE_TEXT): n = Xsqlite3_column_bytes(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt, ii) _sessionAppendVarint(tls, pOut, n, bp) if eType == int32(SQLITE_TEXT) { z = Xsqlite3_column_text(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt, ii) _sessionAppendBlob(tls, pOut, z, n, bp) } else { z1 = Xsqlite3_column_blob(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt, ii) _sessionAppendBlob(tls, pOut, z1, n, bp) } default: _ = libc.Int32FromInt32(0) break } goto _1 _1: ; ii++ } } else { if op == int32(SQLITE_UPDATE) { /* Append missing "undefined" entries to the old.* record. And, if this ** is an UPDATE, to the new.* record as well. */ iOff = 0 if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch == 0 { ii = 0 for { if !(ii < nCol) { break } iOff += _sessionSerialLen(tls, aRec+uintptr(iOff)) goto _2 _2: ; ii++ } _sessionAppendBlob(tls, pOut, aRec, iOff, bp) ii = 0 for { if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol-nCol) { break } _sessionAppendByte(tls, pOut, uint8(0x00), bp) goto _3 _3: ; ii++ } } _sessionAppendBlob(tls, pOut, aRec+uintptr(iOff), nRec-iOff, bp) ii = 0 for { if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol-nCol) { break } _sessionAppendByte(tls, pOut, uint8(0x00), bp) goto _4 _4: ; ii++ } } else { _ = libc.Int32FromInt32(0) _sessionAppendBlob(tls, pOut, aRec, nRec, bp) } } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Add all changes in the changeset traversed by the iterator passed as // ** the first argument to the changegroup hash tables. // */ func _sessionChangesetToHash(tls *libc.TLS, pIter uintptr, pGrp uintptr, bRebase int32) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var bPkOnly1, bPkOnly2, iHash, nNew, rc int32 var pExist, pTab, pp, ppTab uintptr var _ /* aRec at bp+0 */ uintptr var _ /* abPK at bp+40 */ uintptr var _ /* bIndirect at bp+32 */ int32 var _ /* nCol at bp+24 */ int32 var _ /* nRec at bp+4 */ int32 var _ /* op at bp+28 */ int32 var _ /* pChange at bp+36 */ uintptr var _ /* rec at bp+8 */ TSessionBuffer var _ /* zNew at bp+20 */ uintptr _, _, _, _, _, _, _, _, _ = bPkOnly1, bPkOnly2, iHash, nNew, pExist, pTab, pp, ppTab, rc rc = SQLITE_OK pTab = uintptr(0) *(*TSessionBuffer)(unsafe.Pointer(bp + 8)) = TSessionBuffer{} for int32(SQLITE_ROW) == _sessionChangesetNext(tls, pIter, bp, bp+4, uintptr(0)) { pExist = uintptr(0) /* Ensure that only changesets, or only patchsets, but not a mixture ** of both, are being combined. It is an error to try to combine a ** changeset and a patchset. */ if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FpList == uintptr(0) { (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset } else { if (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset != (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch { rc = int32(SQLITE_ERROR) break } } Xsqlite3changeset_op(tls, pIter, bp+20, bp+24, bp+28, bp+32) if !(pTab != 0) || Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer(bp + 20)), (*TSessionTable)(unsafe.Pointer(pTab)).FzName) != 0 { /* Search the list for a matching table */ nNew = int32(libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(bp + 20)))) Xsqlite3changeset_pk(tls, pIter, bp+40, uintptr(0)) pTab = (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FpList for { if !(pTab != 0) { break } if 0 == Xsqlite3_strnicmp(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, *(*uintptr)(unsafe.Pointer(bp + 20)), nNew+int32(1)) { break } goto _1 _1: ; pTab = (*TSessionTable)(unsafe.Pointer(pTab)).FpNext } if !(pTab != 0) { pTab = Xsqlite3_malloc64(tls, uint64(uint32(48)+uint32(*(*int32)(unsafe.Pointer(bp + 24)))+uint32(nNew)+uint32(1))) if !(pTab != 0) { rc = int32(SQLITE_NOMEM) break } libc.Xmemset(tls, pTab, 0, uint32(48)) (*TSessionTable)(unsafe.Pointer(pTab)).FnCol = *(*int32)(unsafe.Pointer(bp + 24)) (*TSessionTable)(unsafe.Pointer(pTab)).FabPK = pTab + 1*48 libc.Xmemcpy(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK, *(*uintptr)(unsafe.Pointer(bp + 40)), uint32(*(*int32)(unsafe.Pointer(bp + 24)))) (*TSessionTable)(unsafe.Pointer(pTab)).FzName = (*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(*(*int32)(unsafe.Pointer(bp + 24))) libc.Xmemcpy(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, *(*uintptr)(unsafe.Pointer(bp + 20)), uint32(nNew+int32(1))) if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fdb != 0 { (*TSessionTable)(unsafe.Pointer(pTab)).FnCol = 0 rc = _sessionInitTable(tls, uintptr(0), pTab, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fdb, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FzDb) if rc != 0 { _ = libc.Int32FromInt32(0) Xsqlite3_free(tls, pTab) break } } /* The new object must be linked on to the end of the list, not ** simply added to the start of it. This is to ensure that the ** tables within the output of sqlite3changegroup_output() are in ** the right order. */ ppTab = pGrp + 8 for { if !(*(*uintptr)(unsafe.Pointer(ppTab)) != 0) { break } goto _2 _2: ; ppTab = *(*uintptr)(unsafe.Pointer(ppTab)) } *(*uintptr)(unsafe.Pointer(ppTab)) = pTab } if !(_sessionChangesetCheckCompat(tls, pTab, *(*int32)(unsafe.Pointer(bp + 24)), *(*uintptr)(unsafe.Pointer(bp + 40))) != 0) { rc = int32(SQLITE_SCHEMA) break } } if *(*int32)(unsafe.Pointer(bp + 24)) < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol { _ = libc.Int32FromInt32(0) rc = _sessionChangesetExtendRecord(tls, pGrp, pTab, *(*int32)(unsafe.Pointer(bp + 24)), *(*int32)(unsafe.Pointer(bp + 28)), *(*uintptr)(unsafe.Pointer(bp)), *(*int32)(unsafe.Pointer(bp + 4)), bp+8) if rc != 0 { break } *(*uintptr)(unsafe.Pointer(bp)) = (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FaBuf *(*int32)(unsafe.Pointer(bp + 4)) = (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FnBuf } if _sessionGrowHash(tls, uintptr(0), (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset, pTab) != 0 { rc = int32(SQLITE_NOMEM) break } iHash = int32(_sessionChangeHash(tls, pTab, libc.BoolInt32((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset != 0 && *(*int32)(unsafe.Pointer(bp + 28)) == int32(SQLITE_DELETE)), *(*uintptr)(unsafe.Pointer(bp)), (*TSessionTable)(unsafe.Pointer(pTab)).FnChange)) /* Search for existing entry. If found, remove it from the hash table. ** Code below may link it back in. */ pp = (*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(iHash)*4 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != 0) { break } bPkOnly1 = 0 bPkOnly2 = 0 if (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset != 0 { bPkOnly1 = libc.BoolInt32(int32((*TSessionChange)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).Fop) == int32(SQLITE_DELETE)) bPkOnly2 = libc.BoolInt32(*(*int32)(unsafe.Pointer(bp + 28)) == int32(SQLITE_DELETE)) } if _sessionChangeEqual(tls, pTab, bPkOnly1, (*TSessionChange)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FaRecord, bPkOnly2, *(*uintptr)(unsafe.Pointer(bp))) != 0 { pExist = *(*uintptr)(unsafe.Pointer(pp)) *(*uintptr)(unsafe.Pointer(pp)) = (*TSessionChange)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FpNext (*TSessionTable)(unsafe.Pointer(pTab)).FnEntry-- break } goto _3 _3: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 16 } rc = _sessionChangeMerge(tls, pTab, bRebase, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset, pExist, *(*int32)(unsafe.Pointer(bp + 28)), *(*int32)(unsafe.Pointer(bp + 32)), *(*uintptr)(unsafe.Pointer(bp)), *(*int32)(unsafe.Pointer(bp + 4)), bp+36) if rc != 0 { break } if *(*uintptr)(unsafe.Pointer(bp + 36)) != 0 { (*TSessionChange)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 36)))).FpNext = *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(iHash)*4)) *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(iHash)*4)) = *(*uintptr)(unsafe.Pointer(bp + 36)) (*TSessionTable)(unsafe.Pointer(pTab)).FnEntry++ } } Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FaBuf) if rc == SQLITE_OK { rc = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Frc } return rc } // C documentation // // /* // ** Serialize a changeset (or patchset) based on all changesets (or patchsets) // ** added to the changegroup object passed as the first argument. // ** // ** If xOutput is not NULL, then the changeset/patchset is returned to the // ** user via one or more calls to xOutput, as with the other streaming // ** interfaces. // ** // ** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a // ** buffer containing the output changeset before this function returns. In // ** this case (*pnOut) is set to the size of the output buffer in bytes. It // ** is the responsibility of the caller to free the output buffer using // ** sqlite3_free() when it is no longer required. // ** // ** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite // ** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut) // ** are both set to 0 before returning. // */ func _sessionChangegroupOutput(tls *libc.TLS, pGrp uintptr, xOutput uintptr, pOut uintptr, pnOut uintptr, ppOut uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i int32 var p, pTab uintptr var _ /* buf at bp+4 */ TSessionBuffer var _ /* rc at bp+0 */ int32 _, _, _ = i, p, pTab *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*TSessionBuffer)(unsafe.Pointer(bp + 4)) = TSessionBuffer{} _ = libc.Int32FromInt32(0) /* Create the serialized output changeset based on the contents of the ** hash tables attached to the SessionTable objects in list p->pList. */ pTab = (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FpList for { if !(*(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && pTab != 0) { break } if (*TSessionTable)(unsafe.Pointer(pTab)).FnEntry == 0 { goto _1 } _sessionAppendTableHdr(tls, bp+4, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch, pTab, bp) i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnChange) { break } p = *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(i)*4)) for { if !(p != 0) { break } _sessionAppendByte(tls, bp+4, (*TSessionChange)(unsafe.Pointer(p)).Fop, bp) _sessionAppendByte(tls, bp+4, (*TSessionChange)(unsafe.Pointer(p)).FbIndirect, bp) _sessionAppendBlob(tls, bp+4, (*TSessionChange)(unsafe.Pointer(p)).FaRecord, (*TSessionChange)(unsafe.Pointer(p)).FnRecord, bp) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && xOutput != 0 && (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf >= _sessions_strm_chunk_size { *(*int32)(unsafe.Pointer(bp)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{xOutput})))(tls, pOut, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf) (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf = 0 } goto _3 _3: ; p = (*TSessionChange)(unsafe.Pointer(p)).FpNext } goto _2 _2: ; i++ } goto _1 _1: ; pTab = (*TSessionTable)(unsafe.Pointer(pTab)).FpNext } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { if xOutput != 0 { if (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf > 0 { *(*int32)(unsafe.Pointer(bp)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{xOutput})))(tls, pOut, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf) } } else { if ppOut != 0 { *(*uintptr)(unsafe.Pointer(ppOut)) = (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf if pnOut != 0 { *(*int32)(unsafe.Pointer(pnOut)) = (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FnBuf } (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf = uintptr(0) } } } Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 4))).FaBuf) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Allocate a new, empty, sqlite3_changegroup. // */ func Xsqlite3changegroup_new(tls *libc.TLS, pp uintptr) (r int32) { var p uintptr var rc int32 _, _ = p, rc rc = SQLITE_OK /* New object */ p = Xsqlite3_malloc(tls, int32(20)) if p == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, p, 0, uint32(20)) } *(*uintptr)(unsafe.Pointer(pp)) = p return rc } // C documentation // // /* // ** Provide a database schema to the changegroup object. // */ func Xsqlite3changegroup_schema(tls *libc.TLS, pGrp uintptr, db uintptr, zDb uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 _ = rc rc = SQLITE_OK if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FpList != 0 || (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fdb != 0 { /* Cannot add a schema after one or more calls to sqlite3changegroup_add(), ** or after sqlite3changegroup_schema() has already been called. */ rc = int32(SQLITE_MISUSE) } else { (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FzDb = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+8, zDb)) if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FzDb == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fdb = db } } return rc } // C documentation // // /* // ** Add the changeset currently stored in buffer pData, size nData bytes, // ** to changeset-group p. // */ func Xsqlite3changegroup_add(tls *libc.TLS, pGrp uintptr, nData int32, pData uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* pIter at bp+0 */ uintptr _ = rc /* Return code */ rc = Xsqlite3changeset_start(tls, bp, nData, pData) if rc == SQLITE_OK { rc = _sessionChangesetToHash(tls, *(*uintptr)(unsafe.Pointer(bp)), pGrp, 0) } Xsqlite3changeset_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) return rc } // C documentation // // /* // ** Obtain a buffer containing a changeset representing the concatenation // ** of all changesets added to the group so far. // */ func Xsqlite3changegroup_output(tls *libc.TLS, pGrp uintptr, pnData uintptr, ppData uintptr) (r int32) { return _sessionChangegroupOutput(tls, pGrp, uintptr(0), uintptr(0), pnData, ppData) } // C documentation // // /* // ** Streaming versions of changegroup_add(). // */ func Xsqlite3changegroup_add_strm(tls *libc.TLS, pGrp uintptr, xInput uintptr, pIn uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* pIter at bp+0 */ uintptr _ = rc /* Return code */ rc = Xsqlite3changeset_start_strm(tls, bp, xInput, pIn) if rc == SQLITE_OK { rc = _sessionChangesetToHash(tls, *(*uintptr)(unsafe.Pointer(bp)), pGrp, 0) } Xsqlite3changeset_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) return rc } // C documentation // // /* // ** Streaming versions of changegroup_output(). // */ func Xsqlite3changegroup_output_strm(tls *libc.TLS, pGrp uintptr, xOutput uintptr, pOut uintptr) (r int32) { return _sessionChangegroupOutput(tls, pGrp, xOutput, pOut, uintptr(0), uintptr(0)) } // C documentation // // /* // ** Delete a changegroup object. // */ func Xsqlite3changegroup_delete(tls *libc.TLS, pGrp uintptr) { if pGrp != 0 { Xsqlite3_free(tls, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FzDb) _sessionDeleteTable(tls, uintptr(0), (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FpList) Xsqlite3_free(tls, pGrp) } } // C documentation // // /* // ** Combine two changesets together. // */ func Xsqlite3changeset_concat(tls *libc.TLS, nLeft int32, pLeft uintptr, nRight int32, pRight uintptr, pnOut uintptr, ppOut uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* pGrp at bp+0 */ uintptr _ = rc rc = Xsqlite3changegroup_new(tls, bp) if rc == SQLITE_OK { rc = Xsqlite3changegroup_add(tls, *(*uintptr)(unsafe.Pointer(bp)), nLeft, pLeft) } if rc == SQLITE_OK { rc = Xsqlite3changegroup_add(tls, *(*uintptr)(unsafe.Pointer(bp)), nRight, pRight) } if rc == SQLITE_OK { rc = Xsqlite3changegroup_output(tls, *(*uintptr)(unsafe.Pointer(bp)), pnOut, ppOut) } Xsqlite3changegroup_delete(tls, *(*uintptr)(unsafe.Pointer(bp))) return rc } // C documentation // // /* // ** Streaming version of sqlite3changeset_concat(). // */ func Xsqlite3changeset_concat_strm(tls *libc.TLS, xInputA uintptr, pInA uintptr, xInputB uintptr, pInB uintptr, xOutput uintptr, pOut uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* pGrp at bp+0 */ uintptr _ = rc rc = Xsqlite3changegroup_new(tls, bp) if rc == SQLITE_OK { rc = Xsqlite3changegroup_add_strm(tls, *(*uintptr)(unsafe.Pointer(bp)), xInputA, pInA) } if rc == SQLITE_OK { rc = Xsqlite3changegroup_add_strm(tls, *(*uintptr)(unsafe.Pointer(bp)), xInputB, pInB) } if rc == SQLITE_OK { rc = Xsqlite3changegroup_output_strm(tls, *(*uintptr)(unsafe.Pointer(bp)), xOutput, pOut) } Xsqlite3changegroup_delete(tls, *(*uintptr)(unsafe.Pointer(bp))) return rc } /* ** Changeset rebaser handle. */ type Tsqlite3_rebaser1 = struct { Fgrp Tsqlite3_changegroup } type sqlite3_rebaser1 = Tsqlite3_rebaser1 // C documentation // // /* // ** Buffers a1 and a2 must both contain a sessions module record nCol // ** fields in size. This function appends an nCol sessions module // ** record to buffer pBuf that is a copy of a1, except that for // ** each field that is undefined in a1[], swap in the field from a2[]. // */ func _sessionAppendRecordMerge(tls *libc.TLS, pBuf uintptr, nCol int32, a1 uintptr, n1 int32, a2 uintptr, n2 int32, pRc uintptr) { var i, nn1, nn2 int32 var pOut uintptr _, _, _, _ = i, nn1, nn2, pOut _sessionBufferGrow(tls, pBuf, int64(n1+n2), pRc) if *(*int32)(unsafe.Pointer(pRc)) == SQLITE_OK { pOut = (*TSessionBuffer)(unsafe.Pointer(pBuf)).FaBuf + uintptr((*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf) i = 0 for { if !(i < nCol) { break } nn1 = _sessionSerialLen(tls, a1) nn2 = _sessionSerialLen(tls, a2) if int32(*(*Tu8)(unsafe.Pointer(a1))) == 0 || int32(*(*Tu8)(unsafe.Pointer(a1))) == int32(0xFF) { libc.Xmemcpy(tls, pOut, a2, uint32(nn2)) pOut += uintptr(nn2) } else { libc.Xmemcpy(tls, pOut, a1, uint32(nn1)) pOut += uintptr(nn1) } a1 += uintptr(nn1) a2 += uintptr(nn2) goto _1 _1: ; i++ } (*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf = int32(pOut) - int32((*TSessionBuffer)(unsafe.Pointer(pBuf)).FaBuf) _ = libc.Int32FromInt32(0) } } // C documentation // // /* // ** This function is called when rebasing a local UPDATE change against one // ** or more remote UPDATE changes. The aRec/nRec buffer contains the current // ** old.* and new.* records for the change. The rebase buffer (a single // ** record) is in aChange/nChange. The rebased change is appended to buffer // ** pBuf. // ** // ** Rebasing the UPDATE involves: // ** // ** * Removing any changes to fields for which the corresponding field // ** in the rebase buffer is set to "replaced" (type 0xFF). If this // ** means the UPDATE change updates no fields, nothing is appended // ** to the output buffer. // ** // ** * For each field modified by the local change for which the // ** corresponding field in the rebase buffer is not "undefined" (0x00) // ** or "replaced" (0xFF), the old.* value is replaced by the value // ** in the rebase buffer. // */ func _sessionAppendPartialUpdate(tls *libc.TLS, pBuf uintptr, pIter uintptr, aRec uintptr, nRec int32, aChange uintptr, nChange int32, pRc uintptr) { var a1, a2, pOut, v1, v2, v4, v6 uintptr var bData, i, n1, n11, n2, n21 int32 _, _, _, _, _, _, _, _, _, _, _, _, _ = a1, a2, bData, i, n1, n11, n2, n21, pOut, v1, v2, v4, v6 _sessionBufferGrow(tls, pBuf, int64(int32(2)+nRec+nChange), pRc) if *(*int32)(unsafe.Pointer(pRc)) == SQLITE_OK { bData = 0 pOut = (*TSessionBuffer)(unsafe.Pointer(pBuf)).FaBuf + uintptr((*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf) a1 = aRec a2 = aChange v1 = pOut pOut++ *(*Tu8)(unsafe.Pointer(v1)) = uint8(SQLITE_UPDATE) v2 = pOut pOut++ *(*Tu8)(unsafe.Pointer(v2)) = uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect) i = 0 for { if !(i < (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol) { break } n1 = _sessionSerialLen(tls, a1) n2 = _sessionSerialLen(tls, a2) if *(*Tu8)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FabPK + uintptr(i))) != 0 || int32(*(*Tu8)(unsafe.Pointer(a2))) == 0 { if !(*(*Tu8)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FabPK + uintptr(i))) != 0) && *(*Tu8)(unsafe.Pointer(a1)) != 0 { bData = int32(1) } libc.Xmemcpy(tls, pOut, a1, uint32(n1)) pOut += uintptr(n1) } else { if int32(*(*Tu8)(unsafe.Pointer(a2))) != int32(0xFF) && *(*Tu8)(unsafe.Pointer(a1)) != 0 { bData = int32(1) libc.Xmemcpy(tls, pOut, a2, uint32(n2)) pOut += uintptr(n2) } else { v4 = pOut pOut++ *(*Tu8)(unsafe.Pointer(v4)) = uint8('\000') } } a1 += uintptr(n1) a2 += uintptr(n2) goto _3 _3: ; i++ } if bData != 0 { a2 = aChange i = 0 for { if !(i < (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol) { break } n11 = _sessionSerialLen(tls, a1) n21 = _sessionSerialLen(tls, a2) if *(*Tu8)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FabPK + uintptr(i))) != 0 || int32(*(*Tu8)(unsafe.Pointer(a2))) != int32(0xFF) { libc.Xmemcpy(tls, pOut, a1, uint32(n11)) pOut += uintptr(n11) } else { v6 = pOut pOut++ *(*Tu8)(unsafe.Pointer(v6)) = uint8('\000') } a1 += uintptr(n11) a2 += uintptr(n21) goto _5 _5: ; i++ } (*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf = int32(pOut) - int32((*TSessionBuffer)(unsafe.Pointer(pBuf)).FaBuf) } } } // C documentation // // /* // ** pIter is configured to iterate through a changeset. This function rebases // ** that changeset according to the current configuration of the rebaser // ** object passed as the first argument. If no error occurs and argument xOutput // ** is not NULL, then the changeset is returned to the caller by invoking // ** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL, // ** then (*ppOut) is set to point to a buffer containing the rebased changeset // ** before this function returns. In this case (*pnOut) is set to the size of // ** the buffer in bytes. It is the responsibility of the caller to eventually // ** free the (*ppOut) buffer using sqlite3_free(). // ** // ** If an error occurs, an SQLite error code is returned. If ppOut and // ** pnOut are not NULL, then the two output parameters are set to 0 before // ** returning. // */ func _sessionRebase(tls *libc.TLS, p uintptr, pIter uintptr, xOutput uintptr, pOut uintptr, pnOut uintptr, ppOut uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var bDone, iHash, v2 int32 var pChange, pTab, zTab uintptr var _ /* aRec at bp+4 */ uintptr var _ /* bNew at bp+12 */ int32 var _ /* nRec at bp+8 */ int32 var _ /* pCsr at bp+28 */ uintptr var _ /* rc at bp+0 */ int32 var _ /* sOut at bp+16 */ TSessionBuffer _, _, _, _, _, _ = bDone, iHash, pChange, pTab, zTab, v2 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 8)) = 0 *(*int32)(unsafe.Pointer(bp + 12)) = 0 pTab = uintptr(0) *(*TSessionBuffer)(unsafe.Pointer(bp + 16)) = TSessionBuffer{} for int32(SQLITE_ROW) == _sessionChangesetNext(tls, pIter, bp+4, bp+8, bp+12) { pChange = uintptr(0) bDone = 0 if *(*int32)(unsafe.Pointer(bp + 12)) != 0 { zTab = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab pTab = (*Tsqlite3_rebaser)(unsafe.Pointer(p)).Fgrp.FpList for { if !(pTab != 0) { break } if 0 == Xsqlite3_stricmp(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, zTab) { break } goto _1 _1: ; pTab = (*TSessionTable)(unsafe.Pointer(pTab)).FpNext } *(*int32)(unsafe.Pointer(bp + 12)) = 0 /* A patchset may not be rebased */ if (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset != 0 { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } /* Append a table header to the output for this new table */ if (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset != 0 { v2 = int32('P') } else { v2 = int32('T') } _sessionAppendByte(tls, bp+16, uint8(v2), bp) _sessionAppendVarint(tls, bp+16, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol, bp) _sessionAppendBlob(tls, bp+16, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FabPK, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol, bp) _sessionAppendBlob(tls, bp+16, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab, int32(libc.Xstrlen(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab))+int32(1), bp) } if pTab != 0 && *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { iHash = int32(_sessionChangeHash(tls, pTab, 0, *(*uintptr)(unsafe.Pointer(bp + 4)), (*TSessionTable)(unsafe.Pointer(pTab)).FnChange)) pChange = *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(iHash)*4)) for { if !(pChange != 0) { break } if _sessionChangeEqual(tls, pTab, 0, *(*uintptr)(unsafe.Pointer(bp + 4)), 0, (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord) != 0 { break } goto _3 _3: ; pChange = (*TSessionChange)(unsafe.Pointer(pChange)).FpNext } } if pChange != 0 { _ = libc.Int32FromInt32(0) switch (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fop { case int32(SQLITE_INSERT): if int32((*TSessionChange)(unsafe.Pointer(pChange)).Fop) == int32(SQLITE_INSERT) { bDone = int32(1) if int32((*TSessionChange)(unsafe.Pointer(pChange)).FbIndirect) == 0 { _sessionAppendByte(tls, bp+16, uint8(SQLITE_UPDATE), bp) _sessionAppendByte(tls, bp+16, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect), bp) _sessionAppendBlob(tls, bp+16, (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord, (*TSessionChange)(unsafe.Pointer(pChange)).FnRecord, bp) _sessionAppendBlob(tls, bp+16, *(*uintptr)(unsafe.Pointer(bp + 4)), *(*int32)(unsafe.Pointer(bp + 8)), bp) } } case int32(SQLITE_UPDATE): bDone = int32(1) if int32((*TSessionChange)(unsafe.Pointer(pChange)).Fop) == int32(SQLITE_DELETE) { if int32((*TSessionChange)(unsafe.Pointer(pChange)).FbIndirect) == 0 { *(*uintptr)(unsafe.Pointer(bp + 28)) = *(*uintptr)(unsafe.Pointer(bp + 4)) _sessionSkipRecord(tls, bp+28, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol) _sessionAppendByte(tls, bp+16, uint8(SQLITE_INSERT), bp) _sessionAppendByte(tls, bp+16, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect), bp) _sessionAppendRecordMerge(tls, bp+16, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol, *(*uintptr)(unsafe.Pointer(bp + 28)), *(*int32)(unsafe.Pointer(bp + 8))-(int32(*(*uintptr)(unsafe.Pointer(bp + 28)))-int32(*(*uintptr)(unsafe.Pointer(bp + 4)))), (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord, (*TSessionChange)(unsafe.Pointer(pChange)).FnRecord, bp) } } else { _sessionAppendPartialUpdate(tls, bp+16, pIter, *(*uintptr)(unsafe.Pointer(bp + 4)), *(*int32)(unsafe.Pointer(bp + 8)), (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord, (*TSessionChange)(unsafe.Pointer(pChange)).FnRecord, bp) } default: _ = libc.Int32FromInt32(0) bDone = int32(1) if int32((*TSessionChange)(unsafe.Pointer(pChange)).Fop) == int32(SQLITE_INSERT) { _sessionAppendByte(tls, bp+16, uint8(SQLITE_DELETE), bp) _sessionAppendByte(tls, bp+16, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect), bp) _sessionAppendRecordMerge(tls, bp+16, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol, (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord, (*TSessionChange)(unsafe.Pointer(pChange)).FnRecord, *(*uintptr)(unsafe.Pointer(bp + 4)), *(*int32)(unsafe.Pointer(bp + 8)), bp) } break } } if bDone == 0 { _sessionAppendByte(tls, bp+16, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fop), bp) _sessionAppendByte(tls, bp+16, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect), bp) _sessionAppendBlob(tls, bp+16, *(*uintptr)(unsafe.Pointer(bp + 4)), *(*int32)(unsafe.Pointer(bp + 8)), bp) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && xOutput != 0 && (*(*TSessionBuffer)(unsafe.Pointer(bp + 16))).FnBuf > _sessions_strm_chunk_size { *(*int32)(unsafe.Pointer(bp)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{xOutput})))(tls, pOut, (*(*TSessionBuffer)(unsafe.Pointer(bp + 16))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 16))).FnBuf) (*(*TSessionBuffer)(unsafe.Pointer(bp + 16))).FnBuf = 0 } if *(*int32)(unsafe.Pointer(bp)) != 0 { break } } if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 16))).FaBuf) libc.Xmemset(tls, bp+16, 0, uint32(12)) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { if xOutput != 0 { if (*(*TSessionBuffer)(unsafe.Pointer(bp + 16))).FnBuf > 0 { *(*int32)(unsafe.Pointer(bp)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{xOutput})))(tls, pOut, (*(*TSessionBuffer)(unsafe.Pointer(bp + 16))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 16))).FnBuf) } } else { if ppOut != 0 { *(*uintptr)(unsafe.Pointer(ppOut)) = (*(*TSessionBuffer)(unsafe.Pointer(bp + 16))).FaBuf *(*int32)(unsafe.Pointer(pnOut)) = (*(*TSessionBuffer)(unsafe.Pointer(bp + 16))).FnBuf (*(*TSessionBuffer)(unsafe.Pointer(bp + 16))).FaBuf = uintptr(0) } } } Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 16))).FaBuf) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Create a new rebaser object. // */ func Xsqlite3rebaser_create(tls *libc.TLS, ppNew uintptr) (r int32) { var pNew uintptr var rc int32 _, _ = pNew, rc rc = SQLITE_OK pNew = Xsqlite3_malloc(tls, int32(20)) if pNew == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, pNew, 0, uint32(20)) } *(*uintptr)(unsafe.Pointer(ppNew)) = pNew return rc } // C documentation // // /* // ** Call this one or more times to configure a rebaser. // */ func Xsqlite3rebaser_configure(tls *libc.TLS, p uintptr, nRebase int32, pRebase uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* pIter at bp+0 */ uintptr _ = rc *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Return code */ rc = Xsqlite3changeset_start(tls, bp, nRebase, pRebase) if rc == SQLITE_OK { rc = _sessionChangesetToHash(tls, *(*uintptr)(unsafe.Pointer(bp)), p, int32(1)) } Xsqlite3changeset_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) return rc } // C documentation // // /* // ** Rebase a changeset according to current rebaser configuration // */ func Xsqlite3rebaser_rebase(tls *libc.TLS, p uintptr, nIn int32, pIn uintptr, pnOut uintptr, ppOut uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* pIter at bp+0 */ uintptr _ = rc *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Iterator to skip through input */ rc = Xsqlite3changeset_start(tls, bp, nIn, pIn) if rc == SQLITE_OK { rc = _sessionRebase(tls, p, *(*uintptr)(unsafe.Pointer(bp)), uintptr(0), uintptr(0), pnOut, ppOut) Xsqlite3changeset_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) } return rc } // C documentation // // /* // ** Rebase a changeset according to current rebaser configuration // */ func Xsqlite3rebaser_rebase_strm(tls *libc.TLS, p uintptr, xInput uintptr, pIn uintptr, xOutput uintptr, pOut uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* pIter at bp+0 */ uintptr _ = rc *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Iterator to skip through input */ rc = Xsqlite3changeset_start_strm(tls, bp, xInput, pIn) if rc == SQLITE_OK { rc = _sessionRebase(tls, p, *(*uintptr)(unsafe.Pointer(bp)), xOutput, pOut, uintptr(0), uintptr(0)) Xsqlite3changeset_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) } return rc } // C documentation // // /* // ** Destroy a rebaser object // */ func Xsqlite3rebaser_delete(tls *libc.TLS, p uintptr) { if p != 0 { _sessionDeleteTable(tls, uintptr(0), (*Tsqlite3_rebaser)(unsafe.Pointer(p)).Fgrp.FpList) Xsqlite3_free(tls, p) } } // C documentation // // /* // ** Global configuration // */ func Xsqlite3session_config(tls *libc.TLS, op int32, pArg uintptr) (r int32) { var pInt uintptr var rc int32 _, _ = pInt, rc rc = SQLITE_OK switch op { case int32(SQLITE_SESSION_CONFIG_STRMSIZE): pInt = pArg if *(*int32)(unsafe.Pointer(pInt)) > 0 { _sessions_strm_chunk_size = *(*int32)(unsafe.Pointer(pInt)) } *(*int32)(unsafe.Pointer(pInt)) = _sessions_strm_chunk_size default: rc = int32(SQLITE_MISUSE) break } return rc } /************** End of sqlite3session.c **************************************/ /************** Begin file fts5.c ********************************************/ /* ** 2014 May 31 ** ** 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. ** ****************************************************************************** ** ** Interfaces to extend FTS5. Using the interfaces defined in this file, ** FTS5 may be extended with: ** ** * custom tokenizers, and ** * custom auxiliary functions. */ /* ** 2014 May 31 ** ** 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. ** ****************************************************************************** ** */ /* #include "fts5.h" */ /* #include "sqlite3ext.h" */ /* #include */ /* #include */ /* Truncate very long tokens to this many bytes. Hard limit is ** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset ** field that occurs at the start of each leaf page (see fts5_index.c). */ /* ** Maximum number of prefix indexes on single FTS5 table. This must be ** less than 32. If it is set to anything large than that, an #error ** directive in fts5_index.c will cause the build to fail. */ /* ** Maximum segments permitted in a single index */ /* Name of rank and rowid columns */ /* ** The assert_nc() macro is similar to the assert() macro, except that it ** is used for assert() conditions that are true only if it can be ** guranteed that the database is not corrupt. */ /* ** A version of memcmp() that does not cause asan errors if one of the pointer ** parameters is NULL and the number of bytes to compare is zero. */ /* Mark a function parameter as unused, to suppress nuisance compiler ** warnings. */ type TFts5Global = struct { F__ccgo_align [0]uint32 Fapi Tfts5_api Fdb uintptr F__ccgo_align2 [4]byte FiNextId Ti64 FpAux uintptr FpTok uintptr FpDfltTok uintptr FpCsr uintptr } type Fts5Global = TFts5Global type TFts5Colset = struct { FnCol int32 FaiCol [1]int32 } type Fts5Colset = TFts5Colset /* If a NEAR() clump or phrase may only match a specific set of columns, ** then an object of the following type is used to record the set of columns. ** Each entry in the aiCol[] array is a column that may be matched. ** ** This object is used by fts5_expr.c and fts5_index.c. */ type TFts5Colset1 = struct { FnCol int32 FaiCol [1]int32 } type Fts5Colset1 = TFts5Colset1 /************************************************************************** ** Interface to code in fts5_config.c. fts5_config.c contains contains code ** to parse the arguments passed to the CREATE VIRTUAL TABLE statement. */ type TFts5Config = struct { Fdb uintptr FzDb uintptr FzName uintptr FnCol int32 FazCol uintptr FabUnindexed uintptr FnPrefix int32 FaPrefix uintptr FeContent int32 FbContentlessDelete int32 FzContent uintptr FzContentRowid uintptr FbColumnsize int32 FbTokendata int32 FeDetail int32 FzContentExprlist uintptr FpTok uintptr FpTokApi uintptr FbLock int32 FePattern int32 FiVersion int32 FiCookie int32 Fpgsz int32 FnAutomerge int32 FnCrisisMerge int32 FnUsermerge int32 FnHashSize int32 FzRank uintptr FzRankArgs uintptr FbSecureDelete int32 FnDeleteMerge int32 FpzErrmsg uintptr } type Fts5Config = TFts5Config /* ** An instance of the following structure encodes all information that can ** be gleaned from the CREATE VIRTUAL TABLE statement. ** ** And all information loaded from the %_config table. ** ** nAutomerge: ** The minimum number of segments that an auto-merge operation should ** attempt to merge together. A value of 1 sets the object to use the ** compile time default. Zero disables auto-merge altogether. ** ** bContentlessDelete: ** True if the contentless_delete option was present in the CREATE ** VIRTUAL TABLE statement. ** ** zContent: ** ** zContentRowid: ** The value of the content_rowid= option, if one was specified. Or ** the string "rowid" otherwise. This text is not quoted - if it is ** used as part of an SQL statement it needs to be quoted appropriately. ** ** zContentExprlist: ** ** pzErrmsg: ** This exists in order to allow the fts5_index.c module to return a ** decent error message if it encounters a file-format version it does ** not understand. ** ** bColumnsize: ** True if the %_docsize table is created. ** ** bPrefixIndex: ** This is only used for debugging. If set to false, any prefix indexes ** are ignored. This value is configured using: ** ** INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex); ** */ type TFts5Config1 = struct { Fdb uintptr FzDb uintptr FzName uintptr FnCol int32 FazCol uintptr FabUnindexed uintptr FnPrefix int32 FaPrefix uintptr FeContent int32 FbContentlessDelete int32 FzContent uintptr FzContentRowid uintptr FbColumnsize int32 FbTokendata int32 FeDetail int32 FzContentExprlist uintptr FpTok uintptr FpTokApi uintptr FbLock int32 FePattern int32 FiVersion int32 FiCookie int32 Fpgsz int32 FnAutomerge int32 FnCrisisMerge int32 FnUsermerge int32 FnHashSize int32 FzRank uintptr FzRankArgs uintptr FbSecureDelete int32 FnDeleteMerge int32 FpzErrmsg uintptr } type Fts5Config1 = TFts5Config1 /* ** End of interface to code in fts5_config.c. **************************************************************************/ /************************************************************************** ** Interface to code in fts5_buffer.c. */ // C documentation // // /* // ** Buffer object for the incremental building of string data. // */ type TFts5Buffer = struct { Fp uintptr Fn int32 FnSpace int32 } type Fts5Buffer = TFts5Buffer type TFts5Buffer1 = struct { Fp uintptr Fn int32 FnSpace int32 } type Fts5Buffer1 = TFts5Buffer1 type TFts5PoslistReader = struct { F__ccgo_align [0]uint32 Fa uintptr Fn int32 Fi int32 FbFlag Tu8 FbEof Tu8 F__ccgo_align5 [2]byte FiPos Ti64 } type Fts5PoslistReader = TFts5PoslistReader type TFts5PoslistReader1 = struct { F__ccgo_align [0]uint32 Fa uintptr Fn int32 Fi int32 FbFlag Tu8 FbEof Tu8 F__ccgo_align5 [2]byte FiPos Ti64 } type Fts5PoslistReader1 = TFts5PoslistReader1 type TFts5PoslistWriter = struct { F__ccgo_align [0]uint32 FiPrev Ti64 } type Fts5PoslistWriter = TFts5PoslistWriter type TFts5PoslistWriter1 = struct { F__ccgo_align [0]uint32 FiPrev Ti64 } type Fts5PoslistWriter1 = TFts5PoslistWriter1 // C documentation // // /* Bucket of terms object used by the integrity-check in offsets=0 mode. */ type TFts5Termset = struct { FapHash [512]uintptr } type Fts5Termset = TFts5Termset /* ** End of interface to code in fts5_buffer.c. **************************************************************************/ /************************************************************************** ** Interface to code in fts5_index.c. fts5_index.c contains contains code ** to access the data stored in the %_data table. */ type TFts5Index = struct { F__ccgo_align [0]uint32 FpConfig uintptr FzDataTbl uintptr FnWorkUnit int32 FpHash uintptr FnPendingData int32 F__ccgo_align5 [4]byte FiWriteRowid Ti64 FbDelete int32 FnContentlessDelete int32 FnPendingRow int32 Frc int32 FflushRc int32 FpReader uintptr FpWriter uintptr FpDeleter uintptr FpIdxWriter uintptr FpIdxDeleter uintptr FpIdxSelect uintptr FpIdxNextSelect uintptr FnRead int32 FpDeleteFromIdx uintptr FpDataVersion uintptr F__ccgo_align21 [4]byte FiStructVersion Ti64 FpStruct uintptr F__ccgo_pad23 [4]byte } type Fts5Index = TFts5Index type TFts5IndexIter = struct { F__ccgo_align [0]uint32 FiRowid Ti64 FpData uintptr FnData int32 FbEof Tu8 F__ccgo_pad4 [7]byte } type Fts5IndexIter = TFts5IndexIter type TFts5IndexIter1 = struct { F__ccgo_align [0]uint32 FiRowid Ti64 FpData uintptr FnData int32 FbEof Tu8 F__ccgo_pad4 [7]byte } type Fts5IndexIter1 = TFts5IndexIter1 /* ** End of interface to code in fts5_varint.c. **************************************************************************/ /************************************************************************** ** Interface to code in fts5_main.c. */ // C documentation // // /* // ** Virtual-table object. // */ type TFts5Table = struct { Fbase Tsqlite3_vtab FpConfig uintptr FpIndex uintptr } type Fts5Table = TFts5Table type TFts5Table1 = struct { Fbase Tsqlite3_vtab FpConfig uintptr FpIndex uintptr } type Fts5Table1 = TFts5Table1 /* ** End of interface to code in fts5.c. **************************************************************************/ // C documentation // // /************************************************************************** // ** Interface to code in fts5_hash.c. // */ type TFts5Hash = struct { FeDetail int32 FpnByte uintptr FnEntry int32 FnSlot int32 FpScan uintptr FaSlot uintptr } type Fts5Hash = TFts5Hash /* ** End of interface to code in fts5_hash.c. **************************************************************************/ /************************************************************************** ** Interface to code in fts5_storage.c. fts5_storage.c contains contains ** code to access the data stored in the %_content and %_docsize tables. */ type TFts5Storage = struct { F__ccgo_align [0]uint32 FpConfig uintptr FpIndex uintptr FbTotalsValid int32 F__ccgo_align3 [4]byte FnTotalRow Ti64 FaTotalSize uintptr FaStmt [11]uintptr } type Fts5Storage = TFts5Storage /* ** End of interface to code in fts5_storage.c. **************************************************************************/ // C documentation // // /************************************************************************** // ** Interface to code in fts5_expr.c. // */ type TFts5Expr = struct { FpIndex uintptr FpConfig uintptr FpRoot uintptr FbDesc int32 FnPhrase int32 FapExprPhrase uintptr } type Fts5Expr = TFts5Expr type TFts5ExprNode = struct { F__ccgo_align [0]uint32 FeType int32 FbEof int32 FbNomatch int32 FiHeight int32 FxNext uintptr F__ccgo_align5 [4]byte FiRowid Ti64 FpNear uintptr FnChild int32 FapChild [1]uintptr F__ccgo_pad9 [4]byte } type Fts5ExprNode = TFts5ExprNode type TFts5Parse = struct { FpConfig uintptr FzErr uintptr Frc int32 FnPhrase int32 FapPhrase uintptr FpExpr uintptr FbPhraseToAnd int32 } type Fts5Parse = TFts5Parse type TFts5Token = struct { Fp uintptr Fn int32 } type Fts5Token = TFts5Token type TFts5ExprPhrase = struct { FpNode uintptr Fposlist TFts5Buffer FnTerm int32 FaTerm [1]TFts5ExprTerm } type Fts5ExprPhrase = TFts5ExprPhrase type TFts5ExprNearset = struct { FnNear int32 FpColset uintptr FnPhrase int32 FapPhrase [1]uintptr } type Fts5ExprNearset = TFts5ExprNearset type TFts5Token1 = struct { Fp uintptr Fn int32 } type Fts5Token1 = TFts5Token1 type TFts5PoslistPopulator = struct { F__ccgo_align [0]uint32 Fwriter TFts5PoslistWriter FbOk int32 FbMiss int32 } type Fts5PoslistPopulator = TFts5PoslistPopulator /* ** End of interface to code in fts5_unicode2.c. **************************************************************************/ /* This file is automatically generated by Lemon from input grammar ** source file "fts5parse.y". */ /* ** 2000-05-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. ** ************************************************************************* ** Driver template for the LEMON parser generator. ** ** The "lemon" program processes an LALR(1) input grammar file, then uses ** this template to construct a parser. The "lemon" program inserts text ** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the ** interstitial "-" characters) contained in this template is changed into ** the value of the %name directive from the grammar. Otherwise, the content ** of this template is copied straight through into the generate parser ** source file. ** ** The following is the concatenation of all %include directives from the ** input grammar file: */ /************ Begin %include sections from the grammar ************************/ /* #include "fts5Int.h" */ /* #include "fts5parse.h" */ /* ** Disable all error recovery processing in the parser push-down ** automaton. */ /* ** Make fts5yytestcase() the same as testcase() */ /* ** Indicate that sqlite3ParserFree() will never be called with a null ** pointer. */ /* ** Alternative datatype for the argument to the malloc() routine passed ** into sqlite3ParserAlloc(). The default is size_t. */ /**************** End of %include directives **********************************/ /* These constants specify the various numeric values for terminal symbols. ***************** Begin token definitions *************************************/ /**************** End token definitions ***************************************/ // C documentation // // /* The next sections is a series of control #defines. // ** various aspects of the generated parser. // ** fts5YYCODETYPE is the data type used to store the integer codes // ** that represent terminal and non-terminal symbols. // ** "unsigned char" is used if there are fewer than // ** 256 symbols. Larger types otherwise. // ** fts5YYNOCODE is a number of type fts5YYCODETYPE that is not used for // ** any terminal or nonterminal symbol. // ** fts5YYFALLBACK If defined, this indicates that one or more tokens // ** (also known as: "terminal symbols") have fall-back // ** values which should be used if the original symbol // ** would not parse. This permits keywords to sometimes // ** be used as identifiers, for example. // ** fts5YYACTIONTYPE is the data type used for "action codes" - numbers // ** that indicate what to do in response to the next // ** token. // ** sqlite3Fts5ParserFTS5TOKENTYPE is the data type used for minor type for terminal // ** symbols. Background: A "minor type" is a semantic // ** value associated with a terminal or non-terminal // ** symbols. For example, for an "ID" terminal symbol, // ** the minor type might be the name of the identifier. // ** Each non-terminal can have a different minor type. // ** Terminal symbols all have the same minor type, though. // ** This macros defines the minor type for terminal // ** symbols. // ** fts5YYMINORTYPE is the data type used for all minor types. // ** This is typically a union of many types, one of // ** which is sqlite3Fts5ParserFTS5TOKENTYPE. The entry in the union // ** for terminal symbols is called "fts5yy0". // ** fts5YYSTACKDEPTH is the maximum depth of the parser's stack. If // ** zero the stack is dynamically sized using realloc() // ** sqlite3Fts5ParserARG_SDECL A static variable declaration for the %extra_argument // ** sqlite3Fts5ParserARG_PDECL A parameter declaration for the %extra_argument // ** sqlite3Fts5ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter // ** sqlite3Fts5ParserARG_STORE Code to store %extra_argument into fts5yypParser // ** sqlite3Fts5ParserARG_FETCH Code to extract %extra_argument from fts5yypParser // ** sqlite3Fts5ParserCTX_* As sqlite3Fts5ParserARG_ except for %extra_context // ** fts5YYERRORSYMBOL is the code number of the error symbol. If not // ** defined, then do no error processing. // ** fts5YYNSTATE the combined number of states. // ** fts5YYNRULE the number of rules in the grammar // ** fts5YYNFTS5TOKEN Number of terminal symbols // ** fts5YY_MAX_SHIFT Maximum value for shift actions // ** fts5YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions // ** fts5YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions // ** fts5YY_ERROR_ACTION The fts5yy_action[] code for syntax error // ** fts5YY_ACCEPT_ACTION The fts5yy_action[] code for accept // ** fts5YY_NO_ACTION The fts5yy_action[] code for no-op // ** fts5YY_MIN_REDUCE Minimum value for reduce actions // ** fts5YY_MAX_REDUCE Maximum value for reduce actions // */ // /************* Begin control #defines *****************************************/ type Tfts5YYMINORTYPE = struct { Ffts5yy0 [0]TFts5Token Ffts5yy4 [0]int32 Ffts5yy11 [0]uintptr Ffts5yy24 [0]uintptr Ffts5yy46 [0]uintptr Ffts5yy53 [0]uintptr Ffts5yyinit int32 F__ccgo_pad7 [4]byte } type fts5YYMINORTYPE = Tfts5YYMINORTYPE /************* End control #defines *******************************************/ /* Define the fts5yytestcase() macro to be a no-op if is not already defined ** otherwise. ** ** Applications can choose to define fts5yytestcase() in the %include section ** to a macro that can assist in verifying code coverage. For production ** code the fts5yytestcase() macro should be turned off. But it is useful ** for testing. */ // C documentation // // /* Next are the tables used to determine what action to take based on the // ** current state and lookahead token. These tables are used to implement // ** functions that take a state number and lookahead value and return an // ** action integer. // ** // ** Suppose the action integer is N. Then the action is determined as // ** follows // ** // ** 0 <= N <= fts5YY_MAX_SHIFT Shift N. That is, push the lookahead // ** token onto the stack and goto state N. // ** // ** N between fts5YY_MIN_SHIFTREDUCE Shift to an arbitrary state then // ** and fts5YY_MAX_SHIFTREDUCE reduce by rule N-fts5YY_MIN_SHIFTREDUCE. // ** // ** N == fts5YY_ERROR_ACTION A syntax error has occurred. // ** // ** N == fts5YY_ACCEPT_ACTION The parser accepts its input. // ** // ** N == fts5YY_NO_ACTION No such action. Denotes unused // ** slots in the fts5yy_action[] table. // ** // ** N between fts5YY_MIN_REDUCE Reduce by rule N-fts5YY_MIN_REDUCE // ** and fts5YY_MAX_REDUCE // ** // ** The action table is constructed as a single large table named fts5yy_action[]. // ** Given state S and lookahead X, the action is computed as either: // ** // ** (A) N = fts5yy_action[ fts5yy_shift_ofst[S] + X ] // ** (B) N = fts5yy_default[S] // ** // ** The (A) formula is preferred. The B formula is used instead if // ** fts5yy_lookahead[fts5yy_shift_ofst[S]+X] is not equal to X. // ** // ** The formulas above are for computing the action when the lookahead is // ** a terminal symbol. If the lookahead is a non-terminal (as occurs after // ** a reduce action) then the fts5yy_reduce_ofst[] array is used in place of // ** the fts5yy_shift_ofst[] array. // ** // ** The following are the tables generated in this section: // ** // ** fts5yy_action[] A single table containing all actions. // ** fts5yy_lookahead[] A table containing the lookahead for each entry in // ** fts5yy_action. Used to detect hash collisions. // ** fts5yy_shift_ofst[] For each state, the offset into fts5yy_action for // ** shifting terminals. // ** fts5yy_reduce_ofst[] For each state, the offset into fts5yy_action for // ** shifting non-terminals after a reduce. // ** fts5yy_default[] Default action for each state. // ** // *********** Begin parsing tables **********************************************/ var _fts5yy_action = [105]uint8{ 0: uint8(81), 1: uint8(20), 2: uint8(96), 3: uint8(6), 4: uint8(28), 5: uint8(99), 6: uint8(98), 7: uint8(26), 8: uint8(26), 9: uint8(18), 10: uint8(96), 11: uint8(6), 12: uint8(28), 13: uint8(17), 14: uint8(98), 15: uint8(56), 16: uint8(26), 17: uint8(19), 18: uint8(96), 19: uint8(6), 20: uint8(28), 21: uint8(14), 22: uint8(98), 23: uint8(14), 24: uint8(26), 25: uint8(31), 26: uint8(92), 27: uint8(96), 28: uint8(6), 29: uint8(28), 30: uint8(108), 31: uint8(98), 32: uint8(25), 33: uint8(26), 34: uint8(21), 35: uint8(96), 36: uint8(6), 37: uint8(28), 38: uint8(78), 39: uint8(98), 40: uint8(58), 41: uint8(26), 42: uint8(29), 43: uint8(96), 44: uint8(6), 45: uint8(28), 46: uint8(107), 47: uint8(98), 48: uint8(22), 49: uint8(26), 50: uint8(24), 51: uint8(16), 52: uint8(12), 53: uint8(11), 54: uint8(1), 55: uint8(13), 56: uint8(13), 57: uint8(24), 58: uint8(16), 59: uint8(23), 60: uint8(11), 61: uint8(33), 62: uint8(34), 63: uint8(13), 64: uint8(97), 65: uint8(8), 66: uint8(27), 67: uint8(32), 68: uint8(98), 69: uint8(7), 70: uint8(26), 71: uint8(3), 72: uint8(4), 73: uint8(5), 74: uint8(3), 75: uint8(4), 76: uint8(5), 77: uint8(3), 78: uint8(83), 79: uint8(4), 80: uint8(5), 81: uint8(3), 82: uint8(63), 83: uint8(5), 84: uint8(3), 85: uint8(62), 86: uint8(12), 87: uint8(2), 88: uint8(86), 89: uint8(13), 90: uint8(9), 91: uint8(30), 92: uint8(10), 93: uint8(10), 94: uint8(54), 95: uint8(57), 96: uint8(75), 97: uint8(78), 98: uint8(78), 99: uint8(53), 100: uint8(57), 101: uint8(15), 102: uint8(82), 103: uint8(82), 104: uint8(71), } var _fts5yy_lookahead = [121]uint8{ 0: uint8(16), 1: uint8(17), 2: uint8(18), 3: uint8(19), 4: uint8(20), 5: uint8(22), 6: uint8(22), 7: uint8(24), 8: uint8(24), 9: uint8(17), 10: uint8(18), 11: uint8(19), 12: uint8(20), 13: uint8(7), 14: uint8(22), 15: uint8(9), 16: uint8(24), 17: uint8(17), 18: uint8(18), 19: uint8(19), 20: uint8(20), 21: uint8(9), 22: uint8(22), 23: uint8(9), 24: uint8(24), 25: uint8(13), 26: uint8(17), 27: uint8(18), 28: uint8(19), 29: uint8(20), 30: uint8(26), 31: uint8(22), 32: uint8(24), 33: uint8(24), 34: uint8(17), 35: uint8(18), 36: uint8(19), 37: uint8(20), 38: uint8(15), 39: uint8(22), 40: uint8(9), 41: uint8(24), 42: uint8(17), 43: uint8(18), 44: uint8(19), 45: uint8(20), 46: uint8(26), 47: uint8(22), 48: uint8(21), 49: uint8(24), 50: uint8(6), 51: uint8(7), 52: uint8(9), 53: uint8(9), 54: uint8(10), 55: uint8(12), 56: uint8(12), 57: uint8(6), 58: uint8(7), 59: uint8(21), 60: uint8(9), 61: uint8(24), 62: uint8(25), 63: uint8(12), 64: uint8(18), 65: uint8(5), 66: uint8(20), 67: uint8(14), 68: uint8(22), 69: uint8(5), 70: uint8(24), 71: uint8(3), 72: uint8(1), 73: uint8(2), 74: uint8(3), 75: uint8(1), 76: uint8(2), 77: uint8(3), 79: uint8(1), 80: uint8(2), 81: uint8(3), 82: uint8(11), 83: uint8(2), 84: uint8(3), 85: uint8(11), 86: uint8(9), 87: uint8(10), 88: uint8(5), 89: uint8(12), 90: uint8(23), 91: uint8(24), 92: uint8(10), 93: uint8(10), 94: uint8(8), 95: uint8(9), 96: uint8(9), 97: uint8(15), 98: uint8(15), 99: uint8(8), 100: uint8(9), 101: uint8(9), 102: uint8(27), 103: uint8(27), 104: uint8(11), 105: uint8(27), 106: uint8(27), 107: uint8(27), 108: uint8(27), 109: uint8(27), 110: uint8(27), 111: uint8(27), 112: uint8(27), 113: uint8(27), 114: uint8(27), 115: uint8(27), 116: uint8(27), 117: uint8(27), 118: uint8(27), 119: uint8(27), 120: uint8(27), } var _fts5yy_shift_ofst = [35]uint8{ 0: uint8(44), 1: uint8(44), 2: uint8(44), 3: uint8(44), 4: uint8(44), 5: uint8(44), 6: uint8(51), 7: uint8(77), 8: uint8(43), 9: uint8(12), 10: uint8(14), 11: uint8(83), 12: uint8(82), 13: uint8(14), 14: uint8(23), 15: uint8(23), 16: uint8(31), 17: uint8(31), 18: uint8(71), 19: uint8(74), 20: uint8(78), 21: uint8(81), 22: uint8(86), 23: uint8(91), 24: uint8(6), 25: uint8(53), 26: uint8(53), 27: uint8(60), 28: uint8(64), 29: uint8(68), 30: uint8(53), 31: uint8(87), 32: uint8(92), 33: uint8(53), 34: uint8(93), } var _fts5yy_reduce_ofst = [18]int8{ 0: int8(-int32(16)), 1: int8(-int32(8)), 3: int8(9), 4: int8(17), 5: int8(25), 6: int8(46), 7: int8(-int32(17)), 8: int8(-int32(17)), 9: int8(37), 10: int8(67), 11: int8(4), 12: int8(4), 13: int8(8), 14: int8(4), 15: int8(20), 16: int8(27), 17: int8(38), } var _fts5yy_default = [35]uint8{ 0: uint8(80), 1: uint8(80), 2: uint8(80), 3: uint8(80), 4: uint8(80), 5: uint8(80), 6: uint8(95), 7: uint8(80), 8: uint8(80), 9: uint8(105), 10: uint8(80), 11: uint8(110), 12: uint8(110), 13: uint8(80), 14: uint8(110), 15: uint8(110), 16: uint8(80), 17: uint8(80), 18: uint8(80), 19: uint8(80), 20: uint8(80), 21: uint8(91), 22: uint8(80), 23: uint8(80), 24: uint8(80), 25: uint8(101), 26: uint8(100), 27: uint8(80), 28: uint8(80), 29: uint8(90), 30: uint8(103), 31: uint8(80), 32: uint8(80), 33: uint8(104), 34: uint8(80), } /********** End of lemon-generated parsing tables *****************************/ /* The next table maps tokens (terminal symbols) into fallback tokens. ** If a construct like the following: ** ** %fallback ID X Y Z. ** ** appears in the grammar, then ID becomes a fallback token for X, Y, ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser ** but it does not parse, the type of the token is changed to ID and ** the parse is retried before an error is thrown. ** ** This feature can be used, for example, to cause some keywords in a language ** to revert to identifiers if they keyword does not apply in the context where ** it appears. */ /* The following structure represents a single element of the ** parser's stack. Information stored includes: ** ** + The state number for the parser at this level of the stack. ** ** + The value of the token stored at this level of the stack. ** (In other words, the "major" token.) ** ** + The semantic value stored at this level of the stack. This is ** the information used by the action routines in the grammar. ** It is sometimes called the "minor" token. ** ** After the "shift" half of a SHIFTREDUCE action, the stateno field ** actually contains the reduce action for the second half of the ** SHIFTREDUCE. */ type Tfts5yyStackEntry1 = struct { Fstateno uint8 Fmajor uint8 Fminor Tfts5YYMINORTYPE } type fts5yyStackEntry1 = Tfts5yyStackEntry1 type Tfts5yyStackEntry = struct { Fstateno uint8 Fmajor uint8 Fminor Tfts5YYMINORTYPE } type fts5yyStackEntry = Tfts5yyStackEntry /* The state of the parser is completely contained in an instance of ** the following structure */ type Tfts5yyParser1 = struct { Ffts5yytos uintptr FpParse uintptr Ffts5yystack [100]Tfts5yyStackEntry Ffts5yystackEnd uintptr } type fts5yyParser1 = Tfts5yyParser1 type Tfts5yyParser = struct { Ffts5yytos uintptr FpParse uintptr Ffts5yystack [100]Tfts5yyStackEntry Ffts5yystackEnd uintptr } type fts5yyParser = Tfts5yyParser /* #include */ /* Datatype of the argument to the memory allocated passed as the ** second argument to sqlite3Fts5ParserAlloc() below. This can be changed by ** putting an appropriate #define in the %include section of the input ** grammar. */ // C documentation // // /* Initialize a new parser that has already been allocated. // */ func _sqlite3Fts5ParserInit(tls *libc.TLS, fts5yypRawParser uintptr) { var fts5yypParser uintptr _ = fts5yypParser fts5yypParser = fts5yypRawParser (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos = fts5yypParser + 8 (*(*Tfts5yyStackEntry)(unsafe.Pointer(fts5yypParser + 8))).Fstateno = uint8(0) (*(*Tfts5yyStackEntry)(unsafe.Pointer(fts5yypParser + 8))).Fmajor = uint8(0) (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yystackEnd = fts5yypParser + 8 + uintptr(libc.Int32FromInt32(fts5YYSTACKDEPTH)-libc.Int32FromInt32(1))*12 } // C documentation // // /* // ** This function allocates a new parser. // ** The only argument is a pointer to a function which works like // ** malloc. // ** // ** Inputs: // ** A pointer to the function used to allocate memory. // ** // ** Outputs: // ** A pointer to a parser. This pointer is used in subsequent calls // ** to sqlite3Fts5Parser and sqlite3Fts5ParserFree. // */ func _sqlite3Fts5ParserAlloc(tls *libc.TLS, mallocProc uintptr) (r uintptr) { var fts5yypParser uintptr _ = fts5yypParser fts5yypParser = (*(*func(*libc.TLS, Tu64) uintptr)(unsafe.Pointer(&struct{ uintptr }{mallocProc})))(tls, libc.Uint64FromInt64(1212)) if fts5yypParser != 0 { _sqlite3Fts5ParserInit(tls, fts5yypParser) } return fts5yypParser } // C documentation // // /* The following function deletes the "minor type" or semantic value // ** associated with a symbol. The symbol can be either a terminal // ** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is // ** a pointer to the value to be deleted. The code used to do the // ** deletions is derived from the %destructor and/or %token_destructor // ** directives of the input grammar. // */ func _fts5yy_destructor(tls *libc.TLS, fts5yypParser uintptr, fts5yymajor uint8, fts5yypminor uintptr) { var pParse uintptr _ = pParse pParse = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).FpParse switch int32(fts5yymajor) { /* Here is inserted the actions which take place when a ** terminal or non-terminal is destroyed. This can happen ** when the symbol is popped from the stack during a ** reduce or during error processing or when a parser is ** being destroyed before it is finished parsing. ** ** Note: during a reduce, the only symbols destroyed are those ** which appear on the RHS of the rule, but which are *not* used ** inside the C code. */ /********* Begin destructor definitions ***************************************/ case int32(16): /* input */ _ = pParse case int32(17): /* expr */ fallthrough case int32(18): /* cnearset */ fallthrough case int32(19): /* exprlist */ _sqlite3Fts5ParseNodeFree(tls, *(*uintptr)(unsafe.Pointer(fts5yypminor))) case int32(20): /* colset */ fallthrough case int32(21): /* colsetlist */ Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(fts5yypminor))) case int32(22): /* nearset */ fallthrough case int32(23): /* nearphrases */ _sqlite3Fts5ParseNearsetFree(tls, *(*uintptr)(unsafe.Pointer(fts5yypminor))) case int32(24): /* phrase */ _sqlite3Fts5ParsePhraseFree(tls, *(*uintptr)(unsafe.Pointer(fts5yypminor))) break /********* End destructor definitions *****************************************/ fallthrough default: break /* If no destructor action specified: do nothing */ } } // C documentation // // /* // ** Pop the parser's stack once. // ** // ** If there is a destructor routine associated with the token which // ** is popped from the stack, then call it. // */ func _fts5yy_pop_parser_stack(tls *libc.TLS, pParser uintptr) { var fts5yytos, v1, v2 uintptr _, _, _ = fts5yytos, v1, v2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) v2 = pParser v1 = *(*uintptr)(unsafe.Pointer(v2)) *(*uintptr)(unsafe.Pointer(v2)) -= 12 fts5yytos = v1 _fts5yy_destructor(tls, pParser, (*Tfts5yyStackEntry)(unsafe.Pointer(fts5yytos)).Fmajor, fts5yytos+4) } // C documentation // // /* // ** Clear all secondary memory allocations from the parser // */ func _sqlite3Fts5ParserFinalize(tls *libc.TLS, p uintptr) { var pParser uintptr _ = pParser pParser = p for (*Tfts5yyParser)(unsafe.Pointer(pParser)).Ffts5yytos > pParser+8 { _fts5yy_pop_parser_stack(tls, pParser) } } // C documentation // // /* // ** Deallocate and destroy a parser. Destructors are called for // ** all stack elements before shutting the parser down. // ** // ** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it // ** is defined in a %include section of the input grammar) then it is // ** assumed that the input pointer is never NULL. // */ func _sqlite3Fts5ParserFree(tls *libc.TLS, p uintptr, freeProc uintptr) { if p == uintptr(0) { return } _sqlite3Fts5ParserFinalize(tls, p) (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{freeProc})))(tls, p) } /* ** Return the peak depth of the stack for a parser. */ /* This array of booleans keeps track of the parser statement ** coverage. The element fts5yycoverage[X][Y] is set when the parser ** is in state X and has a lookahead token Y. In a well-tested ** systems, every element of this matrix should end up being set. */ /* ** Write into out a description of every state/lookahead combination that ** ** (1) has not been used by the parser, and ** (2) is not a syntax error. ** ** Return the number of missed state/lookahead combinations. */ // C documentation // // /* // ** Find the appropriate action for a parser given the terminal // ** look-ahead token iLookAhead. // */ func _fts5yy_find_shift_action(tls *libc.TLS, iLookAhead uint8, stateno uint8) (r uint8) { var i int32 _ = i if int32(stateno) > int32(fts5YY_MAX_SHIFT) { return stateno } _ = libc.Int32FromInt32(0) for cond := true; cond; cond = int32(1) != 0 { i = int32(_fts5yy_shift_ofst[stateno]) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) i += int32(iLookAhead) _ = libc.Int32FromInt32(0) if int32(_fts5yy_lookahead[i]) != int32(iLookAhead) { return _fts5yy_default[stateno] } else { _ = libc.Int32FromInt32(0) return _fts5yy_action[i] } } return r } // C documentation // // /* // ** Find the appropriate action for a parser given the non-terminal // ** look-ahead token iLookAhead. // */ func _fts5yy_find_reduce_action(tls *libc.TLS, stateno uint8, iLookAhead uint8) (r uint8) { var i int32 _ = i _ = libc.Int32FromInt32(0) i = int32(_fts5yy_reduce_ofst[stateno]) _ = libc.Int32FromInt32(0) i += int32(iLookAhead) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return _fts5yy_action[i] } // C documentation // // /* // ** The following routine is called if the stack overflows. // */ func _fts5yyStackOverflow(tls *libc.TLS, fts5yypParser uintptr) { var pParse uintptr _ = pParse pParse = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).FpParse for (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos > fts5yypParser+8 { _fts5yy_pop_parser_stack(tls, fts5yypParser) } /* Here code is inserted which will execute if the parser ** stack every overflows */ /******** Begin %stack_overflow code ******************************************/ _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+34722, 0) /******** End %stack_overflow code ********************************************/ (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).FpParse = pParse /* Suppress warning about unused %extra_argument var */ } /* ** Print tracing information for a SHIFT action */ // C documentation // // /* // ** Perform a shift action. // */ func _fts5yy_shift(tls *libc.TLS, fts5yypParser uintptr, fts5yyNewState uint8, fts5yyMajor uint8, fts5yyMinor TFts5Token) { var fts5yytos uintptr _ = fts5yytos (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos += 12 if (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos > (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yystackEnd { (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos -= 12 _fts5yyStackOverflow(tls, fts5yypParser) return } if int32(fts5yyNewState) > int32(fts5YY_MAX_SHIFT) { fts5yyNewState = uint8(int32(fts5yyNewState) + (libc.Int32FromInt32(fts5YY_MIN_REDUCE) - libc.Int32FromInt32(fts5YY_MIN_SHIFTREDUCE))) } fts5yytos = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos (*Tfts5yyStackEntry)(unsafe.Pointer(fts5yytos)).Fstateno = fts5yyNewState (*Tfts5yyStackEntry)(unsafe.Pointer(fts5yytos)).Fmajor = fts5yyMajor *(*TFts5Token)(unsafe.Pointer(fts5yytos + 4)) = fts5yyMinor } // C documentation // // /* For rule J, fts5yyRuleInfoLhs[J] contains the symbol on the left-hand side // ** of that rule */ var _fts5yyRuleInfoLhs = [28]uint8{ 0: uint8(16), 1: uint8(20), 2: uint8(20), 3: uint8(20), 4: uint8(20), 5: uint8(21), 6: uint8(21), 7: uint8(17), 8: uint8(17), 9: uint8(17), 10: uint8(17), 11: uint8(17), 12: uint8(17), 13: uint8(19), 14: uint8(19), 15: uint8(18), 16: uint8(18), 17: uint8(22), 18: uint8(22), 19: uint8(22), 20: uint8(23), 21: uint8(23), 22: uint8(25), 23: uint8(25), 24: uint8(24), 25: uint8(24), 26: uint8(26), 27: uint8(26), } // C documentation // // /* For rule J, fts5yyRuleInfoNRhs[J] contains the negative of the number // ** of symbols on the right-hand side of that rule. */ var _fts5yyRuleInfoNRhs = [28]int8{ 0: int8(-int32(1)), 1: int8(-int32(4)), 2: int8(-int32(3)), 3: int8(-int32(1)), 4: int8(-int32(2)), 5: int8(-int32(2)), 6: int8(-int32(1)), 7: int8(-int32(3)), 8: int8(-int32(3)), 9: int8(-int32(3)), 10: int8(-int32(5)), 11: int8(-int32(3)), 12: int8(-int32(1)), 13: int8(-int32(1)), 14: int8(-int32(2)), 15: int8(-int32(1)), 16: int8(-int32(3)), 17: int8(-int32(1)), 18: int8(-int32(2)), 19: int8(-int32(5)), 20: int8(-int32(1)), 21: int8(-int32(2)), 23: int8(-int32(2)), 24: int8(-int32(4)), 25: int8(-int32(2)), 26: int8(-int32(1)), } // C documentation // // /* // ** Perform a reduce action and the shift that must immediately // ** follow the reduce. // ** // ** The fts5yyLookahead and fts5yyLookaheadToken parameters provide reduce actions // ** access to the lookahead token (if any). The fts5yyLookahead will be fts5YYNOCODE // ** if the lookahead token has already been consumed. As this procedure is // ** only called from one place, optimizing compilers will in-line it, which // ** means that the extra parameters have no performance impact. // */ func _fts5yy_reduce(tls *libc.TLS, fts5yypParser uintptr, fts5yyruleno uint32, fts5yyLookahead int32, fts5yyLookaheadToken TFts5Token) (r uint8) { var fts5yyact uint8 var fts5yygoto, fts5yysize int32 var fts5yylhsminor Tfts5YYMINORTYPE var fts5yymsp, pParse uintptr _, _, _, _, _, _ = fts5yyact, fts5yygoto, fts5yylhsminor, fts5yymsp, fts5yysize, pParse /* Amount to pop the stack */ pParse = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).FpParse _ = fts5yyLookahead _ = fts5yyLookaheadToken fts5yymsp = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos switch fts5yyruleno { case uint32(0): goto _1 case uint32(1): goto _2 case uint32(2): goto _3 case uint32(3): goto _4 case uint32(4): goto _5 case uint32(5): goto _6 case uint32(6): goto _7 case uint32(7): goto _8 case uint32(8): goto _9 case uint32(9): goto _10 case uint32(10): goto _11 case uint32(11): goto _12 case uint32(13): goto _13 case uint32(12): goto _14 case uint32(14): goto _15 case uint32(15): goto _16 case uint32(16): goto _17 case uint32(17): goto _18 case uint32(18): goto _19 case uint32(19): goto _20 case uint32(20): goto _21 case uint32(21): goto _22 case uint32(22): goto _23 case uint32(23): goto _24 case uint32(24): goto _25 case uint32(25): goto _26 case uint32(26): goto _27 case uint32(27): goto _28 default: goto _29 } goto _30 _1: ; /* input ::= expr */ _sqlite3Fts5ParseFinished(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + 4))) goto _30 _2: ; /* colset ::= MINUS LCP colsetlist RCP */ *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3Fts5ParseColsetInvert(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) goto _30 _3: ; /* colset ::= LCP colsetlist RCP */ *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) goto _30 _4: ; /* colset ::= STRING */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseColset(tls, pParse, uintptr(0), fts5yymsp+4) *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _5: ; /* colset ::= MINUS STRING */ *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3Fts5ParseColset(tls, pParse, uintptr(0), fts5yymsp+4) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3Fts5ParseColsetInvert(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))) goto _30 _6: ; /* colsetlist ::= colsetlist STRING */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseColset(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), fts5yymsp+4) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _7: ; /* colsetlist ::= STRING */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseColset(tls, pParse, uintptr(0), fts5yymsp+4) *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _8: ; /* expr ::= expr AND expr */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNode(tls, pParse, int32(FTS5_AND), *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)), uintptr(0)) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _9: ; /* expr ::= expr OR expr */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNode(tls, pParse, int32(FTS5_OR), *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)), uintptr(0)) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _10: ; /* expr ::= expr NOT expr */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNode(tls, pParse, int32(FTS5_NOT), *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)), uintptr(0)) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _11: ; /* expr ::= colset COLON LP expr RP */ _sqlite3Fts5ParseSetColset(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4))) *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _12: ; /* expr ::= LP expr RP */ *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) goto _30 _14: ; /* expr ::= exprlist */ _13: ; *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)) *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _15: ; /* exprlist ::= exprlist cnearset */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseImplicitAnd(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4))) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _16: ; /* cnearset ::= nearset */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNode(tls, pParse, int32(FTS5_STRING), uintptr(0), uintptr(0), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4))) *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _17: ; /* cnearset ::= colset COLON nearset */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNode(tls, pParse, int32(FTS5_STRING), uintptr(0), uintptr(0), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4))) _sqlite3Fts5ParseSetColset(tls, pParse, *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)), *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _18: ; /* nearset ::= phrase */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNearset(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4))) *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _19: ; /* nearset ::= CARET phrase */ _sqlite3Fts5ParseSetCaret(tls, *(*uintptr)(unsafe.Pointer(fts5yymsp + 4))) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3Fts5ParseNearset(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4))) goto _30 _20: ; /* nearset ::= STRING LP nearphrases neardist_opt RP */ _sqlite3Fts5ParseNear(tls, pParse, fts5yymsp+uintptr(-libc.Int32FromInt32(4))*12+4) _sqlite3Fts5ParseSetDistance(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), fts5yymsp+uintptr(-libc.Int32FromInt32(1))*12+4) *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _21: ; /* nearphrases ::= phrase */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNearset(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4))) *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _22: ; /* nearphrases ::= nearphrases phrase */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNearset(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4))) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _23: ; /* neardist_opt ::= */ *(*uintptr)(unsafe.Pointer(fts5yymsp + 1*12 + 4)) = uintptr(0) *(*int32)(unsafe.Pointer(fts5yymsp + 1*12 + 4 + 4)) = 0 goto _30 _24: ; /* neardist_opt ::= COMMA STRING */ *(*TFts5Token)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*TFts5Token)(unsafe.Pointer(fts5yymsp + 4)) goto _30 _25: ; /* phrase ::= phrase PLUS STRING star_opt */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), fts5yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, *(*int32)(unsafe.Pointer(fts5yymsp + 4))) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _26: ; /* phrase ::= STRING star_opt */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseTerm(tls, pParse, uintptr(0), fts5yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, *(*int32)(unsafe.Pointer(fts5yymsp + 4))) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _27: ; /* star_opt ::= STAR */ *(*int32)(unsafe.Pointer(fts5yymsp + 4)) = int32(1) goto _30 _28: ; /* star_opt ::= */ *(*int32)(unsafe.Pointer(fts5yymsp + 1*12 + 4)) = 0 goto _30 _29: ; goto _30 /********** End reduce actions ************************************************/ _30: ; _ = libc.Int32FromInt32(0) fts5yygoto = int32(_fts5yyRuleInfoLhs[fts5yyruleno]) fts5yysize = int32(_fts5yyRuleInfoNRhs[fts5yyruleno]) fts5yyact = _fts5yy_find_reduce_action(tls, (*(*Tfts5yyStackEntry)(unsafe.Pointer(fts5yymsp + uintptr(fts5yysize)*12))).Fstateno, uint8(fts5yygoto)) /* There are no SHIFTREDUCE actions on nonterminals because the table ** generator has simplified them to pure REDUCE actions. */ _ = libc.Int32FromInt32(0) /* It is not possible for a REDUCE to be followed by an error */ _ = libc.Int32FromInt32(0) fts5yymsp += uintptr(fts5yysize+int32(1)) * 12 (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos = fts5yymsp (*Tfts5yyStackEntry)(unsafe.Pointer(fts5yymsp)).Fstateno = fts5yyact (*Tfts5yyStackEntry)(unsafe.Pointer(fts5yymsp)).Fmajor = uint8(fts5yygoto) return fts5yyact } /* ** The following code executes when the parse fails */ // C documentation // // /* // ** The following code executes when a syntax error first occurs. // */ func _fts5yy_syntax_error(tls *libc.TLS, fts5yypParser uintptr, fts5yymajor int32, fts5yyminor TFts5Token) { bp := tls.Alloc(32) defer tls.Free(32) var pParse uintptr _ = pParse pParse = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).FpParse /************ Begin %syntax_error code ****************************************/ _ = fts5yymajor /* Silence a compiler warning */ _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+34750, libc.VaList(bp+8, fts5yyminor.Fn, fts5yyminor.Fp)) /************ End %syntax_error code ******************************************/ (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).FpParse = pParse /* Suppress warning about unused %extra_argument variable */ } // C documentation // // /* // ** The following is executed when the parser accepts // */ func _fts5yy_accept(tls *libc.TLS, fts5yypParser uintptr) { var pParse uintptr _ = pParse pParse = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).FpParse _ = libc.Int32FromInt32(0) /* Here code is inserted which will be executed whenever the ** parser accepts */ /*********** Begin %parse_accept code *****************************************/ /*********** End %parse_accept code *******************************************/ (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).FpParse = pParse /* Suppress warning about unused %extra_argument variable */ } // C documentation // // /* The main parser program. // ** The first argument is a pointer to a structure obtained from // ** "sqlite3Fts5ParserAlloc" which describes the current state of the parser. // ** The second argument is the major token number. The third is // ** the minor token. The fourth optional argument is whatever the // ** user wants (and specified in the grammar) and is available for // ** use by the action routines. // ** // ** Inputs: // **
      // **
    • A pointer to the parser (an opaque structure.) // **
    • The major token number. // **
    • The minor token number. // **
    • An option argument of a grammar-specified type. // **
    // ** // ** Outputs: // ** None. // */ func _sqlite3Fts5Parser(tls *libc.TLS, fts5yyp uintptr, fts5yymajor int32, fts5yyminor TFts5Token, pParse uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var fts5yyact uint8 var fts5yypParser uintptr var fts5yyruleno uint32 var _ /* fts5yyminorunion at bp+0 */ Tfts5YYMINORTYPE _, _, _ = fts5yyact, fts5yypParser, fts5yyruleno /* The parser action. */ fts5yypParser = fts5yyp /* The parser */ (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).FpParse = pParse _ = libc.Int32FromInt32(0) fts5yyact = (*Tfts5yyStackEntry)(unsafe.Pointer((*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos)).Fstateno for int32(1) != 0 { /* Exit by "break" */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) fts5yyact = _fts5yy_find_shift_action(tls, uint8(fts5yymajor), fts5yyact) if int32(fts5yyact) >= int32(fts5YY_MIN_REDUCE) { fts5yyruleno = uint32(int32(fts5yyact) - int32(fts5YY_MIN_REDUCE)) /* Reduce by this rule */ /* Check that the stack is large enough to grow by a single entry ** if the RHS of the rule is empty. This ensures that there is room ** enough on the stack to push the LHS value */ if int32(_fts5yyRuleInfoNRhs[fts5yyruleno]) == 0 { if (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos >= (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yystackEnd { _fts5yyStackOverflow(tls, fts5yypParser) break } } fts5yyact = _fts5yy_reduce(tls, fts5yypParser, fts5yyruleno, fts5yymajor, fts5yyminor) } else { if int32(fts5yyact) <= int32(fts5YY_MAX_SHIFTREDUCE) { _fts5yy_shift(tls, fts5yypParser, fts5yyact, uint8(fts5yymajor), fts5yyminor) break } else { if int32(fts5yyact) == int32(fts5YY_ACCEPT_ACTION) { (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos -= 12 _fts5yy_accept(tls, fts5yypParser) return } else { _ = libc.Int32FromInt32(0) *(*TFts5Token)(unsafe.Pointer(bp)) = fts5yyminor /* If the fts5YYNOERRORRECOVERY macro is defined, then do not attempt to ** do any kind of error recovery. Instead, simply invoke the syntax ** error routine and continue going as if nothing had happened. ** ** Applications can set this macro (for example inside %include) if ** they intend to abandon the parse upon the first syntax error seen. */ _fts5yy_syntax_error(tls, fts5yypParser, fts5yymajor, fts5yyminor) _fts5yy_destructor(tls, fts5yypParser, uint8(fts5yymajor), bp) break } } } } return } // C documentation // // /* // ** Return the fallback token corresponding to canonical token iToken, or // ** 0 if iToken has no fallback. // */ func _sqlite3Fts5ParserFallback(tls *libc.TLS, iToken int32) (r int32) { _ = iToken return 0 } /* ** 2014 May 31 ** ** 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. ** ****************************************************************************** */ /* #include "fts5Int.h" */ /* Declarations for math functions. Copyright (C) 1991-2022 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, see . */ /* * ISO C99 Standard: 7.12 Mathematics */ // C documentation // // /* // ** Object used to iterate through all "coalesced phrase instances" in // ** a single column of the current row. If the phrase instances in the // ** column being considered do not overlap, this object simply iterates // ** through them. Or, if they do overlap (share one or more tokens in // ** common), each set of overlapping instances is treated as a single // ** match. See documentation for the highlight() auxiliary function for // ** details. // ** // ** Usage is: // ** // ** for(rc = fts5CInstIterNext(pApi, pFts, iCol, &iter); // ** (rc==SQLITE_OK && 0==fts5CInstIterEof(&iter); // ** rc = fts5CInstIterNext(&iter) // ** ){ // ** printf("instance starts at %d, ends at %d\n", iter.iStart, iter.iEnd); // ** } // ** // */ type TCInstIter = struct { FpApi uintptr FpFts uintptr FiCol int32 FiInst int32 FnInst int32 FiStart int32 FiEnd int32 } type CInstIter = TCInstIter type TCInstIter1 = struct { FpApi uintptr FpFts uintptr FiCol int32 FiInst int32 FnInst int32 FiStart int32 FiEnd int32 } type CInstIter1 = TCInstIter1 // C documentation // // /* // ** Advance the iterator to the next coalesced phrase instance. Return // ** an SQLite error code if an error occurs, or SQLITE_OK otherwise. // */ func _fts5CInstIterNext(tls *libc.TLS, pIter uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iEnd, rc int32 var _ /* ic at bp+4 */ int32 var _ /* io at bp+8 */ int32 var _ /* ip at bp+0 */ int32 _, _ = iEnd, rc rc = SQLITE_OK (*TCInstIter)(unsafe.Pointer(pIter)).FiStart = -int32(1) (*TCInstIter)(unsafe.Pointer(pIter)).FiEnd = -int32(1) for rc == SQLITE_OK && (*TCInstIter)(unsafe.Pointer(pIter)).FiInst < (*TCInstIter)(unsafe.Pointer(pIter)).FnInst { rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer((*TCInstIter)(unsafe.Pointer(pIter)).FpApi)).FxInst})))(tls, (*TCInstIter)(unsafe.Pointer(pIter)).FpFts, (*TCInstIter)(unsafe.Pointer(pIter)).FiInst, bp, bp+4, bp+8) if rc == SQLITE_OK { if *(*int32)(unsafe.Pointer(bp + 4)) == (*TCInstIter)(unsafe.Pointer(pIter)).FiCol { iEnd = *(*int32)(unsafe.Pointer(bp + 8)) - int32(1) + (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer((*TCInstIter)(unsafe.Pointer(pIter)).FpApi)).FxPhraseSize})))(tls, (*TCInstIter)(unsafe.Pointer(pIter)).FpFts, *(*int32)(unsafe.Pointer(bp))) if (*TCInstIter)(unsafe.Pointer(pIter)).FiStart < 0 { (*TCInstIter)(unsafe.Pointer(pIter)).FiStart = *(*int32)(unsafe.Pointer(bp + 8)) (*TCInstIter)(unsafe.Pointer(pIter)).FiEnd = iEnd } else { if *(*int32)(unsafe.Pointer(bp + 8)) <= (*TCInstIter)(unsafe.Pointer(pIter)).FiEnd { if iEnd > (*TCInstIter)(unsafe.Pointer(pIter)).FiEnd { (*TCInstIter)(unsafe.Pointer(pIter)).FiEnd = iEnd } } else { break } } } (*TCInstIter)(unsafe.Pointer(pIter)).FiInst++ } } return rc } // C documentation // // /* // ** Initialize the iterator object indicated by the final parameter to // ** iterate through coalesced phrase instances in column iCol. // */ func _fts5CInstIterInit(tls *libc.TLS, pApi uintptr, pFts uintptr, iCol int32, pIter uintptr) (r int32) { var rc int32 _ = rc libc.Xmemset(tls, pIter, 0, uint32(28)) (*TCInstIter)(unsafe.Pointer(pIter)).FpApi = pApi (*TCInstIter)(unsafe.Pointer(pIter)).FpFts = pFts (*TCInstIter)(unsafe.Pointer(pIter)).FiCol = iCol rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInstCount})))(tls, pFts, pIter+16) if rc == SQLITE_OK { rc = _fts5CInstIterNext(tls, pIter) } return rc } // C documentation // // /************************************************************************* // ** Start of highlight() implementation. // */ type THighlightContext = struct { FiRangeStart int32 FiRangeEnd int32 FzOpen uintptr FzClose uintptr FzIn uintptr FnIn int32 Fiter TCInstIter FiPos int32 FiOff int32 FbOpen int32 FzOut uintptr } type HighlightContext = THighlightContext type THighlightContext1 = struct { FiRangeStart int32 FiRangeEnd int32 FzOpen uintptr FzClose uintptr FzIn uintptr FnIn int32 Fiter TCInstIter FiPos int32 FiOff int32 FbOpen int32 FzOut uintptr } type HighlightContext1 = THighlightContext1 // C documentation // // /* // ** Append text to the HighlightContext output string - p->zOut. Argument // ** z points to a buffer containing n bytes of text to append. If n is // ** negative, everything up until the first '\0' is appended to the output. // ** // ** If *pRc is set to any value other than SQLITE_OK when this function is // ** called, it is a no-op. If an error (i.e. an OOM condition) is encountered, // ** *pRc is set to an error code before returning. // */ func _fts5HighlightAppend(tls *libc.TLS, pRc uintptr, p uintptr, z uintptr, n int32) { bp := tls.Alloc(32) defer tls.Free(32) if *(*int32)(unsafe.Pointer(pRc)) == SQLITE_OK && z != 0 { if n < 0 { n = int32(libc.Xstrlen(tls, z)) } (*THighlightContext)(unsafe.Pointer(p)).FzOut = Xsqlite3_mprintf(tls, __ccgo_ts+34781, libc.VaList(bp+8, (*THighlightContext)(unsafe.Pointer(p)).FzOut, n, z)) if (*THighlightContext)(unsafe.Pointer(p)).FzOut == uintptr(0) { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) } } } // C documentation // // /* // ** Tokenizer callback used by implementation of highlight() function. // */ func _fts5HighlightCb(tls *libc.TLS, pContext uintptr, tflags int32, pToken uintptr, nToken int32, iStartOff int32, iEndOff int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iPos, v1 int32 var p, v2 uintptr var _ /* rc at bp+0 */ int32 _, _, _, _ = iPos, p, v1, v2 p = pContext *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK _ = pToken _ = nToken if tflags&int32(FTS5_TOKEN_COLOCATED) != 0 { return SQLITE_OK } v2 = p + 52 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ iPos = v1 if (*THighlightContext)(unsafe.Pointer(p)).FiRangeEnd >= 0 { if iPos < (*THighlightContext)(unsafe.Pointer(p)).FiRangeStart || iPos > (*THighlightContext)(unsafe.Pointer(p)).FiRangeEnd { return SQLITE_OK } if (*THighlightContext)(unsafe.Pointer(p)).FiRangeStart != 0 && iPos == (*THighlightContext)(unsafe.Pointer(p)).FiRangeStart { (*THighlightContext)(unsafe.Pointer(p)).FiOff = iStartOff } } /* If the parenthesis is open, and this token is not part of the current ** phrase, and the starting byte offset of this token is past the point ** that has currently been copied into the output buffer, close the ** parenthesis. */ if (*THighlightContext)(unsafe.Pointer(p)).FbOpen != 0 && (iPos <= (*THighlightContext)(unsafe.Pointer(p)).Fiter.FiStart || (*THighlightContext)(unsafe.Pointer(p)).Fiter.FiStart < 0) && iStartOff > (*THighlightContext)(unsafe.Pointer(p)).FiOff { _fts5HighlightAppend(tls, bp, p, (*THighlightContext)(unsafe.Pointer(p)).FzClose, -int32(1)) (*THighlightContext)(unsafe.Pointer(p)).FbOpen = 0 } /* If this is the start of a new phrase, and the highlight is not open: ** ** * copy text from the input up to the start of the phrase, and ** * open the highlight. */ if iPos == (*THighlightContext)(unsafe.Pointer(p)).Fiter.FiStart && (*THighlightContext)(unsafe.Pointer(p)).FbOpen == 0 { _fts5HighlightAppend(tls, bp, p, (*THighlightContext)(unsafe.Pointer(p)).FzIn+uintptr((*THighlightContext)(unsafe.Pointer(p)).FiOff), iStartOff-(*THighlightContext)(unsafe.Pointer(p)).FiOff) _fts5HighlightAppend(tls, bp, p, (*THighlightContext)(unsafe.Pointer(p)).FzOpen, -int32(1)) (*THighlightContext)(unsafe.Pointer(p)).FiOff = iStartOff (*THighlightContext)(unsafe.Pointer(p)).FbOpen = int32(1) } if iPos == (*THighlightContext)(unsafe.Pointer(p)).Fiter.FiEnd { if (*THighlightContext)(unsafe.Pointer(p)).FbOpen == 0 { _ = libc.Int32FromInt32(0) _fts5HighlightAppend(tls, bp, p, (*THighlightContext)(unsafe.Pointer(p)).FzOpen, -int32(1)) (*THighlightContext)(unsafe.Pointer(p)).FbOpen = int32(1) } _fts5HighlightAppend(tls, bp, p, (*THighlightContext)(unsafe.Pointer(p)).FzIn+uintptr((*THighlightContext)(unsafe.Pointer(p)).FiOff), iEndOff-(*THighlightContext)(unsafe.Pointer(p)).FiOff) (*THighlightContext)(unsafe.Pointer(p)).FiOff = iEndOff if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _fts5CInstIterNext(tls, p+24) } } if iPos == (*THighlightContext)(unsafe.Pointer(p)).FiRangeEnd { if (*THighlightContext)(unsafe.Pointer(p)).FbOpen != 0 { if (*THighlightContext)(unsafe.Pointer(p)).Fiter.FiStart >= 0 && iPos >= (*THighlightContext)(unsafe.Pointer(p)).Fiter.FiStart { _fts5HighlightAppend(tls, bp, p, (*THighlightContext)(unsafe.Pointer(p)).FzIn+uintptr((*THighlightContext)(unsafe.Pointer(p)).FiOff), iEndOff-(*THighlightContext)(unsafe.Pointer(p)).FiOff) (*THighlightContext)(unsafe.Pointer(p)).FiOff = iEndOff } _fts5HighlightAppend(tls, bp, p, (*THighlightContext)(unsafe.Pointer(p)).FzClose, -int32(1)) (*THighlightContext)(unsafe.Pointer(p)).FbOpen = 0 } _fts5HighlightAppend(tls, bp, p, (*THighlightContext)(unsafe.Pointer(p)).FzIn+uintptr((*THighlightContext)(unsafe.Pointer(p)).FiOff), iEndOff-(*THighlightContext)(unsafe.Pointer(p)).FiOff) (*THighlightContext)(unsafe.Pointer(p)).FiOff = iEndOff } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Implementation of highlight() function. // */ func _fts5HighlightFunction(tls *libc.TLS, pApi uintptr, pFts uintptr, pCtx uintptr, nVal int32, apVal uintptr) { bp := tls.Alloc(80) defer tls.Free(80) var iCol int32 var zErr uintptr var _ /* ctx at bp+0 */ THighlightContext var _ /* rc at bp+68 */ int32 _, _ = iCol, zErr if nVal != int32(3) { zErr = __ccgo_ts + 34788 Xsqlite3_result_error(tls, pCtx, zErr, -int32(1)) return } iCol = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(apVal))) libc.Xmemset(tls, bp, 0, uint32(68)) (*(*THighlightContext)(unsafe.Pointer(bp))).FzOpen = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(apVal + 1*4))) (*(*THighlightContext)(unsafe.Pointer(bp))).FzClose = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(apVal + 2*4))) (*(*THighlightContext)(unsafe.Pointer(bp))).FiRangeEnd = -int32(1) *(*int32)(unsafe.Pointer(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnText})))(tls, pFts, iCol, bp+16, bp+20) if *(*int32)(unsafe.Pointer(bp + 68)) == int32(SQLITE_RANGE) { Xsqlite3_result_text(tls, pCtx, __ccgo_ts+1648, -int32(1), libc.UintptrFromInt32(0)) *(*int32)(unsafe.Pointer(bp + 68)) = SQLITE_OK } else { if (*(*THighlightContext)(unsafe.Pointer(bp))).FzIn != 0 { if *(*int32)(unsafe.Pointer(bp + 68)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 68)) = _fts5CInstIterInit(tls, pApi, pFts, iCol, bp+24) } if *(*int32)(unsafe.Pointer(bp + 68)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 68)) = (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxTokenize})))(tls, pFts, (*(*THighlightContext)(unsafe.Pointer(bp))).FzIn, (*(*THighlightContext)(unsafe.Pointer(bp))).FnIn, bp, __ccgo_fp(_fts5HighlightCb)) } if (*(*THighlightContext)(unsafe.Pointer(bp))).FbOpen != 0 { _fts5HighlightAppend(tls, bp+68, bp, (*(*THighlightContext)(unsafe.Pointer(bp))).FzClose, -int32(1)) } _fts5HighlightAppend(tls, bp+68, bp, (*(*THighlightContext)(unsafe.Pointer(bp))).FzIn+uintptr((*(*THighlightContext)(unsafe.Pointer(bp))).FiOff), (*(*THighlightContext)(unsafe.Pointer(bp))).FnIn-(*(*THighlightContext)(unsafe.Pointer(bp))).FiOff) if *(*int32)(unsafe.Pointer(bp + 68)) == SQLITE_OK { Xsqlite3_result_text(tls, pCtx, (*(*THighlightContext)(unsafe.Pointer(bp))).FzOut, -int32(1), uintptr(-libc.Int32FromInt32(1))) } Xsqlite3_free(tls, (*(*THighlightContext)(unsafe.Pointer(bp))).FzOut) } } if *(*int32)(unsafe.Pointer(bp + 68)) != SQLITE_OK { Xsqlite3_result_error_code(tls, pCtx, *(*int32)(unsafe.Pointer(bp + 68))) } } /* ** End of highlight() implementation. **************************************************************************/ // C documentation // // /* // ** Context object passed to the fts5SentenceFinderCb() function. // */ type TFts5SFinder = struct { FiPos int32 FnFirstAlloc int32 FnFirst int32 FaFirst uintptr FzDoc uintptr } type Fts5SFinder = TFts5SFinder type TFts5SFinder1 = struct { FiPos int32 FnFirstAlloc int32 FnFirst int32 FaFirst uintptr FzDoc uintptr } type Fts5SFinder1 = TFts5SFinder1 // C documentation // // /* // ** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if // ** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an // ** error occurs. // */ func _fts5SentenceFinderAdd(tls *libc.TLS, p uintptr, iAdd int32) (r int32) { var aNew, v3 uintptr var nNew, v1, v2 int32 _, _, _, _, _ = aNew, nNew, v1, v2, v3 if (*TFts5SFinder)(unsafe.Pointer(p)).FnFirstAlloc == (*TFts5SFinder)(unsafe.Pointer(p)).FnFirst { if (*TFts5SFinder)(unsafe.Pointer(p)).FnFirstAlloc != 0 { v1 = (*TFts5SFinder)(unsafe.Pointer(p)).FnFirstAlloc * int32(2) } else { v1 = int32(64) } nNew = v1 aNew = Xsqlite3_realloc64(tls, (*TFts5SFinder)(unsafe.Pointer(p)).FaFirst, uint64(uint32(nNew)*uint32(4))) if aNew == uintptr(0) { return int32(SQLITE_NOMEM) } (*TFts5SFinder)(unsafe.Pointer(p)).FaFirst = aNew (*TFts5SFinder)(unsafe.Pointer(p)).FnFirstAlloc = nNew } v3 = p + 8 v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ *(*int32)(unsafe.Pointer((*TFts5SFinder)(unsafe.Pointer(p)).FaFirst + uintptr(v2)*4)) = iAdd return SQLITE_OK } // C documentation // // /* // ** This function is an xTokenize() callback used by the auxiliary snippet() // ** function. Its job is to identify tokens that are the first in a sentence. // ** For each such token, an entry is added to the SFinder.aFirst[] array. // */ func _fts5SentenceFinderCb(tls *libc.TLS, pContext uintptr, tflags int32, pToken uintptr, nToken int32, iStartOff int32, iEndOff int32) (r int32) { var c uint8 var i, rc int32 var p uintptr _, _, _, _ = c, i, p, rc rc = SQLITE_OK _ = pToken _ = nToken _ = iEndOff if tflags&int32(FTS5_TOKEN_COLOCATED) == 0 { p = pContext if (*TFts5SFinder)(unsafe.Pointer(p)).FiPos > 0 { c = uint8(0) i = iStartOff - int32(1) for { if !(i >= 0) { break } c = *(*uint8)(unsafe.Pointer((*TFts5SFinder)(unsafe.Pointer(p)).FzDoc + uintptr(i))) if int32(c) != int32(' ') && int32(c) != int32('\t') && int32(c) != int32('\n') && int32(c) != int32('\r') { break } goto _1 _1: ; i-- } if i != iStartOff-int32(1) && (int32(c) == int32('.') || int32(c) == int32(':')) { rc = _fts5SentenceFinderAdd(tls, p, (*TFts5SFinder)(unsafe.Pointer(p)).FiPos) } } else { rc = _fts5SentenceFinderAdd(tls, p, 0) } (*TFts5SFinder)(unsafe.Pointer(p)).FiPos++ } return rc } func _fts5SnippetScore(tls *libc.TLS, pApi uintptr, pFts uintptr, nDocsize int32, aSeen uintptr, iCol int32, iPos int32, nToken int32, pnScore uintptr, piPos uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, iFirst, iLast, nScore, rc, v2 int32 var iAdj, iEnd Tsqlite3_int64 var _ /* iOff at bp+8 */ int32 var _ /* ic at bp+4 */ int32 var _ /* ip at bp+0 */ int32 var _ /* nInst at bp+12 */ int32 _, _, _, _, _, _, _, _ = i, iAdj, iEnd, iFirst, iLast, nScore, rc, v2 *(*int32)(unsafe.Pointer(bp)) = 0 *(*int32)(unsafe.Pointer(bp + 4)) = 0 *(*int32)(unsafe.Pointer(bp + 8)) = 0 iFirst = -int32(1) nScore = 0 iLast = 0 iEnd = int64(iPos) + int64(nToken) rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInstCount})))(tls, pFts, bp+12) i = 0 for { if !(i < *(*int32)(unsafe.Pointer(bp + 12)) && rc == SQLITE_OK) { break } rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInst})))(tls, pFts, i, bp, bp+4, bp+8) if rc == SQLITE_OK && *(*int32)(unsafe.Pointer(bp + 4)) == iCol && *(*int32)(unsafe.Pointer(bp + 8)) >= iPos && int64(*(*int32)(unsafe.Pointer(bp + 8))) < iEnd { if *(*uint8)(unsafe.Pointer(aSeen + uintptr(*(*int32)(unsafe.Pointer(bp))))) != 0 { v2 = int32(1) } else { v2 = int32(1000) } nScore += v2 *(*uint8)(unsafe.Pointer(aSeen + uintptr(*(*int32)(unsafe.Pointer(bp))))) = uint8(1) if iFirst < 0 { iFirst = *(*int32)(unsafe.Pointer(bp + 8)) } iLast = *(*int32)(unsafe.Pointer(bp + 8)) + (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxPhraseSize})))(tls, pFts, *(*int32)(unsafe.Pointer(bp))) } goto _1 _1: ; i++ } *(*int32)(unsafe.Pointer(pnScore)) = nScore if piPos != 0 { iAdj = int64(iFirst - (nToken-(iLast-iFirst))/int32(2)) if iAdj+int64(nToken) > int64(nDocsize) { iAdj = int64(nDocsize - nToken) } if iAdj < 0 { iAdj = 0 } *(*int32)(unsafe.Pointer(piPos)) = int32(iAdj) } return rc } // C documentation // // /* // ** Return the value in pVal interpreted as utf-8 text. Except, if pVal // ** contains a NULL value, return a pointer to a static string zero // ** bytes in length instead of a NULL pointer. // */ func _fts5ValueToText(tls *libc.TLS, pVal uintptr) (r uintptr) { var zRet, v1 uintptr _, _ = zRet, v1 zRet = Xsqlite3_value_text(tls, pVal) if zRet != 0 { v1 = zRet } else { v1 = __ccgo_ts + 1648 } return v1 } // C documentation // // /* // ** Implementation of snippet() function. // */ func _fts5SnippetFunction(tls *libc.TLS, pApi uintptr, pFts uintptr, pCtx uintptr, nVal int32, apVal uintptr) { bp := tls.Alloc(128) defer tls.Free(128) var aSeen, zEllips, zErr uintptr var i, iBestCol, iBestStart, iCol, ii, jj, nBestScore, nCol, nPhrase, nToken, v1, v5 int32 var _ /* ctx at bp+0 */ THighlightContext var _ /* iAdj at bp+120 */ int32 var _ /* ic at bp+112 */ int32 var _ /* io at bp+116 */ int32 var _ /* ip at bp+108 */ int32 var _ /* nColSize at bp+76 */ int32 var _ /* nDoc at bp+100 */ int32 var _ /* nDocsize at bp+104 */ int32 var _ /* nInst at bp+72 */ int32 var _ /* nScore at bp+124 */ int32 var _ /* rc at bp+68 */ int32 var _ /* sFinder at bp+80 */ TFts5SFinder _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aSeen, i, iBestCol, iBestStart, iCol, ii, jj, nBestScore, nCol, nPhrase, nToken, zEllips, zErr, v1, v5 *(*int32)(unsafe.Pointer(bp + 68)) = SQLITE_OK /* 5th argument to snippet() */ *(*int32)(unsafe.Pointer(bp + 72)) = 0 /* Column containing best snippet */ iBestStart = 0 /* First token of best snippet */ nBestScore = 0 /* Score of best snippet */ *(*int32)(unsafe.Pointer(bp + 76)) = 0 if nVal != int32(5) { zErr = __ccgo_ts + 34838 Xsqlite3_result_error(tls, pCtx, zErr, -int32(1)) return } nCol = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnCount})))(tls, pFts) libc.Xmemset(tls, bp, 0, uint32(68)) iCol = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(apVal))) (*(*THighlightContext)(unsafe.Pointer(bp))).FzOpen = _fts5ValueToText(tls, *(*uintptr)(unsafe.Pointer(apVal + 1*4))) (*(*THighlightContext)(unsafe.Pointer(bp))).FzClose = _fts5ValueToText(tls, *(*uintptr)(unsafe.Pointer(apVal + 2*4))) (*(*THighlightContext)(unsafe.Pointer(bp))).FiRangeEnd = -int32(1) zEllips = _fts5ValueToText(tls, *(*uintptr)(unsafe.Pointer(apVal + 3*4))) nToken = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(apVal + 4*4))) if iCol >= 0 { v1 = iCol } else { v1 = 0 } iBestCol = v1 nPhrase = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxPhraseCount})))(tls, pFts) aSeen = Xsqlite3_malloc(tls, nPhrase) if aSeen == uintptr(0) { *(*int32)(unsafe.Pointer(bp + 68)) = int32(SQLITE_NOMEM) } if *(*int32)(unsafe.Pointer(bp + 68)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 68)) = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInstCount})))(tls, pFts, bp+72) } libc.Xmemset(tls, bp+80, 0, uint32(20)) i = 0 for { if !(i < nCol) { break } if iCol < 0 || iCol == i { (*(*TFts5SFinder)(unsafe.Pointer(bp + 80))).FiPos = 0 (*(*TFts5SFinder)(unsafe.Pointer(bp + 80))).FnFirst = 0 *(*int32)(unsafe.Pointer(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnText})))(tls, pFts, i, bp+80+16, bp+100) if *(*int32)(unsafe.Pointer(bp + 68)) != SQLITE_OK { break } *(*int32)(unsafe.Pointer(bp + 68)) = (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxTokenize})))(tls, pFts, (*(*TFts5SFinder)(unsafe.Pointer(bp + 80))).FzDoc, *(*int32)(unsafe.Pointer(bp + 100)), bp+80, __ccgo_fp(_fts5SentenceFinderCb)) if *(*int32)(unsafe.Pointer(bp + 68)) != SQLITE_OK { break } *(*int32)(unsafe.Pointer(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnSize})))(tls, pFts, i, bp+104) if *(*int32)(unsafe.Pointer(bp + 68)) != SQLITE_OK { break } ii = 0 for { if !(*(*int32)(unsafe.Pointer(bp + 68)) == SQLITE_OK && ii < *(*int32)(unsafe.Pointer(bp + 72))) { break } *(*int32)(unsafe.Pointer(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInst})))(tls, pFts, ii, bp+108, bp+112, bp+116) if *(*int32)(unsafe.Pointer(bp + 112)) != i { goto _3 } if *(*int32)(unsafe.Pointer(bp + 116)) > *(*int32)(unsafe.Pointer(bp + 104)) { *(*int32)(unsafe.Pointer(bp + 68)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< nBestScore { nBestScore = *(*int32)(unsafe.Pointer(bp + 124)) iBestCol = i iBestStart = *(*int32)(unsafe.Pointer(bp + 120)) *(*int32)(unsafe.Pointer(bp + 76)) = *(*int32)(unsafe.Pointer(bp + 104)) } if *(*int32)(unsafe.Pointer(bp + 68)) == SQLITE_OK && (*(*TFts5SFinder)(unsafe.Pointer(bp + 80))).FnFirst != 0 && *(*int32)(unsafe.Pointer(bp + 104)) > nToken { jj = 0 for { if !(jj < (*(*TFts5SFinder)(unsafe.Pointer(bp + 80))).FnFirst-int32(1)) { break } if *(*int32)(unsafe.Pointer((*(*TFts5SFinder)(unsafe.Pointer(bp + 80))).FaFirst + uintptr(jj+int32(1))*4)) > *(*int32)(unsafe.Pointer(bp + 116)) { break } goto _4 _4: ; jj++ } if *(*int32)(unsafe.Pointer((*(*TFts5SFinder)(unsafe.Pointer(bp + 80))).FaFirst + uintptr(jj)*4)) < *(*int32)(unsafe.Pointer(bp + 116)) { libc.Xmemset(tls, aSeen, 0, uint32(nPhrase)) *(*int32)(unsafe.Pointer(bp + 68)) = _fts5SnippetScore(tls, pApi, pFts, *(*int32)(unsafe.Pointer(bp + 104)), aSeen, i, *(*int32)(unsafe.Pointer((*(*TFts5SFinder)(unsafe.Pointer(bp + 80))).FaFirst + uintptr(jj)*4)), nToken, bp+124, uintptr(0)) if *(*int32)(unsafe.Pointer((*(*TFts5SFinder)(unsafe.Pointer(bp + 80))).FaFirst + uintptr(jj)*4)) == 0 { v5 = int32(120) } else { v5 = int32(100) } *(*int32)(unsafe.Pointer(bp + 124)) += v5 if *(*int32)(unsafe.Pointer(bp + 68)) == SQLITE_OK && *(*int32)(unsafe.Pointer(bp + 124)) > nBestScore { nBestScore = *(*int32)(unsafe.Pointer(bp + 124)) iBestCol = i iBestStart = *(*int32)(unsafe.Pointer((*(*TFts5SFinder)(unsafe.Pointer(bp + 80))).FaFirst + uintptr(jj)*4)) *(*int32)(unsafe.Pointer(bp + 76)) = *(*int32)(unsafe.Pointer(bp + 104)) } } } goto _3 _3: ; ii++ } } goto _2 _2: ; i++ } if *(*int32)(unsafe.Pointer(bp + 68)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnText})))(tls, pFts, iBestCol, bp+16, bp+20) } if *(*int32)(unsafe.Pointer(bp + 68)) == SQLITE_OK && *(*int32)(unsafe.Pointer(bp + 76)) == 0 { *(*int32)(unsafe.Pointer(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnSize})))(tls, pFts, iBestCol, bp+76) } if (*(*THighlightContext)(unsafe.Pointer(bp))).FzIn != 0 { if *(*int32)(unsafe.Pointer(bp + 68)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 68)) = _fts5CInstIterInit(tls, pApi, pFts, iBestCol, bp+24) } (*(*THighlightContext)(unsafe.Pointer(bp))).FiRangeStart = iBestStart (*(*THighlightContext)(unsafe.Pointer(bp))).FiRangeEnd = iBestStart + nToken - int32(1) if iBestStart > 0 { _fts5HighlightAppend(tls, bp+68, bp, zEllips, -int32(1)) } /* Advance iterator ctx.iter so that it points to the first coalesced ** phrase instance at or following position iBestStart. */ for (*(*THighlightContext)(unsafe.Pointer(bp))).Fiter.FiStart >= 0 && (*(*THighlightContext)(unsafe.Pointer(bp))).Fiter.FiStart < iBestStart && *(*int32)(unsafe.Pointer(bp + 68)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 68)) = _fts5CInstIterNext(tls, bp+24) } if *(*int32)(unsafe.Pointer(bp + 68)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 68)) = (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxTokenize})))(tls, pFts, (*(*THighlightContext)(unsafe.Pointer(bp))).FzIn, (*(*THighlightContext)(unsafe.Pointer(bp))).FnIn, bp, __ccgo_fp(_fts5HighlightCb)) } if (*(*THighlightContext)(unsafe.Pointer(bp))).FbOpen != 0 { _fts5HighlightAppend(tls, bp+68, bp, (*(*THighlightContext)(unsafe.Pointer(bp))).FzClose, -int32(1)) } if (*(*THighlightContext)(unsafe.Pointer(bp))).FiRangeEnd >= *(*int32)(unsafe.Pointer(bp + 76))-int32(1) { _fts5HighlightAppend(tls, bp+68, bp, (*(*THighlightContext)(unsafe.Pointer(bp))).FzIn+uintptr((*(*THighlightContext)(unsafe.Pointer(bp))).FiOff), (*(*THighlightContext)(unsafe.Pointer(bp))).FnIn-(*(*THighlightContext)(unsafe.Pointer(bp))).FiOff) } else { _fts5HighlightAppend(tls, bp+68, bp, zEllips, -int32(1)) } } if *(*int32)(unsafe.Pointer(bp + 68)) == SQLITE_OK { Xsqlite3_result_text(tls, pCtx, (*(*THighlightContext)(unsafe.Pointer(bp))).FzOut, -int32(1), uintptr(-libc.Int32FromInt32(1))) } else { Xsqlite3_result_error_code(tls, pCtx, *(*int32)(unsafe.Pointer(bp + 68))) } Xsqlite3_free(tls, (*(*THighlightContext)(unsafe.Pointer(bp))).FzOut) Xsqlite3_free(tls, aSeen) Xsqlite3_free(tls, (*(*TFts5SFinder)(unsafe.Pointer(bp + 80))).FaFirst) } /************************************************************************/ // C documentation // // /* // ** The first time the bm25() function is called for a query, an instance // ** of the following structure is allocated and populated. // */ type TFts5Bm25Data = struct { F__ccgo_align [0]uint32 FnPhrase int32 F__ccgo_align1 [4]byte Favgdl float64 FaIDF uintptr FaFreq uintptr } type Fts5Bm25Data = TFts5Bm25Data type TFts5Bm25Data1 = struct { F__ccgo_align [0]uint32 FnPhrase int32 F__ccgo_align1 [4]byte Favgdl float64 FaIDF uintptr FaFreq uintptr } type Fts5Bm25Data1 = TFts5Bm25Data1 // C documentation // // /* // ** Callback used by fts5Bm25GetData() to count the number of rows in the // ** table matched by each individual phrase within the query. // */ func _fts5CountCb(tls *libc.TLS, pApi uintptr, pFts uintptr, pUserData uintptr) (r int32) { var pn uintptr _ = pn pn = pUserData _ = pApi _ = pFts *(*Tsqlite3_int64)(unsafe.Pointer(pn))++ return SQLITE_OK } // C documentation // // /* // ** Set *ppData to point to the Fts5Bm25Data object for the current query. // ** If the object has not already been allocated, allocate and populate it // ** now. // */ func _fts5Bm25GetData(tls *libc.TLS, pApi uintptr, pFts uintptr, ppData uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var i, nPhrase, rc int32 var idf float64 var nByte Tsqlite3_int64 var p uintptr var _ /* nHit at bp+16 */ Tsqlite3_int64 var _ /* nRow at bp+0 */ Tsqlite3_int64 var _ /* nToken at bp+8 */ Tsqlite3_int64 _, _, _, _, _, _ = i, idf, nByte, nPhrase, p, rc rc = SQLITE_OK /* Object to return */ p = (*(*func(*libc.TLS, uintptr, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxGetAuxdata})))(tls, pFts, 0) if p == uintptr(0) { /* Number of phrases in query */ *(*Tsqlite3_int64)(unsafe.Pointer(bp)) = 0 /* Number of rows in table */ *(*Tsqlite3_int64)(unsafe.Pointer(bp + 8)) = 0 /* Allocate the Fts5Bm25Data object */ nPhrase = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxPhraseCount})))(tls, pFts) nByte = int64(uint32(24) + uint32(nPhrase*int32(2))*uint32(8)) p = Xsqlite3_malloc64(tls, uint64(nByte)) if p == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, p, 0, uint32(nByte)) (*TFts5Bm25Data)(unsafe.Pointer(p)).FnPhrase = nPhrase (*TFts5Bm25Data)(unsafe.Pointer(p)).FaIDF = p + 1*24 (*TFts5Bm25Data)(unsafe.Pointer(p)).FaFreq = (*TFts5Bm25Data)(unsafe.Pointer(p)).FaIDF + uintptr(nPhrase)*8 } /* Calculate the average document length for this FTS5 table */ if rc == SQLITE_OK { rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxRowCount})))(tls, pFts, bp) } _ = libc.Int32FromInt32(0) if rc == SQLITE_OK { rc = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnTotalSize})))(tls, pFts, -int32(1), bp+8) } if rc == SQLITE_OK { (*TFts5Bm25Data)(unsafe.Pointer(p)).Favgdl = float64(*(*Tsqlite3_int64)(unsafe.Pointer(bp + 8))) / float64(*(*Tsqlite3_int64)(unsafe.Pointer(bp))) } /* Calculate an IDF for each phrase in the query */ i = 0 for { if !(rc == SQLITE_OK && i < nPhrase) { break } *(*Tsqlite3_int64)(unsafe.Pointer(bp + 16)) = 0 rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxQueryPhrase})))(tls, pFts, i, bp+16, __ccgo_fp(_fts5CountCb)) if rc == SQLITE_OK { /* Calculate the IDF (Inverse Document Frequency) for phrase i. ** This is done using the standard BM25 formula as found on wikipedia: ** ** IDF = log( (N - nHit + 0.5) / (nHit + 0.5) ) ** ** where "N" is the total number of documents in the set and nHit ** is the number that contain at least one instance of the phrase ** under consideration. ** ** The problem with this is that if (N < 2*nHit), the IDF is ** negative. Which is undesirable. So the mimimum allowable IDF is ** (1e-6) - roughly the same as a term that appears in just over ** half of set of 5,000,000 documents. */ idf = libc.Xlog(tls, (float64(*(*Tsqlite3_int64)(unsafe.Pointer(bp))-*(*Tsqlite3_int64)(unsafe.Pointer(bp + 16)))+float64(0.5))/(float64(*(*Tsqlite3_int64)(unsafe.Pointer(bp + 16)))+float64(0.5))) if idf <= float64(0) { idf = float64(1e-06) } *(*float64)(unsafe.Pointer((*TFts5Bm25Data)(unsafe.Pointer(p)).FaIDF + uintptr(i)*8)) = idf } goto _1 _1: ; i++ } if rc != SQLITE_OK { Xsqlite3_free(tls, p) } else { rc = (*(*func(*libc.TLS, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxSetAuxdata})))(tls, pFts, p, __ccgo_fp(Xsqlite3_free)) } if rc != SQLITE_OK { p = uintptr(0) } } *(*uintptr)(unsafe.Pointer(ppData)) = p return rc } // C documentation // // /* // ** Implementation of bm25() function. // */ func _fts5Bm25Function(tls *libc.TLS, pApi uintptr, pFts uintptr, pCtx uintptr, nVal int32, apVal uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var D, b, k1, score, w, v2 float64 var aFreq uintptr var i, rc int32 var _ /* ic at bp+12 */ int32 var _ /* io at bp+16 */ int32 var _ /* ip at bp+8 */ int32 var _ /* nInst at bp+4 */ int32 var _ /* nTok at bp+20 */ int32 var _ /* pData at bp+0 */ uintptr _, _, _, _, _, _, _, _, _ = D, aFreq, b, i, k1, rc, score, w, v2 k1 = float64(1.2) /* Constant "k1" from BM25 formula */ b = float64(0.75) /* Error code */ score = float64(0) /* Iterator variable */ *(*int32)(unsafe.Pointer(bp + 4)) = 0 /* Value returned by xInstCount() */ D = float64(0) /* Total number of tokens in row */ aFreq = uintptr(0) /* Array of phrase freq. for current row */ /* Calculate the phrase frequency (symbol "f(qi,D)" in the documentation) ** for each phrase in the query for the current row. */ rc = _fts5Bm25GetData(tls, pApi, pFts, bp) if rc == SQLITE_OK { aFreq = (*TFts5Bm25Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaFreq libc.Xmemset(tls, aFreq, 0, uint32(8)*uint32((*TFts5Bm25Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnPhrase)) rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInstCount})))(tls, pFts, bp+4) } i = 0 for { if !(rc == SQLITE_OK && i < *(*int32)(unsafe.Pointer(bp + 4))) { break } rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInst})))(tls, pFts, i, bp+8, bp+12, bp+16) if rc == SQLITE_OK { if nVal > *(*int32)(unsafe.Pointer(bp + 12)) { v2 = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(apVal + uintptr(*(*int32)(unsafe.Pointer(bp + 12)))*4))) } else { v2 = float64(1) } w = v2 *(*float64)(unsafe.Pointer(aFreq + uintptr(*(*int32)(unsafe.Pointer(bp + 8)))*8)) += w } goto _1 _1: ; i++ } /* Figure out the total size of the current row in tokens. */ if rc == SQLITE_OK { rc = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnSize})))(tls, pFts, -int32(1), bp+20) D = float64(*(*int32)(unsafe.Pointer(bp + 20))) } /* Determine and return the BM25 score for the current row. Or, if an ** error has occurred, throw an exception. */ if rc == SQLITE_OK { i = 0 for { if !(i < (*TFts5Bm25Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnPhrase) { break } score += *(*float64)(unsafe.Pointer((*TFts5Bm25Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaIDF + uintptr(i)*8)) * (*(*float64)(unsafe.Pointer(aFreq + uintptr(i)*8)) * (k1 + libc.Float64FromFloat64(1)) / (*(*float64)(unsafe.Pointer(aFreq + uintptr(i)*8)) + k1*(libc.Float64FromInt32(1)-b+b*D/(*TFts5Bm25Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Favgdl))) goto _3 _3: ; i++ } Xsqlite3_result_double(tls, pCtx, -libc.Float64FromFloat64(1)*score) } else { Xsqlite3_result_error_code(tls, pCtx, rc) } } func _sqlite3Fts5AuxInit(tls *libc.TLS, pApi uintptr) (r int32) { var aBuiltin [3]struct { FzFunc uintptr FpUserData uintptr FxFunc Tfts5_extension_function FxDestroy uintptr } var i, rc int32 _, _, _ = aBuiltin, i, rc aBuiltin = [3]struct { FzFunc uintptr FpUserData uintptr FxFunc Tfts5_extension_function FxDestroy uintptr }{ 0: { FzFunc: __ccgo_ts + 34886, FxFunc: __ccgo_fp(_fts5SnippetFunction), }, 1: { FzFunc: __ccgo_ts + 34894, FxFunc: __ccgo_fp(_fts5HighlightFunction), }, 2: { FzFunc: __ccgo_ts + 34904, FxFunc: __ccgo_fp(_fts5Bm25Function), }, } rc = SQLITE_OK /* To iterate through builtin functions */ i = 0 for { if !(rc == SQLITE_OK && i < int32(libc.Uint32FromInt64(48)/libc.Uint32FromInt64(16))) { break } rc = (*(*func(*libc.TLS, uintptr, uintptr, uintptr, Tfts5_extension_function, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tfts5_api)(unsafe.Pointer(pApi)).FxCreateFunction})))(tls, pApi, aBuiltin[i].FzFunc, aBuiltin[i].FpUserData, aBuiltin[i].FxFunc, aBuiltin[i].FxDestroy) goto _1 _1: ; i++ } return rc } /* ** 2014 May 31 ** ** 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. ** ****************************************************************************** */ /* #include "fts5Int.h" */ func _sqlite3Fts5BufferSize(tls *libc.TLS, pRc uintptr, pBuf uintptr, nByte Tu32) (r int32) { var nNew Tu64 var pNew uintptr var v1 int32 _, _, _ = nNew, pNew, v1 if uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).FnSpace) < nByte { if (*TFts5Buffer)(unsafe.Pointer(pBuf)).FnSpace != 0 { v1 = (*TFts5Buffer)(unsafe.Pointer(pBuf)).FnSpace } else { v1 = int32(64) } nNew = uint64(v1) for nNew < uint64(nByte) { nNew = nNew * uint64(2) } pNew = Xsqlite3_realloc64(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp, nNew) if pNew == uintptr(0) { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) return int32(1) } else { (*TFts5Buffer)(unsafe.Pointer(pBuf)).FnSpace = int32(nNew) (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp = pNew } } return 0 } // C documentation // // /* // ** Encode value iVal as an SQLite varint and append it to the buffer object // ** pBuf. If an OOM error occurs, set the error code in p. // */ func _sqlite3Fts5BufferAppendVarint(tls *libc.TLS, pRc uintptr, pBuf uintptr, iVal Ti64) { var v1 int32 _ = v1 if uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)+uint32(libc.Int32FromInt32(9)) <= uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).FnSpace) { v1 = 0 } else { v1 = _sqlite3Fts5BufferSize(tls, pRc, pBuf, uint32(int32(9)+(*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)) } if v1 != 0 { return } *(*int32)(unsafe.Pointer(pBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), uint64(iVal)) } func _sqlite3Fts5Put32(tls *libc.TLS, aBuf uintptr, iVal int32) { *(*Tu8)(unsafe.Pointer(aBuf)) = uint8(iVal >> int32(24) & int32(0x00FF)) *(*Tu8)(unsafe.Pointer(aBuf + 1)) = uint8(iVal >> int32(16) & int32(0x00FF)) *(*Tu8)(unsafe.Pointer(aBuf + 2)) = uint8(iVal >> int32(8) & int32(0x00FF)) *(*Tu8)(unsafe.Pointer(aBuf + 3)) = uint8(iVal >> 0 & int32(0x00FF)) } func _sqlite3Fts5Get32(tls *libc.TLS, aBuf uintptr) (r int32) { return int32(uint32(*(*Tu8)(unsafe.Pointer(aBuf)))<n count. // */ func _sqlite3Fts5BufferAppendString(tls *libc.TLS, pRc uintptr, pBuf uintptr, zStr uintptr) { var nStr int32 _ = nStr nStr = int32(libc.Xstrlen(tls, zStr)) _sqlite3Fts5BufferAppendBlob(tls, pRc, pBuf, uint32(nStr+int32(1)), zStr) (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn-- } // C documentation // // /* // ** Argument zFmt is a printf() style format string. This function performs // ** the printf() style processing, then appends the results to buffer pBuf. // ** // ** Like sqlite3Fts5BufferAppendString(), this function ensures that the byte // ** following the buffer data is set to 0x00, even though this byte is not // ** included in the pBuf->n count. // */ func _sqlite3Fts5BufferAppendPrintf(tls *libc.TLS, pRc uintptr, pBuf uintptr, zFmt uintptr, va uintptr) { var ap Tva_list var zTmp uintptr _, _ = ap, zTmp if *(*int32)(unsafe.Pointer(pRc)) == SQLITE_OK { ap = va zTmp = Xsqlite3_vmprintf(tls, zFmt, ap) _ = ap if zTmp == uintptr(0) { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) } else { _sqlite3Fts5BufferAppendString(tls, pRc, pBuf, zTmp) Xsqlite3_free(tls, zTmp) } } } func _sqlite3Fts5Mprintf(tls *libc.TLS, pRc uintptr, zFmt uintptr, va uintptr) (r uintptr) { var ap Tva_list var zRet uintptr _, _ = ap, zRet zRet = uintptr(0) if *(*int32)(unsafe.Pointer(pRc)) == SQLITE_OK { ap = va zRet = Xsqlite3_vmprintf(tls, zFmt, ap) _ = ap if zRet == uintptr(0) { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) } } return zRet } // C documentation // // /* // ** Free any buffer allocated by pBuf. Zero the structure before returning. // */ func _sqlite3Fts5BufferFree(tls *libc.TLS, pBuf uintptr) { Xsqlite3_free(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp) libc.Xmemset(tls, pBuf, 0, uint32(12)) } // C documentation // // /* // ** Zero the contents of the buffer object. But do not free the associated // ** memory allocation. // */ func _sqlite3Fts5BufferZero(tls *libc.TLS, pBuf uintptr) { (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn = 0 } // C documentation // // /* // ** Set the buffer to contain nData/pData. If an OOM error occurs, leave an // ** the error code in p. If an error has already occurred when this function // ** is called, it is a no-op. // */ func _sqlite3Fts5BufferSet(tls *libc.TLS, pRc uintptr, pBuf uintptr, nData int32, pData uintptr) { (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn = 0 _sqlite3Fts5BufferAppendBlob(tls, pRc, pBuf, uint32(nData), pData) } func _sqlite3Fts5PoslistNext64(tls *libc.TLS, a uintptr, n int32, pi uintptr, piOff uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, v1, v2, v3 int32 var iOff Ti64 var _ /* iVal at bp+0 */ Tu32 _, _, _, _, _ = i, iOff, v1, v2, v3 i = *(*int32)(unsafe.Pointer(pi)) _ = libc.Int32FromInt32(0) if i >= n { /* EOF */ *(*Ti64)(unsafe.Pointer(piOff)) = int64(-int32(1)) return int32(1) } else { iOff = *(*Ti64)(unsafe.Pointer(piOff)) _ = libc.Int32FromInt32(0) v1 = i i++ *(*Tu32)(unsafe.Pointer(bp)) = uint32(*(*Tu8)(unsafe.Pointer(a + uintptr(v1)))) if *(*Tu32)(unsafe.Pointer(bp))&uint32(0x80) != 0 { i-- i += _sqlite3Fts5GetVarint32(tls, a+uintptr(i), bp) } if *(*Tu32)(unsafe.Pointer(bp)) <= uint32(1) { if *(*Tu32)(unsafe.Pointer(bp)) == uint32(0) { *(*int32)(unsafe.Pointer(pi)) = i return 0 } v2 = i i++ *(*Tu32)(unsafe.Pointer(bp)) = uint32(*(*Tu8)(unsafe.Pointer(a + uintptr(v2)))) if *(*Tu32)(unsafe.Pointer(bp))&uint32(0x80) != 0 { i-- i += _sqlite3Fts5GetVarint32(tls, a+uintptr(i), bp) } iOff = int64(*(*Tu32)(unsafe.Pointer(bp))) << int32(32) _ = libc.Int32FromInt32(0) v3 = i i++ *(*Tu32)(unsafe.Pointer(bp)) = uint32(*(*Tu8)(unsafe.Pointer(a + uintptr(v3)))) if *(*Tu32)(unsafe.Pointer(bp))&uint32(0x80) != 0 { i-- i += _sqlite3Fts5GetVarint32(tls, a+uintptr(i), bp) } if *(*Tu32)(unsafe.Pointer(bp)) < uint32(2) { /* This is a corrupt record. So stop parsing it here. */ *(*Ti64)(unsafe.Pointer(piOff)) = int64(-int32(1)) return int32(1) } *(*Ti64)(unsafe.Pointer(piOff)) = iOff + int64((*(*Tu32)(unsafe.Pointer(bp))-libc.Uint32FromInt32(2))&libc.Uint32FromInt32(0x7FFFFFFF)) } else { *(*Ti64)(unsafe.Pointer(piOff)) = iOff&(libc.Int64FromInt32(0x7FFFFFFF)<= *(*Ti64)(unsafe.Pointer(piPrev)) { if iPos&_colmask != *(*Ti64)(unsafe.Pointer(piPrev))&_colmask { v2 = pBuf + 4 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ *(*Tu8)(unsafe.Pointer((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp + uintptr(v1))) = uint8(1) *(*int32)(unsafe.Pointer(pBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), uint64(iPos>>libc.Int32FromInt32(32))) *(*Ti64)(unsafe.Pointer(piPrev)) = iPos & _colmask } *(*int32)(unsafe.Pointer(pBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), uint64(iPos-*(*Ti64)(unsafe.Pointer(piPrev))+int64(2))) *(*Ti64)(unsafe.Pointer(piPrev)) = iPos } } var _colmask = int64(libc.Int32FromInt32(0x7FFFFFFF)) << libc.Int32FromInt32(32) func _sqlite3Fts5PoslistWriterAppend(tls *libc.TLS, pBuf uintptr, pWriter uintptr, iPos Ti64) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var v1 int32 var _ /* rc at bp+0 */ int32 _ = v1 *(*int32)(unsafe.Pointer(bp)) = 0 /* Initialized only to suppress erroneous warning from Clang */ if uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)+uint32(libc.Int32FromInt32(5)+libc.Int32FromInt32(5)+libc.Int32FromInt32(5)) <= uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).FnSpace) { v1 = 0 } else { v1 = _sqlite3Fts5BufferSize(tls, bp, pBuf, uint32(libc.Int32FromInt32(5)+libc.Int32FromInt32(5)+libc.Int32FromInt32(5)+(*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)) } if v1 != 0 { return *(*int32)(unsafe.Pointer(bp)) } _sqlite3Fts5PoslistSafeAppend(tls, pBuf, pWriter, iPos) return SQLITE_OK } func _sqlite3Fts5MallocZero(tls *libc.TLS, pRc uintptr, nByte Tsqlite3_int64) (r uintptr) { var pRet uintptr _ = pRet pRet = uintptr(0) if *(*int32)(unsafe.Pointer(pRc)) == SQLITE_OK { pRet = Xsqlite3_malloc64(tls, uint64(nByte)) if pRet == uintptr(0) { if nByte > 0 { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) } } else { libc.Xmemset(tls, pRet, 0, uint32(nByte)) } } return pRet } // C documentation // // /* // ** Return a nul-terminated copy of the string indicated by pIn. If nIn // ** is non-negative, then it is the length of the string in bytes. Otherwise, // ** the length of the string is determined using strlen(). // ** // ** It is the responsibility of the caller to eventually free the returned // ** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned. // */ func _sqlite3Fts5Strndup(tls *libc.TLS, pRc uintptr, pIn uintptr, nIn int32) (r uintptr) { var zRet uintptr _ = zRet zRet = uintptr(0) if *(*int32)(unsafe.Pointer(pRc)) == SQLITE_OK { if nIn < 0 { nIn = int32(libc.Xstrlen(tls, pIn)) } zRet = Xsqlite3_malloc(tls, nIn+int32(1)) if zRet != 0 { libc.Xmemcpy(tls, zRet, pIn, uint32(nIn)) *(*uint8)(unsafe.Pointer(zRet + uintptr(nIn))) = uint8('\000') } else { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) } } return zRet } // C documentation // // /* // ** Return true if character 't' may be part of an FTS5 bareword, or false // ** otherwise. Characters that may be part of barewords: // ** // ** * All non-ASCII characters, // ** * The 52 upper and lower case ASCII characters, and // ** * The 10 integer ASCII characters. // ** * The underscore character "_" (0x5F). // ** * The unicode "subsitute" character (0x1A). // */ func _sqlite3Fts5IsBareword(tls *libc.TLS, t uint8) (r int32) { var aBareword [128]Tu8 _ = aBareword aBareword = [128]Tu8{ 26: uint8(1), 48: uint8(1), 49: uint8(1), 50: uint8(1), 51: uint8(1), 52: uint8(1), 53: uint8(1), 54: uint8(1), 55: uint8(1), 56: uint8(1), 57: uint8(1), 65: uint8(1), 66: uint8(1), 67: uint8(1), 68: uint8(1), 69: uint8(1), 70: uint8(1), 71: uint8(1), 72: uint8(1), 73: uint8(1), 74: uint8(1), 75: uint8(1), 76: uint8(1), 77: uint8(1), 78: uint8(1), 79: uint8(1), 80: uint8(1), 81: uint8(1), 82: uint8(1), 83: uint8(1), 84: uint8(1), 85: uint8(1), 86: uint8(1), 87: uint8(1), 88: uint8(1), 89: uint8(1), 90: uint8(1), 95: uint8(1), 97: uint8(1), 98: uint8(1), 99: uint8(1), 100: uint8(1), 101: uint8(1), 102: uint8(1), 103: uint8(1), 104: uint8(1), 105: uint8(1), 106: uint8(1), 107: uint8(1), 108: uint8(1), 109: uint8(1), 110: uint8(1), 111: uint8(1), 112: uint8(1), 113: uint8(1), 114: uint8(1), 115: uint8(1), 116: uint8(1), 117: uint8(1), 118: uint8(1), 119: uint8(1), 120: uint8(1), 121: uint8(1), 122: uint8(1), } return libc.BoolInt32(int32(t)&int32(0x80) != 0 || aBareword[int32(t)] != 0) } // C documentation // // /************************************************************************* // */ type TFts5TermsetEntry = struct { FpTerm uintptr FnTerm int32 FiIdx int32 FpNext uintptr } type Fts5TermsetEntry = TFts5TermsetEntry type TFts5TermsetEntry1 = struct { FpTerm uintptr FnTerm int32 FiIdx int32 FpNext uintptr } type Fts5TermsetEntry1 = TFts5TermsetEntry1 type TFts5Termset1 = struct { FapHash [512]uintptr } type Fts5Termset1 = TFts5Termset1 func _sqlite3Fts5TermsetNew(tls *libc.TLS, pp uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* rc at bp+0 */ int32 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*uintptr)(unsafe.Pointer(pp)) = _sqlite3Fts5MallocZero(tls, bp, int64(2048)) return *(*int32)(unsafe.Pointer(bp)) } func _sqlite3Fts5TermsetAdd(tls *libc.TLS, p uintptr, iIdx int32, pTerm uintptr, nTerm int32, pbPresent uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var hash Tu32 var i int32 var pEntry uintptr var _ /* rc at bp+0 */ int32 _, _, _ = hash, i, pEntry *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*int32)(unsafe.Pointer(pbPresent)) = 0 if p != 0 { hash = uint32(13) /* Calculate a hash value for this term. This is the same hash checksum ** used by the fts5_hash.c module. This is not important for correct ** operation of the module, but is necessary to ensure that some tests ** designed to produce hash table collisions really do work. */ i = nTerm - int32(1) for { if !(i >= 0) { break } hash = hash<= int32('0') && int32(a) <= int32('9')) } func _fts5ConfigSkipLiteral(tls *libc.TLS, pIn uintptr) (r uintptr) { var p uintptr _ = p p = pIn switch int32(*(*uint8)(unsafe.Pointer(p))) { case int32('n'): fallthrough case int32('N'): if Xsqlite3_strnicmp(tls, __ccgo_ts+1634, p, int32(4)) == 0 { p = p + 4 } else { p = uintptr(0) } case int32('x'): fallthrough case int32('X'): p++ if int32(*(*uint8)(unsafe.Pointer(p))) == int32('\'') { p++ for int32(*(*uint8)(unsafe.Pointer(p))) >= int32('a') && int32(*(*uint8)(unsafe.Pointer(p))) <= int32('f') || int32(*(*uint8)(unsafe.Pointer(p))) >= int32('A') && int32(*(*uint8)(unsafe.Pointer(p))) <= int32('F') || int32(*(*uint8)(unsafe.Pointer(p))) >= int32('0') && int32(*(*uint8)(unsafe.Pointer(p))) <= int32('9') { p++ } if int32(*(*uint8)(unsafe.Pointer(p))) == int32('\'') && 0 == (int32(p)-int32(pIn))%int32(2) { p++ } else { p = uintptr(0) } } else { p = uintptr(0) } case int32('\''): p++ for p != 0 { if int32(*(*uint8)(unsafe.Pointer(p))) == int32('\'') { p++ if int32(*(*uint8)(unsafe.Pointer(p))) != int32('\'') { break } } p++ if int32(*(*uint8)(unsafe.Pointer(p))) == 0 { p = uintptr(0) } } default: /* maybe a number */ if int32(*(*uint8)(unsafe.Pointer(p))) == int32('+') || int32(*(*uint8)(unsafe.Pointer(p))) == int32('-') { p++ } for _fts5_isdigit(tls, *(*uint8)(unsafe.Pointer(p))) != 0 { p++ } /* At this point, if the literal was an integer, the parse is ** finished. Or, if it is a floating point value, it may continue ** with either a decimal point or an 'E' character. */ if int32(*(*uint8)(unsafe.Pointer(p))) == int32('.') && _fts5_isdigit(tls, *(*uint8)(unsafe.Pointer(p + 1))) != 0 { p += uintptr(2) for _fts5_isdigit(tls, *(*uint8)(unsafe.Pointer(p))) != 0 { p++ } } if p == pIn { p = uintptr(0) } break } return p } // C documentation // // /* // ** The first character of the string pointed to by argument z is guaranteed // ** to be an open-quote character (see function fts5_isopenquote()). // ** // ** This function searches for the corresponding close-quote character within // ** the string and, if found, dequotes the string in place and adds a new // ** nul-terminator byte. // ** // ** If the close-quote is found, the value returned is the byte offset of // ** the character immediately following it. Or, if the close-quote is not // ** found, -1 is returned. If -1 is returned, the buffer is left in an // ** undefined state. // */ func _fts5Dequote(tls *libc.TLS, z uintptr) (r int32) { var iIn, iOut, v1, v2, v3 int32 var q uint8 _, _, _, _, _, _ = iIn, iOut, q, v1, v2, v3 iIn = int32(1) iOut = 0 q = *(*uint8)(unsafe.Pointer(z)) /* Set stack variable q to the close-quote character */ _ = libc.Int32FromInt32(0) if int32(q) == int32('[') { q = uint8(']') } for *(*uint8)(unsafe.Pointer(z + uintptr(iIn))) != 0 { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(iIn)))) == int32(q) { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(iIn+int32(1))))) != int32(q) { /* Character iIn was the close quote. */ iIn++ break } else { /* Character iIn and iIn+1 form an escaped quote character. Skip ** the input cursor past both and copy a single quote character ** to the output buffer. */ iIn += int32(2) v1 = iOut iOut++ *(*uint8)(unsafe.Pointer(z + uintptr(v1))) = q } } else { v2 = iOut iOut++ v3 = iIn iIn++ *(*uint8)(unsafe.Pointer(z + uintptr(v2))) = *(*uint8)(unsafe.Pointer(z + uintptr(v3))) } } *(*uint8)(unsafe.Pointer(z + uintptr(iOut))) = uint8('\000') return iIn } // C documentation // // /* // ** Convert an SQL-style quoted string into a normal string by removing // ** the quote characters. The conversion is done in-place. If the // ** input does not begin with a quote character, then this routine // ** is a no-op. // ** // ** Examples: // ** // ** "abc" becomes abc // ** 'xyz' becomes xyz // ** [pqr] becomes pqr // ** `mno` becomes mno // */ func _sqlite3Fts5Dequote(tls *libc.TLS, z uintptr) { var quote uint8 _ = quote /* Quote character (if any ) */ _ = libc.Int32FromInt32(0) quote = *(*uint8)(unsafe.Pointer(z)) if int32(quote) == int32('[') || int32(quote) == int32('\'') || int32(quote) == int32('"') || int32(quote) == int32('`') { _fts5Dequote(tls, z) } } type TFts5Enum1 = struct { FzName uintptr FeVal int32 } type Fts5Enum1 = TFts5Enum1 type TFts5Enum = struct { FzName uintptr FeVal int32 } type Fts5Enum = TFts5Enum func _fts5ConfigSetEnum(tls *libc.TLS, aEnum uintptr, zEnum uintptr, peVal uintptr) (r int32) { var i, iVal, nEnum, v2 int32 _, _, _, _ = i, iVal, nEnum, v2 nEnum = int32(libc.Xstrlen(tls, zEnum)) iVal = -int32(1) i = 0 for { if !((*(*TFts5Enum)(unsafe.Pointer(aEnum + uintptr(i)*8))).FzName != 0) { break } if Xsqlite3_strnicmp(tls, (*(*TFts5Enum)(unsafe.Pointer(aEnum + uintptr(i)*8))).FzName, zEnum, nEnum) == 0 { if iVal >= 0 { return int32(SQLITE_ERROR) } iVal = (*(*TFts5Enum)(unsafe.Pointer(aEnum + uintptr(i)*8))).FeVal } goto _1 _1: ; i++ } *(*int32)(unsafe.Pointer(peVal)) = iVal if iVal < 0 { v2 = int32(SQLITE_ERROR) } else { v2 = SQLITE_OK } return v2 } // C documentation // // /* // ** Parse a "special" CREATE VIRTUAL TABLE directive and update // ** configuration object pConfig as appropriate. // ** // ** If successful, object pConfig is updated and SQLITE_OK returned. If // ** an error occurs, an SQLite error code is returned and an error message // ** may be left in *pzErr. It is the responsibility of the caller to // ** eventually free any such error message using sqlite3_free(). // */ func _fts5ConfigParseSpecial(tls *libc.TLS, pGlobal uintptr, pConfig uintptr, zCmd uintptr, zArg uintptr, pzErr uintptr) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var azArg, p, p1, p2, pDel, pSpace uintptr var bFirst, nByte, nCmd, nPre, v2 int32 var nArg Tsqlite3_int64 var _ /* aDetail at bp+4 */ [4]TFts5Enum var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _, _, _ = azArg, bFirst, nArg, nByte, nCmd, nPre, p, p1, p2, pDel, pSpace, v2 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK nCmd = int32(libc.Xstrlen(tls, zCmd)) if Xsqlite3_strnicmp(tls, __ccgo_ts+34909, zCmd, nCmd) == 0 { nByte = int32(libc.Uint32FromInt64(4) * libc.Uint32FromInt32(FTS5_MAX_PREFIX_INDEXES)) bFirst = int32(1) if (*TFts5Config)(unsafe.Pointer(pConfig)).FaPrefix == uintptr(0) { (*TFts5Config)(unsafe.Pointer(pConfig)).FaPrefix = _sqlite3Fts5MallocZero(tls, bp, int64(nByte)) if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } } p = zArg for int32(1) != 0 { nPre = 0 for int32(*(*uint8)(unsafe.Pointer(p))) == int32(' ') { p++ } if bFirst == 0 && int32(*(*uint8)(unsafe.Pointer(p))) == int32(',') { p++ for int32(*(*uint8)(unsafe.Pointer(p))) == int32(' ') { p++ } } else { if int32(*(*uint8)(unsafe.Pointer(p))) == int32('\000') { break } } if int32(*(*uint8)(unsafe.Pointer(p))) < int32('0') || int32(*(*uint8)(unsafe.Pointer(p))) > int32('9') { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+34916, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) break } if (*TFts5Config)(unsafe.Pointer(pConfig)).FnPrefix == int32(FTS5_MAX_PREFIX_INDEXES) { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+34947, libc.VaList(bp+48, int32(FTS5_MAX_PREFIX_INDEXES))) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) break } for int32(*(*uint8)(unsafe.Pointer(p))) >= int32('0') && int32(*(*uint8)(unsafe.Pointer(p))) <= int32('9') && nPre < int32(1000) { nPre = nPre*int32(10) + (int32(*(*uint8)(unsafe.Pointer(p))) - int32('0')) p++ } if nPre <= 0 || nPre >= int32(1000) { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+34980, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) break } *(*int32)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(pConfig)).FaPrefix + uintptr((*TFts5Config)(unsafe.Pointer(pConfig)).FnPrefix)*4)) = nPre (*TFts5Config)(unsafe.Pointer(pConfig)).FnPrefix++ bFirst = 0 } _ = libc.Int32FromInt32(0) return *(*int32)(unsafe.Pointer(bp)) } if Xsqlite3_strnicmp(tls, __ccgo_ts+35017, zCmd, nCmd) == 0 { p1 = zArg nArg = int64(libc.Xstrlen(tls, zArg) + uint32(1)) azArg = _sqlite3Fts5MallocZero(tls, bp, int64(4)*nArg) pDel = _sqlite3Fts5MallocZero(tls, bp, nArg*int64(2)) pSpace = pDel if azArg != 0 && pSpace != 0 { if (*TFts5Config)(unsafe.Pointer(pConfig)).FpTok != 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35026, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { nArg = 0 for { if !(p1 != 0 && *(*uint8)(unsafe.Pointer(p1)) != 0) { break } p2 = _fts5ConfigSkipWhitespace(tls, p1) if int32(*(*uint8)(unsafe.Pointer(p2))) == int32('\'') { p1 = _fts5ConfigSkipLiteral(tls, p2) } else { p1 = _fts5ConfigSkipBareword(tls, p2) } if p1 != 0 { libc.Xmemcpy(tls, pSpace, p2, uint32(int32(p1)-int32(p2))) *(*uintptr)(unsafe.Pointer(azArg + uintptr(nArg)*4)) = pSpace _sqlite3Fts5Dequote(tls, pSpace) pSpace += uintptr(int32(p1) - int32(p2) + int32(1)) p1 = _fts5ConfigSkipWhitespace(tls, p1) } goto _1 _1: ; nArg++ } if p1 == uintptr(0) { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35059, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { *(*int32)(unsafe.Pointer(bp)) = _sqlite3Fts5GetTokenizer(tls, pGlobal, azArg, int32(nArg), pConfig, pzErr) } } } Xsqlite3_free(tls, azArg) Xsqlite3_free(tls, pDel) return *(*int32)(unsafe.Pointer(bp)) } if Xsqlite3_strnicmp(tls, __ccgo_ts+35093, zCmd, nCmd) == 0 { if (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent != FTS5_CONTENT_NORMAL { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35101, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { if *(*uint8)(unsafe.Pointer(zArg)) != 0 { (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent = int32(FTS5_CONTENT_EXTERNAL) (*TFts5Config)(unsafe.Pointer(pConfig)).FzContent = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+35133, libc.VaList(bp+48, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, zArg)) } else { (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent = int32(FTS5_CONTENT_NONE) } } return *(*int32)(unsafe.Pointer(bp)) } if Xsqlite3_strnicmp(tls, __ccgo_ts+35139, zCmd, nCmd) == 0 { if int32(*(*uint8)(unsafe.Pointer(zArg))) != int32('0') && int32(*(*uint8)(unsafe.Pointer(zArg))) != int32('1') || int32(*(*uint8)(unsafe.Pointer(zArg + 1))) != int32('\000') { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35158, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { (*TFts5Config)(unsafe.Pointer(pConfig)).FbContentlessDelete = libc.BoolInt32(int32(*(*uint8)(unsafe.Pointer(zArg))) == int32('1')) } return *(*int32)(unsafe.Pointer(bp)) } if Xsqlite3_strnicmp(tls, __ccgo_ts+35201, zCmd, nCmd) == 0 { if (*TFts5Config)(unsafe.Pointer(pConfig)).FzContentRowid != 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35215, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { (*TFts5Config)(unsafe.Pointer(pConfig)).FzContentRowid = _sqlite3Fts5Strndup(tls, bp, zArg, -int32(1)) } return *(*int32)(unsafe.Pointer(bp)) } if Xsqlite3_strnicmp(tls, __ccgo_ts+35253, zCmd, nCmd) == 0 { if int32(*(*uint8)(unsafe.Pointer(zArg))) != int32('0') && int32(*(*uint8)(unsafe.Pointer(zArg))) != int32('1') || int32(*(*uint8)(unsafe.Pointer(zArg + 1))) != int32('\000') { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35264, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { (*TFts5Config)(unsafe.Pointer(pConfig)).FbColumnsize = libc.BoolInt32(int32(*(*uint8)(unsafe.Pointer(zArg))) == int32('1')) } return *(*int32)(unsafe.Pointer(bp)) } if Xsqlite3_strnicmp(tls, __ccgo_ts+5499, zCmd, nCmd) == 0 { *(*[4]TFts5Enum)(unsafe.Pointer(bp + 4)) = [4]TFts5Enum{ 0: { FzName: __ccgo_ts + 8218, FeVal: int32(FTS5_DETAIL_NONE), }, 1: { FzName: __ccgo_ts + 17747, }, 2: { FzName: __ccgo_ts + 35299, FeVal: int32(FTS5_DETAIL_COLUMNS), }, 3: {}, } v2 = _fts5ConfigSetEnum(tls, bp+4, zArg, pConfig+56) *(*int32)(unsafe.Pointer(bp)) = v2 if v2 != 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35307, 0) } return *(*int32)(unsafe.Pointer(bp)) } if Xsqlite3_strnicmp(tls, __ccgo_ts+35338, zCmd, nCmd) == 0 { if int32(*(*uint8)(unsafe.Pointer(zArg))) != int32('0') && int32(*(*uint8)(unsafe.Pointer(zArg))) != int32('1') || int32(*(*uint8)(unsafe.Pointer(zArg + 1))) != int32('\000') { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35348, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { (*TFts5Config)(unsafe.Pointer(pConfig)).FbTokendata = libc.BoolInt32(int32(*(*uint8)(unsafe.Pointer(zArg))) == int32('1')) } return *(*int32)(unsafe.Pointer(bp)) } *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35382, libc.VaList(bp+48, nCmd, zCmd)) return int32(SQLITE_ERROR) } // C documentation // // /* // ** Allocate an instance of the default tokenizer ("simple") at // ** Fts5Config.pTokenizer. Return SQLITE_OK if successful, or an SQLite error // ** code if an error occurs. // */ func _fts5ConfigDefaultTokenizer(tls *libc.TLS, pGlobal uintptr, pConfig uintptr) (r int32) { _ = libc.Int32FromInt32(0) return _sqlite3Fts5GetTokenizer(tls, pGlobal, uintptr(0), 0, pConfig, uintptr(0)) } // C documentation // // /* // ** Gobble up the first bareword or quoted word from the input buffer zIn. // ** Return a pointer to the character immediately following the last in // ** the gobbled word if successful, or a NULL pointer otherwise (failed // ** to find close-quote character). // ** // ** Before returning, set pzOut to point to a new buffer containing a // ** nul-terminated, dequoted copy of the gobbled word. If the word was // ** quoted, *pbQuoted is also set to 1 before returning. // ** // ** If *pRc is other than SQLITE_OK when this function is called, it is // ** a no-op (NULL is returned). Otherwise, if an OOM occurs within this // ** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not* // ** set if a parse error (failed to find close quote) occurs. // */ func _fts5ConfigGobbleWord(tls *libc.TLS, pRc uintptr, zIn uintptr, pzOut uintptr, pbQuoted uintptr) (r uintptr) { var ii int32 var nIn Tsqlite3_int64 var zOut, zRet uintptr _, _, _, _ = ii, nIn, zOut, zRet zRet = uintptr(0) nIn = int64(libc.Xstrlen(tls, zIn)) zOut = Xsqlite3_malloc64(tls, uint64(nIn+int64(1))) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pbQuoted)) = 0 *(*uintptr)(unsafe.Pointer(pzOut)) = uintptr(0) if zOut == uintptr(0) { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) } else { libc.Xmemcpy(tls, zOut, zIn, uint32(nIn+libc.Int64FromInt32(1))) if _fts5_isopenquote(tls, *(*uint8)(unsafe.Pointer(zOut))) != 0 { ii = _fts5Dequote(tls, zOut) zRet = zIn + uintptr(ii) *(*int32)(unsafe.Pointer(pbQuoted)) = int32(1) } else { zRet = _fts5ConfigSkipBareword(tls, zIn) if zRet != 0 { *(*uint8)(unsafe.Pointer(zOut + uintptr(int32(zRet)-int32(zIn)))) = uint8('\000') } } } if zRet == uintptr(0) { Xsqlite3_free(tls, zOut) } else { *(*uintptr)(unsafe.Pointer(pzOut)) = zOut } return zRet } func _fts5ConfigParseColumn(tls *libc.TLS, p uintptr, zCol uintptr, zArg uintptr, pzErr uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc, v1 int32 var v2 uintptr _, _, _ = rc, v1, v2 rc = SQLITE_OK if 0 == Xsqlite3_stricmp(tls, zCol, __ccgo_ts+35410) || 0 == Xsqlite3_stricmp(tls, zCol, __ccgo_ts+16694) { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35415, libc.VaList(bp+8, zCol)) rc = int32(SQLITE_ERROR) } else { if zArg != 0 { if 0 == Xsqlite3_stricmp(tls, zArg, __ccgo_ts+35445) { *(*Tu8)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(p)).FabUnindexed + uintptr((*TFts5Config)(unsafe.Pointer(p)).FnCol))) = uint8(1) } else { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35455, libc.VaList(bp+8, zArg)) rc = int32(SQLITE_ERROR) } } } v2 = p + 12 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ *(*uintptr)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(p)).FazCol + uintptr(v1)*4)) = zCol return rc } // C documentation // // /* // ** Populate the Fts5Config.zContentExprlist string. // */ func _fts5ConfigMakeExprlist(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var i int32 var _ /* buf at bp+4 */ TFts5Buffer var _ /* rc at bp+0 */ int32 _ = i *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*TFts5Buffer)(unsafe.Pointer(bp + 4)) = TFts5Buffer{} _sqlite3Fts5BufferAppendPrintf(tls, bp, bp+4, __ccgo_ts+35486, libc.VaList(bp+24, (*TFts5Config)(unsafe.Pointer(p)).FzContentRowid)) if (*TFts5Config)(unsafe.Pointer(p)).FeContent != int32(FTS5_CONTENT_NONE) { i = 0 for { if !(i < (*TFts5Config)(unsafe.Pointer(p)).FnCol) { break } if (*TFts5Config)(unsafe.Pointer(p)).FeContent == int32(FTS5_CONTENT_EXTERNAL) { _sqlite3Fts5BufferAppendPrintf(tls, bp, bp+4, __ccgo_ts+35491, libc.VaList(bp+24, *(*uintptr)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(p)).FazCol + uintptr(i)*4)))) } else { _sqlite3Fts5BufferAppendPrintf(tls, bp, bp+4, __ccgo_ts+35498, libc.VaList(bp+24, i)) } goto _1 _1: ; i++ } } _ = libc.Int32FromInt32(0) (*TFts5Config)(unsafe.Pointer(p)).FzContentExprlist = (*(*TFts5Buffer)(unsafe.Pointer(bp + 4))).Fp return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Arguments nArg/azArg contain the string arguments passed to the xCreate // ** or xConnect method of the virtual table. This function attempts to // ** allocate an instance of Fts5Config containing the results of parsing // ** those arguments. // ** // ** If successful, SQLITE_OK is returned and *ppOut is set to point to the // ** new Fts5Config object. If an error occurs, an SQLite error code is // ** returned, *ppOut is set to NULL and an error message may be left in // ** *pzErr. It is the responsibility of the caller to eventually free any // ** such error message using sqlite3_free(). // */ func _sqlite3Fts5ConfigParse(tls *libc.TLS, pGlobal uintptr, db uintptr, nArg int32, azArg uintptr, ppOut uintptr, pzErr uintptr) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var bOption, i int32 var nByte Tsqlite3_int64 var pRet, z, zOrig, zTail, v1, v2, v4, v5 uintptr var _ /* bDummy at bp+16 */ int32 var _ /* bMustBeCol at bp+12 */ int32 var _ /* rc at bp+0 */ int32 var _ /* zOne at bp+4 */ uintptr var _ /* zTwo at bp+8 */ uintptr _, _, _, _, _, _, _, _, _, _, _ = bOption, i, nByte, pRet, z, zOrig, zTail, v1, v2, v4, v5 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK v1 = Xsqlite3_malloc(tls, int32(128)) pRet = v1 *(*uintptr)(unsafe.Pointer(ppOut)) = v1 if pRet == uintptr(0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, pRet, 0, uint32(128)) (*TFts5Config)(unsafe.Pointer(pRet)).Fdb = db (*TFts5Config)(unsafe.Pointer(pRet)).FiCookie = -int32(1) nByte = int64(uint32(nArg) * (libc.Uint32FromInt64(4) + libc.Uint32FromInt64(1))) (*TFts5Config)(unsafe.Pointer(pRet)).FazCol = _sqlite3Fts5MallocZero(tls, bp, nByte) if (*TFts5Config)(unsafe.Pointer(pRet)).FazCol != 0 { v2 = (*TFts5Config)(unsafe.Pointer(pRet)).FazCol + uintptr(nArg)*4 } else { v2 = uintptr(0) } (*TFts5Config)(unsafe.Pointer(pRet)).FabUnindexed = v2 (*TFts5Config)(unsafe.Pointer(pRet)).FzDb = _sqlite3Fts5Strndup(tls, bp, *(*uintptr)(unsafe.Pointer(azArg + 1*4)), -int32(1)) (*TFts5Config)(unsafe.Pointer(pRet)).FzName = _sqlite3Fts5Strndup(tls, bp, *(*uintptr)(unsafe.Pointer(azArg + 2*4)), -int32(1)) (*TFts5Config)(unsafe.Pointer(pRet)).FbColumnsize = int32(1) (*TFts5Config)(unsafe.Pointer(pRet)).FeDetail = FTS5_DETAIL_FULL if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && Xsqlite3_stricmp(tls, (*TFts5Config)(unsafe.Pointer(pRet)).FzName, __ccgo_ts+35410) == 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35506, libc.VaList(bp+32, (*TFts5Config)(unsafe.Pointer(pRet)).FzName)) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } _ = libc.Int32FromInt32(0) i = int32(3) for { if !(*(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && i < nArg) { break } zOrig = *(*uintptr)(unsafe.Pointer(azArg + uintptr(i)*4)) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) bOption = 0 *(*int32)(unsafe.Pointer(bp + 12)) = 0 z = _fts5ConfigGobbleWord(tls, bp, zOrig, bp+4, bp+12) z = _fts5ConfigSkipWhitespace(tls, z) if z != 0 && int32(*(*uint8)(unsafe.Pointer(z))) == int32('=') { bOption = int32(1) _ = libc.Int32FromInt32(0) z++ if *(*int32)(unsafe.Pointer(bp + 12)) != 0 { z = uintptr(0) } } z = _fts5ConfigSkipWhitespace(tls, z) if z != 0 && *(*uint8)(unsafe.Pointer(z)) != 0 { z = _fts5ConfigGobbleWord(tls, bp, z, bp+8, bp+16) if z != 0 && *(*uint8)(unsafe.Pointer(z)) != 0 { z = uintptr(0) } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { if z == uintptr(0) { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35535, libc.VaList(bp+32, zOrig)) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { if bOption != 0 { if *(*uintptr)(unsafe.Pointer(bp + 4)) != 0 { v4 = *(*uintptr)(unsafe.Pointer(bp + 4)) } else { v4 = __ccgo_ts + 1648 } if *(*uintptr)(unsafe.Pointer(bp + 8)) != 0 { v5 = *(*uintptr)(unsafe.Pointer(bp + 8)) } else { v5 = __ccgo_ts + 1648 } *(*int32)(unsafe.Pointer(bp)) = _fts5ConfigParseSpecial(tls, pGlobal, pRet, v4, v5, pzErr) } else { *(*int32)(unsafe.Pointer(bp)) = _fts5ConfigParseColumn(tls, pRet, *(*uintptr)(unsafe.Pointer(bp + 4)), *(*uintptr)(unsafe.Pointer(bp + 8)), pzErr) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) } } } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) goto _3 _3: ; i++ } /* We only allow contentless_delete=1 if the table is indeed contentless. */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pRet)).FbContentlessDelete != 0 && (*TFts5Config)(unsafe.Pointer(pRet)).FeContent != int32(FTS5_CONTENT_NONE) { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35555, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } /* We only allow contentless_delete=1 if columnsize=0 is not present. ** ** This restriction may be removed at some point. */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pRet)).FbContentlessDelete != 0 && (*TFts5Config)(unsafe.Pointer(pRet)).FbColumnsize == 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35605, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } /* If a tokenizer= option was successfully parsed, the tokenizer has ** already been allocated. Otherwise, allocate an instance of the default ** tokenizer (unicode61) now. */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pRet)).FpTok == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = _fts5ConfigDefaultTokenizer(tls, pGlobal, pRet) } /* If no zContent option was specified, fill in the default values. */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pRet)).FzContent == uintptr(0) { zTail = uintptr(0) _ = libc.Int32FromInt32(0) if (*TFts5Config)(unsafe.Pointer(pRet)).FeContent == FTS5_CONTENT_NORMAL { zTail = __ccgo_ts + 35093 } else { if (*TFts5Config)(unsafe.Pointer(pRet)).FbColumnsize != 0 { zTail = __ccgo_ts + 35660 } } if zTail != 0 { (*TFts5Config)(unsafe.Pointer(pRet)).FzContent = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+35668, libc.VaList(bp+32, (*TFts5Config)(unsafe.Pointer(pRet)).FzDb, (*TFts5Config)(unsafe.Pointer(pRet)).FzName, zTail)) } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pRet)).FzContentRowid == uintptr(0) { (*TFts5Config)(unsafe.Pointer(pRet)).FzContentRowid = _sqlite3Fts5Strndup(tls, bp, __ccgo_ts+16694, -int32(1)) } /* Formulate the zContentExprlist text */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _fts5ConfigMakeExprlist(tls, pRet) } if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { _sqlite3Fts5ConfigFree(tls, pRet) *(*uintptr)(unsafe.Pointer(ppOut)) = uintptr(0) } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Free the configuration object passed as the only argument. // */ func _sqlite3Fts5ConfigFree(tls *libc.TLS, pConfig uintptr) { var i int32 _ = i if pConfig != 0 { if (*TFts5Config)(unsafe.Pointer(pConfig)).FpTok != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tfts5_tokenizer)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(pConfig)).FpTokApi)).FxDelete})))(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FpTok) } Xsqlite3_free(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb) Xsqlite3_free(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName) i = 0 for { if !(i < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) { break } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(pConfig)).FazCol + uintptr(i)*4))) goto _1 _1: ; i++ } Xsqlite3_free(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FazCol) Xsqlite3_free(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FaPrefix) Xsqlite3_free(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FzRank) Xsqlite3_free(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FzRankArgs) Xsqlite3_free(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FzContent) Xsqlite3_free(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FzContentRowid) Xsqlite3_free(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FzContentExprlist) Xsqlite3_free(tls, pConfig) } } // C documentation // // /* // ** Call sqlite3_declare_vtab() based on the contents of the configuration // ** object passed as the only argument. Return SQLITE_OK if successful, or // ** an SQLite error code if an error occurs. // */ func _sqlite3Fts5ConfigDeclareVtab(tls *libc.TLS, pConfig uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var i int32 var zSep, zSql, v2 uintptr var _ /* rc at bp+0 */ int32 _, _, _, _ = i, zSep, zSql, v2 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK zSql = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+35679, 0) i = 0 for { if !(zSql != 0 && i < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) { break } if i == 0 { v2 = __ccgo_ts + 1648 } else { v2 = __ccgo_ts + 14990 } zSep = v2 zSql = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+35695, libc.VaList(bp+16, zSql, zSep, *(*uintptr)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(pConfig)).FazCol + uintptr(i)*4)))) goto _1 _1: ; i++ } zSql = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+35702, libc.VaList(bp+16, zSql, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName, __ccgo_ts+35410)) _ = libc.Int32FromInt32(0) if zSql != 0 { *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_declare_vtab(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb, zSql) Xsqlite3_free(tls, zSql) } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Tokenize the text passed via the second and third arguments. // ** // ** The callback is invoked once for each token in the input text. The // ** arguments passed to it are, in order: // ** // ** void *pCtx // Copy of 4th argument to sqlite3Fts5Tokenize() // ** const char *pToken // Pointer to buffer containing token // ** int nToken // Size of token in bytes // ** int iStart // Byte offset of start of token within input text // ** int iEnd // Byte offset of end of token within input text // ** int iPos // Position of token in input (first token is 0) // ** // ** If the callback returns a non-zero value the tokenization is abandoned // ** and no further callbacks are issued. // ** // ** This function returns SQLITE_OK if successful or an SQLite error code // ** if an error occurs. If the tokenization was abandoned early because // ** the callback returned SQLITE_DONE, this is not an error and this function // ** still returns SQLITE_OK. Or, if the tokenization was abandoned early // ** because the callback returned another non-zero value, it is assumed // ** to be an SQLite error code and returned to the caller. // */ func _sqlite3Fts5Tokenize(tls *libc.TLS, pConfig uintptr, flags int32, pText uintptr, nText int32, pCtx uintptr, xToken uintptr) (r int32) { if pText == uintptr(0) { return SQLITE_OK } return (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tfts5_tokenizer)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(pConfig)).FpTokApi)).FxTokenize})))(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FpTok, pCtx, flags, pText, nText, xToken) } // C documentation // // /* // ** Argument pIn points to the first character in what is expected to be // ** a comma-separated list of SQL literals followed by a ')' character. // ** If it actually is this, return a pointer to the ')'. Otherwise, return // ** NULL to indicate a parse error. // */ func _fts5ConfigSkipArgs(tls *libc.TLS, pIn uintptr) (r uintptr) { var p uintptr _ = p p = pIn for int32(1) != 0 { p = _fts5ConfigSkipWhitespace(tls, p) p = _fts5ConfigSkipLiteral(tls, p) p = _fts5ConfigSkipWhitespace(tls, p) if p == uintptr(0) || int32(*(*uint8)(unsafe.Pointer(p))) == int32(')') { break } if int32(*(*uint8)(unsafe.Pointer(p))) != int32(',') { p = uintptr(0) break } p++ } return p } // C documentation // // /* // ** Parameter zIn contains a rank() function specification. The format of // ** this is: // ** // ** + Bareword (function name) // ** + Open parenthesis - "(" // ** + Zero or more SQL literals in a comma separated list // ** + Close parenthesis - ")" // */ func _sqlite3Fts5ConfigParseRank(tls *libc.TLS, zIn uintptr, pzRank uintptr, pzRankArgs uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var p, pArgs, pRank, zRank, zRankArgs uintptr var _ /* rc at bp+0 */ int32 _, _, _, _, _ = p, pArgs, pRank, zRank, zRankArgs p = zIn zRank = uintptr(0) zRankArgs = uintptr(0) *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*uintptr)(unsafe.Pointer(pzRank)) = uintptr(0) *(*uintptr)(unsafe.Pointer(pzRankArgs)) = uintptr(0) if p == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { p = _fts5ConfigSkipWhitespace(tls, p) pRank = p p = _fts5ConfigSkipBareword(tls, p) if p != 0 { zRank = _sqlite3Fts5MallocZero(tls, bp, int64(int32(uintptr(1)+p)-int32(pRank))) if zRank != 0 { libc.Xmemcpy(tls, zRank, pRank, uint32(int32(p)-int32(pRank))) } } else { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { p = _fts5ConfigSkipWhitespace(tls, p) if int32(*(*uint8)(unsafe.Pointer(p))) != int32('(') { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } p++ } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { p = _fts5ConfigSkipWhitespace(tls, p) pArgs = p if int32(*(*uint8)(unsafe.Pointer(p))) != int32(')') { p = _fts5ConfigSkipArgs(tls, p) if p == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { zRankArgs = _sqlite3Fts5MallocZero(tls, bp, int64(int32(uintptr(1)+p)-int32(pArgs))) if zRankArgs != 0 { libc.Xmemcpy(tls, zRankArgs, pArgs, uint32(int32(p)-int32(pArgs))) } } } } } if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { Xsqlite3_free(tls, zRank) _ = libc.Int32FromInt32(0) } else { *(*uintptr)(unsafe.Pointer(pzRank)) = zRank *(*uintptr)(unsafe.Pointer(pzRankArgs)) = zRankArgs } return *(*int32)(unsafe.Pointer(bp)) } func _sqlite3Fts5ConfigSetValue(tls *libc.TLS, pConfig uintptr, zKey uintptr, pVal uintptr, pbBadkey uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bVal, nAutomerge, nCrisisMerge, nHashSize, nUsermerge, nVal, pgsz, rc, v1 int32 var zIn uintptr var _ /* zRank at bp+0 */ uintptr var _ /* zRankArgs at bp+4 */ uintptr _, _, _, _, _, _, _, _, _, _ = bVal, nAutomerge, nCrisisMerge, nHashSize, nUsermerge, nVal, pgsz, rc, zIn, v1 rc = SQLITE_OK if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+35728) { pgsz = 0 if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) { pgsz = Xsqlite3_value_int(tls, pVal) } if pgsz < int32(32) || pgsz > libc.Int32FromInt32(64)*libc.Int32FromInt32(1024) { *(*int32)(unsafe.Pointer(pbBadkey)) = int32(1) } else { (*TFts5Config)(unsafe.Pointer(pConfig)).Fpgsz = pgsz } } else { if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+35733) { nHashSize = -int32(1) if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) { nHashSize = Xsqlite3_value_int(tls, pVal) } if nHashSize <= 0 { *(*int32)(unsafe.Pointer(pbBadkey)) = int32(1) } else { (*TFts5Config)(unsafe.Pointer(pConfig)).FnHashSize = nHashSize } } else { if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+35742) { nAutomerge = -int32(1) if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) { nAutomerge = Xsqlite3_value_int(tls, pVal) } if nAutomerge < 0 || nAutomerge > int32(64) { *(*int32)(unsafe.Pointer(pbBadkey)) = int32(1) } else { if nAutomerge == int32(1) { nAutomerge = int32(FTS5_DEFAULT_AUTOMERGE) } (*TFts5Config)(unsafe.Pointer(pConfig)).FnAutomerge = nAutomerge } } else { if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+35752) { nUsermerge = -int32(1) if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) { nUsermerge = Xsqlite3_value_int(tls, pVal) } if nUsermerge < int32(2) || nUsermerge > int32(16) { *(*int32)(unsafe.Pointer(pbBadkey)) = int32(1) } else { (*TFts5Config)(unsafe.Pointer(pConfig)).FnUsermerge = nUsermerge } } else { if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+35762) { nCrisisMerge = -int32(1) if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) { nCrisisMerge = Xsqlite3_value_int(tls, pVal) } if nCrisisMerge < 0 { *(*int32)(unsafe.Pointer(pbBadkey)) = int32(1) } else { if nCrisisMerge <= int32(1) { nCrisisMerge = int32(FTS5_DEFAULT_CRISISMERGE) } if nCrisisMerge >= int32(FTS5_MAX_SEGMENT) { nCrisisMerge = libc.Int32FromInt32(FTS5_MAX_SEGMENT) - libc.Int32FromInt32(1) } (*TFts5Config)(unsafe.Pointer(pConfig)).FnCrisisMerge = nCrisisMerge } } else { if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+35774) { nVal = -int32(1) if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) { nVal = Xsqlite3_value_int(tls, pVal) } else { *(*int32)(unsafe.Pointer(pbBadkey)) = int32(1) } if nVal < 0 { nVal = int32(FTS5_DEFAULT_DELETE_AUTOMERGE) } if nVal > int32(100) { nVal = 0 } (*TFts5Config)(unsafe.Pointer(pConfig)).FnDeleteMerge = nVal } else { if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+35410) { zIn = Xsqlite3_value_text(tls, pVal) rc = _sqlite3Fts5ConfigParseRank(tls, zIn, bp, bp+4) if rc == SQLITE_OK { Xsqlite3_free(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FzRank) Xsqlite3_free(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FzRankArgs) (*TFts5Config)(unsafe.Pointer(pConfig)).FzRank = *(*uintptr)(unsafe.Pointer(bp)) (*TFts5Config)(unsafe.Pointer(pConfig)).FzRankArgs = *(*uintptr)(unsafe.Pointer(bp + 4)) } else { if rc == int32(SQLITE_ERROR) { rc = SQLITE_OK *(*int32)(unsafe.Pointer(pbBadkey)) = int32(1) } } } else { if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+35786) { bVal = -int32(1) if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) { bVal = Xsqlite3_value_int(tls, pVal) } if bVal < 0 { *(*int32)(unsafe.Pointer(pbBadkey)) = int32(1) } else { if bVal != 0 { v1 = int32(1) } else { v1 = 0 } (*TFts5Config)(unsafe.Pointer(pConfig)).FbSecureDelete = v1 } } else { *(*int32)(unsafe.Pointer(pbBadkey)) = int32(1) } } } } } } } } return rc } // C documentation // // /* // ** Load the contents of the %_config table into memory. // */ func _sqlite3Fts5ConfigLoad(tls *libc.TLS, pConfig uintptr, iCookie int32) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var iVersion int32 var pVal, zK, zSelect, zSql uintptr var _ /* bDummy at bp+8 */ int32 var _ /* p at bp+0 */ uintptr var _ /* rc at bp+4 */ int32 _, _, _, _, _ = iVersion, pVal, zK, zSelect, zSql zSelect = __ccgo_ts + 35800 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 4)) = SQLITE_OK iVersion = 0 /* Set default values */ (*TFts5Config)(unsafe.Pointer(pConfig)).Fpgsz = int32(FTS5_DEFAULT_PAGE_SIZE) (*TFts5Config)(unsafe.Pointer(pConfig)).FnAutomerge = int32(FTS5_DEFAULT_AUTOMERGE) (*TFts5Config)(unsafe.Pointer(pConfig)).FnUsermerge = int32(FTS5_DEFAULT_USERMERGE) (*TFts5Config)(unsafe.Pointer(pConfig)).FnCrisisMerge = int32(FTS5_DEFAULT_CRISISMERGE) (*TFts5Config)(unsafe.Pointer(pConfig)).FnHashSize = libc.Int32FromInt32(1024) * libc.Int32FromInt32(1024) (*TFts5Config)(unsafe.Pointer(pConfig)).FnDeleteMerge = int32(FTS5_DEFAULT_DELETE_AUTOMERGE) zSql = _sqlite3Fts5Mprintf(tls, bp+4, zSelect, libc.VaList(bp+24, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName)) if zSql != 0 { *(*int32)(unsafe.Pointer(bp + 4)) = Xsqlite3_prepare_v2(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb, zSql, -int32(1), bp, uintptr(0)) Xsqlite3_free(tls, zSql) } _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK { for int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) { zK = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), 0) pVal = Xsqlite3_column_value(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)) if 0 == Xsqlite3_stricmp(tls, zK, __ccgo_ts+35832) { iVersion = Xsqlite3_value_int(tls, pVal) } else { *(*int32)(unsafe.Pointer(bp + 8)) = 0 _sqlite3Fts5ConfigSetValue(tls, pConfig, zK, pVal, bp+8) } } *(*int32)(unsafe.Pointer(bp + 4)) = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) } if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK && iVersion != int32(FTS5_CURRENT_VERSION) && iVersion != int32(FTS5_CURRENT_VERSION_SECUREDELETE) { *(*int32)(unsafe.Pointer(bp + 4)) = int32(SQLITE_ERROR) if (*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg != 0 { _ = libc.Int32FromInt32(0) *(*uintptr)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+35840, libc.VaList(bp+24, iVersion, int32(FTS5_CURRENT_VERSION), int32(FTS5_CURRENT_VERSION_SECUREDELETE))) } } else { (*TFts5Config)(unsafe.Pointer(pConfig)).FiVersion = iVersion } if *(*int32)(unsafe.Pointer(bp + 4)) == SQLITE_OK { (*TFts5Config)(unsafe.Pointer(pConfig)).FiCookie = iCookie } return *(*int32)(unsafe.Pointer(bp + 4)) } /* ** 2014 May 31 ** ** 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. ** ****************************************************************************** ** */ /* #include "fts5Int.h" */ /* #include "fts5parse.h" */ /* ** All token types in the generated fts5parse.h file are greater than 0. */ type TFts5ExprTerm = struct { FbPrefix Tu8 FbFirst Tu8 FpTerm uintptr FnQueryTerm int32 FnFullTerm int32 FpIter uintptr FpSynonym uintptr } type Fts5ExprTerm = TFts5ExprTerm type TFts5Expr1 = struct { FpIndex uintptr FpConfig uintptr FpRoot uintptr FbDesc int32 FnPhrase int32 FapExprPhrase uintptr } type Fts5Expr1 = TFts5Expr1 /* ** eType: ** Expression node type. Always one of: ** ** FTS5_AND (nChild, apChild valid) ** FTS5_OR (nChild, apChild valid) ** FTS5_NOT (nChild, apChild valid) ** FTS5_STRING (pNear valid) ** FTS5_TERM (pNear valid) ** ** iHeight: ** Distance from this node to furthest leaf. This is always 0 for nodes ** of type FTS5_STRING and FTS5_TERM. For all other nodes it is one ** greater than the largest child value. */ type TFts5ExprNode1 = struct { F__ccgo_align [0]uint32 FeType int32 FbEof int32 FbNomatch int32 FiHeight int32 FxNext uintptr F__ccgo_align5 [4]byte FiRowid Ti64 FpNear uintptr FnChild int32 FapChild [1]uintptr F__ccgo_pad9 [4]byte } type Fts5ExprNode1 = TFts5ExprNode1 /* ** Invoke the xNext method of an Fts5ExprNode object. This macro should be ** used as if it has the same signature as the xNext() methods themselves. */ /* ** An instance of the following structure represents a single search term ** or term prefix. */ type TFts5ExprTerm1 = struct { FbPrefix Tu8 FbFirst Tu8 FpTerm uintptr FnQueryTerm int32 FnFullTerm int32 FpIter uintptr FpSynonym uintptr } type Fts5ExprTerm1 = TFts5ExprTerm1 /* ** A phrase. One or more terms that must appear in a contiguous sequence ** within a document for it to match. */ type TFts5ExprPhrase1 = struct { FpNode uintptr Fposlist TFts5Buffer FnTerm int32 FaTerm [1]TFts5ExprTerm } type Fts5ExprPhrase1 = TFts5ExprPhrase1 /* ** One or more phrases that must appear within a certain token distance of ** each other within each matching document. */ type TFts5ExprNearset1 = struct { FnNear int32 FpColset uintptr FnPhrase int32 FapPhrase [1]uintptr } type Fts5ExprNearset1 = TFts5ExprNearset1 /* ** Parse context. */ type TFts5Parse1 = struct { FpConfig uintptr FzErr uintptr Frc int32 FnPhrase int32 FapPhrase uintptr FpExpr uintptr FbPhraseToAnd int32 } type Fts5Parse1 = TFts5Parse1 /* ** Check that the Fts5ExprNode.iHeight variables are set correctly in ** the expression tree passed as the only argument. */ func _sqlite3Fts5ParseError(tls *libc.TLS, pParse uintptr, zFmt uintptr, va uintptr) { var ap Tva_list _ = ap ap = va if (*TFts5Parse)(unsafe.Pointer(pParse)).Frc == SQLITE_OK { _ = libc.Int32FromInt32(0) (*TFts5Parse)(unsafe.Pointer(pParse)).FzErr = Xsqlite3_vmprintf(tls, zFmt, ap) (*TFts5Parse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_ERROR) } _ = ap } func _fts5ExprIsspace(tls *libc.TLS, t uint8) (r int32) { return libc.BoolInt32(int32(t) == int32(' ') || int32(t) == int32('\t') || int32(t) == int32('\n') || int32(t) == int32('\r')) } // C documentation // // /* // ** Read the first token from the nul-terminated string at *pz. // */ func _fts5ExprGetToken(tls *libc.TLS, pParse uintptr, pz uintptr, pToken uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var tok int32 var z, z2, z21 uintptr _, _, _, _ = tok, z, z2, z21 z = *(*uintptr)(unsafe.Pointer(pz)) /* Skip past any whitespace */ for _fts5ExprIsspace(tls, *(*uint8)(unsafe.Pointer(z))) != 0 { z++ } (*TFts5Token)(unsafe.Pointer(pToken)).Fp = z (*TFts5Token)(unsafe.Pointer(pToken)).Fn = int32(1) switch int32(*(*uint8)(unsafe.Pointer(z))) { case int32('('): tok = int32(FTS5_LP) case int32(')'): tok = int32(FTS5_RP) case int32('{'): tok = int32(FTS5_LCP) case int32('}'): tok = int32(FTS5_RCP) case int32(':'): tok = int32(FTS5_COLON) case int32(','): tok = int32(FTS5_COMMA) case int32('+'): tok = int32(FTS5_PLUS) case int32('*'): tok = int32(FTS5_STAR) case int32('-'): tok = int32(FTS5_MINUS) case int32('^'): tok = int32(FTS5_CARET) case int32('\000'): tok = FTS5_EOF case int32('"'): tok = int32(FTS5_STRING) z2 = z + 1 for { if !(int32(1) != 0) { break } if int32(*(*uint8)(unsafe.Pointer(z2))) == int32('"') { z2++ if int32(*(*uint8)(unsafe.Pointer(z2))) != int32('"') { break } } if int32(*(*uint8)(unsafe.Pointer(z2))) == int32('\000') { _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+35911, 0) return FTS5_EOF } goto _1 _1: ; z2++ } (*TFts5Token)(unsafe.Pointer(pToken)).Fn = int32(z2) - int32(z) default: if _sqlite3Fts5IsBareword(tls, *(*uint8)(unsafe.Pointer(z))) == 0 { _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+35931, libc.VaList(bp+8, z)) return FTS5_EOF } tok = int32(FTS5_STRING) z21 = z + 1 for { if !(_sqlite3Fts5IsBareword(tls, *(*uint8)(unsafe.Pointer(z21))) != 0) { break } goto _2 _2: ; z21++ } (*TFts5Token)(unsafe.Pointer(pToken)).Fn = int32(z21) - int32(z) if (*TFts5Token)(unsafe.Pointer(pToken)).Fn == int32(2) && libc.Xmemcmp(tls, (*TFts5Token)(unsafe.Pointer(pToken)).Fp, __ccgo_ts+35962, uint32(2)) == 0 { tok = int32(FTS5_OR) } if (*TFts5Token)(unsafe.Pointer(pToken)).Fn == int32(3) && libc.Xmemcmp(tls, (*TFts5Token)(unsafe.Pointer(pToken)).Fp, __ccgo_ts+35965, uint32(3)) == 0 { tok = int32(FTS5_NOT) } if (*TFts5Token)(unsafe.Pointer(pToken)).Fn == int32(3) && libc.Xmemcmp(tls, (*TFts5Token)(unsafe.Pointer(pToken)).Fp, __ccgo_ts+30706, uint32(3)) == 0 { tok = int32(FTS5_AND) } break } *(*uintptr)(unsafe.Pointer(pz)) = (*TFts5Token)(unsafe.Pointer(pToken)).Fp + uintptr((*TFts5Token)(unsafe.Pointer(pToken)).Fn) return tok } func _fts5ParseAlloc(tls *libc.TLS, t Tu64) (r uintptr) { return Xsqlite3_malloc64(tls, uint64(int64(t))) } func _fts5ParseFree(tls *libc.TLS, p uintptr) { Xsqlite3_free(tls, p) } func _sqlite3Fts5ExprNew(tls *libc.TLS, pConfig uintptr, bPhraseToAnd int32, iCol int32, zExpr uintptr, ppNew uintptr, pzErr uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var n, nByte, t int32 var pColset, pEngine, pNew, v1 uintptr var _ /* sParse at bp+0 */ TFts5Parse var _ /* token at bp+32 */ TFts5Token var _ /* z at bp+40 */ uintptr _, _, _, _, _, _, _ = n, nByte, pColset, pEngine, pNew, t, v1 *(*uintptr)(unsafe.Pointer(bp + 40)) = zExpr *(*uintptr)(unsafe.Pointer(ppNew)) = uintptr(0) *(*uintptr)(unsafe.Pointer(pzErr)) = uintptr(0) libc.Xmemset(tls, bp, 0, uint32(28)) (*(*TFts5Parse)(unsafe.Pointer(bp))).FbPhraseToAnd = bPhraseToAnd pEngine = _sqlite3Fts5ParserAlloc(tls, __ccgo_fp(_fts5ParseAlloc)) if pEngine == uintptr(0) { return int32(SQLITE_NOMEM) } (*(*TFts5Parse)(unsafe.Pointer(bp))).FpConfig = pConfig for cond := true; cond; cond = (*(*TFts5Parse)(unsafe.Pointer(bp))).Frc == SQLITE_OK && t != FTS5_EOF { t = _fts5ExprGetToken(tls, bp, bp+40, bp+32) _sqlite3Fts5Parser(tls, pEngine, t, *(*TFts5Token)(unsafe.Pointer(bp + 32)), bp) } _sqlite3Fts5ParserFree(tls, pEngine, __ccgo_fp(_fts5ParseFree)) /* If the LHS of the MATCH expression was a user column, apply the ** implicit column-filter. */ if iCol < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol && (*(*TFts5Parse)(unsafe.Pointer(bp))).FpExpr != 0 && (*(*TFts5Parse)(unsafe.Pointer(bp))).Frc == SQLITE_OK { n = int32(8) pColset = _sqlite3Fts5MallocZero(tls, bp+8, int64(n)) if pColset != 0 { (*TFts5Colset)(unsafe.Pointer(pColset)).FnCol = int32(1) *(*int32)(unsafe.Pointer(pColset + 4)) = iCol _sqlite3Fts5ParseSetColset(tls, bp, (*(*TFts5Parse)(unsafe.Pointer(bp))).FpExpr, pColset) } } _ = libc.Int32FromInt32(0) if (*(*TFts5Parse)(unsafe.Pointer(bp))).Frc == SQLITE_OK { v1 = Xsqlite3_malloc(tls, int32(24)) pNew = v1 *(*uintptr)(unsafe.Pointer(ppNew)) = v1 if pNew == uintptr(0) { (*(*TFts5Parse)(unsafe.Pointer(bp))).Frc = int32(SQLITE_NOMEM) _sqlite3Fts5ParseNodeFree(tls, (*(*TFts5Parse)(unsafe.Pointer(bp))).FpExpr) } else { if !((*(*TFts5Parse)(unsafe.Pointer(bp))).FpExpr != 0) { nByte = int32(48) (*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot = _sqlite3Fts5MallocZero(tls, bp+8, int64(nByte)) if (*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot != 0 { (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FbEof = int32(1) } } else { (*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot = (*(*TFts5Parse)(unsafe.Pointer(bp))).FpExpr } (*TFts5Expr)(unsafe.Pointer(pNew)).FpIndex = uintptr(0) (*TFts5Expr)(unsafe.Pointer(pNew)).FpConfig = pConfig (*TFts5Expr)(unsafe.Pointer(pNew)).FapExprPhrase = (*(*TFts5Parse)(unsafe.Pointer(bp))).FapPhrase (*TFts5Expr)(unsafe.Pointer(pNew)).FnPhrase = (*(*TFts5Parse)(unsafe.Pointer(bp))).FnPhrase (*TFts5Expr)(unsafe.Pointer(pNew)).FbDesc = 0 (*(*TFts5Parse)(unsafe.Pointer(bp))).FapPhrase = uintptr(0) } } else { _sqlite3Fts5ParseNodeFree(tls, (*(*TFts5Parse)(unsafe.Pointer(bp))).FpExpr) } Xsqlite3_free(tls, (*(*TFts5Parse)(unsafe.Pointer(bp))).FapPhrase) *(*uintptr)(unsafe.Pointer(pzErr)) = (*(*TFts5Parse)(unsafe.Pointer(bp))).FzErr return (*(*TFts5Parse)(unsafe.Pointer(bp))).Frc } // C documentation // // /* // ** Assuming that buffer z is at least nByte bytes in size and contains a // ** valid utf-8 string, return the number of characters in the string. // */ func _fts5ExprCountChar(tls *libc.TLS, z uintptr, nByte int32) (r int32) { var ii, nRet int32 _, _ = ii, nRet nRet = 0 ii = 0 for { if !(ii < nByte) { break } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(ii))))&int32(0xC0) != int32(0x80) { nRet++ } goto _1 _1: ; ii++ } return nRet } // C documentation // // /* // ** This function is only called when using the special 'trigram' tokenizer. // ** Argument zText contains the text of a LIKE or GLOB pattern matched // ** against column iCol. This function creates and compiles an FTS5 MATCH // ** expression that will match a superset of the rows matched by the LIKE or // ** GLOB. If successful, SQLITE_OK is returned. Otherwise, an SQLite error // ** code. // */ func _sqlite3Fts5ExprPattern(tls *libc.TLS, pConfig uintptr, bGlob int32, iCol int32, zText uintptr, pp uintptr) (r int32) { var aSpec [3]uint8 var bAnd, i, iFirst, iOut, jj, rc, v1, v3, v4, v5, v6 int32 var nText Ti64 var zExpr uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aSpec, bAnd, i, iFirst, iOut, jj, nText, rc, zExpr, v1, v3, v4, v5, v6 nText = int64(libc.Xstrlen(tls, zText)) zExpr = Xsqlite3_malloc64(tls, uint64(nText*int64(4)+int64(1))) rc = SQLITE_OK if zExpr == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { iOut = 0 i = 0 iFirst = 0 if bGlob == 0 { aSpec[0] = uint8('_') aSpec[int32(1)] = uint8('%') aSpec[int32(2)] = uint8(0) } else { aSpec[0] = uint8('*') aSpec[int32(1)] = uint8('?') aSpec[int32(2)] = uint8('[') } for int64(i) <= nText { if int64(i) == nText || int32(*(*uint8)(unsafe.Pointer(zText + uintptr(i)))) == int32(aSpec[0]) || int32(*(*uint8)(unsafe.Pointer(zText + uintptr(i)))) == int32(aSpec[int32(1)]) || int32(*(*uint8)(unsafe.Pointer(zText + uintptr(i)))) == int32(aSpec[int32(2)]) { if _fts5ExprCountChar(tls, zText+uintptr(iFirst), i-iFirst) >= int32(3) { v1 = iOut iOut++ *(*uint8)(unsafe.Pointer(zExpr + uintptr(v1))) = uint8('"') jj = iFirst for { if !(jj < i) { break } v3 = iOut iOut++ *(*uint8)(unsafe.Pointer(zExpr + uintptr(v3))) = *(*uint8)(unsafe.Pointer(zText + uintptr(jj))) if int32(*(*uint8)(unsafe.Pointer(zText + uintptr(jj)))) == int32('"') { v4 = iOut iOut++ *(*uint8)(unsafe.Pointer(zExpr + uintptr(v4))) = uint8('"') } goto _2 _2: ; jj++ } v5 = iOut iOut++ *(*uint8)(unsafe.Pointer(zExpr + uintptr(v5))) = uint8('"') v6 = iOut iOut++ *(*uint8)(unsafe.Pointer(zExpr + uintptr(v6))) = uint8(' ') } if int32(*(*uint8)(unsafe.Pointer(zText + uintptr(i)))) == int32(aSpec[int32(2)]) { i += int32(2) if int32(*(*uint8)(unsafe.Pointer(zText + uintptr(i-int32(1))))) == int32('^') { i++ } for int64(i) < nText && int32(*(*uint8)(unsafe.Pointer(zText + uintptr(i)))) != int32(']') { i++ } } iFirst = i + int32(1) } i++ } if iOut > 0 { bAnd = 0 if (*TFts5Config)(unsafe.Pointer(pConfig)).FeDetail != FTS5_DETAIL_FULL { bAnd = int32(1) if (*TFts5Config)(unsafe.Pointer(pConfig)).FeDetail == int32(FTS5_DETAIL_NONE) { iCol = (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol } } *(*uint8)(unsafe.Pointer(zExpr + uintptr(iOut))) = uint8('\000') rc = _sqlite3Fts5ExprNew(tls, pConfig, bAnd, iCol, zExpr, pp, (*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg) } else { *(*uintptr)(unsafe.Pointer(pp)) = uintptr(0) } Xsqlite3_free(tls, zExpr) } return rc } // C documentation // // /* // ** Free the expression node object passed as the only argument. // */ func _sqlite3Fts5ParseNodeFree(tls *libc.TLS, p uintptr) { var i int32 _ = i if p != 0 { i = 0 for { if !(i < (*TFts5ExprNode)(unsafe.Pointer(p)).FnChild) { break } _sqlite3Fts5ParseNodeFree(tls, *(*uintptr)(unsafe.Pointer(p + 40 + uintptr(i)*4))) goto _1 _1: ; i++ } _sqlite3Fts5ParseNearsetFree(tls, (*TFts5ExprNode)(unsafe.Pointer(p)).FpNear) Xsqlite3_free(tls, p) } } // C documentation // // /* // ** Free the expression object passed as the only argument. // */ func _sqlite3Fts5ExprFree(tls *libc.TLS, p uintptr) { if p != 0 { _sqlite3Fts5ParseNodeFree(tls, (*TFts5Expr)(unsafe.Pointer(p)).FpRoot) Xsqlite3_free(tls, (*TFts5Expr)(unsafe.Pointer(p)).FapExprPhrase) Xsqlite3_free(tls, p) } } func _sqlite3Fts5ExprAnd(tls *libc.TLS, pp1 uintptr, p2 uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var ap, p1 uintptr var i, nPhrase int32 var _ /* sParse at bp+0 */ TFts5Parse _, _, _, _ = ap, i, nPhrase, p1 libc.Xmemset(tls, bp, 0, uint32(28)) if *(*uintptr)(unsafe.Pointer(pp1)) != 0 && p2 != 0 { p1 = *(*uintptr)(unsafe.Pointer(pp1)) nPhrase = (*TFts5Expr)(unsafe.Pointer(p1)).FnPhrase + (*TFts5Expr)(unsafe.Pointer(p2)).FnPhrase (*TFts5Expr)(unsafe.Pointer(p1)).FpRoot = _sqlite3Fts5ParseNode(tls, bp, int32(FTS5_AND), (*TFts5Expr)(unsafe.Pointer(p1)).FpRoot, (*TFts5Expr)(unsafe.Pointer(p2)).FpRoot, uintptr(0)) (*TFts5Expr)(unsafe.Pointer(p2)).FpRoot = uintptr(0) if (*(*TFts5Parse)(unsafe.Pointer(bp))).Frc == SQLITE_OK { ap = Xsqlite3_realloc(tls, (*TFts5Expr)(unsafe.Pointer(p1)).FapExprPhrase, int32(uint32(nPhrase)*uint32(4))) if ap == uintptr(0) { (*(*TFts5Parse)(unsafe.Pointer(bp))).Frc = int32(SQLITE_NOMEM) } else { libc.Xmemmove(tls, ap+uintptr((*TFts5Expr)(unsafe.Pointer(p2)).FnPhrase)*4, ap, uint32((*TFts5Expr)(unsafe.Pointer(p1)).FnPhrase)*uint32(4)) i = 0 for { if !(i < (*TFts5Expr)(unsafe.Pointer(p2)).FnPhrase) { break } *(*uintptr)(unsafe.Pointer(ap + uintptr(i)*4)) = *(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(p2)).FapExprPhrase + uintptr(i)*4)) goto _1 _1: ; i++ } (*TFts5Expr)(unsafe.Pointer(p1)).FnPhrase = nPhrase (*TFts5Expr)(unsafe.Pointer(p1)).FapExprPhrase = ap } } Xsqlite3_free(tls, (*TFts5Expr)(unsafe.Pointer(p2)).FapExprPhrase) Xsqlite3_free(tls, p2) } else { if p2 != 0 { *(*uintptr)(unsafe.Pointer(pp1)) = p2 } } return (*(*TFts5Parse)(unsafe.Pointer(bp))).Frc } // C documentation // // /* // ** Argument pTerm must be a synonym iterator. Return the current rowid // ** that it points to. // */ func _fts5ExprSynonymRowid(tls *libc.TLS, pTerm uintptr, bDesc int32, pbEof uintptr) (r Ti64) { var bRetValid int32 var iRet, iRowid Ti64 var p uintptr _, _, _, _ = bRetValid, iRet, iRowid, p iRet = 0 bRetValid = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) p = pTerm for { if !(p != 0) { break } if 0 == int32((*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter)).FbEof) { iRowid = (*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter)).FiRowid if bRetValid == 0 || bDesc != libc.BoolInt32(iRowid < iRet) { iRet = iRowid bRetValid = int32(1) } } goto _1 _1: ; p = (*TFts5ExprTerm)(unsafe.Pointer(p)).FpSynonym } if pbEof != 0 && bRetValid == 0 { *(*int32)(unsafe.Pointer(pbEof)) = int32(1) } return iRet } // C documentation // // /* // ** Argument pTerm must be a synonym iterator. // */ func _fts5ExprSynonymList(tls *libc.TLS, pTerm uintptr, iRowid Ti64, pBuf uintptr, pa uintptr, pn uintptr) (r int32) { bp := tls.Alloc(112) defer tls.Free(112) var aIter, aNew, p, pIter uintptr var i, nAlloc, nIter, rc int32 var iMin, iPrev Ti64 var nByte Tsqlite3_int64 var _ /* aStatic at bp+0 */ [4]TFts5PoslistReader var _ /* writer at bp+96 */ TFts5PoslistWriter _, _, _, _, _, _, _, _, _, _, _ = aIter, aNew, i, iMin, iPrev, nAlloc, nByte, nIter, p, pIter, rc aIter = bp nIter = 0 nAlloc = int32(4) rc = SQLITE_OK _ = libc.Int32FromInt32(0) p = pTerm for { if !(p != 0) { break } pIter = (*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter if int32((*TFts5IndexIter)(unsafe.Pointer(pIter)).FbEof) == 0 && (*TFts5IndexIter)(unsafe.Pointer(pIter)).FiRowid == iRowid { if (*TFts5IndexIter)(unsafe.Pointer(pIter)).FnData == 0 { goto _1 } if nIter == nAlloc { nByte = int64(uint32(24) * uint32(nAlloc) * uint32(2)) aNew = Xsqlite3_malloc64(tls, uint64(nByte)) if aNew == uintptr(0) { rc = int32(SQLITE_NOMEM) goto synonym_poslist_out } libc.Xmemcpy(tls, aNew, aIter, uint32(24)*uint32(nIter)) nAlloc = nAlloc * int32(2) if aIter != bp { Xsqlite3_free(tls, aIter) } aIter = aNew } _sqlite3Fts5PoslistReaderInit(tls, (*TFts5IndexIter)(unsafe.Pointer(pIter)).FpData, (*TFts5IndexIter)(unsafe.Pointer(pIter)).FnData, aIter+uintptr(nIter)*24) _ = libc.Int32FromInt32(0) nIter++ } goto _1 _1: ; p = (*TFts5ExprTerm)(unsafe.Pointer(p)).FpSynonym } if nIter == int32(1) { *(*uintptr)(unsafe.Pointer(pa)) = (*(*TFts5PoslistReader)(unsafe.Pointer(aIter))).Fa *(*int32)(unsafe.Pointer(pn)) = (*(*TFts5PoslistReader)(unsafe.Pointer(aIter))).Fn } else { *(*TFts5PoslistWriter)(unsafe.Pointer(bp + 96)) = TFts5PoslistWriter{} iPrev = int64(-int32(1)) _sqlite3Fts5BufferZero(tls, pBuf) for int32(1) != 0 { iMin = libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)<poslist buffer accordingly. Output parameter *pbMatch // ** is set to true if this is really a match, or false otherwise. // ** // ** SQLITE_OK is returned if an error occurs, or an SQLite error code // ** otherwise. It is not considered an error code if the current rowid is // ** not a match. // */ func _fts5ExprPhraseIsMatch(tls *libc.TLS, pNode uintptr, pPhrase uintptr, pbMatch uintptr) (r int32) { bp := tls.Alloc(128) defer tls.Free(128) var aIter, pPos, pTerm uintptr var bFirst, bFlag, bMatch, i, rc int32 var iAdj, iPos Ti64 var nByte Tsqlite3_int64 var _ /* a at bp+108 */ uintptr var _ /* aStatic at bp+8 */ [4]TFts5PoslistReader var _ /* buf at bp+112 */ TFts5Buffer var _ /* n at bp+104 */ int32 var _ /* writer at bp+0 */ TFts5PoslistWriter _, _, _, _, _, _, _, _, _, _, _ = aIter, bFirst, bFlag, bMatch, i, iAdj, iPos, nByte, pPos, pTerm, rc *(*TFts5PoslistWriter)(unsafe.Pointer(bp)) = TFts5PoslistWriter{} aIter = bp + 8 rc = SQLITE_OK bFirst = int32((*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20))).FbFirst) _sqlite3Fts5BufferZero(tls, pPhrase+4) /* If the aStatic[] array is not large enough, allocate a large array ** using sqlite3_malloc(). This approach could be improved upon. */ if (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm > int32(libc.Uint32FromInt64(96)/libc.Uint32FromInt64(24)) { nByte = int64(uint32(24) * uint32((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm)) aIter = Xsqlite3_malloc64(tls, uint64(nByte)) if !(aIter != 0) { return int32(SQLITE_NOMEM) } } libc.Xmemset(tls, aIter, 0, uint32(24)*uint32((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm)) /* Initialize a term iterator for each term in the phrase */ i = 0 for { if !(i < (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm) { break } pTerm = pPhrase + 20 + uintptr(i)*24 *(*int32)(unsafe.Pointer(bp + 104)) = 0 bFlag = 0 *(*uintptr)(unsafe.Pointer(bp + 108)) = uintptr(0) if (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpSynonym != 0 { *(*TFts5Buffer)(unsafe.Pointer(bp + 112)) = TFts5Buffer{} rc = _fts5ExprSynonymList(tls, pTerm, (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid, bp+112, bp+108, bp+104) if rc != 0 { Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 108))) goto ismatch_out } if *(*uintptr)(unsafe.Pointer(bp + 108)) == (*(*TFts5Buffer)(unsafe.Pointer(bp + 112))).Fp { bFlag = int32(1) } } else { *(*uintptr)(unsafe.Pointer(bp + 108)) = (*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpIter)).FpData *(*int32)(unsafe.Pointer(bp + 104)) = (*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpIter)).FnData } _sqlite3Fts5PoslistReaderInit(tls, *(*uintptr)(unsafe.Pointer(bp + 108)), *(*int32)(unsafe.Pointer(bp + 104)), aIter+uintptr(i)*24) (*(*TFts5PoslistReader)(unsafe.Pointer(aIter + uintptr(i)*24))).FbFlag = uint8(bFlag) if (*(*TFts5PoslistReader)(unsafe.Pointer(aIter + uintptr(i)*24))).FbEof != 0 { goto ismatch_out } goto _1 _1: ; i++ } for int32(1) != 0 { iPos = (*(*TFts5PoslistReader)(unsafe.Pointer(aIter))).FiPos for cond := true; cond; cond = bMatch == 0 { bMatch = int32(1) i = 0 for { if !(i < (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm) { break } pPos = aIter + uintptr(i)*24 iAdj = iPos + int64(i) if (*TFts5PoslistReader)(unsafe.Pointer(pPos)).FiPos != iAdj { bMatch = 0 for (*TFts5PoslistReader)(unsafe.Pointer(pPos)).FiPos < iAdj { if _sqlite3Fts5PoslistReaderNext(tls, pPos) != 0 { goto ismatch_out } } if (*TFts5PoslistReader)(unsafe.Pointer(pPos)).FiPos > iAdj { iPos = (*TFts5PoslistReader)(unsafe.Pointer(pPos)).FiPos - int64(i) } } goto _2 _2: ; i++ } } /* Append position iPos to the output */ if bFirst == 0 || int32(iPos&libc.Int64FromInt32(0x7FFFFFFF)) == 0 { rc = _sqlite3Fts5PoslistWriterAppend(tls, pPhrase+4, bp, iPos) if rc != SQLITE_OK { goto ismatch_out } } i = 0 for { if !(i < (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm) { break } if _sqlite3Fts5PoslistReaderNext(tls, aIter+uintptr(i)*24) != 0 { goto ismatch_out } goto _3 _3: ; i++ } } goto ismatch_out ismatch_out: ; *(*int32)(unsafe.Pointer(pbMatch)) = libc.BoolInt32((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fn > 0) i = 0 for { if !(i < (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm) { break } if (*(*TFts5PoslistReader)(unsafe.Pointer(aIter + uintptr(i)*24))).FbFlag != 0 { Xsqlite3_free(tls, (*(*TFts5PoslistReader)(unsafe.Pointer(aIter + uintptr(i)*24))).Fa) } goto _4 _4: ; i++ } if aIter != bp+8 { Xsqlite3_free(tls, aIter) } return rc } type TFts5LookaheadReader = struct { F__ccgo_align [0]uint32 Fa uintptr Fn int32 Fi int32 F__ccgo_align3 [4]byte FiPos Ti64 FiLookahead Ti64 } type Fts5LookaheadReader = TFts5LookaheadReader type TFts5LookaheadReader1 = struct { F__ccgo_align [0]uint32 Fa uintptr Fn int32 Fi int32 F__ccgo_align3 [4]byte FiPos Ti64 FiLookahead Ti64 } type Fts5LookaheadReader1 = TFts5LookaheadReader1 func _fts5LookaheadReaderNext(tls *libc.TLS, p uintptr) (r int32) { (*TFts5LookaheadReader)(unsafe.Pointer(p)).FiPos = (*TFts5LookaheadReader)(unsafe.Pointer(p)).FiLookahead if _sqlite3Fts5PoslistNext64(tls, (*TFts5LookaheadReader)(unsafe.Pointer(p)).Fa, (*TFts5LookaheadReader)(unsafe.Pointer(p)).Fn, p+8, p+24) != 0 { (*TFts5LookaheadReader)(unsafe.Pointer(p)).FiLookahead = libc.Int64FromInt32(1) << libc.Int32FromInt32(62) } return libc.BoolInt32((*TFts5LookaheadReader)(unsafe.Pointer(p)).FiPos == libc.Int64FromInt32(1)< int32(libc.Uint32FromInt64(192)/libc.Uint32FromInt64(48)) { nByte = int64(uint32(48) * uint32((*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase)) a = _sqlite3Fts5MallocZero(tls, bp+192, nByte) } else { libc.Xmemset(tls, bp, 0, uint32(192)) } if *(*int32)(unsafe.Pointer(bp + 192)) != SQLITE_OK { *(*int32)(unsafe.Pointer(pRc)) = *(*int32)(unsafe.Pointer(bp + 192)) return 0 } /* Initialize a lookahead iterator for each phrase. After passing the ** buffer and buffer size to the lookaside-reader init function, zero ** the phrase poslist buffer. The new poslist for the phrase (containing ** the same entries as the original with some entries removed on account ** of the NEAR constraint) is written over the original even as it is ** being read. This is safe as the entries for the new poslist are a ** subset of the old, so it is not possible for data yet to be read to ** be overwritten. */ i = 0 for { if !(i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) { break } pPoslist = *(*uintptr)(unsafe.Pointer(apPhrase + uintptr(i)*4)) + 4 _fts5LookaheadReaderInit(tls, (*TFts5Buffer)(unsafe.Pointer(pPoslist)).Fp, (*TFts5Buffer)(unsafe.Pointer(pPoslist)).Fn, a+uintptr(i)*48) (*TFts5Buffer)(unsafe.Pointer(pPoslist)).Fn = 0 (*(*TFts5NearTrimmer)(unsafe.Pointer(a + uintptr(i)*48))).FpOut = pPoslist goto _1 _1: ; i++ } for int32(1) != 0 { /* This block advances the phrase iterators until they point to a set of ** entries that together comprise a match. */ iMax = (*(*TFts5NearTrimmer)(unsafe.Pointer(a))).Freader.FiPos for cond := true; cond; cond = bMatch == 0 { bMatch = int32(1) i = 0 for { if !(i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) { break } pPos = a + uintptr(i)*48 iMin = iMax - int64((*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12 + uintptr(i)*4)))).FnTerm) - int64((*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnNear) if (*TFts5LookaheadReader)(unsafe.Pointer(pPos)).FiPos < iMin || (*TFts5LookaheadReader)(unsafe.Pointer(pPos)).FiPos > iMax { bMatch = 0 for (*TFts5LookaheadReader)(unsafe.Pointer(pPos)).FiPos < iMin { if _fts5LookaheadReaderNext(tls, pPos) != 0 { goto ismatch_out } } if (*TFts5LookaheadReader)(unsafe.Pointer(pPos)).FiPos > iMax { iMax = (*TFts5LookaheadReader)(unsafe.Pointer(pPos)).FiPos } } goto _2 _2: ; i++ } } /* Add an entry to each output position list */ i = 0 for { if !(i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) { break } iPos = (*(*TFts5NearTrimmer)(unsafe.Pointer(a + uintptr(i)*48))).Freader.FiPos pWriter = a + uintptr(i)*48 + 32 if (*TFts5Buffer)(unsafe.Pointer((*(*TFts5NearTrimmer)(unsafe.Pointer(a + uintptr(i)*48))).FpOut)).Fn == 0 || iPos != (*TFts5PoslistWriter)(unsafe.Pointer(pWriter)).FiPrev { _sqlite3Fts5PoslistWriterAppend(tls, (*(*TFts5NearTrimmer)(unsafe.Pointer(a + uintptr(i)*48))).FpOut, pWriter, iPos) } goto _3 _3: ; i++ } iAdv = 0 iMin = (*(*TFts5NearTrimmer)(unsafe.Pointer(a))).Freader.FiLookahead i = 0 for { if !(i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) { break } if (*(*TFts5NearTrimmer)(unsafe.Pointer(a + uintptr(i)*48))).Freader.FiLookahead < iMin { iMin = (*(*TFts5NearTrimmer)(unsafe.Pointer(a + uintptr(i)*48))).Freader.FiLookahead iAdv = i } goto _4 _4: ; i++ } if _fts5LookaheadReaderNext(tls, a+uintptr(iAdv)*48) != 0 { goto ismatch_out } } goto ismatch_out ismatch_out: ; bRet = libc.BoolInt32((*TFts5Buffer)(unsafe.Pointer((*(*TFts5NearTrimmer)(unsafe.Pointer(a))).FpOut)).Fn > 0) *(*int32)(unsafe.Pointer(pRc)) = *(*int32)(unsafe.Pointer(bp + 192)) if a != bp { Xsqlite3_free(tls, a) } return bRet return r } // C documentation // // /* // ** Advance iterator pIter until it points to a value equal to or laster // ** than the initial value of *piLast. If this means the iterator points // ** to a value laster than *piLast, update *piLast to the new lastest value. // ** // ** If the iterator reaches EOF, set *pbEof to true before returning. If // ** an error occurs, set *pRc to an error code. If either *pbEof or *pRc // ** are set, return a non-zero value. Otherwise, return zero. // */ func _fts5ExprAdvanceto(tls *libc.TLS, pIter uintptr, bDesc int32, piLast uintptr, pRc uintptr, pbEof uintptr) (r int32) { var iLast, iRowid Ti64 var rc int32 _, _, _ = iLast, iRowid, rc iLast = *(*Ti64)(unsafe.Pointer(piLast)) iRowid = (*TFts5IndexIter)(unsafe.Pointer(pIter)).FiRowid if bDesc == 0 && iLast > iRowid || bDesc != 0 && iLast < iRowid { rc = _sqlite3Fts5IterNextFrom(tls, pIter, iLast) if rc != 0 || (*TFts5IndexIter)(unsafe.Pointer(pIter)).FbEof != 0 { *(*int32)(unsafe.Pointer(pRc)) = rc *(*int32)(unsafe.Pointer(pbEof)) = int32(1) return int32(1) } iRowid = (*TFts5IndexIter)(unsafe.Pointer(pIter)).FiRowid _ = libc.Int32FromInt32(0) } *(*Ti64)(unsafe.Pointer(piLast)) = iRowid return 0 } func _fts5ExprSynonymAdvanceto(tls *libc.TLS, pTerm uintptr, bDesc int32, piLast uintptr, pRc uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iLast, iRowid Ti64 var p uintptr var rc int32 var _ /* bEof at bp+0 */ int32 _, _, _, _ = iLast, iRowid, p, rc rc = SQLITE_OK iLast = *(*Ti64)(unsafe.Pointer(piLast)) *(*int32)(unsafe.Pointer(bp)) = 0 p = pTerm for { if !(rc == SQLITE_OK && p != 0) { break } if int32((*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter)).FbEof) == 0 { iRowid = (*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter)).FiRowid if bDesc == 0 && iLast > iRowid || bDesc != 0 && iLast < iRowid { rc = _sqlite3Fts5IterNextFrom(tls, (*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter, iLast) } } goto _1 _1: ; p = (*TFts5ExprTerm)(unsafe.Pointer(p)).FpSynonym } if rc != SQLITE_OK { *(*int32)(unsafe.Pointer(pRc)) = rc *(*int32)(unsafe.Pointer(bp)) = int32(1) } else { *(*Ti64)(unsafe.Pointer(piLast)) = _fts5ExprSynonymRowid(tls, pTerm, bDesc, bp) } return *(*int32)(unsafe.Pointer(bp)) } func _fts5ExprNearTest(tls *libc.TLS, pRc uintptr, pExpr uintptr, pNode uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i int32 var pIter, pIter1, pNear, pPhrase, pPhrase1, pTerm uintptr var _ /* bMatch at bp+4 */ int32 var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _ = i, pIter, pIter1, pNear, pPhrase, pPhrase1, pTerm pNear = (*TFts5ExprNode)(unsafe.Pointer(pNode)).FpNear *(*int32)(unsafe.Pointer(bp)) = *(*int32)(unsafe.Pointer(pRc)) if (*TFts5Config)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FpConfig)).FeDetail != FTS5_DETAIL_FULL { pPhrase = *(*uintptr)(unsafe.Pointer(pNear + 12)) (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fn = 0 pTerm = pPhrase + 20 for { if !(pTerm != 0) { break } pIter = (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpIter if int32((*TFts5IndexIter)(unsafe.Pointer(pIter)).FbEof) == 0 { if (*TFts5IndexIter)(unsafe.Pointer(pIter)).FiRowid == (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid && (*TFts5IndexIter)(unsafe.Pointer(pIter)).FnData > 0 { (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fn = int32(1) } } goto _1 _1: ; pTerm = (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpSynonym } return (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fn } else { /* Check that each phrase in the nearset matches the current row. ** Populate the pPhrase->poslist buffers at the same time. If any ** phrase is not a match, break out of the loop early. */ i = 0 for { if !(*(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) { break } pPhrase1 = *(*uintptr)(unsafe.Pointer(pNear + 12 + uintptr(i)*4)) if (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase1)).FnTerm > int32(1) || (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase1 + 20))).FpSynonym != 0 || (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FpColset != 0 || (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase1 + 20))).FbFirst != 0 { *(*int32)(unsafe.Pointer(bp + 4)) = 0 *(*int32)(unsafe.Pointer(bp)) = _fts5ExprPhraseIsMatch(tls, pNode, pPhrase1, bp+4) if *(*int32)(unsafe.Pointer(bp + 4)) == 0 { break } } else { pIter1 = (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase1 + 20))).FpIter _sqlite3Fts5BufferSet(tls, bp, pPhrase1+4, (*TFts5IndexIter)(unsafe.Pointer(pIter1)).FnData, (*TFts5IndexIter)(unsafe.Pointer(pIter1)).FpData) } goto _2 _2: ; i++ } *(*int32)(unsafe.Pointer(pRc)) = *(*int32)(unsafe.Pointer(bp)) if i == (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase && (i == int32(1) || _fts5ExprNearIsMatch(tls, pRc, pNear) != 0) { return int32(1) } return 0 } return r } // C documentation // // /* // ** Initialize all term iterators in the pNear object. If any term is found // ** to match no documents at all, return immediately without initializing any // ** further iterators. // ** // ** If an error occurs, return an SQLite error code. Otherwise, return // ** SQLITE_OK. It is not considered an error if some term matches zero // ** documents. // */ func _fts5ExprNearInitAll(tls *libc.TLS, pExpr uintptr, pNode uintptr) (r int32) { var bHit, i, j, rc, v4, v5 int32 var p, pNear, pPhrase, pTerm uintptr _, _, _, _, _, _, _, _, _, _ = bHit, i, j, p, pNear, pPhrase, pTerm, rc, v4, v5 pNear = (*TFts5ExprNode)(unsafe.Pointer(pNode)).FpNear _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) { break } pPhrase = *(*uintptr)(unsafe.Pointer(pNear + 12 + uintptr(i)*4)) if (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm == 0 { (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = int32(1) return SQLITE_OK } else { j = 0 for { if !(j < (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm) { break } pTerm = pPhrase + 20 + uintptr(j)*24 bHit = 0 p = pTerm for { if !(p != 0) { break } if (*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter != 0 { _sqlite3Fts5IterClose(tls, (*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter) (*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter = uintptr(0) } if (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FbPrefix != 0 { v4 = int32(FTS5INDEX_QUERY_PREFIX) } else { v4 = 0 } if (*TFts5Expr)(unsafe.Pointer(pExpr)).FbDesc != 0 { v5 = int32(FTS5INDEX_QUERY_DESC) } else { v5 = 0 } rc = _sqlite3Fts5IndexQuery(tls, (*TFts5Expr)(unsafe.Pointer(pExpr)).FpIndex, (*TFts5ExprTerm)(unsafe.Pointer(p)).FpTerm, (*TFts5ExprTerm)(unsafe.Pointer(p)).FnQueryTerm, v4|v5, (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FpColset, p+16) _ = libc.Int32FromInt32(0) if rc != SQLITE_OK { return rc } if 0 == int32((*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter)).FbEof) { bHit = int32(1) } goto _3 _3: ; p = (*TFts5ExprTerm)(unsafe.Pointer(p)).FpSynonym } if bHit == 0 { (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = int32(1) return SQLITE_OK } goto _2 _2: ; j++ } } goto _1 _1: ; i++ } (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = 0 return SQLITE_OK } // C documentation // // /* // ** If pExpr is an ASC iterator, this function returns a value with the // ** same sign as: // ** // ** (iLhs - iRhs) // ** // ** Otherwise, if this is a DESC iterator, the opposite is returned: // ** // ** (iRhs - iLhs) // */ func _fts5RowidCmp(tls *libc.TLS, pExpr uintptr, iLhs Ti64, iRhs Ti64) (r int32) { _ = libc.Int32FromInt32(0) if (*TFts5Expr)(unsafe.Pointer(pExpr)).FbDesc == 0 { if iLhs < iRhs { return -int32(1) } return libc.BoolInt32(iLhs > iRhs) } else { if iLhs > iRhs { return -int32(1) } return libc.BoolInt32(iLhs < iRhs) } return r } func _fts5ExprSetEof(tls *libc.TLS, pNode uintptr) { var i int32 _ = i (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = int32(1) (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbNomatch = 0 i = 0 for { if !(i < (*TFts5ExprNode)(unsafe.Pointer(pNode)).FnChild) { break } _fts5ExprSetEof(tls, *(*uintptr)(unsafe.Pointer(pNode + 40 + uintptr(i)*4))) goto _1 _1: ; i++ } } func _fts5ExprNodeZeroPoslist(tls *libc.TLS, pNode uintptr) { var i, i1 int32 var pNear, pPhrase uintptr _, _, _, _ = i, i1, pNear, pPhrase if (*TFts5ExprNode)(unsafe.Pointer(pNode)).FeType == int32(FTS5_STRING) || (*TFts5ExprNode)(unsafe.Pointer(pNode)).FeType == int32(FTS5_TERM) { pNear = (*TFts5ExprNode)(unsafe.Pointer(pNode)).FpNear i = 0 for { if !(i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) { break } pPhrase = *(*uintptr)(unsafe.Pointer(pNear + 12 + uintptr(i)*4)) (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fn = 0 goto _1 _1: ; i++ } } else { i1 = 0 for { if !(i1 < (*TFts5ExprNode)(unsafe.Pointer(pNode)).FnChild) { break } _fts5ExprNodeZeroPoslist(tls, *(*uintptr)(unsafe.Pointer(pNode + 40 + uintptr(i1)*4))) goto _2 _2: ; i1++ } } } // C documentation // // /* // ** Compare the values currently indicated by the two nodes as follows: // ** // ** res = (*p1) - (*p2) // ** // ** Nodes that point to values that come later in the iteration order are // ** considered to be larger. Nodes at EOF are the largest of all. // ** // ** This means that if the iteration order is ASC, then numerically larger // ** rowids are considered larger. Or if it is the default DESC, numerically // ** smaller rowids are larger. // */ func _fts5NodeCompare(tls *libc.TLS, pExpr uintptr, p1 uintptr, p2 uintptr) (r int32) { if (*TFts5ExprNode)(unsafe.Pointer(p2)).FbEof != 0 { return -int32(1) } if (*TFts5ExprNode)(unsafe.Pointer(p1)).FbEof != 0 { return +libc.Int32FromInt32(1) } return _fts5RowidCmp(tls, pExpr, (*TFts5ExprNode)(unsafe.Pointer(p1)).FiRowid, (*TFts5ExprNode)(unsafe.Pointer(p2)).FiRowid) } // C documentation // // /* // ** All individual term iterators in pNear are guaranteed to be valid when // ** this function is called. This function checks if all term iterators // ** point to the same rowid, and if not, advances them until they do. // ** If an EOF is reached before this happens, *pbEof is set to true before // ** returning. // ** // ** SQLITE_OK is returned if an error occurs, or an SQLite error code // ** otherwise. It is not considered an error code if an iterator reaches // ** EOF. // */ func _fts5ExprNodeTest_STRING(tls *libc.TLS, pExpr uintptr, pNode uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bDesc, bMatch, i, j int32 var iRowid Ti64 var pIter, pLeft, pNear, pPhrase, pTerm uintptr var _ /* iLast at bp+8 */ Ti64 var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _ = bDesc, bMatch, i, iRowid, j, pIter, pLeft, pNear, pPhrase, pTerm pNear = (*TFts5ExprNode)(unsafe.Pointer(pNode)).FpNear pLeft = *(*uintptr)(unsafe.Pointer(pNear + 12)) *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK /* True if all terms are at the same rowid */ bDesc = (*TFts5Expr)(unsafe.Pointer(pExpr)).FbDesc /* Check that this node should not be FTS5_TERM */ _ = libc.Int32FromInt32(0) /* Initialize iLast, the "lastest" rowid any iterator points to. If the ** iterator skips through rowids in the default ascending order, this means ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it ** means the minimum rowid. */ if (*(*TFts5ExprTerm)(unsafe.Pointer(pLeft + 20))).FpSynonym != 0 { *(*Ti64)(unsafe.Pointer(bp + 8)) = _fts5ExprSynonymRowid(tls, pLeft+20, bDesc, uintptr(0)) } else { *(*Ti64)(unsafe.Pointer(bp + 8)) = (*TFts5IndexIter)(unsafe.Pointer((*(*TFts5ExprTerm)(unsafe.Pointer(pLeft + 20))).FpIter)).FiRowid } for cond := true; cond; cond = bMatch == 0 { bMatch = int32(1) i = 0 for { if !(i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) { break } pPhrase = *(*uintptr)(unsafe.Pointer(pNear + 12 + uintptr(i)*4)) j = 0 for { if !(j < (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm) { break } pTerm = pPhrase + 20 + uintptr(j)*24 if (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpSynonym != 0 { iRowid = _fts5ExprSynonymRowid(tls, pTerm, bDesc, uintptr(0)) if iRowid == *(*Ti64)(unsafe.Pointer(bp + 8)) { goto _2 } bMatch = 0 if _fts5ExprSynonymAdvanceto(tls, pTerm, bDesc, bp+8, bp) != 0 { (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbNomatch = 0 (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = int32(1) return *(*int32)(unsafe.Pointer(bp)) } } else { pIter = (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20 + uintptr(j)*24))).FpIter if (*TFts5IndexIter)(unsafe.Pointer(pIter)).FiRowid == *(*Ti64)(unsafe.Pointer(bp + 8)) || (*TFts5IndexIter)(unsafe.Pointer(pIter)).FbEof != 0 { goto _2 } bMatch = 0 if _fts5ExprAdvanceto(tls, pIter, bDesc, bp+8, bp, pNode+4) != 0 { return *(*int32)(unsafe.Pointer(bp)) } } goto _2 _2: ; j++ } goto _1 _1: ; i++ } } (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid = *(*Ti64)(unsafe.Pointer(bp + 8)) (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbNomatch = libc.BoolInt32(0 == _fts5ExprNearTest(tls, bp, pExpr, pNode) && *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK) _ = libc.Int32FromInt32(0) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Advance the first term iterator in the first phrase of pNear. Set output // ** variable *pbEof to true if it reaches EOF or if an error occurs. // ** // ** Return SQLITE_OK if successful, or an SQLite error code if an error // ** occurs. // */ func _fts5ExprNodeNext_STRING(tls *libc.TLS, pExpr uintptr, pNode uintptr, bFromValid int32, iFrom Ti64) (r int32) { var bEof, rc int32 var iRowid, ii Ti64 var p, pIter, pTerm uintptr _, _, _, _, _, _, _ = bEof, iRowid, ii, p, pIter, pTerm, rc pTerm = *(*uintptr)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer(pNode)).FpNear + 12)) + 20 rc = SQLITE_OK (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbNomatch = 0 if (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpSynonym != 0 { bEof = int32(1) /* Find the firstest rowid any synonym points to. */ iRowid = _fts5ExprSynonymRowid(tls, pTerm, (*TFts5Expr)(unsafe.Pointer(pExpr)).FbDesc, uintptr(0)) /* Advance each iterator that currently points to iRowid. Or, if iFrom ** is valid - each iterator that points to a rowid before iFrom. */ p = pTerm for { if !(p != 0) { break } if int32((*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter)).FbEof) == 0 { ii = (*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter)).FiRowid if ii == iRowid || bFromValid != 0 && ii != iFrom && libc.BoolInt32(ii > iFrom) == (*TFts5Expr)(unsafe.Pointer(pExpr)).FbDesc { if bFromValid != 0 { rc = _sqlite3Fts5IterNextFrom(tls, (*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter, iFrom) } else { rc = _sqlite3Fts5IterNext(tls, (*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter) } if rc != SQLITE_OK { break } if int32((*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter)).FbEof) == 0 { bEof = 0 } } else { bEof = 0 } } goto _1 _1: ; p = (*TFts5ExprTerm)(unsafe.Pointer(p)).FpSynonym } /* Set the EOF flag if either all synonym iterators are at EOF or an ** error has occurred. */ (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = libc.BoolInt32(rc != 0 || bEof != 0) } else { pIter = (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpIter _ = libc.Int32FromInt32(0) if bFromValid != 0 { rc = _sqlite3Fts5IterNextFrom(tls, pIter, iFrom) } else { rc = _sqlite3Fts5IterNext(tls, pIter) } (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = libc.BoolInt32(rc != 0 || (*TFts5IndexIter)(unsafe.Pointer(pIter)).FbEof != 0) } if (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof == 0 { _ = libc.Int32FromInt32(0) rc = _fts5ExprNodeTest_STRING(tls, pExpr, pNode) } return rc } func _fts5ExprNodeTest_TERM(tls *libc.TLS, pExpr uintptr, pNode uintptr) (r int32) { var pIter, pPhrase uintptr _, _ = pIter, pPhrase /* As this "NEAR" object is actually a single phrase that consists ** of a single term only, grab pointers into the poslist managed by the ** fts5_index.c iterator object. This is much faster than synthesizing ** a new poslist the way we have to for more complicated phrase or NEAR ** expressions. */ pPhrase = *(*uintptr)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer(pNode)).FpNear + 12)) pIter = (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20))).FpIter _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fn = (*TFts5IndexIter)(unsafe.Pointer(pIter)).FnData if (*TFts5Config)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FpConfig)).FeDetail == FTS5_DETAIL_FULL { (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fp = (*TFts5IndexIter)(unsafe.Pointer(pIter)).FpData } (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid = (*TFts5IndexIter)(unsafe.Pointer(pIter)).FiRowid (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbNomatch = libc.BoolInt32((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fn == 0) return SQLITE_OK } // C documentation // // /* // ** xNext() method for a node of type FTS5_TERM. // */ func _fts5ExprNodeNext_TERM(tls *libc.TLS, pExpr uintptr, pNode uintptr, bFromValid int32, iFrom Ti64) (r int32) { var pIter uintptr var rc int32 _, _ = pIter, rc pIter = (*(*TFts5ExprTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer(pNode)).FpNear + 12)) + 20))).FpIter _ = libc.Int32FromInt32(0) if bFromValid != 0 { rc = _sqlite3Fts5IterNextFrom(tls, pIter, iFrom) } else { rc = _sqlite3Fts5IterNext(tls, pIter) } if rc == SQLITE_OK && int32((*TFts5IndexIter)(unsafe.Pointer(pIter)).FbEof) == 0 { rc = _fts5ExprNodeTest_TERM(tls, pExpr, pNode) } else { (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = int32(1) (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbNomatch = 0 } return rc } func _fts5ExprNodeTest_OR(tls *libc.TLS, pExpr uintptr, pNode uintptr) { var cmp, i int32 var pChild, pNext uintptr _, _, _, _ = cmp, i, pChild, pNext pNext = *(*uintptr)(unsafe.Pointer(pNode + 40)) i = int32(1) for { if !(i < (*TFts5ExprNode)(unsafe.Pointer(pNode)).FnChild) { break } pChild = *(*uintptr)(unsafe.Pointer(pNode + 40 + uintptr(i)*4)) cmp = _fts5NodeCompare(tls, pExpr, pNext, pChild) if cmp > 0 || cmp == 0 && (*TFts5ExprNode)(unsafe.Pointer(pChild)).FbNomatch == 0 { pNext = pChild } goto _1 _1: ; i++ } (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid = (*TFts5ExprNode)(unsafe.Pointer(pNext)).FiRowid (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = (*TFts5ExprNode)(unsafe.Pointer(pNext)).FbEof (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbNomatch = (*TFts5ExprNode)(unsafe.Pointer(pNext)).FbNomatch } func _fts5ExprNodeNext_OR(tls *libc.TLS, pExpr uintptr, pNode uintptr, bFromValid int32, iFrom Ti64) (r int32) { var i, rc int32 var iLast Ti64 var p1 uintptr _, _, _, _ = i, iLast, p1, rc iLast = (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid i = 0 for { if !(i < (*TFts5ExprNode)(unsafe.Pointer(pNode)).FnChild) { break } p1 = *(*uintptr)(unsafe.Pointer(pNode + 40 + uintptr(i)*4)) _ = libc.Int32FromInt32(0) if (*TFts5ExprNode)(unsafe.Pointer(p1)).FbEof == 0 { if (*TFts5ExprNode)(unsafe.Pointer(p1)).FiRowid == iLast || bFromValid != 0 && _fts5RowidCmp(tls, pExpr, (*TFts5ExprNode)(unsafe.Pointer(p1)).FiRowid, iFrom) < 0 { rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, Ti64) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExprNode)(unsafe.Pointer(p1)).FxNext})))(tls, pExpr, p1, bFromValid, iFrom) if rc != SQLITE_OK { (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbNomatch = 0 return rc } } } goto _1 _1: ; i++ } _fts5ExprNodeTest_OR(tls, pExpr, pNode) return SQLITE_OK } // C documentation // // /* // ** Argument pNode is an FTS5_AND node. // */ func _fts5ExprNodeTest_AND(tls *libc.TLS, pExpr uintptr, pAnd uintptr) (r int32) { var bMatch, cmp, iChild, rc int32 var iLast Ti64 var pChild uintptr _, _, _, _, _, _ = bMatch, cmp, iChild, iLast, pChild, rc iLast = (*TFts5ExprNode)(unsafe.Pointer(pAnd)).FiRowid rc = SQLITE_OK _ = libc.Int32FromInt32(0) for cond := true; cond; cond = bMatch == 0 { (*TFts5ExprNode)(unsafe.Pointer(pAnd)).FbNomatch = 0 bMatch = int32(1) iChild = 0 for { if !(iChild < (*TFts5ExprNode)(unsafe.Pointer(pAnd)).FnChild) { break } pChild = *(*uintptr)(unsafe.Pointer(pAnd + 40 + uintptr(iChild)*4)) cmp = _fts5RowidCmp(tls, pExpr, iLast, (*TFts5ExprNode)(unsafe.Pointer(pChild)).FiRowid) if cmp > 0 { /* Advance pChild until it points to iLast or laster */ rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, Ti64) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExprNode)(unsafe.Pointer(pChild)).FxNext})))(tls, pExpr, pChild, int32(1), iLast) if rc != SQLITE_OK { (*TFts5ExprNode)(unsafe.Pointer(pAnd)).FbNomatch = 0 return rc } } /* If the child node is now at EOF, so is the parent AND node. Otherwise, ** the child node is guaranteed to have advanced at least as far as ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the ** new lastest rowid seen so far. */ _ = libc.Int32FromInt32(0) if (*TFts5ExprNode)(unsafe.Pointer(pChild)).FbEof != 0 { _fts5ExprSetEof(tls, pAnd) bMatch = int32(1) break } else { if iLast != (*TFts5ExprNode)(unsafe.Pointer(pChild)).FiRowid { bMatch = 0 iLast = (*TFts5ExprNode)(unsafe.Pointer(pChild)).FiRowid } } if (*TFts5ExprNode)(unsafe.Pointer(pChild)).FbNomatch != 0 { (*TFts5ExprNode)(unsafe.Pointer(pAnd)).FbNomatch = int32(1) } goto _1 _1: ; iChild++ } } if (*TFts5ExprNode)(unsafe.Pointer(pAnd)).FbNomatch != 0 && pAnd != (*TFts5Expr)(unsafe.Pointer(pExpr)).FpRoot { _fts5ExprNodeZeroPoslist(tls, pAnd) } (*TFts5ExprNode)(unsafe.Pointer(pAnd)).FiRowid = iLast return SQLITE_OK } func _fts5ExprNodeNext_AND(tls *libc.TLS, pExpr uintptr, pNode uintptr, bFromValid int32, iFrom Ti64) (r int32) { var rc int32 _ = rc rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, Ti64) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExprNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNode + 40)))).FxNext})))(tls, pExpr, *(*uintptr)(unsafe.Pointer(pNode + 40)), bFromValid, iFrom) if rc == SQLITE_OK { rc = _fts5ExprNodeTest_AND(tls, pExpr, pNode) } else { (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbNomatch = 0 } return rc } func _fts5ExprNodeTest_NOT(tls *libc.TLS, pExpr uintptr, pNode uintptr) (r int32) { var cmp, rc int32 var p1, p2 uintptr _, _, _, _ = cmp, p1, p2, rc rc = SQLITE_OK p1 = *(*uintptr)(unsafe.Pointer(pNode + 40)) p2 = *(*uintptr)(unsafe.Pointer(pNode + 40 + 1*4)) _ = libc.Int32FromInt32(0) for rc == SQLITE_OK && (*TFts5ExprNode)(unsafe.Pointer(p1)).FbEof == 0 { cmp = _fts5NodeCompare(tls, pExpr, p1, p2) if cmp > 0 { rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, Ti64) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExprNode)(unsafe.Pointer(p2)).FxNext})))(tls, pExpr, p2, int32(1), (*TFts5ExprNode)(unsafe.Pointer(p1)).FiRowid) cmp = _fts5NodeCompare(tls, pExpr, p1, p2) } _ = libc.Int32FromInt32(0) if cmp != 0 || (*TFts5ExprNode)(unsafe.Pointer(p2)).FbNomatch != 0 { break } rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, Ti64) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExprNode)(unsafe.Pointer(p1)).FxNext})))(tls, pExpr, p1, 0, int64(libc.Int32FromInt32(0))) } (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = (*TFts5ExprNode)(unsafe.Pointer(p1)).FbEof (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbNomatch = (*TFts5ExprNode)(unsafe.Pointer(p1)).FbNomatch (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid = (*TFts5ExprNode)(unsafe.Pointer(p1)).FiRowid if (*TFts5ExprNode)(unsafe.Pointer(p1)).FbEof != 0 { _fts5ExprNodeZeroPoslist(tls, p2) } return rc } func _fts5ExprNodeNext_NOT(tls *libc.TLS, pExpr uintptr, pNode uintptr, bFromValid int32, iFrom Ti64) (r int32) { var rc int32 _ = rc rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, Ti64) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExprNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNode + 40)))).FxNext})))(tls, pExpr, *(*uintptr)(unsafe.Pointer(pNode + 40)), bFromValid, iFrom) if rc == SQLITE_OK { rc = _fts5ExprNodeTest_NOT(tls, pExpr, pNode) } if rc != SQLITE_OK { (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbNomatch = 0 } return rc } // C documentation // // /* // ** If pNode currently points to a match, this function returns SQLITE_OK // ** without modifying it. Otherwise, pNode is advanced until it does point // ** to a match or EOF is reached. // */ func _fts5ExprNodeTest(tls *libc.TLS, pExpr uintptr, pNode uintptr) (r int32) { var rc int32 _ = rc rc = SQLITE_OK if (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof == 0 { switch (*TFts5ExprNode)(unsafe.Pointer(pNode)).FeType { case int32(FTS5_STRING): rc = _fts5ExprNodeTest_STRING(tls, pExpr, pNode) case int32(FTS5_TERM): rc = _fts5ExprNodeTest_TERM(tls, pExpr, pNode) case int32(FTS5_AND): rc = _fts5ExprNodeTest_AND(tls, pExpr, pNode) case int32(FTS5_OR): _fts5ExprNodeTest_OR(tls, pExpr, pNode) default: _ = libc.Int32FromInt32(0) rc = _fts5ExprNodeTest_NOT(tls, pExpr, pNode) break } } return rc } // C documentation // // /* // ** Set node pNode, which is part of expression pExpr, to point to the first // ** match. If there are no matches, set the Node.bEof flag to indicate EOF. // ** // ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise. // ** It is not an error if there are no matches. // */ func _fts5ExprNodeFirst(tls *libc.TLS, pExpr uintptr, pNode uintptr) (r int32) { var i, nEof, rc int32 var pChild uintptr _, _, _, _ = i, nEof, pChild, rc rc = SQLITE_OK (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = 0 (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbNomatch = 0 if (*TFts5ExprNode)(unsafe.Pointer(pNode)).FeType == int32(FTS5_TERM) || (*TFts5ExprNode)(unsafe.Pointer(pNode)).FeType == int32(FTS5_STRING) { /* Initialize all term iterators in the NEAR object. */ rc = _fts5ExprNearInitAll(tls, pExpr, pNode) } else { if (*TFts5ExprNode)(unsafe.Pointer(pNode)).FxNext == uintptr(0) { (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = int32(1) } else { nEof = 0 i = 0 for { if !(i < (*TFts5ExprNode)(unsafe.Pointer(pNode)).FnChild && rc == SQLITE_OK) { break } pChild = *(*uintptr)(unsafe.Pointer(pNode + 40 + uintptr(i)*4)) rc = _fts5ExprNodeFirst(tls, pExpr, *(*uintptr)(unsafe.Pointer(pNode + 40 + uintptr(i)*4))) _ = libc.Int32FromInt32(0) nEof += (*TFts5ExprNode)(unsafe.Pointer(pChild)).FbEof goto _1 _1: ; i++ } (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid = (*TFts5ExprNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNode + 40)))).FiRowid switch (*TFts5ExprNode)(unsafe.Pointer(pNode)).FeType { case int32(FTS5_AND): if nEof > 0 { _fts5ExprSetEof(tls, pNode) } case int32(FTS5_OR): if (*TFts5ExprNode)(unsafe.Pointer(pNode)).FnChild == nEof { _fts5ExprSetEof(tls, pNode) } default: _ = libc.Int32FromInt32(0) (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = (*TFts5ExprNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNode + 40)))).FbEof break } } } if rc == SQLITE_OK { rc = _fts5ExprNodeTest(tls, pExpr, pNode) } return rc } // C documentation // // /* // ** Begin iterating through the set of documents in index pIdx matched by // ** the MATCH expression passed as the first argument. If the "bDesc" // ** parameter is passed a non-zero value, iteration is in descending rowid // ** order. Or, if it is zero, in ascending order. // ** // ** If iterating in ascending rowid order (bDesc==0), the first document // ** visited is that with the smallest rowid that is larger than or equal // ** to parameter iFirst. Or, if iterating in ascending order (bDesc==1), // ** then the first document visited must have a rowid smaller than or // ** equal to iFirst. // ** // ** Return SQLITE_OK if successful, or an SQLite error code otherwise. It // ** is not considered an error if the query does not match any documents. // */ func _sqlite3Fts5ExprFirst(tls *libc.TLS, p uintptr, pIdx uintptr, iFirst Ti64, bDesc int32) (r int32) { var pRoot uintptr var rc int32 _, _ = pRoot, rc pRoot = (*TFts5Expr)(unsafe.Pointer(p)).FpRoot /* Return code */ (*TFts5Expr)(unsafe.Pointer(p)).FpIndex = pIdx (*TFts5Expr)(unsafe.Pointer(p)).FbDesc = bDesc rc = _fts5ExprNodeFirst(tls, p, pRoot) /* If not at EOF but the current rowid occurs earlier than iFirst in ** the iteration order, move to document iFirst or later. */ if rc == SQLITE_OK && 0 == (*TFts5ExprNode)(unsafe.Pointer(pRoot)).FbEof && _fts5RowidCmp(tls, p, (*TFts5ExprNode)(unsafe.Pointer(pRoot)).FiRowid, iFirst) < 0 { rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, Ti64) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExprNode)(unsafe.Pointer(pRoot)).FxNext})))(tls, p, pRoot, int32(1), iFirst) } /* If the iterator is not at a real match, skip forward until it is. */ for (*TFts5ExprNode)(unsafe.Pointer(pRoot)).FbNomatch != 0 && rc == SQLITE_OK { _ = libc.Int32FromInt32(0) rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, Ti64) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExprNode)(unsafe.Pointer(pRoot)).FxNext})))(tls, p, pRoot, 0, int64(libc.Int32FromInt32(0))) } return rc } // C documentation // // /* // ** Move to the next document // ** // ** Return SQLITE_OK if successful, or an SQLite error code otherwise. It // ** is not considered an error if the query does not match any documents. // */ func _sqlite3Fts5ExprNext(tls *libc.TLS, p uintptr, iLast Ti64) (r int32) { var pRoot uintptr var rc int32 _, _ = pRoot, rc pRoot = (*TFts5Expr)(unsafe.Pointer(p)).FpRoot _ = libc.Int32FromInt32(0) for cond := true; cond; cond = (*TFts5ExprNode)(unsafe.Pointer(pRoot)).FbNomatch != 0 { rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, Ti64) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExprNode)(unsafe.Pointer(pRoot)).FxNext})))(tls, p, pRoot, 0, int64(libc.Int32FromInt32(0))) _ = libc.Int32FromInt32(0) } if _fts5RowidCmp(tls, p, (*TFts5ExprNode)(unsafe.Pointer(pRoot)).FiRowid, iLast) > 0 { (*TFts5ExprNode)(unsafe.Pointer(pRoot)).FbEof = int32(1) } return rc } func _sqlite3Fts5ExprEof(tls *libc.TLS, p uintptr) (r int32) { return (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(p)).FpRoot)).FbEof } func _sqlite3Fts5ExprRowid(tls *libc.TLS, p uintptr) (r Ti64) { return (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(p)).FpRoot)).FiRowid } func _fts5ParseStringFromToken(tls *libc.TLS, pToken uintptr, pz uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* rc at bp+0 */ int32 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*uintptr)(unsafe.Pointer(pz)) = _sqlite3Fts5Strndup(tls, bp, (*TFts5Token)(unsafe.Pointer(pToken)).Fp, (*TFts5Token)(unsafe.Pointer(pToken)).Fn) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Free the phrase object passed as the only argument. // */ func _fts5ExprPhraseFree(tls *libc.TLS, pPhrase uintptr) { var i int32 var pNext, pSyn, pTerm uintptr _, _, _, _ = i, pNext, pSyn, pTerm if pPhrase != 0 { i = 0 for { if !(i < (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm) { break } pTerm = pPhrase + 20 + uintptr(i)*24 Xsqlite3_free(tls, (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpTerm) _sqlite3Fts5IterClose(tls, (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpIter) pSyn = (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpSynonym for { if !(pSyn != 0) { break } pNext = (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FpSynonym _sqlite3Fts5IterClose(tls, (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FpIter) _sqlite3Fts5BufferFree(tls, pSyn+1*24) Xsqlite3_free(tls, pSyn) goto _2 _2: ; pSyn = pNext } goto _1 _1: ; i++ } if (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.FnSpace > 0 { _sqlite3Fts5BufferFree(tls, pPhrase+4) } Xsqlite3_free(tls, pPhrase) } } // C documentation // // /* // ** Set the "bFirst" flag on the first token of the phrase passed as the // ** only argument. // */ func _sqlite3Fts5ParseSetCaret(tls *libc.TLS, pPhrase uintptr) { if pPhrase != 0 && (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm != 0 { (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20))).FbFirst = uint8(1) } } // C documentation // // /* // ** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated // ** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is // ** appended to it and the results returned. // ** // ** If an OOM error occurs, both the pNear and pPhrase objects are freed and // ** NULL returned. // */ func _sqlite3Fts5ParseNearset(tls *libc.TLS, pParse uintptr, pNear uintptr, pPhrase uintptr) (r uintptr) { var SZALLOC, nNew, v1 int32 var nByte, nByte1 Tsqlite3_int64 var pLast, pRet, v2 uintptr _, _, _, _, _, _, _, _ = SZALLOC, nByte, nByte1, nNew, pLast, pRet, v1, v2 SZALLOC = int32(8) pRet = uintptr(0) if (*TFts5Parse)(unsafe.Pointer(pParse)).Frc == SQLITE_OK { if pPhrase == uintptr(0) { return pNear } if pNear == uintptr(0) { nByte = int64(uint32(16) + uint32(SZALLOC)*uint32(4)) pRet = Xsqlite3_malloc64(tls, uint64(nByte)) if pRet == uintptr(0) { (*TFts5Parse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, pRet, 0, uint32(nByte)) } } else { if (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase%SZALLOC == 0 { nNew = (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase + SZALLOC nByte1 = int64(uint32(16) + uint32(nNew)*uint32(4)) pRet = Xsqlite3_realloc64(tls, pNear, uint64(nByte1)) if pRet == uintptr(0) { (*TFts5Parse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM) } } else { pRet = pNear } } } if pRet == uintptr(0) { _ = libc.Int32FromInt32(0) _sqlite3Fts5ParseNearsetFree(tls, pNear) _sqlite3Fts5ParsePhraseFree(tls, pPhrase) } else { if (*TFts5ExprNearset)(unsafe.Pointer(pRet)).FnPhrase > 0 { pLast = *(*uintptr)(unsafe.Pointer(pRet + 12 + uintptr((*TFts5ExprNearset)(unsafe.Pointer(pRet)).FnPhrase-int32(1))*4)) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm == 0 { _fts5ExprPhraseFree(tls, pPhrase) (*TFts5ExprNearset)(unsafe.Pointer(pRet)).FnPhrase-- (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase-- pPhrase = pLast } else { if (*TFts5ExprPhrase)(unsafe.Pointer(pLast)).FnTerm == 0 { _fts5ExprPhraseFree(tls, pLast) *(*uintptr)(unsafe.Pointer((*TFts5Parse)(unsafe.Pointer(pParse)).FapPhrase + uintptr((*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase-int32(2))*4)) = pPhrase (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase-- (*TFts5ExprNearset)(unsafe.Pointer(pRet)).FnPhrase-- } } } v2 = pRet + 8 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ *(*uintptr)(unsafe.Pointer(pRet + 12 + uintptr(v1)*4)) = pPhrase } return pRet } type TTokenCtx = struct { FpPhrase uintptr FpConfig uintptr Frc int32 } type TokenCtx = TTokenCtx type TTokenCtx1 = struct { FpPhrase uintptr FpConfig uintptr Frc int32 } type TokenCtx1 = TTokenCtx1 // C documentation // // /* // ** Callback for tokenizing terms used by ParseTerm(). // */ func _fts5ParseTokenize(tls *libc.TLS, pContext uintptr, tflags int32, pToken uintptr, nToken int32, iUnused1 int32, iUnused2 int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var SZALLOC, nNew, v1, v2, v4, v6 int32 var nByte Tsqlite3_int64 var pCtx, pNew, pPhrase, pSyn, pTerm, v3, v5 uintptr var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _ = SZALLOC, nByte, nNew, pCtx, pNew, pPhrase, pSyn, pTerm, v1, v2, v3, v4, v5, v6 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK SZALLOC = int32(8) pCtx = pContext pPhrase = (*TTokenCtx)(unsafe.Pointer(pCtx)).FpPhrase _ = iUnused1 _ = iUnused2 /* If an error has already occurred, this is a no-op */ if (*TTokenCtx)(unsafe.Pointer(pCtx)).Frc != SQLITE_OK { return (*TTokenCtx)(unsafe.Pointer(pCtx)).Frc } if nToken > int32(FTS5_MAX_TOKEN_SIZE) { nToken = int32(FTS5_MAX_TOKEN_SIZE) } if pPhrase != 0 && (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm > 0 && tflags&int32(FTS5_TOKEN_COLOCATED) != 0 { nByte = int64(libc.Uint32FromInt64(24) + libc.Uint32FromInt64(12) + uint32(nToken) + uint32(1)) pSyn = Xsqlite3_malloc64(tls, uint64(nByte)) if pSyn == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, pSyn, 0, uint32(nByte)) (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FpTerm = pSyn + uintptr(24) + uintptr(12) v1 = nToken (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FnQueryTerm = v1 (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FnFullTerm = v1 if (*TFts5Config)(unsafe.Pointer((*TTokenCtx)(unsafe.Pointer(pCtx)).FpConfig)).FbTokendata != 0 { (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FnQueryTerm = int32(libc.Xstrlen(tls, (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FpTerm)) } libc.Xmemcpy(tls, (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FpTerm, pToken, uint32(nToken)) (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FpSynonym = (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20 + uintptr((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm-int32(1))*24))).FpSynonym (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20 + uintptr((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm-int32(1))*24))).FpSynonym = pSyn } } else { if pPhrase == uintptr(0) || (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm%SZALLOC == 0 { if pPhrase != 0 { v2 = (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm } else { v2 = 0 } nNew = SZALLOC + v2 pNew = Xsqlite3_realloc64(tls, pPhrase, uint64(uint32(44)+uint32(24)*uint32(nNew))) if pNew == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_NOMEM) } else { if pPhrase == uintptr(0) { libc.Xmemset(tls, pNew, 0, uint32(44)) } v3 = pNew pPhrase = v3 (*TTokenCtx)(unsafe.Pointer(pCtx)).FpPhrase = v3 (*TFts5ExprPhrase)(unsafe.Pointer(pNew)).FnTerm = nNew - SZALLOC } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { v5 = pPhrase + 16 v4 = *(*int32)(unsafe.Pointer(v5)) *(*int32)(unsafe.Pointer(v5))++ pTerm = pPhrase + 20 + uintptr(v4)*24 libc.Xmemset(tls, pTerm, 0, uint32(24)) (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpTerm = _sqlite3Fts5Strndup(tls, bp, pToken, nToken) v6 = nToken (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FnQueryTerm = v6 (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FnFullTerm = v6 if (*TFts5Config)(unsafe.Pointer((*TTokenCtx)(unsafe.Pointer(pCtx)).FpConfig)).FbTokendata != 0 && *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FnQueryTerm = int32(libc.Xstrlen(tls, (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpTerm)) } } } (*TTokenCtx)(unsafe.Pointer(pCtx)).Frc = *(*int32)(unsafe.Pointer(bp)) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Free the phrase object passed as the only argument. // */ func _sqlite3Fts5ParsePhraseFree(tls *libc.TLS, pPhrase uintptr) { _fts5ExprPhraseFree(tls, pPhrase) } // C documentation // // /* // ** Free the phrase object passed as the second argument. // */ func _sqlite3Fts5ParseNearsetFree(tls *libc.TLS, pNear uintptr) { var i int32 _ = i if pNear != 0 { i = 0 for { if !(i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) { break } _fts5ExprPhraseFree(tls, *(*uintptr)(unsafe.Pointer(pNear + 12 + uintptr(i)*4))) goto _1 _1: ; i++ } Xsqlite3_free(tls, (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FpColset) Xsqlite3_free(tls, pNear) } } func _sqlite3Fts5ParseFinished(tls *libc.TLS, pParse uintptr, p uintptr) { _ = libc.Int32FromInt32(0) (*TFts5Parse)(unsafe.Pointer(pParse)).FpExpr = p } func _parseGrowPhraseArray(tls *libc.TLS, pParse uintptr) (r int32) { var apNew uintptr var nByte Tsqlite3_int64 _, _ = apNew, nByte if (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase%int32(8) == 0 { nByte = int64(uint32(4) * uint32((*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase+libc.Int32FromInt32(8))) apNew = Xsqlite3_realloc64(tls, (*TFts5Parse)(unsafe.Pointer(pParse)).FapPhrase, uint64(nByte)) if apNew == uintptr(0) { (*TFts5Parse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM) return int32(SQLITE_NOMEM) } (*TFts5Parse)(unsafe.Pointer(pParse)).FapPhrase = apNew } return SQLITE_OK } // C documentation // // /* // ** This function is called by the parser to process a string token. The // ** string may or may not be quoted. In any case it is tokenized and a // ** phrase object consisting of all tokens returned. // */ func _sqlite3Fts5ParseTerm(tls *libc.TLS, pParse uintptr, pAppend uintptr, pToken uintptr, bPrefix int32) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var flags, n, rc, v1, v2 int32 var pConfig uintptr var v3 bool var _ /* sCtx at bp+0 */ TTokenCtx var _ /* z at bp+12 */ uintptr _, _, _, _, _, _, _ = flags, n, pConfig, rc, v1, v2, v3 pConfig = (*TFts5Parse)(unsafe.Pointer(pParse)).FpConfig /* Tokenize return code */ *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) libc.Xmemset(tls, bp, 0, uint32(12)) (*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase = pAppend (*(*TTokenCtx)(unsafe.Pointer(bp))).FpConfig = pConfig rc = _fts5ParseStringFromToken(tls, pToken, bp+12) if rc == SQLITE_OK { if bPrefix != 0 { v1 = int32(FTS5_TOKENIZE_PREFIX) } else { v1 = 0 } flags = int32(FTS5_TOKENIZE_QUERY) | v1 _sqlite3Fts5Dequote(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) n = int32(libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(bp + 12)))) rc = _sqlite3Fts5Tokenize(tls, pConfig, flags, *(*uintptr)(unsafe.Pointer(bp + 12)), n, bp, __ccgo_fp(_fts5ParseTokenize)) } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) if v3 = rc != 0; !v3 { v2 = (*(*TTokenCtx)(unsafe.Pointer(bp))).Frc rc = v2 } if v3 || v2 != 0 { (*TFts5Parse)(unsafe.Pointer(pParse)).Frc = rc _fts5ExprPhraseFree(tls, (*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase) (*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase = uintptr(0) } else { if pAppend == uintptr(0) { if _parseGrowPhraseArray(tls, pParse) != 0 { _fts5ExprPhraseFree(tls, (*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase) return uintptr(0) } (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase++ } if (*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase == uintptr(0) { /* This happens when parsing a token or quoted phrase that contains ** no token characters at all. (e.g ... MATCH '""'). */ (*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase = _sqlite3Fts5MallocZero(tls, pParse+8, int64(44)) } else { if (*TFts5ExprPhrase)(unsafe.Pointer((*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase)).FnTerm != 0 { (*(*TFts5ExprTerm)(unsafe.Pointer((*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase + 20 + uintptr((*TFts5ExprPhrase)(unsafe.Pointer((*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase)).FnTerm-int32(1))*24))).FbPrefix = uint8(bPrefix) } } *(*uintptr)(unsafe.Pointer((*TFts5Parse)(unsafe.Pointer(pParse)).FapPhrase + uintptr((*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase-int32(1))*4)) = (*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase } return (*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase } // C documentation // // /* // ** Create a new FTS5 expression by cloning phrase iPhrase of the // ** expression passed as the second argument. // */ func _sqlite3Fts5ExprClonePhrase(tls *libc.TLS, pExpr uintptr, iPhrase int32, ppNew uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, tflags int32 var nByte Tsqlite3_int64 var p, pColset, pColsetOrig, pNew, pOrig uintptr var _ /* rc at bp+0 */ int32 var _ /* sCtx at bp+4 */ TTokenCtx _, _, _, _, _, _, _, _ = i, nByte, p, pColset, pColsetOrig, pNew, pOrig, tflags *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK /* Return code */ pOrig = uintptr(0) /* The phrase extracted from pExpr */ pNew = uintptr(0) /* Expression to return via *ppNew */ *(*TTokenCtx)(unsafe.Pointer(bp + 4)) = TTokenCtx{} /* Context object for fts5ParseTokenize */ if iPhrase < 0 || iPhrase >= (*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_RANGE) } else { pOrig = *(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(iPhrase)*4)) pNew = _sqlite3Fts5MallocZero(tls, bp, int64(24)) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { (*TFts5Expr)(unsafe.Pointer(pNew)).FapExprPhrase = _sqlite3Fts5MallocZero(tls, bp, int64(4)) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { (*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot = _sqlite3Fts5MallocZero(tls, bp, int64(48)) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FpNear = _sqlite3Fts5MallocZero(tls, bp, int64(libc.Uint32FromInt64(16)+libc.Uint32FromInt64(4))) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && pOrig != uintptr(0) { pColsetOrig = (*TFts5ExprNearset)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer((*TFts5ExprPhrase)(unsafe.Pointer(pOrig)).FpNode)).FpNear)).FpColset if pColsetOrig != 0 { nByte = int64(uint32(8) + uint32((*TFts5Colset)(unsafe.Pointer(pColsetOrig)).FnCol-libc.Int32FromInt32(1))*uint32(4)) pColset = _sqlite3Fts5MallocZero(tls, bp, nByte) if pColset != 0 { libc.Xmemcpy(tls, pColset, pColsetOrig, uint32(nByte)) } (*TFts5ExprNearset)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FpNear)).FpColset = pColset } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { if (*TFts5ExprPhrase)(unsafe.Pointer(pOrig)).FnTerm != 0 { /* Used to iterate through phrase terms */ (*(*TTokenCtx)(unsafe.Pointer(bp + 4))).FpConfig = (*TFts5Expr)(unsafe.Pointer(pExpr)).FpConfig i = 0 for { if !(*(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && i < (*TFts5ExprPhrase)(unsafe.Pointer(pOrig)).FnTerm) { break } tflags = 0 p = pOrig + 20 + uintptr(i)*24 for { if !(p != 0 && *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK) { break } *(*int32)(unsafe.Pointer(bp)) = _fts5ParseTokenize(tls, bp+4, tflags, (*TFts5ExprTerm)(unsafe.Pointer(p)).FpTerm, (*TFts5ExprTerm)(unsafe.Pointer(p)).FnFullTerm, 0, 0) tflags = int32(FTS5_TOKEN_COLOCATED) goto _2 _2: ; p = (*TFts5ExprTerm)(unsafe.Pointer(p)).FpSynonym } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { (*(*TFts5ExprTerm)(unsafe.Pointer((*(*TTokenCtx)(unsafe.Pointer(bp + 4))).FpPhrase + 20 + uintptr(i)*24))).FbPrefix = (*(*TFts5ExprTerm)(unsafe.Pointer(pOrig + 20 + uintptr(i)*24))).FbPrefix (*(*TFts5ExprTerm)(unsafe.Pointer((*(*TTokenCtx)(unsafe.Pointer(bp + 4))).FpPhrase + 20 + uintptr(i)*24))).FbFirst = (*(*TFts5ExprTerm)(unsafe.Pointer(pOrig + 20 + uintptr(i)*24))).FbFirst } goto _1 _1: ; i++ } } else { /* This happens when parsing a token or quoted phrase that contains ** no token characters at all. (e.g ... MATCH '""'). */ (*(*TTokenCtx)(unsafe.Pointer(bp + 4))).FpPhrase = _sqlite3Fts5MallocZero(tls, bp, int64(44)) } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && (*(*TTokenCtx)(unsafe.Pointer(bp + 4))).FpPhrase != 0 { /* All the allocations succeeded. Put the expression object together. */ (*TFts5Expr)(unsafe.Pointer(pNew)).FpIndex = (*TFts5Expr)(unsafe.Pointer(pExpr)).FpIndex (*TFts5Expr)(unsafe.Pointer(pNew)).FpConfig = (*TFts5Expr)(unsafe.Pointer(pExpr)).FpConfig (*TFts5Expr)(unsafe.Pointer(pNew)).FnPhrase = int32(1) *(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FapExprPhrase)) = (*(*TTokenCtx)(unsafe.Pointer(bp + 4))).FpPhrase *(*uintptr)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FpNear + 12)) = (*(*TTokenCtx)(unsafe.Pointer(bp + 4))).FpPhrase (*TFts5ExprNearset)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FpNear)).FnPhrase = int32(1) (*TFts5ExprPhrase)(unsafe.Pointer((*(*TTokenCtx)(unsafe.Pointer(bp + 4))).FpPhrase)).FpNode = (*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot if (*TFts5ExprPhrase)(unsafe.Pointer(pOrig)).FnTerm == int32(1) && (*(*TFts5ExprTerm)(unsafe.Pointer(pOrig + 20))).FpSynonym == uintptr(0) && int32((*(*TFts5ExprTerm)(unsafe.Pointer(pOrig + 20))).FbFirst) == 0 { (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FeType = int32(FTS5_TERM) (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FxNext = __ccgo_fp(_fts5ExprNodeNext_TERM) } else { (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FeType = int32(FTS5_STRING) (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FxNext = __ccgo_fp(_fts5ExprNodeNext_STRING) } } else { _sqlite3Fts5ExprFree(tls, pNew) _fts5ExprPhraseFree(tls, (*(*TTokenCtx)(unsafe.Pointer(bp + 4))).FpPhrase) pNew = uintptr(0) } *(*uintptr)(unsafe.Pointer(ppNew)) = pNew return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Token pTok has appeared in a MATCH expression where the NEAR operator // ** is expected. If token pTok does not contain "NEAR", store an error // ** in the pParse object. // */ func _sqlite3Fts5ParseNear(tls *libc.TLS, pParse uintptr, pTok uintptr) { bp := tls.Alloc(32) defer tls.Free(32) if (*TFts5Token)(unsafe.Pointer(pTok)).Fn != int32(4) || libc.Xmemcmp(tls, __ccgo_ts+35969, (*TFts5Token)(unsafe.Pointer(pTok)).Fp, uint32(4)) != 0 { _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+34750, libc.VaList(bp+8, (*TFts5Token)(unsafe.Pointer(pTok)).Fn, (*TFts5Token)(unsafe.Pointer(pTok)).Fp)) } } func _sqlite3Fts5ParseSetDistance(tls *libc.TLS, pParse uintptr, pNear uintptr, p uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var c uint8 var i, nNear int32 _, _, _ = c, i, nNear if pNear != 0 { nNear = 0 if (*TFts5Token)(unsafe.Pointer(p)).Fn != 0 { i = 0 for { if !(i < (*TFts5Token)(unsafe.Pointer(p)).Fn) { break } c = *(*uint8)(unsafe.Pointer((*TFts5Token)(unsafe.Pointer(p)).Fp + uintptr(i))) if int32(c) < int32('0') || int32(c) > int32('9') { _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+35974, libc.VaList(bp+8, (*TFts5Token)(unsafe.Pointer(p)).Fn, (*TFts5Token)(unsafe.Pointer(p)).Fp)) return } nNear = nNear*int32(10) + (int32(*(*uint8)(unsafe.Pointer((*TFts5Token)(unsafe.Pointer(p)).Fp + uintptr(i)))) - int32('0')) goto _1 _1: ; i++ } } else { nNear = int32(FTS5_DEFAULT_NEARDIST) } (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnNear = nNear } } // C documentation // // /* // ** The second argument passed to this function may be NULL, or it may be // ** an existing Fts5Colset object. This function returns a pointer to // ** a new colset object containing the contents of (p) with new value column // ** number iCol appended. // ** // ** If an OOM error occurs, store an error code in pParse and return NULL. // ** The old colset object (if any) is not freed in this case. // */ func _fts5ParseColset(tls *libc.TLS, pParse uintptr, p uintptr, iCol int32) (r uintptr) { var aiCol, pNew uintptr var i, j, nCol, v1 int32 _, _, _, _, _, _ = aiCol, i, j, nCol, pNew, v1 if p != 0 { v1 = (*TFts5Colset)(unsafe.Pointer(p)).FnCol } else { v1 = 0 } nCol = v1 /* New colset object to return */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) pNew = Xsqlite3_realloc64(tls, p, uint64(uint32(8)+uint32(4)*uint32(nCol))) if pNew == uintptr(0) { (*TFts5Parse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM) } else { aiCol = pNew + 4 i = 0 for { if !(i < nCol) { break } if *(*int32)(unsafe.Pointer(aiCol + uintptr(i)*4)) == iCol { return pNew } if *(*int32)(unsafe.Pointer(aiCol + uintptr(i)*4)) > iCol { break } goto _2 _2: ; i++ } j = nCol for { if !(j > i) { break } *(*int32)(unsafe.Pointer(aiCol + uintptr(j)*4)) = *(*int32)(unsafe.Pointer(aiCol + uintptr(j-int32(1))*4)) goto _3 _3: ; j-- } *(*int32)(unsafe.Pointer(aiCol + uintptr(i)*4)) = iCol (*TFts5Colset)(unsafe.Pointer(pNew)).FnCol = nCol + int32(1) } return pNew } // C documentation // // /* // ** Allocate and return an Fts5Colset object specifying the inverse of // ** the colset passed as the second argument. Free the colset passed // ** as the second argument before returning. // */ func _sqlite3Fts5ParseColsetInvert(tls *libc.TLS, pParse uintptr, p uintptr) (r uintptr) { var i, iOld, nCol, v2 int32 var pRet, v3 uintptr _, _, _, _, _, _ = i, iOld, nCol, pRet, v2, v3 nCol = (*TFts5Config)(unsafe.Pointer((*TFts5Parse)(unsafe.Pointer(pParse)).FpConfig)).FnCol pRet = _sqlite3Fts5MallocZero(tls, pParse+8, int64(uint32(8)+uint32(4)*uint32(nCol))) if pRet != 0 { iOld = 0 i = 0 for { if !(i < nCol) { break } if iOld >= (*TFts5Colset)(unsafe.Pointer(p)).FnCol || *(*int32)(unsafe.Pointer(p + 4 + uintptr(iOld)*4)) != i { v3 = pRet v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ *(*int32)(unsafe.Pointer(pRet + 4 + uintptr(v2)*4)) = i } else { iOld++ } goto _1 _1: ; i++ } } Xsqlite3_free(tls, p) return pRet } func _sqlite3Fts5ParseColset(tls *libc.TLS, pParse uintptr, pColset uintptr, p uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var iCol int32 var pConfig, pRet, z uintptr _, _, _, _ = iCol, pConfig, pRet, z pRet = uintptr(0) /* Dequoted copy of token p */ z = _sqlite3Fts5Strndup(tls, pParse+8, (*TFts5Token)(unsafe.Pointer(p)).Fp, (*TFts5Token)(unsafe.Pointer(p)).Fn) if (*TFts5Parse)(unsafe.Pointer(pParse)).Frc == SQLITE_OK { pConfig = (*TFts5Parse)(unsafe.Pointer(pParse)).FpConfig _sqlite3Fts5Dequote(tls, z) iCol = 0 for { if !(iCol < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) { break } if 0 == Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(pConfig)).FazCol + uintptr(iCol)*4)), z) { break } goto _1 _1: ; iCol++ } if iCol == (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol { _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+21016, libc.VaList(bp+8, z)) } else { pRet = _fts5ParseColset(tls, pParse, pColset, iCol) } Xsqlite3_free(tls, z) } if pRet == uintptr(0) { _ = libc.Int32FromInt32(0) Xsqlite3_free(tls, pColset) } return pRet } // C documentation // // /* // ** If argument pOrig is NULL, or if (*pRc) is set to anything other than // ** SQLITE_OK when this function is called, NULL is returned. // ** // ** Otherwise, a copy of (*pOrig) is made into memory obtained from // ** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation // ** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned. // */ func _fts5CloneColset(tls *libc.TLS, pRc uintptr, pOrig uintptr) (r uintptr) { var nByte Tsqlite3_int64 var pRet uintptr _, _ = nByte, pRet if pOrig != 0 { nByte = int64(uint32(8) + uint32((*TFts5Colset)(unsafe.Pointer(pOrig)).FnCol-libc.Int32FromInt32(1))*uint32(4)) pRet = _sqlite3Fts5MallocZero(tls, pRc, nByte) if pRet != 0 { libc.Xmemcpy(tls, pRet, pOrig, uint32(nByte)) } } else { pRet = uintptr(0) } return pRet } // C documentation // // /* // ** Remove from colset pColset any columns that are not also in colset pMerge. // */ func _fts5MergeColset(tls *libc.TLS, pColset uintptr, pMerge uintptr) { var iDiff, iIn, iMerge, iOut, v1 int32 _, _, _, _, _ = iDiff, iIn, iMerge, iOut, v1 iIn = 0 /* Next input in pColset */ iMerge = 0 /* Next input in pMerge */ iOut = 0 /* Next output slot in pColset */ for iIn < (*TFts5Colset)(unsafe.Pointer(pColset)).FnCol && iMerge < (*TFts5Colset)(unsafe.Pointer(pMerge)).FnCol { iDiff = *(*int32)(unsafe.Pointer(pColset + 4 + uintptr(iIn)*4)) - *(*int32)(unsafe.Pointer(pMerge + 4 + uintptr(iMerge)*4)) if iDiff == 0 { v1 = iOut iOut++ *(*int32)(unsafe.Pointer(pColset + 4 + uintptr(v1)*4)) = *(*int32)(unsafe.Pointer(pMerge + 4 + uintptr(iMerge)*4)) iMerge++ iIn++ } else { if iDiff > 0 { iMerge++ } else { iIn++ } } } (*TFts5Colset)(unsafe.Pointer(pColset)).FnCol = iOut } // C documentation // // /* // ** Recursively apply colset pColset to expression node pNode and all of // ** its decendents. If (*ppFree) is not NULL, it contains a spare copy // ** of pColset. This function may use the spare copy and set (*ppFree) to // ** zero, or it may create copies of pColset using fts5CloneColset(). // */ func _fts5ParseSetColset(tls *libc.TLS, pParse uintptr, pNode uintptr, pColset uintptr, ppFree uintptr) { var i int32 var pNear uintptr _, _ = i, pNear if (*TFts5Parse)(unsafe.Pointer(pParse)).Frc == SQLITE_OK { _ = libc.Int32FromInt32(0) if (*TFts5ExprNode)(unsafe.Pointer(pNode)).FeType == int32(FTS5_STRING) || (*TFts5ExprNode)(unsafe.Pointer(pNode)).FeType == int32(FTS5_TERM) { pNear = (*TFts5ExprNode)(unsafe.Pointer(pNode)).FpNear if (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FpColset != 0 { _fts5MergeColset(tls, (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FpColset, pColset) if (*TFts5Colset)(unsafe.Pointer((*TFts5ExprNearset)(unsafe.Pointer(pNear)).FpColset)).FnCol == 0 { (*TFts5ExprNode)(unsafe.Pointer(pNode)).FeType = FTS5_EOF (*TFts5ExprNode)(unsafe.Pointer(pNode)).FxNext = uintptr(0) } } else { if *(*uintptr)(unsafe.Pointer(ppFree)) != 0 { (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FpColset = pColset *(*uintptr)(unsafe.Pointer(ppFree)) = uintptr(0) } else { (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FpColset = _fts5CloneColset(tls, pParse+8, pColset) } } } else { _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*TFts5ExprNode)(unsafe.Pointer(pNode)).FnChild) { break } _fts5ParseSetColset(tls, pParse, *(*uintptr)(unsafe.Pointer(pNode + 40 + uintptr(i)*4)), pColset, ppFree) goto _1 _1: ; i++ } } } } // C documentation // // /* // ** Apply colset pColset to expression node pExpr and all of its descendents. // */ func _sqlite3Fts5ParseSetColset(tls *libc.TLS, pParse uintptr, pExpr uintptr, pColset uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* pFree at bp+0 */ uintptr *(*uintptr)(unsafe.Pointer(bp)) = pColset if (*TFts5Config)(unsafe.Pointer((*TFts5Parse)(unsafe.Pointer(pParse)).FpConfig)).FeDetail == int32(FTS5_DETAIL_NONE) { _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+36003, 0) } else { _fts5ParseSetColset(tls, pParse, pExpr, pColset, bp) } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp))) } func _fts5ExprAssignXNext(tls *libc.TLS, pNode uintptr) { var pNear uintptr _ = pNear switch (*TFts5ExprNode)(unsafe.Pointer(pNode)).FeType { case int32(FTS5_STRING): pNear = (*TFts5ExprNode)(unsafe.Pointer(pNode)).FpNear if (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase == int32(1) && (*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12)))).FnTerm == int32(1) && (*(*TFts5ExprTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12)) + 20))).FpSynonym == uintptr(0) && int32((*(*TFts5ExprTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12)) + 20))).FbFirst) == 0 { (*TFts5ExprNode)(unsafe.Pointer(pNode)).FeType = int32(FTS5_TERM) (*TFts5ExprNode)(unsafe.Pointer(pNode)).FxNext = __ccgo_fp(_fts5ExprNodeNext_TERM) } else { (*TFts5ExprNode)(unsafe.Pointer(pNode)).FxNext = __ccgo_fp(_fts5ExprNodeNext_STRING) } case int32(FTS5_OR): (*TFts5ExprNode)(unsafe.Pointer(pNode)).FxNext = __ccgo_fp(_fts5ExprNodeNext_OR) case int32(FTS5_AND): (*TFts5ExprNode)(unsafe.Pointer(pNode)).FxNext = __ccgo_fp(_fts5ExprNodeNext_AND) default: _ = libc.Int32FromInt32(0) (*TFts5ExprNode)(unsafe.Pointer(pNode)).FxNext = __ccgo_fp(_fts5ExprNodeNext_NOT) break } } func _fts5ExprAddChildren(tls *libc.TLS, p uintptr, pSub uintptr) { var ii, nByte, v1, v4 int32 var v2 uintptr _, _, _, _, _ = ii, nByte, v1, v2, v4 ii = (*TFts5ExprNode)(unsafe.Pointer(p)).FnChild if (*TFts5ExprNode)(unsafe.Pointer(p)).FeType != int32(FTS5_NOT) && (*TFts5ExprNode)(unsafe.Pointer(pSub)).FeType == (*TFts5ExprNode)(unsafe.Pointer(p)).FeType { nByte = int32(uint32(4) * uint32((*TFts5ExprNode)(unsafe.Pointer(pSub)).FnChild)) libc.Xmemcpy(tls, p+40+uintptr((*TFts5ExprNode)(unsafe.Pointer(p)).FnChild)*4, pSub+40, uint32(nByte)) *(*int32)(unsafe.Pointer(p + 36)) += (*TFts5ExprNode)(unsafe.Pointer(pSub)).FnChild Xsqlite3_free(tls, pSub) } else { v2 = p + 36 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ *(*uintptr)(unsafe.Pointer(p + 40 + uintptr(v1)*4)) = pSub } for { if !(ii < (*TFts5ExprNode)(unsafe.Pointer(p)).FnChild) { break } if (*TFts5ExprNode)(unsafe.Pointer(p)).FiHeight > (*TFts5ExprNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 40 + uintptr(ii)*4)))).FiHeight+int32(1) { v4 = (*TFts5ExprNode)(unsafe.Pointer(p)).FiHeight } else { v4 = (*TFts5ExprNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 40 + uintptr(ii)*4)))).FiHeight + int32(1) } (*TFts5ExprNode)(unsafe.Pointer(p)).FiHeight = v4 goto _3 _3: ; ii++ } } // C documentation // // /* // ** This function is used when parsing LIKE or GLOB patterns against // ** trigram indexes that specify either detail=column or detail=none. // ** It converts a phrase: // ** // ** abc + def + ghi // ** // ** into an AND tree: // ** // ** abc AND def AND ghi // */ func _fts5ParsePhraseToAnd(tls *libc.TLS, pParse uintptr, pNear uintptr) (r uintptr) { var ii, nByte, nTerm, v2 int32 var p, pPhrase, pRet, pTo, v3 uintptr _, _, _, _, _, _, _, _, _ = ii, nByte, nTerm, p, pPhrase, pRet, pTo, v2, v3 nTerm = (*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12)))).FnTerm _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) nByte = int32(uint32(48) + uint32(nTerm)*uint32(4)) pRet = _sqlite3Fts5MallocZero(tls, pParse+8, int64(nByte)) if pRet != 0 { (*TFts5ExprNode)(unsafe.Pointer(pRet)).FeType = int32(FTS5_AND) (*TFts5ExprNode)(unsafe.Pointer(pRet)).FnChild = nTerm (*TFts5ExprNode)(unsafe.Pointer(pRet)).FiHeight = int32(1) _fts5ExprAssignXNext(tls, pRet) (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase-- ii = 0 for { if !(ii < nTerm) { break } pPhrase = _sqlite3Fts5MallocZero(tls, pParse+8, int64(44)) if pPhrase != 0 { if _parseGrowPhraseArray(tls, pParse) != 0 { _fts5ExprPhraseFree(tls, pPhrase) } else { p = *(*uintptr)(unsafe.Pointer(pNear + 12)) + 20 + uintptr(ii)*24 pTo = pPhrase + 20 v3 = pParse + 12 v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ *(*uintptr)(unsafe.Pointer((*TFts5Parse)(unsafe.Pointer(pParse)).FapPhrase + uintptr(v2)*4)) = pPhrase (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm = int32(1) (*TFts5ExprTerm)(unsafe.Pointer(pTo)).FpTerm = _sqlite3Fts5Strndup(tls, pParse+8, (*TFts5ExprTerm)(unsafe.Pointer(p)).FpTerm, (*TFts5ExprTerm)(unsafe.Pointer(p)).FnFullTerm) (*TFts5ExprTerm)(unsafe.Pointer(pTo)).FnQueryTerm = (*TFts5ExprTerm)(unsafe.Pointer(p)).FnQueryTerm (*TFts5ExprTerm)(unsafe.Pointer(pTo)).FnFullTerm = (*TFts5ExprTerm)(unsafe.Pointer(p)).FnFullTerm *(*uintptr)(unsafe.Pointer(pRet + 40 + uintptr(ii)*4)) = _sqlite3Fts5ParseNode(tls, pParse, int32(FTS5_STRING), uintptr(0), uintptr(0), _sqlite3Fts5ParseNearset(tls, pParse, uintptr(0), pPhrase)) } } goto _1 _1: ; ii++ } if (*TFts5Parse)(unsafe.Pointer(pParse)).Frc != 0 { _sqlite3Fts5ParseNodeFree(tls, pRet) pRet = uintptr(0) } else { _sqlite3Fts5ParseNearsetFree(tls, pNear) } } return pRet } // C documentation // // /* // ** Allocate and return a new expression object. If anything goes wrong (i.e. // ** OOM error), leave an error code in pParse and return NULL. // */ func _sqlite3Fts5ParseNode(tls *libc.TLS, pParse uintptr, eType int32, pLeft uintptr, pRight uintptr, pNear uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var iPhrase, nChild int32 var nByte Tsqlite3_int64 var pPhrase, pRet, v2 uintptr _, _, _, _, _, _ = iPhrase, nByte, nChild, pPhrase, pRet, v2 pRet = uintptr(0) if (*TFts5Parse)(unsafe.Pointer(pParse)).Frc == SQLITE_OK { nChild = 0 /* Bytes of space to allocate for this node */ _ = libc.Int32FromInt32(0) if eType == int32(FTS5_STRING) && pNear == uintptr(0) { return uintptr(0) } if eType != int32(FTS5_STRING) && pLeft == uintptr(0) { return pRight } if eType != int32(FTS5_STRING) && pRight == uintptr(0) { return pLeft } if eType == int32(FTS5_STRING) && (*TFts5Parse)(unsafe.Pointer(pParse)).FbPhraseToAnd != 0 && (*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12)))).FnTerm > int32(1) { pRet = _fts5ParsePhraseToAnd(tls, pParse, pNear) } else { if eType == int32(FTS5_NOT) { nChild = int32(2) } else { if eType == int32(FTS5_AND) || eType == int32(FTS5_OR) { nChild = int32(2) if (*TFts5ExprNode)(unsafe.Pointer(pLeft)).FeType == eType { nChild += (*TFts5ExprNode)(unsafe.Pointer(pLeft)).FnChild - int32(1) } if (*TFts5ExprNode)(unsafe.Pointer(pRight)).FeType == eType { nChild += (*TFts5ExprNode)(unsafe.Pointer(pRight)).FnChild - int32(1) } } } nByte = int64(uint32(48) + uint32(4)*uint32(nChild-libc.Int32FromInt32(1))) pRet = _sqlite3Fts5MallocZero(tls, pParse+8, nByte) if pRet != 0 { (*TFts5ExprNode)(unsafe.Pointer(pRet)).FeType = eType (*TFts5ExprNode)(unsafe.Pointer(pRet)).FpNear = pNear _fts5ExprAssignXNext(tls, pRet) if eType == int32(FTS5_STRING) { iPhrase = 0 for { if !(iPhrase < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) { break } (*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12 + uintptr(iPhrase)*4)))).FpNode = pRet if (*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12 + uintptr(iPhrase)*4)))).FnTerm == 0 { (*TFts5ExprNode)(unsafe.Pointer(pRet)).FxNext = uintptr(0) (*TFts5ExprNode)(unsafe.Pointer(pRet)).FeType = FTS5_EOF } goto _1 _1: ; iPhrase++ } if (*TFts5Config)(unsafe.Pointer((*TFts5Parse)(unsafe.Pointer(pParse)).FpConfig)).FeDetail != FTS5_DETAIL_FULL { pPhrase = *(*uintptr)(unsafe.Pointer(pNear + 12)) if (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase != int32(1) || (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm > int32(1) || (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm > 0 && (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20))).FbFirst != 0 { if (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase == int32(1) { v2 = __ccgo_ts + 36056 } else { v2 = __ccgo_ts + 35969 } _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+36063, libc.VaList(bp+8, v2)) Xsqlite3_free(tls, pRet) pRet = uintptr(0) } } } else { _fts5ExprAddChildren(tls, pRet, pLeft) _fts5ExprAddChildren(tls, pRet, pRight) if (*TFts5ExprNode)(unsafe.Pointer(pRet)).FiHeight > int32(SQLITE_FTS5_MAX_EXPR_DEPTH) { _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+36113, libc.VaList(bp+8, int32(SQLITE_FTS5_MAX_EXPR_DEPTH))) Xsqlite3_free(tls, pRet) pRet = uintptr(0) } } } } } if pRet == uintptr(0) { _ = libc.Int32FromInt32(0) _sqlite3Fts5ParseNodeFree(tls, pLeft) _sqlite3Fts5ParseNodeFree(tls, pRight) _sqlite3Fts5ParseNearsetFree(tls, pNear) } return pRet } func _sqlite3Fts5ParseImplicitAnd(tls *libc.TLS, pParse uintptr, pLeft uintptr, pRight uintptr) (r uintptr) { var ap, pPrev, pRet uintptr _, _, _ = ap, pPrev, pRet pRet = uintptr(0) if (*TFts5Parse)(unsafe.Pointer(pParse)).Frc != 0 { _sqlite3Fts5ParseNodeFree(tls, pLeft) _sqlite3Fts5ParseNodeFree(tls, pRight) } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TFts5ExprNode)(unsafe.Pointer(pLeft)).FeType == int32(FTS5_AND) { pPrev = *(*uintptr)(unsafe.Pointer(pLeft + 40 + uintptr((*TFts5ExprNode)(unsafe.Pointer(pLeft)).FnChild-int32(1))*4)) } else { pPrev = pLeft } _ = libc.Int32FromInt32(0) if (*TFts5ExprNode)(unsafe.Pointer(pRight)).FeType == FTS5_EOF { _ = libc.Int32FromInt32(0) _sqlite3Fts5ParseNodeFree(tls, pRight) pRet = pLeft (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase-- } else { if (*TFts5ExprNode)(unsafe.Pointer(pPrev)).FeType == FTS5_EOF { if pPrev == pLeft { pRet = pRight } else { *(*uintptr)(unsafe.Pointer(pLeft + 40 + uintptr((*TFts5ExprNode)(unsafe.Pointer(pLeft)).FnChild-int32(1))*4)) = pRight pRet = pLeft } ap = (*TFts5Parse)(unsafe.Pointer(pParse)).FapPhrase + uintptr((*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase-int32(1)-(*TFts5ExprNearset)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer(pRight)).FpNear)).FnPhrase)*4 _ = libc.Int32FromInt32(0) libc.Xmemmove(tls, ap, ap+1*4, uint32(4)*uint32((*TFts5ExprNearset)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer(pRight)).FpNear)).FnPhrase)) (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase-- _sqlite3Fts5ParseNodeFree(tls, pPrev) } else { pRet = _sqlite3Fts5ParseNode(tls, pParse, int32(FTS5_AND), pLeft, pRight, uintptr(0)) } } } return pRet } // C documentation // // /* // ** This is called during initialization to register the fts5_expr() scalar // ** UDF with the SQLite handle passed as the only argument. // */ func _sqlite3Fts5ExprInit(tls *libc.TLS, pGlobal uintptr, db uintptr) (r int32) { var rc int32 _ = rc rc = SQLITE_OK _ = pGlobal _ = db /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and ** sqlite3Fts5ParserFallback() are unused */ _ = __ccgo_fp(_sqlite3Fts5ParserFallback) return rc } // C documentation // // /* // ** Return the number of phrases in expression pExpr. // */ func _sqlite3Fts5ExprPhraseCount(tls *libc.TLS, pExpr uintptr) (r int32) { var v1 int32 _ = v1 if pExpr != 0 { v1 = (*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase } else { v1 = 0 } return v1 } // C documentation // // /* // ** Return the number of terms in the iPhrase'th phrase in pExpr. // */ func _sqlite3Fts5ExprPhraseSize(tls *libc.TLS, pExpr uintptr, iPhrase int32) (r int32) { if iPhrase < 0 || iPhrase >= (*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase { return 0 } return (*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(iPhrase)*4)))).FnTerm } // C documentation // // /* // ** This function is used to access the current position list for phrase // ** iPhrase. // */ func _sqlite3Fts5ExprPoslist(tls *libc.TLS, pExpr uintptr, iPhrase int32, pa uintptr) (r int32) { var nRet int32 var pNode, pPhrase uintptr _, _, _ = nRet, pNode, pPhrase pPhrase = *(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(iPhrase)*4)) pNode = (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FpNode if (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof == 0 && (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid == (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FpRoot)).FiRowid { *(*uintptr)(unsafe.Pointer(pa)) = (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fp nRet = (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fn } else { *(*uintptr)(unsafe.Pointer(pa)) = uintptr(0) nRet = 0 } return nRet } type TFts5PoslistPopulator1 = struct { F__ccgo_align [0]uint32 Fwriter TFts5PoslistWriter FbOk int32 FbMiss int32 } type Fts5PoslistPopulator1 = TFts5PoslistPopulator1 // C documentation // // /* // ** Clear the position lists associated with all phrases in the expression // ** passed as the first argument. Argument bLive is true if the expression // ** might be pointing to a real entry, otherwise it has just been reset. // ** // ** At present this function is only used for detail=col and detail=none // ** fts5 tables. This implies that all phrases must be at most 1 token // ** in size, as phrase matches are not supported without detail=full. // */ func _sqlite3Fts5ExprClearPoslists(tls *libc.TLS, pExpr uintptr, bLive int32) (r uintptr) { var i int32 var pBuf, pNode, pRet uintptr _, _, _, _ = i, pBuf, pNode, pRet pRet = Xsqlite3_malloc64(tls, uint64(uint32(16)*uint32((*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase))) if pRet != 0 { libc.Xmemset(tls, pRet, 0, uint32(16)*uint32((*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase)) i = 0 for { if !(i < (*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase) { break } pBuf = *(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(i)*4)) + 4 pNode = (*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(i)*4)))).FpNode _ = libc.Int32FromInt32(0) if bLive != 0 && ((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn == 0 || (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid != (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FpRoot)).FiRowid || (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof != 0) { (*(*TFts5PoslistPopulator)(unsafe.Pointer(pRet + uintptr(i)*16))).FbMiss = int32(1) } else { (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn = 0 } goto _1 _1: ; i++ } } return pRet } type TFts5ExprCtx1 = struct { F__ccgo_align [0]uint32 FpExpr uintptr FaPopulator uintptr FiOff Ti64 } type Fts5ExprCtx1 = TFts5ExprCtx1 type TFts5ExprCtx = struct { F__ccgo_align [0]uint32 FpExpr uintptr FaPopulator uintptr FiOff Ti64 } type Fts5ExprCtx = TFts5ExprCtx // C documentation // // /* // ** TODO: Make this more efficient! // */ func _fts5ExprColsetTest(tls *libc.TLS, pColset uintptr, iCol int32) (r int32) { var i int32 _ = i i = 0 for { if !(i < (*TFts5Colset)(unsafe.Pointer(pColset)).FnCol) { break } if *(*int32)(unsafe.Pointer(pColset + 4 + uintptr(i)*4)) == iCol { return int32(1) } goto _1 _1: ; i++ } return 0 } // C documentation // // /* // ** pToken is a buffer nToken bytes in size that may or may not contain // ** an embedded 0x00 byte. If it does, return the number of bytes in // ** the buffer before the 0x00. If it does not, return nToken. // */ func _fts5QueryTerm(tls *libc.TLS, pToken uintptr, nToken int32) (r int32) { var ii int32 _ = ii ii = 0 for { if !(ii < nToken && *(*uint8)(unsafe.Pointer(pToken + uintptr(ii))) != 0) { break } goto _1 _1: ; ii++ } return ii } func _fts5ExprPopulatePoslistsCb(tls *libc.TLS, pCtx uintptr, tflags int32, pToken uintptr, nToken int32, iUnused1 int32, iUnused2 int32) (r int32) { var i, iCol, iTokOff, nQuery, rc int32 var iRowid Ti64 var p, pExpr, pT uintptr _, _, _, _, _, _, _, _, _ = i, iCol, iRowid, iTokOff, nQuery, p, pExpr, pT, rc p = pCtx pExpr = (*TFts5ExprCtx)(unsafe.Pointer(p)).FpExpr nQuery = nToken iRowid = (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FpRoot)).FiRowid _ = iUnused1 _ = iUnused2 if nQuery > int32(FTS5_MAX_TOKEN_SIZE) { nQuery = int32(FTS5_MAX_TOKEN_SIZE) } if (*TFts5Config)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FpConfig)).FbTokendata != 0 { nQuery = _fts5QueryTerm(tls, pToken, nQuery) } if tflags&int32(FTS5_TOKEN_COLOCATED) == 0 { (*TFts5ExprCtx)(unsafe.Pointer(p)).FiOff++ } i = 0 for { if !(i < (*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase) { break } if (*(*TFts5PoslistPopulator)(unsafe.Pointer((*TFts5ExprCtx)(unsafe.Pointer(p)).FaPopulator + uintptr(i)*16))).FbOk == 0 { goto _1 } pT = *(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(i)*4)) + 20 for { if !(pT != 0) { break } if ((*TFts5ExprTerm)(unsafe.Pointer(pT)).FnQueryTerm == nQuery || (*TFts5ExprTerm)(unsafe.Pointer(pT)).FnQueryTerm < nQuery && (*TFts5ExprTerm)(unsafe.Pointer(pT)).FbPrefix != 0) && libc.Xmemcmp(tls, (*TFts5ExprTerm)(unsafe.Pointer(pT)).FpTerm, pToken, uint32((*TFts5ExprTerm)(unsafe.Pointer(pT)).FnQueryTerm)) == 0 { rc = _sqlite3Fts5PoslistWriterAppend(tls, *(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(i)*4))+4, (*TFts5ExprCtx)(unsafe.Pointer(p)).FaPopulator+uintptr(i)*16, (*TFts5ExprCtx)(unsafe.Pointer(p)).FiOff) if rc == SQLITE_OK && (*TFts5Config)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FpConfig)).FbTokendata != 0 && !((*TFts5ExprTerm)(unsafe.Pointer(pT)).FbPrefix != 0) { iCol = int32((*TFts5ExprCtx)(unsafe.Pointer(p)).FiOff >> int32(32)) iTokOff = int32((*TFts5ExprCtx)(unsafe.Pointer(p)).FiOff & int64(0x7FFFFFFF)) rc = _sqlite3Fts5IndexIterWriteTokendata(tls, (*TFts5ExprTerm)(unsafe.Pointer(pT)).FpIter, pToken, nToken, iRowid, iCol, iTokOff) } if rc != 0 { return rc } break } goto _2 _2: ; pT = (*TFts5ExprTerm)(unsafe.Pointer(pT)).FpSynonym } goto _1 _1: ; i++ } return SQLITE_OK } func _sqlite3Fts5ExprPopulatePoslists(tls *libc.TLS, pConfig uintptr, pExpr uintptr, aPopulator uintptr, iCol int32, z uintptr, n int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i int32 var pColset, pNode uintptr var _ /* sCtx at bp+0 */ TFts5ExprCtx _, _, _ = i, pColset, pNode (*(*TFts5ExprCtx)(unsafe.Pointer(bp))).FpExpr = pExpr (*(*TFts5ExprCtx)(unsafe.Pointer(bp))).FaPopulator = aPopulator (*(*TFts5ExprCtx)(unsafe.Pointer(bp))).FiOff = int64(iCol)< 0) case int32(FTS5_AND): i = 0 for { if !(i < (*TFts5ExprNode)(unsafe.Pointer(pNode)).FnChild) { break } if _fts5ExprCheckPoslists(tls, *(*uintptr)(unsafe.Pointer(pNode + 40 + uintptr(i)*4)), iRowid) == 0 { _fts5ExprClearPoslists(tls, pNode) return 0 } goto _1 _1: ; i++ } case int32(FTS5_OR): bRet = 0 i1 = 0 for { if !(i1 < (*TFts5ExprNode)(unsafe.Pointer(pNode)).FnChild) { break } if _fts5ExprCheckPoslists(tls, *(*uintptr)(unsafe.Pointer(pNode + 40 + uintptr(i1)*4)), iRowid) != 0 { bRet = int32(1) } goto _2 _2: ; i1++ } return bRet default: _ = libc.Int32FromInt32(0) if 0 == _fts5ExprCheckPoslists(tls, *(*uintptr)(unsafe.Pointer(pNode + 40)), iRowid) || 0 != _fts5ExprCheckPoslists(tls, *(*uintptr)(unsafe.Pointer(pNode + 40 + 1*4)), iRowid) { _fts5ExprClearPoslists(tls, pNode) return 0 } break } return int32(1) } func _sqlite3Fts5ExprCheckPoslists(tls *libc.TLS, pExpr uintptr, iRowid Ti64) { _fts5ExprCheckPoslists(tls, (*TFts5Expr)(unsafe.Pointer(pExpr)).FpRoot, iRowid) } // C documentation // // /* // ** This function is only called for detail=columns tables. // */ func _sqlite3Fts5ExprPhraseCollist(tls *libc.TLS, pExpr uintptr, iPhrase int32, ppCollist uintptr, pnCollist uintptr) (r int32) { var pBuf, pNode, pPhrase, pTerm uintptr var rc int32 _, _, _, _, _ = pBuf, pNode, pPhrase, pTerm, rc pPhrase = *(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(iPhrase)*4)) pNode = (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FpNode rc = SQLITE_OK _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof == 0 && (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid == (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FpRoot)).FiRowid && (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fn > 0 { pTerm = pPhrase + 20 if (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpSynonym != 0 { pBuf = (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpSynonym + 1*24 rc = _fts5ExprSynonymList(tls, pTerm, (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid, pBuf, ppCollist, pnCollist) } else { *(*uintptr)(unsafe.Pointer(ppCollist)) = (*TFts5IndexIter)(unsafe.Pointer((*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20))).FpIter)).FpData *(*int32)(unsafe.Pointer(pnCollist)) = (*TFts5IndexIter)(unsafe.Pointer((*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20))).FpIter)).FnData } } else { *(*uintptr)(unsafe.Pointer(ppCollist)) = uintptr(0) *(*int32)(unsafe.Pointer(pnCollist)) = 0 } return rc } // C documentation // // /* // ** Does the work of the fts5_api.xQueryToken() API method. // */ func _sqlite3Fts5ExprQueryToken(tls *libc.TLS, pExpr uintptr, iPhrase int32, iToken int32, ppOut uintptr, pnOut uintptr) (r int32) { var pPhrase uintptr _ = pPhrase pPhrase = uintptr(0) if iPhrase < 0 || iPhrase >= (*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase { return int32(SQLITE_RANGE) } pPhrase = *(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(iPhrase)*4)) if iToken < 0 || iToken >= (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm { return int32(SQLITE_RANGE) } *(*uintptr)(unsafe.Pointer(ppOut)) = (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20 + uintptr(iToken)*24))).FpTerm *(*int32)(unsafe.Pointer(pnOut)) = (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20 + uintptr(iToken)*24))).FnFullTerm return SQLITE_OK } // C documentation // // /* // ** Does the work of the fts5_api.xInstToken() API method. // */ func _sqlite3Fts5ExprInstToken(tls *libc.TLS, pExpr uintptr, iRowid Ti64, iPhrase int32, iCol int32, iOff int32, iToken int32, ppOut uintptr, pnOut uintptr) (r int32) { var pPhrase, pTerm uintptr var rc int32 _, _, _ = pPhrase, pTerm, rc pPhrase = uintptr(0) pTerm = uintptr(0) rc = SQLITE_OK if iPhrase < 0 || iPhrase >= (*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase { return int32(SQLITE_RANGE) } pPhrase = *(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(iPhrase)*4)) if iToken < 0 || iToken >= (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm { return int32(SQLITE_RANGE) } pTerm = pPhrase + 20 + uintptr(iToken)*24 if int32((*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FbPrefix) == 0 { if (*TFts5Config)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FpConfig)).FbTokendata != 0 { rc = _sqlite3Fts5IterToken(tls, (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpIter, iRowid, iCol, iOff+iToken, ppOut, pnOut) } else { *(*uintptr)(unsafe.Pointer(ppOut)) = (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpTerm *(*int32)(unsafe.Pointer(pnOut)) = (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FnFullTerm } } return rc } // C documentation // // /* // ** Clear the token mappings for all Fts5IndexIter objects mannaged by // ** the expression passed as the only argument. // */ func _sqlite3Fts5ExprClearTokens(tls *libc.TLS, pExpr uintptr) { var ii int32 var pT uintptr _, _ = ii, pT ii = 0 for { if !(ii < (*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase) { break } pT = *(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(ii)*4)) + 20 for { if !(pT != 0) { break } _sqlite3Fts5IndexIterClearTokendata(tls, (*TFts5ExprTerm)(unsafe.Pointer(pT)).FpIter) goto _2 _2: ; pT = (*TFts5ExprTerm)(unsafe.Pointer(pT)).FpSynonym } goto _1 _1: ; ii++ } } /* ** 2014 August 11 ** ** 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. ** ****************************************************************************** ** */ /* #include "fts5Int.h" */ type TFts5HashEntry = struct { F__ccgo_align [0]uint32 FpHashNext uintptr FpScanNext uintptr FnAlloc int32 FiSzPoslist int32 FnData int32 FnKey int32 FbDel Tu8 FbContent Tu8 FiCol Ti16 FiPos int32 FiRowid Ti64 } type Fts5HashEntry = TFts5HashEntry /* ** This file contains the implementation of an in-memory hash table used ** to accumuluate "term -> doclist" content before it is flused to a level-0 ** segment. */ type TFts5Hash1 = struct { FeDetail int32 FpnByte uintptr FnEntry int32 FnSlot int32 FpScan uintptr FaSlot uintptr } type Fts5Hash1 = TFts5Hash1 /* ** Each entry in the hash table is represented by an object of the ** following type. Each object, its key, and its current data are stored ** in a single memory allocation. The key immediately follows the object ** in memory. The position list data immediately follows the key data ** in memory. ** ** The key is Fts5HashEntry.nKey bytes in size. It consists of a single ** byte identifying the index (either the main term index or a prefix-index), ** followed by the term data. For example: "0token". There is no ** nul-terminator - in this case nKey=6. ** ** The data that follows the key is in a similar, but not identical format ** to the doclist data stored in the database. It is: ** ** * Rowid, as a varint ** * Position list, without 0x00 terminator. ** * Size of previous position list and rowid, as a 4 byte ** big-endian integer. ** ** iRowidOff: ** Offset of last rowid written to data area. Relative to first byte of ** structure. ** ** nData: ** Bytes of data written since iRowidOff. */ type TFts5HashEntry1 = struct { F__ccgo_align [0]uint32 FpHashNext uintptr FpScanNext uintptr FnAlloc int32 FiSzPoslist int32 FnData int32 FnKey int32 FbDel Tu8 FbContent Tu8 FiCol Ti16 FiPos int32 FiRowid Ti64 } type Fts5HashEntry1 = TFts5HashEntry1 /* ** Eqivalent to: ** ** char *fts5EntryKey(Fts5HashEntry *pEntry){ return zKey; } */ // C documentation // // /* // ** Allocate a new hash table. // */ func _sqlite3Fts5HashNew(tls *libc.TLS, pConfig uintptr, ppNew uintptr, pnByte uintptr) (r int32) { var nByte Tsqlite3_int64 var pNew, v1 uintptr var rc int32 _, _, _, _ = nByte, pNew, rc, v1 rc = SQLITE_OK v1 = Xsqlite3_malloc(tls, int32(24)) pNew = v1 *(*uintptr)(unsafe.Pointer(ppNew)) = v1 if pNew == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, pNew, 0, uint32(24)) (*TFts5Hash)(unsafe.Pointer(pNew)).FpnByte = pnByte (*TFts5Hash)(unsafe.Pointer(pNew)).FeDetail = (*TFts5Config)(unsafe.Pointer(pConfig)).FeDetail (*TFts5Hash)(unsafe.Pointer(pNew)).FnSlot = int32(1024) nByte = int64(uint32(4) * uint32((*TFts5Hash)(unsafe.Pointer(pNew)).FnSlot)) (*TFts5Hash)(unsafe.Pointer(pNew)).FaSlot = Xsqlite3_malloc64(tls, uint64(nByte)) if (*TFts5Hash)(unsafe.Pointer(pNew)).FaSlot == uintptr(0) { Xsqlite3_free(tls, pNew) *(*uintptr)(unsafe.Pointer(ppNew)) = uintptr(0) rc = int32(SQLITE_NOMEM) } else { libc.Xmemset(tls, (*TFts5Hash)(unsafe.Pointer(pNew)).FaSlot, 0, uint32(nByte)) } } return rc } // C documentation // // /* // ** Free a hash table object. // */ func _sqlite3Fts5HashFree(tls *libc.TLS, pHash uintptr) { if pHash != 0 { _sqlite3Fts5HashClear(tls, pHash) Xsqlite3_free(tls, (*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot) Xsqlite3_free(tls, pHash) } } // C documentation // // /* // ** Empty (but do not delete) a hash table. // */ func _sqlite3Fts5HashClear(tls *libc.TLS, pHash uintptr) { var i int32 var pNext, pSlot uintptr _, _, _ = i, pNext, pSlot i = 0 for { if !(i < (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot) { break } pSlot = *(*uintptr)(unsafe.Pointer((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(i)*4)) for { if !(pSlot != 0) { break } pNext = (*TFts5HashEntry)(unsafe.Pointer(pSlot)).FpHashNext Xsqlite3_free(tls, pSlot) goto _2 _2: ; pSlot = pNext } goto _1 _1: ; i++ } libc.Xmemset(tls, (*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot, 0, uint32((*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot)*uint32(4)) (*TFts5Hash)(unsafe.Pointer(pHash)).FnEntry = 0 } func _fts5HashKey(tls *libc.TLS, nSlot int32, p uintptr, n int32) (r uint32) { var h uint32 var i int32 _, _ = h, i h = uint32(13) i = n - int32(1) for { if !(i >= 0) { break } h = h<= 0) { break } h = h< (iRowid,iCol,iPos) // ** // ** Or, if iCol is negative, then the value is a delete marker. // */ func _sqlite3Fts5HashWrite(tls *libc.TLS, pHash uintptr, iRowid Ti64, iCol int32, iPos int32, bByte uint8, pToken uintptr, nToken int32) (r int32) { var bNew, nIncr, rc, v2, v4, v5, v7 int32 var iDiff Tu64 var iHash uint32 var nByte, nNew Tsqlite3_int64 var p, pNew, pPtr, pp, zKey, zKey1, v6 uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bNew, iDiff, iHash, nByte, nIncr, nNew, p, pNew, pPtr, pp, rc, zKey, zKey1, v2, v4, v5, v6, v7 nIncr = 0 /* If non-delete entry should be written */ bNew = libc.BoolInt32((*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail == FTS5_DETAIL_FULL) /* Attempt to locate an existing hash entry */ iHash = _fts5HashKey2(tls, (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot, bByte, pToken, nToken) p = *(*uintptr)(unsafe.Pointer((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iHash)*4)) for { if !(p != 0) { break } zKey = p + 1*40 if int32(*(*uint8)(unsafe.Pointer(zKey))) == int32(bByte) && (*TFts5HashEntry)(unsafe.Pointer(p)).FnKey == nToken+int32(1) && libc.Xmemcmp(tls, zKey+1, pToken, uint32(nToken)) == 0 { break } goto _1 _1: ; p = (*TFts5HashEntry)(unsafe.Pointer(p)).FpHashNext } /* If an existing hash entry cannot be found, create a new one. */ if p == uintptr(0) { nByte = int64(uint32(40) + uint32(nToken+libc.Int32FromInt32(1)) + uint32(1) + uint32(64)) if nByte < int64(128) { nByte = int64(128) } /* Grow the Fts5Hash.aSlot[] array if necessary. */ if (*TFts5Hash)(unsafe.Pointer(pHash)).FnEntry*int32(2) >= (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot { rc = _fts5HashResize(tls, pHash) if rc != SQLITE_OK { return rc } iHash = _fts5HashKey2(tls, (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot, bByte, pToken, nToken) } /* Allocate new Fts5HashEntry and add it to the hash table. */ p = Xsqlite3_malloc64(tls, uint64(nByte)) if !(p != 0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, p, 0, uint32(40)) (*TFts5HashEntry)(unsafe.Pointer(p)).FnAlloc = int32(nByte) zKey1 = p + 1*40 *(*uint8)(unsafe.Pointer(zKey1)) = bByte libc.Xmemcpy(tls, zKey1+1, pToken, uint32(nToken)) _ = libc.Int32FromInt32(0) (*TFts5HashEntry)(unsafe.Pointer(p)).FnKey = nToken + int32(1) *(*uint8)(unsafe.Pointer(zKey1 + uintptr(nToken+int32(1)))) = uint8('\000') (*TFts5HashEntry)(unsafe.Pointer(p)).FnData = int32(uint32(nToken+int32(1)) + uint32(40)) (*TFts5HashEntry)(unsafe.Pointer(p)).FpHashNext = *(*uintptr)(unsafe.Pointer((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iHash)*4)) *(*uintptr)(unsafe.Pointer((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iHash)*4)) = p (*TFts5Hash)(unsafe.Pointer(pHash)).FnEntry++ /* Add the first rowid field to the hash-entry */ *(*int32)(unsafe.Pointer(p + 16)) += _sqlite3Fts5PutVarint(tls, p+uintptr((*TFts5HashEntry)(unsafe.Pointer(p)).FnData), uint64(iRowid)) (*TFts5HashEntry)(unsafe.Pointer(p)).FiRowid = iRowid (*TFts5HashEntry)(unsafe.Pointer(p)).FiSzPoslist = (*TFts5HashEntry)(unsafe.Pointer(p)).FnData if (*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail != int32(FTS5_DETAIL_NONE) { *(*int32)(unsafe.Pointer(p + 16)) += int32(1) if (*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail == FTS5_DETAIL_FULL { v2 = 0 } else { v2 = -int32(1) } (*TFts5HashEntry)(unsafe.Pointer(p)).FiCol = int16(v2) } } else { /* Appending to an existing hash-entry. Check that there is enough ** space to append the largest possible new entry. Worst case scenario ** is: ** ** + 9 bytes for a new rowid, ** + 4 byte reserved for the "poslist size" varint. ** + 1 byte for a "new column" byte, ** + 3 bytes for a new column number (16-bit max) as a varint, ** + 5 bytes for the new position offset (32-bit max). */ if (*TFts5HashEntry)(unsafe.Pointer(p)).FnAlloc-(*TFts5HashEntry)(unsafe.Pointer(p)).FnData < libc.Int32FromInt32(9)+libc.Int32FromInt32(4)+libc.Int32FromInt32(1)+libc.Int32FromInt32(3)+libc.Int32FromInt32(5) { nNew = int64((*TFts5HashEntry)(unsafe.Pointer(p)).FnAlloc * int32(2)) pNew = Xsqlite3_realloc64(tls, p, uint64(nNew)) if pNew == uintptr(0) { return int32(SQLITE_NOMEM) } (*TFts5HashEntry)(unsafe.Pointer(pNew)).FnAlloc = int32(nNew) pp = (*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iHash)*4 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != p) { break } goto _3 _3: ; pp = *(*uintptr)(unsafe.Pointer(pp)) } *(*uintptr)(unsafe.Pointer(pp)) = pNew p = pNew } nIncr -= (*TFts5HashEntry)(unsafe.Pointer(p)).FnData } _ = libc.Int32FromInt32(0) pPtr = p /* If this is a new rowid, append the 4-byte size field for the previous ** entry, and the new rowid for this entry. */ if iRowid != (*TFts5HashEntry)(unsafe.Pointer(p)).FiRowid { iDiff = uint64(iRowid) - uint64((*TFts5HashEntry)(unsafe.Pointer(p)).FiRowid) _fts5HashAddPoslistSize(tls, pHash, p, uintptr(0)) *(*int32)(unsafe.Pointer(p + 16)) += _sqlite3Fts5PutVarint(tls, pPtr+uintptr((*TFts5HashEntry)(unsafe.Pointer(p)).FnData), iDiff) (*TFts5HashEntry)(unsafe.Pointer(p)).FiRowid = iRowid bNew = int32(1) (*TFts5HashEntry)(unsafe.Pointer(p)).FiSzPoslist = (*TFts5HashEntry)(unsafe.Pointer(p)).FnData if (*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail != int32(FTS5_DETAIL_NONE) { *(*int32)(unsafe.Pointer(p + 16)) += int32(1) if (*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail == FTS5_DETAIL_FULL { v4 = 0 } else { v4 = -int32(1) } (*TFts5HashEntry)(unsafe.Pointer(p)).FiCol = int16(v4) (*TFts5HashEntry)(unsafe.Pointer(p)).FiPos = 0 } } if iCol >= 0 { if (*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail == int32(FTS5_DETAIL_NONE) { (*TFts5HashEntry)(unsafe.Pointer(p)).FbContent = uint8(1) } else { /* Append a new column value, if necessary */ _ = libc.Int32FromInt32(0) if iCol != int32((*TFts5HashEntry)(unsafe.Pointer(p)).FiCol) { if (*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail == FTS5_DETAIL_FULL { v6 = p + 16 v5 = *(*int32)(unsafe.Pointer(v6)) *(*int32)(unsafe.Pointer(v6))++ *(*Tu8)(unsafe.Pointer(pPtr + uintptr(v5))) = uint8(0x01) *(*int32)(unsafe.Pointer(p + 16)) += _sqlite3Fts5PutVarint(tls, pPtr+uintptr((*TFts5HashEntry)(unsafe.Pointer(p)).FnData), uint64(iCol)) (*TFts5HashEntry)(unsafe.Pointer(p)).FiCol = int16(iCol) (*TFts5HashEntry)(unsafe.Pointer(p)).FiPos = 0 } else { bNew = int32(1) v7 = iCol iPos = v7 (*TFts5HashEntry)(unsafe.Pointer(p)).FiCol = int16(v7) } } /* Append the new position offset, if necessary */ if bNew != 0 { *(*int32)(unsafe.Pointer(p + 16)) += _sqlite3Fts5PutVarint(tls, pPtr+uintptr((*TFts5HashEntry)(unsafe.Pointer(p)).FnData), uint64(iPos-(*TFts5HashEntry)(unsafe.Pointer(p)).FiPos+int32(2))) (*TFts5HashEntry)(unsafe.Pointer(p)).FiPos = iPos } } } else { /* This is a delete. Set the delete flag. */ (*TFts5HashEntry)(unsafe.Pointer(p)).FbDel = uint8(1) } nIncr += (*TFts5HashEntry)(unsafe.Pointer(p)).FnData *(*int32)(unsafe.Pointer((*TFts5Hash)(unsafe.Pointer(pHash)).FpnByte)) += nIncr return SQLITE_OK } // C documentation // // /* // ** Arguments pLeft and pRight point to linked-lists of hash-entry objects, // ** each sorted in key order. This function merges the two lists into a // ** single list and returns a pointer to its first element. // */ func _fts5HashEntryMerge(tls *libc.TLS, pLeft uintptr, pRight uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var cmp, nMin, v1 int32 var p1, p2, ppOut, zKey1, zKey2 uintptr var _ /* pRet at bp+0 */ uintptr _, _, _, _, _, _, _, _ = cmp, nMin, p1, p2, ppOut, zKey1, zKey2, v1 p1 = pLeft p2 = pRight *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) ppOut = bp for p1 != 0 || p2 != 0 { if p1 == uintptr(0) { *(*uintptr)(unsafe.Pointer(ppOut)) = p2 p2 = uintptr(0) } else { if p2 == uintptr(0) { *(*uintptr)(unsafe.Pointer(ppOut)) = p1 p1 = uintptr(0) } else { zKey1 = p1 + 1*40 zKey2 = p2 + 1*40 if (*TFts5HashEntry)(unsafe.Pointer(p1)).FnKey < (*TFts5HashEntry)(unsafe.Pointer(p2)).FnKey { v1 = (*TFts5HashEntry)(unsafe.Pointer(p1)).FnKey } else { v1 = (*TFts5HashEntry)(unsafe.Pointer(p2)).FnKey } nMin = v1 cmp = libc.Xmemcmp(tls, zKey1, zKey2, uint32(nMin)) if cmp == 0 { cmp = (*TFts5HashEntry)(unsafe.Pointer(p1)).FnKey - (*TFts5HashEntry)(unsafe.Pointer(p2)).FnKey } _ = libc.Int32FromInt32(0) if cmp > 0 { /* p2 is smaller */ *(*uintptr)(unsafe.Pointer(ppOut)) = p2 ppOut = p2 + 4 p2 = (*TFts5HashEntry)(unsafe.Pointer(p2)).FpScanNext } else { /* p1 is smaller */ *(*uintptr)(unsafe.Pointer(ppOut)) = p1 ppOut = p1 + 4 p1 = (*TFts5HashEntry)(unsafe.Pointer(p1)).FpScanNext } *(*uintptr)(unsafe.Pointer(ppOut)) = uintptr(0) } } } return *(*uintptr)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Link all tokens from hash table iHash into a list in sorted order. The // ** tokens are not removed from the hash table. // */ func _fts5HashEntrySort(tls *libc.TLS, pHash uintptr, pTerm uintptr, nTerm int32, ppSorted uintptr) (r int32) { var ap, pEntry, pIter, pList uintptr var i, iSlot, nMergeSlot int32 _, _, _, _, _, _, _ = ap, i, iSlot, nMergeSlot, pEntry, pIter, pList nMergeSlot = int32(32) *(*uintptr)(unsafe.Pointer(ppSorted)) = uintptr(0) ap = Xsqlite3_malloc64(tls, uint64(uint32(4)*uint32(nMergeSlot))) if !(ap != 0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, ap, 0, uint32(4)*uint32(nMergeSlot)) iSlot = 0 for { if !(iSlot < (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot) { break } pIter = *(*uintptr)(unsafe.Pointer((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iSlot)*4)) for { if !(pIter != 0) { break } if pTerm == uintptr(0) || (*TFts5HashEntry)(unsafe.Pointer(pIter)).FnKey >= nTerm && 0 == libc.Xmemcmp(tls, pIter+1*40, pTerm, uint32(nTerm)) { pEntry = pIter (*TFts5HashEntry)(unsafe.Pointer(pEntry)).FpScanNext = uintptr(0) i = 0 for { if !(*(*uintptr)(unsafe.Pointer(ap + uintptr(i)*4)) != 0) { break } pEntry = _fts5HashEntryMerge(tls, pEntry, *(*uintptr)(unsafe.Pointer(ap + uintptr(i)*4))) *(*uintptr)(unsafe.Pointer(ap + uintptr(i)*4)) = uintptr(0) goto _3 _3: ; i++ } *(*uintptr)(unsafe.Pointer(ap + uintptr(i)*4)) = pEntry } goto _2 _2: ; pIter = (*TFts5HashEntry)(unsafe.Pointer(pIter)).FpHashNext } goto _1 _1: ; iSlot++ } pList = uintptr(0) i = 0 for { if !(i < nMergeSlot) { break } pList = _fts5HashEntryMerge(tls, pList, *(*uintptr)(unsafe.Pointer(ap + uintptr(i)*4))) goto _4 _4: ; i++ } Xsqlite3_free(tls, ap) *(*uintptr)(unsafe.Pointer(ppSorted)) = pList return SQLITE_OK } // C documentation // // /* // ** Query the hash table for a doclist associated with term pTerm/nTerm. // */ func _sqlite3Fts5HashQuery(tls *libc.TLS, pHash uintptr, nPre int32, pTerm uintptr, nTerm int32, ppOut uintptr, pnDoclist uintptr) (r int32) { var iHash uint32 var nHashPre, nList int32 var p, pFaux, pRet, zKey, v2 uintptr _, _, _, _, _, _, _, _ = iHash, nHashPre, nList, p, pFaux, pRet, zKey, v2 iHash = _fts5HashKey(tls, (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot, pTerm, nTerm) zKey = uintptr(0) p = *(*uintptr)(unsafe.Pointer((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iHash)*4)) for { if !(p != 0) { break } zKey = p + 1*40 if nTerm == (*TFts5HashEntry)(unsafe.Pointer(p)).FnKey && libc.Xmemcmp(tls, zKey, pTerm, uint32(nTerm)) == 0 { break } goto _1 _1: ; p = (*TFts5HashEntry)(unsafe.Pointer(p)).FpHashNext } if p != 0 { nHashPre = int32(uint32(40) + uint32(nTerm)) nList = (*TFts5HashEntry)(unsafe.Pointer(p)).FnData - nHashPre v2 = Xsqlite3_malloc64(tls, uint64(nPre+nList+int32(10))) *(*uintptr)(unsafe.Pointer(ppOut)) = v2 pRet = v2 if pRet != 0 { pFaux = pRet + uintptr(nPre-nHashPre) libc.Xmemcpy(tls, pRet+uintptr(nPre), p+uintptr(nHashPre), uint32(nList)) nList += _fts5HashAddPoslistSize(tls, pHash, p, pFaux) *(*int32)(unsafe.Pointer(pnDoclist)) = nList } else { *(*int32)(unsafe.Pointer(pnDoclist)) = 0 return int32(SQLITE_NOMEM) } } else { *(*uintptr)(unsafe.Pointer(ppOut)) = uintptr(0) *(*int32)(unsafe.Pointer(pnDoclist)) = 0 } return SQLITE_OK } func _sqlite3Fts5HashScanInit(tls *libc.TLS, p uintptr, pTerm uintptr, nTerm int32) (r int32) { return _fts5HashEntrySort(tls, p, pTerm, nTerm, p+16) } // C documentation // // /* // ** Return true if the hash table is empty, false otherwise. // */ func _sqlite3Fts5HashIsEmpty(tls *libc.TLS, pHash uintptr) (r int32) { _ = libc.Int32FromInt32(0) return libc.BoolInt32((*TFts5Hash)(unsafe.Pointer(pHash)).FnEntry == 0) } func _sqlite3Fts5HashScanNext(tls *libc.TLS, p uintptr) { _ = libc.Int32FromInt32(0) (*TFts5Hash)(unsafe.Pointer(p)).FpScan = (*TFts5HashEntry)(unsafe.Pointer((*TFts5Hash)(unsafe.Pointer(p)).FpScan)).FpScanNext } func _sqlite3Fts5HashScanEof(tls *libc.TLS, p uintptr) (r int32) { return libc.BoolInt32((*TFts5Hash)(unsafe.Pointer(p)).FpScan == uintptr(0)) } func _sqlite3Fts5HashScanEntry(tls *libc.TLS, pHash uintptr, pzTerm uintptr, pnTerm uintptr, ppDoclist uintptr, pnDoclist uintptr) { var nTerm int32 var p, zKey, v1 uintptr _, _, _, _ = nTerm, p, zKey, v1 v1 = (*TFts5Hash)(unsafe.Pointer(pHash)).FpScan p = v1 if v1 != 0 { zKey = p + 1*40 nTerm = (*TFts5HashEntry)(unsafe.Pointer(p)).FnKey _fts5HashAddPoslistSize(tls, pHash, p, uintptr(0)) *(*uintptr)(unsafe.Pointer(pzTerm)) = zKey *(*int32)(unsafe.Pointer(pnTerm)) = nTerm *(*uintptr)(unsafe.Pointer(ppDoclist)) = zKey + uintptr(nTerm) *(*int32)(unsafe.Pointer(pnDoclist)) = int32(uint32((*TFts5HashEntry)(unsafe.Pointer(p)).FnData) - (uint32(40) + uint32(nTerm))) } else { *(*uintptr)(unsafe.Pointer(pzTerm)) = uintptr(0) *(*int32)(unsafe.Pointer(pnTerm)) = 0 *(*uintptr)(unsafe.Pointer(ppDoclist)) = uintptr(0) *(*int32)(unsafe.Pointer(pnDoclist)) = 0 } } /* ** 2014 May 31 ** ** 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. ** ****************************************************************************** ** ** Low level access to the FTS index stored in the database file. The ** routines in this file file implement all read and write access to the ** %_data table. Other parts of the system access this functionality via ** the interface defined in fts5Int.h. */ /* #include "fts5Int.h" */ /* ** Overview: ** ** The %_data table contains all the FTS indexes for an FTS5 virtual table. ** As well as the main term index, there may be up to 31 prefix indexes. ** The format is similar to FTS3/4, except that: ** ** * all segment b-tree leaf data is stored in fixed size page records ** (e.g. 1000 bytes). A single doclist may span multiple pages. Care is ** taken to ensure it is possible to iterate in either direction through ** the entries in a doclist, or to seek to a specific entry within a ** doclist, without loading it into memory. ** ** * large doclists that span many pages have associated "doclist index" ** records that contain a copy of the first rowid on each page spanned by ** the doclist. This is used to speed up seek operations, and merges of ** large doclists with very small doclists. ** ** * extra fields in the "structure record" record the state of ongoing ** incremental merge operations. ** */ /* ** There are two versions of the format used for the structure record: ** ** 1. the legacy format, that may be read by all fts5 versions, and ** ** 2. the V2 format, which is used by contentless_delete=1 databases. ** ** Both begin with a 4-byte "configuration cookie" value. Then, a legacy ** format structure record contains a varint - the number of levels in ** the structure. Whereas a V2 structure record contains the constant ** 4 bytes [0xff 0x00 0x00 0x01]. This is unambiguous as the value of a ** varint has to be at least 16256 to begin with "0xFF". And the default ** maximum number of levels is 64. ** ** See below for more on structure record formats. */ /* ** Details: ** ** The %_data table managed by this module, ** ** CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB); ** ** , contains the following 6 types of records. See the comments surrounding ** the FTS5_*_ROWID macros below for a description of how %_data rowids are ** assigned to each fo them. ** ** 1. Structure Records: ** ** The set of segments that make up an index - the index structure - are ** recorded in a single record within the %_data table. The record consists ** of a single 32-bit configuration cookie value followed by a list of ** SQLite varints. ** ** If the structure record is a V2 record, the configuration cookie is ** followed by the following 4 bytes: [0xFF 0x00 0x00 0x01]. ** ** Next, the record continues with three varints: ** ** + number of levels, ** + total number of segments on all levels, ** + value of write counter. ** ** Then, for each level from 0 to nMax: ** ** + number of input segments in ongoing merge. ** + total number of segments in level. ** + for each segment from oldest to newest: ** + segment id (always > 0) ** + first leaf page number (often 1, always greater than 0) ** + final leaf page number ** ** Then, for V2 structures only: ** ** + lower origin counter value, ** + upper origin counter value, ** + the number of tombstone hash pages. ** ** 2. The Averages Record: ** ** A single record within the %_data table. The data is a list of varints. ** The first value is the number of rows in the index. Then, for each column ** from left to right, the total number of tokens in the column for all ** rows of the table. ** ** 3. Segment leaves: ** ** TERM/DOCLIST FORMAT: ** ** Most of each segment leaf is taken up by term/doclist data. The ** general format of term/doclist, starting with the first term ** on the leaf page, is: ** ** varint : size of first term ** blob: first term data ** doclist: first doclist ** zero-or-more { ** varint: number of bytes in common with previous term ** varint: number of bytes of new term data (nNew) ** blob: nNew bytes of new term data ** doclist: next doclist ** } ** ** doclist format: ** ** varint: first rowid ** poslist: first poslist ** zero-or-more { ** varint: rowid delta (always > 0) ** poslist: next poslist ** } ** ** poslist format: ** ** varint: size of poslist in bytes multiplied by 2, not including ** this field. Plus 1 if this entry carries the "delete" flag. ** collist: collist for column 0 ** zero-or-more { ** 0x01 byte ** varint: column number (I) ** collist: collist for column I ** } ** ** collist format: ** ** varint: first offset + 2 ** zero-or-more { ** varint: offset delta + 2 ** } ** ** PAGE FORMAT ** ** Each leaf page begins with a 4-byte header containing 2 16-bit ** unsigned integer fields in big-endian format. They are: ** ** * The byte offset of the first rowid on the page, if it exists ** and occurs before the first term (otherwise 0). ** ** * The byte offset of the start of the page footer. If the page ** footer is 0 bytes in size, then this field is the same as the ** size of the leaf page in bytes. ** ** The page footer consists of a single varint for each term located ** on the page. Each varint is the byte offset of the current term ** within the page, delta-compressed against the previous value. In ** other words, the first varint in the footer is the byte offset of ** the first term, the second is the byte offset of the second less that ** of the first, and so on. ** ** The term/doclist format described above is accurate if the entire ** term/doclist data fits on a single leaf page. If this is not the case, ** the format is changed in two ways: ** ** + if the first rowid on a page occurs before the first term, it ** is stored as a literal value: ** ** varint: first rowid ** ** + the first term on each page is stored in the same way as the ** very first term of the segment: ** ** varint : size of first term ** blob: first term data ** ** 5. Segment doclist indexes: ** ** Doclist indexes are themselves b-trees, however they usually consist of ** a single leaf record only. The format of each doclist index leaf page ** is: ** ** * Flags byte. Bits are: ** 0x01: Clear if leaf is also the root page, otherwise set. ** ** * Page number of fts index leaf page. As a varint. ** ** * First rowid on page indicated by previous field. As a varint. ** ** * A list of varints, one for each subsequent termless page. A ** positive delta if the termless page contains at least one rowid, ** or an 0x00 byte otherwise. ** ** Internal doclist index nodes are: ** ** * Flags byte. Bits are: ** 0x01: Clear for root page, otherwise set. ** ** * Page number of first child page. As a varint. ** ** * Copy of first rowid on page indicated by previous field. As a varint. ** ** * A list of delta-encoded varints - the first rowid on each subsequent ** child page. ** ** 6. Tombstone Hash Page ** ** These records are only ever present in contentless_delete=1 tables. ** There are zero or more of these associated with each segment. They ** are used to store the tombstone rowids for rows contained in the ** associated segments. ** ** The set of nHashPg tombstone hash pages associated with a single ** segment together form a single hash table containing tombstone rowids. ** To find the page of the hash on which a key might be stored: ** ** iPg = (rowid % nHashPg) ** ** Then, within page iPg, which has nSlot slots: ** ** iSlot = (rowid / nHashPg) % nSlot ** ** Each tombstone hash page begins with an 8 byte header: ** ** 1-byte: Key-size (the size in bytes of each slot). Either 4 or 8. ** 1-byte: rowid-0-tombstone flag. This flag is only valid on the ** first tombstone hash page for each segment (iPg=0). If set, ** the hash table contains rowid 0. If clear, it does not. ** Rowid 0 is handled specially. ** 2-bytes: unused. ** 4-bytes: Big-endian integer containing number of entries on page. ** ** Following this are nSlot 4 or 8 byte slots (depending on the key-size ** in the first byte of the page header). The number of slots may be ** determined based on the size of the page record and the key-size: ** ** nSlot = (nByte - 8) / key-size */ /* ** Rowids for the averages and structure records in the %_data table. */ /* ** Macros determining the rowids used by segment leaves and dlidx leaves ** and nodes. All nodes and leaves are stored in the %_data table with large ** positive rowids. ** ** Each segment has a unique non-zero 16-bit id. ** ** The rowid for each segment leaf is found by passing the segment id and ** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered ** sequentially starting from 1. */ /* ** Each time a blob is read from the %_data table, it is padded with this ** many zero bytes. This makes it easier to decode the various record formats ** without overreading if the records are corrupt. */ type TFts5Data = struct { Fp uintptr Fnn int32 FszLeaf int32 } type Fts5Data = TFts5Data type TFts5DlidxIter = struct { F__ccgo_align [0]uint32 FnLvl int32 FiSegid int32 FaLvl [1]TFts5DlidxLvl } type Fts5DlidxIter = TFts5DlidxIter type TFts5DlidxLvl = struct { F__ccgo_align [0]uint32 FpData uintptr FiOff int32 FbEof int32 FiFirstOff int32 FiLeafPgno int32 F__ccgo_align5 [4]byte FiRowid Ti64 } type Fts5DlidxLvl = TFts5DlidxLvl type TFts5DlidxWriter = struct { F__ccgo_align [0]uint32 Fpgno int32 FbPrevValid int32 FiPrev Ti64 Fbuf TFts5Buffer F__ccgo_pad4 [4]byte } type Fts5DlidxWriter = TFts5DlidxWriter type TFts5Iter = struct { F__ccgo_align [0]uint32 Fbase TFts5IndexIter FpTokenDataIter uintptr FpIndex uintptr Fposlist TFts5Buffer FpColset uintptr FxSetOutputs uintptr FnSeg int32 FbRev int32 FbSkipEmpty Tu8 F__ccgo_align9 [3]byte FiSwitchRowid Ti64 FaFirst uintptr F__ccgo_align11 [4]byte FaSeg [1]TFts5SegIter } type Fts5Iter = TFts5Iter type TFts5PageWriter = struct { Fpgno int32 FiPrevPgidx int32 Fbuf TFts5Buffer Fpgidx TFts5Buffer Fterm TFts5Buffer } type Fts5PageWriter = TFts5PageWriter type TFts5SegIter = struct { F__ccgo_align [0]uint32 FpSeg uintptr Fflags int32 FiLeafPgno int32 FpLeaf uintptr FpNextLeaf uintptr F__ccgo_align5 [4]byte FiLeafOffset Ti64 FpTombArray uintptr FxNext uintptr FiTermLeafPgno int32 FiTermLeafOffset int32 FiPgidxOff int32 FiEndofDoclist int32 FiRowidOffset int32 FnRowidOffset int32 FaRowidOffset uintptr FpDlidx uintptr Fterm TFts5Buffer F__ccgo_align17 [4]byte FiRowid Ti64 FnPos int32 FbDel Tu8 F__ccgo_pad20 [3]byte } type Fts5SegIter = TFts5SegIter type TFts5DoclistIter = struct { F__ccgo_align [0]uint32 FaEof uintptr F__ccgo_align1 [4]byte FiRowid Ti64 FaPoslist uintptr FnPoslist int32 FnSize int32 F__ccgo_pad5 [4]byte } type Fts5DoclistIter = TFts5DoclistIter type TFts5SegWriter = struct { F__ccgo_align [0]uint32 FiSegid int32 Fwriter TFts5PageWriter FiPrevRowid Ti64 FbFirstRowidInDoclist Tu8 FbFirstRowidInPage Tu8 FbFirstTermInPage Tu8 FnLeafWritten int32 FnEmpty int32 FnDlidx int32 FaDlidx uintptr Fbtterm TFts5Buffer FiBtPage int32 F__ccgo_pad12 [4]byte } type Fts5SegWriter = TFts5SegWriter type TFts5Structure = struct { F__ccgo_align [0]uint32 FnRef int32 F__ccgo_align1 [4]byte FnWriteCounter Tu64 FnOriginCntr Tu64 FnSegment int32 FnLevel int32 FaLevel [1]TFts5StructureLevel F__ccgo_pad6 [4]byte } type Fts5Structure = TFts5Structure type TFts5StructureLevel = struct { FnMerge int32 FnSeg int32 FaSeg uintptr } type Fts5StructureLevel = TFts5StructureLevel type TFts5StructureSegment = struct { F__ccgo_align [0]uint32 FiSegid int32 FpgnoFirst int32 FpgnoLast int32 F__ccgo_align3 [4]byte FiOrigin1 Tu64 FiOrigin2 Tu64 FnPgTombstone int32 F__ccgo_align6 [4]byte FnEntryTombstone Tu64 FnEntry Tu64 } type Fts5StructureSegment = TFts5StructureSegment type TFts5TokenDataIter = struct { FnIter int32 FnIterAlloc int32 FnMap int32 FnMapAlloc int32 FaMap uintptr FaPoslistReader uintptr FaPoslistToIter uintptr FapIter [1]uintptr } type Fts5TokenDataIter = TFts5TokenDataIter type TFts5TokenDataMap = struct { F__ccgo_align [0]uint32 FiRowid Ti64 FiPos Ti64 FiIter int32 F__ccgo_pad3 [4]byte } type Fts5TokenDataMap = TFts5TokenDataMap type TFts5TombstoneArray = struct { FnRef int32 FnTombstone int32 FapTombstone [1]uintptr } type Fts5TombstoneArray = TFts5TombstoneArray type TFts5Data1 = struct { Fp uintptr Fnn int32 FszLeaf int32 } type Fts5Data1 = TFts5Data1 /* ** One object per %_data table. ** ** nContentlessDelete: ** The number of contentless delete operations since the most recent ** call to fts5IndexFlush() or fts5IndexDiscardData(). This is tracked ** so that extra auto-merge work can be done by fts5IndexFlush() to ** account for the delete operations. */ type TFts5Index1 = struct { F__ccgo_align [0]uint32 FpConfig uintptr FzDataTbl uintptr FnWorkUnit int32 FpHash uintptr FnPendingData int32 F__ccgo_align5 [4]byte FiWriteRowid Ti64 FbDelete int32 FnContentlessDelete int32 FnPendingRow int32 Frc int32 FflushRc int32 FpReader uintptr FpWriter uintptr FpDeleter uintptr FpIdxWriter uintptr FpIdxDeleter uintptr FpIdxSelect uintptr FpIdxNextSelect uintptr FnRead int32 FpDeleteFromIdx uintptr FpDataVersion uintptr F__ccgo_align21 [4]byte FiStructVersion Ti64 FpStruct uintptr F__ccgo_pad23 [4]byte } type Fts5Index1 = TFts5Index1 type TFts5DoclistIter1 = struct { F__ccgo_align [0]uint32 FaEof uintptr F__ccgo_align1 [4]byte FiRowid Ti64 FaPoslist uintptr FnPoslist int32 FnSize int32 F__ccgo_pad5 [4]byte } type Fts5DoclistIter1 = TFts5DoclistIter1 /* ** The contents of the "structure" record for each index are represented ** using an Fts5Structure record in memory. Which uses instances of the ** other Fts5StructureXXX types as components. ** ** nOriginCntr: ** This value is set to non-zero for structure records created for ** contentlessdelete=1 tables only. In that case it represents the ** origin value to apply to the next top-level segment created. */ type TFts5StructureSegment1 = struct { F__ccgo_align [0]uint32 FiSegid int32 FpgnoFirst int32 FpgnoLast int32 F__ccgo_align3 [4]byte FiOrigin1 Tu64 FiOrigin2 Tu64 FnPgTombstone int32 F__ccgo_align6 [4]byte FnEntryTombstone Tu64 FnEntry Tu64 } type Fts5StructureSegment1 = TFts5StructureSegment1 type TFts5StructureLevel1 = struct { FnMerge int32 FnSeg int32 FaSeg uintptr } type Fts5StructureLevel1 = TFts5StructureLevel1 type TFts5Structure1 = struct { F__ccgo_align [0]uint32 FnRef int32 F__ccgo_align1 [4]byte FnWriteCounter Tu64 FnOriginCntr Tu64 FnSegment int32 FnLevel int32 FaLevel [1]TFts5StructureLevel F__ccgo_pad6 [4]byte } type Fts5Structure1 = TFts5Structure1 /* ** An object of type Fts5SegWriter is used to write to segments. */ type TFts5PageWriter1 = struct { Fpgno int32 FiPrevPgidx int32 Fbuf TFts5Buffer Fpgidx TFts5Buffer Fterm TFts5Buffer } type Fts5PageWriter1 = TFts5PageWriter1 type TFts5DlidxWriter1 = struct { F__ccgo_align [0]uint32 Fpgno int32 FbPrevValid int32 FiPrev Ti64 Fbuf TFts5Buffer F__ccgo_pad4 [4]byte } type Fts5DlidxWriter1 = TFts5DlidxWriter1 type TFts5SegWriter1 = struct { F__ccgo_align [0]uint32 FiSegid int32 Fwriter TFts5PageWriter FiPrevRowid Ti64 FbFirstRowidInDoclist Tu8 FbFirstRowidInPage Tu8 FbFirstTermInPage Tu8 FnLeafWritten int32 FnEmpty int32 FnDlidx int32 FaDlidx uintptr Fbtterm TFts5Buffer FiBtPage int32 F__ccgo_pad12 [4]byte } type Fts5SegWriter1 = TFts5SegWriter1 type TFts5CResult = struct { FiFirst Tu16 FbTermEq Tu8 } type Fts5CResult = TFts5CResult type TFts5CResult1 = struct { FiFirst Tu16 FbTermEq Tu8 } type Fts5CResult1 = TFts5CResult1 /* ** Object for iterating through a single segment, visiting each term/rowid ** pair in the segment. ** ** pSeg: ** The segment to iterate through. ** ** iLeafPgno: ** Current leaf page number within segment. ** ** iLeafOffset: ** Byte offset within the current leaf that is the first byte of the ** position list data (one byte passed the position-list size field). ** ** pLeaf: ** Buffer containing current leaf page data. Set to NULL at EOF. ** ** iTermLeafPgno, iTermLeafOffset: ** Leaf page number containing the last term read from the segment. And ** the offset immediately following the term data. ** ** flags: ** Mask of FTS5_SEGITER_XXX values. Interpreted as follows: ** ** FTS5_SEGITER_ONETERM: ** If set, set the iterator to point to EOF after the current doclist ** has been exhausted. Do not proceed to the next term in the segment. ** ** FTS5_SEGITER_REVERSE: ** This flag is only ever set if FTS5_SEGITER_ONETERM is also set. If ** it is set, iterate through rowid in descending order instead of the ** default ascending order. ** ** iRowidOffset/nRowidOffset/aRowidOffset: ** These are used if the FTS5_SEGITER_REVERSE flag is set. ** ** For each rowid on the page corresponding to the current term, the ** corresponding aRowidOffset[] entry is set to the byte offset of the ** start of the "position-list-size" field within the page. ** ** iTermIdx: ** Index of current term on iTermLeafPgno. ** ** apTombstone/nTombstone: ** These are used for contentless_delete=1 tables only. When the cursor ** is first allocated, the apTombstone[] array is allocated so that it ** is large enough for all tombstones hash pages associated with the ** segment. The pages themselves are loaded lazily from the database as ** they are required. */ type TFts5SegIter1 = struct { F__ccgo_align [0]uint32 FpSeg uintptr Fflags int32 FiLeafPgno int32 FpLeaf uintptr FpNextLeaf uintptr F__ccgo_align5 [4]byte FiLeafOffset Ti64 FpTombArray uintptr FxNext uintptr FiTermLeafPgno int32 FiTermLeafOffset int32 FiPgidxOff int32 FiEndofDoclist int32 FiRowidOffset int32 FnRowidOffset int32 FaRowidOffset uintptr FpDlidx uintptr Fterm TFts5Buffer F__ccgo_align17 [4]byte FiRowid Ti64 FnPos int32 FbDel Tu8 F__ccgo_pad20 [3]byte } type Fts5SegIter1 = TFts5SegIter1 /* ** Array of tombstone pages. Reference counted. */ type TFts5TombstoneArray1 = struct { FnRef int32 FnTombstone int32 FapTombstone [1]uintptr } type Fts5TombstoneArray1 = TFts5TombstoneArray1 /* ** Argument is a pointer to an Fts5Data structure that contains a ** leaf page. */ /* ** Argument is a pointer to an Fts5Data structure that contains a leaf ** page. This macro evaluates to true if the leaf contains no terms, or ** false if it contains at least one term. */ /* ** Object for iterating through the merged results of one or more segments, ** visiting each term/rowid pair in the merged data. ** ** nSeg is always a power of two greater than or equal to the number of ** segments that this object is merging data from. Both the aSeg[] and ** aFirst[] arrays are sized at nSeg entries. The aSeg[] array is padded ** with zeroed objects - these are handled as if they were iterators opened ** on empty segments. ** ** The results of comparing segments aSeg[N] and aSeg[N+1], where N is an ** even number, is stored in aFirst[(nSeg+N)/2]. The "result" of the ** comparison in this context is the index of the iterator that currently ** points to the smaller term/rowid combination. Iterators at EOF are ** considered to be greater than all other iterators. ** ** aFirst[1] contains the index in aSeg[] of the iterator that points to ** the smallest key overall. aFirst[0] is unused. ** ** poslist: ** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered. ** There is no way to tell if this is populated or not. ** ** pColset: ** If not NULL, points to an object containing a set of column indices. ** Only matches that occur in one of these columns will be returned. ** The Fts5Iter does not own the Fts5Colset object, and so it is not ** freed when the iterator is closed - it is owned by the upper layer. */ type TFts5Iter1 = struct { F__ccgo_align [0]uint32 Fbase TFts5IndexIter FpTokenDataIter uintptr FpIndex uintptr Fposlist TFts5Buffer FpColset uintptr FxSetOutputs uintptr FnSeg int32 FbRev int32 FbSkipEmpty Tu8 F__ccgo_align9 [3]byte FiSwitchRowid Ti64 FaFirst uintptr F__ccgo_align11 [4]byte FaSeg [1]TFts5SegIter } type Fts5Iter1 = TFts5Iter1 /* ** An instance of the following type is used to iterate through the contents ** of a doclist-index record. ** ** pData: ** Record containing the doclist-index data. ** ** bEof: ** Set to true once iterator has reached EOF. ** ** iOff: ** Set to the current offset within record pData. */ type TFts5DlidxLvl1 = struct { F__ccgo_align [0]uint32 FpData uintptr FiOff int32 FbEof int32 FiFirstOff int32 FiLeafPgno int32 F__ccgo_align5 [4]byte FiRowid Ti64 } type Fts5DlidxLvl1 = TFts5DlidxLvl1 type TFts5DlidxIter1 = struct { F__ccgo_align [0]uint32 FnLvl int32 FiSegid int32 FaLvl [1]TFts5DlidxLvl } type Fts5DlidxIter1 = TFts5DlidxIter1 func _fts5PutU16(tls *libc.TLS, aOut uintptr, iVal Tu16) { *(*Tu8)(unsafe.Pointer(aOut)) = uint8(int32(iVal) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(aOut + 1)) = uint8(int32(iVal) & libc.Int32FromInt32(0xFF)) } func _fts5GetU16(tls *libc.TLS, aIn uintptr) (r Tu16) { return uint16(int32(uint16(*(*Tu8)(unsafe.Pointer(aIn))))<> libc.Int32FromInt32(56) & libc.Uint64FromInt32(0xFF)) *(*Tu8)(unsafe.Pointer(a + 1)) = uint8(iVal >> libc.Int32FromInt32(48) & libc.Uint64FromInt32(0xFF)) *(*Tu8)(unsafe.Pointer(a + 2)) = uint8(iVal >> libc.Int32FromInt32(40) & libc.Uint64FromInt32(0xFF)) *(*Tu8)(unsafe.Pointer(a + 3)) = uint8(iVal >> libc.Int32FromInt32(32) & libc.Uint64FromInt32(0xFF)) *(*Tu8)(unsafe.Pointer(a + 4)) = uint8(iVal >> libc.Int32FromInt32(24) & libc.Uint64FromInt32(0xFF)) *(*Tu8)(unsafe.Pointer(a + 5)) = uint8(iVal >> libc.Int32FromInt32(16) & libc.Uint64FromInt32(0xFF)) *(*Tu8)(unsafe.Pointer(a + 6)) = uint8(iVal >> libc.Int32FromInt32(8) & libc.Uint64FromInt32(0xFF)) *(*Tu8)(unsafe.Pointer(a + 7)) = uint8(iVal >> libc.Int32FromInt32(0) & libc.Uint64FromInt32(0xFF)) } // C documentation // // /* // ** Write iVal, formated as a 32-bit big-endian unsigned integer, to the // ** buffer indicated by the first argument. // */ func _fts5PutU32(tls *libc.TLS, a uintptr, iVal Tu32) { *(*Tu8)(unsafe.Pointer(a)) = uint8(iVal >> libc.Int32FromInt32(24) & libc.Uint32FromInt32(0xFF)) *(*Tu8)(unsafe.Pointer(a + 1)) = uint8(iVal >> libc.Int32FromInt32(16) & libc.Uint32FromInt32(0xFF)) *(*Tu8)(unsafe.Pointer(a + 2)) = uint8(iVal >> libc.Int32FromInt32(8) & libc.Uint32FromInt32(0xFF)) *(*Tu8)(unsafe.Pointer(a + 3)) = uint8(iVal >> libc.Int32FromInt32(0) & libc.Uint32FromInt32(0xFF)) } // C documentation // // /* // ** Allocate and return a buffer at least nByte bytes in size. // ** // ** If an OOM error is encountered, return NULL and set the error code in // ** the Fts5Index handle passed as the first argument. // */ func _fts5IdxMalloc(tls *libc.TLS, p uintptr, nByte Tsqlite3_int64) (r uintptr) { return _sqlite3Fts5MallocZero(tls, p+44, nByte) } /* ** Compare the contents of the pLeft buffer with the pRight/nRight blob. ** ** Return -ve if pLeft is smaller than pRight, 0 if they are equal or ** +ve if pRight is smaller than pLeft. In other words: ** ** res = *pLeft - *pRight */ // C documentation // // /* // ** Compare the contents of the two buffers using memcmp(). If one buffer // ** is a prefix of the other, it is considered the lesser. // ** // ** Return -ve if pLeft is smaller than pRight, 0 if they are equal or // ** +ve if pRight is smaller than pLeft. In other words: // ** // ** res = *pLeft - *pRight // */ func _fts5BufferCompare(tls *libc.TLS, pLeft uintptr, pRight uintptr) (r int32) { var nCmp, res, v1, v2, v3 int32 _, _, _, _, _ = nCmp, res, v1, v2, v3 if (*TFts5Buffer)(unsafe.Pointer(pLeft)).Fn < (*TFts5Buffer)(unsafe.Pointer(pRight)).Fn { v1 = (*TFts5Buffer)(unsafe.Pointer(pLeft)).Fn } else { v1 = (*TFts5Buffer)(unsafe.Pointer(pRight)).Fn } nCmp = v1 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if nCmp <= 0 { v2 = 0 } else { v2 = libc.Xmemcmp(tls, (*TFts5Buffer)(unsafe.Pointer(pLeft)).Fp, (*TFts5Buffer)(unsafe.Pointer(pRight)).Fp, uint32(nCmp)) } res = v2 if res == 0 { v3 = (*TFts5Buffer)(unsafe.Pointer(pLeft)).Fn - (*TFts5Buffer)(unsafe.Pointer(pRight)).Fn } else { v3 = res } return v3 } func _fts5LeafFirstTermOff(tls *libc.TLS, pLeaf uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var _ /* ret at bp+0 */ int32 _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr((*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf), bp) return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Close the read-only blob handle, if it is open. // */ func _sqlite3Fts5IndexCloseReader(tls *libc.TLS, p uintptr) { var pReader uintptr _ = pReader if (*TFts5Index)(unsafe.Pointer(p)).FpReader != 0 { pReader = (*TFts5Index)(unsafe.Pointer(p)).FpReader (*TFts5Index)(unsafe.Pointer(p)).FpReader = uintptr(0) Xsqlite3_blob_close(tls, pReader) } } // C documentation // // /* // ** Retrieve a record from the %_data table. // ** // ** If an error occurs, NULL is returned and an error left in the // ** Fts5Index object. // */ func _fts5DataRead(tls *libc.TLS, p uintptr, iRowid Ti64) (r uintptr) { var aOut, pBlob, pConfig, pRet, v1 uintptr var nAlloc Tsqlite3_int64 var nByte, rc int32 _, _, _, _, _, _, _, _ = aOut, nAlloc, nByte, pBlob, pConfig, pRet, rc, v1 pRet = uintptr(0) if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { rc = SQLITE_OK if (*TFts5Index)(unsafe.Pointer(p)).FpReader != 0 { /* This call may return SQLITE_ABORT if there has been a savepoint ** rollback since it was last used. In this case a new blob handle ** is required. */ pBlob = (*TFts5Index)(unsafe.Pointer(p)).FpReader (*TFts5Index)(unsafe.Pointer(p)).FpReader = uintptr(0) rc = Xsqlite3_blob_reopen(tls, pBlob, iRowid) _ = libc.Int32FromInt32(0) (*TFts5Index)(unsafe.Pointer(p)).FpReader = pBlob if rc != SQLITE_OK { _sqlite3Fts5IndexCloseReader(tls, p) } if rc == int32(SQLITE_ABORT) { rc = SQLITE_OK } } /* If the blob handle is not open at this point, open it and seek ** to the requested entry. */ if (*TFts5Index)(unsafe.Pointer(p)).FpReader == uintptr(0) && rc == SQLITE_OK { pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig rc = Xsqlite3_blob_open(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Index)(unsafe.Pointer(p)).FzDataTbl, __ccgo_ts+36166, iRowid, 0, p+52) } /* If either of the sqlite3_blob_open() or sqlite3_blob_reopen() calls ** above returned SQLITE_ERROR, return SQLITE_CORRUPT_VTAB instead. ** All the reasons those functions might return SQLITE_ERROR - missing ** table, missing row, non-blob/text in block column - indicate ** backing store corruption. */ if rc == int32(SQLITE_ERROR) { rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< (*TFts5Data)(unsafe.Pointer(pRet)).Fnn { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<= v1 { _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) { break } Xsqlite3_free(tls, (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(i)*12))).FaSeg) goto _4 _4: ; i++ } Xsqlite3_free(tls, pStruct) } } func _fts5StructureRef(tls *libc.TLS, pStruct uintptr) { (*TFts5Structure)(unsafe.Pointer(pStruct)).FnRef++ } func _sqlite3Fts5StructureRef(tls *libc.TLS, p uintptr) (r uintptr) { _fts5StructureRef(tls, (*TFts5Index)(unsafe.Pointer(p)).FpStruct) return (*TFts5Index)(unsafe.Pointer(p)).FpStruct } func _sqlite3Fts5StructureRelease(tls *libc.TLS, p uintptr) { if p != 0 { _fts5StructureRelease(tls, p) } } func _sqlite3Fts5StructureTest(tls *libc.TLS, p uintptr, pStruct uintptr) (r int32) { if (*TFts5Index)(unsafe.Pointer(p)).FpStruct != pStruct { return int32(SQLITE_ABORT) } return SQLITE_OK } // C documentation // // /* // ** Ensure that structure object (*pp) is writable. // ** // ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called. If // ** an error occurs, (*pRc) is set to an SQLite error code before returning. // */ func _fts5StructureMakeWritable(tls *libc.TLS, pRc uintptr, pp uintptr) { var i int32 var nByte Ti64 var p, pLvl, pNew uintptr _, _, _, _, _ = i, nByte, p, pLvl, pNew p = *(*uintptr)(unsafe.Pointer(pp)) if *(*int32)(unsafe.Pointer(pRc)) == SQLITE_OK && (*TFts5Structure)(unsafe.Pointer(p)).FnRef > int32(1) { nByte = int64(uint32(48) + uint32((*TFts5Structure)(unsafe.Pointer(p)).FnLevel-libc.Int32FromInt32(1))*uint32(12)) pNew = _sqlite3Fts5MallocZero(tls, pRc, nByte) if pNew != 0 { libc.Xmemcpy(tls, pNew, p, uint32(nByte)) i = 0 for { if !(i < (*TFts5Structure)(unsafe.Pointer(p)).FnLevel) { break } (*(*TFts5StructureLevel)(unsafe.Pointer(pNew + 32 + uintptr(i)*12))).FaSeg = uintptr(0) goto _1 _1: ; i++ } i = 0 for { if !(i < (*TFts5Structure)(unsafe.Pointer(p)).FnLevel) { break } pLvl = pNew + 32 + uintptr(i)*12 nByte = int64(uint32(56) * uint32((*(*TFts5StructureLevel)(unsafe.Pointer(pNew + 32 + uintptr(i)*12))).FnSeg)) (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg = _sqlite3Fts5MallocZero(tls, pRc, nByte) if (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg == uintptr(0) { i = 0 for { if !(i < (*TFts5Structure)(unsafe.Pointer(p)).FnLevel) { break } Xsqlite3_free(tls, (*(*TFts5StructureLevel)(unsafe.Pointer(pNew + 32 + uintptr(i)*12))).FaSeg) goto _3 _3: ; i++ } Xsqlite3_free(tls, pNew) return } libc.Xmemcpy(tls, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg, (*(*TFts5StructureLevel)(unsafe.Pointer(p + 32 + uintptr(i)*12))).FaSeg, uint32(nByte)) goto _2 _2: ; i++ } (*TFts5Structure)(unsafe.Pointer(p)).FnRef-- (*TFts5Structure)(unsafe.Pointer(pNew)).FnRef = int32(1) } *(*uintptr)(unsafe.Pointer(pp)) = pNew } } // C documentation // // /* // ** Deserialize and return the structure record currently stored in serialized // ** form within buffer pData/nData. // ** // ** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array // ** are over-allocated by one slot. This allows the structure contents // ** to be more easily edited. // ** // ** If an error occurs, *ppOut is set to NULL and an SQLite error code // ** returned. Otherwise, *ppOut is set to point to the new object and // ** SQLITE_OK returned. // */ func _fts5StructureDecode(tls *libc.TLS, pData uintptr, nData int32, piCookie uintptr, ppOut uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bStructureV2, i, iLvl, iSeg int32 var nByte Tsqlite3_int64 var nOriginCntr Tu64 var pLvl, pRet, pSeg uintptr var v3 uint64 var _ /* nLevel at bp+4 */ int32 var _ /* nSegment at bp+8 */ int32 var _ /* nTotal at bp+12 */ int32 var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _ = bStructureV2, i, iLvl, iSeg, nByte, nOriginCntr, pLvl, pRet, pSeg, v3 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK i = 0 *(*int32)(unsafe.Pointer(bp + 4)) = 0 *(*int32)(unsafe.Pointer(bp + 8)) = 0 /* Bytes of space to allocate at pRet */ pRet = uintptr(0) /* Structure object to return */ bStructureV2 = 0 /* True for FTS5_STRUCTURE_V2 */ nOriginCntr = uint64(0) /* Largest origin value seen so far */ /* Grab the cookie value */ if piCookie != 0 { *(*int32)(unsafe.Pointer(piCookie)) = _sqlite3Fts5Get32(tls, pData) } i = int32(4) /* Check if this is a V2 structure record. Set bStructureV2 if it is. */ if 0 == libc.Xmemcmp(tls, pData+uintptr(i), __ccgo_ts+36312, uint32(4)) { i += int32(4) bStructureV2 = int32(1) } /* Read the total number of levels and segments from the start of the ** structure record. */ i += _sqlite3Fts5GetVarint32(tls, pData+uintptr(i), bp+4) i += _sqlite3Fts5GetVarint32(tls, pData+uintptr(i), bp+8) if *(*int32)(unsafe.Pointer(bp + 4)) > int32(FTS5_MAX_SEGMENT) || *(*int32)(unsafe.Pointer(bp + 4)) < 0 || *(*int32)(unsafe.Pointer(bp + 8)) > int32(FTS5_MAX_SEGMENT) || *(*int32)(unsafe.Pointer(bp + 8)) < 0 { return libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<= nData { *(*int32)(unsafe.Pointer(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<= nData { *(*int32)(unsafe.Pointer(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin2 { v3 = nOriginCntr } else { v3 = (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin2 } nOriginCntr = v3 } if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast < (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst { *(*int32)(unsafe.Pointer(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< 0 && (*(*TFts5StructureLevel)(unsafe.Pointer(pLvl + uintptr(-libc.Int32FromInt32(1))*12))).FnMerge != 0 && *(*int32)(unsafe.Pointer(bp + 12)) == 0 { *(*int32)(unsafe.Pointer(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< uint64(0) { v1 = libc.Int32FromInt32(4) + libc.Int32FromInt32(4) + libc.Int32FromInt32(9) + libc.Int32FromInt32(9) + libc.Int32FromInt32(9) } else { v1 = libc.Int32FromInt32(4) + libc.Int32FromInt32(9) + libc.Int32FromInt32(9) } /* Cookie value to store */ nHdr = v1 _ = libc.Int32FromInt32(0) libc.Xmemset(tls, bp, 0, uint32(12)) /* Append the current configuration cookie */ iCookie = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FiCookie if iCookie < 0 { iCookie = 0 } if 0 == _sqlite3Fts5BufferSize(tls, p+44, bp, uint32(nHdr)) { _sqlite3Fts5Put32(tls, (*(*TFts5Buffer)(unsafe.Pointer(bp))).Fp, iCookie) (*(*TFts5Buffer)(unsafe.Pointer(bp))).Fn = int32(4) if (*TFts5Structure)(unsafe.Pointer(pStruct)).FnOriginCntr > uint64(0) { _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(bp)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp)).Fn), __ccgo_ts+36312, uint32(4)) *(*int32)(unsafe.Pointer(bp + 4)) += int32(4) } *(*int32)(unsafe.Pointer(bp + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp)).Fn), uint64((*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel)) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp)).Fn), uint64((*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment)) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp)).Fn), uint64(int64((*TFts5Structure)(unsafe.Pointer(pStruct)).FnWriteCounter))) _ = libc.Int32FromInt32(0) } iLvl = 0 for { if !(iLvl < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) { break } /* Used to iterate through segments */ pLvl = pStruct + 32 + uintptr(iLvl)*12 _sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge)) _sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg)) _ = libc.Int32FromInt32(0) iSeg = 0 for { if !(iSeg < (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg) { break } pSeg = (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr(iSeg)*56 _sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid)) _sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst)) _sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast)) if (*TFts5Structure)(unsafe.Pointer(pStruct)).FnOriginCntr > uint64(0) { _sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin1)) _sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin2)) _sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone)) _sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnEntryTombstone)) _sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnEntry)) } goto _3 _3: ; iSeg++ } goto _2 _2: ; iLvl++ } _fts5DataWrite(tls, p, int64(FTS5_STRUCTURE_ROWID), (*(*TFts5Buffer)(unsafe.Pointer(bp))).Fp, (*(*TFts5Buffer)(unsafe.Pointer(bp))).Fn) _sqlite3Fts5BufferFree(tls, bp) } } func _fts5SegmentSize(tls *libc.TLS, pSeg uintptr) (r int32) { return int32(1) + (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast - (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst } // C documentation // // /* // ** Return a copy of index structure pStruct. Except, promote as many // ** segments as possible to level iPromote. If an OOM occurs, NULL is // ** returned. // */ func _fts5StructurePromoteTo(tls *libc.TLS, p uintptr, iPromote int32, szPromote int32, pStruct uintptr) { var il, is, sz int32 var pLvl, pOut uintptr _, _, _, _, _ = il, is, pLvl, pOut, sz pOut = pStruct + 32 + uintptr(iPromote)*12 if (*TFts5StructureLevel)(unsafe.Pointer(pOut)).FnMerge == 0 { il = iPromote + int32(1) for { if !(il < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) { break } pLvl = pStruct + 32 + uintptr(il)*12 if (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge != 0 { return } is = (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg - int32(1) for { if !(is >= 0) { break } sz = _fts5SegmentSize(tls, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg+uintptr(is)*56) if sz > szPromote { return } _fts5StructureExtendLevel(tls, p+44, pStruct, iPromote, int32(1), int32(1)) if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 { return } libc.Xmemcpy(tls, (*TFts5StructureLevel)(unsafe.Pointer(pOut)).FaSeg, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg+uintptr(is)*56, uint32(56)) (*TFts5StructureLevel)(unsafe.Pointer(pOut)).FnSeg++ (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg-- goto _2 _2: ; is-- } goto _1 _1: ; il++ } } } // C documentation // // /* // ** A new segment has just been written to level iLvl of index structure // ** pStruct. This function determines if any segments should be promoted // ** as a result. Segments are promoted in two scenarios: // ** // ** a) If the segment just written is smaller than one or more segments // ** within the previous populated level, it is promoted to the previous // ** populated level. // ** // ** b) If the segment just written is larger than the newest segment on // ** the next populated level, then that segment, and any other adjacent // ** segments that are also smaller than the one just written, are // ** promoted. // ** // ** If one or more segments are promoted, the structure object is updated // ** to reflect this. // */ func _fts5StructurePromote(tls *libc.TLS, p uintptr, iLvl int32, pStruct uintptr) { var i, iPromote, iTst, nSeg, sz, szMax, szPromote, szSeg int32 var pSeg, pTst uintptr _, _, _, _, _, _, _, _, _, _ = i, iPromote, iTst, nSeg, pSeg, pTst, sz, szMax, szPromote, szSeg if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { iPromote = -int32(1) szPromote = 0 /* Size of segment just written */ nSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FnSeg if nSeg == 0 { return } pSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FaSeg + uintptr((*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FnSeg-int32(1))*56 szSeg = int32(1) + (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast - (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst /* Check for condition (a) */ iTst = iLvl - int32(1) for { if !(iTst >= 0 && (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iTst)*12))).FnSeg == 0) { break } goto _1 _1: ; iTst-- } if iTst >= 0 { szMax = 0 pTst = pStruct + 32 + uintptr(iTst)*12 _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*TFts5StructureLevel)(unsafe.Pointer(pTst)).FnSeg) { break } sz = (*(*TFts5StructureSegment)(unsafe.Pointer((*TFts5StructureLevel)(unsafe.Pointer(pTst)).FaSeg + uintptr(i)*56))).FpgnoLast - (*(*TFts5StructureSegment)(unsafe.Pointer((*TFts5StructureLevel)(unsafe.Pointer(pTst)).FaSeg + uintptr(i)*56))).FpgnoFirst + int32(1) if sz > szMax { szMax = sz } goto _2 _2: ; i++ } if szMax >= szSeg { /* Condition (a) is true. Promote the newest segment on level ** iLvl to level iTst. */ iPromote = iTst szPromote = szMax } } /* If condition (a) is not met, assume (b) is true. StructurePromoteTo() ** is a no-op if it is not. */ if iPromote < 0 { iPromote = iLvl szPromote = szSeg } _fts5StructurePromoteTo(tls, p, iPromote, szPromote, pStruct) } } // C documentation // // /* // ** Advance the iterator passed as the only argument. If the end of the // ** doclist-index page is reached, return non-zero. // */ func _fts5DlidxLvlNext(tls *libc.TLS, pLvl uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iOff int32 var pData, p2 uintptr var _ /* iVal at bp+0 */ Tu64 _, _, _ = iOff, pData, p2 pData = (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData if (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff == 0 { _ = libc.Int32FromInt32(0) (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff = int32(1) *(*int32)(unsafe.Pointer(pLvl + 4)) += _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp+1, pLvl+16) *(*int32)(unsafe.Pointer(pLvl + 4)) += int32(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp+uintptr((*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff), pLvl+24)) (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiFirstOff = (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff } else { iOff = (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff for { if !(iOff < (*TFts5Data)(unsafe.Pointer(pData)).Fnn) { break } if *(*Tu8)(unsafe.Pointer((*TFts5Data)(unsafe.Pointer(pData)).Fp + uintptr(iOff))) != 0 { break } goto _1 _1: ; iOff++ } if iOff < (*TFts5Data)(unsafe.Pointer(pData)).Fnn { *(*int32)(unsafe.Pointer(pLvl + 16)) += iOff - (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff + int32(1) iOff += int32(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp+uintptr(iOff), bp)) p2 = pLvl + 24 *(*Ti64)(unsafe.Pointer(p2)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(p2))) + *(*Tu64)(unsafe.Pointer(bp))) (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff = iOff } else { (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FbEof = int32(1) } } return (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FbEof } // C documentation // // /* // ** Advance the iterator passed as the only argument. // */ func _fts5DlidxIterNextR(tls *libc.TLS, p uintptr, pIter uintptr, iLvl int32) (r int32) { var pLvl uintptr _ = pLvl pLvl = pIter + 8 + uintptr(iLvl)*32 _ = libc.Int32FromInt32(0) if _fts5DlidxLvlNext(tls, pLvl) != 0 { if iLvl+int32(1) < (*TFts5DlidxIter)(unsafe.Pointer(pIter)).FnLvl { _fts5DlidxIterNextR(tls, p, pIter, iLvl+int32(1)) if (*(*TFts5DlidxLvl)(unsafe.Pointer(pLvl + 1*32))).FbEof == 0 { _fts5DataRelease(tls, (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData) libc.Xmemset(tls, pLvl, 0, uint32(32)) (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData = _fts5DataRead(tls, p, int64((*TFts5DlidxIter)(unsafe.Pointer(pIter)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(1))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(iLvl)<iLeafPgno is the page number the // ** doclist is associated with (the one featuring the term). // */ func _fts5DlidxIterFirst(tls *libc.TLS, pIter uintptr) (r int32) { var i int32 _ = i i = 0 for { if !(i < (*TFts5DlidxIter)(unsafe.Pointer(pIter)).FnLvl) { break } _fts5DlidxLvlNext(tls, pIter+8+uintptr(i)*32) goto _1 _1: ; i++ } return (*(*TFts5DlidxLvl)(unsafe.Pointer(pIter + 8))).FbEof } func _fts5DlidxIterEof(tls *libc.TLS, p uintptr, pIter uintptr) (r int32) { return libc.BoolInt32((*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK || (*(*TFts5DlidxLvl)(unsafe.Pointer(pIter + 8))).FbEof != 0) } func _fts5DlidxIterLast(tls *libc.TLS, p uintptr, pIter uintptr) { var i int32 var pChild, pLvl uintptr _, _, _ = i, pChild, pLvl /* Advance each level to the last entry on the last page */ i = (*TFts5DlidxIter)(unsafe.Pointer(pIter)).FnLvl - int32(1) for { if !((*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && i >= 0) { break } pLvl = pIter + 8 + uintptr(i)*32 for _fts5DlidxLvlNext(tls, pLvl) == 0 { } (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FbEof = 0 if i > 0 { pChild = pLvl + uintptr(-libc.Int32FromInt32(1))*32 _fts5DataRelease(tls, (*TFts5DlidxLvl)(unsafe.Pointer(pChild)).FpData) libc.Xmemset(tls, pChild, 0, uint32(32)) (*TFts5DlidxLvl)(unsafe.Pointer(pChild)).FpData = _fts5DataRead(tls, p, int64((*TFts5DlidxIter)(unsafe.Pointer(pIter)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(1))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(i-libc.Int32FromInt32(1))<= iOff { break } *(*int32)(unsafe.Pointer(pLvl + 16)) += nZero + int32(1) p1 = pLvl + 24 *(*Ti64)(unsafe.Pointer(p1)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(p1))) + *(*Tu64)(unsafe.Pointer(bp))) (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff = ii } } return (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FbEof } func _fts5DlidxIterPrevR(tls *libc.TLS, p uintptr, pIter uintptr, iLvl int32) (r int32) { var pLvl uintptr _ = pLvl pLvl = pIter + 8 + uintptr(iLvl)*32 _ = libc.Int32FromInt32(0) if _fts5DlidxLvlPrev(tls, pLvl) != 0 { if iLvl+int32(1) < (*TFts5DlidxIter)(unsafe.Pointer(pIter)).FnLvl { _fts5DlidxIterPrevR(tls, p, pIter, iLvl+int32(1)) if (*(*TFts5DlidxLvl)(unsafe.Pointer(pLvl + 1*32))).FbEof == 0 { _fts5DataRelease(tls, (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData) libc.Xmemset(tls, pLvl, 0, uint32(32)) (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData = _fts5DataRead(tls, p, int64((*TFts5DlidxIter)(unsafe.Pointer(pIter)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(1))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(iLvl)<= (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn { (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn + int32(1) } else { *(*int32)(unsafe.Pointer(pIter + 48)) += _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr((*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff), pIter+52) } } } // C documentation // // /* // ** Argument p points to a buffer containing a varint to be interpreted as a // ** position list size field. Read the varint and return the number of bytes // ** read. Before returning, set *pnSz to the number of bytes in the position // ** list, and *pbDel to true if the delete flag is set, or false otherwise. // */ func _fts5GetPoslistSize(tls *libc.TLS, p uintptr, pnSz uintptr, pbDel uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var n, v1 int32 var _ /* nSz at bp+0 */ int32 _, _ = n, v1 n = 0 v1 = n n++ *(*int32)(unsafe.Pointer(bp)) = int32(*(*Tu8)(unsafe.Pointer(p + uintptr(v1)))) if *(*int32)(unsafe.Pointer(bp))&int32(0x80) != 0 { n-- n += _sqlite3Fts5GetVarint32(tls, p+uintptr(n), bp) } _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pnSz)) = *(*int32)(unsafe.Pointer(bp)) / int32(2) *(*int32)(unsafe.Pointer(pbDel)) = *(*int32)(unsafe.Pointer(bp)) & int32(0x0001) return n } // C documentation // // /* // ** Fts5SegIter.iLeafOffset currently points to the first byte of a // ** position-list size field. Read the value of the field and store it // ** in the following variables: // ** // ** Fts5SegIter.nPos // ** Fts5SegIter.bDel // ** // ** Leave Fts5SegIter.iLeafOffset pointing to the first byte of the // ** position list content (if any). // */ func _fts5SegIterLoadNPos(tls *libc.TLS, p uintptr, pIter uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var iEod, iOff, v1, v2 int32 var _ /* nSz at bp+0 */ int32 _, _, _, _ = iEod, iOff, v1, v2 if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { iOff = int32((*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset) /* Offset to read at */ _ = libc.Int32FromInt32(0) if (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail == int32(FTS5_DETAIL_NONE) { if (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist < (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf { v1 = (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist } else { v1 = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf } iEod = v1 (*TFts5SegIter)(unsafe.Pointer(pIter)).FbDel = uint8(0) (*TFts5SegIter)(unsafe.Pointer(pIter)).FnPos = int32(1) if iOff < iEod && int32(*(*Tu8)(unsafe.Pointer((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp + uintptr(iOff)))) == 0 { (*TFts5SegIter)(unsafe.Pointer(pIter)).FbDel = uint8(1) iOff++ if iOff < iEod && int32(*(*Tu8)(unsafe.Pointer((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp + uintptr(iOff)))) == 0 { (*TFts5SegIter)(unsafe.Pointer(pIter)).FnPos = int32(1) iOff++ } else { (*TFts5SegIter)(unsafe.Pointer(pIter)).FnPos = 0 } } } else { v2 = iOff iOff++ *(*int32)(unsafe.Pointer(bp)) = int32(*(*Tu8)(unsafe.Pointer((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp + uintptr(v2)))) if *(*int32)(unsafe.Pointer(bp))&int32(0x80) != 0 { iOff-- iOff += _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp+uintptr(iOff), bp) } (*TFts5SegIter)(unsafe.Pointer(pIter)).FbDel = uint8(*(*int32)(unsafe.Pointer(bp)) & libc.Int32FromInt32(0x0001)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FnPos = *(*int32)(unsafe.Pointer(bp)) >> int32(1) _ = libc.Int32FromInt32(0) } (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(iOff) } } func _fts5SegIterLoadRowid(tls *libc.TLS, p uintptr, pIter uintptr) { var a uintptr var iOff Ti64 _, _ = a, iOff a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp /* Buffer to read data from */ iOff = (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset _ = libc.Int32FromInt32(0) for iOff >= int64((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf) { _fts5SegIterNextPage(tls, p, pIter) if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf == uintptr(0) { if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< int64((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf) || nKeep > (*TFts5SegIter)(unsafe.Pointer(pIter)).Fterm.Fn || *(*int32)(unsafe.Pointer(bp)) == 0 { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<= (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn { (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn + int32(1) } else { *(*int32)(unsafe.Pointer(pIter + 48)) += _sqlite3Fts5GetVarint32(tls, a+uintptr((*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff), bp+4) *(*int32)(unsafe.Pointer(pIter + 52)) += *(*int32)(unsafe.Pointer(bp + 4)) } _fts5SegIterLoadRowid(tls, p, pIter) } func _fts5SegIterSetNext(tls *libc.TLS, p uintptr, pIter uintptr) { if (*TFts5SegIter)(unsafe.Pointer(pIter)).Fflags&int32(FTS5_SEGITER_REVERSE) != 0 { (*TFts5SegIter)(unsafe.Pointer(pIter)).FxNext = __ccgo_fp(_fts5SegIterNext_Reverse) } else { if (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail == int32(FTS5_DETAIL_NONE) { (*TFts5SegIter)(unsafe.Pointer(pIter)).FxNext = __ccgo_fp(_fts5SegIterNext_None) } else { (*TFts5SegIter)(unsafe.Pointer(pIter)).FxNext = __ccgo_fp(_fts5SegIterNext) } } } // C documentation // // /* // ** Allocate a tombstone hash page array object (pIter->pTombArray) for // ** the iterator passed as the second argument. If an OOM error occurs, // ** leave an error in the Fts5Index object. // */ func _fts5SegIterAllocTombstone(tls *libc.TLS, p uintptr, pIter uintptr) { var nByte, nTomb int32 var pNew uintptr _, _, _ = nByte, nTomb, pNew nTomb = (*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg)).FnPgTombstone if nTomb > 0 { nByte = int32(uint32(nTomb)*uint32(4) + uint32(12)) pNew = _sqlite3Fts5MallocZero(tls, p+44, int64(nByte)) if pNew != 0 { (*TFts5TombstoneArray)(unsafe.Pointer(pNew)).FnTombstone = nTomb (*TFts5TombstoneArray)(unsafe.Pointer(pNew)).FnRef = int32(1) (*TFts5SegIter)(unsafe.Pointer(pIter)).FpTombArray = pNew } } } // C documentation // // /* // ** Initialize the iterator object pIter to iterate through the entries in // ** segment pSeg. The iterator is left pointing to the first entry when // ** this function returns. // ** // ** If an error occurs, Fts5Index.rc is set to an appropriate error code. If // ** an error has already occurred when this function is called, it is a no-op. // */ func _fts5SegIterInit(tls *libc.TLS, p uintptr, pSeg uintptr, pIter uintptr) { if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst == 0 { /* This happens if the segment is being used as an input to an incremental ** merge and all data has already been "trimmed". See function ** fts5TrimSegments() for details. In this case leave the iterator empty. ** The caller will see the (pIter->pLeaf==0) and assume the iterator is ** at EOF already. */ _ = libc.Int32FromInt32(0) return } if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { libc.Xmemset(tls, pIter, 0, uint32(104)) _fts5SegIterSetNext(tls, p, pIter) (*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg = pSeg (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno = (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst - int32(1) for cond := true; cond; cond = (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf != 0 && (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn == int32(4) { _fts5SegIterNextPage(tls, p, pIter) } } if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf != 0 { (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(4) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf + int32(1) _fts5SegIterLoadTerm(tls, p, pIter, 0) _fts5SegIterLoadNPos(tls, p, pIter) _fts5SegIterAllocTombstone(tls, p, pIter) } } // C documentation // // /* // ** This function is only ever called on iterators created by calls to // ** Fts5IndexQuery() with the FTS5INDEX_QUERY_DESC flag set. // ** // ** The iterator is in an unusual state when this function is called: the // ** Fts5SegIter.iLeafOffset variable is set to the offset of the start of // ** the position-list size field for the first relevant rowid on the page. // ** Fts5SegIter.rowid is set, but nPos and bDel are not. // ** // ** This function advances the iterator so that it points to the last // ** relevant rowid on the page and, if necessary, initializes the // ** aRowidOffset[] and iRowidOffset variables. At this point the iterator // ** is in its regular state - Fts5SegIter.iLeafOffset points to the first // ** byte of the position list content associated with said rowid. // */ func _fts5SegIterReverseInitPage(tls *libc.TLS, p uintptr, pIter uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var a, aNew, p1 uintptr var eDetail, i, iRowidOffset, n, nNew, v2 int32 var _ /* bDummy at bp+12 */ int32 var _ /* iDelta at bp+0 */ Tu64 var _ /* nPos at bp+8 */ int32 _, _, _, _, _, _, _, _, _ = a, aNew, eDetail, i, iRowidOffset, n, nNew, v2, p1 eDetail = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail n = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf i = int32((*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset) a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp iRowidOffset = 0 if n > (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist { n = (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist } _ = libc.Int32FromInt32(0) for int32(1) != 0 { *(*Tu64)(unsafe.Pointer(bp)) = uint64(0) if eDetail == int32(FTS5_DETAIL_NONE) { /* todo */ if i < n && int32(*(*Tu8)(unsafe.Pointer(a + uintptr(i)))) == 0 { i++ if i < n && int32(*(*Tu8)(unsafe.Pointer(a + uintptr(i)))) == 0 { i++ } } } else { i += _fts5GetPoslistSize(tls, a+uintptr(i), bp+8, bp+12) i += *(*int32)(unsafe.Pointer(bp + 8)) } if i >= n { break } i += int32(_sqlite3Fts5GetVarint(tls, a+uintptr(i), bp)) p1 = pIter + 88 *(*Ti64)(unsafe.Pointer(p1)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(p1))) + *(*Tu64)(unsafe.Pointer(bp))) /* If necessary, grow the pIter->aRowidOffset[] array. */ if iRowidOffset >= (*TFts5SegIter)(unsafe.Pointer(pIter)).FnRowidOffset { nNew = (*TFts5SegIter)(unsafe.Pointer(pIter)).FnRowidOffset + int32(8) aNew = Xsqlite3_realloc64(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FaRowidOffset, uint64(uint32(nNew)*uint32(4))) if aNew == uintptr(0) { (*TFts5Index)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) break } (*TFts5SegIter)(unsafe.Pointer(pIter)).FaRowidOffset = aNew (*TFts5SegIter)(unsafe.Pointer(pIter)).FnRowidOffset = nNew } v2 = iRowidOffset iRowidOffset++ *(*int32)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FaRowidOffset + uintptr(v2)*4)) = int32((*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(i) } (*TFts5SegIter)(unsafe.Pointer(pIter)).FiRowidOffset = iRowidOffset _fts5SegIterLoadNPos(tls, p, pIter) } // C documentation // // /* // ** // */ func _fts5SegIterReverseNewPage(tls *libc.TLS, p uintptr, pIter uintptr) { var a, pNew uintptr var iRowidOff int32 _, _, _ = a, iRowidOff, pNew _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf) (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = uintptr(0) for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno > (*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafPgno { (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno-- pNew = _fts5DataRead(tls, p, int64((*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(libc.Int32FromInt32(0))<pLeaf==0, this iterator is at EOF. */ if (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno == (*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafPgno { _ = libc.Int32FromInt32(0) if (*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafOffset < (*TFts5Data)(unsafe.Pointer(pNew)).FszLeaf { (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = pNew (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64((*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafOffset) } } else { iRowidOff = int32(_fts5GetU16(tls, (*TFts5Data)(unsafe.Pointer(pNew)).Fp)) if iRowidOff != 0 { if iRowidOff >= (*TFts5Data)(unsafe.Pointer(pNew)).FszLeaf { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< 0 { a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp (*TFts5SegIter)(unsafe.Pointer(pIter)).FiRowidOffset-- (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(*(*int32)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FaRowidOffset + uintptr((*TFts5SegIter)(unsafe.Pointer(pIter)).FiRowidOffset)*4))) _fts5SegIterLoadNPos(tls, p, pIter) iOff = int32((*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset) if (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail != int32(FTS5_DETAIL_NONE) { iOff += (*TFts5SegIter)(unsafe.Pointer(pIter)).FnPos } _sqlite3Fts5GetVarint(tls, a+uintptr(iOff), bp) p1 = pIter + 88 *(*Ti64)(unsafe.Pointer(p1)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(p1))) - *(*Tu64)(unsafe.Pointer(bp))) } else { _fts5SegIterReverseNewPage(tls, p, pIter) } } // C documentation // // /* // ** Advance iterator pIter to the next entry. // ** // ** This version of fts5SegIterNext() is only used if detail=none and the // ** iterator is not a reverse direction iterator. // */ func _fts5SegIterNext_None(tls *libc.TLS, p uintptr, pIter uintptr, pbNewTerm uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var iOff int32 var _ /* iDelta at bp+0 */ Ti64 var _ /* nKeep at bp+8 */ int32 var _ /* nList at bp+24 */ int32 var _ /* nTerm at bp+20 */ int32 var _ /* pList at bp+12 */ uintptr var _ /* zTerm at bp+16 */ uintptr _ = iOff _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) iOff = int32((*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset) /* Next entry is on the next page */ for (*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg != 0 && iOff >= (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf { _fts5SegIterNextPage(tls, p, pIter) if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 || (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf == uintptr(0) { return } (*TFts5SegIter)(unsafe.Pointer(pIter)).FiRowid = 0 iOff = int32(4) } if iOff < (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist { iOff += int32(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp+uintptr(iOff), bp)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(iOff) *(*Ti64)(unsafe.Pointer(pIter + 88)) += *(*Ti64)(unsafe.Pointer(bp)) } else { if (*TFts5SegIter)(unsafe.Pointer(pIter)).Fflags&int32(FTS5_SEGITER_ONETERM) == 0 { if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg != 0 { *(*int32)(unsafe.Pointer(bp + 8)) = 0 if iOff != _fts5LeafFirstTermOff(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf) { iOff += _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp+uintptr(iOff), bp+8) } (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(iOff) _fts5SegIterLoadTerm(tls, p, pIter, *(*int32)(unsafe.Pointer(bp + 8))) } else { *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 20)) = 0 _sqlite3Fts5HashScanNext(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash) _sqlite3Fts5HashScanEntry(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, bp+16, bp+20, bp+12, bp+24) if *(*uintptr)(unsafe.Pointer(bp + 12)) == uintptr(0) { goto next_none_eof } (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp = *(*uintptr)(unsafe.Pointer(bp + 12)) (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn = *(*int32)(unsafe.Pointer(bp + 24)) (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf = *(*int32)(unsafe.Pointer(bp + 24)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = *(*int32)(unsafe.Pointer(bp + 24)) _sqlite3Fts5BufferSet(tls, p+44, pIter+72, *(*int32)(unsafe.Pointer(bp + 20)), *(*uintptr)(unsafe.Pointer(bp + 16))) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(_sqlite3Fts5GetVarint(tls, *(*uintptr)(unsafe.Pointer(bp + 12)), pIter+88)) } if pbNewTerm != 0 { *(*int32)(unsafe.Pointer(pbNewTerm)) = int32(1) } } else { goto next_none_eof } } _fts5SegIterLoadNPos(tls, p, pIter) return goto next_none_eof next_none_eof: ; _fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf) (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = uintptr(0) } // C documentation // // /* // ** Advance iterator pIter to the next entry. // ** // ** If an error occurs, Fts5Index.rc is set to an appropriate error code. It // ** is not considered an error if the iterator reaches EOF. If an error has // ** already occurred when this function is called, it is a no-op. // */ func _fts5SegIterNext(tls *libc.TLS, p uintptr, pIter uintptr, pbNewTerm uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var a, pLeaf, v4, p1 uintptr var bNewTerm, n, v2 int32 var v3 Ti64 var _ /* iDelta at bp+8 */ Tu64 var _ /* iOff at bp+0 */ int32 var _ /* nKeep at bp+4 */ int32 var _ /* nList at bp+28 */ int32 var _ /* nSz at bp+32 */ int32 var _ /* nTerm at bp+24 */ int32 var _ /* pList at bp+16 */ uintptr var _ /* zTerm at bp+20 */ uintptr _, _, _, _, _, _, _, _ = a, bNewTerm, n, pLeaf, v2, v3, v4, p1 pLeaf = (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf bNewTerm = 0 *(*int32)(unsafe.Pointer(bp + 4)) = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Search for the end of the position list within the current page. */ a = (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp n = (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp)) = int32((*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset + int64((*TFts5SegIter)(unsafe.Pointer(pIter)).FnPos)) if *(*int32)(unsafe.Pointer(bp)) < n { /* The next entry is on the current page. */ _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp)) >= (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist { bNewTerm = int32(1) if *(*int32)(unsafe.Pointer(bp)) != _fts5LeafFirstTermOff(tls, pLeaf) { *(*int32)(unsafe.Pointer(bp)) += _sqlite3Fts5GetVarint32(tls, a+uintptr(*(*int32)(unsafe.Pointer(bp))), bp+4) } } else { *(*int32)(unsafe.Pointer(bp)) += int32(_sqlite3Fts5GetVarint(tls, a+uintptr(*(*int32)(unsafe.Pointer(bp))), bp+8)) p1 = pIter + 88 *(*Ti64)(unsafe.Pointer(p1)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(p1))) + *(*Tu64)(unsafe.Pointer(bp + 8))) _ = libc.Int32FromInt32(0) } (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(*(*int32)(unsafe.Pointer(bp))) } else { if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg == uintptr(0) { *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 20)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 24)) = 0 *(*int32)(unsafe.Pointer(bp + 28)) = 0 _ = libc.Int32FromInt32(0) if 0 == (*TFts5SegIter)(unsafe.Pointer(pIter)).Fflags&int32(FTS5_SEGITER_ONETERM) { _sqlite3Fts5HashScanNext(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash) _sqlite3Fts5HashScanEntry(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, bp+20, bp+24, bp+16, bp+28) } if *(*uintptr)(unsafe.Pointer(bp + 16)) == uintptr(0) { _fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf) (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = uintptr(0) } else { (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp = *(*uintptr)(unsafe.Pointer(bp + 16)) (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn = *(*int32)(unsafe.Pointer(bp + 28)) (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf = *(*int32)(unsafe.Pointer(bp + 28)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = *(*int32)(unsafe.Pointer(bp + 28)) + int32(1) _sqlite3Fts5BufferSet(tls, p+44, pIter+72, *(*int32)(unsafe.Pointer(bp + 24)), *(*uintptr)(unsafe.Pointer(bp + 20))) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(_sqlite3Fts5GetVarint(tls, *(*uintptr)(unsafe.Pointer(bp + 16)), pIter+88)) *(*int32)(unsafe.Pointer(pbNewTerm)) = int32(1) } } else { *(*int32)(unsafe.Pointer(bp)) = 0 /* Next entry is not on the current page */ for *(*int32)(unsafe.Pointer(bp)) == 0 { _fts5SegIterNextPage(tls, p, pIter) pLeaf = (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf if pLeaf == uintptr(0) { break } _ = libc.Int32FromInt32(0) v2 = int32(_fts5GetU16(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp)) *(*int32)(unsafe.Pointer(bp)) = v2 if v2 != 0 && *(*int32)(unsafe.Pointer(bp)) < (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf { *(*int32)(unsafe.Pointer(bp)) += int32(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr(*(*int32)(unsafe.Pointer(bp))), pIter+88)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(*(*int32)(unsafe.Pointer(bp))) if (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn > (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf { (*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff = (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf + _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr((*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf), pIter+52) } } else { if (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn > (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf { (*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff = (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf + _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr((*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf), bp) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(*(*int32)(unsafe.Pointer(bp))) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = *(*int32)(unsafe.Pointer(bp)) bNewTerm = int32(1) } } _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp)) > (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<> int32(1) _ = libc.Int32FromInt32(0) } } } // C documentation // // /* // ** Iterator pIter currently points to the first rowid in a doclist. This // ** function sets the iterator up so that iterates in reverse order through // ** the doclist. // */ func _fts5SegIterReverse(tls *libc.TLS, p uintptr, pIter uintptr) { var bTermless, iEnd, iOff, iPoslist, iRowid, iSegid, pgno, pgnoLast, v1 int32 var iAbs Ti64 var pDlidx, pLast, pLeaf, pNew, pSeg, tmp uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bTermless, iAbs, iEnd, iOff, iPoslist, iRowid, iSegid, pDlidx, pLast, pLeaf, pNew, pSeg, pgno, pgnoLast, tmp, v1 pDlidx = (*TFts5SegIter)(unsafe.Pointer(pIter)).FpDlidx pLast = uintptr(0) pgnoLast = 0 if pDlidx != 0 && (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FiVersion == int32(FTS5_CURRENT_VERSION) { iSegid = (*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg)).FiSegid pgnoLast = _fts5DlidxIterPgno(tls, pDlidx) pLast = _fts5LeafRead(tls, p, int64(iSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(libc.Int32FromInt32(0))<= (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf { pSeg = (*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg /* The last rowid in the doclist may not be on the current page. Search ** forward to find the page containing the last rowid. */ pgno = (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno + int32(1) for { if !(!((*TFts5Index)(unsafe.Pointer(p)).Frc != 0) && pgno <= (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast) { break } iAbs = int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<= (*TFts5Data)(unsafe.Pointer(pNew)).Fnn) if iRowid != 0 { tmp = pNew pNew = pLast pLast = tmp pgnoLast = pgno } _fts5DataRelease(tls, pNew) if bTermless == 0 { break } } goto _2 _2: ; pgno++ } } } /* If pLast is NULL at this point, then the last rowid for this doclist ** lies on the page currently indicated by the iterator. In this case ** pIter->iLeafOffset is already set to point to the position-list size ** field associated with the first relevant rowid on the page. ** ** Or, if pLast is non-NULL, then it is the page that contains the last ** rowid. In this case configure the iterator so that it points to the ** first rowid on this page. */ if pLast != 0 { _fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf) (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = pLast (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno = pgnoLast iOff = int32(_fts5GetU16(tls, (*TFts5Data)(unsafe.Pointer(pLast)).Fp)) if iOff > (*TFts5Data)(unsafe.Pointer(pLast)).FszLeaf { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<= (*TFts5Data)(unsafe.Pointer(pLast)).Fnn { (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = (*TFts5Data)(unsafe.Pointer(pLast)).Fnn + int32(1) } else { (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = _fts5LeafFirstTermOff(tls, pLast) } } _fts5SegIterReverseInitPage(tls, p, pIter) } // C documentation // // /* // ** Iterator pIter currently points to the first rowid of a doclist. // ** There is a doclist-index associated with the final term on the current // ** page. If the current term is the last term on the page, load the // ** doclist-index from disk and initialize an iterator at (pIter->pDlidx). // */ func _fts5SegIterLoadDlidx(tls *libc.TLS, p uintptr, pIter uintptr) { var bRev, iSeg int32 var pLeaf uintptr _, _, _ = bRev, iSeg, pLeaf iSeg = (*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg)).FiSegid bRev = (*TFts5SegIter)(unsafe.Pointer(pIter)).Fflags & int32(FTS5_SEGITER_REVERSE) pLeaf = (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf /* Current leaf data */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Check if the current doclist ends on this page. If it does, return ** early without loading the doclist-index (as it belongs to a different ** term. */ if (*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafPgno == (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno && (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist < (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf { return } (*TFts5SegIter)(unsafe.Pointer(pIter)).FpDlidx = _fts5DlidxIterInit(tls, p, bRev, iSeg, (*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafPgno) } // C documentation // // /* // ** The iterator object passed as the second argument currently contains // ** no valid values except for the Fts5SegIter.pLeaf member variable. This // ** function searches the leaf page for a term matching (pTerm/nTerm). // ** // ** If the specified term is found on the page, then the iterator is left // ** pointing to it. If argument bGe is zero and the term is not found, // ** the iterator is left pointing at EOF. // ** // ** If bGe is non-zero and the specified term is not found, then the // ** iterator is left pointing to the smallest term in the segment that // ** is larger than the specified term, even if this term is not on the // ** current page. // */ func _fts5LeafSeek(tls *libc.TLS, p uintptr, bGe int32, pIter uintptr, pTerm uintptr, nTerm int32) { bp := tls.Alloc(32) defer tls.Free(32) var a uintptr var bEndOfPage int32 var i, iPgidx, n, nCmp, nMatch, v1, v4 Tu32 var v2 uint32 var _ /* iOff at bp+0 */ Tu32 var _ /* iTermOff at bp+12 */ Tu32 var _ /* nExtra at bp+16 */ int32 var _ /* nKeep at bp+4 */ Tu32 var _ /* nNew at bp+8 */ Tu32 _, _, _, _, _, _, _, _, _, _ = a, bEndOfPage, i, iPgidx, n, nCmp, nMatch, v1, v2, v4 a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp n = uint32((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn) nMatch = uint32(0) *(*Tu32)(unsafe.Pointer(bp + 4)) = uint32(0) *(*Tu32)(unsafe.Pointer(bp + 8)) = uint32(0) /* Current offset in pgidx */ bEndOfPage = 0 _ = libc.Int32FromInt32(0) iPgidx = uint32((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf) iPgidx += uint32(_sqlite3Fts5GetVarint32(tls, a+uintptr(iPgidx), bp+12)) *(*Tu32)(unsafe.Pointer(bp)) = *(*Tu32)(unsafe.Pointer(bp + 12)) if *(*Tu32)(unsafe.Pointer(bp)) > n { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< int32(*(*Tu8)(unsafe.Pointer(pTerm + uintptr(nMatch)))) { goto search_failed } } } if iPgidx >= n { bEndOfPage = int32(1) break } iPgidx += uint32(_sqlite3Fts5GetVarint32(tls, a+uintptr(iPgidx), bp+4)) *(*Tu32)(unsafe.Pointer(bp + 12)) += *(*Tu32)(unsafe.Pointer(bp + 4)) *(*Tu32)(unsafe.Pointer(bp)) = *(*Tu32)(unsafe.Pointer(bp + 12)) if *(*Tu32)(unsafe.Pointer(bp)) >= n { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<= (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn) == 0 { iPgidx = uint32((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf) iPgidx += uint32(_sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp+uintptr(iPgidx), bp)) if *(*Tu32)(unsafe.Pointer(bp)) < uint32(4) || int64(*(*Tu32)(unsafe.Pointer(bp))) >= int64((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf) { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< int64(n) || *(*Tu32)(unsafe.Pointer(bp + 8)) < uint32(1) { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<= n { (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn + int32(1) } else { iPgidx += uint32(_sqlite3Fts5GetVarint32(tls, a+uintptr(iPgidx), bp+16)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = int32(*(*Tu32)(unsafe.Pointer(bp + 12)) + uint32(*(*int32)(unsafe.Pointer(bp + 16)))) } (*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff = int32(iPgidx) _fts5SegIterLoadRowid(tls, p, pIter) _fts5SegIterLoadNPos(tls, p, pIter) } func _fts5IdxSelectStmt(tls *libc.TLS, p uintptr) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var pConfig uintptr _ = pConfig if (*TFts5Index)(unsafe.Pointer(p)).FpIdxSelect == uintptr(0) { pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig _fts5IndexPrepareStmt(tls, p, p+72, Xsqlite3_mprintf(tls, __ccgo_ts+36340, libc.VaList(bp+8, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName))) } return (*TFts5Index)(unsafe.Pointer(p)).FpIdxSelect } // C documentation // // /* // ** Initialize the object pIter to point to term pTerm/nTerm within segment // ** pSeg. If there is no such term in the index, the iterator is set to EOF. // ** // ** If an error occurs, Fts5Index.rc is set to an appropriate error code. If // ** an error has already occurred when this function is called, it is a no-op. // */ func _fts5SegIterSeekInit(tls *libc.TLS, p uintptr, pTerm uintptr, nTerm int32, flags int32, pSeg uintptr, pIter uintptr) { var bDlidx, bGe, iPg int32 var pIdxSelect uintptr var val Ti64 _, _, _, _, _ = bDlidx, bGe, iPg, pIdxSelect, val iPg = int32(1) bGe = flags & int32(FTS5INDEX_QUERY_SCAN) bDlidx = 0 /* True if there is a doclist-index */ pIdxSelect = uintptr(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) libc.Xmemset(tls, pIter, 0, uint32(104)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg = pSeg /* This block sets stack variable iPg to the leaf page number that may ** contain term (pTerm/nTerm), if it is present in the segment. */ pIdxSelect = _fts5IdxSelectStmt(tls, p) if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 { return } Xsqlite3_bind_int(tls, pIdxSelect, int32(1), (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid) Xsqlite3_bind_blob(tls, pIdxSelect, int32(2), pTerm, nTerm, libc.UintptrFromInt32(0)) if int32(SQLITE_ROW) == Xsqlite3_step(tls, pIdxSelect) { val = int64(Xsqlite3_column_int(tls, pIdxSelect, 0)) iPg = int32(val >> libc.Int32FromInt32(1)) bDlidx = int32(val & libc.Int64FromInt32(0x0001)) } (*TFts5Index)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, pIdxSelect) Xsqlite3_bind_null(tls, pIdxSelect, int32(2)) if iPg < (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst { iPg = (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst bDlidx = 0 } (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno = iPg - int32(1) _fts5SegIterNextPage(tls, p, pIter) if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf != 0 { _fts5LeafSeek(tls, p, bGe, pIter, pTerm, nTerm) } if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (bGe == 0 || flags&int32(FTS5INDEX_QUERY_SCANONETERM) != 0) { *(*int32)(unsafe.Pointer(pIter + 4)) |= int32(FTS5_SEGITER_ONETERM) if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf != 0 { if flags&int32(FTS5INDEX_QUERY_DESC) != 0 { *(*int32)(unsafe.Pointer(pIter + 4)) |= int32(FTS5_SEGITER_REVERSE) } if bDlidx != 0 { _fts5SegIterLoadDlidx(tls, p, pIter) } if flags&int32(FTS5INDEX_QUERY_DESC) != 0 { _fts5SegIterReverse(tls, p, pIter) } } } _fts5SegIterSetNext(tls, p, pIter) if 0 == flags&int32(FTS5INDEX_QUERY_SCANONETERM) { _fts5SegIterAllocTombstone(tls, p, pIter) } /* Either: ** ** 1) an error has occurred, or ** 2) the iterator points to EOF, or ** 3) the iterator points to an entry with term (pTerm/nTerm), or ** 4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points ** to an entry with a term greater than or equal to (pTerm/nTerm). */ _ = libc.Int32FromInt32(0) } // C documentation // // /* // ** SQL used by fts5SegIterNextInit() to find the page to open. // */ func _fts5IdxNextStmt(tls *libc.TLS, p uintptr) (r uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var pConfig uintptr _ = pConfig if (*TFts5Index)(unsafe.Pointer(p)).FpIdxNextSelect == uintptr(0) { pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig _fts5IndexPrepareStmt(tls, p, p+76, Xsqlite3_mprintf(tls, __ccgo_ts+36424, libc.VaList(bp+8, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName))) } return (*TFts5Index)(unsafe.Pointer(p)).FpIdxNextSelect } // C documentation // // /* // ** This is similar to fts5SegIterSeekInit(), except that it initializes // ** the segment iterator to point to the first term following the page // ** with pToken/nToken on it. // */ func _fts5SegIterNextInit(tls *libc.TLS, p uintptr, pTerm uintptr, nTerm int32, pSeg uintptr, pIter uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var a, pSel uintptr var bDlidx, iPg int32 var val Ti64 var _ /* iTermOff at bp+0 */ int32 _, _, _, _, _ = a, bDlidx, iPg, pSel, val iPg = -int32(1) /* Page of segment to open */ bDlidx = 0 pSel = uintptr(0) /* SELECT to find iPg */ pSel = _fts5IdxNextStmt(tls, p) if pSel != 0 { _ = libc.Int32FromInt32(0) Xsqlite3_bind_int(tls, pSel, int32(1), (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid) Xsqlite3_bind_blob(tls, pSel, int32(2), pTerm, nTerm, libc.UintptrFromInt32(0)) if Xsqlite3_step(tls, pSel) == int32(SQLITE_ROW) { val = Xsqlite3_column_int64(tls, pSel, 0) iPg = int32(val >> libc.Int32FromInt32(1)) bDlidx = int32(val & libc.Int64FromInt32(0x0001)) } (*TFts5Index)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, pSel) Xsqlite3_bind_null(tls, pSel, int32(2)) if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 { return } } libc.Xmemset(tls, pIter, 0, uint32(104)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg = pSeg *(*int32)(unsafe.Pointer(pIter + 4)) |= int32(FTS5_SEGITER_ONETERM) if iPg >= 0 { (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno = iPg - int32(1) _fts5SegIterNextPage(tls, p, pIter) _fts5SegIterSetNext(tls, p, pIter) } if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf != 0 { a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp *(*int32)(unsafe.Pointer(bp)) = 0 (*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf *(*int32)(unsafe.Pointer(pIter + 48)) += _sqlite3Fts5GetVarint32(tls, a+uintptr((*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff), bp) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(*(*int32)(unsafe.Pointer(bp))) _fts5SegIterLoadTerm(tls, p, pIter, 0) _fts5SegIterLoadNPos(tls, p, pIter) if bDlidx != 0 { _fts5SegIterLoadDlidx(tls, p, pIter) } _ = libc.Int32FromInt32(0) } } // C documentation // // /* // ** Initialize the object pIter to point to term pTerm/nTerm within the // ** in-memory hash table. If there is no such term in the hash-table, the // ** iterator is set to EOF. // ** // ** If an error occurs, Fts5Index.rc is set to an appropriate error code. If // ** an error has already occurred when this function is called, it is a no-op. // */ func _fts5SegIterHashInit(tls *libc.TLS, p uintptr, pTerm uintptr, nTerm int32, flags int32, pIter uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var v1 int32 var _ /* n at bp+8 */ int32 var _ /* nList at bp+0 */ int32 var _ /* pLeaf at bp+12 */ uintptr var _ /* pList at bp+16 */ uintptr var _ /* z at bp+4 */ uintptr _ = v1 *(*int32)(unsafe.Pointer(bp)) = 0 *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 8)) = 0 *(*uintptr)(unsafe.Pointer(bp + 12)) = uintptr(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if pTerm == uintptr(0) || flags&int32(FTS5INDEX_QUERY_SCAN) != 0 { *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) (*TFts5Index)(unsafe.Pointer(p)).Frc = _sqlite3Fts5HashScanInit(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, pTerm, nTerm) _sqlite3Fts5HashScanEntry(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, bp+4, bp+8, bp+16, bp) if *(*uintptr)(unsafe.Pointer(bp + 16)) != 0 { *(*uintptr)(unsafe.Pointer(bp + 12)) = _fts5IdxMalloc(tls, p, int64(12)) if *(*uintptr)(unsafe.Pointer(bp + 12)) != 0 { (*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)))).Fp = *(*uintptr)(unsafe.Pointer(bp + 16)) } } /* The call to sqlite3Fts5HashScanInit() causes the hash table to ** fill the size field of all existing position lists. This means they ** can no longer be appended to. Since the only scenario in which they ** can be appended to is if the previous operation on this table was ** a DELETE, by clearing the Fts5Index.bDelete flag we can avoid this ** possibility altogether. */ (*TFts5Index)(unsafe.Pointer(p)).FbDelete = 0 } else { (*TFts5Index)(unsafe.Pointer(p)).Frc = _sqlite3Fts5HashQuery(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, int32(12), pTerm, nTerm, bp+12, bp) if *(*uintptr)(unsafe.Pointer(bp + 12)) != 0 { (*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)))).Fp = *(*uintptr)(unsafe.Pointer(bp + 12)) + 1*12 } *(*uintptr)(unsafe.Pointer(bp + 4)) = pTerm *(*int32)(unsafe.Pointer(bp + 8)) = nTerm *(*int32)(unsafe.Pointer(pIter + 4)) |= int32(FTS5_SEGITER_ONETERM) } if *(*uintptr)(unsafe.Pointer(bp + 12)) != 0 { _sqlite3Fts5BufferSet(tls, p+44, pIter+72, *(*int32)(unsafe.Pointer(bp + 8)), *(*uintptr)(unsafe.Pointer(bp + 4))) v1 = *(*int32)(unsafe.Pointer(bp)) (*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)))).FszLeaf = v1 (*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)))).Fnn = v1 (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = *(*uintptr)(unsafe.Pointer(bp + 12)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)))).Fp, pIter+88)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = (*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 12)))).Fnn if flags&int32(FTS5INDEX_QUERY_DESC) != 0 { *(*int32)(unsafe.Pointer(pIter + 4)) |= int32(FTS5_SEGITER_REVERSE) _fts5SegIterReverseInitPage(tls, p, pIter) } else { _fts5SegIterLoadNPos(tls, p, pIter) } } _fts5SegIterSetNext(tls, p, pIter) } // C documentation // // /* // ** Array ap[] contains n elements. Release each of these elements using // ** fts5DataRelease(). Then free the array itself using sqlite3_free(). // */ func _fts5IndexFreeArray(tls *libc.TLS, ap uintptr, n int32) { var ii int32 _ = ii if ap != 0 { ii = 0 for { if !(ii < n) { break } _fts5DataRelease(tls, *(*uintptr)(unsafe.Pointer(ap + uintptr(ii)*4))) goto _1 _1: ; ii++ } Xsqlite3_free(tls, ap) } } // C documentation // // /* // ** Decrement the ref-count of the object passed as the only argument. If it // ** reaches 0, free it and its contents. // */ func _fts5TombstoneArrayDelete(tls *libc.TLS, p uintptr) { var ii int32 _ = ii if p != 0 { (*TFts5TombstoneArray)(unsafe.Pointer(p)).FnRef-- if (*TFts5TombstoneArray)(unsafe.Pointer(p)).FnRef <= 0 { ii = 0 for { if !(ii < (*TFts5TombstoneArray)(unsafe.Pointer(p)).FnTombstone) { break } _fts5DataRelease(tls, *(*uintptr)(unsafe.Pointer(p + 8 + uintptr(ii)*4))) goto _1 _1: ; ii++ } Xsqlite3_free(tls, p) } } } // C documentation // // /* // ** Zero the iterator passed as the only argument. // */ func _fts5SegIterClear(tls *libc.TLS, pIter uintptr) { _sqlite3Fts5BufferFree(tls, pIter+72) _fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf) _fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpNextLeaf) _fts5TombstoneArrayDelete(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpTombArray) _fts5DlidxIterFree(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpDlidx) Xsqlite3_free(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FaRowidOffset) libc.Xmemset(tls, pIter, 0, uint32(104)) } // C documentation // // /* // ** Do the comparison necessary to populate pIter->aFirst[iOut]. // ** // ** If the returned value is non-zero, then it is the index of an entry // ** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing // ** to a key that is a duplicate of another, higher priority, // ** segment-iterator in the pSeg->aSeg[] array. // */ func _fts5MultiIterDoCompare(tls *libc.TLS, pIter uintptr, iOut int32) (r int32) { var i1, i2, iRes, res, v1 int32 var p1, p2, pRes uintptr _, _, _, _, _, _, _, _ = i1, i2, iRes, p1, p2, pRes, res, v1 /* Right-hand Fts5SegIter */ pRes = (*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + uintptr(iOut)*4 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if iOut >= (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg/int32(2) { i1 = (iOut - (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg/int32(2)) * int32(2) i2 = i1 + int32(1) } else { i1 = int32((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + uintptr(iOut*int32(2))*4))).FiFirst) i2 = int32((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + uintptr(iOut*int32(2)+int32(1))*4))).FiFirst) } p1 = pIter + 80 + uintptr(i1)*104 p2 = pIter + 80 + uintptr(i2)*104 (*TFts5CResult)(unsafe.Pointer(pRes)).FbTermEq = uint8(0) if (*TFts5SegIter)(unsafe.Pointer(p1)).FpLeaf == uintptr(0) { /* If p1 is at EOF */ iRes = i2 } else { if (*TFts5SegIter)(unsafe.Pointer(p2)).FpLeaf == uintptr(0) { /* If p2 is at EOF */ iRes = i1 } else { res = _fts5BufferCompare(tls, p1+72, p2+72) if res == 0 { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TFts5CResult)(unsafe.Pointer(pRes)).FbTermEq = uint8(1) if (*TFts5SegIter)(unsafe.Pointer(p1)).FiRowid == (*TFts5SegIter)(unsafe.Pointer(p2)).FiRowid { return i2 } if libc.BoolInt32((*TFts5SegIter)(unsafe.Pointer(p1)).FiRowid > (*TFts5SegIter)(unsafe.Pointer(p2)).FiRowid) == (*TFts5Iter)(unsafe.Pointer(pIter)).FbRev { v1 = -int32(1) } else { v1 = +libc.Int32FromInt32(1) } res = v1 } _ = libc.Int32FromInt32(0) if res < 0 { iRes = i1 } else { iRes = i2 } } } (*TFts5CResult)(unsafe.Pointer(pRes)).FiFirst = uint16(iRes) return 0 } // C documentation // // /* // ** Move the seg-iter so that it points to the first rowid on page iLeafPgno. // ** It is an error if leaf iLeafPgno does not exist. Unless the db is // ** a 'secure-delete' db, if it contains no rowids then this is also an error. // */ func _fts5SegIterGotoPage(tls *libc.TLS, p uintptr, pIter uintptr, iLeafPgno int32) { var a uintptr var iOff, n int32 _, _, _ = a, iOff, n _ = libc.Int32FromInt32(0) if iLeafPgno > (*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg)).FpgnoLast { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< 0 { a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp n = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf if iOff < int32(4) || iOff >= n { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< _fts5DlidxIterRowid(tls, pDlidx) { iLeafPgno = _fts5DlidxIterPgno(tls, pDlidx) _fts5DlidxIterNext(tls, p, pDlidx) } _ = libc.Int32FromInt32(0) if iLeafPgno > (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno { _fts5SegIterGotoPage(tls, p, pIter, iLeafPgno) bMove = 0 } } else { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) for !(_fts5DlidxIterEof(tls, p, pDlidx) != 0) && iMatch < _fts5DlidxIterRowid(tls, pDlidx) { _fts5DlidxIterPrev(tls, p, pDlidx) } iLeafPgno = _fts5DlidxIterPgno(tls, pDlidx) _ = libc.Int32FromInt32(0) if iLeafPgno < (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno { (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno = iLeafPgno + int32(1) _fts5SegIterReverseNewPage(tls, p, pIter) bMove = 0 } } for cond := true; cond; cond = (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { if bMove != 0 && (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5SegIter)(unsafe.Pointer(pIter)).FxNext})))(tls, p, pIter, uintptr(0)) } if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf == uintptr(0) { break } if bRev == 0 && (*TFts5SegIter)(unsafe.Pointer(pIter)).FiRowid >= iMatch { break } if bRev != 0 && (*TFts5SegIter)(unsafe.Pointer(pIter)).FiRowid <= iMatch { break } bMove = int32(1) } } // C documentation // // /* // ** Free the iterator object passed as the second argument. // */ func _fts5MultiIterFree(tls *libc.TLS, pIter uintptr) { var i int32 _ = i if pIter != 0 { i = 0 for { if !(i < (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg) { break } _fts5SegIterClear(tls, pIter+80+uintptr(i)*104) goto _1 _1: ; i++ } _sqlite3Fts5BufferFree(tls, pIter+32) Xsqlite3_free(tls, pIter) } } func _fts5MultiIterAdvanced(tls *libc.TLS, p uintptr, pIter uintptr, iChanged int32, iMinset int32) { var i, iEq, v2 int32 var pSeg uintptr _, _, _, _ = i, iEq, pSeg, v2 i = ((*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg + iChanged) / int32(2) for { if !(i >= iMinset && (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK) { break } v2 = _fts5MultiIterDoCompare(tls, pIter, i) iEq = v2 if v2 != 0 { pSeg = pIter + 80 + uintptr(iEq)*104 _ = libc.Int32FromInt32(0) (*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5SegIter)(unsafe.Pointer(pSeg)).FxNext})))(tls, p, pSeg, uintptr(0)) i = (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg + iEq } goto _1 _1: ; i = i / int32(2) } } // C documentation // // /* // ** Sub-iterator iChanged of iterator pIter has just been advanced. It still // ** points to the same term though - just a different rowid. This function // ** attempts to update the contents of the pIter->aFirst[] accordingly. // ** If it does so successfully, 0 is returned. Otherwise 1. // ** // ** If non-zero is returned, the caller should call fts5MultiIterAdvanced() // ** on the iterator instead. That function does the same as this one, except // ** that it deals with more complicated cases as well. // */ func _fts5MultiIterAdvanceRowid(tls *libc.TLS, pIter uintptr, iChanged int32, ppFirst uintptr) (r int32) { var i int32 var pNew, pOther, pRes uintptr var v1 int64 _, _, _, _, _ = i, pNew, pOther, pRes, v1 pNew = pIter + 80 + uintptr(iChanged)*104 if (*TFts5SegIter)(unsafe.Pointer(pNew)).FiRowid == (*TFts5Iter)(unsafe.Pointer(pIter)).FiSwitchRowid || libc.BoolInt32((*TFts5SegIter)(unsafe.Pointer(pNew)).FiRowid < (*TFts5Iter)(unsafe.Pointer(pIter)).FiSwitchRowid) == (*TFts5Iter)(unsafe.Pointer(pIter)).FbRev { pOther = pIter + 80 + uintptr(iChanged^int32(0x0001))*104 if (*TFts5Iter)(unsafe.Pointer(pIter)).FbRev != 0 { v1 = int64(-libc.Int32FromInt32(1)) - (libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)< (*TFts5SegIter)(unsafe.Pointer(pNew)).FiRowid) == (*TFts5Iter)(unsafe.Pointer(pIter)).FbRev { (*TFts5Iter)(unsafe.Pointer(pIter)).FiSwitchRowid = (*TFts5SegIter)(unsafe.Pointer(pOther)).FiRowid pNew = pOther } else { if libc.BoolInt32((*TFts5SegIter)(unsafe.Pointer(pOther)).FiRowid > (*TFts5Iter)(unsafe.Pointer(pIter)).FiSwitchRowid) == (*TFts5Iter)(unsafe.Pointer(pIter)).FbRev { (*TFts5Iter)(unsafe.Pointer(pIter)).FiSwitchRowid = (*TFts5SegIter)(unsafe.Pointer(pOther)).FiRowid } } } } (*TFts5CResult)(unsafe.Pointer(pRes)).FiFirst = uint16((int32(pNew) - t__predefined_ptrdiff_t(pIter+80)) / 104) if i == int32(1) { break } pOther = pIter + 80 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + uintptr(i^int32(0x0001))*4))).FiFirst)*104 goto _2 _2: ; i = i / int32(2) } } *(*uintptr)(unsafe.Pointer(ppFirst)) = pNew return 0 } // C documentation // // /* // ** Set the pIter->bEof variable based on the state of the sub-iterators. // */ func _fts5MultiIterSetEof(tls *libc.TLS, pIter uintptr) { var pSeg uintptr _ = pSeg pSeg = pIter + 80 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)*104 (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FbEof = libc.BoolUint8((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf == uintptr(0)) (*TFts5Iter)(unsafe.Pointer(pIter)).FiSwitchRowid = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiRowid } /* ** The argument to this macro must be an Fts5Data structure containing a ** tombstone hash page. This macro returns the key-size of the hash-page. */ // C documentation // // /* // ** Query a single tombstone hash table for rowid iRowid. Return true if // ** it is found or false otherwise. The tombstone hash table is one of // ** nHashTable tables. // */ func _fts5IndexTombstoneQuery(tls *libc.TLS, pHash uintptr, nHashTable int32, iRowid Tu64) (r int32) { var aSlot, aSlot1 uintptr var iSlot, nCollide, nSlot, szKey, v1, v2, v3, v4, v5 int32 _, _, _, _, _, _, _, _, _, _, _ = aSlot, aSlot1, iSlot, nCollide, nSlot, szKey, v1, v2, v3, v4, v5 if int32(*(*Tu8)(unsafe.Pointer((*TFts5Data)(unsafe.Pointer(pHash)).Fp))) == int32(4) { v1 = int32(4) } else { v1 = int32(8) } szKey = v1 if (*TFts5Data)(unsafe.Pointer(pHash)).Fnn > int32(16) { if int32(*(*Tu8)(unsafe.Pointer((*TFts5Data)(unsafe.Pointer(pHash)).Fp))) == int32(4) { v3 = int32(4) } else { v3 = int32(8) } v2 = ((*TFts5Data)(unsafe.Pointer(pHash)).Fnn - int32(8)) / v3 } else { v2 = int32(1) } nSlot = v2 iSlot = int32(iRowid / uint64(nHashTable) % uint64(nSlot)) nCollide = nSlot if iRowid == uint64(0) { return int32(*(*Tu8)(unsafe.Pointer((*TFts5Data)(unsafe.Pointer(pHash)).Fp + 1))) } else { if szKey == int32(4) { aSlot = (*TFts5Data)(unsafe.Pointer(pHash)).Fp + 8 for *(*Tu32)(unsafe.Pointer(aSlot + uintptr(iSlot)*4)) != 0 { if uint64(_fts5GetU32(tls, aSlot+uintptr(iSlot)*4)) == iRowid { return int32(1) } v4 = nCollide nCollide-- if v4 == 0 { break } iSlot = (iSlot + int32(1)) % nSlot } } else { aSlot1 = (*TFts5Data)(unsafe.Pointer(pHash)).Fp + 8 for *(*Tu64)(unsafe.Pointer(aSlot1 + uintptr(iSlot)*8)) != 0 { if _fts5GetU64(tls, aSlot1+uintptr(iSlot)*8) == iRowid { return int32(1) } v5 = nCollide nCollide-- if v5 == 0 { break } iSlot = (iSlot + int32(1)) % nSlot } } } return 0 } // C documentation // // /* // ** Return true if the iterator passed as the only argument points // ** to an segment entry for which there is a tombstone. Return false // ** if there is no tombstone or if the iterator is already at EOF. // */ func _fts5MultiIterIsDeleted(tls *libc.TLS, pIter uintptr) (r int32) { var iFirst, iPg int32 var pArray, pSeg uintptr _, _, _, _ = iFirst, iPg, pArray, pSeg iFirst = int32((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst) pSeg = pIter + 80 + uintptr(iFirst)*104 pArray = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpTombArray if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf != 0 && pArray != 0 { /* Figure out which page the rowid might be present on. */ iPg = int32(uint64((*TFts5SegIter)(unsafe.Pointer(pSeg)).FiRowid) % uint64((*TFts5TombstoneArray)(unsafe.Pointer(pArray)).FnTombstone)) _ = libc.Int32FromInt32(0) /* If tombstone hash page iPg has not yet been loaded from the ** database, load it now. */ if *(*uintptr)(unsafe.Pointer(pArray + 8 + uintptr(iPg)*4)) == uintptr(0) { *(*uintptr)(unsafe.Pointer(pArray + 8 + uintptr(iPg)*4)) = _fts5DataRead(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex, int64((*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg)).FiSegid+libc.Int32FromInt32(1)<= nSeg */ nSlot = int64(2) for { if !(nSlot < int64(nSeg)) { break } goto _1 _1: ; nSlot = nSlot * int64(2) } pNew = _fts5IdxMalloc(tls, p, int64(184)+int64(104)*(nSlot-int64(1))+int64(4)*nSlot) if pNew != 0 { (*TFts5Iter)(unsafe.Pointer(pNew)).FnSeg = int32(nSlot) (*TFts5Iter)(unsafe.Pointer(pNew)).FaFirst = pNew + 80 + uintptr(nSlot)*104 (*TFts5Iter)(unsafe.Pointer(pNew)).FpIndex = p (*TFts5Iter)(unsafe.Pointer(pNew)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_Noop) } return pNew } func _fts5PoslistCallback(tls *libc.TLS, pUnused uintptr, pContext uintptr, pChunk uintptr, nChunk int32) { _ = pUnused _ = libc.Int32FromInt32(0) if nChunk > 0 { _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(pContext)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pContext)).Fn), pChunk, uint32(nChunk)) *(*int32)(unsafe.Pointer(pContext + 4)) += nChunk } } type TPoslistCallbackCtx = struct { FpBuf uintptr FpColset uintptr FeState int32 } type PoslistCallbackCtx = TPoslistCallbackCtx type TPoslistCallbackCtx1 = struct { FpBuf uintptr FpColset uintptr FeState int32 } type PoslistCallbackCtx1 = TPoslistCallbackCtx1 type TPoslistOffsetsCtx = struct { FpBuf uintptr FpColset uintptr FiRead int32 FiWrite int32 } type PoslistOffsetsCtx = TPoslistOffsetsCtx type TPoslistOffsetsCtx1 = struct { FpBuf uintptr FpColset uintptr FiRead int32 FiWrite int32 } type PoslistOffsetsCtx1 = TPoslistOffsetsCtx1 // C documentation // // /* // ** TODO: Make this more efficient! // */ func _fts5IndexColsetTest(tls *libc.TLS, pColset uintptr, iCol int32) (r int32) { var i int32 _ = i i = 0 for { if !(i < (*TFts5Colset)(unsafe.Pointer(pColset)).FnCol) { break } if *(*int32)(unsafe.Pointer(pColset + 4 + uintptr(i)*4)) == iCol { return int32(1) } goto _1 _1: ; i++ } return 0 } func _fts5PoslistOffsetsCallback(tls *libc.TLS, pUnused uintptr, pContext uintptr, pChunk uintptr, nChunk int32) { bp := tls.Alloc(16) defer tls.Free(16) var i int32 var pCtx uintptr var _ /* iVal at bp+0 */ int32 _, _ = i, pCtx pCtx = pContext _ = pUnused _ = libc.Int32FromInt32(0) if nChunk > 0 { i = 0 for i < nChunk { i += _sqlite3Fts5GetVarint32(tls, pChunk+uintptr(i), bp) *(*int32)(unsafe.Pointer(bp)) += (*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FiRead - int32(2) (*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FiRead = *(*int32)(unsafe.Pointer(bp)) if _fts5IndexColsetTest(tls, (*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FpColset, *(*int32)(unsafe.Pointer(bp))) != 0 { *(*int32)(unsafe.Pointer((*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FpBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer((*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer((*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fn), uint64(*(*int32)(unsafe.Pointer(bp))+libc.Int32FromInt32(2)-(*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FiWrite)) _ = libc.Int32FromInt32(0) (*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FiWrite = *(*int32)(unsafe.Pointer(bp)) } } } } func _fts5PoslistFilterCallback(tls *libc.TLS, pUnused uintptr, pContext uintptr, pChunk uintptr, nChunk int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, iStart, v1, v2 int32 var pCtx uintptr var _ /* iCol at bp+0 */ int32 var _ /* iCol at bp+4 */ int32 _, _, _, _, _ = i, iStart, pCtx, v1, v2 pCtx = pContext _ = pUnused _ = libc.Int32FromInt32(0) if nChunk > 0 { /* Search through to find the first varint with value 1. This is the ** start of the next columns hits. */ i = 0 iStart = 0 if (*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FeState == int32(2) { v1 = i i++ *(*int32)(unsafe.Pointer(bp)) = int32(*(*Tu8)(unsafe.Pointer(pChunk + uintptr(v1)))) if *(*int32)(unsafe.Pointer(bp))&int32(0x80) != 0 { i-- i += _sqlite3Fts5GetVarint32(tls, pChunk+uintptr(i), bp) } if _fts5IndexColsetTest(tls, (*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpColset, *(*int32)(unsafe.Pointer(bp))) != 0 { (*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FeState = int32(1) *(*int32)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fn), uint64(libc.Int32FromInt32(1))) _ = libc.Int32FromInt32(0) } else { (*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FeState = 0 } } for cond := true; cond; cond = i < nChunk { for i < nChunk && int32(*(*Tu8)(unsafe.Pointer(pChunk + uintptr(i)))) != int32(0x01) { for int32(*(*Tu8)(unsafe.Pointer(pChunk + uintptr(i))))&int32(0x80) != 0 { i++ } i++ } if (*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FeState != 0 { _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fn), pChunk+uintptr(iStart), uint32(i-iStart)) *(*int32)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf + 4)) += i - iStart } if i < nChunk { iStart = i i++ if i >= nChunk { (*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FeState = int32(2) } else { v2 = i i++ *(*int32)(unsafe.Pointer(bp + 4)) = int32(*(*Tu8)(unsafe.Pointer(pChunk + uintptr(v2)))) if *(*int32)(unsafe.Pointer(bp + 4))&int32(0x80) != 0 { i-- i += _sqlite3Fts5GetVarint32(tls, pChunk+uintptr(i), bp+4) } (*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FeState = _fts5IndexColsetTest(tls, (*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpColset, *(*int32)(unsafe.Pointer(bp + 4))) if (*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FeState != 0 { _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fn), pChunk+uintptr(iStart), uint32(i-iStart)) *(*int32)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf + 4)) += i - iStart iStart = i } } } } } } func _fts5ChunkIterate(tls *libc.TLS, p uintptr, pSeg uintptr, pCtx uintptr, xChunk uintptr) { var nChunk, nRem, pgno, pgnoSave, v2 int32 var pChunk, pData uintptr var v1 int64 _, _, _, _, _, _, _, _ = nChunk, nRem, pChunk, pData, pgno, pgnoSave, v1, v2 nRem = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos /* Number of bytes still to come */ pData = uintptr(0) pChunk = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).Fp + uintptr((*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset) if int64(nRem) < int64((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).FszLeaf)-(*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset { v1 = int64(nRem) } else { v1 = int64((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).FszLeaf) - (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset } nChunk = int32(v1) pgno = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno pgnoSave = 0 /* This function does not work with detail=none databases. */ _ = libc.Int32FromInt32(0) if (*TFts5SegIter)(unsafe.Pointer(pSeg)).Fflags&int32(FTS5_SEGITER_REVERSE) == 0 { pgnoSave = pgno + int32(1) } for int32(1) != 0 { (*(*func(*libc.TLS, uintptr, uintptr, uintptr, int32))(unsafe.Pointer(&struct{ uintptr }{xChunk})))(tls, p, pCtx, pChunk, nChunk) nRem -= nChunk _fts5DataRelease(tls, pData) if nRem <= 0 { break } else { if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg == uintptr(0) { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<base.pData/nData to point to the new position list. // ** If memory is required for the new position list, use buffer pIter->poslist. // ** Or, if the new position list is a contiguous subset of the input, set // ** pIter->base.pData/nData to point directly to it. // ** // ** This function is a no-op if *pRc is other than SQLITE_OK when it is // ** called. If an OOM error is encountered, *pRc is set to SQLITE_NOMEM // ** before returning. // */ func _fts5IndexExtractColset(tls *libc.TLS, pRc uintptr, pColset uintptr, pPos uintptr, nPos int32, pIter uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var aCopy, p, pEnd, v1, v2, v3 uintptr var i int32 var _ /* iCurrent at bp+0 */ int32 _, _, _, _, _, _, _ = aCopy, i, p, pEnd, v1, v2, v3 if *(*int32)(unsafe.Pointer(pRc)) == SQLITE_OK { p = pPos aCopy = p pEnd = p + uintptr(nPos) /* One byte past end of position list */ i = 0 *(*int32)(unsafe.Pointer(bp)) = 0 if (*TFts5Colset)(unsafe.Pointer(pColset)).FnCol > int32(1) && _sqlite3Fts5BufferSize(tls, pRc, pIter+32, uint32(nPos)) != 0 { return } for int32(1) != 0 { for *(*int32)(unsafe.Pointer(pColset + 4 + uintptr(i)*4)) < *(*int32)(unsafe.Pointer(bp)) { i++ if i == (*TFts5Colset)(unsafe.Pointer(pColset)).FnCol { (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fp (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fn return } } /* Advance pointer p until it points to pEnd or an 0x01 byte that is ** not part of a varint */ for p < pEnd && int32(*(*Tu8)(unsafe.Pointer(p))) != int32(0x01) { for { v1 = p p++ if !(int32(*(*Tu8)(unsafe.Pointer(v1)))&int32(0x80) != 0) { break } } } if *(*int32)(unsafe.Pointer(pColset + 4 + uintptr(i)*4)) == *(*int32)(unsafe.Pointer(bp)) { if (*TFts5Colset)(unsafe.Pointer(pColset)).FnCol == int32(1) { (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = aCopy (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = int32(p) - int32(aCopy) return } _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(pIter+32)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pIter+32)).Fn), aCopy, uint32(int32(p)-int32(aCopy))) *(*int32)(unsafe.Pointer(pIter + 32 + 4)) += int32(p) - int32(aCopy) } if p >= pEnd { (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fp (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fn return } v2 = p p++ aCopy = v2 v3 = p p++ *(*int32)(unsafe.Pointer(bp)) = int32(*(*Tu8)(unsafe.Pointer(v3))) if *(*int32)(unsafe.Pointer(bp))&int32(0x80) != 0 { p-- p += uintptr(_sqlite3Fts5GetVarint32(tls, p, bp)) } } } } // C documentation // // /* // ** xSetOutputs callback used by detail=none tables. // */ func _fts5IterSetOutputs_None(tls *libc.TLS, pIter uintptr, pSeg uintptr) { _ = libc.Int32FromInt32(0) (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FiRowid = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiRowid (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos } // C documentation // // /* // ** xSetOutputs callback used by detail=full and detail=col tables when no // ** column filters are specified. // */ func _fts5IterSetOutputs_Nocolset(tls *libc.TLS, pIter uintptr, pSeg uintptr) { (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FiRowid = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiRowid (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset+int64((*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos) <= int64((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).FszLeaf) { /* All data is stored on the current page. Populate the output ** variables to point into the body of the page object. */ (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).Fp + uintptr((*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset) } else { /* The data is distributed over two or more pages. Copy it into the ** Fts5Iter.poslist buffer and then set the output pointer to point ** to this buffer. */ _sqlite3Fts5BufferZero(tls, pIter+32) _fts5SegiterPoslist(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex, pSeg, uintptr(0), pIter+32) (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fp } } // C documentation // // /* // ** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match // ** against no columns at all). // */ func _fts5IterSetOutputs_ZeroColset(tls *libc.TLS, pIter uintptr, pSeg uintptr) { _ = pSeg (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = 0 } // C documentation // // /* // ** xSetOutputs callback used by detail=col when there is a column filter // ** and there are 100 or more columns. Also called as a fallback from // ** fts5IterSetOutputs_Col100 if the column-list spans more than one page. // */ func _fts5IterSetOutputs_Col(tls *libc.TLS, pIter uintptr, pSeg uintptr) { _sqlite3Fts5BufferZero(tls, pIter+32) _fts5SegiterPoslist(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex, pSeg, (*TFts5Iter)(unsafe.Pointer(pIter)).FpColset, pIter+32) (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FiRowid = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiRowid (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fp (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fn } // C documentation // // /* // ** xSetOutputs callback used when: // ** // ** * detail=col, // ** * there is a column filter, and // ** * the table contains 100 or fewer columns. // ** // ** The last point is to ensure all column numbers are stored as // ** single-byte varints. // */ func _fts5IterSetOutputs_Col100(tls *libc.TLS, pIter uintptr, pSeg uintptr) { var a, aOut, aiCol, aiColEnd, pEnd, v1, v2 uintptr var iPrev, iPrevOut int32 _, _, _, _, _, _, _, _, _ = a, aOut, aiCol, aiColEnd, iPrev, iPrevOut, pEnd, v1, v2 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset+int64((*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos) > int64((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).FszLeaf) { _fts5IterSetOutputs_Col(tls, pIter, pSeg) } else { a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).Fp + uintptr((*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset) pEnd = a + uintptr((*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos) iPrev = 0 aiCol = (*TFts5Iter)(unsafe.Pointer(pIter)).FpColset + 4 aiColEnd = aiCol + uintptr((*TFts5Colset)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FpColset)).FnCol)*4 aOut = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fp iPrevOut = 0 (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FiRowid = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiRowid for a < pEnd { v1 = a a++ iPrev += int32(*(*Tu8)(unsafe.Pointer(v1))) - int32(2) for *(*int32)(unsafe.Pointer(aiCol)) < iPrev { aiCol += 4 if aiCol == aiColEnd { goto setoutputs_col_out } } if *(*int32)(unsafe.Pointer(aiCol)) == iPrev { v2 = aOut aOut++ *(*Tu8)(unsafe.Pointer(v2)) = uint8(iPrev - iPrevOut + libc.Int32FromInt32(2)) iPrevOut = iPrev } } goto setoutputs_col_out setoutputs_col_out: ; (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fp (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = int32(aOut) - int32((*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fp) } } // C documentation // // /* // ** xSetOutputs callback used by detail=full when there is a column filter. // */ func _fts5IterSetOutputs_Full(tls *libc.TLS, pIter uintptr, pSeg uintptr) { var a, pColset, pRc uintptr _, _, _ = a, pColset, pRc pColset = (*TFts5Iter)(unsafe.Pointer(pIter)).FpColset (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FiRowid = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiRowid _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset+int64((*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos) <= int64((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).FszLeaf) { /* All data is stored on the current page. Populate the output ** variables to point into the body of the page object. */ a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).Fp + uintptr((*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset) pRc = (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex + 44 _sqlite3Fts5BufferZero(tls, pIter+32) _fts5IndexExtractColset(tls, pRc, pColset, a, (*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos, pIter) } else { /* The data is distributed over two or more pages. Copy it into the ** Fts5Iter.poslist buffer and then set the output pointer to point ** to this buffer. */ _sqlite3Fts5BufferZero(tls, pIter+32) _fts5SegiterPoslist(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex, pSeg, pColset, pIter+32) (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fp (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fn } } func _fts5IterSetOutputCb(tls *libc.TLS, pRc uintptr, pIter uintptr) { var pConfig uintptr _ = pConfig _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(pRc)) == SQLITE_OK { pConfig = (*TFts5Index)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex)).FpConfig if (*TFts5Config)(unsafe.Pointer(pConfig)).FeDetail == int32(FTS5_DETAIL_NONE) { (*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_None) } else { if (*TFts5Iter)(unsafe.Pointer(pIter)).FpColset == uintptr(0) { (*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_Nocolset) } else { if (*TFts5Colset)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FpColset)).FnCol == 0 { (*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_ZeroColset) } else { if (*TFts5Config)(unsafe.Pointer(pConfig)).FeDetail == FTS5_DETAIL_FULL { (*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_Full) } else { _ = libc.Int32FromInt32(0) if (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol <= int32(100) { (*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_Col100) _sqlite3Fts5BufferSize(tls, pRc, pIter+32, uint32((*TFts5Config)(unsafe.Pointer(pConfig)).FnCol)) } else { (*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_Col) } } } } } } } // C documentation // // /* // ** All the component segment-iterators of pIter have been set up. This // ** functions finishes setup for iterator pIter itself. // */ func _fts5MultiIterFinishSetup(tls *libc.TLS, p uintptr, pIter uintptr) { var iEq, iIter, v2 int32 var pSeg, pSeg1 uintptr _, _, _, _, _ = iEq, iIter, pSeg, pSeg1, v2 iIter = (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg - int32(1) for { if !(iIter > 0) { break } v2 = _fts5MultiIterDoCompare(tls, pIter, iIter) iEq = v2 if v2 != 0 { pSeg = pIter + 80 + uintptr(iEq)*104 if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5SegIter)(unsafe.Pointer(pSeg)).FxNext})))(tls, p, pSeg, uintptr(0)) } _fts5MultiIterAdvanced(tls, p, pIter, iEq, iIter) } goto _1 _1: ; iIter-- } _fts5MultiIterSetEof(tls, pIter) if (*TFts5Iter)(unsafe.Pointer(pIter)).FbSkipEmpty != 0 && _fts5MultiIterIsEmpty(tls, p, pIter) != 0 || _fts5MultiIterIsDeleted(tls, pIter) != 0 { _fts5MultiIterNext(tls, p, pIter, 0, 0) } else { if int32((*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FbEof) == 0 { pSeg1 = pIter + 80 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)*104 (*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs})))(tls, pIter, pSeg1) } } } // C documentation // // /* // ** Allocate a new Fts5Iter object. // ** // ** The new object will be used to iterate through data in structure pStruct. // ** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel // ** is zero or greater, data from the first nSegment segments on level iLevel // ** is merged. // ** // ** The iterator initially points to the first term/rowid entry in the // ** iterated data. // */ func _fts5MultiIterNew(tls *libc.TLS, p uintptr, pStruct uintptr, flags int32, pColset uintptr, pTerm uintptr, nTerm int32, iLevel int32, nSegment int32, ppOut uintptr) { var iIter, iSeg, nSeg, v1, v3, v6, v8 int32 var pEnd, pIter, pIter1, pLvl, pNew, pSeg, v2 uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _ = iIter, iSeg, nSeg, pEnd, pIter, pIter1, pLvl, pNew, pSeg, v1, v2, v3, v6, v8 nSeg = 0 /* Number of segment-iters in use */ iIter = 0 _ = libc.Int32FromInt32(0) /* Allocate space for the new multi-seg-iterator. */ if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { if iLevel < 0 { _ = libc.Int32FromInt32(0) nSeg = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment nSeg += libc.BoolInt32((*TFts5Index)(unsafe.Pointer(p)).FpHash != 0 && 0 == flags&int32(FTS5INDEX_QUERY_SKIPHASH)) } else { if (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLevel)*12))).FnSeg < nSegment { v1 = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLevel)*12))).FnSeg } else { v1 = nSegment } nSeg = v1 } } v2 = _fts5MultiIterAlloc(tls, p, nSeg) pNew = v2 *(*uintptr)(unsafe.Pointer(ppOut)) = v2 if pNew == uintptr(0) { _ = libc.Int32FromInt32(0) goto fts5MultiIterNew_post_check } (*TFts5Iter)(unsafe.Pointer(pNew)).FbRev = libc.BoolInt32(0 != flags&int32(FTS5INDEX_QUERY_DESC)) (*TFts5Iter)(unsafe.Pointer(pNew)).FbSkipEmpty = libc.BoolUint8(libc.Int32FromInt32(0) != flags&libc.Int32FromInt32(FTS5INDEX_QUERY_SKIPEMPTY)) (*TFts5Iter)(unsafe.Pointer(pNew)).FpColset = pColset if flags&int32(FTS5INDEX_QUERY_NOOUTPUT) == 0 { _fts5IterSetOutputCb(tls, p+44, pNew) } /* Initialize each of the component segment iterators. */ if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { if iLevel < 0 { pEnd = pStruct + 32 + uintptr((*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel)*12 if (*TFts5Index)(unsafe.Pointer(p)).FpHash != 0 && 0 == flags&int32(FTS5INDEX_QUERY_SKIPHASH) { v3 = iIter iIter++ /* Add a segment iterator for the current contents of the hash table. */ pIter = pNew + 80 + uintptr(v3)*104 _fts5SegIterHashInit(tls, p, pTerm, nTerm, flags, pIter) } pLvl = pStruct + 32 for { if !(pLvl < pEnd) { break } iSeg = (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg - int32(1) for { if !(iSeg >= 0) { break } pSeg = (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr(iSeg)*56 v6 = iIter iIter++ pIter1 = pNew + 80 + uintptr(v6)*104 if pTerm == uintptr(0) { _fts5SegIterInit(tls, p, pSeg, pIter1) } else { _fts5SegIterSeekInit(tls, p, pTerm, nTerm, flags, pSeg, pIter1) } goto _5 _5: ; iSeg-- } goto _4 _4: ; pLvl += 12 } } else { pLvl = pStruct + 32 + uintptr(iLevel)*12 iSeg = nSeg - int32(1) for { if !(iSeg >= 0) { break } v8 = iIter iIter++ _fts5SegIterInit(tls, p, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg+uintptr(iSeg)*56, pNew+80+uintptr(v8)*104) goto _7 _7: ; iSeg-- } } _ = libc.Int32FromInt32(0) } /* If the above was successful, each component iterator now points ** to the first entry in its segment. In this case initialize the ** aFirst[] array. Or, if an error has occurred, free the iterator ** object and set the output variable to NULL. */ if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { _fts5MultiIterFinishSetup(tls, p, pNew) } else { _fts5MultiIterFree(tls, pNew) *(*uintptr)(unsafe.Pointer(ppOut)) = uintptr(0) } goto fts5MultiIterNew_post_check fts5MultiIterNew_post_check: ; _ = libc.Int32FromInt32(0) return } // C documentation // // /* // ** Create an Fts5Iter that iterates through the doclist provided // ** as the second argument. // */ func _fts5MultiIterNew2(tls *libc.TLS, p uintptr, pData uintptr, bDesc int32, ppOut uintptr) { var pIter, pNew uintptr _, _ = pIter, pNew pNew = _fts5MultiIterAlloc(tls, p, int32(2)) if pNew != 0 { pIter = pNew + 80 + 1*104 (*TFts5SegIter)(unsafe.Pointer(pIter)).Fflags = int32(FTS5_SEGITER_ONETERM) if (*TFts5Data)(unsafe.Pointer(pData)).FszLeaf > 0 { (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = pData (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp, pIter+88)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = (*TFts5Data)(unsafe.Pointer(pData)).Fnn (*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pNew)).FaFirst + 1*4))).FiFirst = uint16(1) if bDesc != 0 { (*TFts5Iter)(unsafe.Pointer(pNew)).FbRev = int32(1) *(*int32)(unsafe.Pointer(pIter + 4)) |= int32(FTS5_SEGITER_REVERSE) _fts5SegIterReverseInitPage(tls, p, pIter) } else { _fts5SegIterLoadNPos(tls, p, pIter) } pData = uintptr(0) } else { (*TFts5Iter)(unsafe.Pointer(pNew)).Fbase.FbEof = uint8(1) } _fts5SegIterSetNext(tls, p, pIter) *(*uintptr)(unsafe.Pointer(ppOut)) = pNew } _fts5DataRelease(tls, pData) } // C documentation // // /* // ** Return true if the iterator is at EOF or if an error has occurred. // ** False otherwise. // */ func _fts5MultiIterEof(tls *libc.TLS, p uintptr, pIter uintptr) (r int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) return libc.BoolInt32((*TFts5Index)(unsafe.Pointer(p)).Frc != 0 || (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FbEof != 0) } // C documentation // // /* // ** Return the rowid of the entry that the iterator currently points // ** to. If the iterator points to EOF when this function is called the // ** results are undefined. // */ func _fts5MultiIterRowid(tls *libc.TLS, pIter uintptr) (r Ti64) { _ = libc.Int32FromInt32(0) return (*(*TFts5SegIter)(unsafe.Pointer(pIter + 80 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)*104))).FiRowid } // C documentation // // /* // ** Move the iterator to the next entry at or following iMatch. // */ func _fts5MultiIterNextFrom(tls *libc.TLS, p uintptr, pIter uintptr, iMatch Ti64) { var iRowid Ti64 _ = iRowid for int32(1) != 0 { _fts5MultiIterNext(tls, p, pIter, int32(1), iMatch) if _fts5MultiIterEof(tls, p, pIter) != 0 { break } iRowid = _fts5MultiIterRowid(tls, pIter) if (*TFts5Iter)(unsafe.Pointer(pIter)).FbRev == 0 && iRowid >= iMatch { break } if (*TFts5Iter)(unsafe.Pointer(pIter)).FbRev != 0 && iRowid <= iMatch { break } } } // C documentation // // /* // ** Return a pointer to a buffer containing the term associated with the // ** entry that the iterator currently points to. // */ func _fts5MultiIterTerm(tls *libc.TLS, pIter uintptr, pn uintptr) (r uintptr) { var p uintptr _ = p p = pIter + 80 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)*104 *(*int32)(unsafe.Pointer(pn)) = (*TFts5SegIter)(unsafe.Pointer(p)).Fterm.Fn return (*TFts5SegIter)(unsafe.Pointer(p)).Fterm.Fp } // C documentation // // /* // ** Allocate a new segment-id for the structure pStruct. The new segment // ** id must be between 1 and 65335 inclusive, and must not be used by // ** any currently existing segment. If a free segment id cannot be found, // ** SQLITE_FULL is returned. // ** // ** If an error has already occurred, this function is a no-op. 0 is // ** returned in this case. // */ func _fts5AllocateSegid(tls *libc.TLS, p uintptr, pStruct uintptr) (r int32) { bp := tls.Alloc(256) defer tls.Free(256) var i, iId, iLvl, iSeg, iSegid int32 var mask Tu32 var _ /* aUsed at bp+0 */ [63]Tu32 _, _, _, _, _, _ = i, iId, iLvl, iSeg, iSegid, mask iSegid = 0 if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { if (*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment >= int32(FTS5_MAX_SEGMENT) { (*TFts5Index)(unsafe.Pointer(p)).Frc = int32(SQLITE_FULL) } else { libc.Xmemset(tls, bp, 0, uint32(252)) iLvl = 0 for { if !(iLvl < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) { break } iSeg = 0 for { if !(iSeg < (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FnSeg) { break } iId = (*(*TFts5StructureSegment)(unsafe.Pointer((*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FaSeg + uintptr(iSeg)*56))).FiSegid if iId <= int32(FTS5_MAX_SEGMENT) && iId > 0 { *(*Tu32)(unsafe.Pointer(bp + uintptr((iId-int32(1))/int32(32))*4)) |= libc.Uint32FromInt32(1) << ((iId - int32(1)) % int32(32)) } goto _2 _2: ; iSeg++ } goto _1 _1: ; iLvl++ } i = 0 for { if !((*(*[63]Tu32)(unsafe.Pointer(bp)))[i] == uint32(0xFFFFFFFF)) { break } goto _3 _3: ; i++ } mask = (*(*[63]Tu32)(unsafe.Pointer(bp)))[i] iSegid = 0 for { if !(mask&(libc.Uint32FromInt32(1)<) shares with buffer (pOld/nOld). // ** // ** Buffer (pNew/) is guaranteed to be greater // ** than buffer (pOld/nOld). // */ func _fts5PrefixCompress(tls *libc.TLS, nOld int32, pOld uintptr, pNew uintptr) (r int32) { var i int32 _ = i i = 0 for { if !(i < nOld) { break } if int32(*(*Tu8)(unsafe.Pointer(pOld + uintptr(i)))) != int32(*(*Tu8)(unsafe.Pointer(pNew + uintptr(i)))) { break } goto _1 _1: ; i++ } return i } func _fts5WriteDlidxClear(tls *libc.TLS, p uintptr, pWriter uintptr, bFlush int32) { var i int32 var pDlidx uintptr _, _ = i, pDlidx _ = libc.Int32FromInt32(0) i = 0 for { if !(i < (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnDlidx) { break } pDlidx = (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx + uintptr(i)*32 if (*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fbuf.Fn == 0 { break } if bFlush != 0 { _ = libc.Int32FromInt32(0) _fts5DataWrite(tls, p, int64((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(1))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(i)<aDlidx[] array to at least nLvl elements in size. // ** Any new array elements are zeroed before returning. // */ func _fts5WriteDlidxGrow(tls *libc.TLS, p uintptr, pWriter uintptr, nLvl int32) (r int32) { var aDlidx uintptr var nByte Tsize_t _, _ = aDlidx, nByte if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && nLvl >= (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnDlidx { aDlidx = Xsqlite3_realloc64(tls, (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx, uint64(uint32(32)*uint32(nLvl))) if aDlidx == uintptr(0) { (*TFts5Index)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) } else { nByte = uint32(32) * uint32(nLvl-(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnDlidx) libc.Xmemset(tls, aDlidx+uintptr((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnDlidx)*32, 0, nByte) (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx = aDlidx (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnDlidx = nLvl } } return (*TFts5Index)(unsafe.Pointer(p)).Frc } // C documentation // // /* // ** If the current doclist-index accumulating in pWriter->aDlidx[] is large // ** enough, flush it to disk and return 1. Otherwise discard it and return // ** zero. // */ func _fts5WriteFlushDlidx(tls *libc.TLS, p uintptr, pWriter uintptr) (r int32) { var bFlag int32 _ = bFlag bFlag = 0 /* If there were FTS5_MIN_DLIDX_SIZE or more empty leaf pages written ** to the database, also write the doclist-index to disk. */ if (*(*TFts5DlidxWriter)(unsafe.Pointer((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx))).Fbuf.Fn > 0 && (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnEmpty >= int32(FTS5_MIN_DLIDX_SIZE) { bFlag = int32(1) } _fts5WriteDlidxClear(tls, p, pWriter, bFlag) (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnEmpty = 0 return bFlag } // C documentation // // /* // ** This function is called whenever processing of the doclist for the // ** last term on leaf page (pWriter->iBtPage) is completed. // ** // ** The doclist-index for that term is currently stored in-memory within the // ** Fts5SegWriter.aDlidx[] array. If it is large enough, this function // ** writes it out to disk. Or, if it is too small to bother with, discards // ** it. // ** // ** Fts5SegWriter.btterm currently contains the first term on page iBtPage. // */ func _fts5WriteFlushBtree(tls *libc.TLS, p uintptr, pWriter uintptr) { var bFlag int32 var z, v1 uintptr _, _, _ = bFlag, z, v1 _ = libc.Int32FromInt32(0) if (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiBtPage == 0 { return } bFlag = _fts5WriteFlushDlidx(tls, p, pWriter) if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { if (*TFts5SegWriter)(unsafe.Pointer(pWriter)).Fbtterm.Fn > 0 { v1 = (*TFts5SegWriter)(unsafe.Pointer(pWriter)).Fbtterm.Fp } else { v1 = __ccgo_ts + 1648 } z = v1 /* The following was already done in fts5WriteInit(): */ /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */ Xsqlite3_bind_blob(tls, (*TFts5Index)(unsafe.Pointer(p)).FpIdxWriter, int32(2), z, (*TFts5SegWriter)(unsafe.Pointer(pWriter)).Fbtterm.Fn, libc.UintptrFromInt32(0)) Xsqlite3_bind_int64(tls, (*TFts5Index)(unsafe.Pointer(p)).FpIdxWriter, int32(3), int64(bFlag)+int64((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiBtPage)< 0 { pDlidx = (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx _ = libc.Int32FromInt32(0) _sqlite3Fts5BufferAppendVarint(tls, p+44, pDlidx+16, 0) } /* Increment the "number of sequential leaves without a term" counter. */ (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnEmpty++ } func _fts5DlidxExtractFirstRowid(tls *libc.TLS, pBuf uintptr) (r Ti64) { bp := tls.Alloc(16) defer tls.Free(16) var iOff int32 var _ /* iRowid at bp+0 */ Ti64 _ = iOff iOff = int32(1) + int32(_sqlite3Fts5GetVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+1, bp)) _sqlite3Fts5GetVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr(iOff), bp) return *(*Ti64)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Rowid iRowid has just been appended to the current leaf page. It is the // ** first on the page. This function appends an appropriate entry to the current // ** doclist-index. // */ func _fts5WriteDlidxAppend(tls *libc.TLS, p uintptr, pWriter uintptr, iRowid Ti64) { var bDone, i, v2 int32 var iFirst, iPgno, iVal Ti64 var pDlidx uintptr _, _, _, _, _, _, _ = bDone, i, iFirst, iPgno, iVal, pDlidx, v2 bDone = 0 i = 0 for { if !((*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && bDone == 0) { break } pDlidx = (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx + uintptr(i)*32 if (*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fbuf.Fn >= (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz { /* The current doclist-index page is full. Write it to disk and push ** a copy of iRowid (which will become the first rowid on the next ** doclist-index leaf page) up into the next level of the b-tree ** hierarchy. If the node being flushed is currently the root node, ** also push its first rowid upwards. */ *(*Tu8)(unsafe.Pointer((*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fbuf.Fp)) = uint8(0x01) /* Not the root node */ _fts5DataWrite(tls, p, int64((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(1))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(i)<= (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz { if (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn > int32(4) { _fts5WriteFlushLeaf(tls, p, pWriter) if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK { return } } if !(uint32((*TFts5Buffer)(unsafe.Pointer(pPage+8)).Fn)+uint32(nTerm+libc.Int32FromInt32(FTS5_DATA_PADDING)) <= uint32((*TFts5Buffer)(unsafe.Pointer(pPage+8)).FnSpace)) { _sqlite3Fts5BufferSize(tls, p+44, pPage+8, uint32(nTerm+int32(FTS5_DATA_PADDING)+(*TFts5Buffer)(unsafe.Pointer(pPage+8)).Fn)) } } /* TODO1: Updating pgidx here. */ *(*int32)(unsafe.Pointer(pPgidx + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fn), uint64((*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn-(*TFts5PageWriter)(unsafe.Pointer(pPage)).FiPrevPgidx)) (*TFts5PageWriter)(unsafe.Pointer(pPage)).FiPrevPgidx = (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn if (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstTermInPage != 0 { nPrefix = 0 if (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgno != int32(1) { /* This is the first term on a leaf that is not the leftmost leaf in ** the segment b-tree. In this case it is necessary to add a term to ** the b-tree hierarchy that is (a) larger than the largest term ** already written to the segment and (b) smaller than or equal to ** this term. In other words, a prefix of (pTerm/nTerm) that is one ** byte longer than the longest prefix (pTerm/nTerm) shares with the ** previous term. ** ** Usually, the previous term is available in pPage->term. The exception ** is if this is the first term written in an incremental-merge step. ** In this case the previous term is not available, so just write a ** copy of (pTerm/nTerm) into the parent node. This is slightly ** inefficient, but still correct. */ n = nTerm if (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fterm.Fn != 0 { n = int32(1) + _fts5PrefixCompress(tls, nMin, (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fterm.Fp, pTerm) } _fts5WriteBtreeTerm(tls, p, pWriter, n, pTerm) if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK { return } pPage = pWriter + 4 } } else { nPrefix = _fts5PrefixCompress(tls, nMin, (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fterm.Fp, pTerm) _sqlite3Fts5BufferAppendVarint(tls, p+44, pPage+8, int64(nPrefix)) } /* Append the number of bytes of new data, then the term data itself ** to the page. */ _sqlite3Fts5BufferAppendVarint(tls, p+44, pPage+8, int64(nTerm)-int64(nPrefix)) _sqlite3Fts5BufferAppendBlob(tls, p+44, pPage+8, uint32(nTerm-nPrefix), pTerm+uintptr(nPrefix)) /* Update the Fts5PageWriter.term field. */ _sqlite3Fts5BufferSet(tls, p+44, pPage+32, nTerm, pTerm) (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstTermInPage = uint8(0) (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInPage = uint8(0) (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInDoclist = uint8(1) _ = libc.Int32FromInt32(0) (*(*TFts5DlidxWriter)(unsafe.Pointer((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx))).Fpgno = (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgno } // C documentation // // /* // ** Append a rowid and position-list size field to the writers output. // */ func _fts5WriteAppendRowid(tls *libc.TLS, p uintptr, pWriter uintptr, iRowid Ti64) { var pPage uintptr _ = pPage if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { pPage = pWriter + 4 if (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn+(*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgidx.Fn >= (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz { _fts5WriteFlushLeaf(tls, p, pWriter) } /* If this is to be the first rowid written to the page, set the ** rowid-pointer in the page-header. Also append a value to the dlidx ** buffer, in case a doclist-index is required. */ if (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInPage != 0 { _fts5PutU16(tls, (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fp, uint16((*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn)) _fts5WriteDlidxAppend(tls, p, pWriter, iRowid) } /* Write the rowid. */ if (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInDoclist != 0 || (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInPage != 0 { _sqlite3Fts5BufferAppendVarint(tls, p+44, pPage+8, iRowid) } else { _ = libc.Int32FromInt32(0) _sqlite3Fts5BufferAppendVarint(tls, p+44, pPage+8, int64(uint64(int64(uint64(iRowid)))-uint64((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiPrevRowid))) } (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiPrevRowid = iRowid (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInDoclist = uint8(0) (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInPage = uint8(0) } } func _fts5WriteAppendPoslistData(tls *libc.TLS, p uintptr, pWriter uintptr, aData uintptr, nData int32) { bp := tls.Alloc(16) defer tls.Free(16) var a, pPage uintptr var n, nCopy, nReq int32 var _ /* dummy at bp+0 */ Ti64 _, _, _, _, _ = a, n, nCopy, nReq, pPage pPage = pWriter + 4 a = aData n = nData _ = libc.Int32FromInt32(0) for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn+(*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgidx.Fn+n >= (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz { nReq = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz - (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn - (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgidx.Fn nCopy = 0 for nCopy < nReq { nCopy += int32(_sqlite3Fts5GetVarint(tls, a+uintptr(nCopy), bp)) } _sqlite3Fts5BufferAppendBlob(tls, p+44, pPage+8, uint32(nCopy), a) a += uintptr(nCopy) n -= nCopy _fts5WriteFlushLeaf(tls, p, pWriter) } if n > 0 { _sqlite3Fts5BufferAppendBlob(tls, p+44, pPage+8, uint32(n), a) } } // C documentation // // /* // ** Flush any data cached by the writer object to the database. Free any // ** allocations associated with the writer. // */ func _fts5WriteFinish(tls *libc.TLS, p uintptr, pWriter uintptr, pnLeaf uintptr) { var i int32 var pLeaf uintptr _, _ = i, pLeaf pLeaf = pWriter + 4 if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { _ = libc.Int32FromInt32(0) if (*TFts5PageWriter)(unsafe.Pointer(pLeaf)).Fbuf.Fn > int32(4) { _fts5WriteFlushLeaf(tls, p, pWriter) } *(*int32)(unsafe.Pointer(pnLeaf)) = (*TFts5PageWriter)(unsafe.Pointer(pLeaf)).Fpgno - int32(1) if (*TFts5PageWriter)(unsafe.Pointer(pLeaf)).Fpgno > int32(1) { _fts5WriteFlushBtree(tls, p, pWriter) } } _sqlite3Fts5BufferFree(tls, pLeaf+32) _sqlite3Fts5BufferFree(tls, pLeaf+8) _sqlite3Fts5BufferFree(tls, pLeaf+20) _sqlite3Fts5BufferFree(tls, pWriter+76) i = 0 for { if !(i < (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnDlidx) { break } _sqlite3Fts5BufferFree(tls, (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx+uintptr(i)*32+16) goto _1 _1: ; i++ } Xsqlite3_free(tls, (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx) } func _fts5WriteInit(tls *libc.TLS, p uintptr, pWriter uintptr, iSegid int32) { bp := tls.Alloc(32) defer tls.Free(32) var nBuffer int32 var pConfig uintptr _, _ = nBuffer, pConfig nBuffer = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz + int32(FTS5_DATA_PADDING) libc.Xmemset(tls, pWriter, 0, uint32(96)) (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiSegid = iSegid _fts5WriteDlidxGrow(tls, p, pWriter, int32(1)) (*TFts5SegWriter)(unsafe.Pointer(pWriter)).Fwriter.Fpgno = int32(1) (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstTermInPage = uint8(1) (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiBtPage = int32(1) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Grow the two buffers to pgsz + padding bytes in size. */ _sqlite3Fts5BufferSize(tls, p+44, pWriter+4+20, uint32(nBuffer)) _sqlite3Fts5BufferSize(tls, p+44, pWriter+4+8, uint32(nBuffer)) if (*TFts5Index)(unsafe.Pointer(p)).FpIdxWriter == uintptr(0) { pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig _fts5IndexPrepareStmt(tls, p, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+36506, libc.VaList(bp+8, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName))) } if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { /* Initialize the 4-byte leaf-page header to 0x00. */ libc.Xmemset(tls, (*TFts5SegWriter)(unsafe.Pointer(pWriter)).Fwriter.Fbuf.Fp, 0, uint32(4)) (*TFts5SegWriter)(unsafe.Pointer(pWriter)).Fwriter.Fbuf.Fn = int32(4) /* Bind the current output segment id to the index-writer. This is an ** optimization over binding the same value over and over as rows are ** inserted into %_idx by the current writer. */ Xsqlite3_bind_int(tls, (*TFts5Index)(unsafe.Pointer(p)).FpIdxWriter, int32(1), (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiSegid) } } // C documentation // // /* // ** Iterator pIter was used to iterate through the input segments of on an // ** incremental merge operation. This function is called if the incremental // ** merge step has finished but the input has not been completely exhausted. // */ func _fts5TrimSegments(tls *libc.TLS, p uintptr, pIter uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var i, iId, iOff, nDiff int32 var iLeafRowid Ti64 var pData, pSeg uintptr var _ /* aHdr at bp+12 */ [4]Tu8 var _ /* buf at bp+0 */ TFts5Buffer _, _, _, _, _, _, _ = i, iId, iLeafRowid, iOff, nDiff, pData, pSeg libc.Xmemset(tls, bp, 0, uint32(12)) i = 0 for { if !(i < (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg && (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK) { break } pSeg = pIter + 80 + uintptr(i)*104 if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg == uintptr(0) { /* no-op */ } else { if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf == uintptr(0) { /* All keys from this input segment have been transfered to the output. ** Set both the first and last page-numbers to 0 to indicate that the ** segment is now empty. */ (*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg)).FpgnoLast = 0 (*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg)).FpgnoFirst = 0 } else { iOff = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafOffset iId = (*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg)).FiSegid *(*[4]Tu8)(unsafe.Pointer(bp + 12)) = [4]Tu8{} iLeafRowid = int64(iId)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))< (*TFts5Data)(unsafe.Pointer(pData)).FszLeaf { /* This can occur if the pages that the segments occupy overlap - if ** a single page has been assigned to more than one segment. In ** this case a prior iteration of this loop may have corrupted the ** segment currently being trimmed. */ (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< uint64(0) { (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin1 = (*(*TFts5StructureSegment)(unsafe.Pointer((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg))).FiOrigin1 (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin2 = (*(*TFts5StructureSegment)(unsafe.Pointer((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg-int32(1))*56))).FiOrigin2 } } bOldest = libc.BoolInt32((*TFts5StructureLevel)(unsafe.Pointer(pLvlOut)).FnSeg == int32(1) && (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel == iLvl+int32(2)) _ = libc.Int32FromInt32(0) _fts5MultiIterNew(tls, p, pStruct, flags, uintptr(0), uintptr(0), 0, iLvl, nInput, bp) for { if !(_fts5MultiIterEof(tls, p, *(*uintptr)(unsafe.Pointer(bp))) == 0) { break } pSegIter = *(*uintptr)(unsafe.Pointer(bp)) + 80 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaFirst + 1*4))).FiFirst)*104 pTerm = _fts5MultiIterTerm(tls, *(*uintptr)(unsafe.Pointer(bp)), bp+116) if v4 = *(*int32)(unsafe.Pointer(bp + 116)) != (*(*TFts5Buffer)(unsafe.Pointer(bp + 104))).Fn; !v4 { if *(*int32)(unsafe.Pointer(bp + 116)) <= 0 { v3 = 0 } else { v3 = libc.Xmemcmp(tls, pTerm, (*(*TFts5Buffer)(unsafe.Pointer(bp + 104))).Fp, uint32(*(*int32)(unsafe.Pointer(bp + 116)))) } } if v4 || v3 != 0 { if pnRem != 0 && (*(*TFts5SegWriter)(unsafe.Pointer(bp + 8))).FnLeafWritten > nRem { break } _sqlite3Fts5BufferSet(tls, p+44, bp+104, *(*int32)(unsafe.Pointer(bp + 116)), pTerm) bTermWritten = 0 } /* Check for key annihilation. */ if (*TFts5SegIter)(unsafe.Pointer(pSegIter)).FnPos == 0 && (bOldest != 0 || int32((*TFts5SegIter)(unsafe.Pointer(pSegIter)).FbDel) == 0) { goto _2 } if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && bTermWritten == 0 { /* This is a new term. Append a term to the output segment. */ _fts5WriteAppendTerm(tls, p, bp+8, *(*int32)(unsafe.Pointer(bp + 116)), pTerm) bTermWritten = int32(1) } /* Append the rowid to the output */ /* WRITEPOSLISTSIZE */ _fts5WriteAppendRowid(tls, p, bp+8, _fts5MultiIterRowid(tls, *(*uintptr)(unsafe.Pointer(bp)))) if eDetail == int32(FTS5_DETAIL_NONE) { if (*TFts5SegIter)(unsafe.Pointer(pSegIter)).FbDel != 0 { _sqlite3Fts5BufferAppendVarint(tls, p+44, bp+8+4+8, libc.Int64FromInt32(0)) if (*TFts5SegIter)(unsafe.Pointer(pSegIter)).FnPos > 0 { _sqlite3Fts5BufferAppendVarint(tls, p+44, bp+8+4+8, libc.Int64FromInt32(0)) } } } else { /* Append the position-list data to the output */ nPos = (*TFts5SegIter)(unsafe.Pointer(pSegIter)).FnPos*int32(2) + int32((*TFts5SegIter)(unsafe.Pointer(pSegIter)).FbDel) _sqlite3Fts5BufferAppendVarint(tls, p+44, bp+8+4+8, int64(nPos)) _fts5ChunkIterate(tls, p, pSegIter, bp+8, __ccgo_fp(_fts5MergeChunkCallback)) } goto _2 _2: ; _fts5MultiIterNext(tls, p, *(*uintptr)(unsafe.Pointer(bp)), 0, 0) } /* Flush the last leaf page to disk. Set the output segment b-tree height ** and last leaf page number at the same time. */ _fts5WriteFinish(tls, p, bp+8, pSeg+8) _ = libc.Int32FromInt32(0) if _fts5MultiIterEof(tls, p, *(*uintptr)(unsafe.Pointer(bp))) != 0 { /* Remove the redundant segments from the %_data table */ _ = libc.Int32FromInt32(0) i = 0 for { if !(i < nInput) { break } pOld = (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr(i)*56 *(*Tu64)(unsafe.Pointer(pSeg + 48)) += (*TFts5StructureSegment)(unsafe.Pointer(pOld)).FnEntry - (*TFts5StructureSegment)(unsafe.Pointer(pOld)).FnEntryTombstone _fts5DataRemoveSegment(tls, p, pOld) goto _5 _5: ; i++ } /* Remove the redundant segments from the input level */ if (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg != nInput { nMove = int32(uint32((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg-nInput) * uint32(56)) libc.Xmemmove(tls, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg+uintptr(nInput)*56, uint32(nMove)) } *(*int32)(unsafe.Pointer(pStruct + 24)) -= nInput *(*int32)(unsafe.Pointer(pLvl + 4)) -= nInput (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge = 0 if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast == 0 { (*TFts5StructureLevel)(unsafe.Pointer(pLvlOut)).FnSeg-- (*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment-- } } else { _ = libc.Int32FromInt32(0) _fts5TrimSegments(tls, p, *(*uintptr)(unsafe.Pointer(bp))) (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge = nInput } _fts5MultiIterFree(tls, *(*uintptr)(unsafe.Pointer(bp))) _sqlite3Fts5BufferFree(tls, bp+104) if pnRem != 0 { *(*int32)(unsafe.Pointer(pnRem)) -= (*(*TFts5SegWriter)(unsafe.Pointer(bp + 8))).FnLeafWritten } } // C documentation // // /* // ** If this is not a contentless_delete=1 table, or if the 'deletemerge' // ** configuration option is set to 0, then this function always returns -1. // ** Otherwise, it searches the structure object passed as the second argument // ** for a level suitable for merging due to having a large number of // ** tombstones in the tombstone hash. If one is found, its index is returned. // ** Otherwise, if there is no suitable level, -1. // */ func _fts5IndexFindDeleteMerge(tls *libc.TLS, p uintptr, pStruct uintptr) (r int32) { var iRet, iSeg, ii, nBest, nPercent int32 var nEntry, nTomb Ti64 var pConfig, pLvl uintptr _, _, _, _, _, _, _, _, _ = iRet, iSeg, ii, nBest, nEntry, nPercent, nTomb, pConfig, pLvl pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig iRet = -int32(1) if (*TFts5Config)(unsafe.Pointer(pConfig)).FbContentlessDelete != 0 && (*TFts5Config)(unsafe.Pointer(pConfig)).FnDeleteMerge > 0 { nBest = 0 ii = 0 for { if !(ii < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) { break } pLvl = pStruct + 32 + uintptr(ii)*12 nEntry = 0 nTomb = 0 iSeg = 0 for { if !(iSeg < (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg) { break } nEntry = Ti64(uint64(nEntry) + (*(*TFts5StructureSegment)(unsafe.Pointer((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr(iSeg)*56))).FnEntry) nTomb = Ti64(uint64(nTomb) + (*(*TFts5StructureSegment)(unsafe.Pointer((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr(iSeg)*56))).FnEntryTombstone) goto _2 _2: ; iSeg++ } _ = libc.Int32FromInt32(0) if nEntry > 0 { nPercent = int32(nTomb * int64(100) / nEntry) if nPercent >= (*TFts5Config)(unsafe.Pointer(pConfig)).FnDeleteMerge && nPercent > nBest { iRet = ii nBest = nPercent } } goto _1 _1: ; ii++ } } return iRet } // C documentation // // /* // ** Do up to nPg pages of automerge work on the index. // ** // ** Return true if any changes were actually made, or false otherwise. // */ func _fts5IndexMerge(tls *libc.TLS, p uintptr, ppStruct uintptr, nPg int32, nMin int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bRet, iBestLvl, iLvl, nBest int32 var pLvl uintptr var _ /* nRem at bp+0 */ int32 var _ /* pStruct at bp+4 */ uintptr _, _, _, _, _ = bRet, iBestLvl, iLvl, nBest, pLvl *(*int32)(unsafe.Pointer(bp)) = nPg bRet = 0 *(*uintptr)(unsafe.Pointer(bp + 4)) = *(*uintptr)(unsafe.Pointer(ppStruct)) for *(*int32)(unsafe.Pointer(bp)) > 0 && (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { /* To iterate through levels */ iBestLvl = 0 /* Level offering the most input segments */ nBest = 0 /* Number of input segments on best level */ /* Set iBestLvl to the level to read input segments from. Or to -1 if ** there is no level suitable to merge segments from. */ _ = libc.Int32FromInt32(0) iLvl = 0 for { if !(iLvl < (*TFts5Structure)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FnLevel) { break } pLvl = *(*uintptr)(unsafe.Pointer(bp + 4)) + 32 + uintptr(iLvl)*12 if (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge != 0 { if (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge > nBest { iBestLvl = iLvl nBest = nMin } break } if (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg > nBest { nBest = (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg iBestLvl = iLvl } goto _1 _1: ; iLvl++ } if nBest < nMin { iBestLvl = _fts5IndexFindDeleteMerge(tls, p, *(*uintptr)(unsafe.Pointer(bp + 4))) } if iBestLvl < 0 { break } bRet = int32(1) _fts5IndexMergeLevel(tls, p, bp+4, iBestLvl, bp) if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*(*TFts5StructureLevel)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)) + 32 + uintptr(iBestLvl)*12))).FnMerge == 0 { _fts5StructurePromote(tls, p, iBestLvl+int32(1), *(*uintptr)(unsafe.Pointer(bp + 4))) } if nMin == int32(1) { nMin = int32(2) } } *(*uintptr)(unsafe.Pointer(ppStruct)) = *(*uintptr)(unsafe.Pointer(bp + 4)) return bRet } // C documentation // // /* // ** A total of nLeaf leaf pages of data has just been flushed to a level-0 // ** segment. This function updates the write-counter accordingly and, if // ** necessary, performs incremental merge work. // ** // ** If an error occurs, set the Fts5Index.rc error code. If an error has // ** already occurred, this function is a no-op. // */ func _fts5IndexAutomerge(tls *libc.TLS, p uintptr, ppStruct uintptr, nLeaf int32) { var nRem, nWork int32 var nWrite Tu64 var pStruct uintptr _, _, _, _ = nRem, nWork, nWrite, pStruct if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FnAutomerge > 0 && *(*uintptr)(unsafe.Pointer(ppStruct)) != uintptr(0) { pStruct = *(*uintptr)(unsafe.Pointer(ppStruct)) /* Number of leaf pages left to write */ /* Update the write-counter. While doing so, set nWork. */ nWrite = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnWriteCounter nWork = int32((nWrite+uint64(nLeaf))/uint64((*TFts5Index)(unsafe.Pointer(p)).FnWorkUnit) - nWrite/uint64((*TFts5Index)(unsafe.Pointer(p)).FnWorkUnit)) *(*Tu64)(unsafe.Pointer(pStruct + 8)) += uint64(nLeaf) nRem = (*TFts5Index)(unsafe.Pointer(p)).FnWorkUnit * nWork * (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel _fts5IndexMerge(tls, p, ppStruct, nRem, (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FnAutomerge) } } func _fts5IndexCrisismerge(tls *libc.TLS, p uintptr, ppStruct uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var iLvl, nCrisis int32 var _ /* pStruct at bp+0 */ uintptr _, _ = iLvl, nCrisis nCrisis = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FnCrisisMerge *(*uintptr)(unsafe.Pointer(bp)) = *(*uintptr)(unsafe.Pointer(ppStruct)) if *(*uintptr)(unsafe.Pointer(bp)) != 0 && (*TFts5Structure)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnLevel > 0 { iLvl = 0 for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*(*TFts5StructureLevel)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)) + 32 + uintptr(iLvl)*12))).FnSeg >= nCrisis { _fts5IndexMergeLevel(tls, p, bp, iLvl, uintptr(0)) _ = libc.Int32FromInt32(0) _fts5StructurePromote(tls, p, iLvl+int32(1), *(*uintptr)(unsafe.Pointer(bp))) iLvl++ } *(*uintptr)(unsafe.Pointer(ppStruct)) = *(*uintptr)(unsafe.Pointer(bp)) } } func _fts5IndexReturn(tls *libc.TLS, p uintptr) (r int32) { var rc int32 _ = rc rc = (*TFts5Index)(unsafe.Pointer(p)).Frc (*TFts5Index)(unsafe.Pointer(p)).Frc = SQLITE_OK return rc } type TFts5FlushCtx = struct { F__ccgo_align [0]uint32 FpIdx uintptr F__ccgo_align1 [4]byte Fwriter TFts5SegWriter } type Fts5FlushCtx = TFts5FlushCtx type TFts5FlushCtx1 = struct { F__ccgo_align [0]uint32 FpIdx uintptr F__ccgo_align1 [4]byte Fwriter TFts5SegWriter } type Fts5FlushCtx1 = TFts5FlushCtx1 // C documentation // // /* // ** Buffer aBuf[] contains a list of varints, all small enough to fit // ** in a 32-bit integer. Return the size of the largest prefix of this // ** list nMax bytes or less in size. // */ func _fts5PoslistPrefix(tls *libc.TLS, aBuf uintptr, nMax int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var i, ret int32 var _ /* dummy at bp+0 */ Tu32 _, _ = i, ret ret = _sqlite3Fts5GetVarint32(tls, aBuf, bp) if ret < nMax { for int32(1) != 0 { i = _sqlite3Fts5GetVarint32(tls, aBuf+uintptr(ret), bp) if ret+i > nMax { break } ret += i } } return ret } // C documentation // // /* // ** Execute the SQL statement: // ** // ** DELETE FROM %_idx WHERE (segid, (pgno/2)) = ($iSegid, $iPgno); // ** // ** This is used when a secure-delete operation removes the last term // ** from a segment leaf page. In that case the %_idx entry is removed // ** too. This is done to ensure that if all instances of a token are // ** removed from an fts5 database in secure-delete mode, no trace of // ** the token itself remains in the database. // */ func _fts5SecureDeleteIdxEntry(tls *libc.TLS, p uintptr, iSegid int32, iPgno int32) { bp := tls.Alloc(32) defer tls.Free(32) if iPgno != int32(1) { _ = libc.Int32FromInt32(0) if (*TFts5Index)(unsafe.Pointer(p)).FpDeleteFromIdx == uintptr(0) { _fts5IndexPrepareStmt(tls, p, p+84, Xsqlite3_mprintf(tls, __ccgo_ts+36563, libc.VaList(bp+8, (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FzDb, (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FzName))) } if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { Xsqlite3_bind_int(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDeleteFromIdx, int32(1), iSegid) Xsqlite3_bind_int(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDeleteFromIdx, int32(2), iPgno) Xsqlite3_step(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDeleteFromIdx) (*TFts5Index)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDeleteFromIdx) } } } // C documentation // // /* // ** This is called when a secure-delete operation removes a position-list // ** that overflows onto segment page iPgno of segment pSeg. This function // ** rewrites node iPgno, and possibly one or more of its right-hand peers, // ** to remove this portion of the position list. // ** // ** Output variable (*pbLastInDoclist) is set to true if the position-list // ** removed is followed by a new term or the end-of-segment, or false if // ** it is followed by another rowid/position list. // */ func _fts5SecureDeleteOverflow(tls *libc.TLS, p uintptr, pSeg uintptr, iPgno int32, pbLastInDoclist uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var aIdx, aPg, pLeaf uintptr var bDetailNone, i1, i2, nIdx, nPg, nShift, pgno int32 var iRowid Ti64 var _ /* aEmpty at bp+4 */ [4]Tu8 var _ /* iFirst at bp+8 */ int32 var _ /* iNext at bp+0 */ int32 _, _, _, _, _, _, _, _, _, _, _ = aIdx, aPg, bDetailNone, i1, i2, iRowid, nIdx, nPg, nShift, pLeaf, pgno bDetailNone = libc.BoolInt32((*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail == libc.Int32FromInt32(FTS5_DETAIL_NONE)) pLeaf = uintptr(0) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pbLastInDoclist)) = int32(1) pgno = iPgno for { if !((*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && pgno <= (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast) { break } iRowid = int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<= (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf || (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn < (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf || *(*int32)(unsafe.Pointer(bp)) < int32(4) { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf { *(*int32)(unsafe.Pointer(bp + 8)) = 0 i1 = (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf i2 = 0 i1 += _sqlite3Fts5GetVarint32(tls, aPg+uintptr(i1), bp+8) if *(*int32)(unsafe.Pointer(bp + 8)) < *(*int32)(unsafe.Pointer(bp)) { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< 0 { libc.Xmemcpy(tls, aPg+uintptr(nPg), aIdx, uint32(nIdx)) nPg += nIdx } Xsqlite3_free(tls, aIdx) /* Write the new page to disk and exit the loop */ _ = libc.Int32FromInt32(0) _fts5DataWrite(tls, p, iRowid, aPg, nPg) break } } } goto _1 _1: ; pgno++ } _fts5DataRelease(tls, pLeaf) } // C documentation // // /* // ** Completely remove the entry that pSeg currently points to from // ** the database. // */ func _fts5DoSecureDelete(tls *libc.TLS, p uintptr, pSeg uintptr) { bp := tls.Alloc(64) defer tls.Free(64) var aIdx, aPg, aTermIdx, pPg, pTerm uintptr var bDetailNone, bEmpty, iDelKeyOff, iIdx, iKey, iKeyIn, iKeyOff, iKeyOff1, iKeyOut, iNextOff, iOff, iPgIdx, iPgno, iPrevKeyOut, iSOP, iSegid, iStart, iTermIdx, iTermOff, nByte, nIdx, nMove, nPg, nShift, nTermIdx, pgno, v2, v4, v5, v8 int32 var iId Ti64 var _ /* bLastInDoclist at bp+8 */ int32 var _ /* iDelta at bp+0 */ Tu64 var _ /* iNextDelta at bp+24 */ Tu64 var _ /* iVal at bp+16 */ Tu32 var _ /* iVal at bp+32 */ Tu32 var _ /* iVal at bp+52 */ Tu32 var _ /* iVal at bp+56 */ Tu32 var _ /* nPos at bp+12 */ int32 var _ /* nPrefix at bp+36 */ int32 var _ /* nPrefix2 at bp+44 */ int32 var _ /* nSuffix at bp+40 */ int32 var _ /* nSuffix2 at bp+48 */ int32 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aIdx, aPg, aTermIdx, bDetailNone, bEmpty, iDelKeyOff, iId, iIdx, iKey, iKeyIn, iKeyOff, iKeyOff1, iKeyOut, iNextOff, iOff, iPgIdx, iPgno, iPrevKeyOut, iSOP, iSegid, iStart, iTermIdx, iTermOff, nByte, nIdx, nMove, nPg, nShift, nTermIdx, pPg, pTerm, pgno, v2, v4, v5, v8 bDetailNone = libc.BoolInt32((*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail == libc.Int32FromInt32(FTS5_DETAIL_NONE)) iSegid = (*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg)).FiSegid aPg = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).Fp nPg = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).Fnn iPgIdx = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).FszLeaf *(*Tu64)(unsafe.Pointer(bp)) = uint64(0) iNextOff = 0 iOff = 0 nIdx = 0 aIdx = uintptr(0) *(*int32)(unsafe.Pointer(bp + 8)) = 0 iIdx = 0 iStart = 0 iDelKeyOff = 0 /* Offset of deleted key, if any */ nIdx = nPg - iPgIdx aIdx = _sqlite3Fts5MallocZero(tls, p+44, int64(nIdx+int32(16))) if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 { return } libc.Xmemcpy(tls, aIdx, aPg+uintptr(iPgIdx), uint32(nIdx)) /* At this point segment iterator pSeg points to the entry ** this function should remove from the b-tree segment. ** ** In detail=full or detail=column mode, pSeg->iLeafOffset is the ** offset of the first byte in the position-list for the entry to ** remove. Immediately before this comes two varints that will also ** need to be removed: ** ** + the rowid or delta rowid value for the entry, and ** + the size of the position list in bytes. ** ** Or, in detail=none mode, there is a single varint prior to ** pSeg->iLeafOffset - the rowid or delta rowid value. ** ** This block sets the following variables: ** ** iStart: ** The offset of the first byte of the rowid or delta-rowid ** value for the doclist entry being removed. ** ** iDelta: ** The value of the rowid or delta-rowid value for the doclist ** entry being removed. ** ** iNextOff: ** The offset of the next entry following the position list ** for the one being removed. If the position list for this ** entry overflows onto the next leaf page, this value will be ** greater than pLeaf->szLeaf. */ /* Start-Of-Position-list */ if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno == (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafPgno { iStart = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafOffset } else { iStart = int32(_fts5GetU16(tls, aPg)) } iSOP = iStart + int32(_sqlite3Fts5GetVarint(tls, aPg+uintptr(iStart), bp)) _ = libc.Int32FromInt32(0) if bDetailNone != 0 { for int64(iSOP) < (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset { if int32(*(*Tu8)(unsafe.Pointer(aPg + uintptr(iSOP)))) == 0x00 { iSOP++ } if int32(*(*Tu8)(unsafe.Pointer(aPg + uintptr(iSOP)))) == 0x00 { iSOP++ } iStart = iSOP iSOP = iStart + int32(_sqlite3Fts5GetVarint(tls, aPg+uintptr(iStart), bp)) } iNextOff = iSOP if iNextOff < (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiEndofDoclist && int32(*(*Tu8)(unsafe.Pointer(aPg + uintptr(iNextOff)))) == 0x00 { iNextOff++ } if iNextOff < (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiEndofDoclist && int32(*(*Tu8)(unsafe.Pointer(aPg + uintptr(iNextOff)))) == 0x00 { iNextOff++ } } else { *(*int32)(unsafe.Pointer(bp + 12)) = 0 iSOP += _sqlite3Fts5GetVarint32(tls, aPg+uintptr(iSOP), bp+12) for int64(iSOP) < (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset { iStart = iSOP + *(*int32)(unsafe.Pointer(bp + 12))/int32(2) iSOP = iStart + int32(_sqlite3Fts5GetVarint(tls, aPg+uintptr(iStart), bp)) iSOP += _sqlite3Fts5GetVarint32(tls, aPg+uintptr(iSOP), bp+12) } _ = libc.Int32FromInt32(0) iNextOff = int32((*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset + int64((*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos)) } iOff = iStart /* If the position-list for the entry being removed flows over past ** the end of this page, delete the portion of the position-list on the ** next page and beyond. ** ** Set variable bLastInDoclist to true if this entry happens ** to be the last rowid in the doclist for its term. */ if iNextOff >= iPgIdx { pgno = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno + int32(1) _fts5SecureDeleteOverflow(tls, p, (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg, pgno, bp+8) iNextOff = iPgIdx } if int32((*TFts5SegIter)(unsafe.Pointer(pSeg)).FbDel) == 0 { if iNextOff != iPgIdx { /* Loop through the page-footer. If iNextOff (offset of the ** entry following the one we are removing) is equal to the ** offset of a key on this page, then the entry is the last ** in its doclist. */ iKeyOff = 0 iIdx = 0 for { if !(iIdx < nIdx) { break } *(*Tu32)(unsafe.Pointer(bp + 16)) = uint32(0) iIdx += _sqlite3Fts5GetVarint32(tls, aIdx+uintptr(iIdx), bp+16) iKeyOff = int32(uint32(iKeyOff) + *(*Tu32)(unsafe.Pointer(bp + 16))) if iKeyOff == iNextOff { *(*int32)(unsafe.Pointer(bp + 8)) = int32(1) } goto _1 _1: } } /* If this is (a) the first rowid on a page and (b) is not followed by ** another position list on the same page, set the "first-rowid" field ** of the header to 0. */ if int32(_fts5GetU16(tls, aPg)) == iStart && (*(*int32)(unsafe.Pointer(bp + 8)) != 0 || iNextOff == iPgIdx) { _fts5PutU16(tls, aPg, uint16(0)) } } if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FbDel != 0 { iOff += _sqlite3Fts5PutVarint(tls, aPg+uintptr(iOff), *(*Tu64)(unsafe.Pointer(bp))) v2 = iOff iOff++ *(*Tu8)(unsafe.Pointer(aPg + uintptr(v2))) = uint8(0x01) } else { if *(*int32)(unsafe.Pointer(bp + 8)) == 0 { if iNextOff != iPgIdx { *(*Tu64)(unsafe.Pointer(bp + 24)) = uint64(0) iNextOff += int32(_sqlite3Fts5GetVarint(tls, aPg+uintptr(iNextOff), bp+24)) iOff += _sqlite3Fts5PutVarint(tls, aPg+uintptr(iOff), *(*Tu64)(unsafe.Pointer(bp))+*(*Tu64)(unsafe.Pointer(bp + 24))) } } else { if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno == (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafPgno && iStart == (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafOffset { /* The entry being removed was the only position list in its ** doclist. Therefore the term needs to be removed as well. */ iKey = 0 iKeyOff1 = 0 /* Set iKeyOff to the offset of the term that will be removed - the ** last offset in the footer that is not greater than iStart. */ iIdx = 0 for { if !(iIdx < nIdx) { break } *(*Tu32)(unsafe.Pointer(bp + 32)) = uint32(0) iIdx += _sqlite3Fts5GetVarint32(tls, aIdx+uintptr(iIdx), bp+32) if uint32(iKeyOff1)+*(*Tu32)(unsafe.Pointer(bp + 32)) > uint32(iStart) { break } iKeyOff1 = int32(uint32(iKeyOff1) + *(*Tu32)(unsafe.Pointer(bp + 32))) goto _3 _3: ; iKey++ } _ = libc.Int32FromInt32(0) /* Set iDelKeyOff to the value of the footer entry to remove from ** the page. */ v4 = iKeyOff1 iOff = v4 iDelKeyOff = v4 if iNextOff != iPgIdx { /* This is the only position-list associated with the term, and there ** is another term following it on this page. So the subsequent term ** needs to be moved to replace the term associated with the entry ** being removed. */ *(*int32)(unsafe.Pointer(bp + 36)) = 0 *(*int32)(unsafe.Pointer(bp + 40)) = 0 *(*int32)(unsafe.Pointer(bp + 44)) = 0 *(*int32)(unsafe.Pointer(bp + 48)) = 0 iDelKeyOff = iNextOff iNextOff += _sqlite3Fts5GetVarint32(tls, aPg+uintptr(iNextOff), bp+44) iNextOff += _sqlite3Fts5GetVarint32(tls, aPg+uintptr(iNextOff), bp+48) if iKey != int32(1) { iKeyOff1 += _sqlite3Fts5GetVarint32(tls, aPg+uintptr(iKeyOff1), bp+36) } iKeyOff1 += _sqlite3Fts5GetVarint32(tls, aPg+uintptr(iKeyOff1), bp+40) if *(*int32)(unsafe.Pointer(bp + 36)) < *(*int32)(unsafe.Pointer(bp + 44)) { v5 = *(*int32)(unsafe.Pointer(bp + 36)) } else { v5 = *(*int32)(unsafe.Pointer(bp + 44)) } *(*int32)(unsafe.Pointer(bp + 36)) = v5 *(*int32)(unsafe.Pointer(bp + 40)) = *(*int32)(unsafe.Pointer(bp + 44)) + *(*int32)(unsafe.Pointer(bp + 48)) - *(*int32)(unsafe.Pointer(bp + 36)) if iKeyOff1+*(*int32)(unsafe.Pointer(bp + 40)) > iPgIdx || iNextOff+*(*int32)(unsafe.Pointer(bp + 48)) > iPgIdx { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< (*TFts5SegIter)(unsafe.Pointer(pSeg)).Fterm.Fn { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< *(*int32)(unsafe.Pointer(bp + 36)) { libc.Xmemcpy(tls, aPg+uintptr(iOff), (*TFts5SegIter)(unsafe.Pointer(pSeg)).Fterm.Fp+uintptr(*(*int32)(unsafe.Pointer(bp + 36))), uint32(*(*int32)(unsafe.Pointer(bp + 44))-*(*int32)(unsafe.Pointer(bp + 36)))) iOff += *(*int32)(unsafe.Pointer(bp + 44)) - *(*int32)(unsafe.Pointer(bp + 36)) } } libc.Xmemmove(tls, aPg+uintptr(iOff), aPg+uintptr(iNextOff), uint32(*(*int32)(unsafe.Pointer(bp + 48)))) iOff += *(*int32)(unsafe.Pointer(bp + 48)) iNextOff += *(*int32)(unsafe.Pointer(bp + 48)) } } } else { if iStart == int32(4) { _ = libc.Int32FromInt32(0) /* The entry being removed may be the only position list in ** its doclist. */ iPgno = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno - int32(1) for { if !(iPgno > (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafPgno) { break } pPg = _fts5DataRead(tls, p, int64(iSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(libc.Int32FromInt32(0))<= nTermIdx { break } iTermIdx += nByte } nTermIdx = iTermIdx libc.Xmemmove(tls, (*TFts5Data)(unsafe.Pointer(pTerm)).Fp+uintptr(iTermOff), (*TFts5Data)(unsafe.Pointer(pTerm)).Fp+uintptr((*TFts5Data)(unsafe.Pointer(pTerm)).FszLeaf), uint32(nTermIdx)) _fts5PutU16(tls, (*TFts5Data)(unsafe.Pointer(pTerm)).Fp+2, uint16(iTermOff)) _fts5DataWrite(tls, p, iId, (*TFts5Data)(unsafe.Pointer(pTerm)).Fp, iTermOff+nTermIdx) if nTermIdx == 0 { _fts5SecureDeleteIdxEntry(tls, p, iSegid, (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafPgno) } } _fts5DataRelease(tls, pTerm) } } } } } /* Assuming no error has occurred, this block does final edits to the ** leaf page before writing it back to disk. Input variables are: ** ** nPg: Total initial size of leaf page. ** iPgIdx: Initial offset of page footer. ** ** iOff: Offset to move data to ** iNextOff: Offset to move data from */ if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { nMove = nPg - iNextOff /* Number of bytes to move */ nShift = iNextOff - iOff /* Distance to move them */ iPrevKeyOut = 0 iKeyIn = 0 libc.Xmemmove(tls, aPg+uintptr(iOff), aPg+uintptr(iNextOff), uint32(nMove)) iPgIdx -= nShift nPg = iPgIdx _fts5PutU16(tls, aPg+2, uint16(iPgIdx)) iIdx = 0 for { if !(iIdx < nIdx) { break } *(*Tu32)(unsafe.Pointer(bp + 56)) = uint32(0) iIdx += _sqlite3Fts5GetVarint32(tls, aIdx+uintptr(iIdx), bp+56) iKeyIn = int32(uint32(iKeyIn) + *(*Tu32)(unsafe.Pointer(bp + 56))) if iKeyIn != iDelKeyOff { if iKeyIn > iOff { v8 = nShift } else { v8 = 0 } iKeyOut = iKeyIn - v8 nPg += _sqlite3Fts5PutVarint(tls, aPg+uintptr(nPg), uint64(iKeyOut-iPrevKeyOut)) iPrevKeyOut = iKeyOut } goto _7 _7: } if iPgIdx == nPg && nIdx > 0 && (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno != int32(1) { _fts5SecureDeleteIdxEntry(tls, p, iSegid, (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno) } _ = libc.Int32FromInt32(0) _fts5DataWrite(tls, p, int64(iSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(libc.Int32FromInt32(0))<= (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn+(*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fn+*(*int32)(unsafe.Pointer(bp + 116))+int32(1) { /* The entire doclist will fit on the current leaf. */ _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), *(*uintptr)(unsafe.Pointer(bp + 112)), uint32(*(*int32)(unsafe.Pointer(bp + 116)))) *(*int32)(unsafe.Pointer(pBuf + 4)) += *(*int32)(unsafe.Pointer(bp + 116)) } else { bTermWritten = libc.BoolInt32(!(bSecureDelete != 0)) iRowid = 0 iPrev = 0 iOff = 0 /* The entire doclist will not fit on this leaf. The following ** loop iterates through the poslists that make up the current ** doclist. */ for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && iOff < *(*int32)(unsafe.Pointer(bp + 116)) { *(*Tu64)(unsafe.Pointer(bp + 120)) = uint64(0) iOff += int32(_sqlite3Fts5GetVarint(tls, *(*uintptr)(unsafe.Pointer(bp + 112))+uintptr(iOff), bp+120)) iRowid = Ti64(uint64(iRowid) + *(*Tu64)(unsafe.Pointer(bp + 120))) /* If in secure delete mode, and if this entry in the poslist is ** in fact a delete, then edit the existing segments directly ** using fts5FlushSecureDelete(). */ if bSecureDelete != 0 { if eDetail == int32(FTS5_DETAIL_NONE) { if iOff < *(*int32)(unsafe.Pointer(bp + 116)) && int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 112)) + uintptr(iOff)))) == 0x00 { _fts5FlushSecureDelete(tls, p, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 104)), *(*int32)(unsafe.Pointer(bp + 108)), iRowid) iOff++ if iOff < *(*int32)(unsafe.Pointer(bp + 116)) && int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 112)) + uintptr(iOff)))) == 0x00 { iOff++ *(*int32)(unsafe.Pointer(bp + 116)) = 0 } else { continue } } } else { if int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 112)) + uintptr(iOff))))&int32(0x01) != 0 { _fts5FlushSecureDelete(tls, p, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 104)), *(*int32)(unsafe.Pointer(bp + 108)), iRowid) if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK || int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 112)) + uintptr(iOff)))) == int32(0x01) { iOff++ continue } } } } if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && bTermWritten == 0 { _fts5WriteAppendTerm(tls, p, bp+8, *(*int32)(unsafe.Pointer(bp + 108)), *(*uintptr)(unsafe.Pointer(bp + 104))) bTermWritten = int32(1) _ = libc.Int32FromInt32(0) } if (*(*TFts5SegWriter)(unsafe.Pointer(bp + 8))).FbFirstRowidInPage != 0 { _fts5PutU16(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp, uint16((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)) /* first rowid on page */ *(*int32)(unsafe.Pointer(pBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), uint64(iRowid)) (*(*TFts5SegWriter)(unsafe.Pointer(bp + 8))).FbFirstRowidInPage = uint8(0) _fts5WriteDlidxAppend(tls, p, bp+8, iRowid) } else { iRowidDelta = uint64(iRowid) - uint64(iPrev) *(*int32)(unsafe.Pointer(pBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), iRowidDelta) } if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK { break } _ = libc.Int32FromInt32(0) iPrev = iRowid if eDetail == int32(FTS5_DETAIL_NONE) { if iOff < *(*int32)(unsafe.Pointer(bp + 116)) && int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 112)) + uintptr(iOff)))) == 0 { v2 = pBuf + 4 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ *(*Tu8)(unsafe.Pointer((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp + uintptr(v1))) = uint8(0) iOff++ if iOff < *(*int32)(unsafe.Pointer(bp + 116)) && int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 112)) + uintptr(iOff)))) == 0 { v4 = pBuf + 4 v3 = *(*int32)(unsafe.Pointer(v4)) *(*int32)(unsafe.Pointer(v4))++ *(*Tu8)(unsafe.Pointer((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp + uintptr(v3))) = uint8(0) iOff++ } } if (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn+(*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fn >= pgsz { _fts5WriteFlushLeaf(tls, p, bp+8) } } else { *(*int32)(unsafe.Pointer(bp + 128)) = 0 *(*int32)(unsafe.Pointer(bp + 132)) = 0 nCopy = _fts5GetPoslistSize(tls, *(*uintptr)(unsafe.Pointer(bp + 112))+uintptr(iOff), bp+132, bp+128) if *(*int32)(unsafe.Pointer(bp + 128)) != 0 && bSecureDelete != 0 { _sqlite3Fts5BufferAppendVarint(tls, p+44, pBuf, int64(*(*int32)(unsafe.Pointer(bp + 132)))*int64(2)) iOff += nCopy nCopy = *(*int32)(unsafe.Pointer(bp + 132)) } else { nCopy += *(*int32)(unsafe.Pointer(bp + 132)) } if (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn+(*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fn+nCopy <= pgsz { /* The entire poslist will fit on the current leaf. So copy ** it in one go. */ _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), *(*uintptr)(unsafe.Pointer(bp + 112))+uintptr(iOff), uint32(nCopy)) *(*int32)(unsafe.Pointer(pBuf + 4)) += nCopy } else { /* The entire poslist will not fit on this leaf. So it needs ** to be broken into sections. The only qualification being ** that each varint must be stored contiguously. */ pPoslist = *(*uintptr)(unsafe.Pointer(bp + 112)) + uintptr(iOff) iPos = 0 for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { nSpace = pgsz - (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn - (*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fn n = 0 if nCopy-iPos <= nSpace { n = nCopy - iPos } else { n = _fts5PoslistPrefix(tls, pPoslist+uintptr(iPos), nSpace) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), pPoslist+uintptr(iPos), uint32(n)) *(*int32)(unsafe.Pointer(pBuf + 4)) += n iPos += n if (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn+(*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fn >= pgsz { _fts5WriteFlushLeaf(tls, p, bp+8) } if iPos >= nCopy { break } } } iOff += nCopy } } } /* TODO2: Doclist terminator written here. */ /* pBuf->p[pBuf->n++] = '\0'; */ _ = libc.Int32FromInt32(0) if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { _sqlite3Fts5HashScanNext(tls, pHash) } } _fts5WriteFinish(tls, p, bp+8, bp+4) _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp + 4)) > 0 { /* Update the Fts5Structure. It is written back to the database by the ** fts5StructureRelease() call below. */ if (*TFts5Structure)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnLevel == 0 { _fts5StructureAddLevel(tls, p+44, bp) } _fts5StructureExtendLevel(tls, p+44, *(*uintptr)(unsafe.Pointer(bp)), 0, int32(1), 0) if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { v6 = *(*uintptr)(unsafe.Pointer(bp)) + 32 + 4 v5 = *(*int32)(unsafe.Pointer(v6)) *(*int32)(unsafe.Pointer(v6))++ pSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)) + 32))).FaSeg + uintptr(v5)*56 (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid = iSegid (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst = int32(1) (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast = *(*int32)(unsafe.Pointer(bp + 4)) if (*TFts5Structure)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnOriginCntr > uint64(0) { (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin1 = (*TFts5Structure)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnOriginCntr (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin2 = (*TFts5Structure)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnOriginCntr (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnEntry = uint64((*TFts5Index)(unsafe.Pointer(p)).FnPendingRow) (*TFts5Structure)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnOriginCntr++ } (*TFts5Structure)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnSegment++ } _fts5StructurePromote(tls, p, 0, *(*uintptr)(unsafe.Pointer(bp))) } } } _fts5IndexAutomerge(tls, p, bp, *(*int32)(unsafe.Pointer(bp + 4))+(*TFts5Index)(unsafe.Pointer(p)).FnContentlessDelete) _fts5IndexCrisismerge(tls, p, bp) _fts5StructureWrite(tls, p, *(*uintptr)(unsafe.Pointer(bp))) _fts5StructureRelease(tls, *(*uintptr)(unsafe.Pointer(bp))) } // C documentation // // /* // ** Flush any data stored in the in-memory hash tables to the database. // */ func _fts5IndexFlush(tls *libc.TLS, p uintptr) { /* Unless it is empty, flush the hash table to disk */ if (*TFts5Index)(unsafe.Pointer(p)).FflushRc != 0 { (*TFts5Index)(unsafe.Pointer(p)).Frc = (*TFts5Index)(unsafe.Pointer(p)).FflushRc return } if (*TFts5Index)(unsafe.Pointer(p)).FnPendingData != 0 || (*TFts5Index)(unsafe.Pointer(p)).FnContentlessDelete != 0 { _ = libc.Int32FromInt32(0) _fts5FlushOneHash(tls, p) if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { _sqlite3Fts5HashClear(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash) (*TFts5Index)(unsafe.Pointer(p)).FnPendingData = 0 (*TFts5Index)(unsafe.Pointer(p)).FnPendingRow = 0 (*TFts5Index)(unsafe.Pointer(p)).FnContentlessDelete = 0 } else { if (*TFts5Index)(unsafe.Pointer(p)).FnPendingData != 0 || (*TFts5Index)(unsafe.Pointer(p)).FnContentlessDelete != 0 { (*TFts5Index)(unsafe.Pointer(p)).FflushRc = (*TFts5Index)(unsafe.Pointer(p)).Frc } } } } func _fts5IndexOptimizeStruct(tls *libc.TLS, p uintptr, pStruct uintptr) (r uintptr) { var i, iLvl, iSeg, iSegOut, nMerge, nSeg, nThis, v2, v5 int32 var nByte Tsqlite3_int64 var pLvl, pNew uintptr _, _, _, _, _, _, _, _, _, _, _, _ = i, iLvl, iSeg, iSegOut, nByte, nMerge, nSeg, nThis, pLvl, pNew, v2, v5 pNew = uintptr(0) nByte = int64(48) nSeg = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment /* Figure out if this structure requires optimization. A structure does ** not require optimization if either: ** ** 1. it consists of fewer than two segments, or ** 2. all segments are on the same level, or ** 3. all segments except one are currently inputs to a merge operation. ** ** In the first case, if there are no tombstone hash pages, return NULL. In ** the second, increment the ref-count on *pStruct and return a copy of the ** pointer to it. */ if nSeg == 0 { return uintptr(0) } i = 0 for { if !(i < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) { break } nThis = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(i)*12))).FnSeg nMerge = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(i)*12))).FnMerge if nThis > 0 && (nThis == nSeg || nThis == nSeg-int32(1) && nMerge == nThis) { if nSeg == int32(1) && nThis == int32(1) && (*(*TFts5StructureSegment)(unsafe.Pointer((*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(i)*12))).FaSeg))).FnPgTombstone == 0 { return uintptr(0) } _fts5StructureRef(tls, pStruct) return pStruct } _ = libc.Int32FromInt32(0) goto _1 _1: ; i++ } nByte += int64(uint32((*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel+libc.Int32FromInt32(1)) * uint32(12)) pNew = _sqlite3Fts5MallocZero(tls, p+44, nByte) if pNew != 0 { nByte = int64(uint32(nSeg) * uint32(56)) if (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel+int32(1) < int32(FTS5_MAX_LEVEL) { v2 = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel + int32(1) } else { v2 = int32(FTS5_MAX_LEVEL) } (*TFts5Structure)(unsafe.Pointer(pNew)).FnLevel = v2 (*TFts5Structure)(unsafe.Pointer(pNew)).FnRef = int32(1) (*TFts5Structure)(unsafe.Pointer(pNew)).FnWriteCounter = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnWriteCounter (*TFts5Structure)(unsafe.Pointer(pNew)).FnOriginCntr = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnOriginCntr pLvl = pNew + 32 + uintptr((*TFts5Structure)(unsafe.Pointer(pNew)).FnLevel-int32(1))*12 (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg = _sqlite3Fts5MallocZero(tls, p+44, nByte) if (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg != 0 { iSegOut = 0 /* Iterate through all segments, from oldest to newest. Add them to ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest ** segment in the data structure. */ iLvl = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel - int32(1) for { if !(iLvl >= 0) { break } iSeg = 0 for { if !(iSeg < (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FnSeg) { break } *(*TFts5StructureSegment)(unsafe.Pointer((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr(iSegOut)*56)) = *(*TFts5StructureSegment)(unsafe.Pointer((*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FaSeg + uintptr(iSeg)*56)) iSegOut++ goto _4 _4: ; iSeg++ } goto _3 _3: ; iLvl-- } v5 = nSeg (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg = v5 (*TFts5Structure)(unsafe.Pointer(pNew)).FnSegment = v5 } else { Xsqlite3_free(tls, pNew) pNew = uintptr(0) } } return pNew } func _sqlite3Fts5IndexOptimize(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var iLvl int32 var pStruct uintptr var _ /* nRem at bp+4 */ int32 var _ /* pNew at bp+0 */ uintptr _, _ = iLvl, pStruct *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) _ = libc.Int32FromInt32(0) _fts5IndexFlush(tls, p) _ = libc.Int32FromInt32(0) pStruct = _fts5StructureRead(tls, p) _ = libc.Int32FromInt32(0) _fts5StructureInvalidate(tls, p) if pStruct != 0 { *(*uintptr)(unsafe.Pointer(bp)) = _fts5IndexOptimizeStruct(tls, p, pStruct) } _fts5StructureRelease(tls, pStruct) _ = libc.Int32FromInt32(0) if *(*uintptr)(unsafe.Pointer(bp)) != 0 { iLvl = 0 for { if !((*(*TFts5StructureLevel)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)) + 32 + uintptr(iLvl)*12))).FnSeg == 0) { break } goto _1 _1: ; iLvl++ } for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*(*TFts5StructureLevel)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)) + 32 + uintptr(iLvl)*12))).FnSeg > 0 { *(*int32)(unsafe.Pointer(bp + 4)) = int32(FTS5_OPT_WORK_UNIT) _fts5IndexMergeLevel(tls, p, bp, iLvl, bp+4) } _fts5StructureWrite(tls, p, *(*uintptr)(unsafe.Pointer(bp))) _fts5StructureRelease(tls, *(*uintptr)(unsafe.Pointer(bp))) } return _fts5IndexReturn(tls, p) } // C documentation // // /* // ** This is called to implement the special "VALUES('merge', $nMerge)" // ** INSERT command. // */ func _sqlite3Fts5IndexMerge(tls *libc.TLS, p uintptr, nMerge int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var nMin int32 var pNew uintptr var _ /* pStruct at bp+0 */ uintptr _, _ = nMin, pNew *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) _fts5IndexFlush(tls, p) *(*uintptr)(unsafe.Pointer(bp)) = _fts5StructureRead(tls, p) if *(*uintptr)(unsafe.Pointer(bp)) != 0 { nMin = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FnUsermerge _fts5StructureInvalidate(tls, p) if nMerge < 0 { pNew = _fts5IndexOptimizeStruct(tls, p, *(*uintptr)(unsafe.Pointer(bp))) _fts5StructureRelease(tls, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = pNew nMin = int32(1) nMerge = nMerge * -int32(1) } if *(*uintptr)(unsafe.Pointer(bp)) != 0 && (*TFts5Structure)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnLevel != 0 { if _fts5IndexMerge(tls, p, bp, nMerge, nMin) != 0 { _fts5StructureWrite(tls, p, *(*uintptr)(unsafe.Pointer(bp))) } } _fts5StructureRelease(tls, *(*uintptr)(unsafe.Pointer(bp))) } return _fts5IndexReturn(tls, p) } func _fts5AppendRowid(tls *libc.TLS, p uintptr, iDelta Tu64, pUnused uintptr, pBuf uintptr) { _ = pUnused _sqlite3Fts5BufferAppendVarint(tls, p+44, pBuf, int64(iDelta)) } func _fts5AppendPoslist(tls *libc.TLS, p uintptr, iDelta Tu64, pMulti uintptr, pBuf uintptr) { var nByte, nData, v1 int32 var v2 bool _, _, _, _ = nByte, nData, v1, v2 nData = (*TFts5Iter)(unsafe.Pointer(pMulti)).Fbase.FnData nByte = nData + int32(9) + int32(9) + int32(FTS5_DATA_ZERO_PADDING) _ = libc.Int32FromInt32(0) if v2 = (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK; v2 { if uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)+uint32(nByte) <= uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).FnSpace) { v1 = 0 } else { v1 = _sqlite3Fts5BufferSize(tls, p+44, pBuf, uint32(nByte+(*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)) } } if v2 && 0 == v1 { *(*int32)(unsafe.Pointer(pBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), iDelta) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), uint64(nData*libc.Int32FromInt32(2))) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), (*TFts5Iter)(unsafe.Pointer(pMulti)).Fbase.FpData, uint32(nData)) *(*int32)(unsafe.Pointer(pBuf + 4)) += nData libc.Xmemset(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), 0, uint32(FTS5_DATA_ZERO_PADDING)) } } func _fts5DoclistIterNext(tls *libc.TLS, pIter uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var p uintptr var _ /* iDelta at bp+0 */ Ti64 var _ /* nPos at bp+8 */ int32 _ = p p = (*TFts5DoclistIter)(unsafe.Pointer(pIter)).FaPoslist + uintptr((*TFts5DoclistIter)(unsafe.Pointer(pIter)).FnSize) + uintptr((*TFts5DoclistIter)(unsafe.Pointer(pIter)).FnPoslist) _ = libc.Int32FromInt32(0) if p >= (*TFts5DoclistIter)(unsafe.Pointer(pIter)).FaEof { (*TFts5DoclistIter)(unsafe.Pointer(pIter)).FaPoslist = uintptr(0) } else { p += uintptr(_sqlite3Fts5GetVarint(tls, p, bp)) *(*Ti64)(unsafe.Pointer(pIter + 8)) += *(*Ti64)(unsafe.Pointer(bp)) /* Read position list size */ if int32(*(*Tu8)(unsafe.Pointer(p)))&int32(0x80) != 0 { (*TFts5DoclistIter)(unsafe.Pointer(pIter)).FnSize = _sqlite3Fts5GetVarint32(tls, p, bp+8) (*TFts5DoclistIter)(unsafe.Pointer(pIter)).FnPoslist = *(*int32)(unsafe.Pointer(bp + 8)) >> int32(1) } else { (*TFts5DoclistIter)(unsafe.Pointer(pIter)).FnPoslist = int32(*(*Tu8)(unsafe.Pointer(p))) >> int32(1) (*TFts5DoclistIter)(unsafe.Pointer(pIter)).FnSize = int32(1) } (*TFts5DoclistIter)(unsafe.Pointer(pIter)).FaPoslist = p if (*TFts5DoclistIter)(unsafe.Pointer(pIter)).FaPoslist+uintptr((*TFts5DoclistIter)(unsafe.Pointer(pIter)).FnPoslist) > (*TFts5DoclistIter)(unsafe.Pointer(pIter)).FaEof { (*TFts5DoclistIter)(unsafe.Pointer(pIter)).FaPoslist = uintptr(0) } } } func _fts5DoclistIterInit(tls *libc.TLS, pBuf uintptr, pIter uintptr) { libc.Xmemset(tls, pIter, 0, uint32(32)) if (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn > 0 { (*TFts5DoclistIter)(unsafe.Pointer(pIter)).FaPoslist = (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp (*TFts5DoclistIter)(unsafe.Pointer(pIter)).FaEof = (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp + uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn) _fts5DoclistIterNext(tls, pIter) } } // C documentation // // /* // ** Swap the contents of buffer *p1 with that of *p2. // */ func _fts5BufferSwap(tls *libc.TLS, p1 uintptr, p2 uintptr) { var tmp TFts5Buffer _ = tmp tmp = *(*TFts5Buffer)(unsafe.Pointer(p1)) *(*TFts5Buffer)(unsafe.Pointer(p1)) = *(*TFts5Buffer)(unsafe.Pointer(p2)) *(*TFts5Buffer)(unsafe.Pointer(p2)) = tmp } func _fts5NextRowid(tls *libc.TLS, pBuf uintptr, piOff uintptr, piRowid uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var i int32 var p1 uintptr var _ /* iVal at bp+0 */ Tu64 _, _ = i, p1 i = *(*int32)(unsafe.Pointer(piOff)) if i >= (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn { *(*int32)(unsafe.Pointer(piOff)) = -int32(1) } else { *(*int32)(unsafe.Pointer(piOff)) = i + int32(_sqlite3Fts5GetVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr(i), bp)) p1 = piRowid *(*Ti64)(unsafe.Pointer(p1)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(p1))) + *(*Tu64)(unsafe.Pointer(bp))) } } // C documentation // // /* // ** This is the equivalent of fts5MergePrefixLists() for detail=none mode. // ** In this case the buffers consist of a delta-encoded list of rowids only. // */ func _fts5MergeRowidLists(tls *libc.TLS, p uintptr, p1 uintptr, nBuf int32, aBuf uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var iOut Ti64 var p2 uintptr var _ /* i1 at bp+0 */ int32 var _ /* i2 at bp+4 */ int32 var _ /* iRowid1 at bp+8 */ Ti64 var _ /* iRowid2 at bp+16 */ Ti64 var _ /* out at bp+24 */ TFts5Buffer _, _ = iOut, p2 *(*int32)(unsafe.Pointer(bp)) = 0 *(*int32)(unsafe.Pointer(bp + 4)) = 0 *(*Ti64)(unsafe.Pointer(bp + 8)) = 0 *(*Ti64)(unsafe.Pointer(bp + 16)) = 0 iOut = 0 p2 = aBuf _ = nBuf libc.Xmemset(tls, bp+24, 0, uint32(12)) _ = libc.Int32FromInt32(0) _sqlite3Fts5BufferSize(tls, p+44, bp+24, uint32((*TFts5Buffer)(unsafe.Pointer(p1)).Fn+(*TFts5Buffer)(unsafe.Pointer(p2)).Fn)) if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 { return } _fts5NextRowid(tls, p1, bp, bp+8) _fts5NextRowid(tls, p2, bp+4, bp+16) for *(*int32)(unsafe.Pointer(bp)) >= 0 || *(*int32)(unsafe.Pointer(bp + 4)) >= 0 { if *(*int32)(unsafe.Pointer(bp)) >= 0 && (*(*int32)(unsafe.Pointer(bp + 4)) < 0 || *(*Ti64)(unsafe.Pointer(bp + 8)) < *(*Ti64)(unsafe.Pointer(bp + 16))) { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp + 24 + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp+24)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+24)).Fn), uint64(*(*Ti64)(unsafe.Pointer(bp + 8))-iOut)) _ = libc.Int32FromInt32(0) iOut = *(*Ti64)(unsafe.Pointer(bp + 8)) _fts5NextRowid(tls, p1, bp, bp+8) } else { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp + 24 + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp+24)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+24)).Fn), uint64(*(*Ti64)(unsafe.Pointer(bp + 16))-iOut)) _ = libc.Int32FromInt32(0) iOut = *(*Ti64)(unsafe.Pointer(bp + 16)) if *(*int32)(unsafe.Pointer(bp)) >= 0 && *(*Ti64)(unsafe.Pointer(bp + 8)) == *(*Ti64)(unsafe.Pointer(bp + 16)) { _fts5NextRowid(tls, p1, bp, bp+8) } _fts5NextRowid(tls, p2, bp+4, bp+16) } } _fts5BufferSwap(tls, bp+24, p1) _sqlite3Fts5BufferFree(tls, bp+24) } type TPrefixMerger = struct { F__ccgo_align [0]uint32 Fiter TFts5DoclistIter FiPos Ti64 FiOff int32 FaPos uintptr FpNext uintptr F__ccgo_pad5 [4]byte } type PrefixMerger = TPrefixMerger type TPrefixMerger1 = struct { F__ccgo_align [0]uint32 Fiter TFts5DoclistIter FiPos Ti64 FiOff int32 FaPos uintptr FpNext uintptr F__ccgo_pad5 [4]byte } type PrefixMerger1 = TPrefixMerger1 func _fts5PrefixMergerInsertByRowid(tls *libc.TLS, ppHead uintptr, p uintptr) { var pp uintptr _ = pp if (*TPrefixMerger)(unsafe.Pointer(p)).Fiter.FaPoslist != 0 { pp = ppHead for *(*uintptr)(unsafe.Pointer(pp)) != 0 && (*TPrefixMerger)(unsafe.Pointer(p)).Fiter.FiRowid > (*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).Fiter.FiRowid { pp = *(*uintptr)(unsafe.Pointer(pp)) + 48 } (*TPrefixMerger)(unsafe.Pointer(p)).FpNext = *(*uintptr)(unsafe.Pointer(pp)) *(*uintptr)(unsafe.Pointer(pp)) = p } } func _fts5PrefixMergerInsertByPosition(tls *libc.TLS, ppHead uintptr, p uintptr) { var pp uintptr _ = pp if (*TPrefixMerger)(unsafe.Pointer(p)).FiPos >= 0 { pp = ppHead for *(*uintptr)(unsafe.Pointer(pp)) != 0 && (*TPrefixMerger)(unsafe.Pointer(p)).FiPos > (*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FiPos { pp = *(*uintptr)(unsafe.Pointer(pp)) + 48 } (*TPrefixMerger)(unsafe.Pointer(p)).FpNext = *(*uintptr)(unsafe.Pointer(pp)) *(*uintptr)(unsafe.Pointer(pp)) = p } } // C documentation // // /* // ** Array aBuf[] contains nBuf doclists. These are all merged in with the // ** doclist in buffer p1. // */ func _fts5MergePrefixLists(tls *libc.TLS, p uintptr, p1 uintptr, nBuf int32, aBuf uintptr) { bp := tls.Alloc(944) defer tls.Free(944) var i, nMerge, nOut, nTail, nTmp int32 var iLastRowid Ti64 var pI, pNext, pSave, pThis, pThis1, pX uintptr var _ /* aMerger at bp+0 */ [16]TPrefixMerger var _ /* iPrev at bp+928 */ Ti64 var _ /* out at bp+900 */ TFts5Buffer var _ /* pHead at bp+896 */ uintptr var _ /* tmp at bp+912 */ TFts5Buffer _, _, _, _, _, _, _, _, _, _, _, _ = i, iLastRowid, nMerge, nOut, nTail, nTmp, pI, pNext, pSave, pThis, pThis1, pX *(*uintptr)(unsafe.Pointer(bp + 896)) = uintptr(0) nOut = 0 *(*TFts5Buffer)(unsafe.Pointer(bp + 900)) = TFts5Buffer{} *(*TFts5Buffer)(unsafe.Pointer(bp + 912)) = TFts5Buffer{} iLastRowid = 0 /* Initialize a doclist-iterator for each input buffer. Arrange them in ** a linked-list starting at pHead in ascending order of rowid. Avoid ** linking any iterators already at EOF into the linked list at all. */ _ = libc.Int32FromInt32(0) libc.Xmemset(tls, bp, 0, uint32(56)*uint32(nBuf+libc.Int32FromInt32(1))) *(*uintptr)(unsafe.Pointer(bp + 896)) = bp + uintptr(nBuf)*56 _fts5DoclistIterInit(tls, p1, *(*uintptr)(unsafe.Pointer(bp + 896))) i = 0 for { if !(i < nBuf) { break } _fts5DoclistIterInit(tls, aBuf+uintptr(i)*12, bp+uintptr(i)*56) _fts5PrefixMergerInsertByRowid(tls, bp+896, bp+uintptr(i)*56) nOut += (*(*TFts5Buffer)(unsafe.Pointer(aBuf + uintptr(i)*12))).Fn goto _1 _1: ; i++ } if nOut == 0 { return } nOut += (*TFts5Buffer)(unsafe.Pointer(p1)).Fn + int32(9) + int32(10)*nBuf /* The maximum size of the output is equal to the sum of the ** input sizes + 1 varint (9 bytes). The extra varint is because if the ** first rowid in one input is a large negative number, and the first in ** the other a non-negative number, the delta for the non-negative ** number will be larger on disk than the literal integer value ** was. ** ** Or, if the input position-lists are corrupt, then the output might ** include up to (nBuf+1) extra 10-byte positions created by interpreting -1 ** (the value PoslistNext64() uses for EOF) as a position and appending ** it to the output. This can happen at most once for each input ** position-list, hence (nBuf+1) 10 byte paddings. */ if _sqlite3Fts5BufferSize(tls, p+44, bp+900, uint32(nOut)) != 0 { return } for *(*uintptr)(unsafe.Pointer(bp + 896)) != 0 { _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(bp + 900 + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp+900)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+900)).Fn), uint64((*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 896)))).Fiter.FiRowid)-uint64(iLastRowid)) _ = libc.Int32FromInt32(0) iLastRowid = (*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 896)))).Fiter.FiRowid if (*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 896)))).FpNext != 0 && iLastRowid == (*TPrefixMerger)(unsafe.Pointer((*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 896)))).FpNext)).Fiter.FiRowid { /* Merge data from two or more poslists */ *(*Ti64)(unsafe.Pointer(bp + 928)) = 0 nTmp = int32(FTS5_DATA_ZERO_PADDING) nMerge = 0 pSave = *(*uintptr)(unsafe.Pointer(bp + 896)) pThis = uintptr(0) nTail = 0 *(*uintptr)(unsafe.Pointer(bp + 896)) = uintptr(0) for pSave != 0 && (*TPrefixMerger)(unsafe.Pointer(pSave)).Fiter.FiRowid == iLastRowid { pNext = (*TPrefixMerger)(unsafe.Pointer(pSave)).FpNext (*TPrefixMerger)(unsafe.Pointer(pSave)).FiOff = 0 (*TPrefixMerger)(unsafe.Pointer(pSave)).FiPos = 0 (*TPrefixMerger)(unsafe.Pointer(pSave)).FaPos = (*TPrefixMerger)(unsafe.Pointer(pSave)).Fiter.FaPoslist + uintptr((*TPrefixMerger)(unsafe.Pointer(pSave)).Fiter.FnSize) _sqlite3Fts5PoslistNext64(tls, (*TPrefixMerger)(unsafe.Pointer(pSave)).FaPos, (*TPrefixMerger)(unsafe.Pointer(pSave)).Fiter.FnPoslist, pSave+40, pSave+32) nTmp += (*TPrefixMerger)(unsafe.Pointer(pSave)).Fiter.FnPoslist + int32(10) nMerge++ _fts5PrefixMergerInsertByPosition(tls, bp+896, pSave) pSave = pNext } if *(*uintptr)(unsafe.Pointer(bp + 896)) == uintptr(0) || (*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 896)))).FpNext == uintptr(0) { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< nTmp-int32(FTS5_DATA_ZERO_PADDING) { if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< 0 { _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(bp+900)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+900)).Fn), (*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 896)))).FaPos+uintptr((*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 896)))).FiOff), uint32(nTail)) *(*int32)(unsafe.Pointer(bp + 900 + 4)) += nTail } *(*uintptr)(unsafe.Pointer(bp + 896)) = pSave i = 0 for { if !(i < nBuf+int32(1)) { break } pX = bp + uintptr(i)*56 if (*TPrefixMerger)(unsafe.Pointer(pX)).Fiter.FaPoslist != 0 && (*TPrefixMerger)(unsafe.Pointer(pX)).Fiter.FiRowid == iLastRowid { _fts5DoclistIterNext(tls, pX) _fts5PrefixMergerInsertByRowid(tls, bp+896, pX) } goto _2 _2: ; i++ } } else { /* Copy poslist from pHead to output */ pThis1 = *(*uintptr)(unsafe.Pointer(bp + 896)) pI = pThis1 _ = libc.Int32FromInt32(0) libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(bp+900)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+900)).Fn), (*TFts5DoclistIter)(unsafe.Pointer(pI)).FaPoslist, uint32((*TFts5DoclistIter)(unsafe.Pointer(pI)).FnPoslist+(*TFts5DoclistIter)(unsafe.Pointer(pI)).FnSize)) *(*int32)(unsafe.Pointer(bp + 900 + 4)) += (*TFts5DoclistIter)(unsafe.Pointer(pI)).FnPoslist + (*TFts5DoclistIter)(unsafe.Pointer(pI)).FnSize _fts5DoclistIterNext(tls, pI) *(*uintptr)(unsafe.Pointer(bp + 896)) = (*TPrefixMerger)(unsafe.Pointer(pThis1)).FpNext _fts5PrefixMergerInsertByRowid(tls, bp+896, pThis1) } } _sqlite3Fts5BufferFree(tls, p1) _sqlite3Fts5BufferFree(tls, bp+912) libc.Xmemset(tls, (*(*TFts5Buffer)(unsafe.Pointer(bp + 900))).Fp+uintptr((*(*TFts5Buffer)(unsafe.Pointer(bp + 900))).Fn), 0, uint32(FTS5_DATA_ZERO_PADDING)) *(*TFts5Buffer)(unsafe.Pointer(p1)) = *(*TFts5Buffer)(unsafe.Pointer(bp + 900)) } func _fts5SetupPrefixIter(tls *libc.TLS, p uintptr, bDesc int32, iIdx int32, pToken uintptr, nToken int32, pColset uintptr, ppIter uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var aBuf, pData, pSeg, pSeg1, pStruct, pTerm, xAppend, xMerge uintptr var f2, flags, i, i1, iFree, iStore, nBuf, nMerge, nTerm, v8 int32 var iLastRowid Ti64 var _ /* bNewTerm at bp+16 */ int32 var _ /* doclist at bp+4 */ TFts5Buffer var _ /* dummy at bp+20 */ int32 var _ /* p1 at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aBuf, f2, flags, i, i1, iFree, iLastRowid, iStore, nBuf, nMerge, nTerm, pData, pSeg, pSeg1, pStruct, pTerm, xAppend, xMerge, v8 nBuf = int32(32) nMerge = int32(1) if (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail == int32(FTS5_DETAIL_NONE) { xMerge = __ccgo_fp(_fts5MergeRowidLists) xAppend = __ccgo_fp(_fts5AppendRowid) } else { nMerge = libc.Int32FromInt32(FTS5_MERGE_NLIST) - libc.Int32FromInt32(1) nBuf = nMerge * int32(8) /* Sufficient to merge (16^8)==(2^32) lists */ xMerge = __ccgo_fp(_fts5MergePrefixLists) xAppend = __ccgo_fp(_fts5AppendPoslist) } aBuf = _fts5IdxMalloc(tls, p, int64(uint32(12)*uint32(nBuf))) pStruct = _fts5StructureRead(tls, p) _ = libc.Int32FromInt32(0) if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { flags = libc.Int32FromInt32(FTS5INDEX_QUERY_SCAN) | libc.Int32FromInt32(FTS5INDEX_QUERY_SKIPEMPTY) | libc.Int32FromInt32(FTS5INDEX_QUERY_NOOUTPUT) iLastRowid = 0 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 16)) = int32(1) libc.Xmemset(tls, bp+4, 0, uint32(12)) /* If iIdx is non-zero, then it is the number of a prefix-index for ** prefixes 1 character longer than the prefix being queried for. That ** index contains all the doclists required, except for the one ** corresponding to the prefix itself. That one is extracted from the ** main term index here. */ if iIdx != 0 { *(*int32)(unsafe.Pointer(bp + 20)) = 0 f2 = libc.Int32FromInt32(FTS5INDEX_QUERY_SKIPEMPTY) | libc.Int32FromInt32(FTS5INDEX_QUERY_NOOUTPUT) *(*Tu8)(unsafe.Pointer(pToken)) = uint8('0') _fts5MultiIterNew(tls, p, pStruct, f2, pColset, pToken, nToken, -int32(1), 0, bp) _fts5IterSetOutputCb(tls, p+44, *(*uintptr)(unsafe.Pointer(bp))) for { if !(_fts5MultiIterEof(tls, p, *(*uintptr)(unsafe.Pointer(bp))) == 0) { break } pSeg = *(*uintptr)(unsafe.Pointer(bp)) + 80 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaFirst + 1*4))).FiFirst)*104 (*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FxSetOutputs})))(tls, *(*uintptr)(unsafe.Pointer(bp)), pSeg) if (*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fbase.FnData != 0 { (*(*func(*libc.TLS, uintptr, Tu64, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{xAppend})))(tls, p, uint64((*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fbase.FiRowid)-uint64(iLastRowid), *(*uintptr)(unsafe.Pointer(bp)), bp+4) iLastRowid = (*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fbase.FiRowid } goto _1 _1: ; _fts5MultiIterNext2(tls, p, *(*uintptr)(unsafe.Pointer(bp)), bp+20) } _fts5MultiIterFree(tls, *(*uintptr)(unsafe.Pointer(bp))) } *(*Tu8)(unsafe.Pointer(pToken)) = uint8(int32('0') + iIdx) _fts5MultiIterNew(tls, p, pStruct, flags, pColset, pToken, nToken, -int32(1), 0, bp) _fts5IterSetOutputCb(tls, p+44, *(*uintptr)(unsafe.Pointer(bp))) for { if !(_fts5MultiIterEof(tls, p, *(*uintptr)(unsafe.Pointer(bp))) == 0) { break } pSeg1 = *(*uintptr)(unsafe.Pointer(bp)) + 80 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaFirst + 1*4))).FiFirst)*104 nTerm = (*TFts5SegIter)(unsafe.Pointer(pSeg1)).Fterm.Fn pTerm = (*TFts5SegIter)(unsafe.Pointer(pSeg1)).Fterm.Fp (*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FxSetOutputs})))(tls, *(*uintptr)(unsafe.Pointer(bp)), pSeg1) _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp + 16)) != 0 { if nTerm < nToken || libc.Xmemcmp(tls, pToken, pTerm, uint32(nToken)) != 0 { break } } if (*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fbase.FnData == 0 { goto _2 } if (*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fbase.FiRowid <= iLastRowid && (*(*TFts5Buffer)(unsafe.Pointer(bp + 4))).Fn > 0 { i = 0 for { if !((*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*(*TFts5Buffer)(unsafe.Pointer(bp + 4))).Fn != 0) { break } i1 = i * nMerge _ = libc.Int32FromInt32(0) iStore = i1 for { if !(iStore < i1+nMerge) { break } if (*(*TFts5Buffer)(unsafe.Pointer(aBuf + uintptr(iStore)*12))).Fn == 0 { _fts5BufferSwap(tls, bp+4, aBuf+uintptr(iStore)*12) _sqlite3Fts5BufferZero(tls, bp+4) break } goto _4 _4: ; iStore++ } if iStore == i1+nMerge { (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{xMerge})))(tls, p, bp+4, nMerge, aBuf+uintptr(i1)*12) iStore = i1 for { if !(iStore < i1+nMerge) { break } _sqlite3Fts5BufferZero(tls, aBuf+uintptr(iStore)*12) goto _5 _5: ; iStore++ } } goto _3 _3: ; i++ } iLastRowid = 0 } (*(*func(*libc.TLS, uintptr, Tu64, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{xAppend})))(tls, p, uint64((*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fbase.FiRowid)-uint64(iLastRowid), *(*uintptr)(unsafe.Pointer(bp)), bp+4) iLastRowid = (*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fbase.FiRowid goto _2 _2: ; _fts5MultiIterNext2(tls, p, *(*uintptr)(unsafe.Pointer(bp)), bp+16) } _ = libc.Int32FromInt32(0) i = 0 for { if !(i < nBuf) { break } if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{xMerge})))(tls, p, bp+4, nMerge, aBuf+uintptr(i)*12) } iFree = i for { if !(iFree < i+nMerge) { break } _sqlite3Fts5BufferFree(tls, aBuf+uintptr(iFree)*12) goto _7 _7: ; iFree++ } goto _6 _6: ; i += nMerge } _fts5MultiIterFree(tls, *(*uintptr)(unsafe.Pointer(bp))) pData = _fts5IdxMalloc(tls, p, int64(uint32(12)+uint32((*(*TFts5Buffer)(unsafe.Pointer(bp + 4))).Fn)+uint32(FTS5_DATA_ZERO_PADDING))) if pData != 0 { (*TFts5Data)(unsafe.Pointer(pData)).Fp = pData + 1*12 v8 = (*(*TFts5Buffer)(unsafe.Pointer(bp + 4))).Fn (*TFts5Data)(unsafe.Pointer(pData)).FszLeaf = v8 (*TFts5Data)(unsafe.Pointer(pData)).Fnn = v8 if (*(*TFts5Buffer)(unsafe.Pointer(bp + 4))).Fn != 0 { libc.Xmemcpy(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp, (*(*TFts5Buffer)(unsafe.Pointer(bp + 4))).Fp, uint32((*(*TFts5Buffer)(unsafe.Pointer(bp + 4))).Fn)) } _fts5MultiIterNew2(tls, p, pData, bDesc, ppIter) } _sqlite3Fts5BufferFree(tls, bp+4) } _fts5StructureRelease(tls, pStruct) Xsqlite3_free(tls, aBuf) } // C documentation // // /* // ** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain // ** to the document with rowid iRowid. // */ func _sqlite3Fts5IndexBeginWrite(tls *libc.TLS, p uintptr, bDelete int32, iRowid Ti64) (r int32) { _ = libc.Int32FromInt32(0) /* Allocate the hash table if it has not already been allocated */ if (*TFts5Index)(unsafe.Pointer(p)).FpHash == uintptr(0) { (*TFts5Index)(unsafe.Pointer(p)).Frc = _sqlite3Fts5HashNew(tls, (*TFts5Index)(unsafe.Pointer(p)).FpConfig, p+12, p+16) } /* Flush the hash table to disk if required */ if iRowid < (*TFts5Index)(unsafe.Pointer(p)).FiWriteRowid || iRowid == (*TFts5Index)(unsafe.Pointer(p)).FiWriteRowid && (*TFts5Index)(unsafe.Pointer(p)).FbDelete == 0 || (*TFts5Index)(unsafe.Pointer(p)).FnPendingData > (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FnHashSize { _fts5IndexFlush(tls, p) } (*TFts5Index)(unsafe.Pointer(p)).FiWriteRowid = iRowid (*TFts5Index)(unsafe.Pointer(p)).FbDelete = bDelete if bDelete == 0 { (*TFts5Index)(unsafe.Pointer(p)).FnPendingRow++ } return _fts5IndexReturn(tls, p) } // C documentation // // /* // ** Commit data to disk. // */ func _sqlite3Fts5IndexSync(tls *libc.TLS, p uintptr) (r int32) { _ = libc.Int32FromInt32(0) _fts5IndexFlush(tls, p) _sqlite3Fts5IndexCloseReader(tls, p) return _fts5IndexReturn(tls, p) } // C documentation // // /* // ** Discard any data stored in the in-memory hash tables. Do not write it // ** to the database. Additionally, assume that the contents of the %_data // ** table may have changed on disk. So any in-memory caches of %_data // ** records must be invalidated. // */ func _sqlite3Fts5IndexRollback(tls *libc.TLS, p uintptr) (r int32) { _sqlite3Fts5IndexCloseReader(tls, p) _fts5IndexDiscardData(tls, p) _fts5StructureInvalidate(tls, p) /* assert( p->rc==SQLITE_OK ); */ return SQLITE_OK } // C documentation // // /* // ** The %_data table is completely empty when this function is called. This // ** function populates it with the initial structure objects for each index, // ** and the initial version of the "averages" record (a zero-byte blob). // */ func _sqlite3Fts5IndexReinit(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* s at bp+0 */ TFts5Structure _fts5StructureInvalidate(tls, p) _fts5IndexDiscardData(tls, p) libc.Xmemset(tls, bp, 0, uint32(48)) if (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FbContentlessDelete != 0 { (*(*TFts5Structure)(unsafe.Pointer(bp))).FnOriginCntr = uint64(1) } _fts5DataWrite(tls, p, int64(FTS5_AVERAGES_ROWID), __ccgo_ts+1648, 0) _fts5StructureWrite(tls, p, bp) return _fts5IndexReturn(tls, p) } // C documentation // // /* // ** Open a new Fts5Index handle. If the bCreate argument is true, create // ** and initialize the underlying %_data table. // ** // ** If successful, set *pp to point to the new object and return SQLITE_OK. // ** Otherwise, set *pp to NULL and return an SQLite error code. // */ func _sqlite3Fts5IndexOpen(tls *libc.TLS, pConfig uintptr, bCreate int32, pp uintptr, pzErr uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var p, v1 uintptr var _ /* rc at bp+0 */ int32 _, _ = p, v1 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK /* New object */ v1 = _sqlite3Fts5MallocZero(tls, bp, int64(112)) p = v1 *(*uintptr)(unsafe.Pointer(pp)) = v1 if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { (*TFts5Index)(unsafe.Pointer(p)).FpConfig = pConfig (*TFts5Index)(unsafe.Pointer(p)).FnWorkUnit = int32(FTS5_WORK_UNIT) (*TFts5Index)(unsafe.Pointer(p)).FzDataTbl = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+36624, libc.VaList(bp+16, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName)) if (*TFts5Index)(unsafe.Pointer(p)).FzDataTbl != 0 && bCreate != 0 { *(*int32)(unsafe.Pointer(bp)) = _sqlite3Fts5CreateTable(tls, pConfig, __ccgo_ts+25741, __ccgo_ts+36632, 0, pzErr) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sqlite3Fts5CreateTable(tls, pConfig, __ccgo_ts+11793, __ccgo_ts+36667, int32(1), pzErr) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sqlite3Fts5IndexReinit(tls, p) } } } _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp)) != 0 { _sqlite3Fts5IndexClose(tls, p) *(*uintptr)(unsafe.Pointer(pp)) = uintptr(0) } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Close a handle opened by an earlier call to sqlite3Fts5IndexOpen(). // */ func _sqlite3Fts5IndexClose(tls *libc.TLS, p uintptr) (r int32) { var rc int32 _ = rc rc = SQLITE_OK if p != 0 { _ = libc.Int32FromInt32(0) _fts5StructureInvalidate(tls, p) Xsqlite3_finalize(tls, (*TFts5Index)(unsafe.Pointer(p)).FpWriter) Xsqlite3_finalize(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDeleter) Xsqlite3_finalize(tls, (*TFts5Index)(unsafe.Pointer(p)).FpIdxWriter) Xsqlite3_finalize(tls, (*TFts5Index)(unsafe.Pointer(p)).FpIdxDeleter) Xsqlite3_finalize(tls, (*TFts5Index)(unsafe.Pointer(p)).FpIdxSelect) Xsqlite3_finalize(tls, (*TFts5Index)(unsafe.Pointer(p)).FpIdxNextSelect) Xsqlite3_finalize(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDataVersion) Xsqlite3_finalize(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDeleteFromIdx) _sqlite3Fts5HashFree(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash) Xsqlite3_free(tls, (*TFts5Index)(unsafe.Pointer(p)).FzDataTbl) Xsqlite3_free(tls, p) } return rc } // C documentation // // /* // ** Argument p points to a buffer containing utf-8 text that is n bytes in // ** size. Return the number of bytes in the nChar character prefix of the // ** buffer, or 0 if there are less than nChar characters in total. // */ func _sqlite3Fts5IndexCharlenToBytelen(tls *libc.TLS, p uintptr, nByte int32, nChar int32) (r int32) { var i, n, v2 int32 _, _, _ = i, n, v2 n = 0 i = 0 for { if !(i < nChar) { break } if n >= nByte { return 0 } /* Input contains fewer than nChar chars */ v2 = n n++ if int32(*(*uint8)(unsafe.Pointer(p + uintptr(v2)))) >= int32(0xc0) { if n >= nByte { return 0 } for int32(*(*uint8)(unsafe.Pointer(p + uintptr(n))))&int32(0xc0) == int32(0x80) { n++ if n >= nByte { if i+int32(1) == nChar { break } return 0 } } } goto _1 _1: ; i++ } return n } // C documentation // // /* // ** pIn is a UTF-8 encoded string, nIn bytes in size. Return the number of // ** unicode characters in the string. // */ func _fts5IndexCharlen(tls *libc.TLS, pIn uintptr, nIn int32) (r int32) { var i, nChar, v1 int32 _, _, _ = i, nChar, v1 nChar = 0 i = 0 for i < nIn { v1 = i i++ if int32(*(*uint8)(unsafe.Pointer(pIn + uintptr(v1)))) >= int32(0xc0) { for i < nIn && int32(*(*uint8)(unsafe.Pointer(pIn + uintptr(i))))&int32(0xc0) == int32(0x80) { i++ } } nChar++ } return nChar } // C documentation // // /* // ** Insert or remove data to or from the index. Each time a document is // ** added to or removed from the index, this function is called one or more // ** times. // ** // ** For an insert, it must be called once for each token in the new document. // ** If the operation is a delete, it must be called (at least) once for each // ** unique token in the document with an iCol value less than zero. The iPos // ** argument is ignored for a delete. // */ func _sqlite3Fts5IndexWrite(tls *libc.TLS, p uintptr, iCol int32, iPos int32, pToken uintptr, nToken int32) (r int32) { var i, nByte, nChar, rc int32 var pConfig uintptr _, _, _, _, _ = i, nByte, nChar, pConfig, rc /* Used to iterate through indexes */ rc = SQLITE_OK /* Return code */ pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* Add the entry to the main terms index. */ rc = _sqlite3Fts5HashWrite(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, (*TFts5Index)(unsafe.Pointer(p)).FiWriteRowid, iCol, iPos, uint8('0'), pToken, nToken) i = 0 for { if !(i < (*TFts5Config)(unsafe.Pointer(pConfig)).FnPrefix && rc == SQLITE_OK) { break } nChar = *(*int32)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(pConfig)).FaPrefix + uintptr(i)*4)) nByte = _sqlite3Fts5IndexCharlenToBytelen(tls, pToken, nToken, nChar) if nByte != 0 { rc = _sqlite3Fts5HashWrite(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, (*TFts5Index)(unsafe.Pointer(p)).FiWriteRowid, iCol, iPos, uint8(libc.Int32FromUint8('0')+i+libc.Int32FromInt32(1)), pToken, nByte) } goto _1 _1: ; i++ } return rc } // C documentation // // /* // ** pToken points to a buffer of size nToken bytes containing a search // ** term, including the index number at the start, used on a tokendata=1 // ** table. This function returns true if the term in buffer pBuf matches // ** token pToken/nToken. // */ func _fts5IsTokendataPrefix(tls *libc.TLS, pBuf uintptr, pToken uintptr, nToken int32) (r int32) { return libc.BoolInt32((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn >= nToken && 0 == libc.Xmemcmp(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp, pToken, uint32(nToken)) && ((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn == nToken || int32(*(*Tu8)(unsafe.Pointer((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp + uintptr(nToken)))) == 0x00)) } // C documentation // // /* // ** Ensure the segment-iterator passed as the only argument points to EOF. // */ func _fts5SegIterSetEOF(tls *libc.TLS, pSeg uintptr) { _fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf) (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf = uintptr(0) } /* ** Usually, a tokendata=1 iterator (struct Fts5TokenDataIter) accumulates an ** array of these for each row it visits. Or, for an iterator used by an ** "ORDER BY rank" query, it accumulates an array of these for the entire ** query. ** ** Each instance in the array indicates the iterator (and therefore term) ** associated with position iPos of rowid iRowid. This is used by the ** xInstToken() API. */ type TFts5TokenDataMap1 = struct { F__ccgo_align [0]uint32 FiRowid Ti64 FiPos Ti64 FiIter int32 F__ccgo_pad3 [4]byte } type Fts5TokenDataMap1 = TFts5TokenDataMap1 /* ** An object used to supplement Fts5Iter for tokendata=1 iterators. */ type TFts5TokenDataIter1 = struct { FnIter int32 FnIterAlloc int32 FnMap int32 FnMapAlloc int32 FaMap uintptr FaPoslistReader uintptr FaPoslistToIter uintptr FapIter [1]uintptr } type Fts5TokenDataIter1 = TFts5TokenDataIter1 // C documentation // // /* // ** This function appends iterator pAppend to Fts5TokenDataIter pIn and // ** returns the result. // */ func _fts5AppendTokendataIter(tls *libc.TLS, p uintptr, pIn uintptr, pAppend uintptr) (r uintptr) { var nAlloc, nByte, v1, v2 int32 var pNew, pRet, v3 uintptr _, _, _, _, _, _, _ = nAlloc, nByte, pNew, pRet, v1, v2, v3 pRet = pIn if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { if pIn == uintptr(0) || (*TFts5TokenDataIter)(unsafe.Pointer(pIn)).FnIter == (*TFts5TokenDataIter)(unsafe.Pointer(pIn)).FnIterAlloc { if pIn != 0 { v1 = (*TFts5TokenDataIter)(unsafe.Pointer(pIn)).FnIterAlloc * int32(2) } else { v1 = int32(16) } nAlloc = v1 nByte = int32(uint32(nAlloc)*uint32(4) + uint32(32)) pNew = Xsqlite3_realloc(tls, pIn, nByte) if pNew == uintptr(0) { (*TFts5Index)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) } else { if pIn == uintptr(0) { libc.Xmemset(tls, pNew, 0, uint32(nByte)) } pRet = pNew (*TFts5TokenDataIter)(unsafe.Pointer(pNew)).FnIterAlloc = nAlloc } } } if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 { _sqlite3Fts5IterClose(tls, pAppend) } else { v3 = pRet v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ *(*uintptr)(unsafe.Pointer(pRet + 28 + uintptr(v2)*4)) = pAppend } _ = libc.Int32FromInt32(0) return pRet } // C documentation // // /* // ** Delete an Fts5TokenDataIter structure and its contents. // */ func _fts5TokendataIterDelete(tls *libc.TLS, pSet uintptr) { var ii int32 _ = ii if pSet != 0 { ii = 0 for { if !(ii < (*TFts5TokenDataIter)(unsafe.Pointer(pSet)).FnIter) { break } _fts5MultiIterFree(tls, *(*uintptr)(unsafe.Pointer(pSet + 28 + uintptr(ii)*4))) goto _1 _1: ; ii++ } Xsqlite3_free(tls, (*TFts5TokenDataIter)(unsafe.Pointer(pSet)).FaPoslistReader) Xsqlite3_free(tls, (*TFts5TokenDataIter)(unsafe.Pointer(pSet)).FaMap) Xsqlite3_free(tls, pSet) } } // C documentation // // /* // ** Append a mapping to the token-map belonging to object pT. // */ func _fts5TokendataIterAppendMap(tls *libc.TLS, p uintptr, pT uintptr, iIter int32, iRowid Ti64, iPos Ti64) { var aNew uintptr var nByte, nNew, v1 int32 _, _, _, _ = aNew, nByte, nNew, v1 if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { if (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap == (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMapAlloc { if (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMapAlloc != 0 { v1 = (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMapAlloc * int32(2) } else { v1 = int32(64) } nNew = v1 nByte = int32(uint32(nNew) * uint32(24)) aNew = Xsqlite3_realloc(tls, (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap, nByte) if aNew == uintptr(0) { (*TFts5Index)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) return } (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap = aNew (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMapAlloc = nNew } (*(*TFts5TokenDataMap)(unsafe.Pointer((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap)*24))).FiRowid = iRowid (*(*TFts5TokenDataMap)(unsafe.Pointer((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap)*24))).FiPos = iPos (*(*TFts5TokenDataMap)(unsafe.Pointer((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap)*24))).FiIter = iIter (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap++ } } // C documentation // // /* // ** The iterator passed as the only argument must be a tokendata=1 iterator // ** (pIter->pTokenDataIter!=0). This function sets the iterator output // ** variables (pIter->base.*) according to the contents of the current // ** row. // */ func _fts5IterSetOutputsTokendata(tls *libc.TLS, pIter uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var aNew, p, p1, pReader, pT uintptr var eDetail, iMin, ii, nByte, nHit, nNew, nReader, v3, v4 int32 var iMinPos, iRowid Ti64 var _ /* iPrev at bp+0 */ Ti64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aNew, eDetail, iMin, iMinPos, iRowid, ii, nByte, nHit, nNew, nReader, p, p1, pReader, pT, v3, v4 nHit = 0 iRowid = int64(-libc.Int32FromInt32(1)) - (libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)< int32(1) && eDetail != int32(FTS5_DETAIL_NONE) { nReader = 0 nByte = 0 *(*Ti64)(unsafe.Pointer(bp)) = 0 /* Allocate array of iterators if they are not already allocated. */ if (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistReader == uintptr(0) { (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistReader = _sqlite3Fts5MallocZero(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex+44, int64(uint32((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter)*(libc.Uint32FromInt64(24)+libc.Uint32FromInt64(4)))) if (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistReader == uintptr(0) { return } (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistToIter = (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistReader + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter)*24 } /* Populate an iterator for each poslist that will be merged */ ii = 0 for { if !(ii < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter) { break } p1 = *(*uintptr)(unsafe.Pointer(pT + 28 + uintptr(ii)*4)) if iRowid == (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FiRowid { *(*int32)(unsafe.Pointer((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistToIter + uintptr(nReader)*4)) = ii v3 = nReader nReader++ _sqlite3Fts5PoslistReaderInit(tls, (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FpData, (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FnData, (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistReader+uintptr(v3)*24) nByte += (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FnData } goto _2 _2: ; ii++ } /* Ensure the output buffer is large enough */ if uint32((*TFts5Buffer)(unsafe.Pointer(pIter+32)).Fn)+uint32(nByte+nHit*libc.Int32FromInt32(10)) <= uint32((*TFts5Buffer)(unsafe.Pointer(pIter+32)).FnSpace) { v4 = 0 } else { v4 = _sqlite3Fts5BufferSize(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex+44, pIter+32, uint32(nByte+nHit*int32(10)+(*TFts5Buffer)(unsafe.Pointer(pIter+32)).Fn)) } if v4 != 0 { return } /* Ensure the token-mapping is large enough */ if eDetail == FTS5_DETAIL_FULL && (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMapAlloc < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap+nByte { nNew = ((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMapAlloc + nByte) * int32(2) aNew = Xsqlite3_realloc(tls, (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap, int32(uint32(nNew)*uint32(24))) if aNew == uintptr(0) { (*TFts5Index)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex)).Frc = int32(SQLITE_NOMEM) return } (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap = aNew (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMapAlloc = nNew } (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fn = 0 for int32(1) != 0 { iMinPos = libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)<pTokenDataIter!=0). This function advances the iterator. If // ** argument bFrom is false, then the iterator is advanced to the next // ** entry. Or, if bFrom is true, it is advanced to the first entry with // ** a rowid of iFrom or greater. // */ func _fts5TokendataIterNext(tls *libc.TLS, pIter uintptr, bFrom int32, iFrom Ti64) { var ii int32 var p, pIndex, pT uintptr _, _, _, _ = ii, p, pIndex, pT pT = (*TFts5Iter)(unsafe.Pointer(pIter)).FpTokenDataIter pIndex = (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex ii = 0 for { if !(ii < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter) { break } p = *(*uintptr)(unsafe.Pointer(pT + 28 + uintptr(ii)*4)) if int32((*TFts5Iter)(unsafe.Pointer(p)).Fbase.FbEof) == 0 && ((*TFts5Iter)(unsafe.Pointer(p)).Fbase.FiRowid == (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FiRowid || bFrom != 0 && (*TFts5Iter)(unsafe.Pointer(p)).Fbase.FiRowid < iFrom) { _fts5MultiIterNext(tls, pIndex, p, bFrom, iFrom) for bFrom != 0 && int32((*TFts5Iter)(unsafe.Pointer(p)).Fbase.FbEof) == 0 && (*TFts5Iter)(unsafe.Pointer(p)).Fbase.FiRowid < iFrom && (*TFts5Index)(unsafe.Pointer(pIndex)).Frc == SQLITE_OK { _fts5MultiIterNext(tls, pIndex, p, 0, 0) } } goto _1 _1: ; ii++ } if (*TFts5Index)(unsafe.Pointer(pIndex)).Frc == SQLITE_OK { _fts5IterSetOutputsTokendata(tls, pIter) } } // C documentation // // /* // ** If the segment-iterator passed as the first argument is at EOF, then // ** set pIter->term to a copy of buffer pTerm. // */ func _fts5TokendataSetTermIfEof(tls *libc.TLS, pIter uintptr, pTerm uintptr) { if pIter != 0 && (*(*TFts5SegIter)(unsafe.Pointer(pIter + 80))).FpLeaf == uintptr(0) { _sqlite3Fts5BufferSet(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex+44, pIter+80+72, (*TFts5Buffer)(unsafe.Pointer(pTerm)).Fn, (*TFts5Buffer)(unsafe.Pointer(pTerm)).Fp) } } // C documentation // // /* // ** This function sets up an iterator to use for a non-prefix query on a // ** tokendata=1 table. // */ func _fts5SetupTokendataIter(tls *libc.TLS, p uintptr, pToken uintptr, nToken int32, pColset uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var bDone, flags, iLvl, iSeg, iSeg1, ii, ii1 int32 var pII, pIter, pNew, pNewIter, pPrev, pPrevIter, pRet, pSeg, pSet, pSmall, pStruct, v1, v2 uintptr var _ /* bSeek at bp+0 */ TFts5Buffer _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bDone, flags, iLvl, iSeg, iSeg1, ii, ii1, pII, pIter, pNew, pNewIter, pPrev, pPrevIter, pRet, pSeg, pSet, pSmall, pStruct, v1, v2 pRet = uintptr(0) pSet = uintptr(0) pStruct = uintptr(0) flags = libc.Int32FromInt32(FTS5INDEX_QUERY_SCANONETERM) | libc.Int32FromInt32(FTS5INDEX_QUERY_SCAN) *(*TFts5Buffer)(unsafe.Pointer(bp)) = TFts5Buffer{} pSmall = uintptr(0) _fts5IndexFlush(tls, p) pStruct = _fts5StructureRead(tls, p) for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { if pSet != 0 { v1 = *(*uintptr)(unsafe.Pointer(pSet + 28 + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pSet)).FnIter-int32(1))*4)) } else { v1 = uintptr(0) } pPrev = v1 pNew = uintptr(0) pNewIter = uintptr(0) pPrevIter = uintptr(0) pNew = _fts5MultiIterAlloc(tls, p, (*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment) if pSmall != 0 { _sqlite3Fts5BufferSet(tls, p+44, bp, (*TFts5Buffer)(unsafe.Pointer(pSmall)).Fn, (*TFts5Buffer)(unsafe.Pointer(pSmall)).Fp) _sqlite3Fts5BufferAppendBlob(tls, p+44, bp, uint32(1), __ccgo_ts+36711) } else { _sqlite3Fts5BufferSet(tls, p+44, bp, nToken, pToken) } if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 { _sqlite3Fts5IterClose(tls, pNew) break } pNewIter = pNew + 80 if pPrev != 0 { v2 = pPrev + 80 } else { v2 = uintptr(0) } pPrevIter = v2 iLvl = 0 for { if !(iLvl < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) { break } iSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FnSeg - int32(1) for { if !(iSeg >= 0) { break } pSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FaSeg + uintptr(iSeg)*56 bDone = 0 if pPrevIter != 0 { if _fts5BufferCompare(tls, pSmall, pPrevIter+72) != 0 { libc.Xmemcpy(tls, pNewIter, pPrevIter, uint32(104)) libc.Xmemset(tls, pPrevIter, 0, uint32(104)) bDone = int32(1) } else { if (*TFts5SegIter)(unsafe.Pointer(pPrevIter)).FiEndofDoclist > (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pPrevIter)).FpLeaf)).FszLeaf { _fts5SegIterNextInit(tls, p, (*(*TFts5Buffer)(unsafe.Pointer(bp))).Fp, (*(*TFts5Buffer)(unsafe.Pointer(bp))).Fn-int32(1), pSeg, pNewIter) bDone = int32(1) } } } if bDone == 0 { _fts5SegIterSeekInit(tls, p, (*(*TFts5Buffer)(unsafe.Pointer(bp))).Fp, (*(*TFts5Buffer)(unsafe.Pointer(bp))).Fn, flags, pSeg, pNewIter) } if pPrevIter != 0 { if (*TFts5SegIter)(unsafe.Pointer(pPrevIter)).FpTombArray != 0 { (*TFts5SegIter)(unsafe.Pointer(pNewIter)).FpTombArray = (*TFts5SegIter)(unsafe.Pointer(pPrevIter)).FpTombArray (*TFts5TombstoneArray)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pNewIter)).FpTombArray)).FnRef++ } } else { _fts5SegIterAllocTombstone(tls, p, pNewIter) } pNewIter += 104 if pPrevIter != 0 { pPrevIter += 104 } if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 { break } goto _4 _4: ; iSeg-- } goto _3 _3: ; iLvl++ } _fts5TokendataSetTermIfEof(tls, pPrev, pSmall) (*TFts5Iter)(unsafe.Pointer(pNew)).FbSkipEmpty = uint8(1) (*TFts5Iter)(unsafe.Pointer(pNew)).FpColset = pColset _fts5IterSetOutputCb(tls, p+44, pNew) /* Loop through all segments in the new iterator. Find the smallest ** term that any segment-iterator points to. Iterator pNew will be ** used for this term. Also, set any iterator that points to a term that ** does not match pToken/nToken to point to EOF */ pSmall = uintptr(0) ii = 0 for { if !(ii < (*TFts5Iter)(unsafe.Pointer(pNew)).FnSeg) { break } pII = pNew + 80 + uintptr(ii)*104 if 0 == _fts5IsTokendataPrefix(tls, pII+72, pToken, nToken) { _fts5SegIterSetEOF(tls, pII) } if (*TFts5SegIter)(unsafe.Pointer(pII)).FpLeaf != 0 && (!(pSmall != 0) || _fts5BufferCompare(tls, pSmall, pII+72) > 0) { pSmall = pII + 72 } goto _5 _5: ; ii++ } /* If pSmall is still NULL at this point, then the new iterator does ** not point to any terms that match the query. So delete it and break ** out of the loop - all required iterators have been collected. */ if pSmall == uintptr(0) { _sqlite3Fts5IterClose(tls, pNew) break } /* Append this iterator to the set and continue. */ pSet = _fts5AppendTokendataIter(tls, p, pSet, pNew) } if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && pSet != 0 { ii1 = 0 for { if !(ii1 < (*TFts5TokenDataIter)(unsafe.Pointer(pSet)).FnIter) { break } pIter = *(*uintptr)(unsafe.Pointer(pSet + 28 + uintptr(ii1)*4)) iSeg1 = 0 for { if !(iSeg1 < (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg) { break } (*(*TFts5SegIter)(unsafe.Pointer(pIter + 80 + uintptr(iSeg1)*104))).Fflags |= int32(FTS5_SEGITER_ONETERM) goto _7 _7: ; iSeg1++ } _fts5MultiIterFinishSetup(tls, p, pIter) goto _6 _6: ; ii1++ } } if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { pRet = _fts5MultiIterAlloc(tls, p, 0) } if pRet != 0 { (*TFts5Iter)(unsafe.Pointer(pRet)).FpTokenDataIter = pSet if pSet != 0 { _fts5IterSetOutputsTokendata(tls, pRet) } else { (*TFts5Iter)(unsafe.Pointer(pRet)).Fbase.FbEof = uint8(1) } } else { _fts5TokendataIterDelete(tls, pSet) } _fts5StructureRelease(tls, pStruct) _sqlite3Fts5BufferFree(tls, bp) return pRet } // C documentation // // /* // ** Open a new iterator to iterate though all rowid that match the // ** specified token or token prefix. // */ func _sqlite3Fts5IndexQuery(tls *libc.TLS, p uintptr, pToken uintptr, nToken int32, flags int32, pColset uintptr, ppIter uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bDesc, bTokendata, iIdx, iPrefixIdx, nChar, nIdxChar int32 var pConfig, pSeg, pStruct uintptr var _ /* buf at bp+4 */ TFts5Buffer var _ /* pRet at bp+0 */ uintptr _, _, _, _, _, _, _, _, _ = bDesc, bTokendata, iIdx, iPrefixIdx, nChar, nIdxChar, pConfig, pSeg, pStruct pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*TFts5Buffer)(unsafe.Pointer(bp + 4)) = TFts5Buffer{} /* If the QUERY_SCAN flag is set, all other flags must be clear. */ _ = libc.Int32FromInt32(0) if _sqlite3Fts5BufferSize(tls, p+44, bp+4, uint32(nToken+int32(1))) == 0 { iIdx = 0 /* Index to search */ iPrefixIdx = 0 /* +1 prefix index */ bTokendata = (*TFts5Config)(unsafe.Pointer(pConfig)).FbTokendata if nToken > 0 { libc.Xmemcpy(tls, (*(*TFts5Buffer)(unsafe.Pointer(bp + 4))).Fp+1, pToken, uint32(nToken)) } if flags&(libc.Int32FromInt32(FTS5INDEX_QUERY_NOTOKENDATA)|libc.Int32FromInt32(FTS5INDEX_QUERY_SCAN)) != 0 { bTokendata = 0 } /* Figure out which index to search and set iIdx accordingly. If this ** is a prefix query for which there is no prefix index, set iIdx to ** greater than pConfig->nPrefix to indicate that the query will be ** satisfied by scanning multiple terms in the main index. ** ** If the QUERY_TEST_NOIDX flag was specified, then this must be a ** prefix-query. Instead of using a prefix-index (if one exists), ** evaluate the prefix query using the main FTS index. This is used ** for internal sanity checking by the integrity-check in debug ** mode only. */ if flags&int32(FTS5INDEX_QUERY_PREFIX) != 0 { nChar = _fts5IndexCharlen(tls, pToken, nToken) iIdx = int32(1) for { if !(iIdx <= (*TFts5Config)(unsafe.Pointer(pConfig)).FnPrefix) { break } nIdxChar = *(*int32)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(pConfig)).FaPrefix + uintptr(iIdx-int32(1))*4)) if nIdxChar == nChar { break } if nIdxChar == nChar+int32(1) { iPrefixIdx = iIdx } goto _1 _1: ; iIdx++ } } if bTokendata != 0 && iIdx == 0 { *(*Tu8)(unsafe.Pointer((*(*TFts5Buffer)(unsafe.Pointer(bp + 4))).Fp)) = uint8('0') *(*uintptr)(unsafe.Pointer(bp)) = _fts5SetupTokendataIter(tls, p, (*(*TFts5Buffer)(unsafe.Pointer(bp + 4))).Fp, nToken+int32(1), pColset) } else { if iIdx <= (*TFts5Config)(unsafe.Pointer(pConfig)).FnPrefix { /* Straight index lookup */ pStruct = _fts5StructureRead(tls, p) *(*Tu8)(unsafe.Pointer((*(*TFts5Buffer)(unsafe.Pointer(bp + 4))).Fp)) = uint8(libc.Int32FromUint8('0') + iIdx) if pStruct != 0 { _fts5MultiIterNew(tls, p, pStruct, flags|int32(FTS5INDEX_QUERY_SKIPEMPTY), pColset, (*(*TFts5Buffer)(unsafe.Pointer(bp + 4))).Fp, nToken+int32(1), -int32(1), 0, bp) _fts5StructureRelease(tls, pStruct) } } else { /* Scan multiple terms in the main index */ bDesc = libc.BoolInt32(flags&int32(FTS5INDEX_QUERY_DESC) != 0) _fts5SetupPrefixIter(tls, p, bDesc, iPrefixIdx, (*(*TFts5Buffer)(unsafe.Pointer(bp + 4))).Fp, nToken+int32(1), pColset, bp) if *(*uintptr)(unsafe.Pointer(bp)) == uintptr(0) { _ = libc.Int32FromInt32(0) } else { _ = libc.Int32FromInt32(0) _fts5IterSetOutputCb(tls, p+44, *(*uintptr)(unsafe.Pointer(bp))) if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { pSeg = *(*uintptr)(unsafe.Pointer(bp)) + 80 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaFirst + 1*4))).FiFirst)*104 if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf != 0 { (*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FxSetOutputs})))(tls, *(*uintptr)(unsafe.Pointer(bp)), pSeg) } } } } } if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 { _sqlite3Fts5IterClose(tls, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) _sqlite3Fts5IndexCloseReader(tls, p) } *(*uintptr)(unsafe.Pointer(ppIter)) = *(*uintptr)(unsafe.Pointer(bp)) _sqlite3Fts5BufferFree(tls, bp+4) } return _fts5IndexReturn(tls, p) } // C documentation // // /* // ** Return true if the iterator passed as the only argument is at EOF. // */ // /* // ** Move to the next matching rowid. // */ func _sqlite3Fts5IterNext(tls *libc.TLS, pIndexIter uintptr) (r int32) { var pIter uintptr _ = pIter pIter = pIndexIter _ = libc.Int32FromInt32(0) if (*TFts5Iter)(unsafe.Pointer(pIter)).FpTokenDataIter != 0 { _fts5TokendataIterNext(tls, pIter, 0, 0) } else { _fts5MultiIterNext(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex, pIter, 0, 0) } return _fts5IndexReturn(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex) } // C documentation // // /* // ** Move to the next matching term/rowid. Used by the fts5vocab module. // */ func _sqlite3Fts5IterNextScan(tls *libc.TLS, pIndexIter uintptr) (r int32) { var p, pIter, pSeg uintptr _, _, _ = p, pIter, pSeg pIter = pIndexIter p = (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex _ = libc.Int32FromInt32(0) _fts5MultiIterNext(tls, p, pIter, 0, 0) if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { pSeg = pIter + 80 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)*104 if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf != 0 && int32(*(*Tu8)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).Fterm.Fp))) != int32('0') { _fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf) (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf = uintptr(0) (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FbEof = uint8(1) } } return _fts5IndexReturn(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex) } // C documentation // // /* // ** Move to the next matching rowid that occurs at or after iMatch. The // ** definition of "at or after" depends on whether this iterator iterates // ** in ascending or descending rowid order. // */ func _sqlite3Fts5IterNextFrom(tls *libc.TLS, pIndexIter uintptr, iMatch Ti64) (r int32) { var pIter uintptr _ = pIter pIter = pIndexIter if (*TFts5Iter)(unsafe.Pointer(pIter)).FpTokenDataIter != 0 { _fts5TokendataIterNext(tls, pIter, int32(1), iMatch) } else { _fts5MultiIterNextFrom(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex, pIter, iMatch) } return _fts5IndexReturn(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex) } // C documentation // // /* // ** Return the current term. // */ func _sqlite3Fts5IterTerm(tls *libc.TLS, pIndexIter uintptr, pn uintptr) (r uintptr) { bp := tls.Alloc(16) defer tls.Free(16) var z, v1 uintptr var _ /* n at bp+0 */ int32 _, _ = z, v1 z = _fts5MultiIterTerm(tls, pIndexIter, bp) _ = libc.Int32FromInt32(0) *(*int32)(unsafe.Pointer(pn)) = *(*int32)(unsafe.Pointer(bp)) - int32(1) if z != 0 { v1 = z + 1 } else { v1 = uintptr(0) } return v1 } // C documentation // // /* // ** This is used by xInstToken() to access the token at offset iOff, column // ** iCol of row iRowid. The token is returned via output variables *ppOut // ** and *pnOut. The iterator passed as the first argument must be a tokendata=1 // ** iterator (pIter->pTokenDataIter!=0). // */ func _sqlite3Fts5IterToken(tls *libc.TLS, pIndexIter uintptr, iRowid Ti64, iCol int32, iOff int32, ppOut uintptr, pnOut uintptr) (r int32) { var aMap, pIter, pMap, pT uintptr var i1, i2, iTest int32 var iPos Ti64 _, _, _, _, _, _, _, _ = aMap, i1, i2, iPos, iTest, pIter, pMap, pT pIter = pIndexIter pT = (*TFts5Iter)(unsafe.Pointer(pIter)).FpTokenDataIter aMap = (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap iPos = int64(iCol)< i1 { iTest = (i1 + i2) / int32(2) if (*(*TFts5TokenDataMap)(unsafe.Pointer(aMap + uintptr(iTest)*24))).FiRowid < iRowid { i1 = iTest + int32(1) } else { if (*(*TFts5TokenDataMap)(unsafe.Pointer(aMap + uintptr(iTest)*24))).FiRowid > iRowid { i2 = iTest } else { if (*(*TFts5TokenDataMap)(unsafe.Pointer(aMap + uintptr(iTest)*24))).FiPos < iPos { if (*(*TFts5TokenDataMap)(unsafe.Pointer(aMap + uintptr(iTest)*24))).FiPos < 0 { break } i1 = iTest + int32(1) } else { if (*(*TFts5TokenDataMap)(unsafe.Pointer(aMap + uintptr(iTest)*24))).FiPos > iPos { i2 = iTest } else { break } } } } } if i2 > i1 { pMap = *(*uintptr)(unsafe.Pointer(pT + 28 + uintptr((*(*TFts5TokenDataMap)(unsafe.Pointer(aMap + uintptr(iTest)*24))).FiIter)*4)) *(*uintptr)(unsafe.Pointer(ppOut)) = (*(*TFts5SegIter)(unsafe.Pointer(pMap + 80))).Fterm.Fp + uintptr(1) *(*int32)(unsafe.Pointer(pnOut)) = (*(*TFts5SegIter)(unsafe.Pointer(pMap + 80))).Fterm.Fn - int32(1) } return SQLITE_OK } // C documentation // // /* // ** Clear any existing entries from the token-map associated with the // ** iterator passed as the only argument. // */ func _sqlite3Fts5IndexIterClearTokendata(tls *libc.TLS, pIndexIter uintptr) { var pIter uintptr _ = pIter pIter = pIndexIter if pIter != 0 && (*TFts5Iter)(unsafe.Pointer(pIter)).FpTokenDataIter != 0 { (*TFts5TokenDataIter)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FpTokenDataIter)).FnMap = 0 } } // C documentation // // /* // ** Set a token-mapping for the iterator passed as the first argument. This // ** is used in detail=column or detail=none mode when a token is requested // ** using the xInstToken() API. In this case the caller tokenizers the // ** current row and configures the token-mapping via multiple calls to this // ** function. // */ func _sqlite3Fts5IndexIterWriteTokendata(tls *libc.TLS, pIndexIter uintptr, pToken uintptr, nToken int32, iRowid Ti64, iCol int32, iOff int32) (r int32) { var ii int32 var p, pIter, pT, pTerm uintptr _, _, _, _, _ = ii, p, pIter, pT, pTerm pIter = pIndexIter pT = (*TFts5Iter)(unsafe.Pointer(pIter)).FpTokenDataIter p = (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) ii = 0 for { if !(ii < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter) { break } pTerm = *(*uintptr)(unsafe.Pointer(pT + 28 + uintptr(ii)*4)) + 80 + 72 if nToken == (*TFts5Buffer)(unsafe.Pointer(pTerm)).Fn-int32(1) && libc.Xmemcmp(tls, pToken, (*TFts5Buffer)(unsafe.Pointer(pTerm)).Fp+uintptr(1), uint32(nToken)) == 0 { break } goto _1 _1: ; ii++ } if ii < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter { _fts5TokendataIterAppendMap(tls, p, pT, ii, iRowid, int64(iCol)< int32(16) { if int32(*(*Tu8)(unsafe.Pointer((*TFts5Data)(unsafe.Pointer(pPg)).Fp))) == int32(4) { v3 = int32(4) } else { v3 = int32(8) } v2 = ((*TFts5Data)(unsafe.Pointer(pPg)).Fnn - int32(8)) / v3 } else { v2 = int32(1) } nSlot = v2 nElem = int32(_fts5GetU32(tls, (*TFts5Data)(unsafe.Pointer(pPg)).Fp+4)) iSlot = int32(iRowid / uint64(nPg) % uint64(nSlot)) nCollide = nSlot if szKey == int32(4) && iRowid > uint64(0xFFFFFFFF) { return int32(2) } if iRowid == uint64(0) { *(*Tu8)(unsafe.Pointer((*TFts5Data)(unsafe.Pointer(pPg)).Fp + 1)) = uint8(0x01) return 0 } if bForce == 0 && nElem >= nSlot/int32(2) { return int32(1) } _fts5PutU32(tls, (*TFts5Data)(unsafe.Pointer(pPg)).Fp+4, uint32(nElem+int32(1))) if szKey == int32(4) { aSlot = (*TFts5Data)(unsafe.Pointer(pPg)).Fp + 8 for *(*Tu32)(unsafe.Pointer(aSlot + uintptr(iSlot)*4)) != 0 { iSlot = (iSlot + int32(1)) % nSlot v4 = nCollide nCollide-- if v4 == 0 { return 0 } } _fts5PutU32(tls, aSlot+uintptr(iSlot)*4, uint32(iRowid)) } else { aSlot1 = (*TFts5Data)(unsafe.Pointer(pPg)).Fp + 8 for *(*Tu64)(unsafe.Pointer(aSlot1 + uintptr(iSlot)*8)) != 0 { iSlot = (iSlot + int32(1)) % nSlot v5 = nCollide nCollide-- if v5 == 0 { return 0 } } _fts5PutU64(tls, aSlot1+uintptr(iSlot)*8, iRowid) } return 0 } // C documentation // // /* // ** This function attempts to build a new hash containing all the keys // ** currently in the tombstone hash table for segment pSeg. The new // ** hash will be stored in the nOut buffers passed in array apOut[]. // ** All pages of the new hash use key-size szKey (4 or 8). // ** // ** Return 0 if the hash is successfully rebuilt into the nOut pages. // ** Or non-zero if it is not (because one page became overfull). In this // ** case the caller should retry with a larger nOut parameter. // ** // ** Parameter pData1 is page iPg1 of the hash table being rebuilt. // */ func _fts5IndexTombstoneRehash(tls *libc.TLS, p uintptr, pSeg uintptr, pData1 uintptr, iPg1 int32, szKey int32, nOut int32, apOut uintptr) (r int32) { var aSlot, aSlot1, pData, pFree, pPg, v3 uintptr var iIn, ii, nSlotIn, res, szKeyIn, v4 int32 var iVal Tu64 _, _, _, _, _, _, _, _, _, _, _, _, _ = aSlot, aSlot1, iIn, iVal, ii, nSlotIn, pData, pFree, pPg, res, szKeyIn, v3, v4 res = 0 /* Initialize the headers of all the output pages */ ii = 0 for { if !(ii < nOut) { break } *(*Tu8)(unsafe.Pointer((*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(apOut + uintptr(ii)*4)))).Fp)) = uint8(szKey) _fts5PutU32(tls, (*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(apOut + uintptr(ii)*4)))).Fp+4, uint32(0)) goto _1 _1: ; ii++ } /* Loop through the current pages of the hash table. */ ii = 0 for { if !(res == 0 && ii < (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone) { break } pData = uintptr(0) /* Page ii of the current hash table */ pFree = uintptr(0) /* Free this at the end of the loop */ if iPg1 == ii { pData = pData1 } else { v3 = _fts5DataRead(tls, p, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid+libc.Int32FromInt32(1)< ((*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz-int32(8))/szKey { v1 = MINSLOT } else { v1 = ((*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz - int32(8)) / szKey } nSlotPerPage = v1 nSlot = 0 /* Number of slots in each output page */ nOut = 0 /* Figure out how many output pages (nOut) and how many slots per ** page (nSlot). There are three possibilities: ** ** 1. The hash table does not yet exist. In this case the new hash ** table will consist of a single page with MINSLOT slots. ** ** 2. The hash table exists but is currently a single page. In this ** case an attempt is made to grow the page to accommodate the new ** entry. The page is allowed to grow up to nSlotPerPage (see above) ** slots. ** ** 3. The hash table already consists of more than one page, or of ** a single page already so large that it cannot be grown. In this ** case the new hash consists of (nPg*2+1) pages of nSlotPerPage ** slots each, where nPg is the current number of pages in the ** hash table. */ if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone == 0 { /* Case 1. */ nOut = int32(1) nSlot = MINSLOT } else { if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone == int32(1) { /* Case 2. */ nElem = int32(_fts5GetU32(tls, (*TFts5Data)(unsafe.Pointer(pData1)).Fp+4)) _ = libc.Int32FromInt32(0) nOut = int32(1) if nElem*int32(4) > MINSLOT { v2 = nElem * int32(4) } else { v2 = MINSLOT } nSlot = v2 if nSlot > nSlotPerPage { nOut = 0 } } } if nOut == 0 { /* Case 3. */ nOut = (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone*int32(2) + int32(1) nSlot = nSlotPerPage } /* Allocate the required array and output pages */ for int32(1) != 0 { res = 0 ii = 0 szPage = 0 apOut = uintptr(0) /* Allocate space for the new hash table */ _ = libc.Int32FromInt32(0) apOut = _sqlite3Fts5MallocZero(tls, p+44, int64(uint32(4)*uint32(nOut))) szPage = int32(8) + nSlot*szKey ii = 0 for { if !(ii < nOut) { break } pNew = _sqlite3Fts5MallocZero(tls, p+44, int64(uint32(12)+uint32(szPage))) if pNew != 0 { (*TFts5Data)(unsafe.Pointer(pNew)).Fnn = szPage (*TFts5Data)(unsafe.Pointer(pNew)).Fp = pNew + 1*12 *(*uintptr)(unsafe.Pointer(apOut + uintptr(ii)*4)) = pNew } goto _3 _3: ; ii++ } /* Rebuild the hash table. */ if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { res = _fts5IndexTombstoneRehash(tls, p, pSeg, pData1, iPg1, szKey, nOut, apOut) } if res == 0 { if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 { _fts5IndexFreeArray(tls, apOut, nOut) apOut = uintptr(0) nOut = 0 } *(*int32)(unsafe.Pointer(pnOut)) = nOut *(*uintptr)(unsafe.Pointer(papOut)) = apOut break } /* If control flows to here, it was not possible to rebuild the hash ** table. Free all buffers and then try again with more pages. */ _ = libc.Int32FromInt32(0) _fts5IndexFreeArray(tls, apOut, nOut) nSlot = nSlotPerPage nOut = nOut*int32(2) + int32(1) } } // C documentation // // /* // ** Add a tombstone for rowid iRowid to segment pSeg. // */ func _fts5IndexTombstoneAdd(tls *libc.TLS, p uintptr, pSeg uintptr, iRowid Tu64) { bp := tls.Alloc(16) defer tls.Free(16) var iPg, ii, szKey, v1, v2 int32 var iTombstoneRowid Ti64 var pPg uintptr var _ /* apHash at bp+4 */ uintptr var _ /* nHash at bp+0 */ int32 _, _, _, _, _, _, _ = iPg, iTombstoneRowid, ii, pPg, szKey, v1, v2 pPg = uintptr(0) iPg = -int32(1) szKey = 0 *(*int32)(unsafe.Pointer(bp)) = 0 *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) (*TFts5Index)(unsafe.Pointer(p)).FnContentlessDelete++ if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone > 0 { iPg = int32(iRowid % uint64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone)) pPg = _fts5DataRead(tls, p, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid+libc.Int32FromInt32(1)< uint64(0xFFFFFFFF) { szKey = int32(8) } /* Rebuild the hash table */ _fts5IndexTombstoneRebuild(tls, p, pSeg, pPg, iPg, szKey, bp, bp+4) _ = libc.Int32FromInt32(0) /* If all has succeeded, write the new rowid into one of the new hash ** table pages, then write them all out to disk. */ if *(*int32)(unsafe.Pointer(bp)) != 0 { ii = 0 _fts5IndexTombstoneAddToPage(tls, *(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)) + uintptr(iRowid%uint64(*(*int32)(unsafe.Pointer(bp))))*4)), int32(1), *(*int32)(unsafe.Pointer(bp)), iRowid) ii = 0 for { if !(ii < *(*int32)(unsafe.Pointer(bp))) { break } iTombstoneRowid = int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid+libc.Int32FromInt32(1)<= 0) { break } iSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FnSeg - int32(1) for { if !(iSeg >= 0) { break } pSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FaSeg + uintptr(iSeg)*56 if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin1 <= uint64(iOrigin) && (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin2 >= uint64(iOrigin) { if bFound == 0 { (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnEntryTombstone++ bFound = int32(1) } _fts5IndexTombstoneAdd(tls, p, pSeg, uint64(iRowid)) } goto _2 _2: ; iSeg-- } goto _1 _1: ; iLvl-- } _fts5StructureRelease(tls, pStruct) } return _fts5IndexReturn(tls, p) } /************************************************************************* ************************************************************************** ** Below this point is the implementation of the integrity-check ** functionality. */ // C documentation // // /* // ** Return a simple checksum value based on the arguments. // */ func _sqlite3Fts5IndexEntryCksum(tls *libc.TLS, iRowid Ti64, iCol int32, iPos int32, iIdx int32, pTerm uintptr, nTerm int32) (r Tu64) { var i int32 var ret Tu64 _, _ = i, ret ret = uint64(iRowid) ret += ret<= 0 { ret += ret<= (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn) { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<= iNoRowid && 0 != int32(_fts5GetU16(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp)) { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<= int64((*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf) { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< int64((*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf) { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< (*(*TFts5Buffer)(unsafe.Pointer(bp))).Fn || iOff+int64(*(*int32)(unsafe.Pointer(bp + 36))) > int64((*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf) { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<= iOff || iOff >= (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<= (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< 0 { iDlRowid = _fts5DlidxIterRowid(tls, pDlidx) _sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr(iRowidOff1), bp+8) if *(*Ti64)(unsafe.Pointer(bp + 8)) < iDlRowid || bSecureDelete == 0 && *(*Ti64)(unsafe.Pointer(bp + 8)) != iDlRowid { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<> libc.Int32FromInt32(32)) iTokOff = int32(*(*Ti64)(unsafe.Pointer(bp + 24)) & libc.Int64FromInt32(0x7FFFFFFF)) cksum2 ^= _sqlite3Fts5IndexEntryCksum(tls, iRowid, iCol, iTokOff, -int32(1), z, *(*int32)(unsafe.Pointer(bp + 16))) } } goto _3 _3: ; _fts5MultiIterNext(tls, p, *(*uintptr)(unsafe.Pointer(bp + 12)), 0, 0) } _fts5MultiIterFree(tls, *(*uintptr)(unsafe.Pointer(bp + 12))) if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && bUseCksum != 0 && cksum != cksum2 { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< FROM ORDER BY +rank; ** ** aIdx[]: ** There is one entry in the aIdx[] array for each phrase in the query, ** the value of which is the offset within aPoslist[] following the last ** byte of the position list for the corresponding phrase. */ type TFts5Sorter1 = struct { F__ccgo_align [0]uint32 FpStmt uintptr F__ccgo_align1 [4]byte FiRowid Ti64 FaPoslist uintptr FnIdx int32 FaIdx [1]int32 F__ccgo_pad5 [4]byte } type Fts5Sorter1 = TFts5Sorter1 /* ** Virtual-table cursor object. ** ** iSpecial: ** If this is a 'special' query (refer to function fts5SpecialMatch()), ** then this variable contains the result of the query. ** ** iFirstRowid, iLastRowid: ** These variables are only used for FTS5_PLAN_MATCH cursors. Assuming the ** cursor iterates in ascending order of rowids, iFirstRowid is the lower ** limit of rowids to return, and iLastRowid the upper. In other words, the ** WHERE clause in the user's query might have been: ** ** MATCH AND rowid BETWEEN $iFirstRowid AND $iLastRowid ** ** If the cursor iterates in descending order of rowid, iFirstRowid ** is the upper limit (i.e. the "first" rowid visited) and iLastRowid ** the lower. */ type TFts5Cursor1 = struct { F__ccgo_align [0]uint32 Fbase Tsqlite3_vtab_cursor FpNext uintptr FaColumnSize uintptr F__ccgo_align3 [4]byte FiCsrId Ti64 FePlan int32 FbDesc int32 FiFirstRowid Ti64 FiLastRowid Ti64 FpStmt uintptr FpExpr uintptr FpSorter uintptr Fcsrflags int32 FiSpecial Ti64 FzRank uintptr FzRankArgs uintptr FpRank uintptr FnRankArg int32 FapRankArg uintptr FpRankArgStmt uintptr FpAux uintptr FpAuxdata uintptr FaInstIter uintptr FnInstAlloc int32 FnInstCount int32 FaInst uintptr } type Fts5Cursor1 = TFts5Cursor1 /* ** Bits that make up the "idxNum" parameter passed indirectly by ** xBestIndex() to xFilter(). */ /* ** Values for Fts5Cursor.csrflags */ /* ** Macros to Set(), Clear() and Test() cursor flags. */ type TFts5Auxdata1 = struct { FpAux uintptr FpPtr uintptr FxDelete uintptr FpNext uintptr } type Fts5Auxdata1 = TFts5Auxdata1 // C documentation // // /* // ** Return true if pTab is a contentless table. // */ func _fts5IsContentless(tls *libc.TLS, pTab uintptr) (r int32) { return libc.BoolInt32((*TFts5Config)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig)).FeContent == int32(FTS5_CONTENT_NONE)) } // C documentation // // /* // ** Delete a virtual table handle allocated by fts5InitVtab(). // */ func _fts5FreeVtab(tls *libc.TLS, pTab uintptr) { if pTab != 0 { _sqlite3Fts5IndexClose(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpIndex) _sqlite3Fts5StorageClose(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).FpStorage) _sqlite3Fts5ConfigFree(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig) Xsqlite3_free(tls, pTab) } } // C documentation // // /* // ** The xDisconnect() virtual table method. // */ func _fts5DisconnectMethod(tls *libc.TLS, pVtab uintptr) (r int32) { _fts5FreeVtab(tls, pVtab) return SQLITE_OK } // C documentation // // /* // ** The xDestroy() virtual table method. // */ func _fts5DestroyMethod(tls *libc.TLS, pVtab uintptr) (r int32) { var pTab uintptr var rc int32 _, _ = pTab, rc pTab = pVtab rc = _sqlite3Fts5DropAll(tls, (*TFts5Table)(unsafe.Pointer(pTab)).FpConfig) if rc == SQLITE_OK { _fts5FreeVtab(tls, pVtab) } return rc } // C documentation // // /* // ** This function is the implementation of both the xConnect and xCreate // ** methods of the FTS3 virtual table. // ** // ** The argv[] array contains the following: // ** // ** argv[0] -> module name ("fts5") // ** argv[1] -> database name // ** argv[2] -> table name // ** argv[...] -> "column name" and other module argument fields. // */ func _fts5InitVtab(tls *libc.TLS, bCreate int32, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVTab uintptr, pzErr uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var azConfig, pGlobal, pTab uintptr var _ /* pConfig at bp+4 */ uintptr var _ /* rc at bp+0 */ int32 _, _, _ = azConfig, pGlobal, pTab pGlobal = pAux azConfig = argv *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK /* Return code */ *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) /* Results of parsing argc/argv */ pTab = uintptr(0) /* New virtual table object */ /* Allocate the new vtab object and parse the configuration */ pTab = _sqlite3Fts5MallocZero(tls, bp, int64(36)) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sqlite3Fts5ConfigParse(tls, pGlobal, db, argc, azConfig, bp+4, pzErr) _ = libc.Int32FromInt32(0) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig = *(*uintptr)(unsafe.Pointer(bp + 4)) (*TFts5FullTable)(unsafe.Pointer(pTab)).FpGlobal = pGlobal } /* Open the index sub-system */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sqlite3Fts5IndexOpen(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), bCreate, pTab+16, pzErr) } /* Open the storage sub-system */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sqlite3Fts5StorageOpen(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpIndex, bCreate, pTab+20, pzErr) } /* Call sqlite3_declare_vtab() */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sqlite3Fts5ConfigDeclareVtab(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) } /* Load the initial configuration */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { _ = libc.Int32FromInt32(0) (*TFts5Config)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FpzErrmsg = pzErr *(*int32)(unsafe.Pointer(bp)) = _sqlite3Fts5IndexLoadConfig(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpIndex) _sqlite3Fts5IndexRollback(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpIndex) (*TFts5Config)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FpzErrmsg = uintptr(0) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 4)))).FeContent == FTS5_CONTENT_NORMAL { *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_CONSTRAINT_SUPPORT), libc.VaList(bp+16, libc.Int32FromInt32(1))) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_INNOCUOUS), 0) } if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { _fts5FreeVtab(tls, pTab) pTab = uintptr(0) } else { if bCreate != 0 { } } *(*uintptr)(unsafe.Pointer(ppVTab)) = pTab return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** The xConnect() and xCreate() methods for the virtual table. All the // ** work is done in function fts5InitVtab(). // */ func _fts5ConnectMethod(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) { return _fts5InitVtab(tls, 0, db, pAux, argc, argv, ppVtab, pzErr) } func _fts5CreateMethod(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) { return _fts5InitVtab(tls, int32(1), db, pAux, argc, argv, ppVtab, pzErr) } /* ** The different query plans. */ // C documentation // // /* // ** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this // ** extension is currently being used by a version of SQLite too old to // ** support index-info flags. In that case this function is a no-op. // */ func _fts5SetUniqueFlag(tls *libc.TLS, pIdxInfo uintptr) { *(*int32)(unsafe.Pointer(pIdxInfo + 56)) |= int32(SQLITE_INDEX_SCAN_UNIQUE) } func _fts5UsePatternMatch(tls *libc.TLS, pConfig uintptr, p uintptr) (r int32) { _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TFts5Config)(unsafe.Pointer(pConfig)).FePattern == int32(FTS5_PATTERN_GLOB) && int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(FTS5_PATTERN_GLOB) { return int32(1) } if (*TFts5Config)(unsafe.Pointer(pConfig)).FePattern == int32(FTS5_PATTERN_LIKE) && (int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(FTS5_PATTERN_LIKE) || int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(FTS5_PATTERN_GLOB)) { return int32(1) } return 0 } // C documentation // // /* // ** Implementation of the xBestIndex method for FTS5 tables. Within the // ** WHERE constraint, it searches for the following: // ** // ** 1. A MATCH constraint against the table column. // ** 2. A MATCH constraint against the "rank" column. // ** 3. A MATCH constraint against some other column. // ** 4. An == constraint against the rowid column. // ** 5. A < or <= constraint against the rowid column. // ** 6. A > or >= constraint against the rowid column. // ** // ** Within the ORDER BY, the following are supported: // ** // ** 5. ORDER BY rank [ASC|DESC] // ** 6. ORDER BY rowid [ASC|DESC] // ** // ** Information for the xFilter call is passed via both the idxNum and // ** idxStr variables. Specifically, idxNum is a bitmask of the following // ** flags used to encode the ORDER BY clause: // ** // ** FTS5_BI_ORDER_RANK // ** FTS5_BI_ORDER_ROWID // ** FTS5_BI_ORDER_DESC // ** // ** idxStr is used to encode data from the WHERE clause. For each argument // ** passed to the xFilter method, the following is appended to idxStr: // ** // ** Match against table column: "m" // ** Match against rank column: "r" // ** Match against other column: "M" // ** LIKE against other column: "L" // ** GLOB against other column: "G" // ** Equality constraint against the rowid: "=" // ** A < or <= against the rowid: "<" // ** A > or >= against the rowid: ">" // ** // ** This function ensures that there is at most one "r" or "=". And that if // ** there exists an "=" then there is no "<" or ">". // ** // ** Costs are assigned as follows: // ** // ** a) If an unusable MATCH operator is present in the WHERE clause, the // ** cost is unconditionally set to 1e50 (a really big number). // ** // ** a) If a MATCH operator is present, the cost depends on the other // ** constraints also present. As follows: // ** // ** * No other constraints: cost=1000.0 // ** * One rowid range constraint: cost=750.0 // ** * Both rowid range constraints: cost=500.0 // ** * An == rowid constraint: cost=100.0 // ** // ** b) Otherwise, if there is no MATCH: // ** // ** * No other constraints: cost=1000000.0 // ** * One rowid range constraint: cost=750000.0 // ** * Both rowid range constraints: cost=250000.0 // ** * An == rowid constraint: cost=10.0 // ** // ** Costs are not modified by the ORDER BY clause. // */ func _fts5BestIndexMethod(tls *libc.TLS, pVTab uintptr, pInfo uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var bSeenEq, bSeenGt, bSeenLt, bSeenMatch, bSeenRank, i, iCol, iCons, iIdxStr, iSort, idxFlags, nCol, op, v11, v12, v13, v14, v2, v3, v4, v5, v6, v7, v8, v9 int32 var idxStr, p, p1, pConfig, pTab uintptr var v15, v16, v17, v18 float64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bSeenEq, bSeenGt, bSeenLt, bSeenMatch, bSeenRank, i, iCol, iCons, iIdxStr, iSort, idxFlags, idxStr, nCol, op, p, p1, pConfig, pTab, v11, v12, v13, v14, v15, v16, v17, v18, v2, v3, v4, v5, v6, v7, v8, v9 pTab = pVTab pConfig = (*TFts5Table)(unsafe.Pointer(pTab)).FpConfig nCol = (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol idxFlags = 0 iIdxStr = 0 iCons = 0 bSeenEq = 0 bSeenGt = 0 bSeenLt = 0 bSeenMatch = 0 bSeenRank = 0 _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TFts5Config)(unsafe.Pointer(pConfig)).FbLock != 0 { (*TFts5Table)(unsafe.Pointer(pTab)).Fbase.FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+36804, 0) return int32(SQLITE_ERROR) } idxStr = Xsqlite3_malloc(tls, (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FnConstraint*int32(8)+int32(1)) if idxStr == uintptr(0) { return int32(SQLITE_NOMEM) } (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FidxStr = idxStr (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FneedToFreeIdxStr = int32(1) i = 0 for { if !(i < (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FnConstraint) { break } p = (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraint + uintptr(i)*12 iCol = (*Tsqlite3_index_constraint)(unsafe.Pointer(p)).FiColumn if int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INDEX_CONSTRAINT_MATCH) || int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INDEX_CONSTRAINT_EQ) && iCol >= nCol { /* A MATCH operator or equivalent */ if int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fusable) == 0 || iCol < 0 { /* As there exists an unusable MATCH constraint this is an ** unusable plan. Set a prohibitively high cost. */ (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FestimatedCost = float64(1e+50) _ = libc.Int32FromInt32(0) *(*uint8)(unsafe.Pointer(idxStr + uintptr(iIdxStr))) = uint8(0) return SQLITE_OK } else { if iCol == nCol+int32(1) { if bSeenRank != 0 { goto _1 } v2 = iIdxStr iIdxStr++ *(*uint8)(unsafe.Pointer(idxStr + uintptr(v2))) = uint8('r') bSeenRank = int32(1) } else { if iCol >= 0 { bSeenMatch = int32(1) v3 = iIdxStr iIdxStr++ *(*uint8)(unsafe.Pointer(idxStr + uintptr(v3))) = uint8('M') Xsqlite3_snprintf(tls, int32(6), idxStr+uintptr(iIdxStr), __ccgo_ts+5138, libc.VaList(bp+8, iCol)) idxStr += uintptr(libc.Xstrlen(tls, idxStr+uintptr(iIdxStr))) _ = libc.Int32FromInt32(0) } } iCons++ v4 = iCons (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraintUsage + uintptr(i)*8))).FargvIndex = v4 (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraintUsage + uintptr(i)*8))).Fomit = uint8(1) } } else { if (*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fusable != 0 { if iCol >= 0 && iCol < nCol && _fts5UsePatternMatch(tls, pConfig, p) != 0 { _ = libc.Int32FromInt32(0) v5 = iIdxStr iIdxStr++ if int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(FTS5_PATTERN_LIKE) { v6 = int32('L') } else { v6 = int32('G') } *(*uint8)(unsafe.Pointer(idxStr + uintptr(v5))) = uint8(v6) Xsqlite3_snprintf(tls, int32(6), idxStr+uintptr(iIdxStr), __ccgo_ts+5138, libc.VaList(bp+8, iCol)) idxStr += uintptr(libc.Xstrlen(tls, idxStr+uintptr(iIdxStr))) iCons++ v7 = iCons (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraintUsage + uintptr(i)*8))).FargvIndex = v7 _ = libc.Int32FromInt32(0) } else { if bSeenEq == 0 && int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INDEX_CONSTRAINT_EQ) && iCol < 0 { v8 = iIdxStr iIdxStr++ *(*uint8)(unsafe.Pointer(idxStr + uintptr(v8))) = uint8('=') bSeenEq = int32(1) iCons++ v9 = iCons (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraintUsage + uintptr(i)*8))).FargvIndex = v9 } } } } goto _1 _1: ; i++ } if bSeenEq == 0 { i = 0 for { if !(i < (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FnConstraint) { break } p1 = (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraint + uintptr(i)*12 if (*Tsqlite3_index_constraint)(unsafe.Pointer(p1)).FiColumn < 0 && (*Tsqlite3_index_constraint)(unsafe.Pointer(p1)).Fusable != 0 { op = int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p1)).Fop) if op == int32(SQLITE_INDEX_CONSTRAINT_LT) || op == int32(SQLITE_INDEX_CONSTRAINT_LE) { if bSeenLt != 0 { goto _10 } v11 = iIdxStr iIdxStr++ *(*uint8)(unsafe.Pointer(idxStr + uintptr(v11))) = uint8('<') iCons++ v12 = iCons (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraintUsage + uintptr(i)*8))).FargvIndex = v12 bSeenLt = int32(1) } else { if op == int32(SQLITE_INDEX_CONSTRAINT_GT) || op == int32(SQLITE_INDEX_CONSTRAINT_GE) { if bSeenGt != 0 { goto _10 } v13 = iIdxStr iIdxStr++ *(*uint8)(unsafe.Pointer(idxStr + uintptr(v13))) = uint8('>') iCons++ v14 = iCons (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraintUsage + uintptr(i)*8))).FargvIndex = v14 bSeenGt = int32(1) } } } goto _10 _10: ; i++ } } *(*uint8)(unsafe.Pointer(idxStr + uintptr(iIdxStr))) = uint8('\000') /* Set idxFlags flags for the ORDER BY clause ** ** Note that tokendata=1 tables cannot currently handle "ORDER BY rowid DESC". */ if (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FnOrderBy == int32(1) { iSort = (*(*Tsqlite3_index_orderby)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaOrderBy))).FiColumn if iSort == (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol+int32(1) && bSeenMatch != 0 { idxFlags |= int32(FTS5_BI_ORDER_RANK) } else { if iSort == -int32(1) && (!((*(*Tsqlite3_index_orderby)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaOrderBy))).Fdesc != 0) || !((*TFts5Config)(unsafe.Pointer(pConfig)).FbTokendata != 0)) { idxFlags |= int32(FTS5_BI_ORDER_ROWID) } } if idxFlags&(libc.Int32FromInt32(FTS5_BI_ORDER_RANK)|libc.Int32FromInt32(FTS5_BI_ORDER_ROWID)) != 0 { (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).ForderByConsumed = int32(1) if (*(*Tsqlite3_index_orderby)(unsafe.Pointer((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaOrderBy))).Fdesc != 0 { idxFlags |= int32(FTS5_BI_ORDER_DESC) } } } /* Calculate the estimated cost based on the flags set in idxFlags. */ if bSeenEq != 0 { if bSeenMatch != 0 { v15 = float64(100) } else { v15 = float64(10) } (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FestimatedCost = v15 if bSeenMatch == 0 { _fts5SetUniqueFlag(tls, pInfo) } } else { if bSeenLt != 0 && bSeenGt != 0 { if bSeenMatch != 0 { v16 = float64(500) } else { v16 = float64(250000) } (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FestimatedCost = v16 } else { if bSeenLt != 0 || bSeenGt != 0 { if bSeenMatch != 0 { v17 = float64(750) } else { v17 = float64(750000) } (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FestimatedCost = v17 } else { if bSeenMatch != 0 { v18 = float64(1000) } else { v18 = float64(1e+06) } (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FestimatedCost = v18 } } } (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FidxNum = idxFlags return SQLITE_OK } func _fts5NewTransaction(tls *libc.TLS, pTab uintptr) (r int32) { var pCsr uintptr _ = pCsr pCsr = (*TFts5Global)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpGlobal)).FpCsr for { if !(pCsr != 0) { break } if (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab == pTab { return SQLITE_OK } goto _1 _1: ; pCsr = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpNext } return _sqlite3Fts5StorageReset(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).FpStorage) } // C documentation // // /* // ** Implementation of xOpen method. // */ func _fts5OpenMethod(tls *libc.TLS, pVTab uintptr, ppCsr uintptr) (r int32) { var nByte Tsqlite3_int64 var pConfig, pCsr, pGlobal, pTab, v2 uintptr var rc int32 var v1 Ti64 _, _, _, _, _, _, _, _ = nByte, pConfig, pCsr, pGlobal, pTab, rc, v1, v2 pTab = pVTab pConfig = (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig pCsr = uintptr(0) /* Return code */ rc = _fts5NewTransaction(tls, pTab) if rc == SQLITE_OK { nByte = int64(uint32(120) + uint32((*TFts5Config)(unsafe.Pointer(pConfig)).FnCol)*uint32(4)) pCsr = Xsqlite3_malloc64(tls, uint64(nByte)) if pCsr != 0 { pGlobal = (*TFts5FullTable)(unsafe.Pointer(pTab)).FpGlobal libc.Xmemset(tls, pCsr, 0, uint32(nByte)) (*TFts5Cursor)(unsafe.Pointer(pCsr)).FaColumnSize = pCsr + 1*120 (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpNext = (*TFts5Global)(unsafe.Pointer(pGlobal)).FpCsr (*TFts5Global)(unsafe.Pointer(pGlobal)).FpCsr = pCsr v2 = pGlobal + 24 *(*Ti64)(unsafe.Pointer(v2))++ v1 = *(*Ti64)(unsafe.Pointer(v2)) (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiCsrId = v1 } else { rc = int32(SQLITE_NOMEM) } } *(*uintptr)(unsafe.Pointer(ppCsr)) = pCsr return rc } func _fts5StmtType(tls *libc.TLS, pCsr uintptr) (r int32) { var v1 int32 _ = v1 if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan == int32(FTS5_PLAN_SCAN) { if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FbDesc != 0 { v1 = int32(FTS5_STMT_SCAN_DESC) } else { v1 = FTS5_STMT_SCAN_ASC } return v1 } return int32(FTS5_STMT_LOOKUP) } // C documentation // // /* // ** This function is called after the cursor passed as the only argument // ** is moved to point at a different row. It clears all cached data // ** specific to the previous row stored by the cursor object. // */ func _fts5CsrNewrow(tls *libc.TLS, pCsr uintptr) { *(*int32)(unsafe.Pointer(pCsr + 60)) |= libc.Int32FromInt32(FTS5CSR_REQUIRE_CONTENT) | libc.Int32FromInt32(FTS5CSR_REQUIRE_DOCSIZE) | libc.Int32FromInt32(FTS5CSR_REQUIRE_INST) | libc.Int32FromInt32(FTS5CSR_REQUIRE_POSLIST) } func _fts5FreeCursorComponents(tls *libc.TLS, pCsr uintptr) { var eStmt int32 var pData, pNext, pSorter, pTab uintptr _, _, _, _, _ = eStmt, pData, pNext, pSorter, pTab pTab = (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab Xsqlite3_free(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FaInstIter) Xsqlite3_free(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FaInst) if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt != 0 { eStmt = _fts5StmtType(tls, pCsr) _sqlite3Fts5StorageStmtRelease(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).FpStorage, eStmt, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt) } if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpSorter != 0 { pSorter = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpSorter Xsqlite3_finalize(tls, (*TFts5Sorter)(unsafe.Pointer(pSorter)).FpStmt) Xsqlite3_free(tls, pSorter) } if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan != int32(FTS5_PLAN_SOURCE) { _sqlite3Fts5ExprFree(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr) } pData = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpAuxdata for { if !(pData != 0) { break } pNext = (*TFts5Auxdata)(unsafe.Pointer(pData)).FpNext if (*TFts5Auxdata)(unsafe.Pointer(pData)).FxDelete != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5Auxdata)(unsafe.Pointer(pData)).FxDelete})))(tls, (*TFts5Auxdata)(unsafe.Pointer(pData)).FpPtr) } Xsqlite3_free(tls, pData) goto _1 _1: ; pData = pNext } Xsqlite3_finalize(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpRankArgStmt) Xsqlite3_free(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FapRankArg) if (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fcsrflags&int32(FTS5CSR_FREE_ZRANK) != 0 { Xsqlite3_free(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRank) Xsqlite3_free(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRankArgs) } _sqlite3Fts5IndexCloseReader(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpIndex) libc.Xmemset(tls, pCsr+24, 0, uint32(120)-uint32(int32(pCsr+24)-int32(pCsr))) } // C documentation // // /* // ** Close the cursor. For additional information see the documentation // ** on the xClose method of the virtual table interface. // */ func _fts5CloseMethod(tls *libc.TLS, pCursor uintptr) (r int32) { var pCsr, pTab, pp uintptr _, _, _ = pCsr, pTab, pp if pCursor != 0 { pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab pCsr = pCursor _fts5FreeCursorComponents(tls, pCsr) /* Remove the cursor from the Fts5Global.pCsr list */ pp = (*TFts5FullTable)(unsafe.Pointer(pTab)).FpGlobal + 44 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != pCsr) { break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 4 } *(*uintptr)(unsafe.Pointer(pp)) = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpNext Xsqlite3_free(tls, pCsr) } return SQLITE_OK } func _fts5SorterNext(tls *libc.TLS, pCsr uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var a, aBlob, pSorter, v1 uintptr var i, iOff, nBlob, rc int32 var _ /* iVal at bp+0 */ int32 _, _, _, _, _, _, _, _ = a, aBlob, i, iOff, nBlob, pSorter, rc, v1 pSorter = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpSorter rc = Xsqlite3_step(tls, (*TFts5Sorter)(unsafe.Pointer(pSorter)).FpStmt) if rc == int32(SQLITE_DONE) { rc = SQLITE_OK *(*int32)(unsafe.Pointer(pCsr + 60)) |= libc.Int32FromInt32(FTS5CSR_EOF) | libc.Int32FromInt32(FTS5CSR_REQUIRE_CONTENT) } else { if rc == int32(SQLITE_ROW) { iOff = 0 rc = SQLITE_OK (*TFts5Sorter)(unsafe.Pointer(pSorter)).FiRowid = Xsqlite3_column_int64(tls, (*TFts5Sorter)(unsafe.Pointer(pSorter)).FpStmt, 0) nBlob = Xsqlite3_column_bytes(tls, (*TFts5Sorter)(unsafe.Pointer(pSorter)).FpStmt, int32(1)) v1 = Xsqlite3_column_blob(tls, (*TFts5Sorter)(unsafe.Pointer(pSorter)).FpStmt, int32(1)) a = v1 aBlob = v1 /* nBlob==0 in detail=none mode. */ if nBlob > 0 { i = 0 for { if !(i < (*TFts5Sorter)(unsafe.Pointer(pSorter)).FnIdx-int32(1)) { break } a += uintptr(_sqlite3Fts5GetVarint32(tls, a, bp)) iOff += *(*int32)(unsafe.Pointer(bp)) *(*int32)(unsafe.Pointer(pSorter + 24 + uintptr(i)*4)) = iOff goto _2 _2: ; i++ } *(*int32)(unsafe.Pointer(pSorter + 24 + uintptr(i)*4)) = t__predefined_ptrdiff_t(aBlob+uintptr(nBlob)) - int32(a) (*TFts5Sorter)(unsafe.Pointer(pSorter)).FaPoslist = a } _fts5CsrNewrow(tls, pCsr) } } return rc } // C documentation // // /* // ** Set the FTS5CSR_REQUIRE_RESEEK flag on all FTS5_PLAN_MATCH cursors // ** open on table pTab. // */ func _fts5TripCursors(tls *libc.TLS, pTab uintptr) { var pCsr uintptr _ = pCsr pCsr = (*TFts5Global)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpGlobal)).FpCsr for { if !(pCsr != 0) { break } if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan == int32(FTS5_PLAN_MATCH) && (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab == pTab { *(*int32)(unsafe.Pointer(pCsr + 60)) |= int32(FTS5CSR_REQUIRE_RESEEK) } goto _1 _1: ; pCsr = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpNext } } // C documentation // // /* // ** If the REQUIRE_RESEEK flag is set on the cursor passed as the first // ** argument, close and reopen all Fts5IndexIter iterators that the cursor // ** is using. Then attempt to move the cursor to a rowid equal to or laster // ** (in the cursors sort order - ASC or DESC) than the current rowid. // ** // ** If the new rowid is not equal to the old, set output parameter *pbSkip // ** to 1 before returning. Otherwise, leave it unchanged. // ** // ** Return SQLITE_OK if successful or if no reseek was required, or an // ** error code if an error occurred. // */ func _fts5CursorReseek(tls *libc.TLS, pCsr uintptr, pbSkip uintptr) (r int32) { var bDesc, rc int32 var iRowid Ti64 var pTab uintptr _, _, _, _ = bDesc, iRowid, pTab, rc rc = SQLITE_OK _ = libc.Int32FromInt32(0) if (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fcsrflags&int32(FTS5CSR_REQUIRE_RESEEK) != 0 { pTab = (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab bDesc = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FbDesc iRowid = _sqlite3Fts5ExprRowid(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr) rc = _sqlite3Fts5ExprFirst(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr, (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpIndex, iRowid, bDesc) if rc == SQLITE_OK && iRowid != _sqlite3Fts5ExprRowid(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr) { *(*int32)(unsafe.Pointer(pbSkip)) = int32(1) } *(*int32)(unsafe.Pointer(pCsr + 60)) &= ^libc.Int32FromInt32(FTS5CSR_REQUIRE_RESEEK) _fts5CsrNewrow(tls, pCsr) if _sqlite3Fts5ExprEof(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr) != 0 { *(*int32)(unsafe.Pointer(pCsr + 60)) |= int32(FTS5CSR_EOF) *(*int32)(unsafe.Pointer(pbSkip)) = int32(1) } } return rc } // C documentation // // /* // ** Advance the cursor to the next row in the table that matches the // ** search criteria. // ** // ** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned // ** even if we reach end-of-file. The fts5EofMethod() will be called // ** subsequently to determine whether or not an EOF was hit. // */ func _fts5NextMethod(tls *libc.TLS, pCursor uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var pConfig, pCsr uintptr var rc, v1 int32 var _ /* bSkip at bp+0 */ int32 _, _, _, _ = pConfig, pCsr, rc, v1 pCsr = pCursor _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) /* If this cursor uses FTS5_PLAN_MATCH and this is a tokendata=1 table, ** clear any token mappings accumulated at the fts5_index.c level. In ** other cases, specifically FTS5_PLAN_SOURCE and FTS5_PLAN_SORTED_MATCH, ** we need to retain the mappings for the entire query. */ if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan == int32(FTS5_PLAN_MATCH) && (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab)).FpConfig)).FbTokendata != 0 { _sqlite3Fts5ExprClearTokens(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr) } if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan < int32(3) { *(*int32)(unsafe.Pointer(bp)) = 0 v1 = _fts5CursorReseek(tls, pCsr, bp) rc = v1 if v1 != 0 || *(*int32)(unsafe.Pointer(bp)) != 0 { return rc } rc = _sqlite3Fts5ExprNext(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiLastRowid) *(*int32)(unsafe.Pointer(pCsr + 60)) |= _sqlite3Fts5ExprEof(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr) _fts5CsrNewrow(tls, pCsr) } else { switch (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan { case int32(FTS5_PLAN_SPECIAL): *(*int32)(unsafe.Pointer(pCsr + 60)) |= int32(FTS5CSR_EOF) rc = SQLITE_OK case int32(FTS5_PLAN_SORTED_MATCH): rc = _fts5SorterNext(tls, pCsr) default: pConfig = (*TFts5Table)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab)).FpConfig (*TFts5Config)(unsafe.Pointer(pConfig)).FbLock++ rc = Xsqlite3_step(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt) (*TFts5Config)(unsafe.Pointer(pConfig)).FbLock-- if rc != int32(SQLITE_ROW) { *(*int32)(unsafe.Pointer(pCsr + 60)) |= int32(FTS5CSR_EOF) rc = Xsqlite3_reset(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt) if rc != SQLITE_OK { (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab)).FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+16, Xsqlite3_errmsg(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb))) } } else { rc = SQLITE_OK } break } } return rc } func _fts5PrepareStatement(tls *libc.TLS, ppStmt uintptr, pConfig uintptr, zFmt uintptr, va uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var ap Tva_list var rc int32 var zSql uintptr var _ /* pRet at bp+0 */ uintptr _, _, _ = ap, rc, zSql *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) ap = va zSql = Xsqlite3_vmprintf(tls, zFmt, ap) if zSql == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { rc = Xsqlite3_prepare_v3(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb, zSql, -int32(1), uint32(SQLITE_PREPARE_PERSISTENT), bp, uintptr(0)) if rc != SQLITE_OK { *(*uintptr)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+3795, libc.VaList(bp+16, Xsqlite3_errmsg(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb))) } Xsqlite3_free(tls, zSql) } _ = ap *(*uintptr)(unsafe.Pointer(ppStmt)) = *(*uintptr)(unsafe.Pointer(bp)) return rc } func _fts5CursorFirstSorted(tls *libc.TLS, pTab uintptr, pCsr uintptr, bDesc int32) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) var nByte Tsqlite3_int64 var nPhrase, rc int32 var pConfig, pSorter, zRank, zRankArgs, v1, v2, v3 uintptr _, _, _, _, _, _, _, _, _, _ = nByte, nPhrase, pConfig, pSorter, rc, zRank, zRankArgs, v1, v2, v3 pConfig = (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig zRank = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRank zRankArgs = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRankArgs nPhrase = _sqlite3Fts5ExprPhraseCount(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr) nByte = int64(uint32(32) + uint32(4)*uint32(nPhrase-libc.Int32FromInt32(1))) pSorter = Xsqlite3_malloc64(tls, uint64(nByte)) if pSorter == uintptr(0) { return int32(SQLITE_NOMEM) } libc.Xmemset(tls, pSorter, 0, uint32(nByte)) (*TFts5Sorter)(unsafe.Pointer(pSorter)).FnIdx = nPhrase /* TODO: It would be better to have some system for reusing statement ** handles here, rather than preparing a new one for each query. But that ** is not possible as SQLite reference counts the virtual table objects. ** And since the statement required here reads from this very virtual ** table, saving it creates a circular reference. ** ** If SQLite a built-in statement cache, this wouldn't be a problem. */ if zRankArgs != 0 { v1 = __ccgo_ts + 14990 } else { v1 = __ccgo_ts + 1648 } if zRankArgs != 0 { v2 = zRankArgs } else { v2 = __ccgo_ts + 1648 } if bDesc != 0 { v3 = __ccgo_ts + 36843 } else { v3 = __ccgo_ts + 36848 } rc = _fts5PrepareStatement(tls, pSorter, pConfig, __ccgo_ts+36852, libc.VaList(bp+8, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName, zRank, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName, v1, v2, v3)) (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpSorter = pSorter if rc == SQLITE_OK { _ = libc.Int32FromInt32(0) (*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr = pCsr rc = _fts5SorterNext(tls, pCsr) (*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr = uintptr(0) } if rc != SQLITE_OK { Xsqlite3_finalize(tls, (*TFts5Sorter)(unsafe.Pointer(pSorter)).FpStmt) Xsqlite3_free(tls, pSorter) (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpSorter = uintptr(0) } return rc } func _fts5CursorFirst(tls *libc.TLS, pTab uintptr, pCsr uintptr, bDesc int32) (r int32) { var pExpr uintptr var rc int32 _, _ = pExpr, rc pExpr = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr rc = _sqlite3Fts5ExprFirst(tls, pExpr, (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpIndex, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiFirstRowid, bDesc) if _sqlite3Fts5ExprEof(tls, pExpr) != 0 { *(*int32)(unsafe.Pointer(pCsr + 60)) |= int32(FTS5CSR_EOF) } _fts5CsrNewrow(tls, pCsr) return rc } // C documentation // // /* // ** Process a "special" query. A special query is identified as one with a // ** MATCH expression that begins with a '*' character. The remainder of // ** the text passed to the MATCH operator are used as the special query // ** parameters. // */ func _fts5SpecialMatch(tls *libc.TLS, pTab uintptr, pCsr uintptr, zQuery uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var n, rc int32 var z uintptr _, _, _ = n, rc, z rc = SQLITE_OK /* Return code */ z = zQuery /* Number of bytes in text at z */ for int32(*(*uint8)(unsafe.Pointer(z))) == int32(' ') { z++ } n = 0 for { if !(*(*uint8)(unsafe.Pointer(z + uintptr(n))) != 0 && int32(*(*uint8)(unsafe.Pointer(z + uintptr(n)))) != int32(' ')) { break } goto _1 _1: ; n++ } _ = libc.Int32FromInt32(0) (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan = int32(FTS5_PLAN_SPECIAL) if n == int32(5) && 0 == Xsqlite3_strnicmp(tls, __ccgo_ts+36907, z, n) { (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiSpecial = int64(_sqlite3Fts5IndexReads(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpIndex)) } else { if n == int32(2) && 0 == Xsqlite3_strnicmp(tls, __ccgo_ts+5481, z, n) { (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiSpecial = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiCsrId } else { /* An unrecognized directive. Return an error message. */ (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.Fbase.FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+36913, libc.VaList(bp+8, n, z)) rc = int32(SQLITE_ERROR) } } return rc } // C documentation // // /* // ** Search for an auxiliary function named zName that can be used with table // ** pTab. If one is found, return a pointer to the corresponding Fts5Auxiliary // ** structure. Otherwise, if no such function exists, return NULL. // */ func _fts5FindAuxiliary(tls *libc.TLS, pTab uintptr, zName uintptr) (r uintptr) { var pAux uintptr _ = pAux pAux = (*TFts5Global)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpGlobal)).FpAux for { if !(pAux != 0) { break } if Xsqlite3_stricmp(tls, zName, (*TFts5Auxiliary)(unsafe.Pointer(pAux)).FzFunc) == 0 { return pAux } goto _1 _1: ; pAux = (*TFts5Auxiliary)(unsafe.Pointer(pAux)).FpNext } /* No function of the specified name was found. Return 0. */ return uintptr(0) } func _fts5FindRankFunction(tls *libc.TLS, pCsr uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var i int32 var nByte Tsqlite3_int64 var pAux, pConfig, pTab, zRank, zRankArgs, zSql uintptr var _ /* pStmt at bp+4 */ uintptr var _ /* rc at bp+0 */ int32 _, _, _, _, _, _, _, _ = i, nByte, pAux, pConfig, pTab, zRank, zRankArgs, zSql pTab = (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab pConfig = (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK pAux = uintptr(0) zRank = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRank zRankArgs = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRankArgs if zRankArgs != 0 { zSql = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+36941, libc.VaList(bp+16, zRankArgs)) if zSql != 0 { *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_prepare_v3(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb, zSql, -int32(1), uint32(SQLITE_PREPARE_PERSISTENT), bp+4, uintptr(0)) Xsqlite3_free(tls, zSql) _ = libc.Int32FromInt32(0) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { if int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) { (*TFts5Cursor)(unsafe.Pointer(pCsr)).FnRankArg = Xsqlite3_column_count(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) nByte = int64(uint32(4) * uint32((*TFts5Cursor)(unsafe.Pointer(pCsr)).FnRankArg)) (*TFts5Cursor)(unsafe.Pointer(pCsr)).FapRankArg = _sqlite3Fts5MallocZero(tls, bp, nByte) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { i = 0 for { if !(i < (*TFts5Cursor)(unsafe.Pointer(pCsr)).FnRankArg) { break } *(*uintptr)(unsafe.Pointer((*TFts5Cursor)(unsafe.Pointer(pCsr)).FapRankArg + uintptr(i)*4)) = Xsqlite3_column_value(tls, *(*uintptr)(unsafe.Pointer(bp + 4)), i) goto _1 _1: ; i++ } } (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpRankArgStmt = *(*uintptr)(unsafe.Pointer(bp + 4)) } else { *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp + 4))) _ = libc.Int32FromInt32(0) } } } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { pAux = _fts5FindAuxiliary(tls, pTab, zRank) if pAux == uintptr(0) { _ = libc.Int32FromInt32(0) (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.Fbase.FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+36951, libc.VaList(bp+16, zRank)) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } } (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpRank = pAux return *(*int32)(unsafe.Pointer(bp)) } func _fts5CursorParseRank(tls *libc.TLS, pConfig uintptr, pCsr uintptr, pRank uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var rc int32 var z uintptr var _ /* zRank at bp+0 */ uintptr var _ /* zRankArgs at bp+4 */ uintptr _, _ = rc, z rc = SQLITE_OK if pRank != 0 { z = Xsqlite3_value_text(tls, pRank) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 4)) = uintptr(0) if z == uintptr(0) { if Xsqlite3_value_type(tls, pRank) == int32(SQLITE_NULL) { rc = int32(SQLITE_ERROR) } } else { rc = _sqlite3Fts5ConfigParseRank(tls, z, bp, bp+4) } if rc == SQLITE_OK { (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRank = *(*uintptr)(unsafe.Pointer(bp)) (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRankArgs = *(*uintptr)(unsafe.Pointer(bp + 4)) *(*int32)(unsafe.Pointer(pCsr + 60)) |= int32(FTS5CSR_FREE_ZRANK) } else { if rc == int32(SQLITE_ERROR) { (*Tsqlite3_vtab)(unsafe.Pointer((*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab)).FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+36972, libc.VaList(bp+16, z)) } } } else { if (*TFts5Config)(unsafe.Pointer(pConfig)).FzRank != 0 { (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRank = (*TFts5Config)(unsafe.Pointer(pConfig)).FzRank (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRankArgs = (*TFts5Config)(unsafe.Pointer(pConfig)).FzRankArgs } else { (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRank = __ccgo_ts + 34904 (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRankArgs = uintptr(0) } } return rc } func _fts5GetRowidLimit(tls *libc.TLS, pVal uintptr, iDefault Ti64) (r Ti64) { var eType int32 _ = eType if pVal != 0 { eType = Xsqlite3_value_numeric_type(tls, pVal) if eType == int32(SQLITE_INTEGER) { return Xsqlite3_value_int64(tls, pVal) } } return iDefault } // C documentation // // /* // ** This is the xFilter interface for the virtual table. See // ** the virtual table xFilter method documentation for additional // ** information. // ** // ** There are three possible query strategies: // ** // ** 1. Full-text search using a MATCH operator. // ** 2. A by-rowid lookup. // ** 3. A full-table scan. // */ func _fts5FilterMethod(tls *libc.TLS, pCursor uintptr, idxNum int32, idxStr uintptr, nVal int32, apVal uintptr) (r int32) { bp := tls.Alloc(32) defer tls.Free(32) var bDesc, bGlob, bOrderByRank, i, iCol, iIdxStr, rc, v2, v3, v4, v5, v7 int32 var pConfig, pCsr, pRank, pRowidEq, pRowidGe, pRowidLe, pTab, pzErr, pzErrmsg, zText, zText1, v6 uintptr var _ /* pExpr at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bDesc, bGlob, bOrderByRank, i, iCol, iIdxStr, pConfig, pCsr, pRank, pRowidEq, pRowidGe, pRowidLe, pTab, pzErr, pzErrmsg, rc, zText, zText1, v2, v3, v4, v5, v6, v7 pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab pConfig = (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig pCsr = pCursor rc = SQLITE_OK /* True if ORDER BY rank */ pRank = uintptr(0) /* rank MATCH ? expression (or NULL) */ pRowidEq = uintptr(0) /* rowid = ? expression (or NULL) */ pRowidLe = uintptr(0) /* rowid <= ? expression (or NULL) */ pRowidGe = uintptr(0) /* Column on LHS of MATCH operator */ pzErrmsg = (*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg iIdxStr = 0 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) if (*TFts5Config)(unsafe.Pointer(pConfig)).FbLock != 0 { (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.Fbase.FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+36804, 0) return int32(SQLITE_ERROR) } if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan != 0 { _fts5FreeCursorComponents(tls, pCsr) libc.Xmemset(tls, pCsr+24, 0, uint32(120)-uint32(int32(pCsr+24)-int32(pCsr))) } _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) (*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg = pTab + 8 /* Decode the arguments passed through to this function. */ i = 0 for { if !(i < nVal) { break } v2 = iIdxStr iIdxStr++ switch int32(*(*uint8)(unsafe.Pointer(idxStr + uintptr(v2)))) { case int32('r'): pRank = *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*4)) case int32('M'): zText = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*4))) if zText == uintptr(0) { zText = __ccgo_ts + 1648 } iCol = 0 for cond := true; cond; cond = int32(*(*uint8)(unsafe.Pointer(idxStr + uintptr(iIdxStr)))) >= int32('0') && int32(*(*uint8)(unsafe.Pointer(idxStr + uintptr(iIdxStr)))) <= int32('9') { iCol = iCol*int32(10) + (int32(*(*uint8)(unsafe.Pointer(idxStr + uintptr(iIdxStr)))) - int32('0')) iIdxStr++ } if int32(*(*uint8)(unsafe.Pointer(zText))) == int32('*') { /* The user has issued a query of the form "MATCH '*...'". This ** indicates that the MATCH expression is not a full text query, ** but a request for an internal parameter. */ rc = _fts5SpecialMatch(tls, pTab, pCsr, zText+1) goto filter_out } else { pzErr = pTab + 8 rc = _sqlite3Fts5ExprNew(tls, pConfig, 0, iCol, zText, bp, pzErr) if rc == SQLITE_OK { rc = _sqlite3Fts5ExprAnd(tls, pCsr+52, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } if rc != SQLITE_OK { goto filter_out } } case int32('L'): fallthrough case int32('G'): bGlob = libc.BoolInt32(int32(*(*uint8)(unsafe.Pointer(idxStr + uintptr(iIdxStr-int32(1))))) == int32('G')) zText1 = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*4))) iCol = 0 for cond := true; cond; cond = int32(*(*uint8)(unsafe.Pointer(idxStr + uintptr(iIdxStr)))) >= int32('0') && int32(*(*uint8)(unsafe.Pointer(idxStr + uintptr(iIdxStr)))) <= int32('9') { iCol = iCol*int32(10) + (int32(*(*uint8)(unsafe.Pointer(idxStr + uintptr(iIdxStr)))) - int32('0')) iIdxStr++ } if zText1 != 0 { rc = _sqlite3Fts5ExprPattern(tls, pConfig, bGlob, iCol, zText1, bp) } if rc == SQLITE_OK { rc = _sqlite3Fts5ExprAnd(tls, pCsr+52, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) } if rc != SQLITE_OK { goto filter_out } case int32('='): pRowidEq = *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*4)) case int32('<'): pRowidLe = *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*4)) default: _ = libc.Int32FromInt32(0) pRowidGe = *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*4)) break } goto _1 _1: ; i++ } if idxNum&int32(FTS5_BI_ORDER_RANK) != 0 { v3 = int32(1) } else { v3 = 0 } bOrderByRank = v3 if idxNum&int32(FTS5_BI_ORDER_DESC) != 0 { v5 = int32(1) } else { v5 = 0 } v4 = v5 bDesc = v4 (*TFts5Cursor)(unsafe.Pointer(pCsr)).FbDesc = v4 /* Set the cursor upper and lower rowid limits. Only some strategies ** actually use them. This is ok, as the xBestIndex() method leaves the ** sqlite3_index_constraint.omit flag clear for range constraints ** on the rowid field. */ if pRowidEq != 0 { v6 = pRowidEq pRowidGe = v6 pRowidLe = v6 } if bDesc != 0 { (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiFirstRowid = _fts5GetRowidLimit(tls, pRowidLe, libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)< ORDER BY rank" query (ePlan is ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will ** return results to the user for this query. The current cursor ** (pCursor) is used to execute the query issued by function ** fts5CursorFirstSorted() above. */ _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) if (*TFts5Cursor)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr)).FbDesc != 0 { (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiLastRowid = (*TFts5Cursor)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr)).FiFirstRowid (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiFirstRowid = (*TFts5Cursor)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr)).FiLastRowid } else { (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiLastRowid = (*TFts5Cursor)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr)).FiLastRowid (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiFirstRowid = (*TFts5Cursor)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr)).FiFirstRowid } (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan = int32(FTS5_PLAN_SOURCE) (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr = (*TFts5Cursor)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr)).FpExpr rc = _fts5CursorFirst(tls, pTab, pCsr, bDesc) } else { if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr != 0 { _ = libc.Int32FromInt32(0) rc = _fts5CursorParseRank(tls, pConfig, pCsr, pRank) if rc == SQLITE_OK { if bOrderByRank != 0 { (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan = int32(FTS5_PLAN_SORTED_MATCH) rc = _fts5CursorFirstSorted(tls, pTab, pCsr, bDesc) } else { (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan = int32(FTS5_PLAN_MATCH) rc = _fts5CursorFirst(tls, pTab, pCsr, bDesc) } } } else { if (*TFts5Config)(unsafe.Pointer(pConfig)).FzContent == uintptr(0) { *(*uintptr)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+37005, libc.VaList(bp+16, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName)) rc = int32(SQLITE_ERROR) } else { /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup ** by rowid (ePlan==FTS5_PLAN_ROWID). */ if pRowidEq != 0 { v7 = int32(FTS5_PLAN_ROWID) } else { v7 = int32(FTS5_PLAN_SCAN) } (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan = v7 rc = _sqlite3Fts5StorageStmt(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).FpStorage, _fts5StmtType(tls, pCsr), pCsr+48, pTab+8) if rc == SQLITE_OK { if pRowidEq != uintptr(0) { _ = libc.Int32FromInt32(0) Xsqlite3_bind_value(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt, int32(1), pRowidEq) } else { Xsqlite3_bind_int64(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt, int32(1), (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiFirstRowid) Xsqlite3_bind_int64(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt, int32(2), (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiLastRowid) } rc = _fts5NextMethod(tls, pCursor) } } } } goto filter_out filter_out: ; _sqlite3Fts5ExprFree(tls, *(*uintptr)(unsafe.Pointer(bp))) (*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg = pzErrmsg return rc } // C documentation // // /* // ** This is the xEof method of the virtual table. SQLite calls this // ** routine to find out if it has reached the end of a result set. // */ func _fts5EofMethod(tls *libc.TLS, pCursor uintptr) (r int32) { var pCsr uintptr var v1 int32 _, _ = pCsr, v1 pCsr = pCursor if (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fcsrflags&int32(FTS5CSR_EOF) != 0 { v1 = int32(1) } else { v1 = 0 } return v1 } // C documentation // // /* // ** Return the rowid that the cursor currently points to. // */ func _fts5CursorRowid(tls *libc.TLS, pCsr uintptr) (r Ti64) { _ = libc.Int32FromInt32(0) if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpSorter != 0 { return (*TFts5Sorter)(unsafe.Pointer((*TFts5Cursor)(unsafe.Pointer(pCsr)).FpSorter)).FiRowid } else { return _sqlite3Fts5ExprRowid(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr) } return r } // C documentation // // /* // ** This is the xRowid method. The SQLite core calls this routine to // ** retrieve the rowid for the current row of the result set. fts5 // ** exposes %_content.rowid as the rowid for the virtual table. The // ** rowid should be written to *pRowid. // */ func _fts5RowidMethod(tls *libc.TLS, pCursor uintptr, pRowid uintptr) (r int32) { var ePlan int32 var pCsr uintptr _, _ = ePlan, pCsr pCsr = pCursor ePlan = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan _ = libc.Int32FromInt32(0) switch ePlan { case int32(FTS5_PLAN_SPECIAL): *(*Tsqlite_int64)(unsafe.Pointer(pRowid)) = 0 case int32(FTS5_PLAN_SOURCE): fallthrough case int32(FTS5_PLAN_MATCH): fallthrough case int32(FTS5_PLAN_SORTED_MATCH): *(*Tsqlite_int64)(unsafe.Pointer(pRowid)) = _fts5CursorRowid(tls, pCsr) default: *(*Tsqlite_int64)(unsafe.Pointer(pRowid)) = Xsqlite3_column_int64(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt, 0) break } return SQLITE_OK } // C documentation // // /* // ** If the cursor requires seeking (bSeekRequired flag is set), seek it. // ** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise. // ** // ** If argument bErrormsg is true and an error occurs, an error message may // ** be left in sqlite3_vtab.zErrMsg. // */ func _fts5SeekCursor(tls *libc.TLS, pCsr uintptr, bErrormsg int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var eStmt, rc int32 var pTab, pTab1, v1 uintptr _, _, _, _, _ = eStmt, pTab, pTab1, rc, v1 rc = SQLITE_OK /* If the cursor does not yet have a statement handle, obtain one now. */ if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt == uintptr(0) { pTab = (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab eStmt = _fts5StmtType(tls, pCsr) if bErrormsg != 0 { v1 = pTab + 8 } else { v1 = uintptr(0) } rc = _sqlite3Fts5StorageStmt(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).FpStorage, eStmt, pCsr+48, v1) _ = libc.Int32FromInt32(0) _ = libc.Int32FromInt32(0) } if rc == SQLITE_OK && (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fcsrflags&int32(FTS5CSR_REQUIRE_CONTENT) != 0 { pTab1 = (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab _ = libc.Int32FromInt32(0) Xsqlite3_reset(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt) Xsqlite3_bind_int64(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt, int32(1), _fts5CursorRowid(tls, pCsr)) (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer(pTab1)).FpConfig)).FbLock++ rc = Xsqlite3_step(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt) (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer(pTab1)).FpConfig)).FbLock-- if rc == int32(SQLITE_ROW) { rc = SQLITE_OK *(*int32)(unsafe.Pointer(pCsr + 60)) &= ^libc.Int32FromInt32(FTS5CSR_REQUIRE_CONTENT) } else { rc = Xsqlite3_reset(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt) if rc == SQLITE_OK { rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<